@@ -35,6 +35,7 @@ import {
3535 type ProviderAdapterV2TurnInput ,
3636} from "./ProviderAdapter.ts" ;
3737import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts" ;
38+ import * as ThreadManagementService from "./ThreadManagementService.ts" ;
3839import { makeOrchestratorV2ReplayLayerWithRegistry } from "./testkit/ProviderReplayHarness.ts" ;
3940import { checkpointWorkspace } from "./testkit/ReplayFixtureWorkspace.ts" ;
4041
@@ -391,6 +392,208 @@ function makeCompletingHandoffAdapter(startCount: Ref.Ref<number>): ProviderAdap
391392 } ;
392393}
393394
395+ it . live . each ( [
396+ "saved-model" ,
397+ "options" ,
398+ "provider" ,
399+ "unchanged" ,
400+ "idle" ,
401+ "unsupported" ,
402+ "preparing" ,
403+ ] as const ) ( "configures the calling thread without losing work: %s" , ( scenario ) =>
404+ Effect . scoped (
405+ Effect . gen ( function * ( ) {
406+ const name = `configure-continuation-${ scenario } ` ;
407+ const cwd = yield * checkpointWorkspace ( name ) ;
408+ const threadId = ThreadId . make ( `thread:${ name } ` ) ;
409+ const projectId = ProjectId . make ( `project:${ name } ` ) ;
410+ const otherInstanceId = ProviderInstanceId . make ( "codex-configure-target" ) ;
411+ const selection = {
412+ ...initialSelection ,
413+ options : [
414+ { id : "reasoningEffort" , value : "low" } ,
415+ { id : "fastMode" , value : true } ,
416+ ] ,
417+ } satisfies ModelSelection ;
418+ const targetSelection : ModelSelection =
419+ scenario === "unchanged"
420+ ? { ...selection , options : selection . options . toReversed ( ) }
421+ : scenario === "options"
422+ ? { ...selection , options : [ { id : "reasoningEffort" , value : "xhigh" } ] }
423+ : {
424+ ...replacementSelection ,
425+ instanceId : scenario === "provider" ? otherInstanceId : providerInstanceId ,
426+ } ;
427+ const state = yield * Ref . make < RestartAdapterState > ( {
428+ activeTurn : null ,
429+ opened : [ ] ,
430+ started : [ ] ,
431+ closedSessionCount : 0 ,
432+ failedReplacementOpen : true ,
433+ } ) ;
434+ const starts : Array < ProviderAdapterV2TurnInput > = [ ] ;
435+ const capabilities = {
436+ ...pooledCapabilities ,
437+ turns : {
438+ ...pooledCapabilities . turns ,
439+ supportsSteeringByInterruptRestart : scenario !== "unsupported" ,
440+ } ,
441+ } ;
442+ const adapters = [ providerInstanceId , otherInstanceId ] . map ( ( instanceId ) => {
443+ const base = makeRestartAdapter ( state , capabilities , instanceId ) ;
444+ return {
445+ ...base ,
446+ planSelectionTransition : ( ) => Effect . succeed ( { type : "apply_on_next_turn" } as const ) ,
447+ openSession : ( input ) =>
448+ base . openSession ( input ) . pipe (
449+ Effect . map ( ( session ) => ( {
450+ ...session ,
451+ startTurn : ( input ) =>
452+ Effect . gen ( function * ( ) {
453+ starts . push ( input ) ;
454+ yield * session . startTurn ( input ) ;
455+ } ) ,
456+ } ) ) ,
457+ ) ,
458+ } satisfies ProviderAdapterV2Shape ;
459+ } ) ;
460+ const registry = Layer . succeed ( ProviderAdapterRegistry . ProviderAdapterRegistryV2 , {
461+ get : ( instanceId ) =>
462+ Effect . succeed ( adapters . find ( ( adapter ) => adapter . instanceId === instanceId ) ! ) ,
463+ list : ( ) => Effect . succeed ( [ providerInstanceId , otherInstanceId ] ) ,
464+ getMetadata : ( ) =>
465+ Effect . succeed ( {
466+ driver,
467+ continuationKey : "codex:home:/shared" ,
468+ enabled : true ,
469+ capabilities,
470+ } ) ,
471+ } ) ;
472+ yield * Effect . gen ( function * ( ) {
473+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
474+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
475+ const threads = yield * ThreadManagementService . ThreadManagementService ;
476+ yield * orchestrator . dispatch ( {
477+ type : "thread.create" ,
478+ commandId : CommandId . make ( `${ name } :create` ) ,
479+ threadId,
480+ projectId,
481+ title : name ,
482+ modelSelection : selection ,
483+ runtimeMode : "full-access" ,
484+ interactionMode : "default" ,
485+ branch : null ,
486+ worktreePath : cwd ,
487+ createdBy : "user" ,
488+ creationSource : "web" ,
489+ } ) ;
490+ if ( scenario !== "idle" ) {
491+ const afterSequence = yield * orchestrator . getThreadEventSequence ( threadId ) ;
492+ yield * orchestrator . dispatch ( {
493+ type : "message.dispatch" ,
494+ commandId : CommandId . make ( `${ name } :first` ) ,
495+ threadId,
496+ messageId : MessageId . make ( `${ name } :first` ) ,
497+ text : "Switch to the requested model and finish the poem." ,
498+ attachments : [ ] ,
499+ dispatchMode : { type : "start_immediately" } ,
500+ createdBy : "user" ,
501+ creationSource : "web" ,
502+ } ) ;
503+ if ( scenario !== "preparing" ) {
504+ yield * worker . drain ( ) ;
505+ yield * orchestrator . streamStoredEventsFrom ( { threadId, afterSequence } ) . pipe (
506+ Stream . filter (
507+ ( { event } ) =>
508+ event . type === "provider-turn.updated" && event . payload . status === "running" ,
509+ ) ,
510+ Stream . take ( 1 ) ,
511+ Stream . runDrain ,
512+ ) ;
513+ }
514+ }
515+ if ( scenario === "saved-model" ) {
516+ yield * orchestrator . dispatch ( {
517+ type : "thread.model-selection.set" ,
518+ commandId : CommandId . make ( `${ name } :save-next-model` ) ,
519+ threadId,
520+ modelSelection : targetSelection ,
521+ } ) ;
522+ yield * worker . drain ( ) ;
523+ }
524+ const before = yield * orchestrator . getThreadProjection ( threadId ) ;
525+ const sequence = yield * orchestrator . getThreadEventSequence ( threadId ) ;
526+ const configure = threads . configureModelSelection ( {
527+ projectId,
528+ threadId,
529+ commandId : CommandId . make ( `${ name } :configure` ) ,
530+ modelSelection : targetSelection ,
531+ createdBy : "agent" ,
532+ creationSource : "mcp" ,
533+ } ) ;
534+ if ( scenario === "unsupported" || scenario === "preparing" ) {
535+ const error = yield * Effect . flip ( configure ) ;
536+ assert . equal (
537+ error . _tag ,
538+ scenario === "preparing"
539+ ? "ThreadManagementNoSteerableRunError"
540+ : "OrchestratorDispatchError" ,
541+ ) ;
542+ assert . deepEqual ( yield * orchestrator . getThreadProjection ( threadId ) , before ) ;
543+ assert . equal ( yield * orchestrator . getThreadEventSequence ( threadId ) , sequence ) ;
544+ return ;
545+ }
546+ if ( scenario === "unchanged" || scenario === "idle" ) {
547+ const result = yield * configure ;
548+ const after = yield * orchestrator . getThreadProjection ( threadId ) ;
549+ if ( scenario === "unchanged" ) {
550+ assert . equal ( result . sequence , sequence ) ;
551+ assert . deepEqual ( after , before ) ;
552+ } else {
553+ assert . deepEqual ( after . thread . modelSelection , targetSelection ) ;
554+ assert . isEmpty ( after . runs ) ;
555+ assert . isEmpty ( after . messages ) ;
556+ }
557+ return ;
558+ }
559+ yield * configure ;
560+ yield * worker . drain ( ) ;
561+ yield * orchestrator . streamStoredEventsFrom ( { threadId, afterSequence : sequence } ) . pipe (
562+ Stream . filter ( ( { event } ) =>
563+ scenario === "options"
564+ ? event . type === "provider-turn.updated" && event . payload . status === "running"
565+ : event . type === "run.updated" && event . payload . status === "completed" ,
566+ ) ,
567+ Stream . take ( 1 ) ,
568+ Stream . runDrain ,
569+ ) ;
570+ yield * worker . drain ( ) ;
571+ const after = yield * orchestrator . getThreadProjection ( threadId ) ;
572+ assert . lengthOf ( starts , 2 ) ;
573+ assert . deepEqual ( starts [ 1 ] ?. modelSelection , targetSelection ) ;
574+ assert . match ( starts [ 1 ] ! . message . text , / c o n t i n u e | f i n i s h / i) ;
575+ assert . equal ( after . runs [ 0 ] ?. id , before . runs [ 0 ] ?. id ) ;
576+ assert . deepEqual ( after . runs [ 0 ] ?. modelSelection , targetSelection ) ;
577+ assert . deepEqual ( after . thread . modelSelection , targetSelection ) ;
578+ assert . lengthOf ( after . attempts , 2 ) ;
579+ assert . equal ( after . attempts [ 0 ] ?. status , "superseded" ) ;
580+ assert . equal ( after . messages [ 1 ] ?. createdBy , "agent" ) ;
581+ assert . equal ( after . messages [ 1 ] ?. creationSource , "mcp" ) ;
582+ } ) . pipe (
583+ Effect . provide (
584+ ThreadManagementService . layer . pipe (
585+ Layer . provideMerge (
586+ makeOrchestratorV2ReplayLayerWithRegistry ( { name } , registry , {
587+ runEffectWorker : scenario !== "preparing" ,
588+ } ) ,
589+ ) ,
590+ ) ,
591+ ) ,
592+ ) ;
593+ } ) ,
594+ ) ,
595+ ) ;
596+
394597it . live ( "restarts selection as a new attempt and retries after old-session cleanup" , ( ) =>
395598 Effect . scoped (
396599 Effect . gen ( function * ( ) {
0 commit comments