The Open Framework for Building Agents
fix(eve): forward stream cancel so parked dev sessions stop leaking polls (#233)
Ruic9e895b6b01c
diff --git a/.changeset/streaming-parked-session-leak.md b/.changeset/streaming-parked-session-leak.mdnew file mode 100644index 000000000..2e460a7c5--- /dev/null+++ b/.changeset/streaming-parked-session-leak.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Fix `eve dev` streaming throughput and time-to-first-token degrading as parked (`ask_question` / HITL) sessions accumulate. The dev runtime's NDJSON event-stream reader now forwards cancellation to the underlying run stream, so disconnecting from a parked session no longer leaks a filesystem polling loop for the life of the dev server.diff --git a/packages/eve/src/client/ndjson.ts b/packages/eve/src/client/ndjson.tsindex 1e27ed2fc..e70669f2e 100644--- a/packages/eve/src/client/ndjson.ts+++ b/packages/eve/src/client/ndjson.ts@@ -38,12 +38,14 @@ export async function* readNdjsonStream(const reader = body.getReader();const decoder = new TextDecoder();let buffer = "";+ let reachedEof = false;try {while (true) {const result = await reader.read();if (result.done) {+ reachedEof = true;// Flush any remaining bytes in the decoder.buffer += decoder.decode();break;@@ -73,6 +75,11 @@ export async function* readNdjsonStream(yield JSON.parse(trailing) as HandleMessageStreamEvent;}} finally {+ if (!reachedEof) {+ // Breaking an async iteration must close the response body; releasing+ // its lock alone leaves the server-side stream open.+ await reader.cancel().catch(() => {});+ }reader.releaseLock();}}diff --git a/packages/eve/src/client/session.test.ts b/packages/eve/src/client/session.test.tsindex 143bd7cfb..aec7be5da 100644--- a/packages/eve/src/client/session.test.ts+++ b/packages/eve/src/client/session.test.ts@@ -141,6 +141,36 @@ describe("ClientSession", () => {});});+ it("cancels a parked stream after collecting its result", async () => {+ const encoder = new TextEncoder();+ let cancelled = false;+ const stream = new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(+ encoder.encode(+ `${JSON.stringify({ type: "session.waiting", data: { wait: "next-user-message" } })}\n`,+ ),+ );+ },+ cancel() {+ cancelled = true;+ },+ });+ vi.spyOn(globalThis, "fetch").mockImplementation(async (_request, init) => {+ if ((init?.method ?? "GET") === "POST") {+ return createAcceptedResponse();+ }++ return new Response(stream);+ });+ const session = createSession();++ const result = await (await session.send("first")).result();++ expect(result.status).toBe("waiting");+ expect(cancelled).toBe(true);+ });+it("resets the session by default after consuming through session.completed", async () => {const requests: Array<{ body?: unknown; method: string; url: string }> = [];const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (request, init) => {diff --git a/packages/eve/src/execution/ndjson-stream.test.ts b/packages/eve/src/execution/ndjson-stream.test.tsnew file mode 100644index 000000000..0496b8ad4--- /dev/null+++ b/packages/eve/src/execution/ndjson-stream.test.ts@@ -0,0 +1,103 @@+import { describe, expect, it } from "vitest";++import { parseNdjsonStream } from "#execution/ndjson-stream.js";++const encoder = new TextEncoder();++async function drain<T>(stream: ReadableStream<T>): Promise<T[]> {+ const reader = stream.getReader();+ const values: T[] = [];+ try {+ for (;;) {+ const { value, done } = await reader.read();+ if (done) break;+ values.push(value);+ }+ } finally {+ reader.releaseLock();+ }+ return values;+}++describe("parseNdjsonStream", () => {+ it("parses newline-delimited JSON into values", async () => {+ const source = new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(encoder.encode('{"i":0}\n{"i":1}\n'));+ controller.enqueue(encoder.encode('{"i":2}\n'));+ controller.close();+ },+ });++ const values = await drain(parseNdjsonStream<{ i: number }>(() => source));++ expect(values).toEqual([{ i: 0 }, { i: 1 }, { i: 2 }]);+ });++ it("emits a trailing line that is not newline-terminated at EOF", async () => {+ const source = new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(encoder.encode('{"i":0}\n{"i":1}'));+ controller.close();+ },+ });++ const values = await drain(parseNdjsonStream<{ i: number }>(() => source));++ expect(values).toEqual([{ i: 0 }, { i: 1 }]);+ });++ it("reassembles values split across chunk boundaries", async () => {+ const source = new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(encoder.encode('{"i":'));+ controller.enqueue(encoder.encode("0}\n"));+ controller.close();+ },+ });++ const values = await drain(parseNdjsonStream<{ i: number }>(() => source));++ expect(values).toEqual([{ i: 0 }]);+ });++ // Regression guard for the parked-session streaming leak: a parked+ // (`session.waiting`) durable run never closes its event stream, so the+ // world-local streamer keeps a filesystem poll alive until its reader is+ // cancelled. Cancelling the parsed stream must forward the cancel to the+ // source — otherwise the poll leaks for the life of the dev server and+ // degrades streaming for every other session.+ it("forwards cancellation to a source that never reaches EOF", async () => {+ let cancelledReason: unknown = Symbol("not-cancelled");++ const source = new ReadableStream<Uint8Array>({+ start(controller) {+ // Emit one line, then stay open forever (the parked-run shape).+ controller.enqueue(encoder.encode('{"i":0}\n'));+ },+ cancel(reason) {+ cancelledReason = reason;+ },+ });++ const parsed = parseNdjsonStream<{ i: number }>(() => source);+ const reader = parsed.getReader();++ const first = await reader.read();+ expect(first.value).toEqual({ i: 0 });++ await reader.cancel("client-disconnect");++ expect(cancelledReason).toBe("client-disconnect");+ });++ it("surfaces source errors to the consumer", async () => {+ const source = new ReadableStream<Uint8Array>({+ start(controller) {+ controller.error(new Error("source exploded"));+ },+ });++ await expect(drain(parseNdjsonStream(() => source))).rejects.toThrow("source exploded");+ });+});diff --git a/packages/eve/src/execution/ndjson-stream.ts b/packages/eve/src/execution/ndjson-stream.tsnew file mode 100644index 000000000..b53ad0c85--- /dev/null+++ b/packages/eve/src/execution/ndjson-stream.ts@@ -0,0 +1,73 @@+/**+ * Transforms a byte stream of newline-delimited JSON (NDJSON) into a+ * stream of parsed values.+ *+ * The byte stream is produced lazily by `createByteStream`, so the+ * underlying source (a world-local run readable) is only opened when the+ * returned stream is consumed.+ *+ * Cancellation is forwarded to the source. When the returned stream is+ * cancelled — e.g. an SSE client disconnects and the server cancels the+ * response body — the underlying reader is cancelled too. This matters for+ * runs that never reach EOF: a parked (`session.waiting`) durable run keeps+ * its event stream open indefinitely, and the world-local streamer runs a+ * filesystem poll until its reader is cancelled. Without forwarding the+ * cancel, the read loop below would block on `reader.read()` forever and+ * that poll would leak for the life of the process, degrading streaming+ * throughput for every other session.+ */+export function parseNdjsonStream<T>(+ createByteStream: () => ReadableStream<Uint8Array>,+): ReadableStream<T> {+ const decoder = new TextDecoder();+ let buffer = "";+ let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;+ let cancelled = false;++ return new ReadableStream<T>({+ async start(controller) {+ reader = createByteStream().getReader();+ try {+ while (true) {+ const { value, done } = await reader.read();++ if (done) break;++ buffer += decoder.decode(value, { stream: true });++ for (+ let newlineIndex = buffer.indexOf("\n");+ newlineIndex !== -1;+ newlineIndex = buffer.indexOf("\n")+ ) {+ const line = buffer.slice(0, newlineIndex).trim();+ buffer = buffer.slice(newlineIndex + 1);++ if (line.length > 0) {+ controller.enqueue(JSON.parse(line) as T);+ }+ }+ }++ // A cancel resolves the pending read with `done`; bail before+ // touching the controller, which the cancel has already closed.+ if (cancelled) return;++ buffer += decoder.decode();+ const trailing = buffer.trim();+ if (trailing.length > 0) {+ controller.enqueue(JSON.parse(trailing) as T);+ }+ controller.close();+ } catch (error) {+ if (!cancelled) controller.error(error);+ } finally {+ reader.releaseLock();+ }+ },+ async cancel(reason) {+ cancelled = true;+ await reader?.cancel(reason);+ },+ });+}diff --git a/packages/eve/src/execution/workflow-runtime.ts b/packages/eve/src/execution/workflow-runtime.tsindex 05fc8d9ae..9018aa724 100644--- a/packages/eve/src/execution/workflow-runtime.ts+++ b/packages/eve/src/execution/workflow-runtime.ts@@ -19,6 +19,7 @@ import type { HandleMessageStreamEvent } from "#protocol/message.js";import type { RuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";import { getCompiledRuntimeAgentBundle } from "#runtime/sessions/compiled-agent-cache.js";import { buildRunContext } from "#execution/runtime-context.js";+import { parseNdjsonStream } from "#execution/ndjson-stream.js";import { RuntimeNoActiveSessionError } from "#execution/runtime-errors.js";const WORKFLOW_ENTRY_NAME = "workflowEntry";@@ -105,7 +106,9 @@ export function createWorkflowRuntime(config: {let events: ReadableStream<HandleMessageStreamEvent> | undefined;const getEvents = () => {- events ??= parseNdjsonStream(() => getRun(run.runId).getReadable());+ events ??= parseNdjsonStream<HandleMessageStreamEvent>(() =>+ getRun(run.runId).getReadable(),+ );return events;};@@ -145,7 +148,7 @@ export function createWorkflowRuntime(config: {sessionId: string,options?: GetEventStreamOptions,): Promise<ReadableStream<HandleMessageStreamEvent>> {- return parseNdjsonStream(() =>+ return parseNdjsonStream<HandleMessageStreamEvent>(() =>getRun(sessionId).getReadable({ startIndex: options?.startIndex }),);},@@ -206,49 +209,3 @@ function normalizeWorkflowHook(value: unknown): WorkflowHookRecord {runId,};}--function parseNdjsonStream(- createByteStream: () => ReadableStream<Uint8Array>,-): ReadableStream<HandleMessageStreamEvent> {- const decoder = new TextDecoder();- let buffer = "";-- return new ReadableStream<HandleMessageStreamEvent>({- async start(controller) {- const reader = createByteStream().getReader();- try {- while (true) {- const { value, done } = await reader.read();-- if (done) break;-- buffer += decoder.decode(value, { stream: true });-- for (- let newlineIndex = buffer.indexOf("\n");- newlineIndex !== -1;- newlineIndex = buffer.indexOf("\n")- ) {- const line = buffer.slice(0, newlineIndex).trim();- buffer = buffer.slice(newlineIndex + 1);-- if (line.length > 0) {- controller.enqueue(JSON.parse(line) as HandleMessageStreamEvent);- }- }- }-- buffer += decoder.decode();- const trailing = buffer.trim();- if (trailing.length > 0) {- controller.enqueue(JSON.parse(trailing) as HandleMessageStreamEvent);- }- controller.close();- } catch (error) {- controller.error(error);- } finally {- reader.releaseLock();- }- },- });-}