import pathlib from loop_claude.outcome import Stop def read_premises(root): try: found = sections(pathlib.Path(root, "README.md").read_text(encoding="utf-8")) except OSError as failure: return Stop(1, f"README.md in {root}: {failure.strerror} - no premises.") except UnicodeDecodeError as failure: line = failure.object.count(b"\n", 0, failure.start) + 1 return Stop(1, f"README.md is not UTF-8 on line {line} - no premises.") for name in ("Goals", "Audience"): bodies = found.get(name, []) if not bodies: return Stop(1, f"README.md has no ## {name} heading - no premises.") if len(bodies) > 1: return Stop(1, f"README.md has ## {name} more than once - no premises.") if not bodies[0]: return Stop(1, f"README.md has nothing under ## {name} - no premises.") return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}" def sections(text): found, current, fenced = {}, None, False for line in text.splitlines(): fenced ^= line.startswith("```") if not fenced and line.startswith("## "): current = [] found.setdefault(line.strip()[3:], []).append(current) elif not fenced and line.startswith("# "): current = None elif current is not None and line.strip(): current.append(line) return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()}