feat(apps): add connection grants and delegated identities (#12341)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - External tools need explicit identity and access boundaries.
> - Shared connection credentials cannot represent every user-scoped use
case.
> - Grants must stay company-scoped and support safe delegation.
> - This pull request adds connection grants, identity rules, and their
database contract.
> - The benefit is durable control over which identity an agent may use.

## Linked Issues or Issue Description

Refs #11965

This is stack 3 of 11. It depends on stack 2 and replaces another
reviewable part of #11965.

## What Changed

- Add company and user connection grants.
- Add delegated identity and membership rules.
- Synchronize database, shared, server, and UI contracts.
- Register the grant-member replacement route in the OpenAPI surface in
the same layer that mounts it.
- Add migration 0231 with replay-safe guards and coverage.

## Verification

- `pnpm -r typecheck`
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/tool-access-service.test.ts`
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/openapi-routes.test.ts` (5 passed)
- `pnpm --filter @paperclipai/db check:migrations`
- `pnpm build`

## Risks

- Incorrect grant selection could expose the wrong credential scope.
- The service enforces company and subject boundaries before credential
use.
- Migration 0231 is generated, ordered after 0230, and safe to replay.

> I checked `ROADMAP.md`. This stack continues the existing app
connection work from #11965 and does not duplicate another planned item.

## Model Used

OpenAI Codex, GPT-5. The runtime model ID and context window were not
exposed. The model used reasoning, 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 linked a public issue or pull request with `Refs #`
- [x] I have not referenced internal or instance-local Paperclip issues
or links
- [x] My branch name describes the change 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 <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-08-29 12:08:33 -05:00
1 parent b51112798f
commit 20ccf3f476
82 files changed
+50274 -1331

No files matched your search

+4
View File
@@ -868,6 +868,8 @@ export {
connectionTokenScopeSchema,
connectionTokenSubjectSchema,
startConnectionAuthorizationSchema,
createConnectionGrantDelegationSchema,
replaceConnectionGrantMembersSchema,
createToolTrustRuleFromActionRequestSchema,
revokeToolTrustRuleSchema,
toolPolicyTestRequestSchema,
@@ -945,6 +947,8 @@ export {
type UnbindToolProfileBinding,
type UpsertToolCatalogEntry,
type ConnectionTokenRequestInput,
type CreateConnectionGrantDelegation,
type ReplaceConnectionGrantMembersInput,
type ToolPolicyTestRequestInput,
type CreateToolTrustRuleFromActionRequest,
type RevokeToolTrustRule,
+1
View File
@@ -1334,6 +1334,7 @@ export const requestItemVerdictsResultSchema = z.object({
const createIssueThreadInteractionCommon = {
resolverPolicy: issueThreadInteractionResolverPolicySchema.optional(),
addresseeAgentId: z.string().guid().nullable().optional(),
addresseeUserId: z.string().trim().min(1).nullable().optional(),
};
export const createIssueThreadInteractionSchema = z.discriminatedUnion("kind", [
+27 -2
View File
@@ -44,8 +44,13 @@ export const toolApplicationStatusSchema = z.enum(TOOL_APPLICATION_STATUSES);
export const toolConnectionTransportSchema = z.enum(["mcp_remote", "rest_api", "local_stdio"]);
export const toolConnectionAuthKindSchema = z.enum(["oauth", "api_key", "none"]);
export const toolConnectionOwnershipSchema = z.enum(["platform_shared", "platform_provisioned", "customer", "dcr"]);
export const connectionGrantKindSchema = z.enum(["workspace", "user"]);
export const connectionGrantKindSchema = z.enum(["organization", "user"]);
export const connectionGrantStatusSchema = z.enum(["active", "revoked", "expired", "needs_reauthorization"]);
export const createConnectionGrantDelegationSchema = z.object({
agentId: z.string().uuid(),
});
export type CreateConnectionGrantDelegation = z.infer<typeof createConnectionGrantDelegationSchema>;
export const toolConnectionCredentialPolicySchema = z.enum(["shared", "per_user", "per_user_with_fallback"]);
export const toolConnectionStatusSchema = z.enum(["draft", "active", "disabled", "archived"]);
export const toolConnectionInstallTargetTypeSchema = z.enum(["company", "agent"]);
export const toolCredentialPlacementSchema = z.enum(["header", "env"]);
@@ -155,6 +160,7 @@ export const createToolConnectionSchema = z.object({
name: z.string().trim().min(1).max(160),
transport: toolConnectionTransportSchema.optional(),
authKind: toolConnectionAuthKindSchema.default("none"),
credentialPolicy: toolConnectionCredentialPolicySchema.optional(),
ownership: toolConnectionOwnershipSchema.default("customer"),
status: toolConnectionStatusSchema.optional(),
connectionKind: toolConnectionKindSchema.default("managed"),
@@ -197,7 +203,7 @@ export const connectionGrantSchema = z.object({
updatedAt: z.coerce.date(),
}).superRefine((grant, ctx) => {
if ((grant.kind === "user") !== Boolean(grant.subjectUserId)) {
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["subjectUserId"], message: "User grants require a subject user; workspace grants must not have one" });
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["subjectUserId"], message: "User grants require a subject user; organization grants must not have one" });
}
});
@@ -210,6 +216,18 @@ export const putToolConnectionInstallsSchema = z.object({
export type PutToolConnectionInstalls = z.infer<typeof putToolConnectionInstallsSchema>;
/**
* Audience replacement for an organization grant (PAP-17835). An empty array is
* the canonical encoding of "all organization members" — the UI never exposes
* "empty list" as the mental model, so the wire format carries the emptiness and
* the copy layer translates it.
*/
export const replaceConnectionGrantMembersSchema = z.object({
memberUserIds: z.array(z.string().trim().min(1).max(500)).max(1000),
}).strict();
export type ReplaceConnectionGrantMembersInput = z.infer<typeof replaceConnectionGrantMembersSchema>;
export const connectionTokenIssuancePathSchema = z.enum(CONNECTION_TOKEN_ISSUANCE_PATHS);
export const connectionTokenScopeSchema = z.union([
@@ -342,6 +360,13 @@ export const connectToolAppSchema = z.object({
applicationId: z.string().guid().optional(),
authMode: genericMcpAuthModeSchema.optional(),
oauthClient: genericMcpOAuthClientSchema.optional(),
/**
* Which identity this credential becomes (PAP-17835). `user` means "Just me":
* the credential is committed to the caller's own personal grant and never to
* the connection row's shared secret refs or the default organization grant.
* Omitted keeps the historical shared-credential behaviour.
*/
grantKind: connectionGrantKindSchema.optional(),
}).superRefine((value, ctx) => {
if (value.configValues) rejectSensitiveConfigKeys(value.configValues, ctx, ["configValues"]);
if (value.credentialValues) rejectUnsafeHeaderCredentials(value.credentialValues, ctx, ["credentialValues"]);