@aiwg/cli 2026.10.15 → 2026.10.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agentic/code/providers/pi/aiwg-bridge.ts +45 -7
- package/dist/src/artifacts/citation-parser.js +7 -6
- package/dist/src/artifacts/corpus-tools/citation-densify.js +4 -2
- package/dist/src/artifacts/corpus-tools/cli.js +8 -3
- package/dist/src/artifacts/corpus-tools/discovery-log.js +26 -24
- package/dist/src/artifacts/corpus-tools/expand-cli.js +3 -2
- package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
- package/dist/src/artifacts/corpus-tools/expand-queue.js +4 -1
- package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
- package/dist/src/artifacts/corpus-tools/expand-state.js +17 -1
- package/dist/src/artifacts/corpus-tools/expand.js +16 -17
- package/dist/src/artifacts/corpus-tools/induction-audit.js +7 -6
- package/dist/src/artifacts/corpus-tools/integrity-scan.js +3 -3
- package/dist/src/artifacts/corpus-tools/radar-shared.js +7 -9
- package/dist/src/artifacts/corpus-tools/ref-files.js +11 -0
- package/dist/src/artifacts/corpus-tools/resolve-cli.js +11 -3
- package/dist/src/artifacts/corpus-tools/resolve.js +42 -11
- package/dist/src/artifacts/corpus-tools/retrieval-lab.js +2 -1
- package/dist/src/artifacts/corpus-tools/sidecar-lint.js +4 -4
- package/dist/src/artifacts/corpus-tools/sidecar-repair.js +2 -1
- package/dist/src/artifacts/corpus-views/ref-parser.js +3 -17
- package/dist/src/artifacts/graph-query.js +5 -3
- package/dist/src/artifacts/hybrid-query.js +11 -2
- package/dist/src/artifacts/index-refresh.js +2 -0
- package/dist/src/artifacts/query-engine-corpus.js +217 -0
- package/dist/src/artifacts/query-engine-discover-output.js +229 -0
- package/dist/src/artifacts/query-engine-discover.js +285 -0
- package/dist/src/artifacts/query-engine-entries.js +188 -0
- package/dist/src/artifacts/query-engine-index.js +249 -0
- package/dist/src/artifacts/query-engine-matching.js +143 -0
- package/dist/src/artifacts/query-engine-options.js +2 -0
- package/dist/src/artifacts/query-engine-rules.js +202 -0
- package/dist/src/artifacts/query-engine-scoring.js +436 -0
- package/dist/src/artifacts/query-engine-show-card.js +194 -0
- package/dist/src/artifacts/query-engine-show-load.js +211 -0
- package/dist/src/artifacts/query-engine-show.js +300 -0
- package/dist/src/artifacts/query-engine.js +7 -2576
- package/dist/src/cli/handlers/daemon.js +17 -1
- package/dist/src/cli/handlers/doctor-args.js +10 -5
- package/dist/src/cli/handlers/doctor-help.js +4 -1
- package/dist/src/cli/handlers/index.js +2 -2
- package/dist/src/cli/handlers/mc-agents.js +111 -0
- package/dist/src/cli/handlers/mc-args.js +54 -0
- package/dist/src/cli/handlers/mc-control.js +227 -0
- package/dist/src/cli/handlers/mc-dispatch.js +203 -0
- package/dist/src/cli/handlers/mc-help.js +171 -0
- package/dist/src/cli/handlers/mc-run.js +343 -0
- package/dist/src/cli/handlers/mc-session.js +138 -0
- package/dist/src/cli/handlers/mc-status.js +258 -0
- package/dist/src/cli/handlers/mc-summary.js +43 -0
- package/dist/src/cli/handlers/mc-types.js +3 -0
- package/dist/src/cli/handlers/mc-worker.js +205 -0
- package/dist/src/cli/handlers/mc.js +12 -1665
- package/dist/src/cli/handlers/models-refusal.js +13 -0
- package/dist/src/cli/handlers/models-resource-sample.js +10 -0
- package/dist/src/cli/handlers/models.js +5 -3
- package/dist/src/cli/handlers/regenerate.js +8 -16
- package/dist/src/cli/handlers/remove-bundled.js +133 -0
- package/dist/src/cli/handlers/remove-options.js +31 -0
- package/dist/src/cli/handlers/remove-user.js +174 -0
- package/dist/src/cli/handlers/remove.js +201 -0
- package/dist/src/cli/handlers/sessions.js +7 -7
- package/dist/src/cli/handlers/subcommands-bundles.js +257 -0
- package/dist/src/cli/handlers/subcommands-discovery.js +165 -0
- package/dist/src/cli/handlers/subcommands-list-format.js +129 -0
- package/dist/src/cli/handlers/subcommands-list.js +250 -0
- package/dist/src/cli/handlers/subcommands-plugins.js +213 -0
- package/dist/src/cli/handlers/subcommands-quickref.js +58 -0
- package/dist/src/cli/handlers/subcommands-rlm.js +113 -0
- package/dist/src/cli/handlers/subcommands-services.js +66 -0
- package/dist/src/cli/handlers/subcommands-storage.js +259 -0
- package/dist/src/cli/handlers/subcommands.js +18 -1976
- package/dist/src/cli/handlers/update.js +157 -0
- package/dist/src/cli/handlers/use.js +14 -4
- package/dist/src/cli/handlers/utilities.js +3 -560
- package/dist/src/cli/handlers/utility-doctor.js +412 -0
- package/dist/src/cli/scope-resolver.js +1 -142
- package/dist/src/cli/services/deployment-verification.js +0 -1
- package/dist/src/cli/user-scope-mirror.js +173 -0
- package/dist/src/config/aiwg-config.js +2 -1
- package/dist/src/config/authorization-cli.js +7 -3
- package/dist/src/config/dispatch-host-thresholds.js +2 -0
- package/dist/src/config/dispatch-thresholds-runtime.d.mts +3 -2
- package/dist/src/config/dispatch-thresholds-runtime.mjs +21 -7
- package/dist/src/config/framework-aliases.js +12 -0
- package/dist/src/config/host-resource-report.d.mts +24 -0
- package/dist/src/config/host-resource-report.mjs +110 -0
- package/dist/src/config/host-resource-runtime.d.mts +37 -0
- package/dist/src/config/host-resource-runtime.mjs +296 -0
- package/dist/src/config/installed-selection.js +22 -0
- package/dist/src/config/worker-invocation-marker.d.mts +1 -0
- package/dist/src/config/worker-invocation-marker.mjs +2 -0
- package/dist/src/decision/ensemble-study/artifacts.js +7 -0
- package/dist/src/decision/ensemble-study/gate-policy.js +58 -0
- package/dist/src/decision/ensemble-study/gates.js +78 -0
- package/dist/src/decision/ensemble-study/protocol.js +18 -21
- package/dist/src/decision/ensemble-study/score.js +3 -2
- package/dist/src/decision/ensemble-study/statistics.js +7 -26
- package/dist/src/decision/heldout/access.js +74 -0
- package/dist/src/decision/heldout/calibration.js +1 -0
- package/dist/src/decision/heldout/collector.js +8 -1
- package/dist/src/decision/sensitivity/comparative-gates.js +135 -0
- package/dist/src/decision/sensitivity/comparative.js +18 -41
- package/dist/src/effects/verifiers/file.js +22 -5
- package/dist/src/extensions/claude-attribution.js +82 -0
- package/dist/src/extensions/claude-hooks-installer.js +133 -69
- package/dist/src/extensions/commands/daemon.js +109 -0
- package/dist/src/extensions/commands/definitions.js +5 -89
- package/dist/src/gates/evaluate.js +2 -358
- package/dist/src/gates/heldout.js +17 -7
- package/dist/src/gates/observations.js +363 -0
- package/dist/src/gates/pack-composition.js +270 -0
- package/dist/src/gates/pack-tightening.js +197 -0
- package/dist/src/gates/pack-validation.js +262 -0
- package/dist/src/gates/registry-error.js +5 -0
- package/dist/src/gates/registry.js +13 -685
- package/dist/src/lint/check-patterns.js +163 -0
- package/dist/src/lint/checks.js +268 -0
- package/dist/src/lint/reference-resolution.js +54 -0
- package/dist/src/lint/runner.js +3 -465
- package/dist/src/mcp/helpers.mjs +11 -1
- package/dist/src/mcp/json-document.mjs +29 -0
- package/dist/src/mcp/muse-native-config.mjs +1 -28
- package/dist/src/memory/project-registry.js +44 -4
- package/dist/src/mission-protocol/worker-review-gate.js +7 -0
- package/dist/src/models/worker-plan.js +36 -5
- package/dist/src/models/wrapper-deployment.js +25 -12
- package/dist/src/models/wrapper-enforcement.js +47 -7
- package/dist/src/network-analysis/research.js +2 -1
- package/dist/src/plugin/command-ownership.js +34 -0
- package/dist/src/plugin/skill-command-frontmatter.js +197 -0
- package/dist/src/plugin/skill-command-translator.js +6 -232
- package/dist/src/policy/hitl-check.js +14 -4
- package/dist/src/policy/hitl-consume.js +54 -0
- package/dist/src/policy/hitl-receipt.js +2 -0
- package/dist/src/policy/hitl.js +11 -3
- package/dist/src/providers/context-output-ownership.js +26 -0
- package/dist/src/providers/deployment-ownership.d.mts +19 -0
- package/dist/src/providers/deployment-ownership.mjs +226 -0
- package/dist/src/providers/deployment-removal.js +200 -0
- package/dist/src/providers/provider-context-contracts.js +162 -0
- package/dist/src/providers/provider-definition-schema.js +136 -0
- package/dist/src/providers/provider-definitions.js +8 -1298
- package/dist/src/providers/provider-seeds-1.js +360 -0
- package/dist/src/providers/provider-seeds-2.js +387 -0
- package/dist/src/providers/provider-seeds-3.js +262 -0
- package/dist/src/providers/removal-transaction.js +86 -0
- package/dist/src/providers/settings-document.d.mts +3 -0
- package/dist/src/providers/settings-document.mjs +47 -0
- package/dist/src/providers/settings-removal.js +190 -0
- package/dist/src/providers/skill-ownership.d.mts +19 -2
- package/dist/src/providers/skill-ownership.mjs +12 -4
- package/dist/src/providers/transformation-receipt-integration.js +15 -271
- package/dist/src/providers/transformation-receipt-outputs.js +99 -0
- package/dist/src/providers/transformation-receipt-source.js +194 -0
- package/dist/src/research/clients/arxiv.js +4 -30
- package/dist/src/research/clients/base.js +48 -22
- package/dist/src/research/identity.js +42 -4
- package/dist/src/research/query-cli.js +3 -87
- package/dist/src/research/query-options.js +87 -0
- package/dist/src/research/reference-match.js +105 -24
- package/dist/src/research/services/acquisition.js +9 -7
- package/dist/src/research/services/citation.js +5 -2
- package/dist/src/research/types.js +2 -0
- package/dist/src/rules/activation.mjs +15 -6
- package/dist/src/rules/core-startup.mjs +2 -2
- package/dist/src/serve/mission-conductor.js +52 -4
- package/dist/src/serve/mission-summary.js +56 -0
- package/dist/src/sessions/adapters/grok-build-native.js +23 -0
- package/dist/src/sessions/adapters/grok-build.js +14 -7
- package/dist/src/sessions/workspace-discovery.js +5 -5
- package/dist/src/smiths/context-pipeline/aiwg-md.js +39 -46
- package/dist/src/smiths/context-pipeline/claude-hook.js +1 -1
- package/dist/src/smiths/context-pipeline/code-regions.js +28 -0
- package/dist/src/smiths/context-pipeline/context-file-safety.js +3 -0
- package/dist/src/smiths/context-pipeline/finalization.js +81 -51
- package/dist/src/smiths/context-pipeline/generator.js +10 -11
- package/dist/src/smiths/context-pipeline/managed-hook.js +1 -1
- package/dist/src/smiths/context-pipeline/muse-bridge.js +2 -2
- package/dist/src/smiths/context-pipeline/provider-policy.js +6 -12
- package/dist/src/smiths/context-pipeline/workspace-audit.js +299 -0
- package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +148 -0
- package/dist/src/smiths/context-pipeline/workspace-context-shared.js +111 -0
- package/dist/src/smiths/context-pipeline/workspace-context.js +8 -1373
- package/dist/src/smiths/context-pipeline/workspace-doctor.js +192 -0
- package/dist/src/smiths/context-pipeline/workspace-document.js +201 -0
- package/dist/src/smiths/context-pipeline/workspace-extraction.js +205 -0
- package/dist/src/smiths/context-pipeline/workspace-migration.js +250 -0
- package/dist/src/smiths/hook-bridge/index.js +9 -0
- package/package.json +13 -1
- package/schemas/decision/D17StudyProtocol.v1.schema.json +39 -67
- package/schemas/decision/D29Study.v10.schema.json +65 -0
- package/tools/agents/deploy-agents.mjs +24 -675
- package/tools/agents/deploy-command-mirrors.mjs +229 -0
- package/tools/agents/deploy-options.mjs +258 -0
- package/tools/agents/deploy-support.mjs +165 -0
- package/tools/agents/providers/base-addon-discovery.mjs +139 -0
- package/tools/agents/providers/base-behaviors.mjs +168 -0
- package/tools/agents/providers/base-context.mjs +205 -0
- package/tools/agents/providers/base-contract.mjs +116 -0
- package/tools/agents/providers/base-files.mjs +252 -0
- package/tools/agents/providers/base-flat-artifacts.mjs +233 -0
- package/tools/agents/providers/base-framework-discovery.mjs +309 -0
- package/tools/agents/providers/base-frontmatter.mjs +196 -0
- package/tools/agents/providers/base-inventory.mjs +269 -0
- package/tools/agents/providers/base-kernel-skills.mjs +354 -0
- package/tools/agents/providers/base-migrations.mjs +258 -0
- package/tools/agents/providers/base-models-platforms.mjs +273 -0
- package/tools/agents/providers/base-rule-index.mjs +450 -0
- package/tools/agents/providers/base-runtime.mjs +60 -0
- package/tools/agents/providers/base-skill-copy.mjs +273 -0
- package/tools/agents/providers/base.mjs +16 -3364
- package/tools/agents/providers/claude-post-deploy.mjs +20 -1
- package/tools/agents/providers/context-write.mjs +4 -0
- package/tools/agents/providers/hermes-startup.mjs +0 -5
- package/tools/agents/providers/muse.mjs +7 -2
- package/tools/agents/providers/pi.mjs +3 -1
- package/tools/agents/providers/rule-delivery-surface.mjs +359 -0
- package/tools/agents/providers/rule-delivery.mjs +20 -1144
- package/tools/agents/providers/rule-deployment-commit.mjs +148 -0
- package/tools/agents/providers/rule-deployment-prepare.mjs +461 -0
- package/tools/agents/providers/rule-deployment-state.mjs +300 -0
- package/tools/agents/providers/rule-projection.mjs +3 -0
- package/tools/daemon/README.md +511 -0
- package/tools/daemon/adapters/base-adapter.mjs +84 -0
- package/tools/daemon/adapters/webhook-adapter.mjs +107 -0
- package/tools/daemon/agent-supervisor.mjs +299 -0
- package/tools/daemon/automation-engine.mjs +310 -0
- package/tools/daemon/autonomous-engine.mjs +346 -0
- package/tools/daemon/behavior-loader.mjs +316 -0
- package/tools/daemon/behavior.mjs +174 -0
- package/tools/daemon/concierge/intent-router.mjs +401 -0
- package/tools/daemon/concierge/orchestrator.mjs +272 -0
- package/tools/daemon/concierge/response-translator.mjs +462 -0
- package/tools/daemon/config.mjs +377 -0
- package/tools/daemon/cron-scheduler.mjs +178 -0
- package/tools/daemon/daemon-main.mjs +1169 -0
- package/tools/daemon/event-router.mjs +84 -0
- package/tools/daemon/file-watcher.mjs +383 -0
- package/tools/daemon/index.mjs +1181 -0
- package/tools/daemon/init-profile.mjs +124 -0
- package/tools/daemon/ipc-client.mjs +197 -0
- package/tools/daemon/ipc-ops-handlers.mjs +236 -0
- package/tools/daemon/ipc-server.mjs +275 -0
- package/tools/daemon/maintenance-owner.mjs +79 -0
- package/tools/daemon/memory-manager.mjs +386 -0
- package/tools/daemon/message-router.mjs +194 -0
- package/tools/daemon/provider-watcher.mjs +585 -0
- package/tools/daemon/pty-adapter.mjs +420 -0
- package/tools/daemon/repl-chat.mjs +359 -0
- package/tools/daemon/resource-bounds.mjs +88 -0
- package/tools/daemon/sandbox-transport.mjs +295 -0
- package/tools/daemon/scheduled-task-runner.mjs +535 -0
- package/tools/daemon/task-store.mjs +450 -0
- package/tools/daemon/tmux-manager.mjs +324 -0
- package/tools/daemon/update-policy.mjs +517 -0
- package/tools/daemon/web-server.mjs +399 -0
- package/tools/daemon/web-ui/index.html +585 -0
- package/tools/daemon/web-ui/styles.css +156 -0
- package/tools/docker/Dockerfile.daemon +35 -0
- package/tools/docker/container-manager.mjs +291 -0
- package/tools/messaging/adapter-registry.mjs +230 -0
- package/tools/messaging/adapters/base.mjs +326 -0
- package/tools/messaging/adapters/discord.mjs +462 -0
- package/tools/messaging/adapters/slack.mjs +402 -0
- package/tools/messaging/adapters/telegram.mjs +552 -0
- package/tools/messaging/chat-handler.mjs +297 -0
- package/tools/messaging/command-router.mjs +269 -0
- package/tools/messaging/event-bus.mjs +199 -0
- package/tools/messaging/index.mjs +456 -0
- package/tools/messaging/message-formatter.mjs +320 -0
- package/tools/messaging/room-manager.mjs +277 -0
- package/tools/messaging/types.mjs +94 -0
- package/tools/providers/pi-bridge-policy.mjs +4 -0
- package/tools/ralph-external/daemon-supervisor.mjs +394 -0
- package/tools/ralph-external/lib/behavior-registry.mjs +270 -0
- package/tools/ralph-external/lib/worker-invocation-processes.mjs +7 -4
- package/tools/ralph-external/lib/worker-isolation-args.mjs +217 -0
- package/tools/ralph-external/lib/worker-isolation-status.mjs +19 -0
- package/tools/ralph-external/lib/worker-isolation.mjs +11 -237
- package/tools/ralph-external/lib/worker-overflow.mjs +10 -2
- package/tools/ralph-external/lib/worker-termination.mjs +15 -8
- package/tools/ralph-external/lib/worker-unit-cleanup.mjs +72 -0
- package/tools/ralph-external/session-launcher.mjs +7 -7
- package/tools/scripts/resource-preflight.mjs +3 -242
- package/vscode-extension/schemas/aiwg.config.v1.json +2 -4
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
// Pool metric observations and evaluate each gate against resolved binding parameters.
|
|
2
|
+
import { evaluatePredicate } from '../decision/predicates.js';
|
|
3
|
+
import { evaluateUpstreamCeiling } from './upstream-ceiling.js';
|
|
4
|
+
import { clopperPearsonInterval } from './stats/clopper-pearson.js';
|
|
5
|
+
import { wilsonScoreInterval } from './stats/binomial.js';
|
|
6
|
+
import { pairedMeanDifferenceBootstrap } from './stats/bootstrap.js';
|
|
7
|
+
import { PairedDifferenceError } from './stats/error.js';
|
|
8
|
+
import { pairedBinaryDifferenceInterval, pairedNonInferiority } from './stats/paired.js';
|
|
9
|
+
import { qualifyGateParameter } from './types.js';
|
|
10
|
+
const isRecord = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
11
|
+
function supportOf(observation) {
|
|
12
|
+
const n = observation?.n;
|
|
13
|
+
return typeof n === 'number' && Number.isSafeInteger(n) && n >= 0 ? n : null;
|
|
14
|
+
}
|
|
15
|
+
function poolObservations(kind, observations) {
|
|
16
|
+
if (!observations.length)
|
|
17
|
+
return null;
|
|
18
|
+
if (kind === 'proportion') {
|
|
19
|
+
let n = 0;
|
|
20
|
+
let events = 0;
|
|
21
|
+
for (const observation of observations) {
|
|
22
|
+
if (supportOf(observation) === null || typeof observation.events !== 'number'
|
|
23
|
+
|| !Number.isSafeInteger(observation.events) || observation.events < 0)
|
|
24
|
+
return null;
|
|
25
|
+
if (observation.events > observation.n)
|
|
26
|
+
return null;
|
|
27
|
+
n += observation.n;
|
|
28
|
+
events += observation.events;
|
|
29
|
+
}
|
|
30
|
+
return { n, events };
|
|
31
|
+
}
|
|
32
|
+
if (kind === 'paired') {
|
|
33
|
+
const total = { both: 0, candidateOnly: 0, baselineOnly: 0, neither: 0 };
|
|
34
|
+
let n = 0;
|
|
35
|
+
for (const observation of observations) {
|
|
36
|
+
const cells = isRecord(observation?.paired)
|
|
37
|
+
? ['both', 'candidateOnly', 'baselineOnly', 'neither'].map(key => observation.paired[key])
|
|
38
|
+
: [];
|
|
39
|
+
if (supportOf(observation) === null || cells.length !== 4
|
|
40
|
+
|| cells.some(cell => !Number.isSafeInteger(cell) || cell < 0)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const [both, candidateOnly, baselineOnly, neither] = cells;
|
|
44
|
+
// The counted support must equal the cell sums; a disagreement is unknown, fail closed.
|
|
45
|
+
if (observation.n !== both + candidateOnly + baselineOnly + neither)
|
|
46
|
+
return null;
|
|
47
|
+
n += observation.n;
|
|
48
|
+
total.both += both;
|
|
49
|
+
total.candidateOnly += candidateOnly;
|
|
50
|
+
total.baselineOnly += baselineOnly;
|
|
51
|
+
total.neither += neither;
|
|
52
|
+
}
|
|
53
|
+
return { n, paired: total };
|
|
54
|
+
}
|
|
55
|
+
if (kind === 'scalar') {
|
|
56
|
+
let n = 0;
|
|
57
|
+
let weighted = 0;
|
|
58
|
+
for (const observation of observations) {
|
|
59
|
+
if (supportOf(observation) === null || typeof observation.value !== 'number' || !Number.isFinite(observation.value))
|
|
60
|
+
return null;
|
|
61
|
+
n += observation.n;
|
|
62
|
+
weighted += observation.n * observation.value;
|
|
63
|
+
}
|
|
64
|
+
return n === 0 ? null : { n, value: weighted / n };
|
|
65
|
+
}
|
|
66
|
+
if (kind === 'differences') {
|
|
67
|
+
const differences = [];
|
|
68
|
+
let n = 0;
|
|
69
|
+
for (const observation of observations) {
|
|
70
|
+
if (supportOf(observation) === null || !Array.isArray(observation.differences)
|
|
71
|
+
|| observation.differences.some(value => typeof value !== 'number' || !Number.isFinite(value)))
|
|
72
|
+
return null;
|
|
73
|
+
if (observation.n !== observation.differences.length)
|
|
74
|
+
return null;
|
|
75
|
+
n += observation.n;
|
|
76
|
+
differences.push(...observation.differences);
|
|
77
|
+
}
|
|
78
|
+
return { n, differences };
|
|
79
|
+
}
|
|
80
|
+
if (kind === 'evidence') {
|
|
81
|
+
const available = observations.flatMap(observation => observation?.available ?? []);
|
|
82
|
+
if (observations.some(observation => !Array.isArray(observation?.available)))
|
|
83
|
+
return null;
|
|
84
|
+
return { available };
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
export function seriesFor(gate, pack, metrics) {
|
|
89
|
+
if (gate.metric === undefined)
|
|
90
|
+
return null;
|
|
91
|
+
const declared = pack.resolved.spec.metrics[gate.metric.name];
|
|
92
|
+
if (declared === undefined || declared.provider !== gate.metric.provider)
|
|
93
|
+
return null;
|
|
94
|
+
return metrics.providers[gate.metric.provider]?.metrics[gate.metric.name] ?? null;
|
|
95
|
+
}
|
|
96
|
+
export function metricKindOf(gate, pack) {
|
|
97
|
+
if (gate.metric === undefined)
|
|
98
|
+
return '';
|
|
99
|
+
return pack.resolved.spec.metrics[gate.metric.name]?.kind ?? '';
|
|
100
|
+
}
|
|
101
|
+
export function expandScope(gate, kind, series, slices) {
|
|
102
|
+
const mode = gate.scope.mode;
|
|
103
|
+
if (mode === 'all')
|
|
104
|
+
return [{ slice: null, observation: series?.pooled ?? null }];
|
|
105
|
+
if (mode === 'pooled') {
|
|
106
|
+
const observations = (gate.scope.slices ?? []).map(slice => series?.bySlice[slice] ?? null);
|
|
107
|
+
if (observations.some(observation => observation === null)) {
|
|
108
|
+
return [{ slice: null, observation: null }];
|
|
109
|
+
}
|
|
110
|
+
return [{ slice: null, observation: poolObservations(kind, observations) }];
|
|
111
|
+
}
|
|
112
|
+
const excluded = new Set(gate.scope.except ?? []);
|
|
113
|
+
const names = mode === 'each' ? slices.filter(slice => !excluded.has(slice)) : (gate.scope.slices ?? []);
|
|
114
|
+
return names.map(slice => ({ slice, observation: series?.bySlice[slice] ?? null }));
|
|
115
|
+
}
|
|
116
|
+
function pairedCells(observation) {
|
|
117
|
+
if (!isRecord(observation?.paired))
|
|
118
|
+
return null;
|
|
119
|
+
const cells = ['both', 'candidateOnly', 'baselineOnly', 'neither'].map(key => observation.paired[key]);
|
|
120
|
+
if (cells.some(cell => !Number.isSafeInteger(cell) || cell < 0))
|
|
121
|
+
return null;
|
|
122
|
+
const [both, candidateOnly, baselineOnly, neither] = cells;
|
|
123
|
+
return { both, candidateOnly, baselineOnly, neither };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Resolve a literal-or-parameter gate number against the binding's
|
|
127
|
+
* preregistered values. Null when a `{param}` reference has no resolved
|
|
128
|
+
* value: callers hold as `parameter-missing`, never promote.
|
|
129
|
+
*/
|
|
130
|
+
function resolveGateNumber(value, packId, parameters) {
|
|
131
|
+
if (value === undefined)
|
|
132
|
+
return null;
|
|
133
|
+
if (typeof value === 'number')
|
|
134
|
+
return value;
|
|
135
|
+
if (value !== null && typeof value === 'object' && typeof value.param === 'string') {
|
|
136
|
+
const resolved = parameters[qualifyGateParameter(packId, value.param)];
|
|
137
|
+
return typeof resolved === 'number' ? resolved : null;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
export function evaluateObservation(gate, observation, context) {
|
|
142
|
+
const { packId, parameters, metrics, upstream, nowMs, slice, slices } = context;
|
|
143
|
+
const base = { slice, n: supportOf(observation) };
|
|
144
|
+
const minimumN = gate.minimumN === undefined ? 0 : resolveGateNumber(gate.minimumN, packId, parameters);
|
|
145
|
+
// Evidence gates observe attestations, not counted support; upstream and predicate
|
|
146
|
+
// gates read the ceiling and the whole document. None of them take an n.
|
|
147
|
+
if (gate.kind !== 'upstream-ceiling' && gate.kind !== 'predicate' && gate.kind !== 'evidence') {
|
|
148
|
+
if (observation === null || observation === undefined)
|
|
149
|
+
return { ...base, status: 'insufficient', reason: 'metric-missing' };
|
|
150
|
+
if (base.n === null)
|
|
151
|
+
return { ...base, status: 'insufficient', reason: 'support-unknown' };
|
|
152
|
+
if (minimumN === null)
|
|
153
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing' };
|
|
154
|
+
if (base.n < minimumN)
|
|
155
|
+
return { ...base, status: 'insufficient', reason: 'below-minimum-n' };
|
|
156
|
+
}
|
|
157
|
+
const thresholdValue = () => {
|
|
158
|
+
if (gate.threshold?.value !== undefined)
|
|
159
|
+
return gate.threshold.value;
|
|
160
|
+
if (gate.threshold?.param !== undefined) {
|
|
161
|
+
const value = parameters[qualifyGateParameter(packId, gate.threshold.param)];
|
|
162
|
+
return typeof value === 'number' ? value : null;
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
};
|
|
166
|
+
switch (gate.kind) {
|
|
167
|
+
case 'minimum-n': {
|
|
168
|
+
const target = gate.minimumN === undefined ? null : resolveGateNumber(gate.minimumN, packId, parameters);
|
|
169
|
+
if (target === null) {
|
|
170
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing',
|
|
171
|
+
statistic: { method: 'support-count' } };
|
|
172
|
+
}
|
|
173
|
+
const passed = base.n >= target;
|
|
174
|
+
return { ...base, status: passed ? 'pass' : 'insufficient', reason: passed ? 'support-met' : 'below-minimum-n',
|
|
175
|
+
statistic: { method: 'support-count' } };
|
|
176
|
+
}
|
|
177
|
+
case 'interval-bound': {
|
|
178
|
+
const events = observation?.events;
|
|
179
|
+
if (typeof events !== 'number' || !Number.isSafeInteger(events) || events < 0) {
|
|
180
|
+
return { ...base, status: 'insufficient', reason: 'events-unknown', events: null };
|
|
181
|
+
}
|
|
182
|
+
if (events > base.n)
|
|
183
|
+
return { ...base, status: 'insufficient', reason: 'events-exceed-n', events };
|
|
184
|
+
const target = thresholdValue();
|
|
185
|
+
if (target === null)
|
|
186
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing', events };
|
|
187
|
+
const method = gate.statistic?.method ?? 'wilson';
|
|
188
|
+
const levelBps = resolveGateNumber(gate.statistic?.levelBps, packId, parameters);
|
|
189
|
+
if (levelBps === null)
|
|
190
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing', events };
|
|
191
|
+
let lower;
|
|
192
|
+
let upper;
|
|
193
|
+
try {
|
|
194
|
+
if (method === 'clopper-pearson') {
|
|
195
|
+
({ lower, upper } = clopperPearsonInterval({ events, n: base.n, levelBps }));
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
[lower, upper] = wilsonScoreInterval({ events, n: base.n, levelBps });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
if (error instanceof PairedDifferenceError) {
|
|
203
|
+
return { ...base, status: 'insufficient', reason: 'statistic-invalid', events };
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
const lowerBps = Math.max(0, Math.floor(lower * 10000));
|
|
208
|
+
const upperBps = Math.min(10000, Math.ceil(upper * 10000));
|
|
209
|
+
const estimateBps = Math.round(events / base.n * 10000);
|
|
210
|
+
const passed = gate.statistic?.bound === 'upper' ? upperBps <= target : lowerBps >= target;
|
|
211
|
+
return { ...base, status: passed ? 'pass' : 'fail', reason: passed ? 'bound-within-threshold' : 'bound-breaches-threshold',
|
|
212
|
+
events, statistic: { method, levelBps, lowerBps, upperBps, estimateBps },
|
|
213
|
+
threshold: { op: gate.threshold?.op, ...(gate.threshold?.value !== undefined
|
|
214
|
+
? { value: gate.threshold.value } : { param: gate.threshold?.param }), resolved: target } };
|
|
215
|
+
}
|
|
216
|
+
case 'paired-difference': {
|
|
217
|
+
const cells = pairedCells(observation);
|
|
218
|
+
if (cells === null)
|
|
219
|
+
return { ...base, status: 'insufficient', reason: 'paired-unknown' };
|
|
220
|
+
if (base.n !== cells.both + cells.candidateOnly + cells.baselineOnly + cells.neither) {
|
|
221
|
+
return { ...base, status: 'insufficient', reason: 'observation-mismatch' };
|
|
222
|
+
}
|
|
223
|
+
const method = gate.statistic?.method ?? 'newcombe';
|
|
224
|
+
const levelBps = resolveGateNumber(gate.statistic?.levelBps, packId, parameters);
|
|
225
|
+
if (levelBps === null)
|
|
226
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing' };
|
|
227
|
+
let interval;
|
|
228
|
+
try {
|
|
229
|
+
interval = pairedBinaryDifferenceInterval({ counts: cells, levelBps, method: method === 'tango' ? 'tango' : 'newcombe-10' });
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
if (error instanceof PairedDifferenceError)
|
|
233
|
+
return { ...base, status: 'insufficient', reason: 'paired-invalid' };
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
const mode = gate.statistic?.mode ?? 'non-inferiority';
|
|
237
|
+
const margin = resolveGateNumber(gate.statistic?.marginBps ?? 0, packId, parameters);
|
|
238
|
+
if (margin === null)
|
|
239
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing' };
|
|
240
|
+
const statistic = { method, levelBps, lowerBps: interval.lowerBps, upperBps: interval.upperBps,
|
|
241
|
+
estimateBps: interval.estimateBps };
|
|
242
|
+
const threshold = { op: 'gte', resolved: margin };
|
|
243
|
+
if (mode === 'non-inferiority') {
|
|
244
|
+
const verdict = pairedNonInferiority({ interval, marginBps: margin });
|
|
245
|
+
if (verdict.decision === 'insufficient')
|
|
246
|
+
return { ...base, status: 'insufficient', reason: 'paired-invalid', statistic, threshold };
|
|
247
|
+
const passed = verdict.decision === 'non-inferior';
|
|
248
|
+
return { ...base, status: passed ? 'pass' : 'fail',
|
|
249
|
+
reason: passed ? 'lower-bound-at-or-above-margin' : 'lower-bound-below-margin', statistic, threshold };
|
|
250
|
+
}
|
|
251
|
+
const passed = interval.lowerBps > margin;
|
|
252
|
+
return { ...base, status: passed ? 'pass' : 'fail',
|
|
253
|
+
reason: passed ? 'lower-bound-above-margin' : 'lower-bound-at-or-below-margin', statistic, threshold };
|
|
254
|
+
}
|
|
255
|
+
case 'bootstrap-bound': {
|
|
256
|
+
const differences = observation?.differences;
|
|
257
|
+
if (!Array.isArray(differences))
|
|
258
|
+
return { ...base, status: 'insufficient', reason: 'differences-unknown' };
|
|
259
|
+
if (differences.length !== base.n)
|
|
260
|
+
return { ...base, status: 'insufficient', reason: 'observation-mismatch' };
|
|
261
|
+
const target = thresholdValue();
|
|
262
|
+
if (target === null)
|
|
263
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing' };
|
|
264
|
+
const levelBps = resolveGateNumber(gate.statistic?.levelBps, packId, parameters);
|
|
265
|
+
if (levelBps === null)
|
|
266
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing' };
|
|
267
|
+
let interval;
|
|
268
|
+
try {
|
|
269
|
+
interval = pairedMeanDifferenceBootstrap({ differences, levelBps,
|
|
270
|
+
seed: gate.statistic?.seed, resamples: gate.statistic?.resamples,
|
|
271
|
+
bounds: gate.statistic?.bounds });
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
if (error instanceof PairedDifferenceError)
|
|
275
|
+
return { ...base, status: 'insufficient', reason: 'statistic-invalid' };
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
const passed = gate.statistic?.bound === 'upper' ? interval.upperBps <= target : interval.lowerBps >= target;
|
|
279
|
+
return { ...base, status: passed ? 'pass' : 'fail', reason: passed ? 'bound-within-threshold' : 'bound-breaches-threshold',
|
|
280
|
+
statistic: { method: 'bootstrap', levelBps, lowerBps: interval.lowerBps,
|
|
281
|
+
upperBps: interval.upperBps, estimateBps: interval.estimateBps },
|
|
282
|
+
threshold: { op: gate.threshold?.op, ...(gate.threshold?.value !== undefined
|
|
283
|
+
? { value: gate.threshold.value } : { param: gate.threshold?.param }), resolved: target } };
|
|
284
|
+
}
|
|
285
|
+
case 'count-max':
|
|
286
|
+
case 'count-min': {
|
|
287
|
+
const events = observation?.events;
|
|
288
|
+
if (typeof events !== 'number' || !Number.isSafeInteger(events) || events < 0) {
|
|
289
|
+
return { ...base, status: 'insufficient', reason: 'events-unknown', events: null };
|
|
290
|
+
}
|
|
291
|
+
if (events > base.n)
|
|
292
|
+
return { ...base, status: 'insufficient', reason: 'events-exceed-n', events };
|
|
293
|
+
const target = thresholdValue();
|
|
294
|
+
if (target === null)
|
|
295
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing', events };
|
|
296
|
+
const passed = gate.kind === 'count-max' ? events <= target : events >= target;
|
|
297
|
+
return { ...base, status: passed ? 'pass' : 'fail', reason: passed ? 'count-within-threshold' : 'count-breaches-threshold',
|
|
298
|
+
events, statistic: { method: 'exact-count' },
|
|
299
|
+
threshold: { op: gate.threshold?.op, ...(gate.threshold?.value !== undefined
|
|
300
|
+
? { value: gate.threshold.value } : { param: gate.threshold?.param }), resolved: target } };
|
|
301
|
+
}
|
|
302
|
+
case 'value-threshold': {
|
|
303
|
+
const value = observation?.value;
|
|
304
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
305
|
+
return { ...base, status: 'insufficient', reason: 'value-unknown' };
|
|
306
|
+
}
|
|
307
|
+
const target = thresholdValue();
|
|
308
|
+
if (target === null)
|
|
309
|
+
return { ...base, status: 'insufficient', reason: 'parameter-missing' };
|
|
310
|
+
const passed = gate.threshold?.op === 'lte' ? value <= target : value >= target;
|
|
311
|
+
return { ...base, status: passed ? 'pass' : 'fail', reason: passed ? 'value-within-threshold' : 'value-breaches-threshold',
|
|
312
|
+
statistic: { method: 'exact-value' },
|
|
313
|
+
threshold: { op: gate.threshold?.op, ...(gate.threshold?.value !== undefined
|
|
314
|
+
? { value: gate.threshold.value } : { param: gate.threshold?.param }), resolved: target } };
|
|
315
|
+
}
|
|
316
|
+
case 'evidence': {
|
|
317
|
+
const available = observation?.available;
|
|
318
|
+
if (!Array.isArray(available))
|
|
319
|
+
return { ...base, status: 'insufficient', reason: 'evidence-unknown' };
|
|
320
|
+
for (const id of gate.evidence?.required ?? []) {
|
|
321
|
+
const entry = available.find(candidate => candidate?.id === id);
|
|
322
|
+
if (entry === undefined)
|
|
323
|
+
return { ...base, status: 'insufficient', reason: 'evidence-missing', statistic: { method: 'evidence-present' } };
|
|
324
|
+
if (entry.passed !== true) {
|
|
325
|
+
return { ...base, status: 'fail', reason: 'evidence-failed', statistic: { method: 'evidence-present' } };
|
|
326
|
+
}
|
|
327
|
+
if (entry.expiresAt !== undefined && entry.expiresAt !== null) {
|
|
328
|
+
const expiry = Date.parse(entry.expiresAt);
|
|
329
|
+
if (!Number.isFinite(expiry) || expiry <= nowMs) {
|
|
330
|
+
return { ...base, status: 'fail', reason: 'evidence-expired', statistic: { method: 'evidence-present' } };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return { ...base, status: 'pass', reason: 'evidence-present', statistic: { method: 'evidence-present' } };
|
|
335
|
+
}
|
|
336
|
+
case 'predicate': {
|
|
337
|
+
let value = 'unknown';
|
|
338
|
+
try {
|
|
339
|
+
value = evaluatePredicate(gate.predicate, { metrics, parameters, slices }, {});
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
value = 'unknown';
|
|
343
|
+
}
|
|
344
|
+
if (value === true)
|
|
345
|
+
return { ...base, status: 'pass', reason: 'predicate-true', statistic: { method: 'predicate' } };
|
|
346
|
+
if (value === false)
|
|
347
|
+
return { ...base, status: 'fail', reason: 'predicate-false', statistic: { method: 'predicate' } };
|
|
348
|
+
return { ...base, status: 'insufficient', reason: 'predicate-unknown', statistic: { method: 'predicate' } };
|
|
349
|
+
}
|
|
350
|
+
case 'upstream-ceiling': {
|
|
351
|
+
if (upstream === null)
|
|
352
|
+
return { ...base, status: 'insufficient', reason: 'upstream-missing',
|
|
353
|
+
statistic: { method: 'upstream-integrity' } };
|
|
354
|
+
const { problems, compromised } = evaluateUpstreamCeiling(upstream.metadata);
|
|
355
|
+
if (!problems.length) {
|
|
356
|
+
return { ...base, status: 'pass', reason: 'upstream-clean', statistic: { method: 'upstream-integrity' } };
|
|
357
|
+
}
|
|
358
|
+
return { ...base, status: 'fail', reason: compromised ? 'upstream-compromised' : 'upstream-allowlist',
|
|
359
|
+
statistic: { method: 'upstream-integrity' } };
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
//# sourceMappingURL=observations.js.map
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// Compose inherited packs and enforce their project floor minima.
|
|
2
|
+
import { artifactDigest } from '../decision/validate.js';
|
|
3
|
+
import { GateRegistryError } from './registry-error.js';
|
|
4
|
+
import { isParamRef, validateResolvedPack } from './pack-validation.js';
|
|
5
|
+
import { assertGateTightens } from './pack-tightening.js';
|
|
6
|
+
import { validateGateDocument } from './schema.js';
|
|
7
|
+
import { qualifyGateParameter } from './types.js';
|
|
8
|
+
import { INTEGRITY_CEILING_FLOOR_PACK_ID, assertCeilingDefaults, projectCeilingSatisfied } from './floors.js';
|
|
9
|
+
const fail = (message) => { throw new GateRegistryError(message); };
|
|
10
|
+
const pinLabel = (pin) => `${pin.id}@${pin.version}`;
|
|
11
|
+
/**
|
|
12
|
+
* Pure `extends` composition: merges a child's declarations over its already
|
|
13
|
+
* resolved parent. No registry state is read, so a `GateRegistry` subclass
|
|
14
|
+
* cannot change tightening semantics by overriding an instance method. The
|
|
15
|
+
* evaluator calls this (via `composeGatePack`/`resolveGateBinding`) instead of
|
|
16
|
+
* any overridable `GateRegistry` method for security-relevant resolution.
|
|
17
|
+
*/
|
|
18
|
+
export function applyGateExtends(parentResolved, child) {
|
|
19
|
+
const mergedParams = { ...(parentResolved.spec.parameters ?? {}) };
|
|
20
|
+
for (const [name, parameter] of Object.entries(child.spec.parameters ?? {})) {
|
|
21
|
+
const before = mergedParams[name];
|
|
22
|
+
if (before === undefined) {
|
|
23
|
+
mergedParams[name] = parameter;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (before.type !== parameter.type || before.direction !== parameter.direction) {
|
|
27
|
+
throw new GateRegistryError(`pack ${child.metadata.id} may not change parameter ${name} type or direction`);
|
|
28
|
+
}
|
|
29
|
+
if (before.default === undefined && parameter.default !== undefined) {
|
|
30
|
+
throw new GateRegistryError(`pack ${child.metadata.id} may not add a default to required parameter ${name}`);
|
|
31
|
+
}
|
|
32
|
+
if (before.default !== undefined && parameter.default === undefined) {
|
|
33
|
+
mergedParams[name] = parameter;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (before.default !== undefined && parameter.default !== undefined) {
|
|
37
|
+
const tightens = parameter.direction === 'lower-is-stricter'
|
|
38
|
+
? parameter.default <= before.default : parameter.default >= before.default;
|
|
39
|
+
if (!tightens)
|
|
40
|
+
throw new GateRegistryError(`pack ${child.metadata.id} parameter ${name} default loosens`);
|
|
41
|
+
mergedParams[name] = parameter;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const parentGates = new Map(parentResolved.spec.gates.map(gate => [gate.id, gate]));
|
|
45
|
+
const merged = [];
|
|
46
|
+
const seen = new Set();
|
|
47
|
+
for (const gate of parentResolved.spec.gates) {
|
|
48
|
+
const override = (child.spec.gates ?? []).find(candidate => candidate.id === gate.id);
|
|
49
|
+
if (override === undefined) {
|
|
50
|
+
// Removing a non-floor gate is allowed; floor gates pin the minimum.
|
|
51
|
+
if (gate.floor === true)
|
|
52
|
+
throw new GateRegistryError(`pack ${child.metadata.id} may not remove floor gate ${gate.id}`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
seen.add(gate.id);
|
|
56
|
+
assertGateTightens(gate, override, mergedParams);
|
|
57
|
+
merged.push(override);
|
|
58
|
+
}
|
|
59
|
+
for (const gate of child.spec.gates ?? []) {
|
|
60
|
+
if (!seen.has(gate.id) && !parentGates.has(gate.id))
|
|
61
|
+
merged.push(gate);
|
|
62
|
+
else if (!seen.has(gate.id))
|
|
63
|
+
fail(`pack ${child.metadata.id} duplicates gate ${gate.id}`);
|
|
64
|
+
}
|
|
65
|
+
const mergedMetrics = { ...parentResolved.spec.metrics };
|
|
66
|
+
for (const [name, metric] of Object.entries(child.spec.metrics ?? {})) {
|
|
67
|
+
const before = mergedMetrics[name];
|
|
68
|
+
if (before !== undefined && (before.provider !== metric.provider || before.kind !== metric.kind)) {
|
|
69
|
+
throw new GateRegistryError(`pack ${child.metadata.id} may not change inherited metric ${name}`);
|
|
70
|
+
}
|
|
71
|
+
mergedMetrics[name] = metric;
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
apiVersion: child.apiVersion, kind: child.kind, metadata: child.metadata,
|
|
75
|
+
spec: {
|
|
76
|
+
extends: child.spec.extends, parameters: mergedParams, metrics: mergedMetrics, gates: merged,
|
|
77
|
+
// Omit `combine` when the child leaves it unset: a present-but-undefined
|
|
78
|
+
// key is not JSON and would poison the composed-pack digest.
|
|
79
|
+
...(child.spec.combine === undefined ? {} : { combine: child.spec.combine }),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Pure extends-chain composition. The lookup returns authored packs by full id;
|
|
85
|
+
* parent pins (version + authored digest) are enforced here, so a loosened
|
|
86
|
+
* parent always moves the composed digest.
|
|
87
|
+
*/
|
|
88
|
+
export function composeGatePack(pack, lookupAuthored) {
|
|
89
|
+
if (pack.spec.extends === undefined)
|
|
90
|
+
return pack;
|
|
91
|
+
const parentAuthored = lookupAuthored(pack.spec.extends.id);
|
|
92
|
+
const parentPin = pack.spec.extends;
|
|
93
|
+
if (parentAuthored.metadata.version !== parentPin.version
|
|
94
|
+
|| artifactDigest(parentAuthored) !== parentPin.digest) {
|
|
95
|
+
throw new GateRegistryError(`pack ${pack.metadata.id} parent pin mismatch for ${pinLabel(parentPin)}`);
|
|
96
|
+
}
|
|
97
|
+
return applyGateExtends(composeGatePack(parentAuthored, lookupAuthored), pack);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Whether one expanded floor's `integrity-ceiling` gate genuinely governs the
|
|
101
|
+
* default ceiling: it must be an all-scoped `upstream-ceiling` gate that
|
|
102
|
+
* tightens the shipped default gate (floor flag included). A same-id gate of
|
|
103
|
+
* any other kind or scope never suppresses the default.
|
|
104
|
+
*/
|
|
105
|
+
function governsDefaultCeiling(floor, defaultGate) {
|
|
106
|
+
return floor.gates.some(gate => {
|
|
107
|
+
if (gate.id !== 'integrity-ceiling' || gate.kind !== 'upstream-ceiling' || gate.scope.mode !== 'all') {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
assertGateTightens(defaultGate, gate, floor.params);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Expands project floors over an explicit authored-pack map: inline packs are
|
|
121
|
+
* schema-checked again (direct registry callers bypass config validation) and
|
|
122
|
+
* semantically validated, pack references are verified by version and authored
|
|
123
|
+
* digest and composed through the pure `composeGatePack` (so `extends` floors
|
|
124
|
+
* enforce their composed minima). Every floor gate that binds a threshold
|
|
125
|
+
* parameter must declare a default for it: the default is the enforced
|
|
126
|
+
* minimum, and a required (default-less) floor parameter is a load error.
|
|
127
|
+
*
|
|
128
|
+
* The operator default floor — the composed gates of the shipped
|
|
129
|
+
* `aiwg:decision-engine/integrity-ceiling` pack, the single source of truth —
|
|
130
|
+
* is appended unless a configured floor governs it via `governsDefaultCeiling`
|
|
131
|
+
* above. An inline gate that merely reuses the id (any other kind or scope)
|
|
132
|
+
* never suppresses the default; the binding must then satisfy both, which
|
|
133
|
+
* fails closed with a diagnostic.
|
|
134
|
+
*/
|
|
135
|
+
export function expandFloorPacks(floors, lookupAuthored, providers) {
|
|
136
|
+
const expanded = (floors.floors ?? []).map(source => {
|
|
137
|
+
if ('packRef' in source) {
|
|
138
|
+
const pin = source.packRef;
|
|
139
|
+
const authored = lookupAuthored(pin.id);
|
|
140
|
+
if (authored.metadata.version !== pin.version
|
|
141
|
+
|| artifactDigest(authored) !== pin.digest) {
|
|
142
|
+
throw new GateRegistryError(`project floor pack pin mismatch for ${pinLabel(pin)}`);
|
|
143
|
+
}
|
|
144
|
+
const composed = composeGatePack(authored, lookupAuthored);
|
|
145
|
+
validateResolvedPack(composed, providers);
|
|
146
|
+
return {
|
|
147
|
+
packId: authored.metadata.id,
|
|
148
|
+
gates: composed.spec.gates,
|
|
149
|
+
params: { ...(composed.spec.parameters ?? {}) },
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const pack = validateGateDocument(source.pack);
|
|
153
|
+
if (pack.kind !== 'GatePack')
|
|
154
|
+
throw new GateRegistryError('project floor pack requires a GatePack document');
|
|
155
|
+
const composed = pack.spec.extends === undefined ? pack : composeGatePack(pack, lookupAuthored);
|
|
156
|
+
validateResolvedPack(composed, providers);
|
|
157
|
+
for (const gate of composed.spec.gates) {
|
|
158
|
+
const level = gate.statistic?.levelBps;
|
|
159
|
+
const margin = gate.statistic?.marginBps;
|
|
160
|
+
const refs = [gate.threshold?.param, isParamRef(gate.minimumN) ? gate.minimumN.param : undefined,
|
|
161
|
+
isParamRef(level) ? level.param : undefined, isParamRef(margin) ? margin.param : undefined];
|
|
162
|
+
for (const name of refs) {
|
|
163
|
+
if (name !== undefined && composed.spec.parameters?.[name]?.default === undefined) {
|
|
164
|
+
throw new GateRegistryError(`project floor gate '${gate.id}' parameter '${name}' requires a default: it is the enforced minimum`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
packId: pack.metadata.id,
|
|
170
|
+
gates: composed.spec.gates,
|
|
171
|
+
params: { ...(composed.spec.parameters ?? {}) },
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
const defaultAuthored = lookupAuthored(INTEGRITY_CEILING_FLOOR_PACK_ID);
|
|
175
|
+
const defaultComposed = composeGatePack(defaultAuthored, lookupAuthored);
|
|
176
|
+
validateResolvedPack(defaultComposed, providers);
|
|
177
|
+
const defaultGate = defaultComposed.spec.gates.find(gate => gate.id === 'integrity-ceiling');
|
|
178
|
+
if (defaultGate === undefined) {
|
|
179
|
+
throw new GateRegistryError(`shipped default floor ${INTEGRITY_CEILING_FLOOR_PACK_ID} has no integrity-ceiling gate`);
|
|
180
|
+
}
|
|
181
|
+
if (!expanded.some(floor => governsDefaultCeiling(floor, defaultGate))) {
|
|
182
|
+
expanded.push({
|
|
183
|
+
packId: defaultAuthored.metadata.id,
|
|
184
|
+
gates: defaultComposed.spec.gates,
|
|
185
|
+
params: { ...(defaultComposed.spec.parameters ?? {}) },
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return expanded;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Resolves a binding gate's threshold parameter to its preregistered binding
|
|
192
|
+
* value, so floor comparison is always value-against-minimum in the gate's
|
|
193
|
+
* declared direction. A binding may parameterize a floor literal: the value,
|
|
194
|
+
* not the spelling, must tighten.
|
|
195
|
+
*/
|
|
196
|
+
function synthesizeFloorCandidate(gate, packId, parameters) {
|
|
197
|
+
const materialize = (ref) => {
|
|
198
|
+
const value = parameters[qualifyGateParameter(packId, ref.param)];
|
|
199
|
+
if (value === undefined) {
|
|
200
|
+
throw new GateRegistryError(`binding gate '${gate.id}' parameter '${ref.param}' has no resolved value`);
|
|
201
|
+
}
|
|
202
|
+
return value;
|
|
203
|
+
};
|
|
204
|
+
let candidate = gate;
|
|
205
|
+
if (gate.threshold !== undefined && gate.threshold.param !== undefined) {
|
|
206
|
+
const { op, param } = gate.threshold;
|
|
207
|
+
candidate = { ...candidate, threshold: { op, value: materialize({ param }) } };
|
|
208
|
+
}
|
|
209
|
+
if (isParamRef(gate.minimumN))
|
|
210
|
+
candidate = { ...candidate, minimumN: materialize(gate.minimumN) };
|
|
211
|
+
const statistic = gate.statistic;
|
|
212
|
+
if (statistic !== undefined) {
|
|
213
|
+
candidate = { ...candidate, statistic: { ...statistic,
|
|
214
|
+
...(isParamRef(statistic.levelBps) ? { levelBps: materialize(statistic.levelBps) } : {}),
|
|
215
|
+
...(isParamRef(statistic.marginBps) ? { marginBps: materialize(statistic.marginBps) } : {}),
|
|
216
|
+
} };
|
|
217
|
+
}
|
|
218
|
+
return candidate;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Enforces project floors over already-resolved binding packs. Project policy
|
|
222
|
+
* outranks addon and framework packs: a binding that omits a floor gate, or
|
|
223
|
+
* whose gate loosens any floor gate's threshold, parameter value, scope or
|
|
224
|
+
* outcome (via the shared per-kind `assertGateTightens` and the scope-superset
|
|
225
|
+
* rule), is refused with a diagnostic naming the binding, the gate and the
|
|
226
|
+
* floor pack. Every binding gate sharing a floor gate id must tighten it, so
|
|
227
|
+
* a loosened duplicate cannot evade the floor.
|
|
228
|
+
*/
|
|
229
|
+
export function enforceProjectFloors(binding, packs, parameters, expanded, ceilings) {
|
|
230
|
+
// The '*' key is the project-wide default ceiling for every binding; a
|
|
231
|
+
// per-study key overrides it for that study only (exact match — renames
|
|
232
|
+
// never inherit). The star rule is re-checked here (C6): floors handed in
|
|
233
|
+
// directly, bypassing `validateGatesConfig`/`resolveProjectFloors`, still
|
|
234
|
+
// cannot carry starless or loosening per-study ceilings.
|
|
235
|
+
try {
|
|
236
|
+
assertCeilingDefaults(ceilings);
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
throw new GateRegistryError(error instanceof Error ? error.message : 'invalid project ceilings');
|
|
240
|
+
}
|
|
241
|
+
const configured = ceilings[binding.metadata.id] ?? ceilings['*'];
|
|
242
|
+
if (!projectCeilingSatisfied(binding.spec.ceiling, configured)) {
|
|
243
|
+
throw new GateRegistryError(`binding '${binding.metadata.id}' declares ceiling ${binding.spec.ceiling ?? 'PROMOTE'}`
|
|
244
|
+
+ ` below the project ceiling ${configured} for this study`);
|
|
245
|
+
}
|
|
246
|
+
const union = [];
|
|
247
|
+
for (const pack of packs) {
|
|
248
|
+
for (const gate of pack.resolved.spec.gates)
|
|
249
|
+
union.push({ gate, packId: pack.authored.metadata.id });
|
|
250
|
+
}
|
|
251
|
+
for (const floor of expanded) {
|
|
252
|
+
for (const floorGate of floor.gates) {
|
|
253
|
+
const matches = union.filter(candidate => candidate.gate.id === floorGate.id);
|
|
254
|
+
if (!matches.length) {
|
|
255
|
+
throw new GateRegistryError(`binding '${binding.metadata.id}' omits project floor gate '${floorGate.id}'`
|
|
256
|
+
+ ` (floor pack '${floor.packId}'): every binding in this project must include and tighten it`);
|
|
257
|
+
}
|
|
258
|
+
for (const match of matches) {
|
|
259
|
+
try {
|
|
260
|
+
assertGateTightens(floorGate, synthesizeFloorCandidate(match.gate, match.packId, parameters), floor.params);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
throw new GateRegistryError(`binding '${binding.metadata.id}' loosens project floor gate '${floorGate.id}'`
|
|
264
|
+
+ ` (floor pack '${floor.packId}'): ${error instanceof Error ? error.message : 'invalid tightening'}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=pack-composition.js.map
|