feat(chv): 架构与代码质量提升——WarmPool 定案、长函数提取、严格测试替身、Docker 同步守卫、配置统一校验、类型注解与 ADR

堆叠于 #246(feat-chv-secfix),只到 PR OPEN,未合并、未部署。
安全语义不变:egress、key seed 投递、磁盘删除、默认 provisioned、
tap 校验一律保留;前端零改动。
- WarmPool 定案为实验性(ADR-5):dispatch 永远冷启动,
  wired_to_dispatch 恒 False,acquire/resume 只保留实验接口,
  worker 仍调 reconcile_orphans 清理残留。
- 长函数提取:preflight 126→31、wake_vm 135→36、__init__ 69→31、
  build_chv_config 159→58;错误文案逐字不变。
- 严格测试替身:fake API 校验 VmConfig(任务盘+seed 盘、net
  无重复),fake SSH 校验 known_hosts 钉死;新增 5 项测试。
- Docker 同步守卫:8 个真实标记用例下 Docker 内联版与正典
  parse_goal_result 状态全等,改分叉即 fail。
- 配置统一校验:preflight 以 config_invalid 统一兜底端口/配额/
  超时/快照/host-key 模式(构造时已校验正常值,preflight 兜底
  事后改写);CHV_NET 与 CHV_NET_JSON 双设仍警告不硬拒。
- 类型与 lint:chv 叶子模块公开 API 补类型注解;stdlib 扫描
  揪出 executor 内 1 个真死 import(host_key_paths)并删除。
- ADR:docs/chv-architecture.md(ADR-1 至 ADR-7)。
测试:CHV 163 项全过;cloud 全量 1593 项三跑结果为 8 errors
(首跑详细日志因 tail 截断丢失)、全过、1 失败(浏览器代理
时序测试,与 CHV 无关),全量本身存在时序偶发,CHV 套件
确定性全过。
This commit is contained in:
host committed 2026-10-31 05:50:56 +00:00
1 parent baf26beb78
commit c908a0147a
12 files changed
+725 -239

No files matched your search

+23 -5
View File
@@ -1,4 +1,17 @@
"""快照预热池 WarmPool(含 state_dir registry 持久化与孤儿清理)。
"""快照预热池 WarmPool(实验性,非生产路径,feat-chv-arch95 定案)。
定案(架构决策,见 docs/chv-architecture.md ADR-5):WarmPool
不再宣称为生产快速启动特性。原因:预热 VM 的磁盘是干净快照
盘,不是任务盘;dispatch 前查池若要真正接线,必须在派任务
前给已暂停 VM 重新供给任务盘(热换盘/二盘挂载/host.py 磁盘
语义三者都未实现),把预热盘直接当任务盘用会击穿“任务盘
隔离”不变量。acquire 内 resume 只是实验性接口,worker
dispatch 永远走冷启动(wake_vm),_run_with_vm 绝不查池。
pool 保留的唯一生产价值是 reconcile_orphans()(上一世残留
清理),worker 启动/关闭仍调它。warm_pool_wired_to_dispatch
恒为 False,任何把它改成 True 的改动都必须先实现任务盘
重供给并补不变量测试,否则不得合入。
隔离红线(与 #216 一致,绝不妥协):预热条目是一次性的
干净快照 VM,acquire 消耗掉它,release 一律销毁,绝不把
@@ -40,7 +53,10 @@ _REGISTRY_FILENAME = "warm-pool.json"
class WarmPool:
"""快照预热池(快速启动设计,接口与状态机在代码里)。
"""快照预热池(实验性,见模块头定案)。
wired_to_dispatch = False:生产 dispatch 不查池。
目标:冷启动(preflight + 起 VMM + create + boot + SSH
就绪)是慢路径;预热池提前把干净 VM 起好并暂停,任务
@@ -55,6 +71,8 @@ class WarmPool:
prewarm 返回 [](无基准快照可预热,不是静默造快照)。
"""
wired_to_dispatch = False
def __init__(self, executor, size=None, snapshot_url=None,
registry_path=None):
self.executor = executor
@@ -150,7 +168,7 @@ class WarmPool:
_LOGGER.warning("chv_pool_registry_save_failed path=%s",
self.registry_path)
def reconcile_orphans(self):
def reconcile_orphans(self) -> list:
"""清理上一世残留条目:对每个 orphan/assigned 条目
尽力 shutdown_vm(清 socket 与残留登记),条目清掉
并落盘,返回已清理的 VM 名列表。worker 启动/清理时
@@ -177,11 +195,11 @@ class WarmPool:
return cleaned
# -- 池操作 ----------------------------------------------------------
def warm_count(self):
def warm_count(self) -> int:
return sum(1 for e in self._entries.values()
if e["state"] == "warm")
def orphan_count(self):
def orphan_count(self) -> int:
"""上一世残留条目数(待 reconcile,不可分配)。"""
return sum(1 for e in self._entries.values()
if e["state"] in ("orphan", "assigned"))