From 98032106913c5bbf944712d8c922f4937d9a5320 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 22:12:47 +0800 Subject: [PATCH] trae tests: a stream that breaks off breaks after the caller has its first event, not 30 ms after it was sent (#124) The test's fake server sent one event, waited 30 ms and errored the body. The plugin reads its first part before it answers. Under the whole repo's `bun test` that read sometimes started after the reset, and Bun then drops the event it had already received. The read threw ECONNRESET out of the plugin's fetch instead of reaching the mid-stream path the test covers (1259 pass, 1 fail at a6bed5e). The reset now waits until the caller has read "half", so the plugin has always read it first. Only the test changed. index.mjs is untouched, so there's no version bump. Run at a6bed5e + this change, TMPDIR and HOME set to a scratch dir: - before the change, repo-root `bun test`: 1259 pass, 1 fail (this test). A 200 ms pause put by hand before the plugin's first read failed it every time when run alone. - after: repo-root `bun test` 1259 pass, 0 fail, 1 skip (exit 0); packages/trae `bun test` 129 pass; stream.test.mjs alone 3/3; `bun scripts/check.mjs` exit 0. - with the same 200 ms pause in place: passes. - broke it by hand: index.mjs ending a broken stream with finish "stop" and no error turns this test red. - grep found no other test that resets a body on a timer. Not tried: CI's Linux runner. CI's publish runs had passed this test so far (e.g. 38572309548). Their red was the zed and zen-free Trusted Publisher 404s. --- packages/trae/stream.test.mjs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/trae/stream.test.mjs b/packages/trae/stream.test.mjs index 71fc867..d356420 100644 --- a/packages/trae/stream.test.mjs +++ b/packages/trae/stream.test.mjs @@ -123,19 +123,39 @@ test("a reply cut at its limit says length", async () => { }) test("a stream that breaks off ends with an error and [DONE]", async () => { + // the connection breaks only once the caller has the first event, so the + // plugin has read it: a reset timed by the clock alone can beat that read + // on a busy machine, and Bun then drops the event it had (#124) + let reset + const caught = new Promise((r) => (reset = r)) f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => { let n = 0 const body = new ReadableStream({ async pull(ctl) { if (n++ === 0) return ctl.enqueue(new TextEncoder().encode('event: output\ndata: {"response":"half"}\n\n')) - await new Promise((r) => setTimeout(r, 30)) // the headers and the first event are out + await caught ctl.error(new Error("connection reset")) }, }) return new Response(body, { headers: { "content-type": "text/event-stream" } }) }) - const c = await chunks(await ask({ model: "glm-5", stream: true, messages: [{ role: "user", content: "hi" }] })) + const res = await ask({ model: "glm-5", stream: true, messages: [{ role: "user", content: "hi" }] }) + const reader = res.body.getReader() + const dec = new TextDecoder() + let text = "" + while (!text.includes('"half"')) { + const { value, done } = await reader.read() + if (done) break + text += dec.decode(value, { stream: true }) + } + reset() + for (;;) { + const { value, done } = await reader.read() + if (done) break + text += dec.decode(value, { stream: true }) + } + const c = await chunks(new Response(text)) expect(textOf(c)).toBe("half") expect(finishOf(c)).toEqual([]) expect(c.at(-2).error.message).toContain("Trae CN")