From 71a0cd6128b644df211acf1b94f5419d9bb37cc7 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 12:31:08 -0700 Subject: [PATCH 1/7] Add regression coverage for stale agent tab icons --- cmuxTests/TerminalTabAgentIconTests.swift | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/cmuxTests/TerminalTabAgentIconTests.swift b/cmuxTests/TerminalTabAgentIconTests.swift index d69259defc693..8ba29d20515a7 100644 --- a/cmuxTests/TerminalTabAgentIconTests.swift +++ b/cmuxTests/TerminalTabAgentIconTests.swift @@ -207,6 +207,36 @@ struct TerminalTabAgentIconTests { #expect(asset == "AgentIcons/Codex") } + @MainActor + @Test func terminalTitleChangeReplacesStaleClaudeRuntimeIconWithCodex() throws { + try assertStaleAgentIconTransition( + stalePIDKey: "claude_code.old-session", + staleAsset: "AgentIcons/Claude", + title: "codex --yolo", + expectedAsset: "AgentIcons/Codex" + ) + } + + @MainActor + @Test func terminalTitleChangeReplacesStaleCodexRuntimeIconWithClaude() throws { + try assertStaleAgentIconTransition( + stalePIDKey: "codex.old-session", + staleAsset: "AgentIcons/Codex", + title: "claude --resume", + expectedAsset: "AgentIcons/Claude" + ) + } + + @MainActor + @Test func terminalTitleChangeClearsStaleAgentRuntimeIconForPlainShell() throws { + try assertStaleAgentIconTransition( + stalePIDKey: "claude_code.old-session", + staleAsset: "AgentIcons/Claude", + title: "zsh", + expectedAsset: nil + ) + } + @Test func liveRegisteredAgentResolvesThroughRegistrationLookup() { var lookedUpKeys: [String] = [] let asset = TerminalTabAgentIconResolver().assetName( @@ -263,4 +293,27 @@ struct TerminalTabAgentIconTests { #expect(asset == "AgentIcons/HermesAgent") } + + @MainActor + private func assertStaleAgentIconTransition( + stalePIDKey: String, + staleAsset: String, + title: String, + expectedAsset: String? + ) throws { + let workspace = Workspace() + let panel = try #require(workspace.focusedTerminalPanel) + let tabId = try #require(workspace.surfaceIdFromPanelId(panel.id)) + + workspace.recordAgentPID(key: stalePIDKey, pid: 0, panelId: panel.id, refreshPorts: false) + + #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == staleAsset) + #expect(workspace.bonsplitController.tab(tabId)?.iconAsset == staleAsset) + + #expect(workspace.updatePanelTitle(panelId: panel.id, title: title)) + #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == expectedAsset) + #expect(workspace.bonsplitController.tab(tabId)?.iconAsset == expectedAsset) + #expect(workspace.agentPIDs[stalePIDKey] == nil) + #expect(workspace.agentPIDKeysByPanelId[panel.id]?.contains(stalePIDKey) != true) + } } From d518440ddd09c49401e27e9f41d3fd4b22e76a2a Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 12:34:04 -0700 Subject: [PATCH 2/7] Prune stale agent runtime before resolving tab icon --- Sources/TerminalTabAgentIcon.swift | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Sources/TerminalTabAgentIcon.swift b/Sources/TerminalTabAgentIcon.swift index ddeb45450ad9b..f8c7b4e28a7cd 100644 --- a/Sources/TerminalTabAgentIcon.swift +++ b/Sources/TerminalTabAgentIcon.swift @@ -176,6 +176,7 @@ extension Workspace { var didMutatePanelTitle = false var didMutateWorkspaceTitle = false var didMutateTitleDerivedAgent = false + var didPruneStaleAgentRuntime = false if panelTitles[panelId] != trimmed { panelTitles[panelId] = trimmed @@ -183,22 +184,27 @@ extension Workspace { didMutatePanelTitle = true } - if panels[panelId] is TerminalPanel, - updateTitleDerivedTerminalAgentStatusKey(forPanelId: panelId, title: trimmed) { - didMutate = true - didMutateTitleDerivedAgent = true + if panels[panelId] is TerminalPanel { + if updateTitleDerivedTerminalAgentStatusKey(forPanelId: panelId, title: trimmed) { + didMutate = true + didMutateTitleDerivedAgent = true + } + if clearStaleAgentPIDs(panelId: panelId, refreshPorts: false) { + didMutate = true + didPruneStaleAgentRuntime = true + } } // Update bonsplit tab title only when this panel's title changed, and - // update the icon when the title-derived agent fallback changes. - if (didMutatePanelTitle || didMutateTitleDerivedAgent), + // update the icon when the current agent signal changes. + if (didMutatePanelTitle || didMutateTitleDerivedAgent || didPruneStaleAgentRuntime), let tabId = surfaceIdFromPanelId(panelId), let panel = panels[panelId], let existing = bonsplitController.tab(tabId) { let baseTitle = panelTitles[panelId] ?? panel.displayTitle let resolvedTitle = resolvedPanelTitle(panelId: panelId, fallback: baseTitle) let titleUpdate: String? = existing.title == resolvedTitle ? nil : resolvedTitle - let iconAssetUpdate: String?? = didMutateTitleDerivedAgent + let iconAssetUpdate: String?? = didMutateTitleDerivedAgent || didPruneStaleAgentRuntime ? .some(terminalTabAgentIconAsset(forPanelId: panelId)) : nil bonsplitController.updateTab( From a0a725741b7c5e8ebd3efedb50c3ad69e78f1ac8 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 15:28:18 -0700 Subject: [PATCH 3/7] Add regression coverage for stale agent ports --- cmuxTests/TerminalTabAgentIconTests.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmuxTests/TerminalTabAgentIconTests.swift b/cmuxTests/TerminalTabAgentIconTests.swift index 8ba29d20515a7..b92cdc99a9277 100644 --- a/cmuxTests/TerminalTabAgentIconTests.swift +++ b/cmuxTests/TerminalTabAgentIconTests.swift @@ -306,14 +306,20 @@ struct TerminalTabAgentIconTests { let tabId = try #require(workspace.surfaceIdFromPanelId(panel.id)) workspace.recordAgentPID(key: stalePIDKey, pid: 0, panelId: panel.id, refreshPorts: false) + let staleAgentPort = 54_321 + workspace.agentListeningPorts = [staleAgentPort] + workspace.recomputeListeningPorts() #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == staleAsset) #expect(workspace.bonsplitController.tab(tabId)?.iconAsset == staleAsset) + #expect(workspace.listeningPorts.contains(staleAgentPort)) #expect(workspace.updatePanelTitle(panelId: panel.id, title: title)) #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == expectedAsset) #expect(workspace.bonsplitController.tab(tabId)?.iconAsset == expectedAsset) #expect(workspace.agentPIDs[stalePIDKey] == nil) #expect(workspace.agentPIDKeysByPanelId[panel.id]?.contains(stalePIDKey) != true) + #expect(workspace.agentListeningPorts.isEmpty) + #expect(!workspace.listeningPorts.contains(staleAgentPort)) } } From 5fc34c8fbcd4195c46a97b4159506117528e8bb0 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 15:32:03 -0700 Subject: [PATCH 4/7] Refresh agent ports after stale runtime pruning --- Sources/TerminalTabAgentIcon.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/TerminalTabAgentIcon.swift b/Sources/TerminalTabAgentIcon.swift index f8c7b4e28a7cd..d8cf6797c8e55 100644 --- a/Sources/TerminalTabAgentIcon.swift +++ b/Sources/TerminalTabAgentIcon.swift @@ -189,7 +189,7 @@ extension Workspace { didMutate = true didMutateTitleDerivedAgent = true } - if clearStaleAgentPIDs(panelId: panelId, refreshPorts: false) { + if clearStaleAgentPIDs(panelId: panelId, refreshPorts: true) { didMutate = true didPruneStaleAgentRuntime = true } From 8637a2af193b141259fa749e09d936e07507e822 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 15:42:45 -0700 Subject: [PATCH 5/7] Add regression coverage for stale agent notifications --- cmuxTests/TerminalTabAgentIconTests.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmuxTests/TerminalTabAgentIconTests.swift b/cmuxTests/TerminalTabAgentIconTests.swift index b92cdc99a9277..9eee73a152087 100644 --- a/cmuxTests/TerminalTabAgentIconTests.swift +++ b/cmuxTests/TerminalTabAgentIconTests.swift @@ -304,15 +304,36 @@ struct TerminalTabAgentIconTests { let workspace = Workspace() let panel = try #require(workspace.focusedTerminalPanel) let tabId = try #require(workspace.surfaceIdFromPanelId(panel.id)) + let notificationStore = TerminalNotificationStore.shared + let appDelegate = AppDelegate.shared ?? AppDelegate() + let previousNotificationStore = appDelegate.notificationStore + let previousNotifications = notificationStore.notifications + appDelegate.notificationStore = notificationStore + defer { + notificationStore.replaceNotificationsForTesting(previousNotifications) + appDelegate.notificationStore = previousNotificationStore + } workspace.recordAgentPID(key: stalePIDKey, pid: 0, panelId: panel.id, refreshPorts: false) let staleAgentPort = 54_321 workspace.agentListeningPorts = [staleAgentPort] workspace.recomputeListeningPorts() + let staleNotification = TerminalNotification( + id: UUID(), tabId: workspace.id, surfaceId: panel.id, + title: "Stale agent", subtitle: "", body: "Needs input", + createdAt: Date(), isRead: false + ) + let siblingNotification = TerminalNotification( + id: UUID(), tabId: workspace.id, surfaceId: UUID(), + title: "Sibling", subtitle: "", body: "Keep me", + createdAt: Date(), isRead: false + ) + notificationStore.replaceNotificationsForTesting([staleNotification, siblingNotification]) #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == staleAsset) #expect(workspace.bonsplitController.tab(tabId)?.iconAsset == staleAsset) #expect(workspace.listeningPorts.contains(staleAgentPort)) + #expect(notificationStore.notifications.contains { $0.id == staleNotification.id }) #expect(workspace.updatePanelTitle(panelId: panel.id, title: title)) #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == expectedAsset) @@ -321,5 +342,7 @@ struct TerminalTabAgentIconTests { #expect(workspace.agentPIDKeysByPanelId[panel.id]?.contains(stalePIDKey) != true) #expect(workspace.agentListeningPorts.isEmpty) #expect(!workspace.listeningPorts.contains(staleAgentPort)) + #expect(!notificationStore.notifications.contains { $0.id == staleNotification.id }) + #expect(notificationStore.notifications.contains { $0.id == siblingNotification.id }) } } From 28a9ceaf7bfe72d941ae6c79cb7697834ed920fa Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 15:43:04 -0700 Subject: [PATCH 6/7] Unify stale agent runtime cleanup --- Sources/TabManager.swift | 6 +----- Sources/Workspace+PanelLifecycle.swift | 10 ++++++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Sources/TabManager.swift b/Sources/TabManager.swift index 796d7917213c2..b0aaea831d21e 100644 --- a/Sources/TabManager.swift +++ b/Sources/TabManager.swift @@ -686,11 +686,7 @@ class TabManager: ObservableObject { private func sweepStaleAgentPIDs() { for tab in tabs { - if tab.clearStaleAgentPIDs() { - // Also clear stale notifications (e.g. "Doing well, thanks!") - // left behind when Claude was killed without SessionEnd firing. - AppDelegate.shared?.notificationStore?.clearNotifications(forTabId: tab.id) - } + tab.clearStaleAgentPIDs() } } diff --git a/Sources/Workspace+PanelLifecycle.swift b/Sources/Workspace+PanelLifecycle.swift index 0d42b598a03fd..5d4e91c0ce634 100644 --- a/Sources/Workspace+PanelLifecycle.swift +++ b/Sources/Workspace+PanelLifecycle.swift @@ -152,8 +152,9 @@ extension Workspace { didChange = true } } - if didChange, refreshPorts { - refreshTrackedAgentPorts() + if didChange { + if refreshPorts { refreshTrackedAgentPorts() } + AppDelegate.shared?.notificationStore?.clearNotifications(forTabId: id) } return didChange } @@ -174,8 +175,9 @@ extension Workspace { didChange = true } } - if didChange, refreshPorts { - refreshTrackedAgentPorts() + if didChange { + if refreshPorts { refreshTrackedAgentPorts() } + AppDelegate.shared?.notificationStore?.clearNotifications(forTabId: id, surfaceId: panelId) } return didChange } From 74f1c5ee3084e52c3195e1924e2b7f919320176f Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Thu, 9 Jul 2026 16:32:56 -0700 Subject: [PATCH 7/7] Add no-title agent identity regression coverage --- cmuxTests/TerminalTabAgentIconTests.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmuxTests/TerminalTabAgentIconTests.swift b/cmuxTests/TerminalTabAgentIconTests.swift index 78e298f774113..5614878afd625 100644 --- a/cmuxTests/TerminalTabAgentIconTests.swift +++ b/cmuxTests/TerminalTabAgentIconTests.swift @@ -207,6 +207,18 @@ struct TerminalTabAgentIconTests { #expect(asset == "AgentIcons/Codex") } + @MainActor + @Test func terminalTitleDoesNotBecomeAgentIdentity() throws { + let workspace = Workspace() + let panel = try #require(workspace.focusedTerminalPanel) + let tabId = try #require(workspace.surfaceIdFromPanelId(panel.id)) + + #expect(workspace.updatePanelTitle(panelId: panel.id, title: "codex --yolo")) + #expect(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == nil) + #expect(workspace.bonsplitController.tab(tabId)?.iconImageData == nil) + #expect(workspace.bonsplitController.tab(tabId)?.iconAsset == nil) + } + @MainActor @Test func terminalTitleChangeReplacesStaleClaudeRuntimeIconWithCodex() throws { try assertStaleAgentIconTransition(