Skip to content

Write HTML. Render video. Built for agents.

fix(producer): render inside an agent's macOS sandbox no longer dies on uptime (#5842)

Miguel Ángel26a5ab775b77
diff --git a/packages/producer/src/services/render/renderDirOwner.sandbox.test.ts b/packages/producer/src/services/render/renderDirOwner.sandbox.test.ts
new file mode 100644
index 0000000000..d952bbcea4
--- /dev/null
+++ b/packages/producer/src/services/render/renderDirOwner.sandbox.test.ts
@@ -0,0 +1,105 @@
+import { spawnSync } from "node:child_process";
+import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
+import { hostname, tmpdir } from "node:os";
+import { basename, join } from "node:path";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const probes = vi.hoisted(() => ({ uptime: false, pidns: false }));
+
+// An agent's macOS seatbelt refuses uptime: "uv_uptime returned EPERM (operation not permitted)".
+vi.mock("node:os", async (actual) => {
+ const os = await actual<typeof import("node:os")>();
+ return {
+ ...os,
+ uptime: () => {
+ if (probes.uptime) return os.uptime();
+ throw Object.assign(new Error("A system error occurred: uv_uptime returned EPERM"), {
+ code: "ERR_SYSTEM_ERROR",
+ });
+ },
+ };
+});
+
+// Without /proc, as on macOS, so Linux CI takes the uptime path too.
+vi.mock("node:fs", async (actual) => {
+ const fs = await actual<typeof import("node:fs")>();
+ return {
+ ...fs,
+ readFileSync: ((path: string, ...rest: unknown[]) => {
+ if (path === "/proc/sys/kernel/random/boot_id")
+ throw Object.assign(new Error("ENOENT"), { code: "ENOENT" });
+ return (fs.readFileSync as (...args: unknown[]) => unknown)(path, ...rest);
+ }) as typeof fs.readFileSync,
+ readlinkSync: ((path: string, ...rest: unknown[]) => {
+ if (path === "/proc/self/ns/pid" && !probes.pidns)
+ throw Object.assign(new Error("ENOENT"), { code: "ENOENT" });
+ return (fs.readlinkSync as (...args: unknown[]) => unknown)(path, ...rest);
+ }) as typeof fs.readlinkSync,
+ };
+});
+
+const { createOwnedRenderDir, createRenderWorkDir } = await import("./renderDirOwner.js");
+
+const JOB = "0f8c2b1e-5d3a-4c7b-9e21-6a4f0d8b3c19";
+const platform = process.platform;
+
+describe("a render work dir where the system refuses uptime", () => {
+ let parent: string;
+ const deadPid = () => spawnSync(process.execPath, ["-e", ""]).pid!;
+ const plant = (name: string, owner: object): string => {
+ mkdirSync(join(parent, name));
+ writeFileSync(join(parent, name, "owner.json"), JSON.stringify(owner));
+ return name;
+ };
+ const ownStamp = () => {
+ const own = createOwnedRenderDir(join(parent, "self-"));
+ const stamp = JSON.parse(readFileSync(join(own, "owner.json"), "utf-8"));
+ rmSync(own, { recursive: true });
+ return stamp;
+ };
+
+ beforeEach(() => {
+ parent = mkdtempSync(join(tmpdir(), "hf-render-dir-sandbox-"));
+ probes.uptime = false;
+ probes.pidns = false;
+ });
+ afterEach(() => {
+ Object.defineProperty(process, "platform", { value: platform });
+ rmSync(parent, { recursive: true, force: true });
+ });
+
+ it("is still created and stamped with its owner", () => {
+ const dir = createRenderWorkDir(join(parent, "work-"), parent);
+ const owner = JSON.parse(readFileSync(join(dir, "owner.json"), "utf-8"));
+ expect(owner.pid).toBe(process.pid);
+ });
+
+ it("keeps another same-hostname machine's dir when neither boot nor pid namespace can be read", () => {
+ // Machine A's live render on a shared output dir; its pid does not exist here on machine B.
+ const other = plant(`work-${JOB}-Aa0000`, { pid: deadPid(), host: hostname() });
+
+ const created = basename(createRenderWorkDir(join(parent, `work-${JOB}-`), parent));
+
+ expect(readdirSync(parent).sort()).toEqual([other, created].sort());
+ });
+
+ it("on Linux, keeps it when only the pid namespace cannot be read", () => {
+ Object.defineProperty(process, "platform", { value: "linux" });
+ probes.uptime = true;
+ const other = plant(`work-${JOB}-Bb0000`, { ...ownStamp(), pid: deadPid() });
+
+ const created = basename(createRenderWorkDir(join(parent, `work-${JOB}-`), parent));
+
+ expect(readdirSync(parent).sort()).toEqual([other, created].sort());
+ });
+
+ it("on macOS, where there is no pid namespace, still reclaims a dead render's dir by boot time", () => {
+ Object.defineProperty(process, "platform", { value: "darwin" });
+ probes.uptime = true;
+ plant(`work-${JOB}-Cc0000`, { ...ownStamp(), pid: deadPid() });
+
+ const created = basename(createRenderWorkDir(join(parent, `work-${JOB}-`), parent));
+
+ expect(readdirSync(parent)).toEqual([created]);
+ });
+});
diff --git a/packages/producer/src/services/render/renderDirOwner.ts b/packages/producer/src/services/render/renderDirOwner.ts
index a072f73313..6a36df4739 100644
--- a/packages/producer/src/services/render/renderDirOwner.ts
+++ b/packages/producer/src/services/render/renderDirOwner.ts
@@ -35,7 +35,7 @@ function pidScope(): { host: string; boot?: string; pidns?: string } {
// Without /proc (macOS, Windows) the boot time, to the minute, stands in for the boot id.
boot:
attempt(() => readFileSync("/proc/sys/kernel/random/boot_id", "utf-8").trim()) ??
- String(Math.round(Date.now() / 60_000 - uptime() / 60)),
+ attempt(() => String(Math.round(Date.now() / 60_000 - uptime() / 60))),
pidns: attempt(() => readlinkSync("/proc/self/ns/pid")),
};
}
@@ -59,9 +59,21 @@ function alive(pid: number): boolean {
type OwnerRecord = { pid?: unknown; host?: unknown; boot?: unknown; pidns?: unknown };
-function inThisPidScope(owner: OwnerRecord): boolean {
+function knownPidScope(): ReturnType<typeof pidScope> | undefined {
const scope = pidScope();
- return owner.host === scope.host && owner.boot === scope.boot && owner.pidns === scope.pidns;
+ const unknown =
+ scope.boot === undefined || (process.platform === "linux" && scope.pidns === undefined);
+ return unknown ? undefined : scope;
+}
+
+function inThisPidScope(owner: OwnerRecord): boolean {
+ const scope = knownPidScope();
+ return (
+ scope !== undefined &&
+ owner.host === scope.host &&
+ owner.boot === scope.boot &&
+ owner.pidns === scope.pidns
+ );
}
/** "gone" only for an owner in this pid scope that has exited; "none" when there is no owner file, else "unknown". */