tbcresearch.
Menu
← Back to the research index
F-002 / Tool

BrainBox: muscle memory for coding agents

Local procedural memory that learns which files, tools and fixes go together, then recalls them before your agent searches. Research-driven ranking, code search and measured results.

BrainBox: muscle memory for agents, illustrated by a learned network of related files. Open full-size figure. BrainBox: muscle memory for agents, illustrated by a learned network of related files. Open full-size figure.
Local SQLite memory. No LLM calls to learn or recall. Select any figure to inspect it at full size.

Your agent should not rediscover the same codebase every session. BrainBox watches the work: files read and edited together, recurring tool chains, and the edits that turn a failing command into a passing one. On a later task, those traces help it suggest where to start.

The core is a Hebbian graph, not a hosted retrieval service. The v0.2 implementation adds local hybrid snippet search, structural code hints, recall attribution and a learned injection policy. No GPU or paid API is required; project memory stays on your machine. Optional embedding models are downloaded separately and run locally.

Source and installation guide · Paper (v0.2): doi:10.5281/zenodo.23250505 · npm package

The learning loop

Tool calls feed learning, recall, an injection decision and compact context; opened or ignored suggestions feed back into the graph and policy. Tool calls feed learning, recall, an injection decision and compact context; opened or ignored suggestions feed back into the graph and policy.
The agent's actual opens and edits are feedback, not just another stored transcript.

Claude Code's PostToolUse hook records activity. Before the next prompt, UserPromptSubmit retrieves candidates and chooses whether to inject zero, one, three or five files. SessionEnd scores recall outcomes and compiles procedures. A contextual UCB1 policy balances useful suggestions against their token cost.

Ignored recalls weaken the specific edge that produced them. Deleted files are excluded from injection. The brainbox-explain MCP tool exposes recall provenance so a suggestion can be inspected rather than taken on trust.

Fire together, wire together

Repeated co-access strengthens the same file connection across sessions; sigmoid plasticity slows strengthening near the ceiling. Repeated co-access strengthens the same file connection across sessions; sigmoid plasticity slows strengthening near the ceiling.
Repeated use reinforces connections. Access order also contributes a directional signal.

The graph records which files occur together, while directional learning distinguishes what tends to come next. Recall combines lexical matches, Hebbian evidence, snippet matches, frequency, recency, ACT-R base-level activation and recurrence through weighted reciprocal-rank fusion. Query matching remains important: a habitual file should not displace an explicit request for another topic.

Recall a file, find its neighbours

Activation spreads from a matching file through three graph hops into a ranked list; high-degree hubs are damped. Activation spreads from a matching file through three graph hops into a ranked list; high-degree hubs are damped.
Three-hop spreading follows learned associations without letting large hubs dominate.

Tree-sitter import, call and inheritance edges provide weak structural hints for TypeScript/JavaScript, Python, Rust, Swift and Go. Learned co-access can outrank these priors. Code search uses syntax-aware chunks, SQLite FTS5 BM25 and sqlite-vec nearest-neighbour search, merged by rank fusion. Indexing is incremental by content hash.

The default code embedding model is jina-embeddings-v2-base-code. Until a model is cached, hooks fall back to lexical retrieval rather than downloading inside the hook. Set BRAINBOX_EMBEDDINGS=off for keyword-only operation.

Familiar routes become faster

A repeatedly used route becomes a strong red pathway while unused links decay; ACT-R activation accounts for recency and frequency. A repeatedly used route becomes a strong red pathway while unused links decay; ACT-R activation accounts for recency and frequency.
Recency and frequency help familiar paths survive; unused links lose strength.

Tool prediction uses the longest supported suffix of the recent tool sequence, up to three tools, rather than only the last tool. Consolidation replays episodic memory and compiles recurring patterns into procedures.

Remember the fix, not just the error

A failed command, corrective edits and passing rerun become an error-fix procedure, recalled when the same error fingerprint returns. A failed command, corrective edits and passing rerun become an error-fix procedure, recalled when the same error fingerprint returns.
Procedures are quoted observations from earlier work, not trusted instructions.

BrainBox mines failed command → edits → passing rerun sequences and repeated multi-session workflows. Helpful and harmful outcomes are tracked. Generic read/edit loops are not promoted as useful advice, and remembered content is presented as historical evidence rather than a command to execute.

What the evaluation shows

Replay evaluation comparing v0.1, v0.2 and the best simple baseline on SWE tasks and local sessions. Next-tool prediction improves; frequency still wins local edited-file recall, and next-file results tie recency within uncertainty. Replay evaluation comparing v0.1, v0.2 and the best simple baseline on SWE tasks and local sessions. Next-tool prediction improves; frequency still wins local edited-file recall, and next-file results tie recency within uncertainty.
MRR for file retrieval; MRR@3 for next-tool prediction. Higher is better. Losses and ties remain visible. Exact scores and confidence intervals.

The offline harness replays real agent trajectories chronologically. Each task receives a fresh in-memory database trained only on earlier tasks from the same repository. Baselines include recency, frequency, BM25, EdgeBank and random ranking; paired 95% bootstrap confidence intervals are resampled by trajectory.

Ranking weights were fit on development repositories, not the test repositories. The reported test cohort contains 40 SWE tasks from Pillow and conan; the local cohort contains 97 sessions across five projects. These runs have embeddings disabled.

The clear win is next-tool prediction. Both cohorts beat their best simple baseline. Read-file retrieval improves over v0.1, but the result is not a universal win: local edited-file recall regresses and remains below frequency-only ranking. Next-file prediction is statistically tied with recency. These are retrieval measurements, not proof of higher task-completion rates or measured token savings.

A separate 30-query code-search check reports Recall@5 of 0.867 for Jina code embeddings versus 0.667 for MiniLM. That small check is not the trajectory replay benchmark and should not be read as a broad generalisation claim.

Install and use

Requires Node.js 22 or newer. The package's install script registers Claude Code hooks and MCP, creates the local database and starts an embedding-model download. See the repository for the current package release; the research changes described here are in the v0.2 source tree on main.

npm install brainbox-hebbian

Seed memory from a repository's history and structure, then inspect recall:

brainbox bootstrap --repo /path/to/project --imports
brainbox recall "authentication login"
brainbox stats
brainbox procedures list

For the v0.2 source checkout:

git clone https://github.com/thebasedcapital/brainbox.git
cd brainbox
npm install
npm run build

Existing databases with older MiniLM vectors need re-indexing for the new model:

brainbox embed
brainbox extract-snippets --force

Integrations: Claude Code hooks; an MCP server with eight tools (record, recall, explain, error, resolve, predict_next, stats, decay); a native Kilo/OpenCode plugin; and OpenClaw through NeuroVault. The system-wide macOS file watcher is opt-in, not part of the default installation. Integration and configuration instructions.

Research behind the implementation

Research sources mapped to six BrainBox subsystems: ranking, code search, injection policy, attribution, procedures and evaluation. Research sources mapped to six BrainBox subsystems: ranking, code search, injection policy, attribution, procedures and evaluation.
Research supplies design ideas. BrainBox's own replay evaluation supplies the evidence for this implementation.

BrainBox is MIT-licensed. Read the implementation and full results, the paper (v0.2): doi:10.5281/zenodo.23250505, or report an issue.

Cite this entry

@misc{tbc_brainbox,
  title = {BrainBox: muscle memory for coding agents},
  author = {{TBC Research}},
  note = {F-002, catalogue version 2},
  howpublished = {\url{https://tbcresearch.org/research/brainbox/}},
  doi = {10.5281/zenodo.23250505}
}
← All research