From c845b17e9cbd313c97b4ad1afaeba479ec798310 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 21 Oct 2026 07:39:22 +0800 Subject: [PATCH] cursor: http1.test counts a Run when its first message comes, not when its stream is read (#272) Cause: the fake agent counted runs++ when the server's "stream" event fired. In the region test the first Run stalls (mode "stall"), the plugin gives up at H2.wait and goes on over HTTP/1.1, and the test then sets mode "answer". On a loaded machine the server can read the stalled Run's HEADERS only after that switch, so the stale stream was taken as a second Run: ag.runs 2, expected 1. "Runs kept on HTTP/1.1 try HTTP/2 again once H2.retryAfter has passed" has the same stall-then-answer shape. The plugin is right; the fake miscounted. A Run now counts once its first message arrives (the plugin writes it only after the head, so a stream whose client gave up never brings one), and a stream already destroyed when it is read isn't answered. Run on macOS arm64, bun 1.4.2: - Injected: the fake held the first stream and emitted it the moment the test set mode "answer". origin/main's test: red, Expected 1 Received 2, the issue's output. With this change: green. The retryAfter test under the same injection: red on origin/main, green now. - Loaded: 8 parallel `bun test --rerun-each 15 packages/cursor/http1.test.mjs` at load average ~50: 8 x 240 pass, 0 fail. - `HOME=$(mktemp -d) bun test` from the root: 1920 pass, 0 fail. `bun scripts/check.mjs`: ok. Test-only, no version bump. --- packages/cursor/http1.test.mjs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/cursor/http1.test.mjs b/packages/cursor/http1.test.mjs index ad56af9..0ee3800 100644 --- a/packages/cursor/http1.test.mjs +++ b/packages/cursor/http1.test.mjs @@ -59,8 +59,11 @@ const end = (body) => { const update = (num, body) => frame(pb().bytes(1, pb().bytes(num, body)).done()) // the agent API over HTTP/2 on port: "answer" answers a Run "hi" and the -// turn's end; "stall" takes the stream and never sends its head; "refuse" -// closes the stream with code (REFUSED_STREAM unless told) before its head. +// turn's end, and counts it in runs once the Run's first message has come: +// a loaded machine can read a stalled Run's stream only after the test set +// "answer", and that stream, its client gone, never brings a message; +// "stall" takes the stream and never sends its head; "refuse" closes the +// stream with code (REFUSED_STREAM unless told) before its head. // resets refuses that many streams first, whatever the mode. async function agent(port, mode, { resets = 0, code = http2.constants.NGHTTP2_REFUSED_STREAM } = {}) { const a = { mode, runs: 0, resets, code } @@ -71,8 +74,8 @@ async function agent(port, mode, { resets = 0, code = http2.constants.NGHTTP2_RE a.resets-- return stream.close(a.code) } - if (a.mode === "stall") return - a.runs++ + // a stream read so late that its client is gone isn't answered + if (a.mode === "stall" || stream.destroyed) return stream.respond({ ":status": 200, "content-type": "application/connect+proto" }) let buf = Buffer.alloc(0) let answered = false @@ -80,6 +83,7 @@ async function agent(port, mode, { resets = 0, code = http2.constants.NGHTTP2_RE buf = Buffer.concat([buf, c]) if (answered || buf.length < 5 || buf.length < 5 + buf.readUInt32BE(1)) return answered = true + a.runs++ stream.write(update(1, pb().str(1, "hi").done())) stream.write(update(14, pb().varint(1, 0).done())) stream.end(end("{}"))