@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
ts 667 lines 23.7 KB
Raw
1 import assert from 'node:assert/strict';
2 import { execFileSync, spawn, spawnSync } from 'node:child_process';
3 import {
4 copyFileSync,
5 existsSync,
6 mkdirSync,
7 mkdtempSync,
8 readFileSync,
9 readdirSync,
10 rmSync,
11 symlinkSync,
12 writeFileSync,
13 } from 'node:fs';
14 import os from 'node:os';
15 import path from 'node:path';
16 import { afterEach, test } from 'bun:test';
17 import { setTimeout as delay } from 'node:timers/promises';
18
19 interface TaskStatus {
20 task: {
21 id: string;
22 goal: string;
23 nextStep: string | null;
24 blocker: string | null;
25 authority: string;
26 };
27 status: string;
28 receipt: string;
29 }
30 type Fixture = ReturnType<typeof fixture>;
31
32 const script = new URL('../scripts/agent-task.ts', import.meta.url);
33 const cleanups: Array<() => void> = [];
34 afterEach(() => {
35 for (const cleanup of cleanups.splice(0).reverse()) cleanup();
36 });
37
38 const lingeringWorker =
39 'process.on("SIGTERM",()=>{});require("fs").writeFileSync(".artifacts/child.pid",String(process.pid));setInterval(()=>{},1000)';
40
41 const fixture = () => {
42 const root = mkdtempSync(path.join(os.tmpdir(), 'composio-agent-task-'));
43 cleanups.push(() => rmSync(root, { recursive: true, force: true }));
44 mkdirSync(path.join(root, 'scripts'));
45 copyFileSync(script, path.join(root, 'scripts/agent-task.ts'));
46 symlinkSync(new URL('../node_modules', import.meta.url), path.join(root, 'node_modules'), 'dir');
47 writeFileSync(path.join(root, '.gitignore'), '.artifacts/\nnode_modules\n');
48 writeFileSync(path.join(root, 'source.txt'), 'original');
49 mkdirSync(path.join(root, '.claude'));
50 writeFileSync(path.join(root, '.claude/scheduled_tasks.lock'), 'session one');
51 const profiles = {
52 offline: {
53 boundary: 'offline',
54 commands: [{ cwd: '.', argv: [process.execPath, '-e', 'console.log("verified")'] }],
55 },
56 failure: {
57 boundary: 'offline',
58 commands: [
59 {
60 cwd: '.',
61 argv: [
62 process.execPath,
63 '-e',
64 'process.exit(require("fs").existsSync(".artifacts/succeed") ? 0 : 7)',
65 ],
66 },
67 { cwd: '.', argv: [process.execPath, '-e', 'console.log("second")'] },
68 ],
69 },
70 missing: {
71 boundary: 'offline',
72 commands: [{ cwd: '.', argv: ['composio-test-missing-executable'] }],
73 },
74 mutating: {
75 boundary: 'offline',
76 commands: [
77 {
78 cwd: '.',
79 argv: [
80 process.execPath,
81 '-e',
82 'require("fs").writeFileSync("source.txt", "changed during verification")',
83 ],
84 },
85 ],
86 },
87 noisy: {
88 boundary: 'offline',
89 commands: [
90 {
91 cwd: '.',
92 argv: [
93 process.execPath,
94 '-e',
95 // Keep ordered markers on one pipe; stdout and stderr drain independently.
96 'console.log("first line");console.log("x".repeat(200000));console.log("final type error");process.exitCode=2',
97 ],
98 },
99 ],
100 },
101 stderr: {
102 boundary: 'offline',
103 commands: [
104 {
105 cwd: '.',
106 argv: [process.execPath, '-e', 'console.error("saved error");process.exitCode=2'],
107 },
108 ],
109 },
110 interrupt: {
111 boundary: 'offline',
112 commands: [
113 {
114 cwd: '.',
115 argv: [
116 process.execPath,
117 '-e',
118 'const child=require("child_process").spawn(process.execPath,["-e","setInterval(()=>{},1000)"],{stdio:"inherit"});require("fs").writeFileSync(".artifacts/child.pid",String(child.pid));setInterval(()=>{},1000)',
119 ],
120 },
121 ],
122 },
123 lingering: {
124 boundary: 'offline',
125 commands: [
126 {
127 cwd: '.',
128 argv: [
129 process.execPath,
130 '-e',
131 `require("child_process").spawn(process.execPath,["-e",${JSON.stringify(lingeringWorker)}],{stdio:"ignore"});setInterval(()=>{if(require("fs").existsSync(".artifacts/child.pid"))process.exit(0)},10)`,
132 ],
133 },
134 ],
135 },
136 };
137 writeFileSync(path.join(root, 'scripts/agent-profiles.json'), JSON.stringify(profiles));
138 for (const args of [
139 ['init', '-q'],
140 ['add', '.'],
141 ['-c', 'user.name=Test', '-c', 'user.email=test@example.invalid', 'commit', '-qm', 'baseline'],
142 ]) {
143 execFileSync('git', args, { cwd: root });
144 }
145 const run = (...args: string[]) =>
146 spawnSync(process.execPath, ['scripts/agent-task.ts', ...args], {
147 cwd: root,
148 encoding: 'utf8',
149 });
150 const state = (id: string): TaskStatus => JSON.parse(run('status', id).stdout);
151 const init = (id = 'case', profile = 'offline') =>
152 run(
153 'init',
154 id,
155 '--goal',
156 'Verify the changed behavior',
157 '--profile',
158 profile,
159 '--authority',
160 'local verification only'
161 );
162 return { root, run, state, init };
163 };
164
165 async function runningFixture(f: Fixture) {
166 assert.equal(f.init('case', 'interrupt').status, 0);
167 const runner = spawn(process.execPath, ['scripts/agent-task.ts', 'verify', 'case'], {
168 cwd: f.root,
169 stdio: 'ignore',
170 });
171 const exit = new Promise(resolve =>
172 runner.on('exit', (code, signal) => resolve({ code, signal }))
173 );
174 cleanups.push(() => runner.kill('SIGKILL'));
175 const file = path.join(f.root, '.artifacts/child.pid');
176 for (let i = 0; i < 250 && !existsSync(file); i++) await delay(20);
177 const childPid = Number(readFileSync(file, 'utf8'));
178 const receipt = JSON.parse(readFileSync(path.join(f.root, f.state('case').receipt), 'utf8'));
179 const group = receipt.commands[0].pid;
180 cleanups.push(() => {
181 try {
182 process.kill(-group, 'SIGKILL');
183 } catch {}
184 });
185 return { runner, exit, childPid, group };
186 }
187
188 async function waitGone(pid: number) {
189 for (let i = 0; i < 250; i++) {
190 try {
191 process.kill(pid, 0);
192 } catch (error) {
193 const code = error instanceof Error && 'code' in error ? error.code : undefined;
194 if (code === 'ESRCH') return;
195 assert.equal(code, 'EPERM');
196 }
197 await delay(20);
198 }
199 assert.fail(`Process ${pid} did not stop`);
200 }
201
202 function useOfflineProfile(f: Fixture) {
203 const file = path.join(f.root, 'scripts/agent-profiles.json');
204 const profiles = JSON.parse(readFileSync(file, 'utf8'));
205 profiles.interrupt.commands = profiles.offline.commands;
206 writeFileSync(file, JSON.stringify(profiles));
207 }
208
209 test('CLI help describes commands and required initialization inputs without creating a task', () => {
210 const f = fixture();
211 const rootHelp = f.run('--help');
212 assert.equal(rootHelp.status, 0, rootHelp.stderr);
213 assert.match(rootHelp.stdout, /init/);
214 assert.match(rootHelp.stdout, /verify/);
215 const initHelp = f.run('init', '--help');
216 assert.equal(initHelp.status, 0, initHelp.stderr);
217 assert.match(initHelp.stdout, /--goal/);
218 assert.match(initHelp.stdout, /--profile/);
219 assert.equal(existsSync(path.join(f.root, '.artifacts/agent-tasks')), false);
220 });
221
222 test('invalid CLI inputs fail before creating task state', () => {
223 const f = fixture();
224 const missing = f.run('init', 'case', '--profile', 'offline');
225 assert.equal(missing.status, 1);
226 assert.match(missing.stderr, /goal/i);
227 const unknown = f.run(
228 'init',
229 'case',
230 '--goal',
231 'Check behavior',
232 '--profile',
233 'offline',
234 '--bogus'
235 );
236 assert.equal(unknown.status, 1);
237 assert.match(unknown.stderr, /bogus/i);
238 const completions = f.run('--completions', 'nope');
239 assert.equal(completions.status, 1);
240 assert.match(completions.stderr, /nope/);
241 const duplicate = f.run(
242 'init',
243 'case',
244 '--goal',
245 'A goal',
246 '--profile',
247 'offline',
248 '--profile',
249 'failure'
250 );
251 assert.equal(duplicate.status, 1);
252 assert.match(duplicate.stderr, /profile/);
253 const authority = f.run(
254 'init',
255 'case',
256 '--goal',
257 'A goal',
258 '--profile',
259 'offline',
260 '--authority',
261 'First scope',
262 '--authority',
263 'Other scope'
264 );
265 assert.equal(authority.status, 1);
266 assert.match(authority.stderr, /authority/);
267 assert.equal(existsSync(path.join(f.root, '.artifacts/agent-tasks')), false);
268 });
269
270 test('dash-leading task text is saved literally instead of activating built-in flags', () => {
271 const f = fixture();
272 assert.equal(
273 f.run(
274 'init',
275 'case',
276 '--goal',
277 '- Fix the CLI',
278 '--profile',
279 'offline',
280 '--authority',
281 '- No merge'
282 ).status,
283 0
284 );
285 assert.equal(existsSync(path.join(f.root, '.artifacts/agent-tasks/case/task.json')), true);
286 assert.equal(f.state('case').task.goal, '- Fix the CLI');
287 assert.equal(f.state('case').task.authority, '- No merge');
288 });
289
290 test('task context survives a fresh invocation and duplicate init preserves it', () => {
291 const f = fixture();
292 assert.equal(f.init().status, 0);
293 const task = f.state('case').task;
294 assert.equal(task.goal, 'Verify the changed behavior');
295 assert.equal(task.authority, 'local verification only');
296 assert.equal(f.init().status, 1);
297 assert.deepEqual(f.state('case').task, task);
298 assert.equal(f.state('case').status, 'pending');
299 assert.equal(JSON.parse(f.run('list').stdout)[0].task.id, 'case');
300 });
301
302 test('list reports an unreadable task folder without hiding the others', () => {
303 const f = fixture();
304 assert.equal(f.init().status, 0);
305 mkdirSync(path.join(f.root, '.artifacts/agent-tasks/broken'));
306 writeFileSync(path.join(f.root, '.artifacts/agent-tasks/broken/task.json'), '{');
307 const result = f.run('list');
308 assert.equal(result.status, 0, result.stderr);
309 const [broken, valid] = JSON.parse(result.stdout);
310 assert.equal(broken.id, 'broken');
311 assert.equal(broken.status, 'unreadable');
312 assert.equal(valid.task.id, 'case');
313 assert.equal(valid.status, 'pending');
314 });
315
316 test('successful profile evidence expires after dirty edits, untracked sources, or task changes', () => {
317 const f = fixture();
318 assert.equal(f.init().status, 0);
319 assert.equal(f.run('verify', 'case').status, 0);
320 assert.equal(f.state('case').status, 'passed');
321 writeFileSync(path.join(f.root, 'source.txt'), 'changed without a commit');
322 assert.equal(f.state('case').status, 'stale');
323 assert.equal(f.run('verify', 'case').status, 0);
324 assert.equal(f.state('case').status, 'passed');
325 writeFileSync(path.join(f.root, 'new-source.txt'), 'new input');
326 assert.equal(f.state('case').status, 'stale');
327 assert.equal(f.run('verify', 'case').status, 0);
328 const taskPath = path.join(f.root, '.artifacts/agent-tasks/case/task.json');
329 const task = JSON.parse(readFileSync(taskPath, 'utf8'));
330 task.goal = 'A different acceptance outcome';
331 writeFileSync(taskPath, JSON.stringify(task));
332 assert.equal(f.state('case').status, 'stale');
333 });
334
335 test('the Claude Code scheduler lock does not invalidate evidence', () => {
336 const f = fixture();
337 assert.equal(f.init().status, 0);
338 assert.equal(f.run('verify', 'case').status, 0);
339 writeFileSync(path.join(f.root, '.claude/scheduled_tasks.lock'), 'session two');
340 assert.equal(f.state('case').status, 'passed');
341 writeFileSync(path.join(f.root, 'source.txt'), 'changed without a commit');
342 assert.equal(f.state('case').status, 'stale');
343 });
344
345 test('changed profiles invalidate receipts and failed attempts survive later success', () => {
346 const f = fixture();
347 assert.equal(f.init('case', 'failure').status, 0);
348 assert.equal(f.run('verify', 'case').status, 1);
349 assert.equal(f.state('case').status, 'failed');
350 const attempts = path.join(f.root, '.artifacts/agent-tasks/case/attempts');
351 const first = readdirSync(attempts)[0];
352 const failed = readFileSync(path.join(attempts, first, 'receipt.json'), 'utf8');
353 writeFileSync(path.join(f.root, '.artifacts/succeed'), 'ready');
354 assert.equal(f.run('verify', 'case').status, 0);
355 assert.equal(f.state('case').status, 'passed');
356 assert.equal(readdirSync(attempts).length, 2);
357 assert.equal(readFileSync(path.join(attempts, first, 'receipt.json'), 'utf8'), failed);
358 const profilesPath = path.join(f.root, 'scripts/agent-profiles.json');
359 const profiles = JSON.parse(readFileSync(profilesPath, 'utf8'));
360 profiles.failure.boundary = 'packaged';
361 writeFileSync(profilesPath, JSON.stringify(profiles));
362 assert.equal(f.state('case').status, 'stale');
363 });
364
365 test('handoff progress notes preserve proof without changing its acceptance contract', () => {
366 const f = fixture();
367 assert.equal(f.init().status, 0);
368 assert.equal(f.run('verify', 'case').status, 0);
369 const taskPath = path.join(f.root, '.artifacts/agent-tasks/case/task.json');
370 const task = JSON.parse(readFileSync(taskPath, 'utf8'));
371 task.nextStep = 'Review the verified diff';
372 task.blocker = 'Waiting for an owner decision';
373 writeFileSync(taskPath, JSON.stringify(task));
374 assert.equal(f.state('case').status, 'passed');
375 assert.equal(f.state('case').task.nextStep, task.nextStep);
376 });
377
378 test('an unfinished newer attempt cannot expose an older passing receipt', () => {
379 const f = fixture();
380 assert.equal(f.init().status, 0);
381 assert.equal(f.run('verify', 'case').status, 0);
382 mkdirSync(path.join(f.root, '.artifacts/agent-tasks/case/attempts/9999999999999-incomplete'));
383 assert.equal(f.state('case').status, 'interrupted');
384 });
385
386 test('a killed runner exposes interrupted evidence and a recoverable lock', async () => {
387 const f = fixture();
388 const { runner, exit, group } = await runningFixture(f);
389 runner.kill('SIGKILL');
390 assert.deepEqual(await Promise.race([exit, delay(5000, 'timed out', { ref: false })]), {
391 code: null,
392 signal: 'SIGKILL',
393 });
394 assert.equal(f.state('case').status, 'interrupted');
395 const blocked = f.run('verify', 'case');
396 assert.equal(blocked.status, 1);
397 assert.match(blocked.stderr, /Inspect and stop that group/);
398 process.kill(-group, 'SIGKILL');
399 await waitGone(-group);
400 useOfflineProfile(f);
401 assert.equal(f.run('verify', 'case').status, 0);
402 assert.equal(f.state('case').status, 'passed');
403 });
404
405 test('missing tools fail and preserve an attempted command receipt', () => {
406 const f = fixture();
407 assert.equal(f.init('case', 'missing').status, 0);
408 assert.equal(f.run('verify', 'case').status, 1);
409 assert.equal(f.state('case').status, 'failed');
410 const attempts = path.join(f.root, '.artifacts/agent-tasks/case/attempts');
411 const receipt = JSON.parse(
412 readFileSync(path.join(attempts, readdirSync(attempts)[0], 'receipt.json'), 'utf8')
413 );
414 assert.equal(receipt.commands.length, 1);
415 assert.notEqual(receipt.commands[0].exitCode, 0);
416 });
417
418 test('long output keeps its start and end in the capped log', () => {
419 const f = fixture();
420 assert.equal(f.init('case', 'noisy').status, 0);
421 assert.equal(f.run('verify', 'case').status, 1);
422 const receiptPath = path.join(f.root, f.state('case').receipt);
423 const receipt = JSON.parse(readFileSync(receiptPath, 'utf8'));
424 assert.equal(receipt.logTruncated, true);
425 const log = readFileSync(path.join(path.dirname(receiptPath), 'output.log'), 'utf8');
426 assert.ok(log.length <= 64 * 1024 + 100, `log has ${log.length} bytes`);
427 assert.match(log, /^first line\n/);
428 assert.match(log, /bytes of output omitted/);
429 assert.match(log, /final type error\n$/);
430 });
431
432 test('command stderr is saved in the output log', () => {
433 const f = fixture();
434 assert.equal(f.init('case', 'stderr').status, 0);
435 assert.equal(f.run('verify', 'case').status, 1);
436 const receiptPath = path.join(f.root, f.state('case').receipt);
437 const log = readFileSync(path.join(path.dirname(receiptPath), 'output.log'), 'utf8');
438 assert.equal(log, 'saved error\n');
439 });
440
441 test('a successful command that edits its verification subject cannot establish fresh proof', () => {
442 const f = fixture();
443 assert.equal(f.init('case', 'mutating').status, 0);
444 assert.equal(f.run('verify', 'case').status, 1);
445 assert.equal(f.state('case').status, 'stale');
446 });
447
448 for (const signal of ['SIGTERM', 'SIGHUP'] as const) {
449 test(`${signal} terminates the command group, preserves evidence and releases the task for retry`, async () => {
450 const f = fixture();
451 const { runner, exit, childPid } = await runningFixture(f);
452 runner.kill(signal);
453 assert.deepEqual(await Promise.race([exit, delay(5000, 'timed out', { ref: false })]), {
454 code: 1,
455 signal: null,
456 });
457 assert.equal(f.state('case').status, 'interrupted');
458 await waitGone(childPid);
459 useOfflineProfile(f);
460 assert.equal(f.run('verify', 'case').status, 0);
461 assert.equal(f.state('case').status, 'passed');
462 });
463 }
464
465 test('verification remains locked until independent-stdio descendants stop', async () => {
466 const f = fixture();
467 const file = path.join(f.root, 'scripts/agent-profiles.json');
468 const profiles = JSON.parse(readFileSync(file, 'utf8'));
469 const descendant = 'process.on("SIGTERM",()=>{});console.log("ready");setInterval(()=>{},1000)';
470 profiles.interrupt.commands[0].argv[2] = `
471 const child = require('child_process').spawn(process.execPath, ['-e', ${JSON.stringify(descendant)}], { stdio: ['ignore', 'pipe', 'ignore'] });
472 child.stdout.once('data', () => require('fs').writeFileSync('.artifacts/child.pid', String(child.pid)));
473 process.on('SIGTERM', () => process.exit(0));
474 setInterval(() => {}, 1000);
475 `;
476 writeFileSync(file, JSON.stringify(profiles));
477 const { runner, exit, childPid, group } = await runningFixture(f);
478 assert.equal(f.init('other').status, 0);
479 runner.kill('SIGTERM');
480 await waitGone(group);
481 process.kill(childPid, 0);
482 assert.equal(existsSync(path.join(f.root, '.artifacts/agent-locks/verify.lock')), true);
483 const overlap = f.run('verify', 'other');
484 assert.equal(
485 overlap.status,
486 1,
487 'the lock must outlive the direct command until its descendants stop'
488 );
489 assert.match(overlap.stderr, /case/);
490 assert.deepEqual(await Promise.race([exit, delay(5000, 'timed out', { ref: false })]), {
491 code: 1,
492 signal: null,
493 });
494 await waitGone(childPid);
495 assert.equal(f.state('case').status, 'interrupted');
496 });
497
498 test('different task profiles serialize shared worktree verification', async () => {
499 const f = fixture();
500 const { runner, exit } = await runningFixture(f);
501 assert.equal(f.init('other').status, 0);
502 try {
503 const overlap = f.run('verify', 'other');
504 assert.equal(overlap.status, 1, 'a different task must not overlap worktree verification');
505 assert.match(overlap.stderr, /case/);
506 } finally {
507 runner.kill('SIGTERM');
508 await exit;
509 }
510 assert.equal(f.run('verify', 'other').status, 0);
511 });
512
513 test('an unreadable lock is replaced once no attempt is running', () => {
514 const f = fixture();
515 assert.equal(f.init().status, 0);
516 mkdirSync(path.join(f.root, '.artifacts/agent-locks'), { recursive: true });
517 writeFileSync(path.join(f.root, '.artifacts/agent-locks/verify.lock'), '');
518 const result = f.run('verify', 'case');
519 assert.equal(result.status, 0, result.stderr);
520 assert.match(result.stderr, /Replacing unreadable lock/);
521 assert.equal(f.state('case').status, 'passed');
522 });
523
524 test('an unreadable lock still blocks verification while another attempt runs', async () => {
525 const f = fixture();
526 const { runner, exit } = await runningFixture(f);
527 assert.equal(f.init('other').status, 0);
528 try {
529 mkdirSync(path.join(f.root, '.artifacts/agent-locks'), { recursive: true });
530 writeFileSync(path.join(f.root, '.artifacts/agent-locks/verify.lock'), '{');
531 const overlap = f.run('verify', 'other');
532 assert.equal(overlap.status, 1);
533 assert.match(overlap.stderr, /unreadable and task case still has a running attempt/);
534 } finally {
535 runner.kill('SIGTERM');
536 await exit;
537 }
538 });
539
540 test('a revision-only change invalidates otherwise identical source evidence', () => {
541 const f = fixture();
542 assert.equal(f.init().status, 0);
543 assert.equal(f.run('verify', 'case').status, 0);
544 execFileSync(
545 'git',
546 [
547 '-c',
548 'user.name=Test',
549 '-c',
550 'user.email=test@example.invalid',
551 'commit',
552 '--allow-empty',
553 '-qm',
554 'new revision',
555 ],
556 { cwd: f.root }
557 );
558 assert.equal(f.state('case').status, 'stale');
559 });
560
561 test('task ids cannot collide with worktree lock files', () => {
562 const f = fixture();
563 assert.equal(f.init('verify.lock').status, 0);
564 assert.equal(f.init('acquire.lock').status, 0);
565 assert.equal(f.init().status, 0);
566 const result = f.run('verify', 'case');
567 assert.equal(result.status, 0, result.stderr);
568 assert.equal(f.run('verify', 'verify.lock').status, 0);
569 assert.equal(f.state('verify.lock').status, 'passed');
570 });
571
572 test('a command that exits 0 cannot leave workers running after verification', () => {
573 const f = fixture();
574 assert.equal(f.init('case', 'lingering').status, 0);
575 const result = f.run('verify', 'case');
576 const childPid = Number(readFileSync(path.join(f.root, '.artifacts/child.pid'), 'utf8'));
577 cleanups.push(() => {
578 try {
579 process.kill(childPid, 'SIGKILL');
580 } catch {}
581 });
582 assert.equal(result.status, 0, result.stderr);
583 assert.throws(() => process.kill(childPid, 0), { code: 'ESRCH' });
584 assert.equal(f.state('case').status, 'passed');
585 });
586
587 test('a runner killed before saving its command pid keeps the lock', () => {
588 const f = fixture();
589 assert.equal(f.init().status, 0);
590 const deadRunner = spawnSync(process.execPath, ['-e', '']).pid;
591 const attempt = path.join(f.root, '.artifacts/agent-tasks/case/attempts/1-unsaved-pid');
592 mkdirSync(attempt, { recursive: true });
593 writeFileSync(
594 path.join(attempt, 'receipt.json'),
595 JSON.stringify({
596 version: 1,
597 id: '1-unsaved-pid',
598 runnerPid: deadRunner,
599 status: 'running',
600 commands: [
601 {
602 cwd: '.',
603 argv: ['long-check'],
604 startedAt: new Date().toISOString(),
605 finishedAt: null,
606 exitCode: null,
607 signal: null,
608 },
609 ],
610 })
611 );
612 const lock = path.join(f.root, '.artifacts/agent-locks/verify.lock');
613 mkdirSync(path.dirname(lock), { recursive: true });
614 writeFileSync(lock, JSON.stringify({ pid: deadRunner, task: 'case' }));
615 const blocked = f.run('verify', 'case');
616 assert.equal(blocked.status, 1);
617 assert.match(blocked.stderr, /no recorded process group/);
618 assert.equal(existsSync(lock), true);
619 });
620
621 test('an interrupted init can be retried and staging folders stay out of the list', () => {
622 const f = fixture();
623 mkdirSync(path.join(f.root, '.artifacts/agent-tasks/case'), { recursive: true });
624 mkdirSync(path.join(f.root, '.artifacts/agent-tasks/.init-leftover'));
625 const result = f.init();
626 assert.equal(result.status, 0, result.stderr);
627 assert.equal(f.state('case').task.goal, 'Verify the changed behavior');
628 assert.deepEqual(
629 JSON.parse(f.run('list').stdout).map((entry: TaskStatus) => entry.task.id),
630 ['case']
631 );
632 });
633
634 test('a newer attempt stays latest when the clock moves backwards', () => {
635 const f = fixture();
636 assert.equal(f.init('case', 'failure').status, 0);
637 writeFileSync(path.join(f.root, '.artifacts/succeed'), 'ready');
638 assert.equal(f.run('verify', 'case').status, 0);
639 rmSync(path.join(f.root, '.artifacts/succeed'));
640 const clock = path.join(f.root, '.artifacts/clock.ts');
641 writeFileSync(clock, 'Date.now = () => 1;');
642 const earlier = spawnSync(
643 process.execPath,
644 ['--preload', clock, 'scripts/agent-task.ts', 'verify', 'case'],
645 { cwd: f.root, encoding: 'utf8' }
646 );
647 assert.equal(earlier.status, 1, earlier.stderr);
648 assert.equal(f.state('case').status, 'failed');
649 });
650
651 test('output written before a runner is killed survives in the log', async () => {
652 const f = fixture();
653 const file = path.join(f.root, 'scripts/agent-profiles.json');
654 const profiles = JSON.parse(readFileSync(file, 'utf8'));
655 profiles.interrupt.commands[0].argv[2] =
656 'process.stdout.write("x".repeat(20000)+"\\nlate diagnostic\\n",()=>{require("fs").writeFileSync(".artifacts/child.pid",String(process.pid))});setInterval(()=>{},1000)';
657 writeFileSync(file, JSON.stringify(profiles));
658 const { runner, exit } = await runningFixture(f);
659 const log = path.join(f.root, path.dirname(f.state('case').receipt), 'output.log');
660 for (let i = 0; i < 250 && !readFileSync(log, 'utf8').includes('late diagnostic'); i++)
661 await delay(20);
662 runner.kill('SIGKILL');
663 await exit;
664 const saved = readFileSync(log, 'utf8');
665 assert.match(saved, /^x{100}/);
666 assert.match(saved, /late diagnostic\n$/);
667 });