Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a conversation going round one tool call with the same result is answered by magpie after 8 turns (audie on Discord)

yetonede281760e23f
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index e8c5ce433..658c9bced 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -120,6 +120,7 @@ recorded as a prediction failure (`noPrediction`).
- An error that reaches the agent in a stream already begun can't carry its status, so `streamError` says it in the words (`saysPassing`, #2372, zminds): for a status that may pass (408, 429, 5xx) whose text names none of the words a text-judging agent retries on (Pi decides by its `errorMessage` alone), it appends `(<status>: you can retry your request)`; a request's own fault (400, 401, 404 …) and a text that already says so are left as they were. `TestStreamErrorSaysItPasses` holds Pi's own lists; `TestGroupLastFailureInStreamSaysItPasses` runs a group whose last member fails after the keepalive. A 413 byte cap in that stream says `request_too_large` in the error text, and Chat and Anthropic name that type, so Pi compacts; Gemini keeps its own status shape (`TestStreamedByteCapSaysRequestTooLargeToPi`, `TestStreamErrorByteCapInEveryShape`).
- Claude's, GPT's and Gemini's reasoning is held (`refusesAfterThinking`, up to `holdThinking`, 4 minutes) so that a refusal after it goes to the next candidate unseen (#248). It is held only while one could: on the last candidate a refusal isn't asked again, so their reasoning streams as it comes there, and a refusal after it reaches the agent as the vendor's own. Held there, a lone relay's Claude or GPT showed Claude Code its thinking all at once with the text, and Codex none until then (iTianbao on X). Gemini's streams there too: a reply that only reasoned is asked again in place by `streamTranslated`, the reasoning the agent has kept and the next try's after it (#667), so holding it only kept Codex on a Gemini model with nothing at all, not even `response.created`, for the whole of its thinking (LuckymanJC on Discord; `TestThinkingStreamsWithNoOneElse`'s gemini cases). Any other model's reasoning is never held.
- A streamed reply stuck in a loop is ended, not let run to its output limit (#1359, Moody-Sin: WorkBuddy's DeepSeek v4.1 flash reasoned round "Now." "Writing." "Go." "Let me write." "OK." until max tokens while dsh and Pi waited). `holdWriter.Write` feeds every streamed try, passed through or translated, a key's, a built-in subscription's or a plugin's (a moved WorkBuddy's fetch in the plugin host, `TestLoopEndsWorkBuddysReply`), in each of the agent's protocols, to a `loopGuard` ([`loop_guard.go`](../../internal/gateway/loop_guard.go)). It cuts the reasoning and the text, each apart, into units, its lines and sentences (a list's `1.` or a decimal is no sentence's end), compared without case or a closing stop. A unit with no word in it (a letter run with no digit: not a brace, a rule, `0x00`, a hash or a row of numbers) isn't counted. When the last `loopWindow` (256) counted units of either are among no more than `loopDistinct` (24) different ones, the try is cut (`cutLoop`): what came with that write goes out, then, after a blank line (two newlines when that write stopped inside a line, `holdWriter.atLine`, #2446), the protocol's stream error (`streamError`, 502) naming the loop and quoting its units, and the vendor's request is cancelled. It falls back to nobody and rests nobody (`failLoop`); its usage record's error type is `reply_loop` (`usage.LoopErrType`), and Usage counts them per model (`Totals.Loops`), as "{n} looped" beside the model's failures in the Overview's rows and the Requests ranking (#1858, `TestLoopsCountedPerModel`, `usage-loops.test.cjs`). A reply magpie translates into the agent's protocol (`streamTranslated`) whose *reasoning* loops before it has said a text or a call, and isn't a continuation, is asked again once in place instead (`loopReasks`, through `holdWriter.onLoop`; cdredfox on X, WorkBuddy AI's DeepSeek v4 flash): the agent keeps the reasoning it has, the next try's follows it in the same stream, as an empty reply's re-ask does (#667), and only a loop on that try ends the reply with the error (`TestReasoningLoopIsAskedAgain`). A reply passed through in the agent's own protocol is asked again once too, at the fallback loop (`holdWriter.mayReloop`, `letGoLoop`; #2436, lqisgod: pi's Chat stream to WorkBuddy was cut at once): when nothing of the try has reached the agent yet, in any protocol, its held bytes are dropped; when its reasoning is already passing, only a Chat stream goes on, written up to the end of the tripping event, with the next try's chunks after it. An Anthropic or Responses stream already passing still ends with the error, since a second `message_start` or `response.created` can't follow. Each re-ask, in place or at the fallback loop, counts in the usage record's tries. A loop in the text, or after a text or call, is ended at once (`TestLoopAfterTextIsNotAskedAgain`). A cycle need not be exact. Code, tables, lists, logs, CSV and base64 differ line to line or have no words, and don't trip it (`TestLoopGuardLeavesOrdinaryRepliesAlone` feeds magpie's own sources and docs through it). A reply sent whole (no stream, or Gemini without `alt=sse`) isn't read. Settings › Models › Replies › *Stop looping replies* (`noLoopGuard`) turns it off.
+- A conversation going round one tool call is answered by magpie and sent nowhere (audie on Discord: Codex Desktop on WorkBuddy's deepseek-v4-pro read the same file 308 times in half an hour, and 340 times that evening, 537 credits). `stoppedToolLoop` ([`tool_loop.go`](../../internal/gateway/tool_loop.go)) runs in `serveAgent` before hooks, middleware and routing, and in the Codex backend's passthrough on Codex's own sign-in. It reads the request's own history, so it is per conversation and keeps no state: when the last `toolLoopRepeats` (8) turns are each the same calls (name and arguments, keys in any order, call ids ignored) answered with the same results (with Codex's `Wall time`, `Chunk ID` and `duration_seconds` taken out, `runNoise`) and nothing else from the user, the turn is answered in the agent's own protocol, streamed or whole (`writeLocalText` through `relayStatus`), with a notice naming the call. Any different result, or a word of the user's, starts the count again; the notice is a turn without calls, so the user's next word goes on. A body with fewer than 8 tool results isn't parsed (`mayLoop`), and only the newest 64 items of a long one are (`tailOf`, gjson), ~1ms for a 1.5MB turn (`BenchmarkToolLoopCheck`). The turn is recorded as Codex's title turns are (`answeredHere`): the Routing trace as "Tool loop stopped", and a usage row with provider `magpie`, rejected, its session and an error starting `tool loop:`. A poll that gets the same answer 8 times in a row is stopped too. Settings › Models › Replies › *Stop looping replies* (`noLoopGuard`) turns it off with the reply guard. Verification: `go test -tags nogui ./internal/gateway -run 'TestToolLoop'`.
- A Chat request's earlier turns carry the assistant's thinking back in the spelling its own client used: `reasoning_content` (DeepSeek's, which OpenCode and most clients send), `reasoning` (OpenRouter's) or `reasoning_details` (OpenRouter's structured one). A request translated to a non-Chat upstream reads whichever of the three the turn has, in that order, as the turn's thinking (`messageReasoning`, `reasoningText`, read by `parseChat` in [chat.go](../../internal/gateway/chat.go)); the passthrough has read all three since `mistralThinking` (#494), which was the one place that did. Before that the parse layer read `reasoning_content` alone, so a tool-calling session whose client spelled it another way went on from its second turn without the model's own reasoning.
- The order of the steps a request's accounts go through, how much the accounts' own order (*Make first*) counts under each routing, and how often an allowance is read are described for users in [How magpie picks an account](../reference.md#how-magpie-picks-an-account). Smart compares renewals window by window, biggest first, each truncated to the hour (`weighRouted`), so the order decides only when every window falls in the same hour. A window's renewal is its reset, or, when what it has left runs out sooner or it has no reset, that moment (`Limit.RunsOut`, from `QuotaWindow.RunsOutAt`; `Allowance.Renewal`): the WorkBuddy plugin (0.1.14) sends it on its Credits window as the soonest end of a package with credits left (cdredfox on X). Only Smart reads it; Weekly pace, the 90%/98% tiers and rests go by the window's share and reset as before. A row on the Routing page says the share used (the fullest window's, `Allowance.For`) with when that same window renews (`Allowance.Fullest`, the trace's `usedRenews`), not the biggest window's: a Factory account 32% into its week and 16% into its month reads "32% used · renews in 3 days" while Smart still ranks it by its month (#1778). A route from before `usedRenews` says when only for an account of one window. Affinity keeps a conversation on an account at 90–97%: only `SpentShareOf` (98%, 100% In order) stops it (`affine`), while the 90% tier (`lowShare`) orders only conversations nobody has answered.
- A turn's end is told to the app for the "Task done alert" notification (itieniu on X, `turndone.go`). The trace follows each agent session's turn (`turns`, keyed by agent and session): a request is of the turn unless it has a `Kind` or is a Claude Code subagent's (`agentTurnOf`), and its reply is scanned on the way to the agent for a tool call in any wire format (`toolScan`, a JSON value such as `"type":"tool_use"` or `"finish_reason":"tool_calls"`, never a key or text). The turn is done when no request of it is going, the last reply was whole, under 400 and called no tool, and nothing more came for `turnQuiet` (4s); a session idle past `turnStale` (30m) starts over. Only a turn that ran at least `settings.TurnDoneAlert` seconds is told (`TurnDone`, set by the GUI; nil under `magpie serve`, so nothing is followed). The ChatGPT backend path (`codexBackend`) is followed the same way. `TestTurnDoneSaidAtTheTurnsEnd`, `TestTurnDoneCodexBackend`, `TestToolScan`, `turn-done.test.cjs`.
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index fc974e19c..50b86aaf9 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -235,6 +235,11 @@ func (s *Server) codexBackend(w http.ResponseWriter, r *http.Request) {
var recorded, hooked func()
w, recorded = recordConversation(w, r, provider.Responses, body)
defer recorded()
+ // and the tool loop it may be stuck in spends that sign-in's
+ // allowance as it would a magpie model's
+ if s.stoppedToolLoop(w, r, provider.Responses, body) {
+ return
+ }
// Codex's own sign-in: the request hooks, not the middleware
w, body, hooked = gatewayHooks(w, r, provider.Responses, body)
defer hooked()
diff --git a/internal/gateway/codex_titles.go b/internal/gateway/codex_titles.go
index f412b4161..c7174c431 100644
--- a/internal/gateway/codex_titles.go
+++ b/internal/gateway/codex_titles.go
@@ -386,6 +386,17 @@ func (s *Server) codexTitleOff(w http.ResponseWriter, r *http.Request, body []by
// Routing and Usage views say of it, the request kept there as what it
// was, answered by magpie.
func (s *Server) sentNowhere(w http.ResponseWriter, r *http.Request, body []byte, who, why string, out []any) {
+ s.answeredHere(r, provider.Responses, body, who, why, func() {
+ writeTitleReply(w, fmt.Sprintf("resp_magpie_%d", time.Now().UnixNano()), out, nil)
+ })
+}
+
+// answeredHere records a request magpie answered itself with write,
+// sending it nowhere: the Routing view has it tried on magpie alone, who
+// naming why, and the Recent calls and the usage ledger have it as
+// answered by magpie, rejected, with why as its error, so Usage and the
+// Sessions it belongs to say what became of it.
+func (s *Server) answeredHere(r *http.Request, from provider.Protocol, body []byte, who, why string, write func()) {
start := time.Now()
usage.Saw(agentOf(r))
metadata := requestSessionMetadata(r.Header, body)
@@ -394,13 +405,13 @@ func (s *Server) sentNowhere(w http.ResponseWriter, r *http.Request, body []byte
seat := Weighed{ID: "magpie", Provider: "magpie", Name: "magpie", Who: who, Kind: "provider", Model: model}
tr := s.trace.beginFor(r, Route{Time: start, Agent: agentOf(r), Session: sessionOf(r.Header), ParentSession: titleParentSession(r.Header, metadata, kind),
Kind: kind, Model: model, Provider: "magpie", Order: []Weighed{seat}, Tries: []Try{{ID: seat.ID, Model: model, Start: start}}})
- writeTitleReply(w, fmt.Sprintf("resp_magpie_%d", start.UnixNano()), out, nil)
+ write()
ms := time.Since(start).Milliseconds()
s.trace.update(tr, func(t *Route) {
t.Tries[0].Done, t.Tries[0].Status, t.Tries[0].Millis, t.Tries[0].Error = true, 200, ms, why
t.Done, t.Status, t.Error, t.Millis = true, 200, why, ms
})
- s.record(Call{Route: tr.ID, Time: start, From: provider.Responses, To: provider.Responses, Model: model, Provider: "magpie",
+ s.record(Call{Route: tr.ID, Time: start, From: from, To: from, Model: model, Provider: "magpie",
Agent: agentOf(r), Kind: kind, Status: 200, Millis: ms, Error: why})
appendUsage(r, usage.Record{RouteID: tr.ID, Time: start, Agent: agentOf(r), Provider: "magpie", Model: model, Requested: model,
Millis: ms, Status: 200, Rejected: true, Error: why, Session: sessionOf(r.Header), NativeSession: nativeSessionOf(r.Header),
diff --git a/internal/gateway/middleware.go b/internal/gateway/middleware.go
index 504cd40b5..a8141e1d9 100644
--- a/internal/gateway/middleware.go
+++ b/internal/gateway/middleware.go
@@ -23,6 +23,11 @@ func (s *Server) serveAgent(w http.ResponseWriter, r *http.Request, from provide
withGatewaySession(w, r)
w, recorded := recordConversation(w, r, from, body)
defer recorded()
+ // a conversation going round one tool call is answered here, before
+ // anything of it is asked of a hook, a middleware or a provider
+ if s.stoppedToolLoop(w, r, from, body) {
+ return
+ }
w, body, hooked := gatewayHooks(w, r, from, body)
defer hooked()
agents := gwmw.AgentsWanted(middlewareWanted)
diff --git a/internal/gateway/tool_loop.go b/internal/gateway/tool_loop.go
new file mode 100644
index 000000000..dd9951e63
--- /dev/null
+++ b/internal/gateway/tool_loop.go
@@ -0,0 +1,327 @@
+package gateway
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "hash/fnv"
+ "net/http"
+ "regexp"
+ "slices"
+ "strings"
+ "time"
+
+ "github.com/tidwall/gjson"
+ "github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// A model can also loop across turns: it asks for the same tool call, the
+// agent runs it and sends back the same result, and the model asks for it
+// again. Codex Desktop on WorkBuddy's deepseek-v4-pro read the same code
+// 308 times in half an hour, and 340 times that evening, 537 credits for
+// nothing (audie on Discord). Every turn of such a loop carries the whole
+// conversation, so the loop is in the request itself: its newest turns are
+// the same calls with the same arguments answered with the same results.
+//
+// stoppedToolLoop answers a request whose last toolLoopRepeats turns are
+// all that one exchange here, in the agent's own API, with a notice that
+// ends the agent's turn, and sends it nowhere. A turn that got a different
+// result (a poll whose answer moved, a file that changed) is progress and
+// starts the count again, as does anything the user says: the notice
+// itself is a turn without calls, so the user's next word goes on with
+// the conversation. It is on with Settings' Stop looping replies
+// (settings.NoLoopGuard off), as loopGuard is.
+
+// toolLoopRepeats is how many turns in a row of the same calls answered
+// with the same results stop the next: the ninth is answered here.
+const toolLoopRepeats = 8
+
+// toolLoopWho is who answered such a turn, as the Routing view names it.
+const toolLoopWho = "Tool loop stopped"
+
+// toolResultMarks are what each API's tool result is written with, for a
+// body with fewer than toolLoopRepeats of them to be let by unparsed.
+var toolResultMarks = map[provider.Protocol][][]byte{
+ provider.Responses: {[]byte(`_output"`)}, // function_call_output, custom_tool_call_output, …
+ provider.Chat: {[]byte(`"tool_call_id"`)},
+ provider.Anthropic: {[]byte(`"tool_result"`)},
+ provider.Gemini: {[]byte(`"functionResponse"`), []byte(`"function_response"`)},
+}
+
+// stoppedToolLoop answers r here when its conversation is stuck in a tool
+// loop, and says whether it did.
+func (s *Server) stoppedToolLoop(w http.ResponseWriter, r *http.Request, from provider.Protocol, body []byte) bool {
+ if settings.Load().NoLoopGuard || !mayLoop(from, body) {
+ return false
+ }
+ req, err := parse(from, tailOf(from, body))
+ if err != nil {
+ return false // serve says what is wrong with it
+ }
+ loop, ok := toolLoopOf(req.Messages)
+ if !ok {
+ return false
+ }
+ why := loop.why()
+ s.answeredHere(r, from, body, toolLoopWho, why, func() {
+ writeLocalText(w, from, req, loop.notice())
+ })
+ return true
+}
+
+// mayLoop: body has results enough to hold a loop.
+func mayLoop(from provider.Protocol, body []byte) bool {
+ n := 0
+ for _, m := range toolResultMarks[from] {
+ n += bytes.Count(body, m)
+ }
+ return n >= toolLoopRepeats
+}
+
+// conversationKey is where each API keeps a request's conversation.
+var conversationKey = map[provider.Protocol]string{
+ provider.Responses: "input",
+ provider.Chat: "messages",
+ provider.Anthropic: "messages",
+ provider.Gemini: "contents",
+}
+
+// tailItems is how many of a conversation's newest messages (a Responses
+// input's items) are read for a loop: toolLoopRepeats turns are 16
+// messages, or 56 Responses items with reasoning and three calls a turn.
+// A loop of more calls a turn than fit isn't seen.
+const tailItems = 8 * toolLoopRepeats
+
+// tailOf is body with only the newest tailItems of its conversation, for
+// the guard to parse a turn's end and not the whole of a long one, which
+// took ~30ms and 20MB a turn of 1.5MB; gjson finds the items without
+// decoding them. A body whose conversation isn't such a list is as it came.
+func tailOf(from provider.Protocol, body []byte) []byte {
+ list := gjson.GetBytes(body, conversationKey[from])
+ if !list.IsArray() || list.Index <= 0 {
+ return body
+ }
+ var items []gjson.Result
+ list.ForEach(func(_, v gjson.Result) bool {
+ items = append(items, v)
+ return true
+ })
+ if len(items) <= tailItems {
+ return body
+ }
+ var b bytes.Buffer
+ b.Grow(len(body) - list.Index - len(list.Raw) + items[len(items)-1].Index + len(items[len(items)-1].Raw) - items[len(items)-tailItems].Index + list.Index + 2)
+ b.Write(body[:list.Index])
+ b.WriteByte('[')
+ for i, v := range items[len(items)-tailItems:] {
+ if i > 0 {
+ b.WriteByte(',')
+ }
+ b.WriteString(v.Raw)
+ }
+ b.WriteByte(']')
+ b.Write(body[list.Index+len(list.Raw):])
+ return b.Bytes()
+}
+
+// toolLoop is a conversation's newest exchange and how many turns in a row
+// it was made.
+type toolLoop struct {
+ calls []Part // the turn's calls, for the notice to name
+ turns int
+}
+
+// toolLoopOf is the loop msgs end in, when their last toolLoopRepeats
+// turns are one exchange: the same calls answered with the same results.
+func toolLoopOf(msgs []Message) (toolLoop, bool) {
+ turns := exchanges(msgs)
+ if len(turns) < toolLoopRepeats {
+ return toolLoop{}, false
+ }
+ last := turns[len(turns)-1]
+ n := 0
+ for i := len(turns) - 1; i >= 0 && turns[i].sum == last.sum; i-- {
+ n++
+ }
+ if n < toolLoopRepeats {
+ return toolLoop{}, false
+ }
+ return toolLoop{calls: last.calls, turns: n}, true
+}
+
+// exchange is one turn of tool use: the assistant's calls and the results
+// the agent sent back, summed without their ids, which differ every turn.
+type exchange struct {
+ sum uint64
+ calls []Part
+}
+
+// exchanges are the turns of tool use msgs end in, oldest first, back to
+// the newest turn that wasn't one: an assistant turn with no call, or a
+// user turn with no result. Consecutive messages of one role are one turn
+// (a Responses reasoning item and its call, a call's several outputs).
+func exchanges(msgs []Message) []exchange {
+ type run struct {
+ role string
+ parts []Part
+ }
+ var runs []run
+ for _, m := range msgs {
+ if n := len(runs); n > 0 && runs[n-1].role == m.Role {
+ runs[n-1].parts = append(runs[n-1].parts, m.Parts...)
+ continue
+ }
+ runs = append(runs, run{m.Role, append([]Part(nil), m.Parts...)})
+ }
+ var out []exchange
+ for i := len(runs) - 1; i >= 1; i -= 2 {
+ said, asked := runs[i], runs[i-1]
+ if said.role != "user" || asked.role != "assistant" {
+ break
+ }
+ e, ok := exchangeOf(asked.parts, said.parts)
+ if !ok {
+ break
+ }
+ out = append(out, e)
+ }
+ slices.Reverse(out)
+ return out
+}
+
+// exchangeOf sums one turn: each call by its name and arguments, with the
+// result the agent gave it, and anything else the user's side said beside
+// the results (a reminder an agent adds to each, a word of the user's).
+func exchangeOf(asked, said []Part) (exchange, bool) {
+ var calls []Part
+ for _, p := range asked {
+ if p.Kind == ToolCall {
+ calls = append(calls, p)
+ }
+ }
+ results := map[string][]string{}
+ var rest []string
+ hasResult := false
+ for _, p := range said {
+ switch p.Kind {
+ case ToolResult:
+ hasResult = true
+ results[p.CallID] = append(results[p.CallID], resultSum(p))
+ case Text:
+ rest = append(rest, "text\x00"+p.Text)
+ default:
+ rest = append(rest, string(p.Kind)+"\x00"+p.Text+p.Data+p.URL)
+ }
+ }
+ if len(calls) == 0 || !hasResult {
+ return exchange{}, false
+ }
+ keys := make([]string, 0, len(calls)+1)
+ for _, c := range calls {
+ got := results[c.ID]
+ delete(results, c.ID)
+ keys = append(keys, c.Name+"\x00"+steadyArgs(c.Args)+"\x00"+strings.Join(got, "\x01"))
+ }
+ for _, got := range results { // results of no call here
+ keys = append(keys, "\x00\x00"+strings.Join(got, "\x01"))
+ }
+ slices.Sort(keys)
+ h := fnv.New64a()
+ for _, k := range keys {
+ h.Write([]byte(k))
+ h.Write([]byte{0xff})
+ }
+ for _, k := range rest {
+ h.Write([]byte(k))
+ h.Write([]byte{0xfe})
+ }
+ return exchange{sum: h.Sum64(), calls: calls}, true
+}
+
+// steadyArgs is a call's arguments with their keys in one order, as two
+// turns asking the same may write them differently.
+func steadyArgs(raw json.RawMessage) string {
+ var v any
+ if json.Unmarshal(raw, &v) != nil {
+ return string(raw)
+ }
+ b, err := json.Marshal(v)
+ if err != nil {
+ return string(raw)
+ }
+ return string(b)
+}
+
+// resultSum is a result as told apart from another: its text without what
+// an agent writes of the run itself rather than of what it found, and its
+// images.
+func resultSum(p Part) string {
+ var b strings.Builder
+ if p.IsError {
+ b.WriteString("error\x00")
+ }
+ b.WriteString(steadyResult(p.Text))
+ for _, im := range p.Images {
+ b.WriteString("\x00" + im.MediaType + im.Data + im.URL)
+ }
+ return b.String()
+}
+
+// runNoise is what an agent's result says of the run, which differs every
+// time the same command is run: Codex's wall time ("Wall time: 0.0246
+// seconds", its exec's "Wall time 0.1 seconds"), its unified exec's chunk
+// id, and its shell's duration_seconds.
+var runNoise = []struct {
+ mark string // in a result the expression may change
+ re *regexp.Regexp
+ with string
+}{
+ {"Wall time", regexp.MustCompile(`(?m)^(Wall time:? )[0-9.]+( seconds?)`), "${1}#${2}"},
+ {"Chunk ID: ", regexp.MustCompile(`(?m)^(Chunk ID: )\S+`), "${1}#"},
+ {`"duration_seconds"`, regexp.MustCompile(`("duration_seconds"\s*:\s*)[0-9.eE+-]+`), "${1}#"},
+}
+
+func steadyResult(s string) string {
+ for _, n := range runNoise {
+ if strings.Contains(s, n.mark) {
+ s = n.re.ReplaceAllString(s, n.with)
+ }
+ }
+ return s
+}
+
+// called is a turn's calls as the notice names them, each cut short.
+func (l toolLoop) called() string {
+ names := make([]string, 0, len(l.calls))
+ for _, c := range l.calls {
+ args := steadyArgs(c.Args)
+ if r := []rune(args); len(r) > 120 {
+ args = string(r[:120]) + "…"
+ }
+ names = append(names, c.Name+" "+args)
+ }
+ return strings.Join(names, "; ")
+}
+
+// why is what the Routing, Usage and Sessions views say of the turn.
+func (l toolLoop) why() string {
+ return fmt.Sprintf("tool loop: the same tool call got the same result %d turns in a row (%s); answered by magpie, sent nowhere", l.turns, l.called())
+}
+
+// notice is what the agent shows the user, as the turn's answer.
+func (l toolLoop) notice() string {
+ return fmt.Sprintf("magpie stopped this turn before it reached the model: the agent ran the same tool call %d times in a row and got the same result every time (%s). The model is stuck repeating it, and each further turn would spend your quota with no progress, so this turn was sent nowhere. Tell the agent how to go on and the conversation continues; Settings › Models › Replies › Stop looping replies turns this check off.",
+ l.turns, l.called())
+}
+
+// writeLocalText answers req with text in from's API, as a whole reply or
+// a stream as it asked: a finished assistant turn with no call in it.
+func writeLocalText(w http.ResponseWriter, from provider.Protocol, req *Request, text string) {
+ events := make(chan Event, 4)
+ events <- Event{Kind: KStart, MsgID: fmt.Sprintf("msg_magpie_%d", time.Now().UnixNano()), Model: req.Model}
+ events <- Event{Kind: KText, Text: text}
+ events <- Event{Kind: KStop, Stop: "stop"}
+ close(events)
+ relayStatus(w, from, "magpie", req, events, &Usage{}, func() {})
+}
diff --git a/internal/gateway/tool_loop_test.go b/internal/gateway/tool_loop_test.go
new file mode 100644
index 000000000..34654e8a2
--- /dev/null
+++ b/internal/gateway/tool_loop_test.go
@@ -0,0 +1,331 @@
+package gateway
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/settings"
+ "github.com/yetone/magpie/internal/usage"
+)
+
+// countingVendor is a Chat vendor that answers every request with a word,
+// streamed or whole as asked, and counts the requests it got.
+type countingVendor struct{ asked atomic.Int64 }
+
+func (v *countingVendor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ body, _ := io.ReadAll(r.Body)
+ v.asked.Add(1)
+ if strings.Contains(string(body), `"stream":true`) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, `data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4-pro","choices":[{"index":0,"delta":{"role":"assistant","content":"read it"}}]}`+"\n\n")
+ io.WriteString(w, `data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4-pro","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":2,"total_tokens":12}}`+"\n\n")
+ io.WriteString(w, "data: [DONE]\n\n")
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ io.WriteString(w, `{"id":"c1","object":"chat.completion","model":"deepseek-v4-pro","choices":[{"index":0,"message":{"role":"assistant","content":"read it"},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":2,"total_tokens":12}}`)
+}
+
+// toolLoopOn is a gateway whose provider wb serves deepseek-v4-pro from v.
+func toolLoopOn(t *testing.T, v *countingVendor) string {
+ t.Helper()
+ fresh(t)
+ vendor := httptest.NewServer(v)
+ t.Cleanup(vendor.Close)
+ if err := provider.Save(provider.Provider{ID: "wb", Name: "WorkBuddy", Key: "k", Chat: vendor.URL + "/v1", Models: []string{"deepseek-v4-pro"}}); err != nil {
+ t.Fatal(err)
+ }
+ gw := httptest.NewServer(New().Handler())
+ t.Cleanup(gw.Close)
+ return gw.URL
+}
+
+// codexRead is the reporter's turn: Codex Desktop's exec_command reading
+// the same code, its output's wall time and chunk id differing each run.
+func codexRead(i int) (args, result string) {
+ return `{"cmd":"sed -n 1,200p src/parser.rs","workdir":"/Users/audie/code/app"}`,
+ fmt.Sprintf("Chunk ID: %06x\nWall time: 0.0%d1 seconds\nProcess exited with code 0\nOriginal token count: 41\nOutput:\npub fn parse(s: &str) -> Ast {\n todo!()\n}\n", 0xa1b2c3+i, i%9)
+}
+
+// pollJob is a poll whose answer moves: the same call, a new result.
+func pollJob(i int) (args, result string) {
+ return `{"cmd":"gh run view 123 --json status"}`, fmt.Sprintf(`{"status":"in_progress","steps_done":%d}`, i)
+}
+
+type exchangeOfTurn func(i int) (args, result string)
+
+// toolTurns is a request of from's API holding n turns of tool use made
+// by turn, after the user's ask, then tail (raw messages of that API).
+func toolTurns(from provider.Protocol, n int, turn exchangeOfTurn, stream bool, tail ...string) string {
+ var items []string
+ switch from {
+ case provider.Responses:
+ items = append(items, `{"type":"message","role":"user","content":[{"type":"input_text","text":"why does parse panic?"}]}`)
+ for i := range n {
+ args, result := turn(i)
+ items = append(items,
+ fmt.Sprintf(`{"type":"reasoning","summary":[{"type":"summary_text","text":"Reading parser %d"}]}`, i),
+ fmt.Sprintf(`{"type":"function_call","name":"exec_command","arguments":%q,"call_id":"call_%d"}`, args, i),
+ fmt.Sprintf(`{"type":"function_call_output","call_id":"call_%d","output":%q}`, i, result))
+ }
+ items = append(items, tail...)
+ return fmt.Sprintf(`{"model":"wb/deepseek-v4-pro","stream":%t,"store":false,"instructions":"You are Codex.","tools":[{"type":"function","name":"exec_command","parameters":{"type":"object","properties":{"cmd":{"type":"string"}}}}],"input":[%s]}`, stream, strings.Join(items, ","))
+ case provider.Chat:
+ items = append(items, `{"role":"user","content":"why does parse panic?"}`)
+ for i := range n {
+ args, result := turn(i)
+ items = append(items,
+ fmt.Sprintf(`{"role":"assistant","content":null,"tool_calls":[{"id":"call_%d","type":"function","function":{"name":"exec_command","arguments":%q}}]}`, i, args),
+ fmt.Sprintf(`{"role":"tool","tool_call_id":"call_%d","content":%q}`, i, result))
+ }
+ items = append(items, tail...)
+ return fmt.Sprintf(`{"model":"wb/deepseek-v4-pro","stream":%t,"tools":[{"type":"function","function":{"name":"exec_command","parameters":{"type":"object"}}}],"messages":[%s]}`, stream, strings.Join(items, ","))
+ case provider.Anthropic:
+ items = append(items, `{"role":"user","content":"why does parse panic?"}`)
+ for i := range n {
+ args, result := turn(i)
+ items = append(items,
+ fmt.Sprintf(`{"role":"assistant","content":[{"type":"text","text":"Let me look again."},{"type":"tool_use","id":"toolu_%d","name":"Bash","input":%s}]}`, i, args),
+ fmt.Sprintf(`{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_%d","content":%q}]}`, i, result))
+ }
+ items = append(items, tail...)
+ return fmt.Sprintf(`{"model":"wb/deepseek-v4-pro","max_tokens":8000,"stream":%t,"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[%s]}`, stream, strings.Join(items, ","))
+ }
+ // Gemini
+ items = append(items, `{"role":"user","parts":[{"text":"why does parse panic?"}]}`)
+ for i := range n {
+ args, result := turn(i)
+ items = append(items,
+ fmt.Sprintf(`{"role":"model","parts":[{"functionCall":{"name":"run_shell_command","args":%s}}]}`, args),
+ fmt.Sprintf(`{"role":"user","parts":[{"functionResponse":{"name":"run_shell_command","response":{"output":%q}}}]}`, result))
+ }
+ items = append(items, tail...)
+ return fmt.Sprintf(`{"tools":[{"functionDeclarations":[{"name":"run_shell_command","parameters":{"type":"object"}}]}],"contents":[%s]}`, strings.Join(items, ","))
+}
+
+var toolLoopAsks = []struct {
+ name string
+ from provider.Protocol
+ path string
+ stream bool
+ done string // how the agent's reply ends well
+}{
+ {"responses", provider.Responses, "/v1/responses", true, `"type":"response.completed"`},
+ {"responses whole", provider.Responses, "/v1/responses", false, `"status":"completed"`},
+ {"codex", provider.Responses, CodexPath + "/responses", true, `"type":"response.completed"`},
+ {"chat", provider.Chat, "/v1/chat/completions", true, "data: [DONE]"},
+ {"chat whole", provider.Chat, "/v1/chat/completions", false, `"finish_reason":"stop"`},
+ {"anthropic", provider.Anthropic, "/v1/messages", true, `"type":"message_stop"`},
+ {"anthropic whole", provider.Anthropic, "/v1/messages", false, `"stop_reason":"end_turn"`},
+ {"gemini", provider.Gemini, "/v1beta/models/wb/deepseek-v4-pro:streamGenerateContent?alt=sse", true, `"finishReason":"STOP"`},
+}
+
+func askAgent(t *testing.T, url, body string) string {
+ t.Helper()
+ req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(body))
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("session_id", "01a15199-593c-77d1-9913-ce0e1efd48e8")
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer res.Body.Close()
+ b, _ := io.ReadAll(res.Body)
+ if res.StatusCode != 200 {
+ t.Fatalf("status %d: %s", res.StatusCode, b)
+ }
+ return string(b)
+}
+
+// The reporter's loop (audie on Discord: Codex Desktop on WorkBuddy's
+// deepseek-v4-pro reading the same code 308 times): once the same call has
+// had the same result toolLoopRepeats turns in a row, the next turn is
+// answered by magpie in the agent's own API, a finished reply saying why,
+// and the vendor is never asked; Usage and the session's requests have it
+// as magpie's, sent nowhere, with the reason.
+func TestToolLoopIsStoppedOnEveryProtocol(t *testing.T) {
+ for _, x := range toolLoopAsks {
+ t.Run(x.name, func(t *testing.T) {
+ v := &countingVendor{}
+ gw := toolLoopOn(t, v)
+ // a long conversation of progress first, which the guard reads
+ // only the end of (tailOf)
+ turn := func(i int) (string, string) {
+ if i < 60 {
+ return pollJob(i)
+ }
+ return codexRead(i)
+ }
+ got := askAgent(t, gw+x.path, toolTurns(x.from, 60+toolLoopRepeats, turn, x.stream))
+ if !strings.Contains(got, "magpie stopped this turn") || !strings.Contains(got, x.done) || !strings.Contains(got, "sed -n 1,200p src/parser.rs") {
+ t.Fatalf("the agent wasn't told of the loop in its API:\n%s", got)
+ }
+ if n := v.asked.Load(); n != 0 {
+ t.Fatalf("the vendor was asked %d times for a looping turn", n)
+ }
+ var rec usage.Record
+ for range 50 {
+ if recs := usage.Load(time.Time{}); len(recs) > 0 {
+ rec = recs[len(recs)-1]
+ break
+ }
+ time.Sleep(20 * time.Millisecond)
+ }
+ if rec.Provider != "magpie" || !rec.Rejected || !strings.Contains(rec.Error, "tool loop") || rec.Session != "01a15199-593c-77d1-9913-ce0e1efd48e8" {
+ t.Fatalf("usage record %+v", rec)
+ }
+ })
+ }
+}
+
+// Codex on its own sign-in, a turn relayed to the ChatGPT backend as it
+// came, is stopped the same: its loop spends the user's ChatGPT plan.
+func TestToolLoopStoppedOnCodexsOwnSignIn(t *testing.T) {
+ v := &countingVendor{}
+ toolLoopOn(t, &countingVendor{})
+ up := httptest.NewServer(v)
+ t.Cleanup(up.Close)
+ was := provider.CodexBase
+ provider.CodexBase = up.URL + "/backend-api/codex"
+ t.Cleanup(func() { provider.CodexBase = was })
+ gw := httptest.NewServer(New().Handler())
+ t.Cleanup(gw.Close)
+ body := strings.Replace(toolTurns(provider.Responses, toolLoopRepeats, codexRead, true), `"model":"wb/deepseek-v4-pro"`, `"model":"gpt-5.5"`, 1)
+ got := askAgent(t, gw.URL+CodexPath+"/responses", body)
+ if !strings.Contains(got, "magpie stopped this turn") || !strings.Contains(got, `"type":"response.completed"`) {
+ t.Fatalf("Codex wasn't told of the loop:\n%s", got)
+ }
+ if n := v.asked.Load(); n != 0 {
+ t.Fatalf("the ChatGPT backend was asked %d times for a looping turn", n)
+ }
+ // one short goes on to the backend
+ askAgent(t, gw.URL+CodexPath+"/responses", strings.Replace(toolTurns(provider.Responses, toolLoopRepeats-1, codexRead, true), `"model":"wb/deepseek-v4-pro"`, `"model":"gpt-5.5"`, 1))
+ if n := v.asked.Load(); n != 1 {
+ t.Fatalf("the ChatGPT backend was asked %d times for a turn short of a loop", n)
+ }
+}
+
+// One turn short of the loop goes to the vendor, as does the reporter's
+// loop with the guard turned off in Settings.
+func TestToolLoopShortOrOffGoesOn(t *testing.T) {
+ for _, x := range toolLoopAsks {
+ t.Run(x.name, func(t *testing.T) {
+ v := &countingVendor{}
+ gw := toolLoopOn(t, v)
+ got := askAgent(t, gw+x.path, toolTurns(x.from, toolLoopRepeats-1, codexRead, x.stream))
+ if strings.Contains(got, "magpie stopped") || v.asked.Load() != 1 {
+ t.Fatalf("%d turns of the same call were stopped (vendor asked %d):\n%s", toolLoopRepeats-1, v.asked.Load(), got)
+ }
+ if err := settings.Save(settings.Settings{NoLoopGuard: true}); err != nil {
+ t.Fatal(err)
+ }
+ got = askAgent(t, gw+x.path, toolTurns(x.from, 40, codexRead, x.stream))
+ if strings.Contains(got, "magpie stopped") || v.asked.Load() != 2 {
+ t.Fatalf("the loop was stopped with Stop looping replies off (vendor asked %d):\n%s", v.asked.Load(), got)
+ }
+ })
+ }
+}
+
+// Work that makes progress is never stopped, however long it goes on: a
+// poll answered differently each time, and the reporter's read of the
+// same code once the user has said something since, magpie's notice
+// included in the conversation.
+func TestToolLoopLetsProgressBy(t *testing.T) {
+ notice := map[provider.Protocol][]string{
+ provider.Responses: {`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"magpie stopped this turn"}]}`, `{"type":"message","role":"user","content":[{"type":"input_text","text":"read it once more, then fix it"}]}`},
+ provider.Chat: {`{"role":"assistant","content":"magpie stopped this turn"}`, `{"role":"user","content":"read it once more, then fix it"}`},
+ provider.Anthropic: {`{"role":"assistant","content":"magpie stopped this turn"}`, `{"role":"user","content":"read it once more, then fix it"}`},
+ provider.Gemini: {`{"role":"model","parts":[{"text":"magpie stopped this turn"}]}`, `{"role":"user","parts":[{"text":"read it once more, then fix it"}]}`},
+ }
+ for _, x := range toolLoopAsks {
+ t.Run(x.name, func(t *testing.T) {
+ v := &countingVendor{}
+ gw := toolLoopOn(t, v)
+ got := askAgent(t, gw+x.path, toolTurns(x.from, 200, pollJob, x.stream))
+ if strings.Contains(got, "magpie stopped") || v.asked.Load() != 1 {
+ t.Fatalf("a poll whose answer moves was stopped (vendor asked %d):\n%s", v.asked.Load(), got)
+ }
+ // the loop, magpie's notice and the user's word after it
+ body := toolTurns(x.from, toolLoopRepeats, codexRead, x.stream, notice[x.from]...)
+ got = askAgent(t, gw+x.path, body)
+ if strings.Contains(got, "magpie stopped this turn before") || v.asked.Load() != 2 {
+ t.Fatalf("the user's word after the notice was stopped (vendor asked %d):\n%s", v.asked.Load(), got)
+ }
+ })
+ }
+}
+
+// What tells one exchange from another: the call's name and arguments
+// (whatever their keys' order), its result less what Codex writes of the
+// run, and whether it failed; never the call's id.
+func TestToolLoopSums(t *testing.T) {
+ call := func(id, name, args string) Part {
+ return Part{Kind: ToolCall, ID: id, Name: name, Args: json.RawMessage(args)}
+ }
+ result := func(id, text string) Part { return Part{Kind: ToolResult, CallID: id, Text: text} }
+ sum := func(c, r Part) uint64 {
+ e, ok := exchangeOf([]Part{c}, []Part{r})
+ if !ok {
+ t.Fatal("no exchange")
+ }
+ return e.sum
+ }
+ base := sum(call("a", "read", `{"path":"x","limit":2}`), result("a", "Wall time: 0.0246 seconds\nOutput:\nfn x()"))
+ same := []uint64{
+ sum(call("b", "read", `{"limit":2, "path":"x"}`), result("b", "Wall time: 1.5 seconds\nOutput:\nfn x()")),
+ }
+ differ := []uint64{
+ sum(call("c", "read", `{"path":"y","limit":2}`), result("c", "Wall time: 0.0246 seconds\nOutput:\nfn x()")),
+ sum(call("d", "grep", `{"path":"x","limit":2}`), result("d", "Wall time: 0.0246 seconds\nOutput:\nfn x()")),
+ sum(call("e", "read", `{"path":"x","limit":2}`), result("e", "Wall time: 0.0246 seconds\nOutput:\nfn y()")),
+ sum(call("f", "read", `{"path":"x","limit":2}`), Part{Kind: ToolResult, CallID: "f", Text: "Wall time: 0.0246 seconds\nOutput:\nfn x()", IsError: true}),
+ }
+ for i, s := range same {
+ if s != base {
+ t.Errorf("same exchange %d summed apart", i)
+ }
+ }
+ for i, s := range differ {
+ if s == base {
+ t.Errorf("different exchange %d summed the same", i)
+ }
+ }
+ // a turn with no call, or no result, is no exchange
+ if _, ok := exchangeOf([]Part{{Kind: Text, Text: "done"}}, []Part{result("a", "x")}); ok {
+ t.Error("a turn with no call is an exchange")
+ }
+ if _, ok := exchangeOf([]Part{call("a", "read", `{}`)}, []Part{{Kind: Text, Text: "stop"}}); ok {
+ t.Error("a turn with no result is an exchange")
+ }
+}
+
+// The guard reads every agent turn with results enough: a long Codex
+// conversation, ~1.5MB of 400 reads, costs it one parse.
+func BenchmarkToolLoopCheck(b *testing.B) {
+ big := strings.Repeat("let x = 1;\n", 300)
+ body := []byte(toolTurns(provider.Responses, 400, func(i int) (string, string) {
+ return fmt.Sprintf(`{"cmd":"sed -n %d,200p src/f.rs"}`, i), big
+ }, true))
+ b.SetBytes(int64(len(body)))
+ for b.Loop() {
+ if !mayLoop(provider.Responses, body) {
+ b.Fatal("no results")
+ }
+ req, err := parse(provider.Responses, tailOf(provider.Responses, body))
+ if err != nil {
+ b.Fatal(err)
+ }
+ if _, ok := toolLoopOf(req.Messages); ok {
+ b.Fatal("a loop")
+ }
+ }
+}