Skip to content

Commit b16a435

Browse files
authored
fix(agents): guard prompt cache tool names
Make prompt-cache observability collect diagnostic tool names through guarded descriptor reads so an unreadable tool-name getter cannot abort cache tracing/debug collection. Preserve readable trimmed names and keep runtime tool registration/schema behavior strict and unchanged.
1 parent 8b03fd1 commit b16a435

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/agents/embedded-agent-runner/prompt-cache-observability.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ describe("prompt cache observability", () => {
1818
).toEqual(["read", "write"]);
1919
});
2020

21+
it("collects prompt-cache tool names without aborting on unreadable descriptors", () => {
22+
const unreadableTool = {
23+
get name(): string {
24+
throw new Error("tool name getter exploded");
25+
},
26+
};
27+
28+
expect(
29+
collectPromptCacheToolNames([{ name: " read " }, unreadableTool, { name: "write" }]),
30+
).toEqual(["read", "write"]);
31+
});
32+
2133
it("tracks cache-relevant changes and reports a real cache-read drop", () => {
2234
// Observability only emits when a material cache-read drop follows a tracked
2335
// cache-affecting change.

src/agents/embedded-agent-runner/prompt-cache-observability.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,19 @@ function diffSnapshots(
140140
return changes.length > 0 ? changes : null;
141141
}
142142

143-
export function collectPromptCacheToolNames(tools: Array<{ name?: string }>): string[] {
144-
return tools.map((tool) => tool.name?.trim()).filter((name): name is string => Boolean(name));
143+
export function collectPromptCacheToolNames(tools: readonly { name?: string }[]): string[] {
144+
const names: string[] = [];
145+
for (const tool of tools) {
146+
try {
147+
const name = tool.name?.trim();
148+
if (name) {
149+
names.push(name);
150+
}
151+
} catch {
152+
continue;
153+
}
154+
}
155+
return names;
145156
}
146157

147158
export function beginPromptCacheObservation(params: {

0 commit comments

Comments
 (0)