From fe04c2a0c854535cd4711b52dbb3c16d0ec7b58c Mon Sep 17 00:00:00 2001 From: Ji Mao Cai <44921326+jinghero@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:56:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=A4=8D=E7=94=A8=E5=AE=9E=E8=AF=81?= =?UTF-8?q?=E5=81=A5=E5=BA=B7=E7=8E=AF=E5=A2=83=E5=B9=B6=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=8A=B6=E6=80=81=20(T10.17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\244\207\345\244\215\347\224\250.md" | 21 +++ ...06\345\244\207\345\244\215\347\224\250.md" | 53 ++++++ ...73\345\212\241\346\213\206\345\210\206.md" | 7 + ...66\346\236\204\350\256\276\350\256\241.md" | 2 + internal/cli/bootstrap.go | 94 +++++++++-- internal/cli/bootstrap_update_test.go | 122 +++++++++++--- internal/cli/version.go | 13 +- internal/cli/version_test.go | 3 + scripts/benchmark-bootstrap-reuse.ps1 | 153 ++++++++++++++++++ 9 files changed, 424 insertions(+), 44 deletions(-) create mode 100644 "doc/current/M10/\350\256\276\350\256\241-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" create mode 100644 "doc/current/M10/\351\252\214\346\224\266-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" create mode 100644 scripts/benchmark-bootstrap-reuse.ps1 diff --git "a/doc/current/M10/\350\256\276\350\256\241-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" "b/doc/current/M10/\350\256\276\350\256\241-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" new file mode 100644 index 0000000..bb380ae --- /dev/null +++ "b/doc/current/M10/\350\256\276\350\256\241-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" @@ -0,0 +1,21 @@ +# T10.17 健康环境准备复用 + +完整 bootstrap 新增 `--reuse-ready-environment`,仅改变环境复用权限,不改变工作区目标选择。原参数默认行为保持;`--if-needed` 的日常启动语义保持。 + +持原 mutation 锁完成事务恢复、工作区同步和源码真实性检查后,仅当工作区未变化、初始和仓库状态均 ready、版本及 Commit 匹配、venv 结构完整,才允许复用。新选项还用现有 CheckPython 复验受管 Python,随后执行既有离线解释器与锁定依赖检查。明确未就绪回原准备流程;取消、输出、进程或状态基础设施错误直接失败。源码恢复和环境重建不短路。 + +`version` 的 result.details 追加 `bootstrapEnvironmentReuse: true`。桌面端从已有 exe 版本查询提取该能力,在成功对齐的本次结果中传递;旧 exe 或未知能力不传新参数,不猜测首发版本,不增加进程。独立 repair、重建和普通失败重试不启用此选项。进度沿用现有完成收口,遥测复用原命令结果观察者。 + +新选项若执行完整准备,必须在写入 ready 前严格离线复验实际解释器和锁定依赖。同步退出 0 不能替代此实证;仍未就绪或复验故障经既有持久化入口写 broken,保留原错误码与 cause,不再递归准备、不自动删除 venv。健康快速返回不增加检查。旧默认、if-needed 与独立 sync 的既有行为保持,本次不修复它们的配置恢复缺口。 + +不合并准备与监督,不改锁、进程、源测速算法或 uv 安装/同步的全局语义,不改安装版。 + +## 实施与验收 + +1. 先扩展 bootstrap 的 alpha 更新、健康/损坏及错误分类矩阵,确认新选项在基线因不支持而失败;补 version 能力断言。 +2. 实现独立复用策略及受管 Python 检查,保留原依赖检查和修复;定向回归、vet、build、全仓检查。 +3. 主程序接入能力透传及正常初始化选项,覆盖旧/未知 exe、升级后能力、离线、进度和重建语义;运行相关测试、Electron 编译与格式检查。 +4. 在隔离 app root 用真实 uv 和相同仓库/环境,交替测量旧/新二进制。保留网络探测与实际动作证据,验证健康/缺包/解释器失效的原行为。 +5. pstack 审查规范和规格,修复可复现问题后复验。记录验证限制,不以模拟计时替代真实性能。 + +实现、真实计时与损坏试验、规范及规格双轴复审已完成,结果见[本地验收](验收-T10.17-健康环境准备复用.md)。复验开始事件的输出故障也会写 broken;测量身份漂移会中止。进入正式 PR 交付,全仓检查保留有记录的环境失败说明,未替换安装版或发布。 diff --git "a/doc/current/M10/\351\252\214\346\224\266-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" "b/doc/current/M10/\351\252\214\346\224\266-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" new file mode 100644 index 0000000..712de50 --- /dev/null +++ "b/doc/current/M10/\351\252\214\346\224\266-T10.17-\345\201\245\345\272\267\347\216\257\345\242\203\345\207\206\345\244\207\345\244\215\347\224\250.md" @@ -0,0 +1,53 @@ +# T10.17 本地验收 + +2026-11-10。Runtime 基线 `116a045e89abcccc4cce7f4c1995bb6f9a3fb119`,主程序实现基线 `d6ba789a5dd38e29c5dce6e5865fd25c013a3573`,交付前已快进到 `31f2db9b3f642618fe8745ff597b03132ed4cff9`。两个隔离 worktree 的实现与验证记录;未替换安装版、未启动安装版后端或发布。 + +## 实现 + +- 完整 bootstrap 可显式开启健康环境复用。目标查询、pending、源码恢复、原 mutation 锁、状态/revision/venv 条件保持;实际受管 Python、venv 解释器与锁定依赖检查均保留。 +- 通过既有 version 查询的 `bootstrapEnvironmentReuse` 能力确认兼容性,只有布尔 true 才启用。不猜首发版本,不增加查询进程。能力只来自当前路径的 exe,不取旧备份。 +- 正常初始化可启用,普通失败重试和锁文件恢复明确禁用,旧 exe、未知能力与离线回退保持原命令。进度与遥测复用既有完成和结果观察者。 +- 真实坏解释器试验发现 uv sync 返回 0 仍可能未修复配置。因此新选项在完整准备后、写 ready 前再严格离线复验,失败保留原错误并写 broken;不自动删 venv、不重复准备。此保护不改变旧默认和 if-needed 的既有修复行为。 + +## 性能 + +运行 `scripts/benchmark-bootstrap-reuse.ps1 -SourceRoot <已准备安装目录> -BaseRef 116a045e89abcccc4cce7f4c1995bb6f9a3fb119 -Pairs 5`。脚本从同一代码基线构建控制版与候选版,只复制受管程序资产到新目录,排除 Junction,改写并验证 venv Python home,使用独立状态。每轮严格校验实际版本与 Commit,漂移即中止,并保存实际身份。预热不计入中位数,五组按交替顺序执行真实 uv。 + +目标 `v5.8.0`,Commit `a640598bf521c59cb255202e95ae2595dd690b45`。最终构建的结果: + +| 组 | 控制版 ms | 候选版 ms | +| --- | ---: | ---: | +| 1 | 8995.25 | 3835.32 | +| 2 | 6750.02 | 3410.65 | +| 3 | 5879.00 | 3814.17 | +| 4 | 8500.60 | 3294.91 | +| 5 | 7461.03 | 4481.01 | +| 中位数 | 7461.03 | 3814.17 | + +减少 3646.86 ms,约 49%。候选每次均有 readiness 事件,未进入 Python 安装或依赖同步;network.probe 事件由 12 减至 3,保留 Git 源探测。前两轮同条件五组分别为 9658.33 → 5509.54 ms、10193.79 → 4994.35 ms,减少 4148.79 ms、5199.44 ms;网络与文件读取有波动,不能承诺固定节省 5 秒。 + +这只是该机器健康环境的完整 bootstrap,不是首次安装、整机启动或业务主页时间。两轮准备尚未合并,原约 24 秒的未细分异常未由此解释。 + +## 真实安全试验 + +所有故障均在副本制造,安装目录只读: + +1. 向跟踪的 main.py 追加内容。候选恢复到原 SHA-256 `0FE65A0AC9554F6EFAB5C4FFB98709F94B5DC84E554223D4A5EECE87B5D0B32A`,完整同步后复验成功,未走 unchanged 快返回。 +2. 将副本 pyvenv.cfg 的 home 指向副本内不存在目录。离线检查拒绝坏解释器,原准备仍未能修复时,新后置复验返回 `DEPENDENCY_SYNC_FAILED`、退出 50、状态 `environment_broken`,不写 ready。 + +原始 comparison.json、逐次 NDJSON、构建 SHA-256、safety.json 及安全试验脚本保存在作者本机临时目录 `auto-mas-bootstrap-reuse-20261110-pr`。同条件、Commit 漂移、版本漂移三类测量身份故障注入也已验证:缺少守卫时漂移被接受,当前脚本拒绝两种漂移。 + +## 检查 + +- 新参数与 version 能力的基线红灯、实现后绿灯已执行。 +- alpha 目标选择、同 revision 复用、源码变化、venv 损坏、解释器/包缺失、取消、输出/执行故障、准备后仍损坏及后置复验错误均有回归覆盖。 +- 主程序交付前 85 项相关测试、Electron 与 renderer 类型检查、锁定版本 Ruff 格式检查、frontend format、lint 通过。lint 有一条未修改页面的已有 prop 默认值警告。 +- Go gofmt、vet、build、diff-check 与定向回归通过。全量测试未全绿:`SUBST W:` 未就绪已在基线复现;文件系统夹具另有目录非空的清理失败,最新涉及未改的 ExFAT 生命周期用例,该用例基线定向重跑通过。不把这些失败表述为绿色全仓。未新增生产并发,不宣称 race 通过。 + +## 规范 + +双轴独立复审后,无未解决硬违规或可操作的可维护性发现。少量请求构造重复保留了快路径“允许回准备”与准备后“必须失败”的不同语义。 + +## 规格 + +初审两处 P2 已修复并补红绿测试:备份查询污染当前 exe 能力;实际 Python 重试与锁文件恢复误启用新选项。追加故障注入审查发现的后置复验开始事件输出失败 P2 也已修复:输出故障同样经既有入口写 broken,再返回原 cause;原始隔离 overlay 与永久回归均已闭环。P3 测量身份建议已落实。最终规范与规格独立复审无剩余可操作发现,目标更新、源码恢复、锁与错误 cause 保留。 diff --git "a/doc/\344\273\273\345\212\241\346\213\206\345\210\206.md" "b/doc/\344\273\273\345\212\241\346\213\206\345\210\206.md" index 46d7156..f29c2cf 100644 --- "a/doc/\344\273\273\345\212\241\346\213\206\345\210\206.md" +++ "b/doc/\344\273\273\345\212\241\346\213\206\345\210\206.md" @@ -1664,8 +1664,15 @@ T6.1~T6.7 固定测试名、Windows/race 门和双审清零规则完成;已 - 依赖:T4.12;内容:源码恢复扫描复用持续固定的公共目录链,保留身份、Reparse、大小写与文件共享检查,恢复写入前释放扫描句柄。 - 设计、验证计划与本地测量:[T10.16](current/M10/设计-T10.16-源码路径链复用.md)。不改安装版和协议,不发布。 +- [ ] **T10.17 健康环境准备复用** 🚧 2026-11-10 + - 依赖:T13.21、T4.12;内容:完整 bootstrap 显式允许复用经实证的健康同 revision 环境,保持目标更新、源码校验、锁与恢复语义。 + - 设计与验证:[T10.17](current/M10/设计-T10.17-健康环境准备复用.md)。已获贡献分支提交、推送与正式 PR 授权,不替换安装版或发布。 + - 实现、真实计时、损坏验收与双轴复审完成,详见[验收](current/M10/验收-T10.17-健康环境准备复用.md)。最新中位数 7461.03 → 3814.17 ms;P2 输出失败与 P3 测量身份问题已修复,全仓有环境失败,待 PR 合入。 + ## 7. 变更记录 +2026-11-10:登记 T10.17,新增显式健康复用选项与 version 能力声明;桌面端复用既有版本查询识别支持,不增加查询进程。保留日常启动和显式重建,完成同条件计时与 pstack 审查。 + 2026-11-10:登记 T12.10,按 Sentry 事件与离线探针修正普通失败分组和应用栈,补齐已知后端身份、 初始/清理分类与终止请求事实;保持错误四元组和生命周期,不上传原始文本,不访问生产 Sentry 验收。 diff --git "a/doc/\346\236\266\346\236\204\350\256\276\350\256\241.md" "b/doc/\346\236\266\346\236\204\350\256\276\350\256\241.md" index c4f9bf0..3d394cd 100644 --- "a/doc/\346\236\266\346\236\204\350\256\276\350\256\241.md" +++ "b/doc/\346\236\266\346\236\204\350\256\276\350\256\241.md" @@ -525,6 +525,8 @@ stage 独占 mutation Mutex 但不要求 backend Mutex 空闲;其他变更仍 正式安装版通过 `workspace sync` 使用内置 Go Git 能力更新受管后端仓库,不要求目标机器额外安装 Git。开发模式只启动用户指定的源码目录,默认不自动更新或清理该目录。降级不设专用回滚命令,调用 `workspace sync --version <旧版本>` 即可按同一确定性流程重新获取旧发布分支。 +`bootstrap --reuse-ready-environment` 允许完整初始化复用健康同 revision 环境,不改变目标仓库查询。工作区校验恢复、状态和 venv 结构检查仍在原 mutation 锁内完成;新选项还复验受管 Python,再复用上述离线解释器与依赖就绪实证。不满足条件则执行原准备流程,并在写入 ready 前再次严格离线复验;仍未就绪保留原错误并写 broken,不自动删除环境或重复准备。显式修复与默认 bootstrap 不短路。`version` 的 `result.details.bootstrapEnvironmentReuse: true` 公告此能力;旧版本缺省表示不支持,桌面端复用已有查询,不额外启动进程。 + 除 `backend supervise` 外,所有命令执行完成后退出。每次调用只创建一个执行单一顶层操作的 Runtime 进程,不实现通用常驻 RPC 服务。 ### Electron 调用 diff --git a/internal/cli/bootstrap.go b/internal/cli/bootstrap.go index f10fe7f..6e3fc91 100644 --- a/internal/cli/bootstrap.go +++ b/internal/cli/bootstrap.go @@ -36,6 +36,7 @@ func bootstrapCommand(deps *deps) *cobra.Command { } command.Flags().String("version", "", "目标版本(例如 v5.4.0-beta.1)") command.Flags().Bool("if-needed", false, "仅在仓库更新或环境未就绪时同步依赖") + command.Flags().Bool("reuse-ready-environment", false, "复验并复用健康环境,不改变目标仓库查询") return command } @@ -49,6 +50,10 @@ func runBootstrap( if err != nil { return sessionSuccess{}, err } + reuseReadyEnvironment, err := command.Flags().GetBool("reuse-ready-environment") + if err != nil { + return sessionSuccess{}, err + } version, err := command.Flags().GetString("version") if err != nil || version == "" { return sessionSuccess{}, &commandError{ @@ -260,9 +265,9 @@ func runBootstrap( // 结构完整仍可能指向失效的基础 Python,或已被重建成缺包的空环境; // 交给 --no-sync 启动之前必须让 uv 复验实际解释器与锁定依赖。 venvIntact := filesystem.InspectVenv(deps.global.layout).Intact - if ifNeeded && venvIntact && !workspaceResult.Changed && workspaceResult.Status == protocol.StateReadyToStart && + if (ifNeeded || reuseReadyEnvironment) && venvIntact && !workspaceResult.Changed && workspaceResult.Status == protocol.StateReadyToStart && initial.Status == protocol.StateReadyToStart && initial.LastSuccessful.Version == revision.Version() && initial.LastSuccessful.Commit == revision.Commit() { - ready, err := checkBootstrapReadiness(ctx, deps, service, emitter, revision, operationLogger) + ready, err := checkBootstrapReadiness(ctx, deps, service, emitter, revision, operationLogger, reuseReadyEnvironment) if err != nil { return sessionSuccess{}, errors.Join(err, rollbackM5Preparation(emitter, machine, protocol.StageDependenciesCheck, "运行环境检查未完成")) } @@ -362,6 +367,38 @@ func runBootstrap( if err := emitM5Progress(emitter, protocol.StageDependenciesSync, protocol.ProgressSucceeded, "锁定依赖已同步"); err != nil { return sessionSuccess{}, err } + if reuseReadyEnvironment { + // uv sync 退出 0 仍可能留下失效的基础解释器;新路径在写入 ready 前严格复验, + // 不把复验失败再折回准备,也不自动删除用户环境。 + if err := emitM5Progress(emitter, protocol.StageDependenciesCheck, protocol.ProgressRunning, "正在复验准备后的运行环境"); err != nil { + return sessionSuccess{}, persistM5FailureWithLifecycle(ctx, emitter, store, deps.global.layout, initial, revision, uvExecutable, pythonResult.Spec, operationLogger, machine, protocol.StageDependenciesCheck, err) + } + offline, err := mirror.NewPolicy(mirror.PolicySpec{Offline: true}) + if err != nil { + return sessionSuccess{}, err + } + verified, err := service.CheckDependencies(ctx, uv.DependenciesRequest{ + ProjectDir: deps.global.layout.RepoDir(), ProjectEnvDir: deps.global.layout.VenvDir(), + PythonVersion: pythonResult.Spec.Version.String(), OperationID: emitter.OperationID(), + Branch: revision.Branch(), Commit: revision.Commit(), MirrorPolicy: offline, Line: uvLogLine(operationLogger), + }) + if ctxErr := ctx.Err(); ctxErr != nil { + err = ctxErr + } + if err == nil && (!verified.LockfileChecked || !verified.Synchronized) { + err = &commandError{ + code: protocol.CodeDependencySyncFailed, stage: protocol.StageDependenciesCheck, + message: "准备后未确认运行环境就绪", details: map[string]any{}, + cause: errors.New("prepared environment did not pass dependency readiness check"), + } + } + if err != nil { + return sessionSuccess{}, persistM5FailureWithLifecycle(ctx, emitter, store, deps.global.layout, initial, revision, uvExecutable, pythonResult.Spec, operationLogger, machine, protocol.StageDependenciesCheck, err) + } + if err := emitM5Progress(emitter, protocol.StageDependenciesCheck, protocol.ProgressSucceeded, "准备后的运行环境检查通过"); err != nil { + return sessionSuccess{}, err + } + } ready, err := store.NewReadyEnvironment(revision.Version(), revision.Commit()) if err != nil { return sessionSuccess{}, stateStoreError(protocol.StageDependenciesSync, err) @@ -412,18 +449,38 @@ func checkBootstrapReadiness( emitter *protocol.Emitter, revision gitrepo.Revision, logger workspaceLogger, + checkManagedPython bool, ) (bool, error) { if err := emitM5Progress(emitter, protocol.StageDependenciesCheck, protocol.ProgressRunning, "正在检查当前运行环境"); err != nil { return false, err } - spec, err := service.ReadPythonSpec(ctx, deps.global.layout.RepoDir()) - if err != nil { - return false, err - } policy, err := mirror.NewPolicy(mirror.PolicySpec{Offline: true}) if err != nil { return false, err } + var spec uv.PythonSpec + if checkManagedPython { + python, err := service.CheckPython(ctx, uv.PythonRequest{ + ProjectDir: deps.global.layout.RepoDir(), PythonInstallDir: deps.global.layout.PythonDir(), + ProjectEnvDir: deps.global.layout.VenvDir(), CacheDir: deps.global.layout.UVCacheDir(), + Branch: revision.Branch(), Commit: revision.Commit(), MirrorPolicy: policy, Line: uvLogLine(logger), + }) + if ctxErr := ctx.Err(); ctxErr != nil { + return false, ctxErr + } + if err != nil { + if !bootstrapCheckNeedsPreparation(err, protocol.CodePythonVersionMismatch) { + return false, err + } + return false, emitM5Progress(emitter, protocol.StageDependenciesCheck, protocol.ProgressFailed, "受管 Python 未就绪,正在重新准备") + } + spec = python.Spec + } else { + spec, err = service.ReadPythonSpec(ctx, deps.global.layout.RepoDir()) + if err != nil { + return false, err + } + } result, err := service.CheckDependencies(ctx, uv.DependenciesRequest{ ProjectDir: deps.global.layout.RepoDir(), ProjectEnvDir: deps.global.layout.VenvDir(), PythonVersion: spec.Version.String(), OperationID: emitter.OperationID(), @@ -434,17 +491,7 @@ func checkBootstrapReadiness( return false, ctxErr } if err != nil { - // 只有已完成的 uv 检查报告未同步才进入准备流程;读取管道、 - // 启动进程或日志出口失败不能被依赖层的外包装掩盖。 - if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || - findOperationErrorCode(err, protocol.CodeOutputWriteFailed) != nil { - return false, err - } - if executionErr := findOperationErrorCode(err, protocol.CodeUVExecFailed); executionErr != nil && executionErr.Details()["failureKind"] != "nonzero_exit" { - return false, err - } - failure := findOperationErrorCode(err, protocol.CodeDependencySyncFailed) - if failure == nil || detailsExitCode(failure.Details()) <= 0 { + if !bootstrapCheckNeedsPreparation(err, protocol.CodeDependencySyncFailed) { return false, err } return false, emitM5Progress(emitter, protocol.StageDependenciesCheck, protocol.ProgressFailed, "当前运行环境未同步,正在重新准备") @@ -455,6 +502,19 @@ func checkBootstrapReadiness( return true, emitM5Progress(emitter, protocol.StageDependenciesCheck, protocol.ProgressSucceeded, "当前运行环境检查通过") } +// bootstrapCheckNeedsPreparation 只将已完成的 uv 不健康检查归入准备,保留执行与输出故障。 +func bootstrapCheckNeedsPreparation(err error, code protocol.Code) bool { + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || + findOperationErrorCode(err, protocol.CodeOutputWriteFailed) != nil { + return false + } + if executionErr := findOperationErrorCode(err, protocol.CodeUVExecFailed); executionErr != nil && executionErr.Details()["failureKind"] != "nonzero_exit" { + return false + } + failure := findOperationErrorCode(err, code) + return failure != nil && detailsExitCode(failure.Details()) > 0 +} + type directWorkspaceEmitter struct { emitter *protocol.Emitter suppressWorkspaceStates bool diff --git a/internal/cli/bootstrap_update_test.go b/internal/cli/bootstrap_update_test.go index acf6d01..8f79def 100644 --- a/internal/cli/bootstrap_update_test.go +++ b/internal/cli/bootstrap_update_test.go @@ -3,6 +3,7 @@ package cli import ( "bytes" "context" + "encoding/json" "errors" "io" "os" @@ -22,11 +23,9 @@ import ( ) func TestBootstrap_AlphaStartupAndExplicitUpdate(t *testing.T) { - for _, startup := range []bool{true, false} { - name := "explicit update" - if startup { - name = "startup" - } + for _, option := range []string{"--if-needed", "", "--reuse-ready-environment"} { + startup := option == "--if-needed" + name := "explicit update" + option t.Run(name, func(t *testing.T) { root := t.TempDir() log := &m5TestLog{} @@ -52,8 +51,8 @@ func TestBootstrap_AlphaStartupAndExplicitUpdate(t *testing.T) { return gitrepo.SyncResult{Revision: revision, Status: protocol.StateReadyToStart}, err }} args := []string{"--app-root", root, "--output", "ndjson", "bootstrap", "--version", version} - if startup { - args = append(args, "--if-needed") + if option != "" { + args = append(args, option) } var stdout, stderr bytes.Buffer code := Execute(t.Context(), args, IO{In: strings.NewReader(""), Out: &stdout, Err: &stderr}, WithCWD(root), @@ -77,6 +76,13 @@ func TestBootstrap_AlphaStartupAndExplicitUpdate(t *testing.T) { } func TestBootstrap_IfNeededSkipsOnlyUnchangedReadyEnvironment(t *testing.T) { + for _, option := range []string{"--if-needed", "--reuse-ready-environment"} { + t.Run(option, func(t *testing.T) { testBootstrapUnchangedReadyEnvironment(t, option) }) + } +} + +func testBootstrapUnchangedReadyEnvironment(t *testing.T, option string) { + t.Helper() for _, changed := range []bool{false, true} { name := "unchanged" if changed { @@ -110,7 +116,7 @@ func TestBootstrap_IfNeededSkipsOnlyUnchangedReadyEnvironment(t *testing.T) { return gitrepo.SyncResult{Revision: revision, Changed: changed, Status: status}, err }} var stdout, stderr bytes.Buffer - code := Execute(t.Context(), []string{"--app-root", root, "--output", "ndjson", "bootstrap", "--version", "v5.4.0", "--if-needed"}, + code := Execute(t.Context(), []string{"--app-root", root, "--output", "ndjson", "bootstrap", "--version", "v5.4.0", option}, IO{In: strings.NewReader(""), Out: &stdout, Err: &stderr}, WithCWD(root), WithEnvironmentFactory(func(*config.Layout) (environmentService, error) { return environment, nil }), WithWorkspaceFactory(func(*config.Layout) (workspaceService, error) { return workspace, nil }), @@ -182,6 +188,13 @@ func TestBootstrap_IfNeededRebuildsWhenVenvIsBroken(t *testing.T) { } func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { + for _, option := range []string{"--if-needed", "--reuse-ready-environment"} { + t.Run(option, func(t *testing.T) { testBootstrapDependencyReadiness(t, option) }) + } +} + +func testBootstrapDependencyReadiness(t *testing.T, option string) { + t.Helper() unsynchronized := &commandError{ code: protocol.CodeDependencySyncFailed, stage: protocol.StageDependenciesCheck, message: "主项目依赖环境未同步", details: map[string]any{"exitCode": 1}, @@ -192,19 +205,32 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { cause: &commandError{code: protocol.CodeUVExecFailed, stage: protocol.StageDependenciesCheck, details: map[string]any{"failureKind": "nonzero_exit"}}, } tests := []struct { - name string - result uv.DependenciesResult - err error - offline bool - wantSync bool - wantCode protocol.Code + name string + result uv.DependenciesResult + err error + offline bool + wantSync bool + wantCode protocol.Code + pythonErr error + postErr error + postOutputFault bool }{ {name: "healthy", result: uv.DependenciesResult{LockfileChecked: true, Synchronized: true}}, {name: "healthy offline", result: uv.DependenciesResult{LockfileChecked: true, Synchronized: true}, offline: true}, {name: "base interpreter missing", err: interpreterMissing, wantSync: true}, {name: "packages missing", err: unsynchronized, wantSync: true}, {name: "packages missing offline", err: unsynchronized, offline: true, wantSync: true}, + {name: "repair leaves interpreter broken", err: interpreterMissing, postErr: interpreterMissing, wantSync: true, wantCode: protocol.CodeDependencySyncFailed}, + {name: "post repair output fails", err: unsynchronized, postErr: &commandError{code: protocol.CodeOutputWriteFailed, stage: protocol.StageDependenciesCheck}, wantSync: true, wantCode: protocol.CodeOutputWriteFailed}, + {name: "post check progress output fails", err: interpreterMissing, postOutputFault: true, wantSync: true, wantCode: protocol.CodeOutputWriteFailed}, {name: "unknown check result", wantSync: true}, + {name: "managed Python missing", pythonErr: &commandError{code: protocol.CodePythonVersionMismatch, stage: protocol.StagePythonCheck, details: map[string]any{"exitCode": 1}}, wantSync: true}, + {name: "managed Python spawn failure", pythonErr: &commandError{code: protocol.CodeUVExecFailed, stage: protocol.StagePythonCheck}, wantCode: protocol.CodeUVExecFailed}, + {name: "managed Python cancelled", pythonErr: context.Canceled, wantCode: protocol.CodeOperationCancelled}, + {name: "managed Python wrapped stream failure", pythonErr: &commandError{ + code: protocol.CodePythonVersionMismatch, stage: protocol.StagePythonCheck, details: map[string]any{"exitCode": 1}, + cause: &commandError{code: protocol.CodeUVExecFailed, stage: protocol.StagePythonCheck, details: map[string]any{"failureKind": "output_read_failed"}}, + }, wantCode: protocol.CodePythonVersionMismatch}, {name: "cancelled", err: context.Canceled, wantCode: protocol.CodeOperationCancelled}, {name: "uv spawn failed", err: &commandError{code: protocol.CodeUVExecFailed, stage: protocol.StageDependenciesCheck}, wantCode: protocol.CodeUVExecFailed}, {name: "output failed", err: &commandError{code: protocol.CodeOutputWriteFailed, stage: protocol.StageDependenciesCheck}, wantCode: protocol.CodeOutputWriteFailed}, @@ -218,6 +244,9 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { }, wantCode: protocol.CodeDependencySyncFailed}, } for _, test := range tests { + if (test.pythonErr != nil || test.postErr != nil || test.postOutputFault) && option != "--reuse-ready-environment" { + continue + } t.Run(test.name, func(t *testing.T) { root := t.TempDir() layout, err := config.NewLayout(root, root) @@ -230,6 +259,14 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { base := &m5TestEnvironment{calls: &log.calls} environment := &bootstrapReadinessEnvironment{m5TestEnvironment: base} commit := strings.Repeat("a", 40) + pythonChecked := false + environment.checkPython = func(_ context.Context, request uv.PythonRequest) (uv.PythonCheckResult, error) { + pythonChecked = true + if !request.MirrorPolicy.Offline() || request.PythonInstallDir != layout.PythonDir() || request.ProjectEnvDir != layout.VenvDir() || request.Commit != commit || request.Line == nil { + t.Fatalf("Python check must use offline managed identity: %+v", request) + } + return uv.PythonCheckResult{Spec: uv.PythonSpec{Version: uv.PythonVersion{Major: 3, Minor: 12, Patch: 10}}}, test.pythonErr + } checked := false environment.check = func(_ context.Context, request uv.DependenciesRequest) (uv.DependenciesResult, error) { checked = true @@ -237,6 +274,9 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { request.PythonVersion != "3.12.10" || request.Commit != commit || request.Branch != "release/v5.4.0" || request.OperationID == "" || request.Line == nil { t.Fatalf("check request = %+v, want offline managed identity and operation log", request) } + if base.syncCalls > 0 { + return uv.DependenciesResult{LockfileChecked: true, Synchronized: true}, test.postErr + } return test.result, test.err } store := &m5TestStateStore{calls: &log.calls, initial: state.EnvironmentState{ @@ -246,13 +286,17 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { revision, err := gitrepo.NewRevision("v5.4.0", "release/v5.4.0", commit, "github") return gitrepo.SyncResult{Revision: revision, Status: protocol.StateReadyToStart}, err }} - args := []string{"--app-root", root, "--output", "ndjson", "bootstrap", "--version", "v5.4.0", "--if-needed"} + args := []string{"--app-root", root, "--output", "ndjson", "bootstrap", "--version", "v5.4.0", option} if test.offline { args = append([]string{"--offline"}, args...) } else { args = append([]string{"--mirror", "package-index=aliyun"}, args...) } - var stdout, stderr bytes.Buffer + var stderr bytes.Buffer + stdout := bootstrapPostCheckOutput{ + shouldFail: func() bool { return test.postOutputFault && base.syncCalls > 0 }, + fault: errors.New("injected post-check progress output failure"), + } code := Execute(t.Context(), args, IO{In: strings.NewReader(""), Out: &stdout, Err: &stderr}, WithCWD(root), WithEnvironmentFactory(func(*config.Layout) (environmentService, error) { return environment, nil }), WithWorkspaceFactory(func(*config.Layout) (workspaceService, error) { return workspace, nil }), @@ -266,10 +310,14 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { return log, nil }), ) - if !checked { - t.Fatal("dependency check was not called") + if pythonChecked != (option == "--reuse-ready-environment") || checked != (test.pythonErr == nil || (option == "--reuse-ready-environment" && base.syncCalls > 0)) { + t.Fatalf("python checked=%t, dependencies checked=%t", pythonChecked, checked) } - if test.wantCode != "" { + if test.postOutputFault { + if code != 20 || !stdout.failed || !strings.Contains(stderr.String(), stdout.fault.Error()) { + t.Fatalf("output failure not preserved: exit=%d, injected=%t, stderr=%s", code, stdout.failed, stderr.String()) + } + } else if test.wantCode != "" { events := parseNDJSON(t, stdout.String()) last := events[len(events)-1] if code == 0 || last.object["code"] != string(test.wantCode) { @@ -285,7 +333,11 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { t.Fatalf("python calls=%d, state writes=%v, want no preparation", base.pythonPrepareCalls, store.writes) } if test.wantSync { - if base.pythonPrepareCalls != 1 || len(store.writes) != 1 || store.writes[0].Status != protocol.StateReadyToStart { + wantStatus := protocol.StateReadyToStart + if test.wantCode != "" { + wantStatus = protocol.StateEnvironmentBroken + } + if base.pythonPrepareCalls != 1 || len(store.writes) != 1 || store.writes[0].Status != wantStatus { t.Fatalf("python calls=%d, writes=%v, want prepared ready environment", base.pythonPrepareCalls, store.writes) } if base.dependencyRequest.MirrorPolicy.Offline() != test.offline { @@ -303,7 +355,35 @@ func TestBootstrap_IfNeededChecksDependencyReadiness(t *testing.T) { type bootstrapReadinessEnvironment struct { *m5TestEnvironment - check func(context.Context, uv.DependenciesRequest) (uv.DependenciesResult, error) + check func(context.Context, uv.DependenciesRequest) (uv.DependenciesResult, error) + checkPython func(context.Context, uv.PythonRequest) (uv.PythonCheckResult, error) +} + +// bootstrapPostCheckOutput 在同步后的复验开始事件注入真实输出通道故障。 +type bootstrapPostCheckOutput struct { + bytes.Buffer + shouldFail func() bool + fault error + failed bool +} + +func (w *bootstrapPostCheckOutput) Write(payload []byte) (int, error) { + if w.shouldFail() { + var event struct { + Type string `json:"type"` + Stage protocol.Stage `json:"stage"` + Status protocol.ProgressStatus `json:"status"` + } + if json.Unmarshal(payload, &event) == nil && event.Type == "progress" && event.Stage == protocol.StageDependenciesCheck && event.Status == protocol.ProgressRunning { + w.failed = true + return 0, w.fault + } + } + return w.Buffer.Write(payload) +} + +func (s *bootstrapReadinessEnvironment) CheckPython(ctx context.Context, request uv.PythonRequest) (uv.PythonCheckResult, error) { + return s.checkPython(ctx, request) } func (s *bootstrapReadinessEnvironment) CheckDependencies(ctx context.Context, request uv.DependenciesRequest) (uv.DependenciesResult, error) { diff --git a/internal/cli/version.go b/internal/cli/version.go index f69bb20..1ba4b67 100644 --- a/internal/cli/version.go +++ b/internal/cli/version.go @@ -68,12 +68,13 @@ func runVersion( return sessionSuccess{ message: "版本信息查询完成", details: map[string]any{ - "runtimeVersion": info.Version, - "protocolVersion": info.Protocol, - "commit": info.Commit, - "buildDate": info.BuildDate, - "goVersion": info.GoVersion, - "alphaBackend": "dev", + "runtimeVersion": info.Version, + "protocolVersion": info.Protocol, + "commit": info.Commit, + "buildDate": info.BuildDate, + "goVersion": info.GoVersion, + "alphaBackend": "dev", + "bootstrapEnvironmentReuse": true, }, }, nil } diff --git a/internal/cli/version_test.go b/internal/cli/version_test.go index 7a96387..dcb79cb 100644 --- a/internal/cli/version_test.go +++ b/internal/cli/version_test.go @@ -58,6 +58,9 @@ func TestVersionCommand_NDJSONDetails(t *testing.T) { if got := details["alphaBackend"]; got != "dev" { t.Errorf("details.alphaBackend = %v, want dev", got) } + if got := details["bootstrapEnvironmentReuse"]; got != true { + t.Errorf("details.bootstrapEnvironmentReuse = %v, want true", got) + } if got := details["protocolVersion"]; got != float64(protocol.Version) { t.Errorf("details.protocolVersion = %v, want %d", got, protocol.Version) } diff --git a/scripts/benchmark-bootstrap-reuse.ps1 b/scripts/benchmark-bootstrap-reuse.ps1 new file mode 100644 index 0000000..d937e34 --- /dev/null +++ b/scripts/benchmark-bootstrap-reuse.ps1 @@ -0,0 +1,153 @@ +param( + [Parameter(Mandatory)][string]$SourceRoot, + [string]$BaseRef = 'HEAD', + [ValidateRange(3, 20)][int]$Pairs = 5, + [string]$OutputRoot = (Join-Path $env:TEMP ('mas-bootstrap-reuse-' + [guid]::NewGuid().ToString('N'))) +) +$ErrorActionPreference = 'Stop' +$repoRoot = Split-Path -Parent $PSScriptRoot +$source = (Resolve-Path -LiteralPath $SourceRoot).Path.TrimEnd('\') +$output = [IO.Path]::GetFullPath($OutputRoot).TrimEnd('\') +if ($output -eq $source -or $output.StartsWith($source + '\', [StringComparison]::OrdinalIgnoreCase)) { + throw 'Benchmark output must be outside the source installation' +} +if (Test-Path -LiteralPath $output) { throw 'Benchmark output must be a new directory' } +New-Item -ItemType Directory -Path $output | Out-Null +$appRoot = Join-Path $output 'app' +New-Item -ItemType Directory -Path $appRoot | Out-Null +$baseline = & git -C $repoRoot rev-parse $BaseRef +if ($LASTEXITCODE -ne 0) { throw 'Cannot resolve baseline' } +$overlayMap = @{} +foreach ($relative in @('internal/cli/bootstrap.go', 'internal/cli/version.go')) { + $content = & git -C $repoRoot show ('{0}:{1}' -f $baseline, $relative) + if ($LASTEXITCODE -ne 0) { throw 'Cannot read baseline source' } + $saved = Join-Path $output ([IO.Path]::GetFileName($relative)) + [IO.File]::WriteAllText($saved, ($content -join "`n") + "`n", [Text.UTF8Encoding]::new($false)) + $overlayMap[(Join-Path $repoRoot $relative)] = $saved +} +$overlayPath = Join-Path $output 'baseline-overlay.json' +@{ Replace = $overlayMap } | ConvertTo-Json | Set-Content -LiteralPath $overlayPath -Encoding utf8 +$oldExe = Join-Path $output 'baseline.exe' +$newExe = Join-Path $output 'candidate.exe' +Push-Location $repoRoot +try { + & go build -buildvcs=false -overlay $overlayPath -o $oldExe ./cmd/auto-mas-runtime + if ($LASTEXITCODE -ne 0) { throw 'Baseline build failed' } + & go build -buildvcs=false -o $newExe ./cmd/auto-mas-runtime + if ($LASTEXITCODE -ne 0) { throw 'Candidate build failed' } +} finally { Pop-Location } + +# 只复制受管程序文件,不复制用户配置、任务、后端事务或指向其他目录的 Junction。 +$stateSource = Join-Path $source 'runtime-state/environment.json' +$sourceStateBytes = [IO.File]::ReadAllBytes($stateSource) +$sourceStateHash = [Convert]::ToHexString([Security.Cryptography.SHA256]::HashData($sourceStateBytes)) +foreach ($relative in @('repo', 'runtime/tools', 'runtime/environment/python', 'runtime/environment/venv')) { + $from = Join-Path $source $relative + $to = Join-Path $appRoot $relative + & robocopy $from $to /E /COPY:DAT /R:1 /W:1 /XJ /NFL /NDL /NP /NJH /NJS | Out-Null + if ($LASTEXITCODE -ge 8) { throw ('Snapshot copy failed: ' + $relative) } +} +if ((Get-FileHash -LiteralPath $stateSource -Algorithm SHA256).Hash -ne $sourceStateHash) { + throw 'Source environment changed during snapshot; rerun with a stable source' +} +$stateDir = Join-Path $appRoot 'runtime-state' +New-Item -ItemType Directory -Path $stateDir | Out-Null +[IO.File]::WriteAllBytes((Join-Path $stateDir 'environment.json'), $sourceStateBytes) +$initial = Get-Content -LiteralPath (Join-Path $stateDir 'environment.json') -Raw | ConvertFrom-Json +$version = $initial.lastSuccessful.version +if ($initial.status -ne 'ready_to_start' -or -not $version) { throw 'Source must be a prepared environment' } +$venvConfig = Join-Path $appRoot 'runtime/environment/venv/pyvenv.cfg' +$cfg = Get-Content -LiteralPath $venvConfig -Raw +$cfg = $cfg.Replace($source, $appRoot) +$homeSetting = ($cfg -split "`r?`n" | Where-Object { $_ -match '^home\s*=' }) +if ($homeSetting.Count -ne 1) { throw 'Snapshot venv must have one Python home' } +$pythonHome = [IO.Path]::GetFullPath(($homeSetting -split '=', 2)[1].Trim()) +if (-not $pythonHome.StartsWith((Join-Path $appRoot 'runtime/environment/python') + '\', [StringComparison]::OrdinalIgnoreCase)) { + throw 'Snapshot venv must use its isolated managed Python' +} +Set-Content -LiteralPath $venvConfig -Value $cfg -Encoding utf8 +Write-Output ('Isolated snapshot: ' + $appRoot) + +function Measure-Bootstrap { + param([string]$Exe, [bool]$Reuse, [string]$Label) + $stdoutPath = Join-Path $output ($Label + '.ndjson') + $stderrPath = Join-Path $output ($Label + '.stderr.txt') + $arguments = @('--app-root', $appRoot, '--output', 'ndjson', 'bootstrap', '--version', $version) + if ($Reuse) { $arguments += '--reuse-ready-environment' } + $watch = [Diagnostics.Stopwatch]::StartNew() + & $Exe @arguments > $stdoutPath 2> $stderrPath + $exitCode = $LASTEXITCODE + $watch.Stop() + $events = @(Get-Content -LiteralPath $stdoutPath | ForEach-Object { $_ | ConvertFrom-Json }) + $result = @($events | Where-Object type -eq 'result')[-1] + if ($exitCode -ne 0 -or -not $result.success) { + throw ('Bootstrap failed in {0}: exit={1}, code={2}; see {3}' -f $Label, $exitCode, $result.code, $stdoutPath) + } + if ($result.details.version -cne $version -or $result.details.commit -cne $initial.lastSuccessful.commit) { + throw ('Bootstrap identity changed in {0}; expected {1}/{2}, got {3}/{4}; see {5}' -f $Label, $version, $initial.lastSuccessful.commit, $result.details.version, $result.details.commit, $stdoutPath) + } + $pythonPrepared = @($events | Where-Object stage -eq 'python.install').Count -gt 0 + $dependenciesSynced = @($events | Where-Object stage -eq 'dependencies.sync').Count -gt 0 + $checked = @($events | Where-Object stage -eq 'dependencies.check').Count -gt 0 + if ($Reuse -and (-not $result.details.unchanged -or $pythonPrepared -or $dependenciesSynced -or -not $checked)) { + throw ('Candidate did not prove healthy reuse: ' + $stdoutPath) + } + if (-not $Reuse -and (-not $pythonPrepared -or -not $dependenciesSynced)) { + throw ('Control did not perform full preparation: ' + $stdoutPath) + } + $row = [pscustomobject]@{ + Label = $Label; Reuse = $Reuse; DurationMs = [math]::Round($watch.Elapsed.TotalMilliseconds, 2) + Version = $result.details.version; Commit = $result.details.commit + ProbeEvents = @($events | Where-Object stage -eq 'network.probe').Count + PythonPrepared = $pythonPrepared; DependenciesSynced = $dependenciesSynced + ReadinessChecked = $checked; LogPath = $stdoutPath + } + Write-Host ('{0}: {1} ms, probes={2}' -f $Label, $row.DurationMs, $row.ProbeEvents) + return $row +} + +$warmups = @() +$warmups += Measure-Bootstrap $oldExe $false 'warmup-baseline' +$warmups += Measure-Bootstrap $newExe $true 'warmup-candidate' +$rows = @() +for ($pair = 1; $pair -le $Pairs; $pair++) { + if ($pair % 2 -eq 1) { + $rows += Measure-Bootstrap $oldExe $false ('{0}-baseline' -f $pair) + $rows += Measure-Bootstrap $newExe $true ('{0}-candidate' -f $pair) + } else { + $rows += Measure-Bootstrap $newExe $true ('{0}-candidate' -f $pair) + $rows += Measure-Bootstrap $oldExe $false ('{0}-baseline' -f $pair) + } +} +function Get-Median { + param([double[]]$Values) + $sorted = @($Values | Sort-Object) + $middle = [int][math]::Floor($sorted.Count / 2) + if ($sorted.Count % 2 -eq 1) { return $sorted[$middle] } + return ($sorted[$middle - 1] + $sorted[$middle]) / 2 +} +$oldMedian = Get-Median @($rows | Where-Object { -not $_.Reuse } | ForEach-Object DurationMs) +$newMedian = Get-Median @($rows | Where-Object Reuse | ForEach-Object DurationMs) +$savedMs = [math]::Round($oldMedian - $newMedian, 2) +$metadata = [ordered]@{ + Baseline = $baseline; Version = $version; Commit = $initial.lastSuccessful.commit + AppRoot = $appRoot; BaselineSHA256 = (Get-FileHash -LiteralPath $oldExe).Hash + CandidateSHA256 = (Get-FileHash -LiteralPath $newExe).Hash + Warmups = $warmups; Samples = $rows; BaselineMedianMs = $oldMedian; CandidateMedianMs = $newMedian; SavedMs = $savedMs +} +$metadata | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $output 'comparison.json') -Encoding utf8 +$report = @( + '# Bootstrap 健康环境复用实测', '', + ('基线 {0};目标 {1},Commit {2}。' -f $baseline, $version, $initial.lastSuccessful.commit), + '同一隔离快照、真实 uv、交替顺序;预热不计入中位数。不启动后端,不修改安装版。', '', + '| 样本 | 毫秒 | 探针事件数 | Python 准备 | 依赖同步 | 就绪复验 |', + '| --- | ---: | ---: | --- | --- | --- |' +) +foreach ($row in $rows) { + $report += '| {0} | {1} | {2} | {3} | {4} | {5} |' -f $row.Label, $row.DurationMs, $row.ProbeEvents, $row.PythonPrepared, $row.DependenciesSynced, $row.ReadinessChecked +} +$report += '' +$report += '基线中位数 {0:N2} ms;候选 {1:N2} ms;减少 {2:N2} ms。' -f $oldMedian, $newMedian, $savedMs +$report += '仅代表该机器健康环境的完整 bootstrap,不等于整机启动或首次安装收益。源码校验与 Git 网络阶段仍保留,约 24 秒异常未由此实验解释。' +$report | Set-Content -LiteralPath (Join-Path $output 'comparison.md') -Encoding utf8 +Write-Output ('Report: ' + (Join-Path $output 'comparison.md'))