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.
@@ -43,9 +43,10 @@ export function createScheduler(opts) {
43
43
  function runQueue(job, ledger, nowMs) {
44
44
  let queue = queueOf(readSeen(ledger));
45
45
  if (queue.length === 0) return;
46
- const spoken = new Set(readOutcomes(job.flowDir).filter((r) => typeof r.sha === 'string').map((r) => `${r.sha}|${r.outcome}`));
46
+ const key = (/** @type {string} */ sha, /** @type {Record<string, any>} */ r) => `${sha}|${r.outcome}|${r.why ?? ''}`; // a refusal is told once PER REASON; busy / monthly carry no `why`
47
+ const spoken = new Set(readOutcomes(job.flowDir).filter((r) => typeof r.sha === 'string').map((r) => key(r.sha, r)));
47
48
  const cause = (/** @type {any} */ q) => ({ kind: /** @type {'file'} */ ('file'), path: q.path, sha: q.sha });
48
- const told = (/** @type {any} */ q) => (/** @type {string} */ outcome) => { const k = `${q.sha}|${outcome}`; const had = spoken.has(k); spoken.add(k); return had; };
49
+ const told = (/** @type {any} */ q) => (/** @type {Record<string, any>} */ row) => { const k = key(q.sha, row); const had = spoken.has(k); spoken.add(k); return had; };
49
50
  if (door.busy(job)) {
50
51
  for (const q of queue) door.fire(job, cause(q), { told: told(q) });
51
52
  return;
@@ -59,7 +59,7 @@ import { readHistory, endRow } from '../books.js';
59
59
  import { resolveRunDir } from '../flow.js';
60
60
  import { readAsk, requestStop, stopParkedRun } from '../runner.js';
61
61
  import {
62
- listRuns, getRunDetail, getRunControls, getRunAudit, getRunJob, listStops, inboxOpenCount, getRunAsks, readSavedAnswer, hasConsumedAnswer, resolveFlowDir,
62
+ listRuns, listScheduled, summarizeJobs, RUNS_LIST_LIMITS, getRunDetail, getRunControls, getRunAudit, getRunJob, listStops, inboxOpenCount, getRunAsks, readSavedAnswer, hasConsumedAnswer, resolveFlowDir,
63
63
  } from './data.js';
64
64
  import { createResumer } from './resume.js';
65
65
  import { createScheduler } from './scheduler.js';
@@ -511,7 +511,15 @@ export function handleRequest(req, res, opts) {
511
511
  const startingRun = (f, r) => opts.author?.startingRun?.(f, r) === true;
512
512
 
513
513
  if (pathname === '/api/runs') {
514
- send(200, { rows: listRuns({ root: opts.root, catalogue, resumeAttempt, startingRun }) });
514
+ // M11: the page is given every state; it works none out. `jobs` is the order and the quiet split, `scheduled` the Scheduled cards, `limits` the two named numbers.
515
+ const now = opts.now ?? Date.now;
516
+ const rows = listRuns({ root: opts.root, catalogue, resumeAttempt, startingRun, now });
517
+ send(200, {
518
+ rows,
519
+ jobs: summarizeJobs(rows, now()),
520
+ scheduled: listScheduled({ root: opts.root, triggerRead: (f) => opts.author?.trigger?.read(f) ?? null, now, rows }),
521
+ limits: RUNS_LIST_LIMITS,
522
+ });
515
523
  return;
516
524
  }
517
525
 
package/src/primitives.js CHANGED
@@ -269,16 +269,15 @@ function addressCellsPrimitiveTool(inputsByRole) {
269
269
  };
270
270
  }
271
271
 
272
- /** The verbs this piece actually implements (the switch below, as data) —
273
- * one writer for "is this verb wired": `resolvePrimitives`'s own reds use
274
- * it, and `src/runner.js`'s preflight (F46: a step granted a
275
- * catalogue-present-but-unwired verb, e.g. litectx's `compress`, must
276
- * refuse before any model call or spend) imports this SAME set rather than
277
- * keeping a second copy of the list. */
278
272
  /** M7: the in-run tools over this run's own store (src/ctx-tools.js). `remember`/`forget` are
279
273
  * deliberately absent: memory lasts one run (PRD: no learning between flows). */
280
274
  const CTX_VERBS = Object.freeze(['recall', 'get', 'recent', 'related', 'impact', 'compress', 'peek', 'stash']);
281
275
 
276
+ /** The verbs this piece actually implements (the switch below, as data) —
277
+ * one writer for "is this verb wired": `resolvePrimitives`'s own reds use
278
+ * it, and `src/runner.js`'s preflight (F46: a step granted a
279
+ * catalogue-present-but-unwired verb must refuse before any model call or
280
+ * spend) imports this SAME set rather than keeping a second copy of the list. */
282
281
  export const WIRED_VERBS = new Set(['read', 'grep', 'write', 'edit', 'readDocx', 'addressCells', ...CTX_VERBS]);
283
282
 
284
283
  /** The wired menu, one place: the catalogue entries whose verb is in
@@ -48,6 +48,27 @@ export function slotWords(ms) {
48
48
  return `${DAY_SHORT[d.getDay()]} ${d.getDate()} ${MONTH_SHORT[d.getMonth()]}, ${two(d.getHours())}:${two(d.getMinutes())}`;
49
49
  }
50
50
 
51
+ /**
52
+ * M11 am1 item 3, the Scheduled card's first line: "today 17:00", "tomorrow 09:00", else the date the NEXT line already says ("Tue 20 Oct, 09:05").
53
+ * Local time, by calendar day. @param {number} ms the next run @param {number} now
54
+ */
55
+ export function nextTimeWords(ms, now) {
56
+ const d = new Date(ms);
57
+ const day = (/** @type {Date} */ x) => new Date(x.getFullYear(), x.getMonth(), x.getDate()).getTime();
58
+ const apart = Math.round((day(d) - day(new Date(now))) / 86_400_000);
59
+ if (apart === 0) return `today ${two(d.getHours())}:${two(d.getMinutes())}`;
60
+ if (apart === 1) return `tomorrow ${two(d.getHours())}:${two(d.getMinutes())}`;
61
+ return slotWords(ms);
62
+ }
63
+
64
+ /** M11 am1 item 3, the Scheduled card's second line start: "next in 20m" / "next in 3h" / "next in 3d" (whole units, rounded down, never "0m"). @param {number} ms @param {number} now */
65
+ export function nextInWords(ms, now) {
66
+ const mins = Math.max(1, Math.floor((ms - now) / 60_000));
67
+ if (mins < 60) return `next in ${mins}m`;
68
+ const hours = Math.floor(mins / 60);
69
+ return hours < 48 ? `next in ${hours}h` : `next in ${Math.floor(hours / 24)}d`;
70
+ }
71
+
51
72
  /** am1, said before a human signs an edit of a job that has a trigger. @param {Trigger|null} t @param {string} [home] */
52
73
  export function editClearsWords(t, home) {
53
74
  const w = scheduledWords(t, home);
@@ -86,11 +107,6 @@ export function scheduledNextFor(f) {
86
107
  return say(`${lead}Next run ${slotWords(nextSlotMs)}.`, red);
87
108
  }
88
109
 
89
- /** The NEXT sentence alone. @param {Parameters<typeof scheduledNextFor>[0]} f */
90
- export function scheduledNext(f) {
91
- return scheduledNextFor(f).text;
92
- }
93
-
94
110
  /** Fixed catch-up sentence (am2 item 1) from a run's `trigger.json`. @param {any} rec */
95
111
  export function catchUpWords(rec) {
96
112
  return rec && typeof rec === 'object' && rec.catchUp === true ? 'This run was a catch-up: it started more than 2 minutes after its set time (the panel was off).' : null;
@@ -49,13 +49,13 @@ const waiting = (rows, a) => a.kind === 'arrived'
49
49
  /**
50
50
  * Does this finished version arrive? Item 6 + am2: only a sha that already RAN never arrives again; one already arrived and still queued does not
51
51
  * arrive twice; one that was dropped (never ran) may. A `baseline` blocks only while it is still the starting point: file-updated = until the file
52
- * has changed away (any later arrived/ran/dropped row of ANOTHER sha); new-file = for that name + sha (a copy under a new name is a new file).
53
- * @param {SeenRow[]} rows @param {{sha:string, name:string}} it @param {'new'|'updated'} mode
52
+ * has changed away (any later arrived/ran/dropped row of ANOTHER sha); new-file = for that sha for good (hamr 2026-10-13: content present at arm never runs, so a renamed or copied baseline file is not new).
53
+ * @param {SeenRow[]} rows @param {{sha:string}} it @param {'new'|'updated'} mode
54
54
  */
55
55
  function arrives(rows, it, mode) {
56
56
  const mine = ofSha(rows, it.sha);
57
57
  if (mine.some((r) => r.kind === 'ran' || waiting(rows, r))) return false;
58
- const bi = rows.findLastIndex((r) => r.kind === 'baseline' && r.sha === it.sha && (mode === 'updated' || r.name === it.name));
58
+ const bi = rows.findLastIndex((r) => r.kind === 'baseline' && r.sha === it.sha);
59
59
  if (bi < 0) return true;
60
60
  return mode === 'updated' && rows.slice(bi + 1).some((r) => r.kind !== 'baseline' && r.sha !== it.sha);
61
61
  }
@@ -176,7 +176,7 @@ export function createFileTrigger({ mode, target, ledger }) {
176
176
  const st2 = lstatSync(p);
177
177
  if (`${st2.size}:${st2.mtimeMs}` !== t.key) return 'changed'; // changed while reading: retry next poll
178
178
  const sha = sha256(buf);
179
- if (!arrives(readSeen(ledger), { sha, name: basename(p) }, mode)) return null; // ran, still queued, or still the starting point
179
+ if (!arrives(readSeen(ledger), { sha }, mode)) return null; // ran, still queued, or still the starting point
180
180
  return { path: p, sha, size: buf.length };
181
181
  }
182
182
 
@@ -52,26 +52,6 @@ export function createAuthor(opts: {
52
52
  body: any;
53
53
  };
54
54
  trigger: {
55
- inspect: (body: any) => {
56
- ok: false;
57
- reply: {
58
- status: number;
59
- body: {
60
- ok: boolean;
61
- };
62
- };
63
- } | {
64
- ok: true;
65
- flow: any;
66
- read: import("../types.js").ReadFlowOk;
67
- trigger: import("../trigger-line.js").Trigger | null;
68
- flowDir: string;
69
- realRoot: string;
70
- proseText: string;
71
- declaration: any;
72
- hash: string;
73
- };
74
- now: () => number;
75
55
  prepare(body: any): {
76
56
  status: number;
77
57
  body: {
@@ -164,7 +144,7 @@ export function createAuthor(opts: {
164
144
  path: string;
165
145
  sha: string;
166
146
  }, hooks?: {
167
- told?: (outcome: string) => boolean;
147
+ told?: (row: Record<string, any>) => boolean;
168
148
  beforeStart?: () => void;
169
149
  }): Record<string, any>;
170
150
  read(flow: string): {
@@ -38,26 +38,6 @@ export function createTriggerDoor(opts: {
38
38
  }[];
39
39
  now?: () => number;
40
40
  }): {
41
- inspect: (body: any) => {
42
- ok: false;
43
- reply: {
44
- status: number;
45
- body: {
46
- ok: boolean;
47
- };
48
- };
49
- } | {
50
- ok: true;
51
- flow: any;
52
- read: import("../types.js").ReadFlowOk;
53
- trigger: import("../trigger-line.js").Trigger | null;
54
- flowDir: string;
55
- realRoot: string;
56
- proseText: string;
57
- declaration: any;
58
- hash: string;
59
- };
60
- now: () => number;
61
41
  /** `POST /api/author/trigger-prepare`: the first click. Checks, signs and writes nothing. @param {any} body */
62
42
  prepare(body: any): {
63
43
  status: number;
@@ -154,8 +134,8 @@ export function createTriggerDoor(opts: {
154
134
  * The fire / ran ledger row was written by the caller BEFORE this, so a crash loses a run and never doubles one.
155
135
  * @param {{flow: string, flowDir: string, read: any, trigger: Trigger}} job
156
136
  * @param {{kind: 'clock', slot: number, catchUp: boolean, missed: number} | {kind: 'file', path: string, sha: string}} cause
157
- * @param {{ told?: (outcome: string) => boolean, beforeStart?: () => void }} [hooks] M9 am3 (a queued file): `told(outcome)` true = this file
158
- * already has a row for that outcome, so none is written again (the outcome is still returned); `beforeStart` runs right before the ONE
137
+ * @param {{ told?: (row: Record<string, any>) => boolean, beforeStart?: () => void }} [hooks] M9 am3 (a queued file): `told(row)` true = this file
138
+ * 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
159
139
  * start, once every check has passed: the scheduler writes the file's `ran` row there.
160
140
  */
161
141
  fire(job: {
@@ -173,7 +153,7 @@ export function createTriggerDoor(opts: {
173
153
  path: string;
174
154
  sha: string;
175
155
  }, hooks?: {
176
- told?: (outcome: string) => boolean;
156
+ told?: (row: Record<string, any>) => boolean;
177
157
  beforeStart?: () => void;
178
158
  }): Record<string, any>;
179
159
  /**
@@ -444,15 +444,16 @@ export function getRunControls({ root, flow, runId, catalogue, }: {
444
444
  * null`, an explicit `why`) — no flow is silently missing from the list.
445
445
  * Ordered by `orderRuns` (waiting on you, running, then newest `at` first; a row
446
446
  * with no `at` is never given a guessed one).
447
- * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean}} opts
448
- * `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.
447
+ * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean, now?: () => number}} opts
448
+ * `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.
449
449
  * @returns {any[]}
450
450
  */
451
- export function listRuns({ root, catalogue, resumeAttempt, startingRun }: {
451
+ export function listRuns({ root, catalogue, resumeAttempt, startingRun, now }: {
452
452
  root: string;
453
453
  catalogue: any;
454
454
  resumeAttempt?: (flow: string, runId: string) => any;
455
455
  startingRun?: (flow: string, runId: string) => boolean;
456
+ now?: () => number;
456
457
  }): any[];
457
458
  /**
458
459
  * The ONE ordering of Runs — History and Workflows both render the list in the
@@ -467,6 +468,54 @@ export function listRuns({ root, catalogue, resumeAttempt, startingRun }: {
467
468
  * @returns {any[]}
468
469
  */
469
470
  export function orderRuns(rows: any[]): any[];
471
+ /**
472
+ * 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.
473
+ * `Resume`: stuck `[II]`, or stopped `[■]` whose Run tab shows the Resume button. An expired ask `[!]` is never here (it stays in the Inbox).
474
+ * 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.
475
+ * 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).
476
+ * @param {string} glyph a `computeGlyph` sign @param {{atAsk: boolean, canResume: boolean}} controls `runControls`' answer
477
+ * @param {string|null} [at] the run's stop time from the books @param {number} [nowMs]
478
+ * @returns {'Accept'|'Resume'|null}
479
+ */
480
+ export function needsYouTag(glyph: string, controls: {
481
+ atAsk: boolean;
482
+ canResume: boolean;
483
+ }, at?: string | null, nowMs?: number): "Accept" | "Resume" | null;
484
+ /**
485
+ * M11: one entry per job (flow), newest activity first. A job's activity is its newest KNOWN run time (`at`, else `askedAt`, else `startedAt`);
486
+ * `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.
487
+ * @param {any[]} rows `listRuns` rows @param {number} nowMs
488
+ * @returns {{flow: string, quiet: boolean, activityMs: number|null}[]}
489
+ */
490
+ export function summarizeJobs(rows: any[], nowMs: number): {
491
+ flow: string;
492
+ quiet: boolean;
493
+ activityMs: number | null;
494
+ }[];
495
+ /**
496
+ * 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
497
+ * have one, by name). Built from the owners the run page's SCHEDULED block uses (`triggerRead`, `scheduledWords`); `at` / `in` are null when
498
+ * there is no next time (nothing is invented). A job that cannot be read, or has no trigger, is absent.
499
+ * 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.
500
+ * @param {{root: string, triggerRead: (flow: string) => any, now?: () => number, home?: string, rows?: {flow: string, runId: string|null}[]}} opts
501
+ * @returns {{flow: string, at: string|null, in: string|null, words: string, runId: string|null}[]}
502
+ */
503
+ export function listScheduled({ root, triggerRead, now, home, rows, }: {
504
+ root: string;
505
+ triggerRead: (flow: string) => any;
506
+ now?: () => number;
507
+ home?: string;
508
+ rows?: {
509
+ flow: string;
510
+ runId: string | null;
511
+ }[];
512
+ }): {
513
+ flow: string;
514
+ at: string | null;
515
+ in: string | null;
516
+ words: string;
517
+ runId: string | null;
518
+ }[];
470
519
  /**
471
520
  * `GET /api/runs/:flow/:runId` — the Run tab (M4a scope item 2): the step
472
521
  * map from `declaration.steps`, step cards -> attempts from `audit.jsonl`
@@ -721,6 +770,12 @@ export const STUCK_REASONS: {
721
770
  why: string;
722
771
  };
723
772
  };
773
+ /** 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`. */
774
+ export const RUNS_LIST_LIMITS: Readonly<{
775
+ quietDays: 30;
776
+ olderRuns: 7;
777
+ needsYouDays: 7;
778
+ }>;
724
779
  /** The words the Draft says for a flow with no `setup.jsonl` (M4e amendment 6 item 4; renamed Draft by amendment 7 item 2). */
725
780
  export const NO_DRAFT_WORDS: "no draft record";
726
781
  /** M6: what the Job tab says for a run that started before runs kept a copy of their job. */
@@ -50,4 +50,9 @@ export class GateRefusal extends Error {
50
50
  path: any;
51
51
  rule: any;
52
52
  }
53
+ /** The verbs this piece actually implements (the switch below, as data) —
54
+ * one writer for "is this verb wired": `resolvePrimitives`'s own reds use
55
+ * it, and `src/runner.js`'s preflight (F46: a step granted a
56
+ * catalogue-present-but-unwired verb must refuse before any model call or
57
+ * spend) imports this SAME set rather than keeping a second copy of the list. */
53
58
  export const WIRED_VERBS: Set<string>;
@@ -6,6 +6,13 @@
6
6
  export function scheduledWords(t: Trigger | null, home?: string): string | null;
7
7
  /** "Mon 13 Oct, 09:00" — local time, fixed English names (never the machine's locale). @param {number} ms */
8
8
  export function slotWords(ms: number): string;
9
+ /**
10
+ * M11 am1 item 3, the Scheduled card's first line: "today 17:00", "tomorrow 09:00", else the date the NEXT line already says ("Tue 20 Oct, 09:05").
11
+ * Local time, by calendar day. @param {number} ms the next run @param {number} now
12
+ */
13
+ export function nextTimeWords(ms: number, now: number): string;
14
+ /** M11 am1 item 3, the Scheduled card's second line start: "next in 20m" / "next in 3h" / "next in 3d" (whole units, rounded down, never "0m"). @param {number} ms @param {number} now */
15
+ export function nextInWords(ms: number, now: number): string;
9
16
  /** am1, said before a human signs an edit of a job that has a trigger. @param {Trigger|null} t @param {string} [home] */
10
17
  export function editClearsWords(t: Trigger | null, home?: string): string | null;
11
18
  /**
@@ -29,8 +36,6 @@ export function scheduledNextFor(f: {
29
36
  text: string;
30
37
  red: boolean;
31
38
  };
32
- /** The NEXT sentence alone. @param {Parameters<typeof scheduledNextFor>[0]} f */
33
- export function scheduledNext(f: Parameters<typeof scheduledNextFor>[0]): string;
34
39
  /** Fixed catch-up sentence (am2 item 1) from a run's `trigger.json`. @param {any} rec */
35
40
  export function catchUpWords(rec: any): "This run was a catch-up: it started more than 2 minutes after its set time (the panel was off)." | null;
36
41
  /**