Files
magpie/internal/gateway/cursor_conversation_test.go
T
yetone 0f41570859 cursor: a conversation's Runs keep one conversation_id, so Grok's prompt cache on Cursor sticks, and Cursor's usage no longer counts the cache twice (#498, congee949). buildCursorRun put a new UUID in AgentRunRequest's conversation_id (field 5) on every Run, so Cursor's backend had nothing to keep a conversation on the machine holding its cached prefix — the x-grok-conv-id grokSigned sends xAI direct — and a Codex thread on cursor/grok-4.7-fast read about 2% from cache against 94% on the grok provider, its first token growing with the whole prompt. The id is now a UUID (v4-shaped, like the official client's) hashed from what names the session — the client's prompt_cache_key (Codex's thread id), else the agent's own session header (Claude Code's, OpenCode's) — together with the conversation's first user message, which every later request repeats; with neither key nor session header each Run still gets a fresh one, since a first message alone ("hi") would put strangers under one id. On concurrent subagents: Cursor's own client keeps one conversationId per agent session, and whether its backend treats the id as more than a cache-affinity key isn't known, so subagents sharing a session (Claude Code's Task agents share its session id; a Codex collab agent has its own thread and key anyway) get ids of their own through their own first message, never one id sent twice at once. TurnEndedUpdate's input_tokens is the whole prompt, cache read and written included, as Cursor's client has it; decode kept it as Usage.Input, which everywhere else is the uncached rest that prompt() and pricing add the cache to, so a cached turn was counted twice (in=137300 with cache_read=132352 is 4948 uncached). Input is now input_tokens less cache read and write, never below 0, and reasoning_tokens (field 5) is kept as Reasoning. TestCursorConversationID (same Codex key → same id across turns, other key → other id; same Claude Code session → same id, a subagent's first message → another; nothing naming it → a new id each Run), TestCursorUsageTakesTheCacheOut and the updated TestCursorDecodesTheEnd fail without the change. No real Cursor request was made. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./internal/gateway/ ./internal/provider/ pass.
2026-10-02 11:08:14 +08:00

143 lines
5.1 KiB
Go

package gateway
import (
"bufio"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A conversation's Runs go to Cursor with one conversation_id, which keeps
// its prompt cached where it was (#498): the client's prompt_cache_key or
// its own session names it; another key, or another conversation in one
// session, is another id; with nothing naming it each Run has a new one.
func TestCursorConversationID(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
var got []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := readConnectFrame(bufio.NewReader(r.Body))
if err != nil {
http.Error(w, `{"code":"invalid_argument","message":"no run"}`, 400)
return
}
conv := ""
for _, rf := range pbFields(f.data) {
if rf.num == 1 {
for _, x := range pbFields(rf.data) {
if x.num == 5 {
conv = string(x.data)
}
}
}
}
got = append(got, conv)
http.NewResponseController(w).EnableFullDuplex()
w.Header().Set("Content-Type", "application/connect+proto")
w.Write(cursorUpdate(1, pb{}.str(1, "ok")))
w.Write(cursorUpdate(14, pb{}.varint(1, 3).varint(2, 1)))
w.(http.Flusher).Flush()
}))
defer up.Close()
tok, ver, agent, api := cursorToken, cursorVersion, cursorAgent, cursorAPI
cursorToken = func() (string, error) { return "tok", nil }
cursorVersion = func() string { return "cli-test" }
cursorAgent, cursorAPI = up.URL, up.URL
defer func() { cursorToken, cursorVersion, cursorAgent, cursorAPI = tok, ver, agent, api }()
forgetCursorEndpoint(t)
conv := func(from provider.Protocol, header, body string) string {
t.Helper()
r := httptest.NewRequest("POST", "/", strings.NewReader(body))
if header != "" {
k, v, _ := strings.Cut(header, ": ")
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
var u Usage
New().serveCursor(w, r, from, "auto", []byte(body), &u)
if w.Code != 200 || len(got) == 0 {
t.Fatalf("%d %s", w.Code, w.Body)
}
id := got[len(got)-1]
if !regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`).MatchString(id) {
t.Fatalf("conversation_id %q is no UUID", id)
}
return id
}
codex := func(key, first, last string) string {
return `{"model":"x","stream":false,"prompt_cache_key":"` + key + `","input":[` +
`{"type":"message","role":"user","content":[{"type":"input_text","text":"` + first + `"}]},` +
`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"sure"}]},` +
`{"type":"message","role":"user","content":[{"type":"input_text","text":"` + last + `"}]}]}`
}
// Codex: its thread's prompt_cache_key, turn after turn
a1 := conv(provider.Responses, "", codex("thread-a", "fix the bug", "go on"))
a2 := conv(provider.Responses, "", codex("thread-a", "fix the bug", "and the tests"))
if a1 != a2 {
t.Fatalf("one thread, two ids: %s %s", a1, a2)
}
if b := conv(provider.Responses, "", codex("thread-b", "fix the bug", "go on")); b == a1 {
t.Fatal("two threads, one id")
}
// Claude Code: its session header; a Task agent in that session, with a
// first message of its own, is a conversation of its own
cc := func(first, last string) string {
return `{"model":"x","max_tokens":10,"messages":[{"role":"user","content":"` + first + `"},` +
`{"role":"assistant","content":"sure"},{"role":"user","content":"` + last + `"}]}`
}
const session = "x-claude-code-session-id: 3f0c"
m1 := conv(provider.Anthropic, session, cc("plan it", "next"))
m2 := conv(provider.Anthropic, session, cc("plan it", "and then"))
if m1 != m2 {
t.Fatalf("one session, two ids: %s %s", m1, m2)
}
if sub := conv(provider.Anthropic, session, cc("search the repo", "next")); sub == m1 {
t.Fatal("a subagent shares the main conversation's id")
}
// nothing names the conversation: a new id each Run
chat := `{"model":"x","messages":[{"role":"user","content":"hi"}]}`
if conv(provider.Chat, "", chat) == conv(provider.Chat, "", chat) {
t.Fatal("unnamed conversations share an id")
}
}
// TurnEndedUpdate's input_tokens counts the cache read and written; Usage's
// Input is the uncached rest, and reasoning_tokens is kept (#498).
func TestCursorUsageTakesTheCacheOut(t *testing.T) {
for _, c := range []struct {
end pb
want Usage
}{
{pb{}.varint(1, 137300).varint(2, 204).varint(3, 132352).varint(5, 90),
Usage{Input: 4948, Output: 204, CacheRead: 132352, Reasoning: 90}},
{pb{}.varint(1, 5000).varint(2, 10).varint(3, 1024).varint(4, 2048),
Usage{Input: 1928, Output: 10, CacheRead: 1024, CacheWrite: 2048}},
{pb{}.varint(1, 100).varint(2, 1),
Usage{Input: 100, Output: 1}},
{pb{}.varint(1, 10).varint(2, 1).varint(3, 64), // never below none
Usage{Input: 0, Output: 1, CacheRead: 64}},
} {
evs, _, _ := cursorDecoded(t, nil, cursorUpdate(1, pb{}.str(1, "Hi")), cursorUpdate(14, c.end))
var u *Usage
for _, ev := range evs {
if ev.Kind == KUsage {
u = &ev.Usage
}
}
if u == nil || *u != c.want {
t.Fatalf("usage %+v, want %+v", u, c.want)
}
if u.prompt() != int(pbNum(pbFields(c.end), 1)) && u.Input > 0 {
t.Fatalf("prompt %d", u.prompt())
}
}
}