Coverage for src/governed_harness/orchestration/project_setup.py: 95%
98 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
1"""Project setup questions in INTENT (#56, rule ``P1``).
3Under ``intake.projectSetup: ask`` INTENT decides deterministically whether the project is new
4or existing and asks, once per project, what nothing else establishes:
6* a **new** project: the testing strategy and the standards packs, unless the configuration
7 sets them or a person already answered; the architecture too when ``architecture.mode`` is not
8 ``agent`` (with ``agent``, the architecture call proposes options instead of a question);
9* an **existing** project: only what detection could not establish: the testing strategy when
10 the repository has no test, the standards when no pack's language is detected.
12The answers are recorded through ``harness task clarify`` like every clarification (the person,
13the question, the answer, the task digests) and, once per project, as the project setup record
14that later runs read instead of asking again. Each answer also lands in the task as a
15constraint, so the agent sees it."""
17from __future__ import annotations
19import re
20from typing import TYPE_CHECKING, Any
22from governed_harness.configuration.engineering import ARCHITECTURE_STYLES
23from governed_harness.domain.models import (
24 ClarificationQuestion,
25 ClarificationRecord,
26 Execution,
27 PhaseExecution,
28 utc_now,
29)
30from governed_harness.intake.project_kind import ProjectKind, detect_project_kind
31from governed_harness.orchestration.engineering import SETUP_FLAG
32from governed_harness.standards import BUILTIN_PACKS, detect_packs
34if TYPE_CHECKING:
35 from governed_harness.orchestration.hosts import ResultsHost
37TARGETS = ("project:architecture", "project:testing", "project:standards")
38_STYLE_WORDS = {
39 "modular monolith": "modular-monolith",
40 "modular-monolith": "modular-monolith",
41 "micro-services": "microservices",
42 "domain-driven": "ddd",
43 "ports and adapters": "hexagonal",
44 "event driven": "event-driven",
45 "command query": "cqrs",
46}
49def parse_testing(answer: str) -> str | None:
50 text = answer.lower()
51 if re.search(r"\btdd\b|test[- ]driven|test[- ]first", text):
52 return "tdd"
53 if re.search(r"\bbdd\b|behaviou?r[- ]driven|gherkin|cucumber|\bbehave\b", text):
54 return "bdd"
55 if re.search(r"conventional|tests? after|regular|standard|unit tests?", text): 55 ↛ 57line 55 didn't jump to line 57 because the condition on line 55 was always true
56 return "conventional"
57 return None
60def parse_architecture(answer: str) -> str:
61 text = answer.lower()
62 for phrase, style in _STYLE_WORDS.items():
63 if phrase in text:
64 return style
65 for style in ARCHITECTURE_STYLES: 65 ↛ 68line 65 didn't jump to line 68 because the loop on line 65 didn't complete
66 if re.search(rf"\b{re.escape(style)}\b", text):
67 return style
68 return "custom"
71def parse_standards(answer: str, detected: list[str]) -> list[str]:
72 text = answer.lower()
73 named = [item for item in BUILTIN_PACKS if re.search(rf"\b{re.escape(item)}\b", text)]
74 if named:
75 return named
76 if re.search(r"\b(default|detected|yes|ok|recommended)\b", text): 76 ↛ 78line 76 didn't jump to line 78 because the condition on line 76 was always true
77 return detected
78 return []
81class ProjectSetup:
82 def __init__(self, results: ResultsHost) -> None:
83 self.results = results
85 @property
86 def enabled(self) -> bool:
87 intake = self.results.project.intake
88 return bool(intake and intake.project_setup == "ask")
90 def record(self, project_id: str) -> dict[str, Any]:
91 return self.results.engineering.setup_record(project_id)
93 def questions(
94 self, execution: Execution, phase: PhaseExecution, start: int
95 ) -> tuple[ClarificationQuestion, ...]:
96 if not self.enabled:
97 return ()
98 workspace = self.results.s.paths.workspace
99 kind = detect_project_kind(workspace)
100 answered = self.record(execution.project_id)
101 detected_packs = detect_packs(workspace, self.results.engineering.technologies())
102 asked = (
103 self._architecture_question(kind, answered),
104 self._testing_question(execution, kind, answered),
105 self._standards_question(kind, answered, detected_packs),
106 )
107 items = [item for item in asked if item is not None]
108 self.results.s.events.append(
109 execution.execution_id,
110 "project.setup.assessed",
111 {
112 "projectKind": kind.as_dict(),
113 "asked": [target for target, _text in items],
114 "answered": sorted(
115 key for key in answered if key in {"architecture", "testing", "standards"}
116 ),
117 },
118 phase_execution_id=phase.phase_execution_id,
119 )
120 return tuple(
121 ClarificationQuestion.model_validate(
122 {
123 "questionId": f"Q-{number}",
124 "ruleId": "P1",
125 "target": target,
126 "text": text,
127 "category": "project-setup",
128 }
129 )
130 for number, (target, text) in enumerate(items, start=start)
131 )
133 def _architecture_question(
134 self, kind: ProjectKind, answered: dict[str, Any]
135 ) -> tuple[str, str] | None:
136 """Asked for a new project whose architecture nothing settles yet."""
137 architecture = self.results.project.architecture
138 configured = bool(
139 architecture
140 and (architecture.style or architecture.layers or architecture.agent_enabled)
141 )
142 if not kind.new or configured or "architecture" in answered:
143 return None
144 return (
145 "project:architecture",
146 "This is a new project. Which architecture should it follow (ddd, "
147 "hexagonal, clean, layered, modular-monolith, microservices, mvvm, mvi or "
148 "custom), and which layers may depend on which?",
149 )
151 def _testing_question(
152 self, execution: Execution, kind: ProjectKind, answered: dict[str, Any]
153 ) -> tuple[str, str] | None:
154 """Asked when the testing strategy is neither configured nor answered, for a new
155 project or one whose strategy is not detected."""
156 testing = self.results.project.testing
157 if bool(testing and testing.strategy not in {None, "auto"}) or "testing" in answered:
158 return None
159 strategy = self.results.engineering.strategy(execution.project_id)
160 if not kind.new and strategy.strategy != "unknown":
161 return None
162 detected = f" Detected: {strategy.strategy}." if strategy.strategy != "unknown" else ""
163 return (
164 "project:testing",
165 "Which testing strategy should this project follow: tdd (tests first, "
166 "red-green-refactor checked by the harness), bdd (criteria as Gherkin "
167 "scenarios a person approves) or conventional (tests with the code)?" + detected,
168 )
170 def _standards_question(
171 self, kind: ProjectKind, answered: dict[str, Any], detected_packs: list[str]
172 ) -> tuple[str, str] | None:
173 """Asked when the standards packs are not settled, for a new project or one in which
174 no pack is detected."""
175 standards = self.results.project.standards
176 known = bool(standards and standards.packs and "auto" not in standards.packs)
177 if known or "standards" in answered or not (kind.new or not detected_packs):
178 return None
179 suggestion = ", ".join(detected_packs) or "none detected"
180 return (
181 "project:standards",
182 "Which coding standards packs apply (python, javascript, typescript, node, "
183 "react, angular, vue, java, kotlin, go, rust, swift, csharp, php, ruby)? "
184 f"Answer 'default' for the detected ones ({suggestion}).",
185 )
187 def store_answers(
188 self, execution: Execution, record: ClarificationRecord
189 ) -> dict[str, Any] | None:
190 """Keep the answers to ``P1`` questions as the project setup record."""
191 answers = [item for item in record.answers if item.rule_id == "P1"]
192 if not answers:
193 return None
194 current = dict(self.record(execution.project_id))
195 detected = detect_packs(
196 self.results.s.paths.workspace, self.results.engineering.technologies()
197 )
198 for item in answers:
199 part = item.target.removeprefix("project:")
200 if part == "testing":
201 current["testing"] = parse_testing(item.answer) or "conventional"
202 elif part == "architecture":
203 current["architecture"] = parse_architecture(item.answer)
204 elif part == "standards": 204 ↛ 206line 204 didn't jump to line 206 because the condition on line 204 was always true
205 current["standards"] = parse_standards(item.answer, detected)
206 current.setdefault("answers", {})[part] = item.answer
207 current.update(
208 {
209 "answeredBy": record.actor.actor_id,
210 "clarificationId": record.clarification_id,
211 "taskId": record.task_id,
212 "recordedAt": utc_now().isoformat(),
213 }
214 )
215 self.results.set_flag_json(f"{SETUP_FLAG}:{execution.project_id}", current)
216 self.results.s.events.append(
217 execution.execution_id,
218 "project.setup.recorded",
219 {
220 key: current[key]
221 for key in ("testing", "architecture", "standards")
222 if key in current
223 },
224 actor=record.actor,
225 )
226 return current
229__all__ = ["TARGETS", "ProjectSetup", "parse_architecture", "parse_standards", "parse_testing"]