Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
119c83e
Add failing Codex no-TTY live owner test
Jun 26, 2026
ef1bded
Guard Codex no-TTY hooks from live owner surfaces
Jun 26, 2026
e0583ed
Refresh Swift file length budget
Jun 26, 2026
822a7f8
Merge origin/main into issue 5676 fix
Jun 26, 2026
0fa617d
Move Codex no-TTY regression to Swift Testing
Jun 26, 2026
dda9c47
Fail closed on Codex live-owner guard errors
Jun 26, 2026
c524663
Track Codex hook PID capture time
Jun 27, 2026
0cae033
Guard Codex no-TTY routes from any live owner
Jun 27, 2026
ad16592
Handle legacy Codex hook PID records
Jun 29, 2026
e95b6cf
Refresh Codex hook PID captures
Jun 29, 2026
c3333e1
Merge origin/main into issue 5676 fix
Jun 29, 2026
807d446
Continue Codex no-TTY routing on owner-check errors
Jun 29, 2026
7e168f3
Merge origin/main into issue 5676 fix
Jun 29, 2026
0be4201
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jun 29, 2026
d11122f
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 2, 2026
13484cc
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 2, 2026
8dbbc31
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 2, 2026
c6a51fd
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 2, 2026
093cc57
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 3, 2026
19d99b0
Preserve carried hook PID capture timestamps
austinywang Jul 3, 2026
0455a5d
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 3, 2026
feed524
Default hook PID capture timestamp
austinywang Jul 3, 2026
bb82834
Align stale Codex PID capture test
austinywang Jul 3, 2026
ac50151
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 3, 2026
ee4e688
Merge remote-tracking branch 'origin/main' into issue-5676-codex-hook…
austinywang Jul 4, 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
4 changes: 2 additions & 2 deletions .github/swift-file-length-budget.tsv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# cmux-owned Swift file length budget.
# Format: max_lines<TAB>relative path
# Reduce counts as files shrink. CI fails if tracked files exceed this budget.
34629 CLI/cmux.swift
34737 CLI/cmux.swift
17854 Sources/AppDelegate.swift
16109 Sources/ContentView.swift
13913 Sources/TerminalController.swift
Expand Down Expand Up @@ -89,6 +89,7 @@
1009 cmuxTests/CmuxTopSnapshotScopeTests.swift
1006 cmuxTests/CmuxSSHURLRequestTests.swift
1002 Packages/iOS/CmuxMobileShell/Tests/CmuxMobileShellTests/PairedMacBackupTests.swift
962 cmuxTests/CLICodexHookTimeoutRegressionTests.swift
951 Sources/App/TerminalDirectoryOpenSupport.swift
948 Sources/App/ShortcutRoutingSupport.swift
947 Sources/TerminalNotificationPolicy.swift
Expand Down Expand Up @@ -125,7 +126,6 @@
755 CLI/CMUXCLI+AgentHookDefinitions.swift
754 Sources/TerminalController+ControlWorkspaceContext.swift
752 cmuxUITests/CloseWorkspaceCmdDUITests.swift
739 cmuxTests/CLICodexHookTimeoutRegressionTests.swift
738 Packages/macOS/CMUXProjectModel/Sources/CMUXProjectModel/XcodeProjectAdapter.swift
724 Sources/Panels/BrowserPopupWindowController.swift
722 Packages/Shared/CmuxAgentChat/Sources/CmuxAgentChat/Store/ChatConversationStore.swift
Expand Down
124 changes: 116 additions & 8 deletions CLI/cmux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ final class ClaudeHookSessionStore {
}
}

if requireLiveProcess, !Self.processExists(record.pid) {
if requireLiveProcess, !Self.processIsLiveForSession(pid: record.pid, updatedAt: record.updatedAt) {
record.runtimeStatus = nil
record.updatedAt = now
state.sessions[sessionId] = record
Expand All @@ -1234,12 +1234,76 @@ final class ClaudeHookSessionStore {
}
}

private static func processExists(_ pid: Int?) -> Bool {
func surfaceHasEarlierLiveOwner(
workspaceId: String,
surfaceId: String,
incomingSessionId: String?
) throws -> Bool {
guard let normalizedWorkspace = normalizeOptional(workspaceId),
let normalizedSurface = normalizeOptional(surfaceId) else {
return false
}
let incoming = normalizeOptional(incomingSessionId)
return try withLockedState { state in
let incomingStartedAt = incoming.flatMap { state.sessions[$0]?.startedAt }
let now = Date().timeIntervalSince1970

for sessionId in Array(state.sessions.keys) {
guard var record = state.sessions[sessionId] else { continue }
guard record.sessionId != incoming,
normalizeOptional(record.workspaceId) == normalizedWorkspace,
normalizeOptional(record.surfaceId) == normalizedSurface,
Self.runtimeStatusOwnsSurface(record.runtimeStatus) else {
continue
}
if let incomingStartedAt, record.startedAt > incomingStartedAt {
continue
}
if !Self.processIsLiveForSession(pid: record.pid, updatedAt: record.updatedAt) {
record.runtimeStatus = nil
record.updatedAt = now
state.sessions[sessionId] = record
continue
}
Comment thread
austinywang marked this conversation as resolved.
Resolved
return true
}

return false
}
}

private static func runtimeStatusOwnsSurface(_ status: AgentHookRuntimeStatus?) -> Bool {
status == .running || status == .needsInput
}

private static func processIsLiveForSession(pid: Int?, updatedAt: TimeInterval?) -> Bool {
guard let pid, pid > 0 else { return false }
let processExists: Bool
if kill(pid_t(pid), 0) == 0 {
processExists = true
} else {
processExists = errno == EPERM
}
guard processExists else { return false }
guard let updatedAt,
let startedAt = processStartTime(pid: pid) else {
return true
}
return errno == EPERM
return startedAt <= updatedAt
}

private static func processStartTime(pid: Int) -> TimeInterval? {
guard pid > 0, pid <= Int(Int32.max) else { return nil }
var info = kinfo_proc()
var size = MemoryLayout<kinfo_proc>.size
var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, Int32(pid)]
guard sysctl(&mib, u_int(mib.count), &info, &size, nil, 0) == 0,
size >= MemoryLayout<kinfo_proc>.size,
info.kp_proc.p_pid == pid_t(pid) else {
return nil
}
let startTime = info.kp_proc.p_un.__p_starttime
return TimeInterval(startTime.tv_sec) + (TimeInterval(startTime.tv_usec) / 1_000_000)
}

/// Returns true when an event belongs to the workspace's active Claude session.
Expand Down Expand Up @@ -29789,10 +29853,48 @@ export default function cmuxPiSessionExtension(pi: ExtensionAPI) {
return boundSurface
}

func targetBlockedByEarlierLiveOwner(
_ target: (workspaceId: String, surfaceId: String),
mapped: ClaudeHookSessionRecord?
) -> Bool {
guard explicitSurfaceFlag == nil else { return false }
guard processBinding() == nil else { return false }
let currentPID = mapped?.pid ?? inferredPID
guard !shouldSuppressNestedAgentVisibleMutations(currentAgentPID: currentPID, env: env) else {
return false
}
let blocked = (try? store.surfaceHasEarlierLiveOwner(
workspaceId: target.workspaceId,
surfaceId: target.surfaceId,
incomingSessionId: sessionId
)) == true
Comment thread
austinywang marked this conversation as resolved.
Resolved
#if DEBUG
if blocked {
agentHookDebugLog(
"agentHook.target.nil agent=\(def.name) subcommand=\(subcommand) session=\(agentHookDebugShort(sessionId)) reason=earlierLiveSurfaceOwner workspace=\(agentHookDebugShort(target.workspaceId)) surface=\(agentHookDebugShort(target.surfaceId))",
socketPath: client.socketPath,
env: env
)
}
#endif
return blocked
}

func targetUnlessBlockedByEarlierLiveOwner(
_ target: (workspaceId: String, surfaceId: String)?,
mapped: ClaudeHookSessionRecord?
) -> (workspaceId: String, surfaceId: String)? {
guard let target else { return nil }
return targetBlockedByEarlierLiveOwner(target, mapped: mapped) ? nil : target
}

if let workspaceId = resolvedDirectWorkspaceArg {
let preferredSurfaceId = correctedDirectSurfaceId(workspaceId: workspaceId)
?? (hookWsFlag == nil ? processBinding()?.surfaceId : nil)
let target = resolveTarget(workspaceId: workspaceId, preferredSurfaceId: preferredSurfaceId, mapped: mapped)
let target = targetUnlessBlockedByEarlierLiveOwner(
resolveTarget(workspaceId: workspaceId, preferredSurfaceId: preferredSurfaceId, mapped: mapped),
mapped: mapped
)
#if DEBUG
agentHookDebugLog(
"agentHook.target.\(target == nil ? "nil" : "resolved") agent=\(def.name) subcommand=\(subcommand) session=\(agentHookDebugShort(sessionId)) source=direct workspace=\(agentHookDebugShort(target?.workspaceId ?? workspaceId)) surface=\(agentHookDebugShort(target?.surfaceId)) mapped=\(mapped == nil ? 0 : 1)",
Expand All @@ -29805,9 +29907,12 @@ export default function cmuxPiSessionExtension(pi: ExtensionAPI) {

let binding = processBinding()
if let workspaceId = resolveAccessibleWorkspaceId(binding?.workspaceId),
let target = resolveTarget(
workspaceId: workspaceId,
preferredSurfaceId: binding?.surfaceId,
let target = targetUnlessBlockedByEarlierLiveOwner(
resolveTarget(
workspaceId: workspaceId,
preferredSurfaceId: binding?.surfaceId,
mapped: mapped
),
mapped: mapped
) {
#if DEBUG
Expand All @@ -29830,7 +29935,10 @@ export default function cmuxPiSessionExtension(pi: ExtensionAPI) {
#endif
return nil
}
let target = resolveTarget(workspaceId: workspaceId, preferredSurfaceId: nil, mapped: mapped)
let target = targetUnlessBlockedByEarlierLiveOwner(
resolveTarget(workspaceId: workspaceId, preferredSurfaceId: nil, mapped: mapped),
mapped: mapped
)
#if DEBUG
agentHookDebugLog(
"agentHook.target.\(target == nil ? "nil" : "resolved") agent=\(def.name) subcommand=\(subcommand) session=\(agentHookDebugShort(sessionId)) source=mapped workspace=\(agentHookDebugShort(target?.workspaceId ?? workspaceId)) surface=\(agentHookDebugShort(target?.surfaceId)) mapped=\(mapped == nil ? 0 : 1)",
Expand Down
Loading
Loading