← Back to Pull requests

feat(storage): reclaim context-offload SQLite free pages in Host maintenance #5855

Closed
unknown closed this pull request without merging on Oct 08, 2026 09:33 UTC
View on GitHub
unknown unknown commented on Sep 29, 2026 18:29 UTC
Author

Summary

context-offload.sqlite is created with auto_vacuum = INCREMENTAL (sqlite-context-offload-schema.ts), but nothing on main ever ran incremental_vacuum. Garbage collection freed pages logically, and the file never shrank. This PR adds that reclamation as a bounded lane in the existing HostStorageMaintenance loop, so freed pages go back to the filesystem after Ready.

  • SqliteContextOffloadStore.reclaimFreePages({ maxPages }):

  • - It first reads freelist_count in a read transaction, so an idle tick never takes the write lock.
    - It then runs one PRAGMA incremental_vacuum(min(maxPages, freelist)) inside the store's write transaction.
    - It follows a productive batch with a PASSIVE WAL checkpoint, which never waits on readers.
    - It returns { reclaimedPages, hasMore }.
  • The writer facade exposes the method through the same operation tracking as collectGarbage.

  • A new maintenance lane, context-offload page reclamation, runs 64 pages per batch. It uses the existing active and idle cadence: 100 ms while work remains, otherwise 60 s. It runs beside the existing GC lane and the archive-retention lane from #5902.
  • Scope: this PR covers only the context-offload lane. It adds no protocol operation, no epoch bump and no UI. M1's context_offload footprint (#5832) already shows the file shrinking. Converting runtime.sqlite to incremental vacuum still needs a decision on where its one full VACUUM runs, so it moves to a follow-up issue rather than living here.

    Part of #5825.

    Review focus

    This head supersedes the earlier revisions. It takes the 2026-10-01 review on top of @garvit-arora's lane:

  • Rebased onto main. The only conflict was storage-maintenance.ts against #5902's retention lane. Both lanes are kept, and the reclamation lane now declares activeDelay/idleDelay, which #5902 made required.

  • reclaimedBytes removed. Nothing consumed it, and the main/WAL/SHM file-set delta measured checkpoint timing rather than reclamation: the first tick reported 0 bytes for 64 freed pages. With it go the stat calls and the duplicate readSqliteDatabaseFileSetBytes (a copy of storage-footprint.ts's private fileSetBytes), so sqlite-file-set.ts is unchanged from main.

  • sqlite-page-reclamation.ts and its test folded into the store. The per-batch auto_vacuum reader is dropped, because the schema already refuses to open a non-incremental database. So are the second and third copies of the maxPages, empty-freelist and :memory: checks. A guard against incremental_vacuum(0) stays, since a zero argument would free every page.

  • The store test now asserts observable state, not a re-derivation of the implementation's formula:

  • - The first batch frees exactly 64 pages, and freelist_count drops by 64.
    - Before any external checkpoint, the main file has already shrunk, which pins the store's PASSIVE checkpoint.
    - Draining until !hasMore empties the freelist, its batch totals equal the freed pages, and page_count and the file shrink.
    - A further call is a no-op.
  • The lane test pins the idle cadence: once a batch reports no more work, the lane waits for its idle delay instead of retrying every 100 ms.

  • hasMore requires progress (after < before). A batch that frees nothing cannot spin the lane. The schema already makes this unreachable today, so it is a guard, not a fix.
  • An independent review of the follow-up found the PASSIVE checkpoint and the always-true lane uncovered. I confirmed both with mutants. With the second follow-up commit (47acd335d), each mutant now fails its test.

    Verification

  • npm --workspace builds of @maka/core, @maka/storage, @maka/mcp, @maka/runtime and @maka/runtime-host pass.

  • storage-maintenance.test.js: 6/6 pass, including the reclamation lane and #5902's retention lane. The rewritten reclaimFreePages store test passes 3/3 repeated runs. Mutants that drop the PASSIVE checkpoint, or make the lane always report more work, each fail. In the full sqlite-context-offload-store.test.js on Windows, 16/25 pass. The other 9 are untouched tests that fail with EBUSY ... unlink during temp-dir cleanup, the same Windows handle artefact recorded in the previous review.

  • biome format and biome lint on the changed files, the @maka/storage and @maka/runtime-host typechecks, and npm run windows:inventory all pass.

  • I did not run a live Host or packaged Desktop.
  • AI use

    Select exactly one:

  • No generative tool made a substantive contribution

  • Generative tooling made a substantive contribution
  • Tool(s) and scope: the original lane came from @garvit-arora's revisions. Claude Code did the rebase onto main, the review follow-up (the reclaimFreePages simplification and removal of the duplicate helpers) and the rewritten store test.

    Checklist

  • Tests cover the change and fail without it

  • Lint, format, typecheck and the affected suites pass locally
  • Does this PR entail a change in behavior?

  • Yes — described under Summary above

  • No
  • Diff: 637 lines (+477 / -160) across 7 files Reviewer: liugddx Review duration: 12423m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 637 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #5855"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.9% · Safe to merge
    19.9%
    Repository