1---2name: working-with-anki3description: AnkiConnect 연결, 카드/덱/모델 조회, 학습 데이터 복제, 백업/롤백, 어려운 카드 탐지, 커스텀 config 액션 등 Anki와의 모든 상호작용을 다룬다. Triggers: "AnkiConnect 연결", "test 프로필", "ease factor 복제", "카드 정보 조회", "학습 데이터", "AnkiConnect API", "Anki 프로필", "카드 모델", "어려운 카드", "난이도 탐지", "difficulty", "백업", "롤백", "분할 적용", "scheduling", "스케줄링", "getConfig", "setConfig", "커스텀 액션". Covers AnkiConnect API wrapper, scheduling clone, backup/rollback, difficulty detection, and custom config actions.4---56# AnkiConnect 작업78## 안전 규칙 (최우선)910- **반드시 `test` 프로필에서만 작업**: `open -a Anki --args -p test`11- 기본 프로필 접근 **절대 금지** -- 실제 학습 데이터 보호12- `--apply` 없이 항상 미리보기 먼저 확인1314## AnkiConnect 기본 정보1516- **주소**: `$ANKI_CONNECT_URL` (미설정 시 `http://localhost:8765` 폴백, MiniPC headless Anki는 Tailscale 전용)17- **애드온 코드**: 205549215918- **대상 모델**: `KaTeX and Markdown Cloze` (필드: Text, Back Extra)19- **API 버전**: 62021## API 래퍼 (packages/core/src/anki/)2223| 파일 | 역할 | 주요 export |24|------|------|-------------|25| `client.ts` | AnkiConnect 저수준 래퍼 + 고수준 함수 | `ankiConnect()`, `getVersion()`, `getProfiles()`, `getDeckNames()`, `getModelNames()`, `getModelFieldNames()`, `findNotes()`, `getNotesInfo()`, `updateNoteFields()`, `addNote()`, `addNotes()`, `addTags()`, `removeTags()`, `deleteNotes()`, `sync()`, `getConfig()`, `setConfig()` |26| `operations.ts` | 카드 CRUD, 분할 적용 (nid 승계) | `getDeckNotes()`, `getNoteById()`, `updateMainCard()`, `addSplitCards()`, `applySplitResult()`, `extractTextField()`, `extractTags()` |27| `backup.ts` | 분할 전 백업, 롤백 (날짜별 파일, 원자적 쓰기) | `preBackup()`, `updateBackupWithCreatedNotes()`, `rollback()`, `listBackups()`, `getLatestBackupId()` |28| `scheduling.ts` | 학습 데이터 조회/복제 (6개 함수) | `getCardSchedulingInfo()`, `getFullCardInfo()`, `findCardsByNote()`, `setCardScheduling()`, `copySchedulingToNewCards()`, `cloneSchedulingAfterSplit()` |29| `difficulty.ts` | 학습 통계 기반 어려운 카드 탐지 | `computeDifficultyScore()`, `getDifficultyReasons()`, `getDifficultCards()`, `DEFAULT_THRESHOLDS` |3031### 주요 API 호출 패턴3233```typescript34// 고수준 래퍼 (client.ts) -- ankiConnect() 직접 호출 대신 이것을 사용35const decks = await getDeckNames();36const notes = await findNotes('deck:"덱이름"');37const infos = await getNotesInfo([noteId]);38await addTags([noteId], 'tag1 tag2');39await sync();4041// 커스텀 config (miniPC 확장 전용)42const val = await getConfig<string>('some-key');43await setConfig('some-key', { foo: 'bar' });44```4546## 학습 데이터 복제 (scheduling.ts)4748- **`cloneSchedulingAfterSplit(originalNoteId, newCardIds)`**: 메인 진입점49 - 원본 노트의 카드를 찾아 스케줄링 정보 조회50 - **리뷰 카드(type=2)이고 reps>0인 경우에만** ease factor 복제51 - 새 카드/learning 카드는 복제 건너뜀52- **복제 가능**: ease factor (`setEaseFactors` API)53- **복제 불가**: interval, due -- AnkiConnect API 제한5455## 어려운 카드 탐지 (difficulty.ts)5657- **`getDifficultCards(deckName, thresholds?)`**: 덱 내 어려운 카드를 난이도순으로 반환58 - 100개 배치로 `getFullCardInfo` 호출, noteId별 중복 제거(최악 성적 카드 유지)59 - `DEFAULT_THRESHOLDS`: minLapses=3, maxEaseFactor=2100, minReps=560- **`computeDifficultyScore(lapses, easeFactor, interval, reps)`**: 0-100 복합 점수61 - lapses 50% + ease factor 30% + interval 20% 가중치6263## 백업/롤백 (backup.ts)6465- **저장 위치**: `$ANKI_SPLITTER_BACKUP_DIR` 또는 `output/backups/`66- **파일 형식**: `backup-{YYYY-MM-DD}.json` (날짜별, 하루에 여러 엔트리)67- **버전 포맷**: `BackupFile { version: 1, entries: BackupEntry[] }`68- **저장 내용**: deckName, originalNoteId, 원본 fields/tags/modelName, createdNoteIds69- **원자적 쓰기**: `atomicWriteFileSync()` + `withFileMutex()` (in-process 직렬화)70- **손상 파일 격리**: `quarantineCorruptedBackup()` -- `.corrupt-{timestamp}` 접미사71- **`preBackup(deckName, noteId)`**: 분할 적용 전 원본 상태 미리 저장72- **`rollback(backupId)`**: 생성된 서브 카드 삭제 + 원본 필드/태그 복원 + 스케줄링 경고73 - 태그 완전 복원: 현재 태그 전체 제거 후 백업된 태그 재적용74 - 반환에 `warning: "카드 스케줄링 메타데이터는 자동 복원되지 않습니다."` 포함7576## 커스텀 Config 액션 (getConfig/setConfig)7778- 공식 AnkiConnect에는 없는 miniPC 커스텀 확장79- `mapUnsupportedConfigActionError()`: 미지원 에러를 감지하여 `UNSUPPORTED_REMOTE_CONFIG_ACTION` 코드로 매핑80- 미지원 패턴: "unsupported action", "unknown action", "action not found", 또는 bare action name8182## 연결 확인8384```bash85# 직접 테스트86curl -s "${ANKI_CONNECT_URL:-http://localhost:8765}" -X POST -d '{"action":"deckNames","version":6}' | python3 -m json.tool87```8889## 자주 발생하는 문제9091- **연결 실패**: Anki가 실행 중인지 + AnkiConnect 애드온 활성화 확인92- **프로필 혼동**: 반드시 `open -a Anki --args -p test`로 실행93- **cardsInfo vs notesInfo**: 학습 데이터는 `cardsInfo`에서 조회94- **getConfig/setConfig 실패**: miniPC 커스텀 확장 미설치9596## 상세 참조9798- `references/ankiconnect-api.md` -- API 래퍼 상세, 모든 고수준 함수99- `references/scheduling.md` -- 학습 데이터 복제 함수별 상세100- `references/backup-system.md` -- 백업 파일 구조, 원자적 쓰기, 격리101- `references/troubleshooting.md` -- 연결 문제, 프로필 안전, 커스텀 config 에러