fwdloop 0.2.1 → 0.4.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/package.json CHANGED
@@ -1,9 +1,16 @@
1
1
  {
2
2
  "name": "fwdloop",
3
- "version": "0.2.1",
3
+ "version": "0.4.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",
7
+ "types": "./types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.ts",
11
+ "default": "./src/index.js"
12
+ }
13
+ },
7
14
  "license": "Apache-2.0",
8
15
  "author": "hamr0",
9
16
  "repository": {
@@ -25,12 +32,15 @@
25
32
  "node": ">=20"
26
33
  },
27
34
  "scripts": {
28
- "test": "node --test"
35
+ "test": "node --test",
36
+ "typecheck": "tsc --noEmit",
37
+ "build:types": "tsc",
38
+ "prepack": "npm run build:types",
39
+ "check:lockfile": "node scripts/check-lockfile.mjs"
29
40
  },
30
41
  "files": [
31
42
  "src",
32
- "bin",
33
- "fwdloop.context.md",
43
+ "types",
34
44
  "LICENSE",
35
45
  "NOTICE"
36
46
  ],
@@ -38,5 +48,9 @@
38
48
  "bare-agent": "^0.42.0",
39
49
  "bareguard": "^0.15.0",
40
50
  "litectx": "^0.32.0"
51
+ },
52
+ "devDependencies": {
53
+ "typescript": "^5.9.3",
54
+ "@types/node": "^22.20.4"
41
55
  }
42
56
  }
@@ -0,0 +1,350 @@
1
+ // ported-from: poc/m0/catalogue.mjs@c4f272e (CATALOGUE/FENCE data,
2
+ // menu()/primitiveFor()/resolveEntry() shape) — rewritten, never imported.
3
+ // src must not import from poc (CLAUDE.md: "borrow, never import";
4
+ // complete separation between poc/ and src/).
5
+ //
6
+ // M1 piece 4 (docs/wiki/the-module-ladder.md, "M1 — scope, exit,
7
+ // negative — SIGNED", scope item 5): "Catalogue as data. The primitive
8
+ // catalogue is a data file the validator reads, not code the drafter can
9
+ // reach." `src/catalogue.json` is that file; this module parses it against
10
+ // a closed schema (never throws), exposes `menu`/`primitiveFor` with the
11
+ // same absence-not-refusal semantics as the poc, and ports `resolveEntry`
12
+ // so a catalogue entry naming a symbol that doesn't exist is still caught
13
+ // (F23) rather than trusted blind.
14
+ //
15
+ // `plumbing` carries poc's three FENCE entries plus `checkpoint`. Reason:
16
+ // M1's slot grammar (F31; amendment 3, 2026-09-21) means a pause exists only
17
+ // where a human marked a numbered job line `ask:` (or `ask <int><s|m|h>:`);
18
+ // the runner inserts the checkpoint
19
+ // mechanically once that slot resolves, and the drafter never selects it —
20
+ // exactly poc's own definition of plumbing ("the runner wires these... the
21
+ // drafter never selects them, so they are deliberately absent from
22
+ // CATALOGUE, menu(), and primitiveFor()"). Absence, not refusal: `checkpoint`
23
+ // is not offered on any menu(), and primitiveFor() returns null for it, same
24
+ // as any other unknown verb — src/declaration.js's own ask-slot check (rule
25
+ // b) is what turns a step that DOES grant it into a red.
26
+ //
27
+ // `addressCells`/`readDocx` are fwdloop's own citation-contract primitives
28
+ // (F13 ruled 2026-09-09, "it is ours" — not a baresuite primitive). Their
29
+ // backing modules (poc/m0/csv.mjs, poc/m0/docx.mjs) move into src/ at M2;
30
+ // until then `package: "fwdloop"` names the gap explicitly rather than
31
+ // pointing at poc/, and `resolveEntry` reports it as an honest declared
32
+ // unresolved, not a pretend green.
33
+ //
34
+ // No `new RegExp` anywhere in this file (M1 signed scope item 6; the same
35
+ // grep that covers src/signed-text.js and src/declaration.js covers this
36
+ // file too).
37
+
38
+ import { readFileSync } from 'node:fs';
39
+
40
+ /** @typedef {import('./types.js').CatalogueEntry} CatalogueEntry */
41
+ /** @typedef {import('./types.js').PlumbingEntry} PlumbingEntry */
42
+ /** @typedef {import('./types.js').ParseCatalogueResult} ParseCatalogueResult */
43
+
44
+ /** Every top-level and entry-level field this schema defines — kept in sync
45
+ * with the checks below so a mutation suite can assert it tested every one
46
+ * of them (same trick as ARBITER_FIELDS/DECLARATION_FIELDS). */
47
+ export const CATALOGUE_FIELDS = Object.freeze([
48
+ // top level
49
+ 'version',
50
+ 'primitives',
51
+ 'plumbing',
52
+ // primitive entry fields
53
+ 'verb',
54
+ 'component',
55
+ 'package',
56
+ 'symbol',
57
+ 'method',
58
+ 'tool',
59
+ 'class',
60
+ 'skill',
61
+ 'desc',
62
+ // plumbing entry fields
63
+ 'name',
64
+ ]);
65
+
66
+ const VALID_CLASSES = Object.freeze(['read', 'write', 'store']);
67
+
68
+ const TOP_LEVEL_ALLOWED = Object.freeze(['version', 'primitives', 'plumbing']);
69
+ const PRIMITIVE_ALLOWED = Object.freeze([
70
+ 'verb', 'component', 'package', 'symbol', 'method', 'tool', 'class', 'skill', 'desc',
71
+ ]);
72
+ const PRIMITIVE_REQUIRED = Object.freeze([
73
+ 'verb', 'component', 'package', 'symbol', 'class', 'skill', 'desc',
74
+ ]);
75
+ const PLUMBING_ALLOWED = Object.freeze(['name', 'package', 'symbol']);
76
+ const PLUMBING_REQUIRED = Object.freeze(['name', 'package', 'symbol']);
77
+
78
+ /** @returns {value is Record<string, any>} */
79
+ function isPlainObject(value) {
80
+ return !!value && typeof value === 'object' && !Array.isArray(value);
81
+ }
82
+
83
+ function isNonEmptyString(value) {
84
+ return typeof value === 'string' && value.length > 0;
85
+ }
86
+
87
+ function checkOwnKeys(obj, allowed, path, reds) {
88
+ for (const key of Object.keys(obj)) {
89
+ if (!allowed.includes(key)) {
90
+ reds.push(`catalogue: unknown key "${key}" at ${path}.${key}`);
91
+ }
92
+ }
93
+ }
94
+
95
+ function checkPrimitiveEntry(entry, path, reds) {
96
+ if (!isPlainObject(entry)) {
97
+ reds.push(`catalogue: ${path} must be an object`);
98
+ return;
99
+ }
100
+ checkOwnKeys(entry, PRIMITIVE_ALLOWED, path, reds);
101
+ for (const field of PRIMITIVE_REQUIRED) {
102
+ if (!Object.prototype.hasOwnProperty.call(entry, field)) {
103
+ reds.push(`catalogue: ${path}.${field} is required`);
104
+ }
105
+ }
106
+ if (Object.prototype.hasOwnProperty.call(entry, 'verb') && !isNonEmptyString(entry.verb)) {
107
+ reds.push(`catalogue: ${path}.verb must be a non-empty string`);
108
+ }
109
+ for (const field of ['component', 'package', 'symbol', 'skill', 'desc']) {
110
+ if (Object.prototype.hasOwnProperty.call(entry, field) && !isNonEmptyString(entry[field])) {
111
+ reds.push(`catalogue: ${path}.${field} must be a non-empty string`);
112
+ }
113
+ }
114
+ if (Object.prototype.hasOwnProperty.call(entry, 'class')) {
115
+ if (!isNonEmptyString(entry.class) || !VALID_CLASSES.includes(entry.class)) {
116
+ reds.push(`catalogue: ${path}.class must be one of ${VALID_CLASSES.join('|')}`);
117
+ }
118
+ }
119
+ const hasMethod = Object.prototype.hasOwnProperty.call(entry, 'method');
120
+ const hasTool = Object.prototype.hasOwnProperty.call(entry, 'tool');
121
+ if (hasMethod && !isNonEmptyString(entry.method)) {
122
+ reds.push(`catalogue: ${path}.method must be a non-empty string`);
123
+ }
124
+ if (hasTool && !isNonEmptyString(entry.tool)) {
125
+ reds.push(`catalogue: ${path}.tool must be a non-empty string`);
126
+ }
127
+ if (hasMethod && hasTool) {
128
+ reds.push(`catalogue: ${path} carries both "method" and "tool" — an entry may name only one`);
129
+ }
130
+ }
131
+
132
+ function checkPlumbingEntry(entry, path, reds) {
133
+ if (!isPlainObject(entry)) {
134
+ reds.push(`catalogue: ${path} must be an object`);
135
+ return;
136
+ }
137
+ checkOwnKeys(entry, PLUMBING_ALLOWED, path, reds);
138
+ for (const field of PLUMBING_REQUIRED) {
139
+ if (!Object.prototype.hasOwnProperty.call(entry, field)) {
140
+ reds.push(`catalogue: ${path}.${field} is required`);
141
+ } else if (!isNonEmptyString(entry[field])) {
142
+ reds.push(`catalogue: ${path}.${field} must be a non-empty string`);
143
+ }
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Parse the catalogue file's text against the closed schema. Never throws —
149
+ * invalid JSON, a missing/retyped field, an unknown key, a duplicate verb,
150
+ * or a plumbing/primitive name clash all come back as `{ ok: false, reds }`.
151
+ *
152
+ * @param {unknown} text
153
+ * @returns {ParseCatalogueResult}
154
+ */
155
+ export function parseCatalogue(text) {
156
+ if (typeof text !== 'string') {
157
+ return deepFreeze({ ok: false, reds: ['catalogue: input must be a string'] });
158
+ }
159
+
160
+ let parsed;
161
+ try {
162
+ parsed = JSON.parse(text);
163
+ } catch (err) {
164
+ return deepFreeze({ ok: false, reds: [`catalogue: invalid JSON — ${err.message}`] });
165
+ }
166
+
167
+ const reds = [];
168
+
169
+ if (!isPlainObject(parsed)) {
170
+ return deepFreeze({ ok: false, reds: ['catalogue: must be an object'] });
171
+ }
172
+
173
+ checkOwnKeys(parsed, TOP_LEVEL_ALLOWED, 'catalogue', reds);
174
+
175
+ if (!Object.prototype.hasOwnProperty.call(parsed, 'version')) {
176
+ reds.push('catalogue: "version" is required');
177
+ } else if (parsed.version !== 1) {
178
+ reds.push(`catalogue: "version" must be exactly 1, got ${JSON.stringify(parsed.version)}`);
179
+ }
180
+
181
+ const primitives = Array.isArray(parsed.primitives) ? parsed.primitives : null;
182
+ if (primitives === null) {
183
+ reds.push('catalogue: "primitives" must be an array');
184
+ } else {
185
+ primitives.forEach((entry, i) => checkPrimitiveEntry(entry, `catalogue.primitives[${i}]`, reds));
186
+ }
187
+
188
+ const plumbing = Array.isArray(parsed.plumbing) ? parsed.plumbing : null;
189
+ if (plumbing === null) {
190
+ reds.push('catalogue: "plumbing" must be an array');
191
+ } else {
192
+ plumbing.forEach((entry, i) => checkPlumbingEntry(entry, `catalogue.plumbing[${i}]`, reds));
193
+ }
194
+
195
+ // duplicate verb
196
+ if (primitives !== null) {
197
+ const seenAt = new Map();
198
+ primitives.forEach((entry, i) => {
199
+ if (!isPlainObject(entry) || !isNonEmptyString(entry.verb)) return;
200
+ if (seenAt.has(entry.verb)) {
201
+ reds.push(`catalogue: verb "${entry.verb}" is duplicated at primitives[${seenAt.get(entry.verb)}] and primitives[${i}]`);
202
+ } else {
203
+ seenAt.set(entry.verb, i);
204
+ }
205
+ });
206
+
207
+ // plumbing name that is also a primitive verb
208
+ if (plumbing !== null) {
209
+ const verbs = new Set(seenAt.keys());
210
+ plumbing.forEach((entry) => {
211
+ if (isPlainObject(entry) && isNonEmptyString(entry.name) && verbs.has(entry.name)) {
212
+ reds.push(`catalogue: plumbing name "${entry.name}" clashes with a primitive verb of the same name`);
213
+ }
214
+ });
215
+ }
216
+ }
217
+
218
+ if (reds.length > 0) return deepFreeze({ ok: false, reds });
219
+
220
+ return deepFreeze({
221
+ ok: true,
222
+ primitives: (primitives ?? []).map((e) => ({ ...e })),
223
+ plumbing: (plumbing ?? []).map((e) => ({ ...e })),
224
+ });
225
+ }
226
+
227
+ let memo;
228
+
229
+ /**
230
+ * Read `./catalogue.json` next to this module and parse it. One reader;
231
+ * memoised at module level (the file never changes at runtime).
232
+ *
233
+ * @returns {ParseCatalogueResult}
234
+ */
235
+ export function loadCatalogue() {
236
+ if (memo) return memo;
237
+ let text;
238
+ try {
239
+ text = readFileSync(new URL('./catalogue.json', import.meta.url), 'utf8');
240
+ } catch (err) {
241
+ memo = deepFreeze({ ok: false, reds: [`catalogue: could not read catalogue.json — ${err.message}`] });
242
+ return memo;
243
+ }
244
+ memo = parseCatalogue(text);
245
+ return memo;
246
+ }
247
+
248
+ /**
249
+ * The drafter/scout's menu: `primitives` filtered by class and/or skill.
250
+ * Absence, not refusal — a filtered-out verb is not present in the
251
+ * returned array at all. `classes`/`skills` are allow-lists; omit either
252
+ * to leave that axis unfiltered.
253
+ *
254
+ * @param {CatalogueEntry[]} primitives
255
+ * @param {{ classes?: string[], skills?: string[] }} [options]
256
+ * @returns {CatalogueEntry[]}
257
+ */
258
+ export function menu(primitives, { classes, skills } = {}) {
259
+ if (!Array.isArray(primitives)) return [];
260
+ return primitives.filter(
261
+ (entry) => (!classes || classes.includes(entry.class)) && (!skills || skills.includes(entry.skill)),
262
+ );
263
+ }
264
+
265
+ /**
266
+ * Look up one catalogue entry by verb. Returns `null` on an unknown verb —
267
+ * never throws (src style: `validateDeclaration` already turns an unknown
268
+ * verb into a red; this module never needs to).
269
+ *
270
+ * @param {CatalogueEntry[]} primitives
271
+ * @param {string} verb
272
+ * @returns {CatalogueEntry | null}
273
+ */
274
+ export function primitiveFor(primitives, verb) {
275
+ if (!Array.isArray(primitives)) return null;
276
+ return primitives.find((entry) => entry.verb === verb) ?? null;
277
+ }
278
+
279
+ /**
280
+ * Prove one entry's `package`+`symbol` (plus `method`/`tool` where present)
281
+ * actually resolves against the real installed package. Never throws;
282
+ * returns `{ resolved: true }` or `{ resolved: false, reason }`.
283
+ *
284
+ * `package === "fwdloop"` is fwdloop's own citation-contract entries
285
+ * (`addressCells`, `readDocx`) whose backing modules move into src/ at M2 —
286
+ * this is a declared gap, not a resolvable import, so it comes back
287
+ * unresolved with that reason rather than a pretend `true`.
288
+ *
289
+ * @param {{ verb?: string, name?: string, package: string, symbol: string, method?: string, tool?: string }} entry
290
+ * @returns {Promise<{ resolved: true } | { resolved: false, reason: string }>}
291
+ */
292
+ export async function resolveEntry(entry) {
293
+ if (!isPlainObject(entry) || !isNonEmptyString(entry.package) || !isNonEmptyString(entry.symbol)) {
294
+ return { resolved: false, reason: 'entry is missing a package or symbol to resolve' };
295
+ }
296
+
297
+ if (entry.package === 'fwdloop') {
298
+ return { resolved: false, reason: 'own module lands in src at M2' };
299
+ }
300
+
301
+ let mod;
302
+ try {
303
+ mod = await import(entry.package);
304
+ } catch (err) {
305
+ return { resolved: false, reason: `could not import "${entry.package}" — ${err.message}` };
306
+ }
307
+
308
+ const label = entry.verb ?? entry.name ?? '(unnamed)';
309
+
310
+ if (entry.method) {
311
+ const Cls = mod[entry.symbol];
312
+ if (typeof Cls !== 'function') {
313
+ return { resolved: false, reason: `${entry.package}#${entry.symbol} is not an exported class (verb/name "${label}")` };
314
+ }
315
+ if (typeof Cls.prototype?.[entry.method] !== 'function') {
316
+ return { resolved: false, reason: `${entry.package}#${entry.symbol} has no method "${entry.method}" (verb/name "${label}")` };
317
+ }
318
+ return { resolved: true };
319
+ }
320
+
321
+ if (entry.tool) {
322
+ const factory = mod[entry.symbol];
323
+ if (typeof factory !== 'function') {
324
+ return { resolved: false, reason: `${entry.package}#${entry.symbol} is not an exported factory (verb/name "${label}")` };
325
+ }
326
+ let tools;
327
+ try {
328
+ ({ tools } = factory());
329
+ } catch (err) {
330
+ return { resolved: false, reason: `${entry.package}#${entry.symbol}() threw — ${err.message} (verb/name "${label}")` };
331
+ }
332
+ if (!Array.isArray(tools) || !tools.some((t) => t.name === entry.tool)) {
333
+ return { resolved: false, reason: `${entry.package}#${entry.symbol}() does not produce a tool named "${entry.tool}" (verb/name "${label}")` };
334
+ }
335
+ return { resolved: true };
336
+ }
337
+
338
+ if (typeof mod[entry.symbol] !== 'function') {
339
+ return { resolved: false, reason: `${entry.package} has no exported function "${entry.symbol}" (verb/name "${label}")` };
340
+ }
341
+ return { resolved: true };
342
+ }
343
+
344
+ function deepFreeze(value) {
345
+ if (value && typeof value === 'object' && !Object.isFrozen(value)) {
346
+ Object.freeze(value);
347
+ for (const key of Object.keys(value)) deepFreeze(value[key]);
348
+ }
349
+ return value;
350
+ }
@@ -0,0 +1,61 @@
1
+ {
2
+ "version": 1,
3
+ "primitives": [
4
+ {
5
+ "verb": "write", "component": "write", "package": "bare-agent/tools", "symbol": "createShellTools", "tool": "shell_write", "class": "write", "skill": "core", "desc": "Create or overwrite a file with given contents."
6
+ },
7
+ {
8
+ "verb": "edit", "component": "write", "package": "bare-agent/tools", "symbol": "createShellTools", "tool": "shell_edit", "class": "write", "skill": "core", "desc": "Change part of an existing file in place."
9
+ },
10
+ {
11
+ "verb": "read", "component": "select", "package": "bare-agent/tools", "symbol": "createShellTools", "tool": "shell_read", "class": "read", "skill": "core", "desc": "Read a file's text."
12
+ },
13
+ {
14
+ "verb": "grep", "component": "select", "package": "bare-agent/tools", "symbol": "createShellTools", "tool": "shell_grep", "class": "read", "skill": "core", "desc": "Find lines matching a pattern across files."
15
+ },
16
+ {
17
+ "verb": "recall", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "recall", "class": "read", "skill": "core", "desc": "Search saved notes by meaning; returns the best matches, not their full text."
18
+ },
19
+ {
20
+ "verb": "get", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "get", "class": "read", "skill": "core", "desc": "Fetch the full text of one saved note or parked item by its id."
21
+ },
22
+ {
23
+ "verb": "recent", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "recentMemory", "class": "read", "skill": "core", "desc": "List the most recently saved notes, newest first."
24
+ },
25
+ {
26
+ "verb": "compress", "component": "compress", "package": "litectx", "symbol": "compress", "class": "read", "skill": "core", "desc": "Shorten a piece of text to fit a small prompt (full, signature-only, or dropped)."
27
+ },
28
+ {
29
+ "verb": "peek", "component": "compress", "package": "litectx", "symbol": "LiteCtx", "method": "peek", "class": "read", "skill": "core", "desc": "Preview a parked item's start and end without loading all of it."
30
+ },
31
+ {
32
+ "verb": "stash", "component": "isolate", "package": "litectx", "symbol": "LiteCtx", "method": "stash", "class": "store", "skill": "core", "desc": "Park a large piece of text outside the prompt under an id, to fetch back later."
33
+ },
34
+ {
35
+ "verb": "remember", "component": "isolate", "package": "litectx", "symbol": "LiteCtx", "method": "remember", "class": "store", "skill": "memory", "desc": "Save a note that persists across runs, under an id."
36
+ },
37
+ {
38
+ "verb": "forget", "component": "isolate", "package": "litectx", "symbol": "LiteCtx", "method": "forget", "class": "store", "skill": "memory", "desc": "Delete a saved note by id."
39
+ },
40
+ {
41
+ "verb": "addressCells", "component": "fwdloop", "package": "fwdloop", "symbol": "parseCsv", "class": "read", "skill": "core", "desc": "Read a spreadsheet as cells, each with an address that can be cited."
42
+ },
43
+ {
44
+ "verb": "readDocx", "component": "fwdloop", "package": "fwdloop", "symbol": "readDocxText", "class": "read", "skill": "core", "desc": "Read a Word (.docx) file's text."
45
+ }
46
+ ],
47
+ "plumbing": [
48
+ {
49
+ "name": "fence", "package": "bareguard", "symbol": "Gate"
50
+ },
51
+ {
52
+ "name": "redact", "package": "bareguard", "symbol": "redact"
53
+ },
54
+ {
55
+ "name": "wireGate", "package": "bare-agent", "symbol": "wireGate"
56
+ },
57
+ {
58
+ "name": "checkpoint", "package": "bare-agent", "symbol": "Checkpoint"
59
+ }
60
+ ]
61
+ }