From 3e32a8199cdbe1e7238b8f8dcaf221dc1ec16c05 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 11:26:30 +0200 Subject: [PATCH 1/3] =?UTF-8?q?test:=20reproduce=20#659=20=E2=80=94=20one?= =?UTF-8?q?=20device=20whose=20platform=20has=20no=20driver=20aborts=20the?= =?UTF-8?q?=20whole=20cleanup=20run?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/reaper.test.ts | 92 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/core/reaper.test.ts b/src/core/reaper.test.ts index 6e527211d..e52f832a2 100644 --- a/src/core/reaper.test.ts +++ b/src/core/reaper.test.ts @@ -558,6 +558,98 @@ describe("CleanupReaper", () => { harness.reaper.dispose(); }); + it("does not abort its run on a device whose platform has no driver, and still cleans up every device proposed after it", async () => { + const harness = await createHarness(automaticCleanupRules); + // why: the driverless records are registered first so the proposals that must throw precede + // the healthy ones -- that order is what a whole-run abort turns on. Only an ios driver is + // registered, so acting on an android record throws NoDriverError, as it does for a platform + // discovery refused. The ready one exercises the shutdown path, the shut-down one the + // destroy path; a healthy device follows each. + const ghostReady = await harness.registry.registerDevice({ + driverData: {}, + driverDeviceId: "ghost-ready-android", + provisionDuration: 0, + spec: { model: "Pixel 8", osVersion: "35", physical: false, platform: "android" }, + }); + await harness.registry.transitionDevice(ghostReady.id, "ready", { + event: "device.ready", + payload: { bootDuration: 0, deviceId: ghostReady.id }, + }); + const ghostReadyLease = await harness.registry.createLease({ + deviceId: ghostReady.id, + ownerId: "agent-1", + requesterId: "agent-1", + ttlMs: 60_000, + ttlDeadline: 2_000, + }); + await harness.registry.beginRelease(ghostReadyLease.id); + await harness.registry.transitionDevice(ghostReady.id, "ready", { + event: "device.reclaimed", + payload: { deviceId: ghostReady.id, duration: 0, strategy: "erase" }, + }); + const idleReady = await seedReleased(harness, "ready"); + const ghostShutdown = await harness.registry.registerDevice({ + driverData: {}, + driverDeviceId: "ghost-shutdown-android", + provisionDuration: 0, + spec: { model: "Pixel 8", osVersion: "35", physical: false, platform: "android" }, + }); + await harness.registry.transitionDevice(ghostShutdown.id, "ready", { + event: "device.ready", + payload: { bootDuration: 0, deviceId: ghostShutdown.id }, + }); + await harness.registry.transitionDevice(ghostShutdown.id, "shutdown", { + event: "device.shutdown", + payload: { deviceId: ghostShutdown.id, initiator: "test" }, + }); + const idleShutdown = await seedReleased(harness, "shutdown"); + // why: one second past T2, so every destroy proposal fires and every shutdown one already has. + harness.clock.advance(31_000); + + // why: loose on purpose -- whether the run still reports the driverless devices' proposals is + // for the fix to decide; skipping the platform or surviving the failed action both answer it. + await expect(harness.reaper.run()).resolves.toEqual( + expect.arrayContaining([ + expect.objectContaining({ + action: "shutdown", + rule: "idle-shutdown", + target: idleReady.id, + }), + expect.objectContaining({ + action: "destroy", + rule: "idle-destroy", + target: idleShutdown.id, + }), + ]), + ); + + const stateOf = (deviceId: string) => + harness.registry.snapshot.devices.find((device) => device.id === deviceId)?.state; + expect([ + stateOf(ghostReady.id), + stateOf(idleReady.id), + stateOf(ghostShutdown.id), + stateOf(idleShutdown.id), + ]).toEqual(["ready", "shutdown", "shutdown", "deleted"]); + expect( + harness.driver.calls + .filter((call) => call.operation === "shutdown" || call.operation === "destroy") + .map((call) => `${call.operation} ${(call.arguments[0] as { deviceId: string }).deviceId}`) + .sort(), + ).toEqual( + [`destroy ${idleShutdown.driverDeviceId}`, `shutdown ${idleReady.driverDeviceId}`].sort(), + ); + expect( + harness.eventBus + .replay() + .filter((entry) => entry.event === "cleanup.executed") + .map((entry) => (entry.payload as { target: string }).target) + .sort(), + ).toEqual([idleReady.id, idleShutdown.id].sort()); + + harness.reaper.dispose(); + }); + it("prevents leasing from granting a device while cleanup is shutting it down", async () => { const rule: CleanupRule = { evaluate: () => [ From 2b7ed2b18821ce0900a2d420ad78dc5473bcb0eb Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 11:47:35 +0200 Subject: [PATCH 2/3] fix: contain a throwing cleanup action so the run continues (#659) 1 failing -> 0 failing --- src/core/AGENTS.md | 5 +++-- src/core/reaper.test.ts | 35 ++++++++++++++++++++++++++++++++++- src/core/reaper.ts | 15 ++++++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/core/AGENTS.md b/src/core/AGENTS.md index 4f9b75522..bc6a4af75 100644 --- a/src/core/AGENTS.md +++ b/src/core/AGENTS.md @@ -91,8 +91,9 @@ Events: → `absent` `device.absence-detected`; `absent` → `reclaiming` `devic physical one. Physical not present: wait `health.probeIntervalMs`, not counted. 5. **Cleanup** — `CleanupReaper` on `lease.released`, `daemon.started`, `disk.pressure-detected`, a tick. `idle-shutdown`: leased once, `ready`, idle past T1, not warm-targeted. `idle-destroy`: - `shutdown`, idle (lease end, else `shutdownAt`) past T2, or T1 under disk pressure. - `CleanupExecutor` revalidates, acts under `cleanup`, emits `cleanup.executed`. + `shutdown`, idle (lease end, else `shutdownAt`) past T2, or T1 under disk pressure. The executor + revalidates, acts under `cleanup`, emits `cleanup.executed`; a throwing action is logged, the + run goes on, a rule or view failure rejects it. 6. **Nuke** — one at a time: close acquisition, settle reclaim and warm pool → release all leases `killed` → shut down each unleased `ready` virtual device; with delete, destroy each `shutdown` one. 7. **Startup** — `readStartup` (`listManaged` per driver, parallel) → `doctor.reconcile({ read })` diff --git a/src/core/reaper.test.ts b/src/core/reaper.test.ts index e52f832a2..c76d9785c 100644 --- a/src/core/reaper.test.ts +++ b/src/core/reaper.test.ts @@ -199,7 +199,7 @@ async function createHarness( ...(options.tickMs === undefined ? {} : { tickMs: options.tickMs }), }); - return { clock, driver, engine, eventBus, filesystem, reaper, registry }; + return { clock, driver, engine, eventBus, executor, filesystem, reaper, registry }; } async function seedReady(harness: Awaited>) { @@ -650,6 +650,39 @@ describe("CleanupReaper", () => { harness.reaper.dispose(); }); + it("logs a cleanup action that throws at error, naming the action, rule and target, and still resolves the run", async () => { + const sink = new MemoryLogSink(); + const rule: CleanupRule = { + evaluate: () => [ + { action: "shutdown", reason: "test cleanup", rule: "test-rule", target: "dev_1" }, + ], + name: "test-rule", + }; + const harness = await createHarness( + [rule], + {}, + { logger: new JsonLinesLogger({ clock: new FakeClock(1_000), sink }), tickMs: 10_000 }, + ); + await seedReady(harness); + vi.spyOn(harness.executor, "execute").mockRejectedValue(new Error("action exploded")); + + await expect(harness.reaper.run()).resolves.toHaveLength(1); + expect(sink.records).toEqual([ + expect.objectContaining({ + level: "error", + module: "daemon.reaper", + fields: { + action: "shutdown", + error: "Error: action exploded", + rule: "test-rule", + step: "cleanup", + target: "dev_1", + }, + }), + ]); + harness.reaper.dispose(); + }); + it("prevents leasing from granting a device while cleanup is shutting it down", async () => { const rule: CleanupRule = { evaluate: () => [ diff --git a/src/core/reaper.ts b/src/core/reaper.ts index 4475fb0bc..84b933a1e 100644 --- a/src/core/reaper.ts +++ b/src/core/reaper.ts @@ -104,7 +104,20 @@ export class CleanupReaper { } for (const proposal of proposals) { - await this.options.executor.execute(proposal); + // why: one action that throws (a device whose platform has no driver) must not abort the + // proposals after it. Only the action is contained: a rule or registry-view failure above + // still rejects the run. + try { + await this.options.executor.execute(proposal); + } catch (error: unknown) { + this.#logger.error("cleanup action failed", { + step: "cleanup", + action: proposal.action, + rule: proposal.rule, + target: proposal.target, + error: stableError(error), + }); + } } return proposals; From cffd92eeaaabe0188b2dba1794d042b92e703d1a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 11:52:08 +0200 Subject: [PATCH 3/3] docs: keep the core manual within its line budget (#659) --- src/core/AGENTS.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/AGENTS.md b/src/core/AGENTS.md index bc6a4af75..7ec8d149b 100644 --- a/src/core/AGENTS.md +++ b/src/core/AGENTS.md @@ -91,9 +91,8 @@ Events: → `absent` `device.absence-detected`; `absent` → `reclaiming` `devic physical one. Physical not present: wait `health.probeIntervalMs`, not counted. 5. **Cleanup** — `CleanupReaper` on `lease.released`, `daemon.started`, `disk.pressure-detected`, a tick. `idle-shutdown`: leased once, `ready`, idle past T1, not warm-targeted. `idle-destroy`: - `shutdown`, idle (lease end, else `shutdownAt`) past T2, or T1 under disk pressure. The executor - revalidates, acts under `cleanup`, emits `cleanup.executed`; a throwing action is logged, the - run goes on, a rule or view failure rejects it. + `shutdown`, idle (lease end, else `shutdownAt`) past T2, or T1 under pressure. `CleanupExecutor` + revalidates, acts, emits `cleanup.executed`; a throwing action is logged, the run goes on. 6. **Nuke** — one at a time: close acquisition, settle reclaim and warm pool → release all leases `killed` → shut down each unleased `ready` virtual device; with delete, destroy each `shutdown` one. 7. **Startup** — `readStartup` (`listManaged` per driver, parallel) → `doctor.reconcile({ read })`