-
Notifications
You must be signed in to change notification settings - Fork 985
fix(samples): retry shell agent-card discovery during startup #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stablegenius49
wants to merge
1
commit into
google:main
Choose a base branch
from
stablegenius49:fix/587-agent-card-startup-retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { A2AClient } from "@a2a-js/sdk/client"; | ||
|
|
||
| const A2UI_EXTENSION_HEADER = | ||
| "https://a2ui.org/a2a-extension/a2ui/v0.8"; | ||
|
|
||
| export interface AgentCardRetryOptions { | ||
| attempts?: number; | ||
| initialDelayMs?: number; | ||
| maxDelayMs?: number; | ||
| backoffMultiplier?: number; | ||
| fetchImpl?: typeof fetch; | ||
| clientFactory?: typeof A2AClient.fromCardUrl; | ||
| sleep?: (ms: number) => Promise<void>; | ||
| } | ||
|
|
||
| const DEFAULT_AGENT_CARD_RETRY_OPTIONS: Required< | ||
| Pick<AgentCardRetryOptions, "attempts" | "initialDelayMs" | "maxDelayMs" | "backoffMultiplier"> | ||
| > = { | ||
| attempts: 8, | ||
| initialDelayMs: 250, | ||
| maxDelayMs: 2_000, | ||
| backoffMultiplier: 2, | ||
| }; | ||
|
|
||
| const sleep = async (ms: number) => { | ||
| await new Promise((resolve) => setTimeout(resolve, ms)); | ||
| }; | ||
|
|
||
| export const createA2UIFetch = (baseFetch: typeof fetch = fetch): typeof fetch => { | ||
| return async (url, init) => { | ||
| const headers = new Headers(init?.headers); | ||
| headers.set("X-A2A-Extensions", A2UI_EXTENSION_HEADER); | ||
| return baseFetch(url, { ...init, headers }); | ||
| }; | ||
| }; | ||
|
|
||
| export const isRetryableAgentCardError = (error: unknown) => { | ||
| const message = | ||
| error instanceof Error ? error.message : typeof error === "string" ? error : ""; | ||
|
|
||
| return [ | ||
| "ECONNREFUSED", | ||
| "fetch failed", | ||
| "Failed to fetch", | ||
| "NetworkError", | ||
| "network error", | ||
| ].some((needle) => message.includes(needle)); | ||
| }; | ||
|
|
||
| export const createA2AClientWithRetry = async ( | ||
| cardUrl: string, | ||
| options: AgentCardRetryOptions = {} | ||
| ) => { | ||
| const { | ||
| attempts = DEFAULT_AGENT_CARD_RETRY_OPTIONS.attempts, | ||
| initialDelayMs = DEFAULT_AGENT_CARD_RETRY_OPTIONS.initialDelayMs, | ||
| maxDelayMs = DEFAULT_AGENT_CARD_RETRY_OPTIONS.maxDelayMs, | ||
| backoffMultiplier = DEFAULT_AGENT_CARD_RETRY_OPTIONS.backoffMultiplier, | ||
| fetchImpl, | ||
| clientFactory = A2AClient.fromCardUrl, | ||
| sleep: sleepImpl = sleep, | ||
| } = options; | ||
|
|
||
| let delayMs = initialDelayMs; | ||
| let lastError: unknown; | ||
|
|
||
| for (let attempt = 1; attempt <= attempts; attempt++) { | ||
| try { | ||
| return await clientFactory(cardUrl, { | ||
| fetchImpl: fetchImpl ?? createA2UIFetch(), | ||
| }); | ||
| } catch (error) { | ||
| lastError = error; | ||
| const shouldRetry = attempt < attempts && isRetryableAgentCardError(error); | ||
| if (!shouldRetry) { | ||
| throw error; | ||
| } | ||
|
|
||
| await sleepImpl(delayMs); | ||
| delayMs = Math.min(maxDelayMs, Math.round(delayMs * backoffMultiplier)); | ||
| } | ||
| } | ||
|
|
||
| throw lastError instanceof Error | ||
| ? lastError | ||
| : new Error(`Failed to connect to agent card: ${cardUrl}`); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { | ||
| createA2AClientWithRetry, | ||
| isRetryableAgentCardError, | ||
| } from "../a2a-client-factory.js"; | ||
|
|
||
| test("createA2AClientWithRetry retries connection errors until success", async () => { | ||
| let attempts = 0; | ||
| const delays: number[] = []; | ||
| const fakeClient = { kind: "client" }; | ||
|
|
||
| const result = await createA2AClientWithRetry( | ||
| "http://localhost:10002/.well-known/agent-card.json", | ||
| { | ||
| attempts: 4, | ||
| initialDelayMs: 10, | ||
| maxDelayMs: 20, | ||
| backoffMultiplier: 2, | ||
| sleep: async (ms) => { | ||
| delays.push(ms); | ||
| }, | ||
| clientFactory: async () => { | ||
| attempts += 1; | ||
| if (attempts < 3) { | ||
| throw new TypeError("fetch failed: ECONNREFUSED"); | ||
| } | ||
| return fakeClient as never; | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| assert.equal(result, fakeClient); | ||
| assert.equal(attempts, 3); | ||
| assert.deepEqual(delays, [10, 20]); | ||
| }); | ||
|
|
||
| test("createA2AClientWithRetry does not retry non-network errors", async () => { | ||
| let attempts = 0; | ||
|
|
||
| await assert.rejects( | ||
| createA2AClientWithRetry("http://localhost:10002/.well-known/agent-card.json", { | ||
| attempts: 4, | ||
| sleep: async () => {}, | ||
| clientFactory: async () => { | ||
| attempts += 1; | ||
| throw new Error("invalid agent card payload"); | ||
| }, | ||
| }), | ||
| /invalid agent card payload/ | ||
| ); | ||
|
|
||
| assert.equal(attempts, 1); | ||
| }); | ||
|
|
||
| test("isRetryableAgentCardError matches common startup failures", () => { | ||
| assert.equal(isRetryableAgentCardError(new Error("fetch failed")), true); | ||
| assert.equal(isRetryableAgentCardError(new Error("socket ECONNREFUSED")), true); | ||
| assert.equal(isRetryableAgentCardError(new Error("invalid agent card payload")), false); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the retry loop exhausts all attempts, this logic correctly throws an error. However, if
lastErroris not an instance ofError(e.g., it's a plain object or a string from a library), the original error details are lost in the new genericErrormessage. This can make debugging more difficult.To provide more context, you could serialize the
lastErrorinto the new error message. A simpleif/elseblock would also improve readability over the ternary.