Skip to content

Commit f7355ce

Browse files
committed
fix(cron): recover whitespace-padded tool keys before validation
1 parent 269e44e commit f7355ce

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ function repairConcatenatedCronToolKeys(value: Record<string, unknown>): void {
110110
delete value.sessionTargetName;
111111
}
112112

113+
function repairWhitespacePaddedCronToolKeys(value: Record<string, unknown>): void {
114+
// Some small/local tool-call serializers emit valid JSON where specific cron
115+
// keys carry leading/trailing whitespace (e.g. `"schedule "`, `"enabled "`).
116+
// Trim back onto the canonical key before strict gateway validation rejects
117+
// the padded property names. Only recover known cron keys, and skip when the
118+
// canonical key already exists so a genuine duplicate is left for validation
119+
// instead of being silently merged.
120+
for (const key of Object.keys(value)) {
121+
const trimmed = key.trim();
122+
if (trimmed === key || !CRON_RECOVERABLE_OBJECT_KEYS.has(trimmed)) {
123+
continue;
124+
}
125+
if (value[trimmed] !== undefined) {
126+
continue;
127+
}
128+
value[trimmed] = value[key];
129+
delete value[key];
130+
}
131+
}
132+
113133
function setScheduleAtMs(schedule: Record<string, unknown>, value: unknown): void {
114134
const atMs = typeof value === "number" ? value : Number(value);
115135
// Invalid/out-of-range timestamps stay raw so cron gateway validation reports the user error.
@@ -249,6 +269,7 @@ export function canonicalizeCronToolObject(
249269
): Record<string, unknown> {
250270
const unwrapped = isRecord(value.data) ? value.data : isRecord(value.job) ? value.job : value;
251271
const next = { ...unwrapped };
272+
repairWhitespacePaddedCronToolKeys(next);
252273
repairConcatenatedCronToolKeys(next);
253274
canonicalizeCronToolSchedule(next);
254275
canonicalizeCronToolPayload(next);

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,52 @@ describe("cron tool", () => {
10981098
});
10991099
});
11001100

1101+
it("recovers whitespace-padded cron add keys from local tool-call parsers", async () => {
1102+
const tool = createTestCronTool();
1103+
await tool.execute("call-padded-add", {
1104+
action: "add",
1105+
job: {
1106+
name: "Holiday Check-in",
1107+
description: "Casual check-in while on holiday, 2x per day",
1108+
"schedule ": { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" },
1109+
"sessionTarget ": "isolated",
1110+
"payload ": { kind: "agentTurn", message: "Holiday check-in." },
1111+
"enabled ": true,
1112+
},
1113+
});
1114+
1115+
const params = expectSingleGatewayCallMethod("cron.add") as Record<string, unknown>;
1116+
expect(Object.keys(params).every((key) => key === key.trim())).toBe(true);
1117+
expect(params).toMatchObject({
1118+
name: "Holiday Check-in",
1119+
description: "Casual check-in while on holiday, 2x per day",
1120+
schedule: { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" },
1121+
sessionTarget: "isolated",
1122+
payload: { kind: "agentTurn", message: "Holiday check-in." },
1123+
enabled: true,
1124+
});
1125+
});
1126+
1127+
it("keeps a padded duplicate key when the canonical cron key already exists", async () => {
1128+
const tool = createTestCronTool();
1129+
await tool.execute("call-padded-duplicate-add", {
1130+
action: "add",
1131+
job: {
1132+
name: "dup",
1133+
schedule: { kind: "cron", expr: "0 9 * * *" },
1134+
"enabled ": true,
1135+
enabled: false,
1136+
payload: { kind: "agentTurn", message: "dup test" },
1137+
},
1138+
});
1139+
1140+
// Genuine duplicates are not silently merged: the padded key survives so
1141+
// strict gateway validation surfaces the conflict instead of guessing.
1142+
const params = expectSingleGatewayCallMethod("cron.add") as Record<string, unknown>;
1143+
expect(params.enabled).toBe(false);
1144+
expect(params["enabled "]).toBe(true);
1145+
});
1146+
11011147
it("stamps cron.add with caller sessionKey when missing", async () => {
11021148
callGatewayMock.mockResolvedValueOnce({ ok: true });
11031149

0 commit comments

Comments
 (0)