Skip to content

fix #648: add hostUsageCredit switch to opt out of #408 host-usage backfill - #649

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-08_zcode-host-usage-credit
Sep 10, 2026
Merged

fix #648: add hostUsageCredit switch to opt out of #408 host-usage backfill#649
ranxianglei merged 2 commits into
masterfrom
2026-09-08_zcode-host-usage-credit

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Plain proxy clients (ZCode on the anthropic wire — base-url → proxy, no x-bili-plugin header, no special UA) fell through every #408 backfill exemption (pi/omp by pluginAgent, codex by UA in the in-flight #647) and got the full uncompressed-baseline backfill armed. Their UI then showed a cumulative, drifting baseline (real folded value + per-compression backfill) instead of the actually-forwarded context: it overstates real pressure, and the per-round est drift makes it non-monotonic on pure-append turns.

Add a hostUsageCredit config option (auto | off, default auto = current behavior). off disables the #408 backfill entirely so the usage reported to the host is the actually-forwarded (folded) request, matching [acp-usage] input=.

Changes

Why a switch (not a hard exemption)

The #408 backfill exists to keep a host's native auto-compact alive by showing the uncompressed baseline. Whether that helps or hurts depends on the host: pi/omp cancel it (exempted), codex keys off total_tokens which the backfill never touches (exempted in #647). For plain proxy clients like ZCode the value is host-specific and not knowable from the proxy — so an explicit switch (the issue's recommended primary mechanism) is the safe default rather than guessing. auto keeps today's behavior for everyone; ZCode users set hostUsageCredit: "off" (or BILI_HOST_USAGE_CREDIT=off) to get the actually-forwarded value.

Pre-flight

Fixes #648

@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

📦 Built Package Artifact

Branch: 2026-09-08_zcode-host-usage-credit (6fba944)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-649

Each push to this PR publishes a new version under the pr-649 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr649.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #649 审核完成 ✅

结论:技术正确、预检通过、无重复;按当前 auto 默认可直接合并。 我已补上缺失的 CHANGELOG 条目(见下)。

审核(在 PR head 上逐项重验)

1. 代码正确性

  • parseHostUsageCredit(src/config.ts):仅精确匹配 "off"off,其余(typo/大写/空串)一律 auto —— 与既有 parseUpstreamProxyMode 的严格匹配风格一致;loadOptions 接线 env.BILI_HOST_USAGE_CREDIT ?? fileConfig.hostUsageCredit(env 优先)正确。
  • armHostUsageCredit(src/server.ts:1822):先 hostCreditTokens = 0,再 if (hostUsageCredit === "off") return;(在 pi/omp pluginAgent 检查之前)→ off 彻底关闭回填。三个调用点(prepareAnthropic:1983 / prepareOpenai:2236 / prepareResponses:2490)全部传入 opts.hostUsageCredit
  • 消费侧(src/server.ts:3880 + src/loop/core.ts:182):credit = hostCreditTokens ?? 0,off 时恒 0 → backfillHostUsage 不触发 → 回报宿主 usage = 实际转发(折叠)请求 = [acp-usage] input=。与声明一致。
  • 两种压缩模式都覆盖:改动只影响"回报宿主的 usage",不碰 wire 消息重建。plugin 模式(pi/omp)本就被 pluginAgent 检查豁免,off 在那里是 no-op;proxy 模式 off 关闭回填。无 [Bug] ACP: ≥2 compressed blocks render as multiple system messages → SGLang (vLLM-compatible) upstreams return 400 #377 类单模式回归。

2. 预检(我在 PR head 重跑)

3. 重复判定 — 与在飞的 #647(codex UA 豁免,fix #645)互补而非重复:#647 按 UA 豁免 codex(其 total_tokens 不被回填触碰),#649 是通用配置开关覆盖 ZCode 等无稳定 UA/无插件头的普通代理客户端,语义正交。⚠️ 注意:两者都改 armHostUsageCredit(server.ts)+ config.ts + host-usage-backfill.test.ts + CONFIGURATION.md合并顺序决定谁需 rebase(先合的干净,后合的需 rebase),非本 PR 正确性问题。

待你拍板的一点(我给出明确建议)

建议:保持 auto 默认(即当前实现),不要翻 off 理由:

  1. 向后兼容 —— 不改变任何既有宿主(pi/omp/codex/其他普通代理)行为;
  2. 代理压缩不上报改写量,宿主上下文会计塌缩至 ~0(omps recordAnchoredHistoryRewrite 零调用);且持久化 lastInputTokens 恢复无钳制,旧版负值升级后复活 #408 回填的初衷是让宿主原生 auto-compact 看到未压缩基线而保持存活,对"确实依赖原生压缩"的普通代理客户端有价值,一刀切翻 off 会误伤;
  3. ZCode 用户显式 hostUsageCredit: "off" 即可拿到实收值,完全满足 ZCode 宿主 usage 回填未压缩基线:压缩后上下文显示成累加值,非实际转发量(v0.1.88 起;#645 的 ZCode 面) #648 验收;
  4. 若日后证据表明更多普通代理客户端需要 off,翻默认只是一行 follow-up。

你定。若拍板保持 auto,本 PR 现状即可合并。

小项

@ranxianglei

Copy link
Copy Markdown
Owner Author

解决冲突

ework-agent added 2 commits September 10, 2026 11:15
…ckfill

Plain proxy clients (ZCode on the anthropic wire, no plugin header, no
special UA) fell through every #408 backfill exemption (pi/omp by
pluginAgent, codex by UA) and got the full uncompressed-baseline
backfill armed. Their UI then showed a cumulative, drifting baseline
(real folded value + per-compression backfill) instead of the
actually-forwarded context — overstates real pressure, and the
per-round est drift makes it non-monotonic on pure-append turns.

Add a hostUsageCredit config option (auto|off, default auto = current
behavior). 'off' disables the backfill entirely so the host sees the
folded request's own usage, matching [acp-usage] input=. Documented in
CONFIGURATION.md (BILI_HOST_USAGE_CREDIT). Regression tests cover both
sides of the gate on the anthropic wire.
@ranxianglei
ranxianglei force-pushed the 2026-09-08_zcode-host-usage-credit branch from 1fd8806 to 6fba944 Compare September 10, 2026 03:18
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 ✅ 冲突已解决,PR 现在 mergeable: True(不再 dirty)。

冲突点(很小)

master 从 v0.1.95(5836f78)推进到 v0.1.99(6282f35) 后,唯一冲突在 CHANGELOG.md:上游在 [Unreleased] → ### Fixes 顶部新增了 #651 条目,和我上一轮加的 #648 条目落在同一位置。代码文件(config.ts / server.ts / tests / CONFIGURATION.md)rebase 时无冲突 —— 我上轮担心的 #651armHostUsageCredit 的改动与本 PR 不重叠。

解决方式

预检(rebased head 重跑)

现状

可以安排 review/合并了。

@ranxianglei
ranxianglei merged commit 175beb2 into master Sep 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZCode 宿主 usage 回填未压缩基线:压缩后上下文显示成累加值,非实际转发量(v0.1.88 起;#645 的 ZCode 面)

1 participant