feat: proof-carrying reuse cache — forge reuse + metrics + substrate stage (substrate-v2 P3)#28
Merged
CodeWithJuber merged 1 commit intoJul 7, 2026
Conversation
…stage (substrate-v2 P3) 'Reuse already-generated code' as a deterministic system (docs/plans/substrate-v2/03-reuse-cache.md): - src/reuse.js: normalized task fingerprints (volatile literals become typed placeholders so the same task worded across teammates lands in one neighborhood), MinHash + 16x8 LSH banding for near-match, and the exact -> near -> adapt -> miss lookup ladder - proof-carrying serving: an artifact serves only with val >= 0.6 (a fresh unverified mint sits at the 0.5 prior and does NOT serve) and only while every declared dep still resolves in the atlas; failed revalidation appends a graph.reval contradiction — stale code demotes itself for the whole team via the shared ledger - forge reuse query|mint|stats CLI; mint extracts a verifiable pointer (path + content sha256), exports, and relative-import deps from the file - substrate gains a reuse stage: the explicit gate meters + writes evidence, the ambient hook path stays read-only (reusePeek) - src/metrics.js: stage-tagged .forge/metrics.jsonl — the measurement backbone the cost model's savings arithmetic (P8) runs on - reuse-first skill now calls the cache before advising a repo search Honest limit (per spec): MinHash near-match is weak on very short specs (<~15 tokens); realistic-length specs near-hit as tested. The optional embeddings backend (ADR-0005) is the planned lift. 288/288 tests (14 new), biome + tsc clean, full loop smoke-tested live. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc2MvWJbQ1cyNQ76ymv4hs
CodeWithJuber
marked this pull request as ready for review
July 7, 2026 16:50
11 tasks
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.
What & why
Phase P3 of the substrate-v2 plan (#24; P1 = #25/#26, P2 = #27): "reuse already-generated code" becomes a deterministic system, the largest single lever toward the ~90 % cost target. Spec:
docs/plans/substrate-v2/03-reuse-cache.md.src/reuse.js— verified code becomes anartifactclaim keyed by a normalized task fingerprint: volatile literals become typed placeholders (listUsers→⟨ident⟩,25→⟨num⟩, paths →⟨path⟩, quoted strings →⟨str⟩) so the same task worded by different teammates lands in one near-neighborhood. Near-match via MinHash + 16×8 LSH banding (collision probability ≈ 0.96 at J=0.8 vs ≈ 0.17 at J=0.5 — a sharp cliff exactly at the near threshold, so big ledgers skip the all-pairs scan).exact(same normalized spec + same graph-slice context) →near(J ≥ 0.8, serve-with-diff) →adapt(J ≥ 0.6, inject as a verified starting point, generate only the delta) →miss.graph.revalcontradiction to the shared ledger — stale code demotes itself, for everyone, automatically. Serving appends a confirmation, keeping proofs fresh.forge reuse query | mint | stats— mint extracts a verifiable pointer (path + content sha256), exports, and relative-import deps from the file; it's honest when unverified ("serving: NOT YET — no evidence").forge substratereuse stage — the explicit gate runs the metered query (evidence + metrics); the ambient hook path uses a read-only peek, so per-prompt hooks never write. A hit renders as: "reuse: EXACT hit — verified module at src/limit.js — start from it, don't regenerate."src/metrics.js— the stage-tagged.forge/metrics.jsonl(cost-model spec05): every cache lookup logshit_exact|hit_near|hit_adapt|miss+ a saved-tokens estimate, so P8's savings report is arithmetic on measured events, never an assertion.reuse-firstskill — now instructs agents to ask the cache first (guard-over-prose: the prose layer sits on a deterministic lookup).Honest limit (stated in the spec and kept): MinHash near-match is weak on very short specs (< ~15 tokens — one changed word swings the Jaccard below threshold); realistic-length specs near-hit reliably (tested at J≈0.83/0.64 for the near/adapt tiers). The optional embeddings backend (ADR-0005) is the planned lift.
Checklist
npm testpasses (288/288 — 14 new: normalization placeholders, fingerprint slice-sensitivity, LSH determinism, all four ladder tiers, the proof floor, revalidation demotion, mint→hit→demote store roundtrip, metrics)npm run checkpasses on changed filesfeat:)CHANGELOG.mdupdated under## [Unreleased]03-reuse-cache.md§1–§4Risk & rollback
Extra checks (tick if applicable)
npm run typecheckpasses🤖 Generated with Claude Code
https://claude.ai/code/session_01Fc2MvWJbQ1cyNQ76ymv4hs
Generated by Claude Code