Repository navigation
Prefix cache: fix the hit-rate collapse (epoch churn, demand-gated writes, 1,024-token checkpoints, credit band, bounded fence, index sizing, sidecar parity) #1207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 61 commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
937a75d
docs(reports): prefix cache hit-rate analysis and levers (2026-09-26)
Gajesh2007 4433c01
ops(cache-routing): plan-gate widening runbook and Nemotron/Bonsai tu…
Gajesh2007 831ae1b
feat(registry): prefer a proven cache holder inside the near-tie band
Gajesh2007 5b84a8a
fix(cache): bound the proof fence and narrow mismatch invalidation
Gajesh2007 9647118
feat(coordinator): forward observed repeat demand to providers and si…
Gajesh2007 6409a94
feat(provider): gate complete-checkpoint donations on observed repeat…
Gajesh2007 f89ad62
deploy: sync the sanitized prod env reference with live cache-routing…
Gajesh2007 8f0f7f1
test(e2e): observe cache_repeated_prefix_tokens in the provider wire …
Gajesh2007 75723ca
test(prompt-parity): reproduce sidecar/provider prompt-chain mismatches
Gajesh2007 08431f2
fix(registry): spread tied cache holders, clear cache selection on pl…
Gajesh2007 b8f0d90
feat(prefix-cache): stride-aligned historical checkpoints, K retentio…
Gajesh2007 72b07a6
fix(promptsidecar): mirror provider prompt preparation the planner sk…
Gajesh2007 98816d5
test(registry): discriminate the tied-holder burst by first-scan commits
Gajesh2007 f2acfd5
Merge pc/cache-credit: proven cache holders win inside the near-tie band
Gajesh2007 99c26ab
docs(reports): correct the mismatch-detail reading in the hit-rate an…
Gajesh2007 bb119e7
fix(registry): bound the demand-index TTL sweep per observe
Gajesh2007 c8e06d9
fix(cache): lift a replaced capability's fence at the heartbeat, not …
Gajesh2007 cd633fb
Merge pc/fence-bound: bounded proof fence and plan-scoped holder inva…
Gajesh2007 c60610b
Merge pc/demand-donation: demand-gated SSD donation with a coordinato…
Gajesh2007 d559921
fix(provider): stop rotating the SSD cache epoch on per-file removals
Gajesh2007 279224c
docs: cache routing credit band, bounded proof fence, demand-gated do…
Gajesh2007 950c2a7
Merge branch 'feat/prefix-cache-hit-rate' into pc/checkpoint-geometry
Gajesh2007 5a1b5a2
fix(promptsidecar): refuse inputs the provider renders differently; s…
Gajesh2007 eff5c61
docs: record the 30-minute SSD cache TTL sign-off and per-file evicti…
Gajesh2007 722347d
fix(provider): harden per-file SSD removals against rebuild, reader a…
Gajesh2007 089b78a
fix(cache): drop a provider's deeper holders when it hits at a shorte…
Gajesh2007 8c7ade6
feat(provider): raise the SSD prefix-cache TTL from 15 to 30 minutes
Gajesh2007 0a97f02
Merge pc/fence-bound: drop a provider's deeper holders on a shorter hit
Gajesh2007 e81dee1
Merge integration into pc/ttl-docs
Gajesh2007 06fee66
docs: shorter_hit holder removal
Gajesh2007 6057c68
Merge pc/epoch-rotation: per-file SSD removals keep the cache epoch
Gajesh2007 0c91025
Merge pc/ssd-ttl-raise: 30-minute SSD prefix-cache TTL (SEC-035 sign-…
Gajesh2007 42df951
Merge pc/ttl-docs: TTL sign-off record, per-file eviction and shorter…
Gajesh2007 01e39a6
Merge pc/mismatch-investigation: sidecar mirrors five provider prompt…
Gajesh2007 cf2ee42
fix(provider): expire files under a disowned store's root; drop the d…
Gajesh2007 d13ff3f
feat(prefix-cache): demand-targeted checkpoint retention and review f…
Gajesh2007 8f08f51
feat(registry): size the cache indexes for a 30-minute TTL and bound …
Gajesh2007 24093fe
fixup: read the tracker caps under its lock in stateCounts
Gajesh2007 26b2e53
Merge pc/epoch-cleanup: remove the dead maintenance gate, maintainer …
Gajesh2007 339757b
docs: maintainer ownership rule and retired donation outcomes
Gajesh2007 3e52833
perf(registry): index cache holders and attempts by provider
Gajesh2007 c3f6abf
feat(registry): observe cache demand on the 1,024-token checkpoint st…
Gajesh2007 c6fdd75
docs(registry): describe the 1,024-token demand stride in code comments
Gajesh2007 0dfde5d
test(registry): trim the race-time cost of the index sizing tests
Gajesh2007 00dd7b8
feat(prefix-cache): cap staged checkpoint windows across the slot
Gajesh2007 9904968
Merge pc/index-sizing: size cache indexes for a 30-minute window, exp…
Gajesh2007 f2c8a6e
fix(prefix-cache): align the donor byte budget with the slot-wide cap…
Gajesh2007 65a0013
feat(registry): sticky affinity, shallow demand ladder, demand index …
Gajesh2007 9f01887
test(promptsidecarloadproof): expect the 154-vector production corpus
Gajesh2007 329312f
Merge pc/index-sizing: sticky affinity rung, shallow ladder, demand i…
Gajesh2007 681ecd8
docs: index sizing, demand stride and observability
Gajesh2007 b62e01a
docs: demand index status fields and gauges
Gajesh2007 58f21b8
fix(prefix-cache): count checkpoints in publication against the slot cap
Gajesh2007 d20d399
Merge pc/checkpoint-geometry: 1,024-token historical checkpoints, dem…
Gajesh2007 32ae565
docs: historical checkpoint geometry, retention and staging cap; chan…
Gajesh2007 0233a79
registry: log the selection path on routing_decision
Gajesh2007 1a07849
Merge origin/master (preload selected models on every provider start,…
Gajesh2007 9156005
chore(libs): pin mlx-swift-lm to main c9bcdc3e (#168 squash of the ch…
Gajesh2007 2c140e7
fix(provider): keep the demand hint across an abandoned stage
Gajesh2007 680954f
chore(release): prepare v0.9.11 for the prefix-cache hit-rate set
Gajesh2007 6c8dda8
fix(provider): make checkpoint publish-to-index atomic against removals
Gajesh2007 3229885
docs(threat-model): reconcile the remaining 15-minute TTL statements …
Gajesh2007 31ea152
fix(registry): drop the retired tracker's demand index on reconfigure
Gajesh2007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/eigeninference/d-inference/coordinator/protocol" | ||
| ) | ||
|
|
||
| // A proof mismatch received over the provider WebSocket fences the capability | ||
| // for a bounded window: receipts inside it are rejected as capability_fenced, | ||
| // /v1/cache/status counts the window, and a valid proof after it is accepted. | ||
| func TestProofFenceLiftsOverProviderWire(t *testing.T) { | ||
| w := newCacheReceiptWire(t, cacheEligibilityV2Capability("model")) | ||
|
|
||
| w.lookup("mismatch", w.attempt("mismatch"), func(m *protocol.PrefixCacheLookupV2Message) { | ||
| m.PromptAnchor.ChainHash = strings.Repeat("d", 64) | ||
| }) | ||
| w.waitReceipt("lookup_v2", "rejected", "prompt_anchor_mismatch", 1) | ||
| w.waitStatus(func(s ExactCacheStatus) bool { | ||
| return s.Lifecycle.FencesApplied == 1 && s.Lifecycle.FencesExpired == 0 && | ||
| s.Lifecycle.FencedCapabilities == 1 | ||
| }) | ||
|
|
||
| w.lookup("fenced", w.attempt("fenced"), nil) | ||
| w.waitReceipt("lookup_v2", "rejected", "capability_fenced", 1) | ||
|
|
||
| w.advance(61 * time.Second) | ||
| w.lookup("accepted", w.attempt("accepted"), nil) | ||
| w.waitReceipt("lookup_v2", "accepted", "accepted", 1) | ||
| status := w.waitStatus(func(s ExactCacheStatus) bool { | ||
| return s.Lifecycle.FencesApplied == 1 && s.Lifecycle.FencesExpired == 1 && | ||
| s.Lifecycle.FencedCapabilities == 0 | ||
| }) | ||
| if status.Lifecycle.HolderRemoved["proof_mismatch"] != 0 { | ||
| t.Fatalf("mismatch with no holders reported removals: %+v", status.Lifecycle.HolderRemoved) | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "log/slog" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "nhooyr.io/websocket" | ||
|
|
||
| "github.com/eigeninference/d-inference/coordinator/protocol" | ||
| "github.com/eigeninference/d-inference/coordinator/registry" | ||
| "github.com/eigeninference/d-inference/coordinator/store" | ||
| ) | ||
|
|
||
| // cacheReceiptWire drives exact-cache receipts through a real provider | ||
| // WebSocket against an in-process coordinator. Plans come from the public | ||
| // planner with a synthetic local sidecar; only the clock is injected. | ||
| type cacheReceiptWire struct { | ||
| t *testing.T | ||
| ctx context.Context | ||
| reg *registry.Registry | ||
| srv *Server | ||
| url string | ||
| conn *websocket.Conn | ||
| capability protocol.PrefixCacheV2Capability | ||
| plan registry.CachePlan | ||
| provider *registry.Provider | ||
| offset atomic.Int64 | ||
| seq uint64 | ||
| } | ||
|
|
||
| func newCacheReceiptWire(t *testing.T, capability protocol.PrefixCacheV2Capability) *cacheReceiptWire { | ||
| t.Helper() | ||
| logger := slog.New(slog.DiscardHandler) | ||
| w := &cacheReceiptWire{t: t, reg: registry.New(logger), capability: capability} | ||
| w.srv = NewServer(w.reg, store.NewMemory(store.Config{}), ServerConfig{}, logger) | ||
| configureCachePreparationTest(t, w.reg) | ||
| base := time.Now() | ||
| w.reg.SetCacheRoutingClockForTest(func() time.Time { return base.Add(time.Duration(w.offset.Load())) }) | ||
| httpServer := httptest.NewServer(w.srv.Handler()) | ||
| t.Cleanup(httpServer.Close) | ||
| w.url = httpServer.URL | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| t.Cleanup(cancel) | ||
| w.ctx = ctx | ||
| conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(w.url, "http")+"/ws/provider", nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = conn.Close(websocket.StatusNormalClosure, "") }) | ||
| w.conn = conn | ||
| writeProviderJSON(t, ctx, conn, protocol.RegisterMessage{ | ||
| Type: protocol.TypeRegister, Backend: "mlx-swift", | ||
| Models: []protocol.ModelInfo{{ID: capability.ModelID, WeightHash: capability.ModelAggregateHash}}, | ||
| PrefixCacheProtocol: 2, PrefixCacheV2Models: []protocol.PrefixCacheV2Capability{capability}, | ||
| }) | ||
| w.plan = cachePreparationPlanForTest(t, w.reg, capability) | ||
| waitCacheCondition(t, func() bool { | ||
| ids := w.reg.ProviderIDs() | ||
| if len(ids) != 1 { | ||
| return false | ||
| } | ||
| w.provider = w.reg.GetProvider(ids[0]) | ||
| return w.provider != nil | ||
| }) | ||
| return w | ||
| } | ||
|
|
||
| func (w *cacheReceiptWire) advance(d time.Duration) { w.offset.Add(int64(d)) } | ||
|
|
||
| // prompt is the plan's final boundary: the anchor every lookup must prove. | ||
| func (w *cacheReceiptWire) prompt() protocol.PrefixCacheAnchor { | ||
| return w.plan.Boundaries[len(w.plan.Boundaries)-1] | ||
| } | ||
|
|
||
| // boundary returns the plan's anchor at the given number of 256-token blocks. | ||
| func (w *cacheReceiptWire) boundary(blocks int) protocol.PrefixCacheAnchor { | ||
| w.t.Helper() | ||
| for _, anchor := range w.plan.Boundaries { | ||
| if anchor.TokenCount == blocks*int(w.capability.BlockSize) { | ||
| return anchor | ||
| } | ||
| } | ||
| w.t.Fatalf("plan has no boundary at %d blocks", blocks) | ||
| return protocol.PrefixCacheAnchor{} | ||
| } | ||
|
|
||
| // attempt prepares a coordinator attempt and returns the nonce the provider | ||
| // would have received in its inference request. | ||
| func (w *cacheReceiptWire) attempt(id string) string { | ||
| w.t.Helper() | ||
| pr := ®istry.PendingRequest{RequestID: id, Model: w.capability.ModelID, CachePlan: w.plan} | ||
| if err := w.reg.PrepareCacheAttempt(pr, w.provider); err != nil { | ||
| w.t.Fatal(err) | ||
| } | ||
| nonce := providerInferenceWireMessage(id, "ephemeral", "ciphertext", pr).CacheReceiptNonce | ||
| if nonce == "" { | ||
| w.t.Fatalf("attempt %s did not prepare", id) | ||
| } | ||
| return nonce | ||
| } | ||
|
|
||
| func (w *cacheReceiptWire) lookup(id, nonce string, mutate func(*protocol.PrefixCacheLookupV2Message)) { | ||
| w.t.Helper() | ||
| w.seq++ | ||
| msg := protocol.PrefixCacheLookupV2Message{ | ||
| Type: protocol.TypePrefixCacheLookupV2, RequestID: id, CacheReceiptNonce: nonce, | ||
| ModelID: w.capability.ModelID, ModelAggregateHash: w.capability.ModelAggregateHash, | ||
| PromptContractID: w.capability.PromptContractID, CacheEpoch: w.capability.CacheEpoch, | ||
| CacheSeq: w.seq, PromptAnchor: w.prompt(), Outcome: "miss_absent", Tier: "ssd", StageMs: 1, | ||
| } | ||
| if mutate != nil { | ||
| mutate(&msg) | ||
| } | ||
| writeProviderJSON(w.t, w.ctx, w.conn, msg) | ||
| } | ||
|
|
||
| func (w *cacheReceiptWire) ready(id, nonce string, stageMs float64, anchors ...protocol.PrefixCacheAnchor) { | ||
| w.t.Helper() | ||
| w.seq++ | ||
| writeProviderJSON(w.t, w.ctx, w.conn, protocol.PrefixCacheReadyV2Message{ | ||
| Type: protocol.TypePrefixCacheReadyV2, RequestID: id, CacheReceiptNonce: nonce, | ||
| ModelID: w.capability.ModelID, ModelAggregateHash: w.capability.ModelAggregateHash, | ||
| PromptContractID: w.capability.PromptContractID, CacheEpoch: w.capability.CacheEpoch, | ||
| CacheSeq: w.seq, Outcome: "ready", Tier: "ssd", ReadyAnchors: anchors, | ||
| ExpectedPrefillTokensSaved: anchors[len(anchors)-1].TokenCount, StageMs: stageMs, | ||
| }) | ||
| } | ||
|
|
||
| func (w *cacheReceiptWire) receipts(kind, outcome, reason string) int64 { | ||
| return w.srv.Metrics().Snapshot().Counters[metricKey("exact_cache_receipt_total", []MetricLabel{ | ||
| {"type", kind}, {"outcome", outcome}, {"reason", reason}, | ||
| })] | ||
| } | ||
|
|
||
| func (w *cacheReceiptWire) waitReceipt(kind, outcome, reason string, count int64) { | ||
| w.t.Helper() | ||
| waitCacheCondition(w.t, func() bool { return w.receipts(kind, outcome, reason) == count }) | ||
| } | ||
|
|
||
| // waitStatus polls the public endpoint (cached for one second), so the | ||
| // projection itself is what a test asserts. | ||
| func (w *cacheReceiptWire) waitStatus(want func(ExactCacheStatus) bool) ExactCacheStatus { | ||
| w.t.Helper() | ||
| var status ExactCacheStatus | ||
| deadline := time.Now().Add(5 * time.Second) | ||
| for time.Now().Before(deadline) { | ||
| response, err := http.Get(w.url + "/v1/cache/status") | ||
| if err != nil { | ||
| w.t.Fatal(err) | ||
| } | ||
| status = ExactCacheStatus{} | ||
| err = json.NewDecoder(response.Body).Decode(&status) | ||
| _ = response.Body.Close() | ||
| if err != nil { | ||
| w.t.Fatal(err) | ||
| } | ||
| if want(status) { | ||
| return status | ||
| } | ||
| time.Sleep(20 * time.Millisecond) | ||
| } | ||
| w.t.Fatalf("cache status never reached the wanted state: holders=%d lifecycle=%+v", status.Holders, status.Lifecycle) | ||
| return status | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/eigeninference/d-inference/coordinator/protocol" | ||
| ) | ||
|
|
||
| // A provider that stops rotating its epoch on per-file eviction reports a | ||
| // removed deeper checkpoint only as a valid hit at a shallower one. Over the | ||
| // real provider WebSocket, that hit drops the deeper holder under its own | ||
| // removal reason, keeps the shallower one, and applies no fence. | ||
| func TestShorterHitDropsDeeperHolderOverProviderWire(t *testing.T) { | ||
| capability := cacheEligibilityV2Capability("model") | ||
| capability.ReadyBoundaryMode = protocol.PrefixCacheReadyBoundaryCheckpoint | ||
| w := newCacheReceiptWire(t, capability) | ||
| short, long := w.boundary(8), w.boundary(16) // 2,048 and 4,096 tokens | ||
|
|
||
| donor := w.attempt("donor") | ||
| w.lookup("donor", donor, nil) | ||
| w.waitReceipt("lookup_v2", "accepted", "accepted", 1) | ||
| w.ready("donor", donor, 100, short, long) | ||
| w.waitReceipt("ready_v2", "accepted", "accepted", 1) | ||
| w.waitStatus(func(s ExactCacheStatus) bool { return s.Holders == 2 }) | ||
|
|
||
| w.lookup("repeat", w.attempt("repeat"), func(m *protocol.PrefixCacheLookupV2Message) { | ||
| m.Outcome, m.MatchedAnchor = "hit", &short | ||
| m.ExpectedPrefillTokensSaved, m.StageMs = short.TokenCount, 50 | ||
| }) | ||
| w.waitReceipt("lookup_v2", "accepted", "accepted", 2) | ||
| status := w.waitStatus(func(s ExactCacheStatus) bool { | ||
| return s.Holders == 1 && s.Lifecycle.HolderRemoved["shorter_hit"] == 1 | ||
| }) | ||
| lifecycle := status.Lifecycle | ||
| if lifecycle.HolderRemoved["miss_invalidation"] != 0 || lifecycle.HolderRemoved["proof_mismatch"] != 0 { | ||
| t.Fatalf("shorter hit counted under another removal reason: %+v", lifecycle.HolderRemoved) | ||
| } | ||
| if lifecycle.FencesApplied != 0 || lifecycle.FencedCapabilities != 0 { | ||
| t.Fatalf("shorter hit fenced the capability: %+v", lifecycle) | ||
| } | ||
| if lifecycle.SSDHits != 1 { | ||
| t.Fatalf("ssd hits = %d, want the shorter hit counted once", lifecycle.SSDHits) | ||
| } | ||
|
|
||
| // The capability still proves receipts, and the deeper boundary is | ||
| // re-taught by the next durable publication. | ||
| again := w.attempt("again") | ||
| w.lookup("again", again, func(m *protocol.PrefixCacheLookupV2Message) { | ||
| m.Outcome, m.MatchedAnchor = "hit", &short | ||
| m.ExpectedPrefillTokensSaved, m.StageMs = short.TokenCount, 50 | ||
| }) | ||
| w.waitReceipt("lookup_v2", "accepted", "accepted", 3) | ||
| w.ready("again", again, 100, long) | ||
| w.waitReceipt("ready_v2", "accepted", "accepted", 2) | ||
| w.waitStatus(func(s ExactCacheStatus) bool { | ||
| return s.Holders == 2 && s.Lifecycle.HolderRemoved["shorter_hit"] == 1 | ||
| }) | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.