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
Open terminal: bring back the app the session runs in
Mateus Amaral638a4ca8aedc
diff --git a/NotchBuddy/Sources/App/HookServer.swift b/NotchBuddy/Sources/App/HookServer.swiftindex 26dde14c..69ae96ed 100644--- a/NotchBuddy/Sources/App/HookServer.swift+++ b/NotchBuddy/Sources/App/HookServer.swift@@ -418,7 +418,7 @@ final class HookServer: @unchecked Sendable {case "SessionStart":activeSessionId = sessionId- if isExternalAgent { upsertExternalAgent(id: agentId, name: validAgent!) } else { upsertWorkspaceTask(id: agentId, projectName: projectName, cwd: cwd) }+ if isExternalAgent { upsertExternalAgent(id: agentId, name: validAgent!) } else { upsertWorkspaceTask(id: agentId, projectName: projectName, cwd: cwd, bundleId: bundleId) }if let idx = state.tasks.firstIndex(where: { $0.id == agentId }) { state.tasks[idx].finalLine = nil }nbLog("SessionStart \(isExternalAgent ? agentId : projectName) (\(sessionId.prefix(8)))")if state.isPresent { expandIfNeeded(to: .overview) }@@ -426,7 +426,7 @@ final class HookServer: @unchecked Sendable {case "UserPromptSubmit":activeSessionId = sessionId- if isExternalAgent { upsertExternalAgent(id: agentId, name: validAgent!) } else { upsertWorkspaceTask(id: agentId, projectName: projectName, cwd: cwd) }+ if isExternalAgent { upsertExternalAgent(id: agentId, name: validAgent!) } else { upsertWorkspaceTask(id: agentId, projectName: projectName, cwd: cwd, bundleId: bundleId) }if let idx = state.tasks.firstIndex(where: { $0.id == agentId }) { state.tasks[idx].finalLine = nil }state.updateTask(id: agentId, state: .thinking)if let prompt = payload["prompt"] as? String, !prompt.isEmpty {@@ -441,7 +441,7 @@ final class HookServer: @unchecked Sendable {// AskUserQuestion is handled via the dedicated --ask hook.// Skip state/step update here to avoid flickering over the question card.guard tool != "AskUserQuestion" else { break }- if isExternalAgent { upsertExternalAgent(id: agentId, name: validAgent!) } else { upsertWorkspaceTask(id: agentId, projectName: projectName, cwd: cwd) }+ if isExternalAgent { upsertExternalAgent(id: agentId, name: validAgent!) } else { upsertWorkspaceTask(id: agentId, projectName: projectName, cwd: cwd, bundleId: bundleId) }state.updateTask(id: agentId, state: .working)let input = payload["tool_input"] as? [String: Any] ?? [:]let step = frenchStep(tool: tool, input: input)@@ -689,7 +689,7 @@ final class HookServer: @unchecked Sendable {pendingApprovalFD = fdactiveSessionId = sessionId- upsertWorkspaceTask(id: pillId, projectName: projectName, cwd: cwd)+ upsertWorkspaceTask(id: pillId, projectName: projectName, cwd: cwd, bundleId: bundleId)state.updateTask(id: pillId, state: .approval)state.pendingApproval = ApprovalInfo(sessionId: sessionId, tool: tool,command: command, inputKey: inputKey, pillId: pillId)@@ -833,7 +833,7 @@ final class HookServer: @unchecked Sendable {activeSessionId = sessionIdquestionPillId = pillId- upsertWorkspaceTask(id: pillId, projectName: projectName, cwd: cwd)+ upsertWorkspaceTask(id: pillId, projectName: projectName, cwd: cwd, bundleId: bundleId)state.updateTask(id: pillId, state: .question)state.pendingQuestion = parsedstate.isPinned = true@@ -872,19 +872,21 @@ final class HookServer: @unchecked Sendable {/// If the task already exists (persistent), just updates name/cwd./// If missing (transient), creates it and inserts after the main pill.@MainActor- private func upsertWorkspaceTask(id: String, projectName: String, cwd: String = "") {+ private func upsertWorkspaceTask(id: String, projectName: String, cwd: String = "", bundleId: String = "") {let state = AppState.sharedif let idx = state.tasks.firstIndex(where: { $0.id == id }) {state.tasks[idx].name = projectNameif !cwd.isEmpty { state.tasks[idx].sessionCwd = cwd }+ if !bundleId.isEmpty { state.tasks[idx].sessionBundleId = bundleId }return}// Transient: create and insert after the main pilllet def = PillCatalog.definition(for: id)let color = def?.color ?? "#C0C4CC"let source = def?.source ?? .agent- let task = AgentTask(id: id, name: projectName, color: color,+ var task = AgentTask(id: id, name: projectName, color: color,state: .idle, steps: [], source: source, isIntegration: true)+ if !bundleId.isEmpty { task.sessionBundleId = bundleId }if let mainIdx = state.tasks.firstIndex(where: { $0.id == state.mainPillId }) {state.tasks.insert(task, at: mainIdx + 1)} else {diff --git a/NotchBuddy/Sources/App/IslandViewContent.swift b/NotchBuddy/Sources/App/IslandViewContent.swiftindex 3947f0d6..d66c82c1 100644--- a/NotchBuddy/Sources/App/IslandViewContent.swift+++ b/NotchBuddy/Sources/App/IslandViewContent.swift@@ -219,13 +219,7 @@ struct OverviewView: View {#endifcase "agent_gemini", "agent_antigravity":#if !APPSTORE- let terminalBundleIds = ["com.apple.Terminal", "com.googlecode.iterm2",- "net.kovidgoyal.kitty", "com.mitchellh.ghostty"]- if let hit = terminalBundleIds.compactMap({ id in- NSWorkspace.shared.runningApplications.first { $0.bundleIdentifier == id }- }).first {- hit.activate(options: .activateIgnoringOtherApps)- }+ TerminalTarget.activate(sessionBundleId: nil)#endifcase "ai_anthropic":switchChatProvider(.anthropic)@@ -249,13 +243,7 @@ struct OverviewView: View {}} else {#if !APPSTORE- let terminalBundleIds = ["com.apple.Terminal", "com.googlecode.iterm2",- "net.kovidgoyal.kitty", "com.mitchellh.ghostty"]- if let hit = terminalBundleIds.compactMap({ id in- NSWorkspace.shared.runningApplications.first { $0.bundleIdentifier == id }- }).first {- hit.activate(options: .activateIgnoringOtherApps)- }+ TerminalTarget.activate(sessionBundleId: task.sessionBundleId)#endif}}@@ -560,11 +548,7 @@ struct FinishedView: View {HStack(spacing: 8) {#if !APPSTOREPrimaryButton("Open terminal") {- let terminalBundleIds = ["com.apple.Terminal", "com.googlecode.iterm2", "net.kovidgoyal.kitty", "com.mitchellh.ghostty"]- let activated = terminalBundleIds.compactMap { id in- NSWorkspace.shared.runningApplications.first { $0.bundleIdentifier == id }- }.first.map { $0.activate(options: .activateIgnoringOtherApps) }- if activated == nil {+ if !TerminalTarget.activate(sessionBundleId: state.focusTask?.sessionBundleId) {NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Applications/Utilities/Terminal.app"))}NotificationCenter.default.post(name: .islandCollapse, object: nil)diff --git a/NotchBuddy/Sources/App/IslandWindowController.swift b/NotchBuddy/Sources/App/IslandWindowController.swiftindex c49e37ff..863087a5 100644--- a/NotchBuddy/Sources/App/IslandWindowController.swift+++ b/NotchBuddy/Sources/App/IslandWindowController.swift@@ -543,12 +543,7 @@ final class IslandWindowController: NSWindowController {NotificationCenter.default.post(name: .triggerEmote, object: BotEmote.annoyed)return}- let terminalBundleIds = ["com.apple.Terminal", "com.googlecode.iterm2",- "net.kovidgoyal.kitty", "com.mitchellh.ghostty"]- let activated = terminalBundleIds.compactMap { id in- NSWorkspace.shared.runningApplications.first { $0.bundleIdentifier == id }- }.first.map { $0.activate(options: .activateIgnoringOtherApps) }- if activated == nil {+ if !TerminalTarget.activate(sessionBundleId: state.focusTask?.sessionBundleId) {NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Applications/Utilities/Terminal.app"))}diff --git a/NotchBuddy/Sources/App/TerminalTarget+AppKit.swift b/NotchBuddy/Sources/App/TerminalTarget+AppKit.swiftnew file mode 100644index 00000000..ad9cfe8d--- /dev/null+++ b/NotchBuddy/Sources/App/TerminalTarget+AppKit.swift@@ -0,0 +1,14 @@+import AppKit++extension TerminalTarget {+ /// Brings the session's terminal to the front: the app the session runs in first,+ /// then the first running known terminal. Returns false if none is running.+ @discardableResult+ static func activate(sessionBundleId: String?) -> Bool {+ let apps = NSWorkspace.shared.runningApplications+ let running = Set(apps.compactMap(\.bundleIdentifier))+ guard let id = pick(sessionBundleId: sessionBundleId, running: running),+ let app = apps.first(where: { $0.bundleIdentifier == id }) else { return false }+ return app.activate(options: .activateIgnoringOtherApps)+ }+}diff --git a/NotchBuddy/Sources/App/TerminalTarget.swift b/NotchBuddy/Sources/App/TerminalTarget.swiftnew file mode 100644index 00000000..44f4411d--- /dev/null+++ b/NotchBuddy/Sources/App/TerminalTarget.swift@@ -0,0 +1,25 @@+import Foundation++/// Picks which app "Open terminal" / ⌃⌥T should bring to the front.+/// Foundation only, so it can be tested without AppKit (see scripts/test-terminal-target.sh).+enum TerminalTarget {+ /// Terminals tried, in order, when the session's own app is unknown or not running.+ static let knownTerminals = [+ "com.apple.Terminal", "com.googlecode.iterm2", "net.kovidgoyal.kitty",+ "com.mitchellh.ghostty", "com.cmuxterm.app", "dev.warp.Warp-Stable",+ "com.github.wez.wezterm", "org.alacritty",+ ]++ /// Bundle IDs to try, in order: the app the session runs in (from the hook's+ /// `bundle_id`, i.e. `__CFBundleIdentifier`), then the known terminals.+ static func candidates(sessionBundleId: String?) -> [String] {+ let own = sessionBundleId?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""+ guard !own.isEmpty else { return knownTerminals }+ return [own] + knownTerminals.filter { $0 != own }+ }++ /// First candidate that is currently running, or nil.+ static func pick(sessionBundleId: String?, running: Set<String>) -> String? {+ candidates(sessionBundleId: sessionBundleId).first { running.contains($0) }+ }+}diff --git a/NotchBuddy/Sources/CoucouKit/IslandTypes.swift b/NotchBuddy/Sources/CoucouKit/IslandTypes.swiftindex 60fbbe1a..bf3c2e53 100644--- a/NotchBuddy/Sources/CoucouKit/IslandTypes.swift+++ b/NotchBuddy/Sources/CoucouKit/IslandTypes.swift@@ -59,6 +59,7 @@ struct AgentTask: Identifiable, Equatable {var miniEye: EyeShape? = nilvar pillBadge: PillBadge? = nil // alert badge shown on pill when not focusedvar sessionCwd: String? = nil // last known working directory (Claude Code sessions)+ var sessionBundleId: String? = nil // app the session runs in (hook bundle_id), for "Open terminal"var finalLine: String? = nil // last assistant message shown as static text after Stop}diff --git a/scripts/test-terminal-target.sh b/scripts/test-terminal-target.shnew file mode 100755index 00000000..32943a9e--- /dev/null+++ b/scripts/test-terminal-target.sh@@ -0,0 +1,8 @@+#!/usr/bin/env bash+set -euo pipefail+cd "$(dirname "$0")/.."+TEST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/coucou-terminal-target.XXXXXX")"+trap 'rm -rf "$TEST_DIR"' EXIT+swiftc NotchBuddy/Sources/App/TerminalTarget.swift \+ tests/TerminalTargetTests.swift -o "$TEST_DIR/terminal-target-tests"+"$TEST_DIR/terminal-target-tests"diff --git a/tests/TerminalTargetTests.swift b/tests/TerminalTargetTests.swiftnew file mode 100644index 00000000..2023a688--- /dev/null+++ b/tests/TerminalTargetTests.swift@@ -0,0 +1,44 @@+import Foundation++@main+struct TerminalTargetTests {+ static func main() {+ var failures = 0+ func check(_ cond: Bool, _ msg: String) {+ if cond { print("✓ \(msg)") } else { print("✗ \(msg)"); failures += 1 }+ }++ // The session's own app wins over the fixed list+ check(TerminalTarget.pick(sessionBundleId: "com.cmuxterm.app",+ running: ["com.apple.Terminal", "com.cmuxterm.app"]) == "com.cmuxterm.app",+ "session app (cmux) is preferred over a running Terminal")++ // VS Code integrated terminal goes back to VS Code+ check(TerminalTarget.pick(sessionBundleId: "com.microsoft.VSCode",+ running: ["com.microsoft.VSCode", "com.googlecode.iterm2"]) == "com.microsoft.VSCode",+ "VS Code session goes back to VS Code")++ // Session app not running → fall back to known terminals, in order+ check(TerminalTarget.pick(sessionBundleId: "com.cmuxterm.app",+ running: ["com.googlecode.iterm2", "com.mitchellh.ghostty"]) == "com.googlecode.iterm2",+ "falls back to the first running known terminal")++ // No bundle id → known terminals only, cmux included+ check(TerminalTarget.pick(sessionBundleId: nil, running: ["com.cmuxterm.app"]) == "com.cmuxterm.app",+ "cmux is in the fallback list")+ check(TerminalTarget.pick(sessionBundleId: " ", running: ["dev.warp.Warp-Stable"]) == "dev.warp.Warp-Stable",+ "blank bundle id is ignored")++ // Nothing running → nil (caller opens Terminal.app as before)+ check(TerminalTarget.pick(sessionBundleId: "com.cmuxterm.app", running: []) == nil,+ "nothing running returns nil")++ // No duplicates when the session app is already a known terminal+ let c = TerminalTarget.candidates(sessionBundleId: "com.mitchellh.ghostty")+ check(c.first == "com.mitchellh.ghostty" && c.filter { $0 == "com.mitchellh.ghostty" }.count == 1,+ "session app is listed once, first")++ if failures > 0 { print("\(failures) failure(s)"); exit(1) }+ print("All terminal target tests passed")+ }+}