Skip to content

feat(installer): apply sandbox migration 041 on Compose upgrades - #1304

Merged
crispinhu27 merged 2 commits into
mainfrom
feature/auto-sandbox-migration
Nov 24, 2026
Merged

crispinhu27 merged 2 commits into
mainfrom
feature/auto-sandbox-migration

Conversation

@crispinhu27

Copy link
Copy Markdown
Collaborator

Follow-up to #1258.

What changed

  • Detect whether the target image supports inline Dockerfiles and whether the Compose database still has a legacy sandbox asset table.
  • When migration 041 is needed, stop the platform, keep MySQL running, save a private database backup, and apply the existing SQL before starting the target image. A backup or SQL error leaves the platform stopped for retry.
  • Keep fresh installs on the catalog seed. Once the legacy tables are gone, later pulls skip 041 and its maintenance window.

Scope

  • Public adapter / deployment
  • Installer / CLI

Compatibility

Published Compose upgrades get the automatic migration when the target image carries the sandbox catalog label. Source builds and external MySQL retain the explicit migration procedure. Rollback to a release that uses the old sandbox format requires restoring the pre-migration backup.

Validation

  • Ran python3 -B scripts/test-install.py in Linux Docker. All 66 applicable tests passed, and the two Bash 3.2 compatibility cases passed separately on macOS.
  • Verified the Compose upgrade with disposable MySQL 8.4 databases and platform fixtures covering legacy images, old catalogs, fresh seeds, and repeated pulls.
  • Induced a SQL failure against MySQL 8.4, restored the saved backup into a disposable database, and verified that the upgrade recovered on retry.
  • Ran /bin/bash -n start.sh and git diff --check.

Safety

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

Robert0622
Robert0622 previously approved these changes Nov 24, 2026

@Robert0622 Robert0622 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.

构建成功。./start.sh pull 的升级链路按 docs/deployment.md 描述的走通:拉镜像 → 查 io.loopforge.sandbox-catalog label → 单独拉起 MySQL → 查旧表 → 停平台 → mysqldump 备份 → 执行 041 → 启动全部 → 补 schema extensions。备份失败、迁移失败、迁移后新平台起不来这三种情况,平台都保持停止,数据库要么没被改、要么已完整迁移,修好后重跑 ./start.sh pull 都能恢复。

实测

  • PR 自带的 scripts/test-install.py:TMPDIR=/build/tmp/p1304 python3 scripts/test-install.py -v → Ran 68 tests … OK (skipped=2)(跳过的是需要 bash 3.2 的用例)。默认临时目录在 /tmp,本机 /tmp 是 noexec,mock 的 docker 跑不起来会落到系统 docker,导致 43 个失败,属环境问题。新增用例覆盖:迁移先于启动、无 label 或无旧表时跳过、备份/迁移失败保持停止、pull 与 migrate-sandboxes 共用一把锁。
  • 真实旧库(MySQL 8.4.13,log_bin=ON):编译 v0.4.2 的 agentflow,在 lf_p1304_legacy 上导入 v0.4.2 seed,通过 v0.4.2 的 API 造了带 files 的 dockerfile、孤儿 dockerfile、带 PATH/pathEntries/installScript 的 environment、docker 沙箱和 local 沙箱;旧表查询返回 2;按要求参数 mysqldump 备份退出 0、文件 0600;执行 041 退出 0(0.17s),迁移后旧表为 0,local 变 native(变量合并为 FOO/JAVA_HOME,PATH 被删),孤儿 dockerfile 变独立沙箱,docker 沙箱带上 Dockerfile 和 files;再执行一次 041 结果不变(幂等);把备份恢复回新库,旧表与 local 驱动都在(可回滚)。
  • 用 root 建一个主动报错的 trigger 制造事务中途失败:ERROR 1644 … line 111,退出 1,整个事务回滚,旧表仍为 2;删掉 trigger 后重跑成功。
  • AGENTFLOW_SANDBOX_MIGRATION_TEST_DSN=… go test ./internal/repo -run TestMySQLSandboxMigrationAllocatesUnusedIDs 通过。
  • 用 fake docker shim 驱动真实的 bash start.sh pull(compose exec mysql sh -c 转成连真实 MySQL 8.4 执行),逐个场景核对:镜像在跑+旧库 → 顺序正确、退出 0,备份在 .local/loopforge-before-041.*/loopforge.sql(目录 700、文件 600);无 label / label 为空 → 跳过且不停平台;已迁移库重跑 → 跳过、不再备份;备份失败 → 平台停、不留半成品、退出 1;迁移失败 → 平台停、备份保留、退出 1,修好后重跑成功;迁移后新平台起不来 → 平台停、库已迁移,重跑时检测到无旧表直接启动;image inspect 失败 / mysql 起不来 / 旧表查询失败 → 退出且旧平台照常运行、库未改;并发执行 pull 与 migrate-sandboxes → Could not lock the installation。
  • 用真实 Compose 2.26 CLI(不需要 daemon)验证 compose config --format json platform 取到的镜像名(默认 …:0.4.2、@sha256 摘要、dev 的 loopforge-platform:local),以及新增的 up … --no-recreate --pull never mysql、up … --pull never 用 --dry-run 解析通过。
  • LABEL 加在 Dockerfile 最终阶段,release 用 build-push-action 构建最终阶段,会进发布镜像;.local 同时在 .gitignore 和 .dockerignore 里,备份不会进 git 或 dev 构建上下文。

不阻塞的小建议(P3)

  • start.sh:54 迁移失败的提示是 "retry with the platform stopped",而文档写的是重跑 ./start.sh pull;只跑 migrate-sandboxes 的话平台不会自动起来,建议提示语与文档统一。
  • start.sh:62、:68 里 image inspect 失败或旧表查询失败时 set -e 直接退出,用户只看到 docker/mysql 原始报错,建议补一条 ✗ 提示。
  • scripts/install.sh 这条升级路径不会执行 041:用 ./start.sh 重跑安装器升到一个内联沙箱镜像后,旧库的 docker 沙箱 dockerfile 为 null;之后再 pull 能补救(旧表仍在),文档也写明升级要用 pull,不算本 PR 的回归,但安装时检测到旧表可以给个提示。
  • 锁只覆盖 pull 与 migrate-sandboxes,迁移过程中执行 ./start.sh restart 会把已停止的旧平台重新拉起(很窄的竞态)。

未能测试:真实 Docker daemon 与 docker compose exec(本机没有 Docker 权限,用 shim 代替,shim 里执行 SQL/mysqldump 连的是真实 MySQL 8.4);真实 GHCR/NJU 镜像拉取只跑了 mock;macOS bash 3.2 的 set -e 行为;磁盘写满导致备份失败。

@Robert0622 Robert0622 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.

复测 cc65e84(Merge origin/main into feature/auto-sandbox-migration)。上一轮在 469fcdc 上已经通过,这次只有合并 main 这一个提交,没有发现新问题,通过。

合并冲突处理

  • 唯一冲突在 scripts/test-install.py 的 setUp 环境清理:保留了 #1295 新加的 AGENTFLOW_PROJECT_CREDENTIAL_KEY 清理,也保留了本 PR 的 MOCK_SANDBOX_* 等 4 个变量,没有残留冲突标记。
  • 对比合并前后本 PR 相对 main 的改动(git diff merge-base..469fcdc 与 git diff origin/main..cc65e84),增删的行完全一致,合并没有改变 PR 本身的内容。

实测

  • python3 scripts/test-install.py -v:本 PR 69 个用例全部通过(跳过 2 个),main 是 65 个全部通过。新增的 4 个用例也都通过,包括 test_start_pull_migrates_legacy_sandboxes_before_startup、test_start_pull_keeps_platform_stopped_on_backup_or_migration_failure、test_start_pull_and_manual_sandbox_migration_share_an_upgrade_lock。
  • 原生部署 head(Go 1.26.11,MySQL 8.4.7 全新库),健康检查 200。按 PR 重新构建 chatbot,487/487 通过;go vet ./... 通过。
  • ./internal/domain、./internal/gitwork、./internal/repo 全部通过。./internal/runtime、./internal/runner、./internal/engine 的失败用例和 main(177d881)完全一致,原因是本机缺 bwrap 和 Docker。
  • 本机不能运行 Docker,没法跑真实的 Compose ./start.sh pull 升级,这部分靠上面的安装脚本用例覆盖。

@crispinhu27
crispinhu27 merged commit b0406f9 into main Nov 24, 2026
17 checks passed
@crispinhu27
crispinhu27 deleted the feature/auto-sandbox-migration branch November 24, 2026 16:37
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