fwdloop 0.18.0 → 0.19.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fwdloop",
3
- "version": "0.18.0",
3
+ "version": "0.19.1",
4
4
  "description": "Describe a job as steps and guardrails; an agent builds the workflow, pauses for humans where you said, checks every step happened, and reruns it under budgets it cannot touch.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -216,9 +216,6 @@ export function createTriggerDoor(opts) {
216
216
  }
217
217
 
218
218
  return {
219
- inspect,
220
- now,
221
-
222
219
  /** `POST /api/author/trigger-prepare`: the first click. Checks, signs and writes nothing. @param {any} body */
223
220
  prepare(body) {
224
221
  const s = inspect(body);
@@ -296,8 +293,8 @@ export function createTriggerDoor(opts) {
296
293
  * The fire / ran ledger row was written by the caller BEFORE this, so a crash loses a run and never doubles one.
297
294
  * @param {{flow: string, flowDir: string, read: any, trigger: Trigger}} job
298
295
  * @param {{kind: 'clock', slot: number, catchUp: boolean, missed: number} | {kind: 'file', path: string, sha: string}} cause
299
- * @param {{ told?: (outcome: string) => boolean, beforeStart?: () => void }} [hooks] M9 am3 (a queued file): `told(outcome)` true = this file
300
- * already has a row for that outcome, so none is written again (the outcome is still returned); `beforeStart` runs right before the ONE
296
+ * @param {{ told?: (row: Record<string, any>) => boolean, beforeStart?: () => void }} [hooks] M9 am3 (a queued file): `told(row)` true = this file
297
+ * already has a row for that outcome and reason (`why`), so none is written again (the outcome is still returned); `beforeStart` runs right before the ONE
301
298
  * start, once every check has passed: the scheduler writes the file's `ran` row there.
302
299
  */
303
300
  fire(job, cause, hooks = {}) {
@@ -307,7 +304,7 @@ export function createTriggerDoor(opts) {
307
304
  at: new Date(now()).toISOString(), flow, trigger: desc,
308
305
  ...(cause.kind === 'clock' ? { slot: new Date(cause.slot).toISOString(), catchUp: cause.catchUp, missed: cause.missed } : { file: cause.path, sha: cause.sha, catchUp: false }),
309
306
  };
310
- const done = (/** @type {Record<string, any>} */ r) => { const row = /** @type {Record<string, any>} */ ({ ...base, ...r }); if (!(row.outcome !== 'started' && hooks.told?.(row.outcome))) appendOutcome(flowDir, row); return row; };
307
+ const done = (/** @type {Record<string, any>} */ r) => { const row = /** @type {Record<string, any>} */ ({ ...base, ...r }); if (!(row.outcome !== 'started' && hooks.told?.(row))) appendOutcome(flowDir, row); return row; };
311
308
  const busy = flowsDoor.editBlock(flow); // one run at a time per job: starting, running, or waiting at its ask
312
309
  if (busy !== null) return done({ outcome: 'busy', say: busy });
313
310
  const claim = monthlyClaim(read.arbiter.capUsd); // the same check the Run door decides on
@@ -355,7 +352,9 @@ export function createTriggerDoor(opts) {
355
352
  const { trigger } = r.arbiter;
356
353
  const clock = readRows(join(triggerLedgerDir(flowDir, r.signature.flow), 'clock.jsonl'));
357
354
  const armed = clock.find((x) => x.kind === 'armed');
358
- const outcomes = readOutcomes(flowDir);
355
+ // triggers.jsonl spans every trigger the job ever had and its rows carry words, not the hash: only rows from this signing on are this trigger's
356
+ const since = Date.parse(r.signature.signedAt);
357
+ const outcomes = readOutcomes(flowDir).filter((o) => Date.parse(o.at) >= since);
359
358
  const clockKind = trigger !== null && (trigger.kind === 'daily' || trigger.kind === 'weekdays' || trigger.kind === 'weekly');
360
359
  return {
361
360
  flow, trigger, flowHash: r.signature.flow, armed: armed !== undefined, armedAt: armed ? armed.at : null,
package/src/panel/data.js CHANGED
@@ -39,7 +39,7 @@ import { applyRunValues, parseWaitMs, pickRunValues, VALUES_FILE_RE } from '../r
39
39
  import { readSetup } from '../setup.js';
40
40
  import { checkedLines, notCheckedBlock, NOT_CHECKED_LABEL } from '../checked.js';
41
41
  import {
42
- catchUpWords, endedWords, scheduledNextFor, scheduledWords,
42
+ catchUpWords, endedWords, nextInWords, nextTimeWords, scheduledNextFor, scheduledWords,
43
43
  } from '../summary-words.js';
44
44
  import { MAX_STRUCTURE_RETRIES } from '../drafter.js';
45
45
  import {
@@ -906,7 +906,8 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
906
906
  const stateJson = readRunState(runDir);
907
907
  const consumedAnswerExists = hasConsumedAnswer(runDir, askJson && typeof askJson.askId === 'string' ? askJson.askId : null);
908
908
  // A finished run has no resume to report (its history row says how it ended).
909
- const resume = historyRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(runDir), attempt, ask: askJson });
909
+ const savedAnswer = historyRow ? null : readSavedAnswer(runDir);
910
+ const resume = historyRow ? null : deriveResumeState({ savedAnswer, attempt, ask: askJson });
910
911
  const stuckInputs = historyRow ? { liveness: 'unknown', lock: 'none' } : readStuckInputs(runDir);
911
912
 
912
913
  return {
@@ -934,6 +935,8 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
934
935
  consumedAnswerExists,
935
936
  hasStateJson: stateJson !== null,
936
937
  resume,
938
+ // M11 am4 item 1: when the saved answer was written (a stuck run has no stop time; its 7 days in Needs you count from here), or null
939
+ savedAnswerAt: savedAnswer ? savedAnswer.answeredAt : null,
937
940
  // M4c: only read for a run with no end row (an end row always wins).
938
941
  liveness: stuckInputs.liveness,
939
942
  lock: stuckInputs.lock,
@@ -1047,11 +1050,11 @@ export function getRunControls({
1047
1050
  * null`, an explicit `why`) — no flow is silently missing from the list.
1048
1051
  * Ordered by `orderRuns` (waiting on you, running, then newest `at` first; a row
1049
1052
  * with no `at` is never given a guessed one).
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.
1053
+ * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean, now?: () => number}} opts
1054
+ * `now`: the clock for Needs you's 7-day rule (absent = Date.now). `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.
1052
1055
  * @returns {any[]}
1053
1056
  */
1054
- export function listRuns({ root, catalogue, resumeAttempt, startingRun }) {
1057
+ export function listRuns({ root, catalogue, resumeAttempt, startingRun, now = Date.now }) {
1055
1058
  const rows = [];
1056
1059
  for (const flowName of listFlowNames(root)) {
1057
1060
  const flowDir = join(root, flowName);
@@ -1073,6 +1076,7 @@ export function listRuns({ root, catalogue, resumeAttempt, startingRun }) {
1073
1076
  }
1074
1077
  const ctx = loadRunContext(root, run.flowDir, run.runDir, flowName, runId, catalogue, resumeAttempt?.(flowName, runId) ?? null, startingRun);
1075
1078
  const { glyph, label } = computeGlyph(ctx);
1079
+ const controls = runControls(ctx, glyph, label);
1076
1080
  // M4c amendment 1 (c): the ask waiting on the human, by the same rule
1077
1081
  // listStops uses (deriveAskOpenFields) — the least time left if several.
1078
1082
  const waitingAsk = runAsksInOrder(run.runDir, !!ctx.historyRow)
@@ -1103,7 +1107,10 @@ export function listRuns({ root, catalogue, resumeAttempt, startingRun }) {
1103
1107
  ...signParts(glyph, label, ctx.historyRow?.outcome === 'rerun'),
1104
1108
  pulse: glyphPulses({ glyph, label }),
1105
1109
  stuck: glyph === '[II]',
1106
- controls: runControls(ctx, glyph, label),
1110
+ controls,
1111
+ needs: needsYouTag(glyph, controls, glyph === '[II]' ? ctx.savedAnswerAt : (ctx.historyRow ? ctx.historyRow.at : null), now()),
1112
+ live: isRunningNow(ctx, glyph),
1113
+ model: deriveRunModel(ctx.auditRows),
1107
1114
  resume: ctx.resume,
1108
1115
  spend,
1109
1116
  spendWhy: (!ctx.historyRow && spend === null)
@@ -1163,6 +1170,83 @@ export function orderRuns(rows) {
1163
1170
  .map(({ row }) => row);
1164
1171
  }
1165
1172
 
1173
+ /**
1174
+ * M11 am1 item 2 + am2: the ONE rule for a run's Needs-you tag, read off the sign and `runControls` (no second rule). `Accept`: waiting at an open ask.
1175
+ * `Resume`: stuck `[II]`, or stopped `[■]` whose Run tab shows the Resume button. An expired ask `[!]` is never here (it stays in the Inbox).
1176
+ * M11 am3 item 5: a Resume run leaves Needs you once its stop time (`at`, the history row's) is `RUNS_LIST_LIMITS.needsYouDays` old; exactly that old is out.
1177
+ * M11 am4 item 1: a stuck `[II]` run has no stop time; the caller passes the time its saved answer was written. A run with no readable time stays in. An Accept run is never dropped by age (its ask has its own wait).
1178
+ * @param {string} glyph a `computeGlyph` sign @param {{atAsk: boolean, canResume: boolean}} controls `runControls`' answer
1179
+ * @param {string|null} [at] the run's stop time from the books @param {number} [nowMs]
1180
+ * @returns {'Accept'|'Resume'|null}
1181
+ */
1182
+ export function needsYouTag(glyph, controls, at = null, nowMs = Date.now()) {
1183
+ if (glyph === '[·]' && controls.atAsk) return 'Accept';
1184
+ if (glyph !== '[II]' && !(glyph === '[■]' && controls.canResume)) return null;
1185
+ const t = typeof at === 'string' ? Date.parse(at) : NaN;
1186
+ if (Number.isFinite(t) && nowMs - t >= RUNS_LIST_LIMITS.needsYouDays * 86_400_000) return null;
1187
+ return 'Resume';
1188
+ }
1189
+
1190
+ /** M11 scope item 2.5/2.6 and rules, am3 item 5: the named limits of the runs list, in ONE place (30 quiet days, 7 older runs, 7 days in Needs you for a Resume run). The page reads them from `GET /api/runs`. */
1191
+ export const RUNS_LIST_LIMITS = Object.freeze({ quietDays: 30, olderRuns: 7, needsYouDays: 7 });
1192
+
1193
+ /**
1194
+ * M11: one entry per job (flow), newest activity first. A job's activity is its newest KNOWN run time (`at`, else `askedAt`, else `startedAt`);
1195
+ * `quiet` only when that time is known and older than the quiet limit: an unknown age is never guessed into quiet, and a job with no runs is not quiet.
1196
+ * @param {any[]} rows `listRuns` rows @param {number} nowMs
1197
+ * @returns {{flow: string, quiet: boolean, activityMs: number|null}[]}
1198
+ */
1199
+ export function summarizeJobs(rows, nowMs) {
1200
+ /** @type {Map<string, number|null>} */
1201
+ const byFlow = new Map();
1202
+ for (const r of rows) {
1203
+ if (!byFlow.has(r.flow)) byFlow.set(r.flow, null);
1204
+ if (r.runId === null) continue; // eslint-disable-line no-continue
1205
+ const t = [r.at, r.askedAt, r.startedAt].map((x) => (typeof x === 'string' ? Date.parse(x) : NaN)).find((x) => Number.isFinite(x));
1206
+ if (t !== undefined && (byFlow.get(r.flow) === null || t > /** @type {number} */ (byFlow.get(r.flow)))) byFlow.set(r.flow, t);
1207
+ }
1208
+ const quietMs = RUNS_LIST_LIMITS.quietDays * 86_400_000;
1209
+ return [...byFlow.entries()]
1210
+ .map(([flow, activityMs]) => ({ flow, quiet: activityMs !== null && nowMs - activityMs > quietMs, activityMs }))
1211
+ .sort((a, b) => {
1212
+ if (a.activityMs !== b.activityMs) {
1213
+ if (a.activityMs === null) return 1;
1214
+ if (b.activityMs === null) return -1;
1215
+ return b.activityMs - a.activityMs;
1216
+ }
1217
+ return a.flow < b.flow ? -1 : a.flow > b.flow ? 1 : 0;
1218
+ });
1219
+ }
1220
+
1221
+ /**
1222
+ * M11 am1 item 3: every job with a trigger, soonest next run first (a job with no clock slot — a file trigger, or not armed yet — after those that
1223
+ * have one, by name). Built from the owners the run page's SCHEDULED block uses (`triggerRead`, `scheduledWords`); `at` / `in` are null when
1224
+ * there is no next time (nothing is invented). A job that cannot be read, or has no trigger, is absent.
1225
+ * M11 am4 item 2: each card carries `runId`, the job's newest run (the first row `listRuns` gives that job, the one the runs list heads its job card with), or null when it has none.
1226
+ * @param {{root: string, triggerRead: (flow: string) => any, now?: () => number, home?: string, rows?: {flow: string, runId: string|null}[]}} opts
1227
+ * @returns {{flow: string, at: string|null, in: string|null, words: string, runId: string|null}[]}
1228
+ */
1229
+ export function listScheduled({
1230
+ root, triggerRead, now = Date.now, home, rows = [],
1231
+ }) {
1232
+ const t0 = now();
1233
+ const out = [];
1234
+ for (const flow of listFlowNames(root)) {
1235
+ const t = triggerRead(flow);
1236
+ if (!t || !t.trigger) continue; // eslint-disable-line no-continue
1237
+ const ms = t.armed && typeof t.nextSlotMs === 'number' ? t.nextSlotMs : null;
1238
+ out.push({
1239
+ ms, flow, at: ms === null ? null : nextTimeWords(ms, t0), in: ms === null ? null : nextInWords(ms, t0), words: String(scheduledWords(t.trigger, home)),
1240
+ runId: rows.find((r) => r.flow === flow)?.runId ?? null,
1241
+ });
1242
+ }
1243
+ out.sort((a, b) => {
1244
+ if (a.ms !== b.ms) return a.ms === null ? 1 : b.ms === null ? -1 : a.ms - b.ms;
1245
+ return a.flow < b.flow ? -1 : a.flow > b.flow ? 1 : 0;
1246
+ });
1247
+ return out.map(({ ms, ...card }) => card);
1248
+ }
1249
+
1166
1250
  /**
1167
1251
  * hamr's 2026-09-27 step-card review: the ONE place raw `audit.jsonl` rows
1168
1252
  * get their server-derived fields (`action`/`tokensDisplay`/`atWhy`/
@@ -1245,8 +1329,8 @@ function jobWaitingLine(root, flowDir, flow, catalogue) {
1245
1329
  /**
1246
1330
  * 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
1331
  * 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}}
1332
+ * is live; `scheduled` is null when the job has no trigger; `canSchedule` (am3 item 1) is the trigger door's own `hasGreenRun` answer; `catchUp` is a fixed sentence when this run was started late (am2 item 1).
1333
+ * @returns {{ended: {reason: string, next: string}|null, scheduled: {text: string, next: string, nextRed: boolean}|null, canSchedule: boolean, catchUp: string|null}}
1250
1334
  */
1251
1335
  function runSummary(ctx, glyph, { root, catalogue, triggerRead, now, home }) {
1252
1336
  const died = !ctx.historyRow && glyph === '[?]';
@@ -1272,7 +1356,7 @@ function runSummary(ctx, glyph, { root, catalogue, triggerRead, now, home }) {
1272
1356
  let rec = null;
1273
1357
  const f = readFileInside(ctx.runDir, 'trigger.json');
1274
1358
  if (f.ok) { try { rec = JSON.parse(f.text); } catch { rec = null; } }
1275
- return { ended, scheduled, catchUp: catchUpWords(rec) };
1359
+ return { ended, scheduled, canSchedule: !!(t && t.hasGreenRun), catchUp: catchUpWords(rec) };
1276
1360
  }
1277
1361
 
1278
1362
  /**