Skip to content

Page SQL message pruning by id, matching Ruby after #113 #110

Done

Description

@cardmagic

Page SQL message pruning by id, matching Ruby after #113

Goal: maintenance plus parity. After solid-objects-ruby#113 (merged as 038af6b, shipped in Ruby v1.1.0), Ruby's message pruner keeps the same rows and stays fast on large tables by using correlated NOT EXISTS guards plus keyset paging. SQL message pruning in solid-objects-js should have that same observable behavior. No public API change.

Status: Spec. Do not start implementation until this issue is approved and labeled ready-to-implement. Merge and release still need Lucas.

Inspected JS main at ef6a15e (2026-12-02).

Problem

JS message retention already uses correlated NOT EXISTS guards. In src/repository.ts messageRetentionPredicate a message is prunable only when it has a completed_at_ms, is past the retention cutoff (including per-actor overrides), and no live row points at it:

  • a ready_messages row with that message_id
  • a claimed_messages row with that message_id
  • a dead_letters row with that message_id or retried_message_id
  • an effects row with that message_id and status != 'completed'
  • a broadcasts row with that message_id and status != 'delivered'

That already matches the six Ruby #113 guards in MessagePruner#prunable. One query-shape difference is called out under Scope: JS combines the two dead-letter probes into one OR.

The gap is paging. Repository#pruneRetention selects ORDER BY id LIMIT ? with pruneBatchSize and, after a delete, runs the same query again with no id > last_id cursor. The loop stops only when a batch deletes zero rows, so a short last page is always followed by one more full candidate query, and every batch re-walks leading rows that are still protected.

Ruby #113 fixed that in MessagePruner#prune_relation: each batch resumes after the last id of the previous page, and a short page ends the loop without another query. A full final page is still followed by one query that finds nothing.

Ruby measurements only, from the #113 PR description, on synthetic SQLite 3.54.1 (1M messages, 1.5M effects, 1.5M broadcasts, 25k old messages blocked by pending effects). Candidate query per 1000-row batch: 365, 271, 282, 227, 228, 225 ms before, with SCAN of effects and broadcasts; 31, 5.8, 4.8, 3.2, 2.9, 2.9 ms after. The first after-batch still walks the 25k blocked rows once; later batches resume after them. Those numbers are not JS measurements, and JS already uses NOT EXISTS, so the JS win is the missing keyset resume, not a rewrite of the guards. This issue does not claim a JS wall-clock result.

Existing coverage in test/retention.test.ts checks that only terminal unreferenced message history is pruned and that per-actor retention is honored. It does not cover batch-boundary paging, cursor resume, or a large mixed fixture.

Scope

SQL Repository#pruneRetention for target: "messages" (reached from runtime.retention.prune({ target: "messages" })):

  • Keep correlated NOT EXISTS guards. The messages pruned and retained must stay the same as today's predicate on the same data.
  • Page by primary key the way Ruby #113 does: ORDER BY id, at most pruneBatchSize ids per batch, each later batch considering only ids after the last id of the previous candidate page. Message ids in JS are random UUIDv4 values stored as TEXT (column type in src/schema.ts; assigned with randomUUID() in src/repository.ts, which is crypto.randomUUID() in src/platform/uuid.ts). The cursor is still id > last_id under the same collation as ORDER BY id, not Ruby's integer last_id + 1. A row inserted during a prune can sort below the cursor and wait until the next run, where Ruby's autoincrement would catch it in the same run. That is acceptable, because new rows are not past retention.
  • Keep JS's per-batch transaction and the DELETE that re-applies the retention predicate. Do not drop either just to look like Ruby.
  • A short candidate page (fewer than pruneBatchSize ids) ends pruning without another candidate query. That stop keys off the candidate page length, not the DELETE change count, because the re-applied DELETE can remove fewer rows than the page held. A full final page is followed by one query that finds nothing. This is the batch semantics in Ruby #113's tests (test/integration/message_pruner_test.rb at 038af6b, "a short batch ends pruning without another query" and "a full final batch is followed by one query that finds nothing").
  • preview for messages must still report the same count that prune would delete on a stable dataset. Preview may stay a single count query; it does not need to page.
  • If the shared pruneRetention loop is what changes, instance and process pruning must still delete the same rows as today. This issue does not add product work for those targets.
  • The two dead-letter guards may stay one OR or be split into two NOT EXISTS probes, as Ruby #113 does (DeadLetter on message_id, and DeadLetter on non-null retried_message_id). Either shape is in scope only if the pruned set is unchanged. Splitting is not an index change; JS already indexes dead_letters.retried_message_id.

Acceptance criteria

  • For the same data, message prune and preview results match today's behavior: the same messages are deleted, and the same messages are retained. That includes every combination of the six guards, effect statuses pending / processing / completed / dead, broadcast statuses pending / processing / delivered / dead, a dead letter that was retried (both the original and the retry stay), a message still inside the default retention window, and per-actor retention overrides. Ruby #113's tests in test/integration/message_pruner_test.rb at 038af6b are the behavior reference.
  • Paging deletes every prunable message across batch boundaries when protected rows sit before, between, and after prunable ones, including counts that fall around pruneBatchSize (Ruby #113 covers 0–7 prunable rows at batch size 3). A second prune then deletes nothing.
  • Each batch after the first resumes after the last id of the previous candidate page. A short page (fewer than pruneBatchSize candidate ids) is the last page and does not run another candidate query. That stop keys off the candidate page length, not the DELETE change count, because the re-applied DELETE can remove fewer rows than the page held. A full final page is followed by exactly one query that finds nothing.
  • A large-data regression test comparable to Ruby #113's SQLite fixture (300 messages, 10 completed effects and 10 delivered broadcasts per message, plus leading protected rows so a paged run must skip work already passed) asserts that a paged prune deletes the same messages as the current unpaged predicate and that later batches resume after the last id already considered. Ruby's additional SQLite EXPLAIN QUERY PLAN assertion (SEARCH effects and broadcasts by message_id, no SCAN of those tables) must not be forced by adding indexes. JS src/schema.ts at ef6a15e has no message_id index on effects or broadcasts; this issue does not add one.
  • Existing message retention tests keep passing.
  • CI stays green on the current SQL adapter matrix in .github/workflows/ci.yml: SQLite (quality and floor), PostgreSQL 14 and 18, and MySQL 8.0 and 8.4. The other required jobs stay green.
  • CHANGELOG.md gets a bullet under ## Unreleased (no version heading) that tells a user message pruning is fast on large tables because each batch resumes after the last id, the same messages are pruned on every SQL database, and no migration is needed. Write it for the user, as AGENTS.md requires.

What not to build

  • No index or schema changes, and no migration. The Ruby index migration is a separate spec.
  • No public API change: runtime.retention.preview / prune, RetentionOptions, RetentionResult, pruneBatchSize, and retention settings stay as they are.
  • No change to Durable Objects pruning in src/cloudflare/storage.ts. That path uses a different schema and is not this issue.
  • No release PR, version bump, or publish. Merge and release still need Lucas.
  • No edits or comments on other issues.

Activity

  1. added
    ready-to-implement
    cloving-working
    and removed
    ready-to-implement
    cloving-working
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions