@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.
- package/LICENSE +147 -147
- package/README.ja.md +355 -355
- package/README.md +258 -258
- package/bin/lattice-mcp.mjs +0 -0
- package/bin/lattice-scripted-adapter.mjs +0 -0
- package/bin/lattice-scripted-worker.mjs +0 -0
- package/bin/lattice-work-order-adapter.mjs +0 -0
- package/bin/lattice.mjs +0 -0
- package/docs/bridge-setup.md +132 -132
- package/docs/schemas/lattice.executor_packet.v1.schema.json +57 -57
- package/docs/schemas/lattice.executor_receipt.v1.schema.json +66 -66
- package/docs/schemas/lattice.phase_todo_revision.v3.schema.json +360 -360
- package/docs/schemas/lattice.plan_create_input.v1.schema.json +56 -56
- package/docs/schemas/lattice.plan_create_input.v2.schema.json +72 -72
- package/docs/schemas/lattice.plan_create_input.v3.schema.json +81 -81
- package/docs/schemas/lattice.plan_create_input.v4.schema.json +85 -85
- package/docs/schemas/lattice.run_request.v1.schema.json +238 -238
- package/docs/schemas/lattice.runtime_adapter_capabilities.v2.schema.json +55 -55
- package/docs/schemas/lattice.runtime_adapter_registration_input.v1.schema.json +78 -78
- package/docs/schemas/lattice.runtime_adapter_registration_input.v2.schema.json +86 -86
- package/docs/schemas/lattice.todo_extraction.v2.schema.json +298 -298
- package/docs/schemas/lattice.todo_extraction.v3.schema.json +146 -146
- package/docs/schemas/lattice.todo_revision.v2.schema.json +260 -260
- package/docs/schemas/lattice.todo_revision_set.v3.schema.json +363 -363
- package/package.json +103 -103
- package/sensor/LICENSE +21 -21
- package/sensor/NOTICE +19 -19
- package/sensor/dist/bin/lattice-sensor.js +9 -9
- package/sensor/dist/db/index.js +24 -24
- package/sensor/dist/db/migrations.js +41 -41
- package/sensor/dist/db/queries.js +164 -164
- package/sensor/dist/db/schema.sql +205 -205
- package/sensor/dist/directory.js +5 -5
- package/sensor/dist/extraction/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-cfml.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-cfquery.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-cfscript.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-cobol.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-erlang.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-go.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-java.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-javascript.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-nix.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-python.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-tsx.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-typescript.wasm +0 -0
- package/sensor/dist/extraction/wasm/tree-sitter-vbnet.wasm +0 -0
- package/sensor/dist/mcp/liveness-watchdog.js +53 -53
- package/sensor/dist/mcp/server-instructions.js +95 -95
- package/sensor/package.json +56 -56
- 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-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/hash-chain.mjs +76 -76
- package/src/hooks-cli.mjs +1057 -1053
- package/src/isolation-runner.mjs +409 -409
- package/src/node-version-guard.mjs +44 -44
- package/src/project-cli.mjs +697 -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 -1502
- 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 -520
- package/src/runtime-cli.mjs +4588 -4584
- package/src/runtime-contracts.mjs +824 -824
- package/src/runtime-control-store.mjs +604 -600
- 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 -162
- 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 -477
- package/src/runtime-hold-recompile.mjs +916 -916
- package/src/runtime-io-sentinel.mjs +391 -391
- package/src/runtime-lifecycle-lock.mjs +294 -290
- package/src/runtime-managed-supervisor.mjs +1490 -1490
- package/src/runtime-multi-epoch-store.mjs +838 -834
- package/src/runtime-projection.mjs +269 -269
- package/src/runtime-pull-intake.mjs +1192 -1188
- package/src/runtime-scripted-adapter-controller.mjs +1160 -1156
- 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 -1167
- 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 +3141 -3126
- 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 -625
- 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 +4322 -4314
- package/src/treatment-compiler.mjs +728 -728
- package/src/treatment-runner.mjs +656 -656
- package/src/witness-scaffold.mjs +180 -180
|
@@ -1,473 +1,473 @@
|
|
|
1
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
-
|
|
3
|
-
import { digestArtifact } from './artifact-contracts.mjs';
|
|
4
|
-
import { validateRc1BlackBoxOracle } from './rc1-black-box-oracle.mjs';
|
|
5
|
-
import { validateRc1EvidenceBundle } from './rc1-evidence-bundle.mjs';
|
|
6
|
-
|
|
7
|
-
const SHA256 = /^[0-9a-f]{64}$/;
|
|
8
|
-
const GIT_SHA1 = /^[0-9a-f]{40}$/;
|
|
9
|
-
const IDENTIFIER = /^[A-Za-z][A-Za-z0-9_-]{0,127}$/;
|
|
10
|
-
const RECEIPT_KEYS = [
|
|
11
|
-
'schema',
|
|
12
|
-
'role',
|
|
13
|
-
'base_sha',
|
|
14
|
-
'oracle_digest',
|
|
15
|
-
'entrypoint',
|
|
16
|
-
'export_name',
|
|
17
|
-
'entrypoint_content_digest',
|
|
18
|
-
'surface',
|
|
19
|
-
'surface_digest',
|
|
20
|
-
'observation',
|
|
21
|
-
'outcome',
|
|
22
|
-
'case_results',
|
|
23
|
-
'case_contract_digest',
|
|
24
|
-
'runtime_identity',
|
|
25
|
-
'runtime_identity_digest',
|
|
26
|
-
'receipt_digest',
|
|
27
|
-
];
|
|
28
|
-
|
|
29
|
-
function exactRecord(value, keys) {
|
|
30
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)
|
|
31
|
-
|| Object.getPrototypeOf(value) !== Object.prototype) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
const actual = Object.keys(value).sort();
|
|
35
|
-
const expected = [...keys].sort();
|
|
36
|
-
return actual.length === expected.length
|
|
37
|
-
&& actual.every((key, index) => key === expected[index]);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function repoPath(value) {
|
|
41
|
-
return typeof value === 'string'
|
|
42
|
-
&& value.length > 0
|
|
43
|
-
&& value.length <= 512
|
|
44
|
-
&& !value.startsWith('/')
|
|
45
|
-
&& !value.includes('\\')
|
|
46
|
-
&& !value.split('/').includes('..');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function verification(checks) {
|
|
50
|
-
const failedConditions = checks.filter(({ passed }) => !passed).map(({ id }) => id);
|
|
51
|
-
return {
|
|
52
|
-
schema: 'lattice.rc1.causal_binding_verification.v1',
|
|
53
|
-
valid: failedConditions.length === 0,
|
|
54
|
-
checks,
|
|
55
|
-
failed_conditions: failedConditions,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function safeDigest(value) {
|
|
60
|
-
try {
|
|
61
|
-
return digestArtifact(value);
|
|
62
|
-
} catch {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function digestWithout(value, field) {
|
|
68
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
69
|
-
const { [field]: ignored, ...preimage } = value;
|
|
70
|
-
return safeDigest(preimage);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function validRuntimeIdentity(value) {
|
|
74
|
-
if (!exactRecord(value, [
|
|
75
|
-
'schema',
|
|
76
|
-
'node_version',
|
|
77
|
-
'exec_argv',
|
|
78
|
-
'environment',
|
|
79
|
-
'executor_source_digest',
|
|
80
|
-
])
|
|
81
|
-
|| value.schema !== 'lattice.rc1.oracle_runtime_identity.v1'
|
|
82
|
-
|| typeof value.node_version !== 'string'
|
|
83
|
-
|| value.node_version.length === 0
|
|
84
|
-
|| value.node_version.length > 128
|
|
85
|
-
|| !Array.isArray(value.exec_argv)
|
|
86
|
-
|| value.exec_argv.length > 32
|
|
87
|
-
|| !value.exec_argv.every((entry) => typeof entry === 'string' && entry.length <= 1024)
|
|
88
|
-
|| value.environment === null
|
|
89
|
-
|| typeof value.environment !== 'object'
|
|
90
|
-
|| Array.isArray(value.environment)
|
|
91
|
-
|| Object.getPrototypeOf(value.environment) !== Object.prototype
|
|
92
|
-
|| Object.keys(value.environment).length > 32
|
|
93
|
-
|| !Object.entries(value.environment).every(([key, entry]) => (
|
|
94
|
-
/^[A-Z][A-Z0-9_]{0,127}$/.test(key)
|
|
95
|
-
&& typeof entry === 'string'
|
|
96
|
-
&& entry.length <= 4096
|
|
97
|
-
))
|
|
98
|
-
|| !SHA256.test(value.executor_source_digest)) {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
return safeDigest(value) !== null;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function validCaseResult(value) {
|
|
105
|
-
return exactRecord(value, [
|
|
106
|
-
'id',
|
|
107
|
-
'outcome',
|
|
108
|
-
'observed_kind',
|
|
109
|
-
'expected_digest',
|
|
110
|
-
'observed_digest',
|
|
111
|
-
])
|
|
112
|
-
&& typeof value.id === 'string'
|
|
113
|
-
&& IDENTIFIER.test(value.id)
|
|
114
|
-
&& (value.outcome === 'passed' || value.outcome === 'failed')
|
|
115
|
-
&& (value.observed_kind === 'return' || value.observed_kind === 'throw')
|
|
116
|
-
&& SHA256.test(value.expected_digest)
|
|
117
|
-
&& SHA256.test(value.observed_digest);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function expectedCaseContract(oracle) {
|
|
121
|
-
return oracle.cases.map(({ id, expected }) => ({
|
|
122
|
-
id,
|
|
123
|
-
expected_kind: expected.kind,
|
|
124
|
-
expected_digest: digestArtifact(expected),
|
|
125
|
-
}));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function caseSemanticsValid(caseResults, oracle) {
|
|
129
|
-
if (!Array.isArray(caseResults) || caseResults.length !== oracle.cases.length) return false;
|
|
130
|
-
return caseResults.every((result, index) => {
|
|
131
|
-
if (!validCaseResult(result)) return false;
|
|
132
|
-
const oracleCase = oracle.cases[index];
|
|
133
|
-
const expectedDigest = digestArtifact(oracleCase.expected);
|
|
134
|
-
if (result.id !== oracleCase.id
|
|
135
|
-
|| result.expected_digest !== expectedDigest
|
|
136
|
-
|| (result.outcome === 'passed' && (
|
|
137
|
-
result.observed_kind !== oracleCase.expected.kind
|
|
138
|
-
|| result.observed_digest !== expectedDigest
|
|
139
|
-
))) {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
return true;
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function receiptShapeValid(receipt) {
|
|
147
|
-
return exactRecord(receipt, RECEIPT_KEYS)
|
|
148
|
-
&& receipt.schema === 'lattice.rc1.black_box_behavior_receipt.v4'
|
|
149
|
-
&& (receipt.role === 'pre_transform' || receipt.role === 'post_transform')
|
|
150
|
-
&& GIT_SHA1.test(receipt.base_sha)
|
|
151
|
-
&& SHA256.test(receipt.oracle_digest)
|
|
152
|
-
&& repoPath(receipt.entrypoint)
|
|
153
|
-
&& typeof receipt.export_name === 'string'
|
|
154
|
-
&& IDENTIFIER.test(receipt.export_name)
|
|
155
|
-
&& SHA256.test(receipt.entrypoint_content_digest)
|
|
156
|
-
&& SHA256.test(receipt.surface_digest)
|
|
157
|
-
&& (receipt.outcome === 'passed' || receipt.outcome === 'failed')
|
|
158
|
-
&& SHA256.test(receipt.case_contract_digest)
|
|
159
|
-
&& SHA256.test(receipt.runtime_identity_digest)
|
|
160
|
-
&& SHA256.test(receipt.receipt_digest);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* 保存oracleと独立runtime期待値から、v6 behavior receiptのcase意味論を再計算する。
|
|
165
|
-
*/
|
|
166
|
-
export function verifyRc1V6BehaviorReceipt(options) {
|
|
167
|
-
if (!exactRecord(options, [
|
|
168
|
-
'receipt',
|
|
169
|
-
'oracle',
|
|
170
|
-
'expectedRole',
|
|
171
|
-
'expectedRuntimeIdentity',
|
|
172
|
-
])) {
|
|
173
|
-
return verification([{ id: 'input_contract', passed: false }]);
|
|
174
|
-
}
|
|
175
|
-
const {
|
|
176
|
-
receipt,
|
|
177
|
-
oracle,
|
|
178
|
-
expectedRole,
|
|
179
|
-
expectedRuntimeIdentity,
|
|
180
|
-
} = options;
|
|
181
|
-
const oracleValid = validateRc1BlackBoxOracle(oracle);
|
|
182
|
-
const runtimeValid = validRuntimeIdentity(expectedRuntimeIdentity);
|
|
183
|
-
const shapeValid = receiptShapeValid(receipt);
|
|
184
|
-
const caseContract = oracleValid ? expectedCaseContract(oracle) : null;
|
|
185
|
-
const caseSemantics = shapeValid && oracleValid
|
|
186
|
-
? caseSemanticsValid(receipt.case_results, oracle)
|
|
187
|
-
: false;
|
|
188
|
-
const recomputedOutcome = caseSemantics
|
|
189
|
-
? (receipt.case_results.every(({ outcome }) => outcome === 'passed') ? 'passed' : 'failed')
|
|
190
|
-
: null;
|
|
191
|
-
|
|
192
|
-
return verification([
|
|
193
|
-
{ id: 'oracle_contract', passed: oracleValid },
|
|
194
|
-
{ id: 'runtime_contract', passed: runtimeValid },
|
|
195
|
-
{ id: 'receipt_shape', passed: shapeValid },
|
|
196
|
-
{
|
|
197
|
-
id: 'receipt_role',
|
|
198
|
-
passed: shapeValid
|
|
199
|
-
&& (expectedRole === 'pre_transform' || expectedRole === 'post_transform')
|
|
200
|
-
&& receipt.role === expectedRole,
|
|
201
|
-
},
|
|
202
|
-
{
|
|
203
|
-
id: 'oracle_identity',
|
|
204
|
-
passed: shapeValid && oracleValid
|
|
205
|
-
&& receipt.oracle_digest === safeDigest(oracle)
|
|
206
|
-
&& receipt.entrypoint === oracle.entrypoint
|
|
207
|
-
&& receipt.export_name === oracle.export_name,
|
|
208
|
-
},
|
|
209
|
-
{
|
|
210
|
-
id: 'case_contract',
|
|
211
|
-
passed: shapeValid && caseContract !== null
|
|
212
|
-
&& receipt.case_contract_digest === safeDigest(caseContract),
|
|
213
|
-
},
|
|
214
|
-
{ id: 'case_semantics', passed: caseSemantics },
|
|
215
|
-
{
|
|
216
|
-
id: 'overall_outcome',
|
|
217
|
-
passed: recomputedOutcome !== null && receipt.outcome === recomputedOutcome,
|
|
218
|
-
},
|
|
219
|
-
{
|
|
220
|
-
id: 'runtime_identity',
|
|
221
|
-
passed: shapeValid && runtimeValid
|
|
222
|
-
&& validRuntimeIdentity(receipt.runtime_identity)
|
|
223
|
-
&& receipt.runtime_identity_digest === safeDigest(receipt.runtime_identity)
|
|
224
|
-
&& isDeepStrictEqual(receipt.runtime_identity, expectedRuntimeIdentity),
|
|
225
|
-
},
|
|
226
|
-
{
|
|
227
|
-
id: 'surface_identity',
|
|
228
|
-
passed: shapeValid
|
|
229
|
-
&& receipt.surface_digest === safeDigest(receipt.surface)
|
|
230
|
-
&& exactRecord(receipt.observation, ['before_surface_digest', 'after_surface_digest'])
|
|
231
|
-
&& receipt.observation.before_surface_digest === receipt.surface_digest
|
|
232
|
-
&& receipt.observation.after_surface_digest === receipt.surface_digest,
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
id: 'receipt_digest',
|
|
236
|
-
passed: shapeValid && receipt.receipt_digest === digestWithout(receipt, 'receipt_digest'),
|
|
237
|
-
},
|
|
238
|
-
]);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function validSourceSnapshot(value) {
|
|
242
|
-
if (!exactRecord(value, ['schema', 'files'])
|
|
243
|
-
|| value.schema !== 'lattice.rc1.source_snapshot.v1'
|
|
244
|
-
|| !Array.isArray(value.files)
|
|
245
|
-
|| value.files.length === 0
|
|
246
|
-
|| value.files.length > 256) {
|
|
247
|
-
return false;
|
|
248
|
-
}
|
|
249
|
-
const paths = new Set();
|
|
250
|
-
for (const entry of value.files) {
|
|
251
|
-
if (!exactRecord(entry, ['path', 'state', 'content_digest'])
|
|
252
|
-
|| !repoPath(entry.path)
|
|
253
|
-
|| paths.has(entry.path)
|
|
254
|
-
|| !['file', 'present', 'absent'].includes(entry.state)
|
|
255
|
-
|| (entry.state === 'absent'
|
|
256
|
-
? entry.content_digest !== null
|
|
257
|
-
: typeof entry.content_digest !== 'string' || !SHA256.test(entry.content_digest))) {
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
paths.add(entry.path);
|
|
261
|
-
}
|
|
262
|
-
return value.files.every(({ path }, index) => index === 0 || value.files[index - 1].path < path);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function validLatticeSensorIdentity(value) {
|
|
266
|
-
return exactRecord(value, [
|
|
267
|
-
'schema',
|
|
268
|
-
'version',
|
|
269
|
-
'executable_ref',
|
|
270
|
-
'executable_digest',
|
|
271
|
-
])
|
|
272
|
-
&& value.schema === 'lattice.rc1.sensor_identity.v1'
|
|
273
|
-
&& typeof value.version === 'string'
|
|
274
|
-
&& /^\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.-]+)?$/.test(value.version)
|
|
275
|
-
// `sensor` is accepted only as an immutable pre-cutover artifact value.
|
|
276
|
-
// New captures always emit `lattice-sensor`.
|
|
277
|
-
&& (value.executable_ref === 'lattice-sensor' || value.executable_ref === 'sensor')
|
|
278
|
-
&& SHA256.test(value.executable_digest);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function baseEvidenceBundle(bundle) {
|
|
282
|
-
if (bundle === null || typeof bundle !== 'object' || Array.isArray(bundle)) return null;
|
|
283
|
-
const { measurement: ignoredMeasurement, measurement_digest: ignoredDigest, ...base } = bundle;
|
|
284
|
-
return { ...base, schema: 'lattice.rc1.evidence_bundle.v1' };
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export function createRc1V6EvidenceBundleDescriptor(bundle) {
|
|
288
|
-
return {
|
|
289
|
-
schema: 'lattice.rc1.evidence_bundle_descriptor.v1',
|
|
290
|
-
condition: bundle.condition,
|
|
291
|
-
run_id: bundle.run_id,
|
|
292
|
-
query_set_digest: bundle.query_set_digest,
|
|
293
|
-
raw_digest: bundle.raw.payload_digest,
|
|
294
|
-
diagnostic_payload_digest: bundle.diagnostic.payload_digest,
|
|
295
|
-
sanitization_manifest_digest: bundle.diagnostic.sanitization_manifest_digest,
|
|
296
|
-
portable_digest: bundle.portable.aggregate_digest,
|
|
297
|
-
measurement_digest: bundle.measurement_digest,
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* v6 LatticeSensor runを、独立したsource/tool期待値と保存raw evidenceへcross-bindする。
|
|
303
|
-
*/
|
|
304
|
-
export function verifyRc1V6RunEvidence(options) {
|
|
305
|
-
if (!exactRecord(options, ['run', 'bundle', 'expected'])) {
|
|
306
|
-
return verification([{ id: 'input_contract', passed: false }]);
|
|
307
|
-
}
|
|
308
|
-
const { run, bundle, expected } = options;
|
|
309
|
-
const runValid = exactRecord(run, [
|
|
310
|
-
'schema',
|
|
311
|
-
'condition',
|
|
312
|
-
'run_id',
|
|
313
|
-
'evidence_bundle_descriptor_digest',
|
|
314
|
-
'measurement_digest',
|
|
315
|
-
])
|
|
316
|
-
&& run.schema === 'lattice.rc1.condition_run.v2'
|
|
317
|
-
&& (run.condition === 'control' || run.condition === 'treatment')
|
|
318
|
-
&& typeof run.run_id === 'string'
|
|
319
|
-
&& IDENTIFIER.test(run.run_id)
|
|
320
|
-
&& SHA256.test(run.evidence_bundle_descriptor_digest)
|
|
321
|
-
&& SHA256.test(run.measurement_digest);
|
|
322
|
-
const bundleValid = exactRecord(bundle, [
|
|
323
|
-
'schema',
|
|
324
|
-
'condition',
|
|
325
|
-
'run_id',
|
|
326
|
-
'query_set',
|
|
327
|
-
'query_set_digest',
|
|
328
|
-
'raw',
|
|
329
|
-
'diagnostic',
|
|
330
|
-
'portable',
|
|
331
|
-
'component_digests',
|
|
332
|
-
'measurement',
|
|
333
|
-
'measurement_digest',
|
|
334
|
-
])
|
|
335
|
-
&& bundle.schema === 'lattice.rc1.evidence_bundle.v2'
|
|
336
|
-
&& validateRc1EvidenceBundle(baseEvidenceBundle(bundle));
|
|
337
|
-
const expectedValid = exactRecord(expected, [
|
|
338
|
-
'base_sha',
|
|
339
|
-
'patch_digest',
|
|
340
|
-
'snapshot',
|
|
341
|
-
'sensor_identity',
|
|
342
|
-
'query_set_digest',
|
|
343
|
-
])
|
|
344
|
-
&& GIT_SHA1.test(expected.base_sha)
|
|
345
|
-
&& (expected.patch_digest === null
|
|
346
|
-
|| (typeof expected.patch_digest === 'string' && SHA256.test(expected.patch_digest)))
|
|
347
|
-
&& validSourceSnapshot(expected.snapshot)
|
|
348
|
-
&& validLatticeSensorIdentity(expected.sensor_identity)
|
|
349
|
-
&& SHA256.test(expected.query_set_digest);
|
|
350
|
-
const measurement = bundleValid ? bundle.measurement : null;
|
|
351
|
-
const measurementValid = exactRecord(measurement, [
|
|
352
|
-
'schema',
|
|
353
|
-
'base_sha',
|
|
354
|
-
'patch_digest',
|
|
355
|
-
'snapshot',
|
|
356
|
-
'snapshot_digest',
|
|
357
|
-
'sensor_identity',
|
|
358
|
-
'sensor_identity_digest',
|
|
359
|
-
'query_set_digest',
|
|
360
|
-
'raw_evidence_digest',
|
|
361
|
-
])
|
|
362
|
-
&& measurement.schema === 'lattice.rc1.sensor_measurement.v1'
|
|
363
|
-
&& GIT_SHA1.test(measurement.base_sha)
|
|
364
|
-
&& (measurement.patch_digest === null
|
|
365
|
-
|| (typeof measurement.patch_digest === 'string' && SHA256.test(measurement.patch_digest)))
|
|
366
|
-
&& validSourceSnapshot(measurement.snapshot)
|
|
367
|
-
&& SHA256.test(measurement.snapshot_digest)
|
|
368
|
-
&& validLatticeSensorIdentity(measurement.sensor_identity)
|
|
369
|
-
&& SHA256.test(measurement.sensor_identity_digest)
|
|
370
|
-
&& SHA256.test(measurement.query_set_digest)
|
|
371
|
-
&& SHA256.test(measurement.raw_evidence_digest);
|
|
372
|
-
|
|
373
|
-
return verification([
|
|
374
|
-
{ id: 'run_contract', passed: runValid },
|
|
375
|
-
{ id: 'bundle_contract', passed: bundleValid },
|
|
376
|
-
{ id: 'expected_contract', passed: expectedValid },
|
|
377
|
-
{ id: 'measurement_contract', passed: measurementValid },
|
|
378
|
-
{
|
|
379
|
-
id: 'run_bundle_identity',
|
|
380
|
-
passed: runValid && bundleValid
|
|
381
|
-
&& run.condition === bundle.condition
|
|
382
|
-
&& run.run_id === bundle.run_id
|
|
383
|
-
&& run.evidence_bundle_descriptor_digest
|
|
384
|
-
=== safeDigest(createRc1V6EvidenceBundleDescriptor(bundle))
|
|
385
|
-
&& run.measurement_digest === bundle.measurement_digest,
|
|
386
|
-
},
|
|
387
|
-
{
|
|
388
|
-
id: 'measurement_digest',
|
|
389
|
-
passed: bundleValid && measurementValid
|
|
390
|
-
&& bundle.measurement_digest === safeDigest(measurement),
|
|
391
|
-
},
|
|
392
|
-
{
|
|
393
|
-
id: 'snapshot_identity',
|
|
394
|
-
passed: measurementValid && expectedValid
|
|
395
|
-
&& measurement.snapshot_digest === safeDigest(measurement.snapshot)
|
|
396
|
-
&& isDeepStrictEqual(measurement.snapshot, expected.snapshot),
|
|
397
|
-
},
|
|
398
|
-
{
|
|
399
|
-
id: 'sensor_identity',
|
|
400
|
-
passed: measurementValid && expectedValid
|
|
401
|
-
&& measurement.sensor_identity_digest === safeDigest(measurement.sensor_identity)
|
|
402
|
-
&& isDeepStrictEqual(measurement.sensor_identity, expected.sensor_identity),
|
|
403
|
-
},
|
|
404
|
-
{
|
|
405
|
-
id: 'source_projection',
|
|
406
|
-
passed: measurementValid && expectedValid
|
|
407
|
-
&& measurement.base_sha === expected.base_sha
|
|
408
|
-
&& measurement.patch_digest === expected.patch_digest,
|
|
409
|
-
},
|
|
410
|
-
{
|
|
411
|
-
id: 'query_identity',
|
|
412
|
-
passed: measurementValid && expectedValid && bundleValid
|
|
413
|
-
&& measurement.query_set_digest === bundle.query_set_digest
|
|
414
|
-
&& measurement.query_set_digest === expected.query_set_digest,
|
|
415
|
-
},
|
|
416
|
-
{
|
|
417
|
-
id: 'raw_evidence_identity',
|
|
418
|
-
passed: measurementValid && bundleValid
|
|
419
|
-
&& measurement.raw_evidence_digest === bundle.raw.payload_digest,
|
|
420
|
-
},
|
|
421
|
-
]);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
function validPredecessor(value) {
|
|
425
|
-
return exactRecord(value, ['kind', 'ref', 'digest'])
|
|
426
|
-
&& [
|
|
427
|
-
'rejected_plan_archive',
|
|
428
|
-
'phase_decision',
|
|
429
|
-
'accepted_transform',
|
|
430
|
-
'behavior_envelope',
|
|
431
|
-
'evidence_bundle',
|
|
432
|
-
].includes(value.kind)
|
|
433
|
-
&& repoPath(value.ref)
|
|
434
|
-
&& SHA256.test(value.digest);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* v6 plan diffが持つcausal predecessorを、呼出側が実bytesから導出した完全列へ照合する。
|
|
439
|
-
*/
|
|
440
|
-
export function verifyRc1V6PlanPredecessors(options) {
|
|
441
|
-
if (!exactRecord(options, ['planDiff', 'expectedPredecessors'])) {
|
|
442
|
-
return verification([{ id: 'input_contract', passed: false }]);
|
|
443
|
-
}
|
|
444
|
-
const { planDiff, expectedPredecessors } = options;
|
|
445
|
-
const expectedValid = Array.isArray(expectedPredecessors)
|
|
446
|
-
&& expectedPredecessors.length === 8
|
|
447
|
-
&& expectedPredecessors.every(validPredecessor)
|
|
448
|
-
&& new Set(expectedPredecessors.map(({ kind, ref }) => `${kind}:${ref}`)).size === 8
|
|
449
|
-
&& expectedPredecessors.filter(({ kind }) => kind === 'rejected_plan_archive').length === 1
|
|
450
|
-
&& expectedPredecessors.filter(({ kind }) => kind === 'phase_decision').length === 1
|
|
451
|
-
&& expectedPredecessors.filter(({ kind }) => kind === 'accepted_transform').length === 1
|
|
452
|
-
&& expectedPredecessors.filter(({ kind }) => kind === 'behavior_envelope').length === 1
|
|
453
|
-
&& expectedPredecessors.filter(({ kind }) => kind === 'evidence_bundle').length === 4;
|
|
454
|
-
const planValid = planDiff !== null
|
|
455
|
-
&& typeof planDiff === 'object'
|
|
456
|
-
&& !Array.isArray(planDiff)
|
|
457
|
-
&& Object.getPrototypeOf(planDiff) === Object.prototype
|
|
458
|
-
&& planDiff.schema === 'lattice.plan_diff.v3'
|
|
459
|
-
&& !Object.hasOwn(planDiff, 'causal_predecessor')
|
|
460
|
-
&& Array.isArray(planDiff.causal_predecessors)
|
|
461
|
-
&& planDiff.causal_predecessors.length === 8
|
|
462
|
-
&& planDiff.causal_predecessors.every(validPredecessor);
|
|
463
|
-
|
|
464
|
-
return verification([
|
|
465
|
-
{ id: 'expected_predecessor_contract', passed: expectedValid },
|
|
466
|
-
{ id: 'plan_diff_contract', passed: planValid },
|
|
467
|
-
{
|
|
468
|
-
id: 'exact_predecessor_set',
|
|
469
|
-
passed: expectedValid && planValid
|
|
470
|
-
&& isDeepStrictEqual(planDiff.causal_predecessors, expectedPredecessors),
|
|
471
|
-
},
|
|
472
|
-
]);
|
|
473
|
-
}
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
|
|
3
|
+
import { digestArtifact } from './artifact-contracts.mjs';
|
|
4
|
+
import { validateRc1BlackBoxOracle } from './rc1-black-box-oracle.mjs';
|
|
5
|
+
import { validateRc1EvidenceBundle } from './rc1-evidence-bundle.mjs';
|
|
6
|
+
|
|
7
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
8
|
+
const GIT_SHA1 = /^[0-9a-f]{40}$/;
|
|
9
|
+
const IDENTIFIER = /^[A-Za-z][A-Za-z0-9_-]{0,127}$/;
|
|
10
|
+
const RECEIPT_KEYS = [
|
|
11
|
+
'schema',
|
|
12
|
+
'role',
|
|
13
|
+
'base_sha',
|
|
14
|
+
'oracle_digest',
|
|
15
|
+
'entrypoint',
|
|
16
|
+
'export_name',
|
|
17
|
+
'entrypoint_content_digest',
|
|
18
|
+
'surface',
|
|
19
|
+
'surface_digest',
|
|
20
|
+
'observation',
|
|
21
|
+
'outcome',
|
|
22
|
+
'case_results',
|
|
23
|
+
'case_contract_digest',
|
|
24
|
+
'runtime_identity',
|
|
25
|
+
'runtime_identity_digest',
|
|
26
|
+
'receipt_digest',
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
function exactRecord(value, keys) {
|
|
30
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)
|
|
31
|
+
|| Object.getPrototypeOf(value) !== Object.prototype) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const actual = Object.keys(value).sort();
|
|
35
|
+
const expected = [...keys].sort();
|
|
36
|
+
return actual.length === expected.length
|
|
37
|
+
&& actual.every((key, index) => key === expected[index]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function repoPath(value) {
|
|
41
|
+
return typeof value === 'string'
|
|
42
|
+
&& value.length > 0
|
|
43
|
+
&& value.length <= 512
|
|
44
|
+
&& !value.startsWith('/')
|
|
45
|
+
&& !value.includes('\\')
|
|
46
|
+
&& !value.split('/').includes('..');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function verification(checks) {
|
|
50
|
+
const failedConditions = checks.filter(({ passed }) => !passed).map(({ id }) => id);
|
|
51
|
+
return {
|
|
52
|
+
schema: 'lattice.rc1.causal_binding_verification.v1',
|
|
53
|
+
valid: failedConditions.length === 0,
|
|
54
|
+
checks,
|
|
55
|
+
failed_conditions: failedConditions,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function safeDigest(value) {
|
|
60
|
+
try {
|
|
61
|
+
return digestArtifact(value);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function digestWithout(value, field) {
|
|
68
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
69
|
+
const { [field]: ignored, ...preimage } = value;
|
|
70
|
+
return safeDigest(preimage);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function validRuntimeIdentity(value) {
|
|
74
|
+
if (!exactRecord(value, [
|
|
75
|
+
'schema',
|
|
76
|
+
'node_version',
|
|
77
|
+
'exec_argv',
|
|
78
|
+
'environment',
|
|
79
|
+
'executor_source_digest',
|
|
80
|
+
])
|
|
81
|
+
|| value.schema !== 'lattice.rc1.oracle_runtime_identity.v1'
|
|
82
|
+
|| typeof value.node_version !== 'string'
|
|
83
|
+
|| value.node_version.length === 0
|
|
84
|
+
|| value.node_version.length > 128
|
|
85
|
+
|| !Array.isArray(value.exec_argv)
|
|
86
|
+
|| value.exec_argv.length > 32
|
|
87
|
+
|| !value.exec_argv.every((entry) => typeof entry === 'string' && entry.length <= 1024)
|
|
88
|
+
|| value.environment === null
|
|
89
|
+
|| typeof value.environment !== 'object'
|
|
90
|
+
|| Array.isArray(value.environment)
|
|
91
|
+
|| Object.getPrototypeOf(value.environment) !== Object.prototype
|
|
92
|
+
|| Object.keys(value.environment).length > 32
|
|
93
|
+
|| !Object.entries(value.environment).every(([key, entry]) => (
|
|
94
|
+
/^[A-Z][A-Z0-9_]{0,127}$/.test(key)
|
|
95
|
+
&& typeof entry === 'string'
|
|
96
|
+
&& entry.length <= 4096
|
|
97
|
+
))
|
|
98
|
+
|| !SHA256.test(value.executor_source_digest)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return safeDigest(value) !== null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function validCaseResult(value) {
|
|
105
|
+
return exactRecord(value, [
|
|
106
|
+
'id',
|
|
107
|
+
'outcome',
|
|
108
|
+
'observed_kind',
|
|
109
|
+
'expected_digest',
|
|
110
|
+
'observed_digest',
|
|
111
|
+
])
|
|
112
|
+
&& typeof value.id === 'string'
|
|
113
|
+
&& IDENTIFIER.test(value.id)
|
|
114
|
+
&& (value.outcome === 'passed' || value.outcome === 'failed')
|
|
115
|
+
&& (value.observed_kind === 'return' || value.observed_kind === 'throw')
|
|
116
|
+
&& SHA256.test(value.expected_digest)
|
|
117
|
+
&& SHA256.test(value.observed_digest);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function expectedCaseContract(oracle) {
|
|
121
|
+
return oracle.cases.map(({ id, expected }) => ({
|
|
122
|
+
id,
|
|
123
|
+
expected_kind: expected.kind,
|
|
124
|
+
expected_digest: digestArtifact(expected),
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function caseSemanticsValid(caseResults, oracle) {
|
|
129
|
+
if (!Array.isArray(caseResults) || caseResults.length !== oracle.cases.length) return false;
|
|
130
|
+
return caseResults.every((result, index) => {
|
|
131
|
+
if (!validCaseResult(result)) return false;
|
|
132
|
+
const oracleCase = oracle.cases[index];
|
|
133
|
+
const expectedDigest = digestArtifact(oracleCase.expected);
|
|
134
|
+
if (result.id !== oracleCase.id
|
|
135
|
+
|| result.expected_digest !== expectedDigest
|
|
136
|
+
|| (result.outcome === 'passed' && (
|
|
137
|
+
result.observed_kind !== oracleCase.expected.kind
|
|
138
|
+
|| result.observed_digest !== expectedDigest
|
|
139
|
+
))) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function receiptShapeValid(receipt) {
|
|
147
|
+
return exactRecord(receipt, RECEIPT_KEYS)
|
|
148
|
+
&& receipt.schema === 'lattice.rc1.black_box_behavior_receipt.v4'
|
|
149
|
+
&& (receipt.role === 'pre_transform' || receipt.role === 'post_transform')
|
|
150
|
+
&& GIT_SHA1.test(receipt.base_sha)
|
|
151
|
+
&& SHA256.test(receipt.oracle_digest)
|
|
152
|
+
&& repoPath(receipt.entrypoint)
|
|
153
|
+
&& typeof receipt.export_name === 'string'
|
|
154
|
+
&& IDENTIFIER.test(receipt.export_name)
|
|
155
|
+
&& SHA256.test(receipt.entrypoint_content_digest)
|
|
156
|
+
&& SHA256.test(receipt.surface_digest)
|
|
157
|
+
&& (receipt.outcome === 'passed' || receipt.outcome === 'failed')
|
|
158
|
+
&& SHA256.test(receipt.case_contract_digest)
|
|
159
|
+
&& SHA256.test(receipt.runtime_identity_digest)
|
|
160
|
+
&& SHA256.test(receipt.receipt_digest);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 保存oracleと独立runtime期待値から、v6 behavior receiptのcase意味論を再計算する。
|
|
165
|
+
*/
|
|
166
|
+
export function verifyRc1V6BehaviorReceipt(options) {
|
|
167
|
+
if (!exactRecord(options, [
|
|
168
|
+
'receipt',
|
|
169
|
+
'oracle',
|
|
170
|
+
'expectedRole',
|
|
171
|
+
'expectedRuntimeIdentity',
|
|
172
|
+
])) {
|
|
173
|
+
return verification([{ id: 'input_contract', passed: false }]);
|
|
174
|
+
}
|
|
175
|
+
const {
|
|
176
|
+
receipt,
|
|
177
|
+
oracle,
|
|
178
|
+
expectedRole,
|
|
179
|
+
expectedRuntimeIdentity,
|
|
180
|
+
} = options;
|
|
181
|
+
const oracleValid = validateRc1BlackBoxOracle(oracle);
|
|
182
|
+
const runtimeValid = validRuntimeIdentity(expectedRuntimeIdentity);
|
|
183
|
+
const shapeValid = receiptShapeValid(receipt);
|
|
184
|
+
const caseContract = oracleValid ? expectedCaseContract(oracle) : null;
|
|
185
|
+
const caseSemantics = shapeValid && oracleValid
|
|
186
|
+
? caseSemanticsValid(receipt.case_results, oracle)
|
|
187
|
+
: false;
|
|
188
|
+
const recomputedOutcome = caseSemantics
|
|
189
|
+
? (receipt.case_results.every(({ outcome }) => outcome === 'passed') ? 'passed' : 'failed')
|
|
190
|
+
: null;
|
|
191
|
+
|
|
192
|
+
return verification([
|
|
193
|
+
{ id: 'oracle_contract', passed: oracleValid },
|
|
194
|
+
{ id: 'runtime_contract', passed: runtimeValid },
|
|
195
|
+
{ id: 'receipt_shape', passed: shapeValid },
|
|
196
|
+
{
|
|
197
|
+
id: 'receipt_role',
|
|
198
|
+
passed: shapeValid
|
|
199
|
+
&& (expectedRole === 'pre_transform' || expectedRole === 'post_transform')
|
|
200
|
+
&& receipt.role === expectedRole,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
id: 'oracle_identity',
|
|
204
|
+
passed: shapeValid && oracleValid
|
|
205
|
+
&& receipt.oracle_digest === safeDigest(oracle)
|
|
206
|
+
&& receipt.entrypoint === oracle.entrypoint
|
|
207
|
+
&& receipt.export_name === oracle.export_name,
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
id: 'case_contract',
|
|
211
|
+
passed: shapeValid && caseContract !== null
|
|
212
|
+
&& receipt.case_contract_digest === safeDigest(caseContract),
|
|
213
|
+
},
|
|
214
|
+
{ id: 'case_semantics', passed: caseSemantics },
|
|
215
|
+
{
|
|
216
|
+
id: 'overall_outcome',
|
|
217
|
+
passed: recomputedOutcome !== null && receipt.outcome === recomputedOutcome,
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
id: 'runtime_identity',
|
|
221
|
+
passed: shapeValid && runtimeValid
|
|
222
|
+
&& validRuntimeIdentity(receipt.runtime_identity)
|
|
223
|
+
&& receipt.runtime_identity_digest === safeDigest(receipt.runtime_identity)
|
|
224
|
+
&& isDeepStrictEqual(receipt.runtime_identity, expectedRuntimeIdentity),
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
id: 'surface_identity',
|
|
228
|
+
passed: shapeValid
|
|
229
|
+
&& receipt.surface_digest === safeDigest(receipt.surface)
|
|
230
|
+
&& exactRecord(receipt.observation, ['before_surface_digest', 'after_surface_digest'])
|
|
231
|
+
&& receipt.observation.before_surface_digest === receipt.surface_digest
|
|
232
|
+
&& receipt.observation.after_surface_digest === receipt.surface_digest,
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
id: 'receipt_digest',
|
|
236
|
+
passed: shapeValid && receipt.receipt_digest === digestWithout(receipt, 'receipt_digest'),
|
|
237
|
+
},
|
|
238
|
+
]);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function validSourceSnapshot(value) {
|
|
242
|
+
if (!exactRecord(value, ['schema', 'files'])
|
|
243
|
+
|| value.schema !== 'lattice.rc1.source_snapshot.v1'
|
|
244
|
+
|| !Array.isArray(value.files)
|
|
245
|
+
|| value.files.length === 0
|
|
246
|
+
|| value.files.length > 256) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
const paths = new Set();
|
|
250
|
+
for (const entry of value.files) {
|
|
251
|
+
if (!exactRecord(entry, ['path', 'state', 'content_digest'])
|
|
252
|
+
|| !repoPath(entry.path)
|
|
253
|
+
|| paths.has(entry.path)
|
|
254
|
+
|| !['file', 'present', 'absent'].includes(entry.state)
|
|
255
|
+
|| (entry.state === 'absent'
|
|
256
|
+
? entry.content_digest !== null
|
|
257
|
+
: typeof entry.content_digest !== 'string' || !SHA256.test(entry.content_digest))) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
paths.add(entry.path);
|
|
261
|
+
}
|
|
262
|
+
return value.files.every(({ path }, index) => index === 0 || value.files[index - 1].path < path);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function validLatticeSensorIdentity(value) {
|
|
266
|
+
return exactRecord(value, [
|
|
267
|
+
'schema',
|
|
268
|
+
'version',
|
|
269
|
+
'executable_ref',
|
|
270
|
+
'executable_digest',
|
|
271
|
+
])
|
|
272
|
+
&& value.schema === 'lattice.rc1.sensor_identity.v1'
|
|
273
|
+
&& typeof value.version === 'string'
|
|
274
|
+
&& /^\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.-]+)?$/.test(value.version)
|
|
275
|
+
// `sensor` is accepted only as an immutable pre-cutover artifact value.
|
|
276
|
+
// New captures always emit `lattice-sensor`.
|
|
277
|
+
&& (value.executable_ref === 'lattice-sensor' || value.executable_ref === 'sensor')
|
|
278
|
+
&& SHA256.test(value.executable_digest);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function baseEvidenceBundle(bundle) {
|
|
282
|
+
if (bundle === null || typeof bundle !== 'object' || Array.isArray(bundle)) return null;
|
|
283
|
+
const { measurement: ignoredMeasurement, measurement_digest: ignoredDigest, ...base } = bundle;
|
|
284
|
+
return { ...base, schema: 'lattice.rc1.evidence_bundle.v1' };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function createRc1V6EvidenceBundleDescriptor(bundle) {
|
|
288
|
+
return {
|
|
289
|
+
schema: 'lattice.rc1.evidence_bundle_descriptor.v1',
|
|
290
|
+
condition: bundle.condition,
|
|
291
|
+
run_id: bundle.run_id,
|
|
292
|
+
query_set_digest: bundle.query_set_digest,
|
|
293
|
+
raw_digest: bundle.raw.payload_digest,
|
|
294
|
+
diagnostic_payload_digest: bundle.diagnostic.payload_digest,
|
|
295
|
+
sanitization_manifest_digest: bundle.diagnostic.sanitization_manifest_digest,
|
|
296
|
+
portable_digest: bundle.portable.aggregate_digest,
|
|
297
|
+
measurement_digest: bundle.measurement_digest,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* v6 LatticeSensor runを、独立したsource/tool期待値と保存raw evidenceへcross-bindする。
|
|
303
|
+
*/
|
|
304
|
+
export function verifyRc1V6RunEvidence(options) {
|
|
305
|
+
if (!exactRecord(options, ['run', 'bundle', 'expected'])) {
|
|
306
|
+
return verification([{ id: 'input_contract', passed: false }]);
|
|
307
|
+
}
|
|
308
|
+
const { run, bundle, expected } = options;
|
|
309
|
+
const runValid = exactRecord(run, [
|
|
310
|
+
'schema',
|
|
311
|
+
'condition',
|
|
312
|
+
'run_id',
|
|
313
|
+
'evidence_bundle_descriptor_digest',
|
|
314
|
+
'measurement_digest',
|
|
315
|
+
])
|
|
316
|
+
&& run.schema === 'lattice.rc1.condition_run.v2'
|
|
317
|
+
&& (run.condition === 'control' || run.condition === 'treatment')
|
|
318
|
+
&& typeof run.run_id === 'string'
|
|
319
|
+
&& IDENTIFIER.test(run.run_id)
|
|
320
|
+
&& SHA256.test(run.evidence_bundle_descriptor_digest)
|
|
321
|
+
&& SHA256.test(run.measurement_digest);
|
|
322
|
+
const bundleValid = exactRecord(bundle, [
|
|
323
|
+
'schema',
|
|
324
|
+
'condition',
|
|
325
|
+
'run_id',
|
|
326
|
+
'query_set',
|
|
327
|
+
'query_set_digest',
|
|
328
|
+
'raw',
|
|
329
|
+
'diagnostic',
|
|
330
|
+
'portable',
|
|
331
|
+
'component_digests',
|
|
332
|
+
'measurement',
|
|
333
|
+
'measurement_digest',
|
|
334
|
+
])
|
|
335
|
+
&& bundle.schema === 'lattice.rc1.evidence_bundle.v2'
|
|
336
|
+
&& validateRc1EvidenceBundle(baseEvidenceBundle(bundle));
|
|
337
|
+
const expectedValid = exactRecord(expected, [
|
|
338
|
+
'base_sha',
|
|
339
|
+
'patch_digest',
|
|
340
|
+
'snapshot',
|
|
341
|
+
'sensor_identity',
|
|
342
|
+
'query_set_digest',
|
|
343
|
+
])
|
|
344
|
+
&& GIT_SHA1.test(expected.base_sha)
|
|
345
|
+
&& (expected.patch_digest === null
|
|
346
|
+
|| (typeof expected.patch_digest === 'string' && SHA256.test(expected.patch_digest)))
|
|
347
|
+
&& validSourceSnapshot(expected.snapshot)
|
|
348
|
+
&& validLatticeSensorIdentity(expected.sensor_identity)
|
|
349
|
+
&& SHA256.test(expected.query_set_digest);
|
|
350
|
+
const measurement = bundleValid ? bundle.measurement : null;
|
|
351
|
+
const measurementValid = exactRecord(measurement, [
|
|
352
|
+
'schema',
|
|
353
|
+
'base_sha',
|
|
354
|
+
'patch_digest',
|
|
355
|
+
'snapshot',
|
|
356
|
+
'snapshot_digest',
|
|
357
|
+
'sensor_identity',
|
|
358
|
+
'sensor_identity_digest',
|
|
359
|
+
'query_set_digest',
|
|
360
|
+
'raw_evidence_digest',
|
|
361
|
+
])
|
|
362
|
+
&& measurement.schema === 'lattice.rc1.sensor_measurement.v1'
|
|
363
|
+
&& GIT_SHA1.test(measurement.base_sha)
|
|
364
|
+
&& (measurement.patch_digest === null
|
|
365
|
+
|| (typeof measurement.patch_digest === 'string' && SHA256.test(measurement.patch_digest)))
|
|
366
|
+
&& validSourceSnapshot(measurement.snapshot)
|
|
367
|
+
&& SHA256.test(measurement.snapshot_digest)
|
|
368
|
+
&& validLatticeSensorIdentity(measurement.sensor_identity)
|
|
369
|
+
&& SHA256.test(measurement.sensor_identity_digest)
|
|
370
|
+
&& SHA256.test(measurement.query_set_digest)
|
|
371
|
+
&& SHA256.test(measurement.raw_evidence_digest);
|
|
372
|
+
|
|
373
|
+
return verification([
|
|
374
|
+
{ id: 'run_contract', passed: runValid },
|
|
375
|
+
{ id: 'bundle_contract', passed: bundleValid },
|
|
376
|
+
{ id: 'expected_contract', passed: expectedValid },
|
|
377
|
+
{ id: 'measurement_contract', passed: measurementValid },
|
|
378
|
+
{
|
|
379
|
+
id: 'run_bundle_identity',
|
|
380
|
+
passed: runValid && bundleValid
|
|
381
|
+
&& run.condition === bundle.condition
|
|
382
|
+
&& run.run_id === bundle.run_id
|
|
383
|
+
&& run.evidence_bundle_descriptor_digest
|
|
384
|
+
=== safeDigest(createRc1V6EvidenceBundleDescriptor(bundle))
|
|
385
|
+
&& run.measurement_digest === bundle.measurement_digest,
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
id: 'measurement_digest',
|
|
389
|
+
passed: bundleValid && measurementValid
|
|
390
|
+
&& bundle.measurement_digest === safeDigest(measurement),
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
id: 'snapshot_identity',
|
|
394
|
+
passed: measurementValid && expectedValid
|
|
395
|
+
&& measurement.snapshot_digest === safeDigest(measurement.snapshot)
|
|
396
|
+
&& isDeepStrictEqual(measurement.snapshot, expected.snapshot),
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
id: 'sensor_identity',
|
|
400
|
+
passed: measurementValid && expectedValid
|
|
401
|
+
&& measurement.sensor_identity_digest === safeDigest(measurement.sensor_identity)
|
|
402
|
+
&& isDeepStrictEqual(measurement.sensor_identity, expected.sensor_identity),
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
id: 'source_projection',
|
|
406
|
+
passed: measurementValid && expectedValid
|
|
407
|
+
&& measurement.base_sha === expected.base_sha
|
|
408
|
+
&& measurement.patch_digest === expected.patch_digest,
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
id: 'query_identity',
|
|
412
|
+
passed: measurementValid && expectedValid && bundleValid
|
|
413
|
+
&& measurement.query_set_digest === bundle.query_set_digest
|
|
414
|
+
&& measurement.query_set_digest === expected.query_set_digest,
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
id: 'raw_evidence_identity',
|
|
418
|
+
passed: measurementValid && bundleValid
|
|
419
|
+
&& measurement.raw_evidence_digest === bundle.raw.payload_digest,
|
|
420
|
+
},
|
|
421
|
+
]);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function validPredecessor(value) {
|
|
425
|
+
return exactRecord(value, ['kind', 'ref', 'digest'])
|
|
426
|
+
&& [
|
|
427
|
+
'rejected_plan_archive',
|
|
428
|
+
'phase_decision',
|
|
429
|
+
'accepted_transform',
|
|
430
|
+
'behavior_envelope',
|
|
431
|
+
'evidence_bundle',
|
|
432
|
+
].includes(value.kind)
|
|
433
|
+
&& repoPath(value.ref)
|
|
434
|
+
&& SHA256.test(value.digest);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* v6 plan diffが持つcausal predecessorを、呼出側が実bytesから導出した完全列へ照合する。
|
|
439
|
+
*/
|
|
440
|
+
export function verifyRc1V6PlanPredecessors(options) {
|
|
441
|
+
if (!exactRecord(options, ['planDiff', 'expectedPredecessors'])) {
|
|
442
|
+
return verification([{ id: 'input_contract', passed: false }]);
|
|
443
|
+
}
|
|
444
|
+
const { planDiff, expectedPredecessors } = options;
|
|
445
|
+
const expectedValid = Array.isArray(expectedPredecessors)
|
|
446
|
+
&& expectedPredecessors.length === 8
|
|
447
|
+
&& expectedPredecessors.every(validPredecessor)
|
|
448
|
+
&& new Set(expectedPredecessors.map(({ kind, ref }) => `${kind}:${ref}`)).size === 8
|
|
449
|
+
&& expectedPredecessors.filter(({ kind }) => kind === 'rejected_plan_archive').length === 1
|
|
450
|
+
&& expectedPredecessors.filter(({ kind }) => kind === 'phase_decision').length === 1
|
|
451
|
+
&& expectedPredecessors.filter(({ kind }) => kind === 'accepted_transform').length === 1
|
|
452
|
+
&& expectedPredecessors.filter(({ kind }) => kind === 'behavior_envelope').length === 1
|
|
453
|
+
&& expectedPredecessors.filter(({ kind }) => kind === 'evidence_bundle').length === 4;
|
|
454
|
+
const planValid = planDiff !== null
|
|
455
|
+
&& typeof planDiff === 'object'
|
|
456
|
+
&& !Array.isArray(planDiff)
|
|
457
|
+
&& Object.getPrototypeOf(planDiff) === Object.prototype
|
|
458
|
+
&& planDiff.schema === 'lattice.plan_diff.v3'
|
|
459
|
+
&& !Object.hasOwn(planDiff, 'causal_predecessor')
|
|
460
|
+
&& Array.isArray(planDiff.causal_predecessors)
|
|
461
|
+
&& planDiff.causal_predecessors.length === 8
|
|
462
|
+
&& planDiff.causal_predecessors.every(validPredecessor);
|
|
463
|
+
|
|
464
|
+
return verification([
|
|
465
|
+
{ id: 'expected_predecessor_contract', passed: expectedValid },
|
|
466
|
+
{ id: 'plan_diff_contract', passed: planValid },
|
|
467
|
+
{
|
|
468
|
+
id: 'exact_predecessor_set',
|
|
469
|
+
passed: expectedValid && planValid
|
|
470
|
+
&& isDeepStrictEqual(planDiff.causal_predecessors, expectedPredecessors),
|
|
471
|
+
},
|
|
472
|
+
]);
|
|
473
|
+
}
|