Skip to content

Commit 7b26271

Browse files
fix(server): list_thread_pull_requests cursor survives unlinking
The cursor was an index into the current list, so unlinking a returned page shifted the remaining PRs under it and the next page skipped them. Pages now follow a total order on (linkedAt, host, repository, number) and the cursor is the last entry's position in that order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b976fca commit 7b26271

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ describe("pull request toolkit handlers", () => {
537537
]),
538538
});
539539
const result = yield* harness.call("list_thread_pull_requests", {});
540-
expect(result.pullRequests.map((entry) => entry.number)).toEqual([3, 1, 2, 10]);
541-
expect(result.pullRequests[0]).toEqual({
540+
expect(result.pullRequests.map((entry) => entry.number)).toEqual([1, 2, 3, 10]);
541+
expect(result.pullRequests[2]).toEqual({
542542
host: "github.com",
543543
repository: "t3tools/t3code",
544544
number: 3,
@@ -587,8 +587,8 @@ describe("listThreadPullRequests", () => {
587587
],
588588
});
589589
expect(result.pullRequests.map((entry) => [entry.number, entry.stack])).toEqual([
590-
[2, { kind: "native", position: 2, size: 2 }],
591590
[1, { kind: "native", position: 1, size: 2 }],
591+
[2, { kind: "native", position: 2, size: 2 }],
592592
]);
593593
expect(result.chains).toEqual([{ kind: "native", numbers: [1, 2] }]);
594594
});
@@ -632,6 +632,14 @@ describe("listThreadPullRequests", () => {
632632
[],
633633
[stack],
634634
]);
635+
636+
// Unlinking the first page must not shift the cursor past PRs not yet seen.
637+
const remaining = { pullRequests: thread.pullRequests.slice(50) };
638+
expect(
639+
listThreadPullRequests(remaining, pages[0]!.nextCursor!).pullRequests.map(
640+
(entry) => entry.number,
641+
),
642+
).toEqual(Array.from({ length: 50 }, (_, index) => index + 51));
635643
});
636644
});
637645

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,35 @@ function entryOf(
7474

7575
const PULL_REQUEST_LIST_PAGE_SIZE = 50;
7676

77+
// A total order that survives unlinking: the cursor names a position, not an index, so removing
78+
// PRs already returned cannot skip later ones. PRs linked in one call share `linkedAt`.
79+
function pullRequestListPosition(link: ThreadPullRequestLink): string {
80+
return [link.linkedAt, link.host, link.repository, String(link.number).padStart(10, "0")].join(
81+
" ",
82+
);
83+
}
84+
7785
export function listThreadPullRequests(
7886
thread: Pick<OrchestrationV2ThreadShell, "pullRequests">,
79-
cursor = 0,
87+
cursor?: string,
8088
): ListThreadPullRequestsResult {
8189
const chains = resolveThreadPullRequestChains(thread.pullRequests ?? []);
82-
const visible = visibleThreadPullRequests(thread.pullRequests ?? []);
83-
const end = cursor + PULL_REQUEST_LIST_PAGE_SIZE;
84-
const page = visible.slice(cursor, end);
85-
const pageKeys = new Set(page.map(threadPullRequestKeyOf));
90+
const after = visibleThreadPullRequests(thread.pullRequests ?? [])
91+
.map((link) => ({ link, position: pullRequestListPosition(link) }))
92+
.filter(({ position }) => cursor === undefined || position > cursor)
93+
.toSorted((left, right) => (left.position < right.position ? -1 : 1));
94+
const page = after.slice(0, PULL_REQUEST_LIST_PAGE_SIZE);
95+
const pageKeys = new Set(page.map(({ link }) => threadPullRequestKeyOf(link)));
8696
return {
87-
pullRequests: page.map((link) => entryOf(link, chains)),
97+
pullRequests: page.map(({ link }) => entryOf(link, chains)),
8898
// Stacks with a pull request on this page, whole.
8999
chains: chains
90100
.filter((chain) => chain.layers.some((layer) => pageKeys.has(threadPullRequestKeyOf(layer))))
91101
.map((chain) => ({
92102
kind: chain.kind,
93103
numbers: chain.layers.map((layer) => layer.number),
94104
})),
95-
nextCursor: end < visible.length ? end : null,
105+
nextCursor: after.length > page.length ? page.at(-1)!.position : null,
96106
};
97107
}
98108

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
McpCapabilityUnavailableError,
3-
NonNegativeInt,
43
OrchestratorMcpEnvironmentTarget,
54
OrchestratorMcpFailure,
65
PositiveInt,
@@ -238,7 +237,7 @@ export const ListThreadPullRequestsResult = Schema.Struct({
238237
}),
239238
),
240239
/** Pass as `cursor` for the next page; null on the last page. */
241-
nextCursor: Schema.NullOr(NonNegativeInt),
240+
nextCursor: Schema.NullOr(Schema.String),
242241
});
243242
export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
244243

@@ -312,7 +311,7 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
312311
parameters: Schema.Struct({
313312
environmentId: OrchestratorMcpEnvironmentTarget,
314313
threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
315-
cursor: Schema.optional(NonNegativeInt),
314+
cursor: Schema.optional(Schema.String),
316315
}),
317316
success: ListThreadPullRequestsResult,
318317
failure: PullRequestToolError,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"claudeSystemAppendBytes": 5597,
33
"codexAdditionalContextBytes": 7327,
4-
"mcpToolListJsonBytes": 48139
4+
"mcpToolListJsonBytes": 48126
55
}

0 commit comments

Comments
 (0)