Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(in-app-agent): send docker sandbox payloads on stdin (#16831)

Lysander Kiesel0a397ca744bd
diff --git a/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.test.ts b/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.test.ts
index b74157ff39..34805d9f11 100644
--- a/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.test.ts
+++ b/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.test.ts
@@ -1,4 +1,4 @@
-import { PassThrough } from "node:stream";
+import { Duplex, PassThrough } from "node:stream";
import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -65,24 +65,48 @@ class FakeContainer {
};
running = false;
+ lastExecCmd: string[] | undefined;
+ lastExecStdin: string | undefined;
constructor(
public readonly id: string,
private readonly name: string,
) {}
- async exec(options: { Cmd: string[] }) {
- const payload =
- options.Cmd[3] === undefined
- ? JSON.stringify({ status: "ok" })
- : JSON.stringify({ result: { command: "ok" } });
+ async exec(options: { Cmd: string[]; AttachStdin?: boolean }) {
+ this.lastExecCmd = options.Cmd;
+
+ if (!options.AttachStdin) {
+ return {
+ inspect: async () => ({ ExitCode: 0 }),
+ start: async () => {
+ const stream = new PassThrough();
+ queueMicrotask(() => {
+ stream.end(JSON.stringify({ status: "ok" }));
+ });
+ return stream;
+ },
+ };
+ }
return {
inspect: async () => ({ ExitCode: 0 }),
start: async () => {
- const stream = new PassThrough();
- queueMicrotask(() => {
- stream.end(payload);
+ const chunks: Buffer[] = [];
+ const stream = new Duplex({
+ write(chunk, _encoding, callback) {
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
+ callback();
+ },
+ final: (callback) => {
+ this.lastExecStdin = Buffer.concat(chunks).toString("utf8");
+ queueMicrotask(() => {
+ stream.push(JSON.stringify({ result: { command: "ok" } }));
+ stream.push(null);
+ });
+ callback();
+ },
+ read() {},
});
return stream;
},
@@ -188,4 +212,36 @@ describe("in-app agent docker sandbox provider", () => {
});
expect(fakeDockerState.createCount).toBe(2);
});
+
+ it("sends sandbox payloads on exec stdin instead of argv", async () => {
+ const { createDockerSandboxProvider } = await import("./docker");
+ const provider = await createDockerSandboxProvider({
+ image: "langfuse-in-app-agent-sandbox:latest",
+ });
+
+ const session = await provider.ensureSession({
+ conversationId: "conversation-1",
+ });
+
+ const largeToolResult = "x".repeat(200_000);
+ await session.sandbox.syncReadonlyFiles({
+ files: [
+ {
+ path: "tool_calls/listObservations.json",
+ content: largeToolResult,
+ },
+ ],
+ });
+
+ await expect(session.sandbox.bash({ command: "pwd" })).resolves.toEqual({
+ command: "ok",
+ });
+
+ const container = Array.from(fakeDockerState.containersById.values())[0];
+ expect(
+ container?.lastExecCmd?.some((part) => part.includes(largeToolResult)),
+ ).toBe(false);
+ expect(container?.lastExecStdin).toContain(largeToolResult);
+ expect(container?.lastExecStdin).toContain('"command":"pwd"');
+ });
});
diff --git a/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.ts b/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.ts
index 0d7b6c0232..c210960906 100644
--- a/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.ts
+++ b/worker/src/features/in-app-agent/runtime/sandbox/providers/docker.ts
@@ -327,6 +327,8 @@ async function callSandboxServer(
containerId: container.id,
payload: summarizePayload(payload),
});
+ // Docker exec argv is bounded by ARG_MAX. The sandbox POST body, including
+ // rebuilt tool_calls files, goes on stdin like the MicroVM HTTP body.
const result = await execJsonInContainer(
container,
[
@@ -334,7 +336,9 @@ async function callSandboxServer(
"-e",
`
(async () => {
- const payload = JSON.parse(process.argv[1]);
+ const chunks = [];
+ for await (const chunk of process.stdin) chunks.push(chunk);
+ const payload = JSON.parse(Buffer.concat(chunks).toString("utf8"));
const response = await fetch("http://127.0.0.1:${DOCKER_SANDBOX_SERVER_PORT}/sandbox", {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -351,12 +355,13 @@ async function callSandboxServer(
process.exit(1);
});
`,
- JSON.stringify(payload),
],
- undefined,
{
- operation: `sandbox:${String(payload.operation ?? "unknown")}`,
- containerId: container.id,
+ context: {
+ operation: `sandbox:${String(payload.operation ?? "unknown")}`,
+ containerId: container.id,
+ },
+ stdin: JSON.stringify(payload),
},
);
@@ -388,10 +393,13 @@ function getSession(
async function execJsonInContainer(
container: DockerContainer,
cmd: string[],
- timeoutMs?: number,
- context?: DockerExecContext,
+ options?: {
+ timeoutMs?: number;
+ context?: DockerExecContext;
+ stdin?: string;
+ },
) {
- const result = await execInContainer(container, cmd, timeoutMs, context);
+ const result = await execInContainer(container, cmd, options);
if (result.exitCode !== 0) {
throw new Error(
result.stderr || result.stdout || "Container command failed",
@@ -403,20 +411,28 @@ async function execJsonInContainer(
async function execInContainer(
container: DockerContainer,
cmd: string[],
- timeoutMs?: number,
- context?: DockerExecContext,
+ options?: {
+ timeoutMs?: number;
+ context?: DockerExecContext;
+ stdin?: string;
+ },
): Promise<DockerExecResult> {
+ const timeoutMs = options?.timeoutMs;
+ const context = options?.context;
+ const stdin = options?.stdin;
logger.debug("In-app agent docker sandbox exec create", {
containerId: container.id,
operation: context?.operation ?? "unknown",
attempt: context?.attempt,
timeoutMs: timeoutMs ?? null,
+ stdinBytes: stdin === undefined ? null : Buffer.byteLength(stdin, "utf8"),
commandPreview: summarizeCommand(cmd),
});
const exec = await container.exec({
Cmd: cmd,
AttachStdout: true,
AttachStderr: true,
+ ...(stdin !== undefined ? { AttachStdin: true } : {}),
WorkingDir: "/workspace",
});
logger.debug("In-app agent docker sandbox exec created", {
@@ -424,7 +440,10 @@ async function execInContainer(
operation: context?.operation ?? "unknown",
attempt: context?.attempt,
});
- const stream = await exec.start({ Tty: false });
+ const stream = await exec.start({
+ Tty: false,
+ ...(stdin !== undefined ? { hijack: true, stdin: true } : {}),
+ });
logger.debug("In-app agent docker sandbox exec stream started", {
containerId: container.id,
operation: context?.operation ?? "unknown",
@@ -464,6 +483,14 @@ async function execInContainer(
finalizeDemuxStreams();
});
+ if (
+ stdin !== undefined &&
+ "end" in stream &&
+ typeof stream.end === "function"
+ ) {
+ stream.end(stdin);
+ }
+
const timeoutId = timeoutMs
? setTimeout(
() =>