r/PiCodingAgent • • 1d ago

News Built a local, zero-dep Pi extension for pre-flight safety & tool-surface narrowing (Aetheris Microbrains, <2ms p95 on CPU)

I have been working on Aetheris Microbrains and just added native Pi Coding Agent support as a zero-dependency Level 3 extension.

It is a deterministic, CPU-only pre-flight gate (sub-2ms p95 locally) that hooks directly into Pi's tool_call and tool_result lifecycle events. It catches dangerous shell commands, AST/code hazards, protected-path edits, and secret leaks before they execute or hit the LLM.

Why I built it for Pi specifically:

Pi's extension API is a great fit because tool_call gives us native ctx.ui.confirm() escalation in TUI and RPC modes (instead of blindly hard-blocking ambiguous commands), and tool_result lets us scrub secrets synchronously before the model sees tool output.

30-second setup:

git clone https://github.com/drtikov/microbrainsforagents

cd microbrainsforagents

pip install -e .

aetheris install pi --workspace .

aetheris-daemon --http --host pi --port 8765

Then launch pi (or run /reload). Inside Pi you get two commands:

- /aetheris (quick daemon health check + session counters)

- /aetheris-config (effective config with masked token + session stats)

Zero Node dependencies and zero build step — it is a single ESM file loaded directly by Pi, configurable via env vars or .pi/settings.json ("aetheris").

What the 4-layer gate does:

- Layer 0: Protects sensitive & control-plane paths (.pi/, .git/, .ssh/, .env*)

- Layer 1: Polyglot AST + shell inspection (catches eval/exec, obfuscated base64 execution, secret leaks, rm -rf, curl | sh, breaking changes)

- Layer 2: Deterministic Top-4 MoE tool routing + conformal abstention (narrows 32 tools down to 4 with session-sticky caching)

- Layer 3: Escalates uncertain actions to you via ctx.ui.confirm() in TUI/RPC (fail-closed in non-interactive runs)

Local CPU benchmarks (reproducible scripts in examples/, no live API calls):

- 25-step agent loop: 64,800 down to 12,312 schema tokens (81% reduction, ~52k tokens saved/loop), 0 missed hazards, 0 false positives, 0.95ms p95

- Agent security & anti-evasion suite: 22/22 attacks caught (100% TPR), 0/12 benign false positives (0% FPR), 0.38ms p50 / 2.1ms p95

- SWE-bench Lite (300 real gold diffs): 297/300 allowed/escalated (3/300 denied on eval/exec patterns, 0 AST syntax false positives), 36.7% schema saved, 1.76ms p50 / 4.5ms p95

- Frozen 120-task prompt-cache advisor: 100% Recall@4, 0 dropped tools, 56.7% cache-adjusted net token savings (session-sticky)

- Counterfactual safety gate: blocks obfuscated base64 + exec payloads and breaking signature regressions

Modeled token economics (local estimate, not live invoices): ~52k prefill tokens saved per 25-step loop (~ \$0.079/loop at \$1.50/M input, or ~ \$118/month at 50 loops/day), plus ~2.1 Wh avoided per loop, all running locally on CPU so your code never leaves your machine for the gate.

Quick FAQ:

- Does it replace my LLM? No, it is a pre-filter. Your LLM still does all reasoning; Microbrains narrows the tool surface, scrubs secrets, and blocks dangerous actions locally.

- What if the daemon is down? Fail-closed by default. Set AETHERIS_FAIL_CLOSED=0 to allow, or AETHERIS_HOOK_SHADOW=1 for observe-only shadow mode.

- Does it work with any provider/model? Yes, it is model-agnostic (Anthropic, OpenAI, Google, local models via Pi).

Repo: https://github.com/drtikov/microbrainsforagents

Pi extension folder: pi-aetheris-microbrains/

Reproducible benchmarks: examples/05_reproducible_25step_benchmark.py, examples/07_swebench_lite_gate_benchmark.py, examples/08_frozen_advisor_benchmark.py, examples/10_agent_security_benchmark.py

Disclaimer: I am the author of Microbrains. Built it because I wanted fast, deterministic local guardrails for my own coding agent workflows and figured other Pi users might find it useful. Try it out and let me know what breaks — happy to help debug any edge cases.

0 Upvotes

10 comments sorted by

4

u/Material_Lie6294 1d ago

Zero-dep, except you pip install it and keep a daemon running on 8765 before anything works. What's the sub-2ms p95 measured on, an empty diff or a real file whose full AST you're parsing?

-1

u/drtikov 1d ago

Fair call on both counts — I just pushed two fixes directly addressing both points:

  1. On "zero-dep" and managing the daemon on 8765: You are right that calling the whole stack "zero-dep" was sloppy wording. What is zero-dep is the Pi extension itself (zero npm packages, no build step) and the Python security core (pure stdlib; NumPy is only for optional routing modules), while Python 3.10+ + the local daemon are still required. To remove the manual daemon chore, I just added loopback auto-spawning (AETHERIS_AUTO_START=1) to pi-aetheris-microbrains/index.js: if nothing is listening on 127.0.0.1:8765, the extension spawns aetheris_microbrains.hook_daemon cleanly with an ephemeral token passed via env and shuts it down on session_shutdown. You just run pip install -e ., aetheris install pi --workspace ., and launch pi.
  2. On the "<2ms p95" number — fair question, and I updated the README to separate in-process, real-file AST, and HTTP round-trip numbers clearly:
  • Small shell commands & typical tool payloads (in-process, N=500): 0.085ms p50 / 0.237ms p95 (25-step benchmark: 0.95ms p95; 22-attack security suite: 0.383ms p50 / 2.121ms p95).
  • Real files with full AST parsing (300 real SWE-bench Lite gold diffs, in-process): 1.76ms p50 / 4.54ms p95.
  • Larger Python files (in-process AST): ~5.5ms p95 at 100 lines, ~31.5ms p95 at 480 lines.
  • End-to-end over the warm localhost HTTP daemon (what the Pi extension actually hits): adds loopback HTTP + JSON overhead on top — ~6ms p50 / ~10ms mean / ~24ms p95 on Windows (vs ~400ms for spawning Python cold on every tool_call).

Appreciate the pushback — both the README and the extension UX are better for it.

1

u/HyperWinX 4h ago

Not the "fair call" lmfaoo

1

u/fingerthief 1d ago edited 1d ago

Interesting I’m in the middle of improving my small local risky command classifier model (100MB memory overhead), definitely curious how this compares.

Will check it out! There are surprisingly few effective tools when it comes to true risky command guards.

Edit - Since this is just a lame bot instead of a human having a conversation, here is how this actually performs.

1

u/drtikov 1d ago

let me know if you found any bugs risks or whatever, thanks for interest!

1

u/fingerthief 1d ago

For my use case it's not particularly what I'm looking for it seems, but I think the idea is pretty interesting.

The issue I generally have with risk classifiers are they are exceptionally noisy and if I have to manually approve commands often I just end up disabling them entirely. In my tests this setup flagged 80% of safe commands (~7700 safe commands) I benched it on as "ask".

Will definitely keep track of the progress!

-1

u/drtikov 1d ago

Thanks a lot for running that benchmark on 7,700 real commands and calling out the ~80% "ask" rate — that's completely fair feedback. Nobody keeps a security gate enabled if it interrupts 4 out of 5 normal commands.

You caught a real blind spot on our side: the default strict profile only allowlisted ~25 shell commands, and even dev was missing everyday tools (cd, stat, tree, diff, read-only sed/awk, uv, node/npx/bun/tsc/vitest, go, make/just, read-only gh/docker/kubectl, plus git -C tripping the -c config-override check).

Just pushed a fix (47d1816) with three things:

Appreciate you testing it — if you ever try --profile guardrails (or --profile dev) against your 7,700-command set again, would love to hear how much the ask rate drops or which benign commands still trigger it!Thanks a lot for running that benchmark on 7,700 real commands and calling out the ~80% "ask" rate — that's completely fair feedback. Nobody keeps a security gate enabled if it interrupts 4 out of 5 normal commands.

You caught a real blind spot on our side: the default strict profile only allowlisted ~25 shell commands, and even dev was missing everyday tools (cd, stat, tree, diff, read-only sed/awk, uv, node/npx/bun/tsc/vitest, go, make/just, read-only gh/docker/kubectl, plus git -C tripping the -c config-override check).

Just pushed a fix (47d1816) with three things:

Expanded the built-in read-only POSIX and dev toolchain allowlists and fixed git -C / git switch -c parsing.
Added a new --profile guardrails mode (AETHERIS_PROFILE=guardrails) built specifically to eliminate prompt fatigue: it keeps all hard security vetoes (rm -rf /, curl | sh, protected-path/.env access, AST evasions, secret egress, IFC taint) and still asks on genuinely risky categories (network/egress, privilege escalation, recursive delete, inline -c/-e eval, dangerous flags), while letting ordinary unrecognized local dev CLIs run without prompting.
Verified 22/22 attack detection (0% benign FPR) and zero false prompts across our new benign dev corpus in both dev and guardrails.

Appreciate you testing it — if you ever try --profile guardrails (or --profile dev) against your 7,700-command set again, would love to hear how much the ask rate drops or which benign commands still trigger it!

1

u/fingerthief 1d ago

This is a new level of agentic laziness.

1

u/drtikov 1d ago

I agree