Skip to content

Commit 27b7085

Browse files
fix(server): a retried thread creation replays after the project turns its provider off (pingdotgg#18360)
Signed-off-by: Yordis Prieto <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent a195ba0 commit 27b7085

2 files changed

Lines changed: 210 additions & 24 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert, describe, it } from "@effect/vitest";
33
import {
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";
1820
import * as DateTime from "effect/DateTime";
1921
import * as Effect from "effect/Effect";
2022
import * as Layer from "effect/Layer";
2123
import * 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";
2331
import {
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");

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,10 +2336,34 @@ const make = Effect.gen(function* () {
23362336
input.threads,
23372337
(request, index) =>
23382338
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;
23392356
const target = yield* resolveTargetRechecking({
23402357
parent,
2341-
target: request.target,
2358+
target:
2359+
committedThread === null
2360+
? request.target
2361+
: {
2362+
...request.target,
2363+
providerInstanceId: committedThread.modelSelection.instanceId,
2364+
},
23422365
providers,
2366+
skipProjectEnablementFilter: isRetryOfAcceptedCreation,
23432367
});
23442368
const runtimeMode = yield* resolveRuntimeMode(
23452369
parent.thread.runtimeMode,
@@ -2350,16 +2374,7 @@ const make = Effect.gen(function* () {
23502374
parent.thread.interactionMode,
23512375
request.interactionMode,
23522376
);
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);
23632378
const title = threadTitle({
23642379
parentTitle: parent.thread.title,
23652380
prompt: request.prompt,
@@ -2371,12 +2386,7 @@ const make = Effect.gen(function* () {
23712386
type: "thread.create",
23722387
createdBy: "agent",
23732388
creationSource: "mcp",
2374-
commandId: stableCommandId({
2375-
scope,
2376-
requestKey: key,
2377-
operation: "create-thread",
2378-
index,
2379-
}),
2389+
commandId,
23802390
threadId,
23812391
projectId: parent.thread.projectId,
23822392
title,
@@ -2394,6 +2404,10 @@ const make = Effect.gen(function* () {
23942404
),
23952405
),
23962406
);
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;
23972411
if (request.prompt !== undefined) {
23982412
yield* threadManagement
23992413
.dispatch({
@@ -2415,15 +2429,17 @@ const make = Effect.gen(function* () {
24152429
}),
24162430
text: request.prompt,
24172431
attachments: [],
2418-
modelSelection: target.modelSelection,
2432+
modelSelection: resolvedModelSelection,
24192433
dispatchMode: { type: "start_immediately" },
24202434
})
24212435
.pipe(
24222436
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+
),
24272443
),
24282444
);
24292445
}
@@ -2459,8 +2475,8 @@ const make = Effect.gen(function* () {
24592475
title: projection.thread.title,
24602476
createdBy: projection.thread.createdBy,
24612477
creationSource: projection.thread.creationSource,
2462-
providerInstanceId: target.modelSelection.instanceId,
2463-
model: target.modelSelection.model,
2478+
providerInstanceId: resolvedModelSelection.instanceId,
2479+
model: resolvedModelSelection.model,
24642480
} satisfies OrchestratorMcpCreatedThread;
24652481
}),
24662482
{ concurrency: 1 },

0 commit comments

Comments
 (0)