diff --git a/internal/gateway/cursor.go b/internal/gateway/cursor.go index a2a9b4014..21ee076dc 100644 --- a/internal/gateway/cursor.go +++ b/internal/gateway/cursor.go @@ -1016,6 +1016,24 @@ func (st *cursorStream) decode(ctx context.Context, br *bufio.Reader, out chan<- send(Event{Kind: KError, Text: msg}) } var calls, listed, said int + // answered: the step's execs answered here, not handed to the caller + // (Cursor's get_mcp_tools, a shell or file tool of its own refused). + // Cursor's count of a step's calls takes them in (plugins #142). + var answered int + // stepDone is whether every call the step listed has come. A step + // whose calls were all answered here goes on on Cursor's side, under + // a count of its own: this one is spent, not left to end the next + // step early. + stepDone := func() bool { + if listed == 0 || calls+answered < listed { + return false + } + if calls > 0 { + return true + } + listed, answered = 0, 0 + return false + } var usage Usage ended := false // Cursor said what the turn used var finish func() @@ -1091,22 +1109,27 @@ func (st *cursorStream) decode(ctx context.Context, br *bufio.Reader, out chan<- finish() return } - case 27: // how many tool calls the step makes, sent before them - if listed = int(pbNum(uf, 1)); listed > 0 && calls >= listed { + case 27: // how many tool calls the step makes, sent before or after them + if listed = int(pbNum(uf, 1)); stepDone() { settle() return } } } case 2: // exec_server_message: the server asks the client to run something + called := false if !st.exec(m.data, tools, func(id, name string, args []byte) bool { + called = true calls++ said += len(args) return send(Event{Kind: KToolStart, ID: id, Name: name}) && send(Event{Kind: KToolArgs, Text: string(args)}) }) { return } - if listed > 0 && calls >= listed { // every call made: the caller runs them + if !called && !cursorAsksContext(m.data) { + answered++ + } + if stepDone() { // every call made: the caller runs them settle() return } @@ -1192,6 +1215,17 @@ func (st *cursorStream) exec(msg []byte, tools []bridgeTool, call func(id, name return true } +// cursorAsksContext is whether an exec is Cursor asking for the +// environment (request_context_args), which is not a call of the step. +func cursorAsksContext(msg []byte) bool { + for _, e := range pbFields(msg) { + if e.num == 10 { + return true + } + } + return false +} + // pbStr and pbNum are a field of a message read, "" or 0 when it has none. func pbStr(fs []pbField, num int) string { for _, f := range fs { diff --git a/internal/gateway/cursor_test.go b/internal/gateway/cursor_test.go index 0e5af7924..5bb56393c 100644 --- a/internal/gateway/cursor_test.go +++ b/internal/gateway/cursor_test.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "encoding/hex" "encoding/json" "io" "net/http" @@ -106,6 +107,53 @@ func TestCursorDecodesTextAndCalls(t *testing.T) { } } +// cursorCaptured is a frame of real Cursor's Run, as captured (without its +// envelope) on 2026-10-15, model auto, for plugins #142. +func cursorCaptured(t *testing.T, h string) []byte { + t.Helper() + b, err := hex.DecodeString(h) + if err != nil { + t.Fatal(err) + } + return cursorFrame(pb(b)) +} + +// Cursor's count of a step's calls (update 27, sent after them) takes in +// the execs answered here: its get_mcp_tools, and a tool of its own refused +// (plugins #142). Real Cursor's frames: get_mcp_tools, a WebSearch call, a +// count of 2. Counting only the call, the step never ended while Cursor +// waited for WebSearch's result. +func TestCursorCountTakesAnsweredExecs(t *testing.T) { + evs, sent, _ := cursorDecoded(t, nil, + cursorCaptured(t, "123f9a01360a2031633366376432386261313531653137303639616231343163626637316165651210653532623935616633363331663934321800a20200b80300"), + cursorCaptured(t, "12f80108015a91010a106d61677069652d576562536561726368120b0a047061746812031a01781a5563616c6c2d35653434356466642d636438612d343238642d623561352d6562656137313838376435392d310a66635f61633833643337622d666536362d393964622d623036652d3736613663633562616564325f3122066d61677069652a095765625365617263684a066d61677069657a2462616465343834312d366230342d346464632d626330392d3936313763306635343666389a01360a2031633366376432386261313531653137303639616231343163626637316165651210333632323535313938666465653532321800b80301"), + cursorCaptured(t, "0a05da01020802"), + cursorUpdate(1, pb{}.str(1, "never read")), + ) + want := `|call:call-5e445dfd-cd8a-428d-b5a5-ebea71887d59-1__fc_ac83d37b-fe66-99db-b06e-76a6cc5baed2_1/WebSearch|args:{"path":"x"}|stop:tool` + if got := said(evs); got != want { + t.Fatalf("got %s\nwant %s", got, want) + } + if len(sent) == 0 || sent[0][0].num != 2 { + t.Fatalf("get_mcp_tools not answered: %v", sent) + } + + // a step whose calls were all answered here spends its count: the + // next step's two calls both go to the caller, not the first alone + evs, _, _ = cursorDecoded(t, nil, + cursorCaptured(t, "123f9a01360a2031633366376432386261313531653137303639616231343163626637316165651210653532623935616633363331663934321800a20200b80300"), + cursorUpdate(27, pb{}.varint(1, 1)), + cursorCallFrame(3, "call_a", "get_weather", "Paris"), + cursorCallFrame(4, "call_b", "get_weather", "Lima"), + cursorUpdate(27, pb{}.varint(1, 2)), + cursorUpdate(1, pb{}.str(1, "never read")), + ) + want = `|call:call_a/get_weather|args:{"city":"Paris"}|call:call_b/get_weather|args:{"city":"Lima"}|stop:tool` + if got := said(evs); got != want { + t.Fatalf("got %s\nwant %s", got, want) + } +} + func TestCursorDecodesTheEnd(t *testing.T) { evs, _, _ := cursorDecoded(t, nil, cursorUpdate(1, pb{}.str(1, "Hi")), diff --git a/internal/provider/migrate_side.go b/internal/provider/migrate_side.go index eceac7008..781431d55 100644 --- a/internal/provider/migrate_side.go +++ b/internal/provider/migrate_side.go @@ -221,7 +221,7 @@ func init() { // the plugin reads cursor-agent's token as the built-in does. movers["cursor"] = &mover{ pkg: "@magpie-community/opencode-cursor-auth", - min: "0.2.14", // a Run tool call stays on one native Cursor session across its result, and a tool that outlasts Cursor resumes stateless, not as a failed turn (plugins #135); 0.2.13: a Run cancelled while it opens is closed upstream, not left streaming for nobody (plugins #130), and dashboard usage is collected per account, so one account never takes another's event as its debt (plugins #131); 0.2.12: Grok Bot is among the panel's three rings ahead of an On-demand with no limit, which is only a dollar figure (#2213); 0.2.11: a step's usage is its own, and what it collected of earlier steps goes as usage.magpie_deferred, billed with it but never told as its prompt, so pi's context isn't inflated (#1053, plugins #101); 0.2.10: a Team member with no limit of their own spends the team's on-demand pool, which is shown and counted as the limit, not as unlimited (#1868); 0.2.9: a default model whose own branch stops short of the asked effort goes to its nearest effort, so the id and its parameters agree (plugins #55); 0.2.8: a failure's status and its sign-in mark as the built-in's; Max Mode models retried in Max Mode; glm-5.3 listed; catalog entries written as CallDynamicTool calls, an empty turn retried (plugins #11); a tool step's usage from the dashboard's usage event, cache included (#676); far from Cursor a step whose event shows late is counted by the conversation's next steps instead of guessed (#1053); a turn's parallel calls split over two assistant messages keep their results (#1275); the list is Cursor's picker, a model once per context size with that window, effort and fast sent as the variant's parameters, and an id 0.1.x listed still runs as the variant it stood for (plugins #51); an id 0.1.x listed, at an effort only its other branch has, goes as that branch's nearest effort, not as the default's id with parameters that contradict it (plugins #54); a model listed at a size Cursor has a fast variant of says fast, for magpie's Fast switch (#1360); a Run HTTP/2 can't open (a proxy or network that blocks it, "h2 is not supported") goes over HTTP/1.1 as RunSSE + BidiAppend on api2 (plugins #59); a model asked for by its bare name (grok-4.7) counts in the pool its sized ids (grok-4.7@256k) are in (Will on Discord); an account with a Grok Bot allowance shows it on its Usage card, aside, so a spent Bot never stops Cursor requests (plugins #64); an account that may spend on-demand (a team or enterprise that pays past the included usage) is not used up when its included pools are, its On-demand spend is (fottencity on Discord, #1476); an assistant turn's reasoning_content is kept across a tool continuation (plugins #82); an empty reply says what Cursor's Run sent before it, so the next report shows why (plugins #60) + min: "0.2.15", // calls the plugin answers itself (a tool the request doesn't have, get_mcp_tools) count toward Cursor's tool count, so a step with one isn't failed as "inconsistent native tool count" or a batch never completed, and a long history's blob burst waits for a slow link instead of failing as "native outbound messages limit reached" (plugins #142); 0.2.14: a Run tool call stays on one native Cursor session across its result, and a tool that outlasts Cursor resumes stateless, not as a failed turn (plugins #135); 0.2.13: a Run cancelled while it opens is closed upstream, not left streaming for nobody (plugins #130), and dashboard usage is collected per account, so one account never takes another's event as its debt (plugins #131); 0.2.12: Grok Bot is among the panel's three rings ahead of an On-demand with no limit, which is only a dollar figure (#2213); 0.2.11: a step's usage is its own, and what it collected of earlier steps goes as usage.magpie_deferred, billed with it but never told as its prompt, so pi's context isn't inflated (#1053, plugins #101); 0.2.10: a Team member with no limit of their own spends the team's on-demand pool, which is shown and counted as the limit, not as unlimited (#1868); 0.2.9: a default model whose own branch stops short of the asked effort goes to its nearest effort, so the id and its parameters agree (plugins #55); 0.2.8: a failure's status and its sign-in mark as the built-in's; Max Mode models retried in Max Mode; glm-5.3 listed; catalog entries written as CallDynamicTool calls, an empty turn retried (plugins #11); a tool step's usage from the dashboard's usage event, cache included (#676); far from Cursor a step whose event shows late is counted by the conversation's next steps instead of guessed (#1053); a turn's parallel calls split over two assistant messages keep their results (#1275); the list is Cursor's picker, a model once per context size with that window, effort and fast sent as the variant's parameters, and an id 0.1.x listed still runs as the variant it stood for (plugins #51); an id 0.1.x listed, at an effort only its other branch has, goes as that branch's nearest effort, not as the default's id with parameters that contradict it (plugins #54); a model listed at a size Cursor has a fast variant of says fast, for magpie's Fast switch (#1360); a Run HTTP/2 can't open (a proxy or network that blocks it, "h2 is not supported") goes over HTTP/1.1 as RunSSE + BidiAppend on api2 (plugins #59); a model asked for by its bare name (grok-4.7) counts in the pool its sized ids (grok-4.7@256k) are in (Will on Discord); an account with a Grok Bot allowance shows it on its Usage card, aside, so a spent Bot never stops Cursor requests (plugins #64); an account that may spend on-demand (a team or enterprise that pays past the included usage) is not used up when its included pools are, its On-demand spend is (fottencity on Discord, #1476); an assistant turn's reasoning_content is kept across a tool continuation (plugins #82); an empty reply says what Cursor's Run sent before it, so the next report shows why (plugins #60) agents: []string{"cursor"}, served: cursorServed, out: func() ([]Moving, error) {