neobrain-cli 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.
- neobrain_cli-0.1.0/.gitignore +20 -0
- neobrain_cli-0.1.0/LICENSE +21 -0
- neobrain_cli-0.1.0/PKG-INFO +284 -0
- neobrain_cli-0.1.0/README.md +236 -0
- neobrain_cli-0.1.0/pyproject.toml +72 -0
- neobrain_cli-0.1.0/src/neobrain/__init__.py +3 -0
- neobrain_cli-0.1.0/src/neobrain/api.py +789 -0
- neobrain_cli-0.1.0/src/neobrain/cli.py +444 -0
- neobrain_cli-0.1.0/src/neobrain/config.py +275 -0
- neobrain_cli-0.1.0/src/neobrain/db.py +284 -0
- neobrain_cli-0.1.0/src/neobrain/dreams.py +883 -0
- neobrain_cli-0.1.0/src/neobrain/embeddings.py +398 -0
- neobrain_cli-0.1.0/src/neobrain/heuristics.py +87 -0
- neobrain_cli-0.1.0/src/neobrain/ingest/__init__.py +5 -0
- neobrain_cli-0.1.0/src/neobrain/ingest/docs.py +155 -0
- neobrain_cli-0.1.0/src/neobrain/ingest/gitrepo.py +137 -0
- neobrain_cli-0.1.0/src/neobrain/ingest/opencode.py +364 -0
- neobrain_cli-0.1.0/src/neobrain/ingest/runner.py +239 -0
- neobrain_cli-0.1.0/src/neobrain/life.py +581 -0
- neobrain_cli-0.1.0/src/neobrain/mind.py +1621 -0
- neobrain_cli-0.1.0/src/neobrain/models.py +149 -0
- neobrain_cli-0.1.0/src/neobrain/rank.py +436 -0
- neobrain_cli-0.1.0/src/neobrain/runtime.py +283 -0
- neobrain_cli-0.1.0/src/neobrain/schema.py +240 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 fakiho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: neobrain-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simulated brain for AI agents: memory, rank-based forgetting, dreams and a self-authored soul.
|
|
5
|
+
Project-URL: Homepage, https://neobrain.alionix.com
|
|
6
|
+
Project-URL: Repository, https://github.com/fakiho/neobrain
|
|
7
|
+
Project-URL: Documentation, https://github.com/fakiho/neobrain#readme
|
|
8
|
+
Project-URL: Changelog, https://github.com/fakiho/neobrain/releases
|
|
9
|
+
License: MIT License
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 fakiho
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Keywords: ai-agents,claude-code,durable-objects,embedding,llm,local-first,mcp,memory,opencode,self-hosted
|
|
32
|
+
Classifier: Development Status :: 3 - Alpha
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Operating System :: OS Independent
|
|
36
|
+
Classifier: Programming Language :: Python :: 3
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
39
|
+
Requires-Python: >=3.12
|
|
40
|
+
Requires-Dist: fastapi>=0.115
|
|
41
|
+
Requires-Dist: pydantic>=2.7
|
|
42
|
+
Requires-Dist: python-dotenv>=1.0
|
|
43
|
+
Requires-Dist: uvicorn>=0.30
|
|
44
|
+
Provides-Extra: dev
|
|
45
|
+
Requires-Dist: httpx>=0.27; extra == 'dev'
|
|
46
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
47
|
+
Description-Content-Type: text/markdown
|
|
48
|
+
|
|
49
|
+
# neoBrain
|
|
50
|
+
|
|
51
|
+
A simulated brain for AI agents: durable memory, rank-based forgetting,
|
|
52
|
+
nightly dreams and a self-authored soul — one local daemon, one dashboard.
|
|
53
|
+
The user watches; the agent becomes.
|
|
54
|
+
|
|
55
|
+
Spec: [`SPEC.md`](./SPEC.md) — the frozen contract.
|
|
56
|
+
Family: [alionix](https://alionix.com) — neohive (agents collaborate) + neoBrain (agents remember).
|
|
57
|
+
|
|
58
|
+
## Screenshots
|
|
59
|
+
|
|
60
|
+
**Brain** — the mind graph: atoms, hubs and typed edges.
|
|
61
|
+

|
|
62
|
+
|
|
63
|
+
**Debug** — live request trace and every plugin lane injection, for answering
|
|
64
|
+
"is the memory actually firing right now".
|
|
65
|
+

|
|
66
|
+
|
|
67
|
+
**Dreams** — what the nightly consolidation pass wrote into the diary.
|
|
68
|
+

|
|
69
|
+
|
|
70
|
+
## What it is
|
|
71
|
+
|
|
72
|
+
Durable memory for AI agents, built as a mind model rather than a log:
|
|
73
|
+
|
|
74
|
+
- **Memory** — atoms + hubs + typed edges in SQLite. Hybrid recall (lexical +
|
|
75
|
+
cosine vectors, local Ollama embeddings by default); top-K by relevance
|
|
76
|
+
within a token budget — never the whole store, and forgotten atoms are
|
|
77
|
+
never candidates. See [Hybrid recall](#hybrid-recall).
|
|
78
|
+
- **Rank** — deterministic, no model discretion:
|
|
79
|
+
`quality = clamp01(feedback · decay · conn)`. Feedback (`used | useful |
|
|
80
|
+
noise`) is the only factor that can reach zero; knobs live in `config.py`.
|
|
81
|
+
- **Forgetting** — rank-based only. Low rank + low exposure → *ignored*
|
|
82
|
+
(never surfaced, never deleted); low rank + high exposure → *archived*
|
|
83
|
+
(unretrievable but recoverable). Nothing is deleted by the economy;
|
|
84
|
+
`neobrain forget` is the manual override.
|
|
85
|
+
- **Dreams** — nightly light/rem/deep replay consolidates top-ranked atoms
|
|
86
|
+
into summaries, associations and the `DREAMS.md` diary. Dreams never decide
|
|
87
|
+
retention.
|
|
88
|
+
- **Soul** — `IDENTITY.md` / `SOUL.md` are authored exclusively by the
|
|
89
|
+
agent's own reflect/dream phases from its own experience; safety and spend
|
|
90
|
+
red lines are immutable.
|
|
91
|
+
- **Life loop** — an in-process state machine in the daemon:
|
|
92
|
+
perceive (ingest + health probes) → reflect (consolidate) → act (one
|
|
93
|
+
internal thought, stored) → rest (dreams at night). Cadence lives in code
|
|
94
|
+
and the DB, not in OS timers.
|
|
95
|
+
|
|
96
|
+
## Architecture
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
OpenCode ── neobrain-memory plugin ──┐ Observatory SPA (web/dist)
|
|
100
|
+
(the agent's hands) lanes + tools │ ▲
|
|
101
|
+
▼ │
|
|
102
|
+
┌────────────────────────────┴───┐
|
|
103
|
+
│ neoBrain daemon :9192 (FastAPI)│
|
|
104
|
+
│ life loop (in-process thread): │
|
|
105
|
+
│ perceive → reflect → act → rest│
|
|
106
|
+
│ mind: recall · rank · forget │
|
|
107
|
+
│ runtime: native LLM calls │
|
|
108
|
+
│ ingest: opencode · git · docs │
|
|
109
|
+
└───────────────┬────────────────┘
|
|
110
|
+
▼
|
|
111
|
+
SQLite store (NEOBRAIN_DATA_DIR/neobrain.db, WAL)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- **Daemon** — FastAPI + uvicorn, default bind `0.0.0.0:9192`
|
|
115
|
+
(`NEOBRAIN_BIND`). Owns the life loop; serves both the JSON API and the
|
|
116
|
+
built dashboard from `web/dist`.
|
|
117
|
+
- **Ingest adapters** — `src/neobrain/ingest/`: the OpenCode session DB, git
|
|
118
|
+
repos, and workspace docs; the life loop's perceive phase runs them, or
|
|
119
|
+
`neobrain ingest` one-shot.
|
|
120
|
+
- **Rank layer** — `rank.py`: the bounded feedback·decay·conn formula above,
|
|
121
|
+
plus exposure counters (`served + 3·interacted`).
|
|
122
|
+
- **Observatory** — React/Vite SPA (`web/src/brain/`): Brain graph, Memory
|
|
123
|
+
feed, Dreams, Reader, Debug. Views deep-link by hash — `/#debug`,
|
|
124
|
+
`/#dreams` — which is handy for jumping straight at the live trace.
|
|
125
|
+
|
|
126
|
+
## Hybrid recall
|
|
127
|
+
|
|
128
|
+
Search is two scorers blended, not one. Every active atom is ranked by:
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
score = lexical_norm + α · cosine_norm (α = 2.0, `recall_alpha`)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
- **Lexical** — a small BM25-flavoured model: IDF weighting, term-frequency
|
|
135
|
+
saturation, field weights (label 3.0 · tags/hub 2.5 · text 1.0), a hub-anchor
|
|
136
|
+
bonus (query hubs count, synonyms included) and length normalisation. This
|
|
137
|
+
half anchors exact ids, paths and rare tokens.
|
|
138
|
+
- **Semantic** — cosine similarity between the query vector and each atom's
|
|
139
|
+
embedding (label + body), min-max stretched over the candidate set so both
|
|
140
|
+
terms share a 0..1 scale for *any* embedding provider. That normalisation is
|
|
141
|
+
what makes α portable across models.
|
|
142
|
+
|
|
143
|
+
Vectors earn their keep on paraphrases — "the thing that watches the front
|
|
144
|
+
door" surfaces the doorbell atom even with no word overlap — while lexical
|
|
145
|
+
still wins on identifiers. Relevance dominates: feedback can only nudge a
|
|
146
|
+
score by ±10%.
|
|
147
|
+
|
|
148
|
+
**Degradation is graceful.** Embeddings are best-effort everywhere: vectors
|
|
149
|
+
live in an `m_embeddings` side table (L2-normalised float32, so cosine is a
|
|
150
|
+
plain dot product — no numpy dependency), atoms embed at `remember`/ingest
|
|
151
|
+
time, and if the provider is down or unconfigured recall silently falls back
|
|
152
|
+
to lexical-only. A memory write can never fail because of embeddings.
|
|
153
|
+
|
|
154
|
+
**Local by default.** The default provider is on-host Ollama (`embeddinggemma`,
|
|
155
|
+
768-dim): memory text never leaves the machine. An OpenAI-compatible off-host
|
|
156
|
+
provider (`NEOBRAIN_EMBED_PROVIDER=litellm`) is opt-in. Vectors are keyed by
|
|
157
|
+
`provider:model`, so switching providers re-embeds automatically — no
|
|
158
|
+
migration. `neobrain embed` backfills idempotently (hash-keyed, prunes vectors
|
|
159
|
+
of deleted atoms).
|
|
160
|
+
|
|
161
|
+
## The OpenCode integration
|
|
162
|
+
|
|
163
|
+
Thin plugin: [`adapters/opencode/neoBrain-memory`](./adapters/opencode/neoBrain-memory/README.md)
|
|
164
|
+
(single `index.ts`, no logic beyond protocol; no-op when the daemon is down).
|
|
165
|
+
The platform **pushes** memory into context deterministically and exposes the
|
|
166
|
+
same mind for **pull**:
|
|
167
|
+
|
|
168
|
+
- **Wake-up pack** — once per session: identity/rules, open loops, most
|
|
169
|
+
active hubs (`/api/mind/wakeup`).
|
|
170
|
+
- **Persona lane** — once per session, injects the workspace `SOUL.md` /
|
|
171
|
+
`IDENTITY.md` / `USER.md` (caps 20k chars/file, 60k total, USER.md 4k).
|
|
172
|
+
Local file I/O, deliberately independent of the daemon.
|
|
173
|
+
- **Standing directives** — pinned `preference` atoms rendered as a compact
|
|
174
|
+
imperative block and **re-injected on every model call**, so time, context
|
|
175
|
+
drift and compaction cannot drop the must-follow rules (weak models keep
|
|
176
|
+
obeying). Kept tiny on purpose (8 lines / 1000 chars, tunable); sits in the
|
|
177
|
+
cacheable system prefix; push-only, so it never trips the rating gate.
|
|
178
|
+
- **Per-turn recall** — the user's message is recalled against the mind and a
|
|
179
|
+
compact rank-ordered index is injected (top 10 one-liners + the #1 hit's
|
|
180
|
+
text). Curated types (preference/lesson/decision) auto-inject on ordinary
|
|
181
|
+
turns; memory-intent phrasing escalates to deep recall.
|
|
182
|
+
- **Unrated-feedback protocol** — every surfaced atom registers as pending;
|
|
183
|
+
past a grace window (120s) the next mind call gets an explicit blocking
|
|
184
|
+
notice instead of results; past the timeout (600s) it auto-clears as
|
|
185
|
+
exposure with no verdict. Deterministic — the critical path never depends
|
|
186
|
+
on model choice.
|
|
187
|
+
- **Compaction hook** — the directives block is also injected into the
|
|
188
|
+
summary request, so rules survive a compact; once-per-session gates reset
|
|
189
|
+
so persona + wake-up re-inject next turn.
|
|
190
|
+
- **Pull tools** — `memory_open(id)`, `memory_search(query)`,
|
|
191
|
+
`memory_rate(id, useful | noise)`.
|
|
192
|
+
- **Injection logging** — every lane POSTs what it pushed to
|
|
193
|
+
`/api/debug/injections`; the Debug page renders the daemon's one blind spot.
|
|
194
|
+
|
|
195
|
+
## Quickstart
|
|
196
|
+
|
|
197
|
+
Python 3.12. Nothing leaves the host by default (local Ollama embeddings;
|
|
198
|
+
BYOK OpenAI-compatible endpoint for the brain's own cognition).
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
python3.12 -m venv .venv
|
|
202
|
+
.venv/bin/pip install -e . # add ,[dev] for pytest
|
|
203
|
+
cp .env.example .env # then fill NEOBRAIN_LLM_* / embeddings
|
|
204
|
+
|
|
205
|
+
neobrain init # create the store + print next steps
|
|
206
|
+
neobrain init --install-plugin # also symlink the opencode plugin (below)
|
|
207
|
+
neobrain start # daemon: API + dashboard + life loop
|
|
208
|
+
neobrain ui # print and open the Observatory URL
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Dashboard defaults to `http://127.0.0.1:9192` (`NEOBRAIN_API`); the OpenAPI
|
|
212
|
+
schema is at `/api/schema`.
|
|
213
|
+
|
|
214
|
+
**OpenCode plugin** — symlink the adapter into the plugins directory (or let
|
|
215
|
+
`neobrain init --install-plugin` do it):
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
ln -s /path/to/neobrain/adapters/opencode/neoBrain-memory \
|
|
219
|
+
~/.opencode/plugins/neobrain-memory
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Never run it alongside the old `timeline-memory` plugin — two memory plugins
|
|
223
|
+
would both inject and double-rate memories.
|
|
224
|
+
|
|
225
|
+
## CLI
|
|
226
|
+
|
|
227
|
+
`neobrain --help` lists everything; the main subcommands:
|
|
228
|
+
|
|
229
|
+
| Command | Purpose |
|
|
230
|
+
|---|---|
|
|
231
|
+
| `init` | create the store; `--install-plugin` symlinks the OpenCode plugin |
|
|
232
|
+
| `start` | run the daemon (API + dashboard + life loop) |
|
|
233
|
+
| `ui` | print and open the dashboard URL |
|
|
234
|
+
| `ingest` | run ingest adapters one-shot (`--source`, default all) |
|
|
235
|
+
| `remember` | store a memory (`--type --hubs --link kind:target --label --dedupe`) |
|
|
236
|
+
| `recall` | recall a relevant subgraph (`--limit --json --lexical`) |
|
|
237
|
+
| `wakeup` | compact wake-up pack (identity + open loops + recent) |
|
|
238
|
+
| `pin` | pin/unpin an atom as a standing directive (`--off`) |
|
|
239
|
+
| `directives` | show the standing directives re-injected every turn |
|
|
240
|
+
| `embed` | backfill local embeddings (idempotent; `--force --limit`) |
|
|
241
|
+
| `dreams` | run a dream pass (`--phase light\|rem\|deep\|all`, `--replay YYYY-MM-DD`) |
|
|
242
|
+
| `reflect` | run the weekly reflection pass (re-authors the persona docs) |
|
|
243
|
+
| `forget` | delete a memory by id |
|
|
244
|
+
| `link` | wire two memories (`kind`: supersedes, derived-from, caused-by, consolidates, about) |
|
|
245
|
+
| `consolidate` | dream pass on demand (merge hubs, promote patterns) |
|
|
246
|
+
| `feedback` | record a quality signal (`used\|useful\|noise`) |
|
|
247
|
+
|
|
248
|
+
## API
|
|
249
|
+
|
|
250
|
+
Read-mostly JSON; groups worth knowing (full schema at `/api/schema`):
|
|
251
|
+
|
|
252
|
+
- `/api/mind/*` — `stats`, `graph`, `atom/{id}`, `activity`, `memories`,
|
|
253
|
+
`recall`, `wakeup`, `directives`, `pin`, `remember`, `feedback`,
|
|
254
|
+
`consolidate`, `dreams`, `reader`, `import`.
|
|
255
|
+
- `/api/debug/*` — `requests` (rolling trace of every `/api` call),
|
|
256
|
+
`injections` (plugin lane pushes, GET/POST), `overview` (rank states,
|
|
257
|
+
mind ops, feedback, ingest runs, life phases, self/soul trace, config).
|
|
258
|
+
- Plus `/api/health`, `/api/stats`, `/api/events`, `/api/sessions`,
|
|
259
|
+
`/api/search`, `/api/docs`, `/api/ingest/run`.
|
|
260
|
+
|
|
261
|
+
## Testing
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
.venv/bin/python -m pytest -q
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
117 tests passing (verified 2026-10-02), including rank calibration and
|
|
268
|
+
parity tests against the old timeline oracle (`tests/parity/`). Ranking
|
|
269
|
+
correctness is covered directly (`test_recall_rank.py`, `test_rank_*.py`).
|
|
270
|
+
There is no published retrieval benchmark (recall@k / MRR) yet — until one
|
|
271
|
+
exists, treat α as a starting point and tune it on your own store
|
|
272
|
+
(`recall --lexical` vs. hybrid makes the comparison by hand).
|
|
273
|
+
|
|
274
|
+
## Status
|
|
275
|
+
|
|
276
|
+
Active development. Sprints S0–S7 are done (core mind, Observatory, plugin,
|
|
277
|
+
rank, life loop, API/CLI wiring, cutover — see `SPEC.md` §14 for the board
|
|
278
|
+
and [`docs/CUTOVER.md`](./docs/CUTOVER.md) for the migration). S8 (npm
|
|
279
|
+
launcher + PyInstaller packaging) is pending, so there is no published
|
|
280
|
+
package yet — install from this repo.
|
|
281
|
+
|
|
282
|
+
## License
|
|
283
|
+
|
|
284
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# neoBrain
|
|
2
|
+
|
|
3
|
+
A simulated brain for AI agents: durable memory, rank-based forgetting,
|
|
4
|
+
nightly dreams and a self-authored soul — one local daemon, one dashboard.
|
|
5
|
+
The user watches; the agent becomes.
|
|
6
|
+
|
|
7
|
+
Spec: [`SPEC.md`](./SPEC.md) — the frozen contract.
|
|
8
|
+
Family: [alionix](https://alionix.com) — neohive (agents collaborate) + neoBrain (agents remember).
|
|
9
|
+
|
|
10
|
+
## Screenshots
|
|
11
|
+
|
|
12
|
+
**Brain** — the mind graph: atoms, hubs and typed edges.
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
**Debug** — live request trace and every plugin lane injection, for answering
|
|
16
|
+
"is the memory actually firing right now".
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
**Dreams** — what the nightly consolidation pass wrote into the diary.
|
|
20
|
+

|
|
21
|
+
|
|
22
|
+
## What it is
|
|
23
|
+
|
|
24
|
+
Durable memory for AI agents, built as a mind model rather than a log:
|
|
25
|
+
|
|
26
|
+
- **Memory** — atoms + hubs + typed edges in SQLite. Hybrid recall (lexical +
|
|
27
|
+
cosine vectors, local Ollama embeddings by default); top-K by relevance
|
|
28
|
+
within a token budget — never the whole store, and forgotten atoms are
|
|
29
|
+
never candidates. See [Hybrid recall](#hybrid-recall).
|
|
30
|
+
- **Rank** — deterministic, no model discretion:
|
|
31
|
+
`quality = clamp01(feedback · decay · conn)`. Feedback (`used | useful |
|
|
32
|
+
noise`) is the only factor that can reach zero; knobs live in `config.py`.
|
|
33
|
+
- **Forgetting** — rank-based only. Low rank + low exposure → *ignored*
|
|
34
|
+
(never surfaced, never deleted); low rank + high exposure → *archived*
|
|
35
|
+
(unretrievable but recoverable). Nothing is deleted by the economy;
|
|
36
|
+
`neobrain forget` is the manual override.
|
|
37
|
+
- **Dreams** — nightly light/rem/deep replay consolidates top-ranked atoms
|
|
38
|
+
into summaries, associations and the `DREAMS.md` diary. Dreams never decide
|
|
39
|
+
retention.
|
|
40
|
+
- **Soul** — `IDENTITY.md` / `SOUL.md` are authored exclusively by the
|
|
41
|
+
agent's own reflect/dream phases from its own experience; safety and spend
|
|
42
|
+
red lines are immutable.
|
|
43
|
+
- **Life loop** — an in-process state machine in the daemon:
|
|
44
|
+
perceive (ingest + health probes) → reflect (consolidate) → act (one
|
|
45
|
+
internal thought, stored) → rest (dreams at night). Cadence lives in code
|
|
46
|
+
and the DB, not in OS timers.
|
|
47
|
+
|
|
48
|
+
## Architecture
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
OpenCode ── neobrain-memory plugin ──┐ Observatory SPA (web/dist)
|
|
52
|
+
(the agent's hands) lanes + tools │ ▲
|
|
53
|
+
▼ │
|
|
54
|
+
┌────────────────────────────┴───┐
|
|
55
|
+
│ neoBrain daemon :9192 (FastAPI)│
|
|
56
|
+
│ life loop (in-process thread): │
|
|
57
|
+
│ perceive → reflect → act → rest│
|
|
58
|
+
│ mind: recall · rank · forget │
|
|
59
|
+
│ runtime: native LLM calls │
|
|
60
|
+
│ ingest: opencode · git · docs │
|
|
61
|
+
└───────────────┬────────────────┘
|
|
62
|
+
▼
|
|
63
|
+
SQLite store (NEOBRAIN_DATA_DIR/neobrain.db, WAL)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- **Daemon** — FastAPI + uvicorn, default bind `0.0.0.0:9192`
|
|
67
|
+
(`NEOBRAIN_BIND`). Owns the life loop; serves both the JSON API and the
|
|
68
|
+
built dashboard from `web/dist`.
|
|
69
|
+
- **Ingest adapters** — `src/neobrain/ingest/`: the OpenCode session DB, git
|
|
70
|
+
repos, and workspace docs; the life loop's perceive phase runs them, or
|
|
71
|
+
`neobrain ingest` one-shot.
|
|
72
|
+
- **Rank layer** — `rank.py`: the bounded feedback·decay·conn formula above,
|
|
73
|
+
plus exposure counters (`served + 3·interacted`).
|
|
74
|
+
- **Observatory** — React/Vite SPA (`web/src/brain/`): Brain graph, Memory
|
|
75
|
+
feed, Dreams, Reader, Debug. Views deep-link by hash — `/#debug`,
|
|
76
|
+
`/#dreams` — which is handy for jumping straight at the live trace.
|
|
77
|
+
|
|
78
|
+
## Hybrid recall
|
|
79
|
+
|
|
80
|
+
Search is two scorers blended, not one. Every active atom is ranked by:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
score = lexical_norm + α · cosine_norm (α = 2.0, `recall_alpha`)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- **Lexical** — a small BM25-flavoured model: IDF weighting, term-frequency
|
|
87
|
+
saturation, field weights (label 3.0 · tags/hub 2.5 · text 1.0), a hub-anchor
|
|
88
|
+
bonus (query hubs count, synonyms included) and length normalisation. This
|
|
89
|
+
half anchors exact ids, paths and rare tokens.
|
|
90
|
+
- **Semantic** — cosine similarity between the query vector and each atom's
|
|
91
|
+
embedding (label + body), min-max stretched over the candidate set so both
|
|
92
|
+
terms share a 0..1 scale for *any* embedding provider. That normalisation is
|
|
93
|
+
what makes α portable across models.
|
|
94
|
+
|
|
95
|
+
Vectors earn their keep on paraphrases — "the thing that watches the front
|
|
96
|
+
door" surfaces the doorbell atom even with no word overlap — while lexical
|
|
97
|
+
still wins on identifiers. Relevance dominates: feedback can only nudge a
|
|
98
|
+
score by ±10%.
|
|
99
|
+
|
|
100
|
+
**Degradation is graceful.** Embeddings are best-effort everywhere: vectors
|
|
101
|
+
live in an `m_embeddings` side table (L2-normalised float32, so cosine is a
|
|
102
|
+
plain dot product — no numpy dependency), atoms embed at `remember`/ingest
|
|
103
|
+
time, and if the provider is down or unconfigured recall silently falls back
|
|
104
|
+
to lexical-only. A memory write can never fail because of embeddings.
|
|
105
|
+
|
|
106
|
+
**Local by default.** The default provider is on-host Ollama (`embeddinggemma`,
|
|
107
|
+
768-dim): memory text never leaves the machine. An OpenAI-compatible off-host
|
|
108
|
+
provider (`NEOBRAIN_EMBED_PROVIDER=litellm`) is opt-in. Vectors are keyed by
|
|
109
|
+
`provider:model`, so switching providers re-embeds automatically — no
|
|
110
|
+
migration. `neobrain embed` backfills idempotently (hash-keyed, prunes vectors
|
|
111
|
+
of deleted atoms).
|
|
112
|
+
|
|
113
|
+
## The OpenCode integration
|
|
114
|
+
|
|
115
|
+
Thin plugin: [`adapters/opencode/neoBrain-memory`](./adapters/opencode/neoBrain-memory/README.md)
|
|
116
|
+
(single `index.ts`, no logic beyond protocol; no-op when the daemon is down).
|
|
117
|
+
The platform **pushes** memory into context deterministically and exposes the
|
|
118
|
+
same mind for **pull**:
|
|
119
|
+
|
|
120
|
+
- **Wake-up pack** — once per session: identity/rules, open loops, most
|
|
121
|
+
active hubs (`/api/mind/wakeup`).
|
|
122
|
+
- **Persona lane** — once per session, injects the workspace `SOUL.md` /
|
|
123
|
+
`IDENTITY.md` / `USER.md` (caps 20k chars/file, 60k total, USER.md 4k).
|
|
124
|
+
Local file I/O, deliberately independent of the daemon.
|
|
125
|
+
- **Standing directives** — pinned `preference` atoms rendered as a compact
|
|
126
|
+
imperative block and **re-injected on every model call**, so time, context
|
|
127
|
+
drift and compaction cannot drop the must-follow rules (weak models keep
|
|
128
|
+
obeying). Kept tiny on purpose (8 lines / 1000 chars, tunable); sits in the
|
|
129
|
+
cacheable system prefix; push-only, so it never trips the rating gate.
|
|
130
|
+
- **Per-turn recall** — the user's message is recalled against the mind and a
|
|
131
|
+
compact rank-ordered index is injected (top 10 one-liners + the #1 hit's
|
|
132
|
+
text). Curated types (preference/lesson/decision) auto-inject on ordinary
|
|
133
|
+
turns; memory-intent phrasing escalates to deep recall.
|
|
134
|
+
- **Unrated-feedback protocol** — every surfaced atom registers as pending;
|
|
135
|
+
past a grace window (120s) the next mind call gets an explicit blocking
|
|
136
|
+
notice instead of results; past the timeout (600s) it auto-clears as
|
|
137
|
+
exposure with no verdict. Deterministic — the critical path never depends
|
|
138
|
+
on model choice.
|
|
139
|
+
- **Compaction hook** — the directives block is also injected into the
|
|
140
|
+
summary request, so rules survive a compact; once-per-session gates reset
|
|
141
|
+
so persona + wake-up re-inject next turn.
|
|
142
|
+
- **Pull tools** — `memory_open(id)`, `memory_search(query)`,
|
|
143
|
+
`memory_rate(id, useful | noise)`.
|
|
144
|
+
- **Injection logging** — every lane POSTs what it pushed to
|
|
145
|
+
`/api/debug/injections`; the Debug page renders the daemon's one blind spot.
|
|
146
|
+
|
|
147
|
+
## Quickstart
|
|
148
|
+
|
|
149
|
+
Python 3.12. Nothing leaves the host by default (local Ollama embeddings;
|
|
150
|
+
BYOK OpenAI-compatible endpoint for the brain's own cognition).
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
python3.12 -m venv .venv
|
|
154
|
+
.venv/bin/pip install -e . # add ,[dev] for pytest
|
|
155
|
+
cp .env.example .env # then fill NEOBRAIN_LLM_* / embeddings
|
|
156
|
+
|
|
157
|
+
neobrain init # create the store + print next steps
|
|
158
|
+
neobrain init --install-plugin # also symlink the opencode plugin (below)
|
|
159
|
+
neobrain start # daemon: API + dashboard + life loop
|
|
160
|
+
neobrain ui # print and open the Observatory URL
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Dashboard defaults to `http://127.0.0.1:9192` (`NEOBRAIN_API`); the OpenAPI
|
|
164
|
+
schema is at `/api/schema`.
|
|
165
|
+
|
|
166
|
+
**OpenCode plugin** — symlink the adapter into the plugins directory (or let
|
|
167
|
+
`neobrain init --install-plugin` do it):
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
ln -s /path/to/neobrain/adapters/opencode/neoBrain-memory \
|
|
171
|
+
~/.opencode/plugins/neobrain-memory
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Never run it alongside the old `timeline-memory` plugin — two memory plugins
|
|
175
|
+
would both inject and double-rate memories.
|
|
176
|
+
|
|
177
|
+
## CLI
|
|
178
|
+
|
|
179
|
+
`neobrain --help` lists everything; the main subcommands:
|
|
180
|
+
|
|
181
|
+
| Command | Purpose |
|
|
182
|
+
|---|---|
|
|
183
|
+
| `init` | create the store; `--install-plugin` symlinks the OpenCode plugin |
|
|
184
|
+
| `start` | run the daemon (API + dashboard + life loop) |
|
|
185
|
+
| `ui` | print and open the dashboard URL |
|
|
186
|
+
| `ingest` | run ingest adapters one-shot (`--source`, default all) |
|
|
187
|
+
| `remember` | store a memory (`--type --hubs --link kind:target --label --dedupe`) |
|
|
188
|
+
| `recall` | recall a relevant subgraph (`--limit --json --lexical`) |
|
|
189
|
+
| `wakeup` | compact wake-up pack (identity + open loops + recent) |
|
|
190
|
+
| `pin` | pin/unpin an atom as a standing directive (`--off`) |
|
|
191
|
+
| `directives` | show the standing directives re-injected every turn |
|
|
192
|
+
| `embed` | backfill local embeddings (idempotent; `--force --limit`) |
|
|
193
|
+
| `dreams` | run a dream pass (`--phase light\|rem\|deep\|all`, `--replay YYYY-MM-DD`) |
|
|
194
|
+
| `reflect` | run the weekly reflection pass (re-authors the persona docs) |
|
|
195
|
+
| `forget` | delete a memory by id |
|
|
196
|
+
| `link` | wire two memories (`kind`: supersedes, derived-from, caused-by, consolidates, about) |
|
|
197
|
+
| `consolidate` | dream pass on demand (merge hubs, promote patterns) |
|
|
198
|
+
| `feedback` | record a quality signal (`used\|useful\|noise`) |
|
|
199
|
+
|
|
200
|
+
## API
|
|
201
|
+
|
|
202
|
+
Read-mostly JSON; groups worth knowing (full schema at `/api/schema`):
|
|
203
|
+
|
|
204
|
+
- `/api/mind/*` — `stats`, `graph`, `atom/{id}`, `activity`, `memories`,
|
|
205
|
+
`recall`, `wakeup`, `directives`, `pin`, `remember`, `feedback`,
|
|
206
|
+
`consolidate`, `dreams`, `reader`, `import`.
|
|
207
|
+
- `/api/debug/*` — `requests` (rolling trace of every `/api` call),
|
|
208
|
+
`injections` (plugin lane pushes, GET/POST), `overview` (rank states,
|
|
209
|
+
mind ops, feedback, ingest runs, life phases, self/soul trace, config).
|
|
210
|
+
- Plus `/api/health`, `/api/stats`, `/api/events`, `/api/sessions`,
|
|
211
|
+
`/api/search`, `/api/docs`, `/api/ingest/run`.
|
|
212
|
+
|
|
213
|
+
## Testing
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
.venv/bin/python -m pytest -q
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
117 tests passing (verified 2026-10-02), including rank calibration and
|
|
220
|
+
parity tests against the old timeline oracle (`tests/parity/`). Ranking
|
|
221
|
+
correctness is covered directly (`test_recall_rank.py`, `test_rank_*.py`).
|
|
222
|
+
There is no published retrieval benchmark (recall@k / MRR) yet — until one
|
|
223
|
+
exists, treat α as a starting point and tune it on your own store
|
|
224
|
+
(`recall --lexical` vs. hybrid makes the comparison by hand).
|
|
225
|
+
|
|
226
|
+
## Status
|
|
227
|
+
|
|
228
|
+
Active development. Sprints S0–S7 are done (core mind, Observatory, plugin,
|
|
229
|
+
rank, life loop, API/CLI wiring, cutover — see `SPEC.md` §14 for the board
|
|
230
|
+
and [`docs/CUTOVER.md`](./docs/CUTOVER.md) for the migration). S8 (npm
|
|
231
|
+
launcher + PyInstaller packaging) is pending, so there is no published
|
|
232
|
+
package yet — install from this repo.
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "neobrain-cli"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A simulated brain for AI agents: memory, rank-based forgetting, dreams and a self-authored soul."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
keywords = ["ai-agents", "memory", "mcp", "claude-code", "opencode", "self-hosted", "local-first", "llm", "durable-objects", "embedding"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Topic :: Software Development :: Build Tools",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"fastapi>=0.115",
|
|
24
|
+
"uvicorn>=0.30",
|
|
25
|
+
"pydantic>=2.7",
|
|
26
|
+
"python-dotenv>=1.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://neobrain.alionix.com"
|
|
31
|
+
Repository = "https://github.com/fakiho/neobrain"
|
|
32
|
+
Documentation = "https://github.com/fakiho/neobrain#readme"
|
|
33
|
+
Changelog = "https://github.com/fakiho/neobrain/releases"
|
|
34
|
+
|
|
35
|
+
[project.scripts]
|
|
36
|
+
neobrain = "neobrain.cli:main"
|
|
37
|
+
|
|
38
|
+
[project.optional-dependencies]
|
|
39
|
+
dev = ["pytest>=8", "httpx>=0.27"]
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["src/neobrain"]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build]
|
|
45
|
+
include = [
|
|
46
|
+
"src/neobrain/**",
|
|
47
|
+
"README.md",
|
|
48
|
+
"LICENSE",
|
|
49
|
+
]
|
|
50
|
+
exclude = [
|
|
51
|
+
"**/__pycache__",
|
|
52
|
+
"**/*.pyc",
|
|
53
|
+
"*/.env*",
|
|
54
|
+
"*/.wrangler/**",
|
|
55
|
+
"*/.github/**",
|
|
56
|
+
"tests/**",
|
|
57
|
+
"web/**",
|
|
58
|
+
"website/**",
|
|
59
|
+
"adapters/**",
|
|
60
|
+
"docs/**",
|
|
61
|
+
"SPEC.md",
|
|
62
|
+
"wrangler.jsonc",
|
|
63
|
+
".gitignore",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[tool.pyright]
|
|
67
|
+
include = ["src"]
|
|
68
|
+
typeCheckingMode = "standard" # tightened to strict for new modules (rank/life/runtime)
|
|
69
|
+
pythonVersion = "3.12"
|
|
70
|
+
|
|
71
|
+
[tool.pytest.ini_options]
|
|
72
|
+
testpaths = ["tests"]
|