Skip to content

Commit d3d8fe5

Browse files
committed
fix: bound the bytes retained by the tool summary digest cache
The 512-entry limit bounded cardinality only. Keys are the summary text, so a sequence of large runtime-generated descriptions could pin megabytes for the process lifetime. Summaries over the length cap still hash, they just never enter the cache.
1 parent 73ccba1 commit d3d8fe5

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎src/agents/system-prompt-report.tool-digest-cache.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,34 @@ describe("tool summary digest cache", () => {
7171
first.tools.entries.map((entry) => entry.schemaHash),
7272
);
7373
});
74+
75+
it("does not retain oversized tool summaries in the content-keyed cache", () => {
76+
// The cache keys are the summary text, so entry count alone would let a few
77+
// very large runtime-generated descriptions pin megabytes for the process
78+
// lifetime. Over the length cap the digest is computed but never cached.
79+
const oversizedParameters = { type: "object", properties: {} };
80+
const oversized = `oversized probe ${"x".repeat(5_000)}`;
81+
const buildOversized = (promptSuffix: string) =>
82+
buildSystemPromptReport({
83+
source: "run",
84+
generatedAt: 0,
85+
bootstrapMaxChars: 20_000,
86+
systemPrompt: `oversized system prompt ${promptSuffix}`,
87+
bootstrapFiles: [],
88+
injectedFiles: [],
89+
skillsPrompt: `<skill><name>oversized-${promptSuffix}</name></skill>`,
90+
tools: [
91+
{ name: "oversized_probe", description: oversized, parameters: oversizedParameters },
92+
] as never,
93+
});
94+
95+
const first = buildOversized("a");
96+
createHashCalls.count = 0;
97+
const second = buildOversized("b");
98+
99+
// A cached summary would leave only the 2 prompt digests, as the test above
100+
// asserts. The third call is the oversized summary being hashed again.
101+
expect(createHashCalls.count).toBe(3);
102+
expect(second.tools.entries[0]?.summaryHash).toBe(first.tools.entries[0]?.summaryHash);
103+
});
74104
});

‎src/agents/system-prompt-report.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const toolReportEntryCache = new WeakMap<AgentTool, ToolReportEntry>();
2222
// descriptions vary with runtime context and would otherwise accumulate.
2323
const toolSummaryHashCache = new Map<string, string>();
2424
const MAX_TOOL_SUMMARY_HASHES = 512;
25+
// Keys are the summary text itself, so entry count alone does not bound memory:
26+
// 512 arbitrarily long runtime-generated descriptions would be retained for the
27+
// process lifetime. Oversized summaries still hash, they just never enter the cache.
28+
const MAX_CACHED_TOOL_SUMMARY_CHARS = 4_096;
2529
const toolSchemaStatsCache = new WeakMap<
2630
object,
2731
Pick<ToolReportEntry, "propertiesCount" | "schemaChars" | "schemaHash">
@@ -91,6 +95,9 @@ function buildToolSchemaStats(
9195
}
9296

9397
function resolveSummaryHash(summary: string): string {
98+
if (summary.length > MAX_CACHED_TOOL_SUMMARY_CHARS) {
99+
return sha256(summary);
100+
}
94101
const cached = toolSummaryHashCache.get(summary);
95102
if (cached !== undefined) {
96103
return cached;

0 commit comments

Comments
 (0)