test_config_case_matrix.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """config x case matrix.
  2. Replays the same captured case under different configurations to prove the
  3. "foolproof config" safety net: changing config changes the case outcome,
  4. visibly, without breaking the pipeline.
  5. """
  6. from __future__ import annotations
  7. import json
  8. import shutil
  9. from pathlib import Path
  10. import pytest
  11. from content_agent.integrations.policy_json import JsonPolicyBundleStore
  12. from tests.replay_harness import replay_case
  13. ROOT = Path(__file__).resolve().parents[1]
  14. _RULE_PACK_REL = "product_documents/规则包/douyin_rule_packs.v1.json"
  15. _WALK_REL = "product_documents/抖音游走策略/douyin_walk_strategy.v1.json"
  16. def _judge_ok_block_store(root: Path) -> JsonPolicyBundleStore:
  17. """Flip the existing judge_failed gate so judge_status == ok blocks by config alone."""
  18. (root / _RULE_PACK_REL).parent.mkdir(parents=True, exist_ok=True)
  19. (root / _WALK_REL).parent.mkdir(parents=True, exist_ok=True)
  20. shutil.copy(ROOT / _WALK_REL, root / _WALK_REL)
  21. package = json.loads((ROOT / _RULE_PACK_REL).read_text(encoding="utf-8"))
  22. for pack in package.get("rule_packs", []):
  23. for gate in pack.get("hard_gates", []):
  24. if gate.get("gate_id") == "judge_failed":
  25. gate["when"]["value"] = "ok"
  26. gate["decision_action"] = "REJECT_CONTENT"
  27. gate["severity"] = "fatal"
  28. (root / _RULE_PACK_REL).write_text(json.dumps(package, ensure_ascii=False, indent=2), encoding="utf-8")
  29. return JsonPolicyBundleStore(root)
  30. def _outcome(artifacts) -> dict:
  31. return {
  32. "reasons": sorted(d.get("decision_reason_code") for d in artifacts.decisions),
  33. "effect_status_counts": artifacts.summary.get("effect_status_counts"),
  34. "pooled": artifacts.summary.get("pooled_content_count"),
  35. "rejected": artifacts.summary.get("rejected_content_count"),
  36. }
  37. def _variant_overrides(variant: str, cfg_dir: Path):
  38. if variant == "default":
  39. return None
  40. if variant == "judge_ok_block":
  41. return {"policy_store": _judge_ok_block_store(cfg_dir)}
  42. raise ValueError(variant)
  43. @pytest.mark.parametrize("variant", ["default", "judge_ok_block"])
  44. def test_matrix_real_id45(variant, tmp_path):
  45. overrides = _variant_overrides(variant, tmp_path / "cfg")
  46. artifacts = replay_case("real_id45", runtime_root=tmp_path / "rt", config_overrides=overrides)
  47. assert artifacts.state["status"] == "success" # config change must not break the chain
  48. outcome = _outcome(artifacts)
  49. if variant == "default":
  50. assert outcome["pooled"] == 2
  51. assert outcome["rejected"] == 1
  52. assert outcome["effect_status_counts"] == {
  53. "success": 2,
  54. "pending": 1,
  55. "failed": 1,
  56. "rule_blocked": 0,
  57. }
  58. else:
  59. assert outcome["pooled"] == 0
  60. assert outcome["rejected"] == 4
  61. assert outcome["effect_status_counts"]["rule_blocked"] == 4
  62. def test_judge_ok_block_changes_outcome(tmp_path):
  63. base = _outcome(replay_case("real_id45", runtime_root=tmp_path / "rt0"))
  64. blocked = _outcome(
  65. replay_case(
  66. "real_id45",
  67. runtime_root=tmp_path / "rt1",
  68. config_overrides={"policy_store": _judge_ok_block_store(tmp_path / "cfg")},
  69. )
  70. )
  71. assert base != blocked
  72. assert base["effect_status_counts"]["rule_blocked"] == 0
  73. assert base["pooled"] == 2
  74. assert blocked["reasons"] == ["v4_technical_retry_needed"] * 4
  75. assert blocked["effect_status_counts"]["rule_blocked"] == 4
  76. assert blocked["pooled"] == 0
  77. assert blocked["rejected"] == 4
  78. def test_matrix_query_profile_variant():
  79. from scripts.validate_query_prompts_config import validate_query_prompts_config
  80. config = json.loads((ROOT / "product_documents/配置/query_prompts.v1.json").read_text(encoding="utf-8"))
  81. assert validate_query_prompts_config(config) == []
  82. def test_decoupling_counterproof():
  83. # M3A removed the Content hardcode: dispatch is parametrized by target_entity,
  84. # so a non-Content (e.g. Author) pack can be routed without falling back.
  85. source = (ROOT / "content_agent/integrations/policy_json.py").read_text(encoding="utf-8")
  86. assert 'target_entity") == "Content"' not in source
  87. def test_judge_ok_block_blocks_all_walk_expansion(tmp_path):
  88. artifacts = replay_case(
  89. "real_id45",
  90. runtime_root=tmp_path / "rt",
  91. config_overrides={"policy_store": _judge_ok_block_store(tmp_path / "cfg")},
  92. )
  93. walk_actions = artifacts.files["walk_actions.jsonl"]
  94. assert not [row for row in walk_actions if row["edge_id"] == "query_next_page"]
  95. expansions = [
  96. row for row in walk_actions if row["edge_id"] in {"author_to_works", "hashtag_to_query"}
  97. ]
  98. assert expansions
  99. assert all(row["walk_status"] == "skipped" for row in expansions)
  100. assert all(row["reason_code"] == "v4_allow_walk_denied" for row in expansions)
  101. assert all(row["raw_payload"]["allow_walk"] is False for row in expansions)
  102. path_stops = [row for row in walk_actions if row["edge_id"] == "path_stop"]
  103. assert len(path_stops) == 4
  104. for row in path_stops:
  105. assert row["rule_pack_id"] == "douyin_content_discovery_rule_pack_v1"
  106. assert row["raw_payload"]["rule_pack_execution"]["executed_rule_pack_id"] == (
  107. "douyin_content_discovery_rule_pack_v1"
  108. )