# 정제 이력 최종 수정 — 동적 갱신 + 누락 복구 + 삭제 기능 + 미리보기

## 수정 1: 정제 이력 동적 갱신 (3번째 — 이번엔 확실하게)

**파일**: `/home/jay/workspace/dashboard/server.py`
**위치**: GET /api/wiki/refine/history 핸들러 (Line 3321 부근)

현재 refine-history.json을 그대로 반환 → 프로세스가 죽어도 "진행중" 유지

수정: 반환 전에 running 상태 항목의 PID 확인
```python
if self.path == "/api/wiki/refine/history":
    # history 로드
    history = json.loads(...)
    
    # running 상태 항목 동적 갱신
    lock_path = Path(__file__).parent / "data" / "refine-lock.json"
    status_path = Path(__file__).parent / "data" / "refine-status.json"
    updated = False
    
    for entry in history:
        if entry.get("status") in ("running", "resuming"):
            # lock 파일에서 PID 확인
            if lock_path.exists():
                lock_data = json.loads(lock_path.read_text())
                pid = lock_data.get("pid")
                if pid and not _is_process_alive(pid):
                    entry["status"] = "failed"
                    updated = True
            else:
                # lock 없으면 프로세스 없음
                # status 파일 확인
                if status_path.exists():
                    st = json.loads(status_path.read_text())
                    if st.get("status") in ("failed", "completed", "cancelled"):
                        entry["status"] = st["status"]
                        updated = True
                else:
                    entry["status"] = "failed"
                    updated = True
    
    if updated:
        history_path.write_text(json.dumps(history, ensure_ascii=False, indent=2))
    
    self.send_api_response({"history": history})
```

## 수정 2: 정제 이력 삭제 API + UI

### 백엔드
```
DELETE /api/wiki/refine/history/{id}
```
- refine-history.json에서 해당 id 항목 제거
- 성공: `{"status": "ok", "id": "삭제된 ID"}`

### 프론트엔드
**파일**: `/home/jay/workspace/dashboard/components/InsuWikiView.js`

정제 이력 각 항목 우측에 ✕ 삭제 버튼 추가 (블로그 히스토리와 동일 패턴)

## 수정 3: 정제 중 처리 내용 미리보기 (currentPreview)

### 백엔드
**파일**: `/home/jay/projects/insuwiki/scripts/kakao_knowledge/knowledge_extractor_v2.py`

배치 처리 루프에서 refine-status.json에 currentPreview 필드 추가:
```python
if progress_file:
    preview_messages = batch_threads[0].messages[:3]
    preview_text = " | ".join([f"{m.sender}: {m.content[:30]}" for m in preview_messages])
    _write_progress(progress_file, {
        ...기존 필드...
        "currentPreview": preview_text,
    })
```

### 프론트엔드
```jsx
{refineStatus.currentPreview && refineStatus.status === 'running' && (
    <p className="text-xs text-slate-400 dark:text-slate-500 mt-1 truncate">
        {refineStatus.currentPreview}
    </p>
)}
```

## 검증 시나리오
1. 프로세스 kill 후 → 정제 이력에 "실패"로 자동 갱신
2. 이력 ✕ 클릭 → 해당 항목 삭제
3. 정제 진행 중 → progress bar 아래에 처리 중인 대화 미리보기 표시
4. 기존 기능 회귀 없음