Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(engine): read /proc/<pid>/status for RSS on linux instead of shelling out to ps
Vance Ingalls3cee3b94f7b8
diff --git a/packages/engine/src/utils/processRss.test.ts b/packages/engine/src/utils/processRss.test.tsindex 7f4877d8a7..074ed78224 100644--- a/packages/engine/src/utils/processRss.test.ts+++ b/packages/engine/src/utils/processRss.test.ts@@ -1,5 +1,10 @@import { describe, expect, it } from "vitest";-import { parsePsRss, parseTasklistCsv, sampleProcessRss } from "./processRss.js";+import {+ parseProcStatusRss,+ parsePsRss,+ parseTasklistCsv,+ sampleProcessRss,+} from "./processRss.js";describe("parsePsRss", () => {it("parses pid and rss kilobytes into megabytes", () => {@@ -32,7 +37,59 @@ describe("parseTasklistCsv", () => {});});+describe("parseProcStatusRss", () => {+ it("parses the VmRSS line of /proc/<pid>/status", () => {+ const status =+ "Name:\tchrome\nState:\tS (sleeping)\nVmPeak:\t 9999999 kB\nVmRSS:\t 1048576 kB\nThreads:\t12\n";+ expect(parseProcStatusRss(4242, status)).toEqual([{ pid: 4242, rssMb: 1024 }]);+ });++ it("returns [] when VmRSS is absent (zombie) or zero", () => {+ expect(parseProcStatusRss(1, "Name:\tchrome\nState:\tZ (zombie)\nThreads:\t1\n")).toEqual([]);+ expect(parseProcStatusRss(1, "VmRSS:\t 0 kB\n")).toEqual([]);+ });+});+describe("sampleProcessRss", () => {+ it("reads /proc/<pid>/status per pid on linux and never shells out", async () => {+ let execCalled = false;+ const exec = async () => {+ execCalled = true;+ return { stdout: "" };+ };+ const reads: string[] = [];+ const readStatus = async (path: string) => {+ reads.push(path);+ return `Name:\tchrome\nVmRSS:\t ${path.endsWith("/1/status") ? 1024 : 2048} kB\n`;+ };+ const result = await sampleProcessRss([1, 2], exec, "linux", readStatus);+ expect(reads).toEqual(["/proc/1/status", "/proc/2/status"]);+ expect(execCalled).toBe(false);+ expect(result).toEqual([+ { pid: 1, rssMb: 1 },+ { pid: 2, rssMb: 2 },+ ]);+ });++ it("omits only the pid whose /proc read fails on linux", async () => {+ const exec = async () => ({ stdout: "" });+ const readStatus = async (path: string) => {+ if (path === "/proc/1/status") throw new Error("ENOENT");+ return "VmRSS:\t 2048 kB\n";+ };+ expect(await sampleProcessRss([1, 2], exec, "linux", readStatus)).toEqual([+ { pid: 2, rssMb: 2 },+ ]);+ });++ it("never rejects on linux when every /proc read fails", async () => {+ const exec = async () => ({ stdout: "" });+ const readStatus = async () => {+ throw new Error("ENOENT");+ };+ expect(await sampleProcessRss([1, 2], exec, "linux", readStatus)).toEqual([]);+ });+it("uses one ps call for all pids on posix", async () => {const calls: Array<{ file: string; args: readonly string[] }> = [];const exec = async (file: string, args: readonly string[]) => {@@ -84,6 +141,8 @@ describe("sampleProcessRss", () => {const exec = async () => {throw new Error("ENOENT");};- expect(await sampleProcessRss([1], exec, "linux")).toEqual([]);+ // darwin, not linux: linux no longer reaches exec at all.+ expect(await sampleProcessRss([1], exec, "darwin")).toEqual([]);+ expect(await sampleProcessRss([1], exec, "win32")).toEqual([]);});});diff --git a/packages/engine/src/utils/processRss.ts b/packages/engine/src/utils/processRss.tsindex 48af97caf2..d1f55b7635 100644--- a/packages/engine/src/utils/processRss.ts+++ b/packages/engine/src/utils/processRss.ts@@ -1,4 +1,5 @@import { execFile } from "node:child_process";+import { readFile } from "node:fs/promises";import { promisify } from "node:util";export interface ProcessRssSample {@@ -7,9 +8,12 @@ export interface ProcessRssSample {}export type ExecFileLike = (file: string, args: readonly string[]) => Promise<{ stdout: string }>;+export type ReadFileLike = (path: string) => Promise<string>;const KB_PER_MB = 1024;+const defaultReadFile: ReadFileLike = (path) => readFile(path, "utf8");+function defaultExec(): ExecFileLike {// promisify lazily so vitest module mocks of child_process still take effect// (same reason psnr.ts does this).@@ -46,18 +50,43 @@ export function parseTasklistCsv(pid: number, stdout: string): ProcessRssSample[return [{ pid, rssMb: Math.round(kb / KB_PER_MB) }];}+/** `/proc/<pid>/status` line `VmRSS:\t 12345 kB`; absent for a zombie. */+export function parseProcStatusRss(pid: number, status: string): ProcessRssSample[] {+ const match = /^VmRSS:\s*(\d+)\s*kB/m.exec(status);+ if (!match) return [];+ const kb = Number(match[1]);+ if (!Number.isFinite(kb) || kb <= 0) return [];+ return [{ pid, rssMb: Math.round(kb / KB_PER_MB) }];+}+/**- * Resident set size per pid, in MiB. Zero dependencies: `ps` on POSIX (one- * call for all pids), `tasklist` on Windows (one call per pid). Never+ * Resident set size per pid, in MiB. Zero dependencies: `/proc/<pid>/status`+ * on Linux (the render images are `node:*-slim` and ship no `ps`; same+ * precedent as the CLI's orphanCleanup / processTree), `ps` on other POSIX+ * (one call for all pids), `tasklist` on Windows (one call per pid). Never* rejects; a pid that exited between discovery and sampling is simply absent.*/export async function sampleProcessRss(pids: readonly number[],exec: ExecFileLike = defaultExec(),platform: NodeJS.Platform = process.platform,+ readStatus: ReadFileLike = defaultReadFile,): Promise<ProcessRssSample[]> {const unique = [...new Set(pids.filter((p) => Number.isInteger(p) && p > 0))];if (unique.length === 0) return [];+ if (platform === "linux") {+ // Per-pid catch: one exited pid must not erase the others' samples.+ const perPid = await Promise.all(+ unique.map(async (pid) => {+ try {+ return parseProcStatusRss(pid, await readStatus(`/proc/${pid}/status`));+ } catch {+ return [];+ }+ }),+ );+ return perPid.flat();+ }try {if (platform === "win32") {const results: ProcessRssSample[] = [];