Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cross-org-key-fix-first.md
Original file line number Diff line number Diff line change
@@ -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 repeats the key any more, and a provider that needs one specific key now words an explained miss the way the any-key error does, "<provider> credential not configured. <explanation> Tried: <key>". 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".
88 changes: 71 additions & 17 deletions packages/core/src/agent/production-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -183,21 +183,33 @@ 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(
["ACADEMY_CONVEX_SITE_URL"],
{ 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("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");

Expand Down Expand Up @@ -226,7 +238,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");
});

Expand Down
33 changes: 19 additions & 14 deletions packages/core/src/credentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@
credential?.connectionId !== endpoint.connectionId)
) {
throw new CredentialEndpointMismatchError(
`Refusing to send ${key ? `\"${key}\"` : "a credential"} to a workspace connection unless it is bound to that exact connection.`,

Check warning on line 90 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:90:42: Unnecessary escape character '"'

Check warning on line 90 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:90:34: Unnecessary escape character '"'
);
}
if (endpoint.scope === "unknown") {
throw new CredentialEndpointMismatchError(
`Refusing to send ${key ? `\"${key}\"` : "a credential"} to an endpoint with unknown ownership.`,

Check warning on line 95 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:95:42: Unnecessary escape character '"'

Check warning on line 95 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:95:34: Unnecessary escape character '"'
);
}
const soloWorkspaceEndpoint =
Expand All @@ -110,7 +110,7 @@
if (credentialBelongsToEndpointUser) return;

throw new CredentialEndpointMismatchError(
`Refusing to send ${key ? `\"${key}\"` : "a credential"} to a user-controlled endpoint unless it is saved by the same user.`,

Check warning on line 113 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:113:42: Unnecessary escape character '"'

Check warning on line 113 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:113:34: Unnecessary escape character '"'
);
}

Expand All @@ -122,7 +122,7 @@
if (credentialBelongsToEndpointWorkspace) return;

throw new CredentialEndpointMismatchError(
`Refusing to send ${key ? `\"${key}\"` : "a credential"} to a shared endpoint unless it is saved by the same organization or workspace.`,

Check warning on line 125 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:125:42: Unnecessary escape character '"'

Check warning on line 125 in packages/core/src/credentials/index.ts

View workflow job for this annotation

GitHub Actions / Lint & format

eslint(no-useless-escape)

packages/core/src/credentials/index.ts:125:34: Unnecessary escape character '"'
);
}
}
Expand Down Expand Up @@ -395,12 +395,19 @@
* 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, whose 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.
*
* 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
Expand Down Expand Up @@ -441,8 +448,8 @@
for (const key of keys) {
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 ` +
`Ask an organization owner or admin to save it with Workspace or ` +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Personal-key advice does not identify the matched key

The scope-gap probe returns on the first matching key, but this message no longer names it. For providers with multiple fallback keys, Tried: lists all candidates without showing which one is Personal-only, so the user cannot tell which key an organization owner should save with shared scope; please preserve the matched-key identity and cover a multi-key case.

Fix in Builder

`Organization scope. This workspace has it only as a ` +
`Personal key, which works only for the member who saved it.`
);
}
Expand All @@ -451,13 +458,11 @@
? 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 ` +
Comment thread
builder-io-integration[bot] marked this conversation as resolved.
Resolved
Comment on lines +461 to +463

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Long organization names can hide the switch destination

The organization name is still part of this recovery sentence and the stop headline is capped at 240 characters. For sufficiently long names the headline can retain “switch your active organization” but truncate the destination, so the user cannot tell which organization to select; please define bounded headline behavior and add a test with a name longer than the available budget.

Fix in Builder

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered in 077edc0. The headline's behavior for a long name is already bounded by its cut: both fixes always come first, then as much of the name as fits, cut at a word with "…". The new stop-headline case uses a 175-character organization name, past the budget, and the headline ends …switch your active organization to "The Builder.io Developer Relations, Community Programs and Open Source Partnerships Working Group for the Americas, Europe, the Middle…. With a short provider name, names of up to about 130 characters fit whole. The earlier 125-character case no longer went past the budget once the key left the message, so this one replaces it.

Shortening the name in the message itself would cost the full name in the error, which is where a person reads it in full, so the name stays whole there.

`to ${target} it's saved. Credentials aren't shared between ` +
`organizations.`
);
}
}
Expand Down Expand Up @@ -489,7 +494,7 @@
async function findMemberOrgHoldingCredential(
key: string,
ctx: CredentialContext,
): Promise<string | null> {
): Promise<{ name: string | null } | null> {
try {
const { rows } = await getDbExec().execute({
sql: `SELECT o.name AS org_name
Expand All @@ -506,9 +511,9 @@
});
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;
}
Expand Down
52 changes: 48 additions & 4 deletions packages/core/src/provider-api/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -4662,14 +4661,59 @@ 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"]),
credentialContext,
);
});

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({
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/provider-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Comment thread
builder-io-integration[bot] marked this conversation as resolved.
Resolved
: `${options.key} not configured`,
);
}

Expand Down
Loading