@@ -6,8 +6,9 @@ import { afterEach, describe, expect, it, vi } from "vitest";
66import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
77import { NODE_WORKER_CAPACITY_EXHAUSTED_ERROR_CODE } from "../infra/node-commands.js" ;
88import { NODE_WORKER_CAPACITY_MAX } from "../infra/node-runner-inventory.js" ;
9- import { registerSecretValueForRedaction } from "../logging/secret-redaction-registry.js" ;
9+ import * as secretRegistry from "../logging/secret-redaction-registry.js" ;
1010import { resetSecretRedactionRegistryForTest } from "../logging/secret-redaction-registry.test-support.js" ;
11+ import * as processTree from "../process/kill-tree.js" ;
1112import {
1213 closeOpenClawStateDatabaseForTest ,
1314 openOpenClawStateDatabase ,
@@ -55,6 +56,19 @@ function launchInput(workspaceDir: string, launchId: string, prompt = "success")
5556 return input ;
5657}
5758
59+ function evictWorkerCredentialsOnRegistration ( ) {
60+ const { registerSecretValueForRedaction } = secretRegistry ;
61+ // Both launch paths register before sending a turn. Evict every registration so a
62+ // later launch cannot restore the global secret and hide a missing worker scrubber.
63+ return vi . spyOn ( secretRegistry , "registerSecretValueForRedaction" ) . mockImplementation ( ( value ) => {
64+ registerSecretValueForRedaction ( value ) ;
65+ for ( let index = 0 ; index < 600 ; index += 1 ) {
66+ registerSecretValueForRedaction ( `eviction-secret-${ index } ` ) ;
67+ }
68+ expect ( secretRegistry . isSecretValueRegisteredForRedaction ( value ) ) . toBe ( false ) ;
69+ } ) ;
70+ }
71+
5872describe ( "node worker supervisor" , ( ) => {
5973 it . each ( [
6074 { availableParallelism : 0 , expected : 1 } ,
@@ -531,12 +545,12 @@ describe("node worker supervisor", () => {
531545 const failureInput = launchInput ( workspaceDir , "failure-launch" , "secret-fail" ) ;
532546 const overflowInput = launchInput ( workspaceDir , "overflow-launch" , "overflow" ) ;
533547
548+ const registrations = evictWorkerCredentialsOnRegistration ( ) ;
534549 await supervisor . launch ( successInput , TEST_WORKER_ENDPOINT ) ;
535550 await supervisor . launch ( failureInput , TEST_WORKER_ENDPOINT ) ;
536551 await supervisor . launch ( overflowInput , TEST_WORKER_ENDPOINT ) ;
537- for ( let index = 0 ; index < 600 ; index += 1 ) {
538- registerSecretValueForRedaction ( `eviction-secret-${ index } ` ) ;
539- }
552+ expect ( registrations ) . toHaveBeenCalledTimes ( 3 ) ;
553+ expect ( registrations ) . toHaveBeenCalledWith ( TEST_WORKER_CREDENTIAL ) ;
540554 const success = await waitForTerminal ( supervisor , successInput . launchId ) ;
541555 const failure = await waitForTerminal ( supervisor , failureInput . launchId ) ;
542556 const overflow = await waitForTerminal ( supervisor , overflowInput . launchId ) ;
@@ -595,19 +609,19 @@ describe("node worker supervisor", () => {
595609 try {
596610 const original = await supervisor . launch ( first , TEST_WORKER_ENDPOINT ) ;
597611 await waitForTerminal ( supervisor , first . launchId ) ;
612+ const registrations = evictWorkerCredentialsOnRegistration ( ) ;
598613 expect ( await supervisor . launch ( second , TEST_WORKER_ENDPOINT ) ) . toMatchObject ( {
599614 worker : original . worker ,
600615 } ) ;
601- for ( let index = 0 ; index < 600 ; index += 1 ) {
602- registerSecretValueForRedaction ( `rotated-eviction-secret-${ index } ` ) ;
603- }
616+ expect ( registrations ) . toHaveBeenCalledWith ( second . descriptor . admission . credential ) ;
604617 const completed = await waitForTerminal ( supervisor , second . launchId ) ;
605618 expect ( JSON . parse ( completed . resultJson ?? "null" ) ) . toEqual ( {
606619 status : "completed" ,
607620 transcriptLeafId : "raw [REDACTED] encoded [REDACTED]" ,
608621 transcriptNextSeq : 2 ,
609622 } ) ;
610623
624+ registrations . mockRestore ( ) ;
611625 await supervisor . launch ( last , TEST_WORKER_ENDPOINT ) ;
612626 const failed = await waitForTerminal ( supervisor , last . launchId ) ;
613627 expect ( failed ) . toMatchObject ( {
@@ -623,10 +637,21 @@ describe("node worker supervisor", () => {
623637 } ) ;
624638
625639 it ( "does not open or signal a child after markRunning observes its terminal receipt" , async ( ) => {
626- const { supervisor, workspaceDir } = fixture ( ) ;
640+ const capacities : Array < { total : number ; available : number } > = [ ] ;
641+ const { supervisor, workspaceDir, env } = fixture ( {
642+ capacity : 1 ,
643+ onCapacityChanged : ( capacity ) => capacities . push ( capacity ) ,
644+ } ) ;
627645 const input = launchInput ( workspaceDir , "fast-terminal-launch" , "fast-terminal" ) ;
646+ const captureSpawn = vi . spyOn ( childProcess . ChildProcess . prototype , "emit" ) ;
647+ const signalTree = vi . spyOn ( processTree , "signalProcessTree" ) ;
648+ let child : ChildProcess | undefined ;
628649 vi . spyOn ( NodeWorkerLaunchStore . prototype , "markRunning" ) . mockImplementation (
629650 function ( this : NodeWorkerLaunchStore , params ) {
651+ child = captureSpawn . mock . contexts . find (
652+ ( context ) : context is ChildProcess =>
653+ context instanceof childProcess . ChildProcess && context . pid === params . worker . pid ,
654+ ) ;
630655 return this . finish ( {
631656 launchId : params . launchId ,
632657 planHash : params . planHash ,
@@ -638,15 +663,25 @@ describe("node worker supervisor", () => {
638663 } ,
639664 ) ;
640665
641- expect ( await supervisor . launch ( input , TEST_WORKER_ENDPOINT ) ) . toMatchObject ( {
642- state : "completed" ,
643- } ) ;
644- const marker = path . join ( workspaceDir , "fast-terminal-marker" ) ;
645- await new Promise ( ( resolve ) => {
646- setTimeout ( resolve , 150 ) ;
647- } ) ;
648- expect ( fs . existsSync ( marker ) ) . toBe ( false ) ;
649- await supervisor . close ( ) ;
666+ try {
667+ expect ( await supervisor . launch ( input , TEST_WORKER_ENDPOINT ) ) . toMatchObject ( {
668+ state : "completed" ,
669+ } ) ;
670+ // Capacity returns only after the real child exit and adapter settlement.
671+ await vi . waitFor ( ( ) => expect ( capacities . at ( - 1 ) ) . toEqual ( { total : 1 , available : 1 } ) , {
672+ timeout : 5_000 ,
673+ } ) ;
674+ expect ( child ?. exitCode ) . toBe ( 0 ) ;
675+ expect ( child ?. signalCode ) . toBeNull ( ) ;
676+ expect ( child ?. stdout ?. closed ) . toBe ( true ) ;
677+ expect ( child ?. stderr ?. closed ) . toBe ( true ) ;
678+ expect ( fs . existsSync ( path . join ( workspaceDir , "fast-terminal-marker" ) ) ) . toBe ( false ) ;
679+ expect ( new NodeWorkerLaunchStore ( { env } ) . get ( input . launchId ) ?. state ) . toBe ( "completed" ) ;
680+ await supervisor . close ( ) ;
681+ expect ( signalTree ) . not . toHaveBeenCalled ( ) ;
682+ } finally {
683+ await supervisor . close ( ) ;
684+ }
650685 } ) ;
651686
652687 it ( "records a gated child that exits before journal readiness as terminal" , async ( ) => {
@@ -682,6 +717,7 @@ describe("node worker supervisor", () => {
682717 const input = launchInput ( workspaceDir , "blocked-cancel" , "wait" ) ;
683718 const sibling = launchInput ( workspaceDir , "unrelated-worker" , "wait" ) ;
684719 const captureSpawn = vi . spyOn ( childProcess . ChildProcess . prototype , "emit" ) ;
720+ const signalTree = vi . spyOn ( processTree , "signalProcessTree" ) ;
685721 let heldWrite : { data : unknown ; callback ?: ( error ?: Error | null ) => void } | undefined ;
686722 let restoreWrite : ( ( ) => void ) | undefined ;
687723 let cancellation : ReturnType < NodeWorkerSupervisor [ "cancel" ] > | undefined ;
@@ -704,17 +740,29 @@ describe("node worker supervisor", () => {
704740 return false ;
705741 } ) ;
706742 restoreWrite = ( ) => write . mockRestore ( ) ;
743+ vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" ] } ) ;
707744 cancellation = supervisor . cancel ( testNodeWorkerLaunchIdentity ( input ) ) . then ( ( receipt ) => {
708745 cancelled = receipt ;
709746 return receipt ;
710747 } ) ;
711- await vi . waitFor ( ( ) => {
712- expect ( heldWrite ?. data ) . toBe (
713- `${ JSON . stringify ( { type : "cancel" , turnId : input . launchId } ) } \n` ,
714- ) ;
715- expect ( heldWrite ?. callback ) . toEqual ( expect . any ( Function ) ) ;
716- } ) ;
748+ await vi . advanceTimersByTimeAsync ( 0 ) ;
749+ expect ( heldWrite ?. data ) . toBe (
750+ `${ JSON . stringify ( { type : "cancel" , turnId : input . launchId } ) } \n` ,
751+ ) ;
752+ expect ( heldWrite ?. callback ) . toEqual ( expect . any ( Function ) ) ;
753+
754+ await vi . advanceTimersByTimeAsync ( 4_999 ) ;
755+ expect ( signalTree ) . not . toHaveBeenCalled ( ) ;
756+ expect ( cancelled ) . toBeUndefined ( ) ;
757+ expect ( new NodeWorkerLaunchStore ( { env } ) . get ( input . launchId ) ?. state ) . toBe ( "running" ) ;
758+ expect ( inspectNodeWorkerProcessIdentity ( running . worker ! ) ) . toBe ( "live" ) ;
759+ expect ( inspectNodeWorkerProcessIdentity ( unrelated . worker ! ) ) . toBe ( "live" ) ;
760+ expect ( capacities . at ( - 1 ) ) . toEqual ( { total : 2 , available : 0 } ) ;
717761
762+ // Fire only the blocked-write deadline. Restore real timers before its
763+ // rejection continuation schedules process termination and escalation.
764+ vi . advanceTimersByTime ( 1 ) ;
765+ vi . useRealTimers ( ) ;
718766 await vi . waitFor (
719767 ( ) => {
720768 expect ( cancelled ) . toMatchObject ( { state : "cancelled" , worker : running . worker } ) ;
@@ -733,6 +781,7 @@ describe("node worker supervisor", () => {
733781 ) ,
734782 ) . resolves . toMatchObject ( { state : "running" } ) ;
735783 } finally {
784+ vi . useRealTimers ( ) ;
736785 captureSpawn . mockRestore ( ) ;
737786 restoreWrite ?.( ) ;
738787 // Release the injected write even on the pre-fix failure, so cleanup cannot inherit its hang.
0 commit comments