Skip to content

fix(workspace): reclaim workspaces that contain read-only directories - #1412

Merged
Robert0622 merged 2 commits into
mainfrom
fix/workspace-readonly-reclaim
Nov 26, 2026
Merged

Robert0622 merged 2 commits into
mainfrom
fix/workspace-readonly-reclaim

Conversation

@Robert0622

Copy link
Copy Markdown
Collaborator

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.RemoveWorkspaceTree retries a permission-denied removal after it adds owner rwx to every real directory below the workspace. It walks inside an os.Root, so symlinks are not followed. It is used for:

  • platform reclaim (Manager.DestroyRun / removeMatching), which also covers peer-host reclaim
  • wiping a workspace before it is reopened (Manager.open)
  • ephemeral sandbox close
  • Runner rt-cleanup tasks (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 CLI grok-4.7 with high effort. It did not run against this repository. The SOP had four steps: a Cursor node writes out/model.md, a script adds notes.txt, a 6 MiB binary and a chmod 555/500 module cache, then an approval gate, then a verify script. The test aged the run past the idle hour and then approved it.

platform-local workspace f7b066d (before #1410) 14e17fa / main this PR
idle reclaim unlinkat …: permission denied on every 20 s sweep, never reclaimed same reclaimed in 6 s
files left on disk only gomod/ only gomod/ none
checkpoint after sweeps replaced by the partial tree (274 bytes, inline) replaced by the partial tree (273 bytes, chunks) full (6.29 MB, 2 chunks)
approve → verify fails: notes.txt, the model file and the binary are gone same succeeds; modes 500 500 555 555 444 preserved
Runner workspace f7b066d 14e17fa / main this PR
idle save validation fails (mkdirat … permission denied), workspace kept validates; rt-cleanup then fails every 40 s validates; cleanup succeeds
result nothing lost, never reclaimed Runner workspace keeps only gomod/; checkpoint re-saved as 228 bytes restore brings back notes.txt and the read-only .mod

Validation

  • New TestRemoveWorkspaceTreeRemovesRestrictedDirectoriesOnly covers modes 0555, 0500 and 0000. The fixture first checks that os.RemoveAll fails on the tree. The test also checks that a symlink target outside the workspace keeps its mode and contents.
  • New TestConversationWorkspaceReclaimsAndRestoresReadOnlyDirectories runs two reclaim and restore cycles. It fails on main with first reclaim: workspace was not reclaimed.
  • TestExecuteRunCleanupSafeAndUnsafe now includes a read-only module cache. It fails on main with remove runner workspace: unlinkat …: permission denied.
  • Ran go vet on runtime, runner and engine, plus focused -race -count=3. The new file also vets for GOOS=windows. A full go test of 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.
  • API e2e against this build: 169 passed, 0 failed, 16 skipped. The skipped ones are the model-gated cases.
  • Deployment scenarios from fix(workspace): reduce checkpoint writes and restore read-only directories #1410 still hold on this build:
    • During a preview cleanup hold past the idle hour, 140 s of sweeps rewrote the checkpoint 1 time with 0 MiB binlog. f7b066d rewrote it 8 times with 336 MiB.
    • A late edit made during the hold is captured once the hold clears.
    • Rolling upgrade works in both directions: an old-build inline checkpoint restores on this build and is saved again as chunks, and the old build restores that chunked checkpoint.

Scope

  • Platform core / API / runner

Safety

  • I did not include credentials, internal endpoints, production data, or organization-specific infrastructure.
  • I preserved user files and unrelated changes.

🤖 Generated with Claude Code

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
crispinhu27 previously approved these changes Nov 25, 2026

@crispinhu27 crispinhu27 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

初审 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 vet runtime/runner/engine 都干净;新文件单独用 GOOS=windows vet 通过。
  • 补测的边界情况都符合预期:三层嵌套的 0000 目录、根目录本身是 0000、路径不存在、路径是文件、根目录是指向外部的符号链接、目录内部有指向外部 0000 目录的绝对或相对符号链接、对内部目录做符号链接替换的竞争(约 1200 次)。外部目标都没有被改动。
  • CI 15/15 通过。

未做的验证:-race(容器里没有 cgo)、真实 Windows/macOS 运行、需要 MySQL 的测试、实机部署场景(以 PR 描述和 CI 为准)。

行内有一条非阻塞的 P3:根目录本身被替换时的保护缺口,影响很低,可以后续跟进。

Comment thread internal/runtime/workspace_remove.go Resolved
@Robert0622

Copy link
Copy Markdown
Collaborator Author

CR + 部署实测:91685bab,LGTM

评审结论

  • RemoveWorkspaceTree 先按原样 os.RemoveAll,只有遇到 ErrPermission 才给目录补属主 rwx 再删一次,正常路径没有额外开销。WalkDir 在读目录之前先回调,所以连 000 的目录也能先改权限再进去;os.OpenRoot 加上「只对真目录 chmod」,保证不会顺着软链改到工作区外面。
  • 平台回收(removeMatching)、重开前清空(open 的 wipe)、临时沙箱关闭、Runner 清理任务这 4 处都换成了新函数。换不了的情况(比如子目录属于别的 uid)仍然返回原来的错误,不会卡住。
  • 顺带提一句(不在本 PR 范围,非阻塞):internal/engine/repository_workspace.go:426 的 resetCloneDestination 重新 clone 前,仍用 os.RemoveAll 清空工作区。工作区里有只读目录时,会报 clear workspace …: permission denied,不会丢数据,以后可以一起换掉。

部署实测(本机 MySQL 8.4.7,main 2e5176f1 和 PR head 各起一套平台,平台以非 root 用户运行,native 沙箱,没有对着 LoopForge 仓库测)

场景:Cursor grok-4.7 high 节点在工作区写 out/model.md,脚本节点写入 notes.txt、6 MiB 二进制,以及一棵 Go module cache 形状的只读目录树(目录 500/555,文件 444),然后在人工审批节点停住。把运行时间往前调过 1 小时空闲阈值,等空闲回收;回收后点批准,由校验节点检查所有文件和权限。

检查 main 2e5176f1 PR 91685bab
模型写文件、停在审批 通过 通过
空闲回收(存检查点并删除工作区) 120 秒内没完成,平台日志每 20 秒报一次 unlinkat …/v1.0.0.mod: permission denied(共 12 次),磁盘上只剩 gomod 16 秒完成,工作区目录已删除
批准后恢复,校验节点 失败:grep: notes.txt: No such file or directory,残缺目录被当成工作区继续用 成功,6 MiB 文件 sha256 一致
恢复后的权限 — 500 500 555 555 444,和原来一样
模型写的 out/model.md 丢失 还在

不带模型、只用脚本节点的同一场景也跑了一遍:main 2/6,PR 6/6。

边界用例(直接调用 runtime.RemoveWorkspaceTree)

  • 工作区本身是 500,里面有 000 的子目录和 400 的父目录:os.RemoveAll 报 openfdat …: permission denied,新函数能删干净。
  • 工作区里有一个软链,指向外部的只读目录(555,里面有 444 文件):工作区删掉后,外部目录和文件的权限、内容都没变。
  • 传进来的路径本身是指向外部目录的软链:只删掉软链本身,目标目录不受影响。
  • 子目录属于 root、当前用户删不掉:返回 unlinkat …: permission denied,不会卡住;路径不存在时返回 nil。

测试

  • go test ./internal/runtime/ ./internal/runner/ ./internal/engine/:失败集合和 main 相同,都是本机没有 Docker、bwrap、浏览器造成的。PR 上多出的一个 TestWebSocketCDPTransportAndHostEndpoint 是并行负载下偶发失败,单独重跑 3 次都通过,main 上同样 3/3 通过。
  • CI 15/15 通过。
  • 没有另外实测执行机器(Runner)上的清理路径,靠 exec_edges_cases_test.go 覆盖。

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 crispinhu27 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复审: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 与 main 2e5176f1 的失败集合完全一致(都是 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,不代表代码没有任何缺陷。

@Robert0622
Robert0622 merged commit 5dabb61 into main Nov 26, 2026
15 checks passed
@Robert0622
Robert0622 deleted the fix/workspace-readonly-reclaim branch November 26, 2026 02:22
Robert0622 pushed a commit that referenced this pull request Nov 26, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants