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(core): a fractional NODETERM_SESSION_* value must not disarm the reaper
enes29f5542eae6a
diff --git a/docs/session-memory.md b/docs/session-memory.mdindex bc86ab59c..b56e3e9e4 100644--- a/docs/session-memory.md+++ b/docs/session-memory.md@@ -59,6 +59,26 @@ for v1: a low ceiling OOM-kills a long-context session, which is worse than the**Do not re-derive these numbers.** They are here so the next person does not spend an afternoon on`/proc` arithmetic to reach the same conclusion.+### A fractional `NODETERM_SESSION_*` value must not disarm the guard++Found while running the live reaper experiment with `NODETERM_SESSION_GRACE_HOURS=1`.++`envInt` used `Math.floor(n)` behind `n > 0`, so a fractional setting became **zero** — and a zero+here is not a smaller setting, it is the removal of a safety:++| Input | Before | Effect |+|---|---|---|+| `GRACE_HOURS=0.5` | 0 s | a session was reapable the moment it detached |+| `MAX_DETACHED=0.5` | cap 0 | every detached session counted as over-cap: a full batch died every sweep |++The asymmetry is what made it dangerous rather than merely wrong: `abc`, `''`, `0` and `-3` all fell+back to the safe default, while `0.5` — the most PLAUSIBLE thing an operator would type, meaning+"half" — silently disarmed the guard. **A hand-editable value must degrade to the safe default,+never to something more destructive than the default.**++`envInt` now requires `>= 1`, and sub-hour grace (a legitimate wish) is served properly by+`envHours`, which keeps the fraction: `0.5` is 1800 seconds, not zero.+## 2. Why the reaper is deliberately untouched`src/core/session-budget.ts` reaps only **detached** sessions idle past a grace window. On thediff --git a/src/core/session-budget.test.ts b/src/core/session-budget.test.tsindex 329b98e39..9b2aab719 100644--- a/src/core/session-budget.test.ts+++ b/src/core/session-budget.test.ts@@ -374,3 +374,33 @@ describe('darwin default reader: no byte reading may ever reap (behavioural)', (expect(w.calls.filter((c) => c.args[2] === 'kill-session')).toHaveLength(0)})})++describe('sessionBudgetConfig with fractional env values', () => {+ const cfg = (env: Record<string, string>) => sessionBudgetConfig(env, 24576)++ it('a fractional MAX_DETACHED falls back — it must never become a cap of ZERO', () => {+ // Math.floor(0.5) === 0, and a cap of zero is not a smaller cap: every detached session counts+ // as over-cap, so a full batch dies every sweep. The unsafe direction.+ expect(cfg({ NODETERM_SESSION_MAX_DETACHED: '0.5' }).maxDetached).toBe(48)+ expect(cfg({ NODETERM_SESSION_MAX_DETACHED: '0.9' }).maxDetached).toBe(48)+ // A real value still works, and 1.5 still floors to 1 rather than falling back.+ expect(cfg({ NODETERM_SESSION_MAX_DETACHED: '10' }).maxDetached).toBe(10)+ expect(cfg({ NODETERM_SESSION_MAX_DETACHED: '1.5' }).maxDetached).toBe(1)+ })++ it('a fractional GRACE_HOURS means what it says — half an hour, not NO grace', () => {+ // The plausible-input trap: `abc`/``/`0` all fell back safely, but `0.5` floored to zero grace,+ // making a session reapable the moment it detached.+ expect(cfg({ NODETERM_SESSION_GRACE_HOURS: '0.5' }).graceSec).toBe(1800)+ expect(cfg({ NODETERM_SESSION_GRACE_HOURS: '0.25' }).graceSec).toBe(900)+ expect(cfg({ NODETERM_SESSION_GRACE_HOURS: '2' }).graceSec).toBe(7200)+ })++ it('junk and zero still fall back to the safe defaults on every key', () => {+ for (const v of ['abc', '', '0', '-3']) {+ expect(cfg({ NODETERM_SESSION_GRACE_HOURS: v }).graceSec).toBe(6 * 3600)+ expect(cfg({ NODETERM_SESSION_MAX_DETACHED: v }).maxDetached).toBe(48)+ expect(cfg({ NODETERM_SESSION_REAP_BATCH: v }).batchMax).toBe(8)+ }+ })+})diff --git a/src/core/session-budget.ts b/src/core/session-budget.tsindex c762dc694..b0e41fd59 100644--- a/src/core/session-budget.ts+++ b/src/core/session-budget.ts@@ -67,9 +67,32 @@ export interface SessionBudgetConfig {batchMax: number}+/**+ * A positive INTEGER from the environment, or the fallback.+ *+ * The floor is `>= 1`, not `> 0`, and that is the whole point: `Math.floor(0.5)` is 0, and a zero+ * here is not a smaller setting — it is the REMOVAL of a safety. `MAX_DETACHED=0.5` became a cap of+ * zero, so every detached session counted as over-cap and a full batch died every sweep;+ * `GRACE_HOURS=0.5` became zero grace, so a session was reapable the moment it detached.+ *+ * The asymmetry is what makes it dangerous rather than merely wrong: `abc`, `''` and `0` all fall+ * back to the safe default, while `0.5` — the most PLAUSIBLE thing an operator would type, meaning+ * "half" — silently disarmed the guard. A hand-editable value must degrade to the safe default,+ * never to something more destructive than the default.+ *+ * Sub-hour grace is a legitimate wish, so it is served properly by `envHours` rather than being+ * floored into nothing.+ */function envInt(env: NodeJS.ProcessEnv, key: string, fallback: number): number {const n = Number(env[key])- return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback+ return Number.isFinite(n) && n >= 1 ? Math.floor(n) : fallback+}++/** Hours as a possibly-FRACTIONAL positive number, returned in seconds. `0.5` means 30 minutes —+ * the reading the operator intended — instead of `Math.floor`-ing to no grace at all. */+function envHours(env: NodeJS.ProcessEnv, key: string, fallbackHours: number): number {+ const n = Number(env[key])+ return Math.round((Number.isFinite(n) && n > 0 ? n : fallbackHours) * 3600)}/**@@ -85,7 +108,7 @@ export function sessionBudgetConfig(env: NodeJS.ProcessEnv, totalMb: number): Sedisabled: env.NODETERM_SESSION_REAP_DISABLED === '1' || env.NODETERM_SESSION_REAP_DISABLED === 'true',minAvailableMb: envInt(env, 'NODETERM_SESSION_MIN_AVAILABLE_MB', Math.max(1024, Math.round(totalMb * 0.1))),maxDetached: envInt(env, 'NODETERM_SESSION_MAX_DETACHED', 48),- graceSec: envInt(env, 'NODETERM_SESSION_GRACE_HOURS', 6) * 3600,+ graceSec: envHours(env, 'NODETERM_SESSION_GRACE_HOURS', 6),batchMax: envInt(env, 'NODETERM_SESSION_REAP_BATCH', 8)}}