Repository navigation
fix: normalize DeepSeek prompt_cache_hit_tokens into cached_tokens (#779) - #783
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-783Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr783.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] 🏷 PR Review — #783 (independently re-verified on Diff 整洁度(第一道关)✅干净。单 commit,6 files +116/−8,每一行都与 #779 直接相关:3 个源文件(改动点)+ 3 个测试文件。无跑题文件、无生成物/锁文件连带变更、无纯空格/重排噪音、未动 技术审查覆盖完整性 — 确认无遗漏解析点。 全仓 grep
其余命中均为 anthropic/responses 线路专属( 语义正确性:
本地复验(PR 分支上实跑):
无需代修的项。 唯一可挑剔的是 结论Diff 干净、覆盖完整、语义安全、测试与 CI 全绿。可以合并(合并操作按规约由人工执行:https://github.com/ranxianglei/billion-context/pull/783)。 中文摘要:修复了 OpenAI-chat 线路上 DeepSeek 顶层 |
Fixes #779
Problem
On the OpenAI-compatible chat wire, upstreams that report KV-cache hits via top-level usage fields
prompt_cache_hit_tokens/prompt_cache_miss_tokens(DeepSeek's documented format) had their hit count silently dropped: every OpenAI-chat-shaped usage parse site read onlyprompt_tokens_details.cached_tokens. On non-tool-call turns the upstream usage frame is re-synthesized byemitCompletion(), which only writesprompt_tokens_details.cached_tokenswhen a number was parsed — so hosts saw cache reads of 0 / 0% hit rate while the upstream cache was actually hitting.Change (pure fallback — standard field always wins)
src/loop/adapter-openai.ts— newopenaiCachedTokens(u)helper: standardprompt_tokens_details.cached_tokensfirst, then top-levelprompt_cache_hit_tokens. Used by both parse sites (trailingchoices:[]usage-only frame and finish-frame usage).src/plugin.ts— same fallback inusageFromSseEvent(plugin SSE path) and inpipePluginJson's non-stream usage chain (a 4th site the issue missed — the JSON path was affected too).src/util.ts—usageTotals()openai branch: same fallback, so per-round[acp-usage]logging andpromptInputTotalaccounting see the real cached subset.Semantics are safe: DeepSeek's
prompt_tokensalready includes hit+miss (like OpenAI), so normalizing the cached subset composes correctly with the #408 split-semantics guard. When the standard field is present, behavior is byte-identical to before.Tests (6 new, all green)
tests/loop-adapters.test.ts: full-loop drain asserting stats + synthesized completion carries normalizedprompt_tokens_details.cached_tokens; direct adapter test asserting standard-field precedence.tests/host-usage-postfold.test.ts: plugin SSE + plugin JSON paths count DeepSeek hit tokens into session stats.tests/usage-totals.test.ts: normalization + precedence at the totals level.Pre-flight
npm run typecheck— cleannpm test— 1517 pass / 0 fail / 2 skippednpm run build— successE2E_CHECK=1) — pass. The full 4-phase codex suite could not run here (needs the local upstream at127.0.0.1:8199, unreachable from this environment); the changed path is covered by the new adapter/plugin tests above.