@aiwg/cli 2026.10.16 → 2026.10.19

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 (155) hide show
  1. package/agentic/code/providers/capability-matrix.yaml +51 -0
  2. package/agentic/code/providers/flue/README.md +62 -0
  3. package/agentic/code/providers/flue/templates/aiwg/tools.ts +120 -0
  4. package/agentic/code/providers/model-capabilities.v1.json +23 -0
  5. package/agentic/code/providers/model-catalog.v1.json +9 -0
  6. package/dist/src/artifacts/corpus-tools/resolve.js +28 -6
  7. package/dist/src/artifacts/query-engine-corpus.js +217 -0
  8. package/dist/src/artifacts/query-engine-discover-output.js +229 -0
  9. package/dist/src/artifacts/query-engine-discover.js +285 -0
  10. package/dist/src/artifacts/query-engine-entries.js +188 -0
  11. package/dist/src/artifacts/query-engine-index.js +249 -0
  12. package/dist/src/artifacts/query-engine-matching.js +143 -0
  13. package/dist/src/artifacts/query-engine-options.js +2 -0
  14. package/dist/src/artifacts/query-engine-rules.js +202 -0
  15. package/dist/src/artifacts/query-engine-scoring.js +436 -0
  16. package/dist/src/artifacts/query-engine-show-card.js +194 -0
  17. package/dist/src/artifacts/query-engine-show-load.js +211 -0
  18. package/dist/src/artifacts/query-engine-show.js +300 -0
  19. package/dist/src/artifacts/query-engine.js +7 -2576
  20. package/dist/src/cli/handlers/help.js +1 -1
  21. package/dist/src/cli/handlers/init.js +1 -0
  22. package/dist/src/cli/handlers/mc-agents.js +111 -0
  23. package/dist/src/cli/handlers/mc-args.js +54 -0
  24. package/dist/src/cli/handlers/mc-control.js +227 -0
  25. package/dist/src/cli/handlers/mc-dispatch.js +203 -0
  26. package/dist/src/cli/handlers/mc-help.js +171 -0
  27. package/dist/src/cli/handlers/mc-run.js +343 -0
  28. package/dist/src/cli/handlers/mc-session.js +138 -0
  29. package/dist/src/cli/handlers/mc-status.js +258 -0
  30. package/dist/src/cli/handlers/mc-summary.js +43 -0
  31. package/dist/src/cli/handlers/mc-types.js +3 -0
  32. package/dist/src/cli/handlers/mc-worker.js +205 -0
  33. package/dist/src/cli/handlers/mc.js +12 -1665
  34. package/dist/src/cli/handlers/models-refusal.js +13 -0
  35. package/dist/src/cli/handlers/models.js +2 -1
  36. package/dist/src/cli/handlers/remove-bundled.js +133 -0
  37. package/dist/src/cli/handlers/remove-options.js +31 -0
  38. package/dist/src/cli/handlers/remove-user.js +174 -0
  39. package/dist/src/cli/handlers/remove.js +201 -0
  40. package/dist/src/cli/handlers/subcommands-bundles.js +257 -0
  41. package/dist/src/cli/handlers/subcommands-discovery.js +165 -0
  42. package/dist/src/cli/handlers/subcommands-list-format.js +129 -0
  43. package/dist/src/cli/handlers/subcommands-list.js +250 -0
  44. package/dist/src/cli/handlers/subcommands-plugins.js +213 -0
  45. package/dist/src/cli/handlers/subcommands-quickref.js +58 -0
  46. package/dist/src/cli/handlers/subcommands-rlm.js +113 -0
  47. package/dist/src/cli/handlers/subcommands-services.js +66 -0
  48. package/dist/src/cli/handlers/subcommands-storage.js +259 -0
  49. package/dist/src/cli/handlers/subcommands.js +18 -1984
  50. package/dist/src/cli/handlers/use.js +11 -5
  51. package/dist/src/cli/scope-resolver.js +1 -142
  52. package/dist/src/cli/services/deployment-verification.js +3 -0
  53. package/dist/src/cli/user-scope-mirror.js +173 -0
  54. package/dist/src/config/dispatch-host-thresholds.js +2 -0
  55. package/dist/src/config/dispatch-thresholds-runtime.mjs +4 -0
  56. package/dist/src/config/host-resource-report.d.mts +5 -0
  57. package/dist/src/config/host-resource-report.mjs +36 -6
  58. package/dist/src/config/host-resource-runtime.d.mts +3 -0
  59. package/dist/src/config/host-resource-runtime.mjs +9 -6
  60. package/dist/src/decision/ensemble-study/artifacts.js +7 -0
  61. package/dist/src/decision/ensemble-study/gate-policy.js +58 -0
  62. package/dist/src/decision/ensemble-study/gates.js +78 -0
  63. package/dist/src/decision/ensemble-study/protocol.js +18 -21
  64. package/dist/src/decision/ensemble-study/score.js +3 -2
  65. package/dist/src/decision/ensemble-study/statistics.js +7 -26
  66. package/dist/src/decision/sensitivity/comparative-gates.js +135 -0
  67. package/dist/src/decision/sensitivity/comparative.js +18 -41
  68. package/dist/src/extensions/claude-hooks-installer.js +18 -0
  69. package/dist/src/gates/evaluate.js +2 -358
  70. package/dist/src/gates/observations.js +363 -0
  71. package/dist/src/gates/pack-composition.js +270 -0
  72. package/dist/src/gates/pack-tightening.js +197 -0
  73. package/dist/src/gates/pack-validation.js +262 -0
  74. package/dist/src/gates/registry-error.js +5 -0
  75. package/dist/src/gates/registry.js +13 -685
  76. package/dist/src/mcp/json-document.mjs +29 -0
  77. package/dist/src/mcp/muse-native-config.mjs +1 -28
  78. package/dist/src/memory/provider-memory-adapter.js +1 -0
  79. package/dist/src/mission-protocol/worker-review-gate.js +7 -0
  80. package/dist/src/models/model-capabilities.v1.json +23 -0
  81. package/dist/src/models/model-catalog.v1.json +9 -0
  82. package/dist/src/models/policy-resources.js +1 -1
  83. package/dist/src/models/worker-plan.js +5 -5
  84. package/dist/src/plugin/command-ownership.js +34 -0
  85. package/dist/src/plugin/skill-command-frontmatter.js +197 -0
  86. package/dist/src/plugin/skill-command-translator.js +6 -232
  87. package/dist/src/providers/capability-matrix.yaml +51 -0
  88. package/dist/src/providers/context-output-ownership.js +26 -0
  89. package/dist/src/providers/deployment-ownership.d.mts +20 -0
  90. package/dist/src/providers/deployment-ownership.mjs +236 -0
  91. package/dist/src/providers/deployment-removal.js +222 -0
  92. package/dist/src/providers/provider-context-contracts.js +8 -0
  93. package/dist/src/providers/provider-definition-schema.js +1 -0
  94. package/dist/src/providers/provider-definitions.js +1 -0
  95. package/dist/src/providers/provider-seeds-3.js +18 -0
  96. package/dist/src/providers/removal-transaction.js +86 -0
  97. package/dist/src/providers/settings-document.d.mts +3 -0
  98. package/dist/src/providers/settings-document.mjs +47 -0
  99. package/dist/src/providers/settings-removal.js +190 -0
  100. package/dist/src/providers/skill-ownership.d.mts +20 -2
  101. package/dist/src/providers/skill-ownership.mjs +12 -4
  102. package/dist/src/providers/transformation-receipt-integration.js +15 -271
  103. package/dist/src/providers/transformation-receipt-outputs.js +99 -0
  104. package/dist/src/providers/transformation-receipt-source.js +194 -0
  105. package/dist/src/research/identity.js +8 -2
  106. package/dist/src/research/reference-match.js +66 -8
  107. package/dist/src/rules/activation.mjs +15 -6
  108. package/dist/src/rules/core-startup.mjs +2 -2
  109. package/dist/src/serve/mission-conductor.js +4 -0
  110. package/dist/src/serve/mission-summary.js +56 -0
  111. package/dist/src/skills/deployment-ownership.js +2 -2
  112. package/dist/src/skills/deployment-policy.js +9 -0
  113. package/dist/src/smiths/context-pipeline/aiwg-md.js +23 -0
  114. package/dist/src/smiths/context-pipeline/context-file-safety.js +3 -0
  115. package/dist/src/smiths/context-pipeline/flue-context.js +25 -0
  116. package/dist/src/smiths/context-pipeline/generator.js +7 -2
  117. package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +9 -7
  118. package/dist/src/smiths/hook-bridge/index.js +9 -0
  119. package/package.json +5 -1
  120. package/schemas/decision/D17StudyProtocol.v1.schema.json +39 -67
  121. package/tools/agents/deploy-agents.mjs +21 -680
  122. package/tools/agents/deploy-command-mirrors.mjs +229 -0
  123. package/tools/agents/deploy-options.mjs +267 -0
  124. package/tools/agents/deploy-support.mjs +165 -0
  125. package/tools/agents/providers/base-addon-discovery.mjs +139 -0
  126. package/tools/agents/providers/base-behaviors.mjs +168 -0
  127. package/tools/agents/providers/base-context.mjs +205 -0
  128. package/tools/agents/providers/base-contract.mjs +116 -0
  129. package/tools/agents/providers/base-files.mjs +252 -0
  130. package/tools/agents/providers/base-flat-artifacts.mjs +233 -0
  131. package/tools/agents/providers/base-framework-discovery.mjs +309 -0
  132. package/tools/agents/providers/base-frontmatter.mjs +196 -0
  133. package/tools/agents/providers/base-inventory.mjs +269 -0
  134. package/tools/agents/providers/base-kernel-skills.mjs +354 -0
  135. package/tools/agents/providers/base-migrations.mjs +258 -0
  136. package/tools/agents/providers/base-models-platforms.mjs +273 -0
  137. package/tools/agents/providers/base-rule-index.mjs +450 -0
  138. package/tools/agents/providers/base-runtime.mjs +60 -0
  139. package/tools/agents/providers/base-skill-copy.mjs +273 -0
  140. package/tools/agents/providers/base.mjs +16 -3364
  141. package/tools/agents/providers/claude-post-deploy.mjs +10 -2
  142. package/tools/agents/providers/flue-project.mjs +79 -0
  143. package/tools/agents/providers/flue-specialists.mjs +64 -0
  144. package/tools/agents/providers/flue.mjs +50 -0
  145. package/tools/agents/providers/hermes-startup.mjs +0 -5
  146. package/tools/agents/providers/muse.mjs +7 -2
  147. package/tools/agents/providers/rule-delivery-surface.mjs +33 -5
  148. package/tools/agents/providers/rule-deployment-commit.mjs +19 -15
  149. package/tools/agents/providers/rule-deployment-prepare.mjs +13 -3
  150. package/tools/agents/providers/rule-projection.mjs +3 -0
  151. package/tools/agents/providers/rule-startup.mjs +3 -3
  152. package/tools/agents/use-rule-selection.mjs +17 -0
  153. package/tools/ralph-external/lib/worker-invocation-processes.mjs +4 -2
  154. package/tools/ralph-external/lib/worker-termination.mjs +15 -8
  155. package/vscode-extension/schemas/aiwg.config.v1.json +2 -4
@@ -1,12 +1,14 @@
1
- import { ceilingDecision } from './lattice.js';
2
- import { canonicalJson } from '../security/artifact-trust.js';
1
+ // Register pinned packs and resolve validated binding parameters against them.
3
2
  import { artifactDigest } from '../decision/validate.js';
4
3
  import { validateGateDocument } from './schema.js';
5
4
  import { qualifyGateParameter } from './types.js';
6
- import { INTEGRITY_CEILING_FLOOR_PACK_ID, assertCeilingDefaults, projectCeilingSatisfied } from './floors.js';
7
- export class GateRegistryError extends Error {
8
- constructor(message) { super(message); this.name = 'GateRegistryError'; }
9
- }
5
+ import { GateRegistryError } from './registry-error.js';
6
+ import { checkPairedMargin, isParamRef, validateResolvedPack } from './pack-validation.js';
7
+ import { composeGatePack, expandFloorPacks, enforceProjectFloors } from './pack-composition.js';
8
+ export { GateRegistryError } from './registry-error.js';
9
+ export { validateResolvedPack, resolveNumberDefault } from './pack-validation.js';
10
+ export { assertGateTightens, resolveThresholdDefault } from './pack-tightening.js';
11
+ export { applyGateExtends, composeGatePack, expandFloorPacks, enforceProjectFloors } from './pack-composition.js';
10
12
  export const GATE_NAMESPACES = ['aiwg', 'framework', 'addon', 'extension', 'project'];
11
13
  export function splitPackId(id) {
12
14
  if (typeof id !== 'string')
@@ -26,686 +28,7 @@ export function splitPackId(id) {
26
28
  }
27
29
  return { namespace: namespace, rest };
28
30
  }
29
- const fail = (message) => { throw new GateRegistryError(message); };
30
- const STATISTICAL_KINDS = ['interval-bound', 'paired-difference', 'bootstrap-bound'];
31
- function checkThresholdBlock(gate, params) {
32
- const needsThreshold = gate.kind === 'interval-bound' || gate.kind === 'bootstrap-bound'
33
- || gate.kind === 'count-max' || gate.kind === 'count-min' || gate.kind === 'value-threshold';
34
- const threshold = gate.threshold;
35
- if (threshold === undefined) {
36
- if (needsThreshold)
37
- fail(`gate ${gate.id} of kind ${gate.kind} requires a threshold`);
38
- return;
39
- }
40
- if (!needsThreshold)
41
- fail(`gate ${gate.id} of kind ${gate.kind} must not carry a threshold`);
42
- const param = threshold.param === undefined ? undefined : params[threshold.param];
43
- if (threshold.param !== undefined && param === undefined)
44
- fail(`gate ${gate.id} references unknown parameter '${threshold.param}'`);
45
- const expectedParamType = gate.kind === 'interval-bound' || gate.kind === 'bootstrap-bound' ? 'bps'
46
- : gate.kind === 'count-max' || gate.kind === 'count-min' ? 'count' : 'value';
47
- if (param !== undefined && param.type !== expectedParamType) {
48
- fail(`gate ${gate.id} parameter '${threshold.param}' must have type ${expectedParamType}`);
49
- }
50
- if (threshold.value !== undefined) {
51
- if (!Number.isFinite(threshold.value))
52
- fail(`gate ${gate.id} threshold value must be finite`);
53
- if (expectedParamType === 'bps' && (threshold.value < 0 || threshold.value > 10000)) {
54
- fail(`gate ${gate.id} threshold must be within 0..10000 bps`);
55
- }
56
- if (expectedParamType === 'count' && (!Number.isSafeInteger(threshold.value) || threshold.value < 0)) {
57
- fail(`gate ${gate.id} threshold must be a non-negative integer count`);
58
- }
59
- }
60
- const op = gate.kind === 'count-max' ? 'lte' : gate.kind === 'count-min' ? 'gte' : threshold.op;
61
- if (gate.kind === 'count-max' && threshold.op !== 'lte')
62
- fail(`gate ${gate.id} count-max requires op lte`);
63
- if (gate.kind === 'count-min' && threshold.op !== 'gte')
64
- fail(`gate ${gate.id} count-min requires op gte`);
65
- if (gate.kind === 'interval-bound' && gate.statistic?.bound === 'upper' && threshold.op !== 'lte') {
66
- fail(`gate ${gate.id} upper-bound gates require op lte`);
67
- }
68
- if (gate.kind === 'interval-bound' && gate.statistic?.bound === 'lower' && threshold.op !== 'gte') {
69
- fail(`gate ${gate.id} lower-bound gates require op gte`);
70
- }
71
- if (gate.kind === 'value-threshold') {
72
- if (threshold.op === 'lte' && gate.direction !== 'lower-is-stricter')
73
- fail(`gate ${gate.id} lte requires direction lower-is-stricter`);
74
- if (threshold.op === 'gte' && gate.direction !== 'higher-is-stricter')
75
- fail(`gate ${gate.id} gte requires direction higher-is-stricter`);
76
- }
77
- if (gate.kind === 'interval-bound' || gate.kind === 'bootstrap-bound') {
78
- if (op === 'lte' && gate.direction !== 'lower-is-stricter')
79
- fail(`gate ${gate.id} upper-bound gates require direction lower-is-stricter`);
80
- if (op === 'gte' && gate.direction !== 'higher-is-stricter')
81
- fail(`gate ${gate.id} lower-bound gates require direction higher-is-stricter`);
82
- }
83
- }
84
- const isParamRef = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value)
85
- && typeof value.param === 'string';
86
- /**
87
- * Resolve a literal-or-parameter number against pack parameter defaults.
88
- * Returns the default for a `{param}` reference, or null when the reference
89
- * names an unknown parameter, the type mismatches, or no default pins it.
90
- */
91
- export function resolveNumberDefault(value, params, expectedType) {
92
- if (value === undefined)
93
- return null;
94
- if (typeof value === 'number')
95
- return value;
96
- if (!isParamRef(value))
97
- return null;
98
- const declared = params[value.param];
99
- if (declared === undefined || declared.type !== expectedType)
100
- return null;
101
- return declared.default ?? null;
102
- }
103
- /** A literal-or-parameter reference names a known parameter of the expected type. */
104
- function checkParamRef(gateId, value, params, expectedType, what) {
105
- if (value === undefined || typeof value === 'number')
106
- return;
107
- if (!isParamRef(value) || params[value.param] === undefined) {
108
- fail(`gate ${gateId} ${what} references unknown parameter '${value?.param}'`);
109
- }
110
- if (params[value.param].type !== expectedType) {
111
- fail(`gate ${gateId} ${what} parameter '${value.param}' must have type ${expectedType}`);
112
- }
113
- }
114
- /** Statistic coherence per kind. Irrelevant statistic fields are rejected so confused authoring fails at load. */
115
- function checkStatisticBlock(gate, params) {
116
- const statistic = gate.statistic;
117
- if (statistic === undefined) {
118
- if (STATISTICAL_KINDS.includes(gate.kind)) {
119
- fail(`gate ${gate.id} of kind ${gate.kind} requires a statistic`);
120
- }
121
- return;
122
- }
123
- if (!STATISTICAL_KINDS.includes(gate.kind)) {
124
- fail(`gate ${gate.id} of kind ${gate.kind} must not carry a statistic`);
125
- }
126
- if (statistic.kind !== gate.kind)
127
- fail(`gate ${gate.id} statistic kind must match the gate kind`);
128
- const { method, bound, levelBps, mode, marginBps, seed, resamples, bounds } = statistic;
129
- if (gate.kind === 'interval-bound') {
130
- if (method !== undefined && method !== 'wilson' && method !== 'clopper-pearson') {
131
- fail(`gate ${gate.id} interval-bound method must be wilson or clopper-pearson`);
132
- }
133
- if (bound !== 'upper' && bound !== 'lower')
134
- fail(`gate ${gate.id} interval-bound requires bound upper|lower`);
135
- if (levelBps === undefined)
136
- fail(`gate ${gate.id} interval-bound requires levelBps`);
137
- checkParamRef(gate.id, levelBps, params, 'level', 'levelBps');
138
- if (mode !== undefined || marginBps !== undefined || seed !== undefined || resamples !== undefined || bounds !== undefined) {
139
- fail(`gate ${gate.id} interval-bound must not carry mode, margin, seed, resamples or bounds`);
140
- }
141
- }
142
- else if (gate.kind === 'paired-difference') {
143
- if (method !== undefined && method !== 'newcombe' && method !== 'tango') {
144
- fail(`gate ${gate.id} paired-difference method must be newcombe or tango`);
145
- }
146
- if (bound !== undefined)
147
- fail(`gate ${gate.id} paired-difference must not carry bound`);
148
- if (levelBps === undefined)
149
- fail(`gate ${gate.id} paired-difference requires levelBps`);
150
- checkParamRef(gate.id, levelBps, params, 'level', 'levelBps');
151
- const resolvedMode = mode ?? 'non-inferiority';
152
- if (resolvedMode !== 'non-inferiority' && resolvedMode !== 'superiority')
153
- fail(`gate ${gate.id} unknown paired mode`);
154
- if (resolvedMode === 'non-inferiority' && marginBps === undefined)
155
- fail(`gate ${gate.id} non-inferiority requires marginBps`);
156
- if (resolvedMode === 'non-inferiority' && marginBps !== undefined && marginBps > 0) {
157
- fail(`gate ${gate.id} non-inferiority marginBps must be non-positive`);
158
- }
159
- if (resolvedMode === 'superiority' && marginBps !== undefined && marginBps < 0) {
160
- fail(`gate ${gate.id} superiority marginBps must be non-negative`);
161
- }
162
- if (seed !== undefined || resamples !== undefined || bounds !== undefined) {
163
- fail(`gate ${gate.id} paired-difference must not carry seed, resamples or bounds`);
164
- }
165
- }
166
- else {
167
- if (method !== undefined && method !== 'bootstrap')
168
- fail(`gate ${gate.id} bootstrap-bound method must be bootstrap`);
169
- if (bound !== 'upper' && bound !== 'lower')
170
- fail(`gate ${gate.id} bootstrap-bound requires bound upper|lower`);
171
- if (levelBps === undefined || seed === undefined || resamples === undefined || bounds === undefined) {
172
- fail(`gate ${gate.id} bootstrap-bound requires levelBps, seed, resamples and bounds`);
173
- }
174
- checkParamRef(gate.id, levelBps, params, 'level', 'levelBps');
175
- if (mode !== undefined || marginBps !== undefined)
176
- fail(`gate ${gate.id} bootstrap-bound must not carry mode or margin`);
177
- }
178
- }
179
- function checkParameter(name, parameter) {
180
- if (parameter.default !== undefined) {
181
- if (!Number.isFinite(parameter.default))
182
- fail(`parameter ${name} default must be finite`);
183
- if (parameter.type === 'bps' && (parameter.default < 0 || parameter.default > 10000)) {
184
- fail(`parameter ${name} bps default must be within 0..10000`);
185
- }
186
- if (parameter.type === 'level' && (!Number.isSafeInteger(parameter.default)
187
- || parameter.default <= 5000 || parameter.default >= 9999)) {
188
- fail(`parameter ${name} level default must be an integer strictly between 5000 and 9999`);
189
- }
190
- if (parameter.type === 'count' && (!Number.isSafeInteger(parameter.default) || parameter.default < 0)) {
191
- fail(`parameter ${name} count default must be a non-negative integer`);
192
- }
193
- }
194
- }
195
- /** Semantic validation of one fully resolved pack against the provider registry. */
196
- export function validateResolvedPack(pack, providers) {
197
- const ids = pack.spec.gates.map(gate => gate.id);
198
- if (new Set(ids).size !== ids.length)
199
- fail(`pack ${pack.metadata.id} has duplicate gate ids`);
200
- const params = pack.spec.parameters ?? {};
201
- for (const [name, parameter] of Object.entries(params))
202
- checkParameter(name, parameter);
203
- for (const [name, metric] of Object.entries(pack.spec.metrics)) {
204
- if (!providers.has(metric.provider))
205
- fail(`pack ${pack.metadata.id} metric ${name} names unknown provider ${metric.provider}`);
206
- const provider = providers.get(metric.provider);
207
- const declared = provider.metrics[name];
208
- if (declared === undefined)
209
- fail(`pack ${pack.metadata.id} metric ${name} is not computed by ${metric.provider}`);
210
- if (declared.kind !== metric.kind) {
211
- fail(`pack ${pack.metadata.id} metric ${name} kind ${metric.kind} does not match provider kind ${declared.kind}`);
212
- }
213
- }
214
- for (const gate of pack.spec.gates) {
215
- if (gate.onFail !== 'HOLD' && gate.onFail !== 'ROLLBACK') {
216
- fail(`gate ${gate.id} onFail must be HOLD or ROLLBACK, never PROMOTE`);
217
- }
218
- // Insufficient evidence always holds: ROLLBACK needs an observed blocking
219
- // failure, never a shortfall. Rejected here at load, not just in schema.
220
- if (gate.onInsufficient !== undefined && gate.onInsufficient !== 'HOLD') {
221
- fail(`gate ${gate.id} onInsufficient must be HOLD: insufficient evidence never rolls back`);
222
- }
223
- if (gate.metric !== undefined) {
224
- const declared = pack.spec.metrics[gate.metric.name];
225
- if (declared === undefined || declared.provider !== gate.metric.provider) {
226
- fail(`gate ${gate.id} references undeclared metric ${gate.metric.provider}:${gate.metric.name}`);
227
- }
228
- }
229
- const metricKind = gate.metric === undefined ? undefined : pack.spec.metrics[gate.metric.name]?.kind;
230
- const expectedKind = gate.kind === 'interval-bound' || gate.kind === 'count-max' || gate.kind === 'count-min' ? 'proportion'
231
- : gate.kind === 'paired-difference' ? 'paired' : gate.kind === 'bootstrap-bound' ? 'differences'
232
- : gate.kind === 'value-threshold' ? 'scalar' : gate.kind === 'evidence' ? 'evidence' : undefined;
233
- if (expectedKind !== undefined && metricKind !== expectedKind) {
234
- fail(`gate ${gate.id} of kind ${gate.kind} requires a ${expectedKind} metric`);
235
- }
236
- checkThresholdBlock(gate, params);
237
- checkStatisticBlock(gate, params);
238
- checkParamRef(gate.id, gate.minimumN, params, 'count', 'minimumN');
239
- if (gate.kind === 'minimum-n') {
240
- if (gate.minimumN === undefined)
241
- fail(`gate ${gate.id} minimum-n requires minimumN >= 1`);
242
- // A default is the enforced minimum, so a defaulted reference must pin
243
- // at least 1; a defaultless reference is supplied by the binding and an
244
- // unresolvable value holds at evaluation, never promotes.
245
- const resolved = resolveNumberDefault(gate.minimumN, params, 'count');
246
- if (typeof gate.minimumN === 'number' && gate.minimumN < 1) {
247
- fail(`gate ${gate.id} minimum-n requires minimumN >= 1`);
248
- }
249
- if (isParamRef(gate.minimumN) && params[gate.minimumN.param]?.default !== undefined && (resolved ?? 0) < 1) {
250
- fail(`gate ${gate.id} minimum-n requires minimumN >= 1`);
251
- }
252
- if (metricKind === 'evidence')
253
- fail(`gate ${gate.id} minimum-n cannot observe an evidence metric`);
254
- }
255
- else if (gate.minimumN !== undefined && typeof gate.minimumN === 'number'
256
- && (!Number.isSafeInteger(gate.minimumN) || gate.minimumN < 0)) {
257
- fail(`gate ${gate.id} minimumN must be a non-negative integer`);
258
- }
259
- if (gate.kind === 'evidence' && (gate.evidence === undefined || !gate.evidence.required.length)) {
260
- fail(`gate ${gate.id} evidence requires a nonempty required list`);
261
- }
262
- if (gate.kind !== 'evidence' && gate.evidence !== undefined)
263
- fail(`gate ${gate.id} of kind ${gate.kind} must not carry evidence`);
264
- if (gate.kind !== 'predicate' && gate.predicate !== undefined)
265
- fail(`gate ${gate.id} of kind ${gate.kind} must not carry a predicate`);
266
- if ((gate.scope.mode === 'listed' || gate.scope.mode === 'pooled')
267
- && (gate.scope.slices === undefined || !gate.scope.slices.length)) {
268
- fail(`gate ${gate.id} scope ${gate.scope.mode} requires a nonempty slices list`);
269
- }
270
- // `except` is honoured only by `each` (per-slice minus exclusions). Any other
271
- // mode silently ignoring it would be a fail-open scoping bug, so it is a load error.
272
- if (gate.scope.except !== undefined && gate.scope.mode !== 'each') {
273
- fail(`gate ${gate.id} scope except is only allowed with mode each`);
274
- }
275
- }
276
- }
277
- /** Resolve a `$param` threshold against pack parameter defaults. Null when no default pins it. */
278
- export function resolveThresholdDefault(gate, params) {
279
- if (gate.threshold === undefined)
280
- return null;
281
- if (gate.threshold.value !== undefined)
282
- return gate.threshold.value;
283
- if (gate.threshold.param === undefined)
284
- return null;
285
- return params[gate.threshold.param]?.default ?? null;
286
- }
287
- /**
288
- * Scope-tightening rules per gate kind. A scope change is a tightening only when
289
- * the child observes every slice the parent observes under every possible
290
- * binding inventory (so a breach the parent sees can never go unobserved)
291
- * and every world that fails the child also fails the parent:
292
- * - `listed` may only widen (more slices fail in a superset of worlds);
293
- * - `pooled` sets are fixed;
294
- * - `listed -> each` widens coverage to the whole binding inventory, so the
295
- * child's `except` must exclude none of the parent's listed slices;
296
- * - `all -> each` covers the whole inventory only with no exceptions, and is a
297
- * tightening only when per-slice checks are strictly harder than the pooled
298
- * check (`count-min`, `minimum-n`); for `count-max` the pooled bound is
299
- * strictly harder (pooled<=2 implies per-slice<=2, not vice versa), and for
300
- * interval/paired/bootstrap/value gates pooled and per-slice bounds are
301
- * incomparable — so the change is rejected for every other kind.
302
- * Every other cross-mode change crosses aggregation or drops coverage and is
303
- * rejected for every kind.
304
- */
305
- function assertScopeTightens(parent, child) {
306
- const from = parent.scope;
307
- const to = child.scope;
308
- if (from.mode === to.mode) {
309
- if (to.mode === 'listed' || to.mode === 'pooled') {
310
- const before = new Set(from.slices ?? []);
311
- const after = new Set(to.slices ?? []);
312
- if (to.mode === 'listed') {
313
- // A wider listed set fails in a superset of worlds, so it tightens.
314
- for (const slice of before) {
315
- if (!after.has(slice))
316
- fail(`gate ${child.id} scope listed may not drop inherited slices`);
317
- }
318
- }
319
- else if (after.size !== before.size || [...after].some(slice => !before.has(slice))) {
320
- fail(`gate ${child.id} scope pooled may not change its slice set`);
321
- }
322
- }
323
- const beforeExcept = new Set(from.except ?? []);
324
- for (const slice of to.except ?? []) {
325
- if (!beforeExcept.has(slice))
326
- fail(`gate ${child.id} scope may only narrow inherited exceptions`);
327
- }
328
- return;
329
- }
330
- if (from.mode === 'listed' && to.mode === 'each') {
331
- const excluded = new Set(to.except ?? []);
332
- for (const slice of from.slices ?? []) {
333
- if (excluded.has(slice))
334
- fail(`gate ${child.id} scope listed -> each may not except inherited slice '${slice}'`);
335
- }
336
- return;
337
- }
338
- if (from.mode === 'all' && to.mode === 'each'
339
- && (child.kind === 'count-min' || child.kind === 'minimum-n')) {
340
- // `all` watches the whole inventory: any exception drops a watched slice.
341
- if ((to.except ?? []).length)
342
- fail(`gate ${child.id} scope all -> each may not except slices`);
343
- return;
344
- }
345
- fail(`gate ${child.id} scope change ${from.mode} -> ${to.mode} is not a monotone tightening`);
346
- }
347
- /**
348
- * Proves monotone tightening of a child gate over its parent. Any loosening —
349
- * a relaxed threshold, a literal rewritten as a bindable parameter, a renamed
350
- * parameter, a lowered minimumN, a demoted outcome, a widened scope
351
- * or a changed statistic — is a load error.
352
- */
353
- export function assertGateTightens(parent, child, params) {
354
- if (parent.kind !== child.kind)
355
- fail(`gate ${child.id} may not change kind ${parent.kind} -> ${child.kind}`);
356
- if (canonicalJson(parent.metric ?? null) !== canonicalJson(child.metric ?? null)) {
357
- fail(`gate ${child.id} may not change its metric reference`);
358
- }
359
- if (parent.direction !== child.direction)
360
- fail(`gate ${child.id} may not change its tightening direction`);
361
- const parentStatistic = parent.statistic;
362
- const childStatistic = child.statistic;
363
- if (canonicalJson({ ...parentStatistic, levelBps: 0 }) !== canonicalJson({ ...childStatistic, levelBps: 0 })) {
364
- fail(`gate ${child.id} may not change its statistic (levelBps aside)`);
365
- }
366
- // A literal level is a pinned bound: rewriting it as a parameter hands the
367
- // bound to the binding, whose allowed range is wider than the literal.
368
- const parentLevel = parentStatistic?.levelBps;
369
- const childLevel = childStatistic?.levelBps;
370
- if (typeof parentLevel === 'number' && isParamRef(childLevel)) {
371
- fail(`gate ${child.id} may not convert a literal levelBps into a parameter`);
372
- }
373
- if (isParamRef(parentLevel) && isParamRef(childLevel) && parentLevel.param !== childLevel.param) {
374
- fail(`gate ${child.id} may not rename its levelBps parameter`);
375
- }
376
- const levelBefore = parentLevel === undefined ? undefined : resolveNumberDefault(parentLevel, params, 'level');
377
- const levelAfter = childLevel === undefined ? undefined : resolveNumberDefault(childLevel, params, 'level');
378
- if (levelBefore === null || levelAfter === null) {
379
- if (canonicalJson(parentLevel ?? null) !== canonicalJson(childLevel ?? null)) {
380
- fail(`gate ${child.id} levelBps change is unresolvable without parameter defaults`);
381
- }
382
- }
383
- else if ((levelAfter ?? levelBefore) !== undefined && (levelAfter ?? 0) < (levelBefore ?? 0)) {
384
- fail(`gate ${child.id} may only raise levelBps`);
385
- }
386
- if ((parentStatistic?.mode ?? 'non-inferiority') !== (childStatistic?.mode ?? 'non-inferiority')) {
387
- fail(`gate ${child.id} may not change its paired mode`);
388
- }
389
- const marginBefore = parentStatistic?.marginBps;
390
- const marginAfter = childStatistic?.marginBps;
391
- if (marginBefore !== undefined && marginAfter !== undefined && marginAfter < marginBefore) {
392
- fail(`gate ${child.id} may only raise marginBps`);
393
- }
394
- // A literal threshold is a pinned bound. Rewriting it as a parameter hands the
395
- // bound to the binding, whose allowed range (e.g. 0..10000 bps) is wider than
396
- // the literal — a loosening no default-equality check can see. Forbid it.
397
- if (parent.threshold?.value !== undefined && child.threshold?.param !== undefined) {
398
- fail(`gate ${child.id} may not convert a literal threshold into a parameter`);
399
- }
400
- // Renaming a parameter rebinds the gate to a fresh default and range. Params
401
- // may only keep their name with a tighter-or-equal default.
402
- if (parent.threshold?.param !== undefined && child.threshold?.param !== undefined
403
- && parent.threshold.param !== child.threshold.param) {
404
- fail(`gate ${child.id} may not rename its threshold parameter`);
405
- }
406
- const before = resolveThresholdDefault(parent, params);
407
- const after = resolveThresholdDefault(child, params);
408
- if (before === null || after === null) {
409
- if (canonicalJson(parent.threshold ?? null) !== canonicalJson(child.threshold ?? null)) {
410
- fail(`gate ${child.id} threshold change is unresolvable without parameter defaults`);
411
- }
412
- }
413
- else if (child.direction === 'lower-is-stricter' ? after > before : after < before) {
414
- fail(`gate ${child.id} threshold ${before} -> ${after} loosens direction ${child.direction}`);
415
- }
416
- if (typeof parent.minimumN === 'number' && isParamRef(child.minimumN)) {
417
- fail(`gate ${child.id} may not convert a literal minimumN into a parameter`);
418
- }
419
- if (isParamRef(parent.minimumN) && isParamRef(child.minimumN) && parent.minimumN.param !== child.minimumN.param) {
420
- fail(`gate ${child.id} may not rename its minimumN parameter`);
421
- }
422
- const minimumBefore = parent.minimumN === undefined ? 0 : resolveNumberDefault(parent.minimumN, params, 'count');
423
- const minimumAfter = child.minimumN === undefined ? 0 : resolveNumberDefault(child.minimumN, params, 'count');
424
- if (minimumBefore === null || minimumAfter === null) {
425
- if (canonicalJson(parent.minimumN ?? null) !== canonicalJson(child.minimumN ?? null)) {
426
- fail(`gate ${child.id} minimumN change is unresolvable without parameter defaults`);
427
- }
428
- }
429
- else if (minimumAfter < minimumBefore) {
430
- fail(`gate ${child.id} may only raise minimumN`);
431
- }
432
- if (ceilingDecision(child.onFail, parent.onFail) !== child.onFail) {
433
- fail(`gate ${child.id} may not demote onFail ROLLBACK -> HOLD`);
434
- }
435
- // Insufficient evidence always holds: ROLLBACK needs an observed blocking
436
- // failure, so neither side may name it. Checked here as well as in schema
437
- // and pack validation because this pure function is the tightening gate.
438
- if ((parent.onInsufficient ?? 'HOLD') !== 'HOLD' || (child.onInsufficient ?? 'HOLD') !== 'HOLD') {
439
- fail(`gate ${child.id} onInsufficient must be HOLD: insufficient evidence never rolls back`);
440
- }
441
- if (parent.floor === true && child.floor !== true)
442
- fail(`gate ${child.id} is a floor gate and cannot be un-floored`);
443
- assertScopeTightens(parent, child);
444
- const afterEvidence = new Set(child.evidence?.required ?? []);
445
- for (const id of parent.evidence?.required ?? []) {
446
- if (!afterEvidence.has(id))
447
- fail(`gate ${child.id} may not drop required evidence ${id}`);
448
- }
449
- if (canonicalJson(parent.predicate ?? null) !== canonicalJson(child.predicate ?? null)) {
450
- fail(`gate ${child.id} may not change its predicate`);
451
- }
452
- }
453
31
  const pinLabel = (pin) => `${pin.id}@${pin.version}`;
454
- /**
455
- * Pure `extends` composition: merges a child's declarations over its already
456
- * resolved parent. No registry state is read, so a `GateRegistry` subclass
457
- * cannot change tightening semantics by overriding an instance method. The
458
- * evaluator calls this (via `composeGatePack`/`resolveGateBinding`) instead of
459
- * any overridable `GateRegistry` method for security-relevant resolution.
460
- */
461
- export function applyGateExtends(parentResolved, child) {
462
- const mergedParams = { ...(parentResolved.spec.parameters ?? {}) };
463
- for (const [name, parameter] of Object.entries(child.spec.parameters ?? {})) {
464
- const before = mergedParams[name];
465
- if (before === undefined) {
466
- mergedParams[name] = parameter;
467
- continue;
468
- }
469
- if (before.type !== parameter.type || before.direction !== parameter.direction) {
470
- throw new GateRegistryError(`pack ${child.metadata.id} may not change parameter ${name} type or direction`);
471
- }
472
- if (before.default === undefined && parameter.default !== undefined) {
473
- throw new GateRegistryError(`pack ${child.metadata.id} may not add a default to required parameter ${name}`);
474
- }
475
- if (before.default !== undefined && parameter.default === undefined) {
476
- mergedParams[name] = parameter;
477
- continue;
478
- }
479
- if (before.default !== undefined && parameter.default !== undefined) {
480
- const tightens = parameter.direction === 'lower-is-stricter'
481
- ? parameter.default <= before.default : parameter.default >= before.default;
482
- if (!tightens)
483
- throw new GateRegistryError(`pack ${child.metadata.id} parameter ${name} default loosens`);
484
- mergedParams[name] = parameter;
485
- }
486
- }
487
- const parentGates = new Map(parentResolved.spec.gates.map(gate => [gate.id, gate]));
488
- const merged = [];
489
- const seen = new Set();
490
- for (const gate of parentResolved.spec.gates) {
491
- const override = (child.spec.gates ?? []).find(candidate => candidate.id === gate.id);
492
- if (override === undefined) {
493
- // Removing a non-floor gate is allowed; floor gates pin the minimum.
494
- if (gate.floor === true)
495
- throw new GateRegistryError(`pack ${child.metadata.id} may not remove floor gate ${gate.id}`);
496
- continue;
497
- }
498
- seen.add(gate.id);
499
- assertGateTightens(gate, override, mergedParams);
500
- merged.push(override);
501
- }
502
- for (const gate of child.spec.gates ?? []) {
503
- if (!seen.has(gate.id) && !parentGates.has(gate.id))
504
- merged.push(gate);
505
- else if (!seen.has(gate.id))
506
- fail(`pack ${child.metadata.id} duplicates gate ${gate.id}`);
507
- }
508
- const mergedMetrics = { ...parentResolved.spec.metrics };
509
- for (const [name, metric] of Object.entries(child.spec.metrics ?? {})) {
510
- const before = mergedMetrics[name];
511
- if (before !== undefined && (before.provider !== metric.provider || before.kind !== metric.kind)) {
512
- throw new GateRegistryError(`pack ${child.metadata.id} may not change inherited metric ${name}`);
513
- }
514
- mergedMetrics[name] = metric;
515
- }
516
- return {
517
- apiVersion: child.apiVersion, kind: child.kind, metadata: child.metadata,
518
- spec: {
519
- extends: child.spec.extends, parameters: mergedParams, metrics: mergedMetrics, gates: merged,
520
- // Omit `combine` when the child leaves it unset: a present-but-undefined
521
- // key is not JSON and would poison the composed-pack digest.
522
- ...(child.spec.combine === undefined ? {} : { combine: child.spec.combine }),
523
- },
524
- };
525
- }
526
- /**
527
- * Pure extends-chain composition. The lookup returns authored packs by full id;
528
- * parent pins (version + authored digest) are enforced here, so a loosened
529
- * parent always moves the composed digest.
530
- */
531
- export function composeGatePack(pack, lookupAuthored) {
532
- if (pack.spec.extends === undefined)
533
- return pack;
534
- const parentAuthored = lookupAuthored(pack.spec.extends.id);
535
- const parentPin = pack.spec.extends;
536
- if (parentAuthored.metadata.version !== parentPin.version
537
- || artifactDigest(parentAuthored) !== parentPin.digest) {
538
- throw new GateRegistryError(`pack ${pack.metadata.id} parent pin mismatch for ${pinLabel(parentPin)}`);
539
- }
540
- return applyGateExtends(composeGatePack(parentAuthored, lookupAuthored), pack);
541
- }
542
- /**
543
- * Whether one expanded floor's `integrity-ceiling` gate genuinely governs the
544
- * default ceiling: it must be an all-scoped `upstream-ceiling` gate that
545
- * tightens the shipped default gate (floor flag included). A same-id gate of
546
- * any other kind or scope never suppresses the default.
547
- */
548
- function governsDefaultCeiling(floor, defaultGate) {
549
- return floor.gates.some(gate => {
550
- if (gate.id !== 'integrity-ceiling' || gate.kind !== 'upstream-ceiling' || gate.scope.mode !== 'all') {
551
- return false;
552
- }
553
- try {
554
- assertGateTightens(defaultGate, gate, floor.params);
555
- return true;
556
- }
557
- catch {
558
- return false;
559
- }
560
- });
561
- }
562
- /**
563
- * Expands project floors over an explicit authored-pack map: inline packs are
564
- * schema-checked again (direct registry callers bypass config validation) and
565
- * semantically validated, pack references are verified by version and authored
566
- * digest and composed through the pure `composeGatePack` (so `extends` floors
567
- * enforce their composed minima). Every floor gate that binds a threshold
568
- * parameter must declare a default for it: the default is the enforced
569
- * minimum, and a required (default-less) floor parameter is a load error.
570
- *
571
- * The operator default floor — the composed gates of the shipped
572
- * `aiwg:decision-engine/integrity-ceiling` pack, the single source of truth —
573
- * is appended unless a configured floor governs it via `governsDefaultCeiling`
574
- * above. An inline gate that merely reuses the id (any other kind or scope)
575
- * never suppresses the default; the binding must then satisfy both, which
576
- * fails closed with a diagnostic.
577
- */
578
- export function expandFloorPacks(floors, lookupAuthored, providers) {
579
- const expanded = (floors.floors ?? []).map(source => {
580
- if ('packRef' in source) {
581
- const pin = source.packRef;
582
- const authored = lookupAuthored(pin.id);
583
- if (authored.metadata.version !== pin.version
584
- || artifactDigest(authored) !== pin.digest) {
585
- throw new GateRegistryError(`project floor pack pin mismatch for ${pinLabel(pin)}`);
586
- }
587
- const composed = composeGatePack(authored, lookupAuthored);
588
- validateResolvedPack(composed, providers);
589
- return {
590
- packId: authored.metadata.id,
591
- gates: composed.spec.gates,
592
- params: { ...(composed.spec.parameters ?? {}) },
593
- };
594
- }
595
- const pack = validateGateDocument(source.pack);
596
- if (pack.kind !== 'GatePack')
597
- throw new GateRegistryError('project floor pack requires a GatePack document');
598
- const composed = pack.spec.extends === undefined ? pack : composeGatePack(pack, lookupAuthored);
599
- validateResolvedPack(composed, providers);
600
- for (const gate of composed.spec.gates) {
601
- const level = gate.statistic?.levelBps;
602
- const refs = [gate.threshold?.param, isParamRef(gate.minimumN) ? gate.minimumN.param : undefined,
603
- isParamRef(level) ? level.param : undefined];
604
- for (const name of refs) {
605
- if (name !== undefined && composed.spec.parameters?.[name]?.default === undefined) {
606
- throw new GateRegistryError(`project floor gate '${gate.id}' parameter '${name}' requires a default: it is the enforced minimum`);
607
- }
608
- }
609
- }
610
- return {
611
- packId: pack.metadata.id,
612
- gates: composed.spec.gates,
613
- params: { ...(composed.spec.parameters ?? {}) },
614
- };
615
- });
616
- const defaultAuthored = lookupAuthored(INTEGRITY_CEILING_FLOOR_PACK_ID);
617
- const defaultComposed = composeGatePack(defaultAuthored, lookupAuthored);
618
- validateResolvedPack(defaultComposed, providers);
619
- const defaultGate = defaultComposed.spec.gates.find(gate => gate.id === 'integrity-ceiling');
620
- if (defaultGate === undefined) {
621
- throw new GateRegistryError(`shipped default floor ${INTEGRITY_CEILING_FLOOR_PACK_ID} has no integrity-ceiling gate`);
622
- }
623
- if (!expanded.some(floor => governsDefaultCeiling(floor, defaultGate))) {
624
- expanded.push({
625
- packId: defaultAuthored.metadata.id,
626
- gates: defaultComposed.spec.gates,
627
- params: { ...(defaultComposed.spec.parameters ?? {}) },
628
- });
629
- }
630
- return expanded;
631
- }
632
- /**
633
- * Resolves a binding gate's threshold parameter to its preregistered binding
634
- * value, so floor comparison is always value-against-minimum in the gate's
635
- * declared direction. A binding may parameterize a floor literal: the value,
636
- * not the spelling, must tighten.
637
- */
638
- function synthesizeFloorCandidate(gate, packId, parameters) {
639
- const materialize = (ref) => {
640
- const value = parameters[qualifyGateParameter(packId, ref.param)];
641
- if (value === undefined) {
642
- throw new GateRegistryError(`binding gate '${gate.id}' parameter '${ref.param}' has no resolved value`);
643
- }
644
- return value;
645
- };
646
- let candidate = gate;
647
- if (gate.threshold !== undefined && gate.threshold.param !== undefined) {
648
- const { op, param } = gate.threshold;
649
- candidate = { ...candidate, threshold: { op, value: materialize({ param }) } };
650
- }
651
- if (isParamRef(gate.minimumN))
652
- candidate = { ...candidate, minimumN: materialize(gate.minimumN) };
653
- const statistic = gate.statistic;
654
- if (statistic !== undefined && isParamRef(statistic.levelBps)) {
655
- candidate = { ...candidate, statistic: { ...statistic, levelBps: materialize(statistic.levelBps) } };
656
- }
657
- return candidate;
658
- }
659
- /**
660
- * Enforces project floors over already-resolved binding packs. Project policy
661
- * outranks addon and framework packs: a binding that omits a floor gate, or
662
- * whose gate loosens any floor gate's threshold, parameter value, scope or
663
- * outcome (via the shared per-kind `assertGateTightens` and the scope-superset
664
- * rule), is refused with a diagnostic naming the binding, the gate and the
665
- * floor pack. Every binding gate sharing a floor gate id must tighten it, so
666
- * a loosened duplicate cannot evade the floor.
667
- */
668
- export function enforceProjectFloors(binding, packs, parameters, expanded, ceilings) {
669
- // The '*' key is the project-wide default ceiling for every binding; a
670
- // per-study key overrides it for that study only (exact match — renames
671
- // never inherit). The star rule is re-checked here (C6): floors handed in
672
- // directly, bypassing `validateGatesConfig`/`resolveProjectFloors`, still
673
- // cannot carry starless or loosening per-study ceilings.
674
- try {
675
- assertCeilingDefaults(ceilings);
676
- }
677
- catch (error) {
678
- throw new GateRegistryError(error instanceof Error ? error.message : 'invalid project ceilings');
679
- }
680
- const configured = ceilings[binding.metadata.id] ?? ceilings['*'];
681
- if (!projectCeilingSatisfied(binding.spec.ceiling, configured)) {
682
- throw new GateRegistryError(`binding '${binding.metadata.id}' declares ceiling ${binding.spec.ceiling ?? 'PROMOTE'}`
683
- + ` below the project ceiling ${configured} for this study`);
684
- }
685
- const union = [];
686
- for (const pack of packs) {
687
- for (const gate of pack.resolved.spec.gates)
688
- union.push({ gate, packId: pack.authored.metadata.id });
689
- }
690
- for (const floor of expanded) {
691
- for (const floorGate of floor.gates) {
692
- const matches = union.filter(candidate => candidate.gate.id === floorGate.id);
693
- if (!matches.length) {
694
- throw new GateRegistryError(`binding '${binding.metadata.id}' omits project floor gate '${floorGate.id}'`
695
- + ` (floor pack '${floor.packId}'): every binding in this project must include and tighten it`);
696
- }
697
- for (const match of matches) {
698
- try {
699
- assertGateTightens(floorGate, synthesizeFloorCandidate(match.gate, match.packId, parameters), floor.params);
700
- }
701
- catch (error) {
702
- throw new GateRegistryError(`binding '${binding.metadata.id}' loosens project floor gate '${floorGate.id}'`
703
- + ` (floor pack '${floor.packId}'): ${error instanceof Error ? error.message : 'invalid tightening'}`);
704
- }
705
- }
706
- }
707
- }
708
- }
709
32
  /**
710
33
  * Standalone snapshot readers. These are module functions, not `GateRegistry`
711
34
  * methods, so a subclass override of `resolveBinding`/`resolvePack`/`getPack`
@@ -859,6 +182,11 @@ export function resolveGateBinding(doc, authoredById, providers, floors) {
859
182
  const references = new Set(binding.spec.references.map(reference => reference.name));
860
183
  for (const pack of packs) {
861
184
  for (const gate of pack.resolved.spec.gates) {
185
+ if (gate.kind === 'paired-difference' && gate.statistic?.marginBps !== undefined) {
186
+ const ref = gate.statistic.marginBps;
187
+ const margin = isParamRef(ref) ? parameters[qualifyGateParameter(pack.authored.metadata.id, ref.param)] : ref;
188
+ checkPairedMargin(gate.id, gate.statistic.mode ?? 'non-inferiority', margin);
189
+ }
862
190
  for (const slice of [...(gate.scope.slices ?? []), ...(gate.scope.except ?? [])]) {
863
191
  if (!slices.has(slice))
864
192
  throw new GateRegistryError(`gate ${gate.id} names slice '${slice}' outside the binding inventory`);