Files
PaperClipAI/packages/plugins/sdk/src/workspace-operation-error.ts
T
ab79cb9cdd fix(heartbeat): wait for an updating runtime before workspace setup fails (#23779)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Before a run starts its agent, Paperclip prepares the task workspace
through a workspace provider plugin
> - A hosted agent computer can apply an update. While it updates, it
finishes its running tasks and refuses new workspaces
> - The provider can only report this refusal as a generic
`remote_incomplete` failure. Setup retries that failure two times in
about 90 seconds (#23764), but an update can wait for running tasks for
an hour
> - So each new task on that computer fails setup, reports an error, and
stops, although it only had to wait
> - This pull request adds the closed provider reason `runtime_updating`
and a separate wait lane for it. Setup holds the run and retries the
same workspace every 2 minutes for up to 1 hour. The wait does not use
the failure retry budget, and Paperclip does not report the run as a
failure while the wait continues
> - The benefit is that new tasks wait for an updating agent computer
and then start, instead of failing

## Linked Issues or Issue Description

Refs #23764, #23362

**What happened?**

A hosted agent computer refused every new workspace preparation while it
applied an update and finished its long running tasks. The provider
reported each refusal as `remote_incomplete`. Setup failed each new run.
Every retry of the same workspace failed the same way, and each failure
was reported to error tracking. On one staging stack this happened 34
times in two hours, for two agents.

**Expected behavior**

A provider can say that the selected runtime is updating. Setup then
holds the run and retries the same workspace after the update. The retry
does not spend the failure budget. Paperclip reports the failure only if
the wait ends without success.

**Steps to reproduce**

1. Start a legacy run whose workspace preparation calls a workspace
provider plugin.
2. Have the provider reject preparation because its runtime is updating.
Before this change, the only fitting reason is `remote_incomplete`.
3. Setup records `workspace_storage_incomplete` and retries two times
(30 s and 60 s). Then the task stops and the failure is reported, while
the runtime is still updating.

**Paperclip version or commit**

`master` at `30334c1f03`.

**Deployment mode**

Hosted (Paperclip Cloud agent computers). A self-hosted workspace
provider can use the same reason.

## What Changed

- `packages/plugins/sdk/src/workspace-operation-error.ts`: add the
closed reason `runtime_updating` ("the selected runtime is applying an
update and refuses new workspaces until it finishes").
- `server/src/services/workspace-provider-failure.ts`: give
`runtime_updating` a fixed, retryable message. Map it to the new setup
code `workspace_runtime_updating`. Add
`WORKSPACE_RUNTIME_UPDATING_RETRY` (reason `runtime_update_wait`, 30
attempts, 2 minutes apart). Add the code to
`WORKSPACE_STORAGE_RETRY_CODES`, so the existing deferred error report
also covers it.
- `server/src/services/heartbeat/run-completion.ts` and
`heartbeat/retries.ts`: schedule a failed pre-dispatch
`workspace_runtime_updating` run with the wait lane, and use the lane's
budget in `failedRunRetryBudgetSpent`. This is the same pattern as
`runner_revision_mismatch`.
- `server/src/services/execution-recovery-attempt.ts`,
`modules/run-dispatch/domain/wake-context.ts`,
`task-interruption-presentation.ts`: `runtime_update_wait` is an
uncharged preparation wait. An answered interaction keeps its
continuation through the wait, and the task shows a wait, not a failure.
- `failed-preparation-retry.ts`, `legacy-execution-recovery.ts`,
`recovery/service.ts`, `agent-task-run-telemetry.ts`: treat
`workspace_runtime_updating` like the other transient workspace
preparation codes.
- `recovery/stranded-notice.ts`: add a notice for a wait that ended
without success ("Agent computer was still updating").

## Verification

- Red before the change (test files only, source reverted): 9 new tests
fail in `workspace-provider-failure.test.ts`,
`execution-recovery-attempt.test.ts`, `heartbeat/retries.test.ts`,
`heartbeat/run-completion.test.ts` and
`run-failure-report-sandbox-resume.test.ts`.
- `pnpm verify` did not run: its workspace link repair failed for
`plugin-orchestration-smoke-example`, before test selection. The SDK
change also affects many packages, so CI supplies the broad coverage.
- Green after the change: the same 5 files pass (267 tests). Related
files also pass: `task-interruption-automatic-path`,
`task-interruption-query`, `wake-context`,
`stranded-notice-remote-workspace-environment`,
`task-interruption-presentation` (181 tests);
`agent-task-run-telemetry`, `legacy-continuation-authority`,
`heartbeat-retry-scheduling` (179 tests); plugin SDK
`workspace-operation-error`, `workspace-provider` (20 tests).
- `pnpm --dir server typecheck` and `pnpm --filter
@paperclipai/plugin-sdk typecheck`: pass (server typecheck run with
`PAPERCLIP_TYPECHECK_LOW_MEMORY=1` because of a local memory limit).
- CI supplies the full suite.

- Review follow-up (`cf73701742`, `d7540263bd`): update waits keep their
count in `executionRetryAccounting` (`runtimeUpdateWaits`) across other
retry lanes. An answered interaction whose update wait runs out is
escalated like an exhausted storage wait. The pinned SDK input registry
is refreshed. The two new tests failed before the fix and pass after it.
`execution-recovery-attempt`, `heartbeat/retries`,
`workspace-provider-failure` and `run-failure-report-sandbox-resume`
pass (83 tests). `heartbeat/run-completion` and `modules/wake-queue`
pass (577 tests). `tests/runner-e2e/source-sdk-public-inputs` passes (13
tests). `pnpm --dir server typecheck` passes.

## Risks

- Low risk. No schema, migration, API route or wire protocol shape
change. The reason set is closed and validated on both sides. An older
Core that receives `runtime_updating` treats it as an unknown provider
failure, as before.
- A provider must not send `runtime_updating` before the new SDK is
available to it. The SDK constructor rejects an unknown reason, so a
provider update has to follow this change.
- The wait is a bounded scheduled retry after a real refusal, not idle
polling: at most 30 retries in one hour per waiting run. Each retry is
one workspace preparation call. When the wait ends without success, the
run is reported and the task shows a named notice with a Retry action.
- Only legacy runs that fail before provider dispatch use the lane.
Started and native runs keep `setup_failed`.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), via Claude Code with
tool use (code search, file edits, shell, local test execution, GitHub
CLI).

## 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>
2026-12-04 15:31:15 -08:00

85 lines
3.7 KiB
TypeScript

/**
* Why a workspace provider refused or failed a `workspaceOperation`. The set is
* closed: Core decides the user-facing message and whether retrying the same
* workspace can help, so a provider never sends text or values across the bridge.
*/
export const WORKSPACE_OPERATION_FAILURE_REASONS = [
/** The provider does not accept this operation or request shape. */
"unsupported_operation",
/** The provider's own settings are missing or invalid. */
"configuration_unavailable",
/** A credential the operation needs (for example the project's Git credential) is unusable. */
"credential_unavailable",
/** Saved storage access grants are missing or no longer match the project. */
"storage_grant_unavailable",
/** The saved workspace binding does not match this workspace or generation. */
"binding_mismatch",
/** Project storage is still being provisioned or reconciled. */
"storage_pending",
/** The selected runtime is not ready. */
"runtime_unavailable",
/**
* The selected runtime is applying an update and refuses new workspaces until
* it finishes. Core holds the run and retries the same workspace for longer
* than a transient failure.
*/
"runtime_updating",
/** The selected runtime's saved connection is invalid. */
"runtime_connection_invalid",
/** A remote storage or runtime request failed or returned an unusable result. */
"remote_incomplete",
/** The runtime, grants or binding changed while the operation ran. */
"context_changed",
/** The operation deadline passed. */
"deadline_exceeded",
] as const;
export type PluginWorkspaceOperationFailureReason = typeof WORKSPACE_OPERATION_FAILURE_REASONS[number];
const SCHEMA = "paperclip/workspace-operation-error/v1";
function ownField(value: unknown, name: string): unknown {
if (!value || typeof value !== "object") return undefined;
try { return Object.getOwnPropertyDescriptor(value, name)?.value; }
catch { return undefined; }
}
function validateReason(value: unknown): PluginWorkspaceOperationFailureReason | null {
return WORKSPACE_OPERATION_FAILURE_REASONS.includes(value as PluginWorkspaceOperationFailureReason)
? value as PluginWorkspaceOperationFailureReason : null;
}
/**
* Throw from `onWorkspaceOperation` to tell Core why the operation failed. Any
* error with an own `workspaceFailureReason` from the closed set is forwarded the
* same way; the message stays local to the worker's RPC error text.
*/
export class PluginWorkspaceOperationError extends Error {
readonly workspaceFailureReason: PluginWorkspaceOperationFailureReason;
constructor(reason: PluginWorkspaceOperationFailureReason, message = "Workspace operation failed") {
const safe = validateReason(reason);
if (!safe) throw new TypeError("Unknown workspace operation failure reason");
super(message);
this.name = "PluginWorkspaceOperationError";
this.workspaceFailureReason = safe;
}
}
/** Worker side: copy only the closed reason, never provider text or identities. */
export function workspaceOperationErrorData(error: unknown): { schema: string; reason: PluginWorkspaceOperationFailureReason } | undefined {
const reason = validateReason(ownField(error, "workspaceFailureReason"));
return reason ? { schema: SCHEMA, reason } : undefined;
}
/** Host side: validate the envelope again after the worker RPC, through wrapper causes. */
export function readWorkspaceOperationFailureReason(error: unknown): PluginWorkspaceOperationFailureReason | null {
for (let current = error, depth = 0; current && depth < 5; current = ownField(current, "cause"), depth++) {
const data = ownField(current, "data");
if (ownField(data, "schema") === SCHEMA) {
const reason = validateReason(ownField(data, "reason"));
if (reason) return reason;
}
}
return null;
}