@aletheia-labs/dynamics 0.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.
@@ -0,0 +1,216 @@
1
+ const DEFAULT_STATUSES = ['candidate', 'verified', 'trusted'];
2
+ const NO_EVIDENCE_PROVIDER = {
3
+ async evidenceFor() {
4
+ return { sourceConsistentRecalls: 0 };
5
+ },
6
+ };
7
+ export class DynamicsEngine {
8
+ memoryStore;
9
+ conflictRegistry;
10
+ policy;
11
+ evidenceProvider;
12
+ /**
13
+ * Create a deterministic lifecycle engine over explicit stores and policy.
14
+ *
15
+ * @remarks
16
+ * The engine owns no scheduler and starts no background work. Hosts trigger
17
+ * lifecycle passes by calling `tick()` or by wrapping it with `SleepCycleRunner`.
18
+ */
19
+ constructor(options) {
20
+ this.memoryStore = options.stores.memoryStore;
21
+ this.conflictRegistry = options.stores.conflictRegistry;
22
+ this.policy = options.policy;
23
+ this.evidenceProvider = options.evidenceProvider ?? NO_EVIDENCE_PROVIDER;
24
+ }
25
+ /**
26
+ * Plan or apply one lifecycle pass over visible atoms in a scope.
27
+ *
28
+ * @remarks
29
+ * The implementation first validates the logical clock and permitted
30
+ * visibilities. It then queries by permission/scope/status but intentionally
31
+ * does not pass `validAt`, because expired or malformed validity windows must
32
+ * remain visible to the lifecycle pass so they can be deprecated. Mutations
33
+ * happen only when `applyTransitions` is true, and only via
34
+ * `MemoryStore.transitionStatus()`.
35
+ */
36
+ async tick(input) {
37
+ const nowMs = timestampMs(input.now);
38
+ if (nowMs === null) {
39
+ return this.emptyResult(input, 'invalid_now');
40
+ }
41
+ if (input.permittedVisibilities.length === 0) {
42
+ return this.emptyResult(input, 'no_permitted_visibilities');
43
+ }
44
+ // Dynamics is a lifecycle scan: it must be able to see expired or corrupt
45
+ // validity windows in order to deprecate them. Permission/scope/status still
46
+ // happen in the store before any semantic decision; validity is evaluated
47
+ // deterministically below against `input.now`.
48
+ const atoms = await this.memoryStore.query({
49
+ scope: input.scope,
50
+ permittedVisibilities: input.permittedVisibilities,
51
+ statuses: input.statuses ?? DEFAULT_STATUSES,
52
+ ...(input.limit !== undefined ? { limit: input.limit } : {}),
53
+ });
54
+ const limitedAtoms = this.policy.maxAtomsPerTick !== undefined
55
+ ? atoms.slice(0, this.policy.maxAtomsPerTick)
56
+ : atoms;
57
+ const decisions = [];
58
+ for (const atom of limitedAtoms) {
59
+ decisions.push(await this.evaluateAtom(atom, input, nowMs));
60
+ }
61
+ return summarize(input, 'ok', [], decisions);
62
+ }
63
+ emptyResult(input, reason) {
64
+ return summarize(input, 'fetch_abstain', [reason], []);
65
+ }
66
+ async evaluateAtom(atom, input, nowMs) {
67
+ if (atom.status !== 'candidate' && atom.status !== 'verified' && atom.status !== 'trusted') {
68
+ return skipped(atom, 'status_not_processable', []);
69
+ }
70
+ const validFromMs = timestampMs(atom.validFrom);
71
+ if (validFromMs === null) {
72
+ return this.transition(atom, 'deprecated', 'invalid_clock', [], input.now, input.applyTransitions === true);
73
+ }
74
+ if (validFromMs > nowMs) {
75
+ return skipped(atom, 'not_yet_valid', []);
76
+ }
77
+ if (atom.validUntil !== null) {
78
+ const validUntilMs = timestampMs(atom.validUntil);
79
+ if (validUntilMs === null) {
80
+ return this.transition(atom, 'deprecated', 'invalid_clock', [], input.now, input.applyTransitions === true);
81
+ }
82
+ if (validUntilMs < nowMs) {
83
+ return this.transition(atom, 'deprecated', 'expired', [], input.now, input.applyTransitions === true);
84
+ }
85
+ }
86
+ const unresolvedConflicts = await this.unresolvedConflicts(atom, input);
87
+ if (unresolvedConflicts.length > 0) {
88
+ if (atom.status === 'candidate') {
89
+ return skipped(atom, 'unresolved_conflict', conflictIds(unresolvedConflicts));
90
+ }
91
+ return this.transition(atom, 'deprecated', 'unresolved_conflict', conflictIds(unresolvedConflicts), input.now, input.applyTransitions === true);
92
+ }
93
+ const evidence = await this.evidenceProvider.evidenceFor(atom, {
94
+ now: input.now,
95
+ scope: input.scope,
96
+ permittedVisibilities: input.permittedVisibilities,
97
+ });
98
+ if (atom.status === 'candidate') {
99
+ if (this.canPromoteCandidate(atom, evidence)) {
100
+ return this.transition(atom, 'verified', 'promotion_evidence_satisfied', [], input.now, input.applyTransitions === true);
101
+ }
102
+ const staleDecision = this.staleDecision(atom, nowMs, evidence);
103
+ if (staleDecision !== null) {
104
+ return this.transition(atom, 'deprecated', staleDecision, [], input.now, input.applyTransitions === true);
105
+ }
106
+ return skipped(atom, 'insufficient_promotion_evidence', []);
107
+ }
108
+ const staleDecision = this.staleDecision(atom, nowMs, evidence);
109
+ if (staleDecision !== null) {
110
+ return this.transition(atom, 'deprecated', staleDecision, [], input.now, input.applyTransitions === true);
111
+ }
112
+ return skipped(atom, 'not_due', []);
113
+ }
114
+ async unresolvedConflicts(atom, input) {
115
+ return this.conflictRegistry.query({
116
+ touchingMemoryIds: [atom.memoryId],
117
+ statuses: ['unresolved', 'requires_human'],
118
+ scope: input.scope,
119
+ permittedVisibilities: input.permittedVisibilities,
120
+ });
121
+ }
122
+ staleDecision(atom, nowMs, evidence) {
123
+ const anchorMs = latestAnchorMs(atom, evidence);
124
+ if (anchorMs === null) {
125
+ return 'stale_by_policy';
126
+ }
127
+ const ageMs = nowMs - anchorMs;
128
+ if (ageMs < 0)
129
+ return null;
130
+ const threshold = statusThresholdMs(atom.status, this.policy);
131
+ if (threshold === null)
132
+ return null;
133
+ return ageMs > threshold ? 'stale_by_policy' : null;
134
+ }
135
+ canPromoteCandidate(atom, evidence) {
136
+ // These scores are gate-produced metadata thresholds. They are never
137
+ // authority by themselves; explicit source-consistent recall evidence is
138
+ // required before any candidate can become verified.
139
+ return (evidence.sourceConsistentRecalls >= this.policy.promotion.minSourceConsistentRecalls &&
140
+ atom.scores.evidence >= this.policy.promotion.minEvidenceScore &&
141
+ atom.scores.authority >= this.policy.promotion.minAuthorityScore &&
142
+ atom.scores.stability >= this.policy.promotion.minStabilityScore);
143
+ }
144
+ async transition(atom, recommendedStatus, reason, conflicts, at, apply) {
145
+ if (!apply) {
146
+ return {
147
+ memoryId: atom.memoryId,
148
+ fromStatus: atom.status,
149
+ outcome: 'planned',
150
+ reason,
151
+ recommendedStatus,
152
+ conflicts,
153
+ };
154
+ }
155
+ const transition = await this.memoryStore.transitionStatus(atom.memoryId, recommendedStatus, {
156
+ actor: this.policy.actor,
157
+ rationale: `phase2:${reason}`,
158
+ ...(conflicts[0] !== undefined ? { conflictId: conflicts[0] } : {}),
159
+ }, { at });
160
+ return {
161
+ memoryId: atom.memoryId,
162
+ fromStatus: atom.status,
163
+ outcome: transition.kind === 'applied' ? 'applied' : 'rejected',
164
+ reason,
165
+ recommendedStatus,
166
+ conflicts,
167
+ transition,
168
+ };
169
+ }
170
+ }
171
+ function statusThresholdMs(status, policy) {
172
+ if (status === 'candidate')
173
+ return policy.decay.candidateAfterMs;
174
+ if (status === 'verified')
175
+ return policy.decay.verifiedAfterMs;
176
+ if (status === 'trusted')
177
+ return policy.decay.trustedAfterMs;
178
+ return null;
179
+ }
180
+ function timestampMs(timestamp) {
181
+ const parsed = Date.parse(timestamp);
182
+ return Number.isNaN(parsed) ? null : parsed;
183
+ }
184
+ function latestAnchorMs(atom, evidence) {
185
+ const anchors = [atom.validFrom, atom.lastConfirmedAt, evidence.lastUsedAt].filter((timestamp) => timestamp !== null && timestamp !== undefined);
186
+ const parsed = anchors.map(timestampMs);
187
+ if (parsed.some((value) => value === null))
188
+ return null;
189
+ return Math.max(...parsed);
190
+ }
191
+ function skipped(atom, reason, conflicts) {
192
+ return {
193
+ memoryId: atom.memoryId,
194
+ fromStatus: atom.status,
195
+ outcome: 'skipped',
196
+ reason,
197
+ conflicts,
198
+ };
199
+ }
200
+ function conflictIds(conflicts) {
201
+ return conflicts.map((conflict) => conflict.conflictId);
202
+ }
203
+ function summarize(input, outcome, reasons, decisions) {
204
+ return {
205
+ outcome,
206
+ now: input.now,
207
+ scope: input.scope,
208
+ reasons,
209
+ decisions,
210
+ appliedCount: decisions.filter((decision) => decision.outcome === 'applied').length,
211
+ plannedCount: decisions.filter((decision) => decision.outcome === 'planned').length,
212
+ rejectedCount: decisions.filter((decision) => decision.outcome === 'rejected').length,
213
+ skippedCount: decisions.filter((decision) => decision.outcome === 'skipped').length,
214
+ };
215
+ }
216
+ //# sourceMappingURL=dynamics-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dynamics-engine.js","sourceRoot":"","sources":["../src/dynamics-engine.ts"],"names":[],"mappings":"AAgGA,MAAM,gBAAgB,GAA4B,CAAC,WAAW,EAAE,UAAU,EAAE,SAAS,CAAC,CAAC;AAEvF,MAAM,oBAAoB,GAA6B;IACrD,KAAK,CAAC,WAAW;QACf,OAAO,EAAE,uBAAuB,EAAE,CAAC,EAAE,CAAC;IACxC,CAAC;CACF,CAAC;AAEF,MAAM,OAAO,cAAc;IACR,WAAW,CAAc;IACzB,gBAAgB,CAAmB;IACnC,MAAM,CAAiB;IACvB,gBAAgB,CAA2B;IAE5D;;;;;;OAMG;IACH,YAAY,OAA8B;QACxC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC;QAC9C,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC;QACxD,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,oBAAoB,CAAC;IAC3E,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,IAAI,CAAC,KAAwB;QACjC,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;QAChD,CAAC;QACD,IAAI,KAAK,CAAC,qBAAqB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC7C,OAAO,IAAI,CAAC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gB,EAChB,MAA8B,EAC9B,SAAgC;IAEhC,OAAO;QACL,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,UAAU,EAAE,IAAI,CAAC,MAAM;QACvB,OAAO,EAAE,SAAS;QAClB,MAAM;QACN,SAAS;KACV,CAAC;AACJ,CAAC;AAED,SAAS,WAAW,CAAC,SAAoC;IACvD,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,SAAS,CAChB,KAAwB,EACxB,OAA4B,EAC5B,OAA0B,EAC1B,SAAsC;IAEtC,OAAO;QACL,OAAO;QACP,GAAG,EAAE,KAAK,CAAC,GAAG;QACd,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,OAAO;QACP,SAAS;QACT,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM;QACnF,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM;QACnF,aAAa,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,KAAK,UAAU,CAAC,CAAC,MAAM;QACrF,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM;KACpF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from './policy.js';
2
+ export * from './authority-decay.js';
3
+ export * from './recall-evidence.js';
4
+ export * from './dynamics-engine.js';
5
+ export * from './sleep-cycle.js';
6
+ export * from './reconsolidation-planner.js';
7
+ export * from './lineage.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from './policy.js';
2
+ export * from './authority-decay.js';
3
+ export * from './recall-evidence.js';
4
+ export * from './dynamics-engine.js';
5
+ export * from './sleep-cycle.js';
6
+ export * from './reconsolidation-planner.js';
7
+ export * from './lineage.js';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,cAAc,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { MemoryAtom, MemoryId, MemoryStore, Visibility } from '@aletheia-labs/core';
2
+ export type LineageTraceOutcome = 'ok' | 'fetch_abstain';
3
+ export type LineageTraceReason = 'cycle_detected' | 'max_depth_exceeded' | 'memory_missing_or_invisible';
4
+ export interface LineageTraceInput {
5
+ readonly memoryId: MemoryId;
6
+ readonly permittedVisibilities: readonly Visibility[];
7
+ readonly maxDepth?: number;
8
+ }
9
+ export interface LineageTraceResult {
10
+ readonly outcome: LineageTraceOutcome;
11
+ readonly reasons: readonly LineageTraceReason[];
12
+ readonly atoms: readonly MemoryAtom[];
13
+ }
14
+ export interface LineageTracerOptions {
15
+ readonly memoryStore: MemoryStore;
16
+ }
17
+ export declare class LineageTracer {
18
+ private readonly options;
19
+ /**
20
+ * Create a permission-guarded lineage tracer over a memory store.
21
+ *
22
+ * @remarks
23
+ * The tracer reads only through `MemoryStore.get()`, so invisible ancestors
24
+ * are treated the same as missing ancestors. It reconstructs `supersedes`
25
+ * chains without mutating atoms or status history.
26
+ */
27
+ constructor(options: LineageTracerOptions);
28
+ /**
29
+ * Trace from a successor atom backwards through `supersedes` links.
30
+ *
31
+ * @returns Atoms in newest-to-oldest order when every ancestor is visible.
32
+ */
33
+ traceBack(input: LineageTraceInput): Promise<LineageTraceResult>;
34
+ }
35
+ //# sourceMappingURL=lineage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lineage.d.ts","sourceRoot":"","sources":["../src/lineage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEzF,MAAM,MAAM,mBAAmB,GAAG,IAAI,GAAG,eAAe,CAAC;AACzD,MAAM,MAAM,kBAAkB,GAC1B,gBAAgB,GAChB,oBAAoB,GACpB,6BAA6B,CAAC;AAElC,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,qBAAqB,EAAE,SAAS,UAAU,EAAE,CAAC;IACtD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAChD,QAAQ,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,CAAC;CACvC;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;CACnC;AAED,qBAAa,aAAa;IASZ,OAAO,CAAC,QAAQ,CAAC,OAAO;IARpC;;;;;;;OAOG;gBAC0B,OAAO,EAAE,oBAAoB;IAE1D;;;;OAIG;IACG,SAAS,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAyBvE"}
@@ -0,0 +1,45 @@
1
+ export class LineageTracer {
2
+ options;
3
+ /**
4
+ * Create a permission-guarded lineage tracer over a memory store.
5
+ *
6
+ * @remarks
7
+ * The tracer reads only through `MemoryStore.get()`, so invisible ancestors
8
+ * are treated the same as missing ancestors. It reconstructs `supersedes`
9
+ * chains without mutating atoms or status history.
10
+ */
11
+ constructor(options) {
12
+ this.options = options;
13
+ }
14
+ /**
15
+ * Trace from a successor atom backwards through `supersedes` links.
16
+ *
17
+ * @returns Atoms in newest-to-oldest order when every ancestor is visible.
18
+ */
19
+ async traceBack(input) {
20
+ const maxDepth = input.maxDepth ?? 128;
21
+ const atoms = [];
22
+ const visited = new Set();
23
+ let currentId = input.memoryId;
24
+ while (currentId !== null) {
25
+ if (atoms.length >= maxDepth) {
26
+ return { outcome: 'fetch_abstain', reasons: ['max_depth_exceeded'], atoms };
27
+ }
28
+ if (visited.has(currentId)) {
29
+ return { outcome: 'fetch_abstain', reasons: ['cycle_detected'], atoms };
30
+ }
31
+ visited.add(currentId);
32
+ const atom = await this.options.memoryStore.get(currentId, input.permittedVisibilities);
33
+ if (atom === null) {
34
+ return { outcome: 'fetch_abstain', reasons: ['memory_missing_or_invisible'], atoms };
35
+ }
36
+ atoms.push(atom);
37
+ currentId = supersededMemoryId(atom);
38
+ }
39
+ return { outcome: 'ok', reasons: [], atoms };
40
+ }
41
+ }
42
+ function supersededMemoryId(atom) {
43
+ return atom.links.find((link) => link.relation === 'supersedes')?.targetMemoryId ?? null;
44
+ }
45
+ //# sourceMappingURL=lineage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lineage.js","sourceRoot":"","sources":["../src/lineage.ts"],"names":[],"mappings":"AAwBA,MAAM,OAAO,aAAa;IASK;IAR7B;;;;;;;OAOG;IACH,YAA6B,OAA6B;QAA7B,YAAO,GAAP,OAAO,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C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,YAAY,CAAC,EAAE,cAAc,IAAI,IAAI,CAAC;AAC3F,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { AgentId } from '@aletheia-labs/core';
2
+ export interface DecayPolicy {
3
+ readonly candidateAfterMs: number;
4
+ readonly verifiedAfterMs: number;
5
+ readonly trustedAfterMs: number;
6
+ }
7
+ export interface PromotionPolicy {
8
+ readonly minSourceConsistentRecalls: number;
9
+ readonly minEvidenceScore: number;
10
+ readonly minAuthorityScore: number;
11
+ readonly minStabilityScore: number;
12
+ }
13
+ export interface DynamicsPolicy {
14
+ readonly actor: AgentId;
15
+ readonly decay: DecayPolicy;
16
+ readonly promotion: PromotionPolicy;
17
+ readonly maxAtomsPerTick?: number;
18
+ }
19
+ export interface DynamicsPolicyOverrides {
20
+ readonly decay?: Partial<DecayPolicy>;
21
+ readonly promotion?: Partial<PromotionPolicy>;
22
+ readonly maxAtomsPerTick?: number;
23
+ }
24
+ export declare const DEFAULT_DECAY_POLICY: DecayPolicy;
25
+ export declare const DEFAULT_PROMOTION_POLICY: PromotionPolicy;
26
+ /**
27
+ * Build a dynamics policy from defaults plus host overrides.
28
+ *
29
+ * @remarks
30
+ * Use this helper instead of hand-copying defaults in hosts. The `actor` is
31
+ * written into transition audit reasons when the dynamics engine applies
32
+ * status changes.
33
+ */
34
+ export declare function createDynamicsPolicy(actor: AgentId, overrides?: DynamicsPolicyOverrides): DynamicsPolicy;
35
+ //# sourceMappingURL=policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAEnD,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;CACjC;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,0BAA0B,EAAE,MAAM,CAAC;IAC5C,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACnC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;CACpC;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAC;IACpC,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACnC;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IACtC,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;IAC9C,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACnC;AAID,eAAO,MAAM,oBAAoB,EAAE,WAIlC,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,eAKtC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,EACd,SAAS,GAAE,uBAA4B,GACtC,cAAc,CAShB"}
package/dist/policy.js ADDED
@@ -0,0 +1,31 @@
1
+ const DAY_MS = 24 * 60 * 60 * 1000;
2
+ export const DEFAULT_DECAY_POLICY = {
3
+ candidateAfterMs: 7 * DAY_MS,
4
+ verifiedAfterMs: 30 * DAY_MS,
5
+ trustedAfterMs: 180 * DAY_MS,
6
+ };
7
+ export const DEFAULT_PROMOTION_POLICY = {
8
+ minSourceConsistentRecalls: 2,
9
+ minEvidenceScore: 0.7,
10
+ minAuthorityScore: 0.6,
11
+ minStabilityScore: 0.5,
12
+ };
13
+ /**
14
+ * Build a dynamics policy from defaults plus host overrides.
15
+ *
16
+ * @remarks
17
+ * Use this helper instead of hand-copying defaults in hosts. The `actor` is
18
+ * written into transition audit reasons when the dynamics engine applies
19
+ * status changes.
20
+ */
21
+ export function createDynamicsPolicy(actor, overrides = {}) {
22
+ return {
23
+ actor,
24
+ decay: { ...DEFAULT_DECAY_POLICY, ...overrides.decay },
25
+ promotion: { ...DEFAULT_PROMOTION_POLICY, ...overrides.promotion },
26
+ ...(overrides.maxAtomsPerTick !== undefined
27
+ ? { maxAtomsPerTick: overrides.maxAtomsPerTick }
28
+ : {}),
29
+ };
30
+ }
31
+ //# sourceMappingURL=policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AA4BA,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEnC,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C,gBAAgB,EAAE,CAAC,GAAG,MAAM;IAC5B,eAAe,EAAE,EAAE,GAAG,MAAM;IAC5B,cAAc,EAAE,GAAG,GAAG,MAAM;CAC7B,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAoB;IACvD,0BAA0B,EAAE,CAAC;IAC7B,gBAAgB,EAAE,GAAG;IACrB,iBAAiB,EAAE,GAAG;IACtB,iBAAiB,EAAE,GAAG;CACvB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAc,EACd,YAAqC,EAAE;IAEvC,OAAO;QACL,KAAK;QACL,KAAK,EAAE,EAAE,GAAG,oBAAoB,EAAE,GAAG,SAAS,CAAC,KAAK,EAAE;QACtD,SAAS,EAAE,EAAE,GAAG,wBAAwB,EAAE,GAAG,SAAS,CAAC,SAAS,EAAE;QAClE,GAAG,CAAC,SAAS,CAAC,eAAe,KAAK,SAAS;YACzC,CAAC,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,eAAe,EAAE;YAChD,CAAC,CAAC,EAAE,CAAC;KACR,CAAC;AACJ,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { EventId, EventLedger, JsonValue, MemoryAtom, MemoryId } from '@aletheia-labs/core';
2
+ import type { DynamicsEvidence, DynamicsEvidenceContext, DynamicsEvidenceProvider } from './dynamics-engine.js';
3
+ export declare const SOURCE_CONSISTENT_RECALL_EVENT = "aletheia.source_consistent_recall";
4
+ export interface SourceConsistentRecallPayload {
5
+ readonly [key: string]: JsonValue;
6
+ readonly aletheiaEvent: typeof SOURCE_CONSISTENT_RECALL_EVENT;
7
+ readonly memoryId: MemoryId;
8
+ readonly sourceEventIds: readonly EventId[];
9
+ }
10
+ export interface LedgerRecallEvidenceProviderOptions {
11
+ readonly eventLedger: EventLedger;
12
+ }
13
+ export declare class LedgerRecallEvidenceProvider implements DynamicsEvidenceProvider {
14
+ private readonly options;
15
+ /**
16
+ * Create a recall evidence provider backed by append-only events.
17
+ *
18
+ * @remarks
19
+ * The provider treats ledger events as evidence, not authority. Only events
20
+ * with the explicit `SOURCE_CONSISTENT_RECALL_EVENT` payload and matching
21
+ * source-event coverage count toward promotion or last-used freshness.
22
+ */
23
+ constructor(options: LedgerRecallEvidenceProviderOptions);
24
+ /**
25
+ * Count source-consistent recall events for one atom.
26
+ *
27
+ * @remarks
28
+ * Permission and scope filtering are delegated to `EventLedger.query()` before
29
+ * payload inspection. The result is deterministic for a fixed ledger and
30
+ * context clock.
31
+ */
32
+ evidenceFor(atom: MemoryAtom, context: DynamicsEvidenceContext): Promise<DynamicsEvidence>;
33
+ }
34
+ /**
35
+ * Build the canonical payload hosts should append after a source-consistent recall.
36
+ */
37
+ export declare function sourceConsistentRecallPayload(atom: MemoryAtom): SourceConsistentRecallPayload;
38
+ //# sourceMappingURL=recall-evidence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recall-evidence.d.ts","sourceRoot":"","sources":["../src/recall-evidence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,OAAO,EACP,WAAW,EACX,SAAS,EACT,UAAU,EACV,QAAQ,EACT,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EACV,gBAAgB,EAChB,uBAAuB,EACvB,wBAAwB,EACzB,MAAM,sBAAsB,CAAC;AAE9B,eAAO,MAAM,8BAA8B,sCAAsC,CAAC;AAElF,MAAM,WAAW,6BAA6B;IAC5C,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,8BAA8B,CAAC;IAC9D,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,cAAc,EAAE,SAAS,OAAO,EAAE,CAAC;CAC7C;AAED,MAAM,WAAW,mCAAmC;IAClD,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;CACnC;AAED,qBAAa,4BAA6B,YAAW,wBAAwB;IAS/D,OAAO,CAAC,QAAQ,CAAC,OAAO;IARpC;;;;;;;OAOG;gBAC0B,OAAO,EAAE,mCAAmC;IAEzE;;;;;;;OAOG;IACG,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAmBjG;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,UAAU,GAAG,6BAA6B,CAM7F"}
@@ -0,0 +1,67 @@
1
+ export const SOURCE_CONSISTENT_RECALL_EVENT = 'aletheia.source_consistent_recall';
2
+ export class LedgerRecallEvidenceProvider {
3
+ options;
4
+ /**
5
+ * Create a recall evidence provider backed by append-only events.
6
+ *
7
+ * @remarks
8
+ * The provider treats ledger events as evidence, not authority. Only events
9
+ * with the explicit `SOURCE_CONSISTENT_RECALL_EVENT` payload and matching
10
+ * source-event coverage count toward promotion or last-used freshness.
11
+ */
12
+ constructor(options) {
13
+ this.options = options;
14
+ }
15
+ /**
16
+ * Count source-consistent recall events for one atom.
17
+ *
18
+ * @remarks
19
+ * Permission and scope filtering are delegated to `EventLedger.query()` before
20
+ * payload inspection. The result is deterministic for a fixed ledger and
21
+ * context clock.
22
+ */
23
+ async evidenceFor(atom, context) {
24
+ const events = await this.options.eventLedger.query({
25
+ scope: context.scope,
26
+ permittedVisibilities: context.permittedVisibilities,
27
+ since: atom.validFrom,
28
+ until: context.now,
29
+ });
30
+ const matching = events.filter((event) => isSourceConsistentRecallEvent(event, atom));
31
+ const timestamps = matching.map((event) => event.occurredAt).sort();
32
+ const first = timestamps[0];
33
+ const last = timestamps.at(-1);
34
+ return {
35
+ sourceConsistentRecalls: matching.length,
36
+ ...(last !== undefined ? { lastUsedAt: last } : {}),
37
+ ...(first !== undefined ? { sourceConsistentSince: first } : {}),
38
+ };
39
+ }
40
+ }
41
+ /**
42
+ * Build the canonical payload hosts should append after a source-consistent recall.
43
+ */
44
+ export function sourceConsistentRecallPayload(atom) {
45
+ return {
46
+ aletheiaEvent: SOURCE_CONSISTENT_RECALL_EVENT,
47
+ memoryId: atom.memoryId,
48
+ sourceEventIds: [...atom.sourceEventIds],
49
+ };
50
+ }
51
+ function isSourceConsistentRecallEvent(event, atom) {
52
+ const payload = event.payload;
53
+ if (!isRecallPayload(payload))
54
+ return false;
55
+ if (payload.aletheiaEvent !== SOURCE_CONSISTENT_RECALL_EVENT)
56
+ return false;
57
+ if (payload.memoryId !== atom.memoryId)
58
+ return false;
59
+ if (!Array.isArray(payload.sourceEventIds))
60
+ return false;
61
+ const recalledSources = new Set(payload.sourceEventIds.filter((value) => typeof value === 'string'));
62
+ return atom.sourceEventIds.every((eventId) => recalledSources.has(eventId));
63
+ }
64
+ function isRecallPayload(value) {
65
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
66
+ }
67
+ //# sourceMappingURL=recall-evidence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recall-evidence.js","sourceRoot":"","sources":["../src/recall-evidence.ts"],"names":[],"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AgB;IAC5D,OAAO;QACL,aAAa,EAAE,8BAA8B;QAC7C,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,cAAc,EAAE,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC;KACzC,CAAC;AACJ,CAAC;AAED,SAAS,6BAA6B,CAAC,KAAY,EAAE,IAAgB;IACnE,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;IAC9B,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,OAAO,CAAC,aAAa,KAAK,8BAA8B;QAAE,OAAO,KAAK,CAAC;IAC3E,IAAI,OAAO,CAAC,QAAQ,KAAK,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IACrD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC;QAAE,OAAO,KAAK,CAAC;IAEzD,MAAM,eAAe,GAAG,IAAI,GAAG,CAC7B,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,KAAK,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CACrF,CAAC;IACF,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,eAAe,CAAC,KAAc;IAKrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,93 @@
1
+ import type { AgentId, ConflictId, ConflictRegistry, Event, EventId, EventLedger, IsoTimestamp, MemoryAtom, MemoryId, MemoryStore, MemoryType, Scope, StatusTransitionReason, StatusTransitionResult, Visibility } from '@aletheia-labs/core';
2
+ export type ReconsolidationOutcome = 'plan' | 'fetch_abstain';
3
+ export type ReconsolidationApplyOutcome = 'applied' | 'ask_human' | 'fetch_abstain' | 'partial_applied' | 'rejected';
4
+ export type ReconsolidationReason = 'empty_source_events' | 'invalid_successor_draft' | 'no_permitted_visibilities' | 'previous_memory_missing_or_invisible' | 'previous_status_not_reconsolidatable' | 'source_event_missing_or_invisible' | 'source_event_scope_mismatch' | 'successor_insert_failed' | 'transition_rejected' | 'human_confirmation_required' | 'invalid_human_confirmation' | 'unresolved_conflict';
5
+ export interface ReconsolidationPlannerStores {
6
+ readonly eventLedger: EventLedger;
7
+ readonly memoryStore: MemoryStore;
8
+ readonly conflictRegistry: ConflictRegistry;
9
+ }
10
+ export interface ReconsolidationPlanInput {
11
+ readonly previousMemoryId: MemoryId;
12
+ readonly successorMemoryId: MemoryId;
13
+ readonly proposedBy: AgentId;
14
+ readonly proposedAt: IsoTimestamp;
15
+ readonly memoryType: MemoryType;
16
+ readonly claim: string;
17
+ readonly sourceEventIds: readonly EventId[];
18
+ readonly scope: Scope;
19
+ readonly permittedVisibilities: readonly Visibility[];
20
+ }
21
+ export interface PlannedStatusTransition {
22
+ readonly memoryId: MemoryId;
23
+ readonly nextStatus: 'deprecated';
24
+ readonly reason: StatusTransitionReason;
25
+ }
26
+ export interface ReconsolidationPlan {
27
+ readonly outcome: ReconsolidationOutcome;
28
+ readonly reasons: readonly ReconsolidationReason[];
29
+ readonly previousAtom: MemoryAtom | null;
30
+ readonly sourceEvents: readonly Event[];
31
+ readonly successorDraft: MemoryAtom | null;
32
+ readonly plannedTransitions: readonly PlannedStatusTransition[];
33
+ readonly relatedConflictIds: readonly ConflictId[];
34
+ }
35
+ export interface ReconsolidationHumanConfirmation {
36
+ readonly confirmedBy: AgentId;
37
+ readonly confirmedAt: IsoTimestamp;
38
+ readonly rationale: string;
39
+ }
40
+ export interface ReconsolidationApplyInput extends ReconsolidationPlanInput {
41
+ readonly humanConfirmation?: ReconsolidationHumanConfirmation;
42
+ }
43
+ export interface ReconsolidationApplyResult {
44
+ readonly outcome: ReconsolidationApplyOutcome;
45
+ readonly reasons: readonly ReconsolidationReason[];
46
+ readonly plan: ReconsolidationPlan;
47
+ readonly successorAtom: MemoryAtom | null;
48
+ readonly transitionResults: readonly StatusTransitionResult[];
49
+ }
50
+ export declare class ReconsolidationPlanner {
51
+ private readonly stores;
52
+ /**
53
+ * Create a reconsolidation planner over existing authority stores.
54
+ *
55
+ * @remarks
56
+ * The planner is read-only. It validates visibility, source events, scope,
57
+ * status, and conflict state before producing a successor draft.
58
+ */
59
+ constructor(stores: ReconsolidationPlannerStores);
60
+ /**
61
+ * Plan a candidate successor atom and prior-atom deprecation.
62
+ *
63
+ * @remarks
64
+ * Use this when new evidence should update the meaning of an existing atom.
65
+ * The plan never mutates storage and never upgrades authority: successor
66
+ * drafts start as `candidate` and carry `supersedes` lineage back to the
67
+ * previous atom.
68
+ */
69
+ plan(input: ReconsolidationPlanInput): Promise<ReconsolidationPlan>;
70
+ }
71
+ export declare class ReconsolidationApplier {
72
+ private readonly stores;
73
+ private readonly planner;
74
+ /**
75
+ * Create a human-confirmed reconsolidation applier.
76
+ *
77
+ * @remarks
78
+ * This wraps `ReconsolidationPlanner` and adds the explicit mutation path.
79
+ * Hosts should call `plan()` directly when they only need a preview.
80
+ */
81
+ constructor(stores: ReconsolidationPlannerStores);
82
+ /**
83
+ * Apply a reconsolidation plan after human confirmation.
84
+ *
85
+ * @remarks
86
+ * The method fails closed with `ask_human` without valid confirmation. When
87
+ * confirmed, it inserts the successor candidate first and then applies
88
+ * planned deprecations through `MemoryStore.transitionStatus()`. If successor
89
+ * insertion fails, no prior atom is deprecated.
90
+ */
91
+ apply(input: ReconsolidationApplyInput): Promise<ReconsolidationApplyResult>;
92
+ }
93
+ //# sourceMappingURL=reconsolidation-planner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconsolidation-planner.d.ts","sourceRoot":"","sources":["../src/reconsolidation-planner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,UAAU,E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