idea15-edgemind-verifiable-.../edgemind/cli.py

61 lines
2.0 KiB
Python

"""Small CLI helpers to exercise sidecar and safety primitives.
This module provides small, well-tested functions used by unit tests and
simple scripts to exercise core runtime pieces without pulling in a full
CLI dependency. The functions are intentionally thin wrappers around
existing primitives in edgemind.sidecar and edgemind.safety so tests can
verify integration points.
"""
from __future__ import annotations
import json
from typing import Any, Dict, List, Optional
from .sidecar import AuditLogStore, merge_plan_deltas
from .safety import check_contracts
_STORE_CACHE: Dict[str, AuditLogStore] = {}
def _get_store(db_path: str) -> AuditLogStore:
# Reuse store instances per db_path to allow in-memory DBs to be shared
if db_path not in _STORE_CACHE:
_STORE_CACHE[db_path] = AuditLogStore(db_path)
return _STORE_CACHE[db_path]
def append_audit(db_path: str, actor: str, action: str, metadata: Optional[Dict[str, Any]] = None) -> int:
"""Append an audit entry to the SQLite store and return the row id.
Uses a simple per-db_path cache so ":memory:" usages in tests share the same
connection instance.
"""
store = _get_store(db_path)
return store.append(actor, action, metadata)
def list_audit(db_path: str) -> List[Dict[str, Any]]:
"""Return all audit entries as a list of dicts."""
store = _get_store(db_path)
return store.iter_entries()
def verify_audit(db_path: str) -> bool:
"""Verify the integrity of the audit chain."""
store = _get_store(db_path)
return store.verify_chain()
def merge_deltas(deltas: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Merge plan deltas using the sidecar helper."""
return merge_plan_deltas(deltas)
def evaluate_contracts(contracts: Dict[str, Dict[str, Any]], state: Dict[str, Any]):
"""Evaluate a dict of contracts against a state mapping and return results.
Returns the same tuple list as edgemind.safety.check_contracts: [(id, passed, fail_safe), ...]
"""
return check_contracts(contracts, state)