fix(runner): poll for the 401 — auth-failure detection was racing the transcript

The prior unconditional detectAuthFailure check (dade32b) still missed forced
401s in verification: on a fresh/slow agent launch the "/login" error lands in
the transcript AFTER the generate-verify window, so a single point-in-time check
runs too early and returns false — the dispatch was acked "ok / isn't generating
yet" instead of an honest auth failure.

Poll detectAuthFailure for up to ~12s when the agent didn't verifiably generate
(the already-failed path — no latency added to real successes, which check once
and return false). On a hit, force ok=false + the actionable error. Only emit the
soft "isn't generating yet" warning when it is NOT an auth failure.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016g8LdPdzoEbVwQ1SdYW5RY
This commit is contained in:
Mao NakamotoandClaude Opus 4.8 committed 2026-07-10 08:51:25 +02:00
1 parent dade32b16b
commit 972d03c6f0
1 file changed
+18 -12
+18 -12
View File
@@ -622,25 +622,31 @@ async function handleCommand(
ok = true
workspaceId = runnerWorkspaceId(tab)
text = launched ? `launched ${agent} (pty) + injected` : `injected to running ${agent} (pty)`
if (!verified) {
warning = detectAuthFailure(dir)
? `${text}, but the agent reports dead credentials (401/login required) — run 'claude setup-token' on the runner host`
: `${text}, but the agent isn't generating yet — it may still be booting or already idle`
// Auth failure is a HARD failure, and it must WIN over a "verified"
// success: a 401 emits the "/login" error, which counts as output
// and can false-positive waitForAgentGenerating — so a dispatch that
// never ran was being acked ok/verified with the UI cheerfully
// saying "starting shortly" (dogfood 2026-07-10: dispatches 401'd
// while every layer reported success). The 401 also lands in the
// transcript AFTER the generate-verify window on a fresh/slow launch,
// so a single check races it — poll briefly when the agent didn't
// verifiably generate. On a real success one check returns false.
let authFailed = detectAuthFailure(dir)
if (!authFailed && !verified) {
for (let i = 0; i < 6 && !authFailed; i++) {
await asleep(2000)
authFailed = detectAuthFailure(dir)
}
}
// Auth failure is a HARD failure even when verify passed. A 401
// emits the "/login" error, which flips the agent off "idle" and
// false-positives waitForAgentGenerating — so a dispatch that never
// ran gets acked ok/verified and the UI cheerfully says "starting
// shortly" (dogfood 2026-07-10: three dispatches 401'd while every
// layer reported success). Re-check the fresh transcript here and
// turn a dead-credentials dispatch into an honest, actionable fail.
if (detectAuthFailure(dir)) {
if (authFailed) {
ok = false
verified = false
warning = undefined
error =
`${agent} is not authenticated (401 / login required) — the prompt was delivered but the agent can't run. ` +
`On the runner host, remove any stale ~/.claude/.credentials.json and set CLAUDE_CODE_OAUTH_TOKEN (claude setup-token).`
} else if (!verified) {
warning = `${text}, but the agent isn't generating yet — it may still be booting or already idle`
}
break
}