UNPKG

@phren/cli

Version:

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

226 lines (225 loc) • 12.8 kB
import { getRuntimeHealth } from "../governance/policy.js"; import { getMachineName } from "../machine-identity.js"; import { resolveAllStores } from "../store-registry.js"; import { syncConflictSummary } from "../sync/outcome.js"; import { dispatchStatus, updateReceipt } from "./dispatch.js"; import { isLocalComputer } from "./dispatch-hosts.js"; import { DRAFT_REASON, WATCH_MS, workerReaders, workerStates } from "./dispatch-returns.js"; import { paneChatState, recentServers, snapshot } from "./herdr.js"; import { storeSyncAuthFinding } from "./machine-health.js"; import { inboxTargetSchema } from "./owner-inbox.js"; import { hookPeers, peerRequest } from "./peers.js"; import { ownerQuestion } from "./schedule-watch.js"; import { objects, object } from "./protocol.js"; const clean = (text) => String(text).replace(/[\x00-\x1f\x7f]+/g, " ").trim().slice(0, 500); /** Push eligibility for rows from this Hook's reconciliation. Classify each * source prefix here. Peer aggregation is only used to list rows. */ export function isOwnerInboxPushSource(item) { // Persisted by the Hook that accepted the add. Only a row work is stopped on // alerts; the rest wait quietly in the list. if (item.kind === "manual") return item.blocks === true; switch (item.source?.split(":", 1)[0]) { case "dispatch": // The worker's Hook owns the ask. A dispatch row with a remote subject // is a list-only mirror. Local returns omit the subject computer. return item.computer === undefined; case "prompt": // This Hook's own panes and sessions. case "housekeeping": // This Hook's housekeeping and machine-health warnings. case "store-sync": // Conflicts in this computer's checkout. case "store-sync-auth": // Credential failures on this computer. return true; default: // Unclassified sources are list-only. return false; } } export function storeSyncInboxSources(primary, now = Date.now()) { const sources = []; for (const store of resolveAllStores(primary, { migrateAttachments: false })) { if (store.available === false) continue; const auth = storeSyncAuthFinding(store, now); if (auth) sources.push({ source: auth.id, kind: "blocked", computer: getMachineName(), title: clean(`${auth.title}. ${auth.fix}`), }); const count = getRuntimeHealth(store.path).lastSync?.conflictFiles?.length; if (!count) continue; sources.push({ source: `store-sync:${store.id}`, kind: "blocked", computer: getMachineName(), title: clean(`${syncConflictSummary(count)} in ${store.name}. Run phren doctor for the file list.`), }); } return sources; } /** Read the actual panes and pending asks, never a stored inbox's live flag. * Called on reads and on the Hook's five-second activity tick. */ export async function ownerInboxSources(hooks) { const sources = [], unavailableDispatches = []; for (const server of await recentServers()) { const name = String(server.session), state = await snapshot(name); for (const pane of objects(state.panes)) { const chat = await paneChatState(name, pane, { tokenWhenIdentified: false }); const parsed = inboxTargetSchema.safeParse({ server: name, workspace: pane.workspace_id, tab: pane.tab_id, pane: pane.pane_id, source: pane.agent, ...(chat.sessionId ? { session: chat.sessionId } : { starting: true, startingToken: chat.startingToken }) }); if (!parsed.success) continue; const target = parsed.data, waiting = ["blocked", "waiting"].includes(String(pane.agent_status)); if ("session" in target) { // Refresh terminal-derived questions even with no phone chat open. await hooks.syncTerminalDialog(target, waiting); if (!waiting) hooks.clearTerminalPrompt(target); } const approval = "session" in target ? hooks.workerApproval(target) : undefined; const question = "session" in target ? hooks.servedQuestion(target) : undefined; const terminal = waiting && "session" in target ? hooks.terminalPrompt(target) : undefined; const request = approval?.request ?? question?.message ?? terminal?.request ?? terminal?.message ?? (waiting ? `${pane.agent} needs terminal input.` : undefined); if (!request) continue; const actionId = approval?.actionId ?? (typeof question?.actionId === "string" ? question.actionId : undefined); sources.push({ source: `prompt:${JSON.stringify(target)}:${actionId ?? "waiting"}`, kind: "blocked", title: clean(request), target, ...(actionId ? { actionId } : {}) }); } } // A saved return/approval is only a candidate. A worker on this computer is re-observed here, without SSH, so a // closed pane never stays live because its last return waits. This runs on every five-second tick, and an SSH // login per tick per computer (forever, for receipts the returns loop stopped watching) crowded a peer's sshd, so // a worker on another computer is asked about at most once an hour, and only past the returns loop's watch: // within it, its row comes from what that loop last observed and stored on the receipt (`remoteSource`), // refreshed every POLL_MS. const candidates = (await dispatchStatus()).filter(receipt => !receipt.closedAt && receipt.target && receipt.worker?.state !== "gone" && receipt.returned && ["needs-you", "blocked"].includes(receipt.returned.state)); const now = Date.now(); const local = [], aged = new Map(); for (const receipt of candidates) { if (isLocalComputer(receipt.computer)) { local.push(receipt); continue; } if (now - Date.parse(receipt.createdAt) >= WATCH_MS) { aged.set(receipt.computer, [...aged.get(receipt.computer) ?? [], receipt]); continue; } const source = remoteSource(receipt); if (source) sources.push(source); } const answered = async (receipt, seen, computer) => { if (!seen || ["unavailable", "unknown"].includes(String(seen.state))) { unavailableDispatches.push(receipt.id); return; } if (["gone", "closed"].includes(String(seen.state))) { // A closed worker, or one gone after the returns loop stopped watching it, is closed on its receipt so it // stops being a candidate at all, unless its return is still unread: then a conductor taking over is still // handed it (conductor-succession.ts skips closed receipts). if (seen.state === "closed" || (now - Date.parse(receipt.createdAt) >= WATCH_MS && receipt.returned?.read !== false)) await closeReceipt(receipt.id, now); return; } const source = liveSource(receipt, seen, computer); if (!source) return; // The local prompt and dispatch return describe the same wait. Keep // the dispatch's project/receipt metadata as well as its live target. const prompt = computer ? -1 : sources.findIndex(row => JSON.stringify(row.target) === JSON.stringify(source.target) && !row.computer); if (prompt >= 0) sources.splice(prompt, 1); sources.push(source); }; for (let offset = 0; offset < local.length; offset += 64) { const batch = local.slice(offset, offset + 64); const input = { targets: batch.map(receipt => ({ ...receipt.target, dispatch: receipt.id })) }; let workers; try { workers = objects((await workerStates(input, { ...workerReaders(target => hooks.workerApproval(target)), snapshot })).workers); } catch { workers = []; } for (const [index, receipt] of batch.entries()) await answered(receipt, workers[index]); } if (aged.size) { const peers = await hookPeers().catch(() => []); for (const [computer, receipts] of aged) { const seen = await agedStates(computer, receipts, peers.find(peer => peer.name === computer), now); for (const receipt of receipts) await answered(receipt, seen?.get(receipt.id), computer); } } return { sources, unavailableDispatches }; } /** A live worker observation as an inbox row: an approval it waits on, blocked, or stopped with a question. */ function liveSource(receipt, seen, computer) { const approval = object(seen.approval); const actionId = typeof approval.actionId === "string" ? approval.actionId : undefined; // An older peer Hook folded an awaited sentence into its unfinished reason; // a worker parked on CI or a queue is not waiting on the owner. const unfinished = typeof seen.unfinished === "string" && seen.unfinished !== DRAFT_REASON && !seen.unfinished.startsWith("Stopped waiting: ") ? seen.unfinished : undefined; const question = ["idle", "done"].includes(String(seen.state)) && seen.completed && !seen.error && !seen.interrupted ? (typeof seen.reply === "string" ? ownerQuestion(seen.reply) : undefined) ?? unfinished : undefined; if (!actionId && seen.state !== "blocked" && !question) return undefined; return { source: `dispatch:${receipt.id}:${actionId ?? "waiting"}`, kind: actionId || seen.state === "blocked" ? "blocked" : "needs-you", title: clean(approval.request ?? question ?? `${receipt.label} needs input`), project: receipt.project, ...(computer ? { computer } : {}), target: receipt.target, dispatch: receipt.id, ...(actionId ? { actionId } : {}) }; } /** How often the inbox asks another computer about its receipts past the returns loop's watch, failed or not. */ export const AGED_CHECK_MS = 60 * 60 * 1000; const agedChecks = new Map(); /** Forgets the hourly answers (tests). */ export function resetAgedChecks() { agedChecks.clear(); } /** * What another computer says of receipts the returns loop no longer watches: asked once per `AGED_CHECK_MS` per * computer (one request, at most 64 receipts), the answer kept until the next, and a failure waited out as long. * A receipt that aged into the set since keeps no answer until then: hidden, not resolved, and no SSH. */ async function agedStates(computer, receipts, peer, now) { const hit = agedChecks.get(computer); if (hit && now - hit.at < AGED_CHECK_MS) return hit.seen; const entry = { at: now }; agedChecks.set(computer, entry); if (!peer) return undefined; const batch = receipts.slice(0, 64); try { const workers = objects((await peerRequest(peer, "/v1/dispatch/workers", { targets: batch.map(receipt => ({ ...receipt.target, dispatch: receipt.id })) })).workers); entry.seen = new Map(batch.flatMap((receipt, index) => workers[index] ? [[receipt.id, workers[index]]] : [])); } catch { /* waited out until the next check */ } return entry.seen; } /** * A dispatch on another computer, within the returns loop's watch, as that loop last saw it, with no SSH of its * own: waiting on an approval it forwarded (blocked, answerable by its `actionId`), blocked, or stopped with a * question for the owner. A worker the loop saw back at work, or gone, has no row. */ export function remoteSource(receipt) { const { approval, returned } = receipt, state = receipt.worker?.state ?? returned?.state; // A deduped draft updates the worker but leaves the last return in place. const needsInput = state === "needs-you" && returned?.state === "needs-you" && returned.question !== DRAFT_REASON; if (!approval && state !== "blocked" && !needsInput) return undefined; const actionId = approval?.actionId; return { source: `dispatch:${receipt.id}:${actionId ?? "waiting"}`, kind: actionId || state === "blocked" ? "blocked" : "needs-you", title: clean(approval?.request ?? approval?.title ?? returned?.question ?? `${receipt.label} needs input`), project: receipt.project, computer: receipt.computer, target: receipt.target, dispatch: receipt.id, ...(actionId ? { actionId } : {}) }; } /** Records a receipt closed, under the receipt lock, unless something closed it first. */ async function closeReceipt(id, now) { await updateReceipt(id, current => { if (current.closedAt) return false; current.closedAt = new Date(now).toISOString(); return true; }).catch(() => undefined); }