@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
253 lines (252 loc) • 12.6 kB
TypeScript
import { type PhrenResult } from "../shared.js";
import { type ProjectConfigOverrides } from "../project-config.js";
/** @internal Exported for tests. */
export declare const MAX_QUEUE_ENTRY_LENGTH = 500;
/** Size budgets. A budget of 0 is off. Nothing a person or agent wrote is ever removed for one. */
export interface StoreBudgets {
/** Review queue entries per project; past it machine-generated entries are not queued there. */
reviewEntries: number;
/** This computer's primary `.runtime`, in MB (1,048,576 bytes); reported only. */
runtimeMB: number;
/** `.runtime/agent-fanouts-archive` in MB; past it the archive sweep deletes the oldest finished folders. */
fanoutArchiveMB: number;
/** Remove regenerable `.runtime` files unused this many days (`runtimeAgeOut`). */
runtimeAgeOutDays: number;
/** Off by default; on, it still waits for a week of daily store health lines. */
runtimeAgeOut: boolean;
}
export interface RetentionPolicy {
schemaVersion?: number;
ttlDays: number;
retentionDays: number;
autoAcceptThreshold: number;
minInjectConfidence: number;
decay: {
d30: number;
d60: number;
d90: number;
d120: number;
};
budgets: StoreBudgets;
}
export interface WorkflowPolicy {
schemaVersion?: number;
lowConfidenceThreshold: number;
riskySections: Array<"Review" | "Stale" | "Conflicts">;
findingSensitivity: "minimal" | "conservative" | "balanced" | "aggressive";
}
export interface IndexPolicy {
schemaVersion?: number;
includeGlobs: string[];
excludeGlobs: string[];
includeHidden: boolean;
}
/**
* Outcome of the push leg of a sync.
*
* `saved-local` means "committed, nothing to push or no remote" — a success.
* It used to double as the report for a *failed* push, which is how a store
* could go two months without syncing while every Stop hook looked healthy.
* Failures now get their own values:
*
* - `pull-failed` - the fetch/merge leg failed, so push never ran
* - `push-failed` — push was attempted and rejected
* - `unrelated-histories` — local and remote share no merge base; no amount of
* retrying will fix it, so it is called out by name
*/
export type PushStatus = "saved-local" | "saved-pushed" | "no-upstream" | "pull-failed" | "push-failed" | "unrelated-histories" | "error";
export declare const PUSH_STATUSES: readonly PushStatus[];
/** Push statuses that mean the store is NOT in sync with its remote. */
export declare const FAILED_PUSH_STATUSES: ReadonlySet<PushStatus>;
export type AutoSaveStatus = "clean" | "saved-local" | "saved-pushed" | "no-upstream" | "sync-failed" | "error";
export declare const AUTO_SAVE_STATUSES: readonly AutoSaveStatus[];
export interface RuntimeHealth {
schemaVersion?: number;
lastSessionStartAt?: string;
lastPromptAt?: string;
lastStopAt?: string;
lastAutoSave?: {
at: string;
status: AutoSaveStatus;
detail?: string;
};
lastGovernance?: {
at: string;
status: "ok" | "error";
detail: string;
};
lastSync?: {
lastPullAt?: string;
lastPullStatus?: "ok" | "error";
lastPullDetail?: string;
lastSuccessfulPullAt?: string;
lastPushAt?: string;
lastPushStatus?: PushStatus;
lastPushDetail?: string;
unsyncedCommits?: number;
/**
* Consecutive sync attempts that failed to reach the remote. Reset to 0 on
* any successful push. This is what turns "one flaky push" into "this store
* has been broken for weeks" without needing to keep a history.
*/
consecutiveFailures?: number;
/** Last time a push actually reached the remote. */
lastSuccessfulPushAt?: string;
/** Commits ahead of and behind the upstream tracking ref at the last sync outcome. */
ahead?: number;
behind?: number;
/** The last failure of a store sync in plain words, cleared by the next full success. */
lastError?: string;
lastErrorAt?: string;
/** Unresolved paths from the last store merge, cleared once upstream is integrated. */
conflictFiles?: string[];
};
}
export type SyncStatus = NonNullable<RuntimeHealth["lastSync"]>;
export interface BuildSyncStatusOpts {
now: string;
pushStatus: SyncStatus["lastPushStatus"];
pushDetail?: string;
pullAt?: string;
pullStatus?: SyncStatus["lastPullStatus"];
pullDetail?: string;
successfulPullAt?: string;
unsyncedCommits?: number;
consecutiveFailures?: number;
successfulPushAt?: string;
}
export declare function buildSyncStatus(opts: BuildSyncStatusOpts): SyncStatus;
/** Warn after this many consecutive failed syncs. */
export declare const SYNC_FAILURE_WARN_RUNS = 3;
/** ...or after this many days without a successful push, whichever comes first. */
export declare const SYNC_FAILURE_WARN_DAYS = 3;
export interface SyncOutageAssessment {
/** True when the store has been failing long enough to tell the user. */
degraded: boolean;
consecutiveFailures: number;
daysSinceSuccess: number | null;
/** One-line, user-facing explanation. Empty when not degraded. */
summary: string;
}
/**
* Decide whether a store's sync failures have gone on long enough to warrant a
* visible warning. Pure so both the Stop hook and `phren status` can ask the
* same question and get the same answer.
*/
export declare function assessSyncOutage(sync: SyncStatus | undefined, nowMs?: number): SyncOutageAssessment;
export type RetentionPolicyPatch = Partial<Omit<RetentionPolicy, "decay" | "budgets">> & {
decay?: Partial<RetentionPolicy["decay"]>;
budgets?: Partial<StoreBudgets>;
};
export declare const GOVERNANCE_SCHEMA_VERSION = 1;
/** Default retention policy. Exported so {@link config/schema} can render one source of truth. */
export declare const DEFAULT_POLICY: RetentionPolicy;
/** Age-out never removes a file unused for less than a week. */
export declare const MIN_RUNTIME_AGE_OUT_DAYS = 7;
/**
* Budgets with every field present. Only a number is read, so a numeric 0 is off and anything else
* (a string, a negative number) keeps the default. Age-out days have no off value: under a week reads as the default.
*/
export declare function normalizeBudgets(raw: unknown): StoreBudgets;
/** Why a budgets patch from the CLI or set_config is refused, or null. A project sets only its review budget. */
export declare function budgetsPatchError(patch: unknown, scope: "global" | "project"): string | null;
/** This computer's runtime age-out opt-in, in `.runtime` (never synced): when it first saw age-out on, and whether its week has passed. */
export interface RuntimeAgeOutState {
enabledSince: string;
ready: boolean;
}
export declare const RUNTIME_AGE_OUT_STATE = "runtime-age-out.json";
export declare function readRuntimeAgeOutState(phrenPath: string): RuntimeAgeOutState | null;
/** Age-out (and the fanout archive's byte ceiling) acts only while it is on and this computer has waited its week. */
export declare function runtimeAgeOutActive(phrenPath: string): boolean;
/**
* A project's budgets: its own store's retention policy with the project's overrides. `resolved` is budgets a
* caller already merged for `store` (the prompt hook's config); they are used only when that is the project's store.
*/
export declare function projectBudgets(phrenPath: string, project: string, resolved?: {
store: string;
budgets: StoreBudgets;
}): StoreBudgets;
/** Default workflow policy. Exported so {@link config/schema} can render one source of truth. */
export declare const DEFAULT_WORKFLOW_POLICY: WorkflowPolicy;
/** Default index policy. Exported so {@link config/schema} can render one source of truth. */
export declare const DEFAULT_INDEX_POLICY: IndexPolicy;
type GovernanceSchema = "retention-policy" | "workflow-policy" | "index-policy";
export declare function validateGovernanceJson(filePath: string, schema: GovernanceSchema): boolean;
export interface ResolvedConfig {
findingSensitivity: WorkflowPolicy["findingSensitivity"];
proactivity: {
base?: "high" | "medium" | "low";
findings?: "high" | "medium" | "low";
};
retentionPolicy: RetentionPolicy;
workflowPolicy: WorkflowPolicy;
}
export { VALID_PROACTIVITY_LEVELS, VALID_FINDING_SENSITIVITY, type FindingSensitivityLevel, VALID_RISKY_SECTIONS, } from "./policy-constants.js";
export declare function getProjectConfigOverrides(phrenPath: string, projectName: string): ProjectConfigOverrides | null;
export declare function mergeConfig(phrenPath: string, projectName?: string, profile?: string): ResolvedConfig;
export declare function getRetentionPolicy(phrenPath: string, projectName?: string): RetentionPolicy;
export declare function updateRetentionPolicy(phrenPath: string, patch: RetentionPolicyPatch): PhrenResult<RetentionPolicy>;
export declare function getWorkflowPolicy(phrenPath: string, projectName?: string): WorkflowPolicy;
export declare function updateWorkflowPolicy(phrenPath: string, patch: Partial<WorkflowPolicy>): PhrenResult<WorkflowPolicy>;
/** An index glob is matched from each project's folder and stays inside it:
* relative, with no `..` and no backslash (an escape in a glob, a separator
* on Windows). Blank entries are not globs. */
export declare function isProjectGlob(glob: unknown): glob is string;
export declare function getIndexPolicy(phrenPath: string): IndexPolicy;
export declare function updateIndexPolicy(phrenPath: string, patch: Partial<IndexPolicy>): PhrenResult<IndexPolicy>;
export declare function getRuntimeHealth(phrenPath: string): RuntimeHealth;
export declare function updateRuntimeHealth(phrenPath: string, patch: Partial<RuntimeHealth>): RuntimeHealth;
export declare function normalizeQueueEntryText(raw: string, opts?: {
truncate?: boolean;
}): PhrenResult<{
text: string;
truncated: boolean;
}>;
/**
* A queue entry that carries structured provenance alongside its text.
*
* `meta` is a pre-rendered run of HTML comments (e.g. `<!-- source:extract -->
* <!-- phren:cite {...} -->`) appended verbatim to the queue line. It exists so a
* queued candidate stays *promotable*: `approveQueueItem` reads it back and hands
* the same repo/commit/source provenance to the normal add-finding path, which
* makes a promoted finding indistinguishable from one added directly.
*
* Producers build the comments themselves (this module deliberately does not import
* the citation helpers, to keep governance free of a content-layer import cycle).
*/
export interface ReviewQueueEntry {
text: string;
meta?: string;
}
export type ReviewQueueEntryInput = string | ReviewQueueEntry;
/**
* `machine`: an entry phren generated on its own (governance, TTL review, extraction, the tool hook, a dream's
* proposal), held back while the project's queue is over its review budget. `explicit`, the default: an entry
* a person or agent asked for, which always goes in.
*/
export type ReviewQueueOrigin = "machine" | "explicit";
/** Over budget a machine-generated batch fails with `OVER_BUDGET`, so callers can tell it from a duplicate (ok, 0). */
export declare function appendReviewQueue(phrenPath: string, project: string, section: "Review" | "Stale" | "Conflicts", entries: ReviewQueueEntryInput[], opts: {
origin: ReviewQueueOrigin;
}): PhrenResult<number>;
export interface PruneMemoriesResult {
/** Human-readable summary (plus dry-run detail lines), ready to print. */
message: string;
/** Entries deleted for exceeding the retention window. */
pruned: number;
/** TTL-expired entries promoted into review.md's `## Stale` section. */
ttlExpired: number;
/** Of those, entries not queued because the project's review queue was over its budget. */
heldBack: number;
}
/**
* Delete entries past the retention window and promote TTL-expired entries into review.md.
*
* TTL promotion lives here rather than in the CLI handler so that nightly maintenance
* (`handleBackgroundMaintenance`, which calls this directly) gets it too — previously it
* only ran on a manual `phren maintain prune`, leaving `## Stale` empty while `## Review`
* filled up.
*/
export declare function pruneDeadMemories(phrenPath: string, project?: string, dryRun?: boolean): PhrenResult<PruneMemoriesResult>;
export declare function consolidateProjectFindings(phrenPath: string, project: string, dryRun?: boolean): PhrenResult<string>;