Repository navigation
perf(doctor): avoid duplicate agent integrity scans - #158366
Conversation
Let schema admission detect physical corruption before invoking the preserving index repair. Healthy stores no longer take an immediate writer transaction to repeat the same full-file scan. Label startup and maintenance transaction owners so remaining slow holds are attributable.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: blocked before merge. Reviewed September 25, 2026, 5:53 PM ET / 21:53 UTC. ClawSweeper reviewWhat this changesThe branch makes Doctor attempt schema admission before conditional SQLite index repair, labels slow database transactions, and updates focused tests and documentation. Merge readiness⛔ Blocked before merge - 3 items remain Current main still performs the redundant scan, and the reported SQLite benchmark supports a useful improvement. Source review found no discrete introduced correctness defect. A published-updater upgrade check remains outstanding. Priority: P2 Review scores
Verification
How this fits togetherDoctor checks and migrates each agent's SQLite database during maintenance and image activation. It reports repairs and migration results before the Gateway uses that state. flowchart LR
A[Doctor or image activation] --> B[Agent SQLite store]
B --> C[Schema admission and integrity]
C --> D{Integrity failure?}
D -- Yes --> E[Backup and index repair]
D -- No --> F[Media migration and report]
E --> F
Before merge
Agent review detailsSecurityNone. PR surfaceSource +100, Tests +59, Docs +7. Total +166 across 12 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Healthy stores receive one integrity scan while Doctor retains its backed-up index repair and quarantine recovery behavior across upgrades. Do we have a high-confidence way to reproduce the issue? Yes. Current main unconditionally runs index-repair integrity scanning before schema admission, and the PR reports a before-and-after run of that migration API; I did not execute the checkout. Is this the best way to solve the issue? Yes, provisionally. Reusing schema admission and retaining the existing repair owner is a narrow approach; the upgrade path still needs its required proof. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 800bf8e10e7c. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
Let schema admission detect physical corruption before invoking the preserving index repair. Healthy stores no longer take an immediate writer transaction to repeat the same full-file scan. Label startup and maintenance transaction owners so remaining slow holds are attributable.
What Problem This Solves
Media migration scans every healthy agent database twice for physical integrity, holding an immediate write transaction for the first pass. A reported 26 GB store spent 123,621 ms in that unlabeled transaction before media detection.
Related: #151025, which moved legacy repair out of ordinary Gateway startup. This remaining cost affects the shared Doctor owner, including container activation.
User Impact
Doctor and image activation avoid a redundant full-database scan on healthy stores. Slow startup/maintenance transactions now identify their operation. No configuration, schema, retention, or recovery contract changes are required.
Why This Change Was Made
The hold is attributable to
repairSqliteIndexCorruption:migrateLegacyMediaPersistencecalled it unconditionally, and it exhaustsPRAGMA integrity_check(2147483647)inside an immediate transaction. Schema admission immediately afterward already performs full integrity and foreign-key validation.Run schema admission first and enter the existing backup-and-repair path only for a proven integrity failure, then retry admission. This removes the duplicate work without introducing an integrity cache or trusting a schema version as proof of physical health. Quarantine cleanup, original-row preservation, maintenance authority, and legacy migration behavior remain covered. Labels cover the identified outer schema, repair, quarantine, migration cursor, startup lease, and Gateway ownership transactions. Net production growth: 100 lines.
Evidence
Blacksmith Testbox (
blacksmith-testbox, profileopenclaw-check), synthetic canonical store with 50,000 sessions / 500,000 transcript rows, 1 KiB message content, 743,149,568 bytes. Same lease and fixture recipe, one measurement per revision:Total migration improved 22%. The discarded integrity pass alone took 515 ms. These are synthetic measurements, not a production restart or a claim that all startup delay is removed. The existing required admission check and media-content scan remain.
tbx_01m3d55j1af2kjp5y0f95svg9j.node --import tsx .lane211-bench.tsexercised the real migration API and traced native integrity statements/transaction durations. Scratch fixture and script are task-owned and cleaned up. The new one-scan regression failed on the original code (2 checks) and passed after the fix.tbx_01m3d5xdq88r7dwz3yf1wcajhs.node scripts/run-vitest.mjs src/infra/state-migrations.media-persistence.lifecycle-recovery.test.ts src/infra/sqlite-index-corruption.test.ts src/infra/state-migrations.media-persistence.historical.test.ts src/infra/state-migrations.media-persistence.historical-v14.test.ts src/infra/state-migrations.owner-mismatch.test.ts src/state/openclaw-agent-db-maintenance.test.ts --maxWorkers=1: 36 tests passed, 44.26 s Vitest / 65.47 s wrapper wall. Covers real index corruption with and without quarantine, repair backup/rollback/authority, labeled slow holds using a fake clock, historical schemas, and owner mismatch.node scripts/check-changed.mjs --base 8133be64d156a7a7f625ab69469b642930e29aecpassed: core types, all 25 core test type graphs, lint, formatting, docs, unused exports, and repository guards. The first default-base invocation compared this older worktree with newer main and failed on unrelated upstream line-cap changes; pinning the actual pre-change base scoped the gate correctly.pnpm test <file> --maxWorkers=1runs passed on the validation lease: lifecycle recovery, 4 tests / approximately 3.16 s test execution / 11.59 s wrapper wall; index corruption, 7 tests / approximately 0.34 s test execution / 7.48 s wrapper wall. CI run elapsed: 1,096 s including orchestration and queue time (not a per-file benchmark).c4be11cc9a1d: 129 successful jobs, zero failures, includingopenclaw/ci-gate. All reported PR checks passed or were skipped.git diff --checkpasses; local and Testbox Git blob hashes match for all 12 changed files.Infrastructure notes: both leases required the prescribed frozen-lockfile dependency installation. Two later sync attempts timed out before executing tests; replacing that lease restored normal sync. Both leases are stopped at closeout. No production database was changed or restarted. A 26 GB replay and a published-updater end-to-end run were not performed.