@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
77 lines (76 loc) • 3.37 kB
TypeScript
import type { PaneRef, ProcessRow } from "../resources.js";
import type { InventoryAction } from "./inventory.js";
/**
* The programs holding this computer's memory, swap and CPU, so the owner can
* see the hogs and end one from the phone. Read live on each request (a `ps`
* and, on Linux, /proc), never from the hourly scan. A program's helpers are
* one row: Chromium's renderers count toward the browser that started them,
* and ending the row ends that browser. Ending is the owner's signed approve;
* the Hook re-reads the process first and refuses the ones the computer or
* the owner's session needs, other users' processes, Phren Hook itself and the
* conductor.
*/
export interface InventoryProcess {
id: string;
/** The program's name. */
name: string;
/** The row's root process: the one ending it signals. */
pid: number;
/** Processes counted in the row (the root and its same-program helpers). */
processes: number;
cpuPercent: number;
memoryBytes: number;
/** Linux only; macOS does not report swap per process. */
swapBytes?: number;
startedAt?: string;
/** The root's command line, shortened. */
command: string;
/** The agent pane it runs in, when one started it. */
pane?: {
server: string;
pane: string;
label?: string;
agent?: string;
};
action: InventoryAction;
}
export interface ProcessSources {
rows: () => Promise<ProcessRow[]>;
owners?: () => Promise<Map<number, PaneRef>>;
/** Pane keys (`server/pane`) of conductors, which are never ended here. */
conductors?: () => Promise<Set<string>>;
platform?: NodeJS.Platform;
/** This user's uid; the Hook's by default. */
uid?: number;
/** pid -> owner uid for the given pids. */
uids?: (pids: number[]) => Promise<Map<number, number>>;
/** pid -> exact start (a stable identity); `ps lstart` or /proc starttime. */
starts?: (pids: number[]) => Promise<Map<number, string>>;
/** pid -> swapped bytes (Linux). */
swap?: (pids: number[]) => Promise<Map<number, number>>;
/** pid -> CPU percent over a short window (Linux; ps pcpu there is a lifetime average). */
cpu?: (pids: number[]) => Promise<Map<number, number>>;
selfPid?: number;
limit?: number;
}
/** Same-program helpers grouped under the topmost ancestor running the same program. */
export declare function processGroups(rows: readonly ProcessRow[]): Map<number, ProcessRow[]>;
/** Why a row may not be ended, in a plain sentence; undefined when it may. */
export declare function endBlock(root: ProcessRow, context: {
uid?: number;
ownerUid?: number;
selfChain: ReadonlySet<number>;
pane?: PaneRef;
conductor: boolean;
paneProcess: boolean;
}): string | undefined;
/** The largest programs by memory and swap, plus anything busy on the CPU, each with its end action. */
export declare function listProcesses(sources: ProcessSources): Promise<InventoryProcess[]>;
/** Ends the row the owner approved: re-reads it, re-checks the rules, sends TERM, then KILL to whatever of it is left after 5 seconds. */
export declare function endProcess(id: string, sources: ProcessSources, wait?: (ms: number) => Promise<unknown>): Promise<{
status: "done";
freedBytes: number;
} | {
status: "refused" | "gone";
reason: string;
}>;