Skip to content

fix: normalize DeepSeek prompt_cache_hit_tokens into cached_tokens (#779) - #783

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-15_deepseek-cache-hit-fallback
Sep 15, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-15_deepseek-cache-hit-fallback

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

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 only prompt_tokens_details.cached_tokens. On non-tool-call turns the upstream usage frame is re-synthesized by emitCompletion(), which only writes prompt_tokens_details.cached_tokens when 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 — new openaiCachedTokens(u) helper: standard prompt_tokens_details.cached_tokens first, then top-level prompt_cache_hit_tokens. Used by both parse sites (trailing choices:[] usage-only frame and finish-frame usage).
  • src/plugin.ts — same fallback in usageFromSseEvent (plugin SSE path) and in pipePluginJson'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 and promptInputTotal accounting see the real cached subset.

Semantics are safe: DeepSeek's prompt_tokens already 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 normalized prompt_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 — clean
  • npm test — 1517 pass / 0 fail / 2 skipped
  • npm run build — success
  • E2E zero-token preflight (E2E_CHECK=1) — pass. The full 4-phase codex suite could not run here (needs the local upstream at 127.0.0.1:8199, unreachable from this environment); the changed path is covered by the new adapter/plugin tests above.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-15_deepseek-cache-hit-fallback (9208026)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-783

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr783.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 Review — #783 (independently re-verified on 9208026, not taken on faith from the description)

Diff 整洁度(第一道关)✅

干净。单 commit,6 files +116/−8,每一行都与 #779 直接相关:3 个源文件(改动点)+ 3 个测试文件。无跑题文件、无生成物/锁文件连带变更、无纯空格/重排噪音、未动 version。

技术审查

覆盖完整性 — 确认无遗漏解析点。 全仓 grep prompt_tokens_details / cached_tokens / prompt_cache_hit_tokens / cache_read_input_tokens,OpenAI-wire 的 usage 读取点共 4 处,全部已修:

位置 路径 状态
adapter SSE usage-only 帧 + finish 帧 src/loop/adapter-openai.ts:124-128(新 helper 共用) ✅
plugin SSE src/plugin.ts:608 ✅
plugin JSON(issue 漏掉的第 4 点) src/plugin.ts:1245-1249 ✅
usageTotals() openai 分支 src/util.ts:86 ✅ 且经此传递覆盖 src/server.ts:4085(非流式 JSON stats)与 [acp-usage] 日志

其余命中均为 anthropic/responses 线路专属(cache_read_input_tokens、input_tokens_details),正确地在改动范围之外;preflight 不解析 cached tokens,无第 5 个漏网点。

语义正确性:

本地复验(PR 分支上实跑):

  • npm run typecheck ✅ clean
  • npm test ✅ 1517 pass / 0 fail / 2 skipped(与 PR 声明一致)
  • npm run build ✅
  • E2E_CHECK=1 preflight ✅(1 pass / 1 skip);完整 4 阶段 codex E2E 需要本地 upstream,本环境不可达(与 PR 描述一致)——但 GitHub CI 上的 real codex vs deterministic fake upstream (zero tokens) 检查已跑并且全绿(7/7 check runs success),该缺口已被 CI 补上。

无需代修的项。 唯一可挑剔的是 src/server.ts:4080 注释只列了各协议标准字段名、未提 DeepSeek 变体——但它紧接着就注明 "usageTotals() normalizes the per-protocol semantics",不算过时信息,不值得为此加 commit。

结论

Diff 干净、覆盖完整、语义安全、测试与 CI 全绿。可以合并(合并操作按规约由人工执行:https://github.com/ranxianglei/billion-context/pull/783)。


中文摘要:修复了 OpenAI-chat 线路上 DeepSeek 顶层 prompt_cache_hit_tokens 被丢弃导致 cache 命中恒报 0 的问题——在全部 4 个 usage 解析点加了"标准字段优先、顶层字段兜底"的归一,独立复核确认无遗漏点且与 #408 守卫组合正确,typecheck/test/build/E2E-preflight/CI 全绿,可以合并。

@ranxianglei
ranxianglei merged commit 467d8d9 into master Sep 15, 2026
7 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-15_deepseek-cache-hit-fallback branch October 15, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant