@quolu/lattice 0.52.3 → 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 +3 -3
- 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 +8 -8
- 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 +42 -21
- package/src/treatment-compiler.mjs +728 -728
- package/src/treatment-runner.mjs +656 -656
- package/src/witness-scaffold.mjs +180 -180
|
@@ -1,273 +1,273 @@
|
|
|
1
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
digestArtifact,
|
|
5
|
-
validateTransformArtifact,
|
|
6
|
-
} from './artifact-contracts.mjs';
|
|
7
|
-
import { verifyRc1V6BehaviorReceipt } from './rc1-v6-causal-binding.mjs';
|
|
8
|
-
import { sourceSnapshotFromRc1BehaviorSurface } from './rc1-v6-measurement.mjs';
|
|
9
|
-
|
|
10
|
-
const SHA256 = /^[0-9a-f]{64}$/;
|
|
11
|
-
const GIT_SHA1 = /^[0-9a-f]{40}$/;
|
|
12
|
-
const ENVELOPE_KEYS = [
|
|
13
|
-
'schema',
|
|
14
|
-
'base_sha',
|
|
15
|
-
'oracle_digest',
|
|
16
|
-
'runtime_identity_digest',
|
|
17
|
-
'case_contract_digest',
|
|
18
|
-
'pre_receipt_digest',
|
|
19
|
-
'post_receipt_digest',
|
|
20
|
-
'pre_surface_digest',
|
|
21
|
-
'post_surface_digest',
|
|
22
|
-
'transform_artifact_digest',
|
|
23
|
-
'patch_digest',
|
|
24
|
-
'output_snapshot_digest',
|
|
25
|
-
'envelope_digest',
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
function exactRecord(value, keys) {
|
|
29
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)
|
|
30
|
-
|| Object.getPrototypeOf(value) !== Object.prototype) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
const actual = Object.keys(value).sort();
|
|
34
|
-
const expected = [...keys].sort();
|
|
35
|
-
return actual.length === expected.length
|
|
36
|
-
&& actual.every((key, index) => key === expected[index]);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function repoPath(value) {
|
|
40
|
-
return typeof value === 'string'
|
|
41
|
-
&& value.length > 0
|
|
42
|
-
&& value.length <= 512
|
|
43
|
-
&& !value.startsWith('/')
|
|
44
|
-
&& !value.includes('\\')
|
|
45
|
-
&& !value.split('/').includes('..');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function verification(checks) {
|
|
49
|
-
const failedConditions = checks.filter(({ passed }) => !passed).map(({ id }) => id);
|
|
50
|
-
return {
|
|
51
|
-
schema: 'lattice.rc1.behavior_evidence_verification.v1',
|
|
52
|
-
valid: failedConditions.length === 0,
|
|
53
|
-
checks,
|
|
54
|
-
failed_conditions: failedConditions,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function digestWithout(value, field) {
|
|
59
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
60
|
-
try {
|
|
61
|
-
const { [field]: ignored, ...preimage } = value;
|
|
62
|
-
return digestArtifact(preimage);
|
|
63
|
-
} catch {
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function validSurface(value) {
|
|
69
|
-
if (!exactRecord(value, ['schema', 'files'])
|
|
70
|
-
|| value.schema !== 'lattice.rc1.behavior_surface_snapshot.v1'
|
|
71
|
-
|| !Array.isArray(value.files)
|
|
72
|
-
|| value.files.length === 0
|
|
73
|
-
|| value.files.length > 128) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
return value.files.every((file, index) => (
|
|
77
|
-
exactRecord(file, ['path', 'state', 'content_digest'])
|
|
78
|
-
&& repoPath(file.path)
|
|
79
|
-
&& (index === 0 || value.files[index - 1].path < file.path)
|
|
80
|
-
&& (file.state === 'present' || file.state === 'absent')
|
|
81
|
-
&& (file.state === 'present'
|
|
82
|
-
? typeof file.content_digest === 'string' && SHA256.test(file.content_digest)
|
|
83
|
-
: file.content_digest === null)
|
|
84
|
-
));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function validAcceptedTransform(value) {
|
|
88
|
-
return validateTransformArtifact(value)
|
|
89
|
-
&& value.status === 'accepted'
|
|
90
|
-
&& GIT_SHA1.test(value.source?.base_sha)
|
|
91
|
-
&& SHA256.test(value.source?.code_snapshot_digest)
|
|
92
|
-
&& SHA256.test(value.patch?.digest)
|
|
93
|
-
&& Array.isArray(value.scope?.allowed_paths)
|
|
94
|
-
&& value.scope.allowed_paths.length > 0
|
|
95
|
-
&& Array.isArray(value.output?.files)
|
|
96
|
-
&& value.output.files.length > 0
|
|
97
|
-
&& SHA256.test(value.output?.snapshot_digest)
|
|
98
|
-
&& value.output.snapshot_digest === digestArtifact({ files: value.output.files });
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function surfacePaths(surface) {
|
|
102
|
-
return validSurface(surface) ? surface.files.map(({ path }) => path) : [];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function outputProjection(surface) {
|
|
106
|
-
if (!validSurface(surface) || surface.files.some(({ state }) => state !== 'present')) return null;
|
|
107
|
-
return {
|
|
108
|
-
files: surface.files.map(({ path, content_digest: contentDigest }) => ({
|
|
109
|
-
path,
|
|
110
|
-
content_digest: contentDigest,
|
|
111
|
-
})),
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function envelopeValid(value) {
|
|
116
|
-
return exactRecord(value, ENVELOPE_KEYS)
|
|
117
|
-
&& value.schema === 'lattice.rc1.behavior_evidence_envelope.v2'
|
|
118
|
-
&& GIT_SHA1.test(value.base_sha)
|
|
119
|
-
&& [
|
|
120
|
-
value.oracle_digest,
|
|
121
|
-
value.runtime_identity_digest,
|
|
122
|
-
value.case_contract_digest,
|
|
123
|
-
value.pre_receipt_digest,
|
|
124
|
-
value.post_receipt_digest,
|
|
125
|
-
value.pre_surface_digest,
|
|
126
|
-
value.post_surface_digest,
|
|
127
|
-
value.transform_artifact_digest,
|
|
128
|
-
value.patch_digest,
|
|
129
|
-
value.output_snapshot_digest,
|
|
130
|
-
value.envelope_digest,
|
|
131
|
-
].every((entry) => typeof entry === 'string' && SHA256.test(entry))
|
|
132
|
-
&& value.envelope_digest === digestWithout(value, 'envelope_digest');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function behaviorChecks(options) {
|
|
136
|
-
const inputValid = exactRecord(options, [
|
|
137
|
-
'oracle',
|
|
138
|
-
'runtimeIdentity',
|
|
139
|
-
'preReceipt',
|
|
140
|
-
'postReceipt',
|
|
141
|
-
'transformArtifact',
|
|
142
|
-
'patchDigest',
|
|
143
|
-
'envelope',
|
|
144
|
-
]);
|
|
145
|
-
const oracle = inputValid ? options.oracle : null;
|
|
146
|
-
const runtimeIdentity = inputValid ? options.runtimeIdentity : null;
|
|
147
|
-
const pre = inputValid ? options.preReceipt : null;
|
|
148
|
-
const post = inputValid ? options.postReceipt : null;
|
|
149
|
-
const transform = inputValid ? options.transformArtifact : null;
|
|
150
|
-
const patchDigest = inputValid ? options.patchDigest : null;
|
|
151
|
-
const envelope = inputValid ? options.envelope : null;
|
|
152
|
-
const preVerification = inputValid ? verifyRc1V6BehaviorReceipt({
|
|
153
|
-
receipt: pre,
|
|
154
|
-
oracle,
|
|
155
|
-
expectedRole: 'pre_transform',
|
|
156
|
-
expectedRuntimeIdentity: runtimeIdentity,
|
|
157
|
-
}) : { valid: false };
|
|
158
|
-
const postVerification = inputValid ? verifyRc1V6BehaviorReceipt({
|
|
159
|
-
receipt: post,
|
|
160
|
-
oracle,
|
|
161
|
-
expectedRole: 'post_transform',
|
|
162
|
-
expectedRuntimeIdentity: runtimeIdentity,
|
|
163
|
-
}) : { valid: false };
|
|
164
|
-
const receiptsValid = preVerification.valid && postVerification.valid;
|
|
165
|
-
const transformValid = inputValid && validAcceptedTransform(transform);
|
|
166
|
-
const validEnvelope = inputValid && envelopeValid(envelope);
|
|
167
|
-
const preSurfaceValid = receiptsValid && validSurface(pre.surface);
|
|
168
|
-
const postSurfaceValid = receiptsValid && validSurface(post.surface);
|
|
169
|
-
const prePaths = preSurfaceValid ? surfacePaths(pre.surface) : [];
|
|
170
|
-
const postPaths = postSurfaceValid ? surfacePaths(post.surface) : [];
|
|
171
|
-
const projection = postSurfaceValid ? outputProjection(post.surface) : null;
|
|
172
|
-
const outputBound = transformValid
|
|
173
|
-
&& projection !== null
|
|
174
|
-
&& isDeepStrictEqual(prePaths, postPaths)
|
|
175
|
-
&& isDeepStrictEqual(postPaths, transform.scope.allowed_paths)
|
|
176
|
-
&& digestArtifact(projection) === transform.output.snapshot_digest
|
|
177
|
-
&& digestArtifact(projection.files) === digestArtifact(transform.output.files);
|
|
178
|
-
|
|
179
|
-
return [
|
|
180
|
-
{ id: 'input_contract', passed: inputValid },
|
|
181
|
-
{ id: 'pre_receipt', passed: preVerification.valid },
|
|
182
|
-
{ id: 'post_receipt', passed: postVerification.valid },
|
|
183
|
-
{ id: 'envelope_contract', passed: validEnvelope },
|
|
184
|
-
{ id: 'transform_contract', passed: transformValid },
|
|
185
|
-
{
|
|
186
|
-
id: 'base_binding',
|
|
187
|
-
passed: receiptsValid && transformValid
|
|
188
|
-
&& pre.base_sha === post.base_sha
|
|
189
|
-
&& pre.base_sha === transform.source.base_sha,
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
id: 'oracle_runtime_binding',
|
|
193
|
-
passed: receiptsValid
|
|
194
|
-
&& pre.oracle_digest === post.oracle_digest
|
|
195
|
-
&& pre.runtime_identity_digest === post.runtime_identity_digest
|
|
196
|
-
&& pre.case_contract_digest === post.case_contract_digest,
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
id: 'behavior_outcome',
|
|
200
|
-
passed: receiptsValid && pre.outcome === 'passed' && post.outcome === 'passed',
|
|
201
|
-
},
|
|
202
|
-
{
|
|
203
|
-
id: 'pre_source_binding',
|
|
204
|
-
passed: preSurfaceValid && transformValid
|
|
205
|
-
&& digestArtifact(sourceSnapshotFromRc1BehaviorSurface(pre.surface))
|
|
206
|
-
=== transform.source.code_snapshot_digest,
|
|
207
|
-
},
|
|
208
|
-
{ id: 'post_output_binding', passed: outputBound },
|
|
209
|
-
{
|
|
210
|
-
id: 'patch_binding',
|
|
211
|
-
passed: transformValid
|
|
212
|
-
&& typeof patchDigest === 'string'
|
|
213
|
-
&& SHA256.test(patchDigest)
|
|
214
|
-
&& patchDigest === transform.patch.digest,
|
|
215
|
-
},
|
|
216
|
-
{
|
|
217
|
-
id: 'envelope_binding',
|
|
218
|
-
passed: receiptsValid && transformValid && validEnvelope && outputBound
|
|
219
|
-
&& envelope.base_sha === pre.base_sha
|
|
220
|
-
&& envelope.oracle_digest === pre.oracle_digest
|
|
221
|
-
&& envelope.runtime_identity_digest === pre.runtime_identity_digest
|
|
222
|
-
&& envelope.case_contract_digest === pre.case_contract_digest
|
|
223
|
-
&& envelope.pre_receipt_digest === pre.receipt_digest
|
|
224
|
-
&& envelope.post_receipt_digest === post.receipt_digest
|
|
225
|
-
&& envelope.pre_surface_digest === pre.surface_digest
|
|
226
|
-
&& envelope.post_surface_digest === post.surface_digest
|
|
227
|
-
&& envelope.transform_artifact_digest === digestArtifact(transform)
|
|
228
|
-
&& envelope.patch_digest === patchDigest
|
|
229
|
-
&& envelope.output_snapshot_digest === transform.output.snapshot_digest,
|
|
230
|
-
},
|
|
231
|
-
];
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/** v6 full receiptsをsaved oracle、runtime、accepted transformへcross-bindする。 */
|
|
235
|
-
export function compileRc1V6BehaviorEvidence(options = {}) {
|
|
236
|
-
if (!exactRecord(options, [
|
|
237
|
-
'oracle',
|
|
238
|
-
'runtimeIdentity',
|
|
239
|
-
'preReceipt',
|
|
240
|
-
'postReceipt',
|
|
241
|
-
'transformArtifact',
|
|
242
|
-
'patchDigest',
|
|
243
|
-
])) {
|
|
244
|
-
throw new TypeError('RC1 v6 behavior evidence契約違反: input shapeが不正');
|
|
245
|
-
}
|
|
246
|
-
const preimage = {
|
|
247
|
-
schema: 'lattice.rc1.behavior_evidence_envelope.v2',
|
|
248
|
-
base_sha: options.preReceipt?.base_sha,
|
|
249
|
-
oracle_digest: options.preReceipt?.oracle_digest,
|
|
250
|
-
runtime_identity_digest: options.preReceipt?.runtime_identity_digest,
|
|
251
|
-
case_contract_digest: options.preReceipt?.case_contract_digest,
|
|
252
|
-
pre_receipt_digest: options.preReceipt?.receipt_digest,
|
|
253
|
-
post_receipt_digest: options.postReceipt?.receipt_digest,
|
|
254
|
-
pre_surface_digest: options.preReceipt?.surface_digest,
|
|
255
|
-
post_surface_digest: options.postReceipt?.surface_digest,
|
|
256
|
-
transform_artifact_digest: digestArtifact(options.transformArtifact),
|
|
257
|
-
patch_digest: options.patchDigest,
|
|
258
|
-
output_snapshot_digest: options.transformArtifact?.output?.snapshot_digest,
|
|
259
|
-
};
|
|
260
|
-
const envelope = { ...preimage, envelope_digest: digestArtifact(preimage) };
|
|
261
|
-
const result = verifyRc1V6BehaviorEvidence({ ...options, envelope });
|
|
262
|
-
if (!result.valid) {
|
|
263
|
-
throw new TypeError(
|
|
264
|
-
`RC1 v6 behavior evidence契約違反: cross-binding failed: ${result.failed_conditions.join(', ')}`,
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
return envelope;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/** 保存artifactだけからv6 behavior evidenceの全cross-bindingを再計算する。 */
|
|
271
|
-
export function verifyRc1V6BehaviorEvidence(options = {}) {
|
|
272
|
-
return verification(behaviorChecks(options));
|
|
273
|
-
}
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
digestArtifact,
|
|
5
|
+
validateTransformArtifact,
|
|
6
|
+
} from './artifact-contracts.mjs';
|
|
7
|
+
import { verifyRc1V6BehaviorReceipt } from './rc1-v6-causal-binding.mjs';
|
|
8
|
+
import { sourceSnapshotFromRc1BehaviorSurface } from './rc1-v6-measurement.mjs';
|
|
9
|
+
|
|
10
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
11
|
+
const GIT_SHA1 = /^[0-9a-f]{40}$/;
|
|
12
|
+
const ENVELOPE_KEYS = [
|
|
13
|
+
'schema',
|
|
14
|
+
'base_sha',
|
|
15
|
+
'oracle_digest',
|
|
16
|
+
'runtime_identity_digest',
|
|
17
|
+
'case_contract_digest',
|
|
18
|
+
'pre_receipt_digest',
|
|
19
|
+
'post_receipt_digest',
|
|
20
|
+
'pre_surface_digest',
|
|
21
|
+
'post_surface_digest',
|
|
22
|
+
'transform_artifact_digest',
|
|
23
|
+
'patch_digest',
|
|
24
|
+
'output_snapshot_digest',
|
|
25
|
+
'envelope_digest',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
function exactRecord(value, keys) {
|
|
29
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)
|
|
30
|
+
|| Object.getPrototypeOf(value) !== Object.prototype) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const actual = Object.keys(value).sort();
|
|
34
|
+
const expected = [...keys].sort();
|
|
35
|
+
return actual.length === expected.length
|
|
36
|
+
&& actual.every((key, index) => key === expected[index]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function repoPath(value) {
|
|
40
|
+
return typeof value === 'string'
|
|
41
|
+
&& value.length > 0
|
|
42
|
+
&& value.length <= 512
|
|
43
|
+
&& !value.startsWith('/')
|
|
44
|
+
&& !value.includes('\\')
|
|
45
|
+
&& !value.split('/').includes('..');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function verification(checks) {
|
|
49
|
+
const failedConditions = checks.filter(({ passed }) => !passed).map(({ id }) => id);
|
|
50
|
+
return {
|
|
51
|
+
schema: 'lattice.rc1.behavior_evidence_verification.v1',
|
|
52
|
+
valid: failedConditions.length === 0,
|
|
53
|
+
checks,
|
|
54
|
+
failed_conditions: failedConditions,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function digestWithout(value, field) {
|
|
59
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
60
|
+
try {
|
|
61
|
+
const { [field]: ignored, ...preimage } = value;
|
|
62
|
+
return digestArtifact(preimage);
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function validSurface(value) {
|
|
69
|
+
if (!exactRecord(value, ['schema', 'files'])
|
|
70
|
+
|| value.schema !== 'lattice.rc1.behavior_surface_snapshot.v1'
|
|
71
|
+
|| !Array.isArray(value.files)
|
|
72
|
+
|| value.files.length === 0
|
|
73
|
+
|| value.files.length > 128) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return value.files.every((file, index) => (
|
|
77
|
+
exactRecord(file, ['path', 'state', 'content_digest'])
|
|
78
|
+
&& repoPath(file.path)
|
|
79
|
+
&& (index === 0 || value.files[index - 1].path < file.path)
|
|
80
|
+
&& (file.state === 'present' || file.state === 'absent')
|
|
81
|
+
&& (file.state === 'present'
|
|
82
|
+
? typeof file.content_digest === 'string' && SHA256.test(file.content_digest)
|
|
83
|
+
: file.content_digest === null)
|
|
84
|
+
));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function validAcceptedTransform(value) {
|
|
88
|
+
return validateTransformArtifact(value)
|
|
89
|
+
&& value.status === 'accepted'
|
|
90
|
+
&& GIT_SHA1.test(value.source?.base_sha)
|
|
91
|
+
&& SHA256.test(value.source?.code_snapshot_digest)
|
|
92
|
+
&& SHA256.test(value.patch?.digest)
|
|
93
|
+
&& Array.isArray(value.scope?.allowed_paths)
|
|
94
|
+
&& value.scope.allowed_paths.length > 0
|
|
95
|
+
&& Array.isArray(value.output?.files)
|
|
96
|
+
&& value.output.files.length > 0
|
|
97
|
+
&& SHA256.test(value.output?.snapshot_digest)
|
|
98
|
+
&& value.output.snapshot_digest === digestArtifact({ files: value.output.files });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function surfacePaths(surface) {
|
|
102
|
+
return validSurface(surface) ? surface.files.map(({ path }) => path) : [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function outputProjection(surface) {
|
|
106
|
+
if (!validSurface(surface) || surface.files.some(({ state }) => state !== 'present')) return null;
|
|
107
|
+
return {
|
|
108
|
+
files: surface.files.map(({ path, content_digest: contentDigest }) => ({
|
|
109
|
+
path,
|
|
110
|
+
content_digest: contentDigest,
|
|
111
|
+
})),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function envelopeValid(value) {
|
|
116
|
+
return exactRecord(value, ENVELOPE_KEYS)
|
|
117
|
+
&& value.schema === 'lattice.rc1.behavior_evidence_envelope.v2'
|
|
118
|
+
&& GIT_SHA1.test(value.base_sha)
|
|
119
|
+
&& [
|
|
120
|
+
value.oracle_digest,
|
|
121
|
+
value.runtime_identity_digest,
|
|
122
|
+
value.case_contract_digest,
|
|
123
|
+
value.pre_receipt_digest,
|
|
124
|
+
value.post_receipt_digest,
|
|
125
|
+
value.pre_surface_digest,
|
|
126
|
+
value.post_surface_digest,
|
|
127
|
+
value.transform_artifact_digest,
|
|
128
|
+
value.patch_digest,
|
|
129
|
+
value.output_snapshot_digest,
|
|
130
|
+
value.envelope_digest,
|
|
131
|
+
].every((entry) => typeof entry === 'string' && SHA256.test(entry))
|
|
132
|
+
&& value.envelope_digest === digestWithout(value, 'envelope_digest');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function behaviorChecks(options) {
|
|
136
|
+
const inputValid = exactRecord(options, [
|
|
137
|
+
'oracle',
|
|
138
|
+
'runtimeIdentity',
|
|
139
|
+
'preReceipt',
|
|
140
|
+
'postReceipt',
|
|
141
|
+
'transformArtifact',
|
|
142
|
+
'patchDigest',
|
|
143
|
+
'envelope',
|
|
144
|
+
]);
|
|
145
|
+
const oracle = inputValid ? options.oracle : null;
|
|
146
|
+
const runtimeIdentity = inputValid ? options.runtimeIdentity : null;
|
|
147
|
+
const pre = inputValid ? options.preReceipt : null;
|
|
148
|
+
const post = inputValid ? options.postReceipt : null;
|
|
149
|
+
const transform = inputValid ? options.transformArtifact : null;
|
|
150
|
+
const patchDigest = inputValid ? options.patchDigest : null;
|
|
151
|
+
const envelope = inputValid ? options.envelope : null;
|
|
152
|
+
const preVerification = inputValid ? verifyRc1V6BehaviorReceipt({
|
|
153
|
+
receipt: pre,
|
|
154
|
+
oracle,
|
|
155
|
+
expectedRole: 'pre_transform',
|
|
156
|
+
expectedRuntimeIdentity: runtimeIdentity,
|
|
157
|
+
}) : { valid: false };
|
|
158
|
+
const postVerification = inputValid ? verifyRc1V6BehaviorReceipt({
|
|
159
|
+
receipt: post,
|
|
160
|
+
oracle,
|
|
161
|
+
expectedRole: 'post_transform',
|
|
162
|
+
expectedRuntimeIdentity: runtimeIdentity,
|
|
163
|
+
}) : { valid: false };
|
|
164
|
+
const receiptsValid = preVerification.valid && postVerification.valid;
|
|
165
|
+
const transformValid = inputValid && validAcceptedTransform(transform);
|
|
166
|
+
const validEnvelope = inputValid && envelopeValid(envelope);
|
|
167
|
+
const preSurfaceValid = receiptsValid && validSurface(pre.surface);
|
|
168
|
+
const postSurfaceValid = receiptsValid && validSurface(post.surface);
|
|
169
|
+
const prePaths = preSurfaceValid ? surfacePaths(pre.surface) : [];
|
|
170
|
+
const postPaths = postSurfaceValid ? surfacePaths(post.surface) : [];
|
|
171
|
+
const projection = postSurfaceValid ? outputProjection(post.surface) : null;
|
|
172
|
+
const outputBound = transformValid
|
|
173
|
+
&& projection !== null
|
|
174
|
+
&& isDeepStrictEqual(prePaths, postPaths)
|
|
175
|
+
&& isDeepStrictEqual(postPaths, transform.scope.allowed_paths)
|
|
176
|
+
&& digestArtifact(projection) === transform.output.snapshot_digest
|
|
177
|
+
&& digestArtifact(projection.files) === digestArtifact(transform.output.files);
|
|
178
|
+
|
|
179
|
+
return [
|
|
180
|
+
{ id: 'input_contract', passed: inputValid },
|
|
181
|
+
{ id: 'pre_receipt', passed: preVerification.valid },
|
|
182
|
+
{ id: 'post_receipt', passed: postVerification.valid },
|
|
183
|
+
{ id: 'envelope_contract', passed: validEnvelope },
|
|
184
|
+
{ id: 'transform_contract', passed: transformValid },
|
|
185
|
+
{
|
|
186
|
+
id: 'base_binding',
|
|
187
|
+
passed: receiptsValid && transformValid
|
|
188
|
+
&& pre.base_sha === post.base_sha
|
|
189
|
+
&& pre.base_sha === transform.source.base_sha,
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
id: 'oracle_runtime_binding',
|
|
193
|
+
passed: receiptsValid
|
|
194
|
+
&& pre.oracle_digest === post.oracle_digest
|
|
195
|
+
&& pre.runtime_identity_digest === post.runtime_identity_digest
|
|
196
|
+
&& pre.case_contract_digest === post.case_contract_digest,
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
id: 'behavior_outcome',
|
|
200
|
+
passed: receiptsValid && pre.outcome === 'passed' && post.outcome === 'passed',
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
id: 'pre_source_binding',
|
|
204
|
+
passed: preSurfaceValid && transformValid
|
|
205
|
+
&& digestArtifact(sourceSnapshotFromRc1BehaviorSurface(pre.surface))
|
|
206
|
+
=== transform.source.code_snapshot_digest,
|
|
207
|
+
},
|
|
208
|
+
{ id: 'post_output_binding', passed: outputBound },
|
|
209
|
+
{
|
|
210
|
+
id: 'patch_binding',
|
|
211
|
+
passed: transformValid
|
|
212
|
+
&& typeof patchDigest === 'string'
|
|
213
|
+
&& SHA256.test(patchDigest)
|
|
214
|
+
&& patchDigest === transform.patch.digest,
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
id: 'envelope_binding',
|
|
218
|
+
passed: receiptsValid && transformValid && validEnvelope && outputBound
|
|
219
|
+
&& envelope.base_sha === pre.base_sha
|
|
220
|
+
&& envelope.oracle_digest === pre.oracle_digest
|
|
221
|
+
&& envelope.runtime_identity_digest === pre.runtime_identity_digest
|
|
222
|
+
&& envelope.case_contract_digest === pre.case_contract_digest
|
|
223
|
+
&& envelope.pre_receipt_digest === pre.receipt_digest
|
|
224
|
+
&& envelope.post_receipt_digest === post.receipt_digest
|
|
225
|
+
&& envelope.pre_surface_digest === pre.surface_digest
|
|
226
|
+
&& envelope.post_surface_digest === post.surface_digest
|
|
227
|
+
&& envelope.transform_artifact_digest === digestArtifact(transform)
|
|
228
|
+
&& envelope.patch_digest === patchDigest
|
|
229
|
+
&& envelope.output_snapshot_digest === transform.output.snapshot_digest,
|
|
230
|
+
},
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** v6 full receiptsをsaved oracle、runtime、accepted transformへcross-bindする。 */
|
|
235
|
+
export function compileRc1V6BehaviorEvidence(options = {}) {
|
|
236
|
+
if (!exactRecord(options, [
|
|
237
|
+
'oracle',
|
|
238
|
+
'runtimeIdentity',
|
|
239
|
+
'preReceipt',
|
|
240
|
+
'postReceipt',
|
|
241
|
+
'transformArtifact',
|
|
242
|
+
'patchDigest',
|
|
243
|
+
])) {
|
|
244
|
+
throw new TypeError('RC1 v6 behavior evidence契約違反: input shapeが不正');
|
|
245
|
+
}
|
|
246
|
+
const preimage = {
|
|
247
|
+
schema: 'lattice.rc1.behavior_evidence_envelope.v2',
|
|
248
|
+
base_sha: options.preReceipt?.base_sha,
|
|
249
|
+
oracle_digest: options.preReceipt?.oracle_digest,
|
|
250
|
+
runtime_identity_digest: options.preReceipt?.runtime_identity_digest,
|
|
251
|
+
case_contract_digest: options.preReceipt?.case_contract_digest,
|
|
252
|
+
pre_receipt_digest: options.preReceipt?.receipt_digest,
|
|
253
|
+
post_receipt_digest: options.postReceipt?.receipt_digest,
|
|
254
|
+
pre_surface_digest: options.preReceipt?.surface_digest,
|
|
255
|
+
post_surface_digest: options.postReceipt?.surface_digest,
|
|
256
|
+
transform_artifact_digest: digestArtifact(options.transformArtifact),
|
|
257
|
+
patch_digest: options.patchDigest,
|
|
258
|
+
output_snapshot_digest: options.transformArtifact?.output?.snapshot_digest,
|
|
259
|
+
};
|
|
260
|
+
const envelope = { ...preimage, envelope_digest: digestArtifact(preimage) };
|
|
261
|
+
const result = verifyRc1V6BehaviorEvidence({ ...options, envelope });
|
|
262
|
+
if (!result.valid) {
|
|
263
|
+
throw new TypeError(
|
|
264
|
+
`RC1 v6 behavior evidence契約違反: cross-binding failed: ${result.failed_conditions.join(', ')}`,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return envelope;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** 保存artifactだけからv6 behavior evidenceの全cross-bindingを再計算する。 */
|
|
271
|
+
export function verifyRc1V6BehaviorEvidence(options = {}) {
|
|
272
|
+
return verification(behaviorChecks(options));
|
|
273
|
+
}
|