"""Prompt/file contracts, not a substitute for trace-based execution validation.""" import json from pathlib import Path import pytest from tests.test_diagnosis_orchestration_config import _catalog_rows ROOT = Path(__file__).resolve().parents[2] CONFIG = ROOT / "agentkit/config" SKILLS = ROOT / "agentkit/skills" CLINIC = SKILLS / "builtin/tidb-clinic-diagnosis" def test_default_loading_does_not_require_general_manual(): investigator = (CONFIG / "investibook/agents/investigator.md").read_text() analyst = (CONFIG / "investibook/agents/diagnosis-analyst.md").read_text() catalog = (CONFIG / "SKILL_CATALOG.md").read_text() entry = (CLINIC / "SKILL.md").read_text() assert "primary playbook" not in analyst assert "primary manual" not in analyst assert "default `core` + `tidb-clinic-diagnosis`" not in investigator assert "do not add the root `tidb-clinic-diagnosis` manual" in investigator assert "not a default required read" in catalog assert "General-method Fallback" in analyst assert "Low confidence, missing" in analyst assert "record the requested name/path and failure" in analyst assert len(entry.splitlines()) < 100 assert "### Node C:" not in entry assert "references/general-diagnosis-fallback.md" in entry assert "references/evidence-collection.md" in entry assert (CLINIC / "references/general-diagnosis-fallback.md").is_file() assert (CLINIC / "references/evidence-collection.md").is_file() @pytest.mark.parametrize( ("family", "specialist", "references"), [ ("sql-plan-or-query", "tidb-query-tuning-autox", ("SUBSKILLS_INDEX.md", "references/case-contract.md", "workflow/diagnosis-classification/SUBSKILL.md")), ("transaction-or-lock-contention", "transaction-oncall-handbook", ()), ("cdc-or-replication-lag", "ticdc-health-inspection", ("references/dashboard-mapping.md", "references/inspection-rules.md")), ], ) def test_required_specialists_resolve_to_readable_files_and_allowed_tools(family, specialist, references): catalog = {name: (group, path) for group, name, path in _catalog_rows()} family_text = (CLINIC / family / "SUBSKILL.md").read_text() assert "# Specialist Loading Contract" in family_text assert "task-selected route" in family_text assert specialist in family_text group, path = catalog[specialist] assert group == "diagnosis" assert (SKILLS / path / "SKILL.md").is_file() for reference in references: assert (SKILLS / path / reference).is_file() permissions = json.loads((CONFIG / "opencode.json.template").read_text())["agent"]["diagnosis-analyst"]["permission"] assert permissions["skill"][specialist] == "allow" assert permissions["bash"]["*"] == "deny" assert not any("collect_evidence" in command for command in permissions["bash"]) def test_optional_cdc_boundary_handbook_is_discoverable_but_not_loaded_for_lag_alone(): catalog = {name: path for _, name, path in _catalog_rows()} assert (SKILLS / catalog["tikv-cdc"] / "SKILL.md").is_file() cdc = (CLINIC / "cdc-or-replication-lag/SUBSKILL.md").read_text() assert "Lag alone does not require it" in cdc assert "never deployed" in cdc assert "rather than declaring it healthy" in cdc assert "no direct data collection" in cdc assert "not a new\n30m/24h health sweep" in cdc assert "analyst-discovered primary class" in cdc def test_nodegroup_comparability_is_in_mandatory_shared_rules(): overview = (SKILLS / "builtin/diagnosis-overview/SKILL.md").read_text() assert "within the same nodegroup" in overview assert "capacity, quota, and workload/routing differences" in overview assert "do not infer membership from node" in overview assert "comparability is unknown" in overview def test_shared_guardrails_and_multibranch_contract_survive_without_root_manual(): investigator = (CONFIG / "investibook/agents/investigator.md").read_text() analyst = (CONFIG / "investibook/agents/diagnosis-analyst.md").read_text() description = json.loads((CONFIG / "opencode.json.template").read_text())["agent"]["diagnosis-analyst"]["description"] assert "hard diagnoses" in description assert "the investigator concludes those directly" in description assert "every exact selected family subskill path" in investigator assert "never state the expected conclusion" in investigator assert "Compare every selected branch explicitly" in analyst assert "stop synthesis, read the final family" in analyst assert "classification_override" in analyst assert "canonical window" in analyst assert "High TiDB CPU → also check Go GC metrics" in analyst assert "incorrect-conclusions.json" in analyst assert "write_analyst_report.py" in analyst assert "An expected specialist missing" in investigator def test_fallback_does_not_restore_generic_vetoes_or_a_second_workflow(): fallback = (CLINIC / "references/general-diagnosis-fallback.md").read_text() assert "not a second orchestration workflow" in fallback assert "Finish through the analyst's normal report writer" in fallback assert "are **NEVER** the primary root cause" not in fallback assert "If the suspected SQL is NOT in TopSQL top 20" not in fallback assert "not proof of absence" in fallback assert "SQL specialist candidate conditions" not in fallback assert "sql-plan-or-query/SUBSKILL.md" in fallback assert "## Data Access for Additional Queries" not in fallback def test_unselected_sql_does_not_route_from_rank_or_missing_schema(): sql = (CLINIC / "sql-plan-or-query/SUBSKILL.md").read_text() assert "For analyst-discovered candidates" in sql assert "For a task-selected SQL route, load AutoX first" in sql assert "Direct mechanism signal beyond rank" in sql assert "Claim-appropriate attribution" in sql assert "incomplete workflow" in sql assert "do not substitute an uncataloged skill" in sql assert "do not add an unselected SQL branch" in sql