Repository navigation
fix(#767): image billing modes (pixels/bytes) end stale-baseline 502 loops on pixel-tile upstreams - #771
Conversation
…loops on pixel-tile upstreams On official Codex/OpenAI/Anthropic upstreams (pixel-tile billing), once a session's historical lastInputTokens baseline sits above the window, the default base64/4 image cost closes BOTH safe-forward paths at once (#496 forward-once requires a sub-window baseline; #300 stale-baseline fit requires payloadEstimate < limit) — preflight then fails 502 forever even though the images bill only a few thousand tokens upstream. - new global + per-provider imageBilling ("auto"|"pixels"|"bytes"), env BILI_IMAGE_BILLING overrides both (live-read); auto classifies first-party pixel hosts (openai.com / openai.azure.com / chatgpt.com / api.anthropic.com) - pixels mode parses the container header (PNG/JPEG/WebP/GIF/BMP, bounded decode — never the full body) and charges OpenAI high-detail tile billing (85 + 170x tiles = 765..2805 per image); unparsable formats fall back to a flat 16384 - bytes stays the conservative default for byte-counting relays; BILI_IMAGE_TOKEN_CAP still clamps both modes; remote URLs stay flat 4096 - regression tests: recovery e2e (stale 696789 baseline + two large screenshots forwards and recovers under pixels), bytes-mode control (same payload still fails fast, zero images forwarded), learned-limit-only variant, env precedence; typecheck + full suite (1438 pass) + build green
…RL at all gates imageBillingFor received origin-only URLs at four of five call sites, so a per-provider imageBilling override under a path-qualified provider key (the pattern CONFIGURATION.md documents) was only honored by the forward() overflow gate while preflight/clamps/guard silently fell back to auto classification. Route lookups now use route.rewrittenUrl like every other per-route setting (passthrough/compress/proxy). New e2e regression pin: path-qualified key + stale baseline recovers under explicit pixels; verified to 502 without the fix.
[bot] 🏷 Review of PR #771 (fix for #767) — one real wiring gap found, fixed directly on the branch as Verification (all run on the branch)
Fixed on the branch: per-route override invisible at 4 of 5 gates for path-qualified provider keys
Fix ( Noted, not blocking (pre-existing, left untouched)
Merge is yours — I can't merge PRs. 中文摘要: 修复了按路由 |
|
冲突 |
# Conflicts: # CHANGELOG.md # src/config.ts
[bot] 🏷 冲突已解决并推送到 PR 分支(merge commit master 自分支点以来前进了不少(kimi/jcode launcher、reasoning guard #739、session 加密 #708/#711、release v0.1.109,acp-kernel → 0.0.69),与 #771 的冲突只有两处,都是"同一位置各自加了不同内容":
合并后全量验证(deps 已按新 lockfile 重装): 中文摘要: 把最新 master 合入 PR 分支解决了两处冲突(config.ts 的 import 行与 CHANGELOG Features 顶部条目,均为双方各自新增、全部保留),验证了 image-billing 改动在合并后完整无损且 typecheck/测试/构建全绿,已推送,可以合并。 |
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-771Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr771.tgz
npm install -g packageThis comment is automatically updated on each push. |
Fixes #767
Problem
Follow-up to #496/#500. On official Codex/OpenAI/Anthropic upstreams (pixel-tile billing), once a session's historical
lastInputTokensbaseline sits above the window (reported: 696789 vs a 272000 window), the defaultbase64 length / 4image cost closes both safe-forward paths at once:lastInputTokens < limit(a sub-window baseline);payloadEstimate < limit— which base64/4 pushes past the window for large screenshots.So preflight fails 502
preflight_compress_failed("Images alone account for ~N tokens") forever, even though the images bill only a few thousand tokens upstream (the issue's repro: byte estimate ~300K vs real billing 2458/1910 per image). Text-only sessions already self-heal via #300; only image-bearing payloads were bricked.What changed
imageBillingsetting — global + per-providerproviders.<url>.imageBilling("auto" | "pixels" | "bytes"), envBILI_IMAGE_BILLINGoverrides both (live-read, no restart). Precedence: env > per-provider > global > auto host classification.pixelsmode: parses the container header (PNG/JPEG/WebP/GIF/BMP) with a bounded decode — never the full body — and charges OpenAI high-detail tile billing (512px tiles, short side scaled up to 768px, long side capped at 2048px → 85 + 170×tiles = 765–2805 tokens/image). Unparsable formats fall back to a flat 16384 — above both published first-party ceilings (OpenAI max 2805, Anthropic ~1534) yet far below base64/4 for any inline image over ~64KB.bytesremains the default — conservative relay protection unchanged (多模态会话上下文控制失效 + preflight 救援 502(codex responses store:false) #488/PR #489 后续: 默认图片计费 b64/4 在像素 tile 计费上游(官方 Anthropic/OpenAI)会硬 502 误杀多模态会话 #496 guarantees intact);BILI_IMAGE_TOKEN_CAPstill clamps on top of both modes; remote URLs stay flat 4096.autoclassifies by upstream host:openai.com/openai.azure.com/chatgpt.com/api.anthropic.comsuffixes → pixels, everything else → bytes. An official Codex route (https://chatgpt.com/backend-api/codex) therefore gets tile billing with zero config.Recovery is one turn: usage reports replace
lastInputTokensabsolutely. Per the issue's request, #496's one-rejection-then-block guarantee for genuine byte-relay overflow is preserved —noOverflowEvidenceis untouched; only the estimate layer changed.Tests
New
tests/issue767-pixel-billing.test.ts(9 tests):pixels, both images forwarded verbatim, baseline recovers below the window, steady state continues; identical payload under default bytes billing → 502 fail-fast, zero images forwarded (relay protection pinned); learned-limit-only variant closes forward-once too; env precedence check.Pre-flight:
npm run typecheck✓ ·npm test1438 pass / 0 fail (2 default-gated E2E skips) ✓ ·npm run build✓Docs: CONFIGURATION.md + CONFIGURATION.zh-CN.md (global & per-provider
imageBilling,BILI_IMAGE_BILLING, refreshedBILI_IMAGE_TOKEN_CAProw), CHANGELOG entry.For the reporter
Once released, the process-wide
BILI_IMAGE_TOKEN_CAP=16384workaround can be dropped — the official Codex route bills images by tiles automatically (auto). Keep explicit control if desired:"imageBilling": "pixels"on that provider, orEnvironment=BILI_IMAGE_BILLING=pixels.