Fix agent hooks misrouting notifications/status/summary to the focused tab

Merged
WO
5mo ago
+633-62

Problem

With several agent (Claude Code) sessions open in the sidebar, a background session's notification, "Needs input" status, and summary would land on an unrelated session — irregularly.

Repro that makes it obvious: open the same folder in multiple tabs. Because macOS reuses ttysNNN device names across those panes, the old first-match TTY resolver would route one session's alert to a same-folder sibling pane; and when a session's workspace couldn't be validated, routing fell back to workspace.current (the focused tab), which was usually another same-folder tab. So a background session's alerts appeared to "follow the folder" onto the wrong tab, seemingly at random. cmux never actually routes by cwd — it just looked that way.

Root cause

Claude hooks resolve the target workspace from the per-session_id record, CMUX_WORKSPACE_ID, and a caller-TTY match. resolvePreferredWorkspaceIdForClaudeHook had two defects:

  1. It committed to the first non-empty candidate and, if that failed validation (stale/recreated workspace), never tried the live CMUX_WORKSPACE_ID before dropping through.
  2. The ultimate fallback was workspace.current, i.e. whatever tab is focused.

The (wrong) resolved workspace was also stamped into activeSessionsByWorkspace, so the pollution persisted: one session_id became the "active" session for tabs it doesn't own — stealing their alerts and marking their own events stale. (Confirmed on a live machine: one session registered as active for 3 different workspaces.) The caller-TTY fallback also used first-match on the reused ttysNNN name.

Fix

  • resolvePreferredWorkspaceIdForClaudeHook resolves strictly: recorded workspace → live CMUX_WORKSPACE_ID → an unambiguous caller-TTY binding, each validated against a live workspace and required to be a UUID (so a non-UUID value can't slip through resolveWorkspaceId into workspace.current). Returns nil (graceful no-op) instead of falling back to the focused tab.
  • All Claude hook call sites treat an unresolved workspace as a no-op (localized OK).
  • uniqueCallerWorkspaceIdForClaudeHook refuses to guess when a reused TTY name maps to more than one workspace — the multiple-tabs-same-folder case.
  • ClaudeHookSessionStore self-heals cross-workspace activeSessionsByWorkspace pointers, cleaning pollution left by older builds.

The generic-agent hook path already no-ops instead of guessing; this brings Claude in line.

Tests

Two-commit regression policy (failing test commit, then fix). Behavior-level tests drive the bundled cmux CLI against a mock socket:

  • notification falls back to the live CMUX_WORKSPACE_ID, not workspace.current
  • ambiguous TTY match no-ops instead of first-match guessing
  • polluted activeSessionsByWorkspace pointer is self-healed

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Claude hook routing now resolves workspaces more strictly, reducing accidental cross-workspace actions.
    • Notifications and session events now honor explicit workspace selection and safer TTY-based identification.
  • Bug Fixes

    • Prevented routing to the wrong workspace when session data is stale, ambiguous, or polluted.
    • Improved cleanup of active session state so incorrect workspace pointers are removed automatically.
    • When a workspace can’t be identified confidently, the CLI now exits cleanly instead of guessing.