Skip to content

Commit 6a8278d

Browse files
authored
feat(cron): schedule the skill collection review as a system-owned job (#130030)
* feat(skills): expose the collection review as a single cron-driven run The weekly Skill Workshop collection review lived in a bespoke gateway maintenance loop with its own interval, due-state check, and gateway admission wrapper. Replace it with `runSkillCollectionReviewForAgent`, one run that keeps the workspace claim, identity check, and status recording and reports `{ status, summary }` to its caller. The cron timer that will own the schedule already runs under gateway root work admission, so the extra wrapper and its drain test go away. * feat(cron): schedule the skill collection review as a system-owned job Fold the weekly Skill Workshop review into cron the same way the heartbeat monitor is: a `skillCollectionReview` payload kind, one system-owned job per workspace (first agent of each canonical workspace) reconciled at gateway startup, enabled only when the workshop runs in `auto` mode, on a 7-day `every` schedule with the heartbeat phase spread. The timer invokes the review runner directly; cron clients cannot create, patch, or remove system-owned payloads, and the job shows up in `openclaw cron list`. * fix(cron): warn when skill reviews are disabled * fix(cron): preserve skill review failure details * fix(cron): narrow serialized payload variants * test(cron): tighten skill review coverage * fix(gateway): reconcile skill reviews on config reload * fix(cron): fence revoked skill reviews * fix(cron): satisfy system payload type coverage * fix(ui): protect system-owned cron jobs * test(gateway): prove review revocation fence * fix(gateway): revoke skill reviews before config publish
1 parent 2e017ba commit 6a8278d

57 files changed

Lines changed: 1318 additions & 530 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/automation/cron-jobs.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ Every job carries exactly one payload kind, chosen by flag:
184184
| Command | `--command <shell>` or `--command-argv <json>` | A shell/process on the Gateway host, no model call |
185185
| Script | `--script <file\|->` | A headless code-mode script using the owning agent's tools |
186186

187-
One additional payload kind, `heartbeat`, is system-owned: the gateway converges one heartbeat monitor job per heartbeat-enabled agent (see [Heartbeat](/gateway/heartbeat)). It appears in `automations list --all` but cannot be created or edited through the CLI or API. Heartbeat config is written through to the persisted monitor schedule at startup, on config reload, or by `openclaw doctor --fix`. When automations are disabled, the monitor does not tick and no fallback heartbeat timer runs.
187+
System-owned payload kinds are gateway-converged and cannot be created or edited through the CLI or API. The `heartbeat` kind creates one heartbeat monitor job per heartbeat-enabled agent (see [Heartbeat](/gateway/heartbeat)). The `skillCollectionReview` kind creates one Skill Workshop review job per writable workspace. Both appear in `openclaw cron list`; use `--all` to include disabled rows.
188+
189+
Skill collection review runs every 7 days. It is enabled when `skills.workshop.autonomous.mode` is `auto`; `propose` and `off` keep the system-owned job disabled. The Gateway converges these jobs at startup and after config reload. Scheduled reviews require automations. When `cron.enabled` is `false` or `OPENCLAW_SKIP_CRON=1`, the Gateway logs a startup warning and does not run scheduled reviews. There is no separate weekly Gateway timer.
188190

189191
### Agent-turn options
190192

‎docs/tools/skill-workshop.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ Only a `pending` proposal can be revised, applied, rejected, or quarantined.
5757

5858
## Collection review
5959

60-
In `auto` mode, the Gateway starts one isolated collection-review session per
61-
agent workspace each week. The session can only read skills and submit
62-
one atomic collection reconciliation listing only changes. It keeps distinct useful skills,
60+
In `auto` mode, the Gateway runs one system-owned cron job per writable
61+
workspace each week. The job appears in `openclaw cron list` and runs every
62+
7 days. Cron owns the cadence; the job is enabled only when
63+
`skills.workshop.autonomous.mode` is `auto`. The review can only read skills
64+
and submit one atomic collection reconciliation listing only changes. It keeps distinct useful skills,
6365
rewrites weak ones, consolidates overlap, and drops junk or stale fragments.
6466
Choosing `auto` intentionally authorizes those rewrites and drops without a
6567
second approval **for Workshop-owned paths only**; `propose` and `off` do not
@@ -93,8 +95,7 @@ To undo the last completed cleanup, ask the agent to restore the skill
9395
collection. It uses `skill_workshop` action `restore_collection` under the same
9496
workspace lock. Restore refuses if any affected skill changed after cleanup.
9597

96-
The weekly attempt is persisted per workspace before the model starts. Gateway
97-
restarts do not repeat a failed or successful attempt within 7 days. Review is admitted only for collections of at most
98+
Each attempt is persisted per workspace before the model starts. Review is admitted only for collections of at most
9899
200 skills and 240,000 total `SKILL.md` bytes. Larger collections stay unchanged.
99100
The reconciled result must stay inside the same byte limit.
100101

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ const CronPayloadSchema = Type.Union([
316316
]);
317317

318318
/**
319-
* Reported payloads add the system-owned heartbeat monitor kind; it is
319+
* Reported payloads add system-owned monitor kinds; they are
320320
* gateway-converged only, so create/patch schemas intentionally omit it.
321321
*/
322322
const CronReportedPayloadSchema = Type.Union([
@@ -325,6 +325,7 @@ const CronReportedPayloadSchema = Type.Union([
325325
CronCommandPayloadSchema,
326326
CronScriptPayloadSchema,
327327
closedObject({ kind: Type.Literal("heartbeat") }),
328+
closedObject({ kind: Type.Literal("skillCollectionReview") }),
328329
]);
329330

330331
/** Partial cron payload for job updates. */

‎src/agents/tools/skill-workshop-tool-collection.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export async function executeSkillCollectionReconcile(params: {
112112
agentIds: params.context?.agentIds,
113113
approvedSkillNamesByAgent: params.context?.approvedSkillNamesByAgent,
114114
env: params.env,
115+
...(params.context?.assertCurrent ? { assertCurrent: params.context.assertCurrent } : {}),
115116
});
116117
if (params.context) {
117118
params.context.result = result;

‎src/cli/cron-cli/register.cron-edit-options.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseStrictPositiveInteger } from "@openclaw/normalization-core/number-coercion";
22
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
33
import type { CronJob } from "../../cron/types.js";
4+
import { isSystemOwnedCronPayloadKind } from "../../cron/types.js";
45
import {
56
parseCronCommandArgv,
67
parseCronCommandEnv,
@@ -156,7 +157,7 @@ export async function resolveCronEditPayloadDeliveryPatch(
156157
if (existingKind === "script") {
157158
throw new Error("Use --script-timeout-seconds for script jobs, not --timeout-seconds.");
158159
}
159-
if (existingKind === "systemEvent" || existingKind === "heartbeat") {
160+
if (existingKind === "systemEvent" || isSystemOwnedCronPayloadKind(existingKind)) {
160161
throw new Error(`--timeout-seconds is not supported for ${existingKind} jobs.`);
161162
}
162163
timeoutOnlyPayloadKind = existingKind;

‎src/cron/active-jobs.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Tracks in-process cron executions so schedulers and wake paths avoid duplicate runs. */
22
import { resolveGlobalSingleton } from "../shared/global-singleton.js";
3+
import type { CronPayload } from "./types.js";
34

45
type CronActiveJobState = {
56
activeJobs: Map<string, CronActiveJobMarker>;
@@ -12,6 +13,7 @@ const CRON_ACTIVE_JOB_STATE_KEY = Symbol.for("openclaw.cron.activeJobs");
1213

1314
export type CronActiveJobMarker = {
1415
jobId: string;
16+
payloadKind?: CronPayload["kind"];
1517
generation: number;
1618
token: number;
1719
cancellation?:
@@ -88,7 +90,7 @@ function notifyCronJobInactive(marker: CronActiveJobMarker) {
8890
/** Marks a cron job id as currently executing for duplicate-run suppression. */
8991
export function markCronJobActive(
9092
jobId: string,
91-
opts?: { preserveAcrossGenerationAdvance?: boolean },
93+
opts?: { payloadKind?: CronPayload["kind"]; preserveAcrossGenerationAdvance?: boolean },
9294
): CronActiveJobMarker | undefined {
9395
if (!jobId) {
9496
return undefined;
@@ -98,6 +100,7 @@ export function markCronJobActive(
98100
state.nextToken += 1;
99101
const marker: CronActiveJobMarker = {
100102
jobId,
103+
...(opts?.payloadKind ? { payloadKind: opts.payloadKind } : {}),
101104
generation: state.generation,
102105
token,
103106
...(opts?.preserveAcrossGenerationAdvance ? { preserveAcrossGenerationAdvance: true } : {}),
@@ -178,6 +181,23 @@ export function requestActiveCronJobCancellation(jobId: string, reason: string):
178181
}
179182
}
180183

184+
/** Revokes every active run admitted from one payload family. */
185+
export function requestActiveCronJobCancellationByPayloadKind(
186+
payloadKind: CronPayload["kind"],
187+
reason: string,
188+
): void {
189+
const state = getCronActiveJobState();
190+
for (const marker of state.activeJobs.values()) {
191+
if (
192+
marker.payloadKind !== payloadKind ||
193+
!isMarkerActiveInGeneration(marker, state.generation)
194+
) {
195+
continue;
196+
}
197+
requestCronActiveJobMarkerCancellation(marker, reason);
198+
}
199+
}
200+
181201
/** Returns whether the given cron job id is currently executing in this process. */
182202
export function isCronJobActive(jobId: string) {
183203
if (!jobId) {

‎src/cron/cron-protocol-schema.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { Value } from "typebox/value";
12
// Cron protocol schema tests cover runtime validation for cron protocol payloads.
23
import { describe, expect, it } from "vitest";
34
import {
5+
CronAddParamsSchema,
46
CronJobStateSchema,
57
CronPacingSchema,
68
CronUpdateParamsSchema,
@@ -55,4 +57,23 @@ describe("cron protocol schema", () => {
5557
"at least one of min or max is required",
5658
);
5759
});
60+
61+
it("keeps system-owned payloads out of client create and patch schemas", () => {
62+
const payload = { kind: "skillCollectionReview" };
63+
expect(
64+
Value.Check(CronAddParamsSchema, {
65+
name: "skill review",
66+
schedule: { kind: "every", everyMs: 604_800_000 },
67+
sessionTarget: "main",
68+
wakeMode: "next-heartbeat",
69+
payload,
70+
}),
71+
).toBe(false);
72+
expect(
73+
Value.Check(CronUpdateParamsSchema, {
74+
id: "job-1",
75+
patch: { payload },
76+
}),
77+
).toBe(false);
78+
});
5879
});

‎src/cron/heartbeat-task.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import { createHash } from "node:crypto";
33
import type { CronJob } from "./types.js";
44

5-
const HEARTBEAT_TASK_DECLARATION_PREFIX = "heartbeat-task:";
5+
export const HEARTBEAT_TASK_DECLARATION_PREFIX = "heartbeat-task:";
66

77
/** Whether a declaration key belongs to the doctor-owned heartbeat-task namespace. */
8-
export function isHeartbeatTaskDeclarationKey(declarationKey: string | undefined): boolean {
8+
function isHeartbeatTaskDeclarationKey(declarationKey: string | undefined): boolean {
99
return declarationKey?.startsWith(HEARTBEAT_TASK_DECLARATION_PREFIX) === true;
1010
}
1111

‎src/cron/normalize-payload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export function normalizeCronPayload(payload: UnknownRecord): UnknownRecord {
7676
next.kind = "command";
7777
} else if (kindRaw === "script") {
7878
next.kind = "script";
79+
} else if (kindRaw === "skillcollectionreview") {
80+
next.kind = "skillCollectionReview";
7981
} else if (kindRaw) {
8082
next.kind = kindRaw;
8183
}

‎src/cron/normalize.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from "./session-target.js";
2727
import { normalizeCronStaggerMs, resolveDefaultCronStaggerMs } from "./stagger.js";
2828
import { normalizeCronStreamBatching } from "./stream-schedule.js";
29-
import type { CronJobCreate, CronJobPatch } from "./types.js";
29+
import { isSystemOwnedCronPayloadKind, type CronJobCreate, type CronJobPatch } from "./types.js";
3030

3131
type UnknownRecord = Record<string, unknown>;
3232

@@ -539,7 +539,7 @@ export function normalizeCronJobInput(
539539
// Agent turns bind to the creating conversation by default: the run carries
540540
// that chat's context and announces its result there. Callers without session
541541
// context are downgraded to isolated by resolveCronCurrentSessionTarget.
542-
if (kind === "systemEvent" || kind === "heartbeat") {
542+
if (kind === "systemEvent" || isSystemOwnedCronPayloadKind(kind)) {
543543
next.sessionTarget = "main";
544544
} else if (kind === "agentTurn") {
545545
next.sessionTarget = "current";

0 commit comments

Comments
 (0)