@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/src/diagnosticRun.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CORE_PACKAGE_VERSION,
|
|
3
|
+
DiagnosticValidationError,
|
|
3
4
|
canonicalJson,
|
|
5
|
+
compileDiagnosticDefinition,
|
|
4
6
|
fnv1a64,
|
|
5
7
|
getMetricDiagnosticsResultIdentity,
|
|
6
8
|
getPreparedDiagnosticDataIdentity,
|
|
@@ -8,44 +10,111 @@ import {
|
|
|
8
10
|
verifyPreparedDiagnosticDataIntegrity,
|
|
9
11
|
type DiagnosticDeepReadonly,
|
|
10
12
|
type CompiledDiagnosticDefinition,
|
|
13
|
+
type DiagnosticDefinition,
|
|
11
14
|
type JsonValue as CoreDiagnosticJsonValue,
|
|
12
15
|
type NormalizedDiagnosticDefinitionIdentity,
|
|
13
16
|
type NormalizedDiagnosticPreparationIdentity,
|
|
14
17
|
type NormalizedDiagnosticResultIdentity,
|
|
18
|
+
type MetricDiagnosticsResult,
|
|
15
19
|
} from "@actuarial-ts/core";
|
|
16
20
|
import {
|
|
17
21
|
DATA_PACKAGE_VERSION,
|
|
18
22
|
assertCompletedValidatedMetricDiagnosticsRun,
|
|
19
23
|
reviewPreparedDiagnosticData,
|
|
24
|
+
runValidatedMetricDiagnostics,
|
|
25
|
+
validateDiagnosticRunInput,
|
|
20
26
|
type CompletedValidatedMetricDiagnosticsRun,
|
|
21
27
|
type DiagnosticReviewIdentityBody,
|
|
22
28
|
type DiagnosticReviewReceipt,
|
|
23
29
|
} from "@actuarial-ts/data";
|
|
24
30
|
import { ComplianceError } from "./errors.js";
|
|
25
31
|
import { COMPLIANCE_PACKAGE_VERSION } from "./version.js";
|
|
32
|
+
import {
|
|
33
|
+
token,
|
|
34
|
+
plain,
|
|
35
|
+
exactKeys,
|
|
36
|
+
snapshotLineage,
|
|
37
|
+
validateArtifactGraph,
|
|
38
|
+
} from "./diagnosticRunEvidence.js";
|
|
26
39
|
|
|
27
|
-
export interface DiagnosticArtifactDigestBase {
|
|
28
|
-
|
|
40
|
+
export interface DiagnosticArtifactDigestBase {
|
|
41
|
+
readonly id: string;
|
|
42
|
+
readonly value: string;
|
|
43
|
+
readonly scope: "input" | "preparation";
|
|
44
|
+
}
|
|
45
|
+
export type DiagnosticArtifactDigest = DiagnosticArtifactDigestBase &
|
|
46
|
+
(
|
|
47
|
+
| {
|
|
48
|
+
readonly assurance: "sdk-computed";
|
|
49
|
+
readonly algorithm: "sha256";
|
|
50
|
+
readonly byteLength: number;
|
|
51
|
+
}
|
|
52
|
+
| { readonly assurance: "caller-declared"; readonly algorithm: string }
|
|
53
|
+
);
|
|
29
54
|
export type DiagnosticArtifactEvidence =
|
|
30
|
-
| {
|
|
31
|
-
|
|
32
|
-
|
|
55
|
+
| {
|
|
56
|
+
readonly id: string;
|
|
57
|
+
readonly scope: "input" | "preparation";
|
|
58
|
+
readonly assurance: "sdk-computed";
|
|
59
|
+
readonly bytes: Uint8Array;
|
|
60
|
+
}
|
|
61
|
+
| {
|
|
62
|
+
readonly id: string;
|
|
63
|
+
readonly scope: "input" | "preparation";
|
|
64
|
+
readonly assurance: "caller-declared";
|
|
65
|
+
readonly algorithm: string;
|
|
66
|
+
readonly value: string;
|
|
67
|
+
};
|
|
68
|
+
export interface DiagnosticPreparationLineage {
|
|
69
|
+
readonly outputArtifactId: string;
|
|
70
|
+
readonly inputArtifactIds: readonly string[];
|
|
71
|
+
readonly transformationArtifactIds: readonly string[];
|
|
72
|
+
}
|
|
33
73
|
|
|
34
74
|
export interface DiagnosticRunManifest {
|
|
35
75
|
readonly definitionIntegrity: string;
|
|
36
76
|
readonly runPresetId: string | null;
|
|
37
77
|
readonly datasetArtifactId: string | null;
|
|
38
|
-
readonly packageVersions: Readonly<Record<string, string>>;
|
|
39
|
-
readonly preparation: DiagnosticDeepReadonly<NormalizedDiagnosticPreparationIdentity>;
|
|
40
78
|
readonly preparationFingerprint: string;
|
|
41
|
-
readonly
|
|
42
|
-
readonly
|
|
79
|
+
readonly inputArtifacts: readonly DiagnosticArtifactDigest[];
|
|
80
|
+
readonly preparationArtifacts: readonly DiagnosticArtifactDigest[];
|
|
81
|
+
readonly preparationLineage: readonly DiagnosticPreparationLineage[];
|
|
82
|
+
readonly inputAudit: DiagnosticDeepReadonly<
|
|
83
|
+
NormalizedDiagnosticPreparationIdentity["inputAudit"]
|
|
84
|
+
>;
|
|
85
|
+
readonly filter: DiagnosticDeepReadonly<
|
|
86
|
+
NormalizedDiagnosticPreparationIdentity["filter"]
|
|
87
|
+
>;
|
|
43
88
|
readonly groupMap: Readonly<Record<string, string>>;
|
|
44
89
|
readonly groupDimensions: Readonly<Record<string, CoreDiagnosticJsonValue>>;
|
|
45
|
-
readonly
|
|
46
|
-
|
|
90
|
+
readonly completePeriodCutoffs: DiagnosticDeepReadonly<
|
|
91
|
+
NormalizedDiagnosticPreparationIdentity["completePeriodCutoffs"]
|
|
92
|
+
>;
|
|
93
|
+
readonly expectedCellGridFingerprint: string | null;
|
|
94
|
+
readonly executionPolicy: {
|
|
95
|
+
readonly review: DiagnosticReviewReceipt;
|
|
96
|
+
readonly gate: CompletedValidatedMetricDiagnosticsRun["gate"];
|
|
97
|
+
};
|
|
98
|
+
readonly engine: {
|
|
99
|
+
readonly packages: {
|
|
100
|
+
readonly core: string;
|
|
101
|
+
readonly data: string;
|
|
102
|
+
readonly compliance: string;
|
|
103
|
+
};
|
|
104
|
+
readonly algorithmVersion: "diagnostics-1";
|
|
105
|
+
};
|
|
47
106
|
}
|
|
48
|
-
export type NormalizedDiagnosticRunManifestIdentity = DiagnosticDeepReadonly<
|
|
107
|
+
export type NormalizedDiagnosticRunManifestIdentity = DiagnosticDeepReadonly<
|
|
108
|
+
Omit<DiagnosticRunManifest, "executionPolicy"> & {
|
|
109
|
+
readonly executionPolicy: {
|
|
110
|
+
readonly review: {
|
|
111
|
+
readonly body: DiagnosticReviewIdentityBody;
|
|
112
|
+
readonly reportFingerprint: string;
|
|
113
|
+
};
|
|
114
|
+
readonly gate: DiagnosticRunManifest["executionPolicy"]["gate"];
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
>;
|
|
49
118
|
|
|
50
119
|
export interface DiagnosticRunIdentity {
|
|
51
120
|
readonly runFingerprint: string;
|
|
@@ -53,96 +122,964 @@ export interface DiagnosticRunIdentity {
|
|
|
53
122
|
readonly runResultFingerprint: string;
|
|
54
123
|
}
|
|
55
124
|
export interface DiagnosticRunProvenance extends DiagnosticRunIdentity {
|
|
56
|
-
readonly definition:
|
|
57
|
-
|
|
58
|
-
|
|
125
|
+
readonly definition: {
|
|
126
|
+
readonly definition: DiagnosticDeepReadonly<NormalizedDiagnosticDefinitionIdentity>;
|
|
127
|
+
readonly identities: {
|
|
128
|
+
readonly algorithm: "fnv1a64-jcs-v1";
|
|
129
|
+
readonly formulaById: Readonly<Record<string, string>>;
|
|
130
|
+
readonly calculationByInstanceId: Readonly<Record<string, string>>;
|
|
131
|
+
readonly definition: string;
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
readonly manifest: DiagnosticDeepReadonly<DiagnosticRunManifest>;
|
|
59
135
|
readonly review: DiagnosticReviewReceipt;
|
|
60
|
-
readonly result: DiagnosticDeepReadonly<
|
|
136
|
+
readonly result: DiagnosticDeepReadonly<MetricDiagnosticsResult>;
|
|
61
137
|
}
|
|
62
138
|
declare const verifiedDiagnosticRunProvenanceBrand: unique symbol;
|
|
63
|
-
export interface VerifiedDiagnosticRunProvenance
|
|
64
|
-
|
|
139
|
+
export interface VerifiedDiagnosticRunProvenance
|
|
140
|
+
extends DiagnosticRunProvenance {
|
|
141
|
+
readonly [verifiedDiagnosticRunProvenanceBrand]: true;
|
|
142
|
+
}
|
|
143
|
+
export interface CreateDiagnosticRunIdentityInput {
|
|
144
|
+
readonly completedRun: CompletedValidatedMetricDiagnosticsRun;
|
|
145
|
+
readonly inputArtifacts: readonly DiagnosticArtifactEvidence[];
|
|
146
|
+
readonly preparationArtifacts: readonly DiagnosticArtifactEvidence[];
|
|
147
|
+
readonly preparationLineage: readonly DiagnosticPreparationLineage[];
|
|
148
|
+
}
|
|
65
149
|
|
|
66
150
|
const verified = new WeakMap<object, CompletedValidatedMetricDiagnosticsRun>();
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
151
|
+
function freeze<T>(
|
|
152
|
+
value: T,
|
|
153
|
+
seen = new WeakSet<object>(),
|
|
154
|
+
): DiagnosticDeepReadonly<T> {
|
|
155
|
+
if (value === null || typeof value !== "object" || seen.has(value))
|
|
156
|
+
return value as DiagnosticDeepReadonly<T>;
|
|
157
|
+
seen.add(value);
|
|
158
|
+
for (const child of Object.values(value as Record<string, unknown>))
|
|
159
|
+
freeze(child, seen);
|
|
160
|
+
return Object.freeze(value) as DiagnosticDeepReadonly<T>;
|
|
161
|
+
}
|
|
162
|
+
function tag(kind: string, key: string, value: unknown): string {
|
|
163
|
+
return `fnv1a64-jcs-v1:${fnv1a64(canonicalJson({ identityVersion: 1, kind, [key]: value }))}`;
|
|
164
|
+
}
|
|
165
|
+
function manifestIdentity(
|
|
166
|
+
manifest: DiagnosticRunManifest,
|
|
167
|
+
): NormalizedDiagnosticRunManifestIdentity {
|
|
168
|
+
return {
|
|
169
|
+
...manifest,
|
|
170
|
+
executionPolicy: {
|
|
171
|
+
gate: manifest.executionPolicy.gate,
|
|
172
|
+
review: {
|
|
173
|
+
body: manifest.executionPolicy.review.identityBody,
|
|
174
|
+
reportFingerprint: manifest.executionPolicy.review.reportFingerprint,
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function bindingTag(runFingerprint: string, resultFingerprint: string): string {
|
|
180
|
+
return `fnv1a64-jcs-v1:${fnv1a64(canonicalJson({ identityVersion: 1, kind: "diagnostic-run-result", runFingerprint, resultFingerprint }))}`;
|
|
181
|
+
}
|
|
182
|
+
type SnapshottedArtifact =
|
|
183
|
+
| DiagnosticArtifactDigest
|
|
184
|
+
| {
|
|
185
|
+
readonly id: string;
|
|
186
|
+
readonly scope: "input" | "preparation";
|
|
187
|
+
readonly assurance: "sdk-computed";
|
|
188
|
+
readonly bytes: Uint8Array;
|
|
189
|
+
};
|
|
190
|
+
function snapshotArtifacts(
|
|
191
|
+
evidence: unknown,
|
|
192
|
+
scope: "input" | "preparation",
|
|
193
|
+
path: string,
|
|
194
|
+
): readonly SnapshottedArtifact[] {
|
|
195
|
+
if (!Array.isArray(evidence))
|
|
196
|
+
throw new ComplianceError(
|
|
197
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
198
|
+
`${path} must be an array`,
|
|
199
|
+
path,
|
|
200
|
+
);
|
|
201
|
+
const seen = new Set<string>();
|
|
202
|
+
const result: SnapshottedArtifact[] = [];
|
|
203
|
+
for (const [index, item] of evidence.entries()) {
|
|
204
|
+
const itemPath = `${path}[${index}]`;
|
|
205
|
+
if (!plain(item))
|
|
206
|
+
throw new ComplianceError(
|
|
207
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
208
|
+
"Artifact evidence must be a plain object",
|
|
209
|
+
itemPath,
|
|
210
|
+
);
|
|
211
|
+
if (typeof item.id !== "string")
|
|
212
|
+
throw new ComplianceError(
|
|
213
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
214
|
+
"Artifact id must be a string",
|
|
215
|
+
`${itemPath}.id`,
|
|
216
|
+
);
|
|
217
|
+
token(item.id, `${itemPath}.id`);
|
|
218
|
+
if (item.scope !== scope)
|
|
219
|
+
throw new ComplianceError(
|
|
220
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
221
|
+
`Artifact scope must be ${scope}`,
|
|
222
|
+
`${itemPath}.scope`,
|
|
223
|
+
);
|
|
224
|
+
if (seen.has(item.id))
|
|
225
|
+
throw new ComplianceError(
|
|
226
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
227
|
+
`Duplicate artifact ID ${item.id}`,
|
|
228
|
+
`${itemPath}.id`,
|
|
229
|
+
);
|
|
230
|
+
seen.add(item.id);
|
|
231
|
+
if (item.assurance === "sdk-computed") {
|
|
232
|
+
exactKeys(item, ["id", "scope", "assurance", "bytes"], itemPath);
|
|
233
|
+
if (!(item.bytes instanceof Uint8Array))
|
|
234
|
+
throw new ComplianceError(
|
|
235
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
236
|
+
"SDK-computed artifact evidence requires actual Uint8Array bytes",
|
|
237
|
+
`${itemPath}.bytes`,
|
|
238
|
+
);
|
|
239
|
+
const bytes = new Uint8Array(item.bytes.byteLength);
|
|
240
|
+
bytes.set(item.bytes);
|
|
241
|
+
result.push({ id: item.id, scope, assurance: item.assurance, bytes });
|
|
242
|
+
} else if (item.assurance === "caller-declared") {
|
|
243
|
+
exactKeys(
|
|
244
|
+
item,
|
|
245
|
+
["id", "scope", "assurance", "algorithm", "value"],
|
|
246
|
+
itemPath,
|
|
247
|
+
);
|
|
248
|
+
if (typeof item.algorithm !== "string")
|
|
249
|
+
throw new ComplianceError(
|
|
250
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
251
|
+
"Artifact algorithm must be a string",
|
|
252
|
+
`${itemPath}.algorithm`,
|
|
253
|
+
);
|
|
254
|
+
if (typeof item.value !== "string")
|
|
255
|
+
throw new ComplianceError(
|
|
256
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
257
|
+
"Artifact value must be a string",
|
|
258
|
+
`${itemPath}.value`,
|
|
259
|
+
);
|
|
260
|
+
token(item.algorithm, `${itemPath}.algorithm`);
|
|
261
|
+
token(item.value, `${itemPath}.value`);
|
|
262
|
+
result.push({
|
|
263
|
+
id: item.id,
|
|
264
|
+
scope,
|
|
265
|
+
assurance: item.assurance,
|
|
266
|
+
algorithm: item.algorithm,
|
|
267
|
+
value: item.value,
|
|
268
|
+
});
|
|
269
|
+
} else
|
|
270
|
+
throw new ComplianceError(
|
|
271
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
272
|
+
"Unknown artifact assurance",
|
|
273
|
+
`${itemPath}.assurance`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
return result.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
277
|
+
}
|
|
278
|
+
async function digestArtifacts(
|
|
279
|
+
snapshot: readonly SnapshottedArtifact[],
|
|
280
|
+
path: string,
|
|
281
|
+
): Promise<readonly DiagnosticArtifactDigest[]> {
|
|
282
|
+
if (
|
|
283
|
+
snapshot.some((item) => "bytes" in item) &&
|
|
284
|
+
globalThis.crypto?.subtle === undefined
|
|
285
|
+
)
|
|
286
|
+
throw new ComplianceError(
|
|
287
|
+
"CRYPTO_UNAVAILABLE",
|
|
288
|
+
"Web Crypto SHA-256 is unavailable",
|
|
289
|
+
path,
|
|
290
|
+
);
|
|
291
|
+
return Promise.all(
|
|
292
|
+
snapshot.map(async (item) => {
|
|
293
|
+
if (!("bytes" in item)) return item;
|
|
294
|
+
const hash = await globalThis.crypto.subtle.digest("SHA-256", item.bytes);
|
|
295
|
+
return {
|
|
296
|
+
id: item.id,
|
|
297
|
+
scope: item.scope,
|
|
298
|
+
assurance: item.assurance,
|
|
299
|
+
algorithm: "sha256" as const,
|
|
300
|
+
value: [...new Uint8Array(hash)]
|
|
301
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
302
|
+
.join(""),
|
|
303
|
+
byteLength: item.bytes.byteLength,
|
|
304
|
+
};
|
|
305
|
+
}),
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function rerunAndVerify(run: CompletedValidatedMetricDiagnosticsRun): {
|
|
310
|
+
review: DiagnosticReviewReceipt;
|
|
311
|
+
result: DiagnosticDeepReadonly<NormalizedDiagnosticResultIdentity>;
|
|
312
|
+
} {
|
|
313
|
+
assertCompletedValidatedMetricDiagnosticsRun(run);
|
|
314
|
+
verifyPreparedDiagnosticDataIntegrity(run.prepared);
|
|
315
|
+
const review = reviewPreparedDiagnosticData({
|
|
316
|
+
prepared: run.prepared,
|
|
317
|
+
evidence: run.review.evidence,
|
|
318
|
+
});
|
|
319
|
+
if (
|
|
320
|
+
review.reportFingerprint !== run.review.reportFingerprint ||
|
|
321
|
+
canonicalJson(review.identityBody) !==
|
|
322
|
+
canonicalJson(run.review.identityBody)
|
|
323
|
+
)
|
|
324
|
+
throw new ComplianceError(
|
|
325
|
+
"DIAGNOSTIC_MISMATCH",
|
|
326
|
+
"Stored diagnostic review does not match a regenerated review",
|
|
327
|
+
"$.review",
|
|
328
|
+
);
|
|
329
|
+
const rerun = runMetricDiagnostics({
|
|
330
|
+
prepared: run.prepared,
|
|
331
|
+
groupMap: run.groupMap,
|
|
332
|
+
groupDimensions: run.groupDimensions,
|
|
333
|
+
});
|
|
334
|
+
if (
|
|
335
|
+
canonicalJson(getMetricDiagnosticsResultIdentity(rerun)) !==
|
|
336
|
+
canonicalJson(getMetricDiagnosticsResultIdentity(run.result))
|
|
337
|
+
)
|
|
338
|
+
throw new ComplianceError(
|
|
339
|
+
"DIAGNOSTIC_MISMATCH",
|
|
340
|
+
"Stored diagnostic result does not match deterministic replay",
|
|
341
|
+
"$.result",
|
|
342
|
+
);
|
|
343
|
+
const reviewBlocked =
|
|
344
|
+
review.report.checks.some(
|
|
345
|
+
(check) => !run.gate.allowedReviewStatuses.includes(check.status),
|
|
346
|
+
) ||
|
|
347
|
+
review.evaluations.some((evaluation) => {
|
|
348
|
+
const status =
|
|
349
|
+
evaluation.expressionOverflows.length > 0
|
|
350
|
+
? "fail"
|
|
351
|
+
: evaluation.status === "triggered"
|
|
352
|
+
? evaluation.severity
|
|
353
|
+
: evaluation.status;
|
|
354
|
+
return !run.gate.allowedReviewStatuses.includes(status);
|
|
355
|
+
});
|
|
356
|
+
const metricBlocked = rerun.findings.some(
|
|
357
|
+
(finding) =>
|
|
358
|
+
finding.category !== "structural" &&
|
|
359
|
+
!run.gate.allowedMetricFindingSeverities.includes(finding.severity),
|
|
360
|
+
);
|
|
361
|
+
if (
|
|
362
|
+
reviewBlocked ||
|
|
363
|
+
metricBlocked ||
|
|
364
|
+
run.gate.reviewGate !== "passed" ||
|
|
365
|
+
run.gate.metricGate !== "passed"
|
|
366
|
+
)
|
|
367
|
+
throw new ComplianceError(
|
|
368
|
+
"DIAGNOSTIC_MISMATCH",
|
|
369
|
+
"Diagnostic execution gates do not recompute as passed",
|
|
370
|
+
"$.gate",
|
|
371
|
+
);
|
|
372
|
+
return { review, result: getMetricDiagnosticsResultIdentity(run.result) };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function buildManifest(
|
|
376
|
+
run: CompletedValidatedMetricDiagnosticsRun,
|
|
377
|
+
review: DiagnosticReviewReceipt,
|
|
378
|
+
inputArtifacts: readonly DiagnosticArtifactDigest[],
|
|
379
|
+
preparationArtifacts: readonly DiagnosticArtifactDigest[],
|
|
380
|
+
lineage: readonly DiagnosticPreparationLineage[],
|
|
381
|
+
): DiagnosticDeepReadonly<DiagnosticRunManifest> {
|
|
382
|
+
const preparation = getPreparedDiagnosticDataIdentity(run.prepared);
|
|
383
|
+
return freeze({
|
|
384
|
+
definitionIntegrity: run.prepared.definition.definitionIntegrity,
|
|
385
|
+
preparationFingerprint: run.prepared.preparationFingerprint,
|
|
386
|
+
runPresetId: run.runPresetId,
|
|
387
|
+
datasetArtifactId: run.datasetArtifactId,
|
|
388
|
+
inputArtifacts: [...inputArtifacts].sort((a, b) =>
|
|
389
|
+
a.id < b.id ? -1 : a.id > b.id ? 1 : 0,
|
|
390
|
+
),
|
|
391
|
+
preparationArtifacts: [...preparationArtifacts].sort((a, b) =>
|
|
392
|
+
a.id < b.id ? -1 : a.id > b.id ? 1 : 0,
|
|
393
|
+
),
|
|
394
|
+
preparationLineage: lineage,
|
|
395
|
+
inputAudit: preparation.inputAudit,
|
|
396
|
+
filter: preparation.filter,
|
|
397
|
+
groupMap: { ...run.groupMap },
|
|
398
|
+
groupDimensions: { ...run.groupDimensions },
|
|
399
|
+
completePeriodCutoffs: preparation.completePeriodCutoffs,
|
|
400
|
+
expectedCellGridFingerprint: preparation.expectedCellsProvided
|
|
401
|
+
? tag(
|
|
402
|
+
"diagnostic-expected-cell-grid",
|
|
403
|
+
"expectedCells",
|
|
404
|
+
preparation.expectedCells,
|
|
405
|
+
)
|
|
406
|
+
: null,
|
|
407
|
+
executionPolicy: { review, gate: run.gate },
|
|
408
|
+
engine: {
|
|
409
|
+
packages: {
|
|
410
|
+
core: CORE_PACKAGE_VERSION,
|
|
411
|
+
data: DATA_PACKAGE_VERSION,
|
|
412
|
+
compliance: COMPLIANCE_PACKAGE_VERSION,
|
|
413
|
+
},
|
|
414
|
+
algorithmVersion: "diagnostics-1" as const,
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
export async function createDiagnosticRunIdentity(
|
|
420
|
+
input: CreateDiagnosticRunIdentityInput,
|
|
421
|
+
): Promise<VerifiedDiagnosticRunProvenance> {
|
|
422
|
+
if (!plain(input))
|
|
423
|
+
throw new ComplianceError(
|
|
424
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
425
|
+
"Diagnostic evidence must be a plain object",
|
|
426
|
+
"$",
|
|
427
|
+
);
|
|
428
|
+
exactKeys(
|
|
429
|
+
input,
|
|
430
|
+
[
|
|
431
|
+
"completedRun",
|
|
432
|
+
"inputArtifacts",
|
|
433
|
+
"preparationArtifacts",
|
|
434
|
+
"preparationLineage",
|
|
435
|
+
],
|
|
436
|
+
"$",
|
|
437
|
+
);
|
|
438
|
+
const run = input.completedRun;
|
|
439
|
+
let authenticated: ReturnType<typeof rerunAndVerify>;
|
|
440
|
+
try {
|
|
441
|
+
authenticated = rerunAndVerify(run);
|
|
442
|
+
} catch (error) {
|
|
443
|
+
if (error instanceof ComplianceError) throw error;
|
|
444
|
+
throw new ComplianceError(
|
|
445
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
446
|
+
"completedRun must be an authentic completed diagnostic run",
|
|
447
|
+
"$.completedRun",
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
const inputSnapshot = snapshotArtifacts(
|
|
451
|
+
(input as unknown as Record<string, unknown>).inputArtifacts,
|
|
452
|
+
"input",
|
|
453
|
+
"$.inputArtifacts",
|
|
454
|
+
);
|
|
455
|
+
const preparationSnapshot = snapshotArtifacts(
|
|
456
|
+
(input as unknown as Record<string, unknown>).preparationArtifacts,
|
|
457
|
+
"preparation",
|
|
458
|
+
"$.preparationArtifacts",
|
|
459
|
+
);
|
|
460
|
+
const lineage = snapshotLineage(
|
|
461
|
+
(input as unknown as Record<string, unknown>).preparationLineage,
|
|
462
|
+
);
|
|
463
|
+
validateArtifactGraph(run, inputSnapshot, preparationSnapshot, lineage);
|
|
464
|
+
const [inputArtifacts, preparationArtifacts] = await Promise.all([
|
|
465
|
+
digestArtifacts(inputSnapshot, "$.inputArtifacts"),
|
|
466
|
+
digestArtifacts(preparationSnapshot, "$.preparationArtifacts"),
|
|
467
|
+
]);
|
|
468
|
+
const manifest = buildManifest(
|
|
469
|
+
run,
|
|
470
|
+
authenticated.review,
|
|
471
|
+
inputArtifacts,
|
|
472
|
+
preparationArtifacts,
|
|
473
|
+
lineage,
|
|
474
|
+
);
|
|
475
|
+
const runFingerprint = tag(
|
|
476
|
+
"diagnostic-run",
|
|
477
|
+
"manifest",
|
|
478
|
+
manifestIdentity(manifest),
|
|
479
|
+
);
|
|
480
|
+
const resultFingerprint = tag(
|
|
481
|
+
"diagnostic-result",
|
|
482
|
+
"result",
|
|
483
|
+
authenticated.result,
|
|
484
|
+
);
|
|
485
|
+
const runResultFingerprint = bindingTag(runFingerprint, resultFingerprint);
|
|
486
|
+
const definition = run.prepared.definition;
|
|
487
|
+
const provenance = freeze({
|
|
488
|
+
definition: {
|
|
489
|
+
definition: definition.definition,
|
|
490
|
+
identities: {
|
|
491
|
+
algorithm: "fnv1a64-jcs-v1" as const,
|
|
492
|
+
formulaById: { ...definition.formulaFingerprints },
|
|
493
|
+
calculationByInstanceId: { ...definition.calculationFingerprints },
|
|
494
|
+
definition: definition.definitionIntegrity,
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
manifest,
|
|
498
|
+
review: authenticated.review,
|
|
499
|
+
result: run.result,
|
|
500
|
+
runFingerprint,
|
|
501
|
+
resultFingerprint,
|
|
502
|
+
runResultFingerprint,
|
|
503
|
+
}) as unknown as VerifiedDiagnosticRunProvenance;
|
|
504
|
+
verified.set(provenance, run);
|
|
505
|
+
return provenance;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
export function assertVerifiedDiagnosticRunProvenance(
|
|
509
|
+
value: unknown,
|
|
510
|
+
): asserts value is VerifiedDiagnosticRunProvenance {
|
|
511
|
+
if (value === null || typeof value !== "object" || !verified.has(value))
|
|
512
|
+
throw new ComplianceError(
|
|
513
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
514
|
+
"Value is not authentic verified diagnostic provenance",
|
|
515
|
+
"$",
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function readArtifactDigests(
|
|
520
|
+
value: unknown,
|
|
521
|
+
scope: "input" | "preparation",
|
|
522
|
+
path: string,
|
|
523
|
+
): DiagnosticArtifactDigest[] {
|
|
524
|
+
if (!Array.isArray(value))
|
|
525
|
+
throw new ComplianceError(
|
|
526
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
527
|
+
"Artifact digests must be an array",
|
|
528
|
+
path,
|
|
529
|
+
);
|
|
530
|
+
return value.map((item, index) => {
|
|
531
|
+
const itemPath = `${path}[${index}]`;
|
|
532
|
+
if (!plain(item))
|
|
533
|
+
throw new ComplianceError(
|
|
534
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
535
|
+
"Artifact digest must be a plain object",
|
|
536
|
+
itemPath,
|
|
537
|
+
);
|
|
538
|
+
for (const key of ["id", "algorithm", "value"] as const) {
|
|
539
|
+
if (typeof item[key] !== "string")
|
|
540
|
+
throw new ComplianceError(
|
|
541
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
542
|
+
`${key} must be a token`,
|
|
543
|
+
`${itemPath}.${key}`,
|
|
544
|
+
);
|
|
545
|
+
token(item[key], `${itemPath}.${key}`);
|
|
546
|
+
}
|
|
547
|
+
if (item.scope !== scope)
|
|
548
|
+
throw new ComplianceError(
|
|
549
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
550
|
+
`Artifact scope must be ${scope}`,
|
|
551
|
+
`${itemPath}.scope`,
|
|
552
|
+
);
|
|
553
|
+
if (item.assurance === "sdk-computed") {
|
|
554
|
+
exactKeys(
|
|
555
|
+
item,
|
|
556
|
+
["id", "scope", "assurance", "algorithm", "value", "byteLength"],
|
|
557
|
+
itemPath,
|
|
558
|
+
);
|
|
559
|
+
if (item.algorithm !== "sha256")
|
|
560
|
+
throw new ComplianceError(
|
|
561
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
562
|
+
"SDK digests must use sha256",
|
|
563
|
+
`${itemPath}.algorithm`,
|
|
564
|
+
);
|
|
565
|
+
if (!/^[0-9a-f]{64}$/.test(item.value as string))
|
|
566
|
+
throw new ComplianceError(
|
|
567
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
568
|
+
"Invalid SHA-256 digest",
|
|
569
|
+
`${itemPath}.value`,
|
|
570
|
+
);
|
|
571
|
+
if (
|
|
572
|
+
!Number.isSafeInteger(item.byteLength) ||
|
|
573
|
+
(item.byteLength as number) < 0
|
|
574
|
+
)
|
|
575
|
+
throw new ComplianceError(
|
|
576
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
577
|
+
"Invalid byte length",
|
|
578
|
+
`${itemPath}.byteLength`,
|
|
579
|
+
);
|
|
580
|
+
} else if (item.assurance === "caller-declared") {
|
|
581
|
+
exactKeys(
|
|
582
|
+
item,
|
|
583
|
+
["id", "scope", "assurance", "algorithm", "value"],
|
|
584
|
+
itemPath,
|
|
585
|
+
);
|
|
586
|
+
} else {
|
|
587
|
+
throw new ComplianceError(
|
|
588
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
589
|
+
"Unknown artifact assurance",
|
|
590
|
+
`${itemPath}.assurance`,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
return item as unknown as DiagnosticArtifactDigest;
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function readRecordedEngine(value: unknown): DiagnosticRunManifest["engine"] {
|
|
598
|
+
if (!plain(value))
|
|
599
|
+
throw new ComplianceError(
|
|
600
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
601
|
+
"Invalid diagnostic engine",
|
|
602
|
+
"$.manifest.engine",
|
|
603
|
+
);
|
|
604
|
+
exactKeys(value, ["packages", "algorithmVersion"], "$.manifest.engine");
|
|
605
|
+
if (value.algorithmVersion !== "diagnostics-1")
|
|
606
|
+
throw new ComplianceError(
|
|
607
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
608
|
+
"Unsupported diagnostic algorithm",
|
|
609
|
+
"$.manifest.engine.algorithmVersion",
|
|
610
|
+
);
|
|
611
|
+
if (!plain(value.packages))
|
|
612
|
+
throw new ComplianceError(
|
|
613
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
614
|
+
"Invalid diagnostic package versions",
|
|
615
|
+
"$.manifest.engine.packages",
|
|
616
|
+
);
|
|
617
|
+
exactKeys(
|
|
618
|
+
value.packages,
|
|
619
|
+
["core", "data", "compliance"],
|
|
620
|
+
"$.manifest.engine.packages",
|
|
621
|
+
);
|
|
622
|
+
for (const name of ["core", "data", "compliance"] as const) {
|
|
623
|
+
if (typeof value.packages[name] !== "string")
|
|
624
|
+
throw new ComplianceError(
|
|
625
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
626
|
+
"Package version must be a token",
|
|
627
|
+
`$.manifest.engine.packages.${name}`,
|
|
628
|
+
);
|
|
629
|
+
token(value.packages[name], `$.manifest.engine.packages.${name}`);
|
|
82
630
|
}
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
631
|
+
return value as unknown as DiagnosticRunManifest["engine"];
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function auditedNumber(value: unknown, path: string): number | null {
|
|
635
|
+
if (!plain(value))
|
|
636
|
+
throw new ComplianceError(
|
|
637
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
638
|
+
"Invalid audited number",
|
|
639
|
+
path,
|
|
640
|
+
);
|
|
641
|
+
if (
|
|
642
|
+
value.status === "observed" &&
|
|
643
|
+
typeof value.value === "number" &&
|
|
644
|
+
Number.isFinite(value.value)
|
|
645
|
+
) {
|
|
646
|
+
exactKeys(value, ["status", "value"], path);
|
|
647
|
+
return value.value;
|
|
648
|
+
}
|
|
649
|
+
if (value.status === "missing" && value.value === null) {
|
|
650
|
+
exactKeys(value, ["status", "value"], path);
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
if (value.status === "non-finite" && value.value === null) {
|
|
654
|
+
exactKeys(value, ["status", "value", "nonFiniteKind"], path);
|
|
655
|
+
if (value.nonFiniteKind === "nan") return NaN;
|
|
656
|
+
if (value.nonFiniteKind === "positive-infinity") return Infinity;
|
|
657
|
+
if (value.nonFiniteKind === "negative-infinity") return -Infinity;
|
|
658
|
+
}
|
|
659
|
+
throw new ComplianceError(
|
|
660
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
661
|
+
"Invalid audited number",
|
|
662
|
+
path,
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** Normalized optional nulls are omitted only when rebuilding authored input. */
|
|
667
|
+
function omitNulls(value: unknown): unknown {
|
|
668
|
+
return plain(value)
|
|
669
|
+
? Object.fromEntries(
|
|
670
|
+
Object.entries(value).filter(([, item]) => item !== null),
|
|
671
|
+
)
|
|
672
|
+
: value;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** Human descriptions/details are regenerated, but are not identity-bearing. */
|
|
676
|
+
function reviewIdentityView(value: unknown): unknown {
|
|
677
|
+
if (
|
|
678
|
+
!plain(value) ||
|
|
679
|
+
!plain(value.report) ||
|
|
680
|
+
!Array.isArray(value.report.checks)
|
|
681
|
+
)
|
|
682
|
+
return value;
|
|
683
|
+
return {
|
|
684
|
+
...value,
|
|
685
|
+
report: {
|
|
686
|
+
...value.report,
|
|
687
|
+
checks: value.report.checks.map((check) => {
|
|
688
|
+
if (!plain(check)) return check;
|
|
689
|
+
const {
|
|
690
|
+
description: _description,
|
|
691
|
+
details: _details,
|
|
692
|
+
...identity
|
|
693
|
+
} = check;
|
|
694
|
+
return identity;
|
|
695
|
+
}),
|
|
696
|
+
},
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function provenanceIdentityView(value: unknown): unknown {
|
|
701
|
+
if (!plain(value)) return value;
|
|
702
|
+
const manifest = value.manifest;
|
|
703
|
+
const executionPolicy = plain(manifest) ? manifest.executionPolicy : null;
|
|
704
|
+
return {
|
|
705
|
+
...value,
|
|
706
|
+
review: reviewIdentityView(value.review),
|
|
707
|
+
...(plain(manifest) && plain(executionPolicy)
|
|
708
|
+
? {
|
|
709
|
+
manifest: {
|
|
710
|
+
...manifest,
|
|
711
|
+
executionPolicy: {
|
|
712
|
+
...executionPolicy,
|
|
713
|
+
review: reviewIdentityView(executionPolicy.review),
|
|
714
|
+
},
|
|
715
|
+
},
|
|
716
|
+
}
|
|
717
|
+
: {}),
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function replaySerializedRun(
|
|
722
|
+
compiled: CompiledDiagnosticDefinition,
|
|
723
|
+
manifest: Record<string, unknown>,
|
|
724
|
+
review: Record<string, unknown>,
|
|
725
|
+
): CompletedValidatedMetricDiagnosticsRun {
|
|
726
|
+
if (!Array.isArray(manifest.inputAudit))
|
|
727
|
+
throw new ComplianceError(
|
|
728
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
729
|
+
"Input audit must be an array",
|
|
730
|
+
"$.manifest.inputAudit",
|
|
731
|
+
);
|
|
732
|
+
const losses: unknown[] = [],
|
|
733
|
+
exposures: unknown[] = [],
|
|
734
|
+
expectedCells: unknown[] = [];
|
|
735
|
+
for (const [index, item] of manifest.inputAudit.entries()) {
|
|
736
|
+
const path = `$.manifest.inputAudit[${index}]`;
|
|
737
|
+
if (!plain(item) || !plain(item.record))
|
|
738
|
+
throw new ComplianceError(
|
|
739
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
740
|
+
"Invalid input audit entry",
|
|
741
|
+
path,
|
|
742
|
+
);
|
|
743
|
+
exactKeys(item, ["kind", "record", "disposition"], path);
|
|
744
|
+
const record = { ...item.record };
|
|
745
|
+
if (record.source === null) delete record.source;
|
|
746
|
+
else if (record.source !== undefined)
|
|
747
|
+
record.source = omitNulls(record.source);
|
|
748
|
+
if (item.kind === "loss") {
|
|
749
|
+
if (!plain(record.measures))
|
|
750
|
+
throw new ComplianceError(
|
|
751
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
752
|
+
"Invalid audited measures",
|
|
753
|
+
`${path}.record.measures`,
|
|
754
|
+
);
|
|
755
|
+
record.measures = Object.fromEntries(
|
|
756
|
+
Object.entries(record.measures).map(([id, value]) => [
|
|
757
|
+
id,
|
|
758
|
+
auditedNumber(value, `${path}.record.measures.${id}`),
|
|
759
|
+
]),
|
|
760
|
+
);
|
|
761
|
+
if (record.claimId === null) delete record.claimId;
|
|
762
|
+
losses.push(record);
|
|
763
|
+
} else if (item.kind === "exposure") {
|
|
764
|
+
record.value = auditedNumber(record.value, `${path}.record.value`);
|
|
765
|
+
if (record.valuation === null) delete record.valuation;
|
|
766
|
+
exposures.push(record);
|
|
767
|
+
} else if (item.kind === "expected-cell") expectedCells.push(record);
|
|
768
|
+
else
|
|
769
|
+
throw new ComplianceError(
|
|
770
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
771
|
+
"Unknown input audit kind",
|
|
772
|
+
`${path}.kind`,
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
if (!plain(manifest.executionPolicy) || !plain(manifest.executionPolicy.gate))
|
|
776
|
+
throw new ComplianceError(
|
|
777
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
778
|
+
"Missing execution gate",
|
|
779
|
+
"$.manifest.executionPolicy.gate",
|
|
780
|
+
);
|
|
781
|
+
const gate = manifest.executionPolicy.gate;
|
|
782
|
+
exactKeys(
|
|
783
|
+
gate,
|
|
784
|
+
[
|
|
785
|
+
"allowedReviewStatuses",
|
|
786
|
+
"allowedMetricFindingSeverities",
|
|
787
|
+
"rationaleRef",
|
|
788
|
+
"reviewGate",
|
|
789
|
+
"metricGate",
|
|
790
|
+
],
|
|
791
|
+
"$.manifest.executionPolicy.gate",
|
|
792
|
+
);
|
|
793
|
+
let outcome: ReturnType<typeof runValidatedMetricDiagnostics>;
|
|
794
|
+
try {
|
|
795
|
+
outcome = runValidatedMetricDiagnostics(
|
|
796
|
+
validateDiagnosticRunInput({
|
|
797
|
+
definition: compiled.definition,
|
|
798
|
+
losses,
|
|
799
|
+
exposures,
|
|
800
|
+
...(manifest.filter === null
|
|
801
|
+
? {}
|
|
802
|
+
: { filter: omitNulls(manifest.filter) }),
|
|
803
|
+
completePeriodCutoffs: manifest.completePeriodCutoffs,
|
|
804
|
+
...(manifest.expectedCellGridFingerprint === null
|
|
805
|
+
? {}
|
|
806
|
+
: { expectedCells }),
|
|
807
|
+
reviewEvidence: review.evidence,
|
|
808
|
+
...(manifest.runPresetId === null
|
|
809
|
+
? {}
|
|
810
|
+
: { runPresetId: manifest.runPresetId }),
|
|
811
|
+
...(manifest.datasetArtifactId === null
|
|
812
|
+
? {}
|
|
813
|
+
: { datasetArtifactId: manifest.datasetArtifactId }),
|
|
814
|
+
groupMap: manifest.groupMap,
|
|
815
|
+
groupDimensions: manifest.groupDimensions,
|
|
816
|
+
policy: {
|
|
817
|
+
allowedReviewStatuses: gate.allowedReviewStatuses,
|
|
818
|
+
allowedMetricFindingSeverities: gate.allowedMetricFindingSeverities,
|
|
819
|
+
...(gate.rationaleRef === null
|
|
820
|
+
? {}
|
|
821
|
+
: { rationaleRef: gate.rationaleRef }),
|
|
822
|
+
},
|
|
823
|
+
}),
|
|
824
|
+
);
|
|
825
|
+
} catch (error) {
|
|
826
|
+
if (!(error instanceof DiagnosticValidationError)) throw error;
|
|
827
|
+
const issue = error.issues[0];
|
|
828
|
+
let issuePath = issue?.path ?? "$";
|
|
829
|
+
const recordPath =
|
|
830
|
+
/^\$\.(losses|exposures|expectedCells)\[(\d+)\](.*)$/.exec(issuePath);
|
|
831
|
+
if (recordPath) {
|
|
832
|
+
const kind =
|
|
833
|
+
recordPath[1] === "losses"
|
|
834
|
+
? "loss"
|
|
835
|
+
: recordPath[1] === "exposures"
|
|
836
|
+
? "exposure"
|
|
837
|
+
: "expected-cell";
|
|
838
|
+
const auditIndices = manifest.inputAudit.flatMap((item, index) =>
|
|
839
|
+
plain(item) && item.kind === kind ? [index] : [],
|
|
840
|
+
);
|
|
841
|
+
issuePath = `$.manifest.inputAudit[${auditIndices[Number(recordPath[2])]}].record${recordPath[3]}`;
|
|
842
|
+
} else if (issuePath.startsWith("$.reviewEvidence"))
|
|
843
|
+
issuePath = issuePath.replace("$.reviewEvidence", "$.review.evidence");
|
|
844
|
+
else if (issuePath.startsWith("$.policy"))
|
|
845
|
+
issuePath = issuePath.replace(
|
|
846
|
+
"$.policy",
|
|
847
|
+
"$.manifest.executionPolicy.gate",
|
|
848
|
+
);
|
|
849
|
+
else if (issuePath.startsWith("$.definition"))
|
|
850
|
+
issuePath = issuePath.replace("$.definition", "$.definition.definition");
|
|
851
|
+
else issuePath = issuePath.replace(/^\$\./, "$.manifest.");
|
|
852
|
+
throw new ComplianceError(
|
|
853
|
+
"BAD_DIAGNOSTIC_RUN",
|
|
854
|
+
issue?.message ?? "Invalid diagnostic replay input",
|
|
855
|
+
issuePath,
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
if (outcome.status !== "completed")
|
|
859
|
+
throw new ComplianceError(
|
|
860
|
+
"DIAGNOSTIC_MISMATCH",
|
|
861
|
+
"Stored execution does not pass its declared gates",
|
|
862
|
+
"$.manifest.executionPolicy.gate",
|
|
863
|
+
);
|
|
864
|
+
return outcome;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/** Verify serialized evidence by replaying the owning core/data public workflows. */
|
|
868
|
+
export function serializedDiagnosticRunMismatch(
|
|
869
|
+
value: unknown,
|
|
870
|
+
path = "$.diagnosticRuns[0]",
|
|
871
|
+
): string | null {
|
|
872
|
+
try {
|
|
873
|
+
if (!plain(value)) return path;
|
|
874
|
+
const fields = [
|
|
875
|
+
"definition",
|
|
876
|
+
"manifest",
|
|
877
|
+
"review",
|
|
878
|
+
"result",
|
|
879
|
+
"runFingerprint",
|
|
880
|
+
"resultFingerprint",
|
|
881
|
+
"runResultFingerprint",
|
|
882
|
+
];
|
|
883
|
+
exactKeys(value, fields, "$");
|
|
884
|
+
for (const key of fields)
|
|
885
|
+
if (!Object.hasOwn(value, key)) return `${path}.${key}`;
|
|
886
|
+
if (!plain(value.definition)) return `${path}.definition`;
|
|
887
|
+
exactKeys(value.definition, ["definition", "identities"], "$.definition");
|
|
888
|
+
let compiled: CompiledDiagnosticDefinition;
|
|
889
|
+
try {
|
|
890
|
+
compiled = compileDiagnosticDefinition(
|
|
891
|
+
value.definition.definition as DiagnosticDefinition,
|
|
892
|
+
);
|
|
893
|
+
} catch (error) {
|
|
894
|
+
const issuePath =
|
|
895
|
+
error instanceof DiagnosticValidationError
|
|
896
|
+
? error.issues[0]?.path
|
|
897
|
+
: undefined;
|
|
898
|
+
return `${path}.definition.definition${issuePath?.startsWith("$") ? issuePath.slice(1) : ""}`;
|
|
899
|
+
}
|
|
900
|
+
const identities = {
|
|
901
|
+
algorithm: "fnv1a64-jcs-v1",
|
|
902
|
+
formulaById: compiled.formulaFingerprints,
|
|
903
|
+
calculationByInstanceId: compiled.calculationFingerprints,
|
|
904
|
+
definition: compiled.definitionIntegrity,
|
|
905
|
+
};
|
|
906
|
+
let mismatch = firstDifference(
|
|
907
|
+
value.definition.identities,
|
|
908
|
+
identities,
|
|
909
|
+
`${path}.definition.identities`,
|
|
910
|
+
);
|
|
911
|
+
if (mismatch !== null) return mismatch;
|
|
912
|
+
if (!plain(value.manifest)) return `${path}.manifest`;
|
|
913
|
+
if (!plain(value.review)) return `${path}.review`;
|
|
914
|
+
const manifest = value.manifest;
|
|
915
|
+
const engine = readRecordedEngine(manifest.engine);
|
|
916
|
+
const inputArtifacts = readArtifactDigests(
|
|
917
|
+
manifest.inputArtifacts,
|
|
918
|
+
"input",
|
|
919
|
+
"$.manifest.inputArtifacts",
|
|
920
|
+
);
|
|
921
|
+
const preparationArtifacts = readArtifactDigests(
|
|
922
|
+
manifest.preparationArtifacts,
|
|
923
|
+
"preparation",
|
|
924
|
+
"$.manifest.preparationArtifacts",
|
|
925
|
+
);
|
|
926
|
+
const lineage = snapshotLineage(manifest.preparationLineage);
|
|
927
|
+
const run = replaySerializedRun(compiled, manifest, value.review);
|
|
928
|
+
if (manifest.preparationFingerprint !== run.prepared.preparationFingerprint)
|
|
929
|
+
return `${path}.manifest.preparationFingerprint`;
|
|
930
|
+
validateArtifactGraph(run, inputArtifacts, preparationArtifacts, lineage);
|
|
931
|
+
mismatch = firstDifference(
|
|
932
|
+
reviewIdentityView(value.review),
|
|
933
|
+
reviewIdentityView(run.review),
|
|
934
|
+
`${path}.review`,
|
|
935
|
+
);
|
|
936
|
+
if (mismatch !== null) return mismatch;
|
|
937
|
+
const expectedManifest = {
|
|
938
|
+
...buildManifest(
|
|
939
|
+
run,
|
|
940
|
+
run.review,
|
|
941
|
+
inputArtifacts,
|
|
942
|
+
preparationArtifacts,
|
|
943
|
+
lineage,
|
|
944
|
+
),
|
|
945
|
+
// Historical package versions describe the original run; reproduction
|
|
946
|
+
// checks the supported algorithm and bundle-wide version agreement.
|
|
947
|
+
engine,
|
|
948
|
+
};
|
|
949
|
+
mismatch = firstDifference(
|
|
950
|
+
(provenanceIdentityView({ manifest }) as Record<string, unknown>)
|
|
951
|
+
.manifest,
|
|
952
|
+
(
|
|
953
|
+
provenanceIdentityView({ manifest: expectedManifest }) as Record<
|
|
954
|
+
string,
|
|
955
|
+
unknown
|
|
956
|
+
>
|
|
957
|
+
).manifest,
|
|
958
|
+
`${path}.manifest`,
|
|
959
|
+
);
|
|
960
|
+
if (mismatch !== null) return mismatch;
|
|
961
|
+
const result = getMetricDiagnosticsResultIdentity(run.result);
|
|
962
|
+
let candidateResult: DiagnosticDeepReadonly<NormalizedDiagnosticResultIdentity>;
|
|
963
|
+
try {
|
|
964
|
+
candidateResult = getMetricDiagnosticsResultIdentity(
|
|
965
|
+
value.result as MetricDiagnosticsResult,
|
|
966
|
+
);
|
|
967
|
+
} catch (error) {
|
|
968
|
+
const issuePath =
|
|
969
|
+
error instanceof DiagnosticValidationError
|
|
970
|
+
? error.issues[0]?.path
|
|
971
|
+
: undefined;
|
|
972
|
+
return `${path}.result${issuePath?.startsWith("$") ? issuePath.slice(1) : ""}`;
|
|
973
|
+
}
|
|
974
|
+
mismatch = firstDifference(candidateResult, result, `${path}.result`);
|
|
975
|
+
if (mismatch !== null) return mismatch;
|
|
976
|
+
const runFingerprint = tag(
|
|
977
|
+
"diagnostic-run",
|
|
978
|
+
"manifest",
|
|
979
|
+
manifestIdentity(expectedManifest),
|
|
980
|
+
);
|
|
981
|
+
const resultFingerprint = tag("diagnostic-result", "result", result);
|
|
982
|
+
if (value.runFingerprint !== runFingerprint)
|
|
983
|
+
return `${path}.runFingerprint`;
|
|
984
|
+
if (value.resultFingerprint !== resultFingerprint)
|
|
985
|
+
return `${path}.resultFingerprint`;
|
|
986
|
+
if (
|
|
987
|
+
value.runResultFingerprint !==
|
|
988
|
+
bindingTag(runFingerprint, resultFingerprint)
|
|
989
|
+
)
|
|
990
|
+
return `${path}.runResultFingerprint`;
|
|
991
|
+
return null;
|
|
992
|
+
} catch (error) {
|
|
993
|
+
if (error instanceof ComplianceError && error.path) {
|
|
994
|
+
const relative = error.path
|
|
995
|
+
.replace(/^\$\.completedRun\.prepared/, "$.manifest")
|
|
996
|
+
.replace(/^\$\.completedRun\.review/, "$.review")
|
|
997
|
+
.replace(/^\$\.completedRun\.gate/, "$.manifest.executionPolicy.gate")
|
|
998
|
+
.replace(/^\$\.completedRun\./, "$.manifest.")
|
|
999
|
+
.replace(
|
|
1000
|
+
/^\$\.(inputArtifacts|preparationArtifacts|preparationLineage)/,
|
|
1001
|
+
"$.manifest.$1",
|
|
1002
|
+
);
|
|
1003
|
+
return `${path}${relative.slice(1)}`;
|
|
1004
|
+
}
|
|
1005
|
+
return path;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
136
1008
|
|
|
137
1009
|
/** @internal Bundle authoring uses owner state rather than trusting the public snapshot. */
|
|
138
|
-
export function verifiedDefinitionForBundle(
|
|
1010
|
+
export function verifiedDefinitionForBundle(
|
|
1011
|
+
value: VerifiedDiagnosticRunProvenance,
|
|
1012
|
+
): CompiledDiagnosticDefinition {
|
|
1013
|
+
assertVerifiedDiagnosticRunProvenance(value);
|
|
1014
|
+
return verified.get(value)!.prepared.definition;
|
|
1015
|
+
}
|
|
139
1016
|
|
|
140
|
-
export async function verifyDiagnosticRunIdentity(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
1017
|
+
export async function verifyDiagnosticRunIdentity(
|
|
1018
|
+
candidate: unknown,
|
|
1019
|
+
input: CreateDiagnosticRunIdentityInput,
|
|
1020
|
+
): Promise<VerifiedDiagnosticRunProvenance> {
|
|
1021
|
+
let left: string;
|
|
1022
|
+
let snapshot: unknown;
|
|
1023
|
+
try {
|
|
1024
|
+
left = canonicalJson(candidate);
|
|
1025
|
+
snapshot = JSON.parse(left);
|
|
1026
|
+
} catch {
|
|
1027
|
+
throw new ComplianceError(
|
|
1028
|
+
"DIAGNOSTIC_MISMATCH",
|
|
1029
|
+
"Stored diagnostic provenance is not canonical JSON",
|
|
1030
|
+
"$",
|
|
1031
|
+
);
|
|
1032
|
+
}
|
|
1033
|
+
const regenerated = await createDiagnosticRunIdentity(input);
|
|
1034
|
+
const comparedSnapshot = provenanceIdentityView(snapshot);
|
|
1035
|
+
const comparedRegenerated = provenanceIdentityView(regenerated);
|
|
1036
|
+
if (canonicalJson(comparedSnapshot) !== canonicalJson(comparedRegenerated))
|
|
1037
|
+
throw new ComplianceError(
|
|
1038
|
+
"DIAGNOSTIC_MISMATCH",
|
|
1039
|
+
"Stored diagnostic provenance differs from regenerated provenance",
|
|
1040
|
+
firstDifference(comparedSnapshot, comparedRegenerated, "$") ?? "$",
|
|
1041
|
+
);
|
|
144
1042
|
return regenerated;
|
|
145
1043
|
}
|
|
146
1044
|
|
|
147
|
-
function
|
|
148
|
-
|
|
1045
|
+
function firstDifference(
|
|
1046
|
+
left: unknown,
|
|
1047
|
+
right: unknown,
|
|
1048
|
+
path: string,
|
|
1049
|
+
): string | null {
|
|
1050
|
+
if (plain(left) && plain(right)) {
|
|
1051
|
+
const keys = [
|
|
1052
|
+
...new Set([...Object.keys(left), ...Object.keys(right)]),
|
|
1053
|
+
].sort();
|
|
1054
|
+
for (const key of keys) {
|
|
1055
|
+
const childPath = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)
|
|
1056
|
+
? `${path}.${key}`
|
|
1057
|
+
: `${path}[${JSON.stringify(key)}]`;
|
|
1058
|
+
if (
|
|
1059
|
+
!Object.prototype.hasOwnProperty.call(left, key) ||
|
|
1060
|
+
!Object.prototype.hasOwnProperty.call(right, key)
|
|
1061
|
+
)
|
|
1062
|
+
return childPath;
|
|
1063
|
+
const found = firstDifference(left[key], right[key], childPath);
|
|
1064
|
+
if (found) return found;
|
|
1065
|
+
}
|
|
1066
|
+
return null;
|
|
1067
|
+
}
|
|
1068
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
1069
|
+
const shared = Math.min(left.length, right.length);
|
|
1070
|
+
for (let index = 0; index < shared; index++) {
|
|
1071
|
+
const found = firstDifference(
|
|
1072
|
+
left[index],
|
|
1073
|
+
right[index],
|
|
1074
|
+
`${path}[${index}]`,
|
|
1075
|
+
);
|
|
1076
|
+
if (found) return found;
|
|
1077
|
+
}
|
|
1078
|
+
return left.length === right.length ? null : `${path}[${shared}]`;
|
|
1079
|
+
}
|
|
1080
|
+
try {
|
|
1081
|
+
return canonicalJson(left) === canonicalJson(right) ? null : path;
|
|
1082
|
+
} catch {
|
|
1083
|
+
return path;
|
|
1084
|
+
}
|
|
1085
|
+
}
|