claude bridge: a run's tools are set before its Claude Code starts

TestClaudeBridgeHandsOverAnUnloadedMagpieTool was red on ubuntu CI at
4c6c3a453 (run 43919594403): call-search was handed
"magpie-web-search__web_search", want
"mcp__magpie__magpie-web-search__web_search".

The bridge set run.tools (and magpie's search and answer tool names)
after start returned, while Claude Code was already running and
readOutput was reading its stdout. On a loaded runner the first
tool_use was read with tools still nil, so 1490045f5's hand-over of an
unloaded deferred tool (which needs r.tools set) didn't apply and the
name went out cut. The same window let magpie's own web search call go
to the client unanswered.

The request now carries runTold, which startRun calls before launch;
the caller still calls it after start for a resumed run.

Reproduced: a 50ms sleep before the old assignment, red 3 of 3 with
CI's exact message; with the fix and the same sleep after start, green
3 of 3. Unchanged code, -count=30: green (the race needs load).

Run at this head (mica, base dbbb2f26b): go test ./internal/gateway
-timeout 45m ok (897s); GOOS=linux and GOOS=windows go vet
./internal/gateway ok; gofmt -l clean. Full suite at dbbb2f26b
(the base, without this change): 102 packages ok.
Not tried: a real Claude subscription run.
This commit is contained in:
yetone
2026-11-17 03:13:10 +08:00
parent ead91a9a50
commit 43d9aae09f
2 changed files with 21 additions and 8 deletions
+17 -8
View File
@@ -1395,6 +1395,9 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model,
run.outputs = []*os.File{stdout, stderr}
run.reading.Add(2)
run.room = room
if req.runTold != nil {
req.runTold(run)
}
err = run.launch()
launched = err == nil
_, _ = stdoutW.Close(), stderrW.Close() // Claude Code has its own
@@ -7459,14 +7462,15 @@ func (s *Server) serveSubscriptionAs(w http.ResponseWriter, r *http.Request, fro
}
}
if run == nil {
run, events, err = start(r.Context(), req)
if err == nil {
// its agent is running already: what it was told is set
// under the run's lock, as offers and shelve read it
tools := map[string]bool{}
for _, t := range req.Tools {
tools[t.Name] = true
}
// what its agent is told is set before the agent starts: the
// output reader names a call by it, and Claude Code's first
// tool_use could be read before start returned (a deferred tool
// handed over cut, on a loaded CI box)
tools := map[string]bool{}
for _, t := range req.Tools {
tools[t.Name] = true
}
req.runTold = func(run *subscriptionRun) {
run.mu.Lock()
run.tools = tools
if search.Name != "" {
@@ -7476,6 +7480,11 @@ func (s *Server) serveSubscriptionAs(w http.ResponseWriter, r *http.Request, fro
run.answerName, run.answerSchema = answer.Name, answerSchema
}
run.mu.Unlock()
}
run, events, err = start(r.Context(), req)
if err == nil {
// a run resumed is told this request's too
req.runTold(run)
run.adviseOn(r.Context(), req, s.adviceRound)
}
}
+4
View File
@@ -233,6 +233,10 @@ type Request struct {
// reply made the calls this request answers after another request
// did: its run branches off it there (subscriptionBridge.branch).
branch *savedSession
// runTold, for the Claude bridge, sets what a run started for this
// request is told (its tools, magpie's search and answer tools)
// before its agent starts, so the first call it reads is named by them
runTold func(*subscriptionRun)
// told, for the Claude bridge's interactive mode, is a run's being
// told the conversation whole, not given it as a transcript
// (replaySession), as after Claude Code couldn't go on from one