diff --git a/robot-docs/cache-warming.md b/robot-docs/cache-warming.md index 5819b976a..dbe7cb586 100644 --- a/robot-docs/cache-warming.md +++ b/robot-docs/cache-warming.md @@ -51,7 +51,7 @@ ping at `min(0.9 × ttl, ttl − 10)` seconds after the previous send — from t a warm cache bills the next turn about `read × cached` input, a cold one about `write × cached` — `write` the matching tier's write multiplier (default 1.0), `read` the entry's read multiplier (default 0.1) — and each ping costs another `read × cached`. `n` pings pay for themselves while `(n + 1) × read ≤ write`, so at most `floor(write / read) − 1` consecutive pings per idle stretch, then it stops and lets the cache go cold: ski rental, with the break-even at Claude's 5-minute tier at 1.25 / 0.1 − 1 = 11 pings, one every 270 s, keeping the cache for about 54 minutes. a write priced under twice the read means no warming. a new turn resets the budget. -a tick that fires late (a sleeping machine, a blocked scheduler) past half the margin left before expiry sends nothing: the cache is probably gone, and a ping would pay for a rewrite. +a tick that fires late (a sleeping machine, a blocked scheduler) past the margin left before expiry, less 5s for the ping to land (never less than 2s: jitter does not shrink with the TTL), sends nothing: the cache is probably gone, and a ping would pay for a rewrite. inside it the ping goes out, since the cache is still alive to keep: at the 5m tier a tick up to 25s late pings. warming also stops when: diff --git a/src/albedo/harness/extensions/warm/extension.gleam b/src/albedo/harness/extensions/warm/extension.gleam index bb98df67d..711c1e5e2 100644 --- a/src/albedo/harness/extensions/warm/extension.gleam +++ b/src/albedo/harness/extensions/warm/extension.gleam @@ -49,8 +49,7 @@ type Warmer { type Message { Observed(extension.Session, extension.SessionEvent) - /// A ping is due. `latest_ms` is the last moment it still beats the cache's - /// expiry with half its margin to spare. + /// A ping is due. `latest_ms` is the last moment it still goes out. Tick(generation: Int, latest_ms: Int) } @@ -201,7 +200,7 @@ fn schedule( False -> finished_ms } let at = anchor + plan.interval_ms - let latest = at + { plan.ttl_ms - plan.interval_ms } / 2 + let latest = latest_ms(at, plan.ttl_ms, plan.interval_ms) let delay = int.max(0, at - requests.now()) let _ = process.send_after(inbox, delay, Tick(warmer.generation, latest)) Nil @@ -348,6 +347,24 @@ fn tier_write(entry: Option(cache_ttl.Entry), ttl_seconds: Int) -> Float { } } +/// What a ping needs after it goes out to land before the cache expires: the +/// request's flight to the provider. +const landing_ms = 5000 + +/// Scheduler jitter does not shrink with the TTL, so a tick this late is a +/// loaded machine, not a sleeping one: the grace never drops below it. Real +/// TTLs leave tens of seconds; it is the short fixture TTL it is for. +const jitter_floor_ms = 2000 + +/// The last moment a ping due at `at` still goes out: the margin the interval +/// leaves before the cache expires, less the ping's landing time, but at +/// least the jitter floor. Later than that the cache is probably gone and a +/// ping would pay for a rewrite; earlier, dropping the tick would let a cache +/// that is still alive expire. +pub fn latest_ms(at: Int, ttl_ms: Int, interval_ms: Int) -> Int { + at + int.max(ttl_ms - interval_ms - landing_ms, jitter_floor_ms) +} + /// Ping at the smaller of ten percent and ten seconds under the TTL, so the /// entry is always refreshed with time to spare. fn interval_ms(ttl_seconds: Int) -> Int { diff --git a/test/e2e/warm_test.py b/test/e2e/warm_test.py index 02ca6fb44..aa7be2901 100644 --- a/test/e2e/warm_test.py +++ b/test/e2e/warm_test.py @@ -60,8 +60,8 @@ CHILD_JOB_TASK = "start the build and report back" # A local cache-table override matching the fixture host: a short refresh TTL, # bought at 1.0x and read at 0.3x, so pings come 2.7s apart and at most -# floor(1.0 / 0.3) - 1 = 2 of them pay for themselves. A tick later than half -# the 0.3s margin is dropped, so a shorter TTL fails on a loaded machine. +# floor(1.0 / 0.3) - 1 = 2 of them pay for themselves. A tick later than the +# 2s jitter floor (the 0.3s margin is under the 5s landing time) is dropped. TTL_SECONDS = 3 # Longer than a ping interval by more than a loaded machine's jitter: a ping # that was due would have gone out within it. diff --git a/test/harness/warm_grace_test.gleam b/test/harness/warm_grace_test.gleam new file mode 100644 index 000000000..66d31bd05 --- /dev/null +++ b/test/harness/warm_grace_test.gleam @@ -0,0 +1,39 @@ +//// A warm tick that fires late is dropped only when the cache is probably +//// gone. The grace is the margin the interval leaves less the ping's 5s +//// landing time, floored at the scheduler's jitter: the e2e fixture's 3s TTL +//// leaves 300ms, which a loaded machine overruns, while a real 5m TTL leaves +//// 25s. E2E cannot make a tick late on demand, so the arithmetic is pinned +//// here. + +import albedo/harness/extensions/warm/extension +import gleeunit/should + +pub fn a_real_ttl_keeps_its_margin_less_the_landing_test() -> Nil { + // claude's 5m tier: pings every 270s, 30s of margin, 25s of grace + extension.latest_ms(270_000, 300_000, 270_000) + |> should.equal(295_000) +} + +pub fn the_hour_tier_keeps_its_margin_less_the_landing_test() -> Nil { + // pings every 3240s, 360s of margin, 355s of grace + extension.latest_ms(3_240_000, 3_600_000, 3_240_000) + |> should.equal(3_595_000) +} + +pub fn a_tick_still_inside_the_cache_lifetime_pings_test() -> Nil { + // 20s late at the 5m tier: the cache lives 10s more, so the ping goes out + { 270_000 + 20_000 <= extension.latest_ms(270_000, 300_000, 270_000) } + |> should.be_true +} + +pub fn a_short_ttl_keeps_the_jitter_floor_test() -> Nil { + // the fixture's 3s TTL: 2.7s interval, 300ms margin, under the landing time + extension.latest_ms(2700, 3000, 2700) + |> should.equal(4700) +} + +pub fn a_sleeping_machine_is_past_every_grace_test() -> Nil { + // woken an hour after the 5m tier's tick was due + { 270_000 + 3_600_000 > extension.latest_ms(270_000, 300_000, 270_000) } + |> should.be_true +}