feat(ai): add PixelRAG marketplace adapter dry run

This commit is contained in:
ogt
2026-07-10 11:32:30 +08:00
parent edd5157496
commit 8877261ae4
10 changed files with 1097 additions and 7 deletions

View File

@@ -1437,6 +1437,7 @@ def test_collect_ai_automation_smoke_uses_worst_status(monkeypatch):
monkeypatch.setattr(smoke, "_pixelrag_rag_candidate_replay_check", lambda: smoke._check("pixelrag replay", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_source_contract_replay_worker_check", lambda: smoke._check("pixelrag source contract", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_marketplace_adapter_preflight_check", lambda: smoke._check("pixelrag marketplace adapter preflight", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_marketplace_adapter_dry_run_check", lambda: smoke._check("pixelrag marketplace adapter dry-run", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_ocr_vlm_replay_check", lambda: smoke._check("pixelrag ocr vlm", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_vlm_route_readiness_check", lambda: smoke._check("pixelrag vlm route", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_vlm_replay_worker_check", lambda: smoke._check("pixelrag vlm worker", "ok", "ok"))
@@ -1446,7 +1447,7 @@ def test_collect_ai_automation_smoke_uses_worst_status(monkeypatch):
result = smoke.collect_ai_automation_smoke(record_history=False)
assert result["status"] == "critical"
assert result["summary"] == {"ok": 38, "warning": 1, "critical": 1, "total": 40}
assert result["summary"] == {"ok": 39, "warning": 1, "critical": 1, "total": 41}
def test_pchome_controlled_apply_drift_monitor_reports_verified_zero_drift(monkeypatch):
@@ -3978,6 +3979,7 @@ def test_collect_ai_automation_smoke_persists_recent_history(tmp_path, monkeypat
monkeypatch.setattr(smoke, "_pixelrag_rag_candidate_replay_check", lambda: smoke._check("pixelrag replay", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_source_contract_replay_worker_check", lambda: smoke._check("pixelrag source contract", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_marketplace_adapter_preflight_check", lambda: smoke._check("pixelrag marketplace adapter preflight", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_marketplace_adapter_dry_run_check", lambda: smoke._check("pixelrag marketplace adapter dry-run", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_ocr_vlm_replay_check", lambda: smoke._check("pixelrag ocr vlm", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_vlm_route_readiness_check", lambda: smoke._check("pixelrag vlm route", "ok", "ok"))
monkeypatch.setattr(smoke, "_pixelrag_vlm_replay_worker_check", lambda: smoke._check("pixelrag vlm worker", "ok", "ok"))
@@ -4038,7 +4040,7 @@ def test_scheduled_automation_health_summary_reads_history_without_side_effects(
json.dumps({
"generated_at": datetime.now().isoformat(timespec="seconds"),
"status": "ok",
"summary": {"ok": 40, "warning": 0, "critical": 0, "total": 40},
"summary": {"ok": 41, "warning": 0, "critical": 0, "total": 41},
"checks": [
{
"name": "PChome 受控落地 drift monitor",
@@ -4190,6 +4192,29 @@ def test_scheduled_automation_health_summary_reads_history_without_side_effects(
"primary_human_gate_count": 0,
},
},
{
"name": "PixelRAG marketplace adapter dry-run",
"status": "ok",
"summary": "PixelRAG marketplace adapter dry-run candidates=1, ready=1, blocked=0, dry_run=1, executed=0, receipts=0, fields=14, outputs=2",
"details": {
"policy": "controlled_pixelrag_marketplace_source_contract_adapter_dry_run_v1",
"candidate_count": 1,
"ready_count": 1,
"blocked_count": 0,
"dry_run_count": 1,
"executed_count": 0,
"receipt_written_count": 0,
"capture_runtime_gap_count": 0,
"field_contract_count": 14,
"dry_run_output_contract_count": 2,
"network_call_performed": False,
"model_call_performed": False,
"artifact_write_performed": False,
"next_machine_action": "run_marketplace_source_contract_adapter_dry_run_execute",
"writes_database_count": 0,
"primary_human_gate_count": 0,
},
},
{
"name": "PixelRAG OCR/VLM replay contract",
"status": "ok",
@@ -4304,7 +4329,7 @@ def test_scheduled_automation_health_summary_reads_history_without_side_effects(
)
assert summary["policy"] == "read_only_ai_automation_scheduled_health_summary"
assert summary["status"] == "ok"
assert summary["summary"]["total"] == 37
assert summary["summary"]["total"] == 38
assert summary["summary"]["primary_human_gate_count"] == 0
assert summary["summary"]["writes_database_count"] == 0
assert pchome_family["status"] == "ok"
@@ -6646,6 +6671,11 @@ def test_surface_html_readback_check_is_part_of_ai_smoke(monkeypatch):
"ok",
"pixelrag marketplace adapter preflight ok",
))
monkeypatch.setattr(smoke, "_pixelrag_marketplace_adapter_dry_run_check", lambda: smoke._check(
"PixelRAG marketplace adapter dry-run",
"ok",
"pixelrag marketplace adapter dry-run ok",
))
monkeypatch.setattr(smoke, "_pixelrag_ocr_vlm_replay_check", lambda: smoke._check(
"PixelRAG OCR/VLM replay contract",
"ok",
@@ -6686,7 +6716,7 @@ def test_surface_html_readback_check_is_part_of_ai_smoke(monkeypatch):
item for item in result["checks"]
if item["name"] == "Sitewide visual QA readback"
)
assert result["summary"]["total"] == 40
assert result["summary"]["total"] == 41
assert surface_check["status"] == "ok"
assert surface_check["details"]["checked_surface_count"] == 10
assert sitewide_check["status"] == "ok"

View File

@@ -0,0 +1,259 @@
import json
import subprocess
import sys
from datetime import datetime, timezone
def _write_adapter_preflight_receipt(
root,
*,
platform="coupang_tw",
manifest_id="coupang-403",
adapter_code="coupang",
barrier_type="access_denied",
capture_runtime_unavailable=False,
):
receipt_dir = root / platform / manifest_id
receipt_dir.mkdir(parents=True)
payload = {
"generated_at": datetime.now(timezone.utc).isoformat(),
"policy": "controlled_pixelrag_marketplace_source_contract_adapter_preflight_v1",
"worker_status": "executed_adapter_preflight_ready",
"platform": platform,
"manifest_id": manifest_id,
"adapter_code": adapter_code,
"source_contract_replay_receipt_path": (
f"runtime_artifacts/pixelrag_source_contract_replay_receipts/"
f"{platform}/{manifest_id}/source_contract_replay_receipt.json"
),
"source_worker_receipt_path": (
f"runtime_artifacts/pixelrag_platform_probe_worker_receipts/"
f"{platform}/{manifest_id}/platform_probe_worker_receipt.json"
),
"source_receipt_path": (
f"runtime_artifacts/pixelrag_visual_evidence/"
f"{platform}/{manifest_id}/capture_receipt.json"
),
"adapter_preflight_check_count": 16,
"adapter_preflight_check_pass_count": 16,
"adapter_preflight_checks": {
"adapter_code_present": True,
"blocked_page_not_product_data": True,
"canonical_offer_fields_present": True,
"database_write_disallowed": True,
"direct_price_write_disallowed": True,
"identity_and_promotion_gates_required": True,
"network_request_deferred": True,
"provenance_contract_required": True,
"public_source_boundary_required": True,
"rate_limit_contract_required": True,
"receipt_fresh": True,
"receipt_parse_ok": True,
"source_contract_checks_all_passed": True,
"source_contract_replay_ready": True,
"source_contract_version_supported": True,
"source_count_positive": True,
},
"adapter_preflight": {
"adapter_preflight_version": "pixelrag_marketplace_adapter_preflight_v1",
"source_contract_version": "pixelrag_marketplace_source_contract_v1",
"contract_id": f"{platform}:{manifest_id}:{adapter_code}",
"adapter_code": adapter_code,
"barrier_type": barrier_type,
"capture_runtime_unavailable": capture_runtime_unavailable,
"source_count": 2,
"expected_offer_fields": [
"platform",
"platform_product_id",
"title",
"url",
"image_url",
"price",
"currency",
"availability",
"seller",
"promotion_badges",
"shipping_badges",
"evidence_time",
"source_method",
"confidence",
],
"required_before_data_promotion": [
"public_source_boundary",
"rate_limit_contract",
"provenance_contract",
"identity_matcher_replay",
"promotion_gate",
"embedding_signature_guard",
],
},
"promotion_boundary": {
"direct_ai_insights_write_allowed": False,
"direct_price_write_allowed": False,
},
"writes_database": False,
"writes_database_count": 0,
"network_call_performed": False,
"model_call_performed": False,
}
path = receipt_dir / "marketplace_adapter_preflight_receipt.json"
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
return path
def test_marketplace_adapter_dry_run_reads_preflight_receipts(tmp_path):
from services.pixelrag_marketplace_source_contract_adapter_dry_run_service import (
POLICY,
run_pixelrag_marketplace_source_contract_adapter_dry_run,
)
preflight_root = tmp_path / "adapter_preflight"
_write_adapter_preflight_receipt(preflight_root)
_write_adapter_preflight_receipt(
preflight_root,
platform="shopee_tw",
manifest_id="shopee-runtime-gap",
adapter_code="shopee",
barrier_type="language_or_region_interstitial",
capture_runtime_unavailable=True,
)
payload = run_pixelrag_marketplace_source_contract_adapter_dry_run(
adapter_preflight_receipt_root=preflight_root,
platform=("coupang_tw", "shopee_tw"),
)
by_platform = {item["platform"]: item for item in payload["worker_items"]}
assert payload["policy"] == POLICY
assert payload["status"] == "ok"
assert payload["summary"]["candidate_count"] == 2
assert payload["summary"]["ready_count"] == 2
assert payload["summary"]["dry_run_count"] == 2
assert payload["summary"]["capture_runtime_gap_count"] == 1
assert payload["summary"]["field_contract_count"] == 28
assert payload["summary"]["dry_run_output_contract_count"] == 4
assert payload["summary"]["writes_database_count"] == 0
assert payload["controlled_apply"]["network_call"] is False
assert payload["controlled_apply"]["writes_database"] is False
assert payload["promotion_boundary"]["writes_price_tables"] is False
assert payload["next_machine_action"] == (
"run_marketplace_source_contract_adapter_dry_run_execute"
)
assert by_platform["coupang_tw"]["worker_status"] == (
"dry_run_ready_for_marketplace_adapter_dry_run"
)
assert by_platform["coupang_tw"]["adapter_dry_run_checks"][
"price_write_blocked_until_promotion_gate"
] is True
assert by_platform["coupang_tw"]["adapter_dry_run"]["offer_candidate_contracts"][0][
"ready_for_price_table_write"
] is False
assert by_platform["shopee_tw"]["adapter_dry_run"][
"capture_runtime_unavailable"
] is True
def test_marketplace_adapter_dry_run_execute_writes_receipt(tmp_path):
from services.pixelrag_marketplace_source_contract_adapter_dry_run_service import (
run_pixelrag_marketplace_source_contract_adapter_dry_run,
)
preflight_root = tmp_path / "adapter_preflight"
output_root = tmp_path / "adapter_dry_run"
_write_adapter_preflight_receipt(preflight_root)
payload = run_pixelrag_marketplace_source_contract_adapter_dry_run(
adapter_preflight_receipt_root=preflight_root,
output_root=output_root,
platform="coupang_tw",
execute=True,
write_receipt=True,
)
assert payload["status"] == "ok"
assert payload["summary"]["executed_count"] == 1
assert payload["summary"]["receipt_written_count"] == 1
assert payload["controlled_apply"]["artifact_write"] is True
assert payload["controlled_apply"]["writes_database"] is False
assert payload["next_machine_action"] == "run_marketplace_adapter_identity_matcher_replay"
receipt_path = (
output_root
/ "coupang_tw"
/ "coupang-403"
/ "marketplace_adapter_dry_run_receipt.json"
)
assert receipt_path.exists()
receipt = json.loads(receipt_path.read_text(encoding="utf-8"))
assert receipt["worker_status"] == "executed_marketplace_adapter_dry_run_ready"
assert receipt["artifact_write_performed"] is True
assert receipt["writes_database"] is False
assert receipt["adapter_dry_run_check_pass_count"] == (
receipt["adapter_dry_run_check_count"]
)
assert receipt["adapter_dry_run"]["adapter_dry_run_version"] == (
"pixelrag_marketplace_adapter_dry_run_v1"
)
assert receipt["adapter_dry_run"]["allowed_next_step"] == (
"run_marketplace_adapter_identity_matcher_replay"
)
def test_marketplace_adapter_dry_run_cli_outputs_json(tmp_path):
preflight_root = tmp_path / "adapter_preflight"
_write_adapter_preflight_receipt(preflight_root)
completed = subprocess.run(
[
sys.executable,
"scripts/ops/run_pixelrag_marketplace_source_contract_adapter_dry_run.py",
"--adapter-preflight-receipt-root",
str(preflight_root),
"--platform",
"coupang_tw",
],
capture_output=True,
check=False,
text=True,
)
assert completed.returncode == 0
payload = json.loads(completed.stdout)
assert payload["summary"]["candidate_count"] == 1
assert payload["summary"]["ready_count"] == 1
assert payload["summary"]["field_contract_count"] == 14
assert payload["controlled_apply"]["writes_database"] is False
def test_marketplace_adapter_dry_run_route_returns_readback(
tmp_path,
monkeypatch,
):
from flask import Flask
from routes import system_public_routes as routes
from services import pixelrag_marketplace_source_contract_adapter_dry_run_service as service
preflight_root = tmp_path / "adapter_preflight"
_write_adapter_preflight_receipt(preflight_root)
monkeypatch.setattr(
service,
"DEFAULT_ADAPTER_PREFLIGHT_RECEIPT_ROOT",
str(preflight_root),
)
app = Flask(__name__)
with app.test_request_context(
"/api/ai-automation/pixelrag-marketplace-source-contract-adapter-dry-run?platform=coupang_tw"
):
response = (
routes
.ai_automation_pixelrag_marketplace_source_contract_adapter_dry_run_api
.__wrapped__()
)
payload = response.get_json()
assert payload["policy"] == (
"controlled_pixelrag_marketplace_source_contract_adapter_dry_run_v1"
)
assert payload["summary"]["candidate_count"] == 1
assert payload["summary"]["ready_count"] == 1
assert payload["controlled_apply"]["writes_database"] is False