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 21, 12:15 UTC
fix(server): MCP send errors say how to continue a finished thread (#31908)
Julius Marminge committed
Nov 14, 2026 at 19:43 UTC
fa6ecb125b6ee3b7c1b8e6b9789b5e619da9cd15
4 files changed
+73
-9
apps/server/src/mcp/OrchestratorMcpService.test.ts
+30
@@ -4428,6 +4428,36 @@ describe("OrchestratorMcpService thread lookup", () => {
4428
}),
4429
);
4430
4431
+ it.effect("tells an agent steering a finished thread to start a new turn instead", () =>
4432
+ Effect.gen(function* () {
4433
+ const sent = yield* Ref.make(0);
4434
+ return yield* Effect.gen(function* () {
4435
+ const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
4436
+ for (const mode of ["steer", "restart"] as const) {
4437
+ const error = yield* mcp
4438
+ .sendToThread(client, { threadId: launched.id, message: "hello", mode })
4439
+ .pipe(Effect.flip);
4440
+ assert.equal(error.code, "thread_not_sendable");
4441
+ assert.include(error.message, 'mode: "auto"');
4442
+ assert.include(error.message, '"queue"');
4443
+ }
4444
+ }).pipe(
4445
+ Effect.provide(
4446
+ service([launched], sent, {
4447
+ getProjectThreadRecords: () => Effect.succeed(idleThreadProjection(launched)),
4448
+ sendToThread: (input) =>
4449
+ Effect.fail(
4450
+ new ThreadManagementService.ThreadManagementNoSteerableRunError({
4451
+ threadId: input.threadId,
4452
+ mode: input.mode === "restart" ? "restart" : "steer",
4453
+ }),
4454
+ ),
4455
+ }),
4456
+ ),
4457
+ );
4458
+ }),
4459
+ );
4460
+
4461
it.effect("records the linked environment's thread that sent a message", () =>
4462
Effect.gen(function* () {
4463
const target = liveThreadShell(ThreadId.make("thread:target"));
apps/server/src/mcp/OrchestratorMcpService.ts
+5
-1
@@ -252,9 +252,13 @@ function threadManagementFailure(error: unknown): OrchestratorMcpFailure {
252
case "ThreadManagementRunNotFoundError":
253
return failure("run_not_found", error.message);
254
case "ThreadManagementThreadArchivedError":
255
- case "ThreadManagementNoSteerableRunError":
255
case "ThreadManagementWorktreeRestoreError":
256
return failure("thread_not_sendable", error.message);
257
+ case "ThreadManagementNoSteerableRunError":
258
+ return failure(
259
+ "thread_not_sendable",
260
+ `${error.message} Send with mode: "auto" or "queue" to start a new turn there.`,
261
+ );
262
case "ThreadManagementThreadNotInterruptibleError":
263
return failure("thread_not_interruptible", error.message);
264
case "ThreadManagementProjectionLoadError":
apps/server/src/mcp/threadAccess.test.ts
+17
-1
@@ -34,11 +34,12 @@ const linksHint =
34
const readBare = (
35
existing: ReadonlyArray<OrchestrationV2ThreadShell | ThreadId>,
36
scope: McpInvocationContext.McpInvocationScope = client,
37
+ threadId: ThreadId = bareId,
38
) => {
39
const shells = existing.map((shell) =>
40
typeof shell === "string" ? liveThreadShell(shell) : shell,
41
);
41
- return readThread(bareId).pipe(
42
+ return readThread(threadId).pipe(
43
Effect.flip,
44
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
45
Effect.provide(
@@ -101,3 +102,18 @@ it.effect("tells a caller with linked environments that the thread may be on one
102
expect(fenced.message).toBe("The thread was not found.");
103
}),
104
);
105
+
106
+it.effect("points an unencoded delegated-task id at the encoded thread it belongs to", () =>
107
+ Effect.gen(function* () {
108
+ const commandId = `command:mcp:${bareId}:delegate-task:0e7d2c41-6b9a-4f3e-8d15-a2c4b6e8f019`;
109
+ const encoded = ThreadId.make(`thread:delegated-task:${encodeURIComponent(commandId)}`);
110
+ const error = yield* readBare(
111
+ [encoded],
112
+ client,
113
+ ThreadId.make(`thread:delegated-task:${commandId}`),
114
+ );
115
+
116
+ expect(error.code).toBe("thread_not_found");
117
+ expect(error.message).toContain(`\`${encoded}\``);
118
+ }),
119
+);
apps/server/src/mcp/threadAccess.ts
+21
-7
@@ -21,11 +21,22 @@ import { resolveInteractionMode, resolveRuntimeMode } from "./delegatedTaskModes
21
import { fencingLinkOrigin } from "./linkOrigin.ts";
22
import * as McpInvocationContext from "./McpInvocationContext.ts";
23
24
+const delegatedTaskPrefix = "thread:delegated-task:";
25
+
26
+const suggestedThreadId = (threadId: ThreadId) => {
27
+ if (!threadId.startsWith(delegatedTaskPrefix)) return ThreadId.make(`mcp:${threadId}`);
28
+ const rest = threadId.slice(delegatedTaskPrefix.length);
29
+ // encodeURIComponent throws on lone surrogates, which a JSON string can carry.
30
+ if (!rest.isWellFormed()) return null;
31
+ const encoded = encodeURIComponent(rest);
32
+ return encoded === rest ? null : ThreadId.make(`${delegatedTaskPrefix}${encoded}`);
33
+};
34
+
35
/**
25
- * Threads an agent starts over MCP are `mcp:<uuid>`, and agents often paste the
26
- * bare uuid. The id is never rewritten, so permission checks keyed on it still
27
- * hold; the miss only names a live thread the caller could read by its full id.
28
- * Ids from another machine miss here too. The link hint is static: listing links probes peers.
36
+ * Agents paste bare uuids for `mcp:<uuid>` threads and unencoded delegated-task ids.
37
+ * The id is never rewritten, so permission checks keyed on it still hold; the miss
38
+ * only names a live thread the caller could read by its full id. Ids from another
39
+ * machine miss here too. The link hint is static: listing links probes peers.
40
*/
41
export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")(function* (
42
threads: Pick<ThreadManagement.ThreadManagementService["Service"], "getThreadShell">,
@@ -33,10 +44,13 @@ export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")
44
threadId: ThreadId,
45
message: string,
46
) {
36
- const prefixed = ThreadId.make(`mcp:${threadId}`);
37
- const shell = yield* threads.getThreadShell(prefixed).pipe(Effect.orElseSucceed(() => null));
47
+ const suggested = suggestedThreadId(threadId);
48
+ const shell =
49
+ suggested === null
50
+ ? null
51
+ : yield* threads.getThreadShell(suggested).pipe(Effect.orElseSucceed(() => null));
52
const mcpHint =
39
- shell === null || shell.deletedAt !== null ? "" : ` Did you mean \`${prefixed}\`?`;
53
+ shell === null || shell.deletedAt !== null ? "" : ` Did you mean \`${suggested}\`?`;
54
const caller =
55
scope.thread === undefined
56
? null