Skip to content

[PR] fix(hermes): bootstrap the proxy off the host's load deadline - #4642

Closed
yanhuojiyusheng wants to merge 2 commits into
ranxianglei:masterfrom
yanhuojiyusheng:2026-10-25_hermes-bootstrap
Closed

yanhuojiyusheng wants to merge 2 commits into
ranxianglei:masterfrom
yanhuojiyusheng:2026-10-25_hermes-bootstrap

Conversation

@yanhuojiyusheng

Copy link
Copy Markdown
Contributor

Fixes #4641

问题

Hermes 冷启动后 /acp、/acp-cache 与 bili 工具整体消失(宿主 capability 检查 deny)。根因不在插件注册顺序,而在宿主加载器的 10s 预算:超时后 _dispose_registrations() + _forget_registrations() 会把整个插件判失败并回收已注册项,之后任何 register_tool 都是空操作。旧版 register() 同步跑 ensure_origin(10s)+ apply_env + manifest(5s),冷启动必超。

文件

hermes-plugin/__init__.py

  • register(ctx) 只做廉价注册(middleware、pre_api_request、/acp、/acp-cache、on_unload)+ ready_event.wait(REGISTER_WAIT_S = 4.0),低于宿主 10s 预算;
  • 慢活搬到后台 daemon 线程:_bootstrap() 用单一单调 deadline BOOTSTRAP_BUDGET_S = 90.0 贯穿 attach 轮询 / marker 仲裁 / spawn / 健康探测(原实现按阶段各算一份等待,最坏情况相加);
  • 首个模型请求由 on_llm_request 等就绪,上限 FIRST_REQUEST_WAIT_S = BOOTSTRAP_BUDGET_S + 15.0;等不到则以 proxy 模式继续并明确告知,不再静默直连;
  • 同一路径上的必然缺陷一并修:未定义的 BOOTSTRAP_SPAWN_WAIT_S / FIRST_REQUEST_WAIT_S、discover_instance 内 _stopped(stop) 的 TypeError(真机每次挂接必败)、_stopped() 与 register_tool / apply_env 的 TOCTOU、re-enable 时 stop_event 未重置导致新一轮 bootstrap 立即自杀。

tests/hermes-native.test.ts:新增 slow-boot-restart 与 re-enable 场景;改动前用 mod.BOOTSTRAP_SPAWN_WAIT_S = … 动态赋值掩盖了未定义常量,现改为真实常量名。

两种压缩模式

只动插件侧加载/引导时序与 env 注入时机,不动 wire 形状、不动 acp_summary 处理,plugin 模式与 proxy 模式的语义差异未涉及。

验证

  • node --import tsx --test tests/hermes-native.test.ts → 43/43 pass(负向控制:删掉代数重置即红)
  • npm run typecheck / npm run build / python -m py_compile hermes-plugin/__init__.py 通过
  • 已 rebase 到 upstream/master bd29924(behind 0,ahead 1)

范围边界

一个 86s 量级的冷启动不再丢掉插件,但首个请求仍需等待。冷启动本身(3.26GB index.db 启动期 quick_check)与代理落 failed 后的自愈,另开 issue 处理。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重:不重复——open+closed issue/PR 检索无 hermes 加载超时/bootstrap 机制的在先条目(#4641 为唯一相关项;最接近的 hermes 系 PR #3934/#3705/#4275/#3730/#2217 等均已关闭且机制不同)。
标签:bug / hermes / scope/wiring / p1(冷启动后整个 hermes 车道不可用 =「不能用」档)。

审核结论:方向正确、实现闭环,代码可合(附两个前置条件,见文末判定)

验证过的证据(不是照抄 PR 描述)

  • 逐行通读全量 diff(hermes-plugin/__init__.py ≈+438/-76、tests/hermes-native.test.ts +306),并对照改动前代码清点既有行为。
  • 本地:python -m py_compile ✓;npm ci && npm run build ✓;node --import tsx --test tests/hermes-native.test.ts → 43/43 通过(与 PR 声明一致)。
  • 负向控制自己复现:注释掉 register() 里的代际重置行 _state["stop_event"] = threading.Event() → 恰好一个红(slow-boot-restart 场景,42/43),恢复后绿——新增测试有牙,不是恒真断言。
  • 对账 master/v0.1.206:两版 hermes-plugin/__init__.py 逐字节相同,且都不含 BOOTSTRAP_SPAWN_WAIT_S / FIRST_REQUEST_WAIT_S / _stopped / stop_event。即 PR 描述里「顺带修掉未定义常量、discover_instance 内 _stopped(stop) TypeError」指的是本 PR 开发过程中的中间草稿状态,不是 master 或已发布版本的存量缺陷。最终 diff 自洽,此点仅作描述准确性备注,不影响代码判定。

行为变更披露(旧→新,均有披露义务)

项 旧行为 新行为 原因/风险
显式 BILI_HERMES_ATTACH 目标 单次探测,不健康即转 spawn 轮询至 min(剩余预算, 30s) 再回退 spawn 启动器并发起代理时单次探测易误判而 spawn 出竞争代理;现移入后台线程,不占宿主加载预算(死目标最多多拖 ~30s 回退)
冷启动 spawn 等待 分阶段各算一份等待(spawn 轮询 10s 等);>10s 才健康 → 放弃、杀子进程、降级直连 单一单调 deadline 90s 贯穿 attach/仲裁/spawn/健康/manifest 慢但健康的代理不再被放弃(可用性提升,/acp 可见预算进度)
首个模型请求 立即放行(可能未压缩直连上游) 有界等待 bootstrap 就绪(上限 105s)再决定路由;失败则明确告知并以 proxy/直连模式继续 turn 1 不再静默不压缩。风险:若宿主客户端请求超时短于该等待,turn 1 会报错而非静默直连(有意选择可见失败)
plugin 头盖章条件 tools_ready 即盖章 另要求出站请求本身携带已注册的核心 ACP 工具 hermes 每会话一次性快照工具表;注册前快照的会话改走 proxy 模式(服务端压缩)——原路径会抑制注入而模型无法回调 compress = 静默不压缩,属收紧、无原本正常工作的场景失去盖章
/acp 面板输出 仅 ready/failed 信息 增加 starting/failed/stopped 启动态措辞 冷启动可观测(输出格式变化,已披露)
env 路由时机 register() 内同步执行 后台线程执行,仍待健康探测通过后写 env #1375 combined CA bundle 语义不变,仅时机移到后台

wire 形状、acp_summary 处理、两种压缩模式的语义差异均未触碰(已对照 diff 核实,属实)。

审核发现

  1. (已修,待你收)两处注释与代码事实不符——模块 docstring 仍描述改动前行为("round 1 rides wire mode"、"every LLM request once the tools are ready"),_state 注释声称 bootstrap 记录含代码从未存储的 deadline 字段。按仓库标准这类注释是缺陷,我直接修了(commit fdd3d9f2c,独立标注,修后 py_compile + 43/43 复验通过),但推你的 fork 被拒(本账号对 yanhuojiyusheng/billion-context 无写权限)。patch 如下,git am 或手工应用即可;也可给账号开 push 权限我直接推。commit 同时存在于 ranxianglei/billion-context 的同名分支(孤儿分支,仅供取用):
--- a/hermes-plugin/__init__.py
+++ b/hermes-plugin/__init__.py
@@ -7,8 +7,11 @@
   model traffic through it via ``HTTPS_PROXY`` + ``SSL_CERT_FILE`` (combined CA bundle) —
   the same wire path the ``bili hermes`` launcher uses (CONNECT + certificate MITM);
 * registers the proxy's ACP tools (compress / decompress / acp_status) as native Hermes tools;
-* stamps plugin-mode headers on every LLM request once the tools are ready — round 1 rides
-  wire mode so strict backends see a clean head;
+* keeps register() inside the host's plugin-load deadline — discovery, spawn and tool
+  registration run on a background thread, and the first model request waits (bounded) for
+  readiness instead of slipping out uncompressed;
+* stamps plugin-mode headers only on requests carrying the registered ACP tools — a session
+  whose tool snapshot predates the registration rides proxy mode (server-side compression);
 * reports runtime info (model + max output tokens) to the proxy via bootstrap POSTs.
@@ -93,7 +93,7 @@
-# bootstrap: {"state": "starting"|"ready"|"failed"|"stopped", "started_at", "deadline", "error"} —
+# bootstrap: {"state": "starting"|"ready"|"failed"|"stopped", "started_at", "error"} —
  1. (观察,非阻塞)zombie 线程竞态窗口:_shutdown() 对旧代 bootstrap 线程的 join 只有 1.0s,而一次健康探测最多阻塞 2.0s——若 join 超时时旧线程仍在探测中,re-enable 换入全新未置位的 stop_event 后,zombie 看到的「停止」信号是清空的,可能继续跑完剩余步骤(向已废弃 ctx 注册工具、置 tools_ready、finally set 新一代的 ready_event)。最坏结果安全降级(visibility gate 失败 → proxy 模式;env 写入是进程级且无害),但建议后续用代际计数器加固。
  2. (观察) _register_tools 在零工具注册时也置 tools_ready=True——因 visibility gate 下 required=[] → False 不会误盖章,安全;语义上「ready」略宽泛,可接受。

CI 现状

head 65a9a6a4d 上 test 分片红(windows node22 ×3、ubuntu 24 等)= 继承性失败:master head bd29924d4(本 PR base)自身刚跑的全量矩阵 14 分片 9 红,哨兵已在 #4599(floor 94)钉死为存储车道 7 项 + epoch-confirm/issue2915 的 born-red 簇 + 仅存于 ci-fixes 的修复。本 PR 只动 hermes 插件文件与其测试,本地定向 43/43 绿,与那些套件无关。因无 fork 写权限我无法自行 merge origin/ci-fixes,已在 #4599 报备一行,等哨兵 help-merge 或作者处理。

八问

  1. 是什么问题:Hermes 冷启动(无常驻 bili 代理)时,宿主的 10 秒插件加载预算耗尽,整个插件被判失败并回收全部注册——/acp、/acp-cache、bili 工具整体消失,模型流量直连上游不压缩。
  2. 什么原因导致的:register() 同步执行慢路径(attach/spawn 轮询 10s + env 注入 + manifest 拉取 5s),冷启动必然超过宿主 10s 预算;宿主把超时判为插件失败并回收所有已注册项,之后任何注册都是空操作。不是插件注册顺序问题。
  3. 影响范围与严重程度:所有 bili plugin install hermes 且启动 hermes 时没有热代理的用户(页缓存冷更易触发);严重度 = 核心能力整体不可用(工具+命令消失、流量不压缩)→ p1。热 attach(毫秒级)用户不受影响。
  4. 怎么修复的:register() 拆成廉价同步部分(≤4s,低于宿主预算:middleware/hook/命令/on_unload)+ 后台线程 bootstrap(单一单调 90s 预算贯穿 attach 轮询/marker 仲裁/spawn/健康探测/manifest);首个模型请求有界等待就绪(≤105s)而非放行;生命周期加固(lifecycle lock 串行化 unload 与副作用、stop_event 代际重置修 re-enable 自杀、未就绪子进程条件回收);header 盖章收紧为「请求本身携带已注册 ACP 工具」(修掉会话工具快照竞态导致的静默不压缩)。
  5. 是否有回归问题:已逐项清点既有行为——热 attach 路径语义不变;显式 attach 目标由单次探测改 ≤30s 轮询(死目标最多多拖 ~30s 回退,现处后台线程);冷 spawn 放弃窗口 10s→90s(可用性提升,慢但健康不再被杀);首请求新增最长 105s 阻塞(有意为之,风险见上表);header 盖章条件只收紧不放松;/acp 措辞新增(已披露)。无 fail-fast 变吞错、无未披露阈值/时序漂移、wire 形状不变。
  6. 是否破坏缓存:不涉及——wire 形状未动、acp_summary 处理未动、前缀缓存策略不受影响;proxy 模式回退保持服务端压缩。
  7. 是否改了设计方向:没有——仍在既有插件车道设计内(自起/attach、env 路由、middleware 盖章),只改时序与生命周期。
  8. 是否引入了新的配置:没有——全部为模块级常量(REGISTER_WAIT_S=4.0 / BOOTSTRAP_BUDGET_S=90.0 / ATTACH_WAIT_S=30.0 / FIRST_REQUEST_WAIT_S=105 派生值),无新 env/config key。

判定:代码可合 —— 待 (a) 应用上述两行 doc patch(或给账号 fork push 权限,commit fdd3d9f2c 已备好);(b) ci-fixes 收敛后 CI 转绿(当前红为继承性,与本 PR 无关,已报备 #4599)。本地 43/43 + 负向控制复现均通过。

@ranxianglei ranxianglei added bug Something isn't working hermes 改动范围:内核之外的主机侧接线(src/agent 适配器、server 路由、注入拦截)/范围兜底/客户端标签 p1 scope/wiring labels Oct 25, 2026
register() no longer waits on discovery/spawn/manifest inside the host's 10s
load budget; a daemon thread bootstraps under one 90s deadline, the first model
request is held for readiness (falling back to proxy mode, never a silent
direct turn), /acp reports starting/failed/unloaded, and unload can no longer
be raced by a late registration or env write.
@ranxianglei

Copy link
Copy Markdown
Owner

closing as superseded by #4646, which merged this branch's cold-start restructure verbatim (git diff 65a9a6a 8290941 -- hermes-plugin/ is empty) plus the #4644 self-heal and the #4656 registry-pid port. thanks for the solid groundwork.

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 路由、注入拦截)/范围兜底/客户端标签 p1 scope/wiring 马上合并

Projects

None yet

2 participants