cursor: the latency test's far events show once their step has answered, and a step's wait is counted in asks, not ms (#159, #162, #165)

#159 and #162: the "far from Cursor" fake showed a Run's usage event
SHOWS (225 ms scaled) after the Run's stream closed. The step's wait
takes its last look about 180 ms after that close, so only ~45 ms stood
between "the event is not there yet" (what the test means) and "the
event is already there" (the step owns it and the sum moves). A loaded
machine eats 45 ms. #162's title guessed an earlier test's cached
usage; it was the step's own event showing inside its wait.

The fake now holds a far Run's event until the step whose Run it is has
answered, and shows it at once if that step already has: the state the
test is about, not a duration.

#165: test 1 bounded each step's wait in milliseconds, which load
stretches. Each wait asks once and the round trip is most of it, so the
bound is now at most 2 asks per step (its own event, plus what earlier
steps owe).

Run (bun 1.4.2, load average 280-295 throughout):
- Old test, cause injected by showing the event earlier (SHOWS - 50 ms):
  #159's failure; SHOWS - 80 ms: #162's. New test with the same
  injections: green.
- packages/cursor/latency.test.mjs: 9 of 9 green.
- packages/cursor whole package (HOME=$(mktemp -d) bun test): 3 of 3,
  272 pass, 0 fail.
- Broken by hand in index.mjs, each turned the new test red: the step
  waiting for its own event removed; owed() not gathering at the end;
  the ask bound raised (a step asking 3+ times).
Not run: the whole-repo suite (test-only change in one file).
This commit is contained in:
yetone
2026-10-16 19:11:56 +08:00
parent be7b03e090
commit 2ea25b925b
+23 -4
View File
@@ -37,6 +37,7 @@ beforeEach(async () => {
_internal.unreadable?.clear() _internal.unreadable?.clear()
}) })
afterEach(() => { afterEach(() => {
far = true
NATIVE_LIMITS.perProcess = retained NATIVE_LIMITS.perProcess = retained
globalThis.fetch = real globalThis.fetch = real
Object.assign(STEP_WAIT, wait) Object.assign(STEP_WAIT, wait)
@@ -62,12 +63,15 @@ const CURSOR = [
let server, base let server, base
let script = [] // per Run: "tool" or "end", with its CURSOR usage let script = [] // per Run: "tool" or "end", with its CURSOR usage
const shown = [] // the dashboard's events const shown = [] // the dashboard's events
let far = true // a Run's event shows once its step has answered
let asking = null // the step being asked: its Runs' events held till it answers
beforeAll(async () => { beforeAll(async () => {
server = http2.createServer() server = http2.createServer()
server.on("stream", (stream) => { server.on("stream", (stream) => {
stream.respond({ ":status": 200, "content-type": "application/connect+proto" }) stream.respond({ ":status": 200, "content-type": "application/connect+proto" })
let buf = Buffer.alloc(0) let buf = Buffer.alloc(0)
let step let step
const by = asking // the step whose Run this is
stream.on("data", (c) => { stream.on("data", (c) => {
buf = Buffer.concat([buf, c]) buf = Buffer.concat([buf, c])
if (step || buf.length < 5 || buf.length < 5 + buf.readUInt32BE(1)) return if (step || buf.length < 5 || buf.length < 5 + buf.readUInt32BE(1)) return
@@ -94,7 +98,14 @@ beforeAll(async () => {
}) })
stream.on("close", () => { stream.on("close", () => {
if (!step?.usage) return if (!step?.usage) return
setTimeout(() => shown.push({ timestamp: String(Date.now()), model: "claude-fable-5-thinking-max", conversationId: step.conv, tokenUsage: step.usage }), SHOWS) const show = () => shown.push({ timestamp: String(Date.now()), model: "claude-fable-5-thinking-max", conversationId: step.conv, tokenUsage: step.usage })
// Far from Cursor a Run's event shows only once its step has answered,
// never inside that step's wait. Not SHOWS after the Run closed: that
// is 45 ms past the wait's last look, which a loaded machine eats
// (plugins#159, #162).
if (!far) setTimeout(show, SHOWS)
else if (by?.done) show()
else by?.held.push(show)
}) })
stream.on("error", () => {}) stream.on("error", () => {})
}) })
@@ -135,10 +146,14 @@ async function ask(auth, chat, session) {
await hooks["chat.headers"]({ sessionID: session, model: { providerID: "cursor", id: chat.model }, provider: { info: { id: "cursor" } } }, out) await hooks["chat.headers"]({ sessionID: session, model: { providerID: "cursor", id: chat.model }, provider: { info: { id: "cursor" } } }, out)
const l = await hooks.auth.loader(async () => auth) const l = await hooks.auth.loader(async () => auth)
const t0 = Date.now() const t0 = Date.now()
const asked = asks
const me = (asking = { done: false, held: [] })
const res = await l.fetch(base + "/v1/chat/completions", { method: "POST", headers: new Headers(out.headers), body: JSON.stringify(chat) }) const res = await l.fetch(base + "/v1/chat/completions", { method: "POST", headers: new Headers(out.headers), body: JSON.stringify(chat) })
const j = await res.json() const j = await res.json()
me.done = true
for (const show of me.held.splice(0)) show()
expect([res.status, j.error]).toEqual([200, undefined]) expect([res.status, j.error]).toEqual([200, undefined])
return { stop: j.choices[0].finish_reason, usage: j.usage, ms: Date.now() - t0 } return { stop: j.choices[0].finish_reason, usage: j.usage, ms: Date.now() - t0, asks: asks - asked }
} }
const chat = (steps) => { const chat = (steps) => {
@@ -187,8 +202,11 @@ test("far from Cursor, a conversation's tool steps add up to what Cursor counted
expect(steps.map((s) => s.stop)).toEqual(["tool_calls", "tool_calls", "stop"]) expect(steps.map((s) => s.stop)).toEqual(["tool_calls", "tool_calls", "stop"])
// Cursor counted 95–99% of it from the cache; so does the conversation // Cursor counted 95–99% of it from the cache; so does the conversation
expect(sum(steps.map((s) => s.usage))).toEqual(cursorSum(CURSOR)) expect(sum(steps.map((s) => s.usage))).toEqual(cursorSum(CURSOR))
// and no step waited longer than it did before // and no step waited longer than it did before: one wait for its own
for (const s of steps) expect(s.ms).toBeLessThan(STEP_WAIT.until * 2.5) // event, and at most one more for what earlier steps owe. Each wait asks
// once (the round trip is most of it), so count asks, not milliseconds,
// which a loaded machine stretches (plugins#165).
for (const s of steps) expect(s.asks).toBeLessThanOrEqual(2)
}) })
test("an account whose events never show in time stops waiting for them, and the count still adds up", async () => { test("an account whose events never show in time stops waiting for them, and the count still adds up", async () => {
@@ -222,6 +240,7 @@ test("near Cursor a tool step still counts its own Run", async () => {
shown.length = 0 shown.length = 0
const auth = fresh() const auth = fresh()
// the event shows at once, the round trip short // the event shows at once, the round trip short
far = false
const keep = STEP_WAIT.until const keep = STEP_WAIT.until
script = [{ kind: "tool", usage: CURSOR[0] }] script = [{ kind: "tool", usage: CURSOR[0] }]
const fast = globalThis.fetch const fast = globalThis.fetch