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 應用從穩定的 SDK 版本遷移至 `@next` 預覽版(SDK 1.0)。它提供了一套逐步工作流程,包括審查規則、審計程式碼、與使用者確認、升級套件及映像檔,以及最終驗證。遷移過程涉及多項重大變更,例如將 `sandbox.exec()` 從返回緩衝結果改為返回進程控制代碼,移除傳輸層和會話 API,並將終端機和直譯器 API 現代化。此技能主要適用於開發環境,並強烈警告不應在未經使用者同意的情況下強制進行生產環境遷移。它支援基於 Workers 和容器的部署,並要求 Worker 套件和容器映像檔必須使用相同的 `@next` 版本。對於新專案,官方建議直接使用 `sandbox-next` 技能,而此技能僅用於遷移既有程式碼庫。

相容平台~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 版本。
  • 生產環境切換使用立即容器部署。穩定版和 @next 的控制協定是互不相容的;逐步部署會產生一個有問題的混合視窗。正在進行的容器工作可能會停止。
  • 切換後,await sandbox.exec(...) 表示行程已啟動,而非命令已完成。
  • 參數列表保持原樣(沒有隱含的 shell)。Shell 語法需要明確的 shell 二進制檔案。
  • 行程控制代碼沒有標準輸入 → 用於互動式輸入的終端機。
  • 觀測 timeout / AbortSignal 僅取消等待,而非取消行程。
  • 不要為所有錯誤使用單一重試迴圈。
  • 不要發明 API(例如核心上的 gitCheckout、行程標準輸入、字串執行的完成輔助函式)。
  • 自行部署的橋接保持在穩定版(目前不屬於預覽版的一部分)。

替換對照表

穩定版@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("終端機已消失", { 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 將失效。詳細資訊:遷移指南 · 容器部署

驗證

  1. Lockfile 和 Dockerfile 使用相同的 @next 版本
  2. 根據 @next 進行型別檢查
  3. 測試 argv exec + output({ encoding: "utf8" })
  4. 如果使用,測試長時間運行的行程 / 終端機 / 直譯器
  5. 區分錯誤類型:不可用 / RPC 中斷 / 過時 / 本地等待
  6. Sandbox 環境中沒有即時的秘密
  7. 再次搜尋已移除的 API
  8. 生產環境使用了 --containers-rollout=immediate

然後,日常開發使用 sandbox-next。

紅旗——請停止並修正

  • 將 @next Worker 與穩定版映像檔混合使用(或反過來)
  • 對此切換使用逐步容器部署
  • 將 await exec 視為命令完成
  • 假設 cd / exports 會在多次 exec 呼叫之間保留
  • 為所有錯誤使用單一重試包裝器
  • 發明 gitCheckout、行程標準輸入或未記載的 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.

相關技能