"""Decision-log service (P3 deepening, S2). Pulled out of ``web/routers/intelligent_evals.py`` so the router only handles HTTP validation and error translation. The ORM writes and reads now live here. """ from typing import Any from sqlmodel import Session, select from agenteval.storage.db import IntelligentEvalDB, IntelligentEvalDecisionLogDB def create_decision_log( eval_id: str, decision_type: str, reason: str, cron_id: str, context: dict[str, Any], session: Session, ) -> dict: """Create a decision log entry. Raises ``LookupError`` if eval not found.""" eval_db = session.get(IntelligentEvalDB, eval_id) if eval_db is None: raise LookupError(f"intelligent eval {eval_id} not found") log = IntelligentEvalDecisionLogDB( eval_id=eval_id, decision_type=decision_type, reason=reason, cron_id=cron_id, ) log.set_context(context) session.add(log) session.commit() session.refresh(log) return { "id": log.id, "eval_id": log.eval_id, "decision_type": log.decision_type, "reason": log.reason, "context": log.get_context(), "cron_id": log.cron_id, "created_at": log.created_at.isoformat() if log.created_at else None, } def list_decision_logs(eval_id: str, session: Session) -> list[dict]: """List decision logs for an eval. Raises ``LookupError`` if eval not found.""" eval_db = session.get(IntelligentEvalDB, eval_id) if eval_db is None: raise LookupError(f"intelligent eval {eval_id} not found") logs = session.exec( select(IntelligentEvalDecisionLogDB) .where(IntelligentEvalDecisionLogDB.eval_id == eval_id) .order_by(IntelligentEvalDecisionLogDB.created_at.desc()) ).all() return [ { "id": log.id, "eval_id": log.eval_id, "decision_type": log.decision_type, "reason": log.reason, "context": log.get_context(), "cron_id": log.cron_id, "created_at": log.created_at.isoformat() if log.created_at else None, } for log in logs ]