brainmem 0.1.0__tar.gz

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,14 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ .venv/
7
+ venv/
8
+ .pytest_cache/
9
+ .ruff_cache/
10
+ *.db
11
+ maintain.log
12
+
13
+ # Working artefacts from the agent that built this, not part of the project.
14
+ docs/superpowers/
brainmem-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,304 @@
1
+ Metadata-Version: 2.4
2
+ Name: brainmem
3
+ Version: 0.1.0
4
+ Summary: Agent memory gated on surprise, budgeted on attention, with provenance and validity intervals.
5
+ Project-URL: Homepage, https://github.com/Jimmycarroll2021/Brainmem
6
+ Project-URL: Issues, https://github.com/Jimmycarroll2021/Brainmem/issues
7
+ Author: Jimmi Carroll
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Keywords: agent,claude,llm,mcp,memory,rag,retrieval
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: numpy>=1.24
20
+ Provides-Extra: anthropic
21
+ Requires-Dist: anthropic>=0.40; extra == 'anthropic'
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=8.0; extra == 'dev'
24
+ Requires-Dist: ruff>=0.6; extra == 'dev'
25
+ Provides-Extra: embeddings
26
+ Requires-Dist: sentence-transformers>=3.0; extra == 'embeddings'
27
+ Provides-Extra: mcp
28
+ Requires-Dist: mcp[cli]>=1.0; extra == 'mcp'
29
+ Description-Content-Type: text/markdown
30
+
31
+ # brainmem
32
+
33
+ [![CI](https://github.com/Jimmycarroll2021/Brainmem/actions/workflows/ci.yml/badge.svg)](https://github.com/Jimmycarroll2021/Brainmem/actions/workflows/ci.yml)
34
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/downloads/)
35
+ [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
36
+
37
+ Agent memory built around the constraint that actually binds: storage is free, attention is not. Encoding is gated on surprise, retrieval is budgeted, and beliefs carry validity intervals and provenance so you can answer *what did the agent believe, when, and on what evidence*.
38
+
39
+ ## Install
40
+
41
+ ```bash
42
+ pip install brainmem # core; numpy is the only dependency
43
+ pip install 'brainmem[mcp,embeddings]' # MCP tools + real semantic retrieval
44
+ ./install.sh # wire into Claude Code (PREFIX=… to relocate)
45
+ ```
46
+
47
+ ## 60 seconds
48
+
49
+ ```python
50
+ from brainmem import Memory
51
+
52
+ m = Memory("memory.db")
53
+ m.encode("Validation of the 60MB CSV timed out.", outcome=False)
54
+ m.encode("Chunking the CSV to 20MB completed validation.", outcome=True)
55
+ m.consolidate() # distil episodes into durable facts
56
+
57
+ print(m.context("run the validation batch", token_budget=600))
58
+ # ## What has gone wrong before
59
+ # - [1] Avoid: Validation of the 60MB CSV timed out (unverified, n=1)
60
+ # ## What I know
61
+ # - [2] Chunking the CSV to 20MB completed validation (conf 0.60, n=1)
62
+
63
+ m.record_outcome(2, success=True) # close the loop; this is what makes it learn
64
+ ```
65
+
66
+ Or from the shell: `brainmem encode "..." --outcome fail`, `brainmem retrieve "..."`, `brainmem stats`.
67
+
68
+ `install.sh` writes `~/.brainmem/settings-brainmem.json` with **absolute paths baked
69
+ in**, then merge it into `~/.claude/settings.json`. Do not substitute `$HOME` or `~`:
70
+ Claude Code expands variables in `.mcp.json` but not in `settings.json`, so a
71
+ placeholder path leaves the hook silently dead. On Windows the same applies to Git
72
+ Bash paths — `install.sh` converts them with `cygpath`, so run it from the shell you
73
+ intend to use and merge what it generates rather than hand-writing the paths.
74
+
75
+ When merging, **append** to any `SessionStart` array you already have instead of
76
+ replacing it, and re-run `install.sh` after installing the `mcp` SDK — the
77
+ `mcpServers` block is only emitted when the SDK is importable at install time.
78
+
79
+ ## Verify
80
+
81
+ ```bash
82
+ python test_brainmem.py # 44 invariants — gating, supersession, budget, utility
83
+ bash smoke_test.sh # 29 checks — install, CLI, both hooks, cross-process persistence
84
+ python e2e_mcp.py # 22 checks — spawns the real MCP server over stdio
85
+ python demo.py # full lifecycle, no API key needed
86
+ ruff check .
87
+ ```
88
+
89
+ All four pass on a clean checkout, on Linux, macOS and Windows across Python
90
+ 3.10–3.13. `test_brainmem.py` also runs under `pytest`. `e2e_mcp.py` needs the
91
+ `mcp` extra; the rest need only numpy. See [CONTRIBUTING.md](CONTRIBUTING.md) for
92
+ why the shell and MCP suites exist separately from the unit tests.
93
+
94
+ ## Where this sits
95
+
96
+ Agent memory is a crowded field and most of it is bigger than this. brainmem is
97
+ deliberately small: one readable Python module, numpy as the only required
98
+ dependency, SQLite on disk, no service to run.
99
+
100
+ It is built around three claims that are unusual rather than better:
101
+
102
+ 1. **Attention is the binding constraint, not storage.** Retrieval is fitted to a
103
+ token budget in priority order, and the assembled block is ordered in a
104
+ serial-position V because the middle of a context window is where things go to
105
+ be ignored. Writing is gated on surprise, so the store grows on novelty rather
106
+ than volume.
107
+ 2. **Beliefs have a validity interval, and nothing is deleted.** A contradiction
108
+ closes the old belief off and links the successor. `retrieve(..., at=t)` answers
109
+ *what did the agent believe last Tuesday, and on what evidence* — which is the
110
+ question you need when an agent did something surprising.
111
+ 3. **Failures are a separate valence, and they lead.** They distil under their own
112
+ prompt, rank separately, and are fitted to the budget *before* facts, so under
113
+ pressure they are the last thing dropped. Ma et al. (2026) ablated this:
114
+ removing failure reasons cost 8 points, removing success patterns cost 2. Most
115
+ stores keep only what worked.
116
+
117
+ **Use something else if:** you want a managed service, multi-tenant user profiles,
118
+ or a knowledge graph over a large corpus. [mem0](https://github.com/mem0ai/mem0),
119
+ [cognee](https://github.com/topoteretes/cognee), [Letta](https://github.com/letta-ai/letta)
120
+ and [Zep](https://github.com/getzep/zep) are all larger, more featureful, and more
121
+ production-hardened than this is. [HippoRAG](https://github.com/OSU-NLP-Group/HippoRAG)
122
+ is the research-grade take on memory-inspired retrieval.
123
+
124
+ **Use this if** you want something you can read end to end in an afternoon, audit
125
+ the provenance of every belief, and wire into Claude Code in one command.
126
+
127
+ ## Layers
128
+
129
+ | Layer | Role | Key property |
130
+ |---|---|---|
131
+ | L0 working | assembled context | token-budgeted, never persisted |
132
+ | L1 episodic | append-only event log | immutable, carries outcome |
133
+ | L2 semantic | distilled propositions | validity intervals, provenance, valence |
134
+ | L3 procedural | cached action sequences | scored by success rate |
135
+ | core | pinned identity | always loaded |
136
+
137
+ ```mermaid
138
+ flowchart LR
139
+ E["<b>L1 episodic</b><br/>append-only, immutable<br/>carries outcome"]
140
+ E -->|"consolidate()<br/><i>the sleep pass</i>"| SPLIT{"outcome"}
141
+
142
+ SPLIT -->|"failed"| F["<b>valence = failure</b><br/>'Avoid: X fails when Y'"]
143
+ SPLIT -->|"ok / unknown"| FACT["<b>valence = fact</b>"]
144
+
145
+ F --> RANK["<b>retrieve()</b><br/>utility = 0.7·confidence + 0.3·usage<br/>+ MMR diversity"]
146
+ FACT --> RANK
147
+
148
+ RANK --> CTX["<b>context()</b><br/>token-budgeted<br/>failures fitted first"]
149
+ OUT["record_outcome()"] -.->|"the only thing that<br/>moves confidence"| RANK
150
+ RANK -.->|"decay() · prune_guidelines()"| GONE["retired<br/><i>still queryable with at=t</i>"]
151
+
152
+ classDef fail fill:#ffeef0,stroke:#d1495b
153
+ class F fail
154
+ ```
155
+
156
+ Processes: `encode()` gates on novelty, `consolidate()` distils offline, `retrieve()` ranks and diversifies, `record_outcome()` closes the feedback loop, `decay()`/`prune_guidelines()` forget.
157
+
158
+ ## Two ways memory reaches the agent
159
+
160
+ ```mermaid
161
+ flowchart TD
162
+ DB[("SQLite store<br/>episodes · facts · skills")]
163
+
164
+ DB -->|"SessionStart hook<br/>~600 tokens, before turn 1"| BLOCK["<b>Context block</b><br/>failures → facts → recent events"]
165
+ BLOCK --> AGENT(["Agent"])
166
+
167
+ AGENT <-->|"memory_search · memory_write<br/><i>at inference time, goal known</i>"| MCP["MCP tools"]
168
+ MCP <--> DB
169
+
170
+ AGENT -->|"memory_outcome<br/><i>did acting on it work?</i>"| MCP
171
+ AGENT -.->|"SessionEnd: consolidate · prune · decay"| DB
172
+
173
+ classDef floor fill:#eef4ff,stroke:#5b7cfa
174
+ classDef ceil fill:#eefaf0,stroke:#3fa96a
175
+ class BLOCK floor
176
+ class MCP ceil
177
+ ```
178
+
179
+ **SessionStart hook** — injects ~600 tokens before the first turn. A floor, not the whole store. It pre-commits against an unknown goal, so it stays small deliberately.
180
+
181
+ **MCP tools** — `memory_search`, `memory_write`, `memory_outcome`, `memory_explain`, `memory_status`. Defers retrieval to inference time, when the agent knows what it's doing. This is the ceiling.
182
+
183
+ `SessionEnd` runs `maintain`: consolidate, prune, decay. That's the sleep pass — LLM-expensive and batched, because finding the invariant across events needs several events at once.
184
+
185
+ ## The write gate
186
+
187
+ The single highest-leverage decision is what *not* to store. Encoding is driven by
188
+ prediction error: if the store already predicts the observation, strengthen instead
189
+ of duplicating.
190
+
191
+ ```mermaid
192
+ flowchart TD
193
+ OBS["New observation"] --> GATE{"Compare against nearest facts<br/>+ unconsolidated episodes"}
194
+
195
+ GATE -->|novel| STORE["Store episode"]
196
+ GATE -->|refinement| STORE
197
+ GATE -->|contradiction| SUP["Store, and close off the old belief<br/><i>valid_to set, superseded_by linked</i>"]
198
+ GATE -->|redundant| CONF{"Outcome differs from<br/>the thing it resembles?"}
199
+
200
+ CONF -->|no| STRONG["Strengthen support only<br/><i>no new row, no confidence change</i>"]
201
+ CONF -->|"yes — did this before,<br/>got a different result"| STORE
202
+
203
+ classDef keep fill:#eefaf0,stroke:#3fa96a
204
+ classDef drop fill:#fff4e6,stroke:#e8973a
205
+ class STORE,SUP keep
206
+ class STRONG drop
207
+ ```
208
+
209
+ Nothing is deleted on contradiction. The old belief keeps its validity interval and
210
+ its provenance, which is what makes `retrieve(..., at=t)` able to answer *what did
211
+ the agent believe last Tuesday*.
212
+
213
+ ## The outcome channel
214
+
215
+ `encode(..., outcome=True|False|None)` records whether acting on something worked. Three consequences:
216
+
217
+ 1. **Failures distil separately** into `valence='failure'` lessons, and lead the assembled context. Ma et al. (2026) ablated their semantic memory: removing failure reasons cost 8 points, removing success patterns cost 2. Most memory systems store only successes.
218
+ 2. **Outcome conflict defeats redundancy.** Identical text with a flipped outcome is stored, not collapsed — "I did this before and got a different result" is the strongest prediction error available.
219
+ 3. **Utility ranks beliefs.** `0.7·confidence + 0.3·usage` after `record_outcome()`. Without it, ranking rewards beliefs that look relevant over beliefs that have been right.
220
+
221
+ **The catch.** In a simulator the oracle is free. In advisory, analytical, or consulting work there is no oracle — nothing emits `success=True`. You must supply outcomes: a human verdict, a downstream check, a test result. Everything degrades gracefully to `outcome=None`, but the mechanisms carrying most of the measured gain are exactly the ones that need the signal. Wiring `memory_outcome` into a real workflow is the difference between this being useful and being decoration.
222
+
223
+ ## Defaults worth knowing
224
+
225
+ - `retrieve(k=3)` — retrieval quality saturates fast (74% at k=1, 82% at k=2, flat at k=3 and k=5 in Ma et al.). Raise only with evidence.
226
+ - `context()` orders each block in a serial-position V — best at head and tail, weakest in the middle (Liu et al., 2023, "Lost in the Middle").
227
+ - Failures are fitted to the token budget *before* facts, so under pressure they're the last thing dropped.
228
+ - `prune_guidelines(keep=20)` caps outcome-scored rules; anything ≥0.8 confidence with ≥5 successes is protected.
229
+
230
+ ## Production swaps, in order of impact
231
+
232
+ 1. **Real embedder** — shipped. `pip install 'brainmem[embeddings]'` and set
233
+ `BRAINMEM_EMBEDDER=sentence-transformers`. The `HashEmbedder` default has no
234
+ semantic generalisation (it is hashed n-grams), so "the batch aborted" and "the
235
+ job failed" share no vector mass. Switching changes the vector dimension — start
236
+ a fresh store, because the old vectors are not comparable to the new ones.
237
+ 2. **`BRAINMEM_LLM=anthropic`** for the write gate and extractor. The heuristic judge cannot detect contradiction reliably; a cosine threshold structurally can't, since "X leads the project" and "X has left the project" embed almost identically.
238
+ 3. **pgvector or FAISS** above ~100k rows. Only `_nearest_facts` changes.
239
+
240
+ ## Found by testing, not by reading
241
+
242
+ Bugs the unit tests could not have caught, listed because they show what the
243
+ suites are for:
244
+
245
+ - **MCP SDK 2.0 removed `mcp.server.fastmcp`.** The server imported a module that
246
+ no longer exists. `brainmem_mcp.py` now tries `MCPServer` and falls back to
247
+ `FastMCP`, so it runs on both.
248
+ - **`memory_write` misreported outcome conflicts.** It said "already known" based
249
+ on the gate's verdict, while the episode had in fact been stored. The data was
250
+ right and the tool was lying to the agent about it.
251
+ - **The token budget only governed facts.** Recent events and skills were
252
+ unbounded, so `token_budget` was closer to a suggestion. Everything is fitted
253
+ now, in priority order: failures, facts, procedures, raw events.
254
+ - **`$HOME` in `settings.json` is never expanded.** The original config would have
255
+ installed cleanly and then never fired. Hence `install.sh`.
256
+ - **`install.sh` and `smoke_test.sh` copied from an `integration/` directory that
257
+ does not exist.** Both die on that line under `set -e`, so neither the installer
258
+ nor the shell suite could run at all. Nothing that only imports the library
259
+ notices.
260
+ - **The same dead-hook failure has a Windows form.** Under Git Bash the generated
261
+ paths were `/c/Users/...`, which the process Claude Code spawns cannot resolve,
262
+ and the hooks relied on the executable bit rather than an explicit interpreter.
263
+ Installed cleanly, never fired. Paths now go through `cygpath` and hooks are
264
+ invoked as `bash "<script>"`.
265
+ - **The SessionStart hook read its goal from `$1`.** Claude Code delivers hook
266
+ payloads as JSON on stdin and never passes argv, so in a live session the goal
267
+ was always empty and silently fell back to the working directory — the block
268
+ still rendered, it just stopped being goal-conditioned. Every test passed the
269
+ goal as `$1`, which is exactly why none of them saw it.
270
+ - **The embedding hash was salted per process.** `HashEmbedder` used builtin
271
+ `hash()`, which Python salts per process (PEP 456). Every deployment path here —
272
+ the SessionStart hook, the CLI, the MCP server — is a separate process over one
273
+ database, so vectors written by one session were meaningless to the next. The
274
+ store still returned rows in the right shape, so nothing looked broken while
275
+ ranking was quietly random; the same query that ranked a failure lesson first
276
+ in-process ranked it fourth from a fresh process. Now blake2b.
277
+ - **Confidence rose on restatement, not evidence.** Both the `encode` redundancy
278
+ path and the `_distil` "reinforced" path did `confidence + 0.05`. But the gate
279
+ decides redundancy by entity overlap and token similarity — it measures how alike
280
+ two *strings* are, not whether two *independent sources* agree, and it cannot tell
281
+ a paraphrase from a caveat. Recording "the thirty percent rule is not a
282
+ demonstrated optimum" made the store **more** certain of the thirty percent rule.
283
+ Restatement now moves `support` only; `confidence` has exactly one mutator,
284
+ `record_outcome`.
285
+ - **One `--success` pinned confidence to 1.00.** `conf = n_success/n_total`
286
+ overwrote the distilled prior, so a belief the store itself labelled `unverified,
287
+ n=1` jumped to maximum confidence and 1/1 ranked identically to 9/9 — breaking the
288
+ very rule the docstring states. Now Laplace-smoothed: 0.67, 0.91.
289
+ - **Distillation split clauses into claims.** `_extract` kept any 12–240 char
290
+ fragment, so "…not yet load-tested at that volume" became a standalone fact whose
291
+ antecedent stayed behind in L1. A fragment is indistinguishable from a fact once
292
+ embedded: it ranks, it fits the budget, it gets injected. Fragments that cannot be
293
+ read alone are now dropped — the raw episode still holds them.
294
+ - **`memory_write` silently coerced unrecognised outcomes.** `outcome` was a bare
295
+ `str` looked up in a dict, so a natural near-miss like `"failure"` became
296
+ `unknown`: the failure signal was discarded, and with no outcome left to
297
+ conflict, the observation was swallowed as redundant and reported as "already
298
+ known". It is a `Literal` now, enumerated in the tool schema and rejected at the
299
+ boundary. Losing the store's most valuable signal quietly is worse than failing
300
+ loudly.
301
+
302
+ ## What remains unproven
303
+
304
+ Surprisal-gated writes are principled but I know of no clean benchmark showing they beat write-everything at scale. Ma et al. don't test it either — their episodic store grows monotonically. If you find or run such a benchmark, it's the result most likely to change this design.