diff --git a/server/src/__tests__/agent-directory-working-copies.test.ts b/server/src/__tests__/agent-directory-working-copies.test.ts index 3216e40173b..31cbd5b5823 100644 --- a/server/src/__tests__/agent-directory-working-copies.test.ts +++ b/server/src/__tests__/agent-directory-working-copies.test.ts @@ -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" }; diff --git a/server/src/__tests__/database-backup-owner.test.ts b/server/src/__tests__/database-backup-owner.test.ts index 8dfea74f3be..8a2ff27f59b 100644 --- a/server/src/__tests__/database-backup-owner.test.ts +++ b/server/src/__tests__/database-backup-owner.test.ts @@ -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(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()); diff --git a/server/src/__tests__/delivery-work-coordinator.test.ts b/server/src/__tests__/delivery-work-coordinator.test.ts index b375e8a4a9b..6fb1ca3471f 100644 --- a/server/src/__tests__/delivery-work-coordinator.test.ts +++ b/server/src/__tests__/delivery-work-coordinator.test.ts @@ -157,7 +157,7 @@ describe("delivery work coordinator", () => { expiresAt: current?.expiresAt ?? null, activeRuns: 0, pendingWakes: 0 }; }; const db = { transaction: async (fn: (tx: unknown) => Promise) => - 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(resolve => { entered = resolve; }); const checkpoint = async () => { diff --git a/server/src/__tests__/idle-sleep-diagnostics-postgres.test.ts b/server/src/__tests__/idle-sleep-diagnostics-postgres.test.ts index 0730a4798c1..3671c3ad0e8 100644 --- a/server/src/__tests__/idle-sleep-diagnostics-postgres.test.ts +++ b/server/src/__tests__/idle-sleep-diagnostics-postgres.test.ts @@ -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) { + 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); }); diff --git a/server/src/__tests__/idle-sleep-diagnostics.test.ts b/server/src/__tests__/idle-sleep-diagnostics.test.ts index 995e95eaa75..888a57e8077 100644 --- a/server/src/__tests__/idle-sleep-diagnostics.test.ts +++ b/server/src/__tests__/idle-sleep-diagnostics.test.ts @@ -27,7 +27,7 @@ function database(present: string[] = [], change?: (row: Record 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) => run({ execute })); + const transaction = vi.fn(async (run: (tx: unknown) => Promise) => run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute })); return { db: { transaction } as unknown as Db, transaction, execute, keys: () => keys }; } diff --git a/server/src/__tests__/idle-sleep-preparation.test.ts b/server/src/__tests__/idle-sleep-preparation.test.ts index 29332846097..a01ef4c9db5 100644 --- a/server/src/__tests__/idle-sleep-preparation.test.ts +++ b/server/src/__tests__/idle-sleep-preparation.test.ts @@ -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) => run({ execute: async () => [{ blocked: false }] })); + const transaction = vi.fn(async (run: (tx: unknown) => Promise) => run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked: false }] })); return { db: { transaction } as unknown as Db, transaction }; }; const inspect = (prepareLocalWork: NonNullable[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; }); diff --git a/server/src/__tests__/idle-sleep-safety.test.ts b/server/src/__tests__/idle-sleep-safety.test.ts index b8fccc1d9c8..51ef2ce29f3 100644 --- a/server/src/__tests__/idle-sleep-safety.test.ts +++ b/server/src/__tests__/idle-sleep-safety.test.ts @@ -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) => run({ execute: async () => [{ blocked }] })); + const transaction = vi.fn(async (run: (tx: unknown) => Promise) => run({ select: () => ({ from: () => ({ where: async () => [] }) }), execute: async () => [{ blocked }] })); return { db: { transaction } as unknown as Db, transaction }; }; const backup = (work: () => Promise = 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) => fn({ execute })); + const transaction = vi.fn(async (fn: (tx: unknown) => Promise) => 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) => fn({ execute })); + const transaction = vi.fn(async (fn: (tx: unknown) => Promise) => 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) => fn({ execute: async () => [{ blocked: false }] }) }) as unknown as Db; + const emptyDb = () => ({ transaction: async (fn: (tx: unknown) => Promise) => 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); diff --git a/server/src/__tests__/live-events-ws.test.ts b/server/src/__tests__/live-events-ws.test.ts index 91e6d9cac9e..bdcb462d50e 100644 --- a/server/src/__tests__/live-events-ws.test.ts +++ b/server/src/__tests__/live-events-ws.test.ts @@ -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) => - 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 { diff --git a/server/src/__tests__/server-startup-feedback-export.test.ts b/server/src/__tests__/server-startup-feedback-export.test.ts index d0816940929..0c174f74817 100644 --- a/server/src/__tests__/server-startup-feedback-export.test.ts +++ b/server/src/__tests__/server-startup-feedback-export.test.ts @@ -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) => - 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"); diff --git a/server/src/middleware/cloud-tenant-actor.test.ts b/server/src/middleware/cloud-tenant-actor.test.ts index eff8f83b671..61929549d0d 100644 --- a/server/src/middleware/cloud-tenant-actor.test.ts +++ b/server/src/middleware/cloud-tenant-actor.test.ts @@ -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) => - 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 }), diff --git a/server/src/services/agent-instruction-working-copies.ts b/server/src/services/agent-instruction-working-copies.ts index f588c06284f..b2ab372e9b2 100644 --- a/server/src/services/agent-instruction-working-copies.ts +++ b/server/src/services/agent-instruction-working-copies.ts @@ -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; } } diff --git a/server/src/services/connectors/tailscale/device-login-network.test.ts b/server/src/services/connectors/tailscale/device-login-network.test.ts index 59bc0739ee3..8b2a4f3a4b2 100644 --- a/server/src/services/connectors/tailscale/device-login-network.test.ts +++ b/server/src/services/connectors/tailscale/device-login-network.test.ts @@ -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; @@ -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(); diff --git a/server/src/services/idle-sleep-safety.ts b/server/src/services/idle-sleep-safety.ts index dc53a7394db..dd3cc5367d7 100644 --- a/server/src/services/idle-sleep-safety.ts +++ b/server/src/services/idle-sleep-safety.ts @@ -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 diff --git a/server/src/services/instance-settings.ts b/server/src/services/instance-settings.ts index abede64c234..d440ce2ae25 100644 --- a/server/src/services/instance-settings.ts +++ b/server/src/services/instance-settings.ts @@ -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, "*"); diff --git a/server/src/services/native-runtime/native-session-executor.test.ts b/server/src/services/native-runtime/native-session-executor.test.ts index fa73ec1d27e..393ffe532a0 100644 --- a/server/src/services/native-runtime/native-session-executor.test.ts +++ b/server/src/services/native-runtime/native-session-executor.test.ts @@ -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 void>(); + let released = false; + const closes = new Map Promise>>>(); + try { + for (const [agentId, label] of owners) { + const name = `parallel-close-${agentId}-${label}`; + const close = vi.fn(() => released ? Promise.resolve() : new Promise(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"); diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index 6644293bf64..31b9148339f 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -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(); + 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 }; }