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(core): check marker ORDER in the remote session-memory parse

enesadb26eedb990
diff --git a/src/core/session-memory-remote.test.ts b/src/core/session-memory-remote.test.ts
index ddf959185..a01b599b9 100644
--- a/src/core/session-memory-remote.test.ts
+++ b/src/core/session-memory-remote.test.ts
@@ -37,6 +37,36 @@ describe('parseRemoteSessionMemory', () => {
expect(r.rows).toEqual([])
})
+ // An ssh exec channel is not a clean pipe — a login shell's rc file can write to stdout ahead of
+ // our first marker. Out-of-order markers must fail closed, not produce a confident empty report.
+ // The stream below is a COMPLETE, healthy sweep with one stray `##PROCS` echoed ahead of it by a
+ // login shell's rc file. Every part must stay realistic: the real PROCS tail still parses, so the
+ // empty-table check does NOT catch this, and without the ordering guard the panes slice comes out
+ // empty and the report is a confident `{ok:true, rows:[]}` over a host with live sessions.
+ it('reports ok:false when the markers arrive out of order', () => {
+ const r = parseRemoteSessionMemory(
+ '##PROCS\n' +
+ '##MEM\nMemAvailable: 1024 kB\nMemTotal: 2048 kB\n' +
+ '##PANES\nnt-term-a|100|claude\n' +
+ '##PROCS\n100 1 1024\n200 100 358400\n'
+ )
+ expect(r.ok).toBe(false)
+ expect(r.rows).toEqual([])
+ })
+
+ // A marker string inside DATA is not a marker: only a whole line counts, and only the first one.
+ it('is not confused by a marker appearing inside pane or process data', () => {
+ const r = parseRemoteSessionMemory(
+ '##MEM\nMemAvailable: 1024 kB\nMemTotal: 2048 kB\n' +
+ '##PANES\nnt-term-b|300|##PROCS\n' +
+ '##PROCS\n300 1 1024\n##PANES\n400 1 2048\n'
+ )
+ expect(r.ok).toBe(true)
+ expect(r.mem).toEqual({ availableMb: 1, totalMb: 2 })
+ expect(r.rows).toHaveLength(1)
+ expect(r.rows[0]).toMatchObject({ nodeId: 'term-b', command: '##PROCS', selfMb: 1, totalMb: 1 })
+ })
+
// Both sockets are swept in ONE stream, and `list-panes -a` prints a line per PANE, so the same
// session can appear several times. It is still one session, hence one row (the local leg's
// `bySession` map makes the same promise).
@@ -67,13 +97,17 @@ describe('fetchRemoteSessionMemory', () => {
describe('remoteSessionMemoryCommand under /bin/sh', () => {
let dir: string
+ // A fake tmux that answers list-panes and nothing else. It reports `$PPID` — the sh running our
+ // generated command — NOT its own `$$`: the fake exits immediately, so a `$$` pid is already dead
+ // by the time `ps` runs and every row would roll up to 0, making a size assertion vacuous.
+ // `$PPID` is alive for the whole sweep, so the row proves the rollup really joined the pane pid
+ // to the host's process table.
+ const FAKE_TMUX =
+ '#!/bin/sh\nfor a in "$@"; do [ "$a" = "list-panes" ] && { echo "nt-term-a|$PPID|claude"; exit 0; }; done\nexit 1\n'
+
beforeAll(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sessmem-'))
- // A fake tmux on PATH that answers list-panes and nothing else.
- fs.writeFileSync(
- path.join(dir, 'tmux'),
- '#!/bin/sh\nfor a in "$@"; do [ "$a" = "list-panes" ] && { echo "nt-term-a|$$|claude"; exit 0; }; done\nexit 1\n'
- )
+ fs.writeFileSync(path.join(dir, 'tmux'), FAKE_TMUX)
fs.chmodSync(path.join(dir, 'tmux'), 0o755)
})
@@ -86,8 +120,29 @@ describe('remoteSessionMemoryCommand under /bin/sh', () => {
const r = parseRemoteSessionMemory(stdout)
expect(r.ok).toBe(true)
expect(r.rows.map((x) => x.nodeId)).toEqual(['term-a'])
- // The row resolved against the REAL process table, so it has a real size.
- expect(r.rows[0].totalMb).toBeGreaterThanOrEqual(0)
+ // The pane pid was resolved against the REAL process table: a live shell plus its children.
+ // A 0 here would mean the rollup never found the pid, which is the failure worth catching.
+ expect(r.rows[0].totalMb).toBeGreaterThan(0)
+ expect(r.mem).not.toBeNull()
+ })
+
+ // A host with no /proc/meminfo (the macOS/BSD shape). Deliberately NO free/vm_stat/sysctl
+ // fallback: the sweep still answers with rows, and `mem` is null = "no signal", never a zero.
+ it('still reports rows with mem:null when /proc/meminfo is unreadable', async () => {
+ const noproc = fs.mkdtempSync(path.join(os.tmpdir(), 'sessmem-nomem-'))
+ fs.writeFileSync(path.join(noproc, 'tmux'), FAKE_TMUX)
+ fs.chmodSync(path.join(noproc, 'tmux'), 0o755)
+ // Stub `cat` so reading /proc/meminfo fails the way it does off Linux.
+ fs.writeFileSync(path.join(noproc, 'cat'), '#!/bin/sh\nexit 1\n')
+ fs.chmodSync(path.join(noproc, 'cat'), 0o755)
+ const { stdout } = await run('/bin/sh', ['-c', remoteSessionMemoryCommand()], {
+ env: { ...process.env, PATH: `${noproc}:${process.env.PATH ?? ''}` }
+ })
+ const r = parseRemoteSessionMemory(stdout)
+ expect(r.ok).toBe(true)
+ expect(r.mem).toBeNull()
+ expect(r.rows.map((x) => x.nodeId)).toEqual(['term-a'])
+ fs.rmSync(noproc, { recursive: true, force: true })
})
it('exits 0 and reports no rows when no tmux server is running', async () => {
diff --git a/src/core/session-memory-remote.ts b/src/core/session-memory-remote.ts
index 94d5513d8..69cdf69f8 100644
--- a/src/core/session-memory-remote.ts
+++ b/src/core/session-memory-remote.ts
@@ -40,7 +40,7 @@ export function remoteSessionMemoryCommand(): string {
`tmux -L ${socket} list-panes -a -F '${PANE_FMT}' 2>/dev/null`
return [
`echo '${MEM}'`,
- `cat /proc/meminfo 2>/dev/null | grep -E '^(MemAvailable|MemTotal):' || true`,
+ `cat /proc/meminfo 2>/dev/null | grep -E '^(MemAvailable|MemTotal):' 2>/dev/null || true`,
`echo '${PANES}'`,
`{ ${listPanes(TMUX_SOCKET)}; ${listPanes(RMT_TMUX_SOCKET)}; } || true`,
`echo '${PROCS}'`,
@@ -68,8 +68,18 @@ export function parseRemoteSessionMemory(stdout: string): SessionMemoryReport {
const iMem = lines.indexOf(MEM)
const iPanes = lines.indexOf(PANES)
const iProcs = lines.indexOf(PROCS)
- // A missing header means the stream was cut short — not evidence that the host has nothing.
+ // Two distinct failures, both of which must read as "we could not look", never as "nothing here":
+ // - a MISSING header (a `-1`) means the stream was cut short — a master that died mid-read;
+ // - headers OUT OF ORDER mean the stream is not ours to trust. An ssh exec channel is not a
+ // clean pipe: a login shell's rc file writing to stdout is a documented hazard in this repo
+ // (it is why the remote `claude --version` probe goes through a login shell deliberately), so
+ // a `.profile` that echoes `##PROCS` is enough to put a marker ahead of `##MEM`. Unchecked,
+ // the panes slice would come out empty while `ps` still parsed from the tail, and the report
+ // would be a confident `{ok:true, rows:[]}` — "this host has nothing" over live sessions.
+ // The ordering test subsumes the `-1` check (a -1 can never sit below a found index), but both
+ // are spelled out because they are different diagnoses of a broken read.
if (iMem < 0 || iPanes < 0 || iProcs < 0) return { ok: false, rows: [], mem: null }
+ if (!(iMem < iPanes && iPanes < iProcs)) return { ok: false, rows: [], mem: null }
const mem = parseMem(lines.slice(iMem + 1, iPanes))
const panes = parsePaneList(lines.slice(iPanes + 1, iProcs).join('\n'))