# 정제 status ↔ history 실시간 동기화 수정

## Lv.1 작업

## 문제
대시보드 InsuWiki 탭에서:
- progress bar: "실패" 5% (refine-status.json 기반)
- 정제 이력: "진행중" (refine-history.json 기반)
- 실제: 프로세스 없음, lock 파일 없음

status와 history가 동기화되지 않아 UI 불일치 발생.

### 원인
1. `GET /api/wiki/refine/status`에서 프로세스 비정상 종료 감지 시 status를 "failed"로 변경하지만, **history는 업데이트하지 않음**
2. 정제 시작 시 history에 "진행중" 기록하지만, 비정상 종료 시 history를 "failed"로 변경하는 코드가 없음

## 수정
`/home/jay/workspace/dashboard/server.py`

### 1. `GET /api/wiki/refine/status` 핸들러 (3165줄 부근)
프로세스 비정상 종료 감지 → status "failed" 설정하는 부분에 **history 업데이트 추가**:

현재 (3189-3193줄):
```python
if pid and not _is_process_alive(pid):
    status_data["status"] = "failed"
    status_data["currentStep"] = "프로세스 비정상 종료"
    status_path.write_text(json.dumps(status_data, ensure_ascii=False, indent=2), encoding="utf-8")
    lock_path.unlink(missing_ok=True)
```

추가:
```python
    # history도 동기화
    _update_refine_history_status(status_data, "failed")
```

유사하게 3196-3199줄 (running인데 lock 없음):
```python
if status_data.get("status") == "running" and not lock_path.exists():
    status_data["status"] = "failed"
    status_data["currentStep"] = "프로세스 비정상 종료 (lock 없음)"
    status_path.write_text(...)
    # history도 동기화
    _update_refine_history_status(status_data, "failed")
```

### 2. `_update_refine_history_status` 헬퍼 함수 추가
```python
def _update_refine_history_status(status_data: dict, new_status: str) -> None:
    history_path = Path(__file__).parent / "data" / "refine-history.json"
    if not history_path.exists():
        return
    try:
        history = json.loads(history_path.read_text(encoding="utf-8"))
        for entry in history:
            if entry.get("status") in ("running", "resuming", "진행중"):
                entry["status"] = new_status
        history_path.write_text(json.dumps(history, ensure_ascii=False, indent=2), encoding="utf-8")
    except Exception:
        pass
```

### 3. 정제 완료 시에도 history 동기화
정제 완료(`completed`) 감지 시에도 history status를 "completed"로 업데이트하는 것 확인.
기존 `_update_refine_history` 함수가 있으면 활용, 없으면 위 헬퍼로 통합.

## 검증 시나리오
1. 정제 시작 → `refine-history.json`에 "running" 기록 확인
2. 프로세스 kill → 다음 status polling 시 history도 "failed"로 변경 확인
3. `curl http://localhost:8000/api/wiki/refine/status` → status와 history 일치 확인
4. 정제 취소 → history "cancelled" 동기화 확인
5. 정제 완료 → history "completed" 동기화 확인

## 수정 후
- 대시보드 재시작: `systemctl --user restart dashboard`

## 보고서
`/home/jay/workspace/memory/reports/task-{TASK_ID}.md`