UNPKG

@phren/cli

Version:

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

227 lines (226 loc) • 12.4 kB
import { createHash } from "node:crypto"; import { lstat, readdir, readFile, rm, stat } from "node:fs/promises"; import path from "node:path"; import { z } from "zod"; import { atomicInPrivateDir, BridgeError, bridgeRoot } from "./protocol.js"; import { terminalProvider } from "./terminal.js"; import { briefArrival, briefId, assertUnusedLaunchId } from "./launch-brief.js"; import { moveToSchema, projectName } from "./dispatch.js"; import { dispatchIssueSchema, writeDispatchIssue } from "./dispatch-issue.js"; export const continuationLaunchSchema = z.object({ workerDispatch: briefId.optional(), worker: z.boolean().default(false), computerId: z.string().uuid(), project: projectName, to: moveToSchema.refine(to => to.computer === undefined, "A receiving launch is local only."), label: z.string().max(200), brief: z.object({ id: z.string().uuid(), text: z.string().min(1).max(32768) }).strict(), issue: dispatchIssueSchema.optional(), }).strict().refine(data => !data.issue || data.worker, "An issue binding belongs to a worker."); const queues = new Map(); const creating = new Map(); const fileFor = (id) => path.join(bridgeRoot(), "move-launches", `${z.string().uuid().parse(id)}.json`); /** A durable intent precedes creation. An interrupted intent is reconciled by * its brief arrival, never by creating another pane. */ export async function continuationPlacement(id, includeCreating = true) { let saved; try { saved = JSON.parse(await readFile(fileFor(id), "utf8")); } catch (error) { if (error.code === "ENOENT") return includeCreating ? creating.get(fileFor(id)) : undefined; throw error; } if (!saved.result) { const arrived = (await briefArrival(id))?.accepted?.target; if (arrived) { saved = { ...saved, state: "placed", result: { target: arrived, briefLaunched: true } }; await atomicInPrivateDir(fileFor(id), JSON.stringify(saved)); } } if (!saved.result && !queues.has(fileFor(id)) && saved.createdAt && Date.now() - saved.createdAt >= 10 * 60_000 && saved.server) { // A unique creation label covers a crash between create() and onPlaced(). // Failure to inspect the terminal keeps the placement uncertain. const panes = await terminalProvider().listPanes(saved.server); if (!panes.some(pane => pane.label === `move-${id}` || saved.paneTarget && pane.pane === saved.paneTarget.pane)) { saved = { ...saved, state: "not-placed" }; await atomicInPrivateDir(fileFor(id), JSON.stringify(saved)); } } return saved; } export async function launchContinuation(input, launch, authorization) { const data = continuationLaunchSchema.parse(input); const file = fileFor(data.brief.id); const hash = createHash("sha256").update(JSON.stringify(data)).digest("hex"); const pending = queues.get(file); if (pending) { await pending.catch(() => undefined); return launchContinuation(data, launch, authorization); } const metadata = { hash, worker: data.worker, ...(data.workerDispatch ? { workerDispatch: data.workerDispatch } : {}), createdAt: Date.now(), ...authorization }; creating.set(file, { ...metadata, state: "uncertain" }); const work = (async () => { const saved = await continuationPlacement(data.brief.id, false); if (saved) { if (JSON.stringify(saved.caller) !== JSON.stringify(authorization?.caller)) throw new BridgeError(403, "This peer is not the launch's recorded counterpart."); if (saved.hash !== hash) throw new BridgeError(409, "This move id already names a different launch."); if (saved.result) return saved.result; throw new BridgeError(409, "This move's launch is uncertain; inspect its existing pane or wait for its arrival. It will not be launched twice."); } await assertUnusedLaunchId(data.brief.id); if (data.issue) await writeDispatchIssue(data.brief.id, data.issue); await atomicInPrivateDir(file, JSON.stringify({ ...metadata, state: "uncertain" })); let paneTarget; const result = await launch(data, async (pane) => { paneTarget = pane; await atomicInPrivateDir(file, JSON.stringify({ ...metadata, state: "uncertain", paneTarget })); }); await atomicInPrivateDir(file, JSON.stringify({ ...metadata, state: "placed", paneTarget, result })); return result; })(); queues.set(file, work); try { return await work; } finally { queues.delete(file); creating.delete(file); } } /** An owner-admitted source move publishes one canonical intent privately. * The receiving Hook pulls it back over its own verified reverse enrollment. */ const intentFile = (id) => path.join(bridgeRoot(), "move-intents", `${z.string().uuid().parse(id)}.json`); export async function saveContinuationIntent(input) { const id = z.string().uuid().parse(input.brief.id); const previous = await readFile(intentFile(id), "utf8").catch(error => { if (error.code !== "ENOENT") throw error; return undefined; }); const encoded = JSON.stringify(input); if (previous && previous !== encoded) throw new BridgeError(409, "This move id already has a different launch intent."); await atomicInPrivateDir(intentFile(id), encoded); } export async function continuationIntent(id) { try { return JSON.parse(await readFile(intentFile(id), "utf8")); } catch { throw new BridgeError(404, "No owner-admitted continuation intent is recorded."); } } /** * The whole conversation a summary-by-target move copies to the receiving Hook, beside its intent: the source's * prompts and replies, already redacted the way a peer hand-off is. The brief only has room for its newest turns; * the receiving launch pulls this through the same intent route, in pages, and puts it in the new agent's brief * folder. A Hook that predates it ignores the page parameter and answers with the intent, which has no * `conversation`, so either side being older leaves the excerpt alone. */ export const CONVERSATION_LIMIT = 2_000_000; const CONVERSATION_PAGE = 262_144; /** Under the 1 MiB a Hook reads of one peer answer (client.ts), escaped as JSON. */ const PAGE_BYTES = 900_000; const conversationFile = (id) => path.join(bridgeRoot(), "move-intents", `${z.string().uuid().parse(id)}.conversation.md`); export const CONVERSATION_FILE = "conversation.md"; /** In a summary-by-target brief's opening line, so the receiving launch asks for a copy only for those moves. */ export const CONVERSATION_MARKER = "Phren copied its conversation instead"; const conversationName = /^([0-9a-f-]{36})\.conversation\.md(\.[0-9a-f-]{36}\.tmp)?$/i; const intentName = /^([0-9a-f-]{36})\.json(\.[0-9a-f-]{36}\.tmp)?$/i; const moveId = z.string().uuid(); /** * Removes the saved conversations and intents no move needs any more, as `done` decides from the move id and the * file's age (the move moved or failed, or it has no record at all), and any older than `maxAgeMs` whatever `done` * says. A destination reads a copy only while its launch runs, and a receiving Hook reads an intent only while its * move runs: both are over once the move is moved or failed. A write a crash left behind * (`<file>.<uuid>.tmp`) goes after an hour. Returns the move ids whose files were removed, once each. */ export async function pruneContinuationFiles(done, now = Date.now(), maxAgeMs = 7 * 24 * 60 * 60 * 1000) { const root = path.join(bridgeRoot(), "move-intents"); const names = await readdir(root).catch(() => []); const removed = new Set(); for (const name of names) { const match = conversationName.exec(name) ?? intentName.exec(name); // A 36-character name that is not a UUID names no move; asking `done` about it would fail the whole sweep. if (!match || !moveId.safeParse(match[1]).success) continue; const file = path.join(root, name); const info = await lstat(file).catch(() => undefined); if (!info?.isFile()) continue; const age = now - info.mtimeMs; const remove = match[2] ? age > ORPHAN_GRACE_MS : age > maxAgeMs || await done(match[1], age); if (remove) await rm(file, { force: true }).then(() => { if (!match[2]) removed.add(match[1]); }, () => undefined); } return [...removed]; } /** How long a file with no move record (or a write a crash left) is kept: a move in another process records itself first. */ export const ORPHAN_GRACE_MS = 60 * 60 * 1000; /** Saved once per move: a run of the move that starts again keeps the first copy, which a destination may be part way through reading. */ export async function saveContinuationConversation(id, text) { if (text.length > CONVERSATION_LIMIT) throw new BridgeError(413, "The conversation is too long to copy."); if (await stat(conversationFile(id)).catch(() => undefined)) return; await atomicInPrivateDir(conversationFile(id), text); } /** One page of the saved conversation from character `offset`, with its whole length and hash to check against. */ export async function continuationConversationPage(id, offset) { let text; try { text = await readFile(conversationFile(id), "utf8"); } catch { throw new BridgeError(404, "No conversation is recorded for this move."); } if (!Number.isInteger(offset) || offset < 0 || offset > text.length) throw new BridgeError(400, "Invalid conversation offset."); let size = Math.min(CONVERSATION_PAGE, text.length - offset); // Escaping can grow text several times; a page always fits one answer and always moves forward. while (size > 1 && Buffer.byteLength(JSON.stringify(text.slice(offset, offset + size))) > PAGE_BYTES) size = Math.floor(size / 2); const end = offset + size; return { conversation: { text: text.slice(offset, end), offset, next: end < text.length ? end : null, total: text.length, sha256: createHash("sha256").update(text).digest("hex") } }; } const pageSchema = z.object({ text: z.string(), offset: z.number().int().nonnegative(), next: z.number().int().positive().nullable(), total: z.number().int().nonnegative().max(CONVERSATION_LIMIT), sha256: z.string().regex(/^[0-9a-f]{64}$/) }); /** * The receiving side: every page of the source's conversation through `page(offset)`, checked against its length * and hash and redacted again here, or undefined when the source has none (an older Hook, a move that never * built one) or it does not arrive whole. Never fails the launch: the brief's excerpt stands on its own. */ export async function fetchContinuationConversation(page, redact) { const parts = []; let offset = 0, total, hash; for (let pages = 0; pages < 64; pages++) { let answer; try { answer = await page(offset); } catch { return undefined; } const parsed = pageSchema.safeParse(answer.conversation); if (!parsed.success || parsed.data.offset !== offset) return undefined; if ((total ??= parsed.data.total) !== parsed.data.total || (hash ??= parsed.data.sha256) !== parsed.data.sha256) return undefined; parts.push(parsed.data.text); offset += parsed.data.text.length; if (parsed.data.next === null) { const text = parts.join(""); if (text.length !== total || createHash("sha256").update(text).digest("hex") !== hash) return undefined; return redact(text); } if (parsed.data.next !== offset || offset > total) return undefined; } return undefined; } /** Where the copied conversation is, to tell the new agent; appended only when the brief has room for it. */ export function withConversationPointer(text, file, limit = 32768) { const pointer = `\n\nThe previous agent's conversation, more of it than the excerpt above, is in ${file}. Read what you need of it, from the end back, before you write your hand-off.`; return text.length + pointer.length <= limit ? `${text}${pointer}` : text; }