Skip to content

id: shared compiled-world cache — P1 of the #5965 compile-economics design - #8450

Merged
dezren39 merged 3 commits into
mainfrom
queue/14365-6235-compiled-world-cache
Nov 11, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/14365-6235-compiled-world-cache

Conversation

@dezren39

@dezren39 dezren39 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Closes #6235 — P1 of the #5965 compile-economics design (doc/2026-11-05_design_id5965.md §2, §4 tiers 0–1, §7 P1).

What lands

New module pkgs/id/src/world_cache.rs — the shared, server-side compiled-world cache (tier 1; the per-job scratch directory stays as tier 0 and as the compile isolation boundary):

  • Key (§2.1): BLAKE3 over platform tree hash, source closure hash, DLL closure hash, compiler_blake3, target. Seed and compile limits are not key components. No DLL linking exists yet, so the DLL closure is the reserved empty-closure constant, exactly as id: implement: shared compiled-world cache — P1 of the #5965 compile-economics design #6235 scopes it.
  • Scopes (§2.3): author and server; public stubbed at server behaviour until P3, as the issue allows.
  • Determinism gate (§2.2): cross-author hits on server/public entries require a determinism proof on record for the (compiler, platform) pair; without one, that pair's entries serve their author only. verify_determinism is the proof harness: the same spec compiled twice through the uncached path (fresh job directories each time) records a proof only when the artifacts are byte-identical.
  • No trust in labels (§2.1): a hit re-verifies the stored artifact's BLAKE3 against its content-hash name before serving; mismatch evicts the entry.
  • Eviction (§4): LRU over cache.lru_bytes (default 2 GiB), ordered by a monotonic use sequence. invalidate_compiler / invalidate_platform hard-evict matching entries and drop their determinism proofs — invalidation beats every tier. No pinning (tier 2 is P3).
  • Metering (§7 P1): logged only — measured compiler cost per job (0 on a hit) goes to the world_compile_cache tracing target. No charging, delay, or gating.

Wiring:

  • world_compile::compile_with_cache — a visible verified hit returns the artifact with provenance rebuilt from the entry (no compiler invocation); a miss compiles via the existing path unchanged and stores the artifact under the provenance's own digests. The pre-computed platform digest (digest_platform_tree) is pinned equal to the staged-copy digest provenance records by a dedicated test.
  • Compiler::with_cache; compile_and_install routes through the cache when one is attached. Attribution: every compile today is an admin compile, so entries are stored under the house author (server-admin) at author scope — unpublished source's default. Per-author keys arrive with the P3 gating matrix.
  • serve attaches the cache when ID_WORLD_COMPILE_CACHE_DIR is set (ID_WORLD_COMPILE_CACHE_LRU_BYTES overrides the 2 GiB default). Unset = behaviour exactly as before.

Unchanged: existing compile limits and admin-only gating; all current world_compile behaviour when no cache is configured.

Acceptance mapping (#6235)

  • Second compile of identical inputs (same author) served from cache with no compiler invocation, byte-identical artifact — end-to-end test (roc-gated, same skip pattern as the existing compile tests) + world_cache unit tests.
  • server-scope entry hits across authors only with a determinism proof on record — unit tests for both branches, plus the failed-proof case.
  • LRU respects the budget; compiler-digest / platform-tree invalidation evicts from all tiers present — unit tests.
  • Compile limits and admin-only gating unchanged; current world_compile tests pass — targeted run below.

Tests

cargo test --lib -- world_cache:: world_compile:: on the laptop: 44 passed, 0 failed (10 new world_cache tests, the platform-digest equivalence test, and all pre-existing world_compile tests). The end-to-end cache test skips when no built roc-world platform is present, matching the existing compile tests' pattern in this environment; the cache mechanics themselves are covered deterministically by the unit tests. cargo check --lib clean.

Queue task 14365 · lane code-g.


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

…esign (#6235)

Implements design doc/2026-11-05_design_id5965.md §2 + §4 tiers 0-1
(§7 phase P1) for pkgs/id world compiles:

- New world_cache module: content-addressed server-side cache keyed
  by BLAKE3 over (platform tree hash, source closure hash, DLL
  closure hash, compiler_blake3, target) per §2.1. Seed and compile
  limits are not key components; the DLL closure is the reserved
  empty-closure constant until the sibling DLL design lands.
- Sharing scopes author/server (public stubbed at server behaviour,
  as the P1 scope allows). Cross-author hits are gated on the §2.2
  determinism proof for the (compiler, platform) pair; without a
  proof a pair's entries serve their author only.
- Hits re-verify the artifact's content hash before serving; a
  mismatch evicts the entry. Tier-1 LRU eviction over
  cache.lru_bytes (default 2 GiB) ordered by a monotonic use
  sequence; invalidate_compiler/invalidate_platform hard-evict
  matching entries and drop their proofs (§4: invalidation beats
  every tier). No pinning (tier 2 is P3).
- compile_with_cache wraps the existing compile path: hit = no
  compiler invocation, same provenance rebuilt from the entry;
  miss = compile, then store under the provenance's own digests.
  Metering is logged only (§7 P1): measured compiler cost per job
  (0 on a hit) goes to the world_compile_cache tracing target.
- verify_determinism is the §2.2 proof harness: the same spec
  compiled twice through the uncached path (fresh job directories)
  records a proof only when the artifacts are byte-identical.
- Wiring: Compiler::with_cache; compile_and_install routes through
  the cache when one is attached, attributed to the house author
  (server-admin) at author scope — every compile is an admin compile
  until the P3 gating matrix introduces per-author keys. serve
  attaches the cache when ID_WORLD_COMPILE_CACHE_DIR is set
  (ID_WORLD_COMPILE_CACHE_LRU_BYTES overrides the budget).

Tests: world_cache unit tests cover key composition, DLL-closure
ordering, author-scope isolation, the determinism gate for
server/public scopes, hit re-verification + corrupt eviction, LRU
eviction order, oversize refusal, invalidation, and persistence
across reopen. world_compile gains a staged-vs-precomputed platform
digest equivalence test (the key must name the tree provenance
records) and a roc-gated end-to-end test: a second identical
compile (different seed) is served from cache byte-identically, a
changed source misses. Targeted run: 44 passed, 0 failed
(cargo test --lib -- world_cache:: world_compile::).

Queue task 14365, lane code-g.
@dezren39
dezren39 requested a review from a team as a code owner November 11, 2026 02:34
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T04:01:48.560727Z 1f36307 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.

@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 47 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fbdbbf2a-e5f6-40cf-aa0d-0deef764055c
Commits

Reviewing files that changed from the base of the PR and between 786ee93 and 1f36307.

Files selected for processing: 5

  • pkgs/id/src/commands/serve.rs
  • pkgs/id/src/lib.rs
  • pkgs/id/src/world_cache.rs
  • pkgs/id/src/world_compile.rs
  • pkgs/id/src/world_session/service.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@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: c2dea648d4

ℹ️ 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 +682 to +683
#[test]
fn key_changes_with_every_component_and_only_those() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add property/model tests for cache transitions

The new cache tests cover only handwritten examples, although this cache has meaningful state-machine invariants and an explicit design specification. This violates the root AGENTS.md testing doctrine requiring property-based and model-derived coverage where applicable. Add generated store/lookup/invalidate/reopen sequences checked against a reference model for scope visibility, proof revocation, and storage-budget enforcement; the existing examples miss replacement-artifact leaks and invalidation after eviction.

AGENTS.md reference: AGENTS.md:L41-L45

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 01b63b3 (head 1f36307 after merging current main). Added generated_sequences_match_the_reference_model: a proptest (24 cases, up to 48 ops) driving store/lookup/record-proof/invalidate-compiler/invalidate-platform/reopen sequences against a reference model of entries, proofs, LRU order and budget, asserting after every op that entry count, total bytes and proof visibility match the model, and that the artifact files on disk are exactly the distinct artifacts the model still references (the disk invariant that catches the replacement leak and invalidation-after-eviction cases the handwritten examples missed). Oracle: the reference model in the test. Full world_ suite after the main merge: 473 pass, 0 fail.

Comment thread pkgs/id/src/world_cache.rs Outdated
entry.last_used_seq = index.next_use_seq();
self.write_entry(&entry)?;
index.stats.stores += 1;
index.entries.insert(entry.key.clone(), entry.clone());

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 Reclaim the previous artifact when replacing an entry

When two cache misses compile the same inputs into different artifacts, this insertion replaces the previous entry without reclaiming its artifact file. This can happen with concurrent misses or different authors using author-scoped entries, since deterministic compiler output is explicitly not guaranteed. The old artifact becomes unindexed, so it is excluded from total_bytes() and never visited by subsequent eviction, allowing actual disk usage to grow beyond lru_bytes. Retain the replaced entry and remove its old artifact when no remaining entry references it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 01b63b3. store() now captures the entry its insert replaces and, after the eviction pass, reclaims the replaced artifact via drop_artifact_if_orphan when no remaining entry references its hash (the replacing entry's metadata file is not touched — it belongs to the new entry under the same key). Covered by replaced_entry_reclaims_its_orphaned_artifact and by the model test's on-disk invariant.

Comment thread pkgs/id/src/world_cache.rs Outdated
Comment on lines +579 to +582
.filter(|(compiler, platform)| {
removed.iter().any(|entry| {
entry.compiler_blake3 == *compiler || entry.platform_tree_hash == *platform
})

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 Revoke proofs independently of remaining cache entries

If LRU eviction has already removed the last entry for a compiler/platform pair, invalidating that compiler or platform leaves its determinism proof intact because this predicate searches only the entries removed by the current invalidation. The stale proof also persists across reopening, so a subsequently stored server-scoped entry immediately permits cross-author reuse without a fresh proof. Match proof tuples directly against the requested invalidated component rather than deriving revocation from resident entries.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 01b63b3. invalidate() now takes a proof predicate matched against the proof tuple itself (compiler equality for invalidate_compiler, platform equality for invalidate_platform) instead of deriving revocation from the entries removed by that call, so a proof whose pair's last entry was already LRU-evicted is still revoked — in the index and on disk, so it cannot survive reopening. Covered by invalidate_revokes_proofs_without_resident_entries.

Comment thread pkgs/id/src/world_cache.rs Outdated
}
let hash = artifact_hash(artifact);
let artifact_path = self.root.join("artifacts").join(&hash);
if !artifact_path.is_file() {

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 Repair existing corrupt artifacts when storing fresh output

If two distinct cache keys reference the same artifact and that file becomes corrupt, looking up one key evicts its entry but preserves the corrupt file because the other key still references it. After recompilation produces the correct bytes, this existence check skips rewriting the corrupt file and restores metadata pointing at it. Subsequent requests therefore keep recompiling without repairing the cache; alternating requests for the two keys also preserves the corruption. Verify and replace existing artifact files during storage, or remove the known-corrupt artifact and invalidate all its references when corruption is detected.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 01b63b3. store() now verify-and-repairs: an existing artifact file is trusted only when its bytes hash to its content-hash name; otherwise the fresh bytes are written (temp + rename) over it. A corrupt shared artifact that survived a lookup eviction because another key still referenced it is therefore repaired by the next store, and subsequent lookups of the other key hit. Covered by store_repairs_a_corrupt_artifact_shared_by_two_keys.

repo-worker-code-k added 2 commits November 10, 2026 21:53
…ion, corrupt repair, model tests (task 14365)
…iled-world-cache

# Conflicts:
#	pkgs/id/src/world_session/mod.rs
@dezren39
dezren39 merged commit 0cd4f4a into main Nov 11, 2026
1 of 8 checks passed
@dezren39
dezren39 deleted the queue/14365-6235-compiled-world-cache branch November 11, 2026 03:58

@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: 1f363073cf

ℹ️ 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 +544 to +546
};
for removed in &evictions {
self.remove_entry_files(removed, true);

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 Serialize eviction cleanup with concurrent stores

When concurrent compile requests store entries, eviction removes victims from the index under the mutex but deletes their files after releasing it. Another store can reinsert a victim key and write its new metadata before this cleanup runs; remove_entry_files then unconditionally deletes the replacement's metadata. The replacement still hits in memory, but disappears after reopening and leaves its artifact unindexed, outside subsequent LRU accounting. Keep file cleanup and replacement publication in the same serialized transaction so cleanup cannot delete a newly stored entry.

Useful? React with 👍 / 👎.

Comment on lines +293 to +297
Ok(Self {
root,
lru_bytes,
index: Mutex::new(index),
})

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 Enforce the configured budget when reopening the cache

If an operator lowers ID_WORLD_COMPILE_CACHE_LRU_BYTES and restarts the server, open loads every existing entry without applying the new budget. Eviction runs only after a successful store, so a workload consisting of cache hits—or misses whose artifacts exceed the reduced budget and return Oversize—can leave disk usage above the configured limit indefinitely. Apply LRU eviction to the restored index before returning the opened cache.

Useful? React with 👍 / 👎.

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.

id: implement: shared compiled-world cache — P1 of the #5965 compile-economics design

1 participant