@@ -3,6 +3,7 @@ import { assert, describe, it } from "@effect/vitest";
33import {
44 CommandId ,
55 EnvironmentId ,
6+ MessageId ,
67 NodeId ,
78 type OrchestrationV2ThreadShell ,
89 type ScheduledTask ,
@@ -15,11 +16,18 @@ import {
1516 type OrchestrationV2ThreadProjection ,
1617 type ServerProvider ,
1718} from "@t3tools/contracts" ;
19+ import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace" ;
1820import * as DateTime from "effect/DateTime" ;
1921import * as Effect from "effect/Effect" ;
2022import * as Layer from "effect/Layer" ;
2123import * as Ref from "effect/Ref" ;
2224
25+ import { CodexProviderCapabilitiesV2 } from "../orchestration-v2/Adapters/CodexAdapterV2.ts" ;
26+ import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts" ;
27+ import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts" ;
28+ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts" ;
29+ import * as ProviderReplayHarness from "../orchestration-v2/testkit/ProviderReplayHarness.ts" ;
30+ import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
2331import {
2432 DispatchModeLimit ,
2533 type DispatchModeRefusal ,
@@ -1677,6 +1685,168 @@ describe("OrchestratorMcpService provider resolution", () => {
16771685 } ) ,
16781686 ) ;
16791687
1688+ it . live . each ( [
1689+ { name : "parent instance" , parentEnabled : true , model : "gpt-5.4" } ,
1690+ { name : "alternate instance" , parentEnabled : false , model : "gpt-5.5" } ,
1691+ ] ) (
1692+ "replays accepted creation on the $name without authorizing new disabled work" ,
1693+ ( { parentEnabled, model } ) =>
1694+ Effect . scoped (
1695+ Effect . gen ( function * ( ) {
1696+ const altInstanceId = ProviderInstanceId . make ( "codex-alt" ) ;
1697+ const driver = ProviderDriverKind . make ( "codex" ) ;
1698+ const cwd = yield * checkpointWorkspace ( "mcp-create-retry" ) ;
1699+ const layerRegistry = ProviderAdapterRegistry . layerFromAdapters (
1700+ [ codexInstanceId , altInstanceId ] . map ( ( instanceId ) => ( {
1701+ instanceId,
1702+ driver,
1703+ getCapabilities : ( ) => Effect . succeed ( CodexProviderCapabilitiesV2 ) ,
1704+ planSelectionTransition : ( ) =>
1705+ Effect . succeed ( { type : "apply_on_next_turn" as const } ) ,
1706+ openSession : ( ) =>
1707+ Effect . die ( "These tests exercise committed commands before provider execution." ) ,
1708+ } ) ) ,
1709+ ) ;
1710+ const layerDatabase = SqlitePersistence . layerMemory ;
1711+ const layerOrchestrator = ProviderReplayHarness . layerWithRegistry (
1712+ { name : "mcp-create-retry" , runtimePolicyOverride : { cwd } } ,
1713+ layerRegistry ,
1714+ { databaseLayer : layerDatabase , runEffectWorker : false } ,
1715+ ) ;
1716+ const layerSettings = ServerSettings . layerTest ( {
1717+ providerInstances : {
1718+ [ codexInstanceId ] : { driver : "codex" , enabled : true } ,
1719+ [ altInstanceId ] : { driver : "codex" , enabled : true } ,
1720+ } ,
1721+ projectSettingsOverrides : {
1722+ [ projectId ] : { providerInstances : { [ codexInstanceId ] : { enabled : parentEnabled } } } ,
1723+ } ,
1724+ } ) ;
1725+ const layerThreads = ThreadManagementService . layer . pipe (
1726+ Layer . provideMerge ( layerOrchestrator ) ,
1727+ Layer . provide (
1728+ LegacyV1ThreadImporter . layer . pipe (
1729+ Layer . provide ( layerOrchestrator ) ,
1730+ Layer . provide ( layerDatabase ) ,
1731+ ) ,
1732+ ) ,
1733+ Layer . provide ( CommandReceiptStore . layer . pipe ( Layer . provide ( layerDatabase ) ) ) ,
1734+ Layer . provideMerge ( layerSettings ) ,
1735+ ) ;
1736+ const layerTest = layerOrchestratorService . pipe (
1737+ Layer . provideMerge ( layerThreads ) ,
1738+ Layer . provide (
1739+ Layer . mergeAll (
1740+ NodeServices . layer ,
1741+ layerRegistry ,
1742+ layerSettings ,
1743+ providerRegistryLayer ( [
1744+ providerSnapshot ( { instanceId : codexInstanceId , driver, model : "gpt-5.4" } ) ,
1745+ providerSnapshot ( { instanceId : altInstanceId , driver, model : "gpt-5.5" } ) ,
1746+ ] ) ,
1747+ Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
1748+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
1749+ Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ,
1750+ ) ,
1751+ ) ,
1752+ ) ;
1753+ yield * Effect . gen ( function * ( ) {
1754+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
1755+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
1756+ const settings = yield * ServerSettings . ServerSettingsService ;
1757+ yield * orchestrator . dispatch ( {
1758+ type : "thread.create" ,
1759+ createdBy : "user" ,
1760+ creationSource : "web" ,
1761+ commandId : CommandId . make ( "parent:create" ) ,
1762+ threadId : parentThreadId ,
1763+ projectId,
1764+ title : "MCP parent" ,
1765+ modelSelection : { instanceId : codexInstanceId , model : "gpt-5.4" } ,
1766+ runtimeMode : "full-access" ,
1767+ interactionMode : "default" ,
1768+ branch : null ,
1769+ worktreePath : cwd ,
1770+ } ) ;
1771+ yield * orchestrator . dispatch ( {
1772+ type : "message.dispatch" ,
1773+ createdBy : "user" ,
1774+ creationSource : "web" ,
1775+ commandId : CommandId . make ( "parent:start" ) ,
1776+ threadId : parentThreadId ,
1777+ messageId : MessageId . make ( "parent:message" ) ,
1778+ text : "Create a thread." ,
1779+ attachments : [ ] ,
1780+ dispatchMode : { type : "start_immediately" } ,
1781+ } ) ;
1782+ const request = {
1783+ threads : [ { target : { driverKind : driver , model } } ] ,
1784+ clientRequestId : "create-retry" ,
1785+ } ;
1786+ const first = yield * service . createThreads ( scope , request ) ;
1787+ const child = first . threads [ 0 ] ! ;
1788+ const selection = parentEnabled
1789+ ? { instanceId : codexInstanceId , model : "gpt-5.4" }
1790+ : { instanceId : altInstanceId , model : "gpt-5.5" } ;
1791+ assert . equal ( first . threads . length , 1 ) ;
1792+ assert . equal ( child . providerInstanceId , selection . instanceId ) ;
1793+ assert . equal ( child . model , selection . model ) ;
1794+ assert . deepEqual ( yield * service . createThreads ( scope , request ) , first ) ;
1795+
1796+ // The creation receipt exists, but this prompt has not been accepted yet.
1797+ const withPrompt = {
1798+ ...request ,
1799+ threads : [ { ...request . threads [ 0 ] ! , prompt : "Start work." } ] ,
1800+ } ;
1801+ const started = yield * service . createThreads ( scope , withPrompt ) ;
1802+ assert . equal ( started . threads [ 0 ] ?. threadId , child . threadId ) ;
1803+ const before = yield * orchestrator . getThreadProjection ( child . threadId ) ;
1804+ assert . deepEqual ( before . runs [ 0 ] ?. modelSelection , selection ) ;
1805+ assert . equal ( before . messages . length , 1 ) ;
1806+ const empty = yield * service . createThreads ( scope , {
1807+ ...request ,
1808+ clientRequestId : "empty" ,
1809+ } ) ;
1810+
1811+ yield * settings . updateSettings ( {
1812+ projectSettingsOverrides : {
1813+ [ projectId ] : {
1814+ providerInstances : {
1815+ [ codexInstanceId ] : { enabled : false } ,
1816+ [ altInstanceId ] : { enabled : false } ,
1817+ } ,
1818+ } ,
1819+ } ,
1820+ } ) ;
1821+ assert . deepEqual ( yield * service . createThreads ( scope , withPrompt ) , started ) ;
1822+ const after = yield * orchestrator . getThreadProjection ( child . threadId ) ;
1823+ assert . deepEqual ( after . thread . modelSelection , selection ) ;
1824+ assert . deepEqual ( after . runs , before . runs ) ;
1825+ assert . deepEqual ( after . messages , before . messages ) ;
1826+
1827+ const freshError = yield * service
1828+ . createThreads ( scope , {
1829+ ...request ,
1830+ clientRequestId : "fresh" ,
1831+ } )
1832+ . pipe ( Effect . flip ) ;
1833+ assert . equal ( freshError . code , "provider_unavailable" ) ;
1834+ const promptError = yield * service
1835+ . createThreads ( scope , {
1836+ ...withPrompt ,
1837+ clientRequestId : "empty" ,
1838+ } )
1839+ . pipe ( Effect . flip ) ;
1840+ assert . equal ( promptError . code , "invalid_request" ) ;
1841+ assert . include ( promptError . message , "disabled" ) ;
1842+ const untouched = yield * orchestrator . getThreadProjection ( empty . threads [ 0 ] ! . threadId ) ;
1843+ assert . equal ( untouched . messages . length , 0 ) ;
1844+ assert . equal ( untouched . runs . length , 0 ) ;
1845+ } ) . pipe ( Effect . provide ( layerTest ) ) ;
1846+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
1847+ ) ,
1848+ ) ;
1849+
16801850 it . effect ( "re-probes an unavailable target once before refusing it" , ( ) =>
16811851 Effect . gen ( function * ( ) {
16821852 const claudeInstanceId = ProviderInstanceId . make ( "claudeAgent" ) ;
0 commit comments