audie on Discord: on 10-21 Codex Desktop, routed to WorkBuddy's deepseek-v4-pro, ran the same read-code tool call 308 times between 13:24 and 13:52 (316 requests, 212.65 credits). That evening it did it 340 more times between 19:58 and 20:22 (343 requests, 324.52 credits). loopGuard only catches a loop inside one streamed reply. Here every reply was a short, valid call. The loop runs across turns, so it shows in the request's own history. stoppedToolLoop (internal/gateway/tool_loop.go) runs in serveAgent after the session is recorded and before hooks, middleware and routing. It also runs in the Codex backend's passthrough on Codex's own ChatGPT sign-in. It needs the last 8 turns to be the same exchange: - the same calls: name plus arguments, key order ignored, call ids ignored; - answered with the same results: text, error flag and images, with Codex's "Wall time", "Chunk ID" and "duration_seconds" taken out (runNoise); - with nothing else said on the user's side. When it finds one, it answers the turn in the agent's own protocol, streamed or whole (Responses, Chat, Anthropic, Gemini through relayStatus), with a notice that names the call. Nothing goes upstream, and no prompt, base_instructions or identity text sent to a vendor changes. A different result, or any word from the user, starts the count again. The notice is a turn without calls, so the user's next message carries on. The check keeps no state, so it is per conversation, and a retried request doesn't count twice. The turn is recorded the way Codex's title turns are. sentNowhere's body becomes answeredHere (from-protocol aware): the Routing trace shows "Tool loop stopped", and a usage row has provider magpie, rejected, the conversation's session and native session, and an error starting "tool loop:". So Usage and Sessions both show why. Cost: a body with fewer than 8 tool results isn't parsed (a bytes.Count). For a longer one, only the newest 64 items are parsed, found with gjson (tailOf). For a 1.5MB Codex turn that is 1.1ms and 2.5MB, against 30ms and 20MB for a full parse (BenchmarkToolLoopCheck). There is no new setting. Settings › Models › Replies › Stop looping replies (noLoopGuard, on by default) turns this off together with the reply guard, and the notice says so. The GUI sub-text is unchanged. docs/subsystems/gateway-routing.md has a bullet on it. Run at this commit (vet/build/race at parent bf2e067ef; the full suite after the rebase onto50f52b540, at 558b21b7e): - gofmt -l clean. - go vet ./... on darwin, GOOS=linux go vet -tags nogui ./..., and GOOS=windows go vet ./.... - go build on darwin, linux (-tags nogui) and windows. - TestToolLoop* and the Codex title/description/prediction tests under -race (TestToolLoop* -count=3). - The tests, in a sandbox HOME against fake vendors: - TestToolLoopIsStoppedOnEveryProtocol: 60 poll turns then 8 Codex reads with a varying chunk id and wall time, on Responses (stream and whole), Codex's backend path, Chat (stream and whole), Anthropic (stream and whole) and Gemini. The vendor is asked 0 times, the notice and the protocol's done marker reach the agent, and the usage row is magpie, rejected, "tool loop", with the session. - TestToolLoopStoppedOnCodexsOwnSignIn: gpt-5.5 on the passthrough, with a fake CodexBase. - TestToolLoopShortOrOffGoesOn: 7 turns, and 40 with the setting off. - TestToolLoopLetsProgressBy: 200 polls whose answer moves, and a loop followed by the notice and a word from the user. Neither is stopped. - TestToolLoopSums. - Broken by hand, each red, each restored: - The guard skipped: every protocol subtest and the Codex sign-in test fail ("the agent wasn't told of the loop"). - runNoise removed: the same tests fail, plus TestToolLoopSums. - Results left out of the sum: every progress subtest fails ("a poll whose answer moves was stopped"). - Full suite: set -o pipefail; go test -tags nogui -timeout 45m ./... through suite-lock, in a sandbox HOME, at 558b21b7e on50f52b540: exit 0, 69 packages ok, no FAIL (internal/gateway 854s, internal/agent 812s, internal/provider 393s, internal/gui 291s). - No GUI file changed, so the GUI suite wasn't run. Not tried: - A real Codex Desktop or WorkBuddy loop, or any real account. The fixture's shape is Codex's exec_command output; audie's literal bytes weren't available. - The Windows box. - Codex over websocket (magpie answers it 426). - Responses conversations that use previous_response_id. They carry only the newest turn, so they are never stopped. - Loops with more parallel calls a turn than fit in 64 items. - A poll that gets the same answer 8 times in a row is stopped too. The user's next word, or the setting, carries on. - dsh's two ~1M-token requests to DeepSeek on 10-23 are a different problem, not addressed here.
328 lines
11 KiB
Go
328 lines
11 KiB
Go
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() {})
|
||
}
|