# Own the _pr/ deleted-branch PR-head contract (shared pr_head_branch helper in refs.py) > **Date:** 2026-08-11 > **Type:** issue > **Reference:** [#3](https://git.babylondreams.de/AlexKucera/codeberg-to-gitea-migrator/issues/3) ## Goal Eliminate the duplicated `_pr/` branch-name literal that the deleted-branch PR survival feature rested on. The git-transport layer (`mirror.py`) *created* `refs/heads/_pr/`; the REST/entity layer (`issues.py`) *referenced* the same branch as a PR's head when the source branch was gone. Both spelled `f"_pr/{n}"` with no shared symbol binding them — editing one side silently broke the other, and the failure was narrow and late (404/500 on PR create for deleted-branch PRs only). Own the contract as one shared, importable helper so the two cannot diverge. ## What Was Done - Added `pr_head_branch(n: int) -> str` to `refs.py` (returns `f"_pr/{n}"`); pure, no I/O. The single source of truth for the `_pr/` namespace. - `mirror.py` (producer): added `from .refs import pr_head_branch`; refspec now `f"{sha}:refs/heads/{pr_head_branch(n)}"` (refspec syntax stays local). - `issues.py` (consumer): added `from .refs import pr_head_branch`; PR head now `pr_head_branch(item["number"])` instead of the inline literal. - Widened `refs.py`'s module docstring from "cross-reference rewriting" to "reference-namespace knowledge" (branch-name patterns + cross-ref rewriting); still pure stdlib (`re` only). - Docs closeout: `src/.../AGENTS.md` module-map (`refs.py` + `issues.py` entries) and `README.md` "Pull-request heads" bullet now name `refs.pr_head_branch` as the single source of truth. ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | Free function, not a `Migration` method | The producer (`mirror.py`, git layer) holds no `Migration` handle, so both layers reach a neutral pure module instead of one depending on the other's internals. | | House it in `refs.py` | A branch-name pattern *is* reference-namespace knowledge; broadens `refs.py` coherently. Pure stdlib, so the git layer importing it drags in no REST coupling. | | Shared kernel = bare branch name only | The producer prefixes refspec syntax (`refs/heads/`, the `sha:` source) locally — those are refspec concerns, not part of the shared contract. | | `n` stays `int` on both sides | The mirror parses `int(ref.split("/")[2])`; the consumer reads the source PR number — no int/str bridge needed. | | No change-specific test | A shared symbol prevents divergence by construction; a test asserting "the two literals agree" tests the absence of duplication (issue #3 testing decision). The deleted-branch round-trip stays covered by the existing phase test seam. | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | PRD claimed `refs.py` was "already imported" in `issues.py` | It wasn't — `issues.py` imported `.clients` + `.migration` only (`m.rewrite` reaches refs via the `Migration` handle) | Added `from .refs import pr_head_branch` to `issues.py`. Minimal deviation from the PRD; intent unchanged. | | First cut added a `__main__` self-check `assert pr_head_branch(42) == "_pr/42"` | That asserts "the literal agrees" — exactly the redundant test the spec's testing decision warns against, and a trivial one-liner under ponytail | Removed it after the fresh-context review fanout confirmed it was redundant. | ## Files Changed | File | Change Summary | |------|----------------| | `src/codeberg_to_gitea_migrator/refs.py` | Add `pr_head_branch(n)`; widen module docstring to reference-namespace knowledge. | | `src/codeberg_to_gitea_migrator/mirror.py` | Import + route the PR-head refspec through `pr_head_branch`. | | `src/codeberg_to_gitea_migrator/issues.py` | Import + route the deleted-branch PR head through `pr_head_branch`. | | `src/codeberg_to_gitea_migrator/AGENTS.md` | Module map: `refs.py` and `issues.py` entries note the shared symbol. | | `README.md` | "Pull-request heads" bullet notes `refs.pr_head_branch` as the single source of truth. | | `AGENTS.md` (root) | GitNexus symbol count auto-bump (434→444) from re-index. | ## Open Items & Next Steps - [ ] **Untracked `CONTEXT.md`** — predates this session, not gitignored, but referenced by `AGENTS.md` as the DOX "single-context" core doc. Track-vs-ignore intent undecided; left out of this commit intentionally. Not in #3's scope. - [ ] None for #3 itself — all 13 user stories met; behaviour unchanged. --- *Log written by write-log skill*