Commit Graph
7 Commits
Author SHA1 Message Date
yetoneandggbdpq ef81b7ad16 gateway: a turn translated for another magpie asks its model's own reply limit
A turn for a Remote magpie's model that magpie translates (the search
tool declared, searchedForIt) went out with no reply limit: fillsOutput
left another magpie out, its own filling being translation work it does.
But a body translated here goes to that magpie with its tools converted,
one it passes whole — its filling is translation work it never does, so
the vendor's own short default was all a cascade got: Codex stopped at
1,024 output tokens two magpies deep (#5566) where the same model asked
direct of the far one completed. A turn relayed whole is untouched: no
limit is filled but in a translation, where the other magpie's own never
runs for it.

The exclusion came with d157700a0 ("another magpie fills its own"),
but fillsOutput is consulted only in forwardTranslated, and a body
translated here is one the far magpie passes whole. A far magpie that
serves the model from a ChatGPT account still drops the filled field
there (provider/codex_request.go, siwcDropped), and a vendor refusing
it falls into the existing outputFillRefused retry.

Run on mote at this tree: go test -tags nogui -run
'Output|Remote|Cascade|MaxTokens' ./internal/gateway ok.
TestRemoteMagpieCascadeAsksItsOutputLimit is red with main's
maxoutput.go put back (both hops sent max_output_tokens 0, want
393216), green restored. gofmt -l clean.
Not tried: the reporter's two real magpies with a 1,024-default
upstream.

Reported-by: ltlly
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-07 21:23:43 +08:00
yetone 1360689ef2 provider: Served is shared while a request holds the catalog, not copied for each look (#3725)
Served built a fresh copy of the whole catalog on every call: the
unlisted groups' entries plus every provider entry. An Entry is ~1.3KB,
and a request looks at Served from outputLimit, userOutput, askedOutput
and windowIn, so with 12 providers of 20 models each look copied ~380KB
and a request ~800KB, all garbage by the reply.

Served now goes through heldFrom like providerEntries: one slice, built
once while a request holds the catalog and kept across requests until a
file it is built from changes. It is clipped, so a caller's append makes
its own copy and never reaches the next caller. The four gateway loops
take each entry by pointer instead of copying 1.3KB a step. Every
caller of Served (gateway, gui, cli, tui, agent, provider) was read for
writes into the slice or its entries; all only read.

Measured on BenchmarkZZIO (12 providers x 20 models, an 8KB request,
not committed): 1.57MB allocated per request before, 640KB after.

TestServedSharedWhileHeld: under Hold two calls give the same backing
entry, and an append to one doesn't show in the next. Red on the old
code (a new copy each call), green now.

Run at f2aa7fe34 (origin/main plus this change): gofmt -l clean on
internal/gateway and internal/provider; GOOS=linux and GOOS=windows go
vet -tags nogui ./internal/gateway ./internal/provider ok; go test
-count=1 -timeout 45m ./internal/provider (skipping
TestUnreadLoginsWriteErrorsKeepCredentials, #3632) ./internal/gateway
./internal/gui ./internal/cli ./internal/gwmw all ok.
2026-10-27 01:55:25 +08:00
yetone d157700a0a gateway: a request naming no reply limit asks DeepSeek, and a translated one any vendor, for the model's own
xuanwo on X (tweet 2114748847167815951): Codex on DeepSeek kept ending
"stream disconnected before completion: Incomplete response returned,
reason: max_output_tokens". Codex names no max_output_tokens, magpie
relayed its turn to DeepSeek's /v1/responses as it came, and DeepSeek
reads no limit as 8K without thinking and 64K with it (its
create-chat-completion docs: "When not set, the default is 8K in
non-thinking mode, 64K in thinking mode"), where deepseek-flash and
deepseek-v4-pro give 384K (393216, its /v1/models and pricing page).
Reproduced on api.deepseek.com with the owner's key: deepseek-flash,
effort none, no limit: status incomplete, reason max_output_tokens, at
8192 output tokens; with max_output_tokens 393216: completed at 17602.

- Relayed or translated, a request naming no reply limit, to DeepSeek's
  own API (api.deepseek.com, shortByDefault), is sent the model's own
  limit (askedOutput: the user's Max output, else the vendor's list or
  models.dev, within the window), in the client's words: Responses
  max_output_tokens, Chat max_tokens, Gemini generationConfig.
  maxOutputTokens. A relay serving DeepSeek's models elsewhere goes as
  it came: its default isn't DeepSeek's.
- Translated onto a vendor's Chat or Responses (fillsOutput), a request
  naming none is sent the model's own, as on Anthropic's since #1792:
  the client's API reads none as the model's whole, a vendor's needn't.
  Not for an account's own backend (the ChatGPT one turns
  max_output_tokens away) or another magpie (it fills its own). A
  vendor that turns the filled figure away ("max_tokens" in a 400/422
  that isn't the window) is asked again naming none, and from then on
  (outputFillRefused).
- The window: DeepSeek counts the reply asked for in its 1M window. The
  existing room retry (replyLeft) asks again for what the window left;
  withReplyCap now also adds the field to a Gemini body, which it
  couldn't before, so a Gemini client's long conversation isn't
  refused where DeepSeek's default fitted.
- A limit the client named goes as it is, everywhere. A reply DeepSeek
  cuts on length still reaches Codex as incomplete / max_output_tokens
  (relayed as DeepSeek says it; translated by responses.go's length
  stop), unchanged.

Tests: TestShortDefaultAsksTheModelsOutput (DeepSeek relayed on
Responses and Chat, the client's own limit kept, a Gemini client
translated, and a relay's Responses and Chat with the same model sent
no limit), TestTranslatedAsksTheModelsOutput (Codex onto a Chat-only
vendor: the catalog's 65536; the client's 700 kept; a vendor refusing
past 32768 is asked again naming none, and not filled again).
Broken by hand: withOwnOutput returning the body as it came turns the
two DeepSeek relayed cases red; fillsOutput false for Chat/Responses
turns the translated case red; the refusal retry off turns the refusal
case red (400 to the client).

Real run (sandbox HOME, only the owner's DeepSeek provider copied, this
build serving on 127.0.0.1:3481): a streamed Codex-shaped Responses turn
to deepseek/deepseek-flash naming no limit, asking for ~17K tokens:
response.completed, 17602 output tokens (8192 and incomplete before, as
above). A 699015-token Codex turn: DeepSeek refused 393216 against its
1M window, magpie asked again with max_output_tokens 349561 (the window
left), response.completed.

Run: gofmt -l clean; go vet ./internal/gateway on darwin, linux and
windows; go test ./internal/gateway (whole package) -count=1
-timeout 45m: ok (465s) at base 479de8900.

Not tried: deepseek-v4-pro with thinking on (64K default) in a real
run, only flash with effort none; a plugin provider for real (its path
is forwardTranslated's, covered by the translated test with an API-key
vendor); other vendors whose Chat default is small (Kimi, GLM, MiniMax)
on their relayed path, which this leaves as it was: only DeepSeek's
default was checked against its docs and its API. The full ./... suite
(only internal/gateway changed).
2026-10-27 00:25:18 +08:00
yetone b749cffbb1 gateway: a reply asked past what the window leaves is asked again for what it leaves, not failed as too long (molvqingtai, da34, Jeremy.Zhou on Discord)
DeepSeek counts the reply asked for (max_tokens) in its 1M window with
the prompt: "maximum context length is 1000000 tokens. However, you
requested N tokens (P in the messages, C in the completion)". Since
62020b9c2 (#2355) a client's max_completion_tokens reaches DeepSeek as
max_tokens, so a turn asking the model's whole 384K on a ~620K+
conversation was refused there and read as "too long for its model"
(molvqingtai: auto-deepseek-v4.1-flash, round robin, the official
member failing where WorkBuddy and OpenCode Go answered). Codex turns
are asked the model's own 393216 on Anthropic's API (#1792), and since
1a3b92688 (v0.1.1290) Claude Code beside a [1m] DeepSeek is told 1M
and compacts only near it, so both met the same refusal before they
compacted (da34: Codex "ran out of room"; Jeremy.Zhou: Claude Code).

replyLeft (maxoutput.go) reads the window and the prompt from that
refusal, in DeepSeek's/vLLM's words or Anthropic's ("input length and
`max_tokens` exceed context limit: P + C > W"), and the same account and
model are asked once more for W-P (withReplyCap: the request's own field
lowered, or max_tokens / max_output_tokens set where it named none).
Room under 8192 is left alone: the agent is told it overflowed, to
compact. The try is "room" in the Routing page ("reply asked past the
window left", zh/zh-TW/ja/de). DeepSeek's documented window stays 1M
(api-docs pricing: deepseek-flash 1M, max output 384K), as models.dev
and the catalog say, so no number changes.

Run:
- TestDeepSeekReplyFitsTheWindowLeft: deepseek preset on a fake chat
  API that refuses in DeepSeek's words when prompt+max_tokens > 1M;
  max_completion_tokens 393216, max_tokens 393216 and a Responses
  client each arrive as 393216 then 293216 and answer 200; 32000 is
  asked once; room under 8192 reaches the agent as the 400, asked once.
- TestAnthropicReplyFitsTheWindowLeft: Anthropic's wording; Claude
  Code's max_tokens 64000 -> 30000, Codex naming none -> 393216 -> 30000.
- TestReplyLeft: both wordings, vLLM's newer one, and errors with no
  room or no parts give 0.
- Broken by hand: the retry's condition off -> both tests FAIL with the
  vendor's 400; the cap not applied on the retry -> FAIL the same.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... clean.
- go test -timeout 45m ./... at 33dbdcc52 + this change: 64 packages ok,
  0 FAIL (load average 130-160). An earlier run before the Anthropic
  wording was added timed out in internal/agent (TestPortMovesEveryAgent
  at the 45m mark, load ~110), which this change doesn't touch; the
  second run passed it.
- GUI: i18n-keys, routing-*, panel-routing* in both engines and every
  language. Red here and on origin/main ab7ec8c65 alike: routing-bodies
  (#2559), routing-model-rest (#2115), routing-request-metrics WebKit
  (#2571), routing-keys-fold WebKit ja/de once (#2658).
- Not tried against the real DeepSeek API: the refusal's wording is
  DeepSeek's documented OpenAI-compatible one, not captured from the
  reporters.
2026-10-19 19:05:30 +08:00
yetone b9c589ebb1 gateway: the user's own Max output lowers a plain provider's max_tokens, not only a group's (YaCo on Discord)
Claude Code asks claude-opus-5-5 for max_tokens 128000. A relay that
holds back max_tokens x price from a key's quota answered 403 "token
quota is not enough ... need $2.59" for every turn (128000 x $20/M plus
~8K input x $4/M). Setting the provider's Max output didn't help: the
gateway lowered max_tokens with withMaxOutput only inside a group
(outputLimit), so a request routed straight to a provider went out as
the agent asked.

userOutput finds the Max output the user set for the provider's model
(provider > model or the provider's "*", Entry.OutputSet) and lowers a
larger request to it on any route. Only the user's own value does this
outside a group: a vendor's catalog figure may be stale. Group routing
is unchanged.

Run:
- TestUserMaxOutputLowersAPlainProvidersRequest (new): 128000 -> 32000
  with Max output 32k on the model, 8000 kept, a sibling model with no
  Max output kept at 128000. Red without the gateway.go change ("went
  as 128000").
- go test ./internal/gateway -count=1 (whole package, -timeout 45m):
  ok, 1200s, before rebasing onto 4e9335c96; after the rebase the
  Output/MaxTokens tests again: ok.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
- A real Claude Code 2.1.29x against a fake server with the reporter's
  env: it sends max_tokens 128000 for claude-opus-5-5 with or without
  magpie. Not tried against the reporter's relay.
2026-10-14 21:11:42 +08:00
yetone 50eeaae6ce gateway: a turn that names no reply limit is sent the model's own as max_tokens on Anthropic's API, not a flat 16384 (#1792, tensornull)
Anthropic's Messages API needs max_tokens. A request that names none
(Codex's Responses turns, a Chat client's) was always sent 16384, so a
long thinking and tool turn on a Claude-shaped relay hit the limit, and
Codex showed "Incomplete response returned, reason: max_output_tokens".

Now such a request is sent askedOutput(p, model). That is the plain
Served entry's PublishedOutput(), the same number the agents are told:
the user's Max output first, else the vendor's list or models.dev,
within the window. 16384 (defaultMaxTokens) is used only when nobody
knows the limit. A limit the client names still goes as it is. The
thinking budget is still fitted under max_tokens (anthropicBody),
whatever the limit is.

Both places that build an Anthropic body are covered:
forwardTranslated, for every translated route, and serveZed for a Zed
subscription's Claude models.

The provider editor's Max output hint now says this number is also
sent as max_tokens (en, zh, zh-TW, ja, de). docs/reference.md and
docs/subsystems/gateway-routing.md no longer say the gateway never uses
the limit.

Run:
- TestAnthropicMaxTokensIsTheModels covers: the user's 65536 (Responses
  plain and streamed, and Chat), models.dev's 64000, a 500000 limit
  held to the 100000 window, an unknown model at 16384, the client's
  own 1000, and thinking under 8192.
- TestZedClaudeMaxTokensIsTheModels expects 40000 when the client names
  no limit and 1000 when it does.
- Both tests fail on the old code: 16384 where the model's number was
  expected.
- go test ./internal/gateway/ passed (1270s, under load).
- provider-max-output.test.cjs passes 28/28 in chromium and webkit:
  5 languages at 900 and 420px. The hint names max_tokens and
  Anthropic, is translated, and doesn't overflow.
- gofmt -l is clean. go vet passes on darwin, on linux with -tags
  nogui, and on windows.

Not tried against a real Anthropic-compatible relay with a real Codex
session.
2026-10-12 22:44:26 +08:00
yetone 7df9345903 routing: a group tells agents the longest reply a member of it gives, and each member is asked for no more than its own (ARNO on Discord: 发现group 模型的maxTokens是按照最小的来设定的,导致agent经常跑一半发生错误 "Response was truncated before completion.")
A group's catalog entry took the smallest member's output limit, so /v1/models and the agents' catalogs told agents that, and every reply through the group was capped at it, even from the members that write far longer ones. The group's Output is now its largest member's (as its context window already is, #700), and the gateway, for a group request, lowers the agent's max tokens to each member's own known limit (its catalog Output: the user's per-model setting, else the vendor's list or models.dev) only when the request asks for more: max_tokens / max_completion_tokens (Chat), max_output_tokens (Responses), generationConfig.maxOutputTokens (Gemini), and for Anthropic max_tokens with the thinking budget kept under it (budget = limit-1, or thinking off when no 1024+ budget fits). A request asking for less, or a member with no known limit, goes as before.

TestGroupOutputPerMember (members at 4096 and 32000: the group says 32000; asked for 20000, the 4096 one gets 4096 and the 32000 one 20000; asked for 1000, 1000) fails on the old code at either half; TestWithMaxOutputAnthropicThinking. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (TestProvidersAnswerWhileListsComeIn, TestUserPath and TestClaudeSignedOut failed under load and pass alone).
2026-10-04 22:48:06 +08:00