Skip to content

Commit 17066f2

Browse files
committed
fix(cron): preserve default toolsAllow markers safely
1 parent 9aea104 commit 17066f2

7 files changed

Lines changed: 134 additions & 53 deletions

File tree

‎packages/gateway-protocol/src/schema/cron.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ function cronAgentTurnPayloadSchema(params: {
2727
allowUnsafeExternalContent: Type.Optional(Type.Boolean()),
2828
lightContext: Type.Optional(Type.Boolean()),
2929
toolsAllow: Type.Optional(params.toolsAllow),
30-
// Server-managed marker: set when toolsAllow is an auto-applied creator
31-
// default (vs an explicit per-cron restriction). The cron tool stamps it,
32-
// and it round-trips through persistence so a CLI-resolved run can drop an
33-
// unenforceable default cap. Must be on the wire contract or the stamped
34-
// payload is rejected by additionalProperties:false.
30+
// Server-managed marker for auto-stamped defaults; persisted so CLI cron
31+
// runs can drop only the cap that was never user-explicit.
3532
toolsAllowIsDefault: Type.Optional(Type.Boolean()),
3633
},
3734
{ additionalProperties: false },

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,6 @@ function capCronAgentTurnToolsAllow(params: {
465465
? params.payload.toolsAllow
466466
: params.defaultToolsAllow;
467467
if (!Array.isArray(requestedRaw)) {
468-
// No explicit user toolsAllow: stamp the full creator surface as the default
469-
// cap and flag it so a CLI-resolved run (which cannot enforce a runtime
470-
// toolsAllow) drops it rather than failing to start.
471468
params.payload.toolsAllow = creatorToolNames;
472469
params.payload.toolsAllowIsDefault = true;
473470
return;
@@ -476,15 +473,11 @@ function capCronAgentTurnToolsAllow(params: {
476473
requestedRaw.filter((entry): entry is string => typeof entry === "string"),
477474
);
478475
if (requestedToolsAllow.length === 0) {
479-
// Explicit empty allow-list is a real restriction (no tools): keep it
480-
// fail-closed, never flagged as a droppable default.
481476
params.payload.toolsAllow = [];
482477
delete params.payload.toolsAllowIsDefault;
483478
return;
484479
}
485480
if (requestedToolsAllow.includes("*")) {
486-
// Wildcard requests the creator's full surface with no narrowing — a
487-
// non-restrictive default, droppable on a non-enforcing CLI backend.
488481
params.payload.toolsAllow = creatorToolNames;
489482
params.payload.toolsAllowIsDefault = true;
490483
return;
@@ -497,9 +490,6 @@ function capCronAgentTurnToolsAllow(params: {
497490
{ allow: requestedToolsAllow },
498491
pluginGroups,
499492
);
500-
// Explicit narrowing restriction: keep it fail-closed (no default flag) so a
501-
// CLI backend that cannot enforce it surfaces the error instead of silently
502-
// widening the requested tool surface.
503493
params.payload.toolsAllow = creatorToolNames.filter((toolName) =>
504494
isToolAllowedByPolicyName(toolName, requestedPolicy),
505495
);
@@ -563,13 +553,8 @@ async function capCronAgentTurnUpdatePatchToolsAllow(params: {
563553
capCronAgentTurnToolsAllow({
564554
payload: nextPayload,
565555
creatorToolAllowlist: params.creatorToolAllowlist,
566-
// Carry the existing cap forward across a no-toolsAllow update ONLY when it
567-
// was an explicit per-cron restriction. A previously auto-stamped default
568-
// (toolsAllowIsDefault === true) must NOT be carried forward as a literal
569-
// value: that would route it through the explicit-narrowing branch, strip
570-
// the default flag, and re-break the job on a CLI backend after the next
571-
// restart (the run-time drop keys off the flag). Omitting it lets the cap
572-
// be re-derived as a flagged default from the current creator surface.
556+
// Flagged defaults are re-derived so normal updates do not turn them into
557+
// explicit restrictions or lose the marker needed after restart.
573558
defaultToolsAllow:
574559
existingPayloadKind === "agentTurn" &&
575560
isRecord(existingPayload) &&

‎src/cron/isolated-agent/run-executor.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,8 @@ function resolveCliRuntimeToolsAllow(
168168
if (toolsAllow === undefined) {
169169
return undefined;
170170
}
171-
// CLI backends do not enforce a runtime toolsAllow: cli-runner/prepare.ts
172-
// rejects any defined allow-list, and a CLI backend runs with its own
173-
// configured tool set as the operator on the local machine — it is not a
174-
// runtime tool-policy boundary. #91499 auto-stamps the creator surface as a
175-
// *default* cap on agentTurn payloads (toolsAllowIsDefault); on a CLI backend
176-
// that default is unenforceable theatre, so drop it and let the scheduled run
177-
// proceed instead of failing to start.
178-
//
179-
// An EXPLICIT per-cron restriction (the user narrowed this job; no isDefault
180-
// flag) is deliberately NOT dropped: it falls through and prepare.ts fails it
181-
// closed, surfacing that the requested policy needs an embedded runtime. So
182-
// only the unenforceable inherited default is relaxed on CLI; explicit intent
183-
// is still honoured.
171+
// CLI runners reject runtime toolsAllow. Drop only the auto-stamped default;
172+
// explicit per-cron restrictions stay fail-closed in prepareCliRunContext.
184173
if (toolsAllowIsDefault) {
185174
return undefined;
186175
}

‎src/cron/service.jobs.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,63 @@ describe("applyJobPatch", () => {
392392
}
393393
});
394394

395+
it("clears the default toolsAllow flag when editing to an explicit restriction", () => {
396+
const job = createIsolatedAgentTurnJob("job-tools-explicit", {
397+
mode: "announce",
398+
channel: "telegram",
399+
});
400+
job.payload = {
401+
kind: "agentTurn",
402+
message: "do it",
403+
toolsAllow: ["exec", "read"],
404+
toolsAllowIsDefault: true,
405+
};
406+
407+
applyJobPatch(job, {
408+
payload: {
409+
kind: "agentTurn",
410+
message: "do it",
411+
toolsAllow: ["read"],
412+
toolsAllowIsDefault: true,
413+
},
414+
});
415+
416+
expect(job.payload.kind).toBe("agentTurn");
417+
if (job.payload.kind === "agentTurn") {
418+
expect(job.payload.toolsAllow).toEqual(["read"]);
419+
expect(job.payload.toolsAllowIsDefault).toBeUndefined();
420+
}
421+
});
422+
423+
it("preserves the default toolsAllow flag when a full payload edit keeps the default list", () => {
424+
const job = createIsolatedAgentTurnJob("job-tools-default-edit", {
425+
mode: "announce",
426+
channel: "telegram",
427+
});
428+
job.payload = {
429+
kind: "agentTurn",
430+
message: "do it",
431+
toolsAllow: ["exec", "read"],
432+
toolsAllowIsDefault: true,
433+
};
434+
435+
applyJobPatch(job, {
436+
payload: {
437+
kind: "agentTurn",
438+
message: "do it later",
439+
toolsAllow: ["exec", "read"],
440+
toolsAllowIsDefault: true,
441+
},
442+
});
443+
444+
expect(job.payload.kind).toBe("agentTurn");
445+
if (job.payload.kind === "agentTurn") {
446+
expect(job.payload.message).toBe("do it later");
447+
expect(job.payload.toolsAllow).toEqual(["exec", "read"]);
448+
expect(job.payload.toolsAllowIsDefault).toBe(true);
449+
}
450+
});
451+
395452
it("clears agentTurn payload.toolsAllow when patch requests null", () => {
396453
const job = createIsolatedAgentTurnJob("job-tools-clear", {
397454
mode: "announce",
@@ -401,6 +458,7 @@ describe("applyJobPatch", () => {
401458
kind: "agentTurn",
402459
message: "do it",
403460
toolsAllow: ["exec", "read"],
461+
toolsAllowIsDefault: true,
404462
};
405463

406464
applyJobPatch(job, {
@@ -414,6 +472,7 @@ describe("applyJobPatch", () => {
414472
expect(job.payload.kind).toBe("agentTurn");
415473
if (job.payload.kind === "agentTurn") {
416474
expect(job.payload.toolsAllow).toBeUndefined();
475+
expect(job.payload.toolsAllowIsDefault).toBeUndefined();
417476
}
418477
});
419478

@@ -527,6 +586,30 @@ describe("applyJobPatch", () => {
527586
}
528587
});
529588

589+
it("carries payload.toolsAllow default flag when replacing payload kind via patch", () => {
590+
const job = createIsolatedAgentTurnJob("job-tools-default-switch", {
591+
mode: "announce",
592+
channel: "telegram",
593+
});
594+
job.payload = { kind: "systemEvent", text: "ping" };
595+
596+
applyJobPatch(job, {
597+
payload: {
598+
kind: "agentTurn",
599+
message: "do it",
600+
toolsAllow: ["exec", "read"],
601+
toolsAllowIsDefault: true,
602+
},
603+
});
604+
605+
const payload = job.payload as CronJob["payload"];
606+
expect(payload.kind).toBe("agentTurn");
607+
if (payload.kind === "agentTurn") {
608+
expect(payload.toolsAllow).toEqual(["exec", "read"]);
609+
expect(payload.toolsAllowIsDefault).toBe(true);
610+
}
611+
});
612+
530613
it.each([
531614
{ name: "no delivery update", patch: { enabled: true } satisfies CronJobPatch },
532615
{

‎src/cron/service/jobs.ts‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const STUCK_RUN_MS = 2 * 60 * 60 * 1000;
4040
const STAGGER_OFFSET_CACHE_MAX = 4096;
4141
const staggerOffsetCache = new Map<string, number>();
4242

43+
type CronAgentTurnPayload = Extract<CronPayload, { kind: "agentTurn" }>;
44+
type CronAgentTurnPayloadPatch = Extract<CronPayloadPatch, { kind: "agentTurn" }>;
45+
4346
/** Default retry delays applied after consecutive cron execution errors. */
4447
export const DEFAULT_ERROR_BACKOFF_SCHEDULE_MS = [
4548
30_000,
@@ -897,6 +900,42 @@ export function applyJobPatch(
897900
}
898901
}
899902

903+
function applyAgentTurnToolsAllowPatch(
904+
payload: CronAgentTurnPayload,
905+
patch: CronAgentTurnPayloadPatch,
906+
existing?: CronAgentTurnPayload,
907+
): void {
908+
if (Array.isArray(patch.toolsAllow)) {
909+
payload.toolsAllow = patch.toolsAllow;
910+
// Same-kind edits keep the marker only when the default list is unchanged;
911+
// kind replacements carry the cron-tool-stamped marker into persistence.
912+
if (
913+
patch.toolsAllowIsDefault === true &&
914+
(!existing || (existing.toolsAllowIsDefault === true && toolsAllowEqual(existing, patch)))
915+
) {
916+
payload.toolsAllowIsDefault = true;
917+
} else {
918+
delete payload.toolsAllowIsDefault;
919+
}
920+
} else if (patch.toolsAllow === null) {
921+
delete payload.toolsAllow;
922+
delete payload.toolsAllowIsDefault;
923+
}
924+
}
925+
926+
function toolsAllowEqual(
927+
left: Pick<CronAgentTurnPayload, "toolsAllow">,
928+
right: Pick<CronAgentTurnPayloadPatch, "toolsAllow">,
929+
): boolean {
930+
const rightToolsAllow = right.toolsAllow;
931+
return (
932+
Array.isArray(left.toolsAllow) &&
933+
Array.isArray(rightToolsAllow) &&
934+
left.toolsAllow.length === rightToolsAllow.length &&
935+
left.toolsAllow.every((toolName, index) => toolName === rightToolsAllow[index])
936+
);
937+
}
938+
900939
function mergeCronPayload(existing: CronPayload, patch: CronPayloadPatch): CronPayload {
901940
if (patch.kind !== existing.kind) {
902941
return buildPayloadFromPatch(patch);
@@ -943,7 +982,7 @@ function mergeCronPayload(existing: CronPayload, patch: CronPayloadPatch): CronP
943982
return buildPayloadFromPatch(patch);
944983
}
945984

946-
const next: Extract<CronPayload, { kind: "agentTurn" }> = { ...existing };
985+
const next: CronAgentTurnPayload = { ...existing };
947986
if (typeof patch.message === "string") {
948987
next.message = patch.message;
949988
}
@@ -957,11 +996,7 @@ function mergeCronPayload(existing: CronPayload, patch: CronPayloadPatch): CronP
957996
} else if (patch.fallbacks === null) {
958997
delete next.fallbacks;
959998
}
960-
if (Array.isArray(patch.toolsAllow)) {
961-
next.toolsAllow = patch.toolsAllow;
962-
} else if (patch.toolsAllow === null) {
963-
delete next.toolsAllow;
964-
}
999+
applyAgentTurnToolsAllowPatch(next, patch, existing);
9651000
if (typeof patch.thinking === "string") {
9661001
next.thinking = patch.thinking;
9671002
}
@@ -1005,17 +1040,18 @@ function buildPayloadFromPatch(patch: CronPayloadPatch): CronPayload {
10051040
throw new Error('cron.update payload.kind="agentTurn" requires message');
10061041
}
10071042

1008-
return {
1043+
const next: CronAgentTurnPayload = {
10091044
kind: "agentTurn",
10101045
message: patch.message,
10111046
model: typeof patch.model === "string" ? patch.model : undefined,
10121047
fallbacks: Array.isArray(patch.fallbacks) ? patch.fallbacks : undefined,
1013-
toolsAllow: Array.isArray(patch.toolsAllow) ? patch.toolsAllow : undefined,
10141048
thinking: patch.thinking,
10151049
timeoutSeconds: patch.timeoutSeconds,
10161050
lightContext: patch.lightContext,
10171051
allowUnsafeExternalContent: patch.allowUnsafeExternalContent,
10181052
};
1053+
applyAgentTurnToolsAllowPatch(next, patch);
1054+
return next;
10191055
}
10201056

10211057
function mergeCronDelivery(

‎src/cron/store/payload-codec.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ export function bindPayloadColumns(
119119
payload_external_content_source_json: serializeJson(payload.externalContentSource),
120120
payload_light_context: booleanToInteger(payload.lightContext),
121121
payload_tools_allow_json: serializeJson(payload.toolsAllow),
122-
// Persist whether toolsAllow is the auto-applied creator-surface default
123-
// (#91499) so a CLI-resolved run can drop the unenforceable cap after a
124-
// restart instead of failing. Only meaningful when toolsAllow is set.
125122
payload_tools_allow_is_default: payload.toolsAllow
126123
? booleanToInteger(payload.toolsAllowIsDefault)
127124
: null,

‎src/cron/types.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,7 @@ type CronAgentTurnPayloadFields = {
248248
lightContext?: boolean;
249249
/** Optional tool allow-list; when set, only these tools are sent to the model. */
250250
toolsAllow?: string[];
251-
/**
252-
* Set by the server when `toolsAllow` is the auto-applied creator-surface cap
253-
* (#91499) rather than an explicit user restriction. CLI backends cannot
254-
* enforce a runtime `toolsAllow`, so a CLI-resolved run drops this default cap
255-
* (it is unenforceable) instead of failing; an explicit user restriction has
256-
* no flag and stays fail-closed. Not user-settable.
257-
*/
251+
/** Server-managed marker for auto-stamped defaults; explicit restrictions omit it. */
258252
toolsAllowIsDefault?: boolean;
259253
};
260254

0 commit comments

Comments
 (0)