A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
A shared pill shows the session that is working, not the oldest one
Orlando Castañedaaa6d34fbc60c
diff --git a/NotchBuddy/Sources/App/HookServer.swift b/NotchBuddy/Sources/App/HookServer.swiftindex e570f71c..3b7f01ac 100644--- a/NotchBuddy/Sources/App/HookServer.swift+++ b/NotchBuddy/Sources/App/HookServer.swift@@ -585,6 +585,8 @@ final class HookServer: @unchecked Sendable {case "Stop":let scope = sessions.endTurn(pill: agentId, session: sessionId)+ // A turn ending can hand the name to a sibling session that is still working.+ applyDisplayOwner(pill: agentId)let rawFinal = (payload["last_assistant_message"] as? String)?? (payload["message"] as? String) ?? ""let finalText = DiffEngine.toOneLine(rawFinal)@@ -620,7 +622,9 @@ final class HookServer: @unchecked Sendable {case "StopFailure":RecapStore.shared.stop(sessionId: recapSessionId)- guard sessions.endTurn(pill: agentId, session: sessionId) == .pill else { break }+ let failScope = sessions.endTurn(pill: agentId, session: sessionId)+ applyDisplayOwner(pill: agentId)+ guard failScope == .pill else { break }state.updateTask(id: agentId, state: .error)SoundEngine.shared.play("error")if focused {@@ -632,7 +636,9 @@ final class HookServer: @unchecked Sendable {case "Interrupt":// Codex: user stopped the turnRecapStore.shared.stop(sessionId: recapSessionId)- guard sessions.endTurn(pill: agentId, session: sessionId) == .pill else { break }+ let interruptScope = sessions.endTurn(pill: agentId, session: sessionId)+ applyDisplayOwner(pill: agentId)+ guard interruptScope == .pill else { break }activeSessionId = nilstate.updateTask(id: agentId, state: .idle)clearPillBadge(id: agentId)diff --git a/NotchBuddy/Sources/App/PillSessionRegistry.swift b/NotchBuddy/Sources/App/PillSessionRegistry.swiftindex bccebc26..2a5aea7e 100644--- a/NotchBuddy/Sources/App/PillSessionRegistry.swift+++ b/NotchBuddy/Sources/App/PillSessionRegistry.swift@@ -53,10 +53,13 @@ struct PillSessionRegistry {var bundleId: Stringvar isWorking: Boolvar lastSeen: Date+ /// Last time the session was mid-turn or ended a turn. A stray notification+ /// from an idle session does not move it, unlike `lastSeen`.+ var lastWorked: Date = .distantPast}private struct Pill {- /// Live sessions, oldest first. The first one owns name and cwd.+ /// Live sessions, oldest first.var order: [String] = []var sessions: [String: Session] = [:]}@@ -82,7 +85,7 @@ struct PillSessionRegistry {if !display.cwd.isEmpty { session.cwd = display.cwd }if display.hostApp != nil { session.hostApp = display.hostApp }if !display.bundleId.isEmpty { session.bundleId = display.bundleId }- if working { session.isWorking = true }+ if working { session.isWorking = true; session.lastWorked = now }session.lastSeen = nowp.sessions[display.session] = sessionpills[pill] = p@@ -100,6 +103,7 @@ struct PillSessionRegistry {sweep(&p, now: now)p.sessions[session]?.isWorking = falsep.sessions[session]?.lastSeen = now+ p.sessions[session]?.lastWorked = nowif p.order.isEmpty { pills.removeValue(forKey: pill) } else { pills[pill] = p }return p.sessions.values.contains(where: \.isWorking) ? .sessionOnly : .pill}@@ -121,12 +125,18 @@ struct PillSessionRegistry {// MARK: - Reading- /// The session whose project name and cwd the pill shows. Ownership goes- /// to the oldest live session and only moves on when that one ends, so a- /// single name field stops flipping between two parallel sessions.+ /// The session whose project name and cwd the pill shows, so the name matches+ /// the activity under it: the oldest session that is mid-turn (it keeps the+ /// name while parallel sessions work, so it does not flip between them), else+ /// the one that worked last, else the oldest live one.func displayOwner(of pill: String) -> Display? {- guard let p = pills[pill], let id = p.order.first,- let session = p.sessions[id] else { return nil }+ guard let p = pills[pill] else { return nil }+ let working = p.order.first { p.sessions[$0]?.isWorking == true }+ // max(by:) keeps the first of equal elements: with no turn seen yet, the oldest.+ let lastWorked = p.order.max {+ (p.sessions[$0]?.lastWorked ?? .distantPast) < (p.sessions[$1]?.lastWorked ?? .distantPast)+ }+ guard let id = working ?? lastWorked, let session = p.sessions[id] else { return nil }return Display(session: id, projectName: session.projectName, cwd: session.cwd,hostApp: session.hostApp, bundleId: session.bundleId)}diff --git a/tests/PillSessionRegistryTests.swift b/tests/PillSessionRegistryTests.swiftindex a2d4e7ef..e4bbcf59 100644--- a/tests/PillSessionRegistryTests.swift+++ b/tests/PillSessionRegistryTests.swift@@ -96,6 +96,46 @@ enum PillSessionRegistryTests {check("B may write the card now", reg.ownsDisplay(pill: claude, session: "b"))}+ // ── the name follows the session that is working ───────────────────────+ // Negative case: "oldest live session owns the name" shows Locker here+ // while only GA works.+ print("\nName follows the working session")+ do {+ var reg = PillSessionRegistry()+ let t0 = Date()+ reg.note(pill: claude, display: display("locker", "locker"), working: true, now: t0)+ reg.note(pill: claude, display: display("ga", "ga"), working: true, now: t0 + 1)+ check("both working: the oldest keeps the name",+ reg.displayOwner(of: claude)?.session == "locker")++ _ = reg.endTurn(pill: claude, session: "locker", now: t0 + 2)+ check("Locker done, GA working: GA's name",+ reg.displayOwner(of: claude)?.projectName == "ga")+ check("GA may write the card", reg.ownsDisplay(pill: claude, session: "ga"))+ check("idle Locker may not", !reg.ownsDisplay(pill: claude, session: "locker"))++ // An idle notification from Locker must not take the name back.+ reg.note(pill: claude, display: display("locker", "locker"), working: false, now: t0 + 3)+ check("a notification from idle Locker keeps GA's name",+ reg.displayOwner(of: claude)?.session == "ga")++ _ = reg.endTurn(pill: claude, session: "ga", now: t0 + 4)+ check("all idle: the session that worked last keeps the name",+ reg.displayOwner(of: claude)?.session == "ga")++ reg.note(pill: claude, display: display("locker", "locker"), working: true, now: t0 + 5)+ check("Locker works again: Locker's name",+ reg.displayOwner(of: claude)?.session == "locker")+ }++ do {+ var reg = PillSessionRegistry()+ reg.note(pill: claude, display: display("a", "coucou"), working: false)+ reg.note(pill: claude, display: display("b", "lokil"), working: false)+ check("no turn seen yet: the oldest owns the name",+ reg.displayOwner(of: claude)?.session == "a")+ }+// ── empty cwd must not erase a known one ────────────────────────────────print("\nPartial payloads")do {@@ -156,7 +196,9 @@ enum PillSessionRegistryTests {check("B's Stop reaches the pill again",reg.endTurn(pill: claude, session: "b", now: late) == .pill)check("A is still live", reg.liveCount(pill: claude) == 2)- check("A still owns the display", reg.displayOwner(of: claude)?.session == "a")+ // A went silent mid-turn; B is the one that just worked, so the name is B's.+ check("B, which worked last, owns the display",+ reg.displayOwner(of: claude)?.session == "b")}// ── a SessionEnd that never arrives must not pin the card forever ──────