Repository navigation
Description
Context
Seven core functions exceed 200 lines. The three largest on a6b945b:
audit(packages/core/src/lib/auditor.ts:658-1040, 383 lines; the file is 1,107 lines).applyMigrations(packages/core/src/lib/db.ts:119-452, ~330 lines).reconcile(packages/core/src/lib/hygiene.ts:1168-1477, 310 lines;hygiene.tsis 1,548 lines with 31 exports).
Incremental migrations exist on purpose: a rebuild would drop stored vectors and force a paid re-embedding. Not every historical step preserves vectors, though: the pre-v3 step deliberately deletes them because the vector space changed (DELETE FROM vec_chunks, packages/core/src/lib/db.ts:194-206). A refactor must keep that.
applyMigrations reads its starting point from getSchemaVersion (packages/core/src/lib/db.ts:482-489), which turns a missing or unreadable version row into 0 and wrongly replays every step, including that deletion. That corruption bug is #2505's, not this issue's. Its fix touches the same function, so this refactor lands after it.
hygiene.ts also holds detection (:159-680) and log-file parsing and rendering (:682-1080). Splitting the file along those lines is not part of this issue; its frontmatter regex at :686 belongs to #2507.
Blocked by #1353
Blocked by #2505
Scope
audit: one function per check, composed by a short driver.applyMigrations: one function per schema step, in an ordered list, starting from the version-read behaviour core: a missing or unreadable schema_version replays every index migration and deletes stored vectors #2505 leaves in place.reconcile: separate detection, joining and state-machine application.
Out of scope
- Replacing incremental migrations with rebuild-on-bump.
- The missing/unreadable
schema_versionhandling and its refusal (core: a missing or unreadable schema_version replays every index migration and deletes stored vectors #2505). - Splitting
hygiene.tsinto detect, log-format and reconcile modules, anddoctor.ts. - Any behaviour or output change.
Acceptance criteria
- Each of
audit,applyMigrationsandreconcileis under ~120 lines. - A migration test opens a database at every historical schema version in the fixtures and reaches the current version with the same result as before the refactor: steps that preserve vectors still preserve them, and the pre-v3 step still clears
vec_chunks. The test passes on the tree before the refactor (it is a characterisation test) and a mutation that drops the v3 deletion or reorders two steps makes it fail. - Audit and hygiene outputs on the fixture corpus are byte-identical before and after.
Verification
bun run test packages/core
bun run typecheckContract impact
None.
2026-10-24: removed "Part of #1341" (detached; the epic's outcome is shared CLI/MCP operations, not internal function size), corrected the migration criterion to preserve each step's existing result including the intentional pre-v3 vector deletion, added Blocked by #2505, and refreshed anchors from the 2026-10-22 comment.
Activity
schlessera commentedon Oct 21, 2026
Current anchors from a repository review on 2026-10-22, at main
dcf5341aa:audit:packages/core/src/lib/auditor.ts:658-1040(383 lines; the file is 1,107 lines).reconcile:packages/core/src/lib/hygiene.ts:1168-1477(310 lines).applyMigrations:packages/core/src/lib/db.ts:106-439.hygiene.tsis 1,548 lines with 31 exports.Two observations for whoever picks this up:
reconcile,hygiene.tsholds two more responsibilities this issue does not split: detection (:159-680) and log-file parsing and rendering (:682-1080, with its ownFRONTMATTERregex at:686). Splitting the file into detect, log-format and reconcile modules along those lines would bring the file, not just the function, down to size. If that is wanted, it is a scope addition for this issue or a sibling. The frontmatter regex is being consolidated separately (packages: frontmatter fence detection is hand-written at eight sites that disagree with parseFrontmatter on BOM and end-of-file fences #2507), so leave:686to that work.applyMigrationsreads its starting point fromgetSchemaVersion(packages/core/src/lib/db.ts:469-476), which turns any error, or a missing version row, into 0 and replays the v3DELETE FROM vec_chunks. That is filed separately as a bug. Its fix touches the same function, so sequence the two to avoid conflicting edits. This issue's acceptance criterion "reaches the current version with vectors intact" should still hold after that fix.packages/core/src/cli/commands/doctor.ts(954 lines, with checks,applyFixesat:848-927and rendering in one file) is a similar case that no open issue covers.schlessera commentedon Oct 23, 2026
2026-10-24 restructuring: detached from #1341. That epic's outcome is shared CLI/MCP operations and one index reader. This issue splits internal
audit,reconcileand migration functions, and its criteria are function size and preserved behaviour. A shared prerequisite (#1353) does not make it a member.schlessera commentedon Oct 24, 2026
2026-10-24: body updated.
vec_chunks(packages/core/src/lib/db.ts:194-206). The criterion now requires each historical migration's existing result.schema_versionreplay and edits the same function.