Repository navigation
fix(agent-status): register and handle Claude SessionEnd - #14376
brennanb2025 wants to merge 1 commit into
Conversation
Orca registered 11 Claude hook events but not SessionEnd, and normalizeClaudeEvent had no branch for it, so the event was dropped even if it arrived. A session that exits mid-turn emits no Stop, leaving the pane's non-done row standing until the 30-minute display decay -- which never clears the stored state at all. Register SessionEnd alongside the other lifecycle events and retire the pane on it: clear the subagent roster, running background tasks and session crons so resolveClaudePaneState cannot gate the pane back up, and land a session-boundary 'done' so completion-reactive consumers do not read it as a turn finishing. Unlike SessionStart this accepts every reason -- failing closed on an unknown one would forfeit the backstop the event exists to provide -- but a child-attributed SessionEnd is ignored so a subagent cannot retire the lead's turn. Live testing did not observe Claude Code 2.1.231 emitting SessionEnd on /exit, so this does not on its own resolve the stuck-status reports; see the PR body.
📝 WalkthroughWalkthroughClaude hook settings now include a Mergeability Score: 🔵 Low · up to The change registers SessionEnd and resolves qualifying session-end states to done, improving cleanup on supported exit paths. Mergeability is otherwise good, with limited follow-up needed to correct exit-path wording and strengthen regression coverage for task and cron cleanup. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/shared/agent-hook-listener-claude-session-end.test.ts (1)
39-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert cleanup of task and cron state.
This test only verifies roster cleanup. Populate
claudeRunningNonAgentTaskPaneKeysandclaudeActiveSessionCronPaneKeysbeforeSessionEnd. Assert that both collections no longer containpaneKeyafter normalization.Proposed test extension
claudeEvent(state, paneKey, { hook_event_name: 'SubagentStart', agent_id: 'achild-0000000000000011', agent_type: 'reviewer' }) + state.claudeRunningNonAgentTaskPaneKeys.add(paneKey) + state.claudeActiveSessionCronPaneKeys.add(paneKey) const ended = claudeEvent(state, paneKey, { hook_event_name: 'SessionEnd', reason: 'other' }) expect(ended?.payload.state).toBe('done') expect(state.claudeSubagentRosterByPaneKey.has(paneKey)).toBe(false) + expect(state.claudeRunningNonAgentTaskPaneKeys.has(paneKey)).toBe(false) + expect(state.claudeActiveSessionCronPaneKeys.has(paneKey)).toBe(false)
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 690ae8f7-7e69-4492-b19f-5c9ebd4d5340
📒 Files selected for processing (4)
src/main/claude/hook-service.test.tssrc/main/claude/hook-settings.tssrc/shared/agent-hook-listener-claude-session-end.test.tssrc/shared/agent-hook-listener.ts
| // Why: last line of defense for every missed Stop (STA-3149). A session that exits | ||
| // mid-turn — /exit, Ctrl+D, crash, interrupt — emits no Stop, so without this the pane | ||
| // keeps its non-done row indefinitely. Older Claude builds ignore it (StopFailure precedent). |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Do not state that SessionEnd covers exit paths that do not emit it.
src/main/claude/hook-settings.ts#L43-L45: state thatSessionEndhandles terminal paths that emitSessionEnd; remove/exit, Ctrl+D, crashes, and interrupts as covered examples.src/main/claude/hook-service.test.ts#L422-L423: state that remote panes benefit only when Claude emitsSessionEnd.src/shared/agent-hook-listener-claude-session-end.test.ts#L19-L20: describe the test as handling a receivedSessionEndafter a missingStop; do not claim it handles/exit, Ctrl+D, or crashes.
The PR objectives state that /exit, /clear, crashes, and forced termination do not emit SessionEnd. As per coding guidelines, comments must be concise, non-obvious, and brief.
📍 Affects 3 files
src/main/claude/hook-settings.ts#L43-L45(this comment)src/main/claude/hook-service.test.ts#L422-L423src/shared/agent-hook-listener-claude-session-end.test.ts#L19-L20
Source: Coding guidelines
|
Closing this. It isn't worth landing on its own. Measured on Claude Code 2.1.231: Reviewing how comparable agent-workspace implementations handle this: registering a The right fix is that liveness backstop, which closes every exit path at once — The measured hook contract is preserved in the PR body above for whoever picks that up. Superseded by the real fix in #14375 for the reproduced case. |
ELI5
When a Claude session ends, it can tell Orca so — via a
SessionEndhook. Orca was never listening for it, so a session that quit mid-turn left its spinner running forever. This starts listening.It is not a complete safety net: two of the most common exit paths don't fire the event at all (upstream bugs, below). It strictly improves coverage on the paths that do work.
What Changed
src/main/claude/hook-settings.ts— registerSessionEndinCLAUDE_EVENTS(11 events → 12)src/shared/agent-hook-listener.ts— add theSessionEndbranch tonormalizeClaudeEventsrc/main/claude/hook-service.test.ts— pinSessionEndin the remote-install assertionsrc/shared/agent-hook-listener-claude-session-end.test.ts— new coverageOn
SessionEndthe pane is retired: clear the subagent roster, running background tasks, and session crons — otherwiseresolveClaudePaneStategates the pane straight back up toworking— then land a session-boundarydoneso notifications and automation runs don't read it as a turn completing.Two deliberate asymmetries vs
SessionStart:reason.SessionStartfails closed on unknown sources because it can fire mid-turn.SessionEndcannot: the session is over. The documented reasons areclear | resume | logout | prompt_input_exit | bypass_permissions_disabled | other, and failing closed on an unrecognized one would forfeit the point of a backstop.SessionEndcarryingagent_idis a subagent's and must not retire the lead's turn.Why
normalizeClaudeEventmaps unknown event names tonull, so before this changeSessionEndwas dropped twice over — never registered, and unhandled if it had arrived. Every other lifecycle-complete provider already registers it (devin/hook-settings.ts,grok/hook-service.ts,copilot/hook-service.ts).Consequence today: a session that exits mid-turn leaves its last non-
donerow standing.AGENT_STATUS_STALE_AFTER_MSonly decays the sidebar dot after 30 minutes; the stored row staysworkingand re-hydrates on next launch. Orca's own shell wrappers name this gap — the OSC-133 workaround is commented as covering "a stuck 'working' spinner for up to 30 min after the CLI exits without sending a Stop/SessionEnd hook" — and that workaround is disabled for SSH/WSL/remote PTYs.The SessionEnd contract, as measured on 2.1.231
I verified this rather than assuming it, because it determines what this PR can honestly claim.
-p) session endSessionStart,UserPromptSubmit,Stop,SessionEndall firedCtrl+D/exit/clearCtrl+CSessionStart:resumeTwo further constraints worth knowing:
SessionEndhooks share a ~1.5s budget (raisable via an explicit per-hooktimeout). Orca's managed hook shells out tocurlwith--connect-timeout 0.5 --max-time 1.5, which fits only if the shell spawn is fast. On a loaded machine this could be cut off. Not a regression — the current behavior is that nothing is sent at all — but it argues for an explicittimeouton this entry if we see flakiness.settings.jsonmid-session has no effect, so this only takes effect for sessions started after install.What this means for STA-3149: the ticket's premise — that registering
SessionEndis "the last line of defense for every missed Stop" — does not hold. The two most common interactive exits are exactly the ones that don't fire. This PR is a real but partial improvement; a complete backstop has to come from a liveness signal (process-exit / foreground-pgroup) rather than a hook. I'd suggest rewriting STA-3149 accordingly and tracking the liveness backstop separately.Linked Issue
Linear STA-3149 (premise needs correcting — see above). No GitHub issue exists.
Companion PR #14375 fixes a different mechanism behind the same symptom (start-less
SubagentStopminting a phantomworking); that one is reproduced and verified end to end. The two are independent.Upstream: anthropics/claude-code #17885 (
/exit), #6428 (/clear), #41577 (async work in SessionEnd hooks killed early).Visual Proof
N/A— no UI or copy change. The user-visible effect is an existing status row resolving todoneon session exit rather than persisting.Registration is observable in a fresh
~/.claude/settings.jsonwritten by this build:Testing
Automated — 5 new normalizer cases plus the remote-install assertion. Verified non-vacuous: removing the registration and the normalizer branch fails exactly 5 tests.
npx vitest— 1061 passed / 6 skipped across 64 agent-hook, agent-status, and claude filesoxlint+oxfmtclean (pre-commit hook)Manual (macOS, isolated dev instance, throwaway repo)
HOMEgetsSessionEndinsettings.json. ✅SessionStartlanded asessionBoundary: truedonerow. ✅working, exited with/exit. Process exited, PTY stayed alive, row stayedworking. ❌ — now explained:/exitdoes not emit the event (e2e: "ssh docker watcher isolation" is flaky and fails across unrelated SSH PRs #17885).SessionEnd; a pty-driven interactive session exited with/exitfired onlySessionStart.Note for reviewers: an earlier round of test 3 was invalid — I had hand-edited the managed
settings.jsonmid-session, which both broke hook delivery and, per the snapshot-at-start rule, could not have taken effect anyway. The runs above are the clean redo with a control.Review
SessionEndposts through the existing authenticated hook endpoint with the same token, paneKey validation, and fail-closed parsing as every other event.CLAUDE_EVENTSdrives the POSIX script, the Windows.cmd, and the PowerShell branch from one list, so all three hosts get the event with no per-platform code. Normalizer logic is platform-independent.done. A new host with an old client publishes an ordinarydone; a new client against an old host simply never receives the event, since old hosts drop unknown names. Remote panes benefit most, as the OSC-133 exit fallback is disabled for SSH/WSL PTYs. Covered by the updatedinstallRemotetest.StopFailureprecedent), andapplyManagedHooksinstalls idempotently, so existing configs converge on next install.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred) — targeted lint/tests run locally as listed; full build and remaining typecheck projects left to CIAuthor