Merge landed MCP recovery and current shutdown fixes into Dot delivery

* commit '7c0c3c4f31a135cf58d07ebaa2e75451bf14c194':
  fix(mcp): preserve Tasks sleep authority and re-enable recovery (#21603)
  fix(native): close idle warm sessions in parallel across agents at shutdown (#21597)
This commit is contained in:
Dotta committed 2026-11-14 05:10:31 -06:00
commit 3db45651e9
16 files changed
+223 -30

No files matched your search

@@ -355,6 +355,31 @@ describe("persistent agent directories", () => {
expect(worker.nextWakeAt()).toBeNull();
} finally { release(); await foreground; removes.mockRestore(); await worker.stop(); }
});
it("keeps a cleanup pending without an error while another server holds the agent directory lock", async () => {
const copy = await run();
await db.update(agentInstructionWorkingCopies).set({ location: `remote:${randomUUID()}` })
.where(eq(agentInstructionWorkingCopies.runId, copy.runId));
await stopped(copy, new Date(Date.now() - 1000));
vi.stubEnv("PAPERCLIP_EPHEMERAL_SERVER", "true");
const held = await db.$client.reserve();
const lockKey = `agent-directory:${companyId}:${agentId}`;
await held`select pg_advisory_lock(hashtextextended(${lockKey}, 0))`;
const errors: unknown[] = [], worker = coordinator(() => true, () => true, error => errors.push(error));
try {
const before = Date.now();
await copies.registerDirectoryCleanupWork(worker, () => true, async () => true).ready;
await expect.poll(() => idleWorkSnapshot().active).toBe(0);
expect(errors).toEqual([]);
const pending = (await copies.get(companyId, copy.runId))!;
expect(pending.receipt?.cleanupPending).not.toBe(false);
expect(pending.nextAttemptAt!.getTime()).toBeGreaterThan(before);
} finally {
await worker.stop();
await held`select pg_advisory_unlock(hashtextextended(${lockKey}, 0))`;
held.release();
vi.unstubAllEnvs();
}
});
it("reactivates from the real worktree setting without polling disabled policy", async () => {
const copy = await run(); await stopped(copy);
const runtimeEnv = { PAPERCLIP_IN_WORKTREE: "true", PAPERCLIP_INSTANCE_ID: "sleep41-directory-activation" };
@@ -85,7 +85,7 @@ describe("database backup owner", () => {
let hold: IdleSleepDrainStatus = { ownerId: "owner", draining: true, startedAt: new Date(now - 1), expiresAt: new Date(now + 60_000), activeRuns: 0, pendingWakes: 0 };
let finish!: () => void; const gate = new Promise<void>(resolve => { finish = resolve; });
vi.mocked(runDatabaseBackup).mockImplementationOnce(async options => { await gate; return validArchive(options); });
const db = { transaction: async (fn: (tx: unknown) => unknown) => fn({ execute: async () => [{ blocked: false }] }) } as unknown as Db;
const db = { transaction: async (fn: (tx: unknown) => unknown) => fn({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }) } as unknown as Db;
const run = owner();
const safety = readIdleSleepSafety(db, () => hold, () => now, "owner", async () => "none", undefined, async () => Boolean(await run("idle")));
await vi.waitFor(() => expect(runDatabaseBackup).toHaveBeenCalledOnce());
@@ -157,7 +157,7 @@ describe("delivery work coordinator", () => {
expiresAt: current?.expiresAt ?? null, activeRuns: 0, pendingWakes: 0 };
};
const db = { transaction: async (fn: (tx: unknown) => Promise<unknown>) =>
fn({ execute: async () => [{ blocked: false }] }) } as unknown as Db;
fn({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }) } as unknown as Db;
let entered!: () => void;
const started = new Promise<void>(resolve => { entered = resolve; });
const checkpoint = async () => {
@@ -1,9 +1,13 @@
import { randomUUID } from "node:crypto";
import { eq, sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { agentApiKeys, agents, budgetPolicies, chatDeliveries, chatEndpoints, companies,
companyTransferRuns, connectionEventDeliveries, createDb, heartbeatRuns, issues,
routineTriggers, routines, toolApplications, toolConnections, type Db } from "@paperclipai/db";
routineTriggers, routines, instanceSettings, toolApplications, toolConnections, toolInvocations, toolMcpTasks, type Db } from "@paperclipai/db";
import { instanceSettingsService } from "../services/instance-settings.js";
import { createToolGatewayService } from "../services/tool-gateway.js";
import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js";
import { registerMcpTaskWork } from "../services/mcp-task-work.js";
import { queueBudgetEvaluation } from "../services/budget-evaluation-work.js";
import { readIdleSleepSafety } from "../services/idle-sleep-safety.js";
import { EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
@@ -138,4 +142,95 @@ if (!support.supported) console.warn(`Skipping idle sleep diagnostics Postgres t
expect(report.diagnostics?.checks).toContainEqual({ reason: "durable.snapshot", state: "unknown" });
expect(JSON.stringify(report)).not.toMatch(/private-bad-interval|numeric|SELECT|runtime_config/);
});
// These inventory fixtures never make a provider call: all retained polls are
// in the future. The normal gateway and deadline owner remain unmodified.
async function retainedMcpTask() {
const f = await seed();
const [application] = await db.insert(toolApplications).values({ companyId: f.companyId,
name: "Retained MCP task fixture", type: "rest_api" }).returning();
const [connection] = await db.insert(toolConnections).values({ companyId: f.companyId,
applicationId: application!.id, name: "Retained task connection", uid: randomUUID(),
transport: "mcp_remote", status: "draft", enabled: false }).returning();
const [invocation] = await db.insert(toolInvocations).values({ companyId: f.companyId,
connectionId: connection!.id, toolName: "fixture.task", status: "succeeded" }).returning();
const [task] = await db.insert(toolMcpTasks).values({ companyId: f.companyId,
invocationId: invocation!.id, connectionId: connection!.id, grantId: randomUUID(),
actorType: "system", catalogToolName: "fixture.task", requestHash: "fixture-request",
endpointHash: "fixture-endpoint", protocolVersion: "2026-07-28",
sealedProviderTaskId: "not-read-by-this-inventory-test", providerTaskIdHash: randomUUID(),
status: "working", expiresAt: new Date(Date.now() + 3_600_000),
nextPollAt: new Date(Date.now() + 600_000), argumentsIdentity: "fixture-arguments" }).returning();
return task!;
}
async function withMcpTaskSettings(body: () => Promise<void>) {
const [saved] = await db.select().from(instanceSettings).where(eq(instanceSettings.singletonKey, "default"));
vi.stubEnv("PAPERCLIP_MANAGED_CONFIG", undefined);
try {
await db.delete(instanceSettings).where(eq(instanceSettings.singletonKey, "default"));
await body();
} finally {
vi.unstubAllEnvs();
await db.delete(instanceSettings).where(eq(instanceSettings.singletonKey, "default"));
if (saved) await db.insert(instanceSettings).values(saved);
}
}
it("uses the normal effective MCP Tasks authority for retained future work and settled history", async () => {
await withMcpTaskSettings(async () => {
const taskState = async () => (await read()).diagnostics?.checks.find(check => check.reason === "tool_mcp_tasks")?.state;
expect(await taskState()).toBe("none"); // default off, no row
const task = await retainedMcpTask();
const settings = instanceSettingsService(db);
await settings.updateExperimental({ enableMcpTasks: false });
expect(await taskState()).toBe("none"); // explicitly disabled retained work
vi.stubEnv("PAPERCLIP_MANAGED_CONFIG", JSON.stringify({ v: 1, mode: "cloud", catalogVersion: "fixture",
features: { enableMcpTasks: true }, plugins: { autoInstall: [] } }));
const gateway = createToolGatewayService(db);
expect(await gateway.nextMcpTaskPollAt()).toBe(task.nextPollAt!.getTime());
expect(await taskState()).toBe("present"); // first OLD-product causal oracle
await db.update(toolMcpTasks).set({ status: "completed", nextPollAt: null }).where(eq(toolMcpTasks.id, task.id));
expect(await gateway.nextMcpTaskPollAt()).toBeNull();
expect(await taskState()).toBe("none");
vi.stubEnv("PAPERCLIP_MANAGED_CONFIG", undefined);
await instanceSettingsService(db).updateExperimental({ enableMcpTasks: true });
vi.stubEnv("PAPERCLIP_MANAGED_CONFIG", JSON.stringify({ v: 1, mode: "cloud", catalogVersion: "fixture",
features: { enableMcpTasks: false }, plugins: { autoInstall: [] } }));
await db.update(toolMcpTasks).set({ status: "working", nextPollAt: task.nextPollAt }).where(eq(toolMcpTasks.id, task.id));
expect(await gateway.nextMcpTaskPollAt()).toBeNull();
expect(await taskState()).toBe("none"); // raw true cannot defeat managed false
});
}, 30_000);
it("normal settings re-enable rearms the settled MCP Tasks owner without another task write", async () => {
await withMcpTaskSettings(async () => {
const task = await retainedMcpTask();
const settings = instanceSettingsService(db);
await settings.updateExperimental({ enableMcpTasks: false });
const gateway = createToolGatewayService(db);
const poll = vi.spyOn(gateway, "pollDueMcpTasks"); // pass-through, no mocked result
const errors: unknown[] = [];
const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true,
onError: error => { errors.push(error); } });
try {
const worker = registerMcpTaskWork(coordinator, gateway, null, () => true);
await worker.ready;
expect(poll).toHaveBeenCalledTimes(1);
expect(worker.reconciliationSnapshot().reconciled).toBe(true);
expect(coordinator.nextWakeAt()).toBeNull();
await settings.updateExperimental({ enableMcpTasks: true });
await expect.poll(() => poll.mock.calls.length, { timeout: 2_000 }).toBeGreaterThan(1);
await expect.poll(() => worker.reconciliationSnapshot().reconciled, { timeout: 2_000 }).toBe(true);
expect(coordinator.nextWakeAt()).toBe(task.nextPollAt!.getTime());
expect(await db.select().from(toolMcpTasks).where(eq(toolMcpTasks.id, task.id))).toMatchObject([
{ status: "working", nextPollAt: task.nextPollAt, leaseOwner: null, leaseExpiresAt: null },
]);
expect(errors).toEqual([]);
} finally {
await coordinator.stop();
poll.mockRestore();
}
});
}, 30_000);
});
@@ -27,7 +27,7 @@ function database(present: string[] = [], change?: (row: Record<string, unknown>
const row = Object.fromEntries(keys.map(key => [key, present.includes(key)]));
return change ? change(row) : [row];
});
const transaction = vi.fn(async (run: (tx: unknown) => Promise<unknown>) => run({ execute }));
const transaction = vi.fn(async (run: (tx: unknown) => Promise<unknown>) => run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute }));
return { db: { transaction } as unknown as Db, transaction, execute, keys: () => keys };
}
@@ -13,7 +13,7 @@ const emptyLocal = async () => "none" as const;
describe("explicit idle resource preparation", () => {
const database = () => {
const transaction = vi.fn(async (run: (tx: unknown) => Promise<unknown>) => run({ execute: async () => [{ blocked: false }] }));
const transaction = vi.fn(async (run: (tx: unknown) => Promise<unknown>) => run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }));
return { db: { transaction } as unknown as Db, transaction };
};
const inspect = (prepareLocalWork: NonNullable<Parameters<typeof readIdleSleepSafety>[9]>["prepareLocalWork"],
@@ -102,7 +102,7 @@ describe("explicit idle resource preparation", () => {
const replaceExpiry = () => { status = { ...status,
expiresAt: kind.endsWith("unbounded") ? null : new Date(now + 120_000) }; };
if (kind.startsWith("transaction")) transaction.mockImplementationOnce(async run => {
const result = await run({ execute: async () => [{ blocked: false }] });
const result = await run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] });
replaceExpiry();
return result;
});
@@ -27,7 +27,7 @@ const emptyLocal = async () => "none" as const;
describe("idle backup checkpoint", () => {
const database = (blocked = false) => {
const transaction = vi.fn(async (run: (tx: unknown) => Promise<unknown>) => run({ execute: async () => [{ blocked }] }));
const transaction = vi.fn(async (run: (tx: unknown) => Promise<unknown>) => run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked }] }));
return { db: { transaction } as unknown as Db, transaction };
};
const backup = (work: () => Promise<boolean> = async () => true) => vi.fn(async () => {
@@ -115,20 +115,20 @@ describe("idle sleep safety failure boundaries", () => {
])("rejects a report when the hold changes during the durable scan: %j", async (change) => {
const getStatus = vi.fn().mockReturnValueOnce(held()).mockReturnValue({ ...held(), ...change });
const execute = vi.fn().mockResolvedValue([{ blocked: true }]);
const transaction = vi.fn(async (fn: (tx: unknown) => Promise<unknown>) => fn({ execute }));
const transaction = vi.fn(async (fn: (tx: unknown) => Promise<unknown>) => fn({ select: () => ({ from: () => ({ where: async () => [] }) }), execute }));
expect(await readIdleSleepSafety({ transaction } as unknown as Db, getStatus, () => now, undefined, emptyLocal)).toEqual(unknown);
expect(transaction).toHaveBeenCalledWith(expect.any(Function), { isolationLevel: "repeatable read", accessMode: "read only" });
});
it("reports persisted work when the admission hold remains unchanged", async () => {
const execute = vi.fn().mockResolvedValue([{ blocked: true }]);
const transaction = vi.fn(async (fn: (tx: unknown) => Promise<unknown>) => fn({ execute }));
const transaction = vi.fn(async (fn: (tx: unknown) => Promise<unknown>) => fn({ select: () => ({ from: () => ({ where: async () => [] }) }), execute }));
expect(await readIdleSleepSafety({ transaction } as unknown as Db, held, () => now, undefined, emptyLocal)).toEqual(present);
});
});
describe("idle sleep admission and local work", () => {
const emptyDb = () => ({ transaction: async (fn: (tx: unknown) => Promise<unknown>) => fn({ execute: async () => [{ blocked: false }] }) }) as unknown as Db;
const emptyDb = () => ({ transaction: async (fn: (tx: unknown) => Promise<unknown>) => fn({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }) }) as unknown as Db;
it("requires the exact current idle owner and a bounded expiry", async () => {
expect(await readIdleSleepSafety(emptyDb(), held, () => now, undefined, emptyLocal)).toEqual(unknown);
expect(await readIdleSleepSafety(emptyDb(), owned, () => now, "old-owner", emptyLocal)).toEqual(unknown);
+1 -1
View File
@@ -85,7 +85,7 @@ describe("setupLiveEventsWebSocketServer", () => {
server.emit("upgrade", createUpgradeRequest(), socket as unknown as Duplex, Buffer.alloc(0));
const hold = startTaskDrain({ purpose: "idle", ttlMs: 60_000 });
const emptyDb = { transaction: async (run: (tx: unknown) => Promise<unknown>) =>
run({ execute: async () => [{ blocked: false }] }) };
run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }) };
const report = () => readIdleSleepSafety(emptyDb as never,
() => ({ ...hold, draining: true, activeRuns: 0, pendingWakes: 0 }), Date.now, hold.ownerId, async () => "none");
try {
@@ -1289,7 +1289,7 @@ describe("startServer feedback export wiring", () => {
return { reconciled: 0, seeded: 0, failed: 0 } as never;
});
const emptyDb = { transaction: async (run: (tx: unknown) => Promise<unknown>) =>
run({ execute: async () => [{ blocked: false }] }) };
run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }) };
const hold = { ownerId: "fixture-owner", draining: true, activeRuns: 0, pendingWakes: 0,
startedAt: new Date(), expiresAt: new Date(Date.now() + 60_000) };
const report = () => readIdleSleepSafety(emptyDb as never, () => hold, Date.now, hold.ownerId, async () => "none");
@@ -229,7 +229,7 @@ describe("resolveCloudTenantActor (shared-pool hardening)", () => {
server.use(actorMiddleware(db, { deploymentMode: "authenticated", resolveSession: async () => null }));
const hold = startTaskDrain({ purpose: "idle", ttlMs: 60_000 });
const emptyDb = { transaction: async (run: (tx: unknown) => Promise<unknown>) =>
run({ execute: async () => [{ blocked: false }] }) } as unknown as Db;
run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] }) } as unknown as Db;
server.get("/api/health", (_req, res) => res.sendStatus(204));
server.get("/api/instance/task-drain", async (_req, res) => {
res.json(await readIdleSleepSafety(emptyDb, () => ({ ...hold, draining: true, activeRuns: 0, pendingWakes: 0 }),
@@ -5,7 +5,7 @@ import { notifyInstructionCommit, type InstructionCommitScope } from "./instruct
import { DELIVERY_QUEUES } from "./delivery-work-notifications.js";
import type { createDeliveryWorkCoordinator } from "./delivery-work-coordinator.js";
import { registerScopedDeadlineWork } from "./scoped-deadline-work.js";
import { agentDirectoryWorkingCopyService, isAgentDirectoryCopy } from "./agent-directory-working-copies.js";
import { AgentDirectoryOperationBusyError, agentDirectoryWorkingCopyService, isAgentDirectoryCopy } from "./agent-directory-working-copies.js";
import { createHash } from "node:crypto";
import { execFile as execFileCallback } from "node:child_process";
import { promisify } from "node:util";
@@ -564,7 +564,8 @@ export function agentInstructionWorkingCopyService(db: Db, options: { environmen
if (claimed) {
try { await directories.release(claimed); }
catch (error) {
await deferCleanup();
const deferred = await deferCleanup();
if (error instanceof AgentDirectoryOperationBusyError) return directoryCleanupEntry(deferred);
throw error;
}
}
@@ -5,6 +5,12 @@ import path from "node:path";
import { createTailscaleNetworkManager } from "./network.js";
import { parseTailscaleLoginStatus } from "./device-login-status.js";
async function publishState(statePath: string, state: unknown) {
const nextPath = `${statePath}.next`;
await fs.writeFile(nextPath, JSON.stringify(state));
await fs.rename(nextPath, statePath);
}
describe("Tailscale interactive device enrollment", () => {
let dir: string;
let manager: ReturnType<typeof createTailscaleNetworkManager>;
@@ -52,7 +58,7 @@ if (args[1] === 'up') {
const state = JSON.parse(await fs.readFile(statePath, "utf8"));
expect(credential).not.toBe(emptyState);
expect(authorized).not.toHaveBeenCalled();
await fs.writeFile(statePath, JSON.stringify({ ...state, BackendState: "Running", AuthURL: "" }));
await publishState(statePath, { ...state, BackendState: "Running", AuthURL: "" });
await vi.waitFor(() => expect(authorized).toHaveBeenCalledOnce());
await manager.close();
const restored = await manager.connect({ identity: input.identity, credential, persist: input.persist });
@@ -65,7 +71,7 @@ if (args[1] === 'up') {
login: { expiresAt: Date.now() + 60_000, onAuthorized: authorized, onFailure: vi.fn() } };
const socket = await manager.connect(input);
const statePath = path.join(path.dirname(socket), "state");
await fs.writeFile(statePath, JSON.stringify({ BackendState: "NeedsMachineAuth" }));
await publishState(statePath, { BackendState: "NeedsMachineAuth" });
expect(await manager.loginStatus(input.identity)).toEqual({ status: "needs_approval" });
await manager.disconnect(input.identity);
expect(await manager.loginStatus(input.identity)).toBeNull();
+5 -2
View File
@@ -1,3 +1,4 @@
import { instanceSettingsService } from "./instance-settings.js";
import { idleMcpWorkQuery } from "./public-mcp/idle-work-query.js";
import type { McpWakeAdmission } from "./public-mcp/wake-admission.js";
import { pendingBudgetEvaluationWorkQuery } from "./budget-evaluation-work.js";
@@ -101,8 +102,7 @@ const WORK_CHECKS = [
// A pending provider task (MCP Tasks) is polled: the provider cannot wake
// Paperclip, so the instance stays awake until the task is settled.
// Only while the MCP Tasks setting is on; off, nothing polls these rows.
workCheck("tool_mcp_tasks", `SELECT 1 FROM tool_mcp_tasks WHERE next_poll_at IS NOT NULL
AND EXISTS (SELECT 1 FROM instance_settings WHERE (experimental->>'enableMcpTasks') = 'true')`),
workCheck("tool_mcp_tasks", `SELECT 1 FROM tool_mcp_tasks WHERE next_poll_at IS NOT NULL`),
// Persistent machine credentials can receive work without a human opening
// the instance. They need an ingress wake contract before we can sleep.
workCheck("agent_api_keys", incomingWakeWorkQuery("agent_api_keys")!),
@@ -403,6 +403,7 @@ export async function readIdleSleepSafety(
let scheduledWake: ScheduledWake | undefined;
const blocked = await db.transaction(async (tx) => {
await tx.execute(sql`SET LOCAL statement_timeout = '5s'`);
const mcpTasksEnabled = (await instanceSettingsService(db).getExperimentalReadOnly(tx)).enableMcpTasks;
if (options.incomingWake?.incomingWake) {
if (!ownerId || ownerId !== before.ownerId) return undefined;
incomingProof = await readIdleIncomingWakeProof(tx, { capabilities: options.incomingWake, ownerId, receiverOwner: db, inspectionNow: now() });
@@ -439,6 +440,8 @@ export async function readIdleSleepSafety(
(!scheduledRoutines || check.reason !== "routines")
&& (!scheduledIssueMonitors || check.reason !== "issues"))
.map(check => {
if (check.reason === "tool_mcp_tasks") return { ...check,
query: sql`SELECT 1 FROM tool_mcp_tasks WHERE next_poll_at IS NOT NULL AND ${mcpTasksEnabled}` };
const replacement = deadlineProof?.blocked === false ? deadlineWorkReplacement(check.reason) : undefined;
const mcp = incomingProof?.mcp;
const mcpRetryQuery = mcp && deadlineProof?.blocked === false && deadlineProof.mcp === mcp.schedule
+1
View File
@@ -654,6 +654,7 @@ export function instanceSettingsService(db: Db, options: InstanceSettingsService
if (patch.enableStatusCards !== undefined) await notifyStatusCardEvaluation(tx);
if (chatBecameEnabled) await notifyDeferredExecutionEligibility(tx);
if(toExperimentalView(current.experimental).enablePublicMcp!==toExperimentalView(nextExperimental).enablePublicMcp)await prepareMcpClientMirrorPolicy(tx as unknown as Db,toExperimentalView(nextExperimental).enablePublicMcp);
if (patch.enableMcpTasks !== undefined) await notifyDeliveryWork(tx, DELIVERY_QUEUES.mcpTasks);
await notifyDeliveryWork(tx, DELIVERY_QUEUES.mcpIngressHandoffs);
await notifyDeliveryWork(tx, DELIVERY_QUEUES.mcpClientMirror);
await notifyDeliveryWork(tx, DELIVERY_QUEUES.mcpRevocationIndex, "*");
@@ -13373,6 +13373,50 @@ describe("native warm session supervision", () => {
expect(onGoalCheckpoint).toHaveBeenCalledOnce();
});
it("closes idle warm sessions of different agents in parallel and one at a time within an agent", async () => {
await closeIdleWarmNativeSessionsForRestart();
const owners = [["agent-close-a", "first"], ["agent-close-a", "second"], ["agent-close-b", "only"]] as const;
const finishers = new Map<string, () => void>();
let released = false;
const closes = new Map<string, ReturnType<typeof vi.fn<() => Promise<void>>>>();
try {
for (const [agentId, label] of owners) {
const name = `parallel-close-${agentId}-${label}`;
const close = vi.fn(() => released ? Promise.resolve() : new Promise<void>(resolve => { finishers.set(name, resolve); }));
closes.set(name, close);
const warmExecution = { ...execution, binding: { ...execution.binding, agentId, issueId: name, runId: name, executionWorkspaceId: name },
session: { ...execution.session, normalizedSessionId: name,
lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 60_000 } } } as NativeExecutionInputV1;
state.execute.mockReset().mockImplementationOnce(async options => {
await options.onSession?.({ close });
return { result: { summary: "Completed" }, terminal: { runTerminalState: "succeeded" },
turnId: name, normalizedSessionId: name, providerSessionId: name, driverKind: "test", driverVersion: "1",
nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null };
});
await executePaperclipNativeSession({ db: leaseDb(warmExecution), execution: warmExecution, runnerInstanceId: name });
}
const first = closes.get("parallel-close-agent-close-a-first")!;
const second = closes.get("parallel-close-agent-close-a-second")!;
const other = closes.get("parallel-close-agent-close-b-only")!;
const closing = closeIdleWarmNativeSessionsForRestart();
await vi.waitFor(() => {
expect(first).toHaveBeenCalledOnce();
expect(other).toHaveBeenCalledOnce();
});
await new Promise(resolve => setTimeout(resolve, 50));
expect(second).not.toHaveBeenCalled();
finishers.get("parallel-close-agent-close-a-first")!();
await vi.waitFor(() => expect(second).toHaveBeenCalledOnce());
finishers.get("parallel-close-agent-close-a-second")!();
finishers.get("parallel-close-agent-close-b-only")!();
expect(await closing).toEqual({ closed: 3, busy: 0, failed: 0, failures: [] });
} finally {
released = true;
for (const finish of finishers.values()) finish();
await closeIdleWarmNativeSessionsForRestart();
}
});
it("blocks idle sleep for a released local warm session until its close checkpoint finishes", async () => {
await closeIdleWarmNativeSessionsForRestart();
const { reconcilePersistedRuntimeServicesOnStartup, readWorkspaceRuntimeIdleWork } = await import("../workspace-runtime.js");
@@ -748,6 +748,7 @@ type WarmNativeSession = {
};
const WARM_CLOSE_FAILURE_DETAIL_MAX = 2_000;
const WARM_NATIVE_SESSION_CLOSE_CONCURRENCY = 4;
export function describeWarmCloseFailure(error: unknown): string {
const describe = (value: unknown): string => {
@@ -1142,11 +1143,8 @@ async function closeIdleWarmNativeSessions(input: {
let busy = 0;
let failed = 0;
const failures: WarmNativeSessionCloseFailure[] = [];
for (const [sessionId, entry] of [...warmNativeSessions]) {
if (input.canRetire && !input.canRetire()) break;
if (input.environmentId !== undefined && entry.environmentId !== input.environmentId) {
continue;
}
let stopped = false;
const closeEntry = async (sessionId: string, entry: WarmNativeSession) => {
if (entry.busy || entry.preparingRunId || executingRunnerdSessionScopes.has(sessionId)
|| (input.canRetire && (entry.instructionPreparationRunId || closingWarmNativeSessions.has(sessionId)))) {
// A busy turn can complete while another idle session is checkpointing.
@@ -1154,10 +1152,10 @@ async function closeIdleWarmNativeSessions(input: {
// owner behind after the shutdown sweep has already passed it.
if (input.closeBusyOnRelease) entry.closeOnReleaseReason = input.reason;
busy += 1;
continue;
return;
}
// The host owns retention after release; a quiet local handle needs no retirement for sleep.
if (input.canRetire && entry.released && !entry.closeOnReleaseReason) continue;
if (input.canRetire && entry.released && !entry.closeOnReleaseReason) return;
const detach = input.detachReleased === true && entry.released === true && !entry.closeOnReleaseReason;
if (entry.idleTimer !== null) clearTimeout(entry.idleTimer);
// Keep a fenced owner until both retirement and stopped collection succeed.
@@ -1180,7 +1178,7 @@ async function closeIdleWarmNativeSessions(input: {
if (detach) entry.closeOnReleaseReason = "released Runner local detach is unconfirmed";
if (!detach && error instanceof AgentDirectoryOperationBusyError) {
busy += 1;
continue;
return;
}
const destroyed = !detach && input.forceStopUnsettled && input.db
? await warmSandboxDestroyed(input.db, entry).catch(proofError => {
@@ -1194,7 +1192,7 @@ async function closeIdleWarmNativeSessions(input: {
"warm native session's sandbox was already destroyed; released its owner");
forgetWarmNativeSession(sessionId, entry);
closed += 1;
continue;
return;
}
failed += 1;
const forced = forcedStopFailure === undefined ? "" : `; forced Runner stop failed: ${describeWarmCloseFailure(forcedStopFailure)}`;
@@ -1208,8 +1206,28 @@ async function closeIdleWarmNativeSessions(input: {
}
releaseIdleSessionToolAuthority(sessionId);
}
if (input.canRetire && !input.canRetire()) break;
};
const agentQueues = new Map<string, [string, WarmNativeSession][]>();
for (const [sessionId, entry] of [...warmNativeSessions]) {
if (input.environmentId !== undefined && entry.environmentId !== input.environmentId) continue;
const agentKey = `${entry.companyId}:${entry.agentId}`;
const queue = agentQueues.get(agentKey);
if (queue) queue.push([sessionId, entry]); else agentQueues.set(agentKey, [[sessionId, entry]]);
}
const pendingAgents = [...agentQueues.values()];
const closeAgentQueues = async () => {
for (let queue = pendingAgents.shift(); queue; queue = pendingAgents.shift()) {
for (const [sessionId, entry] of queue) {
if (stopped || (input.canRetire && !input.canRetire())) {
stopped = true;
return;
}
await closeEntry(sessionId, entry);
}
}
};
const concurrency = input.canRetire ? 1 : WARM_NATIVE_SESSION_CLOSE_CONCURRENCY;
await Promise.all(Array.from({ length: Math.min(concurrency, pendingAgents.length) }, closeAgentQueues));
if (failures.length > 0) logger.warn({ reason: input.reason, failures }, "idle warm native sessions did not close");
return { closed, busy, failed, failures };
}