mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-28 04:41:52 +01:00
## Thinking Path > - Paperclip delegates remote Runner execution to environment plugins. > - A plugin can move a session to another task allocation. > - The Core controller can lose an ownership reply or restart before the Runner starts. > - The source and target need a durable record of the same request. > - This change binds restart and attachment to that request and the original unlaunched task lease. ## Linked Issues or Issue Description **What existing behavior does this improve?** Recovery of plugin Runner sessions on replacement compute. **Current behavior** Core cannot replay a session ownership request from saved lease state during startup. **Proposed behavior** Record the operation on both leases and check the matched ownership receipt before attaching. Preserve the original pre-admission lease across controller restart. Managed environment configuration is in [#21065](https://github.com/paperclipai/paperclip/pull/21065). Related warm sandbox cleanup is in [#20065](https://github.com/paperclipai/paperclip/pull/20065). This layer preserves the same-sandbox fallback from [#21001](https://github.com/paperclipai/paperclip/pull/21001). Workspace placement follows in [#20743](https://github.com/paperclipai/paperclip/pull/20743). ## What Changed - Add the optional `sessionHandoffVersion` capability and the ownership task operation. - Save matching ownership claims and fence source checkpoint access. Replay a pending request on the original target. - Restore a portable session using the durable claim and the current credentials for the same account. Start a fresh conversation when the provider reports an unavailable checkpoint, or when Core's persisted compatibility decision selects a different session after transfer. The latter requires an unlaunched target; subsequent recovery must match the fresh task's saved identity. Both paths retain the workspace transfer and source fencing. - Reuse the original unlaunched task lease after hard or graceful controller restart. After a move has attached, validate the surviving Runner through the saved claim rather than requiring its logical lease ID to equal the new physical lease. - Keep older retained leases without a saved account identity on the existing resume path. - Apply the existing supported MCP-only compatibility rule to warm recovery and predecessor accounting. Persist the capability discovered at admission and reuse it after restart without repeating discovery. - Keep predecessor-retirement fallback scoped to the same physical sandbox. A refusal after a portable move preserves the session instead of requesting a replacement that the plugin driver cannot enforce. ## Verification - On `1eb527be27`, server typecheck (including Runner build and SDK dependencies) passed. - Independent final verification passed 168 tests across portable source recovery, session resume, controller recovery, and task authority; two existing tests were skipped. The full portable source integration suite passed 64 tests. - The new integration follows confirmed ownership through an incompatible instruction change, persisted fresh admission, target-only prepare/auth/submit without the source checkpoint, and reattachment after controller replacement. It also rejects unpersisted session selection, ambiguous target leases, and incorrect saved identities. - Before this final change, independent verification passed 1,667 tests across executor, session resume, recovery usage, remote warm recovery, runtime selection, and runtime execution; the environment lifecycle suite passed 208 tests. - Existing regressions cover previous-release account metadata, supported and unsupported MCP changes, provider/instruction incompatibility, and admission capability reuse without another discovery call. - On `51d7eec373`, all four S3 multipart throttle tests passed independently. CI exposed an existing timing-sensitive test twice; its 20 ms overlap assumption now uses synchronized retries and an event-loop yield. Production upload code is unchanged. Temporary concurrency caps of one and eight both fail the retained exact-four assertion. - Exact SDK source-pin checks passed for both stack heads. The upper #20743 code patch remains unchanged; its documentation preserves both layers' behavior. ## Risks - The handoff protocol is opt-in. Existing plugin leases without portable ownership metadata keep the previous release's resume path. Live attachment still requires end-to-end qualification. An unverified claim blocks startup. Git workspaces prepared on a portable allocation also need the workspace placement layer. ## Model Used - OpenAI Codex GPT-6 with GPT-6.1 Sol agents for implementation and independent review. ## Checklist - [x] Thinking path and model are included. - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] The issue is described using the enhancement template. - [x] Local checks passed and new behavior has tests. - [x] All Paperclip CI gates are green. - [x] Greptile is 5/5 with no open findings. --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
241 lines
15 KiB
TypeScript
241 lines
15 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import path from "node:path";
|
|
import { and, eq, sql } from "drizzle-orm";
|
|
import { heartbeatRuns, type Db } from "@paperclipai/db";
|
|
import { adapterExecutionTargetEnablesSandboxDuplexBridge } from "@paperclipai/adapter-utils/execution-target";
|
|
import { parseObject } from "../../adapters/utils.js";
|
|
import { logger } from "../../middleware/logger.js";
|
|
import { AgentDirectoryReuseInvalidatedError, isAgentDirectoryCopy } from "../agent-directory-working-copies.js";
|
|
import { collectStoppedInstructionCopyWithRetries, instructionWorkingCopyGuidance, type agentInstructionWorkingCopyService } from "../agent-instruction-working-copies.js";
|
|
import { claimWarmNativeInstructionCopy, nativeSessionWorkspaceScope, reserveWarmNativeInstructionDirectory } from "../native-runtime/index.js";
|
|
import { parseNativeExecutionInput } from "../../vendor/paperclip-runner/index.js";
|
|
import { supportsNativeWarmFiles } from "../native-runtime/native-warm-capability.js";
|
|
import type { HeartbeatRuntimeSelectionInput } from "./runtime-selection.js";
|
|
import type { createHeartbeatLifecycle } from "./run-lifecycle.js";
|
|
import type { EffectiveRunSessionConfigMetadata } from "./workspaces.js";
|
|
import type { NativeLifecyclePolicy } from "./native-lifecycle.js";
|
|
|
|
type Copies = ReturnType<typeof agentInstructionWorkingCopyService>;
|
|
type Selection = HeartbeatRuntimeSelectionInput;
|
|
type AppendRunEvent = ReturnType<typeof createHeartbeatLifecycle>["appendRunEvent"];
|
|
|
|
function readNonEmptyString(value: unknown): string | null {
|
|
return typeof value === "string" && value.trim().length > 0 ? value : null;
|
|
}
|
|
|
|
/** A failed cleanup is deferred once per run; it cannot replace the run result. */
|
|
export function createHeartbeatInstructionCopyRelease(
|
|
instructionCleanupRun: Selection["run"],
|
|
instructionCopies: Pick<Copies, "release">,
|
|
appendRunEvent: AppendRunEvent,
|
|
) {
|
|
let instructionCleanupDeferred = false;
|
|
const releaseInstructionCopy = async () => {
|
|
// Cleanup is retried from the durable working-copy receipt by the
|
|
// recovery sweep. It must not replace the provider result (or prevent
|
|
// lease release), and a timeout must not be repeated in outer teardown.
|
|
if (instructionCleanupDeferred) return;
|
|
try {
|
|
await instructionCopies.release(instructionCleanupRun.companyId, instructionCleanupRun.id);
|
|
} catch (err) {
|
|
instructionCleanupDeferred = true;
|
|
logger.warn({ err, runId: instructionCleanupRun.id }, "Agent file cleanup deferred; run outcome preserved");
|
|
await appendRunEvent(instructionCleanupRun, {
|
|
eventType: "instruction_cleanup",
|
|
stream: "system",
|
|
level: "warn",
|
|
message: "Agent file cleanup was deferred. The run outcome and file-save receipt are unchanged.",
|
|
payload: { state: "deferred" },
|
|
}).catch((eventError) => {
|
|
logger.warn({ err: eventError, runId: instructionCleanupRun.id }, "Failed to record deferred agent file cleanup");
|
|
});
|
|
}
|
|
};
|
|
return releaseInstructionCopy;
|
|
}
|
|
|
|
interface InstructionWorkingCopyInput {
|
|
observabilityCompatibilityKey?: string;
|
|
db: Db;
|
|
instructionCopies: Copies;
|
|
// The executor replaces its run row before preparation. Callbacks must see it.
|
|
readRun: () => Selection["run"];
|
|
agent: Selection["agent"];
|
|
context: Selection["task"]["context"];
|
|
session: {
|
|
taskSession: { lastRunId: string | null } | null;
|
|
taskSessionForRun: { lastRunId: string | null } | null;
|
|
sessionConfigMetadata: EffectiveRunSessionConfigMetadata;
|
|
};
|
|
workspace: Pick<Selection["workspace"], "executionTarget" | "executionWorkspace" | "persistedExecutionWorkspace">;
|
|
appendRunEvent: AppendRunEvent;
|
|
}
|
|
|
|
/** Per-run copy, receipt, and warm ownership; the executor orders final releases. */
|
|
export function createHeartbeatInstructionWorkingCopy(input: InstructionWorkingCopyInput) {
|
|
const { db, instructionCopies, readRun, agent, context, appendRunEvent } = input;
|
|
const { taskSession, taskSessionForRun, sessionConfigMetadata } = input.session;
|
|
const { executionTarget, executionWorkspace, persistedExecutionWorkspace } = input.workspace;
|
|
let releaseWarmInstructionPreparation: (() => Promise<void>) | null = null;
|
|
let nativeInstructionReservation: Awaited<ReturnType<typeof reserveWarmNativeInstructionDirectory>> = null;
|
|
let instructionCopy: Awaited<ReturnType<typeof instructionCopies.prepare>> = null;
|
|
let instructionSave: Record<string, unknown> | null = null;
|
|
const task = executionTarget?.kind === "remote" && executionTarget.transport === "sandbox" ? executionTarget.runnerTask : undefined;
|
|
const instructionPreparationKey = createHash("sha256").update(JSON.stringify({
|
|
adapterType: agent.adapterType, adapterConfig: agent.adapterConfig, runtimeConfig: agent.runtimeConfig, sessionConfigMetadata,
|
|
duplexBridge: adapterExecutionTargetEnablesSandboxDuplexBridge(executionTarget),
|
|
workspace: nativeSessionWorkspaceScope({
|
|
binding: { runId: readRun().id, executionWorkspaceId: persistedExecutionWorkspace?.id ?? readRun().id },
|
|
workspace: executionWorkspace,
|
|
}), cwd: executionWorkspace.cwd,
|
|
})).digest("hex");
|
|
const recordInstructionSave = async (saved: NonNullable<Awaited<ReturnType<typeof instructionCopies.get>>>) => {
|
|
const receipt = parseObject(saved.receipt);
|
|
const storageWarning = readNonEmptyString(receipt.storageWarning);
|
|
const state = saved.errorCode === "AGENT_FILES_CHECKPOINT_UNSTABLE" ? "pending_collection"
|
|
: saved.state === "warm_saved" ? readNonEmptyString(receipt.checkpointState) ?? "saved" : saved.state;
|
|
instructionSave = { state, entryFile: saved.entryFile,
|
|
...(isAgentDirectoryCopy(saved) ? { contract: "agent_files", appliedCandidateHash: saved.candidateHash, checkpointStats: receipt.checkpointStats }
|
|
: { revisionId: parseObject(receipt.revision).id ?? null }), storageWarning, errorCode: saved.errorCode, errorMessage: saved.errorMessage };
|
|
await appendRunEvent(readRun(), { eventType: "instruction_save", stream: "system",
|
|
level: !saved.errorCode && !storageWarning && ["saved", "unchanged", "resolved"].includes(state) ? "info" : "warn",
|
|
message: storageWarning ?? (state === "saved" ? "Agent files saved."
|
|
: state === "unchanged" ? "Instruction working copy is unchanged."
|
|
: saved.errorMessage ?? "Instruction edits were not saved."), payload: instructionSave });
|
|
};
|
|
const collectStoppedInstructions = async () => {
|
|
if (!instructionCopy) return;
|
|
const saved = await collectStoppedInstructionCopyWithRetries(() => instructionCopies.collectStopped({
|
|
companyId: agent.companyId, runId: readRun().id, target: executionTarget,
|
|
}));
|
|
if (!saved) return;
|
|
if (saved.state !== "superseded") await recordInstructionSave(saved);
|
|
};
|
|
const nativeInstructionWorkingCopy = () => instructionCopy ? {
|
|
...(isAgentDirectoryCopy(instructionCopy) ? { runId: readRun().id, preparationKey: instructionPreparationKey } : {}),
|
|
root: instructionCopy.executionRoot,
|
|
...(instructionCopy.receipt?.warm === true ? { checkpointWarm: async () => {
|
|
const saved = await instructionCopies.checkpointWarm({ companyId: agent.companyId, runId: readRun().id, target: executionTarget });
|
|
if (saved) await recordInstructionSave(saved);
|
|
return saved?.state === "warm_saved" && saved.errorCode === null;
|
|
} } : {}),
|
|
hasChanges: () => instructionCopies.hasChanges({ companyId: agent.companyId, runId: readRun().id, target: executionTarget }),
|
|
collectStopped: collectStoppedInstructions,
|
|
retirementFailed: async () => { await instructionCopies.reportRetirementUnconfirmed(agent.companyId, readRun().id); },
|
|
} : undefined;
|
|
|
|
async function prepare({ runtimeConfig, nativeRuntimeResolution, hasInstructionFilesystem, nativeLifecyclePolicy }: {
|
|
runtimeConfig: Record<string, unknown>;
|
|
nativeRuntimeResolution: Selection["nativeRuntimeResolution"];
|
|
hasInstructionFilesystem: boolean;
|
|
nativeLifecyclePolicy: NativeLifecyclePolicy | null;
|
|
}) {
|
|
if (hasInstructionFilesystem && !task) {
|
|
try {
|
|
// Missing contract fields on a restored session mean the deployed
|
|
// legacy format. New sessions opt into whole-directory persistence.
|
|
const priorFileRun = taskSession?.lastRunId
|
|
? await db.select({ profile: heartbeatRuns.runnerProfileJson }).from(heartbeatRuns).where(and(
|
|
eq(heartbeatRuns.id, taskSession.lastRunId), eq(heartbeatRuns.companyId, agent.companyId), eq(heartbeatRuns.agentId, agent.id))).then(rows => rows[0])
|
|
: null;
|
|
const savedFileInput = parseObject(parseObject(readRun().runnerProfileJson).nativeExecutionInput);
|
|
const priorFileInput = Object.keys(savedFileInput).length ? savedFileInput : parseObject(parseObject(priorFileRun?.profile).nativeExecutionInput);
|
|
const priorWorkingCopy = parseObject(parseObject(parseObject(priorFileInput.runtimeContext).instructions).workingCopy);
|
|
const warmFiles = nativeRuntimeResolution.kind === "native" && (supportsNativeWarmFiles({ ...nativeRuntimeResolution.profile, acpxAgent: parseObject(agent.adapterConfig).acpxAgent }) ||
|
|
(nativeRuntimeResolution.profile.backend === "acpx_runtime" && parseObject(agent.adapterConfig).acpxAgent === "cursor")) &&
|
|
nativeLifecyclePolicy?.mode === "warm";
|
|
if (warmFiles && taskSessionForRun?.lastRunId) {
|
|
nativeInstructionReservation = await reserveWarmNativeInstructionDirectory({ companyId: agent.companyId, agentId: agent.id,
|
|
previousRunId: taskSessionForRun.lastRunId, runId: readRun().id, target: executionTarget,
|
|
observabilityCompatibilityKey: input.observabilityCompatibilityKey,
|
|
canReuse: () => instructionCopies.canReuseWarm(agent.companyId, agent.id, taskSessionForRun!.lastRunId!),
|
|
});
|
|
}
|
|
if (!warmFiles && nativeRuntimeResolution.kind === "native" && priorWorkingCopy.kind === "agent_files" && taskSession) {
|
|
releaseWarmInstructionPreparation = await claimWarmNativeInstructionCopy({
|
|
observabilityCompatibilityKey: input.observabilityCompatibilityKey,
|
|
priorExecution: parseNativeExecutionInput(priorFileInput), companyId: agent.companyId, agentId: agent.id,
|
|
executionWorkspaceId: persistedExecutionWorkspace?.id ?? readRun().id, workspace: executionWorkspace,
|
|
environmentId: executionTarget?.environmentId ?? null, target: executionTarget, runId: readRun().id, preparationKey: instructionPreparationKey, forceRetirement: !taskSessionForRun,
|
|
adopt: async previousRunId => {
|
|
instructionCopy = await instructionCopies.adopt({ companyId: agent.companyId, agentId: agent.id,
|
|
runId: readRun().id, previousRunId, target: executionTarget, cwd: executionWorkspace.cwd, allowRetirementHandoff: true });
|
|
return nativeInstructionWorkingCopy() ?? null;
|
|
},
|
|
});
|
|
// A collection-only handoff was retired. Re-enter normal
|
|
// preparation instead of composing a root already collected.
|
|
if (!releaseWarmInstructionPreparation) instructionCopy = null;
|
|
}
|
|
const prepareInstructions = (reuseRunId?: string) => instructionCopies.prepare({
|
|
companyId: agent.companyId, agentId: agent.id, runId: readRun().id,
|
|
target: executionTarget, cwd: executionWorkspace.cwd,
|
|
legacy: Object.keys(priorFileInput).length > 0 && priorWorkingCopy.kind !== "agent_files",
|
|
warm: warmFiles, reuseRunId,
|
|
onWarmHandoff: copy => {
|
|
instructionCopy = copy;
|
|
nativeInstructionReservation?.adopt(copy.executionRoot, collectStoppedInstructions);
|
|
},
|
|
});
|
|
try {
|
|
instructionCopy ??= await prepareInstructions(nativeInstructionReservation?.reuseRunId);
|
|
} catch (error) {
|
|
if (!(error instanceof AgentDirectoryReuseInvalidatedError)) throw error;
|
|
// prepare has released its canonical lock. Retirement can now
|
|
// collect under that same lock before a fresh restore starts.
|
|
await nativeInstructionReservation?.release();
|
|
nativeInstructionReservation = null;
|
|
instructionCopy = await prepareInstructions();
|
|
}
|
|
} catch (error) {
|
|
if ((error as { status?: number }).status !== 403) throw error;
|
|
// Missing write identity must not break a background run's read-only
|
|
// prompt. It must also never imply that ordinary file edits will save.
|
|
await appendRunEvent(readRun(), { eventType: "instruction_save", stream: "system", level: "warn",
|
|
message: "Persistent instruction editing is unavailable. Use an authenticated user with instruction edit access and a managed instruction bundle.",
|
|
payload: { state: "unavailable", code: "INSTRUCTION_COPY_UNAVAILABLE" } });
|
|
const guidance = "No editable agent instruction working copy is registered for this turn. Use authenticated agent file tools for persistent edits; do not edit a private copy named in an earlier turn or claim its changes will persist.";
|
|
for (const key of ["paperclipTaskMarkdown", "paperclipTaskMarkdownCompact"]) {
|
|
context[key] = [readNonEmptyString(context[key]), guidance].filter(Boolean).join("\n\n");
|
|
}
|
|
}
|
|
if (instructionCopy) {
|
|
const storageWarning = readNonEmptyString(instructionCopy.receipt?.storageWarning);
|
|
if (storageWarning) {
|
|
instructionSave = { state: "prepared", contract: "agent_files", storageWarning };
|
|
// This is an advisory on the run, never an agent pause, execution
|
|
// failure, or scheduling gate. Keep it visible while work runs.
|
|
await db.update(heartbeatRuns).set({ resultJson: sql`coalesce(${heartbeatRuns.resultJson}, '{}'::jsonb) || ${JSON.stringify({ instructionSave })}::jsonb` }).where(eq(heartbeatRuns.id, readRun().id));
|
|
await appendRunEvent(readRun(), { eventType: "instruction_save", stream: "system", level: "warn",
|
|
message: storageWarning, payload: instructionSave });
|
|
}
|
|
runtimeConfig = { ...runtimeConfig, instructionsFilePath: path.join(instructionCopy.localRoot, instructionCopy.entryFile) };
|
|
if (isAgentDirectoryCopy(instructionCopy)) {
|
|
const workspace = parseObject(context.paperclipWorkspace);
|
|
context.paperclipWorkspace = { ...workspace, agentHome: instructionCopy.executionRoot,
|
|
// Keep the pre-existing permission root stable for ACP session
|
|
// identity. The per-run copy is already under the company root.
|
|
agentHomeForPermissions: workspace.agentHome,
|
|
};
|
|
}
|
|
const guidance = instructionWorkingCopyGuidance(instructionCopy);
|
|
for (const key of ["paperclipTaskMarkdown", "paperclipTaskMarkdownCompact"]) {
|
|
context[key] = [readNonEmptyString(context[key]), guidance].filter(Boolean).join("\n\n");
|
|
}
|
|
}
|
|
}
|
|
return runtimeConfig;
|
|
}
|
|
|
|
return {
|
|
prepare,
|
|
collectStoppedInstructions,
|
|
nativeInstructionWorkingCopy,
|
|
get instructionCopy() { return instructionCopy; },
|
|
getInstructionSave: () => instructionSave,
|
|
// These getters remain usable after partial acquisition failures.
|
|
get nativeInstructionReservation() { return nativeInstructionReservation; },
|
|
get releaseWarmInstructionPreparation() { return releaseWarmInstructionPreparation; },
|
|
};
|
|
}
|