Agents: validate persisted tool-call names

This commit is contained in:
Vignesh Natarajan
2026-02-21 23:06:44 -08:00
parent 29a782b9cd
commit cdfe45eeb8
11 changed files with 248 additions and 8 deletions

View File

@@ -203,6 +203,54 @@ describe("sanitizeSessionHistory", () => {
expect(result.map((msg) => msg.role)).toEqual(["user"]);
});
it("drops malformed tool calls with invalid/overlong names", async () => {
const messages = [
{
role: "assistant",
content: [
{
type: "toolCall",
id: "call_bad",
name: 'toolu_01mvznfebfuu <|tool_call_argument_begin|> {"command"',
arguments: {},
},
{ type: "toolCall", id: "call_long", name: `read_${"x".repeat(80)}`, arguments: {} },
],
},
{ role: "user", content: "hello" },
] as unknown as AgentMessage[];
const result = await sanitizeSessionHistory({
messages,
modelApi: "openai-responses",
provider: "openai",
sessionManager: mockSessionManager,
sessionId: TEST_SESSION_ID,
});
expect(result.map((msg) => msg.role)).toEqual(["user"]);
});
it("drops tool calls that are not in the allowed tool set", async () => {
const messages = [
{
role: "assistant",
content: [{ type: "toolCall", id: "call_1", name: "write", arguments: {} }],
},
] as unknown as AgentMessage[];
const result = await sanitizeSessionHistory({
messages,
modelApi: "openai-responses",
provider: "openai",
allowedToolNames: ["read"],
sessionManager: mockSessionManager,
sessionId: TEST_SESSION_ID,
});
expect(result).toEqual([]);
});
it("downgrades orphaned openai reasoning even when the model has not changed", async () => {
const sessionEntries = [
makeModelSnapshotEntry({