@private.me/xcontinuity 2.1.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/AGENTS.md +123 -0
- package/LICENSE.md +26 -0
- package/MIGRATING.md +77 -0
- package/README.md +601 -0
- package/dist/adjudicator.d.ts +75 -0
- package/dist/adjudicator.js +184 -0
- package/dist/cascade.d.ts +157 -0
- package/dist/cascade.js +323 -0
- package/dist/chronicle.d.ts +76 -0
- package/dist/chronicle.js +173 -0
- package/dist/cjs/adjudicator.js +189 -0
- package/dist/cjs/cascade.js +328 -0
- package/dist/cjs/chronicle.js +178 -0
- package/dist/cjs/enforcement.js +108 -0
- package/dist/cjs/errors.js +72 -0
- package/dist/cjs/index.js +108 -0
- package/dist/cjs/memory-runtime.js +129 -0
- package/dist/cjs/memory-session.js +134 -0
- package/dist/cjs/mission.js +178 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/provenance.js +192 -0
- package/dist/cjs/ratification.js +322 -0
- package/dist/cjs/reverse-xorida.js +506 -0
- package/dist/cjs/session.js +273 -0
- package/dist/cjs/state-serializer.js +300 -0
- package/dist/cjs/store-memory.js +33 -0
- package/dist/cjs/trust.js +133 -0
- package/dist/cjs/types.js +59 -0
- package/dist/enforcement.d.ts +40 -0
- package/dist/enforcement.js +104 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +68 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +43 -0
- package/dist/memory-runtime.d.ts +36 -0
- package/dist/memory-runtime.js +125 -0
- package/dist/memory-session.d.ts +38 -0
- package/dist/memory-session.js +97 -0
- package/dist/mission.d.ts +68 -0
- package/dist/mission.js +172 -0
- package/dist/provenance.d.ts +54 -0
- package/dist/provenance.js +182 -0
- package/dist/ratification.d.ts +113 -0
- package/dist/ratification.js +317 -0
- package/dist/reverse-xorida.d.ts +174 -0
- package/dist/reverse-xorida.js +490 -0
- package/dist/session.d.ts +102 -0
- package/dist/session.js +269 -0
- package/dist/state-serializer.d.ts +37 -0
- package/dist/state-serializer.js +294 -0
- package/dist/store-memory.d.ts +18 -0
- package/dist/store-memory.js +29 -0
- package/dist/trust.d.ts +76 -0
- package/dist/trust.js +121 -0
- package/dist/types.d.ts +320 -0
- package/dist/types.js +56 -0
- package/llms.txt +43 -0
- package/package.json +125 -0
- package/share1.dat +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @private.me/xcontinuity — Trust Tier Management
|
|
4
|
+
*
|
|
5
|
+
* Assigns and manages trust tiers for memory entries based on
|
|
6
|
+
* provenance verification, contradiction history, and temporal decay.
|
|
7
|
+
*
|
|
8
|
+
* Three tiers:
|
|
9
|
+
* ratified (3) — signed + verified by a known author
|
|
10
|
+
* inherited (2) — unsigned from a trusted source, or decayed from ratified
|
|
11
|
+
* quarantined (1) — unverified, contradicted, or failed verification
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.DEFAULT_MAX_AGE = exports.TRUST_TIER_RANK = void 0;
|
|
15
|
+
exports.baselineTier = baselineTier;
|
|
16
|
+
exports.applyContradictionDowngrade = applyContradictionDowngrade;
|
|
17
|
+
exports.isDecayed = isDecayed;
|
|
18
|
+
exports.applyDecay = applyDecay;
|
|
19
|
+
exports.effectiveTier = effectiveTier;
|
|
20
|
+
exports.compareTiers = compareTiers;
|
|
21
|
+
exports.isMoreTrusted = isMoreTrusted;
|
|
22
|
+
exports.leastTrusted = leastTrusted;
|
|
23
|
+
const types_js_1 = require("./types.js");
|
|
24
|
+
Object.defineProperty(exports, "TRUST_TIER_RANK", { enumerable: true, get: function () { return types_js_1.TRUST_TIER_RANK; } });
|
|
25
|
+
Object.defineProperty(exports, "DEFAULT_MAX_AGE", { enumerable: true, get: function () { return types_js_1.DEFAULT_MAX_AGE; } });
|
|
26
|
+
/**
|
|
27
|
+
* Determine the baseline trust tier for an entry based on its provenance.
|
|
28
|
+
*
|
|
29
|
+
* @param provenance - The provenance record (if present)
|
|
30
|
+
* @param signatureVerified - Whether the Ed25519 signature was verified
|
|
31
|
+
* @returns The baseline trust tier
|
|
32
|
+
*/
|
|
33
|
+
function baselineTier(provenance, signatureVerified) {
|
|
34
|
+
if (!provenance)
|
|
35
|
+
return 'inherited';
|
|
36
|
+
if (signatureVerified)
|
|
37
|
+
return 'ratified';
|
|
38
|
+
return 'quarantined';
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Apply contradiction-based downgrade to a trust tier.
|
|
42
|
+
*
|
|
43
|
+
* Rules:
|
|
44
|
+
* - 0 contradictions: no change
|
|
45
|
+
* - 1+ contradictions on inherited: downgrade to quarantined
|
|
46
|
+
* - 1+ contradictions on ratified: downgrade to inherited
|
|
47
|
+
* - quarantined stays quarantined regardless
|
|
48
|
+
*
|
|
49
|
+
* @param currentTier - The current trust tier
|
|
50
|
+
* @param contradictionCount - Number of active contradictions
|
|
51
|
+
* @returns The adjusted trust tier
|
|
52
|
+
*/
|
|
53
|
+
function applyContradictionDowngrade(currentTier, contradictionCount) {
|
|
54
|
+
if (contradictionCount <= 0)
|
|
55
|
+
return currentTier;
|
|
56
|
+
switch (currentTier) {
|
|
57
|
+
case 'ratified':
|
|
58
|
+
return 'inherited';
|
|
59
|
+
case 'inherited':
|
|
60
|
+
return 'quarantined';
|
|
61
|
+
case 'quarantined':
|
|
62
|
+
return 'quarantined';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Check whether a trust tier has decayed past its maxAge TTL.
|
|
67
|
+
*
|
|
68
|
+
* A ratified entry past its maxAge downgrades to inherited.
|
|
69
|
+
* Inherited and quarantined entries are not affected by decay.
|
|
70
|
+
*
|
|
71
|
+
* @param entry - The memory entry to check
|
|
72
|
+
* @param now - Current timestamp (ms). Defaults to Date.now().
|
|
73
|
+
* @returns true if the entry has expired and should be downgraded
|
|
74
|
+
*/
|
|
75
|
+
function isDecayed(entry, now) {
|
|
76
|
+
if (entry.tier !== 'ratified')
|
|
77
|
+
return false;
|
|
78
|
+
if (entry.maxAge === undefined)
|
|
79
|
+
return false;
|
|
80
|
+
if (!Number.isFinite(entry.maxAge))
|
|
81
|
+
return false; // Infinity = no decay
|
|
82
|
+
const ratifiedAt = entry.ratifiedAt ?? entry.provenance?.timestamp ?? 0;
|
|
83
|
+
const elapsed = (now ?? Date.now()) - ratifiedAt;
|
|
84
|
+
return elapsed > entry.maxAge;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Apply temporal decay to a trust tier.
|
|
88
|
+
*
|
|
89
|
+
* @param entry - The memory entry to evaluate
|
|
90
|
+
* @param now - Current timestamp (ms). Defaults to Date.now().
|
|
91
|
+
* @returns The effective trust tier after decay
|
|
92
|
+
*/
|
|
93
|
+
function applyDecay(entry, now) {
|
|
94
|
+
if (isDecayed(entry, now))
|
|
95
|
+
return 'inherited';
|
|
96
|
+
return entry.tier;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get the effective trust tier for an entry, considering both
|
|
100
|
+
* contradictions and temporal decay.
|
|
101
|
+
*
|
|
102
|
+
* @param entry - The memory entry
|
|
103
|
+
* @param now - Current timestamp (ms). Defaults to Date.now().
|
|
104
|
+
* @returns The effective trust tier
|
|
105
|
+
*/
|
|
106
|
+
function effectiveTier(entry, now) {
|
|
107
|
+
const afterDecay = applyDecay(entry, now);
|
|
108
|
+
return applyContradictionDowngrade(afterDecay, entry.contradictions.length);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Compare two trust tiers. Returns positive if a > b, negative if a < b, 0 if equal.
|
|
112
|
+
*/
|
|
113
|
+
function compareTiers(a, b) {
|
|
114
|
+
return types_js_1.TRUST_TIER_RANK[a] - types_js_1.TRUST_TIER_RANK[b];
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Check if tier `a` is strictly more trusted than tier `b`.
|
|
118
|
+
*/
|
|
119
|
+
function isMoreTrusted(a, b) {
|
|
120
|
+
return types_js_1.TRUST_TIER_RANK[a] > types_js_1.TRUST_TIER_RANK[b];
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get the least trusted tier from a list.
|
|
124
|
+
*/
|
|
125
|
+
function leastTrusted(...tiers) {
|
|
126
|
+
let min = 'ratified';
|
|
127
|
+
for (const t of tiers) {
|
|
128
|
+
if (types_js_1.TRUST_TIER_RANK[t] < types_js_1.TRUST_TIER_RANK[min]) {
|
|
129
|
+
min = t;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return min;
|
|
133
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @private.me/xcontinuity — Type definitions and constants
|
|
4
|
+
*
|
|
5
|
+
* All types, interfaces, TLV type codes, and constants for the
|
|
6
|
+
* cryptographic state continuity substrate.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.DEFAULT_ENFORCEMENT_CONFIG = exports.DEFAULT_MAX_AGE = exports.TRUST_TIER_RANK = exports.DEFAULT_RUNTIME_MEMORY_CONFIG = exports.DEFAULT_SPLIT_CONFIG = exports.VALUE_TYPE = exports.CONTINUITY_TLV = void 0;
|
|
10
|
+
/* ── TLV Type Codes (0x80+ to avoid collision with shared TLV_TYPE 0x01-0x0d) ── */
|
|
11
|
+
exports.CONTINUITY_TLV = {
|
|
12
|
+
/** State metadata header */
|
|
13
|
+
STATE_METADATA: 0x80,
|
|
14
|
+
/** Agent identifier */
|
|
15
|
+
AGENT_ID: 0x81,
|
|
16
|
+
/** Session identifier */
|
|
17
|
+
SESSION_ID: 0x82,
|
|
18
|
+
/** Sequence number (uint32) */
|
|
19
|
+
SEQUENCE_NUMBER: 0x83,
|
|
20
|
+
/** Timestamp (float64 ms) */
|
|
21
|
+
TIMESTAMP: 0x84,
|
|
22
|
+
/** Tag string */
|
|
23
|
+
TAG: 0x85,
|
|
24
|
+
/** State key-value entry */
|
|
25
|
+
STATE_ENTRY: 0x86,
|
|
26
|
+
/** Key name (UTF-8) */
|
|
27
|
+
ENTRY_KEY: 0x87,
|
|
28
|
+
/** Entry value (typed) */
|
|
29
|
+
ENTRY_VALUE: 0x88,
|
|
30
|
+
/** Description string */
|
|
31
|
+
DESCRIPTION: 0x89,
|
|
32
|
+
/** Checksum (SHA-256) */
|
|
33
|
+
CHECKSUM: 0x8a,
|
|
34
|
+
};
|
|
35
|
+
/* ── Value Type Markers ── */
|
|
36
|
+
exports.VALUE_TYPE = {
|
|
37
|
+
STRING: 0x01,
|
|
38
|
+
NUMBER: 0x02,
|
|
39
|
+
BOOLEAN: 0x03,
|
|
40
|
+
BYTES: 0x04,
|
|
41
|
+
NULL: 0x05,
|
|
42
|
+
JSON: 0x06,
|
|
43
|
+
};
|
|
44
|
+
/** Default split configuration: 3-of-2 threshold. */
|
|
45
|
+
exports.DEFAULT_SPLIT_CONFIG = { n: 3, k: 2 };
|
|
46
|
+
exports.DEFAULT_RUNTIME_MEMORY_CONFIG = {
|
|
47
|
+
maxEntries: 1000,
|
|
48
|
+
};
|
|
49
|
+
/** Numeric ranking for trust tier comparison (higher = more trusted). */
|
|
50
|
+
exports.TRUST_TIER_RANK = {
|
|
51
|
+
ratified: 3,
|
|
52
|
+
inherited: 2,
|
|
53
|
+
quarantined: 1,
|
|
54
|
+
};
|
|
55
|
+
/** Default TTL for trust tier decay (30 days in ms). */
|
|
56
|
+
exports.DEFAULT_MAX_AGE = 30 * 24 * 60 * 60 * 1000;
|
|
57
|
+
exports.DEFAULT_ENFORCEMENT_CONFIG = {
|
|
58
|
+
escalationThreshold: 3,
|
|
59
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity — Enforcement Loop
|
|
3
|
+
*
|
|
4
|
+
* Evaluates proposed actions against mission constraints and produces
|
|
5
|
+
* enforcement decisions: allow, reject (with reason), rewrite (with
|
|
6
|
+
* suggested alternative), or escalate (on repeated violations).
|
|
7
|
+
*
|
|
8
|
+
* Tracks violation history per agent for escalation logic.
|
|
9
|
+
*/
|
|
10
|
+
import type { Result } from '@private.me/shared';
|
|
11
|
+
import type { ProposedAction, EnforcementResult, EnforcementConfig, AuthorRef } from './types.js';
|
|
12
|
+
import type { ContinuityError } from './errors.js';
|
|
13
|
+
import type { MissionGuard } from './mission.js';
|
|
14
|
+
export declare class EnforcementLoop {
|
|
15
|
+
private readonly guard;
|
|
16
|
+
private readonly config;
|
|
17
|
+
/** Violation counts per agent. */
|
|
18
|
+
private readonly violations;
|
|
19
|
+
constructor(guard: MissionGuard, config?: Partial<EnforcementConfig>);
|
|
20
|
+
/**
|
|
21
|
+
* Check a proposed action against the enforcement loop.
|
|
22
|
+
*
|
|
23
|
+
* Flow:
|
|
24
|
+
* 1. Evaluate against mission guard
|
|
25
|
+
* 2. If allowed → return allow
|
|
26
|
+
* 3. If denied and has suggestion → return rewrite
|
|
27
|
+
* 4. If denied without suggestion → increment violations
|
|
28
|
+
* 5. If violations >= threshold → return escalate
|
|
29
|
+
* 6. Otherwise → return reject
|
|
30
|
+
*/
|
|
31
|
+
check(action: ProposedAction): Result<EnforcementResult, ContinuityError>;
|
|
32
|
+
/** Get the current violation count for an agent. */
|
|
33
|
+
getViolationCount(author: AuthorRef): number;
|
|
34
|
+
/** Reset violation count for an agent. */
|
|
35
|
+
resetViolations(author: AuthorRef): void;
|
|
36
|
+
/** Reset all violation counts. */
|
|
37
|
+
resetAllViolations(): void;
|
|
38
|
+
/** Get the escalation threshold. */
|
|
39
|
+
get escalationThreshold(): number;
|
|
40
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity — Enforcement Loop
|
|
3
|
+
*
|
|
4
|
+
* Evaluates proposed actions against mission constraints and produces
|
|
5
|
+
* enforcement decisions: allow, reject (with reason), rewrite (with
|
|
6
|
+
* suggested alternative), or escalate (on repeated violations).
|
|
7
|
+
*
|
|
8
|
+
* Tracks violation history per agent for escalation logic.
|
|
9
|
+
*/
|
|
10
|
+
import { ok, err } from '@private.me/shared';
|
|
11
|
+
import { DEFAULT_ENFORCEMENT_CONFIG } from './types.js';
|
|
12
|
+
export class EnforcementLoop {
|
|
13
|
+
guard;
|
|
14
|
+
config;
|
|
15
|
+
/** Violation counts per agent. */
|
|
16
|
+
violations = new Map();
|
|
17
|
+
constructor(guard, config) {
|
|
18
|
+
this.guard = guard;
|
|
19
|
+
this.config = {
|
|
20
|
+
...DEFAULT_ENFORCEMENT_CONFIG,
|
|
21
|
+
...config,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Check a proposed action against the enforcement loop.
|
|
26
|
+
*
|
|
27
|
+
* Flow:
|
|
28
|
+
* 1. Evaluate against mission guard
|
|
29
|
+
* 2. If allowed → return allow
|
|
30
|
+
* 3. If denied and has suggestion → return rewrite
|
|
31
|
+
* 4. If denied without suggestion → increment violations
|
|
32
|
+
* 5. If violations >= threshold → return escalate
|
|
33
|
+
* 6. Otherwise → return reject
|
|
34
|
+
*/
|
|
35
|
+
check(action) {
|
|
36
|
+
const guardResult = this.guard.evaluate(action);
|
|
37
|
+
// Guard evaluation error — propagate
|
|
38
|
+
if (!guardResult.ok) {
|
|
39
|
+
return err(guardResult.error);
|
|
40
|
+
}
|
|
41
|
+
const constraintResult = guardResult.value;
|
|
42
|
+
// Action allowed
|
|
43
|
+
if (constraintResult.allowed) {
|
|
44
|
+
return ok({
|
|
45
|
+
decision: 'allow',
|
|
46
|
+
action,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// Action denied — check for rewrite suggestion
|
|
50
|
+
if (constraintResult.suggestion) {
|
|
51
|
+
// Verify the suggestion itself passes constraints
|
|
52
|
+
const suggestionCheck = this.guard.evaluate(constraintResult.suggestion);
|
|
53
|
+
if (suggestionCheck.ok && suggestionCheck.value.allowed) {
|
|
54
|
+
return ok({
|
|
55
|
+
decision: 'rewrite',
|
|
56
|
+
action,
|
|
57
|
+
reason: constraintResult.reason,
|
|
58
|
+
rewrittenAction: constraintResult.suggestion,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// Suggestion also fails — fall through to reject/escalate
|
|
62
|
+
}
|
|
63
|
+
// Increment violation count for this agent
|
|
64
|
+
const currentCount = (this.violations.get(action.author) ?? 0) + 1;
|
|
65
|
+
this.violations.set(action.author, currentCount);
|
|
66
|
+
// Check escalation threshold
|
|
67
|
+
if (currentCount >= this.config.escalationThreshold) {
|
|
68
|
+
const result = {
|
|
69
|
+
decision: 'escalate',
|
|
70
|
+
action,
|
|
71
|
+
reason: constraintResult.reason,
|
|
72
|
+
violationCount: currentCount,
|
|
73
|
+
};
|
|
74
|
+
// Fire escalation callback if configured
|
|
75
|
+
if (this.config.onEscalate) {
|
|
76
|
+
this.config.onEscalate(result);
|
|
77
|
+
}
|
|
78
|
+
return ok(result);
|
|
79
|
+
}
|
|
80
|
+
// Reject
|
|
81
|
+
return ok({
|
|
82
|
+
decision: 'reject',
|
|
83
|
+
action,
|
|
84
|
+
reason: constraintResult.reason,
|
|
85
|
+
violationCount: currentCount,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** Get the current violation count for an agent. */
|
|
89
|
+
getViolationCount(author) {
|
|
90
|
+
return this.violations.get(author) ?? 0;
|
|
91
|
+
}
|
|
92
|
+
/** Reset violation count for an agent. */
|
|
93
|
+
resetViolations(author) {
|
|
94
|
+
this.violations.delete(author);
|
|
95
|
+
}
|
|
96
|
+
/** Reset all violation counts. */
|
|
97
|
+
resetAllViolations() {
|
|
98
|
+
this.violations.clear();
|
|
99
|
+
}
|
|
100
|
+
/** Get the escalation threshold. */
|
|
101
|
+
get escalationThreshold() {
|
|
102
|
+
return this.config.escalationThreshold;
|
|
103
|
+
}
|
|
104
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity — Error hierarchy
|
|
3
|
+
*
|
|
4
|
+
* Follows the pattern from @private.me/crypto errors.
|
|
5
|
+
* All fallible public functions return Result<T, ContinuityError>.
|
|
6
|
+
*/
|
|
7
|
+
export type SerializationErrorCode = 'SERIALIZE_FAILED' | 'DESERIALIZE_FAILED' | 'CHECKSUM_MISMATCH' | 'INVALID_TLV' | 'MISSING_FIELD' | 'INVALID_VALUE_TYPE';
|
|
8
|
+
export type SplitErrorCode = 'SPLIT_FAILED' | 'RECONSTRUCT_FAILED' | 'HMAC_FAILURE' | 'INSUFFICIENT_SHARES' | 'INVALID_SHARES' | 'PADDING_ERROR' | 'DELTA_SIZE_MISMATCH';
|
|
9
|
+
export type SessionErrorCode = 'SESSION_CLOSED' | 'SESSION_SUSPENDED' | 'SESSION_ACTIVE' | 'NO_SNAPSHOTS' | 'SNAPSHOT_NOT_FOUND' | 'INVALID_CONFIG';
|
|
10
|
+
export type ChronicleErrorCode = 'ENTRY_NOT_FOUND' | 'INVALID_QUERY';
|
|
11
|
+
export type StoreErrorCode = 'STORE_PUT_FAILED' | 'STORE_GET_FAILED' | 'STORE_DELETE_FAILED';
|
|
12
|
+
export type ProvenanceErrorCode = 'INVALID_SIGNATURE' | 'MISSING_AUTHOR' | 'HASH_CHAIN_BREAK' | 'SIGNATURE_EXPIRED';
|
|
13
|
+
export type TrustErrorCode = 'TIER_DOWNGRADE' | 'QUARANTINED_ENTRY' | 'CONTRADICTION_DETECTED' | 'TRUST_DECAY_EXPIRED';
|
|
14
|
+
export type AdjudicatorErrorCode = 'CONSENSUS_FAILED' | 'POLICY_VIOLATION' | 'QUORUM_NOT_MET' | 'NO_CANDIDATES';
|
|
15
|
+
export type MissionErrorCode = 'CONSTRAINT_VIOLATION' | 'AUTHORITY_EXPIRED' | 'SCOPE_EXCEEDED' | 'INVALID_MISSION_SIGNATURE' | 'NO_ACTIVE_MISSION';
|
|
16
|
+
export type EnforcementErrorCode = 'ACTION_REJECTED' | 'ESCALATION_TRIGGERED' | 'REWRITE_FAILED';
|
|
17
|
+
export type ContinuityErrorCode = SerializationErrorCode | SplitErrorCode | SessionErrorCode | ChronicleErrorCode | StoreErrorCode | ProvenanceErrorCode | TrustErrorCode | AdjudicatorErrorCode | MissionErrorCode | EnforcementErrorCode;
|
|
18
|
+
export interface ContinuityError {
|
|
19
|
+
readonly code: ContinuityErrorCode;
|
|
20
|
+
readonly message: string;
|
|
21
|
+
readonly subCode?: string;
|
|
22
|
+
}
|
|
23
|
+
/** Human-readable descriptions for all error codes. */
|
|
24
|
+
export declare const ERROR_DETAILS: Record<ContinuityErrorCode, string>;
|
|
25
|
+
export declare function continuityError(code: ContinuityErrorCode, message: string, subCode?: string): ContinuityError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity — Error hierarchy
|
|
3
|
+
*
|
|
4
|
+
* Follows the pattern from @private.me/crypto errors.
|
|
5
|
+
* All fallible public functions return Result<T, ContinuityError>.
|
|
6
|
+
*/
|
|
7
|
+
/* ── Error Details Map ── */
|
|
8
|
+
/** Human-readable descriptions for all error codes. */
|
|
9
|
+
export const ERROR_DETAILS = {
|
|
10
|
+
// Serialization
|
|
11
|
+
SERIALIZE_FAILED: 'State serialization failed. Check that all state values are valid types (string, number, boolean, Uint8Array, null, or plain object).',
|
|
12
|
+
DESERIALIZE_FAILED: 'State deserialization failed. The TLV data may be corrupt or in an unsupported format.',
|
|
13
|
+
CHECKSUM_MISMATCH: 'SHA-256 checksum verification failed. The serialized data has been modified since snapshot creation.',
|
|
14
|
+
INVALID_TLV: 'Invalid TLV structure. The byte stream has a truncated header or value exceeding data length.',
|
|
15
|
+
MISSING_FIELD: 'Required field missing from serialized data.',
|
|
16
|
+
INVALID_VALUE_TYPE: 'Unknown value type code in serialized entry. Expected STRING(0x01), NUMBER(0x02), BOOLEAN(0x03), BYTES(0x04), NULL(0x05), or JSON(0x06).',
|
|
17
|
+
// Split
|
|
18
|
+
SPLIT_FAILED: 'XorIDA threshold split failed. Verify n >= 2 and 2 <= k <= n.',
|
|
19
|
+
RECONSTRUCT_FAILED: 'XorIDA share reconstruction failed. Ensure shares are valid and indices are correct.',
|
|
20
|
+
HMAC_FAILURE: 'HMAC-SHA256 verification failed after reconstruction. One or more shares may be corrupted or tampered.',
|
|
21
|
+
INSUFFICIENT_SHARES: 'Not enough shares for reconstruction. Provide at least k shares (threshold).',
|
|
22
|
+
INVALID_SHARES: 'Share validation failed. All shares must have matching stateId, n, and k values.',
|
|
23
|
+
PADDING_ERROR: 'PKCS#7 unpadding failed after reconstruction. Data may be corrupted.',
|
|
24
|
+
DELTA_SIZE_MISMATCH: 'Old and new padded state sizes differ. Incremental update requires same padded length; falling back to fresh split.',
|
|
25
|
+
// Session
|
|
26
|
+
SESSION_CLOSED: 'Session is closed (terminal state). Create a new session to continue.',
|
|
27
|
+
SESSION_SUSPENDED: 'Session is suspended. Call resume() before performing operations.',
|
|
28
|
+
SESSION_ACTIVE: 'Session is already active. Suspend before attempting to resume.',
|
|
29
|
+
NO_SNAPSHOTS: 'No snapshots available. Create at least one snapshot before restoring.',
|
|
30
|
+
SNAPSHOT_NOT_FOUND: 'Snapshot not found in store. The stateId may be incorrect or the snapshot was deleted.',
|
|
31
|
+
INVALID_CONFIG: 'Invalid session configuration. Verify agentId and store are provided.',
|
|
32
|
+
// Chronicle
|
|
33
|
+
ENTRY_NOT_FOUND: 'Chronicle entry not found. The stateId or sequence number does not exist in the history.',
|
|
34
|
+
INVALID_QUERY: 'Invalid chronicle query parameters.',
|
|
35
|
+
// Store
|
|
36
|
+
STORE_PUT_FAILED: 'Failed to write split state to store backend.',
|
|
37
|
+
STORE_GET_FAILED: 'Failed to read split state from store backend.',
|
|
38
|
+
STORE_DELETE_FAILED: 'Failed to delete split state from store backend.',
|
|
39
|
+
// Provenance
|
|
40
|
+
INVALID_SIGNATURE: 'Ed25519 signature verification failed. The entry may have been tampered with or signed by a different key.',
|
|
41
|
+
MISSING_AUTHOR: 'Provenance record is missing the author field. Every signed entry must have an AuthorRef.',
|
|
42
|
+
HASH_CHAIN_BREAK: 'Parent hash does not match the expected predecessor. The chronicle chain has a gap or reorder.',
|
|
43
|
+
SIGNATURE_EXPIRED: 'The signature timestamp is older than the maximum allowed age for this trust tier.',
|
|
44
|
+
// Trust
|
|
45
|
+
TIER_DOWNGRADE: 'Trust tier was downgraded due to contradictions or policy violation.',
|
|
46
|
+
QUARANTINED_ENTRY: 'Entry is quarantined and cannot be used until re-verified or ratified.',
|
|
47
|
+
CONTRADICTION_DETECTED: 'A contradicting entry was found for the same key with incompatible value.',
|
|
48
|
+
TRUST_DECAY_EXPIRED: 'Entry exceeded its maxAge TTL and was downgraded from ratified to inherited.',
|
|
49
|
+
// Adjudicator
|
|
50
|
+
CONSENSUS_FAILED: 'Multi-agent consensus could not be reached. Quorum threshold not met.',
|
|
51
|
+
POLICY_VIOLATION: 'The proposed resolution violates the active policy adjudicator rules.',
|
|
52
|
+
QUORUM_NOT_MET: 'Insufficient agent views to form a quorum for consensus adjudication.',
|
|
53
|
+
NO_CANDIDATES: 'No candidate entries provided for adjudication.',
|
|
54
|
+
// Mission
|
|
55
|
+
CONSTRAINT_VIOLATION: 'The proposed action violates a hard constraint defined by the active mission.',
|
|
56
|
+
AUTHORITY_EXPIRED: 'The mission authority signature has expired. A new mission must be issued.',
|
|
57
|
+
SCOPE_EXCEEDED: 'The proposed action falls outside the scopes authorized by the active mission.',
|
|
58
|
+
INVALID_MISSION_SIGNATURE: 'Ed25519 signature on the mission record failed verification.',
|
|
59
|
+
NO_ACTIVE_MISSION: 'No active mission is set. A mission must be loaded before constraint evaluation.',
|
|
60
|
+
// Enforcement
|
|
61
|
+
ACTION_REJECTED: 'The enforcement loop rejected the proposed action based on constraint evaluation.',
|
|
62
|
+
ESCALATION_TRIGGERED: 'Repeated violations exceeded the escalation threshold. Human review required.',
|
|
63
|
+
REWRITE_FAILED: 'The enforcement loop could not produce a valid rewrite for the rejected action.',
|
|
64
|
+
};
|
|
65
|
+
/* ── Error Factory ── */
|
|
66
|
+
export function continuityError(code, message, subCode) {
|
|
67
|
+
return subCode != null ? { code, message, subCode } : { code, message };
|
|
68
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity
|
|
3
|
+
*
|
|
4
|
+
* Cryptographic state continuity for AI agents.
|
|
5
|
+
* Reverse-XorIDA incremental state updates over GF(2).
|
|
6
|
+
* Trust substrate with provenance, tiers, and enforcement.
|
|
7
|
+
*/
|
|
8
|
+
export declare const VERSION = "2.1.0";
|
|
9
|
+
export type { Result } from '@private.me/shared';
|
|
10
|
+
export { ok, err } from '@private.me/shared';
|
|
11
|
+
export type { StateValue, AgentState, StateMetadata, StateSnapshot, SplitConfig, StateShare, SplitState, StateDelta, SessionStatus, ContinuitySession, SessionConfig, ChronicleEntry, ChronicleQuery, StateStore, RuntimeEntry, RuntimeMemoryConfig, ContinuityTlvType, ValueTypeCode, AuthorRef, TrustTier, ProvenanceRecord, MemoryEntry, TrackedBelief, AdjudicatorResult, AgentView, MissionRecord, HardConstraint, ProposedAction, ConstraintResult, EnforcementDecision, EnforcementResult, EnforcementConfig, TrustStoreEventType, ChangeEvent, ContradictionEvent, TierChangeEvent, EscalationEvent, TrustStoreEvent, TrustStoreListener, } from './types.js';
|
|
12
|
+
export { CONTINUITY_TLV, VALUE_TYPE, DEFAULT_SPLIT_CONFIG, DEFAULT_RUNTIME_MEMORY_CONFIG, TRUST_TIER_RANK, DEFAULT_MAX_AGE, DEFAULT_ENFORCEMENT_CONFIG, } from './types.js';
|
|
13
|
+
export type { ContinuityError, ContinuityErrorCode, SerializationErrorCode, SplitErrorCode, SessionErrorCode, ChronicleErrorCode, StoreErrorCode, ProvenanceErrorCode, TrustErrorCode, AdjudicatorErrorCode, MissionErrorCode, EnforcementErrorCode, } from './errors.js';
|
|
14
|
+
export { continuityError, ERROR_DETAILS } from './errors.js';
|
|
15
|
+
export { serializeState, deserializeState, computeChecksum, statesEqual, } from './state-serializer.js';
|
|
16
|
+
export { splitState, reconstructState, computeDelta, applyDeltaShares, incrementalUpdate, padForSplit, undoDelta, branchState, squashDeltas, blindUpdateShare, refreshShares, blindEqual, networkCodeShares, networkDecodeShare, } from './reverse-xorida.js';
|
|
17
|
+
export { MemoryStateStore } from './store-memory.js';
|
|
18
|
+
export { RuntimeMemory } from './memory-runtime.js';
|
|
19
|
+
export { SessionMemory } from './memory-session.js';
|
|
20
|
+
export { Chronicle, detectContradiction } from './chronicle.js';
|
|
21
|
+
export type { ChronicleContradiction } from './chronicle.js';
|
|
22
|
+
export { SessionManager } from './session.js';
|
|
23
|
+
export type { TrustSessionConfig } from './session.js';
|
|
24
|
+
export { generateSigningKeyPair, publicKeyToAuthorRef, authorRefToPublicKey, canonicalEntryBytes, signEntry, verifyEntry, hashProvenance, verifyChainLink, } from './provenance.js';
|
|
25
|
+
export type { Ed25519KeyPair } from './provenance.js';
|
|
26
|
+
export { baselineTier, applyContradictionDowngrade, isDecayed, applyDecay, effectiveTier, compareTiers, isMoreTrusted, leastTrusted, } from './trust.js';
|
|
27
|
+
export { PolicyAdjudicator, ConsensusAdjudicator } from './adjudicator.js';
|
|
28
|
+
export type { Adjudicator, ViewProvider } from './adjudicator.js';
|
|
29
|
+
export { TrustStore, Hypothesis } from './ratification.js';
|
|
30
|
+
export type { TrustStoreConfig } from './ratification.js';
|
|
31
|
+
export { MissionAuthority, MissionGuard, AlignmentAdjudicator, } from './mission.js';
|
|
32
|
+
export { EnforcementLoop } from './enforcement.js';
|
|
33
|
+
export { CascadeSession, SubAgentCoordinator, DEFAULT_CASCADE_POLICY, DEFAULT_COORDINATOR_CONFIG, } from './cascade.js';
|
|
34
|
+
export type { TrustPropagation, CascadePolicy, CascadeChild, CascadeEscalation, CoordinatorConfig, } from './cascade.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity
|
|
3
|
+
*
|
|
4
|
+
* Cryptographic state continuity for AI agents.
|
|
5
|
+
* Reverse-XorIDA incremental state updates over GF(2).
|
|
6
|
+
* Trust substrate with provenance, tiers, and enforcement.
|
|
7
|
+
*/
|
|
8
|
+
export const VERSION = '2.1.0';
|
|
9
|
+
export { ok, err } from '@private.me/shared';
|
|
10
|
+
export { CONTINUITY_TLV, VALUE_TYPE, DEFAULT_SPLIT_CONFIG, DEFAULT_RUNTIME_MEMORY_CONFIG,
|
|
11
|
+
// Trust Substrate constants (v2.0.0)
|
|
12
|
+
TRUST_TIER_RANK, DEFAULT_MAX_AGE, DEFAULT_ENFORCEMENT_CONFIG, } from './types.js';
|
|
13
|
+
export { continuityError, ERROR_DETAILS } from './errors.js';
|
|
14
|
+
/* ── State Serializer ── */
|
|
15
|
+
export { serializeState, deserializeState, computeChecksum, statesEqual, } from './state-serializer.js';
|
|
16
|
+
/* ── Reverse-XorIDA ── */
|
|
17
|
+
export { splitState, reconstructState, computeDelta, applyDeltaShares, incrementalUpdate, padForSplit,
|
|
18
|
+
// Algebraic extensions (v2.0.0)
|
|
19
|
+
undoDelta, branchState, squashDeltas, blindUpdateShare, refreshShares, blindEqual, networkCodeShares, networkDecodeShare, } from './reverse-xorida.js';
|
|
20
|
+
/* ── Store ── */
|
|
21
|
+
export { MemoryStateStore } from './store-memory.js';
|
|
22
|
+
/* ── Memory Layers ── */
|
|
23
|
+
export { RuntimeMemory } from './memory-runtime.js';
|
|
24
|
+
export { SessionMemory } from './memory-session.js';
|
|
25
|
+
/* ── Chronicle ── */
|
|
26
|
+
export { Chronicle, detectContradiction } from './chronicle.js';
|
|
27
|
+
/* ── Session Manager ── */
|
|
28
|
+
export { SessionManager } from './session.js';
|
|
29
|
+
/* ── Trust Substrate (v2.0.0) ── */
|
|
30
|
+
// Provenance
|
|
31
|
+
export { generateSigningKeyPair, publicKeyToAuthorRef, authorRefToPublicKey, canonicalEntryBytes, signEntry, verifyEntry, hashProvenance, verifyChainLink, } from './provenance.js';
|
|
32
|
+
// Trust Tiers
|
|
33
|
+
export { baselineTier, applyContradictionDowngrade, isDecayed, applyDecay, effectiveTier, compareTiers, isMoreTrusted, leastTrusted, } from './trust.js';
|
|
34
|
+
// Adjudicator
|
|
35
|
+
export { PolicyAdjudicator, ConsensusAdjudicator } from './adjudicator.js';
|
|
36
|
+
// Ratification (TrustStore)
|
|
37
|
+
export { TrustStore, Hypothesis } from './ratification.js';
|
|
38
|
+
// Mission
|
|
39
|
+
export { MissionAuthority, MissionGuard, AlignmentAdjudicator, } from './mission.js';
|
|
40
|
+
// Enforcement
|
|
41
|
+
export { EnforcementLoop } from './enforcement.js';
|
|
42
|
+
/* ── Cascade / Sub-Agent Architecture (v2.1.0) ── */
|
|
43
|
+
export { CascadeSession, SubAgentCoordinator, DEFAULT_CASCADE_POLICY, DEFAULT_COORDINATOR_CONFIG, } from './cascade.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity — Runtime Memory Layer
|
|
3
|
+
*
|
|
4
|
+
* In-process ephemeral key-value store backed by Map<string, RuntimeEntry>.
|
|
5
|
+
* Tracks access counts and update times. LRU eviction at configurable maxEntries.
|
|
6
|
+
*/
|
|
7
|
+
import type { AgentState, StateValue, RuntimeEntry, RuntimeMemoryConfig } from './types.js';
|
|
8
|
+
export declare class RuntimeMemory {
|
|
9
|
+
private readonly entries;
|
|
10
|
+
private readonly config;
|
|
11
|
+
constructor(config?: Partial<RuntimeMemoryConfig>);
|
|
12
|
+
/** Get a value by key. Returns undefined if not found. Updates access count. */
|
|
13
|
+
get(key: string): StateValue | undefined;
|
|
14
|
+
/** Set a value. Evicts LRU entry if at capacity. */
|
|
15
|
+
set(key: string, value: StateValue): void;
|
|
16
|
+
/** Delete a key. Returns true if the key existed. */
|
|
17
|
+
delete(key: string): boolean;
|
|
18
|
+
/** Check if a key exists. */
|
|
19
|
+
has(key: string): boolean;
|
|
20
|
+
/** Get all keys. */
|
|
21
|
+
keys(): string[];
|
|
22
|
+
/** Number of entries. */
|
|
23
|
+
get size(): number;
|
|
24
|
+
/** Export all entries as an AgentState (flat key-value map). */
|
|
25
|
+
toAgentState(): AgentState;
|
|
26
|
+
/** Import from an AgentState, replacing all entries. */
|
|
27
|
+
fromAgentState(state: AgentState): void;
|
|
28
|
+
/** Merge an AgentState into existing entries (existing keys updated, new keys added). */
|
|
29
|
+
merge(state: AgentState): void;
|
|
30
|
+
/** Get the RuntimeEntry metadata for a key (for inspection/testing). */
|
|
31
|
+
getEntry(key: string): RuntimeEntry | undefined;
|
|
32
|
+
/** Clear all entries. */
|
|
33
|
+
clear(): void;
|
|
34
|
+
/** Evict the least-recently-used entry (lowest accessCount, oldest updatedAt). */
|
|
35
|
+
private evictLRU;
|
|
36
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private.me/xcontinuity — Runtime Memory Layer
|
|
3
|
+
*
|
|
4
|
+
* In-process ephemeral key-value store backed by Map<string, RuntimeEntry>.
|
|
5
|
+
* Tracks access counts and update times. LRU eviction at configurable maxEntries.
|
|
6
|
+
*/
|
|
7
|
+
import { DEFAULT_RUNTIME_MEMORY_CONFIG } from './types.js';
|
|
8
|
+
export class RuntimeMemory {
|
|
9
|
+
entries = new Map();
|
|
10
|
+
config;
|
|
11
|
+
constructor(config = {}) {
|
|
12
|
+
this.config = { ...DEFAULT_RUNTIME_MEMORY_CONFIG, ...config };
|
|
13
|
+
}
|
|
14
|
+
/** Get a value by key. Returns undefined if not found. Updates access count. */
|
|
15
|
+
get(key) {
|
|
16
|
+
const entry = this.entries.get(key);
|
|
17
|
+
if (!entry)
|
|
18
|
+
return undefined;
|
|
19
|
+
// Update access count (create new entry for immutability of the interface)
|
|
20
|
+
const updated = {
|
|
21
|
+
...entry,
|
|
22
|
+
accessCount: entry.accessCount + 1,
|
|
23
|
+
};
|
|
24
|
+
this.entries.set(key, updated);
|
|
25
|
+
return entry.value;
|
|
26
|
+
}
|
|
27
|
+
/** Set a value. Evicts LRU entry if at capacity. */
|
|
28
|
+
set(key, value) {
|
|
29
|
+
const now = Date.now();
|
|
30
|
+
const existing = this.entries.get(key);
|
|
31
|
+
if (existing) {
|
|
32
|
+
this.entries.set(key, {
|
|
33
|
+
...existing,
|
|
34
|
+
value,
|
|
35
|
+
accessCount: existing.accessCount + 1,
|
|
36
|
+
updatedAt: now,
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Evict if at capacity
|
|
41
|
+
if (this.entries.size >= this.config.maxEntries) {
|
|
42
|
+
this.evictLRU();
|
|
43
|
+
}
|
|
44
|
+
this.entries.set(key, {
|
|
45
|
+
key,
|
|
46
|
+
value,
|
|
47
|
+
accessCount: 1,
|
|
48
|
+
createdAt: now,
|
|
49
|
+
updatedAt: now,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** Delete a key. Returns true if the key existed. */
|
|
53
|
+
delete(key) {
|
|
54
|
+
return this.entries.delete(key);
|
|
55
|
+
}
|
|
56
|
+
/** Check if a key exists. */
|
|
57
|
+
has(key) {
|
|
58
|
+
return this.entries.has(key);
|
|
59
|
+
}
|
|
60
|
+
/** Get all keys. */
|
|
61
|
+
keys() {
|
|
62
|
+
return Array.from(this.entries.keys());
|
|
63
|
+
}
|
|
64
|
+
/** Number of entries. */
|
|
65
|
+
get size() {
|
|
66
|
+
return this.entries.size;
|
|
67
|
+
}
|
|
68
|
+
/** Export all entries as an AgentState (flat key-value map). */
|
|
69
|
+
toAgentState() {
|
|
70
|
+
const state = {};
|
|
71
|
+
for (const [key, entry] of this.entries) {
|
|
72
|
+
state[key] = entry.value;
|
|
73
|
+
}
|
|
74
|
+
return state;
|
|
75
|
+
}
|
|
76
|
+
/** Import from an AgentState, replacing all entries. */
|
|
77
|
+
fromAgentState(state) {
|
|
78
|
+
this.entries.clear();
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
for (const [key, value] of Object.entries(state)) {
|
|
81
|
+
if (value === undefined)
|
|
82
|
+
continue;
|
|
83
|
+
this.entries.set(key, {
|
|
84
|
+
key,
|
|
85
|
+
value,
|
|
86
|
+
accessCount: 0,
|
|
87
|
+
createdAt: now,
|
|
88
|
+
updatedAt: now,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Merge an AgentState into existing entries (existing keys updated, new keys added). */
|
|
93
|
+
merge(state) {
|
|
94
|
+
for (const [key, value] of Object.entries(state)) {
|
|
95
|
+
if (value === undefined)
|
|
96
|
+
continue;
|
|
97
|
+
this.set(key, value);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Get the RuntimeEntry metadata for a key (for inspection/testing). */
|
|
101
|
+
getEntry(key) {
|
|
102
|
+
return this.entries.get(key);
|
|
103
|
+
}
|
|
104
|
+
/** Clear all entries. */
|
|
105
|
+
clear() {
|
|
106
|
+
this.entries.clear();
|
|
107
|
+
}
|
|
108
|
+
/** Evict the least-recently-used entry (lowest accessCount, oldest updatedAt). */
|
|
109
|
+
evictLRU() {
|
|
110
|
+
let lruKey = null;
|
|
111
|
+
let lruAccess = Infinity;
|
|
112
|
+
let lruTime = Infinity;
|
|
113
|
+
for (const [key, entry] of this.entries) {
|
|
114
|
+
if (entry.accessCount < lruAccess ||
|
|
115
|
+
(entry.accessCount === lruAccess && entry.updatedAt < lruTime)) {
|
|
116
|
+
lruKey = key;
|
|
117
|
+
lruAccess = entry.accessCount;
|
|
118
|
+
lruTime = entry.updatedAt;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (lruKey !== null) {
|
|
122
|
+
this.entries.delete(lruKey);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|