@quolu/lattice 0.52.3 → 0.53.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 (188) hide show
  1. package/LICENSE +147 -147
  2. package/README.ja.md +355 -355
  3. package/README.md +258 -258
  4. package/bin/lattice-bridge.mjs +25 -0
  5. package/bin/lattice-hub.mjs +67 -0
  6. package/bin/lattice-mcp.mjs +0 -0
  7. package/bin/lattice-scripted-adapter.mjs +0 -0
  8. package/bin/lattice-scripted-worker.mjs +0 -0
  9. package/bin/lattice-work-order-adapter.mjs +0 -0
  10. package/bin/lattice.mjs +0 -0
  11. package/docs/bridge-setup.md +132 -132
  12. package/docs/schemas/lattice.executor_packet.v1.schema.json +57 -57
  13. package/docs/schemas/lattice.executor_receipt.v1.schema.json +66 -66
  14. package/docs/schemas/lattice.phase_todo_revision.v3.schema.json +360 -360
  15. package/docs/schemas/lattice.plan_create_input.v1.schema.json +56 -56
  16. package/docs/schemas/lattice.plan_create_input.v2.schema.json +72 -72
  17. package/docs/schemas/lattice.plan_create_input.v3.schema.json +81 -81
  18. package/docs/schemas/lattice.plan_create_input.v4.schema.json +85 -85
  19. package/docs/schemas/lattice.run_request.v1.schema.json +238 -238
  20. package/docs/schemas/lattice.runtime_adapter_capabilities.v2.schema.json +55 -55
  21. package/docs/schemas/lattice.runtime_adapter_registration_input.v1.schema.json +78 -78
  22. package/docs/schemas/lattice.runtime_adapter_registration_input.v2.schema.json +86 -86
  23. package/docs/schemas/lattice.todo_extraction.v2.schema.json +298 -298
  24. package/docs/schemas/lattice.todo_extraction.v3.schema.json +146 -146
  25. package/docs/schemas/lattice.todo_revision.v2.schema.json +260 -260
  26. package/docs/schemas/lattice.todo_revision_set.v3.schema.json +363 -363
  27. package/package.json +103 -103
  28. package/sensor/LICENSE +21 -21
  29. package/sensor/NOTICE +19 -19
  30. package/sensor/dist/bin/lattice-sensor.js +9 -9
  31. package/sensor/dist/db/index.js +24 -24
  32. package/sensor/dist/db/migrations.js +41 -41
  33. package/sensor/dist/db/queries.js +164 -164
  34. package/sensor/dist/db/schema.sql +205 -205
  35. package/sensor/dist/directory.js +5 -5
  36. package/sensor/dist/extraction/wasm/tree-sitter-c_sharp.wasm +0 -0
  37. package/sensor/dist/extraction/wasm/tree-sitter-cfml.wasm +0 -0
  38. package/sensor/dist/extraction/wasm/tree-sitter-cfquery.wasm +0 -0
  39. package/sensor/dist/extraction/wasm/tree-sitter-cfscript.wasm +0 -0
  40. package/sensor/dist/extraction/wasm/tree-sitter-cobol.wasm +0 -0
  41. package/sensor/dist/extraction/wasm/tree-sitter-erlang.wasm +0 -0
  42. package/sensor/dist/extraction/wasm/tree-sitter-go.wasm +0 -0
  43. package/sensor/dist/extraction/wasm/tree-sitter-java.wasm +0 -0
  44. package/sensor/dist/extraction/wasm/tree-sitter-javascript.wasm +0 -0
  45. package/sensor/dist/extraction/wasm/tree-sitter-nix.wasm +0 -0
  46. package/sensor/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
  47. package/sensor/dist/extraction/wasm/tree-sitter-python.wasm +0 -0
  48. package/sensor/dist/extraction/wasm/tree-sitter-tsx.wasm +0 -0
  49. package/sensor/dist/extraction/wasm/tree-sitter-typescript.wasm +0 -0
  50. package/sensor/dist/extraction/wasm/tree-sitter-vbnet.wasm +0 -0
  51. package/sensor/dist/mcp/liveness-watchdog.js +53 -53
  52. package/sensor/dist/mcp/server-instructions.js +95 -95
  53. package/sensor/package.json +56 -56
  54. package/src/artifact-contracts-v2.mjs +325 -325
  55. package/src/artifact-contracts.mjs +895 -895
  56. package/src/boundary-compiler.mjs +712 -712
  57. package/src/boundary-observation-compiler-v2.mjs +344 -344
  58. package/src/bounded-seam.mjs +230 -230
  59. package/src/bridge-address.mjs +107 -107
  60. package/src/bridge-cli.mjs +303 -297
  61. package/src/bridge-config.mjs +382 -346
  62. package/src/bridge-daemon.mjs +378 -378
  63. package/src/bridge-hub-heartbeat.mjs +170 -0
  64. package/src/bridge-hub-protocol.mjs +198 -0
  65. package/src/bridge-hub-server.mjs +544 -0
  66. package/src/bridge-launch-agent.mjs +323 -323
  67. package/src/bridge-registrar.mjs +102 -102
  68. package/src/bridge-server.mjs +376 -376
  69. package/src/cli-help.mjs +308 -308
  70. package/src/cli-stdio.mjs +40 -40
  71. package/src/control-compiler.mjs +532 -532
  72. package/src/dag-chain.mjs +261 -261
  73. package/src/factory-diagnostics.mjs +188 -188
  74. package/src/git-process.mjs +72 -0
  75. package/src/hash-chain.mjs +76 -76
  76. package/src/hooks-cli.mjs +1057 -1057
  77. package/src/isolation-runner.mjs +409 -409
  78. package/src/node-version-guard.mjs +44 -44
  79. package/src/project-cli.mjs +3 -3
  80. package/src/project-identity.mjs +130 -130
  81. package/src/rc1-black-box-oracle.mjs +906 -906
  82. package/src/rc1-comparison.mjs +154 -154
  83. package/src/rc1-evidence-bundle.mjs +456 -456
  84. package/src/rc1-v4-campaign.mjs +708 -708
  85. package/src/rc1-v4-transform.mjs +467 -467
  86. package/src/rc1-v5-artifact-set.mjs +855 -855
  87. package/src/rc1-v5-behavior-evidence.mjs +594 -594
  88. package/src/rc1-v5-campaign.mjs +797 -797
  89. package/src/rc1-v5-transform.mjs +421 -421
  90. package/src/rc1-v6-artifact-set.mjs +807 -807
  91. package/src/rc1-v6-behavior-evidence.mjs +273 -273
  92. package/src/rc1-v6-campaign.mjs +623 -623
  93. package/src/rc1-v6-causal-binding.mjs +473 -473
  94. package/src/rc1-v6-measurement.mjs +313 -313
  95. package/src/rc2-artifact-set.mjs +1584 -1584
  96. package/src/rc2-campaign.mjs +1506 -1506
  97. package/src/rc2-delivery-policy-front-end.mjs +1079 -1079
  98. package/src/rc2-delivery-policy-oracle.mjs +134 -134
  99. package/src/rc2-delivery-policy-transform.mjs +1127 -1127
  100. package/src/rc2-rc1-transfer-front-end.mjs +511 -511
  101. package/src/rc3-actual-dogfood.mjs +652 -652
  102. package/src/rc3-dogfood-scaffold.mjs +315 -315
  103. package/src/rc3-scripted-campaign.mjs +1383 -1383
  104. package/src/rc4-stage1-dogfood.mjs +673 -673
  105. package/src/runtime-adapter-registry.mjs +524 -524
  106. package/src/runtime-cli.mjs +4588 -4588
  107. package/src/runtime-contracts.mjs +824 -824
  108. package/src/runtime-control-store.mjs +604 -604
  109. package/src/runtime-controller-protocol.mjs +587 -587
  110. package/src/runtime-decision-verifier.mjs +701 -701
  111. package/src/runtime-diff-observer.mjs +361 -361
  112. package/src/runtime-direct-os-observer.mjs +301 -301
  113. package/src/runtime-driver-state.mjs +166 -166
  114. package/src/runtime-engine.mjs +779 -779
  115. package/src/runtime-errors.mjs +356 -356
  116. package/src/runtime-event-store.mjs +189 -189
  117. package/src/runtime-front-end.mjs +925 -925
  118. package/src/runtime-gate-store.mjs +481 -481
  119. package/src/runtime-hold-recompile.mjs +916 -916
  120. package/src/runtime-io-sentinel.mjs +391 -391
  121. package/src/runtime-lifecycle-lock.mjs +294 -294
  122. package/src/runtime-managed-supervisor.mjs +1490 -1490
  123. package/src/runtime-multi-epoch-store.mjs +838 -838
  124. package/src/runtime-projection.mjs +269 -269
  125. package/src/runtime-pull-intake.mjs +1192 -1192
  126. package/src/runtime-scripted-adapter-controller.mjs +1160 -1160
  127. package/src/runtime-scripted-executor.mjs +163 -163
  128. package/src/runtime-scripted-worktree.mjs +104 -104
  129. package/src/runtime-seam-resolve.mjs +428 -428
  130. package/src/runtime-seam-treatment.mjs +173 -173
  131. package/src/runtime-socket-owner.mjs +125 -125
  132. package/src/runtime-work-order-contracts.mjs +91 -91
  133. package/src/runtime-work-order-controller.mjs +1171 -1171
  134. package/src/runtime-worktree-executor.mjs +199 -199
  135. package/src/schedulability-compiler-v2.mjs +303 -303
  136. package/src/schedulability-verifier-v2.mjs +317 -317
  137. package/src/seam-apply.mjs +549 -549
  138. package/src/seam-commit-shared.mjs +22 -22
  139. package/src/seam-commit-transform.mjs +81 -81
  140. package/src/seam-commit.mjs +18 -18
  141. package/src/seam-cost.mjs +322 -322
  142. package/src/seam-derivation.mjs +188 -188
  143. package/src/seam-gate.mjs +146 -146
  144. package/src/seam-proposal-contracts.mjs +446 -446
  145. package/src/seam-proposal-queries.mjs +521 -521
  146. package/src/seam-proposal.mjs +2011 -2011
  147. package/src/seam-ref.mjs +33 -33
  148. package/src/seam-rewrite.mjs +286 -286
  149. package/src/seam-transform.mjs +554 -554
  150. package/src/seam-verification.mjs +260 -260
  151. package/src/sensor-adapter.mjs +432 -432
  152. package/src/sensor-cli.mjs +139 -139
  153. package/src/sensor-diff.mjs +661 -661
  154. package/src/sensor-node-runtime.mjs +53 -53
  155. package/src/sensor-runtime.mjs +52 -52
  156. package/src/timestamp-contract.mjs +8 -8
  157. package/src/todo-audit-pending.mjs +91 -91
  158. package/src/todo-chain.mjs +178 -178
  159. package/src/todo-cli.mjs +8 -8
  160. package/src/todo-contracts.mjs +728 -728
  161. package/src/todo-dashboard-registry.mjs +573 -573
  162. package/src/todo-dispatch-shape.mjs +190 -190
  163. package/src/todo-gantt-html-independence.mjs +239 -239
  164. package/src/todo-gantt-html-shared.mjs +226 -226
  165. package/src/todo-gantt-html-style.mjs +131 -131
  166. package/src/todo-gantt-html.mjs +248 -248
  167. package/src/todo-gantt-layout.mjs +974 -974
  168. package/src/todo-gantt-live.mjs +361 -361
  169. package/src/todo-gantt-nested.mjs +263 -263
  170. package/src/todo-gantt-presentation.mjs +217 -217
  171. package/src/todo-gantt-scope.mjs +123 -123
  172. package/src/todo-gantt-svg.mjs +353 -353
  173. package/src/todo-independence-contracts.mjs +595 -595
  174. package/src/todo-independence-guidance.mjs +322 -322
  175. package/src/todo-independence.mjs +640 -640
  176. package/src/todo-markdown-renderer.mjs +260 -260
  177. package/src/todo-migration.mjs +448 -448
  178. package/src/todo-narrative-anchor.mjs +130 -130
  179. package/src/todo-note-store.mjs +629 -629
  180. package/src/todo-parallel-candidates.mjs +114 -114
  181. package/src/todo-revision.mjs +995 -995
  182. package/src/todo-split.mjs +472 -472
  183. package/src/todo-status.mjs +690 -690
  184. package/src/todo-store-git-transaction.mjs +418 -418
  185. package/src/todo-store.mjs +42 -21
  186. package/src/treatment-compiler.mjs +728 -728
  187. package/src/treatment-runner.mjs +656 -656
  188. package/src/witness-scaffold.mjs +180 -180
@@ -1,807 +1,807 @@
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;
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;