Skip to content

The design language that makes your AI harness better at design.

JavaScript

A sandboxed wait cannot signal the server, and EPERM was read as death

Paul Bakaus6c4620bf53ce
diff --git a/skill/scripts/serve-question.mjs b/skill/scripts/serve-question.mjs
index 7925cd2e4..53f3cfd2a 100644
--- a/skill/scripts/serve-question.mjs
+++ b/skill/scripts/serve-question.mjs
@@ -161,15 +161,25 @@ if (hasFlag('wait')) {
const pollSec = Number(arg('poll', '60'));
const deadline = Date.now() + pollSec * 1000;
const answered = () => fs.existsSync(answerFile(key));
+ // Liveness must survive sandboxes: a sandboxed --wait cannot signal the
+ // daemon (kill throws EPERM even for a living process), so a fresh page
+ // heartbeat in the state file is the primary proof of life, the kill probe
+ // is secondary, and EPERM specifically means "exists, but the sandbox
+ // blocks signals", never "dead". Treating EPERM as death told one session
+ // the user had walked away while they were still reading the board.
const alive = () => {
- try { process.kill(JSON.parse(fs.readFileSync(stateFile(key), 'utf8')).pid, 0); return true; }
- catch { return false; }
+ try {
+ const state = JSON.parse(fs.readFileSync(stateFile(key), 'utf8'));
+ if (state.lastBeat && Date.now() - state.lastBeat < 12000) return true;
+ try { process.kill(state.pid, 0); return true; }
+ catch (err) { return err.code === 'EPERM'; }
+ } catch { return false; }
};
let sawClose = false;
while (Date.now() < deadline) {
if (answered()) break;
if (!alive()) {
- console.log('serve-question: the question server is gone with no answer');
+ console.log('serve-question: the question server is gone with no answer. This is a server failure, not a user decision: restart it with --start and the same payload, reopen the URL for the user, and wait again. Never proceed without their choice while their browser session is open.');
process.exit(2);
}
try {
diff --git a/tests/serve-question.test.mjs b/tests/serve-question.test.mjs
index fa2e4685c..0371be4a1 100644
--- a/tests/serve-question.test.mjs
+++ b/tests/serve-question.test.mjs
@@ -146,6 +146,29 @@ describe('serve-question', () => {
assert.equal(code, 1);
});
+ it('trusts a fresh heartbeat over a failed kill probe, and still detects true death', async () => {
+ const dir = mkdtempSync(path.join(tmpdir(), 'serve-question-'));
+ const qdir = path.join(dir, '.impeccable', 'questions');
+ const { mkdirSync } = await import('node:fs');
+ mkdirSync(qdir, { recursive: true });
+ // Fresh heartbeat + a pid that cannot be signaled (throws like a sandbox
+ // EPERM or a recycled pid): the wait must keep WAITING (exit 3), never
+ // declare the server gone (exit 2). Pid 1 throws EPERM for a normal user.
+ writeFileSync(path.join(qdir, 'beat1.state.json'), JSON.stringify({ pid: 1, port: 1, url: 'http://127.0.0.1:1/', lastBeat: Date.now() }));
+ const waiting = await new Promise((resolve) => {
+ const child = spawn(process.execPath, [SCRIPT, '--wait', '--key', 'beat1', '--poll', '2'], { cwd: dir, stdio: 'ignore' });
+ child.on('exit', resolve);
+ });
+ assert.equal(waiting, 3, 'fresh heartbeat must read as alive regardless of the kill probe');
+ // Stale heartbeat + a pid that is genuinely gone (ESRCH): server dead, exit 2.
+ writeFileSync(path.join(qdir, 'dead1.state.json'), JSON.stringify({ pid: 999999999 >>> 8, port: 1, url: 'http://127.0.0.1:1/' }));
+ const dead = await new Promise((resolve) => {
+ const child = spawn(process.execPath, [SCRIPT, '--wait', '--key', 'dead1', '--poll', '2'], { cwd: dir, stdio: 'ignore' });
+ child.on('exit', resolve);
+ });
+ assert.equal(dead, 2, 'a truly missing process must still read as gone');
+ });
+
it('renders anatomy, streams late sketches, and returns the chosen sketch', async () => {
const dir = mkdtempSync(path.join(tmpdir(), 'serve-question-'));
const sketchPath = path.join(dir, 'sketches', 'assigned.webp');