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.

fix(backend): the usage-receipts sweep waits quietly when the database refuses its rule (#2245)

0xken15dc008865cf3a
diff --git a/backend/integration/productUsageReceipts.test.ts b/backend/integration/productUsageReceipts.test.ts
index be0be30e0..2e52f8743 100644
--- a/backend/integration/productUsageReceipts.test.ts
+++ b/backend/integration/productUsageReceipts.test.ts
@@ -217,6 +217,41 @@ it('a failed pass is logged and the next minute\'s pass carries on', async () =>
expect(await left()).toEqual(KEPT.filter(id => !id.startsWith('old-2026-10-30')).sort())
})
+it('a database user that may not install the rule gets one warning and an hourly check, then the sweep runs once it may (prod 10-29)', async () => {
+ const lines: string[] = []
+ const log = { info: (message: string) => lines.push(message), warn: (message: string) => lines.push(message) }
+ await db.productUsageReceipt.deleteMany({ where: { id: { startsWith: 'old-2026-10-30' } } })
+ let refused = true, installs = 0, clock = now.getTime()
+ // As Prisma reports Mongo's refusal (code 13) of collMod to production's app user.
+ const refusing = new Proxy(db, { get: (target, key) => key === '$runCommandRaw'
+ ? (command: Record<string, unknown>) => {
+ if (!('collMod' in command)) return target.$runCommandRaw(command as never)
+ installs++
+ return refused ? Promise.reject(new Error('Raw query failed. Code: `13`. Message: `not authorized on harness to execute command { collMod: "product_usage_receipts" }`'))
+ : target.$runCommandRaw(command as never)
+ }
+ : Reflect.get(target, key) })
+ const stop = sweep.startReceiptSweep(refusing, { everyMs: 20, chunk: 50, chunks: 1, pauseMs: 0, now: () => new Date(clock), log })
+ try {
+ await vi.waitFor(() => expect(installs).toBe(1), { timeout: 5000 })
+ // Many minutes' passes later, within the hour: no second try, no second warning.
+ await new Promise(resolve => setTimeout(resolve, 200))
+ expect(installs).toBe(1)
+ expect(lines).toEqual(['[usage-receipts] waiting: the database user may not install the receipt rule (collMod); nothing is removed, checking hourly'])
+ expect((await left()).length).toBe(109)
+ clock += 3_600_000
+ await vi.waitFor(() => expect(installs).toBe(2), { timeout: 5000 })
+ await new Promise(resolve => setTimeout(resolve, 200))
+ expect(installs).toBe(2)
+ expect(lines).toHaveLength(1)
+ // Ken grants collMod: the next hourly check installs the rule and the sweep carries on, without a restart.
+ refused = false
+ clock += 3_600_000
+ await vi.waitFor(() => expect(lines.at(-1)).toMatch(/done/), { timeout: 5000 })
+ } finally { stop() }
+ expect(await left()).toEqual(KEPT.filter(id => !id.startsWith('old-2026-10-30')).sort())
+})
+
it('counting stays exact: a report inside the window counts once across the sweep, an older one adds nothing', async () => {
const context = { surface: 'cli', release: '0.4.92', platform: 'macos', channel: 'production' } as const
const report = { schemaVersion: 1 as const, revision: 0, id: randomUUID(), day: '2026-11-01', context, counters: { turns_ended: 2 } }
diff --git a/backend/src/lib/db/indexes.ts b/backend/src/lib/db/indexes.ts
index eb7c2f9d4..e77af3c1e 100644
--- a/backend/src/lib/db/indexes.ts
+++ b/backend/src/lib/db/indexes.ts
@@ -2,7 +2,7 @@ import type { PrismaClient } from '@prisma/client'
import { logger } from '../../utils/logger.js'
/** Mongo's "not authorized" (code 13), as Prisma's raw command error carries it. */
-const notAuthorized = (error: unknown): boolean => /not authorized|Unauthorized|Code: `13`/.test(String((error as Error)?.message ?? error))
+export const notAuthorized = (error: unknown): boolean => /not authorized|Unauthorized|Code: `13`/.test(String((error as Error)?.message ?? error))
/** One index as `createIndexes` takes it. */
export type IndexSpec = { name: string; key: Record<string, number>; unique?: boolean; expireAfterSeconds?: number }
diff --git a/backend/src/lib/productUsageReceiptSweep.ts b/backend/src/lib/productUsageReceiptSweep.ts
index 5da09dbae..bf5acaac5 100644
--- a/backend/src/lib/productUsageReceiptSweep.ts
+++ b/backend/src/lib/productUsageReceiptSweep.ts
@@ -2,6 +2,7 @@ import { setTimeout as delay } from 'node:timers/promises'
import type { PrismaClient } from '@prisma/client'
import { PRODUCT_USAGE_RECEIPT_EXPIRY_DAYS } from '../types/productUsageReceipt.js'
import { logger } from '../utils/logger.js'
+import { notAuthorized } from './db/indexes.js'
/**
* Removes, a little at a time, the usage receipts written while a receipt lived 36 days (until 2026-10-29: about 4M of
@@ -21,8 +22,9 @@ import { logger } from '../utils/logger.js'
const COLLECTION = 'product_usage_receipts'
const DAY = 86_400_000
const OLD_EXPIRY_DAYS = 36
-/** About 2,000 a minute, in four deletes with a pause between them: the backlog clears in about a day. */
-export const RECEIPT_SWEEP = { chunk: 500, chunks: 4, pauseMs: 250, everyMs: 60_000 } as const
+/** About 2,000 a minute, in four deletes with a pause between them: the backlog clears in about a day. While the database
+ * refuses the rule, the worker asks again once an hour. */
+export const RECEIPT_SWEEP = { chunk: 500, chunks: 4, pauseMs: 250, everyMs: 60_000, refusedEveryMs: 3_600_000 } as const
/** Every receipt write must carry its life; old receipts, never written again, are not checked. */
export const RECEIPT_RULE = { validator: { $jsonSchema: { required: ['life'] } }, validationLevel: 'strict', validationAction: 'error' } as const
@@ -37,6 +39,11 @@ export interface SweepPass {
const date = (ms: number) => ({ $date: new Date(ms).toISOString() })
const startOfDay = (now: Date) => Math.floor(now.getTime() / DAY) * DAY
+/** The database user may not install [RECEIPT_RULE] (production's app user may not run collMod, 10-29). */
+export class ReceiptRuleRefused extends Error {
+ constructor() { super('The database user is not authorized to install the receipt rule (collMod)') }
+}
+
/** Installs [RECEIPT_RULE] unless it is there already. collMod locks the collection only for the change itself; a receipt
* write in flight finishes first, under the old rule, and every later write is checked. */
export async function ensureReceiptRule(db: PrismaClient): Promise<void> {
@@ -45,7 +52,12 @@ export async function ensureReceiptRule(db: PrismaClient): Promise<void> {
const options = answer.cursor.firstBatch[0]?.options ?? {}
if ((Object.keys(RECEIPT_RULE) as Array<keyof typeof RECEIPT_RULE>)
.every(key => JSON.stringify(options[key]) === JSON.stringify(RECEIPT_RULE[key]))) return
- await db.$runCommandRaw({ collMod: COLLECTION, ...RECEIPT_RULE } as never)
+ try {
+ await db.$runCommandRaw({ collMod: COLLECTION, ...RECEIPT_RULE } as never)
+ } catch (error) {
+ if (notAuthorized(error)) throw new ReceiptRuleRefused()
+ throw error
+ }
}
async function anyReceipt(db: PrismaClient, filter: Record<string, unknown>): Promise<boolean> {
@@ -82,13 +94,14 @@ export async function sweepOldReceipts(db: PrismaClient, now = new Date(), optio
return { removed, done }
}
-/** Runs a pass a minute in the singleton worker until nothing is left to remove, then stops; a restart checks once more. */
-export function startReceiptSweep(db: PrismaClient, settings: Partial<SweepOptions & { everyMs: number; now: () => Date;
- log: Pick<typeof logger, 'info' | 'warn'> }> = {}): () => void {
- const { everyMs, now, log, ...options } = { ...RECEIPT_SWEEP, now: () => new Date(), log: logger, ...settings }
- let running = false, total = 0
+/** Runs a pass a minute in the singleton worker until nothing is left to remove, then stops; a restart checks once more.
+ * While the database refuses the rule, nothing is removed: it says so once and asks again every [refusedEveryMs]. */
+export function startReceiptSweep(db: PrismaClient, settings: Partial<SweepOptions & { everyMs: number; refusedEveryMs: number;
+ now: () => Date; log: Pick<typeof logger, 'info' | 'warn'> }> = {}): () => void {
+ const { everyMs, refusedEveryMs, now, log, ...options } = { ...RECEIPT_SWEEP, now: () => new Date(), log: logger, ...settings }
+ let running = false, total = 0, refusedUntil = 0, warned = false
const run = async () => {
- if (running) return
+ if (running || now().getTime() < refusedUntil) return
running = true
try {
const pass = await sweepOldReceipts(db, now(), options)
@@ -99,6 +112,12 @@ export function startReceiptSweep(db: PrismaClient, settings: Partial<SweepOptio
log.info('[usage-receipts] done: no old receipts left', { removedSinceStart: total })
}
} catch (error) {
+ if (error instanceof ReceiptRuleRefused) {
+ refusedUntil = now().getTime() + refusedEveryMs
+ if (!warned) log.warn('[usage-receipts] waiting: the database user may not install the receipt rule (collMod); nothing is removed, checking hourly')
+ warned = true
+ return
+ }
log.warn('[usage-receipts] pass failed; retrying next minute', { error: error instanceof Error ? error.message : String(error) })
} finally {
running = false