fix: guard direct operations on workspace repair sources

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip committed 2026-10-10 23:40:20 -07:00
1 parent a1e1f5a814
commit 40fe882ca8
2 files changed
+14 -3

No files matched your search

@@ -4432,6 +4432,7 @@ export function environmentRuntimeService(
},
async resumeRunLease(input: EnvironmentDriverLeaseInput): Promise<PluginEnvironmentLease | EnvironmentLease | null> {
if (await legacyShutdownWorkspaceResourceProtected(db, input.lease)) return null;
const driver = requireDriverKey(getLeaseDriverKey(input.lease, input.environment));
if (!driver.resumeRunLease) {
throw new Error(`Environment driver "${driver.driver}" does not support lease resume.`);
@@ -4440,6 +4441,7 @@ export function environmentRuntimeService(
},
async destroyRunLease(input: EnvironmentDriverLeaseInput): Promise<EnvironmentLease | null> {
if (await legacyShutdownWorkspaceResourceProtected(db, input.lease)) return null;
if (input.lease.metadata?.legacyShutdownWorkspaceSettlement) {
if (!(await allowLegacyShutdownWorkspaceCleanup(db, input.lease))) return null;
const current = await environmentsSvc.getLeaseById(input.lease.id);
+12 -3
View File
@@ -1,6 +1,6 @@
import { randomUUID } from "node:crypto";
import type { Db } from "@paperclipai/db";
import { agents, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, environmentLeases, heartbeatRuns, issueRecoveryActions, issues, nativeRunFinalizations } from "@paperclipai/db";
import { agents, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, environments, environmentLeases, heartbeatRuns, issueRecoveryActions, issues, nativeRunFinalizations } from "@paperclipai/db";
import { eq } from "drizzle-orm";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
@@ -329,9 +329,14 @@ describePostgres("heartbeat recovery module ownership", () => {
const f = await seedShutdownSource();
await db.update(environmentLeases).set({ leasePolicy }).where(eq(environmentLeases.id, f.lease.id));
const control = createAdapterExecutionControl(); control.controller.abort(); adapterExecutionControls.set(f.run.id, control);
const [environmentRow] = await db.insert(environments).values({ name: "Guarded source", driver: "sandbox" }).returning();
const environment = (await environmentService(db).getById(environmentRow.id))!;
const leaseSnapshot = (await environmentService(db).getLeaseById(f.lease.id))!;
leaseSnapshot.metadata = f.lease.metadata;
const resume = vi.fn(async () => null); const destroy = vi.fn(async () => null);
const stop = vi.fn(async () => ({ providerLeaseId: f.lease.providerLeaseId, state: "stopped" }));
const runtime = environmentRuntimeService(db, { drivers: [{ driver: "sandbox", resolveCapabilities: async () => classifyEnvironmentCapabilities({}),
acquireRunLease: async () => { throw new Error("Unexpected acquire"); }, releaseRunLease: vi.fn(), retryPendingSandboxTeardown: stop }] });
acquireRunLease: async () => { throw new Error("Unexpected acquire"); }, releaseRunLease: vi.fn(), retryPendingSandboxTeardown: stop, resumeRunLease: resume, destroyRunLease: destroy }] });
try {
await runtime.releaseRunLeases(f.run.id, "released", undefined, "stop_and_retain", true);
adapterExecutionControls.delete(f.run.id); // SIGKILL before export/finally settled.
@@ -347,8 +352,12 @@ describePostgres("heartbeat recovery module ownership", () => {
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)))[0].resultJson)
.toMatchObject({ workspaceRestoreFailure: "restore_failed", workspaceRestoreRecovery: { leaseIds: [f.lease.id] } });
expect(await legacyShutdownWorkspaceResourceProtected(db, source)).toBe(true);
// Direct operations and pre-fence snapshots cannot bypass the source pin.
expect(await runtime.resumeRunLease({ environment, lease: leaseSnapshot })).toBeNull();
expect(await runtime.destroyRunLease({ environment, lease: leaseSnapshot })).toBeNull();
expect(resume).not.toHaveBeenCalled(); expect(destroy).not.toHaveBeenCalled();
expect(stop).toHaveBeenCalledOnce();
} finally { control.finish(); await deleteShutdownSource(f); }
} finally { control.finish(); await deleteShutdownSource(f); await db.delete(environments).where(eq(environments.id, environment.id)); }
});
it("retries lease release when ordinary completion wins the shutdown status race", async () => {