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.

Usage reports: admit four writes at once per backend process, not one (#1819)

0xken152dfa50316bfc
diff --git a/backend/src/lib/productUsage.test.ts b/backend/src/lib/productUsage.test.ts
index 4f8090a75..80217601c 100644
--- a/backend/src/lib/productUsage.test.ts
+++ b/backend/src/lib/productUsage.test.ts
@@ -151,17 +151,18 @@ describe('aggregate ingestion', () => {
it('rechecks permission after waiting for another report without caching an enabled choice', async () => {
let release!: () => void
const gate = new Promise<void>(resolve => { release = resolve })
- db.$transaction.mockImplementationOnce(async fn => { await gate; return fn(db) })
- const first = ingestProductUsage('first-account', report, now)
+ // Four reports fill this process's write slots; the fifth waits for one.
+ for (let i = 0; i < 4; i++) db.$transaction.mockImplementationOnce(async fn => { await gate; return fn(db) })
+ const first = [0, 1, 2, 3].map(i => ingestProductUsage(`account-${i}`, { ...report, id: `receipt-${i}` }, now))
const queued = ingestProductUsage('opted-out-while-queued', { ...report, id: 'another-receipt' }, now)
await Promise.resolve()
- expect(db.$transaction).toHaveBeenCalledOnce()
- db.productUsagePreference.findUnique.mockResolvedValueOnce({ enabled: true, revision: 0 })
- .mockResolvedValueOnce({ enabled: false, revision: 1 })
+ expect(db.$transaction).toHaveBeenCalledTimes(4)
+ for (let i = 0; i < 4; i++) db.productUsagePreference.findUnique.mockResolvedValueOnce({ enabled: true, revision: 0 })
+ db.productUsagePreference.findUnique.mockResolvedValueOnce({ enabled: false, revision: 1 })
release()
- expect(await first).toEqual({ accepted: true })
+ expect(await Promise.all(first)).toEqual(Array(4).fill({ accepted: true }))
expect(await queued).toMatchObject({ accepted: false, reason: 'CHOICE_CHANGED' })
- expect(db.productUsageReceipt.create).toHaveBeenCalledOnce()
+ expect(db.productUsageReceipt.create).toHaveBeenCalledTimes(4)
})
it('stores only totals and an unlinked receipt, in one transaction', async () => {
expect(await ingestProductUsage('private-account', report, now)).toEqual({ accepted: true })
diff --git a/backend/src/lib/productUsageAdmission.test.ts b/backend/src/lib/productUsageAdmission.test.ts
index b7f6c2d1d..6cab8f362 100644
--- a/backend/src/lib/productUsageAdmission.test.ts
+++ b/backend/src/lib/productUsageAdmission.test.ts
@@ -4,8 +4,23 @@ import { createUsageAdmission } from './productUsageAdmission.js'
afterEach(() => vi.useRealTimers())
const deferred = () => { let resolve!: () => void; const promise = new Promise<void>(r => { resolve = r }); return { promise, resolve } }
-it('serializes concurrent transactions in arrival order and releases after an error', async () => {
- const write = createUsageAdmission(), gate = deferred(), seen: number[] = []
+it('runs four writes at once and admits the fifth when one finishes', async () => {
+ const write = createUsageAdmission(), gates = [deferred(), deferred(), deferred(), deferred()], seen: number[] = []
+ const running = gates.map((gate, i) => write(async () => { seen.push(i + 1); await gate.promise; return i + 1 }))
+ const fifth = write(async () => { seen.push(5); return 5 })
+ await Promise.resolve()
+ expect(seen).toEqual([1, 2, 3, 4])
+ gates[2].resolve()
+ expect(await running[2]).toBe(3)
+ await Promise.resolve()
+ expect(seen).toEqual([1, 2, 3, 4, 5])
+ expect(await fifth).toBe(5)
+ for (const gate of gates) gate.resolve()
+ expect(await Promise.all(running)).toEqual([1, 2, 3, 4])
+})
+
+it('with one slot, serializes concurrent transactions in arrival order and releases after an error', async () => {
+ const write = createUsageAdmission(32, 2000, 1), gate = deferred(), seen: number[] = []
const first = write(async () => { seen.push(1); await gate.promise; throw new Error('fixture failure') })
const failure = expect(first).rejects.toThrow('fixture failure')
const second = write(async () => { seen.push(2); return 'second' })
@@ -18,19 +33,19 @@ it('serializes concurrent transactions in arrival order and releases after an er
expect(seen).toEqual([1, 2, 3])
})
-it('bounds waiting work and rejects overflow without executing it', async () => {
- const write = createUsageAdmission(1), gate = deferred(), skipped = vi.fn()
- const first = write(() => gate.promise), second = write(async () => 'accepted')
+it('bounds waiting work behind every running slot and rejects overflow without executing it', async () => {
+ const write = createUsageAdmission(1, 2000, 2), gate = deferred(), skipped = vi.fn()
+ const running = [write(() => gate.promise), write(() => gate.promise)], queued = write(async () => 'accepted')
await expect(write(skipped)).rejects.toMatchObject({ code: 'USAGE_BUSY' })
expect(skipped).not.toHaveBeenCalled()
gate.resolve()
- expect(await second).toBe('accepted'); await first
+ expect(await queued).toBe('accepted'); await Promise.all(running)
})
it('removes expired waiters; no late write, timer, or stuck admission remains', async () => {
vi.useFakeTimers()
- const write = createUsageAdmission(2, 1000), gate = deferred(), skipped = vi.fn()
- const first = write(() => gate.promise)
+ const write = createUsageAdmission(2, 1000, 2), gate = deferred(), skipped = vi.fn()
+ const first = Promise.all([write(() => gate.promise), write(() => gate.promise)])
const expired = expect(write(skipped)).rejects.toMatchObject({ code: 'USAGE_BUSY' })
await vi.advanceTimersByTimeAsync(1000)
await expired
diff --git a/backend/src/lib/productUsageAdmission.ts b/backend/src/lib/productUsageAdmission.ts
index 1159d30df..79d66d1ae 100644
--- a/backend/src/lib/productUsageAdmission.ts
+++ b/backend/src/lib/productUsageAdmission.ts
@@ -1,20 +1,21 @@
-/** Daily totals are shared rows. Serializing this process's report transactions avoids
- * making its own uploads fight over them. Other processes still use transaction retries.
+/** Daily totals are shared rows. Admitting only a few of this process's report transactions at once keeps its
+ * own uploads from piling onto them; their write conflicts are retried by the transaction. One at a time was
+ * too few: a fleet's restart after each release filled the queue and refused reports with USAGE_BUSY.
* Only measurement waits here: bounded RAM, two seconds of queueing, no product traffic. */
-export function createUsageAdmission(maxQueued = 32, waitMs = 2000) {
+export function createUsageAdmission(maxQueued = 32, waitMs = 2000, concurrency = 4) {
type Waiter = { ready: (release: () => void) => void; timer: ReturnType<typeof setTimeout> }
const waiting = new Set<Waiter>()
- let busy = false
+ let running = 0
const unavailable = () => Object.assign(new Error('Usage measurement busy'), { code: 'USAGE_BUSY' })
function release(): void {
const next = waiting.values().next().value
- if (!next) { busy = false; return }
+ if (!next) { running--; return }
waiting.delete(next)
clearTimeout(next.timer)
next.ready(release)
}
function acquire(): Promise<() => void> {
- if (!busy) { busy = true; return Promise.resolve(release) }
+ if (running < concurrency) { running++; return Promise.resolve(release) }
if (waiting.size >= maxQueued) return Promise.reject(unavailable())
return new Promise((ready, reject) => {
const waiter: Waiter = { ready, timer: setTimeout(() => {