UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

58 lines (57 loc) • 3.68 kB
import * as fs from "node:fs"; import { cpus, freemem, homedir, loadavg, totalmem } from "node:os"; import * as path from "node:path"; import * as yaml from "js-yaml"; const GB = 1024 ** 3; export const budgetKeys = { workers: "PHREN_FANOUT_WORKERS", heavySlots: "PHREN_HEAVY_SLOTS", minFreeGB: "PHREN_MIN_FREE_GB", maxLoadPerCore: "PHREN_MAX_LOAD_PER_CORE" }; /** `$XDG_CONFIG_HOME/phren/budget.yaml`: this computer's own budget, never synced with the store. */ export function budgetFile(env = process.env) { return path.join(env.XDG_CONFIG_HOME || path.join(homedir(), ".config"), "phren", "budget.yaml"); } export function defaultBudget(totalBytes) { const gb = totalBytes / GB; return { workers: Math.max(1, Math.floor(gb / 7)), heavySlots: Math.max(1, Math.floor(gb / 12)), minFreeBytes: Math.max(2, Math.round(gb * 0.1)) * GB, maxLoadPerCore: 1.5 }; } /** Defaults from the memory size, then budget.yaml, then the environment. Invalid values are ignored. */ export function readBudget(totalBytes, env = process.env, file = budgetFile(env)) { const budget = defaultBudget(totalBytes); let config = {}; try { const parsed = yaml.load(fs.readFileSync(file, "utf8"), { schema: yaml.CORE_SCHEMA }); if (parsed && typeof parsed === "object") config = parsed; } catch { /* No file or unreadable: defaults. */ } const value = (key) => { const raw = env[budgetKeys[key]] ?? config[key]; const n = typeof raw === "number" ? raw : typeof raw === "string" && raw.trim() ? Number(raw) : NaN; return Number.isFinite(n) && n > 0 ? n : undefined; }; const workers = value("workers"), heavySlots = value("heavySlots"), minFree = value("minFreeGB"), load = value("maxLoadPerCore"); return { workers: workers ? Math.floor(workers) : budget.workers, heavySlots: heavySlots ? Math.floor(heavySlots) : budget.heavySlots, minFreeBytes: minFree ? minFree * GB : budget.minFreeBytes, maxLoadPerCore: load ?? budget.maxLoadPerCore }; } /** Memory comes from the resources gauge's reader (bridge/resources.ts), so the budget and the Hook's gauge agree. */ export async function readPressure(platform = process.platform) { const { readMemory } = await import("../bridge/resources.js"); const memory = await readMemory(platform).catch(() => ({ totalBytes: totalmem(), availablePercent: undefined })); const totalBytes = memory.totalBytes || totalmem(); return { totalBytes, availableBytes: memory.availablePercent === undefined ? freemem() : Math.round(totalBytes * memory.availablePercent / 100), load1: loadavg()[0], cores: cpus().length || 1 }; } const gb = (bytes) => `${(bytes / GB).toFixed(1)} GB`; /** Why nothing new should start now, or undefined when the computer has room. */ export function pressureReason(pressure, budget) { const reasons = []; if (pressure.availableBytes !== undefined && pressure.availableBytes < budget.minFreeBytes) { reasons.push(`${gb(pressure.availableBytes)} of ${gb(pressure.totalBytes)} memory available, under the ${gb(budget.minFreeBytes)} floor`); } const perCore = pressure.load1 / pressure.cores; if (perCore > budget.maxLoadPerCore) reasons.push(`load ${pressure.load1.toFixed(1)} on ${pressure.cores} cores, over ${budget.maxLoadPerCore} per core`); return reasons.length ? reasons.join("; ") : undefined; } export function describePressure(pressure) { return `${pressure.availableBytes === undefined ? "memory unknown" : `${gb(pressure.availableBytes)} of ${gb(pressure.totalBytes)} memory available`}, load ${pressure.load1.toFixed(1)} on ${pressure.cores} cores`; }