@gtrabanco/agentic-workflow-schema 3.4.0 → 4.0.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,676 @@
1
+ /**
2
+ * Canonical internal definition of the two pre-execution evidence contracts.
3
+ *
4
+ * This module is the SINGLE source of the `PreExecutionArtifactSnapshot v1` and
5
+ * `PreExecutionReviewReceipt v1` structural contract: the closed field lists,
6
+ * vocabularies, bounds, patterns, cross-field rules and named runtime-only
7
+ * semantics declared here are consumed by
8
+ * - the two authoritative runtime entries in `index.ts` (which reuse
9
+ * `validateStructure`/`projectStructure` from `verification-contract.ts`, so
10
+ * the package keeps ONE structural engine), and
11
+ * - the deterministic Draft-07 structural projection generator
12
+ * (`scripts/generate-pre-execution-schemas.mjs`).
13
+ *
14
+ * It is package-internal: it is not re-exported from the package root and the
15
+ * published `exports` map does not expose it. The generated JSON Schemas are
16
+ * structural projections of this definition, never a second semantic authority
17
+ * (AD-007). The rules listed in `RUNTIME_RULES` are deliberately not expressible
18
+ * in Draft-07; the projections disclose them instead of implying they are checked.
19
+ *
20
+ * Feature 28, phase P1. The contracts prove what an independent reviewer read
21
+ * BEFORE implementation; they never replace the candidate `ReviewReceipt v1`
22
+ * (feature 25) or the staged `VerificationReceipt v1` (feature 26).
23
+ */
24
+ // ---------------------------------------------------------------------------
25
+ // Contract identities and closed vocabularies (declared once)
26
+ // ---------------------------------------------------------------------------
27
+ export const PRE_EXECUTION_SNAPSHOT_CONTRACT_ID = "agentic-workflow/pre-execution-artifact-snapshot@1";
28
+ export const PRE_EXECUTION_RECEIPT_CONTRACT_ID = "agentic-workflow/pre-execution-review-receipt@1";
29
+ /** The two artifact-selection modes. `spec-product-v1` is the Product projection. */
30
+ export const PRE_EXECUTION_SELECTORS = Object.freeze(["whole-file", "spec-product-v1"]);
31
+ /** The reviewed stage. A fix unit has no SPEC stage (D6). */
32
+ export const PRE_EXECUTION_STAGES = Object.freeze(["spec", "plan"]);
33
+ /** The unit family the snapshot belongs to. */
34
+ export const PRE_EXECUTION_UNIT_KINDS = Object.freeze(["feature", "fix"]);
35
+ /**
36
+ * The artifact roles a pre-execution snapshot may bind. Deliberately excludes
37
+ * execution progress (`progress.md`), raw exploration history, and findings
38
+ * resolution: none of them are Plan authority (SPEC Design §2).
39
+ */
40
+ export const PRE_EXECUTION_ARTIFACT_KINDS = Object.freeze([
41
+ "spec",
42
+ "acceptance",
43
+ "plan",
44
+ "tasks",
45
+ "testing",
46
+ "decisions",
47
+ "architecture-notes",
48
+ "planning-evidence",
49
+ "obligations",
50
+ ]);
51
+ /** Authoritative sources a reviewer was allowed to read besides the artifacts. */
52
+ export const PRE_EXECUTION_CONTEXT_KINDS = Object.freeze([
53
+ "roadmap-row",
54
+ "governing-issue",
55
+ "normalized-repository-state",
56
+ "architectural-invariants",
57
+ "dependency-unit",
58
+ "project-guide",
59
+ ]);
60
+ /**
61
+ * A context is either present (and bound by digest) or explicitly absent (and
62
+ * bound by `null`). "I did not look" is not representable, which is the point:
63
+ * an unknown stays owned by a named row instead of becoming a silent assumption.
64
+ */
65
+ export const PRE_EXECUTION_CONTEXT_PRESENCE = Object.freeze(["present", "absent"]);
66
+ /** The five public review verdicts. There is no generic "approve" verb. */
67
+ export const PRE_EXECUTION_VERDICTS = Object.freeze([
68
+ "spec-review-pass",
69
+ "spec-review-fail",
70
+ "plan-review-pass",
71
+ "plan-review-fail",
72
+ "needs-design",
73
+ ]);
74
+ /** Severities mirror the candidate-review ladder; `info` is the only immaterial one. */
75
+ export const PRE_EXECUTION_FINDING_SEVERITIES = Object.freeze([
76
+ "info", "low", "medium", "high", "critical",
77
+ ]);
78
+ /** The five root-cause owners a finding can be routed to (SPEC Design §4). */
79
+ export const PRE_EXECUTION_FINDING_CLASSES = Object.freeze([
80
+ "product", "plan", "source", "environment", "runtime",
81
+ ]);
82
+ export const PRE_EXECUTION_FINDING_VERIFICATION = Object.freeze(["verified", "unverified"]);
83
+ export const PRE_EXECUTION_FINDING_RESOLUTIONS = Object.freeze(["open", "resolved", "dismissed"]);
84
+ export const PRE_EXECUTION_REVIEW_ROLES = Object.freeze([
85
+ "reviewer", "critic", "synthesizer", "arbiter",
86
+ ]);
87
+ /** The reviewer roles allowed to carry parent receipts (topology, not rank). */
88
+ export const PARENT_CAPABLE_REVIEWER_ROLES = Object.freeze([
89
+ "critic", "synthesizer", "arbiter",
90
+ ]);
91
+ export const PRE_EXECUTION_PARENT_ROLES = Object.freeze(["critic", "synthesis", "arbitration"]);
92
+ export const PRE_EXECUTION_AUTHOR_EXCLUSIONS = Object.freeze(["enforced", "not-enforceable"]);
93
+ /** Which verdicts belong to which stage. There is no generic "approve". */
94
+ export const VERDICTS_BY_STAGE = Object.freeze({
95
+ spec: ["spec-review-pass", "spec-review-fail", "needs-design"],
96
+ plan: ["plan-review-pass", "plan-review-fail", "needs-design"],
97
+ });
98
+ export const PRE_EXECUTION_MODEL_DIVERSITY = Object.freeze([
99
+ "same-model", "cross-model", "not-applicable",
100
+ ]);
101
+ /**
102
+ * Bounded usability. Every number the validator enforces lives here and is
103
+ * published, so a consumer sizes fixtures without restating a literal.
104
+ *
105
+ * The byte budgets are reachable *below* the shape ceilings on purpose: a
106
+ * snapshot of `artifacts` rows whose paths each sit at `pathChars` is already
107
+ * larger than `snapshotBytes`, so the payload budget is the first refusal a wide
108
+ * document earns and the diagnostic ceiling is never spent on it.
109
+ */
110
+ export const PRE_EXECUTION_LIMITS = Object.freeze({
111
+ artifacts: 32,
112
+ contexts: 16,
113
+ findings: 64,
114
+ evidencePerFinding: 8,
115
+ parentReceipts: 8,
116
+ receiptDiagnostics: 8,
117
+ unitIdChars: 128,
118
+ revisionIdChars: 128,
119
+ idChars: 128,
120
+ pathChars: 1024,
121
+ identifierChars: 160,
122
+ claimChars: 2048,
123
+ evidenceChars: 1024,
124
+ resolutionEvidenceChars: 2048,
125
+ policyChars: 64,
126
+ diagnosticChars: 512,
127
+ artifactBytes: 4 * 1024 * 1024,
128
+ snapshotBytes: 32 * 1024,
129
+ receiptBytes: 64 * 1024,
130
+ /** Shared ceiling of the one package-wide diagnostic sink. */
131
+ diagnostics: 50,
132
+ });
133
+ /**
134
+ * The rules a Draft-07 projection cannot express. The generator lists these ids,
135
+ * with their human-readable claim, in the projection `$comment`, so an editor
136
+ * that validates against the projection knows exactly what it did NOT check.
137
+ */
138
+ /**
139
+ * The rules the runtime enforces that Draft-07 cannot express, named and claimed.
140
+ * The generator appends the ids of every non-projectable cross-field rule it
141
+ * finds in the definition, so this list and the rule tables must agree — the
142
+ * projection test fails if a rule is enforced but undisclosed.
143
+ */
144
+ export const PRE_EXECUTION_RUNTIME_RULES = Object.freeze({
145
+ snapshot: [
146
+ { id: "stage-required-artifacts", claim: "stage drives the required artifact set" },
147
+ { id: "selector-requires-product-stage", claim: "spec-product-v1 requires stage spec on the spec row" },
148
+ { id: "artifact-order-is-canonical", claim: "artifact rows are ordered by UTF-8 path bytes" },
149
+ { id: "one-row-per-kind", claim: "each artifact kind appears at most once" },
150
+ { id: "context-order-is-canonical", claim: "context rows are ordered by kind then identifier" },
151
+ { id: "one-context-per-identity", claim: "each (kind, identifier) context appears once" },
152
+ { id: "fix-has-no-product-stage", claim: "unitKind fix cannot carry a SPEC-stage snapshot" },
153
+ ],
154
+ receipt: [
155
+ { id: "finding-requires-evidence", claim: "every finding carries at least one evidence reference" },
156
+ { id: "pass-requires-resolved-material-findings", claim: "PASS cannot coexist with an open or unverified material finding" },
157
+ { id: "dismissal-requires-counter-evidence", claim: "only recorded counter-evidence dismisses a finding" },
158
+ { id: "author-exclusion-where-enforced", claim: "an enforcing runtime rejects a reused author identity" },
159
+ { id: "pass-requires-clean-context", claim: "PASS requires context-clean evidence" },
160
+ { id: "unique-parent-digests", claim: "parent receipt digests are unique" },
161
+ { id: "receipt-bound-to-snapshot", claim: "the verdict must bind the exact snapshot digest" },
162
+ ],
163
+ });
164
+ // ---------------------------------------------------------------------------
165
+ // Shared field vocabulary
166
+ // ---------------------------------------------------------------------------
167
+ const NUL_MESSAGE = "must not contain NUL characters";
168
+ const ISO_8601_PATTERN = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$";
169
+ const LOWERCASE_64HEX_PATTERN = "^[a-f0-9]{64}$";
170
+ /** A git object id is either the historical SHA-1 or a SHA-256 length. */
171
+ const SOURCE_REVISION_PATTERN = "^[a-f0-9]{40}$|^[a-f0-9]{64}$";
172
+ /**
173
+ * Opaque, bounded, transport-safe identifiers (`28-unit`, `rev-0002`,
174
+ * `docs/fix/74-unit`, `review-0001`). They are compared, never resolved, so no path
175
+ * normalization applies — but a leading `-` and every control character are refused,
176
+ * because these values cross CLI and JSON boundaries on the way to a driver.
177
+ */
178
+ const OPAQUE_ID_PATTERN = "^[A-Za-z0-9][A-Za-z0-9._:/-]*$";
179
+ const RELATIVE_PATH_RULES = [
180
+ { regex: "^(?!$)", message: "must not be empty" },
181
+ { regex: "^(?!/)", message: 'must not be an absolute path (leading "/")' },
182
+ { regex: "^(?![A-Za-z]:)", message: "must not be a Windows drive-letter path" },
183
+ { regex: "^(?!.*\\\\)", message: "must not contain backslashes" },
184
+ { regex: "^(?!.*(\\/|^)\\.\\.?($|\\/))", message: 'must not contain "." or ".." segments' },
185
+ { regex: "^(?!.*\\/$)", message: "must not end with a separator" },
186
+ { regex: "^(?!.*//)", message: "must not contain an empty segment" },
187
+ ];
188
+ function opaqueId(key, description, maxLength = PRE_EXECUTION_LIMITS.idChars) {
189
+ return {
190
+ key,
191
+ type: "string",
192
+ minLength: 1,
193
+ maxLength,
194
+ nulFree: true,
195
+ pattern: OPAQUE_ID_PATTERN,
196
+ violationCode: "invalid-value",
197
+ description: `${description} Opaque: compared, never parsed, and never echoed back by a diagnostic.`,
198
+ };
199
+ }
200
+ // ---------------------------------------------------------------------------
201
+ // PreExecutionArtifactSnapshot v1
202
+ // ---------------------------------------------------------------------------
203
+ const ARTIFACT_ROW_SPEC = {
204
+ description: "One artifact bound into a pre-execution snapshot.",
205
+ fields: [
206
+ {
207
+ key: "kind",
208
+ type: "enum",
209
+ enum: PRE_EXECUTION_ARTIFACT_KINDS,
210
+ description: "Artifact role inside the stage's authoritative set.",
211
+ },
212
+ {
213
+ key: "path",
214
+ type: "string",
215
+ // Non-empty is expressed as a pattern, not `minLength`: an un-named file is
216
+ // a malformed PATH (`invalid-value`), not a capacity failure, and a driver
217
+ // routing on the code must be able to tell the two apart.
218
+ maxLength: PRE_EXECUTION_LIMITS.pathChars,
219
+ nulFree: true,
220
+ rules: RELATIVE_PATH_RULES,
221
+ violationCode: "invalid-value",
222
+ description: `Normalized repository-relative path; at most ${PRE_EXECUTION_LIMITS.pathChars} characters.`,
223
+ },
224
+ {
225
+ key: "selector",
226
+ type: "enum",
227
+ enum: PRE_EXECUTION_SELECTORS,
228
+ description: "How the bound bytes were selected out of the file.",
229
+ },
230
+ {
231
+ key: "byteLength",
232
+ type: "integer",
233
+ minimum: 0,
234
+ maximum: PRE_EXECUTION_LIMITS.artifactBytes,
235
+ description: `UTF-8 length of the SELECTION (a Product projection is smaller than its file), at most ${PRE_EXECUTION_LIMITS.artifactBytes} bytes.`,
236
+ },
237
+ {
238
+ key: "digest",
239
+ type: "string",
240
+ minLength: 64,
241
+ maxLength: 64,
242
+ nulFree: true,
243
+ pattern: LOWERCASE_64HEX_PATTERN,
244
+ violationCode: "invalid-value",
245
+ description: "Lowercase SHA-256 of the selected bytes.",
246
+ },
247
+ ],
248
+ // Path/kind uniqueness and byte ordering are properties of the COLLECTION, so
249
+ // they are declared on the snapshot root (see `SNAPSHOT_ROOT_SPEC`) — a row
250
+ // cannot see its siblings.
251
+ rules: [],
252
+ };
253
+ const CONTEXT_ROW_SPEC = {
254
+ description: "One authoritative context the reviewer was allowed to rely on.",
255
+ fields: [
256
+ { key: "kind", type: "enum", enum: PRE_EXECUTION_CONTEXT_KINDS, description: "Context authority." },
257
+ {
258
+ key: "identifier",
259
+ type: "string",
260
+ minLength: 1,
261
+ maxLength: PRE_EXECUTION_LIMITS.identifierChars,
262
+ nulFree: true,
263
+ // A leading `#` is legal HERE and nowhere else: `#146` is how a governing
264
+ // issue gets named in this ecosystem, and refusing it would push reviewers
265
+ // into writing the less checkable "146".
266
+ pattern: "^[#A-Za-z0-9][A-Za-z0-9._:/#-]*$",
267
+ violationCode: "invalid-value",
268
+ description: `Stable identifier (a roadmap row, an issue number such as #146, a snapshot id); at most ${PRE_EXECUTION_LIMITS.identifierChars} characters.`,
269
+ },
270
+ { key: "presence", type: "enum", enum: PRE_EXECUTION_CONTEXT_PRESENCE, description: "Whether the authority exists." },
271
+ {
272
+ key: "digest",
273
+ type: "nullable",
274
+ nullableOf: "string",
275
+ minLength: 64,
276
+ maxLength: 64,
277
+ nulFree: true,
278
+ pattern: LOWERCASE_64HEX_PATTERN,
279
+ violationCode: "invalid-value",
280
+ description: "Lowercase SHA-256 of the context bytes when present; exactly null when absent.",
281
+ },
282
+ ],
283
+ rules: [
284
+ {
285
+ id: "present-context-has-digest",
286
+ description: "A present context binds a digest.",
287
+ projectable: true,
288
+ kind: "non-null-when",
289
+ when: { field: "presence", equals: "present" },
290
+ fields: ["digest"],
291
+ },
292
+ {
293
+ id: "absent-context-has-null-digest",
294
+ description: "An absent context binds exactly `null`.",
295
+ projectable: true,
296
+ kind: "null-when",
297
+ when: { field: "presence", equals: "absent" },
298
+ fields: ["digest"],
299
+ },
300
+ ],
301
+ };
302
+ const SNAPSHOT_ROOT_SPEC = {
303
+ description: "The exact artifact set one independent reviewer read.",
304
+ fields: [
305
+ {
306
+ key: "contract",
307
+ type: "const",
308
+ value: PRE_EXECUTION_SNAPSHOT_CONTRACT_ID,
309
+ violationCode: "invalid-value",
310
+ description: "Contract identifier.",
311
+ },
312
+ { key: "stage", type: "enum", enum: PRE_EXECUTION_STAGES, description: "Reviewed stage." },
313
+ { key: "unitKind", type: "enum", enum: PRE_EXECUTION_UNIT_KINDS, description: "Unit family." },
314
+ opaqueId("unitId", `Roadmap or fix-unit identity; at most ${PRE_EXECUTION_LIMITS.unitIdChars} characters.`, PRE_EXECUTION_LIMITS.unitIdChars),
315
+ {
316
+ key: "sourceRevision",
317
+ type: "string",
318
+ minLength: 40,
319
+ maxLength: 64,
320
+ nulFree: true,
321
+ pattern: SOURCE_REVISION_PATTERN,
322
+ violationCode: "invalid-value",
323
+ description: "Exact repository revision the artifacts were read at (SHA-1 or SHA-256 object id).",
324
+ },
325
+ opaqueId("artifactRevisionId", `Authoring-owned revision identity; at most ${PRE_EXECUTION_LIMITS.revisionIdChars} characters.`, PRE_EXECUTION_LIMITS.revisionIdChars),
326
+ {
327
+ key: "artifacts",
328
+ type: "array",
329
+ specName: "PreExecutionArtifactRowV1",
330
+ minItems: 1,
331
+ maxItems: PRE_EXECUTION_LIMITS.artifacts,
332
+ description: `Ordered artifact rows; 1..${PRE_EXECUTION_LIMITS.artifacts}, ordered by UTF-8 path bytes.`,
333
+ },
334
+ {
335
+ key: "contexts",
336
+ type: "array",
337
+ specName: "PreExecutionContextBindingV1",
338
+ maxItems: PRE_EXECUTION_LIMITS.contexts,
339
+ description: `Authoritative context bindings; 0..${PRE_EXECUTION_LIMITS.contexts}, ordered by kind then identifier.`,
340
+ },
341
+ {
342
+ key: "parentSpecSnapshotDigest",
343
+ type: "nullable",
344
+ nullableOf: "string",
345
+ minLength: 64,
346
+ maxLength: 64,
347
+ nulFree: true,
348
+ pattern: LOWERCASE_64HEX_PATTERN,
349
+ violationCode: "invalid-value",
350
+ description: "Digest of the reviewed Product snapshot: required for a feature Plan snapshot, exactly null at the SPEC stage and for a fix unit (it has no Product half to bind, D6).",
351
+ },
352
+ ],
353
+ rules: [
354
+ {
355
+ id: "spec-stage-has-no-parent",
356
+ description: "A SPEC snapshot is the root of its own lineage.",
357
+ projectable: true,
358
+ kind: "null-when",
359
+ when: { field: "stage", equals: "spec" },
360
+ fields: ["parentSpecSnapshotDigest"],
361
+ },
362
+ {
363
+ id: "plan-stage-requires-parent",
364
+ description: "A Plan snapshot of a FEATURE unit binds the exact reviewed Product snapshot.",
365
+ projectable: true,
366
+ kind: "non-null-when",
367
+ // RS14: the only sanctioned `stage: spec` binding is `spec-product-v1`, and a
368
+ // fix SPEC has no `Size`/`Product half`/`Design status` to project — so a fix
369
+ // unit can never produce the parent this rule used to demand, and its
370
+ // `review-plan` was blocked by construction. The parent is lineage over a
371
+ // Product review that exists, which means `stage == plan AND unitKind == feature`.
372
+ when: {
373
+ allOf: [
374
+ { field: "stage", equals: "plan" },
375
+ { field: "unitKind", equals: "feature" },
376
+ ],
377
+ },
378
+ fields: ["parentSpecSnapshotDigest"],
379
+ },
380
+ {
381
+ id: "fix-unit-has-no-product-parent",
382
+ description: "A fix unit has no Product snapshot, so its Plan snapshot binds no parent.",
383
+ projectable: true,
384
+ kind: "null-when",
385
+ // The other half of the narrowed rule, stated as a prohibition rather than a
386
+ // permission: a fix receipt that named a parent would claim a Product review
387
+ // that no clean-context reviewer ever ran (D6).
388
+ when: { field: "unitKind", equals: "fix" },
389
+ fields: ["parentSpecSnapshotDigest"],
390
+ },
391
+ {
392
+ id: "unique-artifact-paths",
393
+ description: "One artifact row per repository path.",
394
+ projectable: false,
395
+ kind: "unique",
396
+ collection: "artifacts",
397
+ fields: ["path"],
398
+ code: "duplicate-id",
399
+ },
400
+ ],
401
+ };
402
+ // ---------------------------------------------------------------------------
403
+ // PreExecutionReviewReceipt v1
404
+ // ---------------------------------------------------------------------------
405
+ const FINDING_SPEC = {
406
+ description: "One structured review finding.",
407
+ fields: [
408
+ opaqueId("id", "Stable finding identity, unique inside the receipt."),
409
+ { key: "severity", type: "enum", enum: PRE_EXECUTION_FINDING_SEVERITIES, description: "Severity ladder shared with candidate review; `info` is the only immaterial row." },
410
+ { key: "class", type: "enum", enum: PRE_EXECUTION_FINDING_CLASSES, description: "Root-cause owner the finding routes to." },
411
+ {
412
+ key: "claim",
413
+ type: "string",
414
+ minLength: 1,
415
+ maxLength: PRE_EXECUTION_LIMITS.claimChars,
416
+ nulFree: true,
417
+ description: `What the reviewer asserts is missing or wrong; at most ${PRE_EXECUTION_LIMITS.claimChars} characters.`,
418
+ },
419
+ {
420
+ key: "evidenceRefs",
421
+ type: "stringArray",
422
+ minItems: 1,
423
+ maxItems: PRE_EXECUTION_LIMITS.evidencePerFinding,
424
+ itemNulFree: true,
425
+ itemMaxLength: PRE_EXECUTION_LIMITS.evidenceChars,
426
+ description: `Non-empty file:line references; at most ${PRE_EXECUTION_LIMITS.evidencePerFinding}. A claim without evidence is not a finding.`,
427
+ },
428
+ { key: "verification", type: "enum", enum: PRE_EXECUTION_FINDING_VERIFICATION, description: "Whether the claim was checked against the artifact." },
429
+ { key: "resolution", type: "enum", enum: PRE_EXECUTION_FINDING_RESOLUTIONS, description: "Resolution state inside the owning author's repair." },
430
+ {
431
+ key: "resolutionEvidence",
432
+ type: "nullable",
433
+ nullableOf: "string",
434
+ minLength: 1,
435
+ maxLength: PRE_EXECUTION_LIMITS.resolutionEvidenceChars,
436
+ nulFree: true,
437
+ description: "Counter-evidence or repair pointer. Required to dismiss or resolve a finding.",
438
+ },
439
+ ],
440
+ rules: [
441
+ {
442
+ id: "dismissal-needs-counter-evidence",
443
+ description: "A dismissed finding records the evidence that falsifies it.",
444
+ projectable: true,
445
+ kind: "non-null-when",
446
+ when: { field: "resolution", in: ["dismissed", "resolved"] },
447
+ fields: ["resolutionEvidence"],
448
+ code: "invalid-evidence",
449
+ },
450
+ ],
451
+ };
452
+ const PARENT_RECEIPT_SPEC = {
453
+ description: "One optional parent review receipt in a critique/synthesis topology.",
454
+ fields: [
455
+ { key: "role", type: "enum", enum: PRE_EXECUTION_PARENT_ROLES, description: "Parent role. No quorum or majority meaning is attached to this list." },
456
+ {
457
+ key: "receiptDigest",
458
+ type: "string",
459
+ minLength: 64,
460
+ maxLength: 64,
461
+ nulFree: true,
462
+ pattern: LOWERCASE_64HEX_PATTERN,
463
+ violationCode: "invalid-value",
464
+ description: "Lowercase SHA-256 digest of the parent receipt.",
465
+ },
466
+ ],
467
+ rules: [],
468
+ };
469
+ const RECEIPT_ROOT_SPEC = {
470
+ description: "An independent, content-bound verdict on one pre-execution snapshot.",
471
+ fields: [
472
+ {
473
+ key: "contract",
474
+ type: "const",
475
+ value: PRE_EXECUTION_RECEIPT_CONTRACT_ID,
476
+ violationCode: "invalid-value",
477
+ description: "Contract identifier. Candidate and verification receipts carry other ids and are refused here.",
478
+ },
479
+ opaqueId("id", "Stable receipt identity."),
480
+ { key: "stage", type: "enum", enum: PRE_EXECUTION_STAGES, description: "Stage the verdict belongs to." },
481
+ {
482
+ key: "snapshotDigest",
483
+ type: "string",
484
+ minLength: 64,
485
+ maxLength: 64,
486
+ nulFree: true,
487
+ pattern: LOWERCASE_64HEX_PATTERN,
488
+ violationCode: "invalid-value",
489
+ description: "Digest of the exact snapshot that was reviewed.",
490
+ },
491
+ { key: "verdict", type: "enum", enum: PRE_EXECUTION_VERDICTS, description: "One of the five public verdicts." },
492
+ {
493
+ key: "findings",
494
+ type: "array",
495
+ specName: "PreExecutionReviewFindingV1",
496
+ maxItems: PRE_EXECUTION_LIMITS.findings,
497
+ description: `Structured findings; at most ${PRE_EXECUTION_LIMITS.findings}. A PASS never carries an open or unverified material row.`,
498
+ },
499
+ opaqueId("reviewer", "Reviewer identity as the runtime knows it."),
500
+ opaqueId("sessionId", "Session identity of the context-clean review."),
501
+ { key: "reviewerRole", type: "enum", enum: PRE_EXECUTION_REVIEW_ROLES, description: "Role this receipt played in the review topology." },
502
+ opaqueId("authorId", "Identity of the artifact author the exclusion rule compares against."),
503
+ { key: "authorExclusion", type: "enum", enum: PRE_EXECUTION_AUTHOR_EXCLUSIONS, description: "Whether the runtime can actually observe author identity." },
504
+ { key: "contextClean", type: "boolean", description: "The review ran in a context that did not author the artifact." },
505
+ { key: "modelDiversity", type: "enum", enum: PRE_EXECUTION_MODEL_DIVERSITY, description: "Truthful diversity label; never inferred from plurality." },
506
+ {
507
+ key: "policyVersion",
508
+ type: "string",
509
+ minLength: 1,
510
+ maxLength: PRE_EXECUTION_LIMITS.policyChars,
511
+ nulFree: true,
512
+ description: `Review policy version the verdict was produced under; at most ${PRE_EXECUTION_LIMITS.policyChars} characters.`,
513
+ },
514
+ {
515
+ key: "startedAt",
516
+ type: "string",
517
+ minLength: 20,
518
+ maxLength: 40,
519
+ nulFree: true,
520
+ pattern: ISO_8601_PATTERN,
521
+ violationCode: "invalid-value",
522
+ description: "UTC start of the review.",
523
+ },
524
+ {
525
+ key: "finishedAt",
526
+ type: "string",
527
+ minLength: 20,
528
+ maxLength: 40,
529
+ nulFree: true,
530
+ pattern: ISO_8601_PATTERN,
531
+ violationCode: "invalid-value",
532
+ description: "UTC end of the review.",
533
+ },
534
+ {
535
+ key: "parentReceipts",
536
+ type: "array",
537
+ specName: "PreExecutionParentReceiptV1",
538
+ maxItems: PRE_EXECUTION_LIMITS.parentReceipts,
539
+ description: `Optional bounded critique/synthesis/arbitration parents; at most ${PRE_EXECUTION_LIMITS.parentReceipts}. Presence is topology, not a vote.`,
540
+ },
541
+ {
542
+ key: "diagnostics",
543
+ type: "stringArray",
544
+ maxItems: PRE_EXECUTION_LIMITS.receiptDiagnostics,
545
+ itemNulFree: true,
546
+ itemMaxLength: PRE_EXECUTION_LIMITS.diagnosticChars,
547
+ description: `Reviewer-side notes; at most ${PRE_EXECUTION_LIMITS.receiptDiagnostics} rows of ${PRE_EXECUTION_LIMITS.diagnosticChars} characters.`,
548
+ },
549
+ ],
550
+ rules: [
551
+ // The verdict/stage compatibility matrix, declared once from the two closed
552
+ // vocabularies and rendered into the projection: `plan-review-pass` is not a
553
+ // SPEC verdict wearing different clothes, it is a claim about a different
554
+ // artifact set, so the pair is refused as a stage error (`invalid-stage`).
555
+ // One logical rule over two stage branches — both projection blocks carry
556
+ // the same `[verdict-stage-matrix]` marker, each `if` discriminates its stage.
557
+ ...PRE_EXECUTION_STAGES.map((stage) => ({
558
+ id: "verdict-stage-matrix",
559
+ description: `A ${stage.toUpperCase()}-stage receipt carries only ${stage} verdicts.`,
560
+ projectable: true,
561
+ kind: "enum-when",
562
+ when: { field: "stage", equals: stage },
563
+ fields: ["verdict"],
564
+ values: VERDICTS_BY_STAGE[stage],
565
+ code: "invalid-stage",
566
+ })),
567
+ // Parent topology, declared once and enforced by the shared walk (the
568
+ // former inline check is this rule pair, not a second authority):
569
+ // - shaped — parents only exist inside a critique/synthesis/arbitration
570
+ // topology, so their presence narrows the reviewer role;
571
+ // - restrained — a plain reviewer receipt carries no parents at all.
572
+ // Both report `invalid-topology` at the role/collection pointer.
573
+ {
574
+ id: "parent-topology-shaped",
575
+ description: "Parent receipts require a critic, synthesizer, or arbiter reviewer role.",
576
+ projectable: true,
577
+ // Binding-time: a well-formed receipt may record the topology; only the
578
+ // authority that blesses a verdict refuses to act on it.
579
+ enforcement: "binding",
580
+ kind: "enum-when",
581
+ when: { field: "parentReceipts", minItems: 1 },
582
+ fields: ["reviewerRole"],
583
+ values: PARENT_CAPABLE_REVIEWER_ROLES,
584
+ code: "invalid-topology",
585
+ },
586
+ {
587
+ id: "parent-topology-restrained",
588
+ description: "A plain reviewer receipt carries no parent receipts.",
589
+ projectable: true,
590
+ enforcement: "binding",
591
+ kind: "maximum-when",
592
+ when: { field: "reviewerRole", equals: "reviewer" },
593
+ fields: ["parentReceipts"],
594
+ maximum: 0,
595
+ code: "invalid-topology",
596
+ },
597
+ // The Draft-07 floor for parent identity: no two rows are the same value.
598
+ // The digest-level authority below (`unique-parent-receipts`) stays
599
+ // runtime-only and strictly stronger.
600
+ {
601
+ id: "parent-identities-unique",
602
+ description: "Parent receipts are unique rows; digests are unique too (runtime rule).",
603
+ projectable: true,
604
+ kind: "unique-items",
605
+ collection: "parentReceipts",
606
+ code: "duplicate-id",
607
+ },
608
+ {
609
+ id: "timestamp-order",
610
+ description: "A review cannot finish before it started.",
611
+ projectable: false,
612
+ kind: "timestamp-order",
613
+ fields: ["startedAt", "finishedAt"],
614
+ },
615
+ {
616
+ id: "calendar-roundtrip",
617
+ description: "Timestamps name a real UTC instant.",
618
+ projectable: false,
619
+ kind: "calendar-roundtrip",
620
+ fields: ["startedAt", "finishedAt"],
621
+ },
622
+ {
623
+ id: "unique-finding-ids",
624
+ description: "One row per finding identity.",
625
+ projectable: false,
626
+ kind: "unique",
627
+ collection: "findings",
628
+ fields: ["id"],
629
+ code: "duplicate-id",
630
+ },
631
+ {
632
+ id: "unique-parent-receipts",
633
+ description: "One row per parent receipt digest.",
634
+ projectable: false,
635
+ kind: "unique",
636
+ collection: "parentReceipts",
637
+ fields: ["receiptDigest"],
638
+ code: "duplicate-id",
639
+ },
640
+ ],
641
+ };
642
+ /**
643
+ * The two pre-execution contracts in the shape the shared structural engine and
644
+ * the projection generator consume. `authority` names the runtime entry that owns
645
+ * validity — and for the receipt, PASS — which the projections repeat in `$comment`.
646
+ */
647
+ export const PRE_EXECUTION_CONTRACT = Object.freeze({
648
+ snapshot: Object.freeze({
649
+ contractId: PRE_EXECUTION_SNAPSHOT_CONTRACT_ID,
650
+ fileName: "pre-execution-artifact-snapshot.schema.json",
651
+ title: "PreExecutionArtifactSnapshot v1",
652
+ description: "The exact artifact set, authoritative contexts, source revision and authoring revision one independent pre-execution reviewer read.",
653
+ authority: "validatePreExecutionArtifactSnapshotV1",
654
+ rootLabel: "snapshot",
655
+ root: SNAPSHOT_ROOT_SPEC,
656
+ objects: Object.freeze({
657
+ PreExecutionArtifactRowV1: ARTIFACT_ROW_SPEC,
658
+ PreExecutionContextBindingV1: CONTEXT_ROW_SPEC,
659
+ }),
660
+ runtimeRules: PRE_EXECUTION_RUNTIME_RULES.snapshot,
661
+ }),
662
+ receipt: Object.freeze({
663
+ contractId: PRE_EXECUTION_RECEIPT_CONTRACT_ID,
664
+ fileName: "pre-execution-review-receipt.schema.json",
665
+ title: "PreExecutionReviewReceipt v1",
666
+ description: "A content-bound verdict on one pre-execution snapshot, with structured findings, reviewer identities, and an optional critique/synthesis topology.",
667
+ authority: "validatePreExecutionReceiptAgainstSnapshot",
668
+ rootLabel: "receipt",
669
+ root: RECEIPT_ROOT_SPEC,
670
+ objects: Object.freeze({
671
+ PreExecutionReviewFindingV1: FINDING_SPEC,
672
+ PreExecutionParentReceiptV1: PARENT_RECEIPT_SPEC,
673
+ }),
674
+ runtimeRules: PRE_EXECUTION_RUNTIME_RULES.receipt,
675
+ }),
676
+ });