UNPKG

@phren/cli

Version:

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

199 lines (198 loc) • 8.97 kB
/** * The housekeeping safety rules. Every housekeeping check and action goes * through this module; nothing else in `bridge/housekeeping/` may run git, * delete a path or remove a worktree. The rules come from the owner, after * the 2026-11-08 incident in which a cleanup worker deleted worktrees with * unpushed commits and ran `git reset --hard` across about 24 repositories: * * 1. Never delete a worktree or repository that has uncommitted changes, * untracked files, commits on no remote, a stash, an operation in * progress, a dirty or unpushed submodule, a live process inside it or a * pane bound to it. Inspection that fails counts as a blocker. * 2. Never run git reset, checkout, clean, restore, switch, push, branch or a * stash change, and never pass --force. Git runs only through * `housekeepingGit`, which admits a fixed set of read-only commands plus * `worktree remove` (without force) and `worktree prune`. * 3. Suggest by default. Only a path inside an allow-listed cache root, * old enough and not in use, may be acted on without the owner. */ export declare class SafetyViolation extends Error { readonly code: string; constructor(code: string, message: string); } /** Throws unless `args` is a git invocation housekeeping may run. Global * options before the subcommand (-c, -C, --git-dir, --work-tree) are refused * outright: they could re-target or reconfigure the command. */ export declare function assertHousekeepingGit(args: readonly string[]): void; /** The only git runner housekeeping uses. */ export declare function housekeepingGit(cwd: string, ...args: string[]): Promise<string>; export type BlockerCode = "not-a-checkout" | "symlink" | "inspection-failed" | "main-checkout" | "linked-worktrees" | "locked" | "uncommitted-changes" | "untracked-files" | "unpushed-commits" | "stash" | "in-progress-operation" | "dirty-submodule" | "unpushed-submodule" | "live-process" | "bound-pane" | "in-use"; export interface Blocker { code: BlockerCode; detail: string; } /** One plain sentence for the first blocker, for the phone. */ export declare function plainReason(blockers: readonly Blocker[]): string; /** A process and the folder it runs in. */ export interface LiveProcess { pid: number; cwd: string; command?: string; } /** A pane and the folder its agent runs in. */ export interface BoundPane { pane: string; cwd: string; title?: string; } export interface InUseSources { processes: () => Promise<LiveProcess[]>; panes: () => Promise<BoundPane[]>; } export declare function pathKey(file: string): string; export declare function inside(child: string, parent: string): boolean; /** Processes and panes whose folder is `dir` or inside it. */ export declare function inUseBlockers(dir: string, sources: InUseSources): Promise<Blocker[]>; export type CheckoutTarget = "worktree" | "repository"; /** What a checkout holds, as the inventory shows it. */ export interface CheckoutFacts { commonDir: string; main: boolean; branch?: string; /** The repository's main checkout (for a linked worktree, the one it belongs to). */ repo?: string; dirty: number; untracked: number; unpushed: number; stashes: number; inProgress: string[]; locked: boolean; lastCommitAt?: string; } /** * What `dir` holds and every reason it must not be deleted (no blockers means * nothing would be lost). `worktree`: a linked worktree, removed with `git * worktree remove`; only its HEAD's commits matter, since its branch stays. * `repository`: the whole repository folder; every local branch counts, and * linked worktrees block. Stashes block either way: the owner's rule names * them without exception. */ export declare function inspectCheckout(dir: string, target: CheckoutTarget, sources: InUseSources): Promise<{ facts?: CheckoutFacts; blockers: Blocker[]; }>; /** Every reason `dir` must not be deleted; see `inspectCheckout`. */ export declare function checkoutBlockers(dir: string, target: CheckoutTarget, sources: InUseSources): Promise<Blocker[]>; export interface WorktreeEntry { path: string; branch?: string; locked?: string; prunable?: string; bare?: boolean; } export declare function parseWorktreeList(porcelain: string): WorktreeEntry[]; /** Remove a linked worktree only if it has no blockers right now, re-checked * against fresh state, and only with plain `git worktree remove`. A failed * removal is never retried with force. */ export declare function removeWorktreeSafely(dir: string, sources: InUseSources): Promise<{ removed: boolean; blockers: Blocker[]; }>; export interface CacheRule { id: string; /** Absolute root; only entries strictly inside it qualify. */ root: string; /** Minimum age of an entry, by mtime, before it may be acted on. */ minAgeDays: number; } /** Caches the disk-janitor proved safe to remove; every other path is * suggest-only. Platform-specific roots are listed only where they exist. */ export declare function cacheRules(home?: string, platform?: NodeJS.Platform): CacheRule[]; /** Paths housekeeping never acts on, whatever a rule says. DeviceFS is a live * view of booted simulators that `du` reports as ~160 GB; docker volumes hold * data; the store and project folders hold work. */ export declare function protectedRoots(home?: string): string[]; export type CacheDecision = { verdict: "auto"; rule: string; } | { verdict: "suggest" | "keep"; rule?: string; reason: string; blockers?: Blocker[]; }; export interface CacheContext extends InUseSources { rules?: CacheRule[]; protected?: string[]; now?: number; } /** * Whether `target` may be removed without the owner. `auto` only for an entry * strictly inside an allow-listed root (resolved without following a link at * the target), past the rule's age, under no protected root, and with no * process or pane in it. Anything else is `suggest` (the owner may approve) * or `keep` (a safety rule holds it). */ export declare function cacheDecision(target: string, context: CacheContext): Promise<CacheDecision>; /** Delete an allow-listed cache entry, re-deciding against fresh state first. * Refuses (returns the decision) unless the decision is `auto` now, or the * owner approved it and the only thing short of `auto` is its age: an owner * may clear a recent cache entry, never a path outside the cache roots. */ export declare function removeCacheEntry(target: string, context: CacheContext, ownerApproved?: boolean): Promise<{ removed: boolean; decision: CacheDecision; }>; /** Scratch roots whose direct children the owner may delete. */ export declare function tmpRoots(platform?: NodeJS.Platform): string[]; export type PathDecision = { allowed: true; rule: string; } | { allowed: false; reason: string; blockers?: Blocker[]; }; export interface DeletionContext extends CacheContext { tmpRoots?: string[]; uid?: number; } /** * Whether the owner may delete `target`, decided now against the disk: * - an entry inside an allow-listed cache root (any age, since the owner asked); * - a direct child of a scratch root this user owns, untouched for a day, not * a git checkout (those go through the worktree rules), with nothing in it; * - a checkout's own `node_modules` (it has package.json), with nothing * running in that checkout. * Never a symlink, never under a protected root except that node_modules. */ export declare function deletionDecision(target: string, context: DeletionContext): Promise<PathDecision>; /** Delete what the owner approved, deciding again just before. */ export declare function removeApprovedPath(target: string, context: DeletionContext): Promise<{ removed: boolean; decision: PathDecision; }>; export interface PaneState { server: string; pane: string; status?: string; role?: string; } export interface PaneDispatch { id?: string; server: string; pane: string; returned?: { read: boolean; }; closedAt?: string; } /** Why a pane must stay open, or undefined if the owner may close it. A * pane whose dispatch return is unread keeps the evidence (the 2026-11-08 * lesson); a working or waiting agent and the conductor stay too. */ export declare function paneCloseBlock(pane: PaneState, dispatches: readonly PaneDispatch[]): string | undefined; /** What an item of a given kind may be: only a cache can ever be `auto`. */ export declare function allowedVerdict(kind: string, proposed: "auto" | "suggest" | "keep"): "auto" | "suggest" | "keep"; /** Every process's working directory this user can see: `lsof` on macOS, * `/proc/<pid>/cwd` on Linux. A failure throws, so callers fail closed. */ export declare function liveProcessCwds(platform?: NodeJS.Platform): Promise<LiveProcess[]>; export declare function parseLsofCwds(output: string): LiveProcess[];