diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index e9c03fe..38ca081 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -2656,12 +2656,12 @@ async function answerOf(auth, chat, signal, session = "") { let response = native ? await runNative({ ...args, native }) : await runOnce(args) const rebuild = async () => { const fallback = await runOnce(args) - fallback.diagnostic = "stateless; bootstrap=imported-history; reason=missing-batch-count" + fallback.diagnostic = "stateless; bootstrap=imported-history; reason=" + (response.error?.continuityReason || "missing-batch-count") return fallback } - // A missing native batch count may be retried once, only before Chat - // output is exposed or any native result was submitted. Never repeat an - // uncertain tool result write or duplicate a progressive stream's output. + // A missing or conflicting native batch count may be retried once, + // only before Chat output is exposed or any native result was submitted. + // Never repeat an uncertain tool write or progressive stream output. if (response.error?.retentionIneligible && !signal?.aborted) response = await rebuild() else if (native) response.rebuild = rebuild response.diagnostic ??= "stateless; bootstrap=imported-history; reason=" + (prepared.fallback || "retention capacity or reservation unavailable") @@ -2719,8 +2719,10 @@ async function answerOf(auth, chat, signal, session = "") { for await (const e of events) { if (e.error) { if (!e.error.retentionIneligible || !r.rebuild || signal?.aborted) return errorResponse(e.error) + const reason = e.error.continuityReason r = await r.rebuild() if (r.error) return errorResponse(r.error) + if (reason) r.diagnostic = "stateless; bootstrap=imported-history; reason=" + reason events = r.events responseHeaders["X-Magpie-Cursor-Continuity"] = r.diagnostic retry = true diff --git a/packages/cursor/native-e2e.test.mjs b/packages/cursor/native-e2e.test.mjs index d7a0917..4129231 100644 --- a/packages/cursor/native-e2e.test.mjs +++ b/packages/cursor/native-e2e.test.mjs @@ -103,6 +103,12 @@ async function agent(options = {}) { } 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 = () => { @@ -138,6 +144,11 @@ async function agent(options = {}) { 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) @@ -1087,6 +1098,61 @@ for (const options of [{ noCount: true }, { cleanNoCount: true }]) test(`native 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 }) diff --git a/packages/cursor/native-session.mjs b/packages/cursor/native-session.mjs index e739143..f9d874c 100644 --- a/packages/cursor/native-session.mjs +++ b/packages/cursor/native-session.mjs @@ -675,7 +675,13 @@ class NativeSession { } listed(count) { if (!Number.isSafeInteger(count) || count < 1) invalid("invalid native tool count", 502) - if (this.segment.expected != null && this.segment.expected !== count) invalid("inconsistent native tool count", 502) + if (this.segment.expected != null && this.segment.expected !== count) { + // Conflicting counts leave the native step boundary ambiguous. Retire + // this owner; Chat may rebuild from history only if no native result + // was submitted to this Run. + this.fail({ status: 502, message: "inconsistent native tool count", retentionIneligible: !this.resultSubmitted, continuityReason: "inconsistent-batch-count" }) + return + } this.segment.expected = count if (this.state === "awaiting-results") return // an identical retransmission this.waitForBatch() @@ -876,7 +882,10 @@ class NativeSession { } else if (u.num === 14) { if (!this.segment.content && !this.segment.calls.length) invalid("an empty reply (Cursor ended the turn without text or a tool call)", 502) this.terminalSeen(uf) - } else if (u.num === 27) this.listed(num(uf, 1)) + } else if (u.num === 27) { + this.listed(num(uf, 1)) + if (this.error) return + } } break case 2: { diff --git a/packages/cursor/package.json b/packages/cursor/package.json index a91937b..207249b 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.2.18", + "version": "0.2.19", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./index.mjs",