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