[feat] agent: 카드번호 기반 Q-table 마이그레이션 + 학습 리셋 스크립트

카탈로그 카드 추가/삭제/재정렬 시 action_id 가 밀려도 학습이 카드를 따라가도록,
Q-table 버전에 카탈로그 스냅샷(카드번호)을 저장하고 카드번호로 리맵한다.

- q_table_versions.action_cards JSONB 신설(카드번호 목록, index=action_id).
  models.py + init.sql(DDL·ALTER) + 로컬 DB ALTER.
- 버전 생성(get_or_create/warm_start/migrate)이 action_cards 저장.
- migrate_active_version_dim: 옛 action_cards ↔ 새 카탈로그를 카드번호로 리맵
  (중간 삽입/삭제 보정, 사라진 카드 버림, 새 카드 fresh). 레거시(스냅샷 없음)는 위치 폴백.
  version_name 은 vid 접미로 유니크.
- model_store.load: card_list 계산 → 차원변경 OR 동일차원 내용변경 시 마이그레이션,
  레거시 버전 action_cards backfill(set_version_action_cards).
- tools/reset_learning.py: learning 스키마만 비우는 리셋(카드·협상 데이터 보존),
  --company/--yes 옵션. 카탈로그 바꾸고 학습 처음부터 할 때 사용.
- 테스트: 중간 카드 삭제 시 카드번호 리맵으로 학습 보존 검증. agent 100/100.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
HyeongBaeYangandClaude Fable 5 committed 2026-07-08 15:19:55 +09:00
1 parent 7b7f606ace
commit b368a19f79
6 files changed
+154 -25

No files matched your search

+26
View File
@@ -92,6 +92,32 @@ async def test_catalog_dim_change_migrates_preserving_learning(db_engine):
assert str(active.version_id) == str(new_vid) and active.action_space_size == 11
@pytest.mark.asyncio
async def test_migration_remaps_by_card_number(db_engine):
"""카드번호 기반 마이그레이션 — 중간 카드 삭제로 action_id 가 밀려도 학습이 카드를 따라간다."""
import uuid as _uuid
cid = str(_uuid.uuid4())
repo = LearningRepository(cid)
# A=3, action_cards=[NGC-001, NGC-002, NGC-003]. (5,2)=0.9 는 NGC-003 의 학습.
vid = await repo.get_or_create_active_version(
state_space_size=162, action_space_size=3, learning_rate=0.1, discount_factor=0.95,
scope=2, version_name="v_cards3", action_cards=["NGC-001", "NGC-002", "NGC-003"])
await repo.upsert_cell(vid, state_index=5, action_id=2, q_value=0.9, count=4) # NGC-003
await repo.upsert_cell(vid, state_index=5, action_id=0, q_value=0.3, count=2) # NGC-001
_, active = await repo.read(lambda s: repo.get_active_version(s))
# 새 카탈로그: 중간 NGC-002 제거 → [NGC-001, NGC-003] (A=2). NGC-003: old action 2 → new action 1.
new_vid = await repo.migrate_active_version_dim(
old_version=active, state_space_size=162, action_space_size=2,
learning_rate=0.1, discount_factor=0.95, action_cards=["NGC-001", "NGC-003"])
assert new_vid is not None
qcells, _ = await repo.load_cells(new_vid)
qmap = {(st, a): q for st, a, q in qcells}
assert qmap.get((5, 1)) == 0.9 # NGC-003 학습이 새 action_id 1 로 따라감(밀림 보정)
assert qmap.get((5, 0)) == 0.3 # NGC-001 은 그대로 action_id 0
assert (5, 2) not in qmap # 삭제된 NGC-002 자리 없음
@pytest.mark.asyncio
async def test_dimension_mismatch_falls_back_to_heuristic(db_engine):
await _seed_base(S=162, A=9)