From 82a12f1a8bf454c005508741550c7438878ab2b4 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Fri, 16 Oct 2026 00:42:37 -0500 Subject: [PATCH] perf(skills): batch project source authorization reads (#16536) Batch project-source read authorization through the existing decideReads path. Keep company scope, source union rules and fresh permission decisions. The local80-project fixture reduces select builders from322 to4 and complete legacy preparation from689ms to128ms. All88 affected cases and both fresh-database cases pass. Full current-head CI/E2E, security and Apex5/5 pass with no unresolved findings. This is component proof; hosted latency still needs verification. Co-Authored-By: Paperclip --- .../company-skills-runtime-selection.test.ts | 124 ++++++++++++++++++ server/src/services/skill-access.ts | 10 +- 2 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 server/src/__tests__/company-skills-runtime-selection.test.ts diff --git a/server/src/__tests__/company-skills-runtime-selection.test.ts b/server/src/__tests__/company-skills-runtime-selection.test.ts new file mode 100644 index 00000000000..3eb5ca79852 --- /dev/null +++ b/server/src/__tests__/company-skills-runtime-selection.test.ts @@ -0,0 +1,124 @@ +import { randomUUID } from "node:crypto"; +import { promises as fs } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { agents, companies, companyMemberships, companySkillSources, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, projectAccessMembers, projects, projectSkillSources } from "@paperclipai/db"; +import { and, eq } from "drizzle-orm"; +import { PAPERCLIP_FEEDBACK_SKILL_KEYS, PAPERCLIP_OPERATIONAL_SKILL_KEY } from "@paperclipai/adapter-utils/server-utils"; +import { companySkillService } from "../services/company-skills.js"; +import { authorizationService, type AuthorizationActor } from "../services/authorization.js"; +import { skillAccessService, withSkillActor } from "../services/skill-access.js"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; + +const support = await getEmbeddedPostgresTestSupport(); +const describeDatabase = support.supported ? describe : describe.skip; + +describeDatabase("selected runtime skill reconciliation", () => { + let database: Awaited>; + let db: ReturnType; + let home: string; + const previousHome = process.env.PAPERCLIP_HOME; + const previousPrivacy = process.env.PAPERCLIP_ISSUE_PRIVACY_MODE; + const companyId = randomUUID(); + + beforeAll(async () => { + database = await startEmbeddedPostgresTestDatabase("runtime-skill-selection-"); + db = createDb(database.connectionString); + home = await fs.mkdtemp(path.join(os.tmpdir(), "runtime-skill-selection-")); + process.env.PAPERCLIP_HOME = home; + process.env.PAPERCLIP_ISSUE_PRIVACY_MODE = "enforce"; + await db.insert(companies).values({ id: companyId, name: "Runtime selection", issuePrefix: `RS${companyId.slice(0, 6)}` }); + }, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS); + + afterAll(async () => { + if (previousHome === undefined) delete process.env.PAPERCLIP_HOME; + else process.env.PAPERCLIP_HOME = previousHome; + if (previousPrivacy === undefined) delete process.env.PAPERCLIP_ISSUE_PRIVACY_MODE; + else process.env.PAPERCLIP_ISSUE_PRIVACY_MODE = previousPrivacy; + await database?.cleanup(); + if (home) await fs.rm(home, { recursive: true, force: true }); + }); + + it("delivers the real legacy operational selection on cold and repeated reads", async () => { + for (const phase of ["cold", "warm", "repeated"] as const) { + const select = vi.spyOn(db, "select"); + const insert = vi.spyOn(db, "insert"); + const update = vi.spyOn(db, "update"); + const cpu = process.cpuUsage(); + const started = performance.now(); + try { + const entries = await companySkillService(db).listRuntimeSkillEntries(companyId, { + selectionConfig: { paperclipSkillSync: { desiredSkills: [] } }, + includeLegacy: true, + snapshotLatest: true, + }); + console.info(JSON.stringify({ phase, elapsedMs: performance.now() - started, + cpuMicros: process.cpuUsage(cpu), selectBuilderCalls: select.mock.calls.length, + insertBuilderCalls: insert.mock.calls.length, updateBuilderCalls: update.mock.calls.length, + delivered: entries.map(entry => entry.key) })); + expect(entries.map(entry => entry.key).sort()).toEqual([PAPERCLIP_OPERATIONAL_SKILL_KEY, ...PAPERCLIP_FEEDBACK_SKILL_KEYS].sort()); + expect(entries.every(entry => entry.sourceStatus === "available" && entry.versionId)).toBe(true); + } finally { + select.mockRestore(); + insert.mockRestore(); + update.mockRestore(); + } + } + }, 30_000); + + it("bounds project-source authorization work without caching changed permissions", async () => { + const [agent] = await db.insert(agents).values({ companyId, name: "Selected runtime reader", adapterType: "codex_local" }).returning(); + await db.insert(companyMemberships).values({ companyId, principalType: "agent", principalId: agent!.id, membershipRole: "member" }); + const actor: AuthorizationActor = { type: "agent", agentId: agent!.id, companyId, source: "agent_jwt" }; + const counts: number[] = []; + let revoked: { sourceId: string; projectId: string } | undefined; + for (const count of [5, 75]) { + const rows = await db.insert(projects).values(Array.from({ length: count }, (_, index) => ({ + id: randomUUID(), companyId, name: `Runtime source ${count}/${index}`, visibility: "private", + }))).returning(); + const sources = await db.insert(companySkillSources).values(rows.map(row => ({ + id: randomUUID(), companyId, repositoryUrl: `https://github.com/fixture/${row.id}`, fullName: `fixture/${row.id}`, + trackingRef: "main", accessScope: "projects" as const, + }))).returning(); + await db.insert(projectSkillSources).values(rows.map((row, index) => ({ + companyId, projectId: row.id, sourceId: sources[index]!.id, repositoryUrl: sources[index]!.repositoryUrl, + trackingRef: "main", status: "ready" as const, + }))); + await db.insert(projectAccessMembers).values(rows.filter((_, index) => index % 2 === 0).map(row => ({ + companyId, projectId: row.id, subjectType: "agent", subjectId: agent!.id, + }))); + revoked = { sourceId: sources[0]!.id, projectId: rows[0]!.id }; + const bindings = await db.select().from(projectSkillSources).where(eq(projectSkillSources.companyId, companyId)); + const expected = new Set(); + for (const binding of bindings) { + const decision = await authorizationService(db).decide({ actor, action: "project:read", + resource: { type: "project", companyId, projectId: binding.projectId } }); + if (decision.allowed) expected.add(binding.sourceId!); + } + const select = vi.spyOn(db, "select"); + const started = performance.now(); + try { + const visible = await skillAccessService(db, actor).visibleSourceIds(companyId); + counts.push(select.mock.calls.length); + console.info(JSON.stringify({ phase: "actor-source-authorization", projects: bindings.length, + elapsedMs: performance.now() - started, selectBuilderCalls: select.mock.calls.length })); + expect(new Set(visible)).toEqual(expected); + } finally { select.mockRestore(); } + } + await db.delete(projectAccessMembers).where(and(eq(projectAccessMembers.projectId, revoked!.projectId), + eq(projectAccessMembers.subjectId, agent!.id))); + expect(await skillAccessService(db, actor).visibleSourceIds(companyId)).not.toContain(revoked!.sourceId); + const select = vi.spyOn(db, "select"); + const runtimeStarted = performance.now(); + try { + const entries = await withSkillActor(actor, () => companySkillService(db).listRuntimeSkillEntries(companyId, { + selectionConfig: { paperclipSkillSync: { desiredSkills: [] } }, includeLegacy: true, snapshotLatest: true, + })); + console.info(JSON.stringify({ phase: "actor-runtime", projects: 80, elapsedMs: performance.now() - runtimeStarted, + selectBuilderCalls: select.mock.calls.length })); + expect(entries.map(entry => entry.key).sort()).toEqual([PAPERCLIP_OPERATIONAL_SKILL_KEY, ...PAPERCLIP_FEEDBACK_SKILL_KEYS].sort()); + } finally { select.mockRestore(); } + expect(counts[1]).toBeLessThanOrEqual(counts[0]! + 2); + }, 30_000); +}); diff --git a/server/src/services/skill-access.ts b/server/src/services/skill-access.ts index e58d2cdd8da..7fff81ffb3a 100644 --- a/server/src/services/skill-access.ts +++ b/server/src/services/skill-access.ts @@ -27,12 +27,10 @@ export function skillAccessService(db: Db, actor = currentSkillActor()) { ...(sourceId ? [eq(projectSkillSources.sourceId, sourceId)] : []))); if (!actor) return bindings; const access = authorizationService(db); - const decisions = new Map(); - for (const id of new Set(bindings.map(binding => binding.projectId))) { - decisions.set(id, (await access.decide({ actor, action: 'project:read', - resource: { type: 'project', companyId, projectId: id } })).allowed); - } - return bindings.filter(binding => decisions.get(binding.projectId)); + const ids = [...new Set(bindings.map(binding => binding.projectId))]; + const decisions = await access.decideReads(actor, ids.map(projectId => ({ type: 'project', companyId, projectId }))); + const allowed = new Set(ids.filter((_, index) => decisions[index]?.allowed)); + return bindings.filter(binding => allowed.has(binding.projectId)); } async function visibleSourceIds(companyId: string) { if (!canReadCompany(companyId)) return [];