Publish tool profiles and their selectors atomically (#22455)

This commit is contained in:
Nicky Leach authored and GitHub committed 2026-11-20 19:02:54 -08:00
1 parent 7f4cfae5e1
commit 467fd32223
3 files changed
+106 -35

No files matched your search

@@ -2,8 +2,8 @@ import { randomUUID } from "node:crypto";
import express from "express";
import request from "supertest";
import { mcpGatewayProtocolRoutes } from "../routes/tool-gateway.js";
import { eq, sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import { eq, like, sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import {
agents,
activityLog,
@@ -274,6 +274,78 @@ describeEmbeddedPostgres("heartbeat runtime MCP servers", () => {
return seeded;
}
it("waits for the complete native profile when two runs first use the same assignment", async () => {
const [owner] = await seedAssignedAgents(1);
const [next] = await db.insert(heartbeatRuns).values({
companyId: owner!.agent.companyId,
agentId: owner!.agent.id,
status: "running",
}).returning();
const locked = Promise.withResolvers<void>();
const release = Promise.withResolvers<void>();
const blocker = db.transaction(async (tx) => {
await tx.execute(sql`lock table tool_profile_entries in share mode`);
locked.resolve();
await release.promise;
});
blocker.catch(locked.reject);
const pending: ReturnType<typeof buildPaperclipRuntimeMcpServers>[] = [];
const start = (runId: string) => {
const admission = buildPaperclipRuntimeMcpServers({ db, agent: owner!.agent, runId });
pending.push(admission);
void admission.catch(() => {});
};
try {
await locked.promise;
start(owner!.run.id);
await vi.waitFor(async () => {
const waits = await db.execute(sql`select 1 from pg_stat_activity where datname = current_database()
and wait_event_type = 'Lock' and query like 'insert into "tool_profile_entries"%'`);
expect(waits.length).toBe(1);
});
start(next.id);
await vi.waitFor(async () => {
const waits = await db.execute(sql`select 1 from pg_stat_activity where datname = current_database()
and wait_event_type = 'Lock' and query like 'insert into "tool_profiles"%'`);
expect(waits.length).toBe(1);
});
expect(await db.select().from(toolProfiles).where(like(toolProfiles.profileKey, "native:%"))).toEqual([]);
expect(await db.select().from(toolMcpGateways)).toEqual([]);
expect(await db.select().from(toolMcpGatewayTokens)).toEqual([]);
} finally {
release.resolve();
await blocker;
await Promise.allSettled(pending);
}
const [[first], [second]] = await Promise.all(pending);
expect(first!.url).toBe(second!.url);
expect(first!.token).not.toBe(second!.token);
const profiles = await db.select().from(toolProfiles).where(like(toolProfiles.profileKey, "native:%"));
expect(profiles).toHaveLength(1);
expect(await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.profileId, profiles[0]!.id))).toHaveLength(1);
expect(await db.select().from(toolMcpGateways)).toHaveLength(1);
expect(await db.select().from(toolMcpGatewayTokens)).toHaveLength(2);
});
it("rolls back native profile creation when writing its entries fails and retries cleanly", async () => {
const [owner] = await seedAssignedAgents(1);
const build = () => buildPaperclipRuntimeMcpServers({ db, agent: owner!.agent, runId: owner!.run.id });
await db.execute(sql`alter table tool_profile_entries add constraint fixture_reject_new_profile_entries check (false) not valid`);
try {
await expect(build()).rejects.toMatchObject({ cause: { constraint_name: "fixture_reject_new_profile_entries" } });
expect(await db.select().from(toolProfiles).where(like(toolProfiles.profileKey, "native:%"))).toEqual([]);
expect(await db.select().from(toolMcpGateways)).toEqual([]);
expect(await db.select().from(toolMcpGatewayTokens)).toEqual([]);
} finally {
await db.execute(sql`alter table tool_profile_entries drop constraint fixture_reject_new_profile_entries`);
}
expect(await build()).toHaveLength(1);
const profiles = await db.select().from(toolProfiles).where(like(toolProfiles.profileKey, "native:%"));
expect(profiles).toHaveLength(1);
expect(await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.profileId, profiles[0]!.id))).toHaveLength(1);
expect(await db.select().from(toolMcpGatewayTokens)).toHaveLength(1);
});
it("rejects another agent's run token on a run-scoped gateway", async () => {
const [owner, other] = await seedAssignedAgents(2);
const [server] = await buildPaperclipRuntimeMcpServers({ db, agent: owner!.agent, runId: owner!.run.id });
@@ -1,3 +1,4 @@
import { isUniqueViolation } from "../../db-errors.js";
import { skillPackageStore, skillVersionMetadataColumns } from "../skill-package-store.js";
import { skillAccessService } from "../skill-access.js";
import { buildReviewPathRecoveryDisposition } from "../recovery/review-path-recovery.js";
@@ -1035,7 +1036,7 @@ export async function buildPaperclipRuntimeMcpServers(input: {
return [];
}
const profileKey = `native:${input.agent.id}:${assignmentDigest}`;
let [profile] = await input.db
const findProfile = () => input.db
.select()
.from(toolProfiles)
.where(
@@ -1045,6 +1046,7 @@ export async function buildPaperclipRuntimeMcpServers(input: {
),
)
.limit(1);
let [profile] = await findProfile();
if (!profile) {
const fullConnectionIds = new Set(
@@ -1081,7 +1083,7 @@ export async function buildPaperclipRuntimeMcpServers(input: {
// selector here: truncating or replacing them with connection-wide grants
// would either lose assigned tools or authorize tools outside this snapshot.
try {
const created = await access.createProfile(input.agent.companyId, {
profile = await access.createProfileRow(input.agent.companyId, {
profileKey,
name: `Native ${input.agent.id.slice(0, 8)} ${assignmentDigest.slice(0, 12)}`,
description: "Immutable Paperclip Runner MCP assignment profile.",
@@ -1094,22 +1096,10 @@ export async function buildPaperclipRuntimeMcpServers(input: {
},
entries,
});
[profile] = await input.db
.select()
.from(toolProfiles)
.where(eq(toolProfiles.id, created.id))
.limit(1);
} catch (error) {
[profile] = await input.db
.select()
.from(toolProfiles)
.where(
and(
eq(toolProfiles.companyId, input.agent.companyId),
eq(toolProfiles.profileKey, profileKey),
),
)
.limit(1);
if (!isUniqueViolation(error, "tool_profiles_company_key_uq") &&
!isUniqueViolation(error, "tool_profiles_company_name_uq")) throw error;
[profile] = await findProfile();
if (!profile) throw error;
}
}
@@ -256,9 +256,8 @@ export function createToolProfileService(db: Db, now: () => Date) {
companyId: string,
profileId: string,
entries: CreateToolProfileEntryForProfile[],
dbClient: ToolAccessMutationDb = db,
dbClient: ToolAccessMutationDb,
) {
await assertProfileEntryInputs(dbClient, companyId, entries);
if (entries.length === 0) return;
await dbClient.insert(toolProfileEntries).values(
entries.map((entry) => ({
@@ -276,6 +275,27 @@ export function createToolProfileService(db: Db, now: () => Date) {
);
}
async function createProfileRow(companyId: string, input: CreateToolProfileWithEntries) {
const entries = input.entries ?? [];
return db.transaction(async (tx) => {
await assertProfileEntryInputs(tx, companyId, entries);
const [row] = await tx
.insert(toolProfiles)
.values({
companyId,
profileKey: input.profileKey,
name: input.name,
description: input.description ?? null,
status: input.status ?? "active",
defaultAction: input.defaultAction ?? "deny",
metadata: input.metadata ?? {},
})
.returning();
await createProfileEntries(companyId, row.id, entries, tx);
return row;
});
}
async function replaceProfileEntries(
companyId: string,
profileId: string,
@@ -517,23 +537,12 @@ export function createToolProfileService(db: Db, now: () => Date) {
input: CreateToolProfileWithEntries, managementContext?: ConnectionManagementContext,
): Promise<ToolProfileWithDetails> => {
await managementContext?.reauthorize();
await assertProfileEntryInputs(db, companyId, input.entries ?? []);
const [row] = await db
.insert(toolProfiles)
.values({
companyId,
profileKey: input.profileKey,
name: input.name,
description: input.description ?? null,
status: input.status ?? "active",
defaultAction: input.defaultAction ?? "deny",
metadata: input.metadata ?? {},
})
.returning();
await createProfileEntries(companyId, row.id, input.entries ?? []);
const row = await createProfileRow(companyId, input);
return profileDetails(row.id, companyId);
},
createProfileRow,
getProfile: profileDetails,
listProfileNewTools,