@@ -74,6 +74,8 @@ function harness(
7474 timeout ?: ( ) => void
7575 onPause ?: ( count : number ) => void
7676 disconnectOnPause ?: number
77+ onLaunch ?: ( ) => void
78+ onReady ?: ( ) => void
7779 } = { } ,
7880) {
7981 let now = 0
@@ -87,6 +89,8 @@ function harness(
8789 let disconnected = false
8890 let unsubscribed = false
8991 let launched = false
92+ let connected = false
93+ let readyAnnounced = false
9094 let workspaceFiles : readonly { path : string ; content : string } [ ] = [ ]
9195 const commands : TaskCommand [ ] = [ ]
9296 const signals : NodeJS . Signals [ ] = [ ]
@@ -107,6 +111,7 @@ function harness(
107111 launched = true
108112 workspaceFiles = files
109113 if ( scenario === "setup" ) throw new Error ( "private setup detail" )
114+ options . onLaunch ?.( )
110115 return {
111116 get exited ( ) {
112117 return exited
@@ -121,8 +126,15 @@ function harness(
121126 }
122127 } ,
123128 connect ( ) {
129+ connected = true
124130 return {
125- isReady : scenario !== "activation" ,
131+ get isReady ( ) {
132+ if ( ! readyAnnounced && scenario !== "activation" ) {
133+ readyAnnounced = true
134+ options . onReady ?.( )
135+ }
136+ return scenario !== "activation"
137+ } ,
126138 onTaskEvent ( listener ) {
127139 onEvent = listener
128140 } ,
@@ -253,6 +265,9 @@ function harness(
253265 get launched ( ) {
254266 return launched
255267 } ,
268+ get connected ( ) {
269+ return connected
270+ } ,
256271 get workspaceFiles ( ) {
257272 return workspaceFiles
258273 } ,
@@ -343,6 +358,86 @@ describe("agent fixture controller", () => {
343358 assert . equal ( accepted . length , 3 )
344359 } )
345360
361+ it ( "reports a rejected task start without replay, and still tears down cleanly" , async ( ) => {
362+ registerTestFixture ( "controller-start-failure" )
363+ const f = harness ( "start-failure" )
364+ f . emit ( { eventName : RooCodeEventName . TaskStarted , payload : [ "root" ] } )
365+ const result = await runAgentFixture ( f . runtime , {
366+ fixtureName : "controller-start-failure" ,
367+ credential : CREDENTIAL ,
368+ } )
369+ assert . equal ( result . execution . code , "TASK_START_FAILED" )
370+ assert . equal ( result . execution . stopReason , "startNotAccepted" )
371+ assert . equal ( result . execution . activated , true )
372+ assert . equal ( result . execution . taskAccepted , false )
373+ const trace = writtenTrace ( f )
374+ assert . equal ( trace . eventFlow . bufferedBeforeAck , 1 )
375+ assert . equal ( trace . eventFlow . replayedAfterAck , 0 )
376+ assert . ok ( trace . accounting . completeness . reasons . includes ( "taskNotAccepted" ) )
377+ assert . equal ( result . teardown . status , "ok" )
378+ assert . equal ( result . teardown . hostExited , true )
379+ assert . equal ( result . teardown . cleanup , "done" )
380+ assert . ok ( f . cleaned && f . disconnected && f . unsubscribed )
381+ } )
382+
383+ it ( "never connects or starts a task when the interrupt lands during launch" , async ( ) => {
384+ registerTestFixture ( "controller-launch-interrupt" )
385+ const f = harness ( "success" , { onLaunch : ( ) => f . interrupt ( ) } )
386+ const result = await runAgentFixture ( f . runtime , {
387+ fixtureName : "controller-launch-interrupt" ,
388+ credential : CREDENTIAL ,
389+ } )
390+ assert . equal ( f . launched , true )
391+ assert . equal ( f . connected , false )
392+ assert . equal ( f . commands . length , 0 )
393+ assert . equal ( result . execution . code , "SETUP_FAILED" )
394+ assert . equal ( result . execution . activated , false )
395+ assert . equal ( result . execution . taskAccepted , false )
396+ assert . deepEqual ( f . signals , [ "SIGTERM" ] )
397+ assert . equal ( result . teardown . status , "ok" )
398+ assert . equal ( result . teardown . hostExited , true )
399+ assert . equal ( result . teardown . cleanup , "done" )
400+ assert . ok ( f . cleaned && f . unsubscribed )
401+ } )
402+
403+ it ( "stops before the start command when the interrupt lands after activation" , async ( ) => {
404+ registerTestFixture ( "controller-activation-interrupt" )
405+ const f = harness ( "success" , { onReady : ( ) => f . interrupt ( ) } )
406+ const result = await runAgentFixture ( f . runtime , {
407+ fixtureName : "controller-activation-interrupt" ,
408+ credential : CREDENTIAL ,
409+ } )
410+ assert . equal ( result . execution . activated , true )
411+ assert . equal ( result . execution . taskAccepted , false )
412+ assert . equal ( result . execution . code , "SETUP_FAILED" )
413+ assert . ok ( ! f . commands . some ( ( command ) => command . commandName === TaskCommandName . StartNewTask ) )
414+ assert . deepEqual ( f . signals , [ "SIGTERM" ] )
415+ assert . equal ( result . teardown . status , "ok" )
416+ assert . equal ( result . teardown . hostExited , true )
417+ assert . ok ( f . cleaned && f . disconnected && f . unsubscribed )
418+ } )
419+
420+ it ( "drops exactly one event past the pre-ack buffer limit and records the overflow caveat" , async ( ) => {
421+ registerTestFixture ( "controller-overflow" )
422+ const f = harness ( "no-events" )
423+ for ( let index = 0 ; index < 257 ; index ++ ) f . emit ( { eventName : RooCodeEventName . TaskStarted , payload : [ "root" ] } )
424+ const result = await runAgentFixture ( f . runtime , {
425+ fixtureName : "controller-overflow" ,
426+ credential : CREDENTIAL ,
427+ observers : { shouldStop : ( ) => true } ,
428+ } )
429+ assert . equal ( result . execution . code , "STOPPED" )
430+ assert . equal ( result . execution . taskAccepted , true )
431+ const trace = writtenTrace ( f )
432+ assert . equal ( trace . eventFlow . observed , 258 )
433+ assert . equal ( trace . eventFlow . eventBufferLimit , 256 )
434+ assert . equal ( trace . eventFlow . bufferedBeforeAck , 256 )
435+ assert . equal ( trace . eventFlow . replayedAfterAck , 256 )
436+ assert . equal ( trace . eventFlow . bufferOverflow , true )
437+ assert . equal ( trace . eventFlow . droppedByOverflow , 1 )
438+ assert . ok ( trace . accounting . completeness . reasons . includes ( "eventBufferOverflow" ) )
439+ } )
440+
346441 it ( "bounds the run timeout, the activation wait, and the usage drain" , async ( ) => {
347442 registerTestFixture ( "controller-fast" , {
348443 limits : {
0 commit comments