test(agents): share overflow retry compaction fixture

This commit is contained in:
Peter Steinberger
2026-02-19 08:53:56 +00:00
parent b41fd20741
commit 745068a597
3 changed files with 51 additions and 30 deletions

View File

@@ -50,7 +50,7 @@ vi.mock("../pi-embedded-helpers.js", async () => {
import { compactEmbeddedPiSessionDirect } from "./compact.js";
import { log } from "./logger.js";
import { runEmbeddedPiAgent } from "./run.js";
import { makeAttemptResult } from "./run.overflow-compaction.fixture.js";
import { makeAttemptResult, mockOverflowRetrySuccess } from "./run.overflow-compaction.fixture.js";
import { runEmbeddedAttempt } from "./run/attempt.js";
import type { EmbeddedRunAttemptResult } from "./run/types.js";
import {
@@ -87,20 +87,9 @@ describe("overflow compaction in run loop", () => {
});
it("retries after successful compaction on context overflow promptError", async () => {
const overflowError = new Error("request_too_large: Request size exceeds model context window");
mockedRunEmbeddedAttempt
.mockResolvedValueOnce(makeAttemptResult({ promptError: overflowError }))
.mockResolvedValueOnce(makeAttemptResult({ promptError: null }));
mockedCompactDirect.mockResolvedValueOnce({
ok: true,
compacted: true,
result: {
summary: "Compacted session",
firstKeptEntryId: "entry-5",
tokensBefore: 150000,
},
mockOverflowRetrySuccess({
runEmbeddedAttempt: mockedRunEmbeddedAttempt,
compactDirect: mockedCompactDirect,
});
const result = await runEmbeddedPiAgent(baseParams);

View File

@@ -21,3 +21,46 @@ export function makeAttemptResult(
...overrides,
};
}
type MockRunEmbeddedAttempt = {
mockResolvedValueOnce: (value: EmbeddedRunAttemptResult) => unknown;
};
type MockCompactDirect = {
mockResolvedValueOnce: (value: {
ok: true;
compacted: true;
result: {
summary: string;
firstKeptEntryId: string;
tokensBefore: number;
};
}) => unknown;
};
export function mockOverflowRetrySuccess(params: {
runEmbeddedAttempt: MockRunEmbeddedAttempt;
compactDirect: MockCompactDirect;
overflowMessage?: string;
}) {
const overflowError = new Error(
params.overflowMessage ?? "request_too_large: Request size exceeds model context window",
);
params.runEmbeddedAttempt.mockResolvedValueOnce(
makeAttemptResult({ promptError: overflowError }),
);
params.runEmbeddedAttempt.mockResolvedValueOnce(makeAttemptResult({ promptError: null }));
params.compactDirect.mockResolvedValueOnce({
ok: true,
compacted: true,
result: {
summary: "Compacted session",
firstKeptEntryId: "entry-5",
tokensBefore: 150000,
},
});
return overflowError;
}

View File

@@ -2,7 +2,7 @@ import "./run.overflow-compaction.mocks.shared.js";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { compactEmbeddedPiSessionDirect } from "./compact.js";
import { runEmbeddedPiAgent } from "./run.js";
import { makeAttemptResult } from "./run.overflow-compaction.fixture.js";
import { mockOverflowRetrySuccess } from "./run.overflow-compaction.fixture.js";
import { runEmbeddedAttempt } from "./run/attempt.js";
const mockedRunEmbeddedAttempt = vi.mocked(runEmbeddedAttempt);
@@ -14,20 +14,9 @@ describe("runEmbeddedPiAgent overflow compaction trigger routing", () => {
});
it("passes trigger=overflow when retrying compaction after context overflow", async () => {
const overflowError = new Error("request_too_large: Request size exceeds model context window");
mockedRunEmbeddedAttempt
.mockResolvedValueOnce(makeAttemptResult({ promptError: overflowError }))
.mockResolvedValueOnce(makeAttemptResult({ promptError: null }));
mockedCompactDirect.mockResolvedValueOnce({
ok: true,
compacted: true,
result: {
summary: "Compacted session",
firstKeptEntryId: "entry-5",
tokensBefore: 150000,
},
mockOverflowRetrySuccess({
runEmbeddedAttempt: mockedRunEmbeddedAttempt,
compactDirect: mockedCompactDirect,
});
await runEmbeddedPiAgent({