agent-working-memory 0.3.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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +311 -0
  3. package/dist/api/index.d.ts +2 -0
  4. package/dist/api/index.d.ts.map +1 -0
  5. package/dist/api/index.js +2 -0
  6. package/dist/api/index.js.map +1 -0
  7. package/dist/api/routes.d.ts +53 -0
  8. package/dist/api/routes.d.ts.map +1 -0
  9. package/dist/api/routes.js +388 -0
  10. package/dist/api/routes.js.map +1 -0
  11. package/dist/cli.d.ts +12 -0
  12. package/dist/cli.d.ts.map +1 -0
  13. package/dist/cli.js +245 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/core/decay.d.ts +36 -0
  16. package/dist/core/decay.d.ts.map +1 -0
  17. package/dist/core/decay.js +38 -0
  18. package/dist/core/decay.js.map +1 -0
  19. package/dist/core/embeddings.d.ts +33 -0
  20. package/dist/core/embeddings.d.ts.map +1 -0
  21. package/dist/core/embeddings.js +76 -0
  22. package/dist/core/embeddings.js.map +1 -0
  23. package/dist/core/hebbian.d.ts +38 -0
  24. package/dist/core/hebbian.d.ts.map +1 -0
  25. package/dist/core/hebbian.js +74 -0
  26. package/dist/core/hebbian.js.map +1 -0
  27. package/dist/core/index.d.ts +4 -0
  28. package/dist/core/index.d.ts.map +1 -0
  29. package/dist/core/index.js +4 -0
  30. package/dist/core/index.js.map +1 -0
  31. package/dist/core/query-expander.d.ts +24 -0
  32. package/dist/core/query-expander.d.ts.map +1 -0
  33. package/dist/core/query-expander.js +58 -0
  34. package/dist/core/query-expander.js.map +1 -0
  35. package/dist/core/reranker.d.ts +25 -0
  36. package/dist/core/reranker.d.ts.map +1 -0
  37. package/dist/core/reranker.js +75 -0
  38. package/dist/core/reranker.js.map +1 -0
  39. package/dist/core/salience.d.ts +30 -0
  40. package/dist/core/salience.d.ts.map +1 -0
  41. package/dist/core/salience.js +81 -0
  42. package/dist/core/salience.js.map +1 -0
  43. package/dist/engine/activation.d.ts +38 -0
  44. package/dist/engine/activation.d.ts.map +1 -0
  45. package/dist/engine/activation.js +516 -0
  46. package/dist/engine/activation.js.map +1 -0
  47. package/dist/engine/connections.d.ts +31 -0
  48. package/dist/engine/connections.d.ts.map +1 -0
  49. package/dist/engine/connections.js +74 -0
  50. package/dist/engine/connections.js.map +1 -0
  51. package/dist/engine/consolidation-scheduler.d.ts +31 -0
  52. package/dist/engine/consolidation-scheduler.d.ts.map +1 -0
  53. package/dist/engine/consolidation-scheduler.js +115 -0
  54. package/dist/engine/consolidation-scheduler.js.map +1 -0
  55. package/dist/engine/consolidation.d.ts +62 -0
  56. package/dist/engine/consolidation.d.ts.map +1 -0
  57. package/dist/engine/consolidation.js +368 -0
  58. package/dist/engine/consolidation.js.map +1 -0
  59. package/dist/engine/eval.d.ts +22 -0
  60. package/dist/engine/eval.d.ts.map +1 -0
  61. package/dist/engine/eval.js +79 -0
  62. package/dist/engine/eval.js.map +1 -0
  63. package/dist/engine/eviction.d.ts +29 -0
  64. package/dist/engine/eviction.d.ts.map +1 -0
  65. package/dist/engine/eviction.js +86 -0
  66. package/dist/engine/eviction.js.map +1 -0
  67. package/dist/engine/index.d.ts +7 -0
  68. package/dist/engine/index.d.ts.map +1 -0
  69. package/dist/engine/index.js +7 -0
  70. package/dist/engine/index.js.map +1 -0
  71. package/dist/engine/retraction.d.ts +32 -0
  72. package/dist/engine/retraction.d.ts.map +1 -0
  73. package/dist/engine/retraction.js +77 -0
  74. package/dist/engine/retraction.js.map +1 -0
  75. package/dist/engine/staging.d.ts +33 -0
  76. package/dist/engine/staging.d.ts.map +1 -0
  77. package/dist/engine/staging.js +63 -0
  78. package/dist/engine/staging.js.map +1 -0
  79. package/dist/index.d.ts +2 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +95 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/mcp.d.ts +24 -0
  84. package/dist/mcp.d.ts.map +1 -0
  85. package/dist/mcp.js +532 -0
  86. package/dist/mcp.js.map +1 -0
  87. package/dist/storage/index.d.ts +2 -0
  88. package/dist/storage/index.d.ts.map +1 -0
  89. package/dist/storage/index.js +2 -0
  90. package/dist/storage/index.js.map +1 -0
  91. package/dist/storage/sqlite.d.ts +116 -0
  92. package/dist/storage/sqlite.d.ts.map +1 -0
  93. package/dist/storage/sqlite.js +750 -0
  94. package/dist/storage/sqlite.js.map +1 -0
  95. package/dist/types/agent.d.ts +30 -0
  96. package/dist/types/agent.d.ts.map +1 -0
  97. package/dist/types/agent.js +23 -0
  98. package/dist/types/agent.js.map +1 -0
  99. package/dist/types/checkpoint.d.ts +50 -0
  100. package/dist/types/checkpoint.d.ts.map +1 -0
  101. package/dist/types/checkpoint.js +8 -0
  102. package/dist/types/checkpoint.js.map +1 -0
  103. package/dist/types/engram.d.ts +165 -0
  104. package/dist/types/engram.d.ts.map +1 -0
  105. package/dist/types/engram.js +8 -0
  106. package/dist/types/engram.js.map +1 -0
  107. package/dist/types/eval.d.ts +84 -0
  108. package/dist/types/eval.d.ts.map +1 -0
  109. package/dist/types/eval.js +11 -0
  110. package/dist/types/eval.js.map +1 -0
  111. package/dist/types/index.d.ts +5 -0
  112. package/dist/types/index.d.ts.map +1 -0
  113. package/dist/types/index.js +5 -0
  114. package/dist/types/index.js.map +1 -0
  115. package/package.json +55 -0
  116. package/src/api/index.ts +1 -0
  117. package/src/api/routes.ts +528 -0
  118. package/src/cli.ts +260 -0
  119. package/src/core/decay.ts +61 -0
  120. package/src/core/embeddings.ts +82 -0
  121. package/src/core/hebbian.ts +91 -0
  122. package/src/core/index.ts +3 -0
  123. package/src/core/query-expander.ts +64 -0
  124. package/src/core/reranker.ts +99 -0
  125. package/src/core/salience.ts +95 -0
  126. package/src/engine/activation.ts +577 -0
  127. package/src/engine/connections.ts +101 -0
  128. package/src/engine/consolidation-scheduler.ts +123 -0
  129. package/src/engine/consolidation.ts +443 -0
  130. package/src/engine/eval.ts +100 -0
  131. package/src/engine/eviction.ts +99 -0
  132. package/src/engine/index.ts +6 -0
  133. package/src/engine/retraction.ts +98 -0
  134. package/src/engine/staging.ts +72 -0
  135. package/src/index.ts +100 -0
  136. package/src/mcp.ts +635 -0
  137. package/src/storage/index.ts +1 -0
  138. package/src/storage/sqlite.ts +893 -0
  139. package/src/types/agent.ts +65 -0
  140. package/src/types/checkpoint.ts +44 -0
  141. package/src/types/engram.ts +194 -0
  142. package/src/types/eval.ts +98 -0
  143. package/src/types/index.ts +4 -0
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Agent — a consciousness boundary.
3
+ * Each agent has its own isolated memory space with capacity budgets.
4
+ */
5
+
6
+ export interface Agent {
7
+ id: string;
8
+ name: string;
9
+ createdAt: Date;
10
+ config: AgentConfig;
11
+ }
12
+
13
+ export interface AgentConfig {
14
+ // Salience filter thresholds
15
+ salienceThreshold: number; // Below this → discard
16
+ stagingThreshold: number; // Below salience but above this → staging buffer
17
+ stagingTtlMs: number; // Default TTL for staging entries
18
+
19
+ // Capacity budgets (eviction triggers when exceeded)
20
+ maxActiveEngrams: number; // Hard cap on active memory
21
+ maxStagingEngrams: number; // Hard cap on staging buffer
22
+ maxEdgesPerEngram: number; // Prevent graph explosion
23
+
24
+ // Activation pipeline tuning
25
+ activationLimit: number; // Max results per activation query
26
+ hebbianRate: number; // Learning rate for association strengthening
27
+ decayExponent: number; // ACT-R d parameter (default 0.5)
28
+ edgeDecayHalfLifeDays: number; // How fast unused edges weaken
29
+
30
+ // Connection engine
31
+ connectionThreshold: number; // Min resonance score to form a connection
32
+ connectionCheckIntervalMs: number;
33
+
34
+ // Consolidation
35
+ consolidationIntervalMs: number; // How often to check for merge candidates
36
+ consolidationSimilarity: number; // Threshold for merging similar engrams
37
+
38
+ // Confidence updates
39
+ feedbackPositiveBoost: number; // How much positive feedback increases confidence
40
+ feedbackNegativePenalty: number; // How much negative feedback decreases confidence
41
+ }
42
+
43
+ export const DEFAULT_AGENT_CONFIG: AgentConfig = {
44
+ salienceThreshold: 0.4,
45
+ stagingThreshold: 0.2,
46
+ stagingTtlMs: 24 * 60 * 60 * 1000, // 24 hours
47
+
48
+ maxActiveEngrams: 10_000,
49
+ maxStagingEngrams: 1_000,
50
+ maxEdgesPerEngram: 20,
51
+
52
+ activationLimit: 10,
53
+ hebbianRate: 0.25,
54
+ decayExponent: 0.5,
55
+ edgeDecayHalfLifeDays: 7,
56
+
57
+ connectionThreshold: 0.7,
58
+ connectionCheckIntervalMs: 60_000,
59
+
60
+ consolidationIntervalMs: 300_000, // 5 minutes
61
+ consolidationSimilarity: 0.85,
62
+
63
+ feedbackPositiveBoost: 0.05,
64
+ feedbackNegativePenalty: 0.1,
65
+ };
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Checkpoint types — conscious state preservation across compaction.
3
+ *
4
+ * ConsciousState: explicit structured snapshot (saved by agent)
5
+ * AutoCheckpoint: implicit lightweight tracking (updated on every write/recall)
6
+ */
7
+
8
+ export interface ConsciousState {
9
+ currentTask: string;
10
+ decisions: string[];
11
+ activeFiles: string[];
12
+ nextSteps: string[];
13
+ relatedMemoryIds: string[];
14
+ notes: string;
15
+ episodeId: string | null;
16
+ }
17
+
18
+ export interface AutoCheckpoint {
19
+ lastWriteId: string | null;
20
+ lastRecallContext: string | null;
21
+ lastRecallIds: string[];
22
+ lastActivityAt: Date;
23
+ writeCountSinceConsolidation: number;
24
+ recallCountSinceConsolidation: number;
25
+ }
26
+
27
+ export interface CheckpointRow {
28
+ agentId: string;
29
+ auto: AutoCheckpoint;
30
+ executionState: ConsciousState | null;
31
+ checkpointAt: Date | null;
32
+ lastConsolidationAt: Date | null;
33
+ lastMiniConsolidationAt: Date | null;
34
+ updatedAt: Date;
35
+ }
36
+
37
+ export interface RestoreResult {
38
+ executionState: ConsciousState | null;
39
+ checkpointAt: Date | null;
40
+ recalledMemories: Array<{ id: string; concept: string; content: string; score: number }>;
41
+ lastWrite: { id: string; concept: string; content: string } | null;
42
+ idleMs: number;
43
+ miniConsolidationTriggered: boolean;
44
+ }
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Engram — the fundamental unit of agent memory.
3
+ *
4
+ * An engram represents a single memory trace with salience metadata,
5
+ * staging lifecycle, retraction support, and optional task management.
6
+ */
7
+
8
+ export interface Engram {
9
+ id: string;
10
+ agentId: string;
11
+ concept: string;
12
+ content: string;
13
+ embedding: number[] | null;
14
+
15
+ // Cognitive scores
16
+ confidence: number; // 0-1 Bayesian posterior — updated on retrieval feedback
17
+ salience: number; // Write-time importance score
18
+ accessCount: number; // For ACT-R decay calculation
19
+ lastAccessed: Date;
20
+ createdAt: Date;
21
+
22
+ // Salience audit trail
23
+ salienceFeatures: SalienceFeatures;
24
+ reasonCodes: string[];
25
+
26
+ // Lifecycle
27
+ stage: EngramStage;
28
+ ttl: number | null; // Milliseconds — only for staging buffer entries
29
+
30
+ // Negative memory
31
+ retracted: boolean;
32
+ retractedBy: string | null; // ID of the engram that invalidated this one
33
+ retractedAt: Date | null;
34
+
35
+ // Tags for concept-based retrieval
36
+ tags: string[];
37
+
38
+ // Episode grouping
39
+ episodeId: string | null;
40
+
41
+ // Task management (null = not a task)
42
+ taskStatus: TaskStatus | null;
43
+ taskPriority: TaskPriority | null;
44
+ blockedBy: string | null; // ID of blocking engram/task
45
+ }
46
+
47
+ export type EngramStage = 'staging' | 'active' | 'consolidated' | 'archived';
48
+
49
+ export type TaskStatus = 'open' | 'in_progress' | 'blocked' | 'done';
50
+ export type TaskPriority = 'urgent' | 'high' | 'medium' | 'low';
51
+
52
+ /**
53
+ * Raw feature scores that produced the salience score.
54
+ * Persisted for auditability and tuning.
55
+ */
56
+ export interface SalienceFeatures {
57
+ surprise: number;
58
+ decisionMade: boolean;
59
+ causalDepth: number;
60
+ resolutionEffort: number;
61
+ eventType: string;
62
+ }
63
+
64
+ export interface EngramCreate {
65
+ agentId: string;
66
+ concept: string;
67
+ content: string;
68
+ tags?: string[];
69
+ embedding?: number[];
70
+ salience?: number;
71
+ confidence?: number;
72
+ salienceFeatures?: SalienceFeatures;
73
+ reasonCodes?: string[];
74
+ episodeId?: string;
75
+ ttl?: number;
76
+ taskStatus?: TaskStatus;
77
+ taskPriority?: TaskPriority;
78
+ blockedBy?: string;
79
+ }
80
+
81
+ /**
82
+ * Association — weighted edge between two engrams.
83
+ * Strengthened by Hebbian co-activation, decays when unused.
84
+ * Capped at MAX_EDGES_PER_ENGRAM to prevent graph explosion.
85
+ */
86
+ export interface Association {
87
+ id: string;
88
+ fromEngramId: string;
89
+ toEngramId: string;
90
+ weight: number; // Log-space, updated via Hebbian rule
91
+ confidence: number; // Edge-level confidence (separate from node)
92
+ type: AssociationType;
93
+ activationCount: number; // How many times this edge contributed to retrieval
94
+ createdAt: Date;
95
+ lastActivated: Date;
96
+ }
97
+
98
+ export type AssociationType = 'hebbian' | 'connection' | 'causal' | 'temporal' | 'invalidation' | 'bridge';
99
+
100
+ export const MAX_EDGES_PER_ENGRAM = 20;
101
+
102
+ /**
103
+ * Activation result — returned from the activation pipeline.
104
+ */
105
+ export interface ActivationResult {
106
+ engram: Engram;
107
+ score: number;
108
+ phaseScores: PhaseScores; // Per-phase breakdown for explainability
109
+ why: string; // Human-readable explanation
110
+ associations: Association[];
111
+ }
112
+
113
+ /**
114
+ * Per-phase scoring breakdown — full audit of how each phase contributed.
115
+ */
116
+ export interface PhaseScores {
117
+ textMatch: number;
118
+ vectorMatch: number;
119
+ decayScore: number;
120
+ hebbianBoost: number;
121
+ graphBoost: number;
122
+ confidenceGate: number;
123
+ composite: number;
124
+ rerankerScore: number; // Cross-encoder relevance (0-1), 0 if reranker disabled
125
+ }
126
+
127
+ export interface ActivationQuery {
128
+ agentId: string;
129
+ context: string;
130
+ limit?: number;
131
+ minScore?: number;
132
+ includeStaging?: boolean;
133
+ includeRetracted?: boolean;
134
+ useReranker?: boolean; // Enable cross-encoder re-ranking (default: true)
135
+ useExpansion?: boolean; // Enable query expansion (default: true)
136
+ abstentionThreshold?: number; // Min reranker score to return results (default: 0)
137
+ internal?: boolean; // Skip access count increment, Hebbian update, and event logging (for system calls)
138
+ }
139
+
140
+ /**
141
+ * Search query — deterministic retrieval for diagnostics and debugging.
142
+ * Separate from activation (which is cognitive/associative).
143
+ */
144
+ export interface SearchQuery {
145
+ agentId: string;
146
+ text?: string; // Exact or partial text match
147
+ concept?: string; // Exact concept match
148
+ tags?: string[]; // Tag filter (AND)
149
+ stage?: EngramStage;
150
+ retracted?: boolean;
151
+ limit?: number;
152
+ offset?: number;
153
+ }
154
+
155
+ /**
156
+ * Retrieval feedback — agent reports whether a memory was useful.
157
+ * Used to update confidence scores and eval metrics.
158
+ */
159
+ export interface RetrievalFeedback {
160
+ engramId: string;
161
+ useful: boolean;
162
+ context: string; // What the agent was doing when it judged usefulness
163
+ }
164
+
165
+ /**
166
+ * Retraction — marks a memory as invalid/wrong.
167
+ */
168
+ export interface Retraction {
169
+ targetEngramId: string;
170
+ reason: string;
171
+ counterContent?: string; // Optional: what the correct information is
172
+ agentId: string;
173
+ }
174
+
175
+ /**
176
+ * Episode — a temporal grouping of engrams from a session or time window.
177
+ * Enables episode-first retrieval: find relevant episodes, then drill into engrams.
178
+ */
179
+ export interface Episode {
180
+ id: string;
181
+ agentId: string;
182
+ label: string; // Short description (e.g., "Express migration session")
183
+ embedding: number[] | null; // Centroid of member engram embeddings
184
+ engramCount: number;
185
+ startTime: Date;
186
+ endTime: Date;
187
+ createdAt: Date;
188
+ }
189
+
190
+ export interface EpisodeCreate {
191
+ agentId: string;
192
+ label: string;
193
+ embedding?: number[];
194
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Evaluation types — measuring whether memory actually helps.
3
+ *
4
+ * Four measurement dimensions:
5
+ * 1. Retrieval quality (precision, recall, latency)
6
+ * 2. Connection quality (edge utility, stability)
7
+ * 3. Staging accuracy (promotion precision, discard regret)
8
+ * 4. Task impact (with/without memory comparison)
9
+ */
10
+
11
+ /**
12
+ * Single activation event record — logged for offline analysis.
13
+ */
14
+ export interface ActivationEvent {
15
+ id: string;
16
+ agentId: string;
17
+ timestamp: Date;
18
+ context: string;
19
+ resultsReturned: number;
20
+ topScore: number;
21
+ latencyMs: number;
22
+ engramIds: string[];
23
+ feedback?: RetrievalFeedbackEvent[];
24
+ }
25
+
26
+ export interface RetrievalFeedbackEvent {
27
+ engramId: string;
28
+ useful: boolean;
29
+ timestamp: Date;
30
+ }
31
+
32
+ /**
33
+ * Staging lifecycle event — tracks promote/discard decisions.
34
+ */
35
+ export interface StagingEvent {
36
+ engramId: string;
37
+ agentId: string;
38
+ action: 'promoted' | 'discarded' | 'expired';
39
+ resonanceScore: number | null;
40
+ timestamp: Date;
41
+ ageMs: number; // How long it lived in staging
42
+ }
43
+
44
+ /**
45
+ * Aggregate metrics snapshot — computed periodically.
46
+ */
47
+ export interface EvalMetrics {
48
+ agentId: string;
49
+ timestamp: Date;
50
+ window: string; // e.g., "24h", "7d"
51
+
52
+ // Retrieval quality
53
+ activationCount: number;
54
+ avgPrecisionAtK: number; // Of returned results, % judged useful
55
+ avgLatencyMs: number;
56
+ p95LatencyMs: number;
57
+
58
+ // Connection quality
59
+ totalEdges: number;
60
+ edgesUsedInActivation: number;
61
+ edgeUtilityRate: number; // % of edges that contributed to retrieval
62
+ avgEdgeSurvivalDays: number;
63
+
64
+ // Staging accuracy
65
+ totalStaged: number;
66
+ promotedCount: number;
67
+ discardedCount: number;
68
+ promotionPrecision: number; // % of promoted items later used
69
+ discardRegret: number; // % of discarded items agent re-introduced
70
+
71
+ // Memory health
72
+ activeEngramCount: number;
73
+ stagingEngramCount: number;
74
+ retractedCount: number;
75
+ consolidatedCount: number;
76
+ avgConfidence: number;
77
+
78
+ // Contamination tracking
79
+ staleUsageCount: number; // Activations using outdated engrams
80
+ retractionRate: number; // Rate of memories being invalidated
81
+ }
82
+
83
+ /**
84
+ * Task trial — for with/without memory comparison.
85
+ */
86
+ export interface TaskTrial {
87
+ id: string;
88
+ agentId: string;
89
+ taskDescription: string;
90
+ memoryEnabled: boolean;
91
+ startedAt: Date;
92
+ completedAt: Date | null;
93
+ success: boolean | null;
94
+ stepsToCompletion: number;
95
+ errorsEncountered: number;
96
+ memoriesActivated: number;
97
+ userCorrections: number;
98
+ }
@@ -0,0 +1,4 @@
1
+ export * from './engram.js';
2
+ export * from './agent.js';
3
+ export * from './eval.js';
4
+ export * from './checkpoint.js';