@quolu/lattice 0.52.0 → 0.52.2

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 (182) hide show
  1. package/LICENSE +147 -147
  2. package/README.ja.md +355 -355
  3. package/README.md +258 -258
  4. package/bin/lattice-mcp.mjs +0 -0
  5. package/bin/lattice-scripted-adapter.mjs +0 -0
  6. package/bin/lattice-scripted-worker.mjs +0 -0
  7. package/bin/lattice-work-order-adapter.mjs +0 -0
  8. package/bin/lattice.mjs +0 -0
  9. package/docs/bridge-setup.md +132 -132
  10. package/docs/schemas/lattice.executor_packet.v1.schema.json +57 -57
  11. package/docs/schemas/lattice.executor_receipt.v1.schema.json +66 -66
  12. package/docs/schemas/lattice.phase_todo_revision.v3.schema.json +360 -360
  13. package/docs/schemas/lattice.plan_create_input.v1.schema.json +56 -56
  14. package/docs/schemas/lattice.plan_create_input.v2.schema.json +72 -72
  15. package/docs/schemas/lattice.plan_create_input.v3.schema.json +81 -81
  16. package/docs/schemas/lattice.plan_create_input.v4.schema.json +85 -85
  17. package/docs/schemas/lattice.run_request.v1.schema.json +238 -238
  18. package/docs/schemas/lattice.runtime_adapter_capabilities.v2.schema.json +55 -55
  19. package/docs/schemas/lattice.runtime_adapter_registration_input.v1.schema.json +78 -78
  20. package/docs/schemas/lattice.runtime_adapter_registration_input.v2.schema.json +86 -86
  21. package/docs/schemas/lattice.todo_extraction.v2.schema.json +298 -298
  22. package/docs/schemas/lattice.todo_extraction.v3.schema.json +146 -146
  23. package/docs/schemas/lattice.todo_revision.v2.schema.json +260 -260
  24. package/docs/schemas/lattice.todo_revision_set.v3.schema.json +363 -363
  25. package/package.json +103 -103
  26. package/sensor/LICENSE +21 -21
  27. package/sensor/NOTICE +19 -19
  28. package/sensor/dist/bin/lattice-sensor.js +9 -9
  29. package/sensor/dist/db/index.js +24 -24
  30. package/sensor/dist/db/migrations.js +41 -41
  31. package/sensor/dist/db/queries.js +164 -164
  32. package/sensor/dist/db/schema.sql +205 -205
  33. package/sensor/dist/directory.js +5 -5
  34. package/sensor/dist/extraction/wasm/tree-sitter-c_sharp.wasm +0 -0
  35. package/sensor/dist/extraction/wasm/tree-sitter-cfml.wasm +0 -0
  36. package/sensor/dist/extraction/wasm/tree-sitter-cfquery.wasm +0 -0
  37. package/sensor/dist/extraction/wasm/tree-sitter-cfscript.wasm +0 -0
  38. package/sensor/dist/extraction/wasm/tree-sitter-cobol.wasm +0 -0
  39. package/sensor/dist/extraction/wasm/tree-sitter-erlang.wasm +0 -0
  40. package/sensor/dist/extraction/wasm/tree-sitter-go.wasm +0 -0
  41. package/sensor/dist/extraction/wasm/tree-sitter-java.wasm +0 -0
  42. package/sensor/dist/extraction/wasm/tree-sitter-javascript.wasm +0 -0
  43. package/sensor/dist/extraction/wasm/tree-sitter-nix.wasm +0 -0
  44. package/sensor/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
  45. package/sensor/dist/extraction/wasm/tree-sitter-python.wasm +0 -0
  46. package/sensor/dist/extraction/wasm/tree-sitter-tsx.wasm +0 -0
  47. package/sensor/dist/extraction/wasm/tree-sitter-typescript.wasm +0 -0
  48. package/sensor/dist/extraction/wasm/tree-sitter-vbnet.wasm +0 -0
  49. package/sensor/dist/mcp/liveness-watchdog.js +53 -53
  50. package/sensor/dist/mcp/server-instructions.js +95 -95
  51. package/sensor/package.json +56 -56
  52. package/src/artifact-contracts-v2.mjs +325 -325
  53. package/src/artifact-contracts.mjs +895 -895
  54. package/src/boundary-compiler.mjs +712 -712
  55. package/src/boundary-observation-compiler-v2.mjs +344 -344
  56. package/src/bounded-seam.mjs +230 -230
  57. package/src/bridge-address.mjs +107 -107
  58. package/src/bridge-cli.mjs +297 -297
  59. package/src/bridge-config.mjs +346 -346
  60. package/src/bridge-daemon.mjs +378 -378
  61. package/src/bridge-launch-agent.mjs +323 -323
  62. package/src/bridge-registrar.mjs +102 -102
  63. package/src/bridge-server.mjs +376 -376
  64. package/src/cli-help.mjs +308 -308
  65. package/src/cli-stdio.mjs +40 -40
  66. package/src/control-compiler.mjs +532 -532
  67. package/src/dag-chain.mjs +261 -261
  68. package/src/factory-diagnostics.mjs +188 -188
  69. package/src/hash-chain.mjs +76 -76
  70. package/src/hooks-cli.mjs +1057 -1053
  71. package/src/isolation-runner.mjs +409 -409
  72. package/src/node-version-guard.mjs +44 -44
  73. package/src/project-cli.mjs +697 -697
  74. package/src/project-identity.mjs +130 -130
  75. package/src/rc1-black-box-oracle.mjs +906 -906
  76. package/src/rc1-comparison.mjs +154 -154
  77. package/src/rc1-evidence-bundle.mjs +456 -456
  78. package/src/rc1-v4-campaign.mjs +708 -708
  79. package/src/rc1-v4-transform.mjs +467 -467
  80. package/src/rc1-v5-artifact-set.mjs +855 -855
  81. package/src/rc1-v5-behavior-evidence.mjs +594 -594
  82. package/src/rc1-v5-campaign.mjs +797 -797
  83. package/src/rc1-v5-transform.mjs +421 -421
  84. package/src/rc1-v6-artifact-set.mjs +807 -807
  85. package/src/rc1-v6-behavior-evidence.mjs +273 -273
  86. package/src/rc1-v6-campaign.mjs +623 -623
  87. package/src/rc1-v6-causal-binding.mjs +473 -473
  88. package/src/rc1-v6-measurement.mjs +313 -313
  89. package/src/rc2-artifact-set.mjs +1584 -1584
  90. package/src/rc2-campaign.mjs +1506 -1502
  91. package/src/rc2-delivery-policy-front-end.mjs +1079 -1079
  92. package/src/rc2-delivery-policy-oracle.mjs +134 -134
  93. package/src/rc2-delivery-policy-transform.mjs +1127 -1127
  94. package/src/rc2-rc1-transfer-front-end.mjs +511 -511
  95. package/src/rc3-actual-dogfood.mjs +652 -652
  96. package/src/rc3-dogfood-scaffold.mjs +315 -315
  97. package/src/rc3-scripted-campaign.mjs +1383 -1383
  98. package/src/rc4-stage1-dogfood.mjs +673 -673
  99. package/src/runtime-adapter-registry.mjs +524 -520
  100. package/src/runtime-cli.mjs +4588 -4584
  101. package/src/runtime-contracts.mjs +824 -824
  102. package/src/runtime-control-store.mjs +604 -600
  103. package/src/runtime-controller-protocol.mjs +587 -587
  104. package/src/runtime-decision-verifier.mjs +701 -701
  105. package/src/runtime-diff-observer.mjs +361 -361
  106. package/src/runtime-direct-os-observer.mjs +301 -301
  107. package/src/runtime-driver-state.mjs +166 -162
  108. package/src/runtime-engine.mjs +779 -779
  109. package/src/runtime-errors.mjs +356 -356
  110. package/src/runtime-event-store.mjs +189 -189
  111. package/src/runtime-front-end.mjs +925 -925
  112. package/src/runtime-gate-store.mjs +481 -477
  113. package/src/runtime-hold-recompile.mjs +916 -916
  114. package/src/runtime-io-sentinel.mjs +391 -391
  115. package/src/runtime-lifecycle-lock.mjs +294 -290
  116. package/src/runtime-managed-supervisor.mjs +1490 -1490
  117. package/src/runtime-multi-epoch-store.mjs +838 -834
  118. package/src/runtime-projection.mjs +269 -269
  119. package/src/runtime-pull-intake.mjs +1192 -1188
  120. package/src/runtime-scripted-adapter-controller.mjs +1160 -1156
  121. package/src/runtime-scripted-executor.mjs +163 -163
  122. package/src/runtime-scripted-worktree.mjs +104 -104
  123. package/src/runtime-seam-resolve.mjs +428 -428
  124. package/src/runtime-seam-treatment.mjs +173 -173
  125. package/src/runtime-socket-owner.mjs +125 -125
  126. package/src/runtime-work-order-contracts.mjs +91 -91
  127. package/src/runtime-work-order-controller.mjs +1171 -1167
  128. package/src/runtime-worktree-executor.mjs +199 -199
  129. package/src/schedulability-compiler-v2.mjs +303 -303
  130. package/src/schedulability-verifier-v2.mjs +317 -317
  131. package/src/seam-apply.mjs +549 -549
  132. package/src/seam-commit-shared.mjs +22 -22
  133. package/src/seam-commit-transform.mjs +81 -81
  134. package/src/seam-commit.mjs +18 -18
  135. package/src/seam-cost.mjs +322 -322
  136. package/src/seam-derivation.mjs +188 -188
  137. package/src/seam-gate.mjs +146 -146
  138. package/src/seam-proposal-contracts.mjs +446 -446
  139. package/src/seam-proposal-queries.mjs +521 -521
  140. package/src/seam-proposal.mjs +2011 -2011
  141. package/src/seam-ref.mjs +33 -33
  142. package/src/seam-rewrite.mjs +286 -286
  143. package/src/seam-transform.mjs +554 -554
  144. package/src/seam-verification.mjs +260 -260
  145. package/src/sensor-adapter.mjs +432 -432
  146. package/src/sensor-cli.mjs +139 -139
  147. package/src/sensor-diff.mjs +661 -661
  148. package/src/sensor-node-runtime.mjs +53 -53
  149. package/src/sensor-runtime.mjs +52 -52
  150. package/src/timestamp-contract.mjs +8 -8
  151. package/src/todo-audit-pending.mjs +91 -91
  152. package/src/todo-chain.mjs +178 -178
  153. package/src/todo-cli.mjs +3141 -3126
  154. package/src/todo-contracts.mjs +728 -728
  155. package/src/todo-dashboard-registry.mjs +573 -573
  156. package/src/todo-dispatch-shape.mjs +190 -190
  157. package/src/todo-gantt-html-independence.mjs +239 -239
  158. package/src/todo-gantt-html-shared.mjs +226 -226
  159. package/src/todo-gantt-html-style.mjs +131 -131
  160. package/src/todo-gantt-html.mjs +248 -248
  161. package/src/todo-gantt-layout.mjs +974 -974
  162. package/src/todo-gantt-live.mjs +361 -361
  163. package/src/todo-gantt-nested.mjs +263 -263
  164. package/src/todo-gantt-presentation.mjs +217 -217
  165. package/src/todo-gantt-scope.mjs +123 -123
  166. package/src/todo-gantt-svg.mjs +353 -353
  167. package/src/todo-independence-contracts.mjs +595 -595
  168. package/src/todo-independence-guidance.mjs +322 -322
  169. package/src/todo-independence.mjs +640 -640
  170. package/src/todo-markdown-renderer.mjs +260 -260
  171. package/src/todo-migration.mjs +448 -448
  172. package/src/todo-narrative-anchor.mjs +130 -130
  173. package/src/todo-note-store.mjs +629 -625
  174. package/src/todo-parallel-candidates.mjs +114 -114
  175. package/src/todo-revision.mjs +995 -995
  176. package/src/todo-split.mjs +472 -472
  177. package/src/todo-status.mjs +690 -690
  178. package/src/todo-store-git-transaction.mjs +418 -418
  179. package/src/todo-store.mjs +4322 -4314
  180. package/src/treatment-compiler.mjs +728 -728
  181. package/src/treatment-runner.mjs +656 -656
  182. package/src/witness-scaffold.mjs +180 -180
@@ -1,456 +1,456 @@
1
- import { createHash } from 'node:crypto';
2
-
3
- import {
4
- canonicalizeArtifact,
5
- digestArtifact,
6
- } from './artifact-contracts.mjs';
7
- import { portableSensorOutcome } from './sensor-adapter.mjs';
8
-
9
- const IDENTIFIER = /^[0-9A-Za-z](?:[0-9A-Za-z._-]{0,127})$/;
10
- const SHA256 = /^[0-9a-f]{64}$/;
11
- const CONDITIONS = new Set(['control', 'treatment']);
12
- const OPERATIONS = new Set(['status', 'query', 'callers', 'callees', 'impact', 'affected']);
13
- const PROJECTION = 'lattice.sensor_portable_outcome.v1';
14
- const SANITIZER = 'lattice.sensor_diagnostic_sanitizer.v1';
15
- const STATUS_REMOVALS = Object.freeze([
16
- 'projectPath',
17
- 'indexPath',
18
- 'lastIndexed',
19
- 'dbSizeBytes',
20
- ]);
21
- const SANITIZATION_RULES = Object.freeze([
22
- { selector: '/cwd', action: 'replace', replacement: '<repo-root>' },
23
- ...STATUS_REMOVALS.map((field) => ({
24
- selector: `/outcomes/*[operation=status]/data/${field}`,
25
- action: 'remove',
26
- replacement: null,
27
- })),
28
- { selector: '/outcomes/**/node/updatedAt', action: 'remove', replacement: null },
29
- ]);
30
-
31
- function isPlainObject(value) {
32
- return value !== null
33
- && typeof value === 'object'
34
- && !Array.isArray(value)
35
- && Object.getPrototypeOf(value) === Object.prototype;
36
- }
37
-
38
- function exactRecord(value, keys) {
39
- if (!isPlainObject(value)) return false;
40
- const actual = Object.keys(value).sort();
41
- const expected = [...keys].sort();
42
- return actual.length === expected.length
43
- && actual.every((key, index) => key === expected[index]);
44
- }
45
-
46
- function identifier(value) {
47
- return typeof value === 'string' && IDENTIFIER.test(value);
48
- }
49
-
50
- function fail(reason) {
51
- throw new TypeError(`RC1 v4 evidence bundle契約違反: ${reason}`);
52
- }
53
-
54
- function sha256(value) {
55
- return createHash('sha256').update(value).digest('hex');
56
- }
57
-
58
- function escapePointer(value) {
59
- return String(value).replaceAll('~', '~0').replaceAll('/', '~1');
60
- }
61
-
62
- function sameArtifact(left, right) {
63
- try {
64
- return digestArtifact(left) === digestArtifact(right);
65
- } catch {
66
- return false;
67
- }
68
- }
69
-
70
- function validateQuerySet(querySet) {
71
- if (!exactRecord(querySet, ['schema', 'queries'])
72
- || querySet.schema !== 'lattice.sensor_query_set.v2'
73
- || !Array.isArray(querySet.queries)
74
- || querySet.queries.length === 0
75
- || querySet.queries.length > 256) {
76
- return false;
77
- }
78
- const ids = new Set();
79
- for (const query of querySet.queries) {
80
- if (!isPlainObject(query) || !identifier(query.id) || ids.has(query.id)
81
- || !OPERATIONS.has(query.operation)) {
82
- return false;
83
- }
84
- ids.add(query.id);
85
- if (query.operation === 'status') {
86
- if (!exactRecord(query, ['id', 'operation'])) return false;
87
- } else if (query.operation === 'affected') {
88
- if (!exactRecord(query, ['id', 'operation', 'targets'])
89
- || !Array.isArray(query.targets)
90
- || query.targets.length === 0
91
- || query.targets.some((target) => typeof target !== 'string' || target.length === 0)) {
92
- return false;
93
- }
94
- } else if (!exactRecord(query, ['id', 'operation', 'target'])
95
- || typeof query.target !== 'string'
96
- || query.target.length === 0) {
97
- return false;
98
- }
99
- }
100
- return true;
101
- }
102
-
103
- function validateRawEvidence(rawEvidence, querySet) {
104
- if (!exactRecord(rawEvidence, ['cwd', 'outcomes'])
105
- || typeof rawEvidence.cwd !== 'string'
106
- || rawEvidence.cwd.length === 0
107
- || !Array.isArray(rawEvidence.outcomes)
108
- || rawEvidence.outcomes.length !== querySet.queries.length) {
109
- return false;
110
- }
111
- for (let index = 0; index < querySet.queries.length; index += 1) {
112
- const query = querySet.queries[index];
113
- const outcome = rawEvidence.outcomes[index];
114
- if (!isPlainObject(outcome)
115
- || outcome.id !== query.id
116
- || outcome.operation !== query.operation
117
- || typeof outcome.outcome !== 'string'
118
- || outcome.outcome.length === 0) {
119
- return false;
120
- }
121
- if (query.operation !== 'status' && query.operation !== 'affected'
122
- && outcome.target !== query.target) {
123
- return false;
124
- }
125
- if (query.operation === 'affected'
126
- && (!Array.isArray(outcome.targets)
127
- || outcome.targets.length !== query.targets.length
128
- || outcome.targets.some((entry, targetIndex) => (
129
- !isPlainObject(entry) || entry.target !== query.targets[targetIndex]
130
- )))) {
131
- return false;
132
- }
133
- }
134
- canonicalizeArtifact(rawEvidence);
135
- return true;
136
- }
137
-
138
- function scrubNodeTelemetry(value, pointer, operations, parentKey = null) {
139
- if (Array.isArray(value)) {
140
- value.forEach((entry, index) => scrubNodeTelemetry(
141
- entry,
142
- `${pointer}/${index}`,
143
- operations,
144
- parentKey,
145
- ));
146
- return;
147
- }
148
- if (!isPlainObject(value)) return;
149
- if (parentKey === 'node' && Object.hasOwn(value, 'updatedAt')) {
150
- delete value.updatedAt;
151
- operations.push({ path: `${pointer}/updatedAt`, action: 'remove', replacement: null });
152
- }
153
- for (const [key, entry] of Object.entries(value)) {
154
- scrubNodeTelemetry(entry, `${pointer}/${escapePointer(key)}`, operations, key);
155
- }
156
- }
157
-
158
- function sanitizeDiagnostic(rawEvidence) {
159
- const payload = structuredClone(rawEvidence);
160
- const operations = [{ path: '/cwd', action: 'replace', replacement: '<repo-root>' }];
161
- payload.cwd = '<repo-root>';
162
- payload.outcomes.forEach((outcome, index) => {
163
- if (outcome.operation === 'status' && isPlainObject(outcome.data)) {
164
- for (const field of STATUS_REMOVALS) {
165
- if (Object.hasOwn(outcome.data, field)) {
166
- delete outcome.data[field];
167
- operations.push({
168
- path: `/outcomes/${index}/data/${escapePointer(field)}`,
169
- action: 'remove',
170
- replacement: null,
171
- });
172
- }
173
- }
174
- }
175
- scrubNodeTelemetry(outcome, `/outcomes/${index}`, operations);
176
- });
177
- return { payload, operations };
178
- }
179
-
180
- function stringHasAbsolutePath(value) {
181
- return /\bfile:\/\/\//i.test(value)
182
- || /(?:^|[\s("'=:\[])\/(?!\/)[^\s"')\]]+/.test(value)
183
- || /(?:^|[\s("'=:\[])[A-Za-z]:[\\/][^\s"')\]]*/.test(value);
184
- }
185
-
186
- function containsAbsolutePath(value) {
187
- if (typeof value === 'string') return stringHasAbsolutePath(value);
188
- if (Array.isArray(value)) return value.some(containsAbsolutePath);
189
- if (isPlainObject(value)) return Object.values(value).some(containsAbsolutePath);
190
- return false;
191
- }
192
-
193
- function rawReceipt(rawEvidence) {
194
- const canonical = canonicalizeArtifact(rawEvidence);
195
- const bytes = Buffer.from(canonical, 'utf8');
196
- return {
197
- schema: 'lattice.sensor_raw_opaque_receipt.v1',
198
- media_type: 'application/vnd.lattice.sensor-evidence+json',
199
- encoding: 'canonical-json-base64',
200
- payload_base64: bytes.toString('base64'),
201
- payload_digest: sha256(bytes),
202
- canonical_bytes: bytes.byteLength,
203
- };
204
- }
205
-
206
- function diagnosticArtifact(rawEvidence, sourceRawDigest) {
207
- const { payload, operations } = sanitizeDiagnostic(rawEvidence);
208
- if (containsAbsolutePath(payload)) fail('sanitized diagnosticに絶対pathが残る');
209
- const manifest = {
210
- schema: 'lattice.sensor_sanitization_manifest.v1',
211
- sanitizer: SANITIZER,
212
- rules_digest: digestArtifact(SANITIZATION_RULES),
213
- operations,
214
- };
215
- return {
216
- schema: 'lattice.sensor_sanitized_diagnostic.v1',
217
- source_raw_digest: sourceRawDigest,
218
- sanitization_manifest: manifest,
219
- sanitization_manifest_digest: digestArtifact(manifest),
220
- payload,
221
- payload_digest: digestArtifact(payload),
222
- };
223
- }
224
-
225
- function buildPortable(querySet, rawEvidence) {
226
- const querySetDigest = digestArtifact(querySet);
227
- const outcomes = rawEvidence.outcomes.map((outcome) => portableSensorOutcome(outcome));
228
- if (containsAbsolutePath(outcomes)) fail('portable outcomeに絶対pathが残る');
229
- const perQuery = outcomes.map((outcome, index) => ({
230
- id: querySet.queries[index].id,
231
- operation: querySet.queries[index].operation,
232
- outcome: outcome.outcome,
233
- result_digest: digestArtifact(outcome),
234
- }));
235
- const portable = {
236
- schema: 'lattice.sensor_portable_preimage.v1',
237
- projection: PROJECTION,
238
- query_set_digest: querySetDigest,
239
- outcomes,
240
- per_query: perQuery,
241
- aggregate_digest: '',
242
- };
243
- portable.aggregate_digest = recomputePortableAggregate(portable);
244
- return portable;
245
- }
246
-
247
- /** full portable payloadだけからaggregate digestを再計算する。 */
248
- export function recomputePortableAggregate(portable) {
249
- if (!isPlainObject(portable)
250
- || portable.schema !== 'lattice.sensor_portable_preimage.v1'
251
- || portable.projection !== PROJECTION
252
- || typeof portable.query_set_digest !== 'string'
253
- || !SHA256.test(portable.query_set_digest)
254
- || !Array.isArray(portable.outcomes)) {
255
- fail('portable preimageが再計算可能なshapeでない');
256
- }
257
- return digestArtifact({
258
- projection: portable.projection,
259
- query_set_digest: portable.query_set_digest,
260
- outcomes: portable.outcomes,
261
- });
262
- }
263
-
264
- function decodeRawReceipt(receipt) {
265
- if (!exactRecord(receipt, [
266
- 'schema',
267
- 'media_type',
268
- 'encoding',
269
- 'payload_base64',
270
- 'payload_digest',
271
- 'canonical_bytes',
272
- ])
273
- || receipt.schema !== 'lattice.sensor_raw_opaque_receipt.v1'
274
- || receipt.media_type !== 'application/vnd.lattice.sensor-evidence+json'
275
- || receipt.encoding !== 'canonical-json-base64'
276
- || typeof receipt.payload_base64 !== 'string'
277
- || typeof receipt.payload_digest !== 'string'
278
- || !SHA256.test(receipt.payload_digest)
279
- || !Number.isSafeInteger(receipt.canonical_bytes)
280
- || receipt.canonical_bytes <= 0) {
281
- fail('raw opaque receiptが不正');
282
- }
283
- const bytes = Buffer.from(receipt.payload_base64, 'base64');
284
- if (bytes.toString('base64') !== receipt.payload_base64
285
- || bytes.byteLength !== receipt.canonical_bytes
286
- || sha256(bytes) !== receipt.payload_digest) {
287
- fail('raw opaque receiptのbytes/digestが一致しない');
288
- }
289
- const payload = JSON.parse(bytes.toString('utf8'));
290
- if (canonicalizeArtifact(payload) !== bytes.toString('utf8')) {
291
- fail('raw opaque payloadがcanonical JSONでない');
292
- }
293
- return payload;
294
- }
295
-
296
- function validatePortable(portable, querySet, rawEvidence) {
297
- if (!exactRecord(portable, [
298
- 'schema',
299
- 'projection',
300
- 'query_set_digest',
301
- 'outcomes',
302
- 'per_query',
303
- 'aggregate_digest',
304
- ])
305
- || portable.schema !== 'lattice.sensor_portable_preimage.v1'
306
- || portable.projection !== PROJECTION
307
- || portable.query_set_digest !== digestArtifact(querySet)
308
- || !Array.isArray(portable.outcomes)
309
- || portable.outcomes.length !== querySet.queries.length
310
- || !Array.isArray(portable.per_query)
311
- || portable.per_query.length !== portable.outcomes.length
312
- || typeof portable.aggregate_digest !== 'string'
313
- || !SHA256.test(portable.aggregate_digest)
314
- || containsAbsolutePath(portable.outcomes)) {
315
- return false;
316
- }
317
- for (let index = 0; index < portable.outcomes.length; index += 1) {
318
- const record = portable.per_query[index];
319
- if (!exactRecord(record, ['id', 'operation', 'outcome', 'result_digest'])
320
- || record.id !== querySet.queries[index].id
321
- || record.operation !== querySet.queries[index].operation
322
- || record.outcome !== portable.outcomes[index].outcome
323
- || record.result_digest !== digestArtifact(portable.outcomes[index])) {
324
- return false;
325
- }
326
- }
327
- return portable.aggregate_digest === recomputePortableAggregate(portable)
328
- && sameArtifact(portable, buildPortable(querySet, rawEvidence));
329
- }
330
-
331
- function validateDiagnostic(diagnostic, rawEvidence, rawDigest) {
332
- if (!exactRecord(diagnostic, [
333
- 'schema',
334
- 'source_raw_digest',
335
- 'sanitization_manifest',
336
- 'sanitization_manifest_digest',
337
- 'payload',
338
- 'payload_digest',
339
- ])
340
- || diagnostic.schema !== 'lattice.sensor_sanitized_diagnostic.v1'
341
- || diagnostic.source_raw_digest !== rawDigest
342
- || diagnostic.sanitization_manifest_digest
343
- !== digestArtifact(diagnostic.sanitization_manifest)
344
- || diagnostic.payload_digest !== digestArtifact(diagnostic.payload)
345
- || containsAbsolutePath(diagnostic.payload)) {
346
- return false;
347
- }
348
- return sameArtifact(diagnostic, diagnosticArtifact(rawEvidence, rawDigest));
349
- }
350
-
351
- /**
352
- * 1 fresh LatticeSensor runをraw/diagnostic/portableの独立componentへcompileする。
353
- */
354
- export function createRc1EvidenceBundle({
355
- condition,
356
- runId,
357
- querySet,
358
- rawEvidence,
359
- } = {}) {
360
- if (!CONDITIONS.has(condition) || !identifier(runId)
361
- || !validateQuerySet(querySet) || !validateRawEvidence(rawEvidence, querySet)) {
362
- fail('bundle inputが不正');
363
- }
364
- const fixedQuerySet = structuredClone(querySet);
365
- const fixedRaw = structuredClone(rawEvidence);
366
- const raw = rawReceipt(fixedRaw);
367
- const diagnostic = diagnosticArtifact(fixedRaw, raw.payload_digest);
368
- const portable = buildPortable(fixedQuerySet, fixedRaw);
369
- const componentDigests = {
370
- raw: raw.payload_digest,
371
- diagnostic_payload: diagnostic.payload_digest,
372
- sanitization_manifest: diagnostic.sanitization_manifest_digest,
373
- portable: portable.aggregate_digest,
374
- };
375
- return {
376
- schema: 'lattice.rc1.evidence_bundle.v1',
377
- condition,
378
- run_id: runId,
379
- query_set: fixedQuerySet,
380
- query_set_digest: digestArtifact(fixedQuerySet),
381
- raw,
382
- diagnostic,
383
- portable,
384
- component_digests: componentDigests,
385
- };
386
- }
387
-
388
- function validateBundle(value) {
389
- if (!exactRecord(value, [
390
- 'schema',
391
- 'condition',
392
- 'run_id',
393
- 'query_set',
394
- 'query_set_digest',
395
- 'raw',
396
- 'diagnostic',
397
- 'portable',
398
- 'component_digests',
399
- ])
400
- || value.schema !== 'lattice.rc1.evidence_bundle.v1'
401
- || !CONDITIONS.has(value.condition)
402
- || !identifier(value.run_id)
403
- || !validateQuerySet(value.query_set)
404
- || value.query_set_digest !== digestArtifact(value.query_set)
405
- || !exactRecord(value.component_digests, [
406
- 'raw',
407
- 'diagnostic_payload',
408
- 'sanitization_manifest',
409
- 'portable',
410
- ])) {
411
- return false;
412
- }
413
- const rawEvidence = decodeRawReceipt(value.raw);
414
- if (!validateRawEvidence(rawEvidence, value.query_set)
415
- || !validateDiagnostic(value.diagnostic, rawEvidence, value.raw.payload_digest)
416
- || !validatePortable(value.portable, value.query_set, rawEvidence)) {
417
- return false;
418
- }
419
- return value.component_digests.raw === value.raw.payload_digest
420
- && value.component_digests.diagnostic_payload === value.diagnostic.payload_digest
421
- && value.component_digests.sanitization_manifest
422
- === value.diagnostic.sanitization_manifest_digest
423
- && value.component_digests.portable === value.portable.aggregate_digest;
424
- }
425
-
426
- /** @param {unknown} value @returns {boolean} */
427
- export function validateRc1EvidenceBundle(value) {
428
- try {
429
- return validateBundle(value);
430
- } catch {
431
- return false;
432
- }
433
- }
434
-
435
- /** control/treatment各2 fresh runのportable/diagnostic再現性を検証する。 */
436
- export function validateRc1EvidenceCampaign(value) {
437
- try {
438
- if (!Array.isArray(value) || value.length !== 4
439
- || value.some((bundle) => !validateBundle(bundle))
440
- || new Set(value.map(({ run_id: runId }) => runId)).size !== value.length
441
- || new Set(value.map(({ query_set_digest: digest }) => digest)).size !== 1) {
442
- return false;
443
- }
444
- for (const condition of CONDITIONS) {
445
- const runs = value.filter((bundle) => bundle.condition === condition);
446
- if (runs.length !== 2
447
- || runs[0].portable.aggregate_digest !== runs[1].portable.aggregate_digest
448
- || runs[0].diagnostic.payload_digest !== runs[1].diagnostic.payload_digest) {
449
- return false;
450
- }
451
- }
452
- return true;
453
- } catch {
454
- return false;
455
- }
456
- }
1
+ import { createHash } from 'node:crypto';
2
+
3
+ import {
4
+ canonicalizeArtifact,
5
+ digestArtifact,
6
+ } from './artifact-contracts.mjs';
7
+ import { portableSensorOutcome } from './sensor-adapter.mjs';
8
+
9
+ const IDENTIFIER = /^[0-9A-Za-z](?:[0-9A-Za-z._-]{0,127})$/;
10
+ const SHA256 = /^[0-9a-f]{64}$/;
11
+ const CONDITIONS = new Set(['control', 'treatment']);
12
+ const OPERATIONS = new Set(['status', 'query', 'callers', 'callees', 'impact', 'affected']);
13
+ const PROJECTION = 'lattice.sensor_portable_outcome.v1';
14
+ const SANITIZER = 'lattice.sensor_diagnostic_sanitizer.v1';
15
+ const STATUS_REMOVALS = Object.freeze([
16
+ 'projectPath',
17
+ 'indexPath',
18
+ 'lastIndexed',
19
+ 'dbSizeBytes',
20
+ ]);
21
+ const SANITIZATION_RULES = Object.freeze([
22
+ { selector: '/cwd', action: 'replace', replacement: '<repo-root>' },
23
+ ...STATUS_REMOVALS.map((field) => ({
24
+ selector: `/outcomes/*[operation=status]/data/${field}`,
25
+ action: 'remove',
26
+ replacement: null,
27
+ })),
28
+ { selector: '/outcomes/**/node/updatedAt', action: 'remove', replacement: null },
29
+ ]);
30
+
31
+ function isPlainObject(value) {
32
+ return value !== null
33
+ && typeof value === 'object'
34
+ && !Array.isArray(value)
35
+ && Object.getPrototypeOf(value) === Object.prototype;
36
+ }
37
+
38
+ function exactRecord(value, keys) {
39
+ if (!isPlainObject(value)) return false;
40
+ const actual = Object.keys(value).sort();
41
+ const expected = [...keys].sort();
42
+ return actual.length === expected.length
43
+ && actual.every((key, index) => key === expected[index]);
44
+ }
45
+
46
+ function identifier(value) {
47
+ return typeof value === 'string' && IDENTIFIER.test(value);
48
+ }
49
+
50
+ function fail(reason) {
51
+ throw new TypeError(`RC1 v4 evidence bundle契約違反: ${reason}`);
52
+ }
53
+
54
+ function sha256(value) {
55
+ return createHash('sha256').update(value).digest('hex');
56
+ }
57
+
58
+ function escapePointer(value) {
59
+ return String(value).replaceAll('~', '~0').replaceAll('/', '~1');
60
+ }
61
+
62
+ function sameArtifact(left, right) {
63
+ try {
64
+ return digestArtifact(left) === digestArtifact(right);
65
+ } catch {
66
+ return false;
67
+ }
68
+ }
69
+
70
+ function validateQuerySet(querySet) {
71
+ if (!exactRecord(querySet, ['schema', 'queries'])
72
+ || querySet.schema !== 'lattice.sensor_query_set.v2'
73
+ || !Array.isArray(querySet.queries)
74
+ || querySet.queries.length === 0
75
+ || querySet.queries.length > 256) {
76
+ return false;
77
+ }
78
+ const ids = new Set();
79
+ for (const query of querySet.queries) {
80
+ if (!isPlainObject(query) || !identifier(query.id) || ids.has(query.id)
81
+ || !OPERATIONS.has(query.operation)) {
82
+ return false;
83
+ }
84
+ ids.add(query.id);
85
+ if (query.operation === 'status') {
86
+ if (!exactRecord(query, ['id', 'operation'])) return false;
87
+ } else if (query.operation === 'affected') {
88
+ if (!exactRecord(query, ['id', 'operation', 'targets'])
89
+ || !Array.isArray(query.targets)
90
+ || query.targets.length === 0
91
+ || query.targets.some((target) => typeof target !== 'string' || target.length === 0)) {
92
+ return false;
93
+ }
94
+ } else if (!exactRecord(query, ['id', 'operation', 'target'])
95
+ || typeof query.target !== 'string'
96
+ || query.target.length === 0) {
97
+ return false;
98
+ }
99
+ }
100
+ return true;
101
+ }
102
+
103
+ function validateRawEvidence(rawEvidence, querySet) {
104
+ if (!exactRecord(rawEvidence, ['cwd', 'outcomes'])
105
+ || typeof rawEvidence.cwd !== 'string'
106
+ || rawEvidence.cwd.length === 0
107
+ || !Array.isArray(rawEvidence.outcomes)
108
+ || rawEvidence.outcomes.length !== querySet.queries.length) {
109
+ return false;
110
+ }
111
+ for (let index = 0; index < querySet.queries.length; index += 1) {
112
+ const query = querySet.queries[index];
113
+ const outcome = rawEvidence.outcomes[index];
114
+ if (!isPlainObject(outcome)
115
+ || outcome.id !== query.id
116
+ || outcome.operation !== query.operation
117
+ || typeof outcome.outcome !== 'string'
118
+ || outcome.outcome.length === 0) {
119
+ return false;
120
+ }
121
+ if (query.operation !== 'status' && query.operation !== 'affected'
122
+ && outcome.target !== query.target) {
123
+ return false;
124
+ }
125
+ if (query.operation === 'affected'
126
+ && (!Array.isArray(outcome.targets)
127
+ || outcome.targets.length !== query.targets.length
128
+ || outcome.targets.some((entry, targetIndex) => (
129
+ !isPlainObject(entry) || entry.target !== query.targets[targetIndex]
130
+ )))) {
131
+ return false;
132
+ }
133
+ }
134
+ canonicalizeArtifact(rawEvidence);
135
+ return true;
136
+ }
137
+
138
+ function scrubNodeTelemetry(value, pointer, operations, parentKey = null) {
139
+ if (Array.isArray(value)) {
140
+ value.forEach((entry, index) => scrubNodeTelemetry(
141
+ entry,
142
+ `${pointer}/${index}`,
143
+ operations,
144
+ parentKey,
145
+ ));
146
+ return;
147
+ }
148
+ if (!isPlainObject(value)) return;
149
+ if (parentKey === 'node' && Object.hasOwn(value, 'updatedAt')) {
150
+ delete value.updatedAt;
151
+ operations.push({ path: `${pointer}/updatedAt`, action: 'remove', replacement: null });
152
+ }
153
+ for (const [key, entry] of Object.entries(value)) {
154
+ scrubNodeTelemetry(entry, `${pointer}/${escapePointer(key)}`, operations, key);
155
+ }
156
+ }
157
+
158
+ function sanitizeDiagnostic(rawEvidence) {
159
+ const payload = structuredClone(rawEvidence);
160
+ const operations = [{ path: '/cwd', action: 'replace', replacement: '<repo-root>' }];
161
+ payload.cwd = '<repo-root>';
162
+ payload.outcomes.forEach((outcome, index) => {
163
+ if (outcome.operation === 'status' && isPlainObject(outcome.data)) {
164
+ for (const field of STATUS_REMOVALS) {
165
+ if (Object.hasOwn(outcome.data, field)) {
166
+ delete outcome.data[field];
167
+ operations.push({
168
+ path: `/outcomes/${index}/data/${escapePointer(field)}`,
169
+ action: 'remove',
170
+ replacement: null,
171
+ });
172
+ }
173
+ }
174
+ }
175
+ scrubNodeTelemetry(outcome, `/outcomes/${index}`, operations);
176
+ });
177
+ return { payload, operations };
178
+ }
179
+
180
+ function stringHasAbsolutePath(value) {
181
+ return /\bfile:\/\/\//i.test(value)
182
+ || /(?:^|[\s("'=:\[])\/(?!\/)[^\s"')\]]+/.test(value)
183
+ || /(?:^|[\s("'=:\[])[A-Za-z]:[\\/][^\s"')\]]*/.test(value);
184
+ }
185
+
186
+ function containsAbsolutePath(value) {
187
+ if (typeof value === 'string') return stringHasAbsolutePath(value);
188
+ if (Array.isArray(value)) return value.some(containsAbsolutePath);
189
+ if (isPlainObject(value)) return Object.values(value).some(containsAbsolutePath);
190
+ return false;
191
+ }
192
+
193
+ function rawReceipt(rawEvidence) {
194
+ const canonical = canonicalizeArtifact(rawEvidence);
195
+ const bytes = Buffer.from(canonical, 'utf8');
196
+ return {
197
+ schema: 'lattice.sensor_raw_opaque_receipt.v1',
198
+ media_type: 'application/vnd.lattice.sensor-evidence+json',
199
+ encoding: 'canonical-json-base64',
200
+ payload_base64: bytes.toString('base64'),
201
+ payload_digest: sha256(bytes),
202
+ canonical_bytes: bytes.byteLength,
203
+ };
204
+ }
205
+
206
+ function diagnosticArtifact(rawEvidence, sourceRawDigest) {
207
+ const { payload, operations } = sanitizeDiagnostic(rawEvidence);
208
+ if (containsAbsolutePath(payload)) fail('sanitized diagnosticに絶対pathが残る');
209
+ const manifest = {
210
+ schema: 'lattice.sensor_sanitization_manifest.v1',
211
+ sanitizer: SANITIZER,
212
+ rules_digest: digestArtifact(SANITIZATION_RULES),
213
+ operations,
214
+ };
215
+ return {
216
+ schema: 'lattice.sensor_sanitized_diagnostic.v1',
217
+ source_raw_digest: sourceRawDigest,
218
+ sanitization_manifest: manifest,
219
+ sanitization_manifest_digest: digestArtifact(manifest),
220
+ payload,
221
+ payload_digest: digestArtifact(payload),
222
+ };
223
+ }
224
+
225
+ function buildPortable(querySet, rawEvidence) {
226
+ const querySetDigest = digestArtifact(querySet);
227
+ const outcomes = rawEvidence.outcomes.map((outcome) => portableSensorOutcome(outcome));
228
+ if (containsAbsolutePath(outcomes)) fail('portable outcomeに絶対pathが残る');
229
+ const perQuery = outcomes.map((outcome, index) => ({
230
+ id: querySet.queries[index].id,
231
+ operation: querySet.queries[index].operation,
232
+ outcome: outcome.outcome,
233
+ result_digest: digestArtifact(outcome),
234
+ }));
235
+ const portable = {
236
+ schema: 'lattice.sensor_portable_preimage.v1',
237
+ projection: PROJECTION,
238
+ query_set_digest: querySetDigest,
239
+ outcomes,
240
+ per_query: perQuery,
241
+ aggregate_digest: '',
242
+ };
243
+ portable.aggregate_digest = recomputePortableAggregate(portable);
244
+ return portable;
245
+ }
246
+
247
+ /** full portable payloadだけからaggregate digestを再計算する。 */
248
+ export function recomputePortableAggregate(portable) {
249
+ if (!isPlainObject(portable)
250
+ || portable.schema !== 'lattice.sensor_portable_preimage.v1'
251
+ || portable.projection !== PROJECTION
252
+ || typeof portable.query_set_digest !== 'string'
253
+ || !SHA256.test(portable.query_set_digest)
254
+ || !Array.isArray(portable.outcomes)) {
255
+ fail('portable preimageが再計算可能なshapeでない');
256
+ }
257
+ return digestArtifact({
258
+ projection: portable.projection,
259
+ query_set_digest: portable.query_set_digest,
260
+ outcomes: portable.outcomes,
261
+ });
262
+ }
263
+
264
+ function decodeRawReceipt(receipt) {
265
+ if (!exactRecord(receipt, [
266
+ 'schema',
267
+ 'media_type',
268
+ 'encoding',
269
+ 'payload_base64',
270
+ 'payload_digest',
271
+ 'canonical_bytes',
272
+ ])
273
+ || receipt.schema !== 'lattice.sensor_raw_opaque_receipt.v1'
274
+ || receipt.media_type !== 'application/vnd.lattice.sensor-evidence+json'
275
+ || receipt.encoding !== 'canonical-json-base64'
276
+ || typeof receipt.payload_base64 !== 'string'
277
+ || typeof receipt.payload_digest !== 'string'
278
+ || !SHA256.test(receipt.payload_digest)
279
+ || !Number.isSafeInteger(receipt.canonical_bytes)
280
+ || receipt.canonical_bytes <= 0) {
281
+ fail('raw opaque receiptが不正');
282
+ }
283
+ const bytes = Buffer.from(receipt.payload_base64, 'base64');
284
+ if (bytes.toString('base64') !== receipt.payload_base64
285
+ || bytes.byteLength !== receipt.canonical_bytes
286
+ || sha256(bytes) !== receipt.payload_digest) {
287
+ fail('raw opaque receiptのbytes/digestが一致しない');
288
+ }
289
+ const payload = JSON.parse(bytes.toString('utf8'));
290
+ if (canonicalizeArtifact(payload) !== bytes.toString('utf8')) {
291
+ fail('raw opaque payloadがcanonical JSONでない');
292
+ }
293
+ return payload;
294
+ }
295
+
296
+ function validatePortable(portable, querySet, rawEvidence) {
297
+ if (!exactRecord(portable, [
298
+ 'schema',
299
+ 'projection',
300
+ 'query_set_digest',
301
+ 'outcomes',
302
+ 'per_query',
303
+ 'aggregate_digest',
304
+ ])
305
+ || portable.schema !== 'lattice.sensor_portable_preimage.v1'
306
+ || portable.projection !== PROJECTION
307
+ || portable.query_set_digest !== digestArtifact(querySet)
308
+ || !Array.isArray(portable.outcomes)
309
+ || portable.outcomes.length !== querySet.queries.length
310
+ || !Array.isArray(portable.per_query)
311
+ || portable.per_query.length !== portable.outcomes.length
312
+ || typeof portable.aggregate_digest !== 'string'
313
+ || !SHA256.test(portable.aggregate_digest)
314
+ || containsAbsolutePath(portable.outcomes)) {
315
+ return false;
316
+ }
317
+ for (let index = 0; index < portable.outcomes.length; index += 1) {
318
+ const record = portable.per_query[index];
319
+ if (!exactRecord(record, ['id', 'operation', 'outcome', 'result_digest'])
320
+ || record.id !== querySet.queries[index].id
321
+ || record.operation !== querySet.queries[index].operation
322
+ || record.outcome !== portable.outcomes[index].outcome
323
+ || record.result_digest !== digestArtifact(portable.outcomes[index])) {
324
+ return false;
325
+ }
326
+ }
327
+ return portable.aggregate_digest === recomputePortableAggregate(portable)
328
+ && sameArtifact(portable, buildPortable(querySet, rawEvidence));
329
+ }
330
+
331
+ function validateDiagnostic(diagnostic, rawEvidence, rawDigest) {
332
+ if (!exactRecord(diagnostic, [
333
+ 'schema',
334
+ 'source_raw_digest',
335
+ 'sanitization_manifest',
336
+ 'sanitization_manifest_digest',
337
+ 'payload',
338
+ 'payload_digest',
339
+ ])
340
+ || diagnostic.schema !== 'lattice.sensor_sanitized_diagnostic.v1'
341
+ || diagnostic.source_raw_digest !== rawDigest
342
+ || diagnostic.sanitization_manifest_digest
343
+ !== digestArtifact(diagnostic.sanitization_manifest)
344
+ || diagnostic.payload_digest !== digestArtifact(diagnostic.payload)
345
+ || containsAbsolutePath(diagnostic.payload)) {
346
+ return false;
347
+ }
348
+ return sameArtifact(diagnostic, diagnosticArtifact(rawEvidence, rawDigest));
349
+ }
350
+
351
+ /**
352
+ * 1 fresh LatticeSensor runをraw/diagnostic/portableの独立componentへcompileする。
353
+ */
354
+ export function createRc1EvidenceBundle({
355
+ condition,
356
+ runId,
357
+ querySet,
358
+ rawEvidence,
359
+ } = {}) {
360
+ if (!CONDITIONS.has(condition) || !identifier(runId)
361
+ || !validateQuerySet(querySet) || !validateRawEvidence(rawEvidence, querySet)) {
362
+ fail('bundle inputが不正');
363
+ }
364
+ const fixedQuerySet = structuredClone(querySet);
365
+ const fixedRaw = structuredClone(rawEvidence);
366
+ const raw = rawReceipt(fixedRaw);
367
+ const diagnostic = diagnosticArtifact(fixedRaw, raw.payload_digest);
368
+ const portable = buildPortable(fixedQuerySet, fixedRaw);
369
+ const componentDigests = {
370
+ raw: raw.payload_digest,
371
+ diagnostic_payload: diagnostic.payload_digest,
372
+ sanitization_manifest: diagnostic.sanitization_manifest_digest,
373
+ portable: portable.aggregate_digest,
374
+ };
375
+ return {
376
+ schema: 'lattice.rc1.evidence_bundle.v1',
377
+ condition,
378
+ run_id: runId,
379
+ query_set: fixedQuerySet,
380
+ query_set_digest: digestArtifact(fixedQuerySet),
381
+ raw,
382
+ diagnostic,
383
+ portable,
384
+ component_digests: componentDigests,
385
+ };
386
+ }
387
+
388
+ function validateBundle(value) {
389
+ if (!exactRecord(value, [
390
+ 'schema',
391
+ 'condition',
392
+ 'run_id',
393
+ 'query_set',
394
+ 'query_set_digest',
395
+ 'raw',
396
+ 'diagnostic',
397
+ 'portable',
398
+ 'component_digests',
399
+ ])
400
+ || value.schema !== 'lattice.rc1.evidence_bundle.v1'
401
+ || !CONDITIONS.has(value.condition)
402
+ || !identifier(value.run_id)
403
+ || !validateQuerySet(value.query_set)
404
+ || value.query_set_digest !== digestArtifact(value.query_set)
405
+ || !exactRecord(value.component_digests, [
406
+ 'raw',
407
+ 'diagnostic_payload',
408
+ 'sanitization_manifest',
409
+ 'portable',
410
+ ])) {
411
+ return false;
412
+ }
413
+ const rawEvidence = decodeRawReceipt(value.raw);
414
+ if (!validateRawEvidence(rawEvidence, value.query_set)
415
+ || !validateDiagnostic(value.diagnostic, rawEvidence, value.raw.payload_digest)
416
+ || !validatePortable(value.portable, value.query_set, rawEvidence)) {
417
+ return false;
418
+ }
419
+ return value.component_digests.raw === value.raw.payload_digest
420
+ && value.component_digests.diagnostic_payload === value.diagnostic.payload_digest
421
+ && value.component_digests.sanitization_manifest
422
+ === value.diagnostic.sanitization_manifest_digest
423
+ && value.component_digests.portable === value.portable.aggregate_digest;
424
+ }
425
+
426
+ /** @param {unknown} value @returns {boolean} */
427
+ export function validateRc1EvidenceBundle(value) {
428
+ try {
429
+ return validateBundle(value);
430
+ } catch {
431
+ return false;
432
+ }
433
+ }
434
+
435
+ /** control/treatment各2 fresh runのportable/diagnostic再現性を検証する。 */
436
+ export function validateRc1EvidenceCampaign(value) {
437
+ try {
438
+ if (!Array.isArray(value) || value.length !== 4
439
+ || value.some((bundle) => !validateBundle(bundle))
440
+ || new Set(value.map(({ run_id: runId }) => runId)).size !== value.length
441
+ || new Set(value.map(({ query_set_digest: digest }) => digest)).size !== 1) {
442
+ return false;
443
+ }
444
+ for (const condition of CONDITIONS) {
445
+ const runs = value.filter((bundle) => bundle.condition === condition);
446
+ if (runs.length !== 2
447
+ || runs[0].portable.aggregate_digest !== runs[1].portable.aggregate_digest
448
+ || runs[0].diagnostic.payload_digest !== runs[1].diagnostic.payload_digest) {
449
+ return false;
450
+ }
451
+ }
452
+ return true;
453
+ } catch {
454
+ return false;
455
+ }
456
+ }