Files
stack/packages/cli/src/format.mjs
T
jason.woltjeandClaude Opus 5.5 79d9fc2d56 feat(cli): full-screen terminal app (#1544, row 55)
Bare `mosaic` on a terminal opens the app. It has Inbox, Tasks, Agents
and Trail screens, help, and a decide overlay, and it uses the alternate
screen, raw mode, resize and SIGTERM, and restores the terminal on exit.
draw(state) in screens.mjs is pure. cli.mjs exports resolveDecision,
which `mosaic decide` and the app share, so both write the same lines
and ask the same question. A decision resolves on `y` only while
decideShown() says the whole decide box is on screen. The app reads that
at the key. A box that doesn't fit is replaced by "Too small to show this
decision", and any key answers No. scripts/mosaic sends no arguments to
packages/cli (a decided scope addition), so the queue dispatch test
follows. The cli README and docs/TOOLS.md describe the app.

Darkwing built it (agents/darkwing/work/queue-55), in three rounds.
Round 3 was approved by Dewey (27308) and Filbert (27309) on manifest
59f1b9b4, 9 paths. Their non-blocking notes go to row 69 (#1562): a
narrow-terminal test for the decideShown MIN_COLS guard, a way out from
the hidden box, a U+18D08 width test, a cut box keeping Esc back, and
the README row count stated as an example.

Sage's gate on 3634d06e, sequential and teed: node suites cli 110 and
queue 150, 0 failed; test-auth 15, test-conductor 17, test-config 24,
test-discord 66, test-extension-package 18, test-foundation 44,
test-queue 27, test-release 14, test-task 98, all 0 failed (Docker on).
122b1223..3634d06e touches none of the 9 paths.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-11 16:06:11 -05:00

98 lines
4.0 KiB
JavaScript

// Plain-text views of broker results. Every function takes what the broker
// returned and keeps its order: the broker sorts, this file never re-sorts.
const one = (s, max = 160) => {
const flat = String(s ?? "").replace(/\s+/g, " ").trim();
return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat;
};
export const shortId = (id) => String(id).slice(0, 8);
export function optionsLine(d) {
return d.options.map((o) => `${o.key} = ${one(o.text, 80)}`).join("; ");
}
// What approving means, when the decision carries authorization context.
export function authorizationLines(d) {
const a = d.authorization;
if (!a) return [];
const lines = [`action: ${a.action}${a.target ? ` on ${a.target}` : ""}`];
lines.push(`choosing "${a.approvalChoice}" authorizes it; any other choice declines`);
return lines;
}
export function formatInbox(list) {
if (list.length === 0) return "inbox: empty\n";
const out = [`inbox: ${list.length} open decision(s)`];
for (const d of list) {
const flags = [d.class, d.blocking ? "blocking" : null].filter(Boolean).join(", ");
out.push("", `${shortId(d.id)} ${d.action} (${flags}) raised ${d.at} by ${d.raised_by_role}`);
out.push(` ${one(d.question, 400)}`);
out.push(` options: ${optionsLine(d)} (recommended: ${d.recommendation})`);
for (const l of authorizationLines(d)) out.push(` ${l}`);
if (d.task_ref) out.push(` task: ${d.task_ref}`);
out.push(` decide: mosaic decide ${shortId(d.id)} <option>`);
}
return `${out.join("\n")}\n`;
}
export function formatDecision(d) {
const out = [
`decision ${d.id}`,
` ${d.class}${d.blocking ? ", blocking" : ""}, raised ${d.at} by ${d.raised_by_role} (${d.raised_by_run})`,
` question: ${d.question}`,
` options: ${optionsLine(d)}`,
` recommended: ${d.recommendation}`,
];
for (const l of authorizationLines(d)) out.push(` ${l}`);
if (d.task_ref) out.push(` task: ${d.task_ref}`);
return `${out.join("\n")}\n`;
}
export function formatAgents(list) {
if (list.length === 0) return "agents: no role is claimed\n";
return `${list.map((c) => `${c.role} ${c.holder_run} ${c.harness ?? "-"} since ${c.at}`).join("\n")}\n`;
}
export function formatTasks(list) {
if (list.length === 0) return "tasks: none\n";
return `${list
.map((t) => {
const f = t.fields ?? {};
const title = f.title ?? f.name ?? "";
const state = f.status ?? f.state ?? (f.done === true ? "done" : f.done === false ? "open" : "");
return [t.task_ref, state, one(title, 100)].filter(Boolean).join(" ");
})
.join("\n")}\n`;
}
export function trailSummary(r) {
switch (r.table) {
case "events":
return [r.kind, r.actor_role ?? null, r.body?.operation ?? null, r.body?.decision ? `decision ${shortId(r.body.decision)}` : null].filter(Boolean).join(" ");
case "decisions":
return `${r.class} ${r.action} → ${r.route_to}${r.blocking ? " (blocking)" : ""}: ${one(r.question, 120)}`;
case "decision_events":
return [r.op, r.choice ? `choice ${r.choice}` : null, `by ${r.by}`, r.via ? `via ${r.via}` : null].filter(Boolean).join(" ");
case "messages":
return `${r.from_role} → ${r.to_role}: ${one(r.body, 120)}`;
case "deliveries":
return [r.op, r.transport, r.holder_run].filter(Boolean).join(" ");
case "role_claims":
return `${r.op} ${r.role} by ${r.holder_run}`;
case "task_snapshots":
return `snapshot ${r.task_ref ?? ""}`.trim();
default:
return "";
}
}
// Rows print in the order the broker returned them (at, table, seq).
export function formatTrail(subject, rows) {
const out = [`trail ${subject}: ${rows.length} row(s)`];
for (const r of rows) out.push(`${r.at} ${r.table}#${r.seq} ${trailSummary(r)}`.trimEnd());
const decision = rows.find((r) => r.table === "decisions" && r.id === subject);
if (decision?.task_ref) out.push("", `task: ${decision.task_ref}`, `follow with: mosaic trail ${decision.task_ref}`);
return `${out.join("\n")}\n`;
}