@aiwg/cli 2026.10.12 → 2026.10.14
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 +44 -1
- package/dist/src/cli/handlers/use-bundle-counts.js +32 -6
- package/dist/src/cli/handlers/use-skill-assets.js +207 -0
- package/dist/src/cli/handlers/use.js +30 -193
- package/dist/src/config/authorization-cli.js +11 -3
- package/dist/src/decision/context-pruning-common.js +41 -0
- package/dist/src/decision/context-pruning-evaluation.js +366 -0
- package/dist/src/decision/context-pruning-pilot.js +431 -0
- package/dist/src/decision/context-pruning-types.js +19 -0
- package/dist/src/decision/context-pruning.js +5 -847
- package/dist/src/decision/ensemble/contract.js +4 -24
- package/dist/src/decision/qualification/release.js +5 -62
- package/dist/src/extensions/manifest.js +8 -1
- package/dist/src/extensions/project-local-doctor.js +78 -2
- package/dist/src/extensions/validation.js +10 -11
- package/dist/src/gates/evaluate.js +3 -13
- package/dist/src/gates/upstream-ceiling.js +111 -0
- package/dist/src/models/provider-models.js +5 -8
- package/dist/src/models/wrapper-definitions.js +14 -0
- package/dist/src/models/wrapper-deployment.js +48 -25
- package/dist/src/models/wrapper-enforcement.js +72 -0
- package/dist/src/policy/hitl-check.js +80 -0
- package/dist/src/rules/activation.d.mts +2 -0
- package/dist/src/rules/activation.mjs +23 -0
- package/dist/src/skills/adapters/local.js +32 -9
- package/dist/src/skills/agent-skills.js +2 -0
- package/dist/src/skills/cli-display.js +33 -0
- package/dist/src/skills/cli.js +2 -26
- package/dist/src/skills/collision-discovery.js +84 -0
- package/dist/src/skills/deployer.js +20 -75
- package/dist/src/skills/deployment-ownership.js +74 -0
- package/dist/src/skills/doctor.js +2 -2
- package/dist/src/skills/importer.js +4 -26
- package/dist/src/skills/registry.js +22 -7
- package/dist/src/smiths/context-pipeline/parallelism-section.js +7 -3
- package/package.json +3 -1
- package/tools/agents/providers/antigravity.mjs +3 -5
- package/tools/agents/providers/base.mjs +3 -3
- package/tools/agents/providers/claude.mjs +3 -5
- package/tools/agents/providers/codex.mjs +3 -5
- package/tools/agents/providers/copilot.mjs +3 -5
- package/tools/agents/providers/cursor.mjs +3 -5
- package/tools/agents/providers/factory.mjs +3 -5
- package/tools/agents/providers/grok-build.mjs +9 -7
- package/tools/agents/providers/model-wrapper.mjs +86 -0
- package/tools/agents/providers/omp.mjs +3 -1
- package/tools/agents/providers/openclaw.mjs +3 -5
- package/tools/agents/providers/opencode.mjs +3 -5
- package/tools/agents/providers/provider-paths-staging.mjs +9 -0
- package/tools/agents/providers/rule-delivery.mjs +4 -4
- package/tools/agents/providers/warp.mjs +3 -5
- package/tools/agents/providers/windsurf.mjs +3 -5
- package/tools/plugin/package-plugins.mjs +2 -2
- package/tools/skills/codex-skill-transform.mjs +232 -0
- package/tools/skills/deploy-skills-codex.mjs +8 -249
- package/tools/warp/warp-content.mjs +548 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Context pruning digests and shared validation errors.
|
|
2
|
+
import { sha256 } from './compile-cache/identity.js';
|
|
3
|
+
export class ContextPruningError extends Error {
|
|
4
|
+
reason;
|
|
5
|
+
constructor(message, reason) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.reason = reason;
|
|
8
|
+
this.name = 'ContextPruningError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export const SHA = /^sha256:[0-9a-f]{64}$/;
|
|
12
|
+
export function contextPruningDigest(value) {
|
|
13
|
+
return sha256(value);
|
|
14
|
+
}
|
|
15
|
+
/** Order-independent digest of the frozen evaluation pairs: sorted (pairId, slice) tuples. */
|
|
16
|
+
export function contextPruningPairSetDigest(pairs) {
|
|
17
|
+
const tuples = pairs.map(pair => [pair.pairId, pair.slice])
|
|
18
|
+
.sort(([a, sa], [b, sb]) => a < b ? -1 : a > b ? 1 : sa < sb ? -1 : sa > sb ? 1 : 0);
|
|
19
|
+
return contextPruningDigest({ schemaVersion: 'decision-context-pruning-pair-set/v1', pairs: tuples });
|
|
20
|
+
}
|
|
21
|
+
/** Identity of one pilot run over an ordered candidate set, optionally tied to an evaluation pair. */
|
|
22
|
+
export function contextPruningRunId(pairId, candidates) {
|
|
23
|
+
return contextPruningDigest({
|
|
24
|
+
schemaVersion: 'decision-context-pruning-run-id/v1',
|
|
25
|
+
pairId,
|
|
26
|
+
candidates: candidates.map(candidate => contextPruningDigest(candidate)),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/** Order-independent digest of a receipt set, for anchoring the evaluated receipts separately from the report. */
|
|
30
|
+
export function contextPruningReceiptSetDigest(receipts) {
|
|
31
|
+
return contextPruningDigest({
|
|
32
|
+
schemaVersion: 'decision-context-pruning-receipt-set/v1',
|
|
33
|
+
receipts: receipts.map(receipt => receipt.receiptDigest).sort(),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export function requirePlain(value, label) {
|
|
37
|
+
if (!value || typeof value !== 'object' || ![Object.prototype, null].includes(Object.getPrototypeOf(value))) {
|
|
38
|
+
throw new ContextPruningError(`${label} must be a plain object`, 'schema');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=context-pruning-common.js.map
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// Context pruning preregistration, paired quality evaluation and release reporting.
|
|
2
|
+
import { ceilingDecision } from '../gates/lattice.js';
|
|
3
|
+
import { evaluateUpstreamCeiling } from '../gates/upstream-ceiling.js';
|
|
4
|
+
import { PairedDifferenceError, pairedBinaryDifferenceInterval, pairedMeanDifferenceBootstrap, pairedNonInferiority } from './qualification/quality.js';
|
|
5
|
+
import { ContextPruningError, SHA, contextPruningDigest, contextPruningPairSetDigest, contextPruningReceiptSetDigest, requirePlain } from './context-pruning-common.js';
|
|
6
|
+
import { uniqueCandidates, validateContextPruningReceipt } from './context-pruning-pilot.js';
|
|
7
|
+
import { CONTEXT_PRUNING_PREREGISTRATION_SCHEMA, CONTEXT_PRUNING_EVALUATION_REPORT_SCHEMA, CONTEXT_PRUNING_HOLDOUT_CLOCK_SKEW_MS, CONTEXT_PRUNING_QUALITY_METRICS } from './context-pruning-types.js';
|
|
8
|
+
export function createContextPruningPreregistration(input) {
|
|
9
|
+
const payload = { schemaVersion: CONTEXT_PRUNING_PREREGISTRATION_SCHEMA, ...input };
|
|
10
|
+
return validateContextPruningPreregistration({ ...payload, digest: contextPruningDigest(payload) });
|
|
11
|
+
}
|
|
12
|
+
export function validateContextPruningPreregistration(value) {
|
|
13
|
+
requirePlain(value, 'preregistration');
|
|
14
|
+
const t = value.thresholds;
|
|
15
|
+
const ci = t?.confidenceInterval;
|
|
16
|
+
const metrics = Array.isArray(t?.qualityMetrics) ? t.qualityMetrics : [];
|
|
17
|
+
const slices = Array.isArray(t?.slices) ? t.slices : [];
|
|
18
|
+
const bootstrapTail = ci && Number.isSafeInteger(ci.bootstrapResamples) && Number.isSafeInteger(ci.levelBps)
|
|
19
|
+
? Math.floor(ci.bootstrapResamples * (10_000 - ci.levelBps) / 20_000) : 0;
|
|
20
|
+
if (value.schemaVersion !== CONTEXT_PRUNING_PREREGISTRATION_SCHEMA || !value.id
|
|
21
|
+
|| !Number.isFinite(Date.parse(value.registeredAt))
|
|
22
|
+
|| typeof value.pairSetDigest !== 'string' || !SHA.test(value.pairSetDigest)
|
|
23
|
+
|| !t || t.protectedRetentionBps !== 10_000 || !ci
|
|
24
|
+
// pairedBinaryDifferenceInterval accepts only levels strictly between 5000 and 9999.
|
|
25
|
+
|| !Number.isSafeInteger(ci.levelBps) || ci.levelBps <= 5_000 || ci.levelBps >= 9_999
|
|
26
|
+
|| !['newcombe-10', 'tango'].includes(ci.binaryMethod) || ci.boundedMethod !== 'percentile-bootstrap'
|
|
27
|
+
|| !Number.isSafeInteger(ci.bootstrapSeed) || ci.bootstrapSeed < 0 || ci.bootstrapSeed > 0xffff_ffff
|
|
28
|
+
|| !Number.isSafeInteger(ci.bootstrapResamples) || ci.bootstrapResamples > 1_000_000 || bootstrapTail < 5
|
|
29
|
+
|| metrics.length !== CONTEXT_PRUNING_QUALITY_METRICS.length
|
|
30
|
+
|| new Set(metrics.map(item => item?.metric)).size !== metrics.length
|
|
31
|
+
|| metrics.some(item => !CONTEXT_PRUNING_QUALITY_METRICS.includes(item?.metric) || !['binary', 'bounded'].includes(item.scale))
|
|
32
|
+
|| slices.length === 0 || new Set(slices).size !== slices.length
|
|
33
|
+
|| slices.some(slice => typeof slice !== 'string' || !slice.trim())
|
|
34
|
+
|| !Number.isSafeInteger(t.minimumOverallN) || t.minimumOverallN < 2
|
|
35
|
+
|| !Number.isSafeInteger(t.minimumSliceN) || t.minimumSliceN < 1
|
|
36
|
+
|| !Number.isSafeInteger(t.minimumZeroVarianceN) || t.minimumZeroVarianceN < 2
|
|
37
|
+
|| !Number.isSafeInteger(t.minimumProtectedN) || t.minimumProtectedN < 2
|
|
38
|
+
|| (t.powerRule !== null && (typeof t.powerRule !== 'string' || !t.powerRule.trim()))
|
|
39
|
+
|| !Number.isSafeInteger(t.qualityNonInferiorityMarginBps) || t.qualityNonInferiorityMarginBps > 0
|
|
40
|
+
|| t.qualityNonInferiorityMarginBps < -10_000
|
|
41
|
+
|| !Number.isSafeInteger(t.positiveTotalTokenTarget) || t.positiveTotalTokenTarget <= 0
|
|
42
|
+
|| !Number.isFinite(t.positiveTotalCostTargetUsd) || t.positiveTotalCostTargetUsd <= 0) {
|
|
43
|
+
throw new ContextPruningError('invalid context pruning preregistration', 'schema');
|
|
44
|
+
}
|
|
45
|
+
const { digest, ...payload } = value;
|
|
46
|
+
if (digest !== contextPruningDigest(payload))
|
|
47
|
+
throw new ContextPruningError('preregistration digest mismatch', 'semantic');
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Builds the paired evaluation report. `trustedPreregistrationDigest` must come from a separately anchored
|
|
52
|
+
* record made before holdout access (as evaluatePreregisteredBinaryBenchmark requires): a caller-created
|
|
53
|
+
* preregistration cannot attest itself. A report without a recorded holdout access time cannot PROMOTE.
|
|
54
|
+
* A holdout access attested later than the evaluation clock plus CONTEXT_PRUNING_HOLDOUT_CLOCK_SKEW_MS
|
|
55
|
+
* is rejected as a future attestation.
|
|
56
|
+
*/
|
|
57
|
+
export function buildContextPruningEvaluationReport(input) {
|
|
58
|
+
const preregistration = validateContextPruningPreregistration(input.preregistration);
|
|
59
|
+
if (typeof input.trustedPreregistrationDigest !== 'string' || !SHA.test(input.trustedPreregistrationDigest)
|
|
60
|
+
|| preregistration.digest !== input.trustedPreregistrationDigest) {
|
|
61
|
+
throw new ContextPruningError('preregistration is not anchored to the trusted digest', 'semantic');
|
|
62
|
+
}
|
|
63
|
+
const holdoutAccessedAt = input.holdoutAccessedAt;
|
|
64
|
+
const holdoutAtMs = holdoutAccessedAt === null ? NaN : Date.parse(holdoutAccessedAt);
|
|
65
|
+
if (holdoutAccessedAt !== null && (typeof holdoutAccessedAt !== 'string' || !Number.isFinite(holdoutAtMs)
|
|
66
|
+
|| holdoutAtMs <= Date.parse(preregistration.registeredAt))) {
|
|
67
|
+
throw new ContextPruningError('holdout access must be recorded after preregistration', 'semantic');
|
|
68
|
+
}
|
|
69
|
+
if (Number.isFinite(holdoutAtMs)) {
|
|
70
|
+
const evaluationNowMs = (input.evaluationNow ?? Date.now)();
|
|
71
|
+
// Fail closed: an unknown evaluation time cannot vouch for the attestation.
|
|
72
|
+
if (!Number.isFinite(evaluationNowMs) || holdoutAtMs - evaluationNowMs > CONTEXT_PRUNING_HOLDOUT_CLOCK_SKEW_MS) {
|
|
73
|
+
throw new ContextPruningError('future holdout access attestation is rejected', 'semantic');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
validateIntegrity(input.integrity);
|
|
77
|
+
const thresholds = preregistration.thresholds;
|
|
78
|
+
validateMetrics(input.metrics, preregistration);
|
|
79
|
+
const metrics = input.metrics;
|
|
80
|
+
if (contextPruningPairSetDigest(metrics.pairs) !== preregistration.pairSetDigest) {
|
|
81
|
+
throw new ContextPruningError('evaluated pair set does not match the preregistered pair set', 'semantic');
|
|
82
|
+
}
|
|
83
|
+
const findings = new Set();
|
|
84
|
+
const missing = new Set(input.missingInputs ?? []);
|
|
85
|
+
if (holdoutAccessedAt === null)
|
|
86
|
+
findings.add('holdout-access-unrecorded');
|
|
87
|
+
const sliceSupport = Object.fromEntries(thresholds.slices.map(slice => [slice, 0]));
|
|
88
|
+
for (const pair of metrics.pairs)
|
|
89
|
+
sliceSupport[pair.slice] += 1;
|
|
90
|
+
if (metrics.pairs.length < thresholds.minimumOverallN)
|
|
91
|
+
findings.add('insufficient-overall-sample');
|
|
92
|
+
for (const slice of thresholds.slices) {
|
|
93
|
+
if (sliceSupport[slice] < thresholds.minimumSliceN)
|
|
94
|
+
findings.add(`insufficient-slice:${slice}`);
|
|
95
|
+
}
|
|
96
|
+
const protectedRetention = deriveProtectedRetention(input.pruningRuns, metrics.pairs, input.trustedReceiptSetDigest);
|
|
97
|
+
if (protectedRetention.bps === null)
|
|
98
|
+
findings.add('insufficient-protected-receipts');
|
|
99
|
+
else if (protectedRetention.bps !== metrics.protectedRetentionBps) {
|
|
100
|
+
throw new ContextPruningError('protected retention does not reconcile with the pruning receipts', 'semantic');
|
|
101
|
+
}
|
|
102
|
+
if (metrics.protectedRetentionBps < 10_000 || (protectedRetention.bps !== null && protectedRetention.bps < 10_000)) {
|
|
103
|
+
findings.add('protected-retention-breach');
|
|
104
|
+
}
|
|
105
|
+
// A full retention rate on too few protected items is not evidence: the count comes from validated receipts.
|
|
106
|
+
if (protectedRetention.protectedItems < thresholds.minimumProtectedN)
|
|
107
|
+
findings.add('insufficient-protected-sample');
|
|
108
|
+
const quality = thresholds.qualityMetrics.map(({ metric, scale }) => evaluateQualityMetric(metric, scale, metrics.quality.find(item => item.metric === metric), metrics.pairs, preregistration));
|
|
109
|
+
for (const result of quality) {
|
|
110
|
+
for (const slice of thresholds.slices) {
|
|
111
|
+
if (result.sliceSupport[slice] < thresholds.minimumSliceN)
|
|
112
|
+
findings.add(`insufficient-quality-slice:${result.metric}:${slice}`);
|
|
113
|
+
}
|
|
114
|
+
if (result.missingPairs > 0)
|
|
115
|
+
findings.add(`quality-outcomes-incomplete:${result.metric}`);
|
|
116
|
+
if (result.decision === 'not-non-inferior') {
|
|
117
|
+
findings.add('quality-non-inferiority-failed');
|
|
118
|
+
findings.add(`quality-non-inferiority-failed:${result.metric}`);
|
|
119
|
+
if (result.harm) {
|
|
120
|
+
findings.add('quality-harm');
|
|
121
|
+
findings.add(`quality-harm:${result.metric}`);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
findings.add(`quality-non-inferiority-inconclusive:${result.metric}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (result.decision === 'insufficient' && result.n === 0) {
|
|
128
|
+
findings.add(`quality-metric-missing:${result.metric}`);
|
|
129
|
+
}
|
|
130
|
+
else if (result.decision === 'insufficient' && result.n < thresholds.minimumOverallN) {
|
|
131
|
+
findings.add(`insufficient-quality-sample:${result.metric}`);
|
|
132
|
+
}
|
|
133
|
+
else if (result.decision === 'insufficient' && result.missingPairs === 0 && result.scale === 'bounded'
|
|
134
|
+
&& result.interval !== null && result.interval.lowerBps === result.interval.upperBps
|
|
135
|
+
&& result.n < thresholds.minimumZeroVarianceN) {
|
|
136
|
+
// A conclusive-sized read withheld for lack of variability evidence: a zero-width bounded
|
|
137
|
+
// interval below the preregistered zero-variance support.
|
|
138
|
+
findings.add(`insufficient-quality-support:${result.metric}`);
|
|
139
|
+
}
|
|
140
|
+
else if (result.decision === 'insufficient') {
|
|
141
|
+
// Interval construction failed, the verdict was unreadable, or a passing
|
|
142
|
+
// verdict was withheld for omitted pairs: adequate n is not evidence.
|
|
143
|
+
findings.add(`insufficient-quality-evidence:${result.metric}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const providerSavings = providerUsageSavings(metrics.providerUsage);
|
|
147
|
+
const estimatorSavings = { totalTokens: totalSavings(metrics.estimatorUsage, 'total') };
|
|
148
|
+
if (providerSavings.totalTokens < 0 || providerSavings.uncachedTokens < 0
|
|
149
|
+
|| (providerSavings.costUsd !== null && providerSavings.costUsd < 0)) {
|
|
150
|
+
findings.add('negative-net-economics');
|
|
151
|
+
}
|
|
152
|
+
if (providerSavings.totalTokens < thresholds.positiveTotalTokenTarget
|
|
153
|
+
|| providerSavings.uncachedTokens < thresholds.positiveTotalTokenTarget)
|
|
154
|
+
findings.add('provider-token-target-not-met');
|
|
155
|
+
if (estimatorSavings.totalTokens < thresholds.positiveTotalTokenTarget)
|
|
156
|
+
findings.add('estimator-token-target-not-met');
|
|
157
|
+
if (providerSavings.costUsd === null)
|
|
158
|
+
findings.add('provider-cost-unknown');
|
|
159
|
+
else if (providerSavings.costUsd < thresholds.positiveTotalCostTargetUsd)
|
|
160
|
+
findings.add('provider-cost-target-not-met');
|
|
161
|
+
const integrityGate = evaluateUpstreamCeiling(input.integrity, { policy: 'context-pruning' });
|
|
162
|
+
for (const finding of integrityGate.problems)
|
|
163
|
+
findings.add(finding);
|
|
164
|
+
const upstreamDecision = input.integrity.release_gate.decision;
|
|
165
|
+
for (const name of missing)
|
|
166
|
+
findings.add(`missing-input:${name}`);
|
|
167
|
+
// Automatic rollback triggers from the D26 rollout plan: protected miss, demonstrated quality harm, negative
|
|
168
|
+
// economics. An inconclusive non-inferiority result (no evidence of harm) holds instead.
|
|
169
|
+
const rollback = findings.has('protected-retention-breach')
|
|
170
|
+
|| findings.has('quality-harm') || findings.has('negative-net-economics');
|
|
171
|
+
const decision = ceilingDecision(integrityGate.ceiling, rollback ? 'ROLLBACK' : findings.size > 0 ? 'HOLD' : 'PROMOTE');
|
|
172
|
+
const advisory = [...findings].some(item => item.startsWith('insufficient-') || item.startsWith('quality-metric-missing:')
|
|
173
|
+
|| item.startsWith('missing-input:') || item.startsWith('quality-outcomes-incomplete:')
|
|
174
|
+
|| item.startsWith('quality-non-inferiority-inconclusive:')
|
|
175
|
+
|| item === 'holdout-access-unrecorded' || item === 'provider-cost-unknown')
|
|
176
|
+
? 'INSUFFICIENT EVIDENCE' : null;
|
|
177
|
+
const payload = {
|
|
178
|
+
schemaVersion: CONTEXT_PRUNING_EVALUATION_REPORT_SCHEMA,
|
|
179
|
+
preregistration,
|
|
180
|
+
holdoutAccessedAt,
|
|
181
|
+
integrity: input.integrity,
|
|
182
|
+
metrics,
|
|
183
|
+
derived: { sampleN: metrics.pairs.length, sliceSupport, quality, providerSavings, estimatorSavings, protectedRetention },
|
|
184
|
+
missingInputs: [...missing].sort(),
|
|
185
|
+
findings: [...findings].sort(),
|
|
186
|
+
upstreamDecision,
|
|
187
|
+
decision,
|
|
188
|
+
advisory,
|
|
189
|
+
};
|
|
190
|
+
return { ...payload, digest: contextPruningDigest(payload) };
|
|
191
|
+
}
|
|
192
|
+
function evaluateQualityMetric(metric, scale, outcomes, recordedPairs, preregistration) {
|
|
193
|
+
const { confidenceInterval: ci, minimumOverallN, minimumZeroVarianceN, qualityNonInferiorityMarginBps, slices } = preregistration.thresholds;
|
|
194
|
+
const pairs = outcomes?.pairs ?? [];
|
|
195
|
+
const sliceOf = new Map(recordedPairs.map(pair => [pair.pairId, pair.slice]));
|
|
196
|
+
const sliceSupport = Object.fromEntries(slices.map(slice => [slice, 0]));
|
|
197
|
+
for (const pair of pairs)
|
|
198
|
+
sliceSupport[sliceOf.get(pair.pairId)] += 1;
|
|
199
|
+
// validateMetrics guarantees outcome pair IDs are a unique subset of the recorded pairs.
|
|
200
|
+
const missingPairs = recordedPairs.length - pairs.length;
|
|
201
|
+
const result = (interval, decision) => ({
|
|
202
|
+
metric, scale, n: pairs.length, missingPairs,
|
|
203
|
+
harm: decision === 'not-non-inferior' && interval !== null
|
|
204
|
+
&& (interval.upperBps < 0 || interval.estimateBps < qualityNonInferiorityMarginBps),
|
|
205
|
+
sliceSupport, interval, decision,
|
|
206
|
+
});
|
|
207
|
+
if (pairs.length < minimumOverallN)
|
|
208
|
+
return result(null, 'insufficient');
|
|
209
|
+
let interval;
|
|
210
|
+
try {
|
|
211
|
+
if (scale === 'binary') {
|
|
212
|
+
const counts = { both: 0, candidateOnly: 0, baselineOnly: 0, neither: 0 };
|
|
213
|
+
for (const pair of pairs) {
|
|
214
|
+
if (pair.candidate === 1 && pair.baseline === 1)
|
|
215
|
+
counts.both++;
|
|
216
|
+
else if (pair.candidate === 1)
|
|
217
|
+
counts.candidateOnly++;
|
|
218
|
+
else if (pair.baseline === 1)
|
|
219
|
+
counts.baselineOnly++;
|
|
220
|
+
else
|
|
221
|
+
counts.neither++;
|
|
222
|
+
}
|
|
223
|
+
interval = pairedBinaryDifferenceInterval({ counts, levelBps: ci.levelBps, method: ci.binaryMethod });
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
interval = pairedMeanDifferenceBootstrap({
|
|
227
|
+
differences: pairs.map(pair => pair.candidate - pair.baseline), levelBps: ci.levelBps,
|
|
228
|
+
seed: ci.bootstrapSeed, resamples: ci.bootstrapResamples, bounds: [-1, 1],
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
if (error instanceof PairedDifferenceError)
|
|
234
|
+
return result(null, 'insufficient');
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
const verdict = pairedNonInferiority({ interval, marginBps: qualityNonInferiorityMarginBps });
|
|
238
|
+
// A regression visible in the reported outcomes still fails; omitted pairs can only withhold a pass.
|
|
239
|
+
if (verdict.decision === 'non-inferior' && missingPairs > 0)
|
|
240
|
+
return result(interval, 'insufficient');
|
|
241
|
+
// A zero-width bounded interval carries no variability evidence: identical pairs at a small n are
|
|
242
|
+
// inconclusive, so a would-be pass needs the preregistered zero-variance support first. Only passes
|
|
243
|
+
// are withheld here; demonstrated harm still rolls back through the verdict below.
|
|
244
|
+
if (verdict.decision === 'non-inferior' && scale === 'bounded'
|
|
245
|
+
&& interval.lowerBps === interval.upperBps && pairs.length < minimumZeroVarianceN) {
|
|
246
|
+
return result(interval, 'insufficient');
|
|
247
|
+
}
|
|
248
|
+
return result(interval, verdict.decision);
|
|
249
|
+
}
|
|
250
|
+
function deriveProtectedRetention(runs, pairs, trustedReceiptSetDigest) {
|
|
251
|
+
const pairIds = new Set(pairs.map(pair => pair.pairId));
|
|
252
|
+
if (!Array.isArray(runs) || runs.length !== pairIds.size || new Set(runs.map(run => run?.pairId)).size !== runs.length
|
|
253
|
+
|| runs.some(run => !run || !pairIds.has(run.pairId) || !Array.isArray(run.candidates) || !Array.isArray(run.receipts))) {
|
|
254
|
+
throw new ContextPruningError('pruning runs must cover every evaluated pair exactly once', 'invalid-input');
|
|
255
|
+
}
|
|
256
|
+
const allReceipts = runs.flatMap(run => run.receipts);
|
|
257
|
+
if (typeof trustedReceiptSetDigest !== 'string' || !SHA.test(trustedReceiptSetDigest)
|
|
258
|
+
|| contextPruningReceiptSetDigest(allReceipts) !== trustedReceiptSetDigest) {
|
|
259
|
+
throw new ContextPruningError('evaluated receipt set does not match the trusted receipt-set digest', 'semantic');
|
|
260
|
+
}
|
|
261
|
+
let protectedItems = 0;
|
|
262
|
+
let retained = 0;
|
|
263
|
+
for (const run of runs) {
|
|
264
|
+
uniqueCandidates(run.candidates);
|
|
265
|
+
const itemIds = run.receipts.map((receipt) => receipt?.itemId);
|
|
266
|
+
if (new Set(itemIds).size !== itemIds.length || itemIds.length !== run.candidates.length) {
|
|
267
|
+
throw new ContextPruningError('pruning receipts are not bound to the evaluated run candidates', 'semantic');
|
|
268
|
+
}
|
|
269
|
+
for (const receipt of run.receipts) {
|
|
270
|
+
if (receipt?.pairId !== run.pairId)
|
|
271
|
+
throw new ContextPruningError('pruning receipt is not bound to the evaluated run', 'semantic');
|
|
272
|
+
validateContextPruningReceipt(receipt, run.candidates);
|
|
273
|
+
if (!receipt.classification.protected)
|
|
274
|
+
continue;
|
|
275
|
+
protectedItems++;
|
|
276
|
+
if (receipt.proposedAction === 'keep' && receipt.appliedAction === 'keep')
|
|
277
|
+
retained++;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return { protectedItems, retained, bps: protectedItems === 0 ? null : Math.floor(retained * 10_000 / protectedItems) };
|
|
281
|
+
}
|
|
282
|
+
function validateIntegrity(integrity) {
|
|
283
|
+
if (!integrity || typeof integrity !== 'object' || !integrity.release_gate
|
|
284
|
+
|| !['PROMOTE', 'HOLD', 'ROLLBACK'].includes(integrity.release_gate.decision)
|
|
285
|
+
|| typeof integrity.integrity_state !== 'string') {
|
|
286
|
+
throw new ContextPruningError('invalid eval-integrity metadata', 'schema');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const isCount = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
290
|
+
function validateMetrics(metrics, preregistration) {
|
|
291
|
+
requirePlain(metrics, 'metrics');
|
|
292
|
+
const slices = new Set(preregistration.thresholds.slices);
|
|
293
|
+
const pairs = Array.isArray(metrics.pairs) ? metrics.pairs : null;
|
|
294
|
+
const pairIds = new Set(pairs?.map(pair => pair?.pairId));
|
|
295
|
+
if (!pairs || pairIds.size !== pairs.length
|
|
296
|
+
|| pairs.some(pair => !pair || typeof pair.pairId !== 'string' || !pair.pairId || !slices.has(pair.slice))) {
|
|
297
|
+
throw new ContextPruningError('pair records must have unique IDs and preregistered slices', 'schema');
|
|
298
|
+
}
|
|
299
|
+
const scales = new Map(preregistration.thresholds.qualityMetrics.map(item => [item.metric, item.scale]));
|
|
300
|
+
const quality = Array.isArray(metrics.quality) ? metrics.quality : null;
|
|
301
|
+
if (!quality || new Set(quality.map(item => item?.metric)).size !== quality.length
|
|
302
|
+
|| quality.some(item => !item || !scales.has(item.metric) || !Array.isArray(item.pairs))) {
|
|
303
|
+
throw new ContextPruningError('quality outcomes must name each preregistered metric at most once', 'schema');
|
|
304
|
+
}
|
|
305
|
+
for (const item of quality) {
|
|
306
|
+
const scale = scales.get(item.metric);
|
|
307
|
+
const seen = new Set();
|
|
308
|
+
for (const pair of item.pairs) {
|
|
309
|
+
const inRange = (value) => typeof value === 'number' && Number.isFinite(value)
|
|
310
|
+
&& (scale === 'binary' ? value === 0 || value === 1 : value >= 0 && value <= 1);
|
|
311
|
+
if (!pair || !pairIds.has(pair.pairId) || seen.has(pair.pairId) || !inRange(pair.baseline) || !inRange(pair.candidate)) {
|
|
312
|
+
throw new ContextPruningError(`quality outcome for '${item.metric}' is out of range or not reconciled with the pair records`, 'schema');
|
|
313
|
+
}
|
|
314
|
+
seen.add(pair.pairId);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (!isCount(metrics.protectedRetentionBps) || metrics.protectedRetentionBps > 10_000
|
|
318
|
+
|| metrics.sharedStateAccounting !== 'not-applicable'
|
|
319
|
+
|| !usageAccounting(metrics.providerUsage, 'provider') || !usageAccounting(metrics.estimatorUsage, 'estimator')
|
|
320
|
+
|| !isCount(metrics.totalCalls) || !metrics.latencyMs
|
|
321
|
+
|| [metrics.latencyMs.p50, metrics.latencyMs.p95, metrics.latencyMs.p99]
|
|
322
|
+
.some(value => value !== null && (!Number.isFinite(value) || value < 0))) {
|
|
323
|
+
throw new ContextPruningError('invalid context pruning metrics', 'schema');
|
|
324
|
+
}
|
|
325
|
+
const cache = metrics.promptCache;
|
|
326
|
+
for (const arm of ['baseline', 'prunedDownstream']) {
|
|
327
|
+
const reported = cache?.[arm];
|
|
328
|
+
const usage = metrics.providerUsage[arm];
|
|
329
|
+
// Cache credit is never a caller assertion: it must match provider-reported usage for the same arm.
|
|
330
|
+
if (!reported || !isCount(reported.hits) || !isCount(reported.misses) || !isCount(reported.cachedInputTokens)
|
|
331
|
+
|| reported.cachedInputTokens !== usage.cachedInputTokens
|
|
332
|
+
|| (reported.cachedInputTokens > 0 && reported.hits === 0)) {
|
|
333
|
+
throw new ContextPruningError(`prompt-cache usage for '${arm}' does not reconcile with provider usage`, 'semantic');
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const USAGE_ARMS = ['baseline', 'prunedDownstream', 'decisionCalls', 'fallbackCalls', 'transformationCalls'];
|
|
338
|
+
function usage(value, source) {
|
|
339
|
+
return Boolean(value) && isCount(value.inputTokens) && isCount(value.outputTokens)
|
|
340
|
+
&& (source === 'provider'
|
|
341
|
+
? isCount(value.cachedInputTokens) && value.cachedInputTokens <= value.inputTokens
|
|
342
|
+
: value.cachedInputTokens === null)
|
|
343
|
+
&& (value.costUsd === null || (Number.isFinite(value.costUsd) && value.costUsd >= 0));
|
|
344
|
+
}
|
|
345
|
+
function usageAccounting(value, source) {
|
|
346
|
+
return Boolean(value) && typeof value === 'object'
|
|
347
|
+
&& Object.keys(value).sort().join() === [...USAGE_ARMS].sort().join()
|
|
348
|
+
&& USAGE_ARMS.every(arm => usage(value[arm], source));
|
|
349
|
+
}
|
|
350
|
+
/** Baseline arm minus every pruned-arm call (downstream, decision, fallback, transformation). */
|
|
351
|
+
function totalSavings(value, basis) {
|
|
352
|
+
const tokens = (item) => item.inputTokens + item.outputTokens
|
|
353
|
+
- (basis === 'uncached' ? item.cachedInputTokens ?? 0 : 0);
|
|
354
|
+
return tokens(value.baseline) - USAGE_ARMS.filter(arm => arm !== 'baseline').reduce((sum, arm) => sum + tokens(value[arm]), 0);
|
|
355
|
+
}
|
|
356
|
+
function providerUsageSavings(value) {
|
|
357
|
+
const costs = USAGE_ARMS.map(arm => value[arm].costUsd);
|
|
358
|
+
return {
|
|
359
|
+
totalTokens: totalSavings(value, 'total'),
|
|
360
|
+
uncachedTokens: totalSavings(value, 'uncached'),
|
|
361
|
+
costUsd: costs.some(cost => cost === null) ? null
|
|
362
|
+
: value.baseline.costUsd - USAGE_ARMS.filter(arm => arm !== 'baseline').reduce((sum, arm) => sum + value[arm].costUsd, 0),
|
|
363
|
+
cachedInputTokensDelta: value.prunedDownstream.cachedInputTokens - value.baseline.cachedInputTokens,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=context-pruning-evaluation.js.map
|