test(agents): add compaction and workspace reset regressions

This commit is contained in:
Peter Steinberger
2026-02-26 17:40:29 +01:00
parent 0ec7711bc2
commit 53e30475e2
4 changed files with 78 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
* Test: before_compaction & after_compaction hook wiring
*/
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { emitAgentEvent } from "../infra/agent-events.js";
const hookMocks = vi.hoisted(() => ({
runner: {
@@ -35,6 +36,7 @@ describe("compaction hook wiring", () => {
hookMocks.runner.runBeforeCompaction.mockResolvedValue(undefined);
hookMocks.runner.runAfterCompaction.mockClear();
hookMocks.runner.runAfterCompaction.mockResolvedValue(undefined);
vi.mocked(emitAgentEvent).mockClear();
});
it("calls runBeforeCompaction in handleAutoCompactionStart", () => {
@@ -45,6 +47,7 @@ describe("compaction hook wiring", () => {
runId: "r1",
sessionKey: "agent:main:web-abc123",
session: { messages: [1, 2, 3], sessionFile: "/tmp/test.jsonl" },
onAgentEvent: vi.fn(),
},
state: { compactionInFlight: false },
log: { debug: vi.fn(), warn: vi.fn() },
@@ -67,6 +70,16 @@ describe("compaction hook wiring", () => {
expect(event?.sessionFile).toBe("/tmp/test.jsonl");
const hookCtx = beforeCalls[0]?.[1] as { sessionKey?: string } | undefined;
expect(hookCtx?.sessionKey).toBe("agent:main:web-abc123");
expect(ctx.ensureCompactionPromise).toHaveBeenCalledTimes(1);
expect(emitAgentEvent).toHaveBeenCalledWith({
runId: "r1",
stream: "compaction",
data: { phase: "start" },
});
expect(ctx.params.onAgentEvent).toHaveBeenCalledWith({
stream: "compaction",
data: { phase: "start" },
});
});
it("calls runAfterCompaction when willRetry is false", () => {
@@ -77,6 +90,7 @@ describe("compaction hook wiring", () => {
state: { compactionInFlight: true },
log: { debug: vi.fn(), warn: vi.fn() },
maybeResolveCompactionWait: vi.fn(),
incrementCompactionCount: vi.fn(),
getCompactionCount: () => 1,
};
@@ -98,6 +112,13 @@ describe("compaction hook wiring", () => {
| undefined;
expect(event?.messageCount).toBe(2);
expect(event?.compactedCount).toBe(1);
expect(ctx.incrementCompactionCount).toHaveBeenCalledTimes(1);
expect(ctx.maybeResolveCompactionWait).toHaveBeenCalledTimes(1);
expect(emitAgentEvent).toHaveBeenCalledWith({
runId: "r2",
stream: "compaction",
data: { phase: "end", willRetry: false },
});
});
it("does not call runAfterCompaction when willRetry is true", () => {
@@ -109,6 +130,7 @@ describe("compaction hook wiring", () => {
log: { debug: vi.fn(), warn: vi.fn() },
noteCompactionRetry: vi.fn(),
resetForCompactionRetry: vi.fn(),
maybeResolveCompactionWait: vi.fn(),
getCompactionCount: () => 0,
};
@@ -121,6 +143,14 @@ describe("compaction hook wiring", () => {
);
expect(hookMocks.runner.runAfterCompaction).not.toHaveBeenCalled();
expect(ctx.noteCompactionRetry).toHaveBeenCalledTimes(1);
expect(ctx.resetForCompactionRetry).toHaveBeenCalledTimes(1);
expect(ctx.maybeResolveCompactionWait).not.toHaveBeenCalled();
expect(emitAgentEvent).toHaveBeenCalledWith({
runId: "r3",
stream: "compaction",
data: { phase: "end", willRetry: true },
});
});
it("clears stale assistant usage after final compaction", () => {