cursor 0.2.28: a Run over HTTP/1.1 sends its BidiAppends 16 at a time, as cursor-agent does

Over HTTP/1.1 (where HTTP/2 can't open a Run) each client message goes up
as a BidiAppend. openH1 sent them strictly one at a time, so a new Run's
history (one blob a message, which Cursor asks for all at once) cost a
round trip to api2.cursor.sh per message before the model could start:
hundreds of round trips for a long conversation. cursor-agent 2026.10.01
(startAppendingInputs) keeps up to 16 appends in flight, numbered by
seqno (`m.length>=16&&await Promise.race(m)`).

openH1 now keeps up to atOnce (H1.atOnce, 16) appends on their way, in
seqno order; past that a write waits for one to finish. A native
session's Run passes 1: it counts a write as done only when Cursor
answered that append, so a tool result is never uncertain (the native
HTTP/1 tests hold that, maxInFlight 1). Seen from ysm119's report
(33-36 s to first token on claude-opus-5-5); whether he is on HTTP/1.1 is
not known.

Run: bun test packages/cursor (314 pass, 0 fail) and
bun scripts/check.mjs cursor (ok), on mote.
Broken by hand: with openH1 back to one at a time, the new http1 test is
red (most in flight 1, expected > 1). With runNative not passing 1, five
native-http1 tests are red (maxInFlight 3, uncertain-append and writeWait
cases), so the serial contract is held there; open.test.mjs's serial
openH1 tests now ask for atOnce 1 by name.
Not tried: a real Cursor account over HTTP/1.1; the serial cost was not
measured against Cursor itself, only counted against a fake.
This commit is contained in:
yetone
2026-10-28 14:38:50 +08:00
parent fc1457162c
commit d9c858c3c0
5 changed files with 55 additions and 27 deletions
+5 -2
View File
@@ -50,8 +50,11 @@ plugin's `fetch` answers them on Cursor's agent API:
after it go straight to HTTP/1.1 for 10 minutes; a failed connection
(refused, no network) or a stream the server refused leaves the next Run
to try HTTP/2 again. A region error over HTTP/1.1 sends the Run back to
HTTP/2 at the region's agent host. A BidiAppend with no answer in 60 s
(more for a large one) ends the Run, as cursor-agent does.
HTTP/2 at the region's agent host. BidiAppends go up numbered in order,
up to 16 at a time as cursor-agent sends them (one at a time for a
native session's Run, whose tool results must be certain). A BidiAppend
with no answer in 60 s (more for a large one) ends the Run, as
cursor-agent does.
- A new Run imports the whole conversation as AI SDK messages kept as blobs
the server asks for. The caller's tools are MCP tools, listed in the
system prompt; the model calls them through Cursor's `CallDynamicTool`,
+16 -11
View File
@@ -330,7 +330,10 @@ test("an HTTP error with no Connect error of its own keeps its status: 401 from
expect((await ask(fresh(), base)).status).toBe(429)
})
test("appends go up one at a time, numbered in order, a slow one holding the rest", async () => {
test("appends go up numbered in order, up to H1.atOnce at a time, as cursor-agent sends them", async () => {
// one at a time, a new Run's history took a round trip to Cursor a
// message before the model could start (ysm119 on Discord)
const sent = []
const got = []
let inFlight = 0
let most = 0
@@ -340,9 +343,10 @@ test("appends go up one at a time, numbered in order, a slow one holding the res
if (u.endsWith("/aiserver.v1.BidiService/BidiAppend")) {
const f = fields(Buffer.from(init.body))
const seqno = f.find((x) => x.num === 3)?.n ?? 0
sent.push(seqno)
inFlight++
most = Math.max(most, inFlight)
if (seqno === 0) await new Promise((r) => setTimeout(r, 150)) // the first one slow
await new Promise((r) => setTimeout(r, seqno === 0 ? 150 : 20)) // a round trip each, the first one slow
got.push({ seqno, data: Buffer.from(f.find((x) => x.num === 1).data.toString(), "hex").toString() })
inFlight--
return new Response(new Uint8Array(0), { headers: { "content-type": "application/proto" } })
@@ -350,16 +354,17 @@ test("appends go up one at a time, numbered in order, a slow one holding the res
throw new Error("the test asked " + u)
}
const o = await openH1({ "x-request-id": "r-1", authorization: "Bearer t" })
for (const m of ["first", "blob 1", "blob 2", "blob 3"]) o.req.write(frame(Buffer.from(m)))
for (let i = 0; i < 100 && got.length < 4; i++) await new Promise((r) => setTimeout(r, 10))
const msgs = ["first", ...Array.from({ length: 39 }, (_, i) => "blob " + (i + 1))]
for (const m of msgs) o.req.write(frame(Buffer.from(m)))
for (let i = 0; i < 500 && got.length < msgs.length; i++) await new Promise((r) => setTimeout(r, 10))
o.session.close()
expect(got).toEqual([
{ seqno: 0, data: "first" },
{ seqno: 1, data: "blob 1" },
{ seqno: 2, data: "blob 2" },
{ seqno: 3, data: "blob 3" },
])
expect(most).toBe(1)
// each message once, numbered as written, asked in that order
expect(sent).toEqual(msgs.map((_, i) => i))
expect(got.sort((a, b) => a.seqno - b.seqno)).toEqual(msgs.map((data, seqno) => ({ seqno, data })))
// more than one on its way at once, never more than cursor-agent's 16
expect(most).toBeGreaterThan(1)
expect(most).toBeLessThanOrEqual(H1.atOnce)
expect(H1.atOnce).toBe(16)
})
test("a Run cancelled while HTTP/2 opens is not counted as HTTP/2 failing", async () => {
+29 -9
View File
@@ -2037,10 +2037,11 @@ async function agentURL(tok, fresh) {
// had here (h2Blocked), the Runs after it go straight to HTTP/1.1 for
// H2.retryAfter, or until a region error sends one back (answerOf); a
// connection that failed (refused, no network) or a stream the server
// refused leaves the next Run to try HTTP/2 again.
// refused leaves the next Run to try HTTP/2 again. atOnce is how many
// BidiAppends openH1 may have on their way at once.
let h1Until = 0
const cancelledOpen = () => ({ error: { status: 410, message: "the request was cancelled" }, h2Failed: false })
async function open(base, headers, signal) {
async function open(base, headers, signal, atOnce = H1.atOnce) {
if (signal?.aborted) return cancelledOpen()
if (Date.now() >= h1Until) {
const o = await openH2(base, headers, signal)
@@ -2054,7 +2055,7 @@ async function open(base, headers, signal) {
if (o.blocked) h1Until = Date.now() + H2.retryAfter
}
if (signal?.aborted) return cancelledOpen()
return openH1(headers, signal)
return openH1(headers, signal, atOnce)
}
const H2 = {
@@ -2121,7 +2122,15 @@ function openH2(base, headers, signal) {
})
}
const H1 = { appendWait: 60_000 } // ms a BidiAppend may take before the Run fails, as cursor-agent's appendTimeoutMs
const H1 = {
appendWait: 60_000, // ms a BidiAppend may take before the Run fails, as cursor-agent's appendTimeoutMs
// BidiAppends on their way at once, numbered in order, as cursor-agent
// keeps them: one at a time, a new Run's history (a blob a message,
// hundreds for a long conversation, all asked for at once) took a round
// trip to Cursor each before the model could start. A native session's
// Run keeps them one at a time (runNative).
atOnce: 16,
}
// connectCode is Connect's code for an HTTP status that came with no
// Connect error of its own
@@ -2136,7 +2145,7 @@ function connectCode(status) {
// is a 200 at once; an error Cursor says later, or a call that fails,
// ends the stream with Connect's end frame, which runOnce reads as the
// Run's own.
function openH1(headers, signal) {
function openH1(headers, signal, atOnce = H1.atOnce) {
if (signal?.aborted) return Promise.resolve(cancelledOpen())
const ac = new AbortController()
const id = headers["x-request-id"]
@@ -2144,15 +2153,23 @@ function openH1(headers, signal) {
let ended = false
let seq = 0
let reader
const going = new Set()
const req = new Duplex({
read() {},
write(chunk, _enc, cb) {
// runOnce writes one whole frame each time
const msg = Buffer.from(chunk).subarray(5)
const n = seq++
// Keep the write buffered until its append finishes, so callbacks and
// drain reflect the pending upload rather than an unbounded promise chain.
append(msg, n).then(() => cb(), (e) => cb(e))
const p = append(msg, n)
going.add(p)
const gone = () => going.delete(p)
p.then(gone, gone)
// Up to atOnce appends go at once; past that the write stays
// buffered until one finishes, so callbacks and drain reflect the
// pending upload rather than an unbounded promise chain. At one,
// a write's callback is its own append answered.
if (going.size < atOnce) return cb()
Promise.race(going).then(() => cb(), (e) => cb(e))
},
})
const cleanup = () => {
@@ -2179,7 +2196,7 @@ function openH1(headers, signal) {
}
// append sends one message up. One that gets no answer by its deadline
// (more time for a large one) ends the Run, as cursor-agent's
// bidi_append_deadline_exceeded does: the messages after it wait on it.
// bidi_append_deadline_exceeded does; so does one that fails.
const append = async (msg, n) => {
if (ended || ac.signal.aborted) return
const body = pb().str(1, msg.toString("hex")).bytes(2, pb().str(1, id).done()).varint(3, n).done()
@@ -2415,6 +2432,9 @@ async function runNative({ tok, base, id, tools, conv, convID, signal, maxMode,
"x-cursor-agent-allowed-tools": "mcp_tool_call,get_mcp_tools_tool_call",
},
native.ac.signal,
// one at a time: a native session counts a write as done only when
// Cursor answered its own append, so a tool result is never uncertain
1,
)
if (o.error) {
native.fail(o.error)
+4 -4
View File
@@ -236,7 +236,7 @@ for (const outcome of ["EOF", "HTTP error", "read error"]) {
})
}
test("openH1: write callbacks and drain wait for serialized append response bodies", async () => {
test("openH1 one at a time (a native session's Run): write callbacks and drain wait for serialized append response bodies", async () => {
const downstream = body()
const first = body()
const seqnos = []
@@ -247,7 +247,7 @@ test("openH1: write callbacks and drain wait for serialized append response bodi
seqnos.push(seq)
return new Response(seq === 0 ? first.stream : new Uint8Array(0))
}
const o = keep(await openH1(headers))
const o = keep(await openH1(headers, undefined, 1))
let drained = false
o.req.once("drain", () => { drained = true })
expect(o.req.write(frame(Buffer.alloc(128 * 1024)), () => callbacks.push(0))).toBe(false)
@@ -340,7 +340,7 @@ for (const retained of [0, 32]) for (const outcome of ["head abort", "completion
}
for (const outcome of ["abort", "close"]) {
test(`openH1: ${outcome} cancels a pending append and skips buffered writes`, async () => {
test(`openH1 one at a time: ${outcome} cancels a pending append and skips buffered writes`, async () => {
const downstream = body()
const ac = new AbortController()
const appends = []
@@ -352,7 +352,7 @@ for (const outcome of ["abort", "close"]) {
init.signal.addEventListener("abort", () => reject(init.signal.reason), { once: true })
})
}
const o = keep(await openH1(headers, ac.signal))
const o = keep(await openH1(headers, ac.signal, 1))
o.req.on("error", () => {})
o.req.write(frame(Buffer.from("pending")), () => callbacks.push(0))
o.req.write(frame(Buffer.from("buffered")), () => callbacks.push(1))
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-cursor-auth",
"version": "0.2.27",
"version": "0.2.28",
"description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to",
"type": "module",
"main": "./index.mjs",