The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 00:30 UTC
fix(server): a retried thread creation replays after the project turns its provider off (#18360)
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Yordis Prieto committed
Oct 19, 2026 at 17:13 UTC
27b708542ccb3e8346e8a84790ddcf48cc4055bd
2 files changed
+210
-24
apps/server/src/mcp/OrchestratorMcpService.test.ts
+170
@@ -3,6 +3,7 @@ import { assert, describe, it } from "@effect/vitest";
3
import {
4
CommandId,
5
EnvironmentId,
6
+ MessageId,
7
NodeId,
8
type OrchestrationV2ThreadShell,
9
type ScheduledTask,
@@ -15,11 +16,18 @@ import {
16
type OrchestrationV2ThreadProjection,
17
type ServerProvider,
18
} from "@t3tools/contracts";
19
+import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
20
import * as DateTime from "effect/DateTime";
21
import * as Effect from "effect/Effect";
22
import * as Layer from "effect/Layer";
23
import * as Ref from "effect/Ref";
24
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";
31
import {
32
DispatchModeLimit,
33
type DispatchModeRefusal,
@@ -1677,6 +1685,168 @@ describe("OrchestratorMcpService provider resolution", () => {
1685
}),
1686
);
1687
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
+
1850
it.effect("re-probes an unavailable target once before refusing it", () =>
1851
Effect.gen(function* () {
1852
const claudeInstanceId = ProviderInstanceId.make("claudeAgent");
apps/server/src/mcp/OrchestratorMcpService.ts
+40
-24
@@ -2336,10 +2336,34 @@ const make = Effect.gen(function* () {
2336
input.threads,
2337
(request, index) =>
2338
Effect.gen(function* () {
2339
+ const commandId = stableCommandId({
2340
+ scope,
2341
+ requestKey: key,
2342
+ operation: "create-thread",
2343
+ index,
2344
+ });
2345
+ const threadId = stableThreadId({
2346
+ scope,
2347
+ requestKey: key,
2348
+ index,
2349
+ });
2350
+ const isRetryOfAcceptedCreation = yield* threadManagement.hasReceipt(commandId);
2351
+ const committedThread = isRetryOfAcceptedCreation
2352
+ ? yield* threadManagement
2353
+ .getThreadShell(threadId)
2354
+ .pipe(Effect.mapError(threadManagementFailure))
2355
+ : null;
2356
const target = yield* resolveTargetRechecking({
2357
parent,
2341
- target: request.target,
2358
+ target:
2359
+ committedThread === null
2360
+ ? request.target
2361
+ : {
2362
+ ...request.target,
2363
+ providerInstanceId: committedThread.modelSelection.instanceId,
2364
+ },
2365
providers,
2366
+ skipProjectEnablementFilter: isRetryOfAcceptedCreation,
2367
});
2368
const runtimeMode = yield* resolveRuntimeMode(
2369
parent.thread.runtimeMode,
@@ -2350,16 +2374,7 @@ const make = Effect.gen(function* () {
2374
parent.thread.interactionMode,
2375
request.interactionMode,
2376
);
2353
- const threadId = stableThreadId({
2354
- scope,
2355
- requestKey: key,
2356
- index,
2357
- });
2358
- yield* assertTargetEnabledForProject(
2359
- parent,
2360
- stableCommandId({ scope, requestKey: key, operation: "create-thread", index }),
2361
- target,
2362
- );
2377
+ yield* assertTargetEnabledForProject(parent, commandId, target);
2378
const title = threadTitle({
2379
parentTitle: parent.thread.title,
2380
prompt: request.prompt,
@@ -2371,12 +2386,7 @@ const make = Effect.gen(function* () {
2386
type: "thread.create",
2387
createdBy: "agent",
2388
creationSource: "mcp",
2374
- commandId: stableCommandId({
2375
- scope,
2376
- requestKey: key,
2377
- operation: "create-thread",
2378
- index,
2379
- }),
2389
+ commandId,
2390
threadId,
2391
projectId: parent.thread.projectId,
2392
title,
@@ -2394,6 +2404,10 @@ const make = Effect.gen(function* () {
2404
),
2405
),
2406
);
2407
+ // A receipt can appear after target resolution; use the committed thread's selection.
2408
+ const resolvedModelSelection = (yield* threadManagement
2409
+ .getThreadRecords(threadId, [])
2410
+ .pipe(Effect.mapError(threadManagementFailure))).thread.modelSelection;
2411
if (request.prompt !== undefined) {
2412
yield* threadManagement
2413
.dispatch({
@@ -2415,15 +2429,17 @@ const make = Effect.gen(function* () {
2429
}),
2430
text: request.prompt,
2431
attachments: [],
2418
- modelSelection: target.modelSelection,
2432
+ modelSelection: resolvedModelSelection,
2433
dispatchMode: { type: "start_immediately" },
2434
})
2435
.pipe(
2436
Effect.mapError((error) =>
2423
- failure(
2424
- "orchestration_error",
2425
- `Unable to start thread ${index + 1}: ${errorMessage(error)}`,
2426
- ),
2437
+ error._tag === "ThreadManagementProviderInstanceDisabledError"
2438
+ ? threadManagementFailure(error)
2439
+ : failure(
2440
+ "orchestration_error",
2441
+ `Unable to start thread ${index + 1}: ${errorMessage(error)}`,
2442
+ ),
2443
),
2444
);
2445
}
@@ -2459,8 +2475,8 @@ const make = Effect.gen(function* () {
2475
title: projection.thread.title,
2476
createdBy: projection.thread.createdBy,
2477
creationSource: projection.thread.creationSource,
2462
- providerInstanceId: target.modelSelection.instanceId,
2463
- model: target.modelSelection.model,
2478
+ providerInstanceId: resolvedModelSelection.instanceId,
2479
+ model: resolvedModelSelection.model,
2480
} satisfies OrchestratorMcpCreatedThread;
2481
}),
2482
{ concurrency: 1 },