agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

bug(agent-claude-code): session stuck working/active after a turn dies without its Stop hook (login expired) — observer skips claude-code, no TerminalActivityDetector

Open AgentWrapper opened this issue 3 months ago • 0 comments

Bug

A claude-code worker session (live: agent-orchestrator-139) shows status=working / activity=active indefinitely while the Claude CLI is provably idle at its composer. The last turn aborted on an expired OAuth login (Login expired · Please run /login, Worked for 0s), so the turn's Stop hook never fired — and nothing else ever demotes the stale active.

Source: live observation of session agent-orchestrator-139 (tmux pane capture) | Reported by: maintainer via orchestrator task | Analyzed against: 0e472e2b2 Confidence: High

Reproduction

  1. Spawn a claude-code TUI worker; let a turn start (user-prompt-submit → activity=active).
  2. Kill the turn without a Stop hook — auth expiry mid-stream reproduces it (turn ends with Login expired · Please run /login, ✻ Worked for 0s); a CLI crash or network drop behaves the same.
  3. Observe ao session get: status stays working, activity stays active forever. Any further Enter on the logged-out CLI re-fires user-prompt-submit and re-asserts active (which is why updated advances on each retry while nothing runs).

Root Cause

Claude Code's durable activity is hook-driven only (backend/internal/adapters/agent/claudecode/activity.go → lifecycle.ApplyActivitySignal). The only transitions out of active are the stop / notification(idle_prompt) / session-end hooks. A turn that dies mid-stream emits none of them.

AO already has the exact safety net for this: the activity observer (backend/internal/observe/activity/observer.go:103-131) polls every 30s and, for sessions stuck active past DefaultStaleAfter (2 min), screen-proves the real state via the adapter's TerminalActivityDetector. But the claude-code adapter never implemented DetectTerminalActivity, so reconcile() returns at the type assertion (observer.go:111-114) and claude-code sessions are exempt from reconciliation entirely. (The adapter has InspectTerminalSurface — the classification logic exists — it just isn't wired to the detector port.) The prior test TestPollLeavesOtherHarnessesUntouched even pinned claude-code as the example of a skipped harness.

Verified against the live pane: InspectTerminalSurface on the stuck screen (login-expired footer, staged draft in the composer, no active chrome) correctly yields Work=Idle — the observer would have converged the session within one tick if the detector had been wired.

Hypotheses ruled out: hook/classifier precedence is not involved (there is simply no classifier signal), and nothing refreshes updated unconditionally (it stayed frozen at 15:15:56Z for 6+ minutes of observation; it only advances when the user re-submits on the logged-out CLI).

Fix

Implement DetectTerminalActivity on the claude-code plugin, delegating to InspectTerminalSurface — authoritative idle only when the surface positively shows an idle composer (same pattern codex shipped for #3115). Claude stays non-continuous: hooks remain authoritative while they flow; the observer only reconciles a stale active.

Impact

  • Users see a phantom "Working" session and wait for output that never comes; the logged-out state is hidden.
  • Orchestrators treat the worker as busy and won't dispatch or escalate.
  • Class bug: ANY hook turn-death (auth expiry, crash, network) strands claude-code in working — until now, permanently.

Related

  • #3387 — same class for Kimi; documents that only codex implemented TerminalActivityDetector (this issue is the claude-code instance)
  • #3115 — codex instance (fixed by codex's terminal_activity.go)
  • #4048 — cline instance (fixed via continuous reconciliation)
  • #2047 — broader claude activity misreporting

AgentWrapper avatar Aug 19 '26 15:08 AgentWrapper