mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-28 04:41:52 +01:00
feat(sandbox): add a duplex transport for Daytona behind a default-off kill switch (#11750)
## Thinking Path > - Paperclip is an open source app that manages AI agents for work > - Paperclip runs agents in local and remote sandbox environments > - A sandbox needs a bounded channel for commands and asynchronous input > - Daytona needs a real pseudo-terminal transport for this channel > - The sandbox gateway also needs a mode that handles channel loss safely > - This pull request adds the Daytona transport and gateway mode behind a default-off kill switch > - The benefit is a tested foundation for later transport selection ## Linked Issues or Issue Description **Subsystem affected** Cross-cutting (multiple of the above): sandbox providers, plugin SDK, server settings, and shared types. **Problem or motivation** The merged sandbox protocol has no runtime transport for Daytona. The generated sandbox gateway also has no duplex mode. A later transport-selection change needs both parts and a safe per-run gate. **Proposed solution** Add a Daytona `duplexCommandStream` transport over a raw pseudo-terminal. Add a generated gateway mode named `duplex_v1`. Add the `enableSandboxDuplexBridge` setting with a default value of `false`. Keep transport selection disabled until a later pull request. **Alternatives considered** Keep the protocol unused until the transport-selection change. This would delay provider tests and leave the gateway path without direct coverage. **Roadmap alignment** This change supports the completed Roadmap item for cloud and sandbox agents. It extends the merged sandbox channel foundation in pull request #11738. **Additional context** The Daytona provider remains an untrusted boundary. Deployments must use least-privilege provider credentials and provider-side quota controls. Operators must name an owner for duplex telemetry retention before rollout. ## What Changed - Add the Daytona `duplexCommandStream` capability over a raw pseudo-terminal. - Add a launch wrapper that disables echo and newline translation for NDJSON frames. - Close channels on lease release, destroy, resume of a stopped worker, and worker shutdown. - Declare the capability in the Daytona manifest and set `PLUGIN_VERSION` to `0.1.5`. - Add the worker-to-host notification sink at `ctx.duplexChannel.data` and `ctx.duplexChannel.exit`. - Add the generated sandbox gateway mode `PAPERCLIP_API_BRIDGE_MODE=duplex_v1`. - Add channel-loss results of `409 outcome_indeterminate` and `503 bridge_unavailable`. - Add the per-run setting `enableSandboxDuplexBridge`, with a default value of `false`. - Add unit tests, generated-source codec tests, lifecycle tests, and a credential-gated live Daytona test. ## Verification - Daytona suite: 185 tests pass. - Adapter utilities: 754 tests pass and 4 tests skip. - Plugin SDK: 62 tests pass. - Shared package: 28 tests pass. - Server duplex tests pass. - Shared, plugin SDK, server, and Daytona TypeScript checks pass. - The live Daytona test passes 3 cases when `DAYTONA_API_KEY` is set. - The live Daytona test skips 3 cases without `DAYTONA_API_KEY`. - CI must run the full workspace typecheck, test, and build gates after PR creation. ## Risks - The Daytona control plane and pseudo-terminal remain untrusted boundaries. - The duplex gateway changes behavior only when the mode and per-run setting enable it. - A lost channel fails requests without replay, so callers must handle indeterminate outcomes. - The transport-selection change must require both `duplexCommandStream === true` and `enableSandboxDuplexBridge === true`. - The provider credential and quota limits need operator control before rollout. ## Model Used OpenAI Codex, GPT-5, tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes: #` / `Refs: #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
d5416fde9a
commit
faab2620ad
25 files changed
+2437
-133
No files matched your search
@@ -0,0 +1,194 @@
|
||||
// Live characterization of the Daytona duplex channel against the real provider.
|
||||
//
|
||||
// The test is credential-gated by design. It runs only when the run shell holds a
|
||||
// Daytona API key, and it skips cleanly when the key is absent. The key comes from
|
||||
// `DAYTONA_API_KEY`, which the plugin also reads as the credential fallback
|
||||
// (`plugin.ts`). A skip is acceptable: the merged live coverage re-runs at a later
|
||||
// phase and at rollout.
|
||||
//
|
||||
// What it proves on one live sandbox:
|
||||
// 1. The duplex channel and an ordinary command run at the same time.
|
||||
// 2. Host input sent after the child starts arrives, with no terminal echo and
|
||||
// no frame corruption.
|
||||
// 3. Channel teardown leaves no leaked provider session.
|
||||
//
|
||||
// The channel runs `cat` as the child. `cat` copies its input to its output, so a
|
||||
// host write returns one time as program output. The launch wrapper sets the
|
||||
// pseudo-terminal to raw mode with echo off, so the terminal adds no second copy
|
||||
// and no newline translation. A returned line that equals the sent line, one time,
|
||||
// proves both the delivery and the clean stream.
|
||||
//
|
||||
// The Daytona SDK is a runtime dependency of this live test only. The module loads
|
||||
// it with a dynamic import inside the gated block, so the file imports with no SDK
|
||||
// present and the other Daytona tests still run without it.
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import {
|
||||
openDaytonaDuplexChannelSession,
|
||||
type DaytonaPtyProcess,
|
||||
type DuplexChannelSession,
|
||||
} from "./duplex-command-stream.js";
|
||||
|
||||
const DAYTONA_API_KEY = process.env.DAYTONA_API_KEY?.trim() ?? "";
|
||||
const HAS_DAYTONA_CREDENTIAL = DAYTONA_API_KEY.length > 0;
|
||||
const describeLive = HAS_DAYTONA_CREDENTIAL ? describe : describe.skip;
|
||||
|
||||
if (!HAS_DAYTONA_CREDENTIAL) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
"Skipping the Daytona duplex channel live test: DAYTONA_API_KEY is not set in the run shell.",
|
||||
);
|
||||
}
|
||||
|
||||
// A live sandbox create and destroy needs a generous timeout. The provider round
|
||||
// trip dominates the wall-clock time.
|
||||
const LIVE_TIMEOUT_MS = 180_000;
|
||||
|
||||
// The minimal shape of the live Daytona sandbox that the test uses. The dynamic
|
||||
// import returns the real SDK types; this local shape keeps the file type-safe
|
||||
// without a static SDK import.
|
||||
interface LiveDaytonaSandbox {
|
||||
process: DaytonaPtyProcess & {
|
||||
executeCommand(command: string): Promise<{ exitCode?: number; result?: string }>;
|
||||
listSessions?: () => Promise<Array<{ sessionId?: string }>>;
|
||||
};
|
||||
delete(): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait until `predicate(text)` is true for the running output, or reject after
|
||||
* `timeoutMs`. The poll interval is short, so the wait ends soon after the output
|
||||
* arrives.
|
||||
*/
|
||||
async function waitFor(
|
||||
readOutput: () => string,
|
||||
predicate: (text: string) => boolean,
|
||||
timeoutMs: number,
|
||||
label: string,
|
||||
): Promise<void> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
for (;;) {
|
||||
if (predicate(readOutput())) return;
|
||||
if (Date.now() > deadline) {
|
||||
throw new Error(`Timed out waiting for ${label}. Output so far: ${JSON.stringify(readOutput())}`);
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
}
|
||||
|
||||
/** Open a `cat` duplex channel and collect its output into a growing string. */
|
||||
async function openCatChannel(
|
||||
sandbox: LiveDaytonaSandbox,
|
||||
): Promise<{ session: DuplexChannelSession; readOutput: () => string }> {
|
||||
let output = "";
|
||||
const session = await openDaytonaDuplexChannelSession(sandbox.process, "cat");
|
||||
session.onData((chunk) => {
|
||||
output += chunk;
|
||||
});
|
||||
return { session, readOutput: () => output };
|
||||
}
|
||||
|
||||
describeLive("Daytona duplex channel (live)", () => {
|
||||
let sandbox: LiveDaytonaSandbox | null = null;
|
||||
|
||||
beforeAll(async () => {
|
||||
const { Daytona } = await import("@daytonaio/sdk");
|
||||
const client = new Daytona({ apiKey: DAYTONA_API_KEY });
|
||||
sandbox = (await client.create()) as unknown as LiveDaytonaSandbox;
|
||||
}, LIVE_TIMEOUT_MS);
|
||||
|
||||
afterAll(async () => {
|
||||
await sandbox?.delete().catch(() => undefined);
|
||||
}, LIVE_TIMEOUT_MS);
|
||||
|
||||
it(
|
||||
"runs the duplex channel and an ordinary command on one live sandbox",
|
||||
async () => {
|
||||
const live = sandbox!;
|
||||
const { session, readOutput } = await openCatChannel(live);
|
||||
try {
|
||||
// Run an ordinary command while the channel child stays alive. Both use the
|
||||
// same live sandbox, so a success on both proves they coexist.
|
||||
const token = `ordinary-${randomUUID()}`;
|
||||
const ordinary = await live.process.executeCommand(`printf %s ${token}`);
|
||||
expect(ordinary.exitCode ?? 0).toBe(0);
|
||||
expect(ordinary.result ?? "").toContain(token);
|
||||
|
||||
// The channel child still answers. A write returns as program output.
|
||||
const ping = `chan-${randomUUID()}`;
|
||||
session.write(`${ping}\n`);
|
||||
await waitFor(readOutput, (text) => text.includes(ping), 30_000, "channel echo");
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
},
|
||||
LIVE_TIMEOUT_MS,
|
||||
);
|
||||
|
||||
it(
|
||||
"delivers host input sent after startup with no echo and no frame corruption",
|
||||
async () => {
|
||||
const live = sandbox!;
|
||||
const { session, readOutput } = await openCatChannel(live);
|
||||
try {
|
||||
// Let the launch wrapper run `stty raw -echo` before the host input. The
|
||||
// wrapper output echoes in cooked mode, so wait for the raw-mode switch
|
||||
// before the measured write.
|
||||
await new Promise((resolve) => setTimeout(resolve, 4_000));
|
||||
const baseline = readOutput().length;
|
||||
|
||||
const line = `PING-${randomUUID()}`;
|
||||
session.write(`${line}\n`);
|
||||
await waitFor(
|
||||
readOutput,
|
||||
(text) => text.slice(baseline).includes(line),
|
||||
30_000,
|
||||
"delayed host input",
|
||||
);
|
||||
|
||||
// Only `cat` writes the line, one time. Raw mode with echo off adds no
|
||||
// second copy, so the sent token appears exactly one time after the write.
|
||||
const afterWrite = readOutput().slice(baseline);
|
||||
const occurrences = afterWrite.split(line).length - 1;
|
||||
expect(occurrences).toBe(1);
|
||||
|
||||
// The stream carries the frame bytes with no newline translation. The
|
||||
// returned line keeps its single line-feed and gains no carriage return.
|
||||
expect(afterWrite).toContain(`${line}\n`);
|
||||
expect(afterWrite).not.toContain(`${line}\r`);
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
},
|
||||
LIVE_TIMEOUT_MS,
|
||||
);
|
||||
|
||||
it(
|
||||
"leaves no leaked provider session after channel close",
|
||||
async () => {
|
||||
const live = sandbox!;
|
||||
const listSessions = live.process.listSessions?.bind(live.process);
|
||||
const baseline = listSessions ? (await listSessions()).length : 0;
|
||||
|
||||
const { session } = await openCatChannel(live);
|
||||
await session.close();
|
||||
|
||||
if (listSessions) {
|
||||
// The channel uses a pseudo-terminal, not an ordinary session. So the
|
||||
// session list stays at the baseline. A leaked session would raise the
|
||||
// count.
|
||||
const after = (await listSessions()).length;
|
||||
expect(after).toBe(baseline);
|
||||
}
|
||||
|
||||
// The child exits after close, so a fresh ordinary command still succeeds on
|
||||
// the same sandbox. A leaked channel would block or corrupt the sandbox.
|
||||
const token = `after-close-${randomUUID()}`;
|
||||
const probe = await live.process.executeCommand(`printf %s ${token}`);
|
||||
expect(probe.exitCode ?? 0).toBe(0);
|
||||
expect(probe.result ?? "").toContain(token);
|
||||
},
|
||||
LIVE_TIMEOUT_MS,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,416 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildDuplexChannelLaunchWrapper,
|
||||
createDaytonaDuplexChannelSessionOpener,
|
||||
openDaytonaDuplexChannelSession,
|
||||
type DaytonaPtyCreateOptions,
|
||||
type DaytonaPtyHandle,
|
||||
type DaytonaPtyProcess,
|
||||
} from "./duplex-command-stream.js";
|
||||
|
||||
// The carriage return that submits the launch wrapper line to the terminal.
|
||||
const ENTER = "\r";
|
||||
|
||||
// The gateway command the tests run on the channel. The command is an argument
|
||||
// vector: element 0 is the program and the rest are its arguments. The real
|
||||
// command is the generated duplex gateway; the tests use a placeholder, because
|
||||
// the fake PTY runs no process.
|
||||
const GATEWAY = ["node", "/paperclip/gateway.mjs"];
|
||||
|
||||
// The gateway argument vector after the wrapper quotes each element. The wrapper
|
||||
// quotes every argument as a single-quoted shell word.
|
||||
const GATEWAY_QUOTED = "'node' '/paperclip/gateway.mjs'";
|
||||
|
||||
/**
|
||||
* A fake Daytona PTY handle. It records each input write, drives the output
|
||||
* stream on demand, and records the kill and the disconnect. The tests use it in
|
||||
* place of the real SDK `PtyHandle`, so the session runs with no sandbox. The fake
|
||||
* never echoes input back as output, so a test proves the channel adds no echo.
|
||||
*/
|
||||
function createFakePtyHandle(onData: (data: Uint8Array) => void | Promise<void>): DaytonaPtyHandle & {
|
||||
inputs: string[];
|
||||
emitText: (text: string) => void;
|
||||
emitBytes: (bytes: Uint8Array) => void;
|
||||
finish: (exitCode: number | undefined) => void;
|
||||
killed: number;
|
||||
disconnected: number;
|
||||
} {
|
||||
const encoder = new TextEncoder();
|
||||
const inputs: string[] = [];
|
||||
let resolveWait: ((value: { exitCode?: number; error?: string }) => void) | null = null;
|
||||
const waitPromise = new Promise<{ exitCode?: number; error?: string }>((resolve) => {
|
||||
resolveWait = resolve;
|
||||
});
|
||||
let killed = 0;
|
||||
let disconnected = 0;
|
||||
return {
|
||||
async waitForConnection(): Promise<void> {},
|
||||
async sendInput(data: string | Uint8Array): Promise<void> {
|
||||
inputs.push(typeof data === "string" ? data : new TextDecoder().decode(data));
|
||||
},
|
||||
wait(): Promise<{ exitCode?: number; error?: string }> {
|
||||
return waitPromise;
|
||||
},
|
||||
async kill(): Promise<void> {
|
||||
killed += 1;
|
||||
},
|
||||
async disconnect(): Promise<void> {
|
||||
disconnected += 1;
|
||||
},
|
||||
// Test control below. The session never reads these fields.
|
||||
emitText(text: string): void {
|
||||
onData(encoder.encode(text));
|
||||
},
|
||||
emitBytes(bytes: Uint8Array): void {
|
||||
onData(bytes);
|
||||
},
|
||||
finish(exitCode: number | undefined): void {
|
||||
resolveWait?.({ exitCode });
|
||||
},
|
||||
get inputs(): string[] {
|
||||
return inputs;
|
||||
},
|
||||
get killed(): number {
|
||||
return killed;
|
||||
},
|
||||
get disconnected(): number {
|
||||
return disconnected;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A fake Daytona process. It opens one fake PTY handle and records the create
|
||||
* options, so a test asserts the terminal size and the launch wrapper.
|
||||
*/
|
||||
function createFakeProcess(): DaytonaPtyProcess & {
|
||||
handle: ReturnType<typeof createFakePtyHandle> | null;
|
||||
createOptions: DaytonaPtyCreateOptions | null;
|
||||
} {
|
||||
const state: {
|
||||
handle: ReturnType<typeof createFakePtyHandle> | null;
|
||||
createOptions: DaytonaPtyCreateOptions | null;
|
||||
} = { handle: null, createOptions: null };
|
||||
return {
|
||||
get handle() {
|
||||
return state.handle;
|
||||
},
|
||||
get createOptions() {
|
||||
return state.createOptions;
|
||||
},
|
||||
async createPty(options: DaytonaPtyCreateOptions): Promise<DaytonaPtyHandle> {
|
||||
state.createOptions = options;
|
||||
const handle = createFakePtyHandle(options.onData);
|
||||
state.handle = handle;
|
||||
return handle;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Characterization: which Daytona primitive carries a clean framed stream.
|
||||
// ---------------------------------------------------------------------------
|
||||
// The Daytona SDK proves two primitives. The ordinary session primitive
|
||||
// dispatches a command and tails its output, split into stdout and stderr. It
|
||||
// exposes no call that writes bytes to a running command's stdin, so it cannot
|
||||
// carry host input on the same channel as the output. The PTY primitive exposes
|
||||
// `sendInput` for host input and one `onData` callback for the output, on one live
|
||||
// socket. Only the PTY carries a bidirectional stream on one connection, so the
|
||||
// duplex channel uses the PTY. These tests record that decision against the two
|
||||
// SDK surface shapes.
|
||||
|
||||
/** The subset of the Daytona session command surface, for the characterization. */
|
||||
interface DaytonaSessionCommandSurface {
|
||||
createSession(sessionId: string): Promise<void>;
|
||||
executeSessionCommand(sessionId: string, command: unknown): Promise<{ cmdId?: string }>;
|
||||
getSessionCommandLogs(
|
||||
sessionId: string,
|
||||
commandId: string,
|
||||
onStdout: (chunk: string) => void,
|
||||
onStderr: (chunk: string) => void,
|
||||
): Promise<void>;
|
||||
deleteSession(sessionId: string): Promise<void>;
|
||||
}
|
||||
|
||||
describe("duplex primitive characterization", () => {
|
||||
it("the PTY surface carries host input and process output on one connection", () => {
|
||||
const process = createFakeProcess();
|
||||
// The PTY surface exposes both directions: `sendInput` for host bytes and the
|
||||
// `onData` create option for process bytes. This is the bidirectional shape a
|
||||
// duplex channel needs.
|
||||
expect(typeof process.createPty).toBe("function");
|
||||
const handleKeys: Array<keyof DaytonaPtyHandle> = [
|
||||
"waitForConnection",
|
||||
"sendInput",
|
||||
"wait",
|
||||
"kill",
|
||||
"disconnect",
|
||||
];
|
||||
// `sendInput` is the host-to-process direction; the create option `onData` is
|
||||
// the process-to-host direction. Both exist on the PTY primitive.
|
||||
expect(handleKeys).toContain("sendInput");
|
||||
});
|
||||
|
||||
it("the session command surface exposes no write to a running command's stdin", () => {
|
||||
// The session surface dispatches a command and tails split stdout/stderr. No
|
||||
// member writes bytes to a running command's stdin, so it cannot carry the
|
||||
// host input on the output channel. The duplex channel therefore uses the PTY,
|
||||
// not a session.
|
||||
const sessionMembers: Array<keyof DaytonaSessionCommandSurface> = [
|
||||
"createSession",
|
||||
"executeSessionCommand",
|
||||
"getSessionCommandLogs",
|
||||
"deleteSession",
|
||||
];
|
||||
expect(sessionMembers).not.toContain("sendInput" as never);
|
||||
expect(sessionMembers).not.toContain("write" as never);
|
||||
// The log tail splits the output into two streams; a single framed duplex
|
||||
// stream must stay on one path, which the PTY `onData` provides.
|
||||
expect(sessionMembers).toContain("getSessionCommandLogs");
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Launch wrapper: raw mode, echo off, diagnostics to a file, direct exec.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("buildDuplexChannelLaunchWrapper", () => {
|
||||
it("sets raw mode with echo off, redirects diagnostics to a file, and execs the gateway", () => {
|
||||
const wrapper = buildDuplexChannelLaunchWrapper(GATEWAY, "/tmp/diag.log");
|
||||
|
||||
// Raw mode with echo off stops the terminal echoing host input as data and
|
||||
// stops newline translation, so the newline-delimited frames stay intact.
|
||||
expect(wrapper).toContain("stty raw -echo");
|
||||
// The diagnostics go to the file, never to the stdout frame stream. The
|
||||
// wrapper quotes the path as a single-quoted shell word.
|
||||
expect(wrapper).toContain("2>'/tmp/diag.log'");
|
||||
// The gateway starts with `exec`, so the PTY runs it directly and its exit
|
||||
// code becomes the PTY exit code.
|
||||
expect(wrapper).toContain(`exec ${GATEWAY_QUOTED}`);
|
||||
// The line ends with the Enter byte, so the shell runs it.
|
||||
expect(wrapper.endsWith(ENTER)).toBe(true);
|
||||
// Raw mode is set before the gateway starts.
|
||||
expect(wrapper.indexOf("stty raw -echo")).toBeLessThan(
|
||||
wrapper.indexOf(`exec ${GATEWAY_QUOTED}`),
|
||||
);
|
||||
});
|
||||
|
||||
it("quotes a diagnostics path that holds shell metacharacters, so it cannot inject a command", () => {
|
||||
const wrapper = buildDuplexChannelLaunchWrapper(GATEWAY, "/tmp/x; rm -rf ~");
|
||||
|
||||
// The path is one single-quoted word, so the shell reads the metacharacters as
|
||||
// literal text. The dangerous `rm` never becomes its own command.
|
||||
expect(wrapper).toContain("2>'/tmp/x; rm -rf ~'");
|
||||
expect(wrapper).not.toContain("2>/tmp/x; rm -rf ~");
|
||||
});
|
||||
|
||||
it("quotes a single quote in the diagnostics path", () => {
|
||||
const wrapper = buildDuplexChannelLaunchWrapper(GATEWAY, "/tmp/o'clock.log");
|
||||
|
||||
// A single quote closes the word, adds an escaped quote, and reopens the word.
|
||||
expect(wrapper).toContain(`2>'/tmp/o'"'"'clock.log'`);
|
||||
});
|
||||
|
||||
it("quotes each command argument that holds shell metacharacters", () => {
|
||||
const wrapper = buildDuplexChannelLaunchWrapper(
|
||||
["node", "/paperclip/gateway.mjs", "; rm -rf ~"],
|
||||
"/tmp/diag.log",
|
||||
);
|
||||
|
||||
// Each argument is one single-quoted word, so the metacharacters stay literal
|
||||
// text. The dangerous `rm` argument never becomes its own command.
|
||||
expect(wrapper).toContain(`exec 'node' '/paperclip/gateway.mjs' '; rm -rf ~'`);
|
||||
expect(wrapper).not.toContain("; rm -rf ~;");
|
||||
expect(wrapper).not.toContain("exec node /paperclip/gateway.mjs ; rm -rf ~");
|
||||
});
|
||||
|
||||
it("rejects an empty command argument vector", () => {
|
||||
// An empty vector has no program to exec, so the wrapper fails loudly.
|
||||
expect(() => buildDuplexChannelLaunchWrapper([], "/tmp/diag.log")).toThrow(
|
||||
/at least one argument/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("openDaytonaDuplexChannelSession", () => {
|
||||
it("starts the gateway through the raw-mode wrapper on a fixed-size terminal", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
|
||||
expect(process.createOptions?.cols).toBe(120);
|
||||
expect(process.createOptions?.rows).toBe(30);
|
||||
const launch = process.handle?.inputs[0] ?? "";
|
||||
expect(launch).toContain("stty raw -echo");
|
||||
expect(launch).toContain(`exec ${GATEWAY_QUOTED}`);
|
||||
expect(launch.endsWith(ENTER)).toBe(true);
|
||||
});
|
||||
|
||||
it("redirects diagnostics to the given file path", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
await openDaytonaDuplexChannelSession(process, GATEWAY, {
|
||||
diagnosticsPath: "/tmp/paperclip-duplex-fixed.log",
|
||||
});
|
||||
|
||||
expect(process.handle?.inputs[0]).toContain("2>'/tmp/paperclip-duplex-fixed.log'");
|
||||
});
|
||||
|
||||
it("defaults the diagnostics path under /tmp when the caller gives none", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
|
||||
expect(process.handle?.inputs[0]).toMatch(/2>'\/tmp\/paperclip-duplex-[0-9a-f-]+\.log'/);
|
||||
});
|
||||
|
||||
it("delivers a host write to the process and does not echo it back as data", async () => {
|
||||
const process = createFakeProcess();
|
||||
const received: string[] = [];
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
session.onData((chunk) => received.push(chunk));
|
||||
|
||||
// The launch wrapper is the first input; the host frame write is the second.
|
||||
session.write('{"version":1,"type":"heartbeat"}\n');
|
||||
expect(process.handle?.inputs[1]).toBe('{"version":1,"type":"heartbeat"}\n');
|
||||
// The fake terminal echoes nothing, so the write produces no data callback.
|
||||
// A real terminal in raw mode with echo off behaves the same way.
|
||||
expect(received).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps the data order and the frame newlines intact", async () => {
|
||||
const process = createFakeProcess();
|
||||
const received: string[] = [];
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
session.onData((chunk) => received.push(chunk));
|
||||
|
||||
process.handle?.emitText('{"version":1,"type":"ready","address":"127.0.0.1:8080"}\n');
|
||||
process.handle?.emitText('{"version":1,"type":"heartbeat"}\n');
|
||||
|
||||
// The order is preserved and no newline is translated (no stray "\r").
|
||||
expect(received).toEqual([
|
||||
'{"version":1,"type":"ready","address":"127.0.0.1:8080"}\n',
|
||||
'{"version":1,"type":"heartbeat"}\n',
|
||||
]);
|
||||
expect(received.join("")).not.toContain("\r");
|
||||
});
|
||||
|
||||
it("reassembles a frame split across two output chunks", async () => {
|
||||
const process = createFakeProcess();
|
||||
const received: string[] = [];
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
session.onData((chunk) => received.push(chunk));
|
||||
|
||||
const frame = '{"version":1,"type":"response","id":"r1","status":200,"headers":{},"body":"","outcome":"completed"}\n';
|
||||
process.handle?.emitText(frame.slice(0, 20));
|
||||
process.handle?.emitText(frame.slice(20));
|
||||
|
||||
expect(received.join("")).toBe(frame);
|
||||
});
|
||||
|
||||
it("carries a large frame without loss", async () => {
|
||||
const process = createFakeProcess();
|
||||
const received: string[] = [];
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
session.onData((chunk) => received.push(chunk));
|
||||
|
||||
const body = "x".repeat(500_000);
|
||||
const frame = `{"version":1,"type":"response","id":"big","status":200,"headers":{},"body":"${body}","outcome":"completed"}\n`;
|
||||
process.handle?.emitText(frame);
|
||||
|
||||
expect(received.join("")).toBe(frame);
|
||||
expect(received.join("").length).toBe(frame.length);
|
||||
});
|
||||
|
||||
it("keeps a multibyte character whole across two output chunks", async () => {
|
||||
const process = createFakeProcess();
|
||||
const received: string[] = [];
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
session.onData((chunk) => received.push(chunk));
|
||||
|
||||
// The euro sign is three UTF-8 bytes. Split it across two chunks, so the
|
||||
// stream decoder must join the bytes.
|
||||
const euro = new TextEncoder().encode("€");
|
||||
process.handle?.emitBytes(euro.subarray(0, 2));
|
||||
process.handle?.emitBytes(euro.subarray(2));
|
||||
|
||||
expect(received.join("")).toBe("€");
|
||||
});
|
||||
|
||||
it("buffers early output until the listener registers", async () => {
|
||||
const process = createFakeProcess();
|
||||
const received: string[] = [];
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
// Output arrives before the transport registers the listener.
|
||||
process.handle?.emitText('{"version":1,');
|
||||
process.handle?.emitText('"type":"heartbeat"}\n');
|
||||
expect(received).toEqual([]);
|
||||
|
||||
session.onData((chunk) => received.push(chunk));
|
||||
// The session flushes the buffered output in order on registration.
|
||||
expect(received).toEqual(['{"version":1,"type":"heartbeat"}\n']);
|
||||
});
|
||||
|
||||
it("resolves wait with the gateway exit code", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
process.handle?.finish(3);
|
||||
|
||||
await expect(session.wait()).resolves.toEqual({ exitCode: 3 });
|
||||
});
|
||||
|
||||
it("maps an absent exit code to null", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
process.handle?.finish(undefined);
|
||||
|
||||
await expect(session.wait()).resolves.toEqual({ exitCode: null });
|
||||
});
|
||||
|
||||
it("kills the child on stop and releases the socket on close", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
const session = await openDaytonaDuplexChannelSession(process, GATEWAY);
|
||||
session.kill();
|
||||
expect(process.handle?.killed).toBe(1);
|
||||
|
||||
await session.close();
|
||||
// Close stops the child and releases the socket, so a closed channel holds no
|
||||
// live terminal.
|
||||
expect(process.handle?.killed).toBeGreaterThanOrEqual(1);
|
||||
expect(process.handle?.disconnected).toBe(1);
|
||||
});
|
||||
|
||||
it("passes the working directory to the pseudo-terminal", async () => {
|
||||
const process = createFakeProcess();
|
||||
|
||||
await openDaytonaDuplexChannelSession(process, GATEWAY, { cwd: "/paperclip-workspace" });
|
||||
|
||||
expect(process.createOptions?.cwd).toBe("/paperclip-workspace");
|
||||
});
|
||||
});
|
||||
|
||||
describe("createDaytonaDuplexChannelSessionOpener", () => {
|
||||
it("opens a session for the command on each call", async () => {
|
||||
const process = createFakeProcess();
|
||||
const opener = createDaytonaDuplexChannelSessionOpener(process, { cwd: "/paperclip-workspace" });
|
||||
|
||||
const session = await opener(GATEWAY);
|
||||
|
||||
expect(process.createOptions?.cwd).toBe("/paperclip-workspace");
|
||||
expect(process.handle?.inputs[0]).toContain(`exec ${GATEWAY_QUOTED}`);
|
||||
expect(typeof session.onData).toBe("function");
|
||||
expect(typeof session.write).toBe("function");
|
||||
expect(typeof session.wait).toBe("function");
|
||||
expect(typeof session.kill).toBe("function");
|
||||
expect(typeof session.close).toBe("function");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,229 @@
|
||||
// The Daytona duplex command stream for the sandbox callback bridge. The bridge
|
||||
// needs one live bidirectional byte stream: the host writes request frames to the
|
||||
// process, and the process writes response frames back, on one connection while
|
||||
// the command runs. This module binds a Daytona pseudo-terminal (PTY) to a small
|
||||
// session that the duplex transport consumes, so the gateway runs on a real
|
||||
// terminal, streams its output, and receives host input.
|
||||
//
|
||||
// Primitive choice (the first acceptance criterion of the phase): the Daytona SDK
|
||||
// proves two primitives. The ordinary session primitive (`createSession` plus
|
||||
// `executeSessionCommand` plus `getSessionCommandLogs`) dispatches a command and
|
||||
// tails its output, split into separate stdout and stderr streams. It exposes no
|
||||
// call that writes bytes to a running command's stdin. The PTY primitive
|
||||
// (`createPty`) exposes `sendInput` for host-to-process bytes and one `onData`
|
||||
// callback for process-to-host bytes, on one live socket. Only the PTY carries a
|
||||
// bidirectional stream on one connection, so this module uses the PTY. The
|
||||
// characterization test in `duplex-command-stream.test.ts` records the evidence.
|
||||
//
|
||||
// Clean stream: a PTY echoes its input and translates newlines by default
|
||||
// (`ICRNL` on input, `ONLCR` on output). The frames are newline-delimited JSON, so
|
||||
// echo and newline translation corrupt the stream. The launch wrapper runs
|
||||
// `stty raw -echo` before it starts the gateway, so the terminal becomes an 8-bit
|
||||
// transparent path with no echo and no newline translation. The wrapper also
|
||||
// redirects the gateway diagnostics to a file, so a diagnostic line never reaches
|
||||
// the stdout frame stream. The wrapper starts the gateway with `exec`, so the PTY
|
||||
// runs the gateway directly and the gateway exit code becomes the PTY exit code.
|
||||
//
|
||||
// Dependency boundary: this provider plugin ships standalone (the workspace
|
||||
// excludes `packages/plugins/sandbox-providers/**`). So the module imports no
|
||||
// workspace package. It reuses the narrow Daytona PTY surface that
|
||||
// `setup-token-pty.ts` declares (`DaytonaPtyProcess`, `DaytonaPtyHandle`,
|
||||
// `DaytonaPtyCreateOptions`), so a caller passes `sandbox.process` with no
|
||||
// adapter. The narrow surface keeps the module unit-testable with a fake PTY.
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import type {
|
||||
DaytonaPtyCreateOptions,
|
||||
DaytonaPtyHandle,
|
||||
DaytonaPtyProcess,
|
||||
} from "./setup-token-pty.js";
|
||||
|
||||
export type {
|
||||
DaytonaPtyCreateOptions,
|
||||
DaytonaPtyHandle,
|
||||
DaytonaPtyProcess,
|
||||
} from "./setup-token-pty.js";
|
||||
|
||||
/**
|
||||
* A live duplex channel session for one command. The session allocates a real
|
||||
* pseudo-terminal in raw mode, streams the raw output, accepts host input, and
|
||||
* stops the child. The shape matches the worker duplex channel session, so the
|
||||
* worker forwards the data and the exit with no adapter.
|
||||
*/
|
||||
export interface DuplexChannelSession {
|
||||
/** Registers the one data listener. The session streams each raw chunk in order. */
|
||||
onData(listener: (chunk: string) => void): void;
|
||||
/** Writes raw input bytes to the pseudo-terminal. */
|
||||
write(data: string): void;
|
||||
/** Resolves with the child exit code when the command ends. */
|
||||
wait(): Promise<{ exitCode: number | null }>;
|
||||
/** Stops the child process. Safe to call more than one time. */
|
||||
kill(): void;
|
||||
/** Releases the session resources. Safe to call more than one time. */
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a {@link DuplexChannelSession} for `command`. The transport calls it one
|
||||
* time. `command` is an argument vector: element 0 is the program and the rest
|
||||
* are its arguments. The session quotes each element, so a shell metacharacter in
|
||||
* an element cannot inject a shell command.
|
||||
*/
|
||||
export type DuplexChannelSessionOpener = (
|
||||
command: readonly string[],
|
||||
) => Promise<DuplexChannelSession>;
|
||||
|
||||
/** The options for the Daytona duplex channel session. */
|
||||
export interface DaytonaDuplexChannelOptions {
|
||||
/** The working directory for the duplex channel PTY. Defaults to the sandbox default. */
|
||||
cwd?: string;
|
||||
/**
|
||||
* The absolute sandbox path for the gateway diagnostics. The wrapper redirects
|
||||
* the terminal's stderr to this path, so a diagnostic line never reaches the
|
||||
* stdout frame stream. Defaults to a per-channel path under `/tmp`.
|
||||
*/
|
||||
diagnosticsPath?: string;
|
||||
}
|
||||
|
||||
// The terminal size for the duplex channel PTY. The channel carries bytes, not a
|
||||
// visible UI, so a fixed standard size is enough. A fixed size keeps the launch
|
||||
// deterministic.
|
||||
const DUPLEX_CHANNEL_PTY_COLS = 120;
|
||||
const DUPLEX_CHANNEL_PTY_ROWS = 30;
|
||||
|
||||
/**
|
||||
* The input terminator that submits the launch wrapper line. The terminal reads
|
||||
* the carriage return as the Enter key, so the shell runs the wrapper line.
|
||||
*/
|
||||
const PTY_COMMAND_TERMINATOR = "\r";
|
||||
|
||||
/**
|
||||
* Quotes one value for a POSIX shell. The result is one single-quoted word, so
|
||||
* the shell reads it as literal text and never expands or splits it. The function
|
||||
* closes the quote, adds an escaped single quote, and reopens the quote for each
|
||||
* single quote in the value.
|
||||
*/
|
||||
function shellQuote(value: string): string {
|
||||
return `'${value.replace(/'/g, `'"'"'`)}'`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the launch wrapper input line for the duplex channel. `command` is an
|
||||
* argument vector: element 0 is the program and the rest are its arguments.
|
||||
*
|
||||
* The wrapper does three steps in order:
|
||||
* 1. `exec 2>'<diagnosticsPath>'` redirects the shell's stderr to the file. The
|
||||
* later `exec` inherits it, so the gateway diagnostics land in the file and
|
||||
* never on the stdout frame stream.
|
||||
* 2. `stty raw -echo` sets the terminal to raw mode with echo off, so the
|
||||
* terminal neither echoes host input as data nor translates newlines.
|
||||
* 3. `exec '<program>' '<arg>'...` replaces the shell with the gateway, so the
|
||||
* PTY runs the gateway directly and the gateway exit code becomes the PTY
|
||||
* exit code.
|
||||
*
|
||||
* The wrapper quotes the diagnostics path and every command argument as a
|
||||
* single-quoted shell word. So a shell metacharacter in a path or an argument
|
||||
* stays literal text and cannot inject a shell command.
|
||||
*/
|
||||
export function buildDuplexChannelLaunchWrapper(
|
||||
command: readonly string[],
|
||||
diagnosticsPath: string,
|
||||
): string {
|
||||
if (command.length === 0) {
|
||||
throw new Error("The duplex channel launch command needs at least one argument.");
|
||||
}
|
||||
const quotedCommand = command.map(shellQuote).join(" ");
|
||||
return (
|
||||
`exec 2>${shellQuote(diagnosticsPath)}; stty raw -echo; ` +
|
||||
`exec ${quotedCommand}${PTY_COMMAND_TERMINATOR}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a Daytona duplex channel PTY session for `command` and returns it as a
|
||||
* {@link DuplexChannelSession}. The session allocates a real pseudo-terminal in
|
||||
* raw mode, streams the raw output, accepts host input, and stops the child.
|
||||
*
|
||||
* The function decodes the terminal bytes as a UTF-8 stream, so a multibyte
|
||||
* character that splits across two output chunks stays whole. It buffers the
|
||||
* output until the transport registers the listener, so no early chunk is lost.
|
||||
*/
|
||||
export async function openDaytonaDuplexChannelSession(
|
||||
process: DaytonaPtyProcess,
|
||||
command: readonly string[],
|
||||
options?: DaytonaDuplexChannelOptions,
|
||||
): Promise<DuplexChannelSession> {
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let listener: ((chunk: string) => void) | null = null;
|
||||
let buffered = "";
|
||||
|
||||
const diagnosticsPath =
|
||||
options?.diagnosticsPath ?? `/tmp/paperclip-duplex-${randomUUID()}.log`;
|
||||
|
||||
const handle = await process.createPty({
|
||||
id: `paperclip-duplex-${randomUUID()}`,
|
||||
...(options?.cwd ? { cwd: options.cwd } : {}),
|
||||
cols: DUPLEX_CHANNEL_PTY_COLS,
|
||||
rows: DUPLEX_CHANNEL_PTY_ROWS,
|
||||
onData: (data: Uint8Array): void => {
|
||||
// Decode the terminal bytes as a stream, so a split multibyte character
|
||||
// stays whole across two chunks. Forward the raw text; the frame codec owns
|
||||
// the newline-delimited JSON parsing.
|
||||
const text = decoder.decode(data, { stream: true });
|
||||
if (text.length === 0) return;
|
||||
if (listener) listener(text);
|
||||
else buffered += text;
|
||||
},
|
||||
});
|
||||
|
||||
await handle.waitForConnection();
|
||||
// Start the gateway through the raw-mode wrapper. Raw mode with echo off stops
|
||||
// the terminal from echoing host input back as data and from translating the
|
||||
// frame newlines. The diagnostics redirect keeps the stdout frame stream clean.
|
||||
await handle.sendInput(buildDuplexChannelLaunchWrapper(command, diagnosticsPath));
|
||||
|
||||
return {
|
||||
onData(next: (chunk: string) => void): void {
|
||||
listener = next;
|
||||
if (buffered.length > 0) {
|
||||
const pending = buffered;
|
||||
buffered = "";
|
||||
next(pending);
|
||||
}
|
||||
},
|
||||
write(data: string): void {
|
||||
// Fire the input write. A write error must not throw into the transport, so
|
||||
// the transport's stream stays the single result path.
|
||||
void handle.sendInput(data).catch(() => undefined);
|
||||
},
|
||||
async wait(): Promise<{ exitCode: number | null }> {
|
||||
const result = await handle.wait();
|
||||
return { exitCode: typeof result.exitCode === "number" ? result.exitCode : null };
|
||||
},
|
||||
kill(): void {
|
||||
void handle.kill().catch(() => undefined);
|
||||
},
|
||||
async close(): Promise<void> {
|
||||
// Stop the child and release the socket. `kill` ends the gateway and `wait`
|
||||
// resolves with the exit; `disconnect` releases the PTY socket. Both are
|
||||
// safe to call more than one time, so close stays idempotent.
|
||||
await handle.kill().catch(() => undefined);
|
||||
await handle.disconnect().catch(() => undefined);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link DuplexChannelSessionOpener} bound to a Daytona `process`. Pass
|
||||
* `sandbox.process` from the Daytona SDK. The opener runs the gateway command on a
|
||||
* raw pseudo-terminal, streams the output, accepts host input, and stops the child
|
||||
* for a terminal state.
|
||||
*/
|
||||
export function createDaytonaDuplexChannelSessionOpener(
|
||||
process: DaytonaPtyProcess,
|
||||
options?: DaytonaDuplexChannelOptions,
|
||||
): DuplexChannelSessionOpener {
|
||||
return (command: readonly string[]) =>
|
||||
openDaytonaDuplexChannelSession(process, command, options);
|
||||
}
|
||||
@@ -10,7 +10,8 @@ const PLUGIN_ID = "paperclip.daytona-sandbox-provider";
|
||||
// 0.1.3 renamed the login transport flag from `supportsSetupTokenLogin` to the
|
||||
// neutral `supportsLoginPty`.
|
||||
// 0.1.4 adds the `concurrentSyncOperations` sandbox capability to the driver.
|
||||
const PLUGIN_VERSION = "0.1.4";
|
||||
// 0.1.5 adds the `duplexCommandStream` sandbox capability to the driver.
|
||||
const PLUGIN_VERSION = "0.1.5";
|
||||
|
||||
const manifest: PaperclipPluginManifestV1 = {
|
||||
id: PLUGIN_ID,
|
||||
@@ -44,9 +45,15 @@ const manifest: PaperclipPluginManifestV1 = {
|
||||
// waits for all active calls. Declare the opt-in capability so the host may
|
||||
// schedule sync operations concurrently. The host resolves it `true` only
|
||||
// when the worker also verifies both sync verbs.
|
||||
//
|
||||
// Daytona carries the sandbox callback bridge on one live duplex channel
|
||||
// over a raw pseudo-terminal. Declare the opt-in capability so the host may
|
||||
// select the duplex transport. The host resolves it `true` only when the
|
||||
// worker also verifies the `duplexChannelOpen` handler.
|
||||
sandboxCapabilities: {
|
||||
incrementalSessionOutput: true,
|
||||
concurrentSyncOperations: true,
|
||||
duplexCommandStream: true,
|
||||
},
|
||||
supportsInteractiveSetup: true,
|
||||
interactiveSetupConnectionTypes: ["ssh"],
|
||||
|
||||
@@ -148,6 +148,139 @@ describe("Daytona sandbox provider plugin", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("declares the duplex-command-stream capability and the four channel handlers", () => {
|
||||
// Daytona carries the callback bridge on one duplex channel, so it declares
|
||||
// the opt-in capability. The host resolves it `true` only when the worker also
|
||||
// verifies the `duplexChannelOpen` handler, which the four handlers provide.
|
||||
expect(manifest.environmentDrivers?.[0]?.sandboxCapabilities).toMatchObject({
|
||||
duplexCommandStream: true,
|
||||
});
|
||||
expect(plugin.definition.onDuplexChannelOpen).toBeTypeOf("function");
|
||||
expect(plugin.definition.onDuplexChannelWrite).toBeTypeOf("function");
|
||||
expect(plugin.definition.onDuplexChannelStop).toBeTypeOf("function");
|
||||
expect(plugin.definition.onDuplexChannelClose).toBeTypeOf("function");
|
||||
});
|
||||
|
||||
it("bumps the plugin version so the server reconciles the stored manifest", () => {
|
||||
// The bundled-plugin boot reconcile refreshes the stored manifest for an
|
||||
// existing install only when the version changes. The duplex capability needs
|
||||
// the bump to reach an existing install.
|
||||
expect(manifest.version).toBe("0.1.5");
|
||||
});
|
||||
|
||||
it("opens a duplex channel, forwards a host write, and closes it on lease release", async () => {
|
||||
process.env.DAYTONA_API_KEY = "host-key";
|
||||
// A fake PTY handle records each host write, drives the data stream on demand,
|
||||
// and records the kill and the disconnect.
|
||||
const inputs: string[] = [];
|
||||
let killed = 0;
|
||||
let disconnected = 0;
|
||||
let ptyOnData: ((data: Uint8Array) => void) | null = null;
|
||||
const handle = {
|
||||
async waitForConnection() {},
|
||||
async sendInput(data: string | Uint8Array) {
|
||||
inputs.push(typeof data === "string" ? data : new TextDecoder().decode(data));
|
||||
},
|
||||
wait() {
|
||||
return new Promise<{ exitCode?: number }>(() => {});
|
||||
},
|
||||
async kill() {
|
||||
killed += 1;
|
||||
},
|
||||
async disconnect() {
|
||||
disconnected += 1;
|
||||
},
|
||||
};
|
||||
const sandbox = createMockSandbox();
|
||||
(sandbox.process as Record<string, unknown>).createPty = vi.fn(
|
||||
async (options: { onData: (data: Uint8Array) => void }) => {
|
||||
ptyOnData = options.onData;
|
||||
return handle;
|
||||
},
|
||||
);
|
||||
mockCreate.mockResolvedValue(sandbox);
|
||||
|
||||
// Capture the data and the exit the worker forwards through `ctx.duplexChannel`.
|
||||
const dataChunks: Array<{ workerSessionId: string; chunk: string }> = [];
|
||||
const restore = __setDaytonaPluginContextForTest({
|
||||
duplexChannel: {
|
||||
data: (workerSessionId: string, chunk: string) =>
|
||||
dataChunks.push({ workerSessionId, chunk }),
|
||||
exit: () => {},
|
||||
},
|
||||
} as unknown as PluginContext);
|
||||
|
||||
try {
|
||||
await plugin.definition.onEnvironmentAcquireLease?.({
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
runId: "run-1",
|
||||
agentId: "agent-1",
|
||||
executionWorkspaceId: "workspace-1",
|
||||
adapterType: "codex_local",
|
||||
config: { image: "node:20", timeoutMs: 300000, reuseLease: true },
|
||||
});
|
||||
|
||||
const open = await plugin.definition.onDuplexChannelOpen?.({
|
||||
hostRouteId: "route-1",
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "sandbox-123",
|
||||
command: ["node", "/paperclip/gateway.mjs"],
|
||||
});
|
||||
expect(open?.workerSessionId).toMatch(/^duplex-/);
|
||||
const workerSessionId = open?.workerSessionId ?? "";
|
||||
|
||||
// The launch wrapper sets raw mode with echo off and redirects diagnostics.
|
||||
// It quotes each command argument and the diagnostics path as a shell word.
|
||||
expect(inputs[0]).toContain("stty raw -echo");
|
||||
expect(inputs[0]).toContain("exec 'node' '/paperclip/gateway.mjs'");
|
||||
expect(inputs[0]).toMatch(/2>'\/tmp\/paperclip-duplex-.+\.log'/);
|
||||
|
||||
// A host write reaches the process on the same channel.
|
||||
await plugin.definition.onDuplexChannelWrite?.({
|
||||
workerSessionId,
|
||||
data: '{"version":1,"type":"heartbeat"}\n',
|
||||
});
|
||||
expect(inputs[1]).toBe('{"version":1,"type":"heartbeat"}\n');
|
||||
|
||||
// Process output reaches the host as a data notification bound to the
|
||||
// worker session id.
|
||||
ptyOnData?.(new TextEncoder().encode('{"version":1,"type":"ready","address":"127.0.0.1:1"}\n'));
|
||||
expect(dataChunks).toEqual([
|
||||
{
|
||||
workerSessionId,
|
||||
chunk: '{"version":1,"type":"ready","address":"127.0.0.1:1"}\n',
|
||||
},
|
||||
]);
|
||||
|
||||
// Lease release closes the channel: it kills the child and releases the
|
||||
// pseudo-terminal socket.
|
||||
await plugin.definition.onEnvironmentReleaseLease?.({
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "sandbox-123",
|
||||
config: { image: "node:20", timeoutMs: 300000, reuseLease: true },
|
||||
});
|
||||
expect(killed).toBeGreaterThanOrEqual(1);
|
||||
expect(disconnected).toBe(1);
|
||||
|
||||
// The channel entry is gone, so a later write is a no-op and reaches no
|
||||
// process.
|
||||
const inputsBefore = inputs.length;
|
||||
await plugin.definition.onDuplexChannelWrite?.({
|
||||
workerSessionId,
|
||||
data: "late\n",
|
||||
});
|
||||
expect(inputs.length).toBe(inputsBefore);
|
||||
} finally {
|
||||
restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("normalizes config and validates the API key fallback", async () => {
|
||||
process.env.DAYTONA_API_KEY = "host-key";
|
||||
|
||||
|
||||
@@ -66,6 +66,24 @@ import type {
|
||||
DaytonaPtyProcess,
|
||||
} from "./setup-token-pty.js";
|
||||
|
||||
// The Daytona duplex command stream for the sandbox callback bridge. The channel
|
||||
// runs the gateway command on a raw pseudo-terminal, streams the frames, and
|
||||
// accepts host input. The worker resolves the sandbox by the provider lease id,
|
||||
// registers the channel under the host route id, and streams the data and the
|
||||
// exit through `ctx.duplexChannel`.
|
||||
export {
|
||||
createDaytonaDuplexChannelSessionOpener,
|
||||
openDaytonaDuplexChannelSession,
|
||||
buildDuplexChannelLaunchWrapper,
|
||||
} from "./duplex-command-stream.js";
|
||||
export type {
|
||||
DuplexChannelSession,
|
||||
DuplexChannelSessionOpener,
|
||||
DaytonaDuplexChannelOptions,
|
||||
} from "./duplex-command-stream.js";
|
||||
import { openDaytonaDuplexChannelSession as openDuplexChannelSession } from "./duplex-command-stream.js";
|
||||
import type { DuplexChannelSession } from "./duplex-command-stream.js";
|
||||
|
||||
// Injectable monotonic clock for provider-boundary timing (Open Q1). Defaults
|
||||
// to the real wall clock; `plugin.test.ts` overrides it via
|
||||
// `setDaytonaTimingClockForTest` so the measured `durationMs`/`getDurationMs`
|
||||
@@ -1899,6 +1917,41 @@ function forgetDaytonaSetupTokenPty(entry: DaytonaSetupTokenPtyEntry): void {
|
||||
daytonaSetupTokenPtyBySession.delete(entry.workerSessionId);
|
||||
}
|
||||
|
||||
// The worker-side registry of live duplex channels. The worker registers each
|
||||
// channel under the host-owned route identifier at open time, so the host closes
|
||||
// the exact channel by that identifier even when the open reply was lost. It also
|
||||
// indexes by the worker session identifier for write and stop. Each entry records
|
||||
// the provider lease id, so a lease teardown closes only its own channels. The
|
||||
// `onShutdown` hook closes every open channel here.
|
||||
interface DaytonaDuplexChannelEntry {
|
||||
hostRouteId: string;
|
||||
workerSessionId: string;
|
||||
providerLeaseId: string;
|
||||
session: DuplexChannelSession;
|
||||
}
|
||||
const daytonaDuplexChannelByRoute = new Map<string, DaytonaDuplexChannelEntry>();
|
||||
const daytonaDuplexChannelBySession = new Map<string, DaytonaDuplexChannelEntry>();
|
||||
|
||||
function forgetDaytonaDuplexChannel(entry: DaytonaDuplexChannelEntry): void {
|
||||
daytonaDuplexChannelByRoute.delete(entry.hostRouteId);
|
||||
daytonaDuplexChannelBySession.delete(entry.workerSessionId);
|
||||
}
|
||||
|
||||
// Close every open duplex channel that belongs to one provider lease and drop its
|
||||
// entry. The lease teardown hooks (release, destroy, resume) call this, so a
|
||||
// channel never outlives the sandbox that carries it. The close kills the child
|
||||
// and releases the pseudo-terminal socket, so no live channel survives the
|
||||
// teardown. The stored identifiers are always cleared, so no orphan id survives.
|
||||
async function closeDaytonaDuplexChannelsForLease(providerLeaseId: string): Promise<void> {
|
||||
const matches = [...daytonaDuplexChannelByRoute.values()].filter(
|
||||
(entry) => entry.providerLeaseId === providerLeaseId,
|
||||
);
|
||||
for (const entry of matches) {
|
||||
forgetDaytonaDuplexChannel(entry);
|
||||
await entry.session.close().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
const plugin = definePlugin({
|
||||
async setup(ctx) {
|
||||
// Hoist the context to a module variable so the lifecycle hooks and the
|
||||
@@ -2106,6 +2159,10 @@ const plugin = definePlugin({
|
||||
// session and leak its shell until sandbox reaping.
|
||||
if (sandbox.state !== "started") {
|
||||
sandboxHandleSessionStore.clear(scope);
|
||||
// A stopped sandbox loses its pseudo-terminals, so a stored duplex channel
|
||||
// is dead after a real restart. Close and drop every channel on this lease
|
||||
// before the restart, so no stale channel id survives the resume.
|
||||
await closeDaytonaDuplexChannelsForLease(params.providerLeaseId);
|
||||
}
|
||||
await ensureSandboxStarted(sandbox, toTimeoutSeconds(config.timeoutMs));
|
||||
try {
|
||||
@@ -2170,6 +2227,9 @@ const plugin = definePlugin({
|
||||
evictSandboxHandle(scope);
|
||||
await sandboxHandleActivityGates.waitForIdle(scope);
|
||||
await teardownSession(sandbox, scope);
|
||||
// Close every duplex channel on this lease before the stop or the delete,
|
||||
// so no channel outlives the sandbox and no stored channel id survives.
|
||||
await closeDaytonaDuplexChannelsForLease(params.providerLeaseId);
|
||||
|
||||
if (config.reuseLease) {
|
||||
if (sandbox.state !== "stopped") {
|
||||
@@ -2234,6 +2294,9 @@ const plugin = definePlugin({
|
||||
evictSandboxHandle(scope);
|
||||
await sandboxHandleActivityGates.waitForIdle(scope);
|
||||
await teardownSession(sandbox, scope);
|
||||
// Close every duplex channel on this lease before the delete, so no channel
|
||||
// outlives the sandbox and no stored channel id survives.
|
||||
await closeDaytonaDuplexChannelsForLease(params.providerLeaseId);
|
||||
await sandbox.delete(toTimeoutSeconds(config.timeoutMs));
|
||||
} finally {
|
||||
sandboxHandleTeardownGates.end(scope, teardownGate);
|
||||
@@ -2734,8 +2797,76 @@ const plugin = definePlugin({
|
||||
return { hostRouteId: params.hostRouteId };
|
||||
},
|
||||
|
||||
// Close every open login pseudo-terminal on an orderly shutdown, then drain the
|
||||
// sandbox handle cache, so a graceful shutdown holds no live login terminal.
|
||||
// Open one persistent duplex channel. Resolve the cached sandbox by the provider
|
||||
// lease id, run the gateway command on a raw pseudo-terminal, and register the
|
||||
// channel under the host route id. Stream the raw data and the exit through
|
||||
// `ctx.duplexChannel`, bound to the returned worker session id. Fail closed when
|
||||
// no cached sandbox matches the lease.
|
||||
async onDuplexChannelOpen(params) {
|
||||
const sandbox = await sandboxHandleCache.findByProviderLeaseId(params.providerLeaseId);
|
||||
if (!sandbox) {
|
||||
throw new Error(
|
||||
"Daytona duplex channel: no cached sandbox resolves the provider lease.",
|
||||
);
|
||||
}
|
||||
const session = await openDuplexChannelSession(
|
||||
sandbox.process as unknown as DaytonaPtyProcess,
|
||||
params.command,
|
||||
);
|
||||
const workerSessionId = `duplex-${randomUUID()}`;
|
||||
const entry: DaytonaDuplexChannelEntry = {
|
||||
hostRouteId: params.hostRouteId,
|
||||
workerSessionId,
|
||||
providerLeaseId: params.providerLeaseId,
|
||||
session,
|
||||
};
|
||||
daytonaDuplexChannelByRoute.set(params.hostRouteId, entry);
|
||||
daytonaDuplexChannelBySession.set(workerSessionId, entry);
|
||||
// Register the data listener before the first write, so no early data chunk is
|
||||
// lost. The client stamps the worker session id, so the host binds the data to
|
||||
// the open route.
|
||||
session.onData((chunk) => {
|
||||
pluginContext?.duplexChannel.data(workerSessionId, chunk);
|
||||
});
|
||||
// Forward the child exit one time. The host resolves the open route on it.
|
||||
void session.wait().then(
|
||||
(result) => pluginContext?.duplexChannel.exit(workerSessionId, result.exitCode),
|
||||
() => pluginContext?.duplexChannel.exit(workerSessionId, null),
|
||||
);
|
||||
return { workerSessionId };
|
||||
},
|
||||
|
||||
// Write host input to an open duplex channel, keyed by the worker session id.
|
||||
// Drop the input for an unknown session.
|
||||
async onDuplexChannelWrite(params) {
|
||||
const entry = daytonaDuplexChannelBySession.get(params.workerSessionId);
|
||||
if (!entry) return;
|
||||
entry.session.write(params.data);
|
||||
},
|
||||
|
||||
// Stop an open duplex channel child, keyed by the worker session id.
|
||||
async onDuplexChannelStop(params) {
|
||||
const entry = daytonaDuplexChannelBySession.get(params.workerSessionId);
|
||||
if (!entry) return;
|
||||
entry.session.kill();
|
||||
},
|
||||
|
||||
// Close an open duplex channel by the host route id and acknowledge the close
|
||||
// with the same identifier. The close is idempotent: it returns the
|
||||
// acknowledgement even when the entry is already gone, so the host confirms the
|
||||
// channel is closed. The worker never keys the close on the worker session id.
|
||||
async onDuplexChannelClose(params) {
|
||||
const entry = daytonaDuplexChannelByRoute.get(params.hostRouteId);
|
||||
if (entry) {
|
||||
forgetDaytonaDuplexChannel(entry);
|
||||
await entry.session.close().catch(() => undefined);
|
||||
}
|
||||
return { hostRouteId: params.hostRouteId };
|
||||
},
|
||||
|
||||
// Close every open login pseudo-terminal and every open duplex channel on an
|
||||
// orderly shutdown, then drain the sandbox handle cache, so a graceful shutdown
|
||||
// holds no live terminal and no live channel.
|
||||
async onShutdown() {
|
||||
const openSessions = [...daytonaSetupTokenPtyByRoute.values()];
|
||||
daytonaSetupTokenPtyByRoute.clear();
|
||||
@@ -2743,6 +2874,12 @@ const plugin = definePlugin({
|
||||
for (const entry of openSessions) {
|
||||
await entry.session.close().catch(() => undefined);
|
||||
}
|
||||
const openChannels = [...daytonaDuplexChannelByRoute.values()];
|
||||
daytonaDuplexChannelByRoute.clear();
|
||||
daytonaDuplexChannelBySession.clear();
|
||||
for (const entry of openChannels) {
|
||||
await entry.session.close().catch(() => undefined);
|
||||
}
|
||||
sandboxHandleCache.reset();
|
||||
},
|
||||
});
|
||||
|
||||
@@ -33,7 +33,7 @@ describe("duplex channel request schemas", () => {
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "lease-1",
|
||||
command: "paperclip-bridge",
|
||||
command: ["paperclip-bridge"],
|
||||
};
|
||||
const reply: PluginDuplexChannelOpenResult = { workerSessionId: "ws-1" };
|
||||
expect(open.hostRouteId).toBe("route-1");
|
||||
@@ -47,20 +47,20 @@ describe("duplex channel request schemas", () => {
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "lease-1",
|
||||
command: "paperclip-bridge",
|
||||
command: ["paperclip-bridge"],
|
||||
};
|
||||
expect(open).toBeDefined();
|
||||
});
|
||||
|
||||
it("rejects an open request with a non-string command", () => {
|
||||
it("rejects an open request whose command is not a string array", () => {
|
||||
const open: PluginDuplexChannelOpenParams = {
|
||||
hostRouteId: "route-1",
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "lease-1",
|
||||
// @ts-expect-error — command must be a string.
|
||||
command: 42,
|
||||
// @ts-expect-error — command must be a string array.
|
||||
command: "paperclip-bridge",
|
||||
};
|
||||
expect(open).toBeDefined();
|
||||
});
|
||||
|
||||
@@ -1111,8 +1111,12 @@ export interface PluginDuplexChannelOpenParams {
|
||||
environmentId: string;
|
||||
/** The provider lease the sandbox is cached under. The worker resolves the sandbox by it. */
|
||||
providerLeaseId: string;
|
||||
/** The command the worker runs on the channel. */
|
||||
command: string;
|
||||
/**
|
||||
* The command argument vector the worker runs on the channel. Element 0 is the
|
||||
* program and the rest are its arguments. The worker quotes each element for the
|
||||
* shell, so a shell metacharacter in an element cannot inject a shell command.
|
||||
*/
|
||||
command: readonly string[];
|
||||
}
|
||||
|
||||
/** The open reply. It returns the worker session identifier for data binding only. */
|
||||
|
||||
@@ -2486,6 +2486,14 @@ export function createTestHarness(options: TestHarnessOptions): TestHarness {
|
||||
// No-op in test harness — the host login route is not wired here.
|
||||
},
|
||||
},
|
||||
duplexChannel: {
|
||||
data(_workerSessionId: string, _chunk: string) {
|
||||
// No-op in test harness — the host duplex route is not wired here.
|
||||
},
|
||||
exit(_workerSessionId: string, _exitCode: number | null) {
|
||||
// No-op in test harness — the host duplex route is not wired here.
|
||||
},
|
||||
},
|
||||
tools: {
|
||||
register(name, _decl, fn) {
|
||||
requireCapability(manifest, capabilitySet, "agent.tools.register");
|
||||
|
||||
@@ -2037,6 +2037,36 @@ export interface PluginSetupTokenPtyClient {
|
||||
exit(workerSessionId: string, exitCode: number | null): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* `ctx.duplexChannel` — stream one persistent duplex channel's data and exit from
|
||||
* a sandbox provider worker to the host.
|
||||
*
|
||||
* The worker registers the data listener on the channel and forwards each raw
|
||||
* chunk through `data(workerSessionId, chunk)`. It forwards the child exit through
|
||||
* `exit(workerSessionId, exitCode)`. Each call carries the worker session
|
||||
* identifier the open reply returned, so the host binds the data to the open route
|
||||
* by that identifier while the route is open. The host drops a chunk or an exit
|
||||
* that carries an unknown or a mismatched identifier, and it never logs the raw
|
||||
* bytes. The default is a no-op that never throws. This client models the
|
||||
* `setupTokenPty` client, but it carries no login command allowlist.
|
||||
*/
|
||||
export interface PluginDuplexChannelClient {
|
||||
/**
|
||||
* Deliver one raw data chunk of a persistent duplex channel.
|
||||
*
|
||||
* @param workerSessionId - The worker session identifier the open reply returned.
|
||||
* @param chunk - The raw channel output text.
|
||||
*/
|
||||
data(workerSessionId: string, chunk: string): void;
|
||||
/**
|
||||
* Deliver the child exit of a persistent duplex channel.
|
||||
*
|
||||
* @param workerSessionId - The worker session identifier the open reply returned.
|
||||
* @param exitCode - The child exit code, or null when the child ended with no code.
|
||||
*/
|
||||
exit(workerSessionId: string, exitCode: number | null): void;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Full plugin context
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -2157,6 +2187,10 @@ export interface PluginContext {
|
||||
* pseudo-terminal. */
|
||||
setupTokenPty: PluginSetupTokenPtyClient;
|
||||
|
||||
/** Stream one persistent duplex channel's data and exit to the host. The
|
||||
* default is a no-op for a provider that opens no duplex channel. */
|
||||
duplexChannel: PluginDuplexChannelClient;
|
||||
|
||||
/** Register agent tool handlers. Requires `agent.tools.register`. */
|
||||
tools: PluginToolsClient;
|
||||
|
||||
|
||||
@@ -115,6 +115,8 @@ import type {
|
||||
import {
|
||||
SETUP_TOKEN_PTY_OUTPUT_NOTIFICATION,
|
||||
SETUP_TOKEN_PTY_EXIT_NOTIFICATION,
|
||||
DUPLEX_CHANNEL_DATA_NOTIFICATION,
|
||||
DUPLEX_CHANNEL_EXIT_NOTIFICATION,
|
||||
JSONRPC_VERSION,
|
||||
JSONRPC_ERROR_CODES,
|
||||
PLUGIN_RPC_ERROR_CODES,
|
||||
@@ -1400,6 +1402,30 @@ export function startWorkerRpcHost(options: WorkerRpcHostOptions): WorkerRpcHost
|
||||
},
|
||||
},
|
||||
|
||||
duplexChannel: {
|
||||
data(workerSessionId: string, chunk: string): void {
|
||||
// Forward one raw data chunk of a persistent duplex channel. The
|
||||
// notification carries the worker session identifier, so the host binds
|
||||
// the chunk to the open route by that identifier while the route is
|
||||
// open. The host drops an unknown or a mismatched identifier and never
|
||||
// logs the raw bytes. This notification carries no invocation id,
|
||||
// because it fires after the open reply returns.
|
||||
if (typeof workerSessionId !== "string" || workerSessionId.length === 0) return;
|
||||
if (typeof chunk !== "string" || chunk.length === 0) return;
|
||||
notifyHost(DUPLEX_CHANNEL_DATA_NOTIFICATION, { workerSessionId, chunk });
|
||||
},
|
||||
exit(workerSessionId: string, exitCode: number | null): void {
|
||||
// Forward the child exit of a persistent duplex channel. The host
|
||||
// resolves the open route's wait promise by the worker session
|
||||
// identifier while the route is open.
|
||||
if (typeof workerSessionId !== "string" || workerSessionId.length === 0) return;
|
||||
notifyHost(DUPLEX_CHANNEL_EXIT_NOTIFICATION, {
|
||||
workerSessionId,
|
||||
exitCode: typeof exitCode === "number" ? exitCode : null,
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
tools: {
|
||||
register(
|
||||
name: string,
|
||||
|
||||
@@ -925,7 +925,7 @@ describe("worker duplex channel dispatch", () => {
|
||||
async setup() {},
|
||||
async onDuplexChannelOpen(params) {
|
||||
expect(params.hostRouteId).toBe("route-1");
|
||||
expect(params.command).toBe("paperclip-bridge");
|
||||
expect(params.command).toEqual(["paperclip-bridge"]);
|
||||
expect(params.providerLeaseId).toBe("lease-1");
|
||||
return { workerSessionId: "ws-1" };
|
||||
},
|
||||
@@ -1004,7 +1004,7 @@ describe("worker duplex channel dispatch", () => {
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "lease-1",
|
||||
command: "paperclip-bridge",
|
||||
command: ["paperclip-bridge"],
|
||||
}),
|
||||
).resolves.toEqual({ workerSessionId: "ws-1" });
|
||||
|
||||
|
||||
Reference in new issue
Block a user