Repository navigation
fix(workspace): reclaim workspaces that contain read-only directories - #1412
Conversation
Restored checkpoints now keep read-only directories such as a Go module cache. Reclaim used os.RemoveAll, which stops at those directories after deleting the writable files. The next idle sweep captured that partial tree and replaced the complete checkpoint, so the deleted files were lost. Make restricted directories owner-writable inside an os.Root before retrying the removal, for platform, peer-host and Runner workspace cleanup. Co-Authored-By: Claude Code <noreply@anthropic.com>
crispinhu27
left a comment
There was a problem hiding this comment.
初审 head 91685ba(base main 2e5176f),结论 APPROVE。
审查范围:RemoveWorkspaceTree 本身,以及 4 处调用点:Manager.DestroyRun/removeMatching、Manager.open 清空、临时沙箱关闭、Runner executeRunCleanup。调用方的错误处理语义没有变,只有遇到权限错误时才会多走一遍 chmod。
实际验证(非 root 用户,隔离工作区):
- 新测试
TestRemoveWorkspaceTreeRemovesRestrictedDirectoriesOnly在 head 上通过。 TestExecuteRunCleanupSafeAndUnsafe在 head 上通过;base 上失败:remove runner workspace: unlinkat …/pkg/mod/go.mod: permission denied。TestConversationWorkspaceReclaimsAndRestoresReadOnlyDirectories在 head 上通过;拷到 base 上失败:first reclaim: workspace was not reclaimed。go test ./internal/runtime/ ./internal/runner/:head 和 base 的失败集合完全一致(37 项,都是缺 bwrap/Docker/Chromium 这类环境原因)。engine 里工作区相关子集,head 只比 base 少了那个新测试的失败。go vetruntime/runner/engine 都干净;新文件单独用GOOS=windowsvet 通过。- 补测的边界情况都符合预期:三层嵌套的 0000 目录、根目录本身是 0000、路径不存在、路径是文件、根目录是指向外部的符号链接、目录内部有指向外部 0000 目录的绝对或相对符号链接、对内部目录做符号链接替换的竞争(约 1200 次)。外部目标都没有被改动。
- CI 15/15 通过。
未做的验证:-race(容器里没有 cgo)、真实 Windows/macOS 运行、需要 MySQL 的测试、实机部署场景(以 PR 描述和 CI 为准)。
行内有一条非阻塞的 P3:根目录本身被替换时的保护缺口,影响很低,可以后续跟进。
|
CR + 部署实测: 评审结论
部署实测(本机 MySQL 8.4.7,main 场景:Cursor grok-4.7 high 节点在工作区写
不带模型、只用脚本节点的同一场景也跑了一遍:main 2/6,PR 6/6。 边界用例(直接调用
测试
|
makeDirectoriesOwnerWritable checked path with Lstat, then called
os.Chmod(path) and os.OpenRoot(path), both of which follow a symlink in
the last element. If the workspace was swapped for a symlink in between,
the walk added owner rwx to every directory of the outside tree.
Open the root first, require it to be the directory Lstat saw, and make
the root itself writable through the walk (root.Chmod(".")) instead of a
path-based chmod. A workspace root without owner read can no longer be
chmod-ed by path; it keeps the original removal error.
Co-Authored-By: Claude Code <noreply@anthropic.com>
crispinhu27
left a comment
There was a problem hiding this comment.
复审:head 5fd54f2e(base main 2e5176f1)。结论:APPROVE。
91685bab 上的 APPROVE(6064354259)已经因为新推送被 dismiss。这次重新审查了增量提交 5fd54f2e,改了 internal/runtime/workspace_remove.go,另加一个竞争测试。
历史问题
- [P3,非阻塞] 工作区根目录被并发换成符号链接时,chmod 会改到工作区外面(discussion_r4700364984):FIXED。新测试在
5fd54f2e上跑了 15 次全部通过;在91685bab上跑 3 次有 2 次失败。已在原线程回复并 resolve。
本次审查
- 改成
os.OpenRoot(path)后,用root.Stat(".")和Lstat的结果做os.SameFile比较;根目录本身的 chmod 也改由 WalkDir 第一次回调"."时调root.Chmod完成。这样所有改权限操作都作用在已经核对过身份的 fd 上。调用方(sandbox_manager.go、runner/daemon.go)的逻辑没有改。
实际验证(隔离工作区,非 root,不接触任何凭据)
go build ./...和go vet ./internal/runtime ./internal/runner ./internal/engine都通过。- 调用链用例:
TestExecuteRunCleanupSafeAndUnsafe(runner)和TestConversationWorkspaceReclaimsAndRestoresReadOnlyDirectories(engine)都通过。 - 对
./internal/runtime和./internal/runner跑了全量测试,head 与main2e5176f1的失败集合完全一致(都是 Docker、bwrap、Chromium 这类本机缺依赖的用例),本 PR 没有新增失败。 - 边界用例(直接调用
RemoveWorkspaceTree,工作区里有000、400、500、555的子目录和 444 文件):根目录为 700、755、555、500 时都能删干净;根目录是指向外部的符号链接,或内部有指向外部的软链时,外部目录的权限和内容都没变。 - CI:15 个 check run 全部通过(含
core)。
非阻塞说明(P3,不要求本 PR 处理):代码注释里写的是「根目录没有 owner 读权限时打不开」,实际上根目录缺少 owner 读或执行权限都会失败。根目录为 0400、0600 时,root.Stat(".") 报 statat .: permission denied;为 0100、0300 时 OpenRoot 打不开。这 4 种在 91685bab 上能删掉,现在会返回原来的删除错误。不过 main 上的 os.RemoveAll 同样删不掉这几种,所以和 base 相比不算退化;平台创建的工作区根目录是 0700 或 0755,也不会碰到。如果以后想覆盖这种情况,可以把注释改准确,或者对已打开的根目录 fd 直接 fchmod。
这次审查没有接真实模型做端到端部署:本次增量只改了删除函数,回收和恢复的调用链已经由上面的 engine 和 runner 用例覆盖。以上结论只针对 5fd54f2e,不代表代码没有任何缺陷。
Resolve the sandbox_manager.go conflict with #1412: removeMatching keeps its per-caller remove func, and both callers now delete through RemoveWorkspaceTree. DestroyRun retires a checkout and then removes it, and ReclaimExcept prunes orphans and retired copies; with os.RemoveAll a read-only Go module cache left .loopforge-reclaimed-* directories behind. Add regression tests for both paths. The second sop-mode test from #1411 builds its manager on sandboxRoot(t) like the rest of the file; this branch dropped the path/filepath import. Co-Authored-By: Claude Code <noreply@anthropic.com>
What changed
Follow-up to #1410, which was merged while it was being tested on a deployment.
Workspace reclaim removes the tree with
os.RemoveAll. When the workspace contains read-only directories, such as a Go module cache, that call deletes the writable files and then stops at the first directory it cannot write. The next idle sweep, 20 seconds later, captures the partial tree and replaces the complete checkpoint. The deleted files are lost, and the workspace is never reclaimed.On the platform-local path this already happens before #1410. On the Runner path the old checkpoint validation failed on these directories (
mkdirat …: permission denied), so the workspace was kept. #1410 fixed that validation, so the Runner path now hits the same loss.The new
runtime.RemoveWorkspaceTreeretries a permission-denied removal after it adds ownerrwxto every real directory below the workspace. It walks inside anos.Root, so symlinks are not followed. It is used for:Manager.DestroyRun/removeMatching), which also covers peer-host reclaimManager.open)rt-cleanuptasks (executeRunCleanup)Reproduction (main 2e5176f / #1410 head 14e17fa)
The test platform was deployed locally with MySQL 8.4,
binlog_row_image=FULL, a native sandbox, and the Cursor CLIgrok-4.7with high effort. It did not run against this repository. The SOP had four steps: a Cursor node writesout/model.md, a script addsnotes.txt, a 6 MiB binary and achmod 555/500module cache, then an approval gate, then a verify script. The test aged the run past the idle hour and then approved it.unlinkat …: permission deniedon every 20 s sweep, never reclaimedgomod/gomod/notes.txt, the model file and the binary are gone500 500 555 555 444preservedmkdirat … permission denied), workspace keptrt-cleanupthen fails every 40 sgomod/; checkpoint re-saved as 228 bytesnotes.txtand the read-only.modValidation
TestRemoveWorkspaceTreeRemovesRestrictedDirectoriesOnlycovers modes0555,0500and0000. The fixture first checks thatos.RemoveAllfails on the tree. The test also checks that a symlink target outside the workspace keeps its mode and contents.TestConversationWorkspaceReclaimsAndRestoresReadOnlyDirectoriesruns two reclaim and restore cycles. It fails on main withfirst reclaim: workspace was not reclaimed.TestExecuteRunCleanupSafeAndUnsafenow includes a read-only module cache. It fails on main withremove runner workspace: unlinkat …: permission denied.go veton runtime, runner and engine, plus focused-race -count=3. The new file also vets forGOOS=windows. A fullgo testof repo, runtime, runner and engine (MySQL DSN set) fails the same environment-only tests as main and adds none: Docker, Chromium and bwrap are missing here.Scope
Safety
🤖 Generated with Claude Code