@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,807 +0,0 @@
1
- import { createHash } from 'node:crypto';
2
-
3
- import {
4
- digestArtifact,
5
- validateBoundaryManifest,
6
- validateBoundaryVerdict,
7
- validatePlanGraph,
8
- validatePlanInput,
9
- validateTransformArtifact,
10
- } from './artifact-contracts.mjs';
11
- import { RC1_BOUNDARY_COMPILER_CONTRACT } from './boundary-compiler.mjs';
12
- import { validateRc1BlackBoxOracle } from './rc1-black-box-oracle.mjs';
13
- import { verifyRc1V6BehaviorEvidence } from './rc1-v6-behavior-evidence.mjs';
14
- import {
15
- createRc1V6EvidenceBundleDescriptor,
16
- verifyRc1V6PlanPredecessors,
17
- verifyRc1V6RunEvidence,
18
- } from './rc1-v6-causal-binding.mjs';
19
- import {
20
- compileRc1V6BoundaryCondition,
21
- sourceSnapshotFromRc1BehaviorSurface,
22
- sourceSnapshotFromRc1TransformOutput,
23
- } from './rc1-v6-measurement.mjs';
24
-
25
- const SHA256 = /^[0-9a-f]{64}$/;
26
- const GIT_SHA1 = /^[0-9a-f]{40}$/;
27
- const REJECTED_PLAN_REF =
28
- 'docs/archive/2026-07-16-plan-lattice-research-campaign-1-v5-phase-rejected.md';
29
- const PHASE_DECISION_REF = 'docs/adr/0028-rc1-v5-phase-gate-rejection.md';
30
- const INPUT_PATHS = Object.freeze({
31
- planInput: 'inputs/plan-input.json',
32
- candidateSpec: 'inputs/candidate-spec-v2.json',
33
- normalManualEvidence: 'inputs/manual-evidence.normal.json',
34
- negativeManualEvidence: 'inputs/manual-evidence.shared-state-negative.json',
35
- querySet: 'inputs/query-set-v2.json',
36
- oracle: 'inputs/behavior-oracle-v2.json',
37
- runtimeIdentity: 'inputs/oracle-runtime-identity.json',
38
- sensorIdentity: 'inputs/lattice-sensor-identity.json',
39
- });
40
- const RUN_IDS = Object.freeze(['control-1', 'control-2', 'treatment-1', 'treatment-2']);
41
- const IDENTITY_PATHS = Object.freeze({
42
- compiler: 'identity/boundary-compiler.mjs',
43
- oracleExecutor: 'identity/black-box-oracle.mjs',
44
- sensorExecutable: 'identity/lattice-sensor-executable',
45
- });
46
- const INVALIDATED_CONTEXTS = Object.freeze([
47
- {
48
- kind: 'old_plan',
49
- ref: 'lattice-research-campaign-1-v5',
50
- reason: 'v5 Phase support was rejected by ADR 0028 and cannot receive v6 topology updates',
51
- },
52
- {
53
- kind: 'agent_context',
54
- ref: 'lattice-research-campaign-1-v5-agent-context',
55
- reason: 'v5 context does not bind saved oracle semantics, runtime, or snapshot preimages',
56
- },
57
- {
58
- kind: 'partial_patch',
59
- ref: 'lattice-research-campaign-1-v5-partial-patch',
60
- reason: 'patches compiled from unbound v5 measurement identity cannot cross the v6 barrier',
61
- },
62
- {
63
- kind: 'interface_assumption',
64
- ref: 'self-reported-digest-is-causal-identity',
65
- reason: 'v6 requires typed preimages and exact predecessor descriptors',
66
- },
67
- ]);
68
-
69
- function exactRecord(value, keys) {
70
- if (value === null || typeof value !== 'object' || Array.isArray(value)
71
- || Object.getPrototypeOf(value) !== Object.prototype) {
72
- return false;
73
- }
74
- const actual = Object.keys(value).sort();
75
- const expected = [...keys].sort();
76
- return actual.length === expected.length
77
- && actual.every((key, index) => key === expected[index]);
78
- }
79
-
80
- function sha256(bytes) {
81
- return createHash('sha256').update(bytes).digest('hex');
82
- }
83
-
84
- function sameArtifact(left, right) {
85
- try {
86
- return digestArtifact(left) === digestArtifact(right);
87
- } catch {
88
- return false;
89
- }
90
- }
91
-
92
- function jsonBytes(value) {
93
- return Buffer.from(`${JSON.stringify(value, null, 2)}\n`, 'utf8');
94
- }
95
-
96
- function parseJson(bytes) {
97
- try {
98
- const value = JSON.parse(bytes.toString('utf8'));
99
- return jsonBytes(value).equals(bytes) ? value : null;
100
- } catch {
101
- return null;
102
- }
103
- }
104
-
105
- function compilationSummary(compiled) {
106
- const conflicts = compiled.boundary_manifest.conflicts;
107
- return {
108
- boundary_manifest_digest: compiled.boundary_manifest_digest,
109
- boundary_verdict_digest: compiled.boundary_verdict_digest,
110
- plan_graph_digest: compiled.plan_graph_digest,
111
- verdict: compiled.boundary_verdict.verdicts[0].verdict,
112
- write_conflicts: conflicts.filter(({ kind }) => kind === 'write_boundary').length,
113
- test_write_conflicts: conflicts.filter(({ kind, resource }) => (
114
- kind === 'write_boundary'
115
- && resource.kind === 'path'
116
- && resource.target.startsWith('test/')
117
- )).length,
118
- state_conflicts: conflicts.filter(({ kind }) => kind === 'state').length,
119
- hard_precedence: compiled.boundary_manifest.todos
120
- .reduce((count, todo) => count + todo.hard_needs.length, 0),
121
- unknowns: compiled.boundary_manifest.unknowns.length,
122
- minimum_feasible_waves: compiled.plan_graph.minimum_feasible_waves,
123
- };
124
- }
125
-
126
- function fixedInputIdentity(inputs, runtimeIdentity, sensorIdentity) {
127
- return {
128
- plan_input: digestArtifact(inputs.planInput),
129
- candidate_spec: digestArtifact(inputs.candidateSpec),
130
- normal_manual_evidence: digestArtifact(inputs.normalManualEvidence),
131
- negative_manual_evidence: digestArtifact(inputs.negativeManualEvidence),
132
- query_set: digestArtifact(inputs.querySet),
133
- behavior_oracle: digestArtifact(inputs.oracle),
134
- oracle_runtime: digestArtifact(runtimeIdentity),
135
- sensor_identity: digestArtifact(sensorIdentity),
136
- capacity_writers: inputs.planInput.capacity.writers,
137
- };
138
- }
139
-
140
- function compiledConditionSummary(condition, bundles) {
141
- return {
142
- normal: compilationSummary(condition.normal),
143
- negative: compilationSummary(condition.negative),
144
- snapshot_digest: bundles[0].measurement.snapshot_digest,
145
- evidence_bundle_descriptor_digests: bundles.map((bundle) => (
146
- digestArtifact(createRc1V6EvidenceBundleDescriptor(bundle))
147
- )),
148
- };
149
- }
150
-
151
- export function buildRc1V6PlanDiff({
152
- control,
153
- treatment,
154
- transform,
155
- evidenceBundles,
156
- rejectedPlanBytes,
157
- phaseDecisionBytes,
158
- } = {}) {
159
- const before = compilationSummary(control.normal);
160
- const after = compilationSummary(treatment.normal);
161
- const predecessors = [
162
- {
163
- kind: 'rejected_plan_archive',
164
- ref: REJECTED_PLAN_REF,
165
- digest: sha256(rejectedPlanBytes),
166
- },
167
- {
168
- kind: 'phase_decision',
169
- ref: PHASE_DECISION_REF,
170
- digest: sha256(phaseDecisionBytes),
171
- },
172
- {
173
- kind: 'accepted_transform',
174
- ref: 'transform/transform-artifact.json',
175
- digest: digestArtifact(transform.artifact),
176
- },
177
- {
178
- kind: 'behavior_envelope',
179
- ref: 'behavior/evidence-envelope.json',
180
- digest: transform.behavior_evidence.envelope.envelope_digest,
181
- },
182
- ...evidenceBundles.map((bundle) => ({
183
- kind: 'evidence_bundle',
184
- ref: `evidence/${bundle.run_id}.json`,
185
- digest: digestArtifact(createRc1V6EvidenceBundleDescriptor(bundle)),
186
- })),
187
- ];
188
- return {
189
- schema: 'lattice.plan_diff.v3',
190
- old_plan: {
191
- version: control.normal.plan_graph.plan_version,
192
- digest: control.normal.plan_graph_digest,
193
- },
194
- new_plan: {
195
- version: treatment.normal.plan_graph.plan_version,
196
- digest: treatment.normal.plan_graph_digest,
197
- },
198
- causal_predecessors: predecessors,
199
- transform: {
200
- status: transform.artifact.status,
201
- artifact_digest: transform.artifact_digest,
202
- receipt_digest: transform.receipt_digest,
203
- patch_digest: transform.artifact.patch.digest,
204
- changed_paths: [...transform.artifact.scope.changed_paths],
205
- },
206
- nodes: {
207
- added: [],
208
- removed: [],
209
- changed: control.normal.plan_graph.nodes.map(({ id }) => id),
210
- },
211
- edges: {
212
- added: treatment.normal.plan_graph.edges.map(({ id }) => id),
213
- removed: control.normal.plan_graph.edges.map(({ id }) => id),
214
- },
215
- invalidated_contexts: structuredClone(INVALIDATED_CONTEXTS),
216
- metrics: {
217
- write_conflicts_before: before.write_conflicts,
218
- write_conflicts_after: after.write_conflicts,
219
- test_write_conflicts_before: before.test_write_conflicts,
220
- test_write_conflicts_after: after.test_write_conflicts,
221
- hard_precedence_before: before.hard_precedence,
222
- hard_precedence_after: after.hard_precedence,
223
- minimum_feasible_waves_before: before.minimum_feasible_waves,
224
- minimum_feasible_waves_after: after.minimum_feasible_waves,
225
- },
226
- };
227
- }
228
-
229
- export function compileRc1V6Comparison({
230
- baseSha,
231
- inputs,
232
- runtimeIdentity,
233
- sensorIdentity,
234
- compilerSourceDigest,
235
- control,
236
- treatment,
237
- evidenceBundles,
238
- transform,
239
- planDiff,
240
- sourceInvariant,
241
- } = {}) {
242
- const controlBundles = evidenceBundles.filter(({ condition }) => condition === 'control');
243
- const treatmentBundles = evidenceBundles.filter(({ condition }) => condition === 'treatment');
244
- return {
245
- schema: 'lattice.rc1.control_treatment_comparison.v4',
246
- base_sha: baseSha,
247
- fixed_inputs: fixedInputIdentity(inputs, runtimeIdentity, sensorIdentity),
248
- independent_variable: {
249
- kind: 'accepted_production_and_test_seam',
250
- transform_artifact_digest: transform.artifact_digest,
251
- transform_receipt_digest: transform.receipt_digest,
252
- patch_digest: transform.artifact.patch.digest,
253
- },
254
- compiler: {
255
- export_name: RC1_BOUNDARY_COMPILER_CONTRACT.export_name,
256
- source_digest: compilerSourceDigest,
257
- condition_selector: RC1_BOUNDARY_COMPILER_CONTRACT.condition_selector,
258
- },
259
- sensor_identity: structuredClone(sensorIdentity),
260
- oracle_runtime_identity: structuredClone(runtimeIdentity),
261
- conditions: {
262
- control: compiledConditionSummary(control, controlBundles),
263
- treatment: compiledConditionSummary(treatment, treatmentBundles),
264
- },
265
- behavior: {
266
- evidence_envelope_digest: transform.behavior_evidence.envelope.envelope_digest,
267
- },
268
- source_invariant: structuredClone(sourceInvariant),
269
- version_barrier: {
270
- plan_diff_digest: digestArtifact(planDiff),
271
- causal_predecessors_digest: digestArtifact(planDiff.causal_predecessors),
272
- },
273
- };
274
- }
275
-
276
- export function evaluateRc1V6Hypothesis(comparison) {
277
- const control = comparison?.conditions?.control;
278
- const treatment = comparison?.conditions?.treatment;
279
- const checks = [
280
- {
281
- id: 'compiler_fixed',
282
- passed: comparison?.compiler?.condition_selector === 'forbidden'
283
- && SHA256.test(comparison?.compiler?.source_digest),
284
- },
285
- {
286
- id: 'sensor_identity_fixed',
287
- passed: comparison?.sensor_identity?.schema === 'lattice.rc1.sensor_identity.v1',
288
- },
289
- {
290
- id: 'runtime_identity_fixed',
291
- passed: comparison?.oracle_runtime_identity?.schema
292
- === 'lattice.rc1.oracle_runtime_identity.v1',
293
- },
294
- {
295
- id: 'control_conflict_observed',
296
- passed: control?.normal?.write_conflicts > 0
297
- && control?.normal?.test_write_conflicts > 0
298
- && control?.normal?.minimum_feasible_waves >= 2,
299
- },
300
- {
301
- id: 'treatment_conflict_removed',
302
- passed: treatment?.normal?.write_conflicts === 0
303
- && treatment?.normal?.test_write_conflicts === 0,
304
- },
305
- { id: 'treatment_unknowns_closed', passed: treatment?.normal?.unknowns === 0 },
306
- { id: 'treatment_one_wave', passed: treatment?.normal?.minimum_feasible_waves === 1 },
307
- {
308
- id: 'hard_precedence_not_increased',
309
- passed: treatment?.normal?.hard_precedence <= control?.normal?.hard_precedence,
310
- },
311
- {
312
- id: 'negative_state_serial_preserved',
313
- passed: control?.negative?.state_conflicts > 0
314
- && treatment?.negative?.state_conflicts > 0
315
- && treatment?.negative?.minimum_feasible_waves >= 2,
316
- },
317
- {
318
- id: 'two_fresh_runs_per_condition',
319
- passed: control?.evidence_bundle_descriptor_digests?.length === 2
320
- && treatment?.evidence_bundle_descriptor_digests?.length === 2,
321
- },
322
- {
323
- id: 'snapshot_changed_only_by_intervention',
324
- passed: SHA256.test(control?.snapshot_digest)
325
- && SHA256.test(treatment?.snapshot_digest)
326
- && control.snapshot_digest !== treatment.snapshot_digest,
327
- },
328
- {
329
- id: 'behavior_bound',
330
- passed: SHA256.test(comparison?.behavior?.evidence_envelope_digest),
331
- },
332
- {
333
- id: 'source_invariant',
334
- passed: comparison?.source_invariant?.control === true
335
- && comparison?.source_invariant?.treatment === true,
336
- },
337
- {
338
- id: 'version_barrier',
339
- passed: SHA256.test(comparison?.version_barrier?.plan_diff_digest)
340
- && SHA256.test(comparison?.version_barrier?.causal_predecessors_digest),
341
- },
342
- ];
343
- const failedConditions = checks.filter(({ passed }) => !passed).map(({ id }) => id);
344
- return {
345
- schema: 'lattice.rc1.hypothesis_evaluation.v4',
346
- hypothesis_id: 'H1-v6',
347
- supported: failedConditions.length === 0,
348
- checks,
349
- failed_conditions: failedConditions,
350
- };
351
- }
352
-
353
- function compiledPaths() {
354
- const paths = [];
355
- for (const condition of ['control', 'treatment']) {
356
- for (const variant of ['normal', 'negative']) {
357
- const root = `compiled/${condition}-${variant}`;
358
- paths.push(
359
- `${root}/boundary-manifest.json`,
360
- `${root}/boundary-verdict.json`,
361
- `${root}/plan.json`,
362
- );
363
- }
364
- }
365
- return paths;
366
- }
367
-
368
- const EXPECTED_PATHS = Object.freeze([
369
- ...Object.values(INPUT_PATHS),
370
- ...Object.values(IDENTITY_PATHS),
371
- ...RUN_IDS.map((runId) => `evidence/${runId}.json`),
372
- ...RUN_IDS.map((runId) => `runs/${runId}.json`),
373
- ...compiledPaths(),
374
- 'behavior/pre-receipt.json',
375
- 'behavior/post-receipt.json',
376
- 'behavior/evidence-envelope.json',
377
- 'transform/transform-artifact.json',
378
- 'transform/transform-receipt.json',
379
- 'transform/seam.patch',
380
- 'plan-diff.json',
381
- 'comparison.json',
382
- 'hypothesis-evaluation.json',
383
- 'execution-evidence.json',
384
- `predecessor/${REJECTED_PLAN_REF}`,
385
- `predecessor/${PHASE_DECISION_REF}`,
386
- ].sort());
387
-
388
- /** v6 exact path setに対応する唯一のmedia type契約。 */
389
- export function rc1V6ArtifactMediaType(relativePath) {
390
- if (!EXPECTED_PATHS.includes(relativePath)) {
391
- throw new TypeError(`RC1 v6 artifact pathがexact setに存在しない: ${relativePath}`);
392
- }
393
- if (relativePath.endsWith('.json')) return 'application/json';
394
- if (relativePath.endsWith('.mjs') || relativePath === IDENTITY_PATHS.sensorExecutable) {
395
- return 'application/javascript';
396
- }
397
- if (relativePath.endsWith('.patch')) return 'text/x-diff';
398
- return 'text/markdown';
399
- }
400
-
401
- function payloadMap(payloads) {
402
- if (!Array.isArray(payloads)) return null;
403
- const map = new Map();
404
- for (const payload of payloads) {
405
- if (!exactRecord(payload, ['path', 'bytes'])
406
- || typeof payload.path !== 'string'
407
- || !Buffer.isBuffer(payload.bytes)
408
- || map.has(payload.path)) {
409
- return null;
410
- }
411
- map.set(payload.path, payload.bytes);
412
- }
413
- return map;
414
- }
415
-
416
- function artifactContext(options) {
417
- if (!exactRecord(options, ['manifest', 'payloads'])) return null;
418
- const payloads = payloadMap(options.payloads);
419
- if (!payloads
420
- || !exactRecord(options.manifest, ['schema', 'base_sha', 'result_digest', 'files'])
421
- || options.manifest.schema !== 'lattice.rc1.artifact_manifest.v6'
422
- || !GIT_SHA1.test(options.manifest.base_sha)
423
- || !SHA256.test(options.manifest.result_digest)
424
- || !Array.isArray(options.manifest.files)) {
425
- return null;
426
- }
427
- const manifestPaths = options.manifest.files.map(({ path }) => path).sort();
428
- if (!sameArtifact(manifestPaths, EXPECTED_PATHS)
429
- || !sameArtifact([...payloads.keys()].sort(), EXPECTED_PATHS)) {
430
- return null;
431
- }
432
- for (const entry of options.manifest.files) {
433
- const bytes = payloads.get(entry.path);
434
- if (!exactRecord(entry, ['path', 'media_type', 'bytes', 'sha256'])
435
- || !Buffer.isBuffer(bytes)
436
- || entry.media_type !== rc1V6ArtifactMediaType(entry.path)
437
- || entry.bytes !== bytes.byteLength
438
- || entry.sha256 !== sha256(bytes)) {
439
- return null;
440
- }
441
- }
442
- const json = new Map();
443
- for (const relativePath of EXPECTED_PATHS.filter((entry) => entry.endsWith('.json'))) {
444
- const value = parseJson(payloads.get(relativePath));
445
- if (value === null) return null;
446
- json.set(relativePath, value);
447
- }
448
- const patch = payloads.get('transform/seam.patch');
449
- if (!Buffer.isBuffer(patch) || patch.byteLength === 0) return null;
450
- return { manifest: options.manifest, payloads, json, patch };
451
- }
452
-
453
- function fixedInputs(context) {
454
- return Object.fromEntries(Object.entries(INPUT_PATHS).map(([key, relativePath]) => (
455
- [key, context.json.get(relativePath)]
456
- )));
457
- }
458
-
459
- function compiledFromContext(context, condition, variant) {
460
- const root = `compiled/${condition}-${variant}`;
461
- return {
462
- boundary_manifest: context.json.get(`${root}/boundary-manifest.json`),
463
- boundary_verdict: context.json.get(`${root}/boundary-verdict.json`),
464
- plan_graph: context.json.get(`${root}/plan.json`),
465
- boundary_manifest_digest: digestArtifact(context.json.get(`${root}/boundary-manifest.json`)),
466
- boundary_verdict_digest: digestArtifact(context.json.get(`${root}/boundary-verdict.json`)),
467
- plan_graph_digest: digestArtifact(context.json.get(`${root}/plan.json`)),
468
- };
469
- }
470
-
471
- function savedCompilationMatchesReplay(saved, replayed) {
472
- return sameArtifact(saved.boundary_manifest, replayed.boundary_manifest)
473
- && sameArtifact(saved.boundary_verdict, replayed.boundary_verdict)
474
- && sameArtifact(saved.plan_graph, replayed.plan_graph)
475
- && saved.boundary_manifest_digest === replayed.boundary_manifest_digest
476
- && saved.boundary_verdict_digest === replayed.boundary_verdict_digest
477
- && saved.plan_graph_digest === replayed.plan_graph_digest;
478
- }
479
-
480
- function compilerReplay(context, inputs, bundles, runs, expectations) {
481
- const conditions = {};
482
- for (const condition of ['control', 'treatment']) {
483
- const conditionBundles = bundles.filter((bundle) => bundle.condition === condition);
484
- const conditionRuns = runs.filter((run) => run.condition === condition);
485
- if (conditionBundles.length !== 2 || conditionRuns.length !== 2) return null;
486
- const outputs = [];
487
- for (let index = 0; index < 2; index += 1) {
488
- const common = {
489
- planInput: inputs.planInput,
490
- candidateSpec: inputs.candidateSpec,
491
- querySet: inputs.querySet,
492
- run: conditionRuns[index],
493
- bundle: conditionBundles[index],
494
- expected: expectations[condition],
495
- };
496
- outputs.push({
497
- normal: compileRc1V6BoundaryCondition({
498
- ...common,
499
- manualEvidence: inputs.normalManualEvidence,
500
- planVersion: `rc1-v6-${condition}`,
501
- }),
502
- negative: compileRc1V6BoundaryCondition({
503
- ...common,
504
- manualEvidence: inputs.negativeManualEvidence,
505
- planVersion: `rc1-v6-${condition}-negative`,
506
- }),
507
- });
508
- }
509
- if (!sameArtifact(outputs[0], outputs[1])) return null;
510
- conditions[condition] = outputs[0];
511
- }
512
- return conditions;
513
- }
514
-
515
- function transformValid(context, control, treatment, behaviorEvidence, runtimeIdentity) {
516
- const artifact = context.json.get('transform/transform-artifact.json');
517
- const receipt = context.json.get('transform/transform-receipt.json');
518
- if (!validateTransformArtifact(artifact)
519
- || artifact.status !== 'accepted'
520
- || sha256(context.patch) !== artifact.patch.digest
521
- || digestArtifact(artifact) !== receipt?.transform_artifact_digest
522
- || !exactRecord(receipt, [
523
- 'schema',
524
- 'status',
525
- 'transform_artifact_digest',
526
- 'behavior_envelope_digest',
527
- 'fixed_inputs_digest',
528
- 'runtime_identity_digest',
529
- 'source_invariant',
530
- 'receipt_digest',
531
- ])
532
- || receipt.schema !== 'lattice.rc1.seam_transform_receipt.v6'
533
- || receipt.status !== 'accepted'
534
- || receipt.behavior_envelope_digest !== behaviorEvidence.envelope.envelope_digest
535
- || receipt.fixed_inputs_digest !== digestArtifact(artifact.source)
536
- || receipt.runtime_identity_digest !== digestArtifact(runtimeIdentity)
537
- || receipt.source_invariant?.outcome !== 'passed') {
538
- return null;
539
- }
540
- const { receipt_digest: ignored, ...preimage } = receipt;
541
- if (receipt.receipt_digest !== digestArtifact(preimage)
542
- || artifact.source.boundary_manifest_digest !== control.normal.boundary_manifest_digest
543
- || artifact.source.boundary_verdict_digest !== control.normal.boundary_verdict_digest
544
- || artifact.source.control_plan_digest !== control.normal.plan_graph_digest
545
- || artifact.source.code_snapshot_digest
546
- !== control.normal.boundary_manifest.source.code_snapshot_digest
547
- || !sameArtifact(
548
- sourceSnapshotFromRc1TransformOutput(artifact),
549
- sourceSnapshotFromRc1BehaviorSurface(behaviorEvidence.post_receipt.surface),
550
- )
551
- || treatment.normal.boundary_manifest.source.code_snapshot_digest
552
- !== digestArtifact(sourceSnapshotFromRc1TransformOutput(artifact))) {
553
- return null;
554
- }
555
- return {
556
- artifact,
557
- artifact_digest: digestArtifact(artifact),
558
- receipt,
559
- receipt_digest: receipt.receipt_digest,
560
- patch: context.patch,
561
- behavior_evidence: behaviorEvidence,
562
- };
563
- }
564
-
565
- function executionValid(value, expected) {
566
- return exactRecord(value, [
567
- 'schema',
568
- 'base_sha',
569
- 'elapsed_ms',
570
- 'compiler_source_digest',
571
- 'sensor_identity_digest',
572
- 'runtime_identity_digest',
573
- 'run_descriptor_digests',
574
- 'transform_artifact_digest',
575
- 'behavior_envelope_digest',
576
- 'plan_diff_digest',
577
- 'comparison_digest',
578
- 'hypothesis_evaluation_digest',
579
- 'source_invariant',
580
- ])
581
- && value.schema === 'lattice.rc1.corrected_campaign_execution_evidence.v6'
582
- && value.base_sha === expected.baseSha
583
- && Number.isFinite(value.elapsed_ms)
584
- && value.elapsed_ms >= 0
585
- && value.compiler_source_digest === expected.compilerSourceDigest
586
- && value.sensor_identity_digest === digestArtifact(expected.sensorIdentity)
587
- && value.runtime_identity_digest === digestArtifact(expected.runtimeIdentity)
588
- && sameArtifact(value.run_descriptor_digests, expected.runDescriptorDigests)
589
- && value.transform_artifact_digest === expected.transformArtifactDigest
590
- && value.behavior_envelope_digest === expected.behaviorEnvelopeDigest
591
- && value.plan_diff_digest === expected.planDiffDigest
592
- && value.comparison_digest === expected.comparisonDigest
593
- && value.hypothesis_evaluation_digest === expected.hypothesisEvaluationDigest
594
- && sameArtifact(value.source_invariant, expected.sourceInvariant);
595
- }
596
-
597
- /** v6 manifestと保存payloadだけから全causal relationを再計算する。 */
598
- export function verifyRc1V6CampaignArtifactSet(options) {
599
- const checks = [];
600
- const check = (id, passed) => checks.push({ id, passed: passed === true });
601
- let context;
602
- try {
603
- context = artifactContext(options);
604
- } catch {
605
- context = null;
606
- }
607
- check('exact_artifact_set', context !== null);
608
- if (context === null) {
609
- return {
610
- schema: 'lattice.rc1.artifact_set_verification.v6',
611
- valid: false,
612
- checks,
613
- failed_conditions: ['exact_artifact_set'],
614
- };
615
- }
616
-
617
- try {
618
- const inputs = fixedInputs(context);
619
- const compilerSourceDigest = sha256(context.payloads.get(IDENTITY_PATHS.compiler));
620
- const identitySourceBinding = sha256(context.payloads.get(IDENTITY_PATHS.oracleExecutor))
621
- === inputs.runtimeIdentity?.executor_source_digest
622
- && sha256(context.payloads.get(IDENTITY_PATHS.sensorExecutable))
623
- === inputs.sensorIdentity?.executable_digest;
624
- check('identity_source_binding', identitySourceBinding);
625
- const baseInputsValid = validatePlanInput(inputs.planInput)
626
- && validateRc1BlackBoxOracle(inputs.oracle)
627
- && digestArtifact(inputs.querySet) === context.json.get('evidence/control-1.json').query_set_digest;
628
- check('input_identity', baseInputsValid);
629
-
630
- const behaviorEvidence = {
631
- pre_receipt: context.json.get('behavior/pre-receipt.json'),
632
- post_receipt: context.json.get('behavior/post-receipt.json'),
633
- envelope: context.json.get('behavior/evidence-envelope.json'),
634
- transform_artifact: context.json.get('transform/transform-artifact.json'),
635
- patch_digest: sha256(context.patch),
636
- };
637
- const behaviorVerification = verifyRc1V6BehaviorEvidence({
638
- oracle: inputs.oracle,
639
- runtimeIdentity: inputs.runtimeIdentity,
640
- preReceipt: behaviorEvidence.pre_receipt,
641
- postReceipt: behaviorEvidence.post_receipt,
642
- transformArtifact: behaviorEvidence.transform_artifact,
643
- patchDigest: behaviorEvidence.patch_digest,
644
- envelope: behaviorEvidence.envelope,
645
- });
646
- check('behavior_artifact_set', behaviorVerification.valid);
647
-
648
- const controlSnapshot = sourceSnapshotFromRc1BehaviorSurface(
649
- behaviorEvidence.pre_receipt.surface,
650
- );
651
- const treatmentSnapshot = sourceSnapshotFromRc1TransformOutput(
652
- behaviorEvidence.transform_artifact,
653
- );
654
- const expectations = {
655
- control: {
656
- base_sha: context.manifest.base_sha,
657
- patch_digest: null,
658
- snapshot: controlSnapshot,
659
- sensor_identity: inputs.sensorIdentity,
660
- query_set_digest: digestArtifact(inputs.querySet),
661
- },
662
- treatment: {
663
- base_sha: context.manifest.base_sha,
664
- patch_digest: behaviorEvidence.patch_digest,
665
- snapshot: treatmentSnapshot,
666
- sensor_identity: inputs.sensorIdentity,
667
- query_set_digest: digestArtifact(inputs.querySet),
668
- },
669
- };
670
- const bundles = RUN_IDS.map((runId) => context.json.get(`evidence/${runId}.json`));
671
- const runs = RUN_IDS.map((runId) => context.json.get(`runs/${runId}.json`));
672
- const runEvidenceValid = bundles.every((bundle, index) => (
673
- verifyRc1V6RunEvidence({
674
- run: runs[index],
675
- bundle,
676
- expected: expectations[bundle.condition],
677
- }).valid
678
- ))
679
- && bundles.filter(({ condition }) => condition === 'control').length === 2
680
- && bundles.filter(({ condition }) => condition === 'treatment').length === 2
681
- && bundles[0].portable.aggregate_digest === bundles[1].portable.aggregate_digest
682
- && bundles[2].portable.aggregate_digest === bundles[3].portable.aggregate_digest;
683
- check('evidence_campaign', runEvidenceValid);
684
-
685
- const replay = runEvidenceValid
686
- ? compilerReplay(context, inputs, bundles, runs, expectations)
687
- : null;
688
- const savedControl = {
689
- normal: compiledFromContext(context, 'control', 'normal'),
690
- negative: compiledFromContext(context, 'control', 'negative'),
691
- };
692
- const savedTreatment = {
693
- normal: compiledFromContext(context, 'treatment', 'normal'),
694
- negative: compiledFromContext(context, 'treatment', 'negative'),
695
- };
696
- const savedCompiledValid = [savedControl.normal, savedControl.negative,
697
- savedTreatment.normal, savedTreatment.negative].every((compiled) => (
698
- validateBoundaryManifest(compiled.boundary_manifest)
699
- && validateBoundaryVerdict(compiled.boundary_verdict)
700
- && validatePlanGraph(compiled.plan_graph)
701
- ));
702
- check('compiler_replay', replay !== null && savedCompiledValid
703
- && savedCompilationMatchesReplay(savedControl.normal, replay.control.normal)
704
- && savedCompilationMatchesReplay(savedControl.negative, replay.control.negative)
705
- && savedCompilationMatchesReplay(savedTreatment.normal, replay.treatment.normal)
706
- && savedCompilationMatchesReplay(savedTreatment.negative, replay.treatment.negative));
707
-
708
- const transform = replay === null ? null : transformValid(
709
- context,
710
- savedControl,
711
- savedTreatment,
712
- behaviorEvidence,
713
- inputs.runtimeIdentity,
714
- );
715
- check('transform_binding', transform !== null);
716
-
717
- const rejectedPlanBytes = context.payloads.get(`predecessor/${REJECTED_PLAN_REF}`);
718
- const phaseDecisionBytes = context.payloads.get(`predecessor/${PHASE_DECISION_REF}`);
719
- const expectedPlanDiff = transform === null ? null : buildRc1V6PlanDiff({
720
- control: savedControl,
721
- treatment: savedTreatment,
722
- transform,
723
- evidenceBundles: bundles,
724
- rejectedPlanBytes,
725
- phaseDecisionBytes,
726
- });
727
- const savedPlanDiff = context.json.get('plan-diff.json');
728
- const predecessorVerification = expectedPlanDiff === null
729
- ? { valid: false }
730
- : verifyRc1V6PlanPredecessors({
731
- planDiff: savedPlanDiff,
732
- expectedPredecessors: expectedPlanDiff.causal_predecessors,
733
- });
734
- check('plan_diff_binding', expectedPlanDiff !== null
735
- && sameArtifact(savedPlanDiff, expectedPlanDiff)
736
- && predecessorVerification.valid);
737
-
738
- const sourceInvariant = context.json.get('execution-evidence.json')?.source_invariant;
739
- const expectedComparison = transform === null ? null : compileRc1V6Comparison({
740
- baseSha: context.manifest.base_sha,
741
- inputs,
742
- runtimeIdentity: inputs.runtimeIdentity,
743
- sensorIdentity: inputs.sensorIdentity,
744
- compilerSourceDigest,
745
- control: savedControl,
746
- treatment: savedTreatment,
747
- evidenceBundles: bundles,
748
- transform,
749
- planDiff: savedPlanDiff,
750
- sourceInvariant,
751
- });
752
- const savedComparison = context.json.get('comparison.json');
753
- check('comparison_binding', expectedComparison !== null
754
- && sameArtifact(savedComparison, expectedComparison));
755
-
756
- const expectedEvaluation = expectedComparison === null
757
- ? null
758
- : evaluateRc1V6Hypothesis(expectedComparison);
759
- const savedEvaluation = context.json.get('hypothesis-evaluation.json');
760
- check('hypothesis_evaluation', expectedEvaluation !== null
761
- && sameArtifact(savedEvaluation, expectedEvaluation)
762
- && savedEvaluation.supported === true);
763
-
764
- const execution = context.json.get('execution-evidence.json');
765
- check('execution_evidence', transform !== null && executionValid(execution, {
766
- baseSha: context.manifest.base_sha,
767
- compilerSourceDigest,
768
- sensorIdentity: inputs.sensorIdentity,
769
- runtimeIdentity: inputs.runtimeIdentity,
770
- runDescriptorDigests: runs.map(({ evidence_bundle_descriptor_digest: digest }) => digest),
771
- transformArtifactDigest: transform.artifact_digest,
772
- behaviorEnvelopeDigest: behaviorEvidence.envelope.envelope_digest,
773
- planDiffDigest: digestArtifact(savedPlanDiff),
774
- comparisonDigest: digestArtifact(savedComparison),
775
- hypothesisEvaluationDigest: digestArtifact(savedEvaluation),
776
- sourceInvariant,
777
- }));
778
- check('result_binding', context.manifest.result_digest === digestArtifact(savedEvaluation));
779
- } catch {
780
- for (const id of [
781
- 'input_identity',
782
- 'identity_source_binding',
783
- 'behavior_artifact_set',
784
- 'evidence_campaign',
785
- 'compiler_replay',
786
- 'transform_binding',
787
- 'plan_diff_binding',
788
- 'comparison_binding',
789
- 'hypothesis_evaluation',
790
- 'execution_evidence',
791
- 'result_binding',
792
- ]) {
793
- if (!checks.some((entry) => entry.id === id)) check(id, false);
794
- }
795
- }
796
- const failedConditions = checks.filter(({ passed }) => !passed).map(({ id }) => id);
797
- return {
798
- schema: 'lattice.rc1.artifact_set_verification.v6',
799
- valid: failedConditions.length === 0,
800
- checks,
801
- failed_conditions: failedConditions,
802
- };
803
- }
804
-
805
- export const RC1_V6_ARTIFACT_PATHS = EXPECTED_PATHS;
806
- export const RC1_V6_REJECTED_PLAN_REF = REJECTED_PLAN_REF;
807
- export const RC1_V6_PHASE_DECISION_REF = PHASE_DECISION_REF;