mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-08 22:13:59 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Native execution can save an accepted result before workspace export fails. > - Recover must preserve the files and settle that same result without another agent turn. > - The export failure can own the task's Blocked transition and its recovery notice. > - Recovery must prove preservation, stop, source identity and current authority before settlement. > - Recover can also transfer repair ownership to the system, which must keep explicit board export repair available. > - This change restores those paths while preserving later task state, live owners and unknown action outcomes. ## Linked Issues or Issue Description Related #17916 protects unfinished exports. Related #19004 preserves deliberate Retry authority. This change repairs accepted-result finalization and its recovery notice. **What happened?** A native run accepted its result, then exhausted workspace export. Normal Recover could save the files but leave the result and old recovery notice unsettled. A saved Retry could also use a different generation from preservation. Separately, Recover changed the action owner from board to system. Explicit export Retry then rejected that same source as stale, even when its accepted result and stopped allocation still matched. **Expected behavior** Preserve files, finish the original accepted result and close only its obsolete notice after exact proof. Keep explicit board export repair available after system adoption. Preserve current authority, Stop, later task changes, newer execution and every physical ownership fence. **Steps to reproduce** 1. Accept a native result and exhaust its workspace export. 2. Request ordinary Recover, optionally with an existing saved Retry. 3. Let file preservation complete. Inspect the task and source recovery notice. 4. To reproduce the explicit-repair defect, call export Retry after Recover adopts the action. Before this repair it rejects the valid system-owned source before any provider call. ## What Changed - Record the exact accepted result, contract, task status version and prior decision when export exhaustion creates Blocked. - Bind passive finalization to complete, source-bound preservation and the current chat/review authority. Recheck the proof under status commit locks. - Keep the saved Retry identity separate from its preservation generation. Retain coalescing and reject malformed or mismatched history. - Preserve an existing published GitHub assessment for the no-publication status-unproven wait. Keep all semantic, result, authority and publication checks. - Close an old export notice only after its own accepted result, export or preservation receipts, effects, projection and old Runner ownership settle. Keep newer work and files unchanged. - Permit explicit board export Retry after Recover transfers the exact source to the system. Require valid system and workspace recovery records for that same run, plus every existing result, stop, allocation, task and shared physical-lock check. Reject missing proof, another source and agent ownership before provider access. - Merge current master while retaining the stronger Stop, source and verified preservation checks. - Update the execution contract. No schema, endpoint, Runner protocol, model, trust or provider artifact change. ## Verification - Current head: `9be5a73489801095c9f39c9bfcb285a22b055912`. - The explicit-repair regression fails before the new guard change: ordinary Recover adopts the real database action, then export Retry rejects it as stale. It passes after the repair. - On the merged source, three owning PostgreSQL/file suites pass **220 tests**: `preserved-native-response-wait.integration.test.ts`, `native-workspace-export-retry.test.ts` and `idle-completed-history.test.ts`. The export suite includes four unrelated-owner/source rejection controls and both board-owned and system-adopted active-exporter controls. Results and run counts prove that repair creates no provider turn. - Server layered typecheck, module boundaries, `git diff --check`, and the added-line secret/private-incident scan pass. - Full workspace typecheck (`pnpm -r typecheck`) and build (`pnpm build`) pass. The complete local `pnpm test:run` is still running; its completed serialized chat partitions pass. Current-head CI is green (53 successful checks, 3 expected skips). Greptile reviewed `9be5a73489801095c9f39c9bfcb285a22b055912` at 2026-11-05 00:58 UTC with confidence **5/5**; all review threads are resolved. - Original qualification remains supporting evidence: the accepted-result fixture exercises HTTP Recover, real files, the finalizer, saved Retry coalescing, eight commit races, current principal/resource/head changes, Stop, later task changes, notice closure and repeated delivery. No accepted response, provider turn or publication is replayed. - Earlier failures remain recorded. Original red controls exposed wrong-action preservation, unrelated finalization barriers, generation mismatch and a lost GitHub assessment; the original repair added controls for them. The prior CI run [41908572248](https://github.com/paperclipai/paperclip/actions/runs/41908572248) failed an unchanged completed-history runtime fixture because its loopback service refused connections through the original ten-second readiness window. That owning suite passes unchanged in the 220-case run above. The earlier readiness failure's cause remains unproven; no wait or assertion was weakened. - Local and CI tests do not establish hosted task recovery. The serving revision and original-task outcomes require separate verification after rollout. ## Risks - Historical failures without the exact export-owned Blocked receipt keep their current task status. A saved result does not grant execution or publication authority. - Explicit export repair still resumes only the same retained allocation under the shared physical lock. It cannot take over an active exporter, create another agent turn, choose a replacement sandbox or discard saved files. - Retiring a source notice does not release physical resources or modify a newer task owner, later Done state or saved snapshot history. - The proof adds bounded reads only for concrete recovery obligations. It adds no recurring worker or polling timer. ## Model Used OpenAI GPT-6 through Codex for the original repair. OpenAI GPT-6.1 Sol (`gpt-6.1-sol`), xhigh reasoning, for the merge, integration verification and system-adopted export repair. Both used repository tools, code execution and PostgreSQL tests. The runtime does not expose a context-window size. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used - [x] I have checked ROADMAP.md and searched for duplicate or related work - [x] I have described the issue and linked related public PRs - [x] I have included no internal task identifiers or private incident links - [x] I have added behavioral and safety regression tests - [x] I have documented verification limits and risks - [x] I have updated the execution contract - [x] All Paperclip CI gates are green on the current head - [x] Greptile is 5/5 on the current head with no unresolved comments - [x] I will address review comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Devin Foley <devin@paperclip.ing>
202 lines
17 KiB
TypeScript
202 lines
17 KiB
TypeScript
import { notifyStatusCardIssueChange } from "../status-card-notifications.js";
|
|
import { notifyNativeFinalizationWork } from "../native-finalization-notifications.js";
|
|
import { notifyEnvironmentCleanup } from "../environment-cleanup-notifications.js";
|
|
import { randomUUID } from "node:crypto";
|
|
import type { Environment, EnvironmentLease } from "@paperclipai/shared";
|
|
import { isNativeWorkspaceExportRepairCause } from "@paperclipai/shared";
|
|
import { and, eq, gt, inArray, ne, or, sql } from "drizzle-orm";
|
|
import { environmentLeases, environments, heartbeatRuns, issues, issueRecoveryActions, nativeRunFinalizations, nativeRunResults, type Db } from "@paperclipai/db";
|
|
import { conflict } from "../../errors.js";
|
|
import { persistActivity, publishActivity } from "../activity-log.js";
|
|
import { hasRemoteTerminationReceipt, remoteTerminationReceipt } from "../remote-execution-termination.js";
|
|
import { NATIVE_WORKSPACE_EXPORT_RESUME_KEY, readNativeWorkspaceExportResume, settleNativeWorkspaceExportResume } from "./native-workspace-export-resume.js";
|
|
import { withNativeWorkspaceFinalizationOwnership } from "./native-workspace-finalization-ownership.js";
|
|
import { readNativeWorkspaceSyncReference } from "./native-workspace-sync.js";
|
|
import type { EnvironmentRuntimeService } from "../environment-runtime.js";
|
|
import { resolveEnvironmentExecutionTarget } from "../environment-execution-target.js";
|
|
import { readSystemRecovery } from "../system-recovery-state.js";
|
|
import { readWorkspaceRecovery } from "../workspace-recovery-receipt.js";
|
|
|
|
const record = (value: unknown): Record<string, unknown> => value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
const changed = () => conflict("The recorded export repair is no longer current. Refresh the task and inspect its run.", { code: "workspace_export_retry_stale" });
|
|
|
|
/** Explicit board admission reopens only the accepted result's finalization suffix. */
|
|
export async function retryNativeWorkspaceExport(input: {
|
|
db: Db; companyId: string; issueId: string; actionId: string; runId: string;
|
|
actorId: string; repairNote: string; environmentRuntime: EnvironmentRuntimeService;
|
|
}) {
|
|
async function inspect(db: Db, lock = false, resumeRequestId?: string) {
|
|
const one = async <T>(query: PromiseLike<T[]> & { for(mode: "update"): PromiseLike<T[]> }): Promise<T | null> =>
|
|
(await (lock ? query.for("update") : query))[0] ?? null;
|
|
// Keep the same issue -> coordinator -> run order as status commitment.
|
|
const issue = await one(db.select().from(issues).where(and(eq(issues.companyId, input.companyId), eq(issues.id, input.issueId))).limit(1));
|
|
const coordinator = await one(db.select().from(nativeRunFinalizations).where(and(eq(nativeRunFinalizations.companyId, input.companyId), eq(nativeRunFinalizations.runId, input.runId), eq(nativeRunFinalizations.issueId, input.issueId))).limit(1));
|
|
const run = await one(db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, input.companyId), eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.nativeIssueId, input.issueId))).limit(1));
|
|
const action = await one(db.select().from(issueRecoveryActions).where(and(eq(issueRecoveryActions.companyId, input.companyId), eq(issueRecoveryActions.sourceIssueId, input.issueId), eq(issueRecoveryActions.id, input.actionId))).limit(1));
|
|
const system = action && readSystemRecovery(action.evidence);
|
|
const recovery = action && readWorkspaceRecovery(action.evidence);
|
|
const repairOwner = action?.ownerType === "board" || (action?.ownerType === "system"
|
|
&& system?.sourceRunId === run?.id && recovery?.runId === run?.id);
|
|
if (!issue || !run || !coordinator || !action || action.evidence?.runId !== run.id
|
|
|| action.kind !== "active_run_watchdog" || !isNativeWorkspaceExportRepairCause(action.cause) || !repairOwner
|
|
|| !["active", "escalated"].includes(action.status) || run.runtimeMode !== "native"
|
|
|| !(issue.status === "blocked" || (issue.status === "in_review" && action.cause === "native_workspace_sync_out_retry_exhausted")) || issue.assigneeAgentId !== run.agentId
|
|
|| (issue.executionRunId && issue.executionRunId !== run.id)
|
|
|| (issue.checkoutRunId && issue.checkoutRunId !== run.id) || coordinator.leaseOwner) throw changed();
|
|
const reference = readNativeWorkspaceSyncReference(record(run.runnerProfileJson).nativeWorkspaceSync);
|
|
if (!reference || reference.state !== "prepared") throw changed();
|
|
const lease = await one(db.select().from(environmentLeases).where(and(eq(environmentLeases.companyId, input.companyId), eq(environmentLeases.id, reference.leaseId))).limit(1));
|
|
if (!lease || lease.heartbeatRunId !== run.id || lease.issueId !== issue.id
|
|
|| lease.providerLeaseId !== reference.providerLeaseId || !lease.environmentId
|
|
|| typeof lease.metadata?.pluginId !== "string" || !lease.metadata.pluginId) throw changed();
|
|
const [otherLease] = await db.select({ id: environmentLeases.id }).from(environmentLeases).where(and(
|
|
ne(environmentLeases.id, lease.id), eq(environmentLeases.provider, lease.provider!), eq(environmentLeases.providerLeaseId, lease.providerLeaseId!), inArray(environmentLeases.status, ["active", "retained", "pending_cleanup"]),
|
|
)).limit(1);
|
|
if (otherLease) throw changed();
|
|
const [result] = await db.select().from(nativeRunResults).where(and(eq(nativeRunResults.companyId, input.companyId), eq(nativeRunResults.issueId, issue.id), eq(nativeRunResults.runId, run.id), eq(nativeRunResults.id, coordinator.resultId ?? "00000000-0000-0000-0000-000000000000"))).limit(1);
|
|
if (!result || result.schemaStatus !== "accepted" || result.completionContractId !== run.completionContractId) throw changed();
|
|
const [newer] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(and(
|
|
eq(heartbeatRuns.companyId, input.companyId), ne(heartbeatRuns.id, run.id),
|
|
or(eq(heartbeatRuns.nativeIssueId, issue.id), sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${issue.id}`),
|
|
or(gt(heartbeatRuns.createdAt, run.createdAt), inArray(heartbeatRuns.status, ["queued", "running"])),
|
|
)).limit(1);
|
|
if (newer) throw changed();
|
|
const pending = record(action.evidence?.workspaceExportRetry);
|
|
const alreadyQueued = coordinator.phase !== "terminal_failure" && pending.runId === run.id
|
|
&& pending.resultId === result.id && pending.leaseId === lease.id;
|
|
const resumeIntent = readNativeWorkspaceExportResume(lease);
|
|
const ownsResume = Boolean(resumeRequestId && lease.status === "pending_cleanup"
|
|
&& lease.metadata?.pendingCleanupAttemptId === resumeRequestId
|
|
&& lease.metadata?.pendingCleanupInFlight === true && resumeIntent?.requestId === resumeRequestId
|
|
&& Number(lease.metadata?.pendingCleanupLeaseExpiresAtMs) > Date.now()
|
|
&& resumeIntent.resultId === result.id);
|
|
if (!alreadyQueued && (coordinator.phase !== "terminal_failure" || coordinator.failureCode !== action.cause
|
|
|| run.status !== "failed" || (!ownsResume && (!hasRemoteTerminationReceipt(lease)
|
|
|| record(lease.metadata?.remoteExecutionTermination).state !== "stopped")))) throw changed();
|
|
if (resumeRequestId && !ownsResume) throw changed();
|
|
const [environment] = await db.select().from(environments).where(eq(environments.id, lease.environmentId)).limit(1);
|
|
if (!environment) throw changed();
|
|
return { issue, coordinator, run, action, reference, lease, result, environment, alreadyQueued };
|
|
}
|
|
|
|
const owned = await withNativeWorkspaceFinalizationOwnership(input, async (ownership) => {
|
|
const initial = await inspect(input.db);
|
|
let resumeRequestId: string | undefined;
|
|
try {
|
|
if (!initial.alreadyQueued) {
|
|
// The operator resumes and repairs the exact retained sandbox through its
|
|
// provider console. Never acquire a replacement or seed over saved work.
|
|
const target = await resolveEnvironmentExecutionTarget({ db: input.db, companyId: input.companyId,
|
|
adapterType: "paperclip_runner", environment: initial.environment, leaseId: initial.lease.id,
|
|
leaseMetadata: initial.lease.metadata, lease: initial.lease as EnvironmentLease, environmentRuntime: input.environmentRuntime });
|
|
if (target?.kind !== "remote" || target.transport !== "sandbox" || !target.runner
|
|
|| target.remoteCwd !== initial.reference.remoteCwd) throw changed();
|
|
await ownership.assertHeld();
|
|
const requestId = randomUUID();
|
|
const claimedLease = await input.db.transaction(async tx => {
|
|
const current = await inspect(tx as unknown as Db, true);
|
|
if (current.alreadyQueued || current.result.id !== initial.result.id || current.lease.id !== initial.lease.id
|
|
|| current.reference.descriptorSha256 !== initial.reference.descriptorSha256) throw changed();
|
|
const now = new Date();
|
|
// Persist cleanup authority before any provider call. A lost resume
|
|
// reply or controller restart must not leave a running sandbox behind
|
|
// an old stopped receipt. The pending claim fences the cleanup sweeper.
|
|
await notifyEnvironmentCleanup(tx, { companyId: input.companyId, runId: input.runId });
|
|
const [lease] = await tx.update(environmentLeases).set({ status: "pending_cleanup", cleanupStatus: "failed",
|
|
releasedAt: now, failureReason: "workspace_export_resume_pending", updatedAt: now,
|
|
metadata: { ...current.lease.metadata, remoteExecutionTermination: undefined,
|
|
[NATIVE_WORKSPACE_EXPORT_RESUME_KEY]: { schema: "paperclip.workspace-export-resume.v2", requestId,
|
|
companyId: input.companyId, runId: input.runId, leaseId: current.lease.id,
|
|
pluginId: current.lease.metadata?.pluginId,
|
|
provider: current.lease.provider, providerLeaseId: current.lease.providerLeaseId, resultId: current.result.id },
|
|
pendingCleanupAttemptId: requestId, pendingCleanupInFlight: true,
|
|
pendingCleanupRetryAfterMs: 0, pendingCleanupRetryAttempts: 0, pendingCleanupRetryCapWarned: false,
|
|
pendingCleanupLeaseExpiresAtMs: now.getTime() + 15 * 60_000 },
|
|
}).where(eq(environmentLeases.id, current.lease.id)).returning();
|
|
return lease;
|
|
});
|
|
resumeRequestId = requestId;
|
|
try {
|
|
await ownership.assertHeld();
|
|
await inspect(input.db, false, requestId);
|
|
// A provider stop also closes its controller-side activity gate. The
|
|
// operator's external repair does not reopen that gate. Resume only the
|
|
// recorded lease through its provider so sentinel and drain checks run.
|
|
const resumed = await input.environmentRuntime.resumeRunLease({ environment: initial.environment as unknown as Environment,
|
|
lease: claimedLease as EnvironmentLease });
|
|
if (resumed?.providerLeaseId !== initial.lease.providerLeaseId
|
|
|| resumed.metadata?.remoteCwd !== initial.reference.remoteCwd) throw new Error("sandbox_resume_identity_unproven");
|
|
const probe = await target.runner.execute({ command: target.shellCommand ?? "sh", args: ["-c", "true"], cwd: "/", timeoutMs: 10_000, bypassSession: true });
|
|
if (probe.timedOut || probe.exitCode !== 0) throw new Error("sandbox_not_running");
|
|
} catch {
|
|
throw conflict("Resume and repair the retained sandbox before retrying workspace export. No provider turn was started.", { code: "workspace_export_sandbox_unavailable" });
|
|
}
|
|
}
|
|
await ownership.assertHeld();
|
|
const admitted = await input.db.transaction(async tx => {
|
|
const current = await inspect(tx as unknown as Db, true, resumeRequestId);
|
|
if (current.result.id !== initial.result.id || current.lease.id !== initial.lease.id
|
|
|| current.reference.descriptorSha256 !== initial.reference.descriptorSha256) throw changed();
|
|
const receipt = { runId: current.run.id, resultId: current.result.id, leaseId: current.lease.id, status: "queued" as const };
|
|
if (current.alreadyQueued) return { receipt, publication: null };
|
|
const now = new Date();
|
|
const retry = { ...receipt, requestId: randomUUID(), actorId: input.actorId, repairNote: input.repairNote, requestedAt: now.toISOString(),
|
|
stoppedProvider: initial.lease.metadata?.remoteExecutionTermination };
|
|
await notifyNativeFinalizationWork(tx, { companyId: current.run.companyId, runId: current.run.id });
|
|
await tx.update(nativeRunFinalizations).set({ phase: "result_accepted", failureCode: null, nextAttemptAt: null,
|
|
failureDetail: { ...current.coordinator.failureDetail, workspaceFinalizeAttempt: 0, workspaceExportRetry: retry }, updatedAt: now }).where(eq(nativeRunFinalizations.runId, current.run.id));
|
|
await tx.update(heartbeatRuns).set({ status: "running", finishedAt: null, error: null, errorCode: null,
|
|
nativePhase: "result_accepted", nativePhaseUpdatedAt: now,
|
|
resultJson: { ...current.run.resultJson, workspaceExportRetry: retry, finalizationPhase: "result_accepted", failureCode: null, originalFailureCode: null, nextAttemptAt: null }, updatedAt: now }).where(eq(heartbeatRuns.id, current.run.id));
|
|
await notifyStatusCardIssueChange(tx, current.run.companyId);
|
|
await tx.update(issues).set({ executionRunId: current.run.id, updatedAt: now }).where(eq(issues.id, current.issue.id));
|
|
// Reclaim the same lease only for control-plane copyback. The normal
|
|
// finalization settlement will stop/retain it again on success or failure.
|
|
await notifyEnvironmentCleanup(tx, { companyId: input.companyId, runId: input.runId });
|
|
await tx.update(environmentLeases).set({ status: "active", releasedAt: null, cleanupStatus: null, failureReason: null,
|
|
metadata: { ...current.lease.metadata, remoteExecutionTermination: undefined,
|
|
// Keep stop-only authority through copyback and commitment. A crash
|
|
// before final release must not make the orphan sweeper destroy it.
|
|
pendingCleanupAttemptId: undefined, pendingCleanupInFlight: false,
|
|
pendingCleanupLeaseExpiresAtMs: 0 }, updatedAt: now }).where(eq(environmentLeases.id, current.lease.id));
|
|
await tx.update(issueRecoveryActions).set({ evidence: { ...current.action.evidence, workspaceExportRetry: retry },
|
|
nextAction: "Workspace export is queued for the accepted result. No provider turn will run.",
|
|
wakePolicy: { kind: "resume_native_run", runId: current.run.id }, updatedAt: now }).where(eq(issueRecoveryActions.id, current.action.id));
|
|
const publication = await persistActivity(tx as unknown as Db, { companyId: input.companyId, actorType: "user", actorId: input.actorId,
|
|
action: "issue.workspace_export_retry_requested", entityType: "issue", entityId: input.issueId, runId: input.runId,
|
|
details: { recoveryActionId: input.actionId, resultId: current.result.id, leaseId: current.lease.id, repairNote: input.repairNote } });
|
|
return { receipt, publication };
|
|
});
|
|
if (admitted.publication) publishActivity(admitted.publication.publication);
|
|
return admitted.receipt;
|
|
} catch (error) {
|
|
if (resumeRequestId) {
|
|
// Admission may have committed before a connection failed. Stop only
|
|
// while our durable intent is still present; never stop a later owner.
|
|
try {
|
|
const [lease] = await input.db.select().from(environmentLeases).where(and(eq(environmentLeases.id, initial.lease.id),
|
|
eq(environmentLeases.companyId, input.companyId), eq(environmentLeases.heartbeatRunId, input.runId),
|
|
eq(environmentLeases.status, "pending_cleanup"),
|
|
sql`${environmentLeases.metadata}->>'pendingCleanupAttemptId' = ${resumeRequestId}`)).limit(1);
|
|
const [otherOwner] = await input.db.select({ id: environmentLeases.id }).from(environmentLeases).where(and(
|
|
ne(environmentLeases.id, initial.lease.id), eq(environmentLeases.provider, initial.lease.provider!),
|
|
eq(environmentLeases.providerLeaseId, initial.lease.providerLeaseId!), inArray(environmentLeases.status, ["active", "retained", "pending_cleanup"]),
|
|
)).limit(1);
|
|
if (lease && !otherOwner && readNativeWorkspaceExportResume(lease)?.requestId === resumeRequestId) {
|
|
let termination: ReturnType<typeof remoteTerminationReceipt>;
|
|
try {
|
|
const receipt = await input.environmentRuntime.retryPendingSandboxTeardown({ environment: initial.environment as unknown as Environment, lease: lease as EnvironmentLease });
|
|
termination = remoteTerminationReceipt(lease, receipt);
|
|
if (termination?.state !== "stopped") termination = undefined;
|
|
} catch { /* The durable stop-only intent remains recoverable. */ }
|
|
await settleNativeWorkspaceExportResume(input.db, lease, { attemptId: resumeRequestId, receipt: termination });
|
|
}
|
|
} catch { /* A database outage leaves the pre-resume intent for recovery. */ }
|
|
}
|
|
throw error;
|
|
}
|
|
});
|
|
if (!owned.acquired) throw conflict("Workspace finalization is still owned by another operation. Wait for it to stop before retrying.", { code: "workspace_export_retry_busy" });
|
|
return owned.value;
|
|
}
|