Skip to content

Commit 4416643

Browse files
cliffhallclaude
andcommitted
fix(everything): tasks/update ignores non-outstanding answers; bound minted resources
Copilot round 3 on #5131: - minted resources: the process keeps at most 100 and 64 MiB of content, dropping the oldest, so a burst of gzip-file-as-resource calls cannot exhaust memory before the ten-minute TTL frees anything. - tasks/update ignores answers to requests that are not outstanding (never issued, already answered, or sent while the task is not waiting) instead of refusing the update, as the extension's spec says a server SHOULD. - A re-asked request gets a new key (form-2, sampling-2): the spec forbids reusing a key once a response for it was delivered. - simulate-research-query takes a clarification only if it is one of the interpretations it offered. - memory: a test pins that a 2026-07-28 resources/subscribe gets -32601 from the SDK (the round's memory finding, declined). Part of #4852 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent d19037a commit 4416643

9 files changed

Lines changed: 220 additions & 76 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import { instancePublisher } from "../server/context.js";
3737
import { STATIC_DOCUMENT_CACHE_HINT } from "../resources/files.js";
3838
import {
3939
clearMintedResources,
40+
MAX_MINTED_BYTES,
41+
MAX_MINTED_RESOURCES,
4042
MINTED_RESOURCE_TTL_MS,
43+
mintBlobResource,
4144
} from "../resources/minted.js";
4245
import { LOGGING_LEVELS } from "../tools/trigger-log-messages.js";
4346
import {
@@ -601,6 +604,35 @@ describe("minted resources", () => {
601604
});
602605
});
603606

607+
it("keeps at most MAX_MINTED_RESOURCES, dropping the oldest", async () => {
608+
const first = mintBlobResource("first", "text/plain", "AA==").uri;
609+
for (let i = 1; i < MAX_MINTED_RESOURCES; i++) {
610+
mintBlobResource(`r${i}`, "text/plain", "AA==");
611+
}
612+
session = await connectModern();
613+
expect(await session.client.readResource({ uri: first })).toMatchObject({
614+
contents: [{ uri: first }],
615+
});
616+
mintBlobResource("one-more", "text/plain", "AA==");
617+
await expect(
618+
session.client.readResource({ uri: first }),
619+
).rejects.toMatchObject({ code: -32602 });
620+
});
621+
622+
it("keeps at most MAX_MINTED_BYTES of content, dropping the oldest", async () => {
623+
const half = "A".repeat(MAX_MINTED_BYTES / 2);
624+
const first = mintBlobResource("first", "text/plain", half).uri;
625+
const second = mintBlobResource("second", "text/plain", half).uri;
626+
mintBlobResource("third", "text/plain", "AA==");
627+
session = await connectModern();
628+
await expect(
629+
session.client.readResource({ uri: first }),
630+
).rejects.toMatchObject({ code: -32602 });
631+
expect(
632+
(await session.client.readResource({ uri: second })).contents,
633+
).toHaveLength(1);
634+
});
635+
604636
it("is not served to a 2025-era client", async () => {
605637
session = await connect();
606638
const { resourceTemplates } = await session.client.listResourceTemplates();

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

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -353,32 +353,63 @@ describe("simulate-research-query", () => {
353353
);
354354
});
355355

356-
it("refuses an answer to a request it did not make", async () => {
356+
it("ignores an answer to a request it did not make, and keeps waiting", async () => {
357357
const { taskId } = await toClarification("t");
358-
const response = await peer.request("tasks/update", {
358+
expect(
359+
await peer.result("tasks/update", {
360+
taskId,
361+
inputResponses: { other: { action: "accept" } },
362+
}),
363+
).toMatchObject({ resultType: "complete" });
364+
expect((await peer.get(taskId)).status).toBe("input_required");
365+
});
366+
367+
it("uses only the outstanding answer from an update that also carries others", async () => {
368+
const { taskId } = await toClarification("python");
369+
await peer.result("tasks/update", {
359370
taskId,
360-
inputResponses: { other: { action: "accept" } },
371+
inputResponses: {
372+
other: { action: "decline" },
373+
clarification: {
374+
action: "accept",
375+
content: { interpretation: "snake" },
376+
},
377+
},
361378
});
362-
expect(response.error).toMatchObject({
363-
code: -32602,
364-
message: expect.stringContaining(
365-
"has no outstanding input request other; it is waiting for clarification",
366-
),
379+
advance(STAGE_DURATION * 2);
380+
expect(resultText(await peer.get(taskId))).toMatch(
381+
/^# Research Report: python \(snake\)\n/,
382+
);
383+
});
384+
385+
it("does not take an interpretation it did not offer", async () => {
386+
const { taskId } = await toClarification("python");
387+
await peer.result("tasks/update", {
388+
taskId,
389+
inputResponses: {
390+
clarification: {
391+
action: "accept",
392+
content: { interpretation: "astrology" },
393+
},
394+
},
367395
});
396+
advance(STAGE_DURATION * 2);
397+
expect(resultText(await peer.get(taskId))).toContain(
398+
"No usable answer - using default interpretation",
399+
);
368400
});
369401
});
370402

371-
it("refuses tasks/update while the task is working", async () => {
403+
it("acknowledges and ignores tasks/update while the task is working", async () => {
372404
peer = await rawClient(TASKS);
373405
const created = await peer.call("simulate-research-query", { topic: "t" });
374-
const response = await peer.request("tasks/update", {
375-
taskId: created.taskId,
376-
inputResponses: {},
377-
});
378-
expect(response.error).toMatchObject({
379-
code: -32602,
380-
message: expect.stringContaining("is working, not waiting for input"),
381-
});
406+
expect(
407+
await peer.result("tasks/update", {
408+
taskId: created.taskId,
409+
inputResponses: { clarification: { action: "accept" } },
410+
}),
411+
).toMatchObject({ resultType: "complete" });
412+
expect((await peer.get(created.taskId as string)).status).toBe("working");
382413
});
383414

384415
it("stays cancelled once the client cancels it, and refuses a second cancel", async () => {
@@ -426,7 +457,7 @@ describe("trigger-sampling-request-async", () => {
426457
statusMessage: "Waiting for the client's LLM to respond",
427458
ttlMs: 600000,
428459
inputRequests: {
429-
sampling: {
460+
"sampling-1": {
430461
method: "sampling/createMessage",
431462
params: {
432463
maxTokens: 100,
@@ -448,7 +479,7 @@ describe("trigger-sampling-request-async", () => {
448479
await peer.result("tasks/update", {
449480
taskId,
450481
inputResponses: {
451-
sampling: {
482+
"sampling-1": {
452483
role: "assistant",
453484
model: "test-model",
454485
content: { type: "text", text: "hi" },
@@ -461,18 +492,18 @@ describe("trigger-sampling-request-async", () => {
461492
expect(resultText(done)).toContain('"text": "hi"');
462493
});
463494

464-
it("keeps waiting after an answer of the wrong kind", async () => {
495+
it("asks again, under a new key, after an answer of the wrong kind", async () => {
465496
peer = await rawClient({ ...TASKS, sampling: {} });
466497
const created = await peer.call("trigger-sampling-request-async", {
467498
prompt: "p",
468499
});
469500
await peer.result("tasks/update", {
470501
taskId: created.taskId,
471-
inputResponses: { sampling: { action: "accept" } },
502+
inputResponses: { "sampling-1": { action: "accept" } },
472503
});
473-
expect((await peer.get(created.taskId as string)).status).toBe(
474-
"input_required",
475-
);
504+
const again = await peer.get(created.taskId as string);
505+
expect(again.status).toBe("input_required");
506+
expect(Object.keys(again.inputRequests as object)).toEqual(["sampling-2"]);
476507
});
477508
});
478509

@@ -484,7 +515,7 @@ describe("trigger-elicitation-request-async", () => {
484515
status: "input_required",
485516
statusMessage: "Waiting for the user's input",
486517
inputRequests: {
487-
form: {
518+
"form-1": {
488519
method: "elicitation/create",
489520
params: { mode: "form", requestedSchema: { required: ["name"] } },
490521
},
@@ -493,7 +524,7 @@ describe("trigger-elicitation-request-async", () => {
493524
const taskId = created.taskId as string;
494525
await peer.result("tasks/update", {
495526
taskId,
496-
inputResponses: { form: response },
527+
inputResponses: { "form-1": response },
497528
});
498529
return peer.get(taskId);
499530
}
@@ -524,7 +555,7 @@ describe("trigger-elicitation-request-async", () => {
524555
const answeredAt = new Date(Date.now()).toISOString();
525556
await peer.result("tasks/update", {
526557
taskId: created.taskId,
527-
inputResponses: { form: { action: "decline" } },
558+
inputResponses: { "form-1": { action: "decline" } },
528559
});
529560
advance(60_000);
530561
expect((await peer.get(created.taskId as string)).lastUpdatedAt).toBe(
@@ -537,13 +568,21 @@ describe("trigger-elicitation-request-async", () => {
537568
["a blank name", { name: "" }],
538569
["a color that is not a choice", { name: "Ada", favoriteColor: "mauve" }],
539570
])(
540-
"keeps waiting after an accepted answer with %s",
571+
"asks again, under a new key, after an accepted answer with %s",
541572
async (_label, content) => {
542573
peer = await rawClient(WITH_ELICITATION);
543574
const created = await peer.call("trigger-elicitation-request-async");
544575
await peer.result("tasks/update", {
545576
taskId: created.taskId,
546-
inputResponses: { form: { action: "accept", content } },
577+
inputResponses: { "form-1": { action: "accept", content } },
578+
});
579+
const again = await peer.get(created.taskId as string);
580+
expect(again.status).toBe("input_required");
581+
expect(Object.keys(again.inputRequests as object)).toEqual(["form-2"]);
582+
// The spent key is no longer outstanding, so an answer to it is ignored.
583+
await peer.result("tasks/update", {
584+
taskId: created.taskId,
585+
inputResponses: { "form-1": { action: "decline" } },
547586
});
548587
expect((await peer.get(created.taskId as string)).status).toBe(
549588
"input_required",
@@ -556,15 +595,14 @@ describe("trigger-elicitation-request-async", () => {
556595
expect(resultText(done)).toBe(`Elicitation ${action}.`);
557596
});
558597

559-
it("keeps waiting after an answer of the wrong kind", async () => {
598+
it("asks again, under a new key, after an answer of the wrong kind", async () => {
560599
peer = await rawClient(WITH_ELICITATION);
561600
const created = await peer.call("trigger-elicitation-request-async");
562601
await peer.result("tasks/update", {
563602
taskId: created.taskId,
564-
inputResponses: { form: { roots: [] } },
603+
inputResponses: { "form-1": { roots: [] } },
565604
});
566-
expect((await peer.get(created.taskId as string)).status).toBe(
567-
"input_required",
568-
);
605+
const again = await peer.get(created.taskId as string);
606+
expect(Object.keys(again.inputRequests as object)).toEqual(["form-2"]);
569607
});
570608
});

‎src/everything/docs/features.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ On a 2025-era connection the tools that need a client capability are registered
6969
- Dynamic Blob: `demo://resource/dynamic/blob/{index}` (base64 payload generated on the fly)
7070
- Static Documents: `demo://resource/static/document/<filename>` (serves files from `src/everything/docs/` as static file-based resources; a 2026-07-28 read carries a one-hour public cache hint)
7171
- Session Scoped, **2025-era**: `demo://resource/session/<name>` (per-session resources registered dynamically; available only for the lifetime of the session)
72-
- Minted, **2026-07-28**: `demo://resource/minted/<id>/<name>` (resources a tool created, under a server-minted id; readable by any client that has the URI, for ten minutes)
72+
- Minted, **2026-07-28**: `demo://resource/minted/<id>/<name>` (resources a tool created, under a server-minted id; readable by any client that has the URI, for ten minutes; the process keeps at most 100 of them and 64 MiB of content, dropping the oldest first)
7373

7474
A read of a resource that does not exist (an unknown URI, a template id that is not a positive integer, an expired minted resource) is answered with `-32602` and the URI in the error's `data`.
7575

@@ -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, an update to a task that is not waiting for input or that answers a request it did not make, and a cancel of a finished task are refused with `-32602`. 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 cancel of a finished task are refused with `-32602`. 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 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/resources/minted.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ import {
2424
/** How long a minted resource stays readable, in milliseconds. */
2525
export const MINTED_RESOURCE_TTL_MS = 10 * 60_000;
2626

27+
/**
28+
* The most minted resources the process keeps, and the most bytes of content
29+
* across them. Each one may be as large as the gzip tool's fetch limit, so
30+
* without a budget a burst of calls could exhaust memory before the TTL frees
31+
* anything; past it, the oldest are dropped first.
32+
*/
33+
export const MAX_MINTED_RESOURCES = 100;
34+
export const MAX_MINTED_BYTES = 64 * 1024 * 1024;
35+
2736
/** The URI template every minted resource matches. */
2837
const mintedUriTemplate = "demo://resource/minted/{id}/{name}";
2938

@@ -44,6 +53,20 @@ function sweep(now: number): void {
4453
}
4554
}
4655

56+
/**
57+
* Drop the oldest minted resources (the map keeps insertion order) until one
58+
* more of `bytes` fits both budgets.
59+
*/
60+
function makeRoom(bytes: number): void {
61+
let total = bytes;
62+
for (const resource of minted.values()) total += resource.blob.length;
63+
for (const [uri, resource] of minted) {
64+
if (minted.size < MAX_MINTED_RESOURCES && total <= MAX_MINTED_BYTES) break;
65+
minted.delete(uri);
66+
total -= resource.blob.length;
67+
}
68+
}
69+
4770
/**
4871
* Store a blob resource under a freshly minted URI, and return a link to it.
4972
*
@@ -58,6 +81,7 @@ export function mintBlobResource(
5881
): ResourceLink {
5982
const now = Date.now();
6083
sweep(now);
84+
makeRoom(blob.length);
6185
const uri = `demo://resource/minted/${randomUUID()}/${encodeURIComponent(name)}`;
6286
minted.set(uri, {
6387
name,

‎src/everything/server/tasks.ts‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,11 @@ const EmptyResult = z.object({});
334334
* Serve the extension's methods on a 2026-07-28 server: `tasks/get`,
335335
* `tasks/update` and `tasks/cancel`. A request from a client that does not
336336
* declare the extension is refused with -32021. An unknown or expired task
337-
* id, and an update or cancellation that does not fit the task's status, is
338-
* refused with -32602 (invalid params).
337+
* id, and a cancellation of a finished task, is refused with -32602 (invalid
338+
* params). An update's answers to requests that are not outstanding are
339+
* ignored. Each input request a task makes has a key of its own, never reused
340+
* once answered (the extension requires it), so a re-asked request gets a
341+
* new key.
339342
*
340343
* @param {McpServer} server - The 2026-07-28 server to serve them on.
341344
*/
@@ -362,23 +365,22 @@ export function registerTasksExtension(server: McpServer): void {
362365
const now = Date.now();
363366
const task = find(taskId, now);
364367
const state = stateOf(task, now);
365-
if (state.status !== "input_required") {
366-
throw new ProtocolError(
367-
ProtocolErrorCode.InvalidParams,
368-
`Task ${taskId} is ${state.status}, not waiting for input`,
369-
);
370-
}
371-
const outstanding = Object.keys(state.inputRequests);
372-
const unknown = Object.keys(inputResponses).filter(
373-
(key) => !outstanding.includes(key),
368+
// Only answers to requests outstanding now are passed on. The rest (a
369+
// key never issued, one already answered, or any answer to a task not
370+
// waiting for input) are ignored, as the extension asks, so a client
371+
// that resends or batches responses is not failed for it.
372+
const outstanding =
373+
state.status === "input_required"
374+
? Object.keys(state.inputRequests)
375+
: [];
376+
const answered = Object.fromEntries(
377+
Object.entries(inputResponses).filter(([key]) =>
378+
outstanding.includes(key),
379+
),
374380
);
375-
if (unknown.length > 0) {
376-
throw new ProtocolError(
377-
ProtocolErrorCode.InvalidParams,
378-
`Task ${taskId} has no outstanding input request ${unknown.join(", ")}; it is waiting for ${outstanding.join(", ")}`,
379-
);
381+
if (Object.keys(answered).length > 0) {
382+
task.behavior.receive(answered, now);
380383
}
381-
task.behavior.receive(inputResponses, now);
382384
return {};
383385
},
384386
);

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ const config = {
7373
},
7474
};
7575

76-
/** The clarification question's answer. */
77-
const ClarificationSchema = z.object({ interpretation: z.string() });
78-
7976
/**
8077
* Returns contextual interpretation options based on the topic.
8178
*/
@@ -99,8 +96,18 @@ function getInterpretationsForTopic(
9996
];
10097
}
10198

102-
/** What the user's answer to the clarification request means. */
103-
function clarificationFrom(responses: TaskInputResponses): string {
99+
/**
100+
* What the user's answer to the clarification request means. An accepted
101+
* answer counts only if it is one of the interpretations offered for `topic`.
102+
*/
103+
function clarificationFrom(
104+
responses: TaskInputResponses,
105+
topic: string,
106+
): string {
107+
const offered = getInterpretationsForTopic(topic).map((o) => o.const);
108+
const ClarificationSchema = z.object({
109+
interpretation: z.string().refine((value) => offered.includes(value)),
110+
});
104111
const view = inputResponse(responses, "clarification");
105112
const accepted = acceptedContent(
106113
responses,
@@ -249,7 +256,7 @@ export const registerSimulateResearchQueryTool = (server: McpServer) => {
249256
};
250257
},
251258
receive: (responses, now) => {
252-
clarification = clarificationFrom(responses);
259+
clarification = clarificationFrom(responses, topic);
253260
startedAt = now;
254261
startStage = CLARIFY_AT;
255262
},

0 commit comments

Comments
 (0)