Cursor's Run (native) calls stay on one Cursor session across a tool result. With them, a tool that runs longer than Cursor's limit resumes stateless, not as a failed turn. The session pool lives in native-session.mjs. It applies to a conversation's first user turn, and a new user turn goes stateless (see the README). X-Magpie-Cursor-Continuity says which path a request took.
The dashboard-usage ledger counts two distinct events in one response that share a millisecond, as main did. Only timestamps claimed by earlier fetches are left out. A new deferred-usage test covers it.
Run at the reviewed head 398aeddc0186cfc38189b1f05577b181aa2d587d (merge base 5d5d86e; main has no packages/cursor change since):
- packages/cursor `bun test` 3 times: 258 pass, 0 fail each time.
- `bun test latency` 12 times with fresh HOMEs: 12/12 pass.
- Broken by hand: putting back `if (claimed.has(key + "\0" + ev.timestamp) || at >= before()) continue` makes the new test "distinct same-millisecond events in one response are billed together, not again on later fetches" fail (2 pass, 1 fail). The head's line passes.
- `bun scripts/check.mjs`: all packages pass.
- `npm pack`, extracted into a fresh dir: index.mjs, native-session.mjs, package.json, README.md. `import("./index.mjs")` exports CursorAuthPlugin and _internal, and native-session.mjs imports.
- CI: test and on-npm green.
Not tried: a real Cursor account. The owner's stored plugin sign-in has no access token. The author reports a real Claude Code run through an isolated gateway: a child resumed stateless after a 125s tool, and all 6 requests returned 200.
91 lines
3.6 KiB
JavaScript
91 lines
3.6 KiB
JavaScript
// Synthetic wire fixtures, independent of the implementation's PB/fields codec.
|
|
// Field numbers follow agent.v1: no installed CLI or remote endpoint is needed.
|
|
export const vi = (value) => {
|
|
let v = BigInt(value)
|
|
const b = []
|
|
do {
|
|
const n = Number(v & 127n)
|
|
v >>= 7n
|
|
b.push(v ? n | 128 : n)
|
|
} while (v)
|
|
return Buffer.from(b)
|
|
}
|
|
export const uint = (field, value) => Buffer.concat([vi(field * 8), vi(value)])
|
|
export const bin = (field, value) => {
|
|
const b = Buffer.from(value)
|
|
return Buffer.concat([vi(field * 8 + 2), vi(b.length), b])
|
|
}
|
|
export const text = (field, value) => bin(field, Buffer.from(value))
|
|
export const message = (...parts) => Buffer.concat(parts)
|
|
export function readWire(input) {
|
|
const buffer = Buffer.from(input)
|
|
let at = 0
|
|
const varint = () => {
|
|
let result = 0n
|
|
let shift = 0n
|
|
while (at < buffer.length) {
|
|
const b = buffer[at++]
|
|
result |= BigInt(b & 127) << shift
|
|
if (!(b & 128)) return Number(result)
|
|
shift += 7n
|
|
}
|
|
throw new Error("fixture truncated varint")
|
|
}
|
|
const out = []
|
|
while (at < buffer.length) {
|
|
const tag = varint()
|
|
const no = tag >> 3
|
|
const kind = tag & 7
|
|
if (kind === 0) out.push({ no, value: varint() })
|
|
else if (kind === 2) {
|
|
const n = varint()
|
|
if (at + n > buffer.length) throw new Error("fixture truncated bytes")
|
|
out.push({ no, data: buffer.subarray(at, at + n) })
|
|
at += n
|
|
} else throw new Error("fixture unsupported wire")
|
|
}
|
|
return out
|
|
}
|
|
export const item = (b, n) => readWire(b).find((f) => f.no === n)
|
|
export const nest = (b, n) => {
|
|
const f = item(b, n)
|
|
if (!f?.data) throw new Error("fixture missing message " + n)
|
|
return f.data
|
|
}
|
|
export const string = (b, n) => item(b, n)?.data?.toString() ?? ""
|
|
export const number = (b, n) => item(b, n)?.value ?? 0
|
|
export function envelope(body, flags = 0) {
|
|
const h = Buffer.alloc(5)
|
|
h[0] = flags
|
|
h.writeUInt32BE(body.length, 1)
|
|
return Buffer.concat([h, body])
|
|
}
|
|
export const update = (n, body) => envelope(bin(1, bin(n, body)))
|
|
export const end = () => envelope(Buffer.from("{}"), 2)
|
|
export function tool(id, rawID, value = "seed", { name = "audit_echo", provider = "magpie", server = "magpie", approval = false, omitID = false, argumentValue, omitArguments = false } = {}) {
|
|
const args = omitArguments ? Buffer.alloc(0) : bin(2, message(text(1, "value"), bin(2, argumentValue ?? text(3, value))))
|
|
const a = message(text(1, "magpie-" + name), args, text(3, rawID), text(4, provider), text(5, name), text(9, server), ...(approval ? [uint(7, 1)] : []))
|
|
return envelope(bin(2, message(...(omitID ? [] : [uint(1, id)]), text(15, "exec-" + id), bin(11, a))))
|
|
}
|
|
export const setBlob = (request, id, data) => envelope(bin(4, message(uint(1, request), bin(3, message(bin(1, id), bin(2, data))))))
|
|
export const getBlob = (request, id) => envelope(bin(4, message(uint(1, request), bin(2, bin(1, id)))))
|
|
export const checkpoint = (state) => envelope(bin(3, state))
|
|
export const abortExec = (id) => envelope(bin(5, bin(1, uint(1, id))))
|
|
export function readResult(b) {
|
|
const exec = nest(b, 2)
|
|
const result = nest(exec, 11)
|
|
const success = nest(result, 1)
|
|
if (readWire(result).some((f) => f.no !== 1)) throw new Error("fixture expected McpResult.success")
|
|
return {
|
|
id: number(exec, 1),
|
|
execID: string(exec, 15),
|
|
content: readWire(success).filter((f) => f.no === 1).map((f) => {
|
|
const txt = item(f.data, 1)
|
|
if (txt) return { type: "text", text: string(txt.data, 1) }
|
|
const image = nest(f.data, 2)
|
|
return { type: "image", data: nest(image, 1), mimeType: string(image, 2) }
|
|
}),
|
|
isError: !!number(success, 2),
|
|
}
|
|
}
|