feat(governance): 新增報表 runtime 啟動前閘門
This commit is contained in:
@@ -88,6 +88,9 @@ from src.services.ai_agent_redis_dry_run_gate import (
|
||||
from src.services.ai_agent_report_automation_review import (
|
||||
load_latest_ai_agent_report_automation_review,
|
||||
)
|
||||
from src.services.ai_agent_report_runtime_readiness import (
|
||||
load_latest_ai_agent_report_runtime_readiness,
|
||||
)
|
||||
from src.services.ai_agent_report_truth_actionability_review import (
|
||||
load_latest_ai_agent_report_truth_actionability_review,
|
||||
)
|
||||
@@ -911,6 +914,34 @@ async def get_agent_report_automation_review() -> dict[str, Any]:
|
||||
) from exc
|
||||
|
||||
|
||||
@router.get(
|
||||
"/agent-report-runtime-readiness",
|
||||
response_model=dict[str, Any],
|
||||
summary="取得 AI Agent 報表 runtime 啟動前閘門",
|
||||
description=(
|
||||
"讀取最新已提交的 P2-403L 日週月報派送、Telegram Gateway queue、讀報回執、"
|
||||
"AI 讀報後分析、中低風險自動處理、高風險審核與 post-action verifier 啟動前閘門;"
|
||||
"此端點不排程實發、不送 Telegram、不寫 Gateway queue、不啟動 AI runtime worker、"
|
||||
"不啟動中低風險 auto worker、不執行生產優化、不讀 secret、不回傳內部對話內容。"
|
||||
),
|
||||
)
|
||||
async def get_agent_report_runtime_readiness() -> dict[str, Any]:
|
||||
"""Return the latest read-only AI Agent report runtime readiness gate."""
|
||||
try:
|
||||
return await asyncio.to_thread(load_latest_ai_agent_report_runtime_readiness)
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
except (json.JSONDecodeError, ValueError) as exc:
|
||||
logger.error("ai_agent_report_runtime_readiness_invalid", error=str(exc))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="AI Agent 報表 runtime 啟動前閘門無效",
|
||||
) from exc
|
||||
|
||||
|
||||
@router.get(
|
||||
"/agent-owner-approved-fixture-dry-run",
|
||||
response_model=dict[str, Any],
|
||||
|
||||
199
apps/api/src/services/ai_agent_report_runtime_readiness.py
Normal file
199
apps/api/src/services/ai_agent_report_runtime_readiness.py
Normal file
@@ -0,0 +1,199 @@
|
||||
"""
|
||||
AI Agent report runtime readiness snapshot.
|
||||
|
||||
Loads the latest committed P2-403L report delivery, Telegram receipt, AI
|
||||
analysis, and medium / low risk automation readiness gate. This module does
|
||||
not schedule reports, write Telegram Gateway queues, start AI workers, or
|
||||
optimize production.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from src.services.snapshot_paths import default_evaluations_dir
|
||||
|
||||
_DEFAULT_EVALUATIONS_DIR = default_evaluations_dir(Path(__file__))
|
||||
_SNAPSHOT_PATTERN = "ai_agent_report_runtime_readiness_*.json"
|
||||
_SCHEMA_VERSION = "ai_agent_report_runtime_readiness_v1"
|
||||
_RUNTIME_AUTHORITY = "report_runtime_readiness_only_no_live_delivery_or_optimization"
|
||||
|
||||
|
||||
def load_latest_ai_agent_report_runtime_readiness(
|
||||
evaluations_dir: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Load the newest committed AI Agent report runtime readiness snapshot."""
|
||||
directory = evaluations_dir or _DEFAULT_EVALUATIONS_DIR
|
||||
candidates = sorted(directory.glob(_SNAPSHOT_PATTERN))
|
||||
if not candidates:
|
||||
raise FileNotFoundError(f"no AI Agent report runtime readiness snapshots found in {directory}")
|
||||
|
||||
latest = candidates[-1]
|
||||
with latest.open(encoding="utf-8") as handle:
|
||||
payload = json.load(handle)
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError(f"{latest}: expected JSON object")
|
||||
_require_schema(payload, str(latest))
|
||||
_require_activation_boundaries(payload, str(latest))
|
||||
_require_lane_contract(payload, str(latest))
|
||||
_require_policy_contract(payload, str(latest))
|
||||
_require_telegram_contract(payload, str(latest))
|
||||
_require_rollup_consistency(payload, str(latest))
|
||||
return payload
|
||||
|
||||
|
||||
def _require_schema(payload: dict[str, Any], label: str) -> None:
|
||||
if payload.get("schema_version") != _SCHEMA_VERSION:
|
||||
raise ValueError(f"{label}: expected schema_version={_SCHEMA_VERSION}")
|
||||
status = payload.get("program_status") or {}
|
||||
if status.get("read_only_mode") is not True:
|
||||
raise ValueError(f"{label}: program_status.read_only_mode must be true")
|
||||
if status.get("runtime_authority") != _RUNTIME_AUTHORITY:
|
||||
raise ValueError(f"{label}: runtime_authority must remain {_RUNTIME_AUTHORITY}")
|
||||
if status.get("current_task_id") != "P2-403L":
|
||||
raise ValueError(f"{label}: current_task_id must be P2-403L")
|
||||
|
||||
|
||||
def _require_activation_boundaries(payload: dict[str, Any], label: str) -> None:
|
||||
truth = payload.get("activation_truth") or {}
|
||||
ready_flags = {
|
||||
"report_scheduler_contract_ready",
|
||||
"telegram_gateway_queue_contract_ready",
|
||||
"telegram_delivery_receipt_contract_ready",
|
||||
"ai_readback_analysis_contract_ready",
|
||||
"medium_low_auto_guard_contract_ready",
|
||||
"high_risk_approval_gate_contract_ready",
|
||||
}
|
||||
missing_ready = sorted(flag for flag in ready_flags if truth.get(flag) is not True)
|
||||
if missing_ready:
|
||||
raise ValueError(f"{label}: readiness contract flags must remain true: {missing_ready}")
|
||||
|
||||
false_flags = {
|
||||
"live_report_delivery_enabled",
|
||||
"telegram_gateway_queue_write_enabled",
|
||||
"report_read_receipt_write_enabled",
|
||||
"ai_analysis_runtime_enabled",
|
||||
"medium_low_auto_worker_enabled",
|
||||
"production_optimization_enabled",
|
||||
"high_risk_auto_execution_enabled",
|
||||
}
|
||||
unsafe_flags = sorted(flag for flag in false_flags if truth.get(flag) is not False)
|
||||
if unsafe_flags:
|
||||
raise ValueError(f"{label}: live runtime flags must remain false: {unsafe_flags}")
|
||||
|
||||
zero_counts = {
|
||||
"live_report_delivery_count_24h",
|
||||
"telegram_gateway_queue_write_count_24h",
|
||||
"report_read_receipt_count_24h",
|
||||
"ai_analysis_runtime_count_24h",
|
||||
"medium_low_auto_execution_count_24h",
|
||||
"production_optimization_count_24h",
|
||||
"high_risk_auto_execution_count_24h",
|
||||
}
|
||||
non_zero = sorted(key for key in zero_counts if truth.get(key) != 0)
|
||||
if non_zero:
|
||||
raise ValueError(f"{label}: live report runtime counts must remain zero: {non_zero}")
|
||||
|
||||
|
||||
def _require_lane_contract(payload: dict[str, Any], label: str) -> None:
|
||||
lanes = payload.get("runtime_lanes") or []
|
||||
lane_ids = {lane.get("lane_id") for lane in lanes}
|
||||
required_lanes = {
|
||||
"report_scheduler",
|
||||
"telegram_gateway_queue",
|
||||
"telegram_delivery_receipt",
|
||||
"ai_post_report_analysis",
|
||||
"medium_low_auto_guard",
|
||||
"high_risk_approval",
|
||||
"post_action_verifier",
|
||||
}
|
||||
if lane_ids != required_lanes:
|
||||
raise ValueError(f"{label}: runtime lanes must match {sorted(required_lanes)}")
|
||||
|
||||
agents = {lane.get("owner_agent") for lane in lanes}
|
||||
if not {"openclaw", "hermes", "nemotron"}.issubset(agents):
|
||||
raise ValueError(f"{label}: runtime lanes must include OpenClaw, Hermes, and NemoTron ownership")
|
||||
|
||||
live_lanes = sorted(lane.get("lane_id") for lane in lanes if lane.get("current_live_count_24h") != 0)
|
||||
if live_lanes:
|
||||
raise ValueError(f"{label}: lane live counts must remain zero: {live_lanes}")
|
||||
|
||||
|
||||
def _require_policy_contract(payload: dict[str, Any], label: str) -> None:
|
||||
policies = payload.get("automation_policies") or []
|
||||
policy_ids = {policy.get("risk_id") for policy in policies}
|
||||
if policy_ids != {"low", "medium", "high", "critical"}:
|
||||
raise ValueError(f"{label}: automation policies must include low, medium, high, critical")
|
||||
|
||||
for policy in policies:
|
||||
risk_id = policy.get("risk_id")
|
||||
if policy.get("current_execution_enabled") is not False:
|
||||
raise ValueError(f"{label}: policy {risk_id} current_execution_enabled must remain false")
|
||||
if risk_id in {"high", "critical"}:
|
||||
if policy.get("approval_required") is not True:
|
||||
raise ValueError(f"{label}: policy {risk_id} must require approval")
|
||||
if policy.get("auto_allowed_after_guard") is not False:
|
||||
raise ValueError(f"{label}: policy {risk_id} cannot be auto allowed")
|
||||
if risk_id in {"low", "medium"} and policy.get("auto_allowed_after_guard") is not True:
|
||||
raise ValueError(f"{label}: policy {risk_id} must be auto allowed only after guard")
|
||||
|
||||
|
||||
def _require_telegram_contract(payload: dict[str, Any], label: str) -> None:
|
||||
route = payload.get("telegram_route_readiness") or {}
|
||||
if route.get("canonical_room") != "AwoooI SRE 戰情室":
|
||||
raise ValueError(f"{label}: canonical Telegram room must remain AwoooI SRE 戰情室")
|
||||
if route.get("gateway_required") is not True:
|
||||
raise ValueError(f"{label}: Telegram Gateway must be required")
|
||||
|
||||
false_fields = {
|
||||
"direct_bot_api_allowed",
|
||||
"bot_log_out_allowed",
|
||||
"telegram_gateway_queue_write_enabled",
|
||||
"e2e_delivery_verified",
|
||||
"delivery_receipt_write_enabled",
|
||||
}
|
||||
unsafe = sorted(field for field in false_fields if route.get(field) is not False)
|
||||
if unsafe:
|
||||
raise ValueError(f"{label}: Telegram live route fields must remain false: {unsafe}")
|
||||
|
||||
|
||||
def _require_rollup_consistency(payload: dict[str, Any], label: str) -> None:
|
||||
rollups = payload.get("rollups") or {}
|
||||
lanes = payload.get("runtime_lanes") or []
|
||||
cadences = payload.get("report_delivery_cadence_gates") or []
|
||||
decisions = payload.get("operator_decisions") or []
|
||||
policies = payload.get("automation_policies") or []
|
||||
truth = payload.get("activation_truth") or {}
|
||||
|
||||
expected = {
|
||||
"runtime_lane_count": len(lanes),
|
||||
"report_cadence_gate_count": len(cadences),
|
||||
"operator_decision_count": len(decisions),
|
||||
"automation_policy_count": len(policies),
|
||||
"ready_contract_count": len([lane for lane in lanes if lane.get("contract_status") == "ready_for_owner_review"]),
|
||||
"blocked_contract_count": len([lane for lane in lanes if lane.get("contract_status") == "blocked_by_runtime_gate"]),
|
||||
"current_enabled_count": 0,
|
||||
"live_report_delivery_count": truth.get("live_report_delivery_count_24h"),
|
||||
"live_ai_analysis_count": truth.get("ai_analysis_runtime_count_24h"),
|
||||
"live_medium_low_auto_execution_count": truth.get("medium_low_auto_execution_count_24h"),
|
||||
"telegram_gateway_queue_write_count": truth.get("telegram_gateway_queue_write_count_24h"),
|
||||
"high_risk_auto_execution_count": truth.get("high_risk_auto_execution_count_24h"),
|
||||
}
|
||||
mismatched = {
|
||||
key: {"expected": value, "actual": rollups.get(key)}
|
||||
for key, value in expected.items()
|
||||
if rollups.get(key) != value
|
||||
}
|
||||
if mismatched:
|
||||
raise ValueError(f"{label}: rollup counts must match payload sections: {mismatched}")
|
||||
|
||||
approval_required = sorted(
|
||||
decision.get("decision_id")
|
||||
for decision in decisions
|
||||
if decision.get("approval_required") is True
|
||||
)
|
||||
if sorted(rollups.get("approval_required_decision_ids") or []) != approval_required:
|
||||
raise ValueError(f"{label}: approval_required_decision_ids mismatch")
|
||||
107
apps/api/tests/test_ai_agent_report_runtime_readiness.py
Normal file
107
apps/api/tests/test_ai_agent_report_runtime_readiness.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import copy
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from src.services.ai_agent_report_runtime_readiness import (
|
||||
load_latest_ai_agent_report_runtime_readiness,
|
||||
)
|
||||
|
||||
|
||||
def _write_snapshot(tmp_path, payload):
|
||||
path = tmp_path / "ai_agent_report_runtime_readiness_2026-06-12.json"
|
||||
path.write_text(json.dumps(payload), encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def test_load_latest_ai_agent_report_runtime_readiness():
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
|
||||
assert data["schema_version"] == "ai_agent_report_runtime_readiness_v1"
|
||||
assert data["program_status"]["current_task_id"] == "P2-403L"
|
||||
assert data["program_status"]["next_task_id"] == "P2-403M"
|
||||
assert data["program_status"]["overall_completion_percent"] == 86
|
||||
assert data["activation_truth"]["report_scheduler_contract_ready"] is True
|
||||
assert data["activation_truth"]["telegram_gateway_queue_contract_ready"] is True
|
||||
assert data["activation_truth"]["ai_readback_analysis_contract_ready"] is True
|
||||
assert data["activation_truth"]["medium_low_auto_guard_contract_ready"] is True
|
||||
assert data["activation_truth"]["live_report_delivery_enabled"] is False
|
||||
assert data["activation_truth"]["telegram_gateway_queue_write_enabled"] is False
|
||||
assert data["activation_truth"]["medium_low_auto_worker_enabled"] is False
|
||||
assert data["activation_truth"]["production_optimization_count_24h"] == 0
|
||||
assert data["telegram_route_readiness"]["canonical_room"] == "AwoooI SRE 戰情室"
|
||||
assert data["telegram_route_readiness"]["direct_bot_api_allowed"] is False
|
||||
assert data["telegram_route_readiness"]["bot_log_out_allowed"] is False
|
||||
assert data["rollups"]["runtime_lane_count"] == 7
|
||||
assert data["rollups"]["report_cadence_gate_count"] == 3
|
||||
assert data["rollups"]["operator_decision_count"] == 7
|
||||
assert data["rollups"]["automation_policy_count"] == 4
|
||||
assert data["rollups"]["current_enabled_count"] == 0
|
||||
assert data["rollups"]["live_report_delivery_count"] == 0
|
||||
assert data["rollups"]["telegram_gateway_queue_write_count"] == 0
|
||||
assert data["rollups"]["live_medium_low_auto_execution_count"] == 0
|
||||
|
||||
|
||||
def test_rejects_live_report_delivery_enabled(tmp_path):
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
bad = copy.deepcopy(data)
|
||||
bad["activation_truth"]["live_report_delivery_enabled"] = True
|
||||
_write_snapshot(tmp_path, bad)
|
||||
|
||||
with pytest.raises(ValueError, match="live runtime flags"):
|
||||
load_latest_ai_agent_report_runtime_readiness(tmp_path)
|
||||
|
||||
|
||||
def test_rejects_telegram_gateway_queue_write_count(tmp_path):
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
bad = copy.deepcopy(data)
|
||||
bad["activation_truth"]["telegram_gateway_queue_write_count_24h"] = 1
|
||||
bad["rollups"]["telegram_gateway_queue_write_count"] = 1
|
||||
_write_snapshot(tmp_path, bad)
|
||||
|
||||
with pytest.raises(ValueError, match="live report runtime counts"):
|
||||
load_latest_ai_agent_report_runtime_readiness(tmp_path)
|
||||
|
||||
|
||||
def test_rejects_direct_telegram_api(tmp_path):
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
bad = copy.deepcopy(data)
|
||||
bad["telegram_route_readiness"]["direct_bot_api_allowed"] = True
|
||||
_write_snapshot(tmp_path, bad)
|
||||
|
||||
with pytest.raises(ValueError, match="Telegram live route fields"):
|
||||
load_latest_ai_agent_report_runtime_readiness(tmp_path)
|
||||
|
||||
|
||||
def test_rejects_medium_low_policy_without_guard(tmp_path):
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
bad = copy.deepcopy(data)
|
||||
for policy in bad["automation_policies"]:
|
||||
if policy["risk_id"] == "medium":
|
||||
policy["auto_allowed_after_guard"] = False
|
||||
_write_snapshot(tmp_path, bad)
|
||||
|
||||
with pytest.raises(ValueError, match="medium"):
|
||||
load_latest_ai_agent_report_runtime_readiness(tmp_path)
|
||||
|
||||
|
||||
def test_rejects_high_risk_auto_allowed(tmp_path):
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
bad = copy.deepcopy(data)
|
||||
for policy in bad["automation_policies"]:
|
||||
if policy["risk_id"] == "high":
|
||||
policy["auto_allowed_after_guard"] = True
|
||||
_write_snapshot(tmp_path, bad)
|
||||
|
||||
with pytest.raises(ValueError, match="high"):
|
||||
load_latest_ai_agent_report_runtime_readiness(tmp_path)
|
||||
|
||||
|
||||
def test_rejects_rollup_mismatch(tmp_path):
|
||||
data = load_latest_ai_agent_report_runtime_readiness()
|
||||
bad = copy.deepcopy(data)
|
||||
bad["rollups"]["runtime_lane_count"] = 999
|
||||
_write_snapshot(tmp_path, bad)
|
||||
|
||||
with pytest.raises(ValueError, match="rollup counts"):
|
||||
load_latest_ai_agent_report_runtime_readiness(tmp_path)
|
||||
30
apps/api/tests/test_ai_agent_report_runtime_readiness_api.py
Normal file
30
apps/api/tests/test_ai_agent_report_runtime_readiness_api.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from src.main import app
|
||||
|
||||
|
||||
def test_get_ai_agent_report_runtime_readiness_api():
|
||||
client = TestClient(app)
|
||||
response = client.get("/api/v1/agents/agent-report-runtime-readiness")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["schema_version"] == "ai_agent_report_runtime_readiness_v1"
|
||||
assert data["program_status"]["current_task_id"] == "P2-403L"
|
||||
assert data["program_status"]["next_task_id"] == "P2-403M"
|
||||
assert data["program_status"]["overall_completion_percent"] == 86
|
||||
assert data["activation_truth"]["report_scheduler_contract_ready"] is True
|
||||
assert data["activation_truth"]["telegram_gateway_queue_contract_ready"] is True
|
||||
assert data["activation_truth"]["live_report_delivery_enabled"] is False
|
||||
assert data["activation_truth"]["telegram_gateway_queue_write_enabled"] is False
|
||||
assert data["activation_truth"]["medium_low_auto_worker_enabled"] is False
|
||||
assert data["activation_truth"]["production_optimization_count_24h"] == 0
|
||||
assert data["telegram_route_readiness"]["canonical_room"] == "AwoooI SRE 戰情室"
|
||||
assert data["telegram_route_readiness"]["direct_bot_api_allowed"] is False
|
||||
assert data["telegram_route_readiness"]["bot_log_out_allowed"] is False
|
||||
assert data["rollups"]["runtime_lane_count"] == 7
|
||||
assert data["rollups"]["operator_decision_count"] == 7
|
||||
assert data["rollups"]["current_enabled_count"] == 0
|
||||
assert data["rollups"]["live_report_delivery_count"] == 0
|
||||
assert data["rollups"]["telegram_gateway_queue_write_count"] == 0
|
||||
assert data["rollups"]["live_medium_low_auto_execution_count"] == 0
|
||||
Reference in New Issue
Block a user