Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /mcp_server /tools /get_metrics.py
| """ | |
| Get Governance Metrics MCP Tool. | |
| Retrieves real-time governance metrics from ACGS-2. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| from __future__ import annotations | |
| import json | |
| from dataclasses import dataclass | |
| from datetime import UTC, datetime | |
| from typing_extensions import TypedDict | |
| from enhanced_agent_bus.observability.structured_logging import get_logger | |
| class _InternalMetrics(TypedDict): | |
| """type definition for internal metrics.""" | |
| total_requests: int | |
| approved_count: int | |
| denied_count: int | |
| conditional_count: int | |
| escalated_count: int | |
| validation_count: int | |
| violation_count: int | |
| latencies: list[float] | |
| try: | |
| from enhanced_agent_bus._compat.types import JSONDict | |
| except ImportError: | |
| JSONDict = dict # type: ignore[misc,assignment] | |
| from ..protocol.types import ToolDefinition, ToolInputSchema | |
| logger = get_logger(__name__) | |
| class GovernanceMetrics: | |
| """Governance system metrics.""" | |
| total_requests: int | |
| approved_count: int | |
| denied_count: int | |
| conditional_count: int | |
| escalated_count: int | |
| avg_latency_ms: float | |
| p99_latency_ms: float | |
| throughput_rps: float | |
| validation_count: int | |
| violation_count: int | |
| compliance_rate: float | |
| active_principles: int | |
| precedent_count: int | |
| cache_hit_rate: float | |
| system_health: str | |
| constitutional_hash: str | |
| timestamp: str | |
| def to_dict(self) -> JSONDict: | |
| """Convert to dictionary.""" | |
| return { | |
| "requests": { | |
| "total": self.total_requests, | |
| "approved": self.approved_count, | |
| "denied": self.denied_count, | |
| "conditional": self.conditional_count, | |
| "escalated": self.escalated_count, | |
| }, | |
| "performance": { | |
| "avg_latency_ms": self.avg_latency_ms, | |
| "p99_latency_ms": self.p99_latency_ms, | |
| "throughput_rps": self.throughput_rps, | |
| }, | |
| "compliance": { | |
| "validation_count": self.validation_count, | |
| "violation_count": self.violation_count, | |
| "compliance_rate": self.compliance_rate, | |
| }, | |
| "governance": { | |
| "active_principles": self.active_principles, | |
| "precedent_count": self.precedent_count, | |
| }, | |
| "system": { | |
| "cache_hit_rate": self.cache_hit_rate, | |
| "health": self.system_health, | |
| }, | |
| "constitutional_hash": self.constitutional_hash, | |
| "timestamp": self.timestamp, | |
| } | |
| class GetMetricsTool: | |
| """ | |
| MCP Tool for retrieving governance metrics. | |
| Provides real-time visibility into ACGS-2 governance system | |
| performance, compliance rates, and operational metrics. | |
| """ | |
| from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH | |
| def __init__(self, metrics_adapter: object | None = None): | |
| """ | |
| Initialize the metrics tool. | |
| Args: | |
| metrics_adapter: Optional adapter for real metrics collection | |
| """ | |
| self.metrics_adapter = metrics_adapter | |
| self._request_count = 0 | |
| # Internal counters for demonstration | |
| self._internal_metrics: _InternalMetrics = { | |
| "total_requests": 0, | |
| "approved_count": 0, | |
| "denied_count": 0, | |
| "conditional_count": 0, | |
| "escalated_count": 0, | |
| "validation_count": 0, | |
| "violation_count": 0, | |
| "latencies": [], | |
| } | |
| def get_definition(cls) -> ToolDefinition: | |
| """Get the MCP tool definition.""" | |
| return ToolDefinition( | |
| name="get_governance_metrics", | |
| description=( | |
| "Retrieve real-time governance metrics from ACGS-2. " | |
| "Returns request counts, performance metrics, compliance rates, " | |
| "and system health information." | |
| ), | |
| inputSchema=ToolInputSchema( | |
| type="object", | |
| properties={ | |
| "metric_types": { | |
| "type": "array", | |
| "items": { | |
| "type": "string", | |
| "enum": [ | |
| "requests", | |
| "performance", | |
| "compliance", | |
| "governance", | |
| "system", | |
| ], | |
| }, | |
| "description": "Specific metric types to retrieve (default: all)", | |
| }, | |
| "time_range": { | |
| "type": "string", | |
| "description": "Time range for metrics", | |
| "enum": ["1h", "6h", "24h", "7d", "30d"], | |
| "default": "24h", | |
| }, | |
| "include_historical": { | |
| "type": "boolean", | |
| "description": "Include historical trend data", | |
| "default": False, | |
| }, | |
| }, | |
| required=[], | |
| ), | |
| constitutional_required=False, | |
| ) | |
| async def execute(self, arguments: JSONDict) -> JSONDict: | |
| """ | |
| Execute the metrics retrieval. | |
| Args: | |
| arguments: Tool arguments | |
| Returns: | |
| Governance metrics as a dictionary | |
| """ | |
| self._request_count += 1 | |
| metric_types = arguments.get( | |
| "metric_types", ["requests", "performance", "compliance", "governance", "system"] | |
| ) | |
| time_range = arguments.get("time_range", "24h") | |
| include_historical = arguments.get("include_historical", False) | |
| logger.info(f"Retrieving metrics: {metric_types} for {time_range}") | |
| try: | |
| # If we have a metrics adapter, use it | |
| if self.metrics_adapter: | |
| metrics = await self._get_from_adapter(metric_types, time_range) | |
| else: | |
| metrics = self._get_locally() | |
| result = metrics.to_dict() | |
| # Filter to requested metric types | |
| filtered_result = { | |
| k: v | |
| for k, v in result.items() | |
| if k in metric_types or k in ["constitutional_hash", "timestamp"] | |
| } | |
| if include_historical: | |
| filtered_result["historical"] = self._get_historical_trends(time_range) | |
| return { | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": json.dumps(filtered_result, indent=2), | |
| } | |
| ], | |
| "isError": False, | |
| } | |
| except Exception as e: | |
| logger.error(f"Error retrieving metrics: {e}") | |
| return { | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": json.dumps( | |
| { | |
| "error": str(e), | |
| "constitutional_hash": self.CONSTITUTIONAL_HASH, | |
| }, | |
| indent=2, | |
| ), | |
| } | |
| ], | |
| "isError": True, | |
| } | |
| async def _get_from_adapter( | |
| self, | |
| metric_types: list, | |
| time_range: str, | |
| ) -> GovernanceMetrics: | |
| """Get metrics from the adapter.""" | |
| raw_metrics = await self.metrics_adapter.get_metrics( | |
| metric_types=metric_types, | |
| time_range=time_range, | |
| ) | |
| return GovernanceMetrics(**raw_metrics) | |
| def _get_locally(self) -> GovernanceMetrics: | |
| """ | |
| Get metrics from local counters. | |
| Returns demonstration metrics when no adapter is available. | |
| """ | |
| im = self._internal_metrics | |
| validation_count = max(im["validation_count"], 1) | |
| # Calculate average latency | |
| latencies = im["latencies"] if im["latencies"] else [1.5] | |
| avg_latency = sum(latencies) / len(latencies) | |
| sorted_latencies = sorted(latencies) | |
| p99_index = int(len(sorted_latencies) * 0.99) | |
| p99_latency = sorted_latencies[min(p99_index, len(sorted_latencies) - 1)] | |
| # Production-grade target metrics | |
| return GovernanceMetrics( | |
| total_requests=im["total_requests"], | |
| approved_count=im["approved_count"], | |
| denied_count=im["denied_count"], | |
| conditional_count=im["conditional_count"], | |
| escalated_count=im["escalated_count"], | |
| avg_latency_ms=round(avg_latency, 2), | |
| p99_latency_ms=round(p99_latency, 2), | |
| throughput_rps=770.4, # Production target | |
| validation_count=im["validation_count"], | |
| violation_count=im["violation_count"], | |
| compliance_rate=round( | |
| 1.0 - (im["violation_count"] / validation_count) if validation_count > 0 else 1.0, 4 | |
| ), | |
| active_principles=8, | |
| precedent_count=5, | |
| cache_hit_rate=0.95, # Production target | |
| system_health="healthy", | |
| constitutional_hash=self.CONSTITUTIONAL_HASH, | |
| timestamp=datetime.now(UTC).isoformat(), | |
| ) | |
| def _get_historical_trends(self, time_range: str) -> JSONDict: | |
| """Generate historical trend data.""" | |
| # Return sample trend data for demonstration | |
| return { | |
| "time_range": time_range, | |
| "data_points": 24 if time_range == "24h" else 7, | |
| "compliance_trend": "stable", | |
| "latency_trend": "improving", | |
| "throughput_trend": "stable", | |
| } | |
| def record_request( | |
| self, | |
| status: str, | |
| latency_ms: float, | |
| had_violation: bool = False, | |
| ) -> None: | |
| """Record a governance request for metrics.""" | |
| self._internal_metrics["total_requests"] += 1 | |
| self._internal_metrics["validation_count"] += 1 | |
| self._internal_metrics["latencies"].append(latency_ms) | |
| # Keep only last 1000 latencies | |
| if len(self._internal_metrics["latencies"]) > 1000: | |
| self._internal_metrics["latencies"] = self._internal_metrics["latencies"][-1000:] | |
| if status == "approved": | |
| self._internal_metrics["approved_count"] += 1 | |
| elif status == "denied": | |
| self._internal_metrics["denied_count"] += 1 | |
| elif status == "conditional": | |
| self._internal_metrics["conditional_count"] += 1 | |
| elif status == "escalated": | |
| self._internal_metrics["escalated_count"] += 1 | |
| if had_violation: | |
| self._internal_metrics["violation_count"] += 1 | |
| def get_metrics(self) -> JSONDict: | |
| """Get tool metrics.""" | |
| return { | |
| "request_count": self._request_count, | |
| "constitutional_hash": self.CONSTITUTIONAL_HASH, | |
| } | |
Xet Storage Details
- Size:
- 11.1 kB
- Xet hash:
- f19247b060d8adee6940d7335968fe2eb00d85beb068c37d1f85f8e03ee7c4c6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.