Metadata-Version: 2.4
Name: cachesurgeon
Version: 0.1.1
Summary: CacheSurgeon — Active AI Agent Prefix-Cache Surgeon, Cache TTL Metadata Tracker, Reversible Secret Vault, and Tool-Call Firewall.
Author-email: Jonathan Sumpter <licenses@cachesurgeon.com>
License-Expression: MIT
Project-URL: Homepage, https://cachesurgeon.com
Project-URL: Repository, https://github.com/jsumpter415/cachesurgeon
Project-URL: Issues, https://github.com/jsumpter415/cachesurgeon/issues
Keywords: ai-agents,prompt-caching,llm-proxy,anthropic,openai,ollama,mcp,security,finops
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Environment :: Web Environment
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.115.0
Requires-Dist: uvicorn>=0.32.0
Requires-Dist: httpx>=0.27.0
Requires-Dist: pydantic>=2.7.0
Requires-Dist: cryptography>=42.0.0
Requires-Dist: rich>=13.9.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Dynamic: license-file

# CacheSurgeon

**Active Prefix-Cache Surgeon, Cache TTL Metadata Tracker, Reversible Secret Vault, and Tool-Call Firewall for Autonomous AI Agents.**

> Stop paying 10× for broken prompt caches, runaway coding-agent loops, and accidental `.env` secret leaks.

---

## Why CacheSurgeon Exists

Passive LLM gateways (LiteLLM, Portkey, Bifrost) forward broken prompts untouched. Autonomous coding agents (**Codex**, **Claude Code**, **Cursor**, **Horizon Zero**, **OpenHands**) resend 50K–200K tokens on every turn—meaning a single reordered tool JSON key or per-second timestamp in line 1 of the system prompt destroys **100% of prompt caching**.

CacheSurgeon operates **in flight** in `< 2 ms`:

| Stage | What It Does | Real Impact |
|---|---|---|
| **01. Prefix-Cache Surgeon + X-Ray** | Deterministically sorts tool arrays & JSON keys, relocates volatile system timestamps/trace IDs to the trailing user turn, and injects Anthropic `cache_control` breakpoints. | **40–87%** cloud bill cut; **0% → 98.7%** cache hit & **64×** speedup verified on local MLX. |
| **02. Cache TTL Metadata Tracker** | Observes session TTL metadata locally without running idle eligibility checks or sending background keep-alive pings in `0.1.0`. | From 0.1.1, eligible Anthropic sessions can show projected 5-minute vs. 1-hour cache costs in `/api/metrics` (`ttl_analysis`). Projections are not measured savings. No background keep-alive pings are sent. |
| **03. Reversible Secret & PII Vault** | Scrubs AWS, OpenAI, Stripe, Anthropic, Google, GitHub, Slack, JWT, PEM, DB URLs, and PII into opaque tokens (`__CS_AWS_ACCESS_KEY_01__`) before cloud egress. | Zero raw credentials leave your VPC or touch SQLite audit logs. |
| **04. Tool-Call / MCP Firewall** | Inspects model-emitted OpenAI `tool_calls` and Anthropic `tool_use` blocks *before* local execution, blocking `rm -rf`, `DROP TABLE`, `curl \| bash`, SSH key reads, and force-pushes to `main`. | Pre-execution CISO guardrail with structured compliance logs. |
| **05. Loop Breaker & Safe Failover** | Fingerprints repeating failing tool loops (`warn` / autonomous `pivot` / HTTP `429` hard `trip`), enforces hard USD session budgets, and defaults to same-family safe downgrade on upstream `429`. | Eliminates OWASP LLM10 Denial-of-Wallet surprises. |

---

## 30-Second Quickstart

```bash
pip install cachesurgeon
cachesurgeon start --port 8787
```

Open the live Power-Grid Telemetry Cockpit at `http://127.0.0.1:8787` (click **Simulate Agent Attack & Leak** to see all 5 stages fire immediately without needing an API key).

### Connect Any Coding Agent (1 Env Var)

```bash
# OpenAI / Codex / Cursor / Product Behavior Investigator
export OPENAI_BASE_URL="http://127.0.0.1:8787/v1"

# Anthropic / Claude Code
export ANTHROPIC_BASE_URL="http://127.0.0.1:8787"

# Local Ollama (accelerated through CacheSurgeon)
export OLLAMA_BASE_URL="http://127.0.0.1:8787/v1"
```

### Deploy On-Premise via Docker

```bash
docker compose up -d
```

### Export Expense-Ready Executive ROI Receipt

```bash
cachesurgeon receipt --out monthly-roi.html
```

---

## Public Landing Site (`site/`)

The static marketing & interactive surgical operating theater site lives in `site/` (zero build step, zero framework bloat) and ships with `.github/workflows/pages.yml` for 1-click **GitHub Pages** deployment (or drop straight onto Cloudflare Pages / Netlify).

## Offline Ed25519 Licensing (Air-Gapped Friendly)

```bash
# Vendor: generate signing keypair once
cachesurgeon license keygen

# Vendor: mint a signed customer key (no DRM phone-home required)
cachesurgeon license issue --private-key "<PRIV_B64>" --tier team --org "Acme Corp" --seats 25
```

## Recommended Companion: Product Behavior Investigator

Pair **CacheSurgeon** with our open-source Antigravity & Codex forensic debugging plugin, **[Product Behavior Investigator](https://github.com/jsumpter415/product-behavior-investigator)** (`v1.1.0`):
- **Product Behavior Investigator** traces user-visible failures across client, edge, API, and rollout cohorts to the first broken contract with a 6-heading Evidence Report.
- **CacheSurgeon** runs underneath it to cut multi-turn investigation token bills 40–87%, scrub `.env`/log secrets before cloud egress, and block destructive tool calls.

## Development & Tests

```bash
pip install -e ".[dev]"
pytest -v
```
