Skip to content

Commit 2cdf508

Browse files
fagnersalesclaude
andcommitted
fix(cli): stop a backward read whose cursor does not move back
A server answering with a previousPosition at or past the requested beforePosition would make readBackward read the same page forever. Also shortens the new t3_thread_read wording (+171 bytes instead of +208). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5780879 commit 2cdf508

5 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎apps/server/src/cli/thread.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,23 @@ it.effect.each(SERVERS)(
339339
}),
340340
);
341341

342+
it.effect("show stops when a server's previousPosition does not move back", () =>
343+
Effect.gen(function* () {
344+
const page = {
345+
thread: { itemCount: 10 },
346+
items: [{ position: 5, itemId: "item-5", type: "assistant_message" }],
347+
nextPosition: 5,
348+
hasMore: false,
349+
previousPosition: 5,
350+
};
351+
const { tools, calls } = scriptedTools({ t3_thread_read: [page, page] });
352+
353+
yield* readLatestMessages(tools, threadId, 10);
354+
355+
assert.lengthOf(calls, 2);
356+
}),
357+
);
358+
342359
it.effect("a server's rejected arguments are a tool failure, so show can fall back", () =>
343360
Effect.gen(function* () {
344361
const failWith = (code: number | undefined) =>

‎apps/server/src/cli/thread.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ const readBackward = Effect.fn("cli.thread.readBackward")(function* (
375375
while (previous !== null) {
376376
const page = yield* read(previous);
377377
pages.push(page.items);
378-
previous = done(page.items) ? null : (page.previousPosition ?? null);
378+
const next = page.previousPosition ?? null;
379+
// A cursor that does not move back would read the same page forever.
380+
previous = done(page.items) || next === null || next >= previous ? null : next;
379381
}
380382
return { thread: newest.thread, pages };
381383
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ const ThreadListTool = Tool.make("t3_thread_list", {
238238

239239
const ThreadReadTool = Tool.make("t3_thread_read", {
240240
description:
241-
"Read any thread by id. `messages` (default): user and assistant messages and plans. `activity`: all timeline items. Keep reads small: low `limit` and `maxCharsPerItem`. Page with `afterPosition=nextPosition`, or back from the newest with `beforePosition=previousPosition`. Cut-off text: `itemId` with `textOffset=nextTextOffset`. Reading a child's final result marks it delivered. In your reply, name it as its `link` verbatim.",
241+
"Read any thread by id. `messages` (default): user and assistant messages and plans. `activity`: all timeline items. Keep reads small: low `limit` and `maxCharsPerItem`. Page with `afterPosition=nextPosition`, or back with `beforePosition=previousPosition`. Cut-off text: `itemId` with `textOffset=nextTextOffset`. Reading a child's final result marks it delivered. In your reply, name it as its `link` verbatim.",
242242
parameters: OrchestratorMcpThreadReadInput,
243243
success: OrchestratorMcpThreadReadResult,
244244
failure: OrchestratorMcpFailure,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"claudeSystemAppendBytes": 5350,
33
"codexAdditionalContextBytes": 7080,
4-
"mcpToolListJsonBytes": 48324
4+
"mcpToolListJsonBytes": 48287
55
}

‎packages/contracts/src/orchestratorMcp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export const OrchestratorMcpThreadReadInput = Schema.Struct({
411411
view: Schema.optional(Schema.Literals(["messages", "activity"])),
412412
afterPosition: Schema.optional(NonNegativeInt),
413413
beforePosition: Schema.optional(NonNegativeInt).annotate({
414-
description: "Read the `limit` items just before this position; `itemCount` reads the latest.",
414+
description: "The `limit` items before it. `itemCount` reads the newest.",
415415
}),
416416
limit: Schema.optional(clampedPositiveInt(100)).annotate({
417417
description: "Max items, 1 to 100.",

0 commit comments

Comments
 (0)