@actuarial-ts/compliance 0.6.0 → 0.7.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 +19 -9
- package/dist/bundle.d.ts +1 -1
- package/dist/bundle.d.ts.map +1 -1
- package/dist/bundle.js +163 -21
- package/dist/bundle.js.map +1 -1
- package/dist/diagnosticArtifactStream.d.ts +30 -0
- package/dist/diagnosticArtifactStream.d.ts.map +1 -0
- package/dist/diagnosticArtifactStream.js +128 -0
- package/dist/diagnosticArtifactStream.js.map +1 -0
- package/dist/diagnosticByteSource.d.ts +10 -0
- package/dist/diagnosticByteSource.d.ts.map +1 -0
- package/dist/diagnosticByteSource.js +70 -0
- package/dist/diagnosticByteSource.js.map +1 -0
- package/dist/diagnosticByteView.d.ts +3 -0
- package/dist/diagnosticByteView.d.ts.map +1 -0
- package/dist/diagnosticByteView.js +27 -0
- package/dist/diagnosticByteView.js.map +1 -0
- package/dist/diagnosticCompactRun.d.ts +43 -0
- package/dist/diagnosticCompactRun.d.ts.map +1 -0
- package/dist/diagnosticCompactRun.js +253 -0
- package/dist/diagnosticCompactRun.js.map +1 -0
- package/dist/diagnosticReplayFrames.d.ts +10 -0
- package/dist/diagnosticReplayFrames.d.ts.map +1 -0
- package/dist/diagnosticReplayFrames.js +99 -0
- package/dist/diagnosticReplayFrames.js.map +1 -0
- package/dist/diagnosticReplayProtocol.d.ts +17 -0
- package/dist/diagnosticReplayProtocol.d.ts.map +1 -0
- package/dist/diagnosticReplayProtocol.js +98 -0
- package/dist/diagnosticReplayProtocol.js.map +1 -0
- package/dist/diagnosticReplayReader.d.ts +41 -0
- package/dist/diagnosticReplayReader.d.ts.map +1 -0
- package/dist/diagnosticReplayReader.js +346 -0
- package/dist/diagnosticReplayReader.js.map +1 -0
- package/dist/diagnosticReplayValue.d.ts +34 -0
- package/dist/diagnosticReplayValue.d.ts.map +1 -0
- package/dist/diagnosticReplayValue.js +269 -0
- package/dist/diagnosticReplayValue.js.map +1 -0
- package/dist/diagnosticReplayWriter.d.ts +25 -0
- package/dist/diagnosticReplayWriter.d.ts.map +1 -0
- package/dist/diagnosticReplayWriter.js +148 -0
- package/dist/diagnosticReplayWriter.js.map +1 -0
- package/dist/diagnosticRun.d.ts +52 -29
- package/dist/diagnosticRun.d.ts.map +1 -1
- package/dist/diagnosticRun.js +564 -174
- package/dist/diagnosticRun.js.map +1 -1
- package/dist/diagnosticRunEvidence.d.ts +18 -0
- package/dist/diagnosticRunEvidence.d.ts.map +1 -0
- package/dist/diagnosticRunEvidence.js +195 -0
- package/dist/diagnosticRunEvidence.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/ledger.d.ts.map +1 -1
- package/dist/ledger.js +47 -6
- package/dist/ledger.js.map +1 -1
- package/dist/metadata.d.ts.map +1 -1
- package/dist/metadata.js +27 -3
- package/dist/metadata.js.map +1 -1
- package/dist/sha256Stream.d.ts +6 -0
- package/dist/sha256Stream.d.ts.map +1 -0
- package/dist/sha256Stream.js +103 -0
- package/dist/sha256Stream.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -5
- package/src/bundle.ts +272 -32
- package/src/diagnosticArtifactStream.ts +148 -0
- package/src/diagnosticByteSource.ts +82 -0
- package/src/diagnosticByteView.ts +27 -0
- package/src/diagnosticCompactRun.ts +462 -0
- package/src/diagnosticReplayFrames.ts +99 -0
- package/src/diagnosticReplayProtocol.ts +99 -0
- package/src/diagnosticReplayReader.ts +428 -0
- package/src/diagnosticReplayValue.ts +269 -0
- package/src/diagnosticReplayWriter.ts +190 -0
- package/src/diagnosticRun.ts +1030 -93
- package/src/diagnosticRunEvidence.ts +391 -0
- package/src/index.ts +10 -0
- package/src/ledger.ts +59 -8
- package/src/metadata.ts +48 -7
- package/src/sha256Stream.ts +108 -0
- package/src/version.ts +1 -1
package/dist/diagnosticRun.js
CHANGED
|
@@ -1,216 +1,606 @@
|
|
|
1
|
-
import { CORE_PACKAGE_VERSION, canonicalJson, fnv1a64, getMetricDiagnosticsResultIdentity, getPreparedDiagnosticDataIdentity, runMetricDiagnostics, verifyPreparedDiagnosticDataIntegrity, } from "@actuarial-ts/core";
|
|
2
|
-
import { DATA_PACKAGE_VERSION, assertCompletedValidatedMetricDiagnosticsRun, reviewPreparedDiagnosticData, } from "@actuarial-ts/data";
|
|
1
|
+
import { CORE_PACKAGE_VERSION, DiagnosticValidationError, canonicalJson, compileDiagnosticDefinition, fnv1a64, getMetricDiagnosticsResultIdentity, getPreparedDiagnosticDataIdentity, runMetricDiagnostics, verifyPreparedDiagnosticDataIntegrity, } from "@actuarial-ts/core";
|
|
2
|
+
import { DATA_PACKAGE_VERSION, assertCompletedValidatedMetricDiagnosticsRun, reviewPreparedDiagnosticData, runValidatedMetricDiagnostics, validateDiagnosticRunInput, } from "@actuarial-ts/data";
|
|
3
3
|
import { ComplianceError } from "./errors.js";
|
|
4
4
|
import { COMPLIANCE_PACKAGE_VERSION } from "./version.js";
|
|
5
|
+
import { token, plain, exactKeys, snapshotLineage, validateArtifactGraph, } from "./diagnosticRunEvidence.js";
|
|
5
6
|
const verified = new WeakMap();
|
|
6
|
-
|
|
7
|
-
if (value
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
7
|
+
function freeze(value, seen = new WeakSet()) {
|
|
8
|
+
if (value === null || typeof value !== "object" || seen.has(value))
|
|
9
|
+
return value;
|
|
10
|
+
seen.add(value);
|
|
11
|
+
for (const child of Object.values(value))
|
|
12
|
+
freeze(child, seen);
|
|
13
|
+
return Object.freeze(value);
|
|
14
|
+
}
|
|
15
|
+
function tag(kind, key, value) {
|
|
16
|
+
return `fnv1a64-jcs-v1:${fnv1a64(canonicalJson({ identityVersion: 1, kind, [key]: value }))}`;
|
|
17
|
+
}
|
|
18
|
+
function manifestIdentity(manifest) {
|
|
19
|
+
return {
|
|
20
|
+
...manifest,
|
|
21
|
+
executionPolicy: {
|
|
22
|
+
gate: manifest.executionPolicy.gate,
|
|
23
|
+
review: {
|
|
24
|
+
body: manifest.executionPolicy.review.identityBody,
|
|
25
|
+
reportFingerprint: manifest.executionPolicy.review.reportFingerprint,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function bindingTag(runFingerprint, resultFingerprint) {
|
|
31
|
+
return `fnv1a64-jcs-v1:${fnv1a64(canonicalJson({ identityVersion: 1, kind: "diagnostic-run-result", runFingerprint, resultFingerprint }))}`;
|
|
32
|
+
}
|
|
33
|
+
function snapshotArtifacts(evidence, scope, path) {
|
|
34
|
+
if (!Array.isArray(evidence))
|
|
35
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `${path} must be an array`, path);
|
|
26
36
|
const seen = new Set();
|
|
27
37
|
const result = [];
|
|
28
38
|
for (const [index, item] of evidence.entries()) {
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
const itemPath = `${path}[${index}]`;
|
|
40
|
+
if (!plain(item))
|
|
41
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact evidence must be a plain object", itemPath);
|
|
42
|
+
if (typeof item.id !== "string")
|
|
43
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact id must be a string", `${itemPath}.id`);
|
|
44
|
+
token(item.id, `${itemPath}.id`);
|
|
45
|
+
if (item.scope !== scope)
|
|
46
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Artifact scope must be ${scope}`, `${itemPath}.scope`);
|
|
31
47
|
if (seen.has(item.id))
|
|
32
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Duplicate artifact ID ${item.id}`,
|
|
48
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Duplicate artifact ID ${item.id}`, `${itemPath}.id`);
|
|
33
49
|
seen.add(item.id);
|
|
34
50
|
if (item.assurance === "sdk-computed") {
|
|
51
|
+
exactKeys(item, ["id", "scope", "assurance", "bytes"], itemPath);
|
|
35
52
|
if (!(item.bytes instanceof Uint8Array))
|
|
36
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "SDK-computed artifact evidence requires actual Uint8Array bytes",
|
|
53
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "SDK-computed artifact evidence requires actual Uint8Array bytes", `${itemPath}.bytes`);
|
|
37
54
|
const bytes = new Uint8Array(item.bytes.byteLength);
|
|
38
55
|
bytes.set(item.bytes);
|
|
39
|
-
result.push({ id: item.id, scope
|
|
56
|
+
result.push({ id: item.id, scope, assurance: item.assurance, bytes });
|
|
57
|
+
}
|
|
58
|
+
else if (item.assurance === "caller-declared") {
|
|
59
|
+
exactKeys(item, ["id", "scope", "assurance", "algorithm", "value"], itemPath);
|
|
60
|
+
if (typeof item.algorithm !== "string")
|
|
61
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact algorithm must be a string", `${itemPath}.algorithm`);
|
|
62
|
+
if (typeof item.value !== "string")
|
|
63
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact value must be a string", `${itemPath}.value`);
|
|
64
|
+
token(item.algorithm, `${itemPath}.algorithm`);
|
|
65
|
+
token(item.value, `${itemPath}.value`);
|
|
66
|
+
result.push({
|
|
67
|
+
id: item.id,
|
|
68
|
+
scope,
|
|
69
|
+
assurance: item.assurance,
|
|
70
|
+
algorithm: item.algorithm,
|
|
71
|
+
value: item.value,
|
|
72
|
+
});
|
|
40
73
|
}
|
|
41
|
-
else {
|
|
42
|
-
token(item.algorithm, `$.artifacts[${index}].algorithm`);
|
|
43
|
-
token(item.value, `$.artifacts[${index}].value`);
|
|
44
|
-
if (!Number.isSafeInteger(item.byteLength) || item.byteLength < 0)
|
|
45
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact byteLength must be a nonnegative safe integer", `$.artifacts[${index}].byteLength`);
|
|
46
|
-
result.push({ ...item });
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return result.sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
50
|
-
}
|
|
51
|
-
async function digestArtifacts(snapshot) {
|
|
52
|
-
if (snapshot.some((item) => "bytes" in item) && globalThis.crypto?.subtle === undefined)
|
|
53
|
-
throw new ComplianceError("CRYPTO_UNAVAILABLE", "Web Crypto SHA-256 is unavailable", "$.artifacts");
|
|
54
|
-
return Promise.all(snapshot.map(async (item) => { if (!("bytes" in item))
|
|
55
|
-
return item; const hash = await globalThis.crypto.subtle.digest("SHA-256", item.bytes); return { id: item.id, scope: item.scope, assurance: item.assurance, algorithm: "sha-256", value: [...new Uint8Array(hash)].map((b) => b.toString(16).padStart(2, "0")).join(""), byteLength: item.bytes.byteLength }; }));
|
|
56
|
-
}
|
|
57
|
-
function snapshotLineage(lineage) {
|
|
58
|
-
return lineage.map((item, index) => { token(item.artifactId, `$.lineage[${index}].artifactId`); if (!Array.isArray(item.inputArtifactIds))
|
|
59
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Lineage inputs must be an array", `$.lineage[${index}].inputArtifactIds`); const inputs = item.inputArtifactIds.map((id, inputIndex) => { token(id, `$.lineage[${index}].inputArtifactIds[${inputIndex}]`); return id; }); if (new Set(inputs).size !== inputs.length)
|
|
60
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Lineage inputs must be unique", `$.lineage[${index}].inputArtifactIds`); return { artifactId: item.artifactId, inputArtifactIds: [...inputs].sort() }; }).sort((a, b) => a.artifactId < b.artifactId ? -1 : a.artifactId > b.artifactId ? 1 : 0);
|
|
61
|
-
}
|
|
62
|
-
function validateArtifactGraph(run, artifacts, lineage) {
|
|
63
|
-
const byId = new Map(artifacts.map((item) => [item.id, item]));
|
|
64
|
-
const referenced = new Set();
|
|
65
|
-
const requireArtifact = (id, scope, path) => { const artifact = byId.get(id); if (!artifact)
|
|
66
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Artifact reference ${id} is unresolved`, path); if (artifact.scope !== scope)
|
|
67
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Artifact ${id} must have ${scope} scope`, path); referenced.add(id); };
|
|
68
|
-
let unsourced = false;
|
|
69
|
-
for (const [index, item] of run.prepared.inputAudit.entries()) {
|
|
70
|
-
const source = item.record.source;
|
|
71
|
-
if (source)
|
|
72
|
-
requireArtifact(source.artifactId, "input", `$.completedRun.prepared.inputAudit[${index}].record.source.artifactId`);
|
|
73
74
|
else
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
referenced.add(run.datasetArtifactId);
|
|
98
|
-
}
|
|
99
|
-
else if (run.datasetArtifactId !== null)
|
|
100
|
-
requireArtifact(run.datasetArtifactId, "input", "$.completedRun.datasetArtifactId");
|
|
101
|
-
for (const [basisIndex, basis] of run.prepared.definition.definition.amountBases.entries())
|
|
102
|
-
for (const [componentIndex, component] of basis.components.entries())
|
|
103
|
-
if (component.limitation.kind !== "unlimited" && component.limitation.kind !== "unknown" && component.limitation.derivation.kind === "external")
|
|
104
|
-
requireArtifact(component.limitation.derivation.transformationRef, "preparation", `$.definition.amountBases[${basisIndex}].components[${componentIndex}].limitation.derivation.transformationRef`);
|
|
105
|
-
for (const [ruleIndex, rule] of run.prepared.definition.definition.reviewRules.entries())
|
|
106
|
-
if (rule.kind === "layer-order" && rule.comparability.kind === "caller-asserted")
|
|
107
|
-
requireArtifact(rule.comparability.rationaleArtifactId, "preparation", `$.definition.reviewRules[${ruleIndex}].comparability.rationaleArtifactId`);
|
|
108
|
-
if (run.gate.rationaleRef !== null)
|
|
109
|
-
requireArtifact(run.gate.rationaleRef, "preparation", "$.completedRun.gate.rationaleRef");
|
|
110
|
-
const edges = new Map();
|
|
111
|
-
for (const [index, edge] of lineage.entries()) {
|
|
112
|
-
if (edges.has(edge.artifactId))
|
|
113
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "An artifact may have only one producing lineage edge", `$.lineage[${index}].artifactId`);
|
|
114
|
-
const downstream = byId.get(edge.artifactId);
|
|
115
|
-
if (!downstream)
|
|
116
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Lineage artifact ${edge.artifactId} is unresolved`, `$.lineage[${index}].artifactId`);
|
|
117
|
-
if (downstream.scope !== "input")
|
|
118
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Lineage downstream artifact must have input scope", `$.lineage[${index}].artifactId`);
|
|
119
|
-
for (const [inputIndex, id] of edge.inputArtifactIds.entries()) {
|
|
120
|
-
if (id === edge.artifactId)
|
|
121
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Lineage may not reference itself", `$.lineage[${index}].inputArtifactIds[${inputIndex}]`);
|
|
122
|
-
if (!byId.has(id))
|
|
123
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Lineage reference ${id} is unresolved`, `$.lineage[${index}].inputArtifactIds[${inputIndex}]`);
|
|
124
|
-
}
|
|
125
|
-
edges.set(edge.artifactId, edge.inputArtifactIds);
|
|
126
|
-
}
|
|
127
|
-
const visiting = new Set(), visited = new Set();
|
|
128
|
-
const walk = (id, path) => { if (visiting.has(id))
|
|
129
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact lineage contains a cycle", path); if (visited.has(id))
|
|
130
|
-
return; visiting.add(id); for (const upstream of edges.get(id) ?? []) {
|
|
131
|
-
referenced.add(upstream);
|
|
132
|
-
walk(upstream, path);
|
|
133
|
-
} visiting.delete(id); visited.add(id); };
|
|
134
|
-
for (const id of [...referenced])
|
|
135
|
-
walk(id, "$.lineage");
|
|
136
|
-
for (const [index, artifact] of artifacts.entries())
|
|
137
|
-
if (!referenced.has(artifact.id))
|
|
138
|
-
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Artifact ${artifact.id} is orphaned`, `$.artifacts[${index}].id`);
|
|
75
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Unknown artifact assurance", `${itemPath}.assurance`);
|
|
76
|
+
}
|
|
77
|
+
return result.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
78
|
+
}
|
|
79
|
+
async function digestArtifacts(snapshot, path) {
|
|
80
|
+
if (snapshot.some((item) => "bytes" in item) &&
|
|
81
|
+
globalThis.crypto?.subtle === undefined)
|
|
82
|
+
throw new ComplianceError("CRYPTO_UNAVAILABLE", "Web Crypto SHA-256 is unavailable", path);
|
|
83
|
+
return Promise.all(snapshot.map(async (item) => {
|
|
84
|
+
if (!("bytes" in item))
|
|
85
|
+
return item;
|
|
86
|
+
const hash = await globalThis.crypto.subtle.digest("SHA-256", item.bytes);
|
|
87
|
+
return {
|
|
88
|
+
id: item.id,
|
|
89
|
+
scope: item.scope,
|
|
90
|
+
assurance: item.assurance,
|
|
91
|
+
algorithm: "sha256",
|
|
92
|
+
value: [...new Uint8Array(hash)]
|
|
93
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
94
|
+
.join(""),
|
|
95
|
+
byteLength: item.bytes.byteLength,
|
|
96
|
+
};
|
|
97
|
+
}));
|
|
139
98
|
}
|
|
140
99
|
function rerunAndVerify(run) {
|
|
141
100
|
assertCompletedValidatedMetricDiagnosticsRun(run);
|
|
142
101
|
verifyPreparedDiagnosticDataIntegrity(run.prepared);
|
|
143
|
-
const review = reviewPreparedDiagnosticData({
|
|
144
|
-
|
|
102
|
+
const review = reviewPreparedDiagnosticData({
|
|
103
|
+
prepared: run.prepared,
|
|
104
|
+
evidence: run.review.evidence,
|
|
105
|
+
});
|
|
106
|
+
if (review.reportFingerprint !== run.review.reportFingerprint ||
|
|
107
|
+
canonicalJson(review.identityBody) !==
|
|
108
|
+
canonicalJson(run.review.identityBody))
|
|
145
109
|
throw new ComplianceError("DIAGNOSTIC_MISMATCH", "Stored diagnostic review does not match a regenerated review", "$.review");
|
|
146
|
-
const rerun = runMetricDiagnostics({
|
|
147
|
-
|
|
110
|
+
const rerun = runMetricDiagnostics({
|
|
111
|
+
prepared: run.prepared,
|
|
112
|
+
groupMap: run.groupMap,
|
|
113
|
+
groupDimensions: run.groupDimensions,
|
|
114
|
+
});
|
|
115
|
+
if (canonicalJson(getMetricDiagnosticsResultIdentity(rerun)) !==
|
|
116
|
+
canonicalJson(getMetricDiagnosticsResultIdentity(run.result)))
|
|
148
117
|
throw new ComplianceError("DIAGNOSTIC_MISMATCH", "Stored diagnostic result does not match deterministic replay", "$.result");
|
|
149
|
-
const reviewBlocked = review.report.checks.some((check) => !run.gate.allowedReviewStatuses.includes(check.status))
|
|
150
|
-
|
|
151
|
-
|
|
118
|
+
const reviewBlocked = review.report.checks.some((check) => !run.gate.allowedReviewStatuses.includes(check.status)) ||
|
|
119
|
+
review.evaluations.some((evaluation) => {
|
|
120
|
+
const status = evaluation.expressionOverflows.length > 0
|
|
121
|
+
? "fail"
|
|
122
|
+
: evaluation.status === "triggered"
|
|
123
|
+
? evaluation.severity
|
|
124
|
+
: evaluation.status;
|
|
125
|
+
return !run.gate.allowedReviewStatuses.includes(status);
|
|
126
|
+
});
|
|
127
|
+
const metricBlocked = rerun.findings.some((finding) => finding.category !== "structural" &&
|
|
128
|
+
!run.gate.allowedMetricFindingSeverities.includes(finding.severity));
|
|
129
|
+
if (reviewBlocked ||
|
|
130
|
+
metricBlocked ||
|
|
131
|
+
run.gate.reviewGate !== "passed" ||
|
|
132
|
+
run.gate.metricGate !== "passed")
|
|
152
133
|
throw new ComplianceError("DIAGNOSTIC_MISMATCH", "Diagnostic execution gates do not recompute as passed", "$.gate");
|
|
153
134
|
return { review, result: getMetricDiagnosticsResultIdentity(run.result) };
|
|
154
135
|
}
|
|
136
|
+
function buildManifest(run, review, inputArtifacts, preparationArtifacts, lineage) {
|
|
137
|
+
const preparation = getPreparedDiagnosticDataIdentity(run.prepared);
|
|
138
|
+
return freeze({
|
|
139
|
+
definitionIntegrity: run.prepared.definition.definitionIntegrity,
|
|
140
|
+
preparationFingerprint: run.prepared.preparationFingerprint,
|
|
141
|
+
runPresetId: run.runPresetId,
|
|
142
|
+
datasetArtifactId: run.datasetArtifactId,
|
|
143
|
+
inputArtifacts: [...inputArtifacts].sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0),
|
|
144
|
+
preparationArtifacts: [...preparationArtifacts].sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0),
|
|
145
|
+
preparationLineage: lineage,
|
|
146
|
+
inputAudit: preparation.inputAudit,
|
|
147
|
+
filter: preparation.filter,
|
|
148
|
+
groupMap: { ...run.groupMap },
|
|
149
|
+
groupDimensions: { ...run.groupDimensions },
|
|
150
|
+
completePeriodCutoffs: preparation.completePeriodCutoffs,
|
|
151
|
+
expectedCellGridFingerprint: preparation.expectedCellsProvided
|
|
152
|
+
? tag("diagnostic-expected-cell-grid", "expectedCells", preparation.expectedCells)
|
|
153
|
+
: null,
|
|
154
|
+
executionPolicy: { review, gate: run.gate },
|
|
155
|
+
engine: {
|
|
156
|
+
packages: {
|
|
157
|
+
core: CORE_PACKAGE_VERSION,
|
|
158
|
+
data: DATA_PACKAGE_VERSION,
|
|
159
|
+
compliance: COMPLIANCE_PACKAGE_VERSION,
|
|
160
|
+
},
|
|
161
|
+
algorithmVersion: "diagnostics-1",
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
}
|
|
155
165
|
export async function createDiagnosticRunIdentity(input) {
|
|
166
|
+
if (!plain(input))
|
|
167
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Diagnostic evidence must be a plain object", "$");
|
|
168
|
+
exactKeys(input, [
|
|
169
|
+
"completedRun",
|
|
170
|
+
"inputArtifacts",
|
|
171
|
+
"preparationArtifacts",
|
|
172
|
+
"preparationLineage",
|
|
173
|
+
], "$");
|
|
156
174
|
const run = input.completedRun;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
175
|
+
let authenticated;
|
|
176
|
+
try {
|
|
177
|
+
authenticated = rerunAndVerify(run);
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (error instanceof ComplianceError)
|
|
181
|
+
throw error;
|
|
182
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "completedRun must be an authentic completed diagnostic run", "$.completedRun");
|
|
183
|
+
}
|
|
184
|
+
const inputSnapshot = snapshotArtifacts(input.inputArtifacts, "input", "$.inputArtifacts");
|
|
185
|
+
const preparationSnapshot = snapshotArtifacts(input.preparationArtifacts, "preparation", "$.preparationArtifacts");
|
|
186
|
+
const lineage = snapshotLineage(input.preparationLineage);
|
|
187
|
+
validateArtifactGraph(run, inputSnapshot, preparationSnapshot, lineage);
|
|
188
|
+
const [inputArtifacts, preparationArtifacts] = await Promise.all([
|
|
189
|
+
digestArtifacts(inputSnapshot, "$.inputArtifacts"),
|
|
190
|
+
digestArtifacts(preparationSnapshot, "$.preparationArtifacts"),
|
|
191
|
+
]);
|
|
192
|
+
const manifest = buildManifest(run, authenticated.review, inputArtifacts, preparationArtifacts, lineage);
|
|
193
|
+
const runFingerprint = tag("diagnostic-run", "manifest", manifestIdentity(manifest));
|
|
166
194
|
const resultFingerprint = tag("diagnostic-result", "result", authenticated.result);
|
|
167
|
-
const runResultFingerprint =
|
|
195
|
+
const runResultFingerprint = bindingTag(runFingerprint, resultFingerprint);
|
|
168
196
|
const definition = run.prepared.definition;
|
|
169
|
-
const provenance = freeze({
|
|
197
|
+
const provenance = freeze({
|
|
198
|
+
definition: {
|
|
199
|
+
definition: definition.definition,
|
|
200
|
+
identities: {
|
|
201
|
+
algorithm: "fnv1a64-jcs-v1",
|
|
202
|
+
formulaById: { ...definition.formulaFingerprints },
|
|
203
|
+
calculationByInstanceId: { ...definition.calculationFingerprints },
|
|
204
|
+
definition: definition.definitionIntegrity,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
manifest,
|
|
208
|
+
review: authenticated.review,
|
|
209
|
+
result: run.result,
|
|
210
|
+
runFingerprint,
|
|
211
|
+
resultFingerprint,
|
|
212
|
+
runResultFingerprint,
|
|
213
|
+
});
|
|
170
214
|
verified.set(provenance, run);
|
|
171
215
|
return provenance;
|
|
172
216
|
}
|
|
173
|
-
export function assertVerifiedDiagnosticRunProvenance(value) {
|
|
174
|
-
|
|
217
|
+
export function assertVerifiedDiagnosticRunProvenance(value) {
|
|
218
|
+
if (value === null || typeof value !== "object" || !verified.has(value))
|
|
219
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Value is not authentic verified diagnostic provenance", "$");
|
|
220
|
+
}
|
|
221
|
+
function readArtifactDigests(value, scope, path) {
|
|
222
|
+
if (!Array.isArray(value))
|
|
223
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact digests must be an array", path);
|
|
224
|
+
return value.map((item, index) => {
|
|
225
|
+
const itemPath = `${path}[${index}]`;
|
|
226
|
+
if (!plain(item))
|
|
227
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Artifact digest must be a plain object", itemPath);
|
|
228
|
+
for (const key of ["id", "algorithm", "value"]) {
|
|
229
|
+
if (typeof item[key] !== "string")
|
|
230
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `${key} must be a token`, `${itemPath}.${key}`);
|
|
231
|
+
token(item[key], `${itemPath}.${key}`);
|
|
232
|
+
}
|
|
233
|
+
if (item.scope !== scope)
|
|
234
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", `Artifact scope must be ${scope}`, `${itemPath}.scope`);
|
|
235
|
+
if (item.assurance === "sdk-computed") {
|
|
236
|
+
exactKeys(item, ["id", "scope", "assurance", "algorithm", "value", "byteLength"], itemPath);
|
|
237
|
+
if (item.algorithm !== "sha256")
|
|
238
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "SDK digests must use sha256", `${itemPath}.algorithm`);
|
|
239
|
+
if (!/^[0-9a-f]{64}$/.test(item.value))
|
|
240
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid SHA-256 digest", `${itemPath}.value`);
|
|
241
|
+
if (!Number.isSafeInteger(item.byteLength) ||
|
|
242
|
+
item.byteLength < 0)
|
|
243
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid byte length", `${itemPath}.byteLength`);
|
|
244
|
+
}
|
|
245
|
+
else if (item.assurance === "caller-declared") {
|
|
246
|
+
exactKeys(item, ["id", "scope", "assurance", "algorithm", "value"], itemPath);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Unknown artifact assurance", `${itemPath}.assurance`);
|
|
250
|
+
}
|
|
251
|
+
return item;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
function readRecordedEngine(value) {
|
|
255
|
+
if (!plain(value))
|
|
256
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid diagnostic engine", "$.manifest.engine");
|
|
257
|
+
exactKeys(value, ["packages", "algorithmVersion"], "$.manifest.engine");
|
|
258
|
+
if (value.algorithmVersion !== "diagnostics-1")
|
|
259
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Unsupported diagnostic algorithm", "$.manifest.engine.algorithmVersion");
|
|
260
|
+
if (!plain(value.packages))
|
|
261
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid diagnostic package versions", "$.manifest.engine.packages");
|
|
262
|
+
exactKeys(value.packages, ["core", "data", "compliance"], "$.manifest.engine.packages");
|
|
263
|
+
for (const name of ["core", "data", "compliance"]) {
|
|
264
|
+
if (typeof value.packages[name] !== "string")
|
|
265
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Package version must be a token", `$.manifest.engine.packages.${name}`);
|
|
266
|
+
token(value.packages[name], `$.manifest.engine.packages.${name}`);
|
|
267
|
+
}
|
|
268
|
+
return value;
|
|
269
|
+
}
|
|
270
|
+
function auditedNumber(value, path) {
|
|
271
|
+
if (!plain(value))
|
|
272
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid audited number", path);
|
|
273
|
+
if (value.status === "observed" &&
|
|
274
|
+
typeof value.value === "number" &&
|
|
275
|
+
Number.isFinite(value.value)) {
|
|
276
|
+
exactKeys(value, ["status", "value"], path);
|
|
277
|
+
return value.value;
|
|
278
|
+
}
|
|
279
|
+
if (value.status === "missing" && value.value === null) {
|
|
280
|
+
exactKeys(value, ["status", "value"], path);
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
if (value.status === "non-finite" && value.value === null) {
|
|
284
|
+
exactKeys(value, ["status", "value", "nonFiniteKind"], path);
|
|
285
|
+
if (value.nonFiniteKind === "nan")
|
|
286
|
+
return NaN;
|
|
287
|
+
if (value.nonFiniteKind === "positive-infinity")
|
|
288
|
+
return Infinity;
|
|
289
|
+
if (value.nonFiniteKind === "negative-infinity")
|
|
290
|
+
return -Infinity;
|
|
291
|
+
}
|
|
292
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid audited number", path);
|
|
293
|
+
}
|
|
294
|
+
/** Normalized optional nulls are omitted only when rebuilding authored input. */
|
|
295
|
+
function omitNulls(value) {
|
|
296
|
+
return plain(value)
|
|
297
|
+
? Object.fromEntries(Object.entries(value).filter(([, item]) => item !== null))
|
|
298
|
+
: value;
|
|
299
|
+
}
|
|
300
|
+
/** Human descriptions/details are regenerated, but are not identity-bearing. */
|
|
301
|
+
function reviewIdentityView(value) {
|
|
302
|
+
if (!plain(value) ||
|
|
303
|
+
!plain(value.report) ||
|
|
304
|
+
!Array.isArray(value.report.checks))
|
|
305
|
+
return value;
|
|
306
|
+
return {
|
|
307
|
+
...value,
|
|
308
|
+
report: {
|
|
309
|
+
...value.report,
|
|
310
|
+
checks: value.report.checks.map((check) => {
|
|
311
|
+
if (!plain(check))
|
|
312
|
+
return check;
|
|
313
|
+
const { description: _description, details: _details, ...identity } = check;
|
|
314
|
+
return identity;
|
|
315
|
+
}),
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function provenanceIdentityView(value) {
|
|
320
|
+
if (!plain(value))
|
|
321
|
+
return value;
|
|
322
|
+
const manifest = value.manifest;
|
|
323
|
+
const executionPolicy = plain(manifest) ? manifest.executionPolicy : null;
|
|
324
|
+
return {
|
|
325
|
+
...value,
|
|
326
|
+
review: reviewIdentityView(value.review),
|
|
327
|
+
...(plain(manifest) && plain(executionPolicy)
|
|
328
|
+
? {
|
|
329
|
+
manifest: {
|
|
330
|
+
...manifest,
|
|
331
|
+
executionPolicy: {
|
|
332
|
+
...executionPolicy,
|
|
333
|
+
review: reviewIdentityView(executionPolicy.review),
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
}
|
|
337
|
+
: {}),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
function replaySerializedRun(compiled, manifest, review) {
|
|
341
|
+
if (!Array.isArray(manifest.inputAudit))
|
|
342
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Input audit must be an array", "$.manifest.inputAudit");
|
|
343
|
+
const losses = [], exposures = [], expectedCells = [];
|
|
344
|
+
for (const [index, item] of manifest.inputAudit.entries()) {
|
|
345
|
+
const path = `$.manifest.inputAudit[${index}]`;
|
|
346
|
+
if (!plain(item) || !plain(item.record))
|
|
347
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid input audit entry", path);
|
|
348
|
+
exactKeys(item, ["kind", "record", "disposition"], path);
|
|
349
|
+
const record = { ...item.record };
|
|
350
|
+
if (record.source === null)
|
|
351
|
+
delete record.source;
|
|
352
|
+
else if (record.source !== undefined)
|
|
353
|
+
record.source = omitNulls(record.source);
|
|
354
|
+
if (item.kind === "loss") {
|
|
355
|
+
if (!plain(record.measures))
|
|
356
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Invalid audited measures", `${path}.record.measures`);
|
|
357
|
+
record.measures = Object.fromEntries(Object.entries(record.measures).map(([id, value]) => [
|
|
358
|
+
id,
|
|
359
|
+
auditedNumber(value, `${path}.record.measures.${id}`),
|
|
360
|
+
]));
|
|
361
|
+
if (record.claimId === null)
|
|
362
|
+
delete record.claimId;
|
|
363
|
+
losses.push(record);
|
|
364
|
+
}
|
|
365
|
+
else if (item.kind === "exposure") {
|
|
366
|
+
record.value = auditedNumber(record.value, `${path}.record.value`);
|
|
367
|
+
if (record.valuation === null)
|
|
368
|
+
delete record.valuation;
|
|
369
|
+
exposures.push(record);
|
|
370
|
+
}
|
|
371
|
+
else if (item.kind === "expected-cell")
|
|
372
|
+
expectedCells.push(record);
|
|
373
|
+
else
|
|
374
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Unknown input audit kind", `${path}.kind`);
|
|
375
|
+
}
|
|
376
|
+
if (!plain(manifest.executionPolicy) || !plain(manifest.executionPolicy.gate))
|
|
377
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", "Missing execution gate", "$.manifest.executionPolicy.gate");
|
|
378
|
+
const gate = manifest.executionPolicy.gate;
|
|
379
|
+
exactKeys(gate, [
|
|
380
|
+
"allowedReviewStatuses",
|
|
381
|
+
"allowedMetricFindingSeverities",
|
|
382
|
+
"rationaleRef",
|
|
383
|
+
"reviewGate",
|
|
384
|
+
"metricGate",
|
|
385
|
+
], "$.manifest.executionPolicy.gate");
|
|
386
|
+
let outcome;
|
|
387
|
+
try {
|
|
388
|
+
outcome = runValidatedMetricDiagnostics(validateDiagnosticRunInput({
|
|
389
|
+
definition: compiled.definition,
|
|
390
|
+
losses,
|
|
391
|
+
exposures,
|
|
392
|
+
...(manifest.filter === null
|
|
393
|
+
? {}
|
|
394
|
+
: { filter: omitNulls(manifest.filter) }),
|
|
395
|
+
completePeriodCutoffs: manifest.completePeriodCutoffs,
|
|
396
|
+
...(manifest.expectedCellGridFingerprint === null
|
|
397
|
+
? {}
|
|
398
|
+
: { expectedCells }),
|
|
399
|
+
reviewEvidence: review.evidence,
|
|
400
|
+
...(manifest.runPresetId === null
|
|
401
|
+
? {}
|
|
402
|
+
: { runPresetId: manifest.runPresetId }),
|
|
403
|
+
...(manifest.datasetArtifactId === null
|
|
404
|
+
? {}
|
|
405
|
+
: { datasetArtifactId: manifest.datasetArtifactId }),
|
|
406
|
+
groupMap: manifest.groupMap,
|
|
407
|
+
groupDimensions: manifest.groupDimensions,
|
|
408
|
+
policy: {
|
|
409
|
+
allowedReviewStatuses: gate.allowedReviewStatuses,
|
|
410
|
+
allowedMetricFindingSeverities: gate.allowedMetricFindingSeverities,
|
|
411
|
+
...(gate.rationaleRef === null
|
|
412
|
+
? {}
|
|
413
|
+
: { rationaleRef: gate.rationaleRef }),
|
|
414
|
+
},
|
|
415
|
+
}));
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
if (!(error instanceof DiagnosticValidationError))
|
|
419
|
+
throw error;
|
|
420
|
+
const issue = error.issues[0];
|
|
421
|
+
let issuePath = issue?.path ?? "$";
|
|
422
|
+
const recordPath = /^\$\.(losses|exposures|expectedCells)\[(\d+)\](.*)$/.exec(issuePath);
|
|
423
|
+
if (recordPath) {
|
|
424
|
+
const kind = recordPath[1] === "losses"
|
|
425
|
+
? "loss"
|
|
426
|
+
: recordPath[1] === "exposures"
|
|
427
|
+
? "exposure"
|
|
428
|
+
: "expected-cell";
|
|
429
|
+
const auditIndices = manifest.inputAudit.flatMap((item, index) => plain(item) && item.kind === kind ? [index] : []);
|
|
430
|
+
issuePath = `$.manifest.inputAudit[${auditIndices[Number(recordPath[2])]}].record${recordPath[3]}`;
|
|
431
|
+
}
|
|
432
|
+
else if (issuePath.startsWith("$.reviewEvidence"))
|
|
433
|
+
issuePath = issuePath.replace("$.reviewEvidence", "$.review.evidence");
|
|
434
|
+
else if (issuePath.startsWith("$.policy"))
|
|
435
|
+
issuePath = issuePath.replace("$.policy", "$.manifest.executionPolicy.gate");
|
|
436
|
+
else if (issuePath.startsWith("$.definition"))
|
|
437
|
+
issuePath = issuePath.replace("$.definition", "$.definition.definition");
|
|
438
|
+
else
|
|
439
|
+
issuePath = issuePath.replace(/^\$\./, "$.manifest.");
|
|
440
|
+
throw new ComplianceError("BAD_DIAGNOSTIC_RUN", issue?.message ?? "Invalid diagnostic replay input", issuePath);
|
|
441
|
+
}
|
|
442
|
+
if (outcome.status !== "completed")
|
|
443
|
+
throw new ComplianceError("DIAGNOSTIC_MISMATCH", "Stored execution does not pass its declared gates", "$.manifest.executionPolicy.gate");
|
|
444
|
+
return outcome;
|
|
445
|
+
}
|
|
446
|
+
/** Verify serialized evidence by replaying the owning core/data public workflows. */
|
|
447
|
+
export function serializedDiagnosticRunMismatch(value, path = "$.diagnosticRuns[0]") {
|
|
448
|
+
try {
|
|
449
|
+
if (!plain(value))
|
|
450
|
+
return path;
|
|
451
|
+
const fields = [
|
|
452
|
+
"definition",
|
|
453
|
+
"manifest",
|
|
454
|
+
"review",
|
|
455
|
+
"result",
|
|
456
|
+
"runFingerprint",
|
|
457
|
+
"resultFingerprint",
|
|
458
|
+
"runResultFingerprint",
|
|
459
|
+
];
|
|
460
|
+
exactKeys(value, fields, "$");
|
|
461
|
+
for (const key of fields)
|
|
462
|
+
if (!Object.hasOwn(value, key))
|
|
463
|
+
return `${path}.${key}`;
|
|
464
|
+
if (!plain(value.definition))
|
|
465
|
+
return `${path}.definition`;
|
|
466
|
+
exactKeys(value.definition, ["definition", "identities"], "$.definition");
|
|
467
|
+
let compiled;
|
|
468
|
+
try {
|
|
469
|
+
compiled = compileDiagnosticDefinition(value.definition.definition);
|
|
470
|
+
}
|
|
471
|
+
catch (error) {
|
|
472
|
+
const issuePath = error instanceof DiagnosticValidationError
|
|
473
|
+
? error.issues[0]?.path
|
|
474
|
+
: undefined;
|
|
475
|
+
return `${path}.definition.definition${issuePath?.startsWith("$") ? issuePath.slice(1) : ""}`;
|
|
476
|
+
}
|
|
477
|
+
const identities = {
|
|
478
|
+
algorithm: "fnv1a64-jcs-v1",
|
|
479
|
+
formulaById: compiled.formulaFingerprints,
|
|
480
|
+
calculationByInstanceId: compiled.calculationFingerprints,
|
|
481
|
+
definition: compiled.definitionIntegrity,
|
|
482
|
+
};
|
|
483
|
+
let mismatch = firstDifference(value.definition.identities, identities, `${path}.definition.identities`);
|
|
484
|
+
if (mismatch !== null)
|
|
485
|
+
return mismatch;
|
|
486
|
+
if (!plain(value.manifest))
|
|
487
|
+
return `${path}.manifest`;
|
|
488
|
+
if (!plain(value.review))
|
|
489
|
+
return `${path}.review`;
|
|
490
|
+
const manifest = value.manifest;
|
|
491
|
+
const engine = readRecordedEngine(manifest.engine);
|
|
492
|
+
const inputArtifacts = readArtifactDigests(manifest.inputArtifacts, "input", "$.manifest.inputArtifacts");
|
|
493
|
+
const preparationArtifacts = readArtifactDigests(manifest.preparationArtifacts, "preparation", "$.manifest.preparationArtifacts");
|
|
494
|
+
const lineage = snapshotLineage(manifest.preparationLineage);
|
|
495
|
+
const run = replaySerializedRun(compiled, manifest, value.review);
|
|
496
|
+
if (manifest.preparationFingerprint !== run.prepared.preparationFingerprint)
|
|
497
|
+
return `${path}.manifest.preparationFingerprint`;
|
|
498
|
+
validateArtifactGraph(run, inputArtifacts, preparationArtifacts, lineage);
|
|
499
|
+
mismatch = firstDifference(reviewIdentityView(value.review), reviewIdentityView(run.review), `${path}.review`);
|
|
500
|
+
if (mismatch !== null)
|
|
501
|
+
return mismatch;
|
|
502
|
+
const expectedManifest = {
|
|
503
|
+
...buildManifest(run, run.review, inputArtifacts, preparationArtifacts, lineage),
|
|
504
|
+
// Historical package versions describe the original run; reproduction
|
|
505
|
+
// checks the supported algorithm and bundle-wide version agreement.
|
|
506
|
+
engine,
|
|
507
|
+
};
|
|
508
|
+
mismatch = firstDifference(provenanceIdentityView({ manifest })
|
|
509
|
+
.manifest, provenanceIdentityView({ manifest: expectedManifest }).manifest, `${path}.manifest`);
|
|
510
|
+
if (mismatch !== null)
|
|
511
|
+
return mismatch;
|
|
512
|
+
const result = getMetricDiagnosticsResultIdentity(run.result);
|
|
513
|
+
let candidateResult;
|
|
514
|
+
try {
|
|
515
|
+
candidateResult = getMetricDiagnosticsResultIdentity(value.result);
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
const issuePath = error instanceof DiagnosticValidationError
|
|
519
|
+
? error.issues[0]?.path
|
|
520
|
+
: undefined;
|
|
521
|
+
return `${path}.result${issuePath?.startsWith("$") ? issuePath.slice(1) : ""}`;
|
|
522
|
+
}
|
|
523
|
+
mismatch = firstDifference(candidateResult, result, `${path}.result`);
|
|
524
|
+
if (mismatch !== null)
|
|
525
|
+
return mismatch;
|
|
526
|
+
const runFingerprint = tag("diagnostic-run", "manifest", manifestIdentity(expectedManifest));
|
|
527
|
+
const resultFingerprint = tag("diagnostic-result", "result", result);
|
|
528
|
+
if (value.runFingerprint !== runFingerprint)
|
|
529
|
+
return `${path}.runFingerprint`;
|
|
530
|
+
if (value.resultFingerprint !== resultFingerprint)
|
|
531
|
+
return `${path}.resultFingerprint`;
|
|
532
|
+
if (value.runResultFingerprint !==
|
|
533
|
+
bindingTag(runFingerprint, resultFingerprint))
|
|
534
|
+
return `${path}.runResultFingerprint`;
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
catch (error) {
|
|
538
|
+
if (error instanceof ComplianceError && error.path) {
|
|
539
|
+
const relative = error.path
|
|
540
|
+
.replace(/^\$\.completedRun\.prepared/, "$.manifest")
|
|
541
|
+
.replace(/^\$\.completedRun\.review/, "$.review")
|
|
542
|
+
.replace(/^\$\.completedRun\.gate/, "$.manifest.executionPolicy.gate")
|
|
543
|
+
.replace(/^\$\.completedRun\./, "$.manifest.")
|
|
544
|
+
.replace(/^\$\.(inputArtifacts|preparationArtifacts|preparationLineage)/, "$.manifest.$1");
|
|
545
|
+
return `${path}${relative.slice(1)}`;
|
|
546
|
+
}
|
|
547
|
+
return path;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
175
550
|
/** @internal Bundle authoring uses owner state rather than trusting the public snapshot. */
|
|
176
|
-
export function verifiedDefinitionForBundle(value) {
|
|
551
|
+
export function verifiedDefinitionForBundle(value) {
|
|
552
|
+
assertVerifiedDiagnosticRunProvenance(value);
|
|
553
|
+
return verified.get(value).prepared.definition;
|
|
554
|
+
}
|
|
177
555
|
export async function verifyDiagnosticRunIdentity(candidate, input) {
|
|
178
|
-
const regenerated = await createDiagnosticRunIdentity(input);
|
|
179
556
|
let left;
|
|
557
|
+
let snapshot;
|
|
180
558
|
try {
|
|
181
559
|
left = canonicalJson(candidate);
|
|
560
|
+
snapshot = JSON.parse(left);
|
|
182
561
|
}
|
|
183
562
|
catch {
|
|
184
563
|
throw new ComplianceError("DIAGNOSTIC_MISMATCH", "Stored diagnostic provenance is not canonical JSON", "$");
|
|
185
564
|
}
|
|
186
|
-
|
|
187
|
-
|
|
565
|
+
const regenerated = await createDiagnosticRunIdentity(input);
|
|
566
|
+
const comparedSnapshot = provenanceIdentityView(snapshot);
|
|
567
|
+
const comparedRegenerated = provenanceIdentityView(regenerated);
|
|
568
|
+
if (canonicalJson(comparedSnapshot) !== canonicalJson(comparedRegenerated))
|
|
569
|
+
throw new ComplianceError("DIAGNOSTIC_MISMATCH", "Stored diagnostic provenance differs from regenerated provenance", firstDifference(comparedSnapshot, comparedRegenerated, "$") ?? "$");
|
|
188
570
|
return regenerated;
|
|
189
571
|
}
|
|
190
|
-
function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
572
|
+
function firstDifference(left, right, path) {
|
|
573
|
+
if (plain(left) && plain(right)) {
|
|
574
|
+
const keys = [
|
|
575
|
+
...new Set([...Object.keys(left), ...Object.keys(right)]),
|
|
576
|
+
].sort();
|
|
577
|
+
for (const key of keys) {
|
|
578
|
+
const childPath = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)
|
|
579
|
+
? `${path}.${key}`
|
|
580
|
+
: `${path}[${JSON.stringify(key)}]`;
|
|
581
|
+
if (!Object.prototype.hasOwnProperty.call(left, key) ||
|
|
582
|
+
!Object.prototype.hasOwnProperty.call(right, key))
|
|
583
|
+
return childPath;
|
|
584
|
+
const found = firstDifference(left[key], right[key], childPath);
|
|
585
|
+
if (found)
|
|
586
|
+
return found;
|
|
587
|
+
}
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
591
|
+
const shared = Math.min(left.length, right.length);
|
|
592
|
+
for (let index = 0; index < shared; index++) {
|
|
593
|
+
const found = firstDifference(left[index], right[index], `${path}[${index}]`);
|
|
594
|
+
if (found)
|
|
595
|
+
return found;
|
|
596
|
+
}
|
|
597
|
+
return left.length === right.length ? null : `${path}[${shared}]`;
|
|
598
|
+
}
|
|
599
|
+
try {
|
|
600
|
+
return canonicalJson(left) === canonicalJson(right) ? null : path;
|
|
601
|
+
}
|
|
602
|
+
catch {
|
|
603
|
+
return path;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
216
606
|
//# sourceMappingURL=diagnosticRun.js.map
|