Skip to content

Commit 5f21ecc

Browse files
Merge pull request #9 from devweekends/feat/claude-login-failure
runner: classify Claude login failures
2 parents 4bf58fb + 1b0d068 commit 5f21ecc

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎runner/src/agents/claude-code.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export function claudeResumeArgs(sessionId: string, prompt: string, budgetUsd =
4242
// assistant tool_use Edit/Write -> file.changed
4343
// assistant tool_use Bash/PowerShell -> command.started (Claude uses PowerShell on Windows)
4444
// user tool_result for a command -> command.finished (+ test.result for a test command)
45-
// result -> task.finished, or task.failed when it is an error.
45+
// result -> task.finished, or task.failed when it is an error
46+
// or a login failure reported as success.
4647
// total_cost_usd and usage exactly as given
4748
// Skipped: rate_limit_event (can come before init), thinking, other system lines.
4849
// 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"]);
5455
const TEST_COMMAND = /\b(npm|pnpm|yarn)( run)? test\b|\bnode --test\b|\b(vitest|jest|pytest)\b/;
5556
// Exit codes can hide behind a pipe (`npm test | tail`), so the output is checked too.
5657
const TEST_FAILED = /^# fail [1-9]|\b[1-9]\d* (failed|failing)\b|^FAIL\b/m;
58+
// Claude can report a login problem as a "success" result. Match only that whole message,
59+
// so a summary that merely mentions login ("users who are not logged in …") still finishes.
60+
const AUTH_FAILURE = /^\s*(not logged in|invalid api key)\b.*please run \/login\s*$/i;
5761

5862
// Results can arrive out of order, so a tool_result is matched to its tool_use by id.
5963
export interface ClaudeParseState {
@@ -123,14 +127,16 @@ export function parseClaudeLine(line: string, seq: EventSequence, state = newCla
123127
}
124128

125129
if (o.type === "result") {
126-
const failed = o.is_error === true || o.subtype !== "success";
130+
const summary = typeof o.result === "string" ? o.result : "";
131+
const failed = o.is_error === true || o.subtype !== "success" || AUTH_FAILURE.test(summary);
127132
return [seq.next(failed ? "task.failed" : "task.finished", {
128133
subtype: o.subtype,
129134
total_cost_usd: o.total_cost_usd,
130135
duration_ms: o.duration_ms,
131136
num_turns: o.num_turns,
132137
usage: o.usage,
133138
result: o.result,
139+
reason: failed ? summary || o.subtype : undefined,
134140
agentSessionId: o.session_id,
135141
}, o)];
136142
}

‎runner/test/claude-parser.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ describe("parseClaudeLine", () => {
7171
expect(e!.payload.total_cost_usd).toBe(0.5);
7272
});
7373

74+
it("falls back to the subtype as reason when an error result has no text", () => {
75+
const [e] = replay([JSON.stringify({ type: "result", subtype: "error_max_budget_usd", is_error: true })]);
76+
expect(e!.payload.reason).toBe("error_max_budget_usd");
77+
});
78+
79+
it("maps a login failure reported as success to task.failed", () => {
80+
const result = "Not logged in · Please run /login";
81+
const [e] = replay([JSON.stringify({ type: "result", subtype: "success", is_error: false, result })]);
82+
expect(e!.type).toBe("task.failed");
83+
expect(e!.payload.reason).toBe(result);
84+
});
85+
86+
it("keeps a successful summary that only mentions login as task.finished", () => {
87+
const result = "Fixed the redirect so users who are not logged in go to /signin. Please run /login to test.";
88+
const [e] = replay([JSON.stringify({ type: "result", subtype: "success", is_error: false, result })]);
89+
expect(e!.type).toBe("task.finished");
90+
expect(e!.payload.reason).toBeUndefined();
91+
});
92+
7493
it("turns a malformed or half-finished line into a message with the raw text, and skips blank lines", () => {
7594
const half = lines[1]!.slice(0, 40);
7695
const out = replay(["not json", half, "", " "]);

0 commit comments

Comments
 (0)