Skip to content

Commit 1c4f3f2

Browse files
committed
perf(agents): ship one automations job schema for add and update
The automations tool shipped two near-duplicate ~1.4k-token object schemas (job for add, patch for update) in every prompt on the default tool path — 13,016 chars of tool schema, 88% of it the duplicated pair. One nullable object schema now serves both actions (7,864 chars, ~1.3k tokens saved per prompt): update reads the job parameter, and the add path strips null clears before the strict gateway cron.add contract while preserving the agentId/sessionKey nulls that cron.add accepts. Fixes #121606
1 parent 8a59781 commit 1c4f3f2

10 files changed

Lines changed: 342 additions & 776 deletions

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,31 @@ export function canonicalizeCronToolObject(
313313
return next;
314314
}
315315

316+
// cron.add accepts these nulls, and a null sessionKey intentionally suppresses
317+
// default creator-session binding on create.
318+
const CRON_CREATE_NULLABLE_TOP_LEVEL_KEYS: ReadonlySet<string> = new Set(["agentId", "sessionKey"]);
319+
320+
function deleteNullFields(record: Record<string, unknown>, keep?: ReadonlySet<string>): void {
321+
for (const [key, entry] of Object.entries(record)) {
322+
if (entry === null && !keep?.has(key)) {
323+
delete record[key];
324+
} else if (isRecord(entry)) {
325+
deleteNullFields(entry);
326+
}
327+
}
328+
}
329+
330+
/**
331+
* Drops null-valued fields from a create job in place. The model-facing job
332+
* schema is shared with update, where null means "clear this field"; on create
333+
* there is nothing to clear, and the strict gateway cron.add contract rejects
334+
* the nulls its update patch accepts.
335+
*/
336+
export function stripCronCreateNullClears(value: Record<string, unknown>): Record<string, unknown> {
337+
deleteNullFields(value, CRON_CREATE_NULLABLE_TOP_LEVEL_KEYS);
338+
return value;
339+
}
340+
316341
/** Detects recovered update patches that contain no meaningful cron fields after normalization. */
317342
export function isEmptyRecoveredCronPatch(value: unknown): boolean {
318343
if (!isRecord(value)) {

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

Lines changed: 71 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import { gatewayCallOptionSchemaProperties } from "./gateway-schema.js";
1616
export const REMINDER_CONTEXT_MESSAGES_MAX = 10;
1717
export const CRON_TOOL_LIST_MAX_LIMIT = 200;
1818

19-
// Spell out job/patch properties for model-facing schema; runtime validation
20-
// still happens in normalizeCronJob* to avoid nested union schemas.
19+
// Spell out job properties for the model-facing schema; runtime validation
20+
// still happens in normalizeCronJob* to avoid nested union schemas. One object
21+
// schema serves both add (full job) and update (partial patch; null clears) so
22+
// the near-duplicate shape ships once per prompt instead of twice (#121606).
2123

2224
const CRON_ACTIONS = [
2325
"status",
@@ -45,54 +47,8 @@ function nullableStringArraySchema(description: string) {
4547
return Type.Optional(Type.Union([Type.Array(Type.String()), Type.Null()], { description }));
4648
}
4749

48-
function deliveryStringSchema(params: { description: string; nullableClears: boolean }) {
49-
return params.nullableClears
50-
? nullableStringSchema(`${params.description}, or null to clear`)
51-
: Type.Optional(Type.String({ description: params.description }));
52-
}
53-
54-
function deliveryThreadIdSchema(params: { nullableClears: boolean }) {
55-
const variants = params.nullableClears
56-
? [Type.String(), Type.Number(), Type.Null()]
57-
: [Type.String(), Type.Number()];
58-
return Type.Optional(Type.Union(variants, { description: "Thread/topic id" }));
59-
}
60-
61-
function failureDestinationModeSchema(params: { nullableClears: boolean }) {
62-
const variants = params.nullableClears
63-
? [Type.Literal("announce"), Type.Literal("webhook"), Type.Null()]
64-
: [Type.Literal("announce"), Type.Literal("webhook")];
65-
return Type.Optional(Type.Union(variants));
66-
}
67-
68-
function cronPayloadObjectSchema(params: {
69-
model: TSchema;
70-
toolsAllow: TSchema;
71-
fallbacks: TSchema;
72-
}) {
73-
return Type.Object(
74-
{
75-
kind: optionalStringEnum(CRON_PAYLOAD_KINDS, { description: "Payload kind" }),
76-
text: Type.Optional(Type.String({ description: "systemEvent text" })),
77-
message: Type.Optional(Type.String({ description: "agentTurn prompt" })),
78-
script: Type.Optional(Type.String({ description: "Headless code-mode script" })),
79-
model: params.model,
80-
thinking: Type.Optional(Type.String({ description: "Thinking override" })),
81-
timeoutSeconds: optionalFiniteNumberSchema({ minimum: 0 }),
82-
toolBudget: optionalPositiveIntegerSchema({ description: "Maximum script tool calls" }),
83-
lightContext: Type.Optional(
84-
Type.Boolean({
85-
description: "Lightweight bootstrap context (skip full workspace context)",
86-
}),
87-
),
88-
allowUnsafeExternalContent: Type.Optional(
89-
Type.Boolean({ description: "Allow untrusted external content in prompt" }),
90-
),
91-
fallbacks: params.fallbacks,
92-
toolsAllow: params.toolsAllow,
93-
},
94-
{ additionalProperties: true },
95-
);
50+
function deliveryStringSchema(description: string) {
51+
return nullableStringSchema(`${description}, or null to clear`);
9652
}
9753

9854
function createCronScheduleSchema(): TSchema {
@@ -142,7 +98,7 @@ function createCronScheduleSchema(): TSchema {
14298
);
14399
}
144100

145-
function createCronPacingSchema(params: { nullableClears: boolean }): TSchema {
101+
function createCronPacingSchema(): TSchema {
146102
const pacing = Type.Object(
147103
{
148104
min: Type.Optional(Type.String({ description: "Minimum dynamic delay" })),
@@ -153,11 +109,11 @@ function createCronPacingSchema(params: { nullableClears: boolean }): TSchema {
153109
description: "Dynamic-cadence bounds; at least one of min or max is required",
154110
},
155111
);
156-
return Type.Optional(params.nullableClears ? Type.Union([pacing, Type.Null()]) : pacing);
112+
return Type.Optional(Type.Union([pacing, Type.Null()]));
157113
}
158114

159-
export function assertCronPacingInput(value: unknown, params: { nullableClears: boolean }): void {
160-
if (value === undefined || (params.nullableClears && value === null)) {
115+
export function assertCronPacingInput(value: unknown): void {
116+
if (value === undefined || value === null) {
161117
return;
162118
}
163119
if (!isRecord(value)) {
@@ -168,41 +124,52 @@ export function assertCronPacingInput(value: unknown, params: { nullableClears:
168124

169125
function createCronPayloadSchema(): TSchema {
170126
return Type.Optional(
171-
cronPayloadObjectSchema({
172-
model: Type.Optional(Type.String({ description: "Model override" })),
173-
toolsAllow: Type.Optional(Type.Array(Type.String(), { description: "Allowed tools" })),
174-
fallbacks: Type.Optional(Type.Array(Type.String(), { description: "Fallback models" })),
175-
}),
127+
Type.Object(
128+
{
129+
kind: optionalStringEnum(CRON_PAYLOAD_KINDS, { description: "Payload kind" }),
130+
text: Type.Optional(Type.String({ description: "systemEvent text" })),
131+
message: Type.Optional(Type.String({ description: "agentTurn prompt" })),
132+
script: Type.Optional(Type.String({ description: "Headless code-mode script" })),
133+
model: nullableStringSchema("Model override, or null to clear"),
134+
thinking: Type.Optional(Type.String({ description: "Thinking override" })),
135+
timeoutSeconds: optionalFiniteNumberSchema({ minimum: 0 }),
136+
toolBudget: optionalPositiveIntegerSchema({ description: "Maximum script tool calls" }),
137+
lightContext: Type.Optional(
138+
Type.Boolean({
139+
description: "Lightweight bootstrap context (skip full workspace context)",
140+
}),
141+
),
142+
allowUnsafeExternalContent: Type.Optional(
143+
Type.Boolean({ description: "Allow untrusted external content in prompt" }),
144+
),
145+
fallbacks: nullableStringArraySchema("Fallback models, or null to clear"),
146+
toolsAllow: nullableStringArraySchema("Allowed tool ids, or null to clear"),
147+
},
148+
{ additionalProperties: true },
149+
),
176150
);
177151
}
178152

179-
function createCronTriggerSchema(params: { nullableClears: boolean }): TSchema {
153+
function createCronTriggerSchema(): TSchema {
180154
const trigger = Type.Object(
181155
{
182156
script: Type.String({ minLength: 1, maxLength: 65_536 }),
183157
once: Type.Optional(Type.Boolean()),
184158
},
185159
{ additionalProperties: false },
186160
);
187-
return Type.Optional(params.nullableClears ? Type.Union([trigger, Type.Null()]) : trigger);
161+
return Type.Optional(Type.Union([trigger, Type.Null()]));
188162
}
189163

190-
function cronDeliverySchema(params: { nullableClears: boolean }) {
164+
function createCronDeliverySchema(): TSchema {
191165
const failureDestinationObject = Type.Object(
192166
{
193-
channel: deliveryStringSchema({
194-
description: "Failure delivery channel",
195-
nullableClears: params.nullableClears,
196-
}),
197-
to: deliveryStringSchema({
198-
description: "Failure delivery target",
199-
nullableClears: params.nullableClears,
200-
}),
201-
accountId: deliveryStringSchema({
202-
description: "Failure delivery account",
203-
nullableClears: params.nullableClears,
204-
}),
205-
mode: failureDestinationModeSchema({ nullableClears: params.nullableClears }),
167+
channel: deliveryStringSchema("Failure delivery channel"),
168+
to: deliveryStringSchema("Failure delivery target"),
169+
accountId: deliveryStringSchema("Failure delivery account"),
170+
mode: Type.Optional(
171+
Type.Union([Type.Literal("announce"), Type.Literal("webhook"), Type.Null()]),
172+
),
206173
},
207174
{ additionalProperties: true },
208175
);
@@ -214,59 +181,38 @@ function cronDeliverySchema(params: { nullableClears: boolean }) {
214181
description: "Completion webhook target; only valid with delivery.mode=announce",
215182
}),
216183
},
217-
{
218-
additionalProperties: true,
219-
description: "Additional completion webhook; requires delivery.mode=announce",
220-
},
184+
{ additionalProperties: true },
221185
);
222186

223187
return Type.Optional(
224188
Type.Object(
225189
{
226190
mode: optionalStringEnum(CRON_DELIVERY_MODES, { description: "Delivery mode" }),
227-
channel: deliveryStringSchema({
228-
description: "Delivery channel",
229-
nullableClears: params.nullableClears,
230-
}),
231-
to: deliveryStringSchema({
232-
description: "Delivery target",
233-
nullableClears: params.nullableClears,
234-
}),
235-
threadId: deliveryThreadIdSchema({ nullableClears: params.nullableClears }),
191+
channel: deliveryStringSchema("Delivery channel"),
192+
to: deliveryStringSchema("Delivery target"),
193+
threadId: Type.Optional(
194+
Type.Union([Type.String(), Type.Number(), Type.Null()], {
195+
description: "Thread/topic id",
196+
}),
197+
),
236198
bestEffort: Type.Optional(Type.Boolean()),
237-
accountId: deliveryStringSchema({
238-
description: "Delivery account",
239-
nullableClears: params.nullableClears,
240-
}),
241-
failureDestination: params.nullableClears
242-
? Type.Optional(
243-
Type.Union([failureDestinationObject, Type.Null()], {
244-
description: "Failure destination; null clears.",
245-
}),
246-
)
247-
: Type.Optional(failureDestinationObject),
248-
completionDestination: params.nullableClears
249-
? Type.Optional(
250-
Type.Union([completionDestinationObject, Type.Null()], {
251-
description:
252-
"Completion webhook destination; requires delivery.mode=announce; null clears.",
253-
}),
254-
)
255-
: Type.Optional(completionDestinationObject),
199+
accountId: deliveryStringSchema("Delivery account"),
200+
failureDestination: Type.Optional(
201+
Type.Union([failureDestinationObject, Type.Null()], {
202+
description: "Failure destination; null clears.",
203+
}),
204+
),
205+
completionDestination: Type.Optional(
206+
Type.Union([completionDestinationObject, Type.Null()], {
207+
description: "Completion webhook; requires delivery.mode=announce; null clears.",
208+
}),
209+
),
256210
},
257211
{ additionalProperties: true },
258212
),
259213
);
260214
}
261215

262-
function createCronDeliverySchema(): TSchema {
263-
return cronDeliverySchema({ nullableClears: false });
264-
}
265-
266-
function createCronDeliveryPatchSchema(): TSchema {
267-
return cronDeliverySchema({ nullableClears: true });
268-
}
269-
270216
// Omitting `failureAlert` means "leave defaults/unchanged"; `false` explicitly disables alerts.
271217
// Runtime handles `failureAlert === false` in cron/service/timer.ts.
272218
// The schema declares `type: "object"` to stay compatible with providers that
@@ -298,13 +244,15 @@ function createCronJobObjectSchema(): TSchema {
298244
name: Type.Optional(Type.String({ description: "Job name" })),
299245
declarationKey: Type.Optional(
300246
Type.String({
301-
description: "Idempotent declaration key.",
247+
description: "Idempotent declaration key (add only).",
302248
minLength: 1,
303249
maxLength: 200,
304250
}),
305251
),
306252
displayName: Type.Optional(
307-
Type.String({ description: "Human-readable declarative job label", maxLength: 200 }),
253+
Type.Union([Type.String({ maxLength: 200 }), Type.Null()], {
254+
description: "Human-readable label; null clears it",
255+
}),
308256
),
309257
owner: Type.Optional(
310258
Type.Object(
@@ -316,8 +264,8 @@ function createCronJobObjectSchema(): TSchema {
316264
),
317265
),
318266
schedule: createCronScheduleSchema(),
319-
pacing: createCronPacingSchema({ nullableClears: false }),
320-
trigger: createCronTriggerSchema({ nullableClears: false }),
267+
pacing: createCronPacingSchema(),
268+
trigger: createCronTriggerSchema(),
321269
sessionTarget: Type.Optional(
322270
Type.String({
323271
description: "main | isolated | current (agentTurn default) | session:<id>",
@@ -326,53 +274,18 @@ function createCronJobObjectSchema(): TSchema {
326274
wakeMode: optionalStringEnum(CRON_WAKE_MODES, { description: "Wake timing" }),
327275
payload: createCronPayloadSchema(),
328276
delivery: createCronDeliverySchema(),
329-
agentId: nullableStringSchema("Agent id, or null to keep it unset"),
277+
agentId: nullableStringSchema("Agent id, or null to clear it"),
330278
description: Type.Optional(Type.String({ description: "Human description" })),
331279
enabled: Type.Optional(Type.Boolean()),
332280
deleteAfterRun: Type.Optional(Type.Boolean({ description: "Delete after first run" })),
333281
sessionKey: nullableStringSchema("Explicit session key, or null to clear it"),
334282
failureAlert: createCronFailureAlertSchema(),
335283
},
336-
{ additionalProperties: true },
337-
),
338-
);
339-
}
340-
341-
function createCronPatchObjectSchema(): TSchema {
342-
return Type.Optional(
343-
Type.Object(
344284
{
345-
name: Type.Optional(Type.String({ description: "Job name" })),
346-
displayName: Type.Optional(
347-
Type.Union([Type.String({ maxLength: 200 }), Type.Null()], {
348-
description: "Human-readable label; null clears it",
349-
}),
350-
),
351-
schedule: createCronScheduleSchema(),
352-
pacing: createCronPacingSchema({ nullableClears: true }),
353-
trigger: createCronTriggerSchema({ nullableClears: true }),
354-
sessionTarget: Type.Optional(
355-
Type.String({
356-
description: "main | isolated | current (agentTurn default) | session:<id>",
357-
}),
358-
),
359-
wakeMode: optionalStringEnum(CRON_WAKE_MODES),
360-
payload: Type.Optional(
361-
cronPayloadObjectSchema({
362-
model: nullableStringSchema("Model override, or null to clear"),
363-
toolsAllow: nullableStringArraySchema("Allowed tool ids, or null to clear"),
364-
fallbacks: nullableStringArraySchema("Fallback models, or null to clear"),
365-
}),
366-
),
367-
delivery: createCronDeliveryPatchSchema(),
368-
description: Type.Optional(Type.String()),
369-
enabled: Type.Optional(Type.Boolean()),
370-
deleteAfterRun: Type.Optional(Type.Boolean()),
371-
agentId: nullableStringSchema("Agent id, or null to clear it"),
372-
sessionKey: nullableStringSchema("Explicit session key, or null to clear it"),
373-
failureAlert: createCronFailureAlertSchema(),
285+
additionalProperties: true,
286+
description:
287+
'Job fields. action="add": full job. action="update": partial patch — only supplied fields change; null clears.',
374288
},
375-
{ additionalProperties: true },
376289
),
377290
);
378291
}
@@ -394,7 +307,6 @@ export function createCronToolSchema(): TSchema {
394307
job: createCronJobObjectSchema(),
395308
jobId: Type.Optional(Type.String()),
396309
id: Type.Optional(Type.String()),
397-
patch: createCronPatchObjectSchema(),
398310
in: Type.Optional(
399311
Type.String({
400312
description: 'Relative duration for action="next_check" (for example, "15m")',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ describe("cron tool flat-params", () => {
431431
await tool.execute("call-patch-trailing-space", {
432432
action: "update",
433433
jobId: "job-123",
434-
patch: {
434+
job: {
435435
"schedule ": { kind: "cron", expr: "0 9 * * 1-5", tz: "America/New_York" },
436436
"enabled ": false,
437437
},

0 commit comments

Comments
 (0)