Repository navigation
fix(engine): isolate the prefix cache per tenant and cache only written KV - #119
Merged
Merged
Conversation
…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]>
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
ExtraHashKey::CacheSaltexisted 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.The fix
NormalRequest.cache_saltflows intoSequence, and every block hash, the non-paged prefix cache key and the recurrent-prefix keys carry it. Equal salts share; different salts never do.X-Org-Idplus an optionalcache_saltbody field (/v1/chat/completions,/v1/completions);/v1/messagesand/v1/responsestake 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 guard
kv_isolation(hanzo-engine/src/paged_attention/kv_isolation.rs) drives the realPagedAttentionSchedulerand 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_apartidentical_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_kvreuse_after_evictionsalt_partitions_every_blockprefix_cacher::tests::kv_isolation::*(salted lookup, separate entries per tenant, no salted spill to disk)tenant::tests::*(org partition, a body salt cannot reach another org)CI: a
kv-isolationgate inhanzo.yml, whichcicd.ymlruns throughhanzoai/cion the git.hanzo.ai runner fleet before the image is built. It sits ahead of the workspace-widecheck, so breakage in crates it doesn't build can't skip it. Thehanzoai/cirunner already refuses a gate whose test runner reports no results, so a rename can't silently empty it.Caveat: the
hanzoai/enginemirror 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 startscicd.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 tooconcurrent_tenants_with_identical_prompts_stay_apart:acmeread KV written byevilidentical_prefixes_with_contradictory_suffixes: the retry read position 32, a slot no forward wrotecancellation_mid_generation_caches_only_written_kv: the retry read position 31, a slot no forward wrotesalt_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_evictionpasses on both versions: it guards eviction rather than reproducing a bug.Full lib suites with the fix:
hanzo-engine713 passed (36 ignored),hanzo-server-core142 passed.cargo fmt --all -- --checkis clean.Follow-ups (not in this PR)
universe/infra/lan/engines/spark-serve-vllm.sh,dgx-serve-zen6.sh): prefix caching is on with no tenant partition. vLLM accepts a per-requestcache_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}andprevious_response_idnever 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.X-Org-Idto 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-Idis trusted as gateway-minted, so engines must stay unreachable by clients that could set it themselves.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.