Skip to content

fix(sidebar): keep the working spinner while a Claude Monitor runs - #8853

Closed
nwparker wants to merge 2 commits into
mainfrom
nwparker/sidebar-monitor-status
Closed

nwparker wants to merge 2 commits into
mainfrom
nwparker/sidebar-monitor-status

Conversation

@nwparker

@nwparker nwparker commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

What & why

When a Claude agent ends its turn with a background Monitor still running, the left-sidebar agent card dropped to the static "done" dot instead of the working spinner — even though the session is mid-flight and will resume when the monitor fires. Reported from a real session: the footer read "1 monitor still running" while the agent's sidebar card showed the static done dot.

This makes the card show the working spinner while a Monitor is watching, reusing the exact mechanism that already keeps a card spinning while a subagent runs.

Root cause

The Stop hook payload carries a background_tasks array. A Monitor is a non-agent background-task kind (Claude's task kinds are subagent, workflow, shell, monitor, MCP task, teammate, …). readClaudeBackgroundAgentTasks keeps only subagent/teammate entries and discards the rest, so the monitor never entered the roster — the "stay-working" gate in agent-hook-listener.ts then saw an empty roster and resolved the pane to done:

Stop hook (background_tasks: [{ type: "monitor", status: "running" }])
  -> readClaudeBackgroundAgentTasks() drops the monitor (agent-only filter)
  -> roster empty -> effectiveState = 'done'   (NOT 'working')
  -> activity summary: hasLiveDone -> resolveWorktreeStatus 'done'
  -> StatusIndicator renders the static emerald dot, no spinner

The fix

  • claudeBackgroundTasksHaveRunningMonitor() detects a running monitor in background_tasks (matched on the monitor type prefix, so monitor_ws/monitor_mcp variants gate too; shells/workflows/crons deliberately excluded).
  • Tracked per-pane (claudeRunningMonitorByPaneKey), updated only from a present background_tasks list so an event without the field can't clear a monitor tracked by an earlier turn (mirrors how the roster is retained).
  • OR'd into the same three done -> working gates that already hold a pane working while a subagent runs.

Everything
downstream (activity summary -> status resolver -> StatusIndicator) already promotes a working pane into the spinner, so there are no rendering changes. It self-heals to done on the next Stop that no longer lists the monitor as running.

Evidence

Before/after (proves the fix is load-bearing). With the monitor check temporarily neutered to a no-op, the 3 new integration tests — which drive the real normalizeHookPayload entry point — reproduce the bug exactly:

 ×  reports Stop as working while a Monitor background task is still running
    -> expected 'done' to be 'working'
 ×  retains a running Monitor across a later Stop whose payload omits background_tasks
    -> expected 'done' to be 'working'
 ×  reports Stop as working while a Monitor runs alongside a finished subagent
    -> expected 'done' to be 'working'
 Test Files  1 failed (1)

With the fix in place, the same tests pass (state resolves to working, which is what renders the spinner):

 ✓  detects a running Monitor background task (incl. ws/mcp variants)
 ✓  ignores finished monitors and non-monitor running tasks
 ✓  reports Stop as working while a Monitor background task is still running
 ✓  retains a running Monitor across a later Stop whose payload omits background_tasks
 ✓  reports Stop as working while a Monitor runs alongside a finished subagent

Full affected suite + typecheck (fix on):

 Test Files  2 passed (2)
      Tests  124 passed (124)     # 11 new (integration + pure-function)

tsc --noEmit -p config/tsconfig.node.json  -> exit 0
oxlint (changed files)                     -> clean

Test plan

  • Unit: claudeBackgroundTasksHaveRunningMonitor — running monitor (incl. monitor_ws/monitor_mcp), completed monitor, running shell/subagent (excluded), absent/malformed field.
  • Integration (normalizeHookPayload): Stop with a running monitor -> working; monitor completes on next Stop -> done
    ; running monitor retained across a Stop whose payload omits background_tasks; monitor alongside a finished subagent -> working.
  • tsc + oxlint clean.

Notes / caveats

  • Wire-type confirmation: the type string is matched on the monitor prefix, inferred from the Claude Code build's task-kind list; if a live capture shows a different value it's a one-line tweak (flagged in a code comment).
  • Long monitors: agent status staleness-expires after ~30 min, so a long-timeout monitor that emits no intermediate hooks could stop spinning before it fires. Acceptable for this pass; a full fix would need periodic re-emits.
  • One honest trade-off: since Claude drops kind, a Monitor is byte-identical to a plain run_in_background shell in the payload — so a backgrounded dev server will also hold the spinner until it exits (or the existing 30-min status staleness decays it). There's no signal in the payload to separate the two.

Made with Orca 🐋

When a Claude agent ends its turn with a background Monitor still running,
the sidebar card dropped to the static "done" dot instead of showing the
working spinner — even though the session is mid-flight and will resume
when the monitor fires.

Root cause: the Stop hook's `background_tasks` array carries monitors as a
non-agent task kind, and `readClaudeBackgroundAgentTasks` keeps only
subagent/teammate entries. With the monitor discarded, the "stay-working"
gate saw an empty roster and resolved the pane to `done`.

Fix: detect a running Monitor in `background_tasks`
(`claudeBackgroundTasksHaveRunningMonitor`), track it per-pane, and OR it
into the same three done->working gates that already hold a pane working
while a subagent runs. Everything downstream (activity summary -> status
resolver -> StatusIndicator) already promotes a `working` pane into the
spinner, so there are no rendering changes. The flag is updated only from a
present `background_tasks` list (so an event without the field can't clear a
monitor tracked earlier) and self-heals to `done` on the next Stop that no
longer lists the monitor as running.
@coderabbitai

coderabbitai Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Claude hook handling now detects running or pending shell and monitor entries in background_tasks, stores that state per pane, and clears it with listener cache state. Status normalization treats panes with running tasks as working alongside active subagents, including child-driven and restored-from-wait paths. Tests cover detection, cache retention when task data is omitted, completion, mixed subagent activity, and exclusion of background tasks from subagents rows.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the change well, but it omits required template sections like Screenshots, AI Review Report, and Security Audit. Add the missing template sections, especially Screenshots, a detailed AI Review Report with cross-platform checks, and a Security Audit summary.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and matches the main user-visible change: keeping the sidebar spinner active for a Claude Monitor.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…nitor"

The prior commit gated the working spinner on background_tasks entries with
type starting "monitor", but the Monitor tool never surfaces that way. It
registers its watch as a backgrounded shell, so Claude's Stop-hook payload
reports it as { type: "shell", status: "running" } — the internal
kind: "monitor" is dropped. The predicate never matched, the done-gate never
fired, and the pane still resolved to done while a monitor was running.

Verified against a live 2.1.211 Stop payload:
  background_tasks: [{ id, type: "shell", status: "running",
                      description: "sleep 300 background command",
                      command: "sleep 300" }]

Match type "shell" (the Monitor tool, indistinguishable from a plain
backgrounded shell once kind is dropped) plus "monitor" (ws/mcp monitors),
with status running|pending. Rename claudeBackgroundTasksHaveRunningMonitor ->
...ShellOrMonitor and the pane flag/helpers to match. Subagent/teammate types
stay excluded — the roster owns them and teammate entries read "running"
forever. Tests use the real shell shape.
@nwparker

Copy link
Copy Markdown
Contributor Author

Why the spinner still stopped — and the fix

The first commit gated the spinner on background_tasks entries whose type starts with "monitor". The Monitor tool never appears that way. It registers its watch as a backgrounded shell, and Claude's payload builder maps that to type: "shell" and drops the internal kind: "monitor". So the predicate never matched → the done-gate never fired → the pane still resolved to done while a monitor ran.

Evidence — a live Stop payload captured from Claude Code 2.1.211 with a Monitor running

"background_tasks":[{"id":"b32v7rxmz","type":"shell","status":"running",
                    "description":"sleep 300 background command","command":"sleep 300"}]

Run against the two predicates:

old (type.startsWith("monitor") && running) : false  -> spinner STOPS  ❌ (the reported bug)
new (type∈{shell,monitor} && running|pending): true   -> spinner KEEPS working  ✅

Fix

Match type ∈ {"shell","monitor"} with status ∈ {running, pending} (a Monitor is byte-identical to a plain backgrounded shell once kind is dropped; ws/mcp monitors surface as "monitor"). Renamed claudeBackgroundTasksHaveRunningMonitor → …ShellOrMonitor and the pane flag/helpers to match. Subagent/teammate types stay excluded — the roster owns them and teammate entries read "running" forever. Tests now use the real shell shape.

Known trade-off: because kind is dropped, a plain run_in_background shell (e.g. a dev server) is indistinguishable from a Monitor and will also hold the spinner until it exits or the 30-min status staleness decays it. There is no signal in the payload to separate the two.

✅ tests pass · ✅ node + web typecheck · ✅ oxlint/oxfmt clean

@nwparker

nwparker commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #11838 (preserve Claude background work). That merge cites this as a prior attempt and covers non-agent background_tasks (monitors/shells) so the sidebar stays working until background work clears — same defect this PR was after.

@nwparker nwparker closed this Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant