diff --git a/.changeset/cross-org-key-fix-first.md b/.changeset/cross-org-key-fix-first.md new file mode 100644 index 00000000000..24eea2c163b --- /dev/null +++ b/.changeset/cross-org-key-fix-first.md @@ -0,0 +1,5 @@ +--- +"@agent-native/core": patch +--- + +When a provider key is saved in another organization the person also belongs to, the missing-key error now opens with its two fixes (save the key in this organization, or switch to the one holding it) and explains after, with the organization's name after both fixes. Neither that explanation nor the one for a key saved only as someone's Personal key names the key before its fixes any more (when a provider accepts several keys, each ends by naming the one it found), and a provider that needs one specific key now words an explained miss the way the any-key error does, " credential not configured. Tried: ". Together these keep the fixes in a stop's one-line reason however long the key and organization names are. When that organization's name can't be read, the message no longer says "the another organization". diff --git a/packages/core/src/agent/production-agent.spec.ts b/packages/core/src/agent/production-agent.spec.ts index 6d353a2bb7b..e81dc5304ac 100644 --- a/packages/core/src/agent/production-agent.spec.ts +++ b/packages/core/src/agent/production-agent.spec.ts @@ -18503,32 +18503,86 @@ describe("runAgentLoop", () => { label: "keeps the longest missing-key error's fix and drops its key list", thrown: "hubspot credential not configured. Ask an organization owner or " + - 'admin to save "HUBSPOT_PRIVATE_APP_TOKEN" with Workspace or ' + - "Organization scope. This workspace has it only as a Personal key, " + - "which works only for the member who saved it. Tried: " + - "HUBSPOT_PRIVATE_APP_TOKEN, HUBSPOT_ACCESS_TOKEN", + "admin to save it with Workspace or Organization scope. This " + + "workspace has it only as a Personal key, which works only for the " + + "member who saved it. Tried: HUBSPOT_PRIVATE_APP_TOKEN, " + + "HUBSPOT_ACCESS_TOKEN", headline: "I stopped because github-repo-list-files can't run yet: hubspot " + "credential not configured. Ask an organization owner or admin to " + - 'save "HUBSPOT_PRIVATE_APP_TOKEN" with Workspace or Organization ' + - "scope. This workspace has it only as a Personal key, which works " + - "only for the member who saved it. That needs to be fixed outside " + - "this chat", + "save it with Workspace or Organization scope. This workspace has " + + "it only as a Personal key, which works only for the member who " + + "saved it. That needs to be fixed outside this chat", }, { label: - "keeps the fix and drops the cause when the longest key name pushes the cause past the limit", + "keeps the whole Personal-key explanation however long the credential key is", thrown: - "bigquery credential not configured. Ask an organization owner or " + - 'admin to save "GOOGLE_APPLICATION_CREDENTIALS_JSON" with Workspace ' + - "or Organization scope. This workspace has it only as a Personal " + - "key, which works only for the member who saved it. Tried: " + - "BIGQUERY_PROJECT_ID, GOOGLE_APPLICATION_CREDENTIALS_JSON", + "acme-warehouse credential not configured. Ask an organization " + + "owner or admin to save it with Workspace or Organization scope. " + + "This workspace has it only as a Personal key, which works only " + + "for the member who saved it. Tried: ACME_" + + "WAREHOUSE_ANALYTICS_EXPORT_".repeat(5) + + "SERVICE_ACCOUNT_TOKEN", + headline: + "I stopped because github-repo-list-files can't run yet: " + + "acme-warehouse credential not configured. Ask an organization " + + "owner or admin to save it with Workspace or Organization scope. " + + "This workspace has it only as a Personal key, which works only " + + "for the member who saved it. That needs to be fixed outside this " + + "chat", + }, + { + label: + "keeps the whole cross-organization explanation with the longest key name and a long organization name", + thrown: + "bigquery credential not configured. Save it in this organization, " + + 'or switch your active organization to "Builder.io Developer ' + + "Relations and Community Programs\", where it's saved. Credentials " + + "aren't shared between organizations. Tried: BIGQUERY_PROJECT_ID, " + + "GOOGLE_APPLICATION_CREDENTIALS_JSON", headline: "I stopped because github-repo-list-files can't run yet: bigquery " + - "credential not configured. Ask an organization owner or admin to " + - 'save "GOOGLE_APPLICATION_CREDENTIALS_JSON" with Workspace or ' + - "Organization scope. That needs to be fixed outside this chat", + "credential not configured. Save it in this organization, or " + + 'switch your active organization to "Builder.io Developer ' + + "Relations and Community Programs\", where it's saved. Credentials " + + "aren't shared between organizations. That needs to be fixed " + + "outside this chat", + }, + { + label: + "keeps both cross-organization fixes and the opening words of an organization name too long for the headline", + thrown: + "bigquery credential not configured. Save it in this organization, " + + 'or switch your active organization to "The Builder.io Developer ' + + "Relations, Community Programs and Open Source Partnerships Working " + + "Group for the Americas, Europe, the Middle East, Africa and the " + + "Asia-Pacific Region\", where it's saved. Credentials aren't shared " + + "between organizations. Tried: GOOGLE_APPLICATION_CREDENTIALS_JSON", + headline: + "I stopped because github-repo-list-files can't run yet: bigquery " + + "credential not configured. Save it in this organization, or " + + 'switch your active organization to "The Builder.io Developer ' + + "Relations, Community Programs and Open Source Partnerships Working " + + "Group for the Americas, Europe, the Middle… That needs to be fixed " + + "outside this chat", + }, + { + label: + "keeps both cross-organization fixes however long the credential key is", + thrown: + "acme-warehouse credential not configured. Save it in this " + + "organization, or switch your active organization to " + + "\"Builder.io\", where it's saved. Credentials aren't shared " + + "between organizations. Tried: ACME_" + + "WAREHOUSE_ANALYTICS_EXPORT_".repeat(5) + + "SERVICE_ACCOUNT_TOKEN", + headline: + "I stopped because github-repo-list-files can't run yet: " + + "acme-warehouse credential not configured. Save it in this " + + "organization, or switch your active organization to " + + "\"Builder.io\", where it's saved. Credentials aren't shared " + + "between organizations. That needs to be fixed outside this chat", }, ])("$label in the stop headline", async ({ thrown, headline }) => { let streamCalls = 0; diff --git a/packages/core/src/credentials/scope-gap.spec.ts b/packages/core/src/credentials/index.scope-gap.spec.ts similarity index 78% rename from packages/core/src/credentials/scope-gap.spec.ts rename to packages/core/src/credentials/index.scope-gap.spec.ts index 3b359ab15c3..84745be5be6 100644 --- a/packages/core/src/credentials/scope-gap.spec.ts +++ b/packages/core/src/credentials/index.scope-gap.spec.ts @@ -80,9 +80,9 @@ describe("describeCredentialScopeGap", () => { ); expect(message).toBe( - 'Ask an organization owner or admin to save "GITHUB_TOKEN" with ' + - "Workspace or Organization scope. This workspace has it only as " + - "a Personal key, which works only for the member who saved it.", + "Ask an organization owner or admin to save it with Workspace or " + + "Organization scope. This workspace has it only as a Personal " + + "key, which works only for the member who saved it.", ); expect(message).not.toContain("integration"); expect(message).not.toContain("Save your own"); @@ -144,6 +144,23 @@ describe("describeCredentialScopeGap", () => { expect(message).toContain("Personal key"); }); + it("ends by naming the key it found when several were tried", async () => { + execute = async ({ args }) => + args?.[0] === "GH_TOKEN" ? { rows: [{ 1: 1 }] } : { rows: [] }; + + const message = await describeCredentialScopeGap( + ["GITHUB_TOKEN", "GH_TOKEN"], + { userEmail: "owner@example.com", orgId: "org-1" }, + ); + + expect(message).toBe( + "Ask an organization owner or admin to save it with Workspace or " + + "Organization scope. This workspace has it only as a Personal " + + "key, which works only for the member who saved it. The saved key " + + "is GH_TOKEN.", + ); + }); + it("stays quiet when the key is missing everywhere in the org", async () => { const message = await describeCredentialScopeGap(["SLACK_BOT_TOKEN"], { userEmail: "owner@example.com", @@ -183,7 +200,7 @@ describe("describeCredentialScopeGap across organizations", () => { resolveOrgIdForEmailResult = null; }); - it("names the organization holding the key and the mismatch as the cause", async () => { + it("leads with both fixes, naming the organization holding the key but not the key, then the cause", async () => { onlyAnotherMemberOrgHasKey("Builder.io"); const message = await describeCredentialScopeGap( @@ -191,13 +208,43 @@ describe("describeCredentialScopeGap across organizations", () => { { userEmail: "tim@example.com", orgId: "coach-org" }, ); - expect(message).toContain("ACADEMY_CONVEX_SITE_URL"); - expect(message).toContain('"Builder.io" organization'); - expect(message).toContain( - "organization mismatch rather than a missing key", + // oracle-change: the message was reworded fix-first; one exact match replaces three partial matches on the old wording + expect(message).toBe( + "Save it in this organization, or switch your active organization " + + "to \"Builder.io\", where it's saved. Credentials aren't shared " + + "between organizations.", ); }); + it("ends by naming the key it found when several were tried", async () => { + execute = async ({ sql, args }) => + sql.includes(CROSS_ORG_PROBE) && args?.[1] === "GH_TOKEN" + ? { rows: [{ org_name: "Builder.io" }] } + : { rows: [] }; + + const message = await describeCredentialScopeGap( + ["GITHUB_TOKEN", "GH_TOKEN"], + { userEmail: "owner@example.com", orgId: "org-1" }, + ); + + expect(message).toBe( + "Save it in this organization, or switch your active organization " + + "to \"Builder.io\", where it's saved. Credentials aren't shared " + + "between organizations. The saved key is GH_TOKEN.", + ); + }); + + it("trims the organization's name", async () => { + onlyAnotherMemberOrgHasKey(" Builder.io "); + + const message = await describeCredentialScopeGap(["GITHUB_TOKEN"], { + userEmail: "owner@example.com", + orgId: "org-1", + }); + + expect(message).toContain('organization to "Builder.io", where'); + }); + it("bounds the probe to the caller's own memberships and excludes the active org", async () => { onlyAnotherMemberOrgHasKey("Builder.io"); @@ -226,7 +273,9 @@ describe("describeCredentialScopeGap across organizations", () => { orgId: "org-1", }); - expect(message).toContain("another organization"); + expect(message).toMatch( + /^Save it in this organization, or switch your active organization to one where it's saved\. /, + ); expect(message).not.toContain("org-1"); }); diff --git a/packages/core/src/credentials/index.ts b/packages/core/src/credentials/index.ts index 586f55f54b8..97750585da8 100644 --- a/packages/core/src/credentials/index.ts +++ b/packages/core/src/credentials/index.ts @@ -395,12 +395,21 @@ export async function resolveCredential( * channel) and whether Settings lets a member save that key as Personal * are both unknown here, so "the integration did it" and "save your own" * are each wrong for some runs. The fix comes first, so a chat stop's - * 240-character headline keeps it for every key name. + * 240-character headline keeps it. * - The key is saved in a DIFFERENT organization the caller also belongs to. * Credentials are per-organization, so gaining a second organization (or * having `active-org-id` repointed at one) orphans every key synced under * the first. Without this, the only symptom is a missing-env-var error that - * names the key rather than the org mismatch that actually caused it. + * names the key rather than the org mismatch that actually caused it. Its + * two fixes come first too, so the same headline keeps both. The + * organization's name has no length limit, so it comes after both fixes, + * where a long one is all the cut can drop. + * + * Neither message names the key before its fixes, since its name has no + * length limit either: both callers' sentence before it already names what's + * missing, and their "Tried:" list after it names the keys. When more than + * one key was tried, the message ends by naming the one it found, which that + * list can't show. * * SECURITY: both probes are bounded to organizations the caller is a member * of, report only the scope kind (and, for the cross-org case, the name of an @@ -439,11 +448,12 @@ export async function describeCredentialScopeGap( const scopedCtx: CredentialContext = { ...ctx, orgId: orgLookup.orgId }; for (const key of keys) { + const found = keys.length > 1 ? ` The saved key is ${key}.` : ""; if (await hasForeignPersonalCredentialInOrg(key, scopedCtx)) { return ( - `Ask an organization owner or admin to save "${key}" with ` + - `Workspace or Organization scope. This workspace has it only as a ` + - `Personal key, which works only for the member who saved it.` + `Ask an organization owner or admin to save it with Workspace or ` + + `Organization scope. This workspace has it only as a ` + + `Personal key, which works only for the member who saved it.${found}` ); } @@ -451,13 +461,11 @@ export async function describeCredentialScopeGap( ? null : await findMemberOrgHoldingCredential(key, scopedCtx); if (holder) { + const target = holder.name ? `"${holder.name}", where` : "one where"; return ( - `A "${key}" key is saved in the ${holder} organization, but this ` + - `request resolved to a different organization you also belong to. ` + - `Credentials are scoped per organization and are not shared between ` + - `them, so this is an organization mismatch rather than a missing key. ` + - `Either switch your active organization back to ${holder}, or save ` + - `"${key}" in the organization this request runs in.` + `Save it in this organization, or switch your active organization ` + + `to ${target} it's saved. Credentials aren't shared between ` + + `organizations.${found}` ); } } @@ -489,7 +497,7 @@ async function hasForeignPersonalCredentialInOrg( async function findMemberOrgHoldingCredential( key: string, ctx: CredentialContext, -): Promise { +): Promise<{ name: string | null } | null> { try { const { rows } = await getDbExec().execute({ sql: `SELECT o.name AS org_name @@ -506,9 +514,9 @@ async function findMemberOrgHoldingCredential( }); if (rows.length === 0) return null; const name = (rows[0] as { org_name?: unknown }).org_name; - return typeof name === "string" && name.trim() - ? `"${name.trim()}"` - : "another"; + return { + name: typeof name === "string" && name.trim() ? name.trim() : null, + }; } catch { return null; } diff --git a/packages/core/src/provider-api/index.spec.ts b/packages/core/src/provider-api/index.spec.ts index 4eb32472268..30ffd2d60d6 100644 --- a/packages/core/src/provider-api/index.spec.ts +++ b/packages/core/src/provider-api/index.spec.ts @@ -4644,9 +4644,8 @@ describe("provider API runtime", () => { it("explains an out-of-scope credential instead of reporting it missing", async () => { resolveCredential.mockResolvedValue(null); describeCredentialScopeGap.mockResolvedValue( - 'Ask an organization owner or admin to save "HUBSPOT_ACCESS_TOKEN" ' + - "with Workspace or Organization scope. This workspace has it only " + - "as a Personal key.", + "Ask an organization owner or admin to save it with Workspace or " + + "Organization scope. This workspace has it only as a Personal key.", ); const runtime = createProviderApiRuntime({ appId: "analytics", @@ -4662,7 +4661,7 @@ describe("provider API runtime", () => { ); expect(error?.message).toMatch( - /^hubspot credential not configured\. Ask an organization owner or admin to save "HUBSPOT_ACCESS_TOKEN" .* Personal key\. Tried: \S/, + /^hubspot credential not configured\. Ask an organization owner or admin to save it .* Personal key\. Tried: \S/, ); expect(describeCredentialScopeGap).toHaveBeenCalledWith( expect.arrayContaining(["HUBSPOT_ACCESS_TOKEN"]), @@ -4670,6 +4669,51 @@ describe("provider API runtime", () => { ); }); + it("puts a single required key after the explanation, as the any-key error does", async () => { + resolveCredential.mockResolvedValue(null); + describeCredentialScopeGap.mockResolvedValue( + "Save it in this organization, or switch your active organization " + + 'to "Builder.io", where it\'s saved.', + ); + const runtime = createProviderApiRuntime({ + appId: "analytics", + providerIds: ["apollo"], + getCredentialContext: () => credentialContext, + }); + + const error = await runtime + .executeRequest({ provider: "apollo", path: "/v1/auth/health" }) + .then( + () => null, + (err: Error) => err, + ); + + expect(error?.message).toBe( + "apollo credential not configured. Save it in this organization, or " + + 'switch your active organization to "Builder.io", where it\'s saved. ' + + "Tried: APOLLO_API_KEY", + ); + }); + + it("names a single required key alone when there's nothing to explain", async () => { + resolveCredential.mockResolvedValue(null); + describeCredentialScopeGap.mockResolvedValue(null); + const runtime = createProviderApiRuntime({ + appId: "analytics", + providerIds: ["apollo"], + getCredentialContext: () => credentialContext, + }); + + const error = await runtime + .executeRequest({ provider: "apollo", path: "/v1/auth/health" }) + .then( + () => null, + (err: Error) => err, + ); + + expect(error?.message).toBe("APOLLO_API_KEY not configured"); + }); + it("gives no shared-key advice when the selected connection lacks the key", async () => { describeCredentialScopeGap.mockResolvedValue("SAVE A SHARED KEY"); resolveWorkspaceConnectionForApp.mockResolvedValue({ diff --git a/packages/core/src/provider-api/index.ts b/packages/core/src/provider-api/index.ts index 95837a18754..36a0fbebc1c 100644 --- a/packages/core/src/provider-api/index.ts +++ b/packages/core/src/provider-api/index.ts @@ -5169,7 +5169,9 @@ async function resolveRequiredCredential(options: { }); } throw new Error( - `${options.key} not configured${scopeGap ? `. ${scopeGap}` : ""}`, + scopeGap + ? `${options.provider} credential not configured. ${scopeGap} Tried: ${options.key}` + : `${options.key} not configured`, ); }