Skip to content

Commit 6f3e626

Browse files
fix(mcp): preserve fork projection failures
1 parent 006bb81 commit 6f3e626

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Effect from "effect/Effect";
1111
import * as Layer from "effect/Layer";
1212
import * as Ref from "effect/Ref";
1313

14+
import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
1415
import { ThreadManagementService } from "../orchestration-v2/ThreadManagementService.ts";
1516
import type { McpInvocationScope } from "./McpInvocationContext.ts";
1617
import * as ConversationTransfer from "./ConversationTransferMcpService.ts";
@@ -50,11 +51,12 @@ function testLayer(input: {
5051
readonly parent: OrchestrationV2ThreadProjection;
5152
readonly source: OrchestrationV2ThreadProjection;
5253
readonly dispatch: ThreadManagementService["Service"]["dispatch"];
54+
readonly getThreadProjection?: ThreadManagementService["Service"]["getThreadProjection"];
5355
}) {
5456
return ConversationTransfer.layer.pipe(
5557
Layer.provide(
5658
Layer.mock(ThreadManagementService)({
57-
getThreadProjection: () => Effect.succeed(input.parent),
59+
getThreadProjection: input.getThreadProjection ?? (() => Effect.succeed(input.parent)),
5860
getProjectThread: ({ threadId }) =>
5961
Effect.succeed(threadId === parentThreadId ? input.parent : input.source),
6062
dispatch: input.dispatch,
@@ -171,4 +173,49 @@ describe("ConversationTransferMcpService", () => {
171173
assert.equal(error.code, "capability_denied");
172174
}),
173175
);
176+
177+
it.effect(
178+
"does not treat an operational target projection failure as an absent fork target",
179+
() =>
180+
Effect.gen(function* () {
181+
const dispatched = yield* Ref.make(0);
182+
const parent = projection({
183+
threadId: parentThreadId,
184+
runtimeMode: "full-access",
185+
interactionMode: "default",
186+
});
187+
const error = yield* Effect.gen(function* () {
188+
const service = yield* ConversationTransfer.ConversationTransferMcpService;
189+
return yield* service
190+
.fork(scope(), {
191+
sourcePoint: { type: "latest_stable" },
192+
clientRequestId: "projection-read-failure",
193+
})
194+
.pipe(Effect.flip);
195+
}).pipe(
196+
Effect.provide(
197+
testLayer({
198+
parent,
199+
source: parent,
200+
getThreadProjection: (threadId) =>
201+
threadId === parentThreadId
202+
? Effect.succeed(parent)
203+
: Effect.fail(
204+
new OrchestratorProjectionError({
205+
threadId,
206+
cause: new Error("storage unavailable"),
207+
}),
208+
),
209+
dispatch: () =>
210+
Ref.update(dispatched, (count) => count + 1).pipe(
211+
Effect.andThen(Effect.die("dispatch must not run after a failed target read")),
212+
),
213+
}),
214+
),
215+
);
216+
217+
assert.equal(error.code, "orchestration_error");
218+
assert.equal(yield* Ref.get(dispatched), 0);
219+
}),
220+
);
174221
});

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as DateTime from "effect/DateTime";
2020
import * as Effect from "effect/Effect";
2121
import * as Layer from "effect/Layer";
2222
import * as Option from "effect/Option";
23+
import * as Predicate from "effect/Predicate";
2324
import * as Schema from "effect/Schema";
2425

2526
import type { OrchestratorV2DispatchResult } from "../orchestration-v2/Orchestrator.ts";
@@ -45,6 +46,14 @@ export class ConversationTransferMcpService extends Context.Service<
4546

4647
const isThreadNotFound = Schema.is(ThreadManagementThreadNotFoundError);
4748

49+
function isProjectionNotFound(error: unknown): boolean {
50+
return (
51+
Predicate.hasProperty(error, "cause") &&
52+
Predicate.hasProperty(error.cause, "_tag") &&
53+
error.cause._tag === "ProjectionStoreThreadNotFoundError"
54+
);
55+
}
56+
4857
function failure(code: OrchestratorMcpFailure["code"], message: string): OrchestratorMcpFailure {
4958
return new OrchestratorMcpFailure({ code, message });
5059
}
@@ -261,7 +270,19 @@ export const make = Effect.gen(function* () {
261270
yield* requireCapability(scope);
262271
const commandId = forkCommandId(scope, input.clientRequestId);
263272
const targetId = targetThreadId(scope, input.clientRequestId);
264-
const existingTarget = yield* Effect.option(threads.getThreadProjection(targetId));
273+
const existingTarget = yield* threads.getThreadProjection(targetId).pipe(
274+
Effect.map(Option.some),
275+
Effect.catch((error) =>
276+
isProjectionNotFound(error)
277+
? Effect.succeed(Option.none())
278+
: Effect.fail(
279+
failure(
280+
"orchestration_error",
281+
`Unable to inspect the fork target before dispatch: ${errorMessage(error)}`,
282+
),
283+
),
284+
),
285+
);
265286
const existingTransfer = Option.isSome(existingTarget)
266287
? existingTarget.value.contextTransfers.find(
267288
(transfer) => transfer.type === "fork" && transfer.targetThreadId === targetId,

0 commit comments

Comments
 (0)