Files
yetone 0c35adbf03 cursor 0.2.18: a failed tool result tells Cursor isError, and a refused keychain read is not asked again for ten minutes
Two things magpie's built-in Cursor did that the plugin didn't, ahead of
the built-in's retirement:

- The stateless path (conversation) sent every tool result with
  isError false. A chat tool message with is_error: true (what magpie now
  sends for a failed tool, as the built-in's cursorResult told Cursor
  isError) is marked isError, as the native path already did.
- cliToken ran `security` on every call. It now reads the Keychain as the
  built-in's cursorKeychainToken (internal/provider/cursor.go; magpie
  #746, #2408): a read is kept 10s; a read that failed with anything but
  exit 44 (no such item) may have been a dialog the user said no to, and
  is not asked again for 10 minutes; reads go one at a time, so callers
  at once share one run. A sign-in and the read after `cursor-agent
  status` read afresh, as forgetCursorStatus / cursorTokenOf(true) did.
  README's "read on every request" says so now.

What was run (bun 1.4.2, macOS, load average ~280-300):
- parity.test.mjs (new, 5 tests): green. Broken by hand, each watched
  red, then restored: is_error ignored (1 red), cache check removed
  (3 red), denied backoff at 10s (1 red), exit 44 treated as refusal
  (1 red), no read serialisation (1 red).
- `security find-generic-password` for a missing item gives execFile
  err.code 44 (number) on this Mac, as the mapping assumes.
- Full `bun test` in packages/cursor, 7 runs: 272/272 green in 4; the
  other 3 red only in latency.test.mjs ("events never show in time",
  #159; "nothing naming the session", #162), which the change doesn't
  touch. On clean origin/main aec06bd, latency.test.mjs alone is red
  too, including "far from Cursor ... cache and all" (filed as #165).
- Not tried: a real Keychain dialog denial; the backoff is checked with
  a fake `security`.
2026-10-16 17:36:57 +08:00

90 lines
3.4 KiB
JavaScript

// What magpie's built-in Cursor did that the plugin does too, ahead of the
// built-in's retirement: a failed tool result tells Cursor isError
// (internal/gateway/cursor.go, cursorResult), and the Keychain is read as
// cursorKeychainToken reads it (internal/provider/cursor.go; magpie #746,
// #2408).
import "./nonet.mjs" // first: no request leaves this machine
import { afterEach, expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { conversation, keychain, keychainToken } = _internal
const call = (id) => ({ role: "assistant", content: "", tool_calls: [{ id, type: "function", function: { name: "bash", arguments: "{}" } }] })
const results = (chat) =>
conversation(chat, []).msgs.map((b) => JSON.parse(b.toString())).filter((m) => m.role === "tool").flatMap((m) => m.content)
test("a tool result marked is_error tells Cursor isError on the stateless path; a good one doesn't", () => {
const got = results({
messages: [
{ role: "user", content: "go" },
call("call_a"),
{ role: "tool", tool_call_id: "call_a", content: "exit status 1", is_error: true },
call("call_b"),
{ role: "tool", tool_call_id: "call_b", content: "ok" },
{ role: "user", content: "and?" },
],
})
expect(got.map((r) => [r.toolCallId, r.isError === true])).toEqual([["call_a", true], ["call_b", false]])
})
// a fake clock and `security`: each read counted, its answer set per test
let clock = 0
let reads = 0
let answer = { ok: true, out: "tok\n", code: 0 }
const real = { now: keychain.now, read: keychain.read }
const fake = () => {
clock = 1_000_000
reads = 0
Object.assign(keychain, { tok: "", at: 0, ask: 0, busy: null, now: () => clock, read: async () => (reads++, answer) })
}
afterEach(() => Object.assign(keychain, { tok: "", at: 0, ask: 0, busy: null }, real))
test("a token read from the Keychain is taken as it for ten seconds, then read again", async () => {
fake()
answer = { ok: true, out: "tok\n", code: 0 }
expect(await keychainToken()).toBe("tok")
clock += 9_999
expect(await keychainToken()).toBe("tok")
expect(reads).toBe(1)
clock += 2
expect(await keychainToken()).toBe("tok")
expect(reads).toBe(2)
})
test("callers at once share one `security` run", async () => {
fake()
answer = { ok: true, out: "tok", code: 0 }
const got = await Promise.all([keychainToken(), keychainToken(), keychainToken()])
expect(got).toEqual(["tok", "tok", "tok"])
expect(reads).toBe(1)
})
test("a refused read is not asked again for ten minutes; a fresh read asks", async () => {
fake()
answer = { ok: false, out: "", code: 128 } // the user said no to the dialog
expect(await keychainToken()).toBe("")
clock += 9 * 60_000
expect(await keychainToken()).toBe("")
expect(reads).toBe(1)
clock += 60_001
answer = { ok: true, out: "tok", code: 0 }
expect(await keychainToken()).toBe("tok")
expect(reads).toBe(2)
// after a refusal, a sign-in (fresh) still reads at once
answer = { ok: false, out: "", code: 51 }
expect(await keychainToken(true)).toBe("")
answer = { ok: true, out: "tok2", code: 0 }
expect(await keychainToken(true)).toBe("tok2")
expect(reads).toBe(4)
})
test("no item (exit 44) is no refusal: asked again after ten seconds", async () => {
fake()
answer = { ok: false, out: "", code: 44 }
expect(await keychainToken()).toBe("")
clock += 10_001
answer = { ok: true, out: "tok", code: 0 }
expect(await keychainToken()).toBe("tok")
expect(reads).toBe(2)
})