Skip to content

Commit 1e5dbcd

Browse files
committed
fix(cron): fail closed on lost exec pins
1 parent 7a6d056 commit 1e5dbcd

22 files changed

Lines changed: 531 additions & 13 deletions

‎src/commands/doctor/cron/index.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,39 @@ function mockExdevRename(filePath: string) {
180180
}
181181

182182
describe("collectLegacyCronStoreHealthFindings", () => {
183+
it("reports alias-only Gateway exec jobs with recreation guidance", async () => {
184+
const storePath = await makeTempStorePath();
185+
await writeCurrentCronStore(storePath, [
186+
createCurrentCronJob({
187+
id: "legacy-gateway-exec",
188+
name: "Legacy gateway shell",
189+
scheduledToolPolicy: { version: 1, mode: "trusted" },
190+
payload: {
191+
kind: "agentTurn",
192+
message: "run",
193+
toolsAllow: ["gateway_exec"],
194+
},
195+
}),
196+
]);
197+
198+
const findings = await collectLegacyCronStoreHealthFindings({
199+
cfg: createCronConfig(storePath),
200+
});
201+
202+
expect(findings).toEqual(
203+
expect.arrayContaining([
204+
expect.objectContaining({
205+
requirement: "legacy-gateway-exec-recreation",
206+
message: expect.stringContaining("retired `gateway_exec` alias"),
207+
fixHint: expect.stringContaining("fresh authenticated creator turn"),
208+
}),
209+
]),
210+
);
211+
expect((await readPersistedJobs(storePath))[0]?.payload).toMatchObject({
212+
toolsAllow: ["gateway_exec"],
213+
});
214+
});
215+
183216
it("reports legacy cron store, run-log, and payload findings without mutating files", async () => {
184217
const storePath = await makeTempStorePath();
185218
await writeLegacyCronArrayStore(storePath, [

‎src/commands/doctor/cron/index.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {
2626
formatLegacyIssuePreview,
2727
formatIncompleteInheritedAuthorityAdvisory,
28+
formatLegacyGatewayExecAdvisory,
2829
formatScheduledToolPolicyAdvisory,
2930
formatUnresolvedCommandPromptAdvisory,
3031
formatUnresolvedShellPromptAdvisory,
@@ -304,6 +305,18 @@ export async function collectLegacyCronStoreHealthFindings(params: {
304305
}
305306
}
306307

308+
if (normalized.legacyGatewayExecJobs.length > 0) {
309+
findings.push(
310+
legacyCronStoreFinding({
311+
message: `${pluralize(normalized.legacyGatewayExecJobs.length, "automation")} require recreation because they grant the retired \`gateway_exec\` alias.`,
312+
path: sqliteStorePath,
313+
requirement: "legacy-gateway-exec-recreation",
314+
fixHint:
315+
"Review the affected jobs with `openclaw automations list --all`, then recreate each one from a fresh authenticated creator turn or explicitly reauthorize its complete tool cap from a trusted operator shell.",
316+
}),
317+
);
318+
}
319+
307320
const notifyCount = rawJobs.filter((job) => job.notify === true).length;
308321
if (notifyCount > 0) {
309322
findings.push(
@@ -515,6 +528,12 @@ export async function maybeRepairLegacyCronStore(params: {
515528
if (scheduledToolPolicyAdvisory) {
516529
note(scheduledToolPolicyAdvisory, "Cron");
517530
}
531+
const legacyGatewayExecAdvisory = formatLegacyGatewayExecAdvisory(
532+
normalized.legacyGatewayExecJobs,
533+
);
534+
if (legacyGatewayExecAdvisory) {
535+
note(legacyGatewayExecAdvisory, "Cron");
536+
}
518537
const staticMcpByAgentWorkspace = new Map<string, boolean>();
519538
const incompleteInheritedAuthorityAdvisory = formatIncompleteInheritedAuthorityAdvisory(
520539
rawJobs

‎src/commands/doctor/cron/repair-plan.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ export function formatScheduledToolPolicyAdvisory(params: {
8080
return lines.join("\n");
8181
}
8282

83+
/** Advisory for alias-only jobs whose original exec authority cannot be proven from storage. */
84+
export function formatLegacyGatewayExecAdvisory(names: string[]): string | null {
85+
if (names.length === 0) {
86+
return null;
87+
}
88+
return [
89+
`${pluralize(names.length, "automation")} ${names.length === 1 ? "grants" : "grant"} the retired \`gateway_exec\` alias${formatJobNameList(names)}.`,
90+
"- Doctor will not convert this alias to `exec` because the stored name does not prove its original producer or approval restrictions.",
91+
"- Recreate the automation from a fresh authenticated creator turn, or explicitly reauthorize its complete tool cap from a trusted operator shell.",
92+
].join("\n");
93+
}
94+
8395
/** Advisory for legacy default caps that were captured before configured MCP was final. */
8496
export function formatIncompleteInheritedAuthorityAdvisory(names: string[]): string | null {
8597
if (names.length === 0) {

‎src/commands/doctor/cron/scheduled-tool-policy-migration.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { describe, expect, it } from "vitest";
2-
import { formatScheduledToolPolicyAdvisory } from "./repair-plan.js";
2+
import {
3+
formatLegacyGatewayExecAdvisory,
4+
formatScheduledToolPolicyAdvisory,
5+
} from "./repair-plan.js";
36
import { normalizeStoredCronJobs } from "./store-migration.js";
47

58
function job(overrides: Record<string, unknown> = {}): Record<string, unknown> {
@@ -124,4 +127,19 @@ describe("migrateScheduledToolPolicy", () => {
124127
}),
125128
).toContain("openclaw cron edit <id> --tools");
126129
});
130+
131+
it("reports alias-only Gateway exec jobs without converting their authority", () => {
132+
const raw = job({
133+
payload: { kind: "agentTurn", message: "run", toolsAllow: ["read", "gateway_exec"] },
134+
scheduledToolPolicy: { version: 1, mode: "trusted" },
135+
});
136+
137+
const result = normalizeStoredCronJobs([raw]);
138+
139+
expect(result.legacyGatewayExecJobs).toEqual(["Legacy"]);
140+
expect((raw.payload as { toolsAllow: string[] }).toolsAllow).toEqual(["read", "gateway_exec"]);
141+
expect(formatLegacyGatewayExecAdvisory(result.legacyGatewayExecJobs)).toContain(
142+
"will not convert this alias",
143+
);
144+
});
127145
});

‎src/commands/doctor/cron/store-migration.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ type NormalizeCronStoreJobsResult = {
114114
unsupportedLegacyTriggerScriptJobs: string[];
115115
legacyScheduledToolPolicyJobs: string[];
116116
invalidScheduledToolPolicyJobs: string[];
117+
legacyGatewayExecJobs: string[];
117118
jobs: Array<Record<string, unknown>>;
118119
mutated: boolean;
119120
removedJobs: Array<{ job: Record<string, unknown>; reason: string; sourceIndex: number }>;
@@ -169,6 +170,7 @@ export function normalizeStoredCronJobs(
169170
const unresolvedAgentTurnShellToolPromptJobs: string[] = [];
170171
const legacyTriggerScriptJobs: string[] = [];
171172
const unsupportedLegacyTriggerScriptJobs: string[] = [];
173+
const legacyGatewayExecJobs: string[] = [];
172174
const scheduledToolPolicyMigrations = createScheduledToolPolicyMigrationCollector();
173175
const unresolvedAgentTurnPromptJobsByKind = {
174176
commandPromptWithoutShellAccess: unresolvedAgentTurnCommandPromptJobs,
@@ -354,6 +356,18 @@ export function normalizeStoredCronJobs(
354356
}
355357

356358
if (payloadRecord) {
359+
const hasLegacyGatewayExec =
360+
Array.isArray(payloadRecord.toolsAllow) &&
361+
payloadRecord.toolsAllow.some(
362+
(tool) =>
363+
typeof tool === "string" && normalizeOptionalLowercaseString(tool) === "gateway_exec",
364+
);
365+
if (hasLegacyGatewayExec) {
366+
const name = normalizeOptionalString(raw.name) ?? normalizeOptionalString(raw.id);
367+
if (name) {
368+
legacyGatewayExecJobs.push(name);
369+
}
370+
}
357371
const hadLegacyPayloadProvider = Boolean(normalizeOptionalString(payloadRecord.provider));
358372
const hadLegacyPayloadCodexModel = hasLegacyOpenAICodexCronModelRef(payloadRecord);
359373
const hadLegacyTaskSuggestionToolName = hasLegacyToolNameList(
@@ -635,6 +649,7 @@ export function normalizeStoredCronJobs(
635649
unsupportedLegacyTriggerScriptJobs,
636650
legacyScheduledToolPolicyJobs: scheduledToolPolicyMigrations.legacyJobs,
637651
invalidScheduledToolPolicyJobs: scheduledToolPolicyMigrations.invalidJobs,
652+
legacyGatewayExecJobs,
638653
jobs,
639654
mutated,
640655
removedJobs,

‎src/config/sessions/types.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
CronScheduledToolCallerOrigin,
1818
CronScheduledToolPolicy,
1919
CronToolsAllowExecTarget,
20+
CronToolsAllowExecTargetRequirement,
2021
} from "../../cron/scheduled-tool-policy.js";
2122
import type { ChannelRouteRef } from "../../plugin-sdk/channel-route.js";
2223
import type { SessionBoardFace } from "../../shared/session-types.js";
@@ -460,6 +461,8 @@ type SessionEntryCore = SessionRestartRecoveryState &
460461
scheduledToolPolicy?: CronScheduledToolPolicy;
461462
/** Restrict-only exec pin copied from the owning cron job's cap. */
462463
toolsAllowExecTarget?: CronToolsAllowExecTarget;
464+
/** Expected pin copied with the cap so detached continuation loss fails closed. */
465+
toolsAllowExecTargetRequirement?: CronToolsAllowExecTargetRequirement;
463466
/** Store-private origin paired with an account scheduled-tool policy. */
464467
scheduledToolCallerOrigin?: CronScheduledToolCallerOrigin;
465468
cliSessionBindingFacts?: {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ export async function prepareCronRunContext(params: {
625625
}),
626626
scheduledToolCallerOrigin: input.job.toolsAllowProvenance?.callerOrigin,
627627
toolsAllowExecTarget: input.job.toolsAllowExecTarget,
628+
toolsAllowExecTargetRequirement: input.job.toolsAllowExecTargetRequirement,
628629
cliSessionBindingFacts: {
629630
sourceReplyDeliveryMode: sourceDelivery.sourceReplyDeliveryMode,
630631
requireExplicitMessageTarget: sourceDelivery.messageTool.requireExplicitTarget,

‎src/cron/isolated-agent/run-session-state.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe("createPersistCronSessionEntry", () => {
270270
runSessionKey,
271271
createdActor: { type: "human", id: "profile-ada" },
272272
thinkingLevel: "high",
273-
toolsAllow: ["image_generate", "write"],
273+
toolsAllow: ["image_generate", "exec", "write"],
274274
toolsAllowIsDefault: true,
275275
scheduledToolPolicy: {
276276
version: 1,
@@ -280,6 +280,11 @@ describe("createPersistCronSessionEntry", () => {
280280
},
281281
scheduledToolCallerOrigin: { kind: "local" },
282282
toolsAllowExecTarget: { version: 1, host: "gateway", ask: "always" },
283+
toolsAllowExecTargetRequirement: {
284+
version: 1,
285+
target: { version: 1, host: "gateway", ask: "always" },
286+
grantIndex: 1,
287+
},
283288
persistSessionEntry,
284289
});
285290

@@ -297,6 +302,11 @@ describe("createPersistCronSessionEntry", () => {
297302
host: "gateway",
298303
ask: "always",
299304
});
305+
expect(store[runSessionKey]?.cronRunContinuation?.toolsAllowExecTargetRequirement).toEqual({
306+
version: 1,
307+
target: { version: 1, host: "gateway", ask: "always" },
308+
grantIndex: 1,
309+
});
300310
expect(store[runSessionKey]?.cronRunContinuation?.scheduledToolPolicy).toEqual({
301311
version: 1,
302312
mode: "account",

‎src/cron/isolated-agent/run-session-state.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ import {
1919
normalizeCronScheduledToolCallerOrigin,
2020
normalizeCronScheduledToolPolicy,
2121
normalizeCronToolsAllowExecTarget,
22+
normalizeCronToolsAllowExecTargetRequirement,
23+
stripCronPinnedExecGrant,
2224
} from "../scheduled-tool-policy.js";
2325
import type {
2426
CronScheduledToolCallerOrigin,
2527
CronScheduledToolPolicy,
2628
CronToolsAllowExecTarget,
29+
CronToolsAllowExecTargetRequirement,
2730
} from "../scheduled-tool-policy.js";
2831
import { setSessionRuntimeModel } from "./run.runtime.js";
2932
import type { resolveCronSession } from "./session.js";
@@ -255,6 +258,7 @@ export function createCronRunContinuationSession(params: {
255258
scheduledToolPolicy?: CronScheduledToolPolicy;
256259
scheduledToolCallerOrigin?: CronScheduledToolCallerOrigin;
257260
toolsAllowExecTarget?: CronToolsAllowExecTarget;
261+
toolsAllowExecTargetRequirement?: CronToolsAllowExecTargetRequirement;
258262
cliSessionBindingFacts?: {
259263
extraSystemPromptStatic?: string;
260264
sourceReplyDeliveryMode?: "automatic" | "message_tool_only";
@@ -273,14 +277,23 @@ export function createCronRunContinuationSession(params: {
273277
params.toolsAllow === undefined
274278
? undefined
275279
: normalizeCronToolsAllowExecTarget(params.toolsAllowExecTarget);
280+
const toolsAllowExecTargetRequirement =
281+
params.toolsAllow === undefined
282+
? undefined
283+
: normalizeCronToolsAllowExecTargetRequirement(params.toolsAllowExecTargetRequirement);
284+
const storedToolsAllow = stripCronPinnedExecGrant({
285+
toolsAllow: params.toolsAllow,
286+
requirement: toolsAllowExecTargetRequirement,
287+
});
276288
const continuation: NonNullable<SessionEntry["cronRunContinuation"]> = {
277289
lifecycleRevision: params.cronSession.lifecycleRevision,
278290
phase: "running" as const,
279-
...(params.toolsAllow !== undefined ? { toolsAllow: [...params.toolsAllow] } : {}),
291+
...(storedToolsAllow !== undefined ? { toolsAllow: storedToolsAllow } : {}),
280292
...(params.toolsAllowIsDefault === true ? { toolsAllowIsDefault: true } : {}),
281293
...(scheduledToolPolicy ? { scheduledToolPolicy } : {}),
282294
...(scheduledToolPolicy?.mode === "account" ? { scheduledToolCallerOrigin } : {}),
283295
...(toolsAllowExecTarget ? { toolsAllowExecTarget } : {}),
296+
...(toolsAllowExecTargetRequirement ? { toolsAllowExecTargetRequirement } : {}),
284297
...(params.cliSessionBindingFacts
285298
? { cliSessionBindingFacts: { ...params.cliSessionBindingFacts } }
286299
: {}),

‎src/cron/normalize.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
normalizeCronScheduledToolCallerOrigin,
2020
normalizeCronScheduledToolPolicy,
2121
normalizeCronToolsAllowExecTarget,
22+
normalizeCronToolsAllowExecTargetRequirement,
2223
} from "./scheduled-tool-policy.js";
2324
import { inferCronJobName } from "./service/normalize.js";
2425
import {
@@ -426,6 +427,17 @@ export function normalizeCronJobInput(
426427
}
427428
}
428429

430+
if ("toolsAllowExecTargetRequirement" in base) {
431+
const requirement = normalizeCronToolsAllowExecTargetRequirement(
432+
base.toolsAllowExecTargetRequirement,
433+
);
434+
if (requirement) {
435+
next.toolsAllowExecTargetRequirement = requirement;
436+
} else {
437+
delete next.toolsAllowExecTargetRequirement;
438+
}
439+
}
440+
429441
if ("runtimeAuthority" in base) {
430442
const runtimeAuthority = normalizeCronRuntimeAuthority(base.runtimeAuthority);
431443
if (runtimeAuthority) {

0 commit comments

Comments
 (0)