# task-903.1 완료 보고서: Phase 2 보안 기반 (Layer 3 전제 조건) 구현

**담당**: 오딘 (dev2-team 팀장)
**날짜**: 2026-03-24

---

## SCQA

**S**: 자동화 오케스트레이터(auto_orch.py) 구현을 위한 Phase 2 보안 인프라 구축 작업. injection_guard.py 하드블록 패치는 task-900.1에서 완료됨.

**C**: auto_orch.py 동작 전 필수 보안 모듈 3개(pipeline_validator, token_ledger, event_bus)가 미구현 상태. 이 모듈 없이는 YAML 검증, 토큰 한도, 이벤트 원자적 소비가 불가하여 Phase 3 진행 불가.

**Q**: 설계서 기준 11개 검증 항목, TOCTOU 방어, 일일 토큰 한도를 포함한 보안 인프라를 구현하고 검증할 수 있는가?

**A**: 3개 모듈 + 패키지 초기화 + 테스트 32건 구현 완료. pytest 32/32 통과, pyright 0 에러. 실질 코드 344 LOC + 테스트 554 LOC.

---

## 생성 파일 목록

- `orchestrator/__init__.py` — 패키지 초기화 (7 LOC)
- `orchestrator/pipeline_validator.py` — YAML 스키마 검증 + DAG 검증 + 시크릿 스캔 (200 LOC)
- `orchestrator/token_ledger.py` — 일일 토큰 한도 + 파이프라인별 예산 (92 LOC)
- `orchestrator/event_bus.py` — 원자적 .done 이벤트 소비 (52 LOC)
- `orchestrator/tests/__init__.py` — 테스트 패키지 초기화
- `orchestrator/tests/test_phase2.py` — 테스트 32건 (554 LOC)
- 디렉토리 생성: `orchestrator/{state,locks,incoming,processed,gates,logs}/`

---

## 테스트 결과 (정량적 증거)

**pytest**: 32 passed, 0 failed (0.13s)

- TestPipelineValidator: 15/15 PASS
  - 유효 YAML 통과, schema_version/gates/token_budget/blast_radius/allowed_teams 필수 필드 검증
  - Kahn's algorithm 순환 DAG 감지 (A→B→C→A), 자기 참조(A→A) 감지
  - AWS_ACCESS_KEY_ID 시크릿 패턴 차단
  - target_team∉allowed_teams 거부, 존재하지 않는 depends_on 참조 거부
  - task_desc 인젝션 패턴 차단 (injection_guard 연동)
  - inject_context.source path traversal (../../etc/passwd) 거부
- TestTokenLedger: 13/13 PASS
  - DAILY_HARD_LIMIT=1,000,000 상수 검증 및 초과 거부
  - MAX_CONCURRENT_PIPELINES=3 초과 시 새 파이프라인 거부
  - MAX_PIPELINE_STARTS_PER_DAY=20 초과 시 거부
  - 날짜 변경 시 사용량 리셋 검증
- TestEventBus: 4/4 PASS
  - 정상 소비: incoming→processed 원자적 이동
  - TOCTOU 테스트: multiprocessing 2프로세스 동시 소비 → 정확히 1개만 성공
  - symlink .done 거부

**pyright**: 0 errors, 0 warnings, 0 informations (5 파일)
**black + isort**: 포맷팅 준수 확인 완료

---

## 발견 이슈 및 해결

### 자체 해결 (3건)

1. **monkeypatch 날짜 변경 미도달** — token_ledger.py가 `from datetime import date`로 직접 바인딩하여 monkeypatch가 도달하지 못함. `import datetime` + `datetime.date.today()` 방식으로 변경하여 해결.

2. **pipeline_validator.py LOC 초과** — 목표 ~80 LOC 대비 200 LOC 산출. 설계서 11개 검증 항목 + scan_secrets/validate_dag 헬퍼를 포함하면 불가피. 로직 자체는 최소화 유지.

3. **테스트 import 순서 isort 불일치** — black 포맷팅 후 isort 재정렬로 해결.

---

## 체크리스트 달성 (Phase 2)

- [x] P2-2. pipeline_validator 11개 검증 항목 전부 동작
- [x] P2-3. token_ledger DAILY_HARD_LIMIT/MAX_CONCURRENT/MAX_STARTS 구현
- [x] P2-4. event_bus POSIX rename 원자적 소비 + symlink 거부
- [x] SEC-1. pyright 0 에러
- [x] SEC-2. black + isort 준수
- [x] SEC-5. path traversal 방어 (inject_context.source)
- [x] T2-1~T2-7 테스트 항목 전부 통과

## 수정 금지 파일 준수

- `scripts/` — 미수정
- `dashboard/` — 미수정
- `utils/injection_guard.py` — 미수정 (참조만)
