跳转到内容
返回主页

生命周期、启停与重连

沙箱会经历创建、就绪、运行操作和清理阶段。所有运行时操作都应基于 create/connect 返回的 SDK 对象进行。

create -> starting -> running -> pause/connect or refresh -> delete
动作使用时机
create分配运行时;不指定模板时默认使用 base
waitReady下一步立即需要文件、命令或端口能力。
connect用已有 Sandbox ID 恢复运行时 URL/Token。
pause停止运行资源,但保留元数据供后续重连。
refresh为活跃任务延长 TTL。
setTimeout修改秒级生命周期超时。
delete永久清理一次性任务。

API 响应只暴露少量公开状态。创建和清理过程中的中间阶段会在返回给公开客户端前折叠。

State/status含义
starting运行时正在创建,或等待健康心跳。
running运行时 API 可用。
pausedRuntime 已停止,但持久元数据保留,可 connect/resume。
deleted沙箱已移除。
failed / error运行时未能变为可用,或生命周期操作失败。

沙箱创建、列表和详情响应可能包含 timelinediagnostic。面向用户的进度和排障文案应优先使用这些字段,而不是解析日志文本。

就绪判断以运行时健康心跳为准。当 API 返回 running,或 SDK 的 waitReady 调用返回后,即可认为运行时 API 可用。

创建、详情和列表响应使用同一组公开字段语义。memoryMB 等资源字段、startedAt 等生命周期时间,以及 endAt 等过期时间应直接展示 API 返回值,不应替换为其他更新时间。

参数单位说明
templateIDstring可选模板引用;不传或为空时使用 base
timeout沙箱生命周期 TTL。
waitReadyboolean等待运行时进入可用状态。
envVarsmap运行时环境变量覆盖。
metadatamap用户侧元数据,便于搜索和调试。
autoPauseboolean支持时可将超时动作映射为 pause 而不是 kill。
autoResumeboolean支持时允许 router 访问暂停沙箱后触发自动恢复。
networkobject公网预览与出网策略,包括 allowPublicTrafficallowInternetAccessallowOutdenyOut
volumeMountsarray可选请求级挂载,仅支持 camelCase。

不要在创建/生命周期请求中发送已移除的兼容字段,例如 timeoutMssecuremcp 或 snake_case volume_mounts。这些字段会被拒绝,而不是静默忽略。沙箱生命周期 timeout 是秒;运行时命令 timeoutMs 仍然支持,单位是毫秒。

const sandbox = await Sandbox.create("base", {
timeout: 1800,
waitReady: true,
envVars: { FOO: "bar" },
metadata: { purpose: "demo" },
});
const sandboxId = sandbox.sandboxId;
await sandbox.connect({ timeout: 1800 });
// 或在另一个进程中:
const reconnected = await Sandbox.connect(sandboxId, { timeout: 1800 });
await sandbox.setTimeout(3600);
await sandbox.refresh();
await sandbox.pause();
await sandbox.delete();
MethodPath说明
POST/sandboxes创建沙箱。
GET/sandboxes/:sandboxID查询详情和运行时连接数据。
POST/sandboxes/:sandboxID/connect重连并刷新运行时访问。
POST/sandboxes/:sandboxID/pause暂停运行时。
POST/sandboxes/:sandboxID/refreshes刷新 TTL。
POST/sandboxes/:sandboxID/timeout修改秒级超时。
DELETE/sandboxes/:sandboxID删除沙箱。
信号路由用途
沙箱详情GET /sandboxes/:sandboxID状态、运行时访问字段、生命周期时间戳、timelinediagnostic
沙箱日志GET /sandboxes/:sandboxID/logs启动输出、就绪失败、应用日志和镜像拉取问题。
沙箱指标GET /sandboxes/:sandboxID/metrics当前 CPU、内存、磁盘、网络和任务计数。
可观测性摘要GET /observability/summary用户/项目用量、配额检查、接口可用性和建议动作。