Repository navigation
Resolve agent notification targets from live identity at delivery time #7946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
ea0deb7
Add failing regression tests for wrong-pane notification attribution …
austinywang 113c504
Resolve agent notification targets from live identity at delivery tim…
austinywang fe6a106
Address review findings: pid_t overflow, stale queue keys, same-works…
austinywang 965e780
Address round-2 review: relay pid namespace, PreToolUse rehome, Sessi…
austinywang 428f662
Address round-3 review: shared live retargeting for all notify entryp…
austinywang a3d5aac
Address round-4 review: relay auth boundary, rebind discard, surface …
austinywang 8919c0a
Address round-5 review: authoritative surface in needs-input, Session…
austinywang 1962553
Address round-6 review: env-only pid resolution must not be authorita…
austinywang 3a5d3b9
Address PR review: async clear canonical identity, dead-workspace reh…
austinywang a3fe6fe
Address Cursor review: SessionEnd staleness gate judges the pane bein…
austinywang 2726b0d
Add regression coverage for live-owner workspace clears
austinywang 1a74c60
Address Cursor review: tab-wide clears resolve each pending entry's l…
austinywang c623412
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang 6f4900e
Add move-race notification regression coverage
austinywang a951c84
Close move-time notification routing races
austinywang 1be789f
Add entrypoint and same-workspace cleanup regressions
austinywang 31e936b
Enforce notification trust and pane cleanup boundaries
austinywang d8e63a8
Add PID authentication and clearability regressions
austinywang ade3a2d
Authenticate live hook identity and preserve clearability
austinywang 7c4e9d5
Add fresh-session and in-flight clear regressions
austinywang 3c96d94
Corroborate fresh sessions and cancel cleared policy work
austinywang 5ea5179
Add fail-closed lifecycle and causal clear regressions
austinywang 062c67e
Fail closed across Claude lifecycle routing
austinywang f6f34c8
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang 0b4e451
Respect CLI Swift file length guard
austinywang 3f68509
Add resolver fallback and relay clear regressions
austinywang 99d3096
Import notification routing selectors in move race tests
austinywang dc66d41
Preserve scoped notification routing under fallback and clears
austinywang 457c5ae
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang 65e096e
Add strict PID and confined delivery regressions
austinywang d6fca8c
Honor delivery target trust boundaries
austinywang bd33bf9
Add immediate relay move confinement regression
austinywang 1fed22e
Preserve notification retargeting provenance
austinywang dc8fdbe
Fix throwing Swift Testing assertion
austinywang c468b70
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang 625311d
Add notification provenance boundary regressions
austinywang 91d8900
Close notification provenance boundaries
austinywang 7c5e5c1
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang 24e3cb2
Add remaining notification routing regressions
austinywang e58d3d5
Fix remaining live notification routing gaps
austinywang eab7030
Add mutation boundary regressions
austinywang c7ed229
Preserve notification mutation causality
austinywang 62516cd
Add remaining clear routing regressions
austinywang 3757148
Keep notification regressions within file budget
austinywang 9036a3c
Run notification regressions in focused CI gate
austinywang 045ce17
Preserve notification clear confinement
austinywang 4fb90be
Add phone notification confinement regression
austinywang 11d9972
Cover phone routing in focused CI gate
austinywang 0e0f0fd
Keep confined phone notifications workspace-scoped
austinywang 8c508cf
Prepare confined phone deep-link routing
austinywang d38c8da
Add confined phone routing regressions
austinywang af5aef7
Carry phone notification routing provenance
austinywang 9d6ab5c
Fix Claude lifecycle regression fixtures
austinywang d32a40b
Add confined notification boundary regressions
austinywang b50ad08
Preserve confined notification boundaries
austinywang 7d976c4
Add remaining notification review regressions
austinywang c589512
Resolve remaining notification review findings
austinywang 3aad4c9
Fix in-flight discard type inference
austinywang b60f61a
Add final notification review regressions
austinywang 99eab3c
Keep notification regressions within test budgets
austinywang 529c2fb
Close final notification review gaps
austinywang ef8ea4e
Fix CI compile: import CmuxCore in AgentNotificationMutationBoundaryT…
austinywang 732cdf5
Make notification regression marker waits CI-tolerant
austinywang 3eb637f
Fix CI red test and relay rehome gate; assert guaranteed discard sema…
austinywang 4c6ceb4
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang d11e31a
Align hook test ack expectations with main's structured ack
austinywang 47b45a1
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // One authoritative hook-event → live pane resolution for Claude hooks. | ||
| // | ||
| // Invariant (https://github.com/manaflow-ai/cmux/issues/7939): an agent that | ||
| // finishes in pane P of workspace W gets its notification, unread ring, and | ||
| // status on exactly P in W. Live identity therefore outranks every persisted | ||
| // or spawn-time claim: | ||
| // | ||
| // 1. live agent-pid target (`agent.resolve_delivery_target {pid}`) — the | ||
| // surface that owns the agent process RIGHT NOW; wins over a polluted | ||
| // session record (#7391 resume/tty drift) and heals it via the caller's | ||
| // subsequent upsert. Local direct-socket hooks only: a relay-backed | ||
| // connection carries a remote host's pid namespace. | ||
| // 2. the legacy chain (session record → caller tty → spawn env), each | ||
| // validated against a live workspace (unchanged from #7228). | ||
| // 3. identity-surface re-home (`agent.resolve_delivery_target | ||
| // {surface_id}`) — when the legacy chain would fall back to the resolved | ||
| // workspace's focused surface, ask the app which workspace currently | ||
| // owns the identity surface and deliver to that pane instead (#5781 | ||
| // pane moves; also heals a workspace listing that lags the app's panel | ||
| // map when the owner is the same workspace). | ||
| // | ||
| // Explicit --workspace/--surface flags bypass the live probes entirely, and an | ||
| // app without the resolver method degrades to the legacy chain unchanged. | ||
|
|
||
| import Foundation | ||
|
|
||
| extension CMUXCLI { | ||
| struct ClaudeHookDeliveryTarget { | ||
| let workspaceId: String | ||
| let surfaceId: String | ||
| /// Resolved from the hook's own identity (live pid target, session | ||
| /// record, explicit value, or caller tty) rather than the | ||
| /// focused/first-surface fallback. | ||
| let isAuthoritative: Bool | ||
| } | ||
|
|
||
| /// The per-invocation routing inputs shared by every Claude hook | ||
| /// subcommand: explicit flags, spawn-time env fallbacks, the lazy caller | ||
| /// binding, and the live agent pid. | ||
| struct ClaudeHookRoutingContext { | ||
| let workspaceArg: String? | ||
| let surfaceArg: String? | ||
| let surfaceFlagIsExplicit: Bool | ||
| let preferCallerTTYRouting: Bool | ||
| let callerTerminalBinding: (() -> CallerTerminalBinding?)? | ||
| let agentPid: Int? | ||
| /// Frequent events (per-tool PreToolUse) skip the pid/tty scan and | ||
| /// rely on records healed by the turn-level hooks. This does NOT gate | ||
| /// the cheap `{surface_id}` re-home probe: that probe only fires when | ||
| /// the resolved surface was a non-authoritative guess, and disabling | ||
| /// it would let a stale record mutate (and, via upsert, re-record) the | ||
| /// wrong pane mid-turn. | ||
| var allowsPidProbe: Bool = true | ||
| } | ||
|
|
||
| func resolveClaudeHookDeliveryTarget( | ||
| mappedSession: ClaudeHookSessionRecord?, | ||
| routing: ClaudeHookRoutingContext, | ||
| client: SocketClient | ||
| ) throws -> ClaudeHookDeliveryTarget? { | ||
| let pidProbeAllowed = routing.allowsPidProbe && routing.preferCallerTTYRouting | ||
| let rehomeAllowed = routing.preferCallerTTYRouting | ||
| if pidProbeAllowed, | ||
| let live = liveAgentPidDeliveryTarget( | ||
| pid: routing.agentPid ?? mappedSession?.pid, | ||
| client: client | ||
| ) { | ||
| return live | ||
| } | ||
| guard let workspaceId = try resolvePreferredWorkspaceIdForClaudeHook( | ||
| preferred: mappedSession?.workspaceId, | ||
| fallback: routing.workspaceArg, | ||
| preferCallerTTYOverFallback: routing.preferCallerTTYRouting, | ||
| callerTerminalBinding: routing.callerTerminalBinding, | ||
| client: client | ||
| ) else { | ||
| // Every workspace claim is dead (e.g. the recorded workspace was | ||
| // closed after its pane moved out): follow the identity surface to | ||
| // whichever workspace owns it now, else stay a no-op. | ||
| guard rehomeAllowed else { return nil } | ||
| return rehomedClaudeHookDeliveryTarget( | ||
| surfaceId: mappedSession?.surfaceId, | ||
| claimedWorkspaceId: mappedSession?.workspaceId, | ||
| client: client | ||
| ) ?? rehomedClaudeHookDeliveryTarget( | ||
| surfaceId: routing.surfaceArg, | ||
| claimedWorkspaceId: routing.workspaceArg, | ||
| client: client | ||
| ) | ||
| } | ||
| // The record's workspace died (the legacy chain resolved a DIFFERENT | ||
| // workspace than the session recorded): the record surface's current | ||
| // owner outranks whatever the caller-tty fallback would pick, which | ||
| // can be an unrelated pane via a stale tty row. Only if the record | ||
| // surface is gone too does the legacy fallback proceed. | ||
| if rehomeAllowed, | ||
| let recordedWorkspaceId = nonEmptyClaudeHookIdentifier(mappedSession?.workspaceId), | ||
| recordedWorkspaceId != workspaceId, | ||
| let rehomed = rehomedClaudeHookDeliveryTarget( | ||
| surfaceId: mappedSession?.surfaceId, | ||
| claimedWorkspaceId: nil, | ||
| client: client | ||
| ) { | ||
| return rehomed | ||
| } | ||
| let resolvedSurface = try resolvePreferredSurfaceForClaudeHookDetailed( | ||
| preferred: mappedSession?.surfaceId, | ||
| fallback: routing.surfaceArg, | ||
| fallbackIsExplicit: routing.surfaceFlagIsExplicit, | ||
| workspaceId: workspaceId, | ||
| callerTerminalBinding: routing.callerTerminalBinding, | ||
| client: client | ||
| ) | ||
| if !resolvedSurface.isAuthoritative, rehomeAllowed { | ||
| // The legacy chain fell back to a focused-surface guess: the | ||
| // identity surface was not in the resolved workspace's listing. | ||
| // If the app confirms which workspace currently owns the identity | ||
| // surface, that answer wins — whether the pane moved workspaces | ||
| // (#5781) or the listing merely lagged the app's panel map and | ||
| // the owner is the same workspace (a same-workspace answer still | ||
| // outranks the focused-surface guess). | ||
| let rehomed = rehomedClaudeHookDeliveryTarget( | ||
| surfaceId: mappedSession?.surfaceId, | ||
| claimedWorkspaceId: workspaceId, | ||
| client: client | ||
| ) ?? rehomedClaudeHookDeliveryTarget( | ||
| surfaceId: routing.surfaceArg, | ||
| claimedWorkspaceId: workspaceId, | ||
| client: client | ||
| ) | ||
| if let rehomed { | ||
| return rehomed | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| return ClaudeHookDeliveryTarget( | ||
| workspaceId: workspaceId, | ||
| surfaceId: resolvedSurface.surfaceId, | ||
| isAuthoritative: resolvedSurface.isAuthoritative | ||
| ) | ||
| } | ||
|
|
||
| /// `{pid}` probe: only a `source == "pid"` answer counts — the app refuses | ||
| /// to guess, and an older app (method_not_found) or a dead/remote pid | ||
| /// falls back to the legacy chain. | ||
| private func liveAgentPidDeliveryTarget( | ||
| pid: Int?, | ||
| client: SocketClient | ||
| ) -> ClaudeHookDeliveryTarget? { | ||
| // A relay-backed connection means this hook is not running in the | ||
| // app's process namespace (SSH/cloud host): its CMUX_CLAUDE_PID is a | ||
| // REMOTE pid, and resolving that number against the Mac's local | ||
| // process table could match an unrelated local process attached to | ||
| // some other pane. Surface/workspace UUIDs are machine-independent, | ||
| // so the legacy chain and the {surface_id} re-home probe still apply. | ||
| guard !client.isRelayBacked, | ||
| let pid, pid > 0, | ||
| let payload = try? client.sendV2( | ||
| method: "agent.resolve_delivery_target", | ||
| params: ["pid": pid], | ||
| responseTimeout: 2.0 | ||
| ), | ||
| (payload["source"] as? String) == "pid", | ||
| let workspaceId = normalizedHandleValue(payload["workspace_id"] as? String), | ||
| isUUID(workspaceId), | ||
| let surfaceId = normalizedHandleValue(payload["surface_id"] as? String), | ||
| isUUID(surfaceId) else { | ||
| return nil | ||
| } | ||
| return ClaudeHookDeliveryTarget( | ||
| workspaceId: workspaceId, | ||
| surfaceId: surfaceId, | ||
| isAuthoritative: true | ||
| ) | ||
| } | ||
|
|
||
| /// `{surface_id}` probe: the workspace that CURRENTLY owns a known | ||
| /// identity surface. Only a `source == "surface"` answer counts. | ||
| private func rehomedClaudeHookDeliveryTarget( | ||
| surfaceId: String?, | ||
| claimedWorkspaceId: String?, | ||
| client: SocketClient | ||
| ) -> ClaudeHookDeliveryTarget? { | ||
| guard let surfaceId = nonEmptyClaudeHookIdentifier(surfaceId), isUUID(surfaceId) else { | ||
| return nil | ||
| } | ||
| var params: [String: Any] = ["surface_id": surfaceId] | ||
| if let claimedWorkspaceId = nonEmptyClaudeHookIdentifier(claimedWorkspaceId), isUUID(claimedWorkspaceId) { | ||
| params["workspace_id"] = claimedWorkspaceId | ||
| } | ||
| guard let payload = try? client.sendV2( | ||
| method: "agent.resolve_delivery_target", | ||
| params: params, | ||
| responseTimeout: 2.0 | ||
| ), | ||
| (payload["source"] as? String) == "surface", | ||
| let workspaceId = normalizedHandleValue(payload["workspace_id"] as? String), | ||
| isUUID(workspaceId) else { | ||
| return nil | ||
| } | ||
| return ClaudeHookDeliveryTarget( | ||
| workspaceId: workspaceId, | ||
| surfaceId: surfaceId, | ||
| isAuthoritative: true | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.