Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Backend: the usage-receipt sweep deletes by _id and takes its whole chunk (#2923)

0xken15a4e0e07b3ca3
diff --git a/backend/integration/productUsageReceipts.test.ts b/backend/integration/productUsageReceipts.test.ts
index aeecf3019..402ceb274 100644
--- a/backend/integration/productUsageReceipts.test.ts
+++ b/backend/integration/productUsageReceipts.test.ts
@@ -110,6 +110,24 @@ it('sends commands to the receipts collection only', async () => {
expect([...collections].filter(name => name !== 'system.profile' && name !== '$cmd')).toEqual(['product_usage_receipts'])
})
+it('a chunk is as large as asked, and its delete reads only the receipts it removes (prod 11-04: 101 a chunk, ~4M examined each)', async () => {
+ // Mongo's first batch holds 101 documents unless asked for more, and a delete matching on both _id and the expiry range was
+ // planned on the expiry index: it examined every old receipt to remove a few hundred.
+ await db.productUsageReceipt.createMany({ data: Array.from({ length: 400 }, (_, i) => ({ id: `bulk-${i}`, expiresAt: at('2026-10-10', 36) })) })
+ await db.$runCommandRaw({ profile: 2 })
+ const since = new Date()
+ expect(await sweep.sweepOldReceipts(db, now, { chunk: 300, chunks: 1, pauseMs: 0 })).toMatchObject({ removed: 300 })
+ await db.$runCommandRaw({ profile: 0 })
+ const answer = await db.$runCommandRaw({ find: 'system.profile', filter: { ts: { $gt: { $date: since.toISOString() } }, op: 'remove' },
+ projection: { docsExamined: 1, planSummary: 1 }, batchSize: 100_000, singleBatch: true }) as
+ { cursor: { firstBatch: Array<{ docsExamined: number; planSummary: string }> } }
+ expect(answer.cursor.firstBatch.length).toBeGreaterThan(0)
+ for (const op of answer.cursor.firstBatch) {
+ expect(op.planSummary).toMatch(/_id/)
+ expect(op.docsExamined).toBeLessThanOrEqual(300)
+ }
+})
+
const late = (day = '2026-10-29') => ({ id: randomUUID(), day, counters: { turns_ended: 2 } })
const turnsEnded = async (day: string) =>
(await db.productUsageTotal.findUnique({ where: { dayUtc_metric: { dayUtc: new Date(day), metric: 'turns_ended' } } }))?.value
diff --git a/backend/src/lib/productUsageReceiptSweep.ts b/backend/src/lib/productUsageReceiptSweep.ts
index 66e6d9dbb..b00a3744b 100644
--- a/backend/src/lib/productUsageReceiptSweep.ts
+++ b/backend/src/lib/productUsageReceiptSweep.ts
@@ -86,15 +86,17 @@ export async function lastOldReceipt(db: PrismaClient, now: Date): Promise<Date
return newest ? dateOf(newest.expiresAt) : null
}
-/** One chunk: up to [chunk] old receipts in expiresAt order, then exactly those, their range checked again. */
+/** One chunk: up to [chunk] old receipts in expiresAt order (a first batch holds only 101 unless asked for more), then exactly
+ * those, their range checked again. */
async function removeChunk(db: PrismaClient, now: Date, chunk: number): Promise<number> {
const range = { expiresAt: { $gt: oldFloor(now),
$lte: date(now.getTime() + (OLD_EXPIRY_DAYS - PRODUCT_USAGE_RECEIPT_EXPIRY_DAYS) * DAY) } }
const found = await db.$runCommandRaw({ find: COLLECTION, filter: range, projection: { _id: 1 }, sort: { expiresAt: 1 },
- hint: 'product_usage_expiry', limit: chunk, singleBatch: true } as never) as { cursor: { firstBatch: Array<{ _id: string }> } }
+ hint: 'product_usage_expiry', limit: chunk, batchSize: chunk, singleBatch: true } as never) as { cursor: { firstBatch: Array<{ _id: string }> } }
const ids = found.cursor.firstBatch.map(receipt => receipt._id)
if (!ids.length) return 0
- const answer = await db.$runCommandRaw({ delete: COLLECTION, deletes: [{ q: { _id: { $in: ids }, ...range }, limit: 0 }] } as never) as
+ // By _id: on both _id and the range Mongo planned the expiry index and examined every old receipt (~4M, ~17 s a delete, 11-04).
+ const answer = await db.$runCommandRaw({ delete: COLLECTION, deletes: [{ q: { _id: { $in: ids }, ...range }, limit: 0, hint: { _id: 1 } }] } as never) as
{ n?: number; writeErrors?: unknown[] }
if (answer.writeErrors?.length) throw new Error('The database refused some receipt removals')
return answer.n ?? 0