Files
PaperClipAI/server/src/__tests__/chat-channels/github-setup-and-access.integration.test.ts
T
DottaandPaperclip bc526683c2 fix(github-chat): fix initial repository import and verify UX states (#23760)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The GitHub Code Review Bot setup wizard registers a GitHub App,
discovers its installation repositories, imports initial repository
selections, and verifies access before activation.
> - When a user or agent saves review configuration on a draft before
installing the GitHub App, the wizard skipped initial repository import
because it required `saved.revision === 0`, leaving enabled GitHub
repositories disabled in Paperclip and showing a misleading "Enable at
least one repository from the App installation." error.
> - The wizard also closed initial repository import prematurely when
GitHub returned zero repositories, failed to refresh repositories on
retry, treated removed installations (HTTP 404) as generic 422 failures
and transient repository-sync errors as empty installations, omitted the
direct GitHub App installation link on verification errors, and kept
showing "Checking GitHub access…" while idle on a failed check.
> - This pull request decouples initial repository import from review
configuration revision, recovers verifying endpoints stuck with a
pending initial import, maps removed installations and transient
repository sync errors to structured verification check codes with
validated GitHub URLs, and updates the setup verify screen to show clear
error states, accurate paired links/labels, and active retry feedback.
> - The benefit is that pre-configured GitHub review bots connect
cleanly when the App is installed, and operators get accurate error
diagnostics, direct GitHub installation and Access settings links, and
clear retry feedback when setup checks fail.

## Linked Issues or Issue Description

**What happened?**

During GitHub Code Review Bot setup
(`/apps/chat/connect?provider=github&purpose=chat&resume=...`), the
verify step failed with "Enable at least one repository from the App
installation." even when one repository was already enabled on the
GitHub App installation. The verify screen also kept displaying
"Checking GitHub access…" when idle, did not link to the GitHub App
installation URL, and did not clearly explain whether the problem was on
GitHub or in Paperclip Access settings.

**Expected behavior**

Installing the GitHub App with one or more repositories enabled should
import and enable those repositories during initial setup even if custom
review configuration was saved earlier, while preserving that saved
configuration. When verification genuinely fails for missing
repositories, missing permissions, removed/suspended installations, or
transient GitHub repository sync errors, the screen should stop polling,
replace the checking indicator with a clear error card, pair each GitHub
link with an accurate label, link to Paperclip Access settings, and show
active checking feedback only while a retry or refetch is running.

**Steps to reproduce**

1. Create a GitHub Code Review Bot draft and save custom review
configuration (`PUT /api/chat-endpoints/:id/github/configuration`),
incrementing configuration revision to `1`.
2. Register and install the GitHub App on GitHub with one repository
selected, verify webhook delivery, and link a GitHub identity.
3. Resume
`/apps/chat/connect?provider=github&purpose=chat&resume=<endpointId>`.
4. Observe that `POST /api/chat-endpoints/:id/github/setup` leaves the
repository disabled, marks `initialRepositoriesImported: true` while
leaving `initialRepositoryImportPending: true`, and returns `state:
"verify"` with "Enable at least one repository from the App
installation." and no GitHub installation link.

**Paperclip version or commit**

Reproduced on `master` at commit `e2e2265085`.

**Deployment mode**

Self-hosted server

**Agent adapter(s) involved**

Not adapter-specific (core bug)

## What Changed

- Extract initial GitHub repository import eligibility/transition logic
into `server/src/services/chat-github-initial-import.ts` and decouple
initial repository import in `server/src/services/chat-github-wizard.ts`
from `saved.revision === 0` so pre-saved review configuration no longer
blocks enabling repositories granted during initial App installation,
while keeping `saved.revision === 0` strictly for default review
configuration writes.
- Recover verifying endpoints left in the prior half-state
(`initialRepositoriesImported: true` with
`initialRepositoryImportPending: true` and `repositorySelectionSaved`
unset) without re-enabling repositories after an explicit user save
(`repositorySelectionSaved: true`).
- Keep `initialRepositoryImportPending` open when GitHub returns zero
repositories during initial import
(`server/src/services/chat-github-wizard.ts` and
`server/src/services/chat-channels/resource-reconciliation.ts`), refresh
repository inventory on wizard retry when no enabled available
repository exists, and track transient repository-sync errors so failed
`/installation/repositories` refreshes skip initial import and emit
`repositories_sync_failed` instead of misreporting
`no_installation_repositories`.
- Map HTTP 404 on `/app/installations/:id` in
`server/src/services/chat-github-management.ts` to
`installation_missing` + `managementUrl` (keeping 401/403/5xx and
transient failures separate), and enforce `!permissionsOk` precedence
(`permissions_required`) over cached repository state.
- Preserve `setup.github.managementUrl` and sibling setup fields via
atomic `jsonb_set` in
`server/src/services/chat-channels/github/app-setup.ts`.
- Centralize strict GitHub URL validation
(`safeGitHubInstallationManagementUrl`, `safeGitHubAppInstallUrl`,
`safeGitHubAppUrl`) and structured `GitHubVerificationCheckCode` values
(including `repositories_sync_failed`) in
`packages/shared/src/types/github-app-setup.ts` and
`packages/shared/src/index.ts`, with OpenAPI schema updates in
`server/src/routes/openapi.ts` and UI API types in
`ui/src/api/githubChat.ts`.
- Extract `deriveGitHubVerifyView`, `shouldPollGitHubVerify`, and
`<GitHubVerifyStatus />` into
`ui/src/pages/apps/chat/GitHubVerifyStatus.tsx` and simplify
`ui/src/pages/apps/chat/GitHubChatSetup.tsx` so verification failures
stop 5-second background polling, pair every external GitHub link with
an accurate label (including `installation_missing` fallback to
`managementUrl`), show active checking feedback only while
retrying/refetching, and link to Paperclip Access and Connection
settings.
- Add unit, integration, and UI tests in
`server/src/services/chat-github-initial-import.test.ts`,
`server/src/services/chat-github-management.test.ts`,
`server/src/__tests__/chat-channels/github-setup-and-access.integration.test.ts`,
`ui/src/pages/apps/chat/GitHubVerifyStatus.test.ts`, and
`ui/src/pages/apps/chat/GitHubChatSetup.test.tsx`.
- Update `tests/runner-e2e/source-sdk-public-inputs.json` for the shared
type and helper exports.

## Verification

Tested on Linux through revision `9a5a949098`:

- Red-green TDD proofs:
- Pre-saved review configuration and stuck verifying endpoint recovery
(`server/src/__tests__/chat-channels/github-setup-and-access.integration.test.ts`):
confirmed both failed before the wizard fix (`Received: "verify"`,
`Expected: "connected"`) and passed after.
- Removed installation HTTP 404 (`installation_missing` +
`managementUrl`) and `!permissionsOk` precedence
(`server/src/services/chat-github-management.test.ts` + integration
test): confirmed red before fix, green after.
- Transient `/installation/repositories` 503 failure with empty cache
and valid permissions (`repositories_sync_failed`, followed by
successful retry -> `connected`) and UI `installation_missing` fallback
label (`ui/src/pages/apps/chat/GitHubVerifyStatus.test.ts`,
`GitHubChatSetup.test.tsx`,
`github-setup-and-access.integration.test.ts`): confirmed red before
fix, green after.
- Server unit & OpenAPI tests on `9a5a949098`:
- `pnpm --filter @paperclipai/server exec vitest run
src/services/chat-github-management.test.ts
src/services/chat-github-initial-import.test.ts` (47/47 passed: 39
management + 8 initial-import).
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/openapi-routes.test.ts` (59/59 passed).
- Server integration tests (`c1f234335f`; full suite running in CI on
merged head `9a5a949098`):
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/chat-channels/github-setup-and-access.integration.test.ts
-t "recovers from an empty initial GitHub repository inventory|keeps
explicitly disabled repositories disabled|imports GitHub App
installation repositories even when review configuration was
pre-saved|recovers a verifying endpoint whose
initialRepositoriesImported|returns installation_missing with
managementUrl when the GitHub installation is removed|surfaces a
retryable repositories_sync_failed check"` (6/6 passed).
- UI tests & typecheck on `9a5a949098`:
- `pnpm --filter @paperclipai/ui exec vitest run
src/pages/apps/chat/GitHubChatSetup.test.tsx
src/pages/apps/chat/GitHubVerifyStatus.test.ts
src/pages/apps/chat/chat-ui-contract.test.ts` (27/27 passed) and
`ui/src/lib/analytics/replay-masking.test.tsx` (236/236 passed).
- `pnpm --filter @paperclipai/shared build && pnpm --filter
@paperclipai/shared typecheck && pnpm --filter @paperclipai/ui
typecheck` (passed).
- `node scripts/check-token-gates.mjs` — zero violations in touched
files.
- Release registry check on `9a5a949098`:
- `node tests/runner-e2e/source-sdk-public-inputs.mjs
9a5a949098 --check` (`changed: false`, 602
inputs).
- Required CI checks (`ci / verify`, `ci / e2e`, `ci / Typecheck +
Release Registry`, `ci / Build`) cover the full matrix on `9a5a949098`.

## Risks

- Low risk. Auto-enabling repositories remains strictly fenced by
`bot.status !== "active"`, `initialRepositoryImportPending === true`,
and `!repositorySelectionSaved`, and is skipped when repository refresh
fails, so active bots, recovered installations, and explicit user
repository disables in Access settings are never overwritten.
- `managementUrl`, `installationUrl`, and `appUrl` are validated by
shared helpers on both server and UI to allow only `https://github.com`
paths matching their expected URL shapes.

## Model Used

- Google Gemini 4 Neon (High) via Antigravity with tool use and code
execution (exact internal model version string and context window are
not exposed in this session).

## 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-12-04 18:43:20 -06:00

4151 lines
150 KiB
TypeScript

import { chatHistoryActionBlocks } from "../helpers/chat-history-proof.js";
import { notifyChatEndpointConfiguration } from "../../services/chat-channels/configuration-work-notifications.js";
import { conflict } from "../../errors.js";
import { idleWorkSnapshot } from "../../services/task-admission.js";
import type { Db } from "@paperclipai/db";
import { registerChatActionWork } from "../../services/chat-action-work.js";
import { createDeliveryWorkCoordinator } from "../../services/delivery-work-coordinator.js";
import { DELIVERY_QUEUES } from "../../services/delivery-work-notifications.js";
import { createHmac, generateKeyPairSync, randomUUID } from "node:crypto";
import path from "node:path";
import request from "supertest";
import { and, asc, eq, inArray, like, or, sql } from "drizzle-orm";
import { beforeAll, describe, expect, it, onTestFinished, vi } from "vitest";
import { agents, activityLog, authUsers, chatActions, chatConversations, chatDeliveries, chatEndpointLeases, chatEndpointResources, chatEndpoints, chatExternalPrincipals, chatSdkState, companySecrets, companies, companyMemberships, issueComments, issues, projects, projectWorkspaces, toolConnections } from "@paperclipai/db";
import { instanceSettingsService } from "../../services/instance-settings.js";
import type { InternalSetupState } from "../../services/chat-channels/runtime-types.js";
// Opt-in private physical candidate; normal CI uses the staged pinned package.
vi.mock("@chat-adapter/telegram", async (importOriginal) => {
const candidate = process.env.PAPERCLIP_TELEGRAM_STOP_ADAPTER_MODULE;
return candidate ? import(/* @vite-ignore */ candidate) : importOriginal();
});
import { logActivity } from "../../services/activity-log.js";
import { type TestDb, describeEmbeddedPostgres, FakeChatSdkRuntime, makeThread, makeMessage, routesApp } from "./providers.js";
import { createChatChannelHarness } from "./harness.js";
import { createGitHubHarness } from "./github-harness.js";
describeEmbeddedPostgres("chat channels: github setup and access", () => {
const h = createChatChannelHarness();
let db!: TestDb;
beforeAll(() => { db = h.db; });
const gh = createGitHubHarness(h);
const { seedCompany, readChatConversationsByEndpointId, readChatEndpointsById, readChatDeliveriesByEndpointId, readChatDeliveriesById, readChatActionsById, readChatPublicationsById, readToolConnectionsById, readIssuesByCompanyId, createService, retirePublicationFixture, qualifySetupRoundTrip, deliverMessage } = h;
const { githubWebhookVerificationResponse, recordGitHubWebhookVerification, signedGitHubWebhookRequest, configuredGitHubEndpoint, attachFakeGitHubIssueCommentWebhook, githubPreIngressRecoveryFixture } = gh;
describe("GitHub repository pagination and bulk access", () => {
it("pages and searches 1000 repositories while toggle all changes unloaded rows atomically", async () => {
const fixture = await seedCompany();
const context = createService();
const endpoint = await context.service.create(fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId }, "owner-user");
const foreign = await seedCompany();
const other = await context.service.create(foreign.companyId,
{ provider: "github", assignedAgentId: foreign.assignedAgentId }, "owner-user");
const all = await db.insert(chatEndpointResources).values(Array.from({ length: 1000 }, (_, index) => ({
companyId: fixture.companyId, endpointId: endpoint.id, type: "repository",
providerResourceId: `acme/repo-${index}`, label: `Acme/Repo-${String(index).padStart(4, "0")}`,
enabled: true, availability: "available" as const,
}))).returning();
const [unavailable, channel, unrelated] = await db.insert(chatEndpointResources).values([
{ companyId: fixture.companyId, endpointId: endpoint.id, type: "repository", providerResourceId: "unavailable", label: "Unavailable", enabled: true, availability: "unavailable" },
{ companyId: fixture.companyId, endpointId: endpoint.id, type: "channel", providerResourceId: "channel", label: "Channel", enabled: true, availability: "available" },
{ companyId: foreign.companyId, endpointId: other.id, type: "repository", providerResourceId: "foreign", label: "Foreign", enabled: true, availability: "available" },
]).returning();
const app = routesApp(db, fixture.companyId, context.service);
const path = `/api/chat-endpoints/${endpoint.id}/github/repositories`;
const first = await request(app).get(path).expect(200);
expect(first.body).toMatchObject({ nextOffset: 20, totalCount: 1001, enabledCount: 1001, availableCount: 1000 });
expect(first.body.items).toHaveLength(20);
const second = await request(app).get(path).query({ offset: 20 }).expect(200);
expect(second.body.items).toHaveLength(20);
expect(second.body.items.some((row: { id: string }) => first.body.items.some((initial: { id: string }) => initial.id === row.id))).toBe(false);
const search = await request(app).get(path).query({ search: "repo-0999" }).expect(200);
expect(search.body.items.map((row: { label: string }) => row.label)).toEqual(["Acme/Repo-0999"]);
expect(search.body.totalCount).toBe(1001);
expect((await request(app).get(path).query({ search: "%" }).expect(200)).body.items).toEqual([]);
await request(app).get(path).query({ limit: 1000 }).expect(400);
await request(app).get(path).query({ offset: -1 }).expect(400);
await request(app).put(`${path}/access`).send({ enabled: false, search: "repo-0999" }).expect(400);
await request(app).put(`${path}/access`).send({ enabled: false }).expect(200);
const afterOff = await context.service.listGitHubRepositories(endpoint.id, { limit: 20, offset: 0, search: "" });
expect(afterOff.enabledCount).toBe(0);
expect((await db.select().from(chatEndpointResources).where(inArray(chatEndpointResources.id, [channel.id, unrelated.id]))).every((row) => row.enabled)).toBe(true);
const [last] = await db.select().from(chatEndpointResources).where(eq(chatEndpointResources.id, all[999].id));
expect(last.enabled).toBe(false);
await request(app).put(`${path}/access`).send({ enabled: true }).expect(200);
expect((await context.service.listGitHubRepositories(endpoint.id, { limit: 20, offset: 0, search: "" })).enabledCount).toBe(1000);
expect((await db.select().from(chatEndpointResources).where(eq(chatEndpointResources.id, unavailable.id)))[0].enabled).toBe(false);
const saved = await context.service.get(endpoint.id);
expect(saved.setup.github?.repositorySelectionSaved).toBe(true);
const audits = await db.select().from(activityLog).where(and(eq(activityLog.companyId, fixture.companyId), eq(activityLog.action, "chat_endpoint.resources_updated"))).orderBy(asc(activityLog.createdAt));
expect(audits).toHaveLength(2);
expect(audits.every((audit) => audit.actorId === "owner-user")).toBe(true);
expect((audits[0].details!.changes as unknown[])).toHaveLength(1001);
expect((audits[1].details!.changes as unknown[])).toHaveLength(1000);
const crossCompany = routesApp(db, foreign.companyId, context.service);
expect((await request(crossCompany).get(path)).status).toBeOneOf([403, 404]);
expect((await request(crossCompany).put(`${path}/access`).send({ enabled: false })).status).toBeOneOf([403, 404]);
expect(context.wakeup).not.toHaveBeenCalled();
await context.service.shutdown();
});
});
it("requires connection-manager authority for chat connector administration", async () => {
const fixture = await seedCompany();
const { service } = createService();
const endpoint = await service.create(
fixture.companyId,
{
provider: "github",
assignedAgentId: fixture.assignedAgentId,
},
"owner-user",
);
const memberUserId = `member-${randomUUID()}`;
const now = new Date();
await db.insert(authUsers).values({
id: memberUserId,
name: "Ordinary Member",
email: `${memberUserId}@example.com`,
emailVerified: true,
createdAt: now,
updatedAt: now,
});
await db.insert(companyMemberships).values({
companyId: fixture.companyId,
principalType: "user",
principalId: memberUserId,
status: "active",
membershipRole: "member",
});
const memberApp = routesApp(db, fixture.companyId, service, memberUserId);
await request(memberApp)
.get(`/api/chat-endpoints/${endpoint.id}`)
.expect(200);
await request(memberApp)
.get(`/api/companies/${fixture.companyId}/chat-endpoints`)
.expect(200);
const deniedMutations = [
request(memberApp)
.put(`/api/chat-endpoints/${endpoint.id}/github/repositories/access`)
.send({ enabled: false }),
request(memberApp)
.post(`/api/companies/${fixture.companyId}/chat-endpoints`)
.send({ provider: "slack", assignedAgentId: fixture.assignedAgentId }),
request(memberApp)
.patch(`/api/chat-endpoints/${endpoint.id}`)
.send({ allowDirectMessages: true }),
request(memberApp)
.post(`/api/chat-endpoints/${endpoint.id}/setup`)
.send({ action: "pause" }),
request(memberApp).post(
`/api/chat-endpoints/${endpoint.id}/setup-secret`,
),
request(memberApp).post(`/api/chat-endpoints/${endpoint.id}/test`),
request(memberApp)
.put(`/api/chat-endpoints/${endpoint.id}/resources`)
.send({ resources: [] }),
request(memberApp)
.post(
`/api/chat-endpoints/${endpoint.id}/principals/${randomUUID()}/link-intent`,
)
.send({}),
request(memberApp).delete(
`/api/chat-endpoints/${endpoint.id}/principals/${randomUUID()}/link`,
),
request(memberApp).post(
`/api/chat-endpoints/${endpoint.id}/deliveries/${randomUUID()}/replay`,
),
request(memberApp).post(
`/api/chat-endpoints/${endpoint.id}/publications/${randomUUID()}/replay`,
),
request(memberApp)
.post(
`/api/chat-endpoints/${endpoint.id}/publications/${randomUUID()}/resolve`,
)
.send({ action: "cancel" }),
request(memberApp)
.post(
`/api/chat-endpoints/${endpoint.id}/actions/${randomUUID()}/resolve`,
)
.send({ action: "cancel" }),
];
for (const mutation of deniedMutations) {
const response = await mutation.expect(403);
expect(response.body.error).toContain(
"Missing permission: tools:manage_connections",
);
}
const managerApp = routesApp(db, fixture.companyId, service);
await request(managerApp)
.patch(`/api/chat-endpoints/${endpoint.id}`)
.send({ allowDirectMessages: true })
.expect(200)
.expect(({ body }) => {
expect(body.allowDirectMessages).toBe(true);
});
});
it("globally fences GitHub App identity across owner transfers without leaking another company", async () => {
const firstCompany = await seedCompany();
const secondCompany = await seedCompany();
const appRegistrationId = 987654321;
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
let inventoryRequestCount = 0;
let releaseInventory!: () => void;
const bothInventoriesStarted = new Promise<void>((resolve) => {
releaseInventory = resolve;
});
const providerFetch = (owner: string, installationId: number) =>
(async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return new Response(
JSON.stringify({
id: appRegistrationId,
slug: "shared-paperclip-app",
name: "Shared Paperclip App",
owner: { login: owner },
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
events: [
"github_app_authorization",
"installation",
"installation_repositories",
"issue_comment",
"pull_request_review_comment",
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
if (url === "https://api.github.com/app/installations?per_page=100") {
inventoryRequestCount += 1;
if (inventoryRequestCount === 2) releaseInventory();
await bothInventoriesStarted;
return new Response(
JSON.stringify([
{
id: installationId,
account: { id: installationId, login: owner },
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
suspended_at: null,
},
]),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
if (
url ===
`https://api.github.com/app/installations/${installationId}/access_tokens`
) {
return new Response(JSON.stringify({ token: "installation-token" }), {
status: 201,
headers: { "content-type": "application/json" },
});
}
if (
url ===
"https://api.github.com/installation/repositories?per_page=100&page=1"
) {
return new Response(JSON.stringify({ repositories: [] }), {
status: 200,
headers: { "content-type": "application/json" },
});
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const first = createService(
new FakeChatSdkRuntime(),
providerFetch("original-owner", 8101),
);
const second = createService(
new FakeChatSdkRuntime(),
providerFetch("transferred-owner", 8102),
);
const firstEndpoint = await first.service.create(
firstCompany.companyId,
{
provider: "github",
assignedAgentId: firstCompany.assignedAgentId,
},
"owner-user",
);
const secondEndpoint = await second.service.create(
secondCompany.companyId,
{
provider: "github",
assignedAgentId: secondCompany.assignedAgentId,
},
"owner-user",
);
for (const [service, endpoint] of [
[first.service, firstEndpoint],
[second.service, secondEndpoint],
] as const) {
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
}
const endpoints = [firstEndpoint, secondEndpoint] as const;
const services = [first.service, second.service] as const;
const outcomes = await Promise.allSettled(
services.map((service, index) =>
service.configure(
endpoints[index]!.id,
{
action: "configure",
credentials: { appId: "123456", privateKey },
},
"owner-user",
),
),
);
expect(
outcomes.filter((outcome) => outcome.status === "fulfilled"),
).toHaveLength(1);
const rejectedIndex = outcomes.findIndex(
(outcome) => outcome.status === "rejected",
);
expect(rejectedIndex).toBeGreaterThanOrEqual(0);
const conflict = (outcomes[rejectedIndex] as PromiseRejectedResult).reason;
expect(conflict).toMatchObject({
status: 409,
details: {
code: "chat_bot_identity_in_use",
},
});
expect(conflict).not.toMatchObject({
details: {
endpointId: expect.any(String),
assignedAgentId: expect.any(String),
},
});
await expect(
db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoints[rejectedIndex]!.connectionId))
.then(
(rows) => rows[0]?.refs.map((ref) => ref.configPath).sort() ?? [],
),
).resolves.toEqual(["credentials.webhookSecret"]);
await first.service.shutdown();
await second.service.shutdown();
});
it("configures a customer-owned GitHub App and only auto-enables the first addressed setup repository", async () => {
const fixture = await seedCompany();
const appId = "123456";
const privateKey = generateKeyPairSync("rsa", {
modulusLength: 2048,
})
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
let observedIssuer: string | null = null;
let appRegistrationId = 789;
const providerFetch = vi.fn(
async (input: string | URL | Request, init?: RequestInit) => {
const url = String(input);
if (url === "https://api.github.com/app") {
const authorization = new Headers(init?.headers).get("authorization");
const token = authorization?.replace(/^Bearer\s+/i, "");
const payload = token?.split(".")[1];
if (!payload) throw new Error("GitHub App JWT was not sent");
observedIssuer = String(
(
JSON.parse(
Buffer.from(payload, "base64url").toString("utf8"),
) as { iss?: unknown }
).iss,
);
return new Response(
JSON.stringify({
id: appRegistrationId,
slug: "maya-paperclip",
name: "Maya Paperclip",
owner: { login: "paperclipai" },
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
events: [
"github_app_authorization",
"installation",
"installation_repositories",
"issue_comment",
"pull_request_review_comment",
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return new Response(
JSON.stringify([
{
id: 2468,
account: {
id: 1357,
login: "paperclipai",
type: "Organization",
},
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
suspended_at: null,
},
]),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
if (
url === "https://api.github.com/app/installations/2468/access_tokens"
) {
return new Response(JSON.stringify({ token: "installation-token" }), {
status: 201,
headers: { "content-type": "application/json" },
});
}
if (
url ===
"https://api.github.com/installation/repositories?per_page=100&page=1"
) {
return new Response(
JSON.stringify({
repositories: [
{
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
},
{
id: 97532,
full_name: "paperclipai/paperclip-disabled",
html_url: "https://github.com/paperclipai/paperclip-disabled",
owner: { id: 1357, login: "paperclipai" },
private: false,
},
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
throw new Error(`Unexpected provider request: ${url}`);
},
) as unknown as typeof globalThis.fetch;
const { runtime, service } = createService(
new FakeChatSdkRuntime(),
providerFetch,
{ scheduleDeferredWork: () => undefined },
);
const endpoint = await service.create(
fixture.companyId,
{
provider: "github",
assignedAgentId: fixture.assignedAgentId,
},
"owner-user",
);
const app = routesApp(db, fixture.companyId, service);
const generatedSecretResponse = await request(app)
.post(`/api/chat-endpoints/${endpoint.id}/setup-secret`)
.send({})
.expect(201);
const generatedWebhookSecret = generatedSecretResponse.body
.webhookSecret as string;
expect(generatedSecretResponse.headers["cache-control"]).toBe("no-store");
expect(generatedWebhookSecret).toMatch(/^[a-f0-9]{64}$/);
const endpointAfterGeneration = await request(app)
.get(`/api/chat-endpoints/${endpoint.id}`)
.expect(200);
expect(endpointAfterGeneration.body.setup.webhookSecretConfigured).toBe(
true,
);
expect(JSON.stringify(endpointAfterGeneration.body)).not.toContain(
generatedWebhookSecret,
);
await expect(
service.configure(
endpoint.id,
{
action: "configure",
credentials: { appId, privateKey },
},
"owner-user",
),
).rejects.toMatchObject({
status: 409,
details: { code: "chat_webhook_not_verified" },
});
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
generatedWebhookSecret,
);
const configured = await service.configure(
endpoint.id,
{
action: "configure",
credentials: {
appId,
privateKey,
},
},
"owner-user",
);
expect(observedIssuer).toBe(appId);
expect(configured).toMatchObject({
status: "verifying",
providerAccountId: "paperclipai",
botExternalId: "789",
botUsername: "maya-paperclip[bot]",
setup: { step: "test", webhookVerifiedAt: expect.any(String) },
});
expect(
runtime.configurations.get(endpoint.id)?.providerConfig,
).toMatchObject({
provider: "github",
credentials: {
appId,
privateKey,
installationId: 2468,
webhookSecret: generatedWebhookSecret,
},
});
const githubConfig = runtime.configurations.get(endpoint.id)?.providerConfig;
expect(
githubConfig?.provider ===
"github" &&
githubConfig.credentials
.botUserId,
).toBeUndefined();
appRegistrationId = 790;
await expect(
service.configure(
endpoint.id,
{
action: "reconnect",
credentials: { appId, privateKey },
},
"owner-user",
),
).rejects.toMatchObject({
status: 409,
details: { code: "chat_bot_identity_changed" },
});
appRegistrationId = 789;
const [connection] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection.refs.map((ref) => ref.configPath).sort()).toEqual([
"credentials.appId",
"credentials.installationId",
"credentials.privateKey",
"credentials.webhookSecret",
]);
const githubResources = await service.listResources(endpoint.id);
expect(
githubResources.map((resource) => ({
providerResourceId: resource.providerResourceId,
availability: resource.availability,
enabled: resource.enabled,
})),
).toEqual([
{
providerResourceId: "paperclipai/paperclip",
availability: "available",
enabled: false,
},
{
providerResourceId: "paperclipai/paperclip-disabled",
availability: "available",
enabled: false,
},
]);
const githubCallbacks = runtime.configurations.get(endpoint.id)?.callbacks;
if (!githubCallbacks) throw new Error("Expected GitHub callbacks");
await deliverMessage({
callbacks: githubCallbacks,
endpointId: endpoint.id,
provider: "github",
thread: makeThread({
channelId: "PaperclipAI/Paperclip",
id: "github:PaperclipAI/Paperclip:issue:17",
name: "paperclipai/paperclip",
}).thread,
message: makeMessage({
id: "github-root-17",
text: "@maya triage issue 17",
mentioned: true,
}),
trigger: "mention",
});
await expect(
readChatConversationsByEndpointId(endpoint.id),
).resolves.toHaveLength(1);
expect(
(await service.listResources(endpoint.id)).map((resource) => ({
providerResourceId: resource.providerResourceId,
enabled: resource.enabled,
})),
).toEqual([
{ providerResourceId: "paperclipai/paperclip", enabled: true },
{
providerResourceId: "paperclipai/paperclip-disabled",
enabled: false,
},
]);
await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-repository-removed",
event: "installation_repositories",
payload: {
action: "removed",
repositories_added: [],
repositories_removed: [
{
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
},
],
},
webhookSecret: generatedWebhookSecret,
}),
);
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "available",
enabled: true,
}),
expect.objectContaining({
providerResourceId: "paperclipai/paperclip-disabled",
availability: "available",
enabled: false,
}),
]);
await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-installation-suspended",
event: "installation",
payload: {
action: "suspend",
installation: { id: 2468 },
},
webhookSecret: generatedWebhookSecret,
}),
);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "verifying",
healthMessage: "Waiting for a test conversation",
});
expect(runtime.endpoints.has(endpoint.id)).toBe(true);
});
it("admits only an addressed raw GitHub comment as the first setup repository", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint, runtime, service, webhookSecret } =
await configuredGitHubEndpoint(fixture);
const [resource] = await service.listResources(endpoint.id);
await service.replaceResources(endpoint.id, [
{ id: resource!.id, enabled: false },
]);
const endpointRuntime = runtime.endpoints.get(endpoint.id);
const currentEndpoint = await service.get(endpoint.id);
if (!endpointRuntime || !currentEndpoint.botUsername)
throw new Error("Expected configured GitHub runtime identity");
attachFakeGitHubIssueCommentWebhook({
botUsername: currentEndpoint.botUsername,
callbacks,
endpointId: endpoint.id,
runtime: endpointRuntime,
});
const payload = (body: string, commentId: number, issueNumber: number) => ({
action: "created",
installation: { id: 2468 },
repository: {
id: 97531,
full_name: "paperclipai/paperclip",
name: "paperclip",
owner: { id: 1357, login: "paperclipai" },
},
issue: { number: issueNumber },
comment: {
id: commentId,
body,
created_at: "2026-09-06T12:00:00Z",
updated_at: "2026-09-06T12:00:00Z",
user: { id: 42, login: "octocat" },
},
sender: { id: 42, login: "octocat" },
});
const unaddressedMarker = `private-unaddressed-${randomUUID()}`;
const ignored = await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-unaddressed-first-setup",
event: "issue_comment",
payload: payload(unaddressedMarker, 17001, 17),
webhookSecret,
}),
);
expect(ignored.status).toBe(200);
expect(endpointRuntime.webhookRequest).toBeNull();
await expect(
db.execute(
sql`select id from chat_actions where endpoint_id = ${endpoint.id} and payload::text like ${`%${unaddressedMarker}%`}`,
),
).resolves.toHaveLength(0);
const botMention = currentEndpoint.botUsername.replace(/\[bot\]$/i, "");
const accepted = await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-addressed-first-setup",
event: "issue_comment",
payload: payload(`@${botMention} triage issue 18`, 18001, 18),
webhookSecret,
}),
);
expect(accepted.status).toBe(202);
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
enabled: true,
}),
]);
await expect(service.listConversations(endpoint.id)).resolves.toHaveLength(
1,
);
});
it("retries transient GitHub endpoint lock contention before accepting a wake", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint, service } =
await configuredGitHubEndpoint(fixture);
const current = await service.get(endpoint.id);
const { thread, subscribe } = makeThread({
channelId: "paperclipai/paperclip",
id: "github:paperclipai/paperclip:903",
});
let release!: () => void;
let acquired!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
const locked = new Promise<void>((resolve) => {
acquired = resolve;
});
let transaction: Promise<unknown> | undefined;
let timer: ReturnType<typeof setTimeout> | undefined;
subscribe.mockImplementationOnce(async () => {
transaction = db.transaction(async (tx) => {
await tx
.select({ id: chatEndpoints.id })
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id))
.for("no key update");
acquired();
await held;
});
await locked;
timer = setTimeout(release, 250);
return undefined;
});
try {
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread,
trigger: "mention",
message: makeMessage({
id: "github-contended-admission",
userId: "42",
userName: "octocat",
mentioned: true,
text: `@${current.botUsername} review after concurrent ingress`,
}),
});
} finally {
if (timer) clearTimeout(timer);
release();
await transaction;
}
expect(subscribe).toHaveBeenCalledTimes(1);
const deliveries = await readChatDeliveriesByEndpointId(endpoint.id);
expect(
deliveries.filter((delivery) => delivery.state === "processed"),
).toHaveLength(1);
const wakes = await db
.select()
.from(chatActions)
.where(
and(
eq(chatActions.endpointId, endpoint.id),
eq(chatActions.kind, "inbound_wakeup"),
),
);
expect(wakes).toHaveLength(1);
expect(wakes[0].status).toBe("processed");
await expect(service.listConversations(endpoint.id)).resolves.toHaveLength(
1,
);
});
it("atomically admits only one first GitHub repository under concurrent root mentions", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint, runtime, service, webhookSecret } =
await configuredGitHubEndpoint(fixture);
const endpointRuntime = runtime.endpoints.get(endpoint.id);
const currentEndpoint = await service.get(endpoint.id);
if (!endpointRuntime || !currentEndpoint.botUsername)
throw new Error("Expected configured GitHub runtime identity");
attachFakeGitHubIssueCommentWebhook({
botUsername: currentEndpoint.botUsername,
callbacks,
endpointId: endpoint.id,
runtime: endpointRuntime,
});
const [firstResource] = await service.listResources(endpoint.id);
await service.replaceResources(endpoint.id, [
{ id: firstResource!.id, enabled: false },
]);
await db.insert(chatEndpointResources).values({
companyId: endpoint.companyId,
endpointId: endpoint.id,
type: "repository",
providerResourceId: "paperclipai/paperclip-second",
label: "paperclipai/paperclip-second",
availability: "available",
enabled: false,
});
const roots = [
{
channelId: "paperclipai/paperclip",
issueNumber: 901,
messageId: 901001,
repositoryId: 97531,
},
{
channelId: "paperclipai/paperclip-second",
issueNumber: 902,
messageId: 902001,
repositoryId: 97532,
},
];
await Promise.all(
roots.map((root) => {
const [owner, name] = root.channelId.split("/");
return service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: `github-concurrent-root-${root.issueNumber}`,
event: "issue_comment",
payload: {
action: "created",
installation: { id: 2468 },
repository: {
id: root.repositoryId,
full_name: root.channelId,
name,
owner: { id: 1357, login: owner },
},
issue: { number: root.issueNumber },
comment: {
id: root.messageId,
body: `@${currentEndpoint.botUsername} investigate this race`,
created_at: "2026-09-06T12:00:00Z",
updated_at: "2026-09-06T12:00:00Z",
user: { id: 42, login: "octocat" },
},
sender: { id: 42, login: "octocat" },
},
webhookSecret,
}),
);
}),
);
const resources = await service.listResources(endpoint.id);
expect(resources.filter((resource) => resource.enabled)).toHaveLength(1);
const conversations = await db
.select({ issueId: chatConversations.issueId })
.from(chatConversations)
.where(eq(chatConversations.endpointId, endpoint.id));
expect(conversations).toHaveLength(1);
const [issue] = await db
.select({ title: issues.title })
.from(issues)
.where(eq(issues.id, conversations[0]!.issueId));
expect(issue?.title).toBe("investigate this race");
});
it("rejects caller-controlled GitHub setup secrets and arbitrary credential fields", async () => {
const fixture = await seedCompany();
const providerFetch = vi.fn(async () => {
throw new Error("Credential validation must run before provider access");
}) as unknown as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId },
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
const app = routesApp(db, fixture.companyId, service);
await request(app)
.post(`/api/chat-endpoints/${endpoint.id}/setup`)
.send({
action: "configure",
credentials: {
appId: "123456",
privateKey: "private-key",
webhookSecret: "caller-controlled-secret",
},
})
.expect(422);
await expect(
service.configure(
endpoint.id,
{
action: "configure",
credentials: {
appId: "123456",
privateKey: "private-key",
installationId: "2468",
unexpected: "value",
},
},
"owner-user",
),
).rejects.toMatchObject({
status: 422,
details: {
code: "chat_endpoint_credentials_invalid",
provider: "github",
action: "configure",
unsupportedKeys: ["installationId", "unexpected"],
},
});
expect(providerFetch).not.toHaveBeenCalled();
const [connection] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection!.refs).toEqual([
expect.objectContaining({ configPath: "credentials.webhookSecret" }),
]);
});
it("accepts a manually created GitHub App when /app lists only selectable webhook events", async () => {
const fixture = await seedCompany();
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const providerFetch = vi.fn(async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return new Response(
JSON.stringify({
id: 991124,
slug: "maya-selectable-events",
name: "Maya Selectable Events",
owner: { login: "paperclipai" },
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
events: ["issue_comment", "pull_request_review_comment"],
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return new Response(
JSON.stringify([
{
id: 2468,
account: {
id: 1357,
login: "paperclipai",
type: "Organization",
},
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
suspended_at: null,
},
]),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
if (
url === "https://api.github.com/app/installations/2468/access_tokens"
) {
return new Response(
JSON.stringify({ token: "selectable-events-installation-token" }),
{ status: 201, headers: { "content-type": "application/json" } },
);
}
if (
url ===
"https://api.github.com/installation/repositories?per_page=100&page=1"
) {
return new Response(
JSON.stringify({
repositories: [
{
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
},
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId },
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
await expect(
service.configure(
endpoint.id,
{ action: "configure", credentials: { appId: "991124", privateKey } },
"owner-user",
),
).resolves.toMatchObject({
status: "verifying",
providerAccountId: "paperclipai",
botExternalId: "991124",
botUsername: "maya-selectable-events[bot]",
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "available",
}),
]);
await service.shutdown();
});
it("rejects over-scoped GitHub Apps while tolerating unavoidable lifecycle events", async () => {
const fixture = await seedCompany();
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const { service } = createService(new FakeChatSdkRuntime(), (async (
input: string | URL | Request,
) => {
if (String(input) !== "https://api.github.com/app") {
throw new Error(`Unexpected provider request: ${String(input)}`);
}
return new Response(
JSON.stringify({
id: 991123,
slug: "maya-over-scoped",
name: "Maya Over-scoped",
owner: { login: "paperclipai" },
permissions: {
contents: "write",
issues: "write",
metadata: "read",
pull_requests: "write",
},
events: [
"github_app_authorization",
"installation",
"installation_repositories",
"issue_comment",
"pull_request_review_comment",
"push",
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}) as typeof globalThis.fetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId },
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
await expect(
service.configure(
endpoint.id,
{ action: "configure", credentials: { appId: "991123", privateKey } },
"owner-user",
),
).rejects.toMatchObject({
status: 422,
details: {
code: "chat_provider_permissions_missing",
provider: "github",
missingPermissions: [],
excessivePermissions: ["contents"],
missingEvents: [],
excessiveEvents: ["push"],
},
});
const [connection] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection!.refs).toEqual([
expect.objectContaining({ configPath: "credentials.webhookSecret" }),
]);
});
it("rotates a live GitHub webhook secret fail-closed and reconnects with stored credentials", async () => {
const fixture = await seedCompany();
const { endpoint, runtime, service } =
await configuredGitHubEndpoint(fixture);
await db
.update(chatEndpoints)
.set({ status: "active", setup: { step: "complete" } })
.where(eq(chatEndpoints.id, endpoint.id));
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
expect(webhookSecret).toMatch(/^[a-f0-9]{64}$/);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
healthMessage:
"Update the GitHub webhook secret, then reconnect this App",
setup: { step: "provider_setup", webhookSecretConfigured: true },
});
const [disabledConnection] = await db
.select({
status: toolConnections.status,
enabled: toolConnections.enabled,
healthStatus: toolConnections.healthStatus,
})
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(disabledConnection).toEqual({
status: "disabled",
enabled: false,
healthStatus: "degraded",
});
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
const reconnected = await service.configure(
endpoint.id,
{ action: "reconnect" },
"owner-user",
);
expect(reconnected).toMatchObject({
status: "verifying",
setup: { step: "test", webhookSecretConfigured: true },
});
expect(
runtime.configurations.get(endpoint.id)?.providerConfig,
).toMatchObject({
provider: "github",
credentials: {
appId: "123456",
webhookSecret,
},
});
expect(JSON.stringify(reconnected)).not.toContain(webhookSecret);
});
it("reveals a rotated GitHub secret despite a later completion-audit failure and makes retry a new rotation", async () => {
const fixture = await seedCompany();
let failedAuditAction: string | null = null;
let failCredentialPersistence = false;
const setupSecretActivityLogger: typeof logActivity = async (
database,
input,
publications,
) => {
if (failedAuditAction === input.action) {
failedAuditAction = null;
throw new Error("simulated setup-secret audit outage");
}
return logActivity(database, input, publications);
};
const setupSecretCredentialPersistBarrier = async () => {
if (!failCredentialPersistence) return;
failCredentialPersistence = false;
throw new Error("simulated setup-secret credential persistence outage");
};
const {
endpoint,
runtime,
service,
webhookSecret: originalSecret,
} = await configuredGitHubEndpoint(fixture, {
setupSecretActivityLogger,
setupSecretCredentialPersistBarrier,
});
failedAuditAction = "chat_endpoint.setup_secret_rotation_started";
await expect(
service.generateSetupSecret(endpoint.id, "owner-user"),
).rejects.toThrow("simulated setup-secret audit outage");
expect(runtime.endpoints.has(endpoint.id)).toBe(true);
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
originalSecret,
),
).resolves.toMatchObject({
status: 200,
});
const [refsBeforePersistenceFailure] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
failCredentialPersistence = true;
await expect(
service.generateSetupSecret(endpoint.id, "owner-user"),
).rejects.toThrow("simulated setup-secret credential persistence outage");
const [refsAfterPersistenceFailure] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(refsAfterPersistenceFailure!.refs).toEqual(
refsBeforePersistenceFailure!.refs,
);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
healthMessage:
"Update the GitHub webhook secret, then reconnect this App",
setup: { step: "provider_setup", webhookSecretConfigured: true },
});
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
originalSecret,
),
).resolves.toMatchObject({
status: 200,
});
failedAuditAction = "chat_endpoint.setup_secret_generated";
const first = await service.generateSetupSecret(endpoint.id, "owner-user");
expect(first.webhookSecret).toMatch(/^[a-f0-9]{64}$/);
expect(first.webhookSecret).not.toBe(originalSecret);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
originalSecret,
),
).resolves.toMatchObject({
status: 401,
});
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
first.webhookSecret,
),
).resolves.toMatchObject({ status: 200 });
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
setup: {
step: "provider_setup",
webhookSecretConfigured: true,
},
});
// A caller that lost the successful response may explicitly retry. That
// retry is another serialized rotation: only the newly returned value is
// valid, so the caller can converge without reusing hidden plaintext.
const second = await service.generateSetupSecret(endpoint.id, "owner-user");
expect(second.webhookSecret).toMatch(/^[a-f0-9]{64}$/);
expect(second.webhookSecret).not.toBe(first.webhookSecret);
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
first.webhookSecret,
),
).resolves.toMatchObject({ status: 401 });
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
second.webhookSecret,
),
).resolves.toMatchObject({ status: 200 });
const auditRows = await db
.select({ action: activityLog.action, details: activityLog.details })
.from(activityLog)
.where(eq(activityLog.entityId, endpoint.connectionId));
const started = auditRows.filter(
(row) => row.action === "chat_endpoint.setup_secret_rotation_started",
);
const completed = auditRows.filter(
(row) => row.action === "chat_endpoint.setup_secret_generated",
);
const failed = auditRows.filter(
(row) => row.action === "chat_endpoint.setup_secret_rotation_failed",
);
expect(started).toHaveLength(4);
expect(completed).toHaveLength(2);
expect(failed).toHaveLength(1);
const startedRotationIds = started.map((row) => row.details!.rotationId);
const terminalRotationIds = new Set(
[...completed, ...failed].map((row) => row.details!.rotationId),
);
expect(
startedRotationIds.filter(
(rotationId) => !terminalRotationIds.has(rotationId),
),
).toHaveLength(1);
expect(JSON.stringify(auditRows)).not.toContain(first.webhookSecret);
expect(JSON.stringify(auditRows)).not.toContain(second.webhookSecret);
});
it("replaces a pre-connect GitHub webhook secret without inventing a broken existing App", async () => {
const fixture = await seedCompany();
const { service } = createService();
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId },
"owner-user",
);
const first = await service.generateSetupSecret(endpoint.id, "owner-user");
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
first.webhookSecret,
);
const second = await service.generateSetupSecret(endpoint.id, "owner-user");
expect(second.webhookSecret).not.toBe(first.webhookSecret);
await expect(
githubWebhookVerificationResponse(
service,
endpoint.publicId,
first.webhookSecret,
),
).resolves.toMatchObject({ status: 401 });
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "draft",
providerAccountId: null,
botExternalId: null,
healthMessage: null,
setup: {
step: "provider_setup",
webhookSecretConfigured: true,
webhookVerifiedAt: null,
},
});
const [connection] = await db
.select({
status: toolConnections.status,
enabled: toolConnections.enabled,
healthStatus: toolConnections.healthStatus,
refs: toolConnections.credentialSecretRefs,
})
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection).toMatchObject({
status: "draft",
enabled: false,
healthStatus: "unchecked",
refs: [
expect.objectContaining({
configPath: "credentials.webhookSecret",
}),
],
});
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
second.webhookSecret,
);
const setupActions = await db
.select({ action: activityLog.action, details: activityLog.details })
.from(activityLog)
.where(eq(activityLog.entityId, endpoint.connectionId));
const generatedActions = setupActions.filter(
(item) => item.action === "chat_endpoint.setup_secret_generated",
);
expect(generatedActions).toHaveLength(2);
expect(generatedActions).toEqual(
expect.arrayContaining([
expect.objectContaining({
details: expect.objectContaining({
rotated: false,
replacedPrevious: false,
}),
}),
expect.objectContaining({
details: expect.objectContaining({
rotated: false,
replacedPrevious: true,
}),
}),
]),
);
expect(
setupActions.filter(
(item) => item.action === "chat_endpoint.webhook_verified",
),
).toHaveLength(2);
});
it("preserves GitHub webhook verification through setup completion and reconnect", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint, service } =
await configuredGitHubEndpoint(fixture);
const setupThread = makeThread({
channelId: "paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:811",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: setupThread.thread,
message: makeMessage({
id: "github-setup-root-811",
text: "@maya-paperclip[bot] verify setup state",
mentioned: true,
}),
trigger: "mention",
});
await qualifySetupRoundTrip(service, endpoint.id);
const [reply] = await db.select().from(chatActions).where(and(eq(chatActions.endpointId, endpoint.id), eq(chatActions.kind, "github_review_publication")));
await db.update(chatActions).set({ status: "processing" }).where(eq(chatActions.id, reply.id));
await expect(service.test(endpoint.id)).rejects.toThrow("Wait for the Paperclip agent to reply");
await db.update(chatActions).set({ status: "processed" }).where(eq(chatActions.id, reply.id));
const activated = await service.test(endpoint.id);
expect(activated).toMatchObject({
status: "active",
setup: {
step: "complete",
webhookVerifiedAt: expect.any(String),
},
});
const verifiedAt = activated.setup.webhookVerifiedAt;
const reconnected = await service.configure(
endpoint.id,
{ action: "reconnect" },
"owner-user",
);
expect(reconnected).toMatchObject({
status: "verifying",
setup: {
step: "test",
webhookVerifiedAt: verifiedAt,
},
});
const [stored] = await db
.select({ setup: chatEndpoints.setup })
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id));
expect(stored!.setup).toMatchObject({
step: "test",
webhookVerifiedAt: verifiedAt,
runtimeGeneration: expect.any(Number),
});
});
it("repairs the same GitHub App webhook on reconnect without claiming a fresh signed ping or chat round trip", async () => {
const fixture = await seedCompany();
const { endpoint, service, webhookSecret, webhookSyncRequests } =
await configuredGitHubEndpoint(fixture);
const before = await service.get(endpoint.id);
expect(webhookSyncRequests).toHaveLength(0);
const [storedBefore] = await db
.select({ setup: chatEndpoints.setup })
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id));
const refsBefore = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
const repaired = await service.configure(
endpoint.id,
{ action: "reconnect" },
"owner-user",
);
expect(webhookSyncRequests).toEqual([
{
url: before.setup.webhookUrl,
content_type: "json",
insecure_ssl: "0",
secret: webhookSecret,
},
]);
expect(repaired).toMatchObject({
id: endpoint.id,
assignedAgentId: fixture.assignedAgentId,
botExternalId: before.botExternalId,
status: "verifying",
setup: {
step: "test",
webhookVerifiedAt: before.setup.webhookVerifiedAt,
},
});
const [storedRepair] = await db
.select({
lastEventAt: chatEndpoints.lastEventAt,
setup: chatEndpoints.setup,
})
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id));
expect(storedRepair?.lastEventAt).toBeNull();
expect(storedRepair?.setup?.testStartedAt).not.toBe(
storedBefore?.setup?.testStartedAt,
);
expect((storedRepair?.setup as InternalSetupState | undefined)?.runtimeGeneration).toBe(
Number((storedBefore?.setup as InternalSetupState | undefined)?.runtimeGeneration) + 1,
);
await expect(service.test(endpoint.id)).rejects.toMatchObject({
details: { code: "chat_test_message_missing" },
});
expect(
await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId)),
).toEqual(refsBefore);
const audits = await db
.select({ action: activityLog.action, details: activityLog.details })
.from(activityLog)
.where(eq(activityLog.entityId, endpoint.connectionId));
expect(audits).toContainEqual({
action: "chat_endpoint.webhook_sync_started",
details: { endpointId: endpoint.id, provider: "github" },
});
expect(audits).toContainEqual({
action: "chat_endpoint.webhook_synced",
details: { endpointId: endpoint.id, provider: "github" },
});
expect(JSON.stringify({ repaired, audits })).not.toContain(webhookSecret);
await service.configure(endpoint.id, { action: "reconnect" }, "owner-user");
expect(webhookSyncRequests).toHaveLength(2);
expect(webhookSyncRequests[1]).toEqual(webhookSyncRequests[0]);
});
it.each(["http", "mismatch"])(
"leaves GitHub reconnect unqualified after a %s webhook repair failure",
async (failure) => {
const fixture = await seedCompany();
const {
endpoint,
service,
runtime,
webhookSecret,
setWebhookSyncResponse,
} = await configuredGitHubEndpoint(fixture);
setWebhookSyncResponse(async () => {
if (failure === "transport")
throw new Error(`private provider echo ${webhookSecret}`);
if (failure === "http")
return new Response(webhookSecret, { status: 403 });
if (failure === "oversized") return new Response(" ".repeat(32_769));
if (failure === "malformed") return new Response(webhookSecret);
return new Response(
JSON.stringify({
url: `https://bad.example/${webhookSecret}`,
content_type: "json",
insecure_ssl: "0",
}),
);
});
await expect(
service.configure(endpoint.id, { action: "reconnect" }, "owner-user"),
).rejects.toMatchObject({
details: { code: "chat_provider_setup_failed" },
});
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
const failed = await service.get(endpoint.id);
expect(failed).toMatchObject({ status: "attention" });
const audits = await db
.select({ action: activityLog.action, details: activityLog.details })
.from(activityLog)
.where(eq(activityLog.entityId, endpoint.connectionId));
expect(audits.map((row) => row.action)).toContain(
"chat_endpoint.webhook_sync_started",
);
expect(audits.map((row) => row.action)).not.toContain(
"chat_endpoint.webhook_synced",
);
expect(audits.map((row) => row.action)).not.toContain(
"chat_endpoint.reconnected",
);
expect(JSON.stringify({ failed, audits })).not.toContain(webhookSecret);
await expect(service.test(endpoint.id)).rejects.toMatchObject({
details: { code: "chat_endpoint_not_testing" },
});
},
);
it("does not complete GitHub webhook repair or overwrite a new credential lease owner after remote success", async () => {
const fixture = await seedCompany();
const {
endpoint,
service,
runtime,
setWebhookSyncResponse,
webhookSyncRequests,
} = await configuredGitHubEndpoint(fixture);
setWebhookSyncResponse(async () => {
await db
.update(chatEndpointLeases)
.set({ token: randomUUID() })
.where(
and(
eq(chatEndpointLeases.endpointId, endpoint.id),
eq(chatEndpointLeases.leaseKey, "credentials"),
),
);
await db
.update(chatEndpoints)
.set({ status: "attention", healthMessage: "Winning lease state" })
.where(eq(chatEndpoints.id, endpoint.id));
const config = webhookSyncRequests.at(-1)!;
return new Response(
JSON.stringify({
url: config.url,
content_type: "json",
insecure_ssl: "0",
}),
);
});
await expect(
service.configure(endpoint.id, { action: "reconnect" }, "owner-user"),
).rejects.toMatchObject({ code: "CHAT_CREDENTIAL_LEASE_LOST" });
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
healthMessage: "Winning lease state",
});
const audits = await db
.select({ action: activityLog.action })
.from(activityLog)
.where(eq(activityLog.entityId, endpoint.connectionId));
expect(audits.map((row) => row.action)).not.toContain(
"chat_endpoint.webhook_synced",
);
expect(audits.map((row) => row.action)).not.toContain(
"chat_endpoint.reconnected",
);
});
it("serializes concurrent GitHub setup-secret requests without losing stored credentials", async () => {
const fixture = await seedCompany();
const { endpoint, runtime, service } =
await configuredGitHubEndpoint(fixture);
const app = routesApp(db, fixture.companyId, service);
const [first, second] = await Promise.all([
request(app)
.post(`/api/chat-endpoints/${endpoint.id}/setup-secret`)
.send({})
.expect(201),
request(app)
.post(`/api/chat-endpoints/${endpoint.id}/setup-secret`)
.send({})
.expect(201),
]);
const rotatedSecrets = [
first.body.webhookSecret as string,
second.body.webhookSecret as string,
];
expect(rotatedSecrets[0]).toMatch(/^[a-f0-9]{64}$/);
expect(rotatedSecrets[1]).toMatch(/^[a-f0-9]{64}$/);
expect(rotatedSecrets[0]).not.toBe(rotatedSecrets[1]);
const [connection] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection!.refs.map((ref) => ref.configPath).sort()).toEqual([
"credentials.appId",
"credentials.installationId",
"credentials.privateKey",
"credentials.webhookSecret",
]);
await expect(
db
.select()
.from(activityLog)
.where(
and(
eq(activityLog.entityId, endpoint.connectionId),
eq(activityLog.action, "chat_endpoint.setup_secret_generated"),
),
),
).resolves.toHaveLength(3);
await expect(
db
.select()
.from(chatEndpointLeases)
.where(eq(chatEndpointLeases.endpointId, endpoint.id)),
).resolves.toHaveLength(0);
let currentWebhookSecret: string | null = null;
for (const candidate of rotatedSecrets) {
const response = await githubWebhookVerificationResponse(
service,
endpoint.publicId,
candidate,
);
if (response.status === 200) currentWebhookSecret = candidate;
}
expect(currentWebhookSecret).not.toBeNull();
await service.configure(endpoint.id, { action: "reconnect" }, "owner-user");
const providerConfig = runtime.configurations.get(
endpoint.id,
)?.providerConfig;
expect(providerConfig).toMatchObject({
provider: "github",
credentials: {
appId: "123456",
installationId: 2468,
privateKey: expect.stringContaining("BEGIN PRIVATE KEY"),
},
});
if (providerConfig?.provider !== "github")
throw new Error("Expected GitHub provider configuration");
expect(providerConfig.credentials.webhookSecret).toBe(currentWebhookSecret);
});
it("serializes a GitHub secret rotation racing reconnect and preserves the rotated secret", async () => {
const fixture = await seedCompany();
const { endpoint, runtime, service } =
await configuredGitHubEndpoint(fixture);
const app = routesApp(db, fixture.companyId, service);
const [rotation, reconnect] = await Promise.all([
request(app)
.post(`/api/chat-endpoints/${endpoint.id}/setup-secret`)
.send({})
.expect(201),
request(app)
.post(`/api/chat-endpoints/${endpoint.id}/setup`)
.send({ action: "reconnect" }),
]);
expect([200, 409]).toContain(reconnect.status);
const rotatedSecret = rotation.body.webhookSecret as string;
expect(rotatedSecret).toMatch(/^[a-f0-9]{64}$/);
// The final state depends on which request acquired the lease first. A
// final reconnect must consume the complete, most recent credential set
// in either ordering.
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
rotatedSecret,
);
await service.configure(endpoint.id, { action: "reconnect" }, "owner-user");
const providerConfig = runtime.configurations.get(
endpoint.id,
)?.providerConfig;
expect(providerConfig).toMatchObject({
provider: "github",
credentials: {
appId: "123456",
installationId: 2468,
privateKey: expect.stringContaining("BEGIN PRIVATE KEY"),
webhookSecret: rotatedSecret,
},
});
const [connection] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection!.refs.map((ref) => ref.configPath).sort()).toEqual([
"credentials.appId",
"credentials.installationId",
"credentials.privateKey",
"credentials.webhookSecret",
]);
await expect(
db
.select()
.from(chatEndpointLeases)
.where(eq(chatEndpointLeases.endpointId, endpoint.id)),
).resolves.toHaveLength(0);
});
it("does not create SDK eyes reactions for accepted GitHub requests", async () => {
const fixture = await seedCompany();
let reactionTransportCalls = 0;
const { callbacks, endpoint, runtime } = await configuredGitHubEndpoint(fixture, {
receiptReactionTransportBarrier: async () => { reactionTransportCalls += 1; },
});
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:909",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks, endpointId: endpoint.id, provider: "github", thread: thread.thread,
message: makeMessage({ id: "90901", text: "@maya acknowledge this", mentioned: true }),
trigger: "mention",
});
expect(reactionTransportCalls).toBe(0);
expect(runtime.endpoints.get(endpoint.id)?.reactions).toEqual([]);
expect(await db.select().from(chatActions).where(and(
eq(chatActions.endpointId, endpoint.id), eq(chatActions.kind, "receipt_reaction"),
))).toEqual([]);
});
it("filters a durably admitted GitHub callback when rotation and reconnect supersede its runtime before deferred processing", async () => {
const fixture = await seedCompany();
const deferred: Array<() => void> = [];
const { callbacks, endpoint, runtime, service } =
await configuredGitHubEndpoint(fixture, {
deferWebhookProcessing: true,
scheduleDeferredWork: (task) => deferred.push(task),
});
const obsoleteRuntime = runtime.endpoints.get(endpoint.id);
if (!obsoleteRuntime) throw new Error("Expected initial GitHub runtime");
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:910",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: "91001",
text: "@maya this old callback must not cross the reconnect fence",
mentioned: true,
}),
trigger: "mention",
});
expect(deferred).toHaveLength(1);
const obsoleteDeliveryDrain = deferred[0];
const [delivery] = await readChatDeliveriesByEndpointId(endpoint.id);
expect(delivery.normalizedEvent).toMatchObject({
runtimeContext: {
credentialFingerprint: expect.any(String),
generation: expect.any(Number),
},
});
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
await service.configure(endpoint.id, { action: "reconnect" }, "owner-user");
await db
.update(chatDeliveries)
.set({ nextAttemptAt: new Date() })
.where(eq(chatDeliveries.id, delivery.id));
await service.processPendingDeliveries(25, delivery.id);
await expect(
db
.select({
state: chatDeliveries.state,
error: chatDeliveries.redactedError,
})
.from(chatDeliveries)
.where(eq(chatDeliveries.id, delivery.id)),
).resolves.toEqual([
{
state: "filtered",
error: "Connection activation changed before admission",
},
]);
await expect(
readChatConversationsByEndpointId(endpoint.id),
).resolves.toHaveLength(0);
expect(obsoleteRuntime.reactions).toEqual([]);
expect(runtime.endpoints.get(endpoint.id)?.reactions).toEqual([]);
// The original delivery drain is still queued. Durable ping admission also
// schedules a worker, even though the scoped setup helper has already
// processed that exact receipt; neither callback is executed in this race.
expect(deferred).toHaveLength(2);
expect(deferred[0]).toBe(obsoleteDeliveryDrain);
});
it("does not rotate a GitHub webhook secret when stored credentials cannot be resolved", async () => {
const fixture = await seedCompany();
const { endpoint, runtime, service } =
await configuredGitHubEndpoint(fixture);
await db
.update(chatEndpoints)
.set({ status: "active", setup: { step: "complete" } })
.where(eq(chatEndpoints.id, endpoint.id));
const [connectionBefore] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
const appIdRef = connectionBefore!.refs.find(
(ref) => ref.configPath === "credentials.appId",
);
if (!appIdRef) throw new Error("Expected stored GitHub App ID");
await db
.update(companySecrets)
.set({ status: "disabled" })
.where(eq(companySecrets.id, appIdRef.secretId));
await expect(
service.generateSetupSecret(endpoint.id, "owner-user"),
).rejects.toMatchObject({
status: 422,
details: { code: "secret_inactive" },
});
const [connectionAfter] = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connectionAfter!.refs).toEqual(connectionBefore!.refs);
expect(runtime.endpoints.has(endpoint.id)).toBe(true);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "active",
setup: { step: "complete", webhookSecretConfigured: true },
});
});
it("keeps GitHub issues, PR conversations, and inline review threads on distinct tasks", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint } = await configuredGitHubEndpoint(fixture);
const cases = [
{
id: "github:paperclipai/paperclip:issue:51",
rootId: "51001",
},
{ id: "github:paperclipai/paperclip:52", rootId: "52001" },
{
id: "github:paperclipai/paperclip:52:rc:88001",
rootId: "88001",
},
];
for (const item of cases) {
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: item.id,
name: "paperclipai/paperclip",
});
const rootDelivery = {
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: item.rootId,
text: `@maya handle ${item.id}`,
mentioned: true,
}),
trigger: "mention",
} as const;
await deliverMessage(rootDelivery);
if (item === cases[0]) await deliverMessage(rootDelivery);
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: `${item.rootId}-reply`,
text: "Unmentioned follow-up",
}),
trigger: "subscribed_message",
});
}
const conversations = await readChatConversationsByEndpointId(endpoint.id);
expect(conversations).toHaveLength(3);
expect(new Set(conversations.map((row) => row.issueId)).size).toBe(3);
expect(conversations.map((row) => row.externalThreadId).sort()).toEqual(
cases.map((item) => item.id).sort(),
);
const deliveries = await readChatDeliveriesByEndpointId(endpoint.id);
expect(deliveries).toHaveLength(6);
expect(deliveries.every((row) => row.state === "processed")).toBe(true);
expect(
deliveries.find((row) => row.providerEventId.endsWith(":51001")),
).toMatchObject({
normalizedEvent: { deduplication: { duplicateCount: 1 } },
});
expect(
deliveries.filter(
(row) => row.normalizedEvent.trigger === "subscribed_message",
),
).toHaveLength(3);
});
it("uses GitHub numeric user ids for linked authority and rechecks membership on every follow-up", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint, service, wakeup } =
await configuredGitHubEndpoint(fixture);
await service.update(
endpoint.id,
{ allowUnlinkedPeople: false },
"owner-user",
);
const githubUserId = "18446744073709551";
const thread = makeThread({
channelId: "paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:59",
name: "paperclipai/paperclip",
});
const send = (
id: string,
text: string,
trigger: "mention" | "subscribed_message",
) =>
deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id,
text,
mentioned: trigger === "mention",
userId: githubUserId,
userName: "octocat-renamable",
}),
trigger,
});
await send("59001", "@maya attempt before identity linking", "mention");
await send("59001", "@maya attempt before identity linking", "mention");
expect(await service.listConversations(endpoint.id)).toEqual([]);
const [principal] = await db
.select()
.from(chatExternalPrincipals)
.where(
and(
eq(chatExternalPrincipals.companyId, fixture.companyId),
eq(chatExternalPrincipals.provider, "github"),
eq(chatExternalPrincipals.externalId, githubUserId),
),
);
expect(principal).toMatchObject({ externalId: githubUserId });
const [filtered] = await readChatDeliveriesByEndpointId(endpoint.id);
expect(filtered).toMatchObject({
state: "filtered",
redactedError: "External identity must be linked to a Paperclip account",
normalizedEvent: { deduplication: { duplicateCount: 1 } },
});
const linkedUserId = `github-user-${randomUUID()}`;
const now = new Date();
await db.insert(authUsers).values({
id: linkedUserId,
name: "GitHub Linked User",
email: `${linkedUserId}@example.com`,
emailVerified: true,
createdAt: now,
updatedAt: now,
});
await db.insert(companyMemberships).values({
companyId: fixture.companyId,
principalType: "user",
principalId: linkedUserId,
status: "active",
membershipRole: "operator",
});
const intent = await service.createLinkIntent(
endpoint.id,
principal!.id,
1_800,
);
const token = new URL(intent.confirmationUrl).searchParams.get("token");
if (!token)
throw new Error("GitHub identity-link confirmation token was absent");
await service.confirmIdentityLink(token, linkedUserId);
await send("59002", "@maya authorized GitHub task", "mention");
const [conversation] = await service.listConversations(endpoint.id);
expect(conversation).toBeDefined();
await expect(
db
.select({
authorUserId: issueComments.authorUserId,
body: issueComments.body,
})
.from(issueComments)
.where(eq(issueComments.issueId, conversation!.issueId)),
).resolves.toEqual([
{
authorUserId: linkedUserId,
body: "@maya authorized GitHub task",
},
]);
expect(wakeup).toHaveBeenCalledTimes(1);
await db
.update(companyMemberships)
.set({ status: "suspended" })
.where(
and(
eq(companyMemberships.companyId, fixture.companyId),
eq(companyMemberships.principalId, linkedUserId),
),
);
await send(
"59003",
"This follow-up must be rejected now",
"subscribed_message",
);
await expect(
db
.select({ body: issueComments.body })
.from(issueComments)
.where(eq(issueComments.issueId, conversation!.issueId)),
).resolves.toEqual([{ body: "@maya authorized GitHub task" }]);
expect(wakeup).toHaveBeenCalledTimes(1);
const suspendedDelivery = await db
.select()
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(chatDeliveries.providerEventId, `${thread.thread.id}:59003`),
),
)
.then((rows) => rows[0]);
expect(suspendedDelivery).toMatchObject({
state: "filtered",
redactedError: "Linked Paperclip account is not currently permitted",
});
});
it("reorders same-second GitHub callbacks by comment id before starting the task", async () => {
const fixture = await seedCompany();
const deferred: Array<() => void> = [];
const { callbacks, endpoint, service, wakeup } =
await configuredGitHubEndpoint(fixture, {
deferWebhookProcessing: true,
scheduleDeferredWork: (task) => deferred.push(task),
});
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:71",
name: "paperclipai/paperclip",
});
const providerSentAt = new Date("2026-09-05T18:00:00.000Z");
const laterReply = makeMessage({
id: "71002",
text: "unmentioned follow-up delivered first",
});
laterReply.metadata.dateSent = providerSentAt;
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: laterReply,
trigger: "unaddressed_message",
});
const earlierMention = makeMessage({
id: "71001",
text: "@maya start the GitHub task",
mentioned: true,
});
earlierMention.metadata.dateSent = providerSentAt;
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: earlierMention,
trigger: "mention",
});
const durable = await readChatDeliveriesByEndpointId(endpoint.id);
expect(durable).toHaveLength(2);
expect(durable.every((delivery) => delivery.nextAttemptAt !== null)).toBe(
true,
);
expect(
new Set(
durable.map((delivery) => delivery.nextAttemptAt?.getTime() ?? null),
).size,
).toBe(1);
expect(deferred).toHaveLength(1);
// The production reorder window is 750 ms; allow the subsequent database
// drain to finish on loaded CI instead of sharing the default one-second budget.
deferred.shift()?.();
await vi.waitFor(async () => {
const rows = await readChatConversationsByEndpointId(endpoint.id);
expect(rows).toHaveLength(1);
}, { timeout: 10_000 });
const [conversation] = await readChatConversationsByEndpointId(endpoint.id);
await vi.waitFor(async () => {
const rows = await db
.select({ body: issueComments.body })
.from(issueComments)
.where(eq(issueComments.issueId, conversation!.issueId))
.orderBy(asc(issueComments.createdAt), asc(issueComments.id));
expect(rows.map((row) => row.body)).toEqual([
"@maya start the GitHub task",
"unmentioned follow-up delivered first",
]);
expect(wakeup).toHaveBeenCalledTimes(2);
}, { timeout: 10_000 });
await service.shutdown();
});
it("holds a GitHub follow-up that arrives after the reorder window until its older root mention arrives", async () => {
const fixture = await seedCompany();
const deferred: Array<() => void> = [];
const { callbacks, endpoint, service, wakeup } =
await configuredGitHubEndpoint(fixture, {
deferWebhookProcessing: true,
scheduleDeferredWork: (task) => deferred.push(task),
});
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:72",
name: "paperclipai/paperclip",
});
const laterReply = makeMessage({
id: "72002",
text: "follow-up delivered well before its root callback",
});
laterReply.metadata.dateSent = new Date("2026-09-05T18:00:02.000Z");
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: laterReply,
trigger: "unaddressed_message",
});
const [replyDelivery] = await readChatDeliveriesByEndpointId(endpoint.id);
await db
.update(chatDeliveries)
.set({ nextAttemptAt: new Date() })
.where(eq(chatDeliveries.id, replyDelivery!.id));
await service.processPendingDeliveries(25, replyDelivery!.id);
await expect(
readChatDeliveriesById(replyDelivery!.id),
).resolves.toEqual([
expect.objectContaining({
state: "retry",
attempts: 1,
redactedError: "Waiting briefly for an earlier root mention",
}),
]);
await expect(
readChatConversationsByEndpointId(endpoint.id),
).resolves.toHaveLength(0);
expect(wakeup).not.toHaveBeenCalled();
const earlierMention = makeMessage({
id: "72001",
text: "@maya start the delayed GitHub task",
mentioned: true,
});
earlierMention.metadata.dateSent = new Date("2026-09-05T18:00:01.000Z");
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: earlierMention,
trigger: "mention",
});
const mentionDelivery = await db
.select()
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(chatDeliveries.providerEventId, `${thread.thread.id}:72001`),
),
)
.then((rows) => rows[0]);
await db
.update(chatDeliveries)
.set({ nextAttemptAt: new Date() })
.where(eq(chatDeliveries.id, mentionDelivery!.id));
await service.processPendingDeliveries(25, mentionDelivery!.id);
const [conversation] = await readChatConversationsByEndpointId(endpoint.id);
expect(conversation).toBeDefined();
await db
.update(chatDeliveries)
.set({ nextAttemptAt: new Date() })
.where(eq(chatDeliveries.id, replyDelivery!.id));
await service.processPendingDeliveries(25, replyDelivery!.id);
const comments = await db
.select({ body: issueComments.body })
.from(issueComments)
.where(eq(issueComments.issueId, conversation!.issueId))
.orderBy(asc(issueComments.createdAt), asc(issueComments.id));
expect(comments.map((comment) => comment.body)).toEqual([
"@maya start the delayed GitHub task",
"follow-up delivered well before its root callback",
]);
expect(wakeup).toHaveBeenCalledTimes(2);
expect(deferred).toHaveLength(1);
await service.shutdown();
});
it("filters a standalone unaddressed GitHub comment after bounded orphan retention", async () => {
const fixture = await seedCompany();
const deferred: Array<() => void> = [];
const { callbacks, endpoint, service, wakeup } =
await configuredGitHubEndpoint(fixture, {
deferWebhookProcessing: true,
scheduleDeferredWork: (task) => deferred.push(task),
});
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:73",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: "73001",
text: "ordinary comment that never mentions the agent",
}),
trigger: "unaddressed_message",
});
const [delivery] = await readChatDeliveriesByEndpointId(endpoint.id);
for (let expectedAttempt = 1; expectedAttempt <= 13; expectedAttempt += 1) {
await db
.update(chatDeliveries)
.set({ nextAttemptAt: new Date() })
.where(eq(chatDeliveries.id, delivery!.id));
await service.processPendingDeliveries(25, delivery!.id);
const current = await readChatDeliveriesById(delivery!.id)
.then((rows) => rows[0]);
expect(current).toMatchObject({
state: expectedAttempt <= 12 ? "retry" : "filtered",
attempts: expectedAttempt,
});
}
await expect(
readChatConversationsByEndpointId(endpoint.id),
).resolves.toHaveLength(0);
expect(wakeup).not.toHaveBeenCalled();
expect(deferred).toHaveLength(1);
await service.shutdown();
});
it("revokes a deleted canonical GitHub installation and recovers the same endpoint after reinstall", async () => {
const fixture = await seedCompany();
const context = await configuredGitHubEndpoint(fixture);
const { callbacks, endpoint, runtime, service, webhookSecret } = context;
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:61",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: "61001",
text: "@maya establish the recoverable thread",
mentioned: true,
}),
trigger: "mention",
});
await qualifySetupRoundTrip(service, endpoint.id);
await service.test(endpoint.id);
const recovery = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError: error => { throw error; } });
onTestFinished(() => recovery.stop());
await service.registerGitHubWebhookRecoveryWork(recovery, () => true).ready;
await expect.poll(() => (recovery.nextWakeAt() ?? 0) - Date.now(), { timeout: 5000 }).toBeGreaterThan(30_000);
await expect.poll(() => idleWorkSnapshot().active).toBe(0);
const [conversation] = await readChatConversationsByEndpointId(endpoint.id);
const credentialRefsBeforeDeletion = await db
.select({ refs: toolConnections.credentialSecretRefs })
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId))
.then((rows) => rows[0]!.refs);
context.setInstallationAvailable(false);
const deleted = await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-installation-deleted",
event: "installation",
payload: {
action: "deleted",
installation: { id: 2468 },
},
webhookSecret,
}),
);
expect(deleted.status).toBe(202);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "revoked",
healthMessage: "GitHub App installation was removed",
});
await expect.poll(() => recovery.nextWakeAt(), { timeout: 5000 }).toBeNull();
await expect(
db
.select({
status: toolConnections.status,
enabled: toolConnections.enabled,
healthStatus: toolConnections.healthStatus,
refs: toolConnections.credentialSecretRefs,
})
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId)),
).resolves.toEqual([
{
status: "disabled",
enabled: false,
healthStatus: "failed",
refs: credentialRefsBeforeDeletion,
},
]);
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({ availability: "unavailable", enabled: true }),
]);
await expect(service.listConversations(endpoint.id)).resolves.toEqual([
expect.objectContaining({ id: conversation!.id, state: "unavailable" }),
]);
await expect(
db
.select({ state: chatDeliveries.state })
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(
chatDeliveries.providerEventId,
"lifecycle:github-installation-deleted",
),
),
),
).resolves.toEqual([{ state: "processed" }]);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
context.setInstallationId(8642);
context.setInstallationAvailable(true);
await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-installation-recreated",
event: "installation",
payload: {
action: "created",
installation: { id: 8642 },
},
webhookSecret,
}),
);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "active",
healthMessage: "Connected",
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "available",
enabled: true,
}),
]);
await expect(service.listConversations(endpoint.id)).resolves.toEqual([
expect.objectContaining({ id: conversation.id, state: "active" }),
]);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
const publication = await service.publishBoardMessage(
endpoint.id,
conversation.id,
"Recovered GitHub response",
"recovered-github-response",
"owner-user",
);
expect(
runtime.configurations.get(endpoint.id)?.providerConfig,
).toMatchObject({
provider: "github",
credentials: { installationId: 8642 },
});
await expect(
readChatPublicationsById(publication!.id),
).resolves.toEqual([expect.objectContaining({ state: "published" })]);
});
it("fails closed when GitHub App permissions drift before installation recovery", async () => {
const fixture = await seedCompany();
const context = await configuredGitHubEndpoint(fixture);
const { callbacks, endpoint, runtime, service, webhookSecret } = context;
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:62",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: "62001",
text: "@maya establish the permission drift thread",
mentioned: true,
}),
trigger: "mention",
});
await qualifySetupRoundTrip(service, endpoint.id);
await service.test(endpoint.id);
const [conversation] = await readChatConversationsByEndpointId(endpoint.id);
await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-installation-suspended-before-drift",
event: "installation",
payload: {
action: "suspend",
installation: { id: 2468 },
},
webhookSecret,
}),
);
context.setAppAccess({
permissions: {
issues: "read",
metadata: "read",
pull_requests: "write",
},
});
await expect(
service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-installation-unsuspended-with-drift",
event: "installation",
payload: {
action: "unsuspend",
installation: { id: 2468 },
},
webhookSecret,
}),
),
).rejects.toMatchObject({
status: 422,
details: {
code: "chat_provider_permissions_missing",
provider: "github",
missingPermissions: ["issues"],
},
});
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
healthMessage:
"GitHub App credentials, permissions, events, or identity need attention",
lastError: expect.stringContaining("issues"),
});
const [connection] = await db
.select({
status: toolConnections.status,
enabled: toolConnections.enabled,
healthStatus: toolConnections.healthStatus,
})
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connection).toEqual({
status: "disabled",
enabled: false,
healthStatus: "degraded",
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({ availability: "unavailable", enabled: true }),
]);
await expect(service.listConversations(endpoint.id)).resolves.toEqual([
expect.objectContaining({ id: conversation!.id, state: "unavailable" }),
]);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
const lifecycleDelivery = await db
.select()
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(
chatDeliveries.providerEventId,
"lifecycle:github-installation-unsuspended-with-drift",
),
),
)
.then((rows) => rows[0]);
expect(lifecycleDelivery).toMatchObject({
state: "retry",
redactedError: expect.stringContaining("issues"),
});
});
it("fails closed when the GitHub installation has not approved the App permissions", async () => {
const fixture = await seedCompany();
const context = await configuredGitHubEndpoint(fixture);
const { callbacks, endpoint, service, webhookSecret } = context;
const thread = makeThread({
channelId: "github:paperclipai/paperclip",
id: "github:paperclipai/paperclip:issue:63",
name: "paperclipai/paperclip",
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
provider: "github",
thread: thread.thread,
message: makeMessage({
id: "63001",
text: "@maya establish the installation permission thread",
mentioned: true,
}),
trigger: "mention",
});
await qualifySetupRoundTrip(service, endpoint.id);
await service.test(endpoint.id);
context.setInstallationAccess({
issues: "read",
metadata: "read",
pull_requests: "write",
});
await expect(
service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-installation-permission-approval-pending",
event: "installation",
payload: {
action: "new_permissions_accepted",
installation: { id: 2468 },
},
webhookSecret,
}),
),
).rejects.toMatchObject({
status: 422,
details: { code: "chat_provider_inventory_failed" },
});
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
healthMessage:
"GitHub App credentials, permissions, events, or identity need attention",
lastError: expect.stringContaining(
"active installation has not granted the required access for: issues",
),
});
const lifecycleDelivery = await db
.select()
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(
chatDeliveries.providerEventId,
"lifecycle:github-installation-permission-approval-pending",
),
),
)
.then((rows) => rows[0]);
expect(lifecycleDelivery).toMatchObject({
state: "retry",
redactedError: expect.stringContaining("issues"),
});
});
it("reconciles concurrent opposite GitHub lifecycle callbacks from canonical App state", async () => {
const fixture = await seedCompany();
const context = await configuredGitHubEndpoint(fixture);
const { endpoint, runtime, service, webhookSecret } = context;
const repository = {
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
};
const send = (delivery: string, event: string, payload: unknown) =>
service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery,
event,
payload,
webhookSecret,
}),
);
context.setRepositories([]);
await send(
"github-repositories-canonical-empty",
"installation_repositories",
{
action: "removed",
repositories_added: [],
repositories_removed: [repository],
},
);
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "removed",
enabled: true,
}),
]);
context.setRepositories([repository]);
await Promise.all([
send("github-installation-late-suspend", "installation", {
action: "suspend",
installation: { id: 2468 },
}),
send("github-installation-current-unsuspend", "installation", {
action: "unsuspend",
installation: { id: 2468 },
}),
send("github-repositories-late-remove", "installation_repositories", {
action: "removed",
repositories_added: [],
repositories_removed: [repository],
}),
send("github-repositories-current-add", "installation_repositories", {
action: "added",
repositories_added: [repository],
repositories_removed: [],
}),
]);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "verifying",
healthMessage: "Waiting for a test conversation",
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "available",
enabled: true,
}),
]);
expect(runtime.endpoints.has(endpoint.id)).toBe(true);
});
it("does not let GitHub lifecycle recovery undo a webhook-secret rotation", async () => {
const fixture = await seedCompany();
const {
endpoint,
runtime,
service,
webhookSecret: oldWebhookSecret,
} = await configuredGitHubEndpoint(fixture);
const providerRuntime = runtime.endpoints.get(endpoint.id);
if (!providerRuntime) throw new Error("Expected GitHub runtime");
let releaseWebhook!: () => void;
let markWebhookEntered!: () => void;
const webhookEntered = new Promise<void>((resolve) => {
markWebhookEntered = resolve;
});
const webhookRelease = new Promise<void>((resolve) => {
releaseWebhook = resolve;
});
providerRuntime.webhookHook = async () => {
markWebhookEntered();
await webhookRelease;
};
const body = JSON.stringify({
action: "unsuspend",
installation: { id: 2468 },
});
const signature = createHmac("sha256", oldWebhookSecret)
.update(body)
.digest("hex");
const staleCallback = service.handleWebhook(
endpoint.publicId,
"github",
new Request("https://paperclip.example/github", {
method: "POST",
headers: {
"content-type": "application/json",
"x-github-event": "installation",
"x-github-delivery": "github-available-during-secret-rotation",
"x-hub-signature-256": `sha256=${signature}`,
},
body,
}),
);
await webhookEntered;
await service.generateSetupSecret(endpoint.id, "owner-user");
releaseWebhook();
await expect(staleCallback).resolves.toMatchObject({ status: 202 });
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
setup: { step: "provider_setup", webhookVerifiedAt: null },
});
const [connection] = await readToolConnectionsById(endpoint.connectionId);
expect(connection).toMatchObject({
status: "disabled",
enabled: false,
healthStatus: "degraded",
});
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
const [delivery] = await db
.select()
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(
chatDeliveries.providerEventId,
"lifecycle:github-available-during-secret-rotation",
),
),
);
expect(delivery).toMatchObject({
state: "filtered",
redactedError:
"Provider lifecycle callback belonged to a superseded runtime",
});
});
it("does not let an obsolete GitHub webhook secret reopen a repository during rotation", async () => {
const fixture = await seedCompany();
let blockWebhookAuthentication = false;
let blockCredentialPersistence = false;
let markWebhookReady!: () => void;
let releaseWebhook!: () => void;
let markRotationReady!: () => void;
let releaseRotation!: () => void;
const webhookReady = new Promise<void>((resolve) => {
markWebhookReady = resolve;
});
const webhookRelease = new Promise<void>((resolve) => {
releaseWebhook = resolve;
});
const rotationReady = new Promise<void>((resolve) => {
markRotationReady = resolve;
});
const rotationRelease = new Promise<void>((resolve) => {
releaseRotation = resolve;
});
const {
endpoint,
service,
webhookSecret: oldWebhookSecret,
} = await configuredGitHubEndpoint(fixture, {
githubWebhookAuthenticationBarrier: async () => {
if (!blockWebhookAuthentication) return;
markWebhookReady();
await webhookRelease;
},
setupSecretCredentialPersistBarrier: async () => {
if (!blockCredentialPersistence) return;
markRotationReady();
await rotationRelease;
},
});
blockWebhookAuthentication = true;
const staleCallback = service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-repository-during-secret-rotation",
event: "issue_comment",
payload: {
action: "created",
installation: { id: 2468 },
repository: {
id: 86420,
full_name: "paperclipai/stale-secret-repository",
html_url: "https://github.com/paperclipai/stale-secret-repository",
owner: { id: 1357, login: "paperclipai" },
private: false,
},
issue: { number: 7 },
comment: { id: 7001, body: "obsolete credential callback" },
sender: { id: 42, login: "octocat" },
},
webhookSecret: oldWebhookSecret,
}),
);
await webhookReady;
blockCredentialPersistence = true;
const rotation = service.generateSetupSecret(endpoint.id, "owner-user");
await rotationReady;
releaseWebhook();
releaseRotation();
await rotation;
// The callback authenticated before rotation won the mutation lease, but
// its repository was never enabled. It may be rejected as obsolete or
// acknowledged as out of scope; either way it must not mutate inventory.
await expect(staleCallback).resolves.toMatchObject({ status: 200 });
await expect(service.listResources(endpoint.id)).resolves.not.toEqual(
expect.arrayContaining([
expect.objectContaining({
providerResourceId: "paperclipai/stale-secret-repository",
}),
]),
);
});
it("acknowledges GitHub lifecycle callbacks without changing a paused endpoint", async () => {
const fixture = await seedCompany();
const { endpoint, service } = await configuredGitHubEndpoint(fixture);
const current = await db
.select({ setup: chatEndpoints.setup })
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id))
.then((rows) => rows[0]!);
await db
.update(chatEndpoints)
.set({ status: "active", setup: { ...current.setup, step: "complete" } })
.where(eq(chatEndpoints.id, endpoint.id));
await service.configure(endpoint.id, { action: "pause" }, "owner-user");
const before = await db
.select({ id: chatDeliveries.id })
.from(chatDeliveries)
.where(eq(chatDeliveries.endpointId, endpoint.id))
.then((rows) => rows.length);
const response = await service.handleWebhook(
endpoint.publicId,
"github",
new Request("https://paperclip.example/github", {
method: "POST",
headers: {
"content-type": "application/json",
"x-github-event": "installation",
"x-github-delivery": "github-available-while-paused",
},
body: JSON.stringify({
action: "unsuspend",
installation: { id: 2468 },
}),
}),
);
expect(response.status).toBe(200);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "paused",
});
await expect(
db
.select({ id: chatDeliveries.id })
.from(chatDeliveries)
.where(eq(chatDeliveries.endpointId, endpoint.id))
.then((rows) => rows.length),
).resolves.toBe(before);
});
it("reconciles a foreign GitHub installation lifecycle event against canonical App inventory", async () => {
const fixture = await seedCompany();
const { endpoint, runtime, service, webhookSecret } =
await configuredGitHubEndpoint(fixture);
const setup = await db
.select({ setup: chatEndpoints.setup })
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id))
.then((rows) => rows[0]!.setup);
await db
.update(chatEndpoints)
.set({ status: "active", setup: { ...setup, step: "complete" } })
.where(eq(chatEndpoints.id, endpoint.id));
const providerRuntime = runtime.endpoints.get(endpoint.id);
if (!providerRuntime) throw new Error("Expected GitHub runtime");
providerRuntime.webhookRequest = null;
const payload = JSON.stringify({
action: "deleted",
installation: { id: 9999 },
});
const signature = createHmac("sha256", webhookSecret)
.update(payload)
.digest("hex");
const response = await service.handleWebhook(
endpoint.publicId,
"github",
new Request("https://paperclip.example/github", {
method: "POST",
headers: {
"content-type": "application/json",
"x-github-event": "installation",
"x-github-delivery": "foreign-installation-deleted",
"x-hub-signature-256": `sha256=${signature}`,
},
body: payload,
}),
);
expect(response.status).toBe(202);
expect(providerRuntime.webhookRequest).not.toBeNull();
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "active",
setup: { step: "complete" },
});
await expect(
readChatDeliveriesByEndpointId(endpoint.id),
).resolves.toEqual([
expect.objectContaining({
providerEventId: "lifecycle:foreign-installation-deleted",
state: "processed",
}),
]);
});
it("quarantines an active GitHub endpoint when its dedicated App drifts to multiple installations", async () => {
const fixture = await seedCompany();
const context = await configuredGitHubEndpoint(fixture);
const { endpoint, runtime, service, webhookSecret } = context;
const setup = await db
.select({ setup: chatEndpoints.setup })
.from(chatEndpoints)
.where(eq(chatEndpoints.id, endpoint.id))
.then((rows) => rows[0]!.setup);
await db
.update(chatEndpoints)
.set({ status: "active", setup: { ...setup, step: "complete" } })
.where(eq(chatEndpoints.id, endpoint.id));
context.setAdditionalInstallationIds([9999]);
await expect(
service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-second-installation-created",
event: "installation",
payload: {
action: "created",
installation: { id: 9999 },
},
webhookSecret,
}),
),
).rejects.toMatchObject({
status: 422,
details: { code: "chat_provider_inventory_failed" },
});
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "attention",
healthMessage:
"GitHub App credentials, permissions, events, or identity need attention",
lastError: expect.stringContaining("exactly one active installation"),
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "unavailable",
}),
]);
expect(runtime.endpoints.has(endpoint.id)).toBe(false);
const [failedLifecycle] = await db
.select()
.from(chatDeliveries)
.where(
and(
eq(chatDeliveries.endpointId, endpoint.id),
eq(
chatDeliveries.providerEventId,
"lifecycle:github-second-installation-created",
),
),
);
expect(failedLifecycle).toMatchObject({
state: "retry",
redactedError: expect.stringContaining("exactly one active installation"),
});
context.setAdditionalInstallationIds([]);
const recovered = await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: "github-second-installation-removed",
event: "installation",
payload: {
action: "deleted",
installation: { id: 9999 },
},
webhookSecret,
}),
);
expect(recovered.status).toBe(202);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "active",
healthMessage: "Connected",
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
availability: "available",
}),
]);
});
it("accepts only signed GitHub setup pings before App credentials exist", async () => {
const fixture = await seedCompany();
const runtime = new FakeChatSdkRuntime();
const { service } = createService(runtime);
const endpoint = await service.create(
fixture.companyId,
{
provider: "github",
assignedAgentId: fixture.assignedAgentId,
name: "Maya GitHub setup ping",
},
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
const body = JSON.stringify({ zen: "Keep it logically awesome." });
const signature = createHmac("sha256", webhookSecret)
.update(body)
.digest("hex");
const setupPing = (signatureHeader?: string) =>
service.handleWebhook(
endpoint.publicId,
"github",
new Request("https://paperclip.example/github", {
method: "POST",
headers: {
"content-type": "application/json",
"x-github-event": "ping",
...(signatureHeader
? { "x-hub-signature-256": signatureHeader }
: {}),
},
body,
}),
);
await expect(setupPing()).resolves.toMatchObject({ status: 401 });
await expect(setupPing("sha256=invalid")).resolves.toMatchObject({
status: 401,
});
const accepted = await setupPing(`sha256=${signature}`);
expect(accepted.status).toBe(200);
await expect(accepted.text()).resolves.toBe("pong");
expect(runtime.configurations.has(endpoint.id)).toBe(false);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "draft",
healthMessage: "GitHub webhook verified",
setup: {
webhookSecretConfigured: true,
webhookVerifiedAt: expect.any(String),
},
});
});
it("ignores a signed GitHub installation before App credentials exist", async () => {
const fixture = await seedCompany();
const runtime = new FakeChatSdkRuntime();
const { service } = createService(runtime);
const endpoint = await service.create(
fixture.companyId,
{
provider: "github",
assignedAgentId: fixture.assignedAgentId,
name: "Maya GitHub pre-key installation",
},
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(
endpoint.id,
"owner-user",
);
await recordGitHubWebhookVerification(
service,
endpoint.publicId,
webhookSecret,
);
const endpointBeforeInstallation = await service.get(endpoint.id);
const webhookVerifiedAt =
endpointBeforeInstallation.setup?.webhookVerifiedAt;
expect(webhookVerifiedAt).toEqual(expect.any(String));
const [connectionBeforeInstallation] = await db
.select({
enabled: toolConnections.enabled,
refs: toolConnections.credentialSecretRefs,
status: toolConnections.status,
})
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
const deliveryId = `github-pre-key-installation-${randomUUID()}`;
const response = await service.handleWebhook(
endpoint.publicId,
"github",
signedGitHubWebhookRequest({
delivery: deliveryId,
event: "installation",
payload: {
action: "created",
installation: {
id: 987_654_321,
account: { id: 1, login: "paperclip-e2e", type: "User" },
permissions: {
issues: "write",
metadata: "read",
pull_requests: "write",
},
suspended_at: null,
},
},
webhookSecret,
}),
);
expect(response.status).toBe(200);
await expect(response.text()).resolves.toBe("ignored");
expect(runtime.configurations.has(endpoint.id)).toBe(false);
await expect(service.get(endpoint.id)).resolves.toMatchObject({
status: "draft",
providerAccountId: null,
botExternalId: null,
healthMessage: "GitHub webhook verified",
setup: {
step: "provider_setup",
webhookSecretConfigured: true,
webhookVerifiedAt,
},
});
await expect(service.listResources(endpoint.id)).resolves.toEqual([]);
await expect(service.listConversations(endpoint.id)).resolves.toEqual([]);
await expect(
db
.select({ id: chatActions.id })
.from(chatActions)
.where(
and(
eq(chatActions.endpointId, endpoint.id),
eq(chatActions.kind, "github_webhook_ingress"),
eq(
chatActions.providerActionId,
`github_webhook_ingress:${deliveryId}`,
),
),
),
).resolves.toEqual([]);
const [connectionAfterInstallation] = await db
.select({
enabled: toolConnections.enabled,
refs: toolConnections.credentialSecretRefs,
status: toolConnections.status,
})
.from(toolConnections)
.where(eq(toolConnections.id, endpoint.connectionId));
expect(connectionAfterInstallation).toEqual(connectionBeforeInstallation);
expect(connectionAfterInstallation).toMatchObject({
enabled: false,
refs: [
expect.objectContaining({
configPath: "credentials.webhookSecret",
}),
],
status: "draft",
});
});
it("recovers from an empty initial GitHub repository inventory on retry without prematurely closing initial import", async () => {
const fixture = await seedCompany();
const appRegistrationId = Number.parseInt(
fixture.companyId.replaceAll("-", "").slice(0, 8),
16,
);
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const installationId = 45678;
let repositories: Array<{
id: number;
full_name: string;
html_url: string;
owner: { id: number; login: string };
private: boolean;
}> = [];
const providerFetch = (async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return Response.json({
id: appRegistrationId,
slug: `maya-${fixture.companyId.slice(0, 8)}`,
name: "Maya Paperclip",
owner: { login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
events: ["pull_request", "github_app_authorization", "installation", "installation_repositories", "issue_comment", "pull_request_review_comment"],
});
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return Response.json([{
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
}]);
}
if (url === `https://api.github.com/app/installations/${installationId}`) {
return Response.json({
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
});
}
if (url === `https://api.github.com/app/installations/${installationId}/access_tokens`) {
return Response.json({ token: "installation-token" }, { status: 201 });
}
if (url === "https://api.github.com/installation/repositories?per_page=100&page=1") {
return Response.json({ repositories });
}
if (url === "https://api.github.com/app/hook/deliveries?per_page=20") {
return Response.json([]);
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId, name: "Maya GitHub" },
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(endpoint.id, "owner-user");
await service.storeGitHubApp(
endpoint.id,
"owner-user",
{ appId: String(appRegistrationId), privateKey, webhookSecret },
"existing_app",
);
await recordGitHubWebhookVerification(service, endpoint.publicId, webhookSecret);
const management = (await import("../../services/chat-github-management.js")).githubChatManagementService(db, providerFetch);
await management.linkObservedIdentity(endpoint.id, "owner-user", {
githubUserId: "42",
login: "octocat",
avatarUrl: null,
});
// First advance: GitHub reports 0 repositories (e.g. none granted yet or propagation lag).
const first = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(first.state).toBe("verify");
expect(first.managementUrl).toBe(`https://github.com/organizations/paperclipai/settings/installations/${installationId}`);
expect(first.verification?.checks.find((check) => check.key === "repositories")).toMatchObject({
ok: false,
code: "no_installation_repositories",
});
const afterFirst = await service.get(endpoint.id);
expect(afterFirst.setup.github?.initialRepositoryImportPending).toBe(true);
expect(afterFirst.setup.github?.repositorySelectionSaved).not.toBe(true);
// A direct empty initialGitHubImport call must not prematurely close initial import.
await service.replaceResources(endpoint.id, [], "owner-user", { initialGitHubImport: true });
const afterEmptyImport = await service.get(endpoint.id);
expect(afterEmptyImport.setup.github?.initialRepositoryImportPending).toBe(true);
expect(afterEmptyImport.setup.github?.repositorySelectionSaved).not.toBe(true);
// User grants one repository on GitHub and clicks Try again (advance).
repositories = [{
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
}];
const second = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(second.state).toBe("connected");
const resources = await service.listResources(endpoint.id);
expect(resources).toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
enabled: true,
availability: "available",
}),
]);
await service.shutdown();
});
it("keeps explicitly disabled repositories disabled on wizard retry and distinguishes Paperclip access from GitHub installation access", async () => {
const fixture = await seedCompany();
const configured = await configuredGitHubEndpoint(fixture, {}, true, false, true);
const { service, endpoint, providerFetch } = configured;
const management = (await import("../../services/chat-github-management.js")).githubChatManagementService(db, providerFetch);
await management.linkObservedIdentity(endpoint.id, "owner-user", {
githubUserId: "42",
login: "octocat",
avatarUrl: null,
});
// Explicitly save an all-disabled selection.
await service.toggleAllGitHubRepositories(endpoint.id, false, "owner-user");
const progress = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(progress.state).toBe("verify");
expect(progress.managementUrl).toBe("https://github.com/organizations/paperclipai/settings/installations/2468");
expect(progress.verification?.checks.find((check) => check.key === "repositories")).toMatchObject({
ok: false,
code: "no_enabled_repositories",
});
const resources = await service.listResources(endpoint.id);
expect(resources.every((resource) => !resource.enabled)).toBe(true);
await service.shutdown();
});
it("imports GitHub App installation repositories even when review configuration was pre-saved before installation, and preserves that configuration", async () => {
const fixture = await seedCompany();
const appRegistrationId = Number.parseInt(
fixture.companyId.replaceAll("-", "").slice(0, 8),
16,
);
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const installationId = 56789;
const providerFetch = (async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return Response.json({
id: appRegistrationId,
slug: `storie-${fixture.companyId.slice(0, 8)}`,
name: "Storie Visual Review",
owner: { login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
events: ["pull_request", "github_app_authorization", "installation", "installation_repositories", "issue_comment", "pull_request_review_comment"],
});
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return Response.json([{
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
}]);
}
if (url === `https://api.github.com/app/installations/${installationId}`) {
return Response.json({
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
});
}
if (url === `https://api.github.com/app/installations/${installationId}/access_tokens`) {
return Response.json({ token: "installation-token" }, { status: 201 });
}
if (url === "https://api.github.com/installation/repositories?per_page=100&page=1") {
return Response.json({
repositories: [{
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
}],
});
}
if (url === "https://api.github.com/app/hook/deliveries?per_page=20") {
return Response.json([]);
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId, name: "Storie Visual Review" },
"owner-user",
);
const management = (await import("../../services/chat-github-management.js")).githubChatManagementService(db, providerFetch);
// Pre-save custom review configuration (revision becomes 1) BEFORE the GitHub App is installed.
const initialConfig = await management.configuration(endpoint.id, "owner-user");
const customInstructions = "Check every PR for visual regressions and deploy a Storybook preview.";
await management.saveConfiguration(
endpoint.id,
{
expectedRevision: initialConfig.revision,
configuration: {
...initialConfig.configuration,
toolsEnabled: true,
defaults: {
...initialConfig.configuration.defaults,
invocation: "linked_authors",
events: ["opened", "synchronize"],
instructions: customInstructions,
},
},
},
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(endpoint.id, "owner-user");
await service.storeGitHubApp(
endpoint.id,
"owner-user",
{ appId: String(appRegistrationId), privateKey, webhookSecret },
"existing_app",
);
await recordGitHubWebhookVerification(service, endpoint.publicId, webhookSecret);
await management.linkObservedIdentity(endpoint.id, "owner-user", {
githubUserId: "42",
login: "octocat",
avatarUrl: null,
});
const result = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(result.state).toBe("connected");
const resources = await service.listResources(endpoint.id);
expect(resources).toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
enabled: true,
availability: "available",
}),
]);
const finalConfig = await management.configuration(endpoint.id, "owner-user");
expect(finalConfig.revision).toBe(1);
expect(finalConfig.configuration.defaults.instructions).toBe(customInstructions);
expect(finalConfig.configuration.defaults.invocation).toBe("linked_authors");
expect(finalConfig.configuration.defaults.events).toEqual(["opened", "synchronize"]);
const updatedEndpoint = await service.get(endpoint.id);
expect(updatedEndpoint.setup.github?.repositorySelectionSaved).toBe(true);
expect(updatedEndpoint.setup.github?.initialRepositoryImportPending).toBe(false);
await service.shutdown();
});
it("recovers a verifying endpoint whose initialRepositoriesImported was marked true while initialRepositoryImportPending remained true", async () => {
const fixture = await seedCompany();
const appRegistrationId = Number.parseInt(
fixture.companyId.replaceAll("-", "").slice(0, 8),
16,
);
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const installationId = 67890;
const providerFetch = (async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return Response.json({
id: appRegistrationId,
slug: `storie-rec-${fixture.companyId.slice(0, 8)}`,
name: "Storie Visual Review",
owner: { login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
events: ["pull_request", "github_app_authorization", "installation", "installation_repositories", "issue_comment", "pull_request_review_comment"],
});
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return Response.json([{
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
}]);
}
if (url === `https://api.github.com/app/installations/${installationId}`) {
return Response.json({
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
});
}
if (url === `https://api.github.com/app/installations/${installationId}/access_tokens`) {
return Response.json({ token: "installation-token" }, { status: 201 });
}
if (url === "https://api.github.com/installation/repositories?per_page=100&page=1") {
return Response.json({
repositories: [{
id: 97531,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
}],
});
}
if (url === "https://api.github.com/app/hook/deliveries?per_page=20") {
return Response.json([]);
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId, name: "Storie Visual Review" },
"owner-user",
);
const management = (await import("../../services/chat-github-management.js")).githubChatManagementService(db, providerFetch);
const initialConfig = await management.configuration(endpoint.id, "owner-user");
await management.saveConfiguration(
endpoint.id,
{
expectedRevision: initialConfig.revision,
configuration: {
...initialConfig.configuration,
toolsEnabled: true,
defaults: {
...initialConfig.configuration.defaults,
invocation: "linked_authors",
events: ["opened"],
},
},
},
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(endpoint.id, "owner-user");
await service.storeGitHubApp(
endpoint.id,
"owner-user",
{ appId: String(appRegistrationId), privateKey, webhookSecret },
"existing_app",
);
await recordGitHubWebhookVerification(service, endpoint.publicId, webhookSecret);
await management.linkObservedIdentity(endpoint.id, "owner-user", {
githubUserId: "42",
login: "octocat",
avatarUrl: null,
});
// Simulate the exact half-state left behind by the old bug before this fix:
// repositories were discovered (enabled: false), status became verifying,
// initialRepositoriesImported was set to true while initialRepositoryImportPending
// stayed true and repositorySelectionSaved was never set.
await service.refreshGitHubRepositories(endpoint.id, "owner-user");
await db
.update(chatEndpoints)
.set({
status: "verifying",
setup: sql`jsonb_set(${chatEndpoints.setup}, '{github}', coalesce(${chatEndpoints.setup}->'github', '{}'::jsonb) || '{"initialRepositoriesImported":true,"initialRepositoryImportPending":true,"initialSetupPending":false}'::jsonb)`,
})
.where(eq(chatEndpoints.id, endpoint.id));
const result = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(result.state).toBe("connected");
const resources = await service.listResources(endpoint.id);
expect(resources).toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
enabled: true,
availability: "available",
}),
]);
await service.shutdown();
});
it("returns installation_missing with sanitized URLs when a verifying installation was removed on GitHub (HTTP 404)", async () => {
const fixture = await seedCompany();
const appRegistrationId = Number.parseInt(
fixture.companyId.replaceAll("-", "").slice(0, 8),
16,
);
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const installationId = 78901;
let installationRemoved = false;
const providerFetch = (async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return Response.json({
id: appRegistrationId,
slug: `maya-404-${fixture.companyId.slice(0, 8)}`,
name: "Maya Removed Install",
owner: { login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
events: ["pull_request", "github_app_authorization", "installation", "installation_repositories", "issue_comment", "pull_request_review_comment"],
});
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return Response.json(
installationRemoved
? []
: [{
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
}],
);
}
if (url === `https://api.github.com/app/installations/${installationId}`) {
if (installationRemoved) {
return Response.json({ message: "Not Found" }, { status: 404 });
}
return Response.json({
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
});
}
if (url === `https://api.github.com/app/installations/${installationId}/access_tokens`) {
if (installationRemoved) {
return Response.json({ message: "Not Found" }, { status: 404 });
}
return Response.json({ token: "installation-token" }, { status: 201 });
}
if (url === "https://api.github.com/installation/repositories?per_page=100&page=1") {
return Response.json({ repositories: [] });
}
if (url === "https://api.github.com/app/hook/deliveries?per_page=20") {
return Response.json([]);
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId, name: "Maya Removed Install" },
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(endpoint.id, "owner-user");
await service.storeGitHubApp(
endpoint.id,
"owner-user",
{ appId: String(appRegistrationId), privateKey, webhookSecret },
"existing_app",
);
await recordGitHubWebhookVerification(service, endpoint.publicId, webhookSecret);
const management = (await import("../../services/chat-github-management.js")).githubChatManagementService(db, providerFetch);
await management.linkObservedIdentity(endpoint.id, "owner-user", {
githubUserId: "42",
login: "octocat",
avatarUrl: null,
});
// Complete initial refresh and move to verifying (0 repositories).
const beforeRemove = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(beforeRemove.state).toBe("verify");
// Simulate an unsafe stored managementUrl alongside a removed installation on GitHub.
installationRemoved = true;
await db
.update(chatEndpoints)
.set({
setup: sql`jsonb_set(${chatEndpoints.setup}, '{github}', coalesce(${chatEndpoints.setup}->'github', '{}'::jsonb) || '{"managementUrl":"https://evil.example/settings/installations/78901"}'::jsonb)`,
})
.where(eq(chatEndpoints.id, endpoint.id));
const result = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(result.state).toBe("verify");
expect(result.installationUrl).toBe(
`https://github.com/apps/maya-404-${fixture.companyId.slice(0, 8)}/installations/new`,
);
// Unsafe stored managementUrl is rejected server-side; derived GitHub URL is returned instead.
expect(result.managementUrl).toBe(
`https://github.com/settings/installations/${installationId}`,
);
expect(result.verification?.checks.find((check) => check.key === "permissions")).toMatchObject({
ok: false,
code: "installation_missing",
});
expect(result.verification?.checks.find((check) => check.key === "repositories")).toMatchObject({
ok: false,
code: "permissions_required",
});
await service.shutdown();
});
it("surfaces a retryable repository sync error instead of no_installation_repositories on transient inventory 503 and recovers on retry", async () => {
const fixture = await seedCompany();
const appRegistrationId = Number.parseInt(
fixture.companyId.replaceAll("-", "").slice(0, 8),
16,
);
const privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 })
.privateKey.export({ type: "pkcs8", format: "pem" })
.toString();
const installationId = 78902;
let inventoryStatus = 200;
let repositories: Array<{
id: number;
full_name: string;
html_url: string;
owner: { id: number; login: string };
private: boolean;
}> = [];
const providerFetch = (async (input: string | URL | Request) => {
const url = String(input);
if (url === "https://api.github.com/app") {
return Response.json({
id: appRegistrationId,
slug: `maya-503-${fixture.companyId.slice(0, 8)}`,
name: "Maya Transient Inventory",
owner: { login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
events: ["pull_request", "github_app_authorization", "installation", "installation_repositories", "issue_comment", "pull_request_review_comment"],
});
}
if (url === "https://api.github.com/app/installations?per_page=100") {
return Response.json([{
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
}]);
}
if (url === `https://api.github.com/app/installations/${installationId}`) {
return Response.json({
id: installationId,
html_url: `https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
account: { id: 1357, login: "paperclipai", type: "Organization" },
permissions: { contents: "read", checks: "write", issues: "write", metadata: "read", pull_requests: "write" },
suspended_at: null,
});
}
if (url === `https://api.github.com/app/installations/${installationId}/access_tokens`) {
return Response.json({ token: "installation-token" }, { status: 201 });
}
if (url === "https://api.github.com/installation/repositories?per_page=100&page=1") {
if (inventoryStatus >= 400) {
return Response.json({ message: "Service Unavailable" }, { status: inventoryStatus });
}
return Response.json({ repositories });
}
if (url === "https://api.github.com/app/hook/deliveries?per_page=20") {
return Response.json([]);
}
throw new Error(`Unexpected provider request: ${url}`);
}) as typeof globalThis.fetch;
const { service } = createService(new FakeChatSdkRuntime(), providerFetch);
const endpoint = await service.create(
fixture.companyId,
{ provider: "github", assignedAgentId: fixture.assignedAgentId, name: "Maya Transient Inventory" },
"owner-user",
);
const { webhookSecret } = await service.generateSetupSecret(endpoint.id, "owner-user");
await service.storeGitHubApp(
endpoint.id,
"owner-user",
{ appId: String(appRegistrationId), privateKey, webhookSecret },
"existing_app",
);
await recordGitHubWebhookVerification(service, endpoint.publicId, webhookSecret);
const management = (await import("../../services/chat-github-management.js")).githubChatManagementService(db, providerFetch);
await management.linkObservedIdentity(endpoint.id, "owner-user", {
githubUserId: "42",
login: "octocat",
avatarUrl: null,
});
// Move the endpoint into verifying state with an empty cache first.
const initialVerify = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(initialVerify.state).toBe("verify");
expect(initialVerify.verification?.checks.find((check) => check.key === "repositories")).toMatchObject({
ok: false,
code: "no_installation_repositories",
});
// Now GitHub's /installation/repositories endpoint returns a transient 503
// while /app and /app/installations/:id continue returning valid permissions.
inventoryStatus = 503;
const failedSync = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(failedSync.state).toBe("verify");
expect(failedSync.managementUrl).toBe(
`https://github.com/organizations/paperclipai/settings/installations/${installationId}`,
);
expect(failedSync.verification?.checks.find((check) => check.key === "permissions")).toMatchObject({
ok: true,
});
expect(failedSync.verification?.checks.find((check) => check.key === "repositories")).toMatchObject({
ok: false,
code: "repositories_sync_failed",
});
expect(failedSync.verification?.checks.find((check) => check.key === "repositories")?.detail).not.toContain(
"Grant access to at least one repository",
);
const afterFailedSync = await service.get(endpoint.id);
expect(afterFailedSync.setup.github?.initialRepositoryImportPending).toBe(true);
expect(afterFailedSync.setup.github?.repositorySelectionSaved).not.toBe(true);
// Follow up with a successful retry once GitHub recovers.
inventoryStatus = 200;
repositories = [{
id: 97532,
full_name: "paperclipai/paperclip",
html_url: "https://github.com/paperclipai/paperclip",
owner: { id: 1357, login: "paperclipai" },
private: false,
}];
const recovered = await service.githubWizard.advance(endpoint.id, "owner-user");
expect(recovered.state).toBe("connected");
const resources = await service.listResources(endpoint.id);
expect(resources).toEqual([
expect.objectContaining({
providerResourceId: "paperclipai/paperclip",
enabled: true,
availability: "available",
}),
]);
await service.shutdown();
});
it("strips workspace overrides from GitHub execution defaults on read, write, and task creation while preserving project defaults", async () => {
const previousExperimental = await instanceSettingsService(db).getExperimental();
onTestFinished(async () => {
await instanceSettingsService(db).updateExperimental({
enableIsolatedWorkspaces: previousExperimental.enableIsolatedWorkspaces,
});
});
await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true });
const fixture = await seedCompany();
const { callbacks, endpoint, service } = await configuredGitHubEndpoint(fixture);
const [project] = await db
.insert(projects)
.values({
companyId: fixture.companyId,
name: "GitHub Review Project",
status: "in_progress",
executionWorkspacePolicy: {
enabled: true,
defaultMode: "isolated_workspace",
},
})
.returning();
const [projectWorkspace] = await db
.insert(projectWorkspaces)
.values({
companyId: fixture.companyId,
projectId: project!.id,
name: "paperclipai/paperclip",
sourceType: "git_repo",
repoUrl: "https://github.com/paperclipai/paperclip",
isPrimary: true,
})
.returning();
const [resource] = await service.listResources(endpoint.id);
expect(resource).toBeDefined();
const updatedEndpoint = await service.update(
endpoint.id,
{
executionDefaults: {
projectId: project!.id,
workspace: { kind: "task_directory" },
},
},
"owner-user",
);
expect(updatedEndpoint.executionDefaults).toEqual({ projectId: project!.id });
await service.replaceResources(
endpoint.id,
[
{
id: resource!.id,
executionDefaults: {
projectId: null,
workspace: { kind: "task_directory" },
},
},
],
"owner-user",
);
expect((await service.listResources(endpoint.id)).find((row) => row.id === resource!.id)?.executionDefaults).toEqual({
projectId: null,
});
expect(
(await service.listGitHubRepositories(endpoint.id, { limit: 20, offset: 0, search: "" })).items.find(
(row) => row.id === resource!.id,
)?.executionDefaults,
).toEqual({ projectId: null });
await db
.update(chatEndpoints)
.set({
executionDefaults: {
projectId: project!.id,
workspace: { kind: "task_directory" },
},
})
.where(eq(chatEndpoints.id, endpoint.id));
await db
.update(chatEndpointResources)
.set({
executionDefaults: {
workspace: { kind: "task_directory" },
},
})
.where(eq(chatEndpointResources.id, resource!.id));
expect((await service.get(endpoint.id)).executionDefaults).toEqual({ projectId: project!.id });
expect((await service.listResources(endpoint.id)).find((row) => row.id === resource!.id)?.executionDefaults).toBeNull();
expect(
(await service.listGitHubRepositories(endpoint.id, { limit: 20, offset: 0, search: "" })).items.find(
(row) => row.id === resource!.id,
)?.executionDefaults,
).toBeNull();
const { thread } = makeThread({
channelId: resource!.providerResourceId,
id: `github:${resource!.providerResourceId}:4201`,
name: resource!.label,
});
await deliverMessage({
callbacks,
endpointId: endpoint.id,
thread,
message: makeMessage({
id: "gh-comment-4201",
text: "@paperclip-bot review this pull request",
mentioned: true,
}),
trigger: "mention",
});
const conversations = await readChatConversationsByEndpointId(endpoint.id);
expect(conversations).toHaveLength(1);
const [task] = await db.select().from(issues).where(eq(issues.id, conversations[0]!.issueId));
expect(task?.projectId).toBe(project!.id);
expect(task?.projectWorkspaceId).toBe(projectWorkspace!.id);
expect(task?.executionWorkspaceSettings).toMatchObject({ mode: "isolated_workspace" });
expect(task?.workspaceSelection).toEqual({
version: 1,
source: "project",
selection: {
kind: "configured_source",
projectWorkspaceId: projectWorkspace!.id,
mode: "managed_isolated",
},
});
});
});