@aiwg/cli 2026.10.11 → 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/model-catalog.v1.json +62 -12
- package/agentic/code/providers/pi/aiwg-bridge.ts +44 -1
- package/dist/src/artifacts/binding-target-identity.js +51 -0
- package/dist/src/artifacts/checksum-manifest.js +12 -0
- package/dist/src/artifacts/index-builder.js +11 -13
- package/dist/src/artifacts/query-engine.js +5 -11
- package/dist/src/cli/handlers/doctor-help.js +32 -0
- package/dist/src/cli/handlers/gates.js +19 -18
- package/dist/src/cli/handlers/models-help.js +7 -1
- package/dist/src/cli/handlers/models.js +3 -3
- package/dist/src/cli/handlers/script-runner.js +8 -5
- package/dist/src/cli/handlers/sessions-intake.js +104 -0
- package/dist/src/cli/handlers/sessions-tail.js +172 -0
- package/dist/src/cli/handlers/sessions.js +90 -17
- package/dist/src/cli/handlers/skill-lint.js +10 -3
- package/dist/src/cli/handlers/steward-format.js +100 -0
- package/dist/src/cli/handlers/steward-models.js +167 -0
- package/dist/src/cli/handlers/steward.js +14 -248
- package/dist/src/cli/handlers/use-bundle-counts.js +87 -0
- package/dist/src/cli/handlers/use-skill-assets.js +207 -0
- package/dist/src/cli/handlers/use.js +35 -216
- package/dist/src/cli/handlers/utilities.js +1 -24
- package/dist/src/config/aiwg-config.js +7 -0
- package/dist/src/config/authorization-cli.js +97 -0
- package/dist/src/config/cli-help.js +1 -0
- package/dist/src/config/cli.js +10 -1
- 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 -846
- package/dist/src/decision/ensemble/contract.js +17 -26
- package/dist/src/decision/ensemble-study/artifacts.js +5 -0
- package/dist/src/decision/ensemble-study/score.js +4 -3
- package/dist/src/decision/feature-export.js +3 -9
- package/dist/src/decision/heldout/collector.js +2 -2
- package/dist/src/decision/issue-triage/runtime.js +3 -9
- package/dist/src/decision/qualification/gates.js +2 -1
- package/dist/src/decision/qualification/release.js +6 -64
- package/dist/src/decision/routing/report.js +2 -0
- package/dist/src/decision/sdlc-screening.js +3 -5
- package/dist/src/decision/sensitivity/comparative.js +5 -3
- 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/driver.js +6 -77
- package/dist/src/gates/evaluate.js +6 -23
- package/dist/src/gates/floors.js +4 -4
- package/dist/src/gates/index.js +3 -3
- package/dist/src/gates/lattice.js +26 -0
- package/dist/src/gates/registry.js +2 -1
- package/dist/src/gates/report.js +19 -7
- package/dist/src/gates/upstream-ceiling.js +111 -0
- package/dist/src/issues/audit-cli.js +120 -0
- package/dist/src/issues/audit-contract.js +64 -0
- package/dist/src/issues/audit-local.js +82 -0
- package/dist/src/issues/audit.js +220 -0
- package/dist/src/lint/rule-activation.js +15 -1
- package/dist/src/mission-protocol/canonical-codecs.js +270 -0
- package/dist/src/mission-protocol/codecs.js +22 -550
- package/dist/src/mission-protocol/health-trace-codecs.js +281 -0
- package/dist/src/mission-protocol/health-trace-types.js +43 -0
- package/dist/src/mission-protocol/types.js +2 -42
- package/dist/src/mission-protocol/worker-contract-json.js +10 -0
- package/dist/src/mission-protocol/worker-review-gate.js +100 -0
- package/dist/src/mission-protocol/worker-review-types.js +2 -0
- package/dist/src/models/catalog-policy.js +280 -0
- package/dist/src/models/index.js +1 -1
- package/dist/src/models/model-catalog.v1.json +62 -12
- package/dist/src/models/policy-resources.js +28 -0
- package/dist/src/models/provider-models.js +5 -8
- package/dist/src/models/provider-policy.js +10 -546
- package/dist/src/models/resolver.js +26 -8
- package/dist/src/models/router.js +15 -2
- package/dist/src/models/tiers.js +13 -0
- package/dist/src/models/types.js +0 -6
- package/dist/src/models/worker-ceilings.js +59 -0
- package/dist/src/models/worker-invocation-policy.js +243 -0
- package/dist/src/models/worker-plan.js +16 -20
- package/dist/src/models/worker-review-lineage.js +55 -0
- package/dist/src/models/worker-selection-policy.js +1 -1
- 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/models/wrapper-route.js +6 -3
- package/dist/src/policy/authorization.js +1 -0
- package/dist/src/policy/hitl-check.js +80 -0
- package/dist/src/policy/hitl-config.js +63 -0
- package/dist/src/policy/hitl-receipt.js +57 -0
- package/dist/src/policy/hitl-types.js +4 -0
- package/dist/src/policy/hitl-validation.js +39 -0
- package/dist/src/policy/hitl.js +117 -0
- package/dist/src/rules/activation.d.mts +3 -1
- package/dist/src/rules/activation.mjs +23 -2
- package/dist/src/rules/gate-coverage.js +51 -0
- package/dist/src/rules/gate-references.js +16 -0
- package/dist/src/rules/rule-frontmatter.schema.json +7 -1
- package/dist/src/serve/worker-command-request.js +21 -4
- package/dist/src/serve/worker-invocation-preparation.js +3 -1
- package/dist/src/serve/worker-invocation.js +21 -10
- package/dist/src/sessions/adapters/deepseek-harness.js +1 -0
- package/dist/src/sessions/adapters/grok-build-native.js +269 -0
- package/dist/src/sessions/adapters/grok-build.js +57 -15
- package/dist/src/sessions/adapters/hermes-state-db.js +252 -0
- package/dist/src/sessions/adapters/hermes.js +42 -3
- package/dist/src/sessions/adapters/warp.js +1 -1
- package/dist/src/sessions/batch-import.js +10 -0
- package/dist/src/sessions/contracts.js +1 -1
- package/dist/src/sessions/discovery-capability.js +38 -0
- package/dist/src/sessions/importer.js +8 -251
- package/dist/src/sessions/index.js +4 -0
- package/dist/src/sessions/intake-dir.js +71 -0
- package/dist/src/sessions/normalization.js +251 -0
- package/dist/src/sessions/tail.js +91 -0
- package/dist/src/sessions/workspace-discovery.js +83 -7
- package/dist/src/skills/adapters/local.js +32 -9
- package/dist/src/skills/agent-skill-sidecar.js +28 -0
- package/dist/src/skills/agent-skills.js +6 -23
- package/dist/src/skills/claude-surface-projection.js +25 -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 +26 -361
- package/dist/src/skills/deployment-ownership.js +74 -0
- package/dist/src/skills/deployment-policy.js +114 -0
- package/dist/src/skills/deployment-projection.js +152 -0
- package/dist/src/skills/deployment-types.js +20 -0
- package/dist/src/skills/doctor.js +2 -2
- package/dist/src/skills/exporter.js +8 -31
- package/dist/src/skills/importer.js +4 -26
- package/dist/src/skills/provider-extensions.js +41 -0
- package/dist/src/skills/registry.js +22 -7
- package/dist/src/skills/run.js +6 -0
- package/dist/src/skills/types.js +0 -8
- package/dist/src/skills/validator-resources.js +68 -0
- package/dist/src/skills/validator.js +6 -55
- package/dist/src/smiths/context-pipeline/parallelism-section.js +7 -3
- package/dist/src/storage/cli-migrate-options.js +125 -0
- package/dist/src/storage/cli-migrate.js +111 -0
- package/dist/src/storage/cli.js +4 -279
- package/dist/src/storage/migration-adapter.js +79 -0
- package/dist/src/storage/migration-copy.js +108 -0
- package/dist/src/storage/migration-protocol.js +14 -197
- package/dist/src/storage/migration-records.js +186 -0
- package/dist/src/storage/migration-types.js +12 -0
- package/dist/src/tracker/forge-http.js +2 -1
- package/dist/src/tracker/issue-pages.js +110 -0
- package/package.json +3 -1
- package/schemas/mission-v1.schema.json +335 -0
- package/schemas/models/model-catalog.v1.schema.json +19 -1
- package/schemas/models/model-config-compatibility.v1.schema.json +3 -2
- package/schemas/models/model-policy.v1.schema.json +1 -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 +44 -0
- package/tools/agents/providers/rule-binding-targets.mjs +146 -0
- package/tools/agents/providers/rule-delivery.mjs +9 -128
- 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
- package/vscode-extension/schemas/aiwg.config.v1.json +177 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { validateHitlPolicy } from '../policy/hitl-validation.js';
|
|
1
2
|
import { normalizeAiwgConfigCandidate } from './config-normalization.js';
|
|
2
3
|
import { atomicConfigWrite, withWorkerWriteLock } from './worker-write.js';
|
|
3
4
|
import { workerConfigDigest } from './worker-config-digest.js';
|
|
@@ -979,6 +980,9 @@ export async function readAiwgConfigCandidate(projectDir) {
|
|
|
979
980
|
if (workspaceErrors.length > 0) {
|
|
980
981
|
throw new Error(`Invalid .aiwg/aiwg.config:\n${workspaceErrors.join('\n')}`);
|
|
981
982
|
}
|
|
983
|
+
const hitlErrors = validateHitlPolicy(parsed.authorization?.hitl);
|
|
984
|
+
if (hitlErrors.length)
|
|
985
|
+
throw new Error(hitlErrors.join("\n"));
|
|
982
986
|
const authorizationErrors = parsed.authorization
|
|
983
987
|
? validateAuthorization(parsed.authorization).filter(item => item.severity === 'error')
|
|
984
988
|
: [];
|
|
@@ -1050,6 +1054,9 @@ async function writeAiwgConfigUnlocked(projectDir, config) {
|
|
|
1050
1054
|
throw error;
|
|
1051
1055
|
}
|
|
1052
1056
|
}
|
|
1057
|
+
const hitlErrors = validateHitlPolicy(config.authorization?.hitl);
|
|
1058
|
+
if (hitlErrors.length)
|
|
1059
|
+
throw new Error(hitlErrors.join("\n"));
|
|
1053
1060
|
const pinConfigs = loadRulePinConfigs(projectDir, config);
|
|
1054
1061
|
const currentCapabilities = new Set(installedRuleCapabilities(pinConfigs));
|
|
1055
1062
|
const removed = previous ? installedRuleCapabilities({ ...pinConfigs, project: previous }).filter(id => !currentCapabilities.has(id)) : [];
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Exposes HITL inspection, validation, preview, enable/revoke and signed receipts under config/steward.
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { AiwgError, EXIT_CODES } from '../cli/errors.js';
|
|
5
|
+
import { getProjectDir, readAiwgConfig, writeAiwgConfig } from './aiwg-config.js';
|
|
6
|
+
import { ensureAuthorization, loadHitlLayers } from '../policy/hitl-config.js';
|
|
7
|
+
import { resolveHitlAuthorization } from '../policy/hitl.js';
|
|
8
|
+
import { validateHitlPolicy } from '../policy/hitl-validation.js';
|
|
9
|
+
import { checkHitlAuthorization } from '../policy/hitl-check.js';
|
|
10
|
+
export const authorizationUsage = `aiwg config authorization (also aiwg steward authorization)
|
|
11
|
+
inspect | validate [--target DIRECTORY]
|
|
12
|
+
preview --action-file FILE [--target DIRECTORY]
|
|
13
|
+
check --action-file FILE [--target DIRECTORY]
|
|
14
|
+
enable --policy-file FILE [--target DIRECTORY]
|
|
15
|
+
revoke [--profile NAME] [--target DIRECTORY]
|
|
16
|
+
receipt --action-file FILE [--target DIRECTORY]
|
|
17
|
+
Preview is read-only. Receipt writes the decision to the existing signed effect ledger;
|
|
18
|
+
it does not perform the proposed action. Check writes a verified receipt before returning
|
|
19
|
+
auto-authorized; failures escalate. Action files must be host-classified descriptors,
|
|
20
|
+
never issue/model output. Action files cannot supply session authorization. Platform prompts remain authoritative.`;
|
|
21
|
+
const usageError = (message) => new AiwgError({ code: 'ERR_AUTHORIZATION_USAGE', message, exitCode: EXIT_CODES.USAGE });
|
|
22
|
+
async function jsonFile(file) {
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(await readFile(file, 'utf8'));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
throw usageError('Authorization JSON input could not be read or parsed');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export async function authorizationCli(args, cwd = process.cwd()) {
|
|
31
|
+
if (!args.length || args.includes('--help') || args[0] === 'help') {
|
|
32
|
+
console.log(authorizationUsage);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const [operation, ...rest] = args;
|
|
36
|
+
const flags = {};
|
|
37
|
+
const allowed = ['--target', ...(operation === 'enable' ? ['--policy-file'] : []),
|
|
38
|
+
...(['preview', 'receipt', 'check'].includes(operation) ? ['--action-file'] : []), ...(operation === 'revoke' ? ['--profile'] : [])];
|
|
39
|
+
for (let i = 0; i < rest.length; i += 2) {
|
|
40
|
+
if (!allowed.includes(rest[i]) || !rest[i + 1] || rest[i + 1].startsWith('--') || flags[rest[i]])
|
|
41
|
+
throw usageError('Invalid authorization option');
|
|
42
|
+
flags[rest[i]] = rest[i + 1];
|
|
43
|
+
}
|
|
44
|
+
const target = resolve(getProjectDir({ cwd }, rest));
|
|
45
|
+
if (operation === 'inspect' || operation === 'validate') {
|
|
46
|
+
const loaded = await loadHitlLayers(target);
|
|
47
|
+
console.log(JSON.stringify(operation === 'inspect' ? loaded : { valid: true, layers: loaded.layers.length }, null, 2));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (operation === 'preview' || operation === 'receipt' || operation === 'check') {
|
|
51
|
+
if (!flags['--action-file'])
|
|
52
|
+
throw usageError('--action-file is required');
|
|
53
|
+
const action = await jsonFile(resolve(cwd, flags['--action-file']));
|
|
54
|
+
if (operation === 'check') {
|
|
55
|
+
console.log(JSON.stringify(await checkHitlAuthorization(target, action), null, 2));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const loaded = await loadHitlLayers(target);
|
|
59
|
+
const context = { now: new Date().toISOString(), layers: loaded.layers };
|
|
60
|
+
if (operation === 'preview')
|
|
61
|
+
console.log(JSON.stringify(resolveHitlAuthorization(action, context), null, 2));
|
|
62
|
+
else {
|
|
63
|
+
const { effectSession, openLedger } = await import('../effects/host.js');
|
|
64
|
+
const { recordHitlAuthorization } = await import('../policy/hitl-receipt.js');
|
|
65
|
+
const session = await effectSession(loaded.projectDir, 'custom');
|
|
66
|
+
console.log(JSON.stringify(await recordHitlAuthorization(openLedger(session), action, context), null, 2));
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (operation !== 'enable' && operation !== 'revoke')
|
|
71
|
+
throw usageError('Unknown authorization operation');
|
|
72
|
+
const config = await readAiwgConfig(target);
|
|
73
|
+
if (!config)
|
|
74
|
+
throw usageError('No project config found; initialize project config first');
|
|
75
|
+
const authorization = ensureAuthorization(config);
|
|
76
|
+
if (operation === 'enable') {
|
|
77
|
+
if (!flags['--policy-file'])
|
|
78
|
+
throw usageError('--policy-file is required');
|
|
79
|
+
const policy = await jsonFile(resolve(cwd, flags['--policy-file']));
|
|
80
|
+
const errors = validateHitlPolicy(policy);
|
|
81
|
+
if (policy === undefined || errors.length)
|
|
82
|
+
throw usageError('Invalid authorization.hitl policy');
|
|
83
|
+
authorization.hitl = { ...policy, enabled: true };
|
|
84
|
+
}
|
|
85
|
+
else if (authorization.hitl) {
|
|
86
|
+
if (flags['--profile']) {
|
|
87
|
+
if (!Object.hasOwn(authorization.hitl.profiles, flags['--profile']))
|
|
88
|
+
throw usageError('Unknown authorization profile');
|
|
89
|
+
delete authorization.hitl.profiles[flags['--profile']];
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
authorization.hitl.enabled = false;
|
|
93
|
+
}
|
|
94
|
+
await writeAiwgConfig(target, config);
|
|
95
|
+
console.log(JSON.stringify({ operation, enabled: authorization.hitl?.enabled ?? false }));
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=authorization-cli.js.map
|
|
@@ -17,6 +17,7 @@ Subcommands:
|
|
|
17
17
|
path Print config directory path
|
|
18
18
|
edit Open config in $EDITOR/$VISUAL (requires a terminal)
|
|
19
19
|
edit --print-path Print the config file path instead of opening an editor
|
|
20
|
+
authorization Inspect/validate/preview/enable/revoke bounded HITL policy
|
|
20
21
|
gitignore Show/check/fix .gitignore AIWG entries
|
|
21
22
|
|
|
22
23
|
Reviewed worker policy pair (project scope, no prompts):
|
package/dist/src/config/cli.js
CHANGED
|
@@ -33,6 +33,11 @@ const _scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
|
33
33
|
* Main CLI entry point for `aiwg config <subcommand> [args]`
|
|
34
34
|
*/
|
|
35
35
|
export async function main(args) {
|
|
36
|
+
if (args[0] === 'authorization') {
|
|
37
|
+
const { authorizationCli } = await import('./authorization-cli.js');
|
|
38
|
+
await authorizationCli(args.slice(1));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
36
41
|
if (await handleWorkerAdoptionArgs(args, process.cwd(), 'config'))
|
|
37
42
|
return;
|
|
38
43
|
// Extract --config-dir flag before routing
|
|
@@ -291,7 +296,7 @@ async function projectConfigSet(key, raw, args, quiet = false) {
|
|
|
291
296
|
}
|
|
292
297
|
// Coerce booleans for known boolean fields
|
|
293
298
|
let value = raw;
|
|
294
|
-
if (key === 'rules' || key.startsWith('rules.') || key === 'parallelism' || /^parallelism\.(worker_dispatch|worker_mode|groups|clis|admission|budgets|health|retry|activation)(\.|$)/.test(key)) {
|
|
299
|
+
if (key === 'authorization' || key.startsWith('authorization.') || key === 'rules' || key.startsWith('rules.') || key === 'parallelism' || /^parallelism\.(worker_dispatch|worker_mode|groups|clis|admission|budgets|health|retry|activation)(\.|$)/.test(key)) {
|
|
295
300
|
try {
|
|
296
301
|
value = JSON.parse(raw);
|
|
297
302
|
}
|
|
@@ -397,6 +402,10 @@ async function projectConfigSet(key, raw, args, quiet = false) {
|
|
|
397
402
|
? (existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : emptyConfig())
|
|
398
403
|
: (await readAiwgConfig(projectDir)) ?? emptyConfig();
|
|
399
404
|
const before = structuredClone(getDottedPath(cfg, key));
|
|
405
|
+
if (key.startsWith('authorization.hitl')) {
|
|
406
|
+
const { ensureAuthorization } = await import('../policy/hitl-config.js');
|
|
407
|
+
ensureAuthorization(cfg);
|
|
408
|
+
}
|
|
400
409
|
setDottedPath(cfg, key, value);
|
|
401
410
|
const externalLinkErrors = validateExternalLinks(cfg.externalLinks);
|
|
402
411
|
if (externalLinkErrors.length > 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
|