|
| 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