fix(slice4): make all entry write paths sidecar-aware (§22.4a carried from SLICE-1)

graduate, claim, retire/unretire, _read_meta_entry, mark_entry_reviewed,
body extract/wrap (api_branches + api_prs replay) now dual-read and write
metadata to the sidecar via write_entry_files + bot.commit_entry_files/
open_entry_pr — a migrated body-only .md no longer crashes entry.parse or
re-grows frontmatter; legacy entries lazy-migrate on first metadata write.
Existing tests updated to assert the sidecar (INV-2 clean docs).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ben Stull
2026-06-07 18:57:54 -07:00
parent 734290f344
commit aee9b582e5
8 changed files with 325 additions and 181 deletions
+43 -49
View File
@@ -42,7 +42,7 @@ from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from . import auth, cache, db, entry as entry_mod, projects as projects_mod
from . import auth, cache, db, entry as entry_mod, metadata as metadata_mod, projects as projects_mod
from .bot import Actor, Bot
from .config import Config
from .gitea import Gitea, GiteaError
@@ -341,19 +341,16 @@ def make_router(
if _rfc_id_taken(rfc_id, excluding_slug=slug):
raise HTTPException(409, f"Integer ID {rfc_id} is already taken")
# Read the meta-repo entry once — we need the file's sha for the
# graduation PR's update_file call and the body to carry through
# Dual-read the meta-repo entry once (§22.4a sidecar-aware) — we need its
# git state for the graduation commit and the body to carry through
# unchanged (meta-only keeps the body in the entry, §13.3).
fetched = await gitea.read_file(
config.gitea_org, (projects_mod.default_content_repo(config) or ""), f"rfcs/{slug}.md", ref="main",
st = await metadata_mod.read_entry_from_git(
gitea, config.gitea_org,
(projects_mod.default_content_repo(config) or ""), f"rfcs/{slug}.md",
)
if fetched is None:
if st is None:
raise HTTPException(409, f"Meta entry rfcs/{slug}.md not found on main")
meta_text, meta_sha = fetched
try:
super_draft_entry = entry_mod.parse(meta_text)
except Exception as e:
raise HTTPException(500, f"Meta entry malformed: {e}")
super_draft_entry = st.entry
arbiters = json.loads(rfc["arbiters_json"] or "[]") or owners[:1]
@@ -380,7 +377,8 @@ def make_router(
# through graduation rather than dropping them on the rebuild.
extra=dict(super_draft_entry.extra),
)
graduated_contents = entry_mod.serialize(graduated_entry)
graduation_files = metadata_mod.write_entry_files(
f"rfcs/{slug}.md", graduated_entry, st)
state = _new_active(
slug, rfc_id=rfc_id, owners=owners, arbiters=arbiters,
@@ -400,8 +398,7 @@ def make_router(
coro = _orchestrate(
config=config, gitea=gitea, bot=bot,
actor=viewer.as_actor(), state=state,
graduated_contents=graduated_contents,
meta_file_sha=meta_sha,
graduation_files=graduation_files,
)
if request.query_params.get("_sync") == "1":
await coro
@@ -481,26 +478,23 @@ def make_router(
if already:
raise HTTPException(409, f"A claim PR is already open: #{already['pr_number']}")
fetched = await gitea.read_file(
config.gitea_org, (projects_mod.default_content_repo(config) or ""), f"rfcs/{slug}.md", ref="main",
st = await metadata_mod.read_entry_from_git(
gitea, config.gitea_org,
(projects_mod.default_content_repo(config) or ""), f"rfcs/{slug}.md",
)
if fetched is None:
if st is None:
raise HTTPException(409, f"Meta entry rfcs/{slug}.md not found on main")
meta_text, meta_sha = fetched
try:
ent = entry_mod.parse(meta_text)
except Exception as e:
raise HTTPException(500, f"Meta entry malformed: {e}")
if viewer.gitea_login in ent.owners:
if viewer.gitea_login in st.entry.owners:
return {"ok": True, "noop": True}
ent.owners = ent.owners + [viewer.gitea_login]
new_contents = entry_mod.serialize(ent)
ent = metadata_mod.apply_values(
st.entry, {"owners": st.entry.owners + [viewer.gitea_login]})
files = metadata_mod.write_entry_files(f"rfcs/{slug}.md", ent, st)
try:
pr = await bot.open_claim_pr(
viewer.as_actor(),
org=config.gitea_org, meta_repo=(projects_mod.default_content_repo(config) or ""),
slug=slug,
new_file_contents=new_contents, prior_sha=meta_sha,
files=files,
)
except GiteaError as e:
raise HTTPException(502, f"Gitea: {e.detail}")
@@ -527,11 +521,12 @@ def make_router(
403, "Only this RFC's owners or a site owner may retire it"
)
prior_state = rfc["state"]
entry, sha = await _read_meta_entry(slug)
entry.state = "retired"
st = await _read_meta_entry(slug)
entry = metadata_mod.apply_values(st.entry, {"state": "retired"})
files = metadata_mod.write_entry_files(f"rfcs/{slug}.md", entry, st)
await _run_state_flip(
config=config, gitea=gitea, bot=bot, actor=viewer.as_actor(),
slug=slug, new_contents=entry_mod.serialize(entry), prior_sha=sha,
slug=slug, files=files,
verb="retire", target_state="retired",
)
_audit(
@@ -557,11 +552,12 @@ def make_router(
raise HTTPException(403, "Only a site owner may un-retire an RFC")
_require_retired(slug)
restored = _prior_state_before_retire(slug)
entry, sha = await _read_meta_entry(slug)
entry.state = restored
st = await _read_meta_entry(slug)
entry = metadata_mod.apply_values(st.entry, {"state": restored})
files = metadata_mod.write_entry_files(f"rfcs/{slug}.md", entry, st)
await _run_state_flip(
config=config, gitea=gitea, bot=bot, actor=viewer.as_actor(),
slug=slug, new_contents=entry_mod.serialize(entry), prior_sha=sha,
slug=slug, files=files,
verb="unretire", target_state=restored,
)
_audit(
@@ -602,17 +598,17 @@ def make_router(
raise HTTPException(409, f"RFC is {row['state']}, not retired")
return row
async def _read_meta_entry(slug: str) -> tuple[entry_mod.Entry, str]:
fetched = await gitea.read_file(
config.gitea_org, (projects_mod.default_content_repo(config) or ""), f"rfcs/{slug}.md", ref="main",
async def _read_meta_entry(slug: str):
"""Dual-read an entry from meta-main → EntryGitState (sidecar-aware,
§22.4a). A migrated body-only `.md` reads cleanly; never raises on bad
metadata (INV-3)."""
st = await metadata_mod.read_entry_from_git(
gitea, config.gitea_org,
(projects_mod.default_content_repo(config) or ""), f"rfcs/{slug}.md",
)
if fetched is None:
if st is None:
raise HTTPException(409, f"Meta entry rfcs/{slug}.md not found on main")
text, file_sha = fetched
try:
return entry_mod.parse(text), file_sha
except Exception as e:
raise HTTPException(500, f"Meta entry malformed: {e}")
return st
async def _refresh_catalog() -> None:
# Inline refresh so the catalog reflects the flip immediately; the
@@ -640,8 +636,7 @@ async def _orchestrate(
bot: Bot,
actor: Actor,
state: GraduationState,
graduated_contents: str,
meta_file_sha: str,
graduation_files: list[dict],
) -> None:
"""Open the flip PR, then merge it. Two steps, no transaction:
@@ -661,8 +656,7 @@ async def _orchestrate(
actor,
org=config.gitea_org, meta_repo=(projects_mod.default_content_repo(config) or ""),
slug=state.slug,
new_file_contents=graduated_contents,
prior_sha=meta_file_sha,
files=graduation_files,
rfc_id=state.rfc_id,
owners=state.owners,
)
@@ -850,12 +844,12 @@ async def _run_state_flip(
bot: Bot,
actor: Actor,
slug: str,
new_contents: str,
prior_sha: str,
files: list[dict],
verb: str,
target_state: str,
) -> None:
"""§13.7: open + merge a retire / un-retire frontmatter flip PR. Runs
"""§13.7: open + merge a retire / un-retire state-flip PR. The flip is
written to the entry's metadata sidecar (§22.4a) via `files` ops. Runs
inline (no SSE — the flip is a single quick state change, unlike the
multi-step graduation that streams progress). On an open failure
nothing was created; on a merge failure the half-open PR/branch is
@@ -865,7 +859,7 @@ async def _run_state_flip(
pr = await bot.open_retire_flip_pr(
actor,
org=config.gitea_org, meta_repo=(projects_mod.default_content_repo(config) or ""),
slug=slug, new_file_contents=new_contents, prior_sha=prior_sha,
slug=slug, files=files,
verb=verb, target_state=target_state,
)
except GiteaError as e: