Skip to content

core: split audit, reconcile and applyMigrations into per-check and per-step functions #1357

Open

Description

@schlessera

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.ts is 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

Out of scope

Acceptance criteria

  • Each of audit, applyMigrations and reconcile is 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 typecheck

Contract 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

  1. added
    type: refactor
    area: core
    priority: p3
    agent-ready
    on Oct 9, 2026
  2. added
    blocked
    on Oct 9, 2026
  3. schlessera commented on Oct 21, 2026

    @schlessera
    OwnerAuthor

    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.ts is 1,548 lines with 31 exports.

    Two observations for whoever picks this up:

    • Beyond reconcile, hygiene.ts holds two more responsibilities this issue does not split: detection (:159-680) and log-file parsing and rendering (:682-1080, with its own FRONTMATTER regex 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 :686 to that work.
    • applyMigrations reads its starting point from getSchemaVersion (packages/core/src/lib/db.ts:469-476), which turns any error, or a missing version row, into 0 and replays the v3 DELETE 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, applyFixes at :848-927 and rendering in one file) is a similar case that no open issue covers.

  4. schlessera commented on Oct 23, 2026

    @schlessera
    OwnerAuthor

    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, reconcile and migration functions, and its criteria are function size and preserved behaviour. A shared prerequisite (#1353) does not make it a member.

  5. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    2026-10-24: body updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: coreblockedpriority: p3type: refactor

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions