mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 19:08:33 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The native Runner starts `opencode serve` for each OpenCode agent turn and reads its event stream. > - Sometimes `opencode serve` dies by SIGKILL in the middle of a turn. The run then fails with `opencode_server_exited`. > - The failure record shows `signal: SIGKILL` and `ECONNREFUSED`, but it does not show who sent the signal. > - A SIGKILL alone does not prove a kernel OOM kill. Recent failures were short turns with few or no tool calls, so the cause is not clear. > - This pull request records the kernel cgroup v2 OOM evidence when `opencode serve` dies by a signal. > - The benefit is that the next failure tells us if the kernel memory limit killed OpenCode or if something else did. ## Linked Issues or Issue Description Refs #17398 (the Legacy OpenCode adapter makes sandbox tool commands the first OOM victims). Refs #16844 (the same protection for Claude). **What happened?** On a cloud sandbox, a native OpenCode turn failed with `opencode_server_exited`. The run failure record had `streamFailure: server_exited`, `signal: SIGKILL` and `connectionError: ECONNREFUSED`, and `timeoutFired` was false. The Runner process stayed alive. The Runner sends SIGKILL to OpenCode only at shutdown or at a failed start, so the signal came from outside the Runner. The record has no kernel memory evidence, so we cannot tell if the kernel OOM killer sent it. **Expected behavior** When `opencode serve` dies by a signal, the failure record says if the kernel recorded an OOM kill in its cgroup after launch. It also gives the memory limit and the peak use. **Steps to reproduce** 1. Start a native OpenCode turn in a Linux sandbox that uses cgroup v2. 2. Send SIGKILL to the `opencode serve` process while the turn runs. 3. Read `resultJson.nativeTurnFailure` for the failed run. Before this change, it has no memory evidence. **Paperclip version or commit** `master` at6678554a5a. ## What Changed - Add `memory-kill-evidence.ts` to the OpenCode driver. It reads the process cgroup from `/proc/<pid>/cgroup` (unified v2 entry only). It records the `oom_kill` count from `memory.events` right after spawn. Later it compares the count and finds the nearest numeric `memory.max` and that cgroup `memory.peak`. It never reads outside the cgroup root. It returns nothing when cgroup v2 evidence is not readable. - Start the probe after `opencode serve` spawns, on Linux only. A new `memoryKillProbe` driver option sets other paths for tests, or `false` turns the probe off. - When the event stream fails and the process died by a signal, add `memoryKill` (`observed` or `not_observed`), `memoryLimitBytes` and `memoryPeakBytes` to the `opencodeFailure` record. - Keep the three fields in the server closed-vocabulary projection (`native-provider-failure.ts`). Only the two known `memoryKill` values and positive safe integers pass. They reach `resultJson.nativeTurnFailure` and the run failure context. - This is a diagnostic. It does not change how a turn fails or recovers. ## Verification - `cd packages/paperclip-runner && npx vitest run src/drivers/opencode/memory-kill-evidence.test.ts`: 4/4 pass. The tests cover an observed kill, no kill, no limit, no readable cgroup v2 data, cgroup v1, and a path that tries to leave the cgroup root. - `cd packages/paperclip-runner && npx vitest run src/drivers/opencode/opencode-server-driver.test.ts -t "OpenCode stops mid-turn|classifies a lost event stream"`: 2/2 pass. The SIGKILL integration test uses a fake cgroup tree. It kills OpenCode right after the turn opens, with no tool calls. It now expects `memoryKill: "observed"` and the limit. It fails when the probe is off. - `cd server && npx vitest run src/services/native-runtime/native-provider-failure.test.ts`: 63/63 pass. - These local runs used the parent commit201abacd. The patch applies to current `master` with only line offsets. CI runs the full suites on this branch. ## Risks - Low risk. The probe reads a few small files: once at spawn and once after a signal death. All read errors give no evidence. They do not throw. - On hosts without cgroup v2 (macOS, Windows, cgroup v1), the probe reports nothing. - `memoryKill: "not_observed"` means that the kernel did not count an OOM kill in the cgroup of OpenCode or its child cgroups. An OOM kill in a different cgroup (for example a parent limit with another member) can still kill OpenCode. Read the field with `memoryLimitBytes` and `memoryPeakBytes`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution in an agent sandbox. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
403 lines
28 KiB
TypeScript
403 lines
28 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type { PrpEvent, PrpTerminalState } from "../../vendor/paperclip-runner/index.js";
|
|
import { antigravityModelRefusalMessage } from "@paperclipai/shared";
|
|
import { createNativeProviderFailureObservation, nativeAcceptedResultOutlivesProviderFailure, nativeProviderUsageLimitFromEvent, NATIVE_MODEL_NOT_FOUND_DIAGNOSTIC, NATIVE_MODEL_REJECTION_DIAGNOSTIC, NATIVE_MODEL_REJECTION_MESSAGE, NATIVE_PROVIDER_OVERLOADED_MESSAGE, readNativeTurnFailureDiagnostic } from "./native-provider-failure.js";
|
|
|
|
const model = "test-model";
|
|
const response = {
|
|
type: "error", status: 400,
|
|
error: { type: "invalid_request_error", message: `The '${model}' model is not supported when using Codex with a ChatGPT account.` },
|
|
};
|
|
const event: PrpEvent = {
|
|
schema: "paperclip.prp.event.v1", runId: "run", normalizedSessionId: "session", turnId: "turn",
|
|
sourceInstanceId: "runner", sourceEventId: "failure", sourceSeq: 1, sourceKind: "runner",
|
|
eventType: "turn.failed", schemaVersion: 1, priority: 0, emittedAt: "2026-01-01T00:00:00.000Z",
|
|
payload: { status: "failed", error: { message: JSON.stringify(response), codexErrorInfo: "other" } },
|
|
};
|
|
const terminal: PrpTerminalState = {
|
|
schema: "paperclip.prp.terminal.v1", turnTerminalState: "failed", runTerminalState: "failed", reportedWorkDisposition: "blocked",
|
|
};
|
|
const observation = () => createNativeProviderFailureObservation({ kind: "codex", model });
|
|
|
|
describe("native provider failure observation", () => {
|
|
it.each(["acpx", "codex", "opencode"])("retains a typed explanation for %s without granting recovery authority", kind => {
|
|
const observed = createNativeProviderFailureObservation({ kind });
|
|
const providerFailure = { schema: "paperclip.provider-failure.v1", provider: "new-provider", phase: "turn",
|
|
category: "unknown", message: "An unfamiliar provider rejection.", retryability: "unknown" };
|
|
const failed = { ...event, payload: { status: "failed", error: { providerFailure } } };
|
|
observed.observe({ ...failed, sourceKind: "control_plane" });
|
|
expect(observed.providerFailureForTerminal("turn", terminal)).toBeNull();
|
|
observed.observe(failed);
|
|
expect(observed.providerFailureForTerminal("turn", terminal)).toEqual(providerFailure);
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
expect(observed.providerFailureForTerminal("other-turn", terminal)).toBeNull();
|
|
expect(observed.providerFailureForTerminal("turn", { ...terminal, runTerminalState: "succeeded" })).toBeNull();
|
|
});
|
|
it.each(["opencode", "codex"])("classifies an action-required model_unavailable failure from %s as a model rejection", kind => {
|
|
const observed = createNativeProviderFailureObservation({ kind });
|
|
const providerFailure = { schema: "paperclip.provider-failure.v1", provider: "openrouter", phase: "turn",
|
|
category: "model_unavailable", message: "vendor/private-model is not a valid model ID", retryability: "requires_action", httpStatus: 400 };
|
|
observed.observe({ ...event, payload: { status: "failed", error: { providerFailure } } });
|
|
const result = observed.forTerminal("turn", terminal);
|
|
expect(result).toMatchObject({ errorCode: "native_provider_model_rejected", errorMessage: NATIVE_MODEL_REJECTION_MESSAGE });
|
|
expect(JSON.stringify(result)).not.toContain("private-model");
|
|
// A closed diagnostic only, and the classified turn is not also reported as unclassified.
|
|
expect(result?.diagnostic).toEqual(expect.objectContaining({ provider: kind }));
|
|
expect(JSON.stringify(result?.diagnostic)).not.toContain("not a valid model");
|
|
expect(observed.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
expect(observed.forTerminal("other-turn", terminal)).toBeNull();
|
|
expect(observed.forTerminal("turn", { ...terminal, runTerminalState: "succeeded" })).toBeNull();
|
|
});
|
|
it.each(["transient", "unknown"])("does not treat a %s model_unavailable failure as a model rejection", retryability => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode" });
|
|
observed.observe({ ...event, payload: { status: "failed", error: { providerFailure: { schema: "paperclip.provider-failure.v1", provider: "openrouter", phase: "turn", category: "model_unavailable", message: "busy", retryability } } } });
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
});
|
|
it("classifies the API-key 404 for the configured model as a model rejection (codex 0.160.0 shape)", () => {
|
|
const apiKeyFailure = (message: string, info: unknown = { httpConnectionFailed: { httpStatusCode: 404 } }) => ({ ...event, payload: { status: "failed", error: {
|
|
message, codexErrorInfo: info, additionalDetails: null, misalignment: null } } });
|
|
const real = `unexpected status 404 Not Found: The model \`${model}\` does not exist or you do not have access to it., url: https://api.openai.com/v1/responses, cf-ray: a5833f01da2716ce-IAH, request id: req_584e320775c34b2c9c6d98af40e0ea3d`;
|
|
const observed = observation();
|
|
observed.observe(apiKeyFailure(real));
|
|
const result = observed.forTerminal("turn", terminal);
|
|
expect(result).toEqual({ errorCode: "native_provider_model_rejected", errorMessage: NATIVE_MODEL_REJECTION_MESSAGE, diagnostic: NATIVE_MODEL_NOT_FOUND_DIAGNOSTIC });
|
|
expect(JSON.stringify(result)).not.toContain(model);
|
|
expect(observed.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
expect(observed.forTerminal("other-turn", terminal)).toBeNull();
|
|
// Another model's 404, another status, or a retryable turn is not this rejection.
|
|
for (const [message, info, recoverable] of [
|
|
[real.replace(model, "other-model"), undefined, false],
|
|
[real, { httpConnectionFailed: { httpStatusCode: 500 } }, false],
|
|
[real, "other", false],
|
|
// Codex reports its retried 404s with this kind and willRetry; they never end the turn.
|
|
[real, { responseStreamDisconnected: { httpStatusCode: 404 } }, false],
|
|
[real, undefined, true],
|
|
] as const) {
|
|
const other = observation();
|
|
const failed = apiKeyFailure(message, info);
|
|
other.observe(recoverable ? { ...failed, payload: { ...failed.payload, recoverable: true } } : failed);
|
|
expect(other.forTerminal("turn", terminal)).toBeNull();
|
|
}
|
|
});
|
|
it("surfaces a structured capacity failure without copying arbitrary provider text", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "codex" });
|
|
observed.observe({ ...event, payload: { status: "failed", error: { codexErrorInfo: "serverOverloaded", message: "Private arbitrary response" } } });
|
|
expect(observed.forTerminal("turn", terminal)).toEqual({ errorCode: "native_provider_overloaded", errorMessage: NATIVE_PROVIDER_OVERLOADED_MESSAGE, diagnostic: { provider: "codex", category: "server_overloaded" } });
|
|
expect(observed.forTerminal("different-turn", terminal)).toBeNull();
|
|
expect(observed.forTerminal("turn", { ...terminal, runTerminalState: "succeeded" })).toBeNull();
|
|
});
|
|
|
|
it.each(["usageLimitExceeded", "other", "unauthorized", undefined])("does not retry unrelated provider errors (%s)", (codexErrorInfo) => {
|
|
const observed = observation();
|
|
observed.observe({ ...event, payload: { status: "failed", error: { codexErrorInfo, message: NATIVE_PROVIDER_OVERLOADED_MESSAGE } } });
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
});
|
|
it("binds the structured rejection to the failed turn and emits no response or model text", () => {
|
|
const observed = observation();
|
|
observed.observe(event);
|
|
observed.observe(event); // A byte-identical committed replay remains idempotent.
|
|
const result = observed.forTerminal("turn", terminal);
|
|
expect(result).toEqual({ errorCode: "native_provider_model_rejected", errorMessage: NATIVE_MODEL_REJECTION_MESSAGE, diagnostic: NATIVE_MODEL_REJECTION_DIAGNOSTIC });
|
|
expect(JSON.stringify(result)).not.toContain(model);
|
|
expect(JSON.stringify(result)).not.toContain("invalid_request_error");
|
|
expect(observed.forTerminal("later-turn", terminal)).toBeNull();
|
|
expect(observed.forTerminal(null, terminal)).toBeNull();
|
|
expect(observed.forTerminal("turn", { ...terminal, runTerminalState: "succeeded" })).toBeNull();
|
|
expect(observed.forTerminal("turn", { ...terminal, turnTerminalState: "cancelled" })).toBeNull();
|
|
});
|
|
|
|
it.each([
|
|
{ ...event, sourceKind: "control_plane" as const },
|
|
{ ...event, eventType: "item.completed" as const },
|
|
{ ...event, eventType: "harness.diagnostic" as const },
|
|
{ ...event, turnId: undefined },
|
|
{ ...event, payload: { ...event.payload, status: "completed" } },
|
|
{ ...event, payload: { ...event.payload, recoverable: true } },
|
|
{ ...event, payload: { status: "failed", error: { message: JSON.stringify(response), recoverable: true } } },
|
|
])("ignores non-authoritative or retryable evidence %#", (unrelated) => {
|
|
const observed = observation();
|
|
observed.observe(unrelated);
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
});
|
|
|
|
it.each([
|
|
response.error.message, "malformed JSON", " ".repeat(4_097),
|
|
JSON.stringify({ ...response, status: 401 }),
|
|
JSON.stringify({ ...response, status: "400" }),
|
|
JSON.stringify({ ...response, type: "notice" }),
|
|
JSON.stringify({ ...response, error: { ...response.error, type: "server_error" } }),
|
|
JSON.stringify({ ...response, error: { ...response.error, message: "Private arbitrary provider text" } }),
|
|
JSON.stringify({ ...response, error: { ...response.error, message: response.error.message + " Private suffix" } }),
|
|
JSON.stringify({ ...response, error: { ...response.error, message: response.error.message.replace(model, "another-model") } }),
|
|
JSON.stringify([response]), "null",
|
|
])("leaves unknown failures unchanged %#", (message) => {
|
|
const observed = observation();
|
|
observed.observe({ ...event, payload: { status: "failed", error: { message } } });
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
});
|
|
|
|
it.each([
|
|
[{ codexErrorInfo: "contextWindowExceeded", message: "Private provider text" }, { codexErrorInfo: "contextWindowExceeded" }],
|
|
[{ codexErrorInfo: { responseStreamDisconnected: { httpStatusCode: 502 } }, message: "Private" }, { codexErrorInfo: "responseStreamDisconnected", httpStatus: 502 }],
|
|
[{ codexErrorInfo: { httpConnectionFailed: { httpStatusCode: "502" } } }, { codexErrorInfo: "httpConnectionFailed" }],
|
|
[{ codexErrorInfo: { httpConnectionFailed: { httpStatusCode: 9_999 } } }, { codexErrorInfo: "httpConnectionFailed" }],
|
|
[{ codexErrorInfo: "privateUnknownKind" }, { codexErrorInfo: "unrecognized" }],
|
|
[{ codexErrorInfo: { privateUnknownKind: {} } }, { codexErrorInfo: "unrecognized" }],
|
|
[{ codexErrorInfo: { other: {}, badRequest: {} } }, { codexErrorInfo: "unrecognized" }],
|
|
[{ code: "provider_event_type_invalid", message: "Unknown canonical provider event type.", recoverable: false }, { codexErrorInfo: "absent", runnerFailureCode: "provider_event_type_invalid" }],
|
|
[{ code: "Private Code With Spaces", message: "Private" }, { codexErrorInfo: "absent", runnerFailureCode: "unrecognized" }],
|
|
[{ code: "private_prompt_slug", message: "Private" }, { codexErrorInfo: "absent", runnerFailureCode: "unrecognized" }],
|
|
[{ code: 42, message: "Private" }, { codexErrorInfo: "absent" }],
|
|
[{ message: NATIVE_PROVIDER_OVERLOADED_MESSAGE }, { codexErrorInfo: "absent" }],
|
|
])("describes an unclassified failed turn with closed vocabulary only %#", (error, expected) => {
|
|
const observed = observation();
|
|
observed.observe({ ...event, payload: { status: "failed", error } });
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
const diagnostic = observed.unclassifiedTurnFailure("turn", terminal);
|
|
expect(diagnostic).toEqual({ provider: "codex", category: "turn_failed", ...expected });
|
|
expect(JSON.stringify(diagnostic)).not.toMatch(/private/i);
|
|
expect(readNativeTurnFailureDiagnostic(diagnostic)).toEqual(diagnostic);
|
|
});
|
|
|
|
it("does not describe a classified, unfailed, or unrelated turn as unclassified", () => {
|
|
const rejected = observation();
|
|
rejected.observe(event);
|
|
expect(rejected.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
const overloaded = observation();
|
|
overloaded.observe({ ...event, payload: { status: "failed", error: { codexErrorInfo: "serverOverloaded" } } });
|
|
expect(overloaded.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
const other = observation();
|
|
other.observe({ ...event, payload: { status: "failed", error: { codexErrorInfo: "sandboxError" } } });
|
|
expect(other.unclassifiedTurnFailure("later-turn", terminal)).toBeNull();
|
|
expect(other.unclassifiedTurnFailure(null, terminal)).toBeNull();
|
|
expect(other.unclassifiedTurnFailure("turn", { ...terminal, runTerminalState: "succeeded" })).toBeNull();
|
|
expect(other.unclassifiedTurnFailure("turn", { ...terminal, turnTerminalState: "cancelled" })).toBeNull();
|
|
const claude = createNativeProviderFailureObservation({ kind: "claude", model });
|
|
claude.observe({ ...event, payload: { status: "failed", error: { codexErrorInfo: "sandboxError" } } });
|
|
expect(claude.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
});
|
|
|
|
it.each([
|
|
null, [], "turn_failed",
|
|
{ provider: "codex", category: "turn_failed", codexErrorInfo: "private-kind" },
|
|
{ provider: "claude", category: "turn_failed", codexErrorInfo: "other" },
|
|
{ provider: "codex", category: "server_overloaded", codexErrorInfo: "other" },
|
|
])("rejects a persisted turn-failure diagnostic outside the vocabulary %#", (value) => {
|
|
expect(readNativeTurnFailureDiagnostic(value)).toBeNull();
|
|
});
|
|
|
|
it("drops unbounded fields from a persisted turn-failure diagnostic", () => {
|
|
expect(readNativeTurnFailureDiagnostic({
|
|
provider: "codex", category: "turn_failed", codexErrorInfo: "badRequest", httpStatus: 1.5,
|
|
runnerFailureCode: "private_prompt_slug", message: "private provider response",
|
|
})).toEqual({ provider: "codex", category: "turn_failed", codexErrorInfo: "badRequest", runnerFailureCode: "unrecognized" });
|
|
});
|
|
|
|
it("names a lost OpenCode server without copying provider text", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
observed.observe({ ...event, payload: { status: "failed", providerTerminalObserved: false, error: {
|
|
code: "opencode_server_exited", message: "Private provider message",
|
|
opencodeFailure: { kind: "server_exited", signal: "SIGKILL", connectionError: "ECONNREFUSED", detail: "private" },
|
|
} } });
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
const diagnostic = observed.unclassifiedTurnFailure("turn", terminal);
|
|
expect(diagnostic).toEqual({
|
|
provider: "opencode", category: "turn_failed", runnerFailureCode: "opencode_server_exited",
|
|
streamFailure: "server_exited", signal: "SIGKILL", connectionError: "ECONNREFUSED",
|
|
});
|
|
expect(JSON.stringify(diagnostic)).not.toMatch(/private/i);
|
|
expect(readNativeTurnFailureDiagnostic(diagnostic)).toEqual(diagnostic);
|
|
});
|
|
|
|
it("still identifies an OpenCode turn that failed with no structured cause", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
observed.observe({ ...event, payload: { status: "failed", error: null } });
|
|
expect(observed.unclassifiedTurnFailure("turn", terminal)).toEqual({ provider: "opencode", category: "turn_failed" });
|
|
const providerError = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
providerError.observe({ ...event, payload: { status: "failed", error: { code: "private_provider_code", name: "APIError" } } });
|
|
expect(providerError.unclassifiedTurnFailure("turn", terminal)).toEqual({
|
|
provider: "opencode", category: "turn_failed", runnerFailureCode: "unrecognized",
|
|
});
|
|
});
|
|
|
|
it("drops values outside the OpenCode stream-failure vocabulary", () => {
|
|
expect(readNativeTurnFailureDiagnostic({
|
|
provider: "opencode", category: "turn_failed", runnerFailureCode: "opencode_event_stream_lost",
|
|
streamFailure: "connection_failed", connectionError: "EPRIVATE", exitCode: 300, signal: "private signal", httpStatus: 42,
|
|
})).toEqual({
|
|
provider: "opencode", category: "turn_failed", runnerFailureCode: "opencode_event_stream_lost", streamFailure: "connection_failed",
|
|
});
|
|
expect(readNativeTurnFailureDiagnostic({
|
|
provider: "opencode", category: "turn_failed", streamFailure: "private", exitCode: 137,
|
|
})).toEqual({ provider: "opencode", category: "turn_failed" });
|
|
expect(readNativeTurnFailureDiagnostic({ provider: "opencode", category: "server_overloaded" })).toBeNull();
|
|
});
|
|
|
|
it("keeps the kernel memory evidence for a killed OpenCode server", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
observed.observe({ ...event, payload: { status: "failed", error: {
|
|
code: "opencode_server_exited",
|
|
opencodeFailure: { kind: "server_exited", signal: "SIGKILL", memoryKill: "observed",
|
|
memoryLimitBytes: 8_589_934_592, memoryPeakBytes: 8_400_000_000 },
|
|
} } });
|
|
const diagnostic = observed.unclassifiedTurnFailure("turn", terminal);
|
|
expect(diagnostic).toEqual({
|
|
provider: "opencode", category: "turn_failed", runnerFailureCode: "opencode_server_exited",
|
|
streamFailure: "server_exited", signal: "SIGKILL", memoryKill: "observed",
|
|
memoryLimitBytes: 8_589_934_592, memoryPeakBytes: 8_400_000_000,
|
|
});
|
|
expect(readNativeTurnFailureDiagnostic(diagnostic)).toEqual(diagnostic);
|
|
const base = { provider: "opencode", category: "turn_failed", streamFailure: "server_exited" };
|
|
expect(readNativeTurnFailureDiagnostic({ ...base, memoryKill: "private text", memoryLimitBytes: -1,
|
|
memoryPeakBytes: "8000" })).toEqual(base);
|
|
});
|
|
|
|
it("records only known signal names for a lost OpenCode server", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
observed.observe({ ...event, payload: { status: "failed", error: {
|
|
code: "opencode_server_exited", opencodeFailure: { kind: "server_exited", signal: "SIGPRIVATE" },
|
|
} } });
|
|
const expected = { provider: "opencode", category: "turn_failed", runnerFailureCode: "opencode_server_exited", streamFailure: "server_exited" };
|
|
expect(observed.unclassifiedTurnFailure("turn", terminal)).toEqual(expected);
|
|
expect(readNativeTurnFailureDiagnostic({ ...expected, signal: "SIGPRIVATE" })).toEqual(expected);
|
|
expect(readNativeTurnFailureDiagnostic({ ...expected, signal: "SIGTERM" })).toEqual({ ...expected, signal: "SIGTERM" });
|
|
});
|
|
|
|
it("records an OpenCode upstream provider failure's closed fields without its message or code", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
const providerFailure = { schema: "paperclip.provider-failure.v1", provider: "openrouter", phase: "turn",
|
|
category: "unknown", message: "Private upstream explanation", providerCode: "private_provider_type",
|
|
httpStatus: 502, retryability: "unknown" };
|
|
observed.observe({ ...event, payload: { status: "failed", error: {
|
|
name: "UnknownError", data: { message: "Private upstream body" }, providerFailure,
|
|
} } });
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
expect(observed.providerFailureForTerminal("turn", terminal)).toEqual(providerFailure);
|
|
const diagnostic = observed.unclassifiedTurnFailure("turn", terminal);
|
|
expect(diagnostic).toEqual({
|
|
provider: "opencode", category: "turn_failed",
|
|
providerFailureCategory: "unknown", providerFailureRetryability: "unknown", httpStatus: 502,
|
|
});
|
|
expect(JSON.stringify(diagnostic)).not.toMatch(/private/i);
|
|
expect(readNativeTurnFailureDiagnostic(diagnostic)).toEqual(diagnostic);
|
|
// A malformed provider failure adds nothing, and a persisted record keeps only known values.
|
|
const malformed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
malformed.observe({ ...event, payload: { status: "failed", error: { providerFailure: { ...providerFailure, category: "private" } } } });
|
|
expect(malformed.unclassifiedTurnFailure("turn", terminal)).toEqual({ provider: "opencode", category: "turn_failed" });
|
|
expect(readNativeTurnFailureDiagnostic({ provider: "opencode", category: "turn_failed",
|
|
providerFailureCategory: "private", providerFailureRetryability: "unknown", httpStatus: 502 }))
|
|
.toEqual({ provider: "opencode", category: "turn_failed" });
|
|
expect(readNativeTurnFailureDiagnostic({ provider: "opencode", category: "turn_failed",
|
|
providerFailureCategory: "overloaded", providerFailureRetryability: "private", httpStatus: 42 }))
|
|
.toEqual({ provider: "opencode", category: "turn_failed", providerFailureCategory: "overloaded" });
|
|
});
|
|
|
|
it("names the OpenCode empty-response failure the driver writes", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "opencode", model });
|
|
observed.observe({ ...event, payload: { status: "failed", error: {
|
|
code: "opencode_empty_response", message: "OpenCode ended the turn without a final response.", retryable: false,
|
|
} } });
|
|
const diagnostic = observed.unclassifiedTurnFailure("turn", terminal);
|
|
expect(diagnostic).toEqual({ provider: "opencode", category: "turn_failed", runnerFailureCode: "opencode_empty_response" });
|
|
expect(readNativeTurnFailureDiagnostic(diagnostic)).toEqual(diagnostic);
|
|
});
|
|
|
|
it.each([{ kind: "claude", model }, { kind: "codex" }, { kind: "codex", model: "other" }])(
|
|
"does not apply a rejection to another selected provider/model (%j)", (provider) => {
|
|
const observed = createNativeProviderFailureObservation(provider);
|
|
observed.observe(event);
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("Google Antigravity provider failures", () => {
|
|
const antigravity = (route?: "api_key" | "subscription", modelId = "gemini-4-neon-high") =>
|
|
createNativeProviderFailureObservation({ kind: "acpx", agent: "antigravity", model: modelId }, route ? { antigravityRoute: route } : {});
|
|
const failed = (providerFailure: Record<string, unknown>): PrpEvent => ({ ...event, payload: { status: "failed", error: { providerFailure: {
|
|
schema: "paperclip.provider-failure.v1", provider: "antigravity", phase: "turn", ...providerFailure } } } });
|
|
const refused = failed({ category: "model_unavailable", message: "The selected model is not available for this connection.",
|
|
providerCode: "antigravity_model_unavailable", httpStatus: 429, retryability: "requires_action" });
|
|
|
|
it("names the refused model and the fix for an API key", () => {
|
|
const observed = antigravity("api_key");
|
|
observed.observe(refused);
|
|
const result = observed.forTerminal("turn", terminal);
|
|
expect(result).toMatchObject({ errorCode: "native_provider_model_rejected",
|
|
errorMessage: antigravityModelRefusalMessage("gemini-4-neon-high", "api_key") });
|
|
expect(result?.errorMessage).toContain("Gemini 4 Neon (High) is not available with this Gemini API key.");
|
|
expect(result?.errorMessage).not.toMatch(/antigravity_model_unavailable|RESOURCE_EXHAUSTED|429/);
|
|
expect(observed.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
});
|
|
|
|
it("names the refused model and the fix for a Google sign-in", () => {
|
|
const observed = antigravity("subscription", "gemini-3.8-flash-high");
|
|
observed.observe(refused);
|
|
expect(observed.forTerminal("turn", terminal)?.errorMessage)
|
|
.toBe(antigravityModelRefusalMessage("gemini-3.8-flash-high", "subscription"));
|
|
});
|
|
|
|
it("keeps the shared message when the connection route is unknown (recovered after restart)", () => {
|
|
const observed = antigravity();
|
|
observed.observe(refused);
|
|
expect(observed.forTerminal("turn", terminal)).toMatchObject({ errorCode: "native_provider_model_rejected", errorMessage: NATIVE_MODEL_REJECTION_MESSAGE });
|
|
});
|
|
|
|
it("takes the bounded capacity retry for a transient overload (503)", () => {
|
|
const observed = antigravity("api_key");
|
|
observed.observe(failed({ category: "overloaded", message: "This model is currently experiencing high demand.",
|
|
providerCode: "UNAVAILABLE", httpStatus: 503, retryability: "transient" }));
|
|
expect(observed.forTerminal("turn", terminal)).toEqual({ errorCode: "native_provider_overloaded",
|
|
errorMessage: NATIVE_PROVIDER_OVERLOADED_MESSAGE, diagnostic: { provider: "antigravity", category: "server_overloaded" } });
|
|
expect(observed.unclassifiedTurnFailure("turn", terminal)).toBeNull();
|
|
});
|
|
|
|
it("leaves rate limits, quotas and rejected keys to their own plain-language paths", () => {
|
|
for (const failure of [
|
|
{ category: "rate_limit", message: "Slow down", httpStatus: 429, retryAt: "2026-01-01T00:01:00.000Z", retryability: "transient" },
|
|
{ category: "quota_exhausted", message: "Quota exhausted", httpStatus: 429, retryability: "requires_action" },
|
|
{ category: "authentication", message: "API key not valid. Please pass a valid API key.", httpStatus: 400, retryability: "requires_action" },
|
|
]) {
|
|
const observed = antigravity("api_key");
|
|
const input = failed(failure);
|
|
observed.observe(input);
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
expect(observed.providerFailureForTerminal("turn", terminal)).toMatchObject({ category: failure.category, message: failure.message });
|
|
expect(nativeProviderUsageLimitFromEvent(input)).toBe(failure.category !== "authentication");
|
|
}
|
|
});
|
|
|
|
it("does not turn another ACPX harness's overload into an Antigravity capacity retry", () => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "acpx", agent: "claude", model: "m" });
|
|
observed.observe(failed({ provider: "claude", category: "overloaded", message: "Overloaded", httpStatus: 529, retryability: "transient" }));
|
|
expect(observed.forTerminal("turn", terminal)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("an accepted finish followed by a provider refusal", () => {
|
|
const done: PrpTerminalState = { ...terminal, reportedWorkDisposition: "done" };
|
|
const outlives = (providerFailure: Record<string, unknown>, end: PrpTerminalState = done) => {
|
|
const observed = createNativeProviderFailureObservation({ kind: "acpx", agent: "antigravity", model: "gemini-3.8-flash-low" }, { antigravityRoute: "subscription" });
|
|
observed.observe({ ...event, payload: { status: "failed", error: { providerFailure: {
|
|
schema: "paperclip.provider-failure.v1", provider: "antigravity", phase: "turn", message: "No capacity available for model gemini-3.8-flash-low on the server", ...providerFailure } } } });
|
|
return nativeAcceptedResultOutlivesProviderFailure({ terminal: end,
|
|
providerFailure: observed.forTerminal("turn", end), providerExplanation: observed.providerFailureForTerminal("turn", end) });
|
|
};
|
|
|
|
it("keeps the finish after a transient capacity refusal or rate limit", () => {
|
|
expect(outlives({ category: "overloaded", httpStatus: 503, providerCode: "UNAVAILABLE", retryability: "transient" })).toBe(true);
|
|
expect(outlives({ category: "rate_limit", httpStatus: 429, retryability: "transient" })).toBe(true);
|
|
});
|
|
|
|
it("keeps real failures and unfinished work as failures", () => {
|
|
expect(outlives({ category: "model_unavailable", providerCode: "antigravity_model_unavailable", retryability: "requires_action" })).toBe(false);
|
|
expect(outlives({ category: "quota_exhausted", retryability: "requires_action" })).toBe(false);
|
|
expect(outlives({ category: "authentication", retryability: "requires_action" })).toBe(false);
|
|
expect(outlives({ category: "overloaded", retryability: "unknown" })).toBe(false);
|
|
expect(outlives({ category: "timeout", retryability: "transient" })).toBe(false);
|
|
// Only an accepted done report outlives the refusal; other reports keep the capacity retry.
|
|
expect(outlives({ category: "overloaded", retryability: "transient" }, { ...done, reportedWorkDisposition: "needs_review" })).toBe(false);
|
|
expect(outlives({ category: "overloaded", retryability: "transient" }, { ...done, runTerminalState: "cancelled", turnTerminalState: "cancelled" })).toBe(false);
|
|
});
|
|
});
|