@@ -7,6 +7,7 @@ import { setTimeout as sleep } from "node:timers/promises";
77import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime" ;
88import { redactSensitiveText } from "openclaw/plugin-sdk/logging-core" ;
99import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path" ;
10+ import { createQaPosixCommandSettlement } from "../../../posix-command-settlement.js" ;
1011import {
1112 killMatrixQaCliChild ,
1213 resolveMatrixQaOpenClawCliEntryPath ,
@@ -100,20 +101,6 @@ function formatMatrixQaCliTimeoutError(result: MatrixQaCliRunResult, timeoutMs:
100101 . join ( "\n" ) ;
101102}
102103
103- function isMatrixQaCliChildProcessGroupRunning (
104- child : ReturnType < typeof startOpenClawCliProcess > ,
105- ) : boolean {
106- if ( process . platform === "win32" || ! child . pid ) {
107- return false ;
108- }
109- try {
110- process . kill ( - child . pid , 0 ) ;
111- return true ;
112- } catch {
113- return false ;
114- }
115- }
116-
117104export function startMatrixQaOpenClawCli ( params : {
118105 allowNonZero ?: boolean ;
119106 args : string [ ] ;
@@ -129,11 +116,6 @@ export function startMatrixQaOpenClawCli(params: {
129116 let closed = false ;
130117 let closeError : Error | undefined ;
131118 let closeResult : MatrixQaCliRunResult | undefined ;
132- let killRequested = false ;
133- let timedOut = false ;
134- let forceKillTimeout : NodeJS . Timeout | undefined ;
135- let forceSettleTimeout : NodeJS . Timeout | undefined ;
136- let streamFailure : Error | undefined ;
137119 let settleWait :
138120 | {
139121 reject : ( error : Error ) => void ;
@@ -167,120 +149,67 @@ export function startMatrixQaOpenClawCli(params: {
167149 settleWait . resolve ( result ) ;
168150 }
169151 } ;
170- const finishTimeout = ( result : MatrixQaCliRunResult ) => {
171- finish ( result , new Error ( formatMatrixQaCliTimeoutError ( result , params . timeoutMs ) ) ) ;
172- } ;
173- const finishResult = ( result : MatrixQaCliRunResult ) => {
174- if ( result . exitCode !== 0 && params . allowNonZero !== true ) {
175- finish ( result , new Error ( formatMatrixQaCliExitError ( result ) ) ) ;
176- return ;
177- }
178- finish ( result ) ;
179- } ;
180- const clearForcedTimeouts = ( ) => {
181- if ( forceKillTimeout ) {
182- clearTimeout ( forceKillTimeout ) ;
183- forceKillTimeout = undefined ;
184- }
185- if ( forceSettleTimeout ) {
186- clearTimeout ( forceSettleTimeout ) ;
187- forceSettleTimeout = undefined ;
188- }
189- } ;
190- const finishForcedCleanup = ( result : MatrixQaCliRunResult ) => {
191- if ( timedOut ) {
192- finishTimeout ( result ) ;
193- return ;
194- }
195- if ( streamFailure ) {
196- finish ( result , streamFailure ) ;
197- return ;
198- }
199- finishResult ( result ) ;
200- } ;
201- const scheduleForcedCleanup = ( ) => {
202- if ( forceKillTimeout || forceSettleTimeout ) {
203- return ;
204- }
205- forceKillTimeout = setTimeout ( ( ) => {
206- forceKillTimeout = undefined ;
207- killMatrixQaCliChild ( child , "SIGKILL" ) ;
208- forceSettleTimeout = setTimeout ( ( ) => {
209- forceSettleTimeout = undefined ;
210- finishForcedCleanup (
211- buildMatrixQaCliResult ( {
212- args : params . args ,
213- exitCode : 1 ,
214- output : readOutput ( ) ,
215- } ) ,
216- ) ;
217- } , MATRIX_QA_CLI_TIMEOUT_FORCE_SETTLE_MS ) ;
218- } , MATRIX_QA_CLI_TIMEOUT_KILL_GRACE_MS ) ;
219- } ;
220-
221- const timeout = setTimeout ( ( ) => {
222- timedOut = true ;
223- killMatrixQaCliChild ( child , "SIGTERM" ) ;
224- scheduleForcedCleanup ( ) ;
225- } , params . timeoutMs ) ;
226- const handleStreamError = ( stream : "stderr" | "stdout" , error : Error ) => {
227- if ( closed || timedOut || killRequested ) {
228- return ;
229- }
230- clearTimeout ( timeout ) ;
231- killRequested = true ;
232- streamFailure = new Error ( `${ stream } stream error: ${ formatErrorMessage ( error ) } ` , {
233- cause : error ,
234- } ) ;
235- // Keep stream failures on the normal kill path so detached descendants are gone
236- // before the session reports the parent-side pipe failure.
237- killMatrixQaCliChild ( child , "SIGTERM" ) ;
238- scheduleForcedCleanup ( ) ;
239- } ;
240-
241- child . stdout . on ( "data" , ( chunk ) => stdout . push ( Buffer . from ( chunk ) ) ) ;
242- child . stdout . on ( "error" , ( error ) => handleStreamError ( "stdout" , error ) ) ;
243- child . stderr . on ( "data" , ( chunk ) => stderr . push ( Buffer . from ( chunk ) ) ) ;
244- child . stderr . on ( "error" , ( error ) => handleStreamError ( "stderr" , error ) ) ;
245- if ( params . stdin !== undefined ) {
246- child . stdin . end ( params . stdin ) ;
247- }
248- child . on ( "error" , ( error ) => {
249- if ( streamFailure ) {
250- // Forced cleanup owns settlement after a stream failure. Finishing here could
251- // reject before detached descendants are proven gone.
252- return ;
253- }
254- clearTimeout ( timeout ) ;
255- clearForcedTimeouts ( ) ;
256- finish (
257- buildMatrixQaCliResult ( {
152+ const isWindows = process . platform === "win32" ;
153+ const settlement = createQaPosixCommandSettlement ( {
154+ child,
155+ settlementFailureMessage : `${ formatMatrixQaCliCommand ( params . args ) } settlement failed` ,
156+ forceKillAfterMs : MATRIX_QA_CLI_TIMEOUT_KILL_GRACE_MS ,
157+ initialSignal : "SIGTERM" ,
158+ ...( isWindows
159+ ? {
160+ windowsCleanup : {
161+ closeCompletesCleanup : true ,
162+ signal : ( signal : NodeJS . Signals ) => {
163+ try {
164+ killMatrixQaCliChild ( child , signal ) ;
165+ return undefined ;
166+ } catch ( error ) {
167+ return error instanceof Error ? error : new Error ( String ( error ) ) ;
168+ }
169+ } ,
170+ } ,
171+ }
172+ : { } ) ,
173+ executionTimeoutMs : params . timeoutMs ,
174+ onSettled : ( outcome ) => {
175+ const primary = outcome . primary ;
176+ const result = buildMatrixQaCliResult ( {
258177 args : params . args ,
259- exitCode : 1 ,
178+ exitCode : primary . type === "exit" ? ( primary . exitCode ?? 1 ) : 1 ,
260179 output : readOutput ( ) ,
261- } ) ,
262- error ,
263- ) ;
264- } ) ;
265- child . on ( "close" , ( exitCode ) => {
266- clearTimeout ( timeout ) ;
267- const result = buildMatrixQaCliResult ( {
268- args : params . args ,
269- exitCode : exitCode ?? 1 ,
270- output : readOutput ( ) ,
271- } ) ;
272- if ( timedOut || killRequested ) {
273- // A closed parent is not proof that detached, ignored-stdio descendants are gone.
274- if ( isMatrixQaCliChildProcessGroupRunning ( child ) ) {
275- return ;
276- }
277- clearForcedTimeouts ( ) ;
278- finishForcedCleanup ( result ) ;
279- return ;
280- }
281- clearForcedTimeouts ( ) ;
282- finishResult ( result ) ;
180+ } ) ;
181+ const primaryError =
182+ primary . type === "spawn-error"
183+ ? primary . error
184+ : primary . type === "stream-error"
185+ ? new Error ( `${ primary . stream } stream error: ${ formatErrorMessage ( primary . error ) } ` , {
186+ cause : primary . error ,
187+ } )
188+ : primary . type === "timeout"
189+ ? new Error ( formatMatrixQaCliTimeoutError ( result , params . timeoutMs ) )
190+ : result . exitCode !== 0 && params . allowNonZero !== true
191+ ? new Error ( formatMatrixQaCliExitError ( result ) )
192+ : undefined ;
193+ finish (
194+ result ,
195+ outcome . settlementFailure
196+ ? primaryError
197+ ? new AggregateError (
198+ [ primaryError , outcome . settlementFailure ] ,
199+ "Matrix QA CLI command and settlement failed" ,
200+ )
201+ : outcome . settlementFailure
202+ : primaryError ,
203+ ) ;
204+ } ,
205+ onStderrData : ( chunk ) => stderr . push ( Buffer . from ( chunk ) ) ,
206+ onStdoutData : ( chunk ) => stdout . push ( Buffer . from ( chunk ) ) ,
207+ processGroupId : isWindows ? undefined : child . pid ,
208+ verifyAfterMs : MATRIX_QA_CLI_TIMEOUT_FORCE_SETTLE_MS ,
283209 } ) ;
210+ if ( params . stdin !== undefined ) {
211+ child . stdin . end ( params . stdin ) ;
212+ }
284213
285214 return {
286215 args : params . args ,
@@ -335,10 +264,7 @@ export function startMatrixQaOpenClawCli(params: {
335264 } ,
336265 kill : ( ) => {
337266 if ( ! closed ) {
338- clearTimeout ( timeout ) ;
339- killRequested = true ;
340- killMatrixQaCliChild ( child , "SIGTERM" ) ;
341- scheduleForcedCleanup ( ) ;
267+ settlement . requestCleanup ( ) ;
342268 }
343269 } ,
344270 } ;
0 commit comments