diff --git a/agents/conductors/intake/_intake.py b/agents/conductors/intake/_intake.py index 4099f946..b2c83601 100755 --- a/agents/conductors/intake/_intake.py +++ b/agents/conductors/intake/_intake.py @@ -40,7 +40,8 @@ from _sizing import ( # noqa: E402 WORK_TYPES, HUMAN_REVIEW, MANUAL_ONLY_WORK_TYPES, LIBRARY_REPOS, WORKSPACE_REPOS, ORGANISM_REPOS, KNOWN_REPOS, - RISK_KEYWORDS, AMBIGUITY_KEYWORDS, normalise_repo, declared_header, + RISK_KEYWORDS, AMBIGUITY_KEYWORDS, normalise_repo, norm_work_type, + declared_header, declared_inline, effective_difficulty, strip_declarations, _hits, effective_consequence, effective_unattended, effective_review_minutes, policy as _sizing_policy, BODY_MAP_PATH, @@ -221,6 +222,136 @@ def _repos_in(text: str) -> list: return sorted(m for m in found if m in KNOWN_REPOS) +# --- the declared header block (PyAutoBrain#472) ------------------------------ +# Raw input is often a pasted prompt that already carries a header block. The +# sizing faculty's `declared_header` reads the closed-vocabulary keys and +# returns None for a value it does not recognise — correct for the faculty, but +# it left intake silently inferring over a declared `Type: hygiene`. Intake +# therefore reads the RAW declared values itself and says what it did with them. +# +# Unknown declared types are not a hard error: an `intake ideas` batch must not +# abort on one bullet. Common near-misses map through this table with a visible +# note; anything else files to triage/ with a note naming the valid set. +_TYPE_ALIASES = { + "hygiene": "maintenance", + "upkeep": "maintenance", + "tests": "test", + "documentation": "docs", +} + +# Keys that may sit in a prompt's leading header block. `Repos:`/`Themes:` are +# the list keys (a bare `Key:` then `- ` bullets). +_HEADER_BLOCK_KEYS = ( + "type", "target", "difficulty", "autonomy", "priority", "status", "issued", + "filed", "epic", "phase", "bundle", "blocked-by", "closes-when", + "consequence", "witness", "review-minutes", "unattended", "lane", "memory", + "repos", "themes", +) +_LIST_KEYS = ("repos", "themes") +# A block counts as a header only if it declares at least one of these — a lone +# free-text key ("Status: the dashboard is stale …") is a sentence, not a header. +_ANCHOR_KEYS = ("type", "target", "repos", "difficulty", "autonomy", "priority") +_BLOCK_KEY_RE = re.compile( + r"^(" + "|".join(re.escape(k) for k in _HEADER_BLOCK_KEYS) + r")\s*:\s*(.*)$", + re.I) + + +def _split_header(text: str): + """(heading, header_lines, body) for a prompt-shaped raw input. + + Strips a leading `# heading` plus the contiguous header block that follows + it, but only when that block is made of KNOWN header keys (with `Repos:` / + `Themes:` bullets and blank lines), declares at least one anchor key, and is + closed by a blank line or the end of the text. Anything else — a first line + that merely opens with "Status:" in a running paragraph, say — is prose and + is returned untouched as the body, with an empty heading and no header. + """ + lines = text.splitlines() + i, n = 0, len(lines) + while i < n and not lines[i].strip(): + i += 1 + heading = "" + if i < n and re.match(r"^#\s+\S", lines[i].strip()): + heading = lines[i].strip().lstrip("#").strip() + i += 1 + block, keys, in_list, j = [], set(), False, i + while j < n: + s = lines[j].strip() + if not s: + in_list = False + if keys and j + 1 < n and lines[j + 1].strip() and \ + not _BLOCK_KEY_RE.match(lines[j + 1].strip()): + break # blank line closing the block, prose follows + j += 1 + continue + if in_list and _LIST_BULLET.match(lines[j]): + block.append(lines[j]) + j += 1 + continue + m = _BLOCK_KEY_RE.match(s) + if not m: + break + key = m.group(1).lower() + if key in _LIST_KEYS and not m.group(2).strip(): + in_list = True + elif key in _LIST_KEYS or m.group(2).strip(): + in_list = False + else: + break # a bare non-list key is not a header line + keys.add(key) + block.append(lines[j]) + j += 1 + closed = j >= n or not lines[j].strip() + body = "\n".join(lines[j:]).strip() + if not (keys & set(_ANCHOR_KEYS)) or not closed or not re.search(r"\w", body): + # No header block. A heading alone is still the title source, but the + # body is left verbatim so nothing the author wrote is dropped. + return heading, [], text + return heading, block, body + + +def _declared_type(raw: str | None): + """(work_type or None, note or None) for a raw declared `Type:` value.""" + if not raw: + return None, None + value = norm_work_type(raw.split(" #", 1)[0]) + if value in WORK_TYPES: + return value, None + if value in _TYPE_ALIASES: + mapped = _TYPE_ALIASES[value] + return mapped, (f"Type {raw.strip()} declared → unknown, using {mapped}.") + return None, (f"Type {raw.strip()} declared is not a work type (valid: " + f"{', '.join(sorted(WORK_TYPES))}) — filed to triage/ for a " + "human to re-home.") + + +def _declared_target(raw: str | None): + """(target key or None, note or None) for a raw declared `Target:` value.""" + if not raw: + return None, None + value = raw.split(" #", 1)[0].strip().strip("`*_") + t = normalise_repo(value) + if t in KNOWN_REPOS or t == "workspaces": + return t, None + return None, (f"Target {value} declared is not a known repo — inferred " + "the target from the prose instead.") + + +def _declared_repos(raw: list): + """(repos in declared order, notes) for a raw declared `Repos:` list.""" + repos, notes = [], [] + for item in raw: + value = item.split(" #", 1)[0].strip().strip("`*_").strip() + r = normalise_repo(value) + if r in KNOWN_REPOS: + if r not in repos: + repos.append(r) + elif value: + notes.append(f"Repos entry {value} declared is not a known repo — " + "dropped.") + return repos, notes + + def classify_work_type(text: str): """Return (work_type, confidence, per_type_hits). @@ -345,7 +476,16 @@ def analyse(text: str, source: str, themes=None): already carries is kept; absent that too, the prompt is simply un-themed — formalisation never waits on a theme. """ - repos = _repos_in(text) + # A pasted prompt's own header block (PyAutoBrain#472): read its raw + # Type/Target/Repos so a value the faculty does not recognise is reported, + # not silently replaced by inference, and keep it out of the title. + heading, header_lines, body = _split_header(text) + raw_header = parse_header(text) + notes = [] + declared_repos, repo_notes = _declared_repos(parse_list_header(text, "Repos")) + notes += repo_notes + # Declared repos are taken as written, in order; prose mentions add nothing. + repos = declared_repos or _repos_in(text) # What the input DECLARES outranks what its prose merely suggests — the same # rule the feature and bug conductors apply (the faculty owns it). Raw # conception input may carry a full header block (a pasted prompt) or state @@ -360,9 +500,26 @@ def analyse(text: str, source: str, themes=None): }.items() if v} work_type, confidence, type_hits = classify_work_type(text) + if not declared.get("type") and raw_header.get("type"): + mapped, type_note = _declared_type(raw_header["type"]) + if type_note: + notes.append(type_note) + if mapped: + declared["type"] = mapped + else: + # Unknown and unmapped: the inferred type rides along as the + # provisional guess, but the filing goes to triage/. + confidence = "low" if declared.get("type"): work_type, confidence = declared["type"], "high" - target, target_display, repos = infer_target(text, repos) + declared_target, target_note = _declared_target(raw_header.get("target")) + if target_note: + notes.append(target_note) + if declared_target: + target, target_display = (declared_target, + REPO_DISPLAY.get(declared_target, declared_target)) + else: + target, target_display, repos = infer_target(text, repos) # Build a prompt-shaped dict the shared sizing faculty understands. p = {"text": text, "repos": repos, "words": len(text.split()), @@ -394,7 +551,13 @@ def analyse(text: str, source: str, themes=None): themes = [k for k in dict.fromkeys(_theme_key(t) for t in (themes or [])) if k] or parse_theme_list(text) - title = _title(strip_declarations(text, decl_spans)) + if heading or header_lines: + # Title from the heading, else the first line after the header block — + # never a header line itself. + title_src = heading or body + title = _title(strip_declarations(title_src, declared_inline(title_src)[1])) + else: + title = _title(strip_declarations(text, decl_spans)) slug = _slug(title) folder = work_type if confidence != "low" else "triage" if folder == HUMAN_REVIEW: @@ -430,7 +593,9 @@ def analyse(text: str, source: str, themes=None): "type_signals": type_hits, "target": target, "target_display": target_display, + "target_source": "declared" if declared_target else "inferred", "repos_affected": repos, + "repos_source": "declared" if declared_repos else "inferred", "themes": themes, "difficulty": level, "difficulty_score": score, @@ -458,8 +623,9 @@ def analyse(text: str, source: str, themes=None): "workflow": workflow, "proposed_path": proposed, "header": header, - "risks": _risks(level, factors, confidence, target, declared, estimated, - witness=p["witness"], consequence=consequence), + "risks": notes + _risks(level, factors, confidence, target, declared, + estimated, witness=p["witness"], + consequence=consequence), "next_action": _next_action(proposed, confidence, folder), } @@ -621,6 +787,11 @@ def write_prompt(mind: Path, decision: dict, body_text: str, source_note: str): cites = memory_citations(mind, decision.get("title", ""), body_text) decision["memory"] = cites decision["header"] = _with_memory(decision["header"], cites) + # The input's own header block (and its heading) is already rendered in + # the decision's header — do not echo it into the body (PyAutoBrain#472). + _heading, header_lines, body = _split_header(body_text) + if header_lines: + body_text = body dest.write_text(decision["header"] + "\n\n" + body_text.strip() + note, encoding="utf-8") return str(rel) @@ -4156,8 +4327,10 @@ def emit_human(d: dict): wt_mark = ("declared" if d.get("work_type_source") == "declared" else f"confidence: {d['classification_confidence']}") print(f"Work-type: {d['work_type']} ({wt_mark})") - print(f"Target: {d['target_display']}") - print(f"Repos resolved: {', '.join(d['repos_affected']) or '(none)'}") + t_mark = " (declared)" if d.get("target_source") == "declared" else "" + r_mark = " (declared)" if d.get("repos_source") == "declared" else "" + print(f"Target: {d['target_display']}{t_mark}") + print(f"Repos resolved: {', '.join(d['repos_affected']) or '(none)'}{r_mark}") if d.get("difficulty_source") == "declared": print(f"Difficulty: {d['difficulty']} (declared; heuristic derived " f"{d['difficulty_derived']}, score {d['difficulty_score']})") diff --git a/tests/test_intake_declared_fields.py b/tests/test_intake_declared_fields.py new file mode 100644 index 00000000..482f1473 --- /dev/null +++ b/tests/test_intake_declared_fields.py @@ -0,0 +1,157 @@ +"""tests/test_intake_declared_fields.py — intake honours declared Type, Target, Repos. + +PyAutoBrain#472. The Intake (Conception) Agent honoured a declared Difficulty, +Autonomy and Priority but mishandled the rest of a pasted header block: + +1. an unknown `Type:` (``hygiene``) was dropped silently and the type inferred; +2. a declared `Target:` was overridden by the first library named in prose; +3. a declared `Repos:` list was re-sorted and padded with prose mentions, + including the phantom ``workspaces`` bucket; +4. a header block opening the input became the title/slug, and was echoed into + the written body below the real header. + +Unknown Types are deliberately NOT a hard error — an `intake ideas` batch must +not abort on one bad bullet — so they surface as a visible risk note, mapped +through a small alias table or filed to triage/. +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "agents" / "conductors" / "intake")) +import _intake # noqa: E402 +from _intake import analyse, write_prompt # noqa: E402 + +# Library display names are read from the body map at runtime rather than +# written as literals: real satellite repo names in organ code trip the tenant +# firewall (PyAutoMind/scripts/repos_sync.py). Reading them through +# REPO_DISPLAY keeps every assertion below validating against the REAL known-repo +# registry — a declared Target/Repos is still accepted only if the body map +# knows it. +FIT = _intake.REPO_DISPLAY["autofit"] +GALAXY = _intake.REPO_DISPLAY["autogalaxy"] +ARRAY = _intake.REPO_DISPLAY["autoarray"] + +BODY = "The autoarray grid passed into the light profile is wrong for ellipse fits." + + +def test_display_names_resolve_from_the_body_map(): + # Guard the indirection: each name is a real, known library repo whose + # display spelling differs from its key (so the header assertions below + # cannot pass vacuously on a lowercase fallback). + for key, name in (("autofit", FIT), ("autogalaxy", GALAXY), ("autoarray", ARRAY)): + assert key in _intake.LIBRARY_REPOS + assert _intake.normalise_repo(name) == key + assert name != key + + +def _notes(d: dict) -> str: + return "\n".join(d["risks"]) + + +# --- 1. Type ------------------------------------------------------------------ + +def test_unknown_declared_type_is_flagged_not_silently_dropped(): + d = analyse("Type: vibes\n\nRemove the stale comments from the config yaml " + "files and update the docs readme to match.", "test") + # Not silently replaced by an inferred folder: it goes to triage/ ... + assert d["proposed_path"].startswith("draft/triage/") + # ... and the note names what was declared and the valid set. + notes = _notes(d) + assert "vibes" in notes + for valid in ("feature", "bug", "maintenance", "docs"): + assert valid in notes + + +def test_hygiene_maps_to_maintenance_with_a_visible_note(): + d = analyse("Type: hygiene\n\nRemove the stale comments from the config yaml " + f"files and update the docs readme in @{FIT} to match.", "test") + assert d["work_type"] == "maintenance" + assert d["work_type_source"] == "declared" + assert d["proposed_path"].startswith("draft/maintenance/autofit/") + assert "Type: maintenance" in d["header"] + assert any("hygiene" in r and "maintenance" in r for r in d["risks"]) + + +# --- 2. Target ---------------------------------------------------------------- + +@pytest.mark.parametrize("declared, folder, display", [ + ("autogalaxy", "autogalaxy", GALAXY), + ("autofit", "autofit", FIT), + ("workspaces", "workspaces", "workspaces"), +]) +def test_declared_target_is_honoured_and_homes_the_file(declared, folder, display): + # The prose names the autoarray library first — the scraper's pick. + text = f"Type: bug\nTarget: {declared}\n\n{BODY} See @{ARRAY} and @{FIT}." + d = analyse(text, "test") + assert d["target"] == folder + assert d["target_source"] == "declared" + assert d["proposed_path"].startswith(f"draft/bug/{folder}/") + assert f"Target: {display}" in d["header"] + + +def test_unknown_declared_target_falls_back_with_a_note(): + d = analyse(f"Type: bug\nTarget: NotARepo\n\n{BODY}", "test") + assert d["target"] == "autoarray" + assert d["target_source"] == "inferred" + assert "NotARepo" in _notes(d) + + +# --- 3. Repos ----------------------------------------------------------------- + +def test_declared_repos_taken_as_written_in_order(): + text = (f"Type: maintenance\nTarget: autofit\nRepos:\n- {FIT}\n- PyAutoNerves\n\n" + "The priors config drifts between autofit and autoarray; the " + "workspaces also carry stale copies.") + d = analyse(text, "test") + # Order kept, no prose superset (autoarray), no phantom `workspaces`. + assert d["repos_affected"] == ["autofit", "autonerves"] + assert d["repos_source"] == "declared" + assert f"Repos:\n- {FIT}\n- PyAutoNerves\n" in d["header"] + + +def test_declared_repos_reject_unknown_names_with_a_note(): + text = (f"Type: bug\nTarget: autofit\nRepos:\n- {FIT}\n- NotARepo\n\n" + f"Something in @{ARRAY} is broken.") + d = analyse(text, "test") + assert d["repos_affected"] == ["autofit"] + assert "NotARepo" in _notes(d) + + +# --- 4. Header block vs title / body ----------------------------------------- + +def test_leading_header_block_is_not_the_title_or_slug(): + d = analyse("Target: autogalaxy\nType: bug\nDifficulty: small\n\n" + "Ellipse fitting crashes when the mask is empty in autogalaxy.", + "test") + assert d["title"] == "Ellipse fitting crashes when the mask is empty in autogalaxy" + assert "target" not in d["proposed_path"].split("/")[-1] + assert d["proposed_path"].startswith("draft/bug/autogalaxy/ellipse_fitting") + + +def test_prose_first_line_with_a_header_key_is_not_eaten(): + # "Status:" is a header key, but this is a sentence opening a paragraph. + text = ("Status: the dashboard renders stale rows after a merge in " + "@PyAutoMind.\nIt should re-render on every push.") + head, header_lines, body = _intake._split_header(text) + assert head == "" and header_lines == [] + assert body == text + assert analyse(text, "test")["title"].startswith("Status: the dashboard") + + +def test_header_block_is_not_echoed_into_the_body(tmp_path, monkeypatch): + monkeypatch.setattr(_intake, "memory_citations", lambda *a, **k: []) + text = ("# Ellipse crash\n\nType: bug\nTarget: autogalaxy\nDifficulty: small\n\n" + "Ellipse fitting crashes when the mask is empty.") + d = analyse(text, "test") + rel = write_prompt(tmp_path, d, text, "test") + written = (tmp_path / rel).read_text(encoding="utf-8") + assert written.count("Type:") == 1 + assert written.count("Target:") == 1 + assert written.count("# Ellipse crash") == 1 + assert "Ellipse fitting crashes when the mask is empty." in written