Skip to content

Commit 9c7e889

Browse files
fix(server): keep list_thread_pull_requests unpaged to stay within the agent context budget
Paging inputs (states, cursor, limit) added 233 bytes to the MCP tool list, over main's budget. The thread-read preview cap stays; it costs no model-facing bytes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 478365f commit 9c7e889

6 files changed

Lines changed: 23 additions & 79 deletions

File tree

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -608,29 +608,6 @@ describe("listThreadPullRequests", () => {
608608
]);
609609
expect(result.chains).toEqual([{ kind: "derived", numbers: [1, 2, 3] }]);
610610
});
611-
612-
it("pages the links in the requested states, with each page's stacks whole", () => {
613-
const merged = makeLink(5, { headBranch: "feat-e" });
614-
const thread = {
615-
pullRequests: [
616-
makeLink(1, { headBranch: "feat-a" }),
617-
makeLink(2, { headBranch: "feat-b", baseBranch: "feat-a" }),
618-
makeLink(4),
619-
{ ...merged, snapshot: { ...merged.snapshot!, state: "merged" as const } },
620-
makeLink(6, { headBranch: "feat-f", baseBranch: "feat-b" }),
621-
],
622-
};
623-
const first = listThreadPullRequests(thread, { states: ["open"], limit: 1 });
624-
expect(first.pullRequests.map((entry) => entry.number)).toEqual([1]);
625-
expect(first.chains).toEqual([{ kind: "derived", numbers: [1, 2, 6] }]);
626-
expect([first.nextCursor, first.total]).toEqual([1, 3]);
627-
const last = listThreadPullRequests(thread, { states: ["open"], cursor: 1, limit: 5 });
628-
expect(last.pullRequests.map((entry) => entry.number)).toEqual([2, 6]);
629-
expect(last.nextCursor).toBeNull();
630-
expect(listThreadPullRequests(thread, { states: ["merged"] }).chains).toEqual([
631-
{ kind: "derived", numbers: [5] },
632-
]);
633-
});
634611
});
635612

636613
describe("pull request guide tools", () => {

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import * as McpInvocationContext from "../../McpInvocationContext.ts";
2929
import * as McpToolAccess from "../../McpToolAccess.ts";
3030
import { threadNotFound } from "../../threadAccess.ts";
3131
import {
32-
type ListThreadPullRequestsInput,
3332
type ListThreadPullRequestsResult,
3433
PullRequestLinkFailedError,
3534
PullRequestUnlinkFailedError,
@@ -73,32 +72,18 @@ function entryOf(
7372
};
7473
}
7574

76-
const DEFAULT_PULL_REQUEST_LIST_LIMIT = 50;
77-
7875
export function listThreadPullRequests(
7976
thread: Pick<OrchestrationV2ThreadShell, "pullRequests">,
80-
input: Pick<ListThreadPullRequestsInput, "states" | "cursor" | "limit"> = {},
8177
): ListThreadPullRequestsResult {
8278
const chains = resolveThreadPullRequestChains(thread.pullRequests ?? []);
83-
const states = input.states === undefined ? undefined : new Set(input.states);
84-
const matching = visibleThreadPullRequests(thread.pullRequests ?? []).filter(
85-
(link) =>
86-
states === undefined ||
87-
(link.snapshot?.state !== undefined && states.has(link.snapshot.state)),
88-
);
89-
const cursor = input.cursor ?? 0;
90-
const page = matching.slice(cursor, cursor + (input.limit ?? DEFAULT_PULL_REQUEST_LIST_LIMIT));
91-
const pageKeys = new Set(page.map(threadPullRequestKeyOf));
9279
return {
93-
pullRequests: page.map((link) => entryOf(link, chains)),
94-
chains: chains
95-
.filter((chain) => chain.layers.some((layer) => pageKeys.has(threadPullRequestKeyOf(layer))))
96-
.map((chain) => ({
97-
kind: chain.kind,
98-
numbers: chain.layers.map((layer) => layer.number),
99-
})),
100-
nextCursor: cursor + page.length < matching.length ? cursor + page.length : null,
101-
total: matching.length,
80+
pullRequests: visibleThreadPullRequests(thread.pullRequests ?? []).map((link) =>
81+
entryOf(link, chains),
82+
),
83+
chains: chains.map((chain) => ({
84+
kind: chain.kind,
85+
numbers: chain.layers.map((layer) => layer.number),
86+
})),
10287
};
10388
}
10489

@@ -422,7 +407,7 @@ const make = Effect.gen(function* () {
422407
),
423408
list_thread_pull_requests: McpToolAccess.reads((input) =>
424409
requireThread(PullRequestListFailedError, input.threadId).pipe(
425-
Effect.map((thread) => listThreadPullRequests(thread, input)),
410+
Effect.map(listThreadPullRequests),
426411
),
427412
),
428413
watch_pull_request: writesThread((input) => setWatching(input, true)),

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
2-
clampedPositiveInt,
32
McpCapabilityUnavailableError,
4-
NonNegativeInt,
53
OrchestratorMcpEnvironmentTarget,
64
OrchestratorMcpFailure,
75
PositiveInt,
@@ -229,28 +227,15 @@ export const ThreadPullRequestEntry = Schema.Struct({
229227
});
230228
export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
231229

232-
export const ListThreadPullRequestsInput = Schema.Struct({
233-
environmentId: OrchestratorMcpEnvironmentTarget,
234-
threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
235-
states: Schema.optional(Schema.Array(PullRequestState).check(Schema.isMinLength(1))),
236-
cursor: Schema.optional(NonNegativeInt),
237-
limit: Schema.optional(clampedPositiveInt(50)).annotate({ description: "Max 50." }),
238-
});
239-
export type ListThreadPullRequestsInput = typeof ListThreadPullRequestsInput.Type;
240-
241230
export const ListThreadPullRequestsResult = Schema.Struct({
242231
pullRequests: Schema.Array(ThreadPullRequestEntry),
243-
/** Stacks with a pull request on this page, whole. */
244232
chains: Schema.Array(
245233
Schema.Struct({
246234
kind: Schema.Literals(["native", "derived"]),
247235
/** Bottom to top; each pull request comes before the ones built on it. */
248236
numbers: Schema.Array(Schema.Int),
249237
}),
250238
),
251-
nextCursor: Schema.NullOr(NonNegativeInt),
252-
/** Pull requests matching `states`, across all pages. */
253-
total: NonNegativeInt,
254239
});
255240
export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
256241

@@ -320,8 +305,11 @@ const UnlinkPullRequestsTool = Tool.make("unlink_pull_requests", {
320305

321306
const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
322307
description:
323-
"List PRs linked to a thread, with last known state and stack order (bottom to top). Page with `cursor=nextCursor`. Call only when you need that list. Never as an end-of-work check.",
324-
parameters: ListThreadPullRequestsInput,
308+
"List PRs linked to a thread, with last known state and stack order (bottom to top). Call only when you need that list. Never as an end-of-work check.",
309+
parameters: Schema.Struct({
310+
environmentId: OrchestratorMcpEnvironmentTarget,
311+
threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
312+
}),
325313
success: ListThreadPullRequestsResult,
326314
failure: PullRequestToolError,
327315
dependencies,
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": 48332
4+
"mcpToolListJsonBytes": 48099
55
}

‎evals/mcp-tool-use/run.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,21 +1302,15 @@ export const t3Default = (
13021302
return { ...pr(), watching: true, wasWatching: false };
13031303
case "unwatch_pull_request":
13041304
return { ...pr(), watching: false, wasWatching: true };
1305-
case "list_thread_pull_requests": {
1306-
const states = Array.isArray(input.states) ? input.states : undefined;
1307-
const matching = [...sample.linked]
1308-
.map((url) => ({ ...linkedEntry(sample, url), source: "agent", watching: false }))
1309-
.filter((entry) => states === undefined || states.includes(entry.state));
1310-
const cursor = typeof input.cursor === "number" ? input.cursor : 0;
1311-
const limit = Math.min(typeof input.limit === "number" ? input.limit : 50, 50);
1312-
const page = matching.slice(cursor, cursor + limit);
1305+
case "list_thread_pull_requests":
13131306
return {
1314-
pullRequests: page,
1307+
pullRequests: [...sample.linked].map((url) => ({
1308+
...linkedEntry(sample, url),
1309+
source: "agent",
1310+
watching: false,
1311+
})),
13151312
chains: [],
1316-
nextCursor: cursor + page.length < matching.length ? cursor + page.length : null,
1317-
total: matching.length,
13181313
};
1319-
}
13201314
case "t3_thread_read": {
13211315
const threadId = valueText(input.threadId);
13221316
const launched = sample.threads.get(threadId);

‎packages/contracts/src/orchestratorMcp.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export const OrchestratorMcpThreadListItem = Schema.Struct({
387387
runtimeMode: RuntimeMode,
388388
interactionMode: ProviderInteractionMode,
389389
linkedPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
390-
/** Up to 5 open linked pull requests, watched first; list_thread_pull_requests pages all of them. */
390+
/** Up to 5 open linked pull requests, watched first; list_thread_pull_requests has all of them. */
391391
pullRequests: Schema.optional(Schema.Array(OrchestratorMcpThreadPullRequest)),
392392
pullRequestCount: Schema.optional(NonNegativeInt),
393393
openPullRequestCount: Schema.optional(NonNegativeInt),
@@ -420,7 +420,7 @@ export type OrchestratorMcpThreadListResult = typeof OrchestratorMcpThreadListRe
420420
// Codex hides `minimum` and `maximum` from the model, so a value above the maximum clamps on
421421
// decode instead of failing the call; pagination returns the rest. Values below 1 still fail:
422422
// 0 asks for nothing, and answering it with one item would be a different request.
423-
export const clampedPositiveInt = (maximum: number) =>
423+
const clampedPositiveInt = (maximum: number) =>
424424
PositiveInt.pipe(
425425
Schema.decodeTo(
426426
PositiveInt.check(Schema.isLessThanOrEqualTo(maximum)),
@@ -462,7 +462,7 @@ export const OrchestratorMcpThreadDetail = Schema.Struct({
462462
runtimeMode: RuntimeMode,
463463
interactionMode: ProviderInteractionMode,
464464
linkedPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
465-
/** Up to 5 open linked pull requests, watched first, on the first page only; list_thread_pull_requests pages all of them. */
465+
/** Up to 5 open linked pull requests, watched first, on the first page only; list_thread_pull_requests has all of them. */
466466
pullRequests: Schema.optional(Schema.Array(OrchestratorMcpThreadPullRequest)),
467467
pullRequestCount: Schema.optional(NonNegativeInt),
468468
openPullRequestCount: Schema.optional(NonNegativeInt),

0 commit comments

Comments
 (0)