Repository navigation
perf(chat,hosts): skip frontend source re-hashing on immutable installed releases and extend readiness timeout to 90s - #9089
Conversation
…led releases and extend readiness timeout to 90s Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · Gemini · Google · self_reported
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)
动机 / Motivation
评审精确 head 84a6cde73bc6eb8b7ada3538109260f056b626c1。受影响主体为所有通过 loopx update activate --service all、loopx chat 或 loopx dashboard 启动托管或本地安装包模式服务的桌面运维者及管家守护进程。在安装后不可变 release 目录中,构建产物在 install-local.sh 阶段已通过 chat_bundle.py verify --source 校验且无 .git 元数据;但此前 serve_chat()、launch_dashboard() 与 runtime_workspace.main() 仍无条件向 validate_bundle() 传入 source_root=...,导致在 macOS launchd 与系统文件扫描负载下每次冷启动都要同步逐个读取并哈希 757 个前端与控制面源文件(实测额外耗费约 55–60 秒),叠加 Python 冷导入后超出 45 秒就绪上限并误触发 update activate 回滚;同时即使跳过源码重扫,高负载冷启动下导入与前置管家对账仍可能接近 45 秒。改动后,仅对不存在 .git 且 release.json 含非空 release_id 的不可变已安装 release 返回 source_root=None 跳过源码树重复哈希,并将 _SERVICE_READINESS_TIMEOUT_SECONDS 提升至 90.0 秒。可观测结果为:不可变已安装 release 启动跳过 757 个源文件重复哈希同时仍严格校验 loopx/web/chat/ 已交付产物文件清单与 SHA-256;带 .git 签出/worktree 及缺失或损坏 release.json 仍 fail-closed 执行全量源码哈希校验;_verify_services 容忍高负载冷启动至 90.0 秒并继续在超时后完整回滚。本 PR 不关闭 validate_bundle 对已交付产物的逐文件 SHA-256 校验,不放松 .git 开发签出与 worktree 的源码新鲜度检查,也不改动任何 UI 布局。
改动思路 / Approach
架构定位与边界:复用既有 chat_bundle.validate_bundle(source_root=None) 与 managed_service_runtime._verify_services 契约,以单个 fail-closed 辅助函数集中处理不可变 release 与源码签出分流。仅添加 runtime_source_root、更新 3 处运行时调用点、上调 _SERVICE_READINESS_TIMEOUT_SECONDS 至 90.0 并补全单测。
- 不可变安装快照判定 (
runtime_source_root):在loopx/presentation/chat_bundle.py仅使用既有json与Path添加runtime_source_root(root: Path) -> Path | None(避免给被setup.py/scripts/chat_bundle.py按文件路径 exec 的模块引入新依赖):若.git存在(含目录与 worktree 文件),即使目录内残存release.json也始终返回root.resolve()强制校验源码新鲜度;仅当.git缺失且release.json可解析并含非空字符串release_id时返回None;损坏、缺失或空release_id均 fail-closed 返回root.resolve()。 - 统一接入所有打包 Chat 前端入口:
loopx/chat_server.py、loopx/dashboard_launcher.py、loopx/presentation/runtime_workspace.py改为通过runtime_source_root(...)传递source_root,validate_bundle()对loopx/web/chat/自身bundle-manifest.json的逐文件 SHA-256 与资源代数检查始终运行。 - 提高托管服务就绪等待预算:在
loopx/hosts/managed_service_runtime.py将_SERVICE_READINESS_TIMEOUT_SECONDS从45.0提升至90.0,保护高负载下的launchd冷启动而不拖慢快速就绪路径。
具体改动 / Changes
loopx/presentation/chat_bundle.py(+21): 新增runtime_source_root(root: Path) -> Path | None封装不可变 release 判定与 fail-closed 回退逻辑。loopx/chat_server.py(+5/-2)、loopx/dashboard_launcher.py(+5/-2)、loopx/presentation/runtime_workspace.py(+5/-2): 将validate_bundle(..., source_root=...)统一经由runtime_source_root(...)传入。loopx/hosts/managed_service_runtime.py(+1/-1): 将_SERVICE_READINESS_TIMEOUT_SECONDS由45.0调高至90.0。tests/presentation/test_chat_bundle.py(+52/-1) &tests/test_managed_service_runtime.py(+4/-4): 新增不可变 release 跳过源码扫描但仍拦截篡改index.html、.git目录/worktree 文件强制保留源码检查、缺失/损坏/空白release_idfail-closed,以及 60s 通过 / 90s 超时回滚单测。
对主干的风险 / Risks
风险可控且范围明确(+93/-12 行,聚焦安装快照判定与就绪等待上限):
- 全部
.git开发签出、worktree 及缺失/损坏release.json环境均按 fail-closed 保留 757 源文件新鲜度校验,已通过pytest140 passed, 1 skipped(含test_chat_bundle_source_bootstrap.py、test_runtime_workspace.py、test_dashboard_command.py、test_managed_service_runtime.py)覆盖; ruff check零告警,examples/control_plane/control-plane-maintainability-ratchet-smoke.py与examples/semantic-vocabulary-drift-smoke.py及canary premerge4/4(ok: true)均于精确提交验证通过;- 未改动 UI 布局、消息路由或状态持久化格式,唯一残余验证为合入后立即在本机执行新 release 打包并完成
update activate --service all实测。
我的整体评价 / Verdict
该修复直击 com.loopx.chat 在不可变安装快照下每次启动重算 757 个前端源码哈希耗时 ~55–60s 并触发激活回滚的根本性能瓶颈,同时以严格 fail-closed 边界守住源码修改检测与已打包产物防篡改防线(UI visual review 不适用)。全部本地测试与预合并检查已于精确头 84a6cde73bc6eb8b7ada3538109260f056b626c1 通过,可合入主干。
English Verdict: APPROVE - Verified at exact head 84a6cde; skipping redundant 757-file frontend source hashing on immutable installed releases (while preserving .git worktree checks, fail-closed manifest validation, and full served-bundle verification) plus raising readiness deadline to 90.0s resolves production launchd activation timeouts (ruff pass, pytest 140 passed/1 skipped, canary premerge 4/4 pass).
Goal & Root Cause
Installed LoopX releases are immutable snapshots copied with a valid
release.json(release_id) and no.gitdirectory, and already passedchat_bundle.py verify --sourceduring release staging. However,serve_chat(),launch_dashboard(), andpresentation/runtime_workspace.pystill passedsource_root=...unconditionally intovalidate_bundle(), re-reading and SHA-256 hashing 757 frontend/control-plane source files on every daemon startup (~55–60s under macOSlaunchd+ Endpoint/Xprotect file-open scanning), causingupdate activate --service allto exceed the startup readiness deadline and roll back.Changes
loopx/presentation/chat_bundle.py: addruntime_source_root(root: Path) -> Path | Nonewhich returnsNoneonly when.gitis absent andrelease.jsoncarries a valid non-emptyrelease_id; git checkouts/worktrees and missing/corrupt manifests fail closed and still enforce fullsource_inputs()hashing. Served asset digest verification insidevalidate_bundle()always runs.loopx/chat_server.py,loopx/dashboard_launcher.py,loopx/presentation/runtime_workspace.py: resolvesource_rootviaruntime_source_root(...).loopx/hosts/managed_service_runtime.py: raise_SERVICE_READINESS_TIMEOUT_SECONDSfrom45.0to90.0so cold macOSlaunchdimports + pre-serve Steward reconciliation do not trigger spurious rollbacks.tests/presentation/test_chat_bundle.py,tests/test_managed_service_runtime.py: regression tests for immutable releaseruntime_source_root, git checkout/worktree fail-closed behavior, corrupt/emptyrelease.json, served-bundle tampering detection, and60spass /90sdeadline enforcement.Verification
ruff check: All checks passedpytest tests/presentation/test_chat_bundle.py tests/presentation/test_chat_bundle_source_bootstrap.py tests/presentation/test_runtime_workspace.py tests/test_dashboard_command.py tests/test_managed_service_runtime.py -q: 140 passed, 1 skippedexamples/semantic-vocabulary-drift-smoke.py: ok=trueloopx canary premerge --from-git-diff --git-diff-base origin/main: 4/4 catalog canaries passed (ok: true, includingcontrol-plane-maintainability-ratchet-smoke.pyandsemantic-vocabulary-drift-smoke.py)No private/internal paths or identifiers.