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(hooks): endpoint file v2 advertises NODETERM_NODE_TOKEN_DIR

Claude Codebfb96481a588
diff --git a/src/core/agents/hook-endpoint-file.test.ts b/src/core/agents/hook-endpoint-file.test.ts
new file mode 100644
index 000000000..cf027449f
--- /dev/null
+++ b/src/core/agents/hook-endpoint-file.test.ts
@@ -0,0 +1,32 @@
+import { describe, it, expect, beforeAll, afterAll } from 'vitest'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+import { hookServer } from './hook-server'
+import { nodeTokenDir } from './node-token-files'
+import { initPlatform, resetPlatformForTests } from '../platform'
+import { fakePlatform } from '../platform-fake'
+
+let dir = ''
+beforeAll(async () => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodeterm-ep-'))
+ resetPlatformForTests()
+ initPlatform(fakePlatform({ userDataDir: dir }))
+ await hookServer.start()
+})
+afterAll(() => {
+ hookServer.stop()
+ fs.rmSync(dir, { recursive: true, force: true })
+})
+
+describe('endpoint file v2', () => {
+ it('advertises the token dir and version 2, at 0600', () => {
+ const p = hookServer.endpointFilePath()
+ const body = fs.readFileSync(p, 'utf8')
+ expect(fs.statSync(p).mode & 0o777).toBe(0o600)
+ expect(body).toContain(`NODETERM_NODE_TOKEN_DIR=${nodeTokenDir()}\n`)
+ expect(body).toContain('NODETERM_HOOK_VERSION=2\n')
+ // The token dir is under this run's userDataDir, not a compiled-in path.
+ expect(nodeTokenDir().startsWith(dir)).toBe(true)
+ })
+})
diff --git a/src/core/agents/hook-server.ts b/src/core/agents/hook-server.ts
index 200f013d3..ad12869ed 100644
--- a/src/core/agents/hook-server.ts
+++ b/src/core/agents/hook-server.ts
@@ -6,8 +6,12 @@ import { platform } from '../platform'
import { canControlCanvas, hasSharedIdentity, type AgentId } from '../../shared/agents/config'
import { normalizeFor, type NormalizedAgentEvent } from '../../shared/agents/normalize'
import type { CodexIdentityEvent } from '../../shared/types'
+import { nodeTokenDir } from './node-token-files'
-export const NODETERM_HOOK_PROTOCOL_VERSION = '1'
+// v2 advertises NODETERM_NODE_TOKEN_DIR so clients read their per-node capability from a file
+// rather than receiving it in argv. Nothing consumes the posted version server-side, so the bump
+// is free; it is a marker a client can key on.
+export const NODETERM_HOOK_PROTOCOL_VERSION = '2'
const SLOWLORIS_MS = 2000
// Once the body is fully read the slowloris guard has done its job — it exists for the RECEIVE
@@ -506,7 +510,14 @@ class HookServer {
mkdirSync(path.dirname(p), { recursive: true })
writeFileSync(
p,
- `NODETERM_HOOK_PORT=${this.port}\nNODETERM_HOOK_TOKEN=${this.token}\nNODETERM_HOOK_VERSION=${NODETERM_HOOK_PROTOCOL_VERSION}\n`,
+ `NODETERM_HOOK_PORT=${this.port}\n` +
+ `NODETERM_HOOK_TOKEN=${this.token}\n` +
+ `NODETERM_HOOK_VERSION=${NODETERM_HOOK_PROTOCOL_VERSION}\n` +
+ // Where clients read their PER-NODE capability from, keyed by $NODETERM_NODE_ID.
+ // Advertised (not compiled in) so a failover that sources ANOTHER instance's endpoint
+ // file also picks up THAT instance's token dir: it then finds a token that instance can
+ // verify, or none — never a mismatched one.
+ `NODETERM_NODE_TOKEN_DIR=${nodeTokenDir()}\n`,
// 0o600: this file holds the bearer token — owner read/write only so another local user
// can't read it and forge hook events.
{ encoding: 'utf8', mode: 0o600 }
diff --git a/src/core/remote-ssh/control-master.test.ts b/src/core/remote-ssh/control-master.test.ts
index 1e8d8c74b..d20718b7e 100644
--- a/src/core/remote-ssh/control-master.test.ts
+++ b/src/core/remote-ssh/control-master.test.ts
@@ -294,9 +294,10 @@ describe('hook forwarding', () => {
])
expect(remoteHookEnvArgs('/ep', 'n1', '1')).not.toContain('NODETERM_CANVAS_CONTROL=1')
})
- it('remoteEndpointFileContents writes SOCK/TOKEN/VERSION', () => {
- expect(remoteEndpointFileContents('/r.sock', 'tok', '1')).toBe(
- 'NODETERM_HOOK_SOCK=/r.sock\nNODETERM_HOOK_TOKEN=tok\nNODETERM_HOOK_VERSION=1\n'
+ it('remoteEndpointFileContents writes SOCK/TOKEN/VERSION and the remote token dir', () => {
+ expect(remoteEndpointFileContents('/r.sock', 'tok', '2', '/home/u/.nodeterm/node-tokens')).toBe(
+ 'NODETERM_HOOK_SOCK=/r.sock\nNODETERM_HOOK_TOKEN=tok\nNODETERM_HOOK_VERSION=2\n' +
+ 'NODETERM_NODE_TOKEN_DIR=/home/u/.nodeterm/node-tokens\n'
)
})
})
diff --git a/src/core/remote-ssh/control-master.ts b/src/core/remote-ssh/control-master.ts
index 5ddd7008b..28f4c7f38 100644
--- a/src/core/remote-ssh/control-master.ts
+++ b/src/core/remote-ssh/control-master.ts
@@ -477,8 +477,20 @@ export function remoteHookEnvArgs(
return env
}
/** Contents of the remote endpoint env file the managed hook script sources (unix-socket transport). */
-export function remoteEndpointFileContents(sock: string, token: string, version: string): string {
- return `NODETERM_HOOK_SOCK=${sock}\nNODETERM_HOOK_TOKEN=${token}\nNODETERM_HOOK_VERSION=${version}\n`
+export function remoteEndpointFileContents(
+ sock: string,
+ token: string,
+ version: string,
+ tokenDir: string
+): string {
+ return (
+ `NODETERM_HOOK_SOCK=${sock}\n` +
+ `NODETERM_HOOK_TOKEN=${token}\n` +
+ `NODETERM_HOOK_VERSION=${version}\n` +
+ // The REMOTE token dir ($HOME/.nodeterm/node-tokens on the host), not ours. The host stores
+ // per-node tokens only; it never holds a secret and can never mint.
+ `NODETERM_NODE_TOKEN_DIR=${tokenDir}\n`
+ )
}
/**
diff --git a/src/main/remote-ssh/remote-hooks.ts b/src/main/remote-ssh/remote-hooks.ts
index 85ee29943..e3984643c 100644
--- a/src/main/remote-ssh/remote-hooks.ts
+++ b/src/main/remote-ssh/remote-hooks.ts
@@ -133,7 +133,7 @@ export class RemoteHooks {
// so sessions are never pointed at a socket that answers nothing.
await this.r.run(
childArgs(conn, controlPath, `umask 077; cat > ${endpoint}`),
- remoteEndpointFileContents(sock, hook.token, hook.version)
+ remoteEndpointFileContents(sock, hook.token, hook.version, `${remoteDir}/node-tokens`)
)
// 3. install the managed hook for each JSON agent (script + merged config).
for (const t of AGENT_TARGETS) {