mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-24 22:49:00 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A deployer can replace the server while agent runs continue. Before the swap, it reads the upgrade-readiness report of the old server > - The version 1 report is ready only when every active run is adoptable and no wake admission is in flight > - On a busy instance, new runs arrive each minute. Some runs are always in preparation, so the report is almost never ready and the instance does not upgrade > - A run in preparation has done no agent work. Restart recovery starts it again. A swap loses nothing > - This pull request adds a `handoff` object to the report. It separates recoverable, restartable and blocking runs > - The benefit is that a deployer can wait only for work that a swap would lose ## Linked Issues or Issue Description Refs #20852 (reference only; this change does not depend on it and does not use its version 2 shape). **What happened?** A busy instance had 16 running runs and 1 queued run. Fourteen had a live process. Two or three native runs had not launched a process yet. One was a legacy run. `upgradeReadiness.ready` needs `blockingRuns === 0 && pendingWakes === 0` in one probe. That state almost never occurs, so the deployer never swaps the server. **Expected behavior** The report tells the deployer which runs a swap would lose. Runs that only wait for a launch, and wake admissions in flight, do not block a graceful replacement. **Steps to reproduce** 1. Keep a steady flow of new runs on an instance. 2. Read `GET /api/instance/task-drain?upgradeReadiness=1` repeatedly. 3. See `ready: false` each time, because one or more runs are in preparation or a wake admission is in flight. ## What Changed - `summarizeUpgradeReadiness` returns an additive `handoff` object: `version`, `ready`, `recoverableRuns`, `restartableRuns`, `blockingRuns`, `pendingWakes`, `unsavedWakes`. - `handoff.ready` is true when the instance is not draining, the census is complete, and `handoff.blockingRuns` and `handoff.unsavedWakes` are 0. Saved pending wakes do not change it. - The wake queue tracks each public wake admission until its wake request and queued run commit. `unsavedWakes` is the count of admissions before that commit. Without a valid count, every pending wake is unsaved. - The top-level version 1 fields keep their values and meaning. `ready` and `blockingRuns` are unchanged, so a current consumer sees no looser verdict. - The census query marks a run `restartable` only with all of these: `paperclip_runner` adapter; no process identity; no provider session before or after; no checkpoint header; no dispatched runner task; no execution-start event. It must also be a native run in the `observed` phase with preparation fencing and an unsettled finalization record, or a legacy run in the `preparing` stage under a recorded controller. - All other runs without adoption evidence stay blocking. This includes local adapters, legacy runs with a child process, follow-up runs that carry a provider session, and runs the census could not observe. - The OpenAPI description and `doc/testing-native-remote-continuity.md` describe the new fields and their limits. Why these classes are correct on master: - Restartable native runs: restart recovery claims a run with no checkpoint and no provider evidence as `bootstrap_incomplete` and executes the same run again (`native-restart-recovery.ts` `classifyNativeRunnerRecoveryEvidence`; `heartbeat/recovery.ts` executes each claim). A graceful shutdown suspends the run and notifies recovery. Existing tests: "recovers an unclaimed remote startup once without requiring a provider checkpoint" and "recovers an unclaimed remote startup that a graceful shutdown suspended during preparation". - Restartable legacy runs: process-loss recovery retries a `preparing` run with `providerWorkStarted: false`. Existing test: "continues a chain whose preparations were killed twice and then interrupted again, without a Retry". - Pending wakes: the count is wake admissions in flight in the process, not queue rows. A wake commits its wake request and its queued run in one transaction. After the commit, the next server dispatches the queued run at startup, so a saved wake does not block. Before the commit, nothing is saved. Shutdown waits at most 30 seconds and then exits, so an unsaved wake can be lost and blocks. ## Verification - `cd server && npx vitest run src/services/heartbeat/upgrade-readiness.test.ts src/services/heartbeat/upgrade-readiness-post-result.test.ts`: 28 passed. - Red/green: before the implementation, the 7 new tests failed (6 summary tests, 1 PostgreSQL census test). After it, all pass. - Red/green for unsaved wakes: a test holds the agent row lock while a wake admission waits. Without the fix, `handoff.ready` was true with no saved wake. With the fix, it is false with `unsavedWakes: 1`, and the wake saves after the lock releases. - New cases: only recoverable runs; recoverable plus unlaunched runs plus pending wakes (`handoff.ready` true, top-level `ready` false and `blockingRuns` unchanged); a started run without recovery evidence; an unobserved execution; draining; truncated census. - The PostgreSQL test changes one fact at a time (process identity, provider session, phase, fencing, checkpoint, adapter, each execution-start event) and expects the run to become blocking. - `cd server && npx vitest run src/__tests__/openapi-routes.test.ts src/__tests__/instance-settings-routes.test.ts`: 157 passed (187 with the two readiness files). - `cd server && npx tsc --noEmit`: passed. - `pnpm verify` exited 2 (a documentation path is a global CI input). Required CI supplies the broad coverage. - Not run: a live server replacement. The classification relies on the existing recovery tests named above. ## Risks - Low risk for current consumers. The change is additive and the version 1 fields are unchanged. - `handoff.ready` is an observation at one instant. A wake admission that starts after the probe is unsaved until it commits. - Restart recovery applies its own proofs and a bounded attempt budget before it starts a restartable run again. The report is an observation, not a guarantee for each run. - The probe adds one indexed `not exists` on run events per running row, inside the existing two-second statement timeout. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Anthropic Claude Fable 5.2 (`claude-fable-5-2`), with tool use and code execution in Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
432 lines
34 KiB
TypeScript
432 lines
34 KiB
TypeScript
import { mkdtemp, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { randomUUID } from "node:crypto";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import { agents, agentWakeupRequests, applyPendingMigrations, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, heartbeatRunEvents, heartbeatRuns, issues, environmentLeases, nativeRunFinalizations, nativeRunnerCheckpoints } from "@paperclipai/db";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { eq, sql } from "drizzle-orm";
|
|
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
|
|
import { resolveExternalTestDatabaseUrl } from "../../__tests__/helpers/external-test-database.js";
|
|
import { currentNativeControllerIdentity } from "../native-runtime/native-restart-recovery.js";
|
|
import { DurablePrpControlPlane } from "../../vendor/paperclip-runner/index.js";
|
|
import { nativeRunnerCheckpointWriter } from "../native-runtime/native-runner-checkpoint.js";
|
|
import { createLocalEncryptedCipher } from "../../secrets/local-encrypted-provider.js";
|
|
import { readUpgradeReadiness, summarizeUpgradeReadiness } from "./upgrade-readiness.js";
|
|
const idle = { activeRuns: 0, pendingWakes: 0, draining: false };
|
|
const report = (patch: Partial<Parameters<typeof summarizeUpgradeReadiness>[0]> = {}) =>
|
|
summarizeUpgradeReadiness({ rows: [{ id: "remote", recoverable: true }],
|
|
before: { ...idle, activeRuns: 1 }, after: { ...idle, activeRuns: 1 },
|
|
activeRunIds: ["remote"], controllerBootId: "this-boot", ...patch });
|
|
describe("upgrade readiness", () => {
|
|
it("allows a known recoverable execution without stopping admission", () => {
|
|
expect(report()).toMatchObject({ ready: true, activeRuns: 1, recoverableRuns: 1, blockingRuns: 0, draining: false });
|
|
});
|
|
it("retains an execution without positive recovery evidence as a blocker", () => {
|
|
expect(report({ rows: [{ id: "remote", recoverable: false }] })).toMatchObject({ ready: false, blockingRuns: 1 });
|
|
});
|
|
it("blocks a new execution that appears while the query is in flight", () => {
|
|
expect(report({ activeRunIds: ["remote", "new"], after: { ...idle, activeRuns: 2 } })).toMatchObject({ ready: false, activeRuns: 2, blockingRuns: 1 });
|
|
});
|
|
it("blocks an untracked execution promise and pending admission", () => {
|
|
expect(report({ after: { ...idle, activeRuns: 2 } }).ready).toBe(false);
|
|
expect(report({ after: { ...idle, activeRuns: 1, pendingWakes: 1 } }).ready).toBe(false);
|
|
expect(report({ before: { ...idle, activeRuns: 1, draining: true } }).ready).toBe(false);
|
|
});
|
|
it("never treats a truncated census as complete", () => {
|
|
const rows = Array.from({ length: 1_001 }, (_, i) => ({ id: String(i), recoverable: true }));
|
|
expect(report({ rows, activeRunIds: [] })).toMatchObject({ ready: false, truncated: true });
|
|
expect(report({ rows: rows.map(row => ({ ...row, recoverable: false, retainedUnowned: true })),
|
|
before: idle, after: idle, activeRunIds: [] })).toMatchObject({ ready: false, truncated: true, activeRuns: 0 });
|
|
});
|
|
it("keeps current execution and admission barriers with retained recovery work", () => {
|
|
const rows = [{ id: "retained", recoverable: false, retainedUnowned: true }, { id: "remote", recoverable: true }];
|
|
expect(report({ rows })).toMatchObject({ ready: true, activeRuns: 1, recoverableRuns: 1, retainedBlockedRuns: 1 });
|
|
expect(report({ rows, activeRunIds: ["remote", "retained"], after: { ...idle, activeRuns: 2 } }))
|
|
.toMatchObject({ ready: false, activeRuns: 2, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
for (const before of [{ ...idle, activeRuns: 2 }, { ...idle, pendingWakes: 1 }, { ...idle, draining: true }]) {
|
|
expect(report({ rows, before }).ready).toBe(false);
|
|
}
|
|
});
|
|
describe("handoff verdict", () => {
|
|
const busy = (rows: Parameters<typeof summarizeUpgradeReadiness>[0]["rows"], pendingWakes = 0, unsavedWakes = 0) => report({ rows,
|
|
activeRunIds: rows.map(row => row.id), before: { ...idle, activeRuns: rows.length, pendingWakes, unsavedWakes },
|
|
after: { ...idle, activeRuns: rows.length, pendingWakes, unsavedWakes } });
|
|
it("agrees with the version 1 verdict when every execution is recoverable", () => {
|
|
expect(busy([{ id: "a", recoverable: true }, { id: "b", recoverable: true }])).toMatchObject({ version: 1, ready: true,
|
|
blockingRuns: 0, handoff: { version: 1, ready: true, recoverableRuns: 2, restartableRuns: 0, blockingRuns: 0, pendingWakes: 0, unsavedWakes: 0 } });
|
|
});
|
|
it("is ready with unlaunched runs and saved pending wakes, and leaves the version 1 fields unchanged", () => {
|
|
const rows = [{ id: "live", recoverable: true }, { id: "preparing", recoverable: false, restartable: true },
|
|
{ id: "launching", recoverable: false, restartable: true }];
|
|
expect(busy(rows, 2)).toMatchObject({ version: 1, ready: false, activeRuns: 3, recoverableRuns: 1, blockingRuns: 2,
|
|
pendingWakes: 2, handoff: { version: 1, ready: true, recoverableRuns: 1, restartableRuns: 2, blockingRuns: 0, pendingWakes: 2, unsavedWakes: 0 } });
|
|
});
|
|
it("is not ready while a pending wake has not saved its wake and run", () => {
|
|
const rows = [{ id: "live", recoverable: true }];
|
|
expect(busy(rows, 2, 1).handoff).toMatchObject({ ready: false, blockingRuns: 0, pendingWakes: 2, unsavedWakes: 1 });
|
|
// Seen in either read of the counters.
|
|
expect(report({ rows, activeRunIds: ["live"], before: { ...idle, activeRuns: 1, pendingWakes: 1, unsavedWakes: 1 },
|
|
after: { ...idle, activeRuns: 1 } }).handoff).toMatchObject({ ready: false, unsavedWakes: 1 });
|
|
});
|
|
it("treats every pending wake as unsaved without a valid saved count", () => {
|
|
const rows = [{ id: "live", recoverable: true }];
|
|
for (const unsavedWakes of [undefined, -1, 3, 0.5, Number.NaN]) {
|
|
expect(report({ rows, activeRunIds: ["live"], before: { ...idle, activeRuns: 1, pendingWakes: 2, unsavedWakes },
|
|
after: { ...idle, activeRuns: 1 } }).handoff, String(unsavedWakes)).toMatchObject({ ready: false, pendingWakes: 2, unsavedWakes: 2 });
|
|
}
|
|
});
|
|
it("is not ready while a started execution has no recovery evidence", () => {
|
|
const rows = [{ id: "live", recoverable: true }, { id: "preparing", recoverable: false, restartable: true },
|
|
{ id: "legacy-child", recoverable: false }];
|
|
expect(busy(rows)).toMatchObject({ ready: false, blockingRuns: 2,
|
|
handoff: { ready: false, recoverableRuns: 1, restartableRuns: 1, blockingRuns: 1 } });
|
|
});
|
|
it("counts an execution this report could not observe as a blocker", () => {
|
|
const rows = [{ id: "preparing", recoverable: false, restartable: true }];
|
|
expect(report({ rows, activeRunIds: ["preparing", "unseen"], after: { ...idle, activeRuns: 2 } }).handoff)
|
|
.toMatchObject({ ready: false, restartableRuns: 1, blockingRuns: 1 });
|
|
expect(report({ rows, activeRunIds: ["preparing"], after: { ...idle, activeRuns: 2 } }).handoff)
|
|
.toMatchObject({ ready: false, restartableRuns: 1, blockingRuns: 1 });
|
|
});
|
|
it("never counts one execution in two classes", () => {
|
|
expect(busy([{ id: "a", recoverable: true, restartable: true }]).handoff)
|
|
.toMatchObject({ ready: true, recoverableRuns: 1, restartableRuns: 0, blockingRuns: 0 });
|
|
});
|
|
it("stays not ready while draining or truncated", () => {
|
|
const rows = [{ id: "preparing", recoverable: false, restartable: true }];
|
|
expect(report({ rows, activeRunIds: ["preparing"], before: { ...idle, activeRuns: 1, draining: true } }).handoff)
|
|
.toMatchObject({ ready: false, blockingRuns: 0 });
|
|
const many = Array.from({ length: 1_001 }, (_, i) => ({ id: String(i), recoverable: false, restartable: true }));
|
|
expect(report({ rows: many, activeRunIds: many.map(row => row.id), before: { ...idle, activeRuns: many.length },
|
|
after: { ...idle, activeRuns: many.length } })).toMatchObject({ truncated: true, handoff: { ready: false } });
|
|
});
|
|
});
|
|
});
|
|
|
|
const externalUrl = resolveExternalTestDatabaseUrl("PAPERCLIP_UPGRADE_READINESS_TEST_DATABASE_URL");
|
|
const support = externalUrl ? { supported: true } : await getEmbeddedPostgresTestSupport();
|
|
const postgresTests = support.supported ? describe : describe.skip;
|
|
if (!support.supported) console.warn(`Upgrade readiness SQL tests unavailable: ${support.reason}`);
|
|
postgresTests("upgrade readiness persisted execution evidence", () => {
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>, db: Db;
|
|
beforeAll(async () => {
|
|
if (externalUrl) {
|
|
await applyPendingMigrations(externalUrl);
|
|
database = { connectionString: externalUrl, cleanup: async () => {} };
|
|
} else database = await startEmbeddedPostgresTestDatabase("paperclip-upgrade-readiness-");
|
|
vi.stubEnv("PAPERCLIP_SECRETS_MASTER_KEY", "a".repeat(64));
|
|
db = createDb(database.connectionString);
|
|
}, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS);
|
|
afterAll(async () => { await db?.$client.end(); await database?.cleanup(); vi.unstubAllEnvs(); });
|
|
it("keeps an old unowned preparation visible without making it a live upgrade blocker", async () => {
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), runId = randomUUID();
|
|
const owner = await currentNativeControllerIdentity();
|
|
await db.insert(companies).values({ id: companyId, name: "Retained preparation", issuePrefix: `R${companyId.slice(0, 6)}` });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Retained preparation", role: "engineer", adapterType: "paperclip_runner" });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Interrupted preparation", status: "in_progress", assigneeAgentId: agentId });
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, issueId, nativeIssueId: issueId,
|
|
status: "running", runtimeMode: "native", nativePhase: "observed", runnerInstanceId: randomUUID(), nativeSessionId: randomUUID() });
|
|
const retained = { controllerBootId: "previous-controller", leaseOwner: "previous-controller-lease", controllerGeneration: 1,
|
|
leaseExpiresAt: new Date(0), recoveryState: "awaiting_evidence", phase: "observed", failureCode: null };
|
|
await db.insert(nativeRunFinalizations).values({ runId, companyId, issueId, ...retained });
|
|
const input = { activeRunIds: new Set<string>(), counters: () => idle };
|
|
try {
|
|
const savedRun = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId));
|
|
const savedOwner = await db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, runId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: true, activeRuns: 0,
|
|
recoverableRuns: 0, blockingRuns: 0, retainedBlockedRuns: 1,
|
|
nativeControllerRecovery: { activeExecutionAdoption: { retainedBlockedRuns: 1 } } });
|
|
const [{ default: express }, { default: request }, { instanceSettingsRoutes }, { errorHandler }] = await Promise.all([
|
|
import("express"), import("supertest"), import("../../routes/instance-settings.js"), import("../../middleware/error-handler.js"),
|
|
]);
|
|
const app = express();
|
|
let isInstanceAdmin = false;
|
|
app.use((req, _res, next) => {
|
|
req.actor = { type: "board", userId: "readiness-test", source: "session", isInstanceAdmin };
|
|
next();
|
|
});
|
|
app.use("/api", instanceSettingsRoutes(db));
|
|
app.use(errorHandler);
|
|
await request(app).get("/api/instance/task-drain?upgradeReadiness=1").expect(403);
|
|
isInstanceAdmin = true;
|
|
const response = await request(app).get("/api/instance/task-drain?upgradeReadiness=1").expect(200);
|
|
expect(response.body.upgradeReadiness).toMatchObject({ ready: true, activeRuns: 0, blockingRuns: 0, retainedBlockedRuns: 1 });
|
|
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId))).toEqual(savedRun);
|
|
expect(await db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, runId))).toEqual(savedOwner);
|
|
expect(await db.select({ status: heartbeatRuns.status }).from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))
|
|
.toEqual([{ status: "running" }]);
|
|
expect(await db.select({ state: nativeRunFinalizations.recoveryState }).from(nativeRunFinalizations)
|
|
.where(eq(nativeRunFinalizations.runId, runId))).toEqual([{ state: "awaiting_evidence" }]);
|
|
for (const patch of [
|
|
{ controllerBootId: owner.bootId }, { controllerBootId: null }, { controllerBootId: "" },
|
|
{ leaseExpiresAt: new Date(Date.now() + 60_000) }, { leaseExpiresAt: null },
|
|
{ leaseOwner: null }, { controllerGeneration: 0 }, { failureCode: "uncertain" },
|
|
{ recoveryState: null }, { phase: "workspace_finalizing" },
|
|
]) {
|
|
await db.update(nativeRunFinalizations).set(patch).where(eq(nativeRunFinalizations.runId, runId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: false, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
await db.update(nativeRunFinalizations).set(retained).where(eq(nativeRunFinalizations.runId, runId));
|
|
}
|
|
for (const patch of [{ runtimeMode: "legacy" }, { nativePhase: "running" }, { processPid: process.pid }, { processStartedAt: new Date() }]) {
|
|
await db.update(heartbeatRuns).set(patch).where(eq(heartbeatRuns.id, runId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: false, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
await db.update(heartbeatRuns).set({ runtimeMode: "native", nativePhase: "observed", processPid: null, processStartedAt: null })
|
|
.where(eq(heartbeatRuns.id, runId));
|
|
}
|
|
await db.insert(nativeRunnerCheckpoints).values({ runId, companyId, issueId, part: "header", material: {} });
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: false, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
await db.delete(nativeRunnerCheckpoints).where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
await db.update(agents).set({ adapterType: "process" }).where(eq(agents.id, agentId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: false, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
await db.update(agents).set({ adapterType: "paperclip_runner" }).where(eq(agents.id, agentId));
|
|
input.activeRunIds.add(runId);
|
|
expect(await readUpgradeReadiness(db, { ...input, counters: () => ({ ...idle, activeRuns: 1 }) }))
|
|
.toMatchObject({ ready: false, activeRuns: 1, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
input.activeRunIds.clear();
|
|
let reads = 0;
|
|
expect(await readUpgradeReadiness(db, { ...input, counters: () => {
|
|
if (reads++ > 0) input.activeRunIds.add(runId);
|
|
return { ...idle, activeRuns: input.activeRunIds.size };
|
|
} })).toMatchObject({ ready: false, blockingRuns: 1, retainedBlockedRuns: 0 });
|
|
} finally {
|
|
await db.update(heartbeatRuns).set({ status: "failed" }).where(eq(heartbeatRuns.id, runId));
|
|
}
|
|
});
|
|
it("requires a bound checkpoint and current owner rather than a native label or live process alone", async () => {
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), runId = randomUUID();
|
|
const owner = await currentNativeControllerIdentity();
|
|
await db.insert(companies).values({ id: companyId, name: "Upgrade", issuePrefix: `U${companyId.slice(0, 6)}` });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Upgrade", role: "engineer", adapterType: "paperclip_runner" });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Synthetic upgrade", status: "in_progress", assigneeAgentId: agentId });
|
|
const workspace = { schema: "paperclip.native-workspace-sync/v1", state: "prepared",
|
|
descriptorSha256: "a".repeat(64), baselineSha256: "b".repeat(64), workspaceId: randomUUID(),
|
|
leaseId: randomUUID(), providerLeaseId: "synthetic-sandbox", remoteCwd: "/workspace" };
|
|
const execution = { runnerProfileJson: { nativeWorkspaceSync: workspace }, runtimeMode: "native", runnerInstanceId: randomUUID(), nativeSessionId: randomUUID(), processPid: process.pid, processStartedAt: new Date() };
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, issueId, nativeIssueId: issueId, status: "running", ...execution });
|
|
const lease = { controllerBootId: owner.bootId, leaseOwner: "current-lease", controllerGeneration: 1, leaseExpiresAt: new Date(Date.now() + 60_000) };
|
|
await db.insert(nativeRunFinalizations).values({ runId, companyId, issueId, phase: "observed", ...lease });
|
|
const input = { activeRunIds: new Set([runId]), counters: () => ({ ...idle, activeRuns: 1 }) };
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
const binding = { runId, companyId, issueId };
|
|
await db.insert(nativeRunnerCheckpoints).values({ ...binding, part: "header", material: {} });
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
const root = await mkdtemp(join(tmpdir(), "upgrade-checkpoint-"));
|
|
const core = new DurablePrpControlPlane({ stateDirectory: root,
|
|
identity: { runId, runnerInstanceId: execution.runnerInstanceId, normalizedSessionId: execution.nativeSessionId,
|
|
environmentLeaseId: workspace.workspaceId, turnId: "turn", itemId: "item" },
|
|
expectedRunnerVersion: "0.3.0", expectedRunnerDigest: `sha256:${"a".repeat(64)}` });
|
|
let header = "";
|
|
const writer = nativeRunnerCheckpointWriter(db, { ...binding, ...lease, bootId: owner.bootId,
|
|
authorityRunId: runId, generation: lease.controllerGeneration });
|
|
core.setCheckpointPersistence(async checkpoint => {
|
|
header = checkpoint.records.get("header")!;
|
|
await writer(checkpoint);
|
|
});
|
|
try { core.issueBootstrapTicket(); await core.flushCheckpoint(); }
|
|
finally { await core.stop(); await rm(root, { recursive: true, force: true }); }
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: true, recoverableRuns: 1 });
|
|
const cipher = createLocalEncryptedCipher("write");
|
|
const parsedHeader = JSON.parse(header);
|
|
const representativeHeader = JSON.stringify({ ...parsedHeader, state: { ...parsedHeader.state,
|
|
runAttachTemplate: { provider: { instructions: "x".repeat(300_000) } } } });
|
|
await db.update(nativeRunnerCheckpoints).set({ material: cipher.encrypt(JSON.stringify({ ...binding, part: "header", contents: representativeHeader })) })
|
|
.where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(true);
|
|
for (const contents of ["{", "{}", JSON.stringify({ ...JSON.parse(header), schema: "paperclip.runner.controller-checkpoint.v1" }),
|
|
JSON.stringify({ ...JSON.parse(header), padding: "x".repeat(1_100_000) }),
|
|
JSON.stringify({ ...JSON.parse(header), state: { ...JSON.parse(header).state, identity: { ...JSON.parse(header).state.identity, runId: randomUUID() } } })]) {
|
|
await db.update(nativeRunnerCheckpoints).set({ material: cipher.encrypt(JSON.stringify({ ...binding, part: "header", contents })) })
|
|
.where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
}
|
|
for (const field of ["runnerInstanceId", "normalizedSessionId", "environmentLeaseId"]) {
|
|
const mismatch = JSON.stringify({ ...JSON.parse(header), state: { ...JSON.parse(header).state,
|
|
identity: { ...JSON.parse(header).state.identity, [field]: randomUUID() } } });
|
|
await db.update(nativeRunnerCheckpoints).set({ material: cipher.encrypt(JSON.stringify({ ...binding, part: "header", contents: mismatch })) })
|
|
.where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
}
|
|
await db.update(nativeRunnerCheckpoints).set({ material: cipher.encrypt(JSON.stringify({ ...binding, part: "header", contents: header })) })
|
|
.where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(true);
|
|
for (const patch of [{ runnerProfileJson: {} }, { runnerProfileJson: { nativeWorkspaceSync: {} } }, { runtimeMode: "legacy" }, { runnerInstanceId: null }, { nativeSessionId: null }, { processPid: null }, { processStartedAt: null }]) {
|
|
await db.update(heartbeatRuns).set(patch).where(eq(heartbeatRuns.id, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
await db.update(heartbeatRuns).set(execution).where(eq(heartbeatRuns.id, runId));
|
|
}
|
|
for (const patch of [{ leaseOwner: null }, { controllerGeneration: 0 }, { controllerBootId: "foreign" }, { leaseExpiresAt: new Date(0) }, { failureCode: "uncertain" }]) {
|
|
await db.update(nativeRunFinalizations).set(patch).where(eq(nativeRunFinalizations.runId, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
await db.update(nativeRunFinalizations).set({ ...lease, failureCode: null }).where(eq(nativeRunFinalizations.runId, runId));
|
|
}
|
|
// Remote-authoritative runs retain their logical workspace identity without a host sync record.
|
|
const remoteWorkspace = { schema: "paperclip.remote-workspace-run.v1", workspaceId: workspace.workspaceId,
|
|
leaseId: workspace.leaseId, providerLeaseId: workspace.providerLeaseId, remoteCwd: workspace.remoteCwd,
|
|
epoch: 1, state: "prepared", snapshotId: null };
|
|
const remoteProfile = { remoteWorkspace };
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: remoteProfile }).where(eq(heartbeatRuns.id, runId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject({ ready: true, recoverableRuns: 1, blockingRuns: 0 });
|
|
for (const remote of [{ ...remoteWorkspace, state: "saved", snapshotId: randomUUID() },
|
|
remoteWorkspace]) {
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: { remoteWorkspace: remote,
|
|
nativeWorkspaceSync: { ...workspace, workspaceId: randomUUID() } } }).where(eq(heartbeatRuns.id, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(true); // remote identity takes precedence
|
|
}
|
|
for (const remote of [null, {}, { ...remoteWorkspace, schema: "unknown" }, { ...remoteWorkspace, epoch: 0 },
|
|
{ ...remoteWorkspace, workspaceId: randomUUID() }, { ...remoteWorkspace, leaseId: "invalid" },
|
|
{ ...remoteWorkspace, remoteCwd: "" }, { ...remoteWorkspace, snapshotId: "invalid" }]) {
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: { nativeWorkspaceSync: workspace,
|
|
remoteWorkspace: remote } }).where(eq(heartbeatRuns.id, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false); // no fallback to the legacy identity
|
|
}
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: execution.runnerProfileJson }).where(eq(heartbeatRuns.id, runId));
|
|
// Plugin task admission owns an environment lease identity and deliberately has no workspace-sync record.
|
|
const pluginLeaseId = randomUUID();
|
|
await db.insert(environmentLeases).values({ id: pluginLeaseId, companyId, heartbeatRunId: runId, issueId,
|
|
provider: "plugin", providerLeaseId: "synthetic-task", metadata: { agentId, runnerTaskLease: true, runnerTaskDispatched: true } });
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: remoteProfile }).where(eq(heartbeatRuns.id, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false); // contradictory task authorities cannot be adopted
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: {} }).where(eq(heartbeatRuns.id, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false); // workspace identity is not plugin task authority
|
|
const pluginHeader = JSON.stringify({ ...JSON.parse(header), state: { ...JSON.parse(header).state,
|
|
identity: { ...JSON.parse(header).state.identity, environmentLeaseId: pluginLeaseId } } });
|
|
await db.update(nativeRunnerCheckpoints).set({ material: cipher.encrypt(JSON.stringify({ ...binding, part: "header", contents: pluginHeader })) })
|
|
.where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(true);
|
|
for (const patch of [{ status: "released" }, { expiresAt: new Date(0) }, { releasedAt: new Date() },
|
|
{ cleanupStatus: "pending" }, { metadata: { agentId: randomUUID(), runnerTaskLease: true, runnerTaskDispatched: true } }, { metadata: { agentId, runnerTaskLease: true, runnerTaskDispatched: false } },
|
|
{ metadata: { agentId, runnerTaskLease: true, runnerTaskDispatched: true, runnerTaskExecutionStopped: true } }]) {
|
|
await db.update(environmentLeases).set(patch).where(eq(environmentLeases.id, pluginLeaseId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
await db.update(environmentLeases).set({ status: "active", expiresAt: null, releasedAt: null, cleanupStatus: null, metadata: { agentId, runnerTaskLease: true, runnerTaskDispatched: true } }).where(eq(environmentLeases.id, pluginLeaseId));
|
|
}
|
|
await db.delete(environmentLeases).where(eq(environmentLeases.id, pluginLeaseId));
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: execution.runnerProfileJson }).where(eq(heartbeatRuns.id, runId));
|
|
await db.update(nativeRunnerCheckpoints).set({ material: cipher.encrypt(JSON.stringify({ ...binding, part: "header", contents: header })) })
|
|
.where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
await db.update(agents).set({ adapterType: "process" }).where(eq(agents.id, agentId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false);
|
|
await db.update(agents).set({ adapterType: "paperclip_runner" }).where(eq(agents.id, agentId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(true);
|
|
await db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, runId));
|
|
expect((await readUpgradeReadiness(db, input)).ready).toBe(false); // the in-flight local tail is still unknown
|
|
expect(await readUpgradeReadiness(db, { activeRunIds: new Set(), counters: () => idle })).toMatchObject({ ready: true, activeRuns: 0 });
|
|
});
|
|
it("classifies a run with no launched agent work as restartable, and any started work as a blocker", async () => {
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), runId = randomUUID();
|
|
const owner = await currentNativeControllerIdentity();
|
|
await db.insert(companies).values({ id: companyId, name: "Restartable", issuePrefix: `S${companyId.slice(0, 6)}` });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Restartable", role: "engineer", adapterType: "paperclip_runner" });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Preparing", status: "in_progress", assigneeAgentId: agentId });
|
|
const unlaunched = { runtimeMode: "native", nativePhase: "observed", processPid: null, processGroupId: null,
|
|
processStartedAt: null, sessionIdBefore: null, sessionIdAfter: null, executionStage: null, controllerBootId: null,
|
|
runnerProfileJson: { nativePreparationFencingVersion: 1 } };
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, issueId, nativeIssueId: issueId, status: "running",
|
|
runnerInstanceId: randomUUID(), nativeSessionId: randomUUID(), ...unlaunched });
|
|
const claimed = { controllerBootId: owner.bootId, leaseOwner: "controller-lease", controllerGeneration: 1,
|
|
leaseExpiresAt: new Date(Date.now() + 60_000), phase: "observed", failureCode: null };
|
|
await db.insert(nativeRunFinalizations).values({ runId, companyId, issueId, ...claimed });
|
|
const input = { activeRunIds: new Set([runId]), counters: () => ({ ...idle, activeRuns: 1, pendingWakes: 1, unsavedWakes: 0 }) };
|
|
const restartable = { ready: false, activeRuns: 1, recoverableRuns: 0, blockingRuns: 1, pendingWakes: 1,
|
|
handoff: { version: 1, ready: true, recoverableRuns: 0, restartableRuns: 1, blockingRuns: 0, pendingWakes: 1 } };
|
|
const blocking = { ready: false, blockingRuns: 1, handoff: { ready: false, restartableRuns: 0, blockingRuns: 1 } };
|
|
try {
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject(restartable);
|
|
for (const patch of [{ processPid: process.pid }, { processGroupId: process.pid }, { processStartedAt: new Date() },
|
|
{ sessionIdBefore: "provider-session" }, { sessionIdAfter: "provider-session" }, { nativePhase: "result_accepted" },
|
|
{ runnerProfileJson: {} }, { runtimeMode: "legacy" }]) {
|
|
await db.update(heartbeatRuns).set(patch).where(eq(heartbeatRuns.id, runId));
|
|
expect(await readUpgradeReadiness(db, input), JSON.stringify(patch)).toMatchObject(blocking);
|
|
await db.update(heartbeatRuns).set(unlaunched).where(eq(heartbeatRuns.id, runId));
|
|
}
|
|
for (const patch of [{ failureCode: "uncertain" }, { phase: "workspace_finalizing" }]) {
|
|
await db.update(nativeRunFinalizations).set(patch).where(eq(nativeRunFinalizations.runId, runId));
|
|
expect(await readUpgradeReadiness(db, input), JSON.stringify(patch)).toMatchObject(blocking);
|
|
await db.update(nativeRunFinalizations).set(claimed).where(eq(nativeRunFinalizations.runId, runId));
|
|
}
|
|
await db.update(agents).set({ adapterType: "process" }).where(eq(agents.id, agentId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject(blocking);
|
|
await db.update(agents).set({ adapterType: "paperclip_runner" }).where(eq(agents.id, agentId));
|
|
await db.insert(nativeRunnerCheckpoints).values({ runId, companyId, issueId, part: "header", material: {} });
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject(blocking);
|
|
await db.delete(nativeRunnerCheckpoints).where(eq(nativeRunnerCheckpoints.runId, runId));
|
|
// The executor records this request before any backend may start a runner.
|
|
for (const event of [{ eventType: "native.process_start_requested" }, { eventType: "adapter.invoke" },
|
|
{ eventType: "lifecycle", sourceEventId: "runner-event-1" }]) {
|
|
const [{ id }] = await db.insert(heartbeatRunEvents).values({ companyId, runId, agentId, seq: 1, ...event })
|
|
.returning({ id: heartbeatRunEvents.id });
|
|
expect(await readUpgradeReadiness(db, input), event.eventType).toMatchObject(blocking);
|
|
await db.delete(heartbeatRunEvents).where(eq(heartbeatRunEvents.id, id));
|
|
}
|
|
await db.insert(heartbeatRunEvents).values({ companyId, runId, agentId, seq: 1, eventType: "lifecycle" });
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject(restartable);
|
|
// A direct-adapter preparation that has not reached dispatch is requeued by process-loss recovery.
|
|
const legacyPreparing = { runtimeMode: "legacy", nativePhase: null, executionStage: "preparing", controllerBootId: randomUUID() };
|
|
await db.update(heartbeatRuns).set(legacyPreparing).where(eq(heartbeatRuns.id, runId));
|
|
expect(await readUpgradeReadiness(db, input)).toMatchObject(restartable);
|
|
for (const patch of [{ executionStage: "dispatching" }, { controllerBootId: null }, { processPid: process.pid }]) {
|
|
await db.update(heartbeatRuns).set(patch).where(eq(heartbeatRuns.id, runId));
|
|
expect(await readUpgradeReadiness(db, input), JSON.stringify(patch)).toMatchObject(blocking);
|
|
await db.update(heartbeatRuns).set({ ...legacyPreparing, processPid: null }).where(eq(heartbeatRuns.id, runId));
|
|
}
|
|
} finally {
|
|
await db.delete(heartbeatRunEvents).where(eq(heartbeatRunEvents.runId, runId));
|
|
await db.update(heartbeatRuns).set({ status: "failed" }).where(eq(heartbeatRuns.id, runId));
|
|
}
|
|
});
|
|
it("blocks the handoff while a wake admission waits on the agent lock with nothing saved", async () => {
|
|
const companyId = randomUUID(), agentId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "Unsaved wake", issuePrefix: `W${companyId.slice(0, 6)}` });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Unsaved wake", role: "engineer", adapterType: "process",
|
|
status: "idle", adapterConfig: { command: "true" } });
|
|
const { heartbeatService } = await import("../heartbeat.js");
|
|
const heartbeat = heartbeatService(db);
|
|
const saved = () => db.select({ id: agentWakeupRequests.id }).from(agentWakeupRequests).where(eq(agentWakeupRequests.agentId, agentId));
|
|
const lock = await db.$client.reserve();
|
|
let wake: Promise<unknown> | undefined;
|
|
try {
|
|
await lock.unsafe("begin");
|
|
await lock.unsafe(`select id from agents where id = '${agentId}' for update`);
|
|
wake = heartbeat.wakeup(agentId, { source: "on_demand", triggerDetail: "manual",
|
|
requestedByActorType: "user", requestedByActorId: "readiness-user" });
|
|
await vi.waitFor(async () => {
|
|
const locks = await db.execute(sql`select 1 from pg_stat_activity where wait_event_type = 'Lock' and query like '%from agents where id =%for update%'`);
|
|
expect(locks.length).toBeGreaterThan(0);
|
|
}, { timeout: 20_000, interval: 100 });
|
|
expect(await saved()).toEqual([]);
|
|
expect((await heartbeat.readUpgradeReadiness()).handoff).toMatchObject({ ready: false, pendingWakes: 1, unsavedWakes: 1 });
|
|
} finally {
|
|
await lock.unsafe("rollback"); lock.release();
|
|
await wake;
|
|
await heartbeat.drainActiveRunExecutions();
|
|
}
|
|
expect(await saved()).toHaveLength(1);
|
|
expect((await heartbeat.readUpgradeReadiness()).handoff).toMatchObject({ pendingWakes: 0, unsavedWakes: 0 });
|
|
}, 60_000);
|
|
it("cancels a locked probe within its SQL budget and releases its connection", async () => {
|
|
const probeDb = createDb(database.connectionString, { maxConnections: 1 });
|
|
const lock = await db.$client.reserve();
|
|
try {
|
|
await lock.unsafe("begin");
|
|
await lock.unsafe("lock table native_runner_checkpoints in access exclusive mode");
|
|
const started = performance.now();
|
|
const result = readUpgradeReadiness(probeDb, { activeRunIds: new Set(), counters: () => idle })
|
|
.then(() => null, error => error);
|
|
const unrelatedStarted = performance.now();
|
|
expect(await db.execute(sql`select 1 as value`)).toMatchObject([{ value: 1 }]);
|
|
expect(performance.now() - unrelatedStarted).toBeLessThan(1_000);
|
|
const failure = await result;
|
|
expect(failure?.cause?.code ?? failure?.code).toBe("57014");
|
|
expect(performance.now() - started).toBeGreaterThanOrEqual(1_800);
|
|
expect(performance.now() - started).toBeLessThan(5_000);
|
|
// maxConnections=1 means this cannot pass if the cancelled transaction keeps its connection.
|
|
expect(await probeDb.execute(sql`show statement_timeout`)).toMatchObject([{ statement_timeout: "0" }]);
|
|
expect(await probeDb.execute(sql`select 1 as value`)).toMatchObject([{ value: 1 }]);
|
|
} finally {
|
|
await lock.unsafe("rollback"); lock.release(); await probeDb.$client.end();
|
|
}
|
|
});
|
|
|
|
});
|