A new plugin option, "Cursor web search (test)" (web_search_test, off by
default), sits next to Wire trace. On, a Run adds web_search_tool_call
(ToolCall field 18) to x-cursor-agent-allowed-tools and answers Cursor's
search query with an approval, as cursor-agent does when it auto-approves.
Every other tool of Cursor's own is refused as before. A Run in which
Cursor asked to search saves its WireTrace to cursor-traces whether it
succeeded or failed; a Run that never searched saves nothing unless Wire
trace is on and it failed. The trace is WireTrace as it is (kinds, field
numbers, sizes, times) plus one "web search" event; no search term or
result is written.
Protocol, from cursor-agent 2026.11.05-86047df's agent.v1 descriptors (the
plugin ships no .proto):
- AgentServerMessage 7 interaction_query: InteractionQuery {1 id uint32,
oneof query: 2 web_search_request_query (WebSearchRequestQuery {1 args:
WebSearchArgs {1 search_term, 2 tool_call_id}}), 9 web_fetch, ...}
- AgentClientMessage 6 interaction_response: InteractionResponse {1 id,
oneof result: 2 web_search_request_response (WebSearchRequestResponse
{1 approved {} | 2 rejected {1 reason}})}
- cursor-agent's x1.webSearchApproved builds exactly that approval; with
no args it rejects "Missing web search arguments", and so does this.
Before: native failed the Run on any interaction query ("unsupported native
interaction query", 502); stateless left it unanswered. Both stay so with
the switch off, and for every query but a web search with it on.
Run on mote, HOME a fresh mktemp -d:
- bun test packages/cursor: 421 pass, 0 fail
- bun scripts/check.mjs: ok
- the new off tests against origin/main 907847b's index.mjs and
native-session.mjs: pass (the off behaviour is the old one); the on
tests there: red, as they should be
The full root suite was not run (other reviews were using mote and mica).
Broken by hand, each red (11 tests named plugins#635):
- approval sent as a rejection: 5 red
- native answer not gated on the switch: off test red
- header always adds web_search_tool_call: both off tests red
- header adds shell_tool_call too: 6 red
- stateless never sends the answer: 2 red
- a successful Run without a search saved: 2 red
- a successful Run that searched not saved (ended() a no-op): 2 red
- frame bytes written into the trace: 4 red (secret found)
- any interaction query taken as a search: 3 red
Not tried: Cursor's real servers (fakes only, no real account). Whether
Cursor sends tool_call_started (update 2) for its own search, which would
keep StepCalls from counting it as completed by Cursor, is unknown; the
real trace this switch records is for that. With the switch on, every Run
holds a trace in memory until it ends, as with Wire trace on.
2107 lines
109 KiB
JavaScript
2107 lines
109 KiB
JavaScript
import "./nonet.mjs"
|
||
import { afterEach, beforeEach, expect, spyOn, test } from "bun:test"
|
||
import http2 from "node:http2"
|
||
import { randomUUID } from "node:crypto"
|
||
import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"
|
||
import { tmpdir } from "node:os"
|
||
import { join } from "node:path"
|
||
import { getEventListeners } from "node:events"
|
||
import { CursorAuthPlugin, _internal } from "./index.mjs"
|
||
import { NATIVE_LIMITS, nativeSessions, setWebSearch, setWireTrace } from "./native-session.mjs"
|
||
import { message, bin, text, uint, readWire, nest, number, string, envelope, update, end, tool, setBlob, getBlob, checkpoint, abortExec, readResult } from "./native-test-wire.mjs"
|
||
|
||
const savedFetch = globalThis.fetch
|
||
const limits = { ...NATIVE_LIMITS }
|
||
const usageWait = { ..._internal.STEP_WAIT }
|
||
const agents = []
|
||
beforeEach(async () => {
|
||
await nativeSessions.clear()
|
||
_internal.resetH1()
|
||
Object.assign(_internal.STEP_WAIT, { first: 0, every: 5, until: 40, ask: 250, owedAt: 1000 })
|
||
})
|
||
afterEach(async () => {
|
||
await nativeSessions.clear()
|
||
for (const a of agents.splice(0)) {
|
||
for (const timer of a.timers) clearTimeout(timer)
|
||
for (const s of a.sessions) s.destroy()
|
||
await new Promise((r) => a.server.close(r))
|
||
}
|
||
globalThis.fetch = savedFetch
|
||
for (const key of Object.keys(NATIVE_LIMITS)) if (!(key in limits)) delete NATIVE_LIMITS[key]
|
||
Object.assign(NATIVE_LIMITS, limits)
|
||
Object.assign(_internal.STEP_WAIT, usageWait)
|
||
for (const map of [_internal.owed, _internal.flights, _internal.misses, _internal.unreadable]) map.clear()
|
||
})
|
||
const until = async (predicate) => {
|
||
const deadline = Date.now() + 1200
|
||
while (!predicate()) {
|
||
if (Date.now() >= deadline) throw new Error("fixture did not reach its expected state")
|
||
await new Promise((r) => setTimeout(r, 5))
|
||
}
|
||
}
|
||
const auth = () => ({ type: "oauth", access: `e30.${Buffer.from(JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 7200, sub: "synthetic-" + randomUUID() })).toString("base64url")}.fixture`, refresh: "", accountId: "fixture@example.invalid" })
|
||
const tools = [{ type: "function", function: { name: "audit_echo", description: "synthetic echo", parameters: { type: "object", properties: { value: { type: "string" } } } } }]
|
||
const initial = (extra = {}) => ({ model: "fixture-model", tools, messages: [{ role: "system", content: "synthetic instructions" }, { role: "user", content: "perform two synthetic calls" }], ...extra })
|
||
const continueWith = (chat, reply, values) => ({
|
||
...chat,
|
||
messages: [...chat.messages, reply.choices[0].message, ...reply.choices[0].message.tool_calls.map((c, i) => ({ role: "tool", tool_call_id: c.id, content: values[i] }))],
|
||
})
|
||
const choice = (response) => response.body.choices[0]
|
||
|
||
// The fake only advances on an independently decoded native McpResult followed
|
||
// by the numeric exec close. Replaying a Chat transcript in a fresh Run cannot
|
||
// make the second call or final answer succeed.
|
||
async function agent(options = {}) {
|
||
const firstID = options.omitZeroID ? 0 : 7
|
||
const a = { histories: [], runs: [], started: [], events: [], dashboardRequests: [], results: [], closes: [], unavailable: [], kv: [], errors: [], allowed: [], answers: [], sessions: new Set(), timers: [], closedStreams: 0 }
|
||
a.server = http2.createServer()
|
||
a.server.on("session", (s) => { a.sessions.add(s); s.on("error", () => {}); s.on("close", () => a.sessions.delete(s)) })
|
||
a.server.on("stream", (stream, headers) => {
|
||
a.allowed.push(headers["x-cursor-agent-allowed-tools"])
|
||
stream.on("error", () => {})
|
||
stream.on("close", () => a.closedStreams++)
|
||
stream.respond({ ":status": 200, "content-type": "application/connect+proto" })
|
||
let buffer = Buffer.alloc(0)
|
||
let began = false
|
||
let phase = 0
|
||
const received = new Map()
|
||
const closed = new Set()
|
||
let marker = ""
|
||
let imported = [], bootstrap = new Map()
|
||
const send = (b) => { if (!stream.destroyed && !stream.closed) stream.write(b) }
|
||
a.push = send
|
||
const delay = (fn, ms = 20) => a.timers.push(setTimeout(() => { if (!stream.destroyed && !stream.closed) fn() }, ms))
|
||
const state = (pending, terminal = false) => {
|
||
const root = Buffer.from("fixture-root")
|
||
const user = Buffer.from("fixture-user")
|
||
const thinking = Buffer.from("fixture-thinking-" + phase)
|
||
const turn = Buffer.from("fixture-turn")
|
||
const callID = "call_a\nfc_b"
|
||
const step = bin(3, message(text(1, "server-generated thinking phase " + phase), uint(2, 11)))
|
||
const stepRefs = [thinking]
|
||
const extras = []
|
||
if (terminal) {
|
||
const call = Buffer.from("fixture-tool-step")
|
||
const final = Buffer.from("fixture-answer-step")
|
||
const nativeArgs = message(text(1, "magpie-audit_echo"), text(3, callID), text(4, "magpie"), text(5, "audit_echo"))
|
||
const success = bin(1, bin(1, bin(1, text(1, marker))))
|
||
extras.push([call, bin(2, bin(15, message(bin(1, nativeArgs), bin(2, success))))], [final, bin(1, text(1, marker))])
|
||
stepRefs.push(call, final)
|
||
}
|
||
const turnBytes = bin(1, message(bin(1, user), ...stepRefs.map((id) => bin(2, id))))
|
||
const stateBytes = message(bin(1, root), bin(8, turn), ...(pending ? [text(4, callID)] : []), uint(10, 1), text(22, "cli"))
|
||
send(checkpoint(stateBytes)) // a candidate before its reference closure
|
||
delay(() => {
|
||
const blobs = [[root, Buffer.from('{"role":"system","content":"synthetic root"}')], [user, text(1, "synthetic input")], [thinking, step], ...extras, [turn, turnBytes]]
|
||
for (const [i, [id, b]] of blobs.entries()) send(setBlob(100 + phase * 10 + i, id, b))
|
||
send(getBlob(199 + phase * 10, thinking))
|
||
}, options.stateDelay ?? 20)
|
||
}
|
||
const first = () => {
|
||
if (options.stallInitial) return
|
||
send(update(4, text(1, options.initialReasoning ?? "native planning one")))
|
||
for (const b of options.afterThinking ?? []) send(b)
|
||
// Cursor's own heartbeats, InteractionUpdate.heartbeat, and nothing else
|
||
if (options.serverHeartbeat) {
|
||
const beat = () => delay(() => { send(update(13, Buffer.alloc(0))); beat() }, options.serverHeartbeat)
|
||
beat()
|
||
}
|
||
if (options.toolDisplay) {
|
||
const at = Math.floor(options.toolDisplay.length / 2)
|
||
send(update(1, text(1, options.toolDisplay.slice(0, at))))
|
||
send(update(1, text(1, options.toolDisplay.slice(at))))
|
||
}
|
||
if (options.search) {
|
||
// plugins#635: Cursor's own web search, as cursor-agent's agent.v1
|
||
// has it: the call (ToolCall.web_search_tool_call, 18) being
|
||
// written, then InteractionQuery {id 1, web_search_request_query 2
|
||
// (or another kind's field), args: search_term 1, tool_call_id 2}
|
||
// as AgentServerMessage.interaction_query (7); the search is run
|
||
// once the client answers (AgentClientMessage.interaction_response, 6)
|
||
const q = options.search
|
||
const args = message(text(1, q.term), text(2, "ws1"))
|
||
const call = (n, result) => update(n, message(text(1, "ws1"), bin(2, bin(18, message(bin(1, args), ...(result ? [bin(2, result)] : []))))))
|
||
send(call(7))
|
||
if (q.shell) send(envelope(bin(2, message(uint(1, 78), bin(12, text(1, "synthetic shell request"))))))
|
||
send(envelope(bin(7, message(uint(1, 3), bin(q.query ?? 2, bin(1, args))))))
|
||
a.searched = () => {
|
||
send(call(3, bin(1, bin(1, message(text(1, q.ref), text(2, "https://" + q.ref + ".invalid"))))))
|
||
if (q.fail) return stream.end(envelope(Buffer.from(JSON.stringify({ error: { code: "resource_exhausted", message: "synthetic failure" } })), 2))
|
||
send(update(1, text(1, "searched answer")))
|
||
send(update(14, message(uint(1, 5), uint(2, 3))))
|
||
stream.end(end())
|
||
}
|
||
return
|
||
}
|
||
if (options.unsupportedExec) { send(envelope(bin(2, message(uint(1, 77), bin(12, text(1, "synthetic shell request")))))); return }
|
||
if (options.thinkingOnly) return
|
||
if (options.conflictingCount) {
|
||
const conflict = () => { send(update(27, uint(1, 2))); send(update(27, uint(1, 1))) }
|
||
if (options.initialReasoning === "") conflict()
|
||
else delay(conflict)
|
||
return
|
||
}
|
||
if (options.progressiveFinal || options.finalOnly) {
|
||
send(update(1, text(1, "progressive answer")))
|
||
a.finish = () => {
|
||
send(update(14, message(uint(1, 23), uint(2, 7), uint(3, 10), uint(4, 0))))
|
||
stream.end(end())
|
||
}
|
||
if (options.finalOnly) a.finish()
|
||
return
|
||
}
|
||
const count = options.parallel || options.truncated ? 2 : 1
|
||
const listed = () => { if (!options.noCount && !options.cleanNoCount) send(update(27, uint(1, count))) }
|
||
if (!options.countAfter) listed()
|
||
const c = options.execMessage ? envelope(bin(2, options.execMessage)) : tool(firstID, "call_a\nfc_b", "seed", { ...options, omitID: options.omitZeroID })
|
||
if (options.changedDuplicate || options.extraExec) {
|
||
const extra = options.changedDuplicate ? tool(firstID, "call_a\nfc_b", "changed") : tool(9, "new-exec-after-seal", "unexpected")
|
||
send(envelope(message(c.subarray(5), extra.subarray(5))))
|
||
} else send(c)
|
||
if (options.duplicate) send(c)
|
||
if (options.parallel) {
|
||
const last = tool(8, "call_a__fc_b", "other")
|
||
send(last)
|
||
if (options.duplicateFinal) send(last)
|
||
}
|
||
if (options.countAfter) listed()
|
||
if (options.truncated || options.cleanNoCount) return stream.end(end())
|
||
state(true)
|
||
if (options.serverAbort) delay(() => send(abortExec(7)), 35)
|
||
}
|
||
const advance = () => {
|
||
const expected = phase === 0 && options.parallel ? [firstID, 8] : [phase === 0 ? firstID : 8]
|
||
if (!expected.every((id) => closed.has(id))) return
|
||
if (options.stepUsages?.[phase]) send(update(31, options.stepUsages[phase]))
|
||
if (phase === 0 && !options.parallel && !options.finalAfterFirst) {
|
||
marker = received.get(firstID).content[0].text
|
||
phase = 1
|
||
send(update(4, text(1, "native planning two")))
|
||
if (options.secondConflictingCount) {
|
||
send(update(27, uint(1, 2)))
|
||
send(update(27, uint(1, 1)))
|
||
return
|
||
}
|
||
send(tool(8, "call_a__fc_b", marker, { name: options.name })) // call-before-count in phase two
|
||
if (!options.secondNoCount) send(update(27, uint(1, 1)))
|
||
state(true)
|
||
return
|
||
}
|
||
marker = options.finalAfterFirst ? received.get(firstID).content[0].text : options.parallel ? [7, 8].map((id) => received.get(id).content[0].text).join("|") : received.get(8).content[0].text
|
||
phase = 2
|
||
if (options.stallFinal) return
|
||
// the answer begun and still going when the Run's deadline lands
|
||
if (options.halfFinal) { send(update(1, text(1, "half said"))); return }
|
||
send(update(4, text(1, "native final thinking")))
|
||
send(update(1, text(1, marker)))
|
||
if (options.stepUsages?.[2]) send(update(31, options.stepUsages[2]))
|
||
const usage = options.turnUsage ?? (options.zeroUsage ? message(uint(1, 0), uint(2, 0), uint(5, 0)) : message(uint(1, 23), uint(2, 7), uint(3, 10), uint(4, 0), uint(5, 4)))
|
||
send(update(14, usage))
|
||
state(false, true) // late state after TurnEnded must still be retained
|
||
if (!options.noEnd) delay(() => stream.end(end()), 55)
|
||
}
|
||
stream.on("data", (chunk) => {
|
||
buffer = Buffer.concat([buffer, chunk])
|
||
try {
|
||
while (buffer.length >= 5 && buffer.length >= 5 + buffer.readUInt32BE(1)) {
|
||
const n = buffer.readUInt32BE(1)
|
||
const b = buffer.subarray(5, 5 + n)
|
||
buffer = buffer.subarray(5 + n)
|
||
const fs = readWire(b)
|
||
if (fs.some((f) => f.no === 1)) {
|
||
if (began) throw new Error("more than one RunRequest on retained transport")
|
||
began = true
|
||
const run = nest(b, 1)
|
||
a.runs.push(run)
|
||
a.started.push(Date.now())
|
||
options.onRun?.(a)
|
||
a.histories.push(imported)
|
||
const refs = readWire(nest(run, 1)).filter((f) => f.no === 1)
|
||
bootstrap = new Map(refs.map((f, i) => [1000 + i, i]))
|
||
for (const [i, ref] of refs.entries()) send(getBlob(1000 + i, ref.data))
|
||
if (!refs.length) first()
|
||
} else if (fs.some((f) => f.no === 2)) {
|
||
const r = readResult(b)
|
||
if (received.has(r.id)) throw new Error("duplicate native result submission")
|
||
if (r.execID !== "exec-" + r.id || r.content[0]?.type !== "text") throw new Error("incorrect native result routing/content")
|
||
received.set(r.id, r)
|
||
a.results.push(r)
|
||
} else if (fs.some((f) => f.no === 5)) {
|
||
const control = nest(b, 5)
|
||
const close = readWire(control).find((f) => f.no === 1)
|
||
if (!close) {
|
||
if (readWire(control).some((f) => f.no === 2)) a.unavailable.push(control)
|
||
continue
|
||
}
|
||
const id = number(close.data, 1)
|
||
if (options.search && id === 78) { a.closes.push(id); continue }
|
||
if (options.unsupportedExec && id === 77 || (options.name === "unregistered" || options.provider === "foreign" || options.approval) && id === firstID) {
|
||
send(update(1, text(1, "continued after unavailable")))
|
||
send(update(14, message(uint(1, 5), uint(2, 3))))
|
||
stream.end(end())
|
||
continue
|
||
}
|
||
if (!received.has(id)) throw new Error("exec close arrived before McpResult")
|
||
if (closed.has(id)) throw new Error("duplicate exec close")
|
||
closed.add(id)
|
||
a.closes.push(id)
|
||
advance()
|
||
} else if (fs.some((f) => f.no === 6)) {
|
||
a.answers.push(nest(b, 6))
|
||
a.searched?.()
|
||
} else if (fs.some((f) => f.no === 3)) {
|
||
const kv = nest(b, 3)
|
||
const id = number(kv, 1)
|
||
a.kv.push({ id, fields: readWire(kv) })
|
||
if (bootstrap.has(id)) {
|
||
imported[bootstrap.get(id)] = JSON.parse(nest(nest(kv, 2), 1).toString())
|
||
bootstrap.delete(id)
|
||
if (!bootstrap.size) {
|
||
if ((options.toolRuns ?? [1]).includes(a.runs.length)) first()
|
||
else {
|
||
const results = imported.filter((m) => m.role === "tool").flatMap((m) => m.content)
|
||
const marker = results.flatMap((r) => r.experimental_content ?? r.result?.value?.content ?? []).map((r) => r.text ?? r.content?.value?.text ?? "").join("|")
|
||
send(update(1, text(1, "rebuilt:" + (marker || "new-input"))))
|
||
send(update(14, options.rebuildUsage ?? message(uint(1, 13), uint(2, 4))))
|
||
stream.end(end())
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (e) {
|
||
a.errors.push(e.message)
|
||
if (!stream.destroyed) stream.end(envelope(Buffer.from(JSON.stringify({ error: { code: "invalid_argument", message: e.message } })), 2))
|
||
}
|
||
})
|
||
})
|
||
await new Promise((r) => a.server.listen(0, "127.0.0.1", r))
|
||
a.base = `http://127.0.0.1:${a.server.address().port}`
|
||
agents.push(a)
|
||
globalThis.fetch = async (url, init) => {
|
||
const u = String(url)
|
||
if (u.endsWith("/aiserver.v1.ServerConfigService/GetServerConfig")) return Response.json({ agentUrlConfig: { agentUrl: a.base } })
|
||
if (u.endsWith("/agent.v1.AgentService/GetUsableModels")) return Response.json({ models: [{ modelId: "fixture-model", displayName: "Synthetic" }] })
|
||
if (u.endsWith("/aiserver.v1.AiService/AvailableModels") && options.picker) return Response.json({ models: options.picker })
|
||
if (u.endsWith("/aiserver.v1.DashboardService/GetFilteredUsageEvents")) {
|
||
a.dashboardRequests.push({ account: init.headers.Authorization, at: Date.now(), closed: a.closedStreams })
|
||
return options.dashboard ? await options.dashboard(a, init) : Response.json({ usageEventsDisplay: a.events })
|
||
}
|
||
throw new Error("unexpected fake API request: " + u)
|
||
}
|
||
a.firstID = firstID
|
||
a.ask = async (account, chat, { signal, session = "fixture-session", input } = {}) => {
|
||
const plugin = await CursorAuthPlugin(input)
|
||
const loader = await plugin.auth.loader(async () => account)
|
||
const res = await loader.fetch(a.base + "/v1/chat/completions", { method: "POST", headers: { "x-magpie-cursor-session": session }, body: JSON.stringify(chat), signal })
|
||
if (chat.stream) return res
|
||
return { status: res.status, body: await res.json(), diagnostic: res.headers.get("X-Magpie-Cursor-Continuity") }
|
||
}
|
||
return a
|
||
}
|
||
|
||
// A fresh Run must obtain already provided client results from bootstrap blobs.
|
||
// The marker is never inferred from Chat requests or from native result writes.
|
||
const rebuilt = (response, marker) => {
|
||
expect(response.status).toBe(200)
|
||
expect(choice(response).message.content).toContain("rebuilt:" + marker)
|
||
expect(response.diagnostic).toMatch(/^stateless;/)
|
||
}
|
||
|
||
test("native retention: healthy two external roundtrips use one Run", async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const marker = "synthetic-" + randomUUID() + "\n精确结果"
|
||
const next = continueWith(chat, first.body, [marker])
|
||
const second = await a.ask(account, next)
|
||
expect(second.status).toBe(200)
|
||
expect(JSON.parse(choice(second).message.tool_calls[0].function.arguments)).toEqual({ value: marker })
|
||
const final = await a.ask(account, continueWith(next, second.body, [marker]))
|
||
expect(final.status).toBe(200)
|
||
expect(choice(final).message.content).toBe(marker)
|
||
expect(final.body.usage).toMatchObject({ prompt_tokens: 23, completion_tokens: 7, total_tokens: 30 })
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results.map((r) => r.id)).toEqual([7, 8])
|
||
expect(a.closes).toEqual([7, 8])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
// #156 (erzay, Codex on cursor/claude-opus-5-5@1m): a turn of about 20 tool
|
||
// steps ended with TurnEndedUpdate's totals, 1,105,934 prompt (1,035,734 read
|
||
// from the cache, 70,156 written) as the final step's prompt_tokens; Codex
|
||
// read that as its context and compacted a 61K one. The last call keeps its
|
||
// own share; the rest is still billed, as magpie_deferred.
|
||
test("native accounting: a multi-step turn's final step reports its own share, the rest deferred (#156)", async () => {
|
||
const turnUsage = message(uint(1, 1105934), uint(2, 2100), uint(3, 1035734), uint(4, 70156), uint(5, 900))
|
||
const a = await agent({ turnUsage }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["one"])
|
||
const second = await a.ask(account, next)
|
||
const final = await a.ask(account, continueWith(next, second.body, ["two"]))
|
||
expect(final.status).toBe(200)
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(number(a.runs[0], 37)).toBe(1) // emit_model_call_usage
|
||
const u = final.body.usage, d = u.magpie_deferred
|
||
// three model calls: none is more than half the turn
|
||
expect(u.prompt_tokens).toBeLessThan(1105934 / 2)
|
||
expect(u.prompt_tokens).toBeGreaterThan(0)
|
||
expect(u.total_tokens).toBe(u.prompt_tokens + u.completion_tokens)
|
||
expect(u.prompt_tokens + d.prompt_tokens).toBe(1105934)
|
||
expect(u.prompt_tokens_details.cached_tokens + d.cached_tokens).toBe(1035734)
|
||
expect(u.prompt_tokens_details.cache_write_tokens + d.cache_write_tokens).toBe(70156)
|
||
expect(u.completion_tokens + d.completion_tokens).toBe(2100)
|
||
expect(u.completion_tokens_details.reasoning_tokens).toBeLessThanOrEqual(u.completion_tokens)
|
||
})
|
||
|
||
test("native accounting: ModelCallCompletedUpdate (31) gives the final step its exact context and defers the rest (#156)", async () => {
|
||
// Three model calls: 50K -> 55K -> 61K context; TurnEndedUpdate sums all three (166K).
|
||
const stepUsages = [
|
||
message(text(1, "mc-1"), uint(2, 50000), uint(3, 600), uint(4, 25000), uint(5, 24990), uint(6, 200)),
|
||
message(text(1, "mc-2"), uint(2, 55000), uint(3, 800), uint(4, 50000), uint(5, 4990), uint(6, 300)),
|
||
message(text(1, "mc-3"), uint(2, 61000), uint(3, 700), uint(4, 55000), uint(5, 5980), uint(6, 400)),
|
||
]
|
||
const turnUsage = message(uint(1, 166000), uint(2, 2100), uint(3, 130000), uint(4, 35960), uint(5, 900))
|
||
const a = await agent({ stepUsages, turnUsage }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["one"])
|
||
const second = await a.ask(account, next)
|
||
const final = await a.ask(account, continueWith(next, second.body, ["two"]))
|
||
expect(final.status).toBe(200)
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(number(a.runs[0], 37)).toBe(1)
|
||
expect(final.body.usage).toEqual({
|
||
prompt_tokens: 61000,
|
||
completion_tokens: 700,
|
||
total_tokens: 61700,
|
||
prompt_tokens_details: { cached_tokens: 55000, cache_write_tokens: 5980 },
|
||
completion_tokens_details: { reasoning_tokens: 400 },
|
||
magpie_deferred: {
|
||
prompt_tokens: 105000,
|
||
completion_tokens: 1400,
|
||
cached_tokens: 75000,
|
||
cache_write_tokens: 29980,
|
||
},
|
||
})
|
||
})
|
||
|
||
test("native retention: Task parent awaiting results permits a child sharing session with different first user", async () => {
|
||
const a = await agent({ name: "Task" }), account = auth(), parent = initial({ tools: [{ type: "function", function: { name: "Task", parameters: { type: "object" } } }] })
|
||
const first = await a.ask(account, parent)
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const child = initial({ messages: [{ role: "user", content: "child task with separate first user" }] })
|
||
const response = await a.ask(account, child)
|
||
expect(response.status).toBe(200)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(string(a.runs[0], 5)).not.toBe(string(a.runs[1], 5))
|
||
expect(owner.state).toBe("awaiting-results")
|
||
expect(owner.error).toBeUndefined()
|
||
const continued = await a.ask(account, continueWith(parent, first.body, ["child finished"]))
|
||
expect(continued.status).toBe(200)
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.runs).toHaveLength(2)
|
||
})
|
||
|
||
test("native retention: a near-expiry Run rebuilds before submitting results or exposing output", async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const now = Date.now.bind(Date)
|
||
const clock = spyOn(Date, "now").mockImplementation(() => now() + NATIVE_LIMITS.lifetime - 1000)
|
||
try {
|
||
const marker = "result-before-lifetime-boundary"
|
||
rebuilt(await a.ask(account, continueWith(chat, first.body, [marker])), marker)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toEqual([])
|
||
expect(owner.state).toBe("expired")
|
||
expect(nativeSessions.entries.has(owner)).toBe(false)
|
||
expect(a.errors).toEqual([])
|
||
} finally { clock.mockRestore() }
|
||
})
|
||
|
||
// plugins#616 (yetone/magpie#4419, Okxdd123; ggbdpq's cause and repro): a
|
||
// Pi turn's 13th tool step went on in its retained Run with 494 s of the
|
||
// 15-minute lifetime left, and the answer was still streaming when the
|
||
// deadline cut it. A step with less than continueReserve left starts on a
|
||
// fresh Run instead; one with more still goes on in the same Run. The
|
||
// clock is moved, nothing waits.
|
||
for (const [left, fresh] of [[494_000, true], [NATIVE_LIMITS.continueReserve - 1000, true], [NATIVE_LIMITS.continueReserve + 60_000, false]]) test(`native retention: a continuation with ${left / 1000}s of the lifetime left ${fresh ? "rebuilds on a fresh Run" : "goes on in the same Run"} (#616)`, async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const now = Date.now.bind(Date)
|
||
const shift = owner.expiresAt - now() - left
|
||
const clock = spyOn(Date, "now").mockImplementation(() => now() + shift)
|
||
try {
|
||
const marker = "step-with-" + left
|
||
const next = await a.ask(account, continueWith(chat, first.body, [marker]))
|
||
if (fresh) {
|
||
rebuilt(next, marker)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toEqual([])
|
||
expect(owner.state).toBe("expired")
|
||
} else {
|
||
expect(next.status).toBe(200)
|
||
expect(next.diagnostic).toMatch(/^same-run/)
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toHaveLength(1)
|
||
}
|
||
expect(a.errors).toEqual([])
|
||
} finally { clock.mockRestore() }
|
||
})
|
||
|
||
// When the deadline still lands mid-answer, the 200 is long sent and the
|
||
// agent reads only the error's words: they must be ones it asks again on.
|
||
// Pi judges by the text alone (its _isRetryableError), as magpie's
|
||
// passingSaid does for a stream error; both regexes are copied here.
|
||
const piRetries = /overloaded|provider.?returned.?error|rate.?limit|too many requests|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|network.?error|connection.?error|connection.?refused|connection.?lost|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up|ended without|http2 request did not get a response|timed? out|timeout|terminated|retry delay/i
|
||
const magpiePassing = /\b(429|500|502|503|504)\b|timed? ?out|rate.?limit|too many requests|overloaded|service.?unavailable|server.?error|internal.?error|connection.?(error|refused|reset)|retry your request|try your request again/i
|
||
test("native retention: a Run cut at its deadline mid-answer says to retry the request (#616)", async () => {
|
||
const a = await agent({ finalAfterFirst: true, halfFinal: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const res = await a.ask(account, { ...continueWith(chat, first.body, ["after the tool"]), stream: true })
|
||
expect(res.status).toBe(200)
|
||
const reader = res.body.getReader(), dec = new TextDecoder()
|
||
let sse = ""
|
||
while (!sse.includes("half said")) {
|
||
const { value, done } = await reader.read()
|
||
if (done) break
|
||
sse += dec.decode(value, { stream: true })
|
||
}
|
||
expect(sse).toContain("half said")
|
||
// the deadline brought in to now: the answer is still going
|
||
owner.expireIn(0)
|
||
for (;;) {
|
||
const { value, done } = await reader.read()
|
||
if (done) break
|
||
sse += dec.decode(value, { stream: true })
|
||
}
|
||
const errors = sse.split("\n\n").filter((s) => s.startsWith("data: {")).map((s) => JSON.parse(s.slice(6)).error).filter(Boolean)
|
||
expect(errors).toHaveLength(1)
|
||
expect(errors[0].message).toStartWith("native Run lifetime expired")
|
||
expect(errors[0].message).toMatch(piRetries)
|
||
expect(errors[0].message).toMatch(magpiePassing)
|
||
expect(errors[0].message).toContain("retry your request")
|
||
expect(owner.state).toBe("expired")
|
||
})
|
||
|
||
for (const limit of ["resultWait", "lifetime"]) test(`native retention: ${limit} expiry rebuilds supplied results on a fresh stateless Run`, async () => {
|
||
// #143: the lifetime counts from the Run's start, so a short one set before
|
||
// the first ask expired a first Run slower than it on a loaded machine.
|
||
// The result wait counts from the answer; the lifetime is brought in after
|
||
// it, the first Run having all of it.
|
||
if (limit === "resultWait") NATIVE_LIMITS[limit] = 65
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
if (limit === "lifetime") owner.expireIn(65)
|
||
await until(() => owner.state === "expired")
|
||
const marker = "late-result-" + limit
|
||
rebuilt(await a.ask(account, continueWith(chat, first.body, [marker])), marker)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
for (const loss of ["clear", "restart"]) test(`native retention: process ${loss} rebuilds history without the former owner`, async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["process-" + loss])
|
||
const owner = [...nativeSessions.entries][0]
|
||
await nativeSessions.clear()
|
||
if (loss === "restart") {
|
||
// Synthetic process replacement loses every ownership map, without loading
|
||
// another process's configuration or requiring native history validation.
|
||
nativeSessions.entries.clear(); nativeSessions.handles.clear(); nativeSessions.selectors.clear()
|
||
next.messages.at(-2).content = "caller retained only its display transcript"
|
||
}
|
||
rebuilt(await a.ask(account, next), "process-" + loss)
|
||
expect(owner.state).toBe("cancelled")
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toEqual([])
|
||
})
|
||
|
||
test("native retention: cross-account continuation rebuilds history without writing to the original account", async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
rebuilt(await a.ask(auth(), continueWith(chat, first.body, ["different-account"])), "different-account")
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toEqual([])
|
||
expect(owner.state).toBe("awaiting-results")
|
||
})
|
||
|
||
for (const kind of ["same first user", "different tools", "pending ancestor"]) test(`native retention: busy owner with ${kind} falls back without cancelling its live parent`, async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
let branch = structuredClone(chat)
|
||
let marker = "new-input"
|
||
if (kind === "different tools") branch.tools[0].function.description = "child-specific tools"
|
||
if (kind === "pending ancestor") {
|
||
branch.messages.push(choice(first).message, { role: "user", content: "child request includes a pending parent Task" })
|
||
marker = "Tool use was interrupted and did not produce a result."
|
||
}
|
||
rebuilt(await a.ask(account, branch), marker)
|
||
expect(owner.state).toBe("awaiting-results")
|
||
expect(owner.error).toBeUndefined()
|
||
expect(owner.transportClosed).not.toBe(true)
|
||
expect(a.results).toEqual([])
|
||
expect(a.runs).toHaveLength(2)
|
||
const second = await a.ask(account, continueWith(chat, first.body, ["parent result"]))
|
||
expect(second.status).toBe(200)
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.runs).toHaveLength(2)
|
||
})
|
||
|
||
// Claude Code's reminders: a tool result with text beside it reaches the
|
||
// plugin as the tool message, then a user message (magpie's chat.go).
|
||
const withNewInput = (chat, reply, values, text) => {
|
||
const next = continueWith(chat, reply, values)
|
||
next.messages.push({ role: "user", content: text })
|
||
return next
|
||
}
|
||
|
||
test("native retention: new input past a busy owner gets an owner of its own and leaves the parent's reservation", async () => {
|
||
const a = await agent({ finalAfterFirst: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const parent = [...nativeSessions.entries][0]
|
||
const key = parent.account + ":" + parent.selector
|
||
const branch = withNewInput(chat, first.body, ["already-provided"], "branch on new input")
|
||
const res = await a.ask(account, branch)
|
||
expect(res.status).toBe(200)
|
||
expect(choice(res).message.content).toContain("rebuilt:already-provided")
|
||
expect(res.diagnostic).toMatch(/^same-run;/)
|
||
expect(nativeSessions.entries.size).toBe(2)
|
||
expect(nativeSessions.selectors.get(key)).toBe(parent)
|
||
expect(parent.state).toBe("awaiting-results")
|
||
expect(parent.error).toBeUndefined()
|
||
expect(parent.transportClosed).not.toBe(true)
|
||
expect(a.results).toEqual([])
|
||
expect(a.runs).toHaveLength(2)
|
||
const second = await a.ask(account, continueWith(chat, first.body, ["parent result"]))
|
||
expect(second.status).toBe(200)
|
||
expect(second.diagnostic).toMatch(/^same-run;/)
|
||
expect(choice(second).message.content).toBe("parent result")
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.runs).toHaveLength(2)
|
||
})
|
||
|
||
test("native retention: an owner past a busy reservation is resumed by its own calls' results", async () => {
|
||
const a = await agent({ finalAfterFirst: true, toolRuns: [1, 2] }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const parent = [...nativeSessions.entries][0]
|
||
const branch = withNewInput(chat, first.body, ["already-provided"], "the user hasn't heard from you")
|
||
const step = await a.ask(account, branch)
|
||
expect(step.status).toBe(200)
|
||
expect(choice(step).finish_reason).toBe("tool_calls")
|
||
const own = [...nativeSessions.entries].find((e) => e !== parent)
|
||
expect(choice(step).message.tool_calls[0].id).toContain(own.identity)
|
||
const next = await a.ask(account, continueWith(branch, step.body, ["its own result"]))
|
||
expect(next.status).toBe(200)
|
||
expect(next.diagnostic).toMatch(/^same-run;/)
|
||
expect(choice(next).message.content).toBe("its own result")
|
||
expect(a.results.map((r) => r.content[0].text)).toEqual(["its own result"])
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(parent.state).toBe("awaiting-results")
|
||
})
|
||
|
||
for (const bound of ["perAccount", "perProcess"]) test(`native retention: ${bound} capacity selects production stateless fallback`, async () => {
|
||
NATIVE_LIMITS[bound] = 1
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
rebuilt(await a.ask(account, initial({ messages: [{ role: "user", content: "independent capacity request" }] }), { session: "second-session" }), "new-input")
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(nativeSessions.entries.size).toBe(1)
|
||
expect(owner.state).toBe("awaiting-results")
|
||
})
|
||
|
||
for (const change of ["model", "reasoning_effort", "tools", "metadata", "history", "assistant output", "missing parallel result", "unsupported result"]) test(`native retention: incompatible ${change} rebuilds history instead of enforcing native matching`, async () => {
|
||
const a = await agent({ parallel: change === "missing parallel result" }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["compatible-history", "second-result"])
|
||
if (change === "model") next.model = "different-model"
|
||
if (change === "reasoning_effort") next.reasoning_effort = "high"
|
||
if (change === "tools") next.tools = []
|
||
if (change === "metadata") next.metadata = { synthetic: "new trace" }
|
||
if (change === "history") next.messages[0].content = "changed instructions"
|
||
if (change === "assistant output") next.messages.at(-2).content = "different display text"
|
||
if (change === "missing parallel result") next.messages.pop()
|
||
if (change === "unsupported result") next.messages.at(-1).content = [{ type: "file", file: { filename: "opaque.txt" } }, { type: "text", text: "compatible-history" }]
|
||
rebuilt(await a.ask(account, next), "compatible-history")
|
||
expect(a.results).toEqual([])
|
||
expect(a.runs).toHaveLength(2)
|
||
})
|
||
|
||
test("native retention: terminal old handles do not wedge a new user turn", async () => {
|
||
const a = await agent({ finalAfterFirst: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["finished-tool"])
|
||
const final = await a.ask(account, next)
|
||
expect(final.status).toBe(200)
|
||
next.messages.push(choice(final).message, { role: "user", content: "new turn after terminal owner" })
|
||
// every old call has its result, so the new turn gets a native owner of its own
|
||
const fresh = await a.ask(account, next)
|
||
expect(fresh.status).toBe(200)
|
||
expect(choice(fresh).message.content).toContain("rebuilt:finished-tool")
|
||
expect(fresh.diagnostic).toMatch(/^same-run;/)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toHaveLength(1)
|
||
})
|
||
|
||
test("native retention: unknown exec is refused and the model continues", async () => {
|
||
const a = await agent({ unsupportedExec: true })
|
||
const result = await a.ask(auth(), initial())
|
||
expect(result.status).toBe(200)
|
||
expect(choice(result).message.content).toBe("continued after unavailable")
|
||
expect(a.unavailable).toHaveLength(1)
|
||
expect(a.results).toEqual([])
|
||
})
|
||
|
||
test("native retention: baseline unknown fields and stop remain compatible", async () => {
|
||
const a = await agent({ finalOnly: true })
|
||
const result = await a.ask(auth(), initial({ stop: "END", caller_metadata: { synthetic: true } }))
|
||
expect(result.status).toBe(200)
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|
||
|
||
for (const effort of ["none", "off"]) test(`native retention: ${effort} without a served off variant uses legacy thinking=false`, async () => {
|
||
const variant = { legacySlug: "synthetic-thinking-high", parameterValues: [{ id: "context", value: "200k" }, { id: "thinking", value: "true" }, { id: "effort", value: "high" }], isDefaultNonMaxConfig: true }
|
||
const picker = [{ name: "synthetic-thinking", supportsNonMaxMode: true, parameterDefinitions: [
|
||
{ id: "context", parameterType: { enumParameter: { values: [{ value: "200k" }] } } },
|
||
{ id: "thinking", parameterType: { booleanParameter: { values: [{ value: "true" }, { value: "false" }] } } },
|
||
{ id: "effort", parameterType: { enumParameter: { values: [{ value: "low" }, { value: "high" }] } } },
|
||
], variants: [variant] }]
|
||
const a = await agent({ picker, finalOnly: true })
|
||
const result = await a.ask(auth(), initial({ model: "synthetic-thinking@200k", reasoning_effort: effort }))
|
||
expect(result.status).toBe(200)
|
||
const params = readWire(nest(a.runs[0], 9)).filter((f) => f.no === 3).map((f) => [string(f.data, 1), string(f.data, 2)])
|
||
expect(Object.fromEntries(params).thinking).toBe("false")
|
||
})
|
||
|
||
async function twoRounds(a, account, chat = initial(), options) {
|
||
const first = await a.ask(account, chat, options)
|
||
expect(first.status).toBe(200)
|
||
expect(choice(first).finish_reason).toBe("tool_calls")
|
||
const firstRuns = a.runs.length
|
||
const marker = "synthetic-" + randomUUID() + "\n精确结果"
|
||
const secondChat = continueWith(chat, first.body, [marker])
|
||
const second = await a.ask(account, secondChat)
|
||
expect(second.status).toBe(200)
|
||
expect(choice(second).finish_reason).toBe("tool_calls")
|
||
expect(JSON.parse(choice(second).message.tool_calls[0].function.arguments)).toEqual({ value: marker })
|
||
const secondRuns = a.runs.length
|
||
const finalChat = continueWith(secondChat, second.body, [marker])
|
||
const final = await a.ask(account, finalChat)
|
||
expect(final.status).toBe(200)
|
||
expect(choice(final).finish_reason).toBe("stop")
|
||
expect(choice(final).message.content).toBe(marker)
|
||
expect(choice(final).message.tool_calls).toBeUndefined()
|
||
expect(a.runs).toHaveLength(1)
|
||
expect([firstRuns, secondRuns - firstRuns, a.runs.length - secondRuns]).toEqual([1, 0, 0])
|
||
expect(a.results.map((r) => r.id)).toEqual([a.firstID, 8])
|
||
expect(a.closes).toEqual([a.firstID, 8])
|
||
expect(a.results.every((r) => r.content[0].text === marker)).toBe(true)
|
||
expect(a.errors).toEqual([])
|
||
return { first, second, final, finalChat, secondChat, marker }
|
||
}
|
||
|
||
// Generated offline by Cursor CLI 2026.08.11-e8db854's actual
|
||
// ExecServerMessage serializer with id=0; it omits nonoptional scalar field 1.
|
||
const ZERO_EXEC = Buffer.from("5a370a116d61677069652d61756469745f6563686f1a0e73796e7468657469632d63616c6c22066d61677069652a0a61756469745f6563686f7a1373796e7468657469632d657865632d7a65726f", "hex")
|
||
|
||
test("native exec: actual descriptor golden omits id=0 and routes to numeric zero", () => {
|
||
expect(readWire(ZERO_EXEC).some((f) => f.no === 1)).toBe(false)
|
||
const call = _internal.exec(ZERO_EXEC, [{ name: "audit_echo" }], () => { throw new Error("unexpected native write") }, () => {}, null, true)
|
||
expect(call).toMatchObject({ requestID: 0, execID: "synthetic-exec-zero", nativeID: "synthetic-call", name: "audit_echo", args: "{}" })
|
||
})
|
||
|
||
test("native exec: malformed explicit id is rejected instead of defaulting to zero", () => {
|
||
for (const id of [bin(1, Buffer.from([0])), uint(1, 2 ** 32)]) {
|
||
expect(() => _internal.exec(message(id, ZERO_EXEC), [{ name: "audit_echo" }], () => {}, () => {}, null, true)).toThrow("invalid native exec request ID")
|
||
}
|
||
})
|
||
|
||
test("native HTTP/2: omitted-zero exec ID survives two result roundtrips on one Run", async () => {
|
||
const a = await agent({ omitZeroID: true })
|
||
const { final } = await twoRounds(a, auth())
|
||
expect(final.body.usage.prompt_tokens_details).toEqual({ cached_tokens: 10, cache_write_tokens: 0 })
|
||
expect(a.results[0].execID).toBe("exec-0")
|
||
})
|
||
|
||
|
||
test("native loader: real matched tool result preserves text/image/text and isError on the same exec", async () => {
|
||
const a = await agent()
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const png = Buffer.from("89504e470d0a1a0a", "hex")
|
||
const next = continueWith(chat, first.body, [[{ type: "text", text: "before" }, { type: "image_url", image_url: { url: "data:image/png;base64," + png.toString("base64") } }, { type: "text", text: "after" }]])
|
||
next.messages.at(-1).isError = true
|
||
const second = await a.ask(account, next)
|
||
expect(second.status).toBe(200)
|
||
expect(a.results[0]).toEqual({ id: 7, execID: "exec-7", isError: true, content: [
|
||
{ type: "text", text: "before" }, { type: "image", data: png, mimeType: "image/png" }, { type: "text", text: "after" },
|
||
] })
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|
||
|
||
|
||
for (const representation of ["empty-array", "text-parts", "reasoning-parts", "split-pieces"]) test(`native loader: canonical assistant ${representation} matches current output and accepted history`, async () => {
|
||
const a = await agent({ toolDisplay: representation === "empty-array" ? "" : "First piece.Second piece." })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["canonical display"])
|
||
const assistant = next.messages.at(-2)
|
||
const text = assistant.content, reasoning = assistant.reasoning_content
|
||
if (representation === "empty-array") assistant.content = []
|
||
else if (representation === "text-parts") assistant.content = [{ type: "text", text: text.slice(0, 5) }, { type: "text", text: text.slice(5) }]
|
||
else if (representation === "reasoning-parts") {
|
||
assistant.content = [{ type: "reasoning", text: reasoning }, { type: "text", text }]
|
||
delete assistant.reasoning_content
|
||
} else {
|
||
next.messages.splice(-2, 1,
|
||
{ role: "assistant", content: text.slice(0, 5), reasoning_content: reasoning.slice(0, 5) },
|
||
{ ...assistant, content: text.slice(5), reasoning_content: reasoning.slice(5) })
|
||
}
|
||
const changed = structuredClone(next)
|
||
changed.messages.at(-2).content = [{ type: "text", text: text + "changed" }]
|
||
rebuilt(await a.ask(account, changed), "canonical display")
|
||
expect(a.results).toEqual([])
|
||
const second = await a.ask(account, next)
|
||
expect(second.status).toBe(200)
|
||
const finalChat = continueWith(next, second.body, ["canonical display"])
|
||
if (representation === "split-pieces") {
|
||
for (const m of finalChat.messages.slice(chat.messages.length, chat.messages.length + 2)) m.content = [{ type: "text", text: m.content }]
|
||
} else finalChat.messages[chat.messages.length] = choice(first).message
|
||
const final = await a.ask(account, finalChat)
|
||
expect(final.status).toBe(200)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results.map((r) => r.id)).toEqual([7, 8])
|
||
})
|
||
|
||
|
||
test("native loader: streamed ordinary text/thinking arrives before the server permits completion", async () => {
|
||
const a = await agent({ progressiveFinal: true })
|
||
let timer
|
||
const timeout = new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("progressive output waited for completion")), 1000) })
|
||
try {
|
||
const response = await Promise.race([a.ask(auth(), initial({ stream: true })), timeout])
|
||
expect(response.status).toBe(200)
|
||
const reader = response.body.getReader()
|
||
let sse = ""
|
||
while (!sse.includes("progressive answer")) sse += new TextDecoder().decode((await Promise.race([reader.read(), timeout])).value)
|
||
expect(sse).toContain("native planning one")
|
||
expect(sse).not.toContain("[DONE]")
|
||
expect([...nativeSessions.entries][0].terminal).toBe(false)
|
||
a.finish()
|
||
while (true) {
|
||
const item = await reader.read()
|
||
if (item.done) break
|
||
sse += new TextDecoder().decode(item.value)
|
||
}
|
||
expect(sse).toContain("[DONE]")
|
||
expect(response.headers.get("X-Magpie-Cursor-Continuity")).toContain("same-run")
|
||
} finally { clearTimeout(timer) }
|
||
})
|
||
|
||
|
||
for (const bound of ["bytes", "messages"]) test(`native loader HTTP/2: KV replies stalled past writeWait at the queued ${bound} bound fail the Run`, async () => {
|
||
NATIVE_LIMITS.outboundBytes = 4 << 20
|
||
NATIVE_LIMITS.writeWait = 100
|
||
NATIVE_LIMITS.outboundMessages = bound === "messages" ? 4 : 256
|
||
const a = await agent({ stateDelay: 10_000 })
|
||
const connect = http2.connect
|
||
let req
|
||
http2.connect = (...args) => {
|
||
const session = connect(...args)
|
||
const request = session.request
|
||
session.request = function (...args) { req = request.apply(this, args); return req }
|
||
return session
|
||
}
|
||
try {
|
||
const first = await a.ask(auth(), initial())
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const blobID = Buffer.from("synthetic-h2-queued-blob")
|
||
a.push(setBlob(90, blobID, Buffer.alloc(bound === "bytes" ? 1 << 20 : 1)))
|
||
await until(() => a.kv.some((k) => k.id === 90))
|
||
const stalledWrites = []
|
||
req.write = (chunk) => { stalledWrites.push(chunk); return false }
|
||
for (let i = 0; i < 10; i++) a.push(getBlob(91 + i, blobID))
|
||
await until(() => !!owner.error)
|
||
expect(owner.error.status).toBe(503)
|
||
expect(owner.error.message).toContain("outbound")
|
||
expect(owner.error.message).toContain(bound)
|
||
expect(stalledWrites).toHaveLength(1)
|
||
expect(owner.transport.outbound).toEqual({ bytes: 0, messages: 0 })
|
||
await until(() => a.closedStreams === 1)
|
||
expect(a.results).toEqual([])
|
||
expect(a.runs).toHaveLength(1)
|
||
} finally { http2.connect = connect }
|
||
})
|
||
|
||
|
||
test("native loader HTTP/2: write callback alone does not bypass false-write backpressure; drain preserves the result loop", async () => {
|
||
const a = await agent({ stateDelay: 10_000 })
|
||
const connect = http2.connect
|
||
let req
|
||
http2.connect = (...args) => {
|
||
const session = connect(...args)
|
||
const request = session.request
|
||
session.request = function (...args) { req = request.apply(this, args); return req }
|
||
return session
|
||
}
|
||
try {
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const write = req.write
|
||
const writes = []
|
||
req.write = (chunk, callback) => { writes.push(chunk); queueMicrotask(() => callback?.()); return false }
|
||
a.push(getBlob(90, Buffer.from("missing-a")))
|
||
a.push(getBlob(91, Buffer.from("missing-b")))
|
||
await until(() => writes.length > 0)
|
||
await new Promise((r) => setTimeout(r, 15))
|
||
expect(writes).toHaveLength(1)
|
||
req.emit("drain")
|
||
await until(() => writes.length === 2)
|
||
req.write = write
|
||
req.emit("drain")
|
||
const secondChat = continueWith(chat, first.body, ["after drain"])
|
||
const second = await a.ask(account, secondChat)
|
||
const final = await a.ask(account, continueWith(secondChat, second.body, ["after drain"]))
|
||
expect(final.status).toBe(200)
|
||
expect(a.results.map((r) => r.id)).toEqual([7, 8])
|
||
expect(a.closes).toEqual([7, 8])
|
||
expect(a.runs).toHaveLength(1)
|
||
} finally { http2.connect = connect }
|
||
})
|
||
|
||
test("native HTTP/2: background reader retains/ACKs late blobs and closes checkpoint references while parked", async () => {
|
||
const a = await agent({ stateDelay: 60 })
|
||
const first = await a.ask(auth(), initial())
|
||
expect(first.status).toBe(200)
|
||
const owner = [...nativeSessions.entries][0]
|
||
expect(owner.checkpoint.status).toBe("incomplete")
|
||
await until(() => a.kv.some((k) => k.id === 199))
|
||
expect(owner.state).toBe("awaiting-results")
|
||
expect(owner.checkpoint.status).toBe("complete")
|
||
const response = a.kv.find((k) => k.id === 199).fields.find((f) => f.no === 2).data
|
||
expect(nest(response, 1)).toEqual(owner.blobs.get(Buffer.from("fixture-thinking-0").toString("hex")))
|
||
expect(a.closedStreams).toBe(0)
|
||
})
|
||
|
||
for (const countAfter of [false, true]) test(`native HTTP/2: parallel calls count ${countAfter ? "after" : "before"}, colliding raw IDs, reverse results, exactly one submission`, async () => {
|
||
const a = await agent({ parallel: true, countAfter, duplicate: true })
|
||
const account = auth()
|
||
const chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
const calls = choice(first).message.tool_calls
|
||
expect(calls).toHaveLength(2)
|
||
expect(new Set(calls.map((c) => c.id)).size).toBe(2)
|
||
const continuation = continueWith(chat, first.body, ["result-7", "result-8"])
|
||
continuation.messages.splice(-2, 2, ...continuation.messages.slice(-2).reverse())
|
||
const final = await a.ask(account, continuation)
|
||
expect(final.status).toBe(200)
|
||
expect(choice(final).message.content).toBe("result-7|result-8")
|
||
const replay = await a.ask(account, continuation)
|
||
expect(choice(replay).message.content).toBe(choice(final).message.content)
|
||
expect(replay.body.usage.magpie_native.accounting).toBe("replayed")
|
||
expect(replay.body.usage.total_tokens).toBe(0)
|
||
expect(a.results.map((r) => r.id).sort()).toEqual([7, 8])
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
|
||
for (const cancelBody of [true, false]) test(`native loader: cancelling a cached streamed replay ${cancelBody ? "through its body" : "through its request signal"} preserves the active owner`, async () => {
|
||
const a = await agent()
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const secondChat = continueWith(chat, first.body, ["replay-safe"])
|
||
const second = await a.ask(account, secondChat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const handles = [...nativeSessions.handles.keys()]
|
||
const ac = new AbortController()
|
||
const replay = await a.ask(account, { ...secondChat, stream: true }, { signal: ac.signal })
|
||
expect(replay.status).toBe(200)
|
||
if (cancelBody) await replay.body.cancel()
|
||
else { ac.abort(); await replay.body.cancel() }
|
||
expect(owner.state).toBe("awaiting-results")
|
||
expect(owner.error).toBeUndefined()
|
||
expect(owner.transportClosed).not.toBe(true)
|
||
expect([...nativeSessions.handles.keys()]).toEqual(handles)
|
||
const final = await a.ask(account, continueWith(secondChat, second.body, ["replay-safe"]))
|
||
expect(final.status).toBe(200)
|
||
expect(choice(final).message.content).toBe("replay-safe")
|
||
expect(a.results.map((r) => r.id)).toEqual([7, 8])
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|
||
|
||
test("native loader: cancellation after the transport head but before bind closes the late transport", async () => {
|
||
const a = await agent({ stallInitial: true })
|
||
const ac = new AbortController()
|
||
const connect = http2.connect
|
||
http2.connect = (...args) => {
|
||
const session = connect(...args)
|
||
const request = session.request
|
||
session.request = function (...args) {
|
||
const req = request.apply(this, args)
|
||
req.once("response", () => queueMicrotask(() => ac.abort()))
|
||
return req
|
||
}
|
||
return session
|
||
}
|
||
try {
|
||
const reply = await a.ask(auth(), initial(), { signal: ac.signal })
|
||
expect(reply.status).toBe(410)
|
||
await until(() => a.closedStreams === 1 && a.sessions.size === 0)
|
||
expect(a.runs).toHaveLength(0)
|
||
expect(nativeSessions.entries.size).toBe(0)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
} finally { http2.connect = connect }
|
||
})
|
||
|
||
test("native HTTP/2: allowed display-reasoning omission and argument key/whitespace equivalence retain the Run", async () => {
|
||
const a = await agent()
|
||
const account = auth()
|
||
const chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const continuation = continueWith(chat, first.body, ["equivalent"])
|
||
const assistant = continuation.messages.at(-2)
|
||
delete assistant.reasoning_content
|
||
assistant.content = null
|
||
assistant.tool_calls[0].function.arguments = ' { "value" : "seed" } '
|
||
const second = await a.ask(account, continuation)
|
||
expect(second.status).toBe(200)
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|
||
|
||
test("native HTTP/2: a clean streamed handoff detaches the old signal; later abort does not kill its continuation", async () => {
|
||
const a = await agent()
|
||
const account = auth()
|
||
const ac = new AbortController()
|
||
const chat = initial({ stream: true })
|
||
const res = await a.ask(account, chat, { signal: ac.signal })
|
||
const sse = await res.text()
|
||
expect(sse).toContain("[DONE]")
|
||
expect(getEventListeners(ac.signal, "abort")).toHaveLength(0)
|
||
expect([...nativeSessions.entries][0].state).toBe("awaiting-results")
|
||
const chunks = sse.split("\n\n").filter((s) => s.startsWith("data: {")).map((s) => JSON.parse(s.slice(6)))
|
||
const toolCalls = chunks.flatMap((c) => c.choices?.[0]?.delta?.tool_calls ?? []).map(({ index, ...c }) => c)
|
||
const reasoning = chunks.map((c) => c.choices?.[0]?.delta?.reasoning_content ?? "").join("")
|
||
ac.abort()
|
||
const next = { ...chat, stream: false, messages: [...chat.messages, { role: "assistant", content: "", reasoning_content: reasoning, tool_calls: toolCalls }, { role: "tool", tool_call_id: toolCalls[0].id, content: "after-old-abort" }] }
|
||
expect((await a.ask(account, next)).status).toBe(200)
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.closedStreams).toBe(0)
|
||
})
|
||
|
||
for (const blocked of [false, true]) test(`native HTTP/2: streaming cancellation ${blocked ? "interrupts a blocked read" : "before reading"} settles transport/pump`, async () => {
|
||
const a = await agent({ thinkingOnly: true })
|
||
const res = await a.ask(auth(), initial({ stream: true }))
|
||
const owner = [...nativeSessions.entries][0]
|
||
const before = Date.now()
|
||
if (blocked) {
|
||
const reader = res.body.getReader()
|
||
await reader.read()
|
||
await reader.read()
|
||
const pending = reader.read()
|
||
await reader.cancel()
|
||
await pending
|
||
} else await res.body.cancel()
|
||
await owner.pumpTask
|
||
await until(() => a.closedStreams === 1)
|
||
expect(Date.now() - before).toBeLessThan(1000)
|
||
expect(owner.state).toBe("cancelled")
|
||
expect(owner.transportClosed).toBe(true)
|
||
})
|
||
|
||
test("native loader: abort during metadata lookup leaves no opening owner or transport", async () => {
|
||
const a = await agent()
|
||
const ac = new AbortController()
|
||
const fetch = globalThis.fetch
|
||
globalThis.fetch = async (url, init) => {
|
||
const response = await fetch(url, init)
|
||
if (String(url).endsWith("/aiserver.v1.ServerConfigService/GetServerConfig")) ac.abort()
|
||
return response
|
||
}
|
||
const response = await a.ask(auth(), initial(), { signal: ac.signal })
|
||
expect(response.status).toBe(410)
|
||
expect(a.runs).toHaveLength(0)
|
||
expect(a.sessions.size).toBe(0)
|
||
expect(nativeSessions.entries.size).toBe(0)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
})
|
||
|
||
test("native HTTP/2: already-aborted request never opens Run; abort while awaiting initial output closes it", async () => {
|
||
const a = await agent({ stallInitial: true })
|
||
const account = auth()
|
||
const ac = new AbortController()
|
||
ac.abort()
|
||
expect((await a.ask(account, initial(), { signal: ac.signal })).status).toBe(410)
|
||
expect(a.runs).toHaveLength(0)
|
||
const live = new AbortController()
|
||
const pending = a.ask(account, initial(), { signal: live.signal })
|
||
await until(() => a.runs.length === 1)
|
||
live.abort()
|
||
expect((await pending).status).toBe(410)
|
||
await until(() => a.closedStreams === 1)
|
||
})
|
||
|
||
test("native HTTP/2: housekeeping cannot extend the result deadline; late supplied results rebuild history", async () => {
|
||
NATIVE_LIMITS.resultWait = 60
|
||
const a = await agent()
|
||
const account = auth()
|
||
const chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
await until(() => owner.state === "expired")
|
||
// Housekeeping still arrives while parked; it cannot renew retention. The
|
||
// completed client result is safe imported data for the new stateless Run.
|
||
rebuilt(await a.ask(account, continueWith(chat, first.body, ["late"])), "late")
|
||
expect(a.results).toHaveLength(0)
|
||
expect(a.runs).toHaveLength(2)
|
||
})
|
||
|
||
|
||
for (const options of [{ truncated: true }]) test(`native HTTP/2: ${options.truncated ? "truncated declared batch" : "missing batch count"} cannot yield successful partial calls`, async () => {
|
||
NATIVE_LIMITS.batchWait = 55
|
||
const a = await agent(options)
|
||
const r = await a.ask(auth(), initial())
|
||
expect(r.status).toBe(502)
|
||
expect(r.body.choices).toBeUndefined()
|
||
expect(a.results).toHaveLength(0)
|
||
})
|
||
|
||
|
||
const protobufDouble = (value) => {
|
||
const data = Buffer.alloc(8)
|
||
data.writeDoubleLE(value)
|
||
return message(Buffer.from([0x11]), data)
|
||
}
|
||
const MALFORMED_VALUES = [
|
||
["truncated string", Buffer.from("1a0561", "hex")],
|
||
["dangling tail", Buffer.from("1a017880", "hex")],
|
||
["nested struct tail", bin(5, bin(1, message(text(1, "nested"), bin(2, Buffer.from("1a017880", "hex")))))],
|
||
["nested list truncation", bin(6, bin(1, Buffer.from("1a0561", "hex")))],
|
||
["wrong string wire", uint(3, 1)],
|
||
["wrong number wire", uint(2, 1)],
|
||
["wrong boolean wire", bin(4, Buffer.alloc(0))],
|
||
["wrong struct wire", uint(5, 0)],
|
||
["wrong list wire", uint(6, 0)],
|
||
["wrong struct entry wire", bin(5, uint(1, 0))],
|
||
["wrong struct key wire", bin(5, bin(1, message(uint(1, 0), bin(2, text(3, "x")))))],
|
||
["wrong struct value wire", bin(5, bin(1, message(text(1, "k"), uint(2, 0))))],
|
||
["wrong list item wire", bin(6, uint(1, 0))],
|
||
["nonfinite double", protobufDouble(NaN)],
|
||
["invalid UTF-8", bin(3, Buffer.from([0xff]))],
|
||
["invalid NullValue", uint(1, 1)],
|
||
["excessive nesting", Array.from({ length: 66 }).reduce((v) => bin(6, bin(1, v)), text(3, "nested"))],
|
||
]
|
||
const REPEATED_MCP_EXEC = Buffer.from("08075a0c2a0a61756469745f6563686f5a030a0561", "hex")
|
||
const mcpEntry = (key, value) => bin(2, message(text(1, key), bin(2, value)))
|
||
for (const [label, execMessage] of [
|
||
["truncated second envelope", REPEATED_MCP_EXEC],
|
||
["wrong second wire", message(uint(1, 7), bin(11, text(5, "audit_echo")), uint(11, 0))],
|
||
["recursive second value", message(uint(1, 7), bin(11, text(5, "audit_echo")), bin(11, message(text(5, "audit_echo"), mcpEntry("value", bin(6, bin(1, Buffer.from("1a0561", "hex")))))))],
|
||
["excessive second depth", message(uint(1, 7), bin(11, text(5, "audit_echo")), bin(11, message(text(5, "audit_echo"), mcpEntry("value", MALFORMED_VALUES.find(([label]) => label === "excessive nesting")[1]))))],
|
||
["differing valid arguments", message(uint(1, 7), bin(11, text(5, "audit_echo")), bin(11, message(text(5, "audit_echo"), mcpEntry("value", text(3, "changed")))))],
|
||
["differing valid identity", message(uint(1, 7), bin(11, text(5, "audit_echo")), bin(11, message(text(5, "audit_echo"), text(3, "different-native-call"))))],
|
||
]) test(`native loader: repeated MCP ${label} fails before exposing tools or retaining handles`, async () => {
|
||
const a = await agent({ execMessage, stateDelay: 10_000 })
|
||
const response = await a.ask(auth(), initial())
|
||
expect(response.status).toBe(502)
|
||
expect(response.body.choices).toBeUndefined()
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(nativeSessions.entries.size).toBe(0)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
await until(() => a.closedStreams === 1)
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
for (const reordered of [false, true]) test(`native loader: repeated MCP ${reordered ? "canonically equal" : "identical"} envelopes expose one call and submit one result`, async () => {
|
||
const entries = [mcpEntry("first", text(3, "one")), mcpEntry("nested", bin(5, bin(1, message(text(1, "__proto__"), bin(2, text(3, "safe"))))))]
|
||
const identity = message(text(5, "audit_echo"), text(3, "same-native-call"))
|
||
const execMessage = message(uint(1, 7), text(15, "exec-7"), bin(11, message(identity, ...entries)), bin(11, message(identity, ...(reordered ? [...entries].reverse() : entries))))
|
||
const a = await agent({ execMessage, finalAfterFirst: true })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
expect(choice(first).message.tool_calls).toHaveLength(1)
|
||
expect(JSON.parse(choice(first).message.tool_calls[0].function.arguments)).toEqual({ first: "one", nested: JSON.parse('{"__proto__":"safe"}') })
|
||
const final = await a.ask(account, continueWith(chat, first.body, ["same call once"]))
|
||
expect(final.status).toBe(200)
|
||
expect(choice(final).message.content).toBe("same call once")
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.closes).toEqual([7])
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
for (const [label, argumentValue] of MALFORMED_VALUES) test(`native loader: native protobuf arguments reject ${label} without publishing calls or submitting results`, async () => {
|
||
const a = await agent({ argumentValue })
|
||
const response = await a.ask(auth(), initial())
|
||
expect(response.status).toBe(502)
|
||
expect(response.body.choices).toBeUndefined()
|
||
expect(a.results).toHaveLength(0)
|
||
expect(nativeSessions.entries.size).toBe(0)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
await until(() => a.closedStreams === 1)
|
||
})
|
||
|
||
for (const limit of ["argumentBytes", "argumentFields"]) test(`native loader: native protobuf arguments enforce the ${limit} bound`, async () => {
|
||
const previous = NATIVE_LIMITS[limit]
|
||
NATIVE_LIMITS[limit] = 32
|
||
try {
|
||
const argumentValue = limit === "argumentBytes" ? text(3, "x".repeat(64)) : bin(6, message(...Array.from({ length: 40 }, () => bin(1, Buffer.alloc(0)))))
|
||
const a = await agent({ argumentValue })
|
||
const response = await a.ask(auth(), initial())
|
||
expect(response.status).toBe(502)
|
||
expect(response.body.choices).toBeUndefined()
|
||
expect(a.results).toHaveLength(0)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
} finally {
|
||
if (previous === undefined) delete NATIVE_LIMITS[limit]
|
||
else NATIVE_LIMITS[limit] = previous
|
||
}
|
||
})
|
||
|
||
for (const [label, argumentValue, expected] of [
|
||
["unset Value", Buffer.alloc(0), null],
|
||
["explicit null", uint(1, 0), null],
|
||
["empty Struct", bin(5, Buffer.alloc(0)), {}],
|
||
["default map entry", bin(5, bin(1, Buffer.alloc(0))), { "": null }],
|
||
["default list entries", bin(6, message(bin(1, Buffer.alloc(0)), bin(1, uint(4, 0)))), [null, false]],
|
||
["proto key", bin(5, bin(1, message(text(1, "__proto__"), bin(2, text(3, "safe"))))), JSON.parse('{"__proto__":"safe"}')],
|
||
["last oneof value", message(text(3, "old"), uint(4, 1)), true],
|
||
["finite double", protobufDouble(-12.5), -12.5],
|
||
["literal UTF-8 BOM", text(3, "\uFEFFexact"), "\uFEFFexact"],
|
||
["nested Struct/List", bin(5, bin(1, message(text(1, "items"), bin(2, bin(6, message(bin(1, protobufDouble(0)), bin(1, uint(4, 1)), bin(1, text(3, "精确")))))))), { items: [0, true, "精确"] }],
|
||
]) test(`native loader: native protobuf arguments preserve protobuf ${label}`, async () => {
|
||
const a = await agent({ argumentValue })
|
||
const response = await a.ask(auth(), initial())
|
||
expect(response.status).toBe(200)
|
||
expect(JSON.parse(choice(response).message.tool_calls[0].function.arguments)).toEqual({ value: expected })
|
||
})
|
||
|
||
for (const options of [{ name: "unregistered" }, { provider: "foreign" }, { approval: true }]) test("native HTTP/2: unavailable MCP forms are refused without execution and the model continues", async () => {
|
||
const a = await agent(options)
|
||
const r = await a.ask(auth(), initial())
|
||
expect(r.status).toBe(200)
|
||
expect(choice(r).message.content).toBe("continued after unavailable")
|
||
expect(a.results).toEqual([])
|
||
expect(a.unavailable).toHaveLength(1)
|
||
})
|
||
|
||
|
||
for (const stream of [false, true]) test(`native retention: ${stream ? "streamed" : "JSON"} uncertain result writes are surfaced once; retry rebuilds without native resubmission`, async () => {
|
||
const a = await agent({ stateDelay: 10_000 }), account = auth(), chat = initial()
|
||
const connect = http2.connect
|
||
let req
|
||
http2.connect = (...args) => {
|
||
const session = connect(...args), request = session.request
|
||
session.request = function (...args) { req = request.apply(this, args); return req }
|
||
return session
|
||
}
|
||
try {
|
||
const first = await a.ask(account, chat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const write = req.write
|
||
req.write = function (packet, callback) {
|
||
const result = readWire(packet.subarray(5)).some((f) => f.no === 2)
|
||
return write.call(this, packet, (e) => {
|
||
if (result) setImmediate(() => callback(Object.assign(new Error("synthetic result write failed; outcome uncertain"), { status: 503 })))
|
||
else callback(e)
|
||
})
|
||
}
|
||
const next = { ...continueWith(chat, first.body, ["provided-once"]), stream }
|
||
const failed = await a.ask(account, next)
|
||
expect(failed.status).toBe(503)
|
||
const body = stream ? await failed.json() : failed.body
|
||
expect(body.error.message).toContain("uncertain")
|
||
await until(() => a.results.length === 1 && a.closedStreams === 1)
|
||
expect(owner.state).toBe("failed")
|
||
expect(a.closes).toEqual([])
|
||
// The old hard-error test rejected this retry with 410. Rebuilding imports
|
||
// already supplied results; it does not call the external tool or re-send
|
||
// a native McpResult whose first write might have reached the server.
|
||
rebuilt(await a.ask(account, { ...next, stream: false }), "provided-once")
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.errors).toEqual([])
|
||
} finally { http2.connect = connect }
|
||
})
|
||
|
||
test("native retention: synchronous result-close overflow does not resubmit its admitted result", async () => {
|
||
const a = await agent({ stateDelay: 10_000 }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
await until(() => owner.transport.outbound.messages === 0)
|
||
NATIVE_LIMITS.outboundMessages = 1
|
||
const next = continueWith(chat, first.body, ["admitted-once"])
|
||
const failed = await a.ask(account, next)
|
||
expect(failed.status).toBe(503)
|
||
expect(failed.body.error.message).toContain("uncertain")
|
||
expect(failed.body.error.message).toContain("cannot be replayed")
|
||
await until(() => a.closedStreams === 1)
|
||
NATIVE_LIMITS.outboundMessages = limits.outboundMessages
|
||
rebuilt(await a.ask(account, next), "admitted-once")
|
||
expect(a.results.length).toBeLessThanOrEqual(1)
|
||
expect(a.closes).toEqual([])
|
||
expect(a.runs).toHaveLength(2)
|
||
})
|
||
|
||
test("native retention: concurrent continuation falls back while the original retains its result reservation", async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["concurrent-result"])
|
||
const responses = await Promise.all([a.ask(account, next), a.ask(account, next)])
|
||
expect(responses.map((r) => r.status)).toEqual([200, 200])
|
||
const continued = responses.find((r) => choice(r).finish_reason === "tool_calls")
|
||
const fallback = responses.find((r) => choice(r).finish_reason === "stop")
|
||
rebuilt(fallback, "concurrent-result")
|
||
expect(continued).toBeDefined()
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.runs).toHaveLength(2)
|
||
const replay = await a.ask(account, next)
|
||
expect(choice(replay).message).toEqual(choice(continued).message)
|
||
expect(replay.body.usage.total_tokens).toBe(0)
|
||
expect(a.results).toHaveLength(1)
|
||
})
|
||
|
||
for (const options of [{ noCount: true }, { cleanNoCount: true }]) test(`native retention: missing batch count ${options.noCount ? "deadline" : "clean EOF"} before visible JSON output retries stateless once`, async () => {
|
||
NATIVE_LIMITS.batchWait = 45
|
||
const a = await agent(options), account = auth(), chat = initial()
|
||
const result = await a.ask(account, chat)
|
||
rebuilt(result, "new-input")
|
||
expect(result.diagnostic).toContain("reason=missing-batch-count")
|
||
expect(choice(result).finish_reason).toBe("stop")
|
||
expect(choice(result).message.tool_calls).toBeUndefined()
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.histories[1]).toEqual(a.histories[0])
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
})
|
||
|
||
for (const stream of [false, true]) test(`native retention: conflicting counts before visible ${stream ? "stream" : "JSON"} output rebuild exactly once`, async () => {
|
||
const a = await agent({ conflictingCount: true, ...(stream ? { initialReasoning: "" } : {}) })
|
||
const chat = initial({ stream })
|
||
const result = await a.ask(auth(), chat)
|
||
expect(result.status).toBe(200)
|
||
if (stream) {
|
||
expect(result.headers.get("X-Magpie-Cursor-Continuity")).toContain("reason=inconsistent-batch-count")
|
||
const sse = await result.text()
|
||
expect(sse).toContain("rebuilt:new-input")
|
||
expect(sse).toContain("[DONE]")
|
||
expect(sse).not.toContain("inconsistent native tool count")
|
||
} else {
|
||
rebuilt(result, "new-input")
|
||
expect(result.diagnostic).toContain("reason=inconsistent-batch-count")
|
||
expect(choice(result).message.reasoning_content).toBeUndefined()
|
||
}
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.histories[1]).toEqual(a.histories[0])
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
test("native retention: conflicting counts after visible thinking never replay output", async () => {
|
||
const a = await agent({ conflictingCount: true })
|
||
const result = await a.ask(auth(), initial({ stream: true }))
|
||
const sse = await result.text()
|
||
expect(sse).toContain("native planning one")
|
||
expect(sse).toContain("inconsistent native tool count")
|
||
expect(sse).not.toContain("rebuilt:")
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toEqual([])
|
||
})
|
||
|
||
test("native retention: conflicting counts after submitted result never retry automatically", async () => {
|
||
const a = await agent({ secondConflictingCount: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const next = continueWith(chat, first.body, ["already submitted exactly once"])
|
||
const failed = await a.ask(account, next)
|
||
expect(failed.status).toBe(502)
|
||
expect(failed.body.error.message).toContain("inconsistent native tool count")
|
||
// A result was actually written to this owner before its next step listed
|
||
// conflicting counts. Its error must forbid transparent replay even if
|
||
// the caller has not received any output from the resumed step.
|
||
expect(owner.resultSubmitted).toBe(true)
|
||
expect(owner.error).toMatchObject({ status: 502, retentionIneligible: false, continuityReason: "inconsistent-batch-count" })
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.closes).toEqual([7])
|
||
rebuilt(await a.ask(account, next), "already submitted exactly once")
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toHaveLength(1)
|
||
})
|
||
|
||
test("native retention: progressive thinking before missing count is not transparently replayed", async () => {
|
||
NATIVE_LIMITS.batchWait = 45
|
||
const a = await agent({ noCount: true })
|
||
const response = await a.ask(auth(), initial({ stream: true }))
|
||
const reader = response.body.getReader()
|
||
let sse = ""
|
||
while (!sse.includes("native planning one")) sse += new TextDecoder().decode((await reader.read()).value)
|
||
expect(sse).not.toContain("did not declare")
|
||
while (true) {
|
||
const part = await reader.read()
|
||
if (part.done) break
|
||
sse += new TextDecoder().decode(part.value)
|
||
}
|
||
expect(sse).toContain("did not declare")
|
||
expect(sse).not.toContain("rebuilt:")
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toEqual([])
|
||
})
|
||
|
||
test("native retention: prompt_cache_key keeps ownership when the host session changes", async () => {
|
||
const a = await agent(), account = auth(), chat = initial({ prompt_cache_key: "synthetic-thread" })
|
||
const first = await a.ask(account, chat, { session: "host-one" })
|
||
const second = await a.ask(account, continueWith(chat, first.body, ["same-cache-key"]), { session: "host-two" })
|
||
expect(second.status).toBe(200)
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toHaveLength(1)
|
||
})
|
||
|
||
test("native retention: a missing count before the streaming head can rebuild without duplicate output", async () => {
|
||
const a = await agent({ cleanNoCount: true, initialReasoning: "" })
|
||
const response = await a.ask(auth(), initial({ stream: true }))
|
||
expect(response.status).toBe(200)
|
||
expect(response.headers.get("X-Magpie-Cursor-Continuity")).toContain("reason=missing-batch-count")
|
||
const sse = await response.text()
|
||
expect(sse).toContain("rebuilt:new-input")
|
||
expect(sse).toContain("[DONE]")
|
||
expect(sse).not.toContain("native planning one")
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toEqual([])
|
||
})
|
||
|
||
test("native retention: missing count after a native result never transparently reruns that turn", async () => {
|
||
NATIVE_LIMITS.batchWait = 45
|
||
const a = await agent({ secondNoCount: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["already submitted"])
|
||
const failed = await a.ask(account, next)
|
||
expect(failed.status).toBe(502)
|
||
expect(failed.body.error.message).toContain("declared native tool batch")
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toHaveLength(1)
|
||
rebuilt(await a.ask(account, next), "already submitted")
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toHaveLength(1)
|
||
})
|
||
|
||
test("native retention: exact single-result image suffix preserves images on one Run", async () => {
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["synthetic screenshot"])
|
||
next.messages.at(-1).is_error = true
|
||
const png = Buffer.from("89504e470d0a1a0a", "hex")
|
||
next.messages.push({ role: "user", content: [{ type: "text", text: "Attached image(s) from tool result:" },
|
||
{ type: "image_url", image_url: { url: "data:image/png;base64," + png.toString("base64") } }] })
|
||
const before = JSON.stringify(next)
|
||
const second = await a.ask(account, next)
|
||
expect(second.status).toBe(200)
|
||
expect(JSON.stringify(next)).toBe(before)
|
||
expect(a.results[0]).toMatchObject({ isError: true, content: [{ type: "text", text: "synthetic screenshot" }, { type: "image", data: png, mimeType: "image/png" }] })
|
||
const final = await a.ask(account, continueWith(next, second.body, ["image checked"]))
|
||
expect(final.status).toBe(200)
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.results).toHaveLength(2)
|
||
})
|
||
|
||
test("native retention: actual capacity eviction of terminal handles permits later history rebuild", async () => {
|
||
NATIVE_LIMITS.perAccount = 1
|
||
const a = await agent({ finalAfterFirst: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["terminal evicted result"])
|
||
const owner = [...nativeSessions.entries][0]
|
||
const final = await a.ask(account, next)
|
||
expect(final.status).toBe(200)
|
||
expect(owner.state).toBe("complete")
|
||
expect(nativeSessions.handles.size).toBe(1)
|
||
const fresh = await a.ask(account, initial({ messages: [{ role: "user", content: "replace terminal owner at capacity" }] }))
|
||
expect(fresh.status).toBe(200)
|
||
expect(nativeSessions.entries.has(owner)).toBe(false)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
rebuilt(await a.ask(account, next), "terminal evicted result")
|
||
expect(a.runs).toHaveLength(3)
|
||
expect(a.results).toHaveLength(1)
|
||
})
|
||
|
||
for (const parallel of [false, true]) test(`native retransmission: sealed ${parallel ? "final parallel" : "single"} exec continues one Run and submits once`, async () => {
|
||
const a = await agent({ parallel, duplicate: !parallel, duplicateFinal: parallel, finalAfterFirst: !parallel })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
expect(choice(first).message.tool_calls).toHaveLength(parallel ? 2 : 1)
|
||
const next = continueWith(chat, first.body, ["sealed-one", "sealed-two"])
|
||
const final = await a.ask(account, next)
|
||
expect(final.status).toBe(200)
|
||
expect(choice(final).message.content).toBe(parallel ? "sealed-one|sealed-two" : "sealed-one")
|
||
const replay = await a.ask(account, next)
|
||
expect(replay.body.usage).toMatchObject({ total_tokens: 0, magpie_native: { accounting: "replayed" } })
|
||
expect(a.results.map((r) => r.id).sort()).toEqual(parallel ? [7, 8] : [7])
|
||
expect(a.closes.sort()).toEqual(parallel ? [7, 8] : [7])
|
||
expect(a.runs).toHaveLength(1)
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
for (const changed of [true, false]) test(`native retransmission: sealed ${changed ? "changed duplicate" : "new exec"} remains a protocol failure`, async () => {
|
||
const a = await agent({ changedDuplicate: changed, extraExec: !changed, initialReasoning: "" })
|
||
const response = await a.ask(auth(), initial())
|
||
expect(response.status).toBe(502)
|
||
expect(response.body.error.message).toContain(changed ? "identity changed" : "batch was sealed")
|
||
expect(response.body.choices).toBeUndefined()
|
||
expect(nativeSessions.entries.size).toBe(0)
|
||
expect(nativeSessions.handles.size).toBe(0)
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|
||
|
||
const EARLIER_USAGE = { inputTokens: 11, outputTokens: 3, cacheReadTokens: 100, cacheWriteTokens: 7 }
|
||
const LATER_USAGE = { inputTokens: 22, outputTokens: 5, cacheReadTokens: 200, cacheWriteTokens: 9 }
|
||
const EARLIER_DEFERRED = { prompt_tokens: 118, completion_tokens: 3, cached_tokens: 100, cache_write_tokens: 7 }
|
||
const NATIVE_FINAL_USAGE = message(uint(1, 215), uint(2, 5), uint(3, 200), uint(4, 9))
|
||
const eventFor = (a, run, tokenUsage) => ({ conversationId: string(a.runs[run], 5), timestamp: String(a.started[run] + 2), tokenUsage })
|
||
// The existing dashboard contract identifies Runs by conversation and event
|
||
// time. Leave more than its one-second tolerance between distinct Runs here;
|
||
// the exact-boundary fixture below separately covers ambiguous timestamps.
|
||
const afterEvent = async (event) => {
|
||
const ms = Number(event.timestamp) + 1100 - Date.now()
|
||
if (ms > 0) await new Promise((r) => setTimeout(r, ms))
|
||
}
|
||
|
||
for (const timing of ["ready", "after close"]) test(`native accounting: full capacity stateless E1 then retained E2 collects prior debt ${timing}`, async () => {
|
||
NATIVE_LIMITS.perProcess = 1
|
||
const a = await agent({ toolRuns: [1, 2], rebuildUsage: NATIVE_FINAL_USAGE, dashboard: async (fake) => {
|
||
if (timing === "after close" && fake.runs.length >= 3) await until(() => fake.closedStreams === 3)
|
||
if (fake.runs.length >= 3 && fake.events.length === 1) fake.events.push(eventFor(fake, 2, LATER_USAGE))
|
||
return Response.json({ usageEventsDisplay: fake.events })
|
||
} })
|
||
const account = auth(), chat = initial()
|
||
const holderChat = initial({ messages: [{ role: "user", content: "hold the only retained slot" }] })
|
||
expect((await a.ask(account, holderChat, { session: "capacity-holder" })).status).toBe(200)
|
||
const holder = [...nativeSessions.entries][0]
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
expect(first.diagnostic).toMatch(/^stateless;/)
|
||
expect(first.body.usage.total_tokens).toBe(0)
|
||
expect(holder.state).toBe("awaiting-results")
|
||
const earlier = eventFor(a, 1, EARLIER_USAGE)
|
||
await afterEvent(earlier)
|
||
a.events.push(earlier)
|
||
await holder.cancel("free the fixture capacity")
|
||
const next = continueWith(chat, first.body, ["already-executed-stateless-tool"])
|
||
const final = await a.ask(account, next)
|
||
expect(final.status).toBe(200)
|
||
expect(final.diagnostic).toMatch(/^same-run;/)
|
||
expect(choice(final).message.content).toBe("rebuilt:already-executed-stateless-tool")
|
||
expect(final.body.usage).toMatchObject({ prompt_tokens: 215, completion_tokens: 5, total_tokens: 220,
|
||
prompt_tokens_details: { cached_tokens: 200, cache_write_tokens: 9 }, magpie_deferred: EARLIER_DEFERRED })
|
||
const owner = [...nativeSessions.entries][0]
|
||
expect(owner.state).toBe("complete")
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
nativeSessions.forget(owner)
|
||
await owner.cancel("already completed")
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const later = await a.ask(account, next)
|
||
expect(later.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
expect(a.runs).toHaveLength(4)
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
for (const loss of ["expiry", "failure", "cancellation", "forget"]) test(`native accounting: pending handoff ${loss} enrolls E1 once for stateless history E2`, async () => {
|
||
if (loss === "expiry") NATIVE_LIMITS.resultWait = 55
|
||
const a = await agent({ rebuildUsage: NATIVE_FINAL_USAGE })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.body.usage).toMatchObject({ total_tokens: 0, magpie_native: { accounting: "pending" } })
|
||
const owner = [...nativeSessions.entries][0]
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
if (loss === "expiry") await until(() => owner.state === "expired")
|
||
if (loss === "failure") owner.fail({ status: 502, message: "synthetic abandoned pending Run" })
|
||
if (loss === "cancellation") await owner.cancel("synthetic cancellation after pending handoff")
|
||
if (loss === "forget") nativeSessions.forget(owner)
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
owner.fail({ status: 502, message: "repeat cleanup" })
|
||
nativeSessions.forget(owner)
|
||
await owner.cancel("repeat cancellation")
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
await afterEvent(earlier)
|
||
a.events.push(earlier) // dashboard delivery occurs after ownership was lost
|
||
const next = continueWith(chat, first.body, ["completed-external-tool"])
|
||
const final = await a.ask(account, next)
|
||
rebuilt(final, "completed-external-tool")
|
||
expect(final.body.usage).toMatchObject({ prompt_tokens: 215, completion_tokens: 5, magpie_deferred: EARLIER_DEFERRED })
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
const retry = await a.ask(account, next)
|
||
expect(retry.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
expect(a.runs).toHaveLength(3)
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
test("native accounting: missing E1 stays bounded past authoritative E2 and a later stateless tool E3", async () => {
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const a = await agent({ toolRuns: [1, 3], rebuildUsage: NATIVE_FINAL_USAGE, onRun: (fake) => {
|
||
if (fake.runs.length === 2) fake.events.push(eventFor(fake, 1, LATER_USAGE))
|
||
if (fake.runs.length === 3) fake.events.push(eventFor(fake, 2, LATER_USAGE))
|
||
} })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.body.usage.total_tokens).toBe(0)
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
await afterEvent(earlier)
|
||
NATIVE_LIMITS.perProcess = 1
|
||
const nativeChat = continueWith(chat, first.body, ["E1-result"])
|
||
const nativeFinal = await a.ask(account, nativeChat)
|
||
expect(nativeFinal.body.usage).toMatchObject({ prompt_tokens: 215, completion_tokens: 5, total_tokens: 220 })
|
||
expect(nativeFinal.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
await afterEvent(a.events[0])
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const thirdChat = { ...nativeChat, messages: [...nativeChat.messages, choice(nativeFinal).message, { role: "user", content: "E3 request while E1 is still missing" }] }
|
||
const third = await a.ask(account, thirdChat)
|
||
expect(third.status).toBe(200)
|
||
expect(choice(third).finish_reason).toBe("tool_calls")
|
||
expect(third.body.usage).toMatchObject({ prompt_tokens: 231, completion_tokens: 5, total_tokens: 236 })
|
||
expect(third.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
a.events.push(earlier)
|
||
const fourthChat = continueWith(thirdChat, third.body, ["E3-result"])
|
||
const fourth = await a.ask(account, fourthChat)
|
||
expect(fourth.body.usage).toMatchObject({ prompt_tokens: 215, magpie_deferred: EARLIER_DEFERRED })
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
const retry = await a.ask(account, fourthChat)
|
||
expect(retry.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(a.runs).toHaveLength(5)
|
||
expect(a.results).toEqual([])
|
||
expect(a.closes).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
test("native accounting: a parent lost after an owner past its reservation started never collects that owner's event", async () => {
|
||
const a = await agent({ rebuildUsage: NATIVE_FINAL_USAGE })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.body.usage).toMatchObject({ total_tokens: 0, magpie_native: { accounting: "pending" } })
|
||
const parent = [...nativeSessions.entries][0]
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
await afterEvent(earlier)
|
||
const branch = withNewInput(chat, first.body, ["already-provided"], "new input past the busy parent")
|
||
const second = await a.ask(account, branch)
|
||
expect(second.diagnostic).toMatch(/^same-run;/)
|
||
expect(second.body.usage).toMatchObject({ prompt_tokens: 215, completion_tokens: 5, total_tokens: 220 })
|
||
expect(second.body.usage.magpie_deferred).toBeUndefined()
|
||
// the branch's own event shows; the parent's never does
|
||
const later = eventFor(a, 1, LATER_USAGE)
|
||
await afterEvent(later)
|
||
a.events.push(later)
|
||
await parent.cancel("the parent is lost after its branch began")
|
||
// owed once, at the branch's start, and bounded before it
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
expect(Math.max(..._internal.owed.get(key).groups.map((g) => g.before))).toBeLessThanOrEqual(a.started[1])
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const thirdChat = { ...branch, messages: [...branch.messages, choice(second).message, { role: "user", content: "E3 while the parent's event is missing" }] }
|
||
const third = await a.ask(account, thirdChat)
|
||
expect(third.status).toBe(200)
|
||
expect(third.diagnostic).toMatch(/^stateless;/)
|
||
expect(third.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
a.events.push(earlier)
|
||
const fourth = await a.ask(account, thirdChat)
|
||
expect(fourth.body.usage.magpie_deferred).toEqual(EARLIER_DEFERRED)
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
expect(a.results).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
// A session that ends while a parent still waits out resultWait: nothing
|
||
// after the unreserved Run would collect what the parent owes at abandon.
|
||
test("native accounting: a parent waiting behind an owner past its reservation is collected by that owner, once", async () => {
|
||
const a = await agent({ rebuildUsage: NATIVE_FINAL_USAGE })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const parent = [...nativeSessions.entries][0]
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
await afterEvent(earlier)
|
||
a.events.push(earlier) // stamped before the branch begins, as all 61 were
|
||
const branch = withNewInput(chat, first.body, ["already-provided"], "the user hasn't heard from you")
|
||
const second = await a.ask(account, branch)
|
||
expect(second.diagnostic).toMatch(/^same-run;/)
|
||
expect(parent.state).toBe("awaiting-results")
|
||
expect(second.body.usage).toMatchObject({ prompt_tokens: 215, completion_tokens: 5, magpie_deferred: EARLIER_DEFERRED })
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
// the session ends here: the parent runs out its result wait, owing nothing more
|
||
parent.fail({ status: 410, message: "native tool-result deadline expired" }, "expired")
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const later = await a.ask(account, { ...branch, messages: [...branch.messages, choice(second).message, { role: "user", content: "after the parent expired" }] })
|
||
expect(later.diagnostic).toMatch(/^stateless;/)
|
||
expect(later.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(a.results).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
// Cursor adds each step to a Run's one event as the step ends, seconds
|
||
// later at times. A parent holding an earlier debt it collected hands it
|
||
// over with its step: the later Run asks at once for that debt, and must
|
||
// not take the parent's event before its last step is in it.
|
||
test("native accounting: a handed-over parent's event is collected only once its last step is in it", async () => {
|
||
const partial = { inputTokens: 5, outputTokens: 2, cacheReadTokens: 40, cacheWriteTokens: 0 }
|
||
const whole = { inputTokens: 6, outputTokens: 4, cacheReadTokens: 90, cacheWriteTokens: 1 }
|
||
let parentEvent = null
|
||
const a = await agent({ toolRuns: [1, 2], rebuildUsage: NATIVE_FINAL_USAGE, dashboard: async (fake) => {
|
||
const events = [...fake.events]
|
||
// the parent's last step shows 600 ms after the branch began
|
||
if (parentEvent) events.push({ ...parentEvent, tokenUsage: fake.runs.length >= 3 && Date.now() - fake.started[2] >= 600 ? whole : partial })
|
||
return Response.json({ usageEventsDisplay: events })
|
||
} })
|
||
const account = auth(), chat = initial()
|
||
// R0 hands off and is lost: it owes E0
|
||
const first = await a.ask(account, chat)
|
||
await [...nativeSessions.entries][0].cancel("R0 is lost")
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
await afterEvent(earlier)
|
||
a.events.push(earlier)
|
||
// the next turn's owner collects E0 in the background, then waits on its own call
|
||
const turn = { ...chat, messages: [...continueWith(chat, first.body, ["R0 result"]).messages, { role: "user", content: "next turn" }] }
|
||
const step = await a.ask(account, turn)
|
||
expect(step.diagnostic).toMatch(/^same-run;/)
|
||
expect(choice(step).finish_reason).toBe("tool_calls")
|
||
const parent = [...nativeSessions.entries].find((e) => e.state === "awaiting-results")
|
||
for (const deadline = Date.now() + 3000; _internal.owed.has(key) && Date.now() < deadline;) await new Promise((r) => setTimeout(r, 10))
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
parentEvent = eventFor(a, 1, partial)
|
||
await afterEvent(parentEvent)
|
||
const branch = withNewInput(turn, step.body, ["already-provided"], "the user hasn't heard from you")
|
||
const second = await a.ask(account, branch)
|
||
expect(second.diagnostic).toMatch(/^same-run;/)
|
||
expect(parent.state).toBe("awaiting-results")
|
||
// E0 and the parent's whole event: 118 + 97 in, 3 + 4 out
|
||
expect(second.body.usage).toMatchObject({ prompt_tokens: 215, completion_tokens: 5, magpie_deferred: { prompt_tokens: 215, completion_tokens: 7, cached_tokens: 190, cache_write_tokens: 8 } })
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
parent.fail({ status: 410, message: "native tool-result deadline expired" }, "expired")
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
expect(a.results).toEqual([])
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
// Owed at the branch's start, the parent can still be resumed by its own
|
||
// result: its final, the whole turn's, must not count that step again.
|
||
const PARENT_STEP_USAGE = { inputTokens: 5, outputTokens: 2, cacheReadTokens: 4, cacheWriteTokens: 0 }
|
||
const sumOf = (u) => {
|
||
const d = u.magpie_deferred ?? { prompt_tokens: 0, completion_tokens: 0, cached_tokens: 0, cache_write_tokens: 0 }
|
||
return { prompt: u.prompt_tokens + d.prompt_tokens, output: u.completion_tokens + d.completion_tokens, cached: u.prompt_tokens_details.cached_tokens + d.cached_tokens }
|
||
}
|
||
for (const when of ["after the branch collected it", "before anything collected it"]) test(`native accounting: a parent resumed ${when} counts its step once`, async () => {
|
||
const a = await agent({ finalAfterFirst: true, rebuildUsage: NATIVE_FINAL_USAGE })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const parent = [...nativeSessions.entries][0]
|
||
const step = eventFor(a, 0, PARENT_STEP_USAGE)
|
||
const key = _internal.usageKey(account.access, step.conversationId)
|
||
await afterEvent(step)
|
||
const collected = when === "after the branch collected it"
|
||
if (collected) a.events.push(step)
|
||
const second = await a.ask(account, withNewInput(chat, first.body, ["already-provided"], "the user hasn't heard from you"))
|
||
expect(second.diagnostic).toMatch(/^same-run;/)
|
||
if (collected) {
|
||
expect(second.body.usage.magpie_deferred).toEqual({ prompt_tokens: 9, completion_tokens: 2, cached_tokens: 4, cache_write_tokens: 0 })
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
} else {
|
||
expect(second.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
}
|
||
const resumed = await a.ask(account, continueWith(chat, first.body, ["parent result"]))
|
||
expect(resumed.diagnostic).toMatch(/^same-run;/)
|
||
expect(choice(resumed).message.content).toBe("parent result")
|
||
expect(parent.state).toBe("complete")
|
||
// the turn's TurnEnded: 13 uncached + 10 read in, 7 out
|
||
expect(sumOf(resumed.body.usage)).toEqual(collected ? { prompt: 14, output: 5, cached: 6 } : { prompt: 23, output: 7, cached: 10 })
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
if (!collected) a.events.push(step) // its event shows only now
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const later = await a.ask(account, { ...chat, messages: [...continueWith(chat, first.body, ["parent result"]).messages, choice(resumed).message, { role: "user", content: "next turn" }] })
|
||
expect(later.diagnostic).toMatch(/^stateless;/)
|
||
expect(later.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.errors).toEqual([])
|
||
})
|
||
|
||
test("native accounting: an event exactly at native start remains unknown and bounded debt expires", async () => {
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const clock = Date.now
|
||
let fixed
|
||
const a = await agent({ onRun: (fake) => { if (fake.runs.length === 2) Date.now = clock }, dashboard: (fake) => Response.json({ usageEventsDisplay: fake.events }) })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
fixed = clock()
|
||
a.events.push({ ...earlier, timestamp: String(fixed) })
|
||
NATIVE_LIMITS.perProcess = 1
|
||
try {
|
||
Date.now = () => fixed
|
||
const next = continueWith(chat, first.body, ["ambiguous-E1-time"])
|
||
const final = await a.ask(account, next)
|
||
expect(final.body.usage.prompt_tokens).toBe(13)
|
||
expect(final.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
_internal.STEP_WAIT.owedFor = 1
|
||
await new Promise((r) => setTimeout(r, 5))
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const later = await a.ask(account, next)
|
||
expect(later.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
} finally { Date.now = clock }
|
||
})
|
||
|
||
test("native accounting: authoritative final zero and cached zero replay never enroll dashboard debt", async () => {
|
||
const a = await agent({ finalAfterFirst: true, zeroUsage: true })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const next = continueWith(chat, first.body, ["zero-accounted-result"])
|
||
const final = await a.ask(account, next)
|
||
const owner = [...nativeSessions.entries][0]
|
||
const key = _internal.usageKey(account.access, string(a.runs[0], 5))
|
||
expect(final.body.usage).toEqual({ prompt_tokens: 0, completion_tokens: 0, total_tokens: 0,
|
||
prompt_tokens_details: { cached_tokens: 0, cache_write_tokens: 0 }, completion_tokens_details: { reasoning_tokens: 0 } })
|
||
const replay = await a.ask(account, next)
|
||
expect(replay.body.usage).toMatchObject({ total_tokens: 0, magpie_native: { accounting: "replayed" } })
|
||
nativeSessions.forget(owner)
|
||
await owner.cancel("forget an authoritative zero")
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
a.events.push(eventFor(a, 0, { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 }))
|
||
const rebuiltFinal = await a.ask(account, next)
|
||
expect(rebuiltFinal.body.usage.magpie_deferred).toBeUndefined()
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
expect(a.dashboardRequests).toEqual([])
|
||
expect(a.runs).toHaveLength(2)
|
||
expect(a.results).toHaveLength(1)
|
||
expect(a.closes).toEqual([7])
|
||
})
|
||
|
||
test("native accounting: cancellation interrupts final debt collection and preserves E1 for the next request", async () => {
|
||
NATIVE_LIMITS.perProcess = 0
|
||
let collecting = false, aborted = false
|
||
const a = await agent({ dashboard: async (fake, init) => {
|
||
if (fake.events.length && !aborted) {
|
||
collecting = true
|
||
await new Promise((_, reject) => {
|
||
init.signal.addEventListener("abort", () => { aborted = true; reject(init.signal.reason) }, { once: true })
|
||
})
|
||
}
|
||
return Response.json({ usageEventsDisplay: fake.events })
|
||
} })
|
||
const account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
const earlier = eventFor(a, 0, EARLIER_USAGE)
|
||
const key = _internal.usageKey(account.access, earlier.conversationId)
|
||
await afterEvent(earlier)
|
||
a.events.push(earlier)
|
||
NATIVE_LIMITS.perProcess = 1
|
||
const next = continueWith(chat, first.body, ["result-before-cancellation"])
|
||
const ac = new AbortController()
|
||
const pending = a.ask(account, next, { signal: ac.signal })
|
||
await until(() => collecting && [...nativeSessions.entries][0]?.transportClosed)
|
||
const owner = [...nativeSessions.entries][0]
|
||
expect(owner.transportClosed).toBe(true)
|
||
const at = Date.now()
|
||
ac.abort()
|
||
expect((await pending).status).toBe(410)
|
||
await owner.pumpTask
|
||
expect(aborted).toBe(true)
|
||
expect(Date.now() - at).toBeLessThan(1000)
|
||
expect(_internal.owed.get(key)?.n).toBe(1)
|
||
expect(_internal.flights.has(key)).toBe(false)
|
||
NATIVE_LIMITS.perProcess = 0
|
||
const later = await a.ask(account, next)
|
||
expect(later.body.usage.magpie_deferred).toEqual(EARLIER_DEFERRED)
|
||
expect(_internal.owed.has(key)).toBe(false)
|
||
expect(a.results).toEqual([])
|
||
})
|
||
|
||
// yetone/magpie#2672: a Run that sent some thinking and then nothing (no
|
||
// text, call, count or TurnEnded) kept the reply open until the agent gave
|
||
// up after 305 s, retained (native) or not (stateless, the path it was on).
|
||
const sseOf = async (response) => {
|
||
const reader = response.body.getReader()
|
||
let sse = ""
|
||
for (;;) {
|
||
const part = await reader.read()
|
||
if (part.done) return sse
|
||
sse += new TextDecoder().decode(part.value)
|
||
}
|
||
}
|
||
for (const path of ["native", "stateless"]) {
|
||
test(`${path}: a Run silent after its thinking ends in an error within idle (#2672)`, async () => {
|
||
NATIVE_LIMITS.idle = 150
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
const a = await agent({ thinkingOnly: true })
|
||
const response = await a.ask(auth(), initial({ stream: true }))
|
||
expect(response.headers.get("X-Magpie-Cursor-Continuity")).toMatch(path === "native" ? /^same-run/ : /^stateless/)
|
||
const started = Date.now()
|
||
const sse = await sseOf(response)
|
||
expect(Date.now() - started).toBeLessThan(1000)
|
||
expect(sse).toContain("native planning one")
|
||
expect(sse).toContain("Cursor went quiet: no message for 0 s, its last message thinking (try again)")
|
||
expect(sse).not.toContain("[DONE]")
|
||
await until(() => a.closedStreams === 1)
|
||
})
|
||
|
||
test(`${path}: Cursor's heartbeats alone don't keep a silent Run open past quiet (#2672)`, async () => {
|
||
Object.assign(NATIVE_LIMITS, { idle: 150, quiet: 400 })
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
const a = await agent({ thinkingOnly: true, serverHeartbeat: 40 })
|
||
const response = await a.ask(auth(), initial({ stream: true }))
|
||
// the heartbeats branch names the wait as past quiet: that it ended so,
|
||
// and not on idle, is the check, not how long a loaded machine took
|
||
const sse = await sseOf(response)
|
||
const [, beats] = sse.match(/Cursor went quiet: (\d+) heartbeats and nothing else for \d+ s, its last message thinking/) ?? []
|
||
expect(Number(beats)).toBeGreaterThan(0)
|
||
await until(() => a.closedStreams === 1)
|
||
})
|
||
}
|
||
|
||
// plugins#550: Cursor's heartbeats and nothing else after a checkpoint. The
|
||
// error names the checkpoint, and with Wire trace on, the Run's frames are
|
||
// saved in shape only: no prompt, thinking, checkpoint bytes, token or id.
|
||
for (const path of ["native", "stateless"]) {
|
||
test(`${path}: a quiet Run's error names its checkpoint, and Wire trace saves its frames scrubbed (plugins#550)`, async () => {
|
||
Object.assign(NATIVE_LIMITS, { idle: 150, quiet: 400 })
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
const secret = "SECRET-" + randomUUID()
|
||
const a = await agent({
|
||
thinkingOnly: true, serverHeartbeat: 40, initialReasoning: "thinking " + secret,
|
||
afterThinking: [checkpoint(message(bin(1, Buffer.from("checkpoint " + secret)), uint(10, 1), text(22, "cli")))],
|
||
})
|
||
const dir = mkdtempSync(join(tmpdir(), "cursor-trace-"))
|
||
try {
|
||
const account = auth()
|
||
const input = { directory: dir, magpie: { option: (id, key) => id === "cursor" && key === "wire_trace" } }
|
||
const chat = initial({ stream: true, messages: [{ role: "user", content: "prompt " + secret }] })
|
||
const sse = await sseOf(await a.ask(account, chat, { input }))
|
||
expect(sse).toMatch(path === "native"
|
||
? /Cursor went quiet: \d+ heartbeats and nothing else for \d+ s, its last message checkpoint #1 of the Run, the first of step 1, after blob request ×2, thinking \(try again\)/
|
||
: /Cursor went quiet: \d+ heartbeats and nothing else for \d+ s, its last message checkpoint #1 of the Run, after blob request ×2, thinking \(try again\)/)
|
||
await until(() => a.closedStreams === 1)
|
||
const files = readdirSync(join(dir, "cursor-traces"))
|
||
expect(files).toHaveLength(1)
|
||
const raw = readFileSync(join(dir, "cursor-traces", files[0]), "utf8")
|
||
for (const word of [secret, account.access, account.accountId, "synthetic-", "Bearer", "fixture-session"]) expect(raw).not.toContain(word)
|
||
const t = JSON.parse(raw)
|
||
expect(t).toMatchObject({ trace: 1, path, model: "fixture-model", error: { status: 502 } })
|
||
expect(t.error.message).toContain("checkpoint #1")
|
||
const kinds = t.frames.filter((f) => f.dir === "in").map((f) => f.kind)
|
||
const at = kinds.indexOf("checkpoint")
|
||
// Cursor asked for the history's two blobs, thought, then checkpointed
|
||
expect(kinds.slice(0, at)).toEqual(["blob request", "blob request", "thinking"])
|
||
// after the checkpoint, Cursor's heartbeats and nothing else
|
||
expect(kinds.slice(at + 1).length).toBeGreaterThan(0)
|
||
expect(kinds.slice(at + 1).every((k) => k === "heartbeat")).toBe(true)
|
||
// the checkpoint's shape: AgentServerMessage field 3, its state's fields by number
|
||
expect(t.frames.find((f) => f.kind === "checkpoint").fields[0].slice(0, 2)).toEqual([3, 2])
|
||
expect(t.frames.find((f) => f.kind === "checkpoint").fields[0][3].map((c) => c[0])).toEqual([1, 10, 22])
|
||
expect(t.frames.some((f) => f.dir === "out")).toBe(true)
|
||
expect(t.frames.some((f) => f.event === "quiet")).toBe(true)
|
||
} finally {
|
||
setWireTrace("")
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
}
|
||
|
||
test("native: Wire trace is off unless the option is on (plugins#550)", async () => {
|
||
Object.assign(NATIVE_LIMITS, { idle: 150 })
|
||
const a = await agent({ thinkingOnly: true })
|
||
const dir = mkdtempSync(join(tmpdir(), "cursor-trace-"))
|
||
try {
|
||
const input = { directory: dir, magpie: { option: () => false } }
|
||
const sse = await sseOf(await a.ask(auth(), initial({ stream: true }), { input }))
|
||
expect(sse).toContain("Cursor went quiet")
|
||
await until(() => a.closedStreams === 1)
|
||
expect(readdirSync(dir)).toEqual([])
|
||
} finally {
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
// plugins#635: the web-search test switch. Off, Cursor's own search is
|
||
// turned away as before: not allowed in the header, and its query fails a
|
||
// native Run or goes unanswered on a stateless one. On, the search alone is
|
||
// let through and approved, and the Run's trace is saved, in shape only.
|
||
const NO_SEARCH = "mcp_tool_call,get_mcp_tools_tool_call"
|
||
const searchFixture = (secret, extra = {}) => ({ search: { term: "term " + secret, ref: "ref-" + secret, ...extra } })
|
||
const traces = (dir) => {
|
||
try {
|
||
return readdirSync(join(dir, "cursor-traces"))
|
||
} catch {
|
||
return []
|
||
}
|
||
}
|
||
for (const path of ["native", "stateless"]) {
|
||
test(`${path}: with the web-search switch off, Cursor's search is refused as before (plugins#635)`, async () => {
|
||
Object.assign(NATIVE_LIMITS, { idle: 150 })
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
const a = await agent(searchFixture("off"))
|
||
const dir = mkdtempSync(join(tmpdir(), "cursor-search-"))
|
||
try {
|
||
const input = { directory: dir, magpie: { option: () => false } }
|
||
const result = await a.ask(auth(), initial(), { input })
|
||
expect(result.status).toBe(502)
|
||
expect(result.body.error.message).toContain(path === "native" ? "unsupported native interaction query" : "Cursor went quiet")
|
||
expect(a.allowed.length).toBeGreaterThan(0)
|
||
expect(a.allowed.every((h) => h === NO_SEARCH)).toBe(true)
|
||
expect(a.answers).toEqual([])
|
||
expect(readdirSync(dir)).toEqual([])
|
||
} finally {
|
||
setWebSearch("")
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
for (const outcome of ["succeeds", "fails"]) test(`${path}: with the web-search switch on, the search is approved, other tools refused, and a Run that ${outcome} saves its trace with no text (plugins#635)`, async () => {
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
const secret = "SECRET-" + randomUUID()
|
||
const a = await agent(searchFixture(secret, { shell: true, fail: outcome === "fails" }))
|
||
const dir = mkdtempSync(join(tmpdir(), "cursor-search-"))
|
||
try {
|
||
const account = auth()
|
||
// Wire trace stays off: the switch alone saves the trace
|
||
const input = { directory: dir, magpie: { option: (id, key) => id === "cursor" && key === "web_search_test" } }
|
||
const chat = initial({ messages: [{ role: "user", content: "prompt " + secret }] })
|
||
const result = await a.ask(account, chat, { input })
|
||
if (outcome === "succeeds") {
|
||
expect(result.status).toBe(200)
|
||
expect(choice(result).message.content).toBe("searched answer")
|
||
} else expect(result.status).toBeGreaterThanOrEqual(400)
|
||
expect(a.allowed[0]).toBe(NO_SEARCH + ",web_search_tool_call")
|
||
// InteractionResponse {id 3, web_search_request_response 2 {approved 1 {}}}
|
||
expect(a.answers).toHaveLength(1)
|
||
expect(number(a.answers[0], 1)).toBe(3)
|
||
expect(readWire(a.answers[0]).map((f) => f.no)).toEqual([1, 2])
|
||
expect(readWire(nest(a.answers[0], 2)).map((f) => f.no)).toEqual([1])
|
||
expect(nest(nest(a.answers[0], 2), 1)).toHaveLength(0)
|
||
// Cursor's shell is still turned away
|
||
expect(a.unavailable).toHaveLength(1)
|
||
expect(a.closes).toContain(78)
|
||
await until(() => traces(dir).length > 0)
|
||
const files = traces(dir)
|
||
expect(files).toHaveLength(1)
|
||
const raw = readFileSync(join(dir, "cursor-traces", files[0]), "utf8")
|
||
for (const word of [secret, "searched answer", "synthetic shell", "invalid", account.access, account.accountId, "synthetic-", "Bearer", "fixture-session"]) expect(raw).not.toContain(word)
|
||
const t = JSON.parse(raw)
|
||
expect(t).toMatchObject({ trace: 1, path, model: "fixture-model" })
|
||
if (outcome === "succeeds") expect(t.error).toBeUndefined()
|
||
else expect(t.error.status).toBeGreaterThanOrEqual(400)
|
||
expect(t.frames.some((f) => f.dir === "in" && f.kind === "message #7")).toBe(true)
|
||
expect(t.frames.some((f) => f.event === "web search")).toBe(true)
|
||
expect(t.frames.some((f) => f.dir === "out" && f.fields?.[0]?.[0] === 6)).toBe(true)
|
||
} finally {
|
||
setWebSearch("")
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test(`${path}: with the web-search switch on, a Run in which Cursor never searched saves nothing (plugins#635)`, async () => {
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
const a = await agent({ finalOnly: true })
|
||
const dir = mkdtempSync(join(tmpdir(), "cursor-search-"))
|
||
try {
|
||
const input = { directory: dir, magpie: { option: (id, key) => id === "cursor" && key === "web_search_test" } }
|
||
const result = await a.ask(auth(), initial(), { input })
|
||
expect(result.status).toBe(200)
|
||
expect(a.allowed[0]).toBe(NO_SEARCH + ",web_search_tool_call")
|
||
expect(traces(dir)).toEqual([])
|
||
} finally {
|
||
setWebSearch("")
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test(`${path}: with the web-search switch on, another interaction query is refused as before and saves nothing (plugins#635)`, async () => {
|
||
Object.assign(NATIVE_LIMITS, { idle: 150 })
|
||
if (path === "stateless") NATIVE_LIMITS.perProcess = 0
|
||
// InteractionQuery.web_fetch_request_query (9)
|
||
const a = await agent(searchFixture("fetch", { query: 9 }))
|
||
const dir = mkdtempSync(join(tmpdir(), "cursor-search-"))
|
||
try {
|
||
const input = { directory: dir, magpie: { option: (id, key) => id === "cursor" && key === "web_search_test" } }
|
||
const result = await a.ask(auth(), initial(), { input })
|
||
expect(result.status).toBe(502)
|
||
expect(result.body.error.message).toContain(path === "native" ? "unsupported native interaction query" : "Cursor went quiet")
|
||
expect(a.answers).toEqual([])
|
||
expect(traces(dir)).toEqual([])
|
||
} finally {
|
||
setWebSearch("")
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
}
|
||
|
||
test("stateless: a call with no count, then silence, is an error, not a step of tool calls (#2672)", async () => {
|
||
Object.assign(NATIVE_LIMITS, { idle: 150, perProcess: 0 })
|
||
const a = await agent({ noCount: true, stateDelay: 10_000 })
|
||
const r = await a.ask(auth(), initial())
|
||
expect(r.status).toBe(502)
|
||
expect(r.body.error.message).toContain("Cursor went quiet")
|
||
expect(r.body.choices).toBeUndefined()
|
||
})
|
||
|
||
test("native: a Run parked for the caller's results longer than idle still continues (#2672)", async () => {
|
||
NATIVE_LIMITS.idle = 120
|
||
const a = await agent(), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
await new Promise((r) => setTimeout(r, 400))
|
||
const owner = [...nativeSessions.entries][0]
|
||
expect(owner.error).toBeUndefined()
|
||
const next = continueWith(chat, first.body, ["after a long tool"])
|
||
const second = await a.ask(account, next)
|
||
expect(second.status).toBe(200)
|
||
expect(second.diagnostic).toMatch(/^same-run/)
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|
||
|
||
test("native: a continued Run that goes quiet after the caller's results ends within idle (#2672)", async () => {
|
||
NATIVE_LIMITS.idle = 150
|
||
const a = await agent({ finalAfterFirst: true, stallFinal: true }), account = auth(), chat = initial()
|
||
const first = await a.ask(account, chat)
|
||
expect(first.status).toBe(200)
|
||
expect(first.diagnostic).toMatch(/^same-run/)
|
||
const second = await a.ask(account, continueWith(chat, first.body, ["after the tool"]))
|
||
expect(second.status).toBe(502)
|
||
expect(second.body.error.message).toContain("Cursor went quiet: no message for")
|
||
expect(a.runs).toHaveLength(1)
|
||
})
|