diff --git a/runner/src/agents/claude-code.ts b/runner/src/agents/claude-code.ts index d0c4c45..b8665b3 100644 --- a/runner/src/agents/claude-code.ts +++ b/runner/src/agents/claude-code.ts @@ -42,7 +42,8 @@ export function claudeResumeArgs(sessionId: string, prompt: string, budgetUsd = // assistant tool_use Edit/Write -> file.changed // assistant tool_use Bash/PowerShell -> command.started (Claude uses PowerShell on Windows) // user tool_result for a command -> command.finished (+ test.result for a test command) -// result -> task.finished, or task.failed when it is an error. +// result -> task.finished, or task.failed when it is an error +// or a login failure reported as success. // total_cost_usd and usage exactly as given // Skipped: rate_limit_event (can come before init), thinking, other system lines. // A line that is not JSON becomes a `message` with the raw text. Always keep `raw`. @@ -54,6 +55,9 @@ const COMMAND_TOOLS = new Set(["Bash", "PowerShell"]); const TEST_COMMAND = /\b(npm|pnpm|yarn)( run)? test\b|\bnode --test\b|\b(vitest|jest|pytest)\b/; // Exit codes can hide behind a pipe (`npm test | tail`), so the output is checked too. const TEST_FAILED = /^# fail [1-9]|\b[1-9]\d* (failed|failing)\b|^FAIL\b/m; +// Claude can report a login problem as a "success" result. Match only that whole message, +// so a summary that merely mentions login ("users who are not logged in …") still finishes. +const AUTH_FAILURE = /^\s*(not logged in|invalid api key)\b.*please run \/login\s*$/i; // Results can arrive out of order, so a tool_result is matched to its tool_use by id. export interface ClaudeParseState { @@ -123,7 +127,8 @@ export function parseClaudeLine(line: string, seq: EventSequence, state = newCla } if (o.type === "result") { - const failed = o.is_error === true || o.subtype !== "success"; + const summary = typeof o.result === "string" ? o.result : ""; + const failed = o.is_error === true || o.subtype !== "success" || AUTH_FAILURE.test(summary); return [seq.next(failed ? "task.failed" : "task.finished", { subtype: o.subtype, total_cost_usd: o.total_cost_usd, @@ -131,6 +136,7 @@ export function parseClaudeLine(line: string, seq: EventSequence, state = newCla num_turns: o.num_turns, usage: o.usage, result: o.result, + reason: failed ? summary || o.subtype : undefined, agentSessionId: o.session_id, }, o)]; } diff --git a/runner/test/claude-parser.test.ts b/runner/test/claude-parser.test.ts index 69433f5..3591f1e 100644 --- a/runner/test/claude-parser.test.ts +++ b/runner/test/claude-parser.test.ts @@ -71,6 +71,25 @@ describe("parseClaudeLine", () => { expect(e!.payload.total_cost_usd).toBe(0.5); }); + it("falls back to the subtype as reason when an error result has no text", () => { + const [e] = replay([JSON.stringify({ type: "result", subtype: "error_max_budget_usd", is_error: true })]); + expect(e!.payload.reason).toBe("error_max_budget_usd"); + }); + + it("maps a login failure reported as success to task.failed", () => { + const result = "Not logged in · Please run /login"; + const [e] = replay([JSON.stringify({ type: "result", subtype: "success", is_error: false, result })]); + expect(e!.type).toBe("task.failed"); + expect(e!.payload.reason).toBe(result); + }); + + it("keeps a successful summary that only mentions login as task.finished", () => { + const result = "Fixed the redirect so users who are not logged in go to /signin. Please run /login to test."; + const [e] = replay([JSON.stringify({ type: "result", subtype: "success", is_error: false, result })]); + expect(e!.type).toBe("task.finished"); + expect(e!.payload.reason).toBeUndefined(); + }); + it("turns a malformed or half-finished line into a message with the raw text, and skips blank lines", () => { const half = lines[1]!.slice(0, 40); const out = replay(["not json", half, "", " "]);