Repository navigation
fix: prune messages with NOT EXISTS and keyset paging - #113
Conversation
MessagePruner#prunable guarded each message with six NOT IN subqueries. The effects (status != 'completed') and broadcasts (status != 'delivered') lists cannot use an index, so SQLite scanned both tables for every batch, and #prune_relation re-ran the whole query for every batch, including the final empty one. Rewrite the six guards as correlated NOT EXISTS, which probe the existing message_id indexes, and page by id so each batch resumes after the last. A batch shorter than batch_size ends the loop without another query. Tests compare against the previous NOT IN query for every combination of guards, cover batch boundaries and the query count, and assert on SQLite that effects and broadcasts are searched by message_id.
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge, with both previous findings addressed and no new actionable issues found. SummaryThe PR replaces message-retention guards with correlated
Reviews (2) · Last reviewed commit: "test: make the pruner plan and cursor ch..." · Reviewed by Greptile |
| # @rbs () -> Array[[ActiveRecord::Relation[untyped], Symbol]] | ||
| def reference_guards | ||
| [ | ||
| [ ReadyMessage.all, :message_id ], | ||
| [ ClaimedMessage.all, :message_id ], | ||
| [ DeadLetter.all, :message_id ], | ||
| [ DeadLetter.where.not(retried_message_id: nil), :retried_message_id ], | ||
| [ Effect.where.not(status: "completed"), :message_id ], | ||
| [ Broadcast.where.not(status: "delivered"), :message_id ] | ||
| ] |
There was a problem hiding this comment.
One-use helper adds indirection
reference_guards only returns an array and has one caller. Readers must jump between two methods to understand one query, and the new signature hides the row types behind untyped. This violates the repository directives to inline small functions used once and avoid untyped where real types exist. Satisfy these requirements before merging.
Before:
reference_guards.reduce(Message.all) do |messages, (references, column)|After:
[
[ ReadyMessage.all, :message_id ],
[ ClaimedMessage.all, :message_id ],
[ DeadLetter.all, :message_id ],
[ DeadLetter.where.not(retried_message_id: nil), :retried_message_id ],
[ Effect.where.not(status: "completed"), :message_id ],
[ Broadcast.where.not(status: "delivered"), :message_id ]
].reduce(Message.all) do |messages, (references, column)|Inline the array in prunable, keep its existing @rbs signature, and regenerate the signatures.
Context Used: # De-AI Code Review In this review, suggest simplifying this code by removing "AI slop": Remove: - Comments that restate what the code already shows - Docstrings that just repeat the function signature - Defensive code for impossible scenarios ... (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/solid_objects/message_pruner.rb
Line: 61-70
Comment:
**One-use helper adds indirection**
`reference_guards` only returns an array and has one caller. Readers must jump between two methods to understand one query, and the new signature hides the row types behind `untyped`. This violates the repository directives to inline small functions used once and avoid `untyped` where real types exist. Satisfy these requirements before merging.
Before:
```ruby
reference_guards.reduce(Message.all) do |messages, (references, column)|
```
After:
```ruby
[
[ ReadyMessage.all, :message_id ],
[ ClaimedMessage.all, :message_id ],
[ DeadLetter.all, :message_id ],
[ DeadLetter.where.not(retried_message_id: nil), :retried_message_id ],
[ Effect.where.not(status: "completed"), :message_id ],
[ Broadcast.where.not(status: "delivered"), :message_id ]
].reduce(Message.all) do |messages, (references, column)|
```
Inline the array in `prunable`, keep its existing `@rbs` signature, and regenerate the signatures.
**Context Used:** # De-AI Code Review In this review, suggest simplifying this code by removing "AI slop": **Remove:** - Comments that restate what the code already shows - Docstrings that just repeat the function signature - Defensive code for impossible scenarios ... ([source](https://app.greptile.com/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Inlined the guard list into prunable in ecbea5a and regenerated the signatures.
The SQLite plan assertion depended on whatever statistics earlier tests left behind. Seed 3,000 effects and broadcasts and ANALYZE inside restoring_sqlite_statistics, like the effect recovery plan test. MySQL clients inline the id cursor instead of binding it, so read it from the SQL when there are no binds. Inline the guard list in MessagePruner#prunable.
|
Docs7 for cardmagic/solid-objects-ruby
Commit |
|
@greptileai review |
Summary
The
status !=lists could not use an index, so SQLite scanned all ofeffectsandbroadcastson every batch, and the loop re-ran that query once more after the last batch. No migration and no behavior change: the same messages are pruned.Evidence
EXPLAIN QUERY PLAN:SCANof effects and broadcasts).After: 31, 5.8, 4.8, 3.2, 2.9, 2.9 ms (
SEARCH ... (message_id=?)on both). The first batch still walks the 25k blocked rows once; later batches resume after them.test/integration/message_pruner_test.rb: compares the pruned set with the oldNOT INquery for all 64 combinations of the six guards, covers effect/broadcast statuses, a retried dead letter, batch boundaries (0–7 prunable rows at batch size 3), query counts, and the cursor. A SQLite-only test asserts both tables are searched bymessage_idand not scanned. Against the old code, four of the nine fail.bundle exec rake(test, standard, rubocop, rbs, steep, brakeman) is green locally on Ruby 3.3 and SQLite. Postgres and MySQL run in CI.Merge Danger
Door: two-way
Blast Radius:
MessagePruneronly (#pruneand#preview)