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.

refactor(shared): isSafeNodeId lives in shared so the renderer can use the SAME predicate

Claude Code53f9a76f314b
diff --git a/src/core/remote-safety.test.ts b/src/core/remote-safety.test.ts
index c1c3f97fc..9b56f82b7 100644
--- a/src/core/remote-safety.test.ts
+++ b/src/core/remote-safety.test.ts
@@ -1,7 +1,17 @@
import { describe, expect, it } from 'vitest'
import { NODE_ID_MAX, isSafeNodeId, isSafeRemoteHome } from './remote-safety'
+import { NODE_ID_MAX as SHARED_NODE_ID_MAX, isSafeNodeId as sharedIsSafeNodeId } from '@shared/safe-id'
describe('isSafeNodeId', () => {
+ it('core re-exports the SAME function object — one predicate, not two that drift', () => {
+ // The definition moved to @shared/safe-id so the renderer can validate a project id before it
+ // becomes a session-partition storage key without importing src/core. This pin is what makes
+ // "moved, not copied" a property rather than a claim: if anyone re-declares the predicate here,
+ // the two function objects diverge and this fails.
+ expect(isSafeNodeId).toBe(sharedIsSafeNodeId)
+ expect(NODE_ID_MAX).toBe(SHARED_NODE_ID_MAX)
+ })
+
it('accepts every shape the app actually mints', () => {
// `nextId()` in state/workspace.ts: `<prefix>-<base36 time>-<counter>`.
for (const id of ['term-mabc123-7', 'ssh-mabc123-1', 'node-1', 'a', 'A_b.c-1', '9'])
diff --git a/src/core/remote-safety.ts b/src/core/remote-safety.ts
index 2d49efeb0..594c1ac86 100644
--- a/src/core/remote-safety.ts
+++ b/src/core/remote-safety.ts
@@ -10,33 +10,16 @@
* No node/electron imports: usable from core, main, and the server shell alike.
*/
-/** Generous cap on a node id — longer than anything the app mints, short enough to bound. */
-export const NODE_ID_MAX = 128
+// `isSafeNodeId` / `NODE_ID_MAX` MOVED to `@shared/safe-id` (re-exported here so no existing
+// import path changes): the renderer needs this exact predicate to validate a project id before it
+// becomes a session-partition storage key, and the renderer may not import `src/core`. One
+// predicate, two consumers — `remote-safety.test.ts` pins that this name and the shared one are
+// the same function object, so the two cannot drift.
+export { NODE_ID_MAX, isSafeNodeId } from '../shared/safe-id'
+
/** Generous cap on a host-reported `$HOME`; longer than any real path (PATH_MAX on Linux). */
export const REMOTE_HOME_MAX = 4096
-/**
- * Is this a node id (a `persistKey`) safe to carry into a remote command line and a filesystem
- * path?
- *
- * Charset is deliberately narrower than "no metacharacters": `[A-Za-z0-9._-]` covers every id the
- * app actually produces — `nextId()` emits `<prefix>-<base36 time>-<counter>` and `uuid()` emits
- * hex-with-dashes — so the answer to "could this refuse something legitimate?" is a list, not a
- * guess. `.` and `..` are refused on top of the charset: they pass it but name a directory, and
- * these ids are also used as path components (session files, per-node remote state). It is also
- * the charset the codex identity shim ALREADY enforces on `$NODETERM_NODE_ID` before it will use
- * it (`codex-identity-proxy.ts`: `*[!A-Za-z0-9._-]*) nt_fail node-id-unavailable`), so this is the
- * existing rule moved to the boundary rather than a new one invented here.
- *
- * An EMPTY id is refused here and must be handled by the caller as "not persistent" rather than as
- * a violation — `PtyManager.create` already branches on a falsy `persistKey` before it asks.
- */
-export function isSafeNodeId(id: string | undefined | null): boolean {
- if (!id || id.length > NODE_ID_MAX) return false
- if (id === '.' || id === '..') return false
- return /^[A-Za-z0-9._-]+$/.test(id)
-}
-
/**
* Every character that is live shell syntax at a `$HOME` interpolation site, plus every control
* character. It is a DENYLIST on purpose — see `isSafeRemoteHome`.
diff --git a/src/shared/safe-id.test.ts b/src/shared/safe-id.test.ts
new file mode 100644
index 000000000..336351500
--- /dev/null
+++ b/src/shared/safe-id.test.ts
@@ -0,0 +1,30 @@
+import { describe, it, expect } from 'vitest'
+import { NODE_ID_MAX, isSafeNodeId } from './safe-id'
+
+describe('isSafeNodeId (shared home)', () => {
+ it('refuses dot segments, separators, spaces, empties and over-length ids', () => {
+ // The exact corpus node-auth-token.test.ts pins through the re-export, kept here at the
+ // definition site so the predicate cannot be weakened without a red test in BOTH places.
+ for (const bad of ['..', '.', '../x', 'a b', 'a/b', '', 'x'.repeat(300)]) {
+ expect(isSafeNodeId(bad), bad).toBe(false)
+ }
+ })
+
+ it('accepts every shape the app actually mints', () => {
+ for (const good of ['a', 'browser-3', 'p_1.2', 'node-1', 'term_abc.9', 'A.B-C_d']) {
+ expect(isSafeNodeId(good), good).toBe(true)
+ }
+ })
+
+ it('refuses undefined/null and enforces the exact NODE_ID_MAX boundary', () => {
+ expect(isSafeNodeId(undefined)).toBe(false)
+ expect(isSafeNodeId(null)).toBe(false)
+ expect(isSafeNodeId('x'.repeat(NODE_ID_MAX))).toBe(true)
+ expect(isSafeNodeId('x'.repeat(NODE_ID_MAX + 1))).toBe(false)
+ })
+
+ // The "core re-exports the SAME function object" identity assertion lives in
+ // core/remote-safety.test.ts: this file is part of the WEB typecheck program
+ // (tsconfig.web.json), which deliberately cannot see src/core — the very layering rule that
+ // forced the move. The pin exists; it just sits on the side of the boundary that may import both.
+})
diff --git a/src/shared/safe-id.ts b/src/shared/safe-id.ts
new file mode 100644
index 000000000..90161cab7
--- /dev/null
+++ b/src/shared/safe-id.ts
@@ -0,0 +1,35 @@
+/**
+ * THE node-id predicate, in `src/shared` because both sides of the process boundary need the SAME
+ * one: core/main validate an id before it crosses into a remote shell or a filesystem path, and the
+ * renderer must validate a project id before it becomes a session-partition storage key
+ * (`persist:nt-agent-browser-<projectId>`) — and the renderer may not import `src/core`.
+ *
+ * Moved here from `core/remote-safety.ts`, which re-exports it so every existing import path is
+ * untouched; `safe-id.test.ts` pins that the two names are one function object. No imports at all:
+ * usable from core, main, renderer and the server shell alike.
+ */
+
+/** Generous cap on a node id — longer than anything the app mints, short enough to bound. */
+export const NODE_ID_MAX = 128
+
+/**
+ * Is this a node id (a `persistKey`) safe to carry into a remote command line and a filesystem
+ * path?
+ *
+ * Charset is deliberately narrower than "no metacharacters": `[A-Za-z0-9._-]` covers every id the
+ * app actually produces — `nextId()` emits `<prefix>-<base36 time>-<counter>` and `uuid()` emits
+ * hex-with-dashes — so the answer to "could this refuse something legitimate?" is a list, not a
+ * guess. `.` and `..` are refused on top of the charset: they pass it but name a directory, and
+ * these ids are also used as path components (session files, per-node remote state). It is also
+ * the charset the codex identity shim ALREADY enforces on `$NODETERM_NODE_ID` before it will use
+ * it (`codex-identity-proxy.ts`: `*[!A-Za-z0-9._-]*) nt_fail node-id-unavailable`), so this is the
+ * existing rule moved to the boundary rather than a new one invented here.
+ *
+ * An EMPTY id is refused here and must be handled by the caller as "not persistent" rather than as
+ * a violation — `PtyManager.create` already branches on a falsy `persistKey` before it asks.
+ */
+export function isSafeNodeId(id: string | undefined | null): boolean {
+ if (!id || id.length > NODE_ID_MAX) return false
+ if (id === '.' || id === '..') return false
+ return /^[A-Za-z0-9._-]+$/.test(id)
+}