Skip to content

Commit 7d159fd

Browse files
Apply consented grouped Claw updates (#102982)
* Apply consented grouped Claw updates * test(claws): cover update application * docs(claws): document consented updates * fix(claws): preserve uncertain update state * fix(claws): preserve inherited lifecycle guards * fix(claws): derive update package contracts * fix(claws): type update package lookup * test(claws): complete update capability fixtures * refactor(claws): share cron payload builder for updates * refactor(claws): extract update plan summary * fix(claws): activate cron after update dependencies * fix(claws): preserve state after uncertain cron update * fix(claws): persist partial update provenance * fix(claws): preserve update ownership metadata * test(claws): track update apply temp dirs * test(claws): align update cron mock result * fix(claws): update apply writes agent entries --------- Co-authored-by: Patrick Erichsen <[email protected]>
1 parent 1a7a180 commit 7d159fd

31 files changed

Lines changed: 4274 additions & 245 deletions

‎docs/cli/claws.md‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ This is not a reference count. Ordinary plugin, skill, and agent commands keep
197197
their existing behavior; Claws add provenance and guarded lifecycle operations
198198
on top.
199199

200-
## Preview an update
200+
## Update an installed Claw
201201

202202
By default, update uses the source recorded when the Claw was added. Use
203203
`--from` when that source moved or when testing another package directory:
@@ -218,8 +218,24 @@ warning are included. Removing a package declaration releases this Claw's edge
218218
without uninstalling the artifact during update. The eventual
219219
exact `planIntegrity` confirmation binds that disclosed set as well as ordinary
220220
content changes. Hosts may use the same records for a separate dialog or an
221-
aggregate multi-agent review. This stage is read-only: `claws update` requires
222-
`--dry-run` and does not apply the plan.
221+
aggregate multi-agent review. Apply the exact reviewed plan with explicit
222+
consent:
223+
224+
```bash
225+
openclaw claws update incident-triage \
226+
--yes \
227+
--plan-integrity <SHA256_FROM_DRY_RUN>
228+
```
229+
230+
OpenClaw rebuilds the plan and compare-and-swaps owned state before each
231+
mutation. Removed package declarations release dependency edges without
232+
uninstalling artifacts. Cron changes reread the live scheduler definition and
233+
stop on operator drift. Package installers, source-config writers, and the Gateway scheduler
234+
are not one transaction. If compensation cannot be proven after an external
235+
mutation, OpenClaw reports error code `update_partial` with structured
236+
`status: partial`, preserves uncertain provenance,
237+
and stops. Inspect `claws status`, the affected resource, and `openclaw doctor`;
238+
then preview again before retrying or removing anything.
223239

224240
## Remove an installed Claw
225241

@@ -273,7 +289,7 @@ agents, credentials, sessions, and unowned local state are excluded.
273289
| `claws inspect <source>` | Validate a package directory or JSON manifest. |
274290
| `claws add <source>` | Preview or create one new agent and workspace. |
275291
| `claws status [claw-or-agent]` | Report installed state, ownership, and drift. |
276-
| `claws update <claw-or-agent>` | Preview changes from the recorded or given source. |
292+
| `claws update <claw-or-agent>` | Preview or apply changes from the selected source. |
277293
| `claws remove <claw-or-agent>` | Preview or remove the agent and eligible resources. |
278294
| `claws export <agent> --out <path>` | Create a portable package from an installed agent. |
279295

‎docs/docs_map.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ Do not edit it by hand; run `pnpm docs:map:gen`.
13751375
- H2: Create a grouped manifest
13761376
- H2: Inspect and preview
13771377
- H2: Inspect installed state
1378-
- H2: Preview an update
1378+
- H2: Update an installed Claw
13791379
- H2: Remove an installed Claw
13801380
- H2: Export an installed agent
13811381
- H2: Command reference

‎src/claws/cron-update.test.ts‎

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { normalizeCronJobCreate } from "../cron/normalize.js";
3+
import { applyClawCronUpdate } from "./cron-update.js";
4+
import {
5+
CLAW_CRON_REF_SCHEMA_VERSION,
6+
clawCronGatewayInput,
7+
type PersistedClawCronRef,
8+
} from "./cron.js";
9+
import { CLAW_OUTPUT_STABILITY, type ClawCronJob, type ClawManifest } from "./types.js";
10+
import { CLAW_UPDATE_PLAN_SCHEMA_VERSION, type ClawUpdatePlan } from "./update-plan.js";
11+
12+
const oldDaily: ClawCronJob = {
13+
id: "daily",
14+
schedule: { cron: "0 9 * * *", timezone: "UTC" },
15+
session: "main",
16+
message: "Old daily",
17+
};
18+
const newDaily: ClawCronJob = { ...oldDaily, message: "New daily" };
19+
const legacy: ClawCronJob = {
20+
id: "legacy",
21+
schedule: { cron: "0 8 * * *", timezone: "UTC" },
22+
session: "isolated",
23+
message: "Legacy",
24+
};
25+
const weekly: ClawCronJob = {
26+
id: "weekly",
27+
schedule: { cron: "0 9 * * 1", timezone: "UTC" },
28+
session: "main",
29+
message: "Weekly",
30+
};
31+
32+
function ref(job: ClawCronJob, schedulerJobId: string): PersistedClawCronRef {
33+
return {
34+
schemaVersion: CLAW_CRON_REF_SCHEMA_VERSION,
35+
agentId: "worker",
36+
manifestId: job.id,
37+
declarationKey: `claw:worker:${job.id}`,
38+
schedulerJobId,
39+
status: "complete",
40+
job,
41+
createdAtMs: 10,
42+
updatedAtMs: 10,
43+
};
44+
}
45+
46+
function cronReadView(agentId: string, value: PersistedClawCronRef) {
47+
const normalized = normalizeCronJobCreate(clawCronGatewayInput(agentId, value));
48+
if (!normalized || !value.schedulerJobId) {
49+
throw new Error("expected complete cron provenance");
50+
}
51+
return {
52+
...normalized,
53+
id: value.schedulerJobId,
54+
createdAtMs: 1,
55+
updatedAtMs: 1,
56+
state: {},
57+
};
58+
}
59+
60+
function plan(actions: ClawUpdatePlan["actions"]): ClawUpdatePlan {
61+
return {
62+
schemaVersion: CLAW_UPDATE_PLAN_SCHEMA_VERSION,
63+
stability: CLAW_OUTPUT_STABILITY,
64+
dryRun: true,
65+
mutationAllowed: false,
66+
planIntegrity: "sha256:update-plan",
67+
found: true,
68+
agentId: "worker",
69+
currentClaw: { name: "@acme/worker", version: "1.0.0", integrity: "sha256:old" },
70+
targetClaw: { name: "@acme/worker", version: "2.0.0", integrity: "sha256:new" },
71+
summary: {
72+
totalActions: actions.length,
73+
added: actions.filter((action) => action.action === "add").length,
74+
changed: actions.filter((action) => action.action === "change").length,
75+
removed: actions.filter((action) => action.action === "remove").length,
76+
released: actions.filter((action) => action.action === "release").length,
77+
unchanged: 0,
78+
manual: 0,
79+
blocked: 0,
80+
capabilityChanges: 0,
81+
capabilityEscalations: 0,
82+
},
83+
actions,
84+
capabilityChanges: [],
85+
blockers: [],
86+
diagnostics: [],
87+
};
88+
}
89+
90+
function manifest(): ClawManifest {
91+
return {
92+
schemaVersion: 1,
93+
agent: { id: "worker" },
94+
workspace: { bootstrapFiles: {}, files: [] },
95+
packages: [],
96+
mcpServers: {},
97+
cronJobs: [newDaily, weekly],
98+
};
99+
}
100+
101+
describe("applyClawCronUpdate", () => {
102+
it("converges changes and reverses add, change, and remove operations", async () => {
103+
const add = vi.fn(async (input: Record<string, unknown>) => {
104+
const key = input.declarationKey;
105+
if (key === "claw:worker:daily") {
106+
return { id: "scheduler-daily" };
107+
}
108+
if (key === "claw:worker:legacy") {
109+
return { id: "scheduler-legacy-restored" };
110+
}
111+
return { id: "scheduler-weekly" };
112+
});
113+
const remove = vi.fn(async () => ({ ok: true }));
114+
const upsertRef = vi.fn();
115+
const deleteRef = vi.fn();
116+
const refs = [ref(oldDaily, "scheduler-daily"), ref(legacy, "scheduler-legacy")];
117+
const execution = await applyClawCronUpdate(
118+
plan([
119+
{
120+
kind: "cronJob",
121+
id: "daily",
122+
action: "change",
123+
target: "scheduler-daily",
124+
blocked: false,
125+
reason: "changed",
126+
},
127+
{
128+
kind: "cronJob",
129+
id: "weekly",
130+
action: "add",
131+
target: "claw:worker:weekly",
132+
blocked: false,
133+
reason: "added",
134+
},
135+
{
136+
kind: "cronJob",
137+
id: "legacy",
138+
action: "remove",
139+
target: "scheduler-legacy",
140+
blocked: false,
141+
reason: "removed",
142+
},
143+
]),
144+
manifest(),
145+
{
146+
cronGateway: {
147+
add,
148+
get: async (id) =>
149+
cronReadView("worker", refs.find((item) => item.schedulerJobId === id)!),
150+
remove,
151+
},
152+
readRefs: () => refs,
153+
upsertRef,
154+
deleteRef,
155+
nowMs: 20,
156+
},
157+
);
158+
159+
expect(execution.appliedIds).toEqual(["daily", "weekly", "legacy"]);
160+
expect(remove).toHaveBeenCalledWith("scheduler-legacy");
161+
expect(upsertRef).toHaveBeenCalledTimes(5);
162+
expect(deleteRef).toHaveBeenCalledTimes(1);
163+
164+
await execution.rollback();
165+
166+
expect(remove).toHaveBeenCalledWith("scheduler-weekly");
167+
expect(add).toHaveBeenCalledTimes(4);
168+
expect(upsertRef).toHaveBeenCalledTimes(7);
169+
expect(deleteRef).toHaveBeenCalledTimes(2);
170+
});
171+
172+
it("removes a non-converged replacement and fails closed", async () => {
173+
const remove = vi.fn(async () => ({ ok: true }));
174+
await expect(
175+
applyClawCronUpdate(
176+
plan([
177+
{
178+
kind: "cronJob",
179+
id: "daily",
180+
action: "change",
181+
target: "scheduler-daily",
182+
blocked: false,
183+
reason: "changed",
184+
},
185+
]),
186+
manifest(),
187+
{
188+
cronGateway: {
189+
add: async () => ({ id: "unexpected-copy" }),
190+
get: async () => cronReadView("worker", ref(oldDaily, "scheduler-daily")),
191+
remove,
192+
},
193+
readRefs: () => [ref(oldDaily, "scheduler-daily")],
194+
upsertRef: vi.fn(),
195+
},
196+
),
197+
).rejects.toThrow("did not converge");
198+
expect(remove).toHaveBeenCalledWith("unexpected-copy");
199+
});
200+
201+
it("marks a thrown gateway mutation as uncertain", async () => {
202+
await expect(
203+
applyClawCronUpdate(
204+
plan([
205+
{
206+
kind: "cronJob",
207+
id: "weekly",
208+
action: "add",
209+
target: "claw:worker:weekly",
210+
blocked: false,
211+
reason: "added",
212+
},
213+
]),
214+
manifest(),
215+
{
216+
cronGateway: {
217+
add: async () => {
218+
throw new Error("connection lost");
219+
},
220+
get: vi.fn(),
221+
remove: vi.fn(),
222+
},
223+
readRefs: () => [],
224+
upsertRef: vi.fn(),
225+
},
226+
),
227+
).rejects.toMatchObject({ partial: true });
228+
});
229+
230+
it("rejects a live cron definition changed after planning", async () => {
231+
const remove = vi.fn();
232+
await expect(
233+
applyClawCronUpdate(
234+
plan([
235+
{
236+
kind: "cronJob",
237+
id: "legacy",
238+
action: "remove",
239+
target: "scheduler-legacy",
240+
blocked: false,
241+
reason: "removed",
242+
},
243+
]),
244+
manifest(),
245+
{
246+
cronGateway: {
247+
add: vi.fn(),
248+
get: async () => ({
249+
...cronReadView("worker", ref(legacy, "scheduler-legacy")),
250+
payload: { kind: "agentTurn", message: "Operator edit" },
251+
}),
252+
remove,
253+
},
254+
readRefs: () => [ref(legacy, "scheduler-legacy")],
255+
},
256+
),
257+
).rejects.toThrow("changed after planning");
258+
expect(remove).not.toHaveBeenCalled();
259+
});
260+
});

0 commit comments

Comments
 (0)