fwdloop 0.16.0 → 0.18.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.
Files changed (64) hide show
  1. package/README.md +2 -0
  2. package/bin/fwdloop +10 -7
  3. package/package.json +1 -1
  4. package/src/authoring.js +6 -6
  5. package/src/books.js +6 -1
  6. package/src/catalogue.json +9 -3
  7. package/src/ctx-tools.js +245 -0
  8. package/src/draft-steps.js +89 -30
  9. package/src/drafter.js +28 -4
  10. package/src/draftspend.js +109 -3
  11. package/src/liveness.js +34 -10
  12. package/src/panel/author.js +125 -37
  13. package/src/panel/authorcard.js +15 -10
  14. package/src/panel/authorflows.js +9 -3
  15. package/src/panel/authorresume.js +5 -4
  16. package/src/panel/authorstart.js +21 -9
  17. package/src/panel/authortrigger.js +384 -0
  18. package/src/panel/authorvalues.js +5 -3
  19. package/src/panel/data.js +104 -16
  20. package/src/panel/index.html +257 -44
  21. package/src/panel/scheduler.js +125 -0
  22. package/src/panel/server.js +24 -10
  23. package/src/panel/settings.js +2 -2
  24. package/src/plain-checks.js +51 -0
  25. package/src/primitives.js +34 -3
  26. package/src/provider.js +17 -8
  27. package/src/run-store.js +47 -0
  28. package/src/runner.js +2 -0
  29. package/src/runvalues.js +5 -3
  30. package/src/setup.js +24 -0
  31. package/src/signed-text.js +26 -0
  32. package/src/summary-words.js +155 -0
  33. package/src/trigger-clock.js +142 -0
  34. package/src/trigger-files.js +212 -0
  35. package/src/trigger-line.js +86 -0
  36. package/src/types.js +1 -0
  37. package/src/version.js +13 -0
  38. package/types/authoring.d.ts +1 -3
  39. package/types/ctx-tools.d.ts +18 -0
  40. package/types/draft-steps.d.ts +38 -5
  41. package/types/drafter.d.ts +3 -3
  42. package/types/draftspend.d.ts +56 -0
  43. package/types/liveness.d.ts +7 -1
  44. package/types/panel/author.d.ts +149 -146
  45. package/types/panel/authorcard.d.ts +12 -2
  46. package/types/panel/authorflows.d.ts +11 -3
  47. package/types/panel/authorstart.d.ts +7 -4
  48. package/types/panel/authortrigger.d.ts +210 -0
  49. package/types/panel/authorvalues.d.ts +3 -1
  50. package/types/panel/data.d.ts +14 -7
  51. package/types/panel/scheduler.d.ts +15 -0
  52. package/types/panel/server.d.ts +11 -3
  53. package/types/plain-checks.d.ts +22 -0
  54. package/types/primitives.d.ts +0 -6
  55. package/types/provider.d.ts +7 -1
  56. package/types/run-store.d.ts +11 -0
  57. package/types/runvalues.d.ts +1 -1
  58. package/types/setup.d.ts +27 -0
  59. package/types/summary-words.d.ts +53 -0
  60. package/types/trigger-clock.d.ts +60 -0
  61. package/types/trigger-files.d.ts +90 -0
  62. package/types/trigger-line.d.ts +44 -0
  63. package/types/types.d.ts +4 -0
  64. package/types/version.d.ts +2 -0
package/src/panel/data.js CHANGED
@@ -22,6 +22,7 @@
22
22
  import {
23
23
  existsSync, realpathSync,
24
24
  } from 'node:fs';
25
+ import { plainPlanCheck } from '../plain-checks.js';
25
26
  import { join, basename, sep } from 'node:path';
26
27
 
27
28
  import {
@@ -37,6 +38,9 @@ import { readSpendRows } from '../provider.js';
37
38
  import { applyRunValues, parseWaitMs, pickRunValues, VALUES_FILE_RE } from '../runvalues.js';
38
39
  import { readSetup } from '../setup.js';
39
40
  import { checkedLines, notCheckedBlock, NOT_CHECKED_LABEL } from '../checked.js';
41
+ import {
42
+ catchUpWords, endedWords, scheduledNextFor, scheduledWords,
43
+ } from '../summary-words.js';
40
44
  import { MAX_STRUCTURE_RETRIES } from '../drafter.js';
41
45
  import {
42
46
  runLiveness, booksFresh, readResumeLock,
@@ -359,13 +363,14 @@ export function glyphPulses(g) {
359
363
  * - `[II]` stuck (M4c amendment 2, `isStuck`) — the answer is saved, not taken,
360
364
  * and no live process (pid row or resume lock holder) carries the run on.
361
365
  * A saved answer with a live process is `[▶]` working on your answer.
366
+ * - `[▶]` starting (live-walk fix) — no end row, no open ask, no pid row yet, and the starter reads this run's start folder as phase `starting` (`ctx.starting`).
362
367
  * - `[?]` crashed after taking your answer (amendment 2 (f)) — the answer was
363
368
  * consumed, the process is gone, no end row: cannot be carried on.
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, jobEdited?: boolean}} ctx
369
+ * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string, resumable?: boolean, stoppedAtAsk?: string|null, jobEdited?: boolean, starting?: boolean}} ctx
365
370
  * @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]'|'[■]', label: string}}
366
371
  */
367
372
  export function computeGlyph({
368
- historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk, jobEdited,
373
+ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk, jobEdited, starting,
369
374
  }) {
370
375
  if (historyRow) {
371
376
  if (historyRow.outcome === 'complete') return { glyph: '[✓]', label: 'passed' };
@@ -410,6 +415,8 @@ export function computeGlyph({
410
415
  // (`liveness`, src/liveness.js); with no row or no /proc (`unknown`), the
411
416
  // 10-minute books rule. Never `[▶]` off a parked ask (branches above).
412
417
  if (liveness === 'running') return { glyph: '[▶]', label: 'running' };
418
+ // The starter's own word (`startingFor`): the start folder is alive and the run has no pid row yet — not a crash.
419
+ if (starting === true && liveness !== 'gone') return { glyph: '[▶]', label: 'starting' };
413
420
  if (liveness === 'gone') return { glyph: '[?]', label: 'the process working on this run is gone (no end row was written)' };
414
421
  if (booksFresh) return { glyph: '[▶]', label: 'running (no pid record; its books changed in the last 10 minutes)' };
415
422
  if (hasStateJson && !askJson) {
@@ -882,9 +889,10 @@ const JOB_EDITED_LINE = 'stopped — the job was edited; start a new run';
882
889
  * @param {string} runId
883
890
  * @param {any} catalogue
884
891
  * @param {any} [attempt] the panel's in-memory resume-attempt record for this run, or null
892
+ * @param {(flow: string, runId: string) => boolean} [startingRun] the starter's "this run's start is in phase `starting`" (absent = no)
885
893
  * @returns {any}
886
894
  */
887
- function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attempt = null) {
895
+ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attempt = null, startingRun = undefined) {
888
896
  const flowRead = readFlow({ root, name: flowName, catalogue });
889
897
  // M4e amendment 5 item 3 / amendment 4 item 6: the run's own signed values (its newest version) lie over the flow's
890
898
  const inForce = flowRead.ok ? applyRunValues(flowRead.arbiter, flowName, flowRead.signature.flow, pickRunValues(runDir)) : null;
@@ -933,6 +941,7 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
933
941
  // M4c exit walk: when the process that is working on the run started (its newest pid row's
934
942
  // own `startedAt`), so a `[▶]` run has a real time and never "parked or died". Not a book change.
935
943
  startedAt: historyRow ? null : lastPidStartedAt(runDir),
944
+ starting: !historyRow && !askJson && startingRun?.(flowName, runId) === true,
936
945
  };
937
946
  }
938
947
 
@@ -1038,11 +1047,11 @@ export function getRunControls({
1038
1047
  * null`, an explicit `why`) — no flow is silently missing from the list.
1039
1048
  * Ordered by `orderRuns` (waiting on you, running, then newest `at` first; a row
1040
1049
  * with no `at` is never given a guessed one).
1041
- * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any}} opts
1042
- * `resumeAttempt`: the panel's in-memory resume-attempt lookup (M4b piece 2); absent = none.
1050
+ * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean}} opts
1051
+ * `startingRun`: the starter's phase-`starting` rule for one run (absent = no). `resumeAttempt`: the panel's in-memory resume-attempt lookup (M4b piece 2); absent = none.
1043
1052
  * @returns {any[]}
1044
1053
  */
1045
- export function listRuns({ root, catalogue, resumeAttempt }) {
1054
+ export function listRuns({ root, catalogue, resumeAttempt, startingRun }) {
1046
1055
  const rows = [];
1047
1056
  for (const flowName of listFlowNames(root)) {
1048
1057
  const flowDir = join(root, flowName);
@@ -1062,7 +1071,7 @@ export function listRuns({ root, catalogue, resumeAttempt }) {
1062
1071
  });
1063
1072
  continue;
1064
1073
  }
1065
- const ctx = loadRunContext(root, run.flowDir, run.runDir, flowName, runId, catalogue, resumeAttempt?.(flowName, runId) ?? null);
1074
+ const ctx = loadRunContext(root, run.flowDir, run.runDir, flowName, runId, catalogue, resumeAttempt?.(flowName, runId) ?? null, startingRun);
1066
1075
  const { glyph, label } = computeGlyph(ctx);
1067
1076
  // M4c amendment 1 (c): the ask waiting on the human, by the same rule
1068
1077
  // listStops uses (deriveAskOpenFields) — the least time left if several.
@@ -1203,6 +1212,69 @@ function computeTryCount(closeClass, listAll) {
1203
1212
  return list.length;
1204
1213
  }
1205
1214
 
1215
+ /** The job line a step of this run came from (`fromLine` of the declared step that emits `emits`), or null when the books cannot say. */
1216
+ function lineOfStep(ctx, emits) {
1217
+ if (!ctx.flowRead.ok || typeof emits !== 'string') return null;
1218
+ const st = (ctx.flowRead.declaration.steps ?? []).find((x) => x.emits === emits);
1219
+ return st && Number.isInteger(st.fromLine) ? st.fromLine : null;
1220
+ }
1221
+
1222
+ /** The job line of the ask a waiting run is parked at: its newest `paused` audit row's step. */
1223
+ function waitingLineOf(ctx) {
1224
+ let emits = null;
1225
+ for (const r of ctx.auditRows) if (r.verdict === 'paused') emits = r.step;
1226
+ return lineOfStep(ctx, emits);
1227
+ }
1228
+
1229
+ /**
1230
+ * M9 piece 6: the line of the ask this JOB is waiting at (any of its runs, the newest first), or null. Reads each run that holds an ask
1231
+ * through the same `runControls` the Stop door uses (`atAsk`), so "waiting" is one decision.
1232
+ */
1233
+ function jobWaitingLine(root, flowDir, flow, catalogue) {
1234
+ const ids = listRunIds(flowDir).sort((a, b) => (Number(b.replace(/\D/g, '')) || 0) - (Number(a.replace(/\D/g, '')) || 0));
1235
+ for (const id of ids) {
1236
+ const run = resolveRunDir(flowDir, id);
1237
+ if (!run.ok || !readAsk(run.runDir)) continue; // eslint-disable-line no-continue
1238
+ const ctx = loadRunContext(root, flowDir, run.runDir, flow, id, catalogue, null);
1239
+ const g = computeGlyph(ctx);
1240
+ if (runControls(ctx, g.glyph, g.label).atAsk) return waitingLineOf(ctx);
1241
+ }
1242
+ return null;
1243
+ }
1244
+
1245
+ /**
1246
+ * M9 piece 6 (ladder M9 item 7): the summary block at the top of a run page, as data. Words come from `src/summary-words.js` only, built
1247
+ * from typed facts (the history row's outcome, the declared step's line, the signed trigger and its ledger). `ended` is null while the run
1248
+ * is live; `scheduled` is null when the job has no trigger; `catchUp` is a fixed sentence when this run was started late (am2 item 1).
1249
+ * @returns {{ended: {reason: string, next: string}|null, scheduled: {text: string, next: string, nextRed: boolean}|null, catchUp: string|null}}
1250
+ */
1251
+ function runSummary(ctx, glyph, { root, catalogue, triggerRead, now, home }) {
1252
+ const died = !ctx.historyRow && glyph === '[?]';
1253
+ const outcome = ctx.historyRow ? ctx.historyRow.outcome ?? null : null;
1254
+ let line = null;
1255
+ if (outcome !== null && outcome !== 'complete') {
1256
+ let emits = null;
1257
+ for (const r of ctx.auditRows) if (isBlockedVerdict(r.verdict)) emits = r.step;
1258
+ line = lineOfStep(ctx, emits);
1259
+ }
1260
+ const send = ctx.arbiter && Array.isArray(ctx.arbiter.sends) ? ctx.arbiter.sends.find((x) => x.target && typeof x.target.path === 'string') : undefined;
1261
+ const ended = endedWords({
1262
+ outcome, died, line, destination: send ? send.target.path : null, canResume: ctx.resumable === true && ctx.sameJob === true, home,
1263
+ });
1264
+ let scheduled = null;
1265
+ const t = typeof triggerRead === 'function' ? triggerRead(ctx.flowName) : null;
1266
+ if (t && t.trigger) {
1267
+ const nx = scheduledNextFor({
1268
+ trigger: t.trigger, armed: t.armed, nextSlotMs: t.nextSlotMs, lastOutcome: t.lastOutcome, waitingLine: jobWaitingLine(root, ctx.flowDir, ctx.flowName, catalogue), now: now(),
1269
+ });
1270
+ scheduled = { text: String(scheduledWords(t.trigger, home)), next: nx.text, nextRed: nx.red };
1271
+ }
1272
+ let rec = null;
1273
+ const f = readFileInside(ctx.runDir, 'trigger.json');
1274
+ if (f.ok) { try { rec = JSON.parse(f.text); } catch { rec = null; } }
1275
+ return { ended, scheduled, catchUp: catchUpWords(rec) };
1276
+ }
1277
+
1206
1278
  /**
1207
1279
  * `GET /api/runs/:flow/:runId` — the Run tab (M4a scope item 2): the step
1208
1280
  * map from `declaration.steps`, step cards -> attempts from `audit.jsonl`
@@ -1210,17 +1282,17 @@ function computeTryCount(closeClass, listAll) {
1210
1282
  * `log.json`. `null` when the flow/runId doesn't resolve to a real run
1211
1283
  * directory at all (caller renders 404); a `red` field names why a
1212
1284
  * sub-derivation couldn't fill (never invented).
1213
- * @param {{root: string, flow: string, runId: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any}} opts
1285
+ * @param {{root: string, flow: string, runId: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean, triggerRead?: (flow: string) => any, now?: () => number, home?: string}} opts
1214
1286
  * @returns {any|null}
1215
1287
  */
1216
1288
  export function getRunDetail({
1217
- root, flow, runId, catalogue, resumeAttempt,
1289
+ root, flow, runId, catalogue, resumeAttempt, startingRun, triggerRead, now = Date.now, home,
1218
1290
  }) {
1219
1291
  const run = resolveRunPath(root, flow, runId);
1220
1292
  if (!run.ok) return null;
1221
1293
  if (!existsSync(run.runDir)) return null;
1222
1294
 
1223
- const ctx = loadRunContext(root, run.flowDir, run.runDir, flow, runId, catalogue, resumeAttempt?.(flow, runId) ?? null);
1295
+ const ctx = loadRunContext(root, run.flowDir, run.runDir, flow, runId, catalogue, resumeAttempt?.(flow, runId) ?? null, startingRun);
1224
1296
  const { glyph, label } = computeGlyph(ctx);
1225
1297
 
1226
1298
  // hamr's 2026-09-27 step-card review: the Run tab's step cards reuse the
@@ -1437,6 +1509,8 @@ export function getRunDetail({
1437
1509
  signWords: SIGN_WORDS,
1438
1510
  pulse: glyphPulses({ glyph, label }),
1439
1511
  controls: runControls(ctx, glyph, label),
1512
+ // M9 piece 6: the block at the top of the run page (ENDED / NEXT, SCHEDULED / NEXT), words set by code
1513
+ summary: runSummary(ctx, glyph, { root, catalogue, triggerRead, now, home }),
1440
1514
  resume: ctx.resume,
1441
1515
  outcome: ctx.historyRow ? ctx.historyRow.outcome : null,
1442
1516
  outcomeWhy: ctx.historyRow ? null : (isRunningNow(ctx, glyph) ? RUNNING_WHY : 'no history row (parked or died before completion)'),
@@ -1521,6 +1595,17 @@ export function getDraftBlock(flowDir, runDir) {
1521
1595
  usd: typeof r.costUsd === 'number' ? r.costUsd : undefined, spendComplete: r.spendComplete === true, tokensDisplay: { kind: 'no-model' },
1522
1596
  ...(typeof r.wallMs === 'number' ? { wallMs: r.wallMs } : {}),
1523
1597
  });
1598
+ // M4d am15 item 4: one row per try under the draft's plan row: `try N ✗|✓`, its plain reason (the first refusal, "and k more"), the raw checker text under it.
1599
+ // The free retry after a broken reply reads `try N again`. A flow signed before am15 recorded no tries: nothing is added.
1600
+ const tryRows = (r, n) => (Array.isArray(r.tries) ? r.tries : []).map((t, k) => {
1601
+ const reds = Array.isArray(t.reds) ? t.reds.map(String) : [];
1602
+ const gap = t.passed ? '' : (reds.length > 0 ? `${plainPlanCheck(reds[0]).reason}${reds.length > 1 ? ` (and ${reds.length - 1} more)` : ''}` : String(t.outcome ?? ''));
1603
+ return {
1604
+ attempt: `${n}.${k + 1}`, step: 'drafting', class: null, verdict: t.passed ? 'green' : 'red', gap, at: r.at ?? null, setup: true, blocked: false,
1605
+ action: `${t.label} ${t.passed ? '✓' : '✗'}`, tryRow: true, tokensDisplay: { kind: 'no-model' },
1606
+ ...(t.passed ? {} : { raw: reds.length > 0 ? reds : [String(t.rawOutcome ?? t.outcome ?? '')] }),
1607
+ };
1608
+ });
1524
1609
  // M4e amendment 8 item 1, line 4: one short phrase per Draft row, in order (the drafting card joins them with ` · `)
1525
1610
  const happened = [];
1526
1611
  const retry = (r) => (Number.isInteger(r.structureRetries) && r.structureRetries >= 1 ? ` (retry ${r.structureRetries} of ${MAX_STRUCTURE_RETRIES})` : '');
@@ -1541,11 +1626,11 @@ export function getDraftBlock(flowDir, runDir) {
1541
1626
  typeof c.inputs === 'string' && c.inputs.trim() ? `inputs: ${c.inputs.trim().replace(/\n+/g, ' / ')}` : null, `job: ${String(c.job ?? '').trim().replace(/\n+/g, ' / ')}`].filter(Boolean).join(' · ');
1542
1627
  rows.push(human(r, n, { action: 'card (you)', gap }));
1543
1628
  } else if (r.kind === 'note') rows.push(human(r, n, { action: 'note (you)', gap: String(r.text ?? '') }));
1544
- else if (r.kind === 'draft') rows.push(model(r, n, 'draft'));
1545
- else if (r.kind === 'change') rows.push(model(r, n, `change ${r.n}`));
1546
- else if (r.kind === 'startover') rows.push(model(r, n, 'start over'));
1547
- else if (r.kind === 'revise') rows.push(model(r, n, `revise ${r.n}`));
1548
- else if (r.kind === 'redraft') rows.push(model(r, n, `answers redraft ${r.n}`));
1629
+ else if (r.kind === 'draft') { rows.push(model(r, n, 'draft')); rows.push(...tryRows(r, n)); }
1630
+ else if (r.kind === 'change') { rows.push(model(r, n, `change ${r.n}`)); rows.push(...tryRows(r, n)); }
1631
+ else if (r.kind === 'startover') { rows.push(model(r, n, 'start over')); rows.push(...tryRows(r, n)); }
1632
+ else if (r.kind === 'revise') { rows.push(model(r, n, `revise ${r.n}`)); rows.push(...tryRows(r, n)); }
1633
+ else if (r.kind === 'redraft') { rows.push(model(r, n, `answers redraft ${r.n}`)); rows.push(...tryRows(r, n)); }
1549
1634
  else if (r.kind === 'sign') rows.push(human(r, n, { action: `sign (${r.signedBy ?? 'you'})`, gap: `plan ${String(r.hash ?? '').slice(0, 12)}` }));
1550
1635
  });
1551
1636
  }
@@ -1644,7 +1729,10 @@ function buildJobPlan(declaration, asks, flowDir, flowHash = null) {
1644
1729
  };
1645
1730
  });
1646
1731
  const setup = readSetup(flowDir);
1647
- const signRow = setup.present ? setup.rows.find((r) => r.kind === 'sign') : undefined;
1732
+ // the signing of THIS job: the sign / trigger-only re-sign row carrying its flow hash; else the first sign row (which then reads as another signing)
1733
+ const signRow = setup.present
1734
+ ? (setup.rows.findLast((r) => (r.kind === 'sign' || r.kind === 'trigger-resign') && flowHash !== null && r.flowHash === flowHash) ?? setup.rows.find((r) => r.kind === 'sign'))
1735
+ : undefined;
1648
1736
  const ofThisJob = !!signRow && (typeof signRow.flowHash !== 'string' || flowHash === null || signRow.flowHash === flowHash);
1649
1737
  const nc = ofThisJob ? signRow?.notChecked : undefined;
1650
1738
  const recorded = !!nc && Array.isArray(nc.items);