@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
198 lines (197 loc) • 9.5 kB
JavaScript
import { setTimeout as sleepTimer } from "node:timers/promises";
import { hookRequest } from "./client.js";
import { WATCH_MS, WAIT_UNFINISHED_PREFIX } from "./dispatch-returns.js";
/**
* `phren dispatch wait`: follow one local dispatch to its end. It peeks at the
* Hook's returns (never taking them), filters to this dispatch, and reads the
* worker's live state to tell a gone pane from one still working. The output
* is NDJSON: one line per state change, then one final line. Only the final
* dispatch's own return is acknowledged, so a conductor's returns on the same
* Hook stay unread.
*/
/** How long an id the Hook does not know gets before it is "unknown dispatch". */
export const WAIT_GRACE_MS = 30_000;
/** Continuous Hook unavailability before the wait ends failed. */
export const WAIT_UNAVAILABLE_MS = 5 * 60_000;
/** Longest wait a caller may ask for. */
export const WAIT_MAX_MS = 7 * 24 * 60 * 60 * 1000;
function objects(value) {
return Array.isArray(value) ? value.filter((item) => !!item && typeof item === "object" && !Array.isArray(item)) : [];
}
function object(value) {
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
}
function reason(error) {
const message = error instanceof Error ? error.message.split("\n")[0] : String(error);
return message.replace(/[\x00-\x1f\x7f]/g, " ").slice(0, 200);
}
function questionOf(value) {
if (!value)
return undefined;
if (typeof value.question === "string" && value.question)
return value.question;
const approval = object(value.approval);
for (const key of ["request", "title", "tool"])
if (typeof approval[key] === "string" && approval[key])
return String(approval[key]);
return undefined;
}
/** About 2 s at first, backing off to 10 s, with jitter. */
export function waitDelay(attempt, baseMs = 2_000, jitter = true) {
const target = Math.min(10_000, baseMs * 1.5 ** Math.max(0, attempt - 1));
return Math.max(1, Math.round(target * (jitter ? 0.8 + Math.random() * 0.4 : 1)));
}
export async function waitForDispatch(id, options = {}) {
const request = options.request ?? hookRequest;
const out = options.out ?? ((line) => console.log(JSON.stringify(line)));
const now = options.now ?? Date.now;
const sleep = options.sleep ?? ((ms) => sleepTimer(ms));
const base = options.intervalMs ?? 2_000;
const jitter = options.jitter ?? true;
const grace = options.graceMs ?? WAIT_GRACE_MS;
const unavailableLimit = options.unavailableMs ?? WAIT_UNAVAILABLE_MS;
const timeoutMs = options.timeoutMs === undefined ? undefined : Math.min(Math.max(0, options.timeoutMs), WAIT_MAX_MS);
const started = now();
let lastState;
let unknownSince;
let failingSince;
let lastWorkingAt;
let attempt = 0;
const emit = (state, extra = {}) => {
if (state === lastState)
return;
lastState = state;
out({ dispatch: id, state, ...extra, at: new Date(now()).toISOString() });
};
const final = (state, extra = {}) => out({ dispatch: id, state, final: true, ...extra });
for (;;) {
if (timeoutMs !== undefined && now() - started >= timeoutMs) {
final("timeout");
return 124;
}
let receipt, returned, worker, peeked;
let listError, peekError, workerError;
try {
// The list is bounded to the newest activity; ask for this receipt by
// id. A Hook that predates `id` answers with every receipt.
const list = await request(`/v1/dispatch?id=${encodeURIComponent(id)}`);
receipt = objects(list.dispatches).find(row => row.id === id);
}
catch (error) {
listError = error;
}
try {
const peek = await request("/v1/dispatch/returns?peek=1", {});
peeked = objects(peek.returns).find(row => row.id === id);
}
catch (error) {
peekError = error;
}
if (!listError) {
if (receipt)
unknownSince = undefined;
else if (unknownSince === undefined)
unknownSince = now();
}
const fromReceipt = receipt?.returned ? object(receipt.returned) : undefined;
// A return the conductor already read is still this dispatch's latest;
// use whichever of the peek and the receipt names the newer one.
returned = fromReceipt && String(fromReceipt.at ?? "") > String(peeked?.at ?? "") ? fromReceipt : peeked ?? fromReceipt;
// Live state is read on every poll, even when a return exists: a historical
// needs-you return must not mask a worker that has resumed.
if (receipt?.target) {
try {
const answer = await request("/v1/dispatch/workers", { targets: [{ ...object(receipt.target), dispatch: id }] });
worker = objects(answer.workers)[0];
}
catch (error) {
workerError = error;
}
}
if (listError || peekError || workerError) {
if (failingSince === undefined)
failingSince = now();
else if (now() - failingSince >= unavailableLimit) {
final("failed", { error: `Phren Hook unavailable: ${reason(workerError ?? peekError ?? listError)}` });
return 1;
}
}
else
failingSince = undefined;
if (!listError && !receipt && !returned && unknownSince !== undefined && now() - unknownSince >= grace) {
final("failed", { error: "unknown dispatch" });
return 1;
}
const acknowledge = async () => {
const own = receipt?.returned ? object(receipt.returned) : undefined;
if (!peeked && own?.read !== false)
return;
await request("/v1/dispatch/returns/ack", { dispatch: id }).catch(() => undefined);
};
// A placement that failed before any worker existed has no return of its own.
if (!returned && receipt?.state === "failed") {
final("failed", { ...(typeof receipt.error === "string" ? { error: receipt.error } : {}) });
return 1;
}
const state = typeof returned?.state === "string" ? returned.state : undefined;
if (state === "done") {
final("done", { ...(typeof returned.reply === "string" ? { final_reply: returned.reply } : {}), ...(returned.prs ? { prs: returned.prs } : {}) });
await acknowledge();
return 0;
}
if (state === "failed") {
final("failed", { ...(typeof returned.error === "string" ? { error: returned.error } : {}) });
await acknowledge();
return 1;
}
if (state === "gone") {
final("gone");
await acknowledge();
return 1;
}
const workerState = typeof worker?.state === "string" ? worker.state : typeof object(receipt?.worker).state === "string" ? String(object(receipt?.worker).state) : undefined;
if (workerState === "gone" || workerState === "closed") {
final("gone");
return 1;
}
// A live read can see the worker resume after the Hook stops collecting its returns.
const legacyWait = state === "needs-you" && questionOf(returned)?.startsWith(WAIT_UNFINISHED_PREFIX);
if ((state === "waiting" || legacyWait) && typeof receipt?.createdAt === "string" && now() - Date.parse(receipt.createdAt) >= WATCH_MS) {
final("failed", { error: "The Hook's 24-hour dispatch watch expired after the worker parked." });
return 1;
}
if (workerState === "working")
lastWorkingAt = now();
const returnedAt = Date.parse(String(returned?.at ?? ""));
// A needs-you return from before the worker was last seen working is historical.
const needsYou = (state === "needs-you" || state === "blocked")
&& (lastWorkingAt === undefined || !Number.isFinite(returnedAt) || returnedAt >= lastWorkingAt);
// So is a waiting one: the worker took new work after it parked.
const waitingNow = state === "waiting"
&& (lastWorkingAt === undefined || !Number.isFinite(returnedAt) || returnedAt >= lastWorkingAt);
if (workerState === "working") {
emit("running");
}
else if (waitingNow) {
// Parked on CI, background work or a queue is not finished: say what it
// waits on and keep following, as for needs-you, so `wait && merge`
// never treats a CI wait as done.
const waiting = object(returned);
emit("waiting", { ...(typeof waiting.waitingOn === "string" ? { waitingOn: waiting.waitingOn } : {}),
...(typeof waiting.waitingFor === "string" && waiting.waitingFor ? { waitingFor: waiting.waitingFor } : {}) });
}
else if (needsYou) {
const question = questionOf(returned);
emit("needs_you", question ? { question } : {});
}
else if (workerState === "needs-you" || workerState === "blocked") {
const question = questionOf(worker);
emit("needs_you", question ? { question } : {});
}
else {
emit("running");
}
attempt++;
await sleep(waitDelay(attempt, base, jitter));
}
}