import pathlib 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, home: pathlib.Path) -> str | Failure: """The goals file's path, then its ## Goals and ## Audience; the goals file is the one that the config under home of root's project names. A Failure where no config names root's checkout, the file cannot be read, or the file lacks a section, holds one twice or leaves one empty.""" path = goals_file(root, home) 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} has no ## {name} heading - no premises.") if len(bodies) > 1: return Failure(f"{path} has ## {name} more than once - no premises.") if not bodies[0]: return Failure(f"{path} has no text under ## {name} - no premises.") return f"{path}:\n## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"