-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate.py
More file actions
65 lines (55 loc) · 2.25 KB
/
evaluate.py
File metadata and controls
65 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Evaluate (playground) endpoint — stateless contract evaluation."""
from __future__ import annotations
import asyncio
from fastapi import APIRouter, Depends, HTTPException
from edictum_server.auth.dependencies import (
AuthContext,
require_dashboard_auth,
)
from edictum_server.schemas.evaluate import EvaluateRequest, EvaluateResponse
from edictum_server.services.evaluate_service import evaluate_contracts
router = APIRouter(prefix="/api/v1/bundles", tags=["evaluate"])
# Maximum time allowed for a single evaluation (seconds).
_EVALUATE_TIMEOUT_SECONDS = 5.0
# Cap concurrent evaluations so timed-out threads (which can't be killed)
# don't exhaust the default ThreadPoolExecutor.
_EVALUATE_SEMAPHORE = asyncio.Semaphore(4)
@router.post("/evaluate", response_model=EvaluateResponse)
async def evaluate(
body: EvaluateRequest,
_auth: AuthContext = Depends(require_dashboard_auth),
) -> EvaluateResponse:
"""Evaluate a tool call against YAML contracts (dashboard playground).
This is a development-time endpoint for testing contracts in the dashboard.
It is never called by agents during production execution.
Evaluation runs in a thread with a timeout to prevent DoS via complex YAML.
Concurrent evaluations are capped to prevent thread pool exhaustion.
"""
try:
await asyncio.wait_for(_EVALUATE_SEMAPHORE.acquire(), timeout=1.0)
except TimeoutError:
raise HTTPException(
status_code=429,
detail="Too many concurrent evaluations. Try again shortly.",
)
try:
return await asyncio.wait_for(
asyncio.to_thread(
evaluate_contracts,
yaml_content=body.yaml_content,
tool_name=body.tool_name,
tool_args=body.tool_args,
environment=body.environment,
principal_input=body.principal,
),
timeout=_EVALUATE_TIMEOUT_SECONDS,
)
except TimeoutError:
raise HTTPException(
status_code=422,
detail="Evaluation timed out — contract bundle may be too complex.",
)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
finally:
_EVALUATE_SEMAPHORE.release()