fwdloop 0.6.0 → 0.7.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 CHANGED
@@ -15,21 +15,24 @@
15
15
  // from poc/, and poc/ is not part of the published package.
16
16
 
17
17
  import { parseArgs } from 'node:util';
18
- import {
19
- existsSync, mkdirSync, readFileSync, readdirSync,
20
- } from 'node:fs';
18
+ import { existsSync, realpathSync } from 'node:fs';
21
19
  import { extname, join, resolve } from 'node:path';
22
20
  import { pathToFileURL } from 'node:url';
23
21
  import { randomUUID } from 'node:crypto';
24
22
 
25
- import { readFlow, checkFlowName } from '../src/flow.js';
23
+ import {
24
+ readFlow, checkFlowName, resolveRunDir, listFlowNames, listRunIds, readFileInside,
25
+ } from '../src/flow.js';
26
26
  import { loadCatalogue } from '../src/catalogue.js';
27
- import { runFlow, resumeRun, makeParkingAskStep } from '../src/runner.js';
27
+ import {
28
+ runFlow, resumeRun, makeParkingAskStep, readAsk,
29
+ } from '../src/runner.js';
28
30
  import { makeLiveModelStep } from '../src/model-step.js';
29
31
  import { sendViaPrimitive } from '../src/send.js';
30
32
  import { resolvePrimitives } from '../src/primitives.js';
31
33
  import { checkKeyPreflight } from '../src/provider.js';
32
- import { answerAsk } from '../src/ask.js';
34
+ import { answerAsk, readAskEvidence } from '../src/ask.js';
35
+ import { panelMain } from '../src/panel/server.js';
33
36
 
34
37
  // The baseline model (project rule, CLAUDE.md/AGENT_RULES: "deepseek-flash
35
38
  // ... Never Claude for fwdloop's own runs") — the ONE provider slot this CLI
@@ -42,6 +45,21 @@ function fail(message) {
42
45
  process.exit(1);
43
46
  }
44
47
 
48
+ /**
49
+ * The ONE place a human-typed `--root` becomes a path (hamr ruling
50
+ * 2026-09-29): resolved to its real path once, at command start. Nothing
51
+ * inside the root is ever followed; that stays under bareguard's symlink
52
+ * refusal.
53
+ */
54
+ function resolveRoot(cmd, given) {
55
+ try {
56
+ return realpathSync(resolve(given));
57
+ } catch (err) {
58
+ if (err.code === 'ENOENT') fail(`${cmd}: --root "${given}" does not exist`);
59
+ fail(`${cmd}: --root "${given}" cannot be resolved: ${err.code ?? err.message}`);
60
+ }
61
+ }
62
+
45
63
  function today() {
46
64
  return new Date().toISOString().slice(0, 10);
47
65
  }
@@ -88,25 +106,23 @@ function parseSourceFlags(list) {
88
106
  return out;
89
107
  }
90
108
 
91
- function listDirNames(dir) {
92
- if (!existsSync(dir)) return [];
93
- return readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
94
- }
95
-
109
+ // F48 round 3 (docs/logs/FINDINGS.md): this CLI must never build a run path
110
+ // by hand — every flow/runId it walks goes through `listFlowNames`/
111
+ // `listRunIds`/`resolveRunDir` (`src/flow.js`, the same checked builders
112
+ // `src/panel/data.js` already uses), and every `ask.json` it reads goes
113
+ // through `readAsk` (`src/runner.js`, itself routed through
114
+ // `readFileInside`) — never a private `join()` + raw `readFileSync`. Closes
115
+ // the CLI-side half of F48: a symlinked flow/run/ask.json (or a hand-placed
116
+ // directory whose name would fail `checkFlowName`/`checkRunId`) is skipped
117
+ // here exactly as it already is by the panel, instead of being followed.
96
118
  function findRunDirByAskId(root, askId) {
97
- for (const flow of listDirNames(root)) {
98
- const runsDir = join(root, flow, 'runs');
99
- for (const runId of listDirNames(runsDir)) {
100
- const askPath = join(runsDir, runId, 'ask.json');
101
- if (!existsSync(askPath)) continue;
102
- let ask;
103
- try {
104
- ask = JSON.parse(readFileSync(askPath, 'utf8'));
105
- } catch {
106
- // eslint-disable-next-line no-continue
107
- continue;
108
- }
109
- if (ask.askId === askId) return join(runsDir, runId);
119
+ for (const flow of listFlowNames(root)) {
120
+ const flowDir = join(root, flow);
121
+ for (const runId of listRunIds(flowDir)) {
122
+ const resolved = resolveRunDir(flowDir, runId);
123
+ if (!resolved.ok) continue; // eslint-disable-line no-continue
124
+ const ask = readAsk(resolved.runDir);
125
+ if (ask && ask.askId === askId) return resolved.runDir;
110
126
  }
111
127
  }
112
128
  return null;
@@ -160,7 +176,7 @@ async function cmdRun(rest) {
160
176
  if (!flowName) fail('run: a flow name is required — fwdloop run <flow> --root <dir> --source <role>=<path> ...');
161
177
  if (!values.root) fail('run: --root <dir> is required');
162
178
 
163
- const root = resolve(values.root);
179
+ const root = resolveRoot('run', values.root);
164
180
  const nameCheck = checkFlowName(flowName);
165
181
  if (!nameCheck.ok) fail(nameCheck.red);
166
182
 
@@ -196,7 +212,9 @@ async function cmdRun(rest) {
196
212
  }
197
213
 
198
214
  const runId = values['run-id'] ?? `run-${Date.now().toString(36)}-${randomUUID().slice(0, 8)}`;
199
- const runDir = join(root, flowName, 'runs', runId);
215
+ const runIdCheck = resolveRunDir(join(root, flowName), runId);
216
+ if (!runIdCheck.ok) fail(runIdCheck.red);
217
+ const { runDir } = runIdCheck;
200
218
  if (existsSync(runDir)) fail(`run: run dir "${runDir}" already exists — use a new --run-id`);
201
219
 
202
220
  // The key preflight (item A: "refuses at $0 BEFORE any book write") runs
@@ -212,12 +230,16 @@ async function cmdRun(rest) {
212
230
 
213
231
  // Predict the frozen input paths `runFlow`'s own `freezeInputs` will use
214
232
  // (deterministic: `<runDir>/inputs/<id><ext>`) so the primitives map can
215
- // be built once, before `runFlow` itself freezes anything.
216
- mkdirSync(join(runDir, 'inputs'), { recursive: true });
233
+ // be built once, before `runFlow` itself freezes anything. Pure path
234
+ // arithmetic — `resolvePrimitives` only needs the STRINGS, never the
235
+ // directory to actually exist yet — so nothing under `runDir` is created
236
+ // here. `runFlow` itself now carries the F46 unwired-verb preflight
237
+ // refusal (moved to `src/runner.js` so any caller — this CLI, M4's web
238
+ // panel — gets it for free); it also owns the ONLY `mkdirSync`/
239
+ // `freezeInputs` for this run dir, so a refusal leaves nothing behind.
217
240
  const predictedInputs = sources.map((s) => ({ id: s.id, frozen: join(runDir, 'inputs', `${s.id}${extname(s.path)}`) }));
218
241
  const allVerbs = [...new Set(declaration.steps.flatMap((s) => s.primitives ?? []))];
219
242
  const { tools: primitives, reds: primitiveReds } = resolvePrimitives(catalogue, allVerbs, { runDir, inputs: predictedInputs });
220
- for (const red of primitiveReds) process.stderr.write(`run: ${red}\n`);
221
243
 
222
244
  const result = await runFlow({
223
245
  root,
@@ -229,6 +251,7 @@ async function cmdRun(rest) {
229
251
  askStep: makeParkingAskStep(),
230
252
  sendStep: sendViaPrimitive,
231
253
  primitives,
254
+ primitiveReds,
232
255
  businessDate: today(),
233
256
  });
234
257
 
@@ -244,19 +267,25 @@ function cmdInbox(rest) {
244
267
  args: rest,
245
268
  options: { root: { type: 'string' } },
246
269
  });
247
- const root = resolve(values.root ?? '.');
248
- if (!existsSync(root)) fail(`inbox: root "${root}" does not exist`);
270
+ const root = resolveRoot('inbox', values.root ?? '.');
249
271
 
250
272
  const rows = [];
251
- for (const flow of listDirNames(root)) {
252
- const runsDir = join(root, flow, 'runs');
253
- for (const runId of listDirNames(runsDir)) {
254
- const runDir = join(runsDir, runId);
255
- const askPath = join(runDir, 'ask.json');
256
- if (!existsSync(askPath)) continue;
273
+ // F48 round 3: flow/run identity is walked via `listFlowNames`/
274
+ // `listRunIds`/`resolveRunDir` (never a private `join()`), and `ask.json`
275
+ // is read via `readFileInside` (both `src/flow.js`) — a symlinked
276
+ // ask.json, or a symlinked run/flow dir, is treated as "missing" here,
277
+ // never followed.
278
+ for (const flow of listFlowNames(root)) {
279
+ const flowDir = join(root, flow);
280
+ for (const runId of listRunIds(flowDir)) {
281
+ const resolved = resolveRunDir(flowDir, runId);
282
+ if (!resolved.ok) continue; // eslint-disable-line no-continue
283
+ const runDir = resolved.runDir;
284
+ const askRead = readFileInside(runDir, 'ask.json');
285
+ if (!askRead.ok) continue; // eslint-disable-line no-continue -- missing (or refused) ask.json: nothing to show
257
286
  let ask;
258
287
  try {
259
- ask = JSON.parse(readFileSync(askPath, 'utf8'));
288
+ ask = JSON.parse(askRead.text);
260
289
  } catch (err) {
261
290
  // A malformed/unparseable ask.json must never crash inbox — show it
262
291
  // naming the run as unreadable and move on to the next one.
@@ -276,8 +305,10 @@ function cmdInbox(rest) {
276
305
  // Signed item 6: expiry is checked whenever anyone touches the run —
277
306
  // for `inbox` that means SHOWING an expired ask, never cancelling it
278
307
  // (cancelling happens only at answer/resume, which re-verify anyway).
279
- const consumedPath = join(runDir, `answer.${ask.askId}.consumed.json`);
280
- if (existsSync(consumedPath)) continue; // this specific ask is already answered
308
+ // Consumed-marker check also goes through `readFileInside` — a
309
+ // symlinked consumed marker is treated as "not consumed" (still shown
310
+ // as open/expired), never as proof the ask was answered.
311
+ if (readFileInside(runDir, `answer.${ask.askId}.consumed.json`).ok) continue; // eslint-disable-line no-continue -- this specific ask is already answered
281
312
  // A present-but-unparseable expiresAt (Date.parse -> NaN) must never
282
313
  // read as "not expired" — `Date.now() > NaN` is false, so the naive
283
314
  // compare below would show this as [open] with a "NaNs left"
@@ -323,11 +354,6 @@ function cmdInbox(rest) {
323
354
  // is how a human actually sees what they'd be accepting. Never writes.
324
355
  // ---------------------------------------------------------------------------
325
356
 
326
- function artifactText(artifact) {
327
- if (artifact && typeof artifact === 'object' && typeof artifact.text === 'string') return artifact.text;
328
- return JSON.stringify(artifact, null, 2);
329
- }
330
-
331
357
  function cmdShow(rest) {
332
358
  const { values, positionals } = parseArgs({
333
359
  args: rest,
@@ -337,14 +363,22 @@ function cmdShow(rest) {
337
363
  const [askId] = positionals;
338
364
  if (!askId) fail('show: an askId is required — fwdloop show <askId> --root <dir>');
339
365
 
340
- const root = resolve(values.root ?? '.');
341
- if (!existsSync(root)) fail(`show: root "${root}" does not exist`);
366
+ const root = resolveRoot('show', values.root ?? '.');
342
367
  const runDir = findRunDirByAskId(root, askId);
343
368
  if (!runDir) fail(`show: no open ask found for askId "${askId}" under ${root}`);
344
369
 
370
+ // F48 round 3: routed through `readFileInside` (`src/flow.js`) — a
371
+ // symlinked ask.json is refused by name here, never read through.
372
+ const askRead = readFileInside(runDir, 'ask.json');
373
+ if (!askRead.ok) {
374
+ fail(askRead.missing
375
+ ? `show: no ask.json for askId "${askId}"`
376
+ : `show: ask.json for askId "${askId}" — ${askRead.red}`);
377
+ return;
378
+ }
345
379
  let ask;
346
380
  try {
347
- ask = JSON.parse(readFileSync(join(runDir, 'ask.json'), 'utf8'));
381
+ ask = JSON.parse(askRead.text);
348
382
  } catch (err) {
349
383
  fail(`show: ask.json for askId "${askId}" is not valid JSON — ${err.message}`);
350
384
  return;
@@ -353,16 +387,16 @@ function cmdShow(rest) {
353
387
  process.stdout.write(`question: ${ask.question}\n`);
354
388
  process.stdout.write(`expiresAt: ${ask.expiresAt}\n`);
355
389
 
356
- const evidence = ask.evidence ?? null;
357
- if (!evidence) {
358
- process.stdout.write('\n(no evidence recorded on this ask — legacy park, before F45 finding 2 was fixed)\n');
390
+ const { draft, unjudged, why } = readAskEvidence(ask);
391
+ if (!draft) {
392
+ process.stdout.write(`\n(no evidence recorded on this ask — ${why})\n`);
359
393
  return;
360
394
  }
361
395
  process.stdout.write('\n--- artifact under review ---\n');
362
- process.stdout.write(`${artifactText(evidence.artifact)}\n`);
363
- for (const item of evidence.unjudged ?? []) {
364
- process.stdout.write(`\n--- unjudged: ${item.step ?? item.emits ?? '(step)'} ---\n`);
365
- process.stdout.write(`${artifactText(item.artifact)}\n`);
396
+ process.stdout.write(`${draft.text}\n`);
397
+ for (const item of unjudged) {
398
+ process.stdout.write(`\n--- unjudged: ${item.step} ---\n`);
399
+ process.stdout.write(`${item.text}\n`);
366
400
  }
367
401
  }
368
402
 
@@ -380,8 +414,7 @@ function cmdAnswer(rest) {
380
414
  if (!askId) fail('answer: an askId is required — fwdloop answer <askId> accept|reject "<reason>"|rerun "<reason>"');
381
415
  if (!decision) fail(`answer: a decision (accept|reject|rerun) is required for askId "${askId}"`);
382
416
 
383
- const root = resolve(values.root ?? '.');
384
- if (!existsSync(root)) fail(`answer: root "${root}" does not exist`);
417
+ const root = resolveRoot('answer', values.root ?? '.');
385
418
  const runDir = findRunDirByAskId(root, askId);
386
419
  if (!runDir) fail(`answer: no open ask found for askId "${askId}" under ${root}`);
387
420
 
@@ -411,7 +444,7 @@ async function cmdResume(rest) {
411
444
  if (!values.flow) fail('resume: --flow <name> is required');
412
445
  if (!values.root) fail('resume: --root <dir> is required');
413
446
 
414
- const root = resolve(values.root);
447
+ const root = resolveRoot('resume', values.root);
415
448
  const flowName = values.flow;
416
449
 
417
450
  const loaded = loadCatalogue();
@@ -422,7 +455,9 @@ async function cmdResume(rest) {
422
455
  if (!read.ok) fail(`flow "${flowName}": ${read.reds.join('; ')}`);
423
456
  const { declaration } = read;
424
457
 
425
- const runDir = join(root, flowName, 'runs', runId);
458
+ const runIdCheck = resolveRunDir(join(root, flowName), runId);
459
+ if (!runIdCheck.ok) fail(runIdCheck.red);
460
+ const { runDir } = runIdCheck;
426
461
  if (!existsSync(runDir)) fail(`resume: no run dir for runId "${runId}" under flow "${flowName}"`);
427
462
 
428
463
  const spendPath = join(runDir, 'spend.jsonl');
@@ -437,13 +472,27 @@ async function cmdResume(rest) {
437
472
  // Resume never re-freezes inputs — the run's own `inputs.json` (written at
438
473
  // park by `freezeInputs`) is the one source of the already-frozen paths a
439
474
  // sandboxed primitive may read.
440
- const inputsManifestPath = join(runDir, 'inputs.json');
441
- const inputsManifest = existsSync(inputsManifestPath) ? JSON.parse(readFileSync(inputsManifestPath, 'utf8')) : [];
475
+ // F48 round 3: routed through `readFileInside` (`src/flow.js`) — a
476
+ // symlinked `inputs.json` is treated as "no manifest" (empty array) rather
477
+ // than followed, which would otherwise let an outside file's content name
478
+ // arbitrary "frozen" paths for the sandboxed read/grep/readDocx/
479
+ // addressCells tools built just below.
480
+ const inputsManifestRead = readFileInside(runDir, 'inputs.json');
481
+ let inputsManifest = [];
482
+ if (inputsManifestRead.ok) {
483
+ try {
484
+ inputsManifest = JSON.parse(inputsManifestRead.text);
485
+ } catch (err) {
486
+ fail(`resume: inputs.json for run "${runId}" is not valid JSON — ${err.message}`);
487
+ return;
488
+ }
489
+ }
442
490
  const allVerbs = [...new Set(declaration.steps.flatMap((s) => s.primitives ?? []))];
491
+ // F46's unwired-verb preflight refusal now lives in `resumeRun` itself
492
+ // (`src/runner.js`) — this CLI no longer keeps a second copy of the check.
443
493
  const { tools: primitives, reds: primitiveReds } = resolvePrimitives(catalogue, allVerbs, {
444
494
  runDir, inputs: inputsManifest.map((m) => ({ id: m.id, frozen: m.frozen })),
445
495
  });
446
- for (const red of primitiveReds) process.stderr.write(`resume: ${red}\n`);
447
496
 
448
497
  const result = await resumeRun({
449
498
  root,
@@ -453,24 +502,54 @@ async function cmdResume(rest) {
453
502
  modelStep,
454
503
  sendStep: sendViaPrimitive,
455
504
  primitives,
505
+ primitiveReds,
456
506
  businessDate: today(),
457
507
  });
458
508
 
459
509
  reportRunResult(result, { rootArg: values.root });
460
510
  }
461
511
 
512
+ // ---------------------------------------------------------------------------
513
+ // panel — M4a piece 2 (docs/wiki/the-module-ladder.md, "M4a — read-only
514
+ // panel — SIGNED"): `fwdloop panel [--port 4800] [--root <dir>]`. Read-only,
515
+ // GET/HEAD only, bound to 127.0.0.1 only (src/panel/server.js). Never picks
516
+ // a different port on collision — a taken port is a loud, non-zero-exit
517
+ // failure.
518
+ // ---------------------------------------------------------------------------
519
+
520
+ async function cmdPanel(rest) {
521
+ const { values } = parseArgs({
522
+ args: rest,
523
+ options: {
524
+ port: { type: 'string' },
525
+ root: { type: 'string' },
526
+ },
527
+ });
528
+ const root = resolveRoot('panel', values.root ?? '.');
529
+
530
+ const argv = ['--root', root];
531
+ if (values.port) argv.push('--port', values.port);
532
+
533
+ const code = await panelMain(argv, {
534
+ out: (s) => process.stdout.write(`${s}\n`),
535
+ err: (s) => process.stderr.write(`fwdloop: ${s}\n`),
536
+ });
537
+ process.exitCode = code;
538
+ }
539
+
462
540
  // ---------------------------------------------------------------------------
463
541
 
464
542
  async function main() {
465
543
  const [verb, ...rest] = process.argv.slice(2);
466
- if (!verb) fail('usage: fwdloop run|inbox|show|answer|resume ...');
544
+ if (!verb) fail('usage: fwdloop run|inbox|show|answer|resume|panel ...');
467
545
 
468
546
  if (verb === 'run') return cmdRun(rest);
469
547
  if (verb === 'inbox') return cmdInbox(rest);
470
548
  if (verb === 'show') return cmdShow(rest);
471
549
  if (verb === 'answer') return cmdAnswer(rest);
472
550
  if (verb === 'resume') return cmdResume(rest);
473
- fail(`unknown verb "${verb}" — expected run | inbox | show | answer | resume`);
551
+ if (verb === 'panel') return cmdPanel(rest);
552
+ fail(`unknown verb "${verb}" — expected run | inbox | show | answer | resume | panel`);
474
553
  return undefined;
475
554
  }
476
555
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fwdloop",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
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",
@@ -49,8 +49,8 @@
49
49
  "NOTICE"
50
50
  ],
51
51
  "dependencies": {
52
- "bare-agent": "^0.42.0",
53
- "bareguard": "^0.15.0",
52
+ "bare-agent": "^0.48.0",
53
+ "bareguard": "^0.19.2",
54
54
  "litectx": "^0.32.0"
55
55
  },
56
56
  "devDependencies": {