@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
164 lines (163 loc) • 8.27 kB
JavaScript
import { randomBytes } from "node:crypto";
import * as fs from "node:fs";
import { homedir } from "node:os";
import * as path from "node:path";
import { withFileLock } from "../governance/locks.js";
import { pressureReason } from "./budget.js";
/**
* A computer-wide counting semaphore in a directory: one small JSON ticket per
* waiting or running job, ordered by priority and then arrival. Any shell on
* the computer joins it through `phren heavy`, and `phren fanout run` through
* its `workers` pool, so agents in different panes, harnesses and stores share
* one count. A ticket whose process is gone is dropped by the next reader, so
* a killed job never holds its slot.
*/
export const PRIORITIES = { release: 2, normal: 1, low: 0 };
const SESSION_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const PROVIDERS = new Set(["codex", "opencode", "claude"]);
/** The worker a queued command will start, read from the launcher's own
* environment and command line the way the fan-out wrappers and `phren fanout
* run` choose a manifest's parent: Codex's thread id unless the nearest agent
* process above is Claude, then Claude's session id. The provider comes from
* `--provider` or a wrapper named `<provider>.sh`, the model from `--model`. */
export function workerTag(command, env, nearest, known = {}) {
const codex = [env.CODEX_THREAD_ID, env.CODEX_SESSION_ID].find(value => value && SESSION_UUID.test(value));
const claude = env.CLAUDE_CODE_SESSION_ID && SESSION_UUID.test(env.CLAUDE_CODE_SESSION_ID) ? env.CLAUDE_CODE_SESSION_ID : undefined;
const parent = codex && !(claude && nearest() === "claude") ? { provider: "codex", session: codex.toLowerCase() }
: claude ? { provider: "claude", session: claude.toLowerCase() } : undefined;
if (!parent)
return undefined;
const flag = (name) => { const at = command.indexOf(name); return at >= 0 ? command[at + 1] : undefined; };
const script = /^(codex|opencode|claude)\.sh$/.exec(path.basename(command[0] ?? ""))?.[1];
const provider = [known.provider, flag("--provider"), script].find(value => value && PROVIDERS.has(value));
const model = (known.model ?? flag("--model"))?.trim().slice(0, 200);
return { parent, ...(provider ? { provider } : {}), ...(model ? { model } : {}) };
}
export function slotsRoot(env = process.env) {
return env.PHREN_SLOTS_DIR || path.join(env.XDG_STATE_HOME || path.join(homedir(), ".local", "state"), "phren", "slots");
}
function alive(pid) {
try {
process.kill(pid, 0);
return true;
}
catch (error) {
return error.code === "EPERM";
}
}
export function priorityName(priority) {
return Object.keys(PRIORITIES).find(name => PRIORITIES[name] === priority) ?? "normal";
}
/** Tickets of a pool, with dead holders' and waiters' tickets removed. */
export function readTickets(dir, isAlive = alive) {
let names;
try {
names = fs.readdirSync(dir);
}
catch {
return [];
}
const tickets = [];
for (const name of names.filter(n => n.endsWith(".json"))) {
const file = path.join(dir, name);
try {
const ticket = JSON.parse(fs.readFileSync(file, "utf8"));
if (!Number.isInteger(ticket.pid) || !isAlive(ticket.pid)) {
fs.rmSync(file, { force: true });
continue;
}
tickets.push(ticket);
}
catch { /* Partly written or removed meanwhile. */ }
}
return tickets;
}
/** Waiters in the order they will start: release work first, then by arrival. */
export function queueOrder(tickets) {
return tickets.filter(t => t.state === "waiting").sort((a, b) => b.priority - a.priority || a.createdAt - b.createdAt || a.id.localeCompare(b.id));
}
/**
* Whether ticket `id` may start now. The pressure gate holds only while
* another job in the pool runs: with the pool empty the first job always
* starts (with a warning), so pressure from outside the pool can slow work
* but never stop it for good.
*/
function decide(tickets, id, slots, pressure, budget, now = Date.now()) {
const running = tickets.filter(t => t.state === "running");
const order = queueOrder(tickets), position = order.findIndex(t => t.id === id);
const busy = running.map(t => `${t.label} (pid ${t.pid}, ${minutes(now - (t.startedAt ?? t.createdAt))})`).join(", ");
if (running.length >= slots)
return { start: false, reason: `all ${slots} slot${slots === 1 ? "" : "s"} busy: ${busy}` };
if (position > 0)
return { start: false, reason: `${position} job${position === 1 ? "" : "s"} ahead (${order.slice(0, position).map(t => `${t.label}, ${priorityName(t.priority)}`).join("; ")})` };
const pressed = pressure && pressureReason(pressure, budget);
if (pressed && running.length)
return { start: false, reason: `${pressed}; ${running.length} running: ${busy}` };
return pressed ? { start: true, warning: `${pressed}; starting anyway because nothing else in this pool is running` } : { start: true };
}
export function minutes(ms) {
if (ms < 60_000)
return `${Math.max(0, Math.round(ms / 1000))}s`;
const m = Math.round(ms / 60_000);
return m < 60 ? `${m}m` : `${Math.floor(m / 60)}h ${m % 60}m`;
}
/** Waits for a slot. Throws `QueueTimeout` after `timeoutMs`; the ticket is removed either way. */
export async function acquire(options) {
if (!/^[a-z0-9][a-z0-9-]{0,40}$/.test(options.pool))
throw new Error("Pool names are lowercase letters, digits and dashes.");
const dir = path.join(options.root ?? slotsRoot(), options.pool), now = options.now ?? Date.now;
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
const id = `${now()}-${randomBytes(6).toString("hex")}`, file = path.join(dir, `${id}.json`), began = now();
const ticket = { id, pid: options.pid ?? process.pid, priority: options.priority, createdAt: began, label: options.label.slice(0, 120), state: "waiting",
...(options.worker ? { worker: options.worker } : {}) };
const write = () => { const tmp = `${file}.tmp`; fs.writeFileSync(tmp, JSON.stringify(ticket), { mode: 0o600 }); fs.renameSync(tmp, file); };
const release = () => { fs.rmSync(file, { force: true }); };
write();
const sleep = options.sleep ?? (ms => new Promise(resolve => setTimeout(resolve, ms)));
let said = "";
try {
for (;;) {
const pressure = await options.pressure().catch(() => undefined);
const decision = withFileLock(path.join(dir, ".queue"), () => {
const result = decide(readTickets(dir), id, options.slots, pressure, options.budget, now());
if (result.start) {
ticket.state = "running";
ticket.startedAt = now();
write();
}
return result;
});
if (decision.start) {
const waited = now() - began;
options.report(`${decision.warning ? `warning: ${decision.warning}. ` : ""}started${waited >= 1_000 ? ` after waiting ${minutes(waited)}` : ""}`);
return { id, release, waitedMs: waited };
}
if (decision.reason !== said) {
said = decision.reason;
options.report(`queued: ${decision.reason}`);
}
if (options.timeoutMs !== undefined && now() - began >= options.timeoutMs)
throw new QueueTimeout(`gave up after ${minutes(now() - began)} in the queue: ${said}`);
await sleep(options.pollMs ?? 5_000);
}
}
catch (error) {
release();
throw error;
}
}
export class QueueTimeout extends Error {
}
export function poolStatus(pool, root = slotsRoot()) {
const dir = path.join(root, pool);
const tickets = readTickets(dir);
return [...tickets.filter(t => t.state === "running").sort((a, b) => (a.startedAt ?? 0) - (b.startedAt ?? 0)), ...queueOrder(tickets)];
}
export function pools(root = slotsRoot()) {
try {
return fs.readdirSync(root, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name).sort();
}
catch {
return [];
}
}