Repository navigation
fix(relay): retain confirm results for a week and audit events for 90 days - #25353
Conversation
There was a problem hiding this comment.
ℹ️ No critical issues — one test-coverage note.
Reviewed changes
- Confirm-result retention (7 days):
RelayCredentialStorenow reapsrelay_confirm_resultsoncommitted_atthrough theHeapWindowReaperadded in the base branch, with a{16 pages/stmt, 128 pages/tick, 250 rows/tick, 250ms}budget. - Audit-event retention (90 days):
reapBatchgained an optionalORDER BYand now reapsrelay_audit_eventsbyat, walking therelay_audit_events_atindex. reapBatchdelete shape: Postgres deletes switched fromctid IN (subquery)toctid = ANY(ARRAY(subquery))for all four tables; SQLite keepsrowid IN (...).- Tests: SQLite retention-boundary and in-retention replay tests; a Postgres audit plan test; a Postgres end-to-end reap test.
I verified the two load-bearing claims: relay_confirm_results has exactly one reader (confirmResume, tuple-mismatch on a different basis), and a resume basis deadline is 30s (resumeConfirmationDeadlineMs), so a 7-day window is far beyond any live retry. No production code reads relay_audit_events, and no schema/index is added, so the database.ts catalog rule holds. pnpm typecheck and the SQLite suite pass locally.
ℹ️ Nitpicks
- The two EXPLAIN plan tests (
credential-cleanup-sweep-postgres.test.ts:177and:300) hand-copy the statementreapBatchbuilds, so they would still pass ifreapBatchreverted to theIN (...)shape this PR is fixing — no test fails on that regression. Capturing the SQL passed todatabase.queryduringstore.cleanup()and EXPLAINing that would tie the guard to the production statement.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
… days relay_confirm_results (8.3M rows, 5.8 GB) and relay_audit_events (8.4M rows, 3.6 GB) were never deleted. The director's credential cleanup now reaps both after its existing passes: - Confirm results older than 7 days. The only reader replays a stored result for a retry of the same request on the same connection basis; a different basis is already refused as a tuple mismatch. committed_at has no index, so this uses the TID-window reaper from the reservation prune, capped at 250 rows a tick (the 7.4M-row backlog drains over 2-3 days). - Audit events older than 90 days, ordered by `at` so the batch walks relay_audit_events_at. Nothing in the relay reads them back. The oldest row is from 2026-07-14, so this deletes nothing until 2026-10-12. reapBatch now deletes by `ctid = ANY(ARRAY(...))` on Postgres: with `ctid IN (...)` the planner can choose a hash join over a sequential scan of the whole table.
108ee52 to
c003436
Compare
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 📝 WalkthroughWalkthroughThe credential store now reaps confirmation results after seven days and audit events after 90 days. Audit-event deletion orders batches by timestamp. Batch deletion uses SQLite row IDs or PostgreSQL tuple IDs. Tests cover retention boundaries, confirmation-result availability, and PostgreSQL query plans. Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to Cleanup may do some unnecessary database work when no results have expired, but that work is bounded. No material issue remains that should prevent merging. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The retention changes preserve the inspected credential-confirmation checks and use restartable cleanup. No introduced authorization bypass was identified. Risk remains low rather than minimal because audit-history deletion is irreversible and external retention requirements have not been established. Retained concerns Security review detailsSecurity Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
🛠️ Fix failing CI checks 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cloud/apps/relay/src/credential-store.ts (1)
705-705: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueStop the empty-page scan after a full pass with no deletions.
If the PostgreSQL table has one page and no expired rows,
HeapWindowReaper.reapcan issue up to 128 identical one-page DELETE statements per cleanup tick. The 30–36 second schedule makes this bounded periodic overhead, so this is an optional optimization rather than a high-impact issue. Stop after a complete page cycle with no deletions, and preservenextPageso a later invocation retries rows skipped bySKIP LOCKED. Keep the existing budgets.
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
08c773d1-0fb6-4b6c-b677-a4129d3ef1c0
📒 Files selected for processing (3)
cloud/apps/relay/src/credential-cleanup-sweep-postgres.test.tscloud/apps/relay/src/credential-store-cleanup.test.tscloud/apps/relay/src/credential-store.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

ELI5
The relay keeps two kinds of records forever:
The receipts only matter for a few seconds, if the same request is retried on the same connection. The audit log is for support questions. This keeps receipts for a week and audit lines for 90 days, and deletes older ones slowly.
What Changed
The director's existing credential cleanup (every ~30 s) now also reaps two tables:
relay_confirm_resultsolder than 7 days, through the TID-window reaper from fix(relay): prune released control-connection reservations in bounded batches #25352. Per tick it stops at 250 rows, 128 pages or 250 ms.relay_audit_eventsolder than 90 days, through the existingreapBatch(5,000 rows), now withORDER BY atso the batch walksrelay_audit_events_at.reapBatchnow deletes withctid = ANY(ARRAY(...))on Postgres instead ofctid IN (...), for all of its tables. WithIN, the planner can choose a hash join over a sequential scan of the whole table; local Postgres showed exactly that plan for the audit batch. An array of TIDs is always a TID scan. SQLite keepsrowid IN (...).Nothing changes for users:
Why
What depends on
relay_confirm_results. Exactly one read:confirmResumelooks up(user, host, reqId). It returns the stored result only when thebasis_conn_idmatches, and otherwise throwsconfirmation_tuple_mismatch.confirmResume(currentBasis, reqId)(desktop-relay-service.ts).reqIdon a new basis now runs as a fresh confirm instead of failing with a tuple mismatch. It still needs an active basis and a valid credential decision.What depends on
relay_audit_events.relay_audit_eventsreference outside tests is the INSERT incredential-store.ts.Production evidence (read-only session, 2026-10-04/05):
relay_confirm_results:n_tup_del = 0.committed_at. ProductionEXPLAINof aLIMITdelete picks a Seq Scan from page 0, hence the TID-window reaper. ProductionEXPLAINof the TID-window statement showsTid Range ScanplusTid Scan.relay_audit_events:resume-confirmed, ~2%invite-created, ~1%credential-installed, <1%device-revoked.EXPLAINof the ordered batch usesIndex Scan using relay_audit_events_at, even with 6.3M matching rows.Pacing. At ~9 director ticks a minute, 250 rows a tick is ~3M rows a day. The 7.4M-row confirm-result backlog drains over 2–3 days. Audit deletes are about one day's worth of inserts per day.
For Jinwoo: audit retention decision needed
resume-confirmed, one per phone resume.resume-confirmedalone (for example 14–30 days) would cut the table ~3–6x.relay_audit_eventsas a daily-active-host proxy. Any retention ends that history at the window.Linked Issue
N/A. Relay redesign step 0 (internal).
Visual Proof
N/A. Database maintenance only; there is no UI or client-visible change.
Testing
credential-store-cleanup.test.ts(SQLite):credential-cleanup-sweep-postgres.test.ts(local Postgres 16, port 55440):relay_audit_events_at, not a Seq Scan.cleanup()reaps old confirm results and audit events and keeps recent ones.cloud/apps/relay:pnpm typecheckpasses, and the full relay suite with Postgres passes.Review
Notes
VACUUM FULLorpg_repack.