Files
PaperClipAI/server/src/services/issue-thread-interaction-resolution.test.ts
T
DottaandPaperclip 20ccf3f476 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>
2026-08-29 12:08:33 -05:00

182 lines
6.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
evaluateIssueThreadInteractionResolverAudience,
issueThreadInteractionAttentionAgentAllowed,
} from "./issue-thread-interaction-resolution.js";
const agent = { type: "agent", agentId: "agent-1", runId: "run-1" } as const;
function interaction(overrides: Record<string, unknown> = {}) {
return {
createdByAgentId: "agent-1",
createdByUserId: null,
sourceRunId: "run-1",
addresseeAgentId: null,
addresseeUserId: null,
effectiveResolverPolicy: "anyone",
...overrides,
};
}
describe("issue-thread interaction resolver audience", () => {
it.each([
"suggest_tasks",
"ask_user_questions",
"request_confirmation",
"request_checkbox_confirmation",
"request_item_verdicts",
])("allows the creator and creating run under anyone for %s", () => {
expect(evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction(),
})).toMatchObject({ allowed: true, effectiveResolverPolicy: "anyone", reason: "allow_anyone" });
});
it.each([
["creator agent", { createdByAgentId: "agent-1", sourceRunId: "run-other" }],
["creating run", { createdByAgentId: "agent-other", sourceRunId: "run-1" }],
])("excludes the %s only when not_creator is effective", (_name, creator) => {
const decision = evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction({ ...creator, effectiveResolverPolicy: "not_creator" }),
});
expect(decision).toMatchObject({ allowed: false, code: "interaction_creator_excluded" });
});
it("enforces an explicit addressee", () => {
const decision = evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction({ addresseeAgentId: "agent-2" }),
});
expect(decision).toMatchObject({ allowed: false, code: "interaction_addressee_mismatch" });
});
it("allows only the addressed user to resolve a user-addressed interaction", () => {
expect(evaluateIssueThreadInteractionResolverAudience({
actor: { type: "user", userId: "alice" },
interaction: interaction({ addresseeUserId: "alice", effectiveResolverPolicy: "human_only" }),
})).toMatchObject({ allowed: true, reason: "allow_addressee" });
expect(evaluateIssueThreadInteractionResolverAudience({
actor: { type: "user", userId: "bob" },
interaction: interaction({ addresseeUserId: "alice", effectiveResolverPolicy: "human_only" }),
})).toMatchObject({ allowed: false, code: "interaction_addressee_mismatch" });
});
it("requires run attribution for agents", () => {
const decision = evaluateIssueThreadInteractionResolverAudience({
actor: { type: "agent", agentId: "agent-1", runId: null },
interaction: interaction(),
});
expect(decision).toMatchObject({
allowed: false,
status: 422,
code: "interaction_run_attribution_required",
});
});
it("allows a server-derived restriction to narrow but never widen the audience", () => {
expect(evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction({ createdByAgentId: "agent-other", sourceRunId: "run-other" }),
additionalRestriction: "human_only",
})).toMatchObject({ allowed: false, code: "interaction_human_only" });
expect(evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction({ effectiveResolverPolicy: "human_only" }),
additionalRestriction: "anyone",
})).toMatchObject({ allowed: false, code: "interaction_human_only" });
});
it("intersects human-only and creator-separation restrictions", () => {
expect(evaluateIssueThreadInteractionResolverAudience({
actor: { type: "user", userId: "user-1" },
interaction: interaction({
createdByAgentId: null,
createdByUserId: "user-1",
effectiveResolverPolicy: "not_creator",
}),
additionalRestriction: "human_only",
})).toMatchObject({ allowed: false, code: "interaction_creator_excluded" });
expect(evaluateIssueThreadInteractionResolverAudience({
actor: { type: "user", userId: "user-1" },
interaction: interaction({
createdByAgentId: null,
createdByUserId: "user-1",
effectiveResolverPolicy: "human_only",
}),
additionalRestriction: "not_creator",
})).toMatchObject({ allowed: false, code: "interaction_creator_excluded" });
});
it("excludes the review requester when a review card has null creator columns", () => {
const restriction = {
policy: "not_creator",
excludedActor: { type: "user", id: "review-requester" },
source: "issue_review",
} as const;
expect(evaluateIssueThreadInteractionResolverAudience({
actor: { type: "user", userId: "review-requester" },
interaction: interaction({
createdByAgentId: null,
createdByUserId: null,
sourceRunId: null,
}),
additionalRestriction: restriction,
})).toMatchObject({
allowed: false,
code: "review_policy_denied",
details: {
policy: "not_creator",
allowedActor: "writer_other_than_review_requester",
},
});
expect(evaluateIssueThreadInteractionResolverAudience({
actor: { type: "user", userId: "peer-reviewer" },
interaction: interaction({
createdByAgentId: null,
createdByUserId: null,
sourceRunId: null,
}),
additionalRestriction: restriction,
})).toMatchObject({ allowed: true, effectiveResolverPolicy: "not_creator" });
});
it("keeps governed actions independently human-only", () => {
const decision = evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction(),
governedAction: true,
});
expect(decision).toMatchObject({ allowed: false, code: "interaction_governed_action_denied" });
});
it("preserves legacy inherited board_or_agents creator separation", () => {
const decision = evaluateIssueThreadInteractionResolverAudience({
actor: agent,
interaction: interaction({
effectiveResolverPolicy: "board_or_agents",
resolverPolicyProvenance: "legacy_inherited_restriction",
}),
});
expect(decision).toMatchObject({ allowed: false, code: "interaction_creator_excluded" });
});
it("derives agent attention ownership from the same effective audience", () => {
expect(issueThreadInteractionAttentionAgentAllowed({
agentId: "agent-1",
interaction: interaction(),
})).toBe(true);
expect(issueThreadInteractionAttentionAgentAllowed({
agentId: "agent-1",
interaction: interaction({ effectiveResolverPolicy: "not_creator" }),
})).toBe(false);
expect(issueThreadInteractionAttentionAgentAllowed({
agentId: "agent-2",
interaction: interaction({ effectiveResolverPolicy: "human_only" }),
})).toBe(false);
});
});