UNPKG

@phren/cli

Version:

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

82 lines (81 loc) • 3.42 kB
import { type Budget, type Pressure } 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 declare const PRIORITIES: { readonly release: 2; readonly normal: 1; readonly low: 0; }; export type PriorityName = keyof typeof PRIORITIES; /** What a `workers` ticket says about the fan-out worker it holds a place for, * so the Hook can show a queued worker under the agent that launched it before * the worker has a manifest. `provider` is absent until a picker chooses it. */ export interface WorkerTag { parent: { provider: "codex" | "claude"; session: string; }; provider?: "codex" | "opencode" | "claude"; model?: string; } export interface Ticket { id: string; pid: number; priority: number; createdAt: number; label: string; state: "waiting" | "running"; startedAt?: number; worker?: WorkerTag; } /** 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 declare function workerTag(command: readonly string[], env: NodeJS.ProcessEnv, nearest: () => "codex" | "claude" | undefined, known?: { provider?: string; model?: string; }): WorkerTag | undefined; export declare function slotsRoot(env?: NodeJS.ProcessEnv): string; export declare function priorityName(priority: number): PriorityName; /** Tickets of a pool, with dead holders' and waiters' tickets removed. */ export declare function readTickets(dir: string, isAlive?: (pid: number) => boolean): Ticket[]; /** Waiters in the order they will start: release work first, then by arrival. */ export declare function queueOrder(tickets: readonly Ticket[]): Ticket[]; export declare function minutes(ms: number): string; export interface AcquireOptions { pool: string; slots: number; priority: number; label: string; budget: Budget; pressure: () => Promise<Pressure | undefined>; /** Called each time the reason for waiting changes, and once on start. */ report: (message: string) => void; pollMs?: number; timeoutMs?: number; root?: string; pid?: number; now?: () => number; sleep?: (ms: number) => Promise<void>; /** The fan-out worker this ticket holds a place for (the `workers` pool). */ worker?: WorkerTag; } export interface Held { id: string; release: () => void; waitedMs: number; } /** Waits for a slot. Throws `QueueTimeout` after `timeoutMs`; the ticket is removed either way. */ export declare function acquire(options: AcquireOptions): Promise<Held>; export declare class QueueTimeout extends Error { } export declare function poolStatus(pool: string, root?: string): Ticket[]; export declare function pools(root?: string): string[];