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/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 4231ddc9f27..2a496c6c8dc 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, "*");