Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Unreleased

- Message pruning is faster on large tables.
`runtime.retention.prune({ target: "messages" })` and scheduled retention
start each batch after the last message id of the previous batch, so they do
not read again the protected messages that an earlier batch passed. A batch
with fewer than `pruneBatchSize` messages ends the run without one more query.
Pruning removes the same messages as before on SQLite, PostgreSQL, and MySQL,
and `runtime.retention.preview({ target: "messages" })` reports the same
count. No migration is necessary.

## 1.2.0 - 2026-11-30

- Add `runtime.reconciliation.outcomesFor()` and
Expand Down
64 changes: 44 additions & 20 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,31 +1499,55 @@ export class Repository {
}

async pruneRetention(target: RetentionTarget): Promise<number> {
if (target === "messages") return this.pruneMessagesByPage()
let count = 0
while (true) {
const deleted = await this.settings.database.transaction(async (connection) => {
const now = await connection.nowMilliseconds()
const predicate = this.retentionPredicate({ target, now })
const candidates = await connection.all<{ id: string }>(
`SELECT id FROM ${this.table(target)} WHERE ${predicate.sql}
ORDER BY id LIMIT ?`,
[...predicate.parameters, this.settings.pruneBatchSize],
)
if (candidates.length === 0) return 0
const candidateIds = candidates.map(({ id }) => id)
const result = await connection.run(
`DELETE FROM ${this.table(target)}
WHERE id IN (${parameterList(candidateIds.length)})
AND ${predicate.sql}`,
[...candidateIds, ...predicate.parameters],
)
return result.changes
})
if (deleted === 0) return count
count += deleted
const batch = await this.pruneRetentionBatch({ target })
if (batch.deleted === 0) return count
count += batch.deleted
}
}

private async pruneMessagesByPage(): Promise<number> {
let count = 0
let after: string | undefined
while (true) {
const batch = await this.pruneRetentionBatch({ target: "messages", after })
count += batch.deleted
if (batch.candidateIds.length < this.settings.pruneBatchSize) return count
after = batch.candidateIds.at(-1)
}
}

private async pruneRetentionBatch(options: {
target: RetentionTarget
after?: string | undefined
}): Promise<{ candidateIds: string[]; deleted: number }> {
const table = this.table(options.target)
return this.settings.database.transaction(async (connection) => {
const now = await connection.nowMilliseconds()
const predicate = this.retentionPredicate({ target: options.target, now })
const cursor =
options.after === undefined
? { sql: "", parameters: [] }
: { sql: ` AND ${table}.id > ?`, parameters: [options.after] }
const candidates = await connection.all<{ id: string }>(
`SELECT id FROM ${table} WHERE ${predicate.sql}${cursor.sql}
ORDER BY id LIMIT ?`,
[...predicate.parameters, ...cursor.parameters, this.settings.pruneBatchSize],
)
const candidateIds = candidates.map(({ id }) => id)
if (candidateIds.length === 0) return { candidateIds, deleted: 0 }
const result = await connection.run(
`DELETE FROM ${table}
WHERE id IN (${parameterList(candidateIds.length)})
AND ${predicate.sql}`,
[...candidateIds, ...predicate.parameters],
)
return { candidateIds, deleted: result.changes }
})
}

private retentionPredicate(options: { target: RetentionTarget; now: number }): {
sql: string
parameters: unknown[]
Expand Down
Loading