Repository navigation
fix(desktop): 统一桌面端 10 处原子文件写入到共享 atomicfile(#2257) - #2351
Conversation
|
Lithe Review — 自查与 CI 纠正(最新 head 首轮 Linux CI 发现 最新提交的 Format and script tests、Verify pins、矩阵门禁与远端服务构建/测试均已通过;Linux Go 检查仍在进行,Windows 原生测试因 Draft 跳过。该 PR 仅完成 #2257 其中 2/10 处,仍需完整 CI 及独立审核,保持 Draft、不关闭 Issue、不合并。 |
|
Lithe Review(最新提交 c437ba0) 已纠正最初“2/10 处”的过时描述:本分支现在覆盖 #2257 列出的 10/10 处写入路径。共享 atomicfile 提供 Install、Write 与流式 WriteWith,保留 remoteserver 的竞争重命名测试钩子和各调用方权限;失败时清理临时文件。 最新 Linux CI 中 atomicfile、remoteserver、idehelp、conpty、store、draftstore、pluginruntime、logwindow、pluginservice、pluginhost、projectgen 共 11 个受影响 Go 包均通过;格式/脚本、矩阵、固定依赖、远端构建与测试也通过。完整 CI 未通过:settingswindow/workbench 的 Viva 现有 |
…re atomic publish (#2257)
|
Lithe Review|最终范围与验证摘要(head
原 PR 描述的“仅迁移 2/10”是早期状态,现已过时;请以本整合回复和 Diff 为准。 |
…257-install-atomicfile
…and update feature matrix (#2257)
…257-install-atomicfile # Conflicts: # shared/platform-feature-matrix/features/plugin-command-installers.json # shared/platform-feature-matrix/features/plugin-project-generators.json
关联 Issue / Linked issue
PR 类型 / Type
改动说明 / What changes
背景 / Why
Issue #2257 指出桌面端有 10 处各自实现“在同目录创建临时文件并改名替换目标文件”的原子写入逻辑,导致关键行为长期不一致:其中 4 处(
logwindow、pluginservice、pluginhost、projectgen)缺少fsync,断电或系统崩溃后可能留下空文件或半截文件;只有conpty与remoteserver处理了 Windows 下目标被并发进程/相同文件占用时的改名失败;各处临时文件命名、权限控制、错误清理与测试覆盖也分散在各个包中。原 Draft PR #2351 早期仅完成了“第一阶段(2/10 处)”,后续提交推进至全部 10 处但尚未同步 PR 标题/正文、功能矩阵记录以及本地 Go 1.27 + Rust Core 全量审计。本次审计合并最新
origin/main(3d7b5acb5),补强atomicfile共享包与pluginruntime遗弃临时文件清理缺陷,补齐缺失回归测试与 10 份功能矩阵记录,并完成 macOS 全量本地验证。改动 / Changes
desktop/internal/atomicfile(desktop/internal/atomicfile/atomicfile.go、desktop/internal/atomicfile/atomicfile_test.go):Write(path, data, perm)、Install(path, data, perm)、InstallWithRename(path, data, perm, rename)、WriteWith(path, perm, write)、WriteWithCheck(path, perm, write, beforeRename),内部通过writeWithRename统一执行同目录独占临时文件(os.CreateTemp(dir, "."+filepath.Base(path)+".*"))、写入、设置权限(f.Chmod(perm))、落盘同步(f.Sync())、关闭及rename发布,失败自动清理临时文件;.、..及目录分隔符(validateTargetPath),守卫write == nil与rename == nil;closed标记与errors.Join合并关闭错误,且os.Remove清理仅忽略os.ErrNotExist(附封闭// log-exempt:说明);Install/InstallWithRename通过os.Lstat与holds(path, data)在目标已为相同内容的常规文件时跳过重写(符号链接不视为已有目标);仅在rename阶段失败时通过acceptOnRenameError检查并发写者是否已写入相同字节,非rename阶段错误(写入、权限、fsync、关闭、beforeRename)即使目标恰好已含相同字节也绝不吞错;TestInstallDoesNotSwallowNonRenameErrorsEvenWhenTargetMatches、TestWriteAndInstallRejectInvalidPaths与TestWriteWithStreamsToFileDirectly(验证WriteWith直接流式写入临时文件而不缓冲整块字节)。desktop/internal/idehelp/idehelp.go(Install->atomicfile.Install,0o700)及idehelp_test.go(新增目录单文件无残留断言);desktop/internal/localshell/conpty/conpty.go(place->atomicfile.Install,0o700)及conpty_test.go(临时文件前缀断言同步为.);desktop/internal/remoteserver/remoteserver.go(installFile->atomicfile.InstallWithRename,0o600,保留测试rename注入钩子);desktop/internal/store/store.go((*Store).WriteFile->atomicfile.Write,0o600,保留os.MkdirAll(store.dir, 0o755));desktop/internal/draftstore/store.go((*Store).SaveSource->atomicfile.WriteWith,流式直写*os.File不在内存缓冲整份源码,0o600)及store_test.go(新增TestSaveSourceStreamingFailurePreservesPriorCheckpointAndLeavesNoTemp);desktop/internal/pluginruntime/runtime.go((*Manager).archive->atomicfile.WriteWithCheck,io.Copy流式直写,并在写入后及beforeRename两次检查ctx.Err(),0o600;同时修复removeAbandoned识别新的.archive.临时文件前缀与历史.archive-前缀)及runtime_test.go(新增TestPrepareCleansAbandonedAtomicArchiveTemps);desktop/internal/logwindow/export.go(writeAtomic->atomicfile.Write,0o600+fsync)及export_test.go(校验导出目录无.前缀临时文件残留);desktop/internal/pluginservice/pages.go((*Service).SetPageState->atomicfile.Write,0o600)及pages_test.go(补充SetPageState后无临时文件残留断言);desktop/internal/pluginhost/commands.go((*CommandStore).Record->atomicfile.Write,0o600,保留os.MkdirAll(directory, 0o755))及commands_test.go(同步清理前缀与非 Windows0o600权限断言);desktop/internal/projectgen/files.go(writeFile->atomicfile.Write,保留0o644/0o755权限、ensureDirectory与拒绝目录/符号链接目标)及run_test.go(同步清理前缀断言)。logwindow/export.go增补fsync对大文件导出的性能影响:darwin/arm64、APFS)运行临时对比基准BenchmarkWriteAtomicLargeExport(-benchtime=5x,测毕已移除):因 macOS(*os.File).Sync()调用fcntl(F_FULLFSYNC),1x10MiB从1,533,658 ns/op(1.53 ms)增至9,950,108 ns/op(9.95 ms,+8.4 ms);10x1MiB从2,038,425 ns/op(2.04 ms)增至63,423,225 ns/op(63.42 ms,+61.4 ms);最坏场景200x50KiB(maxExportFiles = 200)从26,770,125 ns/op(26.77 ms)增至1,128,218,875 ns/op(1.13 s,单次F_FULLFSYNC约5.5 ms)。导出执行在后台m.spawn协程且每个文件前均检查ctx.Err(),不阻塞主线程且可随时取消,保留atomicfile.Write与fsync。verificationStatus: "pending"):shared/platform-feature-matrix/features/下 10 份记录:diagnostics-export.json、editor-unsaved-recovery.json、ide-mcp-connection.json、plugin-command-installers.json、plugin-functions.json、plugin-managed-runtimes.json、plugin-project-generators.json、remote-development-workspace.json、runtime-package-integrity.json、terminal-shell.json。不在本 PR 范围内 / Out of scope
desktop/internal/documentio/write.go与desktop/internal/configfile/configfile.go:保存文档或工具配置文件时需要保留目标文件既有权限与扩展属性,语义不同,按 Issue [Feature] 桌面端 10 处各写一份临时文件改名的原子写入,fsync 和 Windows 改名失败处理不一致,合并为共用实现 #2257 明确排除;desktop/internal/marketplace/archive_cache.go:可重建的市场缓存故意不做fsync并依赖.incoming-前缀做 24 小时过期清理,维持现状;desktop/internal/pluginsettings/sources.go、desktop/internal/logstore/store.go及workbench的纯探测/临时文件CreateTemp调用:不以同目录改名原子替换目标文件,不迁移;.tmp+os.Rename写入(desktop/internal/processledger/processledger.go的(*Ledger).write与desktop/internal/loginenv/loginenv_unix.go的writeCache):按develop-lithe第九节单独建单,见重构 Issue [Feature] processledger 与 loginenv 仍手写固定 .tmp 文件改名写入,迁移到 atomicfile.Write #2376,不在本 PR 顺手改动;类型专属:fix
复现与根因
fsync,仅有conpty与remoteserver兼容 Windows 并发占用同字节改名失败)。atomicfile.InstallWithRename若在写入、设权限、fsync、关闭或beforeRename阶段失败,但目标文件当时恰好已包含相同字节,会在错误兜底分支误将该非rename错误吞掉返回nil;(2)pluginruntime.Manager.archive迁移为atomicfile.WriteWithCheck后临时文件命名从.archive-*变为.archive.*,但removeAbandoned仍只匹配.archive-,导致异常中断后的新格式临时文件无法在下次Prepare时被清理。git log -S或git blame查):#2231新增remoteserver.installFile时单独复制了conpty.place逻辑);上述两处审出缺陷由本分支早期提交(0d623d061、82dc8239b)引入,已在dcf7af69a修复。desktop/internal/atomicfileGo 实现。检查项
rename阶段误吞错、非法路径入参、WriteWith流式直写与pluginruntime.removeAbandoned前缀不匹配问题,已在修复同时补全desktop/internal/atomicfile/atomicfile_test.go、desktop/internal/pluginruntime/runtime_test.go与desktop/internal/draftstore/store_test.go对应回归并通过(未单独录制修复前失败输出);全部 10 处既有单测保留并同步修正临时文件前缀断言。rename失败与并发写者竞争)已由 Go 单元测试自动化覆盖;Windows 原生文件系统锁占用竞争与产品窗口手工/VM 验收步骤见shared/platform-feature-matrix/features/各条目及下方“未能执行的检查和原因”。desktop/internal全量检索CreateTemp(、os.Rename(与.tmp,核对 10 处迁移点及workbench等全部上游调用方,确认保留目录预创建、权限、ctx取消、只读/符号链接校验与错误返回契约。atomicfile同时覆盖 macOS 与 Windows;审计全量desktop/internal发现的另外两处预存固定.tmp改名实现已提交重构 Issue [Feature] processledger 与 loginenv 仍手写固定 .tmp 文件改名写入,迁移到 atomicfile.Write #2376).github/workflows/desktop-ci.yml).agents/notes/implemented/bug-fix/记录:未改变既有功能契约或架构边界,已校验现有 204 篇 Agent Note 无过时描述(./scripts/agent-notes/verify-agent-notes.sh通过)。范围检查 / Scope checks
功能矩阵 / Feature matrix
shared/platform-feature-matrix/features/<id>.json:两端的实现状态和验证状态、证据路径、owner、验证动作都如实填写;没有运行验证的保持pending(共更新 10 个能力文件,全部保持verificationStatus: "pending",并移除过时“第一阶段 2/10 / Go 1.23”描述)./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh;改了平台实现路径的,还运行了./scripts/platform-feature-matrix/verify-platform-feature-matrix-change.sh origin/main HEAD(两者均通过,校验 222 项平台能力).artifacts/下生成的矩阵视图(git ls-files .artifacts为空)插件归属 / Plugin ownership
develop-lithe第三节“插件归属与宿主零依赖”的自查命令,结果为空(desktop/下除dev.lithe.test.*/dev.lithe.fixture.*合成测试插件与第三方源禁止dev.lithe.前缀校验外,无任何具体插件依赖)shared/i18n/;插件的构建步骤、资源路径和补丁没有写进docs/ci-builds.md、Agent Note 或 Skill:本次未涉及任何具体插件或shared/i18n/、docs/ci-builds.md改动UI 验证 / UI evidence
N/A:本次改动不涉及布局、控件、样式、文案、菜单、弹窗或任何可见交互状态变化。
统一日志系统 / Structured logging
rust/logging/events/<root>.json声明,再用生成的事件类型和 span 记录;没有NSLog、os_log/Logger、直接写 stderr、print或手写事件 ID(atomicfile返回 Go 错误交由既有的 Core/调用方边界结构化日志处理,未新增NSLog、os_log、直接写 stderr 或手写事件 ID)atomicfile为底层原子写入库,不重复打日志,调用方终态与频次逻辑保持不变)path、错误用error类型,动态通道段来自已校验的 ID(atomicfile仅传递标准错误及清理注释// log-exempt: 预期的缺失:成功改名已移走本次独占临时文件。)scripts/logging/)已移除,等价检查恢复前在“验证”一节写明日志门禁未完成,不记为通过(未增删日志事件,已运行./scripts/desktop/desktop-go.sh test -v -count=1 ./internal/core -run TestEveryLoggedEventIsAcceptedByTheKernel校验 645 个操作 / 1478 处日志调用全部被内核接受;如仅指静态代码生成器旧脚本门禁,已在下方“未能执行的检查和原因”注明)界面文案 / i18n
N/A:未新增或修改任何用户可见文案或
shared/i18n/词条(已运行node scripts/i18n/verify-app-language-localization.mjs、node scripts/i18n/generate-i18n.mjs --check(12628/12628)与node --test --test-timeout=60000 scripts/i18n/test-i18n.mjs(62/62)确认无异常)。逻辑归属 / Logic placement
desktop/internal/atomicfile属于桌面 Go 原生文件系统 I/O 原子替换封装(CreateTemp/Chmod/Sync/Rename),按边界归属平台 Go 层,未新增业务规则或域解析逻辑refactorIssue:未修改可共享业务逻辑;全量核对中发现的另外两处遗留.tmp文件写入重构已建单 [Feature] processledger 与 loginenv 仍手写固定 .tmp 文件改名写入,迁移到 atomicfile.Write #2376性能与内存 / Performance and memory
draftstore.SaveSource与pluginruntime.archive分别通过WriteWith与WriteWithCheck直接流式写入临时文件,避免整文件缓冲进内存)logwindow/export.go导出在后台m.spawn协程执行且逐文件检查ctx.Err(),不阻塞 UI 主线程)logwindow/export.go新加fsync(macOS 为F_FULLFSYNC)前后基准已在“改动”一节给出实测对比(1x10MiB:1.53 ms -> 9.95 ms;10x1MiB:2.04 ms -> 63.42 ms;200x50KiB:26.77 ms -> 1.13 s),无新增依赖。设置 / Settings
N/A:未新增或修改任何设置项、设置页或工具来源配置逻辑。
插件契约 / Plugin contract
N/A:未修改 Rust Core 插件契约(
shared/plugins/contract/v1/)、能力版本、插件函数协议、PINNED_CORE_REVISION或任何宿主方法/通知。发行包与资源 / Bundle and resources
./scripts/desktop/verify/verify-runtime-bundle-immutability.sh(全部迁移站点仅写用户数据/缓存/状态/导出目录,不涉及发行包目录;已运行verify-runtime-bundle-immutability.sh与test-runtime-bundle-immutability.mjs均通过)docs/ci-builds.md“独立工作树的本地编译”一节写清路径、归属、生命周期和能否跨工作树复制:未新增任何缓存、下载或构建产物路径。无用代码与重构 / Dead code and refactoring
idehelp.go旧函数体)refactor标签的 Issue,没有在本 PR 顺手处理:[Feature] processledger 与 loginenv 仍手写固定 .tmp 文件改名写入,迁移到 atomicfile.Write #2376(desktop/internal/processledger/processledger.go与desktop/internal/loginenv/loginenv_unix.go仍手写固定.tmp+os.Rename)验证 / Verification
(cd desktop && gofmt -l .)git diff --check origin/main...HEAD./scripts/desktop/desktop-go.sh vet ./..../scripts/desktop/desktop-go.sh vet -tags dev ./..../scripts/desktop/desktop-go.sh --exec go run github.com/vivaui/viva/cmd/viva vet ./internal/atomicfile/... ./internal/idehelp/... ./internal/localshell/... ./internal/remoteserver/... ./internal/store/... ./internal/draftstore/... ./internal/pluginruntime/... ./internal/logwindow/... ./internal/pluginservice/... ./internal/pluginhost/... ./internal/projectgen/... ./internal/workbench/..../scripts/desktop/desktop-go.sh test -v -count=1 ./internal/core -run TestEveryLoggedEventIsAcceptedByTheKernel3.253s,645 actions / 1478 log calls)./scripts/desktop/desktop-go.sh test -race -tags 'dev vivahead' -count=1 ./internal/atomicfile ./internal/idehelp ./internal/localshell/conpty ./internal/remoteserver ./internal/store ./internal/draftstore ./internal/pluginruntime ./internal/logwindow ./internal/pluginservice ./internal/pluginhost ./internal/projectgenatomicfile 1.407s,idehelp 4.134s,conpty 4.714s,remoteserver 4.466s,store 3.259s,draftstore 2.413s,pluginruntime 3.450s,logwindow 6.392s,pluginservice 60.839s,pluginhost 5.327s,projectgen 9.387s)./scripts/desktop/desktop-go.sh test -race -tags 'dev vivahead' -timeout=20m -count=1 ./internal/workbench585.401s)./.agents/skills/write-stable-tests/scripts/test-stability-macos.sh --package ./internal/atomicfile ... ./internal/projectgen(11 包逐项计时)380 passed, 1 skipped:TestToolchainProviderFixtureProcess为夹具进程入口跳过;最大耗时10830ms<15000ms,新增/更新单测均为0–20ms)./.agents/skills/write-stable-tests/scripts/verify-test-stability.sh --base origin/main./scripts/platform-feature-matrix/verify-platform-feature-matrix-change.sh origin/main HEAD与./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh与node --test scripts/platform-feature-matrix/test-platform-feature-matrix.mjs5/5)./scripts/agent-notes/verify-agent-notes.sh204 active Agent Note(s) verified)node scripts/desktop/validate-pins.mjsviva与 Core9ab2336e8ca651e40baa5e65848b717b26f69e43均可达)./scripts/desktop/verify/verify-runtime-bundle-immutability.sh与node --test scripts/desktop/verify/test-runtime-bundle-immutability.mjs5/5)node scripts/i18n/verify-app-language-localization.mjs与node scripts/i18n/generate-i18n.mjs --check与node --test --test-timeout=60000 scripts/i18n/test-i18n.mjs12628/12628,单测62/62)BenchmarkWriteAtomicLargeExport(APFS、Apple M4、-benchtime=5x,测完移除)1x10MiB:1.53 ms -> 9.95 ms;10x1MiB:2.04 ms -> 63.42 ms;200x50KiB:26.77 ms -> 1.13 s未能执行的检查和原因:
go test(含 ConPTY DLL / 远端文件被进程占用时的atomicfile.Install/InstallWithRename竞争、Windows 文件权限及各功能端到端页面恢复验收):当前工作环境为 macOS(darwin/arm64),未在本机启动 Windows VM 执行,待 CI Windows 检查与后续 Windows VM 验收补齐;因此 10 份功能矩阵记录均保持verificationStatus: "pending"。scripts/logging/日志静态生成校验脚本已被上游移除(develop-lithe说明),等价完整日志声明静态门禁未执行(仅运行了TestEveryLoggedEventIsAcceptedByTheKernel内核目录验证,且本 PR 未增删任何日志事件)。lithevisual):本 PR 未涉及 UI 布局或可见交互改动。提交前确认 / Final checks
main创建;目标分支:feat、fix、docs 为main,release 以发布流程要求为准(已合并最新origin/main3d7b5acb5)gh pr ready).build/、target/、dist/、.artifacts/等产物,没有凭据、个人路径和未脱敏的日志或截图(git ls-files .artifacts为空,未修改.github/workflows/desktop-ci.yml)