Add multi-agent API routes: iterative pipeline + behavior extraction + complexity + security scan
Browse files
backend/app/api/multi_agent_routes.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
# ═══ MULTI-AGENT ITERATIVE PIPELINE (Research-Grade Novelty) ═══
|
| 4 |
+
|
| 5 |
+
class MultiAgentRequest(BaseModel):
|
| 6 |
+
source_code: Optional[str] = None
|
| 7 |
+
requirements: Optional[str] = None
|
| 8 |
+
openapi_spec: Optional[Dict[str, Any]] = None
|
| 9 |
+
framework: str = Field(default="pytest")
|
| 10 |
+
language: str = Field(default="python")
|
| 11 |
+
test_types: Optional[List[str]] = Field(default=["unit", "integration", "edge_case", "security"])
|
| 12 |
+
max_iterations: int = Field(default=2, ge=1, le=5)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@router.post("/generate/multi-agent", summary="🧠 Multi-Agent Iterative Pipeline (Research-Grade)",
|
| 16 |
+
description="5-agent system: Analyze → Generate → Validate → Refine → Map Coverage. Inspired by MuTAP (arxiv:2308.16557).")
|
| 17 |
+
async def multi_agent_generate(req: MultiAgentRequest):
|
| 18 |
+
"""
|
| 19 |
+
RESEARCH-GRADE: Multi-agent iterative test refinement.
|
| 20 |
+
|
| 21 |
+
Unlike single-shot generation, this:
|
| 22 |
+
1. Extracts testable BEHAVIORS from input
|
| 23 |
+
2. Generates initial tests
|
| 24 |
+
3. Scores test quality (A-D)
|
| 25 |
+
4. If quality < B: iteratively refines using mutation feedback
|
| 26 |
+
5. Maps which behaviors are tested vs untested (Behavior Coverage)
|
| 27 |
+
|
| 28 |
+
Returns: tests + quality score + behavior coverage map + refinement history
|
| 29 |
+
"""
|
| 30 |
+
from app.services.multi_agent_engine import run_multi_agent_pipeline
|
| 31 |
+
try:
|
| 32 |
+
result = await run_multi_agent_pipeline(
|
| 33 |
+
source_code=req.source_code or "",
|
| 34 |
+
requirements=req.requirements or "",
|
| 35 |
+
api_spec=req.openapi_spec,
|
| 36 |
+
framework=req.framework,
|
| 37 |
+
language=req.language,
|
| 38 |
+
test_types=req.test_types,
|
| 39 |
+
max_iterations=req.max_iterations,
|
| 40 |
+
)
|
| 41 |
+
return result
|
| 42 |
+
except Exception as e:
|
| 43 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@router.post("/analyze/behaviors", summary="Extract testable behaviors from input")
|
| 47 |
+
async def extract_behaviors_endpoint(req: MultiAgentRequest):
|
| 48 |
+
"""Extract all testable behaviors without generating tests — useful for planning."""
|
| 49 |
+
from app.services.multi_agent_engine import extract_behaviors
|
| 50 |
+
behaviors = extract_behaviors(req.source_code or "", req.requirements or "", req.openapi_spec)
|
| 51 |
+
return {
|
| 52 |
+
"total_behaviors": len(behaviors),
|
| 53 |
+
"behaviors": [{"id": b.id, "description": b.description, "category": b.category, "priority": b.priority, "source": b.source} for b in behaviors],
|
| 54 |
+
"by_category": {cat: len([b for b in behaviors if b.category == cat]) for cat in set(b.category for b in behaviors)},
|
| 55 |
+
"by_priority": {pri: len([b for b in behaviors if b.priority == pri]) for pri in set(b.priority for b in behaviors)},
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@router.post("/analyze/complexity", summary="Analyze code complexity and testing priority")
|
| 60 |
+
async def complexity_endpoint(source_code: str = "", language: str = "python"):
|
| 61 |
+
"""AST-based cyclomatic complexity analysis — identifies which functions need the most tests."""
|
| 62 |
+
from app.services.novelty_features import analyze_code_complexity
|
| 63 |
+
return analyze_code_complexity(source_code, language)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@router.post("/analyze/security", summary="OWASP-style API security scan")
|
| 67 |
+
async def security_scan_endpoint(openapi_spec: Dict[str, Any] = {}):
|
| 68 |
+
"""Scan API spec for security vulnerabilities that need testing."""
|
| 69 |
+
from app.services.novelty_features import scan_api_security
|
| 70 |
+
return scan_api_security(openapi_spec)
|