{"record":{"id":"e2e7cb2040841c68","repo":"can1357/oh-my-pi","slug":"claude-command-failed-before-v1-messages-complete","errorCode":null,"errorMessage":"Claude command failed before /v1/messages completed: ${errorMessage(first.error)}${outputSuffix()}","messagePattern":"Claude command failed before /v1/messages completed: (.+?)(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/cli/claude-trace-cli.ts","lineNumber":777,"sourceCode":"\t\tconst ptyRace = runPromise.then(\n\t\t\t() => ({ kind: \"pty-exit\" as const }),\n\t\t\terror => ({ kind: \"pty-error\" as const, error }),\n\t\t);\n\t\tconst first = await Promise.race([captureRace, ptyRace]);\n\t\tif (first.kind === \"capture\") {\n\t\t\tawait shutdownPty(session, runPromise);\n\t\t\treturn first.exchange;\n\t\t}\n\t\tif (first.kind === \"capture-error\") {\n\t\t\tthrow new Error(`${errorMessage(first.error)}${outputSuffix()}`);\n\t\t}\n\t\tconst late = await Promise.race([captureRace, Bun.sleep(250).then(() => ({ kind: \"late-timeout\" as const }))]);\n\t\tif (late.kind === \"capture\") {\n\t\t\tawait shutdownPty(session, runPromise);\n\t\t\treturn late.exchange;\n\t\t}\n\t\tif (first.kind === \"pty-error\") {\n\t\t\tthrow new Error(\n\t\t\t\t`Claude command failed before /v1/messages completed: ${errorMessage(first.error)}${outputSuffix()}`,\n\t\t\t);\n\t\t}\n\t\tthrow new Error(`Claude command exited before /v1/messages completed${outputSuffix()}`);\n\t} finally {\n\t\tterminal.dispose();\n\t\tawait proxy.stop();\n\t}\n}\n\nexport async function runClaudeTraceCommand(args: ClaudeTraceCommandArgs = {}): Promise<void> {\n\tprocess.stderr.write(\n\t\t`Starting Claude trace proxy on ${args.host ?? DEFAULT_PROXY_HOST}:${args.port ?? DEFAULT_PROXY_PORT}\\n`,\n\t);\n\tconst exchange = await runClaudeMessagesCapture(args);\n\tconst output = args.json ? `${JSON.stringify(exchange, null, 2)}\\n` : formatCapturedMessagesExchange(exchange);\n\tprocess.stdout.write(output.endsWith(\"\\n\") ? output : `${output}\\n`);\n}","sourceCodeStart":759,"sourceCodeEnd":795,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/cli/claude-trace-cli.ts#L759-L795","documentation":"When the PTY running the Claude command reports an error (pty-error) before any /v1/messages exchange was captured — including after a 250ms grace wait for a late capture — the trace throws with the command's failure message plus captured output. It distinguishes 'Claude crashed/errored' from the plain-exit case of the next error.","triggerScenarios":"The spawned `claude` process inside the headless PTY exits with an error (bad auth, missing binary, crash, invalid API key) before completing a /v1/messages request that the MITM proxy could record.","commonSituations":"Expired or missing Anthropic credentials in the environment; `claude` CLI not on PATH or failing at startup; Claude Code hitting an immediate API error (401/403) before any exchange is captured; the one-word prompt rejected client-side.","solutions":["Inspect errorMessage(first.error) and the captured output appended by outputSuffix() for the claude CLI's own error text.","Verify `claude` runs standalone (`claude -p 'hi'`) with valid auth before retrying the trace.","Check ANTHROPIC_API_KEY / logged-in state in the environment the trace spawns claude with.","Increase --timeout or input delay if claude is failing before the injected prompt is delivered."],"exampleFix":"// before: trace fails because claude is unauthenticated\n$ omp claude-trace\n// after: authenticate first\n$ claude login && omp claude-trace","handlingStrategy":"validation","validationCode":"// verify claude works before tracing\nconst probe = await Bun.$`claude -p 'hi'`.quiet().nothrow();\nif (probe.exitCode !== 0) throw new Error(`claude CLI not usable: ${await probe.text()}`);","typeGuard":null,"tryCatchPattern":"try {\n  const exchange = await runTrace();\n} catch (err) {\n  if ((err as Error).message.includes(\"failed before /v1/messages completed\")) {\n    // fix auth/binary per the embedded error + captured output, then retry\n  } else throw err;\n}","preventionTips":["Run `claude -p 'hi'` standalone to confirm auth and binary health before each trace","Keep ANTHROPIC_API_KEY / login state valid in the environment the trace inherits","Match the spawned claude version with one known to work with the PTY driver"],"tags":["claude-code","pty","process","authentication"],"backgroundTag":"claude-command-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}