from failure import Failure from project_config.main import goals_file from loop_claude.project.text_file import read_utf8 from records.markdown_text import sections def read_premises(root: str) -> str | Failure: """## Goals and ## Audience in the file root's project config names for them; a Failure where no config names it, it cannot be read, or it lacks either, or holds one twice or empty.""" path = goals_file(root) if isinstance(path, Failure): return Failure(f"{path.reason} - no premises.") text = read_utf8(str(path.parent), path.name, "no premises") if isinstance(text, Failure): return text found = sections(text) for name in ("Goals", "Audience"): bodies = found.get(name, []) if not bodies: return Failure(f"{path.name} has no ## {name} heading - no premises.") if len(bodies) > 1: return Failure(f"{path.name} has ## {name} more than once - no premises.") if not bodies[0]: return Failure(f"{path.name} has no text under ## {name} - no premises.") return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"