fwdloop 0.14.0 → 0.15.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.
@@ -106,8 +106,20 @@ export function createStarter(opts) {
106
106
  return d === null ? null : readStart(join(d, id), id, keys);
107
107
  };
108
108
 
109
+ /** M6 amendment 3: the ONE rule for "a run of this flow is starting": the newest start folder of the flow in phase `starting`, or null. @param {string} flow @param {string[]} keys */
110
+ function startingFor(flow, keys) {
111
+ const sd = startsDir();
112
+ if (sd === null) return null;
113
+ for (const id of readdirInside(sd, '.').filter((n) => ID_RE.test(n)).sort().reverse()) {
114
+ const v = readStart(join(sd, id), id, keys);
115
+ if (v && v.flow === flow && v.phase === 'starting') return id;
116
+ }
117
+ return null;
118
+ }
119
+
109
120
  return {
110
121
  checkRun,
122
+ startingFor,
111
123
 
112
124
  /**
113
125
  * Start `fwdloop run` for a flow whose every $0 check already passed at the calling door. Re-checks the run id here (nothing
@@ -150,12 +162,10 @@ export function createStarter(opts) {
150
162
  }
151
163
  const keys = providerKeys(env);
152
164
  // one start at a time while one is still spawning for this flow (a double click, two tabs)
153
- for (const id of readdirInside(sd, '.').filter((n) => ID_RE.test(n)).sort().reverse()) {
154
- const v = readStart(join(sd, id), id, keys);
155
- if (v && v.flow === flow && v.phase === 'starting') {
156
- unclaim();
157
- return { status: 409, body: { ok: false, refused: 'start-live', startId: id, say: 'A run of this flow is already starting. Wait for it.' } };
158
- }
165
+ const startingId = startingFor(flow, keys);
166
+ if (startingId !== null) {
167
+ unclaim();
168
+ return { status: 409, body: { ok: false, refused: 'start-live', startId: startingId, say: 'A run of this flow is already starting. Wait for it.' } };
159
169
  }
160
170
  const id = newId();
161
171
  const dir = join(sd, id);
@@ -190,10 +200,8 @@ export function createStarter(opts) {
190
200
  const sd = startsDir();
191
201
  if (sd === null) return { status: 400, body: { ok: false, refused: 'root', say: 'The flows folder this panel serves does not exist.' } };
192
202
  const keys = providerKeys(env);
193
- for (const id of readdirInside(sd, '.').filter((n) => ID_RE.test(n)).sort().reverse()) {
194
- const v = readStart(join(sd, id), id, keys);
195
- if (v && v.flow === flow && v.phase === 'starting') return { status: 409, body: { ok: false, refused: 'start-live', startId: id, say: 'A run of this flow is already starting. Wait for it.' } };
196
- }
203
+ const startingId = startingFor(flow, keys);
204
+ if (startingId !== null) return { status: 409, body: { ok: false, refused: 'start-live', startId: startingId, say: 'A run of this flow is already starting. Wait for it.' } };
197
205
  const id = newId();
198
206
  const dir = join(sd, id);
199
207
  mkdirSync(sd, { recursive: true, mode: 0o700 });
package/src/panel/data.js CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  import { join, basename, sep } from 'node:path';
26
26
 
27
27
  import {
28
- readFlow, listFlowNames, listRunIds, resolveRunDir, checkFlowName, readFileInside, readdirInside,
28
+ readFlow, readRunJobCopy, listFlowNames, listRunIds, resolveRunDir, checkFlowName, readFileInside, readdirInside,
29
29
  } from '../flow.js';
30
30
  import {
31
31
  readAudit, readHistory, endRow, readPidRows, auditRowTokens, auditRowAt, auditRowTools,
@@ -361,14 +361,16 @@ export function glyphPulses(g) {
361
361
  * A saved answer with a live process is `[▶]` working on your answer.
362
362
  * - `[?]` crashed after taking your answer (amendment 2 (f)) — the answer was
363
363
  * 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}} 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, jobEdited?: boolean}} ctx
365
365
  * @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]'|'[■]', label: string}}
366
366
  */
367
367
  export function computeGlyph({
368
- historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk,
368
+ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk, jobEdited,
369
369
  }) {
370
370
  if (historyRow) {
371
371
  if (historyRow.outcome === 'complete') return { glyph: '[✓]', label: 'passed' };
372
+ // M6 amendment 4: a stopped (or cap-halted) run whose job was edited and signed since cannot be resumed; the words say so.
373
+ if (jobEdited === true && (historyRow.outcome === 'stopped' || historyRow.outcome === 'cap-halt')) return { glyph: '[■]', label: JOB_EDITED_LINE };
372
374
  // 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".
373
375
  if (historyRow.outcome === 'stopped') {
374
376
  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' };
@@ -863,6 +865,13 @@ export function summarizeSpendRows(spendRows) {
863
865
  };
864
866
  }
865
867
 
868
+ /** M6 F5: the ONE comparison of a run's halt record with the job on disk (`sameJob`; the runner refuses a Resume otherwise). */
869
+ const haltMatchesJob = (halt, flowRead) => halt.ok && flowRead.ok && halt.halt.signatureHash === flowRead.signature.flow;
870
+ /** M6 amendment 4: a stopped / cap-halted run (its halt record agrees with its end row) whose readable job is not the one it halted under. */
871
+ const runJobEdited = (halt, historyRow, flowRead) => halt.ok && flowRead.ok && historyRow !== null && historyRow.outcome === halt.halt.outcome && !haltMatchesJob(halt, flowRead);
872
+ /** The whole line (Runs, Ask and Inbox alike) of a stopped run whose job was edited since: no Resume is offered. */
873
+ const JOB_EDITED_LINE = 'stopped — the job was edited; start a new run';
874
+
866
875
  /**
867
876
  * Read every book fwdloop can hold for one run, once. The shared context
868
877
  * every deriver below (and the POC before it) builds its fields from.
@@ -903,6 +912,10 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
903
912
  historyRow,
904
913
  // M4e amendment 4 item 4: a cap-halted or stopped run that can be continued (`halt.json` is the runner's record)
905
914
  resumable: halt.ok && historyRow !== null && historyRow.outcome === halt.halt.outcome,
915
+ // M6 F5: the job on disk is still the one the run halted under (the runner refuses a Resume otherwise: "signature mismatch")
916
+ sameJob: haltMatchesJob(halt, flowRead),
917
+ // M6 amendment 4: the stopped / cap-halted run whose job was edited since (the opposite of `sameJob`, only where the job could be read)
918
+ jobEdited: runJobEdited(halt, historyRow, flowRead),
906
919
  // 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
920
  stoppedAtAsk: lastStoppedAtAsk(runDir, auditRows, historyRow !== null),
908
921
  auditRows,
@@ -944,6 +957,8 @@ function stoppedAskWords(ordered, audit) {
944
957
 
945
958
  /** 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
959
  const STOPPED_STEP_UNKNOWN = 'stopped';
960
+ /** The whole line a stopped run reads when its step cannot be named (Runs, Ask and Inbox alike; am41 item 4). `computeGlyph` builds the same words from STOPPED_STEP_UNKNOWN. */
961
+ const STOPPED_UNKNOWN_LINE = `${STOPPED_STEP_UNKNOWN} — Resume to go on`;
947
962
 
948
963
  /**
949
964
  * How a stopped run was stopped, for its Runs label: the newest `stopped` audit row, when the runner wrote its `gap` as
@@ -983,11 +998,13 @@ const isRunningNow = (ctx, glyph) => !ctx.historyRow && glyph === '[▶]';
983
998
  * @returns {{canStop: boolean, atAsk: boolean, stopRequested: boolean, starting: boolean, canResume: boolean, resumeOutcome: string|null, spentUsd: number|null, spendComplete: boolean|null, capUsd: number|null}}
984
999
  */
985
1000
  export function runControls(ctx, glyph, label = '') {
986
- const running = isRunningNow(ctx, glyph) && !ctx.askJson;
1001
+ // M6 F1: ask.json stays on disk once the human has answered, so it blocks `running` only while the ask is unanswered
1002
+ // (`[▶]` with an ask is only ever "working on your answer": a saved answer, or a consumed one with a live process).
1003
+ const running = isRunningNow(ctx, glyph) && (!ctx.askJson || ctx.consumedAnswerExists || ctx.resume?.state === 'not-started');
987
1004
  // M4e amendment 13: a run waiting at its ask (open, unanswered, not expired) shows Stop too; it stops the run at the ask.
988
1005
  const atAsk = !ctx.historyRow && !!ctx.askJson && glyph === '[·]' && label === WAITING_LABEL;
989
1006
  const starting = !ctx.historyRow && !ctx.askJson && readPidRows(ctx.runDir).length === 0;
990
- const canResume = ctx.resumable === true;
1007
+ const canResume = ctx.resumable === true && ctx.sameJob === true;
991
1008
  return {
992
1009
  canStop: running || atAsk,
993
1010
  atAsk,
@@ -1456,6 +1473,8 @@ export function getRunDetail({
1456
1473
 
1457
1474
  /** The words the Draft says for a flow with no `setup.jsonl` (M4e amendment 6 item 4; renamed Draft by amendment 7 item 2). */
1458
1475
  export const NO_DRAFT_WORDS = 'no draft record';
1476
+ /** M6: what the Job tab says for a run that started before runs kept a copy of their job. */
1477
+ export const NO_JOB_COPY_WORDS = 'This run started before runs kept a copy of their job, so this shows the job as it is now.';
1459
1478
 
1460
1479
  /**
1461
1480
  * 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).
@@ -1604,11 +1623,13 @@ export function getRunAudit({ root, flow, runId }) {
1604
1623
  * 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
1624
  * 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
1625
  * 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
1626
+ * M6: a sign row that names another signing than the job being shown (`flowHash`) is not this job's record: `recorded:false`, never the new job's words.
1627
+ * @param {any} declaration @param {Array<{line:number, ttlMs?:number}>} asks @param {string} flowDir @param {string|null} [flowHash] the signature hash of the job being shown
1608
1628
  */
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) => {
1629
+ function buildJobPlan(declaration, asks, flowDir, flowHash = null) {
1630
+ const lines = checkedLines(declaration, { askLines: asks.map((x) => x.line) });
1631
+ const declSteps = Array.isArray(declaration?.steps) ? declaration.steps : [];
1632
+ const steps = declSteps.map((st, i) => {
1612
1633
  const ak = asks.find((x) => x.line === st.fromLine);
1613
1634
  return {
1614
1635
  step: i + 1,
@@ -1623,7 +1644,9 @@ function buildJobPlan(declaration, asks, flowDir) {
1623
1644
  };
1624
1645
  });
1625
1646
  const setup = readSetup(flowDir);
1626
- const nc = setup.present ? setup.rows.find((r) => r.kind === 'sign')?.notChecked : undefined;
1647
+ const signRow = setup.present ? setup.rows.find((r) => r.kind === 'sign') : undefined;
1648
+ const ofThisJob = !!signRow && (typeof signRow.flowHash !== 'string' || flowHash === null || signRow.flowHash === flowHash);
1649
+ const nc = ofThisJob ? signRow?.notChecked : undefined;
1627
1650
  const recorded = !!nc && Array.isArray(nc.items);
1628
1651
  return { steps, notChecked: { label: NOT_CHECKED_LABEL, items: recorded ? notCheckedBlock(nc.items).items : [], recorded } };
1629
1652
  }
@@ -1647,7 +1670,15 @@ export function getRunJob({
1647
1670
  if (!run.ok) return null;
1648
1671
  if (!existsSync(run.runDir)) return null;
1649
1672
 
1650
- const flowRead = readFlow({ root, name: flow, catalogue });
1673
+ // M6: the job THIS run ran is its own copy (`<run>/job/`, saved when it started). A run from before M6 has none: it shows the flow's job as it is
1674
+ // now and says so (`jobFrom: 'current'`, `jobFromWhy`). A copy that does not verify is refused by name, never swapped for the flow's job.
1675
+ const copy = readRunJobCopy(run.runDir, catalogue);
1676
+ if (copy.present && !copy.ok) {
1677
+ return {
1678
+ flow, runId, resolved: false, why: `This run's saved copy of its job does not match its signature, so it is not shown: ${copy.reds.join('; ')}`,
1679
+ };
1680
+ }
1681
+ const flowRead = copy.present ? copy : readFlow({ root, name: flow, catalogue });
1651
1682
  if (!flowRead.ok) {
1652
1683
  return {
1653
1684
  flow, runId, resolved: false, why: `readFlow refused: ${flowRead.reds.join('; ')}`,
@@ -1672,6 +1703,8 @@ export function getRunJob({
1672
1703
  flow,
1673
1704
  runId,
1674
1705
  resolved: true,
1706
+ jobFrom: copy.present ? 'run' : 'current',
1707
+ jobFromWhy: copy.present ? null : NO_JOB_COPY_WORDS,
1675
1708
  model,
1676
1709
  // `model` is read from this run's own audit.jsonl rows, never the signed
1677
1710
  // prose/declaration (neither records one) — the page says so in words
@@ -1701,7 +1734,7 @@ export function getRunJob({
1701
1734
  guardrails: flowRead.lines
1702
1735
  .filter((l) => typeof l.guardrail === 'string' && l.guardrail.length > 0)
1703
1736
  .map((l) => ({ line: l.n, guardrail: l.guardrail })),
1704
- plan: buildJobPlan(flowRead.declaration, asks, run.flowDir),
1737
+ plan: buildJobPlan(flowRead.declaration, asks, run.flowDir, typeof sig?.flow === 'string' ? sig.flow : null),
1705
1738
  signature: (sig && typeof sig.signedBy === 'string' && typeof sig.signedAt === 'string' && typeof sig.flow === 'string')
1706
1739
  ? { signedBy: sig.signedBy, signedAt: sig.signedAt, hash: sig.flow }
1707
1740
  : null,
@@ -1985,7 +2018,7 @@ function legacyRunAsks(runDir, hasHistoryRow) {
1985
2018
  * (hamr's 2026-09-27 live check).
1986
2019
  * @returns {any[]}
1987
2020
  */
1988
- function runAsksInOrder(runDir, hasHistoryRow, endedExpired = false) {
2021
+ function runAsksInOrder(runDir, hasHistoryRow, endedExpired = false, jobEdited = false) {
1989
2022
  const archivedResult = listArchivedAsks(runDir);
1990
2023
  const rows = archivedResult.archived
1991
2024
  ? archivedResult.asks.map(normalizeArchivedRow)
@@ -2012,6 +2045,11 @@ function runAsksInOrder(runDir, hasHistoryRow, endedExpired = false) {
2012
2045
  for (let i = 0; i < ordered.length; i++) {
2013
2046
  if (ordered[i].status === 'stopped' && words[i] !== null) ordered[i] = { ...ordered[i], statusText: words[i] };
2014
2047
  }
2048
+ // am41 item 4: the newest stopped ask whose step the books cannot name says what the Runs label says (the same words), not a bare "stopped".
2049
+ const newest = ordered.length - 1;
2050
+ if (ordered[newest].status === 'stopped' && words[newest] === null) ordered[newest] = { ...ordered[newest], statusText: STOPPED_UNKNOWN_LINE };
2051
+ // M6 amendment 4: the job was edited since the stop, so no stopped ask promises a Resume
2052
+ if (jobEdited) for (let i = 0; i < ordered.length; i++) if (ordered[i].status === 'stopped') ordered[i] = { ...ordered[i], statusText: JOB_EDITED_LINE };
2015
2053
  }
2016
2054
  const last = ordered[ordered.length - 1];
2017
2055
  if (endedExpired && last && (last.status === 'accepted' || last.status === 'redo' || last.status === 'reran')) {
@@ -2140,11 +2178,11 @@ export function getRunAsks({
2140
2178
  // Inbox — otherwise an open ask's Ask-tab header/body falls back to the
2141
2179
  // raw status word "unanswered" and no time-left, disagreeing with the
2142
2180
  // Inbox row for that exact same ask.
2143
- const ordered = runAsksInOrder(run.runDir, hasHistoryRow, histRow?.outcome === 'ask-expired');
2144
- const auditRows = readAudit(run.runDir);
2145
2181
  const flowRead = readFlow({
2146
2182
  root, name: flow, catalogue,
2147
2183
  });
2184
+ const ordered = runAsksInOrder(run.runDir, hasHistoryRow, histRow?.outcome === 'ask-expired', runJobEdited(readHaltRecord(run.runDir), histRow ?? null, flowRead));
2185
+ const auditRows = readAudit(run.runDir);
2148
2186
  const declSteps = flowRead.ok ? flowRead.declaration.steps : null;
2149
2187
  const stepInfo = deriveAskStepInfo(ordered, auditRows, declSteps);
2150
2188
  let resume = hasHistoryRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(run.runDir), attempt: resumeAttempt?.(flow, runId) ?? null, ask: readAsk(run.runDir) });
@@ -2194,10 +2232,10 @@ export function getRunAsks({
2194
2232
  * force-`false` here as a second, independent check (belt-and-braces on the
2195
2233
  * one thing the brief calls out by name: "the Inbox tab's open count counts
2196
2234
  * only truly open asks — parked run, no history row, unexpired, unanswered").
2197
- * @param {{root: string, resumeAttempt?: (flow: string, runId: string) => any}} opts
2235
+ * @param {{root: string, resumeAttempt?: (flow: string, runId: string) => any, catalogue?: any}} opts
2198
2236
  * @returns {any[]}
2199
2237
  */
2200
- export function listStops({ root, resumeAttempt }) {
2238
+ export function listStops({ root, resumeAttempt, catalogue }) {
2201
2239
  const rows = [];
2202
2240
  for (const flowName of listFlowNames(root)) {
2203
2241
  const flowDir = join(root, flowName);
@@ -2213,7 +2251,10 @@ export function listStops({ root, resumeAttempt }) {
2213
2251
  const stuckAskId = !hasHistoryRow && st.stuck ? (resume?.askId ?? null) : null;
2214
2252
  const openAskJson = hasHistoryRow ? null : readAsk(run.runDir);
2215
2253
  const savedAskId = readSavedAnswer(run.runDir)?.askId;
2216
- const runAsks = runAsksInOrder(run.runDir, hasHistoryRow, end?.outcome === 'ask-expired').map((ask) => ({
2254
+ // M6 amendment 4: read the job only for a run that ended stopped / cap-halted (and only when the caller gave the catalogue to read it with)
2255
+ const jobEdited = end !== null && (end.outcome === 'stopped' || end.outcome === 'cap-halt') && catalogue !== undefined
2256
+ && runJobEdited(readHaltRecord(run.runDir), end, readFlow({ root, name: flowName, catalogue }));
2257
+ const runAsks = runAsksInOrder(run.runDir, hasHistoryRow, end?.outcome === 'ask-expired', jobEdited).map((ask) => ({
2217
2258
  flow: flowName,
2218
2259
  runId,
2219
2260
  ...ask,