# Migration: bind the repo-transfer context (slim phase interfaces, retire RefCtx/_path) > **Date:** 2026-08-11 > **Type:** issue > **Reference:** [Gitea issue #2](https://git.babylondreams.de/AlexKucera/codeberg-to-gitea-migrator/issues/2) ## Goal Implement issue #2: introduce a single bound-context type — `Migration` — that holds the identity quintet (source client, target client, owner, name, number map) the four entity-migration phases previously received as five loose params. Rewrite the phases to take the handle; retire `RefCtx` and the shared `_path` helper (their reason to exist is absorbed); and give the entity layer its first test surface — construct a `Migration` from two mock-transport clients and call a phase, with no network. Behavior must be unchanged. ## What Was Done - **New `src/codeberg_to_gitea_migrator/migration.py`** — frozen dataclass `Migration(src, tgt, owner, name, numbers=NumberMap())` with exactly two derived accessors: `.path(suffix)` → `/repos/{owner}/{name}{suffix}`, and `.rewrite(text)` delegating to the pure `refs.rewrite` via the bound clients' `web_root`s. Pure context — no I/O, no phase logic (god-object guard in the module docstring). Includes a `__main__` self-check. - **Four phases take the handle** (`issues.py`, `releases.py`): `migrate_labels(m, *, concurrency)`, `migrate_milestones(m, *, concurrency)`, `migrate_issues_and_prs(m, label_map, ms_map, *, concurrency) -> None` (mutates `m.numbers` in place), `migrate_releases(m, *, concurrency) -> int`. Internal helpers (`_collect`/`_set_state`/`_merge_pr`/`_create_entity`) take `m` too, so owner/name stay bound throughout. Dropped the unused `src` param from `_merge_pr`. - **`migrate_repo`** builds one `Migration` once both clients are open and the target repo is ensured, then drives all phases with it, threading the label/milestone maps between them; report reads `len(migration.numbers)`. - **Retired `RefCtx`** (was `issues.py`) and **`_path`** (was `clients.py`) — fully deleted, zero remaining references in `src/`. Dead imports cleaned (`ApiClient`/`_path`/`NumberMap`/`rewrite`/`dataclass` where now unused). - **`tests/test_phases.py`** (new, 10 tests) — the one seam: two `httpx.MockTransport` clients + a `NumberMap`. Asserts wire paths/bodies the target received + number-map state. Covers identity binding + fresh-map default, label/milestone id maps + bodies, two-pass ref rewrite + comment streaming, closed-state PATCH, best-effort merge tolerating 409, deleted-branch `_pr/` head fallback, label-attachment + PR-milestone follow-up PATCH, release+asset re-upload with rewritten body, and the 404-endpoint clean skip. - **DOX closeout** — updated `src/…/AGENTS.md` (module map: added `migration.py`, removed `_path`, noted the handle on the phase/orchestrator lines; added a bound-context Work Guidance bullet; added `test_phases.py` to Verification) and `tests/AGENTS.md` (+`test_phases.py`). ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | Drove the refactor in-context (not parallel subagents) | The change is atomic — phase signatures, the orchestrator, and the `RefCtx`/`_path` retirement are mutually dependent; intermediate states don't even import. Parallel subagents would produce broken intermediates + merge churn exceeding any context-isolation benefit. Honored `/implement`'s scenario-first / TDD-at-the-seam / verify / review parts instead. | | `.path()`/`.rewrite()` are the only two accessors | They are the two things every phase needs that aren't already on a client. Keeps `Migration` pure context; a future per-phase input type (if cross-phase outputs ever grow beyond two maps) is an additive change. | | `concurrency` stays a per-call keyword, not a handle field | It is run-tuning, not identity; the handle binds exactly the quintet (per the issue's explicit rejection of folding concurrency on). | | `migrate_issues_and_prs` returns `None` and mutates `m.numbers` | The number map already lives on the handle; returning it duplicated the source of truth. The sole caller reads `len(migration.numbers)`. | | Internal helpers take `m`, not loose owner/repo | Keeps owner/name bound once (the whole point) and routes every path through `m.path()` — retiring `_path` without reintroducing f-string paths inside helpers. | | Mock harness splits source (reads) vs target (records writes) | Source only reads, target only writes, so two clients with separate handlers is a clean split. `_wire_path` strips the client's `/api/v1` prefix so phase tests assert repo-scoped paths (a transposition fails) without coupling to the client's api-root convention. | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | Recorded wire path was `/api/v1/repos/o/r/labels`, not repo-scoped | `ApiClient` prepends `/api/v1` as `base_url`; `req.url.path` carries it | `_wire_path(req)` strips the prefix; recorders/assertions use repo-scoped paths | | Multipart asset uploads crashed the recorder's `json.loads` | Asset upload body is multipart bytes, not JSON | Recorder falls back to `{"_raw": …}` on `ValueError`; tests assert the upload *path*, not its body | | Orphan `return`/duplicate `report`/duplicate import/lost `fresh` assertion/duplicate try-block during edits | Several `edit` operations left strays when replace ranges collided with shifted anchors | Each caught immediately by `ruff`/`pytest` and removed; re-read for fresh anchors before re-editing | | `_path` flagged CRITICAL by impact analysis | Count artifact — all 11 direct callers live inside the refactored `issues.py`/`releases.py`; `migrate.py` uses raw f-strings, tests don't import it | Confirmed fully internal via grep; retirement left no external caller broken | ## Files Changed | File | Change Summary | |------|----------------| | `src/codeberg_to_gitea_migrator/migration.py` | **New** — frozen `Migration` bound context + `.path()`/`.rewrite()` accessors + self-check | | `tests/test_phases.py` | **New** — 10 phase tests through the Migration seam (two mock clients + number map) | | `src/codeberg_to_gitea_migrator/issues.py` | Phases + helpers take `m`; `migrate_issues_and_prs` → `None` mutating `m.numbers`; deleted `RefCtx`; cleaned imports | | `src/codeberg_to_gitea_migrator/releases.py` | `migrate_releases(m, *, concurrency)`; dropped `ApiClient`/`_path` imports | | `src/codeberg_to_gitea_migrator/migrate.py` | Builds one `Migration`, drives all phases; dropped `RefCtx`/`NumberMap` + standalone `numbers` | | `src/codeberg_to_gitea_migrator/clients.py` | Deleted the `_path` helper | | `src/codeberg_to_gitea_migrator/AGENTS.md` | Module map (+`migration.py`, −`_path`, handle notes), bound-context guidance, Verification (+`test_phases.py`) | | `tests/AGENTS.md` | Added `test_phases.py` to Work Guidance | ## Verification - `uv run pytest -q` → **54 passed** (was 44; +10 phase tests). - `uv run ruff check .` → clean. `uv run ruff format --check .` → clean. - Independent review fanout (ponytail-review / security-review STRIDE / general review): **no blocker/major/minor**; one accepted fix (label-attachment + PR-milestone PATCH coverage gap) → added a test. Ponytail verdict: net **−124/+67** lines, a deletion of complexity. Security: behavior-preserving, marginally improves integrity (owner/name bound once). Token-leak invariant unchanged (AST guard passes; `migration.py` has no subprocess). - `node .gitnexus/run.cjs analyze` → index rebuilt (434 nodes / 884 edges). - `detect_changes`: blast radius confined to the entity-migration layer; git mirror, pure rewriter, REST-client internals, and CLI untouched. ## Open Items & Next Steps - [ ] Issue #2 itself is closed by this commit (`Closes #2`); confirm on Gitea. - [ ] `CONTEXT.md` is untracked and pre-existing — already documents `Migration` accurately; left as-is (out of scope, not introduced here). - [ ] No known tech debt introduced; the bound-context seam is the additive surface for any future per-phase input type. --- *Log written by write-log skill*