Skip to content

Commit 2b830d4

Browse files
cliffhallclaude
andcommitted
fix(everything): bound the answers a task keeps; correct the task-limit comment
Copilot round 9 on #5131: - tasks/update refuses answers over MAX_ANSWER_BYTES (64 KiB serialized) with -32602, leaving the task unchanged. With MAX_TASKS this bounds what the task store holds, where the transports alone accept 10 MiB messages. - The readOnlyHint comment on simulate-research-query no longer says a full store drops its oldest task (round 7 made it refuse a new one instead). Part of #4852 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 0d01ee5 commit 2b830d4

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎src/everything/__tests__/tasks.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
* `tasks/result` and `tasks/list`, which the extension does not define.
2323
*/
2424
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
25-
import { clearTasks, MAX_TASKS, TASKS_EXTENSION } from "../server/tasks.js";
25+
import {
26+
clearTasks,
27+
MAX_ANSWER_BYTES,
28+
MAX_TASKS,
29+
TASKS_EXTENSION,
30+
} from "../server/tasks.js";
2631
import { STAGE_DURATION, STAGES } from "../tools/simulate-research-query.js";
2732
import { connectRaw as rawClient, type RawSession } from "./harness.js";
2833

@@ -179,6 +184,32 @@ describe("the extension", () => {
179184
expect(result.isError).toBe(true);
180185
});
181186

187+
it("refuses answers over MAX_ANSWER_BYTES with -32602, leaving the task waiting", async () => {
188+
peer = await rawClient({ ...TASKS, sampling: {} });
189+
const created = await peer.call("trigger-sampling-request-async", {
190+
prompt: "p",
191+
});
192+
const response = await peer.request("tasks/update", {
193+
taskId: created.taskId,
194+
inputResponses: {
195+
"sampling-1": {
196+
role: "assistant",
197+
model: "m",
198+
content: { type: "text", text: "x".repeat(MAX_ANSWER_BYTES) },
199+
},
200+
},
201+
});
202+
expect(response.error).toMatchObject({
203+
code: -32602,
204+
message: expect.stringContaining(
205+
`over the ${MAX_ANSWER_BYTES}-byte limit; the task is unchanged`,
206+
),
207+
});
208+
const task = await peer.get(created.taskId as string);
209+
expect(task.status).toBe("input_required");
210+
expect(Object.keys(task.inputRequests as object)).toEqual(["sampling-1"]);
211+
});
212+
182213
it("refuses tasks/update without inputResponses with -32602", async () => {
183214
peer = await rawClient(TASKS);
184215
const created = await peer.call("simulate-research-query", { topic: "t" });

‎src/everything/docs/features.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ Logging is deprecated as of 2026-07-28 (SEP-2577) and remains available on both
9494
- `tasks/update`: the client's `inputResponses` to the outstanding input requests;
9595
- `tasks/cancel`: stop a task that has not finished.
9696

97-
A task tool called by a client whose request does not declare the extension, or the capability its task asks for input with (`sampling` for `trigger-sampling-request-async`, form-mode `elicitation` for `trigger-elicitation-request-async`), is refused with `-32021` (missing required client capability), naming what is missing in `data.requiredCapabilities`; so is a `tasks/*` request from a client without the extension. An unknown or expired task is refused with `-32602`. A `tasks/cancel` is acknowledged whatever the task's status (cancellation is cooperative), and a finished task stays as it ended. An update's answers to requests that are not outstanding (never issued, already answered, or sent while the task is not waiting for input) are ignored, as the extension asks. Each input request a task makes has its own key, never reused once answered, so a request asked again after an answer that does not fit gets a new key (`form-2`, `sampling-2`). The process keeps at most 100 tasks, each until its TTL; at that limit a new task is refused with a tool error. The task id is a server-minted UUID, so a task lives across requests (each 2026-07-28 HTTP request is served by a fresh server instance) and is reached only through its handle. A task's progress is computed from the clock when it is read, so nothing runs in the background.
97+
A task tool called by a client whose request does not declare the extension, or the capability its task asks for input with (`sampling` for `trigger-sampling-request-async`, form-mode `elicitation` for `trigger-elicitation-request-async`), is refused with `-32021` (missing required client capability), naming what is missing in `data.requiredCapabilities`; so is a `tasks/*` request from a client without the extension. An unknown or expired task, and a `tasks/update` whose answers exceed 64 KiB serialized, are refused with `-32602`. A `tasks/cancel` is acknowledged whatever the task's status (cancellation is cooperative), and a finished task stays as it ended. An update's answers to requests that are not outstanding (never issued, already answered, or sent while the task is not waiting for input) are ignored, as the extension asks. Each input request a task makes has its own key, never reused once answered, so a request asked again after an answer that does not fit gets a new key (`form-2`, `sampling-2`). The process keeps at most 100 tasks, each until its TTL; at that limit a new task is refused with a tool error. The task id is a server-minted UUID, so a task lives across requests (each 2026-07-28 HTTP request is served by a fresh server instance) and is reached only through its handle. A task's progress is computed from the clock when it is read, so nothing runs in the background.
9898

9999
The 2025-11-25 core task methods (`tasks/result`, `tasks/list`, task-augmented requests) are not served: SDK v2 removed the experimental layer they were built on.

‎src/everything/server/tasks.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ const tasks = new Map<string, StoredTask>();
105105
*/
106106
export const MAX_TASKS = 100;
107107

108+
/**
109+
* The most bytes of answers, serialized, one `tasks/update` may hand a task.
110+
* A task keeps its answers until its TTL, and a transport accepts messages
111+
* far larger than any of these tasks needs (10 MiB on stdio), so with
112+
* `MAX_TASKS` this bounds what the store holds.
113+
*/
114+
export const MAX_ANSWER_BYTES = 64 * 1024;
115+
108116
/** Drop every task past its TTL. */
109117
function sweep(now: number): void {
110118
for (const [taskId, task] of tasks) {
@@ -405,6 +413,13 @@ export function registerTasksExtension(server: McpServer): void {
405413
outstanding.includes(key),
406414
),
407415
);
416+
const size = JSON.stringify(answered).length;
417+
if (size > MAX_ANSWER_BYTES) {
418+
throw new ProtocolError(
419+
ProtocolErrorCode.InvalidParams,
420+
`Invalid params for tasks/update: the answers are ${size} bytes, over the ${MAX_ANSWER_BYTES}-byte limit; the task is unchanged`,
421+
);
422+
}
408423
if (Object.keys(answered).length > 0) {
409424
task.behavior.receive(answered, now);
410425
}

‎src/everything/tools/simulate-research-query.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const config = {
7171
"input_required until the client answers its clarification request with tasks/update.",
7272
inputSchema: SimulateResearchQuerySchema,
7373
annotations: {
74-
// Each call creates a task, and a full task store drops its oldest one.
74+
// Each call creates a task in the process-wide task store.
7575
readOnlyHint: false,
7676
destructiveHint: false,
7777
idempotentHint: false,

0 commit comments

Comments
 (0)