← All Labs Lab 08 / 28 Cron survives eviction
Lab 08 · Durable agents · Learned

Cron survives eviction

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.

Learned Needs: text-generation Smoke: pnpm exec tsx examples/08-durable-cron-eviction/run.ts --smoke Run: pnpm exec tsx examples/08-durable-cron-eviction/run.ts Deck ↗ Source: examples/08-durable-cron-eviction

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

zsh
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

  1. asleep: the cron task is sleeping with a wake time at the next minute.
  2. evicted: after idleEvictMs (150 ms), evictIdle() closes it.
  3. wake-at-survives: a reopen (before the warp) has the same wake().at.
  4. The clock is warped past that time, and host.get() + resume() stands in for the host's alarm.
  5. fired and rearmed: a cron turn was answered and the next wake is a minute later.

Files

  • run.ts — the cron drill and the ClockWarp helper.
  • PROMPT.md — prompt to rebuild this lab.
  • FINDINGS.md — what we learned.

Prompt

Coding-Agent Prompt · Ready to Paste
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`.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --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.now forward 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 real setTimeout for wake.at, but the lab calls host.get() + resume() itself instead of waiting for it.
  • evictIdle() skips active Habitats. The lab polls until wake().active is false before evicting.

Not tested

  • The real eviction timer firing after a real minute.
  • A host process restart (a new HabitatHost on the same dataDir).
  • 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-survives check: reopen before the warp and compare deadlines. The old drill only showed the reload fired.

Smoke run

Visual Target Wireframe ASCII Layout
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)