feat(governance): 新增 result capture no-write readback
All checks were successful
Code Review / ai-code-review (push) Successful in 13s
CD Pipeline / tests (push) Successful in 1m27s
CD Pipeline / build-and-deploy (push) Successful in 4m11s
CD Pipeline / post-deploy-checks (push) Successful in 1m56s

This commit is contained in:
Your Name
2026-06-13 20:56:49 +08:00
parent 9ed913cd80
commit 69a536516e
10 changed files with 1642 additions and 1 deletions

View File

@@ -0,0 +1,112 @@
import copy
import json
from pathlib import Path
import pytest
from src.services.ai_agent_result_capture_no_write_readback import (
load_latest_ai_agent_result_capture_no_write_readback,
)
REPO_ROOT = Path(__file__).resolve().parents[3]
FIXTURE = REPO_ROOT / "docs/evaluations/ai_agent_result_capture_no_write_readback_2026-06-13.json"
def test_load_latest_ai_agent_result_capture_no_write_readback_snapshot() -> None:
data = load_latest_ai_agent_result_capture_no_write_readback()
assert data["schema_version"] == "ai_agent_result_capture_no_write_readback_v1"
assert data["program_status"]["current_task_id"] == "P2-118"
assert data["program_status"]["next_task_id"] == "P2-119"
assert data["program_status"]["overall_completion_percent"] == 100
assert data["program_status"]["read_only_mode"] is True
rollups = data["rollups"]
assert rollups["result_capture_readback_fixture_count"] == 5
assert rollups["capture_field_mapping_count"] == 5
assert rollups["readback_verifier_check_count"] == 5
assert rollups["blocked_result_capture_write_count"] == 5
assert rollups["operator_action_count"] == 5
assert rollups["approval_required_fixture_count"] == 2
assert rollups["blocked_fixture_count"] == 2
assert rollups["approval_required_mapping_count"] == 1
assert rollups["blocked_mapping_count"] == 2
assert rollups["approval_required_verifier_count"] == 2
assert rollups["critical_blocker_count"] == 3
assert {mapping["target_capture"] for mapping in data["capture_field_mappings"]} == {
"result_capture_preview"
}
assert {fixture["readback_mode"] for fixture in data["result_capture_readback_fixtures"]} == {
"no_write_fixture"
}
zero_fields = [
"owner_approval_received_count",
"canonical_runtime_target_read_count",
"live_query_count",
"reviewer_queue_write_count",
"gateway_queue_write_count",
"telegram_send_count",
"bot_api_call_count",
"report_receipt_write_count",
"result_capture_write_count",
"learning_write_count",
"playbook_trust_write_count",
"production_write_count",
"secret_read_count",
"destructive_operation_count",
]
for field in zero_fields:
assert rollups[field] == 0
def test_result_capture_no_write_readback_rejects_capture_write_enabled(tmp_path: Path) -> None:
source = json.loads(FIXTURE.read_text(encoding="utf-8"))
source["result_capture_readback_fixtures"][0]["result_capture_write_enabled"] = True
target = tmp_path / "ai_agent_result_capture_no_write_readback_2026-06-13.json"
target.write_text(json.dumps(source), encoding="utf-8")
with pytest.raises(ValueError, match="must not enable result capture write"):
load_latest_ai_agent_result_capture_no_write_readback(tmp_path)
def test_result_capture_no_write_readback_rejects_truth_write_flag(tmp_path: Path) -> None:
source = json.loads(FIXTURE.read_text(encoding="utf-8"))
source["readback_truth"]["result_capture_write_enabled"] = True
target = tmp_path / "ai_agent_result_capture_no_write_readback_2026-06-13.json"
target.write_text(json.dumps(source), encoding="utf-8")
with pytest.raises(ValueError, match="live read/send/write flags"):
load_latest_ai_agent_result_capture_no_write_readback(tmp_path)
def test_result_capture_no_write_readback_rejects_target_capture_drift(tmp_path: Path) -> None:
source = json.loads(FIXTURE.read_text(encoding="utf-8"))
source["capture_field_mappings"][0]["target_capture"] = "live_result_capture"
target = tmp_path / "ai_agent_result_capture_no_write_readback_2026-06-13.json"
target.write_text(json.dumps(source), encoding="utf-8")
with pytest.raises(ValueError, match="must target result_capture_preview"):
load_latest_ai_agent_result_capture_no_write_readback(tmp_path)
def test_result_capture_no_write_readback_rejects_rollup_drift(tmp_path: Path) -> None:
source = json.loads(FIXTURE.read_text(encoding="utf-8"))
source["rollups"]["result_capture_readback_fixture_count"] = 4
target = tmp_path / "ai_agent_result_capture_no_write_readback_2026-06-13.json"
target.write_text(json.dumps(source), encoding="utf-8")
with pytest.raises(ValueError, match="rollup counts mismatch"):
load_latest_ai_agent_result_capture_no_write_readback(tmp_path)
def test_result_capture_no_write_readback_rejects_forbidden_display_terms(tmp_path: Path) -> None:
source = copy.deepcopy(json.loads(FIXTURE.read_text(encoding="utf-8")))
source["operator_actions"][0]["operator_instruction"] = "do not expose session_id"
target = tmp_path / "ai_agent_result_capture_no_write_readback_2026-06-13.json"
target.write_text(json.dumps(source), encoding="utf-8")
with pytest.raises(ValueError, match="forbidden display terms"):
load_latest_ai_agent_result_capture_no_write_readback(tmp_path)

View File

@@ -0,0 +1,45 @@
import pytest
from httpx import ASGITransport, AsyncClient
from src.main import app
@pytest.mark.asyncio
async def test_get_agent_result_capture_no_write_readback_api() -> None:
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
response = await client.get("/api/v1/agents/agent-result-capture-no-write-readback")
assert response.status_code == 200
data = response.json()
assert data["schema_version"] == "ai_agent_result_capture_no_write_readback_v1"
assert data["program_status"]["current_task_id"] == "P2-118"
assert data["program_status"]["next_task_id"] == "P2-119"
assert data["program_status"]["overall_completion_percent"] == 100
rollups = data["rollups"]
assert rollups["result_capture_readback_fixture_count"] == 5
assert rollups["capture_field_mapping_count"] == 5
assert rollups["readback_verifier_check_count"] == 5
assert rollups["blocked_result_capture_write_count"] == 5
assert rollups["operator_action_count"] == 5
assert rollups["critical_blocker_count"] == 3
assert rollups["owner_approval_received_count"] == 0
assert rollups["canonical_runtime_target_read_count"] == 0
assert rollups["live_query_count"] == 0
assert rollups["reviewer_queue_write_count"] == 0
assert rollups["gateway_queue_write_count"] == 0
assert rollups["telegram_send_count"] == 0
assert rollups["bot_api_call_count"] == 0
assert rollups["report_receipt_write_count"] == 0
assert rollups["result_capture_write_count"] == 0
assert rollups["learning_write_count"] == 0
assert rollups["playbook_trust_write_count"] == 0
assert rollups["production_write_count"] == 0
assert {mapping["target_capture"] for mapping in data["capture_field_mappings"]} == {
"result_capture_preview"
}
assert {fixture["result_capture_write_enabled"] for fixture in data["result_capture_readback_fixtures"]} == {
False
}