@@ -12,6 +12,7 @@ import {
1212 legacyThreadPullRequestKey ,
1313} from "@t3tools/shared/threadPullRequests" ;
1414import {
15+ ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE ,
1516 type ChatAttachment ,
1617 CommandId ,
1718 isProviderNativeSubagentThread ,
@@ -400,6 +401,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
400401 case "prepared-run.release" :
401402 case "prepared-run.progress" :
402403 case "prepared-run.fail" :
404+ case "prepared-run.retry" :
403405 case "run.interrupt" :
404406 case "queued-message.promote-to-steer" :
405407 case "queue.resume" :
@@ -442,6 +444,14 @@ function pendingThreadTitleGenerationEffect(
442444
443445const WORKSPACE_PREPARATION_INPUT = "Preparing workspace" ;
444446
447+ /** A reopened preparation item drops the output and exit code of the attempt it replaces. */
448+ function withoutPreparationResult (
449+ item : Extract < OrchestrationV2TurnItem , { readonly type : "command_execution" } > ,
450+ ) {
451+ const { output : _output , exitCode : _exitCode , outputIndicatesFailure : _failure , ...rest } = item ;
452+ return rest ;
453+ }
454+
445455function isBlockingRun ( run : OrchestrationV2Run ) : boolean {
446456 return (
447457 run . status === "preparing" ||
@@ -5150,6 +5160,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
51505160 ...( command . restartContinuationOfRunId === undefined
51515161 ? { }
51525162 : { restartContinuationOfRunId : command . restartContinuationOfRunId } ) ,
5163+ ...( dispatchMode . type === "defer_start" && dispatchMode . workspaceStrategy !== undefined
5164+ ? { workspacePreparation : dispatchMode . workspaceStrategy }
5165+ : { } ) ,
51535166 ...wakeWorkStartedAt ( projection . runs , command ) ,
51545167 } ;
51555168 const attempt : OrchestrationV2RunAttempt = {
@@ -7489,7 +7502,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
74897502 command : Extract <
74907503 OrchestrationV2Command ,
74917504 {
7492- readonly type : "prepared-run.release" | "prepared-run.progress" | "prepared-run.fail" ;
7505+ readonly type :
7506+ | "prepared-run.release"
7507+ | "prepared-run.progress"
7508+ | "prepared-run.fail"
7509+ | "prepared-run.retry" ;
74937510 }
74947511 > ,
74957512 projection : Pick <
@@ -7512,7 +7529,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
75127529 candidate . input === WORKSPACE_PREPARATION_INPUT ,
75137530 ) ;
75147531 if (
7515- run ?. status !== " preparing" ||
7532+ run ?. status !== ( command . type === "prepared-run.retry" ? "failed" : " preparing") ||
75167533 attempt === undefined ||
75177534 rootNode === undefined ||
75187535 providerThread === undefined ||
@@ -7730,7 +7747,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
77307747 completedAt : now ,
77317748 updatedAt : now ,
77327749 type : "error" ,
7733- failure : command . failure ,
7750+ failure : {
7751+ ...command . failure ,
7752+ code : ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE ,
7753+ } ,
77347754 } ,
77357755 } ) ;
77367756 yield * emitEvent ( {
@@ -7744,6 +7764,90 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
77447764 } ) ;
77457765 } ) ;
77467766
7767+ /**
7768+ * Returns a run whose workspace preparation failed to preparing. The failure
7769+ * item turns cancelled so clients stop offering the retry; ThreadLaunchService
7770+ * runs the recorded preparation again once this commits.
7771+ */
7772+ const dispatchPreparedRunRetry = (
7773+ command : Extract < OrchestrationV2Command , { readonly type : "prepared-run.retry" } > ,
7774+ events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ,
7775+ ) =>
7776+ Effect . gen ( function * ( ) {
7777+ const projection = yield * loadProjectionForCommand (
7778+ command ,
7779+ [ "runs" , "attempts" , "nodes" , "providerThreads" , "turnItems" ] ,
7780+ { turnItemTypes : [ "command_execution" , "error" ] , turnItemRunId : command . runId } ,
7781+ ) ;
7782+ const state = preparedRunState ( command , projection ) ;
7783+ const failureItem = projection . turnItems . find (
7784+ ( candidate ) : candidate is Extract < OrchestrationV2TurnItem , { readonly type : "error" } > =>
7785+ candidate . type === "error" &&
7786+ candidate . status === "failed" &&
7787+ candidate . failure . code === ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE ,
7788+ ) ;
7789+ if (
7790+ state === null ||
7791+ state . run . workspacePreparation === undefined ||
7792+ failureItem === undefined ||
7793+ projection . thread . archivedAt !== null ||
7794+ projection . thread . deletedAt !== null
7795+ ) {
7796+ return yield * new OrchestratorDispatchError ( {
7797+ commandId : command . commandId ,
7798+ commandType : command . type ,
7799+ cause : `Run ${ command . runId } has no failed workspace preparation to retry.` ,
7800+ } ) ;
7801+ }
7802+ if ( projection . runs . some ( ( run ) => run . id !== state . run . id && isBlockingRun ( run ) ) ) {
7803+ return yield * new OrchestratorDispatchError ( {
7804+ commandId : command . commandId ,
7805+ commandType : command . type ,
7806+ cause : "Another run is active on this thread." ,
7807+ } ) ;
7808+ }
7809+ const now = yield * DateTime . now ;
7810+ const emitEvent = emit ( events , command ) ;
7811+ const scope = {
7812+ threadId : command . threadId ,
7813+ runId : state . run . id ,
7814+ nodeId : state . rootNode . id ,
7815+ providerInstanceId : state . run . providerInstanceId ,
7816+ occurredAt : now ,
7817+ } ;
7818+ yield * emitEvent ( {
7819+ ...scope ,
7820+ type : "turn-item.updated" ,
7821+ payload : { ...failureItem , status : "cancelled" , updatedAt : now } ,
7822+ } ) ;
7823+ yield * emitEvent ( {
7824+ ...scope ,
7825+ type : "turn-item.updated" ,
7826+ payload : {
7827+ ...withoutPreparationResult ( state . preparationItem ) ,
7828+ status : "running" ,
7829+ title : WORKSPACE_PREPARATION_INPUT ,
7830+ completedAt : null ,
7831+ updatedAt : now ,
7832+ } ,
7833+ } ) ;
7834+ yield * emitEvent ( {
7835+ ...scope ,
7836+ type : "run-attempt.updated" ,
7837+ payload : { ...state . attempt , status : "pending" , completedAt : null } ,
7838+ } ) ;
7839+ yield * emitEvent ( {
7840+ ...scope ,
7841+ type : "node.updated" ,
7842+ payload : { ...state . rootNode , status : "pending" , completedAt : null } ,
7843+ } ) ;
7844+ yield * emitEvent ( {
7845+ ...scope ,
7846+ type : "run.updated" ,
7847+ payload : { ...state . run , status : "preparing" , completedAt : null } ,
7848+ } ) ;
7849+ } ) ;
7850+
77477851 /**
77487852 * Ends the background work a settled thread still shows that no provider
77497853 * process will report on: work on the provider thread whose interrupt just
@@ -9450,6 +9554,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94509554 case "prepared-run.fail" :
94519555 yield * dispatchPreparedRunFail ( command , events ) ;
94529556 break ;
9557+ case "prepared-run.retry" :
9558+ yield * dispatchPreparedRunRetry ( command , events ) ;
9559+ break ;
94539560 case "runtime-request.respond" :
94549561 yield * dispatchRuntimeRequestRespond ( command , events , effects ) ;
94559562 break ;
0 commit comments