Skip to main content

m1-m2-agent — Cost-Aware Hierarchical Coding Agent & Context Firewall

Empower any Frontier Coding Agent (Antigravity, Cursor, Claude Code, Cline, Windsurf) with an autonomous, cost-efficient M2 worker agent and context firewall.
Cut LLM token costs by up to 92% while keeping your frontier reasoning context 100% pristine.

PyPI Python License Tests


💡 The Core Philosophy: The Human-Agent Analogy

In modern software development, a senior human engineer never manually reads 50,000 lines of raw compiler logs or scans 1,000 files by hand when an AI coding agent can do it.

  • The Human Engineer operates at the high cognitive tier: planning architecture, evaluating trade-offs, formulating hypotheses, and reviewing results.
  • The Coding Agent operates at the implementation tier: searching directories, parsing AST symbols, executing tests, and writing code.

m1-m2-agent brings this exact division of labor to AI Coding Agents:

  • M1 (The Frontier Reasoner / Orchestrator): Powered by flagship models (Gemini 1.5 Pro, Claude 3.5 Sonnet, GPT-4o), M1 acts like the human software architect—focusing on strategy, architecture, and high-ambiguity decisions.
  • M2 (The Cheap Worker Agent & Context Firewall): Powered by fast, affordable models (Gemini Flash, DeepSeek V4-Flash, Claude Haiku, Local Ollama/Qwen), M2 acts as the autonomous coding agent for M1—executing multi-turn tool loops, searches, and test suites, compressing outputs (80–98% CCR) and returning structured evidence.

⚡ Quickstart

1. Install via pip

pip install m1-m2-agent

2. Drop into Any Project (30 seconds)

cd /path/to/your/project
m2 init

m2 init interactively configures your model and API key via LiteLLM, generates cognitive rule files (AGENTS.md, .cursorrules, CLAUDE.md, .continuerules), and displays the plug-and-play MCP configuration.

3. Connect to Your IDE / Agent

Add to your IDE's MCP settings (.cursor/mcp.json, Antigravity settings, Claude Code, Cline, etc.):

{
  "mcpServers": {
    "m2-worker-agent": {
      "command": "m2-mcp-server",
      "env": {
        "WORKSPACE_ROOT": "/path/to/your/project"
      }
    }
  }
}

🤖 100+ Supported LLM Models via LiteLLM

Switch between cloud APIs or 100% offline local models with a single command:

# Google Gemini Flash (High speed & rate limits)
m2 config set model gemini/gemini-flash-latest

# DeepSeek V4-Flash (Ultra-low token cost)
m2 config set model deepseek/deepseek-chat

# Anthropic Claude 3.5 Haiku (High precision reasoning)
m2 config set model claude-3-5-haiku-20241022

# Local Ollama (100% Offline, 0 API cost)
m2 config set model ollama/qwen2.5-coder:7b
m2 config set api_base http://localhost:11434

# Test your active connection
m2 config test

# Inspect active configuration
m2 config show

For full details, see the Multi-Provider Configuration Guide.


🛠️ CLI Command Reference

m1-m2-agent provides a complete command-line toolkit:

Command Description
m2 "<task>" Execute one-shot task with real-time streaming & KPI telemetry
m2 run "<task>" Explicit task runner subcommand
m2 outline [N] Instant zero-token AST code outline of top N modules
m2 diff [--staged] Instant zero-token Git diff & status summary
m2 hud [--port PORT] Launch the live Webview HUD browser dashboard (localhost:4040)
m2 init [--dir DIR] Scaffold cognitive rules & MCP configuration into any repository
m2 config <subcommand> Interactive wizard, test, show, or set models and API keys
m2 tdd "<issue>" Path C autonomous Red-Green test diagnostic and bugfix loop
m2 Launch interactive M1 pair-programming REPL
m2-mcp-server Run Model Context Protocol server on stdio
m2-hud Standalone Web HUD runner
m2-init Standalone project scaffolding CLI
m2-config Standalone configuration manager

For full CLI options, see the Complete CLI Reference.


🏛️ Tri-Modal Architecture

                          ┌───────────────────────────────┐
                          │              L2               │
                          │   Frontier Reasoner (M1)      │
                          │   Plan • Decide • Review      │
                          └───────┬───────────────┬───────┘
                                  │               │
                          Path A  │        Path B │ (Direct Precision Fallback)
                      (Delegated) │               │
                                  ▼               │
                          ┌───────────────┐       │
                          │      L1       │       │
                          │  Worker (M2)  │       │
                          │ Context F/W   │       │
                          └───────┬───────┘       │
                                  │               │
                                  ▼               ▼
                          ┌───────────────────────────────┐
                          │              L0               │
                          │  Deterministic Fast Execution │
                          │   AST • Ripgrep • Git • LSP   │
                          └───────────────────────────────┘
  • Path A (Delegated Worker): Heavy searches, AST outlines, multi-step refactorings, test runs.
  • Path B (Direct Precision Fallback): Subtle race condition diagnosis, security audits, isolated single-file diffs.
  • Path C (Autonomous TDD Investigation): Red-Green diagnostic loop isolating test failures with reproduction scripts and verified patches.

🐍 Python SDK Usage

from m1_m2_agent import M1OrchestratorAgent, TaskContract

# Initialize agent for current workspace
agent = M1OrchestratorAgent(workspace_root=".")

# Execute task with Context Firewall compression
response = agent.route_and_execute(
    objective="Locate all database connection pools and check for resource leaks",
    execution_path="PATH_A_DELEGATED"
)

print("Synthesized Result:\n", response["result"])
print("Structured Evidence:\n", response.get("evidence"))

# Inspect token savings & KPIs
telemetry = agent.get_telemetry_report()
print(f"Context Compression Ratio: {telemetry['context_compression_ratio']:.1f}%")
print(f"Frontier Token Avoidance:  {telemetry['frontier_token_avoidance_rate']:.1f}x")
print(f"Cost Reduction:            {telemetry['cost_reduction_pct']:.1f}%")

🌐 Live Webview HUD Dashboard

Launch the browser HUD on http://localhost:4040:

m2 hud
  • Live Event Stream: Turn-by-turn thoughts, tool calls, and execution outputs via SSE.
  • Real-Time Gauges: Live CCR, FTAR, and API cost reduction metrics.
  • Zero-Token AST Explorer: Interactive visual map of all Python classes and methods.
  • Git Inspector: Live diff hunks and file status tracking.
  • Transcript Scrubber: Step-by-step history replay with automatic secret redaction.

📚 Documentation Index


📄 License

Apache License 2.0. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

m1_m2_agent-0.3.2.tar.gz (91.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

m1_m2_agent-0.3.2-py3-none-any.whl (83.5 kB view details)

Uploaded Python 3

File details

Details for the file m1_m2_agent-0.3.2.tar.gz.

File metadata

  • Download URL: m1_m2_agent-0.3.2.tar.gz
  • Upload date:
  • Size: 91.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for m1_m2_agent-0.3.2.tar.gz
Algorithm Hash digest
SHA256 557b574952be9d7be5c19e71545e8ac21e65fb85f009a434b927008132d75816
MD5 57b0176a5c5b8779a5922b3f7f26d8d7
BLAKE2b-256 57a5696762902f2b47ca9cbc38b96b0c18f666b6fd2535f290f90db3efcb8b0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for m1_m2_agent-0.3.2.tar.gz:

Publisher: publish.yml on HamzaDevv/hierarchical-coding-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file m1_m2_agent-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: m1_m2_agent-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 83.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for m1_m2_agent-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1eb2ae24c680e8b693eb78320dec14d6742536ba89af8919f09f97ae05d66796
MD5 e5d2cdbc0544ddd390c31bde0ff017b6
BLAKE2b-256 25eae78e1fc9dd429034d32500268bc5cebbf4fc5427768556d9a644936d23a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for m1_m2_agent-0.3.2-py3-none-any.whl:

Publisher: publish.yml on HamzaDevv/hierarchical-coding-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.6.0

2 files

This release

0.3.2 This release

2 files

0.3.1

2 files

0.3.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page