fwdloop 0.27.0 → 0.29.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 (105) hide show
  1. package/README.md +1 -1
  2. package/bin/fwdloop +54 -13
  3. package/package.json +3 -3
  4. package/src/ask.js +1 -1
  5. package/src/audit-fold.js +9 -1
  6. package/src/authoring.js +22 -5
  7. package/src/canrun.js +19 -16
  8. package/src/checked.js +5 -2
  9. package/src/checkpick.js +257 -0
  10. package/src/checkwords.js +24 -0
  11. package/src/closers.js +8 -1
  12. package/src/config.js +18 -0
  13. package/src/declaration.js +85 -5
  14. package/src/drafter.js +143 -47
  15. package/src/flow.js +2 -1
  16. package/src/launcher.js +4 -2
  17. package/src/learn.js +1 -1
  18. package/src/learnstore.js +8 -7
  19. package/src/mcp-checks.js +50 -0
  20. package/src/mcp-pick.js +33 -0
  21. package/src/mcp-sources.js +84 -0
  22. package/src/mcp-use.js +54 -0
  23. package/src/memo.js +120 -0
  24. package/src/model-step.js +89 -17
  25. package/src/natural.js +23 -0
  26. package/src/pageask.js +9 -6
  27. package/src/pagedropwords.js +9 -0
  28. package/src/panel/author.js +50 -28
  29. package/src/panel/authorcard.js +86 -2
  30. package/src/panel/authorflows.js +18 -14
  31. package/src/panel/authortrigger.js +241 -84
  32. package/src/panel/data.js +99 -11
  33. package/src/panel/index.html +683 -210
  34. package/src/panel/jobgrid.js +127 -0
  35. package/src/panel/result.js +28 -7
  36. package/src/panel/scheduler.js +8 -2
  37. package/src/panel/server.js +17 -7
  38. package/src/panel/settings.js +119 -4
  39. package/src/panel/tickets.js +12 -7
  40. package/src/pickrecord.js +146 -0
  41. package/src/plain-checks.js +1 -0
  42. package/src/primitives.js +34 -5
  43. package/src/provider.js +4 -1
  44. package/src/runfile.js +4 -2
  45. package/src/runner.js +170 -45
  46. package/src/send.js +71 -4
  47. package/src/setup.js +13 -1
  48. package/src/summary-words.js +15 -3
  49. package/src/ticket-draft.js +40 -42
  50. package/src/ticket-labels.js +10 -0
  51. package/src/ticket-mcp.js +33 -10
  52. package/src/ticket-next.js +33 -0
  53. package/src/ticket-shape.js +9 -0
  54. package/src/trigger-clock.js +4 -4
  55. package/src/trigger-files.js +3 -3
  56. package/src/trigger-tickets.js +41 -12
  57. package/types/ask.d.ts +2 -1
  58. package/types/audit-fold.d.ts +13 -2
  59. package/types/authoring.d.ts +10 -2
  60. package/types/canrun.d.ts +5 -12
  61. package/types/checked.d.ts +7 -2
  62. package/types/checkpick.d.ts +149 -0
  63. package/types/checkwords.d.ts +14 -0
  64. package/types/closers.d.ts +2 -1
  65. package/types/config.d.ts +6 -0
  66. package/types/declaration.d.ts +17 -0
  67. package/types/drafter.d.ts +446 -50
  68. package/types/learn.d.ts +1 -0
  69. package/types/learnstore.d.ts +2 -3
  70. package/types/mcp-checks.d.ts +8 -0
  71. package/types/mcp-pick.d.ts +23 -0
  72. package/types/mcp-sources.d.ts +51 -0
  73. package/types/mcp-use.d.ts +12 -0
  74. package/types/memo.d.ts +15 -0
  75. package/types/model-step.d.ts +5 -0
  76. package/types/natural.d.ts +2 -0
  77. package/types/pageask.d.ts +9 -2
  78. package/types/pagedropwords.d.ts +1 -0
  79. package/types/panel/author.d.ts +37 -1
  80. package/types/panel/authorcard.d.ts +52 -0
  81. package/types/panel/authorflows.d.ts +9 -1
  82. package/types/panel/authortrigger.d.ts +73 -7
  83. package/types/panel/data.d.ts +56 -2
  84. package/types/panel/jobgrid.d.ts +109 -0
  85. package/types/panel/result.d.ts +9 -3
  86. package/types/panel/server.d.ts +5 -1
  87. package/types/panel/settings.d.ts +3 -2
  88. package/types/panel/tickets.d.ts +26 -4
  89. package/types/pickrecord.d.ts +88 -0
  90. package/types/primitives.d.ts +15 -2
  91. package/types/runfile.d.ts +7 -2
  92. package/types/runner.d.ts +128 -77
  93. package/types/send.d.ts +10 -2
  94. package/types/setup.d.ts +1 -56
  95. package/types/summary-words.d.ts +22 -3
  96. package/types/ticket-draft.d.ts +40 -32
  97. package/types/ticket-labels.d.ts +6 -0
  98. package/types/ticket-mcp.d.ts +18 -1
  99. package/types/ticket-next.d.ts +23 -0
  100. package/types/ticket-shape.d.ts +5 -0
  101. package/types/trigger-clock.d.ts +4 -3
  102. package/types/trigger-files.d.ts +1 -1
  103. package/types/trigger-tickets.d.ts +18 -4
  104. package/src/attemptdiff.js +0 -118
  105. package/types/attemptdiff.d.ts +0 -38
@@ -0,0 +1,146 @@
1
+ // M10 am17 + am18: the ONE record of check picks, carried from plan to plan. A pick is tied to its job line's WORDS (plus its order among lines with the same words)
2
+ // and to its guardrail's exact words, never to a line or rule number: both are recomputed on the new card. This file is the only writer of a record's entries
3
+ // (`entriesFromAnswers`) and the only reader that decides keep-or-ask (`matchRecord`); the logic is the one F146 / F146b proved (poc/m10-am17/keep-picks-am18.mjs).
4
+ // Throws away: line and rule numbers (recomputed), the model's words (it never sees a pick), a pick whose line or guardrail words no longer match.
5
+ import path from 'node:path';
6
+ import { readFileInside } from './flow.js';
7
+
8
+ /** the record's file, inside a drafting folder only (never a run folder) */
9
+ export const RECORD_FILE = 'picks-record.json';
10
+ /** the exact words a kept pick is marked with in the draft log, the Audit and the plan card (M10 am17 (6)) */
11
+ export const KEPT_WORDS = 'kept from last draft';
12
+
13
+ /** @typedef {{lineWords: string, occurrence: number, ruleText: string, added: string|null, check: Record<string, any>|null, own: boolean, left: boolean}} Entry */
14
+ /** @typedef {{line: number, rule: number, text: string}} Rule */
15
+
16
+ const norm = (/** @type {unknown} */ t) => String(t).toLowerCase().replace(/\s+/g, ' ').trim();
17
+ /** @param {unknown} a @param {unknown} b the two texts are the same words (case and spacing aside) */
18
+ export const sameWords = (a, b) => norm(a) === norm(b);
19
+ const id = (/** @type {Rule} */ r) => `${r.line}:${r.rule}`;
20
+
21
+ /**
22
+ * The key of every job line: its own words, plus how many lines before it have the same words (equal lines match in their order).
23
+ * @param {{n: number, text: string}[]} lines @returns {Map<number, {lineWords: string, occurrence: number}>}
24
+ */
25
+ export function lineKeys(lines) {
26
+ /** @type {Map<string, number>} */
27
+ const seen = new Map();
28
+ return new Map(lines.map((l) => {
29
+ const w = norm(l.text);
30
+ const occurrence = seen.get(w) ?? 0;
31
+ seen.set(w, occurrence + 1);
32
+ return [l.n, { lineWords: w, occurrence }];
33
+ }));
34
+ }
35
+
36
+ /**
37
+ * The entries for answers just given, numbered on THIS card (the answer redraft's card: its line and rule numbers are the ones the asks carried).
38
+ * @param {{n: number, text: string}[]} lines @param {Rule[]} rules
39
+ * @param {{line: number, rule: number, check?: Record<string, any>|null, added?: string|null, own?: boolean}[]} picks @param {{line: number, rule: number}[]} left
40
+ * @returns {Entry[]}
41
+ */
42
+ export function entriesFromAnswers(lines, rules, picks, left) {
43
+ const keys = lineKeys(lines);
44
+ /** @param {number} line @param {number} rule */
45
+ const base = (line, rule) => {
46
+ const r = rules.find((x) => x.line === line && x.rule === rule);
47
+ const k = keys.get(line);
48
+ return r === undefined || k === undefined ? null : { ...k, ruleText: r.text };
49
+ };
50
+ /** @type {Entry[]} */
51
+ const out = [];
52
+ for (const p of picks) {
53
+ const b = base(p.line, p.rule);
54
+ if (b) out.push({ ...b, added: typeof p.added === 'string' ? p.added : null, check: p.check ?? null, own: p.own === true, left: false });
55
+ }
56
+ for (const l of left) {
57
+ const b = base(l.line, l.rule);
58
+ if (b) out.push({ ...b, added: null, check: null, own: false, left: true });
59
+ }
60
+ return out;
61
+ }
62
+
63
+ /**
64
+ * Decide, for the card in hand, which stored picks are kept and which guardrails must be asked.
65
+ * A pick is KEPT when the job line with its words (same order among equal lines) still has its source guardrail with the exact words and, if the pick wrote a `~` line,
66
+ * that line with its exact words too. Otherwise the pick is dropped (never partly kept) and its guardrails are asked: a pair broken on one side is ONE ask, so the
67
+ * partner left on the card is folded (neither asked nor given the old check) - except an EDITED added line (M10 am21): that one is asked (`pairRule` = its source's number) and the source is folded. Pure.
68
+ * @param {Entry[]} entries @param {{n: number, text: string}[]} lines @param {Rule[]} rules every guardrail the call settles
69
+ * @returns {{picks: {line: number, rule: number, check: Record<string, any>|null, added?: string, own?: boolean}[], left: {line: number, rule: number}[],
70
+ * kept: {line: number, rule: number, entry: Entry}[], askRules: (Rule & {pairRule?: number})[]}}
71
+ */
72
+ export function matchRecord(entries, lines, rules) {
73
+ const byKey = new Map([...lineKeys(lines)].map(([n, k]) => [`${k.lineWords}#${k.occurrence}`, n]));
74
+ /** @type {Set<string>} */
75
+ const claimed = new Set();
76
+ /** @type {Set<string>} */
77
+ const folded = new Set();
78
+ /** @type {Map<string, number>} an edited added line (by id) -> the rule number of the source it was paired with (M10 am21) */
79
+ const edited = new Map();
80
+ const free = (/** @type {number} */ n, /** @type {string} */ text) => rules.find((r) => r.line === n && !claimed.has(id(r)) && !folded.has(id(r)) && norm(r.text) === norm(text));
81
+ const kept = [];
82
+ /** @type {{e: Entry, n: number|undefined}[]} */
83
+ const broken = [];
84
+ for (const e of Array.isArray(entries) ? entries : []) {
85
+ const n = byKey.get(`${e.lineWords}#${e.occurrence}`);
86
+ const src = n === undefined ? undefined : free(n, e.ruleText);
87
+ if (src) claimed.add(id(src)); // held while the partner is looked for, so a pair never takes one rule twice
88
+ const add = n === undefined || !src || e.added === null ? undefined : free(n, e.added);
89
+ if (n !== undefined && src && (e.added === null || add)) {
90
+ if (add) claimed.add(id(add));
91
+ kept.push({ line: n, rule: src.rule, entry: e });
92
+ } else {
93
+ if (src) claimed.delete(id(src));
94
+ broken.push({ e, n });
95
+ }
96
+ }
97
+ // a pair broken on one side is ONE pick, so ONE ask: the partner that is left folds into it (it keeps no old check and is not asked on its own)
98
+ for (const { e, n } of broken) {
99
+ if (e.added === null) continue;
100
+ if (n === undefined) {
101
+ // the line's own words changed: it is a new line, all its guardrails are asked; its old `~` line with its source still there rides the source's ask
102
+ for (const l of lines) {
103
+ const s = free(l.n, e.ruleText);
104
+ const a = s ? rules.find((r) => r.line === l.n && r !== s && !claimed.has(id(r)) && !folded.has(id(r)) && norm(r.text) === norm(e.added ?? '')) : undefined;
105
+ if (a) { folded.add(id(a)); break; }
106
+ }
107
+ continue;
108
+ }
109
+ const src = free(n, e.ruleText);
110
+ const add = free(n, e.added);
111
+ if (src && !add) {
112
+ // the added line was edited (or deleted): the unclaimed rule right after the source on that line is it. M10 am21: the ONE ask is about THAT line's words
113
+ // (it carries the source's number as `pairRule`, the rule its pick is kept under); the source rides it and is not asked on its own
114
+ const p = rules.find((r) => r.line === n && r.rule > src.rule && !claimed.has(id(r)) && !folded.has(id(r)));
115
+ if (p) { folded.add(id(src)); edited.set(id(p), src.rule); }
116
+ } else if (add && !src) {
117
+ // the source was edited: the unclaimed rule right before the added line is it; the old added line rides that ask
118
+ const p = [...rules].reverse().find((r) => r.line === n && r.rule < add.rule && !claimed.has(id(r)) && !folded.has(id(r)));
119
+ if (p) folded.add(id(add));
120
+ }
121
+ }
122
+ const picks = [];
123
+ const left = [];
124
+ for (const k of kept) {
125
+ const e = k.entry;
126
+ if (e.left) left.push({ line: k.line, rule: k.rule });
127
+ else picks.push({ line: k.line, rule: k.rule, check: e.check, ...(e.added === null ? {} : { added: e.added }), ...(e.own ? { own: true } : {}) });
128
+ }
129
+ return { picks, left, kept, askRules: rules.filter((r) => !claimed.has(id(r)) && !folded.has(id(r))).map((r) => (edited.has(id(r)) ? { ...r, pairRule: edited.get(id(r)) } : r)) };
130
+ }
131
+
132
+ /**
133
+ * Read a carried record (the folder's file). A missing or unreadable file is no record (a plan from before am17 has none).
134
+ * @param {string} file @returns {Entry[]|null}
135
+ */
136
+ export function readRecordFile(file) {
137
+ try {
138
+ const g = readFileInside(path.dirname(file), path.basename(file));
139
+ if (!g.ok) return null;
140
+ const j = JSON.parse(g.text);
141
+ return Array.isArray(j?.picks) ? j.picks.filter((/** @type {any} */ e) => e && typeof e.lineWords === 'string' && Number.isInteger(e.occurrence) && typeof e.ruleText === 'string') : null;
142
+ } catch { return null; }
143
+ }
144
+
145
+ /** where a plan folder keeps its record @param {string} planDir */
146
+ export const recordPath = (planDir) => path.join(planDir, RECORD_FILE);
@@ -28,6 +28,7 @@ export const PLAN_CHECK_RULES = Object.freeze([
28
28
  { re: /picks|inputFacts|invented name/, say: 'The plan names a field that is not in your input.', fix: 'Check the names in your Inputs and job line, then Draft again.' },
29
29
  { re: /guardrailClasses|unjudgeable|refused/, say: "The AI's notes on which lines it can check are not in a form the checker accepts.", fix: 'Draft again.' },
30
30
  { re: /^line \d+'s (guardrail|check)|can't write files/, say: null, fix: 'Change the guardrail on that line the way the sentence says, then Draft again.' },
31
+ { re: /^Input ".+" is read by no step\.$/, say: null, fix: 'Draft again.' },
31
32
  { re: /must be an? |is required|must name|does not name/, say: 'The plan the AI wrote is not in the shape the checker needs.', fix: 'Draft again.' },
32
33
  ]);
33
34
 
package/src/primitives.js CHANGED
@@ -68,8 +68,11 @@ function shellTool(name) {
68
68
  * `instanceof` and records `{verb, path, rule}` on the attempt's audit row —
69
69
  * never parsed back out of the message string. */
70
70
  export class GateRefusal extends Error {
71
- constructor(verb, resolved, rule) {
72
- super(`${verb}: "${resolved}" is outside the sandbox — refused (bareguard rule: ${rule})`);
71
+ /** `tool: true` (M13 item 5): a refused MCP tool, `verb` is its name; it names no path, so it gets no filesystem wording */
72
+ constructor(verb, resolved, rule, { tool = false } = {}) {
73
+ super(tool
74
+ ? `tool "${verb}" is not allowed for this step — refused (bareguard rule: ${rule})`
75
+ : `${verb}: "${resolved}" is outside the sandbox — refused (bareguard rule: ${rule})`);
73
76
  this.name = 'GateRefusal';
74
77
  this.verb = verb;
75
78
  this.path = resolved;
@@ -318,6 +321,15 @@ export function wiredMenu(skills) {
318
321
  return cat.primitives.filter((e) => WIRED_VERBS.has(e.verb) && (!skills || skills.includes(e.skill)));
319
322
  }
320
323
 
324
+ /** M13 item 5: the one name an MCP tool wears everywhere (bare-agent's bridge, the step's gate, the model's tool list, the diary row) */
325
+ export const mcpToolName = (server, tool) => `${server}_${tool}`;
326
+
327
+ /** M13 item 5: the job's MCP, as `resolvePrimitives` wants it: the signed server and every tool any step grants; `undefined` when no step has `mcpTools` */
328
+ export function stepMcp(declaration) {
329
+ const tools = [...new Set((declaration?.steps ?? []).flatMap((s) => s.mcpTools ?? []))];
330
+ return declaration?.mcp?.server && tools.length > 0 ? { server: declaration.mcp.server, tools } : undefined;
331
+ }
332
+
321
333
  /**
322
334
  * Resolve the granted tools for one step's model call. Never throws —
323
335
  * a verb absent from the catalogue, or present but unimplemented here, is
@@ -326,11 +338,11 @@ export function wiredMenu(skills) {
326
338
  *
327
339
  * @param {import('./types.js').CatalogueEntry[]} catalogue - `loadCatalogue().primitives` (or an injected test catalogue).
328
340
  * @param {string[]} grantedVerbs - `step.primitives`.
329
- * @param {{ runDir: string, inputs?: Array<{id:string, frozen:string}> }} ctx
341
+ * @param {{ runDir: string, inputs?: Array<{id:string, frozen:string}>, mcp?: {server: string, tools: string[]} }} ctx - `mcp` (`stepMcp`): the signed `<server>_<tool>` names the gate also allows (M13 item 5)
330
342
  * @returns {{ tools: Record<string, any>, reds: string[] }}
331
343
  */
332
344
  export function resolvePrimitives(catalogue, grantedVerbs, ctx) {
333
- const { runDir, inputs = [] } = ctx;
345
+ const { runDir, inputs = [], mcp } = ctx;
334
346
  const inputsByRole = Object.fromEntries(inputs.map((entry) => [entry.id, entry.frozen]));
335
347
  const outDir = path.join(runDir, 'out');
336
348
 
@@ -350,7 +362,11 @@ export function resolvePrimitives(catalogue, grantedVerbs, ctx) {
350
362
  let gate;
351
363
  try {
352
364
  // M4d am29 item 3: the diaries are denied to the model by name (bareloop's `fs.deny` of its audit sidecar path)
353
- gate = new Gate({ fs: { readScope, writeScope: [outDir], deny: diaryPaths(runDir) } });
365
+ // M13 item 5: an allowlist is exclusive for EVERY action (F116 measured it denying fs too), so with signed MCP tools it lists the step's verbs as well as their names
366
+ gate = new Gate({
367
+ fs: { readScope, writeScope: [outDir], deny: diaryPaths(runDir) },
368
+ ...(mcp ? { tools: { allowlist: [...(grantedVerbs ?? []), ...mcp.tools.map((t) => mcpToolName(mcp.server, t))] } } : {}),
369
+ });
354
370
  } catch (err) {
355
371
  return { tools: {}, reds: [`primitives: bareguard refused the step's file scope — ${err.message}`] };
356
372
  }
@@ -419,6 +435,19 @@ export function resolvePrimitives(catalogue, grantedVerbs, ctx) {
419
435
  }
420
436
  }
421
437
 
438
+ // M13 item 5: one entry per signed MCP tool. The runner binds it to the step's bridge tool; `gatedCall` asks the gate first (the same check, refusal and `gateSeen` as the fs verbs).
439
+ if (mcp) for (const tool of mcp.tools) {
440
+ const name = mcpToolName(mcp.server, tool);
441
+ tools[name] = {
442
+ name,
443
+ gatedCall: async (args, run) => {
444
+ const decision = await gate.check({ type: 'tool', tool: name, args });
445
+ if (decision.outcome !== 'allow') throw new GateRefusal(name, '', decision.rule, { tool: true });
446
+ return run(args);
447
+ },
448
+ };
449
+ }
450
+
422
451
  for (const verb of Object.keys(tools)) tools[verb] = { ...tools[verb], gateSeen };
423
452
  return { tools, reds };
424
453
  }
package/src/provider.js CHANGED
@@ -41,7 +41,7 @@ import { OpenAI, Anthropic, Gemini } from 'bare-agent/providers';
41
41
  * marker on the instance (`this.lastMalformedToolCall`, reset at the top of
42
42
  * every `generate()` call) — the one channel `runModelStepOnPrimitives`
43
43
  * (same provider reference) can read after `loop.run()` returns. We require
44
- * bare-agent >=0.50 (package.json `^0.52.0`) and never patch around its
44
+ * bare-agent >=0.50 (package.json `^0.57.0`) and never patch around its
45
45
  * private response internals.
46
46
  */
47
47
  class MalformedToolCallTolerantOpenAI extends OpenAI {
@@ -443,6 +443,9 @@ export function makeProvider(slotName, options = {}) {
443
443
  exposeMalformedArgs: true,
444
444
  });
445
445
 
446
+ // M4d am37: only the OpenAI-shaped provider sends `toolChoice`; the other shapes ignore it, so a forced call there would be a force that was never sent
447
+ /** @type {any} */ (provider).honoursToolChoice = slot.shape === DEFAULT_SHAPE;
448
+
446
449
  return {
447
450
  provider, rates, modelId, slot: slotName, prices,
448
451
  };
package/src/runfile.js CHANGED
@@ -50,10 +50,12 @@ export function readRunInputs(runDir) {
50
50
  * M8b item 1: the ONE writer of `run.json`, called once when a fresh run has frozen its inputs (`runFlow`); never over an existing record. `model` is the pick
51
51
  * the run started on (M4d am33; null = the section is left out), `thinking` the job's "Thinking on" box as it was at the start (M4d am25: written for every fresh
52
52
  * run, ticked or not), `inputs` the manifest `freezeInputs` returned. A fresh run always has `thinking` and `inputs`.
53
- * @param {string} runDir @param {{ model: { slot: string, model: string }|null, thinking: boolean, inputs: any[] }} start
53
+ * @param {string} runDir @param {{ model: { slot: string, model: string }|null, thinking: boolean, inputs: any[], rerun?: {decision: 'rerun', words: string, afterStep: string|null}|null }} start
54
54
  */
55
- export function writeRunStart(runDir, { model, thinking, inputs }) {
55
+ export function writeRunStart(runDir, { model, thinking, inputs, rerun = null }) {
56
56
  writeFileSync(join(runDir, RUN_FILE), `${JSON.stringify({
57
57
  ...(model ? { model: { slot: model.slot, model: model.model } } : {}), thinking, inputs,
58
+ // M14 am1 item 1: a rerun keeps the reason it was started with (the decision, the human's words, the step the old run's ask followed); a fresh run has no such key
59
+ ...(rerun ? { rerun: { decision: rerun.decision, words: rerun.words, afterStep: rerun.afterStep } } : {}),
58
60
  }, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
59
61
  }