@@ -2,6 +2,7 @@ import { expect, it, vi } from "vite-plus/test";
22import { it as effectIt } from "@effect/vitest" ;
33import {
44 CheckpointScopeId ,
5+ ContextHandoffId ,
56 MessageId ,
67 NodeId ,
78 ProviderSessionId ,
@@ -171,6 +172,7 @@ function makeLocalCommandHarness(input: {
171172 readonly writeFailure ?: unknown ;
172173 /** Loads the thread and starts the run, then fails every later state read. */
173174 readonly failReadsAfterRunning ?: boolean ;
175+ readonly nativeTurn ?: boolean ;
174176} ) {
175177 const now = DateTime . makeUnsafe ( "2026-09-04T12:00:00Z" ) ;
176178 const threadId = ThreadId . make ( "thread-native-account-command" ) ;
@@ -393,6 +395,7 @@ function makeLocalCommandHarness(input: {
393395 ) ,
394396 ensureThread : ( ) => Effect . succeed ( providerThread ) ,
395397 } ;
398+ const startTurn = vi . fn ( ( _input : ProviderAdapter . ProviderAdapterV2TurnInput ) => Effect . void ) ;
396399 const open = vi . fn ( ( ) =>
397400 input . interruptOpen === true
398401 ? Effect . interrupt
@@ -414,7 +417,7 @@ function makeLocalCommandHarness(input: {
414417 ) ,
415418 ) ,
416419 )
417- : input . failReadsAfterRunning === true
420+ : input . failReadsAfterRunning === true || input . nativeTurn === true
418421 ? Effect . succeed ( {
419422 driver : providerThread . driver ,
420423 providerSession : {
@@ -430,15 +433,50 @@ function makeLocalCommandHarness(input: {
430433 lastError : null ,
431434 } ,
432435 ensureThread : ( ) => Effect . succeed ( providerThread ) ,
436+ resumeThread : ( ) => Effect . succeed ( projection . providerThreads . at ( - 1 ) ! ) ,
437+ startTurn,
433438 } as never )
434439 : Effect . die ( "A local command must not open a native session." ) ,
435440 ) ;
436- const startRootRun = vi . fn <
437- ( input : RunExecutionService . RunExecutionServiceV2StartRootRunInput ) => Effect . Effect < void >
438- > ( ( ) =>
439- input . failReadsAfterRunning === true
440- ? Effect . void
441- : Effect . die ( "A local command must not start a native turn." ) ,
441+ const startRootRun = vi . fn ( ( turn : RunExecutionService . RunExecutionServiceV2StartRootRunInput ) =>
442+ input . nativeTurn === true
443+ ? turn . session
444+ . startTurn ( {
445+ appThread : turn . appThread ,
446+ threadId,
447+ runId : turn . run . id ,
448+ runOrdinal : turn . run . ordinal ,
449+ providerTurnOrdinal : turn . run . ordinal ,
450+ attemptId : turn . attemptId ,
451+ rootNodeId : turn . rootNode . id ,
452+ providerThread : turn . providerThread ,
453+ message : {
454+ messageId,
455+ text : input . text ,
456+ attachments : [ ] ,
457+ createdBy : "user" ,
458+ creationSource : "web" ,
459+ } ,
460+ modelSelection : turn . run . modelSelection ,
461+ runtimePolicy : {
462+ cwd : "/tmp/native-account-command" ,
463+ runtimeMode : "full-access" ,
464+ interactionMode : "default" ,
465+ } ,
466+ } )
467+ . pipe (
468+ Effect . mapError (
469+ ( cause ) =>
470+ new RunExecutionService . RunExecutionStartError ( {
471+ commandId : turn . commandId ,
472+ runId : turn . run . id ,
473+ cause,
474+ } ) ,
475+ ) ,
476+ )
477+ : input . failReadsAfterRunning === true
478+ ? Effect . void
479+ : Effect . die ( "A local command must not start a native turn." ) ,
442480 ) ;
443481 const failReadIfRunning = Effect . suspend ( ( ) =>
444482 input . failReadsAfterRunning === true &&
@@ -489,7 +527,18 @@ function makeLocalCommandHarness(input: {
489527 Layer . mock ( ContextHandoffService . ContextHandoffServiceV2 ) ( {
490528 prepareProviderHandoff : ( ) => Effect . die ( "history read must fail first" ) ,
491529 } ) ,
492- Layer . mock ( EventSink . EventSinkV2 ) ( { writeIfRunCurrent } ) ,
530+ Layer . mock ( EventSink . EventSinkV2 ) ( {
531+ writeIfRunCurrent,
532+ write : ( { events : incoming } ) =>
533+ Effect . sync ( ( ) => {
534+ for ( const event of incoming ) {
535+ expect ( isDomainEvent ( event ) ) . toBe ( true ) ;
536+ events . push ( event ) ;
537+ projection = ProjectionStore . applyToProjection ( projection , event ) ;
538+ }
539+ return [ ] ;
540+ } ) ,
541+ } ) ,
493542 IdAllocator . layer ,
494543 FileSystem . layerNoop ( { } ) ,
495544 Layer . mock ( GitWorkflow . GitWorkflowService ) ( { } ) ,
@@ -540,6 +589,12 @@ function makeLocalCommandHarness(input: {
540589 newInstanceId,
541590 attemptId,
542591 projection : ( ) => projection ,
592+ updateProjection : (
593+ update : ( current : OrchestrationV2ThreadProjection ) => OrchestrationV2ThreadProjection ,
594+ ) => {
595+ projection = update ( projection ) ;
596+ } ,
597+ startTurn,
543598 start : Effect . gen ( function * ( ) {
544599 yield * ( yield * ProviderTurnStart . ProviderTurnStartServiceV2 ) . start ( { threadId, runId } ) ;
545600 } ) . pipe ( Effect . provide ( layer ) ) ,
@@ -553,6 +608,83 @@ function makeLocalCommandHarness(input: {
553608 } ;
554609}
555610
611+ effectIt . effect . each ( [
612+ { deferred : true , sameThread : true , usedTokens : 0 , delivers : true } ,
613+ { deferred : true , sameThread : true , usedTokens : 32_000 , delivers : false } ,
614+ { deferred : true , sameThread : false , usedTokens : 0 , delivers : false } ,
615+ { deferred : undefined , sameThread : true , usedTokens : 0 , delivers : false } ,
616+ ] as const ) (
617+ "retries completed ordinary-turn handoffs only when deferred on this provider thread: %j" ,
618+ ( scenario ) =>
619+ Effect . gen ( function * ( ) {
620+ const harness = makeLocalCommandHarness ( { text : "Continue" , nativeTurn : true } ) ;
621+ harness . updateProjection ( ( projection ) => {
622+ const run = projection . runs [ 0 ] ! ;
623+ const providerThread = projection . providerThreads [ 0 ] ! ;
624+ const previousRunId = RunId . make ( "completed-ordinary-handoff-run" ) ;
625+ return {
626+ ...projection ,
627+ runs : [
628+ {
629+ ...run ,
630+ id : previousRunId ,
631+ ordinal : 1 ,
632+ status : "completed" ,
633+ startedAt : run . requestedAt ,
634+ completedAt : run . requestedAt ,
635+ } ,
636+ run ,
637+ ] ,
638+ providerThreads : [
639+ {
640+ ...providerThread ,
641+ nativeThreadRef : {
642+ driver : providerThread . driver ,
643+ nativeId : "native-handoff-target" ,
644+ strength : "strong" ,
645+ } ,
646+ contextUsage : { usedTokens : scenario . usedTokens , maxTokens : 32_000 } ,
647+ } ,
648+ ] ,
649+ contextHandoffs : [
650+ {
651+ id : ContextHandoffId . make ( "completed-ordinary-handoff" ) ,
652+ threadId : run . threadId ,
653+ targetRunId : previousRunId ,
654+ fromProviderThreadIds : [ ] ,
655+ toProviderThreadId : scenario . sameThread
656+ ? providerThread . id
657+ : ProviderThreadId . make ( "another-provider-thread" ) ,
658+ coveredRunOrdinals : { from : 1 , to : 1 } ,
659+ strategy : "full_thread_summary" ,
660+ status : "ready" ,
661+ deferred : scenario . deferred ,
662+ summaryMessageId : null ,
663+ summaryText : "Remember the original constraints." ,
664+ createdByProviderInstanceId : null ,
665+ createdAt : run . requestedAt ,
666+ updatedAt : run . requestedAt ,
667+ } ,
668+ ] ,
669+ } ;
670+ } ) ;
671+
672+ yield * harness . start ;
673+
674+ expect ( harness . startTurn ) . toHaveBeenCalledOnce ( ) ;
675+ const text = harness . startTurn . mock . calls [ 0 ] ! [ 0 ] . message . text ;
676+ expect ( text . includes ( "Remember the original constraints." ) ) . toBe ( scenario . delivers ) ;
677+ expect ( text . endsWith ( "Continue" ) ) . toBe ( true ) ;
678+ const handoff = harness . projection ( ) . contextHandoffs [ 0 ] ! ;
679+ expect ( handoff . delivery ?. status ) . toBe ( scenario . delivers ? "inline" : undefined ) ;
680+ expect ( handoff . deferred ) . toBe ( scenario . delivers ? undefined : scenario . deferred ) ;
681+ expect ( harness . projection ( ) . runs . at ( - 1 ) ?. status ) . toBe ( "running" ) ;
682+ expect (
683+ harness . events . filter ( ( event ) => event . type === "context-handoff.updated" ) ,
684+ ) . toHaveLength ( scenario . delivers ? 2 : 0 ) ;
685+ } ) ,
686+ ) ;
687+
556688effectIt . effect ( "terminalizes a starting run when its provider session cannot open" , ( ) =>
557689 Effect . gen ( function * ( ) {
558690 const harness = makeLocalCommandHarness ( {
0 commit comments