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", } )