import { type Context, fauxAssistantMessage, fauxProvider, fauxToolCall, } from "@earendil-works/pi-ai"; import { SettingsManager } from "@earendil-works/pi-coding-agent"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createTestSession, type TestSession } from "../../../test/harness"; import decayExtension from "../index"; function preparation() { return { firstKeptEntryId: "kept-entry", messagesToSummarize: [ { role: "user", content: [{ type: "text", text: "Keep the exact path src/main.ts" }], timestamp: 1, }, ], turnPrefixMessages: [], isSplitTurn: false, tokensBefore: 42_000, previousSummary: undefined, fileOps: { read: new Set(["README.md"]), written: new Set(), edited: new Set(["src/main.ts"]), }, settings: { enabled: true, reserveTokens: 16_384, keepRecentTokens: 20_000, }, }; } function compactionEvent( signal = new AbortController().signal, overrides: Record = {}, ) { return { type: "session_before_compact" as const, preparation: preparation(), branchEntries: [], customInstructions: "focus on exact paths", reason: "manual" as const, willRetry: false, signal, ...overrides, }; } function classifierMessage() { return fauxAssistantMessage( fauxToolCall("record_chunk", { chunk: 1, of: 1, atoms: [ { key: "path:src/main.ts", text: "Continue work in src/main.ts", kind: "exact", status: "active", }, ], }), { stopReason: "toolUse" }, ); } describe("Decay real extension hook", () => { let testSession: TestSession | undefined; afterEach(() => { testSession?.dispose(); testSession = undefined; vi.restoreAllMocks(); }); async function setup() { testSession = await createTestSession({ extensionFactories: [decayExtension], }); const faux = fauxProvider({ provider: "openai", models: [ { id: "gpt-4o", maxTokens: 16_384 }, { id: "classifier", maxTokens: 16_384 }, { id: "summarizer", maxTokens: 8_192 }, ], }); testSession.session.extensionRunner .createContext() .modelRegistry.registerProvider(faux.provider); return { testSession, faux }; } it("classifies, merges, summarizes, persists details, and preserves Pi's tail", async () => { const { testSession, faux } = await setup(); expect(testSession.session.extensionRunner.createContext().mode).not.toBe( "tui", ); const streamSimple = vi.spyOn(faux.provider, "streamSimple"); const stream = vi.spyOn(faux.provider, "stream"); const contexts: Context[] = []; faux.setResponses([ (context) => { contexts.push(context); return classifierMessage(); }, (context) => { contexts.push(context); return fauxAssistantMessage("## Goal\n\nContinue implementation."); }, ]); const result = await testSession.session.extensionRunner.emit( compactionEvent(), ); expect(result.compaction).toEqual( expect.objectContaining({ firstKeptEntryId: "kept-entry", tokensBefore: 42_000, estimatedTokensAfter: expect.any(Number), summary: expect.stringContaining("Continue implementation"), }), ); expect(result.compaction.summary).toContain( "\nREADME.md\n", ); expect(result.compaction.summary).toContain( "\nsrc/main.ts\n", ); expect(result.compaction.details.decay).toEqual( expect.objectContaining({ version: 1, chunkCount: 1, keepRecentTokens: 20_000, atoms: [expect.objectContaining({ key: "path:src/main.ts" })], }), ); expect(result.compaction.usage.totalTokens).toBeGreaterThan(0); expect(contexts[0].tools).toHaveLength(1); expect(JSON.stringify(contexts[0])).toContain("focus on exact paths"); expect(JSON.stringify(contexts[1])).toContain("allocation"); expect(streamSimple).toHaveBeenCalledTimes(2); expect(stream).not.toHaveBeenCalled(); await testSession.session.extensionRunner.emit({ type: "session_compact", compactionEntry: { ...result.compaction, type: "compaction", id: "compact", parentId: "parent", timestamp: new Date().toISOString(), }, fromExtension: true, reason: "manual", willRetry: false, }); expect(testSession.events.uiCallsFor("notify").at(-1)?.args.at(0)).toMatch( /Decay · gpt-4o · 1\/1 classified · 20,000 kept/, ); }); it("uses a separately configured summarizer model", async () => { const { testSession, faux } = await setup(); vi.spyOn(SettingsManager, "create").mockReturnValue( SettingsManager.inMemory({ decay: { model: "openai/classifier", summarizerModel: "openai/summarizer", }, }), ); const streamSimple = vi.spyOn(faux.provider, "streamSimple"); faux.setResponses([ classifierMessage(), fauxAssistantMessage("## Goal\n\nSeparate summarizer."), ]); const result = await testSession.session.extensionRunner.emit( compactionEvent(), ); expect(result.compaction.summary).toContain("Separate summarizer"); expect(streamSimple.mock.calls.map(([model]) => model.id)).toEqual([ "classifier", "summarizer", ]); expect(streamSimple.mock.calls[1]?.[2]?.maxTokens).toBe(8_192); }); it("runs a passive fixed-height widget without taking editor input", async () => { const { testSession, faux } = await setup(); faux.setResponses([ classifierMessage(), fauxAssistantMessage("## Goal\n\nWidget path."), ]); const runner = testSession.session.extensionRunner; const baseContext = runner.createContext(); const terminalWrite = vi.fn(); const custom = vi.fn(); const setEditorText = vi.fn(); let widgetOptions: unknown; let widgetCleared = false; let component: | { render(width: number): string[]; dispose?(): void } | undefined; runner.createContext = () => ({ ...baseContext, mode: "tui", hasUI: true, ui: { ...baseContext.ui, custom, setEditorText, getEditorText: () => "steering draft", setWidget: ( _key: string, content: Function | undefined, options: unknown, ) => { if (!content) { widgetCleared = true; return; } widgetOptions = options; component = content( { terminal: { rows: 30, write: terminalWrite }, requestRender: vi.fn(), }, { fg: (_color: string, text: string) => text, bold: (text: string) => text, }, ); }, }, }); const result = await runner.emit(compactionEvent()); const rendered = component?.render(80) ?? []; expect(result.compaction.summary).toContain("Widget path"); expect(widgetOptions).toEqual({ placement: "aboveEditor" }); expect(widgetCleared).toBe(true); expect(rendered).toHaveLength(8); expect(rendered.join("\n")).toContain("DECAY.EXE"); expect(rendered.join("\n")).toContain("Enter queues steer"); expect(custom).not.toHaveBeenCalled(); expect(setEditorText).not.toHaveBeenCalled(); expect(terminalWrite).not.toHaveBeenCalled(); }); it("returns nothing so Pi defaults when classification coverage is incomplete", async () => { const { testSession, faux } = await setup(); faux.setResponses([fauxAssistantMessage("no tool records")]); expect( await testSession.session.extensionRunner.emit(compactionEvent()), ).toBeUndefined(); expect( testSession.events.uiCallsFor("notify").at(-1)?.args.at(0), ).toContain("using default compaction"); }); it("persists an agent-invisible diagnostic before provider fallback", async () => { const { testSession, faux } = await setup(); faux.setResponses([ fauxAssistantMessage([], { stopReason: "error", errorMessage: "controlled provider failure", }), ]); const messagesBefore = JSON.stringify(testSession.session.messages); expect( await testSession.session.extensionRunner.emit(compactionEvent()), ).toBeUndefined(); expect( testSession.events.uiCallsFor("notify").at(-1)?.args.at(0), ).toContain("controlled provider failure"); const diagnostic = testSession.session.sessionManager .getBranch() .find( (entry) => entry.type === "custom" && entry.customType === "decay-diagnostic", ); expect(diagnostic).toMatchObject({ type: "custom", customType: "decay-diagnostic", data: { version: 1, outcome: "fallback", stage: "classifier", model: expect.any(String), reason: "controlled provider failure", timestamp: expect.any(Number), }, }); expect(JSON.stringify(testSession.session.messages)).toBe(messagesBefore); expect(JSON.stringify(diagnostic)).not.toContain("stack"); }); it("returns nothing when the summarizer emits a tool call", async () => { const { testSession, faux } = await setup(); faux.setResponses([ classifierMessage(), fauxAssistantMessage(fauxToolCall("unexpected", {}), { stopReason: "toolUse", }), ]); expect( await testSession.session.extensionRunner.emit(compactionEvent()), ).toBeUndefined(); }); it("reuses prior keys for paraphrased repeated compactions", async () => { const { testSession, faux } = await setup(); faux.setResponses([ classifierMessage(), fauxAssistantMessage("## Goal\n\nFirst."), ]); const first = await testSession.session.extensionRunner.emit( compactionEvent(), ); let secondClassifierContext: Context | undefined; faux.setResponses([ (context) => { secondClassifierContext = context; return classifierMessage(); }, fauxAssistantMessage("## Goal\n\nSecond."), ]); const second = await testSession.session.extensionRunner.emit( compactionEvent(new AbortController().signal, { preparation: { ...preparation(), messagesToSummarize: [ { role: "user", content: [ { type: "text", text: "Resume implementation using the same src/main.ts location", }, ], timestamp: 2, }, ], previousSummary: first.compaction.summary, fileOps: { read: new Set(["second.md"]), written: new Set(), edited: new Set(), }, }, branchEntries: [ { type: "compaction", id: "first", parentId: null, timestamp: new Date().toISOString(), ...first.compaction, }, ], }), ); expect(second.compaction.details.decay.atoms[0]).toEqual( expect.objectContaining({ seenCount: 2, firstSeen: 1, lastSeen: 2 }), ); expect(secondClassifierContext).toBeDefined(); const classifierPayload = JSON.parse( ( (secondClassifierContext as Context).messages[0].content as Array<{ text: string; }> )[0].text, ); expect(classifierPayload.priorCatalog).toEqual([ expect.objectContaining({ key: "path:src/main.ts", text: "Continue work in src/main.ts", }), ]); expect(classifierPayload.priorCatalog[0]).not.toHaveProperty("seenCount"); expect(classifierPayload.chunks[0].text).toContain( "Resume implementation using the same src/main.ts location", ); expect(second.compaction.summary).toContain("README.md"); expect(second.compaction.summary).toContain("second.md"); expect(second.compaction.summary).toContain("src/main.ts"); }); it.each([ ["threshold", false], ["overflow", true], ] as const)( "preserves %s compaction semantics while replacing only the summary", async (reason, willRetry) => { const { testSession, faux } = await setup(); faux.setResponses([ classifierMessage(), fauxAssistantMessage("## Goal\n\nContinue."), ]); const result = await testSession.session.extensionRunner.emit( compactionEvent(new AbortController().signal, { reason, willRetry }), ); expect(result.compaction.firstKeptEntryId).toBe("kept-entry"); expect(result.compaction.tokensBefore).toBe(42_000); }, ); it("classifies discarded split-turn prefix separately while preserving the prepared suffix boundary", async () => { const { testSession, faux } = await setup(); faux.setResponses([ fauxAssistantMessage( [ fauxToolCall("record_chunk", { chunk: 1, of: 2, atoms: [ { key: "history", text: "Prior history", kind: "state", status: "active", }, ], }), fauxToolCall("record_chunk", { chunk: 2, of: 2, atoms: [ { key: "split", text: "Discarded turn prefix", kind: "state", status: "active", }, ], }), ], { stopReason: "toolUse" }, ), fauxAssistantMessage("## Goal\n\nContinue split turn."), ]); const splitPreparation = preparation(); splitPreparation.isSplitTurn = true; splitPreparation.turnPrefixMessages = [ { role: "assistant", content: [{ type: "text", text: "discarded assistant prefix" }], api: "faux", provider: "faux", model: "faux", usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, }, }, stopReason: "stop", timestamp: 2, }, ] as never; const result = await testSession.session.extensionRunner.emit( compactionEvent(new AbortController().signal, { preparation: splitPreparation, }), ); expect(result.compaction.firstKeptEntryId).toBe("kept-entry"); expect(result.compaction.details.decay.chunkCount).toBe(2); expect( result.compaction.details.decay.atoms.map( (atom: { key: string }) => atom.key, ), ).toEqual(["history", "split"]); }); it("returns cancellation instead of starting default compaction after abort", async () => { const { testSession, faux } = await setup(); faux.setResponses([classifierMessage()]); const controller = new AbortController(); controller.abort(); expect( await testSession.session.extensionRunner.emit( compactionEvent(controller.signal), ), ).toEqual({ cancel: true }); expect( testSession.session.sessionManager .getBranch() .some( (entry) => entry.type === "custom" && entry.customType === "decay-diagnostic", ), ).toBe(false); }); });