Files
plugins/packages/cursor/reasoning.test.mjs
T
yetone a41a8d3a57 cursor 0.2.20: the MCP server the caller's tools go to Cursor under is "tools", not "magpie"
What goes to Cursor named magpie in four places: the dynamic tool catalog
in the system prompt ('The MCP namespace "magpie" has the tools below' and
each <call> entry's "namespace":"magpie"), every CallDynamicTool call in
the history (args.namespace), each McpToolDefinition's provider_identifier
(field 4), and the server answered for mcp_state_exec_args (name and
identifier). magpie's LESSONS.md rule: text added to a vendor's prompt
names neither magpie nor the vendor's limits, with the care a header
change gets (5d33f060b / 3c4f8f9ae).

cursor-agent names a user's MCP server by its mcp.json label
(providerIdentifier = serverName) and a call by "<label>-<tool>"; read in
cursor-agent 2026.10.01-e373342. The plugin now does the same with the
generic label "tools" (NS), in all four places. Calls are read back under
NAMESPACES = ["tools", "magpie"]: a "<ns>-<tool>" name without field 5 is
stripped of either prefix, and a native exec's provider/server is accepted
as either, so a native session begun on 0.2.19 still has its calls served.
The non-native history is rebuilt from the caller's messages each request,
so it carries the new name at once.

Siblings in other packages that still name magpie upstream, filed, not
changed here: commandcode User-Agent and x-project-slug (#352), kimi-work
User-Agent (#353), qoder business.name (#354), autoclaw device_id (#355),
kimi-desktop's instructions tag (#356), opendesign's host fallback (#357),
layerfs tool descriptions (#358). Left alone as not upstream: X-Magpie-*
response headers, the x-magpie-cursor-session hook header (never sent to
Cursor), magpie_native/magpie_deferred usage fields, sign-in messages.

Test: upstream-words.test.mjs builds a Run (buildRun) from a conversation
with tools and a tool call, and checks that the catalog, every transcript
blob, the request bytes and the mcp_state answer contain no /magpie/i, and
that a call is read back as both "tools-read" and "magpie-read". Red on
the old index.mjs (2 of 3 fail: catalog, mcp_state); green now.
conversation, reasoning and native-test-wire expectations moved to
"tools"; native-e2e still sends provider "magpie", so the old name is
covered on the native path.

Run at this tree:
- bun test in packages/cursor: 279 pass, 0 fail (17 files).
- bun scripts/check.mjs: every package ok.
- Live, owner's Cursor account, sandbox HOME (scratchpad), the plugin's
  loader.fetch with one read_file tool, http2 writes scanned for "magpie":
  auto: turn 1 called read_file {"path":"notes.txt"}, turn 2 with the
  result answered "Pelican"; 0 chunks named magpie (the old index.mjs
  under the same scan: 2). grok-4.7-fast: the same two turns, 0 chunks.
  claude-4.6-sonnet: 429, not in this plan's weekly usage.
Not tried: a native session resumed across the upgrade against real Cursor
(covered by native-e2e's "magpie" provider against the fake server only).
2026-10-24 16:54:42 +08:00

73 lines
4.2 KiB
JavaScript

// Tool continuations replay the assistant's reasoning as AI SDK reasoning
// parts, rather than discarding it when a new Run is built. No request leaves
// this machine; fixtures contain no real conversation or account data.
import "./nonet.mjs"
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const call = (id) => ({ id, type: "function", function: { name: "read", arguments: '{"path":"a"}' } })
const messages = (assistant) => [
{ role: "user", content: "fix the bug" },
{ role: "assistant", content: null, tool_calls: [call("toolu_a")], ...assistant },
{ role: "tool", tool_call_id: "toolu_a", content: "the source" },
]
const convert = (msgs) => _internal.conversation({ messages: msgs }, [])
const parts = (conv) => conv.msgs.map((b) => JSON.parse(b.toString())).filter((m) => m.role === "assistant")
test("reasoning_content survives a tool continuation and the Run's transcript blobs", () => {
const conv = convert(messages({ reasoning_content: "The branch needs a null check.", content: "Reading the source." }))
expect(parts(conv)[0].content).toEqual([
{ type: "reasoning", text: "The branch needs a null check." },
{ type: "text", text: "Reading the source." },
{ type: "tool-call", toolCallId: "toolu_a", toolName: "CallDynamicTool", args: { namespace: "tools", toolName: "read", arguments: { path: "a" } } },
])
const run = _internal.buildRun(conv.msgs, conv.last, [], "grok-4.7-fast")
const transcript = [...run.blobs.values()].map((b) => b.toString())
expect(transcript).toContain(JSON.stringify(parts(conv)[0]))
expect(JSON.stringify(conv.msgs.map((b) => JSON.parse(b.toString())))).toContain("the source")
})
test("reasoning is accepted as the alternate Chat field, without duplicating both fields", () => {
expect(parts(convert(messages({ reasoning: "Keep the existing interface." })))[0].content[0]).toEqual({ type: "reasoning", text: "Keep the existing interface." })
expect(parts(convert(messages({ reasoning_content: "Canonical", reasoning: "Alias" })))[0].content.filter((p) => p.type === "reasoning")).toEqual([{ type: "reasoning", text: "Canonical" }])
})
test("split tool calls retain reasoning from both assistant messages in order", () => {
const msgs = [
{ role: "user", content: "read a and b" },
{ role: "assistant", content: null, reasoning_content: "Check a first.", tool_calls: [call("toolu_a")] },
{ role: "assistant", content: "Reading both.", reasoning: "Then check b.", tool_calls: [call("toolu_b")] },
{ role: "tool", tool_call_id: "toolu_a", content: "A" },
{ role: "tool", tool_call_id: "toolu_b", content: "B" },
]
const before = JSON.stringify(msgs)
const conv = convert(msgs)
expect(parts(conv)[0].content.filter((p) => p.type === "reasoning")).toEqual([{ type: "reasoning", text: "Check a first.\n\nThen check b." }])
expect(parts(conv)[0].content.filter((p) => p.type === "tool-call")).toHaveLength(2)
const results = conv.msgs.map((b) => JSON.parse(b.toString())).find((m) => m.role === "tool").content
expect(results.map((p) => p.experimental_content[0].text)).toEqual(["A", "B"])
expect(JSON.stringify(msgs)).toBe(before)
})
test("reasoning-only history is preserved as reasoning, not ordinary assistant text", () => {
expect(parts(convert([{ role: "user", content: "continue" }, { role: "assistant", content: null, reasoning_content: "An unfinished plan." }]))).toEqual([
{ role: "assistant", content: [{ type: "reasoning", text: "An unfinished plan." }] },
])
})
test("blank or non-string reasoning fields do not create empty or invalid parts", () => {
for (const value of [undefined, null, "", " ", {}, [], 42]) {
const got = parts(convert(messages({ reasoning_content: value })))[0].content
expect(got.filter((p) => p.type === "reasoning")).toEqual([])
expect(got.filter((p) => p.type === "tool-call")).toHaveLength(1)
}
expect(parts(convert(messages({ reasoning_content: "", reasoning: "Fallback" })))[0].content[0]).toEqual({ type: "reasoning", text: "Fallback" })
})
test("the input estimate includes replayed reasoning exactly once", () => {
const baseline = convert(messages({}))
const reasoning = "x".repeat(400)
const conv = convert(messages({ reasoning_content: reasoning, reasoning }))
expect(conv.estimate - baseline.estimate).toBe(100)
})