mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-07-30 18:56:16 +00:00
Last-resort multi-model panel via OpenRouter Fusion. Only invoked when all other approaches (local model, single frontier, existing skills) have failed on the same problem. Architecture: - Problem classification (lean_proof, code_bug, architecture, math_novel, infra_debug) - Per-type model panel selection (Opus + DeepSeek + Gemini for proofs) - Context assembly (files, build logs, AGENTS.md constraints) - OpenRouter Fusion call (parallel panel + judge synthesis) - Structured output (diagnosis, solution, alternatives, risks, cost) Safety: - Explicit 'break glass:' trigger required - Cost gate: /usr/bin/bash.30-.50 per invocation, .00/session cap - Rate limit: 3 invocations per session - Audit trail via ContextStream Files: - 4-Infrastructure/shim/break_glass_mcp.py (MCP server) - 6-Documentation/docs/specs/break_glass_mcp_agent.md (design spec) - .opencode/skills/break-glass/SKILL.md (skill definition) - .mcp.json (server registration)
308 lines
9.4 KiB
Python
308 lines
9.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
break_glass_mcp.py — Last-resort multi-model panel via OpenRouter Fusion.
|
|
|
|
Wraps OpenRouter Fusion as an MCP tool. Only invoked when all other
|
|
approaches have failed on a novel problem.
|
|
|
|
Usage:
|
|
python3 break_glass_mcp.py # starts MCP server on stdio
|
|
|
|
Requires: OPENROUTER_API_KEY env var
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
try:
|
|
import httpx
|
|
except ImportError:
|
|
httpx = None
|
|
|
|
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")
|
|
OPENROUTER_BASE = "https://openrouter.ai/api/v1"
|
|
|
|
# Problem type → model panel configuration
|
|
PANELS = {
|
|
"lean_proof": {
|
|
"analysis_models": [
|
|
"anthropic/claude-opus-4.8",
|
|
"deepseek/deepseek-v4-pro",
|
|
"google/gemini-3.1-pro-preview",
|
|
],
|
|
"judge": "anthropic/claude-opus-4.8",
|
|
"budget": 1.00,
|
|
},
|
|
"code_bug": {
|
|
"analysis_models": [
|
|
"openai/gpt-5.5",
|
|
"anthropic/claude-opus-4.8",
|
|
"moonshotai/kimi-k2.6",
|
|
],
|
|
"judge": "anthropic/claude-opus-4.8",
|
|
"budget": 0.75,
|
|
},
|
|
"architecture": {
|
|
"analysis_models": [
|
|
"anthropic/claude-opus-4.8",
|
|
"openai/gpt-5.5",
|
|
"google/gemini-3.1-pro-preview",
|
|
],
|
|
"judge": "anthropic/claude-opus-4.8",
|
|
"budget": 1.50,
|
|
},
|
|
"math_novel": {
|
|
"analysis_models": [
|
|
"deepseek/deepseek-v4-pro",
|
|
"anthropic/claude-opus-4.8",
|
|
"google/gemini-3.1-pro-preview",
|
|
],
|
|
"judge": "anthropic/claude-opus-4.8",
|
|
"budget": 1.00,
|
|
},
|
|
"infra_debug": {
|
|
"analysis_models": [
|
|
"openai/gpt-5.5",
|
|
"anthropic/claude-opus-4.8",
|
|
"deepseek/deepseek-v4-pro",
|
|
],
|
|
"judge": "anthropic/claude-opus-4.8",
|
|
"budget": 0.75,
|
|
},
|
|
}
|
|
|
|
SYSTEM_PROMPT = """You are a specialist agent assembled to attack a specific
|
|
novel problem that has resisted all常规 approaches. You have access to:
|
|
|
|
1. The full codebase context (provided below)
|
|
2. The project's operating constraints
|
|
3. The specific problem statement
|
|
4. Previous attempts and their failure modes
|
|
|
|
YOUR TASK:
|
|
- Diagnose the root cause
|
|
- Propose a concrete solution with exact file paths and code
|
|
- If the problem is undecidable, explain why and propose what would be needed
|
|
|
|
CONSTRAINTS:
|
|
- Lean is the source of truth for formal claims
|
|
- No Float in compute paths (use Q0_16 or Q16_16)
|
|
- No sorry in committed code unless with TODO(lean-port) + human sign-off
|
|
- All new formal work goes to SilverSight (not Research Stack)
|
|
|
|
OUTPUT FORMAT (JSON):
|
|
{
|
|
"diagnosis": "...",
|
|
"solution": {
|
|
"approach": "...",
|
|
"confidence": 0.0-1.0,
|
|
"files_to_change": ["..."],
|
|
"code_snippets": {"file": "code"}
|
|
},
|
|
"alternatives": ["..."],
|
|
"risks": ["..."],
|
|
"cost_estimate": "$X.XX"
|
|
}"""
|
|
|
|
|
|
def classify_problem(problem: str) -> str:
|
|
"""Auto-classify problem type from text."""
|
|
lower = problem.lower()
|
|
if any(w in lower for w in ["sorry", "lean", "theorem", "proof", "lake build"]):
|
|
return "lean_proof"
|
|
if any(w in lower for w in ["architecture", "design", "refactor", "structure"]):
|
|
return "architecture"
|
|
if any(w in lower for w in ["math", "formula", "eigenvalue", "convergence"]):
|
|
return "math_novel"
|
|
if any(w in lower for w in ["infra", "deploy", "k3s", "docker", "ssh"]):
|
|
return "infra_debug"
|
|
return "code_bug"
|
|
|
|
|
|
def call_fusion(problem: str, context: str, problem_type: str,
|
|
budget: float = 1.00) -> dict:
|
|
"""Call OpenRouter Fusion with the assembled context."""
|
|
if not OPENROUTER_API_KEY:
|
|
return {"error": "OPENROUTER_API_KEY not set"}
|
|
if httpx is None:
|
|
return {"error": "httpx not installed (pip install httpx)"}
|
|
|
|
panel = PANELS.get(problem_type, PANELS["code_bug"])
|
|
|
|
messages = [
|
|
{"role": "system", "content": SYSTEM_PROMPT},
|
|
{"role": "user", "content": f"""## Problem Statement
|
|
{problem}
|
|
|
|
## Codebase Context
|
|
{context}
|
|
|
|
## Problem Type
|
|
{problem_type}
|
|
|
|
Please diagnose and propose a solution."""},
|
|
]
|
|
|
|
payload = {
|
|
"model": "openrouter/fusion",
|
|
"messages": messages,
|
|
"plugins": [{
|
|
"id": "fusion",
|
|
"model": panel["judge"],
|
|
"analysis_models": panel["analysis_models"],
|
|
}],
|
|
"max_tokens": 8192,
|
|
}
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
"HTTP-Referer": "https://researchstack.info",
|
|
"X-Title": "break-glass-agent",
|
|
}
|
|
|
|
try:
|
|
with httpx.Client(timeout=120) as client:
|
|
resp = client.post(
|
|
f"{OPENROUTER_BASE}/chat/completions",
|
|
json=payload,
|
|
headers=headers,
|
|
)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
|
|
# Extract cost
|
|
usage = data.get("usage", {})
|
|
cost = usage.get("total_cost", 0)
|
|
|
|
# Extract response
|
|
content = data["choices"][0]["message"]["content"]
|
|
|
|
return {
|
|
"response": content,
|
|
"cost": cost,
|
|
"model": data.get("model", "openrouter/fusion"),
|
|
"usage": usage,
|
|
}
|
|
except Exception as e:
|
|
return {"error": str(e)}
|
|
|
|
|
|
def handle_tool_call(tool_name: str, arguments: dict) -> dict:
|
|
"""Handle an MCP tool call."""
|
|
if tool_name != "break_glass":
|
|
return {"error": f"Unknown tool: {tool_name}"}
|
|
|
|
problem = arguments.get("problem", "")
|
|
problem_type = arguments.get("problem_type") or classify_problem(problem)
|
|
budget = arguments.get("budget", 1.00)
|
|
context_files = arguments.get("context_files", [])
|
|
context = arguments.get("context", "")
|
|
|
|
# Cost gate
|
|
panel = PANELS.get(problem_type, PANELS["code_bug"])
|
|
if budget > panel["budget"] * 3:
|
|
return {
|
|
"error": f"Budget ${budget:.2f} exceeds threshold ${panel['budget'] * 3:.2f}. "
|
|
f"Escalate to human.",
|
|
"problem_type": problem_type,
|
|
}
|
|
|
|
result = call_fusion(problem, context, problem_type, budget)
|
|
|
|
return {
|
|
"problem_type": problem_type,
|
|
"panel": [panel["judge"]] + panel["analysis_models"],
|
|
"result": result,
|
|
"timestamp": datetime.utcnow().isoformat(),
|
|
}
|
|
|
|
|
|
def mcp_server():
|
|
"""Simple MCP server on stdio (JSON-RPC)."""
|
|
for line in sys.stdin:
|
|
try:
|
|
req = json.loads(line.strip())
|
|
except json.JSONDecodeError:
|
|
continue
|
|
|
|
method = req.get("method", "")
|
|
req_id = req.get("id")
|
|
params = req.get("params", {})
|
|
|
|
if method == "initialize":
|
|
resp = {
|
|
"jsonrpc": "2.0",
|
|
"id": req_id,
|
|
"result": {
|
|
"protocolVersion": "2024-11-05",
|
|
"capabilities": {"tools": {}},
|
|
"serverInfo": {
|
|
"name": "break-glass",
|
|
"version": "0.1.0",
|
|
},
|
|
},
|
|
}
|
|
elif method == "tools/list":
|
|
resp = {
|
|
"jsonrpc": "2.0",
|
|
"id": req_id,
|
|
"result": {
|
|
"tools": [{
|
|
"name": "break_glass",
|
|
"description": (
|
|
"Last-resort multi-model panel for novel problems. "
|
|
"Costs 3-5x a single model call. "
|
|
"Requires explicit 'break glass:' trigger."
|
|
),
|
|
"inputSchema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"problem": {
|
|
"type": "string",
|
|
"description": "The specific problem statement",
|
|
},
|
|
"context": {
|
|
"type": "string",
|
|
"description": "Relevant codebase context",
|
|
},
|
|
"problem_type": {
|
|
"type": "string",
|
|
"enum": list(PANELS.keys()),
|
|
"description": "Problem classification (auto-detected if omitted)",
|
|
},
|
|
"budget": {
|
|
"type": "number",
|
|
"description": "Maximum cost in USD (default: 1.00)",
|
|
},
|
|
},
|
|
"required": ["problem"],
|
|
},
|
|
}],
|
|
},
|
|
}
|
|
elif method == "tools/call":
|
|
tool_name = params.get("name", "")
|
|
arguments = params.get("arguments", {})
|
|
result = handle_tool_call(tool_name, arguments)
|
|
resp = {
|
|
"jsonrpc": "2.0",
|
|
"id": req_id,
|
|
"result": {
|
|
"content": [{"type": "text", "text": json.dumps(result, indent=2)}],
|
|
},
|
|
}
|
|
else:
|
|
resp = {
|
|
"jsonrpc": "2.0",
|
|
"id": req_id,
|
|
"error": {"code": -32601, "message": f"Method not found: {method}"},
|
|
}
|
|
|
|
print(json.dumps(resp), flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
mcp_server()
|