Reuse DCR client per integration instead of minting duplicates (part of #1120)#1142
Reuse DCR client per integration instead of minting duplicates (part of #1120)#1142RhysSullivan wants to merge 1 commit into
Conversation
… duplicates runDcrConnect minted a fresh oauth_client on every connect, so repeated connects to the same integration piled up "Datadog", "Datadog 2", ... "Datadog 10". A fresh probe of the same server yields the same endpoints, so an existing DCR client is still valid. Before minting, reuse an existing client for the same integration + owner + discovered token host (matched via origin.integration, added in the cross-integration fix this stacks on) and just re-run the authorize/token flow with it. Adds unit guards in add-account-modal: reuses a client minted for this integration, and does not reuse one belonging to a different integration. Stacked on the cross-integration picker fix (origin.integration plumbing).
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
executor-marketing | 47bb747 | Commit Preview URL Branch Preview URL |
Jun 26 2026, 05:41 AM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
executor-cloud | 47bb747 | Jun 26 2026, 05:41 AM |
Cloudflare preview
Sign-in is Cloudflare Access (one-time PIN to an allowed email). The preview has its own database and encryption key; it is destroyed when this PR closes. |
@executor-js/cli
@executor-js/config
@executor-js/execution
@executor-js/sdk
@executor-js/codemode-core
@executor-js/runtime-quickjs
@executor-js/plugin-file-secrets
@executor-js/plugin-graphql
@executor-js/plugin-keychain
@executor-js/plugin-mcp
@executor-js/plugin-onepassword
@executor-js/plugin-openapi
executor
commit: |
| const registrationEndpoint = probe.registrationEndpoint; | ||
| if (!registrationEndpoint) return { kind: "fallback", reason: "no-registration-endpoint" }; | ||
|
|
||
| const slug = uniqueClientSlug(input.integrationName, input.existingSlugs); | ||
| // Reuse a client already minted for THIS integration (same owner, same | ||
| // discovered token host) instead of minting a duplicate every connect — the | ||
| // cause of "Datadog", "Datadog 2", … "Datadog 10" piling up. A fresh probe of | ||
| // the same server yields the same endpoints, so an existing DCR client is | ||
| // still valid; we re-run only the authorize/token flow with it. | ||
| const probedTokenRoot = getRootDomain(probe.tokenUrl); | ||
| const reusable = input.existingClients.find( | ||
| (app) => | ||
| app.owner === input.owner && | ||
| app.origin?.integration != null && | ||
| String(app.origin.integration) === String(input.integration) && | ||
| getRootDomain(app.tokenUrl) === probedTokenRoot, | ||
| ); | ||
| if (reusable) { | ||
| deps.start({ client: reusable.slug, owner: input.owner }); | ||
| return { kind: "started" }; | ||
| } |
There was a problem hiding this comment.
Reuse check unreachable when server stops advertising its DCR endpoint
The if (!registrationEndpoint) return { kind: "fallback", reason: "no-registration-endpoint" } guard fires at line 508, before the reusable-client search is reached. If a server probe succeeds but the server temporarily (or permanently) drops its registration_endpoint from discovery metadata, any existing valid DCR client for that integration is ignored and the user hits "Automatic setup unavailable". The reuse path doesn't use registrationEndpoint at all — it only needs probe.tokenUrl — so the check should run after the reuse attempt, or the two paths should be split: try reuse first, only then require registrationEndpoint before minting.
| const probedTokenRoot = getRootDomain(probe.tokenUrl); | ||
| const reusable = input.existingClients.find( | ||
| (app) => | ||
| app.owner === input.owner && | ||
| app.origin?.integration != null && | ||
| String(app.origin.integration) === String(input.integration) && | ||
| getRootDomain(app.tokenUrl) === probedTokenRoot, | ||
| ); |
There was a problem hiding this comment.
getRootDomain returns string | undefined. When probe.tokenUrl is unparseable, probedTokenRoot is undefined, and any existing client whose tokenUrl is also unparseable will satisfy undefined === undefined, producing a spurious match. Add an explicit guard so that an undefined domain never matches anything.
| const probedTokenRoot = getRootDomain(probe.tokenUrl); | |
| const reusable = input.existingClients.find( | |
| (app) => | |
| app.owner === input.owner && | |
| app.origin?.integration != null && | |
| String(app.origin.integration) === String(input.integration) && | |
| getRootDomain(app.tokenUrl) === probedTokenRoot, | |
| ); | |
| const probedTokenRoot = getRootDomain(probe.tokenUrl); | |
| const reusable = | |
| probedTokenRoot != null | |
| ? input.existingClients.find( | |
| (app) => | |
| app.owner === input.owner && | |
| app.origin?.integration != null && | |
| String(app.origin.integration) === String(input.integration) && | |
| getRootDomain(app.tokenUrl) === probedTokenRoot, | |
| ) | |
| : undefined; |
Part of #1120 (2 of 4). Stacked on #1139 (the cross-integration picker fix). Review/merge that first; this PR is based on its branch so the diff here is only the dedup change.
Problem
runDcrConnectminted a freshoauth_clienton every connect, so repeated connects to the same integration piled up "Datadog", "Datadog 2", ... "Datadog 10".Fix
A fresh probe of the same server yields the same endpoints, so an existing DCR client is still valid. Before minting, reuse an existing client for the same integration + owner + discovered token host (matched via
origin.integration, added in the PR this stacks on) and just re-run the authorize/token flow with it.Tests
Adds unit guards in
add-account-modal: reuses a client minted for this integration, and does not reuse one belonging to a different integration (lint 0/0, typecheck 41/41, add-account-modal 23/23).