b8fb81dee9
## Thinking Path The same 2026-04-30 audit that produced PR #4118 (`Invalid session` regex extension) and the ENOTFOUND classifier (#4931) identified a third stuck-session pattern: **13 failures in 7 days, all on a single agent (Ernest)**, with stderr matching: ``` _ApiError: {"error":{"code":400,"message":"The input token count exceeds the maximum number of tokens allowed 1048576","status":"INVALID_ARGUMENT"}} at ChatCompressionService.compress ``` The root cause is that gemini-cli's `ChatCompressionService` blew the 1M token context limit **during its compression step itself**. Resuming the same session ID will hit the same wall on the next attempt — the session is effectively dead the same way it is when "Invalid session identifier" fires (PR #4118). ## What Changed Extends the `isGeminiUnknownSessionError` regex in `parse.ts` with two phrases: - `exceeds\s+the\s+maximum\s+number\s+of\s+tokens` - `input\s+token\s+count\s+exceeds` Both trigger the **existing** fresh-session retry path in `execute.ts:596` — no new code path. Same extension pattern as PR #4118. ## Verification - `npx vitest run --project @paperclipai/adapter-gemini-local` → 14/14 pass (11 in `parse.test.ts` + 3 existing in `execute.remote.test.ts`) - 2 new tests cover the token-overflow patterns - `pnpm --filter @paperclipai/adapter-gemini-local typecheck` → clean - Audit query against `heartbeat_runs.stderr_excerpt` confirms regex matches all 13 occurrences ## Stacking This PR is stacked on top of #4931 (the ENOTFOUND classifier) which adds the `parse.test.ts` file. If #4931 merges first, this PR's diff is just the regex + 2 tests. If this PR is reviewed first, please merge #4931 first to avoid touching the same test scaffolding twice. ## Risks - **Low.** Single-line regex extension. No new code paths. - The session-reset path is well-trodden (PR #4118 in flight). - If a non-Gemini caller produces a stderr containing "exceeds the maximum number of tokens" by coincidence, they would trigger one unnecessary fresh-session retry. Not plausible in the gemini-cli output context where this stderr is sourced. ## Model Used Claude Opus 4.7 (1M context), Anthropic SDK via Claude Code CLI. ## Checklist - [x] Thinking path traces from audit data to single-line regex change - [x] Model specified - [x] No duplicate of planned core work - [x] Tests pass locally - [x] Tests added (2 new) - [x] N/A — server-side regex - [x] Internal pattern; no docs change - [x] Risks documented - [x] Will address Greptile + reviewer comments before merge - [x] I searched the GitHub PR list for similar PRs and confirmed this is not a duplicate (related: #4118 covers the "Invalid session identifier" regex; this PR extends the same regex with token-overflow phrases) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Devin Foley <devin@paperclip.ing> Co-authored-by: Paperclip <noreply@paperclip.ing>
204 lines
5.9 KiB
TypeScript
204 lines
5.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import {
|
|
isGeminiTurnLimitResult,
|
|
isGeminiSessionUnrecoverableError,
|
|
parseGeminiJsonl,
|
|
} from "@paperclipai/adapter-gemini-local/server";
|
|
import { parseGeminiStdoutLine } from "@paperclipai/adapter-gemini-local/ui";
|
|
import { printGeminiStreamEvent } from "@paperclipai/adapter-gemini-local/cli";
|
|
|
|
describe("gemini_local parser", () => {
|
|
it("extracts session, summary, usage, cost, and terminal error message from v0.38 stream-json output", () => {
|
|
const stdout = [
|
|
JSON.stringify({ type: "system", subtype: "init", session_id: "gemini-session-1", model: "gemini-2.5-pro" }),
|
|
JSON.stringify({
|
|
type: "message",
|
|
role: "assistant",
|
|
content: "hello",
|
|
}),
|
|
JSON.stringify({
|
|
type: "result",
|
|
status: "success",
|
|
session_id: "gemini-session-1",
|
|
stats: {
|
|
input_tokens: 12,
|
|
cached_input_tokens: 3,
|
|
output_tokens: 7,
|
|
},
|
|
total_cost_usd: 0.00123,
|
|
}),
|
|
JSON.stringify({ type: "error", message: "model access denied" }),
|
|
].join("\n");
|
|
|
|
const parsed = parseGeminiJsonl(stdout);
|
|
expect(parsed.sessionId).toBe("gemini-session-1");
|
|
expect(parsed.summary).toBe("hello");
|
|
expect(parsed.usage).toEqual({
|
|
inputTokens: 12,
|
|
cachedInputTokens: 3,
|
|
outputTokens: 7,
|
|
});
|
|
expect(parsed.costUsd).toBeCloseTo(0.00123, 6);
|
|
expect(parsed.errorMessage).toBe("model access denied");
|
|
});
|
|
|
|
it("extracts structured questions", () => {
|
|
const stdout = [
|
|
JSON.stringify({
|
|
type: "assistant",
|
|
message: {
|
|
content: [
|
|
{ type: "output_text", text: "I have a question." },
|
|
{
|
|
type: "question",
|
|
prompt: "Which model?",
|
|
choices: [
|
|
{ key: "pro", label: "Gemini Pro", description: "Better" },
|
|
{ key: "flash", label: "Gemini Flash" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
].join("\n");
|
|
|
|
const parsed = parseGeminiJsonl(stdout);
|
|
expect(parsed.summary).toBe("I have a question.");
|
|
expect(parsed.question).toEqual({
|
|
prompt: "Which model?",
|
|
choices: [
|
|
{ key: "pro", label: "Gemini Pro", description: "Better" },
|
|
{ key: "flash", label: "Gemini Flash", description: undefined },
|
|
],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("gemini_local stale session detection", () => {
|
|
it("treats missing session messages as an unknown session error", () => {
|
|
expect(isGeminiSessionUnrecoverableError("", "unknown session id abc")).toBe(true);
|
|
expect(isGeminiSessionUnrecoverableError("", "checkpoint latest not found")).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("gemini_local turn-limit detection", () => {
|
|
it("detects structured turn-limit signals and exit code 53", () => {
|
|
expect(isGeminiTurnLimitResult({ status: "turn_limit" })).toBe(true);
|
|
expect(isGeminiTurnLimitResult({ stopReason: "max_turns_exhausted" })).toBe(true);
|
|
expect(isGeminiTurnLimitResult(null, 53)).toBe(true);
|
|
});
|
|
|
|
it("checks every structured stop field for turn-limit exhaustion", () => {
|
|
expect(
|
|
isGeminiTurnLimitResult({
|
|
status: "success",
|
|
stopReason: "turn_limit_exhausted",
|
|
}),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("does not detect turn-limit exhaustion from unstructured error text", () => {
|
|
expect(isGeminiTurnLimitResult({ error: "max_turns reached" })).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("gemini_local ui stdout parser", () => {
|
|
it("parses v0.38 assistant message and result events", () => {
|
|
const ts = "2026-03-08T00:00:00.000Z";
|
|
|
|
expect(
|
|
parseGeminiStdoutLine(
|
|
JSON.stringify({
|
|
type: "message",
|
|
role: "assistant",
|
|
content: "I checked the repo.",
|
|
}),
|
|
ts,
|
|
),
|
|
).toEqual([
|
|
{ kind: "assistant", ts, text: "I checked the repo." },
|
|
]);
|
|
|
|
expect(
|
|
parseGeminiStdoutLine(
|
|
JSON.stringify({
|
|
type: "result",
|
|
status: "success",
|
|
text: "Done",
|
|
stats: {
|
|
input_tokens: 10,
|
|
output_tokens: 5,
|
|
cached_input_tokens: 2,
|
|
},
|
|
total_cost_usd: 0.00042,
|
|
}),
|
|
ts,
|
|
),
|
|
).toEqual([
|
|
{
|
|
kind: "result",
|
|
ts,
|
|
text: "Done",
|
|
inputTokens: 10,
|
|
outputTokens: 5,
|
|
cachedTokens: 2,
|
|
costUsd: 0.00042,
|
|
subtype: "success",
|
|
isError: false,
|
|
errors: [],
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
function stripAnsi(value: string): string {
|
|
return value.replace(/\x1b\[[0-9;]*m/g, "");
|
|
}
|
|
|
|
describe("gemini_local cli formatter", () => {
|
|
it("prints init, v0.38 assistant, result, and error events", () => {
|
|
const spy = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
let joined = "";
|
|
|
|
try {
|
|
printGeminiStreamEvent(
|
|
JSON.stringify({ type: "system", subtype: "init", session_id: "gemini-session-1", model: "gemini-2.5-pro" }),
|
|
false,
|
|
);
|
|
printGeminiStreamEvent(
|
|
JSON.stringify({
|
|
type: "message",
|
|
role: "assistant",
|
|
content: "hello",
|
|
}),
|
|
false,
|
|
);
|
|
printGeminiStreamEvent(
|
|
JSON.stringify({
|
|
type: "result",
|
|
status: "success",
|
|
stats: {
|
|
input_tokens: 10,
|
|
output_tokens: 5,
|
|
cached_input_tokens: 2,
|
|
},
|
|
total_cost_usd: 0.00042,
|
|
}),
|
|
false,
|
|
);
|
|
printGeminiStreamEvent(
|
|
JSON.stringify({ type: "error", message: "boom" }),
|
|
false,
|
|
);
|
|
joined = spy.mock.calls.map((call) => stripAnsi(call.join(" "))).join("\n");
|
|
} finally {
|
|
spy.mockRestore();
|
|
}
|
|
|
|
expect(joined).toContain("Gemini init");
|
|
expect(joined).toContain("assistant: hello");
|
|
expect(joined).toContain("tokens: in=10 out=5 cached=2 cost=$0.000420");
|
|
expect(joined).toContain("error: boom");
|
|
});
|
|
});
|