@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.
- package/README.md +86 -23
- package/SECURITY.md +254 -0
- package/dist/accountability/engine.d.ts +27 -0
- package/dist/accountability/engine.d.ts.map +1 -0
- package/dist/accountability/engine.js +148 -0
- package/dist/accountability/engine.js.map +1 -0
- package/dist/accountability/types.d.ts +46 -0
- package/dist/accountability/types.d.ts.map +1 -0
- package/dist/accountability/types.js +8 -0
- package/dist/accountability/types.js.map +1 -0
- package/dist/checkpoint/engine.d.ts.map +1 -1
- package/dist/checkpoint/engine.js +4 -0
- package/dist/checkpoint/engine.js.map +1 -1
- package/dist/context/compose.d.ts +62 -0
- package/dist/context/compose.d.ts.map +1 -0
- package/dist/context/compose.js +286 -0
- package/dist/context/compose.js.map +1 -0
- package/dist/context/crypto/hash.d.ts +100 -0
- package/dist/context/crypto/hash.d.ts.map +1 -0
- package/dist/context/crypto/hash.js +248 -0
- package/dist/context/crypto/hash.js.map +1 -0
- package/dist/context/crypto/hmac.d.ts +80 -0
- package/dist/context/crypto/hmac.d.ts.map +1 -0
- package/dist/context/crypto/hmac.js +192 -0
- package/dist/context/crypto/hmac.js.map +1 -0
- package/dist/context/crypto/index.d.ts +7 -0
- package/dist/context/crypto/index.d.ts.map +1 -0
- package/dist/context/crypto/index.js +7 -0
- package/dist/context/crypto/index.js.map +1 -0
- package/dist/context/engine-v3.0-backup.d.ts +197 -0
- package/dist/context/engine-v3.0-backup.d.ts.map +1 -0
- package/dist/context/engine-v3.0-backup.js +392 -0
- package/dist/context/engine-v3.0-backup.js.map +1 -0
- package/dist/context/fragment.d.ts +99 -0
- package/dist/context/fragment.d.ts.map +1 -0
- package/dist/context/fragment.js +316 -0
- package/dist/context/fragment.js.map +1 -0
- package/dist/context/index.d.ts +99 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +180 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/provenance.d.ts +80 -0
- package/dist/context/provenance.d.ts.map +1 -0
- package/dist/context/provenance.js +294 -0
- package/dist/context/provenance.js.map +1 -0
- package/dist/context/resolve.d.ts +106 -0
- package/dist/context/resolve.d.ts.map +1 -0
- package/dist/context/resolve.js +440 -0
- package/dist/context/resolve.js.map +1 -0
- package/dist/context/store.d.ts +156 -0
- package/dist/context/store.d.ts.map +1 -0
- package/dist/context/store.js +396 -0
- package/dist/context/store.js.map +1 -0
- package/dist/context/types.d.ts +463 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/context/types.js +94 -0
- package/dist/context/types.js.map +1 -0
- package/dist/context/utils/atomic.d.ts +76 -0
- package/dist/context/utils/atomic.d.ts.map +1 -0
- package/dist/context/utils/atomic.js +159 -0
- package/dist/context/utils/atomic.js.map +1 -0
- package/dist/context/utils/credit.d.ts +65 -0
- package/dist/context/utils/credit.d.ts.map +1 -0
- package/dist/context/utils/credit.js +164 -0
- package/dist/context/utils/credit.js.map +1 -0
- package/dist/context/utils/index.d.ts +13 -0
- package/dist/context/utils/index.d.ts.map +1 -0
- package/dist/context/utils/index.js +13 -0
- package/dist/context/utils/index.js.map +1 -0
- package/dist/context/utils/utility.d.ts +63 -0
- package/dist/context/utils/utility.d.ts.map +1 -0
- package/dist/context/utils/utility.js +141 -0
- package/dist/context/utils/utility.js.map +1 -0
- package/dist/core/commitment.d.ts +25 -2
- package/dist/core/commitment.d.ts.map +1 -1
- package/dist/core/commitment.js +44 -6
- package/dist/core/commitment.js.map +1 -1
- package/dist/core/crypto.d.ts +2 -0
- package/dist/core/crypto.d.ts.map +1 -1
- package/dist/core/crypto.js +12 -0
- package/dist/core/crypto.js.map +1 -1
- package/dist/index.d.ts +11 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +35 -10
- package/dist/index.js.map +1 -1
- package/dist/mcca/engine.d.ts.map +1 -1
- package/dist/mcca/engine.js +5 -4
- package/dist/mcca/engine.js.map +1 -1
- package/dist/physics/engine.d.ts +1 -0
- package/dist/physics/engine.d.ts.map +1 -1
- package/dist/physics/engine.js +36 -2
- package/dist/physics/engine.js.map +1 -1
- package/dist/provenance/api-handler.d.ts +45 -0
- package/dist/provenance/api-handler.d.ts.map +1 -0
- package/dist/provenance/api-handler.js +223 -0
- package/dist/provenance/api-handler.js.map +1 -0
- package/dist/provenance/api-types.d.ts +108 -0
- package/dist/provenance/api-types.d.ts.map +1 -0
- package/dist/provenance/api-types.js +9 -0
- package/dist/provenance/api-types.js.map +1 -0
- package/dist/provenance/index.d.ts +6 -0
- package/dist/provenance/index.d.ts.map +1 -0
- package/dist/provenance/index.js +3 -0
- package/dist/provenance/index.js.map +1 -0
- package/dist/provenance/provenance-engine.d.ts +63 -0
- package/dist/provenance/provenance-engine.d.ts.map +1 -0
- package/dist/provenance/provenance-engine.js +311 -0
- package/dist/provenance/provenance-engine.js.map +1 -0
- package/dist/provenance/types.d.ts +193 -0
- package/dist/provenance/types.d.ts.map +1 -0
- package/dist/provenance/types.js +9 -0
- package/dist/provenance/types.js.map +1 -0
- package/dist/tee/engine.d.ts.map +1 -1
- package/dist/tee/engine.js +14 -0
- package/dist/tee/engine.js.map +1 -1
- package/dist/warrant/engine.d.ts +24 -1
- package/dist/warrant/engine.d.ts.map +1 -1
- package/dist/warrant/engine.js +76 -1
- package/dist/warrant/engine.js.map +1 -1
- package/dist/zk/engine.d.ts.map +1 -1
- package/dist/zk/engine.js +7 -4
- package/dist/zk/engine.js.map +1 -1
- package/docs/SECURITY-PATCHES.md +170 -0
- package/package.json +17 -5
- package/src/__tests__/accountability.test.ts +308 -0
- package/src/__tests__/l1-verification-modes.test.ts +424 -0
- package/src/__tests__/phase1.benchmark.test.ts +94 -0
- package/src/__tests__/phase1.test.ts +0 -77
- package/src/__tests__/phase2-4.benchmark.test.ts +60 -0
- package/src/__tests__/phase2-4.test.ts +1 -52
- package/src/__tests__/provenance/api-handler.test.ts +356 -0
- package/src/__tests__/provenance/provenance-engine.test.ts +628 -0
- package/src/__tests__/sa-2026-008.test.ts +45 -0
- package/src/__tests__/sa-2026-009.test.ts +86 -0
- package/src/__tests__/sa-2026-010.test.ts +72 -0
- package/src/__tests__/sa-2026-012.test.ts +65 -0
- package/src/__tests__/sa-2026-nfc.test.ts +40 -0
- package/src/__tests__/security.test.ts +786 -0
- package/src/accountability/engine.ts +230 -0
- package/src/accountability/types.ts +58 -0
- package/src/checkpoint/engine.ts +4 -0
- package/src/context/__tests__/caret-v0.2.0.test.ts +860 -0
- package/src/context/__tests__/integration.test.ts +356 -0
- package/src/context/compose.ts +388 -0
- package/src/context/crypto/hash.ts +277 -0
- package/src/context/crypto/hmac.ts +253 -0
- package/src/context/crypto/index.ts +29 -0
- package/src/context/engine-v3.0-backup.ts +598 -0
- package/src/context/fragment.ts +454 -0
- package/src/context/index.ts +427 -0
- package/src/context/provenance.ts +380 -0
- package/src/context/resolve.ts +581 -0
- package/src/context/store.ts +503 -0
- package/src/context/types.ts +679 -0
- package/src/context/utils/atomic.ts +207 -0
- package/src/context/utils/credit.ts +224 -0
- package/src/context/utils/index.ts +13 -0
- package/src/context/utils/utility.ts +200 -0
- package/src/core/commitment.ts +129 -67
- package/src/core/crypto.ts +13 -0
- package/src/index.ts +62 -10
- package/src/mcca/engine.ts +5 -4
- package/src/physics/engine.ts +40 -3
- package/src/provenance/api-handler.ts +248 -0
- package/src/provenance/api-types.ts +112 -0
- package/src/provenance/index.ts +19 -0
- package/src/provenance/provenance-engine.ts +387 -0
- package/src/provenance/types.ts +211 -0
- package/src/tee/engine.ts +16 -0
- package/src/warrant/engine.ts +89 -1
- package/src/zk/engine.ts +8 -4
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caret — Context Primitive Engine
|
|
3
|
+
* @module @longarcstudios/caret
|
|
4
|
+
* @version 0.2.0
|
|
5
|
+
*
|
|
6
|
+
* Core type definitions for the Caret context primitive system.
|
|
7
|
+
* All types are immutable by design — no mutations after creation.
|
|
8
|
+
*
|
|
9
|
+
* Architecture: LLM at the edges, cryptography at the core.
|
|
10
|
+
*
|
|
11
|
+
* v0.2.0: Research patterns integration
|
|
12
|
+
* - MdashActionCode for standardized action vocabulary
|
|
13
|
+
* - InfluenceBudget for MCCA-compliant bounds
|
|
14
|
+
* - InfluenceConstraint for influence-based resolution
|
|
15
|
+
* - Outcome<T> for Intent-Experience-Utility tracking
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// ============================================================================
|
|
19
|
+
// PRIMITIVE TYPES
|
|
20
|
+
// ============================================================================
|
|
21
|
+
|
|
22
|
+
/** SHA-256 hash represented as hex string (64 characters) */
|
|
23
|
+
export type Hash = string & { readonly __brand: 'Hash' };
|
|
24
|
+
|
|
25
|
+
/** UUID v4 for unique identification */
|
|
26
|
+
export type FragmentId = string & { readonly __brand: 'FragmentId' };
|
|
27
|
+
|
|
28
|
+
/** ISO-8601 timestamp with timezone */
|
|
29
|
+
export type Timestamp = string & { readonly __brand: 'Timestamp' };
|
|
30
|
+
|
|
31
|
+
/** HMAC-SHA256 seal for tamper detection */
|
|
32
|
+
export type Seal = string & { readonly __brand: 'Seal' };
|
|
33
|
+
|
|
34
|
+
/** URI-style source identifier (e.g., "org://hr/policies/pto-2024") */
|
|
35
|
+
export type SourceURI = string & { readonly __brand: 'SourceURI' };
|
|
36
|
+
|
|
37
|
+
/** Token count (non-negative integer) */
|
|
38
|
+
export type TokenCount = number & { readonly __brand: 'TokenCount' };
|
|
39
|
+
|
|
40
|
+
// ============================================================================
|
|
41
|
+
// TRUST & ATTRIBUTION
|
|
42
|
+
// ============================================================================
|
|
43
|
+
|
|
44
|
+
/** Trust level from 0-100 indicating confidence in source */
|
|
45
|
+
export type TrustLevel = number & { readonly __brand: 'TrustLevel' };
|
|
46
|
+
|
|
47
|
+
/** Attribution sources for context fragments */
|
|
48
|
+
export type Attribution =
|
|
49
|
+
| 'system' // System-generated (highest trust)
|
|
50
|
+
| 'operator' // Human operator input
|
|
51
|
+
| 'user' // End-user input
|
|
52
|
+
| 'agent' // AI agent generated
|
|
53
|
+
| 'external' // External API/service
|
|
54
|
+
| 'derived'; // Computed from other fragments
|
|
55
|
+
|
|
56
|
+
// ============================================================================
|
|
57
|
+
// MCCA & ACTION CODES (Research Patterns v3.1.0)
|
|
58
|
+
// ============================================================================
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* mdash action codes for atomic attestation encoding.
|
|
62
|
+
* Every action in the system maps to exactly one code.
|
|
63
|
+
*/
|
|
64
|
+
export type MdashActionCode =
|
|
65
|
+
| 'INVOKE' // Warrant activation
|
|
66
|
+
| 'APPROVE' // Authority grant
|
|
67
|
+
| 'ATTEST' // State certification
|
|
68
|
+
| 'REVOKE' // Authority withdrawal
|
|
69
|
+
| 'CHECKPOINT' // State snapshot
|
|
70
|
+
| 'ESCALATE' // Boundary approach
|
|
71
|
+
| 'TERMINATE'; // Execution end
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* MCCA-compliant influence budget.
|
|
75
|
+
* Constrains how much each context source can affect agent behavior.
|
|
76
|
+
*
|
|
77
|
+
* MCCA bounds:
|
|
78
|
+
* - system: ≥0.40 (operator instructions dominate)
|
|
79
|
+
* - user: ≤0.35 (user input bounded)
|
|
80
|
+
* - environment: ≤0.20 (tool outputs limited)
|
|
81
|
+
* - assistant: ≤0.25 (self-influence capped)
|
|
82
|
+
*/
|
|
83
|
+
export interface InfluenceBudget {
|
|
84
|
+
/** System prompt influence (≥0.40 per MCCA) */
|
|
85
|
+
readonly system: number;
|
|
86
|
+
/** User input influence (≤0.35 per MCCA) */
|
|
87
|
+
readonly user: number;
|
|
88
|
+
/** Environment influence (≤0.20 per MCCA) */
|
|
89
|
+
readonly environment: number;
|
|
90
|
+
/** Assistant self-influence (≤0.25 per MCCA) */
|
|
91
|
+
readonly assistant: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Default MCCA influence budget */
|
|
95
|
+
export const DEFAULT_MCCA_BUDGET: InfluenceBudget = {
|
|
96
|
+
system: 0.45,
|
|
97
|
+
user: 0.30,
|
|
98
|
+
environment: 0.15,
|
|
99
|
+
assistant: 0.10,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** Default trust levels by attribution */
|
|
103
|
+
export const DEFAULT_TRUST_LEVELS: Record<Attribution, number> = {
|
|
104
|
+
system: 100,
|
|
105
|
+
operator: 90,
|
|
106
|
+
user: 70,
|
|
107
|
+
derived: 60,
|
|
108
|
+
agent: 50,
|
|
109
|
+
external: 30,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** Provenance record for a single fragment */
|
|
113
|
+
export interface ProvenanceRecord {
|
|
114
|
+
readonly source: SourceURI;
|
|
115
|
+
readonly attribution: Attribution;
|
|
116
|
+
readonly trust_level: TrustLevel;
|
|
117
|
+
readonly timestamp: Timestamp;
|
|
118
|
+
readonly parent_hash: Hash | null;
|
|
119
|
+
readonly signature?: string; // Optional Ed25519 signature
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Chain of provenance records (immutable linked list) */
|
|
123
|
+
export interface ProvenanceChain {
|
|
124
|
+
readonly head: ProvenanceRecord;
|
|
125
|
+
readonly tail: ProvenanceChain | null;
|
|
126
|
+
readonly length: number;
|
|
127
|
+
readonly chain_hash: Hash;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ============================================================================
|
|
131
|
+
// SEMANTIC UNITS
|
|
132
|
+
// ============================================================================
|
|
133
|
+
|
|
134
|
+
/** Semantic type classification for content */
|
|
135
|
+
export type SemanticType =
|
|
136
|
+
| 'text' // Plain text content
|
|
137
|
+
| 'structured' // JSON-serializable object
|
|
138
|
+
| 'policy' // Governance policy
|
|
139
|
+
| 'state' // Agent/system state
|
|
140
|
+
| 'reference' // Reference to external resource
|
|
141
|
+
| 'composite'; // Composed from multiple fragments
|
|
142
|
+
|
|
143
|
+
/** Semantic unit — the content payload of a fragment */
|
|
144
|
+
export interface SemanticUnit<T = unknown> {
|
|
145
|
+
readonly type: SemanticType;
|
|
146
|
+
readonly data: T;
|
|
147
|
+
readonly token_count: TokenCount;
|
|
148
|
+
readonly compression_ratio?: number; // If compressed
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ============================================================================
|
|
152
|
+
// CONSTRAINTS
|
|
153
|
+
// ============================================================================
|
|
154
|
+
|
|
155
|
+
/** Time-based constraint */
|
|
156
|
+
export interface TimeConstraint {
|
|
157
|
+
readonly kind: 'time';
|
|
158
|
+
readonly max_age_ms?: number;
|
|
159
|
+
readonly not_before?: Timestamp;
|
|
160
|
+
readonly not_after?: Timestamp;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Scope-based constraint */
|
|
164
|
+
export interface ScopeConstraint {
|
|
165
|
+
readonly kind: 'scope';
|
|
166
|
+
readonly allowed_domains: readonly string[];
|
|
167
|
+
readonly denied_domains?: readonly string[];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Trust-based constraint */
|
|
171
|
+
export interface TrustConstraint {
|
|
172
|
+
readonly kind: 'trust';
|
|
173
|
+
readonly minimum_trust: TrustLevel;
|
|
174
|
+
readonly required_attributions?: readonly Attribution[];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Signature requirement constraint */
|
|
178
|
+
export interface SignatureConstraint {
|
|
179
|
+
readonly kind: 'signature';
|
|
180
|
+
readonly required: boolean;
|
|
181
|
+
readonly allowed_signers?: readonly string[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Influence budget constraint (MCCA compliance).
|
|
186
|
+
* Enforces bounds on context source influence.
|
|
187
|
+
*/
|
|
188
|
+
export interface InfluenceConstraint {
|
|
189
|
+
readonly kind: 'influence';
|
|
190
|
+
/** MCCA-compliant influence budget */
|
|
191
|
+
readonly budget: InfluenceBudget;
|
|
192
|
+
/** Enforcement mode */
|
|
193
|
+
readonly enforcement: 'strict' | 'advisory';
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Union of all constraint types */
|
|
197
|
+
export type Constraint =
|
|
198
|
+
| TimeConstraint
|
|
199
|
+
| ScopeConstraint
|
|
200
|
+
| TrustConstraint
|
|
201
|
+
| SignatureConstraint
|
|
202
|
+
| InfluenceConstraint;
|
|
203
|
+
|
|
204
|
+
// ============================================================================
|
|
205
|
+
// CONTEXT FRAGMENT
|
|
206
|
+
// ============================================================================
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* ContextFragment — The atomic unit of structured context.
|
|
210
|
+
*
|
|
211
|
+
* Immutable by construction. Once sealed, a fragment cannot be modified.
|
|
212
|
+
* Any "update" creates a new fragment with updated provenance.
|
|
213
|
+
*/
|
|
214
|
+
export interface ContextFragment<T = unknown> {
|
|
215
|
+
/** Unique identifier (UUID v4) */
|
|
216
|
+
readonly id: FragmentId;
|
|
217
|
+
|
|
218
|
+
/** SHA-256 hash of the sealed content */
|
|
219
|
+
readonly hash: Hash;
|
|
220
|
+
|
|
221
|
+
/** The semantic content payload */
|
|
222
|
+
readonly content: SemanticUnit<T>;
|
|
223
|
+
|
|
224
|
+
/** Full provenance chain */
|
|
225
|
+
readonly provenance: ProvenanceChain;
|
|
226
|
+
|
|
227
|
+
/** When this fragment was sealed */
|
|
228
|
+
readonly sealed_at: Timestamp;
|
|
229
|
+
|
|
230
|
+
/** HMAC seal for tamper detection */
|
|
231
|
+
readonly seal: Seal;
|
|
232
|
+
|
|
233
|
+
/** Constraints on fragment usage */
|
|
234
|
+
readonly constraints: readonly Constraint[];
|
|
235
|
+
|
|
236
|
+
/** Version for schema evolution */
|
|
237
|
+
readonly version: '1.0' | 'v3.1';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ============================================================================
|
|
241
|
+
// MANIFOLD (COMPOSED CONTEXT)
|
|
242
|
+
// ============================================================================
|
|
243
|
+
|
|
244
|
+
/** Composition strategy for manifold creation */
|
|
245
|
+
export type CompositionStrategy =
|
|
246
|
+
| 'concatenate' // Simple concatenation
|
|
247
|
+
| 'priority' // Higher trust first
|
|
248
|
+
| 'recency' // Most recent first
|
|
249
|
+
| 'semantic'; // Semantic deduplication
|
|
250
|
+
|
|
251
|
+
/** Options for manifold composition */
|
|
252
|
+
export interface ComposeOptions {
|
|
253
|
+
/** Maximum token ceiling for composed output */
|
|
254
|
+
readonly ceiling: TokenCount | string; // e.g., "8k tokens"
|
|
255
|
+
|
|
256
|
+
/** Composition strategy */
|
|
257
|
+
readonly strategy?: CompositionStrategy;
|
|
258
|
+
|
|
259
|
+
/** Whether to preserve full provenance chain */
|
|
260
|
+
readonly preserve_provenance?: boolean;
|
|
261
|
+
|
|
262
|
+
/** Minimum trust level for included fragments */
|
|
263
|
+
readonly min_trust?: TrustLevel;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Manifold — A composed context from multiple fragments.
|
|
268
|
+
*
|
|
269
|
+
* Represents a "view" over fragments, optimized for a token ceiling.
|
|
270
|
+
* Maintains provenance links to all source fragments.
|
|
271
|
+
*/
|
|
272
|
+
export interface Manifold<T = unknown> {
|
|
273
|
+
/** Unique identifier for this manifold */
|
|
274
|
+
readonly id: FragmentId;
|
|
275
|
+
|
|
276
|
+
/** Hash of the composed content */
|
|
277
|
+
readonly hash: Hash;
|
|
278
|
+
|
|
279
|
+
/** The composed semantic content */
|
|
280
|
+
readonly content: SemanticUnit<T>;
|
|
281
|
+
|
|
282
|
+
/** Source fragment IDs in composition order */
|
|
283
|
+
readonly sources: readonly FragmentId[];
|
|
284
|
+
|
|
285
|
+
/** Aggregated provenance (merged chains) */
|
|
286
|
+
readonly provenance: ProvenanceChain;
|
|
287
|
+
|
|
288
|
+
/** Composition metadata */
|
|
289
|
+
readonly composition: {
|
|
290
|
+
readonly strategy: CompositionStrategy;
|
|
291
|
+
readonly ceiling: TokenCount;
|
|
292
|
+
readonly actual_tokens: TokenCount;
|
|
293
|
+
readonly fragments_included: number;
|
|
294
|
+
readonly fragments_excluded: number;
|
|
295
|
+
readonly compression_applied: boolean;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/** When this manifold was created */
|
|
299
|
+
readonly created_at: Timestamp;
|
|
300
|
+
|
|
301
|
+
/** HMAC seal */
|
|
302
|
+
readonly seal: Seal;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ============================================================================
|
|
306
|
+
// RESOLUTION
|
|
307
|
+
// ============================================================================
|
|
308
|
+
|
|
309
|
+
/** Result of resolving a fragment through constraints */
|
|
310
|
+
export interface ResolutionResult<T = unknown> {
|
|
311
|
+
readonly success: boolean;
|
|
312
|
+
readonly fragment: ContextFragment<T> | null;
|
|
313
|
+
readonly violations: readonly ConstraintViolation[];
|
|
314
|
+
readonly resolved_at: Timestamp;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Details of a constraint violation */
|
|
318
|
+
export interface ConstraintViolation {
|
|
319
|
+
readonly constraint: Constraint;
|
|
320
|
+
readonly reason: string;
|
|
321
|
+
readonly severity: 'error' | 'warning';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ============================================================================
|
|
325
|
+
// STORAGE INTERFACE
|
|
326
|
+
// ============================================================================
|
|
327
|
+
|
|
328
|
+
/** Abstract storage interface for fragments */
|
|
329
|
+
export interface FragmentStore {
|
|
330
|
+
/** Store a fragment */
|
|
331
|
+
put(fragment: ContextFragment): Promise<void>;
|
|
332
|
+
|
|
333
|
+
/** Retrieve a fragment by ID */
|
|
334
|
+
get(id: FragmentId): Promise<ContextFragment | null>;
|
|
335
|
+
|
|
336
|
+
/** Retrieve a fragment by hash */
|
|
337
|
+
getByHash(hash: Hash): Promise<ContextFragment | null>;
|
|
338
|
+
|
|
339
|
+
/** Query fragments by source */
|
|
340
|
+
queryBySource(source: SourceURI): Promise<readonly ContextFragment[]>;
|
|
341
|
+
|
|
342
|
+
/** Query fragments by time range */
|
|
343
|
+
queryByTime(from: Timestamp, to: Timestamp): Promise<readonly ContextFragment[]>;
|
|
344
|
+
|
|
345
|
+
/** Delete a fragment (soft delete — marks as deleted) */
|
|
346
|
+
delete(id: FragmentId): Promise<boolean>;
|
|
347
|
+
|
|
348
|
+
/** Verify integrity of stored fragment */
|
|
349
|
+
verify(id: FragmentId): Promise<boolean>;
|
|
350
|
+
|
|
351
|
+
// === Three-Layer Index (Optional) ===
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Layer 2: Keyword search (lexical).
|
|
355
|
+
* Optional method — implementations may return undefined.
|
|
356
|
+
*
|
|
357
|
+
* @param keywords - Keywords to match
|
|
358
|
+
* @param matchMode - 'any' matches if any keyword found, 'all' requires all keywords
|
|
359
|
+
* @returns Matching fragments
|
|
360
|
+
*/
|
|
361
|
+
queryByKeywords?(
|
|
362
|
+
keywords: string[],
|
|
363
|
+
matchMode: 'any' | 'all'
|
|
364
|
+
): Promise<readonly ContextFragment[]>;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Layer 1: Semantic similarity search.
|
|
368
|
+
* Optional method — implementations may return undefined.
|
|
369
|
+
*
|
|
370
|
+
* @param embedding - Query embedding vector
|
|
371
|
+
* @param threshold - Minimum similarity threshold (0.0-1.0)
|
|
372
|
+
* @param limit - Maximum results to return
|
|
373
|
+
* @returns Matching fragments with similarity scores
|
|
374
|
+
*/
|
|
375
|
+
queryBySimilarity?(
|
|
376
|
+
embedding: Float32Array,
|
|
377
|
+
threshold: number,
|
|
378
|
+
limit: number
|
|
379
|
+
): Promise<readonly { fragment: ContextFragment; similarity: number }[]>;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// ============================================================================
|
|
383
|
+
// RUNTIME INTERFACE
|
|
384
|
+
// ============================================================================
|
|
385
|
+
|
|
386
|
+
/** Caret runtime configuration */
|
|
387
|
+
export interface CaretConfig {
|
|
388
|
+
/** Secret key for HMAC sealing */
|
|
389
|
+
seal_key: string;
|
|
390
|
+
|
|
391
|
+
/** Default trust level for unspecified sources */
|
|
392
|
+
default_trust: TrustLevel;
|
|
393
|
+
|
|
394
|
+
/** Whether to enforce signature requirements */
|
|
395
|
+
require_signatures: boolean;
|
|
396
|
+
|
|
397
|
+
/** Token counting strategy */
|
|
398
|
+
tokenizer: 'cl100k' | 'p50k' | 'custom';
|
|
399
|
+
|
|
400
|
+
/** Custom tokenizer function */
|
|
401
|
+
custom_tokenizer?: (text: string) => number;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** The Caret runtime instance */
|
|
405
|
+
export interface CaretRuntime {
|
|
406
|
+
/** Seal content into a fragment */
|
|
407
|
+
seal<T>(options: SealOptions<T>): Promise<ContextFragment<T>>;
|
|
408
|
+
|
|
409
|
+
/** Compose fragments into a manifold */
|
|
410
|
+
compose<T>(fragments: readonly ContextFragment[], options: ComposeOptions): Promise<Manifold<T>>;
|
|
411
|
+
|
|
412
|
+
/** Resolve a fragment through constraints */
|
|
413
|
+
resolve<T>(fragment: ContextFragment<T>): Promise<ResolutionResult<T>>;
|
|
414
|
+
|
|
415
|
+
/** Verify a fragment's integrity */
|
|
416
|
+
verify(fragment: ContextFragment): Promise<boolean>;
|
|
417
|
+
|
|
418
|
+
/** Get the fragment store */
|
|
419
|
+
readonly store: FragmentStore;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Options for sealing content */
|
|
423
|
+
export interface SealOptions<T = unknown> {
|
|
424
|
+
/** The content to seal */
|
|
425
|
+
readonly content: T;
|
|
426
|
+
|
|
427
|
+
/** Source URI for provenance */
|
|
428
|
+
readonly source: SourceURI | string;
|
|
429
|
+
|
|
430
|
+
/** Attribution type */
|
|
431
|
+
readonly attribution: Attribution;
|
|
432
|
+
|
|
433
|
+
/** Trust level (0-100) */
|
|
434
|
+
readonly trust_level?: number;
|
|
435
|
+
|
|
436
|
+
/** Constraints to apply */
|
|
437
|
+
readonly constraints?: readonly Constraint[];
|
|
438
|
+
|
|
439
|
+
/** Parent fragment for provenance chain */
|
|
440
|
+
readonly parent?: ContextFragment;
|
|
441
|
+
|
|
442
|
+
/** Optional signature */
|
|
443
|
+
readonly signature?: string;
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Enable atomic encoding with coreference resolution.
|
|
447
|
+
* Transforms ambiguous text into explicit references.
|
|
448
|
+
*/
|
|
449
|
+
readonly atomic_encoding?: {
|
|
450
|
+
readonly enabled: boolean;
|
|
451
|
+
readonly context?: CoreferenceContext;
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** Context for coreference resolution */
|
|
456
|
+
export interface CoreferenceContext {
|
|
457
|
+
/** Reference time for relative time resolution */
|
|
458
|
+
readonly referenceTime: string;
|
|
459
|
+
/** Last mentioned agent ID */
|
|
460
|
+
readonly lastAgentId?: string;
|
|
461
|
+
/** Last mentioned warrant ID */
|
|
462
|
+
readonly lastWarrantId?: string;
|
|
463
|
+
/** Last mentioned checkpoint ID */
|
|
464
|
+
readonly lastCheckpointId?: string;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// ============================================================================
|
|
468
|
+
// TYPE GUARDS
|
|
469
|
+
// ============================================================================
|
|
470
|
+
|
|
471
|
+
/** Type guard for Hash */
|
|
472
|
+
export function isHash(value: unknown): value is Hash {
|
|
473
|
+
return typeof value === 'string' && /^[a-f0-9]{64}$/i.test(value);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** Type guard for FragmentId */
|
|
477
|
+
export function isFragmentId(value: unknown): value is FragmentId {
|
|
478
|
+
return typeof value === 'string' &&
|
|
479
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** Type guard for Timestamp */
|
|
483
|
+
export function isTimestamp(value: unknown): value is Timestamp {
|
|
484
|
+
if (typeof value !== 'string') return false;
|
|
485
|
+
const date = new Date(value);
|
|
486
|
+
return !isNaN(date.getTime());
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** Type guard for TrustLevel */
|
|
490
|
+
export function isTrustLevel(value: unknown): value is TrustLevel {
|
|
491
|
+
return typeof value === 'number' && value >= 0 && value <= 100;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** Type guard for TokenCount */
|
|
495
|
+
export function isTokenCount(value: unknown): value is TokenCount {
|
|
496
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** Type guard for ContextFragment */
|
|
500
|
+
export function isContextFragment(value: unknown): value is ContextFragment {
|
|
501
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
502
|
+
const v = value as Record<string, unknown>;
|
|
503
|
+
return (
|
|
504
|
+
isFragmentId(v.id) &&
|
|
505
|
+
isHash(v.hash) &&
|
|
506
|
+
typeof v.content === 'object' &&
|
|
507
|
+
typeof v.provenance === 'object' &&
|
|
508
|
+
isTimestamp(v.sealed_at) &&
|
|
509
|
+
typeof v.seal === 'string' &&
|
|
510
|
+
Array.isArray(v.constraints) &&
|
|
511
|
+
(v.version === 'v3.1' || v.version === '1.0')
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// ============================================================================
|
|
516
|
+
// OUTCOME TRACKING (Intent-Experience-Utility)
|
|
517
|
+
// ============================================================================
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Single step in execution trace.
|
|
521
|
+
* Records action, state observation, and influence consumed.
|
|
522
|
+
*/
|
|
523
|
+
export interface ExecutionStep {
|
|
524
|
+
/** Unique step identifier */
|
|
525
|
+
readonly step_id: string;
|
|
526
|
+
/** ISO 8601 timestamp */
|
|
527
|
+
readonly timestamp: Timestamp;
|
|
528
|
+
/** Action performed */
|
|
529
|
+
readonly action: MdashActionCode;
|
|
530
|
+
/** Hash of observed state after action */
|
|
531
|
+
readonly observation_hash: Hash;
|
|
532
|
+
/** Influence consumed by this step */
|
|
533
|
+
readonly influence_consumed: InfluenceBudget;
|
|
534
|
+
/** Checkpoint hash if step created checkpoint */
|
|
535
|
+
readonly checkpoint_hash?: Hash;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Complete execution trace for a warrant/task.
|
|
540
|
+
*/
|
|
541
|
+
export interface ExecutionTrace {
|
|
542
|
+
/** Unique trace identifier */
|
|
543
|
+
readonly trace_id: FragmentId;
|
|
544
|
+
/** Ordered execution steps */
|
|
545
|
+
readonly steps: readonly ExecutionStep[];
|
|
546
|
+
/** Final execution status */
|
|
547
|
+
readonly outcome: 'success' | 'failure' | 'partial' | 'aborted';
|
|
548
|
+
/** Terminal state hash */
|
|
549
|
+
readonly terminal_state: Hash;
|
|
550
|
+
/** Total influence consumed across all steps */
|
|
551
|
+
readonly total_influence: InfluenceBudget;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Learned utility score for an outcome.
|
|
556
|
+
* Updated incrementally via feedback.
|
|
557
|
+
*/
|
|
558
|
+
export interface UtilityScore {
|
|
559
|
+
/** Learned outcome value [-1.0, 1.0] */
|
|
560
|
+
readonly score: number;
|
|
561
|
+
/** Estimation confidence [0.0, 1.0] */
|
|
562
|
+
readonly confidence: number;
|
|
563
|
+
/** Number of times score has been updated */
|
|
564
|
+
readonly update_count: number;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Agent's contribution to outcome (liability attribution).
|
|
569
|
+
*/
|
|
570
|
+
export interface LiabilityShare {
|
|
571
|
+
/** Agent identifier */
|
|
572
|
+
readonly agent_id: string;
|
|
573
|
+
/** Fraction of outcome attributed (0.0-1.0) */
|
|
574
|
+
readonly contribution: number;
|
|
575
|
+
/** Attestation IDs supporting attribution */
|
|
576
|
+
readonly evidence: readonly string[];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Outcome<T> — Intent-Experience-Utility triplet.
|
|
581
|
+
*
|
|
582
|
+
* Wraps a fragment result with execution context:
|
|
583
|
+
* - Intent: What was authorized
|
|
584
|
+
* - Experience: What actually happened
|
|
585
|
+
* - Utility: How valuable the outcome was
|
|
586
|
+
*/
|
|
587
|
+
export interface Outcome<T = unknown> {
|
|
588
|
+
/** The fragment produced by execution */
|
|
589
|
+
readonly result: ContextFragment<T>;
|
|
590
|
+
|
|
591
|
+
/** Original authorization (Intent) */
|
|
592
|
+
readonly intent: {
|
|
593
|
+
/** Natural language objective */
|
|
594
|
+
readonly objective: string;
|
|
595
|
+
/** Constraints on execution */
|
|
596
|
+
readonly constraints: readonly Constraint[];
|
|
597
|
+
/** MCCA influence budget */
|
|
598
|
+
readonly influence_budget?: InfluenceBudget;
|
|
599
|
+
/** Verifiable success criteria */
|
|
600
|
+
readonly success_criteria?: readonly string[];
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
/** Execution trace (Experience) */
|
|
604
|
+
readonly trace: ExecutionTrace;
|
|
605
|
+
|
|
606
|
+
/** Learned utility score (Utility) */
|
|
607
|
+
readonly utility: UtilityScore;
|
|
608
|
+
|
|
609
|
+
/** Liability distribution across agents */
|
|
610
|
+
readonly liability: readonly LiabilityShare[];
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Feedback for utility score updates.
|
|
615
|
+
*/
|
|
616
|
+
export interface UtilityFeedback {
|
|
617
|
+
/** Observed outcome reward [-1.0, 1.0] */
|
|
618
|
+
readonly reward: number;
|
|
619
|
+
/** Source of feedback */
|
|
620
|
+
readonly source: 'environment' | 'human' | 'system';
|
|
621
|
+
/** ISO 8601 timestamp */
|
|
622
|
+
readonly timestamp: Timestamp;
|
|
623
|
+
/** Optional explanation */
|
|
624
|
+
readonly rationale?: string;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Credit assignment for a single execution step.
|
|
629
|
+
*/
|
|
630
|
+
export interface StepCredit {
|
|
631
|
+
/** Step identifier */
|
|
632
|
+
readonly step_id: string;
|
|
633
|
+
/** Credit value (portion of terminal reward) */
|
|
634
|
+
readonly credit: number;
|
|
635
|
+
/** Influence consumed by this step */
|
|
636
|
+
readonly influence: number;
|
|
637
|
+
/** Influence as fraction of total */
|
|
638
|
+
readonly influence_fraction: number;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Complete credit assignment for an execution.
|
|
643
|
+
*/
|
|
644
|
+
export interface CreditAssignment {
|
|
645
|
+
/** Trace identifier */
|
|
646
|
+
readonly trace_id: FragmentId;
|
|
647
|
+
/** Terminal reward being distributed */
|
|
648
|
+
readonly terminal_reward: number;
|
|
649
|
+
/** Credit per step */
|
|
650
|
+
readonly step_credits: readonly StepCredit[];
|
|
651
|
+
/** Timestamp of assignment */
|
|
652
|
+
readonly assigned_at: Timestamp;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// ============================================================================
|
|
656
|
+
// RESEARCH PATTERN TYPE GUARDS
|
|
657
|
+
// ============================================================================
|
|
658
|
+
|
|
659
|
+
/** Type guard for valid MCCA influence budget */
|
|
660
|
+
export function isValidInfluenceBudget(budget: InfluenceBudget): boolean {
|
|
661
|
+
return (
|
|
662
|
+
budget.system >= 0.40 &&
|
|
663
|
+
budget.user <= 0.35 &&
|
|
664
|
+
budget.environment <= 0.20 &&
|
|
665
|
+
budget.assistant <= 0.25 &&
|
|
666
|
+
Math.abs(budget.system + budget.user + budget.environment + budget.assistant - 1.0) < 0.01
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** Sum all components of an influence budget */
|
|
671
|
+
export function sumInfluence(budget: InfluenceBudget): number {
|
|
672
|
+
return budget.system + budget.user + budget.environment + budget.assistant;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** Type guard for MdashActionCode */
|
|
676
|
+
export function isMdashActionCode(value: unknown): value is MdashActionCode {
|
|
677
|
+
const validCodes = ['INVOKE', 'APPROVE', 'ATTEST', 'REVOKE', 'CHECKPOINT', 'ESCALATE', 'TERMINATE'];
|
|
678
|
+
return typeof value === 'string' && validCodes.includes(value);
|
|
679
|
+
}
|