DEV Community

Cover image for I built AegisBrain: A local memory bus and token arbitrage engine for AI coding agents
PRIYANSHU GHOSH
PRIYANSHU GHOSH

Posted on Edited on

I built AegisBrain: A local memory bus and token arbitrage engine for AI coding agents

I built AegisBrain (GPLv3) to solve two practical frustrations I kept encountering while using AI coding agents (Claude Code, Gemini CLI, Cursor, and Antigravity):

  1. Context Amnesia & Token Waste: Every time an agent starts a new session or compacts context, it typically crawls multi-megabyte JSONL session transcripts (often 40k–60k tokens) just to recall past architectural decisions, rules, and invariants. This wastes tokens and increases latency.

  2. Frontier Token Waste on Boilerplate: Paying frontier model API pricing to generate repetitive boilerplate, DTOs, and unit test suites felt inefficient when quantized local models (such as Qwen 2.5 Coder 7B/14B or Hermes on Ollama) can synthesize raw code locally for free.

How it works:

  • Sub-Second Neural Memory Bus (< 25ms):
    Instead of dumping full session histories into the prompt, AegisBrain indexes decisions, code anchors, and invariants into a local SQLite database using FTS5 (Full-Text Search) with a Porter stemmer in WAL mode. When an agent needs context, "aegis recall " queries the FTS5 index and injects a deterministic summary in under 400 tokens (~18ms query latency on NVMe).

  • Adaptive Dual-Mode Triage:

  • Quick Mode: For surgical one-line fixes, typos, or direct pair-programming, the frontier model operates directly as the lead engineer without delegation latency.

  • Arbitrage Mode: For heavy boilerplate or multi-file test suites, the frontier model specifies architectural invariants and delegates raw code generation to local Tier 3 workers (via Ollama or Hermes CLI).

  • 4-Stage Quality Gate Architecture:
    Before any local candidate draft is committed to the host workspace, it must pass four automated verification gates:

  • Karpathy Simplicity Doctrine: Blocks speculative wrappers and unnecessary abstractions.

  • Unslop Filter: Strips conversational filler and LLM preambles.

  • AST Security Scanner: Detects and rejects dangerous sinks (e.g. shell=True, unsanitized command interpolations).

  • Dual-Environment Verification: Compiles and runs unit tests in an isolated sandbox before host application.

  • Zero-Residue Snapshot Engine:
    Before any agent modifies configurations (e.g., GEMINI.md, CLAUDE.md, .cursorrules), AegisBrain creates a restore snapshot. Running "aegis restore" displays all historical points with timestamps and allows single-key rollback with zero leftover residue.

  • Lightweight Background Daemon:
    An asynchronous local HTTP daemon runs on 127.0.0.1:8765 using ~35MB RAM, providing health checks, an optional Telegram bridge for remote status checks, and scheduling.

Benchmarks:

  • Memory Query Latency: ~18.2 ms on local SSD (vs 8,500ms+ for unindexed log parsing).
  • Context Overhead: ~350 tokens injected into ACTIVE_CONTEXT.md vs 50,000+ tokens for full-transcript crawling.
  • Local Daemon Footprint: ~35 MB RSS memory, < 0.1% idle CPU.

GitHub: https://github.com/PG300604/aegis-brain
Project & Documentation: https://aegis-brain-alpha.vercel.app

100% open source under GPLv3. I would appreciate
feedback and critique of the architectural trade-offs!

Top comments (0)