97 """Return ``{phase_code: verbatim_section_body}`` for every ``# PHASE`` section.
99 The body is everything after the header line up to the next ``# PHASE`` /
100 top-level ``## `` heading, with the inter-section ``---`` rule stripped.
102 text = roadmap_text.replace(
"\r\n",
"\n")
103 out: dict[str, str] = {}
104 for match
in _PHASE_HEADER_RE.finditer(text):
105 code = match.group(1)
106 nxt = _BOUNDARY_RE.search(text, match.end())
107 end = nxt.start()
if nxt
else len(text)
108 body = text[match.end():end].strip(
"\n")
109 body = _TRAILING_RULE_RE.sub(
"", body).strip(
"\n")
115 """Map ``sub_id -> Item-column text`` from a phase section's deliverables table."""
116 items: dict[str, str] = {}
117 for line
in section_body.split(
"\n"):
118 stripped = line.strip()
119 if not stripped.startswith(
"|")
or set(stripped) <= set(
"|-: "):
121 match = _DELIV_ROW_RE.match(stripped)
124 item_id = match.group(
"id").strip()
125 item_text =
" ".join(match.group(
"item").split()).strip()
126 if item_id.lower()
in (
"#",
"item")
or not item_text:
128 items[item_id] = item_text
141def backfill(roadmap_md: Path, source_dir: Path) -> BackfillReport:
142 """Backfill every ``phases/<code>.md`` body from the legacy ``# PHASE`` sections."""
146 for scanned
in scan_source_tree(Path(source_dir)):
147 if scanned.kind !=
"phase":
150 body = sections.get(code)
152 report.skipped.append(code)
154 model, _old_body = load_record(scanned)
157 for sub
in model.sub_phases:
158 if sub.description == sub.id
and sub.id
in items:
159 new_desc =
_capped(items[sub.id])
160 new_subs.append(sub.model_copy(update={
"description": new_desc}))
161 report.stubs_fixed.append((code, sub.id, new_desc))
164 model = model.model_copy(update={
"sub_phases": new_subs})
165 scanned.path.write_text(
166 format_record_text(model, body=body), encoding=
"utf-8", newline=
""
168 report.phases_written += 1
169 report.bodies[code] = body
170 _dbg(
"OC_CHAT.MAIN", func, f
"backfilled {code} ({len(body)} chars)", 3)
171 _dbg(
"OC_CHAT.MAIN", func, f
"wrote {report.phases_written} phase bodies", 2)