proc: deepzora and gumloop ask the shared proc.mjs whether a lock's owner lives (#1164)

#1198 (yetihan) moved 15 packages' liveness checks onto scripts/proc.mjs,
so a Linux zombie no longer holds a lock. Two were left on a bare
kill(pid, 0), which answers for a zombie as for a live process:

- deepzora's bridge lock (bridge.mjs lock(), the same code as
  reasonix-desktop's, which #1198 fixed): a zombie owner left "Another
  bridge operation is running" until the zombie was reaped.
- gumloop's ledger lock peers (gumloop.mjs isAlive): a zombie peer was
  counted live, so its ticket was waited on.

Both now import a synced proc.mjs (scripts/sync-proc.mjs lists 17
copies; both package.json files ship it). gumloop's crash-window test
writes its instrumented copy into a temp dir, so proc.mjs is copied
beside it. A new guard in scripts/proc.test.mjs fails when any package
file outside tests and proc.mjs calls kill(pid, 0).

deepzora 0.2.1, gumloop 0.1.2.

Run (mote, HOME=$(mktemp -d)):
- bun test scripts/proc.test.mjs packages/deepzora packages/gumloop:
  111 pass, 0 fail. node --test scripts/proc.test.mjs: 9 pass.
- deepzora node --test deepzora.test.mjs: 13 pass.
- node scripts/sync-proc.mjs --check: ok. bun scripts/check.mjs: ok.

Broken by hand: main's bridge.mjs and gumloop.mjs with the new tests:
the guard names both files, and the copies test fails on deepzora.

Not tried: a real zombie owner for these two (#1198's fork-based
zombie check covers proc.mjs itself); a real Gumloop or DeepZora
account. gumloop under node --test fails 3 of 3 on bun:test imports,
the same on origin/main.
This commit is contained in:
yetone
2026-11-16 12:31:01 +08:00
parent 9e86a2c7b7
commit 1823ff3a13
9 changed files with 767 additions and 625 deletions
+2 -2
View File
@@ -4,6 +4,7 @@ import path from "node:path"
import { randomUUID } from "node:crypto"
import { isDeepStrictEqual } from "node:util"
import { parseTOML, stringify, parseEnv } from "./vendor.mjs"
import { processExists } from "./proc.mjs"
import { agent } from "./deepzora.agent.js"
export const PROVIDER = "magpie-agent"
@@ -63,8 +64,7 @@ function lock(file) {
if (e.code !== "EEXIST") throw e
const pid = Number(read(file))
if (!Number.isSafeInteger(pid) || pid <= 0) throw new Error("Invalid bridge lock; inspect .magpie-bridge/lock")
try { process.kill(pid, 0) } catch (error) {
if (error.code !== "ESRCH") throw error
if (!processExists(pid)) {
fs.unlinkSync(file)
return lock(file)
}
+2 -1
View File
@@ -1,10 +1,11 @@
{
"name": "@magpie-community/agent-deepzora",
"version": "0.2.0",
"version": "0.2.1",
"description": "Deepzora Desktop, CLI and ACP as a Magpie agent, with a reversible TOML and dotenv configuration bridge",
"type": "module",
"main": "./index.mjs",
"files": [
"proc.mjs",
"index.mjs",
"deepzora.agent.js",
"bridge.mjs",
+59
View File
@@ -0,0 +1,59 @@
// GENERATED by scripts/sync-proc.mjs from scripts/proc.mjs. Do not edit package copies.
import { readFileSync } from "node:fs";
import { join } from "node:path";
const errCode = (error) => (error && typeof error === "object" ? error.code : undefined);
function validPid(pid) {
return Number.isSafeInteger(pid) && pid > 0;
}
export function processZombie(pid, {
procRoot = "/proc",
readFile = (file) => readFileSync(file, "utf8"),
} = {}) {
if (!validPid(pid)) return false;
let stat;
try {
stat = readFile(join(procRoot, String(pid), "stat"));
} catch {
return false;
}
const commEnd = typeof stat === "string" ? stat.lastIndexOf(")") : -1;
if (commEnd < 0) return false;
const state = stat.slice(commEnd + 1).trim()[0];
return state === "Z" || state === "X";
}
export function processAlive(pid, deps = {}) {
const state = processStatus(pid, deps).state;
return state === "alive" || state === "permission";
}
export function processExists(pid, deps = {}) {
const state = processStatus(pid, deps).state;
return state !== "dead" && state !== "invalid";
}
export function processStatus(pid, deps = {}) {
const kill = deps.kill ?? ((target, signal) => process.kill(target, signal));
if (!validPid(pid)) return { state: "invalid" };
let permission = false;
try {
kill(pid, 0);
} catch (error) {
const code = errCode(error);
if (code === "ESRCH") return { state: "dead" };
if (code !== "EPERM") return { state: "unknown", error };
permission = true;
}
if ((deps.platform ?? process.platform) !== "linux") {
return permission ? { state: "permission" } : { state: "alive" };
}
if (processZombie(pid, deps)) return { state: "dead" };
return permission ? { state: "permission" } : { state: "alive" };
}
export function processProbe(pid, deps = {}) {
return processStatus(pid, deps).state;
}
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -1,12 +1,13 @@
{
"name": "@magpie-community/opencode-gumloop-auth",
"version": "0.1.1",
"version": "0.1.2",
"description": "Gumloop (gumloop.com) with your API key and user id in OpenCode and magpie: per-account models, and a credit estimate Gumloop's API doesn't give",
"homepage": "https://www.gumloop.com/",
"type": "module",
"main": "./opencode.mjs",
"exports": { ".": "./opencode2.mjs" },
"files": [
"proc.mjs",
"index.mjs",
"gumloop.mjs",
"opencode.mjs",
+59
View File
@@ -0,0 +1,59 @@
// GENERATED by scripts/sync-proc.mjs from scripts/proc.mjs. Do not edit package copies.
import { readFileSync } from "node:fs";
import { join } from "node:path";
const errCode = (error) => (error && typeof error === "object" ? error.code : undefined);
function validPid(pid) {
return Number.isSafeInteger(pid) && pid > 0;
}
export function processZombie(pid, {
procRoot = "/proc",
readFile = (file) => readFileSync(file, "utf8"),
} = {}) {
if (!validPid(pid)) return false;
let stat;
try {
stat = readFile(join(procRoot, String(pid), "stat"));
} catch {
return false;
}
const commEnd = typeof stat === "string" ? stat.lastIndexOf(")") : -1;
if (commEnd < 0) return false;
const state = stat.slice(commEnd + 1).trim()[0];
return state === "Z" || state === "X";
}
export function processAlive(pid, deps = {}) {
const state = processStatus(pid, deps).state;
return state === "alive" || state === "permission";
}
export function processExists(pid, deps = {}) {
const state = processStatus(pid, deps).state;
return state !== "dead" && state !== "invalid";
}
export function processStatus(pid, deps = {}) {
const kill = deps.kill ?? ((target, signal) => process.kill(target, signal));
if (!validPid(pid)) return { state: "invalid" };
let permission = false;
try {
kill(pid, 0);
} catch (error) {
const code = errCode(error);
if (code === "ESRCH") return { state: "dead" };
if (code !== "EPERM") return { state: "unknown", error };
permission = true;
}
if ((deps.platform ?? process.platform) !== "linux") {
return permission ? { state: "permission" } : { state: "alive" };
}
if (processZombie(pid, deps)) return { state: "dead" };
return permission ? { state: "permission" } : { state: "alive" };
}
export function processProbe(pid, deps = {}) {
return processStatus(pid, deps).state;
}
+3 -1
View File
@@ -53,8 +53,10 @@ async function holdLock(directory, runtime = process.execPath, phase = "held") {
// A file, not a data: URL on the command line: Windows caps that at 32K.
let module = new URL("./gumloop.mjs", import.meta.url).href
if (phase !== "held") {
const copy = join(tempDir(), "gumloop.mjs")
const dir = tempDir()
const copy = join(dir, "gumloop.mjs")
writeFileSync(copy, source)
writeFileSync(join(dir, "proc.mjs"), readFileSync(new URL("./proc.mjs", import.meta.url)))
module = pathToFileURL(copy).href
}
const script = `
+25 -1
View File
@@ -1,3 +1,5 @@
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";
@@ -85,7 +87,7 @@ test("non-Linux platforms do not require procfs", () => {
});
test("runtime proc copies match the canonical implementation", () => {
assert.equal(procTargets.length, 15);
assert.equal(procTargets.length, 17);
for (const relative of procTargets) {
const target = `${root}${relative}`;
assert.deepEqual(readFileSync(target, "utf8"), canonical, relative);
@@ -95,7 +97,9 @@ test("runtime proc copies match the canonical implementation", () => {
"agent-office": ["lib/scan.mjs", "lib/jump.mjs"],
"agy-cli": ["index.mjs"],
astudio: ["browser.mjs"],
deepzora: ["bridge.mjs"],
"gcp-ai": ["lib/lock.mjs"],
gumloop: ["gumloop.mjs"],
goanyapi: ["src/auth.ts"],
"kimi-desktop": ["adapter.mjs"],
mclaw: ["browser.mjs"],
@@ -116,3 +120,23 @@ test("runtime proc copies match the canonical implementation", () => {
}
}
});
// #1164: a kill(pid, 0) of its own takes a zombie for a live process. A
// package that asks whether a pid lives goes through its proc.mjs copy.
test("no package asks kill(pid, 0) beside its proc.mjs", () => {
const { readdirSync } = require("node:fs");
const { join, relative } = require("node:path");
const skip = /(^|\/)(node_modules|dist|test|tests|scripts|\.git)(\/|$)|\.test\.|(^|\/)proc\.mjs$|\.min\.js$|vendor/;
const found = [];
const walk = (dir) => {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const path = join(dir, entry.name);
const rel = relative(root, path);
if (skip.test(rel)) continue;
if (entry.isDirectory()) walk(path);
else if (/\.(mjs|js|cjs|ts|mts)$/.test(entry.name) && /process\.kill\(\s*[\w.$]+\s*,\s*0\s*\)/.test(readFileSync(path, "utf8"))) found.push(rel);
}
};
walk(join(root, "packages"));
assert.deepEqual(found, []);
});
+2
View File
@@ -9,7 +9,9 @@ export const procTargets = [
"packages/agent-office/lib/proc.mjs",
"packages/agy-cli/proc.mjs",
"packages/astudio/proc.mjs",
"packages/deepzora/proc.mjs",
"packages/gcp-ai/lib/proc.mjs",
"packages/gumloop/proc.mjs",
"packages/goanyapi/src/proc.mjs",
"packages/kimi-desktop/proc.mjs",
"packages/mclaw/proc.mjs",