@holdyourvoice/hyv 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +23 -10
- package/dist/ai-editor-rules.js +5 -2
- package/dist/ai-editor.js +52 -9
- package/dist/ai-editor.test.js +62 -10
- package/dist/approval-capability.js +111 -0
- package/dist/approval-capability.test.js +52 -0
- package/dist/approval-context.js +54 -0
- package/dist/approval-context.test.js +38 -0
- package/dist/benchmark.js +232 -0
- package/dist/benchmark.test.js +328 -0
- package/dist/canonical-json.js +123 -0
- package/dist/canonical-json.test.js +24 -0
- package/dist/cli.js +272 -19
- package/dist/cli.test.js +205 -8
- package/dist/hygiene.js +6 -0
- package/dist/hygiene.test.js +7 -1
- package/dist/judgment-task.js +171 -0
- package/dist/judgment-task.test.js +162 -0
- package/dist/learning.js +240 -100
- package/dist/learning.test.js +203 -3
- package/dist/lifecycle-adapter.js +75 -0
- package/dist/lifecycle-adapter.test.js +56 -0
- package/dist/mcp-tools.js +101 -7
- package/dist/mcp-tools.test.js +156 -6
- package/dist/mcp.js +213 -6
- package/dist/mcp.test.js +210 -11
- package/dist/pipeline.js +78 -14
- package/dist/pipeline.test.js +36 -2
- package/dist/preservation.js +89 -0
- package/dist/preservation.test.js +22 -0
- package/dist/profile.js +87 -0
- package/dist/profile.test.js +114 -0
- package/dist/rebuild-task.js +226 -0
- package/dist/rebuild-task.test.js +179 -0
- package/dist/release-audit.test.js +111 -2
- package/dist/rewrite-task.js +136 -16
- package/dist/rewrite-task.test.js +62 -7
- package/dist/rule-reconciliation.test.js +50 -0
- package/dist/semantic-review.js +176 -7
- package/dist/semantic-review.test.js +98 -14
- package/dist/stage1-dry-run.test.js +39 -0
- package/dist/stage1-evaluation.js +579 -0
- package/dist/stage1-evaluation.test.js +184 -0
- package/dist/stage1-human-packet.test.js +102 -0
- package/dist/stage1-schema-contract.test.js +95 -0
- package/dist/stage2-human-packet.test.js +81 -0
- package/dist/version.js +1 -1
- package/dist/voice-dna.js +53 -1
- package/dist/voice-dna.test.js +79 -1
- package/package.json +2 -2
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { canonicalJson } from './canonical-json.js';
|
|
3
|
+
export const BASELINE_COMMIT = '4e6269121d551c008a34db73077e1e4fea41b3f9';
|
|
4
|
+
export const STAGE1_COMMIT = '550ea24f652291dca13757fdbd2f0fa0b5e3f621';
|
|
5
|
+
export class EvaluationContractError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code) {
|
|
8
|
+
super(`Stage 1 evaluation rejected (${code}).`);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.name = 'EvaluationContractError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function fail(code) { throw new EvaluationContractError(code); }
|
|
14
|
+
function record(value, code = 'invalid_record') {
|
|
15
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
16
|
+
fail(code);
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function text(value, code = 'invalid_string') {
|
|
20
|
+
if (typeof value !== 'string' || value.length === 0)
|
|
21
|
+
fail(code);
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function digest(value, code = 'invalid_digest') {
|
|
25
|
+
const result = text(value, code);
|
|
26
|
+
if (!/^[a-f0-9]{64}$/.test(result))
|
|
27
|
+
fail(code);
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
function timestamp(value, code) {
|
|
31
|
+
const result = text(value, code);
|
|
32
|
+
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/.test(result) || !Number.isFinite(Date.parse(result)))
|
|
33
|
+
fail(code);
|
|
34
|
+
const canonical = result.includes('.') ? result : result.replace('Z', '.000Z');
|
|
35
|
+
if (new Date(result).toISOString() !== canonical)
|
|
36
|
+
fail(code);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function exactKeys(value, keys) {
|
|
40
|
+
const allowed = new Set(keys);
|
|
41
|
+
if (Object.keys(value).some((key) => !allowed.has(key)))
|
|
42
|
+
fail('unexpected_field');
|
|
43
|
+
if (keys.some((key) => !(key in value)))
|
|
44
|
+
fail('missing_field');
|
|
45
|
+
}
|
|
46
|
+
function finiteNumber(value, minimum, maximum = Number.POSITIVE_INFINITY, code = 'invalid_number') {
|
|
47
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < minimum || value > maximum)
|
|
48
|
+
fail(code);
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
function count(value, code = 'invalid_count') {
|
|
52
|
+
const result = finiteNumber(value, 0, Number.POSITIVE_INFINITY, code);
|
|
53
|
+
if (!Number.isInteger(result))
|
|
54
|
+
fail(code);
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
export function sha256Canonical(value) {
|
|
58
|
+
try {
|
|
59
|
+
return createHash('sha256').update(canonicalJson(value)).digest('hex');
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return fail('non_canonical_value');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function sha256Text(value) { return createHash('sha256').update(value).digest('hex'); }
|
|
66
|
+
function wilson95(numerator, denominator) {
|
|
67
|
+
if (denominator === 0)
|
|
68
|
+
return [0, 0];
|
|
69
|
+
const rate = numerator / denominator;
|
|
70
|
+
const z = 1.96;
|
|
71
|
+
const center = (rate + z * z / (2 * denominator)) / (1 + z * z / denominator);
|
|
72
|
+
const spread = z * Math.sqrt((rate * (1 - rate) + z * z / (4 * denominator)) / denominator) / (1 + z * z / denominator);
|
|
73
|
+
return [Math.max(0, center - spread), Math.min(1, center + spread)];
|
|
74
|
+
}
|
|
75
|
+
function parseProtocol(input) {
|
|
76
|
+
const value = record(input);
|
|
77
|
+
exactKeys(value, ['kind', 'version', 'mode', 'baseline', 'stage1', 'benchmark', 'cases', 'intentToTreat', 'execution', 'randomization', 'reviewerRosterDigest', 'analysis', 'rights', 'releaseAuditContractDigest', 'registeredAt', 'measures', 'gates']);
|
|
78
|
+
if (value.kind !== 'hyv-stage1-preregistration' || value.version !== '1')
|
|
79
|
+
fail('protocol_version_invalid');
|
|
80
|
+
if (value.mode !== 'development-calibration' && value.mode !== 'locked-human')
|
|
81
|
+
fail('protocol_mode_invalid');
|
|
82
|
+
const baseline = record(value.baseline);
|
|
83
|
+
exactKeys(baseline, ['packageVersion', 'sourceCommit']);
|
|
84
|
+
if (baseline.packageVersion !== '3.2.0' || baseline.sourceCommit !== BASELINE_COMMIT)
|
|
85
|
+
fail('baseline_identity_mismatch');
|
|
86
|
+
const stage1 = record(value.stage1);
|
|
87
|
+
exactKeys(stage1, ['sourceCommit']);
|
|
88
|
+
if (stage1.sourceCommit !== STAGE1_COMMIT)
|
|
89
|
+
fail('stage1_identity_mismatch');
|
|
90
|
+
const benchmark = record(value.benchmark);
|
|
91
|
+
exactKeys(benchmark, ['manifestDigest', 'partitionDigest', 'partition', 'synthetic']);
|
|
92
|
+
digest(benchmark.manifestDigest);
|
|
93
|
+
digest(benchmark.partitionDigest);
|
|
94
|
+
text(benchmark.partition);
|
|
95
|
+
if (typeof benchmark.synthetic !== 'boolean')
|
|
96
|
+
fail('benchmark_synthetic_invalid');
|
|
97
|
+
if ((value.mode === 'locked-human') === benchmark.synthetic)
|
|
98
|
+
fail('evidence_class_mismatch');
|
|
99
|
+
if (value.mode === 'locked-human' && benchmark.partition !== 'locked-test')
|
|
100
|
+
fail('benchmark_partition_mismatch');
|
|
101
|
+
if (!Array.isArray(value.cases) || value.cases.length === 0)
|
|
102
|
+
fail('cases_invalid');
|
|
103
|
+
const caseIds = new Set();
|
|
104
|
+
for (const item of value.cases) {
|
|
105
|
+
const entry = record(item);
|
|
106
|
+
exactKeys(entry, ['caseId', 'caseDigest', 'provenanceDigest', 'rightsDigest', 'providerDisclosureDigest', 'reviewerDisclosureDigest', 'derivedRetentionDigest']);
|
|
107
|
+
const id = text(entry.caseId);
|
|
108
|
+
for (const key of ['caseDigest', 'provenanceDigest', 'rightsDigest', 'providerDisclosureDigest', 'reviewerDisclosureDigest', 'derivedRetentionDigest'])
|
|
109
|
+
digest(entry[key]);
|
|
110
|
+
const expectedRights = sha256Canonical({ caseId: entry.caseId, caseDigest: entry.caseDigest, provenanceDigest: entry.provenanceDigest, providerDisclosureDigest: entry.providerDisclosureDigest, reviewerDisclosureDigest: entry.reviewerDisclosureDigest, derivedRetentionDigest: entry.derivedRetentionDigest });
|
|
111
|
+
if (entry.rightsDigest !== expectedRights)
|
|
112
|
+
fail('case_rights_digest_mismatch');
|
|
113
|
+
if (caseIds.has(id))
|
|
114
|
+
fail('duplicate_case');
|
|
115
|
+
caseIds.add(id);
|
|
116
|
+
}
|
|
117
|
+
const itt = record(value.intentToTreat);
|
|
118
|
+
exactKeys(itt, ['expectedAssignments', 'expectedReviewers']);
|
|
119
|
+
if (!Number.isInteger(itt.expectedAssignments) || itt.expectedAssignments < 1 || !Number.isInteger(itt.expectedReviewers) || itt.expectedReviewers < 1)
|
|
120
|
+
fail('itt_invalid');
|
|
121
|
+
const execution = record(value.execution);
|
|
122
|
+
exactKeys(execution, ['provider', 'model', 'modelRevision', 'settingsDigest', 'taskContractDigest', 'rulesetDigest', 'rubricDigest']);
|
|
123
|
+
text(execution.provider);
|
|
124
|
+
text(execution.model);
|
|
125
|
+
text(execution.modelRevision);
|
|
126
|
+
for (const key of ['settingsDigest', 'taskContractDigest', 'rulesetDigest', 'rubricDigest'])
|
|
127
|
+
digest(execution[key]);
|
|
128
|
+
const randomization = record(value.randomization);
|
|
129
|
+
exactKeys(randomization, ['algorithm', 'commitment']);
|
|
130
|
+
text(randomization.algorithm);
|
|
131
|
+
digest(randomization.commitment);
|
|
132
|
+
digest(value.reviewerRosterDigest);
|
|
133
|
+
const analysis = record(value.analysis);
|
|
134
|
+
exactKeys(analysis, ['statistic', 'decisionRule', 'margin', 'minimumCases', 'minimumRatings', 'missingRatingPolicy', 'tiePolicy', 'retryPolicy', 'routingPolicy']);
|
|
135
|
+
if (analysis.statistic !== 'paired-preference-rate' || analysis.decisionRule !== 'preference-or-correction-with-workflow-non-regression')
|
|
136
|
+
fail('analysis_statistic_invalid');
|
|
137
|
+
if (typeof analysis.margin !== 'number' || analysis.margin < 0 || analysis.margin > 1)
|
|
138
|
+
fail('analysis_margin_invalid');
|
|
139
|
+
if (!Number.isInteger(analysis.minimumCases) || analysis.minimumCases < 1 || !Number.isInteger(analysis.minimumRatings) || analysis.minimumRatings < 1)
|
|
140
|
+
fail('analysis_minimum_invalid');
|
|
141
|
+
if (analysis.missingRatingPolicy !== 'count-as-non-preference' || analysis.tiePolicy !== 'count-as-half' || analysis.retryPolicy !== 'no-retry' || analysis.routingPolicy !== 'precommitted-blind-routing')
|
|
142
|
+
fail('analysis_policy_invalid');
|
|
143
|
+
const rights = record(value.rights);
|
|
144
|
+
exactKeys(rights, ['providerDisclosureDigest', 'reviewerDisclosureDigest', 'derivedRetentionDigest']);
|
|
145
|
+
digest(rights.providerDisclosureDigest);
|
|
146
|
+
digest(rights.reviewerDisclosureDigest);
|
|
147
|
+
digest(rights.derivedRetentionDigest);
|
|
148
|
+
digest(value.releaseAuditContractDigest);
|
|
149
|
+
if (value.cases.some((item) => { const entry = item; return entry.providerDisclosureDigest !== rights.providerDisclosureDigest || entry.reviewerDisclosureDigest !== rights.reviewerDisclosureDigest || entry.derivedRetentionDigest !== rights.derivedRetentionDigest; }))
|
|
150
|
+
fail('rights_scope_mismatch');
|
|
151
|
+
timestamp(value.registeredAt, 'registered_at_invalid');
|
|
152
|
+
if (JSON.stringify(value.measures) !== JSON.stringify(['writer_preference', 'correction_versus_confirm', 'workflow_completion', 'workflow_abandonment']))
|
|
153
|
+
fail('measures_invalid');
|
|
154
|
+
if (JSON.stringify(value.gates) !== JSON.stringify(['semantic', 'copy_spec', 'hygiene', 'preservation', 'cli_mcp_parity', 'backward_compatibility']))
|
|
155
|
+
fail('gates_invalid');
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
export function commitProtocol(input) {
|
|
159
|
+
const protocol = parseProtocol(input);
|
|
160
|
+
return { ...protocol, protocolDigest: sha256Canonical(protocol) };
|
|
161
|
+
}
|
|
162
|
+
function assertCommittedProtocol(input) {
|
|
163
|
+
const value = record(input);
|
|
164
|
+
const { protocolDigest, ...protocol } = value;
|
|
165
|
+
const parsed = parseProtocol(protocol);
|
|
166
|
+
if (digest(protocolDigest, 'protocol_digest_invalid') !== sha256Canonical(parsed))
|
|
167
|
+
fail('protocol_digest_mismatch');
|
|
168
|
+
return value;
|
|
169
|
+
}
|
|
170
|
+
function effectiveRunOutcome(run) {
|
|
171
|
+
if (run.outcome === 'completed' && run.hardGates && Object.values(run.hardGates).some((passed) => !passed))
|
|
172
|
+
return 'hard-gate-failed';
|
|
173
|
+
return run.outcome;
|
|
174
|
+
}
|
|
175
|
+
function candidateDigest(protocol) {
|
|
176
|
+
return sha256Canonical({ baseline: protocol.baseline, stage1: protocol.stage1 });
|
|
177
|
+
}
|
|
178
|
+
function parseRun(protocol, input) {
|
|
179
|
+
const value = record(input);
|
|
180
|
+
const required = ['kind', 'version', 'evidenceClass', 'protocolDigest', 'candidateDigest', 'assignmentId', 'participantId', 'caseId', 'caseDigest', 'rightsDigest', 'arm', 'ordinal', 'latencyMs', 'inputTokens', 'outputTokens', 'costMicrousd', 'occurredAt', 'outcome'];
|
|
181
|
+
exactKeys(value, value.outcome === 'completed' ? [...required, 'outputDigest', 'hardGates'] : required);
|
|
182
|
+
if (value.kind !== 'hyv-stage1-run-record' || value.version !== '1')
|
|
183
|
+
fail('run_version_invalid');
|
|
184
|
+
if (value.protocolDigest !== protocol.protocolDigest)
|
|
185
|
+
fail('protocol_digest_mismatch');
|
|
186
|
+
if (value.candidateDigest !== candidateDigest(protocol))
|
|
187
|
+
fail('candidate_digest_mismatch');
|
|
188
|
+
if (value.evidenceClass !== (protocol.mode === 'locked-human' ? 'human' : 'synthetic-dry-run'))
|
|
189
|
+
fail('evidence_class_mismatch');
|
|
190
|
+
text(value.assignmentId);
|
|
191
|
+
text(value.participantId);
|
|
192
|
+
const caseId = text(value.caseId);
|
|
193
|
+
const expected = protocol.cases.find((item) => item.caseId === caseId);
|
|
194
|
+
if (!expected)
|
|
195
|
+
fail('case_unknown');
|
|
196
|
+
if (value.caseDigest !== expected.caseDigest)
|
|
197
|
+
fail('case_digest_mismatch');
|
|
198
|
+
if (value.rightsDigest !== expected.rightsDigest)
|
|
199
|
+
fail('rights_digest_mismatch');
|
|
200
|
+
if (value.arm !== 'baseline' && value.arm !== 'stage1')
|
|
201
|
+
fail('arm_invalid');
|
|
202
|
+
if (!Number.isInteger(value.ordinal) || value.ordinal < 1)
|
|
203
|
+
fail('ordinal_invalid');
|
|
204
|
+
for (const key of ['latencyMs', 'inputTokens', 'outputTokens', 'costMicrousd'])
|
|
205
|
+
if (typeof value[key] !== 'number' || value[key] < 0 || !Number.isFinite(value[key]))
|
|
206
|
+
fail('run_measure_invalid');
|
|
207
|
+
timestamp(value.occurredAt, 'occurred_at_invalid');
|
|
208
|
+
if (!['completed', 'hard-failure', 'timeout', 'abandoned'].includes(String(value.outcome)))
|
|
209
|
+
fail('outcome_invalid');
|
|
210
|
+
if (value.outcome === 'completed')
|
|
211
|
+
digest(value.outputDigest, 'output_digest_invalid');
|
|
212
|
+
if (value.outcome === 'completed') {
|
|
213
|
+
const gates = record(value.hardGates);
|
|
214
|
+
exactKeys(gates, ['semantic', 'copySpec', 'hygiene', 'preservation', 'cliMcpParity', 'backwardCompatibility']);
|
|
215
|
+
if (Object.values(gates).some((item) => typeof item !== 'boolean'))
|
|
216
|
+
fail('hard_gates_invalid');
|
|
217
|
+
}
|
|
218
|
+
return value;
|
|
219
|
+
}
|
|
220
|
+
export function validateRuns(protocolInput, runInputs) {
|
|
221
|
+
const protocol = assertCommittedProtocol(protocolInput);
|
|
222
|
+
if (!Array.isArray(runInputs))
|
|
223
|
+
fail('runs_invalid');
|
|
224
|
+
const runs = runInputs.map((item) => parseRun(protocol, item));
|
|
225
|
+
if (runs.length !== protocol.intentToTreat.expectedAssignments)
|
|
226
|
+
fail('itt_denominator_mismatch');
|
|
227
|
+
if (new Set(runs.map((run) => run.assignmentId)).size !== runs.length)
|
|
228
|
+
fail('duplicate_assignment');
|
|
229
|
+
if (JSON.stringify(runs.map((run) => run.ordinal).sort((a, b) => a - b)) !== JSON.stringify(Array.from({ length: runs.length }, (_, index) => index + 1)))
|
|
230
|
+
fail('ordinal_sequence_invalid');
|
|
231
|
+
for (const item of protocol.cases) {
|
|
232
|
+
const pair = runs.filter((run) => run.caseId === item.caseId);
|
|
233
|
+
const arms = pair.map((run) => run.arm).sort();
|
|
234
|
+
if (JSON.stringify(arms) !== JSON.stringify(['baseline', 'stage1']))
|
|
235
|
+
fail('paired_assignment_mismatch');
|
|
236
|
+
if (new Set(pair.map((run) => run.participantId)).size !== 1)
|
|
237
|
+
fail('paired_participant_mismatch');
|
|
238
|
+
}
|
|
239
|
+
return { denominator: runs.length, completed: runs.filter((r) => effectiveRunOutcome(r) === 'completed').length, hardFailures: runs.filter((r) => ['hard-failure', 'hard-gate-failed'].includes(effectiveRunOutcome(r))).length, timeouts: runs.filter((r) => r.outcome === 'timeout').length, abandonments: runs.filter((r) => r.outcome === 'abandoned').length, runsDigest: sha256Canonical(runs) };
|
|
240
|
+
}
|
|
241
|
+
function parsePacket(input) {
|
|
242
|
+
const packet = record(input);
|
|
243
|
+
exactKeys(packet, ['kind', 'version', 'protocolDigest', 'candidateDigest', 'runsDigest', 'mappingDigest', 'contentDigest', 'nonReviewableDigest', 'reviewableCount', 'nonReviewableCount', 'encryptedAtRest', 'approvedStorage', 'packetDigest']);
|
|
244
|
+
if (packet.kind !== 'hyv-stage1-blind-packet' || packet.version !== '1' || packet.encryptedAtRest !== true || packet.approvedStorage !== true)
|
|
245
|
+
fail('blind_packet_invalid');
|
|
246
|
+
for (const key of ['protocolDigest', 'candidateDigest', 'runsDigest', 'mappingDigest', 'contentDigest', 'nonReviewableDigest', 'packetDigest'])
|
|
247
|
+
digest(packet[key]);
|
|
248
|
+
if (!Number.isInteger(packet.reviewableCount) || !Number.isInteger(packet.nonReviewableCount) || packet.reviewableCount < 0 || packet.nonReviewableCount < 0)
|
|
249
|
+
fail('blind_packet_invalid');
|
|
250
|
+
const { packetDigest, ...base } = packet;
|
|
251
|
+
if (packetDigest !== sha256Canonical(base))
|
|
252
|
+
fail('packet_digest_mismatch');
|
|
253
|
+
return packet;
|
|
254
|
+
}
|
|
255
|
+
function parseMapping(input) {
|
|
256
|
+
const mapping = record(input);
|
|
257
|
+
exactKeys(mapping, ['kind', 'version', 'nonce', 'labels', 'custodianId', 'unblindingAccess', 'attestation']);
|
|
258
|
+
if (mapping.kind !== 'hyv-stage1-blind-mapping' || mapping.version !== '1')
|
|
259
|
+
fail('blind_mapping_invalid');
|
|
260
|
+
const labels = record(mapping.labels);
|
|
261
|
+
exactKeys(labels, ['A', 'B']);
|
|
262
|
+
if (!['baseline', 'stage1'].includes(String(labels.A)) || !['baseline', 'stage1'].includes(String(labels.B)) || labels.A === labels.B)
|
|
263
|
+
fail('blind_mapping_invalid');
|
|
264
|
+
if (!/^[A-Za-z0-9_-]{22,}$/.test(text(mapping.nonce)))
|
|
265
|
+
fail('randomization_nonce_invalid');
|
|
266
|
+
text(mapping.custodianId);
|
|
267
|
+
if (!Array.isArray(mapping.unblindingAccess) || mapping.unblindingAccess.length === 0 || new Set(mapping.unblindingAccess).size !== mapping.unblindingAccess.length || !mapping.unblindingAccess.every((item) => typeof item === 'string' && item.length > 0))
|
|
268
|
+
fail('unblinding_access_invalid');
|
|
269
|
+
if (mapping.attestation !== null)
|
|
270
|
+
validateAttestation(mapping.attestation);
|
|
271
|
+
return mapping;
|
|
272
|
+
}
|
|
273
|
+
function mappingCore(mapping) { const { attestation: _attestation, ...core } = mapping; return core; }
|
|
274
|
+
function validateMappingOpening(protocol, mapping) {
|
|
275
|
+
if (protocol.randomization.algorithm !== 'sha256-counter-v1' || protocol.randomization.commitment !== sha256Canonical({ algorithm: protocol.randomization.algorithm, nonce: mapping.nonce, labels: mapping.labels }))
|
|
276
|
+
fail('randomization_commitment_mismatch');
|
|
277
|
+
}
|
|
278
|
+
export function freezeBlind(protocolInput, runInputs, mappingInput, contentsInput, nonReviewableInput = []) {
|
|
279
|
+
const protocol = assertCommittedProtocol(protocolInput);
|
|
280
|
+
const runSummary = validateRuns(protocol, runInputs);
|
|
281
|
+
const mapping = parseMapping(mappingInput);
|
|
282
|
+
validateMappingOpening(protocol, mapping);
|
|
283
|
+
if (!Array.isArray(contentsInput))
|
|
284
|
+
fail('blind_contents_invalid');
|
|
285
|
+
const contents = contentsInput.map((item) => { const entry = record(item); exactKeys(entry, ['caseId', 'A', 'B']); text(entry.caseId); text(entry.A); text(entry.B); return entry; });
|
|
286
|
+
if (/baseline|stage\s*[-_ ]?1|550ea24|4e626912/i.test(canonicalJson(contents)))
|
|
287
|
+
fail('blind_label_leak');
|
|
288
|
+
if (!Array.isArray(nonReviewableInput))
|
|
289
|
+
fail('non_reviewable_invalid');
|
|
290
|
+
const allowedOutcomes = ['completed', 'hard-failure', 'hard-gate-failed', 'timeout', 'abandoned'];
|
|
291
|
+
const nonReviewable = nonReviewableInput.map((item) => { const entry = record(item); exactKeys(entry, ['caseId', 'baselineOutcome', 'stage1Outcome']); text(entry.caseId); if (!allowedOutcomes.includes(String(entry.baselineOutcome)) || !allowedOutcomes.includes(String(entry.stage1Outcome)))
|
|
292
|
+
fail('non_reviewable_invalid'); return entry; });
|
|
293
|
+
const completedCases = protocol.cases.filter((item) => runInputs.filter((run) => run.caseId === item.caseId && effectiveRunOutcome(run) === 'completed').length === 2).map((item) => item.caseId);
|
|
294
|
+
const incompleteCases = protocol.cases.map((item) => item.caseId).filter((id) => !completedCases.includes(id));
|
|
295
|
+
if (contents.length !== completedCases.length || new Set(contents.map((item) => item.caseId)).size !== completedCases.length || contents.some((item) => !completedCases.includes(item.caseId)))
|
|
296
|
+
fail('blind_case_set_mismatch');
|
|
297
|
+
if (nonReviewable.length !== incompleteCases.length || new Set(nonReviewable.map((item) => item.caseId)).size !== incompleteCases.length || nonReviewable.some((item) => !incompleteCases.includes(item.caseId)))
|
|
298
|
+
fail('non_reviewable_mismatch');
|
|
299
|
+
for (const entry of nonReviewable)
|
|
300
|
+
for (const arm of ['baseline', 'stage1']) {
|
|
301
|
+
const run = runInputs.find((candidate) => candidate.caseId === entry.caseId && candidate.arm === arm);
|
|
302
|
+
if (!run || effectiveRunOutcome(run) !== entry[`${arm}Outcome`])
|
|
303
|
+
fail('non_reviewable_mismatch');
|
|
304
|
+
}
|
|
305
|
+
for (const entry of contents) {
|
|
306
|
+
for (const label of ['A', 'B']) {
|
|
307
|
+
const arm = mapping.labels[label];
|
|
308
|
+
const run = runInputs.find((item) => item.caseId === entry.caseId && item.arm === arm && effectiveRunOutcome(item) === 'completed');
|
|
309
|
+
if (!run || run.outputDigest !== sha256Text(entry[label]))
|
|
310
|
+
fail('blind_content_digest_mismatch');
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const base = { kind: 'hyv-stage1-blind-packet', version: '1', protocolDigest: protocol.protocolDigest, candidateDigest: candidateDigest(protocol), runsDigest: runSummary.runsDigest, mappingDigest: sha256Canonical(mappingCore(mapping)), contentDigest: sha256Canonical(contents), nonReviewableDigest: sha256Canonical(nonReviewable), reviewableCount: contents.length, nonReviewableCount: nonReviewable.length, encryptedAtRest: true, approvedStorage: true };
|
|
314
|
+
return { packet: { ...base, packetDigest: sha256Canonical(base) } };
|
|
315
|
+
}
|
|
316
|
+
function validateAttestation(value, artifactDigest, protocolDigest, packetDigest) {
|
|
317
|
+
const item = record(value, 'human_attestation_required');
|
|
318
|
+
exactKeys(item, ['kind', 'version', 'verified', 'artifactDigest', 'trustStoreDigest', 'keyId', 'purpose', 'protocolDigest', 'packetDigest', 'nonce', 'issuedAt', 'expiresAt', 'verifiedAt']);
|
|
319
|
+
if (item.kind !== 'external-verification-receipt' || item.version !== '1' || item.verified !== true)
|
|
320
|
+
fail('human_attestation_required');
|
|
321
|
+
if (artifactDigest && item.artifactDigest !== artifactDigest)
|
|
322
|
+
fail('attestation_binding_mismatch');
|
|
323
|
+
else
|
|
324
|
+
digest(item.artifactDigest, 'human_attestation_required');
|
|
325
|
+
digest(item.trustStoreDigest, 'human_attestation_required');
|
|
326
|
+
text(item.keyId, 'human_attestation_required');
|
|
327
|
+
text(item.purpose, 'human_attestation_required');
|
|
328
|
+
text(item.nonce, 'human_attestation_required');
|
|
329
|
+
if (protocolDigest && item.protocolDigest !== protocolDigest)
|
|
330
|
+
fail('attestation_binding_mismatch');
|
|
331
|
+
else
|
|
332
|
+
digest(item.protocolDigest, 'human_attestation_required');
|
|
333
|
+
if (packetDigest && item.packetDigest !== packetDigest)
|
|
334
|
+
fail('attestation_binding_mismatch');
|
|
335
|
+
else
|
|
336
|
+
digest(item.packetDigest, 'human_attestation_required');
|
|
337
|
+
for (const key of ['issuedAt', 'expiresAt', 'verifiedAt'])
|
|
338
|
+
timestamp(item[key], 'human_attestation_required');
|
|
339
|
+
if (Date.parse(item.expiresAt) <= Date.parse(item.issuedAt) || Date.parse(item.verifiedAt) < Date.parse(item.issuedAt) || Date.parse(item.verifiedAt) > Date.parse(item.expiresAt))
|
|
340
|
+
fail('human_attestation_required');
|
|
341
|
+
}
|
|
342
|
+
function parseRating(packet, input) {
|
|
343
|
+
const value = record(input);
|
|
344
|
+
const baseKeys = ['kind', 'version', 'evidenceClass', 'protocolDigest', 'candidateDigest', 'packetDigest', 'reviewerId', 'identityKey', 'recordId', 'sequence', 'previousRecordDigest', 'recordDigest', 'caseId', 'workflow', 'attestation'];
|
|
345
|
+
exactKeys(value, value.workflow === 'completed' ? [...baseKeys, 'preferredLabel', 'correctionVersusConfirm'] : baseKeys);
|
|
346
|
+
if (value.kind !== 'hyv-stage1-reviewer-record' || value.version !== '1')
|
|
347
|
+
fail('reviewer_record_version_invalid');
|
|
348
|
+
if (value.protocolDigest !== packet.protocolDigest || value.candidateDigest !== packet.candidateDigest || value.packetDigest !== packet.packetDigest)
|
|
349
|
+
fail('reviewer_digest_chain_mismatch');
|
|
350
|
+
const reviewerId = text(value.reviewerId);
|
|
351
|
+
text(value.caseId);
|
|
352
|
+
digest(value.identityKey, 'reviewer_identity_invalid');
|
|
353
|
+
digest(value.recordId, 'reviewer_record_id_invalid');
|
|
354
|
+
digest(value.recordDigest, 'reviewer_record_digest_invalid');
|
|
355
|
+
if (value.identityKey !== sha256Canonical({ reviewerId }))
|
|
356
|
+
fail('reviewer_identity_mismatch');
|
|
357
|
+
if (value.recordId !== sha256Canonical({ protocolDigest: packet.protocolDigest, packetDigest: packet.packetDigest, identityKey: value.identityKey, caseId: value.caseId }))
|
|
358
|
+
fail('reviewer_record_id_mismatch');
|
|
359
|
+
if (!Number.isInteger(value.sequence) || value.sequence < 1)
|
|
360
|
+
fail('reviewer_sequence_invalid');
|
|
361
|
+
if (value.previousRecordDigest !== null)
|
|
362
|
+
digest(value.previousRecordDigest, 'previous_record_digest_invalid');
|
|
363
|
+
if (!['completed', 'abandoned'].includes(String(value.workflow)))
|
|
364
|
+
fail('workflow_invalid');
|
|
365
|
+
if (value.workflow === 'completed') {
|
|
366
|
+
if (!['A', 'B', 'tie'].includes(String(value.preferredLabel)))
|
|
367
|
+
fail('preference_invalid');
|
|
368
|
+
const correction = record(value.correctionVersusConfirm);
|
|
369
|
+
exactKeys(correction, ['A', 'B']);
|
|
370
|
+
if (!['correction', 'confirm'].includes(String(correction.A)) || !['correction', 'confirm'].includes(String(correction.B)))
|
|
371
|
+
fail('correction_measure_invalid');
|
|
372
|
+
}
|
|
373
|
+
const { attestation, recordDigest, ...core } = value;
|
|
374
|
+
if (recordDigest !== sha256Canonical(core))
|
|
375
|
+
fail('reviewer_record_digest_mismatch');
|
|
376
|
+
if (value.evidenceClass === 'human') {
|
|
377
|
+
validateAttestation(attestation, recordDigest, packet.protocolDigest, packet.packetDigest);
|
|
378
|
+
if (attestation.purpose !== 'stage1-blind-review')
|
|
379
|
+
fail('attestation_purpose_mismatch');
|
|
380
|
+
}
|
|
381
|
+
else if (value.evidenceClass !== 'synthetic-dry-run' || attestation !== null)
|
|
382
|
+
fail('evidence_class_mismatch');
|
|
383
|
+
return value;
|
|
384
|
+
}
|
|
385
|
+
export function recordRating(packetInput, existingInput, ratingInput) {
|
|
386
|
+
const packet = parsePacket(packetInput);
|
|
387
|
+
if (!Array.isArray(existingInput))
|
|
388
|
+
fail('reviewer_log_invalid');
|
|
389
|
+
const existing = existingInput.map((item) => parseRating(packet, item));
|
|
390
|
+
const rating = parseRating(packet, ratingInput);
|
|
391
|
+
const same = existing.find((item) => item.identityKey === rating.identityKey && item.caseId === rating.caseId);
|
|
392
|
+
if (same)
|
|
393
|
+
fail(same.preferredLabel === rating.preferredLabel && (same.correctionVersusConfirm === undefined ? rating.correctionVersusConfirm === undefined : sha256Canonical(same.correctionVersusConfirm) === sha256Canonical(rating.correctionVersusConfirm)) && same.workflow === rating.workflow ? 'duplicate_reviewer_record' : 'conflicting_reviewer_record');
|
|
394
|
+
for (let index = 0; index < existing.length; index += 1) {
|
|
395
|
+
if (existing[index].sequence !== index + 1 || existing[index].previousRecordDigest !== (index === 0 ? null : existing[index - 1].recordDigest))
|
|
396
|
+
fail('reviewer_chain_invalid');
|
|
397
|
+
}
|
|
398
|
+
if (rating.sequence !== existing.length + 1 || rating.previousRecordDigest !== (existing.length === 0 ? null : existing[existing.length - 1].recordDigest))
|
|
399
|
+
fail('reviewer_chain_invalid');
|
|
400
|
+
return [...existing, rating];
|
|
401
|
+
}
|
|
402
|
+
function validateReviewerLog(protocol, packet, ratingsInput, requireComplete) {
|
|
403
|
+
if (!Array.isArray(ratingsInput))
|
|
404
|
+
fail('reviewer_log_invalid');
|
|
405
|
+
const ratings = ratingsInput.map((item) => parseRating(packet, item));
|
|
406
|
+
for (let index = 0; index < ratings.length; index += 1) {
|
|
407
|
+
if (ratings[index].sequence !== index + 1 || ratings[index].previousRecordDigest !== (index === 0 ? null : ratings[index - 1].recordDigest))
|
|
408
|
+
fail('reviewer_chain_invalid');
|
|
409
|
+
if (protocol && !protocol.cases.some((item) => item.caseId === ratings[index].caseId))
|
|
410
|
+
fail('reviewer_case_unknown');
|
|
411
|
+
}
|
|
412
|
+
const pairs = ratings.map((item) => `${item.identityKey}\0${item.caseId}`);
|
|
413
|
+
if (new Set(pairs).size !== pairs.length)
|
|
414
|
+
fail('duplicate_reviewer_record');
|
|
415
|
+
if (protocol && requireComplete) {
|
|
416
|
+
const identities = new Set(ratings.map((item) => item.identityKey));
|
|
417
|
+
if (identities.size !== protocol.intentToTreat.expectedReviewers || ratings.length !== protocol.intentToTreat.expectedReviewers * protocol.cases.length)
|
|
418
|
+
fail('reviewer_matrix_mismatch');
|
|
419
|
+
for (const identity of identities)
|
|
420
|
+
for (const item of protocol.cases)
|
|
421
|
+
if (!pairs.includes(`${identity}\0${item.caseId}`))
|
|
422
|
+
fail('reviewer_matrix_mismatch');
|
|
423
|
+
}
|
|
424
|
+
return ratings;
|
|
425
|
+
}
|
|
426
|
+
export function sealRatings(packetInput, mappingInput, ratingsInput) {
|
|
427
|
+
const packet = parsePacket(packetInput);
|
|
428
|
+
const mapping = parseMapping(mappingInput);
|
|
429
|
+
if (sha256Canonical(mappingCore(mapping)) !== packet.mappingDigest)
|
|
430
|
+
fail('mapping_digest_mismatch');
|
|
431
|
+
const ratings = validateReviewerLog(undefined, packet, ratingsInput, false);
|
|
432
|
+
const base = { kind: 'hyv-stage1-ratings-seal', version: '1', protocolDigest: packet.protocolDigest, candidateDigest: packet.candidateDigest, packetDigest: packet.packetDigest, mappingDigest: packet.mappingDigest, ratingsDigest: sha256Canonical(ratings), recordCount: ratings.length };
|
|
433
|
+
return { ...base, sealDigest: sha256Canonical(base) };
|
|
434
|
+
}
|
|
435
|
+
export function reduceEvaluation(protocolInput, runsInput, packetInput, mappingInput, ratingsInput, sealInput, releaseAuditInput) {
|
|
436
|
+
const protocol = assertCommittedProtocol(protocolInput);
|
|
437
|
+
const summary = validateRuns(protocol, runsInput);
|
|
438
|
+
const packet = parsePacket(packetInput);
|
|
439
|
+
if (packet.protocolDigest !== protocol.protocolDigest || packet.candidateDigest !== candidateDigest(protocol) || packet.runsDigest !== summary.runsDigest)
|
|
440
|
+
fail('packet_digest_chain_mismatch');
|
|
441
|
+
const runRows = runsInput;
|
|
442
|
+
const expectedReviewableCount = protocol.cases.filter((entry) => runRows.filter((run) => run.caseId === entry.caseId && effectiveRunOutcome(run) === 'completed').length === 2).length;
|
|
443
|
+
if (packet.reviewableCount !== expectedReviewableCount || packet.nonReviewableCount !== protocol.cases.length - expectedReviewableCount)
|
|
444
|
+
fail('blind_packet_count_mismatch');
|
|
445
|
+
const mapping = parseMapping(mappingInput);
|
|
446
|
+
validateMappingOpening(protocol, mapping);
|
|
447
|
+
const ratings = validateReviewerLog(protocol, packet, ratingsInput, true);
|
|
448
|
+
const seal = sealRatings(packet, mapping, ratings);
|
|
449
|
+
if (sha256Canonical(seal) !== sha256Canonical(sealInput))
|
|
450
|
+
fail('ratings_seal_mismatch');
|
|
451
|
+
const audit = record(releaseAuditInput);
|
|
452
|
+
exactKeys(audit, ['kind', 'version', 'candidateCommit', 'protocolDigest', 'contractDigest', 'passed', 'digest']);
|
|
453
|
+
const { digest: auditDigest, ...auditCore } = audit;
|
|
454
|
+
if (auditDigest !== sha256Canonical(auditCore))
|
|
455
|
+
fail('release_audit_digest_mismatch');
|
|
456
|
+
if (audit.kind !== 'hyv-release-audit' || audit.version !== '1' || audit.candidateCommit !== protocol.stage1.sourceCommit || audit.protocolDigest !== protocol.protocolDigest || audit.contractDigest !== protocol.releaseAuditContractDigest || audit.passed !== true)
|
|
457
|
+
fail('release_audit_binding_mismatch');
|
|
458
|
+
const blockers = ['human_writer_evidence_deferred'];
|
|
459
|
+
if (protocol.benchmark.synthetic || ratings.some((r) => r.evidenceClass === 'synthetic-dry-run'))
|
|
460
|
+
blockers.push('synthetic_fixture_evidence');
|
|
461
|
+
if (protocol.mode !== 'locked-human')
|
|
462
|
+
blockers.push('locked_human_evidence_required');
|
|
463
|
+
if (protocol.mode === 'locked-human') {
|
|
464
|
+
blockers.push('reviewer_roster_verification_deferred');
|
|
465
|
+
const { attestation, ...openingCore } = mapping;
|
|
466
|
+
try {
|
|
467
|
+
validateAttestation(attestation, sha256Canonical(openingCore), protocol.protocolDigest, packet.packetDigest);
|
|
468
|
+
if (attestation.purpose !== 'stage1-mapping-custody')
|
|
469
|
+
throw new Error('purpose');
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
blockers.push('mapping_custody_attestation_required');
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (ratings.length !== protocol.intentToTreat.expectedReviewers * protocol.cases.length)
|
|
476
|
+
blockers.push('reviewer_denominator_mismatch');
|
|
477
|
+
if (protocol.cases.length < protocol.analysis.minimumCases || ratings.length < protocol.analysis.minimumRatings)
|
|
478
|
+
blockers.push('preregistered_minimum_not_met');
|
|
479
|
+
if (runsInput.some((run) => run.hardGates && Object.values(run.hardGates).some((passed) => !passed)))
|
|
480
|
+
blockers.push('hard_gate_regression');
|
|
481
|
+
const completedRatings = ratings.filter((rating) => rating.workflow === 'completed');
|
|
482
|
+
const preferred = completedRatings.reduce((total, rating) => total + (rating.preferredLabel === 'tie' ? 0.5 : mapping.labels[rating.preferredLabel] === 'stage1' ? 1 : 0), 0);
|
|
483
|
+
const preferenceDenominator = protocol.intentToTreat.expectedReviewers * protocol.cases.length;
|
|
484
|
+
const preferenceRate = preferenceDenominator ? preferred / preferenceDenominator : 0;
|
|
485
|
+
const stage1Confirm = completedRatings.filter((rating) => rating.correctionVersusConfirm[mapping.labels.A === 'stage1' ? 'A' : 'B'] === 'confirm').length;
|
|
486
|
+
const baselineConfirm = completedRatings.filter((rating) => rating.correctionVersusConfirm[mapping.labels.A === 'baseline' ? 'A' : 'B'] === 'confirm').length;
|
|
487
|
+
const correctionMarginMet = (stage1Confirm - baselineConfirm) / preferenceDenominator >= protocol.analysis.margin;
|
|
488
|
+
if (preferenceRate < 0.5 + protocol.analysis.margin && !correctionMarginMet)
|
|
489
|
+
blockers.push('checkpoint_threshold_not_met');
|
|
490
|
+
const workflowByArm = (arm) => ({ completed: runRows.filter((run) => run.arm === arm && effectiveRunOutcome(run) === 'completed').length, denominator: runRows.filter((run) => run.arm === arm).length });
|
|
491
|
+
const baselineWorkflow = workflowByArm('baseline');
|
|
492
|
+
const stage1Workflow = workflowByArm('stage1');
|
|
493
|
+
if (stage1Workflow.denominator === 0 || baselineWorkflow.denominator === 0 || stage1Workflow.completed / stage1Workflow.denominator < baselineWorkflow.completed / baselineWorkflow.denominator)
|
|
494
|
+
blockers.push('workflow_regression');
|
|
495
|
+
const stage1Correction = completedRatings.filter((rating) => rating.correctionVersusConfirm[mapping.labels.A === 'stage1' ? 'A' : 'B'] === 'correction').length;
|
|
496
|
+
const baselineCorrection = completedRatings.filter((rating) => rating.correctionVersusConfirm[mapping.labels.A === 'baseline' ? 'A' : 'B'] === 'correction').length;
|
|
497
|
+
const metrics = {
|
|
498
|
+
preference: { numerator: preferred, denominator: preferenceDenominator, rate: preferenceRate, uncertainty95: wilson95(preferred, preferenceDenominator) },
|
|
499
|
+
correctionVersusConfirm: { stage1: { corrections: stage1Correction, confirms: stage1Confirm, denominator: preferenceDenominator, correctionRate: stage1Correction / preferenceDenominator, uncertainty95: wilson95(stage1Correction, preferenceDenominator) }, baseline: { corrections: baselineCorrection, confirms: baselineConfirm, denominator: preferenceDenominator, correctionRate: baselineCorrection / preferenceDenominator, uncertainty95: wilson95(baselineCorrection, preferenceDenominator) } },
|
|
500
|
+
completion: { numerator: completedRatings.length, denominator: preferenceDenominator, rate: completedRatings.length / preferenceDenominator, uncertainty95: wilson95(completedRatings.length, preferenceDenominator) },
|
|
501
|
+
abandonment: { numerator: ratings.length - completedRatings.length, denominator: preferenceDenominator, rate: (ratings.length - completedRatings.length) / preferenceDenominator, uncertainty95: wilson95(ratings.length - completedRatings.length, preferenceDenominator) },
|
|
502
|
+
providerRuns: { completed: summary.completed, abandoned: summary.abandonments, hardFailures: summary.hardFailures, timeouts: summary.timeouts, denominator: summary.denominator },
|
|
503
|
+
};
|
|
504
|
+
const intentToTreat = { ...summary, expectedRatings: preferenceDenominator, observedRatings: ratings.length, missingRatings: Math.max(0, preferenceDenominator - ratings.length), reconciled: summary.denominator === protocol.intentToTreat.expectedAssignments && ratings.length === preferenceDenominator };
|
|
505
|
+
const base = { kind: 'hyv-stage1-evaluation-report', version: '1', protocolDigest: protocol.protocolDigest, candidateDigest: candidateDigest(protocol), runsDigest: summary.runsDigest, packetDigest: packet.packetDigest, mappingDigest: packet.mappingDigest, sealDigest: seal.sealDigest, releaseAuditDigest: audit.digest, intentToTreat, metrics, decision: blockers.length ? 'BLOCKED' : 'PASS', promotable: blockers.length === 0, blockers };
|
|
506
|
+
return { ...base, reportDigest: sha256Canonical(base) };
|
|
507
|
+
}
|
|
508
|
+
export function preflight(protocolInput) {
|
|
509
|
+
const protocol = assertCommittedProtocol(protocolInput);
|
|
510
|
+
return { ready: true, protocolDigest: protocol.protocolDigest, humanEvidenceRequired: protocol.mode === 'locked-human' };
|
|
511
|
+
}
|
|
512
|
+
export function recordCheckpointDisposition(reportInput, disposition, attestation) {
|
|
513
|
+
const report = record(reportInput);
|
|
514
|
+
exactKeys(report, ['kind', 'version', 'protocolDigest', 'candidateDigest', 'runsDigest', 'packetDigest', 'mappingDigest', 'sealDigest', 'releaseAuditDigest', 'intentToTreat', 'metrics', 'decision', 'promotable', 'blockers', 'reportDigest']);
|
|
515
|
+
if (report.kind !== 'hyv-stage1-evaluation-report' || report.version !== '1')
|
|
516
|
+
fail('report_contract_invalid');
|
|
517
|
+
for (const field of ['protocolDigest', 'candidateDigest', 'runsDigest', 'packetDigest', 'mappingDigest', 'sealDigest', 'releaseAuditDigest'])
|
|
518
|
+
digest(report[field], 'report_digest_chain_invalid');
|
|
519
|
+
if (report.decision !== 'BLOCKED' || report.promotable !== false)
|
|
520
|
+
fail('report_must_be_blocked');
|
|
521
|
+
if (!Array.isArray(report.blockers))
|
|
522
|
+
fail('report_blockers_invalid');
|
|
523
|
+
const blockers = report.blockers;
|
|
524
|
+
if (!blockers.every((blocker) => typeof blocker === 'string') || !blockers.includes('human_writer_evidence_deferred'))
|
|
525
|
+
fail('report_blockers_invalid');
|
|
526
|
+
const intentToTreat = record(report.intentToTreat, 'report_intent_to_treat_invalid');
|
|
527
|
+
exactKeys(intentToTreat, ['denominator', 'completed', 'hardFailures', 'timeouts', 'abandonments', 'runsDigest', 'expectedRatings', 'observedRatings', 'missingRatings', 'reconciled']);
|
|
528
|
+
for (const field of ['denominator', 'completed', 'hardFailures', 'timeouts', 'abandonments', 'expectedRatings', 'observedRatings', 'missingRatings'])
|
|
529
|
+
count(intentToTreat[field], 'report_intent_to_treat_invalid');
|
|
530
|
+
digest(intentToTreat.runsDigest, 'report_intent_to_treat_invalid');
|
|
531
|
+
if (typeof intentToTreat.reconciled !== 'boolean')
|
|
532
|
+
fail('report_intent_to_treat_invalid');
|
|
533
|
+
const metrics = record(report.metrics, 'report_metrics_invalid');
|
|
534
|
+
exactKeys(metrics, ['preference', 'correctionVersusConfirm', 'completion', 'abandonment', 'providerRuns']);
|
|
535
|
+
const rateMetric = (value, fields) => {
|
|
536
|
+
const metric = record(value, 'report_metrics_invalid');
|
|
537
|
+
exactKeys(metric, fields);
|
|
538
|
+
for (const field of fields) {
|
|
539
|
+
if (field === 'uncertainty95') {
|
|
540
|
+
if (!Array.isArray(metric[field]) || metric[field].length !== 2)
|
|
541
|
+
fail('report_metrics_invalid');
|
|
542
|
+
for (const bound of metric[field])
|
|
543
|
+
finiteNumber(bound, 0, 1, 'report_metrics_invalid');
|
|
544
|
+
}
|
|
545
|
+
else if (field === 'rate' || field === 'correctionRate')
|
|
546
|
+
finiteNumber(metric[field], 0, 1, 'report_metrics_invalid');
|
|
547
|
+
else
|
|
548
|
+
count(metric[field], 'report_metrics_invalid');
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
rateMetric(metrics.preference, ['numerator', 'denominator', 'rate', 'uncertainty95']);
|
|
552
|
+
rateMetric(metrics.completion, ['numerator', 'denominator', 'rate', 'uncertainty95']);
|
|
553
|
+
rateMetric(metrics.abandonment, ['numerator', 'denominator', 'rate', 'uncertainty95']);
|
|
554
|
+
const correction = record(metrics.correctionVersusConfirm, 'report_metrics_invalid');
|
|
555
|
+
exactKeys(correction, ['stage1', 'baseline']);
|
|
556
|
+
rateMetric(correction.stage1, ['corrections', 'confirms', 'denominator', 'correctionRate', 'uncertainty95']);
|
|
557
|
+
rateMetric(correction.baseline, ['corrections', 'confirms', 'denominator', 'correctionRate', 'uncertainty95']);
|
|
558
|
+
const providerRuns = record(metrics.providerRuns, 'report_metrics_invalid');
|
|
559
|
+
exactKeys(providerRuns, ['completed', 'abandoned', 'hardFailures', 'timeouts', 'denominator']);
|
|
560
|
+
for (const field of ['completed', 'abandoned', 'hardFailures', 'timeouts', 'denominator'])
|
|
561
|
+
count(providerRuns[field], 'report_metrics_invalid');
|
|
562
|
+
const reportDigest = digest(report.reportDigest, 'report_digest_invalid');
|
|
563
|
+
const { reportDigest: _reportDigest, ...reportCore } = report;
|
|
564
|
+
if (reportDigest !== sha256Canonical(reportCore))
|
|
565
|
+
fail('report_digest_mismatch');
|
|
566
|
+
const protocolDigest = digest(report.protocolDigest, 'report_digest_chain_invalid');
|
|
567
|
+
const packetDigest = digest(report.packetDigest, 'report_digest_chain_invalid');
|
|
568
|
+
const releaseAuditDigest = digest(report.releaseAuditDigest, 'report_digest_chain_invalid');
|
|
569
|
+
if (!['PROCEED_TO_MAR_363', 'STOP', 'REPEAT_PROTOCOL'].includes(String(disposition)))
|
|
570
|
+
fail('checkpoint_disposition_invalid');
|
|
571
|
+
if (disposition === 'PROCEED_TO_MAR_363')
|
|
572
|
+
fail('human_evidence_deferred');
|
|
573
|
+
const base = { kind: 'hyv-stage1-checkpoint-disposition', version: '1', protocolDigest, packetDigest, releaseAuditDigest, reportDigest, disposition };
|
|
574
|
+
validateAttestation(attestation, sha256Canonical(base), protocolDigest, packetDigest);
|
|
575
|
+
if (attestation.purpose !== 'stage1-checkpoint-disposition')
|
|
576
|
+
fail('attestation_purpose_mismatch');
|
|
577
|
+
const artifact = { ...base, attestation };
|
|
578
|
+
return { ...artifact, dispositionDigest: sha256Canonical(artifact) };
|
|
579
|
+
}
|