From e44d75c20a51142abc056c243b13c1d7bb4be687 Mon Sep 17 00:00:00 2001 From: Vegard Stikbakke Date: Thu, 3 Sep 2026 15:10:32 +0200 Subject: [PATCH] fix(coding-agent): raise branch summary output cap closes #8845 --- packages/coding-agent/CHANGELOG.md | 1 + packages/coding-agent/docs/settings.md | 2 +- .../core/compaction/branch-summarization.ts | 6 ++++-- .../test/branch-summarization.test.ts | 21 +++++++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index e2a220a36..59ab2d754 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixed +- Fixed branch summaries failing when reasoning consumes the previous 2048-token output cap ([#8845](https://github.com/earendil-works/pi/issues/8845)). - Fixed the write tool reporting UTF-16 code-unit counts as byte counts by removing the misleading count ([#8979](https://github.com/earendil-works/pi/issues/8979)). - Fixed proxied plain-HTTP provider requests hanging after a tool call by tunneling them with CONNECT ([#8134](https://github.com/earendil-works/pi/issues/8134)). - Fixed RPC `abort` reporting success without cancelling an in-progress manual compaction ([#8920](https://github.com/earendil-works/pi/issues/8920)). diff --git a/packages/coding-agent/docs/settings.md b/packages/coding-agent/docs/settings.md index d0597043e..d070d895f 100644 --- a/packages/coding-agent/docs/settings.md +++ b/packages/coding-agent/docs/settings.md @@ -133,7 +133,7 @@ Set `PI_SKIP_VERSION_CHECK=1` to disable the Pi version update check. Use `--off | Setting | Type | Default | Description | |---------|------|---------|-------------| -| `branchSummary.reserveTokens` | number | `16384` | Tokens reserved for branch summarization | +| `branchSummary.reserveTokens` | number | `16384` | Tokens reserved when selecting branch history; output is capped at 4096 tokens | | `branchSummary.skipPrompt` | boolean | `false` | Skip "Summarize branch?" prompt on `/tree` navigation (defaults to no summary) | ### Retry diff --git a/packages/coding-agent/src/core/compaction/branch-summarization.ts b/packages/coding-agent/src/core/compaction/branch-summarization.ts index d0abd5450..1e027c65c 100644 --- a/packages/coding-agent/src/core/compaction/branch-summarization.ts +++ b/packages/coding-agent/src/core/compaction/branch-summarization.ts @@ -79,7 +79,7 @@ export interface GenerateBranchSummaryOptions { customInstructions?: string; /** If true, customInstructions replaces the default prompt instead of being appended */ replaceInstructions?: boolean; - /** Tokens reserved for prompt + LLM response (default 16384) */ + /** Tokens reserved when selecting branch history (default 16384) */ reserveTokens?: number; /** Optional session stream function. Used to preserve SDK request behavior without mutating agent state. */ streamFn?: StreamFn; @@ -342,12 +342,14 @@ export async function generateBranchSummary( }, ]; + const maxTokens = Math.min(4096, model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY); + // Call LLM for summarization. Prefer the session stream function so SDK // request behavior (timeouts, retries, attribution headers) stays consistent // without running through agent state/events. Retried via completeSummarization // so transient stream drops reuse the configured retry policy. const context = { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }; - const requestOptions: SimpleStreamOptions = { apiKey, headers, env, signal, maxTokens: 2048 }; + const requestOptions: SimpleStreamOptions = { apiKey, headers, env, signal, maxTokens }; const response = await completeSummarization(model, context, requestOptions, streamFn, retry, callbacks); // Check if aborted or errored diff --git a/packages/coding-agent/test/branch-summarization.test.ts b/packages/coding-agent/test/branch-summarization.test.ts index 7018e969f..cc34bd64b 100644 --- a/packages/coding-agent/test/branch-summarization.test.ts +++ b/packages/coding-agent/test/branch-summarization.test.ts @@ -61,9 +61,30 @@ describe("branch summarization", () => { streamFn, }); + expect(requestOptions?.maxTokens).toBe(4096); expect(requestOptions?.toolChoice).toBeUndefined(); }); + it("clamps the branch summary output cap to the model limit", async () => { + let requestOptions: SimpleStreamOptions | undefined; + const streamFn: StreamFn = (_model, _context, options) => { + requestOptions = options; + const stream = createAssistantMessageEventStream(); + queueMicrotask(() => + stream.push({ type: "done", reason: "stop", message: response([{ type: "text", text: "summary" }]) }), + ); + return stream; + }; + + await generateBranchSummary(entries, { + model: { ...model, maxTokens: 1024 }, + signal: new AbortController().signal, + streamFn, + }); + + expect(requestOptions?.maxTokens).toBe(1024); + }); + it("rejects tool calls from branch summaries", async () => { const streamFn: StreamFn = () => { const stream = createAssistantMessageEventStream();