From 8aae124b0257a2b9c22085f9c7171574281ec48b Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 11:32:19 +0800 Subject: [PATCH] gateway: Claude Code's tool-list notices after a result are not the user's message, so a steer sent with them reaches the model (magicalaciw18 on X) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude Code's deferred-tools and MCP-server notices ("The following deferred tools are now available via ToolSearch…", "…are no longer available…", "# MCP Server Instructions", "The following MCP servers have disconnected / failed to connect / are still connecting…", literal strings from its own transcripts) were not in `injected`, so agentNotice passed them to the run as the user's words. The model read a new "user message" that was only the notice (and a steer sent beside it arrived with the notice glued on its front). Run on the Windows box (xiaomi-gpu, win-run; mote and WSL refused every connection), at the pushed head (rebased on 914fcf59b): - Old prompt.go: TestClaudeToolListNoticesAreNotTheUsersMessage (7 subtests) and TestClaudeSteerBesideToolListNoticeGoesOnAlone red; the lone-steer tests pass on old and new code (they guard that a steer alone still goes through, as a text after the result and as 2.1.296's system message). - Fix: the new tests, TestClaudeHook*, TestAgentNotice*, TestUserWords green. - go vet ./internal/gateway on Windows: clean. gofmt clean. - -run 'Prompt|Classify|Turn|UserWords|Reminder|Notice|Claude' on the package: only TestClaudeAsRootEnv red, a known Windows red (#2425), untouched here. Not run: mote (down), darwin/linux vet, the full suite, a real Claude Code. The first quote of the report (a steer read as command output) is #1878, fixed in v0.1.1220; not reproduced here. --- internal/gateway/claude_tools_notice_test.go | 63 ++++++++++++++++++++ internal/gateway/prompt.go | 3 + 2 files changed, 66 insertions(+) create mode 100644 internal/gateway/claude_tools_notice_test.go diff --git a/internal/gateway/claude_tools_notice_test.go b/internal/gateway/claude_tools_notice_test.go new file mode 100644 index 000000000..31a740dab --- /dev/null +++ b/internal/gateway/claude_tools_notice_test.go @@ -0,0 +1,63 @@ +package gateway + +import "testing" + +// Claude Code's notices that the tool list changed, after a tool's result: +// the deferred tools now available, or no longer, and an MCP server's +// instructions. They are the agent's. Told to the run as the user's message +// the model read "The user sent a new message" of nothing it was asked, and +// a steer sent with them was lost among them (magicalaciw18 on X). +func TestClaudeToolListNoticesAreNotTheUsersMessage(t *testing.T) { + for name, text := range map[string]string{ + // the words of Claude Code 2.1.x, as its transcripts hold them + "deferred": `\nThe following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail:\nmcp__a__b\n`, + "gone": `\nThe following deferred tools are no longer available (their MCP server disconnected). Do not search for them — ToolSearch will return no match:\nmcp__a__b\n`, + "gone-quiet": `\nThe following deferred tools are no longer available in this session. Do not search for them — ToolSearch will return no match:\nmcp__a__b\n`, + "mcp": `# MCP Server Instructions\n\nThe following MCP servers have provided instructions for how to use their tools and resources:\n\n## a\nuse it`, + "failed": `\nThe following MCP servers are configured but failed to connect — their tools (typically named mcp____*) are unavailable:\n- a\n`, + "connecting": `\nThe following MCP servers are still connecting — their tools (typically named mcp____*) are not yet available but will be:\n- a\n`, + "disconnect": `\nThe following MCP servers have disconnected. Their instructions above no longer apply:\n- a\n`, + } { + t.Run(name, func(t *testing.T) { + // the notice beside the result, in the results' message + body := `{"model":"claude-sonnet-5","max_tokens":64,"messages":[` + + `{"role":"assistant","content":[{"type":"tool_use","id":"call-1","name":"Read","input":{"file_path":"a.go"}}]},` + + `{"role":"user","content":[{"type":"tool_result","tool_use_id":"call-1","content":"package a"},{"type":"text","text":"` + text + `"}]}]}` + _, said, _ := continuing(t, body) + if len(said) != 0 { + t.Fatalf("Claude Code was written %q as the user's", said) + } + }) + } +} + +// A steer sent among such a notice is the user's, alone. +func TestClaudeSteerBesideToolListNoticeGoesOnAlone(t *testing.T) { + notice := `\nThe following deferred tools are now available via ToolSearch. Their schemas are NOT loaded:\nmcp__a__b\n` + body := `{"model":"claude-sonnet-5","max_tokens":64,"messages":[` + + `{"role":"assistant","content":[{"type":"tool_use","id":"call-1","name":"Read","input":{"file_path":"a.go"}}]},` + + `{"role":"user","content":[{"type":"tool_result","tool_use_id":"call-1","content":"package a"},{"type":"text","text":"` + notice + `"},{"type":"text","text":"像心理学家一样剖析我"}]}]}` + _, said, _ := continuing(t, body) + if len(said) != 1 || said[0] != "像心理学家一样剖析我" { + t.Fatalf("Claude Code was written %q, want the steer alone", said) + } +} + +// A steer with nothing beside it still reaches Claude as the user's message: +// as a text after the result, and as Claude Code 2.1.296's system message. +func TestClaudeLoneSteerReachesClaude(t *testing.T) { + head := `{"model":"claude-sonnet-5","max_tokens":64,"messages":[` + + `{"role":"assistant","content":[{"type":"tool_use","id":"call-1","name":"Read","input":{"file_path":"a.go"}}]},` + for name, body := range map[string]string{ + "text": head + `{"role":"user","content":[{"type":"tool_result","tool_use_id":"call-1","content":"package a"},{"type":"text","text":"像心理学家一样剖析我"}]}]}`, + "system": head + `{"role":"user","content":[{"type":"tool_result","tool_use_id":"call-1","content":"package a"}]},` + + `{"role":"system","content":[{"type":"text","text":"The user sent a new message while you were working:\n像心理学家一样剖析我\n\nIMPORTANT: After completing your current task, you MUST address the user's message above."}]}]}`, + } { + t.Run(name, func(t *testing.T) { + _, said, _ := continuing(t, body) + if len(said) != 1 || said[0] != "像心理学家一样剖析我" { + t.Fatalf("Claude Code was written %q, want the steer", said) + } + }) + } +} diff --git a/internal/gateway/prompt.go b/internal/gateway/prompt.go index b4609a8c9..ce01a67a8 100644 --- a/internal/gateway/prompt.go +++ b/internal/gateway/prompt.go @@ -731,6 +731,9 @@ var ( "New agent types are now available for the Agent tool:", "The TodoWrite tool hasn't been used recently.", "The task tools haven't been used recently.", "Conversation rewind applied.", "Workspace rewind applied.", + // the tool list changed: Claude Code's deferred tools and MCP + // servers' instructions, told after a result + "The following deferred tools are", "# MCP Server Instructions", "The following MCP servers", } )