UNPKG

@phren/cli

Version:

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

137 lines (136 loc) • 7.16 kB
/** * Dreaming on the Hook: a ticker that starts a dream (docs/dreaming.md) for * each project whose `dreaming` config makes one due, one project at a time. * * Why not `schedules.ts`: a schedule there is a prompt typed into a new agent * pane on a fixed timing, recorded in `schedules.yaml` and launched through * Herdr. A dream is no pane and no prompt: it is a headless pass whose timing * depends on per-project config, on whether anyone is working in the project * and on its own last-run marker. So this is a small module of its own that * borrows schedules.ts's shape (an injected clock, a non-reentrant `tick`, a * timer in server.ts) rather than a schedule entry. * * Each dream runs as `phren dream run --project <p> --scheduled` in a child * process at low CPU priority, so the dream engine and its helper (`claude -p`, * which inherits the niceness) never block or crash the Hook, and the CLI * records `dreams/last-run.json` the same way for manual and scheduled runs. */ import { type DreamingConfig } from "../config/dreaming.js"; import type { AgentHooks } from "./agent-hooks.js"; import type { CodexQuestions } from "./questions.js"; export declare const DREAM_TICK_MS: number; /** After starting a dream, the ticker waits this long unless the child reports lock contention. */ export declare const DREAM_RETRY_MS: number; export declare const DREAM_BUSY_EXIT_CODE = 75; /** A scheduled dream that runs longer than this is stopped. */ export declare const DREAM_CHILD_TIMEOUT_MS: number; /** One live agent session as the Hook sees it. */ export interface DreamSession { project?: string; status?: string; } export { readLastRun, type DreamLastRun } from "../dream/records.js"; import { type DreamAcceptance, type DreamDecisionConflict, type DreamLastRun } from "../dream/records.js"; export declare function dreamsDir(phrenPath: string, project: string): string | null; /** * Per-status counts from a dream result, read loosely so the engine's result * shape can grow: `counts` if it reports one, else its per-change `outcomes` * (or `changes`) tallied by `status`/`outcome`. */ export declare function dreamOutcomeCounts(result: unknown): Record<string, number> | undefined; /** The latest local start of the window at or before `now`. */ export declare function windowStartAtOrBefore(config: DreamingConfig, now: Date): Date; /** The first local start of the window after `now`. */ export declare function nextWindowStart(config: DreamingConfig, now: Date): Date; export interface DreamEligibilityInput { config: DreamingConfig; now: Date; lastRunAt?: Date; /** Live sessions in this project only. Undefined when the caller cannot see sessions (CLI status). */ sessions?: DreamSession[]; /** For the idle cadence: when the Hook last saw a live session in this project. */ lastSeenAt?: Date; } export interface DreamEligibility { eligible: boolean; /** Plain-English why (not). */ reason: string; /** When a dream could next start, when that can be known. */ nextEligibleAt?: Date; } /** Whether a scheduled dream may start for one project now. Pure; tested directly. */ export declare function dreamEligibility(input: DreamEligibilityInput): DreamEligibility; /** What `phren dream status` and `dream_status` report for one project. */ export interface DreamStatus { project: string; enabled: boolean; mode: DreamingConfig["mode"]; cadence: DreamingConfig["cadence"]; window: string; lastRun?: DreamLastRun; /** Null when no scheduled dream will start (off or manual). */ nextEligibleAt: string | null; reason: string; lastOutcome?: Record<string, number>; /** The helper account and model a dream would use, or why there is none. */ runsOn?: string; pendingProposals: number; acceptance: DreamAcceptance; conflicts: DreamDecisionConflict[]; } /** * The status of one project from config and the last-run marker. Live sessions * are not consulted (that needs the Hook), so "eligible now" means as far as * timing goes; the Hook still waits for working agents. */ export declare function dreamStatus(phrenPath: string, project: string, now?: Date, limit?: number): DreamStatus; export interface DreamSchedulerDeps { now: () => Date; /** Project names in the store the Hook serves. */ projects: () => string[]; config: (project: string) => DreamingConfig; lastRun: (project: string) => DreamLastRun | undefined; /** This computer's live agent sessions with their project. Rejects when unknown; the tick is then skipped. */ liveSessions: () => Promise<DreamSession[]>; /** Start one dream and resolve when it ends. */ run: (project: string) => Promise<"busy" | void>; } export declare class DreamScheduler { private readonly deps; private ticking; /** When a live session was last seen in each project (for the idle cadence). */ private readonly lastSeen; /** When the ticker last started a dream for each project, so a failing run is not retried every tick. */ private readonly lastAttempt; constructor(deps: DreamSchedulerDeps); private observe; private decide; /** Projects whose dream would start now, given these sessions. Exposed for tests and diagnostics. */ due(sessions: DreamSession[], now?: Date): string[]; tick(): Promise<string[]>; } /** * The agent panes of one terminal snapshot (the Hook's `{ tabs, panes }` * shape) as dream sessions: the project from the pane's folder, the status * from the pane or, when the terminal reports none per pane, its tab. */ export declare function dreamSessionsFromSnapshot(snapshot: unknown, projectOf: (cwd: string | undefined) => string | undefined): DreamSession[]; /** * A pane folder's project: the registered project whose checkout (or a * worktree of it) holds the folder, else the folder's name, so a session is * never missed when deciding that nobody is working. */ export declare function dreamProjectOf(store: string, profile?: string): (cwd: string | undefined) => string | undefined; /** Projects in the store the Hook serves, skipping `global`. */ export declare function dreamStoreProjects(store: string, profile?: string): string[]; /** Off with `PHREN_DREAMING=off`; otherwise the ticker runs and each project's config decides. */ export declare function dreamTickerEnabled(env?: NodeJS.ProcessEnv): boolean; /** The command that runs a dream; `PHREN_DREAM_COMMAND` points it at a specific phren binary. */ export declare function dreamCommand(project: string, env?: NodeJS.ProcessEnv): { command: string; args: string[]; }; /** Run one scheduled dream as a low-priority child process of the Hook. */ export declare function runDreamChild(store: string, project: string, env?: NodeJS.ProcessEnv): Promise<"busy" | void>; /** Resolve the same evidence used by the live overview before deciding a project is idle. */ export declare function effectiveDreamSessions(server: string, snapshot: unknown, projectOf: (cwd: string | undefined) => string | undefined, hooks?: AgentHooks, questions?: CodexQuestions): Promise<DreamSession[]>;