@longarc/mdash 3.1.2 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +86 -23
  2. package/SECURITY.md +254 -0
  3. package/dist/accountability/engine.d.ts +27 -0
  4. package/dist/accountability/engine.d.ts.map +1 -0
  5. package/dist/accountability/engine.js +148 -0
  6. package/dist/accountability/engine.js.map +1 -0
  7. package/dist/accountability/types.d.ts +46 -0
  8. package/dist/accountability/types.d.ts.map +1 -0
  9. package/dist/accountability/types.js +8 -0
  10. package/dist/accountability/types.js.map +1 -0
  11. package/dist/checkpoint/engine.d.ts.map +1 -1
  12. package/dist/checkpoint/engine.js +4 -0
  13. package/dist/checkpoint/engine.js.map +1 -1
  14. package/dist/context/compose.d.ts +62 -0
  15. package/dist/context/compose.d.ts.map +1 -0
  16. package/dist/context/compose.js +286 -0
  17. package/dist/context/compose.js.map +1 -0
  18. package/dist/context/crypto/hash.d.ts +100 -0
  19. package/dist/context/crypto/hash.d.ts.map +1 -0
  20. package/dist/context/crypto/hash.js +248 -0
  21. package/dist/context/crypto/hash.js.map +1 -0
  22. package/dist/context/crypto/hmac.d.ts +80 -0
  23. package/dist/context/crypto/hmac.d.ts.map +1 -0
  24. package/dist/context/crypto/hmac.js +192 -0
  25. package/dist/context/crypto/hmac.js.map +1 -0
  26. package/dist/context/crypto/index.d.ts +7 -0
  27. package/dist/context/crypto/index.d.ts.map +1 -0
  28. package/dist/context/crypto/index.js +7 -0
  29. package/dist/context/crypto/index.js.map +1 -0
  30. package/dist/context/engine-v3.0-backup.d.ts +197 -0
  31. package/dist/context/engine-v3.0-backup.d.ts.map +1 -0
  32. package/dist/context/engine-v3.0-backup.js +392 -0
  33. package/dist/context/engine-v3.0-backup.js.map +1 -0
  34. package/dist/context/fragment.d.ts +99 -0
  35. package/dist/context/fragment.d.ts.map +1 -0
  36. package/dist/context/fragment.js +316 -0
  37. package/dist/context/fragment.js.map +1 -0
  38. package/dist/context/index.d.ts +99 -0
  39. package/dist/context/index.d.ts.map +1 -0
  40. package/dist/context/index.js +180 -0
  41. package/dist/context/index.js.map +1 -0
  42. package/dist/context/provenance.d.ts +80 -0
  43. package/dist/context/provenance.d.ts.map +1 -0
  44. package/dist/context/provenance.js +294 -0
  45. package/dist/context/provenance.js.map +1 -0
  46. package/dist/context/resolve.d.ts +106 -0
  47. package/dist/context/resolve.d.ts.map +1 -0
  48. package/dist/context/resolve.js +440 -0
  49. package/dist/context/resolve.js.map +1 -0
  50. package/dist/context/store.d.ts +156 -0
  51. package/dist/context/store.d.ts.map +1 -0
  52. package/dist/context/store.js +396 -0
  53. package/dist/context/store.js.map +1 -0
  54. package/dist/context/types.d.ts +463 -0
  55. package/dist/context/types.d.ts.map +1 -0
  56. package/dist/context/types.js +94 -0
  57. package/dist/context/types.js.map +1 -0
  58. package/dist/context/utils/atomic.d.ts +76 -0
  59. package/dist/context/utils/atomic.d.ts.map +1 -0
  60. package/dist/context/utils/atomic.js +159 -0
  61. package/dist/context/utils/atomic.js.map +1 -0
  62. package/dist/context/utils/credit.d.ts +65 -0
  63. package/dist/context/utils/credit.d.ts.map +1 -0
  64. package/dist/context/utils/credit.js +164 -0
  65. package/dist/context/utils/credit.js.map +1 -0
  66. package/dist/context/utils/index.d.ts +13 -0
  67. package/dist/context/utils/index.d.ts.map +1 -0
  68. package/dist/context/utils/index.js +13 -0
  69. package/dist/context/utils/index.js.map +1 -0
  70. package/dist/context/utils/utility.d.ts +63 -0
  71. package/dist/context/utils/utility.d.ts.map +1 -0
  72. package/dist/context/utils/utility.js +141 -0
  73. package/dist/context/utils/utility.js.map +1 -0
  74. package/dist/core/commitment.d.ts +25 -2
  75. package/dist/core/commitment.d.ts.map +1 -1
  76. package/dist/core/commitment.js +44 -6
  77. package/dist/core/commitment.js.map +1 -1
  78. package/dist/core/crypto.d.ts +2 -0
  79. package/dist/core/crypto.d.ts.map +1 -1
  80. package/dist/core/crypto.js +12 -0
  81. package/dist/core/crypto.js.map +1 -1
  82. package/dist/index.d.ts +11 -6
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +35 -10
  85. package/dist/index.js.map +1 -1
  86. package/dist/mcca/engine.d.ts.map +1 -1
  87. package/dist/mcca/engine.js +5 -4
  88. package/dist/mcca/engine.js.map +1 -1
  89. package/dist/physics/engine.d.ts +1 -0
  90. package/dist/physics/engine.d.ts.map +1 -1
  91. package/dist/physics/engine.js +36 -2
  92. package/dist/physics/engine.js.map +1 -1
  93. package/dist/provenance/api-handler.d.ts +45 -0
  94. package/dist/provenance/api-handler.d.ts.map +1 -0
  95. package/dist/provenance/api-handler.js +223 -0
  96. package/dist/provenance/api-handler.js.map +1 -0
  97. package/dist/provenance/api-types.d.ts +108 -0
  98. package/dist/provenance/api-types.d.ts.map +1 -0
  99. package/dist/provenance/api-types.js +9 -0
  100. package/dist/provenance/api-types.js.map +1 -0
  101. package/dist/provenance/index.d.ts +6 -0
  102. package/dist/provenance/index.d.ts.map +1 -0
  103. package/dist/provenance/index.js +3 -0
  104. package/dist/provenance/index.js.map +1 -0
  105. package/dist/provenance/provenance-engine.d.ts +63 -0
  106. package/dist/provenance/provenance-engine.d.ts.map +1 -0
  107. package/dist/provenance/provenance-engine.js +311 -0
  108. package/dist/provenance/provenance-engine.js.map +1 -0
  109. package/dist/provenance/types.d.ts +193 -0
  110. package/dist/provenance/types.d.ts.map +1 -0
  111. package/dist/provenance/types.js +9 -0
  112. package/dist/provenance/types.js.map +1 -0
  113. package/dist/tee/engine.d.ts.map +1 -1
  114. package/dist/tee/engine.js +14 -0
  115. package/dist/tee/engine.js.map +1 -1
  116. package/dist/warrant/engine.d.ts +24 -1
  117. package/dist/warrant/engine.d.ts.map +1 -1
  118. package/dist/warrant/engine.js +76 -1
  119. package/dist/warrant/engine.js.map +1 -1
  120. package/dist/zk/engine.d.ts.map +1 -1
  121. package/dist/zk/engine.js +7 -4
  122. package/dist/zk/engine.js.map +1 -1
  123. package/docs/SECURITY-PATCHES.md +170 -0
  124. package/package.json +17 -5
  125. package/src/__tests__/accountability.test.ts +308 -0
  126. package/src/__tests__/l1-verification-modes.test.ts +424 -0
  127. package/src/__tests__/phase1.benchmark.test.ts +94 -0
  128. package/src/__tests__/phase1.test.ts +0 -77
  129. package/src/__tests__/phase2-4.benchmark.test.ts +60 -0
  130. package/src/__tests__/phase2-4.test.ts +1 -52
  131. package/src/__tests__/provenance/api-handler.test.ts +356 -0
  132. package/src/__tests__/provenance/provenance-engine.test.ts +628 -0
  133. package/src/__tests__/sa-2026-008.test.ts +45 -0
  134. package/src/__tests__/sa-2026-009.test.ts +86 -0
  135. package/src/__tests__/sa-2026-010.test.ts +72 -0
  136. package/src/__tests__/sa-2026-012.test.ts +65 -0
  137. package/src/__tests__/sa-2026-nfc.test.ts +40 -0
  138. package/src/__tests__/security.test.ts +786 -0
  139. package/src/accountability/engine.ts +230 -0
  140. package/src/accountability/types.ts +58 -0
  141. package/src/checkpoint/engine.ts +4 -0
  142. package/src/context/__tests__/caret-v0.2.0.test.ts +860 -0
  143. package/src/context/__tests__/integration.test.ts +356 -0
  144. package/src/context/compose.ts +388 -0
  145. package/src/context/crypto/hash.ts +277 -0
  146. package/src/context/crypto/hmac.ts +253 -0
  147. package/src/context/crypto/index.ts +29 -0
  148. package/src/context/engine-v3.0-backup.ts +598 -0
  149. package/src/context/fragment.ts +454 -0
  150. package/src/context/index.ts +427 -0
  151. package/src/context/provenance.ts +380 -0
  152. package/src/context/resolve.ts +581 -0
  153. package/src/context/store.ts +503 -0
  154. package/src/context/types.ts +679 -0
  155. package/src/context/utils/atomic.ts +207 -0
  156. package/src/context/utils/credit.ts +224 -0
  157. package/src/context/utils/index.ts +13 -0
  158. package/src/context/utils/utility.ts +200 -0
  159. package/src/core/commitment.ts +129 -67
  160. package/src/core/crypto.ts +13 -0
  161. package/src/index.ts +62 -10
  162. package/src/mcca/engine.ts +5 -4
  163. package/src/physics/engine.ts +40 -3
  164. package/src/provenance/api-handler.ts +248 -0
  165. package/src/provenance/api-types.ts +112 -0
  166. package/src/provenance/index.ts +19 -0
  167. package/src/provenance/provenance-engine.ts +387 -0
  168. package/src/provenance/types.ts +211 -0
  169. package/src/tee/engine.ts +16 -0
  170. package/src/warrant/engine.ts +89 -1
  171. package/src/zk/engine.ts +8 -4
  172. package/tsconfig.json +1 -1
@@ -0,0 +1,427 @@
1
+ /**
2
+ * mdash v3.1 — Context Module (formerly Caret)
3
+ * @module @longarcstudios/mdash/context
4
+ * @version 3.1.0
5
+ *
6
+ * The context primitive runtime, now integrated into mdash.
7
+ *
8
+ * Architecture: LLM at the edges, cryptography at the core.
9
+ *
10
+ * v3.1.0: Merged into mdash from Caret v0.2.0
11
+ * - MdashActionCode for standardized action vocabulary
12
+ * - InfluenceBudget for MCCA-compliant bounds
13
+ * - InfluenceConstraint for influence-based resolution
14
+ * - Outcome<T> for Intent-Experience-Utility tracking
15
+ * - Atomic encoding with coreference resolution
16
+ * - Utility learning and credit assignment utilities
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { createCaret, requireInfluence } from '@longarcstudios/caret';
21
+ *
22
+ * const caret = createCaret({ seal_key: 'your-secret-key' });
23
+ *
24
+ * // Seal content into a fragment with MCCA constraints
25
+ * const fragment = await caret.seal({
26
+ * content: { message: 'Hello, world!' },
27
+ * source: 'app://example/greeting',
28
+ * attribution: 'system',
29
+ * constraints: [
30
+ * requireInfluence({ system: 0.45, user: 0.30, environment: 0.15, assistant: 0.10 })
31
+ * ],
32
+ * atomic_encoding: { enabled: true }
33
+ * });
34
+ *
35
+ * // Compose multiple fragments
36
+ * const manifold = await caret.compose([
37
+ * fragment,
38
+ * anotherFragment
39
+ * ], { ceiling: '8k tokens' });
40
+ *
41
+ * // Resolve through constraints
42
+ * const result = await caret.resolve(fragment);
43
+ * if (result.success) {
44
+ * console.log('Fragment is valid:', result.fragment);
45
+ * }
46
+ * ```
47
+ */
48
+
49
+ import type {
50
+ CaretRuntime,
51
+ CaretConfig,
52
+ ContextFragment,
53
+ Manifold,
54
+ SealOptions,
55
+ ComposeOptions,
56
+ ResolutionResult,
57
+ FragmentStore,
58
+ TrustLevel,
59
+ Attribution,
60
+ Constraint,
61
+ } from './types.js';
62
+
63
+ import {
64
+ createFragment,
65
+ verifyFragment,
66
+ setTokenizer,
67
+ } from './fragment.js';
68
+
69
+ import { compose } from './compose.js';
70
+ import { resolve } from './resolve.js';
71
+ import type { ResolutionContext } from './resolve.js';
72
+ import { MemoryStore } from './store.js';
73
+
74
+ // ============================================================================
75
+ // RUNTIME IMPLEMENTATION
76
+ // ============================================================================
77
+
78
+ /**
79
+ * Internal runtime state.
80
+ */
81
+ interface RuntimeState {
82
+ config: CaretConfig;
83
+ store: FragmentStore;
84
+ }
85
+
86
+ /**
87
+ * Create the Caret runtime.
88
+ */
89
+ function createRuntime(state: RuntimeState): CaretRuntime {
90
+ const { config, store } = state;
91
+
92
+ return {
93
+ async seal<T>(options: SealOptions<T>): Promise<ContextFragment<T>> {
94
+ const fragment = await createFragment({
95
+ content: options.content,
96
+ source: options.source,
97
+ attribution: options.attribution,
98
+ trust_level: options.trust_level ?? getDefaultTrust(options.attribution, config),
99
+ constraints: options.constraints as Constraint[] | undefined,
100
+ parent: options.parent,
101
+ signature: options.signature,
102
+ seal_key: config.seal_key,
103
+ });
104
+
105
+ // Store the fragment
106
+ await store.put(fragment);
107
+
108
+ return fragment;
109
+ },
110
+
111
+ async compose<T>(
112
+ fragments: readonly ContextFragment[],
113
+ options: ComposeOptions
114
+ ): Promise<Manifold<T>> {
115
+ return compose(fragments, options, config.seal_key);
116
+ },
117
+
118
+ async resolve<T>(
119
+ fragment: ContextFragment<T>,
120
+ context?: ResolutionContext
121
+ ): Promise<ResolutionResult<T>> {
122
+ return resolve(fragment, context);
123
+ },
124
+
125
+ async verify(fragment: ContextFragment): Promise<boolean> {
126
+ const result = await verifyFragment(fragment, config.seal_key);
127
+ return result.valid;
128
+ },
129
+
130
+ get store(): FragmentStore {
131
+ return store;
132
+ },
133
+ };
134
+ }
135
+
136
+ /**
137
+ * Get default trust level for attribution.
138
+ */
139
+ function getDefaultTrust(attribution: Attribution, config: CaretConfig): number {
140
+ switch (attribution) {
141
+ case 'system':
142
+ return 100;
143
+ case 'operator':
144
+ return 90;
145
+ case 'user':
146
+ return 70;
147
+ case 'derived':
148
+ return 60;
149
+ case 'agent':
150
+ return 50;
151
+ case 'external':
152
+ return 30;
153
+ default:
154
+ return config.default_trust as number;
155
+ }
156
+ }
157
+
158
+ // ============================================================================
159
+ // PUBLIC API
160
+ // ============================================================================
161
+
162
+ /**
163
+ * Options for creating a Caret instance.
164
+ */
165
+ export interface CreateCaretOptions {
166
+ /** Secret key for HMAC sealing (required) */
167
+ seal_key: string;
168
+
169
+ /** Default trust level for unspecified sources */
170
+ default_trust?: number;
171
+
172
+ /** Whether to enforce signature requirements */
173
+ require_signatures?: boolean;
174
+
175
+ /** Token counting strategy */
176
+ tokenizer?: 'cl100k' | 'p50k' | 'custom';
177
+
178
+ /** Custom tokenizer function */
179
+ custom_tokenizer?: (text: string) => number;
180
+
181
+ /** Custom store implementation */
182
+ store?: FragmentStore;
183
+ }
184
+
185
+ /**
186
+ * Create a Caret runtime instance.
187
+ *
188
+ * @param options - Configuration options
189
+ * @returns The Caret runtime
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * const caret = createCaret({
194
+ * seal_key: process.env.CARET_SEAL_KEY!,
195
+ * default_trust: 50,
196
+ * });
197
+ * ```
198
+ */
199
+ export function createCaret(options: CreateCaretOptions): CaretRuntime {
200
+ if (!options.seal_key) {
201
+ throw new Error('seal_key is required');
202
+ }
203
+
204
+ // Enforce minimum key length for security (P5 fix: increased to 32)
205
+ if (options.seal_key.length < 32) {
206
+ throw new Error('seal_key must be at least 32 characters for security');
207
+ }
208
+
209
+ // Build config with defaults
210
+ const config = {
211
+ seal_key: options.seal_key,
212
+ default_trust: (options.default_trust ?? 50) as TrustLevel,
213
+ require_signatures: options.require_signatures ?? false,
214
+ tokenizer: options.tokenizer ?? 'cl100k' as const,
215
+ custom_tokenizer: options.custom_tokenizer,
216
+ };
217
+
218
+ // Set up tokenizer if custom provided
219
+ if (options.custom_tokenizer) {
220
+ setTokenizer((content) => {
221
+ const text = typeof content === 'string' ? content : JSON.stringify(content);
222
+ return options.custom_tokenizer!(text) as any;
223
+ });
224
+ }
225
+
226
+ // Create or use provided store
227
+ const store = options.store ?? new MemoryStore(config.seal_key);
228
+
229
+ return createRuntime({ config, store });
230
+ }
231
+
232
+ // ============================================================================
233
+ // CONVENIENCE EXPORTS
234
+ // ============================================================================
235
+
236
+ // Re-export types
237
+ export type {
238
+ // Core types
239
+ ContextFragment,
240
+ Manifold,
241
+ FragmentId,
242
+ Hash,
243
+ Seal,
244
+ Timestamp,
245
+ SourceURI,
246
+ TokenCount,
247
+ TrustLevel,
248
+ Attribution,
249
+
250
+ // Semantic
251
+ SemanticUnit,
252
+ SemanticType,
253
+
254
+ // Provenance
255
+ ProvenanceRecord,
256
+ ProvenanceChain,
257
+
258
+ // Constraints
259
+ Constraint,
260
+ TimeConstraint,
261
+ ScopeConstraint,
262
+ TrustConstraint,
263
+ SignatureConstraint,
264
+ InfluenceConstraint,
265
+
266
+ // MCCA & Action Codes (Research Patterns)
267
+ MdashActionCode,
268
+ InfluenceBudget,
269
+
270
+ // Outcome Tracking (Intent-Experience-Utility)
271
+ Outcome,
272
+ ExecutionStep,
273
+ ExecutionTrace,
274
+ UtilityScore,
275
+ UtilityFeedback,
276
+ LiabilityShare,
277
+ StepCredit,
278
+ CreditAssignment,
279
+ CoreferenceContext,
280
+
281
+ // Composition
282
+ ComposeOptions,
283
+ CompositionStrategy,
284
+
285
+ // Resolution
286
+ ResolutionResult,
287
+ ConstraintViolation,
288
+
289
+ // Runtime
290
+ CaretRuntime,
291
+ CaretConfig,
292
+ SealOptions,
293
+ FragmentStore,
294
+ } from './types.js';
295
+
296
+ // Re-export constraint builders
297
+ export {
298
+ maxAge,
299
+ notBefore,
300
+ notAfter,
301
+ allowDomains,
302
+ requireTrust,
303
+ requireSignature,
304
+ requireInfluence,
305
+ requireMCCA,
306
+ } from './resolve.js';
307
+
308
+ // Re-export resolution context
309
+ export type { ResolutionContext } from './resolve.js';
310
+
311
+ // Re-export store utilities
312
+ export {
313
+ MemoryStore,
314
+ CachedStore,
315
+ CompositeStore,
316
+ ExtendedMemoryStore,
317
+ createStore,
318
+ } from './store.js';
319
+
320
+ export type { EmbeddingFunction } from './store.js';
321
+
322
+ // Re-export fragment utilities
323
+ export {
324
+ verifyFragment,
325
+ serializeFragment,
326
+ deserializeFragment,
327
+ getFragmentAge,
328
+ getFragmentTrust,
329
+ isFragmentExpired,
330
+ countTokens,
331
+ setTokenizer,
332
+ } from './fragment.js';
333
+
334
+ // Re-export composition utilities
335
+ export {
336
+ verifyManifold,
337
+ getManifoldStats,
338
+ fitsInBudget,
339
+ parseTokenCeiling,
340
+ } from './compose.js';
341
+
342
+ // Re-export provenance utilities
343
+ export {
344
+ createProvenanceRecord,
345
+ createProvenanceChain,
346
+ extendProvenanceChain,
347
+ flattenChain,
348
+ verifyChain,
349
+ mergeChains,
350
+ getMinTrust,
351
+ getMaxTrust,
352
+ } from './provenance.js';
353
+
354
+ // Re-export crypto utilities
355
+ export {
356
+ sha256,
357
+ sha256Object,
358
+ sha256Binary,
359
+ rollingHash,
360
+ verifyHash,
361
+ isValidHash,
362
+ deterministicStringify,
363
+ } from './crypto/hash.js';
364
+
365
+ export {
366
+ hmacSeal,
367
+ hmacVerify,
368
+ isValidSeal,
369
+ } from './crypto/hmac.js';
370
+
371
+ // Re-export type guards
372
+ export {
373
+ isHash,
374
+ isFragmentId,
375
+ isTimestamp,
376
+ isTrustLevel,
377
+ isTokenCount,
378
+ isContextFragment,
379
+ isValidInfluenceBudget,
380
+ isMdashActionCode,
381
+ sumInfluence,
382
+ } from './types.js';
383
+
384
+ // Re-export research pattern utilities
385
+ export {
386
+ // Atomic encoding
387
+ resolveCoreferences,
388
+ createDefaultContext,
389
+ hasUnresolvedCoreferences,
390
+ extractEntityIds,
391
+ canonicalEncode,
392
+ computeCommitmentHash,
393
+ isValidSubject,
394
+ isValidTimestamp,
395
+ } from './utils/atomic.js';
396
+
397
+ export {
398
+ // Utility learning
399
+ updateUtilityScore,
400
+ updateOutcomeUtility,
401
+ createInitialUtility,
402
+ createUtilityFromHistory,
403
+ predictSuccess,
404
+ expectedUtility,
405
+ compareUtility,
406
+ selectBestOutcome,
407
+ } from './utils/utility.js';
408
+
409
+ export {
410
+ // Credit assignment
411
+ assignCredit,
412
+ validateCreditAssignment,
413
+ attributeLiability,
414
+ validateLiabilityShares,
415
+ createDefaultInfluenceBudget,
416
+ isMCCACompliant,
417
+ normalizeInfluenceBudget,
418
+ addInfluenceBudgets,
419
+ computeTraceInfluence,
420
+ } from './utils/credit.js';
421
+
422
+ // Version export
423
+ export const VERSION = '3.1.0';
424
+ export const FORMERLY = 'Caret v0.2.0';
425
+
426
+ /** Alias for createCaret - use this for new code */
427
+ export const createContext = createCaret;