Repository navigation
fix: report uncompressed baseline in host usage + clamp negative persisted token stats (#408) - #425
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-425Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr425.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] 🏷 Review complete — found and fixed one gap; PR updated with commit Verification (all on the PR branch):
Gap found in review: the description (and the #408 thread) state "each prepare* computes hostCreditTokens", but only Fix pushed (
Before merge (human side):
Minor (pre-existing on master, not introduced here): |
|
Reviewed against master @8496194 (v0.1.83). The design is sound and I verified it works on current master — I performed the merge onto master locally to test it (branch base is 470c43b, Aug 31, so it needs a nontrivial rebase). Result: Design — agree
Rebase conflicts — what master changed under this PR
Minor notes
Fixes #408 once rebased. Given the conflict volume, recommend rebasing onto master rather than merging; the semantic points above are the only traps. |
|
解决冲突 |
[bot] 🏷 收到,正在按你给的冲突地图 rebase 到 master @8496194(adapters 已解决;plugin.ts 的 |
80a9f89 to
ac2b75e
Compare
[bot] 🏷 Rebase 完成,已 force-push 到 冲突解决
rebase 后新发现并修掉的两处
验证(rebased 树)
e2e:本环境有 codex CLI 但默认 upstream(本地 sglang :8199)不可达,e2e 套件跑不了——merge 前仍建议在有 upstream 的环境跑一遍 分支已就绪,可以 review/merge。 |
ac2b75e to
6a32faf
Compare
…isted token stats (#408) - prepare* (anthropic/openai/responses) arm session.hostCreditTokens = est(original) - est(processed) each request; every host-facing usage outlet adds the credit back (adapters' raw SSE chunks, loop completion, plugin passthrough SSE/JSON, server JSON path) so the host anchors on the uncompressed baseline instead of the post-fold value - /acp panel contextTokens switches to hostContextTokens; pi agent panel shows the backfill line - persist: buildSession clamps negative lastInputTokens/contextTokens at restore; loaders rewrite stale files carrying negatives (one-time migration, logged) - openai split-semantics upstreams (prompt_tokens < cached_tokens) now add the cached segment back in usageTotals/recordUsage/applyUsageSample via shared promptInputTotal — fixes the ~0% context reading on cache-heavy anthropic-family upstreams served over an openai wire
6a32faf to
294f7c5
Compare
|
已接手收尾:rebase 到当前 master(v0.1.87 本次新增(对应 @wosnxn123 09-04 的 cache 证据)
三处接入同一规则:
内部账本 测试(tests/host-usage-backfill.test.ts,16→19)
Pre-flight
分支 squash 为单提交 范围外(按前述讨论,另开票跟进)
Closes #408 |
[bot] 🏷 独立复核完成,全部属实: 分支状态:远端 tip =
独立 pre-flight(本环境重跑):
范围外两项(anthropic 分支已就绪。剩下就是 human merge —— merge 后 |
Fixes #408. Two defects, two fixes.
1. Host usage backfill (main defect — host context accounting collapses to ~0). When bili folds compressed blocks out of the forwarded view, the provider's usage reports the POST-fold prompt, and the host (omp) anchors its context accounting on that value — status line shows ~0% and the auto-compact safety nets sleep. The issue's proposed fix (calling omp's
recordAnchoredHistoryRewrite) was withdrawn after line-by-line review: that API SUBTRACTS from the anchor (shake semantics — provider over-reports) and is not exposed on the extension surface; in the bili case the anchor is already post-fold-small, so calling it would make it smaller.This PR instead backfills the usage bili returns to the host: each prepare* computes
hostCreditTokens = est(originalMessages) − est(processedMessages)(same tokenizer both sides → systematic error cancels), and every client-facing usage path adds it back into the input-side field (openaiprompt_tokens+total_tokens, anthropic/responsesinput_tokens): loop completions (synthesized), raw passthrough events (openai real-tool finish chunk, anthropic first-round message_start), plugin SSE/JSON pipes, and the non-stream JSON path. The internal ledger (lastInputTokens) stays post-fold (unchanged); a new in-memoryhostContextTokens(last backfilled total) feeds the /acp paneltokenCount+contextTokensso panel == host footer. Trace: info log per armed request + /acp panel note line (host baseline: uncompressed (proxy backfilled +N tok)) — the degraded-host acceptance is covered without any host-side change: hosts without an add-back API simply receive the corrected usage directly.2. Persist restore clamp + one-time migration.
buildSessionnow wrapslastInputTokens/contextTokensinMath.max(0, …);loadAll/loadSyncdetect pre-clamp negative values on disk and rewrite the stale file once (log:one-time migration (#408): clamped … in N session(s) to 0). Old negative values (e.g.-54119) can no longer revive after upgrade; the /acp panel can no longer print negative percentages.Pre-flight: typecheck ✓; new
tests/host-usage-backfill.test.ts15/15 ✓ (backfill unit, responses loop, openai/anthropic adapter raw events, plugin SSE/JSON pipes, persist clamp+migration incl. flat v1); full suite 308 pass / 3 fail — the 3 failures are intests/proxy-codex-compact.test.tsand were verified pre-existing on clean master viagit stash(same failures without this diff); build ✓. No version bump (content branch).