| 1 | """Tests for clarify grounding and brownfield repo evidence.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | |
| 7 | from loader.runtime.clarify_grounding import ( |
| 8 | ClarifyGrounding, |
| 9 | ClarifyGroundingProbe, |
| 10 | ClarifyRepoFact, |
| 11 | build_grounded_clarify_question, |
| 12 | ) |
| 13 | from loader.runtime.clarify_strategy import ClarifyPressureKind, ClarifySlot |
| 14 | |
| 15 | |
| 16 | def test_probe_collects_workspace_references_and_candidate_touchpoints( |
| 17 | tmp_path: Path, |
| 18 | ) -> None: |
| 19 | (tmp_path / "pyproject.toml").write_text("[project]\nname='loader'\n") |
| 20 | (tmp_path / "src" / "loader" / "runtime").mkdir(parents=True) |
| 21 | (tmp_path / "src" / "loader" / "runtime" / "workflow_lanes.py").write_text( |
| 22 | '"""Runtime lane orchestration for Loader."""\n\n' |
| 23 | "class WorkflowLaneRunner:\n" |
| 24 | " pass\n" |
| 25 | ) |
| 26 | (tmp_path / "src" / "loader" / "runtime" / "clarify_strategy.py").write_text( |
| 27 | '"""Intent-aware clarify strategy for runtime follow-up."""\n' |
| 28 | ) |
| 29 | (tmp_path / "tests").mkdir() |
| 30 | (tmp_path / "tests" / "test_workflow_runtime.py").write_text("pass\n") |
| 31 | |
| 32 | grounding = ClarifyGroundingProbe(tmp_path).collect( |
| 33 | task=( |
| 34 | "Tighten Loader runtime clarify behavior around " |
| 35 | "src/loader/runtime/workflow_lanes.py without broad test churn." |
| 36 | ) |
| 37 | ) |
| 38 | |
| 39 | assert grounding.project_type == "python" |
| 40 | assert "pyproject.toml" in grounding.top_level_entries |
| 41 | assert "src/" in grounding.top_level_entries |
| 42 | assert "src/loader/runtime/workflow_lanes.py" in grounding.existing_references |
| 43 | assert any("clarify_strategy.py" in path for path in grounding.candidate_touchpoints) |
| 44 | assert grounding.repo_facts |
| 45 | assert grounding.repo_facts[0].path == "src/loader/runtime/workflow_lanes.py" |
| 46 | assert "WorkflowLaneRunner" in grounding.repo_facts[0].summary |
| 47 | |
| 48 | |
| 49 | def test_grounding_prompt_block_renders_repo_evidence() -> None: |
| 50 | grounding = ClarifyGrounding( |
| 51 | project_type="python", |
| 52 | top_level_entries=["src/", "tests/", "pyproject.toml"], |
| 53 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 54 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 55 | repo_facts=[ |
| 56 | ClarifyRepoFact( |
| 57 | path="src/loader/runtime/workflow_lanes.py", |
| 58 | summary="class WorkflowLaneRunner:", |
| 59 | ) |
| 60 | ], |
| 61 | missing_references=["src/loader/runtime/unknown.py"], |
| 62 | ) |
| 63 | |
| 64 | block = grounding.prompt_block() |
| 65 | |
| 66 | assert "Project type: python" in block |
| 67 | assert "Referenced paths that exist: src/loader/runtime/workflow_lanes.py" in block |
| 68 | assert "Nearby repo touchpoints: src/loader/runtime/clarify_strategy.py" in block |
| 69 | assert ( |
| 70 | "Observed repo facts: `src/loader/runtime/workflow_lanes.py`: " |
| 71 | "class WorkflowLaneRunner:" |
| 72 | ) in block |
| 73 | assert "Referenced paths not found: src/loader/runtime/unknown.py" in block |
| 74 | |
| 75 | |
| 76 | def test_slot_prompt_block_prefers_primary_and_nearby_facts_for_non_goals() -> None: |
| 77 | grounding = ClarifyGrounding( |
| 78 | project_type="python", |
| 79 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 80 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 81 | repo_facts=[ |
| 82 | ClarifyRepoFact( |
| 83 | path="src/loader/runtime/workflow_lanes.py", |
| 84 | summary="class WorkflowLaneRunner:", |
| 85 | ), |
| 86 | ClarifyRepoFact( |
| 87 | path="src/loader/runtime/clarify_strategy.py", |
| 88 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 89 | ), |
| 90 | ], |
| 91 | ) |
| 92 | |
| 93 | block = grounding.slot_prompt_block(ClarifySlot.NON_GOALS) |
| 94 | |
| 95 | assert "Relevant repo facts" in block |
| 96 | assert "workflow_lanes.py" in block |
| 97 | assert "clarify_strategy.py" in block |
| 98 | assert "Relevant paths: src/loader/runtime/workflow_lanes.py" in block |
| 99 | |
| 100 | |
| 101 | def test_slot_prompt_block_adds_secondary_touchpoint_for_example_pressure() -> None: |
| 102 | grounding = ClarifyGrounding( |
| 103 | project_type="python", |
| 104 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 105 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 106 | repo_facts=[ |
| 107 | ClarifyRepoFact( |
| 108 | path="src/loader/runtime/workflow_lanes.py", |
| 109 | summary="class WorkflowLaneRunner:", |
| 110 | ), |
| 111 | ClarifyRepoFact( |
| 112 | path="src/loader/runtime/clarify_strategy.py", |
| 113 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 114 | ), |
| 115 | ], |
| 116 | ) |
| 117 | |
| 118 | block = grounding.slot_prompt_block( |
| 119 | ClarifySlot.LIKELY_TOUCHPOINTS, |
| 120 | ClarifyPressureKind.EXAMPLE, |
| 121 | ) |
| 122 | |
| 123 | assert "Relevant repo facts" in block |
| 124 | assert "workflow_lanes.py" in block |
| 125 | assert "clarify_strategy.py" in block |
| 126 | assert "Relevant paths: src/loader/runtime/workflow_lanes.py" in block |
| 127 | |
| 128 | |
| 129 | def test_brief_hints_seed_touchpoints_constraints_and_acceptance() -> None: |
| 130 | grounding = ClarifyGrounding( |
| 131 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 132 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 133 | repo_facts=[ |
| 134 | ClarifyRepoFact( |
| 135 | path="src/loader/runtime/workflow_lanes.py", |
| 136 | summary="class WorkflowLaneRunner:", |
| 137 | ), |
| 138 | ClarifyRepoFact( |
| 139 | path="src/loader/runtime/clarify_strategy.py", |
| 140 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 141 | ), |
| 142 | ], |
| 143 | ) |
| 144 | |
| 145 | hints = grounding.brief_hints() |
| 146 | |
| 147 | assert hints.likely_touchpoints == [ |
| 148 | "src/loader/runtime/workflow_lanes.py", |
| 149 | "src/loader/runtime/clarify_strategy.py", |
| 150 | ] |
| 151 | assert any("workflow_lanes.py" in item for item in hints.constraints) |
| 152 | assert any("clarify_strategy.py" in item for item in hints.constraints) |
| 153 | assert any("WorkflowLaneRunner" in item for item in hints.assumptions) |
| 154 | assert any("Primary work stays scoped" in item for item in hints.acceptance_criteria) |
| 155 | |
| 156 | |
| 157 | def test_brief_prompt_block_renders_grounded_brief_hints() -> None: |
| 158 | grounding = ClarifyGrounding( |
| 159 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 160 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 161 | repo_facts=[ |
| 162 | ClarifyRepoFact( |
| 163 | path="src/loader/runtime/workflow_lanes.py", |
| 164 | summary="class WorkflowLaneRunner:", |
| 165 | ), |
| 166 | ClarifyRepoFact( |
| 167 | path="src/loader/runtime/clarify_strategy.py", |
| 168 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 169 | ), |
| 170 | ], |
| 171 | ) |
| 172 | |
| 173 | block = grounding.brief_prompt_block() |
| 174 | |
| 175 | assert "Seed likely touchpoints" in block |
| 176 | assert "Preserve constraints" in block |
| 177 | assert "Grounded assumptions" in block |
| 178 | assert "Scope acceptance criteria" in block |
| 179 | assert "workflow_lanes.py" in block |
| 180 | assert "clarify_strategy.py" in block |
| 181 | |
| 182 | |
| 183 | def test_build_grounded_question_anchors_touchpoint_tradeoff_with_repo_fact() -> None: |
| 184 | question = build_grounded_clarify_question( |
| 185 | task="Tighten Loader runtime clarify behavior.", |
| 186 | focus_slot=ClarifySlot.LIKELY_TOUCHPOINTS, |
| 187 | grounding=ClarifyGrounding( |
| 188 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 189 | repo_facts=[ |
| 190 | ClarifyRepoFact( |
| 191 | path="src/loader/runtime/workflow_lanes.py", |
| 192 | summary="class WorkflowLaneRunner:", |
| 193 | ) |
| 194 | ], |
| 195 | ), |
| 196 | pressure_kind=ClarifyPressureKind.TRADEOFF, |
| 197 | ) |
| 198 | |
| 199 | assert question is not None |
| 200 | assert "workflow_lanes.py" in question |
| 201 | assert "WorkflowLaneRunner" in question |
| 202 | assert "stay unchanged" in question.lower() |
| 203 | |
| 204 | |
| 205 | def test_build_grounded_question_uses_counterexample_surface_for_example_pressure() -> None: |
| 206 | question = build_grounded_clarify_question( |
| 207 | task="Tighten Loader runtime clarify behavior.", |
| 208 | focus_slot=ClarifySlot.LIKELY_TOUCHPOINTS, |
| 209 | grounding=ClarifyGrounding( |
| 210 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 211 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 212 | repo_facts=[ |
| 213 | ClarifyRepoFact( |
| 214 | path="src/loader/runtime/workflow_lanes.py", |
| 215 | summary="class WorkflowLaneRunner:", |
| 216 | ), |
| 217 | ClarifyRepoFact( |
| 218 | path="src/loader/runtime/clarify_strategy.py", |
| 219 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 220 | ), |
| 221 | ], |
| 222 | ), |
| 223 | pressure_kind=ClarifyPressureKind.EXAMPLE, |
| 224 | ) |
| 225 | |
| 226 | assert question is not None |
| 227 | assert "counterexample surface" in question |
| 228 | assert "clarify_strategy.py" in question |
| 229 | |
| 230 | |
| 231 | def test_build_grounded_question_uses_nearby_fact_for_decision_boundaries() -> None: |
| 232 | question = build_grounded_clarify_question( |
| 233 | task="Tighten Loader runtime clarify behavior.", |
| 234 | focus_slot=ClarifySlot.DECISION_BOUNDARIES, |
| 235 | grounding=ClarifyGrounding( |
| 236 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 237 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 238 | repo_facts=[ |
| 239 | ClarifyRepoFact( |
| 240 | path="src/loader/runtime/workflow_lanes.py", |
| 241 | summary="class WorkflowLaneRunner:", |
| 242 | ), |
| 243 | ClarifyRepoFact( |
| 244 | path="src/loader/runtime/clarify_strategy.py", |
| 245 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 246 | ), |
| 247 | ], |
| 248 | ), |
| 249 | pressure_kind=ClarifyPressureKind.TRADEOFF, |
| 250 | ) |
| 251 | |
| 252 | assert question is not None |
| 253 | assert "workflow_lanes.py" in question |
| 254 | assert "clarify_strategy.py" in question |
| 255 | assert "stop-and-confirm boundary" in question |
| 256 | |
| 257 | |
| 258 | def test_build_grounded_question_uses_risky_assumption_for_non_goal_pressure() -> None: |
| 259 | question = build_grounded_clarify_question( |
| 260 | task="Tighten Loader runtime clarify behavior.", |
| 261 | focus_slot=ClarifySlot.NON_GOALS, |
| 262 | grounding=ClarifyGrounding( |
| 263 | existing_references=["src/loader/runtime/workflow_lanes.py"], |
| 264 | candidate_touchpoints=["src/loader/runtime/clarify_strategy.py"], |
| 265 | repo_facts=[ |
| 266 | ClarifyRepoFact( |
| 267 | path="src/loader/runtime/workflow_lanes.py", |
| 268 | summary="class WorkflowLaneRunner:", |
| 269 | ), |
| 270 | ClarifyRepoFact( |
| 271 | path="src/loader/runtime/clarify_strategy.py", |
| 272 | summary="Intent-aware clarify strategy for runtime follow-up.", |
| 273 | ), |
| 274 | ], |
| 275 | ), |
| 276 | pressure_kind=ClarifyPressureKind.ASSUMPTION, |
| 277 | ) |
| 278 | |
| 279 | assert question is not None |
| 280 | assert "risky" in question.lower() |
| 281 | assert "clarify_strategy.py" in question |