Skip to content

fix: W-24023175 don't fall back to internal Agent API hosts for customer orgs - #404

Open
nhalko-sf wants to merge 3 commits into
forcedotcom:mainfrom
nhalko-sf:nh/W-24023175-endpoint-fallback
Open

nhalko-sf wants to merge 3 commits into
forcedotcom:mainfrom
nhalko-sf:nh/W-24023175-endpoint-fallback

Conversation

@nhalko-sf

Copy link
Copy Markdown
Collaborator

Replaces #399 (moved to a fork)

What does this PR do?

Fixes sf agent publish authoring-bundle failing with a bare TypeError: fetch failed (UND_ERR_CONNECT_TIMEOUT) on customer orgs.

requestWithEndpointFallback retried every 404 from api.salesforce.com on test., dev. and stage.api.salesforce.com. Those gateways only accept connections from inside the Salesforce network. So when a customer org got a 404 (for example, a scratch org that isn't entitled to agent authoring, while /authoring/scripts still works), the retry on test.api hit a connect timeout. Non-404 errors were rethrown immediately, so that timeout replaced the real 404.

Changes (src/utils.ts):

  • Pick hosts from the org's instance. The internal hosts are tried only when the org's instance URL is internal (SfdcUrl.isInternalUrl(): pc-rnd, stm, vpod, crm.dev, localhost). Customer orgs (*.my.salesforce.com, including develop/scratch/sandbox) call api.salesforce.com only.
  • Don't let an unreachable fallback hide the real error. If a fallback host gives no HTTP response (DNS failure, refused/reset/timed-out connection; detected by error code, walking cause), it's skipped. HTTP errors from any host (401/403/5xx, INVALID_SESSION_ID), and network errors on api.salesforce.com itself, are still thrown right away, as before. Fallback hosts get maxRetries: 0 so an unreachable host is skipped quickly.
  • SF_AGENT_API_ENDPOINTS override. Comma-separated fallback gateway prefixes, for example stage.,test.. It applies to every org, internal or not, and replaces the default chain; none disables the fallback. It's for internal orgs whose domain isn't recognized as internal (for example stage orgs on a customer-style domain).
  • Behavior change for internal users: orgs on customer-style domains now stop after api.salesforce.com instead of trying test./dev./stage.. Internal-instance orgs keep the full chain.
  • Actionable AgentApiNotFound. The error keeps its name, because scriptAgent.ts matches on it. It now names the method and path, the hosts that returned 404 and any unreachable ones, keeps the first 404 as cause, and puts attemptedEndpoints/unreachableEndpoints in data. Its actions say to check Agentforce enablement and licensing (Einstein1AIPlatform for scratch orgs) and user permissions, and to confirm the org's SFAP gateway (Setup > GatewayHawkingInfo, or set SF_AGENT_API_ENDPOINTS). For /authoring/agents, they also note that publishing calls a different Agent API endpoint that may need extra Agentforce entitlements, so validate can pass where publish can't. Query strings are kept out of the message.

Example for a customer org, before and after:

TypeError: fetch failed  [cause]: ConnectTimeoutError ... 34.210.29.116:443 ...
Error (AgentApiNotFound): Unable to access the Salesforce Agent APIs: POST /einstein/ai-agent/v1.1/authoring/agents returned 404 (Not Found) from api.salesforce.com.

Testing

  • yarn compile, yarn lint (0 errors), yarn test:only: 533 passing, 0 failing.
  • New cases in test/utils.test.ts (5 fail on main without this change): an unreachable fallback is skipped; unreachable test./dev. don't hide the prod 404; an unreachable api.salesforce.com is still thrown immediately; a prod org makes one call and gets the new message and actions; the publish hint shows only for /authoring/agents; a test.-prefixed URL is normalized on prod orgs; a missing instanceUrl is treated as prod; an HTTP error without a statusCode (INVALID_SESSION_ID), a 503, and UND_ERR_INVALID_ARG from a fallback host are rethrown; all three fallbacks unreachable gives the exact message; *.stm/*.crm.dev get the chain; fallback hosts get no retries; SF_AGENT_API_ENDPOINTS overrides and none. The existing api→test→dev→stage chain tests now use an internal pc-rnd instance URL.
  • Live, with a CLI built on this branch and test./dev./stage.api made unreachable to mimic a customer network: publishing to a customer-domain org (Agentforce enabled) succeeded and the debug log shows only api.salesforce.com calls. On a scratch org without Einstein1AIPlatform, released 2.5.0 fails with TypeError: fetch failed and this branch with the clear AgentApiNotFound 404.
  • Not tested live: a customer-domain org that 404s on /authoring/agents only (the reporter's exact case; my scratch org landed on an internal domain), and a stage org.

Review note

This assumes aws-stage1 org domains count as internal under SfdcUrl.isInternalUrl(). If they don't, those orgs would lose the stage. fallback added in #365 unless SF_AGENT_API_ENDPOINTS=stage. is set. Atul Kshirsagar (atulkc), can you confirm?

No overlap with #398: different files, and #398 deliberately leaves a failed publish POST unannotated.

What issues does this PR fix or reference?

Fixes forcedotcom/cli#3634
@W-24023175@

…-24023175@

requestWithEndpointFallback retried every 404 from api.salesforce.com on
test./dev./stage.api.salesforce.com. Those gateways only accept connections
from inside the Salesforce network, so for a customer org the fallback hit a
connect timeout, and because non-404 errors were rethrown immediately the
user saw a bare "TypeError: fetch failed" instead of the real 404.

- Only try the internal hosts when the org's instance URL is internal
  (SfdcUrl.isInternalUrl: pc-rnd, stm, crm.dev, ...). Customer orgs use
  api.salesforce.com only.
- Skip a fallback host that can't be reached instead of letting its
  connection error replace the 404.
- AgentApiNotFound now names the request, the hosts tried and any
  unreachable ones, keeps the 404 as the cause, and suggests checking the
  org's Agentforce entitlement and user permissions (plus a publish-specific
  hint for /authoring/agents).

Fixes forcedotcom/cli#3634
Detect real network errors instead of a missing statusCode, add the SF_AGENT_API_ENDPOINTS override, drop retries on fallback hosts, strip query strings from the error path, and add tests.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants