|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { SandboxManager, type SandboxRuntimeConfig } from "@anthropic-ai/sandbox-runtime"; |
| 3 | +import type { |
| 4 | + CreateSandboxBackendParams, |
| 5 | + SandboxBackendCommandParams, |
| 6 | + SandboxBackendCommandResult, |
| 7 | + SandboxBackendHandle, |
| 8 | + SandboxBackendManager, |
| 9 | +} from "openclaw/plugin-sdk/sandbox"; |
| 10 | +import { mapToSandboxRuntimeConfig, type EdgeClawSandboxPluginConfig } from "./config.js"; |
| 11 | + |
| 12 | +let initialized = false; |
| 13 | + |
| 14 | +async function ensureInitialized(config: SandboxRuntimeConfig): Promise<void> { |
| 15 | + if (initialized) { |
| 16 | + SandboxManager.updateConfig(config); |
| 17 | + return; |
| 18 | + } |
| 19 | + await SandboxManager.initialize(config); |
| 20 | + initialized = true; |
| 21 | +} |
| 22 | + |
| 23 | +function runWrappedCommand( |
| 24 | + wrappedCommand: string, |
| 25 | + params: { |
| 26 | + env: Record<string, string>; |
| 27 | + stdin?: Buffer | string; |
| 28 | + allowFailure?: boolean; |
| 29 | + signal?: AbortSignal; |
| 30 | + }, |
| 31 | +): Promise<SandboxBackendCommandResult> { |
| 32 | + return new Promise<SandboxBackendCommandResult>((resolve, reject) => { |
| 33 | + const child = spawn("/bin/sh", ["-c", wrappedCommand], { |
| 34 | + stdio: ["pipe", "pipe", "pipe"], |
| 35 | + env: { ...process.env, ...params.env }, |
| 36 | + signal: params.signal, |
| 37 | + }); |
| 38 | + |
| 39 | + const stdoutChunks: Buffer[] = []; |
| 40 | + const stderrChunks: Buffer[] = []; |
| 41 | + |
| 42 | + child.stdout.on("data", (chunk: Buffer) => stdoutChunks.push(chunk)); |
| 43 | + child.stderr.on("data", (chunk: Buffer) => stderrChunks.push(chunk)); |
| 44 | + child.on("error", reject); |
| 45 | + child.on("close", (code) => { |
| 46 | + const stdout = Buffer.concat(stdoutChunks); |
| 47 | + const stderr = Buffer.concat(stderrChunks); |
| 48 | + const exitCode = code ?? 0; |
| 49 | + if (exitCode !== 0 && !params.allowFailure) { |
| 50 | + reject( |
| 51 | + Object.assign( |
| 52 | + new Error(`Sandbox command failed (exit ${exitCode}): ${stderr.toString("utf8")}`), |
| 53 | + { code: exitCode, stdout, stderr }, |
| 54 | + ), |
| 55 | + ); |
| 56 | + return; |
| 57 | + } |
| 58 | + resolve({ stdout, stderr, code: exitCode }); |
| 59 | + }); |
| 60 | + |
| 61 | + if (params.stdin !== undefined) { |
| 62 | + child.stdin.end(params.stdin); |
| 63 | + } else { |
| 64 | + child.stdin.end(); |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +export function createBwrapSandboxBackendFactory( |
| 70 | + getPluginConfig: () => EdgeClawSandboxPluginConfig, |
| 71 | +) { |
| 72 | + const factory = async (params: CreateSandboxBackendParams): Promise<SandboxBackendHandle> => { |
| 73 | + const pluginConfig = getPluginConfig(); |
| 74 | + const runtimeConfig = mapToSandboxRuntimeConfig( |
| 75 | + pluginConfig, |
| 76 | + params.workspaceDir, |
| 77 | + params.agentWorkspaceDir, |
| 78 | + ); |
| 79 | + await ensureInitialized(runtimeConfig); |
| 80 | + |
| 81 | + return { |
| 82 | + id: "bwrap", |
| 83 | + runtimeId: `bwrap-${params.scopeKey}`, |
| 84 | + runtimeLabel: `OS Sandbox (${params.scopeKey})`, |
| 85 | + workdir: params.workspaceDir, |
| 86 | + env: params.cfg.docker.env, |
| 87 | + |
| 88 | + async buildExecSpec({ command, workdir, env, usePty }) { |
| 89 | + const wrapped = await SandboxManager.wrapWithSandbox(command); |
| 90 | + return { |
| 91 | + argv: ["/bin/sh", "-c", wrapped], |
| 92 | + env: { ...process.env, ...env }, |
| 93 | + stdinMode: usePty ? ("pipe-open" as const) : ("pipe-closed" as const), |
| 94 | + }; |
| 95 | + }, |
| 96 | + |
| 97 | + async runShellCommand(command: SandboxBackendCommandParams) { |
| 98 | + const wrapped = await SandboxManager.wrapWithSandbox(command.script); |
| 99 | + return runWrappedCommand(wrapped, { |
| 100 | + env: {}, |
| 101 | + stdin: command.stdin, |
| 102 | + allowFailure: command.allowFailure, |
| 103 | + signal: command.signal, |
| 104 | + }); |
| 105 | + }, |
| 106 | + }; |
| 107 | + }; |
| 108 | + |
| 109 | + return factory; |
| 110 | +} |
| 111 | + |
| 112 | +export const bwrapSandboxBackendManager: SandboxBackendManager = { |
| 113 | + async describeRuntime() { |
| 114 | + return { |
| 115 | + running: SandboxManager.isSupportedPlatform(), |
| 116 | + configLabelMatch: true, |
| 117 | + }; |
| 118 | + }, |
| 119 | + async removeRuntime() { |
| 120 | + // bwrap has no persistent runtime to remove |
| 121 | + }, |
| 122 | +}; |
| 123 | + |
| 124 | +/** |
| 125 | + * Reset internal initialization state. |
| 126 | + * Exposed for testing only. |
| 127 | + */ |
| 128 | +export function resetBwrapState(): void { |
| 129 | + initialized = false; |
| 130 | +} |
0 commit comments