wishforge-ide/packages/runtime/test/process-cancellation.test.ts
eust-w 5f32e4613e 🐛 Keep the output of stopped commands, and stop what commands leave running
- A check stopped at its time or output limit lost everything it had
  printed: the repair prompt only said that WishForge stopped it, not
  where it hung. The stop now carries the output so far, and the
  verification evidence and failed installations keep it, with the
  reason appended.
- A command that left a process running (a test's server, say) held
  its pipes open until the time limit and counted as hanging, and in
  host mode such processes stayed on the machine. A command now ends
  when its own process exits: the rest of its process group gets two
  seconds to drain the pipes and is then stopped, as is anything else
  it left running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 19:21:24 +08:00

76 lines
3.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { CommandStopped, executeCommand } from '../src/process.js';
import { stoppedResult } from '../src/verify.js';
describe('command cancellation', () => {
it('does not spawn a process when cancellation is already requested', async () => {
const signal = AbortSignal.abort();
await expect(executeCommand({ executable: '/this/must/not/be/executed', args: [], signal })).rejects.toMatchObject({
code: 'CANCELED',
});
});
it('cancels an in-flight process without waiting for its execution deadline', async () => {
const controller = new AbortController();
const started = Date.now();
const command = executeCommand({
executable: process.execPath,
args: ['-e', 'setInterval(()=>{},1000)'],
signal: controller.signal,
timeoutMs: 10000,
});
setTimeout(() => controller.abort(), 50);
await expect(command).rejects.toMatchObject({ code: 'CANCELED' });
expect(Date.now() - started).toBeLessThan(3000);
});
});
const alive = (pid: number) => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};
async function stops(pid: number) {
for (let attempt = 0; attempt < 40 && alive(pid); attempt++) await new Promise(resolve => setTimeout(resolve, 50));
return !alive(pid);
}
const node = (source: string, timeoutMs = 20_000) =>
executeCommand({ executable: process.execPath, args: ['-e', source], timeoutMs });
describe('command output and leftovers', () => {
it('keeps what a command printed when it is stopped at its time limit', async () => {
const stopped = node(
"console.log('test 1 passed'); console.error('test 2 is waiting'); setInterval(() => {}, 1000)",
500,
);
await expect(stopped).rejects.toBeInstanceOf(CommandStopped);
await expect(stopped).rejects.toMatchObject({
code: 'TIMEOUT',
output: { stdout: 'test 1 passed\n', stderr: 'test 2 is waiting\n' },
});
const result = stoppedResult(await stopped.catch(error => error), { timeoutMs: 600_000 });
expect(result).toEqual({
exitCode: 124,
stdout: 'test 1 passed\n',
stderr: 'test 2 is waiting\nWishForge stopped the command after 600 s',
});
});
it('finishes when the command exits, stopping a process it left holding its output', async () => {
const started = Date.now();
const result = await node(
"const child = require('node:child_process').spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'inherit' }); child.unref(); console.log(child.pid);",
);
expect(result.exitCode).toBe(0);
expect(Date.now() - started).toBeLessThan(10_000);
expect(await stops(Number(result.stdout.trim()))).toBe(true);
});
it('stops what the command left running even when it does not hold the output', async () => {
const result = await node(
"const child = require('node:child_process').spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' }); child.unref(); console.log(child.pid);",
);
expect(result.exitCode).toBe(0);
expect(await stops(Number(result.stdout.trim()))).toBe(true);
});
});