Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: coordinate review timeout cleanup by lifecycle phase (#13417)

[._.]/ Adam Eivyfb50511cea22
diff --git a/server/scripts/run-local-code-review.test.js b/server/scripts/run-local-code-review.test.js
index e5759784ae..dee4c083c5 100644
--- a/server/scripts/run-local-code-review.test.js
+++ b/server/scripts/run-local-code-review.test.js
@@ -133,9 +133,58 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
const binDir = join(root, 'bin');
await mkdir(binDir);
+ // Freeze budget accounting and hold only the review's 250ms deadlines.
+ // The synthetic server advances
+ // them after it has reached the phase this case is intended to exercise.
+ // The outer watchdog and app-server shutdown timers still use real time.
+ const clockPreload = join(root, 'review-clock.cjs');
+ await writeFile(clockPreload, `
+ const cp = require('node:child_process');
+ const { syncBuiltinESMExports } = require('node:module');
+ const { writeFileSync } = require('node:fs');
+ const realSetTimeout = globalThis.setTimeout;
+ const realClearTimeout = globalThis.clearTimeout;
+ const pending = new Map();
+ let now = Date.now();
+ if (process.env.FAKE_CODEX_CLOCK_FILE) Date.now = () => now;
+ globalThis.setTimeout = (callback, ms, ...args) => {
+ if (!process.env.FAKE_CODEX_CLOCK_FILE || ms !== 250) return realSetTimeout(callback, ms, ...args);
+ const handle = realSetTimeout(() => {}, 2147483647);
+ pending.set(handle, () => callback(...args));
+ return handle;
+ };
+ globalThis.clearTimeout = handle => {
+ pending.delete(handle);
+ return realClearTimeout(handle);
+ };
+ const realSpawn = cp.spawn;
+ cp.spawn = (...args) => {
+ const child = realSpawn(...args);
+ let buffered = '';
+ child.stdout?.on('data', chunk => {
+ buffered += chunk;
+ const lines = buffered.split('\\n');
+ buffered = lines.pop();
+ for (const line of lines) {
+ if (!line.includes('test/advance-review-clock')) continue;
+ setImmediate(() => {
+ now += 250;
+ const timers = [...pending];
+ writeFileSync(process.env.FAKE_CODEX_CLOCK_FILE, JSON.stringify({ timersFired: timers.length }));
+ for (const [handle, fire] of timers) {
+ globalThis.clearTimeout(handle);
+ fire();
+ }
+ });
+ }
+ });
+ return child;
+ };
+ syncBuiltinESMExports();
+ `);
const fakeCodex = join(binDir, 'fake-codex.cjs');
await writeFile(fakeCodex, `
- const { writeFileSync, appendFileSync } = require('node:fs');
+ const { writeFileSync, appendFileSync, readdirSync } = require('node:fs');
const readline = require('node:readline');
if (process.env.FAKE_CODEX_INVOCATION_FILE) {
@@ -153,6 +202,12 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
try {
const msg = JSON.parse(line);
if (msg.method === 'initialize') {
+ if (mode === 'pre-turn-timeout') {
+ const scratch = readdirSync(process.env.TMPDIR).filter(name => name.startsWith('portos-codex-text-'));
+ writeFileSync(process.env.FAKE_CODEX_SCRATCH_FILE, JSON.stringify(scratch));
+ process.stdout.write(JSON.stringify({ method: 'test/advance-review-clock' }) + '\\n');
+ return;
+ }
process.stdout.write(JSON.stringify({
jsonrpc: '2.0',
id: msg.id,
@@ -173,6 +228,9 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
id: msg.id,
result: { turn: { id: 'turn-test', items: [], status: 'inProgress' } }
}) + '\\n');
+ if (mode === 'timeout') {
+ process.stdout.write(JSON.stringify({ method: 'test/advance-review-clock' }) + '\\n');
+ }
if (mode === 'success') {
process.stdout.write(JSON.stringify({
jsonrpc: '2.0',
@@ -250,6 +308,8 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
},
}));
+ const scratchDir = join(root, 'scratch');
+ await mkdir(scratchDir);
const invocationsFile = join(root, 'codex-invocations.txt');
const unrelatedChild = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' });
@@ -262,10 +322,13 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
MEMORY_BACKEND: 'file',
PORTOS_DATA_ROOT: root,
FAKE_CODEX_INVOCATION_FILE: invocationsFile,
+ TMPDIR: scratchDir,
+ TMP: scratchDir,
+ TEMP: scratchDir,
...envOverrides,
};
delete env.VITEST;
- const child = spawn(process.execPath, ['--preserve-symlinks', '--preserve-symlinks-main', join(root, 'server/scripts/run-local-code-review.mjs')], {
+ const child = spawn(process.execPath, ['--require', clockPreload, '--preserve-symlinks', '--preserve-symlinks-main', join(root, 'server/scripts/run-local-code-review.mjs')], {
cwd: root,
env,
stdio: ['pipe', 'pipe', 'pipe'],
@@ -308,12 +371,13 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
const settingsAfterSuccess = JSON.parse(await readFile(settingsFile, 'utf8'));
expect(settingsAfterSuccess.codeReview?.reviewerHealth?.['provider:codex']).toBeUndefined();
- // 2. Timed-out review: proves child exits, scratch dir is removed, failure exit code
+ // 2. Expire only after turn/start: child exit and scratch cleanup must hold
+ // even when the reviewer accepts a turn but never completes it.
const pidFile2 = join(root, 'codex-timeout.pid');
const cwdFile2 = join(root, 'codex-timeout-cwd.txt');
const timeoutRes = await runReview(
{ backend: 'provider:codex', diff: 'diff --git a/b.js b/b.js', timeoutMs: 250 },
- { FAKE_CODEX_MODE: 'timeout', FAKE_CODEX_PID_FILE: pidFile2, FAKE_CODEX_CWD_FILE: cwdFile2 },
+ { FAKE_CODEX_MODE: 'timeout', FAKE_CODEX_PID_FILE: pidFile2, FAKE_CODEX_CWD_FILE: cwdFile2, FAKE_CODEX_CLOCK_FILE: join(root, 'turn-clock.json') },
);
expect(timeoutRes.code).toBe(1);
@@ -331,11 +395,36 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
expect(scratchCwd2).toContain('portos-codex-text-');
expect(existsSync(scratchCwd2)).toBe(false);
- // 3. Standalone non-Codex reviewers do not spawn Codex child solely for cleanup
+ expect(JSON.parse(await readFile(join(root, 'turn-clock.json'), 'utf8')).timersFired).toBeGreaterThan(0);
+
+ // 3. Expire during initialize: no thread/CWD evidence exists yet, but the
+ // scratch directory created before connecting and the owned child must go.
+ const preTurnPidFile = join(root, 'codex-pre-turn.pid');
+ const preTurnCwdFile = join(root, 'codex-pre-turn-cwd.txt');
+ const preTurnScratchFile = join(root, 'pre-turn-scratch.json');
+ const preTurnRes = await runReview(
+ { backend: 'provider:codex', diff: 'diff --git a/b.js b/b.js', timeoutMs: 250 },
+ { FAKE_CODEX_MODE: 'pre-turn-timeout', FAKE_CODEX_PID_FILE: preTurnPidFile,
+ FAKE_CODEX_CWD_FILE: preTurnCwdFile, FAKE_CODEX_SCRATCH_FILE: preTurnScratchFile,
+ FAKE_CODEX_CLOCK_FILE: join(root, 'pre-turn-clock.json') },
+ );
+ expect(preTurnRes.code, preTurnRes.stderr).toBe(1);
+ expect(JSON.parse(preTurnRes.stdout)).toMatchObject({ ok: false, error: expect.stringMatching(/250ms/) });
+ expect(preTurnRes.stderr).toContain('Codex app-server stopped');
+ const preTurnPid = Number(await readFile(preTurnPidFile, 'utf8'));
+ expect(preTurnPid).toBeGreaterThan(0);
+ expect(() => process.kill(preTurnPid, 0)).toThrow();
+ expect(existsSync(preTurnCwdFile)).toBe(false);
+ const preTurnScratch = JSON.parse(await readFile(preTurnScratchFile, 'utf8'));
+ expect(preTurnScratch).toHaveLength(1);
+ expect(existsSync(join(scratchDir, preTurnScratch[0]))).toBe(false);
+ expect(JSON.parse(await readFile(join(root, 'pre-turn-clock.json'), 'utf8')).timersFired).toBeGreaterThan(0);
+
+ // 4. Standalone non-Codex reviewers do not spawn Codex child solely for cleanup
const invocationsBefore = existsSync(invocationsFile)
? (await readFile(invocationsFile, 'utf8')).trim().split('\n').filter(Boolean).length
: 0;
- expect(invocationsBefore).toBe(2);
+ expect(invocationsBefore).toBe(3);
const nonCodexRes = await runReview(
{ backend: 'provider:unsupported', diff: 'diff --git a/c.js b/c.js' },
@@ -346,9 +435,9 @@ it('shuts down the Codex app-server child and cleans up scratch resources after
expect(nonCodexStdout).toMatchObject({ ok: false, code: 'REVIEWER_UNAVAILABLE' });
const invocationsAfter = (await readFile(invocationsFile, 'utf8')).trim().split('\n').filter(Boolean).length;
- expect(invocationsAfter).toBe(2);
+ expect(invocationsAfter).toBe(3);
- // 4. Unrelated processes remain untouched
+ // 5. Unrelated processes remain untouched
expect(() => process.kill(unrelatedChild.pid, 0)).not.toThrow();
} finally {
try { process.kill(unrelatedChild.pid, 'SIGKILL'); } catch {}