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.
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Parse the catalogue file's text against the closed schema. Never throws —
3
+ * invalid JSON, a missing/retyped field, an unknown key, a duplicate verb,
4
+ * or a plumbing/primitive name clash all come back as `{ ok: false, reds }`.
5
+ *
6
+ * @param {unknown} text
7
+ * @returns {ParseCatalogueResult}
8
+ */
9
+ export function parseCatalogue(text: unknown): ParseCatalogueResult;
10
+ /**
11
+ * Read `./catalogue.json` next to this module and parse it. One reader;
12
+ * memoised at module level (the file never changes at runtime).
13
+ *
14
+ * @returns {ParseCatalogueResult}
15
+ */
16
+ export function loadCatalogue(): ParseCatalogueResult;
17
+ /**
18
+ * The drafter/scout's menu: `primitives` filtered by class and/or skill.
19
+ * Absence, not refusal — a filtered-out verb is not present in the
20
+ * returned array at all. `classes`/`skills` are allow-lists; omit either
21
+ * to leave that axis unfiltered.
22
+ *
23
+ * @param {CatalogueEntry[]} primitives
24
+ * @param {{ classes?: string[], skills?: string[] }} [options]
25
+ * @returns {CatalogueEntry[]}
26
+ */
27
+ export function menu(primitives: CatalogueEntry[], { classes, skills }?: {
28
+ classes?: string[];
29
+ skills?: string[];
30
+ }): CatalogueEntry[];
31
+ /**
32
+ * Look up one catalogue entry by verb. Returns `null` on an unknown verb —
33
+ * never throws (src style: `validateDeclaration` already turns an unknown
34
+ * verb into a red; this module never needs to).
35
+ *
36
+ * @param {CatalogueEntry[]} primitives
37
+ * @param {string} verb
38
+ * @returns {CatalogueEntry | null}
39
+ */
40
+ export function primitiveFor(primitives: CatalogueEntry[], verb: string): CatalogueEntry | null;
41
+ /**
42
+ * Prove one entry's `package`+`symbol` (plus `method`/`tool` where present)
43
+ * actually resolves against the real installed package. Never throws;
44
+ * returns `{ resolved: true }` or `{ resolved: false, reason }`.
45
+ *
46
+ * `package === "fwdloop"` is fwdloop's own citation-contract entries
47
+ * (`addressCells`, `readDocx`) whose backing modules move into src/ at M2 —
48
+ * this is a declared gap, not a resolvable import, so it comes back
49
+ * unresolved with that reason rather than a pretend `true`.
50
+ *
51
+ * @param {{ verb?: string, name?: string, package: string, symbol: string, method?: string, tool?: string }} entry
52
+ * @returns {Promise<{ resolved: true } | { resolved: false, reason: string }>}
53
+ */
54
+ export function resolveEntry(entry: {
55
+ verb?: string;
56
+ name?: string;
57
+ package: string;
58
+ symbol: string;
59
+ method?: string;
60
+ tool?: string;
61
+ }): Promise<{
62
+ resolved: true;
63
+ } | {
64
+ resolved: false;
65
+ reason: string;
66
+ }>;
67
+ /** @typedef {import('./types.js').CatalogueEntry} CatalogueEntry */
68
+ /** @typedef {import('./types.js').PlumbingEntry} PlumbingEntry */
69
+ /** @typedef {import('./types.js').ParseCatalogueResult} ParseCatalogueResult */
70
+ /** Every top-level and entry-level field this schema defines — kept in sync
71
+ * with the checks below so a mutation suite can assert it tested every one
72
+ * of them (same trick as ARBITER_FIELDS/DECLARATION_FIELDS). */
73
+ export const CATALOGUE_FIELDS: readonly string[];
74
+ export type CatalogueEntry = import("./types.js").CatalogueEntry;
75
+ export type PlumbingEntry = import("./types.js").PlumbingEntry;
76
+ export type ParseCatalogueResult = import("./types.js").ParseCatalogueResult;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Validate a drafter declaration against the M1 closed schema. `arbiter`
3
+ * and `lines` are `parseSignedText`'s output; `catalogue` is a plain array
4
+ * of `{ verb, skill, class }`. Never throws — a bad declaration, a bad
5
+ * arbiter, a bad lines array, or a bad catalogue is a red, never an
6
+ * exception.
7
+ *
8
+ * @param {unknown} declaration
9
+ * @param {{ arbiter?: unknown, lines?: unknown, catalogue?: unknown }} [context]
10
+ * @returns {ValidateDeclarationResult}
11
+ */
12
+ export function validateDeclaration(declaration: unknown, context?: {
13
+ arbiter?: unknown;
14
+ lines?: unknown;
15
+ catalogue?: unknown;
16
+ }): ValidateDeclarationResult;
17
+ /** @typedef {import('./types.js').Arbiter} Arbiter */
18
+ /** @typedef {import('./types.js').SignedLine} SignedLine */
19
+ /** @typedef {import('./types.js').CatalogueEntry} CatalogueEntry */
20
+ /** @typedef {import('./types.js').ValidateDeclarationResult} ValidateDeclarationResult */
21
+ /** Every top-level, step-level and close-level field this schema defines —
22
+ * kept in sync with the checks below so a mutation suite can assert it
23
+ * tested every one of them (same trick as ARBITER_FIELDS/SIGNATURE_FIELDS). */
24
+ export const DECLARATION_FIELDS: readonly string[];
25
+ export type Arbiter = import("./types.js").Arbiter;
26
+ export type SignedLine = import("./types.js").SignedLine;
27
+ export type CatalogueEntry = import("./types.js").CatalogueEntry;
28
+ export type ValidateDeclarationResult = import("./types.js").ValidateDeclarationResult;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Check a flow name against the allowed shape: 1..64 characters, lowercase
3
+ * a-z, 0-9, `-`, `_`, starting with a letter or digit. Walked character by
4
+ * character — never a regex — so a name that would escape the flows
5
+ * directory (`..`, a `/` or `\` segment, a leading dot, an absolute path)
6
+ * or is simply empty is refused by the same loop that refuses a bad
7
+ * character, not a separate ad-hoc check.
8
+ *
9
+ * @param {unknown} name
10
+ * @returns {CheckFlowNameResult}
11
+ */
12
+ export function checkFlowName(name: unknown): CheckFlowNameResult;
13
+ /**
14
+ * Write a signed flow to `<root>/<name>/`. Refuses (writing nothing) if the
15
+ * flow name is invalid, if `proseText`/`declaration` don't parse/validate,
16
+ * if `signFlow` refuses, or if any of the three files already exists.
17
+ * Otherwise writes `prose.txt`, `declaration.json`, then `signature.json`
18
+ * last (each via a temp file + rename in the same directory), creates an
19
+ * empty `runs/`, and proves the result by reading it back with `readFlow`.
20
+ *
21
+ * @param {{root: unknown, name: unknown, proseText: unknown, declaration: unknown, signedBy: unknown, signedAt: unknown, catalogue: unknown}} input
22
+ * @returns {WriteFlowResult}
23
+ */
24
+ export function writeFlow({ root, name, proseText, declaration, signedBy, signedAt, catalogue, }: {
25
+ root: unknown;
26
+ name: unknown;
27
+ proseText: unknown;
28
+ declaration: unknown;
29
+ signedBy: unknown;
30
+ signedAt: unknown;
31
+ catalogue: unknown;
32
+ }): WriteFlowResult;
33
+ /**
34
+ * Read and verify a signed flow from `<root>/<name>/`. Verifies the bytes on
35
+ * disk against the signature FIRST — before anything in the flow is
36
+ * trusted — then parses the signed text, then validates the declaration.
37
+ * Never throws; every failure is a red naming the file or path.
38
+ *
39
+ * @param {{root: unknown, name: unknown, catalogue: unknown}} input
40
+ * @returns {ReadFlowResult}
41
+ */
42
+ export function readFlow({ root, name, catalogue }: {
43
+ root: unknown;
44
+ name: unknown;
45
+ catalogue: unknown;
46
+ }): ReadFlowResult;
47
+ /** @typedef {import('./types.js').WriteFlowResult} WriteFlowResult */
48
+ /** @typedef {import('./types.js').ReadFlowResult} ReadFlowResult */
49
+ /** @typedef {import('./types.js').CheckFlowNameResult} CheckFlowNameResult */
50
+ /** The three files a signed flow directory carries. Every place in this
51
+ * module that names a flow file uses this — never a literal string. */
52
+ export const FLOW_FILES: readonly string[];
53
+ export type WriteFlowResult = import("./types.js").WriteFlowResult;
54
+ export type ReadFlowResult = import("./types.js").ReadFlowResult;
55
+ export type CheckFlowNameResult = import("./types.js").CheckFlowNameResult;
@@ -0,0 +1,5 @@
1
+ export { parseSignedText, ARBITER_FIELDS } from "./signed-text.js";
2
+ export { signFlow, verifyFlow, canonicalBytes, SIGNATURE_FIELDS } from "./signature.js";
3
+ export { validateDeclaration, DECLARATION_FIELDS } from "./declaration.js";
4
+ export { parseCatalogue, loadCatalogue, menu, primitiveFor, resolveEntry, CATALOGUE_FIELDS } from "./catalogue.js";
5
+ export { checkFlowName, writeFlow, readFlow, FLOW_FILES } from "./flow.js";
@@ -0,0 +1,78 @@
1
+ /**
2
+ * The canonical bytes hashed for one of the two signed files. Never throws.
3
+ *
4
+ * `prose.txt`: the exact file bytes with CRLF normalised to LF and nothing
5
+ * else changed — no trimming, no Unicode normalisation. The human signs the
6
+ * text as written; a Windows checkout (CRLF line endings) must not flip the
7
+ * hash, but anything the human actually wrote — a trailing space, a changed
8
+ * character — must.
9
+ *
10
+ * `declaration.json`: parsed then re-serialised with object keys sorted
11
+ * recursively, no whitespace, arrays left in their original order. So
12
+ * key order and indentation (cosmetic) can never flip the hash, while any
13
+ * value or structural change (including reordering an array — order is
14
+ * meaning for `steps`) always does. Unparseable JSON is a red, never a
15
+ * throw.
16
+ *
17
+ * @param {'prose.txt'|'declaration.json'} kind
18
+ * @param {unknown} content
19
+ * @returns {CanonicalBytesResult}
20
+ */
21
+ export function canonicalBytes(kind: "prose.txt" | "declaration.json", content: unknown): CanonicalBytesResult;
22
+ /**
23
+ * The one function that computes `signature.flow` — called by both signFlow
24
+ * and verifyFlow, never duplicated (one writer per piece of state).
25
+ *
26
+ * Hashes sha256 over `JSON.stringify([proseHash, declHash, signedBy,
27
+ * signedAt])`, in that fixed order. `flow` pins WHAT was signed (the two
28
+ * file hashes) AND WHO/WHEN (signedBy/signedAt) — editing any of the four
29
+ * afterwards changes the bytes hashed. A JSON array of strings is used
30
+ * instead of a bare `join('some-separator')` specifically because signedBy
31
+ * is a human-chosen name that could itself contain any separator character;
32
+ * `JSON.stringify` quotes and escapes each element, so the byte position of
33
+ * every element's boundary is unambiguous and no two distinct 4-tuples can
34
+ * ever serialise to the same bytes. This function does not itself validate
35
+ * shape (that is signFlow's/verifyFlow's job) — it only encodes and hashes
36
+ * whatever four values it is given.
37
+ *
38
+ * @param {string} proseHash
39
+ * @param {string} declHash
40
+ * @param {unknown} signedBy
41
+ * @param {unknown} signedAt
42
+ * @returns {string} sha256 hex digest
43
+ */
44
+ export function computeFlowHash(proseHash: string, declHash: string, signedBy: unknown, signedAt: unknown): string;
45
+ /**
46
+ * Sign a flow: hash `proseText` and `declarationText` (canonically) and pin
47
+ * who signed and when. Pure — never reads the clock, never touches disk.
48
+ *
49
+ * @param {{proseText: unknown, declarationText: unknown, signedBy: unknown, signedAt: unknown}} input
50
+ * @returns {SignFlowResult}
51
+ */
52
+ export function signFlow({ proseText, declarationText, signedBy, signedAt }: {
53
+ proseText: unknown;
54
+ declarationText: unknown;
55
+ signedBy: unknown;
56
+ signedAt: unknown;
57
+ }): SignFlowResult;
58
+ /**
59
+ * Verify a flow against its signature. Collects every red found — never
60
+ * throws, never stops at the first problem.
61
+ *
62
+ * @param {{proseText: unknown, declarationText: unknown, signature: unknown}} input
63
+ * @returns {VerifyFlowResult}
64
+ */
65
+ export function verifyFlow({ proseText, declarationText, signature }: {
66
+ proseText: unknown;
67
+ declarationText: unknown;
68
+ signature: unknown;
69
+ }): VerifyFlowResult;
70
+ /** @typedef {import('./types.js').CanonicalBytesResult} CanonicalBytesResult */
71
+ /** @typedef {import('./types.js').SignFlowResult} SignFlowResult */
72
+ /** @typedef {import('./types.js').VerifyFlowResult} VerifyFlowResult */
73
+ /** The signature.json fields the mutation suite is required to cover —
74
+ * kept in sync with signFlow/verifyFlow below. */
75
+ export const SIGNATURE_FIELDS: readonly string[];
76
+ export type CanonicalBytesResult = import("./types.js").CanonicalBytesResult;
77
+ export type SignFlowResult = import("./types.js").SignFlowResult;
78
+ export type VerifyFlowResult = import("./types.js").VerifyFlowResult;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Parse the human's signed text. Never throws — any input that isn't a
3
+ * usable signed text comes back as `{ ok: false, reds }`, never an
4
+ * exception, and a non-string input is itself a red.
5
+ *
6
+ * @param {unknown} rawText
7
+ * @returns {SignedTextResult}
8
+ */
9
+ export function parseSignedText(rawText: unknown): SignedTextResult;
10
+ /** @typedef {import('./types.js').SignedTextResult} SignedTextResult */
11
+ /** The arbiter fields this grammar defines — kept in sync with the parser
12
+ * below so a mutation suite can assert it tested every one of them. */
13
+ export const ARBITER_FIELDS: readonly string[];
14
+ export type SignedTextResult = import("./types.js").SignedTextResult;
@@ -0,0 +1,237 @@
1
+ export type SignedLine = {
2
+ /**
3
+ * - the number the human wrote, used as the join key.
4
+ */
5
+ n: number;
6
+ /**
7
+ * - the line's text, trimmed.
8
+ */
9
+ text: string;
10
+ /**
11
+ * - the guardrail directly under this line, or
12
+ * `''` when the human left it blank.
13
+ */
14
+ guardrail: string;
15
+ };
16
+ export type AskSlot = {
17
+ /**
18
+ * - the numbered line this ask binds to (the line
19
+ * carries the `ask:`/`ask <int><s|m|h>:` mark itself, M1 amendment 3).
20
+ */
21
+ line: number;
22
+ /**
23
+ * - the ask's time-to-live in milliseconds.
24
+ */
25
+ ttlMs: number;
26
+ /**
27
+ * - the words after the mark, trimmed — the
28
+ * human's own question. Nothing reads this yet outside signed-text.js.
29
+ */
30
+ question: string;
31
+ };
32
+ export type SendTarget = {
33
+ /**
34
+ * - only `file:` targets are available before M9.
35
+ */
36
+ kind: "file";
37
+ /**
38
+ * - everything after `file:` to end of line, trimmed.
39
+ */
40
+ path: string;
41
+ };
42
+ export type SendSlot = {
43
+ /**
44
+ * - the numbered line this send binds to.
45
+ */
46
+ line: number;
47
+ target: SendTarget;
48
+ };
49
+ export type SourceSlot = {
50
+ /**
51
+ * - `[a-z][a-z0-9_-]*`, unique across sources.
52
+ */
53
+ role: string;
54
+ /**
55
+ * - only `file:` sources are available before M9.
56
+ */
57
+ kind: "file";
58
+ /**
59
+ * - everything after `file:` to end of line, trimmed.
60
+ */
61
+ path: string;
62
+ };
63
+ export type Arbiter = {
64
+ /**
65
+ * - `cap $<decimal> per run`. Required, > 0.
66
+ */
67
+ capUsd: number;
68
+ /**
69
+ * - zero or more marked lines (`ask:` /
70
+ * `ask <int><s|m|h>:` at the start of a numbered job line, M1 amendment 3).
71
+ */
72
+ asks: AskSlot[];
73
+ /**
74
+ * - `redo cap <int>`, 1..3. Default 3.
75
+ */
76
+ redoCap: number;
77
+ /**
78
+ * - zero or more `send at line <int> to ...`.
79
+ */
80
+ sends: SendSlot[];
81
+ /**
82
+ * - `skills <name>[, <name>...]`. Default ['core'].
83
+ */
84
+ skills: string[];
85
+ /**
86
+ * - zero or more `source <role> = file:<path>`.
87
+ */
88
+ sources: SourceSlot[];
89
+ /**
90
+ * - `round budget <int><s|m|h>`. Default 120000.
91
+ */
92
+ roundBudgetMs: number;
93
+ };
94
+ export type SignedTextOk = {
95
+ ok: true;
96
+ lines: SignedLine[];
97
+ arbiter: Arbiter;
98
+ };
99
+ export type SignedTextRed = {
100
+ ok: false;
101
+ reds: string[];
102
+ };
103
+ export type SignedTextResult = SignedTextOk | SignedTextRed;
104
+ export type CanonicalBytesOk = {
105
+ ok: true;
106
+ bytes: Buffer;
107
+ };
108
+ export type CanonicalBytesRed = {
109
+ ok: false;
110
+ red: string;
111
+ };
112
+ export type CanonicalBytesResult = CanonicalBytesOk | CanonicalBytesRed;
113
+ /**
114
+ * sha256 hex digests of each signed file's canonical bytes.
115
+ */
116
+ export type SignatureFiles = {
117
+ "prose.txt": string;
118
+ "declaration.json": string;
119
+ };
120
+ export type Signature = {
121
+ version: 2;
122
+ algorithm: "sha256";
123
+ files: SignatureFiles;
124
+ /**
125
+ * - sha256 hex of
126
+ * `JSON.stringify([files['prose.txt'], files['declaration.json'], signedBy, signedAt])`,
127
+ * in that fixed order — pins WHAT was signed (the two file hashes) and
128
+ * WHO/WHEN (signedBy/signedAt); editing any of the four after signing
129
+ * changes this hash.
130
+ */
131
+ flow: string;
132
+ signedBy: string;
133
+ /**
134
+ * - ISO-8601.
135
+ */
136
+ signedAt: string;
137
+ };
138
+ export type SignFlowOk = {
139
+ ok: true;
140
+ signature: Signature;
141
+ };
142
+ export type SignFlowRed = {
143
+ ok: false;
144
+ reds: string[];
145
+ };
146
+ export type SignFlowResult = SignFlowOk | SignFlowRed;
147
+ export type VerifyFlowOk = {
148
+ ok: true;
149
+ };
150
+ export type VerifyFlowRed = {
151
+ ok: false;
152
+ reds: string[];
153
+ };
154
+ export type VerifyFlowResult = VerifyFlowOk | VerifyFlowRed;
155
+ /**
156
+ * One catalogue primitive entry. M1 piece 3 only needed `verb`/`skill`/
157
+ * `class` (the catalogue was a caller-supplied parameter); piece 4 makes
158
+ * `src/catalogue.json` the real data file, so this typedef grew the rest
159
+ * of the fields the file actually carries. `method`/`tool` are mutually
160
+ * exclusive and optional.
161
+ */
162
+ export type CatalogueEntry = {
163
+ verb: string;
164
+ component: string;
165
+ package: string;
166
+ symbol: string;
167
+ method?: string | undefined;
168
+ tool?: string | undefined;
169
+ class: string;
170
+ skill: string;
171
+ desc: string;
172
+ };
173
+ /**
174
+ * One catalogue plumbing entry — the runner wires these around every step;
175
+ * the drafter never selects them, so they never appear in `primitives`,
176
+ * `menu()`, or `primitiveFor()`.
177
+ */
178
+ export type PlumbingEntry = {
179
+ name: string;
180
+ package: string;
181
+ symbol: string;
182
+ };
183
+ export type ParseCatalogueOk = {
184
+ ok: true;
185
+ primitives: CatalogueEntry[];
186
+ plumbing: PlumbingEntry[];
187
+ };
188
+ export type ParseCatalogueRed = {
189
+ ok: false;
190
+ reds: string[];
191
+ };
192
+ export type ParseCatalogueResult = ParseCatalogueOk | ParseCatalogueRed;
193
+ export type ValidateDeclarationOk = {
194
+ ok: true;
195
+ /**
196
+ * - keyed by each step's `emits`
197
+ * id, value the step's effective guardrail class.
198
+ */
199
+ classes: Record<string, string>;
200
+ };
201
+ export type ValidateDeclarationRed = {
202
+ ok: false;
203
+ reds: string[];
204
+ };
205
+ export type ValidateDeclarationResult = ValidateDeclarationOk | ValidateDeclarationRed;
206
+ export type CheckFlowNameOk = {
207
+ ok: true;
208
+ };
209
+ export type CheckFlowNameRed = {
210
+ ok: false;
211
+ red: string;
212
+ };
213
+ export type CheckFlowNameResult = CheckFlowNameOk | CheckFlowNameRed;
214
+ export type WriteFlowOk = {
215
+ ok: true;
216
+ dir: string;
217
+ signature: Signature;
218
+ };
219
+ export type WriteFlowRed = {
220
+ ok: false;
221
+ reds: string[];
222
+ };
223
+ export type WriteFlowResult = WriteFlowOk | WriteFlowRed;
224
+ export type ReadFlowOk = {
225
+ ok: true;
226
+ dir: string;
227
+ lines: SignedLine[];
228
+ arbiter: Arbiter;
229
+ declaration: Record<string, any>;
230
+ signature: Signature;
231
+ classes: Record<string, string>;
232
+ };
233
+ export type ReadFlowRed = {
234
+ ok: false;
235
+ reds: string[];
236
+ };
237
+ export type ReadFlowResult = ReadFlowOk | ReadFlowRed;