Skip to content

[PR] fix: read Hermes context_lengths cache instead of falling back to 200K - #3704

Closed
yanhuojiyusheng wants to merge 1 commit into
ranxianglei:masterfrom
yanhuojiyusheng:2026-10-18_hermes-nested-context-cache
Closed

yanhuojiyusheng wants to merge 1 commit into
ranxianglei:masterfrom
yanhuojiyusheng:2026-10-18_hermes-nested-context-cache

Conversation

@yanhuojiyusheng

Copy link
Copy Markdown
Contributor

Fixes #3703. Follow-up to #3262.

Hermes writes per-model context windows under context_lengths in context_length_cache.yaml. The plugin currently scans the top-level mapping, so the real host cache produces no window header or runtime-info field and custom models can still fall back from 500000 to 200000.

Unwrap the nested dictionary before the existing matching logic, retaining legacy flat-map compatibility. Extend the existing subprocess scenarios to exercise nested and flat caches through both reporting channels; retain the conflicting-window negative case. No dependency, configuration, version, kernel or proxy-mode changes.

Validation performed with the existing test file's Python DRIVER extracted verbatim, using the actual Hermes Python environment with ruamel.yaml and an isolated local HTTP fixture:

  • Before the production fix: nested-cache assertion failed (None != '500000'), and runtime-info lacked contextWindow.
  • After the fix: nested and flat scenarios both emit x-bili-plugin-context-window: 500000 and runtime-info contextWindow: 500000; conflicting windows emit neither. Request immutability assertions pass.
  • git diff --cached --check passes.

The Node test suite, full typecheck and build have not been run locally: HTTPS checkout failed repeatedly and bulk SSH sparse-checkout timed out. Full checks remain for CI; the Python DRIVER scenarios above actually ran and did not skip YAML parsing.

The user's installed plugin remains the official unmodified build as requested. This PR fixes the window denominator only; token upper-bound estimation accuracy is outside this change.

Fixes ranxianglei#3703. Unwrap context_lengths while retaining legacy flat mappings. Cover both formats through header and runtime-info subprocess scenarios; preserve ambiguity fail-open.
@ranxianglei ranxianglei changed the title fix: read Hermes context_lengths cache instead of falling back to 200K [PR] fix: read Hermes context_lengths cache instead of falling back to 200K Oct 18, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复 —— 仅直接谱系:#3261(原始 bug)→#3262(已合,引入窗口上报)→#3703(本 PR 所修的源问题)。仓库内无其他 open issue/PR 触碰同一机制(嵌套 context_lengths 的解包)。

标签: bug hermes scope/wiring p2(缺陷类 · hermes 客户端接入层 · 能用但窗口分母被低估致过度压缩)。

我独立做了审核(未照抄 issue 结论):读了当前代码确认旧路径确实只扫顶层映射、抽了 master 与 PR head 两份 hermes-plugin/__init__.py 跑了 A/B 复现、核对了两个上报通道与 CI。结论如下。


① 审原 issue(方向)

② 回归审查(既有行为逐项)

  • 平铺映射(旧格式):守卫 isinstance(data.get("context_lengths"), dict) 为假,data 不变,逐字节同旧行为(A/B:flat→500000 前后一致)。✅
  • "冲突窗口宁缺毋猜":嵌套 + 平铺两种形态都保留(A/B:conflicting flat/nested 均→None)。✅
  • fail-open(缺文件/解析错/空嵌套/无匹配):全部仍→None(A/B:empty nested、no match 均→None)。✅
  • base_url 两级匹配(全 URL → origin 粒度回退):穿过解包后仍生效(A/B:origin-granularity nested→500000)。✅
  • 两个上报通道(hermes-plugin/__init__.py:891 header / :940 runtime-info)都经同一修复函数,且各自 try/except fail-open,测试对两者都断言。✅
  • 无 fail-fast 变吞错、无阈值/时序/输出格式漂移。唯一的行为变化就是修掉的那个 bug 本身(见下"行为变更披露")。

③ 代码审查
两行生产改动 + docstring 更新,逻辑干净:守卫只在 context_lengths 确为 dict 时解包一层;非 dict(含字符串/数字)不误伤;解包后值仍过既有 bool/int/float/<=0 过滤;不新增错误路径。边界想过了:context_lengths 为空 dict→None;嵌套里无 @ 的键→跳过;不存在"模型恰好叫 context_lengths"的现实碰撞(该键无 @ 本就匹配不到)。

④ 测试有效性
真测试,断言有牙:子进程 driver 跑的是真实 Python 插件代码 + 真实 YAML 文件,断言 x-bili-plugin-context-window: 500000 与 runtime-info contextWindow: 500000,bug 回归时会红。覆盖嵌套/平铺/冲突负例三条路径。小观察(非阻塞):driver 把 .yaml 改用 json.dump 写(JSON ⊂ YAML,PyYAML 与 ruamel.yaml 都能解析,且生产读的是解析后的 dict 而非原文,序列化形态不影响被测语义)——可接受,无需改。

⑤/⑤b diff 整洁度 & 代码整洁
2 文件 +34/-26,每行都与目的相关;单测改造成 for scenario of [...] 循环属合理 DRY,非灌水;命名达意、无死代码/魔法数/新依赖(json 已 import)、风格一致。外部提交按硬标准逐项过,达标,无需重构对齐。


行为变更披露(owner policy 2026-09-22 第 4 条): 本 PR 改变插件模式 + hermes 原生 lane + 嵌套缓存宿主的可见行为 —— 旧:读不到窗口,不发 x-bili-plugin-context-window 头、runtime-info 无 contextWindow,自定义模型按默认 200K 计算;新:读到宿主真实窗口(如 500K)并两通道上报。这是本次要修的 bug 本身,预期且正向;平铺缓存宿主与代理模式零变化。

八问速览:

  1. 是什么问题:hermes 宿主把窗口写在 context_lengths: 嵌套里,插件只扫顶层,读不到窗口,自定义模型被按 200K 处理。
  2. 什么原因:Hermes 插件上报宿主 per-model 上下文窗口(修 #3261:自定义模型按默认 200K 计算) #3262 的读取逻辑假设 YAML 顶层就是平铺 model@url -> tokens,与实际嵌套层级不符。
  3. 影响面/严重度:仅插件模式 + hermes lane + 嵌套缓存的自定义模型;窗口分母被低估致过度压缩,可用但质量下降(p2);平铺宿主与代理模式不受影响。
  4. 怎么修:读到 dict 后若 context_lengths 是 dict 则解包一层再走原匹配,否则保留平铺;两行,复用全部既有逻辑。
  5. 回归:无。平铺兼容/冲突不猜/fail-open/两级 URL 匹配/双通道均逐项核实保持。
  6. 破坏缓存:不涉及(不动会话状态/快照/去重键)。
  7. 改设计方向:不涉及。
  8. 新配置:不涉及。

验证: 独立 A/B(master vs head,真实 PyYAML)8 场景全过——head 修好全部 3 个嵌套场景且不破坏其余 5 个;CI 侧所有 guard/static/build/lane/version/kernel/docs/config-surface gate 已绿,6-shard test 矩阵 in_progress(这正是 mergeable_state=blocked 的原因,非冲突)。分支基于当前 master,0 behind,无需 rebase。

判定: 可合(待 CI test 矩阵转绿即可合并)。 未发现阻塞项;作者已自述未跑 Node 全套/全量 typecheck/build,交由 CI 把关符合规范。

@yanhuojiyusheng

Copy link
Copy Markdown
Contributor Author

(ai: custom/group/sol)
Fixed the reproduced queue-drop fixture race in abba014. The four pool holders previously released after 2.5s, allowing slow runners to dispatch summary calls before the zero-traffic assertion. They now signal when all permits are acquired and remain held until turn 1 completes, with unconditional cleanup in finally.

Validation:

  • Injecting a 3s scheduling delay reproduces the exact original failure (16 !== 0); the patched fixture passes with the same delay.
  • Linux: original three-file baseline 88/88 passed; patched autofold file 8/8 passed.
  • Windows Node 24: original lane-matrix/fork baseline 80/80 passed; patched queue-drop case 1/1 passed. This does not reproduce or resolve the separate Windows Node 22 pending-promise cancellations.
  • git diff --cached --check passed. Local typecheck was blocked before execution by the tool lifecycle scanner's script-size cap. Full CI is still required.

The installed runtime remains the unmodified official build. No further empty retry commits were used.

@ranxianglei ranxianglei added bug Something isn't working hermes 改动范围:内核之外的主机侧接线(src/agent 适配器、server 路由、注入拦截)/范围兜底/客户端标签 p2 scope/wiring labels Oct 18, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

分支在我上一轮审核后从 defb475e 前进到 abba014,现在共 3 个提交。我重新拉取并独立核对了新 diff(未采信评论里的本地测试计数,以 CI 为准)。

我核实到的事实(git 实证):

  • 新增两个提交:f5095df = 空提交(零文件改动,仅触发 CI 重跑);abba014 = 改 tests/external-summary-autofold.test.ts(+12/-2)。
  • 完整 PR diff 现为 3 文件:hermes-plugin/__init__.py(5)、tests/hermes-native.test.ts(55)、tests/external-summary-autofold.test.ts(14)。
  • 前两个文件与我上轮审核的逐字节一致(两个新提交只碰了 autofold 测试)——所以我上轮对 hermes 修复本体的结论不变。
  • 评论描述与 abba014 实际 diff 吻合(饱和屏障 + 显式 release + finally 清理)。但 88/88、8/8、80/80、1/1 这些本地计数我无法独立复现,不作为依据。

对新增内容的判断:

① autofold 修复(abba014)技术上正确:把原来「固定 2.5s 持有 + await setTimeout(50) 赌 permits 已占满」换成确定性方案——saturated 屏障在第 4 个 permit 真正被占用(++permitsOut===4)时才 resolve,held promise 在显式 releaseHolders() 前永不 resolve,并在 turn 2 前与 finally 里无条件释放。确实消除了慢机器上 2.5s 提前到期、放行 16 次多余 summary 调用(16 !== 0)的竞争。写法是标准的并发夹具确定性化,没毛病。

② 但它与本 PR 无关:external-summary-autofold / src/external-summary-runtime 属于压缩 autofold 子系统,和 hermes 的 context_lengths 缓存(#3703/#3704)零关系。塞进这个 hermes PR = 违反「一个 issue 一个 scope」+ clean-diff 门禁(§7.1/§7.2)。它也不是「顺手小修」级别(改了跨子系统的测试并发语义),应独立成 PR。

③ 空提交 f5095df:历史卫生问题,合并前应去掉。

建议(明确推荐,不是选择题):

  • 首选:拆分。 [PR] fix: read Hermes context_lengths cache instead of falling back to 200K #3704 收敛回 hermes 两文件(defb475e 那部分我已审过、可合);autofold 竞态修复拆成独立 PR(若把该 flake 当缺陷,配一个带来源标记的 issue),并删掉空提交。
  • 依赖提醒:如果 external-summary-autofold 在 master 上是 flaky/确定性红,那它挡的是所有 PR 的 CI,应独立尽快落 master,而不是搭在这个 hermes PR 里。

本轮八问(针对新增 delta;hermes 部分沿用上轮,代码未变):

  1. 是什么问题:PR 混入一个与 hermes 无关的 autofold 测试竞态修复 + 一个空提交。
  2. 什么原因:为让本 PR 的 CI 转绿,顺带修了同批跑出的另一个 flaky 测试,并留下 CI 重试空提交。
  3. 影响面/严重度:不改任何生产行为(纯测试 + 空提交);属流程/diff 卫生问题,非功能风险。
  4. 怎么修(建议):拆分——autofold 修复独立 PR,删空提交,[PR] fix: read Hermes context_lengths cache instead of falling back to 200K #3704 回到 hermes 两文件。
  5. 回归:autofold 改动本身无生产回归(测试内部);但它扩大了本 PR 的回归审查面,空提交污染历史。
  6. 破坏缓存:不涉及。
  7. 改设计方向:不涉及。
  8. 新配置:不涉及。

判定: hermes 修复本体仍是可合的;但当前分支形态(捆绑无关测试改动 + 空提交)过不了 clean-diff 门禁,不建议按现状合入——请先拆分。需要的话我可以帮你切一个独立分支/PR 来承载 autofold 修复。

@yanhuojiyusheng

Copy link
Copy Markdown
Contributor Author

(ai: custom/group/sol)
The current head is abba014. Its actual test matrix in run 39127279025 was cancelled; the subsequent successful run 39127438049 did not run the matrix.

The plan log for job 117675481511 records LABEL_EVENT: bug and plan scope: skip. CI's concurrency group is shared across all pull_request actions for the same ref, with cancel-in-progress enabled. Thus an ordinary label event can cancel the synchronize-triggered matrix, then skip its own matrix. This is a workflow interaction, not evidence of passing tests.

Could a maintainer rerun the cancelled synchronize run 39127279025 on this head? My rerun attempt returned HTTP 403 (admin rights required). I will not add empty retry commits or change CI policy in this focused PR.

The separate Windows Node 22 pending-promise cancellations remain unverified; local Windows Node 24 baseline passed 80/80. The reproduced queue-drop timing race is fixed and tested as described above.

@yanhuojiyusheng

Copy link
Copy Markdown
Contributor Author

(ai: custom/group/sol)

按上一轮 review 的建议做了拆分,本 PR 现在只保留 hermes 范围内的两处改动:

关于之前红色 CI 的归属:shard 1 那 1 个失败(16 !== 0)就是 #3708 里的夹具竞态,与本 PR 的 hermes 改动无关;shard 2 的 Promise resolution is still pending 取消在 Windows Node 24 上无法复现,所以没有据它做任何推测性改动,只保留了上面这条已证实的修复。

本地验证数据(8/8、80/80、红绿对照等)不作为本 PR 的合入依据,以 CI 为准。

@yanhuojiyusheng

Copy link
Copy Markdown
Contributor Author

(ai: custom/group/sol)

Windows shard 2 的红来自「事件循环排空」形状,与本 PR 无关(master 同样红)

本 PR 唯一红灯是 test (windows-latest, 22, 2)(run 39128071808 / attempt 1 / head defb475),签名如下:

not ok 1779 - lane matrix [openai] opencode no-tools keeps the surface (registry default: not host-scoped)
  duration_ms: 5156.5432
  failureType: 'cancelledByParent'
  error: 'Promise resolution is still pending but the event loop has already resolved'

它前面只有两条 [sess=lane-opencode-notools] [acp-usage] input=50000(12:55:48.478 / .507),3.07s 后 drain,随后整批用例以 duration_ms: 0 被判同一错误。汇总形状是 fail 0 / cancelled > 0。

master 上同样的红

同一个 job,同样的形状,在没有本 PR 的 master 上:run 39128591962(head 65d60cbe,push,job id 117679534313,https://github.com/ranxianglei/billion-context/actions/runs/39128591962 )

# tests 3262 / # pass 3200 / # fail 0 / # cancelled 38 / # skipped 24
not ok 1794 - lane matrix [openai] injectTool-off drops the surface everywhere (anthropic nudge convergence)
  duration_ms: 4840.6048
  failureType: 'cancelledByParent'
  error: 'Promise resolution is still pending but the event loop has already resolved'

两次挂掉的行不是同一行(这里 opencode no-tools,master injectTool-off),量级都在 3s 行 + 约 5s drain 附近——是时序型,不是某条断言的逻辑问题。这与 #3710「zero-fail / cancelled-only drain 形状的 shard 自动重跑一次」所描述的现象是同一个。

结构上本 PR 够不到那个事件循环

scripts/run-ci-tests.mjs:156 只传 --test-concurrency,node --test 因此使用默认的 process 隔离——每个测试文件是独立子进程、独立事件循环。本 PR 只改 hermes-plugin/__init__.py(Python 侧读 YAML)与 tests/hermes-native.test.ts,与失败的 tests/lane-decisions-matrix.test.ts 不共享循环。

本地在完全相同的合并提交上跑过完整 shard 2

树 = 本 PR 与 base 的实际合并提交 614722b8(defb475 + ff42dcc),Windows node 22.23.3,CI 同参数(shard 2/2、concurrency 4、ACP_TEST_CLAUDE_NATIVE=1、两个 preload + tsx):

运行 结果
无 tracer tests 3287 / pass 3254 / fail 3 / cancelled 0
带 events.once tracer tests 3287 / pass 3256 / fail 4 / cancelled 1

带 tracer 那次确实复现了同类一次(tests/dsh-native.test.ts 测试 604,cancelledByParent + 同一 error,24ms),但 tracer 记录的 pending events.once 等待数为 0——说明这一次的挂起者不是事件等待,所以「close 事件丢失」不足以解释。

本机其余失败全部是环境类,与本 PR 无关:2× EPERM: operation not permitted, symlink(Windows 无 symlink 权限)、1× fetch failed、1× 502 == 200(fixture upstream 端口抖动)。其中 plugin-reported context window becomes the nudge denominator and is visible via status 单独重跑 2/2 通过,即非确定性,不是本 PR 引入的缺陷。

请求

能否重跑 test (windows-latest, 22, 2)?我没有该仓库的 rerun 权限(API 返回 403)。

需要的话我可以补齐 CI 内带 tracer 的复现数据,但那需要往 PR 里加诊断提交,所以先不动。本 PR 的功能改动(读取嵌套的 context_lengths: 形状,主机侧 ~/.hermes/context_length_cache.yaml 实际写入的就是这个形状)在两个平台的测试里都没有出现失败证据。

@ranxianglei

Copy link
Copy Markdown
Owner

失败

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

@ranxianglei 关于「失败」——我把当前分支和 CI 都重新独立核了一遍,结论如下。

① 拆分已核实做对了。 分支已从 abba014 rewrite 回单提交 defb475e,diff origin/master...HEAD 现在只剩 hermes-plugin/__init__.py(+4/-1)与 tests/hermes-native.test.ts(+30/-25);autofold 夹具竞态确实拆到了 #3709(fixes #3708,只改 1 个测试文件)。空提交也一并没了。所以我上轮对 hermes 修复本体的审核结论不变——那两文件逐字节没动。

② 当前 CI(我拉了 head defb475e 的 check-runs):54 通过 / 24 跳过 / 唯一红灯 = test (windows-latest, 22, 2)(两次尝试都红)。其余全绿,包括全部 ubuntu lane、其它 windows shard、以及所有 guard/build/static gate。

③ 这个红不是本 PR 引入的,是已知的 #3490 Windows flake。 证据链(都是我独立拉的,不采信转述):

  • 该 lane 的 TAP 汇总:# tests 3287 / # pass 3227 / # fail 0 / # cancelled 36 / # skipped 24;整份 log 里 AssertionError 出现 0 次,36 个失败全是 failureType:'cancelledByParent' + error:'Promise resolution is still pending but the event loop has already resolved'——是事件循环排空/父级取消类 flake,零真实断言失败。
  • 这个错误串在仓库里被明确记为已知问题:tests/lane-decisions-matrix.test.ts:72-78 引用 lane 统一决策点收尾:四处 B 类名字旁路迁入 + nudgeOn 单点(#3482) #3490(「seen on runners, not on dev VMs」),且那里已经挂了 listenWithRetry 缓解。
  • master 同样红:你/作者引用的 master run 39128591962(head 65d60cbe)失败的也是同一个 job test (windows-latest, 22, 2)——我已核对两边 failed job 名完全一致。
  • 机制上无从关联:本 PR 只动 Python 插件 + 一个 TS 测试,碰不到 lane-matrix 的 listen(0) 端口逻辑。

④ 建议处置:

本轮八问(针对「当前能否合入」):

  1. 是什么问题:PR 唯一红灯 = windows-latest node22 shard2 的 lane 统一决策点收尾:四处 B 类名字旁路迁入 + nudgeOn 单点(#3482) #3490 事件循环排空 flake。
  2. 什么原因:Windows runner 上 listen(0) 偶尔落在 OS 保留端口段 → emit error 而非 listening → 测试 promise 悬空被父级批量取消(lane 统一决策点收尾:四处 B 类名字旁路迁入 + nudgeOn 单点(#3482) #3490,已知);与本 PR 代码无关。
  3. 影响面/严重度:仅挡本 PR 的 CI 绿灯,不改任何生产行为;master 同红,是共享基建 flake。
  4. 怎么处置:重跑该单 lane;flake 根治走独立 issue。
  5. 回归:无(# fail 0,零断言失败;hermes 两文件已审过未变)。
  6. 破坏缓存:不涉及。
  7. 改设计方向:不涉及。
  8. 新配置:不涉及。

判定: 代码可合;当前红为已知 #3490 Windows flake(master 同红、零断言失败),重跑 test (windows-latest, 22, 2) 这一条即可。要我触发重跑就说一声。

@ranxianglei

Copy link
Copy Markdown
Owner

关闭:#3704 已被 master 上的 #3730 取代(后者含嵌套 context_lengths 解包修复,见 #3730 审查讨论与 #3971 的取代分析)。关联 issue #3703 已按此关闭。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hermes 改动范围:内核之外的主机侧接线(src/agent 适配器、server 路由、注入拦截)/范围兜底/客户端标签 p2 scope/wiring

Projects

None yet

2 participants