Community编程与开发github.com

cloudflare/sandbox-migrate-to-next

将 Cloudflare Sandbox 应用从稳定的 @cloudflare/sandbox 迁移到 @cloudflare/sandbox@next(SDK 1.0 预览版)。对于已经在预览版上的应用,请使用 sandbox-next。

sandbox-migrate-to-next 是什么?

此技能指导开发者将 Cloudflare Sandbox 应用从当前稳定版(`@cloudflare/sandbox`)迁移到 SDK 1.0 预览版(`@cloudflare/sandbox@next`)。它提供了一套系统化的步骤,包括审查替换规则、审计代码库、与用户确认关键决策(如生产切换、自部署桥接、Python 镜像等),最后执行升级和验证。技能详细列出了从稳定版到 @next 的 API 替换映射表、升级后的代码示例(如 exec、终端、解释器、Git 调用)以及生产部署的严格规则(例如,Workers 包和容器镜像必须一致,生产切换必须使用 `--containers-rollout=immediate`)。它还强调了常见的陷阱和需要停止修复的“红旗”情况,确保迁移过程安全且准确。支持的平台包括 Cloudflare Workers 和 Containers。

兼容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/cloudflare/skills/tree/main/skills/sandbox-migrate-to-next

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

迁移稳定版 → Sandbox SDK 1.0 预览版(@next)

执行移植。按顺序执行以下步骤。详细内容参见文档——当某一步需要细节时,请获取链接页面。

人类指南:迁移 · 1.0 预览版

新项目应从 @next(sandbox-next)开始,而非此技能。日常稳定版工作 → sandbox-stable。如果需要进行已弃用 API 清理但不迁移到 @next,请首先参考 2026 弃用指南。

现有应用应在条件允许时迁移,以便在 1.0 成为稳定版本时做好准备。在用户同意之前,请勿强制进行生产环境切换。

优先使用已安装的 @next 类型和迁移文档,而非记忆。

工作流程

  1. 审查硬性规则和替换映射表
  2. 审计代码库;列出匹配项和目标形态
  3. 与用户确认(切换、桥接、Python 镜像、不明确的站点)
  4. 升级包、镜像和代码
  5. 验证

在需要用户决策的步骤后停止。

硬性规则

  • Worker 包和容器镜像必须为相同的 @next 版本线。
  • 生产切换使用立即容器 rollout。稳定版和 @next 的控制协议不兼容(双向);渐进式 rollout 会导致中断的混合窗口。正在进行的容器工作可能会停止。
  • 切换后,await sandbox.exec(...) 表示进程已启动,而非命令已完成。
  • Argv 按原样传递(无隐式 shell)。Shell 语法需要显式指定 shell 二进制文件。
  • 进程句柄没有 stdin → 用于交互式输入的终端。
  • 观察的 timeout / AbortSignal 仅取消等待,而非进程本身。
  • 没有一个适用于所有错误的单一重试循环。
  • 不要发明 API(例如,gitCheckout 在核心上、进程 stdin、字符串执行完成辅助工具)。
  • 自部署桥接保持在稳定版(尚未属于预览版的一部分)。

替换映射表

稳定版@next
SANDBOX_TRANSPORT / transport / setTransport移除——仅 RPC
await sandbox.exec("cmd") → 缓冲结果await sandbox.exec(argv) → 句柄,然后 output / 等待
execStream / startProcess相同句柄:logs、waitFor*、kill
默认/命名会话已移除——每个启动使用 cwd/env,或一个 shell 脚本
sandbox.terminal(request) / 会话终端createTerminal + terminal.connect(request)
xterm sessionIdterminalId
Sandbox 上的解释器方法withInterpreter → sandbox.interpreter.*
gitCheckout通过 exec 使用 argv git
字符串类型的 kill 信号仅数字类型
文件、挂载点、备份、端口、隧道、proxyToSandbox基本不变(在稳定版页面上忽略会话/传输相关部分)

详细内容:迁移 · 移植后,日常使用 → sandbox-next

审计

rg 'SANDBOX_TRANSPORT|transport:|setTransport|enableDefaultSession|createSession|getSession|deleteSession|execStream\(|startProcess\(|killProcess\(|sandbox\.terminal\(|sessionId|gitCheckout\(|SandboxTransport|ExecutionSession'

另外检查:字符串 exec(、cd 后跟后续 exec、在 Sandbox 上直接使用 createCodeContext / runCode。

确认(需要时询问)

  • 是否允许使用 --containers-rollout=immediate 进行生产切换(正在运行的进程/终端/流可能会停止)?
  • 是否使用了自部署桥接?保持其处于稳定版。
  • Python 解释器 → 是否使用 -python 镜像变体?
  • 是否存在映射表未覆盖的调用点?

升级

包和镜像

npm install @cloudflare/sandbox@next
FROM cloudflare/sandbox:next
# Python: cloudflare/sandbox:next-python

当不是使用浮动的 next 标签时,Worker 和镜像上的预发布标签必须相同。

按区域代码

应用映射表中的替换。对于每个区域,从文档中实现——而不是依据稳定版的习惯:

区域文档
命令 / 句柄 / 等待进程 · 进程 API
cwd / env / 机密环境 · 出站流量
移除会话迁移 · 生命周期
终端终端
解释器解释器
错误错误
跨请求的持久性任务进程执行 — 生命周期 / 持久性

命令(形态):

// 之前(稳定版)
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" });
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

终端(形态):

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 });

解释器(形态):

import { Sandbox as BaseSandbox } from "@cloudflare/sandbox";
import { withInterpreter } from "@cloudflare/sandbox/interpreter";

export class Sandbox extends BaseSandbox<Env> {
  interpreter = withInterpreter(this);
}

Git(形态):

const clone = await sandbox.exec(
  ["git", "clone", "--depth", "1", "--", repoUrl, "/workspace/repo"],
  { cwd: "/workspace" },
);
const result = await clone.output({ encoding: "utf8" });

完全删除传输设置。移除会话 API。使用独立的 sandbox ID 隔离用户。

部署切换

先进行暂存/分支部署。生产环境是一次匹配的 Worker + 镜像的部署:

npx wrangler deploy --containers-rollout=immediate

将 rollout_active_grace_period 保持默认值 0(如果已调高,则设置为 0)。切换后,部署前的进程/终端 ID 将失效。详细内容:迁移 · 容器 rollout

验证

  1. Lockfile 和 Dockerfile 使用相同的 @next 版本线
  2. 针对 @next 进行类型检查
  3. 冒烟测试 argv exec + output({ encoding: "utf8" })
  4. 冗长进程 / 终端 / 解释器(如果使用)进行冒烟测试
  5. 区分错误:不可用 / 中断的 RPC / 过期 / 本地等待
  6. Sandbox 环境中没有实时机密
  7. 再次 grep 移除的 API
  8. 生产环境使用了 --containers-rollout=immediate

然后,日常工作使用 sandbox-next。

红旗——停止并修复

  • 将 @next Worker 与稳定版镜像混合使用(或相反)
  • 为此切换进行渐进式容器 rollout
  • 将 await exec 视为命令完成
  • 假设 cd / 导出在多个 exec 调用之间持久存在
  • 为每个错误使用一个重试封装
  • 发明 gitCheckout、进程 stdin 或未文档化的 API
  • 部署后保留切换前的进程/终端 ID
  • 未获用户同意强制生产切换
  • 在 setEnvVars / 启动 env 中放入实时机密

Individual skills in this repo

This repo contains 15 individual skills — each has its own dedicated page.

cloudflare/agents-sdk

Build AI agents on Cloudflare Workers using the Agents SDK. Load when creating stateful agents, durable workflows, real-time WebSocket apps, scheduled tasks, MCP servers, chat applications, voice agents, or browser automation. Covers Agent class, state management, callable RPC, Workflows, durable execution, queues, retries, observability, and React hooks. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/building-ai-agent-on-cloudflare

Builds AI agents on Cloudflare using the Agents SDK with state management, real-time WebSockets, scheduled tasks, tool integration, and chat capabilities. Generates production-ready agent code deployed to Workers. Use when: user wants to "build an agent", "AI agent", "chat agent", "stateful agent", mentions "Agents SDK", needs "real-time AI", "WebSocket AI", or asks about agent "state management", "scheduled tasks", or "tool calling". Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/building-mcp-server-on-cloudflare

Builds remote MCP (Model Context Protocol) servers on Cloudflare Workers with tools, OAuth authentication, and production deployment. Generates server code, configures auth providers, and deploys to Workers. Use when: user wants to "build MCP server", "create MCP tools", "remote MCP", "deploy MCP", add "OAuth to MCP", or mentions Model Context Protocol on Cloudflare. Also triggers on "MCP authentication" or "MCP deployment". Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/cloudflare

Comprehensive Cloudflare platform skill covering Workers, Pages, storage (KV, D1, R2), AI (Workers AI, Vectorize, Agents SDK), feature flags (Flagship), networking (Tunnel, Spectrum), security (WAF, DDoS), and infrastructure-as-code (Terraform, Pulumi). Use for any Cloudflare development task. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/cloudflare-email-service

Send and receive transactional emails with Cloudflare Email Service (Email Sending + Email Routing). Use when building email sending (Workers binding or REST API), email routing, Agents SDK email handling, or integrating email into any app — Workers, Node.js, Python, Go, etc. Also use for email deliverability, SPF/DKIM/DMARC, wrangler email setup, MCP email tools, or when a coding agent needs to send emails. Even for simple requests like "add email to my Worker" — this skill has critical config details.

cloudflare/cloudflare-one

Design, configure, troubleshoot, or review Cloudflare One Zero Trust and SASE deployments. Use cloudflare-one-migrations for migration planning from other vendors.

cloudflare/durable-objects

Create and review Cloudflare Durable Objects. Use when building stateful coordination (chat rooms, multiplayer games, booking systems), implementing RPC methods, SQLite storage, alarms, WebSockets, or reviewing DO code for best practices. Covers Workers integration, wrangler config, and testing with Vitest. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/sandbox-next

Build or maintain Cloudflare Sandbox apps on @cloudflare/sandbox@next (SDK 1.0 preview). Use sandbox-migrate-to-next when porting a stable app.

cloudflare/sandbox-sdk

Build sandboxed applications for secure code execution. Load when building AI code execution, code interpreters, CI/CD systems, interactive dev environments, or executing untrusted code. Covers Sandbox SDK lifecycle, commands, files, code interpreter, and preview URLs. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/sandbox-stable

Build or maintain Cloudflare Sandbox apps on the stable @cloudflare/sandbox package. Use sandbox-next for preview apps and sandbox-migrate-to-next for stable-to-preview migrations.

cloudflare/skills

Build, migrate, and deploy Next.js apps on Cloudflare Workers with vinext. Use when starting a Next.js project on Cloudflare, moving an existing app to Workers, choosing between vinext and OpenNext, or setting up vinext for Workers. For setup, migration, or deployment, install vinext's upstream skills with `npx skills add cloudflare/vinext` if missing, then read and follow the applicable skill and docs.

cloudflare/turnstile-spin

Set up, repair, or migrate to Cloudflare Turnstile bot verification in an existing frontend and backend, including server-side Siteverify.

cloudflare/web-perf

Analyzes web performance using Chrome DevTools MCP. Measures Core Web Vitals (LCP, INP, CLS) and supplementary metrics (FCP, TBT, Speed Index), identifies render-blocking resources, network dependency chains, layout shifts, caching issues, and accessibility gaps. Use when asked to audit, profile, debug, or optimize page load performance, Lighthouse scores, or site speed. Biases towards retrieval from current documentation over pre-trained knowledge.

cloudflare/workers-best-practices

Reviews and authors Cloudflare Workers code against production best practices. Load when writing new Workers, reviewing Worker code, configuring wrangler.jsonc, or checking for common Workers anti-patterns (streaming, floating promises, global state, secrets, bindings, observability). Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/wrangler

Cloudflare Workers CLI for deploying, developing, and managing Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines, and Secrets Store. Load before running wrangler commands to ensure correct syntax and best practices. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

相关技能