Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): size summary chunks with the guard's token estimate
xpeng5278-web39ab73b9afaa
diff --git a/packages/agent-runtime/src/compaction-summary-input.test.ts b/packages/agent-runtime/src/compaction-summary-input.test.tsindex 4c22e388e..56731f5a4 100644--- a/packages/agent-runtime/src/compaction-summary-input.test.ts+++ b/packages/agent-runtime/src/compaction-summary-input.test.ts@@ -1,6 +1,9 @@import { describe, expect, it } from "vitest";import type { AgentMessage } from "@earendil-works/pi-agent-core";-import { estimateTokens } from "./pi-runtime-estimates.js";+import {+ ESTIMATED_TEXT_CHARS_PER_TOKEN,+ estimateTokens,+} from "./pi-runtime-estimates.js";import {addSummaryUsage,compactionSummaryInputLimit,@@ -10,6 +13,7 @@ import {COMPACTION_SUMMARY_PROMPT_SAFETY_TOKENS,COMPACTION_SUMMARY_RETRY_BASE_MS,COMPACTION_SUMMARY_RETRY_POLICY,+ compactionSummaryReserveTokens,estimateSummaryPromptTokens,planSummaryChunks,reduceSummaryInput,@@ -278,6 +282,60 @@ describe("planSummaryChunks", () => {expect(chunks!.map((chunk) => chunk.length)).toEqual([3, 3, 3, 3]);});++ // The first chunk carries the real previous summary. Each later chunk carries+ // a summary that fills the reserve planSummaryRequests holds for it. Both+ // have to stay under the limit the preflight guard enforces.+ function expectPlannedChunksUnderGuard(scenario: {+ summaryInputLimit: number;+ outputBudget: number;+ previousSummary?: string;+ messages: AgentMessage[];+ }): void {+ const reserveTokens = compactionSummaryReserveTokens({+ previousSummary: scenario.previousSummary,+ outputBudget: scenario.outputBudget,+ });+ const chunks = planSummaryChunks(+ input(scenario.messages, { previousSummary: scenario.previousSummary }),+ { summaryInputLimit: scenario.summaryInputLimit, reserveTokens },+ );+ expect(chunks).toBeDefined();+ expect(chunks!.length).toBeGreaterThan(1);+ const reserveSummary = "c".repeat(+ Math.floor(reserveTokens * ESTIMATED_TEXT_CHARS_PER_TOKEN),+ );+ for (const [index, chunk] of chunks!.entries()) {+ const tokens = estimateSummaryPromptTokens({+ messagesToSummarize: chunk,+ turnPrefixMessages: [],+ isSplitTurn: false,+ previousSummary: index === 0 ? scenario.previousSummary : reserveSummary,+ });+ expect(tokens, `chunk ${index}`).toBeLessThan(scenario.summaryInputLimit);+ }+ }++ it("keeps a large carried summary inside the guard's window", () => {+ expectPlannedChunksUnderGuard({+ summaryInputLimit: 10_000,+ outputBudget: 4_096,+ previousSummary: "s".repeat(28_000),+ messages: Array.from({ length: 40 }, (_, index) =>+ user(`note ${index} ${"m".repeat(1_500)}`),+ ),+ });+ });++ it("keeps plain-text chunks with no previous summary inside the guard's window", () => {+ expectPlannedChunksUnderGuard({+ summaryInputLimit: 10_000,+ outputBudget: 4_096,+ messages: Array.from({ length: 48 }, (_, index) =>+ user(`plain ${index} ${"p".repeat(1_500)}`),+ ),+ });+ });});describe("addSummaryUsage", () => {diff --git a/packages/agent-runtime/src/compaction-summary-input.ts b/packages/agent-runtime/src/compaction-summary-input.tsindex 4f68f4e10..e18d35afc 100644--- a/packages/agent-runtime/src/compaction-summary-input.ts+++ b/packages/agent-runtime/src/compaction-summary-input.ts@@ -180,6 +180,25 @@ export function compactionSummaryInputLimit(input: {);}+/**+ * Room one chunk keeps for the summary its prompt carries. The first chunk+ * carries `previousSummary`; every later chunk carries a summary no larger+ * than the model's output budget. The carried text is measured with the same+ * character estimate as {@link estimateSummaryPromptTokens}. Reserving it at+ * four characters per token under-counts the guard by 12.5%, and the chunk+ * margin applies only to what remains, so the first chunk can leave the window+ * it was planned for.+ */+export function compactionSummaryReserveTokens(input: {+ previousSummary?: string;+ outputBudget: number;+}): number {+ return Math.max(+ Math.ceil((input.previousSummary?.length ?? 0) / ESTIMATED_TEXT_CHARS_PER_TOKEN),+ input.outputBudget,+ );+}+/*** Split a summary input into contiguous chunks that each fit the request* budget, so a range too large for one prompt is still summarized instead ofdiff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex 2b6a4ea02..f9309b832 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -264,6 +264,7 @@ import {addSummaryUsage,compactionSummaryInputLimit,compactionSummaryOutputBudget,+ compactionSummaryReserveTokens,COMPACTION_SUMMARY_RETRY_POLICY,estimateSummaryPromptTokens,planSummaryChunks,@@ -7671,13 +7672,13 @@ Do not invent objections or turn speculative risks into blockers. Stop when thebudget: { hardLimit: number; requestHeadroom: number },): AgentMessage[][] | undefined {const reduced = reduceSummaryInput(preparation) ?? preparation;- const reserveTokens = Math.max(- Math.ceil((preparation.previousSummary?.length ?? 0) / 4),- compactionSummaryOutputBudget({+ const reserveTokens = compactionSummaryReserveTokens({+ previousSummary: preparation.previousSummary,+ outputBudget: compactionSummaryOutputBudget({requestHeadroom: budget.requestHeadroom,modelMaxTokens: this.model.maxTokens,}),- );+ });return planSummaryChunks(reduced, {summaryInputLimit: this.summaryInputLimit(budget),reserveTokens,