@@ -7,108 +7,116 @@ import { runCmdBackground } from '@agent-device/host-kit/command';
77import { entry , fixture , run } from './execution.fixtures.ts' ;
88import type { ExecutionReceipt } from './execution.ts' ;
99
10- test ( 'SIGTERM cancels the command group, records cancellation, and releases the host lock' , async ( ) => {
11- const { cwd } = fixture ( ) ;
12- const ready = path . join ( cwd , '.tmp' , 'ready.json' ) ;
13- const source = `
10+ test (
11+ 'SIGTERM cancels the command group, records cancellation, and releases the host lock' ,
12+ { skip : process . platform === 'win32' } ,
13+ async ( ) => {
14+ const { cwd } = fixture ( ) ;
15+ const ready = path . join ( cwd , '.tmp' , 'ready.json' ) ;
16+ const source = `
1417 import { runValidation } from ${ JSON . stringify ( entry ) } ;
1518 const code = await runValidation(${ JSON . stringify ( cwd ) } , { base: 'HEAD', head: 'HEAD', json: true }, async ({execute}) => {
1619 return execute([process.execPath, '-e', ${ JSON . stringify ( `require('node:fs').writeFileSync(${ JSON . stringify ( ready ) } , JSON.stringify(process.pid)); setInterval(() => {}, 1000);` ) } ], ${ JSON . stringify ( cwd ) } , 'sleep');
1720 }); process.exitCode = code;
1821 ` ;
19- const { child, wait } = runCmdBackground (
20- process . execPath ,
21- [ '--experimental-strip-types' , '--input-type=module' , '-e' , source ] ,
22- {
23- cwd,
24- allowFailure : true ,
25- env : { ...process . env , HOME : cwd , USERPROFILE : cwd , AGENT_DEVICE_VALIDATION_PARENT : '' } ,
26- } ,
27- ) ;
28- try {
29- const deadline = Date . now ( ) + 5000 ;
30- while ( ! fs . existsSync ( ready ) && Date . now ( ) < deadline ) await setTimeout ( 20 ) ;
31- assert . ok ( fs . existsSync ( ready ) , 'command must actually start' ) ;
32- const commandPid = JSON . parse ( fs . readFileSync ( ready , 'utf8' ) ) as number ;
33- child . kill ( 'SIGTERM' ) ;
34- const result = await wait ;
35- assert . equal ( result . exitCode , 143 , result . stderr ) ;
36- const receipt = JSON . parse ( result . stdout ) as ExecutionReceipt ;
37- assert . equal ( receipt . status , 'cancelled' ) ;
38- assert . equal ( receipt . results [ 0 ] ?. status , 'cancelled' ) ;
39- assert . throws ( ( ) => process . kill ( commandPid , 0 ) , { code : 'ESRCH' } ) ;
40- assert . equal ( run ( cwd , 'HEAD' , 'return 0;' ) . exitCode , 0 ) ;
41- } finally {
42- child . kill ( 'SIGKILL' ) ;
43- await wait ;
44- fs . rmSync ( cwd , { recursive : true , force : true } ) ;
45- }
46- } ) ;
22+ const { child, wait } = runCmdBackground (
23+ process . execPath ,
24+ [ '--experimental-strip-types' , '--input-type=module' , '-e' , source ] ,
25+ {
26+ cwd,
27+ allowFailure : true ,
28+ env : { ...process . env , HOME : cwd , USERPROFILE : cwd , AGENT_DEVICE_VALIDATION_PARENT : '' } ,
29+ } ,
30+ ) ;
31+ try {
32+ const deadline = Date . now ( ) + 5000 ;
33+ while ( ! fs . existsSync ( ready ) && Date . now ( ) < deadline ) await setTimeout ( 20 ) ;
34+ assert . ok ( fs . existsSync ( ready ) , 'command must actually start' ) ;
35+ const commandPid = JSON . parse ( fs . readFileSync ( ready , 'utf8' ) ) as number ;
36+ child . kill ( 'SIGTERM' ) ;
37+ const result = await wait ;
38+ assert . equal ( result . exitCode , 143 , result . stderr ) ;
39+ const receipt = JSON . parse ( result . stdout ) as ExecutionReceipt ;
40+ assert . equal ( receipt . status , 'cancelled' ) ;
41+ assert . equal ( receipt . results [ 0 ] ?. status , 'cancelled' ) ;
42+ assert . throws ( ( ) => process . kill ( commandPid , 0 ) , { code : 'ESRCH' } ) ;
43+ assert . equal ( run ( cwd , 'HEAD' , 'return 0;' ) . exitCode , 0 ) ;
44+ } finally {
45+ child . kill ( 'SIGKILL' ) ;
46+ await wait ;
47+ fs . rmSync ( cwd , { recursive : true , force : true } ) ;
48+ }
49+ } ,
50+ ) ;
4751
48- test ( 'a killed validation parent does not let a waiter overlap its surviving command group' , async ( ) => {
49- const { cwd } = fixture ( ) ;
50- const ready = path . join ( cwd , '.tmp' , 'orphan.json' ) ;
51- const env = { ...process . env , HOME : cwd , USERPROFILE : cwd , AGENT_DEVICE_VALIDATION_PARENT : '' } ;
52- const source = `
52+ test (
53+ 'a killed validation parent does not let a waiter overlap its surviving command group' ,
54+ { skip : process . platform === 'win32' } ,
55+ async ( ) => {
56+ const { cwd } = fixture ( ) ;
57+ const ready = path . join ( cwd , '.tmp' , 'orphan.json' ) ;
58+ const env = { ...process . env , HOME : cwd , USERPROFILE : cwd , AGENT_DEVICE_VALIDATION_PARENT : '' } ;
59+ const source = `
5360 import { runValidation } from ${ JSON . stringify ( entry ) } ;
5461 const code = await runValidation(${ JSON . stringify ( cwd ) } , { base: 'HEAD', head: 'HEAD', json: true }, async ({execute}) => execute([process.execPath, '-e', ${ JSON . stringify ( `require('node:fs').writeFileSync(${ JSON . stringify ( ready ) } , JSON.stringify(process.pid)); setInterval(() => {}, 1000);` ) } ], ${ JSON . stringify ( cwd ) } , 'orphan'));
5562 process.exitCode = code;
5663 ` ;
57- const parent = runCmdBackground (
58- process . execPath ,
59- [ '--experimental-strip-types' , '--input-type=module' , '-e' , source ] ,
60- { cwd, env, allowFailure : true } ,
61- ) ;
62- let commandPid : number | undefined ;
63- let waiter : ReturnType < typeof runCmdBackground > | undefined ;
64- try {
65- const deadline = Date . now ( ) + 5000 ;
66- while ( ! fs . existsSync ( ready ) && Date . now ( ) < deadline ) await setTimeout ( 20 ) ;
67- assert . ok ( fs . existsSync ( ready ) ) ;
68- commandPid = (
69- JSON . parse (
70- fs . readFileSync (
71- path . join ( cwd , '.cache' , 'agent-device' , 'validation.lock.active.json' ) ,
72- 'utf8' ,
73- ) ,
74- ) as { pid : number }
75- ) . pid ;
76- parent . child . kill ( 'SIGKILL' ) ;
77- await parent . wait ;
78- waiter = runCmdBackground (
64+ const parent = runCmdBackground (
7965 process . execPath ,
80- [
81- '--experimental-strip-types' ,
82- '--input-type=module' ,
83- '-e' ,
84- `
66+ [ '--experimental-strip-types' , '--input-type=module' , '-e' , source ] ,
67+ { cwd, env, allowFailure : true } ,
68+ ) ;
69+ let commandPid : number | undefined ;
70+ let waiter : ReturnType < typeof runCmdBackground > | undefined ;
71+ try {
72+ const deadline = Date . now ( ) + 5000 ;
73+ while ( ! fs . existsSync ( ready ) && Date . now ( ) < deadline ) await setTimeout ( 20 ) ;
74+ assert . ok ( fs . existsSync ( ready ) ) ;
75+ commandPid = (
76+ JSON . parse (
77+ fs . readFileSync (
78+ path . join ( cwd , '.cache' , 'agent-device' , 'validation.lock.active.json' ) ,
79+ 'utf8' ,
80+ ) ,
81+ ) as { pid : number }
82+ ) . pid ;
83+ parent . child . kill ( 'SIGKILL' ) ;
84+ await parent . wait ;
85+ waiter = runCmdBackground (
86+ process . execPath ,
87+ [
88+ '--experimental-strip-types' ,
89+ '--input-type=module' ,
90+ '-e' ,
91+ `
8592 import { runValidation } from ${ JSON . stringify ( entry ) } ;
8693 process.exitCode = await runValidation(${ JSON . stringify ( cwd ) } , {base:'HEAD',head:'HEAD',json:true}, async () => 0);
8794 ` ,
88- ] ,
89- { cwd, env, allowFailure : true } ,
90- ) ;
91- let observedWait = false ;
92- waiter . child . stderr ?. on ( 'data' , ( data ) => {
93- if ( String ( data ) . includes ( '[wait] orphan' ) ) observedWait = true ;
94- } ) ;
95- const waitDeadline = Date . now ( ) + 5000 ;
96- while ( ! observedWait && Date . now ( ) < waitDeadline ) await setTimeout ( 20 ) ;
97- assert . equal ( observedWait , true , 'waiter must observe surviving command before acquiring' ) ;
98- process . kill ( - commandPid , 'SIGTERM' ) ;
99- const result = await waiter . wait ;
100- assert . equal ( result . exitCode , 0 , result . stderr ) ;
101- assert . equal ( JSON . parse ( result . stdout ) . status , 'passed' ) ;
102- } finally {
103- parent . child . kill ( 'SIGKILL' ) ;
104- waiter ?. child . kill ( 'SIGTERM' ) ;
105- if ( commandPid ) {
106- try {
107- process . kill ( - commandPid , 'SIGKILL' ) ;
108- } catch { }
95+ ] ,
96+ { cwd, env, allowFailure : true } ,
97+ ) ;
98+ let observedWait = false ;
99+ waiter . child . stderr ?. on ( 'data' , ( data ) => {
100+ if ( String ( data ) . includes ( '[wait] orphan' ) ) observedWait = true ;
101+ } ) ;
102+ const waitDeadline = Date . now ( ) + 5000 ;
103+ while ( ! observedWait && Date . now ( ) < waitDeadline ) await setTimeout ( 20 ) ;
104+ assert . equal ( observedWait , true , 'waiter must observe surviving command before acquiring' ) ;
105+ process . kill ( - commandPid , 'SIGTERM' ) ;
106+ const result = await waiter . wait ;
107+ assert . equal ( result . exitCode , 0 , result . stderr ) ;
108+ assert . equal ( JSON . parse ( result . stdout ) . status , 'passed' ) ;
109+ } finally {
110+ parent . child . kill ( 'SIGKILL' ) ;
111+ waiter ?. child . kill ( 'SIGTERM' ) ;
112+ if ( commandPid ) {
113+ try {
114+ process . kill ( - commandPid , 'SIGKILL' ) ;
115+ } catch { }
116+ }
117+ await parent . wait ;
118+ await waiter ?. wait ;
119+ fs . rmSync ( cwd , { recursive : true , force : true } ) ;
109120 }
110- await parent . wait ;
111- await waiter ?. wait ;
112- fs . rmSync ( cwd , { recursive : true , force : true } ) ;
113- }
114- } ) ;
121+ } ,
122+ ) ;
0 commit comments