The Open Framework for Building Agents
fix(eve): retry Safari and Firefox stream open network errors (#4174)
e0-gh-vercel-connect[bot]8136e50f9327
diff --git a/.changeset/browser-stream-open-retry.md b/.changeset/browser-stream-open-retry.mdnew file mode 100644index 000000000..f667b720a--- /dev/null+++ b/.changeset/browser-stream-open-retry.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Retry session stream opens and reconnects when Safari, iOS browsers, or Firefox reject `fetch()` with `TypeError: Load failed` or `TypeError: NetworkError when attempting to fetch resource.`, matching the existing Chrome and Node behavior.diff --git a/docs/guides/client/streaming.mdx b/docs/guides/client/streaming.mdxindex b35ba2d25..fcd53d1e7 100644--- a/docs/guides/client/streaming.mdx+++ b/docs/guides/client/streaming.mdx@@ -173,7 +173,7 @@ The sign-in holds the turn open, and the stream emits `turn.waiting` with `on: "HTTP connections can end before a run does. The client reconnects from the number of events already consumed, so long turns continue without replaying events. By default, a turn response keeps reconnecting until it reaches a turn boundary or is aborted, including while the turn is paused for authorization. A manually opened `session.stream()` eventually stops after repeated empty streams when it can no longer make progress.-Browser failures while reading a response body, including `TypeError: Load failed` and `TypeError: network error`, use the same reconnect policy. Invalid stream events still fail the read.+Browser network failures use the same reconnect policy, both when opening a stream (`TypeError: Failed to fetch`, `TypeError: Load failed`, or `TypeError: NetworkError when attempting to fetch resource.`) and while reading a response body (any `TypeError`, such as `TypeError: network error`). Invalid stream events still fail the read.If your consumer persists events, key on `event.meta.id`. It is stable across reconnects and rewinds, so an overlapping replay is safe to ingest twice. See [the event envelope](../../concepts/sessions-runs-and-streaming#the-event-envelope).diff --git a/packages/eve/src/client/ndjson.ts b/packages/eve/src/client/ndjson.tsindex 8f65e2539..2bfb4084c 100644--- a/packages/eve/src/client/ndjson.ts+++ b/packages/eve/src/client/ndjson.ts@@ -24,7 +24,11 @@ export function isStreamDisconnectError(error: unknown): boolean {error.name === "AbortError" ||error.message === "terminated" ||errorCode === "UND_ERR_SOCKET" ||- (error instanceof TypeError && /^(?:failed to fetch|fetch failed)$/i.test(error.message)) ||+ // fetch() network errors from Chrome, Node, WebKit, and Firefox.+ (error instanceof TypeError &&+ /^(?:failed to fetch|fetch failed|load failed|networkerror when attempting to fetch resource\.)$/i.test(+ error.message,+ )) ||/abort|cancel|disconnect|premature close|socket|terminated/i.test(error.message));}diff --git a/packages/eve/src/client/open-stream.test.ts b/packages/eve/src/client/open-stream.test.tsindex 2fe903f55..3216d29e9 100644--- a/packages/eve/src/client/open-stream.test.ts+++ b/packages/eve/src/client/open-stream.test.ts@@ -73,6 +73,33 @@ describe("openStreamBody", () => {EVE_STREAM_CONTROL_VERSION,);});++ it.each(["Load failed", "NetworkError when attempting to fetch resource."])(+ "retries a browser network error while opening a stream: %s",+ async (message) => {+ const fetchMock = vi+ .fn<typeof fetch>()+ .mockRejectedValueOnce(new TypeError(message))+ .mockResolvedValue(+ new Response("", {+ headers: { [EVE_STREAM_VERSION_HEADER]: EVE_MESSAGE_STREAM_VERSION },+ status: 200,+ }),+ );+ vi.stubGlobal("fetch", fetchMock);++ const connection = await openStreamBody({+ host: "https://agent.example",+ resolveHeaders: () => Promise.resolve(new Headers()),+ path: "/eve/v1/session/session_1/stream",+ startIndex: 0,+ streamReconnectPolicy: { streamOpenReconnectPolicy: { baseDelayMs: 1, maxAttempts: 2 } },+ });+ connection.close();++ expect(fetchMock).toHaveBeenCalledTimes(2);+ },+ );});describe("followStreamIterable", () => {diff --git a/packages/eve/src/client/session.test.ts b/packages/eve/src/client/session.test.tsindex 6b0e57b1d..44e1d4595 100644--- a/packages/eve/src/client/session.test.ts+++ b/packages/eve/src/client/session.test.ts@@ -939,7 +939,7 @@ describe("ClientSession", () => {expect(fetchMock).toHaveBeenCalledOnce();});- it.each(["Load failed", "network error"])(+ it.each(["network error"])("does not retry an unrecognized TypeError while opening a stream: %s",async (message) => {const fetchMock = vi.spyOn(globalThis, "fetch").mockRejectedValue(new TypeError(message));