fwdloop 0.19.0 → 0.20.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/bin/fwdloop +22 -13
- package/package.json +1 -1
- package/src/authoring.js +23 -3
- package/src/books.js +3 -2
- package/src/flow.js +218 -2
- package/src/jobkey.js +25 -0
- package/src/jsonl-append.js +29 -0
- package/src/monthly.js +3 -2
- package/src/panel/author.js +44 -18
- package/src/panel/authorcard.js +7 -13
- package/src/panel/authorflows.js +118 -24
- package/src/panel/authorresume.js +8 -4
- package/src/panel/authorstart.js +63 -18
- package/src/panel/authortrigger.js +252 -128
- package/src/panel/authorvalues.js +13 -2
- package/src/panel/data.js +233 -36
- package/src/panel/index.html +330 -124
- package/src/panel/needsyou.js +29 -0
- package/src/panel/scheduler.js +23 -23
- package/src/panel/server.js +30 -6
- package/src/primitives.js +5 -6
- package/src/provider.js +3 -2
- package/src/runner.js +10 -10
- package/src/runvalues.js +10 -2
- package/src/schedule.js +135 -0
- package/src/setup.js +0 -21
- package/src/summary-words.js +180 -12
- package/src/trigger-clock.js +18 -16
- package/src/trigger-files.js +40 -27
- package/src/trigger-line.js +6 -15
- package/src/types.js +1 -1
- package/types/authoring.d.ts +32 -2
- package/types/flow.d.ts +106 -0
- package/types/jobkey.d.ts +11 -0
- package/types/jsonl-append.d.ts +6 -0
- package/types/panel/author.d.ts +48 -38
- package/types/panel/authorcard.d.ts +3 -6
- package/types/panel/authorflows.d.ts +38 -10
- package/types/panel/authorstart.d.ts +15 -2
- package/types/panel/authortrigger.d.ts +75 -63
- package/types/panel/authorvalues.d.ts +6 -0
- package/types/panel/data.d.ts +101 -19
- package/types/panel/needsyou.d.ts +20 -0
- package/types/primitives.d.ts +5 -0
- package/types/runvalues.d.ts +5 -0
- package/types/schedule.d.ts +69 -0
- package/types/setup.d.ts +0 -19
- package/types/summary-words.d.ts +105 -4
- package/types/trigger-clock.d.ts +8 -7
- package/types/trigger-files.d.ts +17 -10
- package/types/trigger-line.d.ts +16 -8
- package/types/types.d.ts +1 -1
package/src/summary-words.js
CHANGED
|
@@ -69,13 +69,6 @@ export function nextInWords(ms, now) {
|
|
|
69
69
|
return hours < 48 ? `next in ${hours}h` : `next in ${Math.floor(hours / 24)}d`;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
/** am1, said before a human signs an edit of a job that has a trigger. @param {Trigger|null} t @param {string} [home] */
|
|
73
|
-
export function editClearsWords(t, home) {
|
|
74
|
-
const w = scheduledWords(t, home);
|
|
75
|
-
if (w === null) return null;
|
|
76
|
-
return `This job runs by itself (${w}). Signing this edit clears that back to By hand. You can set it again once the new job has had one good run.`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
72
|
/**
|
|
80
73
|
* NEXT of a job with a trigger. One sentence, first match wins:
|
|
81
74
|
* 1. a run of this job is waiting at its ask (the next run waits too);
|
|
@@ -107,11 +100,6 @@ export function scheduledNextFor(f) {
|
|
|
107
100
|
return say(`${lead}Next run ${slotWords(nextSlotMs)}.`, red);
|
|
108
101
|
}
|
|
109
102
|
|
|
110
|
-
/** The NEXT sentence alone. @param {Parameters<typeof scheduledNextFor>[0]} f */
|
|
111
|
-
export function scheduledNext(f) {
|
|
112
|
-
return scheduledNextFor(f).text;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
103
|
/** Fixed catch-up sentence (am2 item 1) from a run's `trigger.json`. @param {any} rec */
|
|
116
104
|
export function catchUpWords(rec) {
|
|
117
105
|
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;
|
|
@@ -174,3 +162,183 @@ export function endedWords(f) {
|
|
|
174
162
|
}
|
|
175
163
|
}
|
|
176
164
|
}
|
|
165
|
+
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
// M9 am6 / am7 (docs/wiki/the-module-ladder.md): the words of "how did this run begin", "what schedule does the workflow have", the Audit's schedule rows and
|
|
168
|
+
// the Run tab's SCHEDULE block. ONE owner: the Job tab's Schedule field and the Run tab's SCHEDULE line 1 are built by `scheduleHead`, the run's
|
|
169
|
+
// Started line and the Audit's first row by `startedParts`. Pure: typed facts in, a fixed sentence out; no model text reaches any of it.
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
|
|
172
|
+
/** "MM/DD HH:MM", local time: the same form as the run cards' `rlShortDate`. @param {number|string} when epoch ms or an ISO string */
|
|
173
|
+
export function shortStamp(when) {
|
|
174
|
+
const d = new Date(typeof when === 'string' ? Date.parse(when) : when);
|
|
175
|
+
if (Number.isNaN(d.getTime())) return '';
|
|
176
|
+
return `${two(d.getMonth() + 1)}/${two(d.getDate())} ${two(d.getHours())}:${two(d.getMinutes())}`;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** "Tue 10/20 09:05": the day name then the short stamp. @param {number} ms */
|
|
180
|
+
export function dayStamp(ms) {
|
|
181
|
+
return `${DAY_SHORT[new Date(ms).getDay()]} ${shortStamp(ms)}`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The schedule in words, lower-case first ("every day at 08:15"): the SINGLE line-1 of the Run tab's SCHEDULE block and the first part of the Job tab's
|
|
186
|
+
* Schedule field. `null` (no schedule) has no sentence. @param {Trigger|null} t @param {string} [home]
|
|
187
|
+
*/
|
|
188
|
+
export function scheduleHead(t, home) {
|
|
189
|
+
const w = scheduledWords(t, home);
|
|
190
|
+
return w === null ? null : `${w.charAt(0).toLowerCase()}${w.slice(1)}`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The Job tab's "Schedule" field: the workflow's schedule now and what it is bound to, parts joined by " · ", or `none`.
|
|
195
|
+
* @param {{trigger: Trigger|null, from?: string|null, model?: string|null, destination?: string|null, inputs?: string[]|null, home?: string}} f
|
|
196
|
+
*/
|
|
197
|
+
export function scheduleFieldWords(f) {
|
|
198
|
+
const head = scheduleHead(f.trigger, f.home);
|
|
199
|
+
if (head === null) return 'none';
|
|
200
|
+
return [head, f.from ? `from ${f.from}` : null, f.model || null, f.destination ? tilde(f.destination, f.home) : null, f.inputs && f.inputs.length > 0 ? f.inputs.join(', ') : null]
|
|
201
|
+
.filter((x) => x !== null).join(' · ');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A run's `trigger.json` record -> the typed pieces of "how it began", or `null` for a hand run. @param {any} rec */
|
|
205
|
+
function startedKind(rec) {
|
|
206
|
+
if (!rec || typeof rec !== 'object') return null;
|
|
207
|
+
return rec.kind === 'file' || rec.kind === 'clock' ? rec.kind : null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The Job tab's Started line: `By hand · 10/15 13:46`, `By the schedule: every day at 08:15 · catch-up, 10 min late · 10/15 08:16`,
|
|
212
|
+
* `By a new file: aging-oct.csv · …`, `By a file update: aging.csv · …`. The trailing stamp is the run's start (am7 item 5); absent when the books do not hold one.
|
|
213
|
+
* @param {any} rec the run's trigger.json or null @param {number|null} startMs @param {string} [home]
|
|
214
|
+
*/
|
|
215
|
+
export function startedWords(rec, startMs, home) {
|
|
216
|
+
const stamp = startMs === null || startMs === undefined ? null : shortStamp(startMs);
|
|
217
|
+
const kind = startedKind(rec);
|
|
218
|
+
let head = 'By hand';
|
|
219
|
+
if (kind === 'clock') {
|
|
220
|
+
const late = rec.catchUp === true ? (Number.isFinite(rec.lateMin) ? `catch-up, ${rec.lateMin} min late` : 'catch-up') : null;
|
|
221
|
+
head = `By the schedule: ${rec.words ?? rec.trigger ?? 'a schedule'}${late ? ` · ${late}` : ''}`;
|
|
222
|
+
} else if (kind === 'file') {
|
|
223
|
+
const name = typeof rec.file === 'string' ? basename(rec.file) : '';
|
|
224
|
+
const updated = rec.triggerKind === 'file-updated';
|
|
225
|
+
head = `${updated ? 'By a file update' : 'By a new file'}${name ? `: ${name}` : ''}`;
|
|
226
|
+
}
|
|
227
|
+
return stamp ? `${head} · ${stamp}` : head;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** M9 am8 item 3: the busy sentence, in the busy row of `triggers.jsonl` and in the SCHEDULE block. One owner; the scheduler writes it, the words read it. */
|
|
231
|
+
export const SCHEDULE_BUSY_SAY = 'a run of this job was still working';
|
|
232
|
+
|
|
233
|
+
/** A month-used-up fire in the SCHEDULE block's words (am8 item 5): `month used up: $0.01 left, needs $0.50`. @param {any} row */
|
|
234
|
+
function monthUsedUpWords(row) {
|
|
235
|
+
const left = typeof row.leftUsd === 'number' ? `${row.atLeast === true ? 'at most ' : ''}${money(row.leftUsd)} left` : 'not enough left';
|
|
236
|
+
return `month used up: ${left}${typeof row.needUsd === 'number' ? `, needs ${money(row.needUsd)}` : ''}`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The Run tab's SCHEDULE block (am7 item 3, am8 items 2-8): `{line1, line2, red}`. Line 1 = the schedule words or `By hand`. Line 2 (only with a schedule):
|
|
241
|
+
* `no run yet` before any fire; else `last <stamp> · ` then the newest fire's words, then `next ...`:
|
|
242
|
+
* - a start: `<run-N> · <outcome>[ · catch-up]` (a run waiting at its ask: `<run-N> · waiting for your answer (the next run does not wait)`), normal;
|
|
243
|
+
* - busy: `busy: a run of this job was still working`, normal (it clears by itself);
|
|
244
|
+
* - month used up: `month used up: $0.01 left, needs $0.50 · next: raise the limit in Settings, or it starts on the 1st`, red only while `nowMs` is still that month;
|
|
245
|
+
* - refused: `refused: <why> · next: fix it in Cancel / modify schedule`, red until a later good start.
|
|
246
|
+
* `next` otherwise: `next <day stamp>`, `next: waiting for a new file` (file schedule) or `next: not armed yet`.
|
|
247
|
+
* @param {{trigger: Trigger|null, home?: string, last: {at: string, runId: string, outcome: string, catchUp: boolean}|null, nextSlotMs: number|null, armed: boolean, problem: any, nowMs?: number}} f
|
|
248
|
+
* `problem`: the newest fire when it started nothing (busy, monthly-exhausted, refused), else null.
|
|
249
|
+
* @returns {{line1: string, line2: string|null, red: boolean}}
|
|
250
|
+
*/
|
|
251
|
+
export function scheduleBlockWords(f) {
|
|
252
|
+
const head = scheduleHead(f.trigger, f.home);
|
|
253
|
+
const tr = f.trigger;
|
|
254
|
+
if (head === null || tr === null) return { line1: 'By hand', line2: null, red: false };
|
|
255
|
+
const file = tr.kind === 'new-file' || tr.kind === 'file-updated';
|
|
256
|
+
const next = file ? 'next: waiting for a new file' : (f.nextSlotMs !== null && f.armed ? `next ${dayStamp(f.nextSlotMs)}` : 'next: not armed yet');
|
|
257
|
+
const p = f.problem;
|
|
258
|
+
if (p) {
|
|
259
|
+
const stamp = `last ${shortStamp(p.at)}`;
|
|
260
|
+
if (p.outcome === 'busy') return { line1: head, line2: `${stamp} · busy: ${SCHEDULE_BUSY_SAY} · ${next}`, red: false };
|
|
261
|
+
if (p.outcome === 'monthly-exhausted') {
|
|
262
|
+
const month = (/** @type {number} */ ms) => { const d = new Date(ms); return d.getFullYear() * 12 + d.getMonth(); };
|
|
263
|
+
const still = month(Date.parse(p.at)) === month(f.nowMs ?? Date.now());
|
|
264
|
+
return { line1: head, line2: `${stamp} · ${monthUsedUpWords(p)} · ${still ? 'next: raise the limit in Settings, or it starts on the 1st' : next}`, red: still };
|
|
265
|
+
}
|
|
266
|
+
return { line1: head, line2: `${stamp} · refused: ${String(p.why ?? 'it was refused').trim()} · next: fix it in Cancel / modify schedule`, red: true };
|
|
267
|
+
}
|
|
268
|
+
if (!f.last) return { line1: head, line2: `no run yet · ${next}`, red: false };
|
|
269
|
+
const outcome = f.last.outcome === 'waiting' ? 'waiting for your answer (the next run does not wait)' : `${f.last.outcome}${f.last.catchUp ? ' · catch-up' : ''}`;
|
|
270
|
+
return { line1: head, line2: `last ${shortStamp(f.last.at)} · ${f.last.runId} · ${outcome} · ${next}`, red: false };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** M9 am9 item 3: a `refused` fire's reason ends `Nothing started.` once, whether the scheduler already said so or not. @param {unknown} why */
|
|
274
|
+
function refusedWords(why) {
|
|
275
|
+
const w = String(why ?? 'it was refused').trim().replace(/\s*Nothing started\.?$/, '').replace(/\.$/, '');
|
|
276
|
+
return `refused: ${w || 'it was refused'}. Nothing started.`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Fires that started nothing (the three outcomes of am8), the only rows `tries` may hold. @param {any} row */
|
|
280
|
+
export const startedNothing = (row) => row && (row.outcome === 'busy' || row.outcome === 'monthly-exhausted' || row.outcome === 'refused');
|
|
281
|
+
|
|
282
|
+
/** M9 am16/am18: a file that waited when the schedule changed and so will not run; its row carries the signed sentence in `say`. @param {any} row */
|
|
283
|
+
const droppedByChange = (row) => !!row && row.outcome === 'schedule-changed' && typeof row.say === 'string';
|
|
284
|
+
|
|
285
|
+
/** M9 am9 item 3: one `tries` row's words, after its stamp; the am8 owners' sentences. @param {any} row a triggers.jsonl row that started nothing */
|
|
286
|
+
function triesRowWords(row) {
|
|
287
|
+
if (droppedByChange(row)) return row.say;
|
|
288
|
+
if (row.outcome === 'busy') return `busy: ${String(row.say ?? SCHEDULE_BUSY_SAY).replace(/\.$/, '')}. Nothing started.`;
|
|
289
|
+
if (row.outcome === 'monthly-exhausted') return `${monthUsedUpWords(row)}. Nothing started.`;
|
|
290
|
+
return refusedWords(row.why);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** M9 am9 item 4: at most this many `tries` show. */
|
|
294
|
+
export const SCHEDULE_TRIES_SHOWN = 7;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* M9 am9 / am11: a run's Audit `schedule` section (one for every run of a job that has a schedule NOW; `null` when it has none). Folded head
|
|
298
|
+
* `<sign> <schedule words> · from run-7 · 2 tries started nothing`; unfolded rows `this run` and `tries` (stamp + words each).
|
|
299
|
+
* The schedule words are the SAME `line1` the Run tab's SCHEDULE block carries; the sign is `[▶]` while a run of the job is running, `[✗]` when that block is red, else `[·]` (am11).
|
|
300
|
+
* @param {{trigger: Trigger|null, runId: string, from: string|null, rec: any, startMs: number|null, block: {line1: string, red: boolean}, running: boolean, fires: any[]}} f
|
|
301
|
+
* `rec`: this run's trigger.json or null (a hand run); `fires`: every triggers.jsonl row of the job, across every signing.
|
|
302
|
+
* @returns {{sign: string, head: string, rows: Array<{label: string, stamp: string, text: string}>}|null}
|
|
303
|
+
*/
|
|
304
|
+
export function scheduleAuditWords(f) {
|
|
305
|
+
// M9 am18: the am16 `schedule-changed` rows are listed with the tries, also after the schedule is cancelled (no trigger now, such rows exist)
|
|
306
|
+
if (f.trigger === null && !f.fires.some(droppedByChange)) return null;
|
|
307
|
+
const tries = f.fires.filter(startedNothing).reverse();
|
|
308
|
+
const listed = f.fires.filter((r) => startedNothing(r) || droppedByChange(r)).reverse();
|
|
309
|
+
const sign = f.running ? '[\u25B6]' : (f.block.red ? '[\u2717]' : '[\u00B7]');
|
|
310
|
+
const k = tries.length;
|
|
311
|
+
const head = [f.block.line1, f.from ? `from ${f.from}` : null, k > 0 ? `${k} ${k === 1 ? 'try' : 'tries'} started nothing` : null].filter((x) => x !== null).join(' \u00b7 ');
|
|
312
|
+
const kind = startedKind(f.rec);
|
|
313
|
+
let what = 'by hand.';
|
|
314
|
+
if (kind === 'clock') what = `started by the schedule \u00b7 ${f.rec.words ?? f.rec.trigger ?? 'a schedule'}${f.rec.catchUp === true ? ' \u00b7 catch-up' : ''}`;
|
|
315
|
+
else if (kind === 'file') {
|
|
316
|
+
const name = typeof f.rec.file === 'string' ? basename(f.rec.file) : '';
|
|
317
|
+
what = `started by the schedule \u00b7 ${f.rec.triggerKind === 'file-updated' ? 'file update' : 'new file'}${name ? ` ${name}` : ''}`;
|
|
318
|
+
}
|
|
319
|
+
if (f.from !== null && f.from === f.runId) what += kind === null ? ' The schedule was set from this run.' : '. The schedule was set from this run.';
|
|
320
|
+
/** @type {Array<{label: string, stamp: string, text: string}>} */
|
|
321
|
+
const rows = [{ label: 'this run', stamp: f.startMs === null ? '' : shortStamp(f.startMs), text: what }];
|
|
322
|
+
const shown = listed.slice(0, SCHEDULE_TRIES_SHOWN);
|
|
323
|
+
if (shown.length === 0) rows.push({ label: 'tries', stamp: '', text: 'none' });
|
|
324
|
+
else shown.forEach((r, i) => rows.push({ label: i === 0 ? 'tries' : '', stamp: shortStamp(r.at), text: triesRowWords(r) }));
|
|
325
|
+
return { sign, head, rows };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* M11 am11 items 3-4: the Audit row that says why a card left Needs you: `removed from Needs you by you · 10/15 08:15`, or `removed from Needs you after 7 days`. `null` when it did not.
|
|
330
|
+
* @param {{by: 'you', at: string}|{by: 'age'}|null} left
|
|
331
|
+
*/
|
|
332
|
+
export function needsLeftWords(left) {
|
|
333
|
+
if (left === null) return null;
|
|
334
|
+
return left.by === 'you' ? `removed from Needs you by you \u00b7 ${shortStamp(left.at)}` : 'removed from Needs you after 7 days';
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** ENDED (am7 item 1): `10/14 08:16 · passed · Goal met.`. The stamp is the run's local end time; the word is the run's outcome word (none for a run ended on purpose). @param {string|null} at ISO @param {string|null} word @param {string} reason */
|
|
338
|
+
export function endedLineWords(at, word, reason) {
|
|
339
|
+
const stamp = at ? shortStamp(at) : '';
|
|
340
|
+
return [stamp || null, word || null, reason].filter((x) => x !== null).join(' · ');
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** M9 am12: what a stopped run with no kept copy of its job says where Resume would be (a run made before runs kept one). The one owner of these words. */
|
|
344
|
+
export const NO_COPY_SAY = 'This run started before runs kept their own copy of the job, so it cannot carry on. Start a new run.';
|
package/src/trigger-clock.js
CHANGED
|
@@ -12,12 +12,14 @@
|
|
|
12
12
|
// { id:'fire:<slotMs>', kind:'fire', slot, at, catchUp, missed } one run started for that slot
|
|
13
13
|
// Starting a run is NOT this file; the caller does it after `recordFire`.
|
|
14
14
|
import {
|
|
15
|
-
|
|
15
|
+
mkdirSync,
|
|
16
16
|
} from 'node:fs';
|
|
17
17
|
import { dirname } from 'node:path';
|
|
18
18
|
|
|
19
|
+
import { appendRow, ledgerText } from './jsonl-append.js';
|
|
20
|
+
|
|
19
21
|
/** @typedef {{kind:'daily'|'weekdays', hour:number, minute:number} | {kind:'weekly', day:string, hour:number, minute:number}} Schedule */
|
|
20
|
-
/** @typedef {{id:string, kind:'armed'|'fire', at:number, slot?:number, catchUp?:boolean, missed?:number}} ClockRow */
|
|
22
|
+
/** @typedef {{id:string, kind:'armed'|'fire', at:number, slot?:number, catchUp?:boolean, missed?:number, sig?:string}} ClockRow */
|
|
21
23
|
/** @typedef {{fire:boolean, catchUp:boolean, missed:number, slot?:number}} Due */
|
|
22
24
|
|
|
23
25
|
/** A slot seen MORE than this after its set time is a catch-up (M9 am2 item 1). Exactly this late is on time. */
|
|
@@ -114,29 +116,29 @@ export function nextSlot(schedule, afterMs) {
|
|
|
114
116
|
return /** @type {number} */ (found);
|
|
115
117
|
}
|
|
116
118
|
|
|
117
|
-
/** @param {string} file @returns {ClockRow[]} every row on disk, oldest first; a missing file is no rows. */
|
|
118
|
-
export function readRows(file) {
|
|
119
|
-
|
|
120
|
-
return
|
|
119
|
+
/** @param {string} file @param {string} [sig] M9 am20: only the rows of that schedule signing (the one schedule ledger carries every signing's rows); absent = every row @returns {ClockRow[]} every row on disk that parses, oldest first; a missing file is no rows; a line a crash tore mid-append is skipped (`appendOnce` starts each append on a fresh line, so it never glues onto the next row). */
|
|
120
|
+
export function readRows(file, sig) {
|
|
121
|
+
const rows = ledgerText(file).split('\n').filter(Boolean).flatMap((l) => { try { return [JSON.parse(l)]; } catch { return []; } });
|
|
122
|
+
return sig === undefined ? rows : rows.filter((r) => r.sig === sig);
|
|
121
123
|
}
|
|
122
124
|
|
|
123
|
-
/** Append one row; a row whose id is already on disk is refused (write-once). @param {string} file @param {ClockRow} row */
|
|
124
|
-
function appendOnce(file, row) {
|
|
125
|
-
if (readRows(file).some((r) => r.id === row.id)) throw new Error(`ledger row ${row.id} already written`);
|
|
125
|
+
/** Append one row; a row whose id is already on disk (under the same signing) is refused (write-once). @param {string} file @param {ClockRow} row @param {string} [sig] stamped on the row (M9 am20) */
|
|
126
|
+
function appendOnce(file, row, sig) {
|
|
127
|
+
if (readRows(file, sig).some((r) => r.id === row.id)) throw new Error(`ledger row ${row.id} already written`);
|
|
126
128
|
mkdirSync(dirname(file), { recursive: true });
|
|
127
|
-
|
|
129
|
+
appendRow(file, sig === undefined ? row : { ...row, sig });
|
|
128
130
|
}
|
|
129
131
|
|
|
130
|
-
/** The trigger was set at `now`: no slot before this ever fires. @param {string} file @param {number} now */
|
|
131
|
-
export function armTrigger(file, now) {
|
|
132
|
-
appendOnce(file, { id: `armed:${now}`, kind: 'armed', at: now });
|
|
132
|
+
/** The trigger was set at `now`: no slot before this ever fires. @param {string} file @param {number} now @param {string} [sig] */
|
|
133
|
+
export function armTrigger(file, now, sig) {
|
|
134
|
+
appendOnce(file, { id: `armed:${now}`, kind: 'armed', at: now }, sig);
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
/**
|
|
136
138
|
* Record that the run for this due slot is starting. Call BEFORE starting the run: a crash loses a run, never doubles one.
|
|
137
|
-
* @param {string} file @param {Due} d the result of `due` with fire true @param {number} now
|
|
139
|
+
* @param {string} file @param {Due} d the result of `due` with fire true @param {number} now @param {string} [sig]
|
|
138
140
|
*/
|
|
139
|
-
export function recordFire(file, d, now) {
|
|
141
|
+
export function recordFire(file, d, now, sig) {
|
|
140
142
|
if (!d.fire || d.slot === undefined) throw new Error('recordFire needs a due result with fire true');
|
|
141
|
-
appendOnce(file, { id: `fire:${d.slot}`, kind: 'fire', slot: d.slot, at: now, catchUp: d.catchUp, missed: d.missed });
|
|
143
|
+
appendOnce(file, { id: `fire:${d.slot}`, kind: 'fire', slot: d.slot, at: now, catchUp: d.catchUp, missed: d.missed }, sig);
|
|
142
144
|
}
|
package/src/trigger-files.js
CHANGED
|
@@ -14,29 +14,31 @@
|
|
|
14
14
|
// one. 'dropped' = it was deleted / changed before its turn. A sha with a `ran` row never arrives again, whatever its name or mtime; see `arrives` for baseline / queued / dropped.
|
|
15
15
|
// poll() writes nothing: it only reports finished files whose sha is new.
|
|
16
16
|
import {
|
|
17
|
-
|
|
17
|
+
existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync,
|
|
18
18
|
} from 'node:fs';
|
|
19
19
|
import { basename, dirname, join } from 'node:path';
|
|
20
20
|
import { createHash } from 'node:crypto';
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
import { appendRow, ledgerText } from './jsonl-append.js';
|
|
23
|
+
|
|
24
|
+
/** @typedef {{id:string, kind:'baseline'|'arrived'|'ran'|'dropped', sha:string, name:string, size:number, at:number, path?:string, why?:string, of?:string, sig?:string}} SeenRow */
|
|
23
25
|
/** @typedef {{path:string, sha:string, size:number}} ToRun */
|
|
24
26
|
/** @typedef {{path:string, why:string}} Refused */
|
|
25
27
|
|
|
26
28
|
/** Consecutive polls with the same size and mtime before a file is taken. (Two matches in a row = taken on the third sighting.) */
|
|
27
29
|
export const STABLE_POLLS = 2;
|
|
28
30
|
|
|
29
|
-
/** @param {string} file @returns {SeenRow[]} */
|
|
30
|
-
export function readSeen(file) {
|
|
31
|
-
|
|
32
|
-
return
|
|
31
|
+
/** Every row that parses; a line a crash tore mid-append is skipped (it counts as not written). `appendSeen` starts each append on a fresh line, so a torn line can never glue onto the next row. @param {string} file @param {string} [sig] M9 am20: only the rows of that schedule signing (the one schedule ledger carries every signing's rows); absent = every row @returns {SeenRow[]} */
|
|
32
|
+
export function readSeen(file, sig) {
|
|
33
|
+
const rows = ledgerText(file).split('\n').filter(Boolean).flatMap((l) => { try { return [JSON.parse(l)]; } catch { return []; } });
|
|
34
|
+
return sig === undefined ? rows : rows.filter((r) => r.sig === sig);
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
/** The ONE appender: write-once by id (a second row with the same id is not written). @param {string} file @param {SeenRow} row @returns {boolean} written */
|
|
36
|
-
function appendSeen(file, row) {
|
|
37
|
-
if (readSeen(file).some((r) => r.id === row.id)) return false;
|
|
37
|
+
/** The ONE appender: write-once by id within a signing (a second row with the same id is not written). Starts on a fresh line when the file does not end in a newline (a torn last line). @param {string} file @param {SeenRow} row @param {string} [sig] stamped on the row @returns {boolean} written */
|
|
38
|
+
function appendSeen(file, row, sig) {
|
|
39
|
+
if (readSeen(file, sig).some((r) => r.id === row.id)) return false;
|
|
38
40
|
mkdirSync(dirname(file), { recursive: true });
|
|
39
|
-
|
|
41
|
+
appendRow(file, sig === undefined ? row : { ...row, sig });
|
|
40
42
|
return true;
|
|
41
43
|
}
|
|
42
44
|
|
|
@@ -49,30 +51,30 @@ const waiting = (rows, a) => a.kind === 'arrived'
|
|
|
49
51
|
/**
|
|
50
52
|
* 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
53
|
* 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
|
|
53
|
-
* @param {SeenRow[]} rows @param {{sha:string
|
|
54
|
+
* 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).
|
|
55
|
+
* @param {SeenRow[]} rows @param {{sha:string}} it @param {'new'|'updated'} mode
|
|
54
56
|
*/
|
|
55
57
|
function arrives(rows, it, mode) {
|
|
56
58
|
const mine = ofSha(rows, it.sha);
|
|
57
59
|
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
|
|
60
|
+
const bi = rows.findLastIndex((r) => r.kind === 'baseline' && r.sha === it.sha);
|
|
59
61
|
if (bi < 0) return true;
|
|
60
|
-
return mode === 'updated' && rows.slice(bi + 1).some((r) => r.kind
|
|
62
|
+
return mode === 'updated' && rows.slice(bi + 1).some((r) => (r.kind === 'arrived' || r.kind === 'ran' || r.kind === 'dropped') && r.sha !== it.sha); // file rows only: the ledger also holds `armed` / `fire` / `cancelled` rows (M9 am20)
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
/** Arrived rows are never overwritten: the 2nd arrival of a sha (after a drop) gets its own id. @param {string} ledger @param {ToRun & {name?:string}} item @param {number} at the scheduler's clock, never the file's mtime */
|
|
64
|
-
export function recordArrived(ledger, item, at) {
|
|
65
|
-
const rows = readSeen(ledger);
|
|
65
|
+
/** Arrived rows are never overwritten: the 2nd arrival of a sha (after a drop) gets its own id. @param {string} ledger @param {ToRun & {name?:string}} item @param {number} at the scheduler's clock, never the file's mtime @param {string} [sig] */
|
|
66
|
+
export function recordArrived(ledger, item, at, sig) {
|
|
67
|
+
const rows = readSeen(ledger, sig);
|
|
66
68
|
if (rows.some((r) => r.sha === item.sha && (r.kind === 'ran' || waiting(rows, r)))) return false; // one writer decides: ran or already queued
|
|
67
69
|
const n = rows.filter((r) => r.kind === 'arrived' && r.sha === item.sha).length;
|
|
68
|
-
return appendSeen(ledger, { id: n ? `arrived:${item.sha}:${n}` : `arrived:${item.sha}`, kind: 'arrived', sha: item.sha, name: basename(item.path), size: item.size, path: item.path, at });
|
|
70
|
+
return appendSeen(ledger, { id: n ? `arrived:${item.sha}:${n}` : `arrived:${item.sha}`, kind: 'arrived', sha: item.sha, name: basename(item.path), size: item.size, path: item.path, at }, sig);
|
|
69
71
|
}
|
|
70
|
-
/** Call immediately before the run is started. @param {string} ledger @param {{sha:string, path:string, size:number}} item @param {number} at */
|
|
71
|
-
export const recordRan = (ledger, item, at) => appendSeen(ledger, { id: `ran:${item.sha}`, kind: 'ran', sha: item.sha, name: basename(item.path), size: item.size, at });
|
|
72
|
-
/** `of` = the arrived row it cancels (item.id when a queue row is passed). @param {string} ledger @param {{sha:string, path:string, size:number, id?:string}} item @param {'deleted'|'changed'} why @param {number} at */
|
|
73
|
-
export function recordDropped(ledger, item, why, at) {
|
|
72
|
+
/** Call immediately before the run is started. @param {string} ledger @param {{sha:string, path:string, size:number}} item @param {number} at @param {string} [sig] */
|
|
73
|
+
export const recordRan = (ledger, item, at, sig) => appendSeen(ledger, { id: `ran:${item.sha}`, kind: 'ran', sha: item.sha, name: basename(item.path), size: item.size, at }, sig);
|
|
74
|
+
/** `of` = the arrived row it cancels (item.id when a queue row is passed). @param {string} ledger @param {{sha:string, path:string, size:number, id?:string}} item @param {'deleted'|'changed'|'schedule-changed'|'carried'} why @param {number} at @param {string} [sig] */
|
|
75
|
+
export function recordDropped(ledger, item, why, at, sig) {
|
|
74
76
|
const of = item.id ?? `arrived:${item.sha}`;
|
|
75
|
-
return appendSeen(ledger, { id: `dropped:${of}`, kind: 'dropped', sha: item.sha, name: basename(item.path), size: item.size, why, at, of });
|
|
77
|
+
return appendSeen(ledger, { id: `dropped:${of}`, kind: 'dropped', sha: item.sha, name: basename(item.path), size: item.size, why, at, of }, sig);
|
|
76
78
|
}
|
|
77
79
|
|
|
78
80
|
/**
|
|
@@ -123,9 +125,20 @@ export function checkTarget(mode, target) {
|
|
|
123
125
|
}
|
|
124
126
|
|
|
125
127
|
/**
|
|
126
|
-
*
|
|
128
|
+
* M9 am16: does a trigger on this target still watch this file? New: the file sits directly in the same (real) folder. Updated: it is the same (real) file. False when the target cannot be resolved.
|
|
129
|
+
* @param {'new'|'updated'} mode @param {string} target @param {string} filePath a queued file's path as the watcher recorded it (a real path)
|
|
130
|
+
*/
|
|
131
|
+
export function watches(mode, target, filePath) {
|
|
132
|
+
try {
|
|
133
|
+
if (mode === 'new') return realpathSync(target) === dirname(filePath);
|
|
134
|
+
return join(realpathSync(dirname(target)), basename(target)) === filePath;
|
|
135
|
+
} catch { return false; }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @param {{mode:'new'|'updated', target:string, ledger:string, sig?:string}} opts target = folder (new) or file (updated); `sig` (M9 am20) = the schedule signing every row this trigger reads and writes belongs to
|
|
127
140
|
*/
|
|
128
|
-
export function createFileTrigger({ mode, target, ledger }) {
|
|
141
|
+
export function createFileTrigger({ mode, target, ledger, sig }) {
|
|
129
142
|
if (mode !== 'new' && mode !== 'updated') throw new Error(`file trigger mode "${mode}" is not one of new, updated`);
|
|
130
143
|
/** @type {Map<string, {key:string, same:number, takenKey:string|null}>} */
|
|
131
144
|
const track = new Map();
|
|
@@ -166,7 +179,7 @@ export function createFileTrigger({ mode, target, ledger }) {
|
|
|
166
179
|
for (const p of candidates().files) {
|
|
167
180
|
const buf = readFileSync(p);
|
|
168
181
|
const sha = sha256(buf);
|
|
169
|
-
appendSeen(ledger, { id: `baseline:${sha}:${basename(p)}`, kind: 'baseline', sha, name: basename(p), size: buf.length, at: Date.now() }); // write-once: re-arming adds nothing
|
|
182
|
+
appendSeen(ledger, { id: `baseline:${sha}:${basename(p)}`, kind: 'baseline', sha, name: basename(p), size: buf.length, at: Date.now() }, sig); // write-once: re-arming adds nothing
|
|
170
183
|
}
|
|
171
184
|
}
|
|
172
185
|
|
|
@@ -176,7 +189,7 @@ export function createFileTrigger({ mode, target, ledger }) {
|
|
|
176
189
|
const st2 = lstatSync(p);
|
|
177
190
|
if (`${st2.size}:${st2.mtimeMs}` !== t.key) return 'changed'; // changed while reading: retry next poll
|
|
178
191
|
const sha = sha256(buf);
|
|
179
|
-
if (!arrives(readSeen(ledger), { sha
|
|
192
|
+
if (!arrives(readSeen(ledger, sig), { sha }, mode)) return null; // ran, still queued, or still the starting point
|
|
180
193
|
return { path: p, sha, size: buf.length };
|
|
181
194
|
}
|
|
182
195
|
|
package/src/trigger-line.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// M9 piece 3 (docs/wiki/the-module-ladder.md, "M9" item 1, am1): the trigger as typed data in the signed arbiter block.
|
|
2
|
-
// Pure text: the grammar of the one `guardrail: trigger ...` line
|
|
2
|
+
// Pure text: the grammar of the one `guardrail: trigger ...` line. M9 am20: a schedule is its own signed file (src/schedule.js); NOTHING writes this line any more.
|
|
3
|
+
// The signed-text parser still accepts it so a job signed before am20 still reads and verifies, but nothing acts on it: such a job is By hand.
|
|
3
4
|
//
|
|
4
5
|
// guardrail: trigger daily 09:00
|
|
5
6
|
// guardrail: trigger weekdays 09:00
|
|
@@ -63,23 +64,13 @@ export function stripTrigger(prose) {
|
|
|
63
64
|
return prose.split('\n').filter((l) => !LINE_RE.test(l)).join('\n');
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
/**
|
|
67
|
-
* The ONE writer of the trigger line: the prose with its trigger line replaced by this trigger's (or removed for null). With no trigger
|
|
68
|
-
* in and none asked for, the text is returned unchanged. The arbiter block is last in a signed text, so the line goes at the end.
|
|
69
|
-
* @param {string} prose @param {Trigger|null} trigger
|
|
70
|
-
*/
|
|
71
|
-
export function proseWithTrigger(prose, trigger) {
|
|
72
|
-
const stripped = stripTrigger(prose);
|
|
73
|
-
if (trigger === null) return stripped;
|
|
74
|
-
return `${stripped.replace(/\s*$/, '')}\n${triggerLine(trigger)}\n`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
67
|
/**
|
|
78
68
|
* What the human's second click carries: binds the job, the exact signature it was shown at and the trigger.
|
|
79
|
-
* @param {{flow: string, flowSignatureHash: string, trigger: Trigger|null}} a
|
|
69
|
+
* @param {{flow: string, flowSignatureHash: string, trigger: Trigger|null, model?: {slot: string, model: string}|null, destination?: string|null, inputs?: {role: string, path: string}[]|null, plan?: {run: string, flowHash: string, declSha: string}|null}} a
|
|
80
70
|
*/
|
|
81
|
-
export function triggerHash({ flow, flowSignatureHash, trigger }) {
|
|
82
|
-
|
|
71
|
+
export function triggerHash({ flow, flowSignatureHash, trigger, model = null, destination = null, inputs = null, plan = null }) {
|
|
72
|
+
// M6 am5 item 3: the scheduled model is part of what the click signs; absent = the exact bytes (and hashes) a trigger had before
|
|
73
|
+
return createHash('sha256').update(JSON.stringify({ m9: 'trigger', flow, flowSignatureHash, trigger, ...(model ? { model } : {}), ...(destination ? { destination } : {}), ...(inputs ? { inputs } : {}), ...(plan ? { plan } : {}) })).digest('hex');
|
|
83
74
|
}
|
|
84
75
|
|
|
85
76
|
/** M9: the file a triggered start writes (once, before the spawn) into the run's own folder: why this run started. `appendHistory` copies it into the run's history row. */
|
package/src/types.js
CHANGED
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
* @property {string[]} skills - `skills <name>[, <name>...]`. Default ['core'].
|
|
50
50
|
* @property {SourceSlot[]} sources - zero or more `source <role> = file:<path>`.
|
|
51
51
|
* @property {number} roundBudgetMs - `round budget <int><s|m|h>`. Default 120000.
|
|
52
|
-
* @property {import("./trigger-line.js").Trigger|null} trigger - `trigger <kind> ...` (M9). Default null = By hand.
|
|
52
|
+
* @property {import("./trigger-line.js").Trigger|null} trigger - `trigger <kind> ...` (M9). Default null = By hand. M9 am20: read for jobs signed before, NEVER acted on (a schedule is `schedule.json`).
|
|
53
53
|
*/
|
|
54
54
|
|
|
55
55
|
/**
|
package/types/authoring.d.ts
CHANGED
|
@@ -149,9 +149,11 @@ export function draftToDir({ proseFile, dir, root, name, slot, model, draftThink
|
|
|
149
149
|
* `sessionDir` (the panel's draft folder: card, notes, every change) widens the setup record; the CLI has none, so its record is the one plan folder.
|
|
150
150
|
* M6: `replaces: {flowHash}` (a draft opened from an existing signed job, carrying that job's signature hash) makes this sign REPLACE that job
|
|
151
151
|
* (`writeFlow` checks the hash is still the job's); without it a taken name is refused as ever.
|
|
152
|
-
*
|
|
152
|
+
* M6 am9: `asRunOf: {flow}` (a red run's redraft of an UNCHANGED job) signs the plan as the plan of a NEW run of that existing workflow: the workflow's own files are
|
|
153
|
+
* never written (`writeRunPlan`), and a plan whose job key is not the workflow's is refused here, at sign time.
|
|
154
|
+
* @param {{dir: any, approve?: any, signedBy: any, signedAt?: string, env?: Record<string, string|undefined>, keyNames?: string[], sessionDir?: string, replaces?: {flowHash: string}, asRunOf?: {flow: string}}} a
|
|
153
155
|
*/
|
|
154
|
-
export function signDraft({ dir, approve, signedBy, signedAt, env, keyNames, sessionDir, replaces, }: {
|
|
156
|
+
export function signDraft({ dir, approve, signedBy, signedAt, env, keyNames, sessionDir, replaces, asRunOf, }: {
|
|
155
157
|
dir: any;
|
|
156
158
|
approve?: any;
|
|
157
159
|
signedBy: any;
|
|
@@ -162,9 +164,34 @@ export function signDraft({ dir, approve, signedBy, signedAt, env, keyNames, ses
|
|
|
162
164
|
replaces?: {
|
|
163
165
|
flowHash: string;
|
|
164
166
|
};
|
|
167
|
+
asRunOf?: {
|
|
168
|
+
flow: string;
|
|
169
|
+
};
|
|
165
170
|
}): {
|
|
166
171
|
ok: boolean;
|
|
167
172
|
reds: any[];
|
|
173
|
+
} | {
|
|
174
|
+
ok: boolean;
|
|
175
|
+
flowDir: string;
|
|
176
|
+
runId: string;
|
|
177
|
+
runDir: string;
|
|
178
|
+
signature: any;
|
|
179
|
+
setup: {
|
|
180
|
+
ok: true;
|
|
181
|
+
file: string;
|
|
182
|
+
rows: number;
|
|
183
|
+
} | {
|
|
184
|
+
ok: false;
|
|
185
|
+
red: string;
|
|
186
|
+
};
|
|
187
|
+
modelRecord: {
|
|
188
|
+
ok: boolean;
|
|
189
|
+
recorded: boolean;
|
|
190
|
+
};
|
|
191
|
+
modelPick: {
|
|
192
|
+
slot: string;
|
|
193
|
+
model: string;
|
|
194
|
+
} | null;
|
|
168
195
|
} | {
|
|
169
196
|
ok: boolean;
|
|
170
197
|
flowDir: string;
|
|
@@ -181,6 +208,9 @@ export function signDraft({ dir, approve, signedBy, signedAt, env, keyNames, ses
|
|
|
181
208
|
ok: boolean;
|
|
182
209
|
recorded: boolean;
|
|
183
210
|
};
|
|
211
|
+
runId?: undefined;
|
|
212
|
+
runDir?: undefined;
|
|
213
|
+
modelPick?: undefined;
|
|
184
214
|
};
|
|
185
215
|
export const SPEC_HASH_FILE: "spec.hash";
|
|
186
216
|
/** The one folder under the flows root where the panel keeps its draft folders (M4e). Starts with a dot, so it is never a flow name. */
|