Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(agents): per-agent hook services + installer registry

enes51bbdba9d94a
diff --git a/src/main/agents/hooks/claude.ts b/src/main/agents/hooks/claude.ts
new file mode 100644
index 000000000..adf88b48b
--- /dev/null
+++ b/src/main/agents/hooks/claude.ts
@@ -0,0 +1,38 @@
+// Claude hook service. Installs the managed script into ~/.claude/settings.json under
+// each Claude Code hook event. Thin wrapper over the shared install helper.
+import { homedir } from 'os'
+import path from 'path'
+import { installHooksInto, removeHooksFrom } from './install-helper'
+
+const CLAUDE_EVENTS = [
+ 'SessionStart',
+ 'UserPromptSubmit',
+ 'Stop',
+ 'Notification',
+ 'SessionEnd',
+ 'PreToolUse',
+ 'PostToolUse'
+] as const
+
+const SCRIPT_FILE_NAME = 'claude.sh'
+
+function configPath(): string {
+ return path.join(homedir(), '.claude', 'settings.json')
+}
+
+export function installClaudeHooks(): void {
+ installHooksInto({
+ agentId: 'claude',
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: configPath(),
+ events: CLAUDE_EVENTS
+ })
+}
+
+export function removeClaudeHooks(): void {
+ removeHooksFrom({
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: configPath(),
+ events: CLAUDE_EVENTS
+ })
+}
diff --git a/src/main/agents/hooks/codex.ts b/src/main/agents/hooks/codex.ts
new file mode 100644
index 000000000..ccde4f2f4
--- /dev/null
+++ b/src/main/agents/hooks/codex.ts
@@ -0,0 +1,35 @@
+// Codex hook service. Installs the managed script into codex's config under each codex
+// hook event. Thin wrapper over the shared install helper.
+//
+// NOTE: the config path and event names below are PLACEHOLDERS to be confirmed/refined in
+// the later codex task (Phase 4). `~/.codex/config.json` is a reasonable convention
+// (`~/.codex/` exists on disk); codex's real config is TOML and its hook event names are
+// not yet verified — do not rely on these until Phase 4.
+import { homedir } from 'os'
+import path from 'path'
+import { installHooksInto, removeHooksFrom } from './install-helper'
+
+const CODEX_EVENTS = ['UserPromptSubmit', 'Stop'] as const
+
+const SCRIPT_FILE_NAME = 'codex.sh'
+
+function configPath(): string {
+ return path.join(homedir(), '.codex', 'config.json')
+}
+
+export function installCodexHooks(): void {
+ installHooksInto({
+ agentId: 'codex',
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: configPath(),
+ events: CODEX_EVENTS
+ })
+}
+
+export function removeCodexHooks(): void {
+ removeHooksFrom({
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: configPath(),
+ events: CODEX_EVENTS
+ })
+}
diff --git a/src/main/agents/hooks/gemini.ts b/src/main/agents/hooks/gemini.ts
new file mode 100644
index 000000000..dead691e3
--- /dev/null
+++ b/src/main/agents/hooks/gemini.ts
@@ -0,0 +1,30 @@
+// Gemini hook service. Installs the managed script into ~/.gemini/settings.json under
+// each Gemini hook event. Thin wrapper over the shared install helper.
+import { homedir } from 'os'
+import path from 'path'
+import { installHooksInto, removeHooksFrom } from './install-helper'
+
+const GEMINI_EVENTS = ['BeforeAgent', 'AfterAgent', 'AfterTool', 'BeforeTool'] as const
+
+const SCRIPT_FILE_NAME = 'gemini.sh'
+
+function configPath(): string {
+ return path.join(homedir(), '.gemini', 'settings.json')
+}
+
+export function installGeminiHooks(): void {
+ installHooksInto({
+ agentId: 'gemini',
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: configPath(),
+ events: GEMINI_EVENTS
+ })
+}
+
+export function removeGeminiHooks(): void {
+ removeHooksFrom({
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: configPath(),
+ events: GEMINI_EVENTS
+ })
+}
diff --git a/src/main/agents/hooks/index.ts b/src/main/agents/hooks/index.ts
new file mode 100644
index 000000000..68c8ae03e
--- /dev/null
+++ b/src/main/agents/hooks/index.ts
@@ -0,0 +1,40 @@
+// Installer registry: drives install/remove of the managed hook script across every
+// built-in agent. Each call is wrapped in try/catch with a per-agent console.warn so one
+// agent failing never blocks the others (fail open).
+import { installClaudeHooks, removeClaudeHooks } from './claude'
+import { installCodexHooks, removeCodexHooks } from './codex'
+import { installGeminiHooks, removeGeminiHooks } from './gemini'
+
+type HookInstaller = readonly [string, () => void]
+
+export const MANAGED_HOOK_INSTALLERS: readonly HookInstaller[] = [
+ ['claude', installClaudeHooks],
+ ['codex', installCodexHooks],
+ ['gemini', installGeminiHooks]
+]
+
+export const MANAGED_HOOK_REMOVERS: readonly HookInstaller[] = [
+ ['claude', removeClaudeHooks],
+ ['codex', removeCodexHooks],
+ ['gemini', removeGeminiHooks]
+]
+
+export function installManagedAgentHooks(): void {
+ for (const [agent, install] of MANAGED_HOOK_INSTALLERS) {
+ try {
+ install()
+ } catch (e) {
+ console.warn(`[agent-hooks] ${agent} install failed`, e)
+ }
+ }
+}
+
+export function removeManagedAgentHooks(): void {
+ for (const [agent, remove] of MANAGED_HOOK_REMOVERS) {
+ try {
+ remove()
+ } catch (e) {
+ console.warn(`[agent-hooks] ${agent} remove failed`, e)
+ }
+ }
+}
diff --git a/src/main/agents/hooks/install-helper.ts b/src/main/agents/hooks/install-helper.ts
new file mode 100644
index 000000000..20fc11bd3
--- /dev/null
+++ b/src/main/agents/hooks/install-helper.ts
@@ -0,0 +1,101 @@
+// The ONE implementation of the per-agent settings.json hook merge. Each agent's thin
+// service (claude/codex/gemini) calls these with its own config path, script filename,
+// and event list. Behavior (ported from the original claude-hooks.ts):
+// - write the managed script for `agentId` to <userData>/agent-hooks/<scriptFileName>
+// (chmod 0o755, best-effort), then reference it as `sh "<scriptPath>"` from each event;
+// - idempotent re-install: drop any prior managed entry for that event (command includes
+// the `agent-hooks` path segment OR the legacy `claude-signals` marker) before pushing
+// the fresh one;
+// - preserve every other hook (other tools', other events);
+// - fail open: a missing/unparseable settings.json defaults to {} (install) / returns
+// early (remove); a write error is caught + warned, never thrown.
+import path from 'path'
+import { app } from 'electron'
+import { readFileSync, writeFileSync, mkdirSync, chmodSync } from 'fs'
+import { buildManagedScript } from './managed-script'
+
+type HookDef = { hooks?: { type: string; command: string }[] }
+type Settings = { hooks?: Record<string, HookDef[]>; [k: string]: unknown }
+
+function scriptPathFor(scriptFileName: string): string {
+ return path.join(app.getPath('userData'), 'agent-hooks', scriptFileName)
+}
+
+/** A managed entry: matches the live `agent-hooks/` path or the legacy `claude-signals` marker. */
+function isManaged(d: HookDef): boolean {
+ return !!d.hooks?.some(
+ (h) => h.command.includes('agent-hooks') || h.command.includes('claude-signals')
+ )
+}
+
+export interface InstallHooksOptions {
+ agentId: string
+ scriptFileName: string
+ configPath: string
+ events: readonly string[]
+}
+
+export function installHooksInto(opts: InstallHooksOptions): void {
+ const { agentId, scriptFileName, configPath, events } = opts
+
+ const sp = scriptPathFor(scriptFileName)
+ try {
+ mkdirSync(path.dirname(sp), { recursive: true })
+ writeFileSync(sp, buildManagedScript(agentId), 'utf8')
+ } catch (e) {
+ console.warn(`[agent-hooks] ${agentId} script write failed`, e)
+ return
+ }
+ try {
+ chmodSync(sp, 0o755)
+ } catch {
+ /* fail open */
+ }
+
+ const command = `sh "${sp}"`
+ let config: Settings = {}
+ try {
+ config = JSON.parse(readFileSync(configPath, 'utf8')) as Settings
+ } catch {
+ config = {}
+ }
+ config.hooks = config.hooks ?? {}
+ for (const ev of events) {
+ const existing = (config.hooks[ev] ?? []).filter((d) => !isManaged(d))
+ existing.push({ hooks: [{ type: 'command', command }] })
+ config.hooks[ev] = existing
+ }
+ try {
+ mkdirSync(path.dirname(configPath), { recursive: true })
+ writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8')
+ } catch (e) {
+ console.warn(`[agent-hooks] ${agentId} install failed`, e)
+ }
+}
+
+export interface RemoveHooksOptions {
+ scriptFileName?: string
+ configPath: string
+ events: readonly string[]
+}
+
+export function removeHooksFrom(opts: RemoveHooksOptions): void {
+ const { configPath, events } = opts
+ let config: Settings
+ try {
+ config = JSON.parse(readFileSync(configPath, 'utf8')) as Settings
+ } catch {
+ return
+ }
+ if (!config.hooks) return
+ for (const ev of events) {
+ if (!config.hooks[ev]) continue
+ config.hooks[ev] = config.hooks[ev].filter((d) => !d.hooks?.some((h) => h.command.includes('agent-hooks')))
+ if (config.hooks[ev].length === 0) delete config.hooks[ev]
+ }
+ try {
+ writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8')
+ } catch {
+ /* fail open */
+ }
+}
diff --git a/src/main/agents/hooks/managed-script.ts b/src/main/agents/hooks/managed-script.ts
new file mode 100644
index 000000000..4236c66e9
--- /dev/null
+++ b/src/main/agents/hooks/managed-script.ts
@@ -0,0 +1,28 @@
+// Generates the managed hook script installed into an agent's own config.
+// It sources the endpoint file for the LIVE port/token (restart handoff), no-ops
+// outside nodeterm-spawned sessions (gating via NODETERM_NODE_ID), and posts the
+// raw hook payload to the loopback server. Fails open at every step.
+export function buildManagedScript(agentId: string): string {
+ return [
+ '#!/bin/sh',
+ 'if [ -n "$NODETERM_HOOK_ENDPOINT" ] && [ -r "$NODETERM_HOOK_ENDPOINT" ]; then',
+ ' . "$NODETERM_HOOK_ENDPOINT" 2>/dev/null || :',
+ 'fi',
+ 'if [ -z "$NODETERM_HOOK_PORT" ] || [ -z "$NODETERM_HOOK_TOKEN" ] || [ -z "$NODETERM_NODE_ID" ]; then',
+ ' exit 0',
+ 'fi',
+ 'payload=$(cat)',
+ 'if [ -z "$payload" ]; then',
+ ' exit 0',
+ 'fi',
+ `curl -sS -X POST "http://127.0.0.1:\${NODETERM_HOOK_PORT}/hook/${agentId}" \\`,
+ ' --connect-timeout 0.5 --max-time 1.5 \\',
+ ' -H "Content-Type: application/x-www-form-urlencoded" \\',
+ ' -H "X-Nodeterm-Hook-Token: ${NODETERM_HOOK_TOKEN}" \\',
+ ' --data-urlencode "nodeId=${NODETERM_NODE_ID}" \\',
+ ' --data-urlencode "version=${NODETERM_HOOK_VERSION}" \\',
+ ' --data-urlencode "payload=${payload}" >/dev/null 2>&1 || true',
+ 'exit 0',
+ ''
+ ].join('\n')
+}