fix(mcp): preserve Tasks sleep authority and re-enable recovery (#21603)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Connected apps can retain unfinished provider work through MCP
Tasks.
> - Managed defaults can enable Tasks without a stored experimental
flag.
> - The sleep check used the stored flag and could miss an active
provider task.
> - Enabling Tasks also failed to notify an owner that had become idle.
> - This pull request uses the effective setting and wakes the existing
owner after the setting commits.
> - Pending work keeps its recovery path. Completed history can become
quiet.

## Linked Issues or Issue Description

Refs #21263, which added durable provider Tasks. The current roadmap
covers the governed MCP Tool Gateway. This change repairs two existing
paths.

**What happened?**

The gateway could enable MCP Tasks through managed defaults while the
idle check reported no pending work. After an operator disabled and
enabled Tasks, an existing task could stay idle until a separate task
write woke its owner.

**Expected behavior**

The gateway and sleep check must use the same effective setting.
Enabling Tasks must notify the existing owner. It must retain the task's
original deadline and backoff.

**Steps to reproduce**

1. Retain a task with `next_poll_at`. Use managed Tasks defaults without
a stored true flag. Read the normal sleep projection.
2. Disable Tasks, retain the same task, then enable Tasks through the
settings service without writing the task again.

## What Changed

- Read the effective Tasks flag inside the same read-only sleep
transaction. Bind that value to the existing pending-task query.
- Register the existing Tasks queue notification before the settings
transaction writes. The normal commit protocol owns the wake.
- Add two real-PostgreSQL regressions to the existing nine-case suite.
Extend eight mock transactions with the read projection they now need.
Keep their assertions unchanged.
- Publish the Tailscale enrollment fixture state by rename so its CLI
watcher cannot parse a truncated JSON file. Existing enrollment
assertions and time limits remain unchanged.

## Verification

The two regressions fail against the preceding current-master products:
the pending-work projection reports `none`, and re-enable does not rearm
the owner within the original two-second oracle. After the fix, all nine
PostgreSQL cases pass at the recorded source, including completed
history, disabled Tasks, credential debt and fail-closed query errors.

At current `72bc32f`, the normal DB build and migrations, all nine
PostgreSQL cases, 155 affected mock cases, and the complete normal
Server typecheck/planner pass. The mock run includes three selected
safety families and six complete files; it explicitly excludes 196
existing durable-PG cases. All original case deadlines and assertions
remain. The startup file's earlier 133-case pass is limited to its
unchanged mock contract; a new hot-restart implementation is not
relabeled as tested. Root independently verified all current outputs and
process closure. The MCP product and regression files remain
byte-identical at `321c4b2ae2f3`; its additional change is the Tailscale
test fixture repair below. Exact current-head CI passes: 55 successful
checks, four explicit skips and one neutral result; Snyk succeeds. Fresh
Greptile is 5/5 for this exact head. The complete latest edited summary
and all paginated threads were inspected, with no open findings.

An earlier additional whole-eight-file run reached its 180-second
aggregate watchdog while the existing mixed PostgreSQL suite was still
running. It produced no individual failing assertion. Its startup file
passed 133 cases. The original failure remains recorded. The follow-up
selects the three affected mock families and runs the other affected
mock files whole; it does not claim that the interrupted PostgreSQL file
passed. No test assertion or case deadline changed. Full required CI
remains a merge gate.

The first remote run at `72bc32f` passed 51 checks but failed one
Tailscale enrollment case and an interrupted browser shard; two
aggregate gates then failed. The browser log records a runner shutdown
signal; its underlying cause is unknown, and one specific-job recovery
was requested. The Tailscale case failed its status CLI on both original
CI attempts. A local control then held publication until the original
file watcher observed an empty state: the CLI threw `Unexpected end of
JSON input`, and the existing enrollment assertion failed. Publishing
the state atomically passed the same control. All nine original
enrollment cases now pass in 4.393 seconds with the fixture repair. The
recorded remote interleaving remains unknown; the original logs and the
initial local non-reproduction are retained. No retry count, assertion,
timeout or product behavior was weakened. The later exact-head run
passes all required checks. A subsequent server shard was interrupted by
a runner shutdown and later recovered at the same source; the
interruption and unknown underlying cause remain recorded.

Use Node24/pnpm9, one worker, an owned external PostgreSQL test database
and the existing fixture/admin URL options. The two new regressions are
in `server/src/__tests__/idle-sleep-diagnostics-postgres.test.ts`.

These checks do not establish deployed suspension or a live provider's
availability.

## Risks

- The read adds the normal effective-settings projection to the existing
bounded snapshot. Query errors must still block sleep.
- A settings update now notifies the existing task owner. Transaction
rollback must not publish that notification.
- This adds no schema, provider protocol, timer or replacement task
framework. Existing active task polling remains required until its work
settles.

> 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

OpenAI `gpt-6.1-sol` implemented the regressions and advised the fix.
GPT-6-based Codex also reviewed and integrated it. The host does not
expose every supervising model ID or context-window size. The agents
used source editing, Git, code execution and regression tools.

## 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 and contains no internal
Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass (the focused scope above)
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes (the
existing pending-task contract remains accurate)
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-11-14 05:00:50 -06:00
1 parent aaec67b891
commit 7c0c3c4f31
12 files changed
+123 -18

No files matched your search

@@ -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,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, "*");