Skip to content

feat(cache,node): pluggable admission & eviction policies (ADR 040) - #1757

Merged
alpergundogdu merged 16 commits into
mainfrom
claude/cdn-eviction-admission-policies-11802e
Aug 20, 2026
Merged

alpergundogdu merged 16 commits into
mainfrom
claude/cdn-eviction-admission-policies-11802e

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #1754.

Introduces pluggable cache admission & eviction policies (ADR 040). deCDN caches while serving but previously had no admission policy and only recency-only, whole-blob eviction. This makes both decisions pluggable and adds a real frequency/heat policy.

Architecture — signals in, decisions at the sweep

  • The engine holds zero policy semantics. It emits a hit signal and exposes generic segment primitives (set_segment / segment_of / segment_bytes). Segment membership is pure in-memory metadata; the engine reads no frequency and runs no promotion.
  • AdmissionPolicy decides a segment instantly at store time.
  • EvictionPolicy::plan(ctx) -> { evict, promote } is the single sweep-time decision; the driver is a dumb executor. No tag mutation between sweeps.
  • Shipped policies: W-TinyLFU (in-tree count-min sketch keyed off BLAKE3, no doorkeeper, non-durable) + probationary admission (mechanism C: one-hit-wonders land in a capped probation segment and promote on repeat). Defaults always + lru reproduce prior behavior exactly.
  • Config-selectable, node-local (not governance/consensus): cache.admission_policy / cache.eviction_policy = always|lru|tinylfu, plus [cache.tinylfu] params.

Key correctness properties

  • Hit signal fires on serve, not just fill. deliver and the node→node serve-leg emit exactly one sighting per completed served request, so a hot resident blob accumulates frequency and promotes (fills alone only record recency; a fill-and-serve miss counts once).
  • Admit-before-observe ordering invariant: admission reads the estimate before that request's own observe, so a first-ever request lands in Probation and promotion_threshold = N means "admit to Main after N prior sightings."

Scope / limits

  • Whole-blob reclaim only. Range-aware eviction is blocked on an upstream iroh-blobs partial-truncation primitive (documented as future).
  • In-memory segment membership is lost on restart (un-capped until re-observed); gc_interval_sec=0 reclaim hole and missing write-path backpressure are carried forward, out of scope.

Docs

ADR 040 added; the appendix-blob-cache-eviction is retired and all references repointed.

Testing

Full workspace suite: 2752 passed, 0 skipped (anvil-e2e gated as in CI); clippy/fmt clean.

Follow-ups (separate issues)

🤖 Generated with Claude Code

alpergundogdu and others added 14 commits August 19, 2026 16:56
…ngine gains generic segment primitives

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…utrality

Exercises the real CacheEngine + eviction::sweep driver (not policy
structs in isolation): tinylfu admission caps probation footprint and
promotes a twice-requested blob to Main; tinylfu eviction ranks a primed
hot blob ahead of a cold burst; and lru/always defaults reproduce a
golden LRU sequence end to end.
Documents the shipped signals-in/decisions-at-sweep admission and
eviction policy architecture (AdmissionPolicy, EvictionPolicy,
FrequencyEstimator, W-TinyLFU, probationary admission) as ADR 040,
replacing the LRU-only eviction appendix it supersedes. Repoints every
cross-reference to the appendix, in architecture.md's reading order and
appendices index, appendix-observability.md, 005-protocol.md,
022-content-discovery.md, the ADR Makefile's book build list, and the
pre-launch alternatives history file.
Adds ADR 040 (Pluggable Cache Admission and Eviction Policies),
documenting the shipped signals-in/decisions-at-sweep architecture:
AdmissionPolicy/EvictionPolicy/FrequencyEstimator traits, the shared
W-TinyLFU estimator, probationary admission with sweep-time promotion,
and the whole-blob-only reclaim boundary. Repoints every cross-reference
to the now-deleted eviction appendix: architecture.md's reading order
and appendices index, appendix-observability.md, 005-protocol.md,
022-content-discovery.md, the ADR Makefile's book build list, and the
pre-launch alternatives history file.
Rewords four "prior [...] behavior" phrasings that narrated a
before/after story against the live `lru`/`always` defaults this same
ADR describes, so each sentence reads as present-tense canon on its
own.
…ix refs; validate promotion_threshold

Serve paths now emit the ADR 040 hit signal exactly once per served request.
Split touch into pub observe_hit (recency + frequency observe) and private
record_access (recency only): deliver and serve_leg call observe_hit once at
successful completion, fill paths (populate_inner) only record_access, and get
keeps the full signal. This fixes hot resident blobs never accumulating
frequency, without double-counting a miss that fills and serves.

Also: repoint 12 stale appendix-blob-cache-eviction.md doc refs to ADR 040;
reject cache.tinylfu.promotion_threshold = 0 at config load; add ADR
known-limitation note on the probation-cap under-count; reconcile ADR
serve-signal wording to match the code.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 19, 2026 22:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements ADR 040 by making cache admission and eviction policy pluggable in the cache crate while keeping policy semantics out of the engine, and wiring policy selection/config into the node runtime and operator-facing docs.

Changes:

  • Add AdmissionPolicy / EvictionPolicy / FrequencyEstimator traits plus default (always/lru) and tinylfu implementations, and extend CacheEngine with segment metadata + a hit-signal hook.
  • Wire policy selection and TinyLFU tuning via config (cache.{admission_policy,eviction_policy}, [cache.tinylfu]) and treat policy changes as restart-required in node reload logic.
  • Update ADR/docs to retire the prior blob-cache eviction appendix and document ADR 040 + the new configuration surface.

Reviewed changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
crates/node/tests/sighup_signal.rs Updates test resolved-config seed with new cache policy fields.
crates/node/src/runtime/reload.rs Adds resolved defaults for new policy fields + marks them restart-required.
crates/node/src/runtime/mod.rs Wires estimator/admission/eviction policy selection into bring-up; injects policy into eviction task.
crates/node/src/runtime/eviction.rs Updates driver to delegate ordering/decisions to injected EvictionPolicy and adds end-to-end policy tests.
crates/node/src/handlers/client/serve_leg.rs Emits cache hit-signal on successful completion of served request.
crates/node/src/handlers/client/delivery.rs Emits cache hit-signal on successful completion of served request.
crates/common/src/config/types.rs Adds config schema for policy selectors + [cache.tinylfu] tuning knobs.
crates/common/src/config/resolved.rs Adds resolved fields for policy selectors + resolved TinyLFU knobs.
crates/common/src/config/mod.rs Adds defaults/constants + resolver validation for policy selector names and TinyLFU params.
crates/cli/tests/config_validate.rs Updates CLI config-validation tests for new resolved fields.
crates/cli/src/commands/config.rs Documents new cache policy settings in default config template + updates tests.
crates/cache/src/probe_hold.rs Updates docs to point at ADR 040 references.
crates/cache/src/policy/tinylfu.rs Adds TinyLFU estimator + probation admission + TinyLFU eviction policy.
crates/cache/src/policy/sketch.rs Adds count-min sketch implementation keyed on content hashes.
crates/cache/src/policy/mod.rs Introduces policy traits, shared context structs, and re-exports.
crates/cache/src/policy/lru.rs Adds behavior-preserving LruEviction and AlwaysAdmit implementations.
crates/cache/src/metrics.rs Updates eviction/health metric section docs to reference ADR 040.
crates/cache/src/lib.rs Exposes new policy module and re-exports policy traits/types.
crates/cache/src/engine.rs Adds admission + frequency estimator plumbing; adds in-memory segment membership + hit signal semantics.
adr/Makefile Adds ADR 040 and removes retired blob-cache eviction appendix from book build.
adr/architecture.md Links ADR 040 from the Operations chapter; removes retired appendix reference.
adr/appendix-observability.md Repoints eviction-related references from retired appendix to ADR 040.
adr/appendix-blob-cache-eviction.md Removes retired appendix (replaced by ADR 040).
adr/040-cache-policy.md Adds ADR 040 describing the pluggable policy architecture and acceptance criteria.
adr/022-content-discovery.md Updates pinning reference from retired appendix to ADR 040.
adr/005-protocol.md Updates probe-hold reference from retired appendix to ADR 040.
adr/_history/alternatives-pre-launch.md Updates historical reference to point at ADR 040.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/node/src/runtime/eviction.rs Resolved
Comment thread crates/node/src/runtime/mod.rs Resolved
Comment thread crates/common/src/config/types.rs Resolved
@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 81.09% (was 80.84%, ▲ +0.24%) — 59628/73537 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 91.84% 3792/4129 ▲ +0.48%
crates/cache/src/lib.rs 100.00% 28/28 ● 0.00%
crates/cache/src/policy/lru.rs 98.92% 92/93 new
crates/cache/src/policy/sketch.rs 100.00% 68/68 new
crates/cache/src/policy/tinylfu.rs 99.09% 218/220 new
crates/cache/src/probe_hold.rs 0.00% 0/6 ● 0.00%
crates/cli/src/commands/config.rs 86.30% 523/606 ▲ +0.14%
crates/common/src/config/mod.rs 97.08% 6625/6824 ▲ +0.05%
crates/common/src/config/resolved.rs 100.00% 33/33 ● 0.00%
crates/node/src/handlers/client/delivery.rs 95.92% 235/245 ▲ +0.02%
crates/node/src/handlers/client/serve_leg.rs 85.80% 139/162 ▲ +0.09%
crates/node/src/runtime/eviction.rs 79.13% 345/436 ▲ +41.20%
crates/node/src/runtime/mod.rs 48.23% 1089/2258 ▼ 0.53%
crates/node/src/runtime/reload.rs 96.78% 1412/1459 ▲ +0.05%
History
Commit Total Δ vs base
8d53626 81.08% ▲ +0.24%
1c987af 81.08% ▲ +0.24%
36bfe41 (HEAD) 81.09% ▲ +0.24%

alpergundogdu and others added 2 commits August 19, 2026 23:34
…comments

The anvil-e2e-gated ResolvedCache literal in anvil_bringup_shutdown_e2e.rs
missed the three new cache-policy fields; default cargo nextest skips that
feature so it slipped past local runs and broke CI clippy (--all-features)
and the anvil settlement compile.

Also addresses the PR review:
- Move observe_hit after the terminal StreamEnd frame is written on both
  serve paths (delivery, serve_leg), so a serve that fails to finish cleanly
  is not counted.
- Repoint stale "LRU eviction driver" doc comments to policy-ranked wording
  (eviction.rs module docs, runtime/mod.rs, config/types.rs); the driver now
  executes the injected EvictionPolicy.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ook, segment ordering, free default path

- Warn at bring-up when admission_policy=tinylfu but eviction_policy!=tinylfu:
  promotion and the probation cap live only in TinyLfuEviction::plan, so with
  lru eviction the probation labels are inert and the estimator runs for no
  effect. Document the coupling in ADR 040 (the two selectors are chosen
  independently but tinylfu admission depends on tinylfu eviction).
- Remove the never-invoked EvictionPolicy::on_access hook and its TinyLfu
  override; correct the plan() doc that referenced it. Observes route through
  the shared FrequencyEstimator, which frequency policies read directly.
- admit_bao_stream: label the segment only after protect_partial succeeds, so a
  failed protect leaves no stale membership entry for an unprotected blob.
- observe_hit: clone the estimator Option only via a double-deref of the load()
  guard, so the default (no-estimator) path does no Arc clone / refcount
  roundtrip, instead of load_full()'s unconditional outer-Arc clone.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 62cffd1 into main Aug 20, 2026
50 of 52 checks passed
@alpergundogdu
alpergundogdu deleted the claude/cdn-eviction-admission-policies-11802e branch August 20, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pluggable cache admission & eviction policies (ADR 040)

2 participants