@cogmem/engram 0.0.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/README.md ADDED
@@ -0,0 +1,236 @@
1
+ # engram
2
+
3
+ **Human memory for artificial minds.**
4
+
5
+ > **Note:** This is an experiment in cognitive memory architecture. It's a research prototype, not production software.
6
+
7
+ Every AI agent today has amnesia. They process, respond, and forget. engram fixes this — not with a smarter key-value store, but with a cognitive memory system modeled on how the human brain actually forms, stores, recalls, and forgets information.
8
+
9
+ The name comes from neuroscience: an **engram** is the physical trace a memory leaves in the brain.
10
+
11
+ ## Installation
12
+
13
+ Requires [Bun](https://bun.sh) v1.0+.
14
+
15
+ ```bash
16
+ # Run directly (no install)
17
+ bunx @cogmem/engram
18
+
19
+ # Or install globally
20
+ bun install -g @cogmem/engram
21
+ engram --help
22
+ ```
23
+
24
+ ## The Science
25
+
26
+ engram is built on memory research. Every design decision traces back to how the brain operates.
27
+
28
+ ### Memory Systems
29
+
30
+ The brain has distinct memory systems with different properties:
31
+
32
+ | System | Brain Region | Duration | engram Mapping |
33
+ |---|---|---|---|
34
+ | **Working Memory** | Prefrontal Cortex | Seconds | `engram focus` — capacity-limited buffer (Miller's Law: 7 ± 2 items) |
35
+ | **Episodic Memory** | Hippocampus → Neocortex | Minutes to lifetime | Contextual experiences — the *what, when, where, how it felt* |
36
+ | **Semantic Memory** | Neocortex | Very long-term | Facts and concepts, detached from when you learned them |
37
+ | **Procedural Memory** | Basal Ganglia | Lifetime | Skills and habits — immune to decay, expressed through action |
38
+
39
+ ### ACT-R Activation Model
40
+
41
+ Memory retrieval uses the [ACT-R cognitive architecture](https://act-r.psy.cmu.edu/about/) (Anderson, 1993), the most validated computational model of human memory.
42
+
43
+ **Total activation** of a memory determines whether it can be recalled:
44
+
45
+ ```
46
+ A_i = B_i + Σ(W_j · S_ji) + ε
47
+ ```
48
+
49
+ - `B_i` = base-level activation (how inherently strong the memory is)
50
+ - `Σ(W_j · S_ji)` = spreading activation from associated memories
51
+ - `ε` = stochastic noise (recall isn't perfectly deterministic)
52
+
53
+ **Base-level activation** follows the power law of forgetting:
54
+
55
+ ```
56
+ B_i = ln(Σ t_j^{-d})
57
+ ```
58
+
59
+ Where `n` = number of accesses, `t_j` = time since j-th access, `d` ≈ 0.5. This captures two human behaviors: **recency** (recent accesses contribute more) and **frequency** (more accesses = higher activation).
60
+
61
+ **Retrieval threshold**: A memory can only be recalled if `A_i > τ`. Below this, it's effectively "forgotten" — it still exists but can't be accessed.
62
+
63
+ **Retrieval latency**: `Time = F · e^{-f·A_i}` — stronger memories are recalled faster. Weak memories take longer (the "tip of the tongue" feeling).
64
+
65
+ ### Ebbinghaus Forgetting Curve
66
+
67
+ Retention decays exponentially without reinforcement (Ebbinghaus, 1885):
68
+
69
+ ```
70
+ R(t) = e^{-t/S}
71
+ ```
72
+
73
+ Where `S` (memory strength) increases with recall count, emotional weight, and number of associative links.
74
+
75
+ ### Spreading Activation
76
+
77
+ When one memory is activated, activation spreads along associative links to related memories (Collins & Loftus, 1975). Thinking of "coffee" activates "morning" → "commute" → "that conversation." The spreading strength is:
78
+
79
+ ```
80
+ S_ji = S - ln(fan_j)
81
+ ```
82
+
83
+ Memories with many connections receive *less* boost from each (diffusion). Specific cues work better than generic ones.
84
+
85
+ ### Consolidation (Sleep)
86
+
87
+ During sleep, the brain replays, strengthens, prunes, extracts patterns, and discovers connections. engram's `sleep` command mirrors this:
88
+
89
+ 1. **Replay** — refresh activation levels for all memories
90
+ 2. **Strengthen** — boost frequently-accessed memories (2+ accesses in 24h)
91
+ 3. **Prune** — remove memories below activation threshold
92
+ 4. **Extract** — distill repeated episodic patterns into semantic facts
93
+ 5. **Link** — discover temporal and semantic associations
94
+
95
+ ### Reconsolidation
96
+
97
+ When you recall a memory, it temporarily becomes unstable and can be modified (Nader et al., 2000). It then re-stabilizes with updates incorporated. **Every act of remembering is also an act of rewriting.**
98
+
99
+ ### Emotional Modulation
100
+
101
+ The amygdala modulates encoding strength. High-arousal emotions (anxiety, surprise) produce stronger memory traces than low-arousal states. Emotional memories decay slower.
102
+
103
+ ## CLI Usage
104
+
105
+ ### Encoding Memories
106
+
107
+ ```bash
108
+ # Semantic memory (facts, knowledge)
109
+ engram encode "TypeScript is a superset of JavaScript" --type semantic
110
+
111
+ # Episodic memory (experiences with context)
112
+ engram encode "deployed v2.0 to prod at 3am, monitoring broke" \
113
+ --type episodic --emotion anxiety --context "project:acme"
114
+
115
+ # Procedural memory (skills, immune to decay)
116
+ engram encode "always run smoke tests before deploying" --type procedural
117
+ ```
118
+
119
+ ### Recalling Memories
120
+
121
+ ```bash
122
+ # Associative recall — cue activates related memories via spreading activation
123
+ engram recall "deployment issues"
124
+
125
+ # Filter by type or context
126
+ engram recall "user preferences" --type semantic
127
+ engram recall "incidents" --context "project:acme"
128
+
129
+ # Disable spreading activation
130
+ engram recall "TypeScript" --no-associative
131
+ ```
132
+
133
+ ### Working Memory
134
+
135
+ ```bash
136
+ engram focus "refactoring the auth module" # push to working memory
137
+ engram focus # view current focus
138
+ engram focus --pop # remove most recent
139
+ engram focus --clear # clear all
140
+ ```
141
+
142
+ ### Consolidation (Sleep)
143
+
144
+ ```bash
145
+ engram sleep # run full consolidation cycle
146
+ engram sleep --report # with detailed report
147
+ ```
148
+
149
+ ### Inspection
150
+
151
+ ```bash
152
+ engram stats # memory system health overview
153
+ engram health # diagnostic health check
154
+ engram inspect <memory-id> # examine a memory's full lifecycle
155
+ ```
156
+
157
+ ## MCP Server
158
+
159
+ engram exposes its cognitive model as an MCP (Model Context Protocol) server, so AI agents can use it as a memory backend.
160
+
161
+ ### Setup
162
+
163
+ Add to your MCP client configuration (e.g., Claude Code `settings.json`):
164
+
165
+ ```json
166
+ {
167
+ "mcpServers": {
168
+ "engram": {
169
+ "command": "bunx",
170
+ "args": ["@cogmem/engram-mcp"]
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ ### Available Tools
177
+
178
+ | Tool | Description |
179
+ |---|---|
180
+ | `memory_store` | Encode new memories or reconsolidate existing ones |
181
+ | `memory_recall` | Cue-based retrieval, memory inspection, or system stats |
182
+ | `memory_manage` | Run consolidation or manage working memory |
183
+
184
+ ## Programmatic API
185
+
186
+ ```typescript
187
+ import { EngramEngine, encode, recall, consolidate } from "engram";
188
+
189
+ const engine = EngramEngine.inMemory();
190
+
191
+ // Encode
192
+ const memory = encode(engine.storage, {
193
+ content: "important fact",
194
+ type: "semantic",
195
+ emotion: "curiosity",
196
+ }, engine.config);
197
+
198
+ // Recall
199
+ const results = recall(engine.storage, "important", engine.config);
200
+
201
+ // Consolidate
202
+ const report = consolidate(engine.storage, engine.config);
203
+
204
+ engine.close();
205
+ ```
206
+
207
+ ## Configuration
208
+
209
+ Cognitive parameters can be tuned via environment variables or the `loadConfig()` function:
210
+
211
+ | Parameter | Default | Env Variable | Description |
212
+ |---|---|---|---|
213
+ | `decayRate` | 0.5 | `ENGRAM_DECAY_RATE` | ACT-R power law decay parameter |
214
+ | `retrievalThreshold` | -1.0 | `ENGRAM_RETRIEVAL_THRESHOLD` | Minimum activation for recall |
215
+ | `workingMemoryCapacity` | 7 | `ENGRAM_WM_CAPACITY` | Miller's Law capacity limit |
216
+ | `dbPath` | `~/.engram/memory.db` | `ENGRAM_DB_PATH` | SQLite database location |
217
+
218
+ All parameters are also configurable programmatically:
219
+
220
+ ```typescript
221
+ import { EngramEngine } from "engram";
222
+
223
+ const engine = new EngramEngine({
224
+ decayRate: 0.3,
225
+ workingMemoryCapacity: 5,
226
+ emotionalBoostFactor: 3.0,
227
+ });
228
+ ```
229
+
230
+ ## References
231
+
232
+ - Anderson, J.R. (1993). *Rules of the Mind*. ACT-R Cognitive Architecture.
233
+ - Ebbinghaus, H. (1885). *Uber das Gedachtnis*. Memory and forgetting curves.
234
+ - Collins, A.M. & Loftus, E.F. (1975). A spreading-activation theory of semantic processing.
235
+ - Nader, K., Schafe, G.E. & Le Doux, J.E. (2000). Fear memories require protein synthesis in the amygdala for reconsolidation after retrieval.
236
+ - Miller, G.A. (1956). The magical number seven, plus or minus two.
@@ -0,0 +1,59 @@
1
+ CREATE TABLE `access_log` (
2
+ `id` text PRIMARY KEY NOT NULL,
3
+ `memory_id` text NOT NULL,
4
+ `accessed_at` integer NOT NULL,
5
+ `access_type` text NOT NULL,
6
+ FOREIGN KEY (`memory_id`) REFERENCES `memories`(`id`) ON UPDATE no action ON DELETE cascade
7
+ );
8
+ --> statement-breakpoint
9
+ CREATE INDEX `idx_access_log_memory_id` ON `access_log` (`memory_id`);--> statement-breakpoint
10
+ CREATE INDEX `idx_access_log_accessed_at` ON `access_log` (`accessed_at`);--> statement-breakpoint
11
+ CREATE TABLE `associations` (
12
+ `id` text PRIMARY KEY NOT NULL,
13
+ `source_id` text NOT NULL,
14
+ `target_id` text NOT NULL,
15
+ `strength` real DEFAULT 0.5 NOT NULL,
16
+ `formed_at` integer NOT NULL,
17
+ `type` text NOT NULL,
18
+ FOREIGN KEY (`source_id`) REFERENCES `memories`(`id`) ON UPDATE no action ON DELETE cascade,
19
+ FOREIGN KEY (`target_id`) REFERENCES `memories`(`id`) ON UPDATE no action ON DELETE cascade
20
+ );
21
+ --> statement-breakpoint
22
+ CREATE INDEX `idx_associations_source` ON `associations` (`source_id`);--> statement-breakpoint
23
+ CREATE INDEX `idx_associations_target` ON `associations` (`target_id`);--> statement-breakpoint
24
+ CREATE UNIQUE INDEX `unique_source_target` ON `associations` (`source_id`,`target_id`);--> statement-breakpoint
25
+ CREATE TABLE `consolidation_log` (
26
+ `id` text PRIMARY KEY NOT NULL,
27
+ `ran_at` integer NOT NULL,
28
+ `memories_strengthened` integer DEFAULT 0 NOT NULL,
29
+ `memories_pruned` integer DEFAULT 0 NOT NULL,
30
+ `facts_extracted` integer DEFAULT 0 NOT NULL,
31
+ `associations_discovered` integer DEFAULT 0 NOT NULL
32
+ );
33
+ --> statement-breakpoint
34
+ CREATE TABLE `memories` (
35
+ `id` text PRIMARY KEY NOT NULL,
36
+ `type` text NOT NULL,
37
+ `content` text NOT NULL,
38
+ `encoded_at` integer NOT NULL,
39
+ `last_recalled_at` integer,
40
+ `recall_count` integer DEFAULT 0 NOT NULL,
41
+ `activation` real DEFAULT 0 NOT NULL,
42
+ `emotion` text DEFAULT 'neutral' NOT NULL,
43
+ `emotion_weight` real DEFAULT 0 NOT NULL,
44
+ `context` text,
45
+ `chunk_id` text,
46
+ `reconsolidation_count` integer DEFAULT 0 NOT NULL
47
+ );
48
+ --> statement-breakpoint
49
+ CREATE INDEX `idx_memories_type` ON `memories` (`type`);--> statement-breakpoint
50
+ CREATE INDEX `idx_memories_activation` ON `memories` (`activation`);--> statement-breakpoint
51
+ CREATE INDEX `idx_memories_encoded_at` ON `memories` (`encoded_at`);--> statement-breakpoint
52
+ CREATE INDEX `idx_memories_context` ON `memories` (`context`);--> statement-breakpoint
53
+ CREATE INDEX `idx_memories_chunk_id` ON `memories` (`chunk_id`);--> statement-breakpoint
54
+ CREATE TABLE `working_memory` (
55
+ `slot` integer PRIMARY KEY NOT NULL,
56
+ `memory_ref` text,
57
+ `content` text NOT NULL,
58
+ `pushed_at` integer NOT NULL
59
+ );