|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { eq, sql } from "drizzle-orm"; |
| 3 | +import { startRetentionSweeps } from "../src/audit-retention"; |
| 4 | +import { |
| 5 | + createPageFrameStore, |
| 6 | + FRAME_RETENTION_MS, |
| 7 | +} from "../src/computer/page-frames"; |
| 8 | +import { createDatabase } from "../src/db/client"; |
| 9 | +import { computerPageFrame } from "../src/db/schema"; |
| 10 | +import { TEST_POOL } from "./support/database"; |
| 11 | + |
| 12 | +/** |
| 13 | + * The screenshots have to be able to stop growing, in every deployment rather than in one. |
| 14 | + * |
| 15 | + * Their reaper had a single caller, `scripts/cull-idle-computers.ts`, which refuses to run unless the |
| 16 | + * provider is `sandbox` and is scheduled only by the chart's culler CronJob, which renders only in |
| 17 | + * that mode. Compose, the all-in-one image and the chart's own default of `computers.mode: shared` |
| 18 | + * therefore wrote a row per navigation and removed none, ever. |
| 19 | + * |
| 20 | + * Against a real database because the interval arithmetic and the batching are both SQL. |
| 21 | + */ |
| 22 | + |
| 23 | +const databaseUrl = |
| 24 | + process.env.DATABASE_URL ?? |
| 25 | + "postgres://openbot:openbot@localhost:5432/openbot"; |
| 26 | +const database = createDatabase(databaseUrl, TEST_POOL); |
| 27 | +const store = createPageFrameStore(database); |
| 28 | + |
| 29 | +const COMPUTER = `frame-retention-${crypto.randomUUID().slice(0, 8)}`; |
| 30 | + |
| 31 | +async function frame(daysAgo: number, index: number): Promise<void> { |
| 32 | + await store.save({ |
| 33 | + computerId: COMPUTER, |
| 34 | + toolCallId: `turn-${index}`, |
| 35 | + url: `https://example.com/${index}`, |
| 36 | + title: `page ${index}`, |
| 37 | + frame: "iVBORw0KGgo=", |
| 38 | + }); |
| 39 | + await database |
| 40 | + .update(computerPageFrame) |
| 41 | + .set({ capturedAt: sql`now() - make_interval(days => ${daysAgo})` }) |
| 42 | + .where(eq(computerPageFrame.toolCallId, `turn-${index}`)); |
| 43 | +} |
| 44 | + |
| 45 | +const kept = () => |
| 46 | + database |
| 47 | + .select({ toolCallId: computerPageFrame.toolCallId }) |
| 48 | + .from(computerPageFrame) |
| 49 | + .where(eq(computerPageFrame.computerId, COMPUTER)); |
| 50 | + |
| 51 | +afterEach(async () => { |
| 52 | + await database |
| 53 | + .delete(computerPageFrame) |
| 54 | + .where(eq(computerPageFrame.computerId, COMPUTER)); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("page frame retention", () => { |
| 58 | + test("a deployment that has not configured audit retention still sweeps frames", async () => { |
| 59 | + await frame(90, 1); |
| 60 | + await frame(31, 2); |
| 61 | + await frame(1, 3); |
| 62 | + |
| 63 | + /* |
| 64 | + * `undefined` is the whole point of this case. It is what a deployment that never set |
| 65 | + * `AUDIT_RETENTION_DAYS` has, and the sweeper used to return before starting a timer at all, |
| 66 | + * which is how the frames went unswept everywhere the culler does not run. |
| 67 | + */ |
| 68 | + const sweeps = startRetentionSweeps(databaseUrl, undefined, store, { |
| 69 | + firstRunMs: 10, |
| 70 | + intervalMs: 3_600_000, |
| 71 | + }); |
| 72 | + try { |
| 73 | + for (let attempt = 0; attempt < 100; attempt += 1) { |
| 74 | + if ((await kept()).length === 1) break; |
| 75 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 76 | + } |
| 77 | + } finally { |
| 78 | + sweeps.stop(); |
| 79 | + } |
| 80 | + |
| 81 | + expect((await kept()).map((row) => row.toolCallId)).toEqual(["turn-3"]); |
| 82 | + }); |
| 83 | + |
| 84 | + test("purge removes everything past the window and nothing inside it", async () => { |
| 85 | + await frame(40, 1); |
| 86 | + await frame(29, 2); |
| 87 | + |
| 88 | + await store.purge(FRAME_RETENTION_MS); |
| 89 | + expect((await kept()).map((row) => row.toolCallId)).toEqual(["turn-2"]); |
| 90 | + }); |
| 91 | + |
| 92 | + test("purge past its batch size removes every eligible row", async () => { |
| 93 | + for (let index = 1; index <= 205; index += 1) await frame(45, index); |
| 94 | + |
| 95 | + await store.purge(FRAME_RETENTION_MS); |
| 96 | + expect(await kept()).toHaveLength(0); |
| 97 | + }); |
| 98 | + |
| 99 | + test("purge leaves a frame inside the window alone", async () => { |
| 100 | + await frame(1, 1); |
| 101 | + |
| 102 | + await store.purge(FRAME_RETENTION_MS); |
| 103 | + expect(await kept()).toHaveLength(1); |
| 104 | + }); |
| 105 | + |
| 106 | + /* |
| 107 | + * Asserted on what survives for this computer rather than on what `purge` returns: the sweep is |
| 108 | + * deployment-wide by design, so its count moves with whatever else is in the table. |
| 109 | + * |
| 110 | + * Two replicas sweeping at once, which is the ordinary case: this half takes no advisory lock, |
| 111 | + * because a delete keyed on age run twice removes the same rows once. The risk it has to be shown |
| 112 | + * not to have is double-counting or deadlocking on the same `ctid`s. |
| 113 | + */ |
| 114 | + test("two sweeps at once remove each row once and neither stalls", async () => { |
| 115 | + for (let index = 1; index <= 400; index += 1) await frame(45, index); |
| 116 | + |
| 117 | + await Promise.all([ |
| 118 | + store.purge(FRAME_RETENTION_MS), |
| 119 | + store.purge(FRAME_RETENTION_MS), |
| 120 | + ]); |
| 121 | + |
| 122 | + expect(await kept()).toHaveLength(0); |
| 123 | + }); |
| 124 | +}); |
0 commit comments