sandbox-migrate-to-next
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrate stable → Sandbox SDK 1.0 preview (@next
)
@next从稳定版迁移至Sandbox SDK 1.0预览版(@next
)
@nextPerform the port. Follow the steps in order. Depth lives in docs—fetch the linked page when a step needs detail.
Human guide: Migrate · 1.0 preview
New projects should start on (), not this skill. Day-to-day stable work → . Deprecated-API cleanup without moving to → 2026 deprecation guide first if needed.
@nextsandbox-nextsandbox-stable@nextExisting apps should migrate when you can, so you are ready when 1.0 becomes the stable release. Do not force production cutover without the user agreeing.
Prefer installed types and the migrate doc over memory.
@nextWorkflow
工作流程
- Review hard rules and the replacement map
- Audit the codebase; list hits and target shapes
- Clarify with the user (cutover, bridge, Python image, unclear sites)
- Upgrade package, image, and code
- Validate
Stop after any step that needs a user decision.
- 查阅硬性规则和替换映射表
- 审计代码库;列出需要修改的内容和目标形态
- 与用户确认(切换方案、桥接工具、Python镜像、不明确的站点)
- 升级包、镜像和代码
- 验证
若某步骤需要用户决策,请暂停操作等待确认。
Hard rules
硬性规则
- Worker package and container image must be the same line.
@next - Production cutover uses immediate container rollout. Stable and control protocols are incompatible both ways; gradual rollout leaves a broken mixed window. In-flight container work can stop.
@next - After cutover, means process started, not command finished.
await sandbox.exec(...) - Argv is as-is (no implicit shell). Shell syntax needs an explicit shell binary.
- Process handles have no stdin → terminals for interactive input.
- Observation /
timeoutcancel the wait only, not the process.AbortSignal - No single retry loop for every error.
- Do not invent APIs (on core, process stdin, string-exec completion helper).
gitCheckout - Self-deployed bridge stays on stable (not part of the preview line yet).
- Worker包和容器镜像必须使用同一版本线的。
@next - 生产环境切换需使用立即容器滚动更新。稳定版与的控制协议双向不兼容;逐步滚动更新会导致混合状态下的功能异常。正在运行的容器任务可能会中断。
@next - 切换后,表示进程已启动,而非命令已完成。
await sandbox.exec(...) - 命令参数(Argv)保持原样(无隐式shell)。若需使用shell语法,需显式指定shell二进制文件。
- 进程句柄无标准输入(stdin) → 需使用终端进行交互式输入。
- 观测的/
timeout仅取消等待操作,不会终止进程。AbortSignal - 不要为所有错误设置统一的重试循环。
- 不要自行发明API(如核心模块的、进程标准输入、字符串执行完成助手)。
gitCheckout - 自部署的桥接工具需保留在稳定版(暂未纳入预览版线)。
Replacement map
替换映射表
| Stable | |
|---|---|
| Remove — RPC only |
| |
| Same handle: |
| Default / named sessions | Gone — |
| |
xterm | |
Interpreter methods on | |
| argv |
| String kill signals | Numeric only |
Files, mounts, backups, ports, tunnels, | Mostly unchanged (ignore session/transport bits on stable pages) |
Depth: Migrate · after port, day-to-day →
sandbox-next| 稳定版 | |
|---|---|
| 移除——仅保留RPC |
| |
| 统一使用句柄: |
| 默认/命名会话 | 移除——每次启动时指定 |
| |
xterm | |
| |
| 通过 |
| 字符串类型的终止信号 | 仅支持数值类型 |
文件、挂载、备份、端口、隧道、 | 基本不变(忽略稳定版文档中的会话/传输相关内容) |
详细内容:迁移 · 迁移完成后,日常开发请使用****
sandbox-nextAudit
审计
sh
rg 'SANDBOX_TRANSPORT|transport:|setTransport|enableDefaultSession|createSession|getSession|deleteSession|execStream\(|startProcess\(|killProcess\(|sandbox\.terminal\(|sessionId|gitCheckout\(|SandboxTransport|ExecutionSession'Also: string , then a later , bare / on .
exec(cdexeccreateCodeContextrunCodeSandboxsh
rg 'SANDBOX_TRANSPORT|transport:|setTransport|enableDefaultSession|createSession|getSession|deleteSession|execStream\\(|startProcess\\(|killProcess\\(|sandbox\\.terminal\\(|sessionId|gitCheckout\\(|SandboxTransport|ExecutionSession'此外,还需检查:字符串形式的、先执行再执行后续、上直接调用 / 的情况。
exec(cdexecSandboxcreateCodeContextrunCodeClarify (ask when needed)
确认(必要时询问用户)
- OK to cut production with (live processes/terminals/streams may stop)?
--containers-rollout=immediate - Self-deployed bridge? Leave on stable.
- Python interpreter → image variant?
-python - Call sites not covered by the map?
- 是否同意使用切换至生产环境(正在运行的进程/终端/流可能会中断)?
--containers-rollout=immediate - 是否使用自部署的桥接工具?需保留在稳定版。
- Python解释器是否需要使用****镜像变体?
-python - 是否存在未被映射表覆盖的调用场景?
Upgrade
升级
Package and image
包与镜像
sh
npm install @cloudflare/sandbox@nextdockerfile
FROM cloudflare/sandbox:nextsh
npm install @cloudflare/sandbox@nextdockerfile
FROM cloudflare/sandbox:nextPython: cloudflare/sandbox:next-python
Python版本:cloudflare/sandbox:next-python
Same prerelease tag on Worker and image when not on floating `next`.
若未使用浮动版本`next`,Worker和镜像需使用相同的预发布标签。Code by area
分区域修改代码
Apply replacements from the map. For each area, implement from the doc—not from stable habits:
| Area | Doc |
|---|---|
| Commands / handles / waits | Processes · Processes API |
| Environment · Outbound traffic |
| Drop sessions | Migrate · Lifecycle |
| Terminals | Terminals |
| Interpreter | Interpreter |
| Errors | Errors |
| Durable job across requests | Process execution — lifetime / durability |
Commands (shape):
ts
// Before (stable)
const result = await sandbox.exec("npm test");
// After (@next)
const process = await sandbox.exec(["/bin/bash", "-lc", "npm test"]);
const result = await process.output({ encoding: "utf8" });ts
const server = await sandbox.exec(["/bin/bash", "-lc", "npm run dev"], {
cwd: "/workspace/app",
});
await server.waitForPort(3000, { timeout: 60_000 });
await server.kill(); // numeric; default 15Terminals (shape):
ts
const terminal = await sandbox.createTerminal({ command: ["bash"], cwd: "/workspace" });
const t = await sandbox.getTerminal(terminal.id);
if (!t) return new Response("terminal gone", { status: 410 });
return t.connect(request, { cursor, cols, rows });Interpreter (shape):
ts
import { Sandbox as BaseSandbox } from "@cloudflare/sandbox";
import { withInterpreter } from "@cloudflare/sandbox/interpreter";
export class Sandbox extends BaseSandbox<Env> {
interpreter = withInterpreter(this);
}Git (shape):
ts
const clone = await sandbox.exec(
["git", "clone", "--depth", "1", "--", repoUrl, "/workspace/repo"],
{ cwd: "/workspace" },
);
const result = await clone.output({ encoding: "utf8" });Delete transport settings entirely. Remove session APIs. Isolate users with separate sandbox IDs.
根据映射表进行替换。针对每个功能区域,请依据文档实现——不要沿用稳定版的使用习惯:
| 功能区域 | 文档链接 |
|---|---|
| 命令 / 句柄 / 等待 | 进程 · 进程API |
| 环境 · 出站流量 |
| 移除会话 | 迁移 · 生命周期 |
| 终端 | 终端 |
| 解释器 | 解释器 |
| 错误处理 | 错误 |
| 请求间的持久化任务 | 进程执行——生命周期/持久性 |
命令(代码形态):
ts
// 之前(稳定版)
const result = await sandbox.exec("npm test");
// 之后(@next版)
const process = await sandbox.exec(["/bin/bash", "-lc", "npm test"]);
const result = await process.output({ encoding: "utf8" });ts
const server = await sandbox.exec(["/bin/bash", "-lc", "npm run dev"], {
cwd: "/workspace/app",
});
await server.waitForPort(3000, { timeout: 60_000 });
await server.kill(); // 数值类型;默认值15终端(代码形态):
ts
const terminal = await sandbox.createTerminal({ command: ["bash"], cwd: "/workspace" });
const t = await sandbox.getTerminal(terminal.id);
if (!t) return new Response("terminal gone", { status: 410 });
return t.connect(request, { cursor, cols, rows });解释器(代码形态):
ts
import { Sandbox as BaseSandbox } from "@cloudflare/sandbox";
import { withInterpreter } from "@cloudflare/sandbox/interpreter";
export class Sandbox extends BaseSandbox<Env> {
interpreter = withInterpreter(this);
}Git(代码形态):
ts
const clone = await sandbox.exec(
["git", "clone", "--depth", "1", "--", repoUrl, "/workspace/repo"],
{ cwd: "/workspace" },
);
const result = await clone.output({ encoding: "utf8" });完全删除传输设置。移除会话API。使用独立的sandbox ID隔离用户。
Deploy cutover
部署切换
Staging/branch first. Production is one deploy of matching Worker + image:
sh
npx wrangler deploy --containers-rollout=immediateLeave at default (or set if raised). After cutover, pre-deploy process/terminal IDs are invalid. Details: Migrate · Container rollouts
rollout_active_grace_period00Validate
验证
- Lockfile + Dockerfile on the same line
@next - Typecheck against
@next - Smoke argv +
execoutput({ encoding: "utf8" }) - Smoke long process / terminal / interpreter if used
- Errors distinguished: unavailable / interrupted-RPC / stale / local wait
- No live secrets in sandbox env
- Grep again for removed APIs
- Production used
--containers-rollout=immediate
Then day-to-day work uses .
sandbox-next- 锁文件 + Dockerfile使用同一版本线的
@next - 基于进行类型检查
@next - 测试参数化+
execoutput({ encoding: "utf8" }) - 若使用了长时进程/终端/解释器,进行冒烟测试
- 区分不同错误类型:不可用/RPC中断/过期/本地等待
- sandbox环境中无明文密钥
- 再次检查是否存在已移除的API
- 生产环境部署时使用了
--containers-rollout=immediate
完成后,日常开发请使用****。
sandbox-nextRed flags — stop and fix
危险信号——立即停止并修复
- Mixing Worker with stable image (or reverse)
@next - Gradual container rollout for this cutover
- Treating as command completion
await exec - Assuming / exports persist across
cdcallsexec - One retry wrapper for every error
- Inventing , process stdin, or undocumented APIs
gitCheckout - Keeping pre-cutover process/terminal IDs after deploy
- Forcing production cutover without user agreement
- Putting live secrets in / launch
setEnvVarsenv
- 版Worker与稳定版镜像混用(反之亦然)
@next - 本次切换使用逐步容器滚动更新
- 将视为命令完成
await exec - 假设/ 环境变量在多次
cd调用间持久化exec - 为所有错误设置统一的重试包装器
- 自行实现、进程标准输入或未文档化的API
gitCheckout - 部署后仍使用切换前的进程/终端ID
- 未经用户同意强制切换至生产环境
- 将明文密钥放入/ 启动
setEnvVars中env
",