agent-working-memory 0.5.4 → 0.5.6
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 +87 -46
- package/dist/api/routes.d.ts.map +1 -1
- package/dist/api/routes.js +21 -5
- package/dist/api/routes.js.map +1 -1
- package/dist/cli.js +67 -67
- package/dist/coordination/index.d.ts +11 -0
- package/dist/coordination/index.d.ts.map +1 -0
- package/dist/coordination/index.js +39 -0
- package/dist/coordination/index.js.map +1 -0
- package/dist/coordination/mcp-tools.d.ts +8 -0
- package/dist/coordination/mcp-tools.d.ts.map +1 -0
- package/dist/coordination/mcp-tools.js +216 -0
- package/dist/coordination/mcp-tools.js.map +1 -0
- package/dist/coordination/routes.d.ts +9 -0
- package/dist/coordination/routes.d.ts.map +1 -0
- package/dist/coordination/routes.js +434 -0
- package/dist/coordination/routes.js.map +1 -0
- package/dist/coordination/schema.d.ts +12 -0
- package/dist/coordination/schema.d.ts.map +1 -0
- package/dist/coordination/schema.js +91 -0
- package/dist/coordination/schema.js.map +1 -0
- package/dist/coordination/schemas.d.ts +208 -0
- package/dist/coordination/schemas.d.ts.map +1 -0
- package/dist/coordination/schemas.js +109 -0
- package/dist/coordination/schemas.js.map +1 -0
- package/dist/coordination/stale.d.ts +25 -0
- package/dist/coordination/stale.d.ts.map +1 -0
- package/dist/coordination/stale.js +53 -0
- package/dist/coordination/stale.js.map +1 -0
- package/dist/index.js +21 -3
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +90 -79
- package/dist/mcp.js.map +1 -1
- package/dist/storage/sqlite.d.ts +3 -0
- package/dist/storage/sqlite.d.ts.map +1 -1
- package/dist/storage/sqlite.js +285 -281
- package/dist/storage/sqlite.js.map +1 -1
- package/package.json +55 -55
- package/src/api/index.ts +3 -3
- package/src/api/routes.ts +551 -536
- package/src/cli.ts +397 -397
- package/src/coordination/index.ts +47 -0
- package/src/coordination/mcp-tools.ts +313 -0
- package/src/coordination/routes.ts +656 -0
- package/src/coordination/schema.ts +94 -0
- package/src/coordination/schemas.ts +136 -0
- package/src/coordination/stale.ts +89 -0
- package/src/core/decay.ts +63 -63
- package/src/core/embeddings.ts +88 -88
- package/src/core/hebbian.ts +93 -93
- package/src/core/index.ts +5 -5
- package/src/core/logger.ts +36 -36
- package/src/core/query-expander.ts +66 -66
- package/src/core/reranker.ts +101 -101
- package/src/engine/activation.ts +656 -656
- package/src/engine/connections.ts +103 -103
- package/src/engine/consolidation-scheduler.ts +125 -125
- package/src/engine/eval.ts +102 -102
- package/src/engine/eviction.ts +101 -101
- package/src/engine/index.ts +8 -8
- package/src/engine/retraction.ts +100 -100
- package/src/engine/staging.ts +74 -74
- package/src/index.ts +137 -121
- package/src/mcp.ts +1024 -1013
- package/src/storage/index.ts +3 -3
- package/src/storage/sqlite.ts +968 -963
- package/src/types/agent.ts +67 -67
- package/src/types/checkpoint.ts +46 -46
- package/src/types/engram.ts +217 -217
- package/src/types/eval.ts +100 -100
- package/src/types/index.ts +6 -6
package/src/types/agent.ts
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
// Copyright 2026 Robert Winter / Complete Ideas
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
/**
|
|
4
|
-
* Agent — a consciousness boundary.
|
|
5
|
-
* Each agent has its own isolated memory space with capacity budgets.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export interface Agent {
|
|
9
|
-
id: string;
|
|
10
|
-
name: string;
|
|
11
|
-
createdAt: Date;
|
|
12
|
-
config: AgentConfig;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface AgentConfig {
|
|
16
|
-
// Salience filter thresholds
|
|
17
|
-
salienceThreshold: number; // Below this → discard
|
|
18
|
-
stagingThreshold: number; // Below salience but above this → staging buffer
|
|
19
|
-
stagingTtlMs: number; // Default TTL for staging entries
|
|
20
|
-
|
|
21
|
-
// Capacity budgets (eviction triggers when exceeded)
|
|
22
|
-
maxActiveEngrams: number; // Hard cap on active memory
|
|
23
|
-
maxStagingEngrams: number; // Hard cap on staging buffer
|
|
24
|
-
maxEdgesPerEngram: number; // Prevent graph explosion
|
|
25
|
-
|
|
26
|
-
// Activation pipeline tuning
|
|
27
|
-
activationLimit: number; // Max results per activation query
|
|
28
|
-
hebbianRate: number; // Learning rate for association strengthening
|
|
29
|
-
decayExponent: number; // ACT-R d parameter (default 0.5)
|
|
30
|
-
edgeDecayHalfLifeDays: number; // How fast unused edges weaken
|
|
31
|
-
|
|
32
|
-
// Connection engine
|
|
33
|
-
connectionThreshold: number; // Min resonance score to form a connection
|
|
34
|
-
connectionCheckIntervalMs: number;
|
|
35
|
-
|
|
36
|
-
// Consolidation
|
|
37
|
-
consolidationIntervalMs: number; // How often to check for merge candidates
|
|
38
|
-
consolidationSimilarity: number; // Threshold for merging similar engrams
|
|
39
|
-
|
|
40
|
-
// Confidence updates
|
|
41
|
-
feedbackPositiveBoost: number; // How much positive feedback increases confidence
|
|
42
|
-
feedbackNegativePenalty: number; // How much negative feedback decreases confidence
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const DEFAULT_AGENT_CONFIG: AgentConfig = {
|
|
46
|
-
salienceThreshold: 0.4,
|
|
47
|
-
stagingThreshold: 0.2,
|
|
48
|
-
stagingTtlMs: 24 * 60 * 60 * 1000, // 24 hours
|
|
49
|
-
|
|
50
|
-
maxActiveEngrams: 10_000,
|
|
51
|
-
maxStagingEngrams: 1_000,
|
|
52
|
-
maxEdgesPerEngram: 20,
|
|
53
|
-
|
|
54
|
-
activationLimit: 10,
|
|
55
|
-
hebbianRate: 0.25,
|
|
56
|
-
decayExponent: 0.5,
|
|
57
|
-
edgeDecayHalfLifeDays: 7,
|
|
58
|
-
|
|
59
|
-
connectionThreshold: 0.7,
|
|
60
|
-
connectionCheckIntervalMs: 60_000,
|
|
61
|
-
|
|
62
|
-
consolidationIntervalMs: 300_000, // 5 minutes
|
|
63
|
-
consolidationSimilarity: 0.85,
|
|
64
|
-
|
|
65
|
-
feedbackPositiveBoost: 0.05,
|
|
66
|
-
feedbackNegativePenalty: 0.1,
|
|
67
|
-
};
|
|
1
|
+
// Copyright 2026 Robert Winter / Complete Ideas
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Agent — a consciousness boundary.
|
|
5
|
+
* Each agent has its own isolated memory space with capacity budgets.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface Agent {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
createdAt: Date;
|
|
12
|
+
config: AgentConfig;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AgentConfig {
|
|
16
|
+
// Salience filter thresholds
|
|
17
|
+
salienceThreshold: number; // Below this → discard
|
|
18
|
+
stagingThreshold: number; // Below salience but above this → staging buffer
|
|
19
|
+
stagingTtlMs: number; // Default TTL for staging entries
|
|
20
|
+
|
|
21
|
+
// Capacity budgets (eviction triggers when exceeded)
|
|
22
|
+
maxActiveEngrams: number; // Hard cap on active memory
|
|
23
|
+
maxStagingEngrams: number; // Hard cap on staging buffer
|
|
24
|
+
maxEdgesPerEngram: number; // Prevent graph explosion
|
|
25
|
+
|
|
26
|
+
// Activation pipeline tuning
|
|
27
|
+
activationLimit: number; // Max results per activation query
|
|
28
|
+
hebbianRate: number; // Learning rate for association strengthening
|
|
29
|
+
decayExponent: number; // ACT-R d parameter (default 0.5)
|
|
30
|
+
edgeDecayHalfLifeDays: number; // How fast unused edges weaken
|
|
31
|
+
|
|
32
|
+
// Connection engine
|
|
33
|
+
connectionThreshold: number; // Min resonance score to form a connection
|
|
34
|
+
connectionCheckIntervalMs: number;
|
|
35
|
+
|
|
36
|
+
// Consolidation
|
|
37
|
+
consolidationIntervalMs: number; // How often to check for merge candidates
|
|
38
|
+
consolidationSimilarity: number; // Threshold for merging similar engrams
|
|
39
|
+
|
|
40
|
+
// Confidence updates
|
|
41
|
+
feedbackPositiveBoost: number; // How much positive feedback increases confidence
|
|
42
|
+
feedbackNegativePenalty: number; // How much negative feedback decreases confidence
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const DEFAULT_AGENT_CONFIG: AgentConfig = {
|
|
46
|
+
salienceThreshold: 0.4,
|
|
47
|
+
stagingThreshold: 0.2,
|
|
48
|
+
stagingTtlMs: 24 * 60 * 60 * 1000, // 24 hours
|
|
49
|
+
|
|
50
|
+
maxActiveEngrams: 10_000,
|
|
51
|
+
maxStagingEngrams: 1_000,
|
|
52
|
+
maxEdgesPerEngram: 20,
|
|
53
|
+
|
|
54
|
+
activationLimit: 10,
|
|
55
|
+
hebbianRate: 0.25,
|
|
56
|
+
decayExponent: 0.5,
|
|
57
|
+
edgeDecayHalfLifeDays: 7,
|
|
58
|
+
|
|
59
|
+
connectionThreshold: 0.7,
|
|
60
|
+
connectionCheckIntervalMs: 60_000,
|
|
61
|
+
|
|
62
|
+
consolidationIntervalMs: 300_000, // 5 minutes
|
|
63
|
+
consolidationSimilarity: 0.85,
|
|
64
|
+
|
|
65
|
+
feedbackPositiveBoost: 0.05,
|
|
66
|
+
feedbackNegativePenalty: 0.1,
|
|
67
|
+
};
|
package/src/types/checkpoint.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
// Copyright 2026 Robert Winter / Complete Ideas
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
/**
|
|
4
|
-
* Checkpoint types — conscious state preservation across compaction.
|
|
5
|
-
*
|
|
6
|
-
* ConsciousState: explicit structured snapshot (saved by agent)
|
|
7
|
-
* AutoCheckpoint: implicit lightweight tracking (updated on every write/recall)
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export interface ConsciousState {
|
|
11
|
-
currentTask: string;
|
|
12
|
-
decisions: string[];
|
|
13
|
-
activeFiles: string[];
|
|
14
|
-
nextSteps: string[];
|
|
15
|
-
relatedMemoryIds: string[];
|
|
16
|
-
notes: string;
|
|
17
|
-
episodeId: string | null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface AutoCheckpoint {
|
|
21
|
-
lastWriteId: string | null;
|
|
22
|
-
lastRecallContext: string | null;
|
|
23
|
-
lastRecallIds: string[];
|
|
24
|
-
lastActivityAt: Date;
|
|
25
|
-
writeCountSinceConsolidation: number;
|
|
26
|
-
recallCountSinceConsolidation: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface CheckpointRow {
|
|
30
|
-
agentId: string;
|
|
31
|
-
auto: AutoCheckpoint;
|
|
32
|
-
executionState: ConsciousState | null;
|
|
33
|
-
checkpointAt: Date | null;
|
|
34
|
-
lastConsolidationAt: Date | null;
|
|
35
|
-
lastMiniConsolidationAt: Date | null;
|
|
36
|
-
updatedAt: Date;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface RestoreResult {
|
|
40
|
-
executionState: ConsciousState | null;
|
|
41
|
-
checkpointAt: Date | null;
|
|
42
|
-
recalledMemories: Array<{ id: string; concept: string; content: string; score: number }>;
|
|
43
|
-
lastWrite: { id: string; concept: string; content: string } | null;
|
|
44
|
-
idleMs: number;
|
|
45
|
-
miniConsolidationTriggered: boolean;
|
|
46
|
-
}
|
|
1
|
+
// Copyright 2026 Robert Winter / Complete Ideas
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Checkpoint types — conscious state preservation across compaction.
|
|
5
|
+
*
|
|
6
|
+
* ConsciousState: explicit structured snapshot (saved by agent)
|
|
7
|
+
* AutoCheckpoint: implicit lightweight tracking (updated on every write/recall)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface ConsciousState {
|
|
11
|
+
currentTask: string;
|
|
12
|
+
decisions: string[];
|
|
13
|
+
activeFiles: string[];
|
|
14
|
+
nextSteps: string[];
|
|
15
|
+
relatedMemoryIds: string[];
|
|
16
|
+
notes: string;
|
|
17
|
+
episodeId: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AutoCheckpoint {
|
|
21
|
+
lastWriteId: string | null;
|
|
22
|
+
lastRecallContext: string | null;
|
|
23
|
+
lastRecallIds: string[];
|
|
24
|
+
lastActivityAt: Date;
|
|
25
|
+
writeCountSinceConsolidation: number;
|
|
26
|
+
recallCountSinceConsolidation: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CheckpointRow {
|
|
30
|
+
agentId: string;
|
|
31
|
+
auto: AutoCheckpoint;
|
|
32
|
+
executionState: ConsciousState | null;
|
|
33
|
+
checkpointAt: Date | null;
|
|
34
|
+
lastConsolidationAt: Date | null;
|
|
35
|
+
lastMiniConsolidationAt: Date | null;
|
|
36
|
+
updatedAt: Date;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface RestoreResult {
|
|
40
|
+
executionState: ConsciousState | null;
|
|
41
|
+
checkpointAt: Date | null;
|
|
42
|
+
recalledMemories: Array<{ id: string; concept: string; content: string; score: number }>;
|
|
43
|
+
lastWrite: { id: string; concept: string; content: string } | null;
|
|
44
|
+
idleMs: number;
|
|
45
|
+
miniConsolidationTriggered: boolean;
|
|
46
|
+
}
|
package/src/types/engram.ts
CHANGED
|
@@ -1,217 +1,217 @@
|
|
|
1
|
-
// Copyright 2026 Robert Winter / Complete Ideas
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
/**
|
|
4
|
-
* Engram — the fundamental unit of agent memory.
|
|
5
|
-
*
|
|
6
|
-
* An engram represents a single memory trace with salience metadata,
|
|
7
|
-
* staging lifecycle, retraction support, and optional task management.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export interface Engram {
|
|
11
|
-
id: string;
|
|
12
|
-
agentId: string;
|
|
13
|
-
concept: string;
|
|
14
|
-
content: string;
|
|
15
|
-
embedding: number[] | null;
|
|
16
|
-
|
|
17
|
-
// Cognitive scores
|
|
18
|
-
confidence: number; // 0-1 Bayesian posterior — updated on retrieval feedback
|
|
19
|
-
salience: number; // Write-time importance score
|
|
20
|
-
accessCount: number; // For ACT-R decay calculation
|
|
21
|
-
lastAccessed: Date;
|
|
22
|
-
createdAt: Date;
|
|
23
|
-
|
|
24
|
-
// Salience audit trail
|
|
25
|
-
salienceFeatures: SalienceFeatures;
|
|
26
|
-
reasonCodes: string[];
|
|
27
|
-
|
|
28
|
-
// Lifecycle
|
|
29
|
-
stage: EngramStage;
|
|
30
|
-
ttl: number | null; // Milliseconds — only for staging buffer entries
|
|
31
|
-
|
|
32
|
-
// Negative memory
|
|
33
|
-
retracted: boolean;
|
|
34
|
-
retractedBy: string | null; // ID of the engram that invalidated this one
|
|
35
|
-
retractedAt: Date | null;
|
|
36
|
-
|
|
37
|
-
// Tags for concept-based retrieval
|
|
38
|
-
tags: string[];
|
|
39
|
-
|
|
40
|
-
// Episode grouping
|
|
41
|
-
episodeId: string | null;
|
|
42
|
-
|
|
43
|
-
// Memory class
|
|
44
|
-
memoryClass: MemoryClass;
|
|
45
|
-
|
|
46
|
-
// Supersession — "this replaces that" (not retraction — original wasn't wrong, just outdated)
|
|
47
|
-
supersededBy: string | null; // ID of the engram that replaced this one
|
|
48
|
-
supersedes: string | null; // ID of the engram this one replaces
|
|
49
|
-
|
|
50
|
-
// Task management (null = not a task)
|
|
51
|
-
taskStatus: TaskStatus | null;
|
|
52
|
-
taskPriority: TaskPriority | null;
|
|
53
|
-
blockedBy: string | null; // ID of blocking engram/task
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type EngramStage = 'staging' | 'active' | 'consolidated' | 'archived';
|
|
57
|
-
|
|
58
|
-
export type TaskStatus = 'open' | 'in_progress' | 'blocked' | 'done';
|
|
59
|
-
export type TaskPriority = 'urgent' | 'high' | 'medium' | 'low';
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Memory class — controls salience floor and recall priority.
|
|
63
|
-
*
|
|
64
|
-
* canonical: Source-of-truth facts (current state, decisions, architecture).
|
|
65
|
-
* Never goes to staging. Minimum salience 0.7.
|
|
66
|
-
* working: Normal observations, learnings, context (default).
|
|
67
|
-
* Standard salience rules apply.
|
|
68
|
-
* ephemeral: Temporary context (debugging traces, session-specific notes).
|
|
69
|
-
* Stronger time decay, lower recall priority.
|
|
70
|
-
*/
|
|
71
|
-
export type MemoryClass = 'canonical' | 'working' | 'ephemeral';
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Raw feature scores that produced the salience score.
|
|
75
|
-
* Persisted for auditability and tuning.
|
|
76
|
-
*/
|
|
77
|
-
export interface SalienceFeatures {
|
|
78
|
-
surprise: number;
|
|
79
|
-
decisionMade: boolean;
|
|
80
|
-
causalDepth: number;
|
|
81
|
-
resolutionEffort: number;
|
|
82
|
-
eventType: string;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface EngramCreate {
|
|
86
|
-
agentId: string;
|
|
87
|
-
concept: string;
|
|
88
|
-
content: string;
|
|
89
|
-
tags?: string[];
|
|
90
|
-
embedding?: number[];
|
|
91
|
-
salience?: number;
|
|
92
|
-
confidence?: number;
|
|
93
|
-
salienceFeatures?: SalienceFeatures;
|
|
94
|
-
reasonCodes?: string[];
|
|
95
|
-
episodeId?: string;
|
|
96
|
-
ttl?: number;
|
|
97
|
-
memoryClass?: MemoryClass;
|
|
98
|
-
supersedes?: string;
|
|
99
|
-
taskStatus?: TaskStatus;
|
|
100
|
-
taskPriority?: TaskPriority;
|
|
101
|
-
blockedBy?: string;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Association — weighted edge between two engrams.
|
|
106
|
-
* Strengthened by Hebbian co-activation, decays when unused.
|
|
107
|
-
* Capped at MAX_EDGES_PER_ENGRAM to prevent graph explosion.
|
|
108
|
-
*/
|
|
109
|
-
export interface Association {
|
|
110
|
-
id: string;
|
|
111
|
-
fromEngramId: string;
|
|
112
|
-
toEngramId: string;
|
|
113
|
-
weight: number; // Log-space, updated via Hebbian rule
|
|
114
|
-
confidence: number; // Edge-level confidence (separate from node)
|
|
115
|
-
type: AssociationType;
|
|
116
|
-
activationCount: number; // How many times this edge contributed to retrieval
|
|
117
|
-
createdAt: Date;
|
|
118
|
-
lastActivated: Date;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export type AssociationType = 'hebbian' | 'connection' | 'causal' | 'temporal' | 'invalidation' | 'bridge';
|
|
122
|
-
|
|
123
|
-
export const MAX_EDGES_PER_ENGRAM = 20;
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Activation result — returned from the activation pipeline.
|
|
127
|
-
*/
|
|
128
|
-
export interface ActivationResult {
|
|
129
|
-
engram: Engram;
|
|
130
|
-
score: number;
|
|
131
|
-
phaseScores: PhaseScores; // Per-phase breakdown for explainability
|
|
132
|
-
why: string; // Human-readable explanation
|
|
133
|
-
associations: Association[];
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Per-phase scoring breakdown — full audit of how each phase contributed.
|
|
138
|
-
*/
|
|
139
|
-
export interface PhaseScores {
|
|
140
|
-
textMatch: number;
|
|
141
|
-
vectorMatch: number;
|
|
142
|
-
decayScore: number;
|
|
143
|
-
hebbianBoost: number;
|
|
144
|
-
graphBoost: number;
|
|
145
|
-
confidenceGate: number;
|
|
146
|
-
composite: number;
|
|
147
|
-
rerankerScore: number; // Cross-encoder relevance (0-1), 0 if reranker disabled
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export interface ActivationQuery {
|
|
151
|
-
agentId: string;
|
|
152
|
-
context: string;
|
|
153
|
-
limit?: number;
|
|
154
|
-
minScore?: number;
|
|
155
|
-
includeStaging?: boolean;
|
|
156
|
-
includeRetracted?: boolean;
|
|
157
|
-
useReranker?: boolean; // Enable cross-encoder re-ranking (default: true)
|
|
158
|
-
useExpansion?: boolean; // Enable query expansion (default: true)
|
|
159
|
-
abstentionThreshold?: number; // Min reranker score to return results (default: 0)
|
|
160
|
-
internal?: boolean; // Skip access count increment, Hebbian update, and event logging (for system calls)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Search query — deterministic retrieval for diagnostics and debugging.
|
|
165
|
-
* Separate from activation (which is cognitive/associative).
|
|
166
|
-
*/
|
|
167
|
-
export interface SearchQuery {
|
|
168
|
-
agentId: string;
|
|
169
|
-
text?: string; // Exact or partial text match
|
|
170
|
-
concept?: string; // Exact concept match
|
|
171
|
-
tags?: string[]; // Tag filter (AND)
|
|
172
|
-
stage?: EngramStage;
|
|
173
|
-
retracted?: boolean;
|
|
174
|
-
limit?: number;
|
|
175
|
-
offset?: number;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Retrieval feedback — agent reports whether a memory was useful.
|
|
180
|
-
* Used to update confidence scores and eval metrics.
|
|
181
|
-
*/
|
|
182
|
-
export interface RetrievalFeedback {
|
|
183
|
-
engramId: string;
|
|
184
|
-
useful: boolean;
|
|
185
|
-
context: string; // What the agent was doing when it judged usefulness
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Retraction — marks a memory as invalid/wrong.
|
|
190
|
-
*/
|
|
191
|
-
export interface Retraction {
|
|
192
|
-
targetEngramId: string;
|
|
193
|
-
reason: string;
|
|
194
|
-
counterContent?: string; // Optional: what the correct information is
|
|
195
|
-
agentId: string;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Episode — a temporal grouping of engrams from a session or time window.
|
|
200
|
-
* Enables episode-first retrieval: find relevant episodes, then drill into engrams.
|
|
201
|
-
*/
|
|
202
|
-
export interface Episode {
|
|
203
|
-
id: string;
|
|
204
|
-
agentId: string;
|
|
205
|
-
label: string; // Short description (e.g., "Express migration session")
|
|
206
|
-
embedding: number[] | null; // Centroid of member engram embeddings
|
|
207
|
-
engramCount: number;
|
|
208
|
-
startTime: Date;
|
|
209
|
-
endTime: Date;
|
|
210
|
-
createdAt: Date;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export interface EpisodeCreate {
|
|
214
|
-
agentId: string;
|
|
215
|
-
label: string;
|
|
216
|
-
embedding?: number[];
|
|
217
|
-
}
|
|
1
|
+
// Copyright 2026 Robert Winter / Complete Ideas
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Engram — the fundamental unit of agent memory.
|
|
5
|
+
*
|
|
6
|
+
* An engram represents a single memory trace with salience metadata,
|
|
7
|
+
* staging lifecycle, retraction support, and optional task management.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface Engram {
|
|
11
|
+
id: string;
|
|
12
|
+
agentId: string;
|
|
13
|
+
concept: string;
|
|
14
|
+
content: string;
|
|
15
|
+
embedding: number[] | null;
|
|
16
|
+
|
|
17
|
+
// Cognitive scores
|
|
18
|
+
confidence: number; // 0-1 Bayesian posterior — updated on retrieval feedback
|
|
19
|
+
salience: number; // Write-time importance score
|
|
20
|
+
accessCount: number; // For ACT-R decay calculation
|
|
21
|
+
lastAccessed: Date;
|
|
22
|
+
createdAt: Date;
|
|
23
|
+
|
|
24
|
+
// Salience audit trail
|
|
25
|
+
salienceFeatures: SalienceFeatures;
|
|
26
|
+
reasonCodes: string[];
|
|
27
|
+
|
|
28
|
+
// Lifecycle
|
|
29
|
+
stage: EngramStage;
|
|
30
|
+
ttl: number | null; // Milliseconds — only for staging buffer entries
|
|
31
|
+
|
|
32
|
+
// Negative memory
|
|
33
|
+
retracted: boolean;
|
|
34
|
+
retractedBy: string | null; // ID of the engram that invalidated this one
|
|
35
|
+
retractedAt: Date | null;
|
|
36
|
+
|
|
37
|
+
// Tags for concept-based retrieval
|
|
38
|
+
tags: string[];
|
|
39
|
+
|
|
40
|
+
// Episode grouping
|
|
41
|
+
episodeId: string | null;
|
|
42
|
+
|
|
43
|
+
// Memory class
|
|
44
|
+
memoryClass: MemoryClass;
|
|
45
|
+
|
|
46
|
+
// Supersession — "this replaces that" (not retraction — original wasn't wrong, just outdated)
|
|
47
|
+
supersededBy: string | null; // ID of the engram that replaced this one
|
|
48
|
+
supersedes: string | null; // ID of the engram this one replaces
|
|
49
|
+
|
|
50
|
+
// Task management (null = not a task)
|
|
51
|
+
taskStatus: TaskStatus | null;
|
|
52
|
+
taskPriority: TaskPriority | null;
|
|
53
|
+
blockedBy: string | null; // ID of blocking engram/task
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type EngramStage = 'staging' | 'active' | 'consolidated' | 'archived';
|
|
57
|
+
|
|
58
|
+
export type TaskStatus = 'open' | 'in_progress' | 'blocked' | 'done';
|
|
59
|
+
export type TaskPriority = 'urgent' | 'high' | 'medium' | 'low';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Memory class — controls salience floor and recall priority.
|
|
63
|
+
*
|
|
64
|
+
* canonical: Source-of-truth facts (current state, decisions, architecture).
|
|
65
|
+
* Never goes to staging. Minimum salience 0.7.
|
|
66
|
+
* working: Normal observations, learnings, context (default).
|
|
67
|
+
* Standard salience rules apply.
|
|
68
|
+
* ephemeral: Temporary context (debugging traces, session-specific notes).
|
|
69
|
+
* Stronger time decay, lower recall priority.
|
|
70
|
+
*/
|
|
71
|
+
export type MemoryClass = 'canonical' | 'working' | 'ephemeral';
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Raw feature scores that produced the salience score.
|
|
75
|
+
* Persisted for auditability and tuning.
|
|
76
|
+
*/
|
|
77
|
+
export interface SalienceFeatures {
|
|
78
|
+
surprise: number;
|
|
79
|
+
decisionMade: boolean;
|
|
80
|
+
causalDepth: number;
|
|
81
|
+
resolutionEffort: number;
|
|
82
|
+
eventType: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface EngramCreate {
|
|
86
|
+
agentId: string;
|
|
87
|
+
concept: string;
|
|
88
|
+
content: string;
|
|
89
|
+
tags?: string[];
|
|
90
|
+
embedding?: number[];
|
|
91
|
+
salience?: number;
|
|
92
|
+
confidence?: number;
|
|
93
|
+
salienceFeatures?: SalienceFeatures;
|
|
94
|
+
reasonCodes?: string[];
|
|
95
|
+
episodeId?: string;
|
|
96
|
+
ttl?: number;
|
|
97
|
+
memoryClass?: MemoryClass;
|
|
98
|
+
supersedes?: string;
|
|
99
|
+
taskStatus?: TaskStatus;
|
|
100
|
+
taskPriority?: TaskPriority;
|
|
101
|
+
blockedBy?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Association — weighted edge between two engrams.
|
|
106
|
+
* Strengthened by Hebbian co-activation, decays when unused.
|
|
107
|
+
* Capped at MAX_EDGES_PER_ENGRAM to prevent graph explosion.
|
|
108
|
+
*/
|
|
109
|
+
export interface Association {
|
|
110
|
+
id: string;
|
|
111
|
+
fromEngramId: string;
|
|
112
|
+
toEngramId: string;
|
|
113
|
+
weight: number; // Log-space, updated via Hebbian rule
|
|
114
|
+
confidence: number; // Edge-level confidence (separate from node)
|
|
115
|
+
type: AssociationType;
|
|
116
|
+
activationCount: number; // How many times this edge contributed to retrieval
|
|
117
|
+
createdAt: Date;
|
|
118
|
+
lastActivated: Date;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type AssociationType = 'hebbian' | 'connection' | 'causal' | 'temporal' | 'invalidation' | 'bridge';
|
|
122
|
+
|
|
123
|
+
export const MAX_EDGES_PER_ENGRAM = 20;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Activation result — returned from the activation pipeline.
|
|
127
|
+
*/
|
|
128
|
+
export interface ActivationResult {
|
|
129
|
+
engram: Engram;
|
|
130
|
+
score: number;
|
|
131
|
+
phaseScores: PhaseScores; // Per-phase breakdown for explainability
|
|
132
|
+
why: string; // Human-readable explanation
|
|
133
|
+
associations: Association[];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Per-phase scoring breakdown — full audit of how each phase contributed.
|
|
138
|
+
*/
|
|
139
|
+
export interface PhaseScores {
|
|
140
|
+
textMatch: number;
|
|
141
|
+
vectorMatch: number;
|
|
142
|
+
decayScore: number;
|
|
143
|
+
hebbianBoost: number;
|
|
144
|
+
graphBoost: number;
|
|
145
|
+
confidenceGate: number;
|
|
146
|
+
composite: number;
|
|
147
|
+
rerankerScore: number; // Cross-encoder relevance (0-1), 0 if reranker disabled
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface ActivationQuery {
|
|
151
|
+
agentId: string;
|
|
152
|
+
context: string;
|
|
153
|
+
limit?: number;
|
|
154
|
+
minScore?: number;
|
|
155
|
+
includeStaging?: boolean;
|
|
156
|
+
includeRetracted?: boolean;
|
|
157
|
+
useReranker?: boolean; // Enable cross-encoder re-ranking (default: true)
|
|
158
|
+
useExpansion?: boolean; // Enable query expansion (default: true)
|
|
159
|
+
abstentionThreshold?: number; // Min reranker score to return results (default: 0)
|
|
160
|
+
internal?: boolean; // Skip access count increment, Hebbian update, and event logging (for system calls)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Search query — deterministic retrieval for diagnostics and debugging.
|
|
165
|
+
* Separate from activation (which is cognitive/associative).
|
|
166
|
+
*/
|
|
167
|
+
export interface SearchQuery {
|
|
168
|
+
agentId: string;
|
|
169
|
+
text?: string; // Exact or partial text match
|
|
170
|
+
concept?: string; // Exact concept match
|
|
171
|
+
tags?: string[]; // Tag filter (AND)
|
|
172
|
+
stage?: EngramStage;
|
|
173
|
+
retracted?: boolean;
|
|
174
|
+
limit?: number;
|
|
175
|
+
offset?: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Retrieval feedback — agent reports whether a memory was useful.
|
|
180
|
+
* Used to update confidence scores and eval metrics.
|
|
181
|
+
*/
|
|
182
|
+
export interface RetrievalFeedback {
|
|
183
|
+
engramId: string;
|
|
184
|
+
useful: boolean;
|
|
185
|
+
context: string; // What the agent was doing when it judged usefulness
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Retraction — marks a memory as invalid/wrong.
|
|
190
|
+
*/
|
|
191
|
+
export interface Retraction {
|
|
192
|
+
targetEngramId: string;
|
|
193
|
+
reason: string;
|
|
194
|
+
counterContent?: string; // Optional: what the correct information is
|
|
195
|
+
agentId: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Episode — a temporal grouping of engrams from a session or time window.
|
|
200
|
+
* Enables episode-first retrieval: find relevant episodes, then drill into engrams.
|
|
201
|
+
*/
|
|
202
|
+
export interface Episode {
|
|
203
|
+
id: string;
|
|
204
|
+
agentId: string;
|
|
205
|
+
label: string; // Short description (e.g., "Express migration session")
|
|
206
|
+
embedding: number[] | null; // Centroid of member engram embeddings
|
|
207
|
+
engramCount: number;
|
|
208
|
+
startTime: Date;
|
|
209
|
+
endTime: Date;
|
|
210
|
+
createdAt: Date;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface EpisodeCreate {
|
|
214
|
+
agentId: string;
|
|
215
|
+
label: string;
|
|
216
|
+
embedding?: number[];
|
|
217
|
+
}
|