Files
PaperClipAI/packages/shared
DottaandClaude Opus 5.5 d28becf0f5 fix(server): run person-opened gateway sessions as their opener for personal connection grants (#22643)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The tool gateway lets an agent run call connected apps through a
short-lived session
> - A person on the board can also open a gateway session for a running
agent
> - Such a session resolved credentials for the run's responsible
person, not for the person who opened it
> - A member could therefore use another member's personal connection
grant through that member's running agent
> - This pull request limits personal grants in a person-opened session
to the grant owner and requires permission to act on the agent
> - The benefit is that a personal connection is used only by its owner
or by the owner's own agent runs

## Linked Issues or Issue Description

Refs #22635

**What happened?**
`POST /api/tool-gateway/sessions` let any non-viewer member open a
session for any running agent in the company. Tool calls in that session
used the personal grant of the run's responsible person.

**What did you expect to happen?**
A person who opens a session can use a personal (user-owned) grant only
when they own it. The person needs permission to act on the agent.
Company-shared connections and agent-opened sessions work as before.

**Steps to reproduce**
Two members are in one company. Member A has a personal grant on a
connection that agent X uses. While a run of X is active, member B opens
a gateway session for that run and calls a tool on that connection. The
new route test covers this case.

**Impact**
Access-control hardening for personal connection grants.

## What Changed

- Add the nullable column `tool_gateway_sessions.created_by_user_id`
(migration `0502`, which also adds `tool_mcp_tasks.created_by_user_id`).
The route sets it when a person (not a local implicit board) opens a
session.
- Grant resolution for such a session acts as the person who opened it.
That person's own personal grant is used, or a connect card is addressed
to that person. Another member's personal grant is never selected, and
that member gets no card.
- The opener is recorded on the tool invocation (server-written
`policyExplanation.sessionOpenedByUserId`) and restored when an approved
action runs. An approved call from that session therefore uses the same
person's grant.
- A guard after grant resolution refuses a user-owned grant of another
person with `403` and `reasonCode: personal_grant_owner_required`, as
defense in depth.
- Saved-call replay is scoped to the opener: idempotency keys get an
opener suffix for such sessions, and approval replay matches only
invocations recorded with the same opener. The lookup of keys saved
before this change still runs: a saved call that can be attributed to
the same person replays, one attributed to another person does not, and
one that cannot be attributed returns the existing
`legacy_tool_idempotency_unverifiable` conflict instead of running the
write again.
- Long-running provider tasks (`tool_mcp_tasks`) record the opener in a
new `created_by_user_id` column (same migration) and restore it on each
background poll, so the task keeps the opener's grant and is attributed
to the opener.
- Managed GitHub identity selection uses the opener, in the first and
the fallback selection.
- A call attaches to an existing long-running provider task only when
the task uses the same grant the call resolves to. On a personal grant
it must also be the same person (an agent session matches only tasks
with no opener; the grant owner may also collect a legacy task with no
opener). On a shared grant any person may collect. The wake for a
finished task now carries the task id, so the agent's continuation
resolves the opener's grant and collects the result instead of calling
the provider again.
- `POST /api/tool-gateway/sessions` checks the existing `agent:wake`
permission for a board caller, the same check as invoking the agent.
- Agent-opened sessions do not set the column, so agent runs and the
recovery of interrupted runs do not change.
- Document the rule in `doc/MCP-ACCESS-GOVERNANCE.md`.
- I checked the sibling routes. Named gateway tokens minted by a person
need `tools:admin` and carry no responsible person. Board test calls and
MCP app view calls already run as the caller. Stored MCP task
continuations stay pinned to the grant they started with.

## Verification

- New route tests in `server/src/__tests__/tool-gateway.test.ts`:
- Three members, two with personal grants, on a run that belongs to the
first. A session opened by the second member uses only that member's
credential. The run owner's session and an agent-opened session use the
run owner's credential. A member with no grant gets `409
user_authorization_required`, the card is addressed to that member, and
the remote server gets no request. A reviewed call from the second
member's session, once approved, runs with that member's credential. The
same write from two people's sessions runs once for each person, and
only the same person's retry replays.
- A pre-upgrade saved write retried by the same person replays (or
conflicts when it cannot be attributed) and never reaches the provider
twice; another person's identical write runs.
- `mcp-tasks.test.ts`: a person-started task keeps the opener's grant
through a background poll; an agent retry never receives that task's
result and keeps its own task; the continuation after a wake collects
once with no new provider call; on a shared grant another person
collects the task, including a task started before openers were
recorded.
- `tool-gateway-service.test.ts`: an opener with their own managed
GitHub grant uses it; an opener without one is refused and the run
owner's grant is never used.
  - A viewer cannot open a session for the agent.
- Before the fix, the second member's session used the run owner's
credential, and the viewer got `201`. Both tests pass now.
- `npx vitest run src/__tests__/tool-gateway.test.ts
src/__tests__/connection-management.integration.test.ts
src/__tests__/mcp-tasks.test.ts`: with `tool-access-service.test.ts` and
the policy tests, 833 tests pass.
- `tsc --noEmit` for `server` passes.
- `pnpm verify` exits 2 because a global CI input (a doc) changed.
Required CI gives the broad coverage.

## Risks

- A person who opened a session for another member's run used that
member's personal grants before. Now the session uses the opener's own
grants. This was not an intended flow. Shared connections and agent runs
do not change.
- A person-opened session that repeats exactly the same side-effecting
call an agent session already made in the same run gets a `409` conflict
instead of a second write, because that earlier call has no recorded
opener. Use a new explicit idempotency key for an intentional repeat.
- The migration only adds nullable columns. Sessions and review requests
created before this change have no opener record and keep the previous
behavior until they expire (15 to 60 minutes).

- Follow-up, out of scope: in the experimental MCP Tasks path, a call
with a new explicit idempotency key and the same arguments still
attaches to the same person's open task under the same grant. That is
the existing de-duplication for every caller and does not depend on the
opener rule.

## 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>
2026-11-23 19:16:22 +00:00
..