Skip to content

Hermes 插件不上报 contextWindow:宿主已知窗口丢失,自定义模型按默认 200K 计算(pi 同代理正常) #3261

Closed

Description

@yanhuojiyusheng

环境

  • bili 主程序 0.1.197(npm latest);Hermes 插件目录标记 0.1.196,其 __init__.py 与仓库 HEAD hermes-plugin/__init__.py 逐字节一致(SHA256 8f6b2c2dbef1da911682b9d8b7665bcaace23249a849f3e78fdf474c469f934e,35966 bytes),即 HEAD 代码复现。
  • 客户端:Hermes(原生插件模式)。模型 group/sol(自定义 provider 别名,上游本地中转 http://127.0.0.1:3425/v1)。
  • 对照组:同一台机器、同一代理实例下,pi 客户端正常。

现象

Hermes 会话按默认 200K 计算上下文预算;pi 会话正常按客户端上报的窗口计算。本机代理日志(UTC):

[window] model=group/sol source=default native=none base=200000 ... fallback=true
warning agent=hermes sent no context window
x-bili-plugin-context-window absent, no matching runtime-info

而 Hermes 宿主自己明确知道这个模型的窗口:HERMES_HOME/context_length_cache.yaml 里有 group/sol@http://127.0.0.1:3425/v1: 500000(UI 也显示 500K)。换个说法:窗口数据在宿主侧存在,但整条上报链路上丢了;group/sol 又是自定义别名、不在模型目录里,所以注册表兜底也解析不到,native=none → 回落 base=200000。

根因(插件侧代码)

hermes-plugin/__init__.py 只盖了 model/max-output 两个通道:

  1. 请求头盖章处(L768-779):只设置 x-bili-plugin、x-bili-plugin-conversation、x-bili-plugin-model、x-bili-plugin-max-output,从不设置 x-bili-plugin-context-window(全文件 grep context-window / contextWindow 零命中)。
  2. report_runtime_info()(L787-808):body 只有 {agent, model, source, maxOutput},没有 contextWindow 字段。

于是 proxy 的 native-window 链路对 Hermes 永远拿不到 client-config 层的真实值(头通道在 pluginHeadersMatchModel 的严格匹配下也被整体丢弃),只剩默认值。

关联

建议修复方向

备注 / 不确定性

  • 500K 是 Hermes 端配置/识别的值;该自定义上游的真实硬上限未独立验证(另外 UI 显示的是扣除输出预留后的可用输入预算口径)。

Activity

  1. ranxianglei commented on Oct 15, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    查重: 不重复。检索了 open+closed issue(hermes context window / sent no context window / runtime-info contextWindow / context-window header 等关键词,0 命中);#2381/#2382 是 DSH 按 (provider, model) 键控的另一类根因,本 issue 是 Hermes 插件根本没有 contextWindow 通道,机制不同。

    已核实你的根因分析成立:

    1. hermes-plugin/__init__.py 全文件 grep context-window/contextWindow/context_window 零命中——头盖章处(on_llm_request)只盖 plugin/conversation/model/max-output 四个头;report_runtime_info() 的 body 只有 {agent, model, source, maxOutput}。
    2. proxy 侧两条链路本身是通的、不需要改:x-bili-plugin-context-window 头(src/plugin.ts 的 pluginReportedContextWindow + pluginHeadersMatchModel 门控)和 runtime-info 的 contextWindow 字段(handlePluginRuntimeInfo 已解析并参与 native-window 链)都已实现——pi 正是走这两条路才正常的(src/agent/pi.ts 从 ctx.model.contextWindow 取值盖章)。缺的只是 Hermes 插件侧「把宿主真值带上来」这一环。

    修复方案(纯 Python 插件侧改动,零 TS 变更):

    • 新增 best-effort 读取 HERMES_HOME/context_length_cache.yaml(你证据里宿主存真值的地方),复用插件既有的双 YAML 后端解析模式(PyYAML/ruamel,[Bug] mitm_domains_from_config() bare 'import yaml' returns [] when PyYAML is absent -> silent blind tunnel (ruamel.yaml fallback suggested) #2838),任何异常 fail-open 回退现状;
    • 键按 <model>@<base_url> 精确匹配;base_url 优先取 hook 上下文里现成的 URL 字段,否则用 hook 的 provider 参数从 config.yaml providers 表解析(与既有 mitm_domains_from_config() 同源同键名);base_url 未知时只有该模型所有条目指向同一数值才上报(宁可不上报也不报错窗口——报大导致 preflight 溢出、报小导致过度压缩,都比默认 200K 更糟的情况要避免);
    • 查到后双通道上报:on_llm_request 盖 x-bili-plugin-context-window 头 + runtime-info body 加 contextWindow 字段(去重键同步纳入窗口值,窗口变化会重新上报)。

    一个口径说明:缓存值若是宿主「扣输出预留后的可用输入预算」口径(你在备注里提过),bili 还会再预留输出空间,等于略偏保守地提前压缩——方向安全,但会在 PR 描述里写明。

    开始实现,完成后带 PR 回复。

  2. ranxianglei commented on Oct 15, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    修复完成 → **PR: https://github.com/ranxianglei/billion-context/pull/3262**(`Closes #3261`,CI 结果出来前先报状态:定向测试本地 30/30 全绿、typecheck 干净,全量回归以 CI 为准)。

    结构化八问:

    1. 是什么问题:Hermes 原生插件上报链路里根本没有 context window 这一项——宿主明明知道模型真实窗口(500K),代理却只能按默认 200K 算会话预算;pi 正常是因为 pi 两条上报通道都盖了。
    2. 什么原因导致的:插件侧实现缺口,非 proxy 缺陷。proxy 的头通道(x-bili-plugin-context-window)和 runtime-info 的 contextWindow 字段本就完整可用([native] runtime-info protocol: plugins report real model config (model / contextWindow / maxOutput) to the proxy #955/feat: runtime-info protocol — plugins report real model config to the proxy (#955) #956 协议),但 hermes-plugin/__init__.py 只盖 model/max-output 两个通道,宿主真值没有任何一条路被带上来。自定义别名又不在注册表里,于是整条 native-window 链落到默认值。
    3. 影响范围与严重程度:所有「Hermes 原生插件模式 + 自定义 provider 别名/自托管模型」的会话——nudge 分母和压缩预算全部按错误窗口计算(偏小→过度压缩、偏大→preflight 风险)。目录内知名模型不受影响(注册表兜底能解析到)。属特定条件下的功能性错误,不是数据损坏。
    4. 怎么修复的:纯 Python 插件侧改动(零 TS 变更)。插件 best-effort 读宿主的 HERMES_HOME/context_length_cache.yaml(键 <model>@<base_url>,复用既有双 YAML 后端模式),base URL 用 hook 现成字段或 provider 参数查 config.yaml 解析;匹配两级(归一化全 URL → origin 粒度,覆盖缓存带 /v1 而配置省略的差异);同模型多上游窗口冲突时宁缺毋报。查到后双通道上报:头 + runtime-info(去重键纳入窗口值,变化会重报)。任何缺失/解析失败/歧义一律 fail-open 回现状。
    5. 是否有回归问题:未命中路径行为与现状逐字节一致(新增测试钉住 fail-open:无 cache 文件 → 无头、runtime-info body 形状不变)。顺手修了一个同路径既有缺陷并独立标注:on_pre_api_request 里 table 变量只在 max_tokens 存在的分支内绑定却无条件读取——无 max_tokens 触发时 NameError 被吞掉、整条 runtime-info 静默丢失;已提升绑定。该修复只让原本丢失的上报恢复,无新风险面。
    6. 是否破坏缓存:不涉及压缩缓存/增量游标。runtime-info 去重键从二元扩为三元是向后兼容的扩展(旧值组合只是多一个 None 维度)。
    7. 是否改了设计方向:没有。完全按 [native] runtime-info protocol: plugins report real model config (model / contextWindow / maxOutput) to the proxy #955/feat: runtime-info protocol — plugins report real model config to the proxy (#955) #956 既有协议补上 Hermes 缺失的一环,与 pi 实现对齐;窗口优先级链不变(client-config 最高)。
    8. 是否引入了新的配置:无新 env/config/flag。用户侧生效方式:发版后跑一次 bili plugin update hermes 刷新插件副本即可。

    一个口径备注(已在 PR 描述披露):若宿主缓存值是「扣输出预留后的可用输入预算」口径,bili 再预留输出空间会略偏保守地提前压缩——方向安全。

    发版合并后请在你机器上 bili plugin update hermes 验证一下 group/sol 会话日志里的 [window] 行应显示 source=client-config base=500000。

  3. ranxianglei commented on Oct 15, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    CI 全绿:PR #3262 的 39 个 check run 全部完成、零失败(ubuntu/windows × node 22/24 共 12 个 test shard + static-gates/kernel-guard/config-surface-guard/docs-sync 等全部门禁)。

    合并判定:可合。合并后在你机器上 bili plugin update hermes,group/sol 会话日志的 [window] 行应变为 source=client-config base=500000(若宿主缓存值口径与预期不符,把那一行日志贴回来我再核)。

  4. added a commit that references this issue on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions