@@ -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"]);
5455const TEST_COMMAND = / \b ( n p m | p n p m | y a r n ) ( r u n ) ? t e s t \b | \b n o d e - - t e s t \b | \b ( v i t e s t | j e s t | p y t e s t ) \b / ;
5556// Exit codes can hide behind a pipe (`npm test | tail`), so the output is checked too.
5657const TEST_FAILED = / ^ # f a i l [ 1 - 9 ] | \b [ 1 - 9 ] \d * ( f a i l e d | f a i l i n g ) \b | ^ F A I L \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 * ( n o t l o g g e d i n | i n v a l i d a p i k e y ) \b .* p l e a s e r u n \/ l o g i n \s * $ / i;
5761
5862// Results can arrive out of order, so a tool_result is matched to its tool_use by id.
5963export 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 }
0 commit comments