From a72713af1e9dab3a6d7232b7d0403b3c58d24772 Mon Sep 17 00:00:00 2001 From: ogt Date: Thu, 9 Jul 2026 23:01:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai):=20=E5=BB=BA=E7=AB=8B=20PixelRAG=20OCR?= =?UTF-8?q?=20VLM=20replay=20contract?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO_NEXT_STEPS.txt | 7 +- config.py | 2 +- docs/AI_INTELLIGENCE_MODULE_SOT.md | 6 +- .../ai_automation_mainline_work_items.md | 7 +- docs/guides/ai_automation_session_sop.md | 4 +- docs/guides/browse_sh_crawler_playbook.md | 9 + routes/system_public_routes.py | 22 ++ scripts/ops/report_pixelrag_ocr_vlm_replay.py | 60 +++ services/ai_automation_smoke_service.py | 99 +++++ services/pixelrag_ocr_vlm_replay_service.py | 354 ++++++++++++++++++ tests/test_ai_automation_smoke_service.py | 39 +- tests/test_pixelrag_ocr_vlm_replay_service.py | 144 +++++++ 12 files changed, 739 insertions(+), 14 deletions(-) create mode 100755 scripts/ops/report_pixelrag_ocr_vlm_replay.py create mode 100644 services/pixelrag_ocr_vlm_replay_service.py create mode 100644 tests/test_pixelrag_ocr_vlm_replay_service.py diff --git a/TODO_NEXT_STEPS.txt b/TODO_NEXT_STEPS.txt index 198203b..94ef45c 100644 --- a/TODO_NEXT_STEPS.txt +++ b/TODO_NEXT_STEPS.txt @@ -9,7 +9,7 @@ python scripts/ops/check_production_version_truth.py 目前最新版本仍以 production `https://mo.wooo.work/health` readback 為準。 -本輪 source target 為 `V10.746`;部署完成前不得宣稱正式環境已是 `V10.746`。 +本輪 source target 為 `V10.747`;部署完成前不得宣稱正式環境已是 `V10.747`。 舊 iCloud checkout 不是 Gitea dev worktree,不得拿來當最新版本真相。 ================================================================================ @@ -58,8 +58,9 @@ P6. DB apply authorization / signing / closeout P0-2026-07-09. PixelRAG / MCP / RAG 全自動主線 - 已完成:多電商 PixelRAG visual evidence lane 與 external MCP/RAG integration readback。 - - 進行中:PixelRAG receipt → internal RAG candidate replay,以及 MCP/RAG runtime health → AI automation smoke。 - - 未開始:OCR/VLM replay、Ollama-first visual embedding benchmark、pgvector-compatible visual metadata、Coupang platform probe / structured API、跨平台 source contracts。 + - 已完成:PixelRAG receipt → internal RAG candidate replay,以及 OCR/VLM replay contract no-write readback。 + - 進行中:MCP/RAG runtime health → AI automation smoke。 + - 未開始:Ollama-first OCR/VLM extraction worker、Ollama-first visual embedding benchmark、pgvector-compatible visual metadata、Coupang platform probe / structured API、跨平台 source contracts。 - 主線文件:`docs/guides/ai_automation_mainline_work_items.md`。 ================================================================================ diff --git a/config.py b/config.py index fc12f72..5c407ff 100644 --- a/config.py +++ b/config.py @@ -402,7 +402,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '') # ========================================== # 系統版本與路徑 # ========================================== -SYSTEM_VERSION = "V10.746" +SYSTEM_VERSION = "V10.747" LOG_FILE_PATH = os.path.join(BASE_DIR, 'logs/system.log') public_url = PUBLIC_URL # 用於模板顯示 diff --git a/docs/AI_INTELLIGENCE_MODULE_SOT.md b/docs/AI_INTELLIGENCE_MODULE_SOT.md index a34c03d..0e87108 100644 --- a/docs/AI_INTELLIGENCE_MODULE_SOT.md +++ b/docs/AI_INTELLIGENCE_MODULE_SOT.md @@ -1,8 +1,8 @@ # PChome 業績成長自動化作戰系統 — AI 競價情報模組 Single Source of Truth > **最後更新**: 2026-07-09 (台北時間) -> **狀態**: 🟢 四 AI Agent 自動化閉環已落地;LLM 路由紅線升級為 Ollama-first 三主機級聯;PChome 後台業績匯入韌性已補強;產品定位正名為「PChome 業績成長自動化作戰系統」;外部市場來源正規化層、自動同步、作戰清單與價格參考表優先讀取、CSV 備援預檢、前台操作入口、高可見頁面繁中化守門、比價/作戰 UI 工作台化、AI 密集工作台文字密度守門、跨平台來源治理、商品身份 UI 契約、sitewide visual QA runtime readback、external MCP/RAG integration monitoring、PixelRAG RAG candidate replay monitoring、PChome auto-policy authorization guard monitoring、decision preflight machine evidence monitoring、decision closeout monitoring、authorization issuer gate monitoring、signing decision preflight monitoring、signing decision closeout monitoring、signing issuer guard monitoring、signing issuer closeout monitoring、signing execution preflight monitoring、signing execution closeout monitoring、signed receipt preflight monitoring、signed receipt closeout monitoring、signed receipt evidence intake monitoring、detached verification evidence validation monitoring、verifier receipt closeout monitoring、authorization evidence execution preflight monitoring、authorization evidence execution closeout monitoring、controlled apply final preflight monitoring、controlled dry-run package monitoring 與 controlled dry-run receipt closeout monitoring 已建立,GCP embedding 熔斷延後處理、110 proxy rescue 與 direct host health skip 已建立 -> **適用版本**: V10.746 +> **狀態**: 🟢 四 AI Agent 自動化閉環已落地;LLM 路由紅線升級為 Ollama-first 三主機級聯;PChome 後台業績匯入韌性已補強;產品定位正名為「PChome 業績成長自動化作戰系統」;外部市場來源正規化層、自動同步、作戰清單與價格參考表優先讀取、CSV 備援預檢、前台操作入口、高可見頁面繁中化守門、比價/作戰 UI 工作台化、AI 密集工作台文字密度守門、跨平台來源治理、商品身份 UI 契約、sitewide visual QA runtime readback、external MCP/RAG integration monitoring、PixelRAG RAG candidate replay monitoring、PixelRAG OCR/VLM replay contract monitoring、PChome auto-policy authorization guard monitoring、decision preflight machine evidence monitoring、decision closeout monitoring、authorization issuer gate monitoring、signing decision preflight monitoring、signing decision closeout monitoring、signing issuer guard monitoring、signing issuer closeout monitoring、signing execution preflight monitoring、signing execution closeout monitoring、signed receipt preflight monitoring、signed receipt closeout monitoring、signed receipt evidence intake monitoring、detached verification evidence validation monitoring、verifier receipt closeout monitoring、authorization evidence execution preflight monitoring、authorization evidence execution closeout monitoring、controlled apply final preflight monitoring、controlled dry-run package monitoring 與 controlled dry-run receipt closeout monitoring 已建立,GCP embedding 熔斷延後處理、110 proxy rescue 與 direct host health skip 已建立 +> **適用版本**: V10.747 --- @@ -114,6 +114,7 @@ - 2026-07-03 起整站 desktop/tablet/mobile visual QA 必須進入 AI automation runtime monitoring:`scripts/check_responsive_overflow.js --artifact-output data/ai_automation/sitewide_visual_qa_latest.json` 會輸出機器可讀 artifact;`/api/ai-automation/sitewide-visual-qa-readback` 只讀該 artifact 並回報 route_count、viewport_count、failed_count、overflow_issue_count、stale 與 next_machine_action;`/api/ai-automation/smoke` 需包含 `Sitewide visual QA readback`,`/api/ai-automation/scheduled-health-summary` 需輸出 `sitewide_visual_qa` family,`/metrics` 需暴露 `momo_ai_automation_scheduled_health_family_status{family="sitewide_visual_qa",...}`。readback 不啟動 browser、不寫 DB、不送通知;artifact 生成才需要 Playwright/瀏覽器。 - 2026-07-09 起 external MCP/RAG integration 必須進入 AI automation runtime monitoring:`/api/ai-automation/external-mcp-rag-integration` 與 `scripts/ops/report_external_mcp_rag_integration.py` 需輸出 9 個 capability 的 absorbed / unresolved 狀態、內部治理落點與 runtime flags;`/api/ai-automation/smoke` 需包含 `External MCP/RAG integration readback`,`/api/ai-automation/scheduled-health-summary` 需輸出 `external_mcp_rag_integration` family。`MCP_ROUTER_ENABLED=false` 或 `RAG_ENABLED=false` 必須被視為 runtime gap,不得因 registry 已完成就宣稱 MCP/RAG runtime 已全面啟用。 - 2026-07-09 起 PixelRAG visual receipts 必須可轉為內部 RAG candidate replay readback:`/api/ai-automation/pixelrag-rag-candidate-replay` 與 `scripts/ops/report_pixelrag_rag_candidate_replay.py` 只讀 `capture_receipt.json`,拆分 eligible / blocked / invalid receipts,輸出 tile/missing/barrier count、candidate text、promotion boundary 與 next machine action;`/api/ai-automation/smoke` 需包含 `PixelRAG RAG candidate replay`,`/api/ai-automation/scheduled-health-summary` 需輸出 `pixelrag_rag_candidate_replay` family。blocked / 403 / captcha / access denied / verify traffic page 只能進 platform probe 或 structured API 策略,不得當作商品資料、不得寫正式價格表、不得直接寫 `ai_insights`。 +- 2026-07-09 起 PixelRAG visual receipts 進入 OCR/VLM 前必須先輸出 no-write replay contract:`/api/ai-automation/pixelrag-ocr-vlm-replay` 與 `scripts/ops/report_pixelrag_ocr_vlm_replay.py` 只讀 saved tiles,輸出欄位 schema、輸出 schema、confidence/evidence validation rules、Ollama-first route contract 與 next machine action;`/api/ai-automation/smoke` 需包含 `PixelRAG OCR/VLM replay contract`,`/api/ai-automation/scheduled-health-summary` 需輸出 `pixelrag_ocr_vlm_replay` family。此 contract 不執行 OCR/VLM、不呼叫模型、不讀 secret、不連外、不寫 DB、不寫 `ai_insights`、不寫正式價格表;blocked / 403 / captcha / access denied / verify traffic page 只能進 platform probe 或 structured API 策略。 - 2026-07-02 起 `/ai_intelligence` 商品明細與單品作戰詳情的四格價格證據必須可測:PChome 價格、MOMO 參考價、差距、可信度需以 `data-evidence` 固定,並以 `aria-label="價格證據"` 對應可掃描區塊;候選待確認或缺資料只能顯示「候選待確認 / 待補」,不得捏造價格或讓使用者打開 raw payload 才知道判斷依據。 - 2026-07-02 起 `/ai_intelligence` 必須是密集 AI 工作台,不得退回大段文字說明頁:首屏與明細可見內容只保留短狀態、數字、四格證據與下一步按鈕;KPI note、benchmark detail、alert 副句、策略說明、decision copy、來源長句與單品 reason list 不得佔用第一層視覺。`tests/test_ai_intelligence_text_density_guardrails.py` 必須鎖住 `data-density-guardrail="compact-ai-workbench"`、短任務文案、detail meta 與 hidden explanatory copy。 - 2026-07-02 起 `/observability/overview` 也必須採密集 AI 觀測工作台:首屏以 `data-density-guardrail="compact-observability-workbench"`、`AI 觀測 / 風險優先 / 下一步` 與 golden signals 先呈現狀態、數字與操作入口;hero lede、signal note、route desc、host meta 與資料來源長句不得佔用第一層視覺。`tests/test_observability_text_density_guardrails.py` 必須鎖住 compact marker 與 hidden explanatory copy。 @@ -874,6 +875,7 @@ POSTGRES_HOST=momo-db | 2026-07-03 | PChome auto-policy controlled dry-run package 必須有 runtime monitoring | V10.744 起 `/api/ai-automation/smoke`、`/api/ai-automation/scheduled-health-summary` 與 `/metrics` 必須輸出 `PChome auto-policy controlled dry-run package` / `pchome_auto_policy_controlled_dry_run_package`;此 runtime check 自動驗證 controlled apply final preflight carry-forward、rollback binding、post-apply verifier binding、12 個 dry-run package checks、12 個 package fields、10 個 acceptance gates、receipt preview、result parser、non-executable command shape、same-run production truth 與 future dry-run receipt closeout readiness,但明確標記 `receipt_execution_performed=false`、`receipt_stdout_included=false`、`receipt_stderr_included=false`、`command_shape_execution_allowed=false`、`database_apply_authorized=false`、`reads_secret_count=0`、`executes_script_count=0`、`executes_sql_count=0`、`writes_database_count=0`、`signs_database_apply_authorization_count=0`、`primary_human_gate_count=0`、`executes_authorization_evidence=false`、`executes_database_apply=false`、`ready_for_database_apply_now=false`、`issues_database_apply_authorization=false`、`signs_database_apply_authorization=false`,不讀 secret、不執行 dry-run、不捕捉 stdout/stderr、不執行 authorization evidence、不執行 database apply、不執行 endpoint/SQL、不寫 DB、不簽發 apply authorization,只允許進入下一段 dry-run receipt closeout lane。 | | 2026-07-09 | PChome auto-policy controlled dry-run receipt closeout 必須有 runtime monitoring | V10.745 起 `/api/ai-automation/smoke`、`/api/ai-automation/scheduled-health-summary` 與 `/metrics` 必須輸出 `PChome auto-policy controlled dry-run receipt closeout` / `pchome_auto_policy_controlled_dry_run_receipt_closeout`;此 runtime check 自動驗證 controlled dry-run package carry-forward、dry-run result parser、receipt validation report、command-shape hash、rollback binding、post-apply verifier binding、12 個 receipt closeout checks、12 個 closeout fields、10 個 acceptance gates、same-run production truth 與 future runner readiness,但明確標記 `receipt_validation_status=preview_validated_not_executed`、`validation_execution_performed=false`、`validation_stdout_included=false`、`validation_stderr_included=false`、`validation_database_apply_authorized=false`、`reads_secret_count=0`、`executes_script_count=0`、`executes_sql_count=0`、`writes_database_count=0`、`signs_database_apply_authorization_count=0`、`primary_human_gate_count=0`、`executes_authorization_evidence=false`、`executes_database_apply=false`、`ready_for_database_apply_now=false`、`database_apply_authorized=false`、`issues_database_apply_authorization=false`、`signs_database_apply_authorization=false`,不讀 secret、不執行 dry-run、不捕捉 stdout/stderr、不執行 authorization evidence、不執行 database apply、不執行 endpoint/SQL、不寫 DB、不簽發 apply authorization,只允許進入下一段 controlled dry-run runner readiness lane。 | | 2026-07-09 | PixelRAG / external MCP/RAG 必須有 runtime monitoring 與 candidate replay | V10.746 起 `/api/ai-automation/smoke`、`/api/ai-automation/scheduled-health-summary` 必須輸出 `External MCP/RAG integration readback` / `external_mcp_rag_integration` 與 `PixelRAG RAG candidate replay` / `pixelrag_rag_candidate_replay`;external MCP/RAG readback 回報 9 個 capability 的 absorbed / unresolved 與 `MCP_ROUTER_ENABLED`、`RAG_ENABLED` runtime flags,PixelRAG replay 只讀 visual receipt,拆分 eligible / blocked / invalid,並明確標記 blocked page 不是商品資料。此路徑不讀 secret、不呼叫外部網路、不寫 DB、不寫 `ai_insights`、不寫正式價格表;eligible receipt 仍需 OCR/VLM replay、identity matcher replay、PromotionGate 與 embedding signature guard。 | +| 2026-07-09 | PixelRAG OCR/VLM replay contract 必須有 runtime monitoring | V10.747 起 `/api/ai-automation/pixelrag-ocr-vlm-replay`、`scripts/ops/report_pixelrag_ocr_vlm_replay.py`、`/api/ai-automation/smoke` 與 `/api/ai-automation/scheduled-health-summary` 必須輸出 no-write OCR/VLM replay contract / `pixelrag_ocr_vlm_replay` family;readback 只讀 saved tiles 與 RAG candidate replay,輸出 ready / blocked / invalid contracts、field schema、output schema、validation rules、Ollama-first route contract、blocked page guard 與 next machine action。此階段明確標記 `extraction_execution_performed=false`、`ocr_execution_performed=false`、`vlm_execution_performed=false`、`writes_database=false`、`writes_ai_insights=false`、`writes_price_tables=false`、`network_call=false`、`secret_read=false`、`primary_human_gate_count=0`;ready receipt 才能進下一段 `run_ollama_first_vlm_replay_worker`,blocked receipt 只能進 platform probe 或 structured API 策略。 | | 2026-06-29 | PChome DB apply 授權 lane 必須先通過 no-write guard / decision preflight / decision closeout / issuer gate / signing-decision preflight / signing-decision closeout / signing-issuer guard | V10.725 的 PChome mapping backlog auto-policy 已新增 `/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-lane-guard`、`/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-decision-preflight`、`/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-decision-closeout`、`/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-issuer-gate`、`/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-signing-decision-preflight`、`/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-signing-decision-closeout` 與 `/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-signing-issuer-guard`;這些 endpoint 只驗證 final exact request package、same-run production truth requirement、secret rejection、rollback boundary、lane entry requirements、decision input requirements、rejection policy、post-apply verifier、future authorization decision package、final nonsecret authorization envelope、signing decision preflight inputs、unsigned signing decision package 與 signable request boundary,不讀 secret、不執行 shell/SQL、不寫 DB,也不簽發 database apply authorization。 | | 2026-06-29 | PChome DB apply 授權簽署發行者 lane 必須先產出 final signable request package | V10.725 的 PChome mapping backlog auto-policy 新增 `/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-signing-issuer-closeout`;此 endpoint 只把 signing-issuer guard 的 signable request boundary 收斂成 final signable request package 與 closeout contract,確認 fresh production truth、post-apply verifier、migration hash、secret boundary 與 no-side-effect checks,不讀 secret、不簽發 authorization、不執行 shell/SQL、不寫 DB,也不代表正式 DB apply 已授權。 | | 2026-06-29 | PChome DB apply 授權簽署執行 lane 必須先通過 operator-held secret boundary preflight | V10.725 的 PChome mapping backlog auto-policy 新增 `/api/ai/pchome-growth/mapping-backlog/auto-policy-db-apply-authorization-signing-execution-preflight`;此 endpoint 只把 final signable request package 轉成 future signing execution preflight package、operator-held secret boundary contract、nonsecret signing inputs、command-shape preview、rollback boundary 與 abort conditions,不讀 secret、不接受 plaintext secret、不簽發 authorization、不執行 shell/SQL、不寫 DB,也不代表正式 DB apply 已授權。 | diff --git a/docs/guides/ai_automation_mainline_work_items.md b/docs/guides/ai_automation_mainline_work_items.md index d93ca16..4797b81 100644 --- a/docs/guides/ai_automation_mainline_work_items.md +++ b/docs/guides/ai_automation_mainline_work_items.md @@ -16,15 +16,16 @@ |---|---|---| | Completed | Multi-commerce PixelRAG visual evidence lane for momo, pchome, shopee_tw, coupang_tw, yahoo_shopping_tw, etmall_tw, friday_tw, rakuten_tw | API and CLI can emit expansion plans and marketplace manifests; PChome parser failures can queue manifests. | | Completed | External MCP/RAG capability inventory absorbed into internal governance readback | `/api/ai-automation/external-mcp-rag-integration` and `scripts/ops/report_external_mcp_rag_integration.py` expose 9 capabilities, absorbed/unresolved counts, and runtime flags. | -| In progress | PixelRAG receipts to internal RAG candidate replay | `/api/ai-automation/pixelrag-rag-candidate-replay` and `scripts/ops/report_pixelrag_rag_candidate_replay.py` read receipts, split eligible vs blocked, and require OCR/VLM replay plus PromotionGate before knowledge writes. | -| In progress | MCP/RAG runtime health in AI automation smoke | `/api/ai-automation/smoke` and `/api/ai-automation/scheduled-health-summary` include external MCP/RAG integration and PixelRAG RAG candidate replay families. | +| Completed | PixelRAG receipts to internal RAG candidate replay | `/api/ai-automation/pixelrag-rag-candidate-replay` and `scripts/ops/report_pixelrag_rag_candidate_replay.py` read receipts, split eligible vs blocked, and require OCR/VLM replay plus PromotionGate before knowledge writes. | +| In progress | MCP/RAG runtime health in AI automation smoke | `/api/ai-automation/smoke` and `/api/ai-automation/scheduled-health-summary` include external MCP/RAG integration, PixelRAG RAG candidate replay, and PixelRAG OCR/VLM replay contract families. | | In progress | Formal production deploy/readback discipline | Every mainline change must update version, push Gitea main/dev, deploy to 188 without touching `momo-db`, and read back `/health` plus new endpoints. | ## P1 | Status | Work item | Evidence / next machine action | |---|---|---| -| Not started | OCR/VLM replay contract for visual fields | Build a no-write replay that extracts title, price, promo badge, seller, rating, delivery badge from saved tiles and emits confidence/evidence only. | +| Completed | OCR/VLM replay contract for visual fields | `/api/ai-automation/pixelrag-ocr-vlm-replay` and `scripts/ops/report_pixelrag_ocr_vlm_replay.py` turn saved tiles into no-write field contracts, output schemas, validation rules, and Ollama-first worker actions. | +| Not started | Ollama-first OCR/VLM extraction worker | Execute the replay contract against approved local multimodal/OCR models and emit confidence/evidence receipts without writing formal price truth. | | Not started | Ollama-first multimodal embedding benchmark | Verify local Qwen3-VL or equivalent visual embedding on GCP-A -> GCP-B -> 111 before any visual vector retrieval. | | Not started | pgvector-compatible visual evidence metadata | Design metadata-first retrieval without FAISS in production unless ADR approves a different store. | | Not started | Coupang platform probe / structured API strategy | Treat 403 as platform barrier; prefer public structured data or approved probe, never count blocked pages as product data. | diff --git a/docs/guides/ai_automation_session_sop.md b/docs/guides/ai_automation_session_sop.md index 69733c7..5454311 100644 --- a/docs/guides/ai_automation_session_sop.md +++ b/docs/guides/ai_automation_session_sop.md @@ -30,7 +30,9 @@ 或 `python scripts/ops/report_external_mcp_rag_integration.py` 可讀回每個能力的內部落點、狀態、資料邊界與下一個機器動作。 - PixelRAG 視覺 receipt 導入內部 RAG 時,必須確認 `/api/ai-automation/pixelrag-rag-candidate-replay` 或 `python scripts/ops/report_pixelrag_rag_candidate_replay.py` 可讀回 eligible / blocked / invalid receipts,且 blocked page 不得被視為商品資料。 -- AI automation smoke 必須包含 external MCP/RAG integration 與 PixelRAG RAG candidate replay family,避免 registry 已完成但 runtime flag / receipt replay 未完成時被誤報為全自動閉環。 +- PixelRAG 視覺 receipt 進入 OCR/VLM 自動化前,必須確認 `/api/ai-automation/pixelrag-ocr-vlm-replay` + 或 `python scripts/ops/report_pixelrag_ocr_vlm_replay.py` 可讀回 ready / blocked / invalid replay contracts、field schema、validation rules 與 Ollama-first 下一步,且目前不執行 OCR/VLM、不寫正式價格。 +- AI automation smoke 必須包含 external MCP/RAG integration、PixelRAG RAG candidate replay 與 PixelRAG OCR/VLM replay contract family,避免 registry 已完成但 runtime flag / receipt replay / VLM contract 未完成時被誤報為全自動閉環。 - AI 自動化 Prometheus 指標變更必須同步檢查 `docker/grafana/provisioning/dashboards/json/ai-automation-overview.json` 是否需要新增 panel 或查詢。 - 188 線上 active monitoring stack 以 `monitoring/prometheus.yml` 為準;110 gateway 另有 `/home/wooo/monitoring/prometheus.yml`。若 dashboard 無資料,先確認 Prometheus `momo-app` target 與 `momo-network` 連線;所有 Blackbox HTTP target 必須打 `/health`,不可打 Dashboard 首頁 `/`。 - Smoke dashboard 會保存 JSONL 趨勢;若新增檢查項目,要確保 history compact record 仍保持小而可讀。 diff --git a/docs/guides/browse_sh_crawler_playbook.md b/docs/guides/browse_sh_crawler_playbook.md index 0e8f8f0..7f914df 100644 --- a/docs/guides/browse_sh_crawler_playbook.md +++ b/docs/guides/browse_sh_crawler_playbook.md @@ -171,6 +171,15 @@ python scripts/ops/report_pixelrag_rag_candidate_replay.py --platform shopee_tw 此 replay 只把 receipt 轉成內部 RAG candidate preview;eligible receipt 仍需 OCR/VLM replay、identity matcher replay、PromotionGate 與 embedding signature guard,blocked / 403 / captcha / access denied receipt 只能進 platform probe 或 structured API 策略,不得當成商品資料。 +OCR/VLM replay contract: + +```bash +python scripts/ops/report_pixelrag_ocr_vlm_replay.py +python scripts/ops/report_pixelrag_ocr_vlm_replay.py --platform shopee_tw --platform coupang_tw +``` + +此 contract 只把 saved tiles、欄位 schema、輸出 schema、confidence/evidence 規則與 Ollama-first worker 下一步打包;目前不執行 OCR/VLM、不呼叫模型、不寫 RAG、不寫 `ai_insights`、不寫正式價格表。ready receipt 進 `run_ollama_first_vlm_replay_worker`,blocked / 403 / captcha / access denied receipt 進 platform probe 或 structured API 策略。 + 安全邊界: - read-only;不登入、不下單、不加入購物車、不寫第三方狀態。 diff --git a/routes/system_public_routes.py b/routes/system_public_routes.py index 5f61ccb..5eb73e7 100644 --- a/routes/system_public_routes.py +++ b/routes/system_public_routes.py @@ -711,6 +711,28 @@ def ai_automation_pixelrag_rag_candidate_replay_api(): )) +@system_public_bp.route('/api/ai-automation/pixelrag-ocr-vlm-replay') +@login_required +def ai_automation_pixelrag_ocr_vlm_replay_api(): + """Read-only PixelRAG OCR/VLM replay contract readback.""" + from services.pixelrag_ocr_vlm_replay_service import ( + build_pixelrag_ocr_vlm_replay_contract, + ) + + platforms = tuple( + str(item or '').strip() + for item in request.args.getlist('platform') + if str(item or '').strip() + ) + max_age_hours = request.args.get('max_age_hours', 168, type=int) + limit = request.args.get('limit', 50, type=int) + return jsonify(build_pixelrag_ocr_vlm_replay_contract( + platform=platforms, + max_age_hours=max(1, min(max_age_hours or 168, 720)), + limit=max(1, min(limit or 50, 250)), + )) + + @system_public_bp.route('/api/ai-automation/external-mcp-rag-integration') @login_required def ai_automation_external_mcp_rag_integration_api(): diff --git a/scripts/ops/report_pixelrag_ocr_vlm_replay.py b/scripts/ops/report_pixelrag_ocr_vlm_replay.py new file mode 100755 index 0000000..a706e52 --- /dev/null +++ b/scripts/ops/report_pixelrag_ocr_vlm_replay.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +"""Report PixelRAG receipts as OCR/VLM replay contracts.""" + +from __future__ import annotations + +import argparse +import json +import sys +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[2] +if str(ROOT) not in sys.path: + sys.path.insert(0, str(ROOT)) + +from services.pixelrag_ocr_vlm_replay_service import ( # noqa: E402 + build_pixelrag_ocr_vlm_replay_contract, +) + + +def main() -> int: + parser = argparse.ArgumentParser( + description="輸出 PixelRAG receipt 的 no-write OCR/VLM replay contract。" + ) + parser.add_argument( + "--artifact-root", + help="PixelRAG visual evidence artifact root;預設使用 production/container 設定。", + ) + parser.add_argument( + "--platform", + action="append", + dest="platforms", + help="限制平台,可重複指定,例如 --platform shopee_tw --platform coupang_tw。", + ) + parser.add_argument( + "--max-age-hours", + type=int, + default=168, + help="receipt 最大新鮮度小時數。", + ) + parser.add_argument( + "--limit", + type=int, + default=50, + help="最多輸出 receipt contract 數。", + ) + args = parser.parse_args() + + payload = build_pixelrag_ocr_vlm_replay_contract( + artifact_root=args.artifact_root, + platform=tuple(args.platforms or ()), + max_age_hours=args.max_age_hours, + limit=args.limit, + ) + print(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True)) + return 0 if payload.get("success") else 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/services/ai_automation_smoke_service.py b/services/ai_automation_smoke_service.py index 31d5715..88990f8 100644 --- a/services/ai_automation_smoke_service.py +++ b/services/ai_automation_smoke_service.py @@ -537,6 +537,11 @@ def build_scheduled_automation_health_summary( if not pixelrag_rag_replay or not pixelrag_rag_replay_details: pixelrag_rag_replay = _pixelrag_rag_candidate_replay_check() pixelrag_rag_replay_details = pixelrag_rag_replay.get("details") or {} + pixelrag_ocr_vlm_replay = _find_check(source_result, "PixelRAG OCR/VLM replay contract") + pixelrag_ocr_vlm_replay_details = pixelrag_ocr_vlm_replay.get("details") or {} + if not pixelrag_ocr_vlm_replay or not pixelrag_ocr_vlm_replay_details: + pixelrag_ocr_vlm_replay = _pixelrag_ocr_vlm_replay_check() + pixelrag_ocr_vlm_replay_details = pixelrag_ocr_vlm_replay.get("details") or {} smoke_status = source_result.get("status") or ("warning" if latest_history else "warning") freshness_family = _history_freshness_family( latest_history, @@ -4592,6 +4597,41 @@ def build_scheduled_automation_health_summary( "primary_human_gate_count": 0, }, }, + { + "key": "pixelrag_ocr_vlm_replay", + "label": "PixelRAG OCR/VLM replay contract", + "status": pixelrag_ocr_vlm_replay.get("status") or "warning", + "summary": ( + pixelrag_ocr_vlm_replay.get("summary") + or "PixelRAG OCR/VLM replay contract has no latest readback." + ), + "next_machine_action": pixelrag_ocr_vlm_replay_details.get("next_machine_action") + or "run_pixelrag_ocr_vlm_replay_contract_readback", + "details": { + "policy": pixelrag_ocr_vlm_replay_details.get("policy"), + "receipt_count": int(pixelrag_ocr_vlm_replay_details.get("receipt_count") or 0), + "replay_ready_count": int( + pixelrag_ocr_vlm_replay_details.get("replay_ready_count") or 0 + ), + "blocked_count": int(pixelrag_ocr_vlm_replay_details.get("blocked_count") or 0), + "invalid_count": int(pixelrag_ocr_vlm_replay_details.get("invalid_count") or 0), + "tile_input_count": int( + pixelrag_ocr_vlm_replay_details.get("tile_input_count") or 0 + ), + "field_contract_count": int( + pixelrag_ocr_vlm_replay_details.get("field_contract_count") or 0 + ), + "extraction_execution_performed": bool( + pixelrag_ocr_vlm_replay_details.get("extraction_execution_performed") + ), + "blocked_pages_are_not_product_data": bool( + pixelrag_ocr_vlm_replay_details.get("blocked_pages_are_not_product_data") + ), + "writes_database": False, + "writes_database_count": 0, + "primary_human_gate_count": 0, + }, + }, freshness_family, { "key": "daily_summary_delivery", @@ -13252,6 +13292,64 @@ def _pixelrag_rag_candidate_replay_check() -> Dict[str, Any]: ) +def _pixelrag_ocr_vlm_replay_check() -> Dict[str, Any]: + """Read-only sentinel for PixelRAG receipts becoming OCR/VLM replay contracts.""" + try: + from services.pixelrag_ocr_vlm_replay_service import ( + build_pixelrag_ocr_vlm_replay_contract, + ) + + readback = build_pixelrag_ocr_vlm_replay_contract() + summary = readback.get("summary") or {} + receipt_count = int(summary.get("receipt_count") or 0) + replay_ready_count = int(summary.get("replay_ready_count") or 0) + blocked_count = int(summary.get("blocked_count") or 0) + invalid_count = int(summary.get("invalid_count") or 0) + field_contract_count = int(summary.get("field_contract_count") or 0) + status = readback.get("status") or "warning" + summary_text = ( + f"PixelRAG OCR/VLM replay contract receipts={receipt_count}, " + f"ready={replay_ready_count}, blocked={blocked_count}, invalid={invalid_count}, " + f"field_contracts={field_contract_count}" + ) + return _check( + "PixelRAG OCR/VLM replay contract", + status, + summary_text, + { + "policy": readback.get("policy"), + "receipt_count": receipt_count, + "replay_ready_count": replay_ready_count, + "blocked_count": blocked_count, + "invalid_count": invalid_count, + "tile_input_count": int(summary.get("tile_input_count") or 0), + "missing_tile_count": int(summary.get("missing_tile_count") or 0), + "field_contract_count": field_contract_count, + "extraction_execution_performed": bool( + summary.get("extraction_execution_performed") + ), + "blocked_pages_are_not_product_data": bool( + (readback.get("replay_contract") or {}).get("blocked_pages_are_not_product_data") + ), + "next_machine_action": readback.get("next_machine_action"), + "writes_database": False, + "writes_database_count": 0, + "primary_human_gate_count": 0, + }, + ) + except Exception as exc: + return _check( + "PixelRAG OCR/VLM replay contract", + "critical", + f"PixelRAG OCR/VLM replay contract 無法執行:{exc}", + { + "writes_database": False, + "writes_database_count": 0, + "primary_human_gate_count": 0, + }, + ) + + def collect_ai_automation_smoke(*, record_history: bool = True, history_limit: int = 20) -> Dict[str, Any]: checks: List[Dict[str, Any]] = [ _event_router_check(), @@ -13287,6 +13385,7 @@ def collect_ai_automation_smoke(*, record_history: bool = True, history_limit: i _sitewide_visual_qa_check(), _external_mcp_rag_integration_check(), _pixelrag_rag_candidate_replay_check(), + _pixelrag_ocr_vlm_replay_check(), ] worst = max(checks, key=lambda item: STATUS_RANK.get(item["status"], 2))["status"] result = { diff --git a/services/pixelrag_ocr_vlm_replay_service.py b/services/pixelrag_ocr_vlm_replay_service.py new file mode 100644 index 0000000..16891bb --- /dev/null +++ b/services/pixelrag_ocr_vlm_replay_service.py @@ -0,0 +1,354 @@ +"""Read-only PixelRAG OCR/VLM replay contract builder. + +This module turns PixelRAG visual evidence receipts into a machine-readable +contract for an Ollama-first OCR/VLM worker. It does not execute OCR, call a +model, write RAG data, write price tables, or promote visual fields. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +from pathlib import Path +from typing import Any, Mapping + +from services.pixelrag_crawler_integration_service import ( + DEFAULT_ARTIFACT_MAX_AGE_HOURS, + DEFAULT_ARTIFACT_ROOT, + ECOMMERCE_PLATFORM_PROFILES, +) +from services.pixelrag_rag_candidate_replay_service import ( + build_pixelrag_rag_candidate_replay_readback, +) + + +POLICY = "read_only_pixelrag_ocr_vlm_replay_contract_v1" +DEFAULT_LIMIT = 50 +DEFAULT_CONFIDENCE_THRESHOLD = 0.72 + +FIELD_ALIASES: dict[str, dict[str, str]] = { + "title": {"type": "string", "required": "true", "evidence": "tile_text_or_card_region"}, + "price": {"type": "money", "required": "true", "evidence": "price_region"}, + "currency": {"type": "string", "required": "false", "evidence": "price_region"}, + "bundle_spec": {"type": "string", "required": "false", "evidence": "spec_or_bundle_region"}, + "promo_badge": {"type": "string", "required": "false", "evidence": "badge_region"}, + "campaign_badge": {"type": "string", "required": "false", "evidence": "badge_region"}, + "stock_hint": {"type": "string", "required": "false", "evidence": "availability_region"}, + "image": {"type": "asset_ref", "required": "false", "evidence": "screenshot_or_tile"}, + "product_id": {"type": "string", "required": "false", "evidence": "url_or_card_region"}, + "shop": {"type": "string", "required": "false", "evidence": "seller_region"}, + "sold_count": {"type": "number_or_label", "required": "false", "evidence": "social_proof_region"}, + "rating": {"type": "number_or_label", "required": "false", "evidence": "rating_region"}, + "shipping_badge": {"type": "string", "required": "false", "evidence": "shipping_region"}, + "discount": {"type": "string", "required": "false", "evidence": "discount_region"}, + "delivery_badge": {"type": "string", "required": "false", "evidence": "delivery_region"}, + "spec": {"type": "string", "required": "false", "evidence": "spec_region"}, + "review_count": {"type": "number_or_label", "required": "false", "evidence": "review_region"}, + "coupon": {"type": "string", "required": "false", "evidence": "coupon_region"}, + "sales_hint": {"type": "string", "required": "false", "evidence": "social_proof_region"}, + "member_price": {"type": "money", "required": "false", "evidence": "member_price_region"}, + "shipping_hint": {"type": "string", "required": "false", "evidence": "shipping_region"}, + "reward_badge": {"type": "string", "required": "false", "evidence": "reward_region"}, + "point_reward": {"type": "string", "required": "false", "evidence": "reward_region"}, + "public_page_visual_evidence": { + "type": "evidence_summary", + "required": "true", + "evidence": "visible_page_regions", + }, +} + + +def _normalise_platforms( + platform: str | tuple[str, ...] | list[str] | None, +) -> tuple[str, ...] | None: + if isinstance(platform, str): + return (platform.strip().lower(),) if platform.strip() else None + selected = tuple( + str(item or "").strip().lower() + for item in (platform or []) + if str(item or "").strip() + ) + return selected or None + + +def _tile_inputs(candidate: Mapping[str, Any]) -> list[dict[str, Any]]: + files = ((candidate.get("file_summary") or {}).get("files") or []) + tiles: list[dict[str, Any]] = [] + for index, item in enumerate(files): + if item.get("kind") != "tile": + continue + path = str(item.get("path") or "").strip() + tiles.append({ + "index": index, + "kind": "tile", + "path": path, + "exists": bool(item.get("exists")), + "evidence_ref": f"tile:{index}", + }) + return tiles + + +def _field_contract(platform: str) -> list[dict[str, Any]]: + profile = ECOMMERCE_PLATFORM_PROFILES.get(platform) or {} + fields = [str(item) for item in list(profile.get("data_targets") or []) if str(item)] + if "price" in fields and "currency" not in fields: + fields.insert(fields.index("price") + 1, "currency") + if not fields: + fields = ["public_page_visual_evidence"] + contracts: list[dict[str, Any]] = [] + for field in fields: + base = FIELD_ALIASES.get(field) or { + "type": "string", + "required": "false", + "evidence": "visible_region", + } + contracts.append({ + "field": field, + "type": base["type"], + "required": base["required"] == "true", + "evidence_requirement": base["evidence"], + "min_confidence": DEFAULT_CONFIDENCE_THRESHOLD if field in {"title", "price"} else 0.62, + "allow_empty": base["required"] != "true", + }) + return contracts + + +def _output_schema(fields: list[dict[str, Any]]) -> dict[str, Any]: + return { + "platform": "string", + "manifest_id": "string", + "source_receipt_path": "string", + "blocked_page_detected": "boolean", + "quality": { + "overall_confidence": "float_0_1", + "field_confidence_min": "float_0_1", + "missing_required_fields": "list[string]", + "requires_identity_matcher_replay": "boolean", + "requires_promotion_gate": "boolean", + }, + "fields": { + item["field"]: { + "value": item["type"], + "confidence": "float_0_1", + "evidence_refs": "list[string]", + } + for item in fields + }, + "next_machine_action": "string", + } + + +def _validation_rules() -> list[str]: + return [ + "blocked_or_access_denied_page_must_not_emit_product_fields", + "every_field_value_requires_at_least_one_tile_evidence_ref", + "required_title_and_price_must_meet_min_confidence_before_promotion_gate", + "price_must_parse_to_numeric_money_before_any_price_candidate", + "currency_defaults_to_TWD_only_when_page_locale_or_symbol_supports_it", + "identity_matcher_replay_required_before_offer_matching", + "promotion_gate_required_before_internal_rag_or_ai_insights_write", + "direct_competitor_prices_write_forbidden", + "direct_competitor_price_history_write_forbidden", + "direct_ai_insights_write_forbidden", + ] + + +def _model_route_contract() -> dict[str, Any]: + return { + "runtime_policy": "ollama_first_no_hosted_vlm_default", + "preferred_chain": [ + {"host": "34.87.90.216:11434", "role": "gcp_a_primary"}, + {"host": "34.21.145.224:11434", "role": "gcp_b_secondary"}, + {"host": "192.168.0.111:11434", "role": "lan_final_fallback"}, + ], + "production_app_host_is_not_ollama_node": "192.168.0.188", + "hosted_vlm_api_allowed": False, + "gemini_default_allowed": False, + "secret_read_allowed": False, + "network_call_performed_by_this_contract": False, + } + + +def _candidate_contract(candidate: Mapping[str, Any]) -> dict[str, Any]: + platform = str(candidate.get("platform") or "unknown") + fields = _field_contract(platform) + tiles = _tile_inputs(candidate) + blocked_reasons = list(candidate.get("blocked_reasons") or []) + candidate_status = str(candidate.get("candidate_status") or "unknown") + ready = candidate_status == "eligible" and bool(tiles) + missing_tile_count = sum(1 for item in tiles if not item["exists"]) + if candidate_status == "eligible" and not tiles: + blocked_reasons.append("no_tile_inputs_available") + if missing_tile_count: + blocked_reasons.append("tile_input_missing_files") + if candidate_status == "invalid": + replay_status = "invalid" + elif ready and not missing_tile_count: + replay_status = "ready_for_ocr_vlm_replay" + else: + replay_status = "blocked" + + return { + "replay_status": replay_status, + "ready_for_ollama_vlm_worker": replay_status == "ready_for_ocr_vlm_replay", + "candidate_status": candidate_status, + "blocked_reasons": blocked_reasons, + "platform": platform, + "manifest_id": candidate.get("manifest_id"), + "source_receipt_path": candidate.get("receipt_path"), + "url": candidate.get("url"), + "title_hint": candidate.get("title"), + "http_status": candidate.get("http_status"), + "visual_barrier_detected": bool(candidate.get("visual_barrier_detected")), + "visual_barrier_reason": candidate.get("visual_barrier_reason"), + "input_tiles": tiles, + "field_contract": fields, + "field_contract_count": len(fields), + "output_schema": _output_schema(fields), + "validation_rules": _validation_rules(), + "prompt_contract": { + "task": "extract_public_marketplace_offer_card_fields_from_tiles", + "language": "zh-TW", + "allowed_inputs": ["tile images", "receipt metadata", "field contract"], + "forbidden_inputs": ["secrets", "cookies", "sessions", "private account pages"], + "instruction": ( + "Return only fields supported by visible tile evidence; mark unknown " + "instead of guessing, and set blocked_page_detected=true for access " + "denied, captcha, login, or traffic verification pages." + ), + }, + "automation_boundary": { + "ocr_execution_performed": False, + "vlm_execution_performed": False, + "writes_database": False, + "writes_ai_insights": False, + "writes_price_tables": False, + "network_call": False, + "secret_read": False, + }, + "next_machine_action": ( + "run_ollama_first_vlm_replay_worker" + if replay_status == "ready_for_ocr_vlm_replay" + else ( + "run_platform_probe_or_use_structured_api" + if candidate.get("visual_barrier_detected") or candidate.get("http_status", 0) >= 400 + else "refresh_pixelrag_visual_capture_receipt" + ) + ), + } + + +def build_pixelrag_ocr_vlm_replay_contract( + *, + artifact_root: str | Path | None = None, + platform: str | tuple[str, ...] | list[str] | None = None, + max_age_hours: int | None = None, + limit: int | None = None, +) -> dict[str, Any]: + """Build a no-write OCR/VLM replay contract from PixelRAG receipts.""" + root = Path(artifact_root or DEFAULT_ARTIFACT_ROOT) + max_age = max(1, int(max_age_hours or DEFAULT_ARTIFACT_MAX_AGE_HOURS)) + item_limit = max(1, min(int(limit or DEFAULT_LIMIT), 250)) + platforms = _normalise_platforms(platform) + generated_at = datetime.now(timezone.utc).isoformat() + + candidate_readback = build_pixelrag_rag_candidate_replay_readback( + artifact_root=root, + platform=platforms, + max_age_hours=max_age, + limit=item_limit, + ) + replay_items = [ + _candidate_contract(candidate) + for candidate in list(candidate_readback.get("candidates") or []) + ] + ready_count = sum(1 for item in replay_items if item["replay_status"] == "ready_for_ocr_vlm_replay") + blocked_count = sum(1 for item in replay_items if item["replay_status"] == "blocked") + invalid_count = sum(1 for item in replay_items if item["replay_status"] == "invalid") + tile_input_count = sum(len(item.get("input_tiles") or []) for item in replay_items) + field_contract_count = sum(int(item.get("field_contract_count") or 0) for item in replay_items) + missing_tile_count = sum( + 1 + for item in replay_items + for tile in list(item.get("input_tiles") or []) + if not tile.get("exists") + ) + + if invalid_count and invalid_count == len(replay_items): + status = "critical" + elif not replay_items or ready_count == 0 or blocked_count or invalid_count or missing_tile_count: + status = "warning" + else: + status = "ok" + + summary = { + "receipt_count": len(replay_items), + "replay_ready_count": ready_count, + "blocked_count": blocked_count, + "invalid_count": invalid_count, + "tile_input_count": tile_input_count, + "missing_tile_count": missing_tile_count, + "field_contract_count": field_contract_count, + "extraction_execution_performed": False, + "ocr_execution_performed": False, + "vlm_execution_performed": False, + "platforms": sorted({str(item.get("platform") or "unknown") for item in replay_items}), + } + + return { + "success": invalid_count == 0, + "policy": POLICY, + "status": status, + "generated_at": generated_at, + "artifact_root": str(root), + "max_age_hours": max_age, + "platform_filter": list(platforms or []), + "summary": summary, + "replay_items": replay_items, + "source_candidate_replay": { + "policy": candidate_readback.get("policy"), + "status": candidate_readback.get("status"), + "summary": candidate_readback.get("summary"), + }, + "model_route_contract": _model_route_contract(), + "replay_contract": { + "automation_mode": "no_write_ocr_vlm_replay_package", + "writes_database": False, + "writes_ai_insights": False, + "writes_price_tables": False, + "network_call": False, + "secret_read": False, + "extraction_execution_performed": False, + "blocked_pages_are_not_product_data": True, + "requires_identity_matcher_replay": True, + "requires_promotion_gate": True, + "requires_embedding_signature_guard": True, + }, + "next_machine_action": ( + "run_pixelrag_visual_capture_worker" + if not replay_items + else ( + "fix_invalid_pixelrag_receipts" + if invalid_count and not ready_count + else ( + "run_ollama_first_vlm_replay_worker" + if ready_count + else "run_platform_probe_or_use_structured_api" + ) + ) + ), + "controlled_apply": { + "network_call": False, + "db_write": False, + "secret_read": False, + "production_price_write": False, + "artifact_write": False, + "primary_human_gate_count": 0, + }, + } + + +__all__ = [ + "DEFAULT_CONFIDENCE_THRESHOLD", + "POLICY", + "build_pixelrag_ocr_vlm_replay_contract", +] diff --git a/tests/test_ai_automation_smoke_service.py b/tests/test_ai_automation_smoke_service.py index 3a7bc6b..c3fcc76 100644 --- a/tests/test_ai_automation_smoke_service.py +++ b/tests/test_ai_automation_smoke_service.py @@ -1433,11 +1433,12 @@ def test_collect_ai_automation_smoke_uses_worst_status(monkeypatch): monkeypatch.setattr(smoke, "_sitewide_visual_qa_check", lambda: smoke._check("visual", "ok", "ok")) monkeypatch.setattr(smoke, "_external_mcp_rag_integration_check", lambda: smoke._check("external mcp rag", "ok", "ok")) monkeypatch.setattr(smoke, "_pixelrag_rag_candidate_replay_check", lambda: smoke._check("pixelrag replay", "ok", "ok")) + monkeypatch.setattr(smoke, "_pixelrag_ocr_vlm_replay_check", lambda: smoke._check("pixelrag ocr vlm", "ok", "ok")) result = smoke.collect_ai_automation_smoke(record_history=False) assert result["status"] == "critical" - assert result["summary"] == {"ok": 31, "warning": 1, "critical": 1, "total": 33} + assert result["summary"] == {"ok": 32, "warning": 1, "critical": 1, "total": 34} def test_pchome_controlled_apply_drift_monitor_reports_verified_zero_drift(monkeypatch): @@ -3966,6 +3967,7 @@ def test_collect_ai_automation_smoke_persists_recent_history(tmp_path, monkeypat monkeypatch.setattr(smoke, "_sitewide_visual_qa_check", lambda: smoke._check("visual", "ok", "ok")) monkeypatch.setattr(smoke, "_external_mcp_rag_integration_check", lambda: smoke._check("external mcp rag", "ok", "ok")) monkeypatch.setattr(smoke, "_pixelrag_rag_candidate_replay_check", lambda: smoke._check("pixelrag replay", "ok", "ok")) + monkeypatch.setattr(smoke, "_pixelrag_ocr_vlm_replay_check", lambda: smoke._check("pixelrag ocr vlm", "ok", "ok")) first = smoke.collect_ai_automation_smoke(history_limit=5) second = smoke.collect_ai_automation_smoke(history_limit=5) @@ -4021,7 +4023,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": 33, "warning": 0, "critical": 0, "total": 33}, + "summary": {"ok": 34, "warning": 0, "critical": 0, "total": 34}, "checks": [ { "name": "PChome 受控落地 drift monitor", @@ -4129,6 +4131,23 @@ def test_scheduled_automation_health_summary_reads_history_without_side_effects( "blocked_pages_are_not_product_data": True, "next_machine_action": "run_ocr_vlm_replay_then_promotion_gate", }, + }, + { + "name": "PixelRAG OCR/VLM replay contract", + "status": "ok", + "summary": "PixelRAG OCR/VLM replay contract receipts=1, ready=1, blocked=0, invalid=0, field_contracts=7", + "details": { + "policy": "read_only_pixelrag_ocr_vlm_replay_contract_v1", + "receipt_count": 1, + "replay_ready_count": 1, + "blocked_count": 0, + "invalid_count": 0, + "tile_input_count": 4, + "field_contract_count": 7, + "extraction_execution_performed": False, + "blocked_pages_are_not_product_data": True, + "next_machine_action": "run_ollama_first_vlm_replay_worker", + }, } ], }, ensure_ascii=False) + "\n", @@ -4147,7 +4166,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"] == 30 + assert summary["summary"]["total"] == 31 assert summary["summary"]["primary_human_gate_count"] == 0 assert summary["summary"]["writes_database_count"] == 0 assert pchome_family["status"] == "ok" @@ -5823,6 +5842,13 @@ def test_scheduled_automation_health_summary_reads_history_without_side_effects( ) assert pixelrag_rag_replay_family["status"] == "ok" assert pixelrag_rag_replay_family["details"]["eligible_count"] == 1 + pixelrag_ocr_vlm_replay_family = next( + item for item in summary["families"] + if item["key"] == "pixelrag_ocr_vlm_replay" + ) + assert pixelrag_ocr_vlm_replay_family["status"] == "ok" + assert pixelrag_ocr_vlm_replay_family["details"]["replay_ready_count"] == 1 + assert pixelrag_ocr_vlm_replay_family["details"]["extraction_execution_performed"] is False assert summary["scheduled_outputs"]["telegram_send_in_preview"] is False assert summary["scheduled_outputs"]["writes_database"] is False assert summary["automation_policy"]["primary_flow"] == "ai_controlled" @@ -6439,6 +6465,11 @@ def test_surface_html_readback_check_is_part_of_ai_smoke(monkeypatch): "ok", "pixelrag replay ok", )) + monkeypatch.setattr(smoke, "_pixelrag_ocr_vlm_replay_check", lambda: smoke._check( + "PixelRAG OCR/VLM replay contract", + "ok", + "pixelrag ocr vlm ok", + )) result = smoke.collect_ai_automation_smoke(record_history=False) @@ -6454,7 +6485,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"] == 33 + assert result["summary"]["total"] == 34 assert surface_check["status"] == "ok" assert surface_check["details"]["checked_surface_count"] == 10 assert sitewide_check["status"] == "ok" diff --git a/tests/test_pixelrag_ocr_vlm_replay_service.py b/tests/test_pixelrag_ocr_vlm_replay_service.py new file mode 100644 index 0000000..8889934 --- /dev/null +++ b/tests/test_pixelrag_ocr_vlm_replay_service.py @@ -0,0 +1,144 @@ +import json +import subprocess +import sys +from datetime import datetime, timezone + + +def _write_receipt(root, *, platform, manifest_id, title, url, http_status=200): + receipt_dir = root / platform / manifest_id + tiles_dir = receipt_dir / "tiles" + tiles_dir.mkdir(parents=True) + screenshot = receipt_dir / "screenshot.png" + screenshot.write_bytes(b"png") + files = [{"kind": "screenshot", "path": str(screenshot)}] + for index in range(4): + tile = tiles_dir / f"tile-{index}.png" + tile.write_bytes(b"tile") + files.append({"kind": "tile", "path": str(tile)}) + receipt = { + "generated_at": datetime.now(timezone.utc).isoformat(), + "status": "captured", + "manifest_id": manifest_id, + "http_status": http_status, + "capture_target": { + "platform": platform, + "url": url, + "crawler": "PixelRAGMarketplaceSearch.visual_fallback", + }, + "page_metrics": { + "title": title, + "final_url": url, + }, + "tile_plan": { + "planned_tile_count": 4, + "emitted_tile_count": 4, + }, + "files": files, + } + path = receipt_dir / "capture_receipt.json" + path.write_text(json.dumps(receipt, ensure_ascii=False), encoding="utf-8") + return path + + +def test_pixelrag_ocr_vlm_replay_contract_splits_ready_and_blocked_receipts(tmp_path): + from services.pixelrag_ocr_vlm_replay_service import ( + POLICY, + build_pixelrag_ocr_vlm_replay_contract, + ) + + _write_receipt( + tmp_path, + platform="shopee_tw", + manifest_id="shopee-ok", + title="Shopee 防曬乳", + url="https://shopee.tw/search?keyword=%E9%98%B2%E6%9B%AC%E4%B9%B3", + ) + _write_receipt( + tmp_path, + platform="coupang_tw", + manifest_id="coupang-403", + title="Access Denied", + url="https://www.tw.coupang.com/search?q=iphone", + http_status=403, + ) + + payload = build_pixelrag_ocr_vlm_replay_contract(artifact_root=tmp_path) + by_platform = {item["platform"]: item for item in payload["replay_items"]} + + assert payload["policy"] == POLICY + assert payload["status"] == "warning" + assert payload["summary"]["receipt_count"] == 2 + assert payload["summary"]["replay_ready_count"] == 1 + assert payload["summary"]["blocked_count"] == 1 + assert payload["summary"]["extraction_execution_performed"] is False + assert by_platform["shopee_tw"]["replay_status"] == "ready_for_ocr_vlm_replay" + assert by_platform["shopee_tw"]["ready_for_ollama_vlm_worker"] is True + assert len(by_platform["shopee_tw"]["input_tiles"]) == 4 + assert "price" in { + field["field"] for field in by_platform["shopee_tw"]["field_contract"] + } + assert by_platform["coupang_tw"]["replay_status"] == "blocked" + assert by_platform["coupang_tw"]["next_machine_action"] == ( + "run_platform_probe_or_use_structured_api" + ) + assert payload["model_route_contract"]["hosted_vlm_api_allowed"] is False + assert payload["replay_contract"]["writes_price_tables"] is False + assert payload["controlled_apply"]["primary_human_gate_count"] == 0 + + +def test_pixelrag_ocr_vlm_replay_cli_outputs_machine_readable_json(tmp_path): + _write_receipt( + tmp_path, + platform="shopee_tw", + manifest_id="shopee-ok", + title="Shopee 防曬乳", + url="https://shopee.tw/search?keyword=sunscreen", + ) + + completed = subprocess.run( + [ + sys.executable, + "scripts/ops/report_pixelrag_ocr_vlm_replay.py", + "--artifact-root", + str(tmp_path), + "--platform", + "shopee_tw", + ], + capture_output=True, + check=False, + text=True, + ) + + assert completed.returncode == 0 + payload = json.loads(completed.stdout) + assert payload["summary"]["receipt_count"] == 1 + assert payload["summary"]["replay_ready_count"] == 1 + assert payload["replay_items"][0]["platform"] == "shopee_tw" + assert payload["replay_items"][0]["automation_boundary"]["vlm_execution_performed"] is False + + +def test_pixelrag_ocr_vlm_replay_route_returns_readback(tmp_path, monkeypatch): + from flask import Flask + from routes import system_public_routes as routes + from services import pixelrag_ocr_vlm_replay_service as service + + _write_receipt( + tmp_path, + platform="shopee_tw", + manifest_id="shopee-ok", + title="Shopee 防曬乳", + url="https://shopee.tw/search?keyword=sunscreen", + ) + monkeypatch.setattr(service, "DEFAULT_ARTIFACT_ROOT", str(tmp_path)) + + app = Flask(__name__) + with app.test_request_context( + "/api/ai-automation/pixelrag-ocr-vlm-replay?platform=shopee_tw" + ): + response = routes.ai_automation_pixelrag_ocr_vlm_replay_api.__wrapped__() + payload = response.get_json() + + assert payload["policy"] == "read_only_pixelrag_ocr_vlm_replay_contract_v1" + assert payload["summary"]["receipt_count"] == 1 + assert payload["summary"]["replay_ready_count"] == 1 + assert payload["replay_contract"]["writes_database"] is False