A HabitatHost opens a Habitat with a * * * * * durable schedule and waits for its HabitatCronTask to sleep. The host evicts it from RAM, reopens it to show the deadline came back from SQLite, then warps the clock past the minute. A transparent reload fires the schedule and re-arms it.
Run it
pnpm exec tsx examples/08-durable-cron-eviction/run.ts # narrated, offline faux model
pnpm exec tsx examples/08-durable-cron-eviction/run.ts --smoke # PASS/FAIL lines, exit 0 when all pass
MYCEL_URL=https://mycel.thefocus.ai pnpm exec tsx examples/08-durable-cron-eviction/run.ts --live --model mycel:z-ai/glm-5.3-flash
Add --keep to keep the temp root.
Walkthrough
asleep: the cron task issleepingwith a wake time at the next minute.evicted: afteridleEvictMs(150 ms),evictIdle()closes it.wake-at-survives: a reopen (before the warp) has the samewake().at.- The clock is warped past that time, and
host.get()+resume()stands in for the host's alarm. firedandrearmed: a cron turn was answered and the next wake is a minute later.
Files
run.ts— the cron drill and theClockWarphelper.PROMPT.md— prompt to rebuild this lab.FINDINGS.md— what we learned.
Prompt
Build a standalone lab under `examples/08-durable-cron-eviction/` that tests one thing: a sleeping durable `HabitatCronTask` keeps its `wake().at` deadline across `HabitatHost` idle eviction, and a transparent reload fires it on time and re-arms it (`@umwelten/habitat` on `@earendil-works/pi-durable`).
Read these files first to match exact types and invariants:
- `docs/specs/orchestration/durable-habitats.md`
- `packages/habitat/src/durable/durable-habitat.ts` (`HabitatHost.get`, `evictIdle`, `closeAll`)
- `packages/habitat/src/durable/extensions.ts` (`HabitatCronTask`, `HabitatSchedulesDoc`)
Requirements:
1. One file, `examples/08-durable-cron-eviction/run.ts`, runnable via `pnpm exec tsx examples/08-durable-cron-eviction/run.ts`. Do not import from other example folders.
2. Default to an offline `fauxProvider` from `@earendil-works/pi-ai` (a self-replenishing `FauxResponseFactory` that acknowledges the tick). Support `--live --model <provider:model>` via `resolvePiModel(parseModelSpecString(spec))` from `@umwelten/pi`.
3. In a `mkdtemp` root, create `HabitatHost({ dataDir, idleEvictMs: 150, defaultModel, modelsFactory })` and `register` one Habitat with schedule `{ id: "tick", cron: "* * * * *", prompt: "[cron] ..." }`.
4. Steps: `host.get()`, poll until `harness.wake()` is not active and has `at`; read `lastFiredAt` from `HabitatSchedulesDoc`. Sleep past `idleEvictMs`, call `evictIdle()`. Reopen without changing the clock and check `wake().at` is the same deadline; evict again. Then warp `Date.now` forward past the deadline (cron is minute-granular; do not wait 60 s), call `host.get()` + `harness.resume()`, poll `lastFiredAt` until it changes, wait for idle, read the last assistant text, and check the next `wake().at` is later.
5. Checks, each printed as `PASS <name>: ...` or `FAIL <name>: ...`: asleep, evicted, wake-at-survives, fired, rearmed.
6. The warp only goes forward. The Harness reads its clock when it opens, so only Habitats opened after the warp see it. Restore `Date.now` in `finally`.
7. `--smoke` prints only the PASS/FAIL lines and exits 0 only if all pass; without it, narrate. `--keep` keeps the temp root.
8. Strict TypeScript, zero `any`. `closeAll()`, restore the clock, and remove the temp root in `finally`. Verify with `pnpm exec tsc --noEmit -p examples/tsconfig.json` and `--smoke`.
Also write `README.md` (frontmatter: title, track, status, tests, needs, run, smoke, deck), `PROMPT.md`, and `FINDINGS.md`.
--live opt-in
What we learned
The answer
HabitatCronTask stores its until deadline in the task row. Because of that, wake().at survives HabitatHost.evictIdle(): a reopen before the warp reported the exact same deadline. After the warp, the reopened Habitat fired the schedule, answered the tick, and re-armed for the next minute.
Gotchas
- Cron works to the minute. The lab warps
Date.nowforward instead of waiting up to 60 s. - The Harness reads its clock at open. Only a Habitat reopened after the warp sees it. That is why the lab evicts again before warping.
- The warp is process-wide. It only goes forward and is restored in
finally. It is a test trick, not how a real host wakes. - The alarm is simulated.
evictIdle()arms a realsetTimeoutforwake.at, but the lab callshost.get()+resume()itself instead of waiting for it. evictIdle()skips active Habitats. The lab polls untilwake().activeis false before evicting.
Not tested
- The real eviction timer firing after a real minute.
- A host process restart (a new
HabitatHoston the samedataDir). - Schedules missed while the process was down for several minutes.
- A live model run (only the offline faux run was done).
Changes from the torture box
- New
wake-at-survivescheck: reopen before the warp and compare deadlines. The old drill only showed the reload fired.
Smoke run
PASS asleep: HabitatCronTask sleeping, wake at +33s PASS evicted: evictIdle() (idleEvictMs=150) -> [cron-agent]; open [] PASS wake-at-survives: reopened wake at +33s (same deadline: true) PASS fired: schedule fired a turn on the reloaded Habitat; reply="Cron tick acknowledged: [cron] tick from the durable schedu…" PASS rearmed: next wake at +59s (later than the one that fired)