create-agent-rig 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +105 -1
  2. package/README.md +92 -3
  3. package/package.json +4 -3
  4. package/packages/cli/dist/commands/memory.js +123 -0
  5. package/packages/cli/dist/commands/setup.js +45 -0
  6. package/packages/cli/dist/index.js +107 -3
  7. package/packages/cli/dist/lib/subsystems.js +269 -0
  8. package/packages/cli/dist/lib/version.js +15 -0
  9. package/packages/cli/dist/policy/benchmark/corpus.js +165 -0
  10. package/packages/cli/dist/policy/core/coverage.js +253 -0
  11. package/packages/cli/dist/policy/core/decision-record.js +130 -44
  12. package/packages/cli/dist/policy/core/declaration.js +58 -17
  13. package/packages/cli/dist/policy/core/evidence-matrix.js +94 -0
  14. package/packages/cli/dist/policy/core/probe.js +442 -0
  15. package/packages/cli/dist/policy/core/validation.js +194 -1
  16. package/packages/cli/dist/policy/core/vocabulary.js +70 -3
  17. package/packages/cli/dist/policy/harness/claude.js +9 -1
  18. package/packages/cli/dist/policy/harness/codex.js +48 -1
  19. package/packages/cli/dist/policy/harness/shared-hooks.js +18 -0
  20. package/packages/cli/dist/policy/index.js +9 -2
  21. package/templates/agent-os/stack/aws-cdk/.claude/agents/cdk-diff-reviewer.md +2 -0
  22. package/templates/agent-os/stack/aws-cdk/.codex/agents/cdk-diff-reviewer.toml +2 -0
  23. package/templates/agent-os/subagent-routing.json +32 -0
  24. package/templates/agent-os/universal/.agents/skills/loop/SKILL.md +41 -4
  25. package/templates/agent-os/universal/.claude/agents/code-reviewer.md +2 -0
  26. package/templates/agent-os/universal/.claude/agents/prose-reviewer.md +2 -0
  27. package/templates/agent-os/universal/.claude/agents/security-scanner.md +2 -0
  28. package/templates/agent-os/universal/.claude/agents/test-writer.md +2 -0
  29. package/templates/agent-os/universal/.claude/hooks/guard-subagent-model.mjs +234 -0
  30. package/templates/agent-os/universal/.claude/hooks/lib/edit-input.mjs +75 -32
  31. package/templates/agent-os/universal/.claude/hooks/warn-subagent-routing.mjs +120 -0
  32. package/templates/agent-os/universal/.claude/rules/workflow.md +5 -0
  33. package/templates/agent-os/universal/.claude/scripts/preflight.mjs +27 -3
  34. package/templates/agent-os/universal/.claude/scripts/queue/gate-rounds.mjs +70 -2
  35. package/templates/agent-os/universal/.claude/scripts/queue/index.mjs +12 -4
  36. package/templates/agent-os/universal/.claude/scripts/unattended-flag.mjs +64 -1
  37. package/templates/agent-os/universal/.claude/settings.json +16 -0
  38. package/templates/agent-os/universal/.claude/skills/loop/SKILL.md +41 -4
  39. package/templates/agent-os/universal/.codex/agents/code-reviewer.toml +2 -0
  40. package/templates/agent-os/universal/.codex/agents/prose-reviewer.toml +2 -0
  41. package/templates/agent-os/universal/.codex/agents/security-scanner.toml +2 -0
  42. package/templates/agent-os/universal/.codex/agents/test-writer.toml +2 -0
  43. package/templates/agent-os/universal/.codex/config.toml +3 -0
  44. package/templates/agent-os/universal/docs/decisions/codex-adapter.md +31 -5
  45. package/templates/agent-os/universal/docs/decisions/subagent-routing.md +142 -0
  46. package/templates/agent-os/universal/layers.json +4 -0
  47. package/templates/hash-history.json +8 -4
  48. package/templates/release-ledger.json +2 -1
  49. package/templates/skeleton/node-service/services/api/test/artifact.test.ts +3 -4
  50. package/templates/skeleton/node-service/services/api/test/package-manager.test.ts +40 -0
  51. package/templates/skeleton/node-service/services/api/test/package-manager.ts +51 -0
  52. package/templates/skeleton/node-service/services/api/test/static-dir.test.ts +9 -8
@@ -0,0 +1,253 @@
1
+ /**
2
+ * The capability & degradation contract (RP-36): what each declared policy is
3
+ * worth on one harness surface, how that answer is established, and how it is
4
+ * maintained afterwards.
5
+ *
6
+ * ⚠ A LIBRARY, not a running contract. Nothing in this rig calls any of it
7
+ * yet: `doctor` rendering the coverage report is its own task, and so is the
8
+ * caller that would report observed traffic. The present tense below describes
9
+ * what the functions do when called, not something happening today. The
10
+ * rationale and the full list of what this deliberately does not do are in
11
+ * `docs/decisions/capability-coverage.md`.
12
+ *
13
+ * A status is established by `./probe.ts` on an occasion named from
14
+ * `./vocabulary.ts` (`PROBE_TRIGGERS`), and maintained from what real traffic
15
+ * was expected to show and did not. Three rules follow, and they are the whole
16
+ * design.
17
+ *
18
+ * 🔴 **A timer alone never degrades anything.** `observeExpectedSignal` takes
19
+ * an OBSERVATION, never an elapsed time, and this module exports no function
20
+ * that ages, expires or sweeps an entry — pinned by
21
+ * `packages/cli/test/policy-coverage.test.ts` › "exports nothing that ages,
22
+ * expires or sweeps an entry, so only an observation can move it", and
23
+ * measured end-to-end by › "reports no downgrade when a year passes and the
24
+ * same wiring is probed again, because elapsed time is not evidence" against
25
+ * its positive control. No traffic is not a failure: it is no evidence.
26
+ *
27
+ * 🔴 **Traffic lowers a status; it never raises one.** A missing signal is
28
+ * evidence the mechanism did not act. A present signal is evidence it acted
29
+ * once — not that a wiring defect the probe found has been repaired. So the
30
+ * only route back up is another probe: › "never raises a status on traffic: a
31
+ * degraded surface stays degraded until a probe says otherwise".
32
+ *
33
+ * 🔴 **Traffic that CONTRADICTS the record is its own answer.** A signal
34
+ * observed on a surface the map records as `UNSUPPORTED` is not a promotion
35
+ * and not a miss — the wiring and the traffic disagree, which is what
36
+ * `INTEGRATION-FAILED` names: › "a signal observed where the map says nothing
37
+ * is wired is a contradiction, not a pass". It is still not a pass;
38
+ * `qualifierFor` maps it to `UNVERIFIABLE`.
39
+ *
40
+ * **Why this module throws where its neighbours return a `Validation`.** The
41
+ * line is what the value IS, not how bad it is: outside data read off a disk
42
+ * becomes a RESULT — `snapshot: unknown` is never trusted and never throws, it
43
+ * becomes `INTEGRATION-FAILED` — while a value the CALLER chose (a word from a
44
+ * closed vocabulary, its own clock, its own claim about which build it saw) is
45
+ * a programming error and throws. `./evidence-matrix.ts` sits on the first
46
+ * side, this module's two entry points on the second.
47
+ *
48
+ * Two limits worth stating rather than leaving to be discovered. `DEGRADED`
49
+ * carries no verdict qualifier, so an operation on precisely the tool a
50
+ * degraded matcher lost does not require a qualifier from this library — the
51
+ * item scopes the `UNVERIFIABLE` requirement to the unenforceable states; the
52
+ * caller owns the operation verdict. And a `reason`
53
+ * supplied with a miss that does not cross the threshold is not retained;
54
+ * only the miss that degrades records one — › "discards the reason given with
55
+ * a miss that does not degrade, and records the one given with the miss that
56
+ * does".
57
+ */
58
+ import { probePolicy } from './probe.js';
59
+ import { EXACT_VERSION_EXPECTED, isExactVersion, ISO_8601 } from './validation.js';
60
+ import { PROBE_TRIGGERS, UNENFORCEABLE_STATES } from './vocabulary.js';
61
+ /**
62
+ * How many consecutive expected-but-absent signals degrade a supported policy.
63
+ *
64
+ * Three, not one: a single miss is as easily an operation that never reached
65
+ * the mechanism as a mechanism that failed to act, and a contract that
66
+ * degrades on it reports noise. It is a DEFAULT and not a constant — every
67
+ * caller may set its own, because what counts as enough evidence depends on
68
+ * how much traffic the surface sees.
69
+ */
70
+ export const DEFAULT_DEGRADATION_THRESHOLD = 3;
71
+ /**
72
+ * How much a state can enforce, as an order, for deciding what counts as a
73
+ * fall. `UNSUPPORTED` and `INTEGRATION-FAILED` rank together on purpose: both
74
+ * mean the policy cannot be relied on here, they differ in whether anything is
75
+ * wired at all, and calling a move between them a downgrade would report a
76
+ * change of diagnosis as a loss of capability.
77
+ *
78
+ * This is a DISPLAY ordering and nothing else reads it. What qualifies a
79
+ * verdict is `UNENFORCEABLE_STATES` in `./vocabulary.ts`, which
80
+ * `./decision-record.ts` reads too — keying qualification off a rank would
81
+ * mean a future re-rank silently changed which verdicts are UNVERIFIABLE.
82
+ */
83
+ const ENFORCEMENT_RANK = {
84
+ SUPPORTED: 0,
85
+ DEGRADED: 1,
86
+ UNSUPPORTED: 2,
87
+ 'INTEGRATION-FAILED': 2,
88
+ };
89
+ const requireTimestamp = (field, value) => {
90
+ if (!ISO_8601.test(value)) {
91
+ throw new Error(`${field} must be an ISO-8601 date-time with an explicit zone; got ${JSON.stringify(value)}`);
92
+ }
93
+ };
94
+ /**
95
+ * The probe's answer for every policy, as one map.
96
+ *
97
+ * `trigger` is required, and that is the point: `PROBE_TRIGGERS` names only
98
+ * changes to the surface, so a caller must say which change occasioned this
99
+ * read and cannot name an interval. `at` is the caller's clock — nothing here
100
+ * reads one — and it is validated, because a status whose time cannot be
101
+ * ordered against a change is not auditable. The surface harness and path must
102
+ * also name the adapter doing the read; otherwise valid wiring from one
103
+ * harness could be labeled as support on another.
104
+ */
105
+ export function coverageFromProbe(args) {
106
+ const { surface, policies, adapter, snapshot, at, trigger, evidencePointer } = args;
107
+ if (!PROBE_TRIGGERS.includes(trigger)) {
108
+ throw new Error(`a probe is occasioned by one of ${PROBE_TRIGGERS.join(', ')}; got ${JSON.stringify(trigger)}`);
109
+ }
110
+ requireTimestamp('at', at);
111
+ if (surface.harness !== adapter.harness || surface.surface !== adapter.surfaceFile) {
112
+ throw new Error(`surface identity must match the adapter being probed; expected ${JSON.stringify(adapter.harness)} at ${JSON.stringify(adapter.surfaceFile)}, got ${JSON.stringify(surface.harness)} at ${JSON.stringify(surface.surface)}`);
113
+ }
114
+ // The identity is the caller's claim about WHICH build was observed, and
115
+ // three documents called it exact while nothing read it. A map is the shape
116
+ // that carries it, so this is where it is refused.
117
+ if (!isExactVersion(surface.harnessVersion)) {
118
+ throw new Error(`surface.harnessVersion ${EXACT_VERSION_EXPECTED}; got ${JSON.stringify(surface.harnessVersion)}`);
119
+ }
120
+ const entries = policies.map((policy) => {
121
+ const result = probePolicy(policy, adapter, snapshot);
122
+ const entry = {
123
+ policyId: policy.policyId,
124
+ policyVersion: policy.policyVersion,
125
+ mechanism: policy.mechanism,
126
+ status: result.state,
127
+ verifiedAt: at,
128
+ verifiedBy: 'probe',
129
+ triggeredBy: trigger,
130
+ consecutiveMisses: 0,
131
+ };
132
+ if (result.reason !== undefined)
133
+ entry.degradationReason = result.reason;
134
+ if (evidencePointer !== undefined)
135
+ entry.evidencePointer = evidencePointer;
136
+ return entry;
137
+ });
138
+ return { surface, entries };
139
+ }
140
+ /**
141
+ * Fold one observation of real traffic into an entry.
142
+ *
143
+ * `seen` is the whole input: the operation the policy judges happened, and the
144
+ * observable signal the mechanism should have produced either appeared or did
145
+ * not. There is no parameter for "and this much time has passed", because time
146
+ * is not evidence about a mechanism.
147
+ */
148
+ export function observeExpectedSignal(entry, observation) {
149
+ const { seen, at, threshold = DEFAULT_DEGRADATION_THRESHOLD, reason } = observation;
150
+ if (!Number.isInteger(threshold) || threshold < 1) {
151
+ throw new Error(`the degradation threshold must be a whole number of observations, at least 1; got ${String(threshold)}`);
152
+ }
153
+ requireTimestamp('at', at);
154
+ if (seen) {
155
+ const next = {
156
+ ...entry,
157
+ consecutiveMisses: 0,
158
+ verifiedAt: at,
159
+ verifiedBy: 'traffic',
160
+ };
161
+ // A signal seen where the record says nothing is wired is not a promotion
162
+ // and not a miss: the wiring and the traffic disagree, and that is the
163
+ // state the item reserves for it. The status is not raised — see the
164
+ // second rule in this file's header — so the only status this branch
165
+ // produces is the one naming the contradiction.
166
+ if (entry.status === 'UNSUPPORTED') {
167
+ next.status = 'INTEGRATION-FAILED';
168
+ next.degradationReason = `the expected signal was observed although the recorded status was UNSUPPORTED, so the wiring and the traffic disagree`;
169
+ }
170
+ return next;
171
+ }
172
+ const consecutiveMisses = entry.consecutiveMisses + 1;
173
+ const degrades = entry.status === 'SUPPORTED' && consecutiveMisses >= threshold;
174
+ const next = {
175
+ ...entry,
176
+ consecutiveMisses,
177
+ verifiedAt: at,
178
+ verifiedBy: 'traffic',
179
+ };
180
+ if (!degrades)
181
+ return next;
182
+ next.status = 'DEGRADED';
183
+ next.degradationReason =
184
+ reason ??
185
+ `the expected observable signal was absent on ${String(consecutiveMisses)} consecutive operations`;
186
+ return next;
187
+ }
188
+ /** The entry for one policy, or `null` — never an invented status. */
189
+ export function statusOf(map, policyId) {
190
+ return map.entries.find((entry) => entry.policyId === policyId) ?? null;
191
+ }
192
+ /**
193
+ * The qualifier a verdict must carry when it was produced under this state.
194
+ *
195
+ * The unenforceable states mean the question could not be put to a working
196
+ * mechanism, so an `allow` under either is UNVERIFIABLE rather than a pass —
197
+ * `./decision-record.ts` refuses the unqualified record, and
198
+ * `packages/cli/test/policy-coverage.test.ts` › "refuses the silent pass an
199
+ * unwired surface would otherwise produce, and accepts it once qualifierFor
200
+ * speaks" holds the two modules to it together. How far that refusal reaches is
201
+ * stated once, at `UNENFORCEABLE_STATES` in `./vocabulary.ts` — read it there.
202
+ * This sentence used to paraphrase it as well, and the paraphrase is what went
203
+ * stale: it still said the refusal was "a property of records built the
204
+ * ordinary way" after RP-153 had made it hold against a hand-built prototype
205
+ * too. A second copy of a fact the same sentence claims is spelled once
206
+ * (`rules/invariants.md`, "One mechanism, one implementation").
207
+ */
208
+ export function qualifierFor(state) {
209
+ return UNENFORCEABLE_STATES.includes(state)
210
+ ? 'UNVERIFIABLE'
211
+ : undefined;
212
+ }
213
+ /**
214
+ * Every policy whose status fell between two maps of the same surface.
215
+ *
216
+ * Only the same policy id and declaration version present in both are compared:
217
+ * an entry that appeared, vanished or changed semantics is a change of what is
218
+ * being measured, not a capability that fell.
219
+ * Two maps of DIFFERENT surfaces are refused rather than diffed — the result
220
+ * is attributed to one surface identity, and attributing one surface's fall to
221
+ * another is a report an operator would act on in the wrong place.
222
+ */
223
+ export function downgradesBetween(before, after) {
224
+ // Deliberately NOT harnessVersion. A probe before and after an upgrade
225
+ // carries two versions of one surface, and that pair is exactly what the
226
+ // `upgrade` trigger exists to compare — refusing it disabled the comparison
227
+ // for the case it was added for. Harness, surface and OS are what make it
228
+ // the same place.
229
+ const sameSurface = before.surface.harness === after.surface.harness &&
230
+ before.surface.surface === after.surface.surface &&
231
+ before.surface.os === after.surface.os;
232
+ if (!sameSurface) {
233
+ throw new Error('two coverage maps of different surfaces cannot be compared: a downgrade is attributed to one surface, and these name two');
234
+ }
235
+ const downgrades = [];
236
+ for (const next of after.entries) {
237
+ const previous = before.entries.find((entry) => entry.policyId === next.policyId && entry.policyVersion === next.policyVersion) ?? null;
238
+ if (previous === null)
239
+ continue;
240
+ if (ENFORCEMENT_RANK[next.status] <= ENFORCEMENT_RANK[previous.status])
241
+ continue;
242
+ downgrades.push({
243
+ surface: after.surface,
244
+ policyId: next.policyId,
245
+ from: previous.status,
246
+ to: next.status,
247
+ mechanism: next.mechanism,
248
+ reason: next.degradationReason ?? 'the entry recorded no reason',
249
+ at: next.verifiedAt,
250
+ });
251
+ }
252
+ return downgrades;
253
+ }
@@ -16,12 +16,86 @@
16
16
  * - every evidence kind the policy requires must be present;
17
17
  * - a policy that redacts must not be recorded with unredacted diagnostics;
18
18
  * - the timestamp is supplied by the caller and must be an ISO-8601 date-time
19
- * with seconds and an explicit zone (`ISO_8601` below; a bare date is
19
+ * with seconds and an explicit zone (`ISO_8601` in `./validation.ts`, which
20
+ * every shape recording an observation time reads; a bare date is
20
21
  * refused) — no clock here.
21
22
  *
22
23
  * Each rule is one test in `packages/cli/test/policy-declaration.test.ts`
23
24
  * under "validating a decision record".
24
25
  *
26
+ * 🔴 EVERY field of the input is read through `carriesField`/`ownField`
27
+ * (`./validation.ts`) — own and enumerable, the set `Object.keys` walks — and
28
+ * never off the record directly. Presence and value travel through the same
29
+ * predicate: `'qualifier' in value` was the load-bearing half of this defect,
30
+ * because `unknownKeys` judges the same record by `Object.keys` while the reads
31
+ * walked the prototype chain, so the closed-shape check and the field reads
32
+ * disagreed about what the record contained — with the reads being the wider of
33
+ * the two, which is the direction that passes. A decision record is the
34
+ * artifact a later reader audits, and the shape that made this worth fixing
35
+ * before it had a caller is a record whose verdict qualifier is only inherited:
36
+ * it validates as qualified, and what it then writes out is `{"outcome":
37
+ * "allow","reason":…}` — the reason survives, the QUALIFIER is what is lost,
38
+ * which is exactly the silent pass an `UNSUPPORTED` capability state exists to
39
+ * prevent. Held over every reading site in this module, in both shapes —
40
+ * inherited, and own but not enumerable — in
41
+ * `packages/cli/test/policy-declaration.test.ts` › "refuses an UNSUPPORTED
42
+ * record whose verdict qualifier is %s, because what it writes out is a silent
43
+ * pass", with the other direction held by › "still accepts a record whose every
44
+ * field is defined through Object.defineProperty as own and enumerable". The
45
+ * `%s` is the name as the `it.each` case DECLARES it; quoting one expanded case
46
+ * gives a pointer no grep lands on.
47
+ *
48
+ * 🔴 And an unnarrowed outside value reaches a MESSAGE only through `quote`
49
+ * (`./validation.ts`), never through bare `String` or `JSON.stringify`.
50
+ * Two qualifications, because an earlier version of this sentence claimed more
51
+ * than the file delivers. First, `operation` and `capabilityState` are still
52
+ * interpolated bare into messages, safely: `member` has narrowed each to its
53
+ * closed vocabulary before the message is built. Second — and this is the part
54
+ * the sentence used to hide — a `Problem`'s **`field`** is not a message and is
55
+ * not escaped: `unknownKeys` (`./validation.ts`) puts the outside key name
56
+ * there verbatim, and `./declaration.ts` › `definePolicy` renders a report as
57
+ * `${field}: ${message}`, so an unknown key whose NAME carries a newline still
58
+ * forges a line. Escaping a `field` would break it as the machine-readable
59
+ * pointer it is, so the fix belongs in the rendering; it is RP-160.
60
+ * Both unsafe spellings inside messages were here: a qualifier
61
+ * carrying a newline forged two `field:
62
+ * message` lines of its own in the rendered problem list — the exact shape
63
+ * `./declaration.ts` › `definePolicy` throws — while the neighbouring line
64
+ * escaped the same value; and a circular value crashed the validator with a
65
+ * `TypeError` where `quote` degrades. Held by ›
66
+ * "escapes a verdict qualifier carrying a newline, so it cannot forge a line of
67
+ * the refusal report" and › "refuses a record whose %s is a circular value,
68
+ * rather than throwing while it renders the refusal".
69
+ *
70
+ * ⚠ What this does NOT do, and the limits are stated rather than implied:
71
+ *
72
+ * - the `ok: true` value is the input object itself, not a snapshot of the
73
+ * fields that were certified. Those are the same thing for a value whose
74
+ * fields are plain data, which is every record `JSON.parse` can produce; they
75
+ * are not the same for one carrying a live accessor, which validates on one
76
+ * read and serialises from another. RP-157 owns that, for this module and
77
+ * `./declaration.ts` together;
78
+ * - nothing here caps the cost, and the amplifier is the PROBLEM LIST rather
79
+ * than the input: one `Problem` is pushed per bad entry of `observedFacts` or
80
+ * `evidence`, so a parsed record carrying a million bad entries produces a
81
+ * million objects. That is reachable from ordinary `JSON.parse` input. The
82
+ * first version of this bullet was wrong in every clause — it said
83
+ * `Array.prototype.forEach` VISITS a sparse array's holes, that
84
+ * `new Array(1e7)` cost seconds and gigabytes "before the refusal", and that
85
+ * the cost was unreachable from a parsed record. `forEach` SKIPS holes, a
86
+ * sparse `observedFacts` is not refused at all, and the dense case is the
87
+ * reachable one. Corrected rather than softened, because a stale limit sells
88
+ * cover that is not there (`rules/invariants.md`, "State the limits — and
89
+ * test them");
90
+ * - an array HOLE is consequently read by nothing, while `JSON.stringify`
91
+ * writes it out as `null`: `observedFacts: [{…}, , ,]` serialises as
92
+ * `[{…},null,null]` and validates. That is this module's own rule — read what
93
+ * the serialisation carries — failing one level BELOW the field, which is
94
+ * where this change did not look. RP-161;
95
+ * - `quote` itself re-throws for a value whose `JSON.stringify` and `String`
96
+ * both throw. The direction is a crash, never an `ok: true`, so nothing
97
+ * malformed is certified through it — RP-160.
98
+ *
25
99
  * ⚠ `diagnostics.redacted` is the emitter's claim, and this validator enforces
26
100
  * the claim's presence, not the property: a record marked redacted whose
27
101
  * `diagnostics.text`, `observedFacts[].value` or `evidence[].value` still
@@ -31,8 +105,8 @@
31
105
  * before it persists a record.
32
106
  */
33
107
  import { compatibilityOf, findPolicy } from './registry.js';
34
- import { CAPABILITY_STATES, DECISION_OUTCOMES, EVIDENCE_KINDS, OPERATIONS, VERDICT_QUALIFIERS, } from './vocabulary.js';
35
- import { isRecord, member, nonEmptyString, unknownKeys } from './validation.js';
108
+ import { CAPABILITY_STATES, UNENFORCEABLE_STATES, DECISION_OUTCOMES, EVIDENCE_KINDS, OPERATIONS, VERDICT_QUALIFIERS, } from './vocabulary.js';
109
+ import { ISO_8601, carriesField, isRecord, member, nonEmptyString, ownField, quote, unknownKeys, } from './validation.js';
36
110
  export const DECISION_RECORD_SCHEMA_VERSION = 1;
37
111
  const KEYS = [
38
112
  'schemaVersion',
@@ -48,9 +122,12 @@ const KEYS = [
48
122
  'diagnostics',
49
123
  'recordedAt',
50
124
  ];
51
- const NEVER_SILENT_PASS = ['UNSUPPORTED', 'INTEGRATION-FAILED'];
52
- /** Date, `T`, time to the second (fractions allowed), and an explicit zone. */
53
- const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
125
+ /**
126
+ * Read from `./vocabulary.ts` rather than restated here: `./coverage.ts` ›
127
+ * `qualifierFor` answers from the same list, and a second copy is how the two
128
+ * come to disagree about which verdicts may pass unqualified.
129
+ */
130
+ const NEVER_SILENT_PASS = UNENFORCEABLE_STATES;
54
131
  const namedPairs = (problems, field, value, keys) => {
55
132
  if (!Array.isArray(value)) {
56
133
  problems.push({ field, message: 'must be a list' });
@@ -63,7 +140,7 @@ const namedPairs = (problems, field, value, keys) => {
63
140
  }
64
141
  unknownKeys(problems, entry, keys, `${field}[${index}]`);
65
142
  for (const key of keys)
66
- nonEmptyString(problems, `${field}[${index}].${key}`, entry[key]);
143
+ nonEmptyString(problems, `${field}[${index}].${key}`, ownField(entry, key));
67
144
  });
68
145
  };
69
146
  const checkVerdict = (problems, value, capabilityState) => {
@@ -72,22 +149,23 @@ const checkVerdict = (problems, value, capabilityState) => {
72
149
  return;
73
150
  }
74
151
  unknownKeys(problems, value, ['outcome', 'qualifier', 'reason'], 'verdict');
75
- member(problems, 'verdict.outcome', value.outcome, DECISION_OUTCOMES);
76
- const qualified = 'qualifier' in value;
77
- if (qualified) {
78
- member(problems, 'verdict.qualifier', value.qualifier, VERDICT_QUALIFIERS);
79
- if (typeof value.reason !== 'string' || value.reason.trim() === '') {
152
+ member(problems, 'verdict.outcome', ownField(value, 'outcome'), DECISION_OUTCOMES);
153
+ const qualifier = ownField(value, 'qualifier');
154
+ const reason = ownField(value, 'reason');
155
+ if (carriesField(value, 'qualifier')) {
156
+ member(problems, 'verdict.qualifier', qualifier, VERDICT_QUALIFIERS);
157
+ if (typeof reason !== 'string' || reason.trim() === '') {
80
158
  problems.push({
81
159
  field: 'verdict.reason',
82
- message: `a ${String(value.qualifier)} verdict must say why`,
160
+ message: `a ${quote(qualifier)} verdict must say why`,
83
161
  });
84
162
  }
85
163
  }
86
- else if ('reason' in value && typeof value.reason !== 'string') {
164
+ else if (carriesField(value, 'reason') && typeof reason !== 'string') {
87
165
  problems.push({ field: 'verdict.reason', message: 'must be a string when present' });
88
166
  }
89
167
  if (capabilityState !== null && NEVER_SILENT_PASS.includes(capabilityState)) {
90
- if (value.qualifier !== 'UNVERIFIABLE') {
168
+ if (qualifier !== 'UNVERIFIABLE') {
91
169
  problems.push({
92
170
  field: 'verdict.qualifier',
93
171
  message: `capabilityState ${capabilityState} never yields a silent pass: ` +
@@ -108,10 +186,11 @@ const checkEvidence = (problems, value, required) => {
108
186
  return;
109
187
  }
110
188
  unknownKeys(problems, entry, ['kind', 'value'], `evidence[${index}]`);
111
- if (member(problems, `evidence[${index}].kind`, entry.kind, EVIDENCE_KINDS)) {
112
- present.add(entry.kind);
189
+ const kind = ownField(entry, 'kind');
190
+ if (member(problems, `evidence[${index}].kind`, kind, EVIDENCE_KINDS)) {
191
+ present.add(kind);
113
192
  }
114
- nonEmptyString(problems, `evidence[${index}].value`, entry.value);
193
+ nonEmptyString(problems, `evidence[${index}].value`, ownField(entry, 'value'));
115
194
  });
116
195
  for (const kind of required) {
117
196
  if (!present.has(kind)) {
@@ -125,16 +204,17 @@ const checkDiagnostics = (problems, value, mustRedact) => {
125
204
  return;
126
205
  }
127
206
  unknownKeys(problems, value, ['redacted', 'text'], 'diagnostics');
128
- if (typeof value.redacted !== 'boolean') {
207
+ const redacted = ownField(value, 'redacted');
208
+ if (typeof redacted !== 'boolean') {
129
209
  problems.push({ field: 'diagnostics.redacted', message: 'must be a boolean' });
130
210
  }
131
- else if (mustRedact && !value.redacted) {
211
+ else if (mustRedact && !redacted) {
132
212
  problems.push({
133
213
  field: 'diagnostics.redacted',
134
214
  message: 'the policy redacts matched values, so its diagnostics must be recorded redacted',
135
215
  });
136
216
  }
137
- if (typeof value.text !== 'string') {
217
+ if (typeof ownField(value, 'text') !== 'string') {
138
218
  problems.push({ field: 'diagnostics.text', message: 'must be a string' });
139
219
  }
140
220
  };
@@ -145,54 +225,60 @@ export function validateDecisionRecord(input) {
145
225
  }
146
226
  const problems = [];
147
227
  unknownKeys(problems, input, KEYS);
148
- if (input.schemaVersion !== DECISION_RECORD_SCHEMA_VERSION) {
228
+ const schemaVersion = ownField(input, 'schemaVersion');
229
+ if (schemaVersion !== DECISION_RECORD_SCHEMA_VERSION) {
149
230
  problems.push({
150
231
  field: 'schemaVersion',
151
- message: `must be ${DECISION_RECORD_SCHEMA_VERSION}, got ${String(input.schemaVersion)}`,
232
+ message: `must be ${DECISION_RECORD_SCHEMA_VERSION}, got ${quote(schemaVersion)}`,
152
233
  });
153
234
  }
154
- const policy = typeof input.policyId === 'string' ? findPolicy(input.policyId) : null;
235
+ const policyId = ownField(input, 'policyId');
236
+ const policyVersion = ownField(input, 'policyVersion');
237
+ const policy = typeof policyId === 'string' ? findPolicy(policyId) : null;
155
238
  if (policy === null) {
156
239
  problems.push({
157
240
  field: 'policyId',
158
- message: `${JSON.stringify(input.policyId)} is not a registered policy`,
241
+ message: `${quote(policyId)} is not a registered policy`,
159
242
  });
160
243
  }
161
- else if (typeof input.policyVersion === 'string') {
162
- const compatibility = compatibilityOf(policy.policyId, input.policyVersion);
244
+ else if (typeof policyVersion === 'string') {
245
+ const compatibility = compatibilityOf(policy.policyId, policyVersion);
163
246
  if (compatibility !== 'compatible') {
164
247
  problems.push({
165
248
  field: 'policyVersion',
166
- message: `${JSON.stringify(input.policyVersion)} is ${compatibility} with ${policy.policyId} ${policy.policyVersion}`,
249
+ message: `${quote(policyVersion)} is ${compatibility} with ${policy.policyId} ${policy.policyVersion}`,
167
250
  });
168
251
  }
169
252
  }
170
253
  else {
171
- nonEmptyString(problems, 'policyVersion', input.policyVersion);
254
+ nonEmptyString(problems, 'policyVersion', policyVersion);
172
255
  }
173
- nonEmptyString(problems, 'harness', input.harness);
174
- if (member(problems, 'operation', input.operation, OPERATIONS) && policy !== null) {
175
- if (!policy.operations.includes(input.operation)) {
256
+ nonEmptyString(problems, 'harness', ownField(input, 'harness'));
257
+ const operation = ownField(input, 'operation');
258
+ if (member(problems, 'operation', operation, OPERATIONS) && policy !== null) {
259
+ if (!policy.operations.includes(operation)) {
176
260
  problems.push({
177
261
  field: 'operation',
178
- message: `${policy.policyId} does not apply to ${input.operation}`,
262
+ message: `${policy.policyId} does not apply to ${operation}`,
179
263
  });
180
264
  }
181
265
  }
182
- const capabilityState = member(problems, 'capabilityState', input.capabilityState, CAPABILITY_STATES)
183
- ? input.capabilityState
266
+ const state = ownField(input, 'capabilityState');
267
+ const capabilityState = member(problems, 'capabilityState', state, CAPABILITY_STATES)
268
+ ? state
184
269
  : null;
185
- namedPairs(problems, 'observedFacts', input.observedFacts, ['name', 'value']);
186
- checkVerdict(problems, input.verdict, capabilityState);
187
- checkEvidence(problems, input.evidence, policy?.requiredEvidence ?? []);
188
- nonEmptyString(problems, 'artifactVersion', input.artifactVersion);
189
- checkDiagnostics(problems, input.diagnostics, policy !== null && policy.redaction !== 'none');
190
- if (typeof input.recordedAt !== 'string' ||
191
- !ISO_8601.test(input.recordedAt) ||
192
- Number.isNaN(Date.parse(input.recordedAt))) {
270
+ namedPairs(problems, 'observedFacts', ownField(input, 'observedFacts'), ['name', 'value']);
271
+ checkVerdict(problems, ownField(input, 'verdict'), capabilityState);
272
+ checkEvidence(problems, ownField(input, 'evidence'), policy?.requiredEvidence ?? []);
273
+ nonEmptyString(problems, 'artifactVersion', ownField(input, 'artifactVersion'));
274
+ checkDiagnostics(problems, ownField(input, 'diagnostics'), policy !== null && policy.redaction !== 'none');
275
+ const recordedAt = ownField(input, 'recordedAt');
276
+ if (typeof recordedAt !== 'string' ||
277
+ !ISO_8601.test(recordedAt) ||
278
+ Number.isNaN(Date.parse(recordedAt))) {
193
279
  problems.push({
194
280
  field: 'recordedAt',
195
- message: `must be an ISO-8601 date-time with seconds and an explicit zone, got ${JSON.stringify(input.recordedAt)}`,
281
+ message: `must be an ISO-8601 date-time with seconds and an explicit zone, got ${quote(recordedAt)}`,
196
282
  });
197
283
  }
198
284
  if (problems.length > 0)
@@ -12,10 +12,51 @@
12
12
  * The shape is closed: a field this interface does not name is refused, so a
13
13
  * field added by mistake cannot travel unnoticed into a record somebody later
14
14
  * audits. Every enumerated field draws from `./vocabulary.ts`.
15
+ *
16
+ * 🔴 `validateDeclaration` reads every field of its input through `ownField`
17
+ * (`./validation.ts`) — own and enumerable, the set `Object.keys` walks — and
18
+ * never off the record directly. `unknownKeys` on the line above already judges
19
+ * the record by `Object.keys`, so a wider read made the closed-shape check and
20
+ * the field reads disagree about what the record contains, with the reads being
21
+ * the wider of the two — the direction that passes. The consequence here is one
22
+ * step worse than in `./decision-record.ts`, because this validator's answer is
23
+ * kept: `definePolicy` copies the validated declaration by spread, and a spread
24
+ * copies own-enumerable keys only. So a declaration whose `tier` and `redaction`
25
+ * were only inherited used to be accepted and then FROZEN into the registry
26
+ * with no `tier` key and no `redaction` key at all — reading either yields
27
+ * `undefined` — a malformed entry produced by the function whose whole job is to
28
+ * refuse malformed ones. Held over all fifteen fields, in both shapes (inherited,
29
+ * and own but not enumerable), in `packages/cli/test/policy-declaration.test.ts`
30
+ * › "refuses a declaration whose %s is %s, because the declaration it writes out
31
+ * carries no such field" and, for the frozen-entry consequence, › "refuses a
32
+ * declaration whose tier is %s, rather than freezing an entry with no tier at
33
+ * all". The `%s` are the names as the `it.each` cases DECLARE them — quoting an
34
+ * expanded case instead is a pointer no grep lands on, which is what
35
+ * `test/template/evidence-pointers.test.ts` treats as a wildcard and what
36
+ * `rules/invariants.md` means by "the test's whole name".
37
+ *
38
+ * ⚠ What this does NOT do: `validateDeclaration` returns the input object
39
+ * itself (`input as unknown as PolicyDeclaration`), so what a caller gets back
40
+ * is the record it passed in, not a snapshot of the fields that were certified.
41
+ * Those are the same thing for a value whose fields are plain DATA, which is
42
+ * every declaration `JSON.parse` can produce; they are not the same for one
43
+ * carrying a live accessor. **An object literal is not the line** — a literal
44
+ * can carry a getter, and an earlier version of this sentence put literals on
45
+ * the safe side, which named the one shape that reaches the gap as the shape
46
+ * that avoids it. Here the consequence is kept rather than merely returned:
47
+ * `definePolicy` spreads the validated value, so a getter validated on read 1
48
+ * is FROZEN into the registry from read 2. That gap is RP-157, and
49
+ * `./decision-record.ts` states the identical limit at its own return.
15
50
  */
16
51
  import { AUTONOMY_TIERS, DECISION_OUTCOMES, ENFORCEMENT_TIMINGS, EVIDENCE_KINDS, FAILURE_SEMANTICS, HARNESS_CAPABILITIES, LIFECYCLE_STATES, OPERATIONS, REDACTION_RULES, } from './vocabulary.js';
17
- import { isRecord, matching, member, members, nonEmptyString, unknownKeys } from './validation.js';
18
- const KEYS = [
52
+ import { isRecord, matching, member, members, nonEmptyString, ownField, unknownKeys, } from './validation.js';
53
+ /**
54
+ * The closed set of fields a declaration may carry — exported so a test can
55
+ * check the fixture against THIS list by name rather than by counting it.
56
+ * A count agrees with a set that has drifted; `rules/invariants.md`, "One
57
+ * mechanism, one implementation".
58
+ */
59
+ export const KEYS = [
19
60
  'policyId',
20
61
  'policyVersion',
21
62
  'lifecycle',
@@ -41,23 +82,23 @@ export function validateDeclaration(input) {
41
82
  }
42
83
  const problems = [];
43
84
  unknownKeys(problems, input, KEYS);
44
- matching(problems, 'policyId', input.policyId, KEBAB_CASE, 'kebab-case');
45
- matching(problems, 'policyVersion', input.policyVersion, POLICY_VERSION, 'MAJOR.MINOR');
46
- member(problems, 'lifecycle', input.lifecycle, LIFECYCLE_STATES);
47
- nonEmptyString(problems, 'invariant', input.invariant);
48
- member(problems, 'tier', input.tier, AUTONOMY_TIERS);
49
- members(problems, 'operations', input.operations, OPERATIONS, { nonEmpty: true });
50
- member(problems, 'timing', input.timing, ENFORCEMENT_TIMINGS);
51
- member(problems, 'requiredCapability', input.requiredCapability, HARNESS_CAPABILITIES);
52
- matching(problems, 'mechanism', input.mechanism, KEBAB_CASE, 'kebab-case');
53
- members(problems, 'outcomes', input.outcomes, DECISION_OUTCOMES, { nonEmpty: true });
54
- member(problems, 'onInternalError', input.onInternalError, FAILURE_SEMANTICS);
55
- member(problems, 'onUnreadableInput', input.onUnreadableInput, FAILURE_SEMANTICS);
56
- members(problems, 'requiredEvidence', input.requiredEvidence, EVIDENCE_KINDS, {
85
+ matching(problems, 'policyId', ownField(input, 'policyId'), KEBAB_CASE, 'kebab-case');
86
+ matching(problems, 'policyVersion', ownField(input, 'policyVersion'), POLICY_VERSION, 'MAJOR.MINOR');
87
+ member(problems, 'lifecycle', ownField(input, 'lifecycle'), LIFECYCLE_STATES);
88
+ nonEmptyString(problems, 'invariant', ownField(input, 'invariant'));
89
+ member(problems, 'tier', ownField(input, 'tier'), AUTONOMY_TIERS);
90
+ members(problems, 'operations', ownField(input, 'operations'), OPERATIONS, { nonEmpty: true });
91
+ member(problems, 'timing', ownField(input, 'timing'), ENFORCEMENT_TIMINGS);
92
+ member(problems, 'requiredCapability', ownField(input, 'requiredCapability'), HARNESS_CAPABILITIES);
93
+ matching(problems, 'mechanism', ownField(input, 'mechanism'), KEBAB_CASE, 'kebab-case');
94
+ members(problems, 'outcomes', ownField(input, 'outcomes'), DECISION_OUTCOMES, { nonEmpty: true });
95
+ member(problems, 'onInternalError', ownField(input, 'onInternalError'), FAILURE_SEMANTICS);
96
+ member(problems, 'onUnreadableInput', ownField(input, 'onUnreadableInput'), FAILURE_SEMANTICS);
97
+ members(problems, 'requiredEvidence', ownField(input, 'requiredEvidence'), EVIDENCE_KINDS, {
57
98
  nonEmpty: false,
58
99
  });
59
- member(problems, 'redaction', input.redaction, REDACTION_RULES);
60
- nonEmptyString(problems, 'statedIn', input.statedIn);
100
+ member(problems, 'redaction', ownField(input, 'redaction'), REDACTION_RULES);
101
+ nonEmptyString(problems, 'statedIn', ownField(input, 'statedIn'));
61
102
  if (problems.length > 0)
62
103
  return { ok: false, problems };
63
104
  // Every field above was checked against the shape, so the narrowing is earned