@awebai/oats 0.56.0 → 0.57.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/bin/oats.mjs +18 -10
  2. package/docs/configuration.md +17 -27
  3. package/docs/desktop-cli-api.md +59 -3
  4. package/docs/desktop.md +25 -4
  5. package/docs/execution-targets.md +6 -1
  6. package/docs/implementation.md +106 -5
  7. package/docs/integrations.md +8 -4
  8. package/docs/knowledge.md +93 -38
  9. package/docs/official-catalog.md +3 -3
  10. package/docs/packages.md +12 -12
  11. package/docs/release-notes/v0.57.0.md +283 -0
  12. package/docs/release-notes/v0.57.1.md +275 -0
  13. package/docs/schedules.md +8 -3
  14. package/docs/servers.md +18 -0
  15. package/docs/souls-and-instances.md +126 -15
  16. package/docs/tui.md +333 -95
  17. package/docs/workspaces.md +2 -2
  18. package/lib/core.mjs +439 -126
  19. package/lib/instance-lifecycle.mjs +45 -9
  20. package/lib/session-viewer.mjs +29 -4
  21. package/lib/worktree.mjs +23 -2
  22. package/package-catalog.json +2 -2
  23. package/package.json +1 -1
  24. package/packages/client/cli-adapter.mjs +9 -7
  25. package/packages/client/cli-locator.mjs +2 -2
  26. package/packages/client/composed-gate.mjs +34 -0
  27. package/packages/client/inspect-boundary.mjs +157 -0
  28. package/packages/client/inspect-cache.mjs +86 -0
  29. package/packages/client/instance-lifecycle.mjs +10 -3
  30. package/packages/client/keyed-catalog.mjs +132 -0
  31. package/packages/client/launch-prompt-outcome.mjs +1 -1
  32. package/packages/client/launch-view.mjs +46 -0
  33. package/packages/client/session-attach.mjs +7 -5
  34. package/packages/client/soul-catalog.mjs +77 -0
  35. package/packages/client/soul-colors.mjs +31 -0
  36. package/packages/client/soul-facts.mjs +85 -0
  37. package/packages/client/soul-rows.mjs +48 -0
  38. package/packages/client/spawn-apply-cli.mjs +11 -6
  39. package/packages/client/spawn-apply.mjs +212 -0
  40. package/packages/client/spawn-messages.mjs +131 -85
  41. package/packages/client/spawn-preview-contract.mjs +3 -0
  42. package/packages/client/spawn-preview-facts.mjs +119 -0
  43. package/packages/client/spawn-preview.mjs +152 -0
  44. package/packages/client/spawn-transaction.mjs +136 -0
  45. package/packages/tui/README.md +43 -13
  46. package/packages/tui/lib/actions.mjs +18 -2
  47. package/packages/tui/lib/client.mjs +4 -0
  48. package/packages/tui/lib/config.mjs +9 -14
  49. package/packages/tui/lib/main.mjs +241 -55
  50. package/packages/tui/lib/model.mjs +230 -29
  51. package/packages/tui/lib/panel.mjs +955 -0
  52. package/packages/tui/lib/read.mjs +134 -24
  53. package/packages/tui/lib/store.mjs +184 -31
  54. package/packages/tui/lib/term/capture.mjs +260 -0
  55. package/packages/tui/lib/term/screen.mjs +44 -8
  56. package/packages/tui/lib/term/style.mjs +78 -6
  57. package/packages/tui/lib/term/text.mjs +24 -4
  58. package/packages/tui/lib/term/through.mjs +213 -0
  59. package/packages/tui/lib/term/tty.mjs +7 -60
  60. package/packages/tui/lib/views/act.mjs +85 -0
  61. package/packages/tui/lib/views/frame.mjs +153 -37
  62. package/packages/tui/lib/views/help.mjs +22 -14
  63. package/skills/oats-getting-started/SKILL.md +4 -3
  64. package/packages/tui/lib/open.mjs +0 -45
@@ -10,7 +10,8 @@ import {
10
10
  hasInstanceChildren, heldHome, rosterSections, deploymentState, rowStale, primaryDeployment, isMultiDeployment, runtimeState, runtimeCounts,
11
11
  waitingClaim, waitingLabel, waitedText, waitingClock, waitingBelowText, instanceActs,
12
12
  identityMemory, viewModel, workspaceChoices, workspaceChoiceRows, choiceMatches, UNMATCHED_SECTION, shortPath,
13
- openAct, deployments as heldDeployments, detachKeySupported, hostDetachKeySupported, attachExit, preflightFailure, kernelLines,
13
+ openAct, deployments as heldDeployments, attachExit, preflightFailure, kernelLines,
14
+ startGate, launchFlagIgnored, flagIgnoredNotice, retainedStartMessage,
14
15
  } from "./client.mjs";
15
16
  import { shownLines } from "./term/text.mjs";
16
17
  import { textWidth } from "./term/width.mjs";
@@ -156,13 +157,7 @@ function build(state, at) {
156
157
  // Whether a list stands under the header: not for a deployment of this machine that was never
157
158
  // observed, nor for a machine that is not live and has no row kept. Each has only its reason.
158
159
  const observed = panel !== null && !gone && (local ? status === "observed" : live || instances.length > 0);
159
- // Whether each other machine's OATS has the key that leaves a viewer, by the deployment its rows
160
- // are tagged with (the shared panel's `row.deployment.id`; the TUI builds no deployment id).
161
- // Worked out here, once for what the roster is made from, so a paint reads no machine again.
162
- const opens = new Map();
163
- if (panel !== null) for (const d of heldDeployments(heldOf(state, at))) if (d.remote) opens.set(d.id, hostDetachKeySupported(state.cli, d.group));
164
160
  return {
165
- opens,
166
161
  panel, gone, instances, deployments, multi, parts, rows, filtering, stale, section, live, observed,
167
162
  below: waitingRollup(instances, state.collapsed, state.open, { filtering, stale, section }),
168
163
  status,
@@ -286,13 +281,18 @@ const reasonSpoken = (reason) => spoken(reason.sentence, reason.origin, reason.o
286
281
  * machine's name), drawn through the display filter on its own. `<id>` and `<home>` in the
287
282
  * command are the TUI's placeholders: nothing of the row is in them. */
288
283
  export const OPEN_WORDS = Object.freeze({
289
- left: (name) => [{ own: "You left " }, { shown: name }, { own: ". It keeps running." }],
290
284
  failed: (name) => [{ shown: name }, { own: " could not be opened." }],
291
285
  link: (machine) => [{ own: "The connection to " }, { shown: machine }, { own: " ended." }],
292
286
  unknown: (name) => [{ own: "The viewer of " }, { shown: name }, { own: " ended without saying why." }],
287
+ noTmux: (name) => [{ shown: name }, { own: " could not be opened: the terminal panel needs tmux 3.2 or later on this machine." }],
293
288
  notRunning: (name) => [{ shown: name }, { own: " is not running, so it has no terminal to open." }],
294
289
  stateUnknown: (name) => [{ own: "Whether " }, { shown: name }, { own: " runs is not known, so it is not opened." }],
295
- noKey: (machine) => [machine === null ? { own: "This machine" } : { shown: machine }, { own: "'s OATS has no key to leave a viewer (it needs OATS 0.51.0 or later), so it is not opened from here. `oats session attach --server <id> --home <home>` opens it, and only closing the terminal leaves it." }],
290
+ });
291
+ /** The line of a view that is not in this version (its key: 2, 3), with what to run instead: the
292
+ * help's commands (views/help.mjs VIEWS), in the TUI's own words. */
293
+ export const NOT_BUILT = Object.freeze({
294
+ souls: Object.freeze({ said: Object.freeze([{ own: "Souls are not in this version: `oats souls` lists them, and `oats spawn <soul>` makes an instance of one." }]), under: Object.freeze([]) }),
295
+ capabilities: Object.freeze({ said: Object.freeze([{ own: "Capabilities are not in this version: `oats capabilities` lists them." }]), under: Object.freeze([]) }),
296
296
  });
297
297
  /** A sentence from outside (a reason of the shared home's, the preflight table's), as a text
298
298
  * under the line: drawn as the detail draws a reason, wrapped on the reason's rows, never as one
@@ -309,7 +309,8 @@ const elsewhere = (row) => typeof row.server === "string" && row.server !== "";
309
309
  * for a row of another machine (its preflight runs first)
310
310
  * { open: false, said: { said, under } } opens nothing, and this says why
311
311
  * Whether a row can be opened is the shared openAct's, switched on its reason's code, never on a
312
- * sentence; the TUI adds only the two feature gates on top of an enabled answer. */
312
+ * sentence. The viewer is started with no key of its own (the TUI takes the back key before the
313
+ * pane sees it), so a kernel or a host of any version is opened alike. */
313
314
  export function openDecision(state, row, acts) {
314
315
  const name = row.instance, id = instanceId(row);
315
316
  const machine = elsewhere(row) ? row.deployment?.machine ?? row.server : null;
@@ -324,23 +325,25 @@ export function openDecision(state, row, acts) {
324
325
  if (act.reason.code === "state-unknown") return no(OPEN_WORDS.stateUnknown(name));
325
326
  return no([], outside(reasonSpoken(act.reason)));
326
327
  }
327
- if (!elsewhere(row)) {
328
- return detachKeySupported(state.cli) ? { open: true, id, name, machine, target: { home: row.home }, routed: false } : no(OPEN_WORDS.noKey(null));
329
- }
330
- if (roster(state).opens.get(row.deployment?.id) !== true) return no(OPEN_WORDS.noKey(machine));
328
+ if (!elsewhere(row)) return { open: true, id, name, machine, target: { home: row.home }, routed: false };
331
329
  return { open: true, id, name, machine, target: { serverId: row.server, instance: row.instance, home: row.home }, routed: true };
332
330
  }
333
331
 
334
- /** The line after the viewer returned, or null when the TUI says nothing of its own (the viewer
335
- * ended without the key: the list shows the agent as the kernel now reports it). `status` is the
336
- * child's exit status as Node reports it; the shared attachExit reads it, the TUI compares no
337
- * number. What the viewer printed on stderr stands under a failure, once, as one text in
338
- * quotation marks naming nobody: OATS, tmux and ssh all print there, and nothing is compared
339
- * with it. */
340
- export function returnedLine({ name, machine, routed }, status, stderr) {
341
- const word = attachExit(status, { keyed: true, routed });
332
+ /** The line after a terminal closed by itself, or null when the TUI says nothing of its own: the
333
+ * viewer ended with status 0 (the list shows the agent as the kernel now reports it), or the
334
+ * person closed it (`asked`: c, or Enter on another row). `status` is what the pane's shell
335
+ * wrote of the viewer's exit (lib/panel.mjs), null when it wrote none; the shared attachExit reads
336
+ * it, as for a viewer started with no key, and the TUI compares no number. What the viewer printed
337
+ * on stderr stands under a failure, once, as one text in quotation marks naming nobody: OATS, tmux
338
+ * and ssh all print there, and nothing is compared with it. `refused` is the panel's own refusal
339
+ * when no viewer was started at all: "tmux" (no tmux here, or one too old), or "start" (the
340
+ * panel's server or window was not made; `stderr` is then what tmux said). */
341
+ export function returnedLine({ name, machine, routed }, status, stderr, { asked = false, refused = null } = {}) {
342
+ if (asked) return null;
343
+ if (refused === "tmux") return { said: OPEN_WORDS.noTmux(name), under: [] };
344
+ if (refused !== null) status = 1;
345
+ const word = attachExit(status, { keyed: false, routed });
342
346
  if (word === "ended") return null;
343
- if (word === "left") return { said: OPEN_WORDS.left(name), under: [] };
344
347
  if (word === "unknown") return { said: OPEN_WORDS.unknown(name), under: [] };
345
348
  const line = displayLine(stderr);
346
349
  const under = line === null ? [] : [line === DETAIL_WITHHELD ? DETAIL_WITHHELD : `"${line}"`];
@@ -356,6 +359,14 @@ export function preflightLine({ machine }, error) {
356
359
  return { said: [], under: [...outside(failure.sentence), ...(failure.said === null ? [] : kernelLines(failure.said.message, failure.said.fix, TERMS))] };
357
360
  }
358
361
 
362
+ /** The row's own text (its `runtimeError`), in the form that says who made it (see detailOf), or
363
+ * null. `acts` is the row's instanceActs answer: a reason of it that carries no `origin` and is
364
+ * this very text vouches for it. */
365
+ function rowText(row, acts) {
366
+ const vouched = [acts.blocked, acts.limited, ...acts.acts.map((act) => act.reason)].some((reason) => reason && reason.origin === undefined && reason.sentence === row.runtimeError);
367
+ return spoken(row.runtimeError, row.runtimeErrorOrigin, vouched);
368
+ }
369
+
359
370
  /** The facts of one instance, a label and a value per line; a fact that is absent is left out.
360
371
  * `form`: `line` (one row), `wrap` (a sentence, on as many rows as it needs), `end` (read from
361
372
  * its end: a path). A fact with no label continues the one above it. Every value is text the
@@ -387,8 +398,7 @@ function detailOf(state, r, item, now, acts) {
387
398
  // draws the withheld word and nothing else. (The view asks again about the whole value: words
388
399
  // in front never change the answer; a closing quotation mark can, for a text that ends in a
389
400
  // secret's name and its sign, and that text is then withheld.)
390
- const vouched = [acts.blocked, acts.limited, ...acts.acts.map((act) => act.reason)].some((reason) => reason && reason.origin === undefined && reason.sentence === row.runtimeError);
391
- add("", spoken(row.runtimeError, row.runtimeErrorOrigin, vouched), "wrap");
401
+ add("", rowText(row, acts), "wrap");
392
402
  // What holds the row, when that is a sentence of the shared home's and not the row's text: a
393
403
  // reason with an `origin` is the row's text, and so is one whose sentence is that text. Either
394
404
  // stands once, under the state.
@@ -458,10 +468,12 @@ export function model(state, { page, now, at, cols }) {
458
468
  view: state.view, focus: state.focus, ascii: state.ascii, actions: state.actions, filter: state.filter,
459
469
  workspace: r.workspace, machines: r.machines, here: r.here, machine: r.machine, counts: r.counts, readAt: r.readAt, notice: r.notice, note: r.note, empty: r.empty, observed: r.observed,
460
470
  detailTop: state.detailTop, helpTop: state.helpTop, open: state.open !== null,
461
- detachKey: state.detachKey, opening: state.opening, outcome: state.outcome, openable: false,
471
+ detachKey: state.detachKey, opening: state.opening, outcome: state.outcome, openable: false, actable: [],
472
+ terminal: state.terminal, onTerminal: false,
473
+ act: state.view === "act" && state.act ? actView(state) : null,
462
474
  };
463
- // The view under the help decides what the header says.
464
- const under = state.view === "help" ? state.back : state.view;
475
+ // The view under the help, or under a confirmation, decides what the header says.
476
+ const under = state.view === "help" || state.view === "act" ? state.back : state.view;
465
477
  if (under === "switcher") {
466
478
  const s = switcher(state, at);
467
479
  const at_ = s.selected === null ? -1 : s.items.findIndex((item) => item.key === s.selected);
@@ -490,8 +502,197 @@ export function model(state, { page, now, at, cols }) {
490
502
  list: r.items.slice(win.top, win.top + win.size).map((item) => (item?.id !== undefined ? { kind: "row", ...listRow(state, r, item) } : item)),
491
503
  above: win.above, below: win.below, wordWidth: r.wordWidth,
492
504
  detail: selected >= 0 ? detailOf(state, r, r.items[selected], now, acts) : null,
493
- // Enter opens the selected agent's terminal now: the footer names the key that returns.
505
+ // Enter opens the selected agent's terminal now, or (`onTerminal`) goes to the one open on it.
494
506
  openable: decision?.open === true && state.opening === null,
507
+ onTerminal: row !== null && state.terminal !== null && state.terminal.id === instanceId(row),
508
+ // The acts whose key opens a confirmation now, read from the same instanceActs answer: the
509
+ // footer names them. A key not named still answers, with the line that says why.
510
+ actable: row && state.opening === null ? ACT_VERBS.filter((verb) => actDecision(state, row, acts, verb).open) : [],
495
511
  switcher: null,
496
512
  };
497
513
  }
514
+
515
+ /** The shared home's rows of deployment `id` as the state holds them, unfiltered: the rows the list
516
+ * draws for it, found by the deployment and never by the view that is open. */
517
+ const rowsOf = (held, id) => panelData(held, id, TERMS).instances.filter((row) => row.deployment?.id === id);
518
+ /** Where a routed command runs from (its `localCwd`): the open deployment when it is this
519
+ * machine's, else the first served, else the home. */
520
+ export const actCwd = (state) => (state.ctxs.includes(state.open) ? state.open : state.ctxs[0] ?? state.home);
521
+ /** What the lifecycle boundary admits a request of deployment `id` against (its `getContext()`),
522
+ * asked at every admission: the deployment as the shared `deployments` lists it (a machine's with
523
+ * its roster group), the kernel record, `actCwd`, and that deployment's rows now. */
524
+ export function actContext(state, id, at) {
525
+ const held = heldOf(state, at);
526
+ const workspace = heldDeployments(held).find((w) => w.id === id) ?? null;
527
+ return { workspace, cli: state.cli, localCwd: actCwd(state), instances: workspace ? rowsOf(held, id) : [] };
528
+ }
529
+ /** This machine's deployments and their rows, for the shared start rule's home check
530
+ * (session-start.mjs verifiedLocalHome). */
531
+ export function localDeployments(state, at) {
532
+ const held = heldOf(state, at);
533
+ return heldDeployments(held).filter((w) => !w.remote).map((w) => ({ id: w.id, remote: false, instances: rowsOf(held, w.id) }));
534
+ }
535
+
536
+ /** The four acts on an instance, in the order the help, the footer and the keys know them. */
537
+ export const ACT_VERBS = Object.freeze(["stop", "retire", "start", "restart"]);
538
+ const DONE = Object.freeze({ stop: "stopped", retire: "retired", start: "started", restart: "restarted" });
539
+ /** The TUI's own words around the acts (docs/tui.md, "Stopping, starting, restarting and
540
+ * retiring"), as parts the way OPEN_WORDS are: `{ own }` the TUI's, `{ shown }` a name. A
541
+ * lifecycle sentence the shared home has is never written here: the confirmation's view says it. */
542
+ export const ACT_WORDS = Object.freeze({
543
+ notHere: (name, verb) => [{ shown: name }, { own: ` can't be ${DONE[verb]} here.` }],
544
+ stillStarting: (name, verb) => [{ own: `The ${verb} of ` }, { shown: name }, { own: " is still running." }],
545
+ /** The line on stderr after the TUI has left, one per change still running. */
546
+ leaving: (verb, name) => `oats: the ${verb} of ${name} is still running; \`oats status\` shows its result.`,
547
+ question: (name, verb) => [{ own: verb === "restart" ? "Restart " : "Start " }, { shown: name }, { own: "?" }],
548
+ what: (verb) => (verb === "restart"
549
+ ? "Its session is ended and started again: what it is doing is interrupted. Save any work in progress first."
550
+ : "It starts with the launch settings recorded for it."),
551
+ starting: (name, verb) => [{ own: verb === "restart" ? "Restarting " : "Starting " }, { shown: name }, { own: "…" }],
552
+ started: (name, verb) => [{ shown: name }, { own: ` ${DONE[verb]}.` }],
553
+ unconfirmed: (name) => [{ own: "OATS didn't confirm the start of " }, { shown: name }, { own: ". It may still be running: the list shows it when it is." }],
554
+ waiting: "Esc stops waiting.",
555
+ details: "Details:",
556
+ since: "Started at",
557
+ next: Object.freeze({
558
+ "retire-with-force": (name) => [{ own: "Check the process it names, then retire it with `oats retire " }, { shown: name }, { own: " --force`." }],
559
+ "check-roster": () => [{ own: "The list shows its result when it is done." }],
560
+ }),
561
+ });
562
+ /** The labels of the buttons, the TUI's own, by the confirmation's offer (and the phase it is in). */
563
+ export function buttonLabel(offer, verb, phase) {
564
+ if (offer === "close") {
565
+ if (phase === "reading" || phase === "review") return "Cancel";
566
+ if (phase === "applying" || phase === "starting") return `Close (the ${verb} goes on)`;
567
+ return "Close";
568
+ }
569
+ if (offer === "refresh") return "Review again";
570
+ if (offer === "check-again") return "Check again";
571
+ return { stop: "Stop", retire: "Retire", start: "Start", restart: "Restart" }[verb];
572
+ }
573
+ /** The order the buttons stand in, whatever order the offers come in: the first has the focus. */
574
+ export const BUTTON_ORDER = Object.freeze(["close", "refresh", "check-again", "apply"]);
575
+
576
+ /** Whether an act key on `row` opens a confirmation, and what the line says when it does not.
577
+ * `acts` is the row's instanceActs answer, read once per paint (the footer) or per key, as
578
+ * openDecision reads it; nothing here decides which acts a row has.
579
+ * { open: true, verb, id, name, kind, machine, label, home } a confirmation opens: `kind`
580
+ * "lifecycle" for stop and retire (the shared confirmation), "start" for the TUI's own
581
+ * { open: false, said: { said, under } } nothing opens, and this says why
582
+ * In order: a start or restart of this row from this TUI still running (Done 7); the verb listed
583
+ * and enabled (start and restart also asked of the shared startGate); listed and not enabled
584
+ * (its own reason); not listed (the row's blocked or limited reason, else the TUI's words). */
585
+ export function actDecision(state, row, acts, verb) {
586
+ const name = row.instance, id = instanceId(row);
587
+ const no = (said, under = []) => ({ open: false, said: { said, under } });
588
+ const running = state.starting?.get(id);
589
+ if (running) return no(ACT_WORDS.stillStarting(name, running.verb));
590
+ const act = acts.acts.find((each) => each.verb === verb);
591
+ if (!act) {
592
+ const held = acts.blocked ?? acts.limited;
593
+ return held ? no([], outside(reasonSpoken(held))) : no(ACT_WORDS.notHere(name, verb));
594
+ }
595
+ if (!act.enabled) return no([], outside(reasonSpoken(act.reason)));
596
+ const lifecycle = verb === "stop" || verb === "retire";
597
+ if (!lifecycle) {
598
+ const refused = startGate(state.cli, row, { restart: verb === "restart" }, TERMS);
599
+ // The gate's `detail` is the row's own text: said as the detail says it, with its speaker.
600
+ if (refused) return no([], [refused.sentence, ...outside(refused.detail ? rowText(row, acts) : null)]);
601
+ }
602
+ const machine = elsewhere(row) ? row.deployment?.machine ?? row.server : null;
603
+ return { open: true, verb, id, name, kind: lifecycle ? "lifecycle" : "start", machine, label: machine, home: row.home ?? null };
604
+ }
605
+
606
+ /** What a start's or a restart's answer (the shared startInstance's) shows, as the act view's
607
+ * lines: `{ said }` the TUI's own sentence in parts, `{ text }` a shared sentence or the kernel's
608
+ * line (each already as the shared home words it). Chosen by the answer's fields and codes, never
609
+ * by its words. */
610
+ export function startOutcome(name, verb, answer) {
611
+ const lines = [];
612
+ const kernel = (detail) => (detail ? kernelLines(detail, undefined, TERMS).map((text) => ({ text })) : []);
613
+ if (answer?.ok === true) {
614
+ const fact = launchFlagIgnored(answer.result), notice = fact ? flagIgnoredNotice(name, fact) : null;
615
+ if (notice) return [{ text: notice.headline, look: "bold" }, ...(notice.detail ? notice.detail.split("\n").map((text) => ({ text })) : [])];
616
+ return [{ said: ACT_WORDS.started(name, verb), look: "bold" }];
617
+ }
618
+ if (answer?.outcome === "refused") {
619
+ lines.push({ text: answer.sentence, look: "bold" });
620
+ // A refusal before the CLI ran (the gate, the home check) carries the row's own text, if any.
621
+ if (answer.ran) lines.push(...kernel(answer.detail));
622
+ else if (answer.detail) lines.push({ text: answer.detail });
623
+ return lines;
624
+ }
625
+ // The end of the wait for a start that goes on: the code the shared rule leaves to each client to
626
+ // word. No answer at all (the call itself was refused by the loop) is no more than that either.
627
+ if (!answer || answer.code === "E_START_UNCONFIRMED") return [{ said: ACT_WORDS.unconfirmed(name), look: "bold" }];
628
+ if (answer.incomplete) return [{ text: retainedStartMessage(name, answer.incomplete), look: "bold" }, ...kernel(answer.detail)];
629
+ return [{ text: answer.sentence ?? answer.code, look: "bold" }, ...kernel(answer.detail)];
630
+ }
631
+
632
+ /** An ISO-less clock time, `14:03`, of milliseconds since the epoch, or null. */
633
+ const hourMinute = (ms) => { const at = new Date(ms); return Number.isFinite(ms) && Number.isFinite(at.getTime()) ? `${pad(at.getHours())}:${pad(at.getMinutes())}` : null; };
634
+
635
+ /** The act view's content for the open confirmation (`state.act`), top to bottom, each part only
636
+ * when it has something: { title, lines, buttons }. A line is `{ text, look?, path? }` (a shared
637
+ * sentence or the kernel's text, drawn through the display filter; `path` after it, shortened),
638
+ * `{ said, look? }` (the TUI's own sentence, in parts), `{ pair: [label, value] }` (a state row: both from outside, the plan's label a target's name among them),
639
+ * `{ choice: { id, sentence, value } }` or `{ gap: true }`. `look`: "bold", "dim". */
640
+ export function actView(state) {
641
+ const a = state.act;
642
+ const lines = [];
643
+ const gap = () => { if (lines.length && !lines.at(-1).gap) lines.push({ gap: true }); };
644
+ const short = (path) => shortPath(path, { home: a.machine === null ? state.home : null });
645
+ let title;
646
+ if (a.kind === "start") {
647
+ title = { said: ACT_WORDS.question(a.name, a.verb) };
648
+ if (a.phase === "review") {
649
+ lines.push({ own: ACT_WORDS.what(a.verb) });
650
+ gap();
651
+ if (a.machine !== null) lines.push({ pair: ["machine", a.machine] });
652
+ if (a.home) lines.push({ pair: ["home", short(a.home)] });
653
+ } else if (a.phase === "starting") lines.push({ said: ACT_WORDS.starting(a.name, a.verb) });
654
+ else lines.push(...startOutcome(a.name, a.verb, a.answer));
655
+ } else {
656
+ const view = a.view ?? {}, plan = a.plan;
657
+ title = { text: plan && (a.phase === "review" || view.kind === "reading") ? plan.question : view.title ?? "" };
658
+ if (a.notice?.sentence) lines.push({ text: a.notice.sentence, look: "bold" });
659
+ if (view.kind === "reading" || view.kind === "applying") {
660
+ lines.push({ text: view.message });
661
+ if (view.kind === "reading") lines.push({ own: ACT_WORDS.waiting, look: "dim" });
662
+ }
663
+ if (plan && (a.phase === "review" || view.kind === "reading")) {
664
+ gap();
665
+ for (const h of plan.happening) lines.push({ text: h.text, ...(h.path ? { path: short(h.path) } : {}), ...(h.tone === "attention" ? { look: "bold" } : h.tone === "note" ? { look: "dim" } : {}) });
666
+ if (plan.state.rows.length || plan.state.notes.length) gap();
667
+ for (const row of plan.state.rows) lines.push({ pair: [row.label, row.value] });
668
+ for (const note of plan.state.notes) lines.push({ text: note.text, ...(note.tone === "attention" ? { look: "bold" } : note.tone === "note" ? { look: "dim" } : {}) });
669
+ if (plan.warning) lines.push({ text: plan.warning, look: "bold" });
670
+ const offered = plan.choices.filter((choice) => choice.offered);
671
+ if (offered.length) gap();
672
+ for (const choice of offered) lines.push({ choice: { id: choice.id, sentence: choice.sentence, value: choice.value } });
673
+ if (plan.notice?.sentence) lines.push({ text: plan.notice.sentence, look: "bold" });
674
+ }
675
+ if (a.phase === "settled" && view.status) {
676
+ gap();
677
+ lines.push({ text: view.status.headline, look: "bold" });
678
+ for (const more of view.status.more ?? []) {
679
+ if (more.quote !== undefined) lines.push(...kernelLines(more.quote, undefined, TERMS).map((text) => ({ text })));
680
+ else lines.push({ text: more.text });
681
+ }
682
+ if (view.status.next && ACT_WORDS.next[view.status.next]) lines.push({ said: ACT_WORDS.next[view.status.next](a.name) });
683
+ for (const block of view.blocks ?? []) {
684
+ if (block.type === "details") {
685
+ lines.push({ said: [{ own: `${ACT_WORDS.details} ` }, { shown: block.reason?.code ?? "" }], look: "dim" });
686
+ lines.push(...kernelLines(block.reason?.detail, undefined, TERMS).map((text) => ({ text })));
687
+ } else if (block.type === "quote") lines.push(...kernelLines(block.text, undefined, TERMS).map((text) => ({ text })));
688
+ else if (block.type === "note") lines.push({ text: block.text });
689
+ else if (block.type === "rows") for (const row of block.rows) { lines.push({ text: row.text }); if (row.quote) lines.push(...kernelLines(row.quote, undefined, TERMS).map((text) => ({ text }))); }
690
+ else if (block.type === "lines") for (const line of block.lines) lines.push({ text: line.text, ...(line.path ? { path: short(line.path) } : {}) });
691
+ }
692
+ }
693
+ const since = a.running ? hourMinute(a.running.since) : null;
694
+ if (since) lines.push({ own: `${ACT_WORDS.since} ${since}.`, look: "dim" });
695
+ }
696
+ const buttons = a.offers.map((offer) => ({ id: offer, label: buttonLabel(offer, a.verb, a.phase), focused: offer === a.focus }));
697
+ return { title, lines, buttons, top: a.top };
698
+ }