Own the _pr/<n> deleted-branch PR-head contract (shared pr_head_branch helper in refs.py) #3

Closed
opened 2026-08-11 14:59:05 +02:00 by AlexKucera · 0 comments
Owner

Problem Statement

The deleted-branch PR survival feature — recreating a pull request whose source
head branch is gone — rests on one string, the branch-name pattern
_pr/<n>, duplicated across two modules in two different architectural layers
with no shared symbol binding them. The git-transport layer (mirror.py)
creates the branch refs/heads/_pr/<n> for each PR whose source head is
refs/pull/<n>/head; the REST/entity layer (issues.py) references that same
branch (_pr/<n>) as the PR's head when the original branch is gone. Both
copies use the literal f"_pr/{n}", but nothing ties them together: edit one
side and the other silently breaks, and the failure is narrow and late — only
deleted-branch PRs, only at PR-create time, surfacing as a 404/500 against a
nonexistent head branch. The contract is documented in prose (README.md,
AGENTS.md), but documentation does not compile; it is enforced by nothing.

Solution

Own the contract as a single shared helper — pr_head_branch(n) — that both
layers call instead of each spelling the pattern themselves. Because the two
copies call one function, they cannot diverge on the prefix or the format. The
helper lives in refs.py, which broadens from "cross-reference rewriting" to
"reference-namespace knowledge" (a branch-name pattern is reference-namespace
knowledge). refs.py is pure (stdlib re only), so the git layer importing it
drags in no REST-layer coupling, and neither layer depends on the other's
internals — both reach a neutral pure module. The helper is a free function, not
a method on the Migration handle, because its producer (mirror.py) has no
Migration. No test is added for this change itself: a shared symbol prevents
divergence by construction, so a test asserting "the two literals agree" would
be testing the absence of duplication.

User Stories

  1. As an operator, I want deleted-branch PRs to still recreate correctly after
    this change — the migration's behaviour is unchanged.
  2. As an operator, I want a closed/merged PR whose source branch was deleted to
    still rebuild on the target with its original head commits.
  3. As a maintainer, I want the _pr/<n> branch-name pattern defined in exactly
    one place, so I cannot edit one copy and silently break the other.
  4. As a maintainer, I want the deleted-branch survival contract to be a named,
    importable symbol, not a README note that nobody enforces.
  5. As a maintainer, I want to rename or restructure the namespace by changing
    one symbol, not by hunting two f-strings across two modules.
  6. As a maintainer, I want the git layer and the REST layer to share the
    vocabulary without one layer depending on the other's internals.
  7. As a maintainer, I want refs.py to be the home for all reference-namespace
    knowledge — branch-name patterns and cross-reference rewriting — so reference
    logic is not scattered.
  8. As a contributor, I want to find the deleted-branch contract by grepping for
    one symbol, not by knowing to look in two separate files.
  9. As a test author, I want the shared symbol to prevent divergence by
    construction, so I do not need a test that two string literals agree.
  10. As CI, I want the behavioural round-trip (branch pushed → head referenced →
    PR created) covered by the entity-migration test seam, not duplicated in a
    change that only deduplicates a string.
  11. As CI, I want the existing ruff and pytest gates to keep passing.
  12. As a future maintainer, I want the namespace changeable in one place should
    the convention ever need to evolve (for example, a collision with a real
    branch name).
  13. As a future maintainer, I want the helper to stay a pure function with no
    I/O, so it cannot accrete behaviour beyond naming the branch.

Implementation Decisions

  • New free function pr_head_branch(n: int) -> str in refs.py, returning
    f"_pr/{n}". Pure, no I/O. The shared kernel is the bare branch name; the
    producer prefixes the git-refspec syntax (refs/heads/, the sha: source)
    locally, because those are refspec concerns, not part of the shared contract.

  • refs.py widens in purpose. Its module docstring and AGENTS.md entry
    move from "cross-reference rewriting" to "reference-namespace knowledge"
    (branch-name patterns plus cross-reference rewriting). It remains pure stdlib.

  • Producer (mirror.py) changes f"{sha}:refs/heads/_pr/{n}" to
    f"{sha}:refs/heads/{pr_head_branch(n)}" and adds
    from .refs import pr_head_branch. This is mirror.py's first intra-package
    import; its stdlib-only posture is incidental (no document elevates it to an
    invariant), and importing a pure helper changes no behaviour.

  • Consumer (issues.py) changes head_ref = f"_pr/{item['number']}" to
    head_ref = pr_head_branch(item['number']). refs.py is already imported.

  • n is an int on both sides (the mirror parses int(ref.split("/")[2]);
    the consumer reads the source PR number from the API), so there is no int/str
    mismatch to bridge.

  • Not a Migration method. The producer (mirror.py, git layer) holds no
    Migration handle, so the helper is a free function both layers import. It is
    orthogonal to the reference-rewriting accessor on the Migration handle, which
    rewrites #NNN text references — a different "ref" concept from a PR's head
    branch
    .

  • Timing: lands after the git-op seam and the Migration handle. Both of
    those candidates reorganise this change's two call sites (the push logic moves
    under the git-op seam; _create_entity moves onto the Migration handle), so
    landing this afterwards edits each call site once on settled modules.

  • DOX and doc closeout. refs.py's docstring is updated; README.md and
    AGENTS.md — which both describe refs/heads/_pr/<n> in prose — gain a
    one-line note that pr_head_branch is the single source of truth for the
    namespace. The prose describing deleted-branch PR survival stays.

    Interface (decision-rich parts only):

    # refs.py
    def pr_head_branch(n: int) -> str:        # e.g. n=42 -> "_pr/42"
        return f"_pr/{n}"
    
    # mirror.py (producer) — prefixes the refspec syntax locally
    refspecs = [f"{sha}:refs/heads/{pr_head_branch(n)}" for n, sha in pull_heads.items()]
    
    # issues.py (consumer) — uses the bare branch name as the PR head
    head_ref = pr_head_branch(item['number'])
    

Testing Decisions

  • No change-specific test. A shared symbol prevents string divergence by
    construction: two call sites cannot disagree when they call one function. A
    test asserting "the two literals agree" would be testing the absence of
    duplication, which is redundant once the duplication is removed.
  • Where the coverage lands. The behavioural round-trip — the mirror pushes
    refs/heads/_pr/<n>, the issue phase sends _pr/<n> as the PR head, Gitea
    creates the PR — is the deleted-branch survival path. That path is reached by
    the entity-migration test seam (two mock-transport clients plus a phase call)
    introduced by the Migration-handle candidate; this change does not duplicate
    it.
  • Gates. ruff check ., ruff format --check ., and pytest remain green.
    The change touches two production lines and adds one pure function; it adds no
    new test of its own.

Out of Scope

  • The other architecture-review candidates: the git-op seam, the Migration
    handle, and collapsing the target-repo lifecycle helpers.
  • Renaming the _pr/ prefix. This candidate owns the namespace; it does not
    change it.
  • A behavioural test of the deleted-branch round-trip — that coverage belongs to
    the entity-migration test seam and is not duplicated here.
  • Making pr_head_branch a method on the Migration handle — its producer has
    no handle.
  • Splitting refs.py into separate namespace modules — scaffolding for one
    helper (YAGNI).

Further Notes

  • Architecture-review candidate #3 — the smallest of the four. The grilling is
    recorded: own it as a shared helper (not a constant, which would still
    duplicate the format); house it in refs.py; land it after the git-op seam and
    the Migration handle; add no change-specific test. Settled upfront that
    mirror.py's stdlib-only, zero-intra-package-import posture is incidental,
    not a deliberate isolation invariant, so its first intra-package import is not
    a load-bearing change.
  • Blocked by the git-op-seam and Migration-handle candidates: both
    reorganise this change's two call sites, so this lands last to avoid
    double-editing. The three are otherwise independent.
  • Delivered as a single small PR; target Python ≥ 3.11, ruff line-length 100.

[Generated by AI]

## Problem Statement The deleted-branch PR survival feature — recreating a pull request whose source head branch is gone — rests on **one string**, the branch-name pattern `_pr/<n>`, duplicated across two modules in two different architectural layers with no shared symbol binding them. The git-transport layer (`mirror.py`) *creates* the branch `refs/heads/_pr/<n>` for each PR whose source head is `refs/pull/<n>/head`; the REST/entity layer (`issues.py`) *references* that same branch (`_pr/<n>`) as the PR's `head` when the original branch is gone. Both copies use the literal `f"_pr/{n}"`, but nothing ties them together: edit one side and the other silently breaks, and the failure is narrow and late — only deleted-branch PRs, only at PR-create time, surfacing as a 404/500 against a nonexistent head branch. The contract is documented in prose (`README.md`, `AGENTS.md`), but documentation does not compile; it is enforced by nothing. ## Solution Own the contract as a single shared helper — `pr_head_branch(n)` — that both layers call instead of each spelling the pattern themselves. Because the two copies call one function, they cannot diverge on the prefix or the format. The helper lives in `refs.py`, which broadens from "cross-reference *rewriting*" to "reference-*namespace* knowledge" (a branch-name pattern *is* reference-namespace knowledge). `refs.py` is pure (stdlib `re` only), so the git layer importing it drags in no REST-layer coupling, and neither layer depends on the other's internals — both reach a neutral pure module. The helper is a free function, not a method on the `Migration` handle, because its producer (`mirror.py`) has no `Migration`. No test is added for this change itself: a shared symbol prevents divergence by construction, so a test asserting "the two literals agree" would be testing the absence of duplication. ## User Stories 1. As an operator, I want deleted-branch PRs to still recreate correctly after this change — the migration's behaviour is unchanged. 2. As an operator, I want a closed/merged PR whose source branch was deleted to still rebuild on the target with its original head commits. 3. As a maintainer, I want the `_pr/<n>` branch-name pattern defined in exactly one place, so I cannot edit one copy and silently break the other. 4. As a maintainer, I want the deleted-branch survival contract to be a named, importable symbol, not a README note that nobody enforces. 5. As a maintainer, I want to rename or restructure the namespace by changing one symbol, not by hunting two f-strings across two modules. 6. As a maintainer, I want the git layer and the REST layer to share the vocabulary without one layer depending on the other's internals. 7. As a maintainer, I want `refs.py` to be the home for all reference-namespace knowledge — branch-name patterns and cross-reference rewriting — so reference logic is not scattered. 8. As a contributor, I want to find the deleted-branch contract by grepping for one symbol, not by knowing to look in two separate files. 9. As a test author, I want the shared symbol to prevent divergence by construction, so I do not need a test that two string literals agree. 10. As CI, I want the behavioural round-trip (branch pushed → head referenced → PR created) covered by the entity-migration test seam, not duplicated in a change that only deduplicates a string. 11. As CI, I want the existing ruff and pytest gates to keep passing. 12. As a future maintainer, I want the namespace changeable in one place should the convention ever need to evolve (for example, a collision with a real branch name). 13. As a future maintainer, I want the helper to stay a pure function with no I/O, so it cannot accrete behaviour beyond naming the branch. ## Implementation Decisions - **New free function `pr_head_branch(n: int) -> str`** in `refs.py`, returning `f"_pr/{n}"`. Pure, no I/O. The shared kernel is the *bare branch name*; the producer prefixes the git-refspec syntax (`refs/heads/`, the `sha:` source) locally, because those are refspec concerns, not part of the shared contract. - **`refs.py` widens in purpose.** Its module docstring and `AGENTS.md` entry move from "cross-reference rewriting" to "reference-namespace knowledge" (branch-name patterns plus cross-reference rewriting). It remains pure stdlib. - **Producer (`mirror.py`)** changes `f"{sha}:refs/heads/_pr/{n}"` to `f"{sha}:refs/heads/{pr_head_branch(n)}"` and adds `from .refs import pr_head_branch`. This is `mirror.py`'s first intra-package import; its stdlib-only posture is incidental (no document elevates it to an invariant), and importing a pure helper changes no behaviour. - **Consumer (`issues.py`)** changes `head_ref = f"_pr/{item['number']}"` to `head_ref = pr_head_branch(item['number'])`. `refs.py` is already imported. - **`n` is an `int` on both sides** (the mirror parses `int(ref.split("/")[2])`; the consumer reads the source PR number from the API), so there is no int/str mismatch to bridge. - **Not a `Migration` method.** The producer (`mirror.py`, git layer) holds no `Migration` handle, so the helper is a free function both layers import. It is orthogonal to the reference-rewriting accessor on the `Migration` handle, which rewrites `#NNN` *text* references — a different "ref" concept from a PR's *head branch*. - **Timing: lands after the git-op seam and the `Migration` handle.** Both of those candidates reorganise this change's two call sites (the push logic moves under the git-op seam; `_create_entity` moves onto the `Migration` handle), so landing this afterwards edits each call site once on settled modules. - **DOX and doc closeout.** `refs.py`'s docstring is updated; `README.md` and `AGENTS.md` — which both describe `refs/heads/_pr/<n>` in prose — gain a one-line note that `pr_head_branch` is the single source of truth for the namespace. The prose describing deleted-branch PR survival stays. *Interface (decision-rich parts only):* ``` # refs.py def pr_head_branch(n: int) -> str: # e.g. n=42 -> "_pr/42" return f"_pr/{n}" # mirror.py (producer) — prefixes the refspec syntax locally refspecs = [f"{sha}:refs/heads/{pr_head_branch(n)}" for n, sha in pull_heads.items()] # issues.py (consumer) — uses the bare branch name as the PR head head_ref = pr_head_branch(item['number']) ``` ## Testing Decisions - **No change-specific test.** A shared symbol prevents string divergence by construction: two call sites cannot disagree when they call one function. A test asserting "the two literals agree" would be testing the absence of duplication, which is redundant once the duplication is removed. - **Where the coverage lands.** The behavioural round-trip — the mirror pushes `refs/heads/_pr/<n>`, the issue phase sends `_pr/<n>` as the PR `head`, Gitea creates the PR — is the deleted-branch survival path. That path is reached by the entity-migration test seam (two mock-transport clients plus a phase call) introduced by the `Migration`-handle candidate; this change does not duplicate it. - **Gates.** `ruff check .`, `ruff format --check .`, and `pytest` remain green. The change touches two production lines and adds one pure function; it adds no new test of its own. ## Out of Scope - The other architecture-review candidates: the git-op seam, the `Migration` handle, and collapsing the target-repo lifecycle helpers. - Renaming the `_pr/` prefix. This candidate owns the namespace; it does not change it. - A behavioural test of the deleted-branch round-trip — that coverage belongs to the entity-migration test seam and is not duplicated here. - Making `pr_head_branch` a method on the `Migration` handle — its producer has no handle. - Splitting `refs.py` into separate namespace modules — scaffolding for one helper (YAGNI). ## Further Notes - Architecture-review candidate #3 — the smallest of the four. The grilling is recorded: own it as a shared helper (not a constant, which would still duplicate the format); house it in `refs.py`; land it after the git-op seam and the `Migration` handle; add no change-specific test. Settled upfront that `mirror.py`'s stdlib-only, zero-intra-package-import posture is *incidental*, not a deliberate isolation invariant, so its first intra-package import is not a load-bearing change. - **Blocked by** the git-op-seam and `Migration`-handle candidates: both reorganise this change's two call sites, so this lands last to avoid double-editing. The three are otherwise independent. - Delivered as a single small PR; target Python ≥ 3.11, ruff line-length 100. [Generated by AI]
AlexKucera added the ready-for-agentPRD labels 2026-08-11 14:59:05 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: AlexKucera/codeberg-to-gitea-migrator#3