|
| 1 | +"""Stateless contract evaluation service (development-time playground).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import hashlib |
| 6 | +import time |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from edictum import Edictum, EvaluationResult, Principal |
| 10 | + |
| 11 | +from edictum_server.schemas.evaluate import ( |
| 12 | + ContractEvaluation, |
| 13 | + EvaluateResponse, |
| 14 | + PrincipalInput, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def _build_principal(inp: PrincipalInput | None) -> Principal | None: |
| 19 | + """Convert API input to an edictum Principal, or None.""" |
| 20 | + if inp is None: |
| 21 | + return None |
| 22 | + return Principal( |
| 23 | + user_id=inp.user_id, |
| 24 | + role=inp.role, |
| 25 | + claims=inp.claims or {}, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def _map_result(result: EvaluationResult, mode: str, yaml_hash: str) -> EvaluateResponse: |
| 30 | + """Map an edictum EvaluationResult to the API response schema.""" |
| 31 | + contracts = [ |
| 32 | + ContractEvaluation( |
| 33 | + id=cr.contract_id, |
| 34 | + type=cr.contract_type, |
| 35 | + matched=cr.passed, |
| 36 | + effect=cr.effect, |
| 37 | + message=cr.message, |
| 38 | + observed=cr.observed, |
| 39 | + tags=list(cr.tags), |
| 40 | + ) |
| 41 | + for cr in result.contracts |
| 42 | + ] |
| 43 | + |
| 44 | + # The deciding contract is the first failing non-observed contract |
| 45 | + deciding: str | None = None |
| 46 | + for cr in result.contracts: |
| 47 | + if not cr.passed and not cr.observed: |
| 48 | + deciding = cr.contract_id |
| 49 | + break |
| 50 | + |
| 51 | + return EvaluateResponse( |
| 52 | + verdict=result.verdict, |
| 53 | + mode=mode, |
| 54 | + contracts_evaluated=contracts, |
| 55 | + deciding_contract=deciding, |
| 56 | + policy_version=yaml_hash[:12], |
| 57 | + evaluation_time_ms=0.0, # overwritten by caller |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def evaluate_contracts( |
| 62 | + *, |
| 63 | + yaml_content: str, |
| 64 | + tool_name: str, |
| 65 | + tool_args: dict[str, Any], |
| 66 | + environment: str = "production", |
| 67 | + principal_input: PrincipalInput | None = None, |
| 68 | +) -> EvaluateResponse: |
| 69 | + """Evaluate a tool call against YAML contracts. |
| 70 | +
|
| 71 | + This is a stateless, synchronous operation for the dashboard playground. |
| 72 | + It never persists data or touches the database. |
| 73 | +
|
| 74 | + Args: |
| 75 | + yaml_content: Raw YAML contract bundle. |
| 76 | + tool_name: Tool name to evaluate. |
| 77 | + tool_args: Arguments for the tool call. |
| 78 | + environment: Environment context (default "production"). |
| 79 | + principal_input: Optional principal identity. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + EvaluateResponse with verdict, matched contracts, timing. |
| 83 | +
|
| 84 | + Raises: |
| 85 | + ValueError: If the YAML is invalid or cannot be parsed as contracts. |
| 86 | + """ |
| 87 | + principal = _build_principal(principal_input) |
| 88 | + yaml_hash = hashlib.sha256(yaml_content.encode("utf-8")).hexdigest() |
| 89 | + |
| 90 | + start = time.monotonic() |
| 91 | + |
| 92 | + try: |
| 93 | + edictum_instance = Edictum.from_yaml_string( |
| 94 | + yaml_content, |
| 95 | + environment=environment, |
| 96 | + ) |
| 97 | + except Exception as exc: |
| 98 | + raise ValueError(f"Invalid contract YAML: {exc}") from exc |
| 99 | + |
| 100 | + result: EvaluationResult = edictum_instance.evaluate( |
| 101 | + tool_name, |
| 102 | + tool_args, |
| 103 | + principal=principal, |
| 104 | + ) |
| 105 | + |
| 106 | + elapsed_ms = (time.monotonic() - start) * 1000 |
| 107 | + |
| 108 | + # Determine mode from the edictum instance |
| 109 | + mode = getattr(edictum_instance, "mode", "enforce") or "enforce" |
| 110 | + |
| 111 | + response = _map_result(result, mode, yaml_hash) |
| 112 | + response.evaluation_time_ms = round(elapsed_ms, 2) |
| 113 | + return response |
0 commit comments