Skip to content

Commit 613d1ac

Browse files
onlydolemaria-rcksjuliusmarminge
authored
fix(mcp): background wait fails when none of its task IDs exist (#29773)
Co-authored-by: Taylor Dolezal <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent b1c1e07 commit 613d1ac

8 files changed

Lines changed: 276 additions & 10 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ const handlers = {
7575
Effect.mapError(
7676
(cause) =>
7777
new OrchestratorMcpFailure({
78-
code: cause.reason === "unsupported" ? "invalid_request" : "orchestration_error",
78+
code:
79+
cause.reason === "unsupported" || cause.reason === "unmatched"
80+
? "invalid_request"
81+
: "orchestration_error",
7982
message: cause.message,
8083
}),
8184
),

‎apps/server/src/orchestration-v2/ThreadManagementService.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,13 @@ it.effect("waitForThread returns the cut run once its continuation is declined",
462462
}),
463463
);
464464

465-
it.effect.each(["live", "no-session", "unsupported-runtime"] as const)(
465+
it.effect.each([
466+
"live",
467+
"no-match",
468+
"no-match-clear",
469+
"no-session",
470+
"unsupported-runtime",
471+
] as const)(
466472
"marks background work awaited on the thread's active provider thread with state %s",
467473
(state) =>
468474
Effect.gen(function* () {
@@ -538,7 +544,11 @@ it.effect.each(["live", "no-session", "unsupported-runtime"] as const)(
538544
const result = yield* Effect.gen(function* () {
539545
const service = yield* ThreadManagementService.ThreadManagementService;
540546
return yield* service
541-
.awaitBackgroundTasks({ threadId, taskIds: ["tests", "finished"], awaited: true })
547+
.awaitBackgroundTasks({
548+
threadId,
549+
taskIds: state.startsWith("no-match") ? ["finished"] : ["tests", "finished"],
550+
awaited: state !== "no-match-clear",
551+
})
542552
.pipe(Effect.result);
543553
}).pipe(Effect.provide(Layer.merge(layerTest, ThreadCommandExecutor.layer)));
544554

@@ -547,6 +557,14 @@ it.effect.each(["live", "no-session", "unsupported-runtime"] as const)(
547557
success: { matched: ["tests"], unmatched: ["finished"] },
548558
});
549559
expect(calls).toEqual([{ providerThreadId, awaited: true }]);
560+
} else if (state === "no-match") {
561+
expect(result).toMatchObject({
562+
failure: { _tag: "ThreadManagementBackgroundWaitError", reason: "unmatched" },
563+
});
564+
expect(calls).toEqual([{ providerThreadId, awaited: true }]);
565+
} else if (state === "no-match-clear") {
566+
expect(result).toMatchObject({ success: { matched: [], unmatched: ["finished"] } });
567+
expect(calls).toEqual([{ providerThreadId, awaited: false }]);
550568
} else {
551569
expect(result).toMatchObject({
552570
failure: {

‎apps/server/src/orchestration-v2/ThreadManagementService.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export class ThreadManagementBackgroundWaitError extends Schema.TaggedError<Thre
332332
"ThreadManagementBackgroundWaitError",
333333
{
334334
threadId: ThreadId,
335-
reason: Schema.Literals(["unsupported", "unavailable", "failed"]),
335+
reason: Schema.Literals(["unsupported", "unavailable", "failed", "unmatched"]),
336336
cause: Schema.optional(Schema.Defect()),
337337
},
338338
) {
@@ -344,6 +344,8 @@ export class ThreadManagementBackgroundWaitError extends Schema.TaggedError<Thre
344344
return "This thread has no live provider session.";
345345
case "failed":
346346
return "Failed to update the awaited background work.";
347+
case "unmatched":
348+
return "None of these IDs is running background work on this thread, so this request registers no wait for them. If the task already finished, read its output now. Otherwise pass the task ID of a background command or Monitor you started. Subagent, Agent tool, and delegate_task IDs don't go here.";
347349
}
348350
}
349351
}
@@ -1503,6 +1505,12 @@ const make = Effect.gen(function* () {
15031505
}),
15041506
),
15051507
);
1508+
if (input.awaited && matched.length === 0) {
1509+
return yield* new ThreadManagementBackgroundWaitError({
1510+
threadId: input.threadId,
1511+
reason: "unmatched",
1512+
});
1513+
}
15061514
const matchedIds = new Set(matched);
15071515
return { matched, unmatched: input.taskIds.filter((taskId) => !matchedIds.has(taskId)) };
15081516
});

‎evals/mcp-tool-use/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Cases are JSON lines in `cases/`, one file per area. The case schema in `run.ts`
2626

2727
- `kind`: `bad` reproduces a mistake, `guard` protects correct behavior, `control` is ordinary work.
2828
- `experience`: the real mistake, in a sentence. `source`: where it happened.
29-
- `setup`: the fake thread, its `world` (repo, branch, files, PRs, pages), `priorTurns`, and `mocks` for tool results.
29+
- `setup`: the fake thread, its `world` (repo, branch, files, PRs, pages), `priorTurns`, and `mocks` for tool results. With `world.backgroundTasks`, `t3_background_wait` answers from T3's own handler and ThreadManagementService, so its result and errors are the checkout's; a prior wait with `"result": "default"` gets that result too.
3030
- `setup.priorTurns[].toolCalls[].resultFile`: load a repository-relative file as the tool result instead of copying its contents into the case. An empty `prompt` resumes a top-level thread after its last prior tool result without adding a user message. Side-chat and subagent setups require a nonempty prompt.
3131
- `expect`: the checks, such as `calls`, `callsMustNot`, `maxCalls`, `order`, `shellMust`, `textMust`, `finalTextMust` (the last text, once the turn ended), and `linked`. A call's `args` match partially: `~text` is a case-insensitive substring, `"<=N"` / `">=N"` bound a number, `null` matches a missing key, and keys may be dotted paths. Device cases also use `device` to check every child brief, forbid raw UI commands, bound failed device actions per user turn, and require artifact paths in completed final answers. Secrets cases use `secret` (see [Secrets cases](#secrets-cases)).
3232

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
/**
3+
* T3's real `t3_background_wait` handler and ThreadManagementService over a Claude session.
4+
* Only the adapter is the eval's: like Claude's, it matches task IDs against the session's
5+
* running background work, which the case lists in `world.backgroundTasks`.
6+
*/
7+
import {
8+
EnvironmentId,
9+
type OrchestrationV2ProviderThread,
10+
type OrchestrationV2ThreadProjection,
11+
ProviderDriverKind,
12+
ProviderInstanceId,
13+
ProviderSessionId,
14+
ProviderThreadId,
15+
ThreadId,
16+
} from "@t3tools/contracts";
17+
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
18+
import * as Context from "effect/Context";
19+
import * as DateTime from "effect/DateTime";
20+
import * as Effect from "effect/Effect";
21+
import * as Layer from "effect/Layer";
22+
import * as References from "effect/References";
23+
// oxlint-disable-next-line t3code/no-raw-mcp-registration -- the eval calls the tool McpHttpServer registered and registers none.
24+
import { McpSchema, McpServer } from "effect/ai";
25+
26+
import * as McpHttpServer from "../../apps/server/src/mcp/McpHttpServer.ts";
27+
import * as McpInvocationContext from "../../apps/server/src/mcp/McpInvocationContext.ts";
28+
import { liveThreadShell } from "../../apps/server/src/mcp/McpToolAccess.testkit.ts";
29+
import * as OrchestratorMcpService from "../../apps/server/src/mcp/OrchestratorMcpService.ts";
30+
import * as ThreadMetadataMcpService from "../../apps/server/src/mcp/ThreadMetadataMcpService.ts";
31+
import * as OrchestratorHandlers from "../../apps/server/src/mcp/toolkits/orchestrator/handlers.ts";
32+
import { OrchestratorToolkit } from "../../apps/server/src/mcp/toolkits/orchestrator/tools.ts";
33+
import * as CommandReceiptStore from "../../apps/server/src/orchestration-v2/CommandReceiptStore.ts";
34+
import * as Orchestrator from "../../apps/server/src/orchestration-v2/Orchestrator.ts";
35+
import * as ProviderSessionManager from "../../apps/server/src/orchestration-v2/ProviderSessionManager.ts";
36+
import * as ThreadManagementService from "../../apps/server/src/orchestration-v2/ThreadManagementService.ts";
37+
import * as PeerForwarding from "../../apps/server/src/peer/PeerForwarding.ts";
38+
import * as PeerLinkRequests from "../../apps/server/src/peer/PeerLinkRequests.ts";
39+
import * as PeerLinks from "../../apps/server/src/peer/PeerLinks.ts";
40+
import * as RemoteDelegation from "../../apps/server/src/peer/RemoteDelegation.ts";
41+
import * as SqlitePersistence from "../../apps/server/src/persistence/Sqlite.ts";
42+
import * as ServerSettings from "../../apps/server/src/serverSettings.ts";
43+
44+
const threadId = ThreadId.make("mcp-tool-use-eval");
45+
const providerThreadId = ProviderThreadId.make("provider-thread:mcp-tool-use-eval");
46+
const providerSessionId = ProviderSessionId.make("provider-session:mcp-tool-use-eval");
47+
const providerInstanceId = ProviderInstanceId.make("claudeAgent");
48+
const driver = ProviderDriverKind.make("claudeAgent");
49+
50+
/** Calls T3's `t3_background_wait` with `running` as the session's running background task IDs. */
51+
export const makeBackgroundWait = Effect.fn("evaluateMcpToolUse.makeBackgroundWait")(function* (
52+
running: ReadonlyArray<string>,
53+
) {
54+
const now = yield* DateTime.now;
55+
const providerThread = {
56+
id: providerThreadId,
57+
driver,
58+
providerInstanceId,
59+
providerSessionId,
60+
appThreadId: threadId,
61+
nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" },
62+
pendingBackgroundTasks: running.map((taskId) => ({ taskId, kind: "command" })),
63+
createdAt: now,
64+
updatedAt: now,
65+
} as unknown as OrchestrationV2ProviderThread;
66+
const runtime = {
67+
driver,
68+
providerSessionId,
69+
awaitBackgroundTasks: (input: { readonly taskIds: ReadonlyArray<string> }) =>
70+
Effect.succeed({ matched: [...new Set(input.taskIds)].filter((id) => running.includes(id)) }),
71+
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime;
72+
const real = yield* Layer.build(
73+
ThreadManagementService.layer.pipe(
74+
Layer.provide(
75+
Layer.mergeAll(
76+
Layer.mock(Orchestrator.OrchestratorV2)({
77+
getThreadRecords: () =>
78+
Effect.succeed({
79+
thread: { id: threadId, deletedAt: null, activeProviderThreadId: providerThreadId },
80+
providerThreads: [providerThread],
81+
} as unknown as OrchestrationV2ThreadProjection),
82+
}),
83+
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
84+
get: () => Effect.succeedSome(runtime),
85+
}),
86+
ServerSettings.layerTest(),
87+
CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
88+
),
89+
),
90+
),
91+
// The receipt store's in-memory migrations log every step.
92+
).pipe(Effect.provideService(References.MinimumLogLevel, "Error"));
93+
const service = Context.get(real, ThreadManagementService.ThreadManagementService);
94+
// The caller's thread is live on the Claude instance, mid-turn.
95+
const threads = Layer.mock(ThreadManagementService.ThreadManagementService)({
96+
...service,
97+
getThreadShell: () => Effect.succeed({ ...liveThreadShell(threadId), providerInstanceId }),
98+
});
99+
const context = yield* Layer.build(
100+
McpHttpServer.toolkitRegistration(OrchestratorToolkit, OrchestratorHandlers.layer).pipe(
101+
Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
102+
Layer.provide(Layer.mock(OrchestratorMcpService.OrchestratorMcpService)({})),
103+
Layer.provide(Layer.mock(ThreadMetadataMcpService.ThreadMetadataMcpService)({})),
104+
Layer.provide(threads),
105+
Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})),
106+
Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})),
107+
Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})),
108+
Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})),
109+
),
110+
);
111+
type Entry = Parameters<McpServer.McpServer["Service"]["addTool"]>[0];
112+
// The registry keeps each tool's handler; its public type lists only definitions.
113+
const tools = Context.get(context, McpServer.McpServer).tools as ReadonlyArray<Entry>;
114+
const entry = tools.find((tool) => tool.tool.name === "t3_background_wait");
115+
if (entry === undefined) return yield* Effect.die(new Error("T3 has no t3_background_wait."));
116+
const scope: McpInvocationContext.McpThreadInvocationScope = {
117+
environmentId: EnvironmentId.make("00000000-0000-4000-8000-0000000000e0"),
118+
requestNamespace: "mcp-tool-use-eval",
119+
thread: { threadId, providerSessionId: "mcp-tool-use-eval", providerInstanceId },
120+
client: undefined,
121+
capabilities: new Set<McpInvocationContext.McpCapability>(["orchestration"]),
122+
issuedAt: 0,
123+
};
124+
const request = McpSchema.McpRequestContext.of({
125+
clientId: 0,
126+
protocolVersion: "2025-06-18",
127+
clientCapabilities: {},
128+
});
129+
return Effect.fn("evaluateMcpToolUse.backgroundWait")(function* (input: unknown) {
130+
const result = yield* entry.handle(input).pipe(
131+
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
132+
Effect.provideService(McpSchema.McpRequestContext, request),
133+
// An MCP client shows a protocol error, such as bad parameters, as the tool's error.
134+
Effect.catch((error) =>
135+
Effect.succeed(
136+
new McpSchema.CallToolResult({
137+
isError: true,
138+
content: [{ type: "text", text: error.message }],
139+
}),
140+
),
141+
),
142+
);
143+
if (!("content" in result))
144+
return yield* Effect.die(new Error("Background wait asked for input."));
145+
const text = result.content
146+
.flatMap((part) => (part.type === "text" ? [part.text] : []))
147+
.join("\n");
148+
if (text === "Tool execution failed due to an internal server error.") {
149+
return yield* Effect.die(new Error("t3_background_wait hit a defect in T3 or the eval."));
150+
}
151+
return { text, isError: result.isError === true };
152+
});
153+
});
154+
export type BackgroundWait = Effect.Success<ReturnType<typeof makeBackgroundWait>>;

0 commit comments

Comments
 (0)