The open-source app everyone uses to manage agents at work
TypeScript acpxagent-orchestrationagent-skillsagentic-aiai-agentsai-engineeringai-governanceai-toolsautonomous-agentsclaudeclaude-codeclaude-skillscodexhermesllmmcpmulti-agentopenclawproject-managementworkflow-automation
fix: continue native agent chats after worker loss (#13813)
Dotta74a9730acb5a
diff --git a/server/src/services/explicit-native-continuation.test.ts b/server/src/services/explicit-native-continuation.test.tsindex 4c02a743e2..936a81c1a8 100644--- a/server/src/services/explicit-native-continuation.test.ts+++ b/server/src/services/explicit-native-continuation.test.ts@@ -1,3 +1,4 @@+import * as nativeExecutor from "./native-runtime/native-session-executor.js";import { appendHeartbeatRunEvent } from "./heartbeat-run-events.js";import { recordNativeLocalProcessStop, hasNativeLocalProcessStop, PROCESS_START_REQUESTED } from "./native-local-process-stop.js";import { remoteTerminationReceipt } from "./remote-execution-termination.js";@@ -374,6 +375,32 @@ const support = await getEmbeddedPostgresTestSupport();return result;});+ it.each(["verified", "unproven", "changed", "dry_run", "retry", "duplicate"])(+ "requires exact local cleanup for a new turn after worker loss (%s)", async mode => {+ const f = await seed();+ await db.update(heartbeatRuns).set({ errorCode: "native_session_cleanup_quarantined" }).where(eq(heartbeatRuns.id, f.sourceRunId));+ const retire = vi.fn(() => mode !== "changed");+ const verify = vi.spyOn(nativeExecutor, "verifyStoppedNativeSessionForContinuation").mockResolvedValue(+ mode === "unproven" ? null : { evidence: { runId: f.sourceRunId, schema: "paperclip.stopped_native_conversation.v1" }, retire });+ try {+ const result = mode === "retry"+ ? await db.transaction(tx => admitExplicitNativeContinuation({ ...f, db: tx as unknown as typeof db,+ reason: "retry_failed_run", failedRunId: f.sourceRunId }))+ : await admit(f, mode === "dry_run");+ expect(Boolean(result)).toBe(["verified", "dry_run", "duplicate"].includes(mode));+ expect(retire).toHaveBeenCalledTimes(["verified", "changed", "duplicate"].includes(mode) ? 1 : 0);+ const actions = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId));+ expect(Boolean(actions[0].evidence.explicitUserContinuation)).toBe(["verified", "duplicate"].includes(mode));+ const events = await db.select().from(heartbeatRunEvents).where(and(eq(heartbeatRunEvents.runId, f.sourceRunId),+ eq(heartbeatRunEvents.eventType, "native.stopped_conversation_verified")));+ expect(events).toHaveLength(["verified", "duplicate"].includes(mode) ? 1 : 0);+ if (mode === "duplicate") {+ expect(await admit(f)).toBeNull();+ expect(retire).toHaveBeenCalledTimes(1);+ }+ } finally { verify.mockRestore(); }+ });+it.each(["suspended", "ready", "wrong_run", "wrong_thread", "active_provider", "pending_tool", "pending_output", "missing_state", "new_launch"])("recovers a historical run without process metadata only from exact suspended state (%s)", async kind => {const f = await seed();diff --git a/server/src/services/explicit-native-continuation.ts b/server/src/services/explicit-native-continuation.tsindex dc7bf15d65..2063bcf68a 100644--- a/server/src/services/explicit-native-continuation.ts+++ b/server/src/services/explicit-native-continuation.ts@@ -1,3 +1,4 @@+import { appendHeartbeatRunEvent } from "./heartbeat-run-events.js";import { readQueuedInteractionResponse } from "./queued-interaction-response.js";import { isCancelledNativeStartup } from "./cancelled-native-startup.js";import { hasNativeLocalProcessStop, hasHistoricalSuspendedNativeSession } from "./native-local-process-stop.js";@@ -146,6 +147,7 @@ export async function admitExplicitNativeContinuation(input: {if (pendingInteraction || pendingApproval) return blocked("decision_pending", "A pending approval or question must be resolved before this message can start.");const sources: Run[] = [];+ const stoppedSessions: Array<{ evidence: Record<string, unknown>; retire: () => boolean }> = [];const cancelledStartupIds = new Set<string>();for (const action of actions) {const runId = action.evidence.runId ?? action.evidence.sourceRunId;@@ -235,6 +237,14 @@ export async function admitExplicitNativeContinuation(input: {if (run.processGroupId && !processStopped(-run.processGroupId)) return blocked("process_running", "Waiting for the previous process to stop. Your message will start automatically.");}}+ if (!remote && run.runtimeMode === "native" && run.errorCode === "native_session_cleanup_quarantined") {+ // This is new user input, never permission to retry the interrupted turn.+ if (retry) return blocked("cleanup_quarantined", "Send a new message after the previous provider has stopped.");+ const { verifyStoppedNativeSessionForContinuation } = await import("./native-runtime/native-session-executor.js");+ const stopped = await verifyStoppedNativeSessionForContinuation(db, run);+ if (!stopped) return blocked("local_cleanup", "Waiting for the previous provider and its tools to stop. Your message is saved.");+ stoppedSessions.push(stopped);+ }sources.push(run);}const nativeSources = sources.filter(run => run.runtimeMode === "native");@@ -253,6 +263,15 @@ export async function admitExplicitNativeContinuation(input: {context: { previousRunId: previous.id, wakeCommentId: commentId },summary: null, exposeLowTrustRaw: false });if (input.dryRun) return { previousRunId: previous.id, commentId, ...(retry ? { failedRunId: input.failedRunId! } : {}) };+ for (const stopped of stoppedSessions) {+ if (!stopped.retire()) return blocked("local_cleanup", "The previous provider cleanup changed. Your message is saved.");+ await appendHeartbeatRunEvent(db, {+ companyId, runId: String(stopped.evidence.runId), agentId,+ eventType: "native.stopped_conversation_verified", stream: "system", level: "info",+ message: "The old runner and provider stopped. New user input will use a fresh session; prior action outcomes remain recorded.",+ payload: stopped.evidence,+ });+ }const authorization = { actorId, commentId, ...(response ? { interactionId: response.source.interactionId } : {}), ...(retry ? { failedRunId: input.failedRunId } : {}),...(queuedInterrupt ? { queuedCommentInterruptId: input.queuedCommentInterruptId } : {}),...(queuedRequest ? { queuedCommentRequestId: input.queuedCommentRequestId } : {}), runId: input.successorRunId,diff --git a/server/src/services/native-runtime/native-session-executor.test.ts b/server/src/services/native-runtime/native-session-executor.test.tsindex 1cc3797b21..cab3d6de20 100644--- a/server/src/services/native-runtime/native-session-executor.test.ts+++ b/server/src/services/native-runtime/native-session-executor.test.ts@@ -29,6 +29,7 @@ import {createPrpSemanticToolResultEnvelope,validatePrpStructuredRunResult,validatePrpEvent,+ parseNativeExecutionInput,type NativeExecutionInputV1,type PrpEvent,} from "@paperclipai/paperclip-runner";@@ -253,6 +254,7 @@ import {assertRemoteRunnerBuildMetadata,nativeSessionFailureDisposition,nativeFailedRunRetryStateIsSafe,+ verifyStoppedNativeSessionForContinuation,nativePreProviderRetryAfterCleanupStateIsSafe,reconcileRetainedNativeSessionCleanup,retainedNativeCleanupJournalMatches,@@ -3853,6 +3855,85 @@ describe("retained native cleanup activation", () => {});});+describe("stopped native conversation physical cleanup", () => {+ it.each(["codex", "acpx"].flatMap(provider => [+ "stopped", "provider_alive", "worker_alive", "missing_receipt", "wrong_receipt", "new_launch",+ "foreign_run", "foreign_company", "foreign_runner", "remote", "unreleased", "changed_state", "changed_pid", "symlink", "startup_intent", "pending_identity", "wrong_schema", "replacement", "replacement_alive", "agent_alive", "checkpoint_owner_alive", "diagnostic_owner_alive", "normalized_session_receipt",+ ].map(mode => ({ provider, mode }))))("$provider $mode", async ({ provider, mode }) => {+ const base = await mkdtemp(join(tmpdir(), "native-conversation-cleanup-"));+ const previous = process.env.PAPERCLIP_RUNNER_STATE_DIR;+ process.env.PAPERCLIP_RUNNER_STATE_DIR = base;+ const input = parseNativeExecutionInput({ ...execution,+ provider: provider === "codex" ? execution.provider : { kind: "acpx", agent: "claude", model: "claude-sonnet-5", permissionPolicy: "interactive",+ profile: { driverKind: "acpx_runtime", protocolVersion: 1, acpxVersion: "0.13.1", agent: "claude", agentProfileVersion: 1,+ agentServerPackage: "@zed-industries/claude-agent-acp", agentServerVersion: "1", agentRuntimePackage: null,+ agentRuntimeVersion: null, commandDigest: "fixture" } },+ session: { ...execution.session, driverKind: provider === "codex" ? "codex_app_server" : "acpx_runtime" },+ });+ const canonical = (value: any): string => value && typeof value === "object" && !Array.isArray(value)+ ? `{${Object.entries(value).filter(([, v]) => v !== undefined).sort(([a], [b]) => a.localeCompare(b)).map(([k,v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`+ : JSON.stringify(value);+ const hash = (value: unknown) => createHash("sha256").update(canonical(value)).digest("hex");+ const providerScope = input.provider.kind === "acpx" ? { kind: "acpx", agent: input.provider.agent, profile: input.provider.profile } : { kind: "codex" };+ const root = join(base, hash({ schema: "paperclip.native-session-scope.v2", companyId: input.binding.companyId,+ agentId: input.binding.agentId, workspace: { kind: "managed", executionWorkspaceId: input.binding.executionWorkspaceId },+ provider: { driverKind: input.session.driverKind, identity: providerScope }, normalizedSessionId: input.session.normalizedSessionId }));+ const identity = { runId: input.binding.runId, normalizedSessionId: input.session.normalizedSessionId,+ runnerInstanceId: "runner-crashed", environmentLeaseId: "lease", turnId: "turn", itemId: "item" };+ const event = { schema: "paperclip.prp.event.v1", schemaVersion: 1, sourceKind: "runner", sourceSeq: 1,+ sourceEventId: "provider-identity", sourceInstanceId: identity.runnerInstanceId, runId: identity.runId,+ normalizedSessionId: identity.normalizedSessionId, turnId: identity.turnId, itemId: identity.itemId,+ priority: 0, emittedAt: new Date().toISOString(), eventType: "session.started",+ payload: { providerDescriptor: { agentProcessId: mode === "agent_alive" ? process.pid : 99_999_996 }, providerSessionId: "provider-session", processId: mode === "provider_alive" ? process.pid : 99_999_998 } };+ const replacement = { ...event, sourceSeq: 2, sourceEventId: "replacement", eventType: "session.reconciled",+ payload: { ...event.payload, processId: mode === "replacement_alive" ? process.pid : 99_999_997, previousProcessId: event.payload.processId } };+ const providerEvents = mode.startsWith("replacement") ? [event, replacement] : [event];+ if (mode === "diagnostic_owner_alive") providerEvents.push({ ...event, eventType: "harness.diagnostic",+ payload: { providerMethod: "acpx/process", role: "acp_agent", pid: process.pid } } as unknown as typeof event);+ const providerState = { schema: mode === "wrong_schema" ? "unknown" : `paperclip.runner.${provider}-provider-state.${provider === "codex" ? "v1" : "v3"}`, lifecycle: "turn_active",+ ...(mode === "startup_intent" ? { startupAttempt: { phase: "intent" } } : {}),+ ...(mode === "pending_identity" ? { pendingEvents: [{ eventType: "session.started" }] } : {}),+ };+ const normalizedEvent = mode === "normalized_session_receipt" ? { ...event, turnId: undefined, itemId: undefined } : event;+ const receipt = { sourceEventId: `${identity.runnerInstanceId}:${identity.runId}:1`,+ sourcePayloadSha256: mode === "wrong_receipt" ? "wrong" : hash(normalizedEvent), payload: { prpEvent: normalizedEvent } };+ const stop = { eventType: mode === "new_launch" ? "native.process_start_requested" : "native.local_process_stopped",+ payload: { processPid: mode === "worker_alive" ? process.pid : 99_999_999, processGroupId: mode === "worker_alive" ? process.pid : 99_999_999 } };+ let queryIndex = 0;+ const db = { select() { const rows = [+ [{ provider: mode === "remote" ? "daytona" : "local", releasedAt: mode === "unreleased" ? null : new Date() }],+ [stop], mode === "missing_receipt" ? [] : [receipt, ...(mode.startsWith("replacement") ? [{ ...receipt, sourceEventId: `${identity.runnerInstanceId}:${identity.runId}:2`, sourcePayloadSha256: hash(replacement), payload: { prpEvent: replacement } }] : [])],+ ][queryIndex++] ?? [];+ const q: any = { from: () => q, where: () => q, orderBy: () => q, limit: () => q,+ then: Promise.resolve(rows).then.bind(Promise.resolve(rows)) }; return q;+ } } as unknown as Db;+ const run = { id: input.binding.runId, companyId: mode === "foreign_company" ? "foreign" : input.binding.companyId,+ agentId: input.binding.agentId, nativeIssueId: input.binding.issueId, runtimeMode: "native", status: "failed", finishedAt: new Date(),+ nativeSessionId: input.session.normalizedSessionId, runnerInstanceId: mode === "foreign_runner" ? "foreign" : identity.runnerInstanceId,+ runnerProfileJson: { sessionCheckpoint: { process: { codexPid: mode === "checkpoint_owner_alive" ? process.pid : null } }, nativeExecutionInput: input, nativeToolContractFingerprint: nativeToolContractFingerprintForTarget("local") } } as unknown as typeof heartbeatRuns.$inferSelect;+ try {+ await mkdir(join(root, "runner"), { recursive: true });+ await mkdir(join(root, "control-plane"), { recursive: true });+ const runnerPath = join(root, "runner/runner-state.json");+ await writeFile(join(root, "control-plane/control-plane-state.json"), JSON.stringify({ ...durableControlPlaneState(identity), committedEvents: providerEvents.map(payload => ({ envelope: { payload } })) }));+ await writeFile(runnerPath, JSON.stringify(durableRunnerState({ ...identity, ...(mode === "foreign_run" ? { runId: "foreign" } : {}) }, "ready")));+ await writeFile(join(root, `runner/${provider}-provider-state.json`), JSON.stringify(providerState));+ if (mode === "symlink") { await rename(runnerPath, join(base, "external")); await symlink(join(base, "external"), runnerPath); }+ const proof = await verifyStoppedNativeSessionForContinuation(db, run);+ if (["stopped", "changed_state", "changed_pid", "replacement", "normalized_session_receipt"].includes(mode)) {+ expect(proof).not.toBeNull();+ if (mode === "changed_state") await writeFile(runnerPath, "{}");+ const kill = mode === "changed_pid" ? vi.spyOn(process, "kill").mockReturnValue(true) : null;+ try { expect(proof!.retire()).toBe(["stopped", "replacement", "normalized_session_receipt"].includes(mode)); } finally { kill?.mockRestore(); }+ expect(await readFile(join(root, `runner/${provider}-provider-state.json`), "utf8")).toBe(JSON.stringify(providerState));+ } else expect(proof).toBeNull();+ } finally {+ if (previous === undefined) delete process.env.PAPERCLIP_RUNNER_STATE_DIR; else process.env.PAPERCLIP_RUNNER_STATE_DIR = previous;+ await rm(base, { recursive: true, force: true });+ }+ });+});+describe("explicit failed native retry physical evidence", () => {it.each(["suspended",diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.tsindex 8a8bf41cd7..41b0262134 100644--- a/server/src/services/native-runtime/native-session-executor.ts+++ b/server/src/services/native-runtime/native-session-executor.ts@@ -1756,7 +1756,7 @@ const CLEANUP_CANONICAL_FILES = [] as const;const CLEANUP_ACTIVATION_FILE = "cleanup-activation.json";-function cleanupStateSnapshot(root: string) {+function cleanupStateSnapshot(root: string, providerFile = "codex-provider-state.json") {if (![root, resolve(root, "runner"), resolve(root, "control-plane")].every(isSafeNativeStateDirectory,@@ -1764,7 +1764,8 @@ function cleanupStateSnapshot(root: string) {) {throw new Error("native_cleanup_maintenance_unproven");}- const bytes = CLEANUP_CANONICAL_FILES.map((file) =>+ const files = [...CLEANUP_CANONICAL_FILES.slice(0, 2), `runner/${providerFile}`];+ const bytes = files.map((file) =>readBoundedNativeFile(resolve(root, file),NATIVE_RUNNER_STATE_MAX_BYTES,@@ -2362,6 +2363,117 @@ export function rebaseRetainedNativeCleanupProviderHome(}}+/** Physical cleanup for an explicitly authorized NEW conversation turn. Unlike+ * automatic replacement, this does not certify or replay the interrupted actions.+ * The caller holds the issue/controller/run locks and preserves their history.+ * Retain the old durable root permanently; only the exact in-memory cleanup+ * owner is retired, and the successor must use a fresh normalized session.+ */+export async function verifyStoppedNativeSessionForContinuation(+ db: Db,+ run: typeof heartbeatRuns.$inferSelect,+): Promise<{ evidence: Record<string, unknown>; retire: () => boolean } | null> {+ try {+ if (run.runtimeMode !== "native" || !["failed", "cancelled", "interrupted", "timed_out"].includes(run.status) ||+ !run.finishedAt || !run.nativeIssueId || !run.nativeSessionId || !run.runnerInstanceId) return null;+ const execution = parseNativeExecutionInput(record(run.runnerProfileJson).nativeExecutionInput);+ if (!(["codex", "acpx"] as string[]).includes(execution.provider.kind) ||+ execution.binding.runId !== run.id || execution.binding.companyId !== run.companyId ||+ execution.binding.agentId !== run.agentId || execution.binding.issueId !== run.nativeIssueId ||+ nativeSessionKey(execution) !== run.nativeSessionId ||+ record(run.runnerProfileJson).nativeToolContractFingerprint !== nativeToolContractFingerprintForTarget("local")) return null;+ const scope = nativeSessionScopeKey(execution);+ const idle = () => !activeNativeSessions.has(run.id) && !executingRunnerdSessionScopes.has(scope) &&+ !initializingSessionToolAuthorities.has(scope) && !warmNativeSessions.has(scope);+ if (!idle()) return null;+ const leases = await db.select().from(environmentLeases).where(and(+ eq(environmentLeases.companyId, run.companyId), eq(environmentLeases.heartbeatRunId, run.id)));+ if (leases.some(lease => lease.provider !== "local" || !lease.releasedAt || lease.cleanupStatus === "failed")) return null;+ const stopped = await readNativeLocalProcessStop(db, run.companyId, run.id);+ if (!stopped) return null;+ const root = scopedRunnerdStateRoot(execution);+ const providerFile = runnerProviderStateFilename(execution);+ const snapshot = cleanupStateSnapshot(root, providerFile);+ const identity = record(snapshot.control.identity);+ if (!durableIdentityMatchesExecution(identity, execution) || identity.runnerInstanceId !== run.runnerInstanceId ||+ !["runnerInstanceId", "environmentLeaseId", "runId", "normalizedSessionId", "turnId", "itemId"].every(key =>+ typeof identity[key] === "string" && identity[key] && snapshot.runner[key] === identity[key]) ||+ !Array.isArray(snapshot.control.committedEvents)) return null;+ // An incomplete provider launch can own a process that never emitted its+ // session identity. It cannot be certified from an earlier owner's receipt.+ if (snapshot.control.schema !== "paperclip.runner.durable.control-plane-state.v1" ||+ snapshot.runner.schema !== RUNNERD_STATE_SCHEMA ||+ snapshot.provider.schema !== (execution.provider.kind === "codex" ? "paperclip.runner.codex-provider-state.v1" : ACPX_PROVIDER_STATE_SCHEMA) ||+ !["turn_active", "prepared", "suspended"].includes(String(snapshot.provider.lifecycle)) ||+ snapshot.provider.startupAttempt != null) return null;+ const pending = JSON.stringify([snapshot.runner.outbox, snapshot.provider.pendingEvents, snapshot.provider.queuedEvents]);+ if (/session\.(started|resumed|reconciled)/.test(pending)) return null;+ const events = snapshot.control.committedEvents.map(entry => record(record(record(entry).envelope).payload));+ const providerEvents = events.filter(event => ["session.started", "session.resumed", "session.reconciled"].includes(String(event.eventType)));+ if (!providerEvents.length) return null;+ const receipts = await db.select().from(heartbeatRunEvents).where(and(+ eq(heartbeatRunEvents.companyId, run.companyId), eq(heartbeatRunEvents.runId, run.id),+ eq(heartbeatRunEvents.sourceInstanceId, run.runnerInstanceId),+ inArray(heartbeatRunEvents.eventType, ["session.started", "session.resumed", "session.reconciled", "harness.diagnostic"])));+ const providerPids = new Set<number>();+ for (const event of providerEvents) {+ const provider = record(event.payload);+ if (!validatePrpEvent(event).ok || event.sourceKind !== "runner" || event.sourceInstanceId !== run.runnerInstanceId ||+ event.runId !== run.id || event.normalizedSessionId !== run.nativeSessionId ||+ typeof provider.providerSessionId !== "string" || !provider.providerSessionId ||+ !cleanupProcessAbsent(provider.processId) || provider.processId === stopped.processPid) return null;+ if (event.eventType === "session.reconciled" &&+ (!providerPids.has(Number(provider.previousProcessId)) || !cleanupProcessAbsent(provider.previousProcessId))) return null;+ const receipt = receipts.find(row => row.sourceEventId === `${run.runnerInstanceId}:${run.id}:${event.sourceSeq}`);+ const durable = record(record(receipt?.payload).prpEvent);+ const durableProvider = record(durable.payload);+ if (!receipt || !validatePrpEvent(durable).ok || durable.sourceInstanceId !== event.sourceInstanceId ||+ durable.runId !== run.id || durable.normalizedSessionId !== run.nativeSessionId ||+ // Normalized session-open receipts omit turn/item IDs. Those belong+ // to the durable runner identity checked above, not the open event.+ durable.sourceSeq !== event.sourceSeq || durable.eventType !== event.eventType ||+ receipt.sourcePayloadSha256 !== nativeSha256(durable) ||+ (durableProvider.processId !== undefined && durableProvider.processId !== provider.processId) ||+ (durableProvider.driverSessionId ?? durableProvider.providerSessionId) !== provider.providerSessionId) return null;+ providerPids.add(provider.processId);+ }+ if (receipts.filter(row => record(record(row.payload).prpEvent).eventType !== "harness.diagnostic").length !== providerEvents.length) return null;+ // ACPX's sidecar is not its agent process. Include separately recorded+ // owners from both the durable journal and the independent DB/checkpoint.+ // Extra evidence can only add a stop requirement, never waive one.+ const owners: Record<string, unknown>[] = [record(record(run.runnerProfileJson?.sessionCheckpoint).process),+ snapshot.provider, record(snapshot.provider.identity), record(snapshot.provider.descriptor)];+ for (const event of [...events, ...receipts.map(row => record(record(row.payload).prpEvent))]) {+ const payload = record(event.payload);+ if (["session.started", "session.resumed", "session.reconciled"].includes(String(event.eventType))) {+ owners.push(payload, record(payload.providerDescriptor), record(payload.providerIdentity), record(payload.runtimeIdentity));+ } else if (event.eventType === "harness.diagnostic" && payload.providerMethod === "acpx/process") {+ owners.push({ agentPid: payload.pid });+ }+ }+ for (const owner of owners) for (const key of [+ "processId", "process_id", "processGroupId", "providerPid", "codexPid", "sidecarPid", "agentPid", "agentProcessId",+ ]) {+ const pid = owner[key];+ if (pid === null || pid === undefined) continue;+ if (!cleanupProcessAbsent(pid)) return null;+ providerPids.add(pid);+ }+ const unchanged = () => idle() && cleanupProcessAbsent(stopped.processPid) &&+ [...providerPids].every(cleanupProcessAbsent) && cleanupStateSnapshot(root, providerFile).fingerprint === snapshot.fingerprint;+ return {+ evidence: { schema: "paperclip.stopped_native_conversation.v1", runId: run.id,+ nativeSessionId: run.nativeSessionId, runnerInstanceId: run.runnerInstanceId,+ processPid: stopped.processPid, providerPids: [...providerPids], stateFingerprint: snapshot.fingerprint },+ retire: () => {+ try { return unchanged() && completeTerminatedLocalNativeSessionCleanup({+ companyId: run.companyId, runId: run.id, runnerInstanceId: run.runnerInstanceId!,+ }); } catch { return false; }+ },+ };+ } catch { return null; }+}+/** Prove that a crashed local Codex turn ended without external effects. No provider* is launched and no retained state is rewritten. The successor must use a fresh* normalized session; the old directory remains available for investigation. */@@ -5909,7 +6021,7 @@ export function nativeSessionFailureSourceCode(}const NATIVE_CLEANUP_OPERATOR_RECOVERY_MESSAGE =- "Verify the prior session's retained process ownership and checkpoint before a controlled server restart and explicit task retry. Clearing a task session does not resolve this quarantine. Automatic retries are stopped.";+ "Send a new message to continue after Paperclip verifies that the previous provider and its tools have stopped. If cleanup cannot be verified, inspect the run and its environment. Clearing a task session does not resolve this quarantine. Automatic retries are stopped.";const PROVIDER_DURABLE_EVENT_TYPES = new Set(["harness.ready",diff --git a/tests/runner-e2e/README.md b/tests/runner-e2e/README.mdindex 88c0477358..15b642759e 100644--- a/tests/runner-e2e/README.md+++ b/tests/runner-e2e/README.md@@ -903,12 +903,19 @@ successor-owned document. It budgets three provider runs.Worker recovery requires Linux with Python pidfd support (as on the CI workers).It kills only the exact running native worker PID from the publicrun record, after verifying its command-line run ID, process start identity, and isolated-local workspace. The signal uses an owned pidfd so PID reuse cannot retarget it. The real UI must show one Retry button. Clicking it must produce one-successful attempt, consume the original message once, preserve the saved plan,-and return a reference that was supplied only after the crash. This qualifies-**user-initiated Retry after worker process loss**, not automatic recovery of-arbitrary provider failures. It budgets two provider runs. Unexpected failures-remain fatal; only the positively identified injected-fault run is exempted.+local workspace. The signal uses an owned pidfd so PID reuse cannot retarget it.+The UI must preserve the plan and withhold generic Retry while cleanup remains+quarantined; the public retry API must return 409 without admitting another run.+The fixture then releases the read-only brief wait and sends a new chat message+that records the known saved-plan and interrupted-command outcomes. The server+must verify that the recorded worker and provider process groups stopped before+admitting exactly one successful fresh session. The answer must contain the+reference supplied only after the crash, and the saved plan must remain unchanged. The old quarantined run must not regain+a misleading Try again control after the fresh turn succeeds.+This qualifies **explicit conversation continuation after local worker loss**.+It does not qualify replay of uncertain actions, automatic recovery, remote worker+loss, or exact-session resumption. It budgets two provider runs. Unexpected+failures remain fatal; only the positively identified injected-fault run is exempted.Answer quality uses two read-only turns over public fixture tasks and conflictinghistorical comments. Exact structured propositions grade current blockers,@@ -933,7 +940,6 @@ it does not certify a native option in the wizard, which is not offered yet.Current proof and remaining decisions are recorded in[the 21 September qualification report](QUALIFICATION-2026-09-21.md). In particular,-worker loss currently quarantines both providers. The crash eval retains a red-qualification result when no usable recovery exists, while also verifying that-quarantine preserves the plan and rejects a misleading generic Retry. A passing-quarantine guard is not a recovered workflow.+the original worker-loss attempts quarantined both providers. The version 9 crash+eval requires a usable fresh conversation after verified cleanup. A passing+quarantine guard alone is not a recovered workflow.diff --git a/tests/runner-e2e/catalog.ts b/tests/runner-e2e/catalog.tsindex 4f39bec921..28fa16c2d3 100644--- a/tests/runner-e2e/catalog.ts+++ b/tests/runner-e2e/catalog.ts@@ -975,7 +975,7 @@ export const runnerSuites: readonly RunnerSuiteFixture[] = [profiles: runnerProfiles.filter(profile => ["runner-codex", "runner-acpx-claude"].includes(profile.id)).map(profile => productionStoryProfile(defaultPermissionProfile(profile))),environments: [localEnvironment], tasks: chatQualificationTasks, expectedMatrixSize: 6,- definitionMetadata: { version: 6, permissions: "production-defaults", instructions: "production", crashBoundary: "verified-native-worker-pid-at-file-wait", recovery: "user-visible-retry", answerGrading: "exact-grounded-propositions-plus-separate-semantic-review", scheduling: "explicit-only" },+ definitionMetadata: { version: 9, permissions: "production-defaults", instructions: "production", crashBoundary: "verified-native-worker-pid-at-file-wait", recovery: "new-user-message-after-verified-cleanup", answerGrading: "exact-grounded-propositions-plus-separate-semantic-review", scheduling: "explicit-only" },},...(process.env.PAPERCLIP_RUNNER_E2E_CONNECTION_REVIEWS === "1" ? [connectionReviewSuite] : []),{diff --git a/tests/runner-e2e/chat-qualification.test.ts b/tests/runner-e2e/chat-qualification.test.tsindex ed45d52d80..224e0b96ce 100644--- a/tests/runner-e2e/chat-qualification.test.ts+++ b/tests/runner-e2e/chat-qualification.test.ts@@ -5,12 +5,12 @@ import { assertActiveHandoff, assertAnswerFacts, assertCrashRecovered, assertWorimport { runnerMatrix } from "./catalog.js";import { buildRunnerE2EProcessEnvironment } from "./harness-env.js";-const run = { id: "old", agentId: "original", companyId: "company", runtimeMode: "native", status: "cancelled", errorCode: "issue_reassigned", finishedAt: "2026-09-21T00:01:00Z", contextSnapshot: { issueId: "task" } };+const run = { id: "old", nativeSessionId: "old-session", agentId: "original", companyId: "company", runtimeMode: "native", status: "cancelled", errorCode: "issue_reassigned", finishedAt: "2026-09-21T00:01:00Z", contextSnapshot: { issueId: "task" } };const successor = { ...run, id: "next", agentId: "successor", status: "succeeded", startedAt: "2026-09-21T00:01:01Z" };const before = { id: "task", assigneeAgentId: "original", description: "Preserve scope", projectId: null };const plan = { body: "Friday REFERENCE", latestRevisionId: "revision" };const handoff = { before, after: { ...before, status: "done", assigneeAgentId: "successor" }, oldRun: run, boundary: { ...run, status: "running" }, runs: [run, successor], successorId: "successor", planBefore: plan, planAfter: plan, draft: { id: "draft", latestRevisionId: "v1", body: "REFERENCE" }, draftAfter: { id: "draft", body: "REFERENCE" }, draftRevisions: [{ id: "v1", body: "REFERENCE" }], output: { body: "REFERENCE", createdByAgentId: "successor" }, reference: "REFERENCE", audit: [{ action: "issue.reassigned", details: { source: "paperclip_runner_protocol" } }], taskIds: ["task"] };-const recovery = { boundary: { ...run, status: "running" }, failed: { ...run, status: "failed" }, runs: [{ ...run, status: "failed" }, { ...successor, agentId: "original" }], issueId: "task", prompt: "Read my brief", comments: [{ body: "Read my brief" }, { body: "REFERENCE MARKER", authorAgentId: "original", createdByRunId: "next" }], reference: "REFERENCE", marker: "MARKER", planBefore: { body: "plan MARKER", latestRevisionId: "v1" }, planAfter: { body: "plan MARKER", latestRevisionId: "v1" } };+const recovery = { boundary: { ...run, status: "running" }, failed: { ...run, status: "failed" }, runs: [{ ...run, status: "failed" }, { ...successor, agentId: "original", nativeSessionId: "fresh-session", contextSnapshot: { issueId: "task", previousRunId: "old", forceFreshSession: true } }], issueId: "task", prompt: "Read my brief", comments: [{ body: "Read my brief" }, { body: "REFERENCE MARKER", authorAgentId: "original", createdByRunId: "next" }], reference: "REFERENCE", marker: "MARKER", planBefore: { body: "plan MARKER", latestRevisionId: "v1" }, planAfter: { body: "plan MARKER", latestRevisionId: "v1" } };describe("remaining native chat qualification", () => {it("calibrates the pidfd helper against reuse and wrong-identity faults", () => {@@ -44,6 +44,8 @@ describe("remaining native chat qualification", () => {{ failed: { ...recovery.failed, id: "unrelated" } },{ runs: [recovery.runs[0]!, { ...successor, status: "failed" }] },{ runs: [recovery.runs[0]!, { ...successor, contextSnapshot: { issueId: "new-chat" } }] },+ { runs: [recovery.runs[0]!, { ...recovery.runs[1]!, nativeSessionId: "old-session" }] },+ { runs: [recovery.runs[0]!, { ...recovery.runs[1]!, contextSnapshot: { issueId: "task", previousRunId: "unrelated", forceFreshSession: true } }] },{ comments: [...recovery.comments, recovery.comments[0]!] },{ comments: [recovery.comments[0]!, { ...recovery.comments[1], createdByRunId: "old" }] },{ comments: [recovery.comments[0]!, { ...recovery.comments[1], body: "MARKER" }] },diff --git a/tests/runner-e2e/chat-qualification.ts b/tests/runner-e2e/chat-qualification.tsindex 102016ce82..a57cb170d3 100644--- a/tests/runner-e2e/chat-qualification.ts+++ b/tests/runner-e2e/chat-qualification.ts@@ -63,6 +63,9 @@ export function assertCrashRecovered(e: {expect(e.runs.find(r => r.id === e.failed.id)).toMatchObject({ status: "failed", contextSnapshot: { issueId: e.issueId } });const retry = e.runs.find(r => r.id !== e.failed.id)!;expect(retry).toMatchObject({ agentId: e.failed.agentId, status: "succeeded", runtimeMode: "native", contextSnapshot: { issueId: e.issueId } });+ expect(retry.contextSnapshot).toMatchObject({ previousRunId: e.failed.id, forceFreshSession: true });+ expect((retry as Row).nativeSessionId).not.toBe(e.failed.nativeSessionId);+ expect((retry as Row).nativeSessionId).toBeTruthy();expect(e.comments.filter(c => !c.authorAgentId && c.body === e.prompt)).toHaveLength(1);const replies = e.comments.filter(c => c.authorAgentId && c.body.includes(e.marker));expect(replies).toHaveLength(1);@@ -145,7 +148,7 @@ export async function runWorkerCrash(context: Context) {const processIdentity = JSON.parse(execFileSync("python3", [faultHelper, "inspect", String(boundary.processPid), boundary.id], { encoding: "utf8" }));const command = execFileSync("ps", ["-p", String(boundary.processPid), "-o", "command="], { encoding: "utf8" });assertWorkerIdentity(boundary, command, input.execution.environment.id);- await input.evidence("chat-worker-fault.json", { boundary, planBefore, processIdentity, fault: "SIGKILL through verified Linux pidfd", recovery: "visible Retry button" });+ await input.evidence("chat-worker-fault.json", { boundary, planBefore, processIdentity, fault: "SIGKILL through verified Linux pidfd", recovery: "new user message after verified provider cleanup" });const fault = JSON.parse(execFileSync("python3", [faultHelper, "kill", String(boundary.processPid), boundary.id, processIdentity.startTicks], { encoding: "utf8" }));expect(fault.signalled).toBe(true);await input.evidence("chat-worker-fault-delivered.json", fault);@@ -156,10 +159,11 @@ export async function runWorkerCrash(context: Context) {let failed: Row = {};await expect.poll(async () => {failed = await input.api.get<Row>(`/api/heartbeat-runs/${boundary.id}`);- return ["failed", "recovery_needed"].includes(failed.execution?.phase);+ return failed.status === "failed" && failed.finishedAt && failed.nativePhase === "terminal_failure" &&+ ["failed", "recovery_needed"].includes(failed.execution?.phase);}, { timeout: 120_000 }).toBe(true);await input.evidence("chat-worker-settled-failure.json", failed);- await input.capture("worker-failed", "Worker loss before user Retry", "worker-failed.png");+ await input.capture("worker-failed", "Worker loss before a fresh user turn", "worker-failed.png");await writeFile(wait.gate, reference);if (failed.errorCode === "native_session_cleanup_quarantined") {// Preserve the red qualification result, but verify the stop is honest:@@ -167,7 +171,7 @@ export async function runWorkerCrash(context: Context) {await input.page.reload({ waitUntil: "domcontentloaded" });await expect(input.page.getByTestId("task-chat-composer-input")).toBeVisible();await expect(input.page.getByRole("status", { name: "Task recovery" }).getByRole("link", { name: "Inspect run" })).toBeVisible();- await expect(input.page.getByRole("button", { name: "Retry", exact: true })).toHaveCount(0);+ await expect(input.page.getByRole("button", { name: /^(Retry|Try again)$/ })).toHaveCount(0);const refused = await input.api.request.post(`/api/agents/${input.fixtures.agent.id}/wakeup`, {data: { failedRunId: failed.id, reason: "retry_failed_run" },});@@ -177,23 +181,22 @@ export async function runWorkerCrash(context: Context) {await input.evidence("chat-worker-quarantine.json", { failed, retryStatus: refused.status(), savedPlanPreserved: true,usableRecovery: false, classification: "product recovery boundary; no provider retry was admitted" });await input.capture("worker-quarantined", "Worker recovery requires reconciliation", "worker-quarantined.png");- throw new Error("worker_crash_recovery_unqualified: native_session_cleanup_quarantined requires explicit reconciliation; generic Retry is correctly unavailable");+ // Reconcile the fixture's known outcomes through ordinary user input.+ // The saved plan is verified above; the only interrupted command reads a+ // local brief. The new provider must discover its reference from the file.+ await sendChatMessage(input.page, `The prior worker failed. The plan is already saved and must remain unchanged. The interrupted command only waited for my local brief, which is now available. Continue in a fresh conversation turn: read the brief using node ${wait.scriptPath}, then reply with its reference and ${marker}. Do not create tasks or projects.`);+ }+ else {+ throw new Error(`worker_crash_unexpected_recovery_boundary: ${failed.errorCode}`);}- const retry = input.page.getByRole("button", { name: "Retry", exact: true });- await expect(retry).toHaveCount(1, { timeout: 60_000 });- const [retryResponse] = await Promise.all([- input.page.waitForResponse(response => response.request().method() === "POST" &&- new URL(response.url()).pathname === `/api/agents/${input.fixtures.agent.id}/wakeup`),- retry.click(),- ]);- await input.evidence("chat-worker-retry-response.json", { status: retryResponse.status(), body: await retryResponse.json() });- expect(retryResponse.ok(), "The visible Retry must admit an attempt before waiting for its result").toBe(true);await context.idle(2);const e = { boundary, failed, runs: await context.allRuns(), issueId: context.issue().id,prompt, comments: await context.comments(), reference, marker, planBefore,planAfter: await input.api.get<Row>(`/api/issues/${context.issue().id}/documents/plan`) };await input.evidence("chat-worker-recovery.json", e);assertCrashRecovered(e);+ // A fresh success must not re-enable Retry on the quarantined historical run.+ await expect(input.page.getByTestId("task-chat-run-failed-try-again")).toHaveCount(0);expect(await input.api.get(`/api/companies/${input.fixtures.company.id}/issues`)).toEqual([]);} finally {await writeFile(wait.gate, reference);diff --git a/ui/src/components/TaskChatThread.test.tsx b/ui/src/components/TaskChatThread.test.tsxindex d8c060cfa7..1df30e6039 100644--- a/ui/src/components/TaskChatThread.test.tsx+++ b/ui/src/components/TaskChatThread.test.tsx@@ -1170,6 +1170,20 @@ describe("TaskChatThread runtime transcript selection", () => {expect(onRetryFailedRun).toHaveBeenCalledExactlyOnceWith("failed-bootstrap");});+ it.each([false, true])("never offers the old quarantined run as Try again after continuation: %s", continued => {+ render(<TaskChatThread comments={[]} onAdd={async () => {}} issueStatus="in_progress"+ onRetryFailedRun={vi.fn()} linkedRuns={[{+ runId: "quarantined", runtimeMode: "native", status: "failed", errorCode: "native_session_cleanup_quarantined",+ agentId: "agent-1", agentName: "Runner", adapterType: "paperclip_runner",+ createdAt: "2026-08-25T18:00:00.000Z", startedAt: "2026-08-25T18:00:00.000Z", finishedAt: "2026-08-25T18:00:02.000Z",+ ...(continued ? { execution: { phase: "completed" as const, label: "Continued in another run", cause: "native_session_cleanup_quarantined",+ lastConfirmedActivityAt: null, retryAt: null, attempt: 2, maxAttempts: 3, recoveryOwner: null, nextAction: null,+ permittedActions: ["inspect_run" as const], predecessorRunId: null, successorRunId: "fresh-run" } } : {}),+ }]} />);+ expect(container.textContent).toContain("Run failed");+ expect(container.querySelector('[data-testid="task-chat-run-failed-try-again"]')).toBeNull();+ });+it("explains a native provider usage limit without exposing its error code", async () => {const onRetryFailedRun = vi.fn();render(diff --git a/ui/src/components/TaskChatThread.tsx b/ui/src/components/TaskChatThread.tsxindex 7047ab1418..6c468c2562 100644--- a/ui/src/components/TaskChatThread.tsx+++ b/ui/src/components/TaskChatThread.tsx@@ -1614,6 +1614,7 @@ export function TaskChatThread(props: TaskChatThreadProps) {tone: source.status === "cancelled" ? "neutral" : "error",label,detail,+ retryable: code !== "native_session_cleanup_quarantined",collapsible: true,runId: source.id,createdAtIso: finishedAtdiff --git a/ui/src/components/task-chat/TaskChatThreadView.tsx b/ui/src/components/task-chat/TaskChatThreadView.tsxindex 52d900ecbf..0046380e0e 100644--- a/ui/src/components/task-chat/TaskChatThreadView.tsx+++ b/ui/src/components/task-chat/TaskChatThreadView.tsx@@ -172,7 +172,7 @@ function renderItem(<TaskChatMarkeritem={item}onTryAgain={- item.id === retryableMarkerId+ item.id === retryableMarkerId && item.retryable !== false? item.runId && onRetryFailedRun? () => onRetryFailedRun(item.runId!): onTryAgainNoLiveExecutionPathdiff --git a/ui/src/components/task-chat/task-chat-model.ts b/ui/src/components/task-chat/task-chat-model.tsindex 9dbab8ada1..717b9b36b5 100644--- a/ui/src/components/task-chat/task-chat-model.ts+++ b/ui/src/components/task-chat/task-chat-model.ts@@ -246,6 +246,8 @@ export interface TaskChatMarkerItem {variant: "session_start" | "interrupted" | "turn_boundary";label: string;detail?: string;+ /** False when the recorded run cannot be retried, even after the chat continues. */+ retryable?: boolean;/** Renders the marker as a quiet disclosure row with detail beneath it. */collapsible?: boolean;/** Expected cancellation is neutral; unexpected failures remain destructive. */