Dev #11

Merged
cpfarhood merged 86 commits from dev into local 2026-05-12 00:02:32 +00:00
Showing only changes of commit cb6af7c2cc - Show all commits
@@ -1,5 +1,11 @@
import path from "node:path"; import path from "node:path";
import { CommandExitError, Sandbox, SandboxNotFoundError, TimeoutError } from "e2b"; import { randomUUID } from "node:crypto";
import {
CommandExitError,
Sandbox,
SandboxNotFoundError,
TimeoutError,
} from "e2b";
import { definePlugin } from "@paperclipai/plugin-sdk"; import { definePlugin } from "@paperclipai/plugin-sdk";
import type { import type {
PluginEnvironmentAcquireLeaseParams, PluginEnvironmentAcquireLeaseParams,
@@ -345,79 +351,66 @@ const plugin = definePlugin({
const config = parseDriverConfig(params.config); const config = parseDriverConfig(params.config);
const sandbox = await connectSandbox(config, params.lease.providerLeaseId); const sandbox = await connectSandbox(config, params.lease.providerLeaseId);
const command = buildCommandLine(params.command, params.args); const baseCommand = buildCommandLine(params.command, params.args);
if (params.stdin == null) { const timeoutMs = params.timeoutMs ?? config.timeoutMs;
// For commands with stdin, stage the payload to a temp file inside the
// sandbox and shell-redirect it. Streaming stdin via `sendStdin` raced
// with fast-failing commands (the process exits before the RPC lands),
// and the previous code awaited a foreground `run` before sending stdin
// at all, so the data was never delivered. The staged-file approach
// keeps execution synchronous, avoids the race, and is unaffected by
// whether the command exits in microseconds or minutes.
let stagedStdinPath: string | null = null;
if (params.stdin != null) {
stagedStdinPath = `/tmp/paperclip-stdin-${randomUUID()}`;
try { try {
const result = await sandbox.commands.run(command, { await sandbox.files.write(stagedStdinPath, params.stdin);
cwd: params.cwd,
envs: params.env,
timeoutMs: params.timeoutMs ?? config.timeoutMs,
}) as Awaited<ReturnType<Sandbox["commands"]["run"]>> & {
exitCode: number;
stdout: string;
stderr: string;
};
return {
exitCode: result.exitCode,
timedOut: false,
stdout: result.stdout,
stderr: result.stderr,
};
} catch (error) { } catch (error) {
if (error instanceof CommandExitError) { // Best-effort cleanup in case the write partially succeeded; ignore
const commandError = error as CommandExitError; // remove failures so the original error is what propagates.
return { await sandbox.files.remove(stagedStdinPath).catch(() => undefined);
exitCode: commandError.exitCode,
timedOut: false,
stdout: commandError.stdout,
stderr: commandError.stderr,
};
}
if (error instanceof TimeoutError) {
return buildTimeoutExecuteResult(error);
}
throw error; throw error;
} }
} }
const started = await sandbox.commands.run(command, { const command = stagedStdinPath
stdin: true, ? `${baseCommand} < ${shellQuote(stagedStdinPath)}`
cwd: params.cwd, : baseCommand;
envs: params.env,
timeoutMs: params.timeoutMs ?? config.timeoutMs,
}) as Awaited<ReturnType<Sandbox["commands"]["run"]>> & {
pid: number;
exitCode: number;
stdout: string;
stderr: string;
};
try { try {
try { const result = await sandbox.commands.run(command, {
await sandbox.commands.sendStdin(started.pid, params.stdin); cwd: params.cwd,
} finally { envs: params.env,
await sandbox.commands.closeStdin(started.pid); timeoutMs,
} }) as Awaited<ReturnType<Sandbox["commands"]["run"]>> & {
exitCode: number;
stdout: string;
stderr: string;
};
return { return {
exitCode: started.exitCode, exitCode: result.exitCode,
timedOut: false, timedOut: false,
stdout: started.stdout, stdout: result.stdout,
stderr: started.stderr, stderr: result.stderr,
}; };
} catch (error) { } catch (error) {
if (error instanceof CommandExitError) { if (error instanceof CommandExitError) {
const commandError = error as CommandExitError;
return { return {
exitCode: commandError.exitCode, exitCode: error.exitCode,
timedOut: false, timedOut: false,
stdout: commandError.stdout, stdout: error.stdout,
stderr: commandError.stderr, stderr: error.stderr,
}; };
} }
if (error instanceof TimeoutError) { if (error instanceof TimeoutError) {
return buildTimeoutExecuteResult(error); return buildTimeoutExecuteResult(error);
} }
throw error; throw error;
} finally {
if (stagedStdinPath) {
await sandbox.files.remove(stagedStdinPath).catch(() => undefined);
}
} }
}, },
}); });