Add overflow recovery routing regressions

This commit is contained in:
Tak Hoffman
2026-04-06 01:42:13 -05:00
committed by Peter Steinberger
parent 222cd37e33
commit 6822d828fe

View File

@@ -171,6 +171,62 @@ describe("overflow compaction in run loop", () => {
expect(result.meta.error).toBeUndefined();
});
it("retries after fallback truncation for a mixed oversized-plus-aggregate tool tail", async () => {
mockedRunEmbeddedAttempt
.mockResolvedValueOnce(
makeAttemptResult({
promptError: makeOverflowError(),
messagesSnapshot: [
{
role: "toolResult",
content: [{ type: "text", text: "x".repeat(80_000) }],
} as unknown as EmbeddedRunAttemptResult["messagesSnapshot"][number],
{
role: "toolResult",
content: [{ type: "text", text: "alpha beta gamma delta ".repeat(800) }],
} as unknown as EmbeddedRunAttemptResult["messagesSnapshot"][number],
{
role: "toolResult",
content: [{ type: "text", text: "alpha beta gamma delta ".repeat(800) }],
} as unknown as EmbeddedRunAttemptResult["messagesSnapshot"][number],
],
}),
)
.mockResolvedValueOnce(makeAttemptResult({ promptError: null }));
mockedCompactDirect.mockResolvedValueOnce({
ok: false,
compacted: false,
reason: "nothing to compact",
});
mockedSessionLikelyHasOversizedToolResults.mockReturnValue(true);
mockedTruncateOversizedToolResultsInSession.mockResolvedValueOnce({
truncated: true,
truncatedCount: 2,
});
const result = await runEmbeddedPiAgent(baseParams);
expect(mockedCompactDirect).toHaveBeenCalledTimes(1);
expect(mockedSessionLikelyHasOversizedToolResults).toHaveBeenCalledWith(
expect.objectContaining({
messages: expect.arrayContaining([
expect.objectContaining({ role: "toolResult" }),
expect.objectContaining({ role: "toolResult" }),
expect.objectContaining({ role: "toolResult" }),
]),
}),
);
expect(mockedTruncateOversizedToolResultsInSession).toHaveBeenCalledWith(
expect.objectContaining({ sessionFile: "/tmp/session.json" }),
);
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2);
expect(mockedLog.info).toHaveBeenCalledWith(
expect.stringContaining("Truncated 2 tool result(s)"),
);
expect(result.meta.error).toBeUndefined();
});
it("retries without hitting compaction when attempt-level preflight truncation already handled the overflow", async () => {
mockedRunEmbeddedAttempt
.mockResolvedValueOnce(
@@ -196,6 +252,37 @@ describe("overflow compaction in run loop", () => {
expect(result.meta.error).toBeUndefined();
});
it("falls back to compaction when early truncate-only recovery does not help", async () => {
mockedRunEmbeddedAttempt
.mockResolvedValueOnce(
makeAttemptResult({
promptError: makeOverflowError(
"Context overflow: prompt too large for the model (precheck).",
),
preflightRecovery: { route: "compact_only" },
}),
)
.mockResolvedValueOnce(makeAttemptResult({ promptError: null }));
mockedCompactDirect.mockResolvedValueOnce(
makeCompactionSuccess({
summary: "Compacted after failed early truncation",
firstKeptEntryId: "entry-7",
tokensBefore: 155000,
}),
);
const result = await runEmbeddedPiAgent(baseParams);
expect(mockedCompactDirect).toHaveBeenCalledTimes(1);
expect(mockedTruncateOversizedToolResultsInSession).not.toHaveBeenCalled();
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2);
expect(mockedLog.warn).toHaveBeenCalledWith(
expect.stringContaining("context overflow detected (attempt 1/3); attempting auto-compaction"),
);
expect(result.meta.error).toBeUndefined();
});
it("runs post-compaction tool-result truncation before retry for mixed precheck routes", async () => {
mockedRunEmbeddedAttempt
.mockResolvedValueOnce(