Files
PaperClipAI/packages/shared/src/run-failure-cause.ts
T
Devin FoleyandPaperclip b4cea5b5a4 fix(runner): show disk exhaustion before recovery errors (#22448)
Show the confirmed sandbox disk failure before a later lost-turn or restore error in saved run errors and both run detail views. Preserve the original diagnostics and leave scarce inodes available when preparing the existing runner relaunch reserve.

Verified focused regressions, scoped typechecks, and full CI on the reviewed revision, including Runner checks and all eight end-to-end shards. Greptile is 5/5 with all review findings resolved.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-11-20 17:08:31 -08:00

54 lines
2.8 KiB
TypeScript

/**
* `resultJson.failureCause` on a failed heartbeat run: a short code that says
* why a run with a generic error code (such as `adapter_failed`) failed, for
* example a SQLSTATE (`40001`), a transport code (`ECONNRESET`), a provider
* status (`provider_http_429`) or an error class name.
*
* The shape is closed and bounded: one identifier token of at most 80
* characters. It never carries message text, SQL, prompts, paths or secrets.
* Rows written before the field existed have no cause and read as `null`.
*/
export const RUN_FAILURE_CAUSE_MAX_LENGTH = 80;
export const RUN_FAILURE_CAUSE_PATTERN = /^[A-Za-z0-9_.:-]{1,80}$/;
export function runFailureMessage(run: { errorCode?: string | null; error?: string | null }): string | null {
if (run.errorCode === "sandbox_disk_full") {
return "The agent's computer ran out of disk space, so its runner stopped. "
+ "Ask the agent to use less disk space, then select Retry. Saved task updates remain available.";
}
return run.error ?? null;
}
/** The stored cause when it has the closed shape, else `null` (absent, legacy or malformed). */
export function normalizeRunFailureCause(value: unknown): string | null {
return typeof value === "string" && RUN_FAILURE_CAUSE_PATTERN.test(value) ? value : null;
}
/** `resultJson.failureCause` from a run's stored result, validated. */
export function readRunFailureCause(resultJson: unknown): string | null {
if (typeof resultJson !== "object" || resultJson === null || Array.isArray(resultJson)) return null;
return normalizeRunFailureCause((resultJson as Record<string, unknown>).failureCause);
}
// Drizzle wraps a failed statement as "Failed query: <sql>\nparams: <args>".
const FAILED_QUERY_MARKER = /\bFailed query:/i;
// Unwrapped statement text: a parameter placeholder plus a statement shape.
const SQL_PLACEHOLDER = /\$\d+\b/;
const SQL_STATEMENT = /\b(?:select\b[\s\S]*?\bfrom\b|insert\s+into\b|update\b[\s\S]*?\bset\b|delete\s+from\b|with\b[\s\S]*?\bas\s*\()/i;
/**
* Run error text for the board with any SQL statement and its parameters
* removed. Text before the statement (the caller's own wrapper) is kept and the
* statement is replaced by "Database query failed (<cause>)". Text without SQL
* is returned unchanged.
*/
export function withoutSqlStatementText(message: string, cause?: string | null): string {
let start = message.search(FAILED_QUERY_MARKER);
if (start < 0 && SQL_PLACEHOLDER.test(message)) start = SQL_STATEMENT.exec(message)?.index ?? -1;
if (start < 0) return message;
const prefix = message.slice(0, start).replace(/[\s:;,-]+$/, "").trim();
const safeCause = normalizeRunFailureCause(cause);
const replacement = `Database query failed${safeCause ? ` (${safeCause})` : ""}`;
return prefix ? `${prefix}: ${replacement}` : replacement;
}