Files
PaperClipAI/server/src/__tests__/interrupted-preparation-hold-settle.test.ts
T
DottaandClaude Opus 5.5 535d04edb3 fix(workspaces): exclude named pipes from workspace snapshots like sockets (#22817)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A hosted server can move a workspace's files into a portable
snapshot archive, for example when the server moves to volume-free
storage
> - The archive already skips Unix sockets, because a socket is a
runtime IPC endpoint with no file contents, and records the skipped path
> - A named pipe (FIFO) is the same kind of runtime IPC endpoint, but
capture refused the whole workspace with
`remote_workspace_archive_unsupported_entry`
> - One leftover pipe in a workspace therefore blocked a whole server's
move, with no way to settle it
> - This pull request treats a named pipe like a socket: record it as
excluded and keep the files beside it
> - The benefit is that workspaces holding a stray pipe can be saved and
moved, with the skipped path still visible in the archive manifest

## Linked Issues or Issue Description

Follow-up to #19159, which excluded sockets and kept the existing
refusal of named pipes.

**What happened?**
An offline move of a server to volume-free storage rolled back. The
recorded refusal was `remote_workspace_archive_unsupported_entry` on one
workspace. The only entry types that still reach that refusal are named
pipes and device files.

**Expected behavior**
A named pipe has no portable contents, like a socket, so capture should
record it as excluded and save everything else.

**Steps to reproduce**
1. Create a workspace folder with a file and a named pipe (`mkfifo
pipe`).
2. Capture a snapshot archive of it.
3. See capture fail with `remote_workspace_archive_unsupported_entry`.

## What Changed

- `remote-workspace-snapshot-archive.ts`: a named pipe is recorded in
the archive's excluded paths, like a socket, instead of failing the
capture. The same function also builds the serialized remote capture
script, so both paths change together.
- `remote-workspace-readonly-inventory.ts`: the read-only inventory
excludes named pipes the same way, so its fingerprint matches what
capture saves.
- Device entries and unsafe links are still refused.
- The test that asserted pipe refusal now asserts the new behavior: the
pipe stays in place, adjacent work restores, and the archive manifest
lists the pipe as excluded.

## Verification

- Red: with the source change reverted, the new test `records a named
pipe as excluded and preserves the files beside it` fails.
- Green: `npx vitest run
src/services/remote-workspace-snapshot-archive.test.ts
src/services/remote-workspace-readonly-inventory*.test.ts` (in
`server/`): 3 files, 130 tests pass, using a real `mkfifo` pipe.

## Risks

- A pipe is not recreated on restore, exactly as for sockets. Its path
is visible in the existing exclusion manifest. A process that expected
the pipe to exist recreates it as it would after any restart.
- No new archive entry type, dependency, or background work.

## 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
- [ ] 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 19:12:52 -06:00

560 lines
38 KiB
TypeScript

import { spawn } from "node:child_process";
import { randomUUID } from "node:crypto";
import { and, eq, inArray, ne, sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import {
activityLog,
agents,
agentWakeupRequests,
authUsers,
companyMemberships,
companies,
createDb,
environmentLeases,
environments,
executionWorkspaces,
heartbeatRunEvents,
heartbeatRuns,
issueRecoveryActions,
issues,
} from "@paperclipai/db";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
import { currentProcessSpaceHash } from "../services/controller-process-identity.js";
import { heartbeatService } from "../services/heartbeat.js";
import { REMOTE_WORKSPACE_CAPTURE_POLICY_VERSION } from "../services/remote-workspace-snapshot-archive.js";
import { settleHeldInterruptedPreparation } from "../services/interrupted-preparation-hold-settlement.js";
import { isAbandonedPreparedRemoteWorkspace } from "../services/remote-workspace-abandoned-preparation.js";
import { inspectWorkspaceRecoverySource } from "../services/workspace-recovery-source.js";
import { getExecutionBlocker } from "../services/execution-blocker.js";
import { requestTaskRetry } from "../services/task-retry.js";
import { legacyControllerBootId } from "../services/legacy-controller-lease.js";
import { createOwnershipReportState, runOwnershipReconciliationPass } from "../services/recovery/ownership-invariant-report.js";
const support = await getEmbeddedPostgresTestSupport();
const suite = support.supported ? describe : describe.skip;
const KIND = "legacy_interrupted_preparation_hold";
async function exitedPid(): Promise<number> {
const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });
const pid = child.pid!;
await new Promise<void>(resolve => { child.once("exit", () => resolve()); child.kill("SIGKILL"); });
return pid;
}
/**
* The K2-during-preparation shape from Daytona warm wave w-20261108T142906-s9501
* (t05, run c99b3a2e): a scheduled retry was 2 s into preparation when a
* graceful shutdown interrupted it. The draining process recorded a
* preserve-without-replay hold, because its own controller was still live and
* the sandbox lease was still cleaning up. The lease was released with a
* confirmed stop moments later and the next process booted, but nothing
* re-judged the hold, so the task waited for a manual Retry.
*/
suite("held interrupted preparations on the shared ownership path", () => {
let db!: ReturnType<typeof createDb>;
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
let companyId = "", agentId = "", environmentId = "";
beforeAll(async () => {
temporary = await startEmbeddedPostgresTestDatabase("paperclip-interrupted-preparation-");
db = createDb(temporary.connectionString);
companyId = randomUUID(); agentId = randomUUID(); environmentId = randomUUID();
await db.insert(companies).values({ id: companyId, name: "Paperclip", issuePrefix: "IPH",
requireBoardApprovalForNewAgents: false });
await db.insert(agents).values({ id: agentId, companyId, name: "Coder", role: "engineer", status: "idle",
adapterType: "paperclip_runner", adapterConfig: {}, runtimeConfig: {}, permissions: {} });
await db.insert(environments).values({ id: environmentId, name: "Sandbox", driver: "sandbox", status: "active",
config: { provider: "fake" } });
await db.insert(authUsers).values({ id: "local-board", name: "Local operator", email: "local-board@example.invalid",
createdAt: new Date(), updatedAt: new Date() }).onConflictDoNothing();
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "local-board", membershipRole: "owner" });
}, 30_000);
afterEach(async () => {
await db.delete(activityLog);
await db.delete(issueRecoveryActions);
await db.delete(environmentLeases);
await db.delete(executionWorkspaces);
await db.delete(heartbeatRunEvents);
await db.delete(heartbeatRuns);
await db.delete(agentWakeupRequests);
await db.delete(issues);
});
afterAll(async () => { await temporary?.cleanup(); });
/** The draining controller, now exited, in this PID space. */
async function deadController(overrides: Record<string, unknown> = {}) {
const bootId = randomUUID();
return { bootId, process: { bootId, pid: await exitedPid(), hostHash: currentProcessSpaceHash(),
processStartedAt: new Date(Date.now() - 60_000).toISOString(), ...overrides } as Record<string, unknown> | null };
}
async function interruptedPreparation(options: { controller?: { bootId: string; process: Record<string, unknown> | null };
leaseReleased?: boolean; holdEvidence?: Record<string, unknown>; run?: Partial<typeof heartbeatRuns.$inferInsert>;
issueStatus?: string } = {}) {
const issueId = randomUUID(), runId = randomUUID(), leaseId = randomUUID(), providerLeaseId = `sandbox-${leaseId}`;
const finishedAt = new Date(Date.now() - 10_000);
const controller = options.controller ?? await deadController();
// Terminalization projected the task to blocked and kept a receipt so the
// settle can restore exactly that projection.
await db.insert(issues).values({ id: issueId, companyId, title: "Warm follow-up", status: options.issueStatus ?? "blocked",
assigneeAgentId: agentId, statusVersion: 2 });
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, invocationSource: "automation",
responsibleUserId: "local-board", issueId,
status: "interrupted", errorCode: "server_shutdown_interrupted", runtimeMode: "legacy", executionStage: "preparing",
startedAt: new Date(finishedAt.getTime() - 2_000), finishedAt, nextEventSeq: 1,
controllerBootId: controller.bootId, controllerProcess: controller.process,
scheduledRetryAttempt: 1, scheduledRetryReason: "transient_failure",
contextSnapshot: { issueId },
runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" } },
resultJson: { stopReason: "interrupted", cancellation: { reason: "Server shutdown interrupted execution",
source: "shutdown", expected: true, initiator: { type: "system" }, recordedAt: finishedAt.toISOString() } },
...options.run });
const released = options.leaseReleased !== false;
await db.insert(environmentLeases).values({ id: leaseId, companyId, environmentId, issueId, heartbeatRunId: runId,
status: released ? "released" : "pending_cleanup", cleanupStatus: released ? "success" : "failed",
leasePolicy: "reuse_by_environment", provider: "fake", providerLeaseId,
releasedAt: released ? new Date(finishedAt.getTime() + 2_000) : null,
metadata: released ? { remoteExecutionTermination: { schema: "paperclip.remote-termination.v1", companyId, runId,
leaseId, provider: "fake", providerLeaseId, state: "stopped", confirmedAt: finishedAt.toISOString() } } : {} });
const [hold] = await db.insert(issueRecoveryActions).values({ companyId, sourceIssueId: issueId,
kind: "execution_reconciliation", cause: "legacy_execution_requires_reconciliation",
fingerprint: `legacy-execution:${runId}`, status: "resolved", outcome: "blocked", ownerType: "board",
nextAction: "Inspect the stopped provider and recorded actions.", resolvedAt: finishedAt,
evidence: { runId, automaticRecovery: { policy: "preserve_without_replay_v1", runId, replay: "blocked",
actionOutcome: "unknown", recordedAt: finishedAt.toISOString() },
nativeBootstrapFailureBlock: { runId, previousStatus: "in_progress", statusVersion: 2 },
...options.holdEvidence } }).returning();
return { issueId, runId, holdId: hold!.id };
}
const hold = async (id: string) => (await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.id, id)))[0]!;
const pass = async () => (await runOwnershipReconciliationPass(db, { state: createOwnershipReportState(), record: true }))
.kinds.find(entry => entry.kind === KIND);
it("replays a preparation a restart interrupted once its controller is proven gone", async () => {
// The heartbeat service registers the resume path that schedules the replay.
heartbeatService(db);
const f = await interruptedPreparation();
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
// The task is unblocked and an uncharged interrupted-preparation successor exists.
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toMatchObject({ status: "in_progress" });
const successors = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, f.runId));
expect(successors).toHaveLength(1);
expect(successors[0]).toMatchObject({ scheduledRetryReason: "interrupted_preparation" });
const settled = await hold(f.holdId);
expect(settled).toMatchObject({ status: "resolved", outcome: "false_positive" });
expect(settled.evidence).toMatchObject({ automaticRecovery: { replay: "allowed" },
bootstrapPreparationContinuation: "delivered", bootstrapPreparationSettledAt: expect.any(String) });
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.runId)))[0]!.resultJson)
.toMatchObject({ executionRecovery: { kind: "bootstrap", providerWorkStarted: false } });
const [audit] = await db.select().from(activityLog).where(and(eq(activityLog.runId, f.runId),
eq(activityLog.action, "issue.recovery_action_resolved")));
expect(audit?.details).toMatchObject({ reason: "native_bootstrap_stopped_before_provider", ownerProof: "pid_dead" });
});
// Staging release-swap shape: a Runner preparation that was
// itself an automatic retry had prepared its remote workspace when the
// deploy's SIGTERM interrupted it. Workspace settlement then handed the
// workspace on at its saved version (the run never changed it), which turns
// the run's reference to `saved`. No restore is owed, but the preparation
// proof still demanded a workspace-repair receipt that only exists after a
// copy-back, so the hold never settled, every automatic continuation was
// refused ("could not start yet"), and the person's Retry waited forever on
// previous_execution_unverified.
async function shutdownPreparationWithUnchangedWorkspace(options: { released?: boolean;
receipt?: (runId: string, snapshotId: string) => Record<string, unknown> } = {}) {
const released = options.released !== false;
const f = await interruptedPreparation({ run: { scheduledRetryAttempt: 1, scheduledRetryReason: "transient_failure" } });
const workspaceId = randomUUID(), leaseId = randomUUID(), snapshotId = randomUUID();
const source = { kind: "git", repoUrl: "https://github.com/paperclipai/paperclip.git", ref: "a".repeat(40),
branchMode: "create", branchName: "IPH-1-change" };
await db.insert(executionWorkspaces).values({ id: workspaceId, companyId, sourceIssueId: f.issueId, mode: "isolated_workspace",
strategyType: "git_worktree", name: "Remote workspace", status: "active", providerType: "git_worktree",
metadata: {
remoteWorkspace: { schema: "paperclip.remote-workspace.v1", epoch: 4, source, location: null, snapshotId,
dirty: !released, writerRunId: released ? null : f.runId },
remoteWorkspaceUnchangedBaseline: { schema: "paperclip.remote-workspace-unchanged-baseline.v1", epoch: 4,
runId: f.runId, snapshotId, ...(released ? { releasedAt: new Date().toISOString() } : {}),
...options.receipt?.(f.runId, snapshotId) },
} });
await db.update(heartbeatRuns).set({
contextSnapshot: { issueId: f.issueId, retryReason: "transient_failure",
executionRetryAccounting: { version: 1, failureRetries: 1, maxTurnContinuations: 0 } },
runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" },
remoteWorkspace: { schema: "paperclip.remote-workspace-run.v1", state: released ? "saved" : "prepared",
workspaceId, epoch: 4, leaseId, providerLeaseId: `sandbox-${leaseId}`,
remoteCwd: `/home/daytona/paperclip-workspace/.paperclip-workspaces/${workspaceId}-4-${leaseId}`, snapshotId } } })
.where(eq(heartbeatRuns.id, f.runId));
return { ...f, workspaceId };
}
it("replays a shutdown-interrupted preparation whose remote workspace was handed on unchanged", async () => {
heartbeatService(db);
const f = await shutdownPreparationWithUnchangedWorkspace();
expect(await getExecutionBlocker(db, companyId, f.issueId)).toMatchObject({ cause: "legacy_execution_requires_reconciliation" });
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
expect(await hold(f.holdId)).toMatchObject({ status: "resolved", outcome: "false_positive" });
expect(await getExecutionBlocker(db, companyId, f.issueId)).toBeNull();
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toMatchObject({ status: "in_progress" });
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, f.runId)))
.toEqual([expect.objectContaining({ scheduledRetryReason: "interrupted_preparation" })]);
});
it("keeps the hold while the interrupted preparation still holds its remote workspace unsaved", async () => {
heartbeatService(db);
const f = await shutdownPreparationWithUnchangedWorkspace({ released: false });
const before = await hold(f.holdId);
expect((await pass())?.settled ?? 0).toBe(0);
expect(await hold(f.holdId)).toEqual(before);
});
it.each([
["the receipt names another run", () => ({ runId: randomUUID() })],
["the receipt is for another epoch", () => ({ epoch: 3 })],
["the receipt is for another saved version", () => ({ snapshotId: randomUUID() })],
["the receipt has no release time", () => ({ releasedAt: undefined })],
] as const)("keeps the hold of a handed-on preparation when %s", async (_label, receipt) => {
heartbeatService(db);
const f = await shutdownPreparationWithUnchangedWorkspace({ receipt });
const before = await hold(f.holdId);
expect((await pass())?.settled ?? 0).toBe(0);
expect(await hold(f.holdId)).toEqual(before);
});
it("starts a person's Retry of a shutdown-interrupted preparation whose remote workspace was handed on", async () => {
const heartbeat = heartbeatService(db);
const f = await shutdownPreparationWithUnchangedWorkspace();
const receipt = await requestTaskRetry({ db, companyId, issueId: f.issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: f.runId });
expect(receipt).toMatchObject({ accepted: true });
await heartbeat.resumeTaskRetry(companyId, receipt.requestId!);
const [wake] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.requestId!));
expect(wake?.status).not.toBe("deferred_issue_execution");
expect(await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.issueId, f.issueId),
ne(heartbeatRuns.id, f.runId)))).not.toHaveLength(0);
}, 60_000);
it.each([
["provider work may have started", async () => {
const f = await interruptedPreparation();
await db.insert(heartbeatRunEvents).values({ companyId, runId: f.runId, agentId, seq: 1, eventType: "turn.started",
stream: "system", level: "info", message: "turn started" });
return f;
}],
["its controller stamp is from another process space", async () =>
interruptedPreparation({ controller: await deadController({ hostHash: "other-container" }) })],
["it has no controller stamp", async () =>
interruptedPreparation({ controller: { bootId: randomUUID(), process: null } })],
["its controller is this process", async () => interruptedPreparation({ controller: { bootId: legacyControllerBootId,
process: { bootId: legacyControllerBootId, pid: process.pid, hostHash: currentProcessSpaceHash(), processStartedAt: null } } })],
["its sandbox lease is still cleaning up", async () => interruptedPreparation({ leaseReleased: false })],
["an operator confirmed the owner gone on the hold", async () =>
interruptedPreparation({ holdEvidence: { ownerGoneAttestation: { actorId: "board-user" } } })],
["the user already continued", async () =>
interruptedPreparation({ holdEvidence: { explicitUserContinuation: { actorId: "board-user" } } })],
["it is a workspace-restore hold owned by restore recovery", async () =>
interruptedPreparation({ holdEvidence: { workspaceRestoreFailure: "restore_failed",
workspaceRestoreRecovery: { schema: "paperclip.workspace-restore-recovery.v1", leaseIds: [] } } })],
] as const)("keeps the hold when %s", async (_label, arrange) => {
const f = await arrange();
const before = await hold(f.holdId);
expect((await pass())?.settled ?? 0).toBe(0);
expect(await hold(f.holdId)).toEqual(before);
});
async function preparedRemoteWorkspace(runId: string, issueId: string, options: { receipt?: Record<string, unknown>;
savedSnapshotId?: string | null; authority?: Record<string, unknown>; profile?: Record<string, unknown> } = {}) {
const workspaceId = randomUUID(), leaseId = randomUUID(), providerLeaseId = `sandbox-${leaseId}`;
const remoteCwd = `/home/daytona/paperclip-workspace/.paperclip-workspaces/${workspaceId}-1-${leaseId}`;
const location = { environmentId, leaseId, providerLeaseId, remoteCwd };
const source = { kind: "git", repoUrl: "https://github.com/paperclipai/paperclip.git", ref: "a".repeat(40),
branchMode: "create", branchName: "IPH-1-change" };
const authority = { schema: "paperclip.remote-workspace.v1", epoch: 2, source, location: null, dirty: false, writerRunId: null,
snapshotId: options.savedSnapshotId ?? null, ...options.authority };
const receipts = [{ schema: "paperclip.remote-workspace-abandoned-epoch.v1", epoch: 1, runId,
snapshotId: options.savedSnapshotId ?? null, location, providerRef: remoteCwd, abandonedAt: new Date().toISOString(), ...options.receipt }];
await db.insert(executionWorkspaces).values({ id: workspaceId, companyId, sourceIssueId: issueId, mode: "isolated_workspace",
strategyType: "git_worktree", name: "Remote workspace", status: "active", providerType: "git_worktree",
metadata: { remoteWorkspace: authority, remoteWorkspaceAbandonedEpochs: receipts } });
await db.update(heartbeatRuns).set({ runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" },
remoteWorkspace: { schema: "paperclip.remote-workspace-run.v1", state: "prepared", workspaceId, epoch: 1, leaseId,
providerLeaseId, remoteCwd, snapshotId: null }, ...options.profile } }).where(eq(heartbeatRuns.id, runId));
return { leaseId };
}
const runRow = async (runId: string) => (await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))[0]!;
it("replays a restore-recovery hold saved by an older build once the workspace retired the run's epoch", async () => {
heartbeatService(db);
const f = await interruptedPreparation();
const { leaseId } = await preparedRemoteWorkspace(f.runId, f.issueId);
const restore = { workspaceRestoreFailure: "restore_failed",
workspaceRestoreRecovery: { schema: "paperclip.workspace-restore-recovery.v1", leaseIds: [leaseId] } };
const run = await runRow(f.runId);
await db.update(heartbeatRuns).set({ resultJson: { ...run.resultJson, ...restore } }).where(eq(heartbeatRuns.id, f.runId));
const saved = await hold(f.holdId);
await db.update(issueRecoveryActions).set({ evidence: { ...saved.evidence, ...restore,
systemRecovery: { schema: "paperclip.system-recovery.v1", policyVersion: 1, state: "exhausted", phase: "verify_execution",
sourceRunId: f.runId, attempts: 1, retriesUsed: 0, revision: 5, nextAttemptAt: null,
failure: { code: "source_binding_invalid", retryClass: "service_required", sourceVerification: { reason: "workspace_reference_mismatch" } } } } })
.where(eq(issueRecoveryActions.id, f.holdId));
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toMatchObject({ status: "in_progress" });
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, f.runId))).toHaveLength(1);
});
function controllerLeaseLostRun(finishedAt = new Date(Date.now() - 10_000)): Partial<typeof heartbeatRuns.$inferInsert> {
return { status: "failed", errorCode: "controller_lease_lost", error: "Legacy controller lease lost",
scheduledRetryAttempt: 0, scheduledRetryReason: null,
resultJson: { stopReason: "adapter_failed", timeoutFired: false,
cancellation: { reason: "Legacy controller lease lost (renewal_deadline)", source: "controller_lease", expected: false,
initiator: { type: "system" }, recordedAt: finishedAt.toISOString() },
executionRecovery: { kind: "bootstrap", providerWorkStarted: false },
controllerLeaseLoss: { cause: "renewal_deadline", renewalCount: 25, deadlineLateMs: 673, sinceRenewalMs: 36396,
renewalPendingMs: 29436 } } };
}
async function leaseLostSavedFilesHold(options: { keepStatusReceipt: boolean }) {
const f = await interruptedPreparation({ run: controllerLeaseLostRun() });
const { leaseId } = await preparedRemoteWorkspace(f.runId, f.issueId);
const restore = { workspaceRestoreFailure: "restore_failed",
workspaceRestoreRecovery: { schema: "paperclip.workspace-restore-recovery.v1", leaseIds: [leaseId] } };
const run = await runRow(f.runId);
await db.update(heartbeatRuns).set({ resultJson: { ...run.resultJson, ...restore } }).where(eq(heartbeatRuns.id, f.runId));
const saved = await hold(f.holdId);
const { nativeBootstrapFailureBlock, ...staged } = saved.evidence;
await db.update(issueRecoveryActions).set({ kind: "active_run_watchdog", evidence: { ...staged, ...restore,
...(options.keepStatusReceipt ? { nativeBootstrapFailureBlock } : {}),
originalFailureCode: "controller_lease_lost", continuationDelivery: "invalidated",
historicalWorkspaceSource: { schema: "paperclip.historical-workspace-source.v1", runId: f.runId, leaseIds: [leaseId] },
workspaceRecovery: { schema: "paperclip.workspace-recovery.v1", runId: f.runId, status: "blocked", stage: "verifying_stop",
attempt: 1, attemptLimit: 6, retryable: true, requestIds: [],
message: "Automatic recovery of saved work failed (last failure: source_unavailable). Retry to try again; the agent will not repeat its work." },
systemRecovery: { schema: "paperclip.system-recovery.v1", policyVersion: 1, capturePolicyVersion: REMOTE_WORKSPACE_CAPTURE_POLICY_VERSION, state: "exhausted",
phase: "verify_execution", sourceRunId: f.runId, generationId: randomUUID(), attemptId: randomUUID(), intentId: null,
attempts: 1, retriesUsed: 0, revision: 2, nextAttemptAt: null, providerCooldownAt: null, freshAttemptRunId: null,
preservationOperationIds: [], failure: { code: "source_unavailable", retryClass: "use_alternative_strategy",
evidenceFingerprint: "a".repeat(64) } } } })
.where(eq(issueRecoveryActions.id, f.holdId));
return f;
}
it("replays a preparation that lost its controller lease before provider work instead of holding it for reconciliation", async () => {
heartbeatService(db);
const f = await leaseLostSavedFilesHold({ keepStatusReceipt: true });
await expect(inspectWorkspaceRecoverySource(db, { companyId, issueId: f.issueId, runId: f.runId, actionId: f.holdId }))
.rejects.toMatchObject({ code: "source_unavailable", retryClass: "use_alternative_strategy" });
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
expect(await hold(f.holdId)).toMatchObject({ status: "resolved", outcome: "false_positive" });
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toMatchObject({ status: "in_progress" });
const successors = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, f.runId));
expect(successors).toHaveLength(1);
expect(successors[0]).toMatchObject({ scheduledRetryReason: "interrupted_preparation" });
});
// Staging upgrade shape: a replaced server's Runner preparation was reaped as
// process_lost while it was the last automatic retry (failure budget spent).
// An older release held it for reconciliation ("Automatic recovery stopped").
it.each([
["its lease still names the old controller", false],
// The real reaped shape: revocation swapped in a random boot id and kept the process stamp.
["the orphan reaper revoked its lease", true],
] as const)("releases an older release's hold on a preparation a server replacement stopped with its failure budget spent when %s", async (_label, revoked) => {
heartbeatService(db);
const finishedAt = new Date(Date.now() - 10_000);
const f = await interruptedPreparation({ run: { status: "failed", errorCode: "process_lost",
error: "Process lost -- server may have restarted", scheduledRetryAttempt: 2,
resultJson: { stopReason: "process_lost", executionRecovery: { kind: "bootstrap", providerWorkStarted: false },
processLossDiagnostic: { pidRecorded: false, groupRecorded: false, localCheck: "no_identifiers",
retryEligible: false, controllerReplaced: true } },
contextSnapshot: undefined, finishedAt } });
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId: f.issueId,
executionRetryAccounting: { version: 1, failureRetries: 2, maxTurnContinuations: 0 } },
...(revoked ? { controllerBootId: randomUUID() } : {}) })
.where(eq(heartbeatRuns.id, f.runId));
expect(await getExecutionBlocker(db, companyId, f.issueId)).toMatchObject({ cause: "legacy_execution_requires_reconciliation" });
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
expect(await hold(f.holdId)).toMatchObject({ status: "resolved", outcome: "false_positive" });
expect(await getExecutionBlocker(db, companyId, f.issueId)).toBeNull();
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toMatchObject({ status: "in_progress" });
const successors = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, f.runId));
expect(successors).toEqual([expect.objectContaining({ scheduledRetryReason: "server_restart", scheduledRetryAttempt: 1 })]);
expect(successors[0]!.contextSnapshot?.executionRetryAccounting).toMatchObject({ failureRetries: 2, serverRestarts: 1 });
});
it("clears the saved-files gate of a lost-lease preparation that an older release blocked, so one Retry continues it", async () => {
const heartbeat = heartbeatService(db);
const f = await leaseLostSavedFilesHold({ keepStatusReceipt: false });
expect(await getExecutionBlocker(db, companyId, f.issueId)).toMatchObject({ cause: "legacy_execution_requires_reconciliation" });
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
expect(await hold(f.holdId)).toMatchObject({ status: "resolved", outcome: "false_positive",
evidence: expect.objectContaining({ bootstrapPreparationContinuation: "deferred" }) });
expect(await getExecutionBlocker(db, companyId, f.issueId)).toBeNull();
const receipt = await requestTaskRetry({ db, companyId, issueId: f.issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: f.runId });
expect(receipt).toMatchObject({ accepted: true });
await heartbeat.resumeTaskRetry(companyId, receipt.requestId!);
const [wake] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.requestId!));
expect(wake?.status).not.toBe("deferred_issue_execution");
expect(await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.issueId, f.issueId),
inArray(heartbeatRuns.status, ["queued", "running", "scheduled_retry", "succeeded", "failed"]),
ne(heartbeatRuns.id, f.runId)))).not.toHaveLength(0);
}, 60_000);
it.each([
["an in-progress continuation", "in_progress", { retryReason: "issue_continuation_needed", source: "issue.continuation_recovery" }, 2],
["a todo task that never reached checkout", "todo", {}, 12],
] as const)("bounds repeated lost leases before provider work for %s, then lets Retry continue", async (_label, status, firstContext, cap) => {
const heartbeat = heartbeatService(db);
const controller = await deadController();
const issueId = randomUUID(), firstRunId = randomUUID();
await db.insert(issues).values({ id: issueId, companyId, title: "Lease loop", status,
assigneeAgentId: agentId, statusVersion: 2 });
const lostLease = (runId: string, context: Record<string, unknown>, retry: Partial<typeof heartbeatRuns.$inferInsert> = {}) => {
const finishedAt = new Date(Date.now() - 30 * 60_000);
return { ...controllerLeaseLostRun(finishedAt), id: runId, companyId, agentId, invocationSource: "automation",
responsibleUserId: "local-board", issueId, runtimeMode: "legacy", executionStage: "preparing",
startedAt: new Date(finishedAt.getTime() - 2_000), finishedAt, nextEventSeq: 1,
controllerBootId: controller.bootId, controllerProcess: controller.process,
runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" } },
contextSnapshot: { issueId, ...context }, ...retry } as typeof heartbeatRuns.$inferInsert;
};
await db.insert(heartbeatRuns).values(lostLease(firstRunId, { ...firstContext }));
let latest: string = firstRunId;
let successors = 0;
for (let round = 0; round < 40; round += 1) {
await heartbeat.reconcileStrandedAssignedIssues({ companyId, issueId });
const [task] = await db.select().from(issues).where(eq(issues.id, issueId));
expect(task!.status).toBe(status);
const [next] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, latest));
if (!next) break;
expect(next!.contextSnapshot).toMatchObject({ retryReason: "interrupted_preparation",
executionRetryAccounting: { interruptedPreparations: successors + 1 } });
successors += 1;
const finished = lostLease(next!.id, next!.contextSnapshot ?? {}, {
scheduledRetryAttempt: next!.scheduledRetryAttempt, scheduledRetryReason: next!.scheduledRetryReason,
retryOfRunId: latest, wakeupRequestId: next!.wakeupRequestId });
await db.update(heartbeatRuns).set({ status: "failed", errorCode: finished.errorCode, error: finished.error,
resultJson: finished.resultJson, startedAt: finished.startedAt, finishedAt: finished.finishedAt,
executionStage: "preparing", controllerBootId: finished.controllerBootId, controllerProcess: finished.controllerProcess,
runnerProfileJson: finished.runnerProfileJson }).where(eq(heartbeatRuns.id, next!.id));
latest = next!.id;
}
expect(successors).toBe(cap);
expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0]).toMatchObject({ status });
await db.update(agentWakeupRequests).set({ status: "failed", finishedAt: new Date() })
.where(and(eq(agentWakeupRequests.companyId, companyId), sql`${agentWakeupRequests.payload}->>'issueId' = ${issueId}`));
await db.update(heartbeatRuns).set({ scheduledRetryAt: new Date(Date.now() - 31 * 60_000) })
.where(and(eq(heartbeatRuns.issueId, issueId), ne(heartbeatRuns.id, firstRunId)));
const check = await heartbeat.checkAssignedWorkInvariant({ companyId, issueId });
expect(check).toMatchObject({ outcome: cap >= 6 ? "deferred_cap" : "deferred_rate_limit" });
expect(check!.retryAt!.getTime()).toBeGreaterThan(Date.now());
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, latest))).toEqual([]);
const receipt = await requestTaskRetry({ db, companyId, issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: latest });
expect(receipt).toMatchObject({ accepted: true });
}, 120_000);
it("leaves the task to the assigned-work sweep, not blocked, when a replaced preparation's lane is spent", async () => {
const heartbeat = heartbeatService(db);
const controller = await deadController();
const issueId = randomUUID(), runId = randomUUID();
const finishedAt = new Date(Date.now() - 30 * 60_000);
await db.insert(issues).values({ id: issueId, companyId, title: "Replaced preparation", status: "todo",
assigneeAgentId: agentId, statusVersion: 2 });
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, invocationSource: "automation",
responsibleUserId: "local-board", issueId, status: "failed", errorCode: "process_lost",
error: "Process lost -- server may have restarted", runtimeMode: "legacy", executionStage: "preparing",
startedAt: new Date(finishedAt.getTime() - 2_000), finishedAt, nextEventSeq: 1,
controllerBootId: controller.bootId, controllerProcess: controller.process,
scheduledRetryAttempt: 12, scheduledRetryReason: "interrupted_preparation",
runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" } },
contextSnapshot: { issueId, retryReason: "interrupted_preparation", executionRetryAccounting: { version: 1,
failureRetries: 0, maxTurnContinuations: 0, interruptedPreparations: 12 } },
resultJson: { stopReason: "process_lost", executionRecovery: { kind: "bootstrap", providerWorkStarted: false },
processLossDiagnostic: { pidRecorded: false, groupRecorded: false, localCheck: "no_identifiers",
retryEligible: true, controllerReplaced: true } } });
await heartbeat.reconcileStrandedAssignedIssues({ companyId, issueId });
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, runId))).toEqual([]);
expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0]).toMatchObject({ status: "todo" });
expect(await db.select().from(issueRecoveryActions).where(and(eq(issueRecoveryActions.sourceIssueId, issueId),
inArray(issueRecoveryActions.status, ["active", "escalated"])))).toEqual([]);
});
it.each([
["provider work started", { kind: "bootstrap", providerWorkStarted: true }],
["it has no no-provider-work certificate", undefined],
] as const)("keeps a lost-lease preparation held when %s", async (_label, executionRecovery) => {
const f = await interruptedPreparation({ run: controllerLeaseLostRun() });
const run = await runRow(f.runId);
await db.update(heartbeatRuns).set({ resultJson: { ...run.resultJson, executionRecovery } })
.where(eq(heartbeatRuns.id, f.runId));
const before = await hold(f.holdId);
expect((await pass())?.settled ?? 0).toBe(0);
expect(await hold(f.holdId)).toEqual(before);
});
it("reports a retired prepared epoch to workspace recovery as an unavailable source, not a binding failure", async () => {
const f = await interruptedPreparation();
await preparedRemoteWorkspace(f.runId, f.issueId);
await expect(inspectWorkspaceRecoverySource(db, { companyId, issueId: f.issueId, runId: f.runId, actionId: f.holdId }))
.rejects.toMatchObject({ code: "source_unavailable", retryClass: "use_alternative_strategy" });
});
const proofCases: Array<[string, { authority?: Record<string, unknown>; profile?: Record<string, unknown>; writer?: boolean }, boolean]> = [
["the workspace is still at the run's epoch", { authority: { epoch: 1 } }, false],
["the run is still the workspace writer", { writer: true }, false],
["the run also has a native workspace sync reference", { profile: { nativeWorkspaceSync: { state: "prepared" } } }, false],
["the receipt matches the run's exact epoch and allocation", {}, true],
];
it.each(proofCases)("treats the prepared workspace as retired only when proven: %s", async (_label, options, expected) => {
const f = await interruptedPreparation();
const authority = options.writer ? { writerRunId: f.runId } : options.authority;
await preparedRemoteWorkspace(f.runId, f.issueId, { profile: options.profile, authority });
expect(await isAbandonedPreparedRemoteWorkspace(db, await runRow(f.runId))).toBe(expected);
});
it.each([
["before its first save finished", null],
["after its first save committed", randomUUID()],
] as const)("replays a remote preparation whose unsaved epoch the workspace already retired %s", async (_label, savedSnapshotId) => {
heartbeatService(db);
const f = await interruptedPreparation();
await preparedRemoteWorkspace(f.runId, f.issueId, { savedSnapshotId });
expect(await pass()).toMatchObject({ inProgress: 1, settled: 1 });
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toMatchObject({ status: "in_progress" });
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, f.runId))).toHaveLength(1);
});
it.each([
["the retired epoch belongs to another run", { receipt: { runId: randomUUID() } }],
["the retired epoch was another allocation", { receipt: { location: { leaseId: randomUUID() } } }],
] as const)("keeps a remote preparation hold when %s", async (_label, options) => {
const f = await interruptedPreparation();
await preparedRemoteWorkspace(f.runId, f.issueId, options);
const before = await hold(f.holdId);
expect((await pass())?.settled ?? 0).toBe(0);
expect(await hold(f.holdId)).toEqual(before);
});
it("settles exactly once when two processes judge the same hold", async () => {
heartbeatService(db);
const f = await interruptedPreparation();
const record = { recordId: f.runId, companyId, runId: f.runId, issueId: f.issueId, owner: { kind: "none" as const },
updatedAt: null };
const context = { now: new Date(), proof: { verdict: "gone" as const, reason: "pid_dead" as const } };
const outcomes = await Promise.all([settleHeldInterruptedPreparation(db, record, context),
settleHeldInterruptedPreparation(db, record, context)]);
expect(outcomes.filter(outcome => outcome.settled)).toHaveLength(1);
const resolutions = await db.select().from(activityLog).where(and(eq(activityLog.runId, f.runId),
eq(activityLog.action, "issue.recovery_action_resolved")));
expect(resolutions).toHaveLength(1);
});
});