Buckets:
| # pylint: disable=duplicate-code | |
| """ | |
| Prompt and user-story lint engine. | |
| Detects vague/ambiguous terms in <contract_rules>, <requirements>, | |
| <acceptance_tests>, and user-story acceptance criteria. An optional LLM pass | |
| can add advisory ambiguity findings without changing files. | |
| Public API | |
| ---------- | |
| scan_prompt(path, *, strict=False) -> LintResult | |
| scan_stories(stories_dir, *, strict=False) -> list[LintResult] | |
| run_llm_ambiguity_pass(path, strength, temperature, time, verbose, use_cloud) -> list[LintIssue] | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import logging | |
| import re | |
| from dataclasses import dataclass, field | |
| from pathlib import Path | |
| from typing import Optional | |
| logger = logging.getLogger(__name__) | |
| # --------------------------------------------------------------------------- | |
| # Vocabulary of known vague terms | |
| # --------------------------------------------------------------------------- | |
| # Core vague terms: genuinely ambiguous in behavioral contracts; always checked. | |
| VAGUE_TERMS: frozenset[str] = frozenset({ | |
| "valid", | |
| "invalid", | |
| "safe", | |
| "unsafe", | |
| "active", | |
| "inactive", | |
| "recent", | |
| "duplicate", | |
| "graceful", | |
| "gracefully", | |
| "reasonable", | |
| "authorized", | |
| "unauthorized", | |
| "trusted", | |
| "untrusted", | |
| "complete", | |
| "successful", | |
| }) | |
| # Extended vague terms: common English words that are only meaningfully ambiguous | |
| # in tightly scoped contract text. Only checked in --strict mode to avoid noise | |
| # in LLM instruction prompts and general requirements prose. | |
| VAGUE_TERMS_STRICT: frozenset[str] = frozenset({ | |
| "incomplete", | |
| "proper", | |
| "correct", | |
| "incorrect", | |
| "appropriate", | |
| "normal", | |
| "expected", | |
| "unexpected", | |
| "sufficient", | |
| "necessary", | |
| }) | |
| # Verbs that signal an observable, measurable outcome | |
| OBSERVABLE_VERBS: frozenset[str] = frozenset({ | |
| "return", "returns", "returned", | |
| "raise", "raises", "raised", | |
| "write", "writes", "wrote", | |
| "emit", "emits", "emitted", | |
| "log", "logs", "logged", | |
| "exit", "exits", "exited", | |
| "reject", "rejects", "rejected", | |
| "produce", "produces", "produced", | |
| "store", "stores", "stored", | |
| "increment", "increments", "incremented", | |
| "decrement", "decrements", "decremented", | |
| "set", "sets", | |
| "clear", "clears", "cleared", | |
| "yield", "yields", "yielded", | |
| "throw", "throws", "threw", | |
| "save", "saves", "saved", | |
| "delete", "deletes", "deleted", | |
| "output", "outputs", "outputted", | |
| "append", "appends", "appended", | |
| "insert", "inserts", "inserted", | |
| "update", "updates", "updated", | |
| "remove", "removes", "removed", | |
| "send", "sends", "sent", | |
| "publish", "publishes", "published", | |
| }) | |
| # Pre-compiled patterns: core terms (always) and extended terms (--strict only) | |
| _VAGUE_PATTERN = re.compile( | |
| r"\b(" + "|".join(re.escape(t) for t in sorted(VAGUE_TERMS)) + r")\b", | |
| re.IGNORECASE, | |
| ) | |
| _VAGUE_PATTERN_STRICT = re.compile( | |
| r"\b(" + "|".join(re.escape(t) for t in sorted(VAGUE_TERMS | VAGUE_TERMS_STRICT)) + r")\b", | |
| re.IGNORECASE, | |
| ) | |
| # Observable-outcome verb pattern | |
| _OBSERVABLE_PATTERN = re.compile( | |
| r"\b(" + "|".join(re.escape(v) for v in sorted(OBSERVABLE_VERBS)) + r")\b", | |
| re.IGNORECASE, | |
| ) | |
| # XML-style section extractor: captures content between <tag> … </tag> | |
| _XML_SECTION_RE = re.compile( | |
| r"<(?P<tag>[a-z_][a-z0-9_]*)>(?P<body>.*?)</(?P=tag)>", | |
| re.IGNORECASE | re.DOTALL, | |
| ) | |
| # Markdown heading (up to ###) | |
| _MD_HEADING_RE = re.compile(r"^(#{1,3})\s+(.+)$", re.MULTILINE) | |
| # --------------------------------------------------------------------------- | |
| # Data classes | |
| # --------------------------------------------------------------------------- | |
| class LintIssue: # pylint: disable=too-many-instance-attributes | |
| """A single lint finding.""" | |
| level: str # "warn" or "error" | |
| term: str # matched vague term | |
| section: str # section where found | |
| line: str # original line text | |
| message: str # human-readable description | |
| suggestion: str = "" # proposed <vocabulary> entry | |
| interpretations: list[str] = field(default_factory=list) # LLM-sourced readings | |
| code: str = "" # machine-readable issue code | |
| def as_dict(self) -> dict: | |
| """Serialise this issue to a JSON-safe dictionary.""" | |
| return { | |
| "level": self.level, | |
| "code": self.code, | |
| "term": self.term, | |
| "section": self.section, | |
| "line": self.line, | |
| "message": self.message, | |
| "suggestion": self.suggestion, | |
| "interpretations": self.interpretations, | |
| } | |
| class LintResult: | |
| """Aggregated lint findings for one file.""" | |
| path: Path | |
| issues: list[LintIssue] = field(default_factory=list) | |
| def warn_count(self) -> int: | |
| """Count of warning-level issues.""" | |
| return sum(1 for i in self.issues if i.level == "warn") | |
| def error_count(self) -> int: | |
| """Count of error-level issues.""" | |
| return sum(1 for i in self.issues if i.level == "error") | |
| def as_dict(self) -> dict: | |
| """Serialise this result to a JSON-safe dictionary.""" | |
| return { | |
| "path": str(self.path), | |
| "warn_count": self.warn_count, | |
| "error_count": self.error_count, | |
| "issues": [i.as_dict() for i in self.issues], | |
| } | |
| # --------------------------------------------------------------------------- | |
| # Section extraction helpers | |
| # --------------------------------------------------------------------------- | |
| def _extract_xml_sections(text: str) -> dict[str, str]: | |
| """Return tag-name → inner text for all XML-style sections found.""" | |
| return { | |
| match.group("tag").lower(): match.group("body").strip() | |
| for match in _XML_SECTION_RE.finditer(text) | |
| } | |
| def _extract_markdown_sections(text: str) -> dict[str, str]: | |
| """Return lowered-heading → body for all markdown ## / ### headings.""" | |
| headings = list(_MD_HEADING_RE.finditer(text)) | |
| sections: dict[str, str] = {} | |
| for idx, match in enumerate(headings): | |
| heading_text = match.group(2).strip().lower() | |
| start = match.end() | |
| end = headings[idx + 1].start() if idx + 1 < len(headings) else len(text) | |
| sections[heading_text] = text[start:end].strip() | |
| return sections | |
| def _extract_prose_lines(text: str) -> str: | |
| """Return text from lines that use the PDD % comment convention.""" | |
| return "\n".join( | |
| line.lstrip()[1:].strip() | |
| for line in text.splitlines() | |
| if line.lstrip().startswith("%") | |
| ) | |
| def _extract_sections(text: str) -> dict[str, str]: | |
| """ | |
| Extract all scannable sections from prompt or story text. | |
| Recognised section names (keys in returned dict): | |
| contract_rules, requirements, acceptance_tests, vocabulary (XML tags) | |
| acceptance criteria, glossary, definitions, covers (Markdown headings) | |
| prose (% lines) | |
| """ | |
| sections: dict[str, str] = {} | |
| sections.update(_extract_xml_sections(text)) | |
| sections.update(_extract_markdown_sections(text)) | |
| prose = _extract_prose_lines(text) | |
| if prose: | |
| sections["prose"] = prose | |
| return sections | |
| _SCANNABLE_SECTIONS: frozenset[str] = frozenset({ | |
| "contract_rules", | |
| "requirements", | |
| "acceptance_tests", | |
| "acceptance criteria", | |
| "prose", | |
| }) | |
| # Architecture metadata and structural sections whose content is NOT contract text | |
| # and must never be scanned for vague terms. | |
| _NON_SCANNABLE_SECTIONS: frozenset[str] = frozenset({ | |
| "pdd-reason", | |
| "pdd-interface", | |
| "pdd-dependency", | |
| "waivers", | |
| "deliverables", | |
| "dependencies", | |
| }) | |
| _VOCABULARY_SECTIONS: frozenset[str] = frozenset({ | |
| "vocabulary", | |
| "glossary", | |
| "definitions", | |
| }) | |
| # Sections where we additionally check for observable-outcome verbs | |
| _OBSERVABLE_CHECK_SECTIONS: frozenset[str] = frozenset({ | |
| "contract_rules", | |
| "acceptance_tests", | |
| "acceptance criteria", | |
| }) | |
| def _extract_vocabulary_terms(vocab_text: str) -> set[str]: | |
| """ | |
| Extract defined term names from a vocabulary/glossary/definitions block. | |
| Heuristic: a line whose leading token (after optional bullet) is followed | |
| by `:` or ` - ` is treated as a definition. Both the full phrase AND each | |
| individual word in the phrase are added as known terms, so that "valid | |
| response" in vocabulary suppresses the warning for the word "valid". | |
| """ | |
| terms: set[str] = set() | |
| for line in vocab_text.splitlines(): | |
| stripped = line.strip() | |
| # Standard: "term: ..." or "- term: ..." or "* term: ..." or "term - ..." | |
| term_match = re.match( | |
| r"^[-*]?\s*([A-Za-z][A-Za-z0-9 _-]*?)\s*(?::\s|\s+-\s)", | |
| stripped, | |
| ) | |
| if term_match: | |
| phrase = term_match.group(1).strip().lower() | |
| terms.add(phrase) | |
| # Also add each individual word so "valid response" covers "valid" | |
| for word in re.split(r"[\s_-]+", phrase): | |
| if word: | |
| terms.add(word) | |
| return terms | |
| # --------------------------------------------------------------------------- | |
| # Core scan logic | |
| # --------------------------------------------------------------------------- | |
| def _scan_section( | |
| section_name: str, | |
| section_text: str, | |
| vocab_terms: set[str], | |
| *, | |
| strict: bool, | |
| check_observable: bool, | |
| ) -> list[LintIssue]: | |
| """Scan one section for vague terms and (optionally) observable-outcome gaps.""" | |
| issues: list[LintIssue] = [] | |
| pattern = _VAGUE_PATTERN_STRICT if strict else _VAGUE_PATTERN | |
| for line in section_text.splitlines(): | |
| stripped = line.strip() | |
| if not stripped: | |
| continue | |
| # Strip angle-bracket placeholder tokens (e.g. <expected outcome>, <role>) | |
| # before scanning so template scaffolds don't produce false positives. | |
| scan_text = re.sub(r"<[^>]+>", "", stripped) | |
| matched_terms = {m.lower() for m in pattern.findall(scan_text)} | |
| undefined_terms = matched_terms - vocab_terms | |
| for term in sorted(undefined_terms): | |
| level = "error" if strict else "warn" | |
| issues.append(LintIssue( | |
| level=level, | |
| code="VAGUE_TERM_UNDEFINED", | |
| term=term, | |
| section=section_name, | |
| line=stripped, | |
| message=( | |
| f'Vague term "{term}" used in [{section_name}] without a ' | |
| f"<vocabulary> definition." | |
| ), | |
| suggestion=f"{term}: <add a precise, observable definition here>", | |
| )) | |
| # Observable-outcome check: only fires when at least one vague term on | |
| # this line is UNDEFINED (not already covered by the vocabulary). | |
| if check_observable and undefined_terms and not _OBSERVABLE_PATTERN.search(scan_text): | |
| level = "error" if strict else "warn" | |
| issues.append(LintIssue( | |
| level=level, | |
| code="VAGUE_NO_OBSERVABLE_OUTCOME", | |
| term="(no observable outcome)", | |
| section=section_name, | |
| line=stripped, | |
| message=( | |
| "Rule or criterion contains a vague term but no observable " | |
| "outcome verb (returns/raises/writes/emits/rejects/…)." | |
| ), | |
| )) | |
| return issues | |
| _LLM_TEMPLATE_SECTIONS = frozenset({ | |
| "contract_rules", "requirements", "acceptance_tests", "acceptance criteria", | |
| }) | |
| def _check_llm_template( | |
| path: Path, | |
| sections: dict[str, str], | |
| *, | |
| strict: bool, | |
| llm_template: Optional[bool], | |
| ) -> list[LintIssue]: | |
| """Warn when an LLM template has no scannable contract sections.""" | |
| auto = path.name.lower().endswith("_llm.prompt") | |
| if llm_template is False: | |
| return [] | |
| if llm_template is not True and not auto: | |
| return [] | |
| has_section = any( | |
| sections.get(k, "").strip() for k in _LLM_TEMPLATE_SECTIONS | |
| ) | |
| if has_section: | |
| return [] | |
| level = "error" if strict else "warn" | |
| return [LintIssue( | |
| level=level, | |
| code="LLM_TEMPLATE_NO_CONTRACT_SECTIONS", | |
| term="", | |
| section="file", | |
| line="", | |
| message=( | |
| "LLM template has no scannable contract sections " | |
| "(<contract_rules>, <requirements>, or <acceptance_tests>)." | |
| ), | |
| suggestion=( | |
| "Add <requirements>, <contract_rules>, <acceptance_tests>, " | |
| "and <vocabulary> blocks for lint and formal verification." | |
| ), | |
| )] | |
| def scan_prompt( # pylint: disable=too-many-locals | |
| path: Path, | |
| *, | |
| strict: bool = False, | |
| llm_template: Optional[bool] = None, | |
| ) -> LintResult: | |
| """ | |
| Deterministic lint scan of a single prompt file. | |
| Prompts with no recognised sections produce zero issues (legacy safety | |
| guarantee). Pass strict=True to escalate all warnings to errors. | |
| """ | |
| result = LintResult(path=path) | |
| try: | |
| text = path.read_text(encoding="utf-8", errors="replace") | |
| except OSError as exc: | |
| result.issues.append(LintIssue( | |
| level="error", | |
| code="FILE_READ_ERROR", | |
| term="", | |
| section="file", | |
| line="", | |
| message=f"Cannot read file: {exc}", | |
| )) | |
| return result | |
| sections = _extract_sections(text) | |
| result.issues.extend( | |
| _check_llm_template(path, sections, strict=strict, llm_template=llm_template) | |
| ) | |
| # Collect vocabulary terms from all vocabulary-source sections | |
| vocab_terms: set[str] = set() | |
| for vocab_key in _VOCABULARY_SECTIONS: | |
| if vocab_key in sections: | |
| vocab_terms |= _extract_vocabulary_terms(sections[vocab_key]) | |
| # Prose (% lines) is only meaningful contract text when the file also has at | |
| # least one structured contract section. Without this gate, LLM instruction | |
| # prompts that use % for their body text produce hundreds of false positives. | |
| contract_sections = frozenset({ | |
| "contract_rules", "requirements", "acceptance_tests", "acceptance criteria" | |
| }) | |
| has_contract_section = any(k in sections for k in contract_sections) | |
| # Scan recognised content sections only (skip architecture metadata) | |
| found_any_section = False | |
| for section_name, section_text in sections.items(): | |
| if section_name in _NON_SCANNABLE_SECTIONS: | |
| continue | |
| if section_name not in _SCANNABLE_SECTIONS: | |
| continue | |
| if section_name == "prose" and not has_contract_section: | |
| continue | |
| found_any_section = True | |
| result.issues.extend( | |
| _scan_section( | |
| section_name, | |
| section_text, | |
| vocab_terms, | |
| strict=strict, | |
| check_observable=(section_name in _OBSERVABLE_CHECK_SECTIONS), | |
| ) | |
| ) | |
| if not found_any_section: | |
| logger.debug("prompt_lint: no recognised sections in %s — skipping", path) | |
| return result | |
| def scan_stories(stories_dir: Path, *, strict: bool = False) -> list[LintResult]: | |
| """ | |
| Scan all story__*.md files under stories_dir for vague acceptance criteria. | |
| Each story's ## Glossary / ## Definitions section supplies the vocabulary. | |
| ``## Covers`` is coverage metadata rather than a term-definition source. | |
| """ | |
| if not stories_dir.is_dir(): | |
| return [] | |
| return [ | |
| scan_prompt(story_path, strict=strict) | |
| for story_path in sorted(stories_dir.rglob("story__*.md")) | |
| ] | |
| # --------------------------------------------------------------------------- | |
| # Optional LLM ambiguity pass | |
| # --------------------------------------------------------------------------- | |
| def run_llm_ambiguity_pass( # pylint: disable=too-many-locals,too-many-arguments | |
| path: Path, | |
| *, | |
| strength: float = 0.5, | |
| temperature: float = 0.0, | |
| time: Optional[float] = None, | |
| verbose: bool = False, | |
| use_cloud: Optional[bool] = None, | |
| ) -> list[LintIssue]: | |
| """ | |
| Run the LLM-backed ambiguity analysis for a single prompt file. | |
| Loads pdd/prompts/prompt_lint_LLM.prompt, calls llm_invoke, and parses the | |
| JSON response into LintIssue entries (level="warn"). ``use_cloud=True`` | |
| selects PDD Cloud; ``False`` uses configured local providers. | |
| Safe to call: always returns [] on any error so CI never breaks on LLM failure. | |
| """ | |
| try: | |
| from .llm_invoke import llm_invoke # pylint: disable=import-outside-toplevel | |
| template_path = Path(__file__).parent / "prompts" / "prompt_lint_LLM.prompt" | |
| if not template_path.exists(): | |
| logger.warning("prompt_lint_LLM.prompt not found; skipping LLM pass") | |
| return [] | |
| prompt_content = path.read_text(encoding="utf-8", errors="replace") | |
| vague_terms_list = ", ".join(sorted(VAGUE_TERMS)) | |
| template = template_path.read_text(encoding="utf-8") | |
| filled = template.replace("{prompt_content}", prompt_content).replace( | |
| "{vague_terms_list}", vague_terms_list | |
| ) | |
| result = llm_invoke( | |
| messages=[{"role": "user", "content": filled}], | |
| strength=strength, | |
| temperature=temperature, | |
| time=time, | |
| verbose=verbose, | |
| use_cloud=use_cloud, | |
| ) | |
| response_text = result["result"] | |
| # Strip optional markdown code fences | |
| json_match = re.search(r"```(?:json)?\s*(\[.*?\])\s*```", response_text, re.DOTALL) | |
| raw_json = json_match.group(1) if json_match else response_text.strip() | |
| items = json.loads(raw_json) | |
| issues: list[LintIssue] = [] | |
| for item in items: | |
| if not isinstance(item, dict): | |
| continue | |
| issues.append(LintIssue( | |
| level="warn", | |
| code="LLM_AMBIGUITY", | |
| term=str(item.get("term", "")), | |
| section=str(item.get("section", "llm")), | |
| line="", | |
| message=f'LLM flagged "{item.get("term", "")}" as potentially ambiguous.', | |
| suggestion=str(item.get("suggestion", "")), | |
| interpretations=[str(x) for x in item.get("interpretations", [])], | |
| )) | |
| return issues | |
| except Exception as exc: # noqa: BLE001 # pylint: disable=broad-exception-caught | |
| logger.warning("prompt_lint LLM pass failed: %s", exc) | |
| return [] | |
Xet Storage Details
- Size:
- 18.8 kB
- Xet hash:
- 498096f958ab5c546b4263b70f78a047acc51ec221e4cd0d7ce200d5056151f3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.