22// termination semantics used by agent sessions.
33import { EventEmitter } from "node:events" ;
44import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { withMockedPlatform } from "../../test-utils/vitest-spies.js" ;
56
6- const { spawnMock, waitForChildProcessMock } = vi . hoisted ( ( ) => ( {
7+ const { killProcessTreeMock, spawnMock, waitForChildProcessMock } = vi . hoisted ( ( ) => ( {
8+ killProcessTreeMock : vi . fn ( ) ,
79 spawnMock : vi . fn ( ) ,
810 waitForChildProcessMock : vi . fn ( ) ,
911} ) ) ;
@@ -16,8 +18,13 @@ vi.mock("../utils/child-process.js", () => ({
1618 waitForChildProcess : waitForChildProcessMock ,
1719} ) ) ;
1820
21+ vi . mock ( "../../process/kill-tree.js" , ( ) => ( {
22+ killProcessTree : killProcessTreeMock ,
23+ } ) ) ;
24+
1925type StubChild = EventEmitter & {
2026 kill : ReturnType < typeof vi . fn > ;
27+ pid ?: number ;
2128 stderr : EventEmitter ;
2229 stdout : EventEmitter ;
2330} ;
@@ -42,6 +49,7 @@ function createDeferred<T>() {
4249
4350describe ( "execCommand" , ( ) => {
4451 beforeEach ( ( ) => {
52+ killProcessTreeMock . mockReset ( ) ;
4553 spawnMock . mockReset ( ) ;
4654 waitForChildProcessMock . mockReset ( ) ;
4755 vi . useRealTimers ( ) ;
@@ -93,8 +101,37 @@ describe("execCommand", () => {
93101 expect ( result . stdoutTruncatedChars ) . toBe ( 3 ) ;
94102 } ) ;
95103
104+ it ( "spawns with a process group when the platform supports tree signaling" , async ( ) => {
105+ const { execCommand } = await import ( "./exec.js" ) ;
106+
107+ for ( const [ platform , expectedDetached ] of [
108+ [ "linux" , true ] ,
109+ [ "win32" , false ] ,
110+ ] as const ) {
111+ await withMockedPlatform ( platform , async ( ) => {
112+ const child = createStubChild ( ) ;
113+ const wait = createDeferred < number | null > ( ) ;
114+ spawnMock . mockReturnValue ( child ) ;
115+ waitForChildProcessMock . mockReturnValue ( wait . promise ) ;
116+
117+ const resultPromise = execCommand ( "cmd" , [ "arg" ] , "/tmp" ) ;
118+ wait . resolve ( 0 ) ;
119+ await resultPromise ;
120+
121+ expect ( spawnMock ) . toHaveBeenLastCalledWith ( "cmd" , [ "arg" ] , {
122+ cwd : "/tmp" ,
123+ detached : expectedDetached ,
124+ shell : false ,
125+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
126+ windowsHide : true ,
127+ } ) ;
128+ } ) ;
129+ }
130+ } ) ;
131+
96132 it ( "fails instead of silently truncating default exec output" , async ( ) => {
97133 const child = createStubChild ( ) ;
134+ child . pid = 2468 ;
98135 const wait = createDeferred < number | null > ( ) ;
99136 spawnMock . mockReturnValue ( child ) ;
100137 waitForChildProcessMock . mockReturnValue ( wait . promise ) ;
@@ -105,7 +142,8 @@ describe("execCommand", () => {
105142 wait . resolve ( 0 ) ;
106143
107144 const result = await resultPromise ;
108- expect ( child . kill ) . toHaveBeenCalledWith ( "SIGTERM" ) ;
145+ expect ( killProcessTreeMock ) . toHaveBeenCalledWith ( 2468 , { graceMs : 5000 } ) ;
146+ expect ( child . kill ) . not . toHaveBeenCalled ( ) ;
109147 expect ( result . code ) . toBe ( 1 ) ;
110148 expect ( result . killed ) . toBe ( true ) ;
111149 expect ( result . outputLimitExceeded ) . toBe ( "stdout" ) ;
@@ -114,7 +152,47 @@ describe("execCommand", () => {
114152 expect ( result . stderr ) . toContain ( "exec stdout exceeded output limit" ) ;
115153 } ) ;
116154
117- it ( "escalates timed-out commands to SIGKILL after the grace period" , async ( ) => {
155+ it ( "kills the process tree when commands time out" , async ( ) => {
156+ vi . useFakeTimers ( ) ;
157+ const child = createStubChild ( ) ;
158+ child . pid = 1357 ;
159+ const wait = createDeferred < number | null > ( ) ;
160+ spawnMock . mockReturnValue ( child ) ;
161+ waitForChildProcessMock . mockReturnValue ( wait . promise ) ;
162+ const { execCommand } = await import ( "./exec.js" ) ;
163+
164+ const resultPromise = execCommand ( "cmd" , [ ] , "/tmp" , { timeout : 10 } ) ;
165+ await vi . advanceTimersByTimeAsync ( 10 ) ;
166+
167+ expect ( killProcessTreeMock ) . toHaveBeenCalledWith ( 1357 , { graceMs : 5000 } ) ;
168+ expect ( child . kill ) . not . toHaveBeenCalled ( ) ;
169+
170+ wait . resolve ( null ) ;
171+ const result = await resultPromise ;
172+ expect ( result . killed ) . toBe ( true ) ;
173+ } ) ;
174+
175+ it ( "kills the process tree when aborted" , async ( ) => {
176+ const child = createStubChild ( ) ;
177+ child . pid = 9753 ;
178+ const wait = createDeferred < number | null > ( ) ;
179+ const controller = new AbortController ( ) ;
180+ spawnMock . mockReturnValue ( child ) ;
181+ waitForChildProcessMock . mockReturnValue ( wait . promise ) ;
182+ const { execCommand } = await import ( "./exec.js" ) ;
183+
184+ const resultPromise = execCommand ( "cmd" , [ ] , "/tmp" , { signal : controller . signal } ) ;
185+ controller . abort ( ) ;
186+
187+ expect ( killProcessTreeMock ) . toHaveBeenCalledWith ( 9753 , { graceMs : 5000 } ) ;
188+ expect ( child . kill ) . not . toHaveBeenCalled ( ) ;
189+
190+ wait . resolve ( null ) ;
191+ const result = await resultPromise ;
192+ expect ( result . killed ) . toBe ( true ) ;
193+ } ) ;
194+
195+ it ( "falls back to direct kill when timed-out commands have no pid" , async ( ) => {
118196 // SIGTERM gives child processes a chance to clean up; SIGKILL is the
119197 // bounded fallback so an ignored signal cannot hang the session.
120198 vi . useFakeTimers ( ) ;
@@ -126,6 +204,7 @@ describe("execCommand", () => {
126204
127205 const resultPromise = execCommand ( "cmd" , [ ] , "/tmp" , { timeout : 10 } ) ;
128206 await vi . advanceTimersByTimeAsync ( 10 ) ;
207+ expect ( killProcessTreeMock ) . not . toHaveBeenCalled ( ) ;
129208 expect ( child . kill ) . toHaveBeenCalledWith ( "SIGTERM" ) ;
130209 expect ( child . kill ) . not . toHaveBeenCalledWith ( "SIGKILL" ) ;
131210
0 commit comments