* fix: detect Anthropic 'Request size exceeds model context window' as context overflow Anthropic now returns 'Request size exceeds model context window' instead of the previously detected 'prompt is too long' format. This new error message was not recognized by isContextOverflowError(), causing auto-compaction to NOT trigger. Users would see the raw error twice without any recovery attempt. Changes: - Add 'exceeds model context window' and 'request size exceeds' to isContextOverflowError() detection patterns - Add tests that fail without the fix, verifying both the raw error string and the JSON-wrapped format from Anthropic's API - Add test for formatAssistantErrorText to ensure the friendly 'Context overflow' message is shown instead of the raw error Note: The upstream pi-ai package (@mariozechner/pi-ai) also needs a fix in its OVERFLOW_PATTERNS regex: /exceeds the context window/i should be changed to /exceeds.*context window/i to match both 'the' and 'model' variants for triggering auto-compaction retry. * fix(tests): remove unused imports and helper from test files Remove WorkspaceBootstrapFile references and _makeFile helper that were incorrectly copied from another test file. These caused type errors and were unrelated to the context overflow detection tests. * fix: trigger auto-compaction on context overflow promptError When the LLM rejects a request with a context overflow error that surfaces as a promptError (thrown exception rather than streamed error), the existing auto-compaction in pi-coding-agent never triggers. This happens because the error bypasses the agent's message_end → agent_end → _checkCompaction path. This fix adds a fallback compaction attempt directly in the run loop: - Detects context overflow in promptError (excluding compaction_failure) - Calls compactEmbeddedPiSessionDirect (bypassing lane queues since already in-lane) - Retries the prompt after successful compaction - Limits to one compaction attempt per run to prevent infinite loops Fixes: context overflow errors shown to user without auto-compaction attempt * style: format compact.ts and run.ts with oxfmt * fix: tighten context overflow match (#1627) (thanks @rodrigouroz) --------- Co-authored-by: Claude <claude@anthropic.com> Co-authored-by: Peter Steinberger <steipete@gmail.com>
50 lines
2.1 KiB
TypeScript
50 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { isContextOverflowError } from "./pi-embedded-helpers.js";
|
|
|
|
describe("isContextOverflowError", () => {
|
|
it("matches known overflow hints", () => {
|
|
const samples = [
|
|
"request_too_large",
|
|
"Request exceeds the maximum size",
|
|
"context length exceeded",
|
|
"Maximum context length",
|
|
"prompt is too long: 208423 tokens > 200000 maximum",
|
|
"Context overflow: Summarization failed",
|
|
"413 Request Entity Too Large",
|
|
];
|
|
for (const sample of samples) {
|
|
expect(isContextOverflowError(sample)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("matches Anthropic 'Request size exceeds model context window' error", () => {
|
|
// Anthropic returns this error format when the prompt exceeds the context window.
|
|
// Without this fix, auto-compaction is NOT triggered because neither
|
|
// isContextOverflowError nor pi-ai's isContextOverflow recognizes this pattern.
|
|
// The user sees: "LLM request rejected: Request size exceeds model context window"
|
|
// instead of automatic compaction + retry.
|
|
const anthropicRawError =
|
|
'{"type":"error","error":{"type":"invalid_request_error","message":"Request size exceeds model context window"}}';
|
|
expect(isContextOverflowError(anthropicRawError)).toBe(true);
|
|
});
|
|
|
|
it("matches 'exceeds model context window' in various formats", () => {
|
|
const samples = [
|
|
"Request size exceeds model context window",
|
|
"request size exceeds model context window",
|
|
'400 {"type":"error","error":{"type":"invalid_request_error","message":"Request size exceeds model context window"}}',
|
|
"The request size exceeds model context window limit",
|
|
];
|
|
for (const sample of samples) {
|
|
expect(isContextOverflowError(sample)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("ignores unrelated errors", () => {
|
|
expect(isContextOverflowError("rate limit exceeded")).toBe(false);
|
|
expect(isContextOverflowError("request size exceeds upload limit")).toBe(false);
|
|
expect(isContextOverflowError("model not found")).toBe(false);
|
|
expect(isContextOverflowError("authentication failed")).toBe(false);
|
|
});
|
|
});
|