Skip to content

Notes 2/5: add n-gram and suffix-array index - #1240

Closed
ThinkOffApp wants to merge 14 commits into
ashhart:mainfrom
ThinkOffApp:notes-pr2-index
Closed

ThinkOffApp wants to merge 14 commits into
ashhart:mainfrom
ThinkOffApp:notes-pr2-index

Conversation

@ThinkOffApp

@ThinkOffApp ThinkOffApp commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Adds the LLWZ n-gram index on top of the journaled store: posting lists, or a suffix array over the notes' folded keys (--notes-index sa, the default; postings on request). Storage builds the index after load and journal replay, and rebuilds the whole index before a write or erase commits (O(all notes); the incremental path (layered versions and background merge in layer.zig, bounded.zig and merge.zig) is vendored, but storage does not use it yet). A retryable erase returns before that swap, so the live index stays. If index build fails after the journal opens, the journal is closed. Lookup, live fetch and promotion remain separate slices. docs/NOTES-CODE-COMMENTS.md is the full pin commentary, including sections for later slices (15 is overlay.zig, 21-29 are lookup and after).

Second of five dependent Notes slices for #1012. It sits on top of Notes 1/5 (4ab2519f), so until 1/5 merges this diff also shows 1/5's commit; this slice's own changes are the last 4 commits. Serve notes flags live in notes_flags.zig. Shared snapshot types stay in notes/types.zig; the index re-exports them so Model.state matches.

Receipt

  • Zig 0.17.0, macOS arm64. Source 1c47f39d642ef06a77eef6d1b9b4f437669a4487.
  • zig build test-server-cpu --summary all -Dcpu=apple_m1: 5/5 steps succeeded; 787/791 tests passed, 4 skipped.
  • GitHub CI on this head passes on Linux and Windows.
  • Same lean checker: 437 inherited findings, 437 on this branch, zero new.
  • SA and postings n-grams match on the L6 fixture, including the snapshot's distinct counts. A vocabulary the suffix array cannot match is refused; postings still builds. A committed write rebuilds the index; abandoning prepare leaves it. Erase rebuilds from the kept notes. An unsupported suffix-array load with a journal path closes the journal. --notes-index is sa by default, postings on request, nothing else.

Remaining qualification

This is a draft for review, not a claim that #1012's six gates pass. Retrieval is the next slice. Not run on this follow-up: plain/native rebuild, real-model Metal/CUDA exactness, and the full device-dependent zig build test target.

Checklist

  • Request scheduling stays in the shared lane engine; no new scheduler or reduced precision.
  • Index build, SA/postings agreement, storage rebuild, erase rebuild, journal-close-on-fail and CLI tests pass.
  • Source modules satisfy zero-new lean findings; LLWZ attribution on the vendored index is preserved.
  • Rebase onto PR1 4ab2519f.
  • Independent review of PR1's rebase (ce51371); CI green on 4ab2519.
  • Final-branch Metal and CUDA qualification.
  • Complete final-stack accuracy, cache reuse, live-fetch and promotion gates from Proposal: TensorFold Notes, memory inside the server (for 1.1.0) #1012.

@ThinkOffApp
ThinkOffApp force-pushed the notes-pr2-index branch 2 times, most recently from 7c9fb78 to 1c47f39 Compare October 24, 2026 17:31
@ThinkOffApp
ThinkOffApp marked this pull request as ready for review October 24, 2026 19:29
Open the journal fixture for directory iteration on Linux. Use the platform libc and socket surfaces on Windows, preserve temporary-file identity with volume and file index, and refuse reparse points for no-follow opens. Keep POSIX fork and privileged symlink tests separate from the portable hardlink ownership regression.

Refresh tokenizer example build configuration so the selected tokenizer directory is observed.
Keep the stable sidecar writer lock on Windows without mandatory locks on journal data. Replace an open destination through FileRenameInfoEx when MoveFileEx cannot, preserving old readers. Cover replacement with an open reader and refusal of another writer at the rename seam.

Move Notes flag parsing into notes_flags so the merge with current main remains within the CLI line limit.
Startup refuses --notes, naming the cause, with --recall, --compact-memory,
TENSORFOLD_REQUEST_LOG, --cortheon or a speech model; on an engine that cannot
erase its prompt states (no hook, or a peer that does not acknowledge drops,
found by forgetting a span no tokenizer emits); and on a journal that recorded
a start with one of those writers on. --learn and --slide stay accepted: each
erase names their places with complete: false. The docs list every place erase
does not yet fully reach.
A request log, an engine without prompt-state erase and a peer that does not
acknowledge drops each exit 2 without appending a run to the journal; an
accepted start serves /v1/notes, takes a write and appends its run, so the
journal-size check can fail.
Each refused start runs on its own thread with a 20 s deadline; a start that
listens instead is stopped with SIGTERM and fails as NotRefused, so a lost
refusal is a red test, not a CI timeout.
cli.zig keeps one notes field. This slice adds path and index; later slices add language and lookup in the same struct. Flag table rows and help come from notes_flags so cli.zig does not grow.
overlay.zig has no caller at the pin; past.zig belongs with lookup.
Add NOTES-CODE-COMMENTS.md so the remaining section pointers resolve.
Storage still rebuilds the whole index on every write and erase.
The journal errdefer only covered replay. Index build and alias
populate sit after that block, so an unsupported suffix-array
vocabulary leaked the open journal. The cleanup now covers them,
and a test loads that case with a journal path.
A response reader waiting for EOF could leave the host suite blocked in join after dispatch. Bound its full read, retry interrupted write shutdown, and report timeout or shutdown failure. A peer-withheld EOF control fails without the deadline and completes with it.

@ashhart ashhart left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks @ThinkOffApp. This applies cleanly on #1239 (rebased). Your fix from #1241 (40154d4: usage errors print main's two lines, help only for -h) belongs in this slice. Before it can go further:

  1. The whole index is rebuilt on every write or erase while holding the notes lock (storage.zig), briefly with two copies in memory. Since chat lookups in #1241/#1254 take the same lock, each write would stall every "notes": true request for a rebuild. Please measure write latency and peak memory at 1k, 100k and 1M notes with lookups running.
  2. The incremental path (layer.zig, bounded.zig, merge.zig, core_build.zig, and the index's layer/reaper/retired fields) isn't reachable from storage. Wire it up, or move it to the slice that uses it.
  3. index.zig says it's vendored from another MIT project, but the notices only name the test-suite repo. We need to settle provenance before taking vendored code.
  4. docs/NOTES-CODE-COMMENTS.md moves long comments out of the code (please keep comments short in place instead), covers later slices, and links to a design doc that doesn't exist.
  5. Smaller: the --notes help line promises lookup that this slice doesn't do; --notes-index without --notes is silently ignored; and id_place is sized by the largest id ever issued, which grows forever because ids aren't reused.

@ashhart

ashhart commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Closing with the rest of the Notes series; the reasons are on #1254. Thank you.

@ashhart ashhart closed this Oct 26, 2026
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