fwdloop 0.13.0 → 0.14.0

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.
package/src/panel/data.js CHANGED
@@ -36,6 +36,7 @@ import {
36
36
  import { readSpendRows } from '../provider.js';
37
37
  import { applyRunValues, parseWaitMs, pickRunValues, VALUES_FILE_RE } from '../runvalues.js';
38
38
  import { readSetup } from '../setup.js';
39
+ import { checkedLines, notCheckedBlock, NOT_CHECKED_LABEL } from '../checked.js';
39
40
  import { MAX_STRUCTURE_RETRIES } from '../drafter.js';
40
41
  import {
41
42
  runLiveness, booksFresh, readResumeLock,
@@ -242,7 +243,10 @@ export function signParts(glyph, label, noWord = false) {
242
243
  const word = noWord ? null : (SIGN_WORDS[glyph] ?? null);
243
244
  if (word === null) return { word: null, line: label ?? null };
244
245
  if (typeof label !== 'string' || label === word) return { word, line: null };
245
- return { word, line: label.startsWith(`${word} — `) ? label.slice(word.length + 3) : label };
246
+ if (label.startsWith(`${word} — `)) return { word, line: label.slice(word.length + 3) };
247
+ // amendment 16 I4: the signed words `stopped at the ask of step N` are drawn whole, never as a bold "stopped" + dash + the rest
248
+ if (STOPPED_AT_ASK_START_RE.test(label)) return { word: null, line: label };
249
+ return { word, line: label };
246
250
  }
247
251
 
248
252
  /** The sign an Inbox row wears (null for a past answer, which shows its status word instead). */
@@ -357,16 +361,18 @@ export function glyphPulses(g) {
357
361
  * A saved answer with a live process is `[▶]` working on your answer.
358
362
  * - `[?]` crashed after taking your answer (amendment 2 (f)) — the answer was
359
363
  * consumed, the process is gone, no end row: cannot be carried on.
360
- * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string, resumable?: boolean}} ctx
364
+ * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string, resumable?: boolean, stoppedAtAsk?: string|null}} ctx
361
365
  * @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]'|'[■]', label: string}}
362
366
  */
363
367
  export function computeGlyph({
364
- historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable,
368
+ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk,
365
369
  }) {
366
370
  if (historyRow) {
367
371
  if (historyRow.outcome === 'complete') return { glyph: '[✓]', label: 'passed' };
368
372
  // M4e amendment 4 item 4: a run the human stopped, or one that hit its money cap and can be continued, is `[■]` stopped — never "failed".
369
- if (historyRow.outcome === 'stopped') return { glyph: '[■]', label: 'stopped — after the step that was running; Resume to go on' };
373
+ if (historyRow.outcome === 'stopped') {
374
+ return { glyph: '[■]', label: stoppedAtAsk ? `${stoppedAtAsk}${STOPPED_AT_ASK_START_RE.test(stoppedAtAsk) ? ';' : ' —'} Resume to go on` : 'stopped — after the step that was running; Resume to go on' };
375
+ }
370
376
  if (historyRow.outcome === 'cap-halt' && resumable) return { glyph: '[■]', label: 'stopped — the money cap was reached; raise it and Resume' };
371
377
  // M4b amendment 3: a run the human ended on purpose with rerun is not a failure.
372
378
  if (historyRow.outcome === 'rerun') return { glyph: '[✗]', label: 'stopped by you (rerun), a fresh run was started' };
@@ -897,6 +903,8 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
897
903
  historyRow,
898
904
  // M4e amendment 4 item 4: a cap-halted or stopped run that can be continued (`halt.json` is the runner's record)
899
905
  resumable: halt.ok && historyRow !== null && historyRow.outcome === halt.halt.outcome,
906
+ // amendment 16 I4: how a stopped run was stopped, in the runner's own words (`stopped at the ask of step N`), or null (stopped after a step)
907
+ stoppedAtAsk: lastStoppedAtAsk(runDir, auditRows, historyRow !== null),
900
908
  auditRows,
901
909
  spendRows,
902
910
  logJson,
@@ -915,6 +923,45 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
915
923
  };
916
924
  }
917
925
 
926
+ const STOPPED_AT_ASK_RE = /^stopped at the ask of step \d+$/;
927
+ const STOPPED_AT_ASK_START_RE = /^stopped at the ask of step \d+/;
928
+ /**
929
+ * The ONE reader of the words a stopped ask wears (amendment 19 3): for each ask, oldest-first, the `stopped at the ask of step N`
930
+ * row written for the step `deriveAskStepInfo` pairs it with, or null when the books cannot name that step (never a guess; never
931
+ * by counting those rows in order: a Stop that landed before any ask parked writes one too, with no ask behind it).
932
+ * @param {any[]} ordered `runAsksInOrder`'s asks, oldest-first @param {any[]} audit this run's audit rows
933
+ * @returns {(string|null)[]}
934
+ */
935
+ function stoppedAskWords(ordered, audit) {
936
+ const stepOf = deriveAskStepInfo(ordered, audit, null);
937
+ const atAsk = audit.filter((r) => r.verdict === 'stopped' && STOPPED_AT_ASK_RE.test(String(r.gap)));
938
+ return ordered.map((_, i) => {
939
+ if (stepOf[i].step === null) return null;
940
+ const words = atAsk.filter((r) => r.step === stepOf[i].step).pop()?.gap;
941
+ return typeof words === 'string' ? words : null;
942
+ });
943
+ }
944
+
945
+ /** What the Runs label says of a run stopped at an ask whose step the books cannot name; the Ask tab and the Inbox say the same. */
946
+ const STOPPED_STEP_UNKNOWN = 'stopped';
947
+
948
+ /**
949
+ * How a stopped run was stopped, for its Runs label: the newest `stopped` audit row, when the runner wrote its `gap` as
950
+ * `stopped at the ask of step N` (amendment 13's signed words) -> those words, as the Ask tab's row for that ask reads them
951
+ * (`stoppedAskWords`, the one reader); `stopped` alone when the step cannot be known; null for any other stop (after a step, before step 1).
952
+ * @param {string} runDir @param {any[]} auditRows @param {boolean} hasHistoryRow @returns {string|null}
953
+ */
954
+ function lastStoppedAtAsk(runDir, auditRows, hasHistoryRow) {
955
+ const stopped = auditRows.filter((r) => r.verdict === 'stopped' && typeof r.gap === 'string' && !Number.isInteger(r.attempt));
956
+ const last = stopped.length > 0 ? stopped[stopped.length - 1] : null;
957
+ if (!last || !STOPPED_AT_ASK_RE.test(last.gap)) return null;
958
+ const ordered = runAsksInOrder(runDir, hasHistoryRow);
959
+ const stepOf = deriveAskStepInfo(ordered, auditRows, null);
960
+ const words = stoppedAskWords(ordered, auditRows);
961
+ const i = stepOf.findIndex((x) => x.step !== null && x.step === last.step);
962
+ return i >= 0 && words[i] !== null ? words[i] : STOPPED_STEP_UNKNOWN;
963
+ }
964
+
918
965
  /** @param {string} runDir @returns {string|null} */
919
966
  function lastPidStartedAt(runDir) {
920
967
  const rows = readPidRows(runDir);
@@ -1414,7 +1461,7 @@ export const NO_DRAFT_WORDS = 'no draft record';
1414
1461
  * What the Draft adds up to, ONE place (the Map box, the first step card and the Audit group all read it, so the three can never disagree).
1415
1462
  * M4e amendment 9: `timeMs` is the card row's `at` to the sign row's `at` (the whole drafting, human time included), `null` when either has no
1416
1463
  * usable `at`; never a sum of the model rows' `wallMs`. `calls` sums the rows' `calls`, a row with none counts 1 and sets `callsAtLeast`, so an
1417
- * unknown never shows as a smaller exact number; no model rows = `null`. `humanChecks` = the note rows + the sign row + the run's signed-values
1464
+ * unknown never shows as a smaller exact number; no model rows = `null`. `humanChecks` = the note rows + the revise rows (each is the card the human edited and sent) + the sign row + the run's signed-values
1418
1465
  * rows (the card is not a check), given by the caller as `ctx.humanChecks`. Money follows `costDisplay` ("at least $X" for a floor).
1419
1466
  * @param {any[]} modelRows the draft and change rows (`kind` draft | change) of `setup.jsonl`
1420
1467
  * @param {{cardAt?: any, signAt?: any, humanChecks?: number|null}} [ctx]
@@ -1465,6 +1512,9 @@ export function getDraftBlock(flowDir, runDir) {
1465
1512
  else if (r.kind === 'note') happened.push('your note');
1466
1513
  else if (r.kind === 'draft') happened.push(r.verdict === 'green' ? `drafting${retry(r)}` : 'draft red');
1467
1514
  else if (r.kind === 'change') happened.push(r.verdict === 'red' ? 'change red' : `changing${retry(r)}`);
1515
+ else if (r.kind === 'startover') happened.push(r.verdict === 'green' ? `starting over${retry(r)}` : 'start over red');
1516
+ else if (r.kind === 'revise') happened.push(r.verdict === 'red' ? 'revise red' : `revising${retry(r)}`);
1517
+ else if (r.kind === 'redraft') happened.push(r.verdict === 'red' ? 'redraft red' : `redrafting with your answers${retry(r)}`);
1468
1518
  else if (r.kind === 'sign') happened.push(`signed (${r.signedBy ?? 'you'})`);
1469
1519
  if (r.kind === 'card') {
1470
1520
  const c = r.card && typeof r.card === 'object' ? r.card : {};
@@ -1474,6 +1524,9 @@ export function getDraftBlock(flowDir, runDir) {
1474
1524
  } else if (r.kind === 'note') rows.push(human(r, n, { action: 'note (you)', gap: String(r.text ?? '') }));
1475
1525
  else if (r.kind === 'draft') rows.push(model(r, n, 'draft'));
1476
1526
  else if (r.kind === 'change') rows.push(model(r, n, `change ${r.n}`));
1527
+ else if (r.kind === 'startover') rows.push(model(r, n, 'start over'));
1528
+ else if (r.kind === 'revise') rows.push(model(r, n, `revise ${r.n}`));
1529
+ else if (r.kind === 'redraft') rows.push(model(r, n, `answers redraft ${r.n}`));
1477
1530
  else if (r.kind === 'sign') rows.push(human(r, n, { action: `sign (${r.signedBy ?? 'you'})`, gap: `plan ${String(r.hash ?? '').slice(0, 12)}` }));
1478
1531
  });
1479
1532
  }
@@ -1498,9 +1551,9 @@ export function getDraftBlock(flowDir, runDir) {
1498
1551
  rows.push(human({ at: rec?.at }, rows.length + 1, { action: `${version === 0 ? 'Sign & run' : 'Sign & resume'} (${rec?.signedBy ?? 'you'})`, gap, ...(waits.length ? { gapHead: head, gapWaits: waits } : {}) }));
1499
1552
  }
1500
1553
  const lastSign = setup.present ? [...setup.rows].reverse().find((r) => r.kind === 'sign') : undefined;
1501
- const humanChecks = setup.present ? setup.rows.filter((r) => r.kind === 'note' || r.kind === 'sign').length + versions.length : null;
1554
+ const humanChecks = setup.present ? setup.rows.filter((r) => r.kind === 'note' || r.kind === 'sign' || r.kind === 'revise' || r.kind === 'redraft').length + versions.length : null;
1502
1555
  const totals = setup.present
1503
- ? draftTotals(setup.rows.filter((r) => r.kind === 'draft' || r.kind === 'change'), { cardAt: setup.rows.find((r) => r.kind === 'card')?.at, signAt: lastSign?.at, humanChecks })
1556
+ ? draftTotals(setup.rows.filter((r) => r.kind === 'draft' || r.kind === 'change' || r.kind === 'revise' || r.kind === 'redraft' || r.kind === 'startover'), { cardAt: setup.rows.find((r) => r.kind === 'card')?.at, signAt: lastSign?.at, humanChecks })
1504
1557
  : null;
1505
1558
  // the group the Audit tab draws as a normal card (collapsed by default) and the box the Map and the first step card read
1506
1559
  // `ended` = how the drafting ended: `done` only with a sign row, else `not signed` (no `red` state: a red change shows on line 4 only)
@@ -1547,66 +1600,32 @@ export function getRunAudit({ root, flow, runId }) {
1547
1600
  }
1548
1601
 
1549
1602
  /**
1550
- * `GET /api/runs/:flow/:runId/job` — the Job tab (M4a scope item 2): the
1551
- * signed prose, the arbiter block (cap, asks with TTL, redo cap, sends,
1552
- * sources), and the signature (who, when, hash). Derived purely from the
1553
- * flow's own signed files (`readFlow`) — fwdloop always has exactly one
1554
- * signed `declaration.json` + `signature.json` per flow, so there is no
1555
- * multi-source provenance chain to resolve (unlike bareloop's `getRunJob` —
1556
- * DROPPED per the fit-check). `runId` is only used to confirm the run
1557
- * exists at all; `null` when it doesn't (404).
1558
- * @param {{root: string, flow: string, runId: string, catalogue: any}} opts
1559
- * @returns {any|null}
1560
- */
1561
- /**
1562
- * hamr's 2026-09-27 exit-check review #5: one plain-words sentence for a
1563
- * declared step's own close rule — the Job tab's "Success" field. Derived
1564
- * STRICTLY from `step.close` (and, for the "your accept" wording, whether
1565
- * this step is the one line `arbiter.asks[]` itself binds to) — never a
1566
- * fabricated cite/shape value:
1567
- * - `hitl`: "human check", or "human check (your accept)" when this step's
1568
- * own `fromLine` is a signed ask line (the ask step itself — the one a
1569
- * human pauses a run on, distinct from an ordinary hitl pass-through
1570
- * step with no ask binding).
1571
- * - `softgreen`: "shape: N headings (a / b / c)" when `close.shape.sections`
1572
- * is signed, "max W words" appended when `close.shape.maxWords` is
1573
- * signed, "blocks of L lines" / "must carry: a, b" for the invoice-block
1574
- * shape keys (`linesPerInvoice`/`mustCarry`) — every signed shape key
1575
- * shown, nothing invented; "shape (no shape rules signed)" when a
1576
- * softgreen step signs no `close.shape` at all (declaration.js allows
1577
- * this — an empty/absent shape is not itself a red).
1578
- * - `green`: "cited" — `CLOSE_ALLOWED` (`src/declaration.js`) never lets a
1579
- * step declare WHAT it cites ahead of time (that's the drafted
1580
- * artifact's own per-run field data, not signed prose), so there is
1581
- * never anything honest to append here.
1582
- * - anything else: the raw class name, named as unknown, never silently
1583
- * dropped or guessed into one of the three above; no class at all names
1584
- * that plainly too.
1585
- * @param {{close?: {class?: string, shape?: any}, fromLine?: number|null}} step one `declaration.steps` entry
1586
- * @param {Array<{line:number}>} arbiterAsks `arbiter.asks`, for the "your accept" check
1587
- * @returns {string}
1603
+ * M4e amendment 34: the signed plan as the Job tab shows it. One row per declared step: the job line it came from, what it reads, what it makes, the
1604
+ * primitives it may use (an ask step grants none), its check (the `checkedLines` sentences, the same words the human saw at sign) and, for the step a
1605
+ * signed ask binds to, that ask's wait. "Not checked" comes from the sign row of the flow's setup.jsonl: `recorded:false` (and no items) for a flow
1606
+ * signed before amendment 36, which the page says in words; never an empty list read as "nothing".
1607
+ * @param {any} declaration @param {Array<{line:number, ttlMs?:number}>} asks @param {string} flowDir
1588
1608
  */
1589
- export function deriveStepSuccessText(step, arbiterAsks) {
1590
- const cls = step?.close?.class ?? null;
1591
- if (cls === null) return 'no close class recorded';
1592
- if (cls === 'hitl') {
1593
- const isAskStep = Array.isArray(arbiterAsks) && arbiterAsks.some((a) => a.line === step.fromLine);
1594
- return isAskStep ? 'human check (your accept)' : 'human check';
1595
- }
1596
- if (cls === 'green') return 'cited';
1597
- if (cls === 'softgreen') {
1598
- const shape = step?.close?.shape;
1599
- if (!shape || typeof shape !== 'object') return 'shape (no shape rules signed)';
1600
- const parts = [];
1601
- if (Array.isArray(shape.sections) && shape.sections.length > 0) {
1602
- parts.push(`${shape.sections.length} heading${shape.sections.length === 1 ? '' : 's'} (${shape.sections.join(' / ')})`);
1603
- }
1604
- if (typeof shape.maxWords === 'number') parts.push(`max ${shape.maxWords} words`);
1605
- if (typeof shape.linesPerInvoice === 'number') parts.push(`blocks of ${shape.linesPerInvoice} lines`);
1606
- if (Array.isArray(shape.mustCarry) && shape.mustCarry.length > 0) parts.push(`must carry: ${shape.mustCarry.join(', ')}`);
1607
- return parts.length > 0 ? `shape: ${parts.join(', ')}` : 'shape (no shape rules signed)';
1608
- }
1609
- return `unknown close class "${cls}"`;
1609
+ function buildJobPlan(declaration, asks, flowDir) {
1610
+ const lines = checkedLines(declaration, { hasAsk: asks.length > 0 });
1611
+ const steps = (Array.isArray(declaration?.steps) ? declaration.steps : []).map((st, i) => {
1612
+ const ak = asks.find((x) => x.line === st.fromLine);
1613
+ return {
1614
+ step: i + 1,
1615
+ line: Number.isInteger(st.fromLine) ? st.fromLine : null,
1616
+ reads: Array.isArray(st.reads) ? st.reads : [],
1617
+ makes: typeof st.emits === 'string' ? st.emits : null,
1618
+ mayDo: Array.isArray(st.primitives) ? st.primitives : [],
1619
+ check: lines[i].sentences,
1620
+ checkClass: lines[i].class,
1621
+ ask: !!ak,
1622
+ waitMs: ak && typeof ak.ttlMs === 'number' ? ak.ttlMs : null,
1623
+ };
1624
+ });
1625
+ const setup = readSetup(flowDir);
1626
+ const nc = setup.present ? setup.rows.find((r) => r.kind === 'sign')?.notChecked : undefined;
1627
+ const recorded = !!nc && Array.isArray(nc.items);
1628
+ return { steps, notChecked: { label: NOT_CHECKED_LABEL, items: recorded ? notCheckedBlock(nc.items).items : [], recorded } };
1610
1629
  }
1611
1630
 
1612
1631
  /**
@@ -1617,8 +1636,7 @@ export function deriveStepSuccessText(step, arbiterAsks) {
1617
1636
  * signature.json/prose lines — nothing invented; a piece the books can't
1618
1637
  * name carries its own why, never a blank or a guess. Field order (server
1619
1638
  * order matches the page's own render order): prose, asks, model, cap
1620
- * (+redo cap), sources, sends, guardrails, success (one row per step, off
1621
- * `deriveStepSuccessText`), signature.
1639
+ * (+redo cap), sources, sends, guardrails, plan (one row per step, amendment 34), signature.
1622
1640
  * @param {{root: string, flow: string, runId: string, catalogue: any}} opts
1623
1641
  * @returns {any|null}
1624
1642
  */
@@ -1650,7 +1668,6 @@ export function getRunJob({
1650
1668
  const asks = a.asks ?? [];
1651
1669
  const sends = a.sends ?? [];
1652
1670
  const sources = a.sources ?? [];
1653
- const declSteps = Array.isArray(flowRead.declaration?.steps) ? flowRead.declaration.steps : [];
1654
1671
  return {
1655
1672
  flow,
1656
1673
  runId,
@@ -1684,8 +1701,7 @@ export function getRunJob({
1684
1701
  guardrails: flowRead.lines
1685
1702
  .filter((l) => typeof l.guardrail === 'string' && l.guardrail.length > 0)
1686
1703
  .map((l) => ({ line: l.n, guardrail: l.guardrail })),
1687
- // review #5: "Success" — one row per declared step, in declaration order.
1688
- success: declSteps.map((s) => ({ step: s.emits, text: deriveStepSuccessText(s, asks) })),
1704
+ plan: buildJobPlan(flowRead.declaration, asks, run.flowDir),
1689
1705
  signature: (sig && typeof sig.signedBy === 'string' && typeof sig.signedAt === 'string' && typeof sig.flow === 'string')
1690
1706
  ? { signedBy: sig.signedBy, signedAt: sig.signedAt, hash: sig.flow }
1691
1707
  : null,
@@ -1988,6 +2004,15 @@ function runAsksInOrder(runDir, hasHistoryRow, endedExpired = false) {
1988
2004
  .map(({ row }) => row);
1989
2005
  // M4c-fix amendment 1 (c): a run ended because its ask expired reads expired, never accepted. The late answer the
1990
2006
  // terminal resume consumed was refused, so the newest ask carries `expired` (and says why), not the answer's word.
2007
+ // amendment 17 1A: an ask a Stop set aside takes its words from its OWN place in the run: the step `deriveAskStepInfo` (the one
2008
+ // reader of which step each ask belongs to) names for it, matched to the `stopped at the ask of step N` row written for that step.
2009
+ // Never by counting those rows in order: a Stop that landed before any ask parked writes one too, with no ask behind it.
2010
+ if (ordered.some((a) => a.status === 'stopped')) {
2011
+ const words = stoppedAskWords(ordered, readAudit(runDir));
2012
+ for (let i = 0; i < ordered.length; i++) {
2013
+ if (ordered[i].status === 'stopped' && words[i] !== null) ordered[i] = { ...ordered[i], statusText: words[i] };
2014
+ }
2015
+ }
1991
2016
  const last = ordered[ordered.length - 1];
1992
2017
  if (endedExpired && last && (last.status === 'accepted' || last.status === 'redo' || last.status === 'reran')) {
1993
2018
  ordered[ordered.length - 1] = { ...last, status: 'expired', reason: null, why: LATE_ANSWER_WHY };