diff --git a/src/stewardship/adoption.py b/src/stewardship/adoption.py index 3bda154..2d54dce 100644 --- a/src/stewardship/adoption.py +++ b/src/stewardship/adoption.py @@ -10,6 +10,7 @@ for this first cut beyond structural support in the AdoptionPlan schema. from __future__ import annotations import fnmatch +import os from pathlib import Path from typing import Any @@ -103,6 +104,14 @@ _SECRET_SHAPED_GLOBS = [ ".env", ".env.*", "*.env", "*password*", "*secret*", "*.pfx", "*.p12", ] _SECRET_EXCLUDE_SUFFIXES = (".pub", ".schema.json", ".example") +# Name patterns like "*password*" match documentation assets such as +# `icon-show-password.png`; no credential is ever carried in these formats. +_SECRET_BINARY_SUFFIXES = ( + ".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp", ".bmp", + ".pdf", ".epub", ".woff", ".woff2", ".ttf", ".eot", + ".zip", ".gz", ".tar", ".7z", ".xz", ".mp4", ".exe", ".msi", +) +_SECRET_SKIP_DIRS = {".git", ".venv", "node_modules", "__pycache__"} # Weighted because presence alone is weak evidence: `docs/` exists in almost # every repository and must not outvote a marker that actually identifies the @@ -152,22 +161,42 @@ def _finding(severity: str, code: str, path: str | None = None, message: str | N return item -def _detect_secret_shaped_paths(root: Path) -> list[tuple[str, bool]]: - """Return (relative_path, is_tracked) for files matching secret-shaped name patterns.""" +def _detect_secret_shaped_paths(root: Path) -> list[tuple[str, str]]: + """Return (relative_path, "tracked" | "untracked") for secret-shaped filenames. + + Prunes ignored directories while walking rather than filtering afterwards: + a repository may keep gigabytes of gitignored vendor material, and statting + all of it to reach a decision Git already made is pure cost. Paths the + repository deliberately ignores are dropped entirely; what matters is a + secret that is tracked, or one sitting unignored where it could be added. + """ tracked = gitutil.tracked_files(root) - found: list[tuple[str, bool]] = [] - for path in root.rglob("*"): - if not path.is_file(): - continue - if ".git" in path.parts or ".venv" in path.parts or "node_modules" in path.parts: - continue - name = path.name - if name.lower().endswith(_SECRET_EXCLUDE_SUFFIXES): - continue - if any(fnmatch.fnmatch(name.lower(), pattern) for pattern in _SECRET_SHAPED_GLOBS): - rel = path.relative_to(root).as_posix() - found.append((rel, rel in tracked)) - return found + matches: list[str] = [] + for dirpath, dirnames, filenames in os.walk(root): + rel_dir = Path(dirpath).relative_to(root).as_posix() + prefix = "" if rel_dir == "." else f"{rel_dir}/" + dirnames[:] = [name for name in dirnames if name not in _SECRET_SKIP_DIRS] + if dirnames: + candidates = [f"{prefix}{name}" for name in dirnames] + skipped = gitutil.ignored(root, candidates) + dirnames[:] = [ + name + for name, candidate in zip(dirnames, candidates, strict=True) + if candidate not in skipped + ] + for name in filenames: + lowered = name.lower() + if lowered.endswith(_SECRET_EXCLUDE_SUFFIXES) or lowered.endswith(_SECRET_BINARY_SUFFIXES): + continue + if any(fnmatch.fnmatch(lowered, pattern) for pattern in _SECRET_SHAPED_GLOBS): + matches.append(f"{prefix}{name}") + + ignored_matches = gitutil.ignored(root, matches) + return [ + (rel, "tracked" if rel in tracked else "untracked") + for rel in matches + if rel in tracked or rel not in ignored_matches + ] def _knowledge_format(path: Path) -> str: @@ -293,11 +322,18 @@ def assess(root: Path) -> dict[str, Any]: if (root / "kdb" / "kdb.yaml").exists() and (root / "kdb.yaml").exists(): findings.append(_finding("warning", "ADOPT_KDB_CANONICAL_PATH_OCCUPIED", "kdb/kdb.yaml")) - for rel, tracked in _detect_secret_shaped_paths(root): - if tracked: + for rel, status in _detect_secret_shaped_paths(root): + if status == "tracked": findings.append(_finding("error", "ADOPT_SECRET_TRACKED", rel, "Tracked path matches a secret-shaped filename pattern")) else: - findings.append(_finding("info", "ADOPT_SECRET_TRACKED", rel, "Untracked secret-shaped path (path only, content not read)")) + findings.append( + _finding( + "info", + "ADOPT_SECRET_SHAPED_UNTRACKED", + rel, + "Untracked and unignored secret-shaped path (path only, content not read)", + ) + ) if dirty: findings.append(_finding("warning", "ADOPT_WORKTREE_DIRTY")) diff --git a/src/stewardship/cli.py b/src/stewardship/cli.py index a2d813e..1b9bb9e 100644 --- a/src/stewardship/cli.py +++ b/src/stewardship/cli.py @@ -333,8 +333,9 @@ def build_parser() -> argparse.ArgumentParser: choices=sorted(REPOSITORY_TYPES), help="Confirm the repository type that assess could only propose", ) - p_adopt.add_argument("--strategy", default="overlay", choices=["overlay", "normalize"]) - p_adopt.add_argument("--recipe") + # `normalize` and its recipes are specified but not implemented; offering the + # choice advertised an operation that could only ever raise. + p_adopt.add_argument("--strategy", default="overlay", choices=["overlay"]) p_adopt.add_argument("--plan") p_adopt.add_argument("--approve") p_adopt.set_defaults(func=cmd_adopt) diff --git a/src/stewardship/context.py b/src/stewardship/context.py index f07e664..b672798 100644 --- a/src/stewardship/context.py +++ b/src/stewardship/context.py @@ -2,6 +2,7 @@ from __future__ import annotations +import difflib from dataclasses import dataclass, field from pathlib import Path from typing import Any @@ -67,6 +68,28 @@ def build_context( matched.sort(key=lambda t: (-(t.get("priority") or 50), t.get("id") or "")) topics_selected.extend(matched) + if not matched: + # An empty bundle is a legitimate answer, but silence cannot distinguish + # "no topic covers this yet" from a mistyped intent -- or from reaching + # for a topic id, which is not what intents are. + known: set[str] = set() + for topic in all_topics: + if isinstance(topic, dict): + known.update(topic.get("intents") or []) + if not known: + warnings.append( + "warning:CONTEXT_INTENT_UNKNOWN:The knowledge index declares no intents yet, " + f"so no context can be selected for {intent!r}" + ) + elif intent not in known: + hint = ", ".join(difflib.get_close_matches(intent, sorted(known), n=3)) or ", ".join( + sorted(known)[:3] + ) + warnings.append( + f"warning:CONTEXT_INTENT_UNKNOWN:No topic declares intent {intent!r}. " + f"Closest known intents: {hint}" + ) + if include_related: related_ids: set[str] = set() for topic in matched: