Files
PaperClipAI/tests/runner-e2e/everyday-remote-restart.ts
T
DottaandPaperclip 6987c2f20b test(upgrades): qualify controller replacement with immutable artifacts and phase evidence (#17455)
<!-- entire-trail-link-start -->
https://entire.com/gh/paperclipai/-/trails/105/changes/paperclip/105
<!-- entire-trail-link-end -->

## Thinking Path

> - Paperclip needs evidence that an admitted execution survives a
main-server replacement.
> - Runner mechanics and server adoption belong to #17097 and #17439.
> - A healthy HTTP endpoint does not prove recovery of the original
execution.
> - Qualification must identify the serving artifact, natural ownership,
authenticated attachment and resumed processing separately.
> - It must also retain failed runs and measure normal-operation costs.
> - This PR extends the existing qualification machinery and acceptance
record.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

Operators can assess upgrade continuity from phase evidence tied to
exact artifacts and profiles.

**Current behavior**

A same-artifact restart or synthetic executor pass cannot establish
different-artifact upgrade and rollback support. Idle SQL gates also do
not measure active-run capacity.

**Proposed behavior**

Use immutable A/B artifacts and strict phase receipts through the
existing supervisor. Preserve failed evidence and add measured
before/after workload costs.

Builds on merged #17097 and #17439. The base is now `master`. #17456
follows for review convenience. This PR lands qualification machinery
and the evidence record. The remaining hosted gates stay open for
deployment qualification; merging this machinery does not claim those
experiments passed.

## What Changed

- Extend the main-server supervisor and installed-artifact verification
for actual serving revisions, forward migrations and compatible
rollback.
- Distinguish HTTP readiness, natural ownership, authenticated
attachment, resumed processing and backlog settlement.
- Retain the original reconnect grace and execution identity checks.
Never substitute a new task or edited lease for continuity.
- Reuse real-harness and Daytona qualification paths, with exact profile
and artifact provenance.
- Bind current Copilot attestation inputs in the Daytona image
fingerprint.
- Keep one acceptance record, including both failed file-only stress
phases and the final passing bounded-pool comparison.

## Verification

After retargeting to master, all 158 focused qualification cases across
13 files pass, with E2E typecheck. The first 12-file batch has four
report-lock test failures and 133 passes. Master #17811 had committed
the exact already-approved output lock, while that fixture assumed HEAD
must be the uncorrected input. The fixture now checks both pinned forms,
executes the committed-lock path, and reconstructs the pinned input to
retain all rejection tests. The production helper, policy hashes, patch
and dependency resolution are unchanged. The corrected batch explicitly
includes both process-preservation suites; the prior 146-case receipt
has its own source and selection.

Current candidate `8f1d6ec02f` integrates master `09ad9adce2`, including
the merged server slice. Its diff contains only qualification code and
documentation. The [qualification
record](https://github.com/paperclipai/paperclip/blob/8f1d6ec02f5bf6c90e7b27da8156953cabd9a180/doc/plans/2026-10-22-hot-upgrade-qualification.md)
is the acceptance ledger and retains source-attributed failures and
hosted limits.

On the eighth integration, all 146 cases across 12 focused qualification
files pass, including report-lock preparation, workflow security and
workflow targeting. E2E typecheck passes. The earlier 118-case result
remains attributed to its prior source. The first sandbox invocation has
117 passes and one failure because process inspection was denied during
inert fixture cleanup. The permitted repeat passes all 118 and a
targeted process check finds no remaining fixture daemon. The earlier
96-case receipt remains source-attributed in the ledger. The latest two
review regressions first fail: an intermediate replacement can cite
another runner/session despite a valid final step, and malformed receipt
JSON aborts evidence packaging. Each replacement now binds
run/company/issue/runner/session, and corrupt receipts retain explicit
invalid metadata plus sanitized diagnostics while other logs and
receipts still package. These checks prove qualification machinery with
injected supervisor operations, configuration subprocesses and real
filesystem packaging. They do not qualify a hosted harness.

Three request-reader cases, 14 artifact/configuration/cache/history
cases, and three lost-partial-receipt cases fail before their respective
corrections. The final collector preserves sanitized startup, readiness
and recovery phase receipts through packaging and temporary-root
deletion. Earlier embedded-Postgres initialization, report-catalog
drift, CI scheduling/cleanup failures and fixture collisions remain
recorded with their corrections or limits. The previous Greptile service
error is retained as historical review evidence, not a current review
pass.

The record includes prior source `9bb9d3417e`'s actual Linux
contention/fairness successes and the ordinary-warm regression proof
from the server slice. The historical 300-session component campaign
passes four after phases and retains earlier burst failures. Those
component costs do not establish hosted capacity. The hosted API-only
pair has zero agents/viewers, fails both absolute latency gates and the
candidate memory gate, and does not complete the active-agent/viewer
matrix.

The strict ten-agent same-artifact restart remains attributed to its
original artifact. It does not qualify this updated stack. Actual
A→B/B→A, the normal drain-free Cloud rollout, and other hosted harnesses
remain open. No new provider turn, deployment or hosted qualification
was run during merge preparation.

Prior source `57d3767eac` jobs 122590643944, 122590643983 and
122590644161 ended after runner shutdown/cancellation without final
Vitest results. The shutdown cause is unproven. No retry was issued
before this source was superseded by the required server recovery fix.
These attempts remain failures/incomplete evidence. Fresh exact-head CI,
legacy status, completed Greptile 5/5, resolved feedback and normal
merge eligibility remain required.

## Risks

Different-artifact A→B/B→A, the normal drain-free Cloud rollout path,
other hosted harnesses and the complete concurrency/viewer matrix remain
open. Frozen branch builds are development-build evidence. This PR owns
qualification and evidence; production defects must be fixed in their
earlier owning slice.

## Model Used

OpenAI GPT-6 through Codex, with reasoning, repository tools and code
execution. The runtime does not expose a more specific model ID or
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 (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
- [ ] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-27 21:58:09 -05:00

226 lines
16 KiB
TypeScript

import { createHash } from "node:crypto";
import { canonicalJson } from "../../packages/shared/src/portability-hash.js";
import { isValidNativePrpEnvelope } from "./native-event-envelope.js";
import type { StoryCheck, StoryRun } from "./everyday-observations.js";
import { pollUntil } from "./api.js";
import { validControllerOutageReceipt, type ControllerOutageReceipt } from "./controller-outage.js";
import { validControllerRecoveryReceipt } from "./controller-recovery-observer.js";
type Row = Record<string, unknown>;
const record = (value: unknown): Row => value !== null && typeof value === "object" && !Array.isArray(value) ? value as Row : {};
const id = (value: unknown): value is string => typeof value === "string" && value.length > 0 && value.length <= 256 && !/[\u0000-\u001f\u007f]/u.test(value);
const digest = (value: unknown) => createHash("sha256").update(canonicalJson(value)).digest("hex");
export interface RemoteRestartEvent {
seq: number; sourceSeq: number; sourceEventId: string; eventType: string;
sourceKind: "runner" | "control_plane"; turnId: string | null;
toolExecutionId: string | null; toolCompleted: boolean; toolFailed: boolean;
payloadSha256: string; sha256: string;
}
export interface RemoteRestartBoundary {
identity: { runId: string; companyId: string; agentId: string; issueId: string;
runnerInstanceId: string; nativeSessionId: string; processPid: number | null; processStartedAt: string | null;
taskIdentitySha256?: string };
lease: { id: string; environmentId: string; executionWorkspaceId: string | null; providerLeaseSha256: string };
events: RemoteRestartEvent[];
}
export interface RemoteRestartEvidence {
expectedReplacements?: number;
replacements?: Array<{ before: RemoteRestartBoundary; after: RemoteRestartBoundary; outage: ControllerOutageReceipt }>;
outage?: ControllerOutageReceipt;
before?: RemoteRestartBoundary;
after?: RemoteRestartBoundary;
final?: { run: StoryRun; events: RemoteRestartEvent[]; operations: Row[];
scheduledRetry: unknown; recoveryActions: Row[]; observedAt?: string };
}
export async function waitForRemoteRestartBoundary(input: {
deadlineAt: number;
load: () => Promise<{ run: StoryRun | undefined; boundary?: RemoteRestartBoundary }>;
}): Promise<RemoteRestartBoundary> {
const observed = await pollUntil({
label: "active remote native turn with completed tool receipt",
deadlineAt: input.deadlineAt,
load: input.load,
accept: state => state.run?.status === "running" && state.boundary !== undefined,
reject: state => !state.run || state.run.status !== "running"
? "Remote restart boundary unexercised: original run ended before fault injection" : undefined,
});
return observed.boundary!;
}
export function projectRemoteRestartRun(run: StoryRun): StoryRun {
return { id: run.id, companyId: run.companyId, agentId: run.agentId, nativeIssueId: run.nativeIssueId,
status: run.status, runtimeMode: run.runtimeMode, runnerInstanceId: run.runnerInstanceId, nativeSessionId: run.nativeSessionId,
nativePhase: run.nativePhase === "committed" ? "committed" : null,
resultJson: { finalizationPhase: run.resultJson?.finalizationPhase === "committed" ? "committed" : null,
workspaceFinalizeStatus: run.resultJson?.workspaceFinalizeStatus === "succeeded" ? "succeeded" : null,
nextAttemptAt: run.resultJson?.nextAttemptAt == null ? null : "scheduled",
failureCode: run.resultJson?.failureCode == null ? null : "present" } };
}
/** Keep only receipt identities and hashes; never retain provider text or tool arguments. */
export function projectRemoteRestartEvents(run: StoryRun, rows: readonly Row[]): RemoteRestartEvent[] {
const seen = new Set<string>();
const cursors = new Map<string, number>();
let lastSeq = 0;
return rows.filter(row => record(row.payload).prpEvent !== undefined).map(row => {
const event = record(record(row.payload).prpEvent);
const source = event.sourceInstanceId;
const runnerSource = event.sourceKind === "runner" && source === run.runnerInstanceId;
const controllerSource = event.sourceKind === "control_plane" && source === `${run.runnerInstanceId}:control`
&& ["run.result.accepted", "run.terminal"].includes(String(event.eventType));
if (row.companyId !== run.companyId || row.runId !== run.id || !isValidNativePrpEnvelope(event, row.protocolSchemaVersion)
|| event.runId !== run.id || event.normalizedSessionId !== run.nativeSessionId || event.eventType !== row.eventType
|| (!runnerSource && !controllerSource) || !id(source) || !id(event.sourceEventId)
|| !Number.isSafeInteger(row.seq) || Number(row.seq) <= lastSeq || !Number.isSafeInteger(event.sourceSeq)
|| Number(event.sourceSeq) <= (cursors.get(source) ?? 0) || seen.has(event.sourceEventId)
|| event.sourceEventId !== `${source}:${run.id}:${event.sourceSeq}`) {
throw new Error("Remote restart has foreign, duplicate or invalid native receipts");
}
seen.add(event.sourceEventId); cursors.set(source, Number(event.sourceSeq)); lastSeq = Number(row.seq);
const payload = record(event.payload);
return { seq: lastSeq, sourceSeq: Number(event.sourceSeq), sourceEventId: event.sourceEventId,
eventType: String(event.eventType), sourceKind: event.sourceKind as RemoteRestartEvent["sourceKind"],
turnId: id(event.turnId) ? event.turnId : null,
toolExecutionId: id(payload.executionId) ? payload.executionId : null,
toolCompleted: event.eventType === "tool.execution.completed" && payload.status === "completed",
toolFailed: event.eventType === "tool.execution.completed" && payload.status !== "completed",
payloadSha256: digest(payload),
sha256: digest(event) };
});
}
export function observeRemoteRestartBoundary(run: StoryRun, rows: readonly Row[], leases: readonly Row[], scope: {
companyId: string; agentId: string; issueId: string; environmentId: string;
}, recovered = false): RemoteRestartBoundary {
if (run.companyId !== scope.companyId || run.agentId !== scope.agentId || run.nativeIssueId !== scope.issueId
|| !(run.status === "running" || (recovered && run.status === "succeeded"))
|| run.runtimeMode !== "native" || !id(run.id) || !id(run.runnerInstanceId) || !id(run.nativeSessionId)) {
throw new Error("Remote restart boundary requires the original live native process");
}
const owned = leases.filter(lease => lease.companyId === scope.companyId && lease.environmentId === scope.environmentId
&& lease.issueId === scope.issueId && lease.heartbeatRunId === run.id
&& (lease.provider === "daytona" || lease.provider === "plugin" || String(lease.provider).startsWith("plugin:"))
&& ((lease.status === "active" && lease.releasedAt == null) || (recovered && run.status === "succeeded"))
&& id(lease.id) && id(lease.providerLeaseId));
if (owned.length !== 1) throw new Error("Remote restart boundary requires one exact active sandbox lease");
const lease = owned[0]!;
const metadata = record(lease.metadata), task = record(metadata.runnerTaskIdentity);
const plugin = lease.provider === "plugin" || String(lease.provider).startsWith("plugin:");
if (plugin) {
// The normal task authority clears dispatch only after provider status confirms
// execution stopped. Completion during the outage must retain that proof.
const settled = recovered && run.status === "succeeded"
&& metadata.runnerTaskDispatched === false && metadata.runnerTaskExecutionStopped === true;
if (run.processPid != null || run.processStartedAt != null || (!settled && metadata.runnerTaskDispatched !== true)
|| task.runnerId !== run.runnerInstanceId || task.runId !== run.id || task.sessionId !== run.nativeSessionId
|| !id(task.leaseId) || !id(task.turnId) || !id(task.itemId)) {
throw new Error("Remote restart requires the admitted plugin task identity, not the controller PID");
}
} else if (!Number.isSafeInteger(run.processPid) || Number(run.processPid) <= 0
|| !id(run.processStartedAt) || !Number.isFinite(Date.parse(run.processStartedAt))) {
throw new Error("Remote restart boundary requires the original live native process");
}
const events = projectRemoteRestartEvents(run, rows);
const starts = events.filter(event => event.eventType === "turn.started" && event.sourceKind === "runner");
const completed = events.filter(event => event.toolCompleted && event.sourceKind === "runner" && event.toolExecutionId);
if (starts.length !== 1 || !id(starts[0]!.turnId) || completed.length === 0
|| completed.some(event => event.turnId !== starts[0]!.turnId)
|| (!recovered && events.some(event => ["turn.completed", "turn.failed", "turn.cancelled", "turn.interrupted", "run.terminal"].includes(event.eventType)))) {
throw new Error("Remote restart boundary has no completed tool in an active original provider turn");
}
return {
identity: { runId: run.id, companyId: scope.companyId, agentId: scope.agentId, issueId: scope.issueId,
runnerInstanceId: run.runnerInstanceId, nativeSessionId: run.nativeSessionId,
processPid: plugin ? null : Number(run.processPid), processStartedAt: plugin ? null : run.processStartedAt!,
...(plugin ? { taskIdentitySha256: digest({ runnerId: task.runnerId, leaseId: task.leaseId,
runId: task.runId, sessionId: task.sessionId, turnId: task.turnId, itemId: task.itemId }) } : {}) },
lease: { id: String(lease.id), environmentId: scope.environmentId,
executionWorkspaceId: typeof lease.executionWorkspaceId === "string" ? lease.executionWorkspaceId : null,
providerLeaseSha256: digest(lease.providerLeaseId) },
events,
};
}
function retainsPrefix(before: readonly RemoteRestartEvent[], after: readonly RemoteRestartEvent[]): boolean {
return before.length > 0 && after.length >= before.length && before.every((event, index) => digest(event) === digest(after[index]));
}
export function gradeRemoteRestart(evidence: RemoteRestartEvidence | undefined): StoryCheck[] {
const { before, after, final } = evidence ?? {};
const check = (id: string, passed: boolean, detail: string) => ({ id, passed, detail });
const sameIdentity = Boolean(before && after && digest(before.identity) === digest(after.identity)
&& digest(before.lease) === digest(after.lease) && retainsPrefix(before.events, after.events));
const run = final?.run;
const identity = before?.identity;
const finalIdentity = Boolean(identity && run && run.id === identity.runId && run.companyId === identity.companyId
&& run.agentId === identity.agentId && run.nativeIssueId === identity.issueId && run.runtimeMode === "native"
&& run.runnerInstanceId === identity.runnerInstanceId && run.nativeSessionId === identity.nativeSessionId);
const events = final?.events ?? [];
const turnId = before?.events.find(event => event.eventType === "turn.started")?.turnId;
const once = (kind: string, source: RemoteRestartEvent["sourceKind"]) => events.filter(event => event.eventType === kind && event.sourceKind === source).length === 1;
// Reattachment can redeliver an already-observed activity notification. Its
// stable identity and exact payload must agree; a new submission, changed
// result or failed tool is not receipt replay and must never pass this cell.
const starts = events.filter(event => event.eventType === "turn.started" && event.sourceKind === "runner");
const tools = events.filter(event => event.toolCompleted);
const toolResults = new Map<string, string>();
let consistentToolResults = true;
for (const event of tools) {
if (!event.toolExecutionId || (toolResults.has(event.toolExecutionId) && toolResults.get(event.toolExecutionId) !== event.payloadSha256)) consistentToolResults = false;
else toolResults.set(event.toolExecutionId, event.payloadSha256);
}
const nativeCompletion = finalIdentity && Boolean(before && after && retainsPrefix(after.events, events))
&& once("turn.submitted", "runner") && starts.length > 0 && new Set(starts.map(event => event.payloadSha256)).size === 1
&& once("turn.completed", "runner")
&& once("run.result.accepted", "control_plane") && once("run.terminal", "control_plane")
&& events.filter(event => event.eventType.startsWith("turn.")).every(event => event.turnId === turnId || (event.eventType === "turn.submitted" && event.turnId === null))
&& consistentToolResults && !events.some(event => event.toolFailed);
const operations = final?.operations ?? [];
const saved = finalIdentity && run?.status === "succeeded" && run.nativePhase === "committed"
&& run.resultJson?.finalizationPhase === "committed" && run.resultJson.workspaceFinalizeStatus === "succeeded"
&& run.resultJson.nextAttemptAt == null && run.resultJson.failureCode == null
&& operations.some(operation => operation.companyId === identity?.companyId && operation.heartbeatRunId === identity?.runId
&& operation.issueId === identity?.issueId && operation.phase === "workspace_finalize" && operation.status === "succeeded"
&& operation.executionWorkspaceId === before?.lease.executionWorkspaceId)
&& !operations.some(operation => ["pending", "queued", "running"].includes(String(operation.status)))
&& final?.scheduledRetry == null && !final?.recoveryActions.some(action => ["active", "pending"].includes(String(action.status)));
const expectedReplacements = evidence?.expectedReplacements ?? 1;
const replacements = evidence?.replacements;
const allReplacements = replacements === undefined ? expectedReplacements === 1
: replacements.length === expectedReplacements && replacements.every((step, index, all) => {
const previous = all[index - 1];
const preservesIdentity = digest(step.before.identity) === digest(step.after.identity)
&& digest(step.before.lease) === digest(step.after.lease)
&& retainsPrefix(step.before.events, step.after.events);
const authenticated = validControllerOutageReceipt(step.outage) && validControllerRecoveryReceipt(step.outage)
&& step.outage.recovery?.binding.runId === step.before.identity.runId
&& step.outage.recovery.binding.companyId === step.before.identity.companyId
&& step.outage.recovery.binding.issueId === step.before.identity.issueId
&& step.outage.recovery.runnerInstanceId === step.before.identity.runnerInstanceId
&& step.outage.recovery.nativeSessionId === step.before.identity.nativeSessionId;
const artifactsPresent = expectedReplacements <= 1 || Boolean(step.outage.previousArtifact && step.outage.replacementArtifact);
const chained = !previous || (retainsPrefix(previous.after.events, step.before.events)
&& (!step.outage.previousArtifact || Boolean(previous.outage.replacementArtifact
&& digest(previous.outage.replacementArtifact) === digest(step.outage.previousArtifact))));
return preservesIdentity && authenticated && artifactsPresent && chained;
});
return [
check("remote-restart-same-execution-and-lease", sameIdentity, "The original run, native session, admitted runner identity and sandbox lease survive replacement."),
check("remote-restart-native-completion-once", nativeCompletion, "The original provider turn completes once and retains its native receipt prefix."),
check("remote-restart-files-finalized", Boolean(saved), "The original run commits its workspace receipt and leaves no active operation or recovery."),
check("remote-restart-fault-exercised", validControllerOutageReceipt(evidence?.outage), "The main server exited, its cache was retired, and a distinct replacement became ready after the requested outage."),
check("remote-restart-authenticated-recovery", validControllerRecoveryReceipt(evidence?.outage)
&& evidence?.outage?.recovery?.binding.runId === identity?.runId
&& evidence?.outage?.recovery?.runnerInstanceId === identity?.runnerInstanceId
&& evidence?.outage?.recovery?.nativeSessionId === identity?.nativeSessionId,
"Natural ownership acquisition and new authenticated runner receipts were observed within the original 420-second grace."),
check("remote-restart-backlog-settled", Boolean(saved && nativeCompletion && final?.observedAt
&& Date.parse(final.observedAt) >= Date.parse(evidence?.outage?.startedAt ?? "") + (evidence?.outage?.recovery?.resumedProcessingObservedAfterMs ?? Infinity)),
"Canonical terminal and workspace receipts settle the original backlog after reattachment."),
check("remote-restart-all-replacements", allReplacements,
"Every requested upgrade and rollback exercised the original running execution and retained its receipts."),
];
}