fwdloop 0.3.0 → 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 +18 -4
- package/src/catalogue.js +350 -0
- package/src/catalogue.json +61 -0
- package/src/declaration.js +583 -0
- package/src/flow.js +312 -0
- package/src/index.js +12 -0
- package/src/signature.js +258 -0
- package/src/signed-text.js +419 -0
- package/src/types.js +233 -0
- package/types/catalogue.d.ts +76 -0
- package/types/declaration.d.ts +28 -0
- package/types/flow.d.ts +55 -0
- package/types/index.d.ts +5 -0
- package/types/signature.d.ts +78 -0
- package/types/signed-text.d.ts +14 -0
- package/types/types.d.ts +237 -0
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
// ported-from: poc/m0/validator.mjs@288cb8a (parseLines/resolveGuardrailClass/
|
|
2
|
+
// deriveFromLine/validate — the walkable chain, the fromLine/class
|
|
3
|
+
// derivation, the listing rule, and THE SEND LOCK block around lines
|
|
4
|
+
// 573-634) and poc/m0/artifacts.mjs@deae3da (ArtifactSpace/unwalkableReads —
|
|
5
|
+
// the declared-before check) and poc/m1/slots.mjs@92313d7 (checkAskSlots —
|
|
6
|
+
// the ask slot mechanism, incl. the "a pause is zero primitives AND hitl"
|
|
7
|
+
// fix). Rewritten for the M1 schema, never imported — src must not import
|
|
8
|
+
// from poc (CLAUDE.md: "borrow, never import"; complete separation between
|
|
9
|
+
// poc/ and src/).
|
|
10
|
+
//
|
|
11
|
+
// The send lock is a mechanism, not wording (F21/F24: a lock is typed
|
|
12
|
+
// grants/checks, never prompt phrasing). Generalised from M0b's single ask
|
|
13
|
+
// slot / single send slot to M1's arrays (`arbiter.asks[]`,
|
|
14
|
+
// `arbiter.sends[]`, M1 scope item 3 widened to "however many a human
|
|
15
|
+
// signs"): every signed ask and send line is checked, not just the last one
|
|
16
|
+
// seen. Unlike M0, which hard-coded the verb "write", rule (d) below checks
|
|
17
|
+
// the CATALOGUE's `class` field for the primitive instead — the send lock
|
|
18
|
+
// should not be tied to one verb's name.
|
|
19
|
+
//
|
|
20
|
+
// M1 piece 3 (docs/wiki/the-module-ladder.md, "M1 — scope, exit,
|
|
21
|
+
// negative — SIGNED", scope item 2): "the drafter's half is a schema with
|
|
22
|
+
// no arbiter fields in it." This module validates `declaration.json`
|
|
23
|
+
// against that closed schema, given the ALREADY-PARSED `arbiter`/`lines`
|
|
24
|
+
// from `parseSignedText` (src/signed-text.js) — it never parses prose
|
|
25
|
+
// itself, and it never imports the catalogue: the caller passes it as
|
|
26
|
+
// data (piece 4 makes it a data file; piece 3 only takes it as a param).
|
|
27
|
+
//
|
|
28
|
+
// `inputFacts`/`picks` replace M0's old per-input listing fields — the
|
|
29
|
+
// harness-supplied mechanical listing for one input, generalised from "one
|
|
30
|
+
// spreadsheet header" to "one input per signed source role" (M1 §10, sources as typed
|
|
31
|
+
// arbiter lines). The listing rule itself (M0a exit criterion, F17) is
|
|
32
|
+
// unchanged in spirit: every name a step picks must appear verbatim in the
|
|
33
|
+
// real, harness-supplied listing for that role — never derived from goal
|
|
34
|
+
// prose, never skipped when it cannot be checked.
|
|
35
|
+
//
|
|
36
|
+
// No `new RegExp` anywhere in this file — a test in test/signed-text.test.js
|
|
37
|
+
// greps all of src/ for that, and it must keep passing with this file added.
|
|
38
|
+
|
|
39
|
+
/** @typedef {import('./types.js').Arbiter} Arbiter */
|
|
40
|
+
/** @typedef {import('./types.js').SignedLine} SignedLine */
|
|
41
|
+
/** @typedef {import('./types.js').CatalogueEntry} CatalogueEntry */
|
|
42
|
+
/** @typedef {import('./types.js').ValidateDeclarationResult} ValidateDeclarationResult */
|
|
43
|
+
|
|
44
|
+
/** Every top-level, step-level and close-level field this schema defines —
|
|
45
|
+
* kept in sync with the checks below so a mutation suite can assert it
|
|
46
|
+
* tested every one of them (same trick as ARBITER_FIELDS/SIGNATURE_FIELDS). */
|
|
47
|
+
export const DECLARATION_FIELDS = Object.freeze([
|
|
48
|
+
// top level
|
|
49
|
+
'steps',
|
|
50
|
+
'guardrailClasses',
|
|
51
|
+
'unjudgeable',
|
|
52
|
+
'refused',
|
|
53
|
+
'inputFacts',
|
|
54
|
+
// step level
|
|
55
|
+
'goal',
|
|
56
|
+
'primitives',
|
|
57
|
+
'reads',
|
|
58
|
+
'emits',
|
|
59
|
+
'fromLine',
|
|
60
|
+
'close',
|
|
61
|
+
'picks',
|
|
62
|
+
// close level
|
|
63
|
+
'class',
|
|
64
|
+
'shape',
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
const VALID_CLASSES = Object.freeze(['green', 'softgreen', 'hitl']);
|
|
68
|
+
|
|
69
|
+
// The arbiter's own vocabulary — refused at any depth in a declaration, in
|
|
70
|
+
// any position, because the drafter never authors these (CLAUDE.md hard
|
|
71
|
+
// line: "the agent authors steps; it never authors the trigger, the cap,
|
|
72
|
+
// an ask's position, an ask's TTL, the egress allow-list...").
|
|
73
|
+
const ARBITER_KEYS = Object.freeze(['cap', 'ask', 'ttl', 'send', 'source', 'trigger', 'skills']);
|
|
74
|
+
|
|
75
|
+
const TOP_LEVEL_ALLOWED = Object.freeze(['steps', 'guardrailClasses', 'unjudgeable', 'refused', 'inputFacts']);
|
|
76
|
+
const STEP_ALLOWED = Object.freeze(['goal', 'primitives', 'reads', 'emits', 'fromLine', 'close', 'picks']);
|
|
77
|
+
const CLOSE_ALLOWED = Object.freeze(['class', 'shape']);
|
|
78
|
+
|
|
79
|
+
/** @returns {value is Record<string, any>} */
|
|
80
|
+
function isPlainObject(value) {
|
|
81
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Unknown-key + arbiter-key refusal for one object's OWN keys (not
|
|
85
|
+
* recursive) — arbiter words get their own, distinguishable red wording so
|
|
86
|
+
* it can never be mistaken for a plain unknown key. */
|
|
87
|
+
function checkOwnKeys(obj, allowed, path, reds) {
|
|
88
|
+
for (const key of Object.keys(obj)) {
|
|
89
|
+
if (ARBITER_KEYS.includes(key)) {
|
|
90
|
+
reds.push(`declaration: arbiter field "${key}" at ${path}.${key} — the drafter cannot author it`);
|
|
91
|
+
} else if (!allowed.includes(key)) {
|
|
92
|
+
reds.push(`declaration: unknown key "${key}" at ${path}.${key}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Arbiter-key refusal recursively through `close.shape` — any depth, any
|
|
98
|
+
* key named after the arbiter's vocabulary, in an object or nested inside
|
|
99
|
+
* an array. Every OTHER key inside `shape` is free (the typed shape
|
|
100
|
+
* vocabulary is not signed yet, per M1 scope item 2). */
|
|
101
|
+
function scanShapeForArbiterKeys(value, path, reds) {
|
|
102
|
+
if (Array.isArray(value)) {
|
|
103
|
+
value.forEach((entry, i) => scanShapeForArbiterKeys(entry, `${path}[${i}]`, reds));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (isPlainObject(value)) {
|
|
107
|
+
for (const key of Object.keys(value)) {
|
|
108
|
+
const childPath = `${path}.${key}`;
|
|
109
|
+
if (ARBITER_KEYS.includes(key)) {
|
|
110
|
+
reds.push(`declaration: arbiter field "${key}" at ${childPath} — the drafter cannot author it`);
|
|
111
|
+
}
|
|
112
|
+
scanShapeForArbiterKeys(value[key], childPath, reds);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Resolve one line's class from `guardrailClasses`, exactly as
|
|
119
|
+
* poc/m0/validator.mjs's `resolveGuardrailClass` does: blank guardrail is
|
|
120
|
+
* always hitl; no proposal for a non-blank guardrail is hitl (silence is
|
|
121
|
+
* safe); an invented proposal is a red, never normalised to hitl.
|
|
122
|
+
* Returns `{ ok: true, class }` or `{ ok: false, error }`.
|
|
123
|
+
*/
|
|
124
|
+
function resolveGuardrailClass(line, guardrailClasses) {
|
|
125
|
+
if (!line || !line.guardrail) {
|
|
126
|
+
return { ok: true, class: 'hitl' };
|
|
127
|
+
}
|
|
128
|
+
const key = String(line.n);
|
|
129
|
+
if (!Object.prototype.hasOwnProperty.call(guardrailClasses, key)) {
|
|
130
|
+
return { ok: true, class: 'hitl' };
|
|
131
|
+
}
|
|
132
|
+
const proposed = guardrailClasses[key];
|
|
133
|
+
if (VALID_CLASSES.includes(proposed)) {
|
|
134
|
+
return { ok: true, class: proposed };
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
ok: false,
|
|
138
|
+
error: `guardrailClasses line ${line.n} proposes class "${proposed}", which is not one of `
|
|
139
|
+
+ `${VALID_CLASSES.join(', ')} (an invented proposal, never normalised to hitl)`,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Same shape as poc/m0/validator.mjs's `deriveFromLine`: a step with no
|
|
144
|
+
* `fromLine` is silence -> hitl; a malformed or non-existent `fromLine` is
|
|
145
|
+
* a red; otherwise the class derives from that line's guardrail proposal. */
|
|
146
|
+
function deriveFromLine(fromLine, lines, guardrailClasses) {
|
|
147
|
+
if (fromLine === null || fromLine === undefined) {
|
|
148
|
+
return { ok: true, class: 'hitl', line: null };
|
|
149
|
+
}
|
|
150
|
+
if (!Number.isInteger(fromLine)) {
|
|
151
|
+
return { ok: false, error: `fromLine ${JSON.stringify(fromLine)} must be an integer` };
|
|
152
|
+
}
|
|
153
|
+
const line = lines.find((l) => l.n === fromLine);
|
|
154
|
+
if (!line) {
|
|
155
|
+
return { ok: false, error: `fromLine ${fromLine} does not name any of the signed numbered lines` };
|
|
156
|
+
}
|
|
157
|
+
const resolved = resolveGuardrailClass(line, guardrailClasses);
|
|
158
|
+
if (!resolved.ok) return { ok: false, error: resolved.error };
|
|
159
|
+
return { ok: true, class: resolved.class, line };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Validate a drafter declaration against the M1 closed schema. `arbiter`
|
|
164
|
+
* and `lines` are `parseSignedText`'s output; `catalogue` is a plain array
|
|
165
|
+
* of `{ verb, skill, class }`. Never throws — a bad declaration, a bad
|
|
166
|
+
* arbiter, a bad lines array, or a bad catalogue is a red, never an
|
|
167
|
+
* exception.
|
|
168
|
+
*
|
|
169
|
+
* @param {unknown} declaration
|
|
170
|
+
* @param {{ arbiter?: unknown, lines?: unknown, catalogue?: unknown }} [context]
|
|
171
|
+
* @returns {ValidateDeclarationResult}
|
|
172
|
+
*/
|
|
173
|
+
export function validateDeclaration(declaration, context = {}) {
|
|
174
|
+
const { arbiter, lines, catalogue } = context;
|
|
175
|
+
const reds = [];
|
|
176
|
+
|
|
177
|
+
if (!isPlainObject(declaration)) {
|
|
178
|
+
return deepFreeze({ ok: false, reds: ['declaration: must be an object'] });
|
|
179
|
+
}
|
|
180
|
+
if (!isPlainObject(arbiter)) {
|
|
181
|
+
reds.push('declaration: caller-supplied "arbiter" must be an object (parseSignedText output)');
|
|
182
|
+
}
|
|
183
|
+
if (!Array.isArray(lines)) {
|
|
184
|
+
reds.push('declaration: caller-supplied "lines" must be an array (parseSignedText output)');
|
|
185
|
+
}
|
|
186
|
+
if (!Array.isArray(catalogue)) {
|
|
187
|
+
reds.push('declaration: caller-supplied "catalogue" must be an array of {verb, skill, class}');
|
|
188
|
+
}
|
|
189
|
+
if (reds.length > 0) return deepFreeze({ ok: false, reds });
|
|
190
|
+
|
|
191
|
+
const safeLines = /** @type {SignedLine[]} */ (lines);
|
|
192
|
+
const safeArbiter = /** @type {Arbiter} */ (arbiter);
|
|
193
|
+
const skills = Array.isArray(safeArbiter.skills) ? safeArbiter.skills : [];
|
|
194
|
+
const sourceRoles = Array.isArray(safeArbiter.sources) ? safeArbiter.sources.map((s) => s?.role) : [];
|
|
195
|
+
const asks = Array.isArray(safeArbiter.asks) ? safeArbiter.asks : [];
|
|
196
|
+
const askLines = asks.map((a) => a.line);
|
|
197
|
+
const sends = Array.isArray(safeArbiter.sends) ? safeArbiter.sends : [];
|
|
198
|
+
const sendLines = sends.map((s) => s.line);
|
|
199
|
+
|
|
200
|
+
const catalogueByVerb = new Map();
|
|
201
|
+
for (const entry of /** @type {CatalogueEntry[]} */ (catalogue)) {
|
|
202
|
+
if (entry && typeof entry.verb === 'string') catalogueByVerb.set(entry.verb, entry);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// --- top level: unknown / arbiter keys -----------------------------------
|
|
206
|
+
checkOwnKeys(declaration, TOP_LEVEL_ALLOWED, 'declaration', reds);
|
|
207
|
+
|
|
208
|
+
// --- steps: must be a non-empty array, or nothing else here is meaningful
|
|
209
|
+
if (!Array.isArray(declaration.steps) || declaration.steps.length === 0) {
|
|
210
|
+
reds.push('declaration: "steps" must be a non-empty array');
|
|
211
|
+
return deepFreeze({ ok: false, reds });
|
|
212
|
+
}
|
|
213
|
+
const { steps } = declaration;
|
|
214
|
+
|
|
215
|
+
// --- guardrailClasses (REQUIRED — the harness always writes it; empty {}
|
|
216
|
+
// is fine, missing entirely is not) ------------------------------------
|
|
217
|
+
let guardrailClasses = {};
|
|
218
|
+
if (!Object.prototype.hasOwnProperty.call(declaration, 'guardrailClasses')) {
|
|
219
|
+
reds.push('declaration: "guardrailClasses" is required (an object keyed by line number; {} is fine)');
|
|
220
|
+
} else if (isPlainObject(declaration.guardrailClasses)) {
|
|
221
|
+
guardrailClasses = declaration.guardrailClasses;
|
|
222
|
+
for (const key of Object.keys(guardrailClasses)) {
|
|
223
|
+
const n = Number(key);
|
|
224
|
+
const line = Number.isInteger(n) ? safeLines.find((l) => l.n === n) : undefined;
|
|
225
|
+
if (!line || !line.guardrail) {
|
|
226
|
+
reds.push(`declaration: guardrailClasses key "${key}" does not name a signed line with a guardrail`);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const value = guardrailClasses[key];
|
|
230
|
+
if (!VALID_CLASSES.includes(value)) {
|
|
231
|
+
reds.push(`declaration: guardrailClasses line ${key} value ${JSON.stringify(value)} is not one of ${VALID_CLASSES.join(', ')}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
reds.push('declaration: "guardrailClasses" must be an object keyed by line number');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// --- unjudgeable (REQUIRED; {} is fine) ------------------------------------
|
|
239
|
+
let unjudgeable = {};
|
|
240
|
+
if (!Object.prototype.hasOwnProperty.call(declaration, 'unjudgeable')) {
|
|
241
|
+
reds.push('declaration: "unjudgeable" is required (an object keyed by line number; {} is fine)');
|
|
242
|
+
} else if (isPlainObject(declaration.unjudgeable)) {
|
|
243
|
+
unjudgeable = declaration.unjudgeable;
|
|
244
|
+
for (const [key, reason] of Object.entries(unjudgeable)) {
|
|
245
|
+
const n = Number(key);
|
|
246
|
+
if (!Number.isInteger(n)) {
|
|
247
|
+
reds.push(`declaration: unjudgeable key "${key}" must be an integer line number`);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (typeof reason !== 'string' || reason.length === 0) {
|
|
251
|
+
reds.push(`declaration: unjudgeable reason for line ${n} must be a non-empty string`);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const line = safeLines.find((l) => l.n === n);
|
|
255
|
+
if (!line || !line.guardrail) {
|
|
256
|
+
reds.push(`declaration: unjudgeable names line ${n}, which has no guardrail to be unjudgeable about`);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const proposed = isPlainObject(declaration.guardrailClasses) ? declaration.guardrailClasses[key] : undefined;
|
|
260
|
+
if (proposed !== undefined && proposed !== 'hitl') {
|
|
261
|
+
reds.push(`declaration: line ${n} is marked unjudgeable but guardrailClasses proposes "${proposed}" `
|
|
262
|
+
+ '— unjudgeable never upgrades or downgrades the class, it can only explain a hitl');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
} else {
|
|
266
|
+
reds.push('declaration: "unjudgeable" must be an object keyed by line number');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// --- refused (REQUIRED; [] is fine) ----------------------------------------
|
|
270
|
+
let refused = [];
|
|
271
|
+
if (!Object.prototype.hasOwnProperty.call(declaration, 'refused')) {
|
|
272
|
+
reds.push('declaration: "refused" is required (an array of {line, reason}; [] is fine)');
|
|
273
|
+
} else if (Array.isArray(declaration.refused)) {
|
|
274
|
+
refused = declaration.refused;
|
|
275
|
+
refused.forEach((entry, i) => {
|
|
276
|
+
if (!isPlainObject(entry)) {
|
|
277
|
+
reds.push(`declaration: refused[${i}] must be an object {line, reason}`);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (!Number.isInteger(entry.line)) {
|
|
281
|
+
reds.push(`declaration: refused[${i}].line must be an integer line number`);
|
|
282
|
+
} else if (!safeLines.some((l) => l.n === entry.line)) {
|
|
283
|
+
reds.push(`declaration: refused[${i}].line ${entry.line} does not name any signed numbered line`);
|
|
284
|
+
}
|
|
285
|
+
if (typeof entry.reason !== 'string' || entry.reason.length === 0) {
|
|
286
|
+
reds.push(`declaration: refused[${i}].reason must be a non-empty string`);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
} else {
|
|
290
|
+
reds.push('declaration: "refused" must be an array of {line, reason}');
|
|
291
|
+
}
|
|
292
|
+
const refusedLines = new Set(refused.map((r) => r?.line).filter((n) => Number.isInteger(n)));
|
|
293
|
+
|
|
294
|
+
// --- inputFacts (REQUIRED; {} is fine) -------------------------------------
|
|
295
|
+
let inputFacts = {};
|
|
296
|
+
if (!Object.prototype.hasOwnProperty.call(declaration, 'inputFacts')) {
|
|
297
|
+
reds.push('declaration: "inputFacts" is required (an object keyed by source role; {} is fine)');
|
|
298
|
+
} else if (isPlainObject(declaration.inputFacts)) {
|
|
299
|
+
inputFacts = declaration.inputFacts;
|
|
300
|
+
for (const [role, listing] of Object.entries(inputFacts)) {
|
|
301
|
+
if (!sourceRoles.includes(role)) {
|
|
302
|
+
reds.push(`declaration: inputFacts role "${role}" is not one of the signed source roles`);
|
|
303
|
+
}
|
|
304
|
+
if (!Array.isArray(listing) || listing.some((v) => typeof v !== 'string')) {
|
|
305
|
+
reds.push(`declaration: inputFacts["${role}"] must be an array of strings`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
reds.push('declaration: "inputFacts" must be an object keyed by source role');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// --- steps ------------------------------------------------------------------
|
|
313
|
+
const emitSeen = new Set();
|
|
314
|
+
const declaredEmits = new Set();
|
|
315
|
+
/** @type {Array<{ emits: string|null, effectiveClass: string, fromLine: number|null, primitives: string[], reads: string[] }>} */
|
|
316
|
+
const stepInfo = [];
|
|
317
|
+
|
|
318
|
+
steps.forEach((step, i) => {
|
|
319
|
+
const label = `steps[${i}]`;
|
|
320
|
+
if (!isPlainObject(step)) {
|
|
321
|
+
reds.push(`declaration: ${label} must be an object`);
|
|
322
|
+
stepInfo.push({
|
|
323
|
+
emits: null, effectiveClass: 'hitl', fromLine: null, primitives: [], reads: [],
|
|
324
|
+
});
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
checkOwnKeys(step, STEP_ALLOWED, `declaration.${label}`, reds);
|
|
329
|
+
|
|
330
|
+
// goal
|
|
331
|
+
if (typeof step.goal !== 'string' || step.goal.length === 0) {
|
|
332
|
+
reds.push(`declaration: ${label}.goal must be a non-empty string`);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// primitives
|
|
336
|
+
const primitives = Array.isArray(step.primitives) ? step.primitives : null;
|
|
337
|
+
if (primitives === null) {
|
|
338
|
+
reds.push(`declaration: ${label}.primitives must be an array of catalogue verbs`);
|
|
339
|
+
} else {
|
|
340
|
+
for (const verb of primitives) {
|
|
341
|
+
const entry = typeof verb === 'string' ? catalogueByVerb.get(verb) : undefined;
|
|
342
|
+
if (!entry) {
|
|
343
|
+
reds.push(`declaration: ${label}.primitives names "${verb}", which is not in the catalogue`);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (!skills.includes(entry.skill)) {
|
|
347
|
+
reds.push(`declaration: ${label}.primitives "${verb}" needs skill "${entry.skill}", which is not signed (skills: ${skills.join(', ') || 'none'})`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// reads
|
|
353
|
+
const reads = Array.isArray(step.reads) ? step.reads : null;
|
|
354
|
+
if (reads === null) {
|
|
355
|
+
reds.push(`declaration: ${label}.reads must be an array of artifact ids`);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// emits
|
|
359
|
+
let emits = null;
|
|
360
|
+
if (typeof step.emits !== 'string' || step.emits.length === 0) {
|
|
361
|
+
reds.push(`declaration: ${label}.emits must be a non-empty string`);
|
|
362
|
+
} else {
|
|
363
|
+
emits = step.emits;
|
|
364
|
+
if (emitSeen.has(emits)) {
|
|
365
|
+
reds.push(`declaration: ${label}.emits "${emits}" is a duplicate — one writer per artifact`);
|
|
366
|
+
}
|
|
367
|
+
emitSeen.add(emits);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// walkable chain — every read must have been emitted by an EARLIER step
|
|
371
|
+
if (reads !== null && emits !== null) {
|
|
372
|
+
for (const readId of reads) {
|
|
373
|
+
if (readId === emits || !declaredEmits.has(readId)) {
|
|
374
|
+
reds.push(`declaration: ${label} reads artifact "${readId}" that no earlier step declared`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (emits !== null) declaredEmits.add(emits);
|
|
379
|
+
|
|
380
|
+
// fromLine + class derivation — the key itself is required (value may be
|
|
381
|
+
// null, meaning "no line"; the key being entirely absent is not the same
|
|
382
|
+
// thing and is its own red).
|
|
383
|
+
let effectiveClass = 'hitl';
|
|
384
|
+
let fromLine = null;
|
|
385
|
+
if (!Object.prototype.hasOwnProperty.call(step, 'fromLine')) {
|
|
386
|
+
reds.push(`declaration: ${label}.fromLine is required (an integer naming a real line, or null)`);
|
|
387
|
+
} else {
|
|
388
|
+
fromLine = step.fromLine;
|
|
389
|
+
if (fromLine !== null && typeof fromLine !== 'number') {
|
|
390
|
+
reds.push(`declaration: ${label}.fromLine must be an integer or null`);
|
|
391
|
+
} else {
|
|
392
|
+
const resolved = deriveFromLine(fromLine, safeLines, guardrailClasses);
|
|
393
|
+
if (!resolved.ok) {
|
|
394
|
+
reds.push(`declaration: ${label} ${resolved.error}`);
|
|
395
|
+
} else {
|
|
396
|
+
effectiveClass = resolved.class;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// close
|
|
402
|
+
if (step.close !== undefined && step.close !== null) {
|
|
403
|
+
if (!isPlainObject(step.close)) {
|
|
404
|
+
reds.push(`declaration: ${label}.close must be an object`);
|
|
405
|
+
} else {
|
|
406
|
+
checkOwnKeys(step.close, CLOSE_ALLOWED, `declaration.${label}.close`, reds);
|
|
407
|
+
const declaredClass = step.close.class;
|
|
408
|
+
if (declaredClass !== undefined) {
|
|
409
|
+
if (!VALID_CLASSES.includes(declaredClass)) {
|
|
410
|
+
reds.push(`declaration: ${label}.close.class ${JSON.stringify(declaredClass)} is not one of ${VALID_CLASSES.join(', ')}`);
|
|
411
|
+
} else if (declaredClass !== effectiveClass) {
|
|
412
|
+
reds.push(`declaration: ${label}.close.class "${declaredClass}" does not match "${effectiveClass}", `
|
|
413
|
+
+ 'which is what fromLine derives to — a step cannot claim a class its own guardrail does not produce');
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (step.close.shape !== undefined && step.close.shape !== null) {
|
|
417
|
+
if (!isPlainObject(step.close.shape)) {
|
|
418
|
+
reds.push(`declaration: ${label}.close.shape must be an object`);
|
|
419
|
+
} else {
|
|
420
|
+
scanShapeForArbiterKeys(step.close.shape, `declaration.${label}.close.shape`, reds);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// picks — the listing rule, generalised
|
|
427
|
+
if (step.picks !== undefined) {
|
|
428
|
+
if (!isPlainObject(step.picks)) {
|
|
429
|
+
reds.push(`declaration: ${label}.picks must be an object keyed by source role`);
|
|
430
|
+
} else {
|
|
431
|
+
for (const [role, names] of Object.entries(step.picks)) {
|
|
432
|
+
const listing = Object.prototype.hasOwnProperty.call(inputFacts, role) ? inputFacts[role] : undefined;
|
|
433
|
+
if (listing === undefined) {
|
|
434
|
+
reds.push(`declaration: ${label}.picks names role "${role}", which is missing from declaration.inputFacts`);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (!Array.isArray(names)) {
|
|
438
|
+
reds.push(`declaration: ${label}.picks["${role}"] must be an array of strings`);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
for (const name of names) {
|
|
442
|
+
if (typeof name !== 'string' || !listing.includes(name)) {
|
|
443
|
+
reds.push(`declaration: ${label}.picks["${role}"] names "${name}" — invented name, not in `
|
|
444
|
+
+ `declaration.inputFacts["${role}"] (${listing.join(', ')})`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
stepInfo.push({
|
|
452
|
+
emits,
|
|
453
|
+
effectiveClass,
|
|
454
|
+
fromLine: fromLine ?? null,
|
|
455
|
+
primitives: primitives ?? [],
|
|
456
|
+
reads: reads ?? [],
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// --- ask slots (ported from poc/m1/slots.mjs's checkAskSlots) --------------
|
|
461
|
+
// (a) exactly one hitl step bound to each signed ask line.
|
|
462
|
+
for (const n of askLines) {
|
|
463
|
+
const matches = stepInfo.filter((st) => st.fromLine === n);
|
|
464
|
+
if (matches.length === 0) {
|
|
465
|
+
reds.push(`declaration: ask at line ${n} has no step bound to it`);
|
|
466
|
+
} else if (matches.length >= 2) {
|
|
467
|
+
reds.push(`declaration: ask at line ${n} has ${matches.length} steps bound to it — exactly 1 required`);
|
|
468
|
+
} else if (matches[0].effectiveClass !== 'hitl') {
|
|
469
|
+
reds.push(`declaration: ask at line ${n} step's effective class is "${matches[0].effectiveClass}" — must be "hitl"`);
|
|
470
|
+
}
|
|
471
|
+
// (a2) M1 amendment 3 item 4: a marked line is ONLY the stop — the one
|
|
472
|
+
// step bound to it grants no primitive. Independent of (and collected
|
|
473
|
+
// alongside) the class check above: a step can be both the wrong class
|
|
474
|
+
// AND carrying work, and both reds should surface.
|
|
475
|
+
if (matches.length === 1 && matches[0].primitives.length > 0) {
|
|
476
|
+
const stepIdx = stepInfo.indexOf(matches[0]);
|
|
477
|
+
reds.push(`declaration: ask at line ${n} (steps[${stepIdx}]) is a stop only — granting primitive(s) `
|
|
478
|
+
+ `[${matches[0].primitives.join(', ')}] is work, which belongs on its own line`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
// (b) no step, anywhere, grants "checkpoint" — that belongs to the runner.
|
|
482
|
+
stepInfo.forEach((st, i) => {
|
|
483
|
+
if (st.primitives.includes('checkpoint')) {
|
|
484
|
+
reds.push(`declaration: steps[${i}] (line ${st.fromLine}) grants "checkpoint" — the pause belongs to the runner, never the drafter`);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
// (c) no pause (zero primitives AND effective class hitl) at an unsigned line.
|
|
488
|
+
const signedAskLines = new Set(askLines);
|
|
489
|
+
stepInfo.forEach((st, i) => {
|
|
490
|
+
const isSignedAskLine = st.fromLine !== null && signedAskLines.has(st.fromLine);
|
|
491
|
+
if (st.primitives.length === 0 && st.effectiveClass === 'hitl' && !isSignedAskLine) {
|
|
492
|
+
reds.push(`declaration: steps[${i}] (line ${st.fromLine}) is a pause (no primitives, hitl) at an unsigned line`);
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
// --- THE SEND LOCK (ported from poc/m0/validator.mjs@288cb8a, the block
|
|
497
|
+
// "THE SEND LOCK (M0b Part 1...)" around lines 573-634) — a mechanism, not
|
|
498
|
+
// wording (F21/F24: a lock is typed grants, never wording). Generalised
|
|
499
|
+
// from M0b's single ask/send slot to M1's arrays (`arbiter.asks[]`,
|
|
500
|
+
// `arbiter.sends[]`): every signed slot is checked, not just the last one.
|
|
501
|
+
//
|
|
502
|
+
// (a) every signed ask/send line must be a real numbered line. Redundant
|
|
503
|
+
// with what parseSignedText already guarantees on its own arbiter — this
|
|
504
|
+
// validator does not trust its caller either, same discipline as the rest
|
|
505
|
+
// of this file.
|
|
506
|
+
const lineNumbers = new Set(safeLines.map((l) => l.n));
|
|
507
|
+
for (const n of askLines) {
|
|
508
|
+
if (!lineNumbers.has(n)) reds.push(`declaration: signed ask at line ${n} does not name a real numbered line`);
|
|
509
|
+
}
|
|
510
|
+
for (const n of sendLines) {
|
|
511
|
+
if (!lineNumbers.has(n)) reds.push(`declaration: signed send at line ${n} does not name a real numbered line`);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// (b) a signed ask or send line can never be refused.
|
|
515
|
+
for (const n of askLines) {
|
|
516
|
+
if (refusedLines.has(n)) reds.push(`declaration: line ${n} is the signed ask slot and cannot be refused`);
|
|
517
|
+
}
|
|
518
|
+
for (const n of sendLines) {
|
|
519
|
+
if (refusedLines.has(n)) reds.push(`declaration: line ${n} is the signed send slot and cannot be refused`);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// (c)-(e) — the send step itself: exactly one step bound to it, granted a
|
|
523
|
+
// catalogue primitive of class "write" (not hard-coded to the verb
|
|
524
|
+
// "write" itself, per the coordinator's note — a catalogue is exactly the
|
|
525
|
+
// place that vocabulary belongs), and reading the `emits` of at least one
|
|
526
|
+
// ask step at a strictly earlier signed ask line.
|
|
527
|
+
for (const n of sendLines) {
|
|
528
|
+
const matches = stepInfo.map((st, i) => ({ st, i })).filter((x) => x.st.fromLine === n);
|
|
529
|
+
if (matches.length === 0) {
|
|
530
|
+
reds.push(`declaration: send at line ${n} has no step bound to it`);
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
if (matches.length >= 2) {
|
|
534
|
+
reds.push(`declaration: send at line ${n} has ${matches.length} steps bound to it (steps[${matches.map((m) => m.i).join(', ')}]) — exactly 1 required`);
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
const { st: sendStep, i: sendIdx } = matches[0];
|
|
538
|
+
|
|
539
|
+
const hasWrite = sendStep.primitives.some((verb) => catalogueByVerb.get(verb)?.class === 'write');
|
|
540
|
+
if (!hasWrite) {
|
|
541
|
+
reds.push(`declaration: send at line ${n} (steps[${sendIdx}]) is not granted a catalogue primitive of class "write"`);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const earlierAskLines = askLines.filter((a) => a < n);
|
|
545
|
+
if (earlierAskLines.length === 0) {
|
|
546
|
+
reds.push(`declaration: send at line ${n} (steps[${sendIdx}]) has no signed ask at an earlier line to read from`);
|
|
547
|
+
} else {
|
|
548
|
+
const earlierAskEmits = earlierAskLines
|
|
549
|
+
.map((a) => stepInfo.find((st) => st.fromLine === a))
|
|
550
|
+
.filter((st) => st !== undefined)
|
|
551
|
+
.map((st) => st.emits);
|
|
552
|
+
const readsAnAsk = earlierAskEmits.some((emits) => emits !== null && sendStep.reads.includes(emits));
|
|
553
|
+
if (!readsAnAsk) {
|
|
554
|
+
reds.push(`declaration: send at line ${n} (steps[${sendIdx}]) does not read the emits of any earlier signed `
|
|
555
|
+
+ `ask step (asks at lines ${earlierAskLines.join(', ')})`);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// --- check 6: every numbered line must be served or refused, never dropped
|
|
561
|
+
const claimedLines = new Set(stepInfo.map((st) => st.fromLine).filter((n) => Number.isInteger(n)));
|
|
562
|
+
for (const line of safeLines) {
|
|
563
|
+
if (!claimedLines.has(line.n) && !refusedLines.has(line.n)) {
|
|
564
|
+
reds.push(`declaration: job line ${line.n} ("${line.text}") is neither served by any step's fromLine nor refused with a reason`);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (reds.length > 0) return deepFreeze({ ok: false, reds });
|
|
569
|
+
|
|
570
|
+
const classes = {};
|
|
571
|
+
stepInfo.forEach((st) => {
|
|
572
|
+
if (st.emits !== null) classes[st.emits] = st.effectiveClass;
|
|
573
|
+
});
|
|
574
|
+
return deepFreeze({ ok: true, classes });
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function deepFreeze(value) {
|
|
578
|
+
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
579
|
+
Object.freeze(value);
|
|
580
|
+
for (const key of Object.keys(value)) deepFreeze(value[key]);
|
|
581
|
+
}
|
|
582
|
+
return value;
|
|
583
|
+
}
|