Files
magpie/internal/gateway/user_said_test.go
T
JoeyWu-tech 985b9f1a0a gateway: Reasonix's session-context snapshot is the agent's (#3938)
keeyangyy (#3938) named their client: Reasonix 1.x, which sends no
session header (it sets x-opencode-session on its OpenCode Go paths
only) and opens every conversation on a snapshot it writes, the same
bytes in every conversation started in one workspace while its memory
and skills catalog don't change:

  {"role":"user","content":"<session-context version=\"1\">\nThis host-generated snapshot supersedes every earlier session-context snapshot.\n\n## Environment\n...\nDigest: sha256:<hex>\n</session-context>"},
  {"role":"user","content":"the question the user actually typed"}

userWords didn't know the block, so the commit before this one still
hashed it alone: every conversation a workspace opened had one id, and
under Stays = session each new one was kept on the account that
answered the first.

"<session-context" joins injected, beside Codex's
"<environment_context>", with no closing ">" so that its version
attribute matches. What asks userWords now passes over the snapshot as
it passes over Codex's context:
- the conversation's id runs from the snapshot through what the user
  typed (conversationOpening);
- the turn count: [snapshot][typed] is turn 1, where it was 2
  (turnIn);
- a group's conversation key is the typed words, where it was the
  snapshot, the same for every conversation in the workspace
  (firstWords, in ruleKey and the affinity scope);
- the turn before, which the classifier is told (turnBefore): on a
  conversation's first turn, and on each turn a new snapshot comes
  with, it was the snapshot, workspace and memory and all, as the
  user's previous message with no reply; now it is nothing on a first
  turn, and the user's words before the snapshot and the reply to them
  on a later one;
- the classifier's question, Copilot's Auto and gatewayQuery take the
  user's last words before a snapshot sent after them, and the Claude
  subscription takes one after tool results as the agent's notice
  (agentNotice).

A Reasonix conversation under way when this lands gets an id of its
own on its next turn and is routed once as a new conversation, as the
previous commit says of the others: it may move to another account,
and its cache there starts over.

The shape is the report's, which keeyangyy read from Reasonix's source
(internal/sessioncontext) and didn't capture; its folder and digest
are left out as they left them, and the tests use it as written.

Not changed: the prompt breakdown (promptBuilder.userText) keeps its
own list of Codex's blocks and still files the snapshot as a chat
turn, so a Reasonix conversation's first turn shows there as two
turns, where Codex's context adds none. That is left for a commit of
its own.

What was run (tree 71364f08, on origin/main a5315c70, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
- With the previous commit's prompt.go and the tests as committed, the
  four tests this commit touches are red:
  TestReasonixConversationsInOneWorkspaceAreApart on every protocol
  ("chat: two conversations opened on one snapshot:
  magpie-310d1b42730f273079337f01 and magpie-310d1b42730f273079337f01
  (the snapshot alone: magpie-310d1b42730f273079337f01)");
  TestNewConversationsInOneFolderAreRoutedApart ("Reasonix's second
  conversation went to k2 as "session", conv
  magpie-310d1b42730f273079337f01; its first is on k2, conv
  magpie-310d1b42730f273079337f01"); TestTurnInSkipsWhatTheAgentPutIn
  ("Reasonix's snapshot, then the user's words: turn 2 within false,
  want 1 false"); TestFirstWordsAreWhatTheUserSaid ("key changed for
  conversation 4").
- Here, those four -count=5: 20 pass.
- The tests of the opening and of what userWords feeds, in
  internal/gateway, sessions, gui and provider (the conversation id,
  dsh's ids, affinity, rotate, strict, OpenCode's sessions, the turn
  count, the classifier's question, firstWords, Copilot's Auto, a
  rule's turn held past an agent's message): ok, 121 top-level tests
  passed, none failed.
- The previous commit's 12 hand-breaks of conversationOpening,
  userOpens and writeOpening, each alone on this tree: each red on the
  same tests as at a6037f68, and the first (the first user message
  hashed alone) also on TestReasonixConversationsInOneWorkspaceAreApart.
- -race -count=5 of the routing and opening tests with the new ones:
  85 pass, no race. -race -count=3 of the two allocation bounds: 6
  pass.
- gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go
  build -tags nogui for linux and darwin, and go vet and go build for
  windows without the tag: exit 0 each. go vet and go build without the
  tag on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here).
- go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2, log
  kept; 8m43s, load 28 at its start): 72 packages ok, 5 red, none of
  them this commit's tests or what they cover. internal/gateway ran to
  its end (510s), so the new tests passed in it. The reds: agent
  TestInstalledVersion, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and
  TestOmpDetectedWithPiDir (#3141), and
  TestReasonixCLIRequiresSupportedNativeVersion and
  TestReasonixCLIRecoversAfterProbeTimeout (#3641, the Agents page's
  check of the Reasonix CLI, which this doesn't reach); edit
  TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077,
  #3640); gateway TestPluginToldWhereTheAgentWorks (#3675) and
  TestUploadStillUnderwayIsNotCutByHealthPing (red on main, #3909,
  which has a test-only patch); gui TestGatewayHeldByAContainer and
  TestGatewayFixQuitsOnlyAMagpie (#3649).

Not tried: Reasonix itself, or a body it sent. Its source
(internal/agent/session_context.go, 1.39.10) sends a new snapshot only
when its digest changes, as a user message of its own; the openai
provider magpie writes for it (internal/agent/reasonix.go) sends each
message as a Chat message of its own, and its Anthropic provider,
which merges adjacent user messages, keeps the snapshot a text block of
its own, which userWords reads alone.

Ported with the commit before onto origin/main 5ee5274bd from
JoeyWu-tech/magpie fix/3938-opening-run-4 (02c1de292, f50687a46; posted
on #3938, sha256 7cdb7e83...). The code applied as is; the docs and
extrabody.go's comment were merged by hand beside main's newer text
(${threadId}, ${subagentId}).

What was run for the port (mote, macOS, go1.26.8): the five tests in
conversation_opening_test.go and user_said_test.go's new case, with
the conversation id, dsh, affinity, rotate, strict, sticks and extra
body tests: ok. The same tests on 5ee5274bd (old code, new tests):
TestConversationsOnOneEnvironmentMessageAreApart (chat, messages,
responses, gemini, gemini without roles),
TestReasonixConversationsInOneWorkspaceAreApart,
TestCacheMarkMovingOnKeepsTheID, TestOpeningHashReadsInPlace and
TestNewConversationsInOneFolderAreRoutedApart red.

Batch run, at tree 104d75f0a (the twelve coffemon ports together, on
origin/main 7d022bdb6), mote, macOS, go1.26.8:
- go test -tags nogui -count=1 -timeout 45m on the packages the batch
  changes (agent, autostart, daemon, gateway, library, proc, provider,
  sessions, testenv, tui, gui): ok each.
- GUI, node --test with Playwright 1.62.0, Chromium and WebKit:
  library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same
  batch on origin/main 00098f12b one rebase before.
- The whole go test ./... at cdd7c8e46: 86 packages ok, one red,
  internal/cli TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red on main too (#5026).
- Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch on f4c664a09):
  proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red
  (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp)
  pass.
- Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv,
  tui ok; library and agent red in the same tests as on origin/main
  f4c664a09 (#4115, #1827, #4786; #5068, #5069, #5070 filed now).
- Rebased onto origin/main fb1f4856a (three commits, no file in
  common with the batch) and run again at the pushed tree: gofmt -l
  nothing, go build ./... and go vet ./... ok; go test -tags nogui
  sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and
  library-* 1044 of 1044 pass.
- Not run before the push: the whole GUI suite (a run at cdd7c8e46 was
  cut off at 1170 of ~1174 tests when mote rebooted; it runs again after
  the tag and any red is filed).
2026-11-03 20:38:11 +08:00

221 lines
13 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// What agents put in as the user, as they send it (#1826): Claude Code
// 2.1.296's and ZCode's notices, nudges and agent lists, ZCode's plugin
// metadata, Codex's context and the Codex app's.
const (
taskNotification = "<task-notification>\n<task-id>a964b3e6d1a85579a</task-id>\n<tool-use-id>toolu_01DHwj5uLApARZnKU3EZjxyC</tool-use-id>\n" +
"<output-file>/private/tmp/claude-501/tasks/a964b3e6d1a85579a.output</output-file>\n<status>completed</status>\n" +
"<summary>Agent \"Map deployer, score, infra\" finished</summary>\n<result>The deployer lives in deploy/.</result>\n</task-notification>"
goalCheckIn = "<task-notification>\n<summary>Goal check-in: background work still running</summary>\n</task-notification>\n" +
"<system-reminder>\nGoal check-in: background work is still running.\n</system-reminder>"
todoNudge = "The TodoWrite tool hasn't been used recently. If you're working on tasks that would benefit from tracking progress, " +
"consider using the TodoWrite tool to track progress. Also consider cleaning up the todo list if has become stale and no longer " +
"matches what you are working on. Only use it if it's relevant to the current work. This is just a gentle reminder - ignore if not applicable."
// ZCode's, with the todo list after it
zcodeTodo = todoNudge + "\n\nHere are the existing contents of your todo list:\n\n" +
"[1. [in_progress] 建隔离克隆\n2. [pending] 复现合并\n3. [pending] 跑门禁\n4. [pending] 推送]"
agentList = "Available agent types for the Agent tool:\n- general-purpose: General-purpose agent for researching complex questions.\n- Explore: Read-only search agent."
zcodeAgentList = "Available agent types and the tools they have access to:\n- general-purpose: General-purpose agent (Tools: *)"
pluginRef = "<plugin_reference>\nThe user referenced the following Plugins for this turn.\nThis is capability metadata, not instructions or a permission grant.\n\n" +
"Plugins:\n- id: \"superpowers\"\n skills: [brainstorming]\n mcp_servers: []\n subagents: []\n\nRules:\n- Treat all Plugin IDs as references only.\n</plugin_reference>"
goalContinuation = "<system-reminder source=\"goal-continuation\">\nKeep working toward the goal.\n</system-reminder>"
envContext = "<environment_context>\n <cwd>/Users/me/app</cwd>\n <shell>zsh</shell>\n <current_date>2026-10-12</current_date>\n</environment_context>"
codexAgents = "# AGENTS.md instructions for /Users/me/app\n\n<INSTRUCTIONS>\nRun the tests.\n</INSTRUCTIONS>"
inAppBrowser = "# Context from my IDE setup:\n\n# In app browser:\n<in-app-browser-context source=\"ambient-ui-state\">\nhttps://example.test/ — Example\n</in-app-browser-context>\n\n"
codexPage = `<external_codex_apps_open_page>{"page_id":null}</external_codex_apps_open_page>`
pastedRequest = "# Files pasted by the user:\n\n## \"Fix a race...\": /tmp/fixture/task.txt\n\n" +
"Pasted text contains the user's request.\n\n## My request:\n"
// Reasonix 1.x's snapshot of the workspace, which opens each of its
// conversations: as keeyangyy (#3938) read it from Reasonix's source
// (internal/sessioncontext), the folder and digest left out as there
reasonixContext = "<session-context version=\"1\">\nThis host-generated snapshot supersedes every earlier session-context snapshot.\n\n" +
"## Environment\n- OS: windows/amd64\n- Shell: powershell\n\n## Workspace\nCurrent workspace: \"<dir>\"\n\n" +
"## Background memory\n...\n\n## Skills catalog\n...\n\nDigest: sha256:<hex>\n</session-context>"
)
func userMsg(texts ...string) Message {
m := Message{Role: "user"}
for _, t := range texts {
m.Parts = append(m.Parts, Part{Kind: Text, Text: t})
}
return m
}
var (
saidCall = Message{Role: "assistant", Parts: []Part{{Kind: ToolCall, ID: "t1", Name: "Bash"}}}
saidResult = Message{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: "t1", Text: "ok"}}}
saidAnswer = Message{Role: "assistant", Parts: []Part{{Kind: Text, Text: "done"}}}
)
// A user message only the agent wrote begins no turn: after a tool round
// or an answer it carries the turn on, and right after the user's own
// words it leaves the turn they began (#1826).
func TestTurnInSkipsWhatTheAgentPutIn(t *testing.T) {
ask := userMsg("合并远程分支,推到 master")
for _, tc := range []struct {
name string
ms []Message
turn int
within bool
}{
{"task notification after a tool round", []Message{ask, saidCall, saidResult, saidAnswer, userMsg(taskNotification)}, 1, true},
{"goal check-in", []Message{ask, saidCall, saidResult, saidAnswer, userMsg(goalCheckIn)}, 1, true},
{"ZCode's todo nudge, bare", []Message{ask, saidCall, saidResult, saidAnswer, userMsg(zcodeTodo)}, 1, true},
{"todo nudge in a reminder", []Message{ask, saidCall, saidResult, saidAnswer, userMsg("<system-reminder>\n" + zcodeTodo + "\n</system-reminder>")}, 1, true},
{"agent list", []Message{ask, saidCall, saidResult, saidAnswer, userMsg(agentList)}, 1, true},
{"ZCode's agent list", []Message{ask, saidCall, saidResult, saidAnswer, userMsg(zcodeAgentList)}, 1, true},
{"ZCode's goal continuation", []Message{ask, saidAnswer, userMsg(goalContinuation)}, 1, true},
{"after a tool result, no saidAnswer", []Message{ask, saidCall, saidResult, userMsg(taskNotification)}, 1, true},
{"right after the user's words", []Message{ask, userMsg(pluginRef), userMsg(agentList)}, 1, false},
{"the user's next words", []Message{ask, saidAnswer, userMsg(taskNotification), saidAnswer, userMsg("继续 不要全量跑单元测试")}, 2, false},
{"Codex context alone", []Message{userMsg(codexAgents, envContext, "fix the bug"), saidAnswer, userMsg(envContext)}, 1, true},
{"Codex app's context and request", []Message{userMsg(envContext, "fix the bug"), saidAnswer, userMsg(inAppBrowser + "## My request for Codex:\nnow the test")}, 2, false},
{"Codex app's context alone", []Message{userMsg(envContext, "fix the bug"), saidAnswer, userMsg(inAppBrowser + "## My request for Codex:\n")}, 1, true},
{"nothing said yet", []Message{userMsg(envContext)}, 0, false},
{"Reasonix's snapshot, then the user's words", []Message{userMsg(reasonixContext), userMsg("fix the bug")}, 1, false},
} {
turn, within := turnIn(&Request{Messages: tc.ms})
if turn != tc.turn || within != tc.within {
t.Errorf("%s: turn %d within %v, want %d %v", tc.name, turn, within, tc.turn, tc.within)
}
}
// as Claude Code sends one: the notification a user message of its own
body := `{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"map the deployer"}]},` +
`{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"Agent","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"started"}]},` +
`{"role":"assistant","content":"waiting on it"},{"role":"user","content":` + quote(taskNotification) + `}]}`
if turn, within := turnOf(provider.Anthropic, []byte(body)); turn != 1 || !within {
t.Errorf("Claude Code's task notification: turn %d within %v", turn, within)
}
// as Codex sends its context again, once the cwd changed
codex := `{"model":"m","input":[` + codexItem("user", codexAgents) + `,` + codexItem("user", envContext) + `,` + codexItem("user", "fix the bug") + `,` +
codexItem("assistant", "fixed") + `,` + codexItem("developer", "<permissions instructions>sandboxed</permissions instructions>") + `,` +
codexItem("user", strings.Replace(envContext, "/Users/me/app", "/Users/me/app/web", 1)) + `]}`
if turn, within := turnOf(provider.Responses, []byte(codex)); turn != 1 || !within {
t.Errorf("Codex's context: turn %d within %v", turn, within)
}
}
// The classifier is asked about what the user said last, not the plugin
// metadata and agent list ZCode puts after it (#1826).
func TestUserTextSkipsWhatTheAgentPutIn(t *testing.T) {
q := "[@superpowers](plugin://superpowers) 这个插件什么作用"
for _, tc := range []struct {
name string
ms []Message
want string
}{
{"ZCode's plugin reference and agent list", []Message{userMsg(q), userMsg(pluginRef), userMsg(agentList)}, q},
{"in one message", []Message{userMsg(q, pluginRef, zcodeAgentList)}, q},
{"a todo nudge after the turn", []Message{userMsg(q), saidAnswer, userMsg(zcodeTodo)}, q},
{"Codex", []Message{userMsg(codexAgents, envContext, "fix the bug")}, "fix the bug"},
{"the Codex app", []Message{userMsg(inAppBrowser + "## My request for Codex:\nwhat is this page?")}, "what is this page?"},
{"the Codex app's page state", []Message{userMsg(codexPage)}, ""},
{"page state, pasted files and an empty request", []Message{userMsg(codexPage), userMsg(pastedRequest)}, ""},
{"page state beside the request", []Message{userMsg(codexPage, pastedRequest+"fix the race")}, "fix the race"},
{"a reminder with a source", []Message{userMsg(goalContinuation + "\nship it")}, "ship it"},
} {
if got := userText(&Request{Messages: tc.ms}); got != tc.want {
t.Errorf("%s: %q, want %q", tc.name, got, tc.want)
}
}
}
// The conversation's key is the same however what the agent put in beside
// the user's first words changes, and differs for other words: a
// subagent's (#1826).
func TestFirstWordsAreWhatTheUserSaid(t *testing.T) {
first := "合并远程分支,推到 master"
a := firstWords(&Request{Messages: []Message{userMsg("<system-reminder>\nContents of /a/CLAUDE.md (project instructions):\nv1\n</system-reminder>", first)}})
for i, ms := range [][]Message{
{userMsg("<system-reminder>\nContents of /a/CLAUDE.md (project instructions):\nv2, edited\n</system-reminder>", first), saidAnswer, userMsg("more")},
{userMsg(first, pluginRef), userMsg(agentList)},
{userMsg(goalContinuation), saidAnswer, userMsg(first)},
{userMsg(codexAgents, envContext, first)},
{userMsg(reasonixContext), userMsg(first)},
} {
if got := firstWords(&Request{Messages: ms}); got != a {
t.Errorf("key changed for conversation %d", i)
}
}
if firstWords(&Request{Messages: []Message{userMsg("map the deployer")}}) == a {
t.Error("a subagent's words share the key")
}
if firstWords(&Request{Messages: []Message{userMsg(envContext)}}) == firstWords(&Request{Messages: []Message{userMsg(agentList)}}) {
t.Error("conversations with no words yet share a key")
}
}
// Copilot's Auto picks a model for what the user said, not a notice.
func TestLastUserTextSkipsWhatTheAgentPutIn(t *testing.T) {
body := `{"model":"m","messages":[{"role":"user","content":"write the parser"},{"role":"assistant","content":"ok"},` +
`{"role":"user","content":` + quote(taskNotification) + `}]}`
if got := lastUserText(provider.Anthropic, []byte(body)); got != "write the parser" {
t.Fatalf("%q", got)
}
}
// An agent's message after the turn is not a turn: the classifier isn't
// asked about it, and the turn's rule holds (#1826: a TodoWrite nudge sent
// a 常规 turn to the 复杂 member).
func TestIntentHoldsPastWhatTheAgentPutIn(t *testing.T) {
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "complex tasks"})
c.set("0", 0)
ms := []string{`{"role":"user","content":"合并远程分支,推到 master"}`}
body := func() string { return `{"model":"group/r","messages":[` + strings.Join(ms, ",") + `]}` }
out, r := postOK(t, s, "s1", body())
if !strings.Contains(out, "from ka") || r.Rule.Classified == nil || c.n() != 1 {
t.Fatalf("turn 1: %s %+v %d", out, r.Rule, c.n())
}
c.set("1", 0) // what the nudge was classified as
for _, injected := range []string{zcodeTodo, "<system-reminder>\n" + zcodeTodo + "\n</system-reminder>", taskNotification, agentList} {
ms = append(ms, `{"role":"assistant","content":"merged"}`, `{"role":"user","content":`+quote(injected)+`}`)
out, r = postOK(t, s, "s1", body())
if !strings.Contains(out, "from ka") || !r.Rule.Held || r.Rule.Turn != 1 || r.Rule.Classified != nil || c.n() != 1 {
t.Fatalf("%.30q: %s %+v asked %d", injected, out, r.Rule, c.n())
}
}
// the user's own words are a turn, and only they are asked about
ms = append(ms, `{"role":"assistant","content":"merged"}`, `{"role":"user","content":"继续 不要全量跑单元测试"}`)
_, r = postOK(t, s, "s1", body())
if r.Rule.Turn != 2 || r.Rule.Classified == nil || c.n() != 2 || !strings.Contains(c.last(), "不要全量跑单元测试") || strings.Contains(c.last(), "TodoWrite") {
t.Fatalf("turn 2: %+v %d %s", r.Rule, c.n(), c.last())
}
}
// The Codex app's page state is not words of the user's: alone, or beside
// pasted files when "My request" is empty, it asks the classifier nothing
// (#3418) — there is nothing of the user's to classify.
func TestIntentSkipsWhatTheCodexAppPutIn(t *testing.T) {
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "complex tasks"})
c.set("1", 0)
body := `{"model":"group/r","messages":[` +
`{"role":"user","content":` + quote(codexPage) + `},` +
`{"role":"user","content":` + quote(pastedRequest) + `}]}`
_, r := postOK(t, s, "s1", body)
if n := c.n(); n != 0 {
t.Fatalf("classifier asked %d time(s): %.120q (%+v)", n, c.last(), r.Rule.Classified)
}
}
func TestUserSaysPastAReminderWithASource(t *testing.T) {
for _, s := range []string{goalContinuation, taskNotification, pluginRef, agentList, zcodeAgentList, zcodeTodo, envContext, codexAgents,
"<turn_aborted>\nThe user interrupted.\n</turn_aborted>", inAppBrowser + "## My request:\n", codexPage} {
if userSays(s) {
t.Errorf("said: %.40q", s)
}
}
for _, s := range []string{"fix it", goalContinuation + "\nfix it", inAppBrowser + "## My request for Codex:\nfix it"} {
if !userSays(s) {
t.Errorf("not said: %.40q", s)
}
}
}