@@ -6198,6 +6198,9 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
61986198 "independent-patches" ,
61996199 "expired-snooze" ,
62006200 "wake" ,
6201+ "stop" ,
6202+ "stop-before-arm" ,
6203+ "stop-before-arm-mismatched-recovery" ,
62016204 ] as const ) ( "guards a scheduled usage-limit continuation against %s" , ( scenario ) =>
62026205 Effect . gen ( function * ( ) {
62036206 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
@@ -6354,8 +6357,68 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
63546357 ] . includes ( scenario ) ;
63556358 const autoResume = scenario !== "snooze-only" && scenario !== "wake" ;
63566359 const arm = limitRecoveryCommand ( shell , autoResume , DateTime . toEpochMillis ( now ) , snooze ) ;
6357- assert . isNotNull ( arm ) ;
6358- yield * orchestrator . dispatch ( arm ! ) ;
6360+ assert ( arm ?. type === "thread.metadata.update" && arm . limitRecovery != null ) ;
6361+ const automaticArm = {
6362+ commandId : arm . commandId ,
6363+ threadId,
6364+ limitRecovery : arm . limitRecovery ,
6365+ } ;
6366+ const stopBeforeArm =
6367+ scenario === "stop-before-arm" || scenario === "stop-before-arm-mismatched-recovery" ;
6368+ if ( stopBeforeArm ) {
6369+ if ( scenario === "stop-before-arm-mismatched-recovery" ) {
6370+ const current = yield * orchestrator . getThreadProjection ( threadId ) ;
6371+ yield * events . write ( {
6372+ events : [
6373+ {
6374+ id : EventId . make ( `recovery:older-window:${ scenario } ` ) ,
6375+ type : "thread.metadata-updated" ,
6376+ threadId,
6377+ occurredAt : now ,
6378+ payload : {
6379+ ...current . thread ,
6380+ limitRecovery : {
6381+ runId : run . id ,
6382+ resetAt : DateTime . formatIso ( DateTime . add ( now , { minutes : 2 } ) ) ,
6383+ autoResume : true ,
6384+ snooze : false ,
6385+ } ,
6386+ } ,
6387+ } ,
6388+ ] ,
6389+ } ) ;
6390+ }
6391+ yield * orchestrator . dispatch ( {
6392+ type : "thread.stop" ,
6393+ commandId : CommandId . make ( `recovery:stop:${ scenario } ` ) ,
6394+ threadId,
6395+ } ) ;
6396+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
6397+ assert . isUndefined (
6398+ ( yield * projections . getLimitRecoveryCandidates ( {
6399+ now,
6400+ autoResume : true ,
6401+ snooze : false ,
6402+ } ) ) . find ( ( row ) => row . id === threadId ) ,
6403+ ) ;
6404+ const stoppedShell = ( yield * orchestrator . getShellSnapshot ( ) ) . threads . find (
6405+ ( thread ) => thread . id === threadId ,
6406+ ) ! ;
6407+ assert . isNull ( limitRecoveryCommand ( stoppedShell , true , DateTime . toEpochMillis ( now ) ) ) ;
6408+ yield * orchestrator . autoArmLimitRecovery ( automaticArm ) ;
6409+ assert . deepEqual ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . thread . limitRecovery , {
6410+ runId : run . id ,
6411+ resetAt,
6412+ autoResume : false ,
6413+ snooze : false ,
6414+ requestId : CommandId . make ( `recovery:stop:${ scenario } ` ) ,
6415+ } ) ;
6416+ }
6417+ const armCommand = stopBeforeArm
6418+ ? { ...arm , commandId : CommandId . make ( `recovery:explicit-rearm:${ scenario } ` ) }
6419+ : arm ;
6420+ if ( stopBeforeArm ) yield * orchestrator . dispatch ( armCommand ) ;
6421+ else yield * orchestrator . autoArmLimitRecovery ( automaticArm ) ;
63596422 let armedShell = ( yield * orchestrator . getShellSnapshot ( ) ) . threads . find (
63606423 ( thread ) => thread . id === threadId ,
63616424 ) ! ;
@@ -6364,7 +6427,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
63646427 resetAt,
63656428 autoResume,
63666429 snooze,
6367- requestId : arm ! . commandId ,
6430+ requestId : armCommand . commandId ,
63686431 } ) ;
63696432 if ( snooze )
63706433 assert . equal ( DateTime . toEpochMillis ( armedShell . snoozedUntil ! ) , Date . parse ( resetAt ) ) ;
@@ -6568,6 +6631,17 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
65686631 commandId : CommandId . make ( `recovery:settle:${ scenario } ` ) ,
65696632 threadId,
65706633 } ) ;
6634+ if ( scenario === "stop" ) {
6635+ yield * orchestrator . dispatch ( {
6636+ type : "thread.stop" ,
6637+ commandId : CommandId . make ( `recovery:stop:${ scenario } ` ) ,
6638+ threadId,
6639+ } ) ;
6640+ const stoppedShell = ( yield * orchestrator . getShellSnapshot ( ) ) . threads . find (
6641+ ( thread ) => thread . id === threadId ,
6642+ ) ! ;
6643+ assert . deepInclude ( stoppedShell . limitRecovery , { runId : run . id , autoResume : false } ) ;
6644+ }
65716645 if ( scenario === "replacement" ) {
65726646 const current = yield * orchestrator . getThreadProjection ( threadId ) ;
65736647 const error = current . turnItems . find ( ( item ) => item . type === "error" ) ! ;
@@ -6626,6 +6700,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
66266700 after . runs ,
66276701 before . runs . length +
66286702 ( scenario === "resume" ||
6703+ stopBeforeArm ||
66296704 scenario === "queued-resume" ||
66306705 scenario === "snooze-resume" ||
66316706 scenario === "wake-preserve-resume" ||
@@ -6638,6 +6713,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
66386713 after . messages ,
66396714 before . messages . length +
66406715 ( scenario === "resume" ||
6716+ stopBeforeArm ||
66416717 scenario === "queued-resume" ||
66426718 scenario === "snooze-resume" ||
66436719 scenario === "wake-preserve-resume" ||
@@ -6646,6 +6722,28 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
66466722 ? 1
66476723 : 0 ) ,
66486724 ) ;
6725+ if ( scenario === "stop" ) {
6726+ assert . deepInclude ( after . thread . limitRecovery , { runId : run . id , autoResume : false } ) ;
6727+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
6728+ const sweep = { now : yield * DateTime . now , autoResume : true , snooze : false } ;
6729+ assert . isUndefined (
6730+ ( yield * projections . getLimitRecoveryCandidates ( sweep ) ) . find ( ( row ) => row . id === threadId ) ,
6731+ ) ;
6732+ yield * orchestrator . dispatch ( {
6733+ type : "thread.metadata.update" ,
6734+ commandId : CommandId . make ( "recovery:rearm-after-stop" ) ,
6735+ threadId,
6736+ limitRecovery : { runId : run . id , resetAt, autoResume : true } ,
6737+ } ) ;
6738+ const rearmed = ( yield * projections . getLimitRecoveryCandidates ( sweep ) ) . find (
6739+ ( row ) => row . id === threadId ,
6740+ ) ! ;
6741+ assert . isDefined ( rearmed ) ;
6742+ const freshResume = limitRecoveryCommand ( rearmed , true , DateTime . toEpochMillis ( sweep . now ) ) ;
6743+ assert . isNotNull ( freshResume ) ;
6744+ yield * orchestrator . dispatch ( freshResume ! ) ;
6745+ assert . lengthOf ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs , 2 ) ;
6746+ }
66496747 if ( scenario === "queued-resume" ) {
66506748 const held = after . runs [ 1 ] ! ;
66516749 assert . equal ( held . status , "queued" ) ;
0 commit comments