[bug] Codex + 自定义 Responses 上游:一次上游 "prompt is too long" 就永久废掉这条会话——不换账号、不压缩,重发仍钉在同一后端
现象
Codex(Desktop)经 magpie 走 workbuddy/deepseek-v4.1-flash(wire_api = "responses",/backend-api/codex),上游某次返回 400:
WorkBuddy: prompt is too long: 100001 tokens > 100000 maximum
之后这条会话就再也发不出去:重发仍然是同一个结果;只有把模型切成 Codex 自带的 gpt-6.1-sol(窗口 50 万)才立刻恢复。这个问题不是每次都出现——同一个账号隔 25 分钟再发 142,818 token 的请求是 200 正常的。
环境
- macOS 27.0.1(arm64)
- magpie 0.1.1318
- Codex Desktop 0.163.0-alpha.11.1(
originator: Codex Desktop) - provider:workbuddy(腾讯 copilot 账号池),model:
workbuddy/deepseek-v4.1-flash - 相关配置:
openai_base_url = http://127.0.0.1:3425/backend-api/codex
复现(我这边是视频生成任务,工具每步回灌截图)
- 在 Codex 里用 workbuddy 的模型开一条会话,跑到上下文约 9.5 万 token;
- 一次工具返回 ~3.7MB base64 截图,prompt 从 95,752 跳到 126,809;
- 该请求被上游 400 拒绝(上面的报文);
- 在同一个会话里重发 → 仍然失败;把模型换成 Codex 原生模型 → 立刻恢复。
证据
routing/2026-10-23.jsonl 里那条失败请求:
{"agent":"codex","model":"workbuddy/deepseek-v4.1-flash","status":400,
"tries":[{"id":"workbuddy","status":400,"fail":"other",
"error":"WorkBuddy: prompt is too long: 100001 tokens > 100000 maximum"}],
"prompt":{"window":1000000,"tokens":126809,"turns":3}}
它前后相邻的请求(同一个 session):
09:23:53 200 95,752
09:24:18 400 126,809 <- 上游 10 万上限
09:28:21 499 126,815 <- 重发,被取消
09:29:21 200 123,051 <- 换 gpt-6.1-sol 后立刻通
而 affinity.json 里这条会话至今仍钉着:
"workbuddy/deepseek-v4.1-flash|01a16652-…": {"who":"workbuddy@01-cc","turn":1,"cacheRead":95616}
对照实验:同一个账号(workbuddy@01-cc)在 09:49 单独发 142,818 token 的请求 → 200 正常。所以不是账号被封顶,是这条会话被绑到了只认 10 万的那个后端上。
另外 models_cache.json 里这个模型登记的是 context_window: 272000 / max_context_window: 1000000,Codex 因此认为还有 13 万可用,永远不会提前压缩,直接撞墙。
期望行为(三条,任选其一都能救)
- 把这类错误当可恢复错误:
prompt is too long/maximum \d+命中时,按路由组换下一个账号/模型重试;全组都失败时,在报错里明说"请先 /compact 或换模型",而不是原样透传 400(这点和 #1422 那次 413 的处理方式一致)。 - 学到就记住:从错误里解析出上游真实上限(这里是 100000),写进
accountWindowCaps,并把对外宣称的窗口(models_cache 的context_window)收敛到该值,让 agent 在撞墙前自己压缩。 - 失败后解除会话亲和:一次
prompt is too long之后清掉该 session 的 affinity 钉扎,避免重发永远打同一个后端。
(完整日志可补;已脱敏,无账号邮箱与 key。)
据 #3348 实测,这条通道「无缓存时 10 万、缓存命中后 24 万以上」,所以 10 万更像冷缓存的边界,而不是模型窗口。
Taking this one. Your #3348 supplement is the key: 100001 > 100000 is the cold-cache boundary, not the model's window — so the interesting half is why the session never recovers on resend (which rest or pin keeps the turn from recovering, and why compaction doesn't shrink the context back under it). I'll trace what magpie does with that exact refusal on the /backend-api/codex relay and follow with a patch or a precise diagnosis here.
Generated-by: GLM-5.3-Flash (ZCode)
Draft comment for yetone/magpie#3337
Taking this one meant tracing what magpie does with that exact refusal, so here is the trace, on ea9454d2.
What magpie does with that 400
WorkBuddy: prompt is too long: 100001 tokens > 100000 maximum is matched by tooLongRe (internal/gateway/gateway.go:7102) and answered as an overflow, not as an account's trouble:
failure()sorts it asfailOtherprecisely because "it is the conversation's length, not the plan's" (internal/gateway/routing.go:386-390) — andfailOtherrests nobody.retryable()returns false for an overflow ("a conversation too long for the model is as long on every account of it", internal/gateway/fallback.go:1279-1282), so the failure never reachesrestAfterMarkedeither. Nothing is rested — which matches your control: the same account answered 142,818 tokens with 200 twenty-five minutes later.- When the group has another member that may hold the request, the overflow branch hands it over and "Nobody rests" (internal/gateway/gateway.go:3129-3160, #700). With one member — your case — there is nowhere to hand it to.
- What the agent is told is not WorkBuddy's raw reply:
writeErrorre-says it the way the client's own API names an overflow, a 400 with"code":"context_length_exceeded"(internal/gateway/gateway.go:7056-7061). I probed both/v1/responsesand/backend-api/codex/responseswith that exact body: the client gets{"error":{"code":"context_length_exceeded","message":"Fake: WorkBuddy: prompt is too long: 100001 tokens > 100000 maximum",...}}. - Resending runs the relay every time: three resends in a row reached the fake upstream three times (no local 429, no cool-down, no back-off), each answered the same. The session is not blocked by anything magpie keeps; the stick in
affinity.jsonstays by design — it is written on success only (answered, internal/gateway/affinity.go:647) and dropped only when a reply broke off mid-stream (unanswered, gateway.go:3544), because leaving is what costs the cache.
So the "never recovers" half does not live in a rest, a pin that should have been cut, or a swallowed error. As your #3348 supplement says, 100001 > 100000 is the channel's cold-cache boundary, not the model's window: a resent 126,809-token request is past it whenever the upstream's cache is cold, and a failed request writes no cache, so every resend lands cold again. Meanwhile Codex holds the conversation to the 272,000 the catalog claims, so it never compacts on its own — and the compaction request itself carries the same 126k of history, so on a one-model setup it is refused by the same boundary: the way out is shut too. That is the whole deadlock, and none of its walls is magpie's rest logic.
Against the three fixes proposed
- "Treat it as recoverable, fall over within the routing group" — the fall-over exists (
withRoom, gateway.go:3129), and it deliberately skips other accounts of the same model at the same provider, because they hold the request no better; your control shows why: another account of the same channel is past the cold boundary all the same. The second half — saying in the error what to do — is half-there: the status andcontext_length_exceededare standard, and where the words are magpie's own they do say what to do (bridgeFailed, internal/gateway/claude_subscription.go:4403-4406: "Compact the conversation, or turn on extra usage…"); the relayed vendor wording stays as the vendor said it, whichTestCountTokensUpstreamErrorspins exactly (internal/gateway/count_tokens_test.go:79). I read that pinning as deliberate, so I did not bolt a hint onto it — but a maintainer's "say /compact here too" would be a small, well-precedented change if you want it. - "Learn the ceiling into the window" — this is the one lever that could actually break the deadlock on its own (Codex would compact at ~80% of a corrected window, never reaching the boundary), and nothing in the repo does it today:
replyLeft(internal/gateway/maxoutput.go:124) parses a vendor's numbers but within one request only. The catch is the one your own supplement found: the ceiling is dynamic (cold 100k, cache-hit 240k+). Collapsing the advertised window to 100k makes Codex compact conversations that the warm channel would have answered, and every compaction rewrites the prefix the cache pays for. Whether that trade is worth it — and whether to touch only the catalog Codex sees, orwindowOftoo (withroomy/withRoomthen passing such members over before a request is spent) — is a product call I'd rather not make unilaterally. - "Drop the session affinity on this failure" — leaving on a cold-cache refusal is the one move that guarantees the next turn is cold everywhere it lands, while staying keeps the one account whose cache may still be warm. It also can't help here: the boundary belongs to the channel, not the account.
What works today, without a patch
Put the model in a routing group whose compaction rule sends summaries to a member with a real window: the turns stay on WorkBuddy for its cache, the compaction goes to the big-window member, and the conversation comes back under the boundary (this is the way out compactFails.held already names, internal/gateway/codex_compact_guard.go:63-65). Or, once stuck: switch the thread to a native model, let it compact, switch back — what you did, as a one-off.
If the maintainer wants magpie to learn the learned ceiling (> N maximum) into the window it advertises to Codex, I'm glad to build that behind a red test — said numbers, per provider and model, taken only while they undercut the catalog's claim. It just shouldn't slip in as a side effect of this thread.
补充:2026-10-24 Windows / Codex 再次出现同款超限错误
今天用相同初始提示词分别运行三条 Codex 聊天,制作独立 HTML / SVG 动画。只读核对本地 Codex 会话和 Magpie routing 日志后,确认 WorkBuddy DeepSeek 再次出现本帖的同款错误。以下均为北京时间,已移除账号标识、会话 UUID、凭证和本地路径。
上游错误与客户端表现
三次失败均为 HTTP 400,上游原文完全相同:
WorkBuddy: prompt is too long: 100001 tokens > 100000 maximum
Codex 显示:
Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.
同一条 DeepSeek 会话的关键记录
时间为 routing.time(请求记录时间),不是响应完成时间。
| 时间 | 模型 | HTTP | 输入 / 本地估算 | 说明 |
|---|---|---|---|---|
| 13:58:31 | workbuddy/deepseek-v4.1-flash | 200 | 27,830(usage 回报) | 初始请求成功 |
| 14:21:19 | workbuddy/deepseek-v4.1-flash | 200 | 154,547(usage 回报) | cache_read=154,112,in=435 |
| 14:21:47 | workbuddy/deepseek-v4.1-flash | 400 | 218,967(本地估算) | 100001 > 100000,约74秒返回 |
| 14:40:16 | workbuddy/deepseek-v4.1-flash | 400 | 218,971(本地估算) | 要求继续,仍同样报错 |
| 14:43:14 | antigravity/gemini-3.8-flash | 200 | 175,401(usage 回报) | 同一会话切模型,要求先查看情况,正常回答 |
| 14:46:43 | workbuddy/deepseek-v4.1-flash | 400 | 224,174(本地估算) | 切回 DeepSeek 要求继续,再次同样报错 |
失败请求没有 counted:true,其 prompt.tokens 是 Magpie 本地估算;成功请求数字来自 usage 的 in + cache_read。两类数字不能直接相减,也不能把 100001 当成完整输入的精确计数。
三次400均只有一次上游尝试,fail 字段为 other,没有自动切到其他模型成功的尝试。
上下文积累与对照
- DeepSeek 会话反复预览和修改,记录有 58次 view_image 调用,未发现
compacted记录。第一条400的本地分项估算中,图片占92,800 tokens;这是本地估算,不是供应商的实际图片计费数。 - 客户端 token_count 的
model_context_window为950,000;Magpieroute.prompt.window为1,000,000。 - WorkBuddy Kimi(
workbuddy/kimi-k3.1-pro)对照会话4次路由均为200,最后成功输入41,265。第一轮提前结束,最终回复是尚未完成的英文设计思路;续轮才生成HTML。日志未证实这是网络断流。 - OpenAI
gpt-6-astra对照会话2次路由均为200,一轮完成,最后输入39,240。
三条聊天虽然初始提示词相同,但执行轨迹和上下文长度明显不同,不构成同等长上下文的模型上限对照实验,也不能据此证明Kimi能承载DeepSeek那份15万以上的历史。
这次现场能说明什么
- 今天再次出现本帖同款WorkBuddy超限报错,重发未恢复;同一会话切Gemini能回答,切回DeepSeek仍失败。
- DeepSeek曾在154,547输入、154,112缓存读取时成功,随后更大的请求被10万边界拒绝。这与 #3348 的缓存边界线索吻合,但没有上游内部缓存状态,不能确认缓存失效或账号亲和就是根因。
- 客户端记录的窗口远高于这次拒绝边界,且会话中未发现完成压缩的记录。
希望把这次现场作为本帖的新增复现证据,协助确认后续修复是否覆盖“已有缓存时超过10万可成功、后续超限后同会话持续失败”的路径。
由 Codex 辅助整理,依据真实本地日志;未重放模型请求,未修改模型、路由或压缩设置。
@tking1231 Thanks — this is a careful supplement, and every row of it lands on the same mechanism the trace above describes. Nothing here overturns that reading; two of your records sharpen it.
- 14:21:19 is the key line. 154,547 input with
cache_read=154,112answered 200 is the warm channel accepting well past 100,000, observed first-hand in this issue — until now that rested on the #3348 cross-reference alone. It pins100001 > 100000as the channel's cold-ceiling signature: the figure recurs verbatim in all four refusals across the two reports, so it is a boundary being reported, not a token-accurate count of the input — exactly the caution in your table. - The persistence half reproduces unchanged. Three 400s, each a single upstream attempt with
fail:"other", no hand-over, and the request did reach WorkBuddy every time (the first took ~74s to come back). That is the overflow path as traced: sorted as the conversation's own length, so nobody rests; not retryable, so no fail-over; the affinity stick untouched. Nothing magpie keeps is blocking the resend — each refusal simply leaves the cache as cold as it found it, and the next resend lands past the same ceiling. - The Gemini detour argues the boundary is the channel's, not the account's. Another model answered mid-thread, and returning to DeepSeek still refused; your Kimi and gpt-6-astra controls never reach the boundary at 39-41k. As you say yourself, they are not equal-length controls — but they don't need to be: hopping models neither shrinks DeepSeek's history nor warms its cache, which is why the switch-back refused at 224,174.
- The never-compacts wall shows up too. A client window of 950,000 against a ~100k cold ceiling, no
compactedrecord across 58 view_image turns — Codex sees no reason to compact, so the thread only grows. That client banner ("clear earlier history before retrying") is the standard overflow shape magpie relays; the deadlock is that Codex, told there are 950k of room, never acts on it by itself.
The one link still unobserved — the same residue in both reports — is why a turn meets the cold ceiling within seconds of a warm success: whether the big-jump turn's prefix diverges (images re-encoded, history rewritten) or the pool serves a node without the cache. That lives upstream of magpie's logs; it changes neither the handling trace nor the lever. And nothing in the Windows rerun introduces a platform variable: the routing-log shape is identical to the macOS one.
So, to your closing question: yes — the path you name ("warm past 100k succeeds; after one refusal the same session keeps failing") is precisely what the one proposed change would cover: learn the ceiling a refusal names (> N maximum) into the window magpie advertises to Codex, per provider and model and only where it undercuts the catalog's claim, so the client compacts before the boundary instead of never. That remains a product call for @yetone — the offer to build it behind a red test stands. Until then, the way out of a stuck thread is to shrink its history for real (switch to a big-window model, let it compact or clear earlier history, then return); a one-answer detour doesn't shrink it, as your 14:46 refusal shows.
补充一组 Magpie 0.1.3078 的复现:同一条 Codex Desktop 会话先以 24 万输入成功,随后被 WorkBuddy 以 10 万上限拒绝;只发一句简短的续做指令也未恢复。
- 环境:Windows 10 22H2(19045),Codex Desktop runtime
0.167.0-alpha.3.2,WorkBuddy 插件0.2.26。模型workbuddy/deepseek-v4.1-flash(max),路由/backend-api/codex/responses → /v1/chat/completions。 - 复现条件:一次较长的交互式 3D 开发任务,多次用截图检查画面。首次失败前会话累计回传 24 张截图。下面三条均来自同一会话、同一 WorkBuddy 账号。
| 请求时间(2026-11-10,UTC+8) | HTTP | 输入 token | 请求体(plain_bytes) |
耗时 |
|---|---|---|---|---|
| 13:59:55 最后一次成功 | 200 | 240,339(其中 240,128 为缓存读取) | 49,419,335 B | 33.5 s |
| 14:00:32 首次失败 | 400 | 220,287(Magpie 本地估算) | 52,239,458 B | 120.7 s |
| 14:18:25 短指令继续 | 400 | 220,506(Magpie 本地估算) | 52,240,249 B | 89.0 s |
两次失败均只有一次上游尝试(fail: other),上游原文相同:
WorkBuddy: prompt is too long: 100001 tokens > 100000 maximum
首个失败请求比上一次成功大 2,820,123 B,几乎等于最后一张截图编码后的增量。Codex 在发送前记录 auto_compact_scope_tokens=242245、token_limit_reached=false;其全局压缩阈值是 750,000,高于 Magpie 目录给这个模型的 272,000。未见压缩发生。续做时原历史仍在请求里,Codex 在开始执行工具前就再次终止。
这个现场与上面讨论的冷缓存边界相符,但日志无法确定为何在一次高缓存命中后立即触发,也不能把报错里的 100,001 当成实际输入计数,或把 100,000 当成模型的固定窗口。希望维护者能给这条明确的拒绝路径提供不必改 Codex 全局配置的恢复办法:例如在已配置压缩路由时明确引导到可承载这份历史的模型做压缩;没有可用压缩路由时,给出具体恢复提示,避免用户反复重发约 52 MB 的原历史。