Skip to content
Merged
Show file tree
Hide file tree
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 Jul 12, 2026
113c504
Resolve agent notification targets from live identity at delivery tim…
austinywang Jul 12, 2026
fe6a106
Address review findings: pid_t overflow, stale queue keys, same-works…
austinywang Jul 12, 2026
965e780
Address round-2 review: relay pid namespace, PreToolUse rehome, Sessi…
austinywang Jul 12, 2026
428f662
Address round-3 review: shared live retargeting for all notify entryp…
austinywang Jul 12, 2026
a3d5aac
Address round-4 review: relay auth boundary, rebind discard, surface …
austinywang Jul 12, 2026
8919c0a
Address round-5 review: authoritative surface in needs-input, Session…
austinywang Jul 12, 2026
1962553
Address round-6 review: env-only pid resolution must not be authorita…
austinywang Jul 12, 2026
3a5d3b9
Address PR review: async clear canonical identity, dead-workspace reh…
austinywang Jul 12, 2026
a3fe6fe
Address Cursor review: SessionEnd staleness gate judges the pane bein…
austinywang Jul 12, 2026
2726b0d
Add regression coverage for live-owner workspace clears
austinywang Jul 13, 2026
1a74c60
Address Cursor review: tab-wide clears resolve each pending entry's l…
austinywang Jul 13, 2026
c623412
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 13, 2026
6f4900e
Add move-race notification regression coverage
austinywang Jul 13, 2026
a951c84
Close move-time notification routing races
austinywang Jul 13, 2026
1be789f
Add entrypoint and same-workspace cleanup regressions
austinywang Jul 13, 2026
31e936b
Enforce notification trust and pane cleanup boundaries
austinywang Jul 13, 2026
d8e63a8
Add PID authentication and clearability regressions
austinywang Jul 13, 2026
ade3a2d
Authenticate live hook identity and preserve clearability
austinywang Jul 13, 2026
7c4e9d5
Add fresh-session and in-flight clear regressions
austinywang Jul 13, 2026
3c96d94
Corroborate fresh sessions and cancel cleared policy work
austinywang Jul 13, 2026
5ea5179
Add fail-closed lifecycle and causal clear regressions
austinywang Jul 13, 2026
062c67e
Fail closed across Claude lifecycle routing
austinywang Jul 13, 2026
f6f34c8
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 13, 2026
0b4e451
Respect CLI Swift file length guard
austinywang Jul 13, 2026
3f68509
Add resolver fallback and relay clear regressions
austinywang Jul 13, 2026
99d3096
Import notification routing selectors in move race tests
austinywang Jul 13, 2026
dc66d41
Preserve scoped notification routing under fallback and clears
austinywang Jul 13, 2026
457c5ae
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 13, 2026
65e096e
Add strict PID and confined delivery regressions
austinywang Jul 13, 2026
d6fca8c
Honor delivery target trust boundaries
austinywang Jul 13, 2026
bd33bf9
Add immediate relay move confinement regression
austinywang Jul 13, 2026
1fed22e
Preserve notification retargeting provenance
austinywang Jul 13, 2026
dc8fdbe
Fix throwing Swift Testing assertion
austinywang Jul 13, 2026
c468b70
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 13, 2026
625311d
Add notification provenance boundary regressions
austinywang Jul 13, 2026
91d8900
Close notification provenance boundaries
austinywang Jul 13, 2026
7c5e5c1
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 13, 2026
24e3cb2
Add remaining notification routing regressions
austinywang Jul 13, 2026
e58d3d5
Fix remaining live notification routing gaps
austinywang Jul 13, 2026
eab7030
Add mutation boundary regressions
austinywang Jul 13, 2026
c7ed229
Preserve notification mutation causality
austinywang Jul 13, 2026
62516cd
Add remaining clear routing regressions
austinywang Jul 13, 2026
3757148
Keep notification regressions within file budget
austinywang Jul 13, 2026
9036a3c
Run notification regressions in focused CI gate
austinywang Jul 13, 2026
045ce17
Preserve notification clear confinement
austinywang Jul 13, 2026
4fb90be
Add phone notification confinement regression
austinywang Jul 13, 2026
11d9972
Cover phone routing in focused CI gate
austinywang Jul 13, 2026
0e0f0fd
Keep confined phone notifications workspace-scoped
austinywang Jul 13, 2026
8c508cf
Prepare confined phone deep-link routing
austinywang Jul 13, 2026
d38c8da
Add confined phone routing regressions
austinywang Jul 13, 2026
af5aef7
Carry phone notification routing provenance
austinywang Jul 13, 2026
9d6ab5c
Fix Claude lifecycle regression fixtures
austinywang Jul 13, 2026
d32a40b
Add confined notification boundary regressions
austinywang Jul 13, 2026
b50ad08
Preserve confined notification boundaries
austinywang Jul 13, 2026
7d976c4
Add remaining notification review regressions
austinywang Jul 13, 2026
c589512
Resolve remaining notification review findings
austinywang Jul 13, 2026
3aad4c9
Fix in-flight discard type inference
austinywang Jul 13, 2026
b60f61a
Add final notification review regressions
austinywang Jul 13, 2026
99eab3c
Keep notification regressions within test budgets
austinywang Jul 13, 2026
529c2fb
Close final notification review gaps
austinywang Jul 13, 2026
ef8ea4e
Fix CI compile: import CmuxCore in AgentNotificationMutationBoundaryT…
austinywang Jul 13, 2026
732cdf5
Make notification regression marker waits CI-tolerant
austinywang Jul 13, 2026
3eb637f
Fix CI red test and relay rehome gate; assert guaranteed discard sema…
austinywang Jul 13, 2026
4c6ceb4
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 13, 2026
d11e31a
Align hook test ack expectations with main's structured ack
austinywang Jul 14, 2026
47b45a1
Merge remote-tracking branch 'origin/main' into issue-7939-notificati…
austinywang Jul 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions CLI/CMUXCLI+ClaudeHookDeliveryTarget.swift
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 {
Comment thread
austinywang marked this conversation as resolved.
Resolved
// 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
}
Comment thread
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
)
}
}
24 changes: 6 additions & 18 deletions CLI/CMUXCLI+ClaudePushNotificationHook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ extension CMUXCLI {
telemetry: CLISocketSentryTelemetry,
parsedInput: ClaudeHookParsedInput,
sessionStore: ClaudeHookSessionStore,
workspaceArg: String?,
surfaceArg: String?,
hookSurfaceFlagIsExplicit: Bool,
preferCallerTTYRouting: Bool,
callerTTYBindingProvider: (() -> CallerTerminalBinding?)?,
routing: ClaudeHookRoutingContext,
markFeedTelemetryHandled: () -> Void,
sendFeedTelemetry: (String?, String?) -> Void
) throws {
Expand All @@ -36,26 +32,18 @@ extension CMUXCLI {
return
}
let mappedSession = parsedInput.sessionId.flatMap { try? sessionStore.lookup(sessionId: $0) }
guard let workspaceId = try resolvePreferredWorkspaceIdForClaudeHook(
preferred: mappedSession?.workspaceId,
fallback: workspaceArg,
preferCallerTTYOverFallback: preferCallerTTYRouting,
callerTerminalBinding: callerTTYBindingProvider,
guard let resolvedTarget = try resolveClaudeHookDeliveryTarget(
mappedSession: mappedSession,
routing: routing,
client: client
) else {
markFeedTelemetryHandled()
telemetry.breadcrumb("claude-hook.push-notification.unresolved")
print(String(localized: "common.ok", defaultValue: "OK"))
return
}
let resolvedSurface = try resolvePreferredSurfaceForClaudeHookDetailed(
preferred: mappedSession?.surfaceId,
fallback: surfaceArg,
fallbackIsExplicit: hookSurfaceFlagIsExplicit,
workspaceId: workspaceId,
callerTerminalBinding: callerTTYBindingProvider,
client: client
)
let workspaceId = resolvedTarget.workspaceId
let resolvedSurface = resolvedTarget
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let surfaceId = resolvedSurface.surfaceId
sendFeedTelemetry(workspaceId, surfaceId)
guard shouldApplyClaudeHookVisibleMutation(
Expand Down
Loading
Loading