Commit Graph
5 Commits
Author SHA1 Message Date
yetone 3245b730d2 gateway: the tool-loop notice is written in the conversation's language
When magpie stops a conversation going round one tool call, the notice the
agent shows was English only and began "magpie stopped this turn". A
conversation in Chinese got an English notice. The notice goes upstream
with the next turn, so it now names neither magpie nor a vendor's limits.

conversationLang (reply_lang.go) reads the user's own words in the whole
body. It skips tool results, system prompts and whole <tag> blocks, and
decides by script: kana -> ja, Hangul -> ko, Han -> zh or zh-TW (by
simplified/traditional-only characters), Latin with German marks -> de,
otherwise en. toolLoop.notice(lang) picks the matching text; the setting
is named as the GUI names it in that language (Korean, which the GUI
lacks, names it in English).

Run, at this head on Linux (wsl-run, magpie-desktop):
- gofmt -l: clean; go vet ./... for linux (-tags nogui) and windows: ok (darwin
  vet needs cgo, so it runs with the full suite on mote afterwards)
- go test -run 'Lang|ToolLoop|LoopGuard|Cap|Amount' ./internal/gateway
  ./internal/provider ./internal/cli: ok
  (TestLangOfEachLanguage, TestConversationLangReadsTheUsersWords: each
  language, mixed, empty; TestToolLoopNoticeInTheConversationsLanguage)
Broken by hand: counting kana as Han in langOf; all three tests go red.
Not tried: the full go test ./... at this head (queued on mote, runs
after), a real agent looping in Chinese. The reply loop guard's notice
(loop_guard.go) is still English and names magpie: #5764.

Reported-by: tenacious_eagle_96548 (Discord)
2026-11-09 18:19:02 +08:00
yetone 12d4825afa gateway: a turn of only waits (sleep, wait, delay, pause, poll) is not a tool loop
xiaozhi09400 (Discord, Codex): clock__sleep {"duration_ms":60000} called 8 times
while CI ran was stopped as "the same tool call, the same result". A wait
is asked again with the same arguments and answers the same by design; the
time passing is the progress, and a request carries no timestamps to
measure it, so the guard now skips a turn whose calls are all waits.

A call is a wait when the tool's own name (after server__ / namespace
prefix, so a server called "wait" does not count; snake and camelCase
split into words) has sleep, wait, delay, pause or poll, or when its
arguments are only duration keys with numbers (duration_ms, seconds, ms,
timeout...). A turn that waits and also runs another call is judged as
before. Any other tool repeated 8 times is still stopped.

Run (mote-run): TestToolLoopLetsWaitsBy is red on the old code (8 wait
shapes stopped; guard disabled by hand) and green now; ./internal/gateway
-timeout 45m ok (696s); GOOS=windows and GOOS=linux -tags nogui go vet ok;
gofmt clean. Not tried: a real Codex with the clock MCP server; Codex's
exact namespaced name is taken from the report (clock__sleep).
2026-11-04 02:18:23 +08:00
yetone 4ca64c21b2 gateway: a conversation's tail is made in a slice of its size, so -race counts one copy (#4004)
TestTailOfReadsInPlace was red in both Race jobs at ca499953e (run
40696645744): 2686576 bytes allocated for a 1314661 byte tail, over its
want+body/4 budget. Under -race bytes.Buffer.Grow's append-make pattern
allocates the slice twice (a -memprofilerate 1 profile on mote:
bytes.growSlice 2576kB, two copies of the tail); without -race it is one
(1367680 bytes). tailOf now appends into make([]byte, 0, n), one
allocation either way. Same bytes out.

Run on mote (M5 Pro) at this tree:
- old code, -race -count=8: red 8 of 8, 2686576 bytes each time
- go test -race -count=5 of tool_loop_test.go's six tests: ok
- the same without -race, -count=5: ok
- go vet ./internal/gateway for darwin, linux, windows: ok
Not run: the full suite (one function body changed, its tests above).
2026-10-28 22:09:53 +08:00
yetone 56d26f0bba gateway: a conversation's list is looked at where it sits in the body, not copied (#4004)
gjson.GetBytes copies the value it finds. For "input" or "messages"
that is the conversation, so tailOf, the effort, deferred-tools and
refs-only checks, Cline's images, Codex's images, the compaction
lookups and the title digest each copied a 44 MiB Codex turn once
per request to look at it. getIn reads it in place (gjson.Get over
the body's bytes). None of the nine sites changes the body while a
result is held, and none keeps a string past the request: Codex's
image jobs read url.Str and write a new buffer, the compaction encs
are lookups, the title keeps a digest.

Together with the masked-record copy, a 44 MiB Responses turn sent
on as Chat allocates ~89 MB less (1,068 to 1,005 MB over the turn)
and runs fewer GCs. Peak live heap is unchanged (282 MB at GOGC=10
with gctrace, before and after): the body, the parsed request and
the outgoing Chat body are what is held at the peak. This is less
allocation and GC CPU, not a lower RSS peak.

TestTailOfReadsInPlace: a 4 MiB conversation's last 64 items come back
right, allocating under the tail plus a quarter of the body. Red with
GetBytes put back (5,480,048 bytes for a 1.3 MB tail of a 4.1 MB body).

Run (mote-run at aa8ded04e, both commits, on 56602426d):
- gofmt -l clean; go vet ./internal/gateway ./internal/maskrec for
  linux, windows and darwin: ok.
- go test -count=1 -timeout 45m ./internal/gateway ./internal/maskrec:
  ok (gateway 533s).
- The new test red with the old code put back (above), green with it.
- The allocation and peak numbers: a scratch test of one 44 MiB
  Responses turn sent on as Chat (not committed), GOGC=10 gctrace for
  the peak.
- Rebased onto 27bf14790 (lan.go changed in the gateway): the same vet
  and go test -timeout 45m ./internal/gateway ./internal/maskrec again
  at this head: ok (gateway 549s).

Reported-by: jihuayu
2026-10-28 20:11:57 +08:00
yetone de281760e2 gateway: a conversation going round one tool call with the same result is answered by magpie after 8 turns (audie on Discord)
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 onto 50f52b540, 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 on 50f52b540:
  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.
2026-10-23 21:36:51 +08:00