@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
135 lines (134 loc) • 6.48 kB
TypeScript
import type { InboxSource } from "../owner-inbox.js";
import type { ComputerResources } from "../resources.js";
import { type ApproveResult, type Badge, type Cleaned, type HousekeepingView, type ItemDraft, type StoredReport, type Trigger } from "./report.js";
import { type Inventory, type InventoryOptions } from "./inventory.js";
import { type ProcessSources } from "./processes.js";
import { type InUseSources } from "./safety.js";
/**
* The Hook's housekeeping pass: every TICK_MS, and at once when free disk
* drops below the floor or the computer becomes stressed, run each check,
* store the report for RETAIN_DAYS, and serve the phone's view of it. Checks
* only observe and propose. The cache cleaner acts on allow-listed entries
* daily and on fresh low-disk edges. Every other action requires approval.
* Both paths go through safety.ts against fresh state.
*/
export declare const TICK_MS: number;
export declare const RETAIN_DAYS = 14;
/** Below this the RC suites fail; the audit's floor for warning and cleaning. */
export declare const DISK_FLOOR_BYTES: number;
/** Approve answers within this; later work comes back `started` and lands in `cleaned`. */
export declare const APPROVE_BUDGET_MS = 45000;
/** The inventory scan (sizes are slow) runs hourly and on request. */
export declare const SCAN_MS: number;
/** The overview's badge may wait this long; a slower read falls back to the last good one. */
export declare const BADGE_WAIT_MS = 2000;
/** The overview badge, bounded: one read at a time, and a read slower than `waitMs` falls back to the last good badge. */
export declare function boundedBadge(read: () => Promise<Badge | undefined>, waitMs?: number): () => Promise<Badge | undefined>;
export interface CheckContext extends InUseSources {
resources: ComputerResources;
now: number;
}
export interface Check {
name: string;
run: (context: CheckContext) => Promise<ItemDraft[]>;
}
/** Warnings are machine-health findings, with their ids and wording, so the
* phone and the conductor's `machine_health` name the same thing the same way. */
export declare const healthCheck: Check;
export declare const DEFAULT_CHECKS: Check[];
/** `<bridge>/housekeeping/`: reports/ (one file per pass), cleaned.json, dismissed.json. */
export declare class HousekeepingStore {
readonly dir: string;
readonly reports: string;
constructor(dir?: string);
private names;
save(report: StoredReport, now: number): Promise<void>;
latest(): Promise<StoredReport | undefined>;
cleaned(now: number): Promise<Cleaned[]>;
addCleaned(entry: Cleaned, now: number): Promise<void>;
inventory(): Promise<Inventory | undefined>;
setInventory(inventory: Inventory): Promise<void>;
dismissed(): Promise<Map<string, {
fingerprint: string;
at: string;
}>>;
setDismissed(marks: Map<string, {
fingerprint: string;
at: string;
}>, now: number): Promise<void>;
}
export interface HousekeeperOptions extends InUseSources {
computer: string;
resources: () => Promise<ComputerResources>;
checks?: Check[];
store?: HousekeepingStore;
now?: () => number;
/** Free bytes on the volume actions free space on; statfs of the home folder by default. */
freeBytes?: (volume: string) => Promise<number>;
/** Inventory sources beyond processes and panes (sessions, dispatches, simulators, docker, roots). */
inventory?: Omit<InventoryOptions, "processes" | "panes">;
/** Close a pane through the terminal provider. */
closePane?: (server: string, pane: string) => Promise<void>;
/** Runs simctl and docker for approved actions; execFile by default. */
command?: (file: string, args: string[]) => Promise<void>;
/** The live process table and pane owners, for the Processes list and ending a hog (processes.ts). */
processSources?: ProcessSources;
}
export declare class Housekeeper {
private readonly options;
private current;
private loaded;
private running;
private queue;
private cachedView;
private lastEdge;
private inventory;
private inventoryLoaded;
private scanning;
private readonly store;
private readonly now;
constructor(options: HousekeeperOptions);
private latest;
/** One pass. Concurrent callers share the pass in flight. */
run(trigger?: Trigger, persist?: boolean): Promise<StoredReport>;
private runReport;
private pass;
private latestInventory;
/** Scan the inventory in the background; concurrent callers share the scan. */
scan(): Promise<Inventory>;
/** The phone's view of the newest report; undefined before the first pass. */
view(includeDismissed?: boolean, includeInventory?: boolean): Promise<HousekeepingView | undefined>;
/** The Processes list, read now; omitted when the process table can't be read. */
private liveProcesses;
/** The overview badge, from the cached view: no pass, no git. */
badge(): Promise<Badge | undefined>;
/** Called with each resource read: a fresh drop below the disk floor or a
* fresh stressed state runs a pass now rather than at the next tick. */
observe(resources: ComputerResources): Promise<StoredReport> | undefined;
/** Warnings worth waking the owner for, as owner-inbox sources keyed by item
* id: a reworded finding stays one row, which resolves itself when it goes. */
inboxSources(): InboxSource[];
private serial;
/** Hide items until their state changes. Unknown ids are ignored. */
dismiss(ids: string[]): Promise<{
revision: string;
}>;
restore(ids: string[]): Promise<{
revision: string;
}>;
/**
* Apply the owner's approval. A fresh pass runs first and each id is judged
* against it: an id that is no longer there (its state changed) is `gone`,
* a kept item or a warning is `refused`, and an action re-checks safety
* against the disk itself as it runs. Work past APPROVE_BUDGET_MS comes back
* `started` and finishes in the background, landing in `cleaned`.
*/
approve(ids: string[], by: Cleaned["by"]): Promise<ApproveResult>;
private cleanCaches;
private freeBytes;
private apply;
/** An inventory row (prefixed id) as an approvable item; it is re-checked live by its executor. */
private inventoryItem;
/** Live state for executors: fresh sessions and dispatches, never the scan's copy. */
private fresh;
}