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.
This commit is contained in:
yetone
2026-10-21 07:39:22 +08:00
parent f5548bd5ca
commit c845b17e9c
+8 -4
View File
@@ -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("{}"))