fix(desktop): ignore Claude subagent hook events in notify hook

Merged
KI
3mo ago
+31-3

Problem

Claude Code (2.1.x) now identifies subagent context in hook payloads: hooks fired inside a Task-tool subagent carry an agent_id field (absent in main-loop hooks; session_id is identical for both, so it could never distinguish them).

Superset registers PostToolUse/PostToolUseFailure/PermissionRequest with matcher: *, so every tool call made by a subagent also dispatched to host-service. mapEventType("PostToolUse") → "Start" means a background subagent flips the terminal agent binding back to working after the main loop already stopped — status flap and spurious "agent active" signal while the session is actually idle waiting on a task notification.

Fix

notify-hook.template.sh bails out immediately when the payload carries a non-empty agent_id — only main-loop lifecycle events drive agent status and notifications. Marker bumped v7 → v8. Scoped to snake_case agent_id (Claude schema and forks that mirror it); Grok's camelCase envelope and Codex's payload shape are unaffected.

Tests

  • notify-hook.test.ts: subagent payload (agent_id present) produces no dispatch; main-loop payload still dispatches. Mutation-checked: with the guard stripped, the subagent payload dispatches (test fails).

E2E verification (dev desktop from this branch, CDP-driven)

Real journey: dev app booted from this worktree (regenerated the v8 script at startup), opened this workspace, created a terminal (SUPERSET_TERMINAL_ID=2c4e7fbe…, SUPERSET_HOME_DIR=<worktree>/superset-dev-data confirmed in-pane), ran interactive Claude Code 2.1.229 spawning a background subagent (3 × sleep 4 && echo), polling the terminal_agent_bindings row in host.db each second plus the notify-hook debug log.

Before (guard stripped from the generated script, same journey):

23:38:52 Start → 23:38:54 PostToolUse (Task spawn) → 23:38:55 Stop (main turn ends)
23:39:01 / 23:39:06 / 23:39:11 PostToolUse  ← subagent bash steps, AFTER Stop, all HTTP 200
binding last_event_type: Stop → Start@23:39:01 (flapped back to working while idle)

After (v8 guarded script, same journey, same terminal, same session):

23:40:13 Start → 23:40:15 PostToolUse (Task spawn) → 23:40:16 Stop
— 17s of silence while the subagent runs —
23:40:33 Start / 23:40:35 Stop (genuine task-notification turn)
binding last_event_type: held Stop through the entire subagent window

Binding-state polls and dispatch logs are end-to-end; screenshots captured of the session transcript showing the backgrounded agent. Unit tests are synthetic.

Caveat

Subagent-fired PermissionRequest events are also skipped: if a subagent blocks on a permission prompt, the terminal won't be flagged as needing input. Intentional per "only main agent", and rare (subagents inherit main-loop permission mode), but flag if you want that event exempted.


Summary by cubic

Ignore Claude subagent hook events in the desktop notify.sh so subagent activity no longer flips terminal status. Previously, subagent hooks were dispatched and PostToolUse mapped to Start after a main-loop Stop; now the script exits early when agent_id is present, so only main-loop events drive status and notifications.

  • Marker bump v7→v8 to force regeneration on startup; tests updated to cover ignore and main-loop behavior.
  • Scope: checks snake_case agent_id (Claude schema); Grok/camelCase and Codex payloads are unaffected.
  • Side effect: subagent PermissionRequest events are skipped by design; only the main agent can flag input-needed.
  • Migration: no action required; the desktop app regenerates the v8 notify script on launch.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented subagent activity from generating terminal-level notifications.
    • Main-agent notifications continue to be dispatched normally.
    • Updated notification hook versioning and validation coverage.