UNPKG

@phren/cli

Version:

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

490 lines (489 loc) • 27.7 kB
import { createHash } from "node:crypto"; import { lstat, readFile, rm } from "node:fs/promises"; import path from "node:path"; import { z } from "zod"; import { atomicInPrivateDir, BridgeError, bridgeRoot, objects, provider, targetSchema, startingTargetSchema } from "./protocol.js"; import { validateTarget, paneIdentity, paneChatState, paneStartingToken, clearStartingToken, clearStartingTokenByToken } from "./herdr.js"; import { terminalProvider } from "./terminal.js"; import { claudeComposerState, readClaudeComposerScreen } from "./claude-suggestion.js"; import { briefId } from "./launch-brief.js"; import { paneTurn, workerStates, workerTurnKey } from "./dispatch-returns.js"; import { dispatchReceipt, dispatchStatus, originPaneSchema, scanStartingToken, settleCancelledReservation, startingTargetFromPane, updateReceipt } from "./dispatch.js"; import { idempotencyKeySchema, withIdempotencyKeyLock } from "./dispatch-idempotency.js"; import { dispatchClonePaths, removeDispatchClone, removeDispatchSecrets } from "./dispatch-clone.js"; import { launchInProgress, removeWorkerFile, terminateWorker, workerAlive, workerPid } from "./worker-identity.js"; const closedTargetSchema = z.union([targetSchema, startingTargetSchema]); const file = (target) => path.join(bridgeRoot(), "closed-workers", `${createHash("sha256").update(JSON.stringify([target.server, target.workspace, target.tab, target.pane, target.source, "session" in target ? target.session : target.startingToken])).digest("hex")}.json`); export async function markWorkerClosed(target, terminal) { await atomicInPrivateDir(file(target), { target, terminal, at: new Date().toISOString() }); } export async function intentionallyClosed(target) { try { const info = await lstat(file(target)); if (!info.isFile() || info.isSymbolicLink() || info.size > 4096) return false; const row = JSON.parse(await readFile(file(target), "utf8")); return closedTargetSchema.safeParse(row.target).success; } catch (error) { if (error.code !== "ENOENT") throw error; return false; } } export async function markPaneClosed(server, pane) { const chat = await paneChatState(server, pane); const target = closedTargetSchema.safeParse({ server, 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 (target.success) await markWorkerClosed(target.data, String(pane.terminal_id)); // A dispatch may still hold the launch token before its first identity poll. const starting = startingTargetSchema.safeParse({ server, workspace: pane.workspace_id, tab: pane.tab_id, pane: pane.pane_id, source: pane.agent, starting: true, startingToken: chat.startingToken }); if (starting.success) await markWorkerClosed(starting.data, String(pane.terminal_id)); } /** The exact recorded binding: the session id, or a starting target's starting token. */ export function paneBinding(target, pane, chat) { if (!pane || pane.agent !== target.source) return "different"; const paneSession = typeof pane.sessionId === "string" ? pane.sessionId : typeof pane.session_id === "string" ? pane.session_id : undefined; if ("session" in target) { const actual = paneSession ?? (typeof chat.sessionId === "string" ? chat.sessionId : undefined); if (!actual) return "unknown"; return actual === target.session ? "match" : "different"; } const token = typeof pane.startingToken === "string" ? pane.startingToken : typeof chat.startingToken === "string" ? chat.startingToken : undefined; if (!token) return "unknown"; return token === target.startingToken ? "match" : "different"; } export function paneRunsTarget(target, pane, chat) { return paneBinding(target, pane, chat) === "match"; } async function retainCancelled(dispatchId, duringPlacement, paths) { await finalizeCancelled(dispatchId, duringPlacement); await removeDispatchSecrets(paths); await updateReceipt(dispatchId, value => { const ended = value.endedAt ?? value.closedAt ?? new Date().toISOString(); if (!value.endedAt) value.endedAt = ended; if (!value.retainedUntil) value.retainedUntil = new Date(Date.parse(ended) + 7 * 24 * 60 * 60_000).toISOString(); return true; }).catch(() => undefined); } async function finalizeCancelled(dispatchId, duringPlacement = false) { const at = new Date().toISOString(); let cancelled = false; await updateReceipt(dispatchId, value => { if (value.closedAt) return false; value.closedAt = at; value.state = "failed"; value.error = "cancelled"; value.returned = { state: "failed", at, error: "cancelled", read: false }; cancelled = true; return true; }, { duringPlacement }); return cancelled; } /** * Cancel a running dispatch by id or idempotency key. The intent is recorded * first; the pane is closed only while it still runs the recorded session, and * the terminal `cancelled` return is written only after the pane is confirmed * gone. A close that cannot be confirmed leaves the intent for a later retry. */ export async function cancelDispatch(input) { const data = z.union([ z.object({ dispatch: z.string().uuid() }).strict(), z.object({ idempotencyKey: idempotencyKeySchema }).strict(), ]).parse(input); let dispatchId; let record = undefined; let placement = false; let receipt; const mark = (value, at) => { placement = value.state === "launching" || value.state === "sending"; if (value.cancelRequestedAt) return false; value.cancelRequestedAt = at; return true; }; if ("dispatch" in data) { dispatchId = data.dispatch; receipt = await updateReceipt(dispatchId, value => mark(value, new Date().toISOString()), { duringPlacement: true }); } else { // The key lock serializes this marker with first-receipt creation. await withIdempotencyKeyLock(data.idempotencyKey, async (current, save) => { record = current; dispatchId = current?.dispatchId; if (!dispatchId) return; const at = new Date().toISOString(); const updated = await updateReceipt(dispatchId, value => mark(value, at), { duringPlacement: true }); if (updated) receipt = updated; else if (current && !current.cancelRequestedAt) { current.cancelRequestedAt = at; await save(current); } }); } // An unknown key names no dispatch; a known one whose receipt is gone still // answers with the id it named. if (!dispatchId) return { ok: true, closed: false }; if (!receipt) { // Assigned inside the lock callback, so read it through its declared type. const reserved = record; if (reserved && (reserved.phase === "reserved" || reserved.phase === "launching")) { if (await launchInProgress(reserved.dispatchId)) return { ok: true, dispatch: dispatchId, closed: false, pending: true }; await settleCancelledReservation(reserved); return { ok: true, dispatch: dispatchId, closed: false }; } return { ok: true, dispatch: dispatchId, closed: false }; } if (receipt.closedAt) return { ok: true, dispatch: dispatchId, closed: false }; // The launcher sees cancelRequestedAt and closes the pane it just created; // this call reports the intent as pending. if (placement) return { ok: true, dispatch: dispatchId, closed: false, pending: true }; return closeVerifiedWorker(receipt); } /** Close the recorded pane only after the worker is gone and the pane is just its shell. */ async function closeIdlePane(server, target) { try { const snapshot = await terminalProvider().snapshot(server); const pane = objects(snapshot.panes).find(p => p.pane_id === target.pane && p.tab_id === target.tab && p.workspace_id === target.workspace); if (!pane || typeof pane.pane_id !== "string") return; const info = await terminalProvider().processes(server, pane.pane_id).catch(() => undefined); // A replacement session in this pane is running something other than the // shell; never close it. Only a bare shell prompt is safe to close. if (!info || info.shellPid === undefined || info.foregroundPids.length === 0) return; if (!info.foregroundPids.every(pid => pid === info.shellPid)) return; await terminalProvider().closePane(server, pane.pane_id).catch(() => undefined); } catch { /* leave the pane */ } } /** A keyed worker is closed by its durable process identity, never by a pane's inherited token. */ async function closeKeyedWorker(receipt, duringPlacement, cleanupPaths) { const dispatchId = receipt.id; const finalize = async (closed) => { await retainCancelled(dispatchId, duringPlacement, cleanupPaths); await removeWorkerFile(dispatchId); clearStartingTokenByToken(receipt.startingToken ?? ""); return { ok: true, dispatch: dispatchId, closed, ...(closed ? {} : { gone: true }) }; }; const alive = await workerAlive(dispatchId, receipt.workerFile, receipt.submittedAt); if (alive === "unknown") return { ok: true, dispatch: dispatchId, closed: false, pending: true }; const closePaneIfIdle = async () => { const server = receipt.launchServer ?? receipt.target?.server; if (server && receipt.target) await closeIdlePane(server, receipt.target); }; if (alive === "gone") { await closePaneIfIdle(); return finalize(false); } const worker = await workerPid(dispatchId, receipt.workerFile); if (!worker) return { ok: true, dispatch: dispatchId, closed: false, pending: true }; // Terminate through the recorded process identity first; never signal or // close based on a PID match alone. await terminateWorker(worker.pid, worker.start); if (await workerAlive(dispatchId, receipt.workerFile, receipt.submittedAt) !== "gone") { return { ok: true, dispatch: dispatchId, closed: false, pending: true }; } await closePaneIfIdle(); return finalize(true); } /** The one close path: exact binding, confirmed snapshot, terminal return only after it is gone. */ export async function closeVerifiedWorker(receipt) { const dispatchId = receipt.id; const duringPlacement = receipt.state === "launching" || receipt.state === "sending"; const cleanupPaths = receipt.idempotencyKey ? dispatchClonePaths(receipt.id) : { cloneDir: receipt.cloneDir, gitConfigFile: receipt.gitConfigFile, agentTokenFile: receipt.agentTokenFile }; if (receipt.idempotencyKey) return closeKeyedWorker(receipt, duringPlacement, cleanupPaths); let target = receipt.target; if (!target) { // A launch whose reply was lost may still have started its worker: take // the binding from the worker's own arrival record when there is one. const { briefArrival } = await import("./launch-brief.js"); const arrival = await briefArrival(dispatchId).catch(() => undefined); target = arrival?.accepted?.target ?? arrival?.started?.target; if (target) { receipt.target = target; receipt.launchOutcome = "started"; await updateReceipt(dispatchId, value => { if (!value.target) value.target = target; value.launchOutcome = "started"; return true; }, { duringPlacement: true }).catch(() => undefined); } } if (!target && receipt.launchOutcome === "not-started") { // The launch request was never sent (or was refused before creating // anything): no worker can be running, so finalize without a scan. await retainCancelled(dispatchId, duringPlacement, cleanupPaths); clearStartingTokenByToken(receipt.startingToken ?? ""); return { ok: true, dispatch: dispatchId, closed: false, gone: true }; } if (!target) { // The launch's own starting token is the proof: a successful snapshot // with no pane carrying it, and no recovered session, means no worker. const scan = await scanStartingToken(receipt); if (scan.ok && scan.pane) { const adopted = startingTargetFromPane(receipt, scan.pane); if (adopted) { target = adopted; receipt.target = adopted; receipt.launchOutcome = "started"; await updateReceipt(dispatchId, value => { if (!value.target) value.target = adopted; value.launchOutcome = "started"; return true; }, { duringPlacement: true }).catch(() => undefined); } } else if (scan.ok && !duringPlacement) { await retainCancelled(dispatchId, duringPlacement, cleanupPaths); clearStartingTokenByToken(receipt.startingToken ?? ""); return { ok: true, dispatch: dispatchId, closed: false, gone: true }; } } if (!target) return { ok: true, dispatch: dispatchId, closed: false, pending: true }; const { snapshot, findPane } = await import("./herdr.js"); for (let attempt = 0; attempt < 3; attempt++) { if (attempt) await new Promise(resolve => setTimeout(resolve, attempt === 1 ? 100 : 300)); let before; try { before = await snapshot(target.server); } catch { return { ok: true, dispatch: dispatchId, closed: false, pending: true }; } const pane = findPane(before, target); if (!pane) { // A successful snapshot with no pane is a vanished worker; a failed // snapshot above stays pending instead of claiming success. await retainCancelled(dispatchId, duringPlacement, cleanupPaths); clearStartingTokenByToken(receipt.startingToken ?? ""); return { ok: true, dispatch: dispatchId, closed: false, gone: true }; } const paneSession = typeof pane.sessionId === "string" ? pane.sessionId : typeof pane.session_id === "string" ? pane.session_id : undefined; const paneToken = typeof pane.startingToken === "string" ? pane.startingToken : paneStartingToken(target.server, pane); let chat = {}; if (!paneSession && !paneToken) { try { chat = await paneChatState(target.server, pane, { tokenWhenIdentified: false }); } catch { return { ok: true, dispatch: dispatchId, closed: false, pending: true }; } } const observed = paneToken && typeof pane.startingToken !== "string" ? { ...pane, startingToken: paneToken } : pane; const binding = paneBinding(target, observed, chat); if (binding === "different") { // A replacement session now owns the pane: never close it. return { ok: true, dispatch: dispatchId, closed: false }; } if (binding === "unknown") { // Missing identity evidence: fail closed and retry later. return { ok: true, dispatch: dispatchId, closed: false, pending: true }; } await markPaneClosed(target.server, pane).catch(() => undefined); await terminalProvider().closePane(target.server, target.pane).catch(() => undefined); let after; try { after = await snapshot(target.server); } catch { return { ok: true, dispatch: dispatchId, closed: false, pending: true }; } if (!findPane(after, target)) { await retainCancelled(dispatchId, duringPlacement, cleanupPaths); clearStartingTokenByToken(receipt.startingToken ?? ""); return { ok: true, dispatch: dispatchId, closed: true }; } } // The intent stays recorded; a later close retries it. return { ok: true, dispatch: dispatchId, closed: false, pending: true }; } /** The Hook that launched the worker owns its clone and token: a confirmed close removes both. */ async function releaseClosedFiles(dispatchId) { const paths = dispatchClonePaths(dispatchId); await removeDispatchSecrets(paths); await removeDispatchClone(paths); } async function closeBoundPane(target, pane) { await markWorkerClosed(target, String(pane.terminal_id)); try { await terminalProvider().closePane(target.server, target.pane); } catch (error) { // A refused close must not suppress a later genuine disappearance. const { snapshot, findPane } = await import("./herdr.js"); const remaining = await snapshot(target.server).catch(() => undefined); if (remaining && findPane(remaining, target)?.terminal_id === pane.terminal_id) await rm(file(target), { force: true }); throw error; } } /** Called on the worker's computer only after the sender read the done return. * Recheck this exact ended turn and skip any new work, background work or inbox. */ export async function closeFinishedWorker(input, queue) { const data = z.object({ target: targetSchema, turn: z.string().regex(/^[a-f0-9]{16}$/), dispatch: z.string().uuid() }).strict().parse(input); if (queue) return queue.whenNoPending(data.target, () => closeFinishedWorker(input)); const { snapshot, findPane } = await import("./herdr.js"); if (await intentionallyClosed(data.target) && !findPane(await snapshot(data.target.server), data.target)) return { ok: true, closed: true, replayed: true }; const pane = await validateTarget(data.target, false, true); if (!["idle", "done"].includes(String(pane.agent_status))) return { ok: true, closed: false }; const seen = (await workerStates({ targets: [{ ...data.target, dispatch: data.dispatch }] })).workers[0]; const turn = workerTurnKey(seen); // A turn still working, one that stopped mid-task, waits on CI or a queue, // has background work running, or whose checkout could not be read, keeps its pane. if ((seen.state !== "done" && seen.state !== "idle") || !seen.completed || seen.background || seen.error || seen.unfinished || seen.waiting || seen.unchecked || turn !== data.turn) return { ok: true, closed: false }; // One final fresh binding before the terminal mutation. const current = await validateTarget(data.target, false, true); if (current.terminal_id !== pane.terminal_id || !["idle", "done"].includes(String(current.agent_status))) throw new BridgeError(409, "The worker resumed before it could close."); // The owner can type after the done observation without submitting a new // prompt or changing the Stop record. Check the composer, then the turn in // case the owner submitted a prompt during the screen read. if (data.target.source === "claude" && claudeComposerState(await readClaudeComposerScreen(data.target)) !== "verified-empty") return { ok: true, closed: false }; const record = await paneTurn(data.target.server, current, data.target.source); if ((seen.stopSeq !== undefined || record?.prompt) && (!record?.stop || (record.prompt && record.stop.seq <= record.prompt.seq) || record.terminal !== current.terminal_id || record.session !== data.target.session || workerTurnKey({ endedAt: record.stop.at, stopSeq: record.stop.seq }) !== data.turn)) return { ok: true, closed: false }; await closeBoundPane(data.target, current); // Clean up files only for the dispatch durably bound to this exact pane and // session: a request naming another dispatch must never delete its clone. const owner = await updateReceipt(data.dispatch, () => false).catch(() => undefined); const bound = owner?.target && owner.target.server === data.target.server && owner.target.pane === data.target.pane && owner.target.workspace === data.target.workspace && owner.target.tab === data.target.tab // A starting target has no session yet: the binding can't be proven, so no cleanup. && "session" in owner.target && owner.target.session === data.target.session; if (bound) await releaseClosedFiles(data.dispatch); clearStartingToken(data.target.server, pane); return { ok: true, closed: true }; } /** A close-self settles only on a finished observation of the anchored prompt. */ function settledForClose(own, turn, seen) { if (!own.prompt || !turn?.prompt || turn.terminal !== own.terminal || turn.source !== own.source || turn.session !== own.session || turn.prompt.at !== own.prompt.at || (own.source !== "opencode" && turn.prompt.seq !== own.prompt.seq)) return "cancel"; // OpenCode's synthetic prompt sequence changes at idle; its busy timestamp stays put. if (!turn.stop || turn.stop.seq <= turn.prompt.seq || seen.session !== turn.session || seen.endedAt !== turn.stop.at || seen.stopSeq !== turn.stop.seq) return undefined; if ((seen.state !== "done" && seen.state !== "idle") || !seen.completed || seen.background || seen.error || seen.unchecked) return undefined; return { endedAt: turn.stop.at, stopSeq: turn.stop.seq }; } /** An agent closing the pane it runs in, once its work is committed and reported: * the sanctioned form of `herdr pane close`, which an agent's auto mode refuses. * Only the caller's own pane (named by its terminal variables) is ever closed, * and never on the spot: the reply returns first, and the pane closes only * after the turn that called has ended with nothing still running, so the * report the agent writes after this reply lands before the close. A dispatched * pane also waits for this turn's recorded return; either path stops the * moment the pane is gone, replaced, or has taken new work, and leaves the pane * open past its bound rather than force the close. */ export async function closeOwnPane(input, timing = {}, queue) { const { recheckMs, deadlineMs } = typeof timing === "number" ? { recheckMs: timing } : timing; const data = z.object({ origin: originPaneSchema, dispatch: briefId.optional() }).strict().parse(input); const { snapshot, findPane } = await import("./herdr.js"); const pane = findPane(await snapshot(data.origin.server), data.origin); if (!pane) return { ok: true, closed: false, reason: "The pane is already gone." }; const source = provider.safeParse(pane.agent); const record = source.success ? await paneTurn(data.origin.server, pane, source.data) : undefined; const session = await paneIdentity(data.origin.server, pane, true).catch(() => undefined); const own = record && record.source === pane.agent && record.terminal === pane.terminal_id && (!session || record.session === session) ? record : undefined; const parsed = targetSchema.safeParse({ ...data.origin, source: pane.agent, session: session ?? own?.session }); const unanchored = { ok: true, closed: false, reason: "Pane left open: the conversation and its current prompt could not be verified." }; if (!parsed.success) return unanchored; const target = parsed.data; const terminal = pane.terminal_id; const dispatch = data.dispatch ?? own?.dispatch ?? (await dispatchStatus()).find(value => value.target && paneRunsTarget(value.target, pane, { sessionId: target.session }) && value.target.server === target.server && value.target.workspace === target.workspace && value.target.tab === target.tab && value.target.pane === target.pane)?.id; const deadline = Date.now() + (deadlineMs ?? 600_000); if (dispatch && z.string().uuid().safeParse(dispatch).success) { if (!own?.prompt) return { ok: true, closed: false, pending: true, dispatch, pane: target.pane, reason: "The conductor can close this pane after reading its done return. The Hook has no recorded prompt to recheck locally." }; // Retry for ten minutes, but never close without this turn's recorded return. const timer = setTimeout(() => { void (async () => { if (!findPane(await snapshot(target.server), target)) return; const current = await validateTarget(target, false, true); if (current.terminal_id !== terminal) return; const settled = settledForClose(own, await paneTurn(target.server, current, target.source), (await workerStates({ targets: [target] })).workers[0]); if (settled && settled !== "cancel") { const ended = workerTurnKey(settled); const returned = (await dispatchReceipt(dispatch))?.returned; if (returned?.state === "done" && returned.turn === ended) { if ((await closeFinishedWorker({ target, dispatch, turn: ended }, queue)).closed !== true && Date.now() < deadline) timer.refresh(); return; } } if (settled !== "cancel" && Date.now() < deadline) timer.refresh(); })().catch(() => undefined); }, recheckMs ?? 15_000); timer.unref(); return { ok: true, closed: false, pending: true, dispatch, pane: target.pane, reason: "Close pending on this turn's recorded return; finish this turn. Rechecks every 15 seconds for up to 10 minutes, then leaves the pane open if no return is recorded." }; } if (!own?.prompt) return unanchored; // A lead has no dispatcher to close its pane, so recheck under the same queue guard here. const timer = setTimeout(() => { void (async () => { const seen = (await workerStates({ targets: [target] })).workers[0]; const close = async () => { const current = await validateTarget(target, false, true).catch(() => undefined); if (!current || current.terminal_id !== terminal) return { closed: false, cancelled: true }; const settled = settledForClose(own, await paneTurn(target.server, current, target.source), seen); if (settled === "cancel") return { closed: false, cancelled: true }; if (!settled || !["idle", "done"].includes(String(current.agent_status))) return { closed: false }; await closeBoundPane(target, current); return { closed: true }; }; const result = await (queue ? queue.whenNoPending(target, close) : close()); if (!result.closed && !result.cancelled && Date.now() < deadline) timer.refresh(); })().catch(() => undefined); }, recheckMs ?? 10_000); timer.unref(); return { ok: true, closed: false, pending: true, pane: target.pane, reason: "Close pending. Write your final report and end this turn. The Hook rechecks for up to 10 minutes and closes once the turn, background work and queued hand-offs have cleared." }; }