magpie icon indicating copy to clipboard operation
magpie copied to clipboard

Claude subscription bridge: a named tool_choice (forced first call) leaves the run without the client's other tools for the rest of the turn

Open Momentum96 opened this issue 1 week ago • 5 comments

magpie version

0.1.4777 (also checked: v0.1.4777 source tag; the code is the same at main 83f061e)

Where

Gateway only

OS

macOS 27 (the code is platform independent)

Agent, provider and model

An OpenAI Chat client with its own tools → Claude subscription (Claude Code bridge) → claude-opus-5-5

Steps to reproduce

An agent that forces a todo call on its first request and then uses its other tools in the same turn:

  1. POST /v1/chat/completions, model claude/claude-opus-5-5, tools [todo, bash], tool_choice: {"type":"function","function":{"name":"todo"}}, user: "First call the todo tool with op init and items ["a"]. Then, in the same turn, run pwd with the bash tool."
  2. The reply is a todo tool call. Send request 2: the same messages plus the assistant tool call and a tool result "todo ok", the same two tools, no tool_choice.
  3. Request 2 continues the same bridged Claude Code run (found by tool-call id).

What happened, and what you expected

Happened: in step 3 the model has no bash tool. It answers that it has none, and asked for its tools it lists only EndConversation and mcp__magpie__todo. Without the tool_choice on request 1, the same sequence calls todo and then bash (so this is not a model refusal). Expected: after the forced first call the client's other tools are callable, as they are with tool_choice unset or none.

Cause (v0.1.4777, internal/gateway/claude_subscription.go):

  • bridgeTools (L8436) skips every tool but the named one when req.ToolChoice starts with name: (L8439), so the run's MCP helper (tools.json) offers only the forced tool when the run starts.
  • The run's told-set run.tools is built from all of req.Tools, so for request 2 run.offers(req.Tools) (L6271, used at L7763) is true and the path that hands a run the tools it was never told of is skipped. I also forced that path to run (more non-empty, continueWith, L6587); in this configuration the model still did not get the added tools, so a mid-run addition is not a fix.
  • tool_choice: none already avoids this class of problem: bridgeTools keeps every tool (the comment says why: the prompt cache prefix) and allowedTools restricts the turn.

Proposed fix: do the same for a named choice. bridgeTools stops filtering by name:, and allowedTools returns {name: true} for it, so the forced turn may call only the chosen tool (the existing "You must call the X tool" note stays) and the next request, which has no choice, gets allowed == nil again. With this change the sequence above ends with todo then bash in 2 of 2 runs, and the unforced and none cases behave as before. The diff (one existing test in claude_tool_names_test.go asserted the old "only the named tool is offered" behaviour and is updated to the new contract):

patch against v0.1.4777
diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go
index 48f5cc0..a360ca4 100644
--- a/internal/gateway/claude_subscription.go
+++ b/internal/gateway/claude_subscription.go
@@ -8421,13 +8421,18 @@ func relayFailing(w http.ResponseWriter, r *http.Request, from provider.Protocol
 	return 200, col.err
 }
 
-// bridgeTools are the caller's tools as the MCP helper offers them, less
-// those a named tool_choice rules out. A choice of none keeps them all:
+// bridgeTools are the caller's tools as the MCP helper offers them. A
+// named tool_choice keeps them all, as a choice of none does: the run's
+// agent is offered its tools once, as it starts, and a tool left out for
+// the turn a choice forces never reaches it in the turns after, when the
+// client's tool results come back to the same run and it must call them
+// (the todo a client forces first, then its other tools in the same turn).
 // Anthropic caches a prompt as its tools, then its system prompt, then
 // its messages, so a turn told none of them is a prefix the cache has
 // never seen, and was written to it whole, its tools and system prompt
 // too (bbfatx on X: a title or a summary asked with tool_choice none, of
-// a long conversation). The turn may call none of them (allowedTools).
+// a long conversation). The turn may call none of them, or only the one
+// it chose (allowedTools).
 // Each schema is folded to an object root (objectSchema), as a Messages
 // request magpie builds is: Claude Code leaves out an MCP tool whose
 // inputSchema has a oneOf, anyOf or allOf at its root, which Anthropic's
@@ -8436,9 +8441,6 @@ func relayFailing(w http.ResponseWriter, r *http.Request, from provider.Protocol
 func bridgeTools(req *Request) []bridgeTool {
 	tools := make([]bridgeTool, 0, len(req.Tools))
 	for _, t := range req.Tools {
-		if strings.HasPrefix(req.ToolChoice, "name:") && t.Name != strings.TrimPrefix(req.ToolChoice, "name:") {
-			continue
-		}
 		schema := t.Schema
 		if len(schema) == 0 {
 			schema = json.RawMessage(`{"type":"object","properties":{}}`)
@@ -8450,11 +8452,15 @@ func bridgeTools(req *Request) []bridgeTool {
 }
 
 // allowedTools is the client's tools a turn of req may call, nil for all
-// it offers (subscriptionRun.allowed): none when it chose none.
+// it offers (subscriptionRun.allowed): none when it chose none, the one
+// it named when it chose one.
 func allowedTools(req *Request) map[string]bool {
 	if req.ToolChoice == "none" {
 		return map[string]bool{}
 	}
+	if name, ok := strings.CutPrefix(req.ToolChoice, "name:"); ok {
+		return map[string]bool{name: true}
+	}
 	return nil
 }
 
diff --git a/internal/gateway/claude_tool_names_test.go b/internal/gateway/claude_tool_names_test.go
index 652437d..45d9450 100644
--- a/internal/gateway/claude_tool_names_test.go
+++ b/internal/gateway/claude_tool_names_test.go
@@ -142,8 +142,13 @@ done
 	// under the names it was offered them by
 	req := &Request{Tools: []Tool{{Name: "a.b"}, {Name: "a_b"}, {Name: long}}, ToolChoice: "name:a.b"}
 	offer := bridgeTools(req)
-	if len(offer) != 1 || !validRe.MatchString(offer[0].Name) || offer[0].Name == "a_b" {
-		t.Fatalf("tool_choice a.b offers %+v", offer)
+	if len(offer) != 3 || !validRe.MatchString(offer[0].Name) || offer[0].Name == "a_b" {
+		t.Fatalf("tool_choice a.b offers %+v, want all three, a.b under a valid name", offer)
+	}
+	// the run is offered them all, as a client's later calls come back to it,
+	// and the turn the choice forces may call only the one it named
+	if got := allowedTools(req); len(got) != 1 || !got["a.b"] {
+		t.Errorf("tool_choice a.b allows %v, want only a.b", got)
 	}
 	if got, want := toolChoiceAsk(req), "You must call the mcp__magpie__"+offer[0].Name+" tool."; !strings.Contains(got, want) {
 		t.Errorf("tool choice %q, want %q", got, want)

PRs are collaborator-only, so the patch is here instead of a PR. Credit is not needed; use it as you see fit.

Error text or logs

Request 2 reply (text, no tool call): the model says it has no bash tool; the tool list it reports is EndConversation, mcp__magpie__todo.

Momentum96 avatar Nov 25 '26 10:11 Momentum96

Ported your patch (thanks — the diagnosis and the fix were both right). It's on the fork for a collaborator to PR; this account can't open one.

Same shape as your diff: bridgeTools now offers every tool the client sent regardless of a named tool_choice, and the choice's reach is only the turn it forces — toolChoiceAsk says it in words and allowedTools lets that turn call only the named tool (subscriptionRun.allowed, enforced per call). Your test is moved to say the run is offered all three tools under valid names while allowedTools holds only the named one. Credited as Co-authored-by.

Branch: https://github.com/yetone/magpie/compare/main...joei4cm:magpie:fix/7733-named-tool-choice-offers-all (internal/gateway only, bd0ac4ac1 + one commit; the three tool_choice tests pass, vet/gofmt clean).

Two things worth flagging:

  1. TestClaudeToolChoiceNoneKeepsTheTools is red on main already (before this change) — it counts two runs [pid pid] where it wants one: a tool_choice none title request starts a run of its own instead of waiting in the conversation's. A run-lifecycle issue separate from tool filtering, so I left it out of this change; it wants its own fix. Happy to look next if it's unclaimed.

  2. Your len(offer) != 1 → != 3 expectation is right only because allowedTools now gates the turn; with the offer alone a forced first call could still name a tool the client sent. The allowedTools change is what makes offering all three safe.

joei4cm avatar Nov 25 '26 11:11 joei4cm

Independent maintenance check: L0NE-6/magpie-panel now carries the named-tool-choice fix as 88c7e73b. A named tool_choice keeps the client's complete tool list for the run while allowedTools still limits the named tool to that turn. Focused gateway tests and go vet pass. This is a fork integration, not an upstream merge.

L0NE-6 avatar Nov 25 '26 13:11 L0NE-6

@L0NE-6 thanks for the independent maintenance check — good to see the named-tool_choice fix integrated in L0NE-6/magpie-panel as 88c7e73b with the focused gateway tests and go vet passing.

Since this account cannot open a pull request against yetone/magpie (no collaborator access), may I ask a favour: would you be willing to open the upstream PR for this one? Your branch already carries it with tests passing.

Our fix branch is joei4cm/magpie → fix/7733-named-tool-choice-offers-all (2122bad7a): bridgeTools now offers every tool the client sent regardless of a named tool_choice, while the choice's reach stays limited to the turn it forces via toolChoiceAsk / allowedTools (enforced per call through subscriptionRun.allowed). Happy to provide a clean diff or answer review questions. Co-authorship however you prefer.

joei4cm avatar Nov 25 '26 14:11 joei4cm

Maintenance verification: branch L0NE-6/magpie-panel:fix/7733-named-tool-choice at 52d924bf keeps the complete tool list for a run with a named tool_choice, while allowedTools still limits the forced turn to the named tool. The new regression test is red before the change and green after; focused gateway tests and go vet ./internal/gateway pass. Upstream PR creation is currently disabled, so the branch is available for a maintainer to cherry-pick.

L0NE-6 avatar Nov 25 '26 14:11 L0NE-6

@yetone sorry for the direct ping — this one has two independently written and independently verified fix branches ready to pick.

Cherry-pickable branches (named tool_choice no longer drops the client's tool list):

  • joei4cm/magpie:fix/7733-named-tool-choice-offers-all (2122bad7a)
  • L0NE-6/magpie-panel:fix/7733-named-tool-choice (52d924bf)

Both offer every tool the client sent regardless of a named tool_choice, while the choice's reach stays limited to the turn it forces via toolChoiceAsk / allowedTools (enforced per call through subscriptionRun.allowed). Both report a regression test that is red before the change and green after, plus focused internal/gateway tests and go vet ./internal/gateway passing. Our account cannot open an upstream PR (no collaborator access) and L0NE-6 reports upstream PR creation is currently disabled — so either branch is available for you to cherry-pick as-is.

joei4cm avatar Nov 25 '26 14:11 joei4cm