@quolu/lattice 0.79.5 → 0.81.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.
Files changed (56) hide show
  1. package/docs/schemas/lattice.plan_create_input.v4.schema.json +8 -4
  2. package/docs/schemas/lattice.todo_extraction.v3.schema.json +5 -3
  3. package/docs/schemas/lattice.todo_extraction.v4.schema.json +5 -3
  4. package/package.json +1 -1
  5. package/src/bridge-config.mjs +6 -7
  6. package/src/fs-publish.mjs +9 -0
  7. package/src/mcp-startup-receipt.mjs +3 -3
  8. package/src/plan-backchain.mjs +4 -3
  9. package/src/project-cli.mjs +22 -10
  10. package/src/runtime-multi-epoch-store.mjs +53 -13
  11. package/src/runtime-os-observation.mjs +18 -1
  12. package/src/seat-identity.mjs +23 -0
  13. package/src/store-lock.mjs +250 -0
  14. package/src/todo-cli.mjs +6 -0
  15. package/src/todo-contracts.mjs +95 -50
  16. package/src/todo-migration.mjs +35 -10
  17. package/src/todo-note-store.mjs +45 -12
  18. package/src/todo-readiness.mjs +154 -0
  19. package/src/todo-revision.mjs +7 -5
  20. package/src/todo-status.mjs +4 -33
  21. package/src/todo-store-git-transaction.mjs +11 -6
  22. package/src/todo-store-view.mjs +105 -0
  23. package/src/todo-store.mjs +602 -455
  24. package/src/artifact-contracts-v2.mjs +0 -325
  25. package/src/boundary-compiler.mjs +0 -712
  26. package/src/bounded-seam.mjs +0 -230
  27. package/src/control-compiler.mjs +0 -532
  28. package/src/rc1-black-box-oracle.mjs +0 -906
  29. package/src/rc1-comparison.mjs +0 -154
  30. package/src/rc1-evidence-bundle.mjs +0 -456
  31. package/src/rc1-v4-campaign.mjs +0 -708
  32. package/src/rc1-v4-transform.mjs +0 -467
  33. package/src/rc1-v5-artifact-set.mjs +0 -855
  34. package/src/rc1-v5-behavior-evidence.mjs +0 -594
  35. package/src/rc1-v5-campaign.mjs +0 -797
  36. package/src/rc1-v5-transform.mjs +0 -421
  37. package/src/rc1-v6-artifact-set.mjs +0 -807
  38. package/src/rc1-v6-behavior-evidence.mjs +0 -273
  39. package/src/rc1-v6-campaign.mjs +0 -623
  40. package/src/rc1-v6-causal-binding.mjs +0 -473
  41. package/src/rc1-v6-measurement.mjs +0 -313
  42. package/src/rc2-artifact-set.mjs +0 -1584
  43. package/src/rc2-campaign.mjs +0 -1495
  44. package/src/rc2-delivery-policy-front-end.mjs +0 -1079
  45. package/src/rc2-delivery-policy-oracle.mjs +0 -134
  46. package/src/rc2-delivery-policy-transform.mjs +0 -1127
  47. package/src/rc2-rc1-transfer-front-end.mjs +0 -511
  48. package/src/rc3-actual-dogfood.mjs +0 -652
  49. package/src/rc3-dogfood-scaffold.mjs +0 -315
  50. package/src/rc3-scripted-campaign.mjs +0 -1383
  51. package/src/rc4-stage1-dogfood.mjs +0 -673
  52. package/src/runtime-scripted-executor.mjs +0 -163
  53. package/src/runtime-worktree-executor.mjs +0 -199
  54. package/src/seam-transform.mjs +0 -554
  55. package/src/treatment-compiler.mjs +0 -728
  56. package/src/treatment-runner.mjs +0 -656
@@ -1,154 +0,0 @@
1
- import { canonicalizeArtifact, digestArtifact } from './artifact-contracts.mjs';
2
-
3
- const REQUIRED_INVALIDATIONS = new Set([
4
- 'old_plan',
5
- 'agent_context',
6
- 'partial_patch',
7
- 'interface_assumption',
8
- ]);
9
-
10
- function isPlainObject(value) {
11
- return value !== null
12
- && typeof value === 'object'
13
- && !Array.isArray(value)
14
- && Object.getPrototypeOf(value) === Object.prototype;
15
- }
16
-
17
- function sameArtifact(left, right) {
18
- try {
19
- return digestArtifact(left) === digestArtifact(right);
20
- } catch {
21
- return false;
22
- }
23
- }
24
-
25
- function summary(value) {
26
- return isPlainObject(value) ? value : {};
27
- }
28
-
29
- function passed(value) {
30
- return value === 'passed';
31
- }
32
-
33
- /**
34
- * RC1 v4 comparisonを宣言済み全条件から再評価する。artifact内の自己申告resultは参照しない。
35
- * @param {unknown} comparison
36
- * @returns {{schema: string, supported: boolean, checks: object[], failed_conditions: string[]}}
37
- */
38
- export function evaluateRc1Hypothesis(comparison) {
39
- try {
40
- canonicalizeArtifact(comparison);
41
- } catch {
42
- comparison = null;
43
- }
44
- const value = isPlainObject(comparison) ? comparison : {};
45
- const control = summary(value.control);
46
- const treatment = summary(value.treatment);
47
- const negativeControl = summary(value.negative_control);
48
- const negativeTreatment = summary(value.negative_treatment);
49
- const compiler = summary(value.compiler);
50
- const evidence = summary(value.evidence);
51
- const behavior = summary(value.behavior);
52
- const predecessor = summary(value.predecessor);
53
- const sourceInvariant = summary(value.source_invariant);
54
- const invalidations = Array.isArray(value.version_barrier?.invalidated_contexts)
55
- ? value.version_barrier.invalidated_contexts
56
- : [];
57
- const invalidationKinds = new Set(invalidations.map(({ kind }) => kind));
58
-
59
- const checks = [
60
- {
61
- id: 'schema',
62
- passed: value.schema === 'lattice.rc1.control_treatment_comparison.v2',
63
- },
64
- {
65
- id: 'compiler_identity',
66
- passed: compiler.condition_selector === 'forbidden'
67
- && compiler.control?.export_name === 'compileBoundaryCondition'
68
- && compiler.treatment?.export_name === 'compileBoundaryCondition'
69
- && typeof compiler.control?.source_digest === 'string'
70
- && compiler.control.source_digest === compiler.treatment?.source_digest,
71
- },
72
- {
73
- id: 'fixed_inputs',
74
- passed: sameArtifact(value.fixed_inputs?.control, value.fixed_inputs?.treatment),
75
- },
76
- {
77
- id: 'control_conflict',
78
- passed: control.verdict === 'seam_candidate'
79
- && Number.isSafeInteger(control.write_conflicts)
80
- && control.write_conflicts > 0
81
- && control.minimum_feasible_waves === 2,
82
- },
83
- {
84
- id: 'test_write_conflicts',
85
- passed: Number.isSafeInteger(control.test_write_conflicts)
86
- && control.test_write_conflicts > 0
87
- && treatment.test_write_conflicts === 0,
88
- },
89
- {
90
- id: 'treatment_parallel',
91
- passed: treatment.verdict === 'parallel_ready'
92
- && treatment.write_conflicts === 0
93
- && treatment.minimum_feasible_waves === 1,
94
- },
95
- {
96
- id: 'unknowns',
97
- passed: treatment.unknowns === 0,
98
- },
99
- {
100
- id: 'hard_precedence',
101
- passed: Number.isSafeInteger(control.hard_precedence)
102
- && treatment.hard_precedence === control.hard_precedence,
103
- },
104
- {
105
- id: 'negative_state',
106
- passed: negativeControl.state_conflicts > 0
107
- && negativeTreatment.state_conflicts > 0
108
- && negativeControl.minimum_feasible_waves === 2
109
- && negativeTreatment.minimum_feasible_waves === 2
110
- && negativeTreatment.verdict === 'intentional_serial',
111
- },
112
- {
113
- id: 'behavior',
114
- passed: passed(behavior.control?.outcome)
115
- && passed(behavior.treatment?.outcome)
116
- && behavior.control?.oracle_digest === behavior.treatment?.oracle_digest,
117
- },
118
- {
119
- id: 'portable_preimages',
120
- passed: evidence.control?.portable_preimages_complete === true
121
- && evidence.treatment?.portable_preimages_complete === true
122
- && evidence.control?.digests_recomputed === true
123
- && evidence.treatment?.digests_recomputed === true,
124
- },
125
- {
126
- id: 'sanitized_diagnostics',
127
- passed: evidence.control?.diagnostics_sanitized === true
128
- && evidence.treatment?.diagnostics_sanitized === true,
129
- },
130
- {
131
- id: 'predecessor',
132
- passed: predecessor.transform_status === 'accepted'
133
- && predecessor.same_base === true
134
- && predecessor.fixed_inputs_bound === true,
135
- },
136
- {
137
- id: 'version_barrier',
138
- passed: invalidations.length === REQUIRED_INVALIDATIONS.size
139
- && REQUIRED_INVALIDATIONS.size === invalidationKinds.size
140
- && [...REQUIRED_INVALIDATIONS].every((kind) => invalidationKinds.has(kind)),
141
- },
142
- {
143
- id: 'source_invariant',
144
- passed: passed(sourceInvariant.control) && passed(sourceInvariant.treatment),
145
- },
146
- ];
147
- const failedConditions = checks.filter((check) => !check.passed).map(({ id }) => id);
148
- return {
149
- schema: 'lattice.rc1.hypothesis_evaluation.v2',
150
- supported: failedConditions.length === 0,
151
- checks,
152
- failed_conditions: failedConditions,
153
- };
154
- }
@@ -1,456 +0,0 @@
1
- import { createHash } from 'node:crypto';
2
-
3
- import {
4
- canonicalizeArtifact,
5
- digestArtifact,
6
- } from './artifact-contracts.mjs';
7
- import { portableSensorOutcome } from './sensor-adapter.mjs';
8
-
9
- const IDENTIFIER = /^[0-9A-Za-z](?:[0-9A-Za-z._-]{0,127})$/;
10
- const SHA256 = /^[0-9a-f]{64}$/;
11
- const CONDITIONS = new Set(['control', 'treatment']);
12
- const OPERATIONS = new Set(['status', 'query', 'callers', 'callees', 'impact', 'affected']);
13
- const PROJECTION = 'lattice.sensor_portable_outcome.v1';
14
- const SANITIZER = 'lattice.sensor_diagnostic_sanitizer.v1';
15
- const STATUS_REMOVALS = Object.freeze([
16
- 'projectPath',
17
- 'indexPath',
18
- 'lastIndexed',
19
- 'dbSizeBytes',
20
- ]);
21
- const SANITIZATION_RULES = Object.freeze([
22
- { selector: '/cwd', action: 'replace', replacement: '<repo-root>' },
23
- ...STATUS_REMOVALS.map((field) => ({
24
- selector: `/outcomes/*[operation=status]/data/${field}`,
25
- action: 'remove',
26
- replacement: null,
27
- })),
28
- { selector: '/outcomes/**/node/updatedAt', action: 'remove', replacement: null },
29
- ]);
30
-
31
- function isPlainObject(value) {
32
- return value !== null
33
- && typeof value === 'object'
34
- && !Array.isArray(value)
35
- && Object.getPrototypeOf(value) === Object.prototype;
36
- }
37
-
38
- function exactRecord(value, keys) {
39
- if (!isPlainObject(value)) return false;
40
- const actual = Object.keys(value).sort();
41
- const expected = [...keys].sort();
42
- return actual.length === expected.length
43
- && actual.every((key, index) => key === expected[index]);
44
- }
45
-
46
- function identifier(value) {
47
- return typeof value === 'string' && IDENTIFIER.test(value);
48
- }
49
-
50
- function fail(reason) {
51
- throw new TypeError(`RC1 v4 evidence bundle契約違反: ${reason}`);
52
- }
53
-
54
- function sha256(value) {
55
- return createHash('sha256').update(value).digest('hex');
56
- }
57
-
58
- function escapePointer(value) {
59
- return String(value).replaceAll('~', '~0').replaceAll('/', '~1');
60
- }
61
-
62
- function sameArtifact(left, right) {
63
- try {
64
- return digestArtifact(left) === digestArtifact(right);
65
- } catch {
66
- return false;
67
- }
68
- }
69
-
70
- function validateQuerySet(querySet) {
71
- if (!exactRecord(querySet, ['schema', 'queries'])
72
- || querySet.schema !== 'lattice.sensor_query_set.v2'
73
- || !Array.isArray(querySet.queries)
74
- || querySet.queries.length === 0
75
- || querySet.queries.length > 256) {
76
- return false;
77
- }
78
- const ids = new Set();
79
- for (const query of querySet.queries) {
80
- if (!isPlainObject(query) || !identifier(query.id) || ids.has(query.id)
81
- || !OPERATIONS.has(query.operation)) {
82
- return false;
83
- }
84
- ids.add(query.id);
85
- if (query.operation === 'status') {
86
- if (!exactRecord(query, ['id', 'operation'])) return false;
87
- } else if (query.operation === 'affected') {
88
- if (!exactRecord(query, ['id', 'operation', 'targets'])
89
- || !Array.isArray(query.targets)
90
- || query.targets.length === 0
91
- || query.targets.some((target) => typeof target !== 'string' || target.length === 0)) {
92
- return false;
93
- }
94
- } else if (!exactRecord(query, ['id', 'operation', 'target'])
95
- || typeof query.target !== 'string'
96
- || query.target.length === 0) {
97
- return false;
98
- }
99
- }
100
- return true;
101
- }
102
-
103
- function validateRawEvidence(rawEvidence, querySet) {
104
- if (!exactRecord(rawEvidence, ['cwd', 'outcomes'])
105
- || typeof rawEvidence.cwd !== 'string'
106
- || rawEvidence.cwd.length === 0
107
- || !Array.isArray(rawEvidence.outcomes)
108
- || rawEvidence.outcomes.length !== querySet.queries.length) {
109
- return false;
110
- }
111
- for (let index = 0; index < querySet.queries.length; index += 1) {
112
- const query = querySet.queries[index];
113
- const outcome = rawEvidence.outcomes[index];
114
- if (!isPlainObject(outcome)
115
- || outcome.id !== query.id
116
- || outcome.operation !== query.operation
117
- || typeof outcome.outcome !== 'string'
118
- || outcome.outcome.length === 0) {
119
- return false;
120
- }
121
- if (query.operation !== 'status' && query.operation !== 'affected'
122
- && outcome.target !== query.target) {
123
- return false;
124
- }
125
- if (query.operation === 'affected'
126
- && (!Array.isArray(outcome.targets)
127
- || outcome.targets.length !== query.targets.length
128
- || outcome.targets.some((entry, targetIndex) => (
129
- !isPlainObject(entry) || entry.target !== query.targets[targetIndex]
130
- )))) {
131
- return false;
132
- }
133
- }
134
- canonicalizeArtifact(rawEvidence);
135
- return true;
136
- }
137
-
138
- function scrubNodeTelemetry(value, pointer, operations, parentKey = null) {
139
- if (Array.isArray(value)) {
140
- value.forEach((entry, index) => scrubNodeTelemetry(
141
- entry,
142
- `${pointer}/${index}`,
143
- operations,
144
- parentKey,
145
- ));
146
- return;
147
- }
148
- if (!isPlainObject(value)) return;
149
- if (parentKey === 'node' && Object.hasOwn(value, 'updatedAt')) {
150
- delete value.updatedAt;
151
- operations.push({ path: `${pointer}/updatedAt`, action: 'remove', replacement: null });
152
- }
153
- for (const [key, entry] of Object.entries(value)) {
154
- scrubNodeTelemetry(entry, `${pointer}/${escapePointer(key)}`, operations, key);
155
- }
156
- }
157
-
158
- function sanitizeDiagnostic(rawEvidence) {
159
- const payload = structuredClone(rawEvidence);
160
- const operations = [{ path: '/cwd', action: 'replace', replacement: '<repo-root>' }];
161
- payload.cwd = '<repo-root>';
162
- payload.outcomes.forEach((outcome, index) => {
163
- if (outcome.operation === 'status' && isPlainObject(outcome.data)) {
164
- for (const field of STATUS_REMOVALS) {
165
- if (Object.hasOwn(outcome.data, field)) {
166
- delete outcome.data[field];
167
- operations.push({
168
- path: `/outcomes/${index}/data/${escapePointer(field)}`,
169
- action: 'remove',
170
- replacement: null,
171
- });
172
- }
173
- }
174
- }
175
- scrubNodeTelemetry(outcome, `/outcomes/${index}`, operations);
176
- });
177
- return { payload, operations };
178
- }
179
-
180
- function stringHasAbsolutePath(value) {
181
- return /\bfile:\/\/\//i.test(value)
182
- || /(?:^|[\s("'=:\[])\/(?!\/)[^\s"')\]]+/.test(value)
183
- || /(?:^|[\s("'=:\[])[A-Za-z]:[\\/][^\s"')\]]*/.test(value);
184
- }
185
-
186
- function containsAbsolutePath(value) {
187
- if (typeof value === 'string') return stringHasAbsolutePath(value);
188
- if (Array.isArray(value)) return value.some(containsAbsolutePath);
189
- if (isPlainObject(value)) return Object.values(value).some(containsAbsolutePath);
190
- return false;
191
- }
192
-
193
- function rawReceipt(rawEvidence) {
194
- const canonical = canonicalizeArtifact(rawEvidence);
195
- const bytes = Buffer.from(canonical, 'utf8');
196
- return {
197
- schema: 'lattice.sensor_raw_opaque_receipt.v1',
198
- media_type: 'application/vnd.lattice.sensor-evidence+json',
199
- encoding: 'canonical-json-base64',
200
- payload_base64: bytes.toString('base64'),
201
- payload_digest: sha256(bytes),
202
- canonical_bytes: bytes.byteLength,
203
- };
204
- }
205
-
206
- function diagnosticArtifact(rawEvidence, sourceRawDigest) {
207
- const { payload, operations } = sanitizeDiagnostic(rawEvidence);
208
- if (containsAbsolutePath(payload)) fail('sanitized diagnosticに絶対pathが残る');
209
- const manifest = {
210
- schema: 'lattice.sensor_sanitization_manifest.v1',
211
- sanitizer: SANITIZER,
212
- rules_digest: digestArtifact(SANITIZATION_RULES),
213
- operations,
214
- };
215
- return {
216
- schema: 'lattice.sensor_sanitized_diagnostic.v1',
217
- source_raw_digest: sourceRawDigest,
218
- sanitization_manifest: manifest,
219
- sanitization_manifest_digest: digestArtifact(manifest),
220
- payload,
221
- payload_digest: digestArtifact(payload),
222
- };
223
- }
224
-
225
- function buildPortable(querySet, rawEvidence) {
226
- const querySetDigest = digestArtifact(querySet);
227
- const outcomes = rawEvidence.outcomes.map((outcome) => portableSensorOutcome(outcome));
228
- if (containsAbsolutePath(outcomes)) fail('portable outcomeに絶対pathが残る');
229
- const perQuery = outcomes.map((outcome, index) => ({
230
- id: querySet.queries[index].id,
231
- operation: querySet.queries[index].operation,
232
- outcome: outcome.outcome,
233
- result_digest: digestArtifact(outcome),
234
- }));
235
- const portable = {
236
- schema: 'lattice.sensor_portable_preimage.v1',
237
- projection: PROJECTION,
238
- query_set_digest: querySetDigest,
239
- outcomes,
240
- per_query: perQuery,
241
- aggregate_digest: '',
242
- };
243
- portable.aggregate_digest = recomputePortableAggregate(portable);
244
- return portable;
245
- }
246
-
247
- /** full portable payloadだけからaggregate digestを再計算する。 */
248
- export function recomputePortableAggregate(portable) {
249
- if (!isPlainObject(portable)
250
- || portable.schema !== 'lattice.sensor_portable_preimage.v1'
251
- || portable.projection !== PROJECTION
252
- || typeof portable.query_set_digest !== 'string'
253
- || !SHA256.test(portable.query_set_digest)
254
- || !Array.isArray(portable.outcomes)) {
255
- fail('portable preimageが再計算可能なshapeでない');
256
- }
257
- return digestArtifact({
258
- projection: portable.projection,
259
- query_set_digest: portable.query_set_digest,
260
- outcomes: portable.outcomes,
261
- });
262
- }
263
-
264
- function decodeRawReceipt(receipt) {
265
- if (!exactRecord(receipt, [
266
- 'schema',
267
- 'media_type',
268
- 'encoding',
269
- 'payload_base64',
270
- 'payload_digest',
271
- 'canonical_bytes',
272
- ])
273
- || receipt.schema !== 'lattice.sensor_raw_opaque_receipt.v1'
274
- || receipt.media_type !== 'application/vnd.lattice.sensor-evidence+json'
275
- || receipt.encoding !== 'canonical-json-base64'
276
- || typeof receipt.payload_base64 !== 'string'
277
- || typeof receipt.payload_digest !== 'string'
278
- || !SHA256.test(receipt.payload_digest)
279
- || !Number.isSafeInteger(receipt.canonical_bytes)
280
- || receipt.canonical_bytes <= 0) {
281
- fail('raw opaque receiptが不正');
282
- }
283
- const bytes = Buffer.from(receipt.payload_base64, 'base64');
284
- if (bytes.toString('base64') !== receipt.payload_base64
285
- || bytes.byteLength !== receipt.canonical_bytes
286
- || sha256(bytes) !== receipt.payload_digest) {
287
- fail('raw opaque receiptのbytes/digestが一致しない');
288
- }
289
- const payload = JSON.parse(bytes.toString('utf8'));
290
- if (canonicalizeArtifact(payload) !== bytes.toString('utf8')) {
291
- fail('raw opaque payloadがcanonical JSONでない');
292
- }
293
- return payload;
294
- }
295
-
296
- function validatePortable(portable, querySet, rawEvidence) {
297
- if (!exactRecord(portable, [
298
- 'schema',
299
- 'projection',
300
- 'query_set_digest',
301
- 'outcomes',
302
- 'per_query',
303
- 'aggregate_digest',
304
- ])
305
- || portable.schema !== 'lattice.sensor_portable_preimage.v1'
306
- || portable.projection !== PROJECTION
307
- || portable.query_set_digest !== digestArtifact(querySet)
308
- || !Array.isArray(portable.outcomes)
309
- || portable.outcomes.length !== querySet.queries.length
310
- || !Array.isArray(portable.per_query)
311
- || portable.per_query.length !== portable.outcomes.length
312
- || typeof portable.aggregate_digest !== 'string'
313
- || !SHA256.test(portable.aggregate_digest)
314
- || containsAbsolutePath(portable.outcomes)) {
315
- return false;
316
- }
317
- for (let index = 0; index < portable.outcomes.length; index += 1) {
318
- const record = portable.per_query[index];
319
- if (!exactRecord(record, ['id', 'operation', 'outcome', 'result_digest'])
320
- || record.id !== querySet.queries[index].id
321
- || record.operation !== querySet.queries[index].operation
322
- || record.outcome !== portable.outcomes[index].outcome
323
- || record.result_digest !== digestArtifact(portable.outcomes[index])) {
324
- return false;
325
- }
326
- }
327
- return portable.aggregate_digest === recomputePortableAggregate(portable)
328
- && sameArtifact(portable, buildPortable(querySet, rawEvidence));
329
- }
330
-
331
- function validateDiagnostic(diagnostic, rawEvidence, rawDigest) {
332
- if (!exactRecord(diagnostic, [
333
- 'schema',
334
- 'source_raw_digest',
335
- 'sanitization_manifest',
336
- 'sanitization_manifest_digest',
337
- 'payload',
338
- 'payload_digest',
339
- ])
340
- || diagnostic.schema !== 'lattice.sensor_sanitized_diagnostic.v1'
341
- || diagnostic.source_raw_digest !== rawDigest
342
- || diagnostic.sanitization_manifest_digest
343
- !== digestArtifact(diagnostic.sanitization_manifest)
344
- || diagnostic.payload_digest !== digestArtifact(diagnostic.payload)
345
- || containsAbsolutePath(diagnostic.payload)) {
346
- return false;
347
- }
348
- return sameArtifact(diagnostic, diagnosticArtifact(rawEvidence, rawDigest));
349
- }
350
-
351
- /**
352
- * 1 fresh LatticeSensor runをraw/diagnostic/portableの独立componentへcompileする。
353
- */
354
- export function createRc1EvidenceBundle({
355
- condition,
356
- runId,
357
- querySet,
358
- rawEvidence,
359
- } = {}) {
360
- if (!CONDITIONS.has(condition) || !identifier(runId)
361
- || !validateQuerySet(querySet) || !validateRawEvidence(rawEvidence, querySet)) {
362
- fail('bundle inputが不正');
363
- }
364
- const fixedQuerySet = structuredClone(querySet);
365
- const fixedRaw = structuredClone(rawEvidence);
366
- const raw = rawReceipt(fixedRaw);
367
- const diagnostic = diagnosticArtifact(fixedRaw, raw.payload_digest);
368
- const portable = buildPortable(fixedQuerySet, fixedRaw);
369
- const componentDigests = {
370
- raw: raw.payload_digest,
371
- diagnostic_payload: diagnostic.payload_digest,
372
- sanitization_manifest: diagnostic.sanitization_manifest_digest,
373
- portable: portable.aggregate_digest,
374
- };
375
- return {
376
- schema: 'lattice.rc1.evidence_bundle.v1',
377
- condition,
378
- run_id: runId,
379
- query_set: fixedQuerySet,
380
- query_set_digest: digestArtifact(fixedQuerySet),
381
- raw,
382
- diagnostic,
383
- portable,
384
- component_digests: componentDigests,
385
- };
386
- }
387
-
388
- function validateBundle(value) {
389
- if (!exactRecord(value, [
390
- 'schema',
391
- 'condition',
392
- 'run_id',
393
- 'query_set',
394
- 'query_set_digest',
395
- 'raw',
396
- 'diagnostic',
397
- 'portable',
398
- 'component_digests',
399
- ])
400
- || value.schema !== 'lattice.rc1.evidence_bundle.v1'
401
- || !CONDITIONS.has(value.condition)
402
- || !identifier(value.run_id)
403
- || !validateQuerySet(value.query_set)
404
- || value.query_set_digest !== digestArtifact(value.query_set)
405
- || !exactRecord(value.component_digests, [
406
- 'raw',
407
- 'diagnostic_payload',
408
- 'sanitization_manifest',
409
- 'portable',
410
- ])) {
411
- return false;
412
- }
413
- const rawEvidence = decodeRawReceipt(value.raw);
414
- if (!validateRawEvidence(rawEvidence, value.query_set)
415
- || !validateDiagnostic(value.diagnostic, rawEvidence, value.raw.payload_digest)
416
- || !validatePortable(value.portable, value.query_set, rawEvidence)) {
417
- return false;
418
- }
419
- return value.component_digests.raw === value.raw.payload_digest
420
- && value.component_digests.diagnostic_payload === value.diagnostic.payload_digest
421
- && value.component_digests.sanitization_manifest
422
- === value.diagnostic.sanitization_manifest_digest
423
- && value.component_digests.portable === value.portable.aggregate_digest;
424
- }
425
-
426
- /** @param {unknown} value @returns {boolean} */
427
- export function validateRc1EvidenceBundle(value) {
428
- try {
429
- return validateBundle(value);
430
- } catch {
431
- return false;
432
- }
433
- }
434
-
435
- /** control/treatment各2 fresh runのportable/diagnostic再現性を検証する。 */
436
- export function validateRc1EvidenceCampaign(value) {
437
- try {
438
- if (!Array.isArray(value) || value.length !== 4
439
- || value.some((bundle) => !validateBundle(bundle))
440
- || new Set(value.map(({ run_id: runId }) => runId)).size !== value.length
441
- || new Set(value.map(({ query_set_digest: digest }) => digest)).size !== 1) {
442
- return false;
443
- }
444
- for (const condition of CONDITIONS) {
445
- const runs = value.filter((bundle) => bundle.condition === condition);
446
- if (runs.length !== 2
447
- || runs[0].portable.aggregate_digest !== runs[1].portable.aggregate_digest
448
- || runs[0].diagnostic.payload_digest !== runs[1].diagnostic.payload_digest) {
449
- return false;
450
- }
451
- }
452
- return true;
453
- } catch {
454
- return false;
455
- }
456
- }