@@ -37,6 +37,7 @@ import {
3737import { deliverContextHandoffs } from "./ContextHandoffDelivery.ts" ;
3838import {
3939 ProviderAdapterTurnStartError ,
40+ type ProviderAdapterV2Error ,
4041 type ProviderAdapterV2HistoricalContext ,
4142 type ProviderAdapterV2SessionRuntime ,
4243} from "./ProviderAdapter.ts" ;
@@ -586,8 +587,24 @@ export const layer: Layer.Layer<
586587 return ;
587588 }
588589 const session = sessionResult . success ;
590+ // Only the provider's own thread load fails the run on the last attempt;
591+ // store, id and handoff failures around it keep their typed errors.
592+ const loadFromProvider = (
593+ load : Effect . Effect < OrchestrationV2ProviderThread , ProviderAdapterV2Error > ,
594+ ) =>
595+ Effect . gen ( function * ( ) {
596+ const loaded = yield * Effect . result ( load ) ;
597+ if ( loaded . _tag === "Success" ) return loaded . success ;
598+ if ( input . willRetry === true ) return yield * loaded . failure ;
599+ yield * settleStartFailure ( {
600+ signal : "provider-thread-load-failure" ,
601+ title : "Provider turn failed to start" ,
602+ error : loaded . failure ,
603+ } ) ;
604+ return undefined ;
605+ } ) ;
589606 let effectiveHandoffs = handoffs ;
590- const loadResult = yield * Effect . gen ( function * ( ) {
607+ const loadedProviderThread = yield * Effect . gen ( function * ( ) {
591608 if ( nativeForkTransfer !== undefined ) {
592609 const sourceProjection = yield * projectionStore . getThreadRecords (
593610 nativeForkTransfer . sourceThreadId ,
@@ -613,27 +630,33 @@ export const layer: Layer.Layer<
613630 cause : `Native fork transfer ${ nativeForkTransfer . id } has no source provider execution.` ,
614631 } ) ;
615632 }
616- return yield * session . forkThread ( {
617- sourceProviderThread,
618- sourceProviderTurns : sourceProjection . providerTurns ,
619- targetThreadId : projection . thread . id ,
620- modelSelection : run . modelSelection ,
621- runtimePolicy : resolvedRuntimePolicy ,
622- ...( sourceProviderTurn === undefined ? { } : { providerTurnId : sourceProviderTurn . id } ) ,
623- } ) ;
633+ return yield * loadFromProvider (
634+ session . forkThread ( {
635+ sourceProviderThread,
636+ sourceProviderTurns : sourceProjection . providerTurns ,
637+ targetThreadId : projection . thread . id ,
638+ modelSelection : run . modelSelection ,
639+ runtimePolicy : resolvedRuntimePolicy ,
640+ ...( sourceProviderTurn === undefined
641+ ? { }
642+ : { providerTurnId : sourceProviderTurn . id } ) ,
643+ } ) ,
644+ ) ;
624645 }
625646 if ( providerThread . nativeThreadRef === null ) {
626647 // Hand the run's provider thread to the adapter so it adopts this
627648 // row's identity when attaching native state. An adapter that mints
628649 // its own row instead leaves two live rows per app thread, and
629650 // `activeProviderThreadId` then flaps between them on every update.
630- return yield * session . ensureThread ( {
631- threadId : projection . thread . id ,
632- modelSelection : run . modelSelection ,
633- runtimePolicy : resolvedRuntimePolicy ,
634- providerSessionId,
635- existingProviderThread : providerThread ,
636- } ) ;
651+ return yield * loadFromProvider (
652+ session . ensureThread ( {
653+ threadId : projection . thread . id ,
654+ modelSelection : run . modelSelection ,
655+ runtimePolicy : resolvedRuntimePolicy ,
656+ providerSessionId,
657+ existingProviderThread : providerThread ,
658+ } ) ,
659+ ) ;
637660 }
638661 const uncertainDelivery = projection . contextHandoffs . some (
639662 ( handoff ) =>
@@ -670,16 +693,19 @@ export const layer: Layer.Layer<
670693 reason : uncertainDelivery ? "uncertain_history_delivery" : "resume_failed" ,
671694 errorTag : resumed . failure . _tag ,
672695 } ) ;
673- const replacement = yield * session . ensureThread ( {
674- threadId : projection . thread . id ,
675- modelSelection : run . modelSelection ,
676- runtimePolicy : resolvedRuntimePolicy ,
677- providerSessionId,
678- // The native ref is dropped so the adapter binds a fresh native
679- // session instead of retrying the resume that just failed, while
680- // still adopting this row's identity.
681- existingProviderThread : { ...providerThread , nativeThreadRef : null } ,
682- } ) ;
696+ const replacement = yield * loadFromProvider (
697+ session . ensureThread ( {
698+ threadId : projection . thread . id ,
699+ modelSelection : run . modelSelection ,
700+ runtimePolicy : resolvedRuntimePolicy ,
701+ providerSessionId,
702+ // The native ref is dropped so the adapter binds a fresh native
703+ // session instead of retrying the resume that just failed, while
704+ // still adopting this row's identity.
705+ existingProviderThread : { ...providerThread , nativeThreadRef : null } ,
706+ } ) ,
707+ ) ;
708+ if ( replacement === undefined ) return undefined ;
683709 const transferId = yield * idAllocator . allocate . contextTransfer ( {
684710 sourceThreadId : projection . thread . id ,
685711 targetThreadId : projection . thread . id ,
@@ -747,17 +773,9 @@ export const layer: Layer.Layer<
747773 ] ,
748774 } ) ;
749775 return replacement ;
750- } ) . pipe ( Effect . result ) ;
751- if ( loadResult . _tag === "Failure" ) {
752- if ( input . willRetry === true ) return yield * loadResult . failure ;
753- yield * settleStartFailure ( {
754- signal : "provider-thread-load-failure" ,
755- title : "Provider turn failed to start" ,
756- error : loadResult . failure ,
757- } ) ;
758- return ;
759- }
760- const loadedProviderThread = loadResult . success ;
776+ } ) ;
777+ // The last attempt already failed the run.
778+ if ( loadedProviderThread === undefined ) return ;
761779 if ( ! ( yield * isCurrentAttemptInStatus ( "starting" ) ) ) {
762780 return ;
763781 }
0 commit comments