Skip to content

fix(workspace): reduce checkpoint writes and restore read-only directories - #1410

Merged
Robert0622 merged 4 commits into
mainfrom
codex/reduce-workspace-checkpoint-writes
Nov 25, 2026
Merged

Robert0622 merged 4 commits into
mainfrom
codex/reduce-workspace-checkpoint-writes

Conversation

@Robert0622

@Robert0622 Robert0622 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

This is the smaller follow-up to #1223 and #1138. Save streamed workspace checkpoints in the existing chunk table, including small archives, so MySQL lease updates keep the archive bytes out of the row image. Preserve the last published chunks when a commit reply is lost.

Keep a current checkpoint while preview cleanup is on hold. Capture again before reclaiming so late workspace edits are included.

Restore files into a writable staging tree and apply directory modes before publishing it. The restricted-directory regression checks nested contents, original modes, and cleanup of the validation tree.

Scope

  • Platform core / API / runner
  • Public adapter / deployment
  • Classic
  • Portable
  • Shared behavior contract
  • Installer / CLI
  • Documentation only

Compatibility

The existing chunk schema and reader are used. Legacy inline checkpoints remain readable and convert when saved again. The previous main build also read a newly chunked checkpoint in the shared test database. Reclamation ownership and deployment settings are unchanged. The persistent Worker reclaim failure case remains in #1223.

Validation

  • In an isolated MySQL 8.4.13 FULL-row-image test with seven workspaces, database disk growth fell from 726.8 MiB to 137.8 MiB and binlog growth from 650.3 MiB to 70.2 MiB. Six compressed checkpoints totaling 38.0 MiB remained readable. The full fix(workspace): preserve complete checkpoints when reclaim fails #1223 produced the same volume in this workload.
  • In a separate initialized MySQL stack, six real DeepSeek Flash question-and-answer sessions added 388 KiB to the database directory and created no workspace checkpoints.
  • Exercised API/Worker capture, preview holds, a real Worker process restart, late-edit restore, active checkout protection, and a legacy-format round trip.
  • Tested chunk publication with a lost commit reply against SQLite and MySQL.
  • Restored nested read-only directories as an unprivileged Linux UID and checked the file modes, contents, and removal of temporary validation files.
  • Ran the focused Go engine, repository, and runtime tests, focused go vet, and the public-content check.

An isolated row-image comparison on an initialized database reduced the same heavy batch from 106.1 MiB to 90.1 MiB with NOBLOB. The 16 MiB difference was the old chunk images in the delete events. The application changes use the existing MySQL settings.

Safety

  • I did not include credentials, internal endpoints, production data, or organization-specific infrastructure.
  • I preserved user files and unrelated changes.
  • I updated generated Classic host bundles when applicable.
  • I documented third-party content and licensing changes when applicable.

@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 14e17fae(base / main f7b066d8)。审查了 7 个文件的全部改动:SaveWorkspaceCheckpointStream 一律写入 chunk 表,以及提交回复丢失时保留 chunk 的处理;预览清理暂停期间 claimIdleConversationWorkspace 的保存条件(heldWorkspaceCheckpointNeedsSave / workspaceCheckpointNeedsSave),以及恢复前的再次捕获;恢复时目录先以 0700 解包、再按深度从深到浅应用原始权限,失败时把权限改回去。

验证都在隔离环境中进行,用非特权 Linux 用户、无凭据、合成数据:

  • 在 internal/repo、internal/runtime、internal/engine 上跑 go vet 和 go test:head 和 base 的失败集合完全一致(各 102 项,原因都是本机缺 bwrap/Docker/Chromium),本 PR 没有新增失败。
  • 本 PR 新增的 TestWorkspaceCheckpointRestoresRestrictedDirectoryModes 在 head 上通过;放到 base 上会失败(mkdirat module-cache/version: permission denied),说明它能复现原缺陷。
  • TestConversationWorkspaceReusesCheckpointDuringPreviewHold 以及 Checkpoint/Publish/Backoff 相关测试通过。
  • 用本地 MySQL 8.4.13(binlog_row_image=FULL)跑了 TestMySQLWorkspaceCheckpointChunks、SharedBackoff、PublishCommitError(committed=true/false),head 上全部通过。
  • 在同一个 MySQL 库里做了新旧版本交替读写的兼容性检查:base 写入的内联小 checkpoint,head 能读;head 改写为 chunk 后,base 能读;base 再写回内联格式时,旧 chunk 被清理,head 仍能读。
  • 额外写了恢复边界的探测测试:tar 中子文件排在父目录条目之前;目录权限为 0000、0500、0555;同级目录 a-x 的排序;恢复后再次捕获并恢复,权限保持;发布阶段失败(目标目录在解包期间变为非空)时,父目录下没有残留 staging 目录,已有文件也没被改动。以上全部符合预期。
  • CI 16/16 通过。

没有发现阻塞问题。不阻塞的说明:本 PR 和 #1223 改的是同一个函数 claimIdleConversationWorkspace,先合入其中一个后,另一个需要 rebase。另外没有复现 PR 描述里的磁盘和 binlog 增长数据,那部分是性能结论,不影响正确性判断。

@Robert0622

Copy link
Copy Markdown
Collaborator Author

CR + 部署实测:14e17fae 可以合入,无需修改

代码

  • 保持期间:只在释放后或空闲满 1 小时后补存 1 次,之后复用这份检查点;解除保持后先存档再回收,所以保持期间改的文件也会存进去。保持改的是 workspace_cleanup_not_before,runRow.Updated 没有 autoUpdateTime,续期不会改 updated_at,因此不会反复触发存档。
  • 不论大小都走 chunk 表:提交回复丢失时不删新 chunk;提交确实失败时,多出来的 staging chunk 会在下次保存时被 archive_id <> ? 清掉,不影响旧检查点。
  • 只读目录:解包时目录先建成 0700,按路径倒序设回原权限(先子目录后父目录);发布失败时按反序改回 0700,保证能删掉;校验不设权限,临时目录能删干净。

测试(Go 1.26.11,MySQL 8.4.7,binlog_row_image=FULL)

  • go vet repo/runtime/engine 通过。在 main f7b066d8 和 PR 上分别跑 go test repo/runtime/engine,单独重跑后失败集合一致,都是本机缺 Docker、Chromium 和 git 身份导致的环境类用例。
  • internal/repo 的 Checkpoint 用例接 MySQL(AGENTFLOW_TEST_MYSQL_DSN)跑:TestMySQLWorkspaceCheckpointPublishCommitError 的 committed=true/false、TestMySQLWorkspaceCheckpointChunks、SharedBackoff 都通过。

部署实测(main 和 PR 各用一个全新库,native 沙箱,对比时只起一个平台,binlog 只算本平台写入)

场景 main f7b066d8 PR 14e17fae
H. succeeded run 处于 cleanup hold,空闲超过 1 小时(约 300KB 工作区) 5 轮 sweep 每轮都重新存档(saved_at 变了 6 次),105 秒内 binlog 增长 9.28 MB,检查点 inline 存在 archive 列 补存 1 次后不再存档,105 秒内 binlog 增长 0 B;inline 列长度为 0,存档写在 1 个 chunk 里
H'. 保持期间直接在磁盘上改文件,然后解除保持 回收后恢复,held.txt/late.txt sha 都通过 同样通过:回收前补存,晚改的文件也恢复了,回收后 2 轮 sweep 不再存档
只读目录恢复(把平台真实写出的检查点交给两边的 RestoreWorkspaceCheckpoint/Validate,非 root 运行) mkdirat ro/sub: permission denied,校验和恢复都失败 都成功:ro 500、ro/sub 555、ro/sub/deep 555,文件内容一致,校验用的临时目录没有残留
R. failed SOP 留下嵌套只读目录(500/555/555),空闲 1 小时后回收,再 Retry(root 运行平台,回收时能删掉) — 存档 1 次后回收,名额释放;Retry 恢复后 verify 节点输出 MODES=500,555,555,sha256sum -c 通过,run 成功
M. 真实模型(Cursor CLI grok-4.7 high):Agent 节点写 docs/locked/sub/info.md(含 RO-5521),并把两层目录 chmod 555;后续节点失败,空闲回收后 Retry — 检查点 469B 写进 chunk 表,回收 1 次;Retry 后权限仍为 555,555,模型写的文件逐字节一致,run 成功
滚动升级 / 回滚(同一个库) main 存的 inline 检查点 → PR 恢复,sha 通过,并改存成 chunk PR 存的 chunk 检查点 → 切回 main 后恢复,sha 通过

和本 PR 无关、已有的问题(不阻塞,供 #1223 参考):用非 root 跑 native 沙箱时,工作区里只要有嵌套只读目录,回收用的 os.RemoveAll 就会先删掉一部分文件,再报 unlinkat …/ro/top.txt: permission denied。下一轮 sweep 又会把删剩的目录重新存档,覆盖原来完整的检查点。main 上已经确认:检查点里只剩 ro/ 目录,根目录的 ro.sha 等文件都没了。PR 上日志也一样,每 20 秒存档一次、删除失败一次。本 PR 能恢复只读目录之后,恢复出来的工作区下次回收还会遇到这个问题。建议在 #1223 处理:删除前先把目录改成 u+w,或者删除失败后不要再重新存档。

@Robert0622
Robert0622 merged commit 2e5176f into main Nov 25, 2026
16 checks passed
@Robert0622
Robert0622 deleted the codex/reduce-workspace-checkpoint-writes branch November 25, 2026 19:31
@Robert0622

Copy link
Copy Markdown
Collaborator Author

Post-merge deployment check (local MySQL 8.4 FULL row image, native sandbox, Cursor grok-4.7 high; not run against this repository):

  • Hold and checkpoint reuse works. With a preview cleanup hold past the idle hour, 140 s of sweeps saved the checkpoint once with 0 MiB binlog. f7b066d saved it 8 times with 336 MiB. A late edit made during the hold is captured once the hold clears.
  • Rolling upgrade works both ways: inline → chunks → old build.
  • Lost commit reply works on SQLite and MySQL.
  • One remaining problem: a workspace with read-only directories, such as a Go module cache, is never reclaimed. os.RemoveAll deletes the writable files and stops. The next sweep then replaces the full checkpoint with the partial tree, so notes.txt, the model output and the binary are lost. On the platform path this already happened before this PR. On the Runner path it is new, because the validation fix now lets these trees through.

Fixed in #1412, with reproduction tables and tests.

Robert0622 pushed a commit that referenced this pull request Nov 25, 2026
…point-write-amplification

Resolve conflicts with #1410, a smaller follow-up subset of this branch:

- conversation_workspace.go / workspace_checkpoint_chunks.go: keep this
  branch's implementation; drop the duplicate cleanupHeld check and the
  duplicate heldWorkspaceCheckpointNeedsSave/workspaceCheckpointNeedsSave
  helpers the textual merge produced.
- workspace_checkpoint_publish_test.go: keep this branch's file, which
  already contains all of #1410's tests plus the rebind test.
- workspace_checkpoint_chunks_test.go: keep the chunked-archive assertion
  and add #1410's round-trip check of the repaired archive.
- #1410's runtime read-only directory restore and its new tests merge
  cleanly and are kept as is.

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