fix(agent-runtime): guard prompt cache tool names

This commit is contained in:
Vincent Koc
2026-06-04 01:45:44 +02:00
parent d9d4514c00
commit b2cabfb51f
2 changed files with 43 additions and 1 deletions

View File

@@ -17,6 +17,25 @@ describe("prompt cache observability", () => {
).toEqual(["read", "write"]);
});
it("skips unreadable tool name rows", () => {
const tools: Array<{ name?: string }> = [{ name: "read" }];
Object.defineProperty(tools, "1", {
get() {
throw new Error("prompt cache tool row exploded");
},
});
tools.length = 3;
Object.defineProperty(tools, "2", {
value: {
get name() {
throw new Error("prompt cache tool name exploded");
},
},
});
expect(collectPromptCacheToolNames(tools)).toEqual(["read"]);
});
it("tracks cache-relevant changes and reports a real cache-read drop", () => {
const first = beginPromptCacheObservation({
sessionId: "session-1",

View File

@@ -138,7 +138,30 @@ function diffSnapshots(
}
export function collectPromptCacheToolNames(tools: Array<{ name?: string }>): string[] {
return tools.map((tool) => tool.name?.trim()).filter((name): name is string => Boolean(name));
const names: string[] = [];
for (const key of Object.keys(tools)) {
const index = Number(key);
if (!Number.isInteger(index)) {
continue;
}
let tool: { name?: string } | undefined;
try {
tool = tools[index];
} catch {
continue;
}
let rawName: string | undefined;
try {
rawName = tool?.name;
} catch {
continue;
}
const name = rawName?.trim();
if (name) {
names.push(name);
}
}
return names;
}
export function beginPromptCacheObservation(params: {