Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
20 changes: 13 additions & 7 deletions Sources/TerminalTabAgentIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,35 @@ extension Workspace {
var didMutatePanelTitle = false
var didMutateWorkspaceTitle = false
var didMutateTitleDerivedAgent = false
var didPruneStaleAgentRuntime = false

if panelTitles[panelId] != trimmed {
panelTitles[panelId] = trimmed
didMutate = true
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) {
Comment thread
austinywang marked this conversation as resolved.
Resolved
didMutate = true
didPruneStaleAgentRuntime = true
}
Comment thread
austinywang marked this conversation as resolved.
Resolved
}

// 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a test for the didPruneStaleAgentRuntime-only path.

All three test cases transition the title to a different agent (or plain shell), so didMutateTitleDerivedAgent is always true alongside didPruneStaleAgentRuntime. The specific path this fix enables — where didPruneStaleAgentRuntime is true but didMutateTitleDerivedAgent is false (e.g., title changes from "codex --yolo" to "codex --yolo --verbose", both mapping to "codex", while a stale Claude PID is pruned) — is not exercised. Without coverage, a future regression could drop didPruneStaleAgentRuntime from the icon-update condition and the existing tests would still pass.

🧪 Suggested additional test
`@MainActor`
`@Test` func terminalTitleChangePrunesStaleAgentWithoutTitleDerivedKeyChange() throws {
    let workspace = Workspace()
    let panel = try `#require`(workspace.focusedTerminalPanel)
    let tabId = try `#require`(workspace.surfaceIdFromPanelId(panel.id))

    // Establish title-derived key as "codex" first
    _ = workspace.updatePanelTitle(panelId: panel.id, title: "codex --yolo")

    // Record a stale Claude PID
    workspace.recordAgentPID(key: "claude_code.old-session", pid: 0, panelId: panel.id, refreshPorts: false)

    // Stale Claude icon should be showing (live agent wins over title-derived)
    `#expect`(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == "AgentIcons/Claude")

    // Title changes but still maps to "codex" — title-derived key doesn't change
    `#expect`(workspace.updatePanelTitle(panelId: panel.id, title: "codex --yolo --verbose"))
    // Stale Claude pruned, icon should now show Codex from title-derived key
    `#expect`(workspace.terminalTabAgentIconAsset(forPanelId: panel.id) == "AgentIcons/Codex")
    `#expect`(workspace.bonsplitController.tab(tabId)?.iconAsset == "AgentIcons/Codex")
    `#expect`(workspace.agentPIDs["claude_code.old-session"] == nil)
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Sources/TerminalTabAgentIcon.swift` around lines 198 - 207, Add a regression
test alongside the existing terminal title/icon tests covering the
didPruneStaleAgentRuntime-only path, using the relevant Workspace
updatePanelTitle and agent PID recording APIs. Start with a Codex-derived title,
record a stale Claude PID, then change to another Codex title that preserves the
derived agent key; assert the stale PID is removed and both the workspace icon
and Bonsplit tab icon update to Codex.

? .some(terminalTabAgentIconAsset(forPanelId: panelId))
: nil
bonsplitController.updateTab(
Expand Down
53 changes: 53 additions & 0 deletions cmuxTests/TerminalTabAgentIconTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
}
Loading