Skip to content

Commit a1ad608

Browse files
committed
fix(cron): restrict key trimming to known cron keys and fix test assertions
- Rename trimObjectKeys to trimKnownCronKeys and restrict to CRON_RECOVERABLE_OBJECT_KEYS - Apply trimming after unwrapping data/job wrappers, not before - Fix test assertions to check top-level cron.add params, not wrapped job - Add expected staggerMs for top-of-hour cron expression Fixes ClawSweeper review findings for PR #95458: - P1: Restrict whitespace repair to recognized cron keys - P1: Run whitespace repair after unwrapping - P2: Assert direct cron.add payload Fixes #95407
1 parent aa57ea0 commit a1ad608

2 files changed

Lines changed: 31 additions & 30 deletions

File tree

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,24 @@ function moveDefinedField(params: {
8686
}
8787

8888
/**
89-
* Trims whitespace from object keys to handle malformed model outputs.
89+
* Trims whitespace from known cron keys to handle malformed model outputs.
9090
* Some small/local models (e.g. qwen35b via llamacpp) emit JSON with trailing
9191
* whitespace in object keys (e.g. `"schedule "` instead of `"schedule"`).
92-
* See: https://github.com/openclaw/openclaw/issues/95407
92+
* Only trims recognized cron keys to avoid silent field overwrites or
93+
* prototype-sensitive assignments. See: https://github.com/openclaw/openclaw/issues/95407
9394
*/
94-
function trimObjectKeys(value: Record<string, unknown>): Record<string, unknown> {
95+
function trimKnownCronKeys(value: Record<string, unknown>): Record<string, unknown> {
9596
const trimmed: Record<string, unknown> = {};
96-
for (const key of Object.keys(value)) {
97-
const clean = key.trim();
98-
if (clean.length > 0) {
99-
trimmed[clean] = value[key];
97+
for (const [key, entry] of Object.entries(value)) {
98+
if (CRON_RECOVERABLE_OBJECT_KEYS.has(key)) {
99+
trimmed[key] = entry;
100+
continue;
101+
}
102+
const cleanKey = key.trim();
103+
if (CRON_RECOVERABLE_OBJECT_KEYS.has(cleanKey)) {
104+
trimmed[cleanKey] = entry;
105+
} else {
106+
trimmed[key] = entry;
100107
}
101108
}
102109
return trimmed;
@@ -264,15 +271,10 @@ function canonicalizeCronToolPayload(value: Record<string, unknown>): void {
264271
export function canonicalizeCronToolObject(
265272
value: Record<string, unknown>,
266273
): Record<string, unknown> {
267-
// Trim whitespace from keys to handle malformed model outputs
268-
// Some small/local models emit keys with trailing spaces (e.g. "schedule ")
269-
const trimmedValue = trimObjectKeys(value);
270-
const unwrapped = isRecord(trimmedValue.data)
271-
? trimmedValue.data
272-
: isRecord(trimmedValue.job)
273-
? trimmedValue.job
274-
: trimmedValue;
275-
const next = { ...unwrapped };
274+
// Unwrap data/job wrappers first, then trim whitespace from known keys
275+
// to handle malformed model outputs (e.g. "schedule " instead of "schedule")
276+
const unwrapped = isRecord(value.data) ? value.data : isRecord(value.job) ? value.job : value;
277+
const next = trimKnownCronKeys({ ...unwrapped });
276278
repairConcatenatedCronToolKeys(next);
277279
canonicalizeCronToolSchedule(next);
278280
canonicalizeCronToolPayload(next);
@@ -299,9 +301,9 @@ export function recoverCronObjectFromFlatParams(params: Record<string, unknown>)
299301
found: boolean;
300302
value: Record<string, unknown>;
301303
} {
302-
// Trim whitespace from keys to handle malformed model outputs
304+
// Trim whitespace from known keys to handle malformed model outputs
303305
// Some small/local models emit keys with trailing spaces (e.g. "schedule ")
304-
const trimmedParams = trimObjectKeys(params);
306+
const trimmedParams = trimKnownCronKeys(params);
305307
const value: Record<string, unknown> = {};
306308
let found = false;
307309
for (const key of Object.keys(trimmedParams)) {

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,27 +207,26 @@ describe("cron tool flat-params", () => {
207207
});
208208

209209
const [method, _gatewayOpts, params] = firstGatewayToolCall<{
210-
job?: {
211-
name?: string;
212-
schedule?: unknown;
213-
sessionTarget?: string;
214-
payload?: unknown;
215-
enabled?: boolean;
216-
};
210+
name?: string;
211+
schedule?: unknown;
212+
sessionTarget?: string;
213+
payload?: unknown;
214+
enabled?: boolean;
217215
}>();
218216
expect(method).toBe("cron.add");
219-
expect(params.job?.name).toBe("test job");
220-
expect(params.job?.schedule).toEqual({
217+
expect(params.name).toBe("test job");
218+
expect(params.schedule).toEqual({
221219
kind: "cron",
222220
expr: "0 * * * *",
223221
tz: "UTC",
222+
staggerMs: 300000,
224223
});
225-
expect(params.job?.sessionTarget).toBe("isolated");
226-
expect(params.job?.payload).toEqual({
224+
expect(params.sessionTarget).toBe("isolated");
225+
expect(params.payload).toEqual({
227226
kind: "agentTurn",
228227
message: "test message",
229228
});
230-
expect(params.job?.enabled).toBe(true);
229+
expect(params.enabled).toBe(true);
231230
});
232231

233232
it("trims trailing whitespace from flat params (Issue #95407)", async () => {

0 commit comments

Comments
 (0)