Files
PaperClipAI/server/src/__tests__/connection-instructions.test.ts
T
Nicky LeachandPaperclip d8ae55198d Scope runtime catalog reads to standing-instruction sources (#22356)
## Thinking Path

> - Paperclip uses standing instructions from authorized app connections
when it prepares a run.
> - The gateway already knows which connections have instructions
enabled.
> - The catalog read still loads tool schemas for every eligible
connection in the company.
> - This work delays prompt preparation and consumes database and
application resources.
> - The gateway can restrict the existing catalog query while preserving
the names used for authorization.

## Linked Issues or Issue Description

**Subsystem affected** — Tool gateway and run preparation.

**Problem or motivation** — The instruction resolver filters tool
descriptors after the database has returned unrelated catalog entries.

**Proposed solution** — Scope the persisted catalog read to possible
instruction sources. Include peers that share a candidate’s short
connection ID when assigning tool names.

**Alternatives considered** — An exact connection ID filter could change
a tool name and its policy decision. A separate instruction
authorization query would duplicate gateway policy.

**Roadmap alignment** — This improves the existing task-start path.
Related work includes #22347 and #14864.

**Additional context** — The change uses the existing read-only catalog
snapshot. It adds no schema or cache.

## What Changed

- Pass the instruction candidates to the shared gateway catalog reader.
- Read eligible naming peers in the same snapshot. Return only candidate
descriptors after assigning their names.
- Retain the existing run identity, guest restrictions, model
visibility, provider tools, policy decisions and grants.

## Verification

- Ran the connection instruction and gateway listing-memory integration
suites against embedded PostgreSQL. All 23 tests passed.
- Confirmed the bounded-read assertion fails on the baseline and passes
on the change. A full listing reads 20 catalog entries from two
connections. The instruction lookup reads 10 entries from one.
- Covered exact-name approval and block policies, peer quarantine,
unchanged instruction text and digest, and visibility of unrelated tools
in normal discovery.
- Ran integration tests for gateway discovery, gateway service behavior,
and assigned MCP tools. All 256 tests passed.
- Compared complete instruction resolution with embedded PostgreSQL on
macOS, using 16 alternating measured pairs after five warm-ups. The
one-tool fixture took 10.05 ms median on the baseline and 10.32 ms on
the change. With 1,994 synthetic entries across 21 connections and four
candidate entries, the medians were 50.44 ms and 24.47 ms. The median
paired saving was 26.03 ms. Final instruction text and digest matched
for every pair.
- The comparison measures the local instruction resolver. Remote
submission-to-turn latency was outside this verification.

## Risks

The candidate read uses the first UUID component because it participates
in gateway naming. Matching peers contribute to name collisions even
when an agent cannot use the peer. The regression checks that a blocked
candidate cannot gain access when the catalog is narrowed. Companies
that enable instructions on most connections can expect smaller savings.

## Model Used

OpenAI Codex directed and qualified the change. GPT-6.2 Sol implemented
the change and focused regression tests.

## 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
- [ ] 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 <noreply@paperclip.ing>
2026-11-19 14:07:48 -08:00

299 lines
20 KiB
TypeScript

import { tmpdir } from "node:os";
import { join } from "node:path";
import { nativeSystemInstructions } from "../../../packages/paperclip-runner/src/backends/runtime-context.js";
import type { NativeExecutionInput } from "../vendor/paperclip-runner/index.js";
import { randomUUID } from "node:crypto";
import { access, mkdtemp, writeFile, rm, readFile } from "node:fs/promises";
import { eq } from "drizzle-orm";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { agents, companies, companyMemberships, connectionGrants, createDb, heartbeatRuns, issues, toolApplications, toolCatalogEntries, toolConnections, toolPolicies, toolProfileBindings, toolActionRequests } from "@paperclipai/db";
import { createToolGatewayService } from "../services/tool-gateway.js";
import { toolDiscoveryScheduler } from "../services/tool-discovery-scheduler.js";
import { toolAccessService, projectedConnectionToolArguments, projectedConnectionToolInputSchema } from "../services/tool-access.js";
import { composeConnectionInstructions, prepareConnectionInstructionDelivery } from "../services/connection-instructions.js";
import { renderPaperclipWakePrompt } from "@paperclipai/adapter-utils/server-utils";
import { canonicalNativeRuntimeContextDigest, parseNativeRuntimeContext } from "../vendor/paperclip-runner/index.js";
import { nativeRuntimeContextFixture } from "../services/native-runtime/runtime-context.test-fixture.js";
import { execute as executeProcess } from "../adapters/process/execute.js";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
const support = await getEmbeddedPostgresTestSupport();
const paragraph = "Use the release handbook before changing a deployment. Cite the relevant release checklist.";
async function createRunFixture(db: ReturnType<typeof createDb>) {
const company = await db.insert(companies).values({
name: `Gateway ${randomUUID()}`,
issuePrefix: `TG${randomUUID().slice(0, 6).toUpperCase()}`,
}).returning().then((rows) => rows[0]!);
const agent = await db.insert(agents).values({
companyId: company.id,
name: `Gateway Agent ${randomUUID()}`,
role: "engineer",
adapterType: "process",
adapterConfig: {},
runtimeConfig: {},
permissions: {},
}).returning().then((rows) => rows[0]!);
const issue = await db.insert(issues).values({
companyId: company.id,
title: "Gateway approval work",
status: "in_progress",
assigneeAgentId: agent.id,
}).returning().then((rows) => rows[0]!);
const run = await db.insert(heartbeatRuns).values({
companyId: company.id,
agentId: agent.id,
invocationSource: "assignment",
status: "running",
contextSnapshot: { issueId: issue.id },
}).returning().then((rows) => rows[0]!);
return { company, agent, issue, run };
}
async function createRemoteMcpToolFixture(db: ReturnType<typeof createDb>, companyId: string, id?: string) {
const applicationKey = `remote-${randomUUID().slice(0, 8)}`;
const application = await db.insert(toolApplications).values({
companyId,
applicationKey,
name: `Remote MCP ${applicationKey}`,
type: "mcp_http",
status: "active",
}).returning().then((rows) => rows[0]!);
const connection = await db.insert(toolConnections).values({
id,
companyId,
applicationId: application.id,
name: "Remote connection",
uid: `test/${randomUUID()}`,
transport: "mcp_remote",
status: "active",
enabled: true,
healthStatus: "ok",
// Use a public IP literal so protocol tests remain independent of DNS while
// still exercising the production egress guard and their global fetch stub.
credentialPolicy: "shared",
config: { url: "https://8.8.8.8/mcp" },
}).returning().then((rows) => rows[0]!);
await db.insert(connectionGrants).values({
companyId,
connectionId: connection.id,
kind: "organization",
credentialSecretRefs: [],
status: "active",
isDefault: true,
});
const catalogEntry = await db.insert(toolCatalogEntries).values({
companyId,
applicationId: application.id,
connectionId: connection.id,
entryKind: "tool",
name: "needs_input",
toolName: "needs_input",
title: "Needs input",
riskLevel: "read",
isReadOnly: true,
status: "active",
versionHash: randomUUID(),
schemaHash: randomUUID(),
}).returning().then((rows) => rows[0]!);
return { application, connection, catalogEntry };
}
(support.supported ? describe : describe.skip)("generic connection instruction delivery", () => {
let db: ReturnType<typeof createDb>;
let temp: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
beforeAll(async () => { temp = await startEmbeddedPostgresTestDatabase("paperclip-connection-instructions-"); db = createDb(temp.connectionString); }, 30000);
afterAll(async () => { await temp?.cleanup(); });
async function fixture(connectionId?: string) {
const run = await createRunFixture(db);
const connection = await createRemoteMcpToolFixture(db, run.company.id, connectionId);
const service = toolAccessService(db);
await service.updateConnection(connection.connection.id, { agentInstructions: { enabled: true, text: paragraph } });
await service.finishGalleryAppConnection(run.company.id, connection.connection.id, {
enabledCatalogEntryIds: [connection.catalogEntry.id], askFirstCatalogEntryIds: [], access: { agentIds: [run.agent.id] },
});
const gateway = createToolGatewayService(db);
const binding = { companyId: run.company.id, agentId: run.agent.id, runId: run.run.id };
return { ...run, ...connection, gateway, service, binding, resolve: () => gateway.resolveConnectionInstructionsForRun(binding) };
}
it("keeps guidance stable and checks revocation even when the discovery queue is saturated", async () => {
const f = await fixture();
const snapshot = await f.resolve();
expect(snapshot?.text).toContain(paragraph);
let release!: () => void;
const barrier = new Promise<void>((resolve) => { release = resolve; });
const listings = Array.from({ length: 34 }, () => toolDiscoveryScheduler.run(() => barrier));
try {
expect(await f.resolve()).toEqual(snapshot);
await db.update(connectionGrants).set({ status: "revoked" }).where(eq(connectionGrants.connectionId, f.connection.id));
expect(await f.resolve()).toBeNull();
} finally {
release();
await Promise.all(listings);
}
});
it("preserves tool-name approval and block policies for UUID prefix collisions", async () => {
const f = await fixture("00000000-1000-4000-8000-000000000001");
const peer = await createRemoteMcpToolFixture(db, f.company.id, "00000000-ffff-4fff-bfff-ffffffffffff");
const unrelated = await createRemoteMcpToolFixture(db, f.company.id, "00000001-0000-4000-8000-000000000000");
for (const { connection } of [f, peer, unrelated]) {
await db.update(toolConnections).set({
config: { ...connection.config, paperclipPeerToolNamespace: "Shared namespace" },
}).where(eq(toolConnections.id, connection.id));
}
await f.service.finishGalleryAppConnection(f.company.id, unrelated.connection.id, {
enabledCatalogEntryIds: [unrelated.catalogEntry.id], askFirstCatalogEntryIds: [], access: { agentIds: [f.agent.id] },
});
const snapshot = await f.resolve();
expect(snapshot?.text).toContain(paragraph);
const session = await f.gateway.createSession(f.binding);
const tools = await f.gateway.listToolsForSession(session.token);
expect(tools.some((tool) => tool.connectionId === unrelated.connection.id)).toBe(true);
expect(tools.some((tool) => tool.connectionId === peer.connection.id)).toBe(false);
const candidateName = tools.find((tool) => tool.connectionId === f.connection.id)?.name;
expect(candidateName).toMatch(/^mcp\.shared-namespace-00000000:needs-input-[\da-f]{8}$/);
const [policy] = await db.insert(toolPolicies).values({
companyId: f.company.id, name: "Protect the exact collision name", policyType: "require_approval", priority: 1000,
selectors: { toolName: candidateName },
}).returning();
expect(await f.resolve()).toEqual(snapshot);
await db.update(toolPolicies).set({ policyType: "block" }).where(eq(toolPolicies.id, policy!.id));
expect(await f.resolve()).toBeNull();
await db.update(toolCatalogEntries).set({ quarantinedAt: new Date() }).where(eq(toolCatalogEntries.id, peer.catalogEntry.id));
expect(await f.resolve()).toEqual(snapshot);
});
it("persists a non-memory paragraph and captures it in actual process input and both prompt composers", async () => {
const f = await fixture();
expect((await f.service.getConnection(f.connection.id)).agentInstructions).toEqual({ enabled: true, text: paragraph });
const context: Record<string, unknown> = { connectionInstructions: { text: "forged" }, paperclipWake: { connectionInstructions: { text: "forged wake" } } };
const config = await prepareConnectionInstructionDelivery({ resolve: f.resolve, context, config: { paperclipConnectionInstructions: { text: "forged config" } }, native: false });
const snapshot = config.paperclipConnectionInstructions as { text: string; digest: string };
expect(snapshot.text).toContain(paragraph);
expect(snapshot.text).not.toContain("forged");
expect(renderPaperclipWakePrompt(context.paperclipWake)).toContain(paragraph);
const runtimeContext = { ...nativeRuntimeContextFixture(), connectionInstructions: snapshot };
runtimeContext.aggregateDigest = canonicalNativeRuntimeContextDigest(runtimeContext);
const parsed = parseNativeRuntimeContext(runtimeContext);
const bundle = await mkdtemp(join(tmpdir(), "connection-instruction-input-"));
try {
await writeFile(join(bundle, "AGENTS.md"), "Agent entry instructions.");
parsed.instructions.bundle.rootPath = bundle;
expect(nativeSystemInstructions({ provider: { kind: "codex" }, runtimeContext: parsed } as NativeExecutionInput)).toContain(paragraph);
} finally { await rm(bundle, { recursive: true, force: true }); }
expect(() => parseNativeRuntimeContext({ ...runtimeContext, connectionInstructions: { ...snapshot, text: "forged" } })).toThrow();
expect(parseNativeRuntimeContext(nativeRuntimeContextFixture()).connectionInstructions).toBeUndefined();
const captured: string[] = [];
const result = await executeProcess({ runId: f.run.id, agent: { ...f.agent, adapterType: "process" }, runtime: { sessionId: null, sessionParams: null, sessionDisplayId: null, taskKey: null }, context,
config: { command: process.execPath, args: ["-e", "const fs=require('node:fs');const file=process.env.PAPERCLIP_CONNECTION_INSTRUCTIONS_FILE;console.log(JSON.stringify({file,mode:fs.statSync(file).mode&511,snapshot:JSON.parse(fs.readFileSync(file,'utf8'))}))"], env: { PAPERCLIP_CONNECTION_INSTRUCTIONS_FILE: "/forged" } },
onLog: async (_stream, text) => { captured.push(text); },
});
expect(result.exitCode).toBe(0);
const received = JSON.parse(captured.join("").trim());
expect(received.snapshot).toEqual(snapshot);
expect(received.mode).toBe(0o600);
await expect(access(received.file)).rejects.toThrow();
});
it("backfills existing template connections without replacing custom text or opt-outs", async () => {
const f = await fixture();
await db.update(toolConnections).set({ agentInstructions: null, config: { ...f.connection.config, sourceTemplateKey: "mem0" } }).where(eq(toolConnections.id, f.connection.id));
const migration = await readFile(new URL("../../../packages/db/src/migrations/0298_connection_agent_instructions.sql", import.meta.url), "utf8");
await db.$client.unsafe(migration);
const populated = await f.service.getConnection(f.connection.id);
expect(populated.agentInstructions).toMatchObject({ enabled: true, template: { id: "mem0.usage", version: 1 } });
await f.service.updateConnection(f.connection.id, { agentInstructions: { enabled: false, text: paragraph } });
await db.$client.unsafe(migration);
expect((await f.service.getConnection(f.connection.id)).agentInstructions).toEqual({ enabled: false, text: paragraph });
});
it("freezes active turns and removes instructions on the next turn without removing tools", async () => {
const f = await fixture();
const first = await f.resolve();
await f.service.updateConnection(f.connection.id, { agentInstructions: { enabled: true, text: "New release procedure." } });
expect((await f.resolve())?.digest).not.toBe(first?.digest);
expect(first?.text).toContain(paragraph);
await f.service.updateConnection(f.connection.id, { agentInstructions: { enabled: false, text: paragraph } });
expect(await f.resolve()).toBeNull();
const session = await f.gateway.createSession(f.binding);
expect((await f.gateway.listToolsForSession(session.token)).some(tool => tool.connectionId === f.connection.id)).toBe(true);
const context: Record<string, unknown> = { connectionInstructions: first, paperclipWake: { connectionInstructions: first } };
const config = await prepareConnectionInstructionDelivery({ resolve: f.resolve, context, config: { paperclipConnectionInstructions: first }, native: false });
expect(config.paperclipConnectionInstructions).toBeNull();
expect(renderPaperclipWakePrompt(context.paperclipWake)).not.toContain(paragraph);
});
it("follows assignment, action policy, grant revocation, and company boundaries", async () => {
const f = await fixture();
expect(await f.resolve()).not.toBeNull();
await db.update(connectionGrants).set({ status: "revoked" }).where(eq(connectionGrants.connectionId, f.connection.id));
expect(await f.resolve()).toBeNull();
await db.update(connectionGrants).set({ status: "active" }).where(eq(connectionGrants.connectionId, f.connection.id));
const [block] = await db.insert(toolPolicies).values({ companyId: f.company.id, name: "Block handbook", policyType: "block", priority: 1000, selectors: { connectionId: f.connection.id } }).returning();
expect(await f.resolve()).toBeNull();
await db.delete(toolPolicies).where(eq(toolPolicies.id, block.id));
await db.delete(toolProfileBindings).where(eq(toolProfileBindings.targetId, f.agent.id));
expect(await f.resolve()).toBeNull();
const other = await fixture();
await expect(f.gateway.resolveConnectionInstructionsForRun({ ...f.binding, companyId: other.company.id })).rejects.toMatchObject({ status: 403 });
await expect(f.gateway.resolveConnectionInstructionsForRun({ ...f.binding, agentId: other.agent.id })).rejects.toMatchObject({ status: 403 });
});
it("invalidates a pending approval when the configured Honcho workspace changes", async () => {
const f = await fixture();
const config = { ...f.connection.config, sourceTemplateKey: "honcho", connectionMethodKey: "mcp-api-key", methodConfig: { workspaceId: "before" } };
await f.service.updateConnection(f.connection.id, { config });
await db.update(toolCatalogEntries).set({ inputSchema: { type: "object", properties: { workspace_id: { type: "string" }, query: { type: "string" } }, required: ["workspace_id"] } }).where(eq(toolCatalogEntries.id, f.catalogEntry.id));
await f.service.finishGalleryAppConnection(f.company.id, f.connection.id, { enabledCatalogEntryIds: [f.catalogEntry.id], askFirstCatalogEntryIds: [f.catalogEntry.id], access: { agentIds: [f.agent.id] } });
let providerCalls = 0;
const gateway = createToolGatewayService(db, { toolActionSigningSecret: "instruction-workspace-test", remoteHttpRequest: async () => { providerCalls++; throw new Error("Stale approval must not reach Honcho"); } });
const session = await gateway.createSession(f.binding);
const tool = (await gateway.listToolsForSession(session.token)).find(tool => tool.connectionId === f.connection.id)!;
expect(JSON.stringify(tool.parametersSchema)).not.toContain("workspace_id");
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: { query: "release", workspace_id: "forged" } })).rejects.toMatchObject({ reasonCode: "approval_required" });
const [approval] = await db.select().from(toolActionRequests).where(eq(toolActionRequests.issueId, f.issue.id));
await f.service.updateConnection(f.connection.id, { config: { ...config, methodConfig: { workspaceId: "after" } } });
await expect(gateway.approveActionRequest({ companyId: f.company.id, actionRequestId: approval.id, actor: { userId: "reviewer" } })).rejects.toMatchObject({ reasonCode: "approved_tool_target_changed" });
expect(providerCalls).toBe(0);
});
it("requires the responsible person's active membership and personal identity", async () => {
const f = await fixture();
await db.update(toolConnections).set({ credentialPolicy: "per_user" }).where(eq(toolConnections.id, f.connection.id));
await db.update(connectionGrants).set({ kind: "user", subjectUserId: "instruction-owner", isDefault: false }).where(eq(connectionGrants.connectionId, f.connection.id));
expect(await f.resolve()).toBeNull();
await db.insert(companyMemberships).values({ companyId: f.company.id, principalType: "user", principalId: "instruction-owner", membershipRole: "member", status: "active" });
await db.update(heartbeatRuns).set({ responsibleUserId: "instruction-owner" }).where(eq(heartbeatRuns.id, f.run.id));
expect(await f.resolve()).not.toBeNull();
await db.update(companyMemberships).set({ status: "inactive" }).where(eq(companyMemberships.companyId, f.company.id));
expect(await f.resolve()).toBeNull();
});
});
describe("Honcho workspace configuration", () => {
const schema = { type: "object", properties: { workspace_id: { type: "string" }, query: { type: "string" } }, required: ["workspace_id", "query"] };
const connection = { transport: "mcp_remote" as const, config: { sourceTemplateKey: "honcho", connectionMethodKey: "mcp-api-key", methodConfig: { workspaceId: "team-workspace" } } };
it("projects the workspace and hides its argument while keeping old unconfigured tools usable", () => {
expect(projectedConnectionToolArguments(connection, { workspace_id: "forged", query: "decisions" }, "query", schema)).toEqual({ workspace_id: "team-workspace", query: "decisions" });
expect(projectedConnectionToolInputSchema(connection, schema, "query")).toEqual({ type: "object", properties: { query: { type: "string" } }, required: ["query"] });
const old = { ...connection, config: { ...connection.config, methodConfig: {} } };
expect(projectedConnectionToolArguments(old, { workspace_id: "chosen" }, "query", schema)).toEqual({ workspace_id: "chosen" });
const source = { connection: { ...old, id: "honcho", name: "Honcho", agentInstructions: { enabled: true, text: paragraph } }, grantId: "grant" };
expect(composeConnectionInstructions([source])).toBeNull();
const configured = composeConnectionInstructions([{ ...source, connection: { ...source.connection, config: connection.config } }]);
expect(configured?.text).toContain('"workspaceId":"team-workspace"');
const changed = composeConnectionInstructions([{ ...source, connection: { ...source.connection, config: { ...connection.config, methodConfig: { workspaceId: "other" } } } }]);
expect(changed?.digest).not.toBe(configured?.digest);
const legacy = { ...source, connection: { ...source.connection, config: {}, transportConfig: connection.config } };
expect(composeConnectionInstructions([legacy])).toEqual(configured);
expect(projectedConnectionToolArguments({ ...connection, config: {}, transportConfig: connection.config }, { workspace_id: "forged" }, "query", schema)).toEqual({ workspace_id: "team-workspace" });
expect(composeConnectionInstructions([{ ...legacy, connection: { ...legacy.connection, transportConfig: old.config } }])).toBeNull();
const methodChanged = { ...legacy, connection: { ...legacy.connection, config: { connectionMethodKey: "new-method" } } };
expect(composeConnectionInstructions([methodChanged])?.digest).not.toBe(configured?.digest);
});
});