Skip to content

Commit 8925e8e

Browse files
mimenclaude
andcommitted
fix(mcp): return the raced thread on a clientRequestId retry
A retry with the same clientRequestId could dispatch before the first call's thread reached the projection. The deterministic id then failed the thread-absent invariant and the caller saw ThreadStartFailedError instead of the thread it asked for. When the dispatch fails under a clientRequestId, re-read the thread; if it now exists, return it with alreadyStarted: true. Without a key the failure still propagates. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 9a36178 commit 8925e8e

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎apps/server/src/mcp/toolkits/threads/handlers.test.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ interface HarnessOptions {
9696
readonly project?: OrchestrationProjectShell | null;
9797
readonly existing?: ReadonlyMap<string, OrchestrationThreadShell>;
9898
readonly failDispatch?: boolean;
99+
readonly appearsAfterFailedDispatch?: OrchestrationThreadShell;
99100
}
100101

101102
const makeHarness = Effect.fn("makeThreadsToolkitHarness")(function* (
@@ -104,7 +105,7 @@ const makeHarness = Effect.fn("makeThreadsToolkitHarness")(function* (
104105
const commands = yield* Ref.make<ReadonlyArray<TurnStart>>([]);
105106
const thread = options.thread === undefined ? makeThread() : options.thread;
106107
const project = options.project === undefined ? makeProject() : options.project;
107-
const existing = options.existing ?? new Map();
108+
const existing = new Map(options.existing ?? []);
108109
const dependencies = Layer.mergeAll(
109110
Layer.mock(ProjectionSnapshotQuery)({
110111
getThreadShellById: (threadId) =>
@@ -116,12 +117,20 @@ const makeHarness = Effect.fn("makeThreadsToolkitHarness")(function* (
116117
getProjectShellById: () => Effect.succeed(Option.fromNullishOr(project)),
117118
}),
118119
Layer.mock(ThreadBootstrap)({
119-
dispatchTurnStart: (command) =>
120-
options.failDispatch
120+
dispatchTurnStart: (command) => {
121+
const raced = options.appearsAfterFailedDispatch;
122+
if (raced) {
123+
existing.set(raced.id, raced);
124+
return Effect.fail(
125+
new OrchestrationDispatchCommandError({ message: "Thread already exists." }),
126+
);
127+
}
128+
return options.failDispatch
121129
? Effect.fail(new OrchestrationDispatchCommandError({ message: "worktree exists" }))
122130
: Ref.update(commands, (recorded) => [...recorded, command]).pipe(
123131
Effect.as({ sequence: 1 }),
124-
),
132+
);
133+
},
125134
}),
126135
Layer.succeed(Crypto.Crypto, testCrypto),
127136
);
@@ -316,6 +325,23 @@ describe("threads toolkit handlers", () => {
316325
}),
317326
);
318327

328+
it.effect("returns the thread a concurrent call created under the same clientRequestId", () =>
329+
Effect.gen(function* () {
330+
const probe = yield* makeHarness();
331+
const created = yield* probe.call({ prompt: PROMPT, clientRequestId: "req-1" });
332+
const raced = makeThread({ id: ThreadId.make(created.threadId), title: "Raced" });
333+
const harness = yield* makeHarness({ appearsAfterFailedDispatch: raced });
334+
const result = yield* harness.call({ prompt: PROMPT, clientRequestId: "req-1" });
335+
expect(result.threadId).toBe(created.threadId);
336+
expect(result.title).toBe("Raced");
337+
expect(result.alreadyStarted).toBe(true);
338+
339+
const noKey = yield* makeHarness({ appearsAfterFailedDispatch: raced });
340+
const error = yield* noKey.call({ prompt: PROMPT }).pipe(Effect.flip);
341+
expect(error._tag).toBe("ThreadStartFailedError");
342+
}),
343+
);
344+
319345
it.effect("rejects an archived caller and a missing project", () =>
320346
Effect.gen(function* () {
321347
const archived = yield* makeHarness({

‎apps/server/src/mcp/toolkits/threads/handlers.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const make = Effect.gen(function* () {
167167
createdAt,
168168
};
169169

170-
yield* bootstrap
170+
const alreadyStarted = yield* bootstrap
171171
.dispatchTurnStart({
172172
type: "thread.turn.start",
173173
commandId,
@@ -194,11 +194,21 @@ const make = Effect.gen(function* () {
194194
? Effect.failCause(cause as Cause.Cause<never>)
195195
: Effect.fail(new ThreadStartFailedError({ cause })),
196196
),
197+
Effect.as(false),
198+
requestKey === undefined
199+
? (effect) => effect
200+
: Effect.catchTag("ThreadStartFailedError", (error) =>
201+
readThread(threadId).pipe(
202+
Effect.flatMap((thread) =>
203+
Option.isSome(thread) ? Effect.succeed(true) : Effect.fail(error),
204+
),
205+
),
206+
),
197207
);
198208

199209
const started = yield* readThread(threadId);
200210
return Option.isSome(started)
201-
? resultOf(started.value, false)
211+
? resultOf(started.value, alreadyStarted)
202212
: resultOf({ id: threadId, ...createThread }, false);
203213
});
204214

0 commit comments

Comments
 (0)