@longarc/mdash 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +278 -0
- package/dist/checkpoint/engine.d.ts +208 -0
- package/dist/checkpoint/engine.d.ts.map +1 -0
- package/dist/checkpoint/engine.js +369 -0
- package/dist/checkpoint/engine.js.map +1 -0
- package/dist/context/engine.d.ts +197 -0
- package/dist/context/engine.d.ts.map +1 -0
- package/dist/context/engine.js +392 -0
- package/dist/context/engine.js.map +1 -0
- package/dist/core/commitment.d.ts +154 -0
- package/dist/core/commitment.d.ts.map +1 -0
- package/dist/core/commitment.js +305 -0
- package/dist/core/commitment.js.map +1 -0
- package/dist/core/crypto.d.ts +100 -0
- package/dist/core/crypto.d.ts.map +1 -0
- package/dist/core/crypto.js +243 -0
- package/dist/core/crypto.js.map +1 -0
- package/dist/index.d.ts +121 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +234 -0
- package/dist/index.js.map +1 -0
- package/dist/mcca/engine.d.ts +260 -0
- package/dist/mcca/engine.d.ts.map +1 -0
- package/dist/mcca/engine.js +518 -0
- package/dist/mcca/engine.js.map +1 -0
- package/dist/physics/engine.d.ts +165 -0
- package/dist/physics/engine.d.ts.map +1 -0
- package/dist/physics/engine.js +371 -0
- package/dist/physics/engine.js.map +1 -0
- package/dist/tee/engine.d.ts +285 -0
- package/dist/tee/engine.d.ts.map +1 -0
- package/dist/tee/engine.js +505 -0
- package/dist/tee/engine.js.map +1 -0
- package/dist/warrant/engine.d.ts +195 -0
- package/dist/warrant/engine.d.ts.map +1 -0
- package/dist/warrant/engine.js +409 -0
- package/dist/warrant/engine.js.map +1 -0
- package/dist/zk/engine.d.ts +243 -0
- package/dist/zk/engine.d.ts.map +1 -0
- package/dist/zk/engine.js +489 -0
- package/dist/zk/engine.js.map +1 -0
- package/package.json +25 -0
- package/src/__tests__/phase1.test.ts +1120 -0
- package/src/__tests__/phase2-4.test.ts +898 -0
- package/src/checkpoint/engine.ts +532 -0
- package/src/context/engine.ts +598 -0
- package/src/core/commitment.ts +438 -0
- package/src/core/crypto.ts +304 -0
- package/src/index.ts +320 -0
- package/src/mcca/engine.ts +778 -0
- package/src/physics/engine.ts +563 -0
- package/src/tee/engine.ts +810 -0
- package/src/warrant/engine.ts +625 -0
- package/src/zk/engine.ts +730 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mdash v3.0 - Sealed Context Architecture (SCA)
|
|
3
|
+
*
|
|
4
|
+
* Incremental Merkle sealing with streaming support.
|
|
5
|
+
* O(log n) proof verification.
|
|
6
|
+
*
|
|
7
|
+
* Target Latency:
|
|
8
|
+
* - Context chunk seal: <2ms P50, <5ms P99
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
Hash,
|
|
13
|
+
Seal,
|
|
14
|
+
Timestamp,
|
|
15
|
+
FragmentId,
|
|
16
|
+
generateFragmentId,
|
|
17
|
+
generateTimestamp,
|
|
18
|
+
sha256Object,
|
|
19
|
+
hmacSeal,
|
|
20
|
+
hmacVerify,
|
|
21
|
+
deriveKey,
|
|
22
|
+
deterministicStringify,
|
|
23
|
+
} from '../core/crypto';
|
|
24
|
+
|
|
25
|
+
import { IncrementalMerkleTree, CommitmentEngine } from '../core/commitment';
|
|
26
|
+
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// CONTEXT TYPES
|
|
29
|
+
// ============================================================================
|
|
30
|
+
|
|
31
|
+
export type SourceClass =
|
|
32
|
+
| 'system' // Internal, fully trusted (100)
|
|
33
|
+
| 'operator' // Human oversight (90)
|
|
34
|
+
| 'user' // Authenticated user (70)
|
|
35
|
+
| 'derived' // Computed from other contexts (60)
|
|
36
|
+
| 'agent' // AI-generated (50)
|
|
37
|
+
| 'external'; // Third-party APIs (30)
|
|
38
|
+
|
|
39
|
+
export type ContentType =
|
|
40
|
+
| 'text'
|
|
41
|
+
| 'structured'
|
|
42
|
+
| 'code'
|
|
43
|
+
| 'binary'
|
|
44
|
+
| 'reference';
|
|
45
|
+
|
|
46
|
+
export interface SemanticUnit<T = unknown> {
|
|
47
|
+
/** Content type */
|
|
48
|
+
type: ContentType;
|
|
49
|
+
/** The actual data */
|
|
50
|
+
data: T;
|
|
51
|
+
/** Approximate token count */
|
|
52
|
+
tokens: number;
|
|
53
|
+
/** Optional metadata */
|
|
54
|
+
metadata?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Provenance {
|
|
58
|
+
/** Source URI */
|
|
59
|
+
source: string;
|
|
60
|
+
/** Attribution type */
|
|
61
|
+
attribution: SourceClass;
|
|
62
|
+
/** Trust level (0-100) */
|
|
63
|
+
trust_level: number;
|
|
64
|
+
/** Creation timestamp */
|
|
65
|
+
timestamp: Timestamp;
|
|
66
|
+
/** Parent fragment (if derived) */
|
|
67
|
+
parent_hash: Hash | null;
|
|
68
|
+
/** Optional signature */
|
|
69
|
+
signature?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ContextFragment<T = unknown> {
|
|
73
|
+
/** Unique identifier */
|
|
74
|
+
id: FragmentId;
|
|
75
|
+
/** Content hash */
|
|
76
|
+
hash: Hash;
|
|
77
|
+
/** Semantic content */
|
|
78
|
+
content: SemanticUnit<T>;
|
|
79
|
+
/** Provenance chain */
|
|
80
|
+
provenance: Provenance;
|
|
81
|
+
/** Seal timestamp */
|
|
82
|
+
sealed_at: Timestamp;
|
|
83
|
+
/** HMAC seal */
|
|
84
|
+
seal: Seal;
|
|
85
|
+
/** Constraints for resolution */
|
|
86
|
+
constraints: ContextConstraint[];
|
|
87
|
+
/** Protocol version */
|
|
88
|
+
version: 'v3.0';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface ContextConstraint {
|
|
92
|
+
kind: 'time' | 'scope' | 'trust' | 'signature';
|
|
93
|
+
type: string;
|
|
94
|
+
params: Record<string, unknown>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ============================================================================
|
|
98
|
+
// TRUST LEVELS
|
|
99
|
+
// ============================================================================
|
|
100
|
+
|
|
101
|
+
export const DEFAULT_TRUST_LEVELS: Record<SourceClass, number> = {
|
|
102
|
+
system: 100,
|
|
103
|
+
operator: 90,
|
|
104
|
+
user: 70,
|
|
105
|
+
derived: 60,
|
|
106
|
+
agent: 50,
|
|
107
|
+
external: 30,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// ============================================================================
|
|
111
|
+
// CONTEXT STREAM - For incremental sealing
|
|
112
|
+
// ============================================================================
|
|
113
|
+
|
|
114
|
+
export interface StreamChunk<T = unknown> {
|
|
115
|
+
/** Chunk index */
|
|
116
|
+
index: number;
|
|
117
|
+
/** Chunk content */
|
|
118
|
+
content: T;
|
|
119
|
+
/** Chunk hash */
|
|
120
|
+
hash: Hash;
|
|
121
|
+
/** Sealed flag */
|
|
122
|
+
sealed: boolean;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export class ContextStream<T = unknown> {
|
|
126
|
+
private chunks: StreamChunk<T>[] = [];
|
|
127
|
+
private tree: IncrementalMerkleTree;
|
|
128
|
+
private key: CryptoKey | null = null;
|
|
129
|
+
private sourceClass: SourceClass;
|
|
130
|
+
private source: string;
|
|
131
|
+
|
|
132
|
+
constructor(params: {
|
|
133
|
+
source: string;
|
|
134
|
+
sourceClass: SourceClass;
|
|
135
|
+
maxDepth?: number;
|
|
136
|
+
}) {
|
|
137
|
+
this.source = params.source;
|
|
138
|
+
this.sourceClass = params.sourceClass;
|
|
139
|
+
this.tree = new IncrementalMerkleTree(params.maxDepth || 16);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Initialize with seal key
|
|
144
|
+
*/
|
|
145
|
+
async initialize(sealKey: string): Promise<void> {
|
|
146
|
+
this.key = await deriveKey(sealKey);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Add a chunk to the stream
|
|
151
|
+
* Target: <2ms P50, <5ms P99
|
|
152
|
+
*/
|
|
153
|
+
async addChunk(content: T): Promise<StreamChunk<T>> {
|
|
154
|
+
const startTime = performance.now();
|
|
155
|
+
|
|
156
|
+
const index = this.chunks.length;
|
|
157
|
+
const hash = await sha256Object({ index, content });
|
|
158
|
+
|
|
159
|
+
const chunk: StreamChunk<T> = {
|
|
160
|
+
index,
|
|
161
|
+
content,
|
|
162
|
+
hash,
|
|
163
|
+
sealed: false,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// Add to Merkle tree
|
|
167
|
+
await this.tree.addLeaf(hash);
|
|
168
|
+
chunk.sealed = true;
|
|
169
|
+
|
|
170
|
+
this.chunks.push(chunk);
|
|
171
|
+
|
|
172
|
+
const elapsed = performance.now() - startTime;
|
|
173
|
+
if (elapsed > 5) {
|
|
174
|
+
console.warn(`Context chunk seal exceeded P99: ${elapsed.toFixed(2)}ms`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return chunk;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Get the current root hash
|
|
182
|
+
*/
|
|
183
|
+
async getRoot(): Promise<Hash> {
|
|
184
|
+
return this.tree.getRoot();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Get proof for a specific chunk
|
|
189
|
+
*/
|
|
190
|
+
async getProof(index: number): Promise<{
|
|
191
|
+
chunk: StreamChunk<T>;
|
|
192
|
+
path: Array<{ hash: Hash; position: 'left' | 'right' }>;
|
|
193
|
+
root: Hash;
|
|
194
|
+
}> {
|
|
195
|
+
if (index < 0 || index >= this.chunks.length) {
|
|
196
|
+
throw new Error(`Invalid chunk index: ${index}`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const chunk = this.chunks[index];
|
|
200
|
+
if (!chunk) {
|
|
201
|
+
throw new Error(`Chunk not found at index: ${index}`);
|
|
202
|
+
}
|
|
203
|
+
const path = await this.tree.getProof(index);
|
|
204
|
+
const root = await this.tree.getRoot();
|
|
205
|
+
|
|
206
|
+
return { chunk, path, root };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Finalize the stream into a single sealed fragment
|
|
211
|
+
*/
|
|
212
|
+
async finalize(): Promise<ContextFragment<T[]>> {
|
|
213
|
+
if (!this.key) {
|
|
214
|
+
throw new Error('Stream not initialized. Call initialize() first.');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const id = generateFragmentId();
|
|
218
|
+
const now = generateTimestamp();
|
|
219
|
+
const root = await this.tree.getRoot();
|
|
220
|
+
|
|
221
|
+
// Collect all chunk data
|
|
222
|
+
const data = this.chunks.map(c => c.content);
|
|
223
|
+
const totalTokens = this.chunks.length * 100; // Approximate
|
|
224
|
+
|
|
225
|
+
const content: SemanticUnit<T[]> = {
|
|
226
|
+
type: 'structured',
|
|
227
|
+
data,
|
|
228
|
+
tokens: totalTokens,
|
|
229
|
+
metadata: {
|
|
230
|
+
chunks: this.chunks.length,
|
|
231
|
+
merkle_root: root,
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const provenance: Provenance = {
|
|
236
|
+
source: this.source,
|
|
237
|
+
attribution: this.sourceClass,
|
|
238
|
+
trust_level: DEFAULT_TRUST_LEVELS[this.sourceClass],
|
|
239
|
+
timestamp: now,
|
|
240
|
+
parent_hash: null,
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const hash = await sha256Object({
|
|
244
|
+
content,
|
|
245
|
+
provenance,
|
|
246
|
+
sealed_at: now,
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
const sealData = {
|
|
250
|
+
_v: 1,
|
|
251
|
+
id,
|
|
252
|
+
hash,
|
|
253
|
+
content,
|
|
254
|
+
provenance,
|
|
255
|
+
sealed_at: now,
|
|
256
|
+
constraints: [],
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const seal = await hmacSeal(sealData, this.key);
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
id,
|
|
263
|
+
hash,
|
|
264
|
+
content,
|
|
265
|
+
provenance,
|
|
266
|
+
sealed_at: now,
|
|
267
|
+
seal,
|
|
268
|
+
constraints: [],
|
|
269
|
+
version: 'v3.0',
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Get stream statistics
|
|
275
|
+
*/
|
|
276
|
+
getStats(): {
|
|
277
|
+
chunks: number;
|
|
278
|
+
treeStats: ReturnType<IncrementalMerkleTree['getStats']>;
|
|
279
|
+
} {
|
|
280
|
+
return {
|
|
281
|
+
chunks: this.chunks.length,
|
|
282
|
+
treeStats: this.tree.getStats(),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ============================================================================
|
|
288
|
+
// SEALED CONTEXT ENGINE
|
|
289
|
+
// ============================================================================
|
|
290
|
+
|
|
291
|
+
export class SealedContextEngine {
|
|
292
|
+
private key: CryptoKey | null = null;
|
|
293
|
+
private commitmentEngine: CommitmentEngine;
|
|
294
|
+
private fragments: Map<FragmentId, ContextFragment> = new Map();
|
|
295
|
+
private byHash: Map<Hash, FragmentId> = new Map();
|
|
296
|
+
|
|
297
|
+
constructor(commitmentEngine: CommitmentEngine) {
|
|
298
|
+
this.commitmentEngine = commitmentEngine;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Initialize the engine with a seal key
|
|
303
|
+
*/
|
|
304
|
+
async initialize(sealKey: string): Promise<void> {
|
|
305
|
+
this.key = await deriveKey(sealKey);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Create a sealed context fragment
|
|
310
|
+
*/
|
|
311
|
+
async seal<T>(params: {
|
|
312
|
+
content: T;
|
|
313
|
+
contentType: ContentType;
|
|
314
|
+
source: string;
|
|
315
|
+
sourceClass: SourceClass;
|
|
316
|
+
parentHash?: Hash;
|
|
317
|
+
constraints?: ContextConstraint[];
|
|
318
|
+
tokens?: number;
|
|
319
|
+
}): Promise<ContextFragment<T>> {
|
|
320
|
+
if (!this.key) {
|
|
321
|
+
throw new Error('Engine not initialized. Call initialize() first.');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const startTime = performance.now();
|
|
325
|
+
|
|
326
|
+
const id = generateFragmentId();
|
|
327
|
+
const now = generateTimestamp();
|
|
328
|
+
|
|
329
|
+
const semanticUnit: SemanticUnit<T> = {
|
|
330
|
+
type: params.contentType,
|
|
331
|
+
data: params.content,
|
|
332
|
+
tokens: params.tokens ?? this.estimateTokens(params.content),
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const provenance: Provenance = {
|
|
336
|
+
source: params.source,
|
|
337
|
+
attribution: params.sourceClass,
|
|
338
|
+
trust_level: DEFAULT_TRUST_LEVELS[params.sourceClass],
|
|
339
|
+
timestamp: now,
|
|
340
|
+
parent_hash: params.parentHash || null,
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const hash = await sha256Object({
|
|
344
|
+
content: semanticUnit,
|
|
345
|
+
provenance,
|
|
346
|
+
sealed_at: now,
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const sealData = {
|
|
350
|
+
_v: 1,
|
|
351
|
+
id,
|
|
352
|
+
hash,
|
|
353
|
+
content: semanticUnit,
|
|
354
|
+
provenance,
|
|
355
|
+
sealed_at: now,
|
|
356
|
+
constraints: params.constraints || [],
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const seal = await hmacSeal(sealData, this.key);
|
|
360
|
+
|
|
361
|
+
const fragment: ContextFragment<T> = {
|
|
362
|
+
id,
|
|
363
|
+
hash,
|
|
364
|
+
content: semanticUnit,
|
|
365
|
+
provenance,
|
|
366
|
+
sealed_at: now,
|
|
367
|
+
seal,
|
|
368
|
+
constraints: params.constraints || [],
|
|
369
|
+
version: 'v3.0',
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// Commit to L1
|
|
373
|
+
await this.commitmentEngine.commit(fragment, `context:${id}`);
|
|
374
|
+
|
|
375
|
+
// Store
|
|
376
|
+
this.fragments.set(id, fragment as ContextFragment);
|
|
377
|
+
this.byHash.set(hash, id);
|
|
378
|
+
|
|
379
|
+
const elapsed = performance.now() - startTime;
|
|
380
|
+
if (elapsed > 5) {
|
|
381
|
+
console.warn(`Context seal exceeded P99: ${elapsed.toFixed(2)}ms`);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return fragment;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Derive a new fragment from an existing one
|
|
389
|
+
*/
|
|
390
|
+
async derive<T, U>(params: {
|
|
391
|
+
parent: ContextFragment<T>;
|
|
392
|
+
transform: (data: T) => U;
|
|
393
|
+
source: string;
|
|
394
|
+
constraints?: ContextConstraint[];
|
|
395
|
+
}): Promise<ContextFragment<U>> {
|
|
396
|
+
const derived = params.transform(params.parent.content.data);
|
|
397
|
+
|
|
398
|
+
const sealParams: {
|
|
399
|
+
content: U;
|
|
400
|
+
contentType: ContentType;
|
|
401
|
+
source: string;
|
|
402
|
+
sourceClass: SourceClass;
|
|
403
|
+
parentHash: Hash;
|
|
404
|
+
constraints?: ContextConstraint[];
|
|
405
|
+
} = {
|
|
406
|
+
content: derived,
|
|
407
|
+
contentType: params.parent.content.type,
|
|
408
|
+
source: params.source,
|
|
409
|
+
sourceClass: 'derived',
|
|
410
|
+
parentHash: params.parent.hash,
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
if (params.constraints) {
|
|
414
|
+
sealParams.constraints = params.constraints;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return this.seal(sealParams);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Verify a fragment
|
|
422
|
+
*/
|
|
423
|
+
async verify<T>(fragment: ContextFragment<T>): Promise<boolean> {
|
|
424
|
+
if (!this.key) {
|
|
425
|
+
throw new Error('Engine not initialized. Call initialize() first.');
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Verify hash
|
|
429
|
+
const expectedHash = await sha256Object({
|
|
430
|
+
content: fragment.content,
|
|
431
|
+
provenance: fragment.provenance,
|
|
432
|
+
sealed_at: fragment.sealed_at,
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
if (expectedHash !== fragment.hash) {
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Verify seal
|
|
440
|
+
const sealData = {
|
|
441
|
+
_v: 1,
|
|
442
|
+
id: fragment.id,
|
|
443
|
+
hash: fragment.hash,
|
|
444
|
+
content: fragment.content,
|
|
445
|
+
provenance: fragment.provenance,
|
|
446
|
+
sealed_at: fragment.sealed_at,
|
|
447
|
+
constraints: fragment.constraints,
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
return hmacVerify(sealData, fragment.seal, this.key);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Resolve a fragment (check constraints)
|
|
455
|
+
*/
|
|
456
|
+
async resolve<T>(
|
|
457
|
+
fragment: ContextFragment<T>,
|
|
458
|
+
context: { domain?: string; now?: Date }
|
|
459
|
+
): Promise<{
|
|
460
|
+
success: boolean;
|
|
461
|
+
violations: Array<{ constraint: ContextConstraint; reason: string }>;
|
|
462
|
+
}> {
|
|
463
|
+
const violations: Array<{ constraint: ContextConstraint; reason: string }> = [];
|
|
464
|
+
const now = context.now || new Date();
|
|
465
|
+
|
|
466
|
+
for (const constraint of fragment.constraints) {
|
|
467
|
+
switch (constraint.kind) {
|
|
468
|
+
case 'time': {
|
|
469
|
+
if (constraint.type === 'max_age') {
|
|
470
|
+
const maxAgeMs = constraint.params.max_age_ms as number;
|
|
471
|
+
const sealedAt = new Date(fragment.sealed_at);
|
|
472
|
+
if (now.getTime() - sealedAt.getTime() > maxAgeMs) {
|
|
473
|
+
violations.push({
|
|
474
|
+
constraint,
|
|
475
|
+
reason: `Fragment age exceeds max_age_ms: ${maxAgeMs}`,
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
case 'trust': {
|
|
483
|
+
if (constraint.type === 'minimum') {
|
|
484
|
+
const minTrust = constraint.params.minimum_trust as number;
|
|
485
|
+
if (fragment.provenance.trust_level < minTrust) {
|
|
486
|
+
violations.push({
|
|
487
|
+
constraint,
|
|
488
|
+
reason: `Trust ${fragment.provenance.trust_level} < required ${minTrust}`,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
case 'scope': {
|
|
496
|
+
if (constraint.type === 'domain' && context.domain) {
|
|
497
|
+
const allowed = constraint.params.allowed_domains as string[];
|
|
498
|
+
if (!allowed.includes(context.domain)) {
|
|
499
|
+
violations.push({
|
|
500
|
+
constraint,
|
|
501
|
+
reason: `Domain ${context.domain} not in allowed list`,
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return {
|
|
511
|
+
success: violations.length === 0,
|
|
512
|
+
violations,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Get fragment by ID
|
|
518
|
+
*/
|
|
519
|
+
get<T>(id: FragmentId): ContextFragment<T> | null {
|
|
520
|
+
return (this.fragments.get(id) as ContextFragment<T>) || null;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Get fragment by hash
|
|
525
|
+
*/
|
|
526
|
+
getByHash<T>(hash: Hash): ContextFragment<T> | null {
|
|
527
|
+
const id = this.byHash.get(hash);
|
|
528
|
+
if (!id) return null;
|
|
529
|
+
return this.get(id);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Create a streaming context
|
|
534
|
+
*/
|
|
535
|
+
createStream<T>(params: {
|
|
536
|
+
source: string;
|
|
537
|
+
sourceClass: SourceClass;
|
|
538
|
+
}): ContextStream<T> {
|
|
539
|
+
return new ContextStream<T>(params);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Estimate tokens (rough: ~4 chars/token)
|
|
544
|
+
*/
|
|
545
|
+
private estimateTokens(content: unknown): number {
|
|
546
|
+
const str = typeof content === 'string'
|
|
547
|
+
? content
|
|
548
|
+
: deterministicStringify(content);
|
|
549
|
+
return Math.ceil(str.length / 4);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Get statistics
|
|
554
|
+
*/
|
|
555
|
+
getStats(): {
|
|
556
|
+
fragments: number;
|
|
557
|
+
} {
|
|
558
|
+
return {
|
|
559
|
+
fragments: this.fragments.size,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// ============================================================================
|
|
565
|
+
// CONSTRAINT BUILDERS
|
|
566
|
+
// ============================================================================
|
|
567
|
+
|
|
568
|
+
export function maxAge(ms: number): ContextConstraint {
|
|
569
|
+
return {
|
|
570
|
+
kind: 'time',
|
|
571
|
+
type: 'max_age',
|
|
572
|
+
params: { max_age_ms: ms },
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export function requireTrust(minimum: number): ContextConstraint {
|
|
577
|
+
return {
|
|
578
|
+
kind: 'trust',
|
|
579
|
+
type: 'minimum',
|
|
580
|
+
params: { minimum_trust: minimum },
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export function allowDomains(domains: string[]): ContextConstraint {
|
|
585
|
+
return {
|
|
586
|
+
kind: 'scope',
|
|
587
|
+
type: 'domain',
|
|
588
|
+
params: { allowed_domains: domains },
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
export function requireSignature(signers: string[]): ContextConstraint {
|
|
593
|
+
return {
|
|
594
|
+
kind: 'signature',
|
|
595
|
+
type: 'required',
|
|
596
|
+
params: { allowed_signers: signers },
|
|
597
|
+
};
|
|
598
|
+
}
|