Repository navigation
refactor: keep usage pricing SQLite reads off the Gateway thread - #148709
Conversation
|
🦞👀 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: needs maintainer review before merge. Reviewed September 15, 2026, 1:24 AM ET / 05:24 UTC (Revision 6). ClawSweeper reviewWhat this changesMoves usage-pricing metadata and catalog reads into existing SQLite workers while preserving cost calculations, propagating cancellation, and scheduling database-owned tests once. Merge readiness✅ Ready for maintainer review This remains useful, maintainer-sponsored work that current main does not implement. The earlier cancellation defect is fixed, GitHub now reports the branch mergeable, and no new blocking defect was found. Priority: P2 Review scores
Verification
How this fits togetherUsage reporting combines session records with configured and catalog prices to produce costs, logs, and charts. This change prepares pricing metadata through the shared database worker before synchronous calculations consume it. flowchart TD
A[Usage report request] --> B[Session records]
B --> C{Estimate needed?}
C -->|No| F[Preserve recorded cost]
C -->|Yes| D[Prepare pricing metadata]
D --> E[Shared SQLite worker]
E --> G[Cached pricing calculation]
F --> H[Costs logs and charts]
G --> H
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +331, Tests +296, Other +6. Total +633 across 21 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Technical reviewBest possible solution: Keep pricing calculations synchronous over prepared facts, with SQLite reads and cancellation owned by the existing cache and database lifecycles. Do we have a high-confidence way to reproduce the issue? Yes, at source level: cold usage pricing on main reaches synchronous metadata and catalog reads. The reported Node/Bun measurements support that mechanism; this review did not execute it. Is this the best way to solve the issue? Yes. The patch prepares existing owners before calculation, preserves pricing precedence and stored formats, and fixes cancellation at the actual log entrypoint without adding configuration or a competing store. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against d47f299c681a. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (5 earlier review cycles)
|
|
Merged via squash.
|
…nclaw#148709) * refactor: prepare usage pricing through SQLite workers * fix: schedule database-owned infra tests once * fix: propagate pricing cancellation from session logs * test: capture config read failure identity
Related: #144592
Builds on the metadata preparation merged in #148305.
What Problem This Solves
Cold usage reports, session logs, and usage charts can block the Gateway thread while loading SQLite-backed metadata and remote prices for cost estimation.
User Impact
Usage pricing prepares its database inputs asynchronously while preserving recorded costs, configured prices, scoped metadata, and startup catalog selection. No configuration changes or data migration are required.
Why This Change Was Made
Usage fingerprints await the existing metadata and shared-state worker owners. Logs and charts prepare pricing only when a record needs an estimate; billed-only reads and disabled remote catalogs retain their existing laziness. Per-record calculation keeps using the cached synchronous resolver.
The metadata resolver preserves current, workspace-scoped, and explicitly fresh selections. Remote preparation preserves the first published startup catalog and optional read/parse fallback. Original-owner checks and typed cancellation prevent retired or invalidated reads from publishing fallback absence into the existing startup or pricing caches. Log decoding remains best-effort, while failed asynchronous pricing preparation rejects the log request instead of silently omitting a valid record.
Evidence
Six compiled synthetic comparisons using JSONL diagnostic inputs on the qualified pricing source preserved costs, chart order/cumulative values, and fingerprints. Cold logs and charts went from 65 to zero parent-thread SQLite calls on Node and from 60 to zero on Bun, including cleanup. Billed-only reads stayed at zero; later estimation went from 41 to zero after a recorded catalog update.
All seven native SQLite methods were counted without throwing. Explicit fixture writes and their cleanup remain separately counted. Bun's five runtime-validation calls precede the counters; no fixture access is exempted.
140 affected-owner tests and the complete changed-file gate passed before the additive main composition. Controlled JavaScript cancellation regressions failed before repair and passed afterward. The composition preserves the backup-record worker commands; 43 focused backup/metadata tests, core types, scoped lint, import-cycle checks, and fresh independent review passed. One existing Windows-only test was skipped on macOS.
Both the qualified pricing source (
70c8cddc63b6) and runtime composition (f59f310a8eb6) passed normal builds and isolated CLI checks. Runtime-artifact Node/Bun cold-log controls also passed with all seven parent SQLite counters at zero across 16 windows, including series reads, repeats, and cleanup. The six-case comparisons remain attributed to70c8cddc63b6.CI preflight exposed duplicate scheduling when an infra test also belonged to the canonical database-worker list. The planner now defers to that owner once; the identical hybrid planner call passes locally, all 76 planner tests pass, and filesystem coverage plus the separate uniqueness assertion remain intact. This follow-up changes no pricing runtime source.
The actual log-entrypoint cancellation regression failed on
60b2cbebbf3aand passed after narrowing the malformed-record catch. Both database-admission and plugin-cache retirement reject the request; fresh reads preserve malformed-record skips, billed-zero costs, and estimates. Twenty-five focused log/reporting/stream tests, the full changed-file gate, and independent review passed for this caller repair. Earlier compiled evidence remains pinned to its named runtime commits.A subsequent CI run encountered the memory-wiki fixture cleanup failure family addressed by merged fix(tests): drain shared database workers before fixture cleanup #148734. This branch now includes that canonical awaited cleanup, with both landed fixture files preserved exactly; 14 selected ordinary Wiki, Teams, pricing and maintenance cases, static checks and fresh review passed on the combined source. The exact inode/generation cause is not claimed, and admission checks remain unchanged.
CI also reported an invalid-config reply in the existing runtime-inspection fixture. Its snapshot matched the written fixture, but the original exception was unavailable. This branch now inherits the exact diagnostic merged in chore(plugins): expose hidden CI inspection errors #148736, which records the existing config-read phase and allowlisted error identity only on failure. The redundant branch-local observer was removed. Production behavior and assertions remain unchanged; the cause is unproven pending new-head CI. Static checks and independent review cover the composition; no held scenario was replayed.
Timings were mixed and do not establish a general speedup. These counts cover the named pricing reads; other SQLite transcript, usage-cache and catalog-write owners remain tracked separately in the campaign.