fix(browse): absorb #2414 residuals — EPERM-alive liveness + Windows-dead test tripwires (re-derived)

Re-derive of PR #2414 (SYKhayyat) onto current main. Most of the PR already
landed in earlier waves: the tick-derived RESPAWN_GUARD_WINDOW_MS, the
spawnTerminalAgent windowsHide flag, the process-liveness regression tests,
and the browse/test import.meta.path sweep are all on main. Two pieces
remained:

1. isProcessAlive EPERM semantics (error-handling.ts): on the signal-0 path,
   EPERM means the process EXISTS but we lack rights to signal it — that is
   ALIVE. Returning false made callers that validate liveness before killing
   (killAgentByRecord, the terminal-agent watchdog) skip the kill and respawn
   around a survivor — the self-reinforcing one-leak-per-tick chain from
   #2414/#2295. Matters for cross-user PID checks.

2. Six test/ files ADDED SINCE the PR reintroduced the exact Windows bug its
   second commit fixed: `new URL(import.meta.url).pathname` yields
   `/C:/Users/...` on Windows, so path.resolve prepends the cwd drive and
   every tripwire ENOENTs instead of asserting anything (egress-receipt,
   egress-lib, egress-receipt-wiring, gstack-egress-cli,
   pty-skill-seeding-wiring, skill-census). All six now use
   import.meta.path — Bun's absolute native path, identical arity.

The remaining #2414 piece — replacing the Windows tasklist probe with
signal-0 — lands as its own commit (#1952) on top of this shape.

Tests: the 6 touched test files 47 pass; process-liveness-windows +
error-handling 13 pass.

Re-derived from PR #2414 by @SYKhayyat. Fixes the residual of #2295.

Co-authored-by: SYKhayyat <shaulyoelkhayyat@gmail.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-08-16 10:59:05 -07:00
co-authored by SYKhayyat Claude Fable 5
parent 25ccda996d
commit b3a27173fe
7 changed files with 17 additions and 9 deletions
+11 -3
View File
@@ -36,7 +36,15 @@ export function safeKill(pid: number, signal: NodeJS.Signals | number): void {
}
}
/** Check if a PID is alive. Pure boolean probe — returns false for ALL errors. */
/**
* Check if a PID is alive. Pure boolean probe — never throws.
*
* EPERM means the process EXISTS but we lack rights to signal it. That is
* alive — returning false there makes callers that validate liveness before
* killing (killAgentByRecord, the terminal-agent watchdog) skip the kill and
* respawn around a survivor, leaking one process per watchdog tick (#2414,
* #2295).
*/
export function isProcessAlive(pid: number): boolean {
if (IS_WINDOWS) {
try {
@@ -52,7 +60,7 @@ export function isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
} catch (err: any) {
return err?.code === 'EPERM';
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ import * as os from 'os';
import * as path from 'path';
import { listReceipts, sha256Hex } from '../lib/egress-receipt';
const ROOT = path.resolve(new URL(import.meta.url).pathname, '..', '..');
const ROOT = path.resolve(import.meta.path, '..', '..');
const LIB = path.join(ROOT, 'bin', 'gstack-egress-lib.sh');
const received: string[] = [];
+1 -1
View File
@@ -28,7 +28,7 @@ import { describe, test, expect } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
const ROOT = path.resolve(new URL(import.meta.url).pathname, '..', '..');
const ROOT = path.resolve(import.meta.path, '..', '..');
function read(rel: string): string {
return fs.readFileSync(path.join(ROOT, rel), 'utf-8');
+1 -1
View File
@@ -31,7 +31,7 @@ import {
writeReceipt,
} from '../lib/egress-receipt';
const ROOT = path.resolve(new URL(import.meta.url).pathname, '..', '..');
const ROOT = path.resolve(import.meta.path, '..', '..');
let home: string;
+1 -1
View File
@@ -19,7 +19,7 @@ import {
writeReceipt,
} from '../lib/egress-receipt';
const ROOT = path.resolve(new URL(import.meta.url).pathname, '..', '..');
const ROOT = path.resolve(import.meta.path, '..', '..');
const BIN = path.join(ROOT, 'bin', 'gstack-egress');
let home: string;
+1 -1
View File
@@ -17,7 +17,7 @@ import { describe, test, expect } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
const ROOT = path.resolve(new URL(import.meta.url).pathname, '..', '..');
const ROOT = path.resolve(import.meta.path, '..', '..');
/** A PTY send whose payload starts with a slash command (`/name`, optionally
* followed by `\r`, whitespace, or the closing quote). A second slash right
+1 -1
View File
@@ -11,7 +11,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { frontmatterName, skillCensus } from './helpers/skill-census';
const ROOT = path.resolve(new URL(import.meta.url).pathname, '..', '..');
const ROOT = path.resolve(import.meta.path, '..', '..');
const census = skillCensus(ROOT);
describe('skillCensus', () => {