V10.495 add market intel queue writer preflight gate
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
================================================================================
|
||||
|
||||
【已完成】
|
||||
- V10.495 新增市場情報 MCP Fetch Candidate Queue Writer Preflight 安全預覽 gate:只審核 queue review 後的 writer preflight 草案,確認 target_table、write_mode、dedupe strategy、insert columns、payload rows 與候選 key 對齊;API 不開 DB、不執行 CLI、不建立 queue、不更新 review_state、不寫 DB、不連外、不掛 scheduler。
|
||||
- V10.494 新增市場情報 MCP Fetch Candidate Queue Review 安全預覽 gate:只審核 candidate handoff 後的人工 queue review 草案,要求候選 key 對齊、review_state 停在 needs_review、allowed actions 限定人工確認/否決/延後、queue_write_status 維持 not_persisted;API 不建立 queue、不更新 review_state、不寫 DB、不連外、不掛 scheduler。
|
||||
- V10.493 新增市場情報 MCP Fetch Candidate Handoff Review 安全預覽 gate:只審核 parser review 後的候選交接包,確認 source/candidate key 對齊、queue policy 仍是 manual preview、候選數維持小批次、無 raw/secret/side-effect;API 不建立 queue、不寫 DB、不讀 artifact、不連外、不掛 scheduler。
|
||||
- V10.492 收緊 PChome 近門檻自動回刷隊列:`retryable_candidate_revalidation` 不再把 `identity_veto`、`unit_comparable`、`true_low_confidence` 納入每日自動回刷;只處理 `recoverable_low_score` 與 legacy `low_score / refresh_low_score`,並要求無 hard veto、仍在 `exact_identity`、且具備同品線/identity anchor 證據。這讓「可救回」與「正確阻擋」在操作層面真正分流,避免為了壓低 low_score 而重跑不該自動推進的候選。
|
||||
|
||||
@@ -350,7 +350,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '')
|
||||
# ==========================================
|
||||
# 系統版本與路徑
|
||||
# ==========================================
|
||||
SYSTEM_VERSION = "V10.494"
|
||||
SYSTEM_VERSION = "V10.495"
|
||||
LOG_FILE_PATH = os.path.join(BASE_DIR, 'logs/system.log')
|
||||
public_url = PUBLIC_URL # 用於模板顯示
|
||||
|
||||
|
||||
@@ -166,6 +166,7 @@ EwoooC 目前已有 MOMO EDM / 節慶活動資料、`promo_products`、PChome
|
||||
- 2026-05-31 追加 MCP fetch result parser review gate:`services.market_intel.mcp_fetch_result_parser_review` 與 `/api/market_intel/mcp_fetch_result_parser_review` 在 receipt 通過後審核操作員貼回的 parser 結構化摘要,檢查 receipt source/receipt path 對帳、parser artifact path、活動/商品候選必要欄位、公開 URL、小批次候選上限、raw HTML/secret 外洩與 API/DB/scheduler 副作用旗標;API/UI 不讀 artifact、不執行 parser CLI、不發外部 request、不保存 parser result、不寫入、不掛 scheduler,只放行到候選 handoff review。
|
||||
- 2026-05-31 追加 MCP fetch candidate handoff review gate:`services.market_intel.mcp_fetch_candidate_handoff_review` 與 `/api/market_intel/mcp_fetch_candidate_handoff_review` 在 parser review 通過後審核候選交接包,檢查 source/candidate key 對齊、queue policy 是否仍是 `manual_candidate_review` / `preview_only`、小批次候選上限、操作員無寫入/無連外/無排程確認、raw HTML/secret 外洩與 side-effect flags;API/UI 不建立 queue、不讀 artifact、不寫 DB、不掛 scheduler,只放行到人工 candidate queue review。
|
||||
- 2026-05-31 追加 MCP fetch candidate queue review gate:`services.market_intel.mcp_fetch_candidate_queue_review` 與 `/api/market_intel/mcp_fetch_candidate_queue_review` 在 handoff review 通過後審核人工 queue review 草案,檢查候選 key 對齊、`review_state=needs_review`、allowed actions 人工限定、`queue_write_status=not_persisted`、操作員無寫入/無連外/無排程確認、raw HTML/secret 外洩與 side-effect flags;API/UI 不建立 queue、不更新 review_state、不讀 artifact、不寫 DB、不掛 scheduler,只放行到 writer preflight。
|
||||
- 2026-05-31 追加 MCP fetch candidate queue writer preflight gate:`services.market_intel.mcp_fetch_candidate_queue_writer_preflight` 與 `/api/market_intel/mcp_fetch_candidate_queue_writer_preflight` 在 queue review 通過後審核 writer preflight 草案,檢查 `target_table=market_alert_review_queue`、`write_mode=cli_only_later`、dedupe strategy、insert columns、payload rows、候選 key 對齊、小批次上限、操作員無寫入/無連外/無 CLI/無排程確認、raw HTML/secret 外洩與 side-effect flags;API/UI 不開 DB、不執行 CLI、不建立 queue、不更新 review_state、不寫 DB、不掛 scheduler,只放行到 CLI writer review。
|
||||
- 2026-05-18 追加 scheduler attach plan preview:`services.market_intel.scheduler_plan` 與 `/api/market_intel/scheduler_plan` 描述未來 `campaign_discovery_daily`、`campaign_product_probe`、`product_match_review_seed` 三個 job 的 cadence、gate、fallback 與安全邊界。此階段不註冊 scheduler job、不啟動 crawler、不連外、不寫 DB;排程掛載必須等 migration、seed、MCP fetch gate、manual sample 與人工批准全過。
|
||||
- 2026-05-18 追加 match review plan preview:`services.market_intel.match_review_plan` 與 `/api/market_intel/match_review_plan` 定義商品比對訊號、分數門檻、`needs_review → confirmed/rejected` HITL 流程與安全邊界。此階段不建立 review queue、不自動 confirmed、不寫 `market_product_matches`、不呼叫 MCP;價格只能作為輔助訊號,不能單獨決定同品比對。
|
||||
- 2026-05-18 追加 opportunity plan preview:`services.market_intel.opportunity_plan` 與 `/api/market_intel/opportunity_plan` 定義競品低價威脅、促銷缺口、深折重疊、活動即將結束四類規則與分級策略。此階段不建立 opportunity queue、不派送 Telegram、不產生 AI 摘要、不寫 DB;高風險項必須先有 confirmed match 與 DB evidence 才能升級。
|
||||
|
||||
@@ -43,6 +43,8 @@
|
||||
- 2026-05-24 追記:同步市場情報 MCP fetch run package gate 後的 `routes/market_intel_routes.py` 與 `services/market_intel/deployment_readiness.py` 行數;本次新增 endpoint 已拆到 `routes/market_intel_mcp_run_routes.py`,主 Blueprint 只新增 extension import,後續 MCP route 應延續此模式。
|
||||
- 2026-05-24 追記:同步市場情報 MCP fetch run readiness gate 後的 `services/market_intel/deployment_readiness.py` 行數;本次新增 endpoint 延續 `routes/market_intel_mcp_run_routes.py` route extension,新增邏輯放在獨立 `services/market_intel/mcp_fetch_run_readiness.py`。
|
||||
- 2026-05-31 追記:同步市場情報 MCP fetch candidate queue review gate 後的 `services/market_intel/deployment_readiness.py` 行數;本次新增邏輯維持在獨立 `services/market_intel/mcp_fetch_candidate_queue_review.py`,route 延續 `routes/market_intel_mcp_run_routes.py` extension。
|
||||
- 2026-05-31 追記:同步市場情報 MCP fetch candidate queue writer preflight gate 後的 `services/market_intel/deployment_readiness.py` 行數;本次新增邏輯維持在獨立 `services/market_intel/mcp_fetch_candidate_queue_writer_preflight.py`,route 延續 `routes/market_intel_mcp_run_routes.py` extension。
|
||||
- 2026-05-31 追記:`services/market_intel/mcp_fetch_candidate_queue_writer_preflight.py` 目前 628 行,略過 600 行提醒門檻;暫不拆分的理由是 gate 條件、sample payload 與 side-effect blocklist 需留在單一 preview module 便於審核,下一個 writer CLI review gate 若共用相同常數再抽 `mcp_fetch_candidate_queue_writer_policy.py`。
|
||||
- 2026-05-24 追記:同步背景 Code Review 111 fallback 保護合併後的 `services/code_review_pipeline_service.py` 行數;此處只更新 inventory,不變更 Code Review 行為。
|
||||
- 2026-05-21 追記:同步 PChome/LUDEYA 商品線名稱漂移比對更新後的 `services/marketplace_product_matcher.py` 行數;此處只更新 inventory,不變更模組化決策。
|
||||
- 2026-05-21 追記:同步 MAC/Yuskin/AHC 名稱漂移與 bundle equivalent matcher 更新後的 `services/marketplace_product_matcher.py` 行數;此處只更新 inventory,不變更模組化決策。
|
||||
@@ -95,7 +97,7 @@
|
||||
| 805 | `routes/bot_api_routes.py` | P2 Bot API Blueprint | route glue / bot action service |
|
||||
| 1319 | `routes/market_intel_review_report_routes.py` | P2 market intel review report Blueprint | review report route glue / export payload / phase handoff orchestration |
|
||||
| 917 | `routes/market_intel_routes.py` | P2 market intel Blueprint | page route / API route glue / MCP gate route registration helper |
|
||||
| 1181 | `services/market_intel/deployment_readiness.py` | P2 market intel deployment readiness | preflight gates / readiness payload / route contract helpers |
|
||||
| 1219 | `services/market_intel/deployment_readiness.py` | P2 market intel deployment readiness | preflight gates / readiness payload / route contract helpers |
|
||||
| 846 | `services/market_intel/candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt.py` | P2 market intel review receipt pipeline | AI summary / persistence / Telegram dispatch / report catalog run receipt orchestration |
|
||||
|
||||
## 市場情報開發前置禁區
|
||||
|
||||
@@ -90,6 +90,7 @@
|
||||
- 2026-05-31 起,`V10.492` 收緊 PChome 近門檻自動回刷:`run_retryable_candidate_revalidation()` 只回刷 `recoverable_low_score` 與 legacy `low_score / refresh_low_score`,且 SQL 端要求 `hard_veto=false`、`comparison_mode=exact_identity`、diagnostic reasons 命中同品線/identity anchor;`identity_veto`、`unit_comparable`、`true_low_confidence` 不再進每日自動回刷隊列,需等待新證據或人工處理。
|
||||
- 2026-05-31 起,`V10.493` 新增市場情報 MCP Fetch Candidate Handoff Review gate:在 parser review 通過後只審核候選交接包,要求 source/candidate key 完全對齊、queue policy 維持 manual preview、小批次上限與操作員無寫入/無連外/無排程確認;仍不建立 queue、不寫 DB、不讀 artifact、不連外、不掛 scheduler。
|
||||
- 2026-05-31 起,`V10.494` 新增市場情報 MCP Fetch Candidate Queue Review gate:在 handoff review 通過後只審核人工 queue review 草案,要求候選 key 完全對齊、review_state 只停在 `needs_review`、allowed actions 限人工操作、queue_write_status 維持 `not_persisted`;仍不建立 queue、不更新 review_state、不寫 DB、不連外、不掛 scheduler。
|
||||
- 2026-05-31 起,`V10.495` 新增市場情報 MCP Fetch Candidate Queue Writer Preflight gate:在 queue review 通過後只審核 writer preflight 草案,要求 target table、write mode、dedupe strategy、insert columns、payload rows 與候選 key 完全對齊;仍不開 DB、不執行 CLI、不建立 queue、不更新 review_state、不寫 DB、不連外、不掛 scheduler。
|
||||
|
||||
## 3. 12 Agent 決策信封整合
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
## 📅 詳細更新日誌 (考古存檔)
|
||||
|
||||
### 2026-05-24:PChome 近門檻身份回收第二輪
|
||||
- **V10.495 市場情報 MCP Fetch Candidate Queue Writer Preflight gate**: 新增 `/api/market_intel/mcp_fetch_candidate_queue_writer_preflight` 與 UI preview,只審核 queue review 後的 writer preflight 草案;要求 `target_table=market_alert_review_queue`、`write_mode=cli_only_later`、dedupe strategy、insert columns、payload rows 與候選 key 完全對齊,且 API 不開 DB、不執行 CLI、不建立 queue、不更新 review_state、不寫 DB、不連外、不掛 scheduler。
|
||||
- **V10.494 市場情報 MCP Fetch Candidate Queue Review gate**: 新增 `/api/market_intel/mcp_fetch_candidate_queue_review` 與 UI preview,只審核 candidate handoff 後的人工 queue review 草案;要求候選 key 完全對齊、`review_state=needs_review`、allowed actions 限人工確認/否決/延後、`queue_write_status=not_persisted`,且 API 不建立 queue、不更新 review_state、不寫 DB、不連外、不掛 scheduler。
|
||||
- **V10.493 市場情報 MCP Fetch Candidate Handoff Review gate**: 新增 `/api/market_intel/mcp_fetch_candidate_handoff_review` 與 UI preview,只審核 parser review 後的候選交接包;要求 source/candidate key 完全對齊、queue policy 維持 `manual_candidate_review` / `preview_only`、候選數維持小批次,且 API 不建立 queue、不寫 DB、不讀 artifact、不連外、不掛 scheduler。
|
||||
- **V10.491 市場情報 MCP Fetch Result Parser Review gate**: 新增 `/api/market_intel/mcp_fetch_result_parser_review` 與 UI preview,只審核操作員 shell parser 後貼回的結構化摘要;API 不讀 artifact、不執行 parser CLI、不抓外站、不寫檔、不開 DB、不掛 scheduler,且會阻擋 raw HTML/body/snapshot、secret/token 欄位與 side-effect flags。
|
||||
|
||||
@@ -25,6 +25,9 @@ from services.market_intel.mcp_fetch_candidate_handoff_review import (
|
||||
from services.market_intel.mcp_fetch_candidate_queue_review import (
|
||||
build_mcp_fetch_candidate_queue_review_preview,
|
||||
)
|
||||
from services.market_intel.mcp_fetch_candidate_queue_writer_preflight import (
|
||||
build_mcp_fetch_candidate_queue_writer_preflight_preview,
|
||||
)
|
||||
|
||||
|
||||
@market_intel_bp.route("/api/market_intel/mcp_fetch_run_package", methods=["GET", "POST"])
|
||||
@@ -250,3 +253,48 @@ def market_intel_mcp_fetch_candidate_queue_review():
|
||||
phase=service.phase,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@market_intel_bp.route(
|
||||
"/api/market_intel/mcp_fetch_candidate_queue_writer_preflight",
|
||||
methods=["GET", "POST"],
|
||||
)
|
||||
@login_required
|
||||
def market_intel_mcp_fetch_candidate_queue_writer_preflight():
|
||||
queue_review_package = {}
|
||||
queue_review_result = None
|
||||
writer_preflight = None
|
||||
if request.method == "POST":
|
||||
payload = request.get_json(silent=True) or {}
|
||||
package = (
|
||||
payload.get("writer_preflight_review")
|
||||
or payload.get("candidate_queue_writer_preflight")
|
||||
or payload.get("writer_preflight")
|
||||
or payload
|
||||
)
|
||||
queue_review_package = (
|
||||
package.get("queue_review_package")
|
||||
or package.get("candidate_queue_review")
|
||||
or package.get("queue_review")
|
||||
or {}
|
||||
)
|
||||
queue_review_result = (
|
||||
package.get("queue_review_result")
|
||||
or package.get("mcp_fetch_candidate_queue_review")
|
||||
)
|
||||
writer_preflight = (
|
||||
package.get("writer_preflight")
|
||||
or package.get("candidate_queue_writer_preflight")
|
||||
or package.get("preflight")
|
||||
or package.get("preflight_payload")
|
||||
)
|
||||
|
||||
service = MarketIntelService()
|
||||
return jsonify(
|
||||
build_mcp_fetch_candidate_queue_writer_preflight_preview(
|
||||
queue_review_package=queue_review_package,
|
||||
queue_review_result=queue_review_result,
|
||||
writer_preflight=writer_preflight,
|
||||
phase=service.phase,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -81,6 +81,9 @@ from services.market_intel.mcp_fetch_candidate_handoff_review import (
|
||||
from services.market_intel.mcp_fetch_candidate_queue_review import (
|
||||
build_mcp_fetch_candidate_queue_review_preview,
|
||||
)
|
||||
from services.market_intel.mcp_fetch_candidate_queue_writer_preflight import (
|
||||
build_mcp_fetch_candidate_queue_writer_preflight_preview,
|
||||
)
|
||||
from services.market_intel.mcp_manual_fetch_handoff import (
|
||||
build_mcp_manual_fetch_handoff_preview,
|
||||
)
|
||||
@@ -213,6 +216,11 @@ PRODUCTION_SMOKE_TARGETS = (
|
||||
+ ("/api/market_intel/mcp_fetch_candidate_queue_review",)
|
||||
+ PRODUCTION_SMOKE_TARGETS[-1:]
|
||||
)
|
||||
PRODUCTION_SMOKE_TARGETS = (
|
||||
PRODUCTION_SMOKE_TARGETS[:-1]
|
||||
+ ("/api/market_intel/mcp_fetch_candidate_queue_writer_preflight",)
|
||||
+ PRODUCTION_SMOKE_TARGETS[-1:]
|
||||
)
|
||||
def _run_review_preview_safe(payload, mode):
|
||||
return bool(payload["mode"] == mode and all(not payload.get(key) for key in BLOCKED_RUN_REVIEW_KEYS))
|
||||
def build_deployment_readiness_preview(*, service, market_intel_tables, schema_smoke_builder):
|
||||
@@ -262,6 +270,11 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s
|
||||
phase=service.phase,
|
||||
)
|
||||
)
|
||||
mcp_fetch_candidate_queue_writer_preflight = (
|
||||
build_mcp_fetch_candidate_queue_writer_preflight_preview(
|
||||
phase=service.phase,
|
||||
)
|
||||
)
|
||||
scheduler_plan = service.build_scheduler_plan()
|
||||
manual_sample_plan = service.build_manual_sample_plan()
|
||||
manual_sample_acceptance = service.build_manual_sample_acceptance()
|
||||
@@ -785,6 +798,30 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s
|
||||
and not candidate_queue_writer_preflight["database_commit_executed"]
|
||||
and not candidate_queue_writer_preflight["scheduler_attached"]
|
||||
),
|
||||
"mcp_fetch_candidate_queue_writer_preflight_preview_safe": bool(
|
||||
mcp_fetch_candidate_queue_writer_preflight["mode"]
|
||||
== "mcp_fetch_candidate_queue_writer_preflight_preview"
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["payload_persisted"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["preflight_persisted"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["candidate_queue_created"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["candidate_queue_persisted"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["candidate_review_state_updated"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["network_request_allowed"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["api_executes_cli"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight[
|
||||
"api_opens_database_connection"
|
||||
]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["api_writes_database"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight[
|
||||
"api_uses_external_network"
|
||||
]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight[
|
||||
"database_write_executed"
|
||||
]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["cli_executed"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["file_written"]
|
||||
and not mcp_fetch_candidate_queue_writer_preflight["scheduler_attached"]
|
||||
),
|
||||
"candidate_queue_writer_postwrite_smoke_planned_safe": bool(
|
||||
candidate_queue_writer_postwrite_smoke["mode"]
|
||||
== "candidate_queue_writer_postwrite_smoke_planned"
|
||||
@@ -1104,6 +1141,7 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s
|
||||
"mcp_fetch_result_parser_review": mcp_fetch_result_parser_review,
|
||||
"mcp_fetch_candidate_handoff_review": mcp_fetch_candidate_handoff_review,
|
||||
"mcp_fetch_candidate_queue_review": mcp_fetch_candidate_queue_review,
|
||||
"mcp_fetch_candidate_queue_writer_preflight": mcp_fetch_candidate_queue_writer_preflight,
|
||||
"scheduler_plan": scheduler_plan,
|
||||
"manual_sample_plan": manual_sample_plan,
|
||||
"manual_sample_acceptance": manual_sample_acceptance,
|
||||
|
||||
@@ -0,0 +1,628 @@
|
||||
"""市場情報 MCP fetch candidate queue writer preflight preview。
|
||||
|
||||
本模組只審核 candidate queue review 後的 writer preflight 草案;
|
||||
不開 DB、不建立 transaction、不執行 CLI、不寫 queue、不掛 scheduler。
|
||||
"""
|
||||
|
||||
from services.market_intel.mcp_fetch_candidate_queue_review import (
|
||||
build_mcp_fetch_candidate_queue_review_preview,
|
||||
)
|
||||
|
||||
|
||||
TARGET_TABLE = "market_alert_review_queue"
|
||||
MAX_PREFLIGHT_ITEMS = 80
|
||||
SAFE_PREFLIGHT_MODES = {"candidate_queue_writer_preflight_preview"}
|
||||
SAFE_WRITE_MODES = {"cli_only_later"}
|
||||
SAFE_DEDUPE_STRATEGIES = {"candidate_type_platform_source_key"}
|
||||
REQUIRED_INSERT_COLUMNS = (
|
||||
"candidate_type",
|
||||
"platform_code",
|
||||
"source_key",
|
||||
"candidate_key",
|
||||
"candidate_name",
|
||||
"candidate_url",
|
||||
"review_state",
|
||||
"priority_lane",
|
||||
"evidence_ref",
|
||||
)
|
||||
|
||||
FORBIDDEN_SECRET_KEYS = (
|
||||
"approval_token",
|
||||
"approval-token",
|
||||
"api_key",
|
||||
"authorization",
|
||||
"bearer",
|
||||
"client_secret",
|
||||
"cookie",
|
||||
"password",
|
||||
"secret",
|
||||
"session_cookie",
|
||||
"token",
|
||||
)
|
||||
|
||||
SAFE_SECRET_METADATA_KEYS = {
|
||||
"operator_confirmed_no_secret_payload",
|
||||
}
|
||||
|
||||
FORBIDDEN_RAW_PAYLOAD_KEYS = (
|
||||
"body_html",
|
||||
"document_html",
|
||||
"html",
|
||||
"page_html",
|
||||
"raw_body",
|
||||
"raw_html",
|
||||
"raw_snapshot",
|
||||
"response_body",
|
||||
)
|
||||
|
||||
_BLOCKED_SIDE_EFFECT_KEYS = (
|
||||
"allow_api_execution",
|
||||
"allow_database_write",
|
||||
"allow_external_network_in_api",
|
||||
"allow_scheduler_attach",
|
||||
"api_executed_cli",
|
||||
"api_executes_cli",
|
||||
"api_executes_docker",
|
||||
"api_executes_health_check",
|
||||
"api_executes_ssh",
|
||||
"api_opens_database_connection",
|
||||
"api_uses_external_network",
|
||||
"api_writes_database",
|
||||
"attach_scheduler",
|
||||
"candidate_queue_created",
|
||||
"candidate_queue_persisted",
|
||||
"candidate_review_state_updated",
|
||||
"cli_executed",
|
||||
"command_executed",
|
||||
"database_commit_executed",
|
||||
"database_session_created",
|
||||
"database_write_executed",
|
||||
"external_network_executed",
|
||||
"file_written",
|
||||
"network_request_allowed",
|
||||
"payload_persisted",
|
||||
"preflight_persisted",
|
||||
"scheduler_attached",
|
||||
"write_database",
|
||||
"writes_executed",
|
||||
"would_write_database",
|
||||
)
|
||||
|
||||
|
||||
def _as_dict(value):
|
||||
return value if isinstance(value, dict) else {}
|
||||
|
||||
|
||||
def _as_list(value):
|
||||
if value is None:
|
||||
return []
|
||||
if isinstance(value, (list, tuple, set)):
|
||||
return list(value)
|
||||
return [value]
|
||||
|
||||
|
||||
def _safe_int(value):
|
||||
try:
|
||||
return int(value or 0)
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
def _safe_text(value, limit=500):
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text[:limit] if text else None
|
||||
|
||||
|
||||
def _contains_forbidden_key(value, forbidden_keys, *, safe_keys=None):
|
||||
safe_keys = safe_keys or set()
|
||||
if isinstance(value, dict):
|
||||
for key, nested in value.items():
|
||||
normalized_key = str(key).lower()
|
||||
if normalized_key in safe_keys and isinstance(nested, bool):
|
||||
continue
|
||||
if any(forbidden_key in normalized_key for forbidden_key in forbidden_keys):
|
||||
return True
|
||||
if _contains_forbidden_key(nested, forbidden_keys, safe_keys=safe_keys):
|
||||
return True
|
||||
elif isinstance(value, list):
|
||||
return any(
|
||||
_contains_forbidden_key(item, forbidden_keys, safe_keys=safe_keys)
|
||||
for item in value
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def _blocked_side_effects(payload):
|
||||
found = []
|
||||
|
||||
def visit(value, path):
|
||||
if isinstance(value, dict):
|
||||
for key, item in value.items():
|
||||
normalized_key = str(key).lower()
|
||||
key_path = f"{path}.{key}" if path else key
|
||||
if normalized_key in _BLOCKED_SIDE_EFFECT_KEYS and bool(item):
|
||||
found.append(key_path)
|
||||
visit(item, key_path)
|
||||
elif isinstance(value, list):
|
||||
for index, item in enumerate(value):
|
||||
visit(item, f"{path}[{index}]")
|
||||
|
||||
visit(payload, "")
|
||||
return found
|
||||
|
||||
|
||||
def _queue_review_from_inputs(queue_review_package, queue_review_result, phase):
|
||||
if isinstance(queue_review_result, dict) and queue_review_result:
|
||||
return queue_review_result
|
||||
|
||||
queue_review_package = _as_dict(queue_review_package)
|
||||
return build_mcp_fetch_candidate_queue_review_preview(
|
||||
handoff_review_package=(
|
||||
queue_review_package.get("handoff_review_package")
|
||||
or queue_review_package.get("candidate_handoff_review")
|
||||
or queue_review_package.get("handoff_review")
|
||||
),
|
||||
handoff_review_result=(
|
||||
queue_review_package.get("handoff_review_result")
|
||||
or queue_review_package.get("mcp_fetch_candidate_handoff_review")
|
||||
),
|
||||
candidate_queue_review=(
|
||||
queue_review_package.get("candidate_queue_review")
|
||||
or queue_review_package.get("queue_review")
|
||||
or queue_review_package.get("review_payload")
|
||||
),
|
||||
phase=phase,
|
||||
)
|
||||
|
||||
|
||||
def _sample_writer_preflight_package():
|
||||
queue_preview = build_mcp_fetch_candidate_queue_review_preview()
|
||||
queue_review_package = queue_preview["sample_candidate_queue_review_package"]
|
||||
queue_review_result = build_mcp_fetch_candidate_queue_review_preview(
|
||||
handoff_review_package=queue_review_package["handoff_review_package"],
|
||||
handoff_review_result=queue_review_package["handoff_review_result"],
|
||||
candidate_queue_review=queue_review_package["candidate_queue_review"],
|
||||
)
|
||||
review_summary = queue_review_result["candidate_queue_review_summary"]
|
||||
items = review_summary.get("review_items", [])
|
||||
payload_rows = []
|
||||
for item in items:
|
||||
payload_rows.append(
|
||||
{
|
||||
"candidate_type": item.get("candidate_type"),
|
||||
"platform_code": item.get("platform_code"),
|
||||
"source_key": item.get("source_key"),
|
||||
"candidate_key": item.get("candidate_key"),
|
||||
"candidate_name": item.get("candidate_name"),
|
||||
"candidate_url": item.get("candidate_url"),
|
||||
"review_state": item.get("review_state"),
|
||||
"priority_lane": item.get("priority_lane"),
|
||||
"evidence_ref": item.get("evidence_ref"),
|
||||
"dedupe_key": (
|
||||
f"{item.get('candidate_type')}::"
|
||||
f"{item.get('platform_code')}::"
|
||||
f"{item.get('source_key')}::"
|
||||
f"{item.get('candidate_key')}"
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"queue_review_package": queue_review_package,
|
||||
"queue_review_result": queue_review_result,
|
||||
"writer_preflight": {
|
||||
"preflight_id": "market-intel-candidate-writer-preflight-sample",
|
||||
"preflight_mode": "candidate_queue_writer_preflight_preview",
|
||||
"target_table": TARGET_TABLE,
|
||||
"write_mode": "cli_only_later",
|
||||
"dedupe_strategy": "candidate_type_platform_source_key",
|
||||
"insert_columns": list(REQUIRED_INSERT_COLUMNS),
|
||||
"operator_confirmed_no_database_write": True,
|
||||
"operator_confirmed_no_external_network": True,
|
||||
"operator_confirmed_no_scheduler_attach": True,
|
||||
"operator_confirmed_no_cli_execution": True,
|
||||
"operator_confirmed_no_persistence": True,
|
||||
"operator_confirmed_no_secret_payload": True,
|
||||
"summary": {
|
||||
"payload_row_count": len(payload_rows),
|
||||
"campaign_candidate_count": review_summary.get(
|
||||
"campaign_candidate_count",
|
||||
0,
|
||||
),
|
||||
"product_candidate_count": review_summary.get(
|
||||
"product_candidate_count",
|
||||
0,
|
||||
),
|
||||
},
|
||||
"payload_rows": payload_rows,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _queue_review_summary(queue_review):
|
||||
queue_review = _as_dict(queue_review)
|
||||
side_effects_clear = bool(
|
||||
not queue_review.get("network_request_allowed")
|
||||
and not queue_review.get("api_executes_cli")
|
||||
and not queue_review.get("api_opens_database_connection")
|
||||
and not queue_review.get("api_writes_database")
|
||||
and not queue_review.get("api_uses_external_network")
|
||||
and not queue_review.get("database_write_executed")
|
||||
and not queue_review.get("fetch_executed")
|
||||
and not queue_review.get("cli_executed")
|
||||
and not queue_review.get("file_written")
|
||||
and not queue_review.get("scheduler_attached")
|
||||
and not queue_review.get("candidate_queue_created")
|
||||
and not queue_review.get("candidate_queue_persisted")
|
||||
and not queue_review.get("candidate_review_state_updated")
|
||||
)
|
||||
summary = _as_dict(queue_review.get("candidate_queue_review_summary"))
|
||||
return {
|
||||
"mode": queue_review.get("mode"),
|
||||
"accepted": bool(
|
||||
queue_review.get("mcp_fetch_candidate_queue_review_accepted")
|
||||
),
|
||||
"ready_for_candidate_queue_writer_preflight": bool(
|
||||
queue_review.get("ready_for_candidate_queue_writer_preflight")
|
||||
),
|
||||
"review_item_count": _safe_int(queue_review.get("review_item_count")),
|
||||
"campaign_candidate_count": _safe_int(
|
||||
queue_review.get("campaign_candidate_count")
|
||||
),
|
||||
"product_candidate_count": _safe_int(
|
||||
queue_review.get("product_candidate_count")
|
||||
),
|
||||
"campaign_candidate_keys": sorted(
|
||||
key for key in _as_list(summary.get("campaign_candidate_keys")) if key
|
||||
),
|
||||
"product_candidate_keys": sorted(
|
||||
key for key in _as_list(summary.get("product_candidate_keys")) if key
|
||||
),
|
||||
"side_effects_clear": side_effects_clear,
|
||||
"blocked_reasons": queue_review.get("blocked_reasons", []),
|
||||
}
|
||||
|
||||
|
||||
def _payload_row_summary(row):
|
||||
row = _as_dict(row)
|
||||
return {
|
||||
"candidate_type": _safe_text(row.get("candidate_type"), 40),
|
||||
"platform_code": _safe_text(row.get("platform_code"), 80),
|
||||
"source_key": _safe_text(row.get("source_key"), 160),
|
||||
"candidate_key": _safe_text(row.get("candidate_key"), 240),
|
||||
"candidate_name": _safe_text(row.get("candidate_name"), 300),
|
||||
"candidate_url": _safe_text(row.get("candidate_url"), 500),
|
||||
"review_state": _safe_text(row.get("review_state"), 80),
|
||||
"priority_lane": _safe_text(row.get("priority_lane"), 80),
|
||||
"evidence_ref": _safe_text(row.get("evidence_ref"), 160),
|
||||
"dedupe_key": _safe_text(row.get("dedupe_key"), 500),
|
||||
"required_fields_present": bool(
|
||||
row.get("candidate_type") in {"campaign", "product"}
|
||||
and row.get("platform_code")
|
||||
and row.get("source_key")
|
||||
and row.get("candidate_key")
|
||||
and row.get("candidate_name")
|
||||
and row.get("candidate_url")
|
||||
and row.get("review_state") == "needs_review"
|
||||
and row.get("dedupe_key")
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def _preflight_summary(writer_preflight):
|
||||
writer_preflight = _as_dict(writer_preflight)
|
||||
rows = [_payload_row_summary(row) for row in _as_list(
|
||||
writer_preflight.get("payload_rows")
|
||||
)]
|
||||
campaign_rows = [row for row in rows if row["candidate_type"] == "campaign"]
|
||||
product_rows = [row for row in rows if row["candidate_type"] == "product"]
|
||||
summary = _as_dict(writer_preflight.get("summary"))
|
||||
insert_columns = sorted(
|
||||
column for column in _as_list(writer_preflight.get("insert_columns")) if column
|
||||
)
|
||||
return {
|
||||
"provided_keys": sorted(writer_preflight.keys()),
|
||||
"preflight_id": _safe_text(writer_preflight.get("preflight_id"), 160),
|
||||
"preflight_mode": _safe_text(writer_preflight.get("preflight_mode"), 120),
|
||||
"target_table": _safe_text(writer_preflight.get("target_table"), 160),
|
||||
"write_mode": _safe_text(writer_preflight.get("write_mode"), 80),
|
||||
"dedupe_strategy": _safe_text(writer_preflight.get("dedupe_strategy"), 160),
|
||||
"insert_columns": insert_columns,
|
||||
"operator_confirmed_no_database_write": bool(
|
||||
writer_preflight.get("operator_confirmed_no_database_write")
|
||||
),
|
||||
"operator_confirmed_no_external_network": bool(
|
||||
writer_preflight.get("operator_confirmed_no_external_network")
|
||||
),
|
||||
"operator_confirmed_no_scheduler_attach": bool(
|
||||
writer_preflight.get("operator_confirmed_no_scheduler_attach")
|
||||
),
|
||||
"operator_confirmed_no_cli_execution": bool(
|
||||
writer_preflight.get("operator_confirmed_no_cli_execution")
|
||||
),
|
||||
"operator_confirmed_no_persistence": bool(
|
||||
writer_preflight.get("operator_confirmed_no_persistence")
|
||||
),
|
||||
"operator_confirmed_no_secret_payload": bool(
|
||||
writer_preflight.get("operator_confirmed_no_secret_payload")
|
||||
),
|
||||
"payload_row_count": len(rows),
|
||||
"summary_payload_row_count": _safe_int(summary.get("payload_row_count")),
|
||||
"campaign_candidate_count": len(campaign_rows),
|
||||
"summary_campaign_candidate_count": _safe_int(
|
||||
summary.get("campaign_candidate_count")
|
||||
),
|
||||
"product_candidate_count": len(product_rows),
|
||||
"summary_product_candidate_count": _safe_int(
|
||||
summary.get("product_candidate_count")
|
||||
),
|
||||
"campaign_candidate_keys": sorted(
|
||||
row["candidate_key"] for row in campaign_rows if row["candidate_key"]
|
||||
),
|
||||
"product_candidate_keys": sorted(
|
||||
row["candidate_key"] for row in product_rows if row["candidate_key"]
|
||||
),
|
||||
"payload_rows": rows,
|
||||
"insert_columns_cover_required": bool(
|
||||
set(REQUIRED_INSERT_COLUMNS).issubset(set(insert_columns))
|
||||
),
|
||||
"all_rows_required_fields_present": bool(
|
||||
rows and all(row["required_fields_present"] for row in rows)
|
||||
),
|
||||
"dedupe_keys_unique": bool(
|
||||
rows
|
||||
and len({row["dedupe_key"] for row in rows if row["dedupe_key"]})
|
||||
== len(rows)
|
||||
),
|
||||
"summary_counts_match_rows": bool(
|
||||
_safe_int(summary.get("payload_row_count")) == len(rows)
|
||||
and _safe_int(summary.get("campaign_candidate_count")) == len(campaign_rows)
|
||||
and _safe_int(summary.get("product_candidate_count")) == len(product_rows)
|
||||
),
|
||||
"raw_payload_submitted_to_api": _contains_forbidden_key(
|
||||
writer_preflight,
|
||||
FORBIDDEN_RAW_PAYLOAD_KEYS,
|
||||
),
|
||||
"secret_or_token_submitted_to_api": _contains_forbidden_key(
|
||||
writer_preflight,
|
||||
FORBIDDEN_SECRET_KEYS,
|
||||
safe_keys=SAFE_SECRET_METADATA_KEYS,
|
||||
),
|
||||
"blocked_side_effects": _blocked_side_effects(writer_preflight),
|
||||
}
|
||||
|
||||
|
||||
def _preflight_gates(
|
||||
*,
|
||||
queue_review_received,
|
||||
writer_preflight_received,
|
||||
writer_preflight_valid_object,
|
||||
queue_review,
|
||||
preflight,
|
||||
):
|
||||
queue_campaign_keys = set(queue_review["campaign_candidate_keys"])
|
||||
preflight_campaign_keys = set(preflight["campaign_candidate_keys"])
|
||||
queue_product_keys = set(queue_review["product_candidate_keys"])
|
||||
preflight_product_keys = set(preflight["product_candidate_keys"])
|
||||
operator_boundaries_confirmed = bool(
|
||||
preflight["operator_confirmed_no_database_write"]
|
||||
and preflight["operator_confirmed_no_external_network"]
|
||||
and preflight["operator_confirmed_no_scheduler_attach"]
|
||||
and preflight["operator_confirmed_no_cli_execution"]
|
||||
and preflight["operator_confirmed_no_persistence"]
|
||||
and preflight["operator_confirmed_no_secret_payload"]
|
||||
)
|
||||
return [
|
||||
{
|
||||
"key": "queue_review_payload_or_result_received",
|
||||
"label": "已提供 candidate queue review package 或已審核結果",
|
||||
"passed": queue_review_received,
|
||||
},
|
||||
{
|
||||
"key": "queue_review_accepted",
|
||||
"label": "candidate queue review gate 必須已通過",
|
||||
"passed": queue_review["accepted"],
|
||||
},
|
||||
{
|
||||
"key": "queue_review_ready_for_writer_preflight",
|
||||
"label": "queue review 只放行到 writer preflight",
|
||||
"passed": queue_review["ready_for_candidate_queue_writer_preflight"],
|
||||
},
|
||||
{
|
||||
"key": "queue_review_side_effect_free",
|
||||
"label": "queue review 未顯示 API 執行、連外、寫檔、寫 DB 或掛 scheduler",
|
||||
"passed": queue_review["side_effects_clear"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_payload_received",
|
||||
"label": "已提供 writer preflight 草案",
|
||||
"passed": writer_preflight_received,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_valid_object",
|
||||
"label": "writer preflight payload 必須是 JSON object",
|
||||
"passed": writer_preflight_valid_object,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_identity_recorded",
|
||||
"label": "writer preflight 必須記錄 preflight_id",
|
||||
"passed": bool(preflight["preflight_id"]),
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_mode_preview_only",
|
||||
"label": "writer preflight 必須維持 preview mode",
|
||||
"passed": preflight["preflight_mode"] in SAFE_PREFLIGHT_MODES,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_target_table_safe",
|
||||
"label": "target table 必須是 market_alert_review_queue",
|
||||
"passed": preflight["target_table"] == TARGET_TABLE,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_write_mode_cli_later",
|
||||
"label": "write mode 必須是 cli_only_later,不得由 API 寫入",
|
||||
"passed": preflight["write_mode"] in SAFE_WRITE_MODES,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_dedupe_strategy_safe",
|
||||
"label": "dedupe strategy 必須可由候選 identity 決定",
|
||||
"passed": preflight["dedupe_strategy"] in SAFE_DEDUPE_STRATEGIES,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_candidates_match_queue_review",
|
||||
"label": "writer payload candidate key 必須完全對齊 queue review",
|
||||
"passed": bool(
|
||||
queue_campaign_keys
|
||||
and queue_product_keys
|
||||
and preflight_campaign_keys == queue_campaign_keys
|
||||
and preflight_product_keys == queue_product_keys
|
||||
),
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_payload_count_within_limit",
|
||||
"label": "writer payload rows 需維持小批次上限",
|
||||
"passed": bool(0 < preflight["payload_row_count"] <= MAX_PREFLIGHT_ITEMS),
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_counts_match_summary",
|
||||
"label": "summary count 必須與 payload rows 數量一致",
|
||||
"passed": preflight["summary_counts_match_rows"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_required_columns_present",
|
||||
"label": "insert columns 必須覆蓋 queue 基本欄位",
|
||||
"passed": preflight["insert_columns_cover_required"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_required_fields_present",
|
||||
"label": "payload rows 必須具備候選識別與 needs_review 狀態",
|
||||
"passed": preflight["all_rows_required_fields_present"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_dedupe_keys_unique",
|
||||
"label": "payload rows dedupe key 必須唯一",
|
||||
"passed": preflight["dedupe_keys_unique"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_operator_boundaries_confirmed",
|
||||
"label": "操作員確認無寫入、無連外、無 CLI、無排程、無保存",
|
||||
"passed": operator_boundaries_confirmed,
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_no_raw_payload",
|
||||
"label": "writer preflight payload 不得回貼 raw HTML/body/snapshot",
|
||||
"passed": not preflight["raw_payload_submitted_to_api"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_no_secret_or_token_key",
|
||||
"label": "writer preflight payload 不得包含 secret、cookie、password 或 token key",
|
||||
"passed": not preflight["secret_or_token_submitted_to_api"],
|
||||
},
|
||||
{
|
||||
"key": "writer_preflight_side_effect_free",
|
||||
"label": "writer preflight payload 不得要求 API 執行、連外、寫檔、寫 DB 或掛 scheduler",
|
||||
"passed": not preflight["blocked_side_effects"],
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def build_mcp_fetch_candidate_queue_writer_preflight_preview(
|
||||
*,
|
||||
queue_review_package=None,
|
||||
queue_review_result=None,
|
||||
writer_preflight=None,
|
||||
phase=None,
|
||||
):
|
||||
"""建立 fetch candidate queue writer preflight;不開 DB 或寫入。"""
|
||||
queue_review_package = _as_dict(queue_review_package)
|
||||
queue_review_result_received = bool(
|
||||
isinstance(queue_review_result, dict) and queue_review_result
|
||||
)
|
||||
writer_preflight_valid_object = isinstance(writer_preflight, dict) if (
|
||||
writer_preflight is not None
|
||||
) else True
|
||||
writer_preflight_payload = _as_dict(writer_preflight)
|
||||
queue_review_data = _queue_review_from_inputs(
|
||||
queue_review_package,
|
||||
queue_review_result,
|
||||
phase,
|
||||
)
|
||||
queue_review_received = bool(
|
||||
queue_review_package or queue_review_result_received
|
||||
)
|
||||
payload_received = bool(
|
||||
queue_review_received
|
||||
or writer_preflight_payload
|
||||
or writer_preflight is not None
|
||||
)
|
||||
writer_preflight_received = bool(writer_preflight_payload)
|
||||
queue_review = _queue_review_summary(queue_review_data)
|
||||
preflight = _preflight_summary(writer_preflight_payload)
|
||||
gates = _preflight_gates(
|
||||
queue_review_received=queue_review_received,
|
||||
writer_preflight_received=writer_preflight_received,
|
||||
writer_preflight_valid_object=writer_preflight_valid_object,
|
||||
queue_review=queue_review,
|
||||
preflight=preflight,
|
||||
)
|
||||
blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]]
|
||||
accepted = bool(payload_received and not blocked_reasons)
|
||||
|
||||
return {
|
||||
"mode": (
|
||||
"mcp_fetch_candidate_queue_writer_preflight"
|
||||
if payload_received
|
||||
else "mcp_fetch_candidate_queue_writer_preflight_preview"
|
||||
),
|
||||
"phase": phase,
|
||||
"writer_preflight_payload_received": payload_received,
|
||||
"queue_review_received": queue_review_received,
|
||||
"writer_preflight_received": writer_preflight_received,
|
||||
"writer_preflight_valid_object": writer_preflight_valid_object,
|
||||
"queue_review_accepted": queue_review["accepted"],
|
||||
"mcp_fetch_candidate_queue_writer_preflight_accepted": accepted,
|
||||
"candidate_queue_writer_preflight_ready": accepted,
|
||||
"ready_for_candidate_queue_writer_cli_review": accepted,
|
||||
"ready_for_api_database_write": False,
|
||||
"ready_for_scheduler_attach": False,
|
||||
"network_request_allowed": False,
|
||||
"cli_executed": False,
|
||||
"candidate_queue_created": False,
|
||||
"candidate_queue_persisted": False,
|
||||
"candidate_review_state_updated": False,
|
||||
"review_item_count": queue_review["review_item_count"],
|
||||
"payload_row_count": preflight["payload_row_count"],
|
||||
"campaign_candidate_count": preflight["campaign_candidate_count"],
|
||||
"product_candidate_count": preflight["product_candidate_count"],
|
||||
"gate_count": len(gates),
|
||||
"passed_gate_count": sum(1 for gate in gates if gate["passed"]),
|
||||
"blocked_reasons": blocked_reasons,
|
||||
"gates": gates,
|
||||
"queue_review_summary": queue_review,
|
||||
"writer_preflight_summary": preflight,
|
||||
"sample_writer_preflight_package": _sample_writer_preflight_package(),
|
||||
"next_operator_steps": [
|
||||
"writer preflight 通過後,只代表可進 CLI writer review,不代表可寫 market_*",
|
||||
"實際 writer run、DB commit、post-write smoke、scheduler attach 都必須另開獨立 gate",
|
||||
"API/UI 仍不得開 DB、不得執行 CLI、不得建立 queue、不得更新 review_state",
|
||||
],
|
||||
"payload_persisted": False,
|
||||
"preflight_persisted": False,
|
||||
"api_executes_health_check": False,
|
||||
"api_executes_docker": False,
|
||||
"api_executes_ssh": False,
|
||||
"api_executes_cli": False,
|
||||
"api_opens_database_connection": False,
|
||||
"api_writes_database": False,
|
||||
"api_uses_external_network": False,
|
||||
"database_session_created": False,
|
||||
"database_commit_executed": False,
|
||||
"database_write_executed": False,
|
||||
"external_network_executed": False,
|
||||
"file_written": False,
|
||||
"writes_executed": False,
|
||||
"would_write_database": False,
|
||||
"scheduler_attached": False,
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
"""市場情報 rollout phase 單一來源。"""
|
||||
|
||||
MARKET_INTEL_PHASE = "phase_127_market_intel_mcp_fetch_candidate_queue_review"
|
||||
MARKET_INTEL_PHASE = "phase_128_market_intel_mcp_fetch_candidate_queue_writer_preflight"
|
||||
|
||||
@@ -838,6 +838,32 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="market-intel-panel" data-market-intel-mcp-fetch-candidate-queue-writer-preflight>
|
||||
<div class="market-intel-preview-head">
|
||||
<div>
|
||||
<p class="market-intel-muted momo-mono mb-1">MCP / QUEUE WRITER PREFLIGHT</p>
|
||||
<h2 class="market-intel-preview-title">MCP Candidate Queue Writer Preflight</h2>
|
||||
</div>
|
||||
<button class="market-intel-icon-button" type="button" title="重新整理 MCP Candidate Queue Writer Preflight" data-market-intel-mcp-fetch-candidate-queue-writer-preflight-refresh>
|
||||
<i class="fas fa-rotate-right" aria-hidden="true"></i>
|
||||
</button>
|
||||
</div>
|
||||
<div class="market-intel-preview-meta" data-market-intel-mcp-fetch-candidate-queue-writer-preflight-meta>
|
||||
<span class="market-intel-pill">loading</span>
|
||||
</div>
|
||||
<div data-market-intel-mcp-fetch-candidate-queue-writer-preflight-body>
|
||||
<div class="market-intel-empty">讀取 MCP Candidate Queue Writer Preflight 中...</div>
|
||||
</div>
|
||||
<div class="market-intel-control-row mt-3">
|
||||
<textarea class="market-intel-json-input" rows="9" spellcheck="false" data-market-intel-mcp-fetch-candidate-queue-writer-preflight-input placeholder="candidate queue review and writer preflight JSON"></textarea>
|
||||
<div class="market-intel-control-actions">
|
||||
<button class="market-intel-icon-button" type="button" title="審核 MCP Candidate Queue Writer Preflight JSON" data-market-intel-mcp-fetch-candidate-queue-writer-preflight-review>
|
||||
<i class="fas fa-check" aria-hidden="true"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="market-intel-panel" data-market-intel-manual-sample>
|
||||
<div class="market-intel-preview-head">
|
||||
<div>
|
||||
@@ -1353,6 +1379,7 @@
|
||||
const mcpFetchResultParserReviewRoot = document.querySelector('[data-market-intel-mcp-fetch-result-parser-review]');
|
||||
const mcpFetchCandidateHandoffReviewRoot = document.querySelector('[data-market-intel-mcp-fetch-candidate-handoff-review]');
|
||||
const mcpFetchCandidateQueueReviewRoot = document.querySelector('[data-market-intel-mcp-fetch-candidate-queue-review]');
|
||||
const mcpFetchCandidateQueueWriterPreflightRoot = document.querySelector('[data-market-intel-mcp-fetch-candidate-queue-writer-preflight]');
|
||||
const manualSampleRoot = document.querySelector('[data-market-intel-manual-sample]');
|
||||
const sampleAcceptanceRoot = document.querySelector('[data-market-intel-sample-acceptance]');
|
||||
const sampleReviewRoot = document.querySelector('[data-market-intel-sample-review]');
|
||||
@@ -1369,7 +1396,7 @@
|
||||
const liveInventoryRoot = document.querySelector('[data-market-intel-live-inventory]');
|
||||
const approvalRoot = document.querySelector('[data-market-intel-approval]');
|
||||
const deployRoot = document.querySelector('[data-market-intel-deploy]');
|
||||
if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !mcpReadinessRoot && !mcpPreflightRoot && !mcpActivationRoot && !mcpFetchGateRoot && !mcpCompletionRoot && !mcpActivationEvidenceRoot && !mcpRuntimeSmokeRoot && !mcpRuntimePromotionRoot && !mcpManualFetchHandoffRoot && !mcpFetchTargetReviewRoot && !mcpFetchRunPackageRoot && !mcpFetchRunReadinessRoot && !mcpFetchRunReceiptRoot && !mcpFetchResultParserReviewRoot && !mcpFetchCandidateHandoffReviewRoot && !mcpFetchCandidateQueueReviewRoot && !manualSampleRoot && !sampleAcceptanceRoot && !sampleReviewRoot && !schedulerRoot && !matchReviewRoot && !opportunityRoot && !opportunityScoringRoot && !opportunityEvidenceRoot && !opportunityAlertRoot && !migrationRoot && !migrationDrillRoot && !catalogReviewRoot && !liveSmokeRoot && !liveInventoryRoot && !approvalRoot && !deployRoot) return;
|
||||
if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !mcpReadinessRoot && !mcpPreflightRoot && !mcpActivationRoot && !mcpFetchGateRoot && !mcpCompletionRoot && !mcpActivationEvidenceRoot && !mcpRuntimeSmokeRoot && !mcpRuntimePromotionRoot && !mcpManualFetchHandoffRoot && !mcpFetchTargetReviewRoot && !mcpFetchRunPackageRoot && !mcpFetchRunReadinessRoot && !mcpFetchRunReceiptRoot && !mcpFetchResultParserReviewRoot && !mcpFetchCandidateHandoffReviewRoot && !mcpFetchCandidateQueueReviewRoot && !mcpFetchCandidateQueueWriterPreflightRoot && !manualSampleRoot && !sampleAcceptanceRoot && !sampleReviewRoot && !schedulerRoot && !matchReviewRoot && !opportunityRoot && !opportunityScoringRoot && !opportunityEvidenceRoot && !opportunityAlertRoot && !migrationRoot && !migrationDrillRoot && !catalogReviewRoot && !liveSmokeRoot && !liveInventoryRoot && !approvalRoot && !deployRoot) return;
|
||||
|
||||
const meta = root ? root.querySelector('[data-market-intel-preview-meta]') : null;
|
||||
const body = root ? root.querySelector('[data-market-intel-preview-body]') : null;
|
||||
@@ -1482,6 +1509,12 @@
|
||||
const mcpFetchCandidateQueueReviewReview = mcpFetchCandidateQueueReviewRoot ? mcpFetchCandidateQueueReviewRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-review-review]') : null;
|
||||
const mcpFetchCandidateQueueReviewRefresh = mcpFetchCandidateQueueReviewRoot ? mcpFetchCandidateQueueReviewRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-review-refresh]') : null;
|
||||
const mcpFetchCandidateQueueReviewEndpoint = "{{ url_for('market_intel.market_intel_mcp_fetch_candidate_queue_review') }}";
|
||||
const mcpFetchCandidateQueueWriterPreflightMeta = mcpFetchCandidateQueueWriterPreflightRoot ? mcpFetchCandidateQueueWriterPreflightRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-writer-preflight-meta]') : null;
|
||||
const mcpFetchCandidateQueueWriterPreflightBody = mcpFetchCandidateQueueWriterPreflightRoot ? mcpFetchCandidateQueueWriterPreflightRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-writer-preflight-body]') : null;
|
||||
const mcpFetchCandidateQueueWriterPreflightInput = mcpFetchCandidateQueueWriterPreflightRoot ? mcpFetchCandidateQueueWriterPreflightRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-writer-preflight-input]') : null;
|
||||
const mcpFetchCandidateQueueWriterPreflightReview = mcpFetchCandidateQueueWriterPreflightRoot ? mcpFetchCandidateQueueWriterPreflightRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-writer-preflight-review]') : null;
|
||||
const mcpFetchCandidateQueueWriterPreflightRefresh = mcpFetchCandidateQueueWriterPreflightRoot ? mcpFetchCandidateQueueWriterPreflightRoot.querySelector('[data-market-intel-mcp-fetch-candidate-queue-writer-preflight-refresh]') : null;
|
||||
const mcpFetchCandidateQueueWriterPreflightEndpoint = "{{ url_for('market_intel.market_intel_mcp_fetch_candidate_queue_writer_preflight') }}";
|
||||
const manualSampleMeta = manualSampleRoot ? manualSampleRoot.querySelector('[data-market-intel-manual-sample-meta]') : null;
|
||||
const manualSampleBody = manualSampleRoot ? manualSampleRoot.querySelector('[data-market-intel-manual-sample-body]') : null;
|
||||
const manualSampleRefresh = manualSampleRoot ? manualSampleRoot.querySelector('[data-market-intel-manual-sample-refresh]') : null;
|
||||
@@ -3819,6 +3852,135 @@
|
||||
}
|
||||
};
|
||||
|
||||
const renderMcpFetchCandidateQueueWriterPreflightMeta = data => {
|
||||
mcpFetchCandidateQueueWriterPreflightMeta.innerHTML = [
|
||||
`mode=${data.mode || 'unknown'}`,
|
||||
`accepted=${data.mcp_fetch_candidate_queue_writer_preflight_accepted ? 'yes' : 'no'}`,
|
||||
`gates=${data.passed_gate_count || 0}/${data.gate_count || 0}`,
|
||||
`rows=${data.payload_row_count || 0}`,
|
||||
`campaigns=${data.campaign_candidate_count || 0}`,
|
||||
`products=${data.product_candidate_count || 0}`
|
||||
].map(item => `<span class="market-intel-pill">${escapeHtml(item)}</span>`).join('');
|
||||
};
|
||||
|
||||
const renderMcpFetchCandidateQueueWriterPreflightBody = data => {
|
||||
const blockers = (data.blocked_reasons || []).join(' / ');
|
||||
const gates = data.gates || [];
|
||||
const queue = data.queue_review_summary || {};
|
||||
const preflight = data.writer_preflight_summary || {};
|
||||
const rows = preflight.payload_rows || [];
|
||||
const steps = data.next_operator_steps || [];
|
||||
const renderCheck = (key, label, status) => `
|
||||
<div class="market-intel-check">
|
||||
<div>
|
||||
<strong>${escapeHtml(key)}</strong>
|
||||
<small>${escapeHtml(label || '')}</small>
|
||||
</div>
|
||||
<span>${escapeHtml(status)}</span>
|
||||
</div>
|
||||
`;
|
||||
mcpFetchCandidateQueueWriterPreflightBody.innerHTML = `
|
||||
<div class="market-intel-empty mb-3">此 writer preflight 只審核未來 CLI writer 草案;API 不開 DB、不建立 transaction、不執行 CLI、不寫 queue、不掛 scheduler。${blockers ? `阻擋:${escapeHtml(blockers)}` : ''}</div>
|
||||
<div class="market-intel-deploy-grid">
|
||||
<div data-market-intel-mcp-fetch-candidate-queue-writer-preflight-gates>
|
||||
<p class="market-intel-deploy-section-title">WRITER PREFLIGHT GATES</p>
|
||||
<div class="market-intel-check-list">${
|
||||
gates.length
|
||||
? gates.map(item => renderCheck(item.key, item.label, item.passed ? 'PASS' : 'BLOCK')).join('')
|
||||
: '<div class="market-intel-empty">尚未提供 writer preflight gates。</div>'
|
||||
}</div>
|
||||
</div>
|
||||
<div data-market-intel-mcp-fetch-candidate-queue-writer-preflight-queue>
|
||||
<p class="market-intel-deploy-section-title">QUEUE REVIEW LINK</p>
|
||||
<div class="market-intel-check-list">
|
||||
${renderCheck('queue_review', `${queue.accepted ? 'accepted' : 'pending'} / items=${queue.review_item_count || 0}`, queue.accepted ? 'ACCEPTED' : 'PENDING')}
|
||||
${renderCheck('writer_ready', queue.ready_for_candidate_queue_writer_preflight ? 'ready_for_writer_preflight' : 'not_ready', queue.ready_for_candidate_queue_writer_preflight ? 'READY' : 'BLOCK')}
|
||||
${renderCheck('queue_boundary', 'no queue create / no DB / no scheduler', queue.side_effects_clear ? 'CLOSED' : 'BLOCK')}
|
||||
</div>
|
||||
</div>
|
||||
<div data-market-intel-mcp-fetch-candidate-queue-writer-preflight-payload>
|
||||
<p class="market-intel-deploy-section-title">WRITER PAYLOAD ROWS</p>
|
||||
<div class="market-intel-check-list">${
|
||||
rows.slice(0, 10).map(item => renderCheck(
|
||||
item.candidate_key || 'candidate',
|
||||
`${item.candidate_type || 'candidate'} / ${item.review_state || 'missing'} / ${item.priority_lane || 'lane'}`,
|
||||
item.required_fields_present ? 'READY' : 'BLOCK'
|
||||
)).join('') || '<div class="market-intel-empty">尚未提供 writer payload rows。</div>'
|
||||
}</div>
|
||||
</div>
|
||||
<div data-market-intel-mcp-fetch-candidate-queue-writer-preflight-columns>
|
||||
<p class="market-intel-deploy-section-title">WRITE PLAN</p>
|
||||
<div class="market-intel-check-list">
|
||||
${renderCheck('target_table', preflight.target_table || 'missing', preflight.target_table === 'market_alert_review_queue' ? 'TARGET' : 'BLOCK')}
|
||||
${renderCheck('write_mode', preflight.write_mode || 'missing', preflight.write_mode === 'cli_only_later' ? 'CLI LATER' : 'BLOCK')}
|
||||
${renderCheck('dedupe_strategy', preflight.dedupe_strategy || 'missing', preflight.dedupe_strategy === 'candidate_type_platform_source_key' ? 'READY' : 'BLOCK')}
|
||||
${renderCheck('required_columns', 'candidate queue insert columns', preflight.insert_columns_cover_required ? 'READY' : 'BLOCK')}
|
||||
</div>
|
||||
</div>
|
||||
<div data-market-intel-mcp-fetch-candidate-queue-writer-preflight-next>
|
||||
<p class="market-intel-deploy-section-title">BOUNDARY / NEXT</p>
|
||||
<div class="market-intel-check-list">
|
||||
${renderCheck(
|
||||
'api_boundary',
|
||||
'no DB / no transaction / no CLI / no queue write / no scheduler',
|
||||
data.api_writes_database ? 'BLOCK' : 'CLOSED'
|
||||
)}
|
||||
${steps.map((item, index) => renderCheck(`step_${index + 1}`, item, 'NEXT')).join('')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
if (mcpFetchCandidateQueueWriterPreflightInput && !mcpFetchCandidateQueueWriterPreflightInput.value.trim() && data.sample_writer_preflight_package) {
|
||||
mcpFetchCandidateQueueWriterPreflightInput.value = JSON.stringify(data.sample_writer_preflight_package, null, 2);
|
||||
}
|
||||
};
|
||||
|
||||
const loadMcpFetchCandidateQueueWriterPreflight = async () => {
|
||||
if (!mcpFetchCandidateQueueWriterPreflightMeta || !mcpFetchCandidateQueueWriterPreflightBody) return;
|
||||
mcpFetchCandidateQueueWriterPreflightBody.innerHTML = '<div class="market-intel-empty">讀取 MCP Candidate Queue Writer Preflight 中...</div>';
|
||||
try {
|
||||
const response = await fetch(mcpFetchCandidateQueueWriterPreflightEndpoint, { credentials: 'same-origin' });
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const data = await response.json();
|
||||
renderMcpFetchCandidateQueueWriterPreflightMeta(data);
|
||||
renderMcpFetchCandidateQueueWriterPreflightBody(data);
|
||||
} catch (error) {
|
||||
mcpFetchCandidateQueueWriterPreflightMeta.innerHTML = '<span class="market-intel-pill">error</span>';
|
||||
mcpFetchCandidateQueueWriterPreflightBody.innerHTML = `<div class="market-intel-empty">MCP Candidate Queue Writer Preflight 讀取失敗:${escapeHtml(error.message)}</div>`;
|
||||
}
|
||||
};
|
||||
|
||||
const reviewMcpFetchCandidateQueueWriterPreflight = async () => {
|
||||
if (!mcpFetchCandidateQueueWriterPreflightMeta || !mcpFetchCandidateQueueWriterPreflightBody || !mcpFetchCandidateQueueWriterPreflightInput) return;
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(mcpFetchCandidateQueueWriterPreflightInput.value || '{}');
|
||||
} catch (error) {
|
||||
mcpFetchCandidateQueueWriterPreflightMeta.innerHTML = '<span class="market-intel-pill">json_error</span>';
|
||||
mcpFetchCandidateQueueWriterPreflightBody.innerHTML = `<div class="market-intel-empty">JSON 格式錯誤:${escapeHtml(error.message)}</div>`;
|
||||
return;
|
||||
}
|
||||
mcpFetchCandidateQueueWriterPreflightBody.innerHTML = '<div class="market-intel-empty">審核 MCP Candidate Queue Writer Preflight 中...</div>';
|
||||
try {
|
||||
const response = await fetch(mcpFetchCandidateQueueWriterPreflightEndpoint, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({ candidate_queue_writer_preflight: parsed })
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok && !data.mode) throw new Error(`HTTP ${response.status}`);
|
||||
renderMcpFetchCandidateQueueWriterPreflightMeta(data);
|
||||
renderMcpFetchCandidateQueueWriterPreflightBody(data);
|
||||
} catch (error) {
|
||||
mcpFetchCandidateQueueWriterPreflightMeta.innerHTML = '<span class="market-intel-pill">error</span>';
|
||||
mcpFetchCandidateQueueWriterPreflightBody.innerHTML = `<div class="market-intel-empty">MCP Candidate Queue Writer Preflight 審核失敗:${escapeHtml(error.message)}</div>`;
|
||||
}
|
||||
};
|
||||
|
||||
const renderManualSampleMeta = data => {
|
||||
manualSampleMeta.innerHTML = [
|
||||
`mode=${data.mode || 'unknown'}`,
|
||||
@@ -13288,6 +13450,12 @@
|
||||
if (mcpFetchCandidateQueueReviewReview) {
|
||||
mcpFetchCandidateQueueReviewReview.addEventListener('click', reviewMcpFetchCandidateQueueReview);
|
||||
}
|
||||
if (mcpFetchCandidateQueueWriterPreflightRefresh) {
|
||||
mcpFetchCandidateQueueWriterPreflightRefresh.addEventListener('click', loadMcpFetchCandidateQueueWriterPreflight);
|
||||
}
|
||||
if (mcpFetchCandidateQueueWriterPreflightReview) {
|
||||
mcpFetchCandidateQueueWriterPreflightReview.addEventListener('click', reviewMcpFetchCandidateQueueWriterPreflight);
|
||||
}
|
||||
if (manualSampleRefresh) {
|
||||
manualSampleRefresh.addEventListener('click', loadManualSample);
|
||||
}
|
||||
@@ -13553,6 +13721,7 @@
|
||||
loadMcpFetchResultParserReview();
|
||||
loadMcpFetchCandidateHandoffReview();
|
||||
loadMcpFetchCandidateQueueReview();
|
||||
loadMcpFetchCandidateQueueWriterPreflight();
|
||||
loadManualSample();
|
||||
loadSampleAcceptance();
|
||||
loadSampleReview();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user