Repository navigation
improve(audit): reduce database reads while recording events - #200523
Conversation
Count existing compact indexes and delete only the oldest excess rows inside the existing synchronous transactions. Preserve all retention bounds and FIFO ordering. Synthetic A/A/B proof cuts main-DB reads 55.6% and writer CPU about half, with identical retained rows.
|
🦞👀 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 Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex review: needs maintainer review before merge. What this changesThis PR replaces deep retention OFFSET scans with row counts and oldest-row selection inside the existing audit storage owners. Example: Record 1,200 synthetic turns with 250,000 retained decision facts and the other audit tables at their caps.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this PR provides a focused, measured reduction in audit retention work that current main does not contain; no actionable correctness or security findings were identified. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe shared-state audit writer receives queued activity, execution identity, decision, and delivery-progress records, persists them, and prunes retained history within existing transactions. flowchart LR
A[Audit producers] --> B[Audit FIFO]
B --> C[Shared state writer]
C --> D[Four retention owners]
D --> E[State database]
E --> F[Audit inspection]
Technical reviewBest possible solution: Retain the focused query replacements and rerunnable benchmark while preserving the existing storage owners and retention contracts. Do we have a high-confidence way to reproduce the issue? The baseline queries visibly traverse deep retained-row offsets, and the supplied benchmark records the corresponding read-budget failure; this review did not execute the workload. Is this the best way to solve the issue? The change addresses measured work at the existing owners and preserves equivalent oldest-first selection, expiry budgets, transaction scope, and rollback invalidation. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 7f07edeba9ea. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +5, Docs +33, Other +129. Total +167 across 8 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 17, 2026, 4:38 AM ET / 09:38 UTC. |
|
Merged via squash.
|
Release adaptation: backport only the Socket Mode reconnect waiter owner and focused regression tests; omit the performance harness and main-only transport documentation addition. Add listener-count assertions for timer settlement and disconnect cancellation; adjust the test allocation to 41. Companion dispositions: #198007 is specific to SDK 3.2 callback/abort semantics and is not applicable to this line pinned to socket-mode 3.1.0. #197657 retires main allocations; other audit hits (#200523, #197009, #194964, #184507, #192197, #192051, #190003, #189850) only overlap the excluded shared perf README. No later #188745 mention through bd692e9. (cherry picked from commit a1d1da7) Co-authored-by: RomneyDa <[email protected]>
…w#200523) Count existing compact indexes and delete only the oldest excess rows inside the existing synchronous transactions. Preserve all retention bounds and FIFO ordering. Synthetic A/A/B proof cuts main-DB reads 55.6% and writer CPU about half, with identical retained rows.
What Problem This Solves
Recording audit events on a large state database repeatedly walks retained history just to find the rows beyond its cap. This consumes SQLite page reads and writer CPU, increasing contention for other state operations.
User Impact
User impact: audit recording does less database work while preserving the same retained events, expiry cutoffs, pruning budgets and FIFO ordering. No schema, index, configuration, collection default or migration changes are required.
Why This Change Was Made
Identity and decision retention step through 100,000/250,000 ordered entries to find an overflow boundary on each insert. Activity and delivery-progress overflow also walk retained rows. Count the existing compact indexes, then select only the oldest excess rows for deletion inside the same synchronous transaction. Progress reuses its existing count; activity retains its exact-count recovery behavior. Runtime changes total five net added lines across the existing owners.
Evidence
A synthetic 4.63 GB database holds 100k activity events, 100k identity contexts, 250k decision facts and 200k progress rows. Five serial A1/A2/B groups used fresh fixture copies for each arm and the real shared writer kernel. No production data or service access was used. Every arm preserved identical row digests and counts.
Main reads decrease 55.6%; CPU and latency beat both controls. Allocation differs by 0.09–0.20%, within the controls' observed range. Pager and allocation instrumentation were measured separately from timing. VFS reads measure SQLite file reads, not physical disk I/O; macOS/APFS with a warm OS cache does not reproduce filesystem stalls. Indexed INSERT probes were sub-millisecond: this change addresses measured retention waste without claiming to explain multi-second production INSERTs or all other shared-state readers.
The documented benchmark read budget fails on base (5,227,048 > 3,000,000) and passes on the fix (2,322,273), with matching row digests. Five cold SQLite-cache SQL probe groups also preserve deletion counts: activity misses 9,691 → 2,382; identity 1,254 → 1,124; decision 3,114 → 861. CPU profiles attribute the largest saving to decision retention.
All 86 focused tests pass across audit-events, execution-identity-context, execution-decision-facts, message-delivery-progress-store and audit-event-writer.worker. Existing ordering, budget, replay/rollback, lazy-store and count-drift expectations remain unchanged. Codex autoreview found no actionable P0–P2 findings. Both unused-file scans, targeted typecheck/lint/format, SQLite guards and docs link checks pass. No affected Control UI imports were found.
pnpm check:changedpasses its other gates but reports an inherited main regression: unused exportsresolveSessionTranscriptPathandresolveSessionTranscriptPathInDir. Both findings reproduce on exact base96763b979fed377e61d44520294b9eb12a740ae0. Introducing commit4c79d8f41631ede63a1de20546224ee4c8c37e50(#200461) removed their last production callers fromsrc/gateway/session-transcript-files.fs.tswhile leaving the exports. These files are unchanged here; this is an identified main regression, not a passing check.