mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 01:39:51 +01:00
fix(tool-access): do not store provider-removed actions in the connection profile (#22908)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Connected apps give agents actions. A person reviews and turns on each action on the Permissions page. > - #22877 lets the review save when it contains an action that the provider no longer lists (status `removed`, or `disabled` with `quarantineReason: mcp_tool_removed`). > - But the save still wrote that action to the connection profile as an `include` entry. > - When the provider lists the action again, the catalog refresh sets the row back to `active`. The stored `include` then turns the action on for agents with no new review. > - The Permissions page does not send removed ids now, but a stale page or a direct API call can still send them. > - This pull request keeps provider-removed actions out of the profile entries and the Ask first policies when the review is saved. > - The benefit is that an action that comes back from the provider is not offered to agents until a person turns it on again. ## Linked Issues or Issue Description Refs #22877 **What happened?** `finishGalleryAppConnection` accepted a provider-removed action in `enabledCatalogEntryIds` or `askFirstCatalogEntryIds` and stored it as an `include` profile entry (or an Ask first policy). When the provider listed the action again, the refresh set the row to `active` and agents got the action with no new review. **What did you expect to happen?** The save accepts the removed action, but does not store it. When the provider lists the action again, the action is `active` in the catalog but it is off for agents until a person turns it on. **Steps to reproduce** 1. Connect Google Workspace and save a review. 2. Make Google stop listing `gmail__list_threads`. Refresh the catalog. The row becomes `disabled` with `mcp_tool_removed`. 3. Save the review again with that row id in `enabledCatalogEntryIds`. 4. Make Google list the action again. Refresh the catalog. 5. Before this fix, the agent tool list contains `gmail__list_threads`. ## What Changed - `server/src/services/tool-access.ts`: `finishGalleryAppConnection` filters the enabled rows and the Ask first rows with `isCatalogEntryOffered` before it builds the profile entries and the Ask first policies. The validation is the same. A blocked action still gives "Disabled actions cannot be enabled". - `server/src/services/tool-access.ts`: when `preserveExistingAccess` is true (OAuth completion and task setup use it), the save no longer copies back a prior `include` entry whose catalog row is provider-removed. A prior `exclude` entry is kept, so a saved exclusion still blocks the action if it comes back. - `server/src/__tests__/google-workspace-mcp-transport.test.ts`: the test "saves the review when Google no longer offers actions that were on…" now makes Google list the two removed actions again. It also saves one removed action as Ask first and checks that no enabled `require_approval` policy is made for it. It checks that the rows are `active` and that the agent tool list does not contain them. - New test "does not keep a removed action on when a save keeps the existing access": a save with `preserveExistingAccess: true` does not keep the old `include` for a removed action, and keeps an `exclude` for a removed action. What happens to a removed action that the provider lists again: the refresh sets the row to `active` with no profile entry. The action is off. A person must turn it on again on the Permissions page. It is not held as `quarantined`, because the definition was already reviewed; only the on/off choice is reset to off. This applies to every connection kind that saves through `finishGalleryAppConnection`. Remote MCP aggregators (Zapier, Arcade, Composio, Executor) also stop storing a removed action when a review is saved. This is the safe result for them too: an aggregator can list a different tool under a reused name, so a returning action must not turn on by itself. Their existing tests (`remote-mcp-connectors.test.ts`, `tool-access-service.test.ts`) pass with no change. Scope: this PR changes what a save stores. It does not change the catalog refresh or a reconnect that keeps the profile unchanged (`mcpPreserveAccess`). An `include` that was saved before the provider removed the action, with no later save, stays in the profile as before. A follow-up can decide if a refresh should drop it. ## Verification - Red: with only the test change, the new assertion fails: `expected [ 'docs__read_doc', …(8) ] to not include 'gmail__list_threads'`. - Red: without the preserve-path filter, the preserve test fails (`gmail__list_threads` is offered again). With the filter but without the `include` check, the exclusion assertion fails (`expected [] to deeply equal [ 'exclude' ]`). Without the Ask first filter, the policy assertion fails. - Green: with the fix, `google-workspace-mcp-transport.test.ts` and `tool-access-service.test.ts` pass (547 tests). - `remote-mcp-connectors.test.ts`, `tool-profile-review-service.test.ts`, `app-permission-save.test.ts`, `tool-connection-removal.test.ts`, `tool-gateway.test.ts` pass. - `pnpm --filter @paperclipai/server typecheck` and `pnpm verify` run locally. CI covers the full suite. ## Risks - Low risk. A person who saved a removed action as on must turn it on again if the provider lists it again. This is the intended result. - A prior Ask first policy for a removed action can stay on the preserve path (`disableStale` is false there). It only restricts an action. It cannot turn an action on. - No schema change. No migration. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with tool use 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 - [ ] 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>
This commit is contained in:
1 parent
007d1c2ef1
commit
371ece9375
2 files changed
+70
-7
No files matched your search
@@ -4,7 +4,7 @@ import { and, eq } from "drizzle-orm";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
agents, companies, companyMemberships, createDb, heartbeatRuns, issues, issueThreadInteractions,
|
||||
toolAccessAuditEvents, toolActionRequests, toolCallEvents, toolCatalogEntries, toolInvocations,
|
||||
toolAccessAuditEvents, toolActionRequests, toolCallEvents, toolCatalogEntries, toolInvocations, toolPolicies, toolProfileEntries,
|
||||
} from "@paperclipai/db";
|
||||
import {
|
||||
GOOGLE_WORKSPACE_DEFAULT_SELECTION, GOOGLE_WORKSPACE_TOOL_TRANSPORT, googleWorkspaceServiceToolNames,
|
||||
@@ -596,16 +596,19 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
expect(removed.every((row) => row.status === "disabled")).toBe(true);
|
||||
const held = rows.filter((row) => row.status === "quarantined");
|
||||
expect(held.length).toBeGreaterThan(0);
|
||||
const choices = (enabled: string[]) => f.service.finishGalleryAppConnection(f.company.id, f.connectionId, {
|
||||
enabledCatalogEntryIds: enabled, askFirstCatalogEntryIds: [],
|
||||
const choices = (enabled: string[], askFirst: string[] = []) => f.service.finishGalleryAppConnection(f.company.id, f.connectionId, {
|
||||
enabledCatalogEntryIds: enabled, askFirstCatalogEntryIds: askFirst,
|
||||
reviewedCatalogEntryIds: [...held, ...removed].map((row) => row.id), access: { agentIds: [f.agent.id] },
|
||||
}, f.actor);
|
||||
|
||||
await expect(choices([...held, blocked!].map((row) => row.id)))
|
||||
.rejects.toMatchObject({ status: 400, message: "Disabled actions cannot be enabled" });
|
||||
|
||||
await choices([...held, ...removed].map((row) => row.id));
|
||||
await choices([...held, removed[0]!].map((row) => row.id), [removed[1]!.id]);
|
||||
const after = await byName(f.connectionId);
|
||||
const askFirstPolicies = (await db.select().from(toolPolicies).where(eq(toolPolicies.companyId, f.company.id)))
|
||||
.filter((policy) => policy.enabled && policy.policyType === "require_approval");
|
||||
expect(askFirstPolicies.map((policy) => (policy.selectors as { catalogEntryId?: string }).catalogEntryId)).not.toContain(removed[1]!.id);
|
||||
expect(after.get("gmail__list_threads")).toMatchObject({ status: "disabled", quarantineReason: "mcp_tool_removed" });
|
||||
expect(after.get("gmail__search_messages")).toMatchObject({ status: "disabled", quarantineReason: "mcp_tool_removed" });
|
||||
const offered = (await f.listed()).map((tool) => tool.upstreamToolName);
|
||||
@@ -613,6 +616,55 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
expect(offered).not.toContain("gmail__list_threads");
|
||||
expect(offered).not.toContain("gmail__search_messages");
|
||||
expect(offered).not.toContain("gmail__get_draft");
|
||||
|
||||
forgetMcpHttpSessions(f.connectionId);
|
||||
await f.service.refreshCatalog(f.connectionId, f.actor);
|
||||
const relisted = await byName(f.connectionId);
|
||||
expect(relisted.get("gmail__list_threads")?.status).toBe("active");
|
||||
expect(relisted.get("gmail__search_messages")?.status).toBe("active");
|
||||
const offeredAgain = (await f.listed()).map((tool) => tool.upstreamToolName);
|
||||
expect(offeredAgain).toContain("gmail__get_message");
|
||||
expect(offeredAgain).not.toContain("gmail__list_threads");
|
||||
expect(offeredAgain).not.toContain("gmail__search_messages");
|
||||
});
|
||||
|
||||
it("does not keep a removed action on when a save keeps the existing access", async () => {
|
||||
const f = await savedOnRest();
|
||||
vi.mocked(restDispatch.googleWorkspaceRestShippedSchemaHashes).mockReturnValue([]);
|
||||
UNLISTED.add("list_threads");
|
||||
try {
|
||||
await backOnGoogle(f);
|
||||
} finally {
|
||||
vi.mocked(restDispatch.googleWorkspaceRestShippedSchemaHashes).mockRestore();
|
||||
UNLISTED.clear();
|
||||
}
|
||||
const rows = await savedRows(f.connectionId);
|
||||
const removed = rows.filter((row) => row.quarantineReason === "mcp_tool_removed");
|
||||
expect(names(removed)).toEqual(["gmail__list_threads"]);
|
||||
const held = rows.filter((row) => row.status === "quarantined");
|
||||
const [draftRow] = rows.filter((row) => row.toolName === "gmail__get_draft");
|
||||
await db.update(toolCatalogEntries).set({ status: "disabled", quarantineReason: "mcp_tool_removed" }).where(eq(toolCatalogEntries.id, draftRow!.id));
|
||||
const excluded = await db.update(toolProfileEntries).set({ effect: "exclude" })
|
||||
.where(eq(toolProfileEntries.catalogEntryId, draftRow!.id)).returning();
|
||||
expect(excluded).toHaveLength(1);
|
||||
await f.service.finishGalleryAppConnection(f.company.id, f.connectionId, {
|
||||
enabledCatalogEntryIds: held.map((row) => row.id), askFirstCatalogEntryIds: [removed[0]!.id],
|
||||
reviewedCatalogEntryIds: [...held, ...removed].map((row) => row.id), access: { agentIds: [f.agent.id] },
|
||||
preserveExistingAccess: true,
|
||||
}, f.actor);
|
||||
const kept = await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.profileId, excluded[0]!.profileId));
|
||||
expect(kept.filter((entry) => entry.catalogEntryId === draftRow!.id).map((entry) => entry.effect)).toEqual(["exclude"]);
|
||||
expect(kept.some((entry) => entry.catalogEntryId === removed[0]!.id)).toBe(false);
|
||||
const askFirstPolicies = (await db.select().from(toolPolicies).where(eq(toolPolicies.companyId, f.company.id)))
|
||||
.filter((policy) => policy.enabled && policy.policyType === "require_approval");
|
||||
expect(askFirstPolicies.map((policy) => (policy.selectors as { catalogEntryId?: string }).catalogEntryId)).not.toContain(removed[0]!.id);
|
||||
|
||||
forgetMcpHttpSessions(f.connectionId);
|
||||
await f.service.refreshCatalog(f.connectionId, f.actor);
|
||||
expect((await byName(f.connectionId)).get("gmail__list_threads")?.status).toBe("active");
|
||||
const offered = (await f.listed()).map((tool) => tool.upstreamToolName);
|
||||
expect(offered).toContain("gmail__get_message");
|
||||
expect(offered).not.toContain("gmail__list_threads");
|
||||
});
|
||||
|
||||
it("keeps an action that a switch before this fix held for review held, because the hold replaced the saved REST definition", async () => {
|
||||
|
||||
@@ -99,7 +99,7 @@ import { defaultConnectionAgentInstructions, paperclipPeerAgentInstructions, pap
|
||||
import { isBrowserUseConnection } from "./browser-use-client.js";
|
||||
|
||||
import { COGNEE_STDIO_TEMPLATE } from "./cognee-connection.js";
|
||||
import { isCatalogEntryBlocked, isCatalogEntryRemovedByProvider, isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared";
|
||||
import { isCatalogEntryBlocked, isCatalogEntryOffered, isCatalogEntryRemovedByProvider, isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared";
|
||||
|
||||
import { githubBotRequest } from "./chat-github-client.js";
|
||||
import { syncConnectionCredentialBindings } from "./connection-credential-bindings.js";
|
||||
@@ -3438,7 +3438,7 @@ export function toolAccessService(
|
||||
if (input.access !== "all_agents")
|
||||
await assertAgentsInCompany(companyId, input.access.agentIds);
|
||||
|
||||
const entries: CreateToolProfileEntryForProfile[] = enabledRows.map(
|
||||
const entries: CreateToolProfileEntryForProfile[] = enabledRows.filter(isCatalogEntryOffered).map(
|
||||
(entry) => ({
|
||||
selectorType: "catalog_entry",
|
||||
effect: "include",
|
||||
@@ -3540,8 +3540,19 @@ export function toolAccessService(
|
||||
priority: prior.priority,
|
||||
metadata: prior.metadata,
|
||||
});
|
||||
const priorCatalogEntryIds = [...new Set(priorEntries.flatMap((prior) => prior.catalogEntryId ? [prior.catalogEntryId] : []))];
|
||||
const removedPriorCatalogEntryIds = new Set(
|
||||
(priorCatalogEntryIds.length > 0
|
||||
? await tx
|
||||
.select({ id: toolCatalogEntries.id, status: toolCatalogEntries.status, quarantineReason: toolCatalogEntries.quarantineReason })
|
||||
.from(toolCatalogEntries)
|
||||
.where(and(eq(toolCatalogEntries.companyId, companyId), inArray(toolCatalogEntries.id, priorCatalogEntryIds)))
|
||||
: []
|
||||
).filter(isCatalogEntryRemovedByProvider).map((entry) => entry.id),
|
||||
);
|
||||
for (const prior of priorEntries)
|
||||
if (
|
||||
!(prior.effect === "include" && prior.catalogEntryId && removedPriorCatalogEntryIds.has(prior.catalogEntryId)) &&
|
||||
!entries.some(
|
||||
(entry) =>
|
||||
entry.catalogEntryId &&
|
||||
@@ -3729,7 +3740,7 @@ export function toolAccessService(
|
||||
{
|
||||
companyId,
|
||||
connection,
|
||||
askFirstEntries: askFirstRows,
|
||||
askFirstEntries: askFirstRows.filter(isCatalogEntryOffered),
|
||||
disableStale: !input.preserveExistingAccess,
|
||||
actor,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user