From 5859e7cfa6b25a8fd58c9579b358f16255b3b4e5 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:57:03 -0600 Subject: [PATCH] perf(apps): avoid repeated writes during permission saves (#21033) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Apps give agents access to tools through saved profiles and approval rules. > - A save replaces the full set of action entries and access bindings. > - It also updates each Ask first rule and each retired rule in a separate query. > - Adding one agent can therefore cause hundreds of database round trips. > - This pull request keeps unchanged rows and batches the required changes. > - The benefit is faster saves with the same action permissions. ## Linked Issues or Issue Description **What happened?** An app permission save repeats database work for actions and agents that did not change. With 100 actions and 500 retired rules, one agent addition makes 636 queries. Of these, 599 update approval policies. Related boundary coverage: Refs #19973. This change keeps that issue's Off and agent-selection protections. **Expected behavior** Adding an agent applies the saved Allowed, Ask first, and Off choices. The save changes only the rows that need a change. Independent approval rules remain in force. **Steps to reproduce** 1. Create an app with 100 active actions and 99 Ask first rules. 2. Add 500 disabled app approval rules. 3. Save access for three agents. Then add a fourth agent. 4. Count database queries during the save. **Paperclip version or commit** Baseline: `51d77648bc62`. **Deployment mode** Built from source. The reproduction uses disposable local PostgreSQL. ## What Changed - Keep action entries and access bindings when their saved values match. - Insert new bindings in one query. Remove only bindings and entries that no longer apply. - Read managed approval rules for the selected app. Skip unchanged rules and rules that are already disabled. - Batch new or changed Ask first rules and disable stale enabled rules in one query. - Keep existing policy conditions, priority, author, and creation time when a rule changes. - Add real PostgreSQL regression tests and a repeatable benchmark. Document independent approval rules. ## Verification - Red: the original regression made 2,995 policy writes across five saves and failed the zero-write check. - Green: 636 focused service and runtime tests passed after the rebase. CI supplies the broad checks required by `pnpm verify` (exit 2). - Benchmark: `node --import ./server/node_modules/tsx/dist/loader.mjs scripts/benchmark-app-permission-save.ts --baseline-ref=51d77648bc62 --output=/tmp/app-permission-save.json`. - Local benchmark: two warmups and six samples in alternating order. Restore three agents outside each timed sample, then add the fourth agent. Median save time fell from 458.51 ms to 38.05 ms (12.0 times faster). Query count fell from 636 to 32. Unchanged policy writes fell from 599 to zero. - Compare all saved profile, entry, binding, and policy values after mapping fixture IDs and excluding generated row IDs and write times. Check Allowed and Ask first decisions for the added agent. Tests also cover Off, an unassigned agent, additive access, removal, independent policies, company boundaries, and re-enabled rules. - CI passes on `069e2c76252a19ec3c11a061de0506eccb0463d1`: [PR workflow](https://github.com/paperclipai/paperclip/actions/runs/43162600023). This includes typecheck, build, server tests, and all eight browser shards. - [Greptile](https://github.com/paperclipai/paperclip/pull/21033#issuecomment-6815608829) gives the final head 5/5. Its one finding is resolved. The separate Paperclip Review check is still queued. Its review task stopped after a Runner failure. - Hosted request timing and the specific reported approval prompt are not verified. No live permissions changed. ## Risks - The save now retains unchanged row IDs and timestamps. Consumers must use permission values to determine access. - Existing transaction locks and permission checks remain in place. The runtime concurrency tests exercise these paths. - The benchmark measures a local service with synthetic data. It does not establish hosted endpoint latency. - No migration or startup change is required. ## Model Used OpenAI Codex, GPT-6. Exact provider model ID is not exposed in this session. Used reasoning, tool calls, and code execution. ## 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 (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [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 --- doc/connections/CONNECTOR-PLAYBOOK.md | 18 +- scripts/benchmark-app-permission-save.ts | 150 ++++++++++++++++ .../src/__tests__/app-permission-save.test.ts | 162 ++++++++++++++++++ server/src/services/tool-access.ts | 125 ++++++-------- .../tool-access/catalog/default-access.ts | 97 +++++------ 5 files changed, 435 insertions(+), 117 deletions(-) create mode 100644 scripts/benchmark-app-permission-save.ts create mode 100644 server/src/__tests__/app-permission-save.test.ts diff --git a/doc/connections/CONNECTOR-PLAYBOOK.md b/doc/connections/CONNECTOR-PLAYBOOK.md index f723a970c5e..a5df04b1c6f 100644 --- a/doc/connections/CONNECTOR-PLAYBOOK.md +++ b/doc/connections/CONNECTOR-PLAYBOOK.md @@ -765,7 +765,23 @@ This is an opt-in restriction model. Finishing a connection is still limited to a board user with connection-configuration access, commits the selected action IDs to an auditable profile, and leaves **Ask first** available for any action. The open default changes the initial policy; it does not create a route around a -policy the operator has applied. +policy the operator has applied. Adding an agent uses the saved action choices. +An independent approval policy can still require approval for an action marked +Allowed on the app page. That page manages the app-specific Ask first rules; it +does not remove independent policies. + +The app permission save reconciles the selected targets and actions with their +saved rows. It retains unchanged bindings, entries, and Ask first policies, +batches policy changes, and leaves already-disabled rules untouched. Use the +disposable PostgreSQL benchmark to measure this path: + +```sh +node --import ./server/node_modules/tsx/dist/loader.mjs scripts/benchmark-app-permission-save.ts --baseline-ref= --output=/tmp/app-permission-save.json +``` + +The benchmark uses synthetic data, two warmups, and six samples in alternating +order. It compares saved values and action decisions. Its timings measure the +local service, not hosted endpoint latency. Saving a connection's installation list grants its app profile only to newly added targets. Targets that remain installed keep their existing profile diff --git a/scripts/benchmark-app-permission-save.ts b/scripts/benchmark-app-permission-save.ts new file mode 100644 index 00000000000..3430b62a111 --- /dev/null +++ b/scripts/benchmark-app-permission-save.ts @@ -0,0 +1,150 @@ +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { randomUUID } from "node:crypto"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { performance } from "node:perf_hooks"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { + agents, companies, createDb, startEmbeddedPostgresTestDatabase, + toolApplications, toolCatalogEntries, toolConnections, toolPolicies, +} from "../packages/db/src/index.js"; +import { toolAccessService } from "../server/src/services/tool-access.js"; +import { toolAccessPolicyService } from "../server/src/services/tool-access-policy.js"; + +const baselineRef = process.argv.find(arg => arg.startsWith("--baseline-ref="))?.slice(15); +assert.ok(baselineRef, "Supply an explicit pre-fix --baseline-ref="); +const output = process.argv.find(arg => arg.startsWith("--output="))?.slice(9); +const repository = fileURLToPath(new URL("../", import.meta.url)); +const git = (...args: string[]) => execFileSync("git", args, { cwd: repository, encoding: "utf8" }).trim(); +const baselineRevision = git("rev-parse", "--verify", `${baselineRef}^{commit}`); +const servicePath = "server/src/services/tool-access.ts"; +const policyPath = "server/src/services/tool-access/catalog/default-access.ts"; +const runtimeChanges = git("diff", baselineRevision, "--name-only", "--", "server/src", "packages/db/src", "packages/shared/src") + .split("\n").filter(Boolean).filter(name => !name.includes("/__tests__/") && !/\.test\.[cm]?ts$/.test(name)); +assert.deepEqual(runtimeChanges.filter(name => ![servicePath, policyPath].includes(name)), [], + "Isolate additional changed runtime dependencies before measuring"); +assert.equal(git("diff", baselineRevision, "--", "pnpm-lock.yaml"), "", "Dependencies must match the pinned baseline"); +const requireFromServer = createRequire(new URL("../server/package.json", import.meta.url)); +const scratch = await mkdtemp(path.join(tmpdir(), "app-permission-baseline-")); +function baselineSource(sourcePath: string) { + const source = execFileSync("git", ["show", `${baselineRevision}:${sourcePath}`], { cwd: repository, encoding: "utf8" }); + return source.replace(/from "([^"]+)"/g, (_match, specifier: string) => { + const resolved = sourcePath === servicePath && specifier === "./tool-access/catalog/default-access.js" + ? path.join(scratch, "default-access.mts") + : specifier.startsWith(".") + ? fileURLToPath(new URL(specifier, new URL(sourcePath, pathToFileURL(`${repository}/`)))) + : requireFromServer.resolve(specifier).replace(/\.cjs$/, ".js"); + return `from ${JSON.stringify(resolved)}`; + }); +} +const keepAlive = setInterval(() => {}, 1000); +let database: Awaited> | undefined; +let db: ReturnType | undefined; +try { + await writeFile(path.join(scratch, "default-access.mts"), baselineSource(policyPath)); + await writeFile(path.join(scratch, "tool-access.mts"), baselineSource(servicePath)); + const baseline = await import(pathToFileURL(path.join(scratch, "tool-access.mts")).href) as { toolAccessService: typeof toolAccessService }; + database = await startEmbeddedPostgresTestDatabase("app-permission-benchmark-"); + const client = db = createDb(database.connectionString); + type Statement = { sql: string; params: unknown[] }; + let captured: Statement[] | undefined; + client.$client.options.debug = (_connection, sql, params) => { + captured?.push({ sql, params: [...params] }); + }; + const fixtures = []; + for (const [label, factory] of [["baseline", baseline.toolAccessService], ["treatment", toolAccessService]] as const) { + const [company] = await client.insert(companies).values({ name: "Benchmark", issuePrefix: `B${randomUUID().slice(0, 6)}` }).returning(); + const roster = await client.insert(agents).values(Array.from({ length: 4 }, (_, i) => ({ companyId: company.id, name: `Agent ${i}` }))).returning(); + const [application] = await client.insert(toolApplications).values({ companyId: company.id, name: "App", type: "mcp_http" }).returning(); + const [connection] = await client.insert(toolConnections).values({ companyId: company.id, applicationId: application.id, + uid: randomUUID(), name: "App connection", transport: "mcp_remote", status: "active", enabled: true }).returning(); + const catalog = await client.insert(toolCatalogEntries).values(Array.from({ length: 100 }, (_, i) => ({ + companyId: company.id, applicationId: application.id, connectionId: connection.id, + name: `action_${i}`, toolName: `action_${i}`, riskLevel: "write" as const, status: "active" as const, + versionHash: `v${i}`, schemaHash: `s${i}`, + }))).returning(); + const service = factory(client); + const save = (agentIds: string[]) => service.finishGalleryAppConnection(company.id, connection.id, { + access: { agentIds }, enabledCatalogEntryIds: catalog.map(entry => entry.id), + askFirstCatalogEntryIds: catalog.slice(1).map(entry => entry.id), + }); + const initial = await save(roster.slice(0, 3).map(agent => agent.id)); + await client.insert(toolPolicies).values(Array.from({ length: 500 }, (_, i) => ({ + companyId: company.id, name: `Retired ${i}`, policyType: "require_approval" as const, enabled: false, + config: { source: "app_gallery_finish", connectionId: connection.id, catalogEntryId: randomUUID() }, + }))); + const ids = new Map([[company.id, "company"], [connection.id, "connection"], [application.id, "application"], [initial.profile.id, "profile"], + ...roster.map((agent, i) => [agent.id, `agent_${i}`] as [string, string]), + ...catalog.map((entry, i) => [entry.id, `catalog_${i}`] as [string, string])]); + function normalize(value: unknown): unknown { + if (Array.isArray(value)) return value.map(normalize).sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b))); + if (typeof value === "string") return ids.get(value) ?? value.replaceAll(connection.id, "connection").replaceAll(connection.id.slice(0, 8), "connection"); + if (value && typeof value === "object") return Object.fromEntries(Object.entries(value) + .filter(([key]) => !["id", "createdAt", "updatedAt"].includes(key)).map(([key, nested]) => [key, normalize(nested)])); + return value; + } + fixtures.push({ label, save: () => save(roster.map(agent => agent.id)), + reset: () => save(roster.slice(0, 3).map(agent => agent.id)), normalize, + company, roster, connection, catalog, samples: [] as number[], statements: [] as Statement[], result: initial }); + } + for (let i = 0; i < 2; i++) for (const fixture of fixtures) { + await fixture.reset(); + await fixture.save(); + } + for (let i = 0; i < 6; i++) { + for (const fixture of i % 2 ? [...fixtures].reverse() : fixtures) { + await fixture.reset(); + captured = []; + const started = performance.now(); + fixture.result = await fixture.save(); + fixture.samples.push(performance.now() - started); + fixture.statements = captured; + captured = undefined; + } + } + const snapshot = (fixture: typeof fixtures[number]) => fixture.normalize({ + profile: fixture.result.profile, entries: fixture.result.profileEntries, + bindings: fixture.result.profileBindings, policies: fixture.result.policies, + }); + assert.deepEqual(snapshot(fixtures[0]), snapshot(fixtures[1]), "Complete saved profile/entry/binding/policy values must match except generated IDs and write timestamps"); + const policy = toolAccessPolicyService(client); + for (const fixture of fixtures) { + for (const [entry, expected] of [[fixture.catalog[0], "allow"], [fixture.catalog[1], "require_approval"]] as const) { + const result = await policy.decide({ companyId: fixture.company.id, + actor: { actorType: "agent", actorId: fixture.roster[3].id, agentId: fixture.roster[3].id }, + request: { connectionId: fixture.connection.id, catalogEntryId: entry.id, toolName: entry.toolName, arguments: {} } }); + assert.equal(result.decision, expected); + } + } + const measurements = fixtures.map(fixture => { + const sorted = [...fixture.samples].sort((a, b) => a - b); + const policyWrites = fixture.statements.filter(statement => /^(insert into|update|delete from) "tool_policies"/i.test(statement.sql)); + return { label: fixture.label, samplesMs: fixture.samples, medianMs: (sorted[2] + sorted[3]) / 2, + minMs: sorted[0], maxMs: sorted.at(-1), queriesPerSave: fixture.statements.length, policyWritesPerSave: policyWrites.length }; + }); + assert.equal(measurements[1].policyWritesPerSave, 0, "Unchanged policies must not be written"); + assert.ok(measurements[1].queriesPerSave < measurements[0].queriesPerSave / 5, "Remove at least 80% of round trips on this fixture"); + const plans = []; + for (const fixture of fixtures) { + const select = fixture.statements.find(statement => /^select .* from "tool_policies" where /i.test(statement.sql)); + if (select) plans.push({ label: fixture.label, sql: select.sql, + plan: await client.$client.unsafe(`EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ${select.sql}`, select.params as never[]) }); + } + const report = { baselineRevision, treatmentRevision: git("rev-parse", "HEAD"), treatmentDiff: git("diff", "--", servicePath, policyPath), + postgres: await client.$client`select version(), current_setting('shared_buffers') as shared_buffers`, + fixture: { actions: 100, askFirst: 99, disabledPolicies: 500, agentsBefore: 3, agentsAfter: 4, warmups: 2, samples: 6, order: "alternating", reset: "restore three-agent selection outside each timed sample" }, + measurements, speedup: measurements[0].medianMs / measurements[1].medianMs, + correctness: "Complete profile, entries, bindings, and policies match after mapping fixture IDs and excluding generated row IDs and write timestamps. Allowed and Ask first decisions match for the added agent.", + scope: "Local assembled-service timings with disposable PostgreSQL. Hosted endpoint latency is unverified.", + plans, statements: fixtures.map(fixture => ({ label: fixture.label, statements: fixture.statements })) }; + if (output) await writeFile(output, JSON.stringify(report, null, 2)); + console.log(JSON.stringify({ ...report, treatmentDiff: undefined, statements: undefined, plans: undefined }, null, 2)); +} finally { + clearInterval(keepAlive); + await db?.$client.end(); + await database?.cleanup(); + await rm(scratch, { recursive: true, force: true }); +} diff --git a/server/src/__tests__/app-permission-save.test.ts b/server/src/__tests__/app-permission-save.test.ts new file mode 100644 index 00000000000..1c9211c12af --- /dev/null +++ b/server/src/__tests__/app-permission-save.test.ts @@ -0,0 +1,162 @@ +import { randomUUID } from "node:crypto"; +import { performance } from "node:perf_hooks"; +import { and, eq } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { + agents, companies, createDb, toolApplications, toolCatalogEntries, + toolConnections, toolConnectionInstalls, toolPolicies, toolProfileBindings, toolProfileEntries, +} from "@paperclipai/db"; +import { toolAccessService } from "../services/tool-access.js"; +import { toolAccessPolicyService } from "../services/tool-access-policy.js"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; + +const support = await getEmbeddedPostgresTestSupport(); + +(support.supported ? describe : describe.skip)("app permission saves", () => { + let db: ReturnType; + let database: Awaited>; + beforeAll(async () => { + database = await startEmbeddedPostgresTestDatabase("app-permission-save-"); + db = createDb(database.connectionString); + }, 30000); + afterAll(async () => { await database?.cleanup(); }); + + async function fixture(toolCount = 3) { + const [company] = await db.insert(companies).values({ name: "App permissions", issuePrefix: `AP${randomUUID().slice(0, 6)}` }).returning(); + const roster = await db.insert(agents).values(Array.from({ length: 4 }, (_, i) => ({ companyId: company.id, name: `Agent ${i}` }))).returning(); + const [application] = await db.insert(toolApplications).values({ companyId: company.id, name: "App", type: "mcp_http" }).returning(); + const [connection] = await db.insert(toolConnections).values({ companyId: company.id, applicationId: application.id, + uid: randomUUID(), name: "App connection", transport: "mcp_remote", status: "active", enabled: true, + config: { url: "https://example.com/mcp" } }).returning(); + const catalog = await db.insert(toolCatalogEntries).values(Array.from({ length: toolCount }, (_, i) => ({ + companyId: company.id, applicationId: application.id, connectionId: connection.id, + name: `action_${i}`, toolName: `action_${i}`, riskLevel: i === 0 ? "read" as const : "write" as const, + status: "active" as const, versionHash: `v${i}`, schemaHash: `s${i}`, + }))).returning(); + const service = toolAccessService(db); + const save = (agentIds: string[], enabledIds = catalog.map(entry => entry.id), askIds = catalog.slice(1).map(entry => entry.id)) => + service.finishGalleryAppConnection(company.id, connection.id, { + enabledCatalogEntryIds: enabledIds, askFirstCatalogEntryIds: askIds, access: { agentIds }, + }); + return { company, roster, connection, catalog, service, save }; + } + + it("adds an agent without rewriting unchanged action policies or bindings, including retired rules", async () => { + const f = await fixture(100); + const initial = await f.save(f.roster.slice(0, 3).map(agent => agent.id)); + await db.insert(toolPolicies).values(Array.from({ length: 500 }, (_, i) => ({ + companyId: f.company.id, name: `Retired ${i}`, policyType: "require_approval" as const, enabled: false, + config: { source: "app_gallery_finish", connectionId: f.connection.id, catalogEntryId: randomUUID() }, + }))); + const queries: string[] = []; + const previousDebug = db.$client.options.debug; + db.$client.options.debug = (_connection, query) => { queries.push(query); }; + const timings: number[] = []; + let result = initial; + try { + for (let i = 0; i < 5; i++) { + const started = performance.now(); + result = await f.save(f.roster.map(agent => agent.id)); + timings.push(performance.now() - started); + } + } finally { + db.$client.options.debug = previousDebug; + } + const policyWrites = queries.filter(query => /^(insert into|update|delete from) "tool_policies"/i.test(query)); + console.info("app agent-access save", { timingsMs: timings.map(n => Math.round(n)), queries: queries.length, policyWrites: policyWrites.length }); + expect(policyWrites).toHaveLength(0); + expect(result.profileBindings.map(binding => binding.targetId).sort()).toEqual(f.roster.map(agent => agent.id).sort()); + for (const binding of initial.profileBindings) expect(result.profileBindings).toContainEqual(binding); + expect(result.profileEntries).toEqual(initial.profileEntries); + const byId = (a: { id: string }, b: { id: string }) => a.id.localeCompare(b.id); + expect([...result.policies].sort(byId)).toEqual([...initial.policies].sort(byId)); + }); + + it("applies Allowed, Ask first, and Off to newly added agents and preserves other approval policies", async () => { + const f = await fixture(); + const [allowed, ask, off] = f.catalog; + const enabledIds = [allowed.id, ask.id]; + await f.save([f.roster[0].id], enabledIds, [ask.id]); + await f.save([f.roster[0].id, f.roster[1].id], enabledIds, [ask.id]); + const policy = toolAccessPolicyService(db); + const decision = (catalogEntry: typeof allowed, agentId = f.roster[1].id) => policy.decide({ + companyId: f.company.id, actor: { actorType: "agent", actorId: agentId, agentId }, + request: { connectionId: f.connection.id, catalogEntryId: catalogEntry.id, toolName: catalogEntry.toolName, arguments: {} }, + }); + expect(await decision(allowed)).toMatchObject({ allowed: true }); + expect(await decision(ask)).toMatchObject({ decision: "require_approval" }); + expect(await decision(off)).toMatchObject({ decision: "deny" }); + expect(await decision(allowed, f.roster[2].id)).toMatchObject({ decision: "deny" }); + const [separatePolicy] = await db.insert(toolPolicies).values({ companyId: f.company.id, name: "Separate approval rule", + policyType: "require_approval", priority: 0, selectors: { catalogEntryId: allowed.id }, config: { source: "operator" } }).returning(); + await f.save(f.roster.slice(0, 2).map(agent => agent.id), enabledIds, []); + expect(await decision(ask)).toMatchObject({ allowed: true }); + expect(await decision(allowed)).toMatchObject({ decision: "require_approval", matchedPolicyIds: [separatePolicy.id] }); + const managedPolicies = await db.select().from(toolPolicies).where(and(eq(toolPolicies.companyId, f.company.id), eq(toolPolicies.enabled, true))); + expect(managedPolicies.map(row => row.id)).toEqual([separatePolicy.id]); + const [profileBinding] = await db.select().from(toolProfileBindings).where(eq(toolProfileBindings.targetId, f.roster[1].id)); + expect(await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.profileId, profileBinding.profileId))).toHaveLength(2); + }); + + it("batches policy changes and keeps other apps, companies, and policy settings intact", async () => { + const f = await fixture(30); + const other = await fixture(); + const otherResult = await other.save([other.roster[0].id]); + const [otherConnection] = await db.insert(toolConnections).values({ + companyId: f.company.id, applicationId: f.connection.applicationId, + uid: randomUUID(), name: "Other connection", transport: "mcp_remote", status: "active", enabled: true, + }).returning(); + const [otherPolicy] = await db.insert(toolPolicies).values({ + companyId: f.company.id, name: "Other app policy", policyType: "require_approval", + config: { source: "app_gallery_finish", connectionId: otherConnection.id, catalogEntryId: randomUUID() }, + }).returning(); + const initial = await f.save([f.roster[0].id]); + const original = initial.policies[0]; + await db.update(toolPolicies).set({ priority: 17, conditions: { actorType: "agent" }, createdByUserId: "original-owner" }) + .where(eq(toolPolicies.id, original.id)); + const queries: string[] = []; + const previousDebug = db.$client.options.debug; + db.$client.options.debug = (_connection, query) => { queries.push(query); }; + try { + await f.save([f.roster[0].id], f.catalog.map(entry => entry.id), []); + expect(queries.filter(query => /^update "tool_policies"/i.test(query))).toHaveLength(1); + expect(queries.filter(query => /^insert into "tool_policies"/i.test(query))).toHaveLength(0); + queries.length = 0; + const restored = await f.save([f.roster[0].id]); + expect(queries.filter(query => /^insert into "tool_policies"/i.test(query))).toHaveLength(1); + expect(restored.policies.find(policy => policy.id === original.id)).toMatchObject({ + enabled: true, priority: 17, conditions: { actorType: "agent" }, createdByUserId: "original-owner", createdAt: original.createdAt, + }); + } finally { + db.$client.options.debug = previousDebug; + } + expect((await db.select().from(toolPolicies).where(eq(toolPolicies.id, otherPolicy.id)))[0]).toEqual(otherPolicy); + expect(await db.select().from(toolPolicies).where(eq(toolPolicies.companyId, other.company.id))) + .toEqual(otherResult.policies); + }); + + it("preserves additive access and removes bindings and actions when access is replaced", async () => { + const f = await fixture(); + const [first, second] = f.catalog; + const initial = await f.save([f.roster[0].id], [first.id], [first.id]); + const additive = await f.service.finishGalleryAppConnection(f.company.id, f.connection.id, { + access: { agentIds: [f.roster[1].id] }, enabledCatalogEntryIds: [second.id], + askFirstCatalogEntryIds: [second.id], preserveExistingAccess: true, + }); + expect(additive.profileBindings).toEqual(expect.arrayContaining(initial.profileBindings)); + expect(additive.profileEntries).toEqual(expect.arrayContaining(initial.profileEntries)); + expect((await db.select().from(toolPolicies).where(and( + eq(toolPolicies.companyId, f.company.id), eq(toolPolicies.enabled, true), + )))).toHaveLength(2); + const replaced = await f.save([f.roster[1].id], [second.id], []); + expect(replaced.profileBindings).toEqual(additive.profileBindings.filter(binding => binding.targetId === f.roster[1].id)); + expect(replaced.profileEntries).toEqual(additive.profileEntries.filter(entry => entry.catalogEntryId === second.id)); + expect(await db.select().from(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, f.connection.id))) + .toMatchObject([{ targetType: "agent", targetId: f.roster[1].id }]); + const empty = await f.save([], [], []); + expect(empty.profileBindings).toEqual([]); + expect(empty.profileEntries).toEqual([]); + expect(await db.select().from(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, f.connection.id))).toEqual([]); + }); + +}); diff --git a/server/src/services/tool-access.ts b/server/src/services/tool-access.ts index f0b2cefff46..1a3b6d9f5aa 100644 --- a/server/src/services/tool-access.ts +++ b/server/src/services/tool-access.ts @@ -3320,6 +3320,8 @@ export function toolAccessService( connection.name, otherProfiles.map((profile) => profile.name), ); + const priorEntries = existingProfile ? await tx.select().from(toolProfileEntries) + .where(and(eq(toolProfileEntries.companyId, companyId), eq(toolProfileEntries.profileId, existingProfile.id))) : []; let profileId: string; if (existingProfile) { if (input.preserveExistingAccess) { @@ -3337,10 +3339,6 @@ export function toolAccessService( priority: prior.priority, metadata: prior.metadata, }); - const priorEntries = await tx - .select() - .from(toolProfileEntries) - .where(eq(toolProfileEntries.profileId, existingProfile.id)); for (const prior of priorEntries) if ( !entries.some( @@ -3360,38 +3358,6 @@ export function toolAccessService( conditions: prior.conditions, }); } - await tx - .delete(toolProfileBindings) - .where( - and( - eq(toolProfileBindings.companyId, companyId), - eq(toolProfileBindings.profileId, existingProfile.id), - ), - ); - await tx - .delete(toolProfileEntries) - .where( - and( - eq(toolProfileEntries.companyId, companyId), - eq(toolProfileEntries.profileId, existingProfile.id), - ), - ); - if (entries.length > 0) { - await tx.insert(toolProfileEntries).values( - entries.map((entry) => ({ - companyId, - profileId: existingProfile.id, - selectorType: entry.selectorType, - effect: entry.effect ?? "include", - applicationId: entry.applicationId ?? null, - connectionId: entry.connectionId ?? null, - catalogEntryId: entry.catalogEntryId ?? null, - toolName: entry.toolName ?? null, - riskLevel: entry.riskLevel ?? null, - conditions: entry.conditions ?? null, - })), - ); - } const [updated] = await tx .update(toolProfiles) .set({ @@ -3424,25 +3390,35 @@ export function toolAccessService( }, }) .returning(); - if (entries.length > 0) { - await tx.insert(toolProfileEntries).values( - entries.map((entry) => ({ - companyId, - profileId: created.id, - selectorType: entry.selectorType, - effect: entry.effect ?? "include", - applicationId: entry.applicationId ?? null, - connectionId: entry.connectionId ?? null, - catalogEntryId: entry.catalogEntryId ?? null, - toolName: entry.toolName ?? null, - riskLevel: entry.riskLevel ?? null, - conditions: entry.conditions ?? null, - })), - ); - } profileId = created.id; } + const entryValues = (entry: CreateToolProfileEntryForProfile) => ({ + companyId, + profileId, + selectorType: entry.selectorType, + effect: entry.effect ?? "include", + applicationId: entry.applicationId ?? null, + connectionId: entry.connectionId ?? null, + catalogEntryId: entry.catalogEntryId ?? null, + toolName: entry.toolName ?? null, + riskLevel: entry.riskLevel ?? null, + conditions: entry.conditions ?? null, + }); + const priorEntriesByValue = new Map(); + for (const entry of priorEntries) { + const key = stableHash(entryValues(entry)); + const matches = priorEntriesByValue.get(key) ?? []; + matches.push(entry); + priorEntriesByValue.set(key, matches); + } + const addedEntries = entries.map(entryValues).filter((entry) => !priorEntriesByValue.get(stableHash(entry))?.shift()); + const removedEntries = [...priorEntriesByValue.values()].flat(); + if (removedEntries.length) await tx.delete(toolProfileEntries).where(and( + eq(toolProfileEntries.companyId, companyId), inArray(toolProfileEntries.id, removedEntries.map(entry => entry.id)), + )); + if (addedEntries.length) await tx.insert(toolProfileEntries).values(addedEntries); + const profileBindings: ToolProfileBinding[] = []; // Agent access is also the credential admission boundary. Save its // installation and permission targets together for every tool connector. @@ -3467,24 +3443,37 @@ export function toolAccessService( details: { added: added.map(({ targetType, targetId }) => ({ targetType, targetId })), removed: removed.map(({ targetType, targetId }) => ({ targetType, targetId })) }, }); } + const existingBindingsByTarget = new Map(existingBindings.map(binding => [`${binding.targetType}:${binding.targetId}`, binding])); + const retainedBindings = new Map(); + const addedBindings: Array = []; for (const bindingInput of bindingInputs) { - const [binding] = await tx - .insert(toolProfileBindings) - .values({ - companyId, - profileId, - targetType: bindingInput.targetType, - targetId: bindingInput.targetId, - priority: bindingInput.priority ?? 100, - metadata: bindingInput.metadata ?? {}, - createdByAgentId: - actor?.actorType === "agent" ? (actor.actorId ?? null) : null, - createdByUserId: - connectionActorUserId(actor), - }) - .returning(); - profileBindings.push(toProfileBinding(binding)); + const key = `${bindingInput.targetType}:${bindingInput.targetId}`; + const existing = existingBindingsByTarget.get(key); + if (existing && existing.priority === (bindingInput.priority ?? 100) + && stableHash(existing.metadata) === stableHash(bindingInput.metadata ?? {})) { + retainedBindings.set(key, existing); + continue; + } + addedBindings.push({ + companyId, + profileId, + targetType: bindingInput.targetType, + targetId: bindingInput.targetId, + priority: bindingInput.priority ?? 100, + metadata: bindingInput.metadata ?? {}, + createdByAgentId: actor?.actorType === "agent" ? (actor.actorId ?? null) : null, + createdByUserId: connectionActorUserId(actor), + }); } + const removedBindings = existingBindings.filter(binding => !retainedBindings.has(`${binding.targetType}:${binding.targetId}`)); + if (removedBindings.length) await tx.delete(toolProfileBindings).where(and( + eq(toolProfileBindings.companyId, companyId), inArray(toolProfileBindings.id, removedBindings.map(binding => binding.id)), + )); + if (addedBindings.length) { + const saved = await tx.insert(toolProfileBindings).values(addedBindings).returning(); + for (const binding of saved) retainedBindings.set(`${binding.targetType}:${binding.targetId}`, binding); + } + profileBindings.push(...bindingInputs.map(binding => toProfileBinding(retainedBindings.get(`${binding.targetType}:${binding.targetId}`)!))); const reviewedAt = new Date(); if (reviewedIds.length > 0) { diff --git a/server/src/services/tool-access/catalog/default-access.ts b/server/src/services/tool-access/catalog/default-access.ts index 408a33d9b14..7f77a9560dc 100644 --- a/server/src/services/tool-access/catalog/default-access.ts +++ b/server/src/services/tool-access/catalog/default-access.ts @@ -1,7 +1,8 @@ +import { isDeepStrictEqual } from "node:util"; import { connectionActorUserId } from "../actor.js"; import { lockManagedAppPolicies, lockToolAssignmentAgents } from "../assignment-locks.js"; import type { ActorInfo, ToolAccessMutationDb } from "../types.js"; -import { and, eq, inArray } from "drizzle-orm"; +import { and, eq, inArray, sql } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; import { toolCatalogEntries, toolConnections, toolPolicies, toolProfileBindings, toolProfileEntries, toolProfiles } from "@paperclipai/db"; import type { ToolPolicy } from "@paperclipai/shared"; @@ -230,22 +231,17 @@ export function createCatalogDefaultAccess(db: Db) { }, dbClient: ToolAccessMutationDb = db, ): Promise { - const existingPolicies = await dbClient + const managedPolicies = await dbClient .select() .from(toolPolicies) .where( and( eq(toolPolicies.companyId, input.companyId), eq(toolPolicies.policyType, "require_approval"), + sql`${toolPolicies.config}->>'source' = 'app_gallery_finish'`, + sql`${toolPolicies.config}->>'connectionId' = ${input.connection.id}`, ), ); - const managedPolicies = existingPolicies.filter((policy) => { - const config = asRecord(policy.config); - return ( - config.source === "app_gallery_finish" && - config.connectionId === input.connection.id - ); - }); const policiesByCatalogEntryId = new Map< string, typeof toolPolicies.$inferSelect @@ -257,7 +253,7 @@ export function createCatalogDefaultAccess(db: Db) { } } const askFirstIds = new Set(input.askFirstEntries.map((entry) => entry.id)); - const results: ToolPolicy[] = []; + const writes: Array = []; for (const entry of input.askFirstEntries) { const config = { source: "app_gallery_finish", @@ -265,59 +261,64 @@ export function createCatalogDefaultAccess(db: Db) { catalogEntryId: entry.id, }; const existing = policiesByCatalogEntryId.get(entry.id); - if (existing) { - const [updated] = await dbClient - .update(toolPolicies) - .set({ - name: policyNameForApp(input.connection, entry), - description: `Ask first before running ${entry.toolName}.`, - enabled: true, - selectors: { catalogEntryId: entry.id }, - config, - updatedAt: new Date(), - }) - .where(eq(toolPolicies.id, existing.id)) - .returning(); - results.push(toPolicy(updated)); - } else { - const [created] = await dbClient - .insert(toolPolicies) - .values({ - companyId: input.companyId, - name: policyNameForApp(input.connection, entry), - description: `Ask first before running ${entry.toolName}.`, - policyType: "require_approval", - priority: 50, - enabled: true, - selectors: { catalogEntryId: entry.id }, - config, - createdByAgentId: - input.actor?.actorType === "agent" - ? (input.actor.actorId ?? null) - : null, - createdByUserId: - connectionActorUserId(input.actor), - }) - .returning(); - results.push(toPolicy(created)); + const values = { + companyId: input.companyId, + name: policyNameForApp(input.connection, entry), + description: `Ask first before running ${entry.toolName}.`, + policyType: "require_approval" as const, + priority: 50, + enabled: true, + selectors: { catalogEntryId: entry.id }, + config, + createdByAgentId: + input.actor?.actorType === "agent" ? (input.actor.actorId ?? null) : null, + createdByUserId: connectionActorUserId(input.actor), + }; + if ( + !existing || !existing.enabled || existing.name !== values.name + || existing.description !== values.description + || !isDeepStrictEqual(existing.selectors, values.selectors) + || !isDeepStrictEqual(existing.config, values.config) + ) { + writes.push({ ...values, ...(existing ? { id: existing.id } : {}), updatedAt: new Date() }); + } + } + if (writes.length > 0) { + const saved = await dbClient.insert(toolPolicies).values(writes).onConflictDoUpdate({ + target: toolPolicies.id, + set: { + name: sql`excluded.name`, + description: sql`excluded.description`, + enabled: true, + selectors: sql`excluded.selectors`, + config: sql`excluded.config`, + updatedAt: sql`excluded.updated_at`, + }, + }).returning(); + for (const policy of saved) { + policiesByCatalogEntryId.set(String(asRecord(policy.config).catalogEntryId), policy); } } if (input.disableStale !== false) { const stalePolicies = managedPolicies.filter((policy) => { const config = asRecord(policy.config); return ( + policy.enabled && typeof config.catalogEntryId === "string" && !askFirstIds.has(config.catalogEntryId) ); }); - for (const policy of stalePolicies) { + if (stalePolicies.length > 0) { await dbClient .update(toolPolicies) .set({ enabled: false, updatedAt: new Date() }) - .where(eq(toolPolicies.id, policy.id)); + .where(and( + eq(toolPolicies.companyId, input.companyId), + inArray(toolPolicies.id, stalePolicies.map(policy => policy.id)), + )); } } - return results; + return input.askFirstEntries.map(entry => toPolicy(policiesByCatalogEntryId.get(entry.id)!)); } return { enableCatalogEntriesByDefault, upsertAskFirstPolicies }; }