fwdloop 0.19.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +2 -0
  2. package/bin/fwdloop +22 -13
  3. package/package.json +1 -1
  4. package/src/audit-fold.js +177 -0
  5. package/src/authoring.js +24 -4
  6. package/src/books.js +4 -3
  7. package/src/closers.js +49 -2
  8. package/src/ctx-tools.js +1 -1
  9. package/src/diary.js +49 -0
  10. package/src/flow.js +218 -2
  11. package/src/flowmodel.js +42 -5
  12. package/src/jobkey.js +25 -0
  13. package/src/jsonl-append.js +29 -0
  14. package/src/model-step.js +103 -5
  15. package/src/monthly.js +3 -2
  16. package/src/panel/author.js +44 -18
  17. package/src/panel/authorcard.js +7 -13
  18. package/src/panel/authorflows.js +118 -24
  19. package/src/panel/authorresume.js +8 -4
  20. package/src/panel/authorstart.js +63 -18
  21. package/src/panel/authortrigger.js +250 -125
  22. package/src/panel/authorvalues.js +13 -2
  23. package/src/panel/data.js +279 -43
  24. package/src/panel/index.html +547 -150
  25. package/src/panel/needsyou.js +29 -0
  26. package/src/panel/scheduler.js +21 -22
  27. package/src/panel/server.js +30 -6
  28. package/src/primitives.js +44 -2
  29. package/src/provider.js +3 -2
  30. package/src/runner.js +58 -22
  31. package/src/runvalues.js +10 -2
  32. package/src/schedule.js +135 -0
  33. package/src/setup.js +0 -21
  34. package/src/summary-words.js +180 -7
  35. package/src/thinking.js +39 -0
  36. package/src/trigger-clock.js +18 -16
  37. package/src/trigger-files.js +37 -24
  38. package/src/trigger-line.js +6 -15
  39. package/src/try-checks.js +62 -0
  40. package/src/types.js +2 -1
  41. package/types/audit-fold.d.ts +54 -0
  42. package/types/authoring.d.ts +33 -2
  43. package/types/books.d.ts +17 -0
  44. package/types/ctx-tools.d.ts +1 -0
  45. package/types/diary.d.ts +57 -0
  46. package/types/flow.d.ts +106 -0
  47. package/types/flowmodel.d.ts +25 -3
  48. package/types/jobkey.d.ts +11 -0
  49. package/types/jsonl-append.d.ts +6 -0
  50. package/types/model-step.d.ts +4 -1
  51. package/types/panel/author.d.ts +47 -17
  52. package/types/panel/authorcard.d.ts +3 -6
  53. package/types/panel/authorflows.d.ts +38 -10
  54. package/types/panel/authorstart.d.ts +15 -2
  55. package/types/panel/authortrigger.d.ts +73 -41
  56. package/types/panel/authorvalues.d.ts +6 -0
  57. package/types/panel/data.d.ts +101 -19
  58. package/types/panel/needsyou.d.ts +20 -0
  59. package/types/runvalues.d.ts +5 -0
  60. package/types/schedule.d.ts +69 -0
  61. package/types/setup.d.ts +0 -19
  62. package/types/summary-words.d.ts +105 -2
  63. package/types/thinking.d.ts +31 -0
  64. package/types/trigger-clock.d.ts +8 -7
  65. package/types/trigger-files.d.ts +17 -10
  66. package/types/trigger-line.d.ts +16 -8
  67. package/types/try-checks.d.ts +55 -0
  68. package/types/types.d.ts +10 -1
@@ -22,15 +22,16 @@
22
22
  // arrive together run one after the other and the second sees the first's `starting` start and is refused.
23
23
  import { randomBytes } from 'node:crypto';
24
24
  import {
25
- existsSync, mkdirSync, realpathSync, rmdirSync, writeFileSync,
25
+ existsSync, mkdirSync, realpathSync, rmdirSync, rmSync, writeFileSync,
26
26
  } from 'node:fs';
27
27
  import { dirname, join } from 'node:path';
28
28
  import { fileURLToPath } from 'node:url';
29
29
 
30
+ import { loadCatalogue } from '../catalogue.js';
30
31
  import { scrub } from '../authoring.js';
31
32
  import { readPidRows } from '../books.js';
32
33
  import {
33
- PANEL_STARTS_DIR, checkRunId, claimRunId, readFileInside, readdirInside, resolveRunDir,
34
+ PANEL_STARTS_DIR, checkRunId, claimRunId, nextRunId, readFileInside, readdirInside, resolveRunDir, runOwnsPlan, copyRunPlan, RUN_PLAN_MARKER,
34
35
  } from '../flow.js';
35
36
  import { booksFresh, runLiveness } from '../liveness.js';
36
37
  import { dropUnstartedValues, writeRunValues } from '../runvalues.js';
@@ -107,6 +108,9 @@ export function createStarter(opts) {
107
108
  return d === null ? null : readStart(join(d, id), id, keys);
108
109
  };
109
110
 
111
+ /** @type {Set<string>} start ids whose child is dead for good: `startingFor` skips them (cost does not grow with old finished starts) */
112
+ const deadStarts = new Set();
113
+
110
114
  /**
111
115
  * 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.
112
116
  * With `runId`, only a start of that run (the Runs list reads a run folder as `[▶]` starting by this same rule).
@@ -116,15 +120,50 @@ export function createStarter(opts) {
116
120
  const sd = startsDir();
117
121
  if (sd === null) return null;
118
122
  for (const id of readdirInside(sd, '.').filter((n) => ID_RE.test(n)).sort().reverse()) {
123
+ if (deadStarts.has(id)) continue;
124
+ // phase `starting` needs a live child (readStart's `alive`), and a child that has a pid.json and is not running never runs again (a pid recorded with its
125
+ // procStart; the no-/proc fallback is a 10-minute window that only closes), so that folder can never be `starting`: it is not read again, nothing is pruned.
126
+ // A folder with no pid.json yet is NOT remembered (a start still being written).
127
+ if (!childRunning(join(sd, id))) {
128
+ if (readJsonFile(join(sd, id), 'pid.json') !== null) deadStarts.add(id);
129
+ continue;
130
+ }
119
131
  const v = readStart(join(sd, id), id, keys);
120
132
  if (v && v.flow === flow && (runId === undefined || v.runId === runId) && v.phase === 'starting') return id;
121
133
  }
122
134
  return null;
123
135
  }
124
136
 
137
+ /**
138
+ * The ONE decider of whether a start can begin, for `start` and for a trigger's pre-check: only the three checks that need no side effect. The flows root exists, the run
139
+ * id is one the CLI would take (`runId` '' = the next `run-<n>`, looked at and NOT claimed; a signed plan's run folder already exists and is not looked at), and no run of
140
+ * this flow is starting. Nothing is written. A refusal is the `{status, body}` `start` returns; null = it can start.
141
+ * @param {{ flow: string, runId: string, planRun?: boolean }} a @param {{ env: Record<string, string|undefined>, keyNames?: string[] }} loaded
142
+ * @returns {{ status: number, body: any }|null}
143
+ */
144
+ function canStart({ flow, runId, planRun = false }, loaded) {
145
+ if (startsDir() === null) return { status: 400, body: { ok: false, refused: 'root', say: 'The flows folder this panel serves does not exist.' } };
146
+ if (!planRun) {
147
+ const flowDir = join(realpathSync(root), flow);
148
+ if (runId === '') {
149
+ const next = nextRunId(flowDir);
150
+ const r = resolveRunDir(flowDir, next);
151
+ if (!r.ok) return { status: 400, body: { ok: false, refused: 'run-id', say: `${r.red.replace(/^run: /, '')}.` } };
152
+ if (existsSync(r.runDir)) return { status: 400, body: { ok: false, refused: 'run-id', say: `There is already a run "${next}" of this flow. Use a new run id.` } };
153
+ } else {
154
+ const run = checkRun(flow, runId);
155
+ if (!run.ok) return { status: 400, body: { ok: false, refused: 'run-id', say: run.say } };
156
+ }
157
+ }
158
+ const startingId = startingFor(flow, providerKeys(loaded));
159
+ 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.' } };
160
+ return null;
161
+ }
162
+
125
163
  return {
126
164
  checkRun,
127
165
  startingFor,
166
+ canStart,
128
167
 
129
168
  /**
130
169
  * Start `fwdloop run` for a flow whose every $0 check already passed at the calling door. Re-checks the run id here (nothing
@@ -134,16 +173,17 @@ export function createStarter(opts) {
134
173
  * folder as `signed-values.json` BEFORE the spawn; the run then uses them (`pickRunValues`). None = the flow's own.
135
174
  * `trigger` (M9): `{kind, reason, catchUp, ...}` for a start a trigger fired (kind 'trigger'): written once into the run's own folder as `trigger.json`
136
175
  * BEFORE the spawn; `appendHistory` copies it into the run's history row (the catch-up mark, am2 item 1).
137
- * @param {{ kind: 'run'|'sign'|'trigger', flow: string, runId: string, sources: {role: string, path: string}[], values?: any, pick?: {slot: string, model: string}|null, trigger?: Record<string, any>|null }} a
176
+ * @param {{ kind: 'run'|'sign'|'trigger', flow: string, runId: string, sources: {role: string, path: string}[], planRun?: boolean, planOf?: string|null, values?: any, pick?: {slot: string, model: string}|null, trigger?: Record<string, any>|null }} a
138
177
  * @param {{ env: Record<string, string|undefined>, keyNames?: string[] }} loaded the keys load: its env is the child's env AND (with its key names) the scrub list
139
178
  * @returns {{ status: number, body: any }}
140
179
  */
141
180
  start({
142
- kind, flow, runId, sources, values = null, pick = null, trigger = null,
181
+ kind, flow, runId, sources, values = null, pick = null, trigger = null, planRun = false, planOf = null,
143
182
  }, loaded) {
144
183
  const { env } = loaded;
145
- const sd = startsDir();
146
- if (sd === null) return { status: 400, body: { ok: false, refused: 'root', say: 'The flows folder this panel serves does not exist.' } };
184
+ const refused = canStart({ flow, runId, planRun }, loaded);
185
+ if (refused !== null) return refused;
186
+ const sd = /** @type {string} */ (startsDir());
147
187
  /** @type {string|null} */
148
188
  let claimedDir = null;
149
189
  if (runId === '') {
@@ -151,21 +191,32 @@ export function createStarter(opts) {
151
191
  if (!c.ok) return { status: 400, body: { ok: false, refused: 'run-id', say: `${c.red.replace(/^run: /, '')}.` } };
152
192
  ({ runId } = c);
153
193
  claimedDir = c.runDir;
154
- } else {
155
- const run = checkRun(flow, runId);
156
- if (!run.ok) return { status: 400, body: { ok: false, refused: 'run-id', say: run.say } };
194
+ } else if (planRun) {
195
+ // M6 am9: the run's folder already exists (signing claimed it and put its plan there); it is a run that keeps its plan, or nothing starts
196
+ const r = resolveRunDir(join(realpathSync(root), flow), runId);
197
+ if (!r.ok || !runOwnsPlan(r.runDir)) return { status: 400, body: { ok: false, refused: 'run-id', say: `"${runId}" is not a run that holds its own plan. ${NOTHING_SPENT}` } };
198
+ claimedDir = r.runDir;
157
199
  }
158
200
  const runDirOf = () => claimedDir ?? join(realpathSync(root), flow, 'runs', runId);
159
201
  const unclaim = () => {
160
- if (claimedDir === null && values === null && trigger === null) return;
202
+ if (planRun) return; // a signed plan's run folder is never removed here
203
+ if (claimedDir === null && values === null && trigger === null && planOf === null) return;
204
+ if (planOf !== null) { try { rmSync(join(runDirOf(), 'job'), { recursive: true, force: true }); rmSync(join(runDirOf(), RUN_PLAN_MARKER), { force: true }); } catch { /* best-effort */ } }
161
205
  dropUnstartedValues(runDirOf());
162
206
  try { rmdirSync(runDirOf()); } catch { /* not empty / gone */ }
163
207
  };
164
- if (values !== null || trigger !== null) {
208
+ if (values !== null || trigger !== null || planOf !== null) {
165
209
  // the run folder holds its signed values (and a triggered start's reason) from before the first step: a claimed one already exists,
166
210
  // a typed id's is made here (exclusively: a folder that appeared meanwhile refuses)
167
211
  try { if (claimedDir === null) mkdirSync(runDirOf()); } catch { return { status: 400, body: { ok: false, refused: 'run-id', say: `There is already a run "${runId}" of this flow. Use a new run id.` } }; }
168
212
  }
213
+ if (planOf !== null) {
214
+ // M6 am9: this run executes the plan another run kept: a verified copy goes into its folder BEFORE the spawn (the source is read through the same safe door)
215
+ const src = resolveRunDir(join(realpathSync(root), flow), planOf);
216
+ const cat = loadCatalogue();
217
+ const c = src.ok && cat.ok ? copyRunPlan({ fromRunDir: src.runDir, runDir: runDirOf(), catalogue: cat.primitives }) : { ok: false, red: 'The plan to reuse cannot be read.' };
218
+ if (!c.ok) { unclaim(); return { status: 409, body: { ok: false, refused: 'plan', say: `${c.red} ${NOTHING_SPENT}` } }; }
219
+ }
169
220
  if (values !== null) {
170
221
  const w = writeRunValues(runDirOf(), { ...values, runId });
171
222
  if (!w.ok) { unclaim(); return { status: 500, body: { ok: false, refused: 'values', say: `The signed values could not be saved. ${NOTHING_SPENT}` } }; }
@@ -173,13 +224,7 @@ export function createStarter(opts) {
173
224
  if (trigger !== null) {
174
225
  try { writeFileSync(join(runDirOf(), TRIGGER_RECORD_FILE), `${JSON.stringify(trigger)}\n`, { flag: 'wx', mode: 0o600 }); } catch { unclaim(); return { status: 500, body: { ok: false, refused: 'trigger', say: `The reason this run started could not be saved. ${NOTHING_SPENT}` } }; }
175
226
  }
176
- const keys = providerKeys(loaded);
177
- // one start at a time while one is still spawning for this flow (a double click, two tabs)
178
- const startingId = startingFor(flow, keys);
179
- if (startingId !== null) {
180
- unclaim();
181
- return { status: 409, body: { ok: false, refused: 'start-live', startId: startingId, say: 'A run of this flow is already starting. Wait for it.' } };
182
- }
227
+ // one start at a time while one is still spawning for this flow (a double click, two tabs): `canStart` above decided it, and nothing between it and the spawn awaits or starts anything
183
228
  const id = newId();
184
229
  const dir = join(sd, id);
185
230
  mkdirSync(sd, { recursive: true, mode: 0o700 });