Skip to content

fix(recall): enforce retention on open, not only on store - #4145

Open
dopaemon wants to merge 1 commit into
rtk-ai:developfrom
dopaemon:fix/recall-retention-on-open
Open

dopaemon wants to merge 1 commit into
rtk-ai:developfrom
dopaemon:fix/recall-retention-on-open

Conversation

@dopaemon

Copy link
Copy Markdown

Fixes #4144

Problem

evict() was called from exactly one place — store_inner, right after an insert — so retention_days measured continued use, not elapsed time. A machine that stops storing output (project finished, laptop shelved, filter disabled) kept every row indefinitely while rtk config recall and the documented default both promised 30 days.

Demonstrated on develop with an isolated store (RTK_RECALL_DB), one row aged 31 days, no new output stored:

$ rtk recall --list          # before this change
HASH           COMMAND       LINES  HIDDEN  ORIG  EXIT
aaa111111111   stale cmd         1       1     1     0
$ sqlite3 recall.db 'SELECT COUNT(*) FROM recall;'
1

Fix

Option (2) from the issue, which also subsumes (1): sweep from the two connection entry points rather than from a command path, so every invocation that touches the store — rtk recall, gain, config recall, the tee stats path — enforces retention. open() and open_existing() both call the new expire() right after init_schema, and expire is just evict(conn, cfg, "") with no exempt hash (nothing is being protected on a sweep).

Properties kept:

  • Best-effort, like evict itself — a read-only or locked store keeps its rows instead of erroring.
  • The read path still never creates the database: open_existing returns early when the file is absent, so test_stats_snapshot_never_creates_db still holds.
  • Cost: one DELETE plus a COUNT(*) over at most max_entries (default 200) rows, on a connection that was being opened anyway.

After:

$ rtk recall --list
HASH           COMMAND       LINES  HIDDEN  ORIG  EXIT
bbb222222222   fresh cmd         1       1     1     0
$ sqlite3 recall.db 'SELECT COUNT(*) FROM recall;'
1                                  # the 31-day-old row is gone

Tests

New test_retention_applies_without_a_new_store: two stored rows, one aged past retention_days, then a read-only open_existing() — the stale row must be gone and the fresh one untouched. The 48 existing retriever tests are unchanged and green (several of them open the store after inserting rows, which is what would break if the sweep were too eager).

cargo fmt --all + cargo clippy --all-targets clean; cargo test --bin rtk → 3672 passed.

Note

The issue also flags that set_owner_only is a no-op off Unix, so on Windows the store carries whatever the profile ACL grants. That's a separate concern and isn't touched here — worth its own issue as the reporter suggests.

`evict()` ran from `store_inner` alone, so `retention_days` measured
continued use rather than elapsed time: a machine that stops storing
output — project finished, laptop shelved, filter disabled — kept every
row forever while `rtk config recall` still advertised a 30-day window.

Sweep from both connection entry points (`open`, `open_existing`) so any
rtk invocation that touches the store — `recall`, `gain`, `config recall`,
the tee stats path — bounds staleness by "rtk ran at all". Best-effort
like `evict`: a read-only or locked store just keeps its rows, and the
read path still never creates the database.

Fixes rtk-ai#4144

Signed-off-by: dopaemon <[email protected]>
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes a retention bug where recall database rows were only expired on store/insert, so a machine that stopped storing output retained rows indefinitely past retention_days. The fix moves eviction into the two connection entry points (open() and open_existing()) via a new expire() helper, ensuring retention is enforced on every read/open, not just writes.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4144


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@Wilson421

Copy link
Copy Markdown

Reporter of #4144 here — I verified this independently rather than just reading it, since it is my issue and an unverified "looks good" is worth nothing.

Built and ran it on Windows 11, rtk at bf5747f:

  • cargo test --bin rtk core::retriever — 45 passed, 0 failed.
  • Negative control, because a test that passes against the bug proves nothing: I applied only your new test onto a clean develop (not the fix) and it fails exactly where it should — a row past retention_days must not survive a read-only open, at the load_by_hash assertion. So the test genuinely catches the reported behaviour.

On the shape you chose: option (2) is the one I would have picked, and for the reason you give — one sweep site instead of one per caller. I checked every path that opens the store (stats_snapshot, reset_stats, record_tee_elision, run_recall), and all four are legitimately "rtk ran" moments; there is no backup or export path where an automatic DELETE would surprise someone. Keeping it best-effort and leaving open_existing's early return intact means a read-only or absent store still behaves as before.

Two observations, neither a blocker:

  1. A stale hash is now swept before it can be read. rtk recall <hash> on a row past retention_days previously still returned content (eviction only ran on store); now the sweep in open_existing removes it first, so the user gets nothing instead of something stale. That is the stated policy working as intended, and I think it is right — but it is a visible behaviour change for anyone recalling a hash out of an old transcript, so it may deserve a CHANGELOG line.
  2. Precedent for sampling, if the sweep ever shows up in a profile. record_hook_decision faces the same "this is on a hot path" problem and solves it with maybe_cleanup_hook_decisions — a sampled sweep rather than one per call. The recall store is opened far less often, so I would not add that complexity now; just noting where the pattern already lives in this codebase if it becomes necessary.

Thanks for picking this up so quickly, and for reproducing the gap on develop first — that is the part most fixes skip.

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

Labels

bug Something isn't working recall

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recall retention only applies on write: a quiet machine keeps rows past retention_days

2 participants