Skip to content

perf(tracking): sweep expired rows every Nth insert, not every one - #2615

Open
guyoron1 wants to merge 1 commit into
rtk-ai:developfrom
guyoron1:perf/tracking-cleanup-gating
Open

guyoron1 wants to merge 1 commit into
rtk-ai:developfrom
guyoron1:perf/tracking-cleanup-gating

Conversation

@guyoron1

@guyoron1 guyoron1 commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #2208

record() and record_parse_failure() ran cleanup_old() after every insert. That is three DELETEs (commands, parse_failures, hook_decisions), each its own autocommit statement and so its own WAL commit, on top of the one the INSERT already paid: four commits per recorded command, to delete nothing almost every time (retention is 90 days).

This sweeps on every 100th insert instead, gated on last_insert_rowid(), which the INSERT just above already produced:

  • no extra read on the hot path, no clock, no schema change (an earlier draft of this PR used a meta table; that would not survive the user_version-gated migrations, which never re-run on an existing database)
  • an exact 1-in-100 cadence rather than a probabilistic one, so at most ~100 rows can sit past the 90-day window whatever the command rate
  • hook_decisions keeps its own sampled sweep (maybe_cleanup_hook_decisions), which is the backstop for installs whose commands are mostly denied and so rarely reach record()

The sweep is now best-effort (a warning on stderr), like record_hook_decision: the INSERT has already committed by then, so a failed sweep should not be reported as the record failing.

Measurements

macOS/APFS, WAL, 5k-row history, running the exact statements record() runs (release build): 63-99 µs per record() with the sweep vs 51-89 µs without it. That is 10-20 µs and three of the four WAL commits removed per recorded command. It is well short of the ~7 ms end-to-end figure in #2208, which that issue's own PRAGMA table attributes mostly to journal/sync settings; the sweep's share grows with how expensive fsync is on the filesystem.

Tests

  • test_record_skips_the_sweep_between_samples: a single record() must not sweep (fails when the sweep runs on every call)
  • test_record_still_sweeps_within_one_sample_window: a full window of inserts still deletes expired rows
  • cargo fmt --all -- --check, cargo clippy --all-targets (0 warnings), cargo test --all (all pass)

kylehgc added a commit to kylehgc/rtk that referenced this pull request Jul 23, 2026
- Conflict list: drop rtk-ai#2579/rtk-ai#2590/rtk-ai#2615 (already flagged 07-17), add
  rtk-ai#2401/rtk-ai#2453 and the chunk-7 conflict cluster; note top-pick rtk-ai#2401 is
  a hand-port (issue #54).
- Next-wave: restore recovered candidate rtk-ai#2256 (gh prompt-injection
  scan) dropped in distillation.

Co-Authored-By: Claude Fable 5 <[email protected]>
@guyoron1
guyoron1 force-pushed the perf/tracking-cleanup-gating branch from e1d2f1b to 376afa9 Compare July 24, 2026 15:36
@guyoron1
guyoron1 force-pushed the perf/tracking-cleanup-gating branch from 376afa9 to 9af836f Compare August 14, 2026 18:54
cleanup_old() ran after every record() and record_parse_failure(). It is
three DELETEs, each its own autocommit statement and so its own WAL
commit, on top of the one the INSERT already paid: four fsyncs per
recorded command on a path that runs for every rtk <cmd>, under the
project's <10ms budget. Retention is 90 days, so almost every one of
those sweeps deletes nothing.

Gate it on last_insert_rowid(), which the INSERT just above already
produced - no extra read, no clock, and an exact 1-in-N cadence rather
than a probabilistic one. Because the cadence is per row written rather
than per unit time, the slack is self-scaling and bounded: at most
RECORD_CLEANUP_SAMPLE_RATE rows can sit past the window between sweeps,
whatever the command rate. hook_decisions keeps its own sampled sweep
(maybe_cleanup_hook_decisions), which is the backstop for installs whose
commands are mostly denied and so rarely reach record() at all.

should_sample_cleanup is not reused here: it needs a key that differs per
call, and record() has none - the command text repeats verbatim across
calls, and the clock is the input that function's own comment warns off.

The sweep is now best-effort, matching record_hook_decision: by the time
it runs the INSERT has committed, so a failing sweep must not be reported
as the record itself having failed.

Measured on macOS/APFS, WAL, a 5k-row history: 63-99us per record() with
the sweep, 51-89us without it - 10-20us and three of the four WAL commits
gone per recorded command. That is well short of the ~7ms rtk-ai#2208 measures
end to end; the PRAGMA table in that issue points at journal/sync
settings for the bulk of it, and this only removes the sweep's share,
which grows with how expensive fsync is on the filesystem.

Refs rtk-ai#2208
@guyoron1 guyoron1 changed the title perf(tracking): gate cleanup_old behind 24h interval perf(tracking): sweep expired rows every Nth insert, not every one Sep 21, 2026
@guyoron1
guyoron1 force-pushed the perf/tracking-cleanup-gating branch from 9af836f to 1102e9e Compare September 21, 2026 12:19
@guyoron1

Copy link
Copy Markdown
Contributor Author

Rebased, and reworked: the meta-table design can't survive the user_version migration gating that landed since. On an existing DB the table would never be created and the gate would silently no-op.

It now gates on last_insert_rowid(), which the INSERT above already produced: no extra read, no clock, no schema change, and an exact 1-in-100 cadence, so at most ~100 rows can sit past the 90-day window. That also matches the sampled sweep hook_decisions already uses.

Measured here (macOS/APFS, WAL, 5k-row history): 63-99 µs per record() with the sweep, 51-89 µs without, so three of the four WAL commits per recorded command go away. I can't reproduce the ~7 ms from #2208 on this machine (that issue's own PRAGMA table points at journal/sync for most of it), so I've dropped that claim from the description.

One small behaviour change is bundled in: the sweep is now best-effort (a warning on stderr) rather than failing record(), mirroring record_hook_decision. Easy to drop if you'd rather keep that separate.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant