Skip to content

Commit 7d82461

Browse files
cliffhallclaude
andcommitted
fix(everything): measure the elicitation poll deadline monotonically
Copilot round 1 on #5014. The deadline is now read from performance.now(), so a wall-clock adjustment cannot stretch it, and it is checked after each wait rather than before, so a timer that fires late cannot slip a poll in past the deadline. A regression test covers a wait that ends past it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 8ea5e05 commit 7d82461

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,15 @@ describe("trigger-elicitation-request-async", () => {
576576
expect(texts[0]).toMatch(/^\[FAILED\] No message\n\nProgress:\n/);
577577
});
578578

579-
/** Also fake `Date`, which the tool's polling deadline is measured with. */
580-
function fakeDate() {
581-
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
579+
/** Also fake `performance`, which the tool's polling deadline is measured with. */
580+
function fakeClock() {
581+
vi.useFakeTimers({
582+
toFake: ["setTimeout", "clearTimeout", "performance"],
583+
});
582584
}
583585

584586
it("gives up 5 seconds before the 10-minute TTL of a task that never finishes", async () => {
585-
fakeDate();
587+
fakeClock();
586588
// A client that keeps its task longer than the 10-minute TTL asked for.
587589
await connectTaskClient(() => {}, 60_000);
588590
const texts = await callAsync("trigger-elicitation-request-async");
@@ -592,7 +594,7 @@ describe("trigger-elicitation-request-async", () => {
592594
});
593595

594596
it("times out before a client that honors the 10-minute TTL expires the task", async () => {
595-
fakeDate();
597+
fakeClock();
596598
let created = "";
597599
const { taskStore } = await connectTaskClient((_m, taskId) => {
598600
created = taskId!;
@@ -609,6 +611,22 @@ describe("trigger-elicitation-request-async", () => {
609611
expect((await taskStore.getTask(created))?.ttl).toBe(600000);
610612
});
611613

614+
it("times out without polling when its wait ends past the deadline", async () => {
615+
// A timer that fires late: the first reading sets the deadline, and every
616+
// later one is already past it.
617+
let readings = 0;
618+
vi.spyOn(performance, "now").mockImplementation(() =>
619+
readings++ === 0 ? 0 : 600_000,
620+
);
621+
await connectTaskClient(() => {});
622+
const texts = await callAsync("trigger-elicitation-request-async");
623+
expect(texts).toEqual([
624+
expect.stringMatching(
625+
/^\[TIMEOUT\] Task timed out after 0 poll attempts, before its 10-minute TTL expired\n\nProgress:\nTask created: \S+$/,
626+
),
627+
]);
628+
});
629+
612630
it("returns a synchronous answer from a client that ignores the task request", async () => {
613631
session = await connect({
614632
capabilities: {

‎src/everything/tools/trigger-elicitation-request-async.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export const registerTriggerElicitationRequestAsyncTool = (
6565
async (args, extra): Promise<CallToolResult> => {
6666
// Polling must end before the client may expire the task. The client
6767
// starts the TTL when it creates the task, after this point, so a
68-
// deadline measured from here is conservative.
69-
const pollDeadline = Date.now() + TASK_TTL - TTL_SAFETY_MARGIN;
68+
// deadline measured from here is conservative. performance.now() is
69+
// monotonic, so a wall-clock adjustment cannot stretch the deadline.
70+
const pollDeadline = performance.now() + TASK_TTL - TTL_SAFETY_MARGIN;
7071

7172
// Create the elicitation request WITH task metadata
7273
// Using z.any() schema to avoid complex type matching with _meta
@@ -160,14 +161,16 @@ export const registerTriggerElicitationRequestAsyncTool = (
160161
taskStatus !== "failed" &&
161162
taskStatus !== "cancelled"
162163
) {
163-
// Give up rather than poll past the TTL the task was created with
164-
if (Date.now() + POLL_INTERVAL > pollDeadline) {
164+
// Wait before polling
165+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
166+
167+
// Give up rather than poll past the TTL the task was created with.
168+
// Checked after the wait, so a timer that fires late cannot slip a
169+
// poll in after the deadline.
170+
if (performance.now() > pollDeadline) {
165171
timedOut = true;
166172
break;
167173
}
168-
169-
// Wait before polling
170-
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
171174
attempts++;
172175

173176
// Get task status from client

0 commit comments

Comments
 (0)