---
task_id: task-2510
type: context
scope: task
created: 2026-05-08
updated: 2026-05-08
status: completed
---

# 맥락 노트: task-2510 — replacement_pr_runner

**task**: task-2510 — contaminated PR 자동 replacement 생성 (5 모듈 #2)

---

## 결정 근거

### [결정 1] helpers 재사용 (merge_queue_executor.py에서 import)
- **이유**: `compare_effective_diff`, `detect_forbidden_paths`, `assert_no_forbidden_git_flags`, `_normalize_file_list`, `DEFAULT_FORBIDDEN_PATTERNS`는 task-2509에서 검증된 helper. 동일 로직을 복제하면 enum/path 정의가 분산되어 forbidden_path 정의 충돌 위험.
- **대안 기각**: helper 복제 → 4 모듈 × 5 helper = 20 정의 분산. 회장 §단일 정의 원칙 위배.
- **회장 §allowed_resources 호환**: read_only_paths에 `utils/merge_queue_executor.py` 명시 → import 합법.

### [결정 2] automation_contracts에서 ReplacementResult/EscalationPacket import
- **이유**: task-2509+2 freeze 결과. 회장 §5 명시 "automation_contracts.py 사용". `ReplacementResult` 도메인 규칙(`success=False → failure_reason 필수`)을 freeze 모듈에 위임.

### [결정 3] subprocess runner injection 패턴 (테스트 가능)
- **이유**: 회귀 12건 중 8/9번이 fixture 재현(PR #54 78건, task-2506 117건, task-2507) — 실제 git/gh 호출 없이 fake runner로 시뮬레이션해야 함.
- **대안 기각**: 직접 subprocess.run 호출 → 테스트 시 실제 PR이 필요해서 회귀 불가.

### [결정 4] origin/main 기준 신규 brace 생성 (contaminated branch 재활용 금지)
- **이유**: 회장 §6 amendment "contaminated branch 재사용 금지". 원 PR base에 누적된 commit이 섞여있으면 replacement도 오염됨. 반드시 `origin/main` HEAD에서 fresh branch 생성.
- **branch 명명**: `task/<task_id>-replacement-<timestamp>` — 충돌 방지.

### [결정 5] expected_files만 git show + write로 이식 (cherry-pick 금지)
- **이유**: 회장 §6 amendment "자동 cherry-pick 금지". cherry-pick은 commit history 단위로 적용되어 무관한 변경이 따라올 수 있음. 파일 단위 추출(`git show <head>:<path>` → write)이 안전.

### [결정 6] 원 PR 보존 (close/delete 금지)
- **이유**: 회장 §3 명시. 사람이 contaminated 원인을 분석할 수 있어야 함. close 시 audit trail 손실.
- **표시**: `[REPLACED]` 코멘트만 추가. label 변경/title 변경 X.

### [결정 7] critical escalation은 EscalationPacket 작성만 (실제 보고는 task-2513)
- **이유**: 회장 §critical_escalation_reporter 구현 금지(task-2513 영역). 본 task에서는 EscalationPacket dataclass 인스턴스만 생성하여 ReplacementResult.failure_reason에 enum value를 기록.

## 3 Step Why 자문 (G1 Lv.3 의무)

**1st Why**: 왜 이 설계(replacement_pr_runner)가 필요한가?
→ A. PR #54 / task-2506 / task-2507 같은 contaminated 사례에서 base 누적 commit이 섞이면 회장이 수동으로 1) close 신호, 2) clean branch 만들기, 3) expected_files만 추출, 4) PR 다시 만들기를 반복해야 함. 자동화하면 사람 개입 없이 일관된 처리가 가능.

**2nd Why**: 왜 A가 최선의 접근인가? (대안: cherry-pick 자동화 / rebase 자동화 / contaminated branch에서 reset)
→ B. cherry-pick은 commit 단위라 expected_files 외 무관 변경이 함께 옴. rebase는 force push 필요(회장 금지). reset은 history 손실. "신규 branch + 파일 단위 git show + write"가 안전성/감사성/원자성 모두 충족.

**3rd Why**: 왜 B가 다른 대안보다 나은가?
→ C. 파일 단위 이식은 (1) commit history 영향 0, (2) 원 PR 보존, (3) replacement diff가 expected_files와 정확히 일치함을 사후 검증 가능, (4) cherry-pick/rebase 같은 git 마법 없이 file copy + commit만으로 구현 → 단위 테스트로 100% 시뮬레이션 가능. fake runner inject로 fixture 12건 재현 가능.

A-B-C 일관: 자동화 필요 → 신규 branch + 파일 이식 → 안전성/테스트 가능성/원자성 ✓

## Codex 사전 검증 결과 (2026-05-08 23:01)

- **결과**: pass=False (작업 시작 시점 기준 자명한 critical)
- **risks**:
  - critical: worktree HEAD가 origin/main과 동일 (`9cd28bf1`) — 산출물 미생성 ← 작업 시작 시점이라 의도된 상태. 진행 후 자동 해소.
  - high: workspace root에 의존 파일 부재 ← worktree에서 작업하므로 무관 (worktree는 origin/main 정렬 완료).
  - medium: dependency 문서가 task-2509+2를 in-progress로 표기 ← task 파일 metadata 텍스트일 뿐, 실제 main에는 머지됨(`9cd28bf1`).
- **판정**: 모든 risk가 작업 진행으로 해소되는 자명한 상태. 작업 종료 시 재검증 예정.

## 참조 자료

- 공통 계약: `utils/automation_contracts.py` (task-2509+2 freeze, main `9cd28bf1`)
- helpers: `utils/merge_queue_executor.py` (task-2509)
- 실전 fixture:
  - PR #54 (task-2507 first attempt) — effective diff 78건 (base: task-2487+1/2503/2485+1/2488/2489/2493 + POC + scripts)
  - task-2506 contaminated branch — task-2479-dev1 base 117건
  - task-2507 contaminated branch — PR #54 동일

## 주의사항 (회장 명시 금지 행위)

- ❌ dispatch.py wiring 금지
- ❌ merge_queue_executor 대규모 수정 금지 (helper import만)
- ❌ auto_gemini_triage / post_merge_smoke_runner / critical_escalation_reporter 구현 금지
- ❌ PR #52/#49/#50/#51 수정 금지
- ❌ 원 PR close/delete 금지 (★ original_pr_preserved=True 보장)
- ❌ contaminated branch 재활용 / force push / rebase / admin override 금지
- ❌ 자동 cherry-pick 구현 금지 (★ 회장 amendment)
- ❌ Critical 7종 외 회장 보고 금지
- ❌ expected_files(2개) 외 수정 금지
- ❌ 정책 md만 작성하고 종료 금지 (산출물 = 코드 + 테스트)
- ❌ manual .done 생성 금지
