mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Native runs report an unexpected terminal failure to Sentry through the run-failure reporter > - The reporter keeps an expected provider usage-limit failure (`native_provider_usage_limit`) out of Sentry, because the task takes its provider-limit wait and runs again after the reset > - A server restart can end such a run after its provider turn failed on the limit, but before its executor saves `native_provider_usage_limit`. Restart recovery then blocks the run as `provider_checkpoint_permanently_failed`, and the reporter sends it to Sentry > - On staging, one deploy restart sent seven such reports at once. Each task then took its provider-limit wait and finished after the reset, so no operator action was necessary > - This pull request re-derives the usage-limit fact from the run's last committed runner turn terminal and keeps that expected failure out of Sentry > - The benefit is that a deploy during a provider limit does not create false error reports, and real checkpoint failures are still reported ## Linked Issues or Issue Description **What happened?** A deploy restarted the server while several native runs were settling a provider turn that failed on a weekly allowance (`quota_exhausted`). Restart recovery blocked each run as `native_restart_recovery_blocked` / `provider_checkpoint_permanently_failed`. The run-failure reporter sent one Sentry event per run. **Expected behavior** A run that ended on a provider usage limit is an expected failure. The reporter does not send it to Sentry when the executor settles it normally. A restart during settlement must not change that. **Steps to reproduce** 1. Start a native run whose provider turn fails with a `quota_exhausted` or `rate_limit` provider failure. 2. Restart the server before the executor saves `native_provider_usage_limit`. 3. Restart recovery blocks the run as `provider_checkpoint_permanently_failed`, and the reporter sends a Sentry event. Refs #20149 ## What Changed - `native-provider-failure.ts`: move `nativeProviderUsageLimitFromEvent` here from `native-session-executor.ts` so that the evidence reader can use it. The executor re-exports it, so its callers do not change. - `native-provider-failure-evidence.ts`: add `readPersistedNativeTerminalUsageLimit`. It reads the run's last committed turn lifecycle event. It returns true only when that event is a `turn.failed` from the run's own runner instance, the PRP event validates against the stored row (run, session, source, sequence and payload hash), and the failure is a provider usage limit. - `run-failure-report.ts`: for an unexpected `native_restart_recovery_blocked` / `provider_checkpoint_permanently_failed` failure, skip the report when that reader returns true. A read error logs a warning and keeps the report. - Add a real-process restart-recovery test with four cases: usage limit and rate limit are not reported; an unknown failure and a later turn after the limit are still reported. ## Verification - `cd server && npx vitest run src/services/native-runtime/native-runner-restart-recovery.integration.test.ts -t "provider usage limit|pending retry|unreported"`: 5 passed. - Red proof: with only `run-failure-report.ts` reverted, the `usage-limit` and `rate-limit` cases fail (`expected ... to have a length of +0 but got 1`). The two report cases still pass. - `cd server && PAPERCLIP_TYPECHECK_LOW_MEMORY=1 pnpm run typecheck`: passed. - `npx vitest run src/services/run-failure-report src/services/native-runtime/native-provider-failure src/services/native-runtime/native-session-executor.test.ts`: 1619 passed, 1 failed. The failure (`compares export process compatibility without changing conversation identity (changed: false)`, `db.transaction is not a function`) also fails on unchanged `master` (`ce8006de3c`), so this change does not cause it. ## Risks - Low risk. The change only removes a Sentry report. It does not change run status, task status, recovery, or the provider-limit wait. - The filter needs a validated, run-bound runner terminal. A missing, tampered, foreign or later event keeps the report. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution in an agent harness. ## 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 - [x] 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: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
5.6 KiB
TypeScript
105 lines
5.6 KiB
TypeScript
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
|
import { heartbeatRunEvents, heartbeatRuns, type Db } from "@paperclipai/db";
|
|
import { parseNativeExecutionInput, validatePrpEvent, type PrpTerminalState } from "../../vendor/paperclip-runner/index.js";
|
|
import { nativeSha256 } from "./canonical.js";
|
|
import { createNativeProviderFailureObservation, nativeProviderUsageLimitFromEvent } from "./native-provider-failure.js";
|
|
|
|
function record(value: unknown): Record<string, unknown> {
|
|
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
? value as Record<string, unknown> : {};
|
|
}
|
|
|
|
async function observePersistedNativeTurnFailure(
|
|
db: Db,
|
|
run: typeof heartbeatRuns.$inferSelect,
|
|
turnId: string | null,
|
|
terminal: PrpTerminalState,
|
|
) {
|
|
if (run.runtimeMode !== "native" || !turnId || !run.nativeSessionId || !run.runnerInstanceId ||
|
|
terminal.turnTerminalState !== "failed" || terminal.runTerminalState !== "failed") return null;
|
|
let execution;
|
|
try { execution = parseNativeExecutionInput(record(run.runnerProfileJson).nativeExecutionInput); } catch { return null; }
|
|
if (execution.binding.runId !== run.id || execution.binding.companyId !== run.companyId ||
|
|
execution.binding.agentId !== run.agentId || execution.binding.issueId !== run.nativeIssueId ||
|
|
execution.session.normalizedSessionId !== run.nativeSessionId ||
|
|
execution.completionContract.id !== run.completionContractId ||
|
|
execution.completionContract.sha256 !== run.completionContractSha256) return null;
|
|
|
|
const [row] = await db.select().from(heartbeatRunEvents).where(and(
|
|
eq(heartbeatRunEvents.companyId, run.companyId), eq(heartbeatRunEvents.runId, run.id),
|
|
eq(heartbeatRunEvents.sourceInstanceId, run.runnerInstanceId), eq(heartbeatRunEvents.eventType, "turn.failed"),
|
|
sql`${heartbeatRunEvents.payload}->'prpEvent'->>'turnId' = ${turnId}`,
|
|
)).orderBy(desc(heartbeatRunEvents.seq)).limit(1);
|
|
if (!row) return null;
|
|
const parsed = validatePrpEvent(record(row.payload).prpEvent);
|
|
if (!parsed.ok || parsed.event.runId !== run.id || parsed.event.normalizedSessionId !== run.nativeSessionId ||
|
|
parsed.event.sourceKind !== "runner" || parsed.event.sourceInstanceId !== run.runnerInstanceId ||
|
|
parsed.event.sourceEventId !== row.sourceEventId || parsed.event.sourceSeq !== row.sourceSeq ||
|
|
parsed.event.eventType !== row.eventType || parsed.event.turnId !== turnId ||
|
|
row.sourcePayloadSha256 !== nativeSha256(parsed.event)) return null;
|
|
const observed = createNativeProviderFailureObservation(execution.provider);
|
|
observed.observe(parsed.event);
|
|
return observed;
|
|
}
|
|
|
|
/** Re-derive authority from immutable run-bound evidence, including after restart. */
|
|
export async function readPersistedNativeProviderFailure(
|
|
...args: Parameters<typeof observePersistedNativeTurnFailure>
|
|
) {
|
|
const [, , turnId, terminal] = args;
|
|
return (await observePersistedNativeTurnFailure(...args))?.forTerminal(turnId, terminal) ?? null;
|
|
}
|
|
|
|
/**
|
|
* Recovery can finalize before heartbeat saves the adapter result. Rebuild the
|
|
* classified failure and the diagnostics-only turn failure from one event.
|
|
*/
|
|
export async function readPersistedNativeFailureEvidence(
|
|
...args: Parameters<typeof observePersistedNativeTurnFailure>
|
|
) {
|
|
const [, , turnId, terminal] = args;
|
|
const observed = await observePersistedNativeTurnFailure(...args);
|
|
return {
|
|
providerFailure: observed?.forTerminal(turnId, terminal) ?? null,
|
|
turnFailure: observed?.unclassifiedTurnFailure(turnId, terminal) ?? null,
|
|
providerExplanation: observed?.providerFailureForTerminal(turnId, terminal) ?? null,
|
|
};
|
|
}
|
|
|
|
const TURN_LIFECYCLE_EVENT_TYPES = [
|
|
"turn.submitted", "turn.started", "turn.accepted", "turn.completed", "turn.failed", "turn.interrupted", "turn.cancelled",
|
|
] as const;
|
|
|
|
/**
|
|
* True only when the run's last committed runner turn terminal is an exhausted
|
|
* provider allowance. A restart can end such a run before its executor saves
|
|
* `native_provider_usage_limit`; this re-derives that fact from the same
|
|
* immutable, run-bound event the executor classified.
|
|
*/
|
|
export async function readPersistedNativeTerminalUsageLimit(
|
|
db: Db,
|
|
run: typeof heartbeatRuns.$inferSelect,
|
|
): Promise<boolean> {
|
|
if (run.runtimeMode !== "native" || !run.nativeSessionId || !run.runnerInstanceId) return false;
|
|
const [row] = await db.select().from(heartbeatRunEvents).where(and(
|
|
eq(heartbeatRunEvents.companyId, run.companyId), eq(heartbeatRunEvents.runId, run.id),
|
|
sql`${heartbeatRunEvents.sourceInstanceId} is not null`,
|
|
inArray(heartbeatRunEvents.eventType, [...TURN_LIFECYCLE_EVENT_TYPES]),
|
|
)).orderBy(desc(heartbeatRunEvents.seq)).limit(1);
|
|
// A later turn, or one from another runner, means this failure did not end the run.
|
|
if (!row || row.eventType !== "turn.failed" || row.sourceInstanceId !== run.runnerInstanceId) return false;
|
|
const parsed = validatePrpEvent(record(row.payload).prpEvent);
|
|
if (!parsed.ok || parsed.event.runId !== run.id || parsed.event.normalizedSessionId !== run.nativeSessionId ||
|
|
parsed.event.sourceKind !== "runner" || parsed.event.sourceInstanceId !== run.runnerInstanceId ||
|
|
parsed.event.sourceEventId !== row.sourceEventId || parsed.event.sourceSeq !== row.sourceSeq ||
|
|
parsed.event.eventType !== row.eventType || row.sourcePayloadSha256 !== nativeSha256(parsed.event)) return false;
|
|
return nativeProviderUsageLimitFromEvent(parsed.event);
|
|
}
|
|
|
|
export async function readPersistedNativeModelRejection(
|
|
...args: Parameters<typeof readPersistedNativeProviderFailure>
|
|
) {
|
|
const failure = await readPersistedNativeProviderFailure(...args);
|
|
return failure?.errorCode === "native_provider_model_rejected" ? failure : null;
|
|
}
|