# task-372.1 완료 보고서: 자동 체이닝 시스템 구현

## 작업 요약
순차 작업 체이닝 관리 유틸리티 `chain_manager.py`를 신규 구현하고, DIRECT-WORKFLOW.md에 자동 체인 확인 로직을 통합.

## 생성/수정 파일
- **신규** `/home/jay/workspace/chain_manager.py` (19,669 bytes) — 순차 체이닝 CLI 유틸
- **신규** `/home/jay/workspace/tests/test_chain_manager.py` (37,249 bytes) — 단위 테스트 31개
- **수정** `/home/jay/workspace/prompts/DIRECT-WORKFLOW.md` — Step 7에 chain_manager.py next 호출 추가

## chain_manager.py 구현 내용

### CLI 명령어
- `create --chain-id <id> --tasks '<JSON>'` — 체인 생성 + watchdog cron 자동 등록
- `next --task-id <id>` — 완료 task 마킹 → QC gate 확인 → 다음 pending 반환
- `update --task-id <id> --status <status>` — task 상태 업데이트
- `check-stalled --max-hours 2` — 정체 작업 검색
- `list` — 활성 체인 목록

### 핵심 기능
- **QC Gate**: gate=auto 시 보고서의 FAIL 키워드 검색 → FAIL 발견 시 체인 stalled
- **중복 task_file 차단**: 이미 done/running인 task_file과 동일한 pending 차단
- **원자적 lock**: `Path.touch(exist_ok=False)` + 5초 대기 3회 재시도
- **백업**: 상태 업데이트 전 .bak 파일 자동 생성, 읽기 실패 시 .bak 복구
- **watchdog cron**: create 시 2h 후 자동 등록, chain_complete 시 자동 제거
- **no_chain**: 체인에 없는 task_id는 `{"action": "no_chain"}` 반환 (exit 0)

### chain.py와의 관계
- `chain.py`: Phase 기반 멀티팀 동시 협업 체이닝 (기존)
- `chain_manager.py`: 순차 단일 작업 체이닝 + QC 게이트 (신규)
- 두 시스템은 독립적으로 운영, 체인 파일 모두 `memory/chains/`에 저장

## DIRECT-WORKFLOW.md 수정
Step 7 아누 통보 프롬프트에 다음 지시 추가:
- `python3 chain_manager.py next --task-id {task_id}` 실행
- action=dispatch → dispatch.py 위임
- action=stalled → 제이회장님 보고
- action=chain_complete → 체인 완료 보고
- action=no_chain → 무시 (일반 작업)

## 테스트 결과
- **31/31 PASS** (pytest 0.11s)
- TestCreate: 5개 (정상생성, watchdog cron, max_tasks 초과, 중복ID, 잘못된JSON)
- TestNext: 8개 (dispatch, QC FAIL, gate=none, chain_complete, cron제거, no_chain, 중복task_file, completed_at)
- TestUpdate: 5개 (running/done/failed/stalled, not_found)
- TestCheckStalled: 4개 (검출/미검출/빈결과/completed체인스킵)
- TestList: 4개 (빈목록/여러체인/필드확인/task_count)
- TestLock: 2개 (lock생성삭제/순차일관성)
- TestBackup: 3개 (.bak생성/원본보존/next시.bak)

## QC 검증 결과
- **tdd_check**: PASS (테스트 먼저 작성 확인)
- **style_check**: PASS (black + isort 준수)
- **pyright_check**: WARN (utils.logger, chain_manager import — sys.path.insert 런타임 해결, chain.py와 동일한 기존 구조적 한계)
- **file_check**: 보고서/이벤트 파일은 워크플로우 완료 시점에 생성
- **test_runner**: test_qc_integration::TestRegressionTeamPrompts 기존 실패 (이번 작업과 무관)

## 버그
- 없음

## 비고
- pyright reportMissingImports 2건은 chain.py, dispatch.py와 동일한 구조적 이슈 (sys.path.insert로 런타임 해결)
- 기존 insuwiki-p2p3.lock 파일이 chains/ 디렉토리에 잔존 — chain_manager.py는 chain-*.json만 글로빙하므로 충돌 없음
