Skip to content

impl(011): wire the embedding cache into the backend (3.14) - #490

Merged
bartekus merged 1 commit into
mainfrom
impl/011-cache-wiring
Nov 4, 2026
Merged

bartekus merged 1 commit into
mainfrom
impl/011-cache-wiring

Conversation

@bartekus

@bartekus bartekus commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Spec 011: the embedding cache wired into the backend (3.14, 3.7.1)

Wires the cache that increment 4 (#445) landed into EmbedLinearBackend. No file under src/cache.rs changes; the wiring lives in src/backend.rs and src/lib.rs.

  • EmbedConfig gains embedding_cache: Option<CacheLimits>, off when None. Setup builds the cache with the synthetic encoder's repeatability evidence for a synthetic bundle and none otherwise, so an onnx bundle with a cache configured is refused cache_not_equivalent (today it is refused unqualified_platform first).
  • Each prepared request is looked up on the caller, before any handoff, keyed by principal handle and post-truncation token ids and mask. A hit applies the head on the caller, reaches no worker, and is charged observed{units_per_call} (3.11.4).
  • A miss carries its ticket to the worker. The worker admits the embedding only when the run completed and its attempt was not cancelled or abandoned, so a late result is never cached (3.12.3, 3.14.4). A clear_cache or erase_principal during the run invalidates the ticket (3.14.5).
  • New methods: EmbedLinearBackend::cache_stats(), clear_cache() and erase_principal(handle). With the cache off, the counters stay zero.
  • 011's Implementation record gains a paragraph after increment 4's. It goes there instead of at the end so it does not touch impl(011): increment 6, the Node binding's local-embed feature and synthetic conformance parity #485's hunk.

Interaction with #485

#485 builds EmbedConfig with a struct literal in integrations/rustev-node/src/local_embed.rs and backends/rustev-backend-embed-linear/tests/support_routing/conformance.rs. Whichever of the two lands second must add embedding_cache: None to both literals. It is a compile error, not a silent change.

Tests

tests/backend/cache.rs (9 tests):

  • cache off by default
  • a hit's logits bitwise equal to the miss's and to an uncached backend's
  • principal isolation
  • eviction by entries (least recently used) and by bytes
  • clear_cache and erase_principal
  • erase_principal racing an in-flight run
  • cancelled, failed and abandoned runs not admitted
  • the cache configuration leaves the descriptor and cost bound unchanged (3.5.2)

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

AI review (no-findings)

Reviewing the diff across the five changed source files and the spec increment.

backend.rs — open refactor
The old Self::start(config, preparer.clone(), default_run(&preparer)?) cloned the Arc only to drop the original after the call. The new ordering (let run = default_run(&preparer)?; Self::start(config, preparer, run)) moves the Arc directly. Functionally identical; no defect.

backend.rs — open_cache
The #[cfg(feature = "synthetic")] arm on the EncoderKind match correctly maps to RepeatabilityEvidence::Synthetic only when the feature is compiled in; without it the _ arm produces Unproven, and EmbeddingCache::new will refuse with cache_not_equivalent. Consistent with the spec note.

backend.rs — hit path
The early return report(output, cost.units_per_call, false) on a cache hit exits before the Job is constructed. No miss ticket is created; no worker is involved. Charge is units_per_call only. Correct per 3.11.4.

backend.rs — admission condition in worker
result.map(|(output, embedding)| { … admit … }) runs only on Ok; a failed run is Err and never reaches admit. For succeeded runs, j.abandoned || cancel.is_raised() gates the outcome. If abandoned, j.result = Some(result) is also skipped (separately guarded), but the map closure still executes, calling admit with RunOutcome::Cancelled — correctly voiding the ticket. Consistent with 3.14.4 and 3.12.3.

backend.rs — lock ordering
drop(cancel) moved from before the state-lock acquisition to inside the locked block. cancel is an Arc wrapping a cancel signal (simple atomic); its drop just decrements the refcount and does not acquire any lock. No deadlock path introduced. The state→job→cache ordering is consistent: caller calls lock(cache).lookup with no other lock held; worker calls lock(cache).admit inside state+job locks but never the reverse.

backend.rs — erase_principal / clear_cache
These call lock(cache).{erase_principal,clear}() with no other lock held. The test erase_principal_racing_an_in_flight_run_is_not_stored correctly verifies the generation-stamp mechanism refuses the stale ticket.

tests/backend/cache.rs
Test coverage matches the spec's enumerated cases: off by default, bit-for-bit equality between hit/miss/uncached, principal isolation, entry-bound eviction, byte-bound eviction (1-byte limit ensures zero admitted), clear_cache and erase_principal, racing erase against in-flight run, cancelled/failed/abandoned not admitted, and descriptor/cost-bound independence.

specs/011-semantic-backend/spec.md
The increment 4 wiring note accurately describes the implementation. No claim about code is made as draft; the spec's status/implementation fields are not changed in the diff so no lifecycle issue is introduced. References to 3.5.2, 3.7.1, 3.11.4, 3.12.3, 3.14.x are internal section references, not cross-corpus spec ids.

No concrete defect found.

{"head": "9c7ab775322bb86b870b6847915cf81fa03fe036", "verdict": "no-findings", "findings": []}

Subject: f1f08cd...9c7ab77, diff digest 6576b116da81521d1a55599e6dded5b9568fe40e433fd2305624aa19567d11ed, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus added this pull request to the merge queue Nov 3, 2026
@bartekus
bartekus removed this pull request from the merge queue due to a manual request Nov 3, 2026
@bartekus
bartekus added this pull request to the merge queue Nov 4, 2026
Merged via the queue into main with commit 5d9923e Nov 4, 2026
27 checks passed
@bartekus
bartekus deleted the impl/011-cache-wiring branch November 4, 2026 01:57
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.

1 participant