|
| 1 | +import { type AssistantMessage, fauxAssistantMessage } from "@ponythewhite/base-context-ai"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { createTestResourceLoader } from "../utilities.js"; |
| 4 | +import { createHarness } from "./harness.js"; |
| 5 | + |
| 6 | +describe("default automatic compaction across recursive session depths", () => { |
| 7 | + it.each([0, 1, 2, 3])("ninety percent (depth %i)", async (rlmDepth) => { |
| 8 | + const harness = await createHarness({ |
| 9 | + rlmDepth, |
| 10 | + persistSession: true, |
| 11 | + models: [{ id: "faux-1", contextWindow: 272_000 }], |
| 12 | + tools: [], |
| 13 | + resourceLoader: { ...createTestResourceLoader(), getSystemPrompt: () => "You are a test assistant." }, |
| 14 | + settings: { autoRefine: { enabled: false } }, |
| 15 | + }); |
| 16 | + try { |
| 17 | + expect(harness.settingsManager.getCompactionSettings()).toEqual({ |
| 18 | + enabled: true, |
| 19 | + reserveTokens: 16_384, |
| 20 | + keepRecentTokens: 20_000, |
| 21 | + }); |
| 22 | + harness.setResponses([ |
| 23 | + () => fauxAssistantMessage("Historical work recorded."), |
| 24 | + () => fauxAssistantMessage("Below the threshold."), |
| 25 | + () => fauxAssistantMessage("Above the threshold."), |
| 26 | + () => fauxAssistantMessage("Ninety-percent checkpoint summary."), |
| 27 | + ]); |
| 28 | + |
| 29 | + await harness.session.prompt(`Historical work: ${"old ".repeat(30_000)}`); |
| 30 | + const seedUsage = (harness.session.messages.at(-1) as AssistantMessage).usage; |
| 31 | + // Faux counts new chars/4 in both input and cacheWrite; cached input counts once. |
| 32 | + await harness.session.prompt("x".repeat((240_000 - seedUsage.input - seedUsage.cacheRead) * 2)); |
| 33 | + const belowTokens = (await harness.session.getContextUsage())!.tokens!; |
| 34 | + expect(belowTokens).toBeGreaterThan(239_800); |
| 35 | + expect(belowTokens).toBeLessThan(244_800); |
| 36 | + expect(harness.eventsOfType("compaction_start")).toHaveLength(0); |
| 37 | + |
| 38 | + const belowUsage = (harness.session.messages.at(-1) as AssistantMessage).usage; |
| 39 | + await harness.session.prompt("y".repeat((245_000 - belowUsage.input - belowUsage.cacheRead) * 2)); |
| 40 | + await harness.session.waitForHeadlessIdle(); |
| 41 | + const outcomes = harness.eventsOfType("compaction_end"); |
| 42 | + expect(outcomes).toHaveLength(1); |
| 43 | + expect(outcomes[0]).toMatchObject({ |
| 44 | + reason: "threshold", |
| 45 | + result: { summary: "Ninety-percent checkpoint summary." }, |
| 46 | + aborted: false, |
| 47 | + }); |
| 48 | + expect(outcomes[0].result!.tokensBefore).toBeGreaterThan(244_800); |
| 49 | + expect(outcomes[0].result!.tokensBefore).toBeLessThan(245_800); |
| 50 | + expect( |
| 51 | + (await harness.sessionManager.readEntries()).filter((entry) => entry.type === "compaction"), |
| 52 | + ).toHaveLength(1); |
| 53 | + expect(harness.getPendingResponseCount()).toBe(0); |
| 54 | + } finally { |
| 55 | + await harness.cleanup(); |
| 56 | + } |
| 57 | + }); |
| 58 | +}); |
0 commit comments