@quolu/lattice 0.52.2 → 0.52.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/artifact-contracts-v2.mjs +325 -325
- package/src/artifact-contracts.mjs +895 -895
- package/src/boundary-compiler.mjs +712 -712
- package/src/boundary-observation-compiler-v2.mjs +344 -344
- package/src/bounded-seam.mjs +230 -230
- package/src/bridge-address.mjs +107 -107
- package/src/bridge-cli.mjs +297 -297
- package/src/bridge-config.mjs +346 -346
- package/src/bridge-daemon.mjs +378 -378
- package/src/bridge-hub-protocol.mjs +198 -0
- package/src/bridge-launch-agent.mjs +323 -323
- package/src/bridge-registrar.mjs +102 -102
- package/src/bridge-server.mjs +376 -376
- package/src/cli-help.mjs +308 -308
- package/src/cli-stdio.mjs +40 -40
- package/src/control-compiler.mjs +532 -532
- package/src/dag-chain.mjs +261 -261
- package/src/factory-diagnostics.mjs +188 -188
- package/src/git-process.mjs +72 -0
- package/src/hash-chain.mjs +76 -76
- package/src/hooks-cli.mjs +1057 -1057
- package/src/isolation-runner.mjs +409 -409
- package/src/node-version-guard.mjs +44 -44
- package/src/project-cli.mjs +699 -697
- package/src/project-identity.mjs +130 -130
- package/src/rc1-black-box-oracle.mjs +906 -906
- package/src/rc1-comparison.mjs +154 -154
- package/src/rc1-evidence-bundle.mjs +456 -456
- package/src/rc1-v4-campaign.mjs +708 -708
- package/src/rc1-v4-transform.mjs +467 -467
- package/src/rc1-v5-artifact-set.mjs +855 -855
- package/src/rc1-v5-behavior-evidence.mjs +594 -594
- package/src/rc1-v5-campaign.mjs +797 -797
- package/src/rc1-v5-transform.mjs +421 -421
- package/src/rc1-v6-artifact-set.mjs +807 -807
- package/src/rc1-v6-behavior-evidence.mjs +273 -273
- package/src/rc1-v6-campaign.mjs +623 -623
- package/src/rc1-v6-causal-binding.mjs +473 -473
- package/src/rc1-v6-measurement.mjs +313 -313
- package/src/rc2-artifact-set.mjs +1584 -1584
- package/src/rc2-campaign.mjs +1506 -1506
- package/src/rc2-delivery-policy-front-end.mjs +1079 -1079
- package/src/rc2-delivery-policy-oracle.mjs +134 -134
- package/src/rc2-delivery-policy-transform.mjs +1127 -1127
- package/src/rc2-rc1-transfer-front-end.mjs +511 -511
- package/src/rc3-actual-dogfood.mjs +652 -652
- package/src/rc3-dogfood-scaffold.mjs +315 -315
- package/src/rc3-scripted-campaign.mjs +1383 -1383
- package/src/rc4-stage1-dogfood.mjs +673 -673
- package/src/runtime-adapter-registry.mjs +524 -524
- package/src/runtime-cli.mjs +4588 -4588
- package/src/runtime-contracts.mjs +824 -824
- package/src/runtime-control-store.mjs +604 -604
- package/src/runtime-controller-protocol.mjs +587 -587
- package/src/runtime-decision-verifier.mjs +701 -701
- package/src/runtime-diff-observer.mjs +361 -361
- package/src/runtime-direct-os-observer.mjs +301 -301
- package/src/runtime-driver-state.mjs +166 -166
- package/src/runtime-engine.mjs +779 -779
- package/src/runtime-errors.mjs +356 -356
- package/src/runtime-event-store.mjs +189 -189
- package/src/runtime-front-end.mjs +925 -925
- package/src/runtime-gate-store.mjs +481 -481
- package/src/runtime-hold-recompile.mjs +916 -916
- package/src/runtime-io-sentinel.mjs +391 -391
- package/src/runtime-lifecycle-lock.mjs +294 -294
- package/src/runtime-managed-supervisor.mjs +1490 -1490
- package/src/runtime-multi-epoch-store.mjs +838 -838
- package/src/runtime-projection.mjs +269 -269
- package/src/runtime-pull-intake.mjs +1192 -1192
- package/src/runtime-scripted-adapter-controller.mjs +1160 -1160
- package/src/runtime-scripted-executor.mjs +163 -163
- package/src/runtime-scripted-worktree.mjs +104 -104
- package/src/runtime-seam-resolve.mjs +428 -428
- package/src/runtime-seam-treatment.mjs +173 -173
- package/src/runtime-socket-owner.mjs +125 -125
- package/src/runtime-work-order-contracts.mjs +91 -91
- package/src/runtime-work-order-controller.mjs +1171 -1171
- package/src/runtime-worktree-executor.mjs +199 -199
- package/src/schedulability-compiler-v2.mjs +303 -303
- package/src/schedulability-verifier-v2.mjs +317 -317
- package/src/seam-apply.mjs +549 -549
- package/src/seam-commit-shared.mjs +22 -22
- package/src/seam-commit-transform.mjs +81 -81
- package/src/seam-commit.mjs +18 -18
- package/src/seam-cost.mjs +322 -322
- package/src/seam-derivation.mjs +188 -188
- package/src/seam-gate.mjs +146 -146
- package/src/seam-proposal-contracts.mjs +446 -446
- package/src/seam-proposal-queries.mjs +521 -521
- package/src/seam-proposal.mjs +2011 -2011
- package/src/seam-ref.mjs +33 -33
- package/src/seam-rewrite.mjs +286 -286
- package/src/seam-transform.mjs +554 -554
- package/src/seam-verification.mjs +260 -260
- package/src/sensor-adapter.mjs +432 -432
- package/src/sensor-cli.mjs +139 -139
- package/src/sensor-diff.mjs +661 -661
- package/src/sensor-node-runtime.mjs +53 -53
- package/src/sensor-runtime.mjs +52 -52
- package/src/timestamp-contract.mjs +8 -8
- package/src/todo-audit-pending.mjs +91 -91
- package/src/todo-chain.mjs +178 -178
- package/src/todo-cli.mjs +3143 -3141
- package/src/todo-contracts.mjs +728 -728
- package/src/todo-dashboard-registry.mjs +573 -573
- package/src/todo-dispatch-shape.mjs +190 -190
- package/src/todo-gantt-html-independence.mjs +239 -239
- package/src/todo-gantt-html-shared.mjs +226 -226
- package/src/todo-gantt-html-style.mjs +131 -131
- package/src/todo-gantt-html.mjs +248 -248
- package/src/todo-gantt-layout.mjs +974 -974
- package/src/todo-gantt-live.mjs +361 -361
- package/src/todo-gantt-nested.mjs +263 -263
- package/src/todo-gantt-presentation.mjs +217 -217
- package/src/todo-gantt-scope.mjs +123 -123
- package/src/todo-gantt-svg.mjs +353 -353
- package/src/todo-independence-contracts.mjs +595 -595
- package/src/todo-independence-guidance.mjs +322 -322
- package/src/todo-independence.mjs +640 -640
- package/src/todo-markdown-renderer.mjs +260 -260
- package/src/todo-migration.mjs +448 -448
- package/src/todo-narrative-anchor.mjs +130 -130
- package/src/todo-note-store.mjs +629 -629
- package/src/todo-parallel-candidates.mjs +114 -114
- package/src/todo-revision.mjs +995 -995
- package/src/todo-split.mjs +472 -472
- package/src/todo-status.mjs +690 -690
- package/src/todo-store-git-transaction.mjs +418 -418
- package/src/todo-store.mjs +4360 -4322
- package/src/treatment-compiler.mjs +728 -728
- package/src/treatment-runner.mjs +656 -656
- 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
|
+
}
|