Files
PaperClipAI/server/src/__tests__/plugin-connectors-circleback-parity.test.ts
T
DottaandClaude Opus 5.5 01451fae5b Let plugins add connector types to the Connectors catalog (#22004)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents use external services through connectors: a catalog of
connector types, connections (accounts) per type, and per-agent
installs, policies and audit in the tool gateway
> - Today only core can add a connector type. Every type is a JSON
definition compiled into `@paperclipai/shared`
> - Plugins can add agent tools, but those tools skip connections,
grants, per-agent installs and the Connectors page
> - This pull request lets a plugin add connector types with the same
definition schema, and makes them behave exactly like built-in
connectors
> - The benefit is that a connector can ship as a plugin, and an
operator controls it with the same Connectors page and the same access
rules

## Linked Issues or Issue Description

**Subsystem affected**
Plugins, Connectors (tool access), tool gateway

**Problem or motivation**
A plugin cannot add a connector type to the Connectors catalog. Plugin
tools (`tools` in the manifest) do not use connections, credentials per
account, agent installs, policies or the Connectors page.

**Proposed solution**
Add a manifest field `apps` (gated by a new `connectors.provide`
capability). Each entry uses the built-in app-definition schema. The
host namespaces the slug by plugin key and runs setup, OAuth, discovery,
grants, policies and audit through the existing code. This is the first
of two pull requests. The second adds a `plugin` transport so a plugin
worker can serve the tools itself.

**Activation rules (product requirements)**
1. Installing or enabling a plugin grants no tools. It only adds
connector types to the catalog.
2. Connecting an account grants no tools.
3. An agent gets the tools only from an agent install or a company
install of an active connection. Discovery and invocation both enforce
this.
4. A disabled or uninstalled plugin (or an upgrade that removes the app)
hides its connector types and connections from the Connectors APIs and
they reach no agent. Saved setup survives disable and comes back on
re-enable. Uninstall keeps rows archived for audit and revokes
credentials.


**Alternatives considered**
Keep plugin tools (`tools`) as the only plugin path. Rejected: they
bypass connections, per-account credentials, per-agent installs and the
Connectors page.

## What Changed

- `packages/shared`: new capability `connectors.provide`; manifest
`apps` validated with `appDefinitionSchema`. The validator rejects apps
without the capability, duplicate slugs, host-derived fields (`plugin`,
`aiConnectionRouter`, `experimentalFeatureKey`, `availability`), and
methods that need provider code in the host (REST adapters, chat
channels, AI runtimes).
- `packages/shared/src/plugin-apps.ts`: reversible namespaced slugs
(`plugin-<hex plugin key>-<slug>`), the catalog entry builder, and
`reviewedToolRisk`.
- `packages/shared/src/app-definitions.ts`: catalog lookups
(`getConnectableAppDefinition`, `getAppStoreDefinition`, ...) also
resolve the current plugin apps. The set is replaced on each read of
plugin state, so a built-in app is never shadowed.
- App definitions can declare `reviewedTools` (reviewed
read/write/destructive names and the risk for unlisted tools).
Circleback's reviewed risk moves from
`server/src/services/connectors/circleback/risk.ts` into
`circleback.json`, with the same classification. A copy of the
definition now classifies tools the same way.
- `validators/json-schema.ts`: `jsonSchemaSchema` moves to its own
module to break an import cycle between the plugin and app-definition
validators (still re-exported from `validators/plugin.ts`).
- `server/src/services/plugin-app-catalog.ts`: reads ready plugins from
the `plugins` table and answers "is this connection's connector
available". It does not trust in-memory lifecycle events, because
several lifecycle managers and processes can share one database.
- Gateway: connected-tool discovery and `resolveConnectionGrant` drop
connections from an unavailable plugin connector. Built-in connections
do not read the plugins table.
- Connectors APIs: the gallery lists ready plugin apps (with the same
Cloud connector profile filter as core); `listConnections` and
`listApplications` hide unavailable plugin connections; setup entry
points refresh plugin apps first.
- Plugin uninstall archives the plugin's connections through the
standard connection removal (grants revoked, owned credentials deleted,
rows kept).
- UI: the gallery response registers plugin apps for the setup screens;
`/apps/connect?source=plugin-…` loads the gallery first and shows an
unavailable state when the plugin is disabled.
- Docs: `doc/plugins/PLUGIN_SPEC.md` section 11A and the capability
list.

Design choices where the spec left room (picked to match "a plugin
connector behaves like a built-in connector"):
- Only `ready` plugins provide connectors. `error` and `upgrade_pending`
hide them, the same as the AI connection router plugins.
- `rest_api` is rejected for plugin apps in this PR. Core REST
connectors need reviewed adapter code in the host, so a definition alone
cannot work. The `plugin` transport in the next PR covers that case.
- Disabled plugin connections are hidden from list endpoints. Reading
one connection by id still works, so audit links keep resolving.

## Verification

- `packages/shared`: `npx vitest run src/plugin-apps.test.ts
src/validators/ src/app-definitions.test.ts` (416 passed). New tests:
manifest validation, slug namespacing, no shadowing of built-in apps,
reviewed risk.
- `server`: `npx vitest run
src/__tests__/plugin-connectors-circleback-parity.test.ts` (new). A
test-only plugin
(`server/src/__tests__/fixtures/plugin-connectors/circleback-parity/`)
ships an unmodified copy of `circleback.json`. The same journey runs
against built-in Circleback and the plugin copy with a recorded DCR
OAuth + MCP fixture, and must give identical catalog risk/status,
suggested defaults and per-agent tool sets. It covers each rule:
- Rule 1: the gallery adds only the namespaced type; no connections
exist; no agent tool changes.
  - Rule 2: two signed-in accounts, no agent sees a tool.
- Rule 3: an install for agent A gives only A the tools of that
connection; agent B is denied on invocation; each account calls the
provider with its own credential.
- Rule 4: disable hides the type, both connections and applications,
removes tools from discovery and invocation (404) and blocks new setup,
while built-in Circleback is unchanged; installs and grants are
byte-identical; re-enable restores tools with the same names; an upgrade
that removes the app hides it; uninstall archives both connections,
revokes grants and deletes owned secrets.
- Red/green: removing the gateway gate or the declared review makes the
test fail.
- An env-gated live check (`PAPERCLIP_CIRCLEBACK_LIVE=1`) starts the
real Circleback sign-in for both connectors and compares the requests.
It never runs in CI. I did not run it for this PR.
- Regression: `ai-connection-router`, `connector-tool-policy`,
`mcp-apps`, `plugin-lifecycle-restart`, `tool-access-service`,
`braintrust-connection`, `remote-mcp-connectors`,
`connection-intents-service`, `tool-gateway` (796 passed).
- `ui`: `app-connect-policy.test.ts`, `AppsConnect.test.tsx` (new plugin
cases) pass.
- `tsc --noEmit` for shared, server, ui (only pre-existing ungenerated
Storybook catalog errors), plugin SDK and CLI.
- `pnpm verify --explain` selects a broad plan because a doc under
`doc/plugins` changed; CI supplies that coverage.

## Risks

- No migration. Built-in connectors only gain one string check per
discovery; the plugins table is read only when a plugin connection is
present.
- Circleback's reviewed risk is now data. The behavior is the same, and
the existing policy tests and new tests cover it.
- Connection intents (agent connection requests in chat) still search
only built-in apps. Plugin connectors can be set up from the Connectors
page. This is a follow-up.

> 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

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file editing, test 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
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:51:39 +00:00

387 lines
27 KiB
TypeScript

/**
* Plugin-provided connectors: Circleback parity.
*
* A test-only plugin provides a copy of the built-in Circleback definition.
* The same journey runs against the built-in connector and the plugin copy,
* and both must give the same results. The provider is a recorded MCP and
* OAuth fixture; no request leaves the host. Each hard rule for plugin
* connectors is asserted where it applies:
*
* 1. Enabling the plugin only adds the connector type to the catalog.
* 2. Connecting and signing in an account grants no tools.
* 3. Only an agent (or company) install gives an agent the tools, in
* discovery and in invocation.
* 4. A disabled plugin hides its connector and connections and reaches no
* agent; re-enabling restores the saved setup unchanged. Uninstalling
* retires the connections and revokes their credentials.
*/
import { randomUUID } from "node:crypto";
import { readFileSync } from "node:fs";
import express from "express";
import request from "supertest";
import { and, eq } from "drizzle-orm";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import {
agents, companies, companyMemberships, companySecrets, connectionGrants, createDb, heartbeatRuns,
issues, plugins, toolConnectionInstalls, toolConnections,
} from "@paperclipai/db";
import {
APP_DEFINITIONS, pluginAppSlug, pluginManifestV1Schema, type AppDefinition, type PaperclipPluginManifestV1,
} from "@paperclipai/shared";
import { toolAccessService, type McpToolDescriptor } from "../services/tool-access.js";
import { createToolGatewayService } from "../services/tool-gateway.js";
import { retirePluginAppConnections } from "../services/plugin-app-catalog.js";
import { connectorKeyForConnection } from "../services/connector-telemetry.js";
import { toolAccessRoutes } from "../routes/tool-access.js";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
const fixtureDir = new URL("./fixtures/plugin-connectors/circleback-parity/", import.meta.url);
const pluginCircleback = JSON.parse(readFileSync(new URL("circleback.json", fixtureDir), "utf8")) as AppDefinition;
const fixtureManifest = pluginManifestV1Schema.parse({
...JSON.parse(readFileSync(new URL("manifest.json", fixtureDir), "utf8")),
apps: [pluginCircleback],
}) as PaperclipPluginManifestV1;
const PLUGIN_KEY = fixtureManifest.id;
const PLUGIN_SLUG = pluginAppSlug(PLUGIN_KEY, "circleback");
const actor = { actorType: "user" as const, actorId: "circleback-parity-admin" };
const redirectUri = "https://paperclip.fixture.test/api/tools/oauth/callback";
const origin = "https://circleback.ai";
const endpoint = `${origin}/api/mcp`;
// Recorded shape of Circleback's hosted MCP catalog, trimmed to the tools
// whose risk differs between the reviewed list and the generic classifier.
const tools: McpToolDescriptor[] = [
{ name: "SearchMeetings", inputSchema: { type: "object", properties: { query: { type: "string" } } } },
{ name: "ReadMeetings", inputSchema: { type: "object" } },
{ name: "ShareMeeting", inputSchema: { type: "object" } },
{ name: "ShareFolder", inputSchema: { type: "object" } },
{ name: "SubmitFeedback", inputSchema: { type: "object" } },
{ name: "DeleteFolder", inputSchema: { type: "object" } },
{ name: "UpdateActionItem", inputSchema: { type: "object" } },
];
/** One recorded Circleback provider: DCR OAuth plus the MCP endpoint. */
function circlebackProvider() {
const issued = new Set<string>();
const calls: Array<{ tool: string; token: string }> = [];
let tokenCount = 0;
const fetchFixture = vi.fn(async (url: string, init: RequestInit) => {
expect(new URL(url).origin).toBe(origin);
const headers = new Headers(init.headers);
const body = String(init.body ?? "");
if (url === endpoint && init.method === "POST") {
const token = headers.get("authorization")?.replace(/^Bearer /, "") ?? "";
if (!issued.has(token)) {
return new Response(null, { status: 401, headers: { "www-authenticate": `Bearer resource_metadata="${origin}/.well-known/oauth-protected-resource/api/mcp"` } });
}
const rpc = JSON.parse(body);
if (rpc.method === "initialize") return Response.json({ jsonrpc: "2.0", id: rpc.id,
result: { protocolVersion: "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "Circleback fixture", version: "1" } } });
if (rpc.method === "notifications/initialized") return new Response(null, { status: 202 });
if (rpc.method === "tools/list") return Response.json({ jsonrpc: "2.0", id: rpc.id, result: { tools } });
if (rpc.method === "tools/call") {
calls.push({ tool: rpc.params.name, token });
return Response.json({ jsonrpc: "2.0", id: rpc.id, result: { content: [{ type: "text", text: `Circleback ran ${rpc.params.name}` }] } });
}
}
if (url.includes("oauth-protected-resource")) return Response.json({ resource: endpoint, authorization_servers: [origin], scopes_supported: ["user", "user:write"] });
if (url === `${origin}/.well-known/oauth-authorization-server`) return Response.json({
issuer: origin, authorization_endpoint: `${origin}/oauth/authorize`, token_endpoint: `${origin}/oauth/token`,
registration_endpoint: `${origin}/oauth/register`, code_challenge_methods_supported: ["S256"],
token_endpoint_auth_methods_supported: ["none"], scopes_supported: ["user", "user:write"],
});
if (url === `${origin}/oauth/register`) return Response.json({ ...JSON.parse(body), client_id: "circleback-fixture-client" });
if (url === `${origin}/oauth/token`) {
const accessToken = `circleback-account-${++tokenCount}-${randomUUID()}`;
issued.add(accessToken);
return Response.json({ access_token: accessToken, refresh_token: `refresh-${accessToken}`, expires_in: 3600, token_type: "Bearer", scope: "user user:write" });
}
return new Response(null, { status: 404 });
});
return { fetchFixture, calls };
}
const support = await getEmbeddedPostgresTestSupport();
(support.supported ? describe : describe.skip)("plugin-provided connectors: Circleback parity", () => {
let db: ReturnType<typeof createDb>;
let temp: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
beforeAll(async () => {
temp = await startEmbeddedPostgresTestDatabase("paperclip-plugin-connectors-");
db = createDb(temp.connectionString);
}, 60_000);
afterAll(async () => { await temp?.cleanup(); });
it("ships an unmodified copy of the built-in Circleback definition", () => {
expect(pluginCircleback).toEqual(APP_DEFINITIONS.find(app => app.slug === "circleback"));
});
async function setup() {
const [company] = await db.insert(companies).values({ name: "Circleback parity", issuePrefix: `CP${randomUUID().slice(0, 6)}` }).returning();
await db.insert(companyMemberships).values({ companyId: company.id, principalType: "user", principalId: actor.actorId, status: "active", membershipRole: "admin" });
const [agentA, agentB] = await db.insert(agents).values([
{ companyId: company.id, name: "Meeting analyst", role: "engineer", adapterType: "process" },
{ companyId: company.id, name: "Other agent", role: "engineer", adapterType: "process" },
]).returning();
const provider = circlebackProvider();
const service = toolAccessService(db, { remoteHttpRequest: provider.fetchFixture, remoteHttpEndpointLookup: async () => [{ address: "8.8.8.8", family: 4 }] });
const gateway = createToolGatewayService(db, { remoteHttpRequest: provider.fetchFixture, toolActionSigningSecret: "circleback-parity-signing-key" });
const app = express();
app.use((req, _res, next) => { req.actor = { type: "board", userId: actor.actorId, source: "local_implicit", isInstanceAdmin: true }; next(); });
app.use("/api", toolAccessRoutes(db, { paperclipCloudConnector: null }));
const session = async (agentId: string) => {
const [issue] = await db.insert(issues).values({ companyId: company.id, title: "Review meetings", assigneeAgentId: agentId }).returning();
const [run] = await db.insert(heartbeatRuns).values({ companyId: company.id, agentId, issueId: issue.id, scopeKind: "issue",
invocationSource: "on_demand", status: "running", responsibleUserId: actor.actorId, contextSnapshot: { issueId: issue.id } }).returning();
return { ...(await gateway.createSession({ companyId: company.id, agentId, runId: run.id, issueId: issue.id })), runId: run.id };
};
const sessions = { a: await session(agentA.id), b: await session(agentB.id) };
const circlebackTools = async (token: string, galleryKey: string) => (await gateway.listToolsForSession(token))
.filter(tool => tool.providerType === "mcp_remote_http")
.filter(tool => {
const connection = connectionsBySlug.get(galleryKey);
return connection?.has(tool.connectionId ?? "");
});
const connectionsBySlug = new Map<string, Set<string>>();
const gallerySlugs = async () => (await request(app).get(`/api/companies/${company.id}/tools/gallery`)).body.apps.map((entry: { slug: string }) => entry.slug) as string[];
return { company, agentA, agentB, provider, service, gateway, app, sessions, circlebackTools, connectionsBySlug, gallerySlugs };
}
async function connectAccount(f: Awaited<ReturnType<typeof setup>>, galleryKey: string, { saveAgentSelection = true } = {}) {
const connected = await f.service.connectGalleryApp(f.company.id, { galleryKey, connectionMethodKey: "mcp-oauth" }, actor);
// The setup screen saves the operator's agent selection (none yet) before sign-in.
if (saveAgentSelection) await f.service.putConnectionInstalls(connected.connectionId, { installs: [] });
const start = await f.service.startOAuth(f.company.id, connected.connectionId, { redirectUri, actor });
const authorization = new URL(start.authorizationUrl);
expect(start.registrationSource).toBe("dcr");
expect(authorization.origin).toBe(origin);
const signedIn = await f.service.completeOAuthCallback({ state: authorization.searchParams.get("state")!, code: `code-${randomUUID()}`, iss: origin, redirectUri, actor });
const ids = f.connectionsBySlug.get(galleryKey) ?? new Set<string>();
ids.add(signedIn.connectionId);
f.connectionsBySlug.set(galleryKey, ids);
return signedIn;
}
async function journey(f: Awaited<ReturnType<typeof setup>>, galleryKey: string) {
const first = await connectAccount(f, galleryKey);
const second = await connectAccount(f, galleryKey);
const catalog = (await f.service.listCatalog(first.connectionId))
.map(entry => ({ tool: entry.toolName, risk: entry.riskLevel, status: entry.status }))
.sort((left, right) => left.tool.localeCompare(right.tool));
// Rule 2: two signed-in accounts, and still no agent has any tool.
const beforeInstall = {
a: (await f.circlebackTools(f.sessions.a.token, galleryKey)).length,
b: (await f.circlebackTools(f.sessions.b.token, galleryKey)).length,
};
// Rule 3: enable the first account for agent A only.
await f.service.finishGalleryAppConnection(f.company.id, first.connectionId, {
enabledCatalogEntryIds: first.catalog.map(entry => entry.id), askFirstCatalogEntryIds: [], access: { agentIds: [f.agentA.id] },
}, actor);
const toolsA = await f.circlebackTools(f.sessions.a.token, galleryKey);
const toolsB = await f.circlebackTools(f.sessions.b.token, galleryKey);
const search = toolsA.find(tool => tool.upstreamToolName === "SearchMeetings")!;
await f.gateway.executeTool({ sessionToken: f.sessions.a.token, tool: search.name, parameters: { query: "roadmap" }, idempotencyKey: randomUUID() });
const denied = await f.gateway.executeTool({ sessionToken: f.sessions.b.token, tool: search.name, parameters: { query: "roadmap" }, idempotencyKey: randomUUID() })
.then(() => "allowed", () => "denied");
return {
first, second, catalog, beforeInstall, denied, searchTool: search.name,
suggestedDefaults: first.suggestedDefaults,
visibleToA: toolsA.map(tool => ({ tool: tool.upstreamToolName, risk: tool.risk, connection: tool.connectionId === first.connectionId ? "first" : "other" }))
.sort((left, right) => String(left.tool).localeCompare(String(right.tool))),
visibleToB: toolsB.length,
};
}
it("runs the same journey with identical results for the built-in connector and the plugin copy", async () => {
const f = await setup();
// Rule 1: before the plugin exists only the built-in connector is listed.
expect(await f.gallerySlugs()).toContain("circleback");
expect(await f.gallerySlugs()).not.toContain(PLUGIN_SLUG);
const [plugin] = await db.insert(plugins).values({ pluginKey: PLUGIN_KEY, packageName: "@paperclipai/test-circleback-parity", version: fixtureManifest.version,
manifestJson: fixtureManifest, status: "ready" }).returning();
// Rule 1: enabling the plugin adds the namespaced connector type and nothing else.
const slugs = await f.gallerySlugs();
expect(slugs).toEqual(expect.arrayContaining(["circleback", PLUGIN_SLUG]));
// The board also gets the full definition, as it bundles built-in ones.
const gallery = await request(f.app).get(`/api/companies/${f.company.id}/tools/gallery`);
expect(gallery.body.pluginApps).toEqual([expect.objectContaining({ slug: PLUGIN_SLUG, plugin: { pluginKey: PLUGIN_KEY, appSlug: "circleback" },
methods: pluginCircleback.methods })]);
// Telemetry never names a plugin's connector.
expect(connectorKeyForConnection({ config: { sourceTemplateKey: PLUGIN_SLUG } })).toBe("custom");
expect(connectorKeyForConnection({ config: { sourceTemplateKey: "circleback" } })).toBe("circleback");
expect(await db.select().from(toolConnections).where(eq(toolConnections.companyId, f.company.id))).toHaveLength(0);
expect(await f.gateway.listToolsForSession(f.sessions.a.token)).toEqual(await f.gateway.listToolsForSession(f.sessions.b.token));
const core = await journey(f, "circleback");
const fromPlugin = await journey(f, PLUGIN_SLUG);
for (const result of [core, fromPlugin]) {
expect(result.beforeInstall).toEqual({ a: 0, b: 0 });
expect(result.visibleToB).toBe(0);
expect(result.denied).toBe("denied");
expect(result.visibleToA.every(tool => tool.connection === "first")).toBe(true);
expect(result.catalog).toEqual(expect.arrayContaining([
{ tool: "SearchMeetings", risk: "read", status: "active" },
{ tool: "ShareMeeting", risk: "write", status: "active" },
{ tool: "ShareFolder", risk: "write", status: "active" },
{ tool: "SubmitFeedback", risk: "write", status: "active" },
{ tool: "DeleteFolder", risk: "destructive", status: "active" },
]));
}
expect(fromPlugin.catalog).toEqual(core.catalog);
expect(fromPlugin.suggestedDefaults).toEqual(core.suggestedDefaults);
expect(fromPlugin.visibleToA).toEqual(core.visibleToA);
expect(fromPlugin.first.connection.config).toMatchObject({ sourceTemplateKey: PLUGIN_SLUG, url: endpoint, connectionMethodKey: "mcp-oauth" });
// Each account calls the provider with its own credential.
const tokensByTool = f.provider.calls.map(call => call.token);
expect(tokensByTool).toHaveLength(2);
expect(new Set(tokensByTool).size).toBe(2);
// Rule 2, without the setup screen: signing in a plugin account through
// the API alone never installs it for agents.
const apiOnly = await connectAccount(f, PLUGIN_SLUG, { saveAgentSelection: false });
expect(await db.select().from(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, apiOnly.connectionId))).toEqual([]);
expect((await f.gateway.listToolsForSession(f.sessions.b.token)).some(tool => tool.connectionId === apiOnly.connectionId)).toBe(false);
const pluginConnectionIds = [fromPlugin.first.connectionId, fromPlugin.second.connectionId, apiOnly.connectionId];
const installsBefore = await db.select().from(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, fromPlugin.first.connectionId));
const grantsBefore = await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, fromPlugin.first.connectionId));
// Rule 4: disabling the plugin hides its connector type and connections
// and removes the tools from discovery and invocation. Built-in Circleback
// is untouched.
await db.update(plugins).set({ status: "disabled" }).where(eq(plugins.id, plugin.id));
expect(await f.gallerySlugs()).not.toContain(PLUGIN_SLUG);
expect(await f.gallerySlugs()).toContain("circleback");
const listedConnections = (await f.service.listConnections(f.company.id)).map(connection => connection.id);
expect(listedConnections).toEqual(expect.arrayContaining([core.first.connectionId, core.second.connectionId]));
expect(listedConnections).not.toEqual(expect.arrayContaining([pluginConnectionIds[0]]));
expect(listedConnections.some(id => pluginConnectionIds.includes(id))).toBe(false);
const listedApplications = (await f.service.listApplications(f.company.id)).map(application => application.metadata?.sourceTemplateKey);
expect(listedApplications).toContain("circleback");
expect(listedApplications).not.toContain(PLUGIN_SLUG);
expect(await f.circlebackTools(f.sessions.a.token, PLUGIN_SLUG)).toEqual([]);
expect((await f.circlebackTools(f.sessions.a.token, "circleback")).map(tool => tool.upstreamToolName).sort())
.toEqual(core.visibleToA.map(tool => tool.tool));
await expect(f.gateway.executeTool({ sessionToken: f.sessions.a.token, tool: fromPlugin.searchTool, parameters: {}, idempotencyKey: randomUUID() }))
.rejects.toMatchObject({ status: 404 });
await expect(f.service.connectGalleryApp(f.company.id, { galleryKey: PLUGIN_SLUG, connectionMethodKey: "mcp-oauth" }, actor))
.rejects.toMatchObject({ status: 404 });
// Token exchange is refused too.
await expect(f.service.mintConnectionTokenForAgent({ connectionId: fromPlugin.first.connectionId, companyId: f.company.id,
agentId: f.agentA.id, runId: f.sessions.a.runId, body: {} })).rejects.toMatchObject({ status: 404 });
// Saved setup is untouched while disabled.
expect(await db.select().from(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, fromPlugin.first.connectionId))).toEqual(installsBefore);
expect(await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, fromPlugin.first.connectionId))).toEqual(grantsBefore);
// Rule 4: re-enabling restores the connector, both accounts and agent A's tools.
await db.update(plugins).set({ status: "ready" }).where(eq(plugins.id, plugin.id));
expect(await f.gallerySlugs()).toContain(PLUGIN_SLUG);
expect((await f.service.listConnections(f.company.id)).map(connection => connection.id)).toEqual(expect.arrayContaining(pluginConnectionIds));
const restored = await f.circlebackTools(f.sessions.a.token, PLUGIN_SLUG);
expect(restored.map(tool => tool.upstreamToolName).sort()).toEqual(fromPlugin.visibleToA.map(tool => tool.tool));
expect(restored.some(tool => tool.name === fromPlugin.searchTool)).toBe(true);
await f.gateway.executeTool({ sessionToken: f.sessions.a.token, tool: fromPlugin.searchTool, parameters: {}, idempotencyKey: randomUUID() });
expect(await f.circlebackTools(f.sessions.b.token, PLUGIN_SLUG)).toEqual([]);
// Rule 4: an upgrade that drops the app hides it the same way.
await db.update(plugins).set({ manifestJson: { ...fixtureManifest, apps: [] } }).where(eq(plugins.id, plugin.id));
expect(await f.gallerySlugs()).not.toContain(PLUGIN_SLUG);
expect(await f.circlebackTools(f.sessions.a.token, PLUGIN_SLUG)).toEqual([]);
await db.update(plugins).set({ manifestJson: fixtureManifest }).where(eq(plugins.id, plugin.id));
// Rule 4: uninstalling keeps the rows for audit, archived, and revokes
// their grants and connection-owned credentials.
const secretIdsBefore = (await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, fromPlugin.first.connectionId)))
.flatMap(grant => grant.credentialSecretRefs.map(ref => ref.secretId));
expect(secretIdsBefore.length).toBeGreaterThan(0);
await db.update(plugins).set({ status: "uninstalled" }).where(eq(plugins.id, plugin.id));
expect(await retirePluginAppConnections(db, PLUGIN_KEY)).toBe(3);
const retired = await db.select().from(toolConnections).where(and(eq(toolConnections.companyId, f.company.id)));
for (const row of retired) {
if (pluginConnectionIds.includes(row.id)) expect(row).toMatchObject({ status: "archived", enabled: false });
else expect(row.status).not.toBe("archived");
}
for (const id of pluginConnectionIds) {
const grants = await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, id));
expect(grants.length).toBeGreaterThan(0);
expect(grants.every(grant => grant.status === "revoked")).toBe(true);
}
for (const secretId of secretIdsBefore) {
expect(await db.select().from(companySecrets).where(eq(companySecrets.id, secretId))).toHaveLength(0);
}
expect((await f.circlebackTools(f.sessions.a.token, "circleback")).length).toBe(core.visibleToA.length);
});
it("archives a connection whose plugin was disabled while its setup ran", async () => {
const f = await setup();
const pluginKey = `${PLUGIN_KEY}-race`;
const slug = pluginAppSlug(pluginKey, "circleback");
await db.insert(plugins).values({ pluginKey, packageName: "@paperclipai/test-circleback-race", version: fixtureManifest.version,
manifestJson: { ...fixtureManifest, id: pluginKey }, status: "ready" });
const connected = await f.service.connectGalleryApp(f.company.id, { galleryKey: slug, connectionMethodKey: "mcp-oauth" }, actor);
await f.service.putConnectionInstalls(connected.connectionId, { installs: [] });
const start = await f.service.startOAuth(f.company.id, connected.connectionId, { redirectUri, actor });
await f.service.completeOAuthCallback({ state: new URL(start.authorizationUrl).searchParams.get("state")!, code: "race", iss: origin, redirectUri, actor });
// Disabled while an account signs in: the saved setup stays, hidden.
const paused = await f.service.connectGalleryApp(f.company.id, { galleryKey: slug, connectionMethodKey: "mcp-oauth", name: "Paused account" }, actor);
await f.service.putConnectionInstalls(paused.connectionId, { installs: [] });
const pausedStart = await f.service.startOAuth(f.company.id, paused.connectionId, { redirectUri, actor });
const beforeDisable = f.provider.fetchFixture.getMockImplementation()!;
f.provider.fetchFixture.mockImplementation(async (url: string, init: RequestInit) => {
if (url.endsWith("/oauth/token")) await db.update(plugins).set({ status: "disabled" }).where(eq(plugins.pluginKey, pluginKey));
return beforeDisable(url, init);
});
await expect(f.service.completeOAuthCallback({ state: new URL(pausedStart.authorizationUrl).searchParams.get("state")!, code: "paused", iss: origin, redirectUri, actor }))
.rejects.toMatchObject({ status: 404 });
f.provider.fetchFixture.mockImplementation(beforeDisable);
const [pausedRow] = await db.select().from(toolConnections).where(eq(toolConnections.id, paused.connectionId));
expect(pausedRow.status).not.toBe("archived");
await db.update(plugins).set({ status: "ready" }).where(eq(plugins.pluginKey, pluginKey));
expect((await f.service.listConnections(f.company.id)).map(connection => connection.id)).toContain(paused.connectionId);
await f.service.archiveConnection(paused.connectionId, f.company.id, actor);
// The plugin goes away while a second account signs in.
const second = await f.service.connectGalleryApp(f.company.id, { galleryKey: slug, connectionMethodKey: "mcp-oauth", name: "Second account" }, actor);
await f.service.putConnectionInstalls(second.connectionId, { installs: [] });
const secondStart = await f.service.startOAuth(f.company.id, second.connectionId, { redirectUri, actor });
const original = f.provider.fetchFixture.getMockImplementation()!;
f.provider.fetchFixture.mockImplementation(async (url: string, init: RequestInit) => {
if (url.endsWith("/oauth/token")) await db.update(plugins).set({ status: "uninstalled" }).where(eq(plugins.pluginKey, pluginKey));
return original(url, init);
});
await expect(f.service.completeOAuthCallback({ state: new URL(secondStart.authorizationUrl).searchParams.get("state")!, code: "race-2", iss: origin, redirectUri, actor }))
.rejects.toMatchObject({ status: 404 });
f.provider.fetchFixture.mockImplementation(original);
const [secondRow] = await db.select().from(toolConnections).where(eq(toolConnections.id, second.connectionId));
expect(secondRow).toMatchObject({ status: "archived", enabled: false });
expect((await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, second.connectionId))).every(grant => grant.status === "revoked")).toBe(true);
const rows = await db.select().from(toolConnections).where(eq(toolConnections.companyId, f.company.id));
expect(rows.filter(row => row.config.sourceTemplateKey === slug && row.status !== "archived").map(row => row.id)).toEqual([connected.connectionId]);
await db.delete(plugins).where(eq(plugins.pluginKey, pluginKey));
});
// Live check against the real Circleback service. It never runs in CI:
// set PAPERCLIP_CIRCLEBACK_LIVE=1 to run it. It registers a client with
// Circleback for both connectors and compares the sign-in requests, up to
// the point where a person would sign in.
it.skipIf(process.env.PAPERCLIP_CIRCLEBACK_LIVE !== "1")("starts the same real Circleback sign-in for both connectors", async () => {
const [company] = await db.insert(companies).values({ name: "Circleback live", issuePrefix: `CL${randomUUID().slice(0, 6)}` }).returning();
await db.insert(companyMemberships).values({ companyId: company.id, principalType: "user", principalId: actor.actorId, status: "active", membershipRole: "admin" });
const [row] = await db.insert(plugins).values({ pluginKey: `${PLUGIN_KEY}-live-${randomUUID().slice(0, 8)}`, packageName: "@paperclipai/test-circleback-parity",
version: fixtureManifest.version, manifestJson: fixtureManifest, status: "ready" }).returning();
const service = toolAccessService(db);
const signIn = async (galleryKey: string) => {
const connected = await service.connectGalleryApp(company.id, { galleryKey, connectionMethodKey: "mcp-oauth" }, actor);
const start = await service.startOAuth(company.id, connected.connectionId, { redirectUri, actor });
const url = new URL(start.authorizationUrl);
return { registrationSource: start.registrationSource, origin: url.origin, path: url.pathname,
scope: url.searchParams.get("scope"), resource: url.searchParams.get("resource"), method: url.searchParams.get("code_challenge_method") };
};
const core = await signIn("circleback");
expect(core.registrationSource).toBe("dcr");
expect(await signIn(pluginAppSlug(row!.pluginKey, "circleback"))).toEqual(core);
}, 60_000);
});