feat(governance): 新增候選操作 dry-run 證據
All checks were successful
Code Review / ai-code-review (push) Successful in 16s
CD Pipeline / tests (push) Successful in 1m33s
CD Pipeline / build-and-deploy (push) Successful in 8m17s
CD Pipeline / post-deploy-checks (push) Successful in 2m22s

This commit is contained in:
Your Name
2026-06-12 15:45:52 +08:00
parent c4ceb30511
commit 9dbbc579d2
14 changed files with 1927 additions and 14 deletions

View File

@@ -0,0 +1,130 @@
import copy
import json
import pytest
from src.services.ai_agent_candidate_operation_dry_run_evidence import (
load_latest_ai_agent_candidate_operation_dry_run_evidence,
)
def _write_snapshot(tmp_path, payload):
path = tmp_path / "ai_agent_candidate_operation_dry_run_evidence_2026-06-12.json"
path.write_text(json.dumps(payload), encoding="utf-8")
return path
def test_load_latest_ai_agent_candidate_operation_dry_run_evidence():
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
assert data["schema_version"] == "ai_agent_candidate_operation_dry_run_evidence_v1"
assert data["program_status"]["current_task_id"] == "P2-102"
assert data["program_status"]["next_task_id"] == "P2-103"
assert data["program_status"]["overall_completion_percent"] == 98
assert data["dry_run_truth"]["dry_run_evidence_gate_ready"] is True
assert data["dry_run_truth"]["all_candidate_operations_have_dry_run_evidence"] is True
assert data["dry_run_truth"]["runtime_execution_enabled"] is False
assert data["dry_run_truth"]["gateway_queue_write_enabled"] is False
assert data["dry_run_truth"]["telegram_send_enabled"] is False
assert data["dry_run_truth"]["telegram_bot_api_call_enabled"] is False
assert data["dry_run_truth"]["production_write_enabled"] is False
assert data["dry_run_truth"]["secret_value_read_enabled"] is False
assert data["dry_run_truth"]["destructive_operation_enabled"] is False
assert data["rollups"]["candidate_operation_count"] == 13
assert data["rollups"]["candidate_with_dry_run_evidence_count"] == 13
assert data["rollups"]["passed_no_write_count"] == 4
assert data["rollups"]["needs_owner_review_count"] == 3
assert data["rollups"]["blocked_until_allowlist_count"] == 2
assert data["rollups"]["blocked_by_policy_count"] == 4
assert data["rollups"]["verifier_plan_count"] == 6
assert data["rollups"]["gate_evidence_requirement_count"] == 7
assert data["rollups"]["operator_handoff_count"] == 5
assert data["rollups"]["side_effect_count"] == 0
assert data["rollups"]["runtime_execution_count"] == 0
assert data["rollups"]["gateway_queue_write_count"] == 0
assert data["rollups"]["telegram_send_count"] == 0
assert data["rollups"]["production_write_count"] == 0
assert data["rollups"]["secret_value_read_count"] == 0
assert data["rollups"]["destructive_operation_count"] == 0
def test_rejects_runtime_execution_enabled(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["dry_run_truth"]["runtime_execution_enabled"] = True
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="live execution/send/write flags"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_gateway_queue_write_count(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["dry_run_truth"]["gateway_queue_write_count_24h"] = 1
bad["rollups"]["gateway_queue_write_count"] = 1
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="live execution/send/write counts"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_candidate_side_effect(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["candidate_operations"][0]["side_effect_count"] = 1
bad["rollups"]["side_effect_count"] = 1
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="side-effect counts"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_candidate_without_hash(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["candidate_operations"][0]["output_evidence_hash"] = "missing"
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="output_evidence_hash"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_verifier_live_readback(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["verifier_plans"][0]["live_readback_enabled"] = True
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="live_readback_enabled"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_gate_opening_live_execution(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["gate_evidence_requirements"][0]["opens_live_execution"] = True
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="opens_live_execution"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_handoff_runtime_action(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["operator_handoffs"][0]["creates_runtime_action"] = True
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="creates_runtime_action"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)
def test_rejects_rollup_mismatch(tmp_path):
data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
bad = copy.deepcopy(data)
bad["rollups"]["candidate_operation_count"] = 999
_write_snapshot(tmp_path, bad)
with pytest.raises(ValueError, match="rollup counts"):
load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)

View File

@@ -0,0 +1,40 @@
from fastapi.testclient import TestClient
from src.main import app
def test_get_ai_agent_candidate_operation_dry_run_evidence_api():
client = TestClient(app)
response = client.get("/api/v1/agents/agent-candidate-operation-dry-run-evidence")
assert response.status_code == 200
data = response.json()
assert data["schema_version"] == "ai_agent_candidate_operation_dry_run_evidence_v1"
assert data["program_status"]["current_task_id"] == "P2-102"
assert data["program_status"]["next_task_id"] == "P2-103"
assert data["program_status"]["overall_completion_percent"] == 98
assert data["dry_run_truth"]["dry_run_evidence_gate_ready"] is True
assert data["dry_run_truth"]["all_candidate_operations_have_dry_run_evidence"] is True
assert data["dry_run_truth"]["runtime_execution_enabled"] is False
assert data["dry_run_truth"]["gateway_queue_write_enabled"] is False
assert data["dry_run_truth"]["telegram_send_enabled"] is False
assert data["dry_run_truth"]["telegram_bot_api_call_enabled"] is False
assert data["dry_run_truth"]["production_write_enabled"] is False
assert data["dry_run_truth"]["secret_value_read_enabled"] is False
assert data["dry_run_truth"]["destructive_operation_enabled"] is False
assert data["rollups"]["candidate_operation_count"] == 13
assert data["rollups"]["candidate_with_dry_run_evidence_count"] == 13
assert data["rollups"]["passed_no_write_count"] == 4
assert data["rollups"]["needs_owner_review_count"] == 3
assert data["rollups"]["blocked_until_allowlist_count"] == 2
assert data["rollups"]["blocked_by_policy_count"] == 4
assert data["rollups"]["verifier_plan_count"] == 6
assert data["rollups"]["gate_evidence_requirement_count"] == 7
assert data["rollups"]["operator_handoff_count"] == 5
assert data["rollups"]["side_effect_count"] == 0
assert data["rollups"]["runtime_execution_count"] == 0
assert data["rollups"]["gateway_queue_write_count"] == 0
assert data["rollups"]["telegram_send_count"] == 0
assert data["rollups"]["production_write_count"] == 0
assert data["rollups"]["secret_value_read_count"] == 0
assert data["rollups"]["destructive_operation_count"] == 0