mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work.
> - Before a run starts its provider, Paperclip prepares the task
workspace through a workspace provider plugin.
> - A provider can answer a polled preparation with the closed reason
`storage_pending`. This means that the project's storage is still being
prepared. The provider gives this answer before any workspace effect.
> - Setup recorded this answer as a generic `setup_failed`. It scheduled
no retry. The task waited for a person to press Retry, and Sentry
received an error for each attempt.
> - This pull request gives the answer its own transient setup code,
`workspace_storage_pending`. Setup then retries the same workspace
within the bounded bootstrap budget.
> - The benefit is that a task continues by itself when the project's
storage becomes ready, and Sentry gets a report only when no retry owns
recovery.
## Linked Issues or Issue Description
Refs #19249 (the same retry for a stopped plugin worker)
Refs #21510 (the same deferred Sentry report for a saved-sandbox resume
outage)
**What happened?**
On staging, runs failed in the preparing stage with "Project storage is
still being prepared; retry the same workspace shortly". Each run waited
about 3.5 minutes in the polled preparation. Then the provider answered
`storage_pending`. The run got `setup_failed` with
`workspaceProviderFailure: { kind: "provider", reason:
"storage_pending", retryable: true }` and `executionRecovery: { kind:
"bootstrap", providerWorkStarted: false }`. No retry was scheduled. A
person pressed Retry several times, and each attempt sent a new Sentry
error.
**Expected behavior**
No workspace effect or provider work started, and the provider marks the
reason retryable. The run must retry within the bounded bootstrap
budget, as it does for `plugin_worker_unavailable`. Sentry must get a
report only when no retry is scheduled.
**Steps to reproduce**
1. Use a project whose workspace storage is still being provisioned.
2. Wake an agent whose legacy run prepares a workspace through that
provider.
3. The provider answers `storage_pending` at the end of its wait. The
run fails with `setup_failed`. No retry is scheduled.
**Paperclip version or commit**
master at 7ce7d99f8a.
## What Changed
- `workspace-provider-failure.ts`: add `WORKSPACE_STORAGE_PENDING_CODE`
(`workspace_storage_pending`) and `findWorkspaceStoragePendingFailure`.
The finder matches only a provider answer with the closed reason
`storage_pending`.
- `run-completion.ts`: map that answer to `workspace_storage_pending`.
Do this only before provider dispatch and only for a legacy run with no
native session or runner instance. These are the same guards as for the
sibling transient codes.
- `failed-preparation-retry.ts`: add the code to the transient
preparation codes, so setup schedules the bounded bootstrap retry. An
accepted interaction keeps its own continuation retry, as for the
sibling codes.
- Retry, legacy recovery, recovery service and telemetry: treat the code
like the other bootstrap-only transient codes. The stranded-task notice
has a title and a next action for it.
- `run-failure-report.ts`: wait for the retry decision before the Sentry
report (`workspaceStoragePendingRetry`), with the same handshake as
`sandboxResumeRetry`. A scheduled retry is not reported. An unscheduled
or exhausted retry is reported.
- Other retryable provider reasons (`remote_incomplete`,
`context_changed`, `deadline_exceeded`) do not change. These reasons can
follow a started effect, so they keep `setup_failed`.
## Verification
- Red: the new `run-completion` test failed on the unchanged code. A
fresh `storage_pending` answer got `setup_failed`, the same code that
staging recorded.
- Green: the same test passes. A fresh answer gets
`workspace_storage_pending`, `providerWorkStarted: false` and a bounded
retry. A started or native run keeps `setup_failed` and gets no retry.
Provider text does not reach the run.
- New test: `remote_incomplete`, `context_changed` and
`deadline_exceeded` answers keep `setup_failed` and get no retry.
- New report tests: a pending or scheduled decision sends no Sentry
report. An unscheduled decision, an unknown decision, `all` mode, and
another error code are reported once, without provider text.
- `heartbeat-retry-scheduling`: the new code is bounded across a restart
and honors resume, pause, Stop and a blocking dependency.
- `vitest run src/services/heartbeat/run-completion.test.ts
src/services/__tests__/run-failure-report-sandbox-resume.test.ts`: 190
passed.
- `vitest run src/__tests__/heartbeat-retry-scheduling.test.ts -t
"bootstrap retries across restart"`: 20 passed.
- `vitest run src/__tests__/workspace-provider-failure.test.ts
src/__tests__/agent-task-run-telemetry.test.ts`: 61 passed.
- Review fix: the escalated `workspace_storage_pending` notice no longer
says that Paperclip retried automatically, because recovery can show it
when no retry was scheduled. A new notice test failed on the old wording
and passes now (`workspace-provider-failure.test.ts`: 7 passed).
- `PAPERCLIP_TYPECHECK_LOW_MEMORY=1 pnpm --filter @paperclipai/server
typecheck`: all seven layers pass. (A single-program `tsc -p server` run
was stopped by the memory limit of the local machine; the layered check
is the documented low-memory path.)
## Risks
- Low risk. The retry uses the existing bounded bootstrap budget (two
retries). When the budget is exhausted, the existing board recovery
action takes over and Sentry gets the report.
- The retry applies only before provider dispatch on a legacy run. A
native run or a started run keeps the old behavior.
- A wrapper that drops the error `cause` keeps the old `setup_failed`
behavior. It does not cause a wrong retry.
- Project storage that never becomes ready still needs a fix in the
provider. This change only stops a short wait from needing a person.
> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code
execution in Claude Code.
## 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: devinfoley <139239+devinfoley@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
178 lines
9.3 KiB
TypeScript
178 lines
9.3 KiB
TypeScript
import {
|
|
JsonRpcCallError,
|
|
PLUGIN_RPC_ERROR_CODES,
|
|
readWorkspaceOperationFailureReason,
|
|
type PluginWorkspaceOperationFailureReason,
|
|
} from "@paperclipai/plugin-sdk";
|
|
import { HttpError } from "../errors.js";
|
|
import { findPluginWorkerUnavailableError, PluginWorkerUnavailableError } from "./plugin-worker-unavailable-error.js";
|
|
|
|
/**
|
|
* How a `workspaceOperation` call failed.
|
|
* - `worker_unavailable`: the worker was not running, so no request was sent.
|
|
* - `timeout`: no reply arrived in time; the operation may still have run.
|
|
* - `worker_exited`: the worker stopped with the call pending; the operation may have run.
|
|
* - `transport`: any other failure before a reply; the outcome is unknown.
|
|
* - `provider`: the provider replied with an error.
|
|
*/
|
|
export type WorkspaceProviderFailureKind = "worker_unavailable" | "timeout" | "worker_exited" | "transport" | "provider";
|
|
|
|
const INCOMPLETE = "Workspace storage operation is incomplete; retry the same workspace";
|
|
|
|
/** Core owns every message. Provider text and values never reach the run, the log or the API. */
|
|
const REASONS: Record<PluginWorkspaceOperationFailureReason, { message: string; retryable: boolean }> = {
|
|
unsupported_operation: { retryable: false,
|
|
message: "Workspace storage provider does not support this workspace operation" },
|
|
configuration_unavailable: { retryable: false,
|
|
message: "Workspace storage provider configuration is unavailable; an operator must fix it before retrying" },
|
|
credential_unavailable: { retryable: false,
|
|
message: "Workspace storage provider cannot use the project's Git credential; reconnect it before retrying" },
|
|
storage_grant_unavailable: { retryable: false,
|
|
message: "Project storage access is unavailable or out of date; reconcile the project's storage before retrying" },
|
|
binding_mismatch: { retryable: false,
|
|
message: "Saved workspace storage no longer matches this workspace; retrying the same workspace will not help" },
|
|
storage_pending: { retryable: true,
|
|
message: "Project storage is still being prepared; retry the same workspace shortly" },
|
|
runtime_unavailable: { retryable: true,
|
|
message: "The selected runtime is not ready; retry the same workspace once it is ready" },
|
|
runtime_connection_invalid: { retryable: false,
|
|
message: "The selected runtime's saved connection is invalid; reprovision the runtime before retrying" },
|
|
remote_incomplete: { retryable: true, message: INCOMPLETE },
|
|
context_changed: { retryable: true,
|
|
message: "Workspace storage changed during the operation; retry the same workspace" },
|
|
deadline_exceeded: { retryable: true,
|
|
message: "Workspace storage operation expired; retry the same workspace" },
|
|
};
|
|
|
|
/** A failed `workspaceOperation` call, with a bounded kind and reason. */
|
|
export class WorkspaceProviderFailureError extends HttpError {
|
|
readonly kind: WorkspaceProviderFailureKind;
|
|
readonly reason: PluginWorkspaceOperationFailureReason | null;
|
|
readonly retryable: boolean;
|
|
|
|
constructor(input: { kind: WorkspaceProviderFailureKind; reason?: PluginWorkspaceOperationFailureReason | null;
|
|
retryable: boolean; message: string; cause?: unknown }) {
|
|
const reason = input.reason ?? null;
|
|
super(409, input.message, { code: "workspace_provider_failed", kind: input.kind, reason, retryable: input.retryable });
|
|
this.name = "WorkspaceProviderFailureError";
|
|
this.kind = input.kind;
|
|
this.reason = reason;
|
|
this.retryable = input.retryable;
|
|
if (input.cause !== undefined) this.cause = input.cause;
|
|
}
|
|
|
|
/** Bounded fields for run results and logs. */
|
|
summary() {
|
|
return { kind: this.kind, reason: this.reason, retryable: this.retryable };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Classify a rejected `workspaceOperation` call. A provider error never passes
|
|
* its message or data through; only a reason from the SDK's closed set does.
|
|
*/
|
|
export function workspaceProviderCallFailure(error: unknown): WorkspaceProviderFailureError {
|
|
const unavailable = findPluginWorkerUnavailableError(error);
|
|
// Keep the refusal as the cause so setup can retry it: no request reached the provider.
|
|
if (unavailable) return new WorkspaceProviderFailureError({ kind: "worker_unavailable", retryable: true,
|
|
message: "Workspace storage provider is not running; retry the same workspace", cause: unavailable });
|
|
if (!(error instanceof JsonRpcCallError)) return new WorkspaceProviderFailureError({ kind: "transport", retryable: true, message: INCOMPLETE });
|
|
if (error.code === PLUGIN_RPC_ERROR_CODES.TIMEOUT) return new WorkspaceProviderFailureError({ kind: "timeout", retryable: true,
|
|
message: "Workspace storage operation timed out; retry the same workspace" });
|
|
if (error.code === PLUGIN_RPC_ERROR_CODES.WORKER_UNAVAILABLE) return new WorkspaceProviderFailureError({ kind: "worker_exited", retryable: true,
|
|
message: "Workspace storage provider stopped during the operation; retry the same workspace" });
|
|
const reason = readWorkspaceOperationFailureReason(error);
|
|
if (!reason) return new WorkspaceProviderFailureError({ kind: "provider", retryable: true,
|
|
message: "Workspace storage provider reported a failure; retry the same workspace" });
|
|
return new WorkspaceProviderFailureError({ kind: "provider", reason, ...REASONS[reason] });
|
|
}
|
|
|
|
/**
|
|
* Before a call to a ready provider: a worker that is missing, or not running and without
|
|
* `workspaceOperation`, refuses the call retryably. A restart removes or replaces the worker
|
|
* while the plugin stays ready, and its methods are unknown until it runs. A running worker
|
|
* without the method is an unsupported provider, so this returns null for it.
|
|
*/
|
|
export function workspaceWorkerRefusal(pluginId: string, worker: { status?: string; supportedMethods?: readonly string[] } | undefined) {
|
|
if (worker?.supportedMethods?.includes("workspaceOperation") || worker?.status === "running") return null;
|
|
return workspaceProviderCallFailure(new PluginWorkerUnavailableError({ pluginId, workerStatus: worker?.status ?? null,
|
|
message: `Cannot call "workspaceOperation" — worker for "${pluginId}" is ${worker?.status ?? "not registered"}` }));
|
|
}
|
|
|
|
/** Find a provider call failure through setup's error wrappers. */
|
|
export function findWorkspaceProviderFailure(error: unknown): WorkspaceProviderFailureError | null {
|
|
for (let cause = error, depth = 0; cause && depth < 4; cause = (cause as { cause?: unknown }).cause, depth++) {
|
|
if (cause instanceof WorkspaceProviderFailureError) return cause;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Setup failure code for a workspace provider that reported its project storage
|
|
* still pending. The provider answers this reason before it starts any workspace
|
|
* effect, so setup retries the same workspace within the bounded bootstrap budget.
|
|
*/
|
|
export const WORKSPACE_STORAGE_PENDING_CODE = "workspace_storage_pending";
|
|
|
|
/** Find a provider's pending-storage answer through setup's error wrappers. */
|
|
export function findWorkspaceStoragePendingFailure(error: unknown): WorkspaceProviderFailureError | null {
|
|
const failure = findWorkspaceProviderFailure(error);
|
|
return failure?.kind === "provider" && failure.reason === "storage_pending" ? failure : null;
|
|
}
|
|
|
|
export type WorkspaceProviderFailureSummary = {
|
|
kind: WorkspaceProviderFailureKind;
|
|
reason: PluginWorkspaceOperationFailureReason | null;
|
|
retryable: boolean;
|
|
};
|
|
|
|
const KINDS: ReadonlySet<WorkspaceProviderFailureKind> = new Set([
|
|
"worker_unavailable", "timeout", "worker_exited", "transport", "provider",
|
|
]);
|
|
|
|
const KIND_MESSAGES: Record<Exclude<WorkspaceProviderFailureKind, "provider">, string> = {
|
|
worker_unavailable: "Workspace storage provider is not running",
|
|
timeout: "Workspace storage operation timed out",
|
|
worker_exited: "Workspace storage provider stopped during the operation",
|
|
transport: "Workspace storage operation is incomplete",
|
|
};
|
|
|
|
/** Read the bounded workspace provider failure recorded on a failed run's `resultJson`. */
|
|
export function readWorkspaceProviderFailure(
|
|
resultJson: unknown,
|
|
): WorkspaceProviderFailureSummary | null {
|
|
if (!resultJson || typeof resultJson !== "object" || Array.isArray(resultJson)) return null;
|
|
const raw = (resultJson as Record<string, unknown>).workspaceProviderFailure;
|
|
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
const record = raw as Record<string, unknown>;
|
|
if (typeof record.kind !== "string" || !KINDS.has(record.kind as WorkspaceProviderFailureKind)) return null;
|
|
if (typeof record.retryable !== "boolean") return null;
|
|
const reason = typeof record.reason === "string" && Object.hasOwn(REASONS, record.reason)
|
|
? (record.reason as PluginWorkspaceOperationFailureReason)
|
|
: null;
|
|
return {
|
|
kind: record.kind as WorkspaceProviderFailureKind,
|
|
reason,
|
|
retryable: record.retryable,
|
|
};
|
|
}
|
|
|
|
/** Resolve a closed, Core-owned human-readable message for a saved workspace provider failure or reason key. */
|
|
export function workspaceProviderFailureMessage(
|
|
failureOrReason: WorkspaceProviderFailureSummary | string | null | undefined,
|
|
): string | null {
|
|
if (!failureOrReason) return null;
|
|
if (typeof failureOrReason === "string") {
|
|
return Object.hasOwn(REASONS, failureOrReason)
|
|
? REASONS[failureOrReason as PluginWorkspaceOperationFailureReason].message
|
|
: null;
|
|
}
|
|
if (failureOrReason.reason && Object.hasOwn(REASONS, failureOrReason.reason)) {
|
|
return REASONS[failureOrReason.reason].message;
|
|
}
|
|
if (failureOrReason.kind !== "provider") {
|
|
return KIND_MESSAGES[failureOrReason.kind];
|
|
}
|
|
return null;
|
|
}
|