Files
PaperClipAI/server/src/services/tool-access
DottaandPaperclip 0f52d90993 fix(github): detect writes outside GitHub App installations and stop advising Refresh access (#22978)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Agents use managed GitHub App connections through the tool gateway
to open issues, post comments, and perform approved writes on GitHub
repositories.
> - A GitHub App user-to-server token can write only to repositories
inside accounts where the Paperclip GitHub App is installed; even on a
public third-party repository, GitHub rejects writes with `403 Resource
not accessible by integration`.
> - Previously, Paperclip did not check the App's installed accounts
before posting an approval card, mapped every `403 Resource not
accessible by integration` to `github_access_denied`, and told both the
user ("Manage GitHub access") and the agent ("select Refresh access") to
repair access through a flow that cannot reach third-party
organizations.
> - This wastes human approvals on doomed writes and sends operators
into a dead-end permission refresh loop.
> - This pull request adds a pre-check before posting approval cards for
managed GitHub App writes outside installed accounts, distinguishes
`github_repo_outside_app_installations` and
`github_app_installation_suspended` from `github_access_denied` across
server classification, MCP routes, and continuation delivery, and
renders accurate UI guidance without a misleading "Manage GitHub access"
link for external repositories.
> - The benefit is that unreachable external-org writes fail immediately
with actionable alternatives (post directly on GitHub, use a PAT
connection, or have an org admin install the App and refresh access),
suspended installations get explicit unsuspend guidance, and missing
repository grants on installed organizations keep the existing "Manage
GitHub access" and "Refresh access" repair flow.

## Linked Issues or Issue Description

No public issue exists. Refs #19315, #19655, #20141.

**What happened?**
When an agent requested an approval-gated GitHub write targeting a
public repository owned by an external organization where the Paperclip
GitHub App was not installed, Paperclip posted a normal approval card.
After the user approved it, GitHub returned `403 Resource not accessible
by integration`, and both the failed approval card and agent
continuation instructions advised clicking "Manage GitHub access" and
"Refresh access", which cannot grant write access to a third-party
organization.

**Expected behavior**
Paperclip should check managed GitHub App write targets against the
grant's installed accounts before posting an approval card, fail
unreachable external-organization writes cleanly without wasting an
approval, preserve suspended installations separately with unsuspend
guidance, keep incomplete webhook-invalidated snapshots on
`github_access_denied` until refreshed, and present accurate
alternatives in both the UI card and agent continuation instructions.

**Steps to reproduce**
1. Connect GitHub via the managed GitHub App installed on one
organization (for example `acme`), with an approval policy on GitHub
writes.
2. Ask an agent to run `create_issue` or `add_issue_comment` targeting a
repository in an external organization where the App is not installed
(for example `external-org/public-repo`).
3. Previously, Paperclip posted an approval card, executed it upon
approval, received `403 Resource not accessible by integration`, and
showed "GitHub access needed" / "Manage GitHub access" plus "Refresh
access" instructions.

**Paperclip version or commit**
`2d065677959b` (`master`, merged cleanly with `331e1a9fd3`)

**Deployment mode**
Local / Paperclip Cloud managed GitHub App connection

## What Changed

- `server/src/services/tool-access/github-access-discovery.ts`,
`packages/db/src/schema/tool_access.ts`,
`packages/shared/src/types/tool-access.ts`,
`packages/shared/src/validators/tool-access.ts`: Preserved suspended
installation owner logins separately in optional
`suspendedInstallationOwnerLogins` so active `installationOwnerLogins`
stays unchanged while suspended orgs are distinguishable from
uninstalled orgs. Added `github_repo_outside_app_installations` to
`ToolAccessReasonCode` and updated
`tests/runner-e2e/source-sdk-public-inputs.json`.
- `server/src/services/tool-access/github-access-errors.ts`: Added
`github_repo_outside_app_installations`,
`github_app_installation_suspended`, `isGitHubAccessErrorCode`,
`extractGitHubTarget` (`owner`+`repo`, `repository: "owner/repo"`,
`organization`/`org`, `fork_repository` destination-only handling, and
GitHub API URL fallback), `suspendedManagedGitHubOwner`,
`findUnreachableManagedGitHubTarget`,
`githubOutsideAppInstallationsInstructions`,
`githubSuspendedInstallationInstructions`, and
`githubAccessErrorInstructions`. Webhook-invalidated/incomplete
snapshots (`bindingSync` set or `installationIds` ahead of
`installationOwnerLogins` with `repositories` cleared) skip pre-check
blocking and stay on `github_access_denied` on runtime `403` until
access is refreshed.
- `server/src/services/tool-access-policy.ts` and
`server/src/services/tool-gateway.ts`: Reused the already-resolved
remote tool context from `resolveGovernedToolCall` (avoiding redundant
connection/grant lookups for non-GitHub providers), routed unreachable
approval-requiring managed GitHub writes through the canonical policy
denial path with `{ claimIdempotencyKey: false }` (inserting
`idempotencyKey: null` atomically in the single `INSERT` so retries
after installing the App are never falsely replayed), and propagated
`github_repo_outside_app_installations` /
`github_app_installation_suspended` (HTTP 403) without echoing raw
provider text into trusted instructions.
- `server/src/routes/tool-gateway.ts`,
`server/src/services/tool-action-delivery.ts`,
`server/src/services/continuation-cause.ts`,
`server/src/services/agent-task-run-telemetry.ts`: Treated all GitHub
access error codes as MCP `tools/call` `isError: true` results, routed
continuation wakes through `githubAccessErrorInstructions`, and
registered `github_repo_outside_app_installations` and
`github_app_installation_suspended` as person-owned `missing_access`
failure codes.
- `ui/src/components/IssueThreadInteractionCard.tsx` and
`ui/storybook/stories/questions-and-approvals/connection-reviews.stories.tsx`:
Added dedicated alert states for `github_repo_outside_app_installations`
("Repository outside GitHub App access", omitting dead-end "Manage
GitHub access") and `github_app_installation_suspended` ("GitHub App
installation suspended", with unsuspend guidance and "Manage GitHub
access"), using existing design tokens only, while keeping "GitHub
access needed" for `github_access_denied`.
- `doc/connections/GITHUB.md`: Documented pre-check, suspended
installation, incomplete webhook snapshot, and error classification
behavior.
- Added unit, UI component, and embedded-Postgres integration tests in
`server/src/services/tool-access/github-access-errors.test.ts`,
`server/src/__tests__/github-grant-metadata.test.ts`,
`server/src/__tests__/tool-gateway.test.ts`,
`server/src/__tests__/tool-gateway-action-approvals.test.ts`, and
`ui/src/components/IssueThreadInteractionCard.test.tsx`.

## Verification

- `pnpm vitest run
server/src/services/tool-access/github-access-errors.test.ts
server/src/services/tool-access/github-default-owner.test.ts
server/src/__tests__/github-grant-metadata.test.ts
ui/src/components/IssueThreadInteractionCard.test.tsx`: 4 files, 140/140
passed.
- `pnpm vitest run server/src/__tests__/tool-gateway.test.ts -t
"GitHub"`: 13/13 passed (covers pre-check blocking with atomic
`idempotencyKey: null`, installed-org missing-repo
`github_access_denied`, direct + MCP HTTP route `isError: true`,
approved-card runtime 403 URL classification, post-install retry,
suspended installation `github_app_installation_suspended`, and
webhook-invalidated `bindingSync` snapshot pass-through).
- `pnpm vitest run
server/src/__tests__/tool-gateway-action-approvals.test.ts -t "tells the
agent how to recover from"`: 9/9 passed (including
`github_repo_outside_app_installations` and
`github_app_installation_suspended` continuation wakes).
- `pnpm --filter @paperclipai/shared typecheck && pnpm --filter
@paperclipai/db typecheck && pnpm --filter @paperclipai/server typecheck
&& pnpm --filter @paperclipai/ui typecheck`: passed (0 errors across all
7 server layers, shared, db migration safety/compatibility, and UI +
Storybook catalog).
- `pnpm check:token-gates`: all 4 gates clean.
- `node tests/runner-e2e/source-sdk-public-inputs.mjs
6714ad6b68 --check`: passed (`changed:
false`, 578 inputs).
- `node scripts/check-test-budget.mjs --files
server/src/services/tool-access/github-access-errors.test.ts
server/src/__tests__/github-grant-metadata.test.ts
server/src/__tests__/tool-gateway.test.ts
server/src/__tests__/tool-gateway-action-approvals.test.ts
ui/src/components/IssueThreadInteractionCard.test.tsx`: PASS (`113
nightly suites validated, 5 test file(s) checked`).
- PR CI on `6714ad6b68`: 81/81 executed checks passed (`0 failing, 0
pending, 0 cancelled, 6 skipped`), including `ci / verify`, `ci / e2e` +
all 16 E2E shards, all General tests, all 9 Serialized server shards,
all Verify Paperclip Runner lanes, `Build`, `Typecheck + Release
Registry`, `Superagent Security Scan`, `Socket Security`, `Contributor
trust`, `commitperclip`, `Snyk`, and `Greptile Review` (5/5, 0
unresolved threads).
- Not tested against a live GitHub App installation.

## Risks

- Low risk. No SQL database migration is required
(`suspendedInstallationOwnerLogins` is an optional property inside the
existing `connection_grants.provider_tenant` JSONB column, and
`errorCode` is already an open string field).
- Pre-check blocking only activates for approval-requiring non-read
writes on managed GitHub connections that have a complete, populated
grant snapshot (`login` and non-empty `installationOwnerLogins`, without
`bindingSync` invalidation). PAT connections, legacy/empty/incomplete
snapshots, installed organizations, suspended installations, the user's
own personal account, read tools, and upstream `fork_repository` source
repos retain their existing behavior.
- Pre-check denials insert `idempotencyKey: null` atomically so retrying
the same write after an org admin installs the App and access is
refreshed never hits a stale denial replay.

## Model Used

- Google Gemini (Antigravity agent) with tool use and local code/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
- [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-11-25 15:33:31 -06:00
..
…
…
…