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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(rpc): a trailing `undefined` argument survives the wire as `undefined`
enes1958dc11f880
diff --git a/src/shared/rpc.test.ts b/src/shared/rpc.test.tsindex 889861db4..12290ea4b 100644--- a/src/shared/rpc.test.ts+++ b/src/shared/rpc.test.ts@@ -20,6 +20,44 @@ describe('rpc protocol', () => {expect(parseRpcMessage('{"t":"req","id":"x","method":1}')).toBeNull()})+ // `JSON.stringify([a, undefined])` is `[a, null]` — the wire has no `undefined`. So a browser+ // call that omits a trailing optional argument (`git.history(cwd)`, `worktreeMerge(repo, b, base)`)+ // arrives at the handler as an EXPLICIT `null`, which does NOT trigger a default parameter:+ // `worktreeMerge(…, push = false)` runs with `push === null`. Today every such default happens to+ // be falsy, so `null` coerces to the same behavior and the bug is invisible — safe by luck, not by+ // construction. The first `= true` default (or any handler that branches on `arg === undefined`)+ // reintroduces it; `git.history` already broke exactly this way. The decoder is the one place+ // that can close it for every handler at once, so it does: no method in the preload API surface+ // takes a meaningful `null`, which is what makes the substitution safe.+ it('decodes a null argument back to undefined, so default parameters actually apply', () => {+ const req = parseRpcMessage('{"t":"req","id":1,"method":"git:worktree-merge","args":["/repo","feat","main",null]}')+ expect(req).toEqual({+ t: 'req', id: 1, method: 'git:worktree-merge', args: ['/repo', 'feat', 'main', undefined]+ })+ // Arity is preserved (the slot stays), and `undefined` is what makes `push = false` fire.+ const args = (req as { args: unknown[] }).args+ expect(args.length).toBe(4)+ const push = args[3] as boolean | undefined+ expect(((p = false) => p)(push)).toBe(false)++ // Interior nulls too: `[undefined, 2]` serialises to `[null, 2]` just the same.+ expect(parseRpcMessage('{"t":"cast","method":"m","args":[null,2]}')).toEqual({+ t: 'cast', method: 'm', args: [undefined, 2]+ })+ expect(parseRpcMessage('{"t":"ev","channel":"c","args":[null]}')).toEqual({+ t: 'ev', channel: 'c', args: [undefined]+ })+ })++ it('leaves a null NESTED inside an argument alone (only top-level slots are wire artifacts)', () => {+ // A null inside an object/array is real data the sender meant to send — `{cwd: null}` is not the+ // same as `{}` to a handler that checks `'cwd' in opts`. Only the top-level arg slots are the+ // ones JSON mangled, so only those are restored.+ expect(parseRpcMessage('{"t":"req","id":2,"method":"m","args":[{"cwd":null},[null]]}')).toEqual({+ t: 'req', id: 2, method: 'm', args: [{ cwd: null }, [null]]+ })+ })+it('round-trips pty data through the binary codec (incl. multibyte)', () => {const buf = encodePtyData('nt-abc', 'çıktı ✓[31m')expect(buf[0]).toBe(0x01)diff --git a/src/shared/rpc.ts b/src/shared/rpc.tsindex 7606f2fa2..a3cd1351a 100644--- a/src/shared/rpc.ts+++ b/src/shared/rpc.ts@@ -13,6 +13,30 @@ export type RpcMessage = RpcRequest | RpcCast | RpcOk | RpcErr | RpcEventexport const E_UNSUPPORTED = 'E_UNSUPPORTED'export const E_UNAUTHORIZED = 'E_UNAUTHORIZED'export const E_NO_HANDLER = 'E_NO_HANDLER'+/** The socket closed with requests still in flight — they can never be answered. Client-side only:+ * no server ever sends this, the client synthesises it so an `await` fails instead of hanging. */+export const E_DISCONNECTED = 'E_DISCONNECTED'++/**+ * Undo JSON's `undefined` → `null` mangling on the TOP-LEVEL argument slots.+ *+ * `JSON.stringify([a, undefined])` is `[a, null]`: the wire has no `undefined`. So a caller that+ * simply omits a trailing optional argument (`git.history(cwd)`, `worktreeMerge(repo, b, base)`)+ * hands the handler an explicit `null` — and an explicit `null` does NOT trigger a default+ * parameter. `worktreeMerge(…, push = false)` would run with `push === null`.+ *+ * Every such default in the API today is falsy (`push = false`, `pruneOnly = false`, `full = false`),+ * so `null` coerces to the same behavior and nothing breaks — safe BY LUCK. The first `= true`+ * default, or any handler that tests `arg === undefined`, silently reintroduces the bug (`git.history`+ * already broke this way once). Restoring `undefined` here fixes it for every handler at once,+ * instead of asking each new one to remember.+ *+ * Only the top-level slots are touched: a `null` nested inside an object or array is data the sender+ * genuinely meant to send (`{cwd: null}` is not `{}`), and no method in the preload API surface takes+ * a meaningful top-level `null` — which is what makes the substitution safe. Arity is preserved: the+ * slot stays, it just holds `undefined` again.+ */+const decodeArgs = (args: unknown[]): unknown[] => args.map((a) => (a === null ? undefined : a))export function parseRpcMessage(text: string): RpcMessage | null {let m: unknown@@ -26,10 +50,11 @@ export function parseRpcMessage(text: string): RpcMessage | null {switch (o.t) {case 'req':if (typeof o.id === 'number' && typeof o.method === 'string' && Array.isArray(o.args))- return o as RpcRequest+ return { ...(o as RpcRequest), args: decodeArgs(o.args) }return nullcase 'cast':- if (typeof o.method === 'string' && Array.isArray(o.args)) return o as RpcCast+ if (typeof o.method === 'string' && Array.isArray(o.args))+ return { ...(o as RpcCast), args: decodeArgs(o.args) }return nullcase 'res':if (typeof o.id !== 'number') return null@@ -37,7 +62,8 @@ export function parseRpcMessage(text: string): RpcMessage | null {if (o.ok === false && typeof o.error === 'object' && o.error !== null) return o as RpcErrreturn nullcase 'ev':- if (typeof o.channel === 'string' && Array.isArray(o.args)) return o as RpcEvent+ if (typeof o.channel === 'string' && Array.isArray(o.args))+ return { ...(o as RpcEvent), args: decodeArgs(o.args) }return nulldefault:return null