Skip to content

Link incrementally, stop opening unchanged files, and store edges compactly (#88) - #144

Merged
QuintinBotes merged 8 commits into
mainfrom
goal/88-incremental-link-compact-store
Nov 26, 2026
Merged

QuintinBotes merged 8 commits into
mainfrom
goal/88-incremental-link-compact-store

Conversation

@QuintinBotes

Copy link
Copy Markdown
Owner

Summary

The rest of #88 after #139 and #140: incremental linking keyed by changed files and the symbols they declare (FR-001), a census that does not open unchanged files, and compact edges with an ordered, breaking store migration (FR-003). On a 30.8k-file C# monolith a one-file re-map goes from 147 s to 25 to 31 s, and a no-change re-map from 4.7 s to 0.6 s. Every incremental result is compared row by row with a cold map (FR-002). Two of the three synthetic gate ratios are still over their bounds; the numbers are below.

Closes #88

Design

Census. A map stat'ed every file and read the first 4 KB of most of them. Now the entries of tracked files that git sees as unchanged are kept in file_index (census:entry, one row per path, replaced in place). Each entry is valid for the rules it was classified under: globs, test projects, size limit and release. A file with the same blob is neither stat'ed nor opened. The summary reports census.cached.

Incremental linking. The builder gives every linker incremental:

  • the paths whose facts differ from the ones its saved state was built from (re-extracted, added, deleted or reclassified)
  • the data it saved last time, and the facts a changed path held before
  • per-file rows (<id>:link, deflated), written only when the link succeeds

When there is no usable state (first map, a different adapter set, options or configuration), the linker links in full.

  • generic: each file's link result (edges, dependency calls, resolved targets, qualifiers, build rejections) is kept with the keys it looked up. Keys are module paths, type names, packages, directories, C# names and namespaces, and member reach. A file is linked again when it changed, or when a changed file's answer to one of its keys is different now (key added, removed or changed). Every other file replays its kept result. Some changes no key covers, so they still link everything: manifests, the set of types or bases a file declares, friend assemblies, and a SCIP index (a shortcut: comment marks this).
  • quality: a module's clones depend only on the modules it shares a fingerprint hash with, and on the length of those hashes' posting lists. The postings are the reverse index. Only modules that share a hash with a changed file, now or before, are paired again. A posting list over MAX_POSTINGS both before and after the change pairs nobody, so it marks no one.
  • ownership: CODEOWNERS rules are compiled once, answered once per path, and the answers are kept while the rules do not change. This was most of the 147 s on the monolith: the linker recompiled every rule for every node.
  • The build graph's ownerOf is memoised.

Compact edges. edges is a WITHOUT ROWID table keyed by (src, type, dst). id is a virtual generated column, so SELECT * ... ORDER BY id and WHERE id = ? read the same rows and ids as before, and the separate src index is gone. Migration 3 (compact-edges) copies the existing rows, so no re-map is needed. It is breaks: true: older releases write the id column, so they refuse a schema-3 store. Their hooks still allow read-only commands, and the hooks-seen guard holds the migration back while an older session is running.

Rejected.

  • A per-adapter link cache keyed on the whole input: any edit invalidates it.
  • Rebuilding kept link facts from the facts table instead of keeping them: it cannot tell per-file loop output from global output, and fact order matters for fact_ids.
  • Incremental projection and derived facts: they still run over the whole repository. That is what keeps scenario 2 over its bound (see Follow-up).

Scenario and requirement map

Status Evidence
1. No change ≤ 10% of cold (50k) Met: 0.014 (main 0.054) bench.mjs --sizes 50k --gate, table below
2. 10 changed ≤ 2× no change (50k) Not met: 11.6 (first run 16.5; main 4.46) See below. The census cache made the no-change re-map 3 to 4× faster, so the ratio went up, while the 10-file re-map still reads every file's cached facts (2.1 s) and runs projection (2.9 s) and derived facts (2.9 s) over the whole repository
3. Same graph as a cold map Met tests/unit/graph/incremental-link.test.mjs: nodes, edges, facts and derived facts equal a cold map after each of: a body edit, a body edit in a two-type file, a member read from another file, a change to a widely used type, a symbol rename, a broken clone, a manifest change, and files deleted and added (C#, Java, Go, JS). tests/unit/adapters/quality/incremental.test.mjs: 25 random trees × 4 rounds of edits, deletions and additions, with posting lists around the bound. delta.test.mjs and remap-cost.test.mjs pass unchanged
4. Store ≤ 3× tree (50k) Not met: 50.0 (main 53.0) The fixture files are a few hundred bytes each. On the C# monolith the store went from 3.5 GiB to 3.1 GiB (about 8.4× its 0.37 GiB tree)
5. Previous release's store migrates Met node scripts/upgrade-test.mjs --count 5: 5/5 (v0.3.0 to v0.4.0, schema 1 → 3, the old hooks still allow echo hi and git status). New migration test in tests/unit/state/migrations.test.mjs
FR-001 incremental linking Met for generic, quality and ownership The summary's link reports {relinked, modules}. Monolith hub edit: generic linked 1 of 28,990 files again
FR-002 Met As scenario 3. Mutation checks: the test fails when reuse ignores dirty keys, when quality drops the reverse-index lookups, when quality drops the before-size check, and when the declaration shape is compared without canonical key order
FR-003 compact edges Met Migration 3, EDGES_DDL
FR-004 gate Unchanged (from #140) bench.test.mjs

Measurements

Apple M5 Max, Node 26.13. The machine was shared, with load average 2 to 16, so times are upper bounds.

MewsSystems/mews (30.8k files, lexical C#, 210,713 nodes, 1,085,342 edges), unknot map from the CLI. The main column is the run reported on #88.

Run main this branch
Cold map 163 s 128 s
Re-map, no change 4.7 s 0.6 s (2.4 s the first time after the cold map)
One-file edit, hub Enterprise.cs (comment appended, committed) 147 s 31 s, then 25 s
One-file edit, a leaf n/a 25 s
Store (du .unknot) 3.5 GiB 3.1 GiB

bench.mjs --sizes 50k --gate:

Run Cold No change 10 changed Store No change / cold Changed / no change Store / tree
main 49.8 s 2.7 s 12.0 s 1,238 MB 0.054 4.46 53.0
branch (before the posting-list fix) 47.1 s 0.65 s 10.7 s 1,225 MB 0.014 16.5 52.5
branch (final, busier machine) 78.0 s 1.1 s 12.2 s 1,168 MB 0.014 11.6 50.0

Testing

  • node --test tests/unit/scripts/bench.test.mjs tests/unit/graph/delta.test.mjs: 3/3 pass
  • Full npm test: 3,670 tests, 3,654 passed, 9 skipped, 7 failed. All 7 failures are in tests/unit/daemon/remote.test.mjs with "CA signature digest algorithm too weak". They fail the same way on main with the local Node 26 / OpenSSL and are unrelated to this change
  • node scripts/upgrade-test.mjs --count 5: 5/5
  • New tests: tests/unit/graph/incremental-link.test.mjs, tests/unit/graph/census-cache.test.mjs, tests/unit/adapters/quality/incremental.test.mjs, and the compact-edges case in tests/unit/state/migrations.test.mjs

Two existing assertions in migrations.test.mjs changed. They said "no applied migration breaks older releases" (schema_version '1'). A breaking migration makes that false by design, so they now expect '3'. The second also checks that facts-digest alone stays at compat level 1, so its original point still holds.

Migration notes

The first open by this release rebuilds the edges table, which takes a few seconds on a large store. Releases before this one cannot write the store afterwards. Each linker links in full once, then incrementally. This is in the CHANGELOG migration notes and COMPATIBILITY.md.

Follow-up

  • Scenario 2 needs incremental projection (hash and write only the facts of affected files) and incremental derived facts. On the monolith these cost 4.8 s and 4.5 s per re-map, and reading every file's cached facts costs 3.2 s.
  • The JavaScript and Python linkers are not incremental yet. Lift the SCIP shortcut in generic by keying the index's verdicts per file.

🤖 Generated with Claude Code

Quintin Botes and others added 8 commits November 26, 2026 20:47
…ration (#88, FR-003)

The edges table kept a text id that repeats type, src and dst, a primary-key
index on it and a src index beside the dst index. It is now a WITHOUT ROWID
table keyed by (src, type, dst) whose id is a virtual generated column, so
readers (`SELECT * ... ORDER BY id`, `WHERE id = ?`) see the same rows and ids.
Migration 3, `compact-edges`, copies existing rows into the new table; it is
`breaks: true` because older releases write the id column, so schema_version
becomes 3 and the hooks-seen guard holds it while an older session runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…open them (#88)

The census stat'ed every file and read the first 4 KB of most of them on every
map, the floor every re-map paid. Entries of tracked files git sees unchanged
are now kept in file_index (`census:entry`, one row per path, replaced in
place) for the rules they were worked out under (globs, test projects, size
limit, release); a file with the same blob is neither stat'ed nor opened. The
map summary reports `census.cached`. graphPair skips every builder cache row
(an adapter id with a colon), not only `:uses` ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…re (#88, FR-001)

The builder gives each linker `incremental`: the paths whose facts differ from
those its saved state was computed from, what it saved, the facts a changed
path held before, and per-file rows (file_index `<id>:link`) written only when
the link succeeds. With no usable state (first map, another adapter set,
options or configuration) a linker links in full.

- generic: each file's link result is kept with the keys it looked up (module
  paths, type names, packages, directories, C# names and namespaces, member
  reach). A file is linked again when it changed or a changed file's answer to
  one of its keys differs now from before; others replay their kept result.
  Manifests, type sets and bases, friend assemblies and a SCIP index still link
  everything (shortcut noted in code).
- quality: clones depend only on modules sharing a fingerprint hash; the
  postings are the reverse index, and modules sharing no hash with a changed
  file, now or before, keep their clones.

The summary's `link` says how many modules each linker linked again. The new
test compares every stored row with a cold map after a body edit, a member read
from another file, a change to a widely used type, a symbol rename, a broken
clone, a manifest change, and files deleted and added (C#, Java, Go, JS).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a 30k-file C# repository the ownership link compiled every CODEOWNERS
rule's globs again for every ownable node (minutes of a one-file re-map). Rules
are now compiled once, answered once per path, and the answers are kept for the
next map while the rules are the same. The build graph's `ownerOf` is memoised.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A hash seen in more than MAX_POSTINGS files pairs nobody. A changed file in
such a list, over the bound before and after the change, no longer marks the
list's other files as affected; on the synthetic tiers, where boilerplate
hashes are shared by almost every file, that was nearly every module. A
randomised test compares the incremental link with a full one across edits,
deletions and additions with lists around the bound (it fails when the
before-size check is dropped).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The generic linker's kept per-file results (the keys each file looked up and
the edges it produced) were 1.08 GB on a 30.8k-file C# repository, more than
its extracted facts. The rows are now deflated (what files look up repeats
across files): the store after a cold map is 3.1 GiB there, below the 3.5 GiB
before this series. An unreadable row only means that file is linked again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CHANGELOG (Unreleased, Changed and Migration notes), COMPATIBILITY (a store
migration older releases cannot live with), and docs/benchmarks.md with the
50k gate before and after and a 30.8k-file C# monolith (cold, no change,
one-file edits, store size).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@QuintinBotes
QuintinBotes merged commit 2fd9031 into main Nov 26, 2026
12 checks passed
@QuintinBotes
QuintinBotes deleted the goal/88-incremental-link-compact-store branch November 26, 2026 20:58
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.

Make a re-map cost proportional to the change, with a compact store

1 participant