Skip to content

feat(brain): parallel corpus write + sort-by-date - #63

Merged
eSlider merged 7 commits into
mainfrom
feat/brain-parallel-corpus-write
Aug 21, 2026
Merged

eSlider merged 7 commits into
mainfrom
feat/brain-parallel-corpus-write

Conversation

@eSlider

@eSlider eSlider commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Summary

Optimizes the 2dph brain corpus (re)index and adds control/monitoring, TDD-first.

Feat(brain): parallel corpus write

  • corpus_pool.go (cgo-free core): parallelEmbed order-preserving worker pool,
    chunkBounds transaction batching, ProgressReporter (live rate + ETA).
  • WriteCorpus takes WriteOptions{Workers,Batch,Skip,Limit,Progress}; embeds in
    parallel then writes in batched AddLeafs transactions.
  • --skip resume via existingLeafIDSet (skips leafs already in the DB).
  • index.go control flags: --workers --batch --progress --skip.
  • TDD: corpus_pool_test.go (order, worker cap, ctx cancel, error propagation,
    progress counts, chunk bounds).

Feat(brain): sort by date

  • rank: SortByDate / RankAndFilterSort, --sort date[:asc|:desc].
  • read.go: return valid_from/valid_to; wired from CorpusLeaf.Date.
  • httpapi/MCP: surface valid dates.

Rebuild result

Ran on 20-core box: 242,275 leafs at ~100/s with 12 workers (vs sequential),
live ETA; embedding completed before monitor abort (resume via --skip).

Test

go test ./internal/brain/ (cgo-free pool tests) pass; tagged build via zig ok.

Replace the sequential per-leaf embed+upsert loop in WriteCorpus with a
cgo-free worker-pool core (corpus_pool.go):

- parallelEmbed: order-preserving worker pool over N goroutines; embed
  errors collected per-result; ctx cancellation returns context.Canceled.
- chunkBounds: split leafs into [start,end) transaction batches.
- ProgressReporter: live rate + ETA to stderr (control/monitoring).
- WriteCorpus now takes WriteOptions{Workers,Batch,Skip,Limit,Progress},
  embeds in parallel then writes in batched AddLeafs transactions.
- --skip resume: existingLeafIDSet skips leafs already in the db.

TDD: corpus_pool_test.go covers order preservation, worker cap, ctx
cancel, error propagation, progress counts, chunk bounds.
index.go gains --workers --batch --progress --skip control flags.
- rank: add SortByDate / RankAndFilterSort and --sort date[:asc|:desc]
- read.go: get() returns valid_from/valid_to; ValidFrom wired from
  CorpusLeaf.Date in the corpus loader
- search/httpapi: surface valid dates in MCP + spec
The previous skip filtered leafs after parallelEmbed, so a resume still
re-embedded every leaf. Move the skip check to before embedding so a re-run
only embeds leafs not already in the db. Extract filterExistingLeafs + LeafID
into the cgo-free pool file so the resume path is unit-testable; add
TestFilterExistingLeafs.
--rebuild deletes the db, making --skip useless for resuming a crashed
index build. Let --skip run without --rebuild so it preserves the existing
db, skips already-embedded leafs, and only builds missing indexes.

Verified: rebuilt 242k leafs, resume completed EXIT=0, indexes built
(db 1.49GB->4.24GB), semantic search + --sort date:desc work, daemons up.
@eSlider
eSlider merged commit 3aee544 into main Aug 21, 2026
4 checks passed
eSlider added a commit that referenced this pull request Aug 24, 2026
feat(brain): parallel corpus write + sort-by-date
eSlider added a commit that referenced this pull request Sep 15, 2026
feat(brain): parallel corpus write + sort-by-date
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.

1 participant