@@ -3,13 +3,18 @@ import { assert, describe, it } from "@effect/vitest";
33import {
44 CommandId ,
55 EnvironmentId ,
6+ MessageId ,
67 NodeId ,
78 type OrchestrationV2ThreadShell ,
9+ type OrchestrationV2ProviderSession ,
10+ type OrchestrationV2ProviderThread ,
811 type ScheduledTask ,
912 ScheduledTaskId ,
1013 ProjectId ,
1114 ProviderDriverKind ,
1215 ProviderInstanceId ,
16+ ProviderThreadId ,
17+ ProviderTurnId ,
1318 RunId ,
1419 ThreadId ,
1520 type OrchestrationV2ThreadProjection ,
@@ -18,18 +23,26 @@ import {
1823import * as DateTime from "effect/DateTime" ;
1924import * as Effect from "effect/Effect" ;
2025import * as Layer from "effect/Layer" ;
26+ import * as PubSub from "effect/PubSub" ;
2127import * as Ref from "effect/Ref" ;
28+ import * as Stream from "effect/Stream" ;
29+ import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace" ;
2230
31+ import { CodexProviderCapabilitiesV2 } from "../orchestration-v2/Adapters/CodexAdapterV2.ts" ;
32+ import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts" ;
2333import {
2434 DispatchModeLimit ,
2535 type DispatchModeRefusal ,
2636} from "../orchestration-v2/DispatchModeLimit.ts" ;
37+ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts" ;
2738import {
2839 OrchestratorProjectionError ,
2940 OrchestratorThreadAboveModeLimitError ,
3041} from "../orchestration-v2/Orchestrator.ts" ;
3142import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts" ;
43+ import * as ProviderReplayHarness from "../orchestration-v2/testkit/ProviderReplayHarness.ts" ;
3244import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts" ;
45+ import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
3346import * as ProviderRegistry from "../provider/ProviderRegistry.ts" ;
3447import { buildUnavailableProviderSnapshot } from "../provider/unavailableProviderSnapshot.ts" ;
3548import * as ProjectService from "../project/ProjectService.ts" ;
@@ -870,7 +883,10 @@ describe("OrchestratorMcpService provider resolution", () => {
870883 } ) as unknown as OrchestrationV2ThreadProjection ;
871884
872885 const childProjection = {
873- thread : { id : childThreadId } ,
886+ thread : {
887+ id : childThreadId ,
888+ modelSelection : { instanceId : codexInstanceId , model : "gpt-5.4" } ,
889+ } ,
874890 runs : [ ] ,
875891 contextTransfers : [ ] ,
876892 messages : [ ] ,
@@ -879,6 +895,123 @@ describe("OrchestratorMcpService provider resolution", () => {
879895 turnItems : [ ] ,
880896 } as unknown as OrchestrationV2ThreadProjection ;
881897
898+ /**
899+ * A real provider adapter whose turn starts and stays running, so a real
900+ * orchestrator keeps the parent's run active without ever completing it.
901+ */
902+ const makeKeepAliveAdapter = ( input : {
903+ readonly instanceId : ProviderInstanceId ;
904+ readonly driver : ProviderDriverKind ;
905+ } ) : ProviderAdapter . ProviderAdapterV2 [ "Service" ] => ( {
906+ instanceId : input . instanceId ,
907+ driver : input . driver ,
908+ getCapabilities : ( ) => Effect . succeed ( CodexProviderCapabilitiesV2 ) ,
909+ planSelectionTransition : ( ) => Effect . succeed ( { type : "apply_on_next_turn" } ) ,
910+ openSession : ( sessionInput ) =>
911+ Effect . gen ( function * ( ) {
912+ const events = yield * PubSub . unbounded < ProviderAdapter . ProviderAdapterV2Event > ( ) ;
913+ const now = yield * DateTime . now ;
914+ const providerSession : OrchestrationV2ProviderSession = {
915+ id : sessionInput . providerSessionId ,
916+ driver : input . driver ,
917+ providerInstanceId : input . instanceId ,
918+ status : "ready" ,
919+ cwd : sessionInput . runtimePolicy . cwd ?? process . cwd ( ) ,
920+ model : sessionInput . modelSelection . model ,
921+ capabilities : CodexProviderCapabilitiesV2 ,
922+ createdAt : now ,
923+ updatedAt : now ,
924+ lastError : null ,
925+ } ;
926+ return {
927+ instanceId : input . instanceId ,
928+ driver : input . driver ,
929+ providerSessionId : sessionInput . providerSessionId ,
930+ providerSession,
931+ events : Stream . fromPubSub ( events ) ,
932+ ensureThread : ( threadInput ) =>
933+ Effect . gen ( function * ( ) {
934+ const createdAt = yield * DateTime . now ;
935+ const nativeThreadId = `${ input . driver } :${ threadInput . threadId } ` ;
936+ return {
937+ id : ProviderThreadId . make ( `provider-thread:${ nativeThreadId } ` ) ,
938+ driver : input . driver ,
939+ providerInstanceId : input . instanceId ,
940+ providerSessionId : sessionInput . providerSessionId ,
941+ appThreadId : threadInput . threadId ,
942+ ownerNodeId : null ,
943+ nativeThreadRef : {
944+ driver : input . driver ,
945+ nativeId : nativeThreadId ,
946+ strength : "strong" ,
947+ } ,
948+ nativeConversationHeadRef : null ,
949+ status : "idle" ,
950+ firstRunOrdinal : null ,
951+ lastRunOrdinal : null ,
952+ handoffIds : [ ] ,
953+ forkedFrom : null ,
954+ createdAt,
955+ updatedAt : createdAt ,
956+ } satisfies OrchestrationV2ProviderThread ;
957+ } ) ,
958+ resumeThread : ( { providerThread } ) => Effect . succeed ( providerThread ) ,
959+ startTurn : ( turnInput ) =>
960+ Effect . gen ( function * ( ) {
961+ const eventTime = yield * DateTime . now ;
962+ const providerTurnId = ProviderTurnId . make (
963+ `provider-turn:${ input . instanceId } :${ turnInput . threadId } :${ turnInput . runOrdinal } ` ,
964+ ) ;
965+ yield * PubSub . publish ( events , {
966+ type : "provider_turn.updated" ,
967+ driver : input . driver ,
968+ providerTurn : {
969+ id : providerTurnId ,
970+ providerThreadId : turnInput . providerThread . id ,
971+ nodeId : turnInput . rootNodeId ,
972+ runAttemptId : turnInput . attemptId ,
973+ nativeTurnRef : {
974+ driver : input . driver ,
975+ nativeId : `native-turn:${ turnInput . threadId } :${ turnInput . runOrdinal } ` ,
976+ strength : "strong" ,
977+ } ,
978+ ordinal : turnInput . providerTurnOrdinal ,
979+ status : "running" ,
980+ startedAt : eventTime ,
981+ completedAt : null ,
982+ } ,
983+ } ) ;
984+ // Stays running for the test's lifetime: createThreads only
985+ // needs an active parent run, never a completed turn.
986+ } ) ,
987+ steerTurn : ( ) => Effect . void ,
988+ interruptTurn : ( ) => Effect . void ,
989+ respondToRuntimeRequest : ( ) => Effect . void ,
990+ readThreadSnapshot : ( ) => Effect . die ( "readThreadSnapshot is unused in this test" ) ,
991+ rollbackThread : ( ) => Effect . die ( "rollbackThread is unused in this test" ) ,
992+ forkThread : ( ) => Effect . die ( "forkThread is unused in this test" ) ,
993+ } ;
994+ } ) ,
995+ } ) ;
996+
997+ const waitForProjection = (
998+ orchestrator : Orchestrator . OrchestratorV2Shape ,
999+ threadId : ThreadId ,
1000+ predicate : ( projection : OrchestrationV2ThreadProjection ) => boolean ,
1001+ ) =>
1002+ Effect . gen ( function * ( ) {
1003+ for ( let attempt = 0 ; attempt < 1_000 ; attempt += 1 ) {
1004+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
1005+ if ( predicate ( projection ) ) {
1006+ return projection ;
1007+ }
1008+ yield * Effect . sleep ( "5 millis" ) ;
1009+ }
1010+ return yield * Effect . die (
1011+ new Error ( `Timed out waiting for orchestration projection ${ threadId } .` ) ,
1012+ ) ;
1013+ } ) ;
1014+
8821015 it . effect (
8831016 "advertises orchestration capability from registered adapters rather than a driver allowlist" ,
8841017 ( ) =>
@@ -1696,6 +1829,144 @@ describe("OrchestratorMcpService provider resolution", () => {
16961829 } ) ,
16971830 ) ;
16981831
1832+ it . live (
1833+ "a retried driver-only thread creation reports the instance that was actually created" ,
1834+ ( ) =>
1835+ Effect . scoped (
1836+ Effect . gen ( function * ( ) {
1837+ const altInstanceId = ProviderInstanceId . make ( "codex-alt" ) ;
1838+ const codexDriver = ProviderDriverKind . make ( "codex" ) ;
1839+ const cwd = yield * checkpointWorkspace ( "mcp-create-threads-driver-retry" ) ;
1840+
1841+ const layerRegistry = ProviderAdapterRegistry . layerFromAdapters ( [
1842+ makeKeepAliveAdapter ( { instanceId : codexInstanceId , driver : codexDriver } ) ,
1843+ makeKeepAliveAdapter ( { instanceId : altInstanceId , driver : codexDriver } ) ,
1844+ ] ) ;
1845+ const layerOrchestrator = ProviderReplayHarness . layerWithRegistry (
1846+ {
1847+ name : "mcp-create-threads-driver-retry" ,
1848+ runtimePolicyOverride : {
1849+ cwd,
1850+ approvalPolicy : "never" ,
1851+ sandboxPolicy : {
1852+ type : "readOnly" ,
1853+ access : { type : "fullAccess" } ,
1854+ networkAccess : false ,
1855+ } ,
1856+ } ,
1857+ } ,
1858+ layerRegistry ,
1859+ ) ;
1860+ // The project disables the parent's own instance for new work, so
1861+ // the first creation picks the alternate instance of the same
1862+ // driver. A retry must keep reporting that alternate, never the
1863+ // parent's own instance, even though it is first in resolution
1864+ // order once the retry's replay exemption skips the project check.
1865+ const layerSettings = ServerSettings . layerTest ( {
1866+ providerInstances : {
1867+ [ codexInstanceId ] : { driver : "codex" , enabled : true } ,
1868+ [ altInstanceId ] : { driver : "codex" , enabled : true } ,
1869+ } ,
1870+ projectSettingsOverrides : {
1871+ [ projectId ] : { providerInstances : { [ codexInstanceId ] : { enabled : false } } } ,
1872+ } ,
1873+ } ) ;
1874+ const layerThreadManagement = ThreadManagementService . layer . pipe (
1875+ Layer . provide ( layerOrchestrator ) ,
1876+ Layer . provide ( layerSettings ) ,
1877+ Layer . provide (
1878+ CommandReceiptStore . layer . pipe ( Layer . provide ( SqlitePersistence . layerMemory ) ) ,
1879+ ) ,
1880+ ) ;
1881+ const layerOrchestration = Layer . merge ( layerOrchestrator , layerThreadManagement ) ;
1882+ const layerDependencies = Layer . mergeAll (
1883+ NodeServices . layer ,
1884+ providerRegistryLayer ( [
1885+ providerSnapshot ( {
1886+ instanceId : codexInstanceId ,
1887+ driver : codexDriver ,
1888+ model : "gpt-5.4" ,
1889+ } ) ,
1890+ providerSnapshot ( {
1891+ instanceId : altInstanceId ,
1892+ driver : codexDriver ,
1893+ model : "gpt-5.4" ,
1894+ } ) ,
1895+ ] ) ,
1896+ layerRegistry ,
1897+ Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
1898+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
1899+ Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ,
1900+ layerSettings ,
1901+ ) ;
1902+ const layerTest = OrchestratorMcpService . layer . pipe (
1903+ Layer . provideMerge ( layerOrchestration ) ,
1904+ Layer . provide ( layerDependencies ) ,
1905+ ) ;
1906+
1907+ yield * Effect . gen ( function * ( ) {
1908+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
1909+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
1910+
1911+ yield * orchestrator . dispatch ( {
1912+ type : "thread.create" ,
1913+ createdBy : "user" ,
1914+ creationSource : "web" ,
1915+ commandId : CommandId . make ( "command:mcp-create-retry-parent:create" ) ,
1916+ threadId : parentThreadId ,
1917+ projectId,
1918+ title : "MCP parent" ,
1919+ modelSelection : { instanceId : codexInstanceId , model : "gpt-5.4" } ,
1920+ runtimeMode : "full-access" ,
1921+ interactionMode : "default" ,
1922+ branch : null ,
1923+ worktreePath : cwd ,
1924+ } ) ;
1925+ yield * orchestrator . dispatch ( {
1926+ type : "message.dispatch" ,
1927+ createdBy : "user" ,
1928+ creationSource : "web" ,
1929+ commandId : CommandId . make ( "command:mcp-create-retry-parent:start" ) ,
1930+ threadId : parentThreadId ,
1931+ messageId : MessageId . make ( "message:mcp-create-retry-parent:start" ) ,
1932+ text : "Keep this parent turn active while the create retry is tested." ,
1933+ attachments : [ ] ,
1934+ modelSelection : { instanceId : codexInstanceId , model : "gpt-5.4" } ,
1935+ dispatchMode : { type : "start_immediately" } ,
1936+ } ) ;
1937+ yield * waitForProjection (
1938+ orchestrator ,
1939+ parentThreadId ,
1940+ ( projection ) =>
1941+ projection . runs . some ( ( run ) => run . status === "running" ) &&
1942+ projection . providerTurns . some ( ( turn ) => turn . status === "running" ) ,
1943+ ) ;
1944+
1945+ const first = yield * service . createThreads ( scope , {
1946+ threads : [ { target : { driverKind : codexDriver } } ] ,
1947+ clientRequestId : "create-thread-driver-retry-1" ,
1948+ } ) ;
1949+ assert . equal ( first . threads . length , 1 ) ;
1950+ assert . equal ( first . threads [ 0 ] ?. providerInstanceId , altInstanceId ) ;
1951+
1952+ const retry = yield * service . createThreads ( scope , {
1953+ threads : [ { target : { driverKind : codexDriver } } ] ,
1954+ clientRequestId : "create-thread-driver-retry-1" ,
1955+ } ) ;
1956+ assert . equal ( retry . threads . length , 1 ) ;
1957+ assert . equal ( retry . threads [ 0 ] ?. threadId , first . threads [ 0 ] ?. threadId ) ;
1958+ assert . equal ( retry . threads [ 0 ] ?. providerInstanceId , altInstanceId ) ;
1959+ assert . equal ( retry . threads [ 0 ] ?. model , first . threads [ 0 ] ?. model ) ;
1960+
1961+ const createdProjection = yield * orchestrator . getThreadProjection (
1962+ first . threads [ 0 ] ! . threadId ,
1963+ ) ;
1964+ assert . equal ( createdProjection . thread . modelSelection . instanceId , altInstanceId ) ;
1965+ } ) . pipe ( Effect . provide ( layerTest ) ) ;
1966+ } ) ,
1967+ ) ,
1968+ ) ;
1969+
16991970 it . effect ( "re-probes an unavailable target once before refusing it" , ( ) =>
17001971 Effect . gen ( function * ( ) {
17011972 const claudeInstanceId = ProviderInstanceId . make ( "claudeAgent" ) ;
0 commit comments