{"slug": "ctxfw-in-memory-tree-sitter-ast-compactor-that-cuts-coding-tokens-by-72", "title": "Ctxfw – In-memory Tree-sitter AST compactor that cuts coding tokens by 72%", "summary": "CTXFW, an open-core context firewall and Model Context Protocol gatekeeper from HeuristicoLab, compacts peripheral code in coding-agent context windows by 72.4% by replacing distance-1 and distance-2+ module implementations with interface signatures, type definitions, and functional stubs. The tool reports a 59.5% net context reduction in an internal benchmark against 16 dependencies, cutting 49,096 tokens to 20,014 tokens and eliminating 29,222 tokens, with AST compaction latency of 1,407.96 ms and engine compaction overhead under 5.0 ms. CTXFW installs via pip or pipx and registers as a stdio MCP server in clients including Claude Desktop, Cursor, Windsurf, and Antigravity, and is listed with Grade A compliance on the Glama MCP registry.", "body_md": "**The deterministic boundary between probabilistic LLM hallucination and production infrastructure.**\n\n[Installation](#installation) • [Benchmarks](#ast-pruning-benchmarks) • [Diagnostic](#system-diagnostics) • [Architecture](#architecture) • [Enterprise Governance](#enterprise-governance)\n\nAutonomous coding agents (Claude, Gemini, Cursor, Antigravity) consume massive context windows with bloated peripheral dependencies, triggering token exhaustion, context drift, and security degradation.\n\n**CTXFW** is an open-core context firewall and Model Context Protocol (MCP) gatekeeper. It combines an **in-memory polyglot AST pruner** with a **deterministic axiomatic intake sieve**:\n\n1. \n**Compacts Peripheral Code (72.4% token reduction)** : Replaces distance-1 and distance-2+ module implementations with clean interface signatures, type definitions, and functional stubs.\n2. \n**Enforces Axiomatic Integrity (ACI** : Rejects ungrounded or deficient architecture briefs missing negative invariants ($\\ge$ 0.9000)$N \\ge 5$ ), bounded variable domains, deterministic state machines, or formal error taxonomies.\n3. \n**Zero Telemetry Egress** : Guaranteed local execution with zero network telemetry leakage on standard operating mode.\n\nCTXFW operates directly at the syntax tree layer using native polyglot grammars:\n\n| Benchmark Dimension | Raw Context Ingestion | CTXFW Topological Compactor | Performance Gain / Impact | \n|---|---|---|---|\n| **Token Consumption** | 100% (Raw Files) | 27.6% (Interface Stubs) | **72.4% Bloat Eliminated** | \n| **Engine Compaction Overhead** | — | Native in-memory parser | **< 5.0 ms** | \n| **Warm Cache Hit Overhead** | — | SQLite WAL semantic cache | **< 0.8 ms** | \n| **Stdio Telemetry Egress** | Unsanitized stdout | Pure isolated JSON-RPC | **Zero Egress (100% Isolated)** | \n| **Axiom Verification Latency** | — | Sieve evaluation | **< 12.0 ms** | \n| **CI/CD Pre-Commit Latency** | — | Headless git sentry | **< 85.0 ms** | \n\nEmpirical context reduction metrics generated via `ctxfw.resolve_context_bundle` running against 16 internal dependencies:\n\n| Dimension | Raw Context Ingestion | CTXFW Topological Sieve | Performance Delta | \n|---|---|---|---|\n| **Total Context Size** | 49,096 tokens | 20,014 tokens | **-59.5% Net Reduction** | \n| **Tokens Eliminated** | 0 tokens | 29,222 tokens | **29,222 bloat tokens pruned** | \n| **Transitive Deps (** | 7,275 tokens | 4,763 tokens | **Up to 91.9% reduction** | \n| **FinOps Cost Impact** | Base Cost | Reduced by $0.0877 USD / prompt | **~$87.70 USD saved per 1K calls** | \n| **AST Compaction Latency** | — | 1,407.96 ms | In-memory Tree-Sitter parsing | \n| **Attestation Integrity** | None | SHA-256 sealed | Strict interface preservation | \n\n**Topological Hierarchy Breakdown:**\n\n- \n**`ctxfw/cli.py`)** : 100% Full Implementation preserved.$D_0$ Target (\n- \n**`gatekeeper.py`, `mcp.py`)** : Implementation truncated to typed stubs ($D_1$ Direct Deps (e.g.`...` ). Token savings:**73% – 86%** .\n- \n**`polyglot.py`)** : Nominal symbols only. Token savings:$D_{2+}$ Transitive Deps (e.g.**91.9%** .\n\nInstall via `pip` or isolated environment manager:\n\n```\npip install ctxfw\n```\n\nOr for global CLI availability using `pipx`:\n\n```\npipx install ctxfw\n```\n\nRegister the stdio server directly in your IDE or client configuration (`claude_desktop_config.json`, Cursor, Windsurf, or Antigravity):\n\n```\n{\n  \"mcpServers\": {\n    \"ctxfw\": {\n      \"command\": \"ctxfw\",\n      \"args\": [\"mcp\"]\n    }\n  }\n}\n```\n\nCTXFW is indexed and verified with Grade A compliance on the official Glama MCP registry:\n\nDirect access to tool inspection, schemas, and live diagnostic telemetry on [Glama](https://glama.ai/mcp/servers/heuristicolab/ctxfw).\n\nValidate local environment readiness, stdio isolation purity, SQLite WAL concurrency, and Tree-Sitter grammars with a single command:\n\n```\nctxfw doctor\n========================================================================\n  CTXFW DOCTOR // HIGH-ASSURANCE HEALTH & ISOLATION DIAGNOSTIC\n========================================================================\n[PASS]   Python Package & sys.path        ctxfw v3.5.0 loaded cleanly.\n[PASS]   MCP stdio Stream Isolation       100% pure JSON-RPC on stdout. Diagnostic logs isolated to stderr.\n[PASS]   Global CLI Executable (PATH)     Binary 'ctxfw' found in PATH.\n[PASS]   Axiomatic Sieve Engine           Evaluation verified (ACI: 1.0000, Invariants: 5).\n[PASS]   SQLite WAL Cache & Concurrency   Journal mode: WAL, Busy timeout: 5000ms.\n[PASS]   Polyglot Tree-Sitter Grammars    Initialized language parsers (typescript, go, java).\n------------------------------------------------------------------------\nOverall Verdict:            [HEALTHY] [ATTESTED] Perimeter defense operational.\n========================================================================\nCTXFW // 72.4% AST Bloat Eliminated. Zero Telemetry Egress.\nNeed team-wide budget circuit breakers or multi-node proxy governance?\nControl Plane & Enterprise Licensing: https://ctxfw.heuristicolab.com\n========================================================================\n```\n\nCTXFW enforces a strict deterministic perimeter dividing probabilistic agent code from the core codebase:\n\n```\nPROBABILISTIC DOMAIN                      DETERMINISTIC PERIMETER\n┌───────────────────────┐                  ┌────────────────────────────────────────┐\n│  Autonomous AI Agent  │                  │             CTXFW ENGINE               │\n│  (Claude / Gemini /   │                  │                                        │\n│   Cursor / Antigravity│                  │  ┌──────────────────────────────────┐  │\n└───────────┬───────────┘                  │  │   Polyglot AST Topological Engine│  │\n            │                              │  │  - Python (ast)                  │  │\n            │  Target Context / Brief      │  │  - TypeScript / Go / Java (CST)  │  │\n            ▼                              │  │  - Multi-Depth Interface Stubs   │  │\n┌───────────────────────┐                  │  └────────────────┬─────────────────┘  │\n│ MCP Stdio Interceptor ├─────────────────►│                   │                    │\n└───────────────────────┘                  │  ┌────────────────┴─────────────────┐  │\n                                           │  │  SQLite WAL High-Concurrency     │  │\n                                           │  │  Semantic Cache (<5ms warm hit)  │  │\n                                           │  └────────────────┬─────────────────┘  │\n                                           │                   ▼                    │\n                                           │         [ ACI >= 0.9000? ]             │\n                                           │          /              \\              │\n                                           │       YES                NO            │\n                                           │        │                  │            │\n                                           │        ▼                  ▼            │\n                                           │ ┌──────────────┐   ┌─────────────────┐ │\n                                           │ │ VERIFIED     │   │ QUARANTINED     │ │\n                                           │ │ SHA-256 Seal │   │ Execution Halt  │ │\n                                           │ └──────┬───────┘   └────────┬────────┘ │\n                                           └────────┼────────────────────┼──────────┘\n                                                    │                    │\n                                                    ▼                    ▼\n                                           [ Code Generation ]   [ Forensic Report ]\n                                           [ & Git Permitted ]   [ Pre-Commit Abort]\n```\n\n1. **Polyglot Tree-Sitter Pruner** :\n  - Compiles topological dependency trees. Distance 0 (target file) is preserved in full; Distance 1 dependencies retain signatures and docstrings while pruning implementation logic; Distance 2+ dependencies are reduced to compact type stubs.\n  - Built-in support for **Python** ,**TypeScript/JavaScript** ,**Go** , and**Java** .\n2. **SQLite WAL High-Concurrency Semantic Cache** :\n  - Atomic multi-process caching configured with Write-Ahead Logging (`PRAGMA journal_mode=WAL` ) and`busy_timeout=5000ms` , delivering sub-millisecond warm cache hits.\n3. Atomic multi-process caching configured with Write-Ahead Logging (\n4. **Axiomatic Sieve Engine** :\n  - Formal specification gatekeeper evaluating requirements against 5 negative invariants (`shall never` ), explicit mathematical bounds, deterministic state machines, and a 4-class error taxonomy.\n5. Formal specification gatekeeper evaluating requirements against 5 negative invariants (\n\nInject perimeter rules, MCP server declarations, and pre-commit sentinels into your workspace:\n\n```\nctxfw init --global\n```\n\nAutomatically configures Google Antigravity, Cursor, and Claude Desktop.\n\n```\nctxfw init --repo .\n```\n\nDeploys `.git/hooks/pre-commit` to prevent uncertified code commits lacking an attested specification brief.\n\nFor distributed engineering teams requiring centralized policy controls:\n\n- **Team-wide LLM budget circuit breakers** : Hard token and dollar thresholds with automatic killswitches.\n- **Multi-node reverse proxy governance** : Centralized firewall gateways supporting OpenAI and Anthropic streaming SSE endpoints.\n- **FinOps Telemetry Ledger** : Aggregate tokens saved, cost elusion analytics, and tamper-evident audit trails.\n\n**Control Plane & Enterprise Licensing:** [https://ctxfw.heuristicolab.com](https://ctxfw.heuristicolab.com)\n\n<sub>ENGINEERED BY HEURISTICO LAB // SKUNK WORKS DIVISION</sub>\n\n<sub>HIGH-ASSURANCE DEFENSE SYSTEMS GROUP</sub>", "url": "https://wpnews.pro/news/ctxfw-in-memory-tree-sitter-ast-compactor-that-cuts-coding-tokens-by-72", "canonical_source": "https://github.com/heuristicolab/ctxfw", "published_at": "2026-09-22 16:34:42+00:00", "updated_at": "2026-09-22 16:54:42.996630+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "ai-tools", "developer-tools", "ai-infrastructure"], "entities": ["CTXFW", "HeuristicoLab", "Model Context Protocol", "Tree-sitter", "Claude", "Gemini", "Cursor", "Glama"], "alternates": {"html": "https://wpnews.pro/news/ctxfw-in-memory-tree-sitter-ast-compactor-that-cuts-coding-tokens-by-72", "markdown": "https://wpnews.pro/news/ctxfw-in-memory-tree-sitter-ast-compactor-that-cuts-coding-tokens-by-72.md", "text": "https://wpnews.pro/news/ctxfw-in-memory-tree-sitter-ast-compactor-that-cuts-coding-tokens-by-72.txt", "jsonld": "https://wpnews.pro/news/ctxfw-in-memory-tree-sitter-ast-compactor-that-cuts-coding-tokens-by-72.jsonld"}}