Skip to content

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.

fix(hooks): remove only Coucou's own nb-hook entries in the Claude paths

Rahul Ranjan6ed8fa90280f
diff --git a/NotchBuddy/Sources/App/HookServer.swift b/NotchBuddy/Sources/App/HookServer.swift
index d7ef3437..f94b8e17 100644
--- a/NotchBuddy/Sources/App/HookServer.swift
+++ b/NotchBuddy/Sources/App/HookServer.swift
@@ -1478,7 +1478,9 @@ final class HookServer: @unchecked Sendable {
var hooks = try ClaudeSettingsFile.hooks(in: settings, name: "settings.json")
for (event, timeout) in events {
var existing = try ClaudeSettingsFile.hookGroups(in: hooks, event: event, name: "settings.json")
- existing.removeAll { ($0["hooks"] as? [[String: Any]])?.contains { ($0["command"] as? String)?.contains("NotchBuddy") == true || ($0["command"] as? String)?.contains("coucou") == true } ?? false }
+ // Remove only our own nb-hook entries, not whole groups that merely
+ // mention us: a user hook like ~/dev/coucou/scripts/lint.sh must survive.
+ existing = removeNbHookEntries(from: existing)
existing.append(["hooks": [["type": "command", "command": quotedCmd, "timeout": timeout]]])
hooks[event] = existing
}
@@ -1503,12 +1505,9 @@ final class HookServer: @unchecked Sendable {
for key in hooks.keys {
if var matchers = hooks[key] as? [[String: Any]] {
- matchers.removeAll { matcher in
- (matcher["hooks"] as? [[String: Any]])?.contains {
- ($0["command"] as? String)?.contains("NotchBuddy") == true ||
- ($0["command"] as? String)?.contains("coucou") == true
- } ?? false
- }
+ // Per-entry removal, like the Gemini path: user hooks that merely
+ // mention "coucou"/"NotchBuddy" are kept, only nb-hook commands go.
+ matchers = removeNbHookEntries(from: matchers)
if matchers.isEmpty { hooks.removeValue(forKey: key) }
else { hooks[key] = matchers }
}
@@ -1679,12 +1678,9 @@ final class HookServer: @unchecked Sendable {
guard var hooks = settings["hooks"] as? [String: Any] else { return }
for key in hooks.keys {
if var matchers = hooks[key] as? [[String: Any]] {
- matchers.removeAll { matcher in
- (matcher["hooks"] as? [[String: Any]])?.contains {
- ($0["command"] as? String)?.contains("coucou") == true ||
- ($0["command"] as? String)?.contains("NotchBuddy") == true
- } ?? false
- }
+ // Per-entry removal, like the Gemini path: user hooks that merely
+ // mention "coucou"/"NotchBuddy" are kept, only nb-hook commands go.
+ matchers = removeNbHookEntries(from: matchers)
if matchers.isEmpty { hooks.removeValue(forKey: key) }
else { hooks[key] = matchers }
}
@@ -1716,10 +1712,9 @@ final class HookServer: @unchecked Sendable {
var hooks = try ClaudeSettingsFile.hooks(in: settings, name: "settings.json")
for (event, timeout) in events {
var existing = try ClaudeSettingsFile.hookGroups(in: hooks, event: event, name: "settings.json")
- existing.removeAll { ($0["hooks"] as? [[String: Any]])?.contains {
- ($0["command"] as? String)?.contains("coucou") == true ||
- ($0["command"] as? String)?.contains("NotchBuddy") == true
- } ?? false }
+ // Remove only our own nb-hook entries, not whole groups that merely
+ // mention us: a user hook like ~/dev/coucou/scripts/lint.sh must survive.
+ existing = removeNbHookEntries(from: existing)
existing.append(["hooks": [["type": "command", "command": quotedCmd, "timeout": timeout]]])
hooks[event] = existing
}
@@ -2005,9 +2000,10 @@ final class HookServer: @unchecked Sendable {
try data.write(to: url, options: .atomic)
}
- /// Removes entries containing "nb-hook" from a Gemini-format groups array.
- /// Handles both new group format (matcher + hooks[]) and legacy flat format (command at top level).
- /// Returns the cleaned array; empty groups (after inner-hook removal) are dropped.
+ /// Removes entries containing "nb-hook" from a Gemini- or Claude-format groups array
+ /// (matcher + hooks[]), handling the legacy flat format too (command at top level).
+ /// Used by both paths' install/uninstall so only Coucou's own entries are removed;
+ /// whole groups are dropped only when every entry in them was ours.
private func removeNbHookEntries(from groups: [[String: Any]]) -> [[String: Any]] {
groups.compactMap { group -> [String: Any]? in
// Legacy flat entry — command at group level
diff --git a/NotchBuddy/Tests/ClaudeHookRemovalTests.swift b/NotchBuddy/Tests/ClaudeHookRemovalTests.swift
new file mode 100644
index 00000000..81e3a460
--- /dev/null
+++ b/NotchBuddy/Tests/ClaudeHookRemovalTests.swift
@@ -0,0 +1,104 @@
+#!/usr/bin/swift
+// ClaudeHookRemovalTests.swift — standalone test script for the Claude Code
+// hook install/uninstall removal logic in HookServer.swift.
+// Run from repo root:
+// swift NotchBuddy/Tests/ClaudeHookRemovalTests.swift
+//
+// Covers #854: install and uninstall must remove only Coucou's own nb-hook
+// entries, never whole groups that merely mention "coucou"/"NotchBuddy".
+// Keep in sync with HookServer.swift (removeNbHookEntries + the per-event
+// cleanup in buildHooksData / uninstallClaudeHooks).
+
+import Foundation
+
+var passed = 0
+var failed = 0
+
+func check(_ name: String, _ groups: [[String: Any]],
+ contains: [String] = [], notContains: [String] = [],
+ expectEmpty: Bool = false) {
+ var ok = expectEmpty ? groups.isEmpty : true
+ if !expectEmpty {
+ let data = (try? JSONSerialization.data(withJSONObject: groups, options: [.sortedKeys])) ?? Data()
+ let json = String(data: data, encoding: .utf8) ?? ""
+ for needle in contains where !json.contains(needle) { ok = false }
+ for needle in notContains where json.contains(needle) { ok = false }
+ }
+ if ok { passed += 1; print("PASS \(name)") }
+ else { failed += 1; print("FAIL \(name)") }
+}
+
+// ── Copy of removeNbHookEntries from HookServer.swift ─────────────────────────
+// Keep in sync with HookServer.swift removeNbHookEntries(from:).
+func removeNbHookEntries(from groups: [[String: Any]]) -> [[String: Any]] {
+ groups.compactMap { group -> [String: Any]? in
+ // Legacy flat entry — command at group level
+ if let cmd = group["command"] as? String, cmd.contains("nb-hook") { return nil }
+ // Group format — filter inner hooks
+ if var innerHooks = group["hooks"] as? [[String: Any]] {
+ innerHooks.removeAll { ($0["command"] as? String)?.contains("nb-hook") == true }
+ if innerHooks.isEmpty { return nil }
+ var updated = group
+ updated["hooks"] = innerHooks
+ return updated
+ }
+ return group
+ }
+}
+
+// ── Mirror of the per-event cleanup the Claude paths run ──────────────────────
+// buildHooksData removes Coucou's own previous entries, then appends a fresh one.
+let nbCmd = "\"$HOME/Library/Application Support/NotchBuddy/nb-hook\""
+
+func installEvent(_ existing: [[String: Any]], timeout: Int) -> [[String: Any]] {
+ var cleaned = removeNbHookEntries(from: existing)
+ cleaned.append(["hooks": [["type": "command", "command": nbCmd, "timeout": timeout]]])
+ return cleaned
+}
+
+// 1. Install keeps a user group whose command only mentions "coucou"
+let userLint = ["matcher": "Bash",
+ "hooks": [["type": "command", "command": "~/dev/coucou/scripts/lint.sh", "timeout": 5]]] as [String: Any]
+let coucouGroup = ["hooks": [["type": "command", "command": nbCmd, "timeout": 10]]] as [String: Any]
+check("1. install keeps the user's ~/dev/coucou lint group",
+ installEvent([userLint, coucouGroup], timeout: 10),
+ contains: ["~/dev/coucou/scripts/lint.sh", "Bash"])
+
+// 2. Install replaces Coucou's own previous group without duplicating it
+let installedTwice = installEvent(installEvent([coucouGroup], timeout: 10), timeout: 10)
+let nbPlainCount = installedTwice.flatMap { ($0["hooks"] as? [[String: Any]]) ?? [] }
+ .filter { ($0["command"] as? String) == nbCmd }.count
+if nbPlainCount == 1 { passed += 1; print("PASS 2. reinstall leaves exactly one plain nb-hook entry") }
+else { failed += 1; print("FAIL 2. reinstall leaves exactly one plain nb-hook entry (found \(nbPlainCount))") }
+
+// 3. Uninstall keeps a user hook that merely mentions "coucou"
+let mixed = [["matcher": "n",
+ "hooks": [["type": "command", "command": "echo coucou", "timeout": 5],
+ ["type": "command", "command": nbCmd, "timeout": 10]]] as [String: Any]]
+check("3. uninstall keeps \"echo coucou\", drops only the nb-hook entry",
+ removeNbHookEntries(from: mixed),
+ contains: ["echo coucou"],
+ notContains: ["nb-hook"])
+
+// 4. Uninstall drops a group that held only Coucou's hook
+check("4. uninstall drops coucou-only groups entirely",
+ removeNbHookEntries(from: [coucouGroup]),
+ expectEmpty: true)
+
+// 5. Uninstall keeps a user hook whose path mentions NotchBuddy but not nb-hook
+let nbish = [["hooks": [["type": "command", "command": "/opt/NotchBuddy/something-else", "timeout": 5]]] as [String: Any]
+check("5. uninstall keeps a /opt/NotchBuddy path that is not our hook",
+ removeNbHookEntries(from: nbish),
+ contains: ["/opt/NotchBuddy/something-else"])
+
+// 6. Legacy flat entries with nb-hook are removed; others are kept
+let legacy = [["command": nbCmd] as [String: Any],
+ ["command": "~/bin/own.sh"] as [String: Any]]
+check("6. legacy flat entries: only the nb-hook one goes",
+ removeNbHookEntries(from: legacy),
+ contains: ["own.sh"],
+ notContains: ["nb-hook"])
+
+print("")
+print("Results: \(passed) passed, \(failed) failed")
+exit(failed > 0 ? 1 : 0)