diff --git a/apps/server/test/system/thread-pruning-sweep.test.ts b/apps/server/test/system/thread-pruning-sweep.test.ts index 44f336ec5..768b2e7d7 100644 --- a/apps/server/test/system/thread-pruning-sweep.test.ts +++ b/apps/server/test/system/thread-pruning-sweep.test.ts @@ -6,6 +6,7 @@ import { events, getNextThreadPruningPolicy, getThreadEventRewriteGeneration, + pendingInteractions, threadPruningCursors, threads, } from "@bb/db"; @@ -77,6 +78,57 @@ const UNTIMED_SWEEP_LIMITS: ThreadPruningSweepLimits = { }; describe("thread pruning sweep", () => { + it.each(["active", "starting", "pending", "resolving"] as const)( + "prunes settled history while another thread is %s, then revisits it when idle", + async (activity) => { + await withTestHarness(async (harness) => { + const busyThread = seed(harness, 3); + const settledThread = seed(harness, 3); + if (activity === "active" || activity === "starting") { + harness.db + .update(threads) + .set({ status: activity }) + .where(eq(threads.id, busyThread.id)) + .run(); + } else { + harness.db + .insert(pendingInteractions) + .values({ + id: "pruning-interaction", + threadId: busyThread.id, + status: activity, + payload: "{}", + createdAt: 1, + updatedAt: 1, + }) + .run(); + } + const sequences = (threadId: string) => + harness.db + .select({ sequence: events.sequence }) + .from(events) + .where(eq(events.threadId, threadId)) + .orderBy(events.sequence) + .all() + .map((event) => event.sequence); + await runThreadPruningSweep(harness.deps, UNTIMED_SWEEP_LIMITS); + expect(sequences(busyThread.id)).toEqual([1, 2, 3]); + expect(sequences(settledThread.id)).toEqual([3]); + harness.db + .update(threads) + .set({ status: "idle" }) + .where(eq(threads.id, busyThread.id)) + .run(); + harness.db + .delete(pendingInteractions) + .where(eq(pendingInteractions.threadId, busyThread.id)) + .run(); + await runThreadPruningSweep(harness.deps, UNTIMED_SWEEP_LIMITS); + expect(sequences(busyThread.id)).toEqual([3]); + }); + }, + ); + it("skips busy work and rechecks activity after a committed advance", async () => { await withTestHarness(async (harness) => { const thread = seed(harness, 1200);