feat(ai): replay platform probe worker receipts
This commit is contained in:
@@ -40,6 +40,45 @@ def _write_receipt(root, *, platform, manifest_id, title, url, http_status=200):
|
||||
return path
|
||||
|
||||
|
||||
def _write_platform_probe_worker_receipt(
|
||||
root,
|
||||
*,
|
||||
platform,
|
||||
manifest_id,
|
||||
worker_status="executed_structured_source_fallback_package",
|
||||
barrier_type="access_denied",
|
||||
capture_runtime_unavailable=False,
|
||||
):
|
||||
receipt_dir = root / platform / manifest_id
|
||||
receipt_dir.mkdir(parents=True)
|
||||
payload = {
|
||||
"platform": platform,
|
||||
"manifest_id": manifest_id,
|
||||
"source_type": "capture_receipt",
|
||||
"source_receipt_path": f"runtime_artifacts/pixelrag_visual_evidence/{platform}/{manifest_id}/capture_receipt.json",
|
||||
"worker_status": worker_status,
|
||||
"probe_status": "structured_source_or_backoff_required",
|
||||
"barrier_type": barrier_type,
|
||||
"network_call_performed": False,
|
||||
"artifact_write_performed": True,
|
||||
"writes_database": False,
|
||||
"next_machine_action": "run_structured_source_or_platform_backoff_policy",
|
||||
"structured_source_package": {
|
||||
"adapter_code": platform.replace("_tw", ""),
|
||||
"available": True,
|
||||
"source_count": 2,
|
||||
"network_request_allowed": False,
|
||||
"database_write_allowed": False,
|
||||
"dry_run_only": True,
|
||||
"blocked_page_not_product_data": True,
|
||||
"capture_runtime_unavailable": capture_runtime_unavailable,
|
||||
},
|
||||
}
|
||||
path = receipt_dir / "platform_probe_worker_receipt.json"
|
||||
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def test_pixelrag_rag_candidate_replay_splits_eligible_and_blocked_receipts(tmp_path):
|
||||
from services.pixelrag_rag_candidate_replay_service import (
|
||||
POLICY,
|
||||
@@ -62,7 +101,10 @@ def test_pixelrag_rag_candidate_replay_splits_eligible_and_blocked_receipts(tmp_
|
||||
http_status=403,
|
||||
)
|
||||
|
||||
payload = build_pixelrag_rag_candidate_replay_readback(artifact_root=tmp_path)
|
||||
payload = build_pixelrag_rag_candidate_replay_readback(
|
||||
artifact_root=tmp_path,
|
||||
platform_probe_worker_receipt_root=tmp_path / "worker_empty",
|
||||
)
|
||||
by_platform = {item["platform"]: item for item in payload["candidates"]}
|
||||
|
||||
assert payload["policy"] == POLICY
|
||||
@@ -81,6 +123,93 @@ def test_pixelrag_rag_candidate_replay_splits_eligible_and_blocked_receipts(tmp_
|
||||
assert payload["controlled_apply"]["db_write"] is False
|
||||
|
||||
|
||||
def test_pixelrag_rag_candidate_replay_reads_platform_probe_worker_receipts(tmp_path):
|
||||
from services.pixelrag_rag_candidate_replay_service import (
|
||||
build_pixelrag_rag_candidate_replay_readback,
|
||||
)
|
||||
|
||||
worker_root = tmp_path / "worker"
|
||||
_write_platform_probe_worker_receipt(
|
||||
worker_root,
|
||||
platform="coupang_tw",
|
||||
manifest_id="coupang-403",
|
||||
barrier_type="access_denied",
|
||||
)
|
||||
_write_platform_probe_worker_receipt(
|
||||
worker_root,
|
||||
platform="shopee_tw",
|
||||
manifest_id="shopee-runtime-gap",
|
||||
worker_status="capture_runtime_unavailable_structured_fallback_package",
|
||||
barrier_type="traffic_verification_interstitial",
|
||||
capture_runtime_unavailable=True,
|
||||
)
|
||||
|
||||
payload = build_pixelrag_rag_candidate_replay_readback(
|
||||
artifact_root=tmp_path / "visual_empty",
|
||||
platform_probe_worker_receipt_root=worker_root,
|
||||
platform=("coupang_tw", "shopee_tw"),
|
||||
)
|
||||
by_platform = {item["platform"]: item for item in payload["candidates"]}
|
||||
|
||||
assert payload["status"] == "ok"
|
||||
assert payload["summary"]["receipt_count"] == 2
|
||||
assert payload["summary"]["visual_receipt_count"] == 0
|
||||
assert payload["summary"]["platform_probe_worker_receipt_count"] == 2
|
||||
assert payload["summary"]["source_contract_fallback_count"] == 2
|
||||
assert payload["summary"]["capture_runtime_gap_count"] == 1
|
||||
assert payload["next_machine_action"] == (
|
||||
"run_source_contract_replay_for_platform_probe_worker_receipts"
|
||||
)
|
||||
assert by_platform["coupang_tw"]["candidate_status"] == "source_contract_fallback"
|
||||
assert by_platform["coupang_tw"]["eligible_for_source_contract_replay"] is True
|
||||
assert by_platform["coupang_tw"]["eligible_for_rag_candidate_replay"] is False
|
||||
assert by_platform["coupang_tw"]["promotion_boundary"][
|
||||
"requires_source_contract_before_knowledge_write"
|
||||
] is True
|
||||
assert by_platform["shopee_tw"]["platform_probe_worker"][
|
||||
"structured_source_package"
|
||||
]["capture_runtime_unavailable"] is True
|
||||
assert payload["replay_contract"][
|
||||
"platform_probe_worker_receipts_are_source_contract_candidates"
|
||||
] is True
|
||||
assert payload["controlled_apply"]["db_write"] is False
|
||||
|
||||
|
||||
def test_pixelrag_rag_candidate_replay_prefers_worker_source_contract_handoff(tmp_path):
|
||||
from services.pixelrag_rag_candidate_replay_service import (
|
||||
build_pixelrag_rag_candidate_replay_readback,
|
||||
)
|
||||
|
||||
visual_root = tmp_path / "visual"
|
||||
worker_root = tmp_path / "worker"
|
||||
_write_receipt(
|
||||
visual_root,
|
||||
platform="coupang_tw",
|
||||
manifest_id="coupang-403",
|
||||
title="Access Denied",
|
||||
url="https://www.tw.coupang.com/search?q=iphone",
|
||||
http_status=403,
|
||||
)
|
||||
_write_platform_probe_worker_receipt(
|
||||
worker_root,
|
||||
platform="coupang_tw",
|
||||
manifest_id="coupang-403",
|
||||
barrier_type="access_denied",
|
||||
)
|
||||
|
||||
payload = build_pixelrag_rag_candidate_replay_readback(
|
||||
artifact_root=visual_root,
|
||||
platform_probe_worker_receipt_root=worker_root,
|
||||
platform="coupang_tw",
|
||||
)
|
||||
|
||||
assert payload["summary"]["visual_barrier_count"] == 1
|
||||
assert payload["summary"]["source_contract_fallback_count"] == 1
|
||||
assert payload["next_machine_action"] == (
|
||||
"run_source_contract_replay_for_platform_probe_worker_receipts"
|
||||
)
|
||||
|
||||
|
||||
def test_pixelrag_rag_candidate_replay_cli_outputs_machine_readable_json(tmp_path):
|
||||
_write_receipt(
|
||||
tmp_path,
|
||||
@@ -98,6 +227,8 @@ def test_pixelrag_rag_candidate_replay_cli_outputs_machine_readable_json(tmp_pat
|
||||
str(tmp_path),
|
||||
"--platform",
|
||||
"shopee_tw",
|
||||
"--platform-probe-worker-receipt-root",
|
||||
str(tmp_path / "worker_empty"),
|
||||
],
|
||||
capture_output=True,
|
||||
check=False,
|
||||
@@ -124,6 +255,11 @@ def test_pixelrag_rag_candidate_replay_route_returns_readback(tmp_path, monkeypa
|
||||
url="https://shopee.tw/search?keyword=sunscreen",
|
||||
)
|
||||
monkeypatch.setattr(service, "DEFAULT_ARTIFACT_ROOT", str(tmp_path))
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"DEFAULT_PLATFORM_PROBE_WORKER_RECEIPT_ROOT",
|
||||
str(tmp_path / "worker_empty"),
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
with app.test_request_context(
|
||||
|
||||
Reference in New Issue
Block a user