mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-12 07:59:02 +02:00
Three-bug chain behind the Windows terminal-agent leak (console window strobing every 60s, one orphaned agent per watchdog tick until the box ran out of committable memory): 1. isProcessAlive shelled out to `tasklist /FI "PID eq <pid>"` on Windows with a 3s timeout. A Bun.spawnSync that hits its timeout still RETURNS with partial stdout, so the `.includes()` PID match read a LIVE agent as dead — killAgentByRecord skipped the kill, the watchdog respawned around the survivor, and every orphan slowed the next tasklist enough to produce the next false negative. Now: `process.kill(pid, 0)` on every platform (Node and Bun both map signal 0 to an OpenProcess existence check on Windows), with EPERM counted as alive. No subprocess, no timeout, no console window. 2. The respawn circuit-breaker was mathematically unreachable — verified in this tree: RESPAWN_GUARD_WINDOW_MS was a fixed 60_000 against a 60_000ms default tick, and each tick pushes at most one respawn timestamp, so three pushes span ~120s and can never coexist inside a 60s window (eviction is strict `>`, and setInterval drift plus per-tick work always ages the prior entry past the boundary). The guard could not fire at the default tick rate and a steady one-per-tick leak ran unbounded. The window now scales with the tick: max(60_000, tick * (RESPAWN_GUARD_MAX + 2)), so "3 crashes in quick succession → stop" holds at any tick value. 3. The tasklist probe popped a visible console per tick (no windowsHide). Removing the shell-out kills that site; the agent-spawn site itself already passes windowsHide: true (landed with the bun-polyfill windowsHide commit — PR #2414's terminal-agent-control.ts hunk is reconciled there rather than duplicated). New browse/test/process-liveness-windows.test.ts pins all three: no subprocess from the probe, a static tripwire against reintroducing `tasklist` + `PID eq` liveness checks in src/, the spawnTerminalAgent windowsHide + stdio contract, and the window-derived-from-tick arithmetic. terminal-agent-watchdog.test.ts test 4 now pins the window/tick relationship instead of the fixed literal that let this ship. Also converts `new URL(import.meta.url).pathname` to `import.meta.path` across the static-grep tests it touches — the pathname form yields /C:/... on Windows and breaks path.resolve. Contributed by @SYKhayyat (PR #2414). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
52 lines
2.4 KiB
TypeScript
52 lines
2.4 KiB
TypeScript
import { describe, test, expect } from 'bun:test';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
// Static-grep tripwire for the v1.44 internalHandler refactor.
|
|
//
|
|
// /internal/grant and /internal/revoke were copies of the same dance:
|
|
// bearer-auth → x-browse-gen check → req.json().then(...).catch(...).
|
|
// internalHandler<T>(req, fn) collapses that into a single helper call.
|
|
// This test fails CI if the helper goes away or the existing routes
|
|
// regress to inline auth + JSON parse boilerplate. Wiring tests
|
|
// (token grant/revoke behavior) already live in
|
|
// browse/test/terminal-agent-integration.test.ts.
|
|
|
|
const AGENT_TS = path.resolve(import.meta.path, '..', '..', 'src', 'terminal-agent.ts');
|
|
|
|
describe('terminal-agent internalHandler refactor (v1.44+)', () => {
|
|
test('1. internalHandler<T> exists with the documented signature', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
expect(src).toMatch(/async function internalHandler<T>\s*\(/);
|
|
// Body must include the auth gate, body parse, and result coercion.
|
|
expect(src).toContain('checkInternalAuth(req)');
|
|
expect(src).toContain('await req.json()');
|
|
expect(src).toContain('instanceof Response');
|
|
});
|
|
|
|
test('2. /internal/grant routes through internalHandler', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
// Match the route handler block.
|
|
const block = sliceBetween(src, "url.pathname === '/internal/grant'", "url.pathname === '/internal/revoke'");
|
|
expect(block).toContain('internalHandler(req');
|
|
// Must NOT have the old inline pattern (would be a regression).
|
|
expect(block).not.toContain('req.headers.get(\'authorization\')');
|
|
expect(block).not.toContain('req.json().then(');
|
|
});
|
|
|
|
test('3. /internal/revoke routes through internalHandler', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
const block = sliceBetween(src, "url.pathname === '/internal/revoke'", "url.pathname === '/internal/healthz'");
|
|
expect(block).toContain('internalHandler(req');
|
|
expect(block).not.toContain('req.json().then(');
|
|
});
|
|
});
|
|
|
|
function sliceBetween(source: string, start: string, end: string): string {
|
|
const i = source.indexOf(start);
|
|
if (i === -1) throw new Error(`marker not found: ${start}`);
|
|
const j = source.indexOf(end, i + start.length);
|
|
if (j === -1) throw new Error(`end marker not found: ${end}`);
|
|
return source.slice(i, j);
|
|
}
|