walk_graph_json.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """V3 游走配置入口(M4A):读 walk_graph / walk_policy / platform_profiles 并校验。
  2. 镜像 walk_strategy_json.WalkStrategyStore 的 load+validate+raise 结构。
  3. walk_policy 的拍板值可能带 {value, provenance, tbd} 包裹(留痕),load_policy 解包成纯值。
  4. """
  5. from __future__ import annotations
  6. import os
  7. from dataclasses import dataclass
  8. from pathlib import Path
  9. from typing import Any
  10. from content_agent.findings import fail as _fail
  11. WALK_GRAPH_PATH = Path("tech_documents/数据接口与来源/walk_graph.json")
  12. WALK_POLICY_PATH = Path("tech_documents/数据接口与来源/walk_policy.json")
  13. PROFILE_DIR = Path("tech_documents/数据接口与来源/platform_profiles")
  14. EDGE_CATALOG_PATH = Path("product_documents/抖音游走策略/douyin_walk_strategy.v1.json")
  15. PERMISSION_ACTIONS = [
  16. "ADD_TO_CONTENT_POOL",
  17. "KEEP_CONTENT_FOR_REVIEW",
  18. "TECHNICAL_RETRY_REQUIRED",
  19. "REJECT_CONTENT",
  20. ]
  21. _PERMISSION_META_KEYS = {"_meaning", "_provenance", "note", "tbd"}
  22. def _unwrap(value: Any) -> Any:
  23. if isinstance(value, dict) and "value" in value:
  24. return value["value"]
  25. return value
  26. def low_budget(budget: int) -> int:
  27. """low_budget 档:减半向下取整,至少 1(2026-06-11 拍板,policy 值 halve_min_1)。"""
  28. return max(1, budget // 2)
  29. def edge_permission(policy: dict[str, Any], decision_action: str | None, edge_id: str) -> str:
  30. """判定结果 → 该出边通行证;无判定/未列入的组合一律 deny(从严)。"""
  31. row = policy["edge_permissions"].get(decision_action or "", {})
  32. return row.get(edge_id, "deny")
  33. def edge_supported(profile: dict[str, Any], edge_id: str) -> bool:
  34. """profile 未列出的边 = 平台无关控制边(终端边),恒 supported。"""
  35. return profile["edges"].get(edge_id, {"status": "supported"})["status"] == "supported"
  36. @dataclass(frozen=True)
  37. class WalkGraphStore:
  38. root_dir: Path = Path(".")
  39. def load_graph(self) -> dict[str, Any]:
  40. from content_agent.integrations import config_store
  41. graph, _ = config_store.load_json(self.root_dir / WALK_GRAPH_PATH)
  42. catalog, _ = config_store.load_json(self.root_dir / EDGE_CATALOG_PATH)
  43. catalog_ids = {row["edge_id"] for row in catalog["walk_edge_catalog"]}
  44. _raise_on_fail(_validate_graph(graph, catalog_ids), "walk graph")
  45. return graph
  46. def load_policy(self) -> dict[str, Any]:
  47. from content_agent.integrations import config_store
  48. raw_policy, _ = config_store.load_json(self.root_dir / WALK_POLICY_PATH)
  49. policy = _unwrap_policy(raw_policy)
  50. edge_ids = {edge["edge_id"] for edge in self.load_graph()["edges"]}
  51. _raise_on_fail(_validate_policy(policy, edge_ids), "walk policy")
  52. return policy
  53. def load_profile(self, platform: str) -> dict[str, Any]:
  54. from content_agent.integrations import config_store
  55. profile, _ = config_store.load_json(self.root_dir / PROFILE_DIR / f"{platform}.json")
  56. edge_ids = {edge["edge_id"] for edge in self.load_graph()["edges"]}
  57. _raise_on_fail(_validate_profile(profile, edge_ids), f"platform profile {platform}")
  58. return profile
  59. def _unwrap_policy(raw: dict[str, Any]) -> dict[str, Any]:
  60. policy = dict(raw)
  61. policy["global"] = {key: _unwrap(value) for key, value in raw["global"].items()}
  62. _apply_global_env_overrides(policy["global"])
  63. policy["edge_permissions"] = {
  64. action: {edge: _unwrap(perm) for edge, perm in row.items() if edge not in _PERMISSION_META_KEYS}
  65. for action, row in raw["edge_permissions"].items()
  66. if action in PERMISSION_ACTIONS
  67. }
  68. policy["budget_tiers"] = {key: _unwrap(value) for key, value in raw["budget_tiers"].items()}
  69. policy["edge_budgets_by_id"] = {row["edge_id"]: row for row in raw["edge_budgets"]}
  70. return policy
  71. def _apply_global_env_overrides(global_policy: dict[str, Any]) -> None:
  72. overrides = {
  73. "CONTENT_AGENT_WALK_MAX_TOTAL_ACTIONS_PER_RUN": "max_total_actions_per_run",
  74. "CONTENT_AGENT_WALK_MAX_DEPTH": "max_depth",
  75. "CONTENT_AGENT_GEMINI_MAX_WORKERS": "gemini_max_workers",
  76. }
  77. for env_key, policy_key in overrides.items():
  78. value = _optional_positive_int(os.environ.get(env_key))
  79. if value is not None:
  80. global_policy[policy_key] = value
  81. def _optional_positive_int(value: str | None) -> int | None:
  82. if value is None or value == "":
  83. return None
  84. try:
  85. parsed = int(value)
  86. except ValueError:
  87. return None
  88. return parsed if parsed > 0 else None
  89. def _validate_graph(graph: dict[str, Any], catalog_ids: set[str]) -> list[dict[str, Any]]:
  90. findings: list[dict[str, Any]] = []
  91. node_types = {node["node_type"] for node in graph.get("nodes", [])}
  92. for edge in graph.get("edges", []):
  93. if edge.get("edge_id") not in catalog_ids:
  94. _fail(findings, "edge_not_in_catalog", f"{edge.get('edge_id')} not in walk_edge_catalog")
  95. for field in ["from_node", "to_node"]:
  96. if edge.get(field) not in node_types:
  97. _fail(findings, "edge_node_ref", f"{edge.get('edge_id')} unknown {field}: {edge.get(field)}")
  98. return findings
  99. def _validate_policy(policy: dict[str, Any], edge_ids: set[str]) -> list[dict[str, Any]]:
  100. findings: list[dict[str, Any]] = []
  101. for row in policy.get("edge_budgets", []):
  102. if row.get("edge_id") not in edge_ids:
  103. _fail(findings, "budget_edge_ref", f"edge_budgets unknown edge_id: {row.get('edge_id')}")
  104. permission_rows = policy.get("edge_permissions", {})
  105. key_sets = []
  106. for action in PERMISSION_ACTIONS:
  107. if action not in permission_rows:
  108. _fail(findings, "permission_action_missing", f"edge_permissions missing action: {action}")
  109. continue
  110. keys = set(permission_rows[action])
  111. key_sets.append(keys)
  112. for edge_id in keys - edge_ids:
  113. _fail(findings, "permission_edge_ref", f"{action} unknown edge_id: {edge_id}")
  114. if key_sets and any(keys != key_sets[0] for keys in key_sets[1:]):
  115. _fail(findings, "permission_closure", "edge_permissions edge keys differ across actions")
  116. return findings
  117. def _validate_profile(profile: dict[str, Any], edge_ids: set[str]) -> list[dict[str, Any]]:
  118. findings: list[dict[str, Any]] = []
  119. for edge_id, spec in profile.get("edges", {}).items():
  120. if edge_id not in edge_ids:
  121. _fail(findings, "profile_edge_ref", f"profile unknown edge_id: {edge_id}")
  122. if spec.get("status") not in {"supported", "blocked"}:
  123. _fail(findings, "profile_edge_status", f"{edge_id} invalid status: {spec.get('status')}")
  124. return findings
  125. def _raise_on_fail(findings: list[dict[str, Any]], label: str) -> None:
  126. failures = [finding for finding in findings if finding["level"] == "fail"]
  127. if failures:
  128. raise ValueError(f"invalid {label} config: {failures}")