diff --git a/packages/plugins/sandbox-providers/kubernetes/src/file-sync.ts b/packages/plugins/sandbox-providers/kubernetes/src/file-sync.ts new file mode 100644 index 00000000000..da259cbab42 --- /dev/null +++ b/packages/plugins/sandbox-providers/kubernetes/src/file-sync.ts @@ -0,0 +1,805 @@ +/** + * Native file-sync transport for the Kubernetes sandbox provider. + * + * Implements the opt-in `onEnvironmentSyncIn`/`onEnvironmentSyncOut` hooks over + * the pod exec data channel, so workspace/asset transfers stream through a + * SINGLE exec per operation instead of the default base64 chunk loop (which + * costs one exec per ~4 MB chunk — see `upload-interceptor.ts`). + * + * The transport is pure streaming: raw tar bytes move over the exec's stdin / + * stdout channels straight to and from a host file on disk, so neither the host + * nor the pod ever holds the whole payload in memory. + * + * - syncIn: build one host tarball of the operation's payload on disk, stream + * its raw bytes over the exec's stdin into `head -c | tar -x` + * inside the pod (N is the exact archive size, known host-side, so + * the pod command self-terminates without depending on stdin EOF), + * then stage-then-`mv -f` each single file onto its target so an + * interrupted transfer never leaves a truncated file. + * - syncOut: run `tar -c … -f -` inside the pod over one exec, streaming the + * archive on stdout straight into a host file, then reassemble it. + * + * The exec primitive is injected as `PodStreamExec` so this module stays free of + * any `@kubernetes/client-node` coupling and is unit-testable against a + * shell-backed fake. `plugin.ts` binds it to `execInPodStreaming` for a resolved + * pod. + * + * Security model (carried from the seam's design review): every sandbox path is + * confined to the workspace remote dir both lexically on the host AND via an + * in-pod `realpath` check that pins the resolved directory inode through + * `/proc/self/fd` before any byte is written — closing the symlink-swap TOCTOU + * window. Secret files land at their requested `mode` with no world-readable + * window (the staging dir is created `0700` and each file is `chmod`ed before + * the rename). All interpolated paths are single-quoted. Sandbox-authored + * tarballs (outbound) are member-confined before host extraction. + * + * Memory + disk posture: because the archive streams to/from disk, a transfer of + * any size keeps host and pod memory flat — there is no whole-payload buffer and + * therefore no 100 MB in-memory cap (the old base64-buffered transport needed + * one). Inbound bytes are host-authored (the tar this module builds), so they + * need no untrusted-size bound. Outbound bytes are authored by the untrusted + * pod, which controls how much it emits, so the sink `Writable` is wrapped in a + * streamed-bytes guard that fails the transfer closed above `maxOutputBytes` + * before it can fill the host's disk; the pod's stderr is separately capped in + * `execInPodStreaming`. + */ +import path from "node:path"; +import os from "node:os"; +import { promises as fs, createReadStream, createWriteStream } from "node:fs"; +import { Transform } from "node:stream"; +import { finished } from "node:stream/promises"; +import type { Readable, Writable } from "node:stream"; +import { randomUUID } from "node:crypto"; +import { execFile } from "node:child_process"; +import { promisify } from "node:util"; +import type { + PluginEnvironmentSyncResult, + PluginSyncFileMapping, + PluginSyncOperation, +} from "@paperclipai/plugin-sdk"; + +const execFileAsync = promisify(execFile); + +/** + * Injected streaming pod-exec primitive. `command` is always + * `["/bin/sh", "-c",