Fix sidebar conversation subtitle attributed to the wrong workspace (#7132)

Closed
AU
2mo ago
+687-76

Summary

Fixes #7132. In iMessage mode the sidebar subtitle falls through to Workspace.latestConversationMessage, which the app records strictly by the workspace_id on the Claude Stop feed event (TerminalController.v2ApplyIMessageModeSideEffects → handleAssistantFinalMessage). The reporter correctly traced that the app/store layer is keyed by the emitting workspaceId with no active/selected substitution, so the mis-attribution had to originate in the workspace_id the CLI stamps on the hook event.

Root cause (found in the CLI, not the env alone)

The reporter suspected CMUX_WORKSPACE_ID. The actual resolver (runClaudeHook) is richer than that: it resolves the workspace from a mapped session (session store), a caller TTY/PID binding, the caller's CMUX_WORKSPACE_ID/--workspace, and — as a last resort — workspace.current (the focused workspace).

The bug is an asymmetry in that last resort:

  • The .stop / .prompt-submit branches gate their visible mutations (status, notification, resume binding) on resolvedSurface.isAuthoritative, so a non-authoritative (focused/first) fallback correctly suppresses them.
  • But the feed telemetry — the value that drives the iMessage conversation subtitle — was sent unconditionally with the resolved workspaceId, before that guard.

So when a Stop hook cannot authoritatively resolve its own workspace (no mapped session, no TTY/PID binding, no CMUX_WORKSPACE_ID — the detached / shared-daemon session in #7100), the resolver returns the focused workspace, the status update is correctly skipped, but the conversation message is still recorded onto whichever workspace is selected. That matches the report exactly: the stray text is the long latestConversationMessage, no notification is created (.stop is non-blocking), and it lands on the focused row.

Notably, the generic-agent hook path (runGenericAgentHook) already returns nil (dropping the event) when workspace identity is unknown — it never borrows the focused workspace. This change brings the Claude path in line with that existing, safe behavior, and with the surface-less notification rule accepted in #7133.

Fix

Gate the Claude Stop / UserPromptSubmit feed telemetry's workspace_id on an authoritative workspace resolution — a mapped session, an authoritative surface (TTY/PID/explicit/mapped-session surface), or the caller's own CMUX_WORKSPACE_ID/--workspace. When none applies (the focused/first fallback), omit workspace_id so v2ApplyIMessageModeSideEffects drops the turn (its guard requires a non-empty workspaceId) rather than mis-homing it. The visible-mutation gating is unchanged.

The workspace term intentionally keys on the authoritative workspace, not merely an authoritative surface, so the normal per-surface subtitle still records even when the specific surface is a within-workspace fallback (e.g. a closed surface).

Tests

cmuxTests/ClaudeHookConversationAttributionTests.swift drives the real bundled cmux CLI (hooks claude stop) against a mock socket and asserts on the actual feed.push the app would receive:

  • stopWithoutIdentityDoesNotAttributeToFocusedWorkspace — the fix: no mapped session, CMUX_WORKSPACE_ID unset, a leaked/unlisted CMUX_SURFACE_ID, and a different workspace focused → the Stop feed.push must omit workspace_id (pre-fix it carries the focused workspace). Red before the fix, green after.
  • stopWithCallerWorkspaceStaysAttributedToCaller — normal per-surface case (caller CMUX_WORKSPACE_ID, listed surface) still attributes to the caller and never consults workspace.current.
  • stopWithCallerWorkspaceSurvivesNonAuthoritativeSurface — an authoritative caller workspace with a non-authoritative surface still attributes to the caller (guards against over-dropping).

Committed as two commits (failing test, then fix) so CI shows red → green.

Localization

No user-facing strings added or changed — the diff is CLI attribution logic + a test. Localization audit: nothing to update.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Summary by cubic

Fixes #7132: sidebar conversation subtitles now attribute only to an authoritative workspace; unknown identity drops attribution across all Claude feed events. Also separates session resume command display vs execution so the UI shows a clean command while execution still suppresses Codex update checks.

  • Bug Fixes

    • Gate Claude feed attribution on an authoritative workspace (mapped session, trusted surface, or resolved caller CMUX_WORKSPACE_ID); omit workspace_id when unknown; include surface_id only when the surface is authoritative; send best‑effort feed on teardown-ignore without defaulting attribution.
    • Treat ambient CMUX_SURFACE_ID as non-authoritative; avoid silent fallbacks (no default to --workspace/env unless resolved and authoritative); disable caller TTY routing when --workspace is present.
    • Track workspaceIsAuthoritative in the session store, preserve authority across surface fallbacks (including Stop), and clear stale authority on remaps.
    • Session index: split displayed resume command from executed command; displayed command drops the update-check flag, while execution uses the new resumeExecutionCommandWithCwd.
  • Tests

    • Add regression suite driving the real CLI against a mock socket: unknown identity (including repeat/no persistence), authoritative caller workspace with non-authoritative surface, stale caller workspace, and listed ambient surface.
    • Extend coverage to Notification and PreToolUse feed events; update session index tests for the new resume command behavior.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Claude hook telemetry attribution for Stop/idle/notification/pre-tool-use so events credit the correct workspace and don’t fall back to non-authoritative context.
    • Refined session/telemetry logic around authoritative workspace/surface resolution and improved terminal/TTY routing consistency.
    • Corrected resume command behavior for Codex-owned resume execution so update-check suppression is applied only when appropriate.
  • Tests
    • Added regression coverage for hooks claude stop attribution scenarios and wired the test suite into the project.
  • Style
    • Minor iOS UI formatting adjustments in the workspace detail picker menus.