{"record":{"id":"056874fb8a0bf57c","repo":"can1357/oh-my-pi","slug":"ai-staging-request-failed-response-errormessage","errorCode":null,"errorMessage":"AI staging request failed: ${response.errorMessage ?? \"unknown error\"}","messagePattern":"AI staging request failed: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/cli/git-tui/ai-stage.ts","lineNumber":235,"sourceCode":"\n/** One text completion against the resolved model. */\nfunction createCompleter(\n\tmodel: Model<Api>,\n\tapiKey: ApiKey,\n\tsignal?: AbortSignal,\n): (userPrompt: string) => Promise<string> {\n\treturn async userPrompt => {\n\t\tconst response = await retryTransientCompletion(\n\t\t\t() =>\n\t\t\t\tcompleteSimple(\n\t\t\t\t\tmodel,\n\t\t\t\t\t{ messages: [{ role: \"user\", content: userPrompt, timestamp: Date.now() }] },\n\t\t\t\t\t{ apiKey, maxTokens: SAFE_MAX_TOKENS, temperature: 0, disableReasoning: true, signal },\n\t\t\t\t),\n\t\t\t{ signal },\n\t\t);\n\t\tif (response.stopReason === \"error\") {\n\t\t\tthrow new Error(`AI staging request failed: ${response.errorMessage ?? \"unknown error\"}`);\n\t\t}\n\t\treturn extractText(response.content);\n\t};\n}\n\n/**\n * Fan out one judgement per item. A failed judgement rejects just its item so\n * one flaky request cannot sink the run — unless every item failed, which\n * means the backend is broken and the first error surfaces.\n */\nasync function judgeAll<T>(items: readonly T[], run: (item: T) => Promise<boolean>): Promise<boolean[]> {\n\tlet failures = 0;\n\tlet firstError: unknown;\n\tconst verdicts = await Promise.all(\n\t\titems.map(async item => {\n\t\t\ttry {\n\t\t\t\treturn await run(item);\n\t\t\t} catch (error) {","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/cli/git-tui/ai-stage.ts#L217-L253","documentation":"The completer sends the user prompt plus file diffs to the selected model and checks the response's stopReason. A stopReason of \"error\" means the provider request failed; the raw errorMessage (or \"unknown error\") is wrapped in this error. This surfaces upstream API failures (rate limits, invalid key, server errors, aborts) to the AI-stage caller.","triggerScenarios":"The LLM stream completes with response.stopReason === \"error\" — e.g. HTTP 401 (invalid key), 429 (rate limit), 5xx provider outage, network failure mid-request, or a signal abort reported as an error.","commonSituations":"Expired or revoked API key, exceeding provider rate limits with large diffs, provider outage, or corporate proxy blocking the endpoint.","solutions":["Read the wrapped errorMessage to identify the cause (401 → fix key, 429 → retry later).","Retry the staging command; transient rate limits and outages resolve on their own.","Re-authenticate or update the provider API key.","Reduce diff size (stage fewer files at once) if the request is being rejected for size/token limits."],"exampleFix":"// before\nawait aiStage(opts); // surfaces raw provider error\n// after\ntry { await aiStage(opts); }\ncatch (e) {\n  if (String(e.message).includes(\"429\")) await Bun.sleep(5000), retry();\n  else throw e;\n}","handlingStrategy":"retry","validationCode":"// preflight reachability/auth\nconst res = await fetch(`${providerBaseUrl}/models`, { headers: { Authorization: `Bearer ${apiKey}` } });\nif (!res.ok) throw new Error(`Provider preflight failed: ${res.status}`);","typeGuard":null,"tryCatchPattern":"try {\n  await aiStage(opts);\n} catch (e) {\n  const msg = e instanceof Error ? e.message : \"\";\n  if (msg.includes(\"AI staging request failed\") && (msg.includes(\"429\") || msg.includes(\"503\"))) {\n    await Bun.sleep(5000);\n    return aiStage(opts); // one retry for transient errors\n  }\n  throw e;\n}","preventionTips":["Keep API keys valid/rotated to avoid 401s.","Add exponential backoff for 429/5xx in automated staging flows.","Keep diffs small; split large change sets into multiple staging runs.","Verify network/proxy access to the provider endpoint in CI."],"tags":["llm","network","api-error","retry"],"backgroundTag":"llm-request-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}