Skip to content

Commit 17df256

Browse files
committed
fix(cron): trim whitespace-padded keys in job canonicalization
- Local LLM parsers (qwen35b/llamacpp) can emit JSON with trailing-space key names - Add trimWhitespacePaddedCronKeys to strip whitespace before repair/canonicalization - Gateway schemas use strict additionalProperties:false so unpadded keys are rejected Fixes #95407
1 parent 2bae630 commit 17df256

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Test whitespace-padded key recovery for issue #95407
2+
import { describe, expect, it } from "vitest";
3+
import { canonicalizeCronToolObject } from "./cron-tool-canonicalize.js";
4+
5+
describe("canonicalizeCronToolObject whitespace-padded keys (#95407)", () => {
6+
it("trims trailing-space keys like 'schedule ' -> 'schedule'", () => {
7+
const input: Record<string, unknown> = {
8+
name: "Test",
9+
"schedule ": { kind: "cron", expr: "0 * * * *", tz: "UTC" },
10+
"sessionTarget ": "isolated",
11+
"payload ": { kind: "agentTurn", message: "hello" },
12+
"enabled ": true,
13+
};
14+
15+
const result = canonicalizeCronToolObject(input);
16+
17+
// Verify trimmed keys exist
18+
expect(result).toHaveProperty("schedule");
19+
expect(result).toHaveProperty("sessionTarget");
20+
expect(result).toHaveProperty("payload");
21+
expect(result).toHaveProperty("enabled");
22+
expect(result).toHaveProperty("name");
23+
24+
// Verify untrimmed keys do NOT exist
25+
expect(Object.keys(result)).not.toContain("schedule ");
26+
expect(Object.keys(result)).not.toContain("sessionTarget ");
27+
expect(Object.keys(result)).not.toContain("payload ");
28+
expect(Object.keys(result)).not.toContain("enabled ");
29+
30+
// Verify no key has trailing spaces
31+
for (const key of Object.keys(result)) {
32+
expect(key).toBe(key.trim());
33+
}
34+
});
35+
36+
it("trims leading-space keys", () => {
37+
const input: Record<string, unknown> = {
38+
" schedule": { kind: "at", at: "2026-12-25T00:00:00Z" },
39+
" sessionTarget": "main",
40+
};
41+
42+
const result = canonicalizeCronToolObject(input);
43+
44+
expect(result).toHaveProperty("schedule");
45+
expect(result).toHaveProperty("sessionTarget");
46+
expect(Object.keys(result)).not.toContain(" schedule");
47+
expect(Object.keys(result)).not.toContain(" sessionTarget");
48+
});
49+
50+
it("passes through already-clean keys unchanged", () => {
51+
const input: Record<string, unknown> = {
52+
name: "Holiday Check-in",
53+
description: "Casual check-in",
54+
schedule: { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" },
55+
sessionTarget: "isolated",
56+
payload: { kind: "agentTurn", message: "hello" },
57+
enabled: true,
58+
};
59+
60+
const result = canonicalizeCronToolObject(input);
61+
62+
expect(result.schedule).toEqual(input.schedule);
63+
expect(result.sessionTarget).toBe("isolated");
64+
expect(result.payload).toEqual(input.payload);
65+
expect(result.enabled).toBe(true);
66+
expect(result.name).toBe("Holiday Check-in");
67+
});
68+
69+
it("handles mixed clean and whitespace-padded keys together", () => {
70+
const input: Record<string, unknown> = {
71+
name: "Test",
72+
"schedule ": { kind: "cron", expr: "0 * * * *" },
73+
sessionTarget: "isolated",
74+
"payload ": { kind: "agentTurn", message: "hi" },
75+
enabled: true,
76+
};
77+
78+
const result = canonicalizeCronToolObject(input);
79+
80+
expect(result).toHaveProperty("name");
81+
expect(result).toHaveProperty("schedule");
82+
expect(result).toHaveProperty("sessionTarget");
83+
expect(result).toHaveProperty("payload");
84+
expect(result).toHaveProperty("enabled");
85+
86+
for (const key of Object.keys(result)) {
87+
expect(key).toBe(key.trim());
88+
}
89+
});
90+
91+
it("wrapping via job property also trims keys", () => {
92+
const jobValue: Record<string, unknown> = {
93+
name: "Test",
94+
"schedule ": { kind: "at", at: "2026-12-25T00:00:00Z" },
95+
"sessionTarget ": "isolated",
96+
"payload ": { kind: "agentTurn", message: "test" },
97+
};
98+
99+
const wrapped = { job: jobValue };
100+
const result = canonicalizeCronToolObject(wrapped);
101+
102+
expect(result).toHaveProperty("schedule");
103+
expect(result).toHaveProperty("sessionTarget");
104+
expect(result).toHaveProperty("payload");
105+
for (const key of Object.keys(result)) {
106+
expect(key).toBe(key.trim());
107+
}
108+
});
109+
});

‎src/agents/tools/cron-tool-canonicalize.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,37 @@ function moveDefinedField(params: {
8585
return true;
8686
}
8787

88+
/**
89+
* Trim leading/trailing whitespace from object keys in-place.
90+
*
91+
* Some local tool-call parsers (observed with qwen35b via llamacpp) emit JSON
92+
* with whitespace-padded key names (e.g. `"schedule "` instead of `"schedule"`).
93+
* Trim those keys early so downstream repair and canonicalization helpers see
94+
* the expected canonical names. The gateway uses strict `additionalProperties:
95+
* false` schemas, so unpadded keys would be rejected before persistence.
96+
*/
97+
function trimWhitespacePaddedCronKeys(value: Record<string, unknown>): void {
98+
const entries = Object.entries(value);
99+
let changed = false;
100+
const clean: Array<[string, unknown]> = [];
101+
for (const [key, entry] of entries) {
102+
const trimmed = key.trim();
103+
if (trimmed !== key) {
104+
changed = true;
105+
}
106+
clean.push([trimmed, entry]);
107+
}
108+
if (!changed) {
109+
return;
110+
}
111+
for (const key of Object.keys(value)) {
112+
delete value[key];
113+
}
114+
for (const [key, entry] of clean) {
115+
value[key] = entry;
116+
}
117+
}
118+
88119
function repairConcatenatedCronToolKeys(value: Record<string, unknown>): void {
89120
// Some small/local tool-call parsers can return valid JSON with adjacent cron
90121
// key names merged. Recover only the observed schema-specific pairs before
@@ -249,6 +280,9 @@ export function canonicalizeCronToolObject(
249280
): Record<string, unknown> {
250281
const unwrapped = isRecord(value.data) ? value.data : isRecord(value.job) ? value.job : value;
251282
const next = { ...unwrapped };
283+
// Normalize whitespace-padded key names before repair so downstream helpers
284+
// operate on the canonical form (e.g. "schedule " → "schedule").
285+
trimWhitespacePaddedCronKeys(next);
252286
repairConcatenatedCronToolKeys(next);
253287
canonicalizeCronToolSchedule(next);
254288
canonicalizeCronToolPayload(next);

0 commit comments

Comments
 (0)