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.

fix(agent-hooks): a missing managed script must not block every prompt

enes9d07c8549962
diff --git a/src/core/agents/hooks/install-helper.test.ts b/src/core/agents/hooks/install-helper.test.ts
index a6170c750..98085ca21 100644
--- a/src/core/agents/hooks/install-helper.test.ts
+++ b/src/core/agents/hooks/install-helper.test.ts
@@ -1,7 +1,31 @@
import { describe, expect, it } from 'vitest'
-import { mergeManagedHook } from './install-helper'
+import { buildManagedHookCommand, mergeManagedHook } from './install-helper'
-const cmd = 'sh "/remote/.nodeterm/agent-hooks/claude.sh"'
+const cmd = buildManagedHookCommand('/remote/.nodeterm/agent-hooks/claude.sh')
+
+describe('buildManagedHookCommand', () => {
+ it('runs the script only when it is still readable, and exits 0 otherwise', () => {
+ // The whole point: a stale entry (uninstalled app, cleared data dir, a server --data-dir
+ // under a temp path) must not exit non-zero — that BLOCKS every UserPromptSubmit.
+ expect(cmd).toBe(
+ "if [ -r '/remote/.nodeterm/agent-hooks/claude.sh' ]; then sh '/remote/.nodeterm/agent-hooks/claude.sh'; else cat >/dev/null 2>&1 || :; fi"
+ )
+ })
+ it("single-quote escapes the path so a quote or $ in it can't break out", () => {
+ expect(buildManagedHookCommand("/a'b/$x/agent-hooks/claude.sh")).toContain(
+ "'/a'\\''b/$x/agent-hooks/claude.sh'"
+ )
+ })
+ it('still carries the marker that makes the entry ours', () => {
+ const out = mergeManagedHook({}, cmd, ['Stop'])
+ expect(mergeManagedHook(out, cmd, ['Stop']).hooks!.Stop).toHaveLength(1)
+ })
+ it('replaces the pre-guard `sh "<path>"` entry from an older install', () => {
+ const legacy = { hooks: [{ type: 'command', command: 'sh "/old/data/agent-hooks/claude.sh"' }] }
+ const out = mergeManagedHook({ hooks: { UserPromptSubmit: [legacy] } }, cmd, ['UserPromptSubmit'])
+ expect(out.hooks!.UserPromptSubmit).toEqual([{ hooks: [{ type: 'command', command: cmd }] }])
+ })
+})
describe('mergeManagedHook', () => {
it('adds the managed command to each event, preserving other tools hooks', () => {
diff --git a/src/core/agents/hooks/install-helper.ts b/src/core/agents/hooks/install-helper.ts
index 2f014e5b0..6b571fab2 100644
--- a/src/core/agents/hooks/install-helper.ts
+++ b/src/core/agents/hooks/install-helper.ts
@@ -24,6 +24,28 @@ function scriptPathFor(scriptFileName: string): string {
return path.join(platform().userDataDir, 'agent-hooks', scriptFileName)
}
+/**
+ * The managed hook command: run our script, but ONLY if it is still on disk.
+ *
+ * The guard is not cosmetic. A bare `sh "<path>"` exits non-zero when the script is gone, and a
+ * non-zero UserPromptSubmit hook BLOCKS the prompt — so one stale entry bricks every Claude
+ * session on that machine ("cannot open …: No such file", nothing can be submitted) until the
+ * user hand-edits settings.json. The entry goes stale in ordinary situations: the app is
+ * uninstalled, its user-data dir is cleared, or the server edition ran with a `--data-dir` under
+ * a temp path that later got cleaned (see the installHooks note in src/server/config.ts).
+ *
+ * Missing script → swallow stdin (hooks are fed JSON on stdin) and exit 0: the agent simply runs
+ * without status, which is exactly what an uninstalled nodeterm should look like.
+ *
+ * Codex builds its own equivalent (`buildManagedCommand` in codex.ts) — its exact bytes are
+ * hashed into config.toml's trust entries, so the two must stay separate.
+ */
+export function buildManagedHookCommand(scriptPath: string): string {
+ // POSIX single-quote escape so $, `, " and \ in the path are taken literally.
+ const q = `'${scriptPath.replaceAll("'", "'\\''")}'`
+ return `if [ -r ${q} ]; then sh ${q}; else cat >/dev/null 2>&1 || :; fi`
+}
+
// The marker identifying OUR entry: the `agent-hooks/<scriptFile>` tail of the managed
// command. A bare "agent-hooks" substring is NOT enough — other tools use the same dir
// name (e.g. `~/.someapp/agent-hooks/claude-hook.sh`), and matching them would delete a
@@ -76,7 +98,7 @@ export function installHooksInto(opts: InstallHooksOptions): void {
/* fail open */
}
- const command = `sh "${sp}"`
+ const command = buildManagedHookCommand(sp)
let config: Settings = {}
try {
config = JSON.parse(readFileSync(configPath, 'utf8')) as Settings
diff --git a/src/main/remote-ssh/remote-hooks.test.ts b/src/main/remote-ssh/remote-hooks.test.ts
index 7804442e4..66645c48f 100644
--- a/src/main/remote-ssh/remote-hooks.test.ts
+++ b/src/main/remote-ssh/remote-hooks.test.ts
@@ -41,12 +41,17 @@ describe('RemoteHooks.setup', () => {
(c.stdin ?? '').includes('NODETERM_HOOK_SOCK=/home/u/.nodeterm/hook-p1.sock')
)
).toBe(true)
- // managed script written to the absolute path + config merged with `sh "<abs script>"`.
+ // managed script written to the absolute path + config merged with the guarded command.
expect(joined.some((j) => j.includes('cat > /home/u/.nodeterm/agent-hooks/claude.sh'))).toBe(true)
expect(joined.some((j) => j.includes('cat > /home/u/.claude/settings.json'))).toBe(true)
expect(calls.some((c) => (c.stdin ?? '').includes('--unix-socket'))).toBe(true)
- // merged config is JSON, so the command quotes are escaped: sh \"<abs script>\".
- expect(calls.some((c) => (c.stdin ?? '').includes('sh \\"/home/u/.nodeterm/agent-hooks/claude.sh\\"'))).toBe(true)
+ // The merged command guards on the script still existing — a removed ~/.nodeterm must not
+ // make every prompt fail the hook (a non-zero UserPromptSubmit hook blocks the prompt).
+ expect(
+ calls.some((c) =>
+ (c.stdin ?? '').includes("if [ -r '/home/u/.nodeterm/agent-hooks/claude.sh' ]; then sh ")
+ )
+ ).toBe(true)
expect(calls.some((c) => (c.stdin ?? '').includes('"hooks"'))).toBe(true)
// no unexpanded tilde survives in any remote path/command.
expect(joined.some((j) => j.includes('~/'))).toBe(false)
diff --git a/src/main/remote-ssh/remote-hooks.ts b/src/main/remote-ssh/remote-hooks.ts
index ee85ab99e..f3fc90a3e 100644
--- a/src/main/remote-ssh/remote-hooks.ts
+++ b/src/main/remote-ssh/remote-hooks.ts
@@ -6,7 +6,7 @@
// runner so the flow is unit-testable without real ssh/electron.
import { childArgs, hookForwardArgs, hookForwardCancelArgs, remoteEndpointFileContents } from '../../core/remote-ssh/control-master'
import { buildManagedScript } from '../../core/agents/hooks/managed-script'
-import { mergeManagedHook, type HookSettings } from '../../core/agents/hooks/install-helper'
+import { buildManagedHookCommand, mergeManagedHook, type HookSettings } from '../../core/agents/hooks/install-helper'
import {
buildCodexHooksAndTrust,
buildManagedCommand as buildCodexManagedCommand,
@@ -140,7 +140,7 @@ export class RemoteHooks {
} catch {
cfg = {}
}
- const merged = mergeManagedHook(cfg, `sh "${script}"`, t.events)
+ const merged = mergeManagedHook(cfg, buildManagedHookCommand(script), t.events)
await this.r.run(
childArgs(conn, controlPath, `mkdir -p $(dirname ${config}) && cat > ${config}`),
JSON.stringify(merged, null, 2)
@@ -279,7 +279,7 @@ export class RemoteHooks {
} catch {
cfg = {}
}
- const merged = mergeManagedHook(cfg, `sh "${script}"`, events)
+ const merged = mergeManagedHook(cfg, buildManagedHookCommand(script), events)
await this.r.run(
childArgs(conn, controlPath, `mkdir -p ${posixQuote(accountDir)} && cat > ${posixQuote(config)}`),
JSON.stringify(merged, null, 2)