Skip to content

fix(engine): isolate the prefix cache per tenant and cache only written KV - #119

Merged
Darkhorse7stars merged 1 commit into
mainfrom
fix/799-kv-tenant-isolation
Nov 19, 2026
Merged

Darkhorse7stars merged 1 commit into
mainfrom
fix/799-kv-tenant-isolation

Conversation

@Darkhorse7stars

Copy link
Copy Markdown
Member

Closes the engine half of hanzo-inc/cloud#799 and the "Hanzo Engine prefix cache: yes, residual risk" finding of hanzo-inc/cloud#798.

What was wrong

Two separate ways a request could decode against KV that is not its own:

  1. No tenant boundary. Prefix blocks were hashed from token content only. ExtraHashKey::CacheSalt existed but nothing set it, so any two requests with the same prompt prefix shared blocks, across orgs. The sequence-level cache used without paged attention, and its disk spill, were keyed by tokens alone too.
  2. Blocks cached before their KV existed. The scheduler registered blocks in the prefix cache by token count, not by what a forward had written:
    • A prompt admitted and then preempted in the same pass (the length bucketing does this whenever concurrent prompts differ in length) had all its fresh blocks cached before any forward ran. Its retry, or any request with the same prefix, then hit those blocks and read whatever they held: nothing, or the KV of the request that last owned them.
    • Every finished, canceled or preempted sequence cached its last block including the slot of the final sampled token, whose KV only the next forward writes. When the length was a block multiple, a later hit read that stale slot.
    • Errored, aborted and ignored sequences were cached too, though no forward may have filled their blocks.

The fix

  • NormalRequest.cache_salt flows into Sequence, and every block hash, the non-paged prefix cache key and the recurrent-prefix keys carry it. Equal salts share; different salts never do.
  • The server derives the salt from the gateway's X-Org-Id plus an optional cache_salt body field (/v1/chat/completions, /v1/completions); /v1/messages and /v1/responses take the org alone. Both parts are JSON-encoded together, so a body salt narrows sharing within an org and can never equal another org's salt (hanzo-server-core/src/tenant.rs).
  • The non-paged cache drops salted prefixes on eviction instead of spilling them: a spill file is keyed by tokens alone and restores unsalted.
  • The scheduler caches only positions a forward wrote: all but the last sampled token, nothing for a sequence preempted before its first forward, and nothing for a sequence that did not finish normally.

The guard

kv_isolation (hanzo-engine/src/paged_attention/kv_isolation.rs) drives the real PagedAttentionScheduler and block pool against a simulated KV store. Each slot records the context its KV was computed from and the salt of the request that wrote it, and every forward checks every position it reads. Tests:

  • tenants_never_share_cached_prompts, concurrent_tenants_with_identical_prompts_stay_apart
  • identical_prefixes_with_contradictory_suffixes (preemption before its first forward, after a request that answered the other way has used the pool)
  • cancellation_mid_generation_caches_only_written_kv
  • reuse_after_eviction
  • salt_partitions_every_block
  • non-paged: prefix_cacher::tests::kv_isolation::* (salted lookup, separate entries per tenant, no salted spill to disk)
  • server: tenant::tests::* (org partition, a body salt cannot reach another org)

CI: a kv-isolation gate in hanzo.yml, which cicd.yml runs through hanzoai/ci on the git.hanzo.ai runner fleet before the image is built. It sits ahead of the workspace-wide check, so breakage in crates it doesn't build can't skip it. The hanzoai/ci runner already refuses a gate whose test runner reports no results, so a rename can't silently empty it.

Caveat: the hanzoai/engine mirror on git.hanzo.ai has Actions turned off (no Actions tab; hanzo-inc/cloud's mirror runs on every sync), and GitHub Actions is off for this repo. Until Actions is enabled on that mirror, no gate in this repo runs anywhere, this one included. Enabling it also starts cicd.yml's image build on every push to main, which is why I've left that decision to a maintainer.

Results

With the fix, all 13 tests pass (cargo test -p hanzo-engine -p hanzo-server-core --lib -- kv_isolation tenant::).

With only the fix's logic reverted (unsalted keys, the last sampled slot cached, blocks of a prompt preempted before its first forward cached, unfinished sequences cached) and the API left in place, 8 of the 9 engine tests fail. Each failure names the read that would have decoded wrong KV:

  • tenants_never_share_cached_prompts: seq 2 (salt "evil") read position 0: KV written by tenant "acme", and the unsalted request read it too
  • concurrent_tenants_with_identical_prompts_stay_apart: acme read KV written by evil
  • identical_prefixes_with_contradictory_suffixes: the retry read position 32, a slot no forward wrote
  • cancellation_mid_generation_caches_only_written_kv: the retry read position 31, a slot no forward wrote
  • salt_partitions_every_block, and all three non-paged tests (another tenant's lookup hit, three tenants' entries collapsed into one, the salted prefix spilled to disk)

reuse_after_eviction passes on both versions: it guards eviction rather than reproducing a bug.

Full lib suites with the fix: hanzo-engine 713 passed (36 ignored), hanzo-server-core 142 passed. cargo fmt --all -- --check is clean.

Follow-ups (not in this PR)

  • vLLM on spark/dgx (universe/infra/lan/engines/spark-serve-vllm.sh, dgx-serve-zen6.sh): prefix caching is on with no tenant partition. vLLM accepts a per-request cache_salt; the router or gateway must set it from the org, or prefix caching must go off on shared processes.
  • hanzo-server-core/src/cached_responses.rs: the Responses-API cache is keyed by response ID alone (a random UUID). GET/DELETE /v1/responses/{id} and previous_response_id never check the caller's org, so anyone holding another tenant's ID can read, delete or continue that conversation. It needs the org stored with each entry and checked on every lookup.
  • Cloud relay → engine: the salt only partitions production traffic if the cloud relay forwards X-Org-Id to hanzo-router (which forwards all headers to the engine). I did not find where the relay sets it; worth confirming before relying on this.
  • X-Org-Id is trusted as gateway-minted, so engines must stay unreachable by clients that could set it themselves.
  • Seen while reviewing, not fixed here: on the prompt path's starvation branch, schedule() can preempt a sequence it admitted earlier in the same call, and that sequence stays in the batch it returns. A prompt with no blocks gets pad slots, so it would run one forward against no KV and keep the sampled token. Rare (64+ starved passes), and confined to that one request.

…en KV

Prefix blocks were hashed from token content alone, so any two requests with the
same prefix shared KV across orgs, and the sequence-level cache and its disk spill
were keyed by tokens too. Every block hash, sequence-cache key and recurrent-prefix
key now carries the request's cache salt, which the server derives from the
gateway's X-Org-Id plus an optional cache_salt body field. Salted prefixes are
dropped instead of spilled to disk.

The scheduler also cached blocks no forward had written: a prompt preempted in the
pass that admitted it, the last sampled token's slot of every finished or preempted
sequence, and sequences that ended in error. Now only written positions become
reusable.

kv_isolation drives the real scheduler against a simulated KV store and checks every
read for unwritten, foreign-context or other-tenant KV; hanzo.yml gates on it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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