1. From Solo Coding CLI to Deterministic Engineering Organization
Out of the box, Anthropic's Claude Code is an extraordinary single-developer terminal agent. Give it a clear prompt in a clean repository and it will read files, run tests, and ship patches. Yet the moment an enterprise tries to scale that single-prompt loop into a multi-developer, multi-agent engineering organization, four structural failure modes emerge:
- Context & Spec Drift: Agents improvise architecture mid-flight, ignore existing invariants, and leave half-implemented
TODOstubs behind. - Security & Exfiltration Blind Spots: Indirect prompt injections in issues, logs, or third-party dependencies can coerce tool calls into leaking
.envsecrets or running destructive shell commands. - Runaway Token & Subagent Spend: Recursive retry loops and fan-out subagents burn through API quotas without a hard USD or token circuit breaker.
- Workspace Collisions & Ephemeral Audits: Parallel agents editing the same working tree overwrite each other's files, while terminal scrollback leaves zero cryptographic proof of what actually happened.
CLAUDE.md prompts. You wrap the coding CLI in a deterministic control plane built from two native primitives: synchronous lifecycle hooks (PreToolUse, PostToolUse, Stop) and the Model Context Protocol (MCP).
The Benni OS Open-Source Suite decomposes this control plane into six composable, zero-lock-in packages designed specifically for Claude Code: claude-code-sovereign, agent-context-firewall, agent-budget-governor, agent-trace-replay, conductor-sdd, and claude-code-swarm.
2. End-to-End Reference Architecture
Instead of a monolithic wrapper that forks Claude Code, the Benni OS blueprint operates purely through Claude Code's official extension points. Hooks intercept every tool boundary on local CPU in sub-millisecond time, while MCP servers expose stateful coordination, Spec-Driven Development (SDD) contracts, bi-temporal memory, and offline time-travel replay.
Every layer has a strict, non-overlapping responsibility. If a worker hallucinates an unapproved dependency, conductor-sdd catches the spec violation. If an untrusted README attempts prompt injection, agent-context-firewall kills the Bash call before the OS spawns a process. If a loop stalls, agent-budget-governor trips the circuit breaker. And when the run finishes, agent-trace-replay provides an immutable, $0.00-replayable SHA-256 audit log.
3. The Six-Kernel Open-Source Stack
1. claude-code-sovereign — The Microkernel Bus
Benni-OS/claude-code-sovereign is the central hook dispatcher and Reality Lock engine. It binds Python handlers to PreToolUse, PostToolUse, SubagentStop, and Stop. On Stop, it parses modified files via AST, rejects placeholder stubs (TODO, NotImplementedError), and runs fast verification checks—forcing Claude Code to self-heal before returning control to the user.
2. agent-context-firewall — PreToolUse Security Boundary
Operating synchronously inside PreToolUse, agent-context-firewall enforces taint tracking and egress policies across Bash, Read, Write, and MCP payloads. It blocks secret exfiltration (.env, *.pem, SSH keys, cloud credentials), flags encoded shell sinks, and neutralizes indirect prompt injection payloads before they touch the filesystem or network.
3. agent-budget-governor — FinOps Circuit Breakers
Connected to PostToolUse and SubagentStop, agent-budget-governor maintains an atomic per-session and per-subagent ledger of input/output tokens and USD spend. When a worker or subagent tree approaches its configured ceiling (e.g., max_session_usd: 5.00), the next PreToolUse gate trips deterministically, preventing overnight API bill explosions.
4. agent-trace-replay — SHA-256 Time-Travel Observability
Benni-OS/agent-trace-replay records every tool invocation, firewall decision, budget delta, and file diff into an append-only, content-addressed JSONL chain linked by SHA-256 parent hashes. Engineers can step backward and forward through any agent session offline at $0.00 token cost, or query specific failure frames over MCP.
5. conductor-sdd — Spec-Driven Development & Zero-Drift Contracts
conductor-sdd replaces ad-hoc prompting with machine-verifiable specifications. Before code generation begins, an architecture spec is locked into .conductor/spec.json. Exposed as both an MCP server and a Stop hook validator, it guarantees that every implemented symbol, endpoint, and test assertion maps directly to an approved requirement—eliminating architectural drift.
6. claude-code-swarm — Worktree Swarm Governance & Bi-Temporal Memory
Scaling beyond one agent requires physical filesystem isolation. claude-code-swarm orchestrates parallel Claude Code workers inside dedicated git worktree sandboxes, preventing dirty-tree collisions. Shared state is synchronized via a bi-temporal memory MCP server that indexes facts by both valid time (when a codebase invariant became true) and transaction time (when an agent recorded it), so parallel workers never act on stale architectural assumptions.
4. Unified Configuration: Wiring Hooks & MCP
Because the entire ecosystem relies on standard Anthropic configuration files, onboarding a repository requires only .claude/settings.json for deterministic lifecycle hooks and .mcp.json for stateful team coordination.
Lifecycle Hook Pipeline (.claude/settings.json)
{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "agent-context-firewall check --strict && agent-budget-governor gate",
"timeout": 1500
}
]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "agent-budget-governor record && agent-trace-replay append",
"timeout": 1500
}
]
}
],
"SubagentStop": [
{
"hooks": [
{
"type": "command",
"command": "agent-budget-governor rollup-subagent",
"timeout": 2000
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "claude-code-sovereign reality-lock && conductor-sdd verify",
"timeout": 6000
}
]
}
]
}
}
Stateful MCP Control Plane (.mcp.json)
{
"mcpServers": {
"conductor-sdd": {
"command": "conductor-sdd",
"args": ["mcp-serve", "--spec", ".conductor/spec.json"]
},
"claude-code-swarm": {
"command": "claude-code-swarm",
"args": ["mcp-serve", "--worktree-root", ".worktrees"]
},
"bitemporal-memory": {
"command": "claude-code-sovereign",
"args": ["memory-mcp", "--db", ".sovereign/memory.sqlite"]
},
"agent-trace-replay": {
"command": "agent-trace-replay",
"args": ["mcp-serve", "--traces-dir", ".sovereign/traces"]
}
}
}
5. Ecosystem Matrix: Capabilities & Guarantees
| Open-Source Package | Integration Surface | Deterministic Guarantee | Enterprise Failure Eliminated |
|---|---|---|---|
claude-code-sovereign |
Stop hook + Core Bus |
AST & linter Reality Lock | Broken syntax, TODO stubs, false completions |
agent-context-firewall |
PreToolUse (< 1ms) |
Exit code 2 OS-level block | Indirect prompt injection & .env exfiltration |
agent-budget-governor |
PreToolUse / PostToolUse / SubagentStop |
Hard USD & token circuit breaker | Runaway retry loops & unbudgeted subagent spend |
agent-trace-replay |
PostToolUse + MCP |
SHA-256 content-addressed JSONL | Ephemeral logs & expensive live debugging |
conductor-sdd |
MCP + Stop Gate |
Spec-to-diff contract verification | Architectural drift & unrequested refactors |
claude-code-swarm |
MCP + git worktree |
Isolated branches + bi-temporal memory | Parallel file collisions & stale shared context |
6. Bi-Temporal Memory & Worktree Governance in Practice
When five Claude Code workers operate in parallel across five git worktree directories, traditional vector stores fail because they flatten time. If Worker A refactors an authentication middleware at T1 while Worker B is mid-task on an endpoint that depends on the old signature, a naive RAG lookup returns conflicting truths.
By pairing claude-code-swarm with a bi-temporal memory graph over MCP, every architectural fact carries both a valid_from timestamp (tied to commit/spec state) and an ingested_at timestamp. Workers query memory scoped to their worktree's base commit and active conductor-sdd contract—guaranteeing zero-drift coordination across the entire swarm.
Deploy the Deterministic Anthropic Stack
Explore the open-source repositories under Benni-OS, wire the hook & MCP kernels into your repository, and transform Claude Code into an auditable, zero-drift engineering organization.
Explore Benni-OS on GitHub →Frequently Asked Questions
PreToolUse, PostToolUse, Stop), isolated git worktrees, and spec-driven contracts, multi-agent runs suffer from architectural drift, merge conflicts, runaway token spend, and silent security regressions.claude-code-sovereign acts as the core microkernel orchestrating lifecycle hooks; agent-context-firewall blocks prompt injection and secret exfiltration before execution; agent-budget-governor enforces hard USD and token circuit breakers; agent-trace-replay records SHA-256 trajectories for $0.00 offline replay; conductor-sdd enforces Spec-Driven Development contracts; and claude-code-swarm coordinates parallel agents in isolated git worktrees.claude-code-sovereign and agent-context-firewall for immediate PreToolUse protection and FinOps caps, then add conductor-sdd, agent-trace-replay, and claude-code-swarm as they scale to parallel multi-agent delivery.