Files
PaperClipAI/server/src/services/tool-access/oauth/direct-callback.ts
T
4d0847c4ca fix(server): settle external conversations from committed work (#17772)
<!-- entire-trail-link-start -->
https://entire.com/gh/paperclipai/-/trails/6/changes/paperclip/6
<!-- entire-trail-link-end -->

## Thinking Path

> - Paperclip manages AI agents and the tasks that they execute.
> - External conversations must become idle after an authorized response
is published and all related work is complete.
> - Native replay and stranded-task recovery still discover this
settlement work through recurring scans.
> - A new deadline worker must preserve response ordering, monitor
handoffs, and unfinished work.
> - This pull request gives settlement one shared readiness predicate
and activates its worker from committed changes.
> - Existing rows provide recovery truth. No new table or per-feature
timer is added.
> - The benefit is a settlement worker that becomes quiet and resumes
when relevant work changes.

## Linked Issues or Issue Description

Refs #17758. This is slice 56. Slice 55 has merged. Related: #17606
(chat milestones), #17663 (bounded run reads), #17665 (monitor proof),
and #13809 (answered Slack conversation lifecycle). The provider
applicability remains Slack and Speko-owned conversations. This does not
extend Telegram settlement from #16559.

**What happened?**

Conversation settlement depended on periodic native replay and generic
task recovery. An exhausted monitor also cleared its schedule before
recovery admission. During that gap, an answered conversation could look
finished. Failed or deferred admission lost the monitor schedule.

**Expected behavior**

Relevant committed changes activate settlement. The worker uses the
owning service's evidence and becomes quiet when no settlement is owed.
Monitor recovery stays pending until admission, and restart does not
admit an already accepted recovery twice.

**Steps to reproduce**

1. Create an active external conversation with a successful run and an
authorized published response.
2. Keep execution, approval, a goal, or a monitor pending. Let the
settlement worker become quiet.
3. Resolve that prerequisite through its owning service. Verify that the
conversation settles without another periodic sweep.
4. Delay or fail exhausted-monitor recovery admission. Verify that the
monitor remains pending and the conversation cannot settle during the
handoff.

**Paperclip version or commit**

Accepted production source: `50f4432ed88537b8d522cf1dce24c33588db0bac`.
Parent #17758 merged as `8ac5a128e66fb672e88106412f76cf278599bfc4`. The
publication adopts that actual squash through a normal merge with an
unchanged complete child tree.

## What Changed

- Preserve quiet after coarse company-only maintenance hints. The shared
inbound-notice and response-availability helpers keep settlement enabled
by default. Only the milestone dispatcher suppresses their duplicate
settlement fanout. Its scoped issue-review signal and the explicit
company-authority signal still wake real work. Four database cases prove
both quiet and real company-wide producer delivery.
- Preserve source-generation checks for chat handoff,
task-before-comment locking, issue-scoped decision invalidation, and
current conversation event columns from master.
- Track initial external conversation settlement through shutdown
without blocking HTTP readiness on endpoint or task locks.

- Reuse the real queue's coalesced recovery receipt after restart,
without admitting the same monitor recovery again.

- Share the existing conversation readiness predicate between bounded
discovery and locked settlement. Preserve current provider policy,
run/publication proof, company boundaries, and endpoint-to-issue lock
order.
- Register a scoped deadline worker through the delivery coordinator.
Preserve startup, restore/worktree suppression, drain/resume, failure
recovery, and joined shutdown.
- Extend existing review, milestone, wake-notice, and finalization
signals. Add notifications at independent issue, comment, goal, session,
and wake-receipt unblocks.
- Keep monitor owner recovery pending until guarded admission. Reuse an
accepted receipt for the same monitor schedule after restart, even when
its limit reason changes; retain compatibility with legacy receipt keys.
Commit board escalation and its monitor clear together.
- Delegate periodic native settlement while preserving resource
callbacks. Keep direct/manual defaults and preserve generic recovery
ordering with a read-only readiness guard.
- Add the producer and lifecycle audit in
`doc/plans/2026-10-26-external-conversation-settlement-sleep.md` and
update the stack ledger.

## Verification

Accepted production source: `50f4432ed88537b8d522cf1dce24c33588db0bac`.
Publication head: `b256bb52647affd9f5279ea8c40f48bbe40f031f`.
The complete accepted child tree stays unchanged when adopting merged
parent #17758. This publication adds only the audit document.

- **3,129 distinct cases in 37 complete server files pass**, plus **18
selected chat-response cases** (3,147 selected cases). The other 1,796
chat cases were not run locally. Full settlement, queue, scheduling,
run-state, goals, interactions, task Retry, decision targets, status
reassessment, workspace, process recovery, and review consumers are
covered.
- **Preserved failure:** the first 35-file command passed 2,445 cases
and timed out one restored-work handoff at its existing 15-second limit.
That aggregate command failed. The unchanged full 89-case actions file
then passed on a fresh fixture; the affected case took 22 ms. Root cause
was not established. No timeout or assertion changed. The 34 other files
passed originally. Distinct counts exclude duplicate actions passes.
Fresh CI remains a separate gate.
- **Indirect wake regression:** the original unconditional baseline
failed two quiet checks. Guards alone left the chat case failing with
three candidate queries. The final controlled indirect baseline fails
one of four cases; the repair passes all four with the same test hash.
Positive controls preserve real company-only producers.
- **Source and fixture controls:** all 38 selected test files restore to
their exact committed hashes after fixture imports. Fresh PostgreSQL 17
fixtures use 413 migrations. All five manual merge resolutions preserve
current authority, locking, and event-column contracts.
- **Static checks pass:** database build; server typecheck and build;
module boundaries; migration order; Copilot profile identity. Runner
packages equal the accepted parent byte for byte. No new Runner/package
qualification is claimed.
- [Current integration and wake
audit](https://github.com/paperclipai/paperclip/blob/b256bb52647affd9f5279ea8c40f48bbe40f031f/doc/plans/2026-10-26-external-conversation-settlement-sleep.md#current-master-integration-and-indirect-wake-audit-2026-10-29).
Latest-head CI and exact-head Greptile 5/5 must pass before merge.

<details><summary>Earlier revision verification and preserved failure
history</summary>

Receiver failure fairness: a controlled 1,100 ms batch reproduced a
30-second delay for healthy due receivers. The worker now supplies its
existing per-item deadline through `nextRunAtOnError`. All 20 receiver
lifecycle tests pass on the owner and combined tip
`31c12f1d68ff4a51e06fc2be2abf2c2aee9f9d3a`; server production
typechecking passes on the owner. The failed receiver retains its own
backoff, and settled receivers return to zero recurring SQL. The
five-second assertion budget is unchanged. See [the owning
audit](https://github.com/paperclipai/paperclip/blob/83204a67880d980cfbea3771cee19a3cda0022b7/doc/plans/2026-10-25-chat-receiver-discovery-sleep-audit.md#healthy-receiver-progress-after-a-slow-failed-batch).
Current-head CI and review remain required.

Approved delegated Test-tab calls now evaluate caller and target
policies in the existing claim transaction. Real HTTP regressions first
showed a missing caller charge and shared-limit self-rejection. All 78
connection-management cases and 92 gateway cases now pass on the owning
branch, along with server production typechecking. All 78
connection-management cases also pass at combined tip
`d25f830db89c1623bff3547bfc68015c1c66e0cf`. The three new cases cover
shared charges, separate charges, rollback on denial, duplicate
approval, and rejection of a later authorized call. Intermediate
counter/identity fixture failures and a reused-database Slack collision
are recorded in [the owning
audit](https://github.com/paperclipai/paperclip/blob/ef9ae5767430d0093efc736ae0b303ba5afaae65/doc/idle-approved-tool-recovery.md#delegated-test-tab-admission-after-policy-changes).
The earlier combined checks below remain evidence for their stated
revisions. Current-head CI and review remain required.

Inherited voice-cleanup verification: the controlled slow-provider
regression and all 13 lifecycle tests passed on its owner and combined
tip `ab22d8ed0c09028315607414b83b375e5cc0a60a`. That repair changed only
the fixture clock advancement and its audit note.

Current head: `83204a67880d980cfbea3771cee19a3cda0022b7`. This remains a
single-duty PR stacked on #17758.

- Integrated pinned master `8548173fc9efdc0fa07644fb92dcc0b6851a95e5`
with normal merges. Every branch passed generated-schema checks and its
target-branch migration-order gate. Existing SQL is unchanged; the 25
stack migrations follow master’s new migration.
- Duty-specific producer coverage, lifecycle tests, and earlier failure
records:
[2026-10-24-recurring-server-sleep-stack.md](https://github.com/paperclipai/paperclip/blob/d6d8e4ffa6b330ebf4647aec997ee2dde6abc321/doc/plans/2026-10-24-recurring-server-sleep-stack.md),
[2026-10-26-external-conversation-settlement-sleep.md](https://github.com/paperclipai/paperclip/blob/d6d8e4ffa6b330ebf4647aec997ee2dde6abc321/doc/plans/2026-10-26-external-conversation-settlement-sleep.md).
- Combined tip `51e6cbe67979b55b340ea9f0f434c5b5c682ccd6`: **355
startup/cancellation/recovery/dispatch/preservation tests**, **75
connection-management tests**, and production server typechecking pass.
These combined checks do not mean every intermediate PR independently
ran the full repository suite.
- Real master-to-stack database upgrade passes: **379 unchanged prior
migration receipts plus 25 new migrations**, with the gateway
responsibility fixture preserved.
- Owning-branch checks: 84 runner installation/settlement cases plus
runner typechecking and generated TypeScript/Rust profile agreement; 253
GitHub review workflow cases. Stop-attribution and held-worker readiness
regressions failed before their fixes.
- Failure accounting: the initial combined run passed 349 cases and
failed six idle assertions in a reused preservation database containing
old cancelled runs. Repeating the combined command with a fresh
preservation database passed. Earlier invalid project selection, merge
import/type errors, and the interrupted full local suite are not counted
as passing checks.
- **Latest-head CI and review remain required.** Prior CI findings
include GitHub idle observation, route teardown, attention cleanup,
touch-picker E2E, workspace health, and voice-cleanup timing; local
passes do not erase those failures. No full local repository-suite,
deployed sleep, fleet cost, or live Copilot qualification claim is made.


</details>

## Risks

- Signals cover supported in-process writers. They do not supply change
capture for independent SQL or other replicas.
- Real company-wide changes can inspect active candidates. Coarse
maintenance hints do not activate settlement. Configured idle and
retained history must remain quiet.
- Monitor receipt binding and admission order must preserve company,
agent, issue, and exact schedule identity. Current parent handoff locks
and source-generation checks remain intact.
- The local restored-work timeout is retained above. A fresh full-file
pass does not establish its cause; latest-head CI must also pass.
- Generic recovery and Muse/resource duties remain separate slices. This
does not make the whole server idle. Existing Cloud sleep blockers
remain. No deployed sleep, database suspension, external alarm coverage,
fleet cost, or live provider qualification claim is made.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, code execution, PostgreSQL
tests, and GitHub tools. The exact runtime model identifier and
context-window size 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>
Co-authored-by: Codex <noreply@openai.com>
2026-10-29 06:02:43 -05:00

707 lines
29 KiB
TypeScript

import { notifyIssueReviewState } from "../../issue-review-state-notifications.js";
import { notifyToolConnectionHealth } from "../../tool-connection-health-notifications.js";
import { credentialScope } from "./client-settings.js";
import { withoutOAuthRefreshLease } from "./refresh-leases.js";
import type { ActorInfo } from "../types.js";
import { createOAuthDiscovery } from "./discovery.js";
import { createOAuthClientProtocol, storedOAuthTokenEndpointAuthMethod } from "./client.js";
import { normalizeOAuthProviderError, oauthProviderErrorMessage } from "./provider-errors.js";
import { oauthConfig, normalizeOauthScopes, resolveGrantedOauthScopes } from "./metadata.js";
import { connectionMethodForConnection } from "../connection-methods.js";
import { googleWorkspaceEffectiveScopes, isGoogleWorkspaceScopeGrant } from "@paperclipai/shared";
import { emitConnectionUpdated } from "../../connector-telemetry.js";
import { and, eq, ne } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import { connectionGrants, companyMemberships, principalPermissionGrants, issueThreadInteractions, toolApplications, toolConnections } from "@paperclipai/db";
import type { ConnectToolAppResult } from "@paperclipai/shared";
import { getConnectableAppDefinition, recommendedDefaultsForApp } from "@paperclipai/shared";
import { badRequest, forbidden, HttpError, unprocessable } from "../../../errors.js";
import { secretService } from "../../secrets.js";
import type { OAuthStateStore } from "./state.js";
import type { OAuthCredentialStore } from "./credential-store.js";
import type { OAuthCatalogCompletion } from "./catalog-completion.js";
import type { RefreshConnectionCatalog, CheckConnectionHealth } from "../catalog/contracts.js";
import type { ConnectionSetupPolicy } from "../connections/setup-policy.js";
import { toApplication } from "../applications/projection.js";
import { groupedActions } from "../catalog/action-summary.js";
import { assertOAuthRedirectConstraints, assertOAuthCallbackIssuer } from "./authorization-constraints.js";
import type { createOrganizationGrantStore } from "../connections/organization-grant.js";
function asRecord(value: unknown): Record<string, unknown> {
if (value && typeof value === "object" && !Array.isArray(value))
return value as Record<string, unknown>;
return {};
}
type Dependencies = {
now: () => Date;
getConnectionRow(idOrUid: string, companyId?: string): Promise<typeof toolConnections.$inferSelect>;
state: OAuthStateStore;
refreshCatalog: RefreshConnectionCatalog;
finishOAuthCatalogWithRecommendedDefaults: OAuthCatalogCompletion;
store: Pick<OAuthCredentialStore, "createOrRotateOAuthSecret" | "oauthClientForConnection" | "syncCredentialBindings">;
setupPolicy: ConnectionSetupPolicy;
checkConnectionHealth: CheckConnectionHealth;
ensureDefaultOrganizationGrant: ReturnType<typeof createOrganizationGrantStore>;
oauthEndpointsForConnection: ReturnType<typeof createOAuthDiscovery>["oauthEndpointsForConnection"];
exchangeOAuthToken: ReturnType<typeof createOAuthClientProtocol>["exchangeOAuthToken"];
};
export function createDirectOAuthCallback(db: Db, dependencies: Dependencies) {
const { now, getConnectionRow, state, store, setupPolicy, refreshCatalog, finishOAuthCatalogWithRecommendedDefaults, checkConnectionHealth, ensureDefaultOrganizationGrant, oauthEndpointsForConnection, exchangeOAuthToken } = dependencies;
const { consumeOAuthState, rejectPendingOAuthInteraction } = state;
const { createOrRotateOAuthSecret, oauthClientForConnection, syncCredentialBindings } = store;
const { assertConnectorConnectionSetupEnabled } = setupPolicy;
async function completeOAuthCallback(input: {
state: string;
code?: string | null;
/**
* RFC 6749 `error`. Untrusted, and deliberately the *only* thing read from a
* failed callback — `error_description` and `error_uri` are not accepted as
* input at all, so there is nothing for a hostile provider to reflect
* through (PAP-17108).
*/
error?: string | null;
redirectUri: string;
/** RFC 9207 `iss`, when the authorization server returns it. */
iss?: string | null;
actor?: ActorInfo;
}): Promise<ConnectToolAppResult> {
// Binding first, outcome second: the provider's report of a failure is only
// acted on once the callback is bound to a state Paperclip issued and to the
// actor that started the flow, so an unsolicited callback cannot drive any
// path here. Consuming the state up front is what makes a denial terminal —
// a refused request must not stay completable by a later code (PAP-17109).
const stateRow = await consumeOAuthState(input.state, input.actor);
if (input.error) {
await rejectPendingOAuthInteraction(stateRow, input.actor);
const providerError = normalizeOAuthProviderError(input.error);
throw new HttpError(
400,
oauthProviderErrorMessage(
providerError,
"The authorization server denied the request.",
),
{
code: "oauth_authorization_denied",
providerError,
},
);
}
// Neither a code nor an error is not a usable answer either. It still spends
// the request: the recovery is a fresh authorization, not a state left live
// waiting for a better callback.
if (!input.code) throw badRequest("OAuth callback is missing a code");
let connection = await getConnectionRow(
stateRow.connectionId,
stateRow.companyId,
);
// Reauthorization refreshes credentials and catalog without rebuilding the
// operator's action profile, policy rules, or access bindings.
const shouldFinalizeDefaults = connection.status === "draft";
const preCallbackLifecycle = {
status: connection.status,
enabled: connection.enabled,
};
await assertConnectorConnectionSetupEnabled(connection);
const sourceTemplateKey =
typeof connection.config.sourceTemplateKey === "string"
? connection.config.sourceTemplateKey
: null;
const galleryEntry = sourceTemplateKey
? getConnectableAppDefinition(sourceTemplateKey)
: null;
assertOAuthRedirectConstraints(galleryEntry, input.redirectUri);
const endpoints = await oauthEndpointsForConnection(
connection,
null,
input.redirectUri,
);
assertOAuthCallbackIssuer(connection, endpoints, input.iss);
// Generic states created before requested scopes were persisted may still
// complete after an upgrade. Keep their original cached/discovered scope set.
const authorizedScopes = normalizeOauthScopes(
stateRow.requestedScopes ?? (galleryEntry ? [] : endpoints.scopes),
);
const client = await oauthClientForConnection(
connection,
endpoints.provider,
input.actor,
);
if (!client.clientId)
throw unprocessable(
`OAuth client id is not configured for ${endpoints.provider}`,
);
const token = await exchangeOAuthToken({
tokenUrl: endpoints.tokenUrl,
clientId: client.clientId,
clientSecret: client.clientSecret,
tokenEndpointAuthMethod: storedOAuthTokenEndpointAuthMethod(
oauthConfig(connection),
client.clientSecret,
),
redirectUri: input.redirectUri,
codeVerifier: stateRow.codeVerifier,
code: input.code,
resource: endpoints.resource,
});
if (sourceTemplateKey === "google-workspace" && !isGoogleWorkspaceScopeGrant(normalizeOauthScopes(token.scope))) {
throw unprocessable("Google did not return a supported permission grant. Reconnect and choose the Google services you want to use.", { code: "oauth_scope_grant_invalid" });
}
if (sourceTemplateKey === "google-workspace") {
token.scope = googleWorkspaceEffectiveScopes(authorizedScopes, normalizeOauthScopes(token.scope)).join(" ");
if (!token.scope) throw unprocessable("Google did not grant any selected service permissions", { code: "oauth_scope_grant_invalid" });
}
const connectedAt = now();
const expiresAt = token.expiresIn
? new Date(connectedAt.getTime() + token.expiresIn * 1000).toISOString()
: null;
if (stateRow.subjectUserId) {
let personalCredentialSecretRefs: typeof connectionGrants.$inferSelect.credentialSecretRefs =
[];
await db.transaction(async (tx) => {
// Serialize callback persistence with suspension/removal. Those paths
// lock this same membership row before sweeping personal credentials.
const [membership] = await tx
.select({ id: companyMemberships.id })
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, connection.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, stateRow.subjectUserId!),
eq(companyMemberships.status, "active"),
ne(companyMemberships.membershipRole, "viewer"),
),
)
.limit(1)
.for("update");
if (!membership) {
throw forbidden(
"Your company membership no longer permits connection changes. Ask a company owner to restore non-viewer access before you authorize this connection again.",
);
}
const txSecrets = secretService(tx);
const txSecretContext = { dbClient: tx, secretClient: txSecrets };
const [existingUserGrant] = await tx
.select()
.from(connectionGrants)
.where(
and(
eq(connectionGrants.companyId, connection.companyId),
eq(connectionGrants.connectionId, connection.id),
eq(connectionGrants.kind, "user"),
eq(connectionGrants.subjectUserId, stateRow.subjectUserId!),
),
)
.limit(1)
.for("update");
await tx.select({ id: toolApplications.id }).from(toolApplications)
.where(and(eq(toolApplications.id, connection.applicationId), eq(toolApplications.companyId, connection.companyId)))
.for("update");
await tx.select({ id: toolConnections.id }).from(toolConnections)
.where(and(eq(toolConnections.id, connection.id), eq(toolConnections.companyId, connection.companyId)))
.for("update");
const subjectCredentialSecretRefs =
existingUserGrant?.credentialSecretRefs ?? [];
const accessRef = await createOrRotateOAuthSecret(
{
companyId: connection.companyId,
connection,
configPath: "oauth.access_token",
label: "OAuth access token",
value: token.accessToken,
actor: input.actor,
existingRefs: subjectCredentialSecretRefs,
ownerUserId: stateRow.subjectUserId!,
},
txSecretContext,
);
const nextCredentialSecretRefs = [
...subjectCredentialSecretRefs.filter(
(ref) =>
ref.configPath !== "oauth.access_token" &&
ref.configPath !== "oauth.refresh_token",
),
accessRef,
];
if (token.refreshToken) {
nextCredentialSecretRefs.push(
await createOrRotateOAuthSecret(
{
companyId: connection.companyId,
connection,
configPath: "oauth.refresh_token",
label: "OAuth refresh token",
value: token.refreshToken,
actor: input.actor,
existingRefs: subjectCredentialSecretRefs,
ownerUserId: stateRow.subjectUserId!,
},
txSecretContext,
),
);
} else {
const existingRefreshRef = subjectCredentialSecretRefs.find(
(ref) => ref.configPath === "oauth.refresh_token",
);
if (existingRefreshRef)
nextCredentialSecretRefs.push(existingRefreshRef);
}
const grantedScopes = resolveGrantedOauthScopes({
tokenScope: token.scope,
requestedScopes: authorizedScopes,
});
const grantValues = {
providerTenant: {
...(existingUserGrant?.providerTenant ?? {}),
oauth: {
...asRecord(asRecord(existingUserGrant?.providerTenant).oauth),
strategy: "direct_oauth",
accessTokenExpiresAt: expiresAt ?? undefined,
scopes: grantedScopes.scopes,
scopeSource: grantedScopes.scopeSource,
unrequestedScopes: grantedScopes.unrequestedScopes,
// Per-grant, because two users can authorize the same connection with
// different scopes. The connection-level list is whichever callback ran last,
// so it is the wrong baseline for anyone else's refresh.
requestedScopes: authorizedScopes,
consentedAt: connectedAt.toISOString(),
tokenType: token.tokenType,
refreshedAt: connectedAt.toISOString(),
},
},
credentialSecretRefs: nextCredentialSecretRefs,
status: "active" as const,
revokedAt: null,
revokedByAgentId: null,
revokedByUserId: null,
updatedAt: new Date(),
};
if (existingUserGrant) {
await tx
.update(connectionGrants)
.set(grantValues)
.where(eq(connectionGrants.id, existingUserGrant.id));
} else {
await tx.insert(connectionGrants).values({
companyId: connection.companyId,
connectionId: connection.id,
kind: "user",
subjectUserId: stateRow.subjectUserId!,
...grantValues,
isDefault: false,
createdByUserId: stateRow.subjectUserId!,
});
}
personalCredentialSecretRefs = nextCredentialSecretRefs;
const nextConfig = {
...connection.config,
oauth: {
...withoutOAuthRefreshLease(oauthConfig(connection)),
provider: endpoints.provider,
authorizationUrl: endpoints.authorizationUrl,
tokenUrl: endpoints.tokenUrl,
metadataUrl: endpoints.metadataUrl ?? null,
scopes: authorizedScopes,
clientIdEnv: client.clientIdEnv,
clientSecretEnv: client.clientSecret
? client.clientSecretEnv
: null,
credentialScope: credentialScope(connection, input.actor),
issuer: endpoints.issuer ?? oauthConfig(connection).issuer ?? null,
resource:
endpoints.resource ?? oauthConfig(connection).resource ?? null,
expiresAt,
scope: token.scope,
tokenType: token.tokenType,
connectedAt: connectedAt.toISOString(),
},
providerMetadata: {
...asRecord(connection.config.providerMetadata),
oauth: {
expiresAt,
scope: token.scope,
scopeSource: grantedScopes.scopeSource,
unrequestedScopes: grantedScopes.unrequestedScopes,
tokenType: token.tokenType,
},
},
};
await notifyToolConnectionHealth(tx, connection);
const [updatedConnection] = await tx
.update(toolConnections)
.set({
status: "active",
enabled: true,
authKind: "oauth",
credentialPolicy: connection.credentialPolicy,
config: nextConfig,
transportConfig: nextConfig,
// A personal-only connection keeps tokens exclusively on its user
// grant. Adding a personal identity to an existing shared/fallback
// connection must not erase that connection's organization token.
credentialRefs:
connection.credentialPolicy === "per_user"
? connection.credentialRefs.filter(
(ref) => ref.name !== "oauth.access_token",
)
: connection.credentialRefs,
credentialSecretRefs:
connection.credentialPolicy === "per_user"
? connection.credentialSecretRefs.filter(
(ref) =>
ref.configPath !== "oauth.access_token" &&
ref.configPath !== "oauth.refresh_token",
)
: connection.credentialSecretRefs,
updatedAt: new Date(),
})
.where(eq(toolConnections.id, connection.id))
.returning();
if (!updatedConnection)
throw new Error("OAuth connection was not found");
connection = updatedConnection;
await tx
.update(toolApplications)
.set({
status: "active",
updatedAt: new Date(),
})
.where(eq(toolApplications.id, connection.applicationId));
const linkedInteraction = stateRow.interactionId
? await tx
.select({ kind: issueThreadInteractions.kind, issueId: issueThreadInteractions.issueId })
.from(issueThreadInteractions)
.where(eq(issueThreadInteractions.id, stateRow.interactionId))
.limit(1)
.then((rows) => rows[0] ?? null)
: null;
if (
stateRow.interactionId &&
linkedInteraction?.kind === "request_confirmation"
) {
await notifyIssueReviewState(tx, { companyId: connection.companyId, issueId: linkedInteraction.issueId });
await tx
.update(issueThreadInteractions)
.set({
status: "accepted",
result: { version: 1, outcome: "accepted" },
resolvedByUserId: stateRow.subjectUserId!,
resolvedAt: new Date(),
updatedAt: new Date(),
})
.where(
and(
eq(issueThreadInteractions.id, stateRow.interactionId),
eq(issueThreadInteractions.companyId, connection.companyId),
),
);
}
await syncCredentialBindings(
connection,
connection.credentialPolicy === "per_user"
? personalCredentialSecretRefs
: [],
tx,
);
});
emitConnectionUpdated(
connection,
preCallbackLifecycle,
"oauth_callback",
);
// Personal OAuth used to return immediately after saving the grant. That
// left the connection draft/paused and its catalog empty, so the person
// who had just consented landed on a false "Nothing to test" state.
// Activate and discover with the just-issued token before returning.
const refresh = await refreshCatalog(connection.id, input.actor, {
enableAllByDefault: shouldFinalizeDefaults,
skipDefaultProfileSync: shouldFinalizeDefaults,
credentialHeaders: { Authorization: `Bearer ${token.accessToken}` },
});
const [application] = await db
.select()
.from(toolApplications)
.where(eq(toolApplications.id, connection.applicationId));
if (!application)
throw new Error("OAuth connection application was not found");
const suggestedDefaults = galleryEntry
? recommendedDefaultsForApp(
galleryEntry,
connectionMethodForConnection(galleryEntry, connection).key,
)
: { access: "all_agents" as const, askFirstRiskLevels: [] };
const finished = shouldFinalizeDefaults
? await finishOAuthCatalogWithRecommendedDefaults({
interactionId: stateRow.interactionId,
connection,
catalog: refresh.catalog,
suggestedDefaults,
actor: input.actor,
})
: null;
return {
connectionId: refresh.connection.id,
application: toApplication(application),
connection: finished?.connection ?? refresh.connection,
catalog: refresh.catalog,
actions: groupedActions(refresh.catalog),
suggestedDefaults,
auth: null,
};
}
const organizationActorUserId =
stateRow.createdByActorType === "user" ? stateRow.createdByActorId : null;
if (!organizationActorUserId) {
throw forbidden(
"Organization OAuth completion requires the user who started sign-in",
);
}
await db.transaction(async (tx) => {
// Keep callback persistence serialized with membership suspension, role
// downgrade, and removal. Once this row is locked, authority cannot be
// revoked between the live check and the shared credential/grant writes.
const [membership] = await tx
.select({
id: companyMemberships.id,
membershipRole: companyMemberships.membershipRole,
})
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, connection.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, organizationActorUserId),
eq(companyMemberships.status, "active"),
ne(companyMemberships.membershipRole, "viewer"),
),
)
.limit(1)
.for("update");
if (!membership) {
throw forbidden(
"Your company membership no longer permits connection changes. Ask a company owner to restore non-viewer access before you authorize this connection again.",
);
}
const roleCanManage =
membership.membershipRole === "owner" ||
membership.membershipRole === "admin";
const [explicitManagerGrant] = roleCanManage
? []
: await tx
.select({
id: principalPermissionGrants.id,
})
.from(principalPermissionGrants)
.where(
and(
eq(principalPermissionGrants.companyId, connection.companyId),
eq(principalPermissionGrants.principalType, "user"),
eq(
principalPermissionGrants.principalId,
organizationActorUserId,
),
eq(
principalPermissionGrants.permissionKey,
"tools:manage_connections",
),
),
)
.limit(1)
.for("update");
if (!roleCanManage && !explicitManagerGrant) {
throw forbidden(
"Only a company owner, admin, or member with connection-manager permission can share credentials with the organization.",
);
}
await tx.select({ id: connectionGrants.id }).from(connectionGrants)
.where(and(eq(connectionGrants.companyId, connection.companyId), eq(connectionGrants.connectionId, connection.id),
eq(connectionGrants.kind, "organization"), eq(connectionGrants.isDefault, true)))
.for("update");
await tx.select({ id: toolApplications.id }).from(toolApplications)
.where(and(eq(toolApplications.id, connection.applicationId), eq(toolApplications.companyId, connection.companyId)))
.for("update");
await tx.select({ id: toolConnections.id }).from(toolConnections)
.where(and(eq(toolConnections.id, connection.id), eq(toolConnections.companyId, connection.companyId)))
.for("update");
const txSecrets = secretService(tx);
const txSecretContext = { dbClient: tx, secretClient: txSecrets };
const subjectCredentialSecretRefs = connection.credentialSecretRefs;
const accessRef = await createOrRotateOAuthSecret(
{
companyId: connection.companyId,
connection,
configPath: "oauth.access_token",
label: "OAuth access token",
value: token.accessToken,
actor: input.actor,
},
txSecretContext,
);
const nextCredentialSecretRefs = [
...subjectCredentialSecretRefs.filter(
(ref) =>
ref.configPath !== "oauth.access_token" &&
ref.configPath !== "oauth.refresh_token",
),
accessRef,
];
if (token.refreshToken) {
nextCredentialSecretRefs.push(
await createOrRotateOAuthSecret(
{
companyId: connection.companyId,
connection,
configPath: "oauth.refresh_token",
label: "OAuth refresh token",
value: token.refreshToken,
actor: input.actor,
},
txSecretContext,
),
);
} else {
const existingRefreshRef = subjectCredentialSecretRefs.find(
(ref) => ref.configPath === "oauth.refresh_token",
);
if (existingRefreshRef)
nextCredentialSecretRefs.push(existingRefreshRef);
}
const organizationGrantedScopes = resolveGrantedOauthScopes({
tokenScope: token.scope,
requestedScopes: authorizedScopes,
});
const nextConfig = {
...connection.config,
oauth: {
...withoutOAuthRefreshLease(oauthConfig(connection)),
provider: endpoints.provider,
authorizationUrl: endpoints.authorizationUrl,
tokenUrl: endpoints.tokenUrl,
metadataUrl: endpoints.metadataUrl ?? null,
scopes: authorizedScopes,
clientIdEnv: client.clientIdEnv,
clientSecretEnv: client.clientSecret ? client.clientSecretEnv : null,
credentialScope: credentialScope(connection, input.actor),
// Keep the issuer and resource this grant was minted against so refresh,
// reconnect, revoke and diagnostics resolve the same authorization server
// instead of re-discovering one from a possibly-changed endpoint.
issuer: endpoints.issuer ?? oauthConfig(connection).issuer ?? null,
resource:
endpoints.resource ?? oauthConfig(connection).resource ?? null,
expiresAt,
scope: token.scope,
tokenType: token.tokenType,
connectedAt: connectedAt.toISOString(),
},
providerMetadata: {
...asRecord(connection.config.providerMetadata),
oauth: {
expiresAt,
scope: token.scope,
scopeSource: organizationGrantedScopes.scopeSource,
unrequestedScopes: organizationGrantedScopes.unrequestedScopes,
tokenType: token.tokenType,
},
},
};
await notifyToolConnectionHealth(tx, connection);
const [updatedConnection] = await tx
.update(toolConnections)
.set({
status: "active",
enabled: true,
authKind: "oauth",
config: nextConfig,
transportConfig: nextConfig,
credentialSecretRefs: nextCredentialSecretRefs,
credentialRefs: [
...connection.credentialRefs.filter(
(ref) => ref.name !== "oauth.access_token",
),
{
name: "oauth.access_token",
secretId: accessRef.secretId,
version: "latest" as const,
placement: "header" as const,
key: "Authorization",
prefix: "Bearer ",
},
],
updatedAt: new Date(),
})
.where(eq(toolConnections.id, connection.id))
.returning();
connection = updatedConnection;
await tx
.update(toolApplications)
.set({ status: "active", updatedAt: new Date() })
.where(eq(toolApplications.id, connection.applicationId));
// The organization grant is created before OAuth has any secrets to attach.
// Synchronize it after every successful callback/rotation so all real tool
// execution paths receive the credentials that setup and catalog discovery
// just proved.
await ensureDefaultOrganizationGrant(connection, tx, undefined, {
scopes: organizationGrantedScopes.scopes,
scopeSource: organizationGrantedScopes.scopeSource,
unrequestedScopes: organizationGrantedScopes.unrequestedScopes,
requestedScopes: authorizedScopes,
});
await syncCredentialBindings(connection, [], tx);
});
emitConnectionUpdated(
connection,
preCallbackLifecycle,
"oauth_callback",
);
await checkConnectionHealth(connection.id, input.actor);
const refresh = await refreshCatalog(connection.id, input.actor, {
enableAllByDefault: shouldFinalizeDefaults,
skipDefaultProfileSync: shouldFinalizeDefaults,
});
const [application] = await db
.select()
.from(toolApplications)
.where(eq(toolApplications.id, connection.applicationId));
const suggestedDefaults = galleryEntry
? recommendedDefaultsForApp(
galleryEntry,
connectionMethodForConnection(galleryEntry, connection).key,
)
: {
access: "all_agents" as const,
askFirstRiskLevels: [],
};
const finished = shouldFinalizeDefaults
? await finishOAuthCatalogWithRecommendedDefaults({
interactionId: stateRow.interactionId,
connection,
catalog: refresh.catalog,
suggestedDefaults,
actor: input.actor,
})
: null;
return {
connectionId: refresh.connection.id,
application: toApplication(application),
connection: finished?.connection ?? refresh.connection,
catalog: refresh.catalog,
actions: groupedActions(refresh.catalog),
suggestedDefaults,
auth: null,
};
}
return completeOAuthCallback;
}