mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
fix(agent-runtime): guard prompt cache tool names
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user