diff --git a/src/agents/pi-extensions/compaction-safeguard.test.ts b/src/agents/pi-extensions/compaction-safeguard.test.ts index 509bbdd25b2..f9bffadbe37 100644 --- a/src/agents/pi-extensions/compaction-safeguard.test.ts +++ b/src/agents/pi-extensions/compaction-safeguard.test.ts @@ -1490,7 +1490,7 @@ describe("compaction-safeguard extension model fallback", () => { apiKey: null, }); - expect(result).toEqual({ cancel: true }); + expect(result).toBeUndefined(); // KEY ASSERTION: Prove the fallback path was exercised // The handler should have called getApiKey with runtime.model (via ctx.model ?? runtime?.model) @@ -1501,7 +1501,7 @@ describe("compaction-safeguard extension model fallback", () => { expect(retrieved?.model).toEqual(model); }); - it("cancels compaction when both ctx.model and runtime.model are undefined", async () => { + it("falls back to built-in compaction when both ctx.model and runtime.model are undefined", async () => { const sessionManager = stubSessionManager(); // Do NOT set runtime.model (both ctx.model and runtime.model will be undefined) @@ -1516,11 +1516,25 @@ describe("compaction-safeguard extension model fallback", () => { apiKey: null, }); - expect(result).toEqual({ cancel: true }); + expect(result).toBeUndefined(); // Verify early return: getApiKey should NOT have been called when both models are missing expect(getApiKeyMock).not.toHaveBeenCalled(); }); + + it("falls back to built-in compaction when API key is missing (returns undefined, not cancel)", async () => { + const sessionManager = stubSessionManager(); + const model = createAnthropicModelFixture(); + setCompactionSafeguardRuntime(sessionManager, { model }); + const mockEvent = createCompactionEvent({ messageText: "test", tokensBefore: 500 }); + const { result, getApiKeyMock } = await runCompactionScenario({ + sessionManager, + event: mockEvent, + apiKey: null, + }); + expect(result).toBeUndefined(); + expect(getApiKeyMock).toHaveBeenCalledWith(model); + }); }); describe("compaction-safeguard double-compaction guard", () => { @@ -1670,7 +1684,7 @@ describe("compaction-safeguard double-compaction guard", () => { event: mockEvent, apiKey: null, }); - expect(result).toEqual({ cancel: true }); + expect(result).toBeUndefined(); expect(getApiKeyMock).toHaveBeenCalled(); }); }); diff --git a/src/agents/pi-extensions/compaction-safeguard.ts b/src/agents/pi-extensions/compaction-safeguard.ts index 92332140656..e470931ae02 100644 --- a/src/agents/pi-extensions/compaction-safeguard.ts +++ b/src/agents/pi-extensions/compaction-safeguard.ts @@ -761,15 +761,13 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void { "was not called and model was not passed through runtime registry.", ); } - return { cancel: true }; + return undefined; } const apiKey = await ctx.modelRegistry.getApiKey(model); if (!apiKey) { - log.warn( - "Compaction safeguard: no API key available; cancelling compaction to preserve history.", - ); - return { cancel: true }; + log.warn("Compaction safeguard: no API key available; falling back to built-in compaction."); + return undefined; } try {