π OpenHands: AI-Driven Development
fix(verify): measure available memory on macOS (#18645)
Engel Nyst67ddddf7f4f0
diff --git a/.agents/skills/verify-openhands/SKILL.md b/.agents/skills/verify-openhands/SKILL.mdindex d77f143e39..ac14b9a08c 100644--- a/.agents/skills/verify-openhands/SKILL.md+++ b/.agents/skills/verify-openhands/SKILL.md@@ -77,9 +77,12 @@ control-openhands stop # stops only this run; evidence severy shell command when other agents share the machine: an agent that drivessomeone else's run corrupts both evidence ledgers. For the same reason never`pkill -f` or `killall` by pattern; `control-openhands stop` ends only your run.-- **Launch** refuses to start when less than about 2 GB of memory is free: each- run holds an Agent Server, automation, a static frontend and Chromium (about- 1.5 GB together). Stop runs you are done with; several agents on one machine+- **Launch** refuses to start when less than about 2 GB of memory is available+ (Linux `MemAvailable`, macOS `process.availableMemory()`, otherwise unused+ memory). On macOS, libuv >=1.52 includes reclaimable pages; older Node builds+ can still refuse conservatively. Each run holds an Agent Server, automation,+ a static frontend and Chromium (about 1.5 GB together). Stop runs you are done+ with; several agents on one machineshould each `launch --new`, export their own `OH_VERIFY_RUN`, and never touchanother agent's run.- **Launch** starts `bin/agent-canvas.mjs` from this checkout with a privatediff --git a/.agents/skills/verify-openhands/scripts/control-openhands.mjs b/.agents/skills/verify-openhands/scripts/control-openhands.mjsindex e2b8208be3..601d7da4f9 100755--- a/.agents/skills/verify-openhands/scripts/control-openhands.mjs+++ b/.agents/skills/verify-openhands/scripts/control-openhands.mjs@@ -351,6 +351,7 @@ function defaults() {}function availableMemoryBytes() {+ if (process.platform === "darwin") return process.availableMemory();// MemAvailable counts reclaimable cache; os.freemem() alone understates it.try {const line = readFileSync("/proc/meminfo", "utf8").match(diff --git a/.agents/skills/verify-openhands/scripts/control-openhands.test.mjs b/.agents/skills/verify-openhands/scripts/control-openhands.test.mjsindex 56f77d8f69..6bc1d39ea5 100755--- a/.agents/skills/verify-openhands/scripts/control-openhands.test.mjs+++ b/.agents/skills/verify-openhands/scripts/control-openhands.test.mjs@@ -99,8 +99,8 @@ function recorder(path = []) {};}-function run(args, env = {}) {- const result = spawnSync(process.execPath, [cli, ...args], {+function run(args, env = {}, execArgv = []) {+ const result = spawnSync(process.execPath, [...execArgv, cli, ...args], {encoding: "utf8",env: {...process.env,@@ -208,6 +208,103 @@ test("usage errors exit 2 with a hint; a missing run exits 3", () => {assert.match(noRun.json.hint, /control-openhands launch/);});+// Exercise the real launch guard with deterministic OS inputs. An empty PATH+// stops an accepted measurement at the next prerequisite, before any build or+// service starts. The preload changes only memory readings in this child.+function launchWithMemory(memory, flags = []) {+ const emptyPath = mkdtempSync(join(tmpdir(), "cov-empty-path-"));+ const preload = `+ import fs from "node:fs";+ import os from "node:os";+ import { syncBuiltinESMExports } from "node:module";+ const memory = ${JSON.stringify(memory)};+ Object.defineProperty(process, "platform", { value: memory.platform });+ process.availableMemory = () => memory.availableMb * 1024 * 1024;+ os.freemem = () => memory.freeMb * 1024 * 1024;+ const readFileSync = fs.readFileSync;+ fs.readFileSync = (path, ...args) => {+ if (path !== "/proc/meminfo") return readFileSync(path, ...args);+ if (memory.meminfo !== undefined) return memory.meminfo;+ throw new Error("/proc/meminfo is unavailable");+ };+ syncBuiltinESMExports();+ `;+ try {+ return run(["launch", "--new", ...flags], { PATH: emptyPath }, [+ "--import",+ `data:text/javascript,${encodeURIComponent(preload)}`,+ ]);+ } finally {+ rmSync(emptyPath, { recursive: true, force: true });+ }+}++test.each([+ {+ name: "macOS launch accepts available memory beyond unused pages",+ platform: "darwin",+ freeMb: 300,+ availableMb: 8192,+ error: /uvx is not on PATH/,+ },+ {+ name: "macOS launch refuses genuinely low available memory",+ platform: "darwin",+ freeMb: 8192,+ availableMb: 1024,+ error: /Only 1024 MB of memory available/,+ },+ {+ name: "macOS launch refuses an unknown native memory reading",+ platform: "darwin",+ freeMb: 8192,+ availableMb: 0,+ error: /Only 0 MB of memory available/,+ },+ {+ name: "macOS launch honors a custom minimum against available memory",+ platform: "darwin",+ freeMb: 300,+ availableMb: 3000,+ flags: ["--min-free-mb", "4000"],+ error: /Only 3000 MB of memory available/,+ },+ {+ name: "Linux launch keeps using MemAvailable",+ platform: "linux",+ freeMb: 300,+ availableMb: 0,+ meminfo: "MemFree: 307200 kB\nMemAvailable: 8388608 kB\n",+ error: /uvx is not on PATH/,+ },+ {+ name: "Linux launch keeps the free memory fallback when proc is missing",+ platform: "linux",+ freeMb: 4096,+ availableMb: 0,+ error: /uvx is not on PATH/,+ },+ {+ name: "Linux launch keeps the free memory fallback without MemAvailable",+ platform: "linux",+ freeMb: 1024,+ availableMb: 8192,+ meminfo: "MemFree: 1048576 kB\n",+ error: /Only 1024 MB of memory available/,+ },+ {+ name: "other platforms keep the free memory fallback",+ platform: "win32",+ freeMb: 4096,+ availableMb: 0,+ error: /uvx is not on PATH/,+ },+])("$name", ({ flags, error, ...memory }) => {+ const result = launchWithMemory(memory, flags);+ assert.equal(result.status, 3);+ assert.match(result.json.error, error);+});+test("keys are refused on argv before any request is made", () => {const dir = mkdtempSync(join(tmpdir(), "cov-run-"));mkdirSync(join(dir, "private"));