Skip to content

coordinator: persist the cache routing holder and demand indexes across restarts - #1240

Merged
Gajesh2007 merged 91 commits into
masterfrom
feat/cache-routing-persistence
Sep 29, 2026
Merged

Gajesh2007 merged 91 commits into
masterfrom
feat/cache-routing-persistence

Conversation

@Gajesh2007

@Gajesh2007 Gajesh2007 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Keep a write-behind copy of the exact prefix-cache holder and observed-demand indexes across coordinator restarts. Routing continues to read and update its in-memory indexes; Postgres IO stays outside registry locks and off the request path.

  • Persist SSD holders, demand observations, and the cache-key-generation fingerprint. Memory-tier holders and raw chain hashes are never persisted; boundary identifiers are HMAC outputs.
  • Restore within the current TTL and index caps. Holders park by cache epoch and model, then bind through ordinary holder insertion only when the live provider's capability identity matches. Disconnects park evidence; other invalidations remove it durably.
  • Keep revisioned mutations pending until acknowledged. A bounded snapshot does not remove work; successful chunks acknowledge only matching revisions. A failed write requires no drain/requeue repair, and an old acknowledgement cannot erase a newer mutation.
  • Retain pending deletes throughout IO. On delete-backlog overflow, replace the holder backlog, keep a conservative timestamp cutoff that rejects delayed older evidence, and reset the durable copy before continuing. A durable reset marker supports recovery from an interrupted reset.
  • Record validated misses and shorter-hit invalidations even before restore has populated live holders. If the newest shared-session evidence is lost, delete its durable row rather than let an older survivor's monotonic upsert preserve invalidated data. Older valid in-memory holders remain usable; a fresh receipt can persist evidence again.
  • Quiesce and join provider sockets and the periodic writer before the final bounded flush.

Code Organization

coordinator/registry/cachepersist/ separates state/construction (persister.go), intake (marks.go), snapshots and acknowledgements (mutations.go), writing (flush.go), overflow (reset.go), restore, parked holders, delete fences, indexed heaps, maintenance, and status. Tests are grouped by those responsibilities.

coordinator/registry/cache_persistence{,_registry}.go owns registry integration and lifecycle wiring. coordinator/store/cacheroutingstate/ defines records and the store contract; memory and Postgres implementations live in the store package.

Before

flowchart TD
  Receipt["Provider receipt"] --> Upsert["upsertHolderLocked"]
  Observe["observeCacheDemand"] --> Demand["In-memory demand index"]
  Upsert --> Holders["In-memory holder index"]
  Holders --> Route["Requests receive cache-aware routing"]
  Restart["Coordinator restart"] --> Empty["Both indexes start empty"]
  Empty --> Relearn["Wait for demand and provider receipts"]
  Relearn --> Cold["Reduced hit rate during rebuild"]
  classDef code fill:#dbeafe,stroke:#1d4ed8,color:#172554
  classDef memory fill:#dcfce7,stroke:#15803d,color:#14532d
  classDef loss fill:#fee2e2,stroke:#b91c1c,color:#7f1d1d
  class Receipt,Upsert,Observe,Route code
  class Demand,Holders memory
  class Restart,Empty,Relearn,Cold loss
Loading

Blue: execution paths. Green: serving state. Red: lost state and recovery cost.

After

flowchart TD
  Events["Receipts, demand, invalidations"] --> Hot["Update in-memory indexes"]
  Hot --> Route["Requests keep routing from memory"]
  Hot --> Mark["Mark revisioned pending mutation"]
  Mark --> Snapshot["Flush: bounded snapshot, no removal"]
  Snapshot --> DB[("Postgres durable copy")]
  DB --> Ack{"Write acknowledged?"}
  Ack -->|"yes"| Clear["Clear only matching revisions"]
  Ack -->|"no"| Keep["Leave changes pending for retry"]
  Overflow["Delete backlog overflow"] --> Fence["Retain cutoff and reset durable copy"]
  Fence --> DB
  Boot["Restart: Restore"] --> Load["Check generation, TTL and caps"]
  DB --> Load
  Load --> Bind["Seed demand; park and bind matching holders"]
  Bind --> Hot
  classDef code fill:#dbeafe,stroke:#1d4ed8,color:#172554
  classDef memory fill:#dcfce7,stroke:#15803d,color:#14532d
  classDef durable fill:#ede9fe,stroke:#6d28d9,color:#4c1d95
  classDef safety fill:#fef3c7,stroke:#b45309,color:#78350f
  class Events,Route,Snapshot,Boot,Load,Bind code
  class Hot,Mark,Clear,Keep memory
  class DB durable
  class Ack,Overflow,Fence safety
Loading

Purple: durable state. Yellow: acknowledgement and invalidation safeguards. All store calls run outside registry locks.

Validation

  • Full coordinator tests against a disposable local PostgreSQL 16 database: make coordinator-test.
  • Persistence race coverage: go test -race ./coordinator/registry/cachepersist -count=3.
  • Cache integration and shutdown race coverage: go test -race ./coordinator/registry ./coordinator/api -run 'Test(Cache|CloseProvider|WaitProvider|ProviderShutdown)' -count=1.
  • make coordinator-build and go vet ./coordinator/registry/... ./coordinator/store/... ./coordinator/api/....
  • make docs-impact-check BASE=origin/master, make docs-check, and git diff --check.

Regression coverage includes stale acknowledgements, partial writes, failed deletes under retention pressure, overflow and delayed evidence, missed/shorter-hit boundaries before restore, and invalidating the newest shared session with queued and already-flushed rows. Store contracts and restart behavior run against memory and Postgres.

Interfaces and Rollout

  • Coordinator-only. No provider or protocol change. The original persistence feature creates cache_routing_holders, cache_routing_demand, and cache_routing_meta; the acknowledgement refactor adds no further schema changes.
  • EIGENINFERENCE_CACHE_ROUTING_PERSIST defaults on. Set it to false to retain the original empty-index-after-restart behavior.
  • GET /v1/cache/status exposes lifecycle.persistence restore, pending-holder, binding, flush, overflow, and stale-upsert counters. rows_deleted counts successfully submitted delete keys, including missing rows, not actual deleted rows.
  • A reset cutoff is conservative: it can discard unrelated old persistence hints, but does not remove valid live routing indexes. Crashes before a reset marker is written remain a recovery limitation.
  • Writer serialization and cutoff fencing are process-local. Use a non-overlapping restart for a cache-key-generation change; concurrent coordinator writers can repopulate rows during a reset. Shutdown flushes remain bounded and best-effort if the store is unavailable.

Canonical behavior and operations are documented in docs/architecture/cache-aware-routing.md, docs/operations/cache-routing-rollout.md, docs/reference/api-contracts.md, and the existing storage/configuration/deploy references.

@vercel

vercel Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference Ready Ready Preview Sep 29, 2026 11:13pm UTC
d-inference-console-ui-dev Ready Ready Preview Sep 29, 2026 11:13pm UTC
d-inference-landing Ready Ready Preview Sep 29, 2026 11:13pm UTC
eigen-homepages-darkbloom Ready Ready Preview Sep 29, 2026 11:13pm UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T23:20:45.434558Z befa667 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b4d65914b4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coordinator/cmd/coordinator/main.go Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2fea54f1c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4cd59d6189

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 01ac365c4c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coordinator/registry/cache_snapshot.go Resolved
…and re-apply the changelog section and configuration rows after the rebase)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 048c4ddb51

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…store premise

Findings of the independent review of round 28:

- The dirty-cap comments (the field, Options.MaxPending, requeue) say
  what happens to a delete past the cap (a reset, never a drop; a
  requeued delete is kept) instead of claiming every mark is dropped.
- The restore's "nothing parked is condemned" argument states its
  premise: nothing drains before the copy is established, so no decision
  has left the pending set or been evicted from retention in flight.
- An abort into a reset counts the rows merged before it as restored
  (they stay parked); a failed reset records its duration like any failed
  flush; a test covers the overflow that lands during an empty load,
  which only the post-loop check catches.
…and re-apply the changelog section and configuration rows after the rebase)
…fore an overflow

Codex round 29:

- ResetCacheRoutingState records an in-progress marker as the key
  generation before it clears the tables (both stores), and records the
  fingerprint only afterwards, so a same-generation reset interrupted
  between its batched deletes (a crash, the shutdown flush deadline) is
  completed by the next boot instead of read as complete and its
  residual rows restored. Restore treats the marker as an interrupted
  reset, not a key rotation.
- A batch drained before a backlog overflow does not requeue its upserts
  after it: one may be evidence a released decision condemned (its
  queued copy was removed at decision time, the drained copy was already
  in flight), and the reset that follows would write it back. Batches
  carry the overflow count they were drained under; requeue drops the
  upserts of an older batch (counted in dropped_dirty) and keeps its
  deletes and demand.

Tests: a restore against a store left with the marker completes the
reset without counting a rotation; an upsert batch that fails while the
row is invalidated and the backlog overflows is not requeued and the row
is not written back after the reset.
…and re-apply the changelog section and configuration rows after the rebase)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e06fdb2b88

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

… was drained under

Findings of the independent review of round 29:

- Flush rebuilt the batch it requeued without the overflow count it was
  drained under, so after the first overflow of a process every failed
  flush dropped all of its upserts. The requeued batch carries the count;
  a test fails an upsert batch after a handled overflow and asserts it is
  requeued in full.
- A Postgres test interrupts a reset right after its marker (a test-only
  hook on the store) and asserts the marker is the recorded generation
  and the rows are untouched, then that the retry completes the reset.
- The interrupted-reset log no longer attributes the attempt to the
  previous run (a retried restore's own earlier attempt counts); the
  key_rotated row says a completed interrupted reset does not count; the
  rollout page states the single-writer assumption for an overflow reset.
…and re-apply the changelog section and configuration rows after the rebase)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1985e86d92

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…sh loop on overflow

Codex round 31: a backlog overflow between a flush's reset check and its
drain let the flush write its batch with the reset still pending, and a
crash before the next flush left the fingerprint complete over rows the
released decisions had condemned. The check and the drain now share one
lock hold (drainUnlessReset): a drain never proceeds while a reset is
pending, and a flush that finds the flag set at drain time resets first
and then drains. An overflow also wakes the flush loop (Wake), so the
reset's marker lands at the next flush rather than the next tick; the
window between the overflow and the marker is the residual, documented
as such.

Also, from the independent review of the round-29 follow-up: the rollout
note names the exposure of an overlapping overflow reset, and the
Postgres marker test checks load errors.
…and re-apply the changelog section and configuration rows after the rebase)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 344c42a1f2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…fter the drain

Codex round 32: a drained batch was still written after an overflow
that landed between the drain and its first store call, so upserts a
released decision condemned could reach the store ahead of the reset.
The flush now re-checks the overflow count before every upsert chunk;
when the backlog overflowed since the drain it stops writing, requeues
the remainder (whose upserts the fence drops) and runs the reset in the
same flush, so the marker lands before anything else is written. The
window between that check and the store call is the same residual as
between an overflow and the marker.

Also, from the independent review of round 31: tests now guard the
flush's reset after a refused drain (an overflow injected between the
check and the drain through a test-only hook) and the loop's wake path
(a registry-level overflow whose reset must land inside the flush
tick), and the wake-up's practical bound is stated in the architecture
page and the changelog.
…and re-apply the changelog section and configuration rows after the rebase)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 756909f2c3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

… overflow

Codex round 33 (and the independent review of round 32): the mid-flush
fence ran only before upsert chunks, so a delete-only or demand-only
batch spent its store calls with a reset pending only in memory. The
fence now runs before every chunk of every kind: the flush stops at its
next store call, requeues the remainder (upserts dropped, deletes and
demand kept) and resets in the same flush, so the marker lands as early
as the next chunk boundary.

Also from that review: a reset that fails on the mid-flush path is
counted once against its flush (not twice) and retried by the next
flush, which then writes the requeued deletes; a reset's duration counts
in the flush timing; the wake test queues the condemned row's decision
before the fillers so the overflow releases it; the after-check test
asserts a real fingerprint.
…and re-apply the changelog section and configuration rows after the rebase)
…a reset-only flush

Findings of the independent review of round 33:

- On the mid-flush path the flush was stamped before its reset ran, so
  the reset's duration was excluded from last_flush_ms and last_flush_at
  read as done while the reset was in flight. The stamp now follows the
  reset attempt, with its outcome.
- A flush whose reset left nothing to drain behind it bumped no counter,
  so a reset-only flush was invisible in flushes and last_flush_at. It is
  counted now (resetIfPending reports whether it reset).
…and re-apply the changelog section and configuration rows after the rebase)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 26b674d979

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@blacksmith-sh

blacksmith-sh Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
github.com/eigeninference/d-inference/coordinator/api/
TestFirstByteReachesClientWhileRegistryWriteLockHeld
View Logs

Fix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

…r the reset

Findings of the independent review of the round-33 follow-up: the
restamp after a mid-flush reset had no failing test (both stamps fell in
the same second), and the mid-flush path still stamped once before its
reset, so a status read during a slow reset showed the pre-reset stamp.
The path now stamps only after the reset attempt; the failed-reset test
makes the reset slow and asserts its duration lands in last_flush_ms;
the flush-health row says a reset-only flush counts and a mid-flush
reset's duration counts.
…and re-apply the changelog section and configuration rows after the rebase)

@jonathan308 jonathan308 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed commit befa667. No blocking findings from this review.

Checked revision-scoped acknowledgements and pending-delete retention, reset/restore fencing and TTL/cap handling, capability-bound reattachment, shared-session invalidation, HMAC-only persisted boundary identity, and provider-socket/periodic-writer shutdown ordering. The earlier automated review threads are resolved on this head.

Independent local Go 1.25 race checks: 47 top-level tests passed in cachepersist/cacheroutingstate; 178 top-level cache integration/store/shutdown tests passed. Zero failures. PostgreSQL-dependent coverage was not executed locally: two top-level skips and three additional subtest skips with DATABASE_URL deliberately unset. Full PostgreSQL/CI results remain separate; the current Coordinator, Provider and E2E jobs were still running when this review was submitted.

Approval is for this reviewed code, not a declaration that all CI/release gates have passed, and does not perform a merge or deployment.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: befa667037

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +115 to +119
tier = CASE WHEN EXCLUDED.updated_at >= cache_routing_holders.updated_at THEN EXCLUDED.tier ELSE cache_routing_holders.tier END,
model_id = CASE WHEN EXCLUDED.updated_at >= cache_routing_holders.updated_at THEN EXCLUDED.model_id ELSE cache_routing_holders.model_id END,
model_aggregate_hash = CASE WHEN EXCLUDED.updated_at >= cache_routing_holders.updated_at THEN EXCLUDED.model_aggregate_hash ELSE cache_routing_holders.model_aggregate_hash END,
prompt_contract_id = CASE WHEN EXCLUDED.updated_at >= cache_routing_holders.updated_at THEN EXCLUDED.prompt_contract_id ELSE cache_routing_holders.prompt_contract_id END,
block_hash_version = CASE WHEN EXCLUDED.updated_at >= cache_routing_holders.updated_at THEN EXCLUDED.block_hash_version ELSE cache_routing_holders.block_hash_version END,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Normalize tolerated-skew rows before conflict updates

When the previous coordinator wrote a holder up to FutureSkew ahead, LoadCacheHolders clamps only the returned copy while leaving the durable updated_at in the future. Until the local clock catches up, a fresh receipt reaches this conflict clause with an older timestamp, so these CASE expressions retain the old capability fields and measurement instead of the newly proved values. This is observable when a provider changes ready_boundary_mode without rotating its epoch: a restart restores the old identity, rejects it as a mismatch, and loses the valid new evidence. Normalize tolerated future timestamps in storage or let a current receipt supersede a row within the skew window.

Useful? React with 👍 / 👎.

This branch was successfully deployed

4 active deployments
Preview – d-inference — befa6670 Deployed Sep 29, 2026 by vercel[bot]
Preview – d-inference-console-ui-dev — befa6670 Deployed Sep 29, 2026 by vercel[bot]
Preview – eigen-homepages-darkbloom — befa6670 Deployed Sep 29, 2026 by vercel[bot]
Preview – d-inference-landing — befa6670 Deployed Sep 29, 2026 by vercel[bot]
benchmarks — befa6670 Deployed Sep 29, 2026 by Gajesh2007 via E2E Benchmarks #1920
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.

2 participants