11import { PullRequestProviderError } from "@t3tools/source-control-core/server/PullRequestProvider" ;
22import { SourceControlProviderKind } from "@t3tools/contracts" ;
33import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts" ;
4+ import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline" ;
45import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions" ;
56import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts" ;
67import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
@@ -8136,6 +8137,8 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
81368137
81378138 it . effect . each ( [
81388139 "resume" ,
8140+ "resume-started" ,
8141+ "resume-untagged" ,
81398142 "queued-resume" ,
81408143 "rearm" ,
81418144 "snooze-race" ,
@@ -8614,17 +8617,97 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
86148617 yield * orchestrator . dispatch ( resume ) ;
86158618 }
86168619 const after = yield * orchestrator . getThreadProjection ( threadId ) ;
8617- if ( scenario === "resume" ) {
8620+ if (
8621+ scenario === "resume" ||
8622+ scenario === "resume-started" ||
8623+ scenario === "resume-untagged"
8624+ ) {
86188625 assert . deepInclude ( after . messages . at ( - 1 ) , {
86198626 text : "Continue where you left off." ,
86208627 createdBy : "agent" ,
86218628 creationSource : "server" ,
86228629 } ) ;
8630+ const continuation = after . runs . at ( - 1 ) ! ;
8631+ assert . equal ( continuation . status , "starting" ) ;
8632+ if ( scenario === "resume-started" ) {
8633+ const startedAt = yield * DateTime . now ;
8634+ yield * events . write ( {
8635+ events : [
8636+ {
8637+ id : EventId . make ( "recovery:continuation-started:resume-started" ) ,
8638+ type : "run.updated" ,
8639+ threadId,
8640+ runId : continuation . id ,
8641+ occurredAt : startedAt ,
8642+ payload : { ...continuation , status : "running" , startedAt } ,
8643+ } ,
8644+ ] ,
8645+ } ) ;
8646+ }
8647+ if ( scenario === "resume-untagged" ) {
8648+ // A server older than the run tag stored the resume run without it.
8649+ const { usageLimitContinuationOfRunId : _ , ...untagged } = continuation ;
8650+ yield * events . write ( {
8651+ events : [
8652+ {
8653+ id : EventId . make ( "recovery:continuation-untagged" ) ,
8654+ type : "run.updated" ,
8655+ threadId,
8656+ runId : continuation . id ,
8657+ occurredAt : yield * DateTime . now ,
8658+ payload : untagged ,
8659+ } ,
8660+ ] ,
8661+ } ) ;
8662+ }
8663+ yield * ( yield * ProviderRuntimeRecoveryService . ProviderRuntimeRecoveryService ) . reconcile (
8664+ "startup" ,
8665+ ) ;
8666+ const restartedShell = ( yield * orchestrator . getShellSnapshot ( ) ) . threads . find (
8667+ ( thread ) => thread . id === threadId ,
8668+ ) ! ;
8669+ const retry = limitRecoveryCommand (
8670+ restartedShell ,
8671+ true ,
8672+ DateTime . toEpochMillis ( yield * DateTime . now ) ,
8673+ ) ;
8674+ if ( scenario === "resume-started" ) assert . isNull ( retry ) ;
8675+ else assert ( retry ?. type === "message.dispatch" ) ;
8676+ if ( scenario === "resume" ) {
8677+ assert . notEqual ( retry ! . commandId , resume ! . commandId ) ;
8678+ } else {
8679+ assert . equal (
8680+ restartedShell . limitRecovery ?. requestId ,
8681+ after . thread . limitRecovery ?. requestId ,
8682+ ) ;
8683+ }
8684+ if ( retry !== null ) {
8685+ yield * orchestrator . dispatch ( retry ) ;
8686+ yield * orchestrator . dispatch ( retry ) ;
8687+ }
8688+ const resumed = yield * orchestrator . getThreadProjection ( threadId ) ;
8689+ const visible = createOrchestrationV2TurnItemVisibility ( {
8690+ runs : resumed . runs ,
8691+ attempts : resumed . attempts ,
8692+ items : resumed . turnItems ,
8693+ } ) ;
8694+ assert . deepEqual (
8695+ resumed . turnItems . flatMap ( ( item ) =>
8696+ item . type === "user_message" && visible ( item ) ? [ item . runId ] : [ ] ,
8697+ ) ,
8698+ [ run . id , resumed . runs . at ( - 1 ) ! . id ] ,
8699+ ) ;
8700+ assert . deepEqual (
8701+ resumed . runs . map ( ( row ) => row . status ) ,
8702+ scenario === "resume" ? [ "failed" , "cancelled" , "starting" ] : [ "failed" , "cancelled" ] ,
8703+ ) ;
86238704 }
86248705 assert . lengthOf (
86258706 after . runs ,
86268707 before . runs . length +
86278708 ( scenario === "resume" ||
8709+ scenario === "resume-started" ||
8710+ scenario === "resume-untagged" ||
86288711 stopBeforeArm ||
86298712 scenario === "queued-resume" ||
86308713 scenario === "independent-patches" ||
@@ -8636,6 +8719,8 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
86368719 after . messages ,
86378720 before . messages . length +
86388721 ( scenario === "resume" ||
8722+ scenario === "resume-started" ||
8723+ scenario === "resume-untagged" ||
86398724 stopBeforeArm ||
86408725 scenario === "queued-resume" ||
86418726 scenario === "independent-patches" ||
0 commit comments