Compare commits

..

266 Commits

Author SHA1 Message Date
69e4240039 [fix] negodata: 협상카드 성공률에 회사 스코프 누락 — 타사 견적 이력이 섞이던 문제
card_success_map 이 quotations 조인 없이 DB 전체 chats 를 카드별로 집계해,
다른 회사 견적에서 쓰인 이력이 내 카드 목록의 used_count/success_rate 로 잡혔다.
통계 화면 '협상카드 성공률 TOP 5' 와 견적 생성 시 카드 추천 순위가 모두 이 값을 쓴다.

- quotations 조인 + 작성자 회사(users.company_id) 스코프 추가
- 소프트 삭제분 제외(sessions.deleted / quotations.deleted 필터 누락분 보강)
2026-08-13 14:06:34 +09:00
8112394aef [feat] landing: 데모 영상 3종 실화면 재촬영(라이브 협상·견적생성·협상결과, 줌인 연출) + 도입문의·데모요청 폼 mailto 임시연결 2026-08-12 17:28:40 +09:00
1a859b198c [fix] negodata: 직접 낙찰 낙찰가가 계약가 대신 거부가로 표시되던 문제
세션 목록 API(queries.py)가 SessionData 를 필드별로 수동 조립하는데 contract_price 를
빠뜨려, 담당자가 입력한 계약가가 응답에 안 실렸다(항상 null). 그 결과 프론트가 계약가를
못 읽고 제출가(거부가/투찰가)로 폴백해, 14,000,000 으로 직접 낙찰해도 낙찰가에 거부가가
떴다. 프로토콜·ORM·컬럼은 이미 있었으나 이 직렬화 한 줄이 누락돼 있었다.

- queries.py: SessionData 조립에 contract_price=r.contract_price 추가
- ResultSummaryBand: 스펙트럼 낙찰점도 제출가(awardPrice)가 아니라 계약가(contractPrice)로 찍어
  레일 낙찰가와 일치시킴

검증: 세션 API 응답에 contract_price 실제 내려오는 것 확인(화면 아닌 응답 직접). negodata 테스트 통과.
2026-08-12 11:02:10 +09:00
d33648327c [fix] landing: 가이드 협상 탭이 참조하는 negotiation.mp4 복원 — v2 머지로 참조가 되살아남 2026-08-12 10:54:46 +09:00
dce2c42d74 Merge remote-tracking branch 'origin/feat/landing-v2' into feature/landing 2026-08-12 10:53:38 +09:00
0e4ddf0f43 [feat] negodata: 낙찰 방식 구분(자동/직접) + 계약가·사유 저장 위치 재정리
직접 낙찰(담당자 오프라인 계약가)을 AI 자동 낙찰과 통계·화면에서 구분하기 위해
낙찰 방식을 전용 컬럼으로 박고, 계약가·사유를 성격대로 배치했다. 처음엔 계약가·사유를
sessions.custom.offline_award(JSONB) 한 뭉치에 넣었으나 성격이 갈려 정리한다.

DB (postgres-init: init.sql 정본 + alters/2026-08-11-award-type.sql 보정, 멱등)
- quotations.award_type SMALLINT — 낙찰 방식(1=자동/2=직접). 통계 조회·집계 축이라 컬럼
- quotations.custom JSONB — 직접 낙찰 사유·처리자·시각(award={reason,by,at}). 표시·감사용
- sessions.contract_price BIGINT — 직접 낙찰 계약가. bid_price/reject_price 와 같은 협력사
  가격 축이라 세션에. 자동 낙찰은 NULL(투찰가가 곧 계약가)
- 기존 offline_award(JSONB) 데이터를 컬럼·견적 custom 으로 이관 후 키 제거

backend
- 자동 낙찰(close_and_decide)=AUTO, 직접 낙찰(claim_for_award)=MANUAL 로 award_type 기록
- 직접 낙찰: 계약가→세션 contract_price, 사유·처리자·시각→견적 custom.award (한 트랜잭션)
- 통계 계약가 = coalesce(contract_price, bid_price) — JSONB 캐스팅 제거(컬럼끼리, 인덱스·타입 안전)
- QuotationData 에 award_type·custom, SessionData 에 contract_price 노출
- 안 쓰게 된 merge_session_custom 제거

front
- 견적 상세 결과 밴드에 '직접 낙찰' 배지 + 낙찰 사유 표시(견적 custom.award.reason).
  거부·미참여 낙찰은 이미 직접 낙찰을 함의하므로 '— 낙찰' 꼬리를 떼 중복 표기 제거
- 목록 결과 배지에 '낙찰(직접)' 표기 — AI 자동낙찰과 한눈에 구분
- offlineAward()→directAwardPrice()/awardMeta() 로 교체(세션 컬럼·견적 custom 에서 읽음)
- 협상현황 표: 부가정보(회사 필드)와 의견(custom.opinion)을 별도 컬럼으로 분리
- 미응찰 건 레일 4번 칸 라벨 '최저 투찰가'→'결과'(투찰 없을 때)

테스트: negodata 110건 통과. 프론트 tsc+eslint 통과. dev DB 적용·화면 확인.
2026-08-12 10:25:26 +09:00
6eb4dc26f8 [feat] negosium·negodata: 협상 거부 흐름 정리 + 오프라인 계약가 낙찰 + 견적 상세 리모델링
협상 불가 사유를 내면 500 이 나고, 거부 폼은 목록·채팅이 따로 놀았으며,
제출한 내용을 다시 볼 방법이 없었다. 결렬 건은 협력사가 낸 거부가를 계약가로
간주해 낙찰시켜 절감 통계가 음수로 뒤집힐 수 있었고, 견적 상세는 판정 가격이
세 곳에 흩어져 대화 탭에선 아예 보이지 않았다.

agent
- 결렬 종료 로깅 크래시 수정 — _log 를 action_id 기반으로 되돌리고 선택 근거
  (Q·UCB·방문수)는 decision/policy 가 있을 때만 채운다. 종료 행은 카드 선택이
  없고 policy.update 뒤라 값을 넣으면 학습 화면 집계가 오염된다

negosium
- 거부 폼을 목록·채팅 공용 컴포넌트 하나로 통일(사유 3종 + 공급 희망가·의견 선택)
- 거부 사유 열람 — 목록에 '거부 사유 보기'(부가정보 보기와 같은 규격), 채팅
  재진입 시 대화 끝에 거부 내역 카드. 목록·채팅 init 응답에 reject_reason·reject_price 추가
- 자유 입력 거부("협상 포기합니다")가 사유 NULL 로 저장되던 문제 수정 — 폼 마커가
  없으면 원문을 사유로 쓰고, 문장 속 숫자를 희망가로 오인하지 않는다
- koreanNumber 를 전역 lib 으로 이동(공용 폼이 쓴다)

negodata
- 직접 낙찰에 계약가 입력 — 결렬·미응찰 건을 오프라인으로 다시 협상한 결과를
  담당자가 확정해 넣는다. 후보는 초청 협력사 전부(가격 미제출도 포함),
  계약가는 sessions.custom.offline_award 에 근거·작성자·시각과 함께 남긴다
- 통계 계약가 = 담당자 확정가 우선, 없으면 투찰가. 거부가를 계약가로 치던 파생 제거.
  KPI 에 오프라인 반영 건수 추가
- 견적 상세 리모델링 — 가격 레일(앵커링가/투찰현황 · 목표가 · 타결 상한가 · 결과가)을
  시트에 고정해 접힘·탭 전환에도 남기고, 스펙트럼에 타결 판정선과 구간색 추가.
  라벨은 폭을 실측해 두 레인으로 배치(겹침 불가). 상품·마감시각 등 전 행 동일 컬럼 제거,
  협상현황에 부가정보 노출, 1:1 은 협력사·세션상태를 결과 밴드로 올림

테스트: negosium 58 · negodata 110 통과. 프론트 빌드/린트 통과.
2026-08-12 10:25:26 +09:00
d02ba20520 [chore] landing: 미사용 파일 정리 — 히어로 시안 2종(console·glassmorphic)·전용 미디어, gif 재생으로 회귀한 negotiation mp4/jpg, 구 히어로 negotiation_annotated 삭제 2026-08-12 10:17:50 +09:00
ac6366a898 [feat] negosium·negodata: 협상 진행 중 거부 + 결렬 건 최종 제출가로 직접 낙찰
협력사가 단종·품절을 대화 도중 알아채도 봇의 결렬 선언을 기다려야 했고,
거부로 끝난 협상은 가격을 남겨도 낙찰 후보에서 빠져 계약으로 이어지지 않았다.

negosium
- 채팅 액션바에 협상 거부 진입점 — 대화가 끝나지 않고 입력을 기다리는 동안만 노출,
  주 CTA 와 붙지 않게 넓은 화면은 우측 끝 고정·좁은 화면은 wrap
- 거부 팝업은 목록 거부 팝업과 같은 어휘·규격, 대화 중이라 공급 희망 가격·의견을 더 받는다
- /reject 에 reject_price·opinion 추가 — sessions.reject_price 저장, 의견은 custom 병합
- 화면 문구 '거절' → '거부' 통일 (버튼·배지·탭·토스트·안내 팝업)

negodata
- 개찰 견적 직접 낙찰 후보 = 가격을 써낸 세션 — 투찰한 협상완료 + 공급 희망가를 남긴 협상거부
- 계약가 파생 _award_price/awardPrice — coalesce(투찰가, 거부 시 공급 희망가)
- 통계 낙찰 세션 조인도 같은 기준 — 안 고치면 거부가로 낙찰한 건이 절감 집계에서 빠진다
- 세션 상태 탭 라벨 '거절사유/거절가격/거절배송방식' → '거부…'

자동 마감 판정(close_and_decide)은 그대로 — 자동 낙찰은 투찰가만 본다.
2026-08-11 10:34:07 +09:00
f1e924931f [fix] negodata: 협상 학습 화면 정리 — 내부 점수 제거·카드탭 사용 현황 전환·메뉴 개발자 전용
- 관측 사실 기준으로 재구성: 내부 점수 제거, 앵커링 % 표기, 탭 URL 분리
- 카드탭을 성과 지표에서 사용 현황으로 전환, 지표 설명 말풍선·카드 상세 링크
- 사이드바 devOnly + 라우트 가드로 개발자 전용 처리
2026-08-11 08:49:15 +09:00
84090433ab [docs] landing: 히어로 CTA 문구 변경 결정 기록 추가
경영진 보고용 문안을 코드 옆에 남긴다. 최초 지시안(협상 예시 보기 · 직접 체험)을
검토 과정에서 수정한 사유와, 최종안(협상 예시 체험 · 실제 데모 받기)이 지시 의도를
어떻게 유지했는지를 기록한다. 이후 CTA 문구 결정 시 참조할 원칙도 남긴다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-10 17:09:07 +09:00
e1e519c20c [fix] negosium/backend·front: 마감된 협상이 '협상 대기'로 보이던 문제 + 종료 협상 결과 보기
- 목록 상태를 파생 계산(_effective_status): 견적 마감/마감시간 경과 + 협상생성(1) → 미참여(4).
  표시·상태필터·기본정렬 그룹에 같이 적용해 탭·KPI 카운트가 배지와 어긋나지 않게 함(DB 쓰기 없음).
  마감 일괄정리 이후 생긴 세션이 '협상 대기'에 남아 눌러도 마감 에러가 나던 헛클릭 제거.
- 미참여·협상거부 세션도 채팅 init 허용 → '결과 보기'로 지난 대화 열람. 대화 재개는 send 가
  협상중만 허용해 읽기 전용. 열 게 없는 건은 목록 has_chat(대화 이력 유무)으로 버튼 자체를 감춤.
- 종료 건은 참여 API 를 타지 않고 채팅으로 직행, 채팅 액션바는 목록 복귀만 남겨 입력 잠금.
- chain_max_round 가 단일컬럼 select 결과를 행 튜플로 읽어 매 목록 호출마다 예외 → 체인 최대차수
  0 → 옛 라운드에도 재협상 요청이 뜨고 통과되던 버그 수정(+ABC 에 중복된 죽은 구현 제거).
2026-08-10 13:57:30 +09:00
e816bfbba4 [feat] negodata: 협상 학습 메뉴 신설 — 카드별 학습 성적 · 앵커링 현황/조정 이력
agent(협상카드 강화학습)와 anchoring(구간별 자동 조정)이 쌓아온 결과를 볼 수 있는 화면이
없어 DB 를 직접 열어야 확인이 됐다. 두 축은 성격이 달라 한 화면에 섞지 않고 탭으로 나눈다.

백엔드(읽기 전용 — 값의 주인은 agent·anchoring):
- crud/learning_crud.py: learning·anchoring 스키마 경량 조회(negodata ORM 미매핑 테이블)
- services/learning_service.py + router/v1/learning: /v1/learning/cards · /anchoring

프론트:
- 사이드바 '협상 학습'(/learning) 추가, 탭 = 협상카드 | 앵커링
- 협상카드: 학습 협상·기록·타결 비율 요약 + 카드별 사용/타결/평균 점수(0 기준 좌우 막대)
- 앵커링: 구간별 현재 인하폭 + 조정 이력(표본·성공률·값 변화)

검증: 로컬 실협상 3건으로 데이터 생성 후 두 탭 실화면 확인, tsc·eslint 통과.
2026-08-10 13:24:32 +09:00
37fa65707f [fix] postgres-init: 카드 시드를 운영 현행에 맞춤 — 중반 카드 앵커가 인용 + 목표가 카드 종결 전용
협상 중반에 목표가를 부르면 이후 올려줄 금액이 없어져 남은 카드가 같은 값만 반복한다.
제안은 앵커가 → 중간가 → 목표가로 수렴해야 하므로 시드를 그 순서에 맞춘다.

- NGC-010 향후 거래 연계 · NGC-011 양보 가치 강조: 제시 금액 {target_price} → {anchor_price}
- tactic: WC-01 종결 전용 추가, WC-04 min_round 2 → 종결 전용, WC-05 종결 전용 + min_round 2,
  WC-02 min_round 2 추가 (WC-03 종전 유지)

init-data.sql · dbeaver/4_cards_reset_dbeaver.sql 동일 반영.
검증: 임시 DB 에 1_init + 4_cards_reset 적용 후 tactic·변수 확인.
2026-08-10 11:41:25 +09:00
86d50c1291 [fix] negodata/front: 윈도우 모달 한글 깨짐 — 컨테이너 font-mono 제거 + 모노 스택 한글 폴백
모달 래퍼·폼에 걸린 font-mono 가 내부 한글까지 모노 스택으로 태웠다. JetBrains Mono·Consolas 에
한글 글리프가 없어 Windows 에서 굴림으로 떨어졌다(맥은 뒤쪽 폰트가 한글을 커버해 정상으로 보임).

- 한글이 들어가는 컨테이너 15곳에서 font-mono 제거 — 숫자 정렬이 필요한 칸은 유지
- --font-mono 스택 끝에 'Noto Sans KR' 추가, index.html 에 구글폰트 링크
  (self-host Pretendard 가 못 뜰 때의 안전망 — 정상 경로는 동일 도메인 Pretendard)
2026-08-10 09:49:26 +09:00
436384fa36 [fix] agent: 중간값 카드가 목표가로 제시되던 문제 — 제안가 산식 일원화·절충 카드 목표가 이상 미발동·적용 대기 와일드카드 차단
원인 두 겹.
1) 절충 계열 가드가 타결 상한만 봐서, 중간가((앵커+목표)/2)처럼 구조상 목표가를 넘지 못하는
   값은 항상 통과했다. 앵커가 목표가에 붙으면 그 값이 곧 목표가라 목표가로 제시된다.
   → 파생 제안값이 목표가 이상이면 미발동(compute_offer). 목표가 카드는 영향 없음.
2) 제안가 확정 시 계산에 쓴 당사 직전가를 남기지 않고 그 자리를 제안가로 덮은 뒤, 멘트 렌더가
   갱신된 값으로 재계산해 "당사 A 와 귀사 B 의 절반은 A" 자기모순이 났다. 직전 커밋은 갱신을
   렌더 뒤로 미뤘으나 와일드카드는 렌더 지점이 더 뒤라 그대로 남아 있었다.
   → Offer/record_offer 로 금액과 재료(당사 직전가·협력사 제시가)를 함께 박제하고, vars_for 는
     재계산 대신 그 기록을 읽는다. 렌더 순서에 의존하지 않으므로 직전 커밋의 순서 조정은 제거.
   → 산식은 tactics.OFFER_VARIABLES 한 곳만 남기고 chat_engine 의 복사본 삭제.

곁들여: 카드 설정에서 '적용 대기'로 꺼둔 와일드카드가 견적에 담겨 있으면 그대로 발동하던 것을
차단(wild_cards.available 조건). 테이블 정의에 빠져 있던 컬럼도 보강.

검증: 단위/E2E 35건, 실협상 8시나리오(멘트 토큰 노출·절반 산술 정합·카드 중복·제안가 역행·
상한 초과 자동 검사) 통과.
2026-08-10 09:45:30 +09:00
a405e00bf9 [fix] agent·negodata/front: 중간값 멘트 모순 수정(갑 포지션 갱신을 치환 뒤로·원값 인용가 반올림 제외·1% 카드 역행 금지) + 계정 전환 쿼리캐시 초기화·ChatTab 카드 렌더 정리 + font-mono 한글 폴백(Windows 한글깨짐) 2026-08-07 15:48:48 +09:00
민헌
7dc925bcd4 docs(lps): 2026-08-07 세션 기록 — IP 로테이션 검수·네이버 UA 규명·결과 상태 정의
다음 세션이 이어받을 수 있게 남긴다.

- docs/2026-08-07-session-notes.md 신규: 세 덩어리(IP 로테이션 결함 6개, 네이버 컨테이너 차단이
  실은 UA 플랫폼 토큰이었다는 규명, '못 봤다'를 '없다'고 말하던 문제의 4단계 해결)와
  **되돌리기 전에 볼 근거**(예산을 IP 에 묶는 이유, 태우지 않는 판정, 임대 없이 크롤 금지,
  UA 는 플랫폼만 바꾸고 버전은 실값, 상태는 하나로 저장하고 표시에서 접기).
- 미해결: 쿠팡 간헐 차단 — IP 에 따라 갈리므로 환경이 아니라 평판. 쿠팡만 gate(해외 IP)를 쓰는 게
  유력한 원인이고, 네이버처럼 kr_host 로 바꾸는 검증이 중단된 상태.
- 다음 할 일 5가지(운영 마이그레이션 → 쿠팡 KR 검증 → 예산 튜닝 → AI 매칭 정확도 → admin 잡목록).
- 주의사항 5가지: **마이그레이션→코드 순서 강제**(역순이면 최저가 조회가 통째로 실패, 재현 확인),
  워커 켜고 pytest 하면 test_job_queue 3개 깨짐, JS 지문 위장은 patchright 가 막아 불가,
  로컬 negodata-front 는 node_modules 가 named volume 이라 재빌드로 의존성이 안 늘어남.
- README 문서 표에 링크 추가.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 15:02:25 +09:00
민헌
0c6c431f0a Merge branch 'feat/source-state'
'못 봤다'를 '없다'고 말하던 문제를 4단계로 해결한다. 상태를 하나로 정의·저장하고 표시 단계에서
보는 사람에 맞게 접는다 — 운영자(lps-admin)는 7상태 그대로, 실사용자(negodata)는 셋으로.

1) SourceState 7상태 — 어댑터가 이미 알던 '봤다/못 봤다' 구분을 핸들러가 버리지 않게 한다
2) price_history.sources/partial — 그 구분을 담을 자리(by_mall 은 가격 있는 몰만 담는다)
3) lps-admin — 몰별 상태·차단 마커·조치 힌트(blocked=자동회복 vs env_blocked=사람이 고쳐야)
4) negodata — '–'(확인했고 없음)와 '확인 못함'(못 봄)을 가르고, 후자는 재검색 대상으로 남긴다

⚠️ 배포 순서: 마이그레이션 2개(6_lps_2026-08_dbeaver.sql · 2026-08-07-iilp-source-state.sql)를
   **코드보다 먼저** 적용해야 한다. negodata 가 ORM 전체 엔티티를 조회하므로 컬럼이 없으면
   최저가 조회가 통째로 실패한다(재현 확인). 역순(마이그레이션 후 옛 코드)은 안전하다.

실환경 검증: 실제 크롤 결과로 네이버 no_match→'–', 쿠팡 env_blocked→'확인 못함' 확인.
잡 DEAD 0건, 네거티브 캐시 미오염, 포트 소각 8/100 에서 차단.
2026-08-07 14:46:02 +09:00
민헌
67612b477c Merge remote-tracking branch 'origin/main' into feat/source-state 2026-08-07 14:36:41 +09:00
민헌
6e401615eb fix(negodata): '일부 확인 못함'을 결과 열에도 표시 — 몰 칸만 고쳐 놓친 부분
실화면 확인에서 드러났다. 쿠팡 칸은 '확인 못함'으로 잘 나오는데 결과 열이 '변동 없음'이었다.
못 본 몰이 있는데 '변동 없음'이라 하면 사용자는 여전히 "이 가격이 최종이구나"로 읽는다 —
그 몰에 더 싼 값이 있었을 수 있어 이 결과는 최종이 아니다.

docs/result-states.md 3-2절에 이미 정의해 둔 표기인데(| 일부 확인 못함 | partial |)
몰 칸만 구현하고 결과 열을 빠뜨렸다.

- 결과 열: unconfirmed 가 있으면 '일부 확인 못함'(툴팁에 어느 몰인지). '탐색 성공'(더 싸게 찾음)은
  그대로 우선한다 — 값이 실제로 갱신된 건 사실이므로.
- 재검색 대상: 못 본 몰이 있으면 **완료로 치지 않는다**. 가격이 갱신됐어도 다음 '다시 검색'의
  기본 대상으로 남긴다 — 그게 '다시 시도할 이유가 생긴다'의 실제 동작이다.

tsc 오류 없음. 실데이터로 확인(섬유유연제: 네이버 4,820 matched · 쿠팡 blocked/env_blocked).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 14:29:15 +09:00
11b29b45e3 [fix] negodata·frontend·backend: 회사별 브랜드 색(primary_color) 기능 전면 제거 — 편집 UI 없이 값만 남아 적용되던 구현 정리(포털·메일·사이드바 색 솔루션 고정, 시드·응답 필드 제거) 2026-08-07 13:45:23 +09:00
4ed23c56b8 [feat] negodata: IMK 요청 일괄 반영 — 조회 게이팅·카드 매칭·부가세 통일 모드·기준가 이원 표기·마스킹 해제·이미지 일괄 업로드·초청메일 브랜딩·반응형 정리 2026-08-07 11:57:06 +09:00
민헌
6ddb2c4504 refactor(postgres-init): LPS 2026-08 마이그레이션 6_·7_ 통합 + 주석 스타일 정리
같은 DB(lps_db)의 같은 시기 추가분이 두 파일로 갈려 있어 순서·누락을 신경 써야 했다.
6_ 하나로 합친다(7_ 삭제) — 실행이 한 번이면 '어디까지 돌렸더라'를 기억할 일이 없다.

- 주석을 3_lps_dbeaver.sql 스타일로 통일: 객체 위 한 줄 설명 + 컬럼 인라인 주석 정렬.
  기존 6_·7_ 의 긴 배경 산문은 걷어냈다 — 배경은 lps/docs/result-states.md 가 소스고,
  이 파일은 '무엇을 만드는가'만 답하면 된다.
- 내용은 그대로: proxy_port(+LRU 인덱스), price_history 신뢰 신호 2·배송 3,
  몰별 확인 상태 sources/partial(+부분 인덱스), 적용 확인 SELECT.

검증: 기존 lps_db 재실행(멱등 — NOTICE 만) + 빈 DB 에 3_ → 6_ 신규 설치 후
price_history 신규 7컬럼·테이블 6종 전부 확인. 테스트 DB 는 정리.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 11:25:22 +09:00
민헌
95086cb3f6 feat(negodata): 4단계 — 사용자 화면에서 '없음'과 '확인 못함'을 구분
마지막 단계. 차단당해 못 본 몰이 화면에서 '–'(없음)로 보여 사용자가 "쿠팡엔 더 싼 게 없구나"로
오해하던 문제를 끝낸다. 안 본 걸 없다고 말하지 않는다.

사용자 화면은 **셋으로 접는다**(lps/docs/result-states.md 3-2절). 할 수 있는 행동이
'쓴다/다시 시도/넘어간다' 뿐이라, 원인이 달라도 다음 행동이 같으면 같은 표기다:
  matched                            → 가격
  no_match · empty                   → '–'        (확인했고 없었다)
  blocked · env_blocked · unavailable → '확인 못함' (못 봤다)
운영자 화면(lps-admin)은 같은 데이터로 7상태를 그대로 본다 — 목적이 진단이라 접지 않는다.

체인 전체를 이었다:
- postgres-init/alters/2026-08-07-iilp-source-state.sql — item_internet_lowest_prices 에
  sources/partial 추가(멱등, **운영 적용 필요**)
- models.py / lps_sync_crud 읽기 계약 / lps_sync_service 미러링 / LowestPriceEntry 프로토콜
- orval 재생성(ORVAL_INPUT 으로 저장 스펙에서 — 서버 없이). 생성 diff 는 새 필드만.
- PriceUpdateModal: 가격이 없는 몰이 '못 본 몰'이면 '–' 대신 '확인 못함'.

partial 은 LPS 가 판단해 내려준 사실을 그대로 쓴다 — 화면이 '어떤 상태가 확인된 것인가'를
다시 판정하면 상태 정의가 LPS 와 negodata 두 곳으로 흩어진다.

E2E 검증(실 DB 2시나리오): by_mall 은 둘 다 naver 뿐인데 쿠팡 칸이 '확인 못함'(차단) / '–'(0건)
으로 갈린다. tsc 오류 없음(기존 xlsx 미설치 오류는 무관). negodata 97 · lps 292 passed.

> 폴더 관례상 negodata 는 인수인계 대상이나, 사용자 요청으로 이번 건도 예외 적용.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 11:21:56 +09:00
민헌
d1d1ed6ec5 feat(lps-admin): 3단계 — 몰별 확인 상태를 운영 화면에 노출
2단계에서 저장한 sources/partial 을 운영자가 볼 수 있게 한다. 운영자 목적은 **진단**이라
상태를 접지 않는다 — blocked(IP 회전으로 자동 회복)와 env_blocked(사람이 환경·설정을 고쳐야 함)를
뭉뚱그리면 회복될 일에 매달리거나 손봐야 할 설정을 방치하게 된다.

API
- /v1/lps/products, /v1/lps/products/{code}/history 둘 다 sources·partial 을 싣는다.
- 이력은 **시점마다** 싣는다. 최신 상태를 과거 시점의 몰별 표 옆에 붙이면 '그때도 막혔던 것처럼'
  보여 오해를 부른다 — 그래서 ProductItem 이 아니라 PricePoint 에 담았다.

화면
- lib/sourceState.ts: 상태별 라벨·색·설명·confirmed 를 한곳에. 미지의 상태가 와도 화면이 깨지지
  않는다(값 그대로 표시 + '모름' 취급). 색은 전부 @theme 토큰 참조(raw hex 금지).
- 상품 목록: partial 이면 '일부 확인 못함' 배지 + 툴팁에 어느 몰인지.
- 몰별 비교 카드 위: 몰별 상태·수집 건수·실패 사유 원문(툴팁). 가격표에 없는 몰이 **왜** 없는지를
  여기서 답한다 — by_mall 은 가격이 있는 몰만 담으므로 그 답이 여기밖에 없다.

검증: ASGI 직접 호출로 두 엔드포인트 응답 확인(한글 사유 포함), tsc 오류 없음.
테스트 3건 추가(목록 노출 / 시점별 상태가 각각 다르게 / 컬럼 추가 이전 옛 행 호환).
전체 292 passed. 진행 상황은 docs/result-states.md 4절.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 11:05:31 +09:00
민헌
74db218410 feat(lps): 2단계 — price_history 에 몰별 확인 상태 저장
1단계에서 만든 SourceState 가 job.result 에만 있어 화면까지 못 갔다. by_mall 은 **가격이 있는
몰만** 담으므로, 빠진 몰이 '거기엔 없더라'인지 '거기를 못 봤다'인지 구분할 자리가 없었다.

- price_history.sources (JSONB): 몰별 상태를 그대로 담는다.
  {"naver": {"state": "matched", "count": 40}, "coupang": {"state": "blocked", "error": "..."}}
  열린 스키마라 몰이 늘거나 근거를 덧붙여도 마이그레이션이 필요 없다(by_mall 과 같은 방침).
- price_history.partial (bool): 결과가 완전한가. sources 에서 유도 가능하지만 컬럼으로 둔다 —
  소비자가 '어떤 상태가 확인된 것인가'라는 판단 규칙까지 알아야 하면 **상태 정의가 두 곳으로
  흩어진다**. 판단은 LPS 가 끝내고 소비자(negodata·lps-admin)는 사실 하나만 읽는다.
- 부분 인덱스 ix_price_history_partial — partial=true 행만 담아 작게 유지(운영 점검·알림용).
- _record_history 가 per_source 를 받아 partial 을 계산해 기록한다. 네거티브 캐시 히트 경로는
  sources 없이 남긴다(부분 결과는 애초에 캐시하지 않으므로 항상 확정).
- 마이그레이션: postgres-init/dbeaver/7_lps_source_state_dbeaver.sql (재실행 안전, **운영 적용 필요**)

검증(로컬 실 DB): 쿠팡 차단 vs 쿠팡 0건은 by_mall 이 둘 다 ['naver'] 로 같지만
partial(true/false)·sources.coupang.state(blocked/empty)가 두 경우를 갈라낸다.
JSONB 는 ensure_ascii=False 로 한글 사유가 깨지지 않는 것도 테스트로 고정.

테스트 3건 추가, 전체 289 passed. 진행 상황은 docs/result-states.md 4절.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 10:57:19 +09:00
민헌
b278f58d9c feat(lps): 1단계 — 몰별 상태 보존, '없음'과 '못 봄'을 가른다
docs/result-states.md 의 1단계. 어댑터는 이미 구분을 알고 있는데 핸들러가 그 정보를 버리고
있었다(per_source[src] = {"error": 문자열}). 그래서 차단당해 못 본 몰이 화면에서 '그 몰엔 없음'
으로 둔갑했다. 새로 알아낼 정보는 없고, 흘리던 걸 잡아두기만 하면 된다.

- common/enums.py: SourceState 7상태 추가(MATCHED/NO_MATCH/EMPTY/BLOCKED/ENV_BLOCKED/
  UNAVAILABLE/SKIPPED). `.confirmed` 프로퍼티로 **'봤다 vs 못 봤다' 경계를 한곳에** 둔다 —
  이 경계가 무너지면 나머지 판단이 전부 틀어지므로 흩어놓지 않는다.
- AdapterError.state: 어댑터가 아는 구분을 실어 보낸다. blocked/fatal 은 '어떻게 대응할까'
  (회전·재시도)를 위한 값이고 state 는 '사용자에게 뭐라 말할까'를 위한 값이라 쓰임이 다르다.
  특히 blocked=False 하나에 결과0건(EMPTY)과 전송실패(UNAVAILABLE)가 섞여 있어 state 없이는
  갈라낼 수 없었다. **기본값은 UNAVAILABLE** — 모르면 '못 봤다'가 안전하다(EMPTY 로 두면
  확인도 안 한 몰을 '없음'으로 단정한다).
- browser_base: raise 지점 5곳에 상태 부여. 핵심 갈림은 0건 종착 한 곳 —
  blocked=False → EMPTY(정말 없다) / blocked=True → BLOCKED(못 봤다).
- _search_round: {"state": ..., "count"|"error": ...} 로 구조화. EMPTY 는 '정상 응답'으로 세어
  (confirmed) 쿠팡에 정말 없을 때 잡이 재시도로 낭비되지 않게 한다.
- _finalize_states: 수집만 된 소스를 AI 판정 뒤 MATCHED/NO_MATCH 로 확정한다. 실패 상태는
  이미 확정이라 덮지 않는다.

검증(9조합 실측): empty → partial=False(확정) / blocked·env_blocked·unavailable → partial=True.
테스트 12건 추가, 전체 286 passed. 진행 상황은 docs/result-states.md 4절에 기록.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 10:47:28 +09:00
민헌
1e966f7121 docs(lps): 결과 상태 — 운영자와 실사용자를 나눠 접는 원칙 추가
보는 사람이 둘인데 하나로 뭉뚱그리고 있었다.
  lps-admin  운영자 — 목적이 **진단**이다. '왜 그랬나'에 답해야 하므로 7상태를 그대로 보고
             차단 마커·ip_request_no·포트까지 붙인다. 특히 blocked(자동 회복)와
             env_blocked(사람이 고쳐야 함)를 반드시 갈라야 한다 — 개입 여부가 갈린다.
  negodata   실사용자 — 목적이 **행동**이다. 할 수 있는 건 '쓴다/다시 시도/넘어간다' 셋뿐이라,
             원인이 달라도 다음 행동이 같으면 같은 표기로 접는다:
               no_match·empty            → '–'        (둘 다 결론은 '이 몰엔 없다')
               blocked·env_blocked·unavailable → '확인 못함' (행동은 '나중에 다시' 하나뿐)

핵심 원칙: **상태는 하나로 정의·저장하고, 접는 건 표시 단계에서 한다.**
저장을 단순화하면 관리자가 원인을 못 보고, 표시를 상세화하면 사용자가 못 읽는다.
두 화면 모두 price_history 를 읽으므로(admin_service 확인) 데이터는 공유하고 투영만 달리한다.

예외 하나: '일부 확인 못함'(partial)은 사용자에게도 접지 않는다. 이건 행동을 바꾸기 때문이다 —
'이 가격이 최종인가'의 답이 달라진다. 다만 사용자에게 필요한 건 어느 몰이 왜 막혔는지가 아니라
결과가 완전하지 않다는 사실 하나다.

남은 일도 4단계로 갱신(1 플래그 보존 → 2 저장 자리 → 3 admin 표시 · 4 사용자 표시).
3·4 는 같은 데이터를 다르게 접는 것이라 병행 가능.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 10:11:40 +09:00
77d96cd4a4 [fix] landing: 히어로 CTA 문구 — 클릭 후 일어나는 일에 맞춘다
여기서 직접 체험  →  협상 예시 체험
  데모 요청        →  실제 데모 받기

두 버튼은 성격이 전혀 다른데 문구가 그걸 구분하지 못했다.
1차는 페이지 안에서 바로 조작하는 인터랙티브 데모고, 2차는 이름·이메일을 받는
리드 폼이다. 클릭 직후 경험이 다르므로 문구도 갈라야 한다.

협상 예시 체험
  고정 데이터 시뮬레이션이라 "예시"를 밝힌다. 다만 "보기"로 낮추지는 않았다 —
  도착 섹션이 "직접 경험해 보세요" 이고 역할 카드가 "공급사로 해보기" 라,
  버튼만 구경하는 말이면 클릭 전 기대와 도착 후 요구가 어긋난다.

실제 데모 받기
  누르면 폼이 뜨고 데모는 이메일로 온다. "직접 체험" 이라 쓰면 눌렀을 때 바로
  만질 줄 알았다가 입력을 요구받아 낚인 느낌이 된다. "받기" 는 이메일로 온다는
  사실이 드러나서 폼이 뜨는 게 자연스럽다.

모달 제목도 "데모 요청" → "실제 데모 받기" 로 맞췄다. 버튼이 "받기" 인데 열린 창이
"요청" 이면 전환 순간에 다른 화면처럼 읽힌다.

제출 버튼은 "데모 신청" 그대로 둔다. 같은 원칙을 적용하면 그 클릭으로 실제 일어나는
일은 신청 접수이지 데모 수령이 아니다(수령은 이메일로 나중에).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 09:47:07 +09:00
민헌
04b697a9eb docs(lps): 검색 결과 상태 정의 — '못 찾음'과 '못 봄'을 가른다
'가격을 못 찾았다' 한 문장에 뜻이 정반대인 상황이 섞여 있다: 그 몰에 정말 없는 것(사실)과
그 몰이 우리를 막아 못 본 것(미확인). 지금 화면은 둘 다 '–' 로 똑같이 보여준다.
구현 전에 용어를 맞추려고 상태를 정의한다.

코드를 확인해 **실제로 구분 가능한 것**만 정의했다:

몰(소스) 단위 7상태 — 경계는 '사실'(no_match/empty) 대 '미확인'(blocked/env_blocked/unavailable).
앞의 둘은 "없다"고 말해도 되고, 뒤의 셋은 말하면 안 된다.

상품 단위 — found / not_found / error + partial 꼬리표. partial 은 독립 상태가 아니라
found·not_found 에 붙는데, 특히 'not_found + partial' 은 결론이 아니다(못 본 몰에 있었을 수 있음).

확인 과정에서 드러난 근본 원인:
- 어댑터는 이미 구분을 **안다** — AdapterError 에 blocked·fatal 이 있고 detect_block 이
  '차단 vs 정상 빈결과'를 판정한다.
- 그런데 **핸들러가 그 플래그를 버린다**: per_source[src] = {"error": f"{...}"} — 문자열만 남는다.
- 게다가 0건도 예외로 온다(return 은 1건 이상일 때만). 즉 empty 와 blocked 가 둘 다
  AdapterError 로 도착하는데 구분 플래그를 버리므로 이후로는 갈라낼 수 없다.

→ 새로 알아낼 정보는 없다. 이미 아는 걸 흘리고 있을 뿐이라, _search_round 한 곳이 출발점이다.
   남은 일을 1(플래그 보존) → 2(price_history 자리) → 3(화면 표기) 순서로 정리했다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 09:46:23 +09:00
민헌
14bcf1481b fix(negodata): 검색 실패를 '변동 없음'으로 표시하던 문구 정정
LPS 가 outcome='error'(모든 소스 차단)로 내려주는 경우가 화면에서 '변동 없음'으로 보였다.
사용자는 '검색은 잘 됐는데 더 싼 게 없구나'로 읽지만, 실제로는 **가격을 확인조차 못 한** 것이다.
뜻이 정반대라 오해가 크다.

원인: success_yn=false 를 전부 notfound 로 묶었고, notfound 와 done 이 같은 문구('변동 없음')를
쓴다. fail_reason 은 이미 API·프론트 타입까지 내려오는데 쓰이지 않고 있었다.

→ fail_reason='error' 면 failed 로 분류한다. 화면은 '탐색 실패'가 되고, 실패 사유가 함께 뜨며,
   재검색 대상(failedIds)에 포함돼 사용자가 다시 시도할 수 있다 — 확인 못 한 건 재시도가 맞다.
   'not_found'(진짜 못 찾음)는 기존대로 '변동 없음'을 유지한다.

LPS 변경 없음(이미 내려오는 값을 쓴 것). ItemState 주석도 실제 의미에 맞게 정정.
tsc: 이 파일 오류 없음(기존 xlsx 미설치 오류는 무관).

> 폴더 관례상 negodata 는 인수인계 대상이나, 이번 건은 사용자 요청으로 예외 적용.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 09:38:11 +09:00
민헌
152aed9908 fix(lps): 한 소스가 막혀도 잡을 정상 종료 — '탐색 중'에서 멈추던 원인
운영 로그(2026-08-06)에서 드러난 문제. 쿠팡이 막힌 동안 **네이버가 매번 40건을 가져왔는데도**
잡이 전부 DEAD 로 갔다:

  fail 3c0bcaff → DEAD ({'coupang': {'error': 환경 차단}, 'naver': {'count': 40}})

그리고 DEAD 는 price_history 에 행을 남기지 않는다. 이를 폴링하는 negodata 최저가 모달은
받을 결과가 영영 없어 '탐색 중'에서 멈춘다(사용자 화면 확인). 즉 프론트 버그가 아니라
**백엔드가 답을 안 준 것**이다 — 두 증상이 같은 뿌리였다.

원인: 핸들러가 '하나라도 실패했나'(bool)만 보고 무조건 raise 했다. 한 소스가 막혔다고
다른 소스가 멀쩡히 가져온 결과까지 버린 셈이다.

바꾼 것:
- _search_round 가 **성공한 소스 목록**을 돌려준다(bool → list).
- 살아있는 소스가 하나라도 있으면 그 결과로 진행한다. **전부 죽었을 때만** 재시도한다
  ('없음'이라 단정할 수 없는 건 그때뿐이다).
- 결과에 sources_ok/sources_failed/partial 을 실어 커버리지를 드러낸다(부분 결과 로그도 남김).
- 부분 실패 상태의 not_found 는 **네거티브 캐시에 넣지 않는다** — 막힌 소스엔 있었을 수 있는데
  '없음'으로 굳히면 TTL 동안 재검색이 막힌다(사용자가 '다시 검색'을 눌러도 캐시 히트).
- 전부 실패 + 재시도 소진이면 DEAD 대신 outcome='error' 로 **이력을 남기고** 종료한다.
  화면이 무한 대기 대신 결과를 받는 게 중요하다.

negodata 는 변경 없이 받는다(outcome='error' → success_yn=false, fail_reason='error').

테스트 5건 추가·1건 갱신(옛 계약 '한 소스 실패 시 raise' 를 새 계약으로 교체), 전체 274 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 16:34:36 +09:00
민헌
f483839b11 Merge branch 'fix/naver-container-wtm'
네이버 컨테이너 차단 해결 — 원인은 UA 의 플랫폼 토큰(리눅스)이었다.
네이버가 리눅스 데스크톱 Chrome 을 HTTP 405 로 거부한다: Linux UA 0/3 통과 · Mac UA 3/3 통과
(같은 이미지·같은 KR 프록시, IP 3개씩). 405 는 JS 이전 HTTP 계층이라 WebGL·폰트 등 지문은
원인이 아니었다. 리눅스에서만 UA 플랫폼 토큰을 맥으로 치환하며, Chrome 버전은 실제 값을 유지한다.
쿠팡은 잘 통과하므로 건드리지 않았다.
2026-08-06 15:42:49 +09:00
민헌
8d15fe6bb0 fix(lps): 네이버 컨테이너 차단 해결 — 원인은 UA 의 플랫폼 토큰이었다
컨테이너를 로컬에 재현해 규명했다. '컨테이너가 문제'가 아니라 **UA 가 리눅스라고 말하는 것**이
원인이다. 네이버는 리눅스 데스크톱 Chrome 을 HTTP 405 로 거부한다.

실측(같은 이미지·같은 KR 프록시, 서로 다른 IP 3개씩):
  X11; Linux x86_64        0/3 통과   전부 405 + wtm_captcha (~50KB)
  Macintosh; Intel Mac     3/3 통과   전부 200 · 6건 · ~1.0MB
  안드로이드·아이폰 모바일    0/2       418 '비정상적인 접근'(2.6KB, 회전 무효 하드차단)
프록시 없이 같은 집 IP 로도 호스트 통과 / 컨테이너 차단이 재현돼 IP·게이트웨이는 배제됐다.
맥에서 잘 되던 이유도 이걸로 설명된다.

**405 가 열쇠였다** — JS 가 돌기 전에 HTTP 계층에서 거부당한다. 그래서 그동안 의심하던
WebGL·폰트·plugins 는 애초에 원인이 될 수 없었다(확인차 --enable-unsafe-swiftshader 로
WebGL 을 살려봤지만 405 그대로였다).

조치:
- services/search/user_agent.py: 리눅스에서만 UA 플랫폼 토큰을 맥으로 치환. Chrome 버전은
  `--version` 으로 실제 값을 읽어 유지한다 — 하드코딩하면 컨테이너 Chrome 업데이트 시
  UA 와 엔진이 어긋나 그 불일치가 새 봇 신호가 된다. 조회 실패해도 크롤을 막지 않는다.
- NaverShopAdapter.mac_ua_on_linux = True (쿠팡은 잘 통과하므로 기본 False 그대로 — 멀쩡한 걸
  건드리지 않는다). 맥/윈도우에서는 자동 미적용.
- 실제 어댑터로 컨테이너 검증: '생수' 40건, '스페셜티 원두 1kg' 40건 통과.

부수:
- fingerprint.judge: WebGL 이 **아예 없는** 경우를 OK 로 흘려보내던 판정 버그 수정(컨테이너
  재현 중 발견 — 소프트웨어 렌더링보다 더 튀는 값인데 침묵했다). UA 플랫폼 항목 추가.
- docs/operations.md: '미해결' 절을 원인·수치·조치·확인법으로 교체.
- fingerprint.py: JS 위장이 이 스택에서 불가능하다는 실측 기록 유지(재시도 방지).

테스트 8건 추가(리눅스에서만 보정·실제 버전 유지·조회 실패 폴백·네이버만 opt-in), 전체 270 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 15:40:14 +09:00
민헌
9380760869 fix(lps): 네이버 컨테이너 차단 — 계측 CDP 토글 + 지문 판정 + 폰트 보강
운영 실측으로 범위가 좁혀졌다: 같은 컨테이너에서 **쿠팡은 60건 정상, 네이버만** 매 IP
첫 요청부터 wtm_captcha. kr_host 도 정상이다(ensure_ports 에 kr.decodo.com, 마커가 해외 IP
하드차단 '비정상적인 접근'(2.6KB)이 아니라 wtm_captcha(43~57KB)). IP·게이트웨이가 아니라
브라우저 지문이 남은 후보다.

**먼저 확인한 것 — 지문 위장은 이 스택에서 불가능하다(중요)**
WebGL 렌더러(컨테이너=SwiftShader)를 실기기 이름으로 덮으려 했으나 주입 경로가 둘 다 막혔다:
  context.add_init_script()  무반응. 단순 마커(window.__M__) 주입조차 undefined 다.
                             patchright 가 무력화한다 — 이 API 는 CDP
                             addScriptToEvaluateOnNewDocument 로 구현되고 그 흔적이 탐지
                             신호라, 걷어내는 게 patchright 의 존재 이유다.
  확장(MV3, world:MAIN, document_start)  로드는 되지만 값이 바뀌지 않았다.
→ 동작하지 않는 코드를 남기지 않는다. 스푸핑은 넣지 않고, 대신 **판정**만 한다.
   재시도 방지를 위해 이 실측을 fingerprint.py docstring 에 남겼다.

**넣은 것**
- services/search/fingerprint.py: 지문 판정(항목·정상여부·관측값·왜 문제인가). 진단이 곧
  '무엇을 고칠까'로 이어지도록 이유를 함께 낸다.
- net_meter 토글: 네이버에 계측용 CDP(Network.enable)를 붙이지 않는다(기본 off).
  네이버는 '요청 가로채기 자체가 탐지 신호'라 리소스 차단을 끈 이력이 있는데 계측 CDP 는
  그대로 붙고 있었다 — 같은 계열이다. [WorkerConfig].naver_net_meter 로 재빌드 없이 A/B
  (config 는 마운트). 끄면 대역폭이 DOM 크기 근사로 떨어질 뿐 크롤엔 영향 없다.
  쿠팡은 잘 통과하므로 기본값을 바꾸지 않는다.
- Dockerfile.worker: fonts-noto-cjk·fonts-liberation 추가(나눔 하나뿐이면 폰트 지문이 부자연스럽다).
- diag_naver.py 가 같은 판정 모듈을 쓰도록 정리.

통과 환경 기준값(맥 실측): 의심 항목 0개 · HTTP 200 · 6건 · 1.26MB.
테스트 6건 추가(통과 지문을 문제로 몰지 않는지·컨테이너 지문을 놓치지 않는지), 전체 262 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 15:22:01 +09:00
민헌
3bdff74ceb feat(lps): 네이버 WTM 차단 진단 도구 — 컨테이너에서 '네이버가 보는 우리 브라우저'를 덤프
운영(실 x86 서버) 실측으로 문제 범위가 크게 좁아졌다:
  쿠팡   컨테이너에서 **60건 정상**
  네이버 매 IP 첫 요청부터 wtm_captcha(43~57KB)

즉 '컨테이너가 문제'가 아니라 **네이버 WTM 만 이 컨테이너를 걸러낸다**. kr_host 도 정상이다
(ensure_ports 에 kr.decodo.com 이 찍혔고, 마커가 해외 IP 하드차단 '비정상적인 접근'(2.6KB)이
아니라 wtm_captcha 다 = 한국 IP 는 제대로 나가고 있다). IP·게이트웨이가 아니라 지문 쪽이 남았다.

서버는 Docker 전용이라 '워커만 호스트 실행' 우회를 쓸 수 없다. 그래서 추측으로 이것저것
고치는 대신, **고치기 전에 무엇이 다른지 눈으로 보는** 도구를 먼저 만든다.

diag_naver.py — 컨테이너 안에서 실물과 동일한 스택(patchright + 실제 Chrome + locale/timezone +
kr 프록시)으로 띄워:
- 네이버 WTM 이 볼 수 있는 값 덤프(UA·languages·webdriver·plugins·screen/avail·devicePixelRatio·
  WebGL vendor/renderer·한글 폰트·timeZone …)
- 자동화/가상화로 읽히기 쉬운 항목에 판정과 이유를 붙임
- 실제 msearch 요청 → HTTP·카드 등장 여부·파싱 건수·차단 마커, 실패 시 HTML 저장

--no-cdp   바이트 계측 CDP(Network.enable)를 빼고 A/B — 네이버는 '요청 가로채기 자체가 탐지
           신호'라 리소스 차단을 끈 이력이 있는데, 계측용 CDP 는 그대로 붙고 있다
--no-proxy 프록시 없이 시도 — IP 요인과 지문 요인 분리

재빌드 없이 docker cp + docker exec 로 실행한다(사용법은 파일 상단 docstring).

통과 환경 기준값(맥 직결, 참고용):
  glRenderer=ANGLE(Apple M3 Pro) · plugins=5 · 한글폰트 6종 · screen 2560x1080/avail 2560x1050
  → HTTP 200 · 파싱 6건 · html 1.26MB

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 14:45:52 +09:00
민헌
b403ca21c9 feat(postgres-init): LPS 2026-08 스키마 추가분 dbeaver 파일 — 운영 워커 기동 실패 해소
운영 워커가 기동 중 크래시 루프에 빠졌다:
  UndefinedTableError: relation "proxy_port" does not exist
  (worker_main.py:336 store.ensure_ports → 기동 시 반드시 만지는 테이블)

3_lps_dbeaver.sql 이후 늘어난 스키마가 운영 DB(172.30.1.36/lps_db)에 반영되지 않은 것이다.
모델은 바뀌었지만 이미 만들어진 DB 는 자동으로 따라오지 않는다.

6_lps_2026-08_dbeaver.sql 신규:
- proxy_port 테이블 + LRU 인덱스(ix_proxy_port_pick)
- price_history 신뢰 신호 2컬럼(final_rating, final_review_count)
- price_history 배송 3컬럼(final_shipping_fee/type/label)
- 끝에 검증 SELECT — 워커가 요구하는 테이블 6종의 존재 여부를 한눈에 확인

전부 IF NOT EXISTS 라 재실행 안전하고, 신규 설치(3 → 6)와 기존 DB 양쪽에 같은 파일을 쓴다.
검증: 기존 lps_db 재실행(NOTICE 만, 오류 없음) + 빈 DB 에 3 → 6 순서 적용 후 컬럼이 모델과 일치.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 13:25:46 +09:00
d3e33b10f4 [feat] landing: 최종 CTA 결과 화면을 영상에서 컴포넌트로 — 브랜드 팔레트·중립 화자
negotiation_annotated_3d.mp4(677K)를 NegotiationReplay 컴포넌트로 대체했다.
영상이라 두 가지를 못 고쳤고, 둘 다 픽셀에 구워진 것이 원인이었다.

화자
  "아이마켓 구매 MD" 가 박혀 있었다. POC 고객사 이름이라 다른 MRO 회사에 영업할 때
  쓸 수 없다. 이제 "구매 담당자" 로, 회사명 없이 역할만 쓴다 — 상수라 언제든 바꾼다.

색
  실측하면 말풍선 #5A68CD, 목표가 라벨 #B96175, 와일드카드 배지 #DB840B 로 셋 다
  토큰에 없는 색이었고, 정작 브랜드의 핵심 대비 장치인 민트(#0FFFD6, 상대편 색)는
  한 번도 쓰이지 않았다. 이제 토큰만 쓴다 — 우리 쪽 파랑, 상대편 민트.
  진행 막대도 파랑에서 민트로 차오르며 양쪽이 만나는 지점을 그린다.

부수 효과
  - 다크 무대에 네이티브로 그리니 밝은 영상이 구멍처럼 뜨던 문제가 사라졌다.
    카드로 감싸고 그림자로 띄우던 처리가 더는 필요 없다.
  - 677K + 포스터 85K → 0. 어느 해상도에서도 선명하다(영상은 840x1260 고정이었다).
  - role="img" + aria-label 로 스크린리더가 결론을 읽는다. 영상은 못 읽었다.

숫자는 지어내지 않고 DEMO_ITEMS[0] 을 그대로 쓴다. 위쪽 인터랙티브 데모와 같은
품목·같은 가격이라 둘을 다 본 사람에게 앞뒤가 맞는다.

구현 메모
  - 화면 밖에서는 타이머를 멈춘다(IntersectionObserver). 페이지 최하단이라
    대부분의 시간 동안 보이지 않는데 계속 도는 건 배터리를 쓰는 일이다.
  - prefers-reduced-motion 이면 최종 상태를 정지 표시한다.
  - 낙찰 카드 자리를 h-[74px] 로 항상 확보하고 내용만 켜고 끈다. 나타날 때 아래가
    밀리면 페이지가 주기적으로 흔들린다. 대신 금액은 whitespace-nowrap — 고정 높이라
    한 줄이라도 접히면 잘린다(실제로 "1,080,000" 뒤 "원" 이 접혔다).

검증: 1280x900 카드 319x74 · 375x812 285x74, 슬롯 초과·가로 넘침 없음.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 13:15:46 +09:00
17a698a7af Merge branch 'feature/negodata' 2026-08-06 13:13:11 +09:00
민헌
7b9d895ead fix(lps): 풀 고갈 시 남의 IP 를 빌려 쓰던 문제 — 임대 없으면 크롤하지 않는다
동시 다상품 검색 점검 중 발견. 워커 3개(소유자 6)가 포트 2개/게이트웨이를 두고 경합하는
상황을 실제 코드로 돌리니, 임대를 못 받은 워커가 **남이 쥔 포트를 그대로 집어 같은 IP 로
동시에 요청**했다:

  coupang-w1   사용=70002 임대=70002
  coupang-w2   사용=70002 임대=None    ← 같은 IP 를 둘이 사용

원인은 _port() 의 계산식 폴백이다. 장부 모드에서 acquire 가 None 을 줘도 시간창 계산으로
포트를 하나 골라 돌려줬다. 포트 장부가 존재하는 이유("워커 N개가 같은 IP 에 요청을 몰면 그 IP 가
빨리 탄다" — port_registry.py 도입 배경)를 정면으로 무너뜨리는 경로다. 게다가 하필 **풀이 마른
상태 = IP 가 가장 귀할 때** 발동해, 남은 IP 를 두 배 속도로 태우는 악순환을 만든다.

→ 장부 모드에선 임대한 포트만 쓴다(없으면 None). 못 받으면 AdapterError 로 실패하고 잡이
   백오프 후 재시도한다 — 그 사이 쿨다운이 풀린다. 풀 고갈 자체는 proxy_ports_low 가 이미 운다.
→ playwright_proxy() 도 임대가 없으면 예외. 여기서 None 을 돌려주면 **프록시 없이** 브라우저가
   떠 서버 공인 IP 로 크롤하게 되는데, 그 IP 가 타면 회전으로 복구할 수 없다.

동시성 점검 결과(포트 20개/게이트웨이, 워커 3개):
  정상 24건 동시     성공 24 · 포트 중복 보유 0
  풀 고갈           성공 4/6(2건은 정상적으로 실패) · **같은 IP 공유 0**
  전면 차단          소각이 어댑터당 2개에서 멈춤(게이트웨이당 6/20) · 브레이커 6/6 트립

테스트 3건 추가(고갈 시 None 반환·남의 포트 미사용 / 임대 없는 playwright_proxy 예외 /
검색이 깔끔히 실패), 전체 256 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 12:01:15 +09:00
3214e4f782 [feat] landing: 가이드 2번 탭 mp4 연결 · 최종 CTA 에 결과 화면 배치
① 이용 가이드 2번 탭 (협력사별 1:1 협상)
video 필드가 빠져 있어 이 탭만 1.7MB GIF(390x780)를 내려받고 있었다. 같은 내용의
mp4 가 저장소에 있는데 참조만 없었다 — 786K, 540x1080 으로 더 가볍고 더 크다.
GifSlot 이 video 를 우선 재생하므로 한 줄로 끝난다.

원본 캡쳐가 좌우로 잘려 문장이 중간에 끊기는 문제는 남아 있다(540x1080 원본 파일
자체에 잘린 채로 들어 있어 CSS 로 못 고친다). 재촬영이 필요하고 주석에 남겼다.

③ 최종 CTA — negotiation_annotated_3d.mp4
이용 가이드에는 넣지 않았다. 그 섹션은 "실제 화면으로 보여드립니다" 라고 약속하는데
이 영상은 재구성 목업이다(실제 UI 는 "아이마켓코리아 (구매 MD)", 영상은 "아이마켓 구매 MD").
1·3번 탭이 실제 캡쳐인데 2번만 목업이면 register 도 어긋난다.
전환을 요청하는 자리에는 그 약속이 없으므로 마케팅 컴포짓이 맞는 자리다.

- 영상 배경이 밝은 회색(#EEF4F5)이라 무대 위에 그냥 놓으면 구멍처럼 뜬다.
  모서리를 깎고 그림자를 깔아 "떠 있는 물체"로 만들면 밝기 차이가 의도로 읽힌다.
- 폰 프레임이 영상에 내장돼 있어 PhoneFrame 으로 감싸지 않는다. 감싸면 이중 프레임이다.
- 2단이 되면서 width md(max-w-4xl) → lg(max-w-5xl), 제목은 lg 부터 한 단계 축소.
  4xl 안에 세로 목업과 카피를 밀어넣으면 양쪽 다 좁아진다.
- preload="none" — 페이지 최하단이라 첫 로드에 677K 를 미리 받을 이유가 없다.

검증: 1280x900 2단 정상, 375x812 카피→영상 중앙 적층·가로 넘침 없음.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:56:59 +09:00
민헌
08afc7d4ad fix(lps): sticky 만료 회전이 저트래픽에서 안 먹던 문제 + prod 템플릿에 kr_host 누락
prod 영향 점검 중 발견. 직전 커밋(377389f)의 sticky 만료 회전은 **브라우저가 열려 있을 때만**
동작했다 — `_ctx is None` 이면 만료 검사를 건너뛰었다. 그런데 negodata 연동은 수동 트리거
전용이라 검색이 드문드문 들어오고, 그때는 유휴 정리(120s)로 브라우저가 닫힌 채 매 검색이
그 경로로 들어온다. 즉 **실사용 패턴에서만 안 먹는** 반쪽 수정이었다(377389f 커밋 메시지·README
의 '고쳤다'는 서술이 부정확했다).

실측(합성 게이트웨이·10포트, sticky 매번 경과):
  연속 검색            IP 6개 순환      
  저트래픽 · 예산 3     IP 2개          예산이 대신 회전시켜 가려져 있었음
  저트래픽 · 예산 0     **IP 1개 고정**    ([DecodoConfig] 주석의 '0=시간창 회전만'이 거짓)

원인은 시계가 둘이었던 것이다. sticky 만료를 브라우저 기동 시각(_launched_at)으로 쟀는데,
브라우저는 닫혔다 열릴 때마다 시계가 되감긴다. IP 를 쥔 시간과 어긋나는 이 구조가 F1(예산
미발화)과 F4(회전 안 됨)의 공통 원인이었다.
→ 시계를 _session_started_at 하나로 통일하고 _launched_at 을 제거했다. 만료 판정은 브라우저가
   닫혀 있어도 수행한다. 세 시나리오 모두 정상 회전 확인.

**prod 템플릿 kr_host 누락**(기존 문제, 이번 변경과 무관):
config.prod.toml.example 에 kr_host 가 없어 그대로 복사하면 네이버가 국가 무지정 게이트웨이로
떨어진다. 해외 residential IP 는 '비정상적인 접근'(2.6KB) 하드차단이고 회전으로 회복 불가라
네이버 결과가 통째로 0건이 된다. kr_host + naver_ip_request_budget 을 경고 주석과 함께 추가.

테스트 2건 추가(저트래픽 sticky 만료 회전 / IP 를 쥔 시간이 수명 내면 회전 안 함), 전체 253 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:50:08 +09:00
e809129ae9 [feat] landing: 리드 수신 Google Apps Script 추가 (Slack 대체)
사내에서 Slack 을 쓰지 않아 수신처를 Google 로 잡는다. Apps Script 웹 앱이 POST 를
받아 스프레드시트에 적재한다 — 별도 서비스 가입 없이 이미 있는 Google 계정으로 끝나고,
영업이 시트를 그대로 열어 본다.

api/lead.ts 는 고치지 않았다. 이미 LEAD_WEBHOOK_URL 로 JSON 을 POST 하고, Slack 용
text 와 원본 필드를 함께 실어 보내도록 만들어 둬서 수신처만 바꾸면 된다.

스크립트는 docs/ 에 둔다. landing/api/ 안에 두면 Vercel 이 /api 규약에 따라 이 .js 를
서버리스 함수로 빌드하려 들고, Apps Script 전역(SpreadsheetApp 등)을 쓰므로 배포가 깨진다.

스크립트가 처리하는 것
- 헤더 자동 생성·확장. 나중에 필드가 늘어도 스크립트를 다시 안 고쳐도 된다.
- LockService 로 동시 제출이 같은 행에 겹쳐 쓰는 것을 막는다.
- Slack 전용 요약(text)은 시트에서 제외 — 같은 내용이 개별 필드에 이미 있다.
- doGet 은 배포 직후 생존 확인용.

설치 절차는 파일 상단 주석에 있다. 배포 후 Vercel 환경변수 LEAD_WEBHOOK_URL 설정 +
재배포가 필요하다 — 환경변수는 배포 시점에 주입된다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:34:01 +09:00
631a501c5c [chore] landing: 렌더되지 않던 ROI 시뮬레이터 제거
roi-simulator.tsx 는 어디에서도 import 되지 않아 화면에 나오지 않았다.
누락이 아니라 의도적으로 내린 것이다 — 9ccf42c("카피 정직화")에서 <ROISimulator /> 를
빼고 그 자리에 <Comparison />, <Faq /> 를 넣었다.

되살리지 않는 이유는 파일 자체에 있다. 카피는 "아직 실제 도입 실증 사례가 없는
신규 카테고리"라고 스스로 인정하면서 절감액을 제시하고, 계산은 근거 없는 추산이다
(협력사당 6.5시간). 브랜드 보이스 가드레일 통과 기준 3번 — 주장·수치에 출처가 붙어야
하고 출처 없는 주장은 FAIL — 과 정면으로 부딪힌다. 다시 붙이려면 근거부터 확보해야 한다.

range-slider.tsx 도 함께 지운다. 사용처가 roi-simulator 하나뿐이라 같이 죽는다.

되살릴 일이 생기면 git 이력에 그대로 남아 있다.

남은 고아 두 개는 이번 작업과 무관한 기존 항목이라 두었다:
  components/ui/price-sparkline.tsx
  components/ui/slate-renderer.tsx

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:30:03 +09:00
ab77900dac [feat] landing: 본문 브랜드 표기를 네고시움으로 한글화
한글 문장 안에 영문 브랜드명이 섞이면 읽는 흐름이 끊긴다. 본문 카피만 바꾸고,
성격이 다른 자리는 남겼다.

바꾼 곳 (한글 문장 안)
  reinforcement · final-cta · footer 소개문 · how-it-works-demo · comparison 토글 · roi-simulator

남긴 곳과 이유
  logo.tsx alt="NEGOTIUM"    로고는 영문 워드마크 이미지다. alt 만 한글로 바꾸면
                             보이는 것과 읽히는 것이 어긋난다.
  footer © negotium Co., Ltd. 법인 표기. 등기된 이름이라 임의로 한글화할 수 없다.
  console.negotium.ai        실제 도메인.
  og:site_name               브랜드 정식 핸들.
  코드 주석                   내부 문서.

메타 타이틀은 "네고시움(negotium)" 으로 둘 다 담았다. 영문을 빼면 negotium 으로
검색해 들어오던 유입이 끊긴다 — 검색 질의는 사용자가 정하지 우리가 못 정한다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:19:30 +09:00
09d24f5022 [fix] landing: 구매자 모드 응수에 목표가 반영 · 무대 선택색 · 플레이 화면 유리 패널
구매 담당자 모드
공급사 모드와 같은 문제가 있었다. simulateBuyerRun 의 에이전트 첫 수가 marketLow 만
읊어서, 목표가를 얼마로 잡든 같은 문장이 나왔다 — 위임한 기준이 반영되는지 확인할
길이 없었다. 이제 목표가를 받았다고 명시하고, 시장 최저가 대비 위치에 따라 태도를
달리한다(근거부터 깔겠다 / 무리 없이 접근하겠다).
여는 말(협력사 정가 제시)은 아직 목표가를 모르는 시점이라 상수로 둔다.

무대 위 드래그 선택색
루트의 selection:text-primary 는 라이트 본문 기준이다. 그 값(#0101F3)을 무대(#070E24)
위에 얹으면 명암비 2.0:1 — app.css 의 --color-primary-on-stage 주석이 경고하는 바로 그
상황이다. 무대 섹션에서 텍스트를 드래그하면 글자가 사라졌다.
.stage-bg / [data-stage-hero] 안에서는 무대용 액센트로 갈아끼운다.

플레이 화면 유리 패널
컨트롤과 대화 로그가 섹션 배경 위에 그냥 얹혀 있어 무대와 같은 색이라 경계가 없었고,
조작 가능한 영역인지 읽히지 않았다. 재질은 무대 위 2차 CTA 와 같게 맞췄다 — 화면 안에
재질이 두 종류면 따로 논다. 테두리는 white/12 로 얕게: 면이지 조작 요소가 아니라
버튼만큼 세우면 카드가 버튼처럼 보인다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:17:16 +09:00
4536a48e40 [fix] landing: 제시가를 바꿔도 에이전트 응수가 그대로이던 문제
공급사 모드에서 슬라이더를 움직여도 첫 응수의 문장과 숫자가 똑같았다.

원인은 두 겹이다.
- 근거 문장이 offer 를 참조하지 않았다. cardsUsed === 0 분기가 marketLow 만 읊었다.
- CONCESSION[0] = 0 이라 부르는 값(ask)도 앵커로 고정이다.
둘이 겹쳐서, 얼마를 제시하든 화면이 전혀 반응하지 않는 것처럼 보였다.

값 고수는 유지한다 — 앵커를 지키는 게 이 에이전트의 성격이고, 제품이 파는 신뢰가
거기서 나온다. 대신 무엇에 대한 응수인지는 매번 달라지게 했다.

  정가 이상   정가 그대로는 검토가 어렵습니다 + 시장 최저가 제시
  최저가 위   제시가가 최저가보다 얼마 높은지 구체 수치로
  최저가 이하 최저가 선까지 내려온 것을 인정 + 기준까지 남은 격차

입력란 숫자에 콤마 누락도 같이 고쳤다. 화면의 다른 금액은 전부 won() 인데 여기만
1018800 으로 나와 같은 값으로 안 읽혔다. 확정 시 콤마를 넣고 파싱할 때 벗긴다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:10:50 +09:00
ea1249429a [feat] landing: 무대 위 2차 CTA 에 유리 재질 — 데모 요청·상담 신청
두 버튼 모두 무대에서 stageGhost 를 쓰므로 변형 하나로 해결된다.

구성은 넷 — 옅은 흰 채움, 뒤를 흐리는 backdrop-blur(12px), 위쪽 1px 안쪽 하이라이트
(유리 모서리에 빛이 걸리는 부분), 아래로 깔리는 부드러운 그림자. 채도를 살짝 올리면
뒤가 흐려지며 빠지는 색기가 돌아온다(saturate 1.5).

테두리는 white/40 에서 더 내리지 않았다. /30 이면 무대 광원부(#16205A) 대비가
2.60:1 로 떨어져 WCAG 1.4.11(UI 경계 3:1)에 미달한다 — 유리 느낌 때문에 버튼이
안 보이던 원래 문제(대비 1.09:1)로 되돌아간다. 투명감은 채움과 blur 로 내고 테두리는 지킨다.

hover 는 흰색 반전이 아니라 채움을 밝힌다. 유리는 뒤가 비치는 게 정체성이라
불투명하게 뒤집으면 그 순간 유리가 아니게 된다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 11:00:46 +09:00
d2f1a8f6f4 [fix] landing: 히어로 배경이 탁하고 흐리던 문제 — 스크림 축소 + 재인코딩
탁함: 스크림이 원인이었다. stage-deep(#03060F) 을 90/60/95 알파로 전면에 깔아
가장 옅은 지점에서도 영상이 40% 만 남았다. 그 값은 헤드라인 둘째 줄이 회색(/60)이던
시절 그 글자를 살리려고 넣은 것인데, 헤드라인이 흰색이 된 뒤로는 근거가 없어졌다.
게다가 중앙이 가장 옅어서 — 정작 글자가 있는 자리 — 보호 목적으로도 방향이 틀렸다.

흐림: 1280x720 crf 30 은 전면 배경으로 무르다. 1600x900 crf 24 로 재인코딩.
업스케일 1.19배 → 0.96배(다운스케일)로 바뀌어 선명도가 올라간다. 631K → 1.7M.

스크림을 걷었더니 이번엔 리드 문단(17px)이 밝은 입자 위에서 안 읽혔다. 헤드라인은
흰색 74px 이라 버티지만 본문은 못 버틴다. 전면을 덮으면 영상이 탁하고, 다 걷으면
본문이 안 읽힌다 — 둘 다 만족하려면 덮는 범위를 글자가 있는 자리로 좁혀야 한다.

  세로 그라데이션  50 / 투명 / 45   헤더 nav 와 하단 전환만 보호
  중앙 방사형      85 → 55 → 투명   카피 뒤만 국소적으로

색은 color-mix 로 --color-stage-deep 를 참조한다. 하드코딩하면 토큰이 둘이 된다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 10:57:49 +09:00
be039dd92b [feat] landing: 협상 데모에 에이전트 추천가·직접 입력 추가, 히어로 헤드라인 흰색 통일
데모 — 추천하고, 사용자가 고친다
슬라이더 하나뿐이라 정확한 값을 넣을 수 없었고, 에이전트가 추천하는 자리도 없었다.
"에이전트가 제안하면 사람이 확정한다"가 제품의 관계인데 UI 가 그걸 보여주지 못했다.

- 에이전트 추천가 + 근거 + 원클릭 적용. 값은 지어내지 않고 시뮬레이터가 실제로 쓰는
  수치에서 가져온다 — 구매자는 anchor(reachable = target >= anchor 인 최저선),
  공급사는 marketLow(에이전트가 응수할 때 인용하는 값). 지어낸 숫자를 추천이라 부르면
  사용자가 그대로 넣었을 때 결과가 설명되지 않는다.
- 직접 입력란 추가. 실제 구매 담당자는 "얼마까지"라는 숫자를 이미 들고 온다.
  타이핑 중에는 clamp 하지 않고 blur/Enter 에서 확정한다 — 입력 즉시 범위로 당기면
  "1" 을 치는 순간 튕겨서 손가락과 싸운다.
- 슬라이더·직접입력·추천적용이 commitOffer 한 곳을 거친다. 경로가 셋이면 값이 셋 된다.
- 에이전트 응수 후 offer 만 갱신하고 입력란은 두던 버그 수정. 슬라이더와 숫자가 어긋났다.
- 잠금 조건(role === buyer && turns.length > 0)을 inputLocked 로 묶었다.

히어로 헤드라인
"동시에 협상합니다." 를 흰색으로. 예전 캔버스는 하단 밴드에만 입자가 있어 헤드라인
뒤가 거의 단색이었고 그때는 투명도 60% 도 읽혔다. 배경이 영상으로 바뀌며 밝은 요소가
글자 뒤로 지나가 대비가 무너졌다 — 배경이 움직이는 위에서는 가독성이 조판 장치보다
우선한다. 2톤은 배경이 정적인 최종 CTA 에 남아 있다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 10:52:29 +09:00
2888ff2c28 [feat] landing: 리드 수집 서버리스 함수 + 상담 신청 폼 실전송 연결
랜딩은 ssr:false 정적 빌드라 서버 라우트가 없다. Vercel /api 디렉터리 함수로 받는다.
클라이언트가 같은 오리진(/api/lead)으로 쏘므로 엔드포인트 환경변수도 CORS 도 없다.

api/lead.ts — 리드를 잃지 않는 것이 유일한 책임
  1) 먼저 구조화 로그를 남긴다. 웹훅이 죽어도 Vercel 로그에 리드가 남는다.
  2) LEAD_WEBHOOK_URL 이 있으면 전달(Slack Incoming Webhook·Zapier·자체 API 모두 POST).
     Slack 용 text 와 원본 필드를 함께 실어 수신처를 바꿔도 이 파일을 안 고친다.
  3) 웹훅 실패해도 200 — 1) 에서 리드를 확보했으니 방문자에게 재입력을 시킬 이유가 없다.
     대신 실패는 로그에 크게 남긴다.
  봇 함정(website) 값이 차 있으면 조용히 200. 400 을 주면 어떤 필드가 함정인지 알려주는 셈이다.

contact.tsx — 가짜 성공 제거
  기존엔 setTimeout 1.2초 뒤 성공 화면만 띄우고 아무 데도 보내지 않았다. 화면은
  "접수되었습니다"인데 영업이 받을 리드는 없었다. 이제 같은 함수로 보내고 서버가
  리드를 확보했을 때만 성공을 띄운다. 실패 시 role="alert" 로 안내.

vercel.json — SPA 리라이트가 /api 를 삼키지 않도록 제외.
  "/(.*)" 그대로 두면 /api/lead 요청이 index.html 로 갔다.

데모 요청 모달 카피
  "이름과 이메일만 남겨주시면, 담당자가 일정을 잡아 연락드립니다"
  → "성함과 이메일만 남겨주시면, 사용해보실 수 있는 Demo를 보내드립니다!"
  성공 화면도 같은 약속으로 맞췄다. 폼은 데모를 보낸다는데 성공 화면이 일정을
  잡는다고 하면 방문자가 무엇을 기다려야 하는지 모른다. 라벨도 성함으로 통일.

⚠️ LEAD_WEBHOOK_URL 미설정 시 리드는 Vercel 로그에만 남는다. 로그는 최후의 그물이지
   CRM 이 아니다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 10:48:06 +09:00
c53eeefcf6 [feat] landing: 데모 요청 리드 캡처 + 히어로·데모 카피 교체
카피
- 데모 섹션 헤드라인: "직접 협상해 보세요 / 에이전트는 정해진 기준을 벗어나지 않습니다"
  → "사용할수록 진화하는 에이전틱 협상, / 직접 경험해 보세요!"
  예전 카피는 FAQ 2번("에이전트가 우리 기준을 벗어나 합의해 버리면요?")을 헤드라인으로
  끌어올린 것이었다. 아직 생기지도 않은 반론에 가장 비싼 한 줄을 쓰고 있었고,
  부정문이라 "안 하는 일"을 약속했다 — 체험을 권하는 자리에 맞지 않는다.
  방어는 FAQ 에 그대로 남아 있다.
- 히어로 1차 CTA: "직접 협상해 보기" → "여기서 직접 체험"
- 히어로 2차 CTA: "도입 문의" → "데모 요청". 기존엔 #contact-section 으로 가서
  GNB "상담 신청" 과 목적지가 같았다 — 버튼만 둘이고 경로는 하나였다.

리드 캡처
- DemoRequestModal 신규. 이름·이메일 두 칸만 받는다. 필드가 늘수록 이탈이 늘고,
  영업 첫 연락에 필요한 최소가 그 둘이다. 나머지는 통화에서 채운다.
- 모달로 띄우는 이유: 히어로에서 관심이 생긴 자리에서 받아야 한다. 스크롤로
  내려보내면 그 사이에 식는다. 고관여 폼(상담 신청)은 그대로 두고 그 앞단을 만든 것.
- 접근성: Esc 닫기, Tab 을 모달 안에 가둠, 열릴 때 첫 필드 포커스·닫힐 때 복원,
  aria-modal/labelledby, 배경 스크롤 잠금, prefers-reduced-motion 대응.

전송 경로 — 성공을 위조하지 않는다
contact.tsx 는 1.2초 뒤 성공 화면만 띄우고 아무 데도 보내지 않는다("백엔드 미연결").
리드 제너레이션에서 그 방식은 최악이다. 화면은 접수됐다고 하는데 영업이 받을 리드는
없고, 아무도 그 사실을 모른다. 그래서 lib/lead.ts 는 VITE_LEAD_ENDPOINT 가 없으면
not-configured 를 돌려주고, 모달은 그걸 실패로 처리해 상담 신청 경로를 안내한다.

⚠️ VITE_LEAD_ENDPOINT 를 설정하기 전까지 데모 요청은 저장되지 않는다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 10:40:28 +09:00
f214fccc49 [feat] landing: 히어로 배경을 캔버스에서 루프 영상으로 교체
배경이 설명을 지던 구조를 걷어냈다. 예전 캔버스는 negotium 파이프라인을 직접
그렸지만(입자 240개·6단계 서사·무리 라벨), 이제 배경은 배경으로만 쓴다.
설명은 카피와 아래 섹션이 맡는다.

영상: hero_veo31 0~5초를 크로스페이드 루프로. 그냥 자르면 끝에서 처음으로 튀어서
끝 0.8초를 앞 0.8초와 겹쳐 이음새를 없앴다(결과 4.25초, 첫↔끝 프레임 RMS 18.6/255).
1280x720 h264 631K + 포스터 58K. webm 은 뺐다 — VP9 이 1.0M 으로 h264 보다 커서
둘을 다 실을 이유가 없다.

- CLUSTER_ANCHORS 라벨 제거. 좌표 소스가 사라진 라벨은 화면비마다 어긋나기만 하고
  아무것도 설명하지 못한다.
- DataFlowPhase 를 orbit-ring 으로 이관 후 data-flow-canvas.tsx(500줄) 삭제.
  단계 이름을 실제로 쓰는 건 강화학습 섹션의 순환 링뿐이다.
- 영상은 가로 화면에서만 튼다. 16:9 를 세로에 object-cover 로 깔면 가로가 62% 잘리고
  남은 38% 가 1.7배로 확대된다(848x1220 실측) — 입자가 거대한 보케로 뭉개져 배경이
  아니라 노이즈가 된다. 세로에서는 무대 그라데이션만 남긴다. 모바일 대역폭도 아낀다.
- prefers-reduced-motion 도 같은 경로로 영상을 감춘다. 포스터가 뒤를 받친다.
- 스크림은 색 하나(stage-deep)로 알파만 움직인다. 중간 stop 에서 색을 바꿨더니
  그 지점이 가로선으로 읽혔다.
- 밴드 개념이 사라져 히어로 레이아웃을 카피 중앙 정렬로 되돌렸다. 직전 커밋의
  --band-top 기준 높이 계산은 더 이상 필요 없다.

검증: 1440x860 크롭 7%, 375x812 영상 숨김·제목 헤더 여유 152px, 콘솔 에러 없음.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 10:24:21 +09:00
63717f5eb9 [fix] landing: 무대 위 2차 CTA 가 버튼으로 안 읽힘 — stageGhost 대비 확보
GNB "상담 신청" 이 기본 상태에서 버튼으로 인식되지 않았다. 채움이 없고
테두리는 사실상 안 보여서, 남는 건 흰 글자와 화살표뿐 — 옆 nav 링크와
구분될 근거가 없었다.

테두리를 stage-line(#1E2A52)으로 잡은 게 원인이다. 그 색은 무대 바닥(#070E24)
위 카드용인데, 헤더는 방사형 그라데이션의 광원부(#16205A)에 앉는다. 같은 색
가족이라 하필 헤더 자리에서만 선이 배경에 묻힌다.

  수정 전  테두리 대비 1.09:1  (배경과 사실상 동일)
  수정 후  테두리 대비 3.56:1  (WCAG 1.4.11 UI 경계 3:1 통과)

무대색 토큰 대신 흰색 알파로 잡았다 — 무대 어디에 놓여도 같이 성립한다.
옅은 채움(white/10)도 함께 준다. "누를 수 있는 것"은 테두리보다 면이 더
강하게 전달한다.

히어로 "도입 문의" 도 같은 변형이라 함께 해결된다. 1차 CTA(파란 채움)와의
위계는 그대로다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 09:28:23 +09:00
7f15bc10d1 [fix] landing: 히어로 카피와 입자 밴드 겹침 — 경계를 구조로 강제
버튼 행이 입자 밴드를 파고들었다. 1280x720 에서 34px, 무리 라벨과는 7px.
1440x900 에서만 여유가 있어(45px) 안 보이던 문제다.

원인은 두 값이 서로 다른 기준으로 움직인 것:
  카피 상단 여백  19vh        화면 비율
  카피 높이       351px       고정
  밴드 시작       0.63×높이   화면 비율
카피만 px 고정이라 화면이 짧아지면 밴드가 카피를 덮는다.
0.19H + 351 > 0.63H  →  H < 798px 이면 전부 겹친다.
1280x720, 1366x768 같은 흔한 노트북 높이가 여기 들어간다.

pt-[19vh] 는 위치만 잡을 뿐 경계를 강제하지 못한다. 그래서:
- 카피 영역 높이를 밴드 시작선에 묶었다. 뷰포트와 무관하게 구조적으로 못 겹친다.
- BAND_TOP 을 export 해 캔버스와 카피가 같은 값을 쓴다. 라벨 좌표가 이미 쓰던 방식이다.
- 높이·밴드를 한 식에서 뽑는다. 따로 두면 어긋난다 — 밴드를 63vh 로 두고 높이만
  키웠더니 짧은 화면에서 변수가 실제 밴드보다 위를 가리켰다.
- min-h 바닥 780px. 밴드 위 공간에 헤더(104)+카피(351)+여유(24)=479px 가 들어가려면
  높이 760px 이 하한이라, 정렬만으로는 짧은 화면을 못 구한다. 살짝 스크롤될 뿐이다.
- pt-[104px] 를 md 이상에서도 유지. 빼면 가운데 정렬이 카피를 fixed 헤더 밑으로 민다.

검증(실측, 음수 = 밴드 밖):
  1280x620   +20 → -30,  헤더 여유 +12,  라벨 75px
  1280x720   +34 → -30,  헤더 여유 +12,  라벨 75px
  1440x900   -45 → -68,  헤더 여유 +50,  라벨 122px
  375x812          -57,  헤더 여유 +6

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 09:22:54 +09:00
민헌
7b1bcb6d24 docs(lps): README 에 크롤·매칭·IP 로테이션 실제 방식 추가 + 낡은 서술 정정
"어떻게 동작하나"가 워크플로우 그림 한 장뿐이라, 정작 이 시스템에서 어려운 세 가지
(안티봇 뚫는 법·같은 상품 고르는 법·IP 돌리는 법)를 README 만 봐서는 알 수 없었다.
기존 스타일(표·ASCII·비개발자 설명)을 유지해 '🔬 안을 열어보면' 절을 추가한다.

- 소스별 크롤: 네이버(모바일 msearch·WTM)와 쿠팡(Akamai)의 **정반대 전략**을 표로 대조.
  네이버는 리소스 차단을 끄고(요청 가로채기 자체가 탐지 신호) 한국 IP+ko-KR 로케일이 필수,
  쿠팡은 리소스를 막아 대역폭을 줄인다. 스크롤은 횟수가 아니라 '안 늘어남'이 종료 조건인 이유도.
- 같은 상품 판정: 필터 3단계 + AI 판정, '무시할 차이 / 불일치로 볼 차이' 기준표,
  후보를 10건씩 쪼개야 하는 이유(37건 일괄 → 전멸).
- IP 로테이션: 포트=sticky 세션, 게이트웨이 2개, DB 장부(SKIP LOCKED·LRU),
  포트 3상태(임대/휴식/쿨다운), 회전 계기 4종, **태우지 않는 경우**(구조적 차단·서킷브레이커·
  확신 없는 0건)와 예산을 IP 기준으로 세는 이유.

낡은 서술 정정:
- "네이버 쇼핑 API" → 오픈API 는 2026-07-31 종료, 지금은 둘 다 크롤
- 테이블 5종 → 6종(proxy_port), 알림 10룰 → 11룰(fatal_block)
- 요청 예산 "기본 3회" → 쿠팡 3·네이버 10
- config 설명 "배포는 env 주입" → 실제로는 config.local.toml 마운트, env 는 DB 접속점만
- 폴더 구조에 crud/port_lease·profile_slot 추가

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 17:23:23 +09:00
민헌
377389f495 fix(lps): IP 로테이션 안정성 검수 — 예산이 안 먹던 근본 원인 + 차단 시 풀 소각 차단
크롤·IP 로테이션을 검수하며 찾은 결함을 순서대로 고쳤다. 의심 지점은 모두 실제 코드 경로로
재현해 확인했다(브라우저·네트워크만 mock, 프록시·DB 장부는 실물).

**① 요청 예산이 사실상 발화하지 않았다 (핵심)**
유휴 정리(close_if_idle, 120s)는 브라우저만 닫고 임대는 두는데, 재기동 때마다 _ip_requests 를
0 으로 되돌렸다. 게다가 ensure_port 의 renew 가 임대 만료를 계속 뒤로 민다 — 검색이 유휴
임계보다 뜸하고 임대(10분)보다 잦으면 **한 IP 에 영원히 고정**된다(실측: 6회 검색이 전부 같은
포트·ip_req#1). 연속 검색에서는 정상 동작해 부하 테스트로는 안 잡히고, 수동 트리거처럼
드문드문한 실사용 패턴에서만 깨진다.
파급이 하나 더 있다 — bot_detection.ip_request_no 가 항상 1 로 찍혀, operations.md 가 명시한
'1 위주면 IP 평판 / 2 이상이면 예산 하향' 진단이 통째로 무너진다. 과거 "전량 ip_req#1 이라
IP 평판 문제" 결론은 이 착시일 수 있다(문서에 경고 추가).
→ IP 세션 상태를 브라우저 수명과 분리. **포트가 실제로 바뀔 때만** 리셋한다(_begin_ip_session).
   세션 종료 기록도 포트 변경·최종 close 시점으로 옮겼다(idle 사유 소멸).

**② 환경 차단이면 회복 못 하는데 풀을 계속 태웠다**
쿠팡은 fatal 마커가 없어 컨테이너 차단 같은 '회전 무효' 상황을 구분 못 했다. 실측으로
웜업 6포트 + 잡 1건당 6포트를 30분 쿨다운에 묶어 **잡 16건이면 100포트 고갈**. 실제 장부에도
9분간 11포트 연속 소각 이력이 남아 있다(gate 사용 21 / 소각 14).
→ 서킷브레이커: **서로 다른 IP 가 연속 3개 모두 첫 요청부터** 막히면 IP 문제가 아니라고 판정,
   태우기를 멈추고 fatal 로 알린다(env_block 마커 → 기존 fatal_block 알림이 집계).
   같은 IP 반복 차단·뒤쪽 요청 차단은 세지 않는다. 성공 1회로 자동 해제(타이머 불필요).
   결과: 전면 차단 시 소각이 판정 근거 2개에서 멈춘다(웜업 6→0, 잡 6→0).

**③ 종료가 임대를 반납하지 않았다**
close() 후에도 leased_until(최대 10분)까지 그 IP 를 아무도 못 썼다 — 재시작이 잦을수록 가용
풀이 줄었다. DecodoProxy.release() 추가, close() 에서만 호출(유휴 정리는 웜 쿠키·예산 유지를
위해 그대로 둔다).

**④ 시간창 재기동이 IP 를 안 바꿨다** — 로그만 'IP 회전'이었고 renew 로 같은 포트를 붙잡았다.
sticky 수명이 끝나면 같은 포트라도 IP 가 바뀌므로 명시적으로 놓아준다.

**⑤ '검색결과 없음'을 차단으로 오인해 IP 를 태울 수 있었다**
네이버 무결과 페이지 크기는 실측된 적이 없는데 short_html 폴백이 이를 차단으로 본다.
확신도로 대응을 갈랐다 — 알려진 마커만 태우고/서킷브레이커에 세고, 미지의 짧은 HTML 은
회전·재시도까지만. 판단 근거는 bot_detection 에 계속 쌓이므로 나중에 임계를 실측할 수 있다.

**⑥** available_ports() 가 장부 모드에서 늘 최대값을 반환하는 점을 문서화(관측 경로는 미사용).
세션 마감을 멱등하게 만들어 close() 중복 호출 시 이중 기록 방지.

테스트 14건 추가(전체 251 passed). mock 하니스도 실물을 타도록 고쳤다 — 회전 시 포트가 실제로
바뀌고, 재기동 판단·IP 세션 경계는 실제 코드를 그대로 쓴다(고정 포트 mock 은 이 버그를 못 봤다).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 17:09:01 +09:00
d3bcc61609 [chore] landing: Vercel 배포 설정 추가
랜딩은 ssr:false + prerender 인 SSG 라 정적 산출물만 서빙하면 된다.
프레임워크 자동 감지에 맡기면 React Router 를 SSR 로 잡을 수 있어
framework:null 로 명시하고, outputDirectory 를 build/client 로 고정한다.
SPA 리라이트는 Dockerfile.prod 의 nginx try_files 와 동등하게 맞췄다.

.vercel 은 gitignore. 프로젝트·조직 ID 가 들어가는 로컬 링크 파일이다.

배포 메모:
- 저장소가 Gitea 라 Vercel Git 연동(푸시 시 자동 배포)은 안 된다. CLI 수동 배포.
- 새 프로젝트의 첫 배포는 --prod 없이도 production 으로 승격되니,
  preview 를 원하면 --target=preview 를 명시할 것.
- preview 배포는 Vercel 이 x-robots-tag: noindex 를 자동으로 붙인다.
  색인 차단 목적으로 public/robots.txt 를 건드리지 말 것 — 출시 때 되돌리는 걸 잊는다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 16:36:40 +09:00
55e3ab02aa [fix] landing: 섹션 세로 리듬 통일 — 간격을 SectionHeading 이 소유
디자인 시스템이 색·활자만 소유하고 간격은 각 섹션 파일에 흩어져 있어,
같은 관계에 값이 여러 개 생겼다. 스크롤할 때 섹션마다 호흡이 달라지던 원인.

  eyebrow → title      12/20px      → 16px
  title → description  16/20/32px   → 20px (display 티어 32px)
  머리 → 본문          48~96px 5종  → 56px / 80px(md)
  컨트롤 → 본문        56/64px      → 48px
  리드 → CTA           56/48px      → 48px

- SectionHeading 이 리듬을 소유하고 HEADING_GAP·CONTROL_GAP·CTA_GAP·
  DISPLAY_LEAD_GAP 를 노출. 섹션 파일에서 mb-24 같은 값을 직접 쓰지 않는다.
- negotiation-demo·final-cta 가 Section/SectionHeading 을 우회해 머리를 직접
  조판하고 있었다 — 아이브로우 간격이 이 둘만 20px 이던 원인. 프리미티브로 환원.
- SectionHeading 이 eyebrow 없이도 빈 span 을 렌더해 유령 여백이 생기던 버그 수정.
- descriptionClassName 의 text-[17px] 중복 선언 제거. lead 변형이 이미 갖고 있는
  모바일 축소 단계(text-[16px] sm:text-[17px])를 지우고 있었다.
- reinforcement: text-balance 가 쉼표를 넘어 끊던 헤드라인을 <br /> 로 명시.

reinforcement 만 gap="none" 인데, 머리가 그리드 셀 안이라 마진이 상쇄되지 않아
items-center 정렬을 밀기 때문. 간격은 그리드 래퍼가 진다.

검증: tsc --noEmit 통과, 프로덕션 빌드 통과. DOM 실측으로 375/868/1440px
전 구간에서 머리→본문·eyebrow→title·컨트롤→본문 값 일치 확인.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 16:22:24 +09:00
a416668256 [feat] negodata/front: 설정 화면 회사/개발자 분리 + 카드 변수 별칭·미리보기 정비 + 네고율 공통화
- 설정 분리: /settings(최고관리자)는 공급사에게 보이는 브랜딩·안내 문구만, /dev/settings(개발자 신설)는
  협상 동작·데이터 구조를 바꾸는 탭(용어·커스텀 필드 등) — 화면에 보이는 것과 동작을 바꾸는 것을 관리 주체로 가름
- 카드 변수: prev_customer_price(당사 직전가) 칩 추가, 시드/구버전 별칭(anchoring_price 등)은
  툴바 미노출·인식만 지원. SlateRenderer 는 평문에 박힌 미주입 토큰도 카탈로그에 있으면 한글 라벨로 표기
- 견적상세 협상대화의 사용 카드 미리보기를 서식 렌더(renderCardScriptPreview)로 — 마커 원문 노출 제거
- 네고율 차감을 매입가 전용 → 매입가·판매가 공통으로(문구·주석 정리)
2026-08-05 16:00:31 +09:00
54f8f7a6a1 [feat] negodata/front·agent: 카드 제시 가격 변수 선택 + 종결 전용 토글 와일드카드 한정
- 카드 상세 "협상 전술"의 제시 가격을 표시 전용 → 셀렉트로: 기본 '자동(멘트의 마지막 가격 변수)',
  가격 변수가 여럿인 카드만 명시 선택이 의미. 선택지는 멘트에 실제 꽂힌 변수로 제한하고
  멘트 수정으로 선택 변수가 사라지면 자동으로 리셋 — 문구≠계산 어긋남 원천 차단.
  명시 선택 시에만 tactic.offer_variable 저장(agent build_card_spec 이 파싱보다 우선 적용, 기존 경로).
- 종결 전용 토글은 와일드카드 폼에만 노출 — 종결 국면이 와일드카드 목록에서만 카드를 뽑으므로
  협상카드에 켜면 어느 경로에서도 발동하지 않는 죽은 카드가 된다. 협상카드는 저장 시 항상 false.
- 퍼즈 하네스 100케이스로 확대(시드 고정) — 100/100 불변식 위반 0(타결 94/결렬 6).
- override 우선순위 단위 테스트 추가(자동=마지막 변수, tactic.offer_variable 지정 시 지정 변수).

검증: agent 179 통과 · front tsc+eslint 통과 · 랜덤 협상 100회 완주 위반 0
2026-08-05 15:58:23 +09:00
민헌
042e2d3012 docs(lps): 2026-08-05 세션 기록 — 변경 배경·결정 근거·미해결·다음 할 일
다음 세션이 이어받을 수 있게 남긴다.
- docs/2026-08-05-session-notes.md 신규: 왜 네이버가 크롤로 갔는지, 이번에 정한 것
  (추천순 유지·순위는 상품가·신뢰 신호·AI 배치 10건)과 그 근거, 미해결(컨테이너 크롤 차단),
  다음 할 일 5가지, 작업 시 주의(테스트가 실DB를 TRUNCATE 한다)
- architecture.md: IP 회전을 DB 장부·게이트웨이 2개·회전무효 차단 구분·AI 배치로 갱신
- database.md: proxy_port 테이블 추가(6종), price_history 의 신뢰·배송 컬럼 설명과
  '미검증 오퍼 비율' 쿼리
2026-08-05 15:51:19 +09:00
민헌
b2b5708a6a fix(lps): AI 매칭이 후보 많으면 통째로 무너지던 문제 — 배치 분할 + 규칙 충돌 제거
A/B 실험(상품 8개)에서 2개가 매칭 0~1건으로 사실상 실패했다. 수집은 40건 정상, 가격도
정확했는데 판정이 못 찾았다. 파고드니 두 가지였다.

**① 일괄 판정 붕괴(핵심)**: 후보 37건을 한 번에 넣으면 gpt-4o-mini 가 전 항목에 같은 점수(70)를
매기고 **전부 불일치**로 답한다. temperature=0 에서 3회 재현. 10건씩 쪼개면 같은 모델·같은
입력으로 12건이 매칭된다. 상품 3종에서 모두 재현(크리넥스 0→12, 다우니 8→23).
→ _BATCH=10 으로 쪼개 병렬 판정 후 전체 index 로 복원. 지연은 배치 1개분, 토큰은 합산.

**② 프롬프트 규칙 충돌**: "포장(개수/박스) 차이는 동일 상품" vs "용량·규격이 다르면 불일치" 가
함께 있어 '30롤 1팩'과 '30롤 2팩'(=60롤)을 어느 쪽으로 볼지 정할 수 없었다. 최저가 관점에선
수량이 다르면 다른 상품이다(60롤 값을 30롤 최저가로 쓰면 왜곡). 종류 혼동도 실측에서
오탐을 만들었다 — '프라이팬'을 찾는데 볶음팬·웍팬이 매칭됐다.
→ 규칙을 '무시할 차이 / 불일치로 볼 차이'로 갈라 재작성(수량·종류 명시).

결과(캐시 후보 재판정):
  크리넥스 30롤   매칭 0 → 5건, 최저가 **not_found → 15,500원**
  다우니 4.1L    매칭 8 → 12건, 최저 21,890 유지
  테팔 프라이팬   매칭 2 → 0건 (볶음팬·웍팬 오탐 제거 — 후보에 진짜 프라이팬이 없었다)

테스트 6건 추가(배치 분할 시 index 매핑·누락 보수처리·토큰 합산·빈 후보·상수 상한·규칙 충돌),
전체 237 passed.
2026-08-05 15:45:32 +09:00
민헌
8574df4c16 feat(lps): 배송 표기 원문 기록 — 순위는 상품가 유지, 배송은 근거로 남긴다
배송 주체가 다르면(쿠팡 로켓 / 판매자로켓 / 네이버 판매자) 배송비 숫자만으로는 비교가
무의미하다. 그래서 **순위는 지금처럼 상품가**로 두되, 배송 정보는 사후 판단이 가능하도록 남긴다.

실측 조사(섬유유연제·생수 2L, 두 소스):
  네이버  배송비무료 / 배송비3,000·3,900·4,500·5,000·8,800·9,000·10,000원 /
          내일배송 8.6.(목) 도착 · 오늘출발 · 빠른배송 / **배송비포함 혜택가 N원**(가격비교 카드)
  쿠팡    내일(목) 도착 보장 · 와우는 무료배송 ∙ 무료반품 ∙ 새벽도착 / 무료배송 ∙ 오늘출발 /
          모레(금) 도착 예정. 뱃지=logo_rocket_filter(로켓)·logo_rocket_merchant(판매자로켓)

- NormalizedProduct.shipping_label: 화면 문구 원문. 같은 '무료배송'이어도 주체·조건
  (와우회원·최소금액·새벽도착)이 다른데 숫자·분류로는 그게 사라진다
- 네이버: 배송비 문구 + 도착 정보를 잇는다. **_prices() 안에서 뽑는다** — 가격 노드에서
  배송비를 decompose 하기 전에 읽어야 해서(나중에 추가했다가 가격비교 카드에서 라벨이 통째로 빔)
- 쿠팡: 배송 문구가 유틸리티 클래스(fw-text-[14px])에 담겨 셀렉터로 못 집는다 → 텍스트 패턴으로
  조각을 모으고, 구분자 없이 붙은 상위 컨테이너("내일(목) 도착무료배송")는 조각 2개 이상을
  품은 것으로 판별해 버린다. selectolax 의 node.css("*") 가 자기 자신을 포함해
  '자손 매칭' 방식은 못 쓴다(실측)
- price_history.final_shipping_fee/type/label 추가(+마이그레이션). fee 는 0=무료,
  NULL=미확인(로켓 조건부) — 둘은 다른 뜻이라 기본값을 두지 않았다

교차 검증(파싱값 vs 카드 원문, 6개 규칙): 네이버 40건·쿠팡 40건 **불일치 0**.
테스트 3건 추가, 전체 231 passed.
2026-08-05 14:40:00 +09:00
c56cf8e3af [feat] agent: 타결선을 목표가 → 타결 상한가로 — 목표가 초과 낙찰 허용(IMK 0803 ②)
목표가를 1원이라도 넘으면 결렬되던 탓에, 기존 단가보다 인하됐는데도 결렬되는 케이스가 있었다
(EST-202607-973E: 기존 17,500 / 목표 16,980 / 최종 17,300). 견적 생성 시 세션에 박제해 두던
done_ceiling_price(목표가×(1+타결상한율), 세팅 기본 +5%)를 협상 엔진이 실제로 읽게 배선했다.

- tactics: settle_ceiling() 신설 — 타결선 판정을 한 곳으로. 박제가 없는 옛 세션·데모는 목표가 폴백
- 카드 제안가 유효조건의 상한도 목표가 → 타결 상한가 (받아줄 수 있는 금액까지는 부를 수 있어야 함)
- _render 가드레일이 목표가 초과 성공을 결렬로 되돌리고 있어 같이 상한 기준으로 교정 —
  타결 판정만 고치면 이 가드에서 다시 뒤집혀, 배선했는데도 결렬로 떨어졌다
- crud/loader/세션 컨텍스트에 done_ceiling_price 적재

검증(목표가 956,580 · 상한 1,004,410): 950,000·1,000,000·1,004,410 타결 / 1,004,500·1,010,000 결렬.
agent 테스트 178건 통과.
2026-08-05 14:10:38 +09:00
민헌
6c7ff5af51 feat(lps): 최저가 오퍼의 신뢰 신호(평점·리뷰) 수집 — 네이버·쿠팡 공통
'가장 싼 값'과 '실제로 살 수 있는 가장 싼 값'은 다르다. 리뷰·평점이 전혀 없는 오퍼는
재고 없는 미끼가격일 수 있고, 그걸 최저가로 보고하면 사용자는 그 가격에 살 수 없다 —
조금 비싼 정답보다 나쁘다. 판단 근거를 수집해 둔다.

- NormalizedProduct.rating / review_count 추가. 두 소스 모두 카드에 노출하는 값만 담아
  교차 비교가 되게 했다. **없으면 None 유지** — '리뷰 0개'와 '리뷰 정보 없음'은 다른 뜻이다
- 네이버: product_grade 의 <strong>평점</strong><em>리뷰수</em>. 텍스트를 통째로 정규식
  돌리면 '평점4.7473' 이 4.74/73 인지 4.7/473 인지 못 가르므로 노드로 분리해 읽는다.
  '1.7만' 같은 축약은 parse_ko_count 로 푼다(그대로 int() 하면 1 이 된다)
- 쿠팡: 별점은 채워진 별 개수가 아니라 컨테이너 aria-label 에, 리뷰 수는 괄호 텍스트에 있다
- price_history.final_rating/final_review_count 추가(+마이그레이션) → "리뷰 0인 최저가가
  몇 %인가"를 SQL 로 물을 수 있다. NULL 과 0 을 구분해야 해서 기본값을 두지 않았다

정렬 점검(사용자 제기): 두 소스 다 정렬 파라미터 없이 **랭킹/추천순**이다(픽스처 가격이
오름차순이 아님으로 확인). 가격순(sort=price_asc)은 차단 없이 동작하고 실측상 더 싼 후보를
찾지만(15,400→10,900), 리뷰·평점 없는 유령상품을 위로 끌어올려 미채택 — 추천순 유지.
신뢰 신호가 쌓이면 "리뷰 N 이상" 가드를 걸고 가격순을 켜는 선택지가 열린다.

e2e: TR-1/TR-2 최저가에 평점 4.89·리뷰 7,314/102,000 이 함께 기록됨. 테스트 5건 추가, 228 passed.
2026-08-05 14:00:12 +09:00
민헌
5c9567919f feat(lps): 네이버 회수율 48→120건(적응형 스크롤) + 크롤 차단을 기동 즉시 알림
**회수율**: 베이스 _wait_ready 는 '고정 3회 스크롤 → 셀렉터 대기' 순서라, 프록시 지연이 있으면
**아직 아무것도 안 그려진 화면을 스크롤**하고 끝났다. 네이버용으로 순서를 뒤집고 종료 조건을
횟수가 아니라 '카드 수가 더 안 늘어남'으로 바꿨다 — 네트워크가 느리든 빠르든 같은 결과가 나온다.
A/B(같은 IP·같은 세션, 3개 쿼리): 14·14·20 = 48건 → **40·40·40 = 120건**(전부 상한 도달).

**크롤 프리플라이트**: 웜업 대상에 naver 를 추가하고, 3회 모두 실패하면 로그가 아니라 **알림**을
쏜다. 컨테이너 워커는 크롤이 막혀도 하트비트가 살아 있어 healthy 로 보이고, 잡이 DEAD 로
쌓일 때까지 아무도 모른다(실측). 성공하면 해소 알림으로 자동 정리된다.
AlertManager 를 main 에서 만들어 웜업·ops 모니터가 쿨다운 상태를 공유한다.

**문서**: operations 에 차단 마커별 대응표(비정상적인 접근=구조적/wtm_captcha=회전)와
'컨테이너 크롤 차단' 절 추가 — 배제한 원인, Rosetta 에뮬 주의(= '이 맥에서만'일 수 있음),
배포 시 확인 순서(warmup 로그 → 호스트 비교 → 워커만 호스트 실행).

테스트 3건 추가(웜업 실패 알림·성공 해소·비크롤 소스 스킵), 전체 223 passed·0 failed.
2026-08-05 13:33:19 +09:00
민헌
b7fc327779 refactor(lps): 사문화된 네이버 오픈API 어댑터 제거 + 낡은 테스트 계약 갱신
**낡은 테스트**: test_handler_skips_record_on_negative_cache_hit 는 '캐시 히트면 이력을
남기지 않는다'를 검증했는데, 그 동작은 실측 버그였다 — 잡은 DONE 인데 price_history 에
새 행이 없어 이를 폴링하는 소비자(negodata 최저가 모달)가 결과를 영영 못 받고 로딩만 돌았다.
코드는 이미 '캐시 히트도 이 잡의 결과이므로 기록한다'로 고쳐져 있었고 테스트만 남아 있었다.
→ 현재 계약(not_found 스냅샷 1건 기록, 가격은 null)을 검증하도록 다시 씀. 전체 220 passed·0 failed.

**오픈API 어댑터 제거**: shop.json 이 2026-07-31 종료돼 404 SE05 만 반환하고, 파이프라인은
naver_shop(크롤)로 옮겨 갔다. 되살릴 수 없는 코드를 남겨두면 다음 사람이 "키를 넣으면 되나"
하고 시간을 쓴다.
- services/search/naver/ (adapter·transform) 삭제
- NaverConfig 모델·로더·설정 섹션 3개 파일에서 제거(죽은 키)
- test_naver_transform 삭제, test_alerts 는 NaverAdapter 대신 스텁 사용
  (검증 대상인 recent_stats/_note_result 는 베이스 SearchAdapter 계약이라 무관)

**문서 정합화**: architecture(네이버 안티봇=WTM, 통과 3조건) · api(배송비가 이제 채워짐,
가격은 즉시판매가·쿠폰가 제외) · operations(kr_host·naver_ip_request_budget) · README 트리.

source 이름 "naver" 는 그대로다 — price_history·by_mall·프론트 계약은 구현 교체와 무관하다.
2026-08-05 13:26:10 +09:00
민헌
18fe18dd39 feat(lps): 회전으로 못 고치는 차단을 따로 구분 — 태우지 않고 즉시 실패 + 설정 알림
네이버 차단은 두 종류인데 지금까지 똑같이 '회전 후 재시도'로 처리했다(실측):
  비정상적인 접근  2.6KB  해외 IP — 게이트웨이 국가가 틀림. IP 를 바꿔도 결과 동일
  wtm_captcha    47~63KB IP 평판·세션 — 회전으로 회복 가능

전자를 회전시키면 100포트를 순서대로 태우기만 하고, 더 나쁘게는 **같은 게이트웨이를 쓰는
쿠팡의 풀까지 30분씩 말린다**(kr_host 를 비우면 네이버가 gate 로 폴백하므로 실제로 일어난다).

- AdapterError.fatal: 재시도해도 안 되는 구조적 실패 표시
- BrowserSearchAdapter.fatal_block_markers: 걸리면 포트를 태우지 않고 회전도 없이 즉시 실패.
  감지 기록(bot_detection)은 남긴다 — 알림이 그걸 센다
- NaverShopAdapter.fatal_block_markers = ("비정상적인 접근",)
- ops 알림 'fatal_block': 해당 마커가 1h 내 1건만 나와도 발화(자연 회복이 없어 방치하면
  그 소스는 계속 0건이다). BotDetectionLog.recent_count_by_marker 추가

라이브 검증: 일부러 해외 게이트웨이로 네이버 검색 → fatal=True 로 즉시 실패,
쿨다운 증가 0(태우지 않음), ERROR 로그에 원인·조치(kr_host 확인) 명시.
테스트 3건 추가(태우지 않음·회전 없음 / 기록은 남김 / 일반 차단은 기존대로), 전체 220 passed.
2026-08-05 13:22:37 +09:00
6f5d69b5cb [feat] agent·negodata: 협상카드 전술 데이터화(tactic JSONB·스크립트 파싱) + 발동규칙 정비
IMK QA 2건(BB9A 카드 중복·8AB0 중간값 오계산)의 근본 원인이 전술 하드코딩(_TACTICS 번호 매칭)이라
전술을 데이터로 옮기고, 발동을 유효성 검사로 바꿨다. 전술 정본 = 카드 스크립트의 마지막 가격 변수(파싱),
문장으로 알 수 없는 운영 규칙(closing·min_round)만 card.*.tactic JSONB. 세션 시작 시 card_specs 스냅샷 박제.

발동 유효성(하나라도 걸리면 그 라운드 미발동 — 클램프 폐지):
목표가 초과 / 협력사 제시가 이상 / 당사 직전 제안 미만(역행 금지, IMK 논의) / 재료 결측 /
필수 변수 결측(시장가 카드 requires — 토큰 노출 방지) / 이미 쓴 카드(played_card_numbers 공용 이력)

- agent: 와일드=비종결·종결=전용 풀 분리(같은 카드 2회 구조적 차단), 발동 시 자기 제안가 기록(절충가 수렴),
  진입 존 프로브(빈 덱 재사용 교착 방지), 낼 카드 전무 시 소진→종결, 무효 금액 카드는 설득 폴백도 금지(playable),
  에디터 anchor_price 별칭 등록, 에러 재렌더 변수 치환
- backend: 카드 사용 기록을 step 휴리스틱→번호 prefix 판정(종결 발동 card:null 누락 해소)
- negodata: 카드 상세 "협상 전술" 섹션(제시 가격 파싱 표시·종결 전용·최소 라운드) + tactic API 배선
- postgres-init: tactic 컬럼·시드(WC-03/05 closing, WC-04 min_round 2), 멱등 alter 로 dev 정본화
  (번호 WC-0x 정규화, WC-01·03·NGC-010 구멘트 교체, WC-05 변수 middle_price 교정)

검증: agent 178 통과 · 시나리오 하네스 14케이스(BB9A·8AB0·역행 실수치 재현) · 랜덤 퍼즈 50협상 불변식 위반 0
(불변식: 카드 중복 금지·종결 카드 자리·타결가≤목표가·표시가=타결가·토큰 잔존 금지·종료 보장)
2026-08-05 11:48:14 +09:00
71ef28110b [feat] negodata·agent·negosium: 협상 기준가 회사별 선택(공급가/매입가) + 공급사 포털 안내 회사설정화
어느 가격이 "공급사에 지불하는 단가"인지는 회사마다 달라, 컬럼을 합치는 대신 회사가 고르게 했다.
판정은 settings.features.nego_baseline_field 1순위, 미설정 회사는 price 만 숨겼으면 purchase_price 폴백.
판정식 정본은 negodata/backend/common/nego_baseline.py (agent·negosium backend 가 같은 규칙 미러).

- negodata front: 회사 설정에 협상 기준가 라디오(선택지마다 실제 나갈 문장 미리보기 + 학습 경고),
  '공급사 포털 안내' 탭 신설(협상 유의사항·헬프데스크 연락처), 용어 카탈로그에 target_price·supplier 추가.
  인터넷 최저가는 숨김 대상에서 제외(신규 견적의 유일한 목표가 후보).
  상품 등록 기본값에서 개발용 더미 제거(price 1,000,000·PROD-BAT-###·800,000·대한민국·10 EA·14).
- agent: get_item_price → get_item_baseline (기준가·호칭·회사 용어사전을 한 쿼리로),
  "기존 공급가 대비" 하드코딩을 회사 용어로 치환 + 받침 기준 조사 자동 보정,
  협상 스크립트 4종의 협력사·목표가·배송형태 용어를 {label_*} 토큰화, input_options 도 변수 치환 적용.
- negosium: 협상 화면 기준 단가·배송형태 라벨을 회사 설정 기준으로, 유의사항 본문과 헬프데스크 연락처를
  하드코딩에서 회사 설정으로(미등록 시 영역 숨김). 유의사항의 VAT·배송비 문구 삭제(IMK 0803 ⑥).
- negodata backend: LPS 검색 가격 힌트를 items.price 고정에서 기준가 규칙으로.

검증: 기준가 설정 3 × 숨김 4 = 12조합에서 협상 멘트·포털 표시·LPS 힌트가 전부 일치.
agent 테스트 176건 통과. 보고서 negodata/docs/nego-baseline-verification.md
2026-08-05 11:47:06 +09:00
민헌
3b345929d4 feat(lps): 포트(IP) 장부를 DB 로 올려 멀티 프로세스에서 안전하게 — 프로필 슬롯도 분리
한 DECODO 계정을 여러 워커 프로세스가 나눠 쓰는 전제로 전환한다. 인메모리 장부는
프로세스마다 따로라 (1) 같은 IP 를 동시에 잡고 (2) 한쪽이 태운 IP 를 다른 쪽이 곧바로
집으며 (3) 재시작하면 쿨다운이 통째로 사라졌다.

proxy_port 테이블 = 단일 진실. 상태는 세 시각으로만 표현한다(leased/rest/cooldown_until).
- acquire: 한 UPDATE 안에서 FOR UPDATE SKIP LOCKED 로 후보를 잠그고 임대까지 끝낸다
  (잡 큐와 같은 방식 — SELECT 후 UPDATE 로 나누면 그 틈에 다른 프로세스가 같은 행을 집는다)
- 회전은 LRU(last_used_at). 프로세스가 몇 개든 '가장 오래 안 쓴 IP'를 집으므로 전체가
  자연히 한 바퀴씩 돈다 → 프로세스별 seed_offset 계산 제거
- 죽은 프로세스 회수: leased_until 만료로 자동 복귀(별도 reaper 불필요)
- 차단·휴식은 전역이라 재시작해도 유지된다

DB 왕복은 비동기라 검색 루프(동기)에서 곧바로 못 한다 → 회전·차단을 pending 에 적어두고
ensure_port(브라우저 재기동 직전, async)에서 한 번에 flush. _close_ctx 에서도 flush 해
종료 시 유실(=태운 IP 를 남이 그대로 집는 상황)을 막는다.

**프로필 슬롯**(services/search/profile_slot): Chrome 은 user_data_dir 당 1 인스턴스다.
예전엔 워커 인덱스로만 갈라서 프로세스 2개면 같은 경로를 잡아 두 번째가 통째로 죽었다
(실측: 잡 3건 중 2건 DEAD, TargetClosedError). 파일 락으로 슬롯을 선점한다 — PID 경로가
아니라 슬롯이라 재시작 시 재사용돼 웜 쿠키(cf_clearance·Akamai)를 버리지 않는다.

검증: 프로세스 2개 동시 acquire 20회 → 중복 배정 0건. 워커 2프로세스 e2e → 잡 3건 모두
DONE(네이버가 삼다수 최저가 획득 8,960 < 13,200). 테스트 14건 추가, 전체 217 passed.
2026-08-05 11:44:54 +09:00
민헌
495e16b951 feat(lps): IP 를 태우지 말고 한계 직전까지 쓰고 쉬게 — 네이버 예산 상향 + 휴식 개념
기존엔 네이버가 쿠팡 기준 예산(3회)을 그대로 썼다. 실측하니 체급이 다르다:
같은 KR IP 로 **12회 연속 검색까지 무차단**(IP 4개 전부 한계 미도달). 3회로 돌리면
불필요하게 4배 자주 회전해 KR 풀만 빨리 소모하고 회전마다 브라우저 재기동(~20s)이 붙는다.
→ [DecodoConfig].naver_ip_request_budget = 10 (실측 12 에 여유). 쿠팡은 3 유지.

그리고 선제 회전에 빠져 있던 조각을 채웠다 — **휴식(rest)**:
예산 도달로 놓은 포트를 곧바로 다른 워커가 집으면 그 IP 의 요청률이 도로 올라가
예산의 의미가 사라진다. release(rest_sec=...) 로 sticky 수명만큼 쉬게 한다.
차단으로 태우는 burn(30분)과는 별개 상태다:
  휴식  탄 게 아님 · 짧음 · 소진 시 가장 먼저 회수
  쿨다운 차단당함 · 김 · 휴식보다 나중에 회수
회전 종류(kind)를 browser_base → DecodoProxy.rotate(kind) 로 전달해 budget 일 때만 휴식을 건다.

라이브 검증(예산 3으로 낮춰 관찰): 6회 검색 = IP 2개만 사용, 3회마다 선제 회전,
놓은 포트는 휴식 1 · 쿨다운 0 · 차단 0. 즉 IP 를 태우지 않고 로테이션만으로 돌아간다.

테스트 6건 추가(휴식 재사용 금지·만료 복귀·burn 우선·회수 우선순위·budget vs block),
전체 202 passed. _MockProxy.rotate 가 kind 를 받도록 갱신.
2026-08-05 11:16:37 +09:00
민헌
f80cece9c1 feat(lps): 프록시 포트(IP 세션) 공용 중재자 — 소스·워커 간 IP 충돌·재사용 방지
한 DECODO 계정으로 성격이 다른 두 풀을 쓰게 됐다:
  쿠팡   gate.decodo.com  국가 무지정(실측 VN·MY·BD·ID·KZ·IN·PH)
  네이버 kr.decodo.com    한국 전용(LG U+·KT·SK브로드밴드)
네이버는 해외 IP 를 즉시 하드차단한다 — 같은 포트 10091 에서 gate=차단(2,641B) /
kr=정상 14건. 같은 포트 번호라도 게이트웨이가 다르면 IP 가 다르다
(port 10061 → gate=103.99.27.55(ID) / kr=121.180.128.2(KR)) → 자원 키는 (host, port).

PortRegistry 가 프로세스 전체의 포트를 중재한다:
- 배타 임대: 한 (host,port) 는 동시에 한 소유자만. 워커 N개가 회전하다 같은 IP 로
  수렴해 한 IP 에 요청이 몰리던 문제를 없앤다(seed_offset 은 시작점만 벌렸다)
- 전역 쿨다운: 누가 태웠든 만료 전까지 아무도 못 집는다(예전엔 쿨다운이 프록시
  인스턴스별이라 다른 소스가 곧바로 재사용했다)
- 리스 만료 = sticky 수명(session_minutes) → 자동 반납 후 새 IP
- 전 포트 소진 시 가장 빨리 풀릴 포트를 회수(멈추는 것보다 낫다)
- snapshot() 을 ops 알림 페이로드에 실어 게이트웨이별 보유/쿨다운/소유자를 남긴다

DecodoProxy 는 registry 주입 시에만 임대 경로를 타고, 미주입이면 기존 동작 그대로다
(단독 사용·기존 테스트 경로 보존).

검증: 워커 2개 동시 실행 e2e — 쿠팡·네이버 병렬 수집, 삼다수에서 네이버가 최저가
획득(naver 8,960 < coupang 13,200). 테스트 14건 추가, 전체 196 passed.
2026-08-05 10:59:49 +09:00
민헌
23c1c48634 feat(lps): 네이버 최저가를 모바일 크롤로 복구(오픈API 종료 대체)
shop.json 이 2026-07-31 종료(404 SE05)되고 NCP API HUB 에도 승계되지 않아
가격을 얻을 공식 경로가 사라졌다 → 쿠팡과 같은 스택(patchright+실제 Chrome)으로 크롤 전환.

경로: msearch.shopping.naver.com (PC 는 405/418 로 막힘). 7/9 스파이크 때 모바일은
로그인 리다이렉트였는데 그 사이 열렸다.

통과 조건 3개 — 하나라도 빠지면 WTM 캡차(실측):
- **한국 IP**: 해외 residential 은 즉시 하드차단(2.6KB) → kr.decodo.com 게이트웨이
  ([DecodoConfig].kr_host, DecodoProxy(host=...) 로 주입. 쿠팡은 기존 월드와이드 유지)
- **ko-KR 로케일/시간대**: KR IP + en-US 조합을 봇으로 본다
  (BrowserSearchAdapter.context_options 훅 추가)
- **리소스 차단 금지**: route 를 걸면 즉시 캡차. image/media/font 만 막아도 동일 →
  '무엇을 막느냐'가 아니라 요청 가로채기 자체가 탐지 신호. 대신 검색당 ~3MB(~$0.009)

파서는 '정확한 상품의 최저가'를 기준으로 취사선택한다:
- 광고/슈퍼적립/브랜드블록 카드 제외(멤버십·쿠폰 조건부 가격)
- 쿠폰할인가를 price 로 쓰지 않음(조건부라 실구매가보다 싸게 잡힘)
- 가격비교('최저 N원') 카드는 유지하고 mall_name="네이버"(옛 lprice 와 같은 의미)
- **배송비 확보** — 옛 오픈API 는 필드 자체가 없어 전 소스 None 이었다
- 가격 함정 3종 회귀 테스트: 단위가격(548원)·가격노드 안의 배송비(3,900원)·정상가/할인율

source 는 "naver" 유지 — price_history.naver_lowest·MALL_BY_SOURCE·프론트 그래프 계약이
구현(API→크롤) 교체와 무관하게 살아야 한다.

테스트 12건 추가(축약 픽스처 + 합성 함정) · 전체 182 passed.
2026-08-05 09:05:38 +09:00
민헌
55c18e6a83 fix(lps): 워커 재시작 시 Xvfb 가 stale lock 으로 죽어 모든 크롤이 실패하던 문제
docker compose start/restart 는 컨테이너 파일시스템을 재사용해 지난 실행의
/tmp/.X99-lock 과 /tmp/.X11-unix/X99 가 남는다. Xvfb 가 이걸 '이미 켜진 디스플레이'로
보고 종료하면 DISPLAY 가 없어 headful Chrome 이 못 뜨고, 워커는 살아있는 채로
launch_persistent_context 가 'Missing X server or $DISPLAY' 로 실패한다.
→ CMD 에서 Xvfb 기동 전에 stale lock/소켓을 제거한다.

재현·검증: restart 2회 연속 Xvfb 정상 기동 확인.
2026-08-04 16:30:38 +09:00
민헌
005ebc3d76 feat(lps): NCP NAVER API HUB 쇼핑 인사이트 클라이언트 추가
네이버가 2026-07-31 검색 오픈API 중 쇼핑·책·전문자료를 종료(유예·대체 없음)해
shop.json 이 404 SE05 를 반환한다. 후속 플랫폼인 NCP NAVER API HUB 를 붙인다.

- NaverApiHubConfig: 게이트웨이 base_url + NCP Client ID/Secret(둘 다 차야 enabled)
- services/naver_hub/client.py: X-NCP-APIGW-API-KEY-ID/KEY 인증, 오류 바디
  3형식(게이트웨이/Search/인사이트)을 NaverApiHubError 로 정규화(auth_failed·retryable)
- services/naver_hub/shopping_insight.py: POST /shopping/v1/categories.
  문서 제약(기간 2017-08-01~, 분야 최대 3개, timeUnit·device·gender·ages)을
  호출 전에 검증하고 카멜케이스 응답을 타입으로 변환
- tests: MockTransport 로 경로·헤더·오류형식 계약 검증 17건 + LPS_LIVE 스모크

주의: 허브에도 쇼핑 '검색'(상품명·가격·판매처)은 없다. 인사이트의 ratio 는
구간 내 최대값 100 기준 상대지표라 최저가 파이프라인 소스로는 쓸 수 없다.
기존 services/search/naver 어댑터는 손대지 않았다(사문화 상태 유지).
2026-08-04 11:23:26 +09:00
91b8dc77a0 [feat] negodata: 타결 상한(done_ceiling) — 세팅 기본율%·견적 override·세션 박제, 견적생성 3단계 타결/낙찰 섹션 분리·재생성 상속 2026-08-04 08:56:03 +09:00
2bf91d64f5 [refactor] negodata/front: 견적등록 모달 훅 분리 + 시장가 모순 카드 차단(10번), 엑셀 업로드 xlsx 파싱·CP949 폴백(7번) 2026-08-03 09:33:06 +09:00
bae0da42d2 [fix] landing: 히어로 세로 리듬 확보 — 간격 확대·헤드라인 74px
답답함의 원인은 가로 비율이 아니라 세로 간격이었다. 헤드라인 80px 에 대해
헤드라인↔서브 28px, 서브↔CTA 40px 는 너무 촘촘했다.

- 간격 확대: mt-7 → mt-11(44px), mt-10 → mt-14(56px).
- 헤드라인 80 → 74px. 글자 실폭이 뷰포트의 40% (레퍼런스 statworx 는 48%).
- 측정 주의사항을 주석에 남김: h1 은 블록이라 getBoundingClientRect 로 재면
  컨테이너 폭(848px)이 나온다. Range 로 텍스트 노드를 재야 실제 글자 폭이다.
  이걸 헷갈려 한 번 과교정(66px, 36%)했다가 되돌렸다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 18:43:16 +09:00
15369a016d [fix] landing: 히어로 여백 확보 — 순환 링을 강화학습 섹션으로 이동
히어로에 헤드라인·서브·CTA 2개·군집 밴드·순환 링이 다 들어가 세로 예산이 포화라
위를 벌리면 아래가 터지는 상태였다. 링을 옮겨 자리를 만든다.

- 순환 링을 강화학습 섹션으로. "협상할수록 좋아진다"를 말하는 자리라
  루프가 의미상 속하는 곳이기도 하다. 텍스트 좌 / 링 우 2단 배치.
- OrbitRing 에 자체 시계 모드 추가 — progressRef·phase 를 안 주면 스스로 돈다.
  덕분에 히어로 캔버스와 무관한 섹션에서도 단독으로 쓸 수 있다.
- 히어로 상단 여백 14vh → 19vh, 밴드를 하단 전체(0.63/0.30)로 확장.
- 군집 라벨 y 계산 버그 수정. 밴드 지역 좌표(0.5)를 화면 좌표로 착각해
  밴드를 옮길 때마다 라벨이 엉뚱한 곳으로 갔다. 이제 행 수·행간에서
  격자 상단을 실제로 계산해 투영한다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 18:34:42 +09:00
5917942fe3 [fix] landing: 모바일 대응 — 글리프 배율·밴드 리맵·헤더 겹침
- 글리프 크기를 화면 폭에서 파생. 격자 간격은 dx × width 로 폭에 비례하는데
  글리프가 CSS px 고정이라 375px 에서 셀(7.7px)보다 커져(22px) 서로 겹쳐 덩어리가 됐다.
- 좁은 화면(<640px)에서는 밴드를 링 뒤로 내리고 군집 라벨을 접는다.
  모바일은 CTA 가 세로로 쌓여 세로 예산이 부족하고, 375px 에서 3군집을 나란히 두면
  군집당 100px 남짓이라 어차피 판독이 안 된다. 의미는 순환 링이 담는다.
- 히어로 상단 여백을 vh 대신 모바일 고정값으로. 헤더는 고정 높이라
  pt-[7vh](모바일 57px)로는 헤더(약 70px)를 피할 수 없어 헤드라인이 겹쳤다.
- 데스크톱 상단 여백 복원(14vh)과 밴드 재배치(0.575/0.155), 링 290px.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 18:27:02 +09:00
87b896fc2f [fix] landing: 히어로 구도 좌측 쏠림 · 순환 링 정원화 · Playfair self-host
- 군집 좌표를 투영 후 위치에서 역산하도록 변경.
  sx = 0.5 + (x−0.5)·k 라 깊이마다 당겨지는 양이 다르고, 가장 깊은 협력사가
  중앙으로 제일 많이 끌려와 구도가 왼쪽으로 쏠렸다. 이제 화면 19/50/81% 에
  대칭으로 놓인다 — 라벨도 같은 값을 써서 어긋나지 않는다.
- 순환 링을 원근 타원에서 정원으로 교체(orbit-ring). 점선 궤도만 회전하고
  마디·라벨은 고정이다. 눕힌 타원은 깊이는 생기지만 "닫힌 루프"의 완결감이 없다.
- 학습 단계 입자 궤도 제거. 화면을 가로지르는 큰 선이 순환 링과 의미가 겹치고
  시선을 뺏었다. 순환은 링이 맡고 입자는 안쪽으로 흡수만 한다.
- Playfair Display self-host(latin 서브셋 39KB). 구글 폰트 CDN 제거 —
  서드파티 요청이 LCP 를 흔들고 방문자 IP 가 외부로 나간다.
- 히어로 CTA "협상 시연 보기" → "직접 협상해 보기".
  데모는 실제로 동작한다(앵커 이하 낙찰 · 카드 소모 역제안 · 소진 시 개찰 확인).
  수동태 문구는 인터랙티브 기능을 영상처럼 보이게 해 전환을 깎는다.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 17:16:28 +09:00
6785c8a6f4 [feat] landing: 랜딩 v2 — 다크 무대 히어로·인터랙티브 협상 데모·디자인 토큰 재정립
레퍼런스(statworx·jitter·infinith) 실측값으로 디자인 시스템을 다시 잡고,
스크롤 재생형 콘솔을 사용자가 직접 협상하는 데모로 교체했다.

디자인 토큰
- 타이포: 굵기·크기·자간을 함께 조정(섹션 제목 44→64px/600→800/자간 -0.042em).
  한글 행간은 1.07 — 라틴 기준 0.95 를 쓰면 네모틀이 꽉 차 줄이 닿는다.
- 모서리: 12가지로 흩어져 있던 값을 card 8px / control 4px / full 셋으로 수렴.
  20~32px 짜리 뭉툭한 카드가 제네릭 SaaS 의 가장 강한 신호였다.
- 다크 무대 토큰 신설(stage/on-stage/counter) + 배경 그라데이션.
- primary-on-stage(#7C8CFF) 추가 — #0101F3 은 무대 배경 대비 2.0:1 로
  WCAG 최소(4.5:1)의 절반도 안 돼 다크 위에서 읽히지 않았다.
- 영문 디스플레이 서체 Playfair Display self-host(latin 서브셋 39KB).

히어로
- 배경 캔버스가 실제 파이프라인을 그린다: 수집→분류→대조→산정→협상→학습.
  분류 단계는 거래 품목(시트)·사용자·협력사로 갈라지고 라벨이 붙는다.
- z 좌표 + 원근 투영. 군집마다 z 평면을 하나씩 줘 격자는 온전한 채 깊이만 생긴다.
- 순환 링(정원, 점선 궤도 회전). 진행도는 ref 로 공유해 60fps 리렌더를 피한다.
- 카피에 진입 애니메이션을 걸지 않는다 — LCP 요소라 opacity:0 으로 시작하면
  하이드레이션 전까지 첫 화면이 빈다.

협상 데모 (신규)
- 역할 선택(공급사/구매 담당자) → 품목·단가 제시 → 턴 협상 → 낙찰/개찰 → 상담 신청.
- 판정 규칙은 실제 엔진과 동일: 앵커링가 이하 낙찰, 협상 카드 3장, 소진 시 개찰.
- 로직은 lib/negotiation-sim.ts 순수 함수로 분리 — 나중에 agent 서비스 연동 시
  컴포넌트를 건드리지 않는다.

카피 감사
- "봇" 21곳 → "에이전트"(로봇 아이콘 3개 포함). 대리인은 조달 실무의 정당한 역할이다.
- "악역은 봇이", "단가를 깎는 악역" 제거 — 협력사를 적으로 규정하는 프레임은
  같은 페이지 FAQ(공급사 만족도 82%)와 모순이고, 동반성장 평가를 받는
  대기업 구매팀 결재 라인에 올릴 수 없다.
- 번역투 최상급(초정밀·압도적·완벽히·최적의)과 데모 대화문 전면 재작성.

버그 수정
- 헤더가 흰 배경 위에 흰 글자 버튼을 그리던 문제. onStage 를 스크롤 수치로 계산해
  하이드레이션 전 스크롤 복원 시 초기값이 굳었다 → IntersectionObserver 로 교체.
- 버튼 transition-all → transition-colors (variant 교체 시 배경 플리커).
- 폼 입력 14px → 16px. iOS 사파리는 16px 미만 입력에 포커스하면 화면을 확대해
  모바일 폼 이탈을 만든다.

정리
- 대체된 히어로 3종·기존 협상 콘솔 삭제(약 760줄).
- 라벨 아이콘·알약 배지·blur(120px) 글로우 블롭 제거.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 17:08:37 +09:00
3c3f60c7d4 [chore] 루트 업로드 샘플 CSV 4종 제거(upload-samples/ 와 중복) 2026-07-31 10:26:44 +09:00
bd0f39f131 [feat] negodata·agent: 목표가 네고 적용/미적용 산정근거·와일드카드 1% 10원 반올림·표 xl 전환·QA 체크리스트 2026-07-31 10:22:51 +09:00
775984fe48 [fix] negosium: 협상카드 카운터가 10원 단위 반올림 — 앵커·목표가와 표기 통일 2026-07-30 15:20:01 +09:00
5520e3c2b6 [feat] negodata: 견적 삭제(개발자 전용)·연결 데이터 캐스케이드 soft-delete
- 견적관리 체크박스 선택 + 툴바 '선택 삭제'(상품·협력사 패턴), 상세 시트 단건 '삭제'
- 백엔드 soft_delete 를 연결 협상 세션·대화까지 한 트랜잭션 캐스케이드
- 체크박스·선택삭제·상세삭제 모두 개발자(role 3)에게만 노출
2026-07-30 13:20:42 +09:00
2a004734d8 [feat] negodata·negosium: 엑셀 데이터 내보내기·견적상세 협상카드 연동·드로어 뒤로가기
- 엑셀 데이터 내보내기(상품·협력사·협상카드): 현재 검색/페이지 필터를 무시하고 전체를 페이지 순회로 받아(fetchAllForExport, 무언 절삭 방지) 양식과 동일 헤더로 CSV 내보내기(downloadXxxData) + '데이터 다운로드' 메뉴·todayStamp 파일명 → 내려받아 수정 후 재업로드(라운드트립)
- 견적상세 ChatTab: 사용 협상카드 매칭을 chat_id→card_id(카드 PK)로 교정, 카드 사용 메시지 본문 script 중복 제거(카드 박스에만 노출), 카드칩→/cards?detail= 상세 링크, 카드 멘트 실가격 마스킹(target_price 변수 미주입+maskPrices)
- 견적상세 협상카드 탭: 번호·스크립트 미리보기 컬럼 추가(cardScriptPreview 평문 추출)·카드명 상세 링크
- 상세 드로어: 백드롭 좌상단 '뒤로'(navigate(-1)) 버튼 추가(데스크톱), 공용 Sheet·견적상세 동일 적용
- 협상요약(negosium): 배송 리드타임 라벨을 회사설정(labels.lead_time) 연동 → '표준납기' 표기
2026-07-30 11:03:51 +09:00
35d38e0818 [feat] negosium: 협상 리스트 마감기준 정렬 컨트롤
- SortControl(기본순/마감임박순/여유순) 추가, StatusTabs 우측에 배치
- store setDeadline 배선 — 선택값을 서버 정렬(deadlineToOrder)로 전달
2026-07-30 08:31:16 +09:00
82cca894fc [fix] negosium: 'MD → 구매담당자' 표기 통일
- 챗 봇/안내 라벨(ChatMessage·MDInformation·index.css 메뉴 제목)
- negodata 목표가 산정 문구(pricing) · 온보딩 FAQ/용어(OnboardingGuideModal)
- 견적생성 메모 라벨을 '협력사 안내 메모'로 명확화(협상방에서 협력사에게 보여줄 내용)
2026-07-30 08:31:16 +09:00
9716528ce7 [feat] negosium: 협상 챗 헤더 정리 — 상품명 대표 + 견적번호
- 헤더 대표값을 협상 중인 상품명으로(여러 협상건 중 '이 건' 식별), 펄스 점·'가격 협상 채널' 문구 제거
- 견적번호(qt_number)를 백엔드 chat init → 프론트로 배선해 헤더 칩으로 노출(좁은 모바일은 숨김)
- 모바일 상품 드로어 상단에 구매사 로고+견적번호(BuyerBrandStrip) — 데스크톱 사이드바 로고의 모바일 대체
2026-07-30 08:31:16 +09:00
9dca78dc72 [feat] negosium: 협상완료 부가정보 수취·표시·잠금 + VAT 표기 통일
- 완료 요약 카드(투찰/협상 결과)에 부가정보·의견을 항상 구조로 표시, 미입력 필드는 '-'
- 협상 리스트 부가정보를 완전 읽기전용으로 잠금 — 입력은 완료 챗 마무리에서만
- 어드민 협상상세(현황·챗 탭)에 협력사 의견(custom.opinion) 노출
- 협상 화면 VAT 표기를 회사설정(vat_yn) 기준으로 통일 — hidden이면 라벨 생략
2026-07-30 08:31:16 +09:00
671904c762 Merge commit '30f134836b59843d84b60fa8c0923bd81afefb41' into feature/negodata 2026-07-29 09:57:05 +09:00
6f32b466b9 Merge branch 'design/frontend' into feature/negodata 2026-07-29 09:56:59 +09:00
fe9adcaa26 [fix] negosium/front: 포털 모바일 safe-area 여백 정리 — 헤더 노치(safe-t+min-h)·목록 좌우/하단(safe-x/safe-b)·모달 가장자리 여백(p-4), 채팅 스크롤바 트랙 상단 64px 여백 제거 2026-07-29 09:56:11 +09:00
f554202c58 [fix] anchoring: 앵커링가 1원 단위 내림 → 10원 단위 반올림 (자동 제안가 정돈, IMK #11 케이스) 2026-07-29 09:51:26 +09:00
a56589c6d9 [feat] negosium/협상: 협상 종료폼 통일·의견 수취 — rejectRSP/CM를 단일 RejectForm(사유·희망가·의견)으로 통합해 액션바 렌더, 타결 부가정보에 의견 추가(custom.opinion), 결렬 제출값 파싱해 reject_reason/reject_price 컬럼 저장, 상품상세 필드 라벨 회사설정 연동·협상 단가 VAT별도 표기 2026-07-29 09:51:21 +09:00
b33ae05cd6 [feat] negodata: 견적 목표가·앵커 반올림 + 통계 협상카드 효과 — 목표가=매입가×(1−네고율) 자동입력·산정후보를 낙찰기준탭으로 이동·목표가상한 상시표시 제거, 후보가/앵커 생성가 10원 단위 반올림(신규 세션, schedules 앵커와 동일 공식), 산정모달은 구매담당자 제시가 있어도 후보 전부 노출·MD입력가→구매담당자 제시가, 신규상품 등록에 공급사칸 추가, 상품상세/견적생성 라벨 회사설정 연동, 통계 협상카드 효과(유형별 사용빈도+사용직후 평균 제시가 하락)·와일드카드 1%인하 집계, mono 폰트 폴백 2026-07-29 09:51:03 +09:00
30f134836b [fix/feat] 인터넷 최저가(LPS) — 로딩 무한 버그 수정 · 몰별 최저가 노출 · 탐색/상세 UI 재설계 (#10) 2026-07-28 08:25:14 +00:00
민헌
8fa77af866 [chore] lps: 배포 설정 문서화 + 죽은 env 제거 + 알림 문구 정정
배포서버 쿠팡 크롤 조사(2026-07-28)에서 드러난 '설정한 줄 알았는데 아니었던' 것들 정리.
크롤 동작 자체를 바꾸는 변경은 없다.

- Dockerfile.worker: ENV LPS_CHROME_EXECUTABLE 제거.
  읽는 코드가 없는데 값이 박혀 있어 "컨테이너 Chrome 경로가 설정돼 있다"는 오해를 만들었다.
  경로의 유일한 소스는 [WorkerConfig].chrome_executable 이다(설정 시 --no-sandbox 동반).

- config.local.toml.example: profile_dir 경고 추가.
  컨테이너에서 ".profiles" 로 두면 /app/.profiles(컨테이너 레이어)에 쌓여 재생성마다 쿠키가
  날아가고, compose 가 마운트한 lps-profiles 볼륨은 붙어만 있고 아무 일도 하지 않는다.
  로컬·배포서버 양쪽에서 실측(/profiles 는 7/9·7/16 잔재, /app/.profiles 에 9MB 최신).

- config.prod.toml.example 신규: 배포서버 설정 템플릿.
  로컬과 다른 값만 ★ 표시. 헤더에 로드 경로의 함정을 명시했다 —
  docker-compose.prod.yml 이 이 파일을 config.local.toml 자리에 마운트하므로
  APP_ENV=local 인데도 내용은 prod 설정이다(파일명만 보면 오해한다).

- worker_main.py: budget_leak 알림이 "ip_request_budget 하향 검토"를 단정하던 것을 수정.
  차단이 ip_req#1 에 몰리면 새 IP 첫 요청부터 막히는 것이라 예산과 무관하다.
  bot_detection.ip_request_no 분포를 보고 처방을 고르도록 문구를 바꿨다.

⚠️ Dockerfile.worker 가 바뀌었으므로 배포 시 lps-worker 재빌드 필요.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 16:39:14 +09:00
민헌
89652fac1f [refactor] negodata/front: 인터넷 최저가 상세 시트 재설계 — 실구매가 중심 + 근거 검증
협상 근거로 쓰는 화면이라 "얼마에 살 수 있고, 그게 어디서 나온 값인가"를
한 번에 검증할 수 있게 정보 구조를 다시 짰다.

구조: 결론 → 판단 → 근거
  - 히어로: 합계(상품가+배송비) 32px + 단가 대비 판정을 한 블록에.
    면은 브랜드보다 채도를 낮춘 --accent 표면, 값만 브랜드 컬러(화면에 단 하나).
  - 판매처 비교표: 상품가/배송비/합계를 몰별로. 상품가 최저와 실구매가 최저가
    다를 수 있어 둘 다 표시. 결과 없는 몰도 '미발견' 행으로 남겨 빈손임을 드러낸다.
  - 검색어(찾은 상품명) + 복사 버튼 → 판매처에서 다시 찾아볼 때 붙여넣기용.
  - 가격 추이: 상품가 단일 기준. 합계 기준은 배송비를 아는 회차에만 값이 생겨
    (네이버는 배송비 미제공 — 실측 39/39) 기준을 바꾸면 값의 의미와 표본 수가
    동시에 변한다. 기준선(상품 단가) 추가 — 이 선 위면 사는 게 손해.
  - 수집 이력: 미발견도 노출(전체의 30%가 not_found 인데 그동안 숨겨져 있었다).

데이터 정직성
  - 배송비 미상(전체 69%)을 0원으로 뭉개지 않는다 — 무료배송으로 읽히면 안 된다.
    모르면 히어로 라벨이 '합계' → '상품가'로 바뀌고 그 사실을 문구로 밝힌다.
  - 화면 주인공(합계)과 목표가 산정에 쓰이는 값(상품가)이 다르므로 각주로 명시.
  - 금액 옆 배송 주석은 '+3,500'(연산으로 오해) → '배송비 포함(3,500원)'.

접근성·일관성
  - 대비 전수 계산: --accent 면 위에서 muted-fg(4.16)·amber-700(4.28)이 AA 미달이라
    accent-foreground(9.85)·800 계열(6.0~6.5)로 교체. 히어로는 큰 글씨 기준 통과.
  - rose(파괴적 뉘앙스) 제거 → 브랜드 인디고 한 톤. 장식 아이콘 전부 제거.
  - 타이포 7단계를 상수 T 로 고정(임의 크기 금지).
  - 차트 점 3종 모양을 DOT 상수로 추출해 차트와 범례가 공유 — 범례는 차트 밖이라
    --color-price 가 해석되지 않아 색이 어긋났다. 전역 토큰으로 통일.
  - Y축 눈금 1·2·5×10ⁿ, 기준 변경 시 애니메이션 끔, 커스텀 X축 tick 제거
    (recharts 의 가장자리 앵커 보정을 건너뛰어 점과 라벨이 어긋났다).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 16:06:27 +09:00
민헌
5e24741395 [refactor] negodata/front: 최저가 탐색 모달 B2B 재설계 + 완료 판정 버그 수정
완료 판정 버그(로딩이 안 멈추던 원인 중 하나)
  폴링이 crawl_end_time 을 문자열로 비교했다. 서버는 마이크로초 6자리(.755705Z),
  JS toISOString() 은 밀리초 3자리(.755Z)라 자릿수가 달라 사전순이 시간순과 어긋난다
  ('.' < 'Z'). 게다가 클라이언트 시각을 서버 타임스탬프의 기준점으로 써서
  브라우저 시계가 조금만 앞서도 어떤 결과도 기준을 넘지 못했다.
  → 요청 직전 서버가 준 최신 수집 시각을 상품별 기준선으로 읽고 epoch ms 로 비교.

UI 재설계 (소비자 앱풍 → B2B 실무 툴)
  - 상태 3분리: confirm | searching | done. 검색 중에 완료 결론을 섞지 않는다
  - 표가 주인공: [선택 | 상품 | 기존 최저가 | 네이버 | 쿠팡 | 결과], 컴팩트 행, tabular-nums
  - 결과 열은 상태어(탐색 성공 / 변동 없음 / 탐색 실패 / 탐색 중)
  - 진행 카드: 표시등 + mm:ss 경과 + 진행바 + N/M, 셀 단위 스켈레톤으로 진행 위치 표시
  - radius 8/6/4, shadow 최소화, CTA 우측 정렬(풀와이드 금지), 강조는 indigo-800 한 톤
  - 완료 후 자동으로 닫지 않는다(결과 확인). 선택 해제는 닫을 때로 미룸

선택 재검색
  체크박스로 대상을 고르고, 검색이 끝나면 갱신되지 않은 행만 자동 선택된다
  (이미 갱신된 상품에 크롤 비용을 다시 쓰지 않도록). 재검색은 force=true 로 나가
  네거티브 캐시를 우회한다.

접근성 (스킬 기준 + 대비 실측)
  - 보조 텍스트 muted-foreground 4.88:1, indigo-800 9.93:1 (기존 1.92:1 FAIL 해소)
  - 상태는 색 단독이 아니라 텍스트/굵기/sr-only 병행
  - role=status aria-live, 탈출구 3중(X·ESC·오버레이), 버튼 높이 통일

api/generated 는 orval 재생성분. resTargetBreakdown 변경은 이전 백엔드 수정과의
스펙 동기화(이번 작업과 무관하지만 재생성으로 함께 정리됨).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 11:56:00 +09:00
민헌
0cefe315c4 [feat] negodata/backend: 몰별 최저가(by_mall) 노출 + 강제 재검색(force) 연동
몰별 성공/실패 표시
  lps_db.price_history 에는 몰별 정보가 다 있는데(naver_lowest·coupang_lowest·by_mall),
  동기화가 final_source(이긴 몰) 하나만 남기고 나머지를 버려서 API 로는
  "네이버는 어땠는지"를 알 수 없었다. 실제로 네이버는 빈손이고 쿠팡만 성공하는
  케이스가 기본값처럼 나오는 중이라 화면에 드러낼 필요가 있다.
  → price_history.by_mall(JSONB)을 partner.item_internet_lowest_prices 로 그대로
    미러링한다(열린 스키마 — 오픈마켓 폴백이 늘어도 스키마 변경 불필요).
  - crud: 읽기 계약에 by_mall 추가 + SELECT 포함
  - model: item_internet_lowest_prices.by_mall
  - service: 언팩·저장
  - protocol: LowestPriceEntry.by_mall
  - init.sql: 테이블 정의 + 하단 보정 ALTER(기존 DB 반영용)

강제 재검색
  POST /v1/item/{id}/lowest-price?force=true → LPS 네거티브 캐시 우회.
  사용자가 '다시 검색'을 누른 경우에만 true.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 11:55:38 +09:00
민헌
ba9d34fd31 [fix] lps: 네거티브 캐시 히트 시 이력 누락 수정 + force 재검색 지원
1) 캐시 히트가 price_history 를 남기지 않던 문제
   not_found 는 24시간 네거티브 캐시에 들어가는데, 캐시에 걸린 조기 반환 경로만
   _record_history 를 호출하지 않았다(다른 모든 경로는 호출).
   그 결과 잡은 완료인데 price_history 에 새 행이 없어, 이를 폴링하는 소비자
   (negodata 최저가 모달)가 결과를 영영 못 받고 로딩만 돌았다.
   → 캐시 히트도 '이 잡의 결과'이므로 이력을 남긴다.

2) force 플래그
   negodata 는 이제 수동 트리거 전용인데 is_negative() 가 job_type 을 보지 않아
   사람이 직접 누른 재검색까지 캐시가 가로막았다. 게다가 캐시 키가 product_code 라
   상품명·모델을 고쳐 재시도해도 동일하게 막힌다.
   → SearchItem.force=true 면 NegativeCache.drop() 으로 기록을 지우고 실제 검색.
   기본 요청은 캐시를 그대로 써서 비용 절감 효과는 유지.

실측: 캐시에 막혀 not_found 만 반복하던 상품이 force 재검색에서 2라운드 만에 found(8,500원).
⚠️ protocol.py 변경은 lps-api 와 lps-worker 를 함께 재빌드해야 반영된다
   (API 만 옛 스키마면 pydantic 이 force 를 조용히 버린다 — 실측으로 확인).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 11:55:23 +09:00
6534d53696 [docs] readme: 업데이트 + 플로우차트 추가 2026-07-28 09:32:40 +09:00
민헌
ca1291708f [fix] lps-worker: compose 에 platform=linux/amd64 명시 (arm64 맥에서 이미지 빌드 실패 해소)
google-chrome-stable(Linux)은 amd64 전용이라 워커 이미지는 본질적으로 amd64다.
arm64 맥에서 platform 없이 빌드하면 arm64 로 시도하다 Chrome 의존성
(libasound2·libxfixes3 등)에서 exit 100 으로 실패하고, 그 결과 예전에 빌드된
구 chromium 이미지로 뜨면서 LPS_CHROME_EXECUTABLE(/opt/google/chrome/chrome)
경로가 없어 쿠팡 웜업이 매번 실패했다.

platform 명시 후 Rosetta 에뮬로 실제 Chrome 150 이 뜨고 쿠팡 Akamai 챌린지를
시도 1회에 통과함을 확인. prod(amd64 서버)에서는 no-op.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 09:16:19 +09:00
a5fa44ee25 [refactor] negodata/backend: QuotationService 파사드 분할 — services/quotation/(pricing·build·closing·queries·invites), 시그니처·임포트 경로 외 동작 무변경 2026-07-27 16:15:19 +09:00
7b612824be [refactor] negodata: 목표가 산정 정리 — purchase_only 죽은코드 제거(백+프론트)·산정 재료 로더 공용화·자동재생성 잔재 주석 정정 2026-07-27 16:07:21 +09:00
bacf6271df [chore] 작업분 일괄: logs.sh 컨테이너 메뉴화 · dbeaver 초기화 SQL · landing 운영 compose · negodata/front eslint 도입 · 업로드 샘플 CSV 2026-07-24 18:45:46 +09:00
9fb4a52c56 [fix] negosium/front: 협상 챗 에러 말풍선 토스트(하단 짤림 수정) + 가격/할인율 입력 자동 포커스 2026-07-24 18:39:49 +09:00
f9d37e08f4 [fix] lps: 워커 이미지 chromium→google-chrome-stable 교체 (쿠팡 Akamai 지문 차단 회피) 2026-07-24 17:12:20 +09:00
b539e073cb Merge commit 'dc8c288f' — lps-admin 신설·LPS 운영기능(ip_session/alerts/admin API)·최저가 출처링크 반영 2026-07-24 15:30:36 +09:00
36afcd68a6 Merge branch 'fix/nego-card-scripts-cleanup' — 협상카드 스크립트 개정·미사용 카드 정리(시드+alter) 2026-07-24 15:17:22 +09:00
a55975e62a [fix] negosium 시드: 협상카드 스크립트 개정 + 미사용 카드 정리
출처: '260716_AIO2O 수정 및 요청사항_v2_memo check.xlsx' > '협상카드 수정' 시트(D열).
- 스크립트 개정: NGC-003(장기 관계 강조)·NGC-004(명분 제공)·NGC-007(예산 상한 안내)·NGC-008(시장가 근거 제시)
- 미사용('IMK 거래 특성과 관련성 부족') 카드 삭제: NGC-006(총비용 가치 설명)·NGC-009(조건부 가격 조정)
- init-data.sql(신규/초기화 설치 정본)에서 006·009 INSERT 제거 + 4개 멘트 갱신
- 동일 최종본을 기존 DB 보정용 alter(postgres-init/alters/2026-07-24-nego-card-scripts.sql)로도 제공(멱등)

검증: 시드 nego_cards VALUES 를 temp 테이블에 강제 평가 → 9행·전부 유효 jsonb. seed=alter=로컬 DB 일치.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-24 15:17:13 +09:00
e9be649e1f [chore] logs.sh: 운영 로그 조회 스크립트 2026-07-24 14:58:57 +09:00
624a8846b0 [chore] landing: prod Dockerfile(SSG 빌드→nginx) 추가 2026-07-24 14:52:16 +09:00
d580e4d5ea Merge branch 'feature/negodata'
# Conflicts:
#	landing/app/routes/home.tsx
2026-07-24 14:22:31 +09:00
5318ff3263 [chore] landing: 홈 문구 정비 (mobile.mov 제외·ignore) 2026-07-24 14:18:52 +09:00
51d07bb779 [feat] negodata: 재협상 소유권 게이팅·처리담당자·상세 링크·재생성 타이틀·알림 토스트(읽음API)·재협상요청 표기·커스텀필드 select 2026-07-24 14:18:52 +09:00
d7c4e86f30 [feat] 공급사 포털: 협상완료 부가정보(select·종료 동의폼)·상태 라벨 통일·예아니오 Enter=예·목록 결과열·재협상 요청/철회 API·테스트 2026-07-24 14:18:52 +09:00
562dafa5ca Merge branch 'fix/nego-bid-price-card-limit' — 협상 투찰가 표시≠타결가·카드 사용 횟수 미강제 수정 2026-07-24 11:41:43 +09:00
3eec80d989 [fix] agent: 협상 투찰가 표시≠타결가 버그 + 카드 사용 횟수 미강제 수정
1) 표시가≠투찰가 (배포 서버 협상 결과 오류)
- 카운터 제시 중 멘트의 절충/중간 변수(middle_price·target_mid_price)가 vars_for 재계산으로
  compute_counter 의 target 클램프·prev_customer 갱신과 어긋나, 화면엔 1,740,000 인데 실제로는
  1,700,000 으로 타결되던 문제 → vars_for 에서 세 변수(counter/middle/target_mid)를 pending 으로 고정
- WC-03 최후통첩 멘트가 제시 금액을 안 보여줘 수락 시 화면에 없던 target 으로 타결되던 문제 →
  멘트에 {target_price} 명시(seed init-data.sql). 운영 DB 는 별도 UPDATE 필요

2) 카드 사용 횟수 3회 초과
- quotation_settings.card_count(협상카드 사용 횟수 상한, 기본 3)가 어디서도 강제되지 않던 죽은 설정 →
  에이전트가 sessions.qt_setting_id 로 card_count 를 조회해 협상카드 재생 수를 min(선택수, card_count)로 캡
  (session.action_space_size 는 소진 판정 전용 — Q-table 은 카탈로그 전체로 별도 고정)

- 회귀 테스트: 표시가==타결가(test_card_tactics·test_p7_chat), card_count 로드(test_context_loader). 전체 153 pass

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-24 11:41:33 +09:00
2541a391b7 Merge branch 'feat/chat-bugfix' — 협상카드 변수 치환 누락 수정 + 상품 업로드 협력사 목록 422 수정 2026-07-24 10:37:30 +09:00
ce551090b3 [fix] agent·negodata: 협상카드 변수 치환 누락 수정 + 상품 업로드 협력사 목록 422 수정
- agent: 인터넷 최저가(items.internet_lowest_price)를 협상 파이프라인에 연결
  (nego_context_crud 조회 → context loader → session.context → chat_engine.vars_for).
  값 있을 때만 {internet_lowest_price}/internet_min_price 치환, 미수집(0)이면 원형 유지(허위 시장가 방지)
- negodata: 카드 에디터 변수 카탈로그에 '인터넷 최저가' 추가
- negodata: 견적 생성 카드 선택 게이팅 — 조건전략 미작성({customer_condition} 잔존)·
  최저가 미수집 상품의 {internet_lowest_price} 인용 카드는 선택 불가(상품 변경 시 자동 해제 포함)
- negodata: 상품 엑셀 업로드의 협력사 목록 size 1000→100
  (PageParams le=100 초과 시 422 → 목록 비어 전 행 '미등록 협력사' 오탐)
- seed: WC-01 의 데이터 소스 없는 {customer_reference} 제거 → 고정 문구로 대체

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-24 10:36:24 +09:00
4f9789c08a [feat] 공급사 포털·landing: 채팅 남은시간·헤더 정비, 데모 미디어(mp4/gif) 교체
- 공급사 포털: 채팅 말풍선·남은시간 표기·헤더바·채팅 화면 정비
- landing: how-it-works 데모를 GIF 재생으로 되돌리고 히어로·디바이스 목업 조정, negotiation_annotated_3d 미디어 갱신
2026-07-23 15:43:23 +09:00
cfee6e89d0 [feat] negodata: 권한 3단계(개발자 신설)·개발자 메뉴 그룹·디자인 시스템 페이지
- UserRole 1=일반 2=최고관리자 3=개발자(내부 운영). 개발자 계정은 회원 목록·총계에서 제외해 고객사에 노출하지 않음
- 계정 생성 시 권한 선택(일반/최고관리자) 추가, 개발자는 앱에서 부여 불가(DB 시드 전용)
- 사이드바 '개발자' 그룹 신설 — 회사 설정·디자인 시스템을 개발자에게만 노출
- /dev/design 디자인 시스템 페이지: 색 토큰·타이포·버튼·배지·입력·반경, 목록 화면 구조, 반응형 기준, URL 상태 규칙, 회사 커스터마이징 훅
- 공급사 재협상 요청(#15) 플로우 문서 — sessions.custom 기반(DDL 0)
2026-07-23 15:41:36 +09:00
db07a60cfa [fix] negosium·negodata·landing: 채팅 진입·부가정보 저장 오류 수정, 모바일 여백·스크롤·노치 대응
- 채팅 링크로 바로 진입하면 세션이 협상생성(1)에 머물러 오프닝 메시지가 seed 되지 않던 문제:
  chat init/messages 에서 participate 와 동일하게 협상중(2)으로 전이(마감 견적·마감시간 초과는 제외)
- 부가정보 저장이 1301 로 실패하던 문제: '협상완료' 요약은 chat_end=false 라 세션이 아직 협상중이므로,
  마지막 말풍선이 summaryRSP/CM 이면 협상중 세션도 저장 허용
- 채팅 언마운트 시 messages 캐시도 제거 — staleTime:Infinity 라 빈 목록이 남아 재진입해도 빈 화면이던 문제
- 채팅 스크롤을 컨테이너 기준 scrollTo 로 변경(rAF 후 실행) — 요약·폼이 뒤늦게 레이아웃되면 중간에 멈추던 문제
- 모바일 채팅 상단 여백 제거(pt-[64px] 는 우측 협상절차 카드가 있는 lg 이상만)
- 노치 대응: viewport-fit=cover + safe-t/b/x 유틸 신설, 헤더·하단 액션 덱·드로어·랜딩 헤더에 적용, h-screen→100dvh
2026-07-23 12:50:40 +09:00
0f10b6a70f [feat] negodata·공급사포털: 상품 필드 숨김 설정·용어 카탈로그 확장·로그인 전 브랜딩·모바일 대응
- 회사 설정 hidden_fields 신설: 상품 목록·등록폼·엑셀 양식에서 감추고 목표가 후보에서도 제외
- 용어 카탈로그 9→37개(상품·협력사·견적 그룹), 목록·폼·엑셀 헤더 배선
- 엑셀 '최저한도' → '인터넷 최저가' 표기 정정(구양식 헤더는 alias 유지), 공급사 컬럼 검증 유지
- 목표가 산정내역에 적용 모드·숨김 필드 반영, 목표가 상한 라벨에 산식(상품단가×2) 표기
- 공급사 포털: 초청 링크 session_id 로 로그인 전 회사 브랜딩 조회(무인증 엔드포인트), 기본 브랜드 negotium 워드마크, negotium·AI O2O 크레딧 표기
- 목록 테이블 뷰포트 기준 전환·툴바 줄바꿈·협상현황 카드뷰로 모바일/태블릿 대응
- 이용안내 FAQ 탭 추가, 이용안내·회사설정 탭 URL 쿼리 동기화 및 빠른이동 등록
2026-07-23 11:29:35 +09:00
4927c9184e Merge branch 'feature/negodata' of https://gitea.o2o.kr/Negosium/o2o-negosium into feature/negodata 2026-07-22 17:28:10 +09:00
03322d6ca3 [feat] negodata: 카드 성공률 순위·상품 엑셀 TO-BE 싱크·회사설정 JSON 입출력·목록 포털형 UI
- 협상카드 성공률(사용 세션 대비 타결) 집계 추가, 견적 생성 카드선택 순위 정렬·통계 TOP5
- 상품 엑셀: 품목코드·모델명 라벨화, 리드타임 suffix 제거, 공급사 컬럼 신설(등록 후 supplier_items 매핑)
- 설정 화면 JSON 불러오기/내보내기(빈 값은 미갱신), IMK 설정 JSON 보관
- 설정·회원 페이지 새로고침 forbidden 수정(자식 loader 가 initAuth 대기)
- 협력사·견적 목록도 포털형 카드(툴바+테이블 결합)로 통일
2026-07-22 17:27:23 +09:00
fa9113da55 Merge commit 'f8f8f70f54980eb5d1b60a4ff4aaa67215bd1497' 2026-07-22 17:26:52 +09:00
7c4b66aba5 [feat] negodata: 카드 성공률 순위·상품 엑셀 TO-BE 싱크·회사설정 JSON 입출력·목록 포털형 UI
- 협상카드 성공률(사용 세션 대비 타결) 집계 추가, 견적 생성 카드선택 순위 정렬·통계 TOP5
- 상품 엑셀: 품목코드·모델명 라벨화, 리드타임 suffix 제거, 공급사 컬럼 신설(등록 후 supplier_items 매핑)
- 설정 화면 JSON 불러오기/내보내기(빈 값은 미갱신), IMK 설정 JSON 보관
- 설정·회원 페이지 새로고침 forbidden 수정(자식 loader 가 initAuth 대기)
- 협력사·견적 목록도 포털형 카드(툴바+테이블 결합)로 통일
2026-07-22 17:24:58 +09:00
f8f8f70f54 [style] negodata/front: 상품·카드 목록 포털형 카드(툴바+테이블 한 카드로 결합) 2026-07-22 17:23:48 +09:00
bcb87de35e [feat] backend: 선택 와일드카드 card_id 저장 — wild 스텝이면 wild_cards 조회·card_type=2(nego는 card_type=1, step 으로 구분) 2026-07-22 16:10:22 +09:00
0242ecff09 [feat] negodata: 초청 메일 헤더에 회사 브랜딩(settings.branding.email_header) 배선 2026-07-22 15:54:05 +09:00
bb576af346 [feat] negodata/front: 협상완료 부가정보 협상현황 모달·아이콘 툴팁(base-ui)·헤더 회사명 모바일 전용 2026-07-22 15:54:05 +09:00
e67e713270 Merge branch 'feature/negodata' into feature/landing 2026-07-22 15:24:56 +09:00
08c1e66582 [feat] landing: negotiation 데모에 mp4/poster 추가(gif→비디오 재생) 2026-07-22 15:24:08 +09:00
5d0fbbcf6a [feat] 공급사 포털·backend: 협상완료 부가정보 입력(#5)·포털 브랜딩/로고·구분삭제(#19)·카드메타 nego 저장 2026-07-22 15:20:24 +09:00
88edcfed63 [feat] negodata: 회사 커스터마이징(설정·라벨·커스텀필드·엑셀·상품공급사#20·분류카테고리#17)·일괄삭제·로그export·구분·카드 전체선택·부가정보 표시 2026-07-22 15:20:24 +09:00
1e4726f4cc [feat] 스키마: company.settings·items/suppliers/sessions.custom·nego_cards 컬럼 추가(모델·init.sql) 2026-07-22 15:19:41 +09:00
e8d2acaade [feat] negodata: 통계 인상억제율(재협상)·월별 차트 — 직전 라운드 투찰가 대비 인하율 파생 집계 2026-07-22 15:19:14 +09:00
a9db0f7a1b Merge branch 'design/frontend' into feature/landing 2026-07-22 08:57:08 +09:00
3e5f8c6cd4 [feat] landing: 콘솔형 히어로·price-sparkline·데모 미디어(mp4/jpg) 세트 추가, phone 데모(how-it-works) 수정 2026-07-22 08:52:40 +09:00
e7a51b1b75 [feat] negosium/front: 협상방 마감 정비 — 우측 패널 납작 섹션·모바일 협상절차 스텝바·이용가이드 모달 토스화·중복 이용가이드 제거·폰트 정리·마감시간 tabular-nums 2026-07-22 08:29:58 +09:00
e9427543c8 [feat] landing: 히어로/데모 리뉴얼 — 콘솔형 히어로·price-sparkline 추가, 데모 미디어(mp4/jpg) 세트 추가, ROI 시뮬레이터·negotiation-console·버튼·홈 라우트 수정 2026-07-22 08:29:54 +09:00
6ffdb9a389 [feat] landing: 비교 토글 자동재개·헤더 nav 4개 정리·모바일 가운데 정렬·Phone Demo 히어로 디폴트, FAQ 연동답변·중개대행·역경매·와일드카드 등 논란 문구 정리 2026-07-21 13:49:27 +09:00
9ccf42cf6b [feat] landing: 카피 정직화(가이드 3탭·강화학습·이점)·실단가 반영, 비교 토글·FAQ 섹션 신설, 협상 콘솔 목표가 스파크라인·절감 델타 추가, 헤더 플리커·히어로 줄바꿈 수정 2026-07-21 10:47:45 +09:00
e93820a51b Merge branch 'design/frontend' into feature/landing 2026-07-21 08:48:07 +09:00
dd3609173e [feat] landing: 협상 데모 GIF 모바일 UI로 교체 (채팅 밀당→타결, 상단40/하단28 세이프에어리어, 9:18) 2026-07-21 08:45:29 +09:00
2386cb8867 [feat] negosium/front: 아이마켓 보고서 기준 토스풍 UI 리뉴얼 — 로그인·포털 목록(KPI·모바일 카드)·협상방 3단(말풍선·타임라인·결과/성공률 카드)·모바일 드로어, 목록 서버 페이징 유지·검색 비움 2026-07-20 17:27:11 +09:00
b6852ec6aa [feat] landing: 협상 데모 GIF 모바일 UI로 교체 (채팅 밀당→타결, 상단40/하단28 세이프에어리어, 9:18) 2026-07-20 17:26:50 +09:00
03c22f9474 [feat] landing: 솔루션 랜딩페이지 신규 — 히어로·데모·ROI 시뮬레이터 섹션, docker 서비스(:3100) 추가 2026-07-20 09:09:09 +09:00
25e13d7b01 [feat] negodata/front: 디자인 리뉴얼 1차 — 인디고 콘솔 토큰·⌘K 크롬·대시보드/로그인/알림함 개편, 툴바 통일, 시트 스크롤 수정 2026-07-16 16:24:40 +09:00
민헌
dc8c288f68 feat(lps): docker-compose 에 lps-admin 추가 + 환경설정 config.local.toml 단일화
- lps-admin 컨테이너화: Vite 정적 빌드 → nginx, /v1·/healthz·/readyz 를
  lps-api:9600 으로 동일출처 프록시(빌드 타임 API URL 주입 불필요)
- lps-api/worker: APP_ENV=local 고정 + DB_HOST override(도메인 backend·negodata·agent
  와 동일 패턴), config.<env>.toml 마운트 방식 폐기
- server_configs 에 DB 접속 env override(_apply_db_env_override) 복원
- config.dev/prod.toml.example 제거 — 환경 구분 없이 config.local.toml 하나
  (prod 서버도 그 서버의 config.local.toml + docker compose up -d)
- run_docker.sh 환경 선택 제거·lps-admin 포함, README·operations 문서 갱신

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 11:54:32 +09:00
민헌
d111610555 feat(lps-admin): 관리자 페이지 디자인 개선 및 컴포넌트 통일
- 공용 프리미티브(ui.tsx): PageHeader·Segmented·Button·SearchForm·Legend·
  ScrollBox·ScrollTable·Modal + Card(fill/compact) 옵션
- 차트 공용 설정(lib/chart.ts): 축·그리드·툴팁 스타일 단일화
- 뷰포트 채우기 레이아웃 + content 영역 스크롤(Layout), 버튼 커서 복구
- 표: 헤더 밴드·행 좌우 패딩·고정/채움/auto 높이·sticky 헤더, 배지 줄바꿈 방지
- 작업 큐: 풀폭 표 + 행별 상세/JSON 모달(복사)
- 상품·가격: 가로 배치, Y축 0 기준, 시점 클릭→몰별 비교, 커스텀 툴팁·
  그라데이션 area, 몰별 컴팩트 표(소스 뱃지)
- 크롤 상태: 도넛 중앙 총계·라운드 세그먼트, 막대 그라데이션, 툴팁 통일
- 테스트 검색: 결과 통일(모달·표·소스 뱃지), 진행상태 좌측 이동, 초기화 전
  재검색 차단, 퍼널 가독성(감소량), 배송(로켓배송) 표기, 한 화면 컴팩트
- 사이드바: API 상태 상단 뱃지, 테스트 메뉴 배경색으로 구분

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 08:48:43 +09:00
3d515055fb [feat] negodata/front: 온보딩 이모지→아이콘 교체, step0 레이아웃·애니메이션 등 개편 2026-07-15 16:39:58 +09:00
민헌
e6005b4f94 feat(lps-admin): 테스트 검색을 앰버로 구분 — '실제 비용 드는 액션' 신호
테스트 검색은 다른 페이지(읽기전용 모니터링)와 달리 실제 크롤·프록시
비용이 발생하는 실행 액션이라 시각적으로 구분(협의).

- 네비 '테스트 검색': 앰버 점 마커 + 앰버 텍스트/활성색, 위에 구분선으로
  모니터링 페이지들과 분리(설정과 함께 실행/유틸 그룹).
- 테스트 페이지 '검색 실행' 버튼: primary(indigo) → warn-700(#a36a00).
  흰 텍스트 대비 4.55:1 로 AA 통과(실측). 상태색은 장식이 아니라
  '비용 주의' 의미로만 사용(디자인 규칙 준수).
- whitespace-nowrap 로 라벨 줄바꿈 방지. 모바일 hscroll 0 유지.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 11:44:59 +09:00
민헌
479cb862b1 feat(lps-admin): 상품·가격 — '몰별 최저가'+'검증 링크' 를 몰별 가격 비교로 통합
두 카드가 사실 같은 데이터(by_mall)를 다르게 그린 것이라 하나로 합침.
상위 N개(3/5/10/전체 선택)를 순위·쇼핑몰·상품명·가격·최저가 대비 델타·
링크 한 줄로 보여주고 가격 비교 막대를 얹어 한눈에 비교 가능.

- MallEntry 타입 필드명 버그 수정(mall/url → mall_name/detail_url/name)
  — 기존 몰별 최저가 차트에 몰 이름이 안 보이던 원인. 이제 표시됨.
- 상위 N 은 프리셋 칩(by_mall 이 몰별 dedup 소집합이라 자유입력보다 적합).
- '검색어'는 항목별로 없어 각 몰의 상품명(title)으로 대체 — 같은 상품
  검증 역할.
- 모바일 가로 스크롤 해소: 상단 네비 overflow-x-auto 가 페이지를 늘리던
  것(루트 w-full 누락)과 master-detail 그리드 자식 min-w-0 누락 동반 수정.
- 실기동 검증: G마켓 56,880/플랜88 59,900 렌더·칩 토글·데스크톱/모바일
  hscroll 0·콘솔 에러 0.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 10:57:07 +09:00
민헌
a306ad4cf3 docs(lps-admin): README 에 테스트 검색 페이지 추가
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 09:14:14 +09:00
민헌
c126d56631 feat(lps-admin): 테스트 검색 페이지 — 임의 상품을 실제 파이프라인에 흘려 점검
셀렉터·프록시·AI 매칭 회귀를 UI 에서 즉석 점검. API 는 lean(브라우저
없음)이라 크롤은 워커에만 있으므로, 기존 POST /v1/lps/search 로 잡을
넣고 GET /jobs/{id} 를 2초 폴링해 결과를 시각화(신규 백엔드 0).

- 폼(상품명 필수+모델/규격/제조사/현재가) → 제출 → 진행 스텝퍼
  (대기→크롤·판정중→완료, 1초 티커 경과시간) → 결과.
- 결과: 판정(최저가·소스), 파이프라인 퍼널(수집→필터→이상치→AI→top-N
  단계별 잔존 수), 소스별 수집(+실패 시 error 표시), 검색 원가
  (AI/프록시 분해·전송량), 매칭 상품 표, 원본 JSON.
- product_code=TEST-<시각> 자동 — 프로덕션과 필터 구분(negodata 는
  UUID 만 매핑, 연동 무영향). 자격증명은 서버에만, 결과엔 집계 사용량만.
- 워커 미실행 시 8초 후 '워커 확인' 힌트, DEAD 시 오류 표시.
- 작업 큐에 '테스트만'(TEST- 접두) 필터 칩 추가.
- 실기동 검증: 맥심 커피 검색 → 퍼널 80→2·쿠팡 9.5s·원가 /bin/zsh.002 렌더,
  콘솔 에러 0, 모바일 hscroll 0.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 09:13:54 +09:00
민헌
3c8d0af2e4 chore: .gitignore 에 .gstack/ 추가 (gstack 로컬 상태 디렉터리)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:59:14 +09:00
민헌
74fce19be9 style(design): FINDING-009 — 비용 빈 상태에 다음 행동 안내 추가
'없습니다'로 끝나던 빈 상태에 '워커를 켜고 검색을 돌리면 채워집니다'
안내를 붙여 사용자가 다음 행동을 알 수 있게.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:58:51 +09:00
민헌
1006ad11ea style(design): FINDING-012 — 접근성 통일: 검색 라벨·행 키보드 선택·이모지 숨김 [subagent]
Settings 는 label 연결이 모범인데 Jobs/Products 검색 인풋만 접근
가능한 이름이 없었고, Products 목록(button)과 달리 Jobs 행(tr onClick)
은 키보드로 선택 불가였다. aria-label 2곳, tr tabIndex+Enter/Space
핸들러+focus-visible, 장식 이모지(⚠✓🎉) aria-hidden 처리.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:58:51 +09:00
민헌
645a0362b0 style(design): FINDING-011 — 타이포 스케일 정수 6단으로 통일 (본문 14px) [subagent]
작은 앱에 0.5px 간격 10종(10.5~22)이 혼재 — 12 vs 12.5 는 위계를
만들지 못하고 half-pixel 은 zoom/서브픽셀에서 반올림이 갈린다.
{11,12,13,14,17,22} 정수 6단으로 정규화(순서 보존 매핑), 본문
13.5→14px(FINDING-004). 빌드·렌더 검증.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:58:14 +09:00
민헌
29e54ad838 style(design): FINDING-007 — 고정 150px 파이의 ResponsiveContainer 제거
recharts 가 '고정 폭·높이엔 ResponsiveContainer 불필요' 경고를 콘솔에
반복 출력(페이지당 4회). PieChart 직접 렌더로 전환.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:54:46 +09:00
민헌
10ea8ddd45 style(design): FINDING-010 — Crawl.tsx 하드코딩 hex 를 @theme 토큰으로 단일화 [subagent]
Crawl 이 dead-600/ink-400/cost-ai 와 동일한 값을 raw hex 로 재복제해
토큰 변경 시 어긋나는 이중 정의 상태였다. 종료사유 팔레트를 상태
토큰(ok/run/warn/dead)+신규 neutral-300/400 으로 치환, ReferenceLine·
막대·폴백 hex 전부 var() 참조로. src 내 raw hex 0 확인.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:53:58 +09:00
민헌
433b973505 style(design): FINDING-003 — ink-400/500 캡션 대비 WCAG AA 통과로 다크닝
ink-400(#9aa1ac)이 11~13px 소형 텍스트(타일 부제·테이블 헤더·각주)에
쓰이는데 대비 2.54:1 로 AA(4.5:1) 미달. ink-400→#6d7480(4.59:1),
ink-500→#5d6470(5.81:1)으로 2단 위계 유지한 채 통과. 실측 재검증.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:52:37 +09:00
민헌
a750c1098a style(design): FINDING-001 — 모바일(<md) 사이드바를 상단 네비로 전환
375px에서 사이드바가 196px을 고정 점유해 콘텐츠가 낱글자로 세로
분해되고 /jobs 에 가로 스크롤(484px)이 생겼다. <md 에서 상단 바
+가로 스크롤 네비(터치 44px, FINDING-006)로 전환, 로고 그라디언트
hex 를 primary-400 토큰으로 승격. 검증: dashboard/jobs 375px
hscroll=false, 스크린샷 확인.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:51:53 +09:00
민헌
5e286d7949 chore(lps-admin): package-lock name 동기화 — 폴더명 변경 후속
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:39:49 +09:00
민헌
992a3246a8 chore(lps-admin): 폴더명 lps-temp-fe → lps-admin
임시 FE 가 정식 관리자 콘솔이 되어 이름을 용도에 맞게 변경.
루트 병렬 유지(lps/ 는 파이썬 백엔드 루트라 중첩 시 비대칭 +
Docker 빌드 컨텍스트 오염). package.json name 동기화.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 22:07:30 +09:00
민헌
6eb71eb035 feat(lps-temp-fe): React 관리자 페이지 — 대시보드·작업큐·상품가격·크롤·비용
임시 단일 HTML 콘솔을 걷어내고 5페이지 관리자 앱으로 교체(협의:
모니터링+필수 액션, negodata 계열 경량 스택 — Vite+React+TS+Tailwind
+recharts+react-query+react-router).

- 대시보드: /ops 5초 폴링 스탯 타일 8종 + 임계 배너(서버 AlertConfig
  기본값 미러) + 세션 내 누적 큐 추이.
- 작업 큐: 상태 탭·상품 검색·페이지네이션·상세 패널(결과/원가/오류),
  DEAD 재큐 버튼.
- 상품·가격: 가격 추이 3선 + 몰별 최저가 바 + 검증 링크 + 수동 재검색.
  시리즈 색 엔터티 고정 — 네이버 #008a43 은 쿠팡 적색과의 적록색약
  분리(ΔE 14.5)·3:1 대비를 검증해 톤다운한 값.
- 크롤 상태: 종료사유 도넛(의미 기반 상태색)·세션당 요청 수 분포
  +차단 시작 기준선(예산 튜닝 뷰)·차단 추이/이력·세션 테이블.
- 비용: 시간별 원가 스택(AI vs 프록시)·건당 평균·평균 소요(축이 달라
  별도 차트 — 이중축 금지).
- 설정: API 주소·guard 키(localStorage → X-API-Key 자동 첨부).
  서버 설정 변경 UI 없음(toml 단일 소스 원칙).
- dev 는 vite 프록시(:9600)로 CORS 불필요. 5페이지 실렌더 스크린샷 검증.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 21:59:15 +09:00
민헌
61b438d8d0 feat(lps): 관리자 FE 용 API 6종 — 잡 목록/재큐·상품 목록·IP세션/차단/비용 통계
React 관리자 페이지(협의: 모니터링+필수 액션)의 데이터 소스.

- GET /v1/lps/jobs: 최신순 목록+총건수, status/q(상품코드·명) 필터.
  결과에 outcome·최저가·검색원가·오류를 평탄화해 목록에서 바로 보이게.
- POST /v1/lps/jobs/{id}/requeue: DEAD 재큐(attempts 리셋+pg_notify 워커
  깨움). 활성 중복(dedupe)이면 DB_ALREADY_SAME_KEY 로 거절.
- GET /v1/lps/products: 상품별 최신 스냅샷+누적 검색 수(최근 검색순).
- GET /v1/lps/stats/ip-sessions: 종료사유 분포·요청수 히스토그램·차단
  세션 최소 요청수(예산 튜닝 기준선)·최근 세션 50.
- GET /v1/lps/stats/bot: 시간대별 차단 + 최근 감지 목록.
- GET /v1/lps/stats/cost: 시간별 원가(AI/프록시 분해)+평균 소요.
- AdminService/admin_protocol/admin 라우터 신설, guard 일괄 적용.
  설정 변경 UI 는 두지 않음 — toml 단일 소스 원칙.
- 테스트 9건 추가, 전체 154 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 21:42:38 +09:00
민헌
8fb54be830 feat(lps): local/dev/prod 환경별 실행 체계 — 환경 toml 2종 + 대화형 run_docker.sh
다른 개발자가 어느 환경이든 같은 방식으로 띄울 수 있게 한다.
환경 = APP_ENV 가 고르는 config.<env>.toml 하나(구조 동일, 값만 다름).

- config.docker.toml → config.dev.toml 개명, config.prod.toml.example 신설
  (prod 성격 반영: api_keys 필수 표기·debug 로그 금지·웹훅 권장).
- compose 의 lps 서비스 APP_ENV/마운트를 ${APP_ENV:-dev} 로 파라미터화,
  API 포트를 ${LPS_API_BIND:-0.0.0.0} 바인드로 노출 제어.
- run_docker.sh(대화형) 신설: 환경 선택 → 설정 파일 검증(없으면 example
  복사 제안) → 기동/재시작/중지/로그. prod 는 guard 키 비면 경고 후
  확인받고, LPS_API_BIND=127.0.0.1 자동 설정(외부는 리버스프록시 경유).
- 운영 가이드에 환경 개요 표, README 빠른 시작에 Docker 실행 추가.
- 검증: 스크립트 문법·compose(dev/prod) 파싱·APP_ENV=dev 로딩·145 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 21:22:53 +09:00
민헌
c81df5bd88 refactor(lps): 설정을 TOML 단일 소스로 통합 — env/.env 이중 관리 제거
설정이 .env(compose 주입)·config.toml·코드 곳곳의 os.environ 직독 3계층에
흩어져 관리가 어려웠다. TOML 하나로 통합한다(협의 결정).

- 신설 [WorkerConfig](동시성·폴백·프로필·데드라인·유예·Chrome·하트비트),
  [AlertConfig](웹훅·쿨다운·임계 10종). [WebServerConfig].api_keys(guard),
  [DecodoConfig].ip_request_budget/port_cooldown_sec 추가 — 흩어져 있던
  LPS_* env 20여 개를 섹션으로 흡수.
- server_configs 의 env override 계층(DB_*·시크릿·NAVER_KEYS 등) 삭제.
  남는 env 는 APP_ENV(부트스트랩)·PROCESS_COUNT/WORKER_CONCURRENCY(실행
  스크립트 대화형 입력 전용)·LPS_LIVE(테스트 옵트인)뿐.
- Docker: env 주입 → config.docker.toml 마운트 + APP_ENV=docker.
  이미지 무시크릿 유지, 마운트 누락 시 FileNotFoundError 즉시 실패.
  .env.example 삭제, config.docker.toml.example 신설.
- negodata 호출부: guard 키를 env 직독에서 [WebServerConfig].lps_api_key
  (+기존 관례대로 env override)로 이동.
- 실행 스크립트: 프로필·폴백·예산 프롬프트 제거(toml 소스 안내),
  동시성/프로세스 수만 임시 override 로 유지.
- docs 7종·example toml 의 env 표기를 toml 키로 일괄 갱신.
- 전체 145 passed + APP_ENV=docker 로딩·API 기동 스모크 확인.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 21:11:31 +09:00
민헌
993301be8b docs(lps): README·docs 최신화 — IP 선제 회전·알림 10룰·guard·ip_session 반영
이번 기능 3종+알림 확장 이후 문서와 코드의 어긋남을 정리한다.

- README: 주요 기능 표에 선제 회전(예산 3회)·임계 알림·API guard 추가,
  차단 대응 설명을 '막히기 전 교체' 순서로 재서술, 폴더 구조에
  alerts/ip_session 반영, 데이터베이스 문서 링크를 5종으로 수정.
- api.md: /v1/lps/ops 운영 스냅샷 섹션 신설(필드 주석 포함),
  /readyz 문서화, HTTP 401(guard) 상태 코드 추가.
- architecture.md: 구성요소 표에 관측·알림/API guard 행 추가.
- database.md: 제목 '테이블 5종'으로 수정.
- operations.md: 테스트 수 96→145, 로그 읽는 법에 예산 선제 회전·
  포트 쿨다운 로그 2행 추가.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 20:46:59 +09:00
민헌
ca7f057e41 feat(lps): 알림 룰 4종 추가 — 데드라인·검색원가·포트 고갈·예산 누수
협의로 선정한 조기 신호 4종을 AlertManager 에 추가한다.

- deadline: 최근 1h JobDeadlineExceeded 수 ≥ LPS_ALERT_DEADLINE_1H(5).
  재시도로 살아나면 dead 룰엔 안 잡히는 크롤 행 반복 신호를 별도 집계.
- cost: 최근 1h 완료 잡 검색원가 합 ≥ LPS_ALERT_COST_1H_USD(1.0).
  비용의 87%가 프록시 대역폭 — 리소스차단 풀림·재시도 루프의 조용한
  비용 폭주를 감시. job.result 의 metrics.cost.total_usd JSONB 합산.
- proxy_ports_low: 가용 포트 비율 ≤ LPS_ALERT_PORTS_LOW_PCT(30%).
  쿨다운 격리 누적 — blocks_1h(80건)보다 먼저 우는 대규모 차단 조기
  신호. 워커별 프록시 중 가장 소진된 것 기준(min).
- budget_leak: 최근 6h end_reason=block 세션 ≥ LPS_ALERT_BLOCK_SESSIONS_6H(1).
  요청 예산(3회)을 지켰는데도 차단됨 = 예산 하향 검토 신호.
- deadline_1h·cost_1h_usd 는 queue.ops() 에 편입 → /v1/lps/ops 로도 노출.
  포트·세션 지표는 워커 웹훅 스냅샷에 포함(프록시 상태는 워커에만 있음).
- 테스트 4건 추가(ops 집계 2·포트 스냅샷 2), 전체 145 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 17:29:08 +09:00
민헌
e7d2c87fbe feat(lps): API guard — LPS_API_KEY 설정 시에만 /v1 에 X-API-Key 검증
외부에서 API 를 함부로 호출(비용 발생 enqueue 등)하지 못하도록 정적 키
guard 를 추가한다. '키의 존재'가 토글 — 개발(local/dev)은 env 를 비워
개방 모드(기동 시 WARN), prod 만 키를 주입한다(협의 결정).

- router/v1/validator/auth.py: X-API-Key 의존성 — secrets.compare_digest
  상수시간 비교, 콤마 구분 복수 키(무중단 키 교체), 매 요청 env 조회
  (재기동 없이 테스트 가능). /v1 라우터 전체에 적용.
- /healthz·/readyz 는 라우터 밖이라 항상 개방(LB 프로브).
- negodata lps_sync_service: LPS_API_KEY env 있으면 헤더 자동 첨부(한 곳).
- compose(lps-api·negodata-backend) LPS_API_KEY 패스스루 + .env.example.
- prod 체크리스트(operations.md): 키 주입 + lps-api 포트 비공개 + 기동
  로그 'API guard ON' 확인. api.md 인증 섹션 추가.
- 라이브 스모크: 무헤더/오키 401 · 정키 2종 200 · healthz 200 확인.
- 테스트 6건 추가, 전체 141 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 16:45:31 +09:00
민헌
8b9e81777c feat(lps): 알림 확장 — AlertManager 쿨다운·회복, DB풀 포화·소스별 장기실패 룰
기존 ops-monitor 는 임계 초과가 지속되면 30초마다 같은 웹훅을 반복 발송했고
(쿨다운 없음), 해소 여부도 알 수 없었다. 감시 항목도 큐 지표 4종뿐이었다.

- common/alerts.py AlertManager 신설: 룰 키별 상태 관리 — 발화 1회 +
  쿨다운(LPS_ALERT_COOLDOWN_MIN, 기본 30분)마다 리마인드, 해소 시 회복
  알림 1회. sender/clock 주입으로 네트워크·대기 없이 단위 테스트.
- 워커 ops-monitor 를 AlertManager 로 이관(기존 4룰 유지) + 신규 2룰:
  db_pool(풀 포화율 ≥ LPS_ALERT_POOL_PCT 90%) ·
  source_fail:<src>(최근 30분 시도 ≥ LPS_ALERT_SOURCE_FAIL_30M(5) & 성공 0
  — 쿼터 소진·셀렉터 드리프트·전면 차단 신호).
- DBSessionManager.pool_status(): 전 엔진 합산 checked_out/capacity/pct.
- SearchAdapter 에 시간 윈도우 성공/실패 카운터(recent_stats) — 누적
  카운터로는 '최근 30분 성공 0건'을 볼 수 없어 추가. 쿠팡(브라우저)·
  네이버(API) 성공/실패 지점에 배선.
- API 자체 풀 모니터: lifespan 백그라운드 태스크(run_pool_monitor) —
  대량 폴링으로 풀을 고갈시키는 주범이 API 자신일 수 있다.
  /v1/lps/ops 에 pool_checked_out/pool_capacity/pool_pct 노출(스모크 확인).
- 테스트 9건 추가(발화·쿨다운·회복·룰 독립·윈도우 카운터·풀 현황), 전체 135 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 16:42:12 +09:00
민헌
3c5286aeec feat(lps): IP 선제 로테이션 — 요청 예산·포트 쿨다운·ip_session 관측
쿠팡 크롤 IP 를 '막힐 때까지' 쓰던 방식을 '막히기 전에 교체'로 전환한다.

- 요청 예산(LPS_IP_REQUEST_BUDGET, 기본 3): IP당 요청 수가 예산에 닿으면
  차단 전에 선제 회전. 실측상 5회 부근 차단 이력이 있어 보수적으로 3회.
  선제 교체된 포트는 평판이 깨끗해 로테이션 복귀 시 재사용된다.
- 포트 쿨다운(LPS_PORT_COOLDOWN_SEC, 기본 max(sticky,30분)): 차단 감지·
  전송오류 포트는 격리하고 _port() 가 건너뛴다. 전 포트 쿨다운이면 만료
  임박 포트 사용(가용성 우선). 포트 수는 config 범위에서 동적 산출.
- 차단 재시도 소진 시에도 회전 예약 — 불탄 포트로 다음 검색을 하지 않음.
- ip_session 테이블 신설: 세션마다 요청 수·성공/차단·종료 사유(budget/
  block/proxy_error/window/idle/shutdown)를 기록. bot_detection 과 달리
  무사 종료도 남아 예산 상한 튜닝의 원천 데이터가 된다(쿼리 database.md).
  models.py·migrations·init.sql(lps_db 섹션) 동행 갱신, dev DB 적용 완료.
- 테스트 17건 추가(쿨다운·예산 판정·세션 기록·CRUD), 전체 126 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 16:35:44 +09:00
민헌
f97e407f7c Merge branch 'main' into feature/negodata-lps 2026-07-13 13:55:36 +09:00
21f90ed84a [fix] negodata/front: 윈도우 브라우저 폰트 미적용 수정 — format woff2-variations 를 표준 woff2 로 교체 2026-07-13 08:47:09 +09:00
ac487268d8 postgres-init lps db 추가. 2026-07-10 17:19:24 +09:00
민헌
9ef9bfbca3 feat(negodata/front): 최저가 상세보기 버튼 노출 + 상세 시트 가독성 정리
- 상품 테이블 인터넷 최저가 셀: 값 옆에 차트 아이콘 버튼 상시 노출
  (기존 hover 밑줄만으론 클릭 가능성이 안 보임) — 값 없는 상품도
  버튼으로 진입해 빈 상태 안내를 받는다
- 상세 시트 재구성: 견적 상세 드로어와 같은 SectionCard 문법으로 통일
  · 헤드라인 카드: 상품명 → 대표가(26px rose) + 단가 대비 칩
    (절감=emerald·역전=amber, Trending 아이콘) + 단가·마지막 수집 캡션
  · 최저가 출처: 섹션 타이틀 우측 사이트 배지, 링크는 outline 버튼화
  · 가격 추이: 수집 횟수 캡션, 2회 미만이면 안내 문구(빈 영역 방지)
  · 수집 이력: 배지 폭 고정(w-14)으로 컬럼 정렬, 링크 없는 행 자리 유지

검증: 헤드리스 렌더 확인(테이블 셀·시트 v2 스크린샷), tsc 통과

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 17:18:09 +09:00
민헌
4a20194dc8 feat(negodata): 인터넷 최저가 출처·링크·가격 추이 UI — 검증 가능한 협상 근거
인터넷 최저가는 견적 목표가·협상 카드(NGC-008 시장가 근거)의 근거값
— "어디서 찾았는지"를 클릭 한 번으로 검증할 수 있어야 한다.

- 스키마: iilp 에 lp_name(찾은 상품명)·lp_url(판매 페이지 링크) 추가
  (alters/2026-07-10-iilp-source-link.sql, init.sql 멱등 반영·dev 적용)
- 동기화: price_history 의 final_source 에 맞는 naver/coupang name·url
  을 함께 반영(기존 행은 NULL 공존)
- API: LowestPriceEntry 에 lp_name/lp_url + orval 재생성
- UI: 상품 테이블 인터넷 최저가 셀 클릭 → 상세 시트
  · 대표가(rose) + 상품 단가 대비 차액(절감=emerald/역전=amber)
  · 출처 카드: 사이트 배지·찾은 상품명·판매 페이지 새탭 링크
  · 가격 추이 라인 그래프(성공 수집 2건부터, recharts+ChartContainer
    라이트/다크 쌍 — 통계 차트와 동일 문법)
  · 최근 수집 이력 리스트(미발견 amber, 행별 외부링크)

검증: 실데이터 e2e(A4 재수집 → lp_name/lp_url 반영 확인) +
헤드리스 브라우저 렌더 확인(시트·그래프·툴팁·링크), tsc·build 통과

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 17:10:00 +09:00
민헌
1cce752b47 Merge branch 'feature/negodata-lps' — negodata↔LPS 인터넷 최저가 연동(수동 트리거 전용) 2026-07-10 16:49:49 +09:00
민헌
5ddf5f6249 refactor(negodata): LPS 자동 요청 배치 제거 — 검색은 수동 트리거 전용
협의 결정(2026-07-10): 크롤 비용이 사용자 행동에만 비례하도록,
갱신 오래된 상품을 자동으로 검색 요청하던 잡(매일 04:00, 상한 500건,
일 ~$2)을 제거한다. 검색 진입점은 상품 화면의 수동 트리거
(POST /v1/item/{id}/lowest-price) 하나만 남는다.

- request_lps_searches 잡·request_stale_searches 서비스·stale_items
  CRUD·배치 상수(REFRESH_HOURS 등) 제거
- 수집 잡(sync_lps_results, 5분)은 유지 — 크롤을 일으키지 않는
  반영 백스톱(모달 조기 종료·폴링 초과분 자동 반영, 비용 0)

검증: 스케줄러 등록 잡 3개(견적마감 2 + LPS 수집 1) 확인, 수집 잡 실행 정상

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 16:47:39 +09:00
민헌
3171311746 feat(negodata/front): PriceUpdateModal 데모 → LPS 실연동
- 가짜 setTimeout 애니메이션 제거 — 상품별 POST lowest-price(큐 접수,
  duplicated 는 결과 대기 합류) 후 GET 5초 폴링(서버가 조회 시 lps_db
  증분 동기화를 겸하므로 폴링이 곧 수집)
- 요청 시각 이후 crawl_end_time 이력만 이번 결과로 인정, 완료/미발견
  로그 구분(emerald/amber), 5분 대기초과분은 주기 동기화 자동반영 안내
- 종료 시 /v1/item/list invalidate → 테이블 인터넷 최저가 컬럼 갱신
- 닫기 = 폴링만 중단(서버 검색은 계속됨을 UI 에 명시)
- orval 재생성: lowest-price 응답 타입화(LowestPriceEntry·lowest_price)

검증: tsc 통과, vite build 통과 (API 흐름 자체는 ③ e2e 로 검증됨)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 16:32:38 +09:00
민헌
28b4a9e797 feat(negodata): lowest-price 스텁 API 실구현 — 즉시 트리거 + 온디맨드 동기화 조회
- POST /v1/item/{id}/lowest-price: 상품 1건 LPS 즉시 검색요청(manual
  우선순위). queued/duplicated/unavailable 상태 반환, 실패는
  ErrorType.LPS_UNAVAILABLE(2000 블록 신설)
- GET /v1/item/{id}/lowest-price: 대표 최저가 + 최근 수집 이력(최신순,
  LowestPriceEntry 타입화). 조회 전 lps_db 증분 동기화 1회 수행 —
  5분 크론을 기다리지 않는 실시간 폴링 UX(멱등·저비용)
- 동시성 방어 2겹: 프로세스 내 asyncio.Lock(크론·온디맨드 직렬화) +
  uq_iilp_item_crawl_time 유니크 인덱스(alters/2026-07-10, init.sql
  멱등 반영·dev DB 적용) — 경합 진 쪽 tx 실패 후 다음 tick 흡수
- LpsSyncService 무인자 생성자(FastAPI Depends 호환)
- compose: negodata-backend 에 LPS_DB_HOST·LPS_BASE_URL env(미설정 시
  연동 비활성으로 조용히 동작)

검증(도커 컨테이너 e2e): 인증→POST queued→LPS 크롤→GET 온디맨드
동기화로 이력 즉시 노출(not_found 정책: 대표값 미변경 확인),
company 스코프 차단(타사 상품 ITEM_NOT_FOUND) 확인

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 16:17:05 +09:00
c0e6987c3c [fix] negodata/front: 통계 카테고리별 절감 차트 -> 손해(음수)는 안보이게, 대시보드에서 일반관리자도 회사 통계 보이게 수정 2026-07-10 15:58:13 +09:00
민헌
523661e3d2 feat(negodata): LPS 최저가 동기화 배치 — 요청(API)·수집(lps_db)·반영(items)
설계(2026-07-10 결정): 요청은 LPS API(dedupe·워커알림 보존), 결과는
lps_db.price_history 직접 읽기(읽기전용 엔진). product_code=item_id
로 결과가 자동 매핑된다.

- 잡③ request_lps_searches(매일 04:00 KST): 24h 이상 미수집 상품을
  최대 500건 enqueue(job_type=batch, 100건/콜 청크) — 비용 발생 잡
- 잡④ sync_lps_results(5분): 워터마크(max crawl_end_time) 증분 수집
  → item_internet_lowest_prices append(성공/실패 모두) + 성공분 최신값
  items.internet_lowest_price 박제(+yn). 한 트랜잭션(부분반영 방지)
- 스캔 바닥 3일 — 비uuid·미존재 상품 행이 워터마크를 못 올려도
  재스캔 범위 유한
- ⚠️ 시각은 aware UTC 통일 — naive 를 timestamptz 파라미터로 넘기면
  PG 세션 타임존(KST) 해석으로 9시간 어긋남(중복 수집 실측 버그 수정)
- LowestPriceWebsite 코드 enum(naver=1 coupang=2 …), iilp ORM 모델,
  lps_base_url config(+LPS_BASE_URL env)

검증: 실상품 1건 e2e(요청→크롤 found 15,000원→이력+박제 반영),
멱등성(재실행 빈 카운터), 비uuid 12건 스킵, 대상선정 쿼리 500건 상한

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 15:38:57 +09:00
b41f7c67f1 [feat] negodata: 카드 에디터 조건 전략 변수 + 미리보기 변수 라벨 치환
① 카드 목록 스크립트 미리보기에서 {변수} 토큰을 한글 라벨 칩으로 렌더
   (renderCardScriptPreview) — 원시 토큰({prev_partner_price} 등) 노출 제거.
② "조건 전략"(customer_condition) 특수 변수 추가:
   - 툴바 '+ 조건 전략' → 본문에 위치 칩 삽입, 본문 아래 별도 Slate 입력창 노출
   - 조건 내용은 변수 노드에 중첩 보관(edit_script 배열 유지·하위호환) + 저장 시
     script 의 해당 위치에 주입 → agent 는 완성된 문구를 그대로 읽음(무변경)
   - 서브 에디터는 조건 전략 버튼 숨김(중첩 방지), 빈 조건 저장 차단

DB·백엔드·agent 변경 없음(edit_script 타입 unknown, 조건 노드 내 중첩).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 15:18:11 +09:00
민헌
9e9de52200 feat(negodata): lps_db 읽기전용 연결 기반 — DBType.LPS + 조건부 엔진 등록
LPS(인터넷 최저가 검색) 연동 1단계. price_history 동기화 배치가 쓸
읽기전용 접속을 기존 확장점(DBType enum + 세션매니저 맵)에 등록한다.

- DBType.LPS=2, LpsDBConfig(read_* 만 — write 엔진 미등록으로 앱 차원
  읽기전용 강제, write 요청 시 KeyError)
- config name 비면 미등록(구버전 toml·LPS 없는 환경에서도 부팅 가능),
  배치는 is_registered() 로 사전 확인
- env override: LPS_DB_HOST/PORT/USER/PASSWORD/NAME (도커용)
- 검증: 엔진 등록·읽기 세션·write 가드 KeyError·lps_db.price_history
  실조회 확인

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 15:11:18 +09:00
26d9fa3c4b Merge branch 'feat/chat-update' — 협상 고도화(LLM 표현/이해층·카드 전술·변수 카탈로그) 2026-07-10 13:39:19 +09:00
22500eae4c [chore] compose: agent 에 OPENAI_API_KEY passthrough — LLM 표현/이해층 키 주입
빈 값이면 무시(server_configs env override 가 값 있을 때만 적용)라 무해.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 13:37:50 +09:00
afacd74382 [feat] negodata: 카드 변수 카탈로그에 전술 변수 6종 추가
agent 전술 재설계로 늘어난 치환 변수를 편집기 툴바·칩에 노출:
제시가(input_price)·협력사 직전가(prev_partner_price)·제안가(counter_price)·
중간가(target_mid_price)·절충가(middle_price)·인하율(discount_rate).
name 은 agent chat_engine.vars_for 치환 키와 일치. 라벨은 렌더 시점 조회라
기존 저장 카드도 마이그레이션 없이 한글 라벨로 표기된다.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 13:37:50 +09:00
1682481b01 [feat] agent: 협상 고도화 — LLM 표현/이해층 + 카드 전술 실행계층 + ktcommerce 정리
카드 재설계("멘트 카드 → 전술 카드"):
- tactics.py 신설: 카드번호→전술(카운터 산식) 레지스트리, min(counter,target) 클램프
- 카운터 수락=즉시 타결(pending_counter_price 일반화, 구 offer_1pct 흡수)
- 목표가 초과 타결 금지(성공스텝 진입 가드) + "카드 소진=실패" 폐지→종결 국면
- 선택형 와일드카드(WC-*) 발동 + card.wild_cards 멘트 DB 어댑터

LLM 계층:
- Phase 2 표현층 ScriptNaturalizer(카드 멘트 자연화, 마커·치환자·숫자 보존 검증)
- Phase 3 이해층 InputInterpreter(자유발화 NLU→기대입력, 한국어 가격 파서)
- OPENAI_API_KEY env override(server_configs) + 전역 자격증명 게이트

결정 스택(Phase 1):
- 협상 규칙 데이터화(negotiation.wildcard_*_ratio/max_counter_rounds)
- 선택카드 우선순위 prior(UCB 방문수 감쇠, Q-table 오염 없음)

버그픽스:
- 인하율 음수 표기 제거 + 인상/동일/인하 구분(discount_phrase)
- 자연화 강조마커 보존(볼드/색 소실 시 원본 폴백)
- 카드 시드 가격변수(prev_partner_price·target_mid_price·middle_price 등) 치환

정리:
- ktcommerce 테넌트 삭제 + 테스트 21파일 imarketkorea/_base 로 마이그레이션
- 실 LLM 호출 차단 conftest 가드

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 13:37:36 +09:00
민헌
18f6466a8b Merge branch 'feature/lps' — 인터넷 최저가 검색(LPS) 시스템 2026-07-10 13:30:48 +09:00
민헌
d4cee1caf7 feat(lps): API 스케일 파라미터를 .env 로 노출 — PROCESS_COUNT·커넥션 예산
주석 상태였던 PROCESS_COUNT/DB_CONNECTION_BUDGET 를 활성화하되 서버별
값은 .env(LPS_API_PROCESS_COUNT, LPS_DB_CONNECTION_BUDGET)로 조정.
기본 1/40 — API 는 단일 프로세스로도 ~1,100 RPS(실측)라 평시 충분,
부하 대비 시에만 상향. 풀 자동 산정이 예산 초과를 방지.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 12:02:17 +09:00
민헌
a267326bf7 fix(lps): Docker 이미지 무시크릿화 + 배포 견고화 — 리뷰 발견사항 일괄 적용
시크릿 유출 차단(핵심):
- config.local.toml(OpenAI·DECODO·네이버 키)이 COPY . . 로 이미지에
  구워지던 문제 — .dockerignore 제외 + 빌드 시 example(플레이스홀더)
  복사로 대체. 실값은 compose env 주입(리포 루트 .env, 템플릿 .env.example)
- DECODO_PORT_START/END/SESSION_MINUTES env override 추가 — 포트가
  toml(플레이스홀더 0)에만 있으면 자격증명을 넣어도 프록시가 조용히
  꺼지는 구멍 봉합

배포 견고화:
- API Dockerfile 에 HEALTHCHECK(/healthz) 추가
- autoheal 컨테이너 추가 — compose restart 는 unhealthy 를 재시작하지
  않으므로 라벨(autoheal=true) 기반 자동 재시작 담당
- 이미지 python 3.12→3.14 정렬(로컬 개발·테스트 환경과 일치)
- API 이미지 경량화: requirements-api.txt 분리(크롤 의존성 제거, 330MB)

검증: 양 이미지 빌드 성공, 이미지 내 시크릿·.profiles 부재 확인,
무시크릿 API 이미지 스모크(healthz/readyz/HEALTHCHECK healthy) 통과

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 11:53:47 +09:00
민헌
c075dfbcfb fix(lps): .dockerignore 에 .profiles/ 추가 — Chrome 세션 이미지 유출 차단
COPY . . 가 로컬 Chrome 프로필(쿠키·cf_clearance, 149MB)을 API·워커
이미지에 굽고 있었다. 컨테이너는 빈 프로필에서 웜업으로 쿠키를 만들고
volume(/profiles)에 영속하는 설계라 이미지에 있을 이유가 없다.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 11:45:19 +09:00
민헌
4381649658 docs(lps): 워커·브라우저 1:1 근거와 워커 수 상한 산정법 문서화
- 6-2: 브라우저가 아니라 신원(프로필+sticky IP)이 희소 자원인 이유,
  비율이 어긋날 때의 손해(유휴 낭비 vs 락 직렬화), 신원당 처리량 캡
- 6-3: 워커 상한 = min(RAM캡, 포트캡÷3, 실측 정체점) — 하드캡 계산식과
  계단식 실측 시 멈춤 신호 5가지(처리량 정체·blocks_1h·메모리·
  단일코어 포화·p95)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 11:32:12 +09:00
민헌
463416c3a2 fix(lps): 워커 행(hang) 3중 방어 — 잡 데드라인·웜업 타임아웃·정리 루프 격리
부하테스트(N=10) 실측: 쿠팡 브라우저 재기동 경로 행으로 워커 2/3가
12~15분 정지(쿠팡 검색 1콜 880s), heartbeat 가 lease 를 계속 갱신해
reaper 회수 불가 + stuck_running=0 으로 관측 사각. 이에 대한 방어:

- P1 잡 데드라인: runner._process 에 wait_for(기본 300s, env
  LPS_JOB_DEADLINE_SEC) — 초과 시 취소·fail → 백오프 재큐/DEAD
- 웜업 시도당 60s 타임아웃 — 웜업이 어댑터 락을 쥔 채 행하면
  그 워커의 실 검색이 전부 락 대기로 동반 정지(실측 케이스)
- P2 browser reaper: close_if_idle 에 60s 타임아웃 — 한 어댑터
  close 행이 정리 루프 전체를 멈추는 것 방지(실측 케이스)
- P3 stuck_running: lease 만료 OR 실행 10분 초과(run_started_at)
  — 행 상태에서도 지표·알림에 잡히게

테스트 5건 추가(데드라인 DEAD/재큐, stuck 지표, reaper 격리)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 11:27:03 +09:00
민헌
fbd2cec1da test(lps): 부하테스트 상품셋 100종 카탈로그 분리 — 실제 입력 분포 재현
- loadtest/catalog.json 신설(실존 상품 100종): 규격만 54 · 모델포함 26 ·
  이름만 20 · 기준가 포함 7 (price→가격밴드 필터 경로 포함)
- loadtest.py 는 인라인 6종 대신 카탈로그 로드, 실행 시 구성 요약 출력

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 10:50:28 +09:00
민헌
5e485e9b6e feat(lps): 오픈마켓 폴백 기본 비활성(LPS_FALLBACKS 토글) — 협의 결정 B안 적용
2026-07-10 협의: 검색은 네이버+쿠팡만. G마켓·옥션·11번가 폴백은 최종 최저가
기여 0회에 검색당 최대 15s·프록시 비용 ~87%를 차지해 로직에서 제외.
주석처리 대신 env 토글로 코드·테스트는 살려둔다(부패 방지·env 한 줄 재가동).

- worker_main: LPS_FALLBACKS(기본 빈값=OFF)로만 폴백 어댑터 생성, 잘못된 값 경고,
  기동 로그에 폴백 상태 표기. 핸들러는 빈 폴백을 원래 정상 처리라 로직 변경 없음
- run_local_worker.sh: 폴백 여부 대화형 질문 추가(기본 비활성)
- compose: LPS_FALLBACKS 주석 env(재가동용)
- decision-openmarket-crawler.md: 결정(B)·근거·재가동 절차(라이브 스모크 선행) 확정 기록
- README·architecture·operations: 기본 비활성 반영

검증: 전체 테스트 106 passed(폴백 로직 테스트는 fake 주입이라 계속 유효),
워커 실기동 로그 '오픈마켓 폴백: OFF' 확인

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 10:14:39 +09:00
7f1c0be2b3 [feat] negodata: 협력사 채팅 계정(supplier_users) 관리 — 발급·비번 재설정·활성상태
- 백엔드: supplier_users/supplier_user_tokens ORM 매핑, /v1/supplier/{id}/account 4종(조회·발급·재설정·상태), 회사 스코프 게이팅, 재설정·비활성 시 토큰 삭제로 기존 로그인 즉시 무효화
- 비밀번호: 발급=서버 자동생성(1회 반환), 재설정=커스텀 지정 가능(미지정 시 자동생성)
- 프론트: 협력사 상세 SupplierAccountManager 섹션 + 목록 '채팅 계정' 컬럼, orval 재생성(SupplierType 은 enumLabels 로컬 상수로 이동)
- 테스트: test_supplier_account.py 9건, 전체 스위트 green
2026-07-10 10:14:00 +09:00
민헌
89fe2c9805 feat(lps): 실시간 관측 대시보드 추가 — 큐 추이·코어별 CPU·프로세스 그룹 사용률
loadtest/monitor.py (:9700, 단일 파일·외부 인프라 없음) + run_monitor.sh(대화형).
e2e 부하(N=100 loadtest.py) 중 "프로세스가 잘 진행되는지, 코어가 전부 도는지,
병목이 어느 층인지"를 브라우저에서 2초 간격으로 본다:

- 큐 추이: /v1/lps/ops 폴링 — PENDING/RUNNING/DONE/DEAD 라인 + 처리량(개/분) 타일
- 프로세스 그룹 CPU: worker/api/chrome/postgres — 병목 층 판독
  (chrome 은 워커 자손만 집계해 사용자 브라우저와 분리, uvicorn spawn 자식은 부모로 api 귀속)
- 코어별 사용률 막대: 멀티코어 활용 확인
- 현재 스냅샷 표 + 호버 툴팁 + 라인 끝 직접 라벨(dataviz 팔레트 검증 통과, 다크 서피스)
- psutil 의존성 추가(로컬 관측 전용)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 09:50:14 +09:00
민헌
64d5063041 feat(lps): 서버 실행 스크립트에 멀티코어 대화형 선택 추가
일반 실행(1) 선택 시 PROCESS_COUNT 를 물어보고(기본 1, CPU 코어 수 표기),
2 이상이면 DB_CONNECTION_BUDGET 도 물어본다(기본 96 — 전용 PG 벤치값).
워커 스크립트(run_local_worker.sh)의 동시성 질문과 같은 대화형 패턴.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 09:09:55 +09:00
민헌
47c64eaf8b fix(lps): 워커 graceful shutdown — 신호 핸들러·잡 마무리 유예·정리 격리
- SIGINT/SIGTERM 핸들러 등록: cancel 대신 stop 이벤트 set → 새 잡 클레임 중단,
  하던 잡은 마무리 후 자연 종료(트레이스백 없이 exit 0). 신호 재수신 시 강제 종료
- 종료 유예 LPS_SHUTDOWN_GRACE_SEC(기본 60s) 초과 시 강제 취소(잡은 lease 만료 후 재큐)
- 워커/리퍼가 예외로 죽으면 기존처럼 전파하되, finally에서 남은 태스크 취소·완주 대기 후 정리
- 리스너·어댑터 정리를 항목별 try/except로 격리 — 하나 실패해도 나머지 Chrome 정리
- 웜업(bg) 태스크는 종료 신호 즉시 취소해 어댑터 락 해제
- compose lps-worker에 stop_grace_period: 75s (기본 10s면 드레인 전 SIGKILL)
- 운영 가이드에 워커 종료 절차 문서화

검증: SIGTERM/SIGINT 실기동 테스트 — graceful 로그 후 exit 0, 기존 테스트 26개 통과

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 09:03:57 +09:00
민헌
ddb6f6bd2f fix(lps): 워커 로그 노이즈 2건 제거 — 리퍼 네이버 스킵 + 폴백 데드라인 cancel 제거
- browser-reaper가 close_if_idle 없는 어댑터(네이버 httpx)를 건너뛰도록 getattr 가드
  → 30초마다 반복되던 ERROR 로그 제거
- 폴백 데드라인을 wait_for(cancel) → asyncio.wait(버림)으로 변경
  → in-flight page.goto 취소 시 patchright 내부 future가 남기는
    'Future exception was never retrieved' 노이즈 제거, 페이지 어중간 상태 방지
  → 버려진 태스크는 강한 참조 집합(_abandoned_fallbacks)에 보관(GC 중도 파괴 방지),
    종료 시 콜백이 예외 회수 후 집합에서 제거
- 데드라인 테스트에 잔여 태스크 배수(drain) 검증 추가

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 08:50:12 +09:00
민헌
08a9c52088 docs(lps): 아키텍처에 API 멀티코어 스케일·커넥션 풀 자동산정(6-1) 추가
process_count 를 늘릴 때 풀 오버서브스크립션→커넥션 고갈 함정과
connection_budget 기반 자동 역산 해법을 문서화.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 08:35:48 +09:00
민헌
0e30354ba5 feat(lps): 로컬 워커·부하테스트(GUI) 실행 스크립트 추가 (대화형)
기존 run_local_server.sh 스타일에 맞춘 대화형 .sh 두 개.

- run_local_worker.sh: 동시성(WORKER_CONCURRENCY)·Chrome 프로필(LPS_PROFILE_DIR)
  선택 후 워커 기동. 실행 중 워커 감지 시 교체 여부 확인. venv/config 자동 보장.
- run_loadtest_gui.sh: Locust 웹 UI(:8089) 기동. 워커 실행 중이면 경고(크롤 비용),
  API 미기동 시 PROCESS_COUNT 받아 백그라운드 기동(종료 시 함께 정리). 부하생성기 --processes.
- .gitignore(lps): .profiles/(Chrome 쿠키·cf_clearance 세션) 커밋 방지
- README·operations: 새 스크립트 안내로 갱신

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 08:33:11 +09:00
민헌
9c2432c818 feat(lps): process_count 기반 커넥션 풀 자동 산정 — 멀티코어 풀 오버서브스크립션 방지
connection_budget(기본 40)을 두고, 기동 시 process_count 에 맞춰
pool_size/max_overflow 를 역산: (pool+overflow)×2엔진×process_count ≤ budget.
워커를 늘려도 config 가 스스로 예산을 지켜 커넥션 고갈→요청 실패를 예방.

- config_models: MainDBConfig.connection_budget 추가
- server_configs: _autosize_pool(process_count 확정 후 산정) + _apply_pool_env_override
  (우선순위 = 명시 DB_POOL_SIZE > 자동 산정 > toml pool)
- web_main: 기동 로그에 실효 풀/총커넥션/예산 출력
- env: DB_CONNECTION_BUDGET override, docker-compose 에 knob 노출
- tests: test_pool_autosize 10케이스(예산 준수·분할비·비활성·infeasible 바닥)
- docs: operations 2-1 멀티코어/풀 섹션 + loadtest README 자동산정 표

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 23:59:05 +09:00
민헌
a63793daf0 test(lps): Locust API 부하 테스트 + 멀티코어 벤치 (풀 병목 발견·증명)
API(enqueue/조회, asyncpg I/O 바운드) 부하 테스트로 멀티코어 활용·한계 측정.

- loadtest/locustfile.py: enqueue(고유코드 write)+조회 가중 부하.
- loadtest/bench_multicore.sh: PROCESS_COUNT 1→N 자동 비교(--processes 로 부하생성기도 멀티프로세스).
- server_configs: PROCESS_COUNT / DB_POOL_SIZE / DB_MAX_OVERFLOW env override(코드·toml 수정 없이 튜닝).
- loadtest/README.md: 발견 문서화.

발견(11코어·1500users): 기본 풀(10/20)로 워커 늘리면 실패 폭증(1w=0 → 4w=10336) —
(pool+overflow)×2엔진×workers=240 > PG max_connections=100 커넥션 고갈(SQLAlchemy pool checkout 실패).
증명: 풀 8/4(96<100)로 PC=4 재실행 → RPS 1336→2705(2배), 실패 0. 코드는 멀티코어 활용 가능,
막는 건 풀 오버서브스크립션. 규칙: (pool+overflow)×2×process_count ≤ max_connections.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 23:45:25 +09:00
민헌
eba78dbbb9 docs(lps): README 최신화 — Dockerfile.worker·문서목록(배포/관측/크롤러논의)
폴더구조에 Dockerfile.worker, 문서 표에 Docker배포·관측·decision-openmarket-crawler 반영.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 23:32:52 +09:00
민헌
97e566f844 docs(lps): 운영 가이드 — 관측(readyz/ops/알림)·라이브 스모크·Docker 배포
P4/P5 반영: /readyz·/v1/lps/ops·워커 하트비트·임계 알림 env, LPS_LIVE 라이브 스모크,
docker compose 배포(헤드풀 Chromium+Xvfb·시크릿 env·프로필 볼륨·HEALTHCHECK).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 23:27:02 +09:00
민헌
eb1ff8fee1 test(lps): P5 브라우저 경로 스모크 — search 오케스트레이션 + 라이브 드리프트 감지
가장 취약·복잡한 경로(회전·재시도·차단감지)의 자동 테스트 공백을 메운다.

- mock 하니스(_MockPage/_MockCtx/_MockProxy)로 search() 를 결정론 테스트(브라우저 없이):
  정상 반환 / 차단→IP회전→복구 / 프록시전송오류→회전→복구 / 비프록시오류→실패(무회전) /
  차단 소진→blocked 실패. 5종.
- 라이브 스모크(각 어댑터 실제 사이트 검색→파싱): 셀렉터·안티봇 드리프트 감지. IP 의존·느려서
  기본 skip, LPS_LIVE=1 로 명시 실행(수동/야간). 네이버 스모크 통과 확인.

전체 96 passed, 5 skipped.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 23:25:42 +09:00
민헌
d6dd47e652 feat(lps): P4 관측·알림·워커 헬스 — readyz/ops + 하트비트/HEALTHCHECK + 임계 알림
프로덕션 운영 가시성. 행/좀비 워커 감지 + 큐/차단 지표 노출 + 임계 알림.

- API: /readyz(DB 도달성=readiness, 실패 503; /healthz=liveness와 구분).
  /v1/lps/ops(플랫 JSON): 큐 카운트 + oldest_pending_sec(큐지연) + dead_1h + stuck_running + blocks_1h.
- crud: JobQueue.ops()/ping(), BotDetectionLog.recent_count().
- worker: run_ops_monitor — 하트비트 파일 주기 갱신(Docker HEALTHCHECK 가 신선도로 행 워커 감지)
  + 임계(DEAD/차단/큐지연/stuck) 초과 시 WARN 로그 + (LPS_ALERT_WEBHOOK 있으면) Slack 호환 웹훅.
- Dockerfile.worker: HEALTHCHECK(하트비트 <120s). 임계·웹훅은 env(LPS_ALERT_*).
- 테스트: readyz/ops 2종.

검증: 컨테이너 healthy 판정, 하트비트 갱신, ops 스냅샷 정상. 91 tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 23:21:43 +09:00
민헌
3a8d669970 feat(lps): P2 시크릿 env 주입 + Chrome 프로필 영속 볼륨
프로덕션에서 시크릿을 이미지에 굽지 않고 env 로 주입, 재시작 시 챌린지 쿠키 유지.

- server_configs: 시크릿 env override 추가(house 의 DB override 패턴 확장) —
  OPENAI_API_KEY/MODEL, DECODO_HOST/USERNAME/PASSWORD/COST_PER_GB, NAVER_KEYS="id:sec,...".
  로컬은 env 미설정 → config.local.toml 그대로.
- worker_main: Chrome 프로필 경로를 LPS_PROFILE_DIR(기본 /tmp)로 env 화 → 영속 볼륨 마운트 가능.
- docker-compose: lps-worker 에 lps-profiles 볼륨(/profiles) + 시크릿 env 주입 주석 예시.
- config.example: 프로덕션 env 주입 목록 문서화.

검증: env override 동작(OpenAI/DECODO/Naver), 프로필이 /profiles 볼륨에 Cookies 포함 저장(cf_clearance 유지). 89 tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 23:04:37 +09:00
민헌
94d4e54d5e feat(lps): P1 컨테이너화 — 워커 이미지(Chromium+Xvfb) + compose
프로덕션 배포 토대. headless Chrome 은 Akamai/Cloudflare Turnstile 에 탐지됨(실측 실패)
→ 컨테이너에선 Xvfb(가상 디스플레이)로 headful Chromium 을 실행한다.

- Dockerfile.worker: chromium+xvfb+xauth+한글폰트. Xvfb 를 백그라운드로 띄우고 python 을
  exec 승계(로그가 docker logs 로 나옴; xvfb-run 은 stdout 을 삼킴). root=--no-sandbox 필수.
- Dockerfile: API 전용(lean, 브라우저 불필요)임을 명시.
- docker-compose: lps-api(:9600) + lps-worker(shm 1g, DB=host.docker.internal) 서비스 추가.
- browser_base: 브라우저 실행 대상 env 화(LPS_CHROME_EXECUTABLE/CHANNEL). 컨테이너=시스템
  chromium(+no-sandbox/disable-dev-shm-usage), 로컬=실제 Chrome(channel=chrome).

검증(컨테이너): DB연결·프리플라이트·쿠팡(Akamai)·gmarket(Turnstile) 웜업 통과 →
end-to-end 검색 DONE(8몰: 네이버·쿠팡·G마켓·옥션·11번가, 비용 $0.016). docker logs 로 로그 확인.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 22:56:56 +09:00
민헌
e2cd2570e3 docs(lps): 오픈마켓 크롤러 유지 여부 논의 문서
팀 논의용 의사결정 메모 — 크롤러가 비용·복잡성 ~80%인데 ROI 미증명.
현황 데이터(비용 87% DECODO·최저가 이긴 적 0·플래키), 핵심 질문(by_mall 소비처),
옵션 4종(A.TTL캐시 / B.게이트 / C.gmarket드롭 / D.현행), curl_cffi 불가 근거.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 21:51:21 +09:00
민헌
10eb67e8c6 docs(lps): 문서 최신화 — 오픈마켓 폴백·Turnstile·비용계측·동시성·프록시복원력
이번 라운드 변경을 README/아키텍처/운영 가이드에 반영.
- README: 기능표(오픈마켓 폴백·원가계측·다중병렬), 워크플로우 4-1 폴백 단계, 폴더구조
- architecture: 안티봇 대응(쿠팡 Akamai / ESM Cloudflare Turnstile 웜업·동적차단),
  검색 원가 계측(CDP 실측·컴포넌트별 비용), 다중 상품 병렬, 유휴 브라우저 정리
- operations: 로그 항목(프리플라이트·웜업·IP회전·데드라인·유휴정리), 문제해결(407·Turnstile·
  Chrome 정리·느림/비쌈), 배포유의, 원가/몰별 DB 쿼리

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 21:18:12 +09:00
민헌
77a36c43d4 fix(lps): gmarket 웜업 신뢰성 — 나쁜 IP 시 회전 재시도
웜업이 인터랙티브 Turnstile(IP 평판 나쁨)로 실패하면 다른 IP 로 회전해 최대 3회 재시도.
gmarket 커버리지 플래키함 완화(100 IP 풀에서 좋은 IP 를 찾을 확률↑).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 21:06:29 +09:00
민헌
0cd817a61d fix(lps): 대역폭 비용 정확화 — CDP 실제 전송 바이트로 계측
지금까지 last_bytes=page.content()(렌더된 DOM 크기)라 실제 네트워크 전송량이 아니었다
— 리소스 차단 효과가 안 보이고 ESM DOM(~5MB) 과대계상 → DECODO 비용이 부정확했다.

- browser_base: CDP 세션(new_cdp_session) 부착, Network.loadingFinished 의 encodedDataLength
  누적 → last_bytes=실제 전송 바이트. 컨텍스트당 1회 부착, 검색마다 리셋. 미지원 시 DOM 폴백.
- 실측(gmarket): 콜드(차단해제) 3.44MB → 웜(차단활성) 1.07MB(~3x↓) — 동적 차단 효과가 이제 숫자로 보임.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 20:27:11 +09:00
민헌
8a594b667d perf(lps): Step2/3 — Turnstile 웜업·동적 차단 + 유휴 브라우저 정리
G마켓/옥션 Cloudflare Turnstile 을 실제로 통과시키고, 쌓이는 브라우저를 정리한다.

Turnstile 규명(실측): patchright 가 콜드 ~12초에 자동 통과, 통과 후 cf_clearance 쿠키로
이후 요청은 ~5초(웜). 단 **리소스를 조금이라도 차단하면 Turnstile 지문검사에 걸려 실패**
(이미지만 막아도 실패). 하지만 **cf_clearance 확보 후엔 차단해도 재챌린지 없음**.
- 동적 차단: _block_active + _blocking_now() 훅. ESM 은 cf_clearance 있을 때만 차단
  (콜드=허용→Turnstile 통과, 웜=이미지/미디어/폰트 차단→대역폭↓). ready_timeout 25s.
  best-effort Turnstile 체크박스 클릭(인터랙티브/나쁜IP 대비).
- 웜업: 기동 시 gmarket/auction/coupang 를 1회 풀어 쿠키 선점(백그라운드) → 실 작업은 웜.
- 유휴 브라우저 정리: _last_used + close_if_idle + run_browser_reaper(유휴 120s) — 메모리 회수.
  쿠키는 user_data_dir 에 남아 재기동해도 (같은 IP면) 웜 유지.
- 테스트: 유휴 정리 2종.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 20:20:58 +09:00
a80dac20ed [fix] postgres-init: init.sql 병합 충돌 해소 — last_accessed_at DEFAULT now()
stash 충돌 마커 제거. company.users·supplier.supplier_users 에 DEFAULT now() 추가(신규 DB)
+ 말미 보정 ALTER(기존 DB). ORM server_default 의존이라 없으면 유저 생성 시 NOT NULL 위반.

Little-Helped: Stupid Claude
2026-07-09 17:04:40 +09:00
1b9ca062f3 [feat] negodata: 협상카드 회사 스코프·공용 읽기전용 + 견적 목표가 산정내역 수정
협상카드
- 목록/조회를 등록자 개인 → 회사(company.users 조인) 단위로 확장: 같은 회사 유저 카드 + 공용(user_id NULL)
- 공용 카드는 기본 제공 자산 → 수정·삭제 불가(OWNER 포함), is_shared=True API 등록 거부
- 개인 카드 변경은 본인∪최고관리자만, 프론트 버튼 disable+툴팁
- 공개범위 셀렉터·엑셀 공개범위 컬럼·CardTable 전체 배지 제거(항상 개인 등록)
- test_card.py 5케이스 재작성

견적 목표가 산정내역
- get_setting_rates: 단일 컬럼 select 결과를 이중 인덱싱해 항상 예외 → 마진율이 늘 0으로 적용되던 버그 수정
- get_target_breakdown: 채택 후보 판정을 '현재 최소값' → '저장 목표가와 값 일치'로 변경(산정 후 상품가 변동에도 초록 체크 유지)
- 견적상세 협상카드 탭 세션ID 컬럼 제거

견적생성 모달
- 목표가 미리보기·마감기한 기본값(+1h)·미래 검증, combobox 팝오버 scrollIntoView, maskPrices 비율(%) 마스킹

Little-Helped: Stupid Claude
2026-07-09 17:04:40 +09:00
민헌
4b8dee8583 perf(lps): Step1 — 폴백 데드라인 + fail-fast로 검색 지연 상한
G마켓 챌린지가 안 풀린 채 25~70초를 먹던 문제에 상한을 씌운다. 폴백은 '있으면 좋은'
보강이라 데드라인으로 잘라도 안전(그 몰만 스킵).

- handler: 각 폴백 크롤에 fallback_deadline_sec(기본 15s) wait_for. 초과 시 스킵.
  _timed_search 예외를 BaseException 로(CancelledError 계측 포함).
- 폴백 어댑터 max_block_retries=0(챌린지 대기 2배 방지), ESM ready_timeout 22→12s, st11 20→15s.
- 테스트: 느린 폴백 데드라인 스킵 1종.

실측(동시성2·4상품): 상품당 p50 84.6s→34.2s(~2.5x), 벽시계 181s→65s(~2.8x),
처리량 1.3→3.7 상품/분, 1000건 $7.12→$5.06(~29%↓, 실패 챌린지 대역폭 절감).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 17:01:09 +09:00
민헌
ba67275cd8 docs(lps): 운영 가이드 — WORKER_CONCURRENCY 병렬 실행 + loadtest.py 안내
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 16:28:51 +09:00
민헌
ad26e6ce6f feat(lps): 다중 상품 병렬 검색(워커별 브라우저 세트) + 부하테스트 스크립트
WORKER_CONCURRENCY 를 늘려도 공유 브라우저 lock 때문에 직렬화되던 문제를 고쳐 진짜 병렬 검색.

- worker_main: _build_worker(i) 로 워커마다 자립 세트(브라우저 어댑터·AI·핸들러) 생성.
  프로필 분리(user_data_dir_w{i}, ProcessSingleton 충돌 회피) + 워커별 다른 프록시 포트
  (proxy.seed_offset 로 100포트를 균등 분할=다른 IP). naver/judge/keyword 도 워커별(공유상태 경합 제거).
  프리플라이트는 대표 프록시로 게이트 1회 확인.
- proxy.seed_offset(k): 워커 시작 포트 분산.
- loadtest.py: N개 상품 제출→폴링→처리량·지연(p50/p95)·AI/DECODO/총비용 집계.
  실측(동시성2, 4상품): 순차합 323s→벽시계 181s(~1.8x), 상품당 $0.0071, 1000건 ~$7.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 16:28:08 +09:00
민헌
bf19d4a8fd perf(lps): 폴백 크롤 병렬화 + 오픈마켓 대역폭 절감
한 검색의 소요·DECODO 비용을 크게 낮춘다(실측: 110s→52s, proxy 8MB→2MB, 총비용 70%↓).

- 폴백 병렬화: _enrich_with_fallback 의 순차 for-loop → gather 동시 크롤. 각 몰 어댑터는
  별 브라우저 인스턴스라 병렬 안전. 소요=합→최댓값. AI usage 계측 동시성 불변식 주석 명시
  (judge.last_usage 세팅~읽기 사이 await 없음 → asyncio 협조 스케줄링상 안전).
- 대역폭: blocked_resource_types 를 어댑터별 설정화. 오픈마켓은 이미지/미디어/폰트만 차단
  (CSS/JS 유지 — 챌린지/렌더 보호). 격리 실험으로 차단 유무가 ESM 챌린지 성패에 무관함 확인
  (실패 원인은 IP평판/headless). st11 라이브 0.11MB 검증.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 16:11:32 +09:00
0d62440ee1 [feat] negodata/front: 온보딩 페이지 추가 + 이용안내 모달 재작성 2026-07-09 16:08:54 +09:00
민헌
a80109cdcf chore(lps): FE 원가 타일 AI/DECODO 비용 분해 + config example cost_per_gb
- 임시 FE: 총 비용 타일에 AI·DECODO 분해, 크롤 트래픽에 프록시 경유 바이트(과금분)
- config.local.toml.example: DecodoConfig.cost_per_gb 안내

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 15:34:57 +09:00
민헌
c955a8f367 feat(lps): 프록시 전송오류 IP회전 + 시작 프리플라이트 + DECODO/컴포넌트별 비용
프록시(DECODO) 포트/IP 사망(407/ERR_TUNNEL/ERR_HTTP_RESPONSE_CODE_FAILURE)이
봇차단과 구분 없이 예외로 튕겨 같은 죽은 포트로 재시도만 하다 DEAD 되던 문제를 고친다.

- browser_base: is_proxy_error(순수함수) + search 루프에서 프록시 전송오류 시 IP 회전 재시도
  (max_proxy_retries=2). 봇감지 회전과 통합. uses_proxy 프로퍼티.
- 쿠팡 어댑터를 BrowserSearchAdapter 로 통합 — 중복 machinery 제거, 회전 로직 한 곳에서 공유
  (detect_block 순수함수는 유지, 테스트 호환).
- proxy.healthcheck(): 시작 프리플라이트 — 살아있는 포트 선점 + egress IP 로그(빠른 실패·가시성).
  worker_main 기동 시 호출.
- 비용: DecodoConfig.cost_per_gb 추가. metrics 에 proxy_bytes(네이버 직접 제외) + 컴포넌트별
  cost{ai_usd, proxy_usd, total_usd}. FE 원가 타일에 AI/DECODO 분해·프록시 바이트.
- 테스트: is_proxy_error 8종 + 비용 분해 1종.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 15:33:37 +09:00
민헌
9c478e80fd chore(lps): 임시 FE — 검색 원가 4타일(소요/AI비용/토큰/크롤 트래픽)
작업 카드에 result.metrics 렌더: 소요시간(+소스별), AI 비용$·호출수, AI 토큰(in/out),
크롤 트래픽(html bytes·fetch수·크롤 몰).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 15:17:42 +09:00
민헌
69f6641c27 feat(lps): 검색 1건 원가 계측 — AI 토큰·비용 + 시간 + 크롤 트래픽
검색이 소모하는 리소스/비용/시간을 잡 단위로 집계해 result.metrics 로 적재(API/FE 노출).
지금까진 타임스탬프만 있고 실제 비용 동인(AI 토큰·대역폭)은 버려지고 있었다.

- services/metrics.SearchMetrics: duration_ms + ai(calls/tokens/est_cost_usd, gpt-4o-mini 단가)
  + crawl(fetches/html_bytes/malls_crawled) + source_ms
- AI 클라이언트: resp.usage 를 last_usage 로 노출(그동안 폐기하던 토큰)
- 어댑터: last_bytes(처리 HTML 바이트) 노출 — naver/coupang/browser_base 공통
- handler: 각 fetch 타이밍+바이트, AI 호출 토큰을 metrics 로 누적 → 결과에 스냅샷
- FE: 작업 카드에 원가 4타일(소요/AI비용/토큰/크롤 트래픽)
- 테스트 2종. ⚠️ html_bytes 는 대역폭 근사(오픈마켓 리소스 미차단분 제외=하한), CDP 정확화는 백로그

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 15:14:35 +09:00
민헌
61b9e6ae82 docs(lps): 오픈마켓 폴백 크롤 반영 — 아키텍처·README
파이프라인 ⑤-1 폴백 단계(네이버 미커버 몰만 크롤), 구성요소 표에 오픈마켓 폴백,
폴더 구조에 esm/st11 추가.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 14:54:00 +09:00
민헌
7e5814b0e5 chore(lps): 임시 FE — 오픈마켓 크롤 소스 표기(색상·크롤 뱃지·몰별 색)
by_mall/top 에 등장하는 gmarket·auction·st11 을 소스 색상 태그(+‘크롤’ 뱃지)로 표기.
몰별 최저가 막대도 소스별 색. 폴백으로 크롤된 몰임을 UI에서 구분.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 14:47:31 +09:00
민헌
f9714c3f06 feat(lps): 오픈마켓 폴백 오케스트레이션 + 몰 dedup
사용자 규칙 '네이버로 그 몰 값 확보 성공→그 값, 실패(몰 없음)→실사이트 크롤' 구현.

- handler: found 라운드 뒤 _enrich_with_fallback — 네이버 매칭이 커버 못 한 타깃 몰만
  (canonical_mall 로 판정) 크롤 어댑터로 검색→같은상품 판정→병합. 크롤 실패는 격리(무시).
  fallback_crawl STAGE 기록.
- summarize_by_mall: 키를 (source,mall)→몰명(canonical)으로 — 네이버노출 vs 직접크롤 같은 몰
  중복을 최저가 1건으로 병합(dedup).
- card_parser: MALL_BY_SOURCE + canonical_mall() 추가.
- worker_main: gmarket/auction/st11 폴백 어댑터 등록(lazy 기동, 리소스차단 OFF).
- 테스트: 커버몰 크롤생략·미커버 크롤·실패격리·몰 dedup 4종.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 14:45:39 +09:00
민헌
e365e5c6e0 feat(lps): G마켓·옥션·11번가 크롤 어댑터 + 브라우저 어댑터 베이스
네이버 폴백용 오픈마켓 크롤러 3종. 4중 복제를 피해 BrowserSearchAdapter 베이스로
브라우저 수명·프록시 회전·차단감지+IP회전 재시도·감지기록을 공유하고, 사이트 차이는
훅(_search_url/_parse/ready_selector/block_markers)으로 분리.

- browser_base: patchright 공통 machinery(쿠팡 machinery 일반화)
- card_parser: 오픈마켓 공용 카드 파서(CardConfig 주입) + util(extract_price/clean_name/shipping)
- esm: G마켓·옥션(eBay 백엔드 공유, site 파라미터 1개로 커버, '잠시만' 챌린지 대기)
- st11: 11번가 PC(공식API 판매자계정 필요라 불가). 지연로딩 → scroll_steps, '.c-card-item' 토큰
- 학습 반영: 오픈마켓은 리소스차단 OFF(렌더/챌린지 깨짐), 스턱 챌린지='잠시만' 잔존 시 차단 간주→IP회전
- 파서 결정론 테스트 7종(실렌더 카드 3개씩 경량 fixture). 라이브 검증: 신선 IP로 3사 모두 파싱 성공

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 14:41:05 +09:00
민헌
3a2c6e43d4 feat(lps): price_history 몰별 최저가 스냅샷(by_mall JSONB)
몰이 열린 집합이라 와이드 컬럼(gmarket_*, st11_* …) 대신 JSONB 한 컬럼으로 담는다
— 몰 추가 시 마이그레이션 0. naver/coupang/final 3선 컬럼은 그래프 하위호환 유지.

- models: price_history.by_mall JSONB 추가
- crud: record/list 에 by_mall 왕복(json.dumps + CAST jsonb)
- handler: _price_snapshot 에 summarize_by_mall 적재, final 은 소스무관 전체 최저로
- protocol/service: history API 응답에 by_mall 노출
- migrations/2026-07-09: 기존 dev DB 동기화용 ALTER(추적 파일)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 14:15:58 +09:00
민헌
2eccf4de50 chore: 로컬 리서치 노트(Temp.md·new.md) gitignore 처리
크롤링 라이브러리/안티스크래핑 조사 노트로 로컬 참고용. 향후 크롤러 작업에
관련되므로 삭제하지 않고 추적만 제외해 git status 를 정리한다.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 14:03:08 +09:00
민헌
7806b35723 chore(lps): 임시 검색 콘솔 FE(lps-temp-fe) — API/그래프/몰별·배송 확인용
API 통신 확인용 단일 파일 프론트(외부 CDN 0, SVG 차트 자체 구현).
검색 요청 → 잡 폴링(파이프라인 퍼널·소스별 건수) → 최저가 히어로/상품표,
최저가 이력 3선 그래프(네이버·쿠팡·최종), 몰별 최저가 막대, 배송 뱃지,
헤더 헬스·큐 상태 실시간. CORS 매칭 위해 :5173 서빙(serve.sh, 대화형).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 13:59:56 +09:00
민헌
8460a5d833 fix(lps): 쿠팡 차단 감지 다종화 — Edge Deny·권한제한·짧은HTML 폴백
기존엔 Akamai JS 챌린지 마커만 감지해, Edge Access Denied(errors.edgesuite.net)와
'사용권한이 제한된' 권한제한 페이지가 blocked=False 로 오판됐다. 그 결과 IP 회전·
bot_detection 기록이 누락되고 같은 IP 로 재시도만 반복하다 DEAD 로 빠졌다.

- 마커 3계열로 확장(Akamai / Edge Deny / 권한제한)
- 짧은 HTML 폴백(_MIN_RESULT_HTML): 0건인데 <10KB 면 미지의 차단으로 간주
  (정상 '검색결과 없음'은 전체 chrome 포함이라 큼)
- 감지를 순수 함수 detect_block(html, product_count) 로 분리 — 단위 테스트 6종

검증: 신라면 '사용권한이 제한된'(3383B) 감지 → IP 회전 → 새 IP 59건 복구.
파서 배송 분류 테스트도 함께 추가(shared fixture).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 13:59:43 +09:00
민헌
61bc719e28 feat(lps): 배송비 유형 분류 — 쿠팡 배송신호 파싱 + 네이버 배송비제외 명시
NormalizedProduct 에 shipping_type(rocket|rocket_merchant|free|paid|None) 추가.
쿠팡 파서: 로켓 뱃지(img src)로 유형, '무료배송'/'배송비 X원' 텍스트로 금액 판별
(상품명 '무료배송' 오탐은 이름 제거 후 매칭). 네이버 lprice 는 배송비 제외
상품가라 배송 필드 None — 카탈로그 '배송비포함 최저가'와 다른 이유를 docs/api.md 에 명시.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 13:59:28 +09:00
민헌
313d882df4 feat(lps): 네이버 몰별 최저가 분해(by_mall) — 오픈마켓 가시화
네이버 오픈API 결과엔 G마켓·옥션·11번가 등이 이미 mallName 으로 들어오는데
최저가 1건만 쓰고 몰 정보를 버리고 있었다. summarize_by_mall() 로 매칭 후보를
(소스, 판매몰)별 최저가로 축약해 result.by_mall(가격 오름차순)에 노출한다.
추가 요청 0건으로 오픈마켓 몰별 최저가를 확보 — 별도 크롤러 불필요.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 13:59:15 +09:00
민헌
4863425ca5 refactor(lps): job_type 명칭 정리 — 출처 기반(레거시 negowiz 제거)
레거시 용어(negowiz)와 모호한 이름(new/single)을 출처 기반으로 변경.
우선순위 1~4는 유지, 기본값 single→manual.

- new_product(1) / manual(2, 기본) / partner(3) / batch(4)
- lps_service 우선순위 맵 + protocol 기본값/설명 + docs/api.md 표 + 테스트 갱신
- 전체 54/54

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 11:33:46 +09:00
민헌
8bf34ea346 docs(lps): 프로젝트 문서화 — README + docs/(아키텍처·DB·API·운영)
비개발자/기획자/개발자 누구나 이해하도록 일목요연하게 정리. 길이 분산 위해 분할.

- README.md: 3줄 요약 + 워크플로우 다이어그램 + 주요기능 + 빠른시작 + 문서 목차
- docs/architecture.md: 구성요소·처리 파이프라인·재시도/프록시/봇감지/이력 원리
- docs/database.md: 테이블 4종(job/price_history/search_negative/bot_detection) + 코드값
- docs/api.md: 엔드포인트 요청/응답 예시(curl/Postman), 상태·코드 요약
- docs/operations.md: 실행·로그·DB조회·테스트·문제해결·배포유의

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 11:27:39 +09:00
민헌
5906acc48a feat(lps): 최저가 이력(price_history) — 트리거 기반 시계열 그래프
같은 상품 반복 검색 시 최저가를 스냅샷으로 적재 → 네이버/쿠팡/최종 3개 선 그래프.
배치 아님(조회된 상품만, 실제 검색 시각에 기록) — 트래픽/리소스 절약.

- price_history 테이블: product_code·triggered_at(X축)·naver/coupang/final 최저가+상세·outcome
- crud/price_history: record() + list_by_product(시각 오름차순)
- handler: AI 매칭 후 소스별 min + 전체 min 스냅샷 기록(_price_snapshot).
  found/not_found 기록, 네거티브 캐시 히트·기술실패는 미기록
- API: GET /v1/lps/products/{product_code}/history → 그래프 데이터(시각 오름차순)
- tests: 스냅샷 계산/기록·조회/핸들러 기록규칙/API → 전체 54/54

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 11:13:28 +09:00
민헌
bca8032b79 feat(lps): 봇 감지 시 IP 회전+재시도 + 감지 이력 기록
쿠팡이 봇으로 감지(Akamai 차단 페이지)하면, 같은 IP로 재시도하던 것을
'브라우저 끄고 새 IP로 켜서 재시도'로 전환. 감지 패턴도 축적한다.

- DecodoProxy.rotate(): 시간창 무관 즉시 다음 포트(=새 IP). current_port 노출
- coupang/adapter: 0건+차단마커 감지 시 → 감지기록 → proxy.rotate()+강제재기동 →
  인라인 재시도(max_block_retries=1). IP당 요청수(ip_request_no) 추적, 재기동 시 리셋
- bot_detection 테이블 + crud: source/query/ip_request_no/proxy_port/elapsed_sec/marker/
  headless/html_len 기록 → 'IP당 몇 요청 만에 감지되나' 분석 가능
- worker_main: on_detect=BotDetectionLog.record 주입
- 로그: [coupang][BOT-DETECTED] ip_req#N port=... elapsed=...s marker=...
- tests: proxy rotate 즉시회전 + 감지기록 CRUD → 전체 47/47

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 11:04:07 +09:00
민헌
7911ba1745 refactor(lps): 환경설정 TOML 단일화 — .env 제거, 시크릿도 config.local.toml 로 통합
설정이 .env(시크릿)+toml(설정)로 갈려 있던 것을 config.local.toml 하나로 통합.
DB 비번이 이미 toml 에 있어 분리 기준이 임의적이었고, backend(하우스 패턴)도
toml 단일이라 일관성 확보. 환경별로 바뀌는 값(DB_HOST 등)만 env override 유지.

- config_models: NaverConfig(keys 로테이션)·OpenAIConfig·DecodoConfig 추가
- server_configs: 3개 로드, load_dotenv 제거(python-dotenv 의존성도 제거)
- proxy/naver/similarity/keyword/worker_main: os.environ → config 객체 참조
- config.local.toml.example: [NaverConfig]/[OpenAIConfig]/[DecodoConfig] 섹션
- .env/.env.example 삭제, README/주석 갱신 (배포는 toml 마운트 or env override)
- tests: DecodoConfig 기반으로 갱신 → 전체 44/44

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 10:06:00 +09:00
민헌
2481c79e3f feat(lps): DECODO 포트기반 sticky + 대역폭 최적화(리소스 차단)
대시보드 실측 반영 — Decodo residential 은 포트 기반 sticky(gate:10001..N,
고정 user/pass, 각 포트=sticky 세션). IP 회전 = 포트 순환.

- proxy.DecodoProxy: 시간창 기반 포트 선택(창 안 동일 IP, 창 지나면 다음 포트=새 IP).
  _UNSET sentinel 로 '미지정(env)' vs '명시적 빈값' 구분(테스트 결정성)
- coupang/adapter: 이미지/미디어/폰트/CSS 차단(block_resources) — per-GB 대역폭 대폭 절감,
  Akamai(JS)·상품파싱엔 무영향. 라이브 검증: 차단 ON 에도 Akamai 통과+파싱 정상
- .env.example: DECODO_HOST/USERNAME/PASSWORD/PORT_START/PORT_END/SESSION_MINUTES
- tests: 포트범위/시간창 안정성/활성조건 5건 → 전체 44/44

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 09:48:54 +09:00
민헌
4de85d43ed feat(lps): DECODO residential 프록시 — 쿠팡 sticky 세션 + 주기적 IP 회전
datacenter IP + Akamai 대응. 매 요청 IP 변경은 쿠키-IP 불일치로 재챌린지를
유발하므로, sticky 세션(일정 시간 같은 IP) + 주기적 회전 방식 사용.

- search/proxy.DecodoProxy: username 에 -session-<시간창id>-sessionduration-<분> 부착
  → 창 안에선 같은 IP, 창이 지나면 새 IP. 자격증명은 .env(DECODO_*), 4개 다 있어야 활성
- coupang/adapter: proxy 주입 + 세션창 경과 시 브라우저 재기동(IP 회전). 무프록시면 직접 연결
- worker_main: 쿠팡에만 DecodoProxy 경유(네이버는 공식 API라 미적용)
- .env.example: DECODO_HOST/PORT/USERNAME/PASSWORD/SESSION_MINUTES 슬롯
- tests: 활성조건·sticky username·server형식·세션창 회전 4건 → 전체 43/43

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 09:40:26 +09:00
민헌
d54dee4f24 feat(lps): 재시도/못찾음 완결 로직 — 재정제 루프 + not_found + 네거티브 캐시
'못 찾음'을 유한하게 종료. 두 재시도 축을 분리(기술=큐 attempts/백오프,
검색어=refine 라운드). not_found 는 정상 종료(DONE)지 dead-letter 아님.

- ai/keyword: LLM 검색어 생성(정밀/광역) — 원본 0매칭 시에만 지연 호출(비용 절약)
- handler: 한정 재정제 루프(원본→정밀→광역, max_rounds=3) + 명시적 outcome(found/not_found)
  · 0매칭+소스정상 → 다음 라운드, 0매칭+기술실패 → raise(큐 재시도)
  · 라운드 소진 → not_found + 네거티브 캐시 기록
- negative_cache: search_negative 테이블 + TTL(24h) upsert — 같은 상품 재요청 재검색 차단
- worker_main: OPENAI 있으면 judge+keyword_gen ON, neg_cache 상시
- tests: 재정제/not_found/캐시히트/기술실패/캐시CRUD 11건 → 전체 39/39
- 라이브: 없는상품 3라운드→not_found(30s), 재요청 캐시히트(0.00s)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 09:12:47 +09:00
민헌
b0a86b9720 feat(lps): AI 유사도 판정 — '같은 상품' 매칭으로 액세서리 오염 해결
파이프라인의 비워둔 슬롯(이상치 뒤·top-N 앞)에 OpenAI 유사도 판정을 결합.
"스탠리 텀블러" 검색 시 빨대마개·커버 등 호환 액세서리가 최저가로 올라오던
문제를 해결한다(기계적 최저가 → 같은 상품 최저가).

- ai/similarity: SimilarityJudge(OpenAI structured output). 액세서리/부품/다른규격 불일치 판별
- pipeline/core: apply_filters + rank_result 로 분리(AI 를 그 사이에 끼움), run_price_pipeline 동작 불변
- handler: judge 주입 시 ai_match STAGE 추가(필터 후 후보만 판정 → 토큰 절약), 미주입 시 생략
- worker_main: OPENAI_API_KEY 있으면 판정 ON
- requirements: openai / tests: fake judge 필터링 검증 → 전체 32/32
- 라이브: '스탠리 퀜처 887ml' → ai_match(60→15) → 실제 텀블러 top-6(액세서리 제거)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 08:44:57 +09:00
민헌
9557c1b1af feat(lps): 네이버 어댑터 + 다중 소스 병합 검색
네이버 쇼핑 오픈API 어댑터(크롤링 불필요) + 핸들러를 다중 소스로 확장.
쿠팡(브라우저)+네이버(API)를 동시 검색·병합해 교차 최저가를 뽑는다.

- search/naver/adapter: httpx + 오픈API + 키 로테이션(429/403 순환), .env 키 로드
- search/naver/transform: 순수 변환(태그/엔티티 정리, lprice). 가격비교(catalog) lprice 는
  '여러 판매자 중 최저가'라 최저가 솔루션엔 핵심 → 유지
- handler: asyncio.gather 동시 검색 + 소스별 실패 격리(일부 죽어도 결과) + 전체 실패 시 잡 실패
- worker_main: adapters={coupang, naver}
- tests: 네이버 변환 + 병합/실패격리/전체실패 4건 → 전체 31/31
- 라이브: 쿠팡30+네이버30 병합 top-N 최저가(소스 라벨 포함)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 08:38:15 +09:00
민헌
7491bbecd9 chore(lps): .env 에서 Azure OpenAI 제거 — OpenAI 단일 방식
Azure OpenAI 는 사용하지 않기로 하여 슬롯 제거. AI 는 OPENAI_API_KEY 로 통일.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 17:31:16 +09:00
민헌
c4b8692f3a chore(lps): 시크릿용 .env 도입 — API 키(NAVER/OpenAI/Azure) 주입 경로
키는 코드/커밋이 아닌 환경변수로 주입한다(감사: 시크릿 하드코딩 금지).
config.local.toml=비-시크릿 설정, .env=시크릿으로 분리.

- .env.example(템플릿, 커밋) + .env(로컬, 미추적)로 키 슬롯 제공
  · NAVER_CLIENT_ID/SECRET(+로테이션 _2..), OPENAI_API_KEY, AZURE_OPENAI_*
- server_configs 가 기동 시 lps/.env 를 load_dotenv 로 환경변수 주입
- requirements: python-dotenv 추가
- README: .env 셋업 단계 안내

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 17:14:34 +09:00
민헌
fb72ab625f feat(lps): 코어 파이프라인 — mall 필터·IQR 이상치·top-N 최저가
검색 결과를 실제 최저가로 정제하는 파이프라인을 핸들러에 결합한다.
검색→필터→이상치→정렬→top-N. AI 유사도 판정 슬롯은 비워둠(키 대기).

- pipeline/filters: keep_only_mall·filter_out_malls·filter_by_price_band(요청 현재가 기준 targeted 컷)
- pipeline/outliers: IQR 기반 이상치 제거(z-score 대신 — 분포 가정 없음, stdlib만)
- pipeline/core: STAGE in/out 관측 로깅 + top-N 최저가(레퍼런스 pipeline_log 계승)
- handler: 검색→run_price_pipeline 결합, 요청 price 를 밴드 기준으로 사용
- tests: 정렬/IQR(극단 저·고가 제거)/밴드/mall/빈입력/STAGE/실 fixture 7건 → 전체 27/27
- 라이브 확인: 스탠리 텀블러 40건→top-5 최저가, STAGE 카운트 노출

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 17:06:15 +09:00
민헌
abcb58ba05 feat(lps): 워커 루프 + LISTEN/NOTIFY — 큐 소비 파이프라인 가동
큐를 실제로 돌린다: 적재 → 워커 claim → 핸들러 실행 → 결과 저장 → DONE.
API(적재)와 워커(소비)를 분리 프로세스로(코드베이스 공유, 독립 스케일).

- worker/notify: 전용 asyncpg LISTEN 리스너. enqueue 에서 pg_notify → 유휴 워커 즉시 기상(폴링 제거)
- worker/runner: Worker(claim→처리, 처리중 heartbeat 로 lease 갱신, complete/fail) + run_reaper
- worker/handlers: job_type 별 핸들러(주입식). SEARCH=소스 어댑터 검색→정규화 결과
- worker_main: API 분리 워커 진입점(브라우저 무거워 기본 동시성 1)
- job_crud.enqueue: 삽입 시 pg_notify (중복 스킵 시엔 미발생)
- tests: drain→DONE·실패→재시도→dead·reaper 회수 후 재처리·NOTIFY 기상 4건 (전체 20/20)
- 라이브 E2E 확인: 적재→워커가 실제 쿠팡 검색(8건)→DONE

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 16:58:58 +09:00
민헌
bebb6a71e6 feat(lps): 검색 API 라우터 — 요청 적재(enqueue) + 상태/큐 통계 조회
커머스 검색요청 계약을 프레임워크(protocol+service+RemoveNoneResponse)로 재구성.
실제 검색은 워커가 큐에서 꺼내 수행하도록 API 는 적재까지만 담당(비동기 분리).

- POST /v1/lps/search: 상품 리스트 → 상품별 SEARCH 잡 적재, product_code 로 활성 중복 방지, job_type→우선순위 매핑
- GET /v1/lps/jobs/{job_id}: 잡 상태/시도/결과 조회
- GET /v1/lps/queue/stats: 상태별 카운트(모니터링)
- protocol/lps_service 추가, job_crud.get() 단건조회, enums LPS_JOB_NOT_FOUND
- tests: 적재/중복/상태/미존재/통계 5건 (ASGI 클라이언트 + 실 lps_db) → 전체 16/16

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 16:49:28 +09:00
민헌
da9bd7750c feat(lps): 작업 큐 엔진 — PostgreSQL 원자적 claim + lease + dead-letter
레퍼런스의 DB-큐 반면교사를 전부 뒤집는 큐 엔진을 구축한다.
 - 원자적 claim: FOR UPDATE SKIP LOCKED 서브쿼리 + 같은 UPDATE + RETURNING
   → 워커 다수여도 이중 할당 원천 불가(fetch/claim 미분리)
 - 모든 전이는 조건부 CAS(status/worker_id 가드) + RETURNING
 - 복구는 timeout 추측이 아닌 lease 만료 소유권 기반(reaper 회수)
 - 재시도 지수백오프 + max_attempts + dead-letter 를 큐에 내장(스크립트 난립 제거)
 - dedupe_key 부분 유니크로 활성 중복 차단, counts()로 관측(수동 psql 대체)

- enums: JobStatus(PENDING/RUNNING/DONE/DEAD)·JobType(SEARCH/OUTBOX)
- models: job 테이블(무FK·SMALLINT코드·TIMESTAMPTZ, claim/lease/dedupe 인덱스)
- crud/job_crud: JobQueue(enqueue/claim/complete/fail/renew_lease/reap/counts)
- tests: 동시 8워커 이중할당0·lease회수·재시도→dead·소유권가드 등 8건(실 lps_db)

부수: 포트 9400→9600 (negodata 9400 충돌 회피)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 16:37:32 +09:00
민헌
e27b73c018 feat(lps): 쿠팡 검색 어댑터 — Akamai 우회(Patchright) + 정규화 파서
소스 어댑터 패턴으로 크롤링을 시작한다. 쿠팡은 Akamai Bot Manager 의
JS 행동 챌린지라 curl_cffi 단독 불가 → Patchright(스텔스 Playwright) +
실제 Chrome(channel=chrome)으로 챌린지를 통과하고 selectolax 로 파싱한다.

- contract: SearchAdapter(ABC)·NormalizedProduct·AdapterHealth·AdapterError
- coupang: adapter(브라우저 재사용, 챌린지 1회)·parser(순수)·selectors(외부화, webpack 해시 prefix 매칭)
- 공용: rate_limiter(2~8s 랜덤)·proxy(무프록시 off 인터페이스)·util.parse_price
- 가격 파싱: 단위가격 '(1개당 44,400원)'의 앞 '1' 오인 방지 — '원' 앞 숫자 앵커링
- deps: curl_cffi·selectolax·patchright(nodriver 는 py3.14 버그로 대체)
- tests: 파서 회귀(소형 fixture) — 라이브 2회 검색 + 파서 3/3 통과

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 15:59:48 +09:00
민헌
11a7be7154 feat(lps): 인터넷 최저가 검색 솔루션 프레임워크 골격 추가
backend 와 동일한 프레임워크로 lps/ 폴더를 신설한다. 구체적인 도메인
로직(크롤링/오픈API/최저가 산정)은 미정이라 골격만 구성한다.

- FastAPI 부트스트랩(web_main/router) + lifespan·CORS·gzip·로그 미들웨어 + /healthz
- TOML 설정 로더(APP_ENV) + pydantic config + DB env override
- DB 세션 매니저(논리 DB × R/W, service→람다 위임) — 엔진 lazy 라 DB 없이도 부팅
- 공통 응답 규약(gmodel) + ErrorType/DBType/DBWRType
- router→services→crud 계층 컨벤션(services/crud 는 빈 폴더로 자리만)
- 포트 9400(backend 9300·agent 9500 회피), Dockerfile/run_local_server.sh/README
- tests/test_health.py 스모크(healthz, DB 없이 통과)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 15:18:27 +09:00
812 changed files with 59643 additions and 9028 deletions

8
.gitignore vendored
View File

@ -24,3 +24,11 @@ CLAUDE.md
# OS # OS
.DS_Store .DS_Store
# 로컬 리서치 노트(크롤링 라이브러리·안티스크래핑 조사) — 추적 안 함, 로컬 참고용
/Temp.md
/new.md
/mobile.mov
.gstack/
.playwright-mcp/

BIN
0729~30_테스트.xlsx Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

190
README.md
View File

@ -1,63 +1,198 @@
# O2O Negosium # O2O Negosium
동일 구조의 두 서비스(**negosium**, **negodata**)가 **하나의 PostgreSQL 인스턴스**를 공유한다. AI 협상 솔루션. 여러 백엔드·프론트·배치가 **하나의 PostgreSQL 인스턴스**를 공유하고,
전부 `docker compose` 하나로 뜬다. (인터넷 최저가 검색 LPS 만 별도 DB `lps_db` 사용.)
## 서비스 소개
구매기업(바이어)이 협력사(공급사)와 벌이는 **가격 협상을 AI 봇이 대신 수행**하는 B2B 협상 자동화 솔루션이다.
바이어가 상품·목표가·기간만 정해 견적을 열면, 각 협력사와의 1:1 협상은 강화학습 기반 에이전트가
**협상 카드로 밀당**하며 진행하고, 마감 시각에 최저 투찰가를 기준으로 자동 **낙찰/개찰**을 판정한다.
크게 세 축 + 부속으로 나뉜다.
| 축 | 구성요소 | 역할 |
|---|---|---|
| **바이어 측** | negodata (backend + front) | 어드민. 상품·협력사 관리, 견적 생성, 마감·낙찰 관리 |
| **공급사 측** | negosium (backend + frontend) | 협력사 포털. 초청받은 협상 챗에 참여해 가격 제시 |
| **협상 엔진** | agent | 실제 AI 협상 봇. 앵커링가·협상 카드로 자동 협상 (강화학습) |
| 부속 | lps · anchoring · landing | 인터넷 최저가(목표가 재료) · 앵커값 자동 조정 배치 · 솔루션 소개 랜딩 |
## 구성 ## 구성
``` ```
o2o-negosium/ o2o-negosium/
├── docker-compose.yml # 두 backend (DB 는 외부) ├── docker-compose.yml # 전체 서비스 (DB 는 compose 밖, config 로 외부 연결)
├── postgres-init/ # DB·테이블 셋업 SQL (대상 DB 에 1회 적용) ├── postgres-init/ # DB·스키마·시드 SQL (대상 DB 에 1회 적용)
├── backend/ # negosium 백엔드 (포트 9300)
├── negodata/backend/ # negodata 백엔드 (포트 9400) ├── backend/ # negosium 백엔드 — 공급사/협상 API (:9300)
├── agent/ front/ # (예정) ├── frontend/ # negosium 공급사 프론트 (:3300, 프로덕션 빌드 정적 서빙)
└── negodata/front/ # (예정) ├── agent/ # 협상 에이전트 — RL(learning 스키마) (:9500)
├── negodata/backend/ # negodata 백엔드 — 바이어/어드민 API (:9400)
├── negodata/front/ # negodata 어드민 프론트 (Vite, :3000)
├── landing/ # 솔루션 랜딩페이지 (react-router SSG, :3100)
├── lps/ # 인터넷 최저가 검색: lps-api(:9600) + lps-worker(크롤)
├── lps-admin/ # LPS 관리자 UI (nginx → lps-api 프록시, :3400)
└── schedules/anchoring/ # 앵커링 값 자동 조정 배치 (포트 없음, 상주 스케줄러 + Redis)
``` ```
두 백엔드는 같은 코드 골격(MVC · 람다 DB · Depends 주입 · JWT 로그인)을 쓴다. negosium·negodata·agent 백엔드는 같은 코드 골격(MVC · 람다 DB · Depends 주입 · JWT 로그인)을 쓴다.
아키텍처/패턴 상세는 각 서버 README 참고: [backend](backend/README.md) · [negodata/backend](negodata/backend/README.md) 아키텍처/패턴 상세는 각 서브 README 참고:
- 백엔드: [backend](backend/README.md) · [negodata/backend](negodata/backend/README.md) · [agent](agent/README.md) · [lps](lps/README.md)
- 프론트: [frontend](frontend/README.md) · [negodata/front](negodata/front/README.md)
- 배치: [schedules/anchoring](schedules/anchoring/README.md) · 관리자 UI: [lps-admin](lps-admin/README.md)
### 서비스 / 포트
| 서비스 | 주소 | 역할 | DB |
|---|---|---|---|
| negosium-backend | http://localhost:9300/docs | 공급사·협상 API | negosium_db |
| negosium-front | http://localhost:3300 | 공급사 프론트 | — |
| agent | http://localhost:9500/docs | 협상 에이전트(RL) | negosium_db (learning) |
| negodata-backend | http://localhost:9400/docs | 바이어·어드민 API | negosium_db |
| negodata-front | http://localhost:3000 | 어드민 프론트 | — |
| landing | http://localhost:3100 | 솔루션 랜딩 | — |
| lps-api | http://localhost:9600/docs | 최저가 검색 접수/조회 | lps_db |
| lps-worker | 포트 없음 | 크롤 워커(헤드풀 Chromium) | lps_db |
| lps-admin | http://localhost:3400 | LPS 관리자 UI | — |
| anchoring | 포트 없음 | 앵커링 조정 배치(격주 토 00:00 KST) | negosium_db (anchoring) |
| anchoring-redis | 127.0.0.1:6380 | anchoring 조회 캐시 | — |
| autoheal | — | unhealthy 컨테이너 자동 재시작 | — |
### DB 는 compose 밖 (config 로 연결) ### DB 는 compose 밖 (config 로 연결)
DB 는 docker-compose 에서 관리하지 않는다. 각 backend 는 `config.<APP_ENV>.toml` 의 접속 정보대로 DB 는 docker-compose 에서 관리하지 않는다. 각 backend 는 `config.<APP_ENV>.toml` 의 접속 정보대로
**외부 PostgreSQL**(호스트 로컬 postgres, 또는 따로 떠 있는 docker postgres)에 연결한다. **외부 PostgreSQL**(호스트 로컬 postgres, 또는 따로 떠 있는 docker postgres)에 연결한다.
한 PostgreSQL 안에 서비스별 database 를 둔다.
한 PostgreSQL 인스턴스 안에 **단일 `negosium_db`** 를 두고 도메인별 **schema** 로 묶는다.
LPS 만 별도 database(`lps_db`) 를 쓴다.
``` ```
PostgreSQL (외부, 5432) PostgreSQL (외부, 5432)
├── negosium_db ← negosium-backend ├── negosium_db ← negosium-backend · negodata-backend · agent · anchoring 공유
└── negodata_db ← negodata-backend │ ├── company / supplier / partner : 회사·유저·협력사·상품
│ ├── card / quotation / negotiation: 협상 카드·견적·협상 세션
│ ├── learning : RL 자산 (agent 소유)
│ └── anchoring : 앵커링 조정 (schedules/anchoring 소유)
└── lps_db ← lps-api · lps-worker
``` ```
- 컨테이너(docker env)에서 호스트 DB 접근: `host.docker.internal:5432` (`config.docker.toml`) - 컨테이너(docker env)에서 호스트 DB 접근: `host.docker.internal:5432` (compose 가 `DB_HOST` 로 override)
- 로컬 실행/테스트(local·test env): `127.0.0.1:5432` (`config.local/test.toml`) - 로컬 실행/테스트(local·test env): `127.0.0.1:5432` (`config.local/test.toml`)
- 계정/database 명은 config 에 맞춘다 (기본 `postgres` / `password`). - 계정/database 명은 config 에 맞춘다 (기본 `postgres` / `password`).
| 서비스 | 서버 | docs | database | ## 핵심 플로우
|---|---|---|---|
| negosium-backend | http://localhost:9300 | /docs | negosium_db | ### 1. 견적 라이프사이클 (전체 개요)
| negodata-backend | http://localhost:9400 | /docs | negodata_db |
바이어가 견적을 열고 → 협력사가 협상에 참여 → 마감 시각에 판정되는 큰 흐름.
견적 유형은 두 축(재/신규 × 협상 1:1 / 견적 1:N)으로 4종.
```mermaid
flowchart TD
A["바이어: 상품·협력사·기간 선택<br/>견적 유형 4종 + 낙찰 기준(mid/over_action) 설정"] --> B["목표가·앵커링가 산정<br/>(MD제시가 → 인터넷최저가/매입가/판매가)"]
B --> C["협력사 초청 (이메일)"]
C --> D{"견적 유형"}
D -->|"협상 1:1 (재협상·신규협상)"| E["AI 봇 밀당 협상<br/>(협상 카드 사용)"]
D -->|"견적 1:N (재견적·신규견적)"| F["정형 흐름<br/>(배송형태·추가할인 확인)"]
E --> G["세션별 투찰가 확정<br/>(협상완료) 또는 실패"]
F --> G
G --> H{"마감 트리거<br/>①마감시각 ②전세션종결 ③수동"}
H --> I["마감 판정<br/>(최저 투찰가 기준)"]
I --> J["낙찰 (승자 1)"]
I --> K["개찰 (낙찰자 미정)"]
```
### 2. 1:1 협상 봇 판정 (agent)
협력사가 가격을 제시할 때마다 봇이 **앵커링가** 기준으로 판정한다.
재제안은 카드를 한 장씩 쓰며 **최대 3번**, 카드 소진·3번 초과에도 앵커 밑으로 못 내리면 실패(투찰 없음).
```mermaid
flowchart TD
P["협력사 가격 제시"] --> Q{"제시가 vs 앵커링가"}
Q -->|"≤ 앵커링가"| R["협상완료 — 투찰 확정"]
Q -->|"앵커 ~ 앵커×1.02"| S["와일드카드: 1% 인하 요청<br/>(세션당 1회)"]
Q -->|"앵커×1.02 초과"| T["협상 카드로 재제안"]
S --> U{"재제안 횟수 ≤ 3?<br/>카드 남음?"}
T --> U
U -->|"예"| P
U -->|"아니오 (소진·3번 초과)"| V["협상 실패 — 낙찰 후보 아님"]
```
### 3. 마감 판정
마감 시 **협상완료 세션의 최저 투찰가**를 본다. 공통 전제: 완료 세션이 없거나(전원 미응찰·협상거부)
동가 최저가 2곳 이상이면 유형과 무관하게 **개찰**. 그 외 단독 최저가일 때만 낙찰 후보가 되며,
이후 판정이 유형별로 갈린다.
#### 3-1. 견적 1:N — 단독 최저면 무조건 낙찰
가격 구간을 보지 않는다. 생성 시 `mid/over_action`이 낙찰(AWARD)로 강제되기 때문.
```mermaid
flowchart TD
M1["마감: 협상완료 세션 최저 투찰가"] --> N1{"완료 세션 있나?"}
N1 -->|"없음 (전원 미응찰·협상거부)"| O1["개찰"]
N1 -->|"동가 최저 2곳+"| O1
N1 -->|"단독 최저"| X1["낙찰 (가격 구간 무관, 무조건)"]
```
#### 3-2. 협상 1:1 — 가격 구간별, 생성 때 정한 값 적용
앵커링가 이하는 무조건 낙찰. 그 위 구간은 **견적 생성 때 미리 정해둔 값**(`mid_action`/`over_action`,
1=낙찰·2=개찰)을 마감 시 그대로 적용한다. 두 필드는 적용 구간만 다를 뿐 동작은 동일.
```mermaid
flowchart TD
M2["마감: 협상완료 세션 최저 투찰가"] --> N2{"완료 세션 있나?"}
N2 -->|"없음 (전원 미응찰·협상거부)"| O2["개찰"]
N2 -->|"동가 최저 2곳+"| O2
N2 -->|"단독 최저"| W2{"투찰가 위치"}
W2 -->|"≤ 앵커링가"| X2["낙찰"]
W2 -->|"앵커 ~ 목표가"| Y2["생성 시 정한 mid_action 적용<br/>(1=낙찰 / 2=개찰)"]
W2 -->|"목표가 초과"| Z2["생성 시 정한 over_action 적용<br/>(1=낙찰 / 2=개찰)"]
```
> 개찰 = 낙찰자 미정 마감(결렬 아님). 개찰 후 수동 처리로 **직접 낙찰 확정**(`/v1/quotation/award`)
> 또는 **재견적 재생성**(`/v1/quotation/regenerate`)이 있다.
> 비즈니스 로직 정본은 [negodata/docs/business-logic.md](negodata/docs/business-logic.md).
## 빠른 시작 ## 빠른 시작
```bash ```bash
# 1) DB 준비 (최초 1회) — 사용할 PostgreSQL 에 스키마 + 시드 적용 # 1) DB 준비 (최초 1회) — 사용할 PostgreSQL 에 스키마 + 시드 적용
psql -h 127.0.0.1 -p 5432 -U postgres -f postgres-init/00-init.sql # 스키마 전체 (negosium_db + 도메인·learning·anchoring schema) # 스키마 DDL (구 01~05 통합, 전부 IF NOT EXISTS 라 재실행 안전)
psql -h 127.0.0.1 -p 5432 -U postgres -f postgres-init/temp-data.sql # 임시 데이터 시드 (admin / admin1234) psql -h 127.0.0.1 -p 5432 -U postgres -d negosium_db -f postgres-init/init-data/init.sql
# 로컬/개발 시드 (admin / admin1234, 회사·유저·협상 카드)
psql -h 127.0.0.1 -p 5432 -U postgres -d negosium_db -f postgres-init/init-data/init-data.sql
# 2) 백엔드 기동 # (DBeaver 로 처음부터 새로 깔 때는 postgres-init/dbeaver/ 의 0~5 순서 스크립트를 쓴다:
docker compose up -d # 두 backend (DB 는 config 대로 외부 연결) # 0 drop&create → 1 스키마 → 2 시드 → 3 lps_db → 4 카드 리셋 → 5 o2o OWNER 유저)
docker compose logs -f
# 2) 전체 기동
docker compose up -d
docker compose logs -f # 컨테이너별 로그는 ./logs.sh 메뉴로도 확인
docker compose down docker compose down
``` ```
> 스키마 변경 보정은 `postgres-init/alters/` 의 날짜별 SQL 을 대상 DB 에 수동 적용한다
> (postgres-init 은 DB 최초 생성 때만 자동 실행되므로, 기존 DB 엔 alter 를 직접 돌려야 새 컬럼이 반영된다).
## 테스트 ## 테스트
```bash ```bash
# config.test.toml 의 PostgreSQL(기본 127.0.0.1:5432) 이 떠 있어야 한다 # config.test.toml 의 PostgreSQL(기본 127.0.0.1:5432) 이 떠 있어야 한다
cd backend # 또는 negodata/backend cd backend # 또는 negodata/backend, agent, lps ...
pip install pytest pytest-asyncio httpx pip install pytest pytest-asyncio httpx
python -m pytest python -m pytest
``` ```
- httpx `ASGITransport` 로 네트워크 없이 앱을 직접 호출하는 e2e (각 5개). - httpx `ASGITransport` 로 네트워크 없이 앱을 직접 호출하는 e2e.
- `DB_SESSION_MNG` 싱글톤의 커넥션 풀이 첫 이벤트 루프에 묶이므로, 모든 테스트가 단일 session 루프를 공유한다(`pytest.ini`). - `DB_SESSION_MNG` 싱글톤의 커넥션 풀이 첫 이벤트 루프에 묶이므로, 모든 테스트가 단일 session 루프를 공유한다(`pytest.ini`).
- ⚠️ 테스트는 `APP_ENV=test` 로 격리한다(테스트 DB). dev DB(`negosium_db`)에 대고 돌리면 데이터가 날아간다.
## 성능 / 벤치마크 ## 성능 / 벤치마크
@ -99,4 +234,9 @@ python -m locust -f loadtest/locustfile.py --host http://localhost:9300 --headle
``` ```
## 기술 스택 ## 기술 스택
FastAPI · SQLAlchemy(async) · asyncpg · PostgreSQL 16 · python-jose(JWT) · bcrypt · uvicorn · Docker Compose - **백엔드**: FastAPI · SQLAlchemy(async) · asyncpg · PostgreSQL 16 · python-jose(JWT) · bcrypt · uvicorn
- **프론트**: React · react-router v7 · Vite · TanStack Query (negosium-front 는 프로덕션 빌드 정적 서빙)
- **에이전트**: 강화학습(Q-learning, learning 스키마) · OpenAI
- **LPS**: 헤드풀 Chromium + Patchright(스텔스 Playwright 포크, 크롤) · autoheal
- **배치**: Redis(anchoring 캐시) · 상주 스케줄러
- **공통**: Docker Compose

View File

@ -1,6 +1,6 @@
# Negosium Agent # Negosium Agent
범용 멀티테넌트 협상 솔루션 PoC. 여러 회사(ktcommerce·imarketkorea 등)가 **각자 데이터로 분기 학습**하는 범용 멀티테넌트 협상 솔루션 PoC. 여러 회사(imarketkorea 등)가 **각자 데이터로 분기 학습**하는
협상 카드 선택 에이전트. Q-Learning(UCB) 기반 `Chat_server`(단일 테넌트)를 참고해 신규 구축한다. 협상 카드 선택 에이전트. Q-Learning(UCB) 기반 `Chat_server`(단일 테넌트)를 참고해 신규 구축한다.
PoC 목표 두 가지: PoC 목표 두 가지:
@ -99,10 +99,10 @@ APP_ENV=test python -m pytest # 테스트 (config.local.toml 사
**1) 의사결정 루프 데모** — 테넌트별 config 주입·상태분류·보상·DB 격리를 눈으로 확인: **1) 의사결정 루프 데모** — 테넌트별 config 주입·상태분류·보상·DB 격리를 눈으로 확인:
```bash ```bash
APP_ENV=local python -m tools.console_demo --tenant ktcommerce # 기본 3턴 시나리오 APP_ENV=local python -m tools.console_demo --tenant imarketkorea # 기본 3턴 시나리오
APP_ENV=local python -m tools.console_demo --tenant imarketkorea # 다른 테넌트(다른 카드셋·임계값) APP_ENV=local python -m tools.console_demo --tenant imarketkorea # 다른 테넌트(다른 카드셋·임계값)
APP_ENV=local python -m tools.console_demo --tenant ktcommerce --interactive # 직접 입력 APP_ENV=local python -m tools.console_demo --tenant imarketkorea --interactive # 직접 입력
APP_ENV=local python -m tools.console_demo --tenant ktcommerce --no-db # DB 없이 APP_ENV=local python -m tools.console_demo --tenant imarketkorea --no-db # DB 없이
``` ```
> ⚠️ 카드선택은 임시 placeholder 정책(실제 UCB Q-Table 은 H1/P5). 학습은 아직 일어나지 않는다. > ⚠️ 카드선택은 임시 placeholder 정책(실제 UCB Q-Table 은 H1/P5). 학습은 아직 일어나지 않는다.
@ -113,13 +113,13 @@ curl localhost:9500/healthz # 200
curl localhost:9500/v1/health # {"status":"ok",...} curl localhost:9500/v1/health # {"status":"ok",...}
curl localhost:9500/v1/foo # 400 TENANT_HEADER_MISSING (헤더 없음) curl localhost:9500/v1/foo # 400 TENANT_HEADER_MISSING (헤더 없음)
curl localhost:9500/v1/foo -H 'X-Tenant-ID: nonexistent' # 404 TENANT_NOT_REGISTERED curl localhost:9500/v1/foo -H 'X-Tenant-ID: nonexistent' # 404 TENANT_NOT_REGISTERED
curl localhost:9500/v1/foo -H 'X-Tenant-ID: ktcommerce' # 통과(라우트 미존재라 404 Not Found) curl localhost:9500/v1/foo -H 'X-Tenant-ID: imarketkorea' # 통과(라우트 미존재라 404 Not Found)
``` ```
**2-1) 협상 한 라운드 (HTTP 프리뷰)** — `POST /v1/negotiation/step` (Swagger: http://localhost:9500/docs): **2-1) 협상 한 라운드 (HTTP 프리뷰)** — `POST /v1/negotiation/step` (Swagger: http://localhost:9500/docs):
```bash ```bash
curl -s -X POST localhost:9500/v1/negotiation/step \ curl -s -X POST localhost:9500/v1/negotiation/step \
-H 'X-Tenant-ID: ktcommerce' -H 'Content-Type: application/json' \ -H 'X-Tenant-ID: imarketkorea' -H 'Content-Type: application/json' \
-d '{"revenue_amount":20000000,"distribution_code":"A","partner_count":1, -d '{"revenue_amount":20000000,"distribution_code":"A","partner_count":1,
"acceptance_ratio":0.11,"input_price":990,"anchor_price":800,"target_price":1000, "acceptance_ratio":0.11,"input_price":990,"anchor_price":800,"target_price":1000,
"round_number":3,"outcome":"success"}' "round_number":3,"outcome":"success"}'
@ -156,7 +156,7 @@ APP_ENV=local python -m tools.show_logs # learning.experience_logs 를 co
**가격협상 턴 UCB 카드선택·학습 + 종료보상 역전파**. 브라우저 채팅 UI. (`tests/test_p7_chat.py` 5/5) **가격협상 턴 UCB 카드선택·학습 + 종료보상 역전파**. 브라우저 채팅 UI. (`tests/test_p7_chat.py` 5/5)
- ✅ **H5 학습검증 하네스 (PoC 본체)**: 카드별 효과가 다른 시뮬 구매자(`eval_harness/`) → 정책 비교. - ✅ **H5 학습검증 하네스 (PoC 본체)**: 카드별 효과가 다른 시뮬 구매자(`eval_harness/`) → 정책 비교.
**학습형(qtable_ucb)이 random/static 대비 평균보상 우위(95%CI 분리)·좋은카드 적중 0.9 vs 0.32** **학습형(qtable_ucb)이 random/static 대비 평균보상 우위(95%CI 분리)·좋은카드 적중 0.9 vs 0.32**
"학습하면 성과가 오른다" 정량 입증. `python -m eval_harness.runner --config configs/exp_default.yaml --tenant ktcommerce`. (`tests/test_h5_*` 5/5) "학습하면 성과가 오른다" 정량 입증. `python -m eval_harness.runner --config configs/exp_default.yaml --tenant imarketkorea`. (`tests/test_h5_*` 5/5)
- ✅ **P7 14개 API**: chat / q-table(versions·switch·current) / experience-logs / reset-learning · - ✅ **P7 14개 API**: chat / q-table(versions·switch·current) / experience-logs / reset-learning ·
reset-all(타테넌트 무영향) / invalidate-session / **train**(오프라인 Q-learning) / verification-report / reset-all(타테넌트 무영향) / invalidate-session / **train**(오프라인 Q-learning) / verification-report /
card-update · card-search. 전부 X-Tenant-ID 격리. (`tests/test_p7_apis.py` 5/5) card-update · card-search. 전부 X-Tenant-ID 격리. (`tests/test_p7_apis.py` 5/5)
@ -177,7 +177,7 @@ APP_ENV=local python -m tools.show_logs # learning.experience_logs 를 co
### PoC 본체 결과 (H5, 위 경제모델 기준 / target=10000·anchor=8000 시나리오) ### PoC 본체 결과 (H5, 위 경제모델 기준 / target=10000·anchor=8000 시나리오)
``` ```
python -m eval_harness.runner --config configs/exp_default.yaml --tenant ktcommerce python -m eval_harness.runner --config configs/exp_default.yaml --tenant imarketkorea
policy success settled/tgt turns mean_rwd ±95%CI good_hit policy success settled/tgt turns mean_rwd ±95%CI good_hit
random 0.988 0.904 2.02 1.1176 0.0206 0.350 random 0.988 0.904 2.02 1.1176 0.0206 0.350
static 1.000 0.923 2.56 0.9995 0.0020 0.000 static 1.000 0.923 2.56 0.9995 0.0020 0.000

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,14 +0,0 @@
{
"rows": 25,
"episodes": 4,
"skipped": {
"종료행/카드턴 없음(미완결 세션)": 8
},
"min_episodes": 1,
"deployed": false,
"ope_candidate": 0.9029104414200676,
"ope_candidate_ess": 1.0,
"ope_current": 0.9029104414200676,
"ope_current_ess": 1.0,
"result": "gate_failed"
}

View File

@ -40,3 +40,20 @@ def _apply_db_env_override(cfg: MainDBConfig):
_apply_db_env_override(main_db_config) _apply_db_env_override(main_db_config)
# LLM 키/설정 env override (DB 와 동일 패턴). 로컬은 config.local.toml [OpenAIConfig] 에 기재,
# Docker/CI/운영은 toml 없이 env 로 주입한다(docker-compose 가 OPENAI_API_KEY passthrough).
# env 미설정 시 no-op → toml 값 그대로.
def _apply_llm_env_override(cfg: OpenAIConfig):
if os.environ.get("OPENAI_API_KEY"):
cfg.api_key = os.environ["OPENAI_API_KEY"]
if os.environ.get("OPENAI_MODEL"):
cfg.model = os.environ["OPENAI_MODEL"]
if os.environ.get("OPENAI_BASE_URL"):
cfg.base_url = os.environ["OPENAI_BASE_URL"]
if os.environ.get("OPENAI_PROVIDER"):
cfg.provider = os.environ["OPENAI_PROVIDER"]
_apply_llm_env_override(openai_config)

View File

@ -4,10 +4,23 @@ import os
os.environ.setdefault("APP_ENV", "local") os.environ.setdefault("APP_ENV", "local")
import pytest
import pytest_asyncio import pytest_asyncio
from httpx import ASGITransport, AsyncClient from httpx import ASGITransport, AsyncClient
@pytest.fixture(autouse=True)
def _no_real_llm(monkeypatch):
"""실 LLM 호출 차단 — imarketkorea 가 llm.enabled=true 라 로컬에 실키가 있으면
플로우가 자연화/NLU(과금·비결정 응답) 시도한다. LLM 검증 테스트는
available 테스트 안에서 직접 덮어써 가드를 우회한다."""
from negotiation.chat.service.input_interpreter import InputInterpreter
from negotiation.chat.service.script_naturalizer import ScriptNaturalizer
monkeypatch.setattr(ScriptNaturalizer, "available", staticmethod(lambda: False))
monkeypatch.setattr(InputInterpreter, "available", staticmethod(lambda: False))
@pytest_asyncio.fixture(scope="session", autouse=True) @pytest_asyncio.fixture(scope="session", autouse=True)
async def _dispose_app_engines(): async def _dispose_app_engines():
"""테스트 세션 종료 시 앱 싱글톤 엔진 정리 ('Event loop is closed' 경고 제거).""" """테스트 세션 종료 시 앱 싱글톤 엔진 정리 ('Event loop is closed' 경고 제거)."""

View File

@ -1,203 +0,0 @@
# 완전 자율 협상 에이전트 — 처음 대비 변경 정리
> 기준: 협상카드 + 룰 엔진 시절(처음) → 완전 자율 에이전트 v3.2 + LLM 멘트 (2026-07-10 현재)
> 롤백: `docker-compose.yml``AUTONOMY_MODE=0` 하나로 룰 엔진 즉시 복귀 (재빌드 불필요)
---
## 1. 한눈에 보기 — 무엇이 바뀌었나
| 영역 | 처음 (룰 + 카드) | 지금 (자율 에이전트) |
|---|---|---|
| **협상 판정** | 하드코딩 룰 (앵커 이하 타결 / 와일드카드 존 / 3라운드 결렬) | RL 정책이 매 턴 행동을 직접 선택 |
| **발화 선택** | DB 협상카드(NGC-001~011)를 UCB/Q-table 로 선택 | 카드 없음 — 행동 30개 중 신경망이 선택 |
| **역제안 금액** | 카드에 박힌 고정값 | 앵커~목표가 6단 사다리에서 정책이 선택 |
| **와일드카드** | 사람이 등록한 카드(WC-01~05) 발동 | 최종제안·역제시 타이밍을 정책+봉투가 자율 수행 |
| **멘트** | 고정 템플릿 | Gemini LLM 생성 + 할루시네이션 가드 (실패 시 템플릿 폴백) |
| **입력 상태** | 가격 스냅샷 9차원 | 21차원 (마감·협력사 이력·인터넷최저가·에피소드 기억 추가) |
| **학습** | Q-table 온라인 갱신 | 시뮬레이터 DQN 학습 → 프로브 게이트 → npz 번들 배포 |
---
## 2. 의사결정 — 행동 공간 30개
```
ACCEPT 수락 (협상완료, 제시가 타결)
WALK 결렬 의사 → 최종제안 1회 보장 후 종료
COUNTER 역제안: 금액 위치 6단 {-5%, 0, 25, 50, 75, 100% of (목표가-앵커가)} × 화법 4종
PRESS 압박(설득): 화법 4종
```
행동의 실체는 `Action(kind, counter_q, strategy)`**(무엇을, 얼마에, 어떤 말투로)** 좌표 3개짜리 데이터다
(`policies/autonomy_actions.py`, DB 아님). 에이전트는 매 턴 30개 중 조합 1개를 고르고,
원화 환산(앵커 + q×스팬)과 문장(LLM)은 선택 이후의 실행 단계.
역제안 사다리 6단 (실스케일 앵커 418,966/목표 423,198 기준):
q=0.05→418,754 / 0→418,966(앵커) / 0.25→420,024 / 0.5→421,082 / 0.75→422,140 / 1.0→423,198(목표가).
비율(q)이라 견적 스케일과 무관하게 같은 행동 공간이 재사용된다.
화법 4종은 기존 카드 전략 분류를 그대로 승계: **경쟁 압박 / 수용 공감 / 기준 고수 / 협력 파트너**.
톤 선택도 학습 결과 — 라이브에서 초반 경쟁(1)→중반 수용(2)→교착 협력(4)으로 국면별 전환 관측.
설계 출처: 화법 4종·금액 범위(앵커~목표가)는 제품 승계, 수락·결렬 포함은 완전 자율 정의의 필연,
**격자 6단만 설계 재량**(`COUNTER_GRID` 수정+재학습으로 변경 가능). 알려진 한계: 부를 수 있는
금액이 격자 6지점뿐 — 연속 금액 미세조정은 불가(필요 시 격자 확장이 현실적).
- 모델: action-as-feature DQN (ScoreNet MLP — 상태 21 + 행동특징 9 → 점수 1개)
- 서빙: **numpy 전용** (`autonomy_serving.npz`) — 컨테이너에 PyTorch 불필요
- 현재 서빙본: **v3.5** (백업 `autonomy_v35.npz` / 반려본 v3.4 / 이전 v3.2)
## 3. 입력 상태 — 9차원 → 21차원
"완전한 에이전트에는 다 들어가야 한다" 요구로 확장:
| 그룹 | 차원 | 내용 | 출처 |
|---|---|---|---|
| 기본 | 9 | 매출액·유통코드·협력사수·수락률·제시가·앵커가·목표가·라운드 등 | 기존 스냅샷 |
| 테넌트 | 5 | 보상 설정 특징 | reward config |
| **에피소드 기억** | 2 | 직전 역제안 유무·위치 | ctx `autonomy_last` |
| **마감** | 1 | 마감 잔여율 | quotations start/end_time |
| **협력사 이력** | 3 | 과거 협상 횟수·성공률·평균 타결비율 | experience_logs ⨝ sessions |
| **시장가** | 1 | 인터넷 최저가 갭 | items.internet_lowest_price |
- 소스가 없으면 중립값(0.5/0) — 학습 시뮬의 '미상' 표현과 동일
- 상대 **발화 내용 파싱은 보류** (사용자 결정 — 프론트 입력 UI 변경 필요)
## 4. 행동 봉투 — 실전 테스트에서 잡은 결함의 구조적 방지
룰과 다름: **룰은 결과를 정하고, 봉투는 행동만 금지**한다. 나머지(타이밍·속도·금액)는 전부 정책 학습.
| # | 봉투 | 막는 결함 (실제 발생 사례) | 성격 |
|---|---|---|---|
| ① | 목표가 초과 제시가는 **수락 불가** | v3.1 이 보상 구멍을 착취해 목표가+14% 매입 | 안전 (영구) |
| ② | 직전 역제안보다 **낮은 금액 재제시 금지** (단조 양보) | 423,198 → 420,024 제안 철회 사건 | 안전 (영구) |
| ③ | 역제시는 **설득 ≥2회 후 해금** (`AUTONOMY_MIN_PRESS`) | 첫 턴부터 역제시 — 옛 의미론(일반카드=설득, 와일드카드만 역제시) 복원 | 예절 (해제 후보) |
| ④ | 목표가 0.5% 이내 **마무리 국면에선 압박 금지** | 802원 차이에 "재검토 부탁" 반복하던 푼돈 흥정 | 예절 (해제 후보) |
| ⑤ | **첫 역제안은 앵커가 이하만** (q ≤ 0) | 사다리 꼭대기 근처(422,140)에서 개시해 올라갈 계단이 없던 문제 | 예절 (해제 후보) |
| ⑥ | 같은 금액 반복·결렬 의사 → **자율_최종제안 1회 보장, 금액은 목표가** | 확인 없이 결렬 / 직전 금액을 "최종"으로 반복해 승인 여지를 남긴 채 종료하던 문제 | 안전 (영구) |
| ⑦ | **결렬(walk)도 해금 전 금지** — 설득 ≥2회 전에는 설득만 가능 | 설득 0회에 walk 선택 시 최종제안 보장(⑥)과 결합해 "첫 턴 목표가 통보"가 됨 (v3.4 라이브 결함) | 예절 (해제 후보) |
- 구현: 서빙 `policy/autonomy_store.py` 후보 마스크 + 학습 `tools/train_full_autonomy.py` `available_actions` **양쪽 동일**
- 예절 봉투(③④⑤)는 실로그가 쌓이면 `AUTONOMY_MIN_PRESS=0` 등으로 해제 실험 가능
## 5. 멘트 — 템플릿 → LLM + 가드레일
**역할 분리(안전 설계):** 무엇을 말할지(금액/전략/수락/결렬)는 RL 이 결정, LLM 은 **표현만** 담당.
```
설정: agent/config/config.local.toml [OpenAIConfig]
모델: gemini-2.5-flash-lite (OpenAI 호환 base_url)
· 2.5-flash → thinking 지연으로 백엔드 10초 한도 초과 ("협상 응답 지연" 토스트 원인)
· 2.0-flash → 은퇴(404)
시간: LLM_TIMEOUT_S=6 초과 시 템플릿 폴백 (검증 최대 응답 2.9초)
끄기: AUTONOMY_LLM=0
```
**할루시네이션 가드 (하나라도 걸리면 템플릿 폴백, 협상은 계속):**
| 가드 | 내용 |
|---|---|
| 숫자 화이트리스트 | 프롬프트로 준 금액(제시가·제안가·직전제안가·양보폭) 외 숫자 = 즉시 폐기 |
| **목표가 비공개** | 압박 프롬프트에 목표가 미포함 + 화이트리스트에서도 제외 — 노출 사고 재발 방지 |
| 금지어 | 보장/물량/독점/최저가/시장가/%/계약기간/법적 등 승인 안 된 전술·커밋 |
| 문장 완결 | thinking 토큰 소진으로 잘린 문장 폐기 (max_tokens 2048) |
| 제안가 포함 | 역제안·최종제안 멘트에 제안 금액 필수 |
**추가 기능:**
- **인터넷 최저가 인용** (구 NGC-008 자율판): 수집돼 있고 제시가 > 최저가일 때만 근거 인용 허용 — 그 턴에만 '최저가' 금지어 해제, 수치는 화이트리스트 검증
- **대화 기억**: 직전 제안 거절 사실·양보폭을 멘트에 반영("직전 제안에서 5원 상향한…") + 직전 멘트와 같은 문장구조 반복 금지 — "멘트가 다 똑같다" 해결. temperature 0.9
## 6. 학습 시뮬레이터 버전 이력 — 실패 2건 포함
| 버전 | 변경 | 결과 |
|---|---|---|
| v1 | 최초 학습 | 한 방 큰 컷 + 같은 숫자 반복 → "이게 협상이야??" |
| v2 | 에피소드 기억·컷 특징·협력사 반복 짜증/이탈 | 개선되나 지형 불일치 잔존 |
| v3 | 상태 21차원 확장 | — |
| v3.1 | **지형 정합**: 앵커율 0.8~6% 샘플링 (실제 ~1% vs 시뮬 20%) | ⚠️ 보상 구멍 착취 — 목표가+14% 매입 학습 → 봉투 ① 신설 |
| **v3.2** | 컷 반발·반복 짜증·**양보 상호성**(우리가 올리면 상대도 내림)·floor ≤ 첫제시가×0.98 | ✅ **현재 서빙본** (목표가 초과 타결 0/30) |
| v3.3 | 단조·상호성 반영 재학습 | ❌ "무조건 목표가 즉시 지르기"로 퇴화 → **프로브 게이트 반려** (`full_autonomy.pt` 만 보관, 미서빙) |
| v3.4 | 봉투 ①~⑤ 정합 + **보상 수정**(목표가 초과 타결 = 결렬 취급) 재학습 | ❌ **반려** — 초기 게이트(실스케일 단일 지형) 통과 후 라이브에서 퇴화 발견: 소액 지형에서 첫 턴 walk→목표가 통보 / walk 잠금 후엔 압박 12연발·최종제안 생략·화법 단조(전부 전략3). 게이트를 2개 지형으로 확장해 재판정 → v3.2 우위 확인, v3.2 복원 (`autonomy_v34_rejected.npz` 보관) |
| **v3.5** | v3.4 + **관측성 마스크**: 마감 40% 미관측(0.5 고정)·15% 완전 미상 에피소드 — 서빙 중립값 상태를 시뮬 분포에 혼입 (v3.4 퇴화 원인 해소) | ✅ **현재 서빙본** — 게이트 78/78, 사다리 3단 사용, 협조 케이스 목표가 대비 -2,116원 타결. 게이트가 이 과정에서 **철회 실버그** 발견(아래) |
> **교훈 1 — 보상 = 유일한 스펙**: 룰을 제거하면 보상 함수의 구멍이 곧 행동이 된다 (v3.1).
> **교훈 2 — 프로브 게이트**: 재학습은 퇴화할 수 있다. 배포 전 반드시 실스케일 제시가별 행동표(`tools/probe_serving_dqn.py`)로 비교 검증 (v3.3).
> **교훈 3 — 지형 일반화**: 한 지형의 게이트 통과가 다른 지형을 보증하지 않는다 (v3.4 — 실스케일 통과, 소액 퇴화).
> **교훈 4 — 시뮬은 관측까지 닮아야 한다**: 세계뿐 아니라 '무엇을 모르는지'도 서빙과 같아야 한다. 마감·이력 미상(중립값) 상태가 시뮬에 없으면 그 상태가 분포 밖이 된다 (v3.4 원인 → v3.5 해소).
**철회 실버그 (게이트가 발견, 2026-07-10 수정):** 단조 봉투의 기준 `autonomy_last`가 '마지막 행동'이라 counter→**press**→counter 순서에서 설득이 역제안 기억을 덮어써 봉투가 뚫렸다(9,975 제안 후 9,900 재제안). 역제안 기억을 `autonomy_last_counter`로 별도 보존하도록 수정 — 시뮬(역제안만 추적)과도 일치. v3.2는 이 패턴을 쓰지 않아 드러나지 않았을 뿐 프로덕션에 실존하던 구멍.
## 7. 현재 협상 흐름 (검증 완료)
```
협력사 제시
설득(압박) ≥2회 ── 인터넷최저가 근거 인용 가능, 목표가 절대 비공개
역제안 해금 ── 첫 제안은 앵커가 이하로 개시 (봉투⑤)
단조 상향 사다리 ── 후퇴 금지(봉투②), 양보폭·속도는 정책이 결정
목표가 0.5% 이내 ── 압박 중단, 클로징만 (봉투④)
├─ 제시가 ≤ 목표가 → 수락 → 협상완료
├─ 같은 금액 반복 / 결렬 의사 → 자율_최종제안 1회, 금액=목표가 (봉투⑥)
│ ├─ 예 → 협상완료 └─ 아니오 → 협상실패
└─ 12턴 초과(엔지니어링 캡) → 최종제안(목표가) 1회 거쳐 종료 — 캡도 봉투⑥을 우회하지 않음
```
## 8. 운영 스위치 & 파이프라인
| 스위치 (docker-compose agent env) | 값 | 의미 |
|---|---|---|
| `AUTONOMY_MODE` | 1 | 자율 모드 (0 = 룰 엔진 복귀) |
| `DQN_SERVING` | 1 | 카드 선택 DQN (0 = UCB Q-table) |
| `AUTONOMY_LLM` | 1(기본) | LLM 멘트 (0 = 템플릿만) |
| `AUTONOMY_MIN_PRESS` | 2(기본) | 역제시 해금에 필요한 설득 횟수 |
| `LLM_TIMEOUT_S` | 6(기본) | LLM 시간 상한, 초과 시 템플릿 폴백 |
**학습→배포 파이프라인:**
```
tools/train_full_autonomy (시뮬 15k ep, 룰 베이스라인 비교)
→ tools/export_autonomy_serving (artifacts/autonomy_serving.npz, .prev 자동 백업)
→ tools/probe_serving_dqn (실스케일 행동표 — 눈으로 보는 진단)
→ tools/test_autonomy_defects (결함 회귀 게이트 — 지형 2종×시나리오 3종 + 단위·멘트가드 검사,
자동 합격/불합격. 단, v3.4 사례처럼 게이트 통과 ≠ 품질 보증:
궤적 자체도 눈으로 비교할 것)
→ docker compose build agent (npz 는 이미지에 베이크)
```
**로깅:** 자율 행동도 experience_logs 에 기록 (card_id = `AUT|종류|위치|전략`, 진행 row + 종결 row). 카드 재학습(`retrain_from_logs`)은 AUT 세션 자동 제외.
## 9. 변경 파일 지도
| 파일 | 역할 |
|---|---|
| `negotiation/policy/autonomy_store.py` | **신규** — 자율 정책 numpy 서빙 + 봉투 ①~⑤ 마스크 |
| `negotiation/policies/autonomy_actions.py` | **신규** — 행동 30개·특징 인코딩 (학습/서빙 공유) |
| `negotiation/chat/service/ment_generator.py` | **신규** — LLM 멘트 생성 + 가드레일 |
| `negotiation/chat/service/chat_engine.py` | 자율 스텝(자율_역제안/최종제안/압박_1~4) + `_autonomy_next` 봉투⑥ |
| `services/chat_service.py` | decider 주입·행동 로깅·대화기억 ctx 관리 |
| `negotiation/chat/infra/repository/nego_context_crud.py` | 인터넷최저가·견적기간·협력사이력 조회 |
| `negotiation/chat/service/negotiation_context_loader.py` | 확장 컨텍스트 로드 (company_id) |
| `tools/train_full_autonomy.py` | **신규** — 시뮬레이터(현실화 협력사 모델) + DQN 학습 |
| `tools/export_autonomy_serving.py` / `probe_serving_dqn.py` | **신규** — 번들 내보내기 / 프로브 게이트 |
| `tools/test_autonomy_defects.py` | **신규** — 결함 회귀 게이트: 실전에서 발견된 결함 41항목을 시나리오·단위·멘트가드 검사로 자동 재생 (서빙 실물 코드 구동, DB/LLM 불필요) |
| `config/config.local.toml` | Gemini 접속 정보 (gitignore, 이미지에 베이크) |
| `docker-compose.yml` | `AUTONOMY_MODE` / `DQN_SERVING` 플래그 |
## 10. 남은 일
- [x] 결함 회귀 게이트 구축 — `test_autonomy_defects.py` 41항목, v3.2 전항목 통과 확인 (2026-07-10)
- [x] 보상 수정 — 목표가 초과 타결은 학습 보상에서 결렬 취급 (v3.1 구멍을 유인 수준에서 차단, 봉투 ①과 이중 방어)
- [ ] ⚠️ **Gemini API 키 재발급** — 채팅에 노출된 키, 테스트 종료 후 반드시 교체 (config.local.toml + 이미지 리빌드)
- [x] 봉투 정합 재학습 — v3.4 반려(소액 지형 퇴화) → 원인 규명(관측성 불일치) → **v3.5 관측성 마스크로 해소, 배포 완료** (2026-07-10)
- [x] 철회 실버그 수정 — counter→press→counter 에서 단조 봉투 뚫림 → `autonomy_last_counter` 별도 보존
- [x] 턴캡 최종제안 보장 — 캡 종료도 "끝내기 전 한 번 더"를 거침
- [ ] 상대 발화 LLM 파싱 (보류 중 — 프론트 입력 UI 변경 필요)
- [ ] 실로그 축적 후: 예절 봉투(③④⑤) 해제 실험 → LLM 협력사 셀프플레이 (집컴 GPU 단계)
- [ ] (소소) negodata 프론트 "목표 마진율 1000%" 표시 버그 후보

View File

@ -1,4 +1,4 @@
# 알고리즘 비교 실험 기본 설정 (H5). E2E: python -m eval_harness.runner --config configs/exp_default.yaml --tenant ktcommerce # 알고리즘 비교 실험 기본 설정 (H5). E2E: python -m eval_harness.runner --config configs/exp_default.yaml --tenant imarketkorea
episodes: 600 # 정책당 협상 에피소드 수 (action 11장 탐색 수렴 위해 상향) episodes: 600 # 정책당 협상 에피소드 수 (action 11장 탐색 수렴 위해 상향)
seed: 42 # 재현용 (구매자 randomness 페어드) seed: 42 # 재현용 (구매자 randomness 페어드)
max_turns: 5 # 협상 라운드 상한 max_turns: 5 # 협상 라운드 상한

View File

@ -1,104 +0,0 @@
"""FeatureBuyer — 카드 '내용(전략)'과 협력사 '프로필'에 반응하는 시뮬 협력사 (Phase 2·3).
효과를 2축으로 분리한다(성향 조건화가 의미를 가지려면 트레이드오프가 필요):
- 양보력(concession power): 카드가 가격을 얼마나 끌어내리는가
- 수락력(accept power) : 카드가 합의(수락) 확률을 얼마나 높이는가
전략별 기본 프로필(트레이드오프):
경쟁(1): 양보력 수락력 세게 깎지만 결렬 위험
수용(2): 양보력 수락력
고수(3): 양보력·수락력 중간
협력(4): 양보력 수락력 성사되지만 깎임
여기에 협력사 세그먼트 적합도(AFFINITY) 곱해진다: 전략이 협력사에 맞으면 죽는다.
소형·경쟁多 경쟁압박이 먹힘 / 대형·단독 협력이 먹힘(압박 역효과)
'가격 중시' 고객사는 경쟁 카드(많이 깎음, 결렬 감수), '성사 중시' 고객사는 협력 카드가 정답이
되는 구조. 에이전트는 카드 특징 + 협력사 특징 + 고객사 성향으로 이를 학습해야 한다.
"""
from dataclasses import dataclass
from typing import Dict, Tuple
import numpy as np
from eval_harness.buyer import BuyerResponse, Scenario
# strategy_type: 1=경쟁, 2=수용, 3=고수, 4=협력 (card.nego_cards)
# 전략별 (양보력, 수락력) 기본 프로필 — 트레이드오프의 원천
STRATEGY_PROFILE: Dict[int, Tuple[float, float]] = {
1: (0.90, 0.25), # 경쟁: 세게 깎지만 성사 어려움
2: (0.35, 0.70), # 수용
3: (0.55, 0.50), # 고수: 중간
4: (0.30, 0.90), # 협력: 잘 성사되지만 덜 깎임
}
# 세그먼트별 전략 적합도 m ∈ [0,1] — 전략이 그 협력사에 얼마나 '먹히는가'
AFFINITY: Dict[Tuple[str, str], Dict[int, float]] = {
("small", "multi"): {1: 0.90, 2: 0.45, 3: 0.60, 4: 0.40}, # 소형·경쟁多 → 경쟁압박
("small", "single"): {1: 0.35, 2: 0.60, 3: 0.80, 4: 0.55}, # 소형·단독 → 고수/논리
("big", "multi"): {1: 0.65, 2: 0.50, 3: 0.70, 4: 0.60},
("big", "single"): {1: 0.20, 2: 0.70, 3: 0.50, 4: 0.90}, # 대형·단독 → 협력 (압박 역효과)
}
REVENUE_BIG = 50_000_000 # state config 'high' 경계와 정합
@dataclass
class SupplierProfile:
"""협력사 프로필 — 에피소드마다 달라지는 협상 상대. snapshot 필드와 정합."""
revenue_amount: float
partner_count: int # 이 품목의 대안 협력사 수 (BATNA. 1:1 채팅이어도 다양)
distribution_code: str
@property
def segment(self) -> Tuple[str, str]:
size = "big" if self.revenue_amount > REVENUE_BIG else "small"
comp = "multi" if self.partner_count >= 2 else "single"
return (size, comp)
def sample_supplier(rng: np.random.Generator) -> SupplierProfile:
"""무작위 협력사 생성 (학습 데이터 다양성)."""
return SupplierProfile(
revenue_amount=float(rng.choice([5_000_000, 20_000_000, 80_000_000, 200_000_000])),
partner_count=int(rng.choice([1, 1, 2, 3])), # 단독 비중 높게
distribution_code=str(rng.choice(["A", "B", "C"])),
)
class FeatureBuyer:
"""전략 프로필 x 세그먼트 적합도 기반 협력사 모델. (양보력, 수락력) 2축."""
def __init__(self, supplier: SupplierProfile, card_strategy: Dict[str, int], seed: int = 0,
accept_base: float = 0.08, max_turns: int = 5, jitter: float = 0.05):
self.supplier = supplier
self.card_strategy = card_strategy # {card_number: strategy_type}
self.rng = np.random.default_rng(seed)
self.accept_base = accept_base
self.max_turns = max_turns
self.jitter = jitter
# 숨은 하한가(reservation): 앵커의 94~110%. 앵커보다 높으면(약 60%) 가격만으로는
# 타결 불가 → 수락을 받아내야 함 → 수락력 낮은(경쟁) 카드에 진짜 결렬 위험이 생긴다.
self.floor_ratio = float(self.rng.uniform(0.94, 1.10))
def powers(self, card_number: str) -> Tuple[float, float]:
"""숨은 (양보력, 수락력). 전략 프로필 × 세그먼트 적합도 + 카드별 결정론적 지터."""
strat = self.card_strategy.get(card_number, 3)
conc_base, acc_base = STRATEGY_PROFILE.get(strat, (0.5, 0.5))
m = AFFINITY[self.supplier.segment].get(strat, 0.5)
scale = 0.35 + 0.85 * m # 적합도: 안 맞으면 둘 다 죽음 (0.35~1.2)
j = (hash(card_number) % 1000 / 1000.0 - 0.5) * 2 * self.jitter
c_pow = float(np.clip(conc_base * scale + j, 0.02, 0.98))
a_pow = float(np.clip(acc_base * scale + j, 0.02, 0.98))
return c_pow, a_pow
def respond(self, card_number: str, scenario: Scenario, turn: int, current_price: float) -> BuyerResponse:
c_pow, a_pow = self.powers(card_number)
floor = scenario.anchor_price * self.floor_ratio # 숨은 하한가 (앵커 이하 보장 없음)
concession = (current_price - floor) * (0.10 + 0.55 * c_pow)
new_price = max(floor, current_price - concession)
p_accept = min(0.97, self.accept_base + 0.80 * a_pow + 0.05 * (turn - 1))
accept = bool(self.rng.random() < p_accept)
walked = (not accept) and (turn >= self.max_turns)
return BuyerResponse(accept=accept, new_price=new_price, walked=walked)

View File

@ -1,6 +1,6 @@
"""eval_harness 러너 — 정책 비교 + 학습곡선 (H5, PoC 본체). """eval_harness 러너 — 정책 비교 + 학습곡선 (H5, PoC 본체).
E2E: python -m eval_harness.runner --config configs/exp_default.yaml --tenant ktcommerce E2E: python -m eval_harness.runner --config configs/exp_default.yaml --tenant imarketkorea
판정: 학습형(qtable_ucb) random/static 대비 평균보상·성공률 우상향이면 "학습 루프 유효". 판정: 학습형(qtable_ucb) random/static 대비 평균보상·성공률 우상향이면 "학습 루프 유효".
구매자는 카드별 효과가 다른 시뮬(HeuristicBuyer) 학습 정책만 좋은 카드를 알아내 성과가 오른다. 구매자는 카드별 효과가 다른 시뮬(HeuristicBuyer) 학습 정책만 좋은 카드를 알아내 성과가 오른다.
@ -143,7 +143,7 @@ def _print(report: dict):
def main(): def main():
ap = argparse.ArgumentParser(description="협상 정책 비교 하네스 (H5)") ap = argparse.ArgumentParser(description="협상 정책 비교 하네스 (H5)")
ap.add_argument("--config", default="configs/exp_default.yaml") ap.add_argument("--config", default="configs/exp_default.yaml")
ap.add_argument("--tenant", default="ktcommerce") ap.add_argument("--tenant", default="imarketkorea")
ap.add_argument("--save", action="store_true", help="reports/ 에 JSON 저장") ap.add_argument("--save", action="store_true", help="reports/ 에 JSON 저장")
args = ap.parse_args() args = ap.parse_args()

View File

@ -20,6 +20,13 @@ from negotiation.cards.ports.card_script_port import ICardScriptRepository
_NEGO_CARDS = table( _NEGO_CARDS = table(
"nego_cards", "nego_cards",
column("number"), column("script"), column("tone"), column("strategy_type"),
column("created_at"), column("deleted"),
schema="card",
)
_WILD_CARDS = table(
"wild_cards",
column("number"), column("script"), column("created_at"), column("deleted"), column("number"), column("script"), column("created_at"), column("deleted"),
schema="card", schema="card",
) )
@ -27,17 +34,40 @@ _NEGO_CARDS = table(
class CardScriptDbRepository(ICardScriptRepository): class CardScriptDbRepository(ICardScriptRepository):
async def get_script_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[str]]: async def get_script_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[str]]:
err, card = await self.get_card_by_number(cdb, number)
return err, (card[0] if card else None)
async def get_wild_card_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[str]]:
"""card.wild_cards 멘트 조회 (WC-01~05 선택형 와일드카드/종결 전술 발동용)."""
try: try:
query = ( query = (
select(_NEGO_CARDS.c.script) select(_WILD_CARDS.c.script)
.where(_WILD_CARDS.c.number == number, _WILD_CARDS.c.deleted == False) # noqa: E712
.order_by(desc(_WILD_CARDS.c.created_at))
.limit(1)
)
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_wild_card_script failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or not rows[0]:
return err_type, None
return ErrorType.SUCCESS, str(rows[0]) if not isinstance(rows[0], tuple) else str(rows[0][0])
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None
async def get_card_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[tuple]]:
try:
query = (
select(_NEGO_CARDS.c.script, _NEGO_CARDS.c.tone, _NEGO_CARDS.c.strategy_type)
.where(_NEGO_CARDS.c.number == number, _NEGO_CARDS.c.deleted == False) # noqa: E712 .where(_NEGO_CARDS.c.number == number, _NEGO_CARDS.c.deleted == False) # noqa: E712
.order_by(desc(_NEGO_CARDS.c.created_at)) .order_by(desc(_NEGO_CARDS.c.created_at))
.limit(1) .limit(1)
) )
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_card_script failed.", raise_error=False) err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_card_script failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or not rows[0]: if err_type != ErrorType.SUCCESS or not rows or not rows[0] or not rows[0][0]:
return err_type, None return err_type, None
return ErrorType.SUCCESS, str(rows[0]) script, tone, strategy = rows[0]
return ErrorType.SUCCESS, (str(script), int(tone) if tone is not None else None,
int(strategy) if strategy is not None else None)
except Exception as ex: except Exception as ex:
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None return ErrorType.DB_RUN_FAILED, None

View File

@ -0,0 +1,240 @@
"""협상카드 전술 — "스크립트에 꽂힌 변수가 곧 전술" 계층.
카드 멘트가 제시하는 가격({target_price}·{middle_price} ) 파싱해 시스템 상태로 실행한다:
카드가 제안가를 제시하면 pending_counter_price 적재되고, 협력사가 수락하면 가격으로 타결된다.
계층으로 나뉜다.
1) 스크립트 파싱 카드가 부를 금액이 무엇인지 (parse_offer_variable)
2) 변수 정의 금액을 지금 있는지 (OFFER_VARIABLES 계산식 + 유효조건)
3) tactic JSONB 문장으로 없는 운영 규칙 (min_round·closing)
유효 조건은 카드가 아니라 '변수' 붙인다 금액이 성립하는지는 계산식의 성질이지 카드의
성질이 아니다. 변수는 OFFER_VARIABLES 추가하면 코드 분기 없이 끝난다.
"""
import re
from dataclasses import dataclass
from typing import Any, Callable, Dict, Optional
# 제안가 변수 — 우리가 새로 부르는 금액. 값은 (계산식, 재료 설명).
# 여기 없는 치환 변수({prev_partner_price}·{internet_lowest_price} 등)는 읽어주기 전용이라
# 제안가가 되지 않는다 — 과거값·외부값을 협력사에게 "수락하라"고 내밀 수 없기 때문.
OFFER_VARIABLES: Dict[str, Callable[[float, float, float, float], Optional[float]]] = {
# (target, anchor, price, prev_customer) -> 제안가 | None(재료 없음)
"target_price": lambda target, anchor, price, prev: target,
"anchoring_price": lambda target, anchor, price, prev: anchor or None,
# negodata 카드 에디터 칩 표기(variables.ts) — DB 시드 표기(anchoring_price)와 같은 값의 별칭.
"anchor_price": lambda target, anchor, price, prev: anchor or None,
"target_mid_price": lambda target, anchor, price, prev: (anchor + target) / 2 if anchor else None,
"middle_price": lambda target, anchor, price, prev: (prev + price) / 2 if prev else None,
}
_TOKEN_RE = re.compile(r"\{([a-z_]+)\}")
# 절충 계열 변수 — 양측 사이/우리 두 값 사이의 중간을 부르는 카드. 목표가 이상이면 미발동한다.
_MID_VARIABLES = ("middle_price", "target_mid_price")
# 세션 데이터에 따라 값이 없을 수 있는 읽기 전용 변수 → 그 값을 담는 컨텍스트 키.
# 스크립트가 이런 변수를 인용하면 값이 있을 때만 카드가 나간다 — 없는데 나가면 협력사 채팅에
# {internet_lowest_price} 토큰이 원형 노출된다(vars_for 가 미수집이면 키를 안 만드는 것과 짝).
# 견적 생성 화면 게이팅(useCardGating)이 1차 방어, 여기가 2차(런타임) 방어다.
_CONTEXT_REQUIRED_VARIABLES = {
"internet_lowest_price": "internet_lowest_price",
"internet_min_price": "internet_lowest_price",
}
@dataclass(frozen=True)
class CardSpec:
"""카드 1장의 전술. 스크립트 파싱 결과 + tactic JSONB 를 합친 값.
offer_variable: 카드가 제시할 금액의 변수명. None 이면 순수 설득 카드(HOLD).
min_round: 발동 가능 최소 라운드(협력사 가격 입력 횟수 기준).
closing: 종결 국면 전용 라운드 상한·카드 소진 시의 마지막 방으로만 쓴다.
requires: 스크립트가 인용한 세션-의존 변수의 컨텍스트 값이 없으면 미발동(토큰 노출 방지).
"""
offer_variable: Optional[str] = None
min_round: int = 1
closing: bool = False
requires: tuple = ()
HOLD = CardSpec() # 스펙을 못 찾은 카드(테넌트 데모·회사 커스텀)의 폴백 — 기존 동작(설득만) 유지
def settle_ceiling(context: Dict[str, Any]) -> float:
"""이 협상에서 받아줄 수 있는 최고가 — 타결 판정선이자 카드 제안가의 상한.
견적 생성 세션에 박제한 done_ceiling_price(= 목표가 × (1 + 타결상한율)) 목표가를 조금
넘더라도 기존 단가보다 인하됐으면 타결시키기 위한 . 박제가 없으면 목표가로 폴백한다.
"""
return float(context.get("done_ceiling_price") or context.get("target_price") or 0)
def parse_offer_variable(script: Optional[str]) -> Optional[str]:
"""스크립트가 제시하는 제안가 변수. 없으면 None(설득 카드).
변수가 여럿이면 마지막에 등장하는 것이 제안가다 카드 문장은 배경을 먼저 깔고 실제 제안을
마지막에 하기 때문이다.
"""
found = [m.group(1) for m in _TOKEN_RE.finditer(script or "") if m.group(1) in OFFER_VARIABLES]
return found[-1] if found else None
def build_card_spec(script: Optional[str], tactic: Optional[dict] = None) -> CardSpec:
"""스크립트 + tactic JSONB → CardSpec. tactic 이 비어 있으면 전부 기본값."""
t = tactic or {}
cited = {m.group(1) for m in _TOKEN_RE.finditer(script or "")}
return CardSpec(
# 파싱이 정본 카드 전부를 맞히므로 offer_variable 은 예외 카드용 override 로만 둔다.
offer_variable=t.get("offer_variable") or parse_offer_variable(script),
min_round=int(t.get("min_round") or 1),
closing=bool(t.get("closing")),
requires=tuple(sorted({_CONTEXT_REQUIRED_VARIABLES[v] for v in cited if v in _CONTEXT_REQUIRED_VARIABLES})),
)
def spec_from_context(context: Dict[str, Any], number: Optional[str]) -> CardSpec:
"""세션 컨텍스트에 적재된 카드 스펙(card_specs)에서 꺼낸다. 없으면 HOLD 폴백.
스펙은 협상 시작 1 적재된다(negotiation_context_loader) 진행 중인 협상은
카드 멘트가 도중에 바뀌어도 시작 시점 전술로 끝까지 간다.
"""
if not number:
return HOLD
raw = (context.get("card_specs") or {}).get(str(number))
if not raw:
return HOLD
return CardSpec(
offer_variable=raw.get("offer_variable"),
min_round=int(raw.get("min_round") or 1),
closing=bool(raw.get("closing")),
requires=tuple(raw.get("requires") or ()),
)
@dataclass(frozen=True)
class Offer:
"""확정된 제안 한 건 — 금액과 그 금액을 만든 재료를 함께 들고 다닌다.
멘트 치환이 재료를 다시 계산하지 않게 하기 위한 재계산하면 사이 갱신된
prev_customer 읽어 문장이 자기모순이 된다.
"""
price: int # 협력사에게 제시할 금액(수락 시 타결가)
variable: str # 이 금액을 만든 멘트 변수
prev_customer: int # 계산에 쓴 당사 직전 제안
prev_partner: int # 계산에 쓴 협력사 제시가
def record_offer(context: Dict[str, Any], offer: Offer) -> None:
"""확정 제안을 세션에 기록한다 — 수락 판정용 금액과 멘트 치환용 재료를 한 자리에서 쓴다.
키를 항상 함께 써야 표시가와 타결가가 갈라지지 않으므로 기록 지점을 여기 하나로 묶는다.
"""
context["pending_counter_price"] = offer.price
context["pending_offer"] = {
"price": offer.price, "variable": offer.variable,
"prev_customer": offer.prev_customer, "prev_partner": offer.prev_partner,
}
context["prev_customer_price"] = offer.price # 갑의 최신 포지션 — 다음 라운드 계산·역행 금지 기준
def compute_offer_detail(spec: CardSpec, context: Dict[str, Any]) -> Optional[Offer]:
"""카드가 제시할 금액 + 그 계산에 쓴 재료. 쓸 수 없는 상황이면 None."""
price = int(float(context.get("input_price") or 0))
prev_customer = int(float(context.get("prev_customer_price") or context.get("anchor_price") or 0))
value = compute_offer(spec, context)
if value is None:
return None
return Offer(price=value, variable=spec.offer_variable or "", prev_customer=prev_customer, prev_partner=price)
def compute_offer(spec: CardSpec, context: Dict[str, Any]) -> Optional[int]:
"""카드가 제시할 금액. 쓸 수 없는 상황이면 None → 호출부가 카드를 건너뛴다.
변수 공통 유효조건 (전부 만족해야 발동):
· 타결 상한가 받아줄 없는 금액은 부르지 않는다. 넘으면 깎지 않고 미발동
· < 협력사 제시가 이미 싸게 받았는데 비싼 값을 부를 이유가 없다
· 당사 직전 제안 역행 금지. 제안 시퀀스는 앵커목표가로 단조 수렴해야 한다
"""
variable = spec.offer_variable
if not variable:
return None # 설득 카드 — 제시할 금액 없음
calc = OFFER_VARIABLES.get(variable)
if calc is None:
return None # 미등록 변수(오타·구버전 카드)
target = float(context.get("target_price") or 0)
anchor = float(context.get("anchor_price") or 0)
price = float(context.get("input_price") or 0)
# 갑의 직전 포지션. 첫 카운터 전에는 앵커가 갑의 포지션이다.
prev_customer = float(context.get("prev_customer_price") or anchor or 0)
if target <= 0 or price <= 0:
return None # 목표가·제시가 없이는 어떤 변수도 판정 불가
value = calc(target, anchor, price, prev_customer)
if not value or value <= 0:
return None # 재료 부족(앵커 미박제·직전 제안 없음)
if value > settle_ceiling(context):
return None # 타결 상한 초과 — 받아줄 수 없는 금액이라 지금 못 쓴다
if variable in _MID_VARIABLES and value >= target:
# 절충 계열은 목표가 미만일 때만 의미가 있다. 목표가 이상이면 "절반씩 나누자"면서 목표가를
# 부르는 꼴이라 미발동 — 목표가 제시는 목표가 카드(최후통첩)가 할 일이다.
return None
if variable in ("target_price", "anchoring_price", "anchor_price"):
# 원값 인용 변수 — 멘트엔 {target_price} 등 저장 원값이 그대로 나가므로, 반올림하면
# 표시가≠타결가 미스매치가 난다(목표가 7652 멘트 → 7650 타결). 저장값 그대로 제시.
offer = int(value)
else:
offer = int(value / 10 + 0.5) * 10 # 파생가(절충·중간) 10원 반올림 — 앵커·목표가 산정과 표기 통일
if offer >= price:
return None # 제시가가 이미 그 값 이하 → 부를 이유 없음
if prev_customer and offer < prev_customer:
return None # 역행 금지 — 한번 부른 금액 아래로 되돌아가지 않는다(같은 금액 재제시는 허용)
return offer
def available(spec: CardSpec, context: Dict[str, Any], *, closing_phase: bool = False) -> bool:
"""지금 이 카드를 꺼낼 수 있는지 — 금액과 무관한 조건들.
· 이미 카드는 다시 나간다( 카드 공통 규칙 협상카드/와일드카드 구분 없음)
· 종결 전용 카드는 종결 국면에서만, 종결 국면에선 종결 전용 카드만
· min_round 미만이면 아직 이르다
· 스크립트가 인용한 세션-의존 변수(인터넷 최저가 ) 결측이면 미발동 토큰 원형 노출 방지
"""
if spec.closing != closing_phase:
return False
if int(context.get("round") or 0) < spec.min_round:
return False
return all(context.get(key) for key in spec.requires)
def playable(spec: CardSpec, context: Dict[str, Any], *, closing_phase: bool = False) -> bool:
"""이 카드를 지금 실제로 플레이할 수 있는지 — available + (금액 카드는) 제안가 유효까지.
금액을 인용하는 카드(offer_variable 있음) 금액을 부르는 상황이면 설득 폴백으로도
내보내지 않는다 멘트에 무효한 금액(직전 제안보다 낮은 앵커, 제시가보다 높은 목표가)
글자로 박혀 나가 역행/모순 서사가 되기 때문. 설득 카드는 금액이 없으니 무관.
"""
if not available(spec, context, closing_phase=closing_phase):
return False
if not spec.offer_variable:
return True
return compute_offer(spec, context) is not None
def is_played(context: Dict[str, Any], number: Optional[str]) -> bool:
"""이 카드를 이 협상에서 이미 썼는지. 와일드 진입·종결·협상카드가 같은 이력을 본다."""
return bool(number) and str(number) in (context.get("played_card_numbers") or [])
def mark_played(context: Dict[str, Any], number: Optional[str]) -> None:
"""카드를 실제로 내보낸 시점에 이력에 남긴다(노출되지 않은 후보는 남기지 않는다)."""
if not number:
return
played = list(context.get("played_card_numbers") or [])
if str(number) not in played:
played.append(str(number))
context["played_card_numbers"] = played

View File

@ -21,3 +21,18 @@ class ICardScriptRepository(ABC):
async def get_script_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[str]]: async def get_script_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[str]]:
"""카드코드(card.nego_cards.number)로 멘트(script 평문/마커)를 조회. 없으면 None.""" """카드코드(card.nego_cards.number)로 멘트(script 평문/마커)를 조회. 없으면 None."""
... ...
async def get_card_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[tuple]]:
"""멘트 + 메타 (script, tone, strategy_type) 조회 — LLM 표현층의 톤/전략 지시용.
기본 구현은 script 조회하고 메타는 None (파일 소스·구형 더블 호환).
"""
err, script = await self.get_script_by_number(cdb, number)
return err, ((script, None, None) if script else None)
async def get_wild_card_by_number(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, Optional[str]]:
"""와일드카드(card.wild_cards.number) 멘트 조회 — 선택형 WC/종결 전술 발동용.
기본 구현은 미보유(None) DB 어댑터만 실조회한다(더블 호환).
"""
return ErrorType.SUCCESS, None

View File

@ -23,23 +23,48 @@ _SESSIONS = table(
"sessions", "sessions",
column("session_id"), column("quotation_id"), column("item_id"), column("supplier_id"), column("session_id"), column("quotation_id"), column("item_id"), column("supplier_id"),
column("qt_type"), column("target_price"), column("anchoring_price"), column("qt_type"), column("target_price"), column("anchoring_price"),
column("done_ceiling_price"), # 타결 상한가 — 견적 생성 시 박제(목표가×(1+타결상한율))
column("qt_setting_id"),
column("deleted"), column("deleted"),
schema="negotiation", schema="negotiation",
) )
_ITEMS = table("items", column("item_id"), column("price"), column("internet_lowest_price"), # 견적 설정 — card_count(협상 내 협상카드 사용 횟수 상한) 조회용.
column("deleted"), schema="partner") _QUOTATION_SETTINGS = table(
_SUPPLIERS = table("suppliers", column("supplier_id"), column("total_revenue"), column("deleted"), schema="partner") "quotation_settings",
_QUOTATIONS = table( column("qt_setting_id"), column("card_count"), column("deleted"),
"quotations",
column("qt_id"), column("version_id"), column("supplier_type"),
column("start_time"), column("end_time"), column("deleted"),
schema="quotation", schema="quotation",
) )
# 자율 에이전트 이력 특징용 — agent 소유 learning 스키마 (done 행 = 협상 1건의 최종 결과). _ITEMS = table("items", column("item_id"), column("name"), column("price"), column("purchase_price"),
_EXP_LOGS = table( column("company_id"), column("internet_lowest_price"), column("deleted"), schema="partner")
"experience_logs", # 고객사 설정(companies.settings) — 협상 기준가로 쓸 가격 컬럼을 여기서 정한다.
column("session_id"), column("company_id"), column("done"), column("settled_price"), _COMPANIES = table("companies", column("company_id"), column("settings"), column("deleted"), schema="company")
schema="learning",
# 협상 기준가 후보 컬럼. 어느 컬럼을 고르든 공급사 화면 호칭은 '공급가'로 고정한다 —
# 같은 돈을 고객사는 매입가·상품 단가 등으로 부르지만 챗은 공급사가 보는 화면이라
# 공급사 관점 용어 하나만 쓴다. 회사 용어 사전(labels)은 관리자 화면 전용.
_BASELINE_PRICE = "price"
_BASELINE_PURCHASE = "purchase_price"
_SUPPLIER_PRICE_LABEL = "공급가"
def _resolve_baseline(settings: dict) -> str:
"""회사 설정 → 협상 기준가로 쓸 items 컬럼명.
1순위는 관리자가 회사 설정에서 고른 (features.nego_baseline_field).
미설정 회사는 공급가가 기본이되, 공급가를 화면에서 감췄다면 회사는 공급가를 관리하지
않는다는 뜻이므로 매입가로 폴백한다 설정 화면이 생기기 전에 만들어진 회사를 위한 안전망."""
chosen = (settings.get("features") or {}).get("nego_baseline_field")
if chosen in (_BASELINE_PRICE, _BASELINE_PURCHASE):
return chosen
hidden = set(settings.get("hidden_fields") or [])
if "price" in hidden and "purchase_price" not in hidden:
return _BASELINE_PURCHASE
return _BASELINE_PRICE
_SUPPLIERS = table("suppliers", column("supplier_id"), column("name"), column("total_revenue"), column("deleted"), schema="partner")
_QUOTATIONS = table(
"quotations",
column("qt_id"), column("version_id"), column("supplier_type"), column("deleted"),
schema="quotation",
) )
_VERSION_NEGO_CARDS = table( _VERSION_NEGO_CARDS = table(
"version_nego_cards", "version_nego_cards",
@ -53,12 +78,13 @@ _VERSION_WILD_CARDS = table(
) )
_NEGO_CARDS = table( _NEGO_CARDS = table(
"nego_cards", "nego_cards",
column("nego_card_id"), column("number"), column("deleted"), column("nego_card_id"), column("number"), column("script"), column("tactic"), column("deleted"),
schema="card", schema="card",
) )
_WILD_CARDS = table( _WILD_CARDS = table(
"wild_cards", "wild_cards",
column("wild_card_id"), column("number"), column("deleted"), column("wild_card_id"), column("number"), column("script"), column("tactic"), column("deleted"),
column("available"),
schema="card", schema="card",
) )
# 상품↔협력사 매핑 (2026-07-07 신설): supply_type = 이 협력사가 이 상품을 공급하는 방식(SupplierType). # 상품↔협력사 매핑 (2026-07-07 신설): supply_type = 이 협력사가 이 상품을 공급하는 방식(SupplierType).
@ -72,12 +98,28 @@ _SUPPLIER_ITEMS = table(
class INegoContextCRUD(ABC): class INegoContextCRUD(ABC):
@abstractmethod @abstractmethod
async def get_session_row(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, Optional[tuple]]: async def get_session_row(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, Optional[tuple]]:
"""세션 행 (qt_type, target_price, anchoring_price, item_id, quotation_id, supplier_id). 없으면 None.""" """세션 행 (qt_type, target_price, anchoring_price, done_ceiling_price, item_id, quotation_id, supplier_id). 없으면 None."""
pass pass
@abstractmethod @abstractmethod
async def get_item_price(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, int]: async def get_item_baseline(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, Tuple[int, str, dict]]:
"""품목 기준가(items.price). 없으면 0.""" """협상 기준가·그 호칭·회사 용어 사전 (가격, 호칭, labels).
어느 컬럼을 기준가로 쓰는지는 회사 설정(features.nego_baseline_field) 정한다.
labels companies.settings.labels 원본 협상 스크립트의 용어 토큰 치환에 쓴다.
값이 없으면 (0, 호칭, {})."""
pass
@abstractmethod
async def get_card_count(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, Optional[int]]:
"""협상카드 사용 횟수 상한(quotation_settings.card_count) — 세션의 qt_setting_id 로 조인.
설정이 없으면 None(호출부가 상한 미적용). 값이 협상 실제로 플레이 가능한 협상카드 수를 캡한다."""
pass
@abstractmethod
async def get_item_lowest_price(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, int]:
"""상품 인터넷 최저가(items.internet_lowest_price — LPS 수집 대표값). 미수집이면 0.
카드 스크립트 {internet_lowest_price} 치환용(NGC-008 시장가 인용 카드)."""
pass pass
@abstractmethod @abstractmethod
@ -85,6 +127,16 @@ class INegoContextCRUD(ABC):
"""협력사 총매출액(suppliers.total_revenue — KTC 미러). 없으면 0.0.""" """협력사 총매출액(suppliers.total_revenue — KTC 미러). 없으면 0.0."""
pass pass
@abstractmethod
async def get_item_name(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, Optional[str]]:
"""상품명(items.name). 없으면 None — 카드 스크립트 {product_name} 치환용."""
pass
@abstractmethod
async def get_supplier_name(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, Optional[str]]:
"""협력사명(suppliers.name). 없으면 None — 카드 스크립트 {partner_name} 치환용."""
pass
@abstractmethod @abstractmethod
async def get_supply_type(self, cdb: AsyncSession, supplier_id, item_id) -> Tuple[ErrorType, Optional[int]]: async def get_supply_type(self, cdb: AsyncSession, supplier_id, item_id) -> Tuple[ErrorType, Optional[int]]:
"""이 협력사가 이 상품을 공급하는 방식(supplier_items.supply_type: 0=none/1=유통/2=제조/3=총판). """이 협력사가 이 상품을 공급하는 방식(supplier_items.supply_type: 0=none/1=유통/2=제조/3=총판).
@ -102,24 +154,9 @@ class INegoContextCRUD(ABC):
pass pass
@abstractmethod @abstractmethod
async def get_quotation_card_numbers(self, cdb: AsyncSession, quotation_id) -> Tuple[ErrorType, tuple[list[str], list[str]]]: async def get_quotation_card_numbers(self, cdb: AsyncSession, quotation_id) -> Tuple[ErrorType, tuple[list[tuple], list[tuple]]]:
"""견적 version_id 에 연결된 (일반카드 번호 목록, 와일드카드 번호 목록). 없으면 빈 목록.""" """견적 version_id 에 연결된 (일반카드 행 목록, 와일드카드 행 목록). 없으면 빈 목록.
pass = (number, script, tactic) 스크립트 파싱 + tactic JSONB 카드 전술(CardSpec) 만든다."""
@abstractmethod
async def get_item_internet_lowest(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, int]:
"""상품 인터넷최저가(items.internet_lowest_price). 미수집이면 0."""
pass
@abstractmethod
async def get_quotation_period(self, cdb: AsyncSession, quotation_id) -> Tuple[ErrorType, Optional[tuple]]:
"""견적 협상 기간 (start_time, end_time). 없으면 None."""
pass
@abstractmethod
async def get_supplier_history(self, cdb: AsyncSession, company_id: str, supplier_id,
exclude_session_id) -> Tuple[ErrorType, tuple]:
"""이 협력사와의 과거 협상 이력 (횟수, 성사율, 평균 타결가/목표가). 없으면 (0, None, None)."""
pass pass
@ -128,6 +165,7 @@ class NegoContextCRUD(INegoContextCRUD):
try: try:
query = ( query = (
select(_SESSIONS.c.qt_type, _SESSIONS.c.target_price, _SESSIONS.c.anchoring_price, select(_SESSIONS.c.qt_type, _SESSIONS.c.target_price, _SESSIONS.c.anchoring_price,
_SESSIONS.c.done_ceiling_price,
_SESSIONS.c.item_id, _SESSIONS.c.quotation_id, _SESSIONS.c.supplier_id) _SESSIONS.c.item_id, _SESSIONS.c.quotation_id, _SESSIONS.c.supplier_id)
.where(_SESSIONS.c.session_id == session_id, _SESSIONS.c.deleted == False) # noqa: E712 .where(_SESSIONS.c.session_id == session_id, _SESSIONS.c.deleted == False) # noqa: E712
.limit(1) .limit(1)
@ -140,30 +178,61 @@ class NegoContextCRUD(INegoContextCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None return ErrorType.DB_RUN_FAILED, None
async def get_item_price(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, int]: async def get_item_baseline(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, Tuple[int, str, dict]]:
_fallback = (0, _SUPPLIER_PRICE_LABEL, {})
try: try:
# 상품 + 소속 고객사 설정 한 번에. 회사가 없어도(데이터 이상) 상품 행은 나오도록 outer join.
query = ( query = (
select(_ITEMS.c.price) select(_ITEMS.c.price, _ITEMS.c.purchase_price, _COMPANIES.c.settings)
.select_from(_ITEMS.outerjoin(_COMPANIES, _ITEMS.c.company_id == _COMPANIES.c.company_id))
.where(_ITEMS.c.item_id == item_id, _ITEMS.c.deleted == False) # noqa: E712 .where(_ITEMS.c.item_id == item_id, _ITEMS.c.deleted == False) # noqa: E712
.limit(1) .limit(1)
) )
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_item_price failed.", raise_error=False) err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_item_baseline failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or not rows[0]: if err_type != ErrorType.SUCCESS or not rows:
return err_type, 0 return err_type, _fallback
# 컬럼이 2개 이상이면 execute 가 행 리스트를 준다(1개일 때만 스칼라 리스트).
price, purchase_price, settings = rows[0]
settings = settings if isinstance(settings, dict) else {}
labels = settings.get("labels") or {}
field = _resolve_baseline(settings)
value = purchase_price if field == _BASELINE_PURCHASE else price
return ErrorType.SUCCESS, (int(value or 0), _SUPPLIER_PRICE_LABEL, labels)
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, _fallback
async def get_card_count(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, Optional[int]]:
try:
query = (
select(_QUOTATION_SETTINGS.c.card_count)
.select_from(
_SESSIONS.join(
_QUOTATION_SETTINGS,
_QUOTATION_SETTINGS.c.qt_setting_id == _SESSIONS.c.qt_setting_id,
)
)
.where(_SESSIONS.c.session_id == session_id,
_SESSIONS.c.deleted == False, # noqa: E712
_QUOTATION_SETTINGS.c.deleted == False) # noqa: E712
.limit(1)
)
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_card_count failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or rows[0] is None:
return err_type, None
return ErrorType.SUCCESS, int(rows[0]) return ErrorType.SUCCESS, int(rows[0])
except Exception as ex: except Exception as ex:
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, 0 return ErrorType.DB_RUN_FAILED, None
async def get_item_internet_lowest(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, int]: async def get_item_lowest_price(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, int]:
"""상품의 인터넷최저가(partner.items.internet_lowest_price). 미수집이면 0."""
try: try:
query = ( query = (
select(_ITEMS.c.internet_lowest_price) select(_ITEMS.c.internet_lowest_price)
.where(_ITEMS.c.item_id == item_id, _ITEMS.c.deleted == False) # noqa: E712 .where(_ITEMS.c.item_id == item_id, _ITEMS.c.deleted == False) # noqa: E712
.limit(1) .limit(1)
) )
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_item_internet_lowest failed.", raise_error=False) err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_item_lowest_price failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or not rows[0]: if err_type != ErrorType.SUCCESS or not rows or not rows[0]:
return err_type, 0 return err_type, 0
return ErrorType.SUCCESS, int(rows[0]) return ErrorType.SUCCESS, int(rows[0])
@ -171,51 +240,6 @@ class NegoContextCRUD(INegoContextCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, 0 return ErrorType.DB_RUN_FAILED, 0
async def get_quotation_period(self, cdb: AsyncSession, quotation_id) -> Tuple[ErrorType, Optional[tuple]]:
"""견적 협상 기간 (start_time, end_time). 자율 에이전트의 마감 잔여율 특징용."""
try:
query = (
select(_QUOTATIONS.c.start_time, _QUOTATIONS.c.end_time)
.where(_QUOTATIONS.c.qt_id == quotation_id, _QUOTATIONS.c.deleted == False) # noqa: E712
.limit(1)
)
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_quotation_period failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows:
return err_type, None
return ErrorType.SUCCESS, (rows[0][0], rows[0][1])
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None
async def get_supplier_history(self, cdb: AsyncSession, company_id: str, supplier_id,
exclude_session_id) -> Tuple[ErrorType, tuple]:
"""이 협력사와의 과거 협상 이력 집계 → (횟수, 성사율, 평균 타결가/목표가).
소스 = learning.experience_logs 종료행(done=True) negotiation.sessions
(agent 직접 기록한 결과라 카드/자율 모드 무관하게 쌓인다). 이력 없으면 (0, None, None).
"""
try:
query = (
select(_EXP_LOGS.c.settled_price, _SESSIONS.c.target_price)
.select_from(_EXP_LOGS.join(_SESSIONS, _SESSIONS.c.session_id == _EXP_LOGS.c.session_id))
.where(_EXP_LOGS.c.done == True, # noqa: E712
_EXP_LOGS.c.company_id == company_id,
_SESSIONS.c.supplier_id == supplier_id,
_EXP_LOGS.c.session_id != exclude_session_id,
_SESSIONS.c.deleted == False) # noqa: E712
)
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_supplier_history failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows:
return err_type, (0, None, None)
n = len(rows)
settled = [(int(sp), int(tp)) for sp, tp in rows if sp and tp]
success = len([1 for sp, tp in rows if sp]) / n
avg_ratio = (sum(sp / tp for sp, tp in settled) / len(settled)) if settled else None
return ErrorType.SUCCESS, (n, success, avg_ratio)
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, (0, None, None)
async def get_supplier_total_revenue(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, float]: async def get_supplier_total_revenue(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, float]:
try: try:
query = ( query = (
@ -231,6 +255,36 @@ class NegoContextCRUD(INegoContextCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, 0.0 return ErrorType.DB_RUN_FAILED, 0.0
async def get_item_name(self, cdb: AsyncSession, item_id) -> Tuple[ErrorType, Optional[str]]:
try:
query = (
select(_ITEMS.c.name)
.where(_ITEMS.c.item_id == item_id, _ITEMS.c.deleted == False) # noqa: E712
.limit(1)
)
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_item_name failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or not rows[0]:
return err_type, None
return ErrorType.SUCCESS, str(rows[0])
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None
async def get_supplier_name(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, Optional[str]]:
try:
query = (
select(_SUPPLIERS.c.name)
.where(_SUPPLIERS.c.supplier_id == supplier_id, _SUPPLIERS.c.deleted == False) # noqa: E712
.limit(1)
)
err_type, rows = await DB_SESSION_MNG.execute(cdb, query, "get_supplier_name failed.", raise_error=False)
if err_type != ErrorType.SUCCESS or not rows or not rows[0]:
return err_type, None
return ErrorType.SUCCESS, str(rows[0])
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None
async def get_supply_type(self, cdb: AsyncSession, supplier_id, item_id) -> Tuple[ErrorType, Optional[int]]: async def get_supply_type(self, cdb: AsyncSession, supplier_id, item_id) -> Tuple[ErrorType, Optional[int]]:
try: try:
query = ( query = (
@ -276,7 +330,7 @@ class NegoContextCRUD(INegoContextCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, 0 return ErrorType.DB_RUN_FAILED, 0
async def get_quotation_card_numbers(self, cdb: AsyncSession, quotation_id) -> Tuple[ErrorType, tuple[list[str], list[str]]]: async def get_quotation_card_numbers(self, cdb: AsyncSession, quotation_id) -> Tuple[ErrorType, tuple[list[tuple], list[tuple]]]:
try: try:
version_q = ( version_q = (
select(_QUOTATIONS.c.version_id) select(_QUOTATIONS.c.version_id)
@ -291,7 +345,7 @@ class NegoContextCRUD(INegoContextCRUD):
version_id = rows[0] version_id = rows[0]
nego_q = ( nego_q = (
select(_NEGO_CARDS.c.number) select(_NEGO_CARDS.c.number, _NEGO_CARDS.c.script, _NEGO_CARDS.c.tactic)
.select_from( .select_from(
_VERSION_NEGO_CARDS.join( _VERSION_NEGO_CARDS.join(
_NEGO_CARDS, _NEGO_CARDS,
@ -310,7 +364,7 @@ class NegoContextCRUD(INegoContextCRUD):
return n_err, ([], []) return n_err, ([], [])
wild_q = ( wild_q = (
select(_WILD_CARDS.c.number) select(_WILD_CARDS.c.number, _WILD_CARDS.c.script, _WILD_CARDS.c.tactic)
.select_from( .select_from(
_VERSION_WILD_CARDS.join( _VERSION_WILD_CARDS.join(
_WILD_CARDS, _WILD_CARDS,
@ -321,6 +375,8 @@ class NegoContextCRUD(INegoContextCRUD):
_VERSION_WILD_CARDS.c.version_id == version_id, _VERSION_WILD_CARDS.c.version_id == version_id,
_VERSION_WILD_CARDS.c.deleted == False, # noqa: E712 _VERSION_WILD_CARDS.c.deleted == False, # noqa: E712
_WILD_CARDS.c.deleted == False, # noqa: E712 _WILD_CARDS.c.deleted == False, # noqa: E712
# 협상 적용 여부(카드 설정 '적용 대기(수동)') — 꺼진 카드는 견적에 담겨 있어도 발동 금지
_WILD_CARDS.c.available == True, # noqa: E712
) )
.order_by(_VERSION_WILD_CARDS.c.created_at) .order_by(_VERSION_WILD_CARDS.c.created_at)
) )
@ -329,8 +385,8 @@ class NegoContextCRUD(INegoContextCRUD):
return w_err, ([], []) return w_err, ([], [])
return ErrorType.SUCCESS, ( return ErrorType.SUCCESS, (
[str(r) for r in n_rows if r is not None], [(str(r[0]), r[1], r[2]) for r in n_rows if r[0] is not None],
[str(r) for r in w_rows if r is not None], [(str(r[0]), r[1], r[2]) for r in w_rows if r[0] is not None],
) )
except Exception as ex: except Exception as ex:
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)

View File

@ -10,62 +10,28 @@ import re
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from negotiation.cards.domain.tactics import (
OFFER_VARIABLES, Offer, available, compute_offer_detail, is_played, mark_played, playable,
record_offer, settle_ceiling, spec_from_context,
)
from negotiation.chat.service.script_repository import ScriptRepository from negotiation.chat.service.script_repository import ScriptRepository
MAX_ROUNDS = 3 MAX_ROUNDS = 3 # config 미주입 시 폴백 (규칙 정본은 tenant config negotiation.max_counter_rounds)
# 멘트에 찍히는 파생 가격 — 값이 다른 값에서 계산돼 나오는 것들(원값 인용 target/anchor 는 제외).
_DERIVED_PRICE_VARIABLES = ("target_mid_price", "middle_price")
_PRICE_MODES = ("price",) _PRICE_MODES = ("price",)
_CHOICE_MODES = ("yes_no", "confirm", "delivery_type") _CHOICE_MODES = ("yes_no", "confirm", "delivery_type")
# ---- 완전 자율 모드 (AUTONOMY_MODE, autonomy_store) --------------------------------
# 가격협상 판정 룰(check_price_match/wildcard_entry/iteration_limit)과 카드 선택을
# 정책 행동(수락/역제안/압박/결렬)으로 대체할 때 쓰는 스텝들. autonomy_decider 미주입이면 도달 불가.
_AUTONOMY_TURN_CAP = 12 # 엔지니어링 타임아웃(무한 세션 방지) — 협상 룰이 아니다
_AUTONOMY_PRESS_SCRIPTS = {
1: "동일 품목에 대해 복수 공급처의 견적이 함께 검토되고 있습니다. 현재 제시가로는 우선순위 확보가 어려운 상황입니다. 경쟁력 있는 가격으로 다시 제안해 주시겠어요?",
2: "제안하신 조건의 취지는 충분히 이해했습니다. 저희도 최대한 맞춰보려 합니다. 조금만 더 조정해 주시면 내부 설득이 가능할 것 같습니다. 다시 제안해 주시겠어요?",
3: "내부 산정 기준과 현재 제시가 사이에 아직 차이가 있습니다. 기준에 부합하는 수준으로 재검토하여 다시 제안해 주시기를 부탁드립니다.",
4: "귀사를 장기적으로 함께할 파트너로 검토하고 있습니다. 이번 협상이 원만히 마무리되면 후속 거래 확대도 논의하고 싶습니다. 서로 만족할 수 있는 가격으로 다시 제안해 주시겠어요?",
}
_AUTONOMY_STEPS = {
"자율_역제안": {
"script": "제안해 주신 **{input_price}원**, 내부 검토를 마쳤습니다. **{autonomy_offer}원**이라면 즉시 수락하고 우선협상 대상으로 확정하겠습니다. 수락하시겠습니까?",
"next_input_mode": "yes_no",
"input_options": ["", "아니오"],
"next_step": {"": "협상완료", "아니오": "가격협상_재입력"},
"type": "text",
"chat_end": False,
},
# 최종 통보(WC-03 의 자율 버전): 정책이 직전과 같은 금액을 다시 부르는 순간(단조 봉투상
# 더 올릴 수 없음 = 탄약 소진) 발동. 거절하면 협상을 정리한다 — 어정쩡한 반복 대신 명확한 마무리.
"자율_최종제안": {
"script": "지금까지 협의에 성실히 임해 주셔서 감사합니다. **{autonomy_offer}원**은 저희가 제시할 수 있는 마지막 제안입니다. 수락해 주시면 즉시 우선협상 대상으로 확정되며, 어려우시다면 이번 협상은 여기서 마무리하겠습니다.",
"next_input_mode": "yes_no",
"input_options": ["", "아니오"],
"next_step": {"": "협상완료", "아니오": "협상실패"},
"type": "text",
"chat_end": False,
},
**{
f"자율_압박_{s}": {
"script": t,
"next_input_mode": "price",
"input_options": [],
"next_step": {"default": "가격협상_확인"},
"type": "text",
"chat_end": False,
}
for s, t in _AUTONOMY_PRESS_SCRIPTS.items()
},
}
# 최종 타결/결렬 스텝. 재협상=협상완료(우선협상 타결), 재견적=결과제출(투찰확정). 둘 다 협상실패=결렬. # 최종 타결/결렬 스텝. 재협상=협상완료(우선협상 타결), 재견적=결과제출(투찰확정). 둘 다 협상실패=결렬.
# 이 스텝들은 chat_end=False(뒤에 협상종료가 옴)라, outcome 을 컨텍스트에 적재했다가 # 이 스텝들은 chat_end=False(뒤에 협상종료가 옴)라, outcome 을 컨텍스트에 적재했다가
# 실제 종료(chat_end=협상종료) 시점에 확정 보고한다 → backend 가 chat_end 에서 DONE/REJECTED 를 옳게 가른다. # 실제 종료(chat_end=협상종료) 시점에 확정 보고한다 → backend 가 chat_end 에서 DONE/REJECTED 를 옳게 가른다.
_SUCCESS_STEPS = ("협상완료", "결과제출") _SUCCESS_STEPS = ("협상완료", "결과제출")
_FAILURE_STEPS = ("협상실패",) _FAILURE_STEPS = ("협상실패",)
# 카운터 제안(pending_counter_price) 수락으로 인정하는 선택 입력.
_ACCEPT_INPUTS = ("", "수락")
# 프론트는 표시용 문자열로 가격을 보낸다(예: "530,000원"). 천단위 콤마·통화기호("원")·공백 등 # 프론트는 표시용 문자열로 가격을 보낸다(예: "530,000원"). 천단위 콤마·통화기호("원")·공백 등
# 숫자 외 문자를 제거하고 파싱한다. (콤마만 지우면 "원" 때문에 float() 가 실패해 가격 입력이 # 숫자 외 문자를 제거하고 파싱한다. (콤마만 지우면 "원" 때문에 float() 가 실패해 가격 입력이
# 영영 저장되지 않고 같은 step 에 머무는 버그가 났었다.) # 영영 저장되지 않고 같은 step 에 머무는 버그가 났었다.)
@ -83,6 +49,39 @@ def _parse_price(user_input: Any) -> Optional[float]:
return price if price > 0 else None return price if price > 0 else None
# 협상 스크립트가 쓰는 용어 토큰: {label_*} = 회사 용어(없으면 기본값).
# 값은 negodata 용어 카탈로그(LABEL_CATALOG)의 base 와 같아야 화면·멘트 표기가 갈리지 않는다.
_SCRIPT_LABELS = {
"label_supplier": ("supplier", "협력사"),
"label_target_price": ("target_price", "목표가"),
"label_delivery_type": ("item.delivery_type", "배송 형태"),
"label_delivery_type_1": ("delivery_type.1", "협력사배송"),
"label_delivery_type_2": ("delivery_type.2", "지정택배배송"),
"label_delivery_type_3": ("delivery_type.3", "픽업배송"),
"label_product": ("item.name", "상품명"),
}
# 협상 기준가 호칭 — 공급사 화면 고정 용어. 회사 용어 사전(labels)을 타지 않는다(그건 관리자 화면 전용).
# 실제 값은 loader 가 컨텍스트에 박제하고, DB 컨텍스트가 없는 데모/직접호출 경로만 이 폴백을 쓴다.
_SUPPLIER_PRICE_LABEL = "공급가"
# 조사 자동 보정: 토큰 뒤에 조사가 붙는 자리는 {label_supplier_를} 처럼 대표형을 적는다.
# 회사가 바꾼 용어의 받침을 예측할 수 없어 스크립트에 조사를 고정할 수 없다("협력사를"/"공급업체을").
_JOSA = {"": ("", ""), "": ("", ""), "": ("", ""), "": ("", ""),
"": ("", ""), "": ("", ""), "": ("", ""), "": ("", "")}
def _has_batchim(word: str) -> bool:
last = word[-1] if word else ""
return "" <= last <= "" and (ord(last) - 0xAC00) % 28 != 0
def _josa(word: str, form: str) -> str:
"""단어 + 받침에 맞는 조사. form 은 대표형(''·''·''·'')."""
pair = _JOSA.get(form)
if not pair:
return word
return word + (pair[0] if _has_batchim(word) else pair[1])
@dataclass @dataclass
class ChatSession: class ChatSession:
session_id: str session_id: str
@ -114,11 +113,11 @@ class ChatEngine:
def __init__(self, scripts_repo: ScriptRepository, rq_type: str = "재협상"): def __init__(self, scripts_repo: ScriptRepository, rq_type: str = "재협상"):
self.repo = scripts_repo self.repo = scripts_repo
self.rq_type = rq_type self.rq_type = rq_type
# 자율 스텝은 병합만 해둔다(repo 캐시 오염 방지 위해 새 dict) — decider 미주입 시 도달 불가. self.scripts = scripts_repo.load_scripts(rq_type)
self.scripts = {**scripts_repo.load_scripts(rq_type), **_AUTONOMY_STEPS}
self.step_map = scripts_repo.client_step_mapping() self.step_map = scripts_repo.client_step_mapping()
# 완전 자율 모드: ChatService 가 AutonomyStore 정책을 주입하면 가격협상 판정 룰을 대체한다. # 결정 스택 규칙층(Phase 1): 와일드카드 진입 임계·라운드 상한을 테넌트 config 에서 읽는다.
self.autonomy_decider = None # Callable[[dict], autonomy_actions.Action] # (하드코딩 1.02/1.05/3 을 데이터화 — 고객사별로 튜닝 가능, 코드 수정 불필요)
self.rules = scripts_repo.config.negotiation
# ---- public -------------------------------------------------------- # ---- public --------------------------------------------------------
def start(self, session: ChatSession) -> StepView: def start(self, session: ChatSession) -> StepView:
@ -136,25 +135,29 @@ class ChatEngine:
if price is None: if price is None:
return self._error(session, "가격을 숫자로 입력해 주세요.") return self._error(session, "가격을 숫자로 입력해 주세요.")
session.context["input_price"] = price session.context["input_price"] = price
# 새 가격 제시 = 직전 카운터 제안 거절 확정 → 대기 중 카운터·그 재료 폐기.
session.context.pop("pending_counter_price", None)
session.context.pop("pending_offer", None)
session.context["prev_partner_price"] = price
# 협력사 첫 제시가 — 가격 수용률(첫 제시가 대비 양보율) 동적 계산의 기준값. # 협력사 첫 제시가 — 가격 수용률(첫 제시가 대비 양보율) 동적 계산의 기준값.
session.context.setdefault("first_offer_price", price) session.context.setdefault("first_offer_price", price)
session.context["round"] = session.context.get("round", 0) + 1 session.context["round"] = session.context.get("round", 0) + 1
nxt = self._default_next(node) nxt = self._default_next(node)
elif mode in _CHOICE_MODES: elif mode in _CHOICE_MODES:
# 와일드카드 1% 인하 제안을 수락("예")하면 합의가를 제안가(offer_1pct)로 확정한다.
# (멘트에만 쓰이던 offer_1pct 가 input_price 에 반영되지 않아, 요약/입찰가가
# 직전 제시가로 잡히던 버그 수정 — 수락 시 실제 합의가는 인하가다.)
if session.step == "wild_card_1pct" and user_input == "" and session.context.get("offer_1pct"):
session.context["input_price"] = float(session.context["offer_1pct"])
# 자율 역제안/최종제안 수락("예") → 합의가는 에이전트 제안가다 (wild_card_1pct 와 동일 원리).
if session.step in ("자율_역제안", "자율_최종제안") and user_input == "" \
and session.context.get("autonomy_offer"):
session.context["input_price"] = float(session.context["autonomy_offer"])
nxt = self._choice_next(node, user_input, session) nxt = self._choice_next(node, user_input, session)
else: else:
nxt = self._default_next(node) nxt = self._default_next(node)
nxt = self._resolve(nxt, session) nxt = self._resolve(nxt, session)
# 카운터 수락 일반 메커니즘: 카드/와일드카드가 제시한 카운터가(pending_counter_price)를
# 협력사가 수락("예"/"수락")한 채 성공 스텝으로 전이하면 합의가 = 카운터가.
# (구 offer_1pct 특수 분기의 일반화. 거절인데 성공 스텝으로 가는 경로 — 1% 거절 시
# 원 제시가 수락 종결 — 는 카운터를 버리고 기존 input_price 로 타결한다.)
if mode in _CHOICE_MODES and nxt in _SUCCESS_STEPS:
pending = session.context.pop("pending_counter_price", None)
session.context.pop("pending_offer", None)
if pending and user_input in _ACCEPT_INPUTS:
session.context["input_price"] = float(pending)
return self._render(session, nxt) return self._render(session, nxt)
# ---- transition ---------------------------------------------------- # ---- transition ----------------------------------------------------
@ -182,20 +185,12 @@ class ChatEngine:
return nxt return nxt
def _eval_conditions(self, conds: List[dict], session: ChatSession) -> Optional[str]: def _eval_conditions(self, conds: List[dict], session: ChatSession) -> Optional[str]:
"""KT 구매자 관점 조건 평가. """KT 구매자 관점 조건 평가 (임계값은 config negotiation.* — 규칙층 데이터화).
- 협력사 제시가 anchor 우선협상(협상완료). - 협력사 제시가 anchor 우선협상(협상완료).
- anchor 살짝 초과( anchor*1.05) + 와일드카드 미사용 와일드카드로 인하 압박. - anchor 살짝 초과( anchor×wildcard_entry_ratio) + 와일드카드 미사용 와일드카드로 인하 압박.
- 설정 카드(action_space) 모두 소진 협상실패. - 설정 카드(action_space) 모두 소진 협상실패.
- 가격협상(카드 1 플레이 재제안). - 가격협상(카드 1 플레이 재제안).
완전 자율 모드(autonomy_decider 주입)에서는 전체를 정책 행동으로 대체한다.
""" """
# 가격협상 판정 지점(check_price_match 포함 조건 리스트)에서만 자율 정책이 개입한다.
if self.autonomy_decider is not None and any(
c.get("condition") == "check_price_match" for c in conds):
nxt = self._autonomy_next(session)
if nxt is not None:
return nxt # 정책 실패(예외) 시에만 아래 룰로 폴백
ctx = session.context ctx = session.context
price = ctx.get("input_price", 0) price = ctx.get("input_price", 0)
anchor = ctx.get("anchor_price", 0) anchor = ctx.get("anchor_price", 0)
@ -210,91 +205,99 @@ class ChatEngine:
and anchor > 0 and anchor > 0
and anchor < price and anchor < price
and ( and (
price <= anchor * 1.02 price <= anchor * self.rules.wildcard_1pct_ratio
or (bool(ctx.get("allow_selected_wildcards", True)) and price <= anchor * 1.05) or (bool(ctx.get("allow_selected_wildcards", True))
and price <= anchor * self.rules.wildcard_entry_ratio)
) )
) )
# 구간에 들어와도 실제로 낼 카드가 없으면(전부 종결 전용·사용됨·유효조건 미달) 이 조건은
# 불충족으로 두고 다음 조건(우선협상·소진 판정)을 평가한다 — 여기서 매칭돼 버리면
# 카드 소진 판정이 영영 돌지 않아, 빈 덱에서 쓴 카드를 또 꺼내는 무한 협상이 된다.
if ok:
probe = ChatSession(
session_id=session.session_id, tenant_id=session.tenant_id,
company_id=session.company_id, context=dict(ctx),
)
ok = self._pick_wildcard(probe) != "가격협상"
elif cond == "check_is_supplier_type_c": elif cond == "check_is_supplier_type_c":
ok = False # 공급사 유형 미보유 (PoC 단순화) ok = False # 공급사 유형 미보유 (PoC 단순화)
elif cond == "check_price_match": # = 우선협상: 제시가가 앵커가 이하 elif cond == "check_price_match": # = 우선협상: 제시가가 앵커가 이하
ok = anchor > 0 and price <= anchor ok = anchor > 0 and price <= anchor
elif cond == "check_iteration_limit": # 협상 라운드 상한 또는 카드 소진 elif cond == "check_iteration_limit": # 협상 라운드 상한 또는 카드 소진 → 종결 국면
# round 는 매 가격입력마다 증가(기존가격제시=1). 그 이후 카운터제안이 MAX_ROUNDS 회를 # round 는 매 가격입력마다 증가(기존가격제시=1). 카운터제안이 상한을 넘거나 카드가
# 넘으면 종료한다. 카드선택(RL)이 실패(state ValueError)해도 used_action_ids 가 안 늘어 # 소진되면 곧장 실패가 아니라 **종결 국면**으로 처리한다(실제 MD 협상 방식):
# 카드 소진 조건만으로는 종료되지 않으므로, 라운드 상한을 독립적으로 둬 무한 가격입력을 막는다. # ① 아직 종결 전술을 안 썼으면 → 가격협상으로 보내되 force_closing 마킹
# (선행 chat_server 의 `iteration >= 3` 와 동일한 안전장치.) # (ChatService 가 종결 전술 — 중간값 절충/최후통첩 — 을 강제 발동)
# ② 종결 전술까지 소진(closing_played)이면 → 최종 제시가 ≤ target 은 타결,
# 초과는 결렬(협상실패) — "목표가 초과 타결 금지" 가드레일과 정합.
counter_rounds = max(0, ctx.get("round", 0) - 1) counter_rounds = max(0, ctx.get("round", 0) - 1)
ok = counter_rounds >= MAX_ROUNDS or (cards_total > 0 and cards_used >= cards_total) # 담은 협상카드 중 지금 낼 수 있는 게 하나도 없으면(사용됨·발동조건 미달 — 예:
# 시장가 인용 카드인데 최저가 결측) 장수와 무관하게 소진으로 본다 — 안 그러면
# 선택 마스크가 전부 막힌 채 폴백이 부적합 카드를 억지로 꺼낸다(토큰 노출).
selected = ctx.get("selected_nego_card_numbers") or []
none_playable = bool(selected) and not any(
not is_played(ctx, n) and playable(spec_from_context(ctx, n), ctx)
for n in selected
)
exhausted = (
counter_rounds >= self.rules.max_counter_rounds
or (cards_total > 0 and cards_used >= cards_total)
or none_playable
)
if exhausted:
# 타결선은 목표가가 아니라 타결 상한가(견적 생성 시 박제) — 목표가를 넘어도
# 상한 이내면 타결한다.
ceiling = settle_ceiling(ctx)
if not ctx.get("closing_played"):
ctx["force_closing"] = True
return "가격협상"
return "협상완료" if (ceiling > 0 and price <= ceiling) else c.get("next")
ok = False
elif cond == "default": elif cond == "default":
ok = True ok = True
if ok: if ok:
return c.get("next") return c.get("next")
return "가격협상" return "가격협상"
def _autonomy_next(self, session: ChatSession) -> Optional[str]:
"""완전 자율: 정책 행동 → 스텝. 수락/역제안 금액/압박 화법/결렬 타이밍 전부 정책이 결정.
유일한 강제 종료는 상한(_AUTONOMY_TURN_CAP) 무한 세션 방지용 엔지니어링 타임아웃.
정책 호출이 실패하면 None 반환해 기존 평가로 폴백한다(서비스 연속성).
"""
ctx = session.context
if ctx.get("round", 0) > _AUTONOMY_TURN_CAP:
# 턴 상한도 최종제안 보장(봉투 ⑥)을 우회하지 않는다 — 어떤 경로로 끝나든
# "끝내기 전에 한 번 더"(제품 결정)를 거친다. 최종 거절 후에만 협상실패.
if not ctx.get("autonomy_final_asked"):
ctx["autonomy_final_asked"] = True
ctx["autonomy_offer"] = int(ctx.get("target_price", 0))
return "자율_최종제안"
return "협상실패"
try:
act = self.autonomy_decider(ctx)
except Exception: # 정책 오류 → 룰 폴백 (호출부에서 로깅)
return None
session.context["autonomy_action"] = f"{act.kind}:{act.strategy}:{act.counter_q}"
span = max(ctx.get("target_price", 0) - ctx.get("anchor_price", 0), 1.0)
# 탄약소진(같은 금액 재호출) 판정은 '마지막 역제안' 기준 — autonomy_last(마지막 행동)는
# 사이에 낀 설득이 덮어써 판정이 리셋된다 (chat_service 가 counter 마다 별도 보존).
last = ctx.get("autonomy_last_counter") or {}
if act.kind == "accept":
return "협상완료"
if act.kind == "walk":
# 결렬 전 마지막 제안 1회 보장 — "끝내기 전에 한 번 더 물어보고 종료" (제품 결정).
# 최종제안을 이미 거쳤으면(autonomy_final_asked) 그대로 종료한다.
if not ctx.get("autonomy_final_asked"):
ctx["autonomy_final_asked"] = True
# 최종제안 금액 = 목표가. 마지막 기회에 직전 역제안 금액을 반복하면 승인 범위의
# 여지(목표가까지)를 남긴 채 결렬된다 — 최종에는 우리가 수락 가능한 최대치를 부른다.
ctx["autonomy_offer"] = int(ctx.get("target_price", 0))
return "자율_최종제안"
return "협상실패"
if act.kind == "counter":
ctx["autonomy_offer"] = int(round(ctx.get("anchor_price", 0) + act.counter_q * span))
# 직전과 같은 금액을 다시 부름 = 단조 봉투상 더 올릴 수 없음(탄약 소진) → 최종 통보로 전환.
if last.get("kind") == "counter" and act.counter_q <= float(last.get("q", -9)) + 1e-9:
ctx["autonomy_final_asked"] = True
ctx["autonomy_offer"] = int(ctx.get("target_price", 0))
return "자율_최종제안"
return "자율_역제안"
return f"자율_압박_{act.strategy or 3}"
def _pick_wildcard(self, session: ChatSession) -> str: def _pick_wildcard(self, session: ChatSession) -> str:
"""앵커가에 아주 근접(≤ anchor*1.02)한 구간에서만 1% 인하 요청(wild_card_1pct)으로 """앵커가에 아주 근접(≤ anchor×wildcard_1pct_ratio)한 구간에서만 1% 인하 요청(wild_card_1pct)으로
앵커가 이하로 유도한다. 구간은 일반 가격협상(카드 플레이)으로 돌린다. 앵커가 이하로 유도한다. 구간은 견적에서 선택한 와일드카드의 전술로 카운터하고,
카드가 없으면 일반 가격협상(카드 플레이)으로 돌린다.
과거 여기서 반환하던 '재원부족'(wild_card_budget) 하드코딩 카드는 제거했다
견적에서 실제 선택한 와일드카드(중간값 절충·목표가 선제안 ) 매핑되지 않은
'와일드카드를 하나라도 골랐으면' 조건만으로 발동해, 선택하지도 않은 재원부족 멘트가
노출되는 오작동이 있었다.
""" """
ctx = session.context ctx = session.context
price = ctx.get("input_price", 0) price = ctx.get("input_price", 0)
anchor = ctx.get("anchor_price", 0) anchor = ctx.get("anchor_price", 0)
if anchor > 0 and price <= anchor * 1.02: target = ctx.get("target_price", 0)
# 와일드카드는 실제로 노출할 때만 '사용됨'으로 마킹한다 — 가격협상으로 돌아가는 if anchor > 0 and price <= anchor * self.rules.wildcard_1pct_ratio:
# 경우에도 마킹하면 이후 라운드에서 정당한 1% 카드까지 억제된다. offer_1pct = int(price * 0.99 / 10 + 0.5) * 10 # 1% 인하가 — 10원 반올림(앵커·카운터와 통일)
ctx["wildcard_used"] = True # 제안가 공통 유효조건(≤목표가 · <제시가 · 직전 당사 제안 이상=역행 금지)은 시스템 1% 카드에도
ctx["offer_1pct"] = int(round(price * 0.99)) # 1% 인하가 # 동일하게 건다. 기본 앵커 밴드에선 수학적으로 항상 통과하지만 극단 데이터를 방어한다.
return "wild_card_1pct" prev_customer = ctx.get("prev_customer_price") or 0
if 0 < offer_1pct < price and (target <= 0 or offer_1pct <= target) and offer_1pct >= prev_customer:
# 와일드카드는 실제로 노출할 때만 '사용됨'으로 마킹한다 — 가격협상으로 돌아가는
# 경우에도 마킹하면 이후 라운드에서 정당한 1% 카드까지 억제된다.
ctx["wildcard_used"] = True
ctx["offer_1pct"] = offer_1pct
record_offer(ctx, Offer(price=offer_1pct, variable="offer_1pct",
prev_customer=int(prev_customer or anchor), prev_partner=int(price)))
return "wild_card_1pct"
# 1.02 초과 ~ entry(1.05) 구간: 견적에서 선택한 와일드카드의 전술로 카운터 제시.
# (구현 전에는 이 구간이 일반 가격협상으로 회귀해 선택형 WC 가 영영 발동하지 않던 갭.)
if anchor > 0 and price <= anchor * self.rules.wildcard_entry_ratio:
for number in (ctx.get("selected_wild_card_numbers") or []):
number = str(number)
spec = spec_from_context(ctx, number)
# 종결 전용 카드(최종 통보·중간값 절충)는 여기서 안 꺼낸다 — 종결 국면의 마지막 한 방으로 예약.
# 이미 쓴 카드도 제외(같은 멘트 반복 방지).
if not available(spec, ctx) or is_played(ctx, number):
continue
offer = compute_offer_detail(spec, ctx)
if offer is not None:
ctx["wildcard_used"] = True
record_offer(ctx, offer)
ctx["active_wild_card_number"] = number
mark_played(ctx, number)
return "wild_card_dynamic"
return "가격협상" return "가격협상"
# ---- render -------------------------------------------------------- # ---- render --------------------------------------------------------
@ -304,41 +307,109 @@ class ChatEngine:
out = {} out = {}
if "input_price" in ctx: if "input_price" in ctx:
out["input_price"] = int(ctx["input_price"]) out["input_price"] = int(ctx["input_price"])
# 목표가/앵커가: 엔진 내부 파일 스크립트는 {target}/{anchor}, negodata 카드 에디터는
# {target_price}/{anchor_price}(variables.ts) 를 쓴다 — 양쪽 이름 모두 채워 치환 누락 방지.
if "target_price" in ctx: if "target_price" in ctx:
out["target"] = int(ctx["target_price"]) out["target"] = out["target_price"] = int(ctx["target_price"])
# DB 카드 정본(negodata 편집, card.nego_cards.script)은 {target_price} 변수명을 쓴다
# — 파일 스크립트의 {target}과 별개로 둘 다 지원(미치환 토큰 노출 방지).
out["target_price"] = int(ctx["target_price"])
if "anchor_price" in ctx: if "anchor_price" in ctx:
out["anchor"] = int(ctx["anchor_price"]) # anchoring_price = DB 시드 기본 카드/sessions 컬럼 표기, anchor_price = 카드 에디터 표기.
out["anchoring_price"] = int(ctx["anchor_price"]) # DB 카드 정본 변수명(NGC-007 등) out["anchor"] = out["anchor_price"] = out["anchoring_price"] = int(ctx["anchor_price"])
# 용어 토큰 — 회사 용어 사전(labels)이 있으면 그 단어, 없으면 카탈로그 기본값.
# 조사가 붙는 자리를 위해 {label_supplier_를} 같은 파생 키도 함께 만든다.
labels = ctx.get("labels") or {}
for token, (label_key, fallback) in _SCRIPT_LABELS.items():
word = labels.get(label_key) or fallback
out[token] = word
for form in ("", "", "", ""):
out[f"{token}_{form}"] = _josa(word, form)
# 기준가 호칭은 회사 용어가 아니라 공급사 관점 고정 — loader 박제값(없으면 '공급가').
price_word = str(ctx.get("item_price_label") or _SUPPLIER_PRICE_LABEL)
out["label_item_price"] = price_word
for form in ("", "", "", ""):
out[f"label_item_price_{form}"] = _josa(price_word, form)
# 카드 에디터 카탈로그의 협력사명/상품명(partner_name·product_name) 치환.
if ctx.get("partner_name"):
out["partner_name"] = str(ctx["partner_name"])
if ctx.get("product_name"):
out["product_name"] = str(ctx["product_name"])
if "offer_1pct" in ctx: if "offer_1pct" in ctx:
out["offer_1pct"] = int(ctx["offer_1pct"]) out["offer_1pct"] = int(ctx["offer_1pct"])
if "autonomy_offer" in ctx: # 인터넷 최저가: LPS 대표값(items.internet_lowest_price). 카드는 {internet_lowest_price},
out["autonomy_offer"] = int(ctx["autonomy_offer"]) # 라벨 매핑(variable_mapping.json)은 internet_min_price 를 쓰므로 target/anchor 처럼 양쪽 이름 모두 채운다.
# 인터넷 최저가(NGC-008): 수집값이 컨텍스트에 없으면 앵커가로 폴백 — 원형 토큰 노출 방지. # 미수집(0/없음)이면 키를 만들지 않는다 — 원형 유지 → 허위 시장가 인용 방지(NGC-008 은 값 있을 때만 유효).
# TODO: partner.item_internet_lowest_prices 최신 성공 수집값을 context loader 로 연결. ilp = ctx.get("internet_lowest_price") or 0
if ctx.get("internet_lowest_price"): if ilp > 0:
out["internet_lowest_price"] = int(ctx["internet_lowest_price"]) out["internet_lowest_price"] = out["internet_min_price"] = int(ilp)
elif "anchor_price" in ctx: # 전술 카운터 변수(카드 시드 멘트의 가격 변수) — tactics.OFFER_VARIABLES 산식과 동일 정의.
out["internet_lowest_price"] = int(ctx["anchor_price"]) anchor = ctx.get("anchor_price") or 0
# 고객사 교환·요구 조건(NGC-009/010): 런타임 소스 미구현 — 중립 문구 폴백. target = ctx.get("target_price") or 0
# TODO: 견적/카드 편집 단계에서 입력받아 컨텍스트로 전달. # 표시 기준값 — 제안이 확정된 턴이면 그 계산에 쓴 재료(pending_offer)를 쓴다.
out["customer_condition"] = ctx.get("customer_condition") or "상호 협의된 조건" # prev_customer_price 는 확정 즉시 새 제안가로 갱신되므로, 그대로 읽으면 멘트가
# 인하율 = (기존 공급가 - 제시가) / 기존 공급가 * 100. 기존가 없으면 미표시(0.0). # "당사 제안과 귀사 제안의 절반이 당사 제안" 같은 자기모순이 된다.
pending_offer = ctx.get("pending_offer") or {}
prev_customer = int(pending_offer.get("prev_customer") or ctx.get("prev_customer_price") or anchor or 0)
partner_price = int(pending_offer.get("prev_partner") or ctx.get("prev_partner_price") or ctx.get("input_price") or 0)
offer_price = int(pending_offer.get("price") or ctx.get("pending_counter_price") or 0)
offer_variable = pending_offer.get("variable") or ""
if prev_customer:
out["prev_customer_price"] = prev_customer
if partner_price:
out["prev_partner_price"] = partner_price
if offer_price:
out["counter_price"] = offer_price
# 파생 가격(절충가·중간가) — 제안가로 확정된 변수는 그 금액을 그대로 쓴다(멘트에 보이는 금액과
# 수락 시 타결가는 항상 같아야 한다). 나머지는 참고 인용이므로 tactics 산식으로 채운다.
# 산식을 여기 복사해 두면 갱신 시점 차이로 표시가와 제안가가 갈라지므로 정의를 호출만 한다.
# 어느 변수가 제안가인지 모르는 진행 중 세션(구버전 기록)은 종전대로 전부 제안가로 고정한다.
for name in _DERIVED_PRICE_VARIABLES:
if offer_price and (name == offer_variable or not offer_variable):
out[name] = offer_price
continue
value = OFFER_VARIABLES[name](target, anchor, partner_price, prev_customer)
if value:
out[name] = int(value / 10 + 0.5) * 10 # 10원 반올림 — compute_offer 와 동일
# 인하율 = (협상 기준가 - 제시가) / 기준가 * 100. 기준가 없으면 미표시(0.0).
# 제시가가 기준가보다 높으면(인상 제시) 음수가 나오는데, "-1.3% 인하된 금액" 같은
# 모순 표현이 되므로 discount_rate 는 0 미만 금지하고, 인상/동일/인하를 구분한
# 문구는 discount_phrase 로 별도 제공한다(가격협상_확인 멘트가 사용).
# 기준가 호칭은 공급사 화면 고정 용어('공급가') — loader 가 박제한 값.
base = ctx.get("item_price") or 0 base = ctx.get("item_price") or 0
label = ctx.get("item_price_label") or _SUPPLIER_PRICE_LABEL
if base > 1 and "input_price" in ctx: if base > 1 and "input_price" in ctx:
out["discount_rate"] = f"{((base - ctx['input_price']) / base) * 100:.1f}" rate = ((base - ctx["input_price"]) / base) * 100
out["discount_rate"] = f"{max(0.0, rate):.1f}"
if rate >= 0.05:
out["discount_phrase"] = f"기존 {label} 대비 약 **{rate:.1f}%** 인하된 금액입니다. "
elif rate <= -0.05:
out["discount_phrase"] = (
f"기존 {label}(**{int(base)}원**)보다 약 **{abs(rate):.1f}%** 높은 금액입니다. ")
else:
out["discount_phrase"] = f"기존 {_josa(label, '')} 동일한 수준의 금액입니다. "
else: else:
out["discount_rate"] = "0.0" out["discount_rate"] = "0.0"
out["discount_phrase"] = ""
return out return out
def _vars(self, session: ChatSession) -> Dict[str, Any]: def _vars(self, session: ChatSession) -> Dict[str, Any]:
return self.vars_for(session) return self.vars_for(session)
def render_step(self, session: ChatSession, step_key: str) -> StepView:
"""지정 스텝으로 전이·렌더 (공개) — ChatService 가 카드 카운터 제시 시
가격협상 가격협상_카운터로 스텝을 전환할 사용한다."""
return self._render(session, step_key)
def _render(self, session: ChatSession, step_key: Optional[str]) -> StepView: def _render(self, session: ChatSession, step_key: Optional[str]) -> StepView:
if not step_key or step_key not in self.scripts: if not step_key or step_key not in self.scripts:
return self._error(session, f"다음 단계를 찾을 수 없습니다: {step_key}") return self._error(session, f"다음 단계를 찾을 수 없습니다: {step_key}")
# 가드레일(최후 방어선): 구매자 대리는 타결 상한가를 넘겨 타결하지 않는다.
# 상한 = 견적 생성 시 박제한 done_ceiling_price(목표가×(1+타결상한율)), 미박제면 목표가.
# 목표가를 조금 넘어도 상한 이내면 타결이 정상이므로 여기서 뒤집지 않는다.
# 상한까지 넘은 경우만 결렬로 강제 전환한다.
if step_key in _SUCCESS_STEPS and self.rq_type == "재협상":
ctx = session.context
ceiling = settle_ceiling(ctx)
if ceiling > 0 and ctx.get("input_price", 0) > ceiling:
step_key = "협상실패"
node = self.scripts[step_key] node = self.scripts[step_key]
session.step = step_key session.step = step_key
chat_end = bool(node.get("chat_end")) chat_end = bool(node.get("chat_end"))
@ -351,11 +422,14 @@ class ChatEngine:
elif step_key in _FAILURE_STEPS: elif step_key in _FAILURE_STEPS:
session.context["final_outcome"] = "failure" session.context["final_outcome"] = "failure"
outcome = session.context.get("final_outcome") if chat_end else None outcome = session.context.get("final_outcome") if chat_end else None
# 선택지도 스크립트와 같은 변수 치환을 태운다 — 배송형태 보기가 회사 용어({label_delivery_type_1} 등)라
# 치환을 건너뛰면 사용자에게 토큰 원문이 그대로 보인다.
step_vars = self._vars(session)
return StepView( return StepView(
step=step_key, step=step_key,
script=self.repo.format_script(node.get("script", ""), self._vars(session)), script=self.repo.format_script(node.get("script", ""), step_vars),
input_mode=node.get("next_input_mode", "null"), input_mode=node.get("next_input_mode", "null"),
input_options=node.get("input_options", []), input_options=[self.repo.format_script(o, step_vars) for o in node.get("input_options", [])],
chat_end=bool(node.get("chat_end")), chat_end=bool(node.get("chat_end")),
client_step=self.step_map.get(step_key, step_key), client_step=self.step_map.get(step_key, step_key),
needs_card_selection=(step_key == "가격협상"), needs_card_selection=(step_key == "가격협상"),
@ -364,9 +438,13 @@ class ChatEngine:
) )
def _error(self, session: ChatSession, msg: str) -> StepView: def _error(self, session: ChatSession, msg: str) -> StepView:
# 에러 재렌더도 정상 렌더와 같은 변수 치환을 태운다 — 여기만 raw 로 두면
# 가격 오입력 시 옵션 버튼에 {label_*} 토큰이 그대로 노출된다.
node = self.scripts.get(session.step, {}) node = self.scripts.get(session.step, {})
step_vars = self._vars(session)
return StepView( return StepView(
step=session.step, script=node.get("script", ""), step=session.step, script=self.repo.format_script(node.get("script", ""), step_vars),
input_mode=node.get("next_input_mode", "null"), input_options=node.get("input_options", []), input_mode=node.get("next_input_mode", "null"),
input_options=[self.repo.format_script(o, step_vars) for o in node.get("input_options", [])],
chat_end=session.ended, client_step=self.step_map.get(session.step, session.step), error=msg, chat_end=session.ended, client_step=self.step_map.get(session.step, session.step), error=msg,
) )

View File

@ -0,0 +1,149 @@
"""InputInterpreter — 협력사 자유 발화를 대화 단계 기대 입력으로 구조화 (Phase 3 이해층).
원칙: "숫자와 결정은 결정론이, 말은 LLM 이" (ScriptNaturalizer 동일 철학)
- LLM 역할은 의도 분류(choice|price|unknown) 가격 '표현의 위치' 찾기까지다.
가격 숫자 계산은 LLM 출력이 아니라 결정론 한국어 가격 파서(parse_korean_price) 수행한다.
- 검증: choice 허용 선택지 목록에 철자 그대로 있어야 하고, price_text 사용자 원문의
부분 문자열이어야 한다(환각 차단). 하나라도 어긋나면 None 호출부가 원문 그대로 폴백
(기존 엔진의 재질문 흐름 유지 협상은 절대 멈추지 않는다).
게이트: tenant llm.enabled + 전역 자격증명(ScriptNaturalizer 동일). 미설정이면 무동작
기존 버튼/정형 입력 경로는 그대로 두고, 자유 텍스트일 때만 해석을 시도한다.
"""
import asyncio
import json
import re
from dataclasses import dataclass
from typing import Callable, List, Optional
from common.logger import LOG
from negotiation.profiling.config import LlmCredentials
# 정형 가격 입력(버튼/필드) — LLM 없이 기존 결정론 경로로 처리 가능한 형태.
SIMPLE_PRICE_RE = re.compile(r"\s*[\d,]+(\.\d+)?\s*원?\s*")
# 한국어 단위 (큰 단위 → 작은 단위 순서로 등장한다고 가정: "1만 2천 500원")
_KOREAN_UNITS = {"": 100_000_000, "": 10_000, "": 1_000, "": 100}
_PRICE_TOKEN_RE = re.compile(r"[\d.억만천백]+")
def parse_korean_price(text: str) -> Optional[float]:
"""가격 표현 문자열 → 숫자 (결정론). "10,500원"→10500, "1만 500원"→10500, "만원"→10000,
"1.5만"15000, "3만2천원"32000. 해석 불가/0 이하 None.
"""
t = (text or "").replace(",", "").replace(" ", "").replace("", "").strip()
if not t:
return None
if re.fullmatch(r"\d+(\.\d+)?", t):
v = float(t)
return v if v > 0 else None
if not re.fullmatch(r"[\d.억만천백]+", t):
return None # 단위·숫자 외 문자 포함 → 해석 불가(안전 폴백)
total, num = 0.0, ""
for ch in t:
if ch.isdigit() or ch == ".":
num += ch
else: # 단위 문자
try:
n = float(num) if num else 1.0 # "만원" = 1만
except ValueError:
return None
total += n * _KOREAN_UNITS[ch]
num = ""
if num:
try:
total += float(num) # 잔여 숫자: "1만500" 의 500
except ValueError:
return None
return total if total > 0 else None
@dataclass
class InterpretedInput:
kind: str # "choice" | "price"
value: str # ChatEngine.advance 에 그대로 전달할 문자열 ("예" / "10500")
source: str # 판단 근거(선택지 원문 또는 가격 표현 원문) — 로깅/투명성용
_SYSTEM = """너는 B2B 구매 협상 챗봇의 입력 해석기다. 협력사(사용자)의 자유 발화를 현재 대화 단계가 기대하는 입력으로 구조화한다.
규칙 (하나라도 어기면 출력은 폐기된다):
- 출력은 JSON 하나만: {"intent": "choice"|"price"|"unknown", "choice": "<선택지 철자 그대로>"|null, "price_text": "<원문 속 가격 표현 그대로>"|null}
- choice 발화의 '의미' 선택지 하나에 대응시켜, 선택지 문자열을 철자 그대로 복사한다.
) 선택지 ["","아니오"]: "네 접니다"/"맞습니다"/"진행해주세요"/"동의합니다" "",
"아닌데요"/"제가 아닙니다"/"어렵습니다"/"거절하겠습니다" "아니오"
- 사용자가 구체적 가격을 제시/역제안하면 intent=price. price_text 반드시 사용자 원문에 등장한
표현을 그대로 복사한다(: "10,500원", "1만 500원"). 숫자를 계산하거나 변형하지 마라.
- 발화가 어느 선택지의 의미인지 정말 판단할 없거나 주제와 무관할 때만 intent=unknown."""
def _default_llm_call(messages: List[dict]) -> dict:
"""기본 LLM 호출(동기) — 전역 자격증명으로 chat_json. 테스트에서 주입 대체 지점."""
from negotiation.profiling.infra.llm_adapter import chat_json
return chat_json(messages, temperature=0.0, max_tokens=200)
class InputInterpreter:
def __init__(self, llm_call: Optional[Callable[[List[dict]], dict]] = None,
timeout_seconds: float = 6.0):
self._llm_call = llm_call or _default_llm_call
self._timeout = timeout_seconds
@staticmethod
def available() -> bool:
"""전역 LLM 자격증명이 설정돼 있는가 (테넌트 enabled 게이트는 호출부 몫)."""
return LlmCredentials.from_config().is_configured()
async def interpret(self, user_input: str, *, input_mode: str,
input_options: Optional[List[str]] = None,
step_script: Optional[str] = None) -> Optional[InterpretedInput]:
"""자유 발화 → 기대 입력. 검증 불통과/실패/타임아웃 시 None(호출부 원문 폴백).
step_script: 직전 질문(맥락) "네 접니다" 같은 발화는 질문 없이는 의도 판단이
애매해 unknown 되므로, 무엇에 대한 답인지 함께 준다.
"""
text = (user_input or "").strip()
options = [str(o) for o in (input_options or [])]
if not text:
return None
payload = {
"현재 단계 기대 입력": "가격(숫자)" if input_mode == "price" else "선택지 중 하나",
"선택지": options,
"사용자 발화": text,
}
if step_script:
payload["직전 봇 질문"] = step_script[:300]
messages = [
{"role": "system", "content": _SYSTEM},
{"role": "user", "content": json.dumps(payload, ensure_ascii=False) + "\n\nJSON 으로만 답하라."},
]
try:
result = await asyncio.wait_for(asyncio.to_thread(self._llm_call, messages), self._timeout)
except Exception as ex: # 타임아웃 포함 — 원문 폴백
LOG.w(f"[InputInterpreter] LLM 호출 실패(원문 폴백): {ex}")
return None
if not isinstance(result, dict):
return None
intent = result.get("intent")
if intent == "choice":
choice = result.get("choice")
# 결정: 선택지 목록에 철자 그대로 있어야만 채택 (LLM 이 지어낸 분기 차단).
if isinstance(choice, str) and choice in options:
return InterpretedInput(kind="choice", value=choice, source=choice)
LOG.w(f"[InputInterpreter] 검증 실패: choice={choice!r}{options}")
return None
if intent == "price":
span = result.get("price_text")
# 환각 차단: 가격 표현은 사용자 원문의 부분 문자열이어야 한다.
if not isinstance(span, str) or not span.strip() or span.strip() not in text:
LOG.w(f"[InputInterpreter] 검증 실패: price_text={span!r} 가 원문에 없음")
return None
price = parse_korean_price(span.strip()) # 숫자 계산은 결정론 파서가
if price is None:
LOG.w(f"[InputInterpreter] 검증 실패: 가격 해석 불가 span={span!r}")
return None
return InterpretedInput(kind="price", value=str(int(price)), source=span.strip())
return None # unknown → 원문 폴백(엔진 재질문)

View File

@ -1,188 +0,0 @@
"""MentGenerator — 자율 협상 행동을 LLM 이 자연어 멘트로 표현 (v2: 행동은 RL, 문장은 LLM).
역할 분리(안전 설계):
- 무엇을 말할지(수락/역제안 금액/압박 전략/결렬) RL 정책이 결정 LLM 표현만 담당.
- 가드레일: 역제안 멘트에 제안 금액이 정확히 포함되지 않으면 폐기, 예외/미설정 None
호출부(ChatService) 기존 템플릿 멘트로 폴백한다. LLM 죽어도 협상은 계속된다.
설정: config.local.toml [OpenAIConfig] (Gemini OpenAI 호환 base_url 접속).
비활성화: AUTONOMY_LLM=0.
"""
import asyncio
import os
import re
from typing import Optional
from common.logger import LOG
from negotiation.profiling.config import LlmCredentials
_STRATEGY_TONE = {
1: "경쟁 압박형 — 복수 공급처와 비교 검토 중임을 암시하며 긴장감을 준다",
2: "수용 공감형 — 상대 제안의 취지에 공감하며 부드럽게 조정을 요청한다",
3: "기준 고수형 — 내부 산정 기준과 목표가를 근거로 원칙을 지킨다",
4: "협력 파트너형 — 장기 파트너십과 후속 거래 확대 가능성을 강조한다",
}
_SYSTEM = """너는 대기업 구매팀의 가격 협상 챗봇이다. 주어진 '전달 의도'를 자연스러운 한국어 협상 멘트로 바꿔 쓴다.
규칙 (위반 출력은 폐기된다):
- 1~3문장, 정중하되 간결하게. 출력은 멘트 텍스트만 (따옴표·설명 없이).
- 금액 숫자는 주어진 그대로 정확히 포함하고 단위는 '' 쓴다. 주어지지 않은 숫자·비율을 절대 만들지 않는다.
- 지정된 '화법' 전략 안에서만 말한다. 외의 협상 전술(물량·기간 약속, 조건 교환, 거래 연계,
독점 제안, 시장가·최저가 주장, 할인 약속 ) 지어내지 않는다.
- 회사의 정책·사실을 단정하지 않는다. 주어진 의도에 없는 정보는 말하지 않는다.
- 상대는 협력사(판매자)이고 우리는 구매자다."""
# 생성문 금지어 — 승인되지 않은 커밋/주장 계열. 걸리면 템플릿 폴백(협상은 계속).
_FORBIDDEN = ("보장", "물량", "독점", "무조건", "최저가", "시장 가격", "시장가", "계약 기간",
"법적", "위약", "%")
def _configured() -> bool:
if os.getenv("AUTONOMY_LLM", "1").lower() in ("0", "false", "no"):
return False
try:
return LlmCredentials.from_config().is_configured()
except Exception:
return False
def _digits(s) -> str:
return re.sub(r"[^\d]", "", str(s))
def _history_hints(ctx: dict) -> str:
"""대화 기억 힌트 — 무기억 생성이 '매번 같은 멘트'를 만들던 문제의 해법.
직전 우리 제안이 거절된 사실과 이번 제안과의 관계(양보/입장유지) 짚게 하고
직전 멘트를 보여주며 같은 문장 구조·표현의 반복을 금지한다."""
hints = []
prev = ctx.get("autonomy_prev")
if prev and prev.get("kind") == "counter":
anchor, target = float(ctx.get("anchor_price") or 0), float(ctx.get("target_price") or 0)
prev_offer = int(round(anchor + float(prev.get("q", 0.0)) * max(target - anchor, 1.0)))
cur_offer = int(ctx.get("autonomy_offer") or 0)
if cur_offer > prev_offer:
hints.append(f"참고: 직전 라운드에 우리가 {prev_offer:,}원을 제안했으나 거절당했고, "
f"이번에는 {cur_offer - prev_offer:,}원 더 양보한 제안이다. 이 진전을 자연스럽게 짚어라.")
elif cur_offer == prev_offer and cur_offer > 0:
hints.append(f"참고: 직전에 제안한 {prev_offer:,}원을 거절당했지만 같은 금액을 유지한다. "
f"입장이 확고함을 정중하게 전하라.")
elif prev_offer > 0:
hints.append(f"참고: 직전 제안({prev_offer:,}원)이 거절된 뒤의 재제안이다.")
last_ment = ctx.get("autonomy_last_ment")
if last_ment:
hints.append(f'직전 봇 멘트: "{last_ment}" — 이와 같은 문장 구조·표현을 반복하지 말고 다르게 써라.')
return " ".join(hints)
def _prompt_for(step: str, ctx: dict) -> Optional[str]:
price = int(ctx.get("input_price") or 0)
rnd = ctx.get("round", 1)
if step in ("자율_역제안", "자율_최종제안"):
offer = int(ctx.get("autonomy_offer") or 0)
if offer <= 0:
return None
strategy = int((ctx.get("autonomy_last") or {}).get("s") or 3)
tone = _STRATEGY_TONE.get(strategy, _STRATEGY_TONE[3])
final = ("이번이 우리가 제시할 수 있는 마지막 제안이며, 거절하시면 이번 협상은 종료됨을 "
"분명하되 정중하게 밝혀라. " if step == "자율_최종제안" else "")
return (f"상황: 협력사가 {price:,}원을 제시했다(협상 {rnd}라운드). "
f"전달 의도: 우리는 **{offer:,}원**이면 즉시 수락하고 우선협상 대상으로 확정할 수 있다 — "
f"이 핵심 의미는 유지하되 문장 표현은 자유롭게 새로 써라. {final}화법: {tone}. "
f"{_history_hints(ctx)} 마지막에 수락 여부를 물어라.")
if step.startswith("자율_압박_"):
strategy = int(step.rsplit("_", 1)[1])
tone = _STRATEGY_TONE.get(strategy, _STRATEGY_TONE[3])
# 주의: 목표가는 프롬프트에 넣지 않는다 — 압박 중 목표가 노출은 우리 상한을 까는 것
# (상대가 그 밑으로 내려올 이유가 사라진다). 숫자 커밋은 역제안/최종제안에서만.
base = (f"상황: 협력사가 {price:,}원을 제시했다(협상 {rnd}라운드). "
f"전달 의도: 어떤 금액도 언급하지 말고(내부 기준·목표가 숫자 금지), 제시가와 우리 기준의 "
f"거리가 있다는 취지로 가격 재제안을 요청한다. 화법: {tone}. "
f"{_history_hints(ctx)}")
# 시장가 근거 (구 NGC-008 의 자율 버전): 수집된 인터넷최저가가 실재하고 제시가가 그보다
# 높을 때만 사실 근거로 인용을 허용한다 — 미수집 품목에서 지어내는 주장은 가드가 차단.
if _market_evidence(ctx):
il = int(ctx["internet_lowest_price"])
base += (f" 참고 사실(인용 허용되는 유일한 금액): 동일 품목의 인터넷 최저가가 {il:,}원으로 "
f"확인된다. 현재 제시가가 이보다 높다는 점을 근거로 조정 여지를 정중히 짚어라.")
return base
return None
def _market_evidence(ctx: dict) -> bool:
"""시장가 근거 인용 가능 조건: 인터넷최저가 수집됨 + 제시가가 그보다 높음."""
il = int(ctx.get("internet_lowest_price") or 0)
return il > 0 and float(ctx.get("input_price") or 0) > il
def _allowed_amounts(ctx: dict) -> set:
"""멘트에 등장해도 되는 숫자 집합 — 우리가 프롬프트로 준 값들뿐. 이 밖의 금액 = 할루시네이션."""
# 목표가는 화이트리스트에 없다 — 압박 멘트가 목표가를 새면(상한 노출) 즉시 폐기된다.
# 역제안·최종제안의 제안가(autonomy_offer)가 목표가와 같은 경우만 그 값으로 허용된다.
anchor, target = float(ctx.get("anchor_price") or 0), float(ctx.get("target_price") or 0)
out = {int(ctx.get("input_price") or 0), int(ctx.get("autonomy_offer") or 0),
int(ctx.get("round") or 0)}
if _market_evidence(ctx):
out.add(int(ctx["internet_lowest_price"])) # 시장가 근거 인용 시 그 수치만 허용
prev = ctx.get("autonomy_prev")
if prev and prev.get("kind") == "counter":
prev_offer = int(round(anchor + float(prev.get("q", 0.0)) * max(target - anchor, 1.0)))
out |= {prev_offer, abs(int(ctx.get("autonomy_offer") or 0) - prev_offer)}
return {str(v) for v in out if v}
def _guard(step: str, ctx: dict, text: str) -> bool:
"""LLM 출력 검증(할루시네이션 차단) — 실패 시 템플릿 폴백.
길이/문장 완결 금지어(승인 커밋·주장) 숫자 화이트리스트: 멘트의 모든
3자리+ 숫자는 우리가 (제시가·제안가·목표가·직전제안가)이어야 한다 지어낸 금액 즉시 폐기.
역제안은 제안 금액 포함 필수."""
if not text or len(text) < 10 or len(text) > 600:
return False
if not text.rstrip().endswith(("다.", "요.", "요?", "까?", "니까?", ".", "?")):
return False # 문장 중간 잘림(thinking 토큰에 한도 소진 등) → 템플릿 폴백
forbidden = _FORBIDDEN
if _market_evidence(ctx):
# 시장가 근거가 정당한 턴에는 '최저가/시장가' 언급을 허용 (수치는 아래 화이트리스트가 검증).
forbidden = tuple(w for w in _FORBIDDEN if w not in ("최저가", "시장가", "시장 가격"))
if any(w in text for w in forbidden):
return False
allowed = _allowed_amounts(ctx)
for num in re.findall(r"\d{3,}", text.replace(",", "")):
if num not in allowed:
return False # 프롬프트에 없던 금액 생성 = 할루시네이션
if step in ("자율_역제안", "자율_최종제안"):
return _digits(ctx.get("autonomy_offer")) in _digits(text)
return True
async def generate(step: str, ctx: dict) -> Optional[str]:
"""자율 스텝 멘트 생성. 미설정/실패/검증불통과 → None (호출부 템플릿 유지)."""
if not _configured():
return None
prompt = _prompt_for(step, ctx)
if prompt is None:
return None
try:
from negotiation.profiling.infra.llm_adapter import chat_complete
# openai SDK 는 동기 — 이벤트루프 블로킹 방지 위해 스레드로 넘긴다.
# max_tokens 넉넉히 — Gemini 2.5 계열은 thinking 토큰이 한도에 포함돼 짧으면 본문이 잘린다.
# 시간 상한: backend→agent 타임아웃(10s)보다 확실히 짧아야 한다 — 초과 시 템플릿 폴백으로
# 협상은 즉시 계속된다("협상 응답 지연" 토스트 방지). LLM_TIMEOUT_S 로 조절.
text = await asyncio.wait_for(
asyncio.to_thread(
chat_complete,
[{"role": "system", "content": _SYSTEM}, {"role": "user", "content": prompt}],
None, False, 0.9, 2048, # temperature 0.9 — 표현 다양성 (의미는 프롬프트 가드)
),
timeout=float(os.getenv("LLM_TIMEOUT_S", "6")),
)
text = (text or "").strip().strip('"')
if _guard(step, ctx, text):
return text
LOG.w(f"[MentGenerator] 가드레일 불통과 → 템플릿 폴백 (step={step})")
return None
except Exception as ex:
LOG.e_no_callstack(f"[MentGenerator] LLM 실패 → 템플릿 폴백: {ex}")
return None

View File

@ -19,6 +19,7 @@ from typing import Optional
from common.database.db_session_manager import DB_SESSION_MNG from common.database.db_session_manager import DB_SESSION_MNG
from common.enums import DBType, DBWRType, ErrorType from common.enums import DBType, DBWRType, ErrorType
from common.logger import LOG from common.logger import LOG
from negotiation.cards.domain.tactics import build_card_spec
from negotiation.chat.infra.repository.nego_context_crud import INegoContextCRUD, NegoContextCRUD from negotiation.chat.infra.repository.nego_context_crud import INegoContextCRUD, NegoContextCRUD
from negotiation.qtable.domain.model.snapshot import PartnerType from negotiation.qtable.domain.model.snapshot import PartnerType
@ -38,29 +39,30 @@ class NegotiationDbContext:
rq_type: str # 재협상(1:1) | 재견적(1:N) — sessions.qt_type 으로 판별 rq_type: str # 재협상(1:1) | 재견적(1:N) — sessions.qt_type 으로 판별
target_price: int # 목표 매입가(원) — sessions.target_price target_price: int # 목표 매입가(원) — sessions.target_price
anchor_price: int # 앵커링가 — sessions.anchoring_price(생성 시 박제). 없으면 target(무할인 폴백) anchor_price: int # 앵커링가 — sessions.anchoring_price(생성 시 박제). 없으면 target(무할인 폴백)
item_price: int # 기존 공급가(품목 기준가, items.price) — 인하율 멘트용. 없으면 0 done_ceiling_price: int # 타결 상한가 — sessions.done_ceiling_price(생성 시 박제). 없으면 target
item_price: int # 협상 기준가(고객사가 관리하는 가격 — 공급가 또는 매입가) — 인하율 멘트용. 없으면 0
item_price_label: str # 협상 멘트에서 기준가를 부르는 말(회사 용어 설정 → 없으면 카탈로그 기본값)
labels: dict # 회사 용어 사전(companies.settings.labels) — 스크립트 {label_*} 토큰 치환용
internet_lowest_price: int # 인터넷 최저가(items.internet_lowest_price, LPS 대표값) — 카드 {internet_lowest_price} 치환용. 미수집이면 0
partner_name: Optional[str] # 협력사명(suppliers.name) — 카드 {partner_name} 치환용. 없으면 None
product_name: Optional[str] # 상품명(items.name) — 카드 {product_name} 치환용. 없으면 None
partner_type: PartnerType # 상품에 연결된 협력사 수(supplier_items 매핑, 없으면 세션 이력) → NONE/SINGLE/MULTIPLE partner_type: PartnerType # 상품에 연결된 협력사 수(supplier_items 매핑, 없으면 세션 이력) → NONE/SINGLE/MULTIPLE
revenue_amount: float # 매출액(원) — suppliers.total_revenue(KTC 미러). 없으면 0 revenue_amount: float # 매출액(원) — suppliers.total_revenue(KTC 미러). 없으면 0
distribution_code: Optional[str] # 유통 코드(A/B/C) — supplier_items.supply_type. 미지정 시 None distribution_code: Optional[str] # 유통 코드(A/B/C) — supplier_items.supply_type. 미지정 시 None
selected_nego_card_numbers: list[str] # 견적 생성 시 선택된 일반 협상카드 번호(card.nego_cards.number) selected_nego_card_numbers: list[str] # 견적 생성 시 선택된 일반 협상카드 번호(card.nego_cards.number)
selected_wild_card_numbers: list[str] # 견적 생성 시 선택된 와일드카드 번호(card.wild_cards.number) selected_wild_card_numbers: list[str] # 견적 생성 시 선택된 와일드카드 번호(card.wild_cards.number)
# ---- 자율 에이전트 v3 상태 특징 소스 (없으면 0/None — 특징은 중립 기본값으로 폴백) ---- card_count: Optional[int] # 협상카드 사용 횟수 상한(quotation_settings.card_count). None=상한 미적용
internet_lowest_price: int = 0 # items.internet_lowest_price (미수집 0) # 카드번호 → 전술 {offer_variable, min_round, closing}. 스크립트 파싱 + tactic JSONB 로 시작 시 1회 확정 —
deadline_end_ts: Optional[float] = None # 견적 마감(epoch 초) — quotations.end_time # 진행 중 협상은 카드 멘트가 도중에 바뀌어도 시작 시점 전술로 끝까지 간다(세션 컨텍스트에 박제).
deadline_total_s: Optional[float] = None # 협상 전체 기간(초) — endstart card_specs: dict
hist_n: int = 0 # 이 협력사와의 과거 협상 횟수
hist_success: Optional[float] = None # 과거 성사율 (이력 없으면 None)
hist_settle_ratio: Optional[float] = None # 과거 평균 타결가/목표가 (성사 이력 없으면 None)
class NegotiationContextLoader: class NegotiationContextLoader:
def __init__(self, crud: Optional[INegoContextCRUD] = None): def __init__(self, crud: Optional[INegoContextCRUD] = None):
self.crud: INegoContextCRUD = crud or NegoContextCRUD() self.crud: INegoContextCRUD = crud or NegoContextCRUD()
async def load(self, session_id: Optional[str], async def load(self, session_id: Optional[str]) -> Optional[NegotiationDbContext]:
company_id: Optional[str] = None) -> Optional[NegotiationDbContext]: """session_id 로 협상 컨텍스트 조회. 행이 없거나 조회 실패 시 None(호출부 기본값 폴백)."""
"""session_id 로 협상 컨텍스트 조회. 행이 없거나 조회 실패 시 None(호출부 기본값 폴백).
company_id 협력사 이력 집계(experience_logs 테넌트 스코프) 없으면 이력 특징 생략."""
if not session_id: if not session_id:
return None return None
try: try:
@ -72,8 +74,10 @@ class NegotiationContextLoader:
err, row = await self.crud.get_session_row(s, sid) err, row = await self.crud.get_session_row(s, sid)
if err != ErrorType.SUCCESS or row is None: if err != ErrorType.SUCCESS or row is None:
return None return None
qt_type, target_price, anchoring_price, item_id, quotation_id, supplier_id = row qt_type, target_price, anchoring_price, done_ceiling_price, item_id, quotation_id, supplier_id = row
target = int(target_price or 0) target = int(target_price or 0)
# 타결 상한가: 견적 생성 시 박제(목표가×(1+타결상한율)). 옛 세션은 NULL → 목표가로 폴백.
ceiling = int(done_ceiling_price or 0) or target
# 앵커링가: 세션 생성 시 박제된 값(anchoring_price)을 그대로 사용 — 협상 중 불변. # 앵커링가: 세션 생성 시 박제된 값(anchoring_price)을 그대로 사용 — 협상 중 불변.
# 박제가 없으면(데이터 이상) 무할인 폴백 anchor=target + WARN — 앵커링 v1.2 정책상 # 박제가 없으면(데이터 이상) 무할인 폴백 anchor=target + WARN — 앵커링 v1.2 정책상
@ -90,8 +94,16 @@ class NegotiationContextLoader:
# 매핑이 없거나 미지정이면 None → 호출부 기본값. # 매핑이 없거나 미지정이면 None → 호출부 기본값.
_, supplier_type = await self.crud.get_supply_type(s, supplier_id, item_id) _, supplier_type = await self.crud.get_supply_type(s, supplier_id, item_id)
# 기존 공급가(품목 기준가) — 없으면 0(인하율 멘트 미표시). # 협상 기준가 + 그 호칭 — 어느 컬럼을 쓸지는 고객사 설정(hidden_fields)이 정한다(crud).
_, item_price = await self.crud.get_item_price(s, item_id) # 없으면 0(인하율 멘트 미표시).
_, (item_price, item_price_label, labels) = await self.crud.get_item_baseline(s, item_id)
# 인터넷 최저가(LPS 수집 대표값) — 없으면 0(시장가 인용 카드는 값 있을 때만 치환).
_, internet_lowest_price = await self.crud.get_item_lowest_price(s, item_id)
# 카드 스크립트 치환용 이름 — 협력사명/상품명. 없으면 None(호출부 기본값 폴백).
_, partner_name = await self.crud.get_supplier_name(s, supplier_id)
_, product_name = await self.crud.get_item_name(s, item_id)
# 파트너사 유형: 상품에 연결된 협력사 수 — supplier_items 매핑(등록 기준) 우선. # 파트너사 유형: 상품에 연결된 협력사 수 — supplier_items 매핑(등록 기준) 우선.
# 매핑이 아직 없으면 협상 세션 이력 기준 폴백(더미보다 항상 낫다). 실패 시 SINGLE. # 매핑이 아직 없으면 협상 세션 이력 기준 폴백(더미보다 항상 낫다). 실패 시 SINGLE.
@ -104,39 +116,41 @@ class NegotiationContextLoader:
# 견적 생성 모달에서 고른 카드셋. 값이 없으면 운영 DB 기준으로 "선택 카드 없음"이다. # 견적 생성 모달에서 고른 카드셋. 값이 없으면 운영 DB 기준으로 "선택 카드 없음"이다.
# 데모/직접호출 경로(DB context 없음)만 ChatService 에서 기존 기본 카드셋으로 폴백한다. # 데모/직접호출 경로(DB context 없음)만 ChatService 에서 기존 기본 카드셋으로 폴백한다.
_, selected_cards = await self.crud.get_quotation_card_numbers(s, quotation_id) _, selected_cards = await self.crud.get_quotation_card_numbers(s, quotation_id)
selected_nego_cards, selected_wild_cards = selected_cards nego_rows, wild_rows = selected_cards
selected_nego_cards = [number for number, _script, _tactic in nego_rows]
selected_wild_cards = [number for number, _script, _tactic in wild_rows]
# 카드 전술 확정 — "스크립트에 꽂힌 변수가 곧 전술"(제안가 파싱) + tactic JSONB(min_round·closing).
card_specs = {}
for number, script, tactic in [*nego_rows, *wild_rows]:
spec = build_card_spec(script, tactic if isinstance(tactic, dict) else None)
card_specs[number] = {
"offer_variable": spec.offer_variable,
"min_round": spec.min_round,
"closing": spec.closing,
"requires": list(spec.requires), # 세션-의존 변수 결측 시 미발동(available)
}
# ---- 자율 에이전트 v3 특징 소스 (조회 실패는 전부 중립 폴백 — 협상은 계속돼야 한다) ---- # 협상카드 사용 횟수 상한(견적 설정). 없으면 None → 상한 미적용(선택 카드 수로만 캡).
_, internet_lowest = await self.crud.get_item_internet_lowest(s, item_id) _, card_count = await self.crud.get_card_count(s, sid)
_, period = await self.crud.get_quotation_period(s, quotation_id)
deadline_end_ts = deadline_total_s = None
if period and period[1] is not None:
end_ts = period[1].timestamp()
start_ts = period[0].timestamp() if period[0] is not None else None
total = (end_ts - start_ts) if start_ts else None
if total and total > 0:
deadline_end_ts, deadline_total_s = end_ts, total
hist_n, hist_success, hist_settle = 0, None, None
if company_id:
_, hist = await self.crud.get_supplier_history(s, company_id, supplier_id, sid)
hist_n, hist_success, hist_settle = hist
return NegotiationDbContext( return NegotiationDbContext(
rq_type="재협상" if int(qt_type) in _ONE_TO_ONE_QT_TYPES else "재견적", rq_type="재협상" if int(qt_type) in _ONE_TO_ONE_QT_TYPES else "재견적",
target_price=target, target_price=target,
anchor_price=anchor, anchor_price=anchor,
done_ceiling_price=ceiling,
item_price=item_price, item_price=item_price,
item_price_label=item_price_label,
labels=labels,
internet_lowest_price=internet_lowest_price,
partner_name=partner_name,
product_name=product_name,
partner_type=PartnerType.from_count(supplier_count), partner_type=PartnerType.from_count(supplier_count),
revenue_amount=revenue_amount, revenue_amount=revenue_amount,
distribution_code=_SUPPLIER_TYPE_TO_CODE.get(supplier_type) if supplier_type else None, distribution_code=_SUPPLIER_TYPE_TO_CODE.get(supplier_type) if supplier_type else None,
selected_nego_card_numbers=selected_nego_cards, selected_nego_card_numbers=selected_nego_cards,
selected_wild_card_numbers=selected_wild_cards, selected_wild_card_numbers=selected_wild_cards,
internet_lowest_price=internet_lowest, card_count=card_count,
deadline_end_ts=deadline_end_ts, card_specs=card_specs,
deadline_total_s=deadline_total_s,
hist_n=hist_n,
hist_success=hist_success,
hist_settle_ratio=hist_settle,
) )
try: try:

View File

@ -0,0 +1,144 @@
"""ScriptNaturalizer — 협상 카드 멘트를 LLM 으로 상황에 맞게 자연화 (Phase 2 표현층).
원칙: "숫자와 결정은 결정론이, 말은 LLM 이"
- 입력은 **치환 템플릿**({input_price} placeholder 유지 상태). LLM 숫자를 절대 만들지 않는다.
- 상황(라운드·가격구간·수용률) **정성 라벨**로만 전달(수치 미노출 숫자 환각 원천 차단).
- 검증 실패/타임아웃/미설정 None 호출부가 원본 템플릿 폴백(협상은 절대 멈춤).
검증(ScriptVerifier 철학의 플레인 텍스트판):
{placeholder} 집합이 원본과 정확히 동일(누락·추가 금지)
원본에 없던 숫자 등장 금지(가격 환각 차단)
비어있지 않고 길이 폭주 금지
게이트: tenant llm.enabled(기본 false) + 전역 LLM 자격증명(config.local.toml [OpenAIConfig]
또는 OPENAI_API_KEY env). 호출은 스레드로 넘겨 이벤트루프 비차단 + 타임아웃.
"""
import asyncio
import json
import re
from typing import Any, Callable, Dict, List, Optional
from common.logger import LOG
from negotiation.profiling.config import LlmCredentials
_PLACEHOLDER_RE = re.compile(r"\{(\w+)\}")
_DIGITS_RE = re.compile(r"\d+")
# 강조 마커(negodata Slate 편집 정본) — 고객사가 지정한 표시라 자연화가 보존해야 한다.
# 색 마커 {{강조|..}} {{안내|..}} 는 여는 토큰 개수로, 굵게/밑줄은 구분자 개수(짝수=쌍)로 센다.
_COLOR_OPEN_RE = re.compile(r"\{\{(강조|안내)\|")
# 카드 메타 코드 → 프롬프트 라벨 (init-data.sql CardTone/CardStrategyType 정의와 동일)
_TONE_LABEL = {1: "강경", 2: "정중", 3: "우호", 4: "중립", 5: "단호"}
_STRATEGY_LABEL = {1: "경쟁", 2: "수용", 3: "고수", 4: "협력", 5: "선점", 6: "종결"}
_SYSTEM = """너는 B2B 구매 협상 챗봇의 문장 작성기다. 주어진 협상 카드 멘트 '템플릿'을 협상 상황에 맞게 자연스럽게 다시 쓴다.
규칙 (하나라도 어기면 출력은 폐기된다):
- {변수명} 치환자는 철자 그대로 유지한다. 추가/삭제/변경 금지.
- 숫자를 직접 쓰지 마라. 가격·비율 모든 수치는 치환자로만 표현한다.
- 강조 마커(**굵게** __밑줄__ {{강조|...}} {{안내|...}}) **개수와 종류를 그대로 유지**한다.
고객사가 지정한 강조 표시이므로 삭제·추가·종류변경 금지. 감싼 문구는 자연스럽게 바꿔도 되지만
강조된 구절 수만큼 같은 마커로 반드시 다시 감싼다(: **굵게** 2개면 결과도 **** 2).
- 새로운 약속·할인 조건·법적 표현을 만들지 마라. 템플릿의 협상 의도(전술) 유지한다.
- 한국어 존댓말, 2~5문장, 채팅 말풍선에 어울리게 간결히.
- 출력은 JSON 하나만: {"script": "다시 쓴 멘트"}"""
def _default_llm_call(messages: List[dict]) -> dict:
"""기본 LLM 호출(동기) — 전역 자격증명으로 chat_json. 테스트에서 monkeypatch 지점."""
from negotiation.profiling.infra.llm_adapter import chat_json
return chat_json(messages, temperature=0.5, max_tokens=600)
class ScriptNaturalizer:
def __init__(self, llm_call: Optional[Callable[[List[dict]], dict]] = None,
timeout_seconds: float = 8.0):
self._llm_call = llm_call or _default_llm_call
self._timeout = timeout_seconds
@staticmethod
def available() -> bool:
"""전역 LLM 자격증명이 설정돼 있는가 (테넌트 enabled 게이트는 호출부 몫)."""
return LlmCredentials.from_config().is_configured()
async def naturalize(self, template: str, *, situation: Optional[Dict[str, Any]] = None,
tone: Optional[int] = None, strategy: Optional[int] = None) -> Optional[str]:
"""템플릿(치환 전)을 상황 맞춤 문장으로 재작성. 실패/검증불통과 시 None(호출부 폴백)."""
if not template or not template.strip():
return None
ctx = dict(situation or {})
if tone in _TONE_LABEL:
ctx[""] = _TONE_LABEL[tone]
if strategy in _STRATEGY_LABEL:
ctx["전략"] = _STRATEGY_LABEL[strategy]
messages = [
{"role": "system", "content": _SYSTEM},
{"role": "user", "content":
"템플릿:\n" + template +
"\n\n협상 상황:\n" + json.dumps(ctx, ensure_ascii=False) +
'\n\n규칙대로 다시 써서 {"script": "..."} 로만 출력.'},
]
try:
result = await asyncio.wait_for(asyncio.to_thread(self._llm_call, messages), self._timeout)
except Exception as ex: # 타임아웃 포함 — 폴백
LOG.w(f"[ScriptNaturalizer] LLM 호출 실패(폴백): {ex}")
return None
text = result.get("script") if isinstance(result, dict) else None
if not isinstance(text, str) or not text.strip():
return None
return text if self._verify(template, text) else None
# ---- 검증 ----------------------------------------------------------
@staticmethod
def _verify(template: str, rewritten: str) -> bool:
orig = set(_PLACEHOLDER_RE.findall(template))
new = set(_PLACEHOLDER_RE.findall(rewritten))
if orig != new:
LOG.w(f"[ScriptNaturalizer] 검증 실패: 치환자 불일치 (누락={orig - new}, 추가={new - orig})")
return False
# 원본에 없던 숫자 금지 — 가격/비율 환각 차단 (수치는 치환자로만).
orig_digits = set(_DIGITS_RE.findall(template))
new_digits = set(_DIGITS_RE.findall(rewritten)) - orig_digits
if new_digits:
LOG.w(f"[ScriptNaturalizer] 검증 실패: 새 숫자 등장 {new_digits}")
return False
if len(rewritten) > max(600, len(template) * 4):
LOG.w("[ScriptNaturalizer] 검증 실패: 길이 폭주")
return False
# 강조 마커 보존 — 고객사가 지정한 볼드/밑줄/색을 LLM 이 떨어뜨리면 폐기(원본 폴백).
# 굵게/밑줄: 구분자 총 개수가 같아야 짝(쌍)이 보존됨. 색: 여는 토큰 개수 동일.
if (template.count("**") != rewritten.count("**")
or template.count("__") != rewritten.count("__")
or len(_COLOR_OPEN_RE.findall(template)) != len(_COLOR_OPEN_RE.findall(rewritten))):
LOG.w("[ScriptNaturalizer] 검증 실패: 강조 마커 불일치(볼드/색 소실) → 원본 유지")
return False
return True
def build_situation(context: Dict[str, Any]) -> Dict[str, Any]:
"""세션 컨텍스트 → 정성 상황 라벨 (수치 미노출 — 숫자 환각 차단의 핵심).
가격구간: 제시가 vs 앵커/목표 관계, 라운드: 협상 진행 단계, 인하 진행: 협상 기준가 대비.
"""
out: Dict[str, Any] = {}
rnd = context.get("round") or 0
if rnd:
out["라운드"] = "첫 제안" if rnd <= 1 else ("초반 조율" if rnd == 2 else "막바지 조율")
price = context.get("input_price") or 0
anchor = context.get("anchor_price") or 0
target = context.get("target_price") or 0
if price and anchor and target:
if price <= anchor:
out["가격구간"] = "목표 범위 도달(마무리 국면)"
elif price <= target:
out["가격구간"] = "목표 범위 근접(조율 국면)"
else:
out["가격구간"] = "목표 상회(추가 인하 필요)"
base = context.get("item_price") or 0
if base and price:
rate = (base - price) / base
out["인하 진행"] = "아직 미미" if rate < 0.01 else ("일부 진행" if rate < 0.05 else "상당히 진행")
return out

View File

@ -38,6 +38,11 @@ class ScriptRepository:
# 카드 멘트 DB 소스(backoffice_db). file 모드면 미사용. # 카드 멘트 DB 소스(backoffice_db). file 모드면 미사용.
self._card_repo: ICardScriptRepository = card_repo or CardScriptDbRepository() self._card_repo: ICardScriptRepository = card_repo or CardScriptDbRepository()
@property
def config(self) -> TenantConfig:
"""테넌트 config 노출 — ChatEngine 이 협상 규칙(negotiation.*)을 읽는다."""
return self._config
# ---- 경로 해석 (_base 폴백) --------------------------------------- # ---- 경로 해석 (_base 폴백) ---------------------------------------
def _resource_path(self, filename: str) -> Optional[str]: def _resource_path(self, filename: str) -> Optional[str]:
scripts_dir = self._config.resources.scripts_dir scripts_dir = self._config.resources.scripts_dir
@ -89,25 +94,47 @@ class ScriptRepository:
return None return None
return self.format_script(text, variables) return self.format_script(text, variables)
async def resolve_card_template(self, action_id: int, card_id: Optional[str],
prefer_db: bool = False) -> tuple:
"""카드 멘트의 **치환 전 템플릿**과 메타를 해석 → (template, tone, strategy_type).
cards.source_type == 'backoffice_db' card.nego_cards(DB, tone/strategy 포함) 우선,
없거나 file 모드면 scripts_cards.json(파일, 메타 None) 폴백. 없으면 (None, None, None).
치환 템플릿을 그대로 주는 이유: LLM 표현층이 placeholder 유지한 재작성한
format_script 치환해야 숫자를 LLM 절대 만지지 않기 때문.
"""
if (prefer_db or self._config.cards.source_type == _CARD_SOURCE_DB) and card_id:
card = await self._fetch_card_db(card_id)
if card:
return card # (script, tone, strategy)
return self.card_scripts().get(str(action_id)), None, None # 파일 폴백(메타 없음)
async def resolve_card_script(self, action_id: int, card_id: Optional[str], async def resolve_card_script(self, action_id: int, card_id: Optional[str],
variables: Optional[Dict[str, Any]] = None, variables: Optional[Dict[str, Any]] = None,
prefer_db: bool = False) -> Optional[str]: prefer_db: bool = False) -> Optional[str]:
"""카드 멘트 해석. cards.source_type == 'backoffice_db' 면 card.nego_cards.script(DB)를 """카드 멘트 해석(템플릿 + 변수 치환). DB(정본) 우선 → 파일 폴백. 마커는 불투명 텍스트."""
우선 조회하고, 없거나 file 모드면 scripts_cards.json(파일) 폴백. 변수 치환 반환. template, _, _ = await self.resolve_card_template(action_id, card_id, prefer_db=prefer_db)
if not template:
return None
return self.format_script(template, variables)
DB 멘트는 백오피스(negodata) 편집한 정본이라 파일보다 우선한다. 마커(**굵게** ) async def resolve_wild_card_template(self, number: str) -> Optional[str]:
섞여 있어도 agent 불투명 텍스트로 취급 표현 렌더는 프론트 소유. """선택형 와일드카드(WC-*) 멘트 템플릿 — card.wild_cards(DB, negodata 편집 정본) 조회.
""" 없거나 DB 불가면 None(호출부가 스텝 기본 멘트 폴백)."""
if (prefer_db or self._config.cards.source_type == _CARD_SOURCE_DB) and card_id:
db_text = await self._fetch_card_script_db(card_id)
if db_text:
return self.format_script(db_text, variables)
return self.card_script(action_id, variables) # 파일 폴백
async def _fetch_card_script_db(self, card_id: str) -> Optional[str]:
async def _q(s): async def _q(s):
_, text = await self._card_repo.get_script_by_number(s, card_id) _, script = await self._card_repo.get_wild_card_by_number(s, number)
return text return script
try:
return await DB_SESSION_MNG.execute_lambda(DBType.MAIN.value, DBWRType.DB_READ.value, _q)
except Exception as ex:
LOG.e_no_callstack(f"[ScriptRepository] 와일드카드 멘트 DB 조회 실패 number={number}: {ex}")
return None
async def _fetch_card_db(self, card_id: str) -> Optional[tuple]:
async def _q(s):
_, card = await self._card_repo.get_card_by_number(s, card_id)
return card
try: try:
return await DB_SESSION_MNG.execute_lambda(DBType.MAIN.value, DBWRType.DB_READ.value, _q) return await DB_SESSION_MNG.execute_lambda(DBType.MAIN.value, DBWRType.DB_READ.value, _q)

View File

@ -1,85 +0,0 @@
"""완전 자율 협상 행동 공간 (numpy 전용 — 학습(tools)과 서빙(policy)이 공유).
카드 카탈로그 대신 행동의 '의미' 남긴다:
ACCEPT 현재 제시가로 타결
WALK 협상 결렬 선언
COUNTER(q, s) "C원이면 수락" 역제안. C = anchor + q×(targetanchor), s = 화법 전략
PRESS(s) 설득 압박 (카드의 일반화 전략 1경쟁/2수용/3고수/4협력)
특징 벡터(ACTION_DIM=8) = 유형 one-hot(3) + 가격 위치(1) + 전략 one-hot(4).
ScoreNet(상태 + 행동특징) 스칼라 점수로 후보 30개를 채점해 argmax 한다.
"""
from dataclasses import dataclass
import numpy as np
COUNTER_GRID = [-0.05, 0.0, 0.25, 0.5, 0.75, 1.0] # C = anchor + q×(targetanchor)
ACTION_DIM = 3 + 1 + 4 + 1 # 유형(3) + 위치(1) + 전략(4) + 컷폭(1: 현 제시가 대비 인하 요구율)
# 자율 전용 추가 상태 (v3):
# [0] 직전 역제안 존재(0/1) [1] 직전 역제안 위치 q ← 에피소드 기억(같은 숫자 반복 방지)
# [2] 마감 잔여율(남은시간/전체, 미상 0.5) ← 견적 마감(quotations.end_time)
# [3] 과거 협상 횟수 min(n,5)/5 [4] 과거 성사율(미상 0.5)
# [5] 과거 평균 타결수준 norm((타결가/목표가0.8)/0.4, 미상 0.5) ← 이 협력사와의 이력(experience_logs)
# [6] 인터넷최저가 갭 clip((최저가−앵커)/앵커/0.1, ±1, 미상 0) ← 숨은 하한가의 관측 가능한 힌트
# 특징은 학습 시뮬에도 동일하게 존재해야 한다(train_full_autonomy 가 대응물을 생성).
EXTRA_STATE_DIM = 7
def extra_state(last_kind: str = "", last_q: float = 0.0, deadline: float = 0.5,
hist_n: float = 0.0, hist_success: float = 0.5, hist_settle: float = 0.5,
internet_gap: float = 0.0) -> np.ndarray:
has_counter = 1.0 if last_kind == "counter" else 0.0
return np.array([
has_counter,
float(np.clip(last_q, -1.0, 1.0)) * has_counter,
float(np.clip(deadline, 0.0, 1.0)),
float(np.clip(hist_n, 0.0, 1.0)),
float(np.clip(hist_success, 0.0, 1.0)),
float(np.clip(hist_settle, 0.0, 1.0)),
float(np.clip(internet_gap, -1.0, 1.0)),
], dtype=np.float32)
def settle_norm(avg_settle_ratio: float) -> float:
"""평균 (타결가/목표가) → 0~1 정규화 (0.8→0, 1.0→0.5, 1.2→1)."""
return float(np.clip((avg_settle_ratio - 0.8) / 0.4, 0.0, 1.0))
def internet_gap_feat(internet_lowest: float, anchor: float) -> float:
"""인터넷최저가의 앵커 대비 갭 (±10% 스케일). 최저가 없으면 0을 쓴다."""
if not internet_lowest or anchor <= 0:
return 0.0
return float(np.clip((internet_lowest - anchor) / anchor / 0.1, -1.0, 1.0))
@dataclass(frozen=True)
class Action:
kind: str # accept | walk | counter | press
counter_q: float = 0.0 # counter 위치 (anchor~target 스팬 비율)
strategy: int = 0 # press/counter 의 화법 전략 (1~4, 0=없음)
def feat(self, price_pos: float, cut: float = 0.0) -> np.ndarray:
"""cut: 이 행동이 요구하는 인하폭 (현 제시가 대비, counter 만 >0) — 대형컷의 무례함을
정책이 지각하게 한다. 갭이 크면 역제안 대신 압박이 낫다는 배우는 근거 특징."""
t = {"accept": [1, 0, 0], "walk": [0, 1, 0]}.get(self.kind, [0, 0, 1])
pos = price_pos if self.kind == "accept" else self.counter_q
s = np.zeros(4, dtype=np.float32)
if self.strategy:
s[self.strategy - 1] = 1.0
return np.concatenate([np.array(t, dtype=np.float32),
np.array([float(np.clip(pos, -1.0, 2.0)),
], dtype=np.float32), s,
np.array([float(np.clip(cut, 0.0, 1.0))], dtype=np.float32)])
def candidate_actions():
"""전 행동 후보: 수락 1 + 결렬 1 + 역제안 6×전략4 + 압박 4 = 30."""
out = [Action("accept"), Action("walk")]
out += [Action("counter", q, s) for q in COUNTER_GRID for s in (1, 2, 3, 4)]
out += [Action("press", 0.0, s) for s in (1, 2, 3, 4)]
return out
ACTIONS = candidate_actions()

View File

@ -41,6 +41,9 @@ class PolicyContext:
action_space_size: int action_space_size: int
episode: EpisodeState episode: EpisodeState
available_mask: Optional[np.ndarray] = None # None 이면 used_action_ids 로 산출 available_mask: Optional[np.ndarray] = None # None 이면 used_action_ids 로 산출
# 의도층 prior(Phase 1): 갑이 견적에서 고른 카드 순서 등 사전 선호. 방문수로 감쇠되어
# 콜드 스타트 선택만 편향하고 학습(Q)이 쌓이면 영향이 소멸한다 — Q-table 오염 없음.
prior_bonus: Optional[np.ndarray] = None
@dataclass @dataclass

View File

@ -1,132 +0,0 @@
"""FeatureDQNPolicy — action-as-feature DQN (Phase 2·3).
고정 슬롯 Q(s)[11] 대신 ScoreNet(상태벡터 + 카드임베딩) 스칼라 점수.
결정 가용 카드 풀을 순회 채점해 argmax 카드 추가/삭제/ 카드(zero-shot) 구조 변화 없음.
협력사 특징은 상태벡터에 포함(feature_builder) '협력사를 입력으로' 달성.
가변 행동 학습: replay 다음 상태의 '가용 카드 임베딩들' 함께 저장,
target = r + γ · max_{c'∈next_avail} Q(s', c') · (1-done)
"""
import math
import random
from collections import deque
from typing import Dict, List, Optional, Tuple
import numpy as np
import torch
import torch.nn as nn
class ScoreNet(nn.Module):
"""(상태 + 카드임베딩) → 스칼라 점수."""
def __init__(self, state_dim: int, card_dim: int, hidden: int = 128):
super().__init__()
self.net = nn.Sequential(
nn.Linear(state_dim + card_dim, hidden), nn.ReLU(),
nn.Linear(hidden, hidden), nn.ReLU(),
nn.Linear(hidden, 1),
)
def forward(self, x: torch.Tensor) -> torch.Tensor: # x: [B, state+card]
return self.net(x).squeeze(-1) # [B]
class FeatureDQNPolicy:
name = "feature_dqn"
def __init__(self, state_dim: int, card_dim: int, device: str = "cpu",
lr: float = 1e-3, gamma: float = 0.95, hidden: int = 128,
eps_start: float = 1.0, eps_end: float = 0.05, eps_decay: int = 6000,
buffer_size: int = 50_000, batch_size: int = 64, target_sync: int = 500):
self.device = device
self.gamma = gamma
self.batch_size = batch_size
self.target_sync = target_sync
self.q = ScoreNet(state_dim, card_dim, hidden).to(device)
self.tgt = ScoreNet(state_dim, card_dim, hidden).to(device)
self.tgt.load_state_dict(self.q.state_dict())
self.opt = torch.optim.Adam(self.q.parameters(), lr=lr)
self.buf: deque = deque(maxlen=buffer_size)
self.eps_start, self.eps_end, self.eps_decay = eps_start, eps_end, eps_decay
self.steps = 0
self.greedy = False # 평가 모드(탐색 끔)
# ---- 탐색 스케줄 ----------------------------------------------------
def eps(self) -> float:
if self.greedy:
return 0.0
return self.eps_end + (self.eps_start - self.eps_end) * math.exp(-self.steps / self.eps_decay)
# ---- 채점/선택 -------------------------------------------------------
def scores(self, state_feat: np.ndarray, card_embs: np.ndarray) -> np.ndarray:
"""가용 카드 K개 일괄 채점. card_embs: [K, card_dim] → [K]."""
k = card_embs.shape[0]
x = np.concatenate([np.repeat(state_feat[None, :], k, axis=0), card_embs], axis=1)
with torch.no_grad():
return self.q(torch.tensor(x, device=self.device)).cpu().numpy()
def select(self, state_feat: np.ndarray, card_embs: np.ndarray) -> Tuple[int, float, float]:
"""(선택 인덱스, propensity, 선택 점수). 인덱스는 card_embs 행 기준."""
k = card_embs.shape[0]
sc = self.scores(state_feat, card_embs)
e = self.eps()
if random.random() < e:
i = random.randrange(k)
prop = e / k
else:
i = int(sc.argmax())
prop = (1.0 - e) + e / k
return i, prop, float(sc[i])
# ---- 경험/학습 -------------------------------------------------------
def remember(self, state_feat: np.ndarray, card_emb: np.ndarray, reward: float,
next_state_feat: Optional[np.ndarray], next_card_embs: Optional[np.ndarray],
done: bool):
self.buf.append((state_feat, card_emb, reward, next_state_feat, next_card_embs, done))
def train_step(self) -> Optional[float]:
if len(self.buf) < self.batch_size:
return None
batch = random.sample(self.buf, self.batch_size)
# Q(s, a_chosen)
xs = np.stack([np.concatenate([s, c]) for s, c, *_ in batch])
q_sa = self.q(torch.tensor(xs, device=self.device))
# target = r + γ·max_{c'} Q_tgt(s', c') — 가변 후보라 후보 전체를 한 번에 forward 후 세그먼트 max
rewards = torch.tensor([b[2] for b in batch], device=self.device, dtype=torch.float32)
dones = torch.tensor([float(b[5]) for b in batch], device=self.device)
next_rows, owner = [], []
for bi, (_, _, _, s2, cands, done) in enumerate(batch):
if done or s2 is None or cands is None or len(cands) == 0:
continue
for c in cands:
next_rows.append(np.concatenate([s2, c]))
owner.append(bi)
q_next_max = torch.zeros(self.batch_size, device=self.device)
if next_rows:
with torch.no_grad():
q_all = self.tgt(torch.tensor(np.stack(next_rows), device=self.device))
owner_t = torch.tensor(owner, device=self.device)
q_next_max = q_next_max.index_reduce_(0, owner_t, q_all, "amax", include_self=False)
target = rewards + self.gamma * q_next_max * (1.0 - dones)
loss = nn.functional.smooth_l1_loss(q_sa, target)
self.opt.zero_grad()
loss.backward()
self.opt.step()
self.steps += 1
if self.steps % self.target_sync == 0:
self.tgt.load_state_dict(self.q.state_dict())
return float(loss)
# ---- 저장/로드 -------------------------------------------------------
def save(self, path: str):
torch.save(self.q.state_dict(), path)
def load(self, path: str):
sd = torch.load(path, map_location=self.device)
self.q.load_state_dict(sd)
self.tgt.load_state_dict(sd)

View File

@ -36,7 +36,8 @@ class UCBQTablePolicy(NegotiationPolicy):
avail = [a for a in range(ctx.action_space_size) if a not in used] avail = [a for a in range(ctx.action_space_size) if a not in used]
return avail or list(range(ctx.action_space_size)) # 다 썼으면 전체 허용 return avail or list(range(ctx.action_space_size)) # 다 썼으면 전체 허용
def _ucb_scores(self, state_index: int, available: List[int]) -> np.ndarray: def _ucb_scores(self, state_index: int, available: List[int],
prior: "np.ndarray | None" = None) -> np.ndarray:
q = self.qtable.row(state_index) q = self.qtable.row(state_index)
visits = self.qtable.visit_row(state_index) visits = self.qtable.visit_row(state_index)
total = self.qtable.state_visits(state_index) total = self.qtable.state_visits(state_index)
@ -45,11 +46,15 @@ class UCBQTablePolicy(NegotiationPolicy):
for a in available: for a in available:
bonus = self.c * math.sqrt(ln / (visits[a] + 1e-6)) bonus = self.c * math.sqrt(ln / (visits[a] + 1e-6))
scores[a] = q[a] + bonus scores[a] = q[a] + bonus
if prior is not None:
# 의도층 prior — 방문수 감쇠: 콜드 스타트 동점(전부 Q=0·bonus=0)일 때만 순서를
# 결정하고, 학습이 쌓이면 1/(1+visits) 로 사라진다.
scores[a] += float(prior[a]) / (1.0 + visits[a])
return scores return scores
def select(self, ctx: PolicyContext) -> ActionDecision: def select(self, ctx: PolicyContext) -> ActionDecision:
available = self._available(ctx) available = self._available(ctx)
scores = self._ucb_scores(ctx.state_index, available) scores = self._ucb_scores(ctx.state_index, available, prior=ctx.prior_bonus)
action_id = int(np.argmax(scores)) action_id = int(np.argmax(scores))
n = len(available) n = len(available)
# ε-greedy 근사 propensity (greedy 액션) # ε-greedy 근사 propensity (greedy 액션)

View File

@ -1,152 +0,0 @@
"""AutonomyStore — 완전 자율 협상 정책 서빙 (룰 대체, numpy 전용).
AUTONOMY_MODE=1 이면 가격협상 판정 (앵커 이하 타결 / 와일드카드 / 라운드 상한)
카드 선택을 전부 정책의 행동 결정으로 대체한다:
accept 협상완료 (제시가 타결) walk 협상실패
counter "C원이면 수락" 역제안 스텝 press 전략별 압박 멘트 스텝
행동의 유일한 유인은 보상 함수다. 남는 제한은 가지뿐이며 비즈니스 룰이 아니다:
- 역제안 후보 격자가 [anchor5%span, target] (행동 공간 정의)
- 세션 상한(엔지니어링 타임아웃, ChatEngine._AUTONOMY_TURN_CAP)
번들: artifacts/autonomy_serving.npz (tools/export_autonomy_serving.py).
불가(플래그 꺼짐/번들 없음) None 기존 엔진 그대로 (즉시 롤백 경로).
"""
import os
from typing import Optional
import numpy as np
from common.logger import LOG
from negotiation.policies.autonomy_actions import (
ACTIONS, Action, extra_state, internet_gap_feat, settle_norm)
from negotiation.qtable.domain.model.snapshot import NegotiationSnapshot
from negotiation.qtable.domain.service.feature_builder import (
build_state_features, build_tenant_features)
_HERE = os.path.dirname(os.path.abspath(__file__))
BUNDLE_PATH = os.path.join(_HERE, "..", "..", "artifacts", "autonomy_serving.npz")
class AutonomyPolicy:
"""세션 컨텍스트 → 상태특징 → 행동(greedy). ChatEngine 에 decider 로 주입된다."""
name = "full_autonomy"
def __init__(self, z, reward_cfg):
self._W = (z["W0"], z["b0"], z["W1"], z["b1"], z["W2"], z["b2"])
self._state_dim = int(z["state_dim"])
self._tenant_feat = build_tenant_features(reward_cfg)
@staticmethod
def _acceptance(ctx: dict) -> float:
base = ctx.get("item_price") or ctx.get("first_offer_price") or 0
cur = ctx.get("input_price") or 0
if base <= 0 or cur <= 0:
return 0.0
return max(0.0, (base - cur) / base)
def decide(self, ctx: dict) -> Action:
"""ChatSession.context → Action. 상태 구성은 ChatService._snapshot 과 동일 규칙."""
snap = NegotiationSnapshot(
revenue_amount=ctx["revenue_amount"], distribution_code=ctx["distribution_code"],
partner_count=ctx["partner_count"], acceptance_ratio=self._acceptance(ctx),
input_price=ctx.get("input_price", ctx["anchor_price"]), anchor_price=ctx["anchor_price"],
target_price=ctx["target_price"], round_number=ctx.get("round", 0),
)
# v3 추가 특징: 직전 역제안 기억 + 마감 잔여율 + 협력사 이력 + 인터넷최저가 갭.
# 소스가 없으면 전부 중립값(0.5/0) — 학습 시뮬의 '미상' 표현과 동일해야 한다.
# 역제안 기억은 autonomy_last_counter(역제안만 갱신) — autonomy_last(마지막 행동)를 쓰면
# 사이에 낀 설득이 기억을 지워 단조 봉투가 뚫린다(counter→press→counter 철회 실버그).
# 시뮬의 last_kind/last_q 도 역제안만 추적하므로 이쪽이 학습 분포와도 일치한다.
last = ctx.get("autonomy_last_counter") or {}
deadline = 0.5
end_ts, total_s = ctx.get("deadline_end_ts"), ctx.get("deadline_total_s")
if end_ts and total_s:
import time
deadline = float(np.clip((end_ts - time.time()) / total_s, 0.0, 1.0))
hist_n = int(ctx.get("hist_n") or 0)
hist_success = float(ctx["hist_success"]) if ctx.get("hist_success") is not None else 0.5
hist_settle = (settle_norm(float(ctx["hist_settle_ratio"]))
if ctx.get("hist_settle_ratio") is not None else 0.5)
sf = np.concatenate([build_state_features(snap), self._tenant_feat, extra_state(
last.get("kind", ""), float(last.get("q", 0.0)),
deadline=deadline, hist_n=min(hist_n, 5) / 5.0,
hist_success=hist_success if hist_n else 0.5,
hist_settle=hist_settle,
internet_gap=internet_gap_feat(float(ctx.get("internet_lowest_price") or 0),
float(snap.anchor_price)),
)])
span = max(snap.target_price - snap.anchor_price, 1.0)
pos = (snap.input_price - snap.anchor_price) / span
# 행동 봉투 (학습 available_actions 와 동일해야 한다):
# ① 목표가 초과 제시가는 '수락' 제외 — 매입 승인 범위(v3.1 착취 방지)
# ② 직전 역제안보다 낮은 금액의 역제안 제외 — 단조 양보 원칙(제안 철회는 협상 예절 위반;
# 올리는 '속도'는 정책 학습, 후퇴 '금지'만 구조로 보장)
# ③ 역제시 해금 조건 — 옛 제품 의미론 복원(제품 결정 2026-07-10): 일반 카드는 설득만,
# 역제시(숫자 제안)는 와일드카드처럼 마무리 수단. 최소 AUTONOMY_MIN_PRESS(기본 2)회
# 설득 이후에만 역제시 후보가 열린다. 해금 후의 타이밍·금액은 정책 학습.
# ④ 마무리 국면 — 제시가가 목표가 0.5% 이내로 붙으면 압박 제외(+역제시 잠금 해제):
# 푼돈 차이에서 '재검토 부탁' 반복은 상대만 지치게 한다. 클로징(역제안/최종제안)하거나 끝내거나.
min_press = int(os.getenv("AUTONOMY_MIN_PRESS", "2"))
near_target = snap.input_price <= snap.target_price * 1.005
counter_locked = (int(ctx.get("autonomy_press_n") or 0) < min_press) and not near_target
last_counter_q = float(last["q"]) if last.get("kind") == "counter" else None
if last_counter_q is not None:
counter_locked = False # 이미 역제시를 시작했으면 잠그지 않는다(단조 봉투가 관리)
# ⑤ 첫 역제안은 앵커가 이하(q ≤ 0)만 — 낮게 개시해 목표가까지 천천히 올라간다
# (제품 결정: 사다리를 다 쓰는 앵커링 개시. 이후 단조 봉투가 상향을 관리).
# ⑥ 결렬(walk)도 해금 전 금지 — 설득 0회에 walk 를 고르면 최종제안 보장(엔진)과 결합해
# '첫 턴 목표가 통보'가 된다(v3.4 라이브 결함). 해금 전에는 설득만 가능.
cands = [a for a in ACTIONS
if not (a.kind == "accept" and snap.input_price > snap.target_price)
and not (a.kind == "counter" and counter_locked)
and not (a.kind == "walk" and counter_locked)
and not (a.kind == "press" and near_target)
and not (a.kind == "counter" and last_counter_q is None and a.counter_q > 1e-9)
and not (a.kind == "counter" and last_counter_q is not None
and a.counter_q < last_counter_q - 1e-9)]
feats = []
for a in cands:
cut = 0.0
if a.kind == "counter":
c = snap.anchor_price + a.counter_q * span
cut = max(0.0, (snap.input_price - c) / max(snap.input_price, 1.0))
feats.append(a.feat(pos, cut))
feats = np.stack(feats)
W0, b0, W1, b1, W2, b2 = self._W
x = np.concatenate([np.repeat(sf[None, :], feats.shape[0], axis=0), feats], axis=1)
h = np.maximum(x @ W0.T + b0, 0.0)
h = np.maximum(h @ W1.T + b1, 0.0)
scores = (h @ W2.T + b2).squeeze(-1)
return cands[int(np.argmax(scores))]
@staticmethod
def counter_price(ctx: dict, act: Action) -> int:
span = max(ctx["target_price"] - ctx["anchor_price"], 1.0)
return int(round(ctx["anchor_price"] + act.counter_q * span))
class AutonomyStore:
_z = None
_load_failed = False
@classmethod
def enabled(cls) -> bool:
return os.getenv("AUTONOMY_MODE", "0").lower() in ("1", "true", "yes")
@classmethod
def policy_for(cls, engine) -> Optional[AutonomyPolicy]:
"""engine: tenancy.registry.TenantEngine. 비활성/번들 없음 → None (룰 엔진 유지)."""
if not cls.enabled() or cls._load_failed:
return None
if cls._z is None:
try:
cls._z = np.load(BUNDLE_PATH, allow_pickle=False)
LOG.i("[Autonomy] 완전 자율 정책 번들 로드 완료 — 협상 판정 룰 대체 모드")
except Exception as ex:
cls._load_failed = True
LOG.e_no_callstack(f"[Autonomy] 번들 로드 실패 → 룰 엔진 유지: {ex}")
return None
return AutonomyPolicy(cls._z, engine.config.reward)

View File

@ -1,118 +0,0 @@
"""DQNServingStore — action-as-feature DQN 서빙 (선택 전용, 학습 없음).
tools/export_dqn_serving.py 만든 dqn_serving.npz(ScoreNet 가중치 + 카드특징 392차원)
numpy 추론한다 서빙 컨테이너에 PyTorch 불필요.
역할 분담(계획서 H 트랙으로 가기 파일럿):
- 카드 '선택' DQN(greedy). Q-table 학습/영속/experience_logs 로깅 경로는 기존 그대로 유지
(Q-learning 오프폴리시라 DQN 고른 행동으로 갱신해도 유효, 로그는 DQN 오프라인 재학습 재료).
- 폴백: 플래그 꺼짐 / 번들 없음 / 가용 카드 전부 특징 미보유(신규 카드) None 반환,
호출부(ChatService) 기존 UCB Q-table 선택으로 진행한다.
활성화: 환경변수 DQN_SERVING=1 (docker-compose agent environment).
신규 카드 주의: 번들에 없는 카드번호는 후보에서 제외된다 카드 추가
tools/build_card_embeddings.py tools/export_dqn_serving.py 재실행 재배포 필요.
"""
import os
from typing import List, Optional
import numpy as np
from common.logger import LOG
from negotiation.policies.base import ActionDecision, PolicyContext
from negotiation.qtable.domain.service.feature_builder import (
build_state_features, build_tenant_features)
_HERE = os.path.dirname(os.path.abspath(__file__))
BUNDLE_PATH = os.path.join(_HERE, "..", "..", "artifacts", "dqn_serving.npz")
class _Bundle:
def __init__(self, z):
self.W0, self.b0 = z["W0"], z["b0"]
self.W1, self.b1 = z["W1"], z["b1"]
self.W2, self.b2 = z["W2"], z["b2"]
self.state_dim = int(z["state_dim"])
self.card_feats = {str(n): z["card_feats"][i]
for i, n in enumerate(z["card_numbers"])}
def scores(self, state_feat: np.ndarray, card_feats: np.ndarray) -> np.ndarray:
"""가용 카드 K개 일괄 채점: [K, state+card] → [K]."""
k = card_feats.shape[0]
x = np.concatenate([np.repeat(state_feat[None, :], k, axis=0), card_feats], axis=1)
h = np.maximum(x @ self.W0.T + self.b0, 0.0)
h = np.maximum(h @ self.W1.T + self.b1, 0.0)
return (h @ self.W2.T + self.b2).squeeze(-1)
class DQNServingPolicy:
"""UCBQTablePolicy.select 와 동일한 PolicyContext → ActionDecision 계약(선택 전용)."""
name = "feature_dqn"
_EPS = 0.1 # propensity 근사용 ε (UCB 정책과 동일 관례 — OPE 지지 확보용, 선택은 greedy)
def __init__(self, bundle: _Bundle, engine): # engine: tenancy.registry.TenantEngine
self._bundle = bundle
self._mapper = engine.mapper
self._tenant_feat = build_tenant_features(engine.config.reward)
def _available(self, ctx: PolicyContext) -> List[int]:
# UCBQTablePolicy._available 과 동일 규칙 (마스크 → used 제외 → 소진 시 전체 허용)
if ctx.available_mask is not None:
avail = [a for a in range(ctx.action_space_size) if ctx.available_mask[a]]
else:
used = ctx.episode.used_action_ids if ctx.episode else set()
avail = [a for a in range(ctx.action_space_size) if a not in used]
return avail or list(range(ctx.action_space_size))
def select(self, ctx: PolicyContext) -> Optional[ActionDecision]:
"""카드특징이 있는 가용 카드가 없으면 None → 호출부가 Q-table 로 폴백."""
candidates = [] # (action_id, card_feat)
for a in self._available(ctx):
num = self._mapper.get_card_id(a)
feat = self._bundle.card_feats.get(num) if num else None
if feat is not None:
candidates.append((a, feat))
if not candidates:
return None
state_feat = np.concatenate([build_state_features(ctx.snapshot), self._tenant_feat])
if state_feat.shape[0] != self._bundle.state_dim:
LOG.e_no_callstack(
f"[DQNServing] state_dim 불일치: {state_feat.shape[0]} != {self._bundle.state_dim}")
return None
sc = self._bundle.scores(state_feat, np.stack([f for _, f in candidates]))
i = int(sc.argmax())
n = len(candidates)
return ActionDecision(
action_id=candidates[i][0],
propensity=(1.0 - self._EPS) + self._EPS / n,
q_value=float(sc[i]),
ucb_score=float(sc[i]),
available_actions=[a for a, _ in candidates],
)
class DQNServingStore:
"""번들 lazy 로드 + 캐시. 비활성/부재 시 None (호출부 Q-table 폴백)."""
_bundle: Optional[_Bundle] = None
_load_failed = False
@classmethod
def enabled(cls) -> bool:
return os.getenv("DQN_SERVING", "0").lower() in ("1", "true", "yes")
@classmethod
def policy_for(cls, engine) -> Optional[DQNServingPolicy]:
if not cls.enabled() or cls._load_failed:
return None
if cls._bundle is None:
try:
cls._bundle = _Bundle(np.load(BUNDLE_PATH, allow_pickle=False))
LOG.i(f"[DQNServing] 번들 로드 완료: 카드 {len(cls._bundle.card_feats)}")
except Exception as ex:
cls._load_failed = True # 요청마다 재시도하지 않음
LOG.e_no_callstack(f"[DQNServing] 번들 로드 실패 → Q-table 폴백: {ex}")
return None
return DQNServingPolicy(cls._bundle, engine)

View File

@ -1,44 +0,0 @@
"""build_state_features — snapshot(raw 연속값) → 실수 벡터 (DQN/action-as-feature 용).
state_calculator.build_state(이산화) 대비되는 연속 표현. 이산화(등급/162) 하지 않고
정규화된 raw 값을 그대로 벡터로 내보낸다. 협력사 특징(매출·경쟁사수·유통) 벡터에 포함되므로
'협력사를 입력으로'(Phase 3) 자연스럽게 달성된다.
"""
import numpy as np
from negotiation.qtable.domain.model.snapshot import NegotiationSnapshot
DIST_CLASSES = ("A", "B", "C")
STATE_FEATURE_DIM = 9 # build_state_features 벡터 길이. feature 추가 시 갱신.
TENANT_FEATURE_DIM = 5 # build_tenant_features 벡터 길이.
def build_tenant_features(reward_cfg) -> np.ndarray:
"""고객사 '성향'을 ID 가 아니라 보상 설정값(내용)으로 벡터화 (Phase 3 고객사 조건화).
고객사도 tenant.yaml reward 설정만 있으면 즉시 조건화된다 (cold-start 없음).
"""
return np.array([
reward_cfg.max_weight, # 가격 중시 정도 (W↑ = 가격보상 비중↑)
reward_cfg.success_reward / 2.0, # 성사를 얼마나 크게 치는가
-reward_cfg.failure_penalty / 2.0, # 결렬을 얼마나 무서워하는가
reward_cfg.penalty_lambda * 20.0, # 속도 성향 (오래 끌수록 벌점)
reward_cfg.beta, # 앵커 초과달성 보너스 성향
], dtype=np.float32)
def build_state_features(s: NegotiationSnapshot) -> np.ndarray:
"""정규화된 연속 상태 벡터. 등급화 없음 — 990원과 850원이 구별된다."""
dist_onehot = [1.0 if s.distribution_code == c else 0.0 for c in DIST_CLASSES]
anchor = max(s.anchor_price, 1.0)
target = max(s.target_price, 1.0)
return np.array([
min(s.revenue_amount, 5e8) / 5e8, # 협력사 매출 (0~1)
*dist_onehot, # 유통 A/B/C
min(s.partner_count, 5) / 5.0, # 대안 협력사 수 (BATNA)
float(np.clip(s.acceptance_ratio, 0.0, 1.0)), # 수용률
float(np.clip((s.input_price - anchor) / anchor, -1.0, 2.0)), # 앵커 대비 격차 (연속!)
float(np.clip((target - s.input_price) / target, -2.0, 1.0)), # 목표 대비 여유
min(s.round_number, 10) / 10.0, # 라운드
], dtype=np.float32)

View File

@ -53,6 +53,9 @@ class Res_Chat(Res_WebPacketProtocol):
# 성공 확정 이후 턴(협상완료 요약·협상종료)에 내려주는 합의가. 와일드카드 1% 인하 수락 등 # 성공 확정 이후 턴(협상완료 요약·협상종료)에 내려주는 합의가. 와일드카드 1% 인하 수락 등
# 유저가 직접 입력하지 않은 가격으로 타결될 수 있어, backend 요약/입찰가는 이 값을 최우선 사용한다. # 유저가 직접 입력하지 않은 가격으로 타결될 수 있어, backend 요약/입찰가는 이 값을 최우선 사용한다.
settled_price: Optional[int] = None settled_price: Optional[int] = None
# Phase 3 이해층: 자유 발화를 NLU 로 해석해 진행한 경우, 엔진에 실제 전달된 입력.
# (예: "만원까지는 어렵고 10,500원이면 가능합니다" → "10500") 미해석/정형 입력이면 None.
interpreted_input: Optional[str] = None
class Res_ChatSession(Res_WebPacketProtocol): class Res_ChatSession(Res_WebPacketProtocol):

View File

@ -13,16 +13,19 @@ from common.enums import DBType, ErrorType
from common.database.db_session_manager import DB_SESSION_MNG from common.database.db_session_manager import DB_SESSION_MNG
from common.logger import LOG from common.logger import LOG
from config.server_configs import agent_config from config.server_configs import agent_config
from negotiation.chat.service import ment_generator from negotiation.cards.domain.tactics import (
from negotiation.chat.service.chat_engine import ChatEngine, ChatSession, StepView Offer, available, compute_offer_detail, is_played, mark_played, playable, record_offer, spec_from_context,
)
from negotiation.chat.service.chat_engine import (
_CHOICE_MODES, _PRICE_MODES, ChatEngine, ChatSession, StepView,
)
from negotiation.chat.service.indicator import compute_indicator from negotiation.chat.service.indicator import compute_indicator
from negotiation.chat.service.input_interpreter import SIMPLE_PRICE_RE, InputInterpreter
from negotiation.chat.service.chat_session_repository import ChatSessionRepository from negotiation.chat.service.chat_session_repository import ChatSessionRepository
from negotiation.chat.service.negotiation_context_loader import NegotiationContextLoader from negotiation.chat.service.negotiation_context_loader import NegotiationContextLoader
from negotiation.chat.service.script_naturalizer import ScriptNaturalizer, build_situation
from negotiation.chat.service.script_repository import ScriptRepository from negotiation.chat.service.script_repository import ScriptRepository
from negotiation.policies.base import EpisodeState, PolicyContext, Transition from negotiation.policies.base import EpisodeState, PolicyContext, Transition
from negotiation.policies.autonomy_actions import ACTIONS as AUTONOMY_ACTIONS
from negotiation.policy.autonomy_store import AutonomyStore
from negotiation.policy.dqn_store import DQNServingStore
from negotiation.policy.model_store import QTablePolicyStore from negotiation.policy.model_store import QTablePolicyStore
from negotiation.qtable.domain.model.snapshot import NegotiationOutcome, NegotiationSnapshot, PartnerType from negotiation.qtable.domain.model.snapshot import NegotiationOutcome, NegotiationSnapshot, PartnerType
from negotiation.qtable.domain.service.reward_calculator import RewardCalculator from negotiation.qtable.domain.service.reward_calculator import RewardCalculator
@ -38,9 +41,19 @@ _DEFAULT_TARGET_PRICE = 10000 # KT 목표 매입가
_DEFAULT_ANCHOR_PRICE = 9900 # 앵커링가(목표가보다 낮음). 제시가 ≤ anchor → 우선협상 _DEFAULT_ANCHOR_PRICE = 9900 # 앵커링가(목표가보다 낮음). 제시가 ≤ anchor → 우선협상
_DEFAULT_REVENUE_AMOUNT = 20_000_000 # 매출액(원) — suppliers.total_revenue 미기재 시 폴백 _DEFAULT_REVENUE_AMOUNT = 20_000_000 # 매출액(원) — suppliers.total_revenue 미기재 시 폴백
_DEFAULT_DISTRIBUTION_CODE = "A" # 유통 코드 — supplier_items.supply_type 미지정 시 폴백 _DEFAULT_DISTRIBUTION_CODE = "A" # 유통 코드 — supplier_items.supply_type 미지정 시 폴백
_DEFAULT_PARTNER_NAME = "귀사" # 협력사명 — suppliers.name 미기재/데모 시 폴백(카드 {partner_name})
_DEFAULT_PRODUCT_NAME = "본 상품" # 상품명 — items.name 미기재/데모 시 폴백(카드 {product_name})
_DEFAULT_ITEM_PRICE_LABEL = "공급가" # 협상 기준가 호칭(공급사 화면 고정 용어) — DB 컨텍스트 없는 데모/직접호출 경로 폴백
# (negodata 용어 카탈로그 item.price 의 base 와 같아야 표기가 갈리지 않는다)
class ChatService: class ChatService:
# Phase 2 표현층 / Phase 3 이해층 — 테넌트 llm.enabled + 전역 자격증명일 때만 사용(기본 무동작).
# 클래스 속성인 이유: FastAPI 가 ChatService 를 Depends 로 쓰므로 __init__ 파라미터를 두면
# 쿼리 파라미터로 해석된다. 테스트는 인스턴스 속성으로 덮어 주입한다.
_naturalizer = ScriptNaturalizer()
_interpreter = InputInterpreter()
async def chat(self, engine: TenantEngine, req: Req_Chat) -> Res_Chat: async def chat(self, engine: TenantEngine, req: Req_Chat) -> Res_Chat:
res = Res_Chat() res = Res_Chat()
repo = ScriptRepository(engine.config, agent_config.tenants_dir) repo = ScriptRepository(engine.config, agent_config.tenants_dir)
@ -50,19 +63,9 @@ class ChatService:
session = await sess_repo.get(req.session_id) if req.session_id else None session = await sess_repo.get(req.session_id) if req.session_id else None
# 새 세션 컨텍스트: 요청 페이로드 대신 DB(negotiation.sessions 등)에서 1회 조회. # 새 세션 컨텍스트: 요청 페이로드 대신 DB(negotiation.sessions 등)에서 1회 조회.
# 행이 없으면(데모/테스트 직접 호출) 기본값 폴백. # 행이 없으면(데모/테스트 직접 호출) 기본값 폴백.
db_ctx = None if session else await NegotiationContextLoader().load(req.session_id, engine.company_id) db_ctx = None if session else await NegotiationContextLoader().load(req.session_id)
rq_type = session.rq_type if session else (db_ctx.rq_type if db_ctx else _DEFAULT_RQ_TYPE) rq_type = session.rq_type if session else (db_ctx.rq_type if db_ctx else _DEFAULT_RQ_TYPE)
chat_engine = ChatEngine(repo, rq_type=rq_type) chat_engine = ChatEngine(repo, rq_type=rq_type)
# 완전 자율 모드(AUTONOMY_MODE=1 + 번들 존재): 가격협상 판정 룰·카드 선택을 정책 행동으로 대체.
# decider 를 감싸 결정을 컨텍스트에 기록 → advance() 후 experience_logs 에 적재(_autonomy_learn).
autonomy = AutonomyStore.policy_for(engine)
if autonomy is not None:
def _decide(ctx, _p=autonomy):
act = _p.decide(ctx)
ctx["autonomy_pending"] = {"idx": AUTONOMY_ACTIONS.index(act), "kind": act.kind,
"q": act.counter_q, "s": act.strategy}
return act
chat_engine.autonomy_decider = _decide
# ① step desync 감지: backend 가 본 직전 봇 step(client_step)이 agent 세션 step 과 다르면 경고. # ① step desync 감지: backend 가 본 직전 봇 step(client_step)이 agent 세션 step 과 다르면 경고.
# agent 가 자기 step 을 정답으로 보고 진행하고(응답의 step/client_step 으로 backend 가 따라옴), # agent 가 자기 step 을 정답으로 보고 진행하고(응답의 step/client_step 으로 backend 가 따라옴),
@ -83,8 +86,12 @@ class ChatService:
selected_wild_cards = db_ctx.selected_wild_card_numbers if db_ctx else [] selected_wild_cards = db_ctx.selected_wild_card_numbers if db_ctx else []
# 운영 DB 세션은 견적 version_id 에 묶인 카드만 사용한다. 직접 호출/데모(DB context 없음)는 # 운영 DB 세션은 견적 version_id 에 묶인 카드만 사용한다. 직접 호출/데모(DB context 없음)는
# 기존 테넌트 기본 action mapping 으로 폴백해 로컬 테스트와 콘솔 데모를 유지한다. # 기존 테넌트 기본 action mapping 으로 폴백해 로컬 테스트와 콘솔 데모를 유지한다.
# 협상카드 사용 횟수 상한(quotation_settings.card_count)으로 실제 플레이 가능한 카드 수를 캡한다 —
# session.action_space_size 는 카드 소진 판정(cards_total) 전용이라 여기서 줄여도 Q-table 은
# engine.action_space_size(카탈로그 전체)로 별도 고정된다. card_count 미설정(None)이면 선택 수 그대로.
card_cap = db_ctx.card_count if (db_ctx and db_ctx.card_count and db_ctx.card_count > 0) else None
action_space_size = ( action_space_size = (
min(len(selected_nego_cards), engine.action_space_size) min(len(selected_nego_cards), engine.action_space_size, *( [card_cap] if card_cap else [] ))
if db_ctx is not None if db_ctx is not None
else engine.action_space_size else engine.action_space_size
) )
@ -105,28 +112,40 @@ class ChatService:
# 목표가/앵커링가: sessions 행(생성 시 박제된 anchoring_price) → 박제 ‰ → 1% 폴백 (loader). # 목표가/앵커링가: sessions 행(생성 시 박제된 anchoring_price) → 박제 ‰ → 1% 폴백 (loader).
"anchor_price": db_ctx.anchor_price if db_ctx else _DEFAULT_ANCHOR_PRICE, "anchor_price": db_ctx.anchor_price if db_ctx else _DEFAULT_ANCHOR_PRICE,
"target_price": db_ctx.target_price if db_ctx else _DEFAULT_TARGET_PRICE, "target_price": db_ctx.target_price if db_ctx else _DEFAULT_TARGET_PRICE,
# 타결 상한가(sessions.done_ceiling_price 박제) — 타결 판정선이자 카드 제안가 상한.
# 목표가를 조금 넘어도 이 이하면 타결한다. 미박제/데모는 목표가와 같다.
"done_ceiling_price": db_ctx.done_ceiling_price if db_ctx else _DEFAULT_TARGET_PRICE,
# 협력사명/상품명 — 카드 스크립트 {partner_name}·{product_name} 치환용(loader). 없으면 폴백.
"partner_name": (db_ctx.partner_name if db_ctx and db_ctx.partner_name else _DEFAULT_PARTNER_NAME),
"product_name": (db_ctx.product_name if db_ctx and db_ctx.product_name else _DEFAULT_PRODUCT_NAME),
"round": 0, "round": 0,
# 기존 공급가(품목 기준가) — 가격협상_확인 인하율 산출용. # 협상 기준가(고객사가 관리하는 가격 — 공급가 또는 매입가) — 가격협상_확인 인하율 산출용.
# 호칭은 회사 설정 라벨을 따른다("기존 {label} 대비 …" 멘트).
"item_price": db_ctx.item_price if db_ctx else 0, "item_price": db_ctx.item_price if db_ctx else 0,
"item_price_label": db_ctx.item_price_label if db_ctx else _DEFAULT_ITEM_PRICE_LABEL,
# 회사 용어 사전 — 스크립트의 {label_*} 토큰(협력사·목표가·배송형태 등) 치환용.
"labels": (db_ctx.labels if db_ctx else {}),
# 인터넷 최저가(items.internet_lowest_price, LPS 대표값) — 카드 {internet_lowest_price} 치환용.
# 미수집(0)이면 vars_for 가 키를 만들지 않아 원형 유지(허위 시장가 표기 방지).
"internet_lowest_price": db_ctx.internet_lowest_price if db_ctx else 0,
# 견적 생성 시 선택한 카드. 일반카드는 action_id 0..N-1 에 그대로 매핑한다. # 견적 생성 시 선택한 카드. 일반카드는 action_id 0..N-1 에 그대로 매핑한다.
# 1% 인하는 기본 와일드카드로 항상 열고, 재원부족 등 선택형 와일드카드는 # 1% 인하는 기본 와일드카드로 항상 열고, 재원부족 등 선택형 와일드카드는
# 선택된 와일드카드가 있을 때만 허용한다. # 선택된 와일드카드가 있을 때만 허용한다.
"db_context_loaded": db_ctx is not None, "db_context_loaded": db_ctx is not None,
"selected_nego_card_numbers": selected_nego_cards, "selected_nego_card_numbers": selected_nego_cards,
"selected_wild_card_numbers": selected_wild_cards, "selected_wild_card_numbers": selected_wild_cards,
# 카드번호 → 전술 {offer_variable, min_round, closing}. 시작 시 1회 박제(loader) —
# 이후 카드 멘트가 바뀌어도 이 협상은 시작 시점 전술로 끝까지 간다.
"card_specs": (db_ctx.card_specs if db_ctx else {}),
"allow_selected_wildcards": True if db_ctx is None else bool(selected_wild_cards), "allow_selected_wildcards": True if db_ctx is None else bool(selected_wild_cards),
# ---- 자율 에이전트 v3 특징 소스 (미상이면 키 자체를 중립값으로 — JSON 직렬화 안전) ----
"internet_lowest_price": db_ctx.internet_lowest_price if db_ctx else 0,
"deadline_end_ts": db_ctx.deadline_end_ts if db_ctx else None,
"deadline_total_s": db_ctx.deadline_total_s if db_ctx else None,
"hist_n": db_ctx.hist_n if db_ctx else 0,
"hist_success": db_ctx.hist_success if db_ctx else None,
"hist_settle_ratio": db_ctx.hist_settle_ratio if db_ctx else None,
}, },
) )
view = chat_engine.start(session) view = chat_engine.start(session)
else: else:
view = chat_engine.advance(session, req.user_input) # Phase 3 이해층: 자유 발화(버튼/정형 입력이 아닌 텍스트)를 기대 입력으로 해석.
# 해석 실패/미설정 시 원문 그대로 → 기존 엔진 재질문 흐름 유지.
user_input = await self._interpret_input(engine, chat_engine, session, req.user_input, res)
view = chat_engine.advance(session, user_input)
# 2) 응답 기본 채움 (학습 블록이 가격협상 턴에서 script/indicator 를 덮어쓸 수 있어 먼저 채운다) # 2) 응답 기본 채움 (학습 블록이 가격협상 턴에서 script/indicator 를 덮어쓸 수 있어 먼저 채운다)
res.session_id = session.session_id res.session_id = session.session_id
@ -143,6 +162,18 @@ class ChatService:
if session.context.get("final_outcome") == "success" and session.context.get("input_price"): if session.context.get("final_outcome") == "success" and session.context.get("input_price"):
res.settled_price = int(session.context["input_price"]) res.settled_price = int(session.context["input_price"])
# 선택형 와일드카드 턴(wild_card_dynamic): 멘트 정본은 card.wild_cards(negodata 편집).
# DB 멘트가 있으면 스텝 기본 멘트를 대체하고, 없으면 기본 멘트(counter_price 치환)로 진행.
if view.step == "wild_card_dynamic" and session.context.get("active_wild_card_number"):
number = session.context["active_wild_card_number"]
template = await repo.resolve_wild_card_template(number)
if template:
if engine.config.llm.enabled and ScriptNaturalizer.available():
template = (await self._naturalizer.naturalize(
template, situation=build_situation(session.context))) or template
res.script = repo.format_script(template, chat_engine.vars_for(session))
res.card_id = number
# 3) 학습 결합 (가격협상 카드선택 → 카드 스크립트·협상지표 / 종료 보상) # 3) 학습 결합 (가격협상 카드선택 → 카드 스크립트·협상지표 / 종료 보상)
if view.error is None and session.action_space_size > 0: if view.error is None and session.action_space_size > 0:
if view.needs_card_selection: if view.needs_card_selection:
@ -150,17 +181,6 @@ class ChatService:
elif view.outcome is not None: elif view.outcome is not None:
await self._terminal_learn(engine, session, view.outcome, res) await self._terminal_learn(engine, session, view.outcome, res)
# 3-b) 완전 자율 모드: 정책 결정·종료 결과를 experience_logs 에 적재 (실로그 재학습 재료).
if autonomy is not None and view.error is None:
await self._autonomy_learn(engine, session, view, res)
# 자율 스텝 멘트를 LLM 으로 생성 (행동은 RL, 문장은 LLM). 실패/미설정 → 템플릿 유지.
if view.step.startswith("자율_"):
llm_ment = await ment_generator.generate(view.step, session.context)
if llm_ment:
res.script = llm_ment
# 직전 봇 멘트 보존 — 다음 생성에서 같은 문장 구조 반복을 금지하는 힌트.
session.context["autonomy_last_ment"] = (res.script or "")[:200]
if view.error: if view.error:
res.result.SetResult(ErrorType.NEGO_INVALID_STEP) res.result.SetResult(ErrorType.NEGO_INVALID_STEP)
res.msg = view.error res.msg = view.error
@ -186,6 +206,53 @@ class ChatService:
res.found = True res.found = True
return res return res
# ---- Phase 3 이해층 (자유 발화 NLU) ---------------------------------
async def _interpret_input(self, engine: TenantEngine, chat_engine: ChatEngine,
session: ChatSession, user_input: Optional[str], res: Res_Chat) -> Optional[str]:
"""자유 발화를 현재 step 의 기대 입력으로 해석해 엔진에 넘길 문자열을 돌려준다.
결정론 fast path 우선: 정형 가격([\\d,]+?)·버튼 그대로면 LLM 부르지 않는다.
자유 텍스트 + llm.enabled + 자격증명일 때만 InputInterpreter 호출. 해석 실패/미설정이면
원문 그대로 반환 기존 엔진의 재질문/기본분기 흐름이 그대로 동작(협상 불중단).
"""
if user_input is None:
return user_input
raw = str(user_input).strip()
if not raw:
return user_input
node = chat_engine.scripts.get(session.step, {})
mode = node.get("next_input_mode", "null")
options: list = []
if mode in _PRICE_MODES:
if SIMPLE_PRICE_RE.fullmatch(raw):
return user_input # 정형 가격 — 기존 결정론 파서 경로
elif mode in _CHOICE_MODES:
options = self._step_options(node)
if raw in options:
return user_input # 버튼 값 그대로 — 결정론 경로
else:
return user_input # 입력을 받지 않는 스텝
if not (engine.config.llm.enabled and InputInterpreter.available()):
return user_input
out = await self._interpreter.interpret(raw, input_mode=mode, input_options=options,
step_script=node.get("script"))
if out is None:
return user_input
LOG.i(f"[ChatService] NLU: {raw!r}{out.kind}={out.value!r} (근거={out.source!r}) session={session.session_id}")
res.interpreted_input = out.value # 투명성: backend/front 가 해석 결과를 표시할 수 있게
return out.value
@staticmethod
def _step_options(node: dict) -> list:
"""현재 step 이 허용하는 선택지 — input_options 우선, next_step 분기 키 보강."""
opts = [str(o) for o in (node.get("input_options") or [])]
ns = node.get("next_step")
if isinstance(ns, dict):
for k in ns.keys():
if k != "default" and str(k) not in opts:
opts.append(str(k))
return opts
# ---- 학습 ---------------------------------------------------------- # ---- 학습 ----------------------------------------------------------
@staticmethod @staticmethod
def _acceptance_ratio(context: dict) -> float: def _acceptance_ratio(context: dict) -> float:
@ -215,6 +282,11 @@ class ChatService:
async def _select_and_learn(self, engine: TenantEngine, chat_engine: ChatEngine, async def _select_and_learn(self, engine: TenantEngine, chat_engine: ChatEngine,
scripts: ScriptRepository, session: ChatSession, res: Res_Chat): scripts: ScriptRepository, session: ChatSession, res: Res_Chat):
# 종결 국면(라운드 만료·카드 소진, 엔진 check_iteration_limit 이 마킹): 규칙층이
# 종결 전술(중간값 절충/최후통첩)을 강제 발동한다 — RL 선택·학습 대상이 아니다.
if session.context.pop("force_closing", False):
await self._play_closing_tactic(engine, chat_engine, scripts, session, res)
return
snap = self._snapshot(session, NegotiationOutcome.ONGOING) snap = self._snapshot(session, NegotiationOutcome.ONGOING)
try: try:
idx = state_index(snap, engine.config.state) idx = state_index(snap, engine.config.state)
@ -224,28 +296,35 @@ class ChatService:
policy, version_id, repo = await QTablePolicyStore.load(engine) policy, version_id, repo = await QTablePolicyStore.load(engine)
# action space 는 카탈로그 전체(engine.action_space_size)로 고정 — action_id↔카드 대응을 # action space 는 카탈로그 전체(engine.action_space_size)로 고정 — action_id↔카드 대응을
# 견적마다 일정하게 유지해 Q-table 학습 일관성을 지킨다. 견적 선택은 축소가 아니라 # 견적마다 일정하게 유지해 Q-table 학습 일관성을 지킨다. 견적 선택은 축소가 아니라
# available_mask 로 처리한다(선택 카드만 pickable, 사용분 제외). # available_mask 로 처리한다(선택 카드만 pickable, 사용분 제외 + 전술 발동조건 AND).
ctx = PolicyContext(state_index=idx, snapshot=snap, action_space_size=engine.action_space_size, ctx = PolicyContext(state_index=idx, snapshot=snap, action_space_size=engine.action_space_size,
available_mask=self._selection_mask(engine, session), available_mask=self._combined_mask(engine, session),
prior_bonus=self._selection_prior(engine, session),
episode=EpisodeState(used_action_ids=set(session.used_action_ids))) episode=EpisodeState(used_action_ids=set(session.used_action_ids)))
# 카드 '선택'은 DQN 서빙(활성 시), 학습/영속은 아래 Q-table 경로 그대로(오프폴리시 갱신). decision = policy.select(ctx)
# DQN 불가(비활성/번들 없음/후보 특징 없음)면 None → 기존 UCB 선택 폴백.
dqn = DQNServingStore.policy_for(engine)
decision = dqn.select(ctx) if dqn is not None else None
selector_name = dqn.name if decision is not None else policy.name
if decision is None:
decision = policy.select(ctx)
session.used_action_ids.add(decision.action_id) session.used_action_ids.add(decision.action_id)
card_id = self._card_id_for_action(engine, session, decision.action_id) card_id = self._card_id_for_action(engine, session, decision.action_id)
# 카드번호 공용 이력 — 와일드/종결 경로와 같은 목록을 본다("한 협상 한 카드 1회" 단일 판정).
mark_played(session.context, card_id)
# 전술 실행: 카드가 제시할 금액(스크립트 파싱 결과)을 계산해 세션에 적재한다.
# pending 이 있으면 이 턴은 수락/거절 스텝(가격협상_카운터)으로 전환되고,
# 협력사가 수락하면 이 가격으로 즉시 타결된다(chat_engine 의 수락 메커니즘).
# 유효조건(≤목표가 · <제시가) 미달이면 None → 금액 없이 설득 멘트만 나간다(HOLD 강등).
spec = spec_from_context(session.context, card_id)
offer = compute_offer_detail(spec, session.context) if available(spec, session.context) else None
counter = offer.price if offer else None
if offer is not None:
record_offer(session.context, offer)
reward = RewardCalculator(engine.config.reward, engine.config.state).calculate(snap) reward = RewardCalculator(engine.config.reward, engine.config.state).calculate(snap)
policy.update(Transition(state_index=idx, action_id=decision.action_id, reward=reward.total, done=False)) policy.update(Transition(state_index=idx, action_id=decision.action_id, reward=reward.total, done=False))
await QTablePolicyStore.persist_cell(repo, version_id, policy, idx, decision.action_id) await QTablePolicyStore.persist_cell(repo, version_id, policy, idx, decision.action_id)
session.context["last_state"] = idx session.context["last_state"] = idx
session.context["last_action"] = decision.action_id session.context["last_action"] = decision.action_id
await self._log(repo, session, idx, decision.action_id, card_id, snap, reward, decision.propensity, done=False) await self._log(repo, session, idx, decision.action_id, card_id, snap, reward, done=False,
decision=decision, policy=policy)
res.card_id = card_id res.card_id = card_id
res.policy = selector_name res.policy = policy.name
res.q_value = decision.q_value res.q_value = decision.q_value
res.updated_q = float(policy.qtable.q[idx, decision.action_id]) res.updated_q = float(policy.qtable.q[idx, decision.action_id])
res.visit_count = int(policy.qtable.visits[idx, decision.action_id]) res.visit_count = int(policy.qtable.visits[idx, decision.action_id])
@ -255,12 +334,17 @@ class ChatService:
# ① 선택된 카드의 스크립트를 봇 메시지(script)로 출력 ② 협상지표 게이지(indicator_value) 동봉. # ① 선택된 카드의 스크립트를 봇 메시지(script)로 출력 ② 협상지표 게이지(indicator_value) 동봉.
# backend/front 가 indicator/bot_chat_type 패스스루·게이지 렌더 준비 완료 → 값만 채우면 표시된다. # backend/front 가 indicator/bot_chat_type 패스스루·게이지 렌더 준비 완료 → 값만 채우면 표시된다.
# 카드 멘트: backoffice_db 모드면 card.nego_cards.script(negodata 편집 정본), 아니면 파일 폴백. # 카드 멘트: backoffice_db 모드면 card.nego_cards.script(negodata 편집 정본), 아니면 파일 폴백.
card_script = await scripts.resolve_card_script( template, tone, strategy = await scripts.resolve_card_template(
decision.action_id, decision.action_id, card_id,
card_id,
chat_engine.vars_for(session),
prefer_db=bool(session.context.get("selected_nego_card_numbers")), prefer_db=bool(session.context.get("selected_nego_card_numbers")),
) )
# Phase 2 표현층: llm.enabled(테넌트) + 자격증명 있으면 템플릿을 상황 맞춤 자연화.
# placeholder 유지 상태로 재작성 → 검증(치환자/숫자) → 실패·타임아웃 시 원본 폴백.
if template and engine.config.llm.enabled and ScriptNaturalizer.available():
naturalized = await self._naturalizer.naturalize(
template, situation=build_situation(session.context), tone=tone, strategy=strategy)
template = naturalized or template
card_script = scripts.format_script(template, chat_engine.vars_for(session)) if template else None
if card_script: if card_script:
res.script = card_script res.script = card_script
c = session.context c = session.context
@ -270,48 +354,59 @@ class ChatService:
res.indicator_range = ind[1] res.indicator_range = ind[1]
res.bot_chat_type = "indicator" res.bot_chat_type = "indicator"
async def _autonomy_learn(self, engine: TenantEngine, session: ChatSession, view: StepView, res: Res_Chat): # 카운터 제시 카드면 수락/거절 스텝(가격협상_카운터)으로 전환 — 카드 멘트({target_price} 등
"""완전 자율 행동 로깅 — Q-table 은 건드리지 않고 experience_logs 만 적재한다. # 카운터가 포함)는 그대로 두고, 입력만 [수락|다른 가격 제시] 버튼으로 바꾼다.
if counter is not None:
action_id = autonomy_actions.ACTIONS 인덱스, card_id = "AUT|종류|위치|전략" (카드 재학습 view2 = chat_engine.render_step(session, "가격협상_카운터")
파이프라인이 임베딩 매칭에서 자동 제외하도록 프리픽스로 구분). 종료 최종 보상 (done=True) res.step, res.client_step = view2.step, view2.client_step
남겨 retrain 에피소드 재구성 규약(카드턴 N + 종료 1) 정합을 맞춘다. res.input_mode, res.input_options = view2.input_mode, view2.input_options
""" if not card_script:
def _card_id(d) -> str: res.script = view2.script # 카드 멘트 없으면 스텝 기본 카운터 멘트
return f"AUT|{d['kind']}|{d['q']:g}|{d['s']}"[:40]
async def _play_closing_tactic(self, engine: TenantEngine, chat_engine: ChatEngine,
scripts: ScriptRepository, session: ChatSession, res: Res_Chat):
"""종결 국면 강제 전술 — 견적에서 선택한 종결 와일드카드(WC-05 중간값 절충 등) 우선,
없으면 목표가 최후통첩. 최종 카운터를 제시하고 수락/거절 스텝으로 전환한다.
규칙층의 강제 결정이므로 RL 선택/학습을 우회한다."""
ctx = session.context ctx = session.context
lrepo = LearningRepository(engine.company_id) ctx["closing_played"] = True
pending = ctx.pop("autonomy_pending", None) # 선택 와일드카드 중 종결 전용 카드(closing) — 이미 쓴 카드는 건너뛰고(같은 멘트 반복 방지),
if pending is not None: # 제안가 유효조건(≤목표가 · <제시가) 미달 카드도 건너뛴다(예: 절충가가 목표가 초과 → 미발동).
snap = self._snapshot(session, NegotiationOutcome.ONGOING) closing_number, closing_offer = None, None
try: for n in (ctx.get("selected_wild_card_numbers") or []):
idx = state_index(snap, engine.config.state) # 로깅 호환용 이산 인덱스 n = str(n)
except ValueError: spec = spec_from_context(ctx, n)
idx = 0 # 자율 모드는 이산 상태를 쓰지 않으므로 폴백해도 학습 오염 없음 if not available(spec, ctx, closing_phase=True) or is_played(ctx, n):
if ctx.get("autonomy_last"): continue
ctx["autonomy_prev"] = ctx["autonomy_last"] # 직전 결정 보존 — 멘트 생성 힌트(양보 언급)용 offer = compute_offer_detail(spec, ctx)
ctx["autonomy_last"] = dict(pending, state_index=idx) if offer is not None:
if pending.get("kind") == "counter": closing_number, closing_offer = n, offer
# 역제안 기억은 별도 키로 보존 — autonomy_last 는 '마지막 행동'이라 사이에 낀 break
# 설득이 덮어쓴다. 단조 봉투·탄약소진 판정이 이 기억을 기준으로 해야 if closing_offer is None:
# counter→press→counter 에서 제안 철회가 새지 않는다 (게이트가 잡은 실버그). # 폴백 최후통첩: 목표가 제시 (여기 도달 = 제시가 > target 이므로 항상 유효한 카운터).
ctx["autonomy_last_counter"] = dict(pending) closing_number = None
if pending.get("kind") == "press": target = int(ctx.get("target_price") or 0)
# 설득 횟수 누적 — 역제시 해금 조건(autonomy_store ③)의 카운터. price = int(ctx.get("input_price") or 0)
ctx["autonomy_press_n"] = int(ctx.get("autonomy_press_n") or 0) + 1 if 0 < target < price:
reward = RewardCalculator(engine.config.reward, engine.config.state).calculate(snap) closing_offer = Offer(price=target, variable="target_price",
await self._log(lrepo, session, idx, pending["idx"], _card_id(pending), snap, prev_customer=int(ctx.get("prev_customer_price") or ctx.get("anchor_price") or 0),
reward, (1.0 - 0.1) + 0.1 / len(AUTONOMY_ACTIONS), done=False) prev_partner=price)
res.policy = "full_autonomy" if closing_offer is None:
last = ctx.get("autonomy_last") return # 컨텍스트 이상 — 기존 가격협상 스텝 그대로(재제안 요구)
if view.outcome is not None and last is not None: mark_played(ctx, closing_number) # None(폴백 최후통첩)이면 no-op
oc = NegotiationOutcome.SUCCESS if view.outcome == "success" else NegotiationOutcome.FAILURE record_offer(ctx, closing_offer)
snap = self._snapshot(session, oc)
reward = RewardCalculator(engine.config.reward, engine.config.state).calculate(snap) template = None
res.reward_total = reward.total if closing_number:
await self._log(lrepo, session, last["state_index"], last["idx"], _card_id(last), snap, template = await scripts.resolve_wild_card_template(closing_number)
reward, None, done=True) if template and engine.config.llm.enabled and ScriptNaturalizer.available():
template = (await self._naturalizer.naturalize(
template, situation=build_situation(ctx))) or template
view2 = chat_engine.render_step(session, "가격협상_카운터")
res.step, res.client_step = view2.step, view2.client_step
res.input_mode, res.input_options = view2.input_mode, view2.input_options
res.script = scripts.format_script(template, chat_engine.vars_for(session)) if template else view2.script
res.card_id = closing_number
async def _terminal_learn(self, engine: TenantEngine, session: ChatSession, outcome: str, res: Res_Chat): async def _terminal_learn(self, engine: TenantEngine, session: ChatSession, outcome: str, res: Res_Chat):
oc = NegotiationOutcome.SUCCESS if outcome == "success" else NegotiationOutcome.FAILURE oc = NegotiationOutcome.SUCCESS if outcome == "success" else NegotiationOutcome.FAILURE
@ -326,7 +421,7 @@ class ChatService:
policy.update(Transition(state_index=last_state, action_id=last_action, reward=reward.total, done=True)) policy.update(Transition(state_index=last_state, action_id=last_action, reward=reward.total, done=True))
await QTablePolicyStore.persist_cell(repo, version_id, policy, last_state, last_action) await QTablePolicyStore.persist_cell(repo, version_id, policy, last_state, last_action)
await self._log(repo, session, last_state, last_action, await self._log(repo, session, last_state, last_action,
self._card_id_for_action(engine, session, last_action), snap, reward, None, done=True) self._card_id_for_action(engine, session, last_action), snap, reward, done=True)
res.updated_q = float(policy.qtable.q[last_state, last_action]) res.updated_q = float(policy.qtable.q[last_state, last_action])
@staticmethod @staticmethod
@ -357,13 +452,74 @@ class ChatService:
dtype=bool, dtype=bool,
) )
async def _log(self, repo: LearningRepository, session, state_index, action_id, card_id, snap, reward, propensity, done): @classmethod
def _combined_mask(cls, engine: TenantEngine, session: ChatSession) -> Optional[np.ndarray]:
"""견적 선택 마스크 AND 전술 발동조건 마스크.
결합 결과가 전부 False (선택 카드가 모두 발동 불가) 선택 마스크 단독으로 폴백
협상은 멈추지 않고(HOLD 설득으로라도 진행), 종결은 라운드 규칙이 처리한다.
"""
sel = cls._selection_mask(engine, session)
tac = cls._tactic_mask(engine, session)
if tac is None:
return sel
if sel is None:
return tac if tac.any() else None
both = sel & tac
return both if both.any() else sel
@staticmethod
def _tactic_mask(engine: TenantEngine, session: ChatSession) -> Optional[np.ndarray]:
"""지금 플레이 가능한 action 만 True. HOLD(설득)는 발동조건만, 금액 카드는 제안가 유효까지
본다(playable) 무효 금액(역행·목표가 초과 ) 멘트 글자로 나가는 자체를 막는다.
전부 True None(마스크 불필요)."""
ctx = session.context
mask = np.array(
[playable(spec_from_context(ctx, engine.mapper.get_card_id(a)), ctx)
for a in range(engine.action_space_size)],
dtype=bool,
)
return None if mask.all() else mask
@staticmethod
def _selection_prior(engine: TenantEngine, session: ChatSession) -> Optional[np.ndarray]:
"""의도층 prior(Phase 1) — 견적에서 고른 카드 순서를 콜드 스타트 선호로 반영.
갑이 먼저 고른 카드일수록 높은 보너스(최대 0.3, 순위 선형 감소). UCB 점수에
1/(1+visits) 감쇠로 더해지므로 학습이 쌓이면 Q 지배한다(오염 없음).
선택이 2 미만이면 순서 정보가 무의미 None.
"""
selected = session.context.get("selected_nego_card_numbers") or []
if len(selected) < 2:
return None
prior = np.zeros(engine.action_space_size)
n = len(selected)
for rank, num in enumerate(selected):
a = engine.mapper.get_action_id(str(num))
if a is not None and a < engine.action_space_size:
prior[a] = 0.3 * (n - rank) / n
return prior if prior.any() else None
async def _log(self, repo: LearningRepository, session, state_index, action_id, card_id, snap, reward, done,
decision=None, policy=None):
data = { data = {
"session_id": session.session_id, "state_index": state_index, "action_id": action_id, "session_id": session.session_id, "state_index": state_index, "action_id": action_id,
"card_id": card_id, "snapshot": snap.to_dict(), "propensity": propensity, "card_id": card_id, "snapshot": snap.to_dict(),
"turn": snap.round_number, "reward": reward.total, "done": done, "turn": snap.round_number, "reward": reward.total, "done": done,
"settled_price": int(snap.input_price) if snap.outcome == NegotiationOutcome.SUCCESS else None, "settled_price": int(snap.input_price) if snap.outcome == NegotiationOutcome.SUCCESS else None,
} }
if decision is not None and policy is not None:
# 선택 근거(Q값·UCB·방문수)를 그 턴 값 그대로 박제한다 — 사후에 q_values 를 읽으면 이미 갱신된 뒤라
# "그때 왜 이 카드였나"를 복원할 수 없다. negodata 협상 학습 화면이 이 컬럼들을 읽는다.
# 종료 로그(카드 선택 없는 done 행)는 decision 이 없어 NULL — 화면 집계(avg/max)가 무시한다.
data.update({
"propensity": decision.propensity,
"available_actions": decision.available_actions,
"q_value_at_selection": decision.q_value,
"ucb_score_at_selection": decision.ucb_score,
"visit_count_at_selection": int(policy.qtable.visits[state_index, action_id]),
"total_visits_at_selection": int(policy.qtable.state_visits(state_index)),
})
try: try:
await DB_SESSION_MNG.execute_lambda_run([DBType.MAIN.value], [lambda s: repo.log_transition(s, data)]) await DB_SESSION_MNG.execute_lambda_run([DBType.MAIN.value], [lambda s: repo.log_transition(s, data)])
except Exception as ex: except Exception as ex:

View File

@ -97,10 +97,10 @@ class NegotiationService:
# 7) experience_logs 기록 # 7) experience_logs 기록
if req.log: if req.log:
res.logged = await self._log(engine, session_id, idx, decision, snap, reward) res.logged = await self._log(engine, session_id, idx, decision, snap, reward, policy)
return res return res
async def _log(self, engine, session_id, idx, decision, snap, reward) -> bool: async def _log(self, engine, session_id, idx, decision, snap, reward, policy) -> bool:
repo = LearningRepository(engine.company_id) repo = LearningRepository(engine.company_id)
data = { data = {
"session_id": session_id, "state_index": idx, "action_id": decision.action_id, "session_id": session_id, "state_index": idx, "action_id": decision.action_id,
@ -108,6 +108,8 @@ class NegotiationService:
"turn": snap.round_number, "available_actions": decision.available_actions, "turn": snap.round_number, "available_actions": decision.available_actions,
"reward": reward.total, "done": snap.outcome != NegotiationOutcome.ONGOING, "reward": reward.total, "done": snap.outcome != NegotiationOutcome.ONGOING,
"q_value_at_selection": decision.q_value, "ucb_score_at_selection": decision.ucb_score, "q_value_at_selection": decision.q_value, "ucb_score_at_selection": decision.ucb_score,
"visit_count_at_selection": int(policy.qtable.visits[idx, decision.action_id]),
"total_visits_at_selection": int(policy.qtable.state_visits(idx)),
"settled_price": int(snap.input_price) if snap.outcome == NegotiationOutcome.SUCCESS else None, "settled_price": int(snap.input_price) if snap.outcome == NegotiationOutcome.SUCCESS else None,
} }
try: try:

View File

@ -51,7 +51,7 @@ async def resolve_company_name(repo: ICompanyProfileRepository, tenant_key: str)
try: try:
cid = uuid.UUID(tenant_key) cid = uuid.UUID(tenant_key)
except (ValueError, TypeError): except (ValueError, TypeError):
return None # ktcommerce 등 데모 테넌트명 → 파일 브랜드 유지 return None # imarketkorea 등 데모 테넌트명 → 파일 브랜드 유지
async def _q(s): async def _q(s):
_, name = await repo.get_company_name(s, cid) _, name = await repo.get_company_name(s, cid)

View File

@ -148,6 +148,11 @@ class NegotiationConfig(BaseModel):
anchor_rate: float = 0.01 # 목표가 대비 앵커링 인하율 (기본 1%) anchor_rate: float = 0.01 # 목표가 대비 앵커링 인하율 (기본 1%)
max_rounds: int = 5 # 라운드 상한(보조). 실제 종료는 '카드 소진' 기준. max_rounds: int = 5 # 라운드 상한(보조). 실제 종료는 '카드 소진' 기준.
# 결정 스택 규칙층(Phase 1) — ChatEngine 하드코딩을 테넌트별 데이터로.
wildcard_1pct_ratio: float = 1.02 # 제시가 ≤ anchor×비율 → 1% 인하 와일드카드로 마무리 유도
wildcard_entry_ratio: float = 1.05 # 선택 와일드카드 허용 시 와일드카드 진입 상한(anchor×비율)
max_counter_rounds: int = 3 # 에이전트 카운터 제안 상한(초과 시 협상실패 종료)
def anchor_for(self, target_price: float) -> float: def anchor_for(self, target_price: float) -> float:
return round(target_price * (1.0 - self.anchor_rate)) return round(target_price * (1.0 - self.anchor_rate))

View File

@ -15,5 +15,7 @@
"가격협상_와일드": "가격협상", "가격협상_와일드": "가격협상",
"협상완료": "협상종료", "협상완료": "협상종료",
"협상실패": "협상종료", "협상실패": "협상종료",
"협상종료": "협상종료" "협상종료": "협상종료",
} "가격협상_카운터": "가격협상",
"wild_card_dynamic": "가격협상"
}

View File

@ -1,12 +1,12 @@
{ {
"_comment": "가격협상(카드선택) 턴에 출력할 협상 카드 스크립트. action_id(0~8) → 멘트. 선행 chat_server 의 nego_card_scripts 를 대체하는 중립 기본값(CLEANROOM.md). 실제 운영 시 card.nego_cards.script 로 override(내부 소스만 변경, 흐름 동일). 변수: {target}=목표 매입가, {input_price}=직전 제시가, {anchor}=앵커가, {discount_rate}=기존가 대비 인하율(%).", "_comment": "가격협상(카드선택) 턴에 출력할 협상 카드 스크립트. action_id(0~8) → 멘트. 선행 chat_server 의 nego_card_scripts 를 대체하는 중립 기본값(CLEANROOM.md). 실제 운영 시 card.nego_cards.script 로 override(내부 소스만 변경, 흐름 동일). 변수: {target}=목표가, {input_price}=직전 제시가, {anchor}=앵커가, {discount_rate}=기존가 대비 인하율(%).",
"0": "제안해 주신 **{input_price}원**, 감사합니다. 다만 동일 품목의 시장 거래가를 감안하면 추가 조정 여력이 있어 보입니다. 한 번 더 검토해 가격을 제안해 주시겠어요?", "0": "제안해 주신 **{input_price}원**, 감사합니다. 다만 동일 품목의 시장 거래가를 감안하면 추가 조정 여력이 있어 보입니다. 한 번 더 검토해 가격을 제안해 주시겠어요?",
"1": "적극적으로 협조해 주셔서 감사합니다. 현재 제시가는 목표 매입가(**{target}원**)와는 아직 차이가 있습니다. 조금만 더 좁혀 주시면 우선협상 대상으로 검토하겠습니다.", "1": "적극적으로 협조해 주셔서 감사합니다. 현재 제시가는 {label_target_price}(**{target}원**)와는 아직 차이가 있습니다. 조금만 더 좁혀 주시면 우선협상 대상으로 검토하겠습니다.",
"2": "좋은 제안 감사합니다. 다른 협력사들의 제안 수준을 고려할 때, 현재 금액으로는 경쟁력이 다소 부족합니다. 재검토된 가격을 부탁드립니다.", "2": "좋은 제안 감사합니다. 다른 {label_supplier}들의 제안 수준을 고려할 때, 현재 금액으로는 경쟁력이 다소 부족합니다. 재검토된 가격을 부탁드립니다.",
"3": "협상에 성실히 임해 주셔서 감사합니다. 내부 승인 기준에 맞추려면 앵커가({anchor}원) 수준에 가까운 제안이 필요합니다. 가능하신 범위에서 다시 제안해 주세요.", "3": "협상에 성실히 임해 주셔서 감사합니다. 내부 승인 기준에 맞추려면 앵커가({anchor}원) 수준에 가까운 제안이 필요합니다. 가능하신 범위에서 다시 제안해 주세요.",
"4": "제시해 주신 인하율 약 {discount_rate}%는 의미 있는 진전입니다. 다만 거래를 확정하려면 조금 더 협조가 필요합니다. 한 차례 더 조정해 주시겠어요?", "4": "제시해 주신 조건은 의미 있는 진전입니다. 다만 거래를 확정하려면 조금 더 협조가 필요합니다. 한 차례 더 조정해 주시겠어요?",
"5": "장기적인 협력 관계를 고려해 최대한 반영하고자 합니다. 현재 제시가에서 추가로 조정해 주시면 즉시 검토를 진행하겠습니다. 다시 제안 부탁드립니다.", "5": "장기적인 협력 관계를 고려해 최대한 반영하고자 합니다. 현재 제시가에서 추가로 조정해 주시면 즉시 검토를 진행하겠습니다. 다시 제안 부탁드립니다.",
"6": "검토 결과, 현재 제시가는 우리 기준을 충족하기 직전 단계입니다. 마지막으로 한 번 더 조정된 가격을 제안해 주시면 협상을 마무리할 수 있습니다.", "6": "검토 결과, 현재 제시가는 우리 기준을 충족하기 직전 단계입니다. 마지막으로 한 번 더 조정된 가격을 제안해 주시면 협상을 마무리할 수 있습니다.",
"7": "성의 있는 제안 감사합니다. 다만 물량과 납기 조건을 함께 고려하면 {input_price}원은 다소 높습니다. 목표 매입가({target}원)에 가까운 금액을 제안해 주세요.", "7": "성의 있는 제안 감사합니다. 다만 물량과 납기 조건을 함께 고려하면 {input_price}원은 다소 높습니다. {label_target_price}({target}원)에 가까운 금액을 제안해 주세요.",
"8": "긍정적으로 검토되고 있습니다. 내부 결재를 위해 명분이 조금 더 필요한 상황입니다. 가능하신 선에서 한 번 더 인하된 가격을 제안해 주시겠어요?" "8": "긍정적으로 검토되고 있습니다. 내부 결재를 위해 명분이 조금 더 필요한 상황입니다. 가능하신 선에서 한 번 더 인하된 가격을 제안해 주시겠어요?"
} }

View File

@ -5,25 +5,37 @@
"editor_script_id": "시작", "editor_script_id": "시작",
"next_input_mode": "null", "next_input_mode": "null",
"input_options": [], "input_options": [],
"next_step": { "default": "서비스안내" }, "next_step": {
"default": "서비스안내"
},
"type": "null", "type": "null",
"chat_end": false "chat_end": false
}, },
"서비스안내": { "서비스안내": {
"script": "안녕하세요. {company_name} {service_name}입니다. 본 서비스는 {company_name}와 협력사 간 물품 공급 가격 협상을 위한 것으로, 귀사가 공급 중인 품목의 새로운 가격 협상을 진행합니다. 안내 사항을 확인하신 뒤, 다음 단계로 넘어가려면 [확인]을 눌러 주세요.", "script": "안녕하세요. {company_name} {service_name}입니다. 본 서비스는 {company_name}와 {label_supplier} 간 물품 공급 가격 협상을 위한 것으로, 귀사가 공급 중인 품목의 새로운 가격 협상을 진행합니다. 안내 사항을 확인하신 뒤, 다음 단계로 넘어가려면 [확인]을 눌러 주세요.",
"editor_script_id": "서비스안내", "editor_script_id": "서비스안내",
"next_input_mode": "confirm", "next_input_mode": "confirm",
"input_options": ["확인"], "input_options": [
"next_step": { "default": "담당자확인" }, "확인"
],
"next_step": {
"default": "담당자확인"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
"담당자확인": { "담당자확인": {
"script": "본 안내는 협력사 포털에 등록된 담당자에게 발송되었습니다. 구매 협상 담당자가 맞는지 다시 한 번 확인 부탁드립니다. 담당자가 맞다면 [예], 맞지 않다면 [아니오]를 선택해 주세요.", "script": "본 안내는 {label_supplier} 포털에 등록된 담당자에게 발송되었습니다. 구매 협상 담당자가 맞는지 다시 한 번 확인 부탁드립니다. 담당자가 맞다면 [예], 맞지 않다면 [아니오]를 선택해 주세요.",
"editor_script_id": "담당자확인", "editor_script_id": "담당자확인",
"next_input_mode": "yes_no", "next_input_mode": "yes_no",
"input_options": ["예", "아니오"], "input_options": [
"next_step": { "예": "협상품목안내", "아니오": "담당자확인_아니오" }, "예",
"아니오"
],
"next_step": {
"예": "협상품목안내",
"아니오": "담당자확인_아니오"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -31,13 +43,19 @@
"script": "[아니오]를 선택하셨습니다. 담당자가 변경되어 정보를 수정하시려면 [정보변경]을, 실수로 선택하신 경우 다시 진행하려면 [돌아가기]를 선택해 주세요.", "script": "[아니오]를 선택하셨습니다. 담당자가 변경되어 정보를 수정하시려면 [정보변경]을, 실수로 선택하신 경우 다시 진행하려면 [돌아가기]를 선택해 주세요.",
"editor_script_id": "담당자확인_아니오", "editor_script_id": "담당자확인_아니오",
"next_input_mode": "yes_no", "next_input_mode": "yes_no",
"input_options": ["돌아가기", "정보변경"], "input_options": [
"next_step": { "돌아가기": "담당자확인", "정보변경": "정보변경_완료" }, "돌아가기",
"정보변경"
],
"next_step": {
"돌아가기": "담당자확인",
"정보변경": "정보변경_완료"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
"정보변경_완료": { "정보변경_완료": {
"script": "[정보변경]을 선택하셨습니다. 협력사 관리 시스템에서 담당자 정보를 변경하신 뒤, 고객센터로 새 견적 생성을 요청해 주세요. 24시간 이내에 갱신되지 않으면 참여 의사가 없는 것으로 간주되어 해당 견적 건이 미참여로 처리될 수 있습니다.", "script": "[정보변경]을 선택하셨습니다. {label_supplier} 관리 시스템에서 담당자 정보를 변경하신 뒤, 고객센터로 새 견적 생성을 요청해 주세요. 24시간 이내에 갱신되지 않으면 참여 의사가 없는 것으로 간주되어 해당 견적 건이 미참여로 처리될 수 있습니다.",
"editor_script_id": "정보변경_완료", "editor_script_id": "정보변경_완료",
"next_input_mode": "null", "next_input_mode": "null",
"input_options": [], "input_options": [],
@ -49,8 +67,12 @@
"script": "{company_name}는 귀사의 협력에 진심으로 감사드립니다. 이번 가격 협상 품목과 기본 정보를 안내드립니다. 좌측의 상품 정보를 확인해 주세요. 협상이 원만히 마무리되면 더 많은 협력 기회가 마련될 수 있습니다.", "script": "{company_name}는 귀사의 협력에 진심으로 감사드립니다. 이번 가격 협상 품목과 기본 정보를 안내드립니다. 좌측의 상품 정보를 확인해 주세요. 협상이 원만히 마무리되면 더 많은 협력 기회가 마련될 수 있습니다.",
"editor_script_id": "협상품목안내", "editor_script_id": "협상품목안내",
"next_input_mode": "confirm", "next_input_mode": "confirm",
"input_options": ["확인"], "input_options": [
"next_step": { "확인": "기존가격제시" }, "확인"
],
"next_step": {
"확인": "기존가격제시"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -59,7 +81,9 @@
"editor_script_id": "기존가격제시", "editor_script_id": "기존가격제시",
"next_input_mode": "price", "next_input_mode": "price",
"input_options": [], "input_options": [],
"next_step": { "default": "가격협상_확인" }, "next_step": {
"default": "가격협상_확인"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -68,22 +92,42 @@
"editor_script_id": "가격협상_재입력", "editor_script_id": "가격협상_재입력",
"next_input_mode": "price", "next_input_mode": "price",
"input_options": [], "input_options": [],
"next_step": { "default": "가격협상_확인" }, "next_step": {
"default": "가격협상_확인"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
"가격협상_확인": { "가격협상_확인": {
"script": "제시하신 가격은 **{input_price}원**으로, 기존 공급가 대비 약 **{discount_rate}%** 인하된 금액입니다. 이 금액으로 제안하시겠습니까? 수정하시려면 [아니오]를 선택해 주세요.", "script": "제시하신 가격은 **{input_price}원**입니다. {discount_phrase}이 금액으로 제안하시겠습니까? 수정하시려면 [아니오]를 선택해 주세요.",
"editor_script_id": "가격협상_확인", "editor_script_id": "가격협상_확인",
"next_input_mode": "yes_no", "next_input_mode": "yes_no",
"input_options": ["예", "아니오"], "input_options": [
"예",
"아니오"
],
"next_step": { "next_step": {
"예": [ "예": [
{ "condition": "check_wildcard_entry", "next": "가격협상_와일드" }, {
{ "condition": "check_is_supplier_type_c", "next": "협상완료" }, "condition": "check_wildcard_entry",
{ "condition": "check_price_match", "next": "협상완료" }, "next": "가격협상_와일드"
{ "condition": "check_iteration_limit", "next": "협상실패" }, },
{ "condition": "default", "next": "가격협상" } {
"condition": "check_is_supplier_type_c",
"next": "협상완료"
},
{
"condition": "check_price_match",
"next": "협상완료"
},
{
"condition": "check_iteration_limit",
"next": "협상실패"
},
{
"condition": "default",
"next": "가격협상"
}
], ],
"아니오": "가격협상_재입력" "아니오": "가격협상_재입력"
}, },
@ -94,8 +138,14 @@
"script": "제시하신 금액은 **{input_price}원**입니다. 이 금액으로 견적을 제출하시겠습니까? 수정하시려면 [아니오]를 선택해 주세요.", "script": "제시하신 금액은 **{input_price}원**입니다. 이 금액으로 견적을 제출하시겠습니까? 수정하시려면 [아니오]를 선택해 주세요.",
"editor_script_id": "가격협상_확인_버짓", "editor_script_id": "가격협상_확인_버짓",
"next_input_mode": "yes_no", "next_input_mode": "yes_no",
"input_options": ["예", "아니오"], "input_options": [
"next_step": { "예": "협상완료", "아니오": "가격협상_재입력" }, "예",
"아니오"
],
"next_step": {
"예": "협상완료",
"아니오": "가격협상_재입력"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -104,7 +154,9 @@
"editor_script_id": "가격협상", "editor_script_id": "가격협상",
"next_input_mode": "price", "next_input_mode": "price",
"input_options": [], "input_options": [],
"next_step": { "default": "가격협상_확인" }, "next_step": {
"default": "가격협상_확인"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -112,8 +164,12 @@
"script": "협조해 주신 덕분에 원만히 협상이 완료되었습니다. 협상 결과를 확인하신 뒤 동의해 주세요. 거래 약정에 따라 일부 조건이 조정될 수 있는 점 참고 부탁드립니다. 성실히 응해 주셔서 감사합니다.", "script": "협조해 주신 덕분에 원만히 협상이 완료되었습니다. 협상 결과를 확인하신 뒤 동의해 주세요. 거래 약정에 따라 일부 조건이 조정될 수 있는 점 참고 부탁드립니다. 성실히 응해 주셔서 감사합니다.",
"editor_script_id": "협상완료", "editor_script_id": "협상완료",
"next_input_mode": "confirm", "next_input_mode": "confirm",
"input_options": ["협상 내용을 확인했으며, 이의가 없음에 동의합니다."], "input_options": [
"next_step": { "default": "협상종료" }, "협상 내용을 확인했으며, 이의가 없음에 동의합니다."
],
"next_step": {
"default": "협상종료"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -122,7 +178,9 @@
"editor_script_id": "협상실패", "editor_script_id": "협상실패",
"next_input_mode": "null", "next_input_mode": "null",
"input_options": [], "input_options": [],
"next_step": { "default": "협상종료" }, "next_step": {
"default": "협상종료"
},
"type": "text", "type": "text",
"chat_end": false "chat_end": false
}, },
@ -134,5 +192,21 @@
"next_step": null, "next_step": null,
"type": "text", "type": "text",
"chat_end": true "chat_end": true
},
"가격협상_카운터": {
"script": "제시해 주신 **{input_price}원** 검토했습니다. 저희는 **{counter_price}원**을 제안드립니다. 이 가격으로 진행 가능하시면 '수락'을, 어려우시면 '다른 가격 제시'를 선택해 주세요.",
"editor_script_id": "가격협상_카운터",
"next_input_mode": "yes_no",
"input_options": [
"수락",
"다른 가격 제시"
],
"next_step": {
"수락": "협상완료",
"다른 가격 제시": "가격협상_재입력",
"default": "가격협상_재입력"
},
"type": "text",
"chat_end": false
} }
} }

View File

@ -10,7 +10,7 @@
"chat_end": false "chat_end": false
}, },
"서비스안내": { "서비스안내": {
"script": "안녕하세요. {company_name} {service_name}입니다. 본 서비스는 {company_name}와 협력사 간 신규 물품 공급 협상을 위한 것으로, 귀사에 새로운 공급 기회를 제공하고자 합니다. 이용 방법 안내를 확인하신 뒤 [확인]을 눌러 주세요.", "script": "안녕하세요. {company_name} {service_name}입니다. 본 서비스는 {company_name}와 {label_supplier} 간 신규 물품 공급 협상을 위한 것으로, 귀사에 새로운 공급 기회를 제공하고자 합니다. 이용 방법 안내를 확인하신 뒤 [확인]을 눌러 주세요.",
"editor_script_id": "서비스안내", "editor_script_id": "서비스안내",
"next_input_mode": "confirm", "next_input_mode": "confirm",
"input_options": ["확인"], "input_options": ["확인"],
@ -19,7 +19,7 @@
"chat_end": false "chat_end": false
}, },
"담당자확인": { "담당자확인": {
"script": "본 안내는 협력사 포털에 등록된 담당자에게 발송되었습니다. 구매 협상 담당자가 맞는지 확인 부탁드립니다. 담당자가 맞다면 [예], 맞지 않다면 [아니오]를 선택해 주세요.", "script": "본 안내는 {label_supplier} 포털에 등록된 담당자에게 발송되었습니다. 구매 협상 담당자가 맞는지 확인 부탁드립니다. 담당자가 맞다면 [예], 맞지 않다면 [아니오]를 선택해 주세요.",
"editor_script_id": "담당자확인", "editor_script_id": "담당자확인",
"next_input_mode": "yes_no", "next_input_mode": "yes_no",
"input_options": ["예", "아니오"], "input_options": ["예", "아니오"],
@ -37,7 +37,7 @@
"chat_end": false "chat_end": false
}, },
"정보변경_완료": { "정보변경_완료": {
"script": "[정보변경]을 선택하셨습니다. 협력사 관리 시스템에서 담당자 정보를 변경하신 뒤 고객센터로 새 견적 생성을 요청해 주세요. 24시간 이내 갱신되지 않으면 미참여로 처리될 수 있습니다.", "script": "[정보변경]을 선택하셨습니다. {label_supplier} 관리 시스템에서 담당자 정보를 변경하신 뒤 고객센터로 새 견적 생성을 요청해 주세요. 24시간 이내 갱신되지 않으면 미참여로 처리될 수 있습니다.",
"editor_script_id": "정보변경_완료", "editor_script_id": "정보변경_완료",
"next_input_mode": "null", "next_input_mode": "null",
"input_options": [], "input_options": [],
@ -46,7 +46,7 @@
"chat_end": true "chat_end": true
}, },
"협상품목안내": { "협상품목안내": {
"script": "{company_name}는 아래 상품에 대해 신규 공급사를 선정하고 있으며, 귀사를 초대하여 견적을 요청드립니다. 제출하신 견적은 복수 업체와의 비교 평가를 통해 공급사 선정에 반영됩니다. 상품 정보를 확인해 주세요.", "script": "{company_name}는 아래 상품에 대해 신규 {label_supplier_를} 선정하고 있으며, 귀사를 초대하여 견적을 요청드립니다. 제출하신 견적은 복수 업체와의 비교 평가를 통해 {label_supplier} 선정에 반영됩니다. 상품 정보를 확인해 주세요.",
"editor_script_id": "협상품목안내", "editor_script_id": "협상품목안내",
"next_input_mode": "confirm", "next_input_mode": "confirm",
"input_options": ["네, 알겠습니다."], "input_options": ["네, 알겠습니다."],
@ -73,10 +73,10 @@
"chat_end": false "chat_end": false
}, },
"배송형태선택": { "배송형태선택": {
"script": "배송 형태를 선택해 주세요.", "script": "{label_delivery_type_를} 선택해 주세요.",
"editor_script_id": "배송형태선택", "editor_script_id": "배송형태선택",
"next_input_mode": "delivery_type", "next_input_mode": "delivery_type",
"input_options": ["협력사배송", "지정택배배송", "픽업배송"], "input_options": ["{label_delivery_type_1}", "{label_delivery_type_2}", "{label_delivery_type_3}"],
"next_step": { "default": "가격협상_입력" }, "next_step": { "default": "가격협상_입력" },
"type": "text", "type": "text",
"chat_end": false "chat_end": false

View File

@ -5,17 +5,39 @@
"type": "text", "type": "text",
"chat_end": false, "chat_end": false,
"next_input_mode": "yes_no", "next_input_mode": "yes_no",
"input_options": ["예", "아니오"], "input_options": [
"next_step": { "default": "협상완료" }, "예",
"아니오"
],
"next_step": {
"default": "협상완료"
},
"editor_script_id": "wild_card_1pct" "editor_script_id": "wild_card_1pct"
}, },
"wild_card_budget": { "wild_card_budget": {
"script": "솔직히 말씀드리면 현재 내부 예산(재원) 사정상 제안을 그대로 수용하기 어렵습니다. 목표 매입가는 **{target}원**입니다. 이 가격에 맞춰 주신다면 즉시 계약을 진행하고자 합니다. 마지막으로 한 번 더 제안 부탁드립니다.", "script": "솔직히 말씀드리면 현재 내부 예산(재원) 사정상 제안을 그대로 수용하기 어렵습니다. 당사 {label_target_price}는 **{target}원**입니다. 이 가격에 맞춰 주신다면 즉시 계약을 진행하고자 합니다. 마지막으로 한 번 더 제안 부탁드립니다.",
"type": "text", "type": "text",
"chat_end": false, "chat_end": false,
"next_input_mode": "price", "next_input_mode": "price",
"input_options": [], "input_options": [],
"next_step": { "default": "가격협상_확인_버짓" }, "next_step": {
"default": "가격협상_확인_버짓"
},
"editor_script_id": "wild_card_budget" "editor_script_id": "wild_card_budget"
},
"wild_card_dynamic": {
"script": "저희는 **{counter_price}원**이면 즉시 진행이 가능합니다. 이 가격으로 진행 가능하시면 '수락'을, 어려우시면 '다른 가격 제시'를 선택해 주세요.",
"next_input_mode": "yes_no",
"input_options": [
"수락",
"다른 가격 제시"
],
"next_step": {
"수락": "협상완료",
"다른 가격 제시": "가격협상_재입력",
"default": "가격협상_재입력"
},
"type": "text",
"chat_end": false
} }
} }

View File

@ -78,7 +78,7 @@ cards:
connection: {} connection: {}
llm: llm:
enabled: false enabled: true
resources: resources:
language: ko language: ko

View File

@ -37,7 +37,9 @@ action_mapping:
"10": "NGC-B011" "10": "NGC-B011"
llm: llm:
enabled: false enabled: true
# api_key_ref 는 미구현(dead) — 현재 LLM 키는 전역 config.local.toml [OpenAIConfig].api_key
# (또는 OPENAI_API_KEY env) 를 사용한다. 테넌트별 키 분리는 표현층(Phase 2) 본작업에서 구현.
api_key_ref: TENANT_B_OPENAI_API_KEY api_key_ref: TENANT_B_OPENAI_API_KEY
resources: resources:

View File

@ -1,32 +0,0 @@
# 데모 테넌트 프로파일 (합성/중립값 — CLEANROOM.md).
# tenant_id 는 라우팅 키일 뿐이며, 아래 값은 해당 회사의 실제 운영값이 아니다.
# 실제 운영 시 카드 카탈로그(card.nego_cards)·튜닝값은 테넌트 비공개 소스에서 주입한다.
tenant_id: ktcommerce
inherits_base: true
name: "Demo Tenant A"
company_id: null # P3 시드 시 company.companies.company_id(uuid) 로 채움
action_mapping:
type: file
action_to_card: # 우리 중립 데모 카드 코드(합성). 실제 카드 코드 아님. 카탈로그 11장(_base 와 정합).
"0": "NGC-A001"
"1": "NGC-A002"
"2": "NGC-A003"
"3": "NGC-A004"
"4": "NGC-A005"
"5": "NGC-A006"
"6": "NGC-A007"
"7": "NGC-A008"
"8": "NGC-A009"
"9": "NGC-A010"
"10": "NGC-A011"
llm:
enabled: false # P7 에서 테넌트별 자격증명 주입
api_key_ref: TENANT_A_OPENAI_API_KEY
resources:
language: ko
scripts_dir: resources # 없으면 _base/resources 폴백
company_name: "데모상사 A" # 스크립트 {company_name} 치환값 (합성)
service_name: "Negosium"

View File

@ -0,0 +1,157 @@
"""협상 퍼즈 하네스 — 랜덤 조건·랜덤 협력사 행동으로 N회 완주시키고 불변식 위반을 수집한다.
시드 고정(재현 가능). test_ 접두사 없음 pytest 수집 대상 아님, 수동 실행 전용:
docker run --rm -v $PWD/agent:/work -w /work -e APP_ENV=local -e DB_HOST=host.docker.internal \
o2o-negosium-agent sh -lc "pip install -q pytest pytest-asyncio httpx; python tests/fuzz_negotiation.py"
케이스마다 검사하는 불변식:
1. success
2. 같은 카드 2 발동 금지
3. 종결 전용(WC-03·05) 가격협상_카운터에서만 / 비종결 와일드는 wild_card_dynamic 에서만
4. 타결 타결가 목표가
5. 카운터/1% 수락으로 타결하면 멘트에 타결가 표기
6. 멘트·버튼에 미치환 토큰({xxx}) 잔존 금지
7. 상한(60) 안에 반드시 종료
"""
import asyncio
import random
import re
import sys
import uuid
sys.path.insert(0, "/work")
from router.v1.chat.protocol import Req_Chat # noqa: E402
from services.chat_service import ChatService, reset_sessions # noqa: E402
from tenancy.config_loader import TenantConfigLoader # noqa: E402
from tenancy.registry import TenantEngineRegistry # noqa: E402
from tests.test_card_tactics import _TENANTS_DIR, _cleanup, _seed_quote_session # noqa: E402
N = 100
SEED = 20260805
TARGET = 10_000
NEGO_POOL = ["NGC-001", "NGC-002", "NGC-003", "NGC-004", "NGC-005",
"NGC-007", "NGC-008", "NGC-010", "NGC-011"]
WILD_POOL = ["WC-01", "WC-02", "WC-03", "WC-04", "WC-05"]
CLOSING = {"WC-03", "WC-05"}
TOKEN_RE = re.compile(r"(?<!\{)\{([a-z_0-9]+)\}(?!\})")
class Supplier:
"""랜덤 협력사 — 높은 시작가에서 점진 양보, 카운터는 확률적으로 수락/거절."""
def __init__(self, rng, anchor):
self.rng = rng
self.anchor = anchor
self.price = TARGET * rng.uniform(1.02, 1.30)
self.accept_p = rng.uniform(0.15, 0.5)
def next_price(self):
p = int(self.price)
# 다음 라운드를 위해 양보 — 가끔 앵커 밑까지 다이브(우선협상 유도).
self.price *= self.rng.uniform(0.90, 0.99)
if self.rng.random() < 0.15:
self.price = self.anchor * self.rng.uniform(0.95, 1.04)
return str(max(p, 100))
def choose(self, options):
if "수락" in options:
return "수락" if self.rng.random() < self.accept_p else "다른 가격 제시"
if set(options) >= {"", "아니오"}:
return "" if self.rng.random() < max(self.accept_p, 0.5) else "아니오"
return options[0] if options else "확인"
async def run_case(idx, rng):
anchor = int(TARGET * rng.choice([0.99, 0.99, 0.97, 0.95, 1.0]))
nego = rng.sample(NEGO_POOL, rng.randint(1, 5))
wild = rng.sample(WILD_POOL, rng.randint(0, 5))
sup = Supplier(rng, anchor)
reset_sessions()
sid = uuid.uuid4()
qid, ver = await _seed_quote_session(sid, nego, wild_numbers=wild, target=TARGET, anchor=anchor)
violations, fired, settled, outcome, ended = [], [], None, None, False
try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(uuid.uuid4()))
svc = ChatService()
ui, last_input = None, None
for _turn in range(60):
r = await svc.chat(eng, Req_Chat(session_id=str(sid), user_input=ui))
if r.result.success is not True:
violations.append(f"턴 실패 input={ui} msg={r.msg}")
break
script, opts = r.script or "", list(r.input_options or [])
if TOKEN_RE.search(script):
violations.append(f"미치환 토큰(script): {TOKEN_RE.findall(script)} @ {r.step}")
for o in opts:
if TOKEN_RE.search(o):
violations.append(f"미치환 토큰(option): {o} @ {r.step}")
if r.card_id:
fired.append((r.step, r.card_id))
if r.settled_price is not None:
settled = r.settled_price
# 카운터/1% '수락' 타결이면 마지막 카운터 멘트에 타결가가 보였어야 한다.
if last_input in ("수락",) and str(settled) not in (last_counter or ""):
violations.append(f"표시가≠타결가: {settled} not in counter script")
if r.step in ("가격협상_카운터", "wild_card_dynamic", "wild_card_1pct"):
last_counter = script
if r.chat_end:
outcome, ended = r.outcome, True
break
# 다음 입력 결정
last_input = None
if r.input_mode == "price":
ui = sup.next_price()
elif opts:
ui = sup.choose(opts)
last_input = ui
else:
ui = "확인"
if not ended:
violations.append("60턴 내 미종료")
# 카드 불변식
ids = [c for _, c in fired]
if len(ids) != len(set(ids)):
violations.append(f"카드 중복: {ids}")
for step, c in fired:
if c in CLOSING and step != "가격협상_카운터":
violations.append(f"종결 카드 {c}{step} 에서 발동")
if c.startswith("WC") and c not in CLOSING and step != "wild_card_dynamic":
violations.append(f"비종결 와일드 {c}{step} 에서 발동")
if outcome == "success":
if settled is None:
violations.append("성공인데 settled 없음")
elif settled > TARGET:
violations.append(f"목표가 초과 타결: {settled}")
finally:
await _cleanup(sid, qid, ver)
return {"idx": idx, "anchor": anchor, "nego": nego, "wild": wild,
"fired": fired, "settled": settled, "outcome": outcome, "violations": violations}
async def main():
rng = random.Random(SEED)
results, bad = [], []
for i in range(N):
res = await run_case(i, random.Random(rng.random()))
results.append(res)
if res["violations"]:
bad.append(res)
tag = "OK " if not res["violations"] else "BAD"
print(f"[{tag}] #{i:02d} anchor={res['anchor']} nego={len(res['nego'])} wild={len(res['wild'])} "
f"fired={''.join(c for _, c in res['fired']) or '-'} settled={res['settled']} {res['outcome']}")
ok = sum(1 for r in results if not r["violations"])
succ = sum(1 for r in results if r["outcome"] == "success")
print(f"\n===== {ok}/{N} clean · 타결 {succ} / 결렬 {N - succ} =====")
for r in bad:
print(f"\n#{r['idx']} 위반: nego={r['nego']} wild={r['wild']} anchor={r['anchor']}")
for v in r["violations"]:
print(" -", v)
from common.database.db_session_manager import DB_SESSION_MNG
await DB_SESSION_MNG.dispose_all()
sys.exit(0 if not bad else 1)
asyncio.run(main())

View File

@ -29,7 +29,7 @@ def _reg():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_4_1_honors_backend_session_id(db_engine): async def test_4_1_honors_backend_session_id(db_engine):
reset_sessions() reset_sessions()
eng = await _reg().get_engine("ktcommerce") eng = await _reg().get_engine("imarketkorea")
svc = ChatService() svc = ChatService()
# 첫 턴: backend 의 session_id 를 그대로 키로 써야 함 (새 uuid 발급 X) # 첫 턴: backend 의 session_id 를 그대로 키로 써야 함 (새 uuid 발급 X)
@ -53,7 +53,7 @@ async def test_4_4_company_id_auto_onboard():
eng = await _reg().get_engine(COMPANY_ID) eng = await _reg().get_engine(COMPANY_ID)
assert eng.tenant_id == COMPANY_ID assert eng.tenant_id == COMPANY_ID
assert eng.company_id == COMPANY_ID # 학습/세션이 이 company_id 로 격리 assert eng.company_id == COMPANY_ID # 학습/세션이 이 company_id 로 격리
assert eng.action_space_size == 11 # base 기본 카드(162×11 정합) assert eng.action_space_size == 9 # DB 카탈로그 9장(NGC-006·009 소프트삭제)
assert eng.state_space_size == 162 assert eng.state_space_size == 162

View File

@ -0,0 +1,154 @@
"""협상카드 선택 E2E — 실 DB 왕복으로 "견적에서 고른 카드만 뽑히는지" 검증.
시나리오: 견적 생성 협상카드 2(NGC-003, NGC-007) 선택
version_nego_cards 연결 협상 세션 시작 가격협상 2 진행.
검증: 뽑힌 카드가 선택 2 안에서만 나옴(선택 마스크) 세션 중복 없음(사용 마스크)
카탈로그(DB, NGC-001~011) 기준 action space 선택 없으면 전체 카탈로그 허용(폴백).
"""
import os
import uuid as _uuid
from datetime import datetime, timedelta, timezone
import pytest
from sqlalchemy import column, delete, insert, select, table
from common.database.db_session_manager import DB_SESSION_MNG
from common.enums import DBType, DBWRType, ErrorType
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
_T_SESSIONS = table(
"sessions",
column("session_id"), column("quotation_id"), column("item_id"), column("supplier_id"),
column("qt_number"), column("qt_round"), column("qt_type"), column("target_price"),
column("anchoring_price"), column("status"), column("end_time"),
schema="negotiation",
)
_T_QUOTATIONS = table(
"quotations",
column("qt_id"), column("user_id"), column("qt_setting_id"), column("version_id"),
column("name"), column("number"), column("type"), column("status"),
column("start_time"), column("end_time"),
schema="quotation",
)
_T_VNC = table(
"version_nego_cards",
column("vnc_id"), column("version_id"), column("nego_card_id"),
schema="card",
)
_T_NEGO = table("nego_cards", column("nego_card_id"), column("number"), column("deleted"), schema="card")
async def _card_uuid(number: str):
def _q(s):
return DB_SESSION_MNG.execute(
s, select(_T_NEGO.c.nego_card_id).where(
_T_NEGO.c.number == number, _T_NEGO.c.deleted == False).limit(1)) # noqa: E712
_, rows = await DB_SESSION_MNG.execute_lambda(DBType.MAIN.value, DBWRType.DB_READ.value, _q)
return rows[0] if rows else None
@pytest.mark.asyncio
async def test_selected_cards_only_are_played(db_engine):
reset_sessions()
sid, qid, ver_id = _uuid.uuid4(), _uuid.uuid4(), _uuid.uuid4()
iid, sup = _uuid.uuid4(), _uuid.uuid4()
now = datetime.now(timezone.utc)
selected = ["NGC-003", "NGC-007"]
card_ids = {}
for n in selected:
card_ids[n] = await _card_uuid(n)
assert card_ids[n] is not None, f"카탈로그에 {n} 없음(시드 확인)"
def _seed(s_):
async def run(s):
e = await DB_SESSION_MNG.add(s, insert(_T_QUOTATIONS).values(
qt_id=qid, user_id=_uuid.uuid4(), qt_setting_id=_uuid.uuid4(), version_id=ver_id,
name="카드선택E2E", number="QT-CARDSEL-E2E", type=1, status=2,
start_time=now, end_time=now + timedelta(days=1)))
if e != ErrorType.SUCCESS:
return e
for n in selected: # 견적 생성 시 선택한 카드 2장
e = await DB_SESSION_MNG.add(s, insert(_T_VNC).values(
vnc_id=_uuid.uuid4(), version_id=ver_id, nego_card_id=card_ids[n]))
if e != ErrorType.SUCCESS:
return e
return await DB_SESSION_MNG.add(s, insert(_T_SESSIONS).values(
session_id=sid, quotation_id=qid, item_id=iid, supplier_id=sup,
qt_number="QT-CARDSEL-E2E", qt_round=1, qt_type=1,
target_price=10000, anchoring_price=9900, status=2,
end_time=now + timedelta(days=1)))
return run(s_)
err = await DB_SESSION_MNG.execute_lambda_run([DBType.MAIN.value], [_seed])
assert err == ErrorType.SUCCESS
try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(_uuid.uuid4())) # 자동 온보딩(_base type:db → 실 DB 카탈로그)
assert eng.action_space_size == 9 # 카탈로그 9장(NGC-006·009 소프트삭제)
svc = ChatService()
played = []
session_id = str(sid)
# 적응형 진행: 카드 전술 재설계 후 카운터 제시 카드(NGC-007 등)는 수락/거절 스텝
# (가격협상_카운터)으로 전환된다 — 거절하고 새 가격을 제시하며 카드 2턴을 유도한다.
prices = iter(["11000", "10600", "10400"])
r = await svc.chat(eng, Req_Chat(session_id=session_id))
for _ in range(14):
if r.step in ("가격협상", "가격협상_카운터") and r.card_id:
played.append(r.card_id)
if len(played) == 2:
break
if r.chat_end:
break
if r.input_mode == "price":
ui = next(prices)
elif r.step == "가격협상_카운터":
ui = "다른 가격 제시"
elif r.input_options:
ui = "" if "" in r.input_options else r.input_options[0]
else:
ui = "확인"
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=ui))
assert len(played) == 2, f"가격협상 카드 턴 2회 기대, 실제 {played}"
# ① 선택한 카드 안에서만 뽑힘 ② 세션 내 중복 없음
assert set(played) <= set(selected), f"선택 밖 카드 발동: {played}"
assert len(set(played)) == 2, f"카드 중복 사용: {played}"
finally:
await DB_SESSION_MNG.execute_lambda_run(
[DBType.MAIN.value],
[lambda s: DB_SESSION_MNG.add(s, delete(_T_SESSIONS).where(_T_SESSIONS.c.session_id == sid)),
lambda s: DB_SESSION_MNG.add(s, delete(_T_VNC).where(_T_VNC.c.version_id == ver_id)),
lambda s: DB_SESSION_MNG.add(s, delete(_T_QUOTATIONS).where(_T_QUOTATIONS.c.qt_id == qid))],
)
@pytest.mark.asyncio
async def test_no_selection_allows_full_catalog(db_engine):
"""선택 카드가 없으면(직접호출/데모) 전체 카탈로그가 허용된다 — 카드가 정상적으로 뽑히는지 기본 검증."""
reset_sessions()
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(_uuid.uuid4())) # _base type:db → DB 카탈로그
catalog = {eng.mapper.get_card_id(a) for a in range(eng.action_space_size)}
svc = ChatService()
played = []
sid = None
for ui in [None, "확인", "", "확인", "11000", "", "10600", "", "10600", ""]:
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input=ui))
sid = r.session_id
if r.step == "가격협상" and r.card_id:
played.append(r.card_id)
if r.chat_end:
break
assert played, "가격협상 카드 턴이 발생해야 함"
assert set(played) <= catalog # 카탈로그(NGC-001~011) 내에서만
assert len(played) == len(set(played)) # 세션 내 중복 없음

View File

@ -0,0 +1,499 @@
"""카드 전술 검증 — "스크립트에 꽂힌 변수가 곧 전술" (파싱 + 변수별 유효조건 + tactic JSONB).
제안가 파싱(마지막 제안가 변수) + 변수별 계산식 결정론
변수 공통 유효조건 목표가 초과·제시가 이상이면 미발동(클램프 아님 IMK 8AB0 회귀)
카운터 수락 = 즉시 타결 / 거절 = 재입력 + pending 폐기
목표가 초과 타결 금지 가드(성공 스텝 진입 차단)
와일드 진입 종결 전용 카드 예약(중반 미발동) + 카드 이력 공유(중복 발동 차단, IMK BB9A 회귀)
E2E: 견적 선택 카드(NGC-010 목표가 제안) 카운터를 수락하면 settled=target
E2E: 협력사가 target 초과를 고수하면 종결 전술(최후통첩) 결렬 고객사 이득 가드레일
"""
import os
import uuid as _uuid
from datetime import datetime, timedelta, timezone
import pytest
from negotiation.cards.domain.tactics import (
CardSpec, HOLD, available, build_card_spec, compute_offer,
is_played, mark_played, parse_offer_variable, playable, spec_from_context,
)
from negotiation.chat.service.chat_engine import ChatEngine, ChatSession
from negotiation.chat.service.script_repository import ScriptRepository
from tenancy.config_loader import TenantConfigLoader
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
# 엔진 단위 테스트용 카드 스펙(로더가 DB 스크립트 파싱으로 만드는 것과 같은 형태).
_SPECS = {
"WC-02": {"offer_variable": "target_mid_price", "min_round": 1, "closing": False},
"WC-05": {"offer_variable": "middle_price", "min_round": 1, "closing": True},
}
def _engine() -> ChatEngine:
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
return ChatEngine(ScriptRepository(cfg, _TENANTS_DIR), rq_type="재협상")
def _session(step="가격협상_확인", **ctx_over):
ctx = {"input_price": 10300, "anchor_price": 10000, "target_price": 10100,
"round": 1, "allow_selected_wildcards": False, "card_specs": dict(_SPECS)}
ctx.update(ctx_over)
return ChatSession(session_id="00000000-0000-0000-0000-00000000e001", tenant_id="imarketkorea",
company_id="imarketkorea", step=step, action_space_size=0, context=ctx)
# ---- ① 제안가 파싱 + 계산식 ---------------------------------------------------
def test_parse_offer_variable_last_offer_wins():
"""제안가 변수가 여럿이면 마지막 것 — 카드 문장은 배경을 먼저, 제안을 마지막에 한다(WC-04)."""
assert parse_offer_variable("적정가는 {anchoring_price}원이었으나 {target_price}원으로 제안") == "target_price"
assert parse_offer_variable("{target_price}원을 제안 드립니다") == "target_price"
# 읽어주기 변수만 있으면 설득 카드 — 제안가 없음
assert parse_offer_variable("시장가 {internet_lowest_price}원 안팎, 제시가 {prev_partner_price}") is None
assert parse_offer_variable("가격 변수 없는 설득 멘트") is None
assert parse_offer_variable(None) is None
# WC-05 정본: 읽어주기(직전 제안·제시가) 뒤 절충가 제안
assert parse_offer_variable("당사 제안 {prev_customer_price}원과 귀사 제안 {prev_partner_price}원을 절반씩, {middle_price}원으로") == "middle_price"
# negodata 에디터 칩 표기(anchor_price)도 앵커가 제안으로 인식 — DB 시드 표기(anchoring_price)의 별칭
assert parse_offer_variable("예산 한도는 {anchor_price}원입니다") == "anchor_price"
def test_build_card_spec_merges_script_and_tactic():
spec = build_card_spec("{target_price}원으로 제안", {"min_round": 2, "closing": True})
assert spec == CardSpec(offer_variable="target_price", min_round=2, closing=True)
# tactic 없음 → 기본값. offer_variable override 는 파싱보다 우선.
assert build_card_spec("설득 멘트", None) == HOLD
assert build_card_spec("멘트", {"offer_variable": "anchoring_price"}).offer_variable == "anchoring_price"
def test_offer_formulas():
ctx = {"input_price": 11000, "anchor_price": 9900, "target_price": 10000}
offer = lambda var, c=None: compute_offer(CardSpec(offer_variable=var), c or ctx) # noqa: E731
assert offer("target_price") == 10000
assert offer("anchoring_price") == 9900
assert offer("target_mid_price") == 9950 # (anchor+target)/2
# 절충가: 갑 직전 포지션 폴백 = anchor → (8900+9500)/2 = 9200
assert offer("middle_price", dict(ctx, input_price=9500, anchor_price=8900)) == 9200
# 갑 직전 포지션이 있으면 그 기준: (9000+9500)/2 = 9250
assert offer("middle_price", dict(ctx, input_price=9500, prev_customer_price=9000)) == 9250
# ---- ② 변수 공통 유효조건 — 미발동(클램프 아님) --------------------------------
def test_offer_over_target_does_not_fire_imk_8ab0():
"""IMK 8AB0 회귀: 목표가 9,000 / 제시가 9,500 → 절충가 (8,910+9,500)/2 = 9,205 > 목표가.
구현이 목표가로 깎아 부르면 '중간에서 만나자며 목표가를 부르는' 모순 클램프가 아니라 미발동이 정답."""
ctx = {"input_price": 9500, "anchor_price": 8910, "target_price": 9000}
assert compute_offer(CardSpec(offer_variable="middle_price"), ctx) is None
def test_offer_at_or_above_input_price_does_not_fire():
"""협력사 제시가가 이미 제안가 이하면 부를 이유가 없다 → 미발동."""
ctx = {"input_price": 9950, "anchor_price": 9900, "target_price": 10000}
assert compute_offer(CardSpec(offer_variable="target_price"), ctx) is None # target ≥ 제시가
assert compute_offer(CardSpec(offer_variable="anchoring_price"), dict(ctx, input_price=9900)) is None
def test_offer_without_materials_does_not_fire():
"""재료 결측(목표가·제시가·앵커) — 어떤 변수도 미발동."""
assert compute_offer(CardSpec(offer_variable="target_price"), {"input_price": 11000}) is None # 목표가 없음
assert compute_offer(CardSpec(offer_variable="anchoring_price"),
{"input_price": 11000, "target_price": 10000}) is None # 앵커 없음
assert compute_offer(HOLD, {"input_price": 11000, "target_price": 10000}) is None # 설득 카드
assert compute_offer(CardSpec(offer_variable="없는변수"), {"input_price": 11000, "target_price": 10000}) is None
def test_available_min_round_and_closing_phase():
spec2 = CardSpec(offer_variable="target_price", min_round=2)
assert available(spec2, {"round": 1}) is False # min_round 미만
assert available(spec2, {"round": 2}) is True
closing = CardSpec(offer_variable="middle_price", closing=True)
assert available(closing, {"round": 1}) is False # 종결 전용 — 중반 미발동(예약)
assert available(closing, {"round": 1}, closing_phase=True) is True
assert available(spec2, {"round": 3}, closing_phase=True) is False # 종결 국면엔 종결 카드만
def test_tactic_offer_variable_overrides_parse():
"""검증: tactic.offer_variable 명시 지정(negodata 셀렉트) — 파싱(마지막 변수) 대신 지정 변수 사용.
기대결과: 멘트 마지막이 target_price 여도 지정한 anchoring_price 제안가 변수가 된다."""
script = "적정가는 {anchoring_price}원이었으나 {target_price}원으로 제안 드립니다."
assert build_card_spec(script).offer_variable == "target_price" # 자동: 마지막 변수
spec = build_card_spec(script, {"offer_variable": "anchoring_price"})
assert spec.offer_variable == "anchoring_price" # 명시 지정이 우선
def test_available_requires_context_value():
"""검증: 시장가 인용 카드(NGC-008류)의 requires 게이트 — build_card_spec 이 스크립트에서 잡아내고,
기대결과: 컨텍스트에 인터넷 최저가가 없으면(0/결측) 미발동, 있으면 발동(퍼즈 #3·13·23·40 회귀)."""
spec = build_card_spec("유사 거래는 {internet_lowest_price}원 안팎에서 합의되고 있습니다.")
assert spec.requires == ("internet_lowest_price",)
assert available(spec, {"round": 1}) is False # 결측
assert available(spec, {"round": 1, "internet_lowest_price": 0}) is False # 미수집(0)
assert available(spec, {"round": 1, "internet_lowest_price": 6300}) is True
# 일반 카드는 requires 없음 — 기존 동작 그대로.
assert build_card_spec("귀사와의 협력을 소중히 생각합니다.").requires == ()
def test_offer_monotonic_no_regression():
"""검증: 역행 금지(IMK 논의 — 절충 16,980 후 예산 상한 16,810 제시) 재현.
기대결과: 직전 당사 제안보다 낮은 제안가 카드는 미발동(설득 폴백으로도 나감).
직전 제안이 없으면 앵커 제시 허용, 같은 금액 재제시 허용, 높은 제안은 정상."""
anchor_card = CardSpec(offer_variable="anchoring_price")
ctx = {"round": 2, "target_price": 17_300, "anchor_price": 16_810, "input_price": 17_500}
assert compute_offer(anchor_card, ctx) == 16_810 # 첫 카운터 전(포지션=앵커): 같은 금액 → 허용
ctx["prev_customer_price"] = 16_980 # 절충 카드가 이미 16,980 을 부른 상태
assert compute_offer(anchor_card, ctx) is None # 앵커 16,810 은 역행 → 미발동
assert playable(anchor_card, ctx) is False # 멘트에 금액이 박히므로 설득 폴백도 금지
assert compute_offer(CardSpec(offer_variable="target_price"), ctx) == 17_300 # 상향 제안은 정상
def test_played_history_is_shared_by_number():
ctx = {}
assert is_played(ctx, "WC-05") is False
mark_played(ctx, "WC-05")
assert is_played(ctx, "WC-05") is True
mark_played(ctx, "WC-05") # 재기록해도 1건 유지
assert ctx["played_card_numbers"] == ["WC-05"]
mark_played(ctx, None) # no-op(폴백 최후통첩)
assert ctx["played_card_numbers"] == ["WC-05"]
def test_spec_from_context_reads_snapshot_and_falls_back_to_hold():
ctx = {"card_specs": dict(_SPECS)}
assert spec_from_context(ctx, "WC-05") == CardSpec(offer_variable="middle_price", min_round=1, closing=True)
assert spec_from_context(ctx, "NGC-B003") == HOLD # 미등록 카드(데모) 폴백
assert spec_from_context({}, "WC-05") == HOLD # 스펙 미적재(구세션·데모) 폴백
# ---- ③ 카운터 수락/거절 메커니즘 (엔진) ---------------------------------------
def test_accept_counter_settles_at_counter_price():
eng = _engine()
s = _session(step="가격협상_카운터", pending_counter_price=10000)
view = eng.advance(s, "수락")
assert view.step == "협상완료"
assert s.context["input_price"] == 10000 # 합의가 = 카운터가
assert "pending_counter_price" not in s.context
def test_reject_counter_reenters_price_and_discards_pending():
eng = _engine()
s = _session(step="가격협상_카운터", pending_counter_price=10000)
view = eng.advance(s, "다른 가격 제시")
assert view.step == "가격협상_재입력"
# 새 가격 입력이 pending 을 폐기한다 — 이후 우선협상 타결이 옛 카운터로 오염되지 않음
view = eng.advance(s, "9900")
assert "pending_counter_price" not in s.context
assert s.context["input_price"] == 9900
def test_wildcard_1pct_decline_keeps_original_price():
"""1% 인하 거절('아니오')도 협상완료로 가지만 합의가는 원 제시가 — pending 미적용 회귀."""
eng = _engine()
s = _session(step="wild_card_1pct", input_price=10000,
offer_1pct=9900, pending_counter_price=9900)
view = eng.advance(s, "아니오")
assert view.step == "협상완료"
assert s.context["input_price"] == 10000 # 거절 → 카운터 미적용
# ---- ④ 목표가 초과 타결 금지 가드 --------------------------------------------
def test_success_step_guard_rejects_over_target():
eng = _engine()
s = _session(input_price=10800, target_price=10000)
view = eng.render_step(s, "협상완료")
assert view.step == "협상실패" # 초과가 성공 진입 → 결렬 강제
# ---- ⑤ 와일드 진입 — 종결 예약 + 중복 차단 (IMK BB9A 회귀) ---------------------
def test_selected_wildcard_fires_in_entry_zone_and_records_position():
eng = _engine()
# 10300: 1pct 존(≤10200) 밖, entry 존(≤10500) 안 + 비종결 WC-02 선택
s = _session(input_price=10300, allow_selected_wildcards=True,
selected_wild_card_numbers=["WC-02"])
view = eng.advance(s, "")
assert view.step == "wild_card_dynamic"
# 제안가 = (anchor 10000 + target 10100)/2 = 10050 ≤ target — 그대로 제시(클램프 없음)
assert s.context["pending_counter_price"] == 10050
assert s.context["prev_customer_price"] == 10050 # 갑 포지션 기록 — "당사 제안" 멘트 정합(BB9A ③)
assert s.context["active_wild_card_number"] == "WC-02"
assert is_played(s.context, "WC-02") # 카드 이력 기록
# 수락 → 그 가격으로 타결
view = eng.advance(s, "수락")
assert view.step == "협상완료" and s.context["input_price"] == 10050
def test_closing_card_is_reserved_never_fires_mid_negotiation():
"""종결 전용 카드(WC-05)는 entry 존이라도 중반에 안 나간다 — 종결 국면의 마지막 한 방으로 예약.
(BB9A 중복의 절반: 중반에 당겨 카드를 종결에서 쓰던 경로 차단.)"""
eng = _engine()
s = _session(input_price=10300, allow_selected_wildcards=True,
selected_wild_card_numbers=["WC-05"])
view = eng.advance(s, "")
assert view.step == "가격협상" # 종결 카드뿐 → 일반 카드 플레이로
assert "active_wild_card_number" not in s.context
assert not is_played(s.context, "WC-05") # 안 나갔으니 이력도 없음
# 종결 국면에선 발동 가능 + 이력 없음 — 서비스 종결 루프가 이 카드를 쓴다
spec = spec_from_context(s.context, "WC-05")
assert available(spec, s.context, closing_phase=True) is True
def test_played_wildcard_is_skipped_on_reentry():
"""이미 쓴 카드는 같은 협상에서 다시 안 나간다 — 다음 후보로 넘어간다."""
eng = _engine()
s = _session(input_price=10300, allow_selected_wildcards=True, wildcard_used=False,
selected_wild_card_numbers=["WC-02"], played_card_numbers=["WC-02"])
view = eng.advance(s, "")
assert view.step == "가격협상" # 유일 후보가 사용됨 → 발동 없음
def test_unselected_wildcard_zone_still_falls_to_nego():
"""와일드카드 미선택이면 entry 존이라도 일반 가격협상 — 기존 동작 보존."""
eng = _engine()
s = _session(input_price=10300, allow_selected_wildcards=True, selected_wild_card_numbers=[])
view = eng.advance(s, "")
assert view.step == "가격협상"
# ---- 인하율 표기 (회귀: 인상 제시가 "-1.3% 인하"로 표기되던 버그) -----------------
def test_discount_never_negative_and_phrase_matches_direction():
eng = _engine()
# 인상 제시(기존 공급가 78000 < 제시 79000): 음수 인하율 금지 + "높은 금액" 문구
s = _session(item_price=78000, input_price=79000)
v = eng.vars_for(s)
assert v["discount_rate"] == "0.0" # 마이너스 인하 표기 금지
assert "높은 금액" in v["discount_phrase"] and "78000원" in v["discount_phrase"]
assert "-" not in v["discount_phrase"]
# 인하 제시: 상품단가(item_price) 기준 인하율
v = eng.vars_for(_session(item_price=78000, input_price=77000))
assert v["discount_rate"] == "1.3"
assert "인하된 금액" in v["discount_phrase"]
# 동일가
v = eng.vars_for(_session(item_price=78000, input_price=78000))
assert "동일한 수준" in v["discount_phrase"]
# 기존가 미보유(신규 협상) → 문구 생략
v = eng.vars_for(_session(item_price=0, input_price=79000))
assert v["discount_phrase"] == "" and v["discount_rate"] == "0.0"
def test_price_confirm_script_renders_raise_correctly():
"""가격협상_확인 멘트 E2E — 인상 제시에 '인하' 표현이 나오지 않는다."""
eng = _engine()
s = _session(step="기존가격제시", item_price=78000, input_price=None, round=0)
s.context.pop("input_price")
view = eng.advance(s, "79000")
assert view.step == "가격협상_확인"
assert "인하" not in view.script # 인상인데 '인하' 금지
assert "높은 금액" in view.script and "79000원" in view.script
# ---- vars_for 전술 변수 치환 ---------------------------------------------------
def test_vars_for_supplies_tactic_variables():
eng = _engine()
# 카운터 미제시(정보성): 절충/중간 변수는 원 계산값.
s0 = _session(input_price=10300, prev_customer_price=10000) # anchor=10000, target=10100 (기본)
v0 = eng.vars_for(s0)
assert v0["prev_partner_price"] == 10300
assert v0["prev_customer_price"] == 10000
assert v0["target_mid_price"] == 10050 # (anchor 10000 + target 10100)/2
assert v0["middle_price"] == 10150 # (prev_customer 10000 + input 10300)/2
# 카운터 제시 중: 표시 제시가(middle/target_mid/counter) == 타결가(pending) 로 고정.
# 회귀(표시가≠투찰가): 예전엔 middle_price 가 재계산값 10150 을 표시하면서 10100 으로 타결됐다.
s1 = _session(input_price=10300, prev_customer_price=10000, pending_counter_price=10100)
v1 = eng.vars_for(s1)
assert v1["counter_price"] == 10100
assert v1["middle_price"] == 10100 # 재계산 10150 이 아니라 pending
assert v1["target_mid_price"] == 10100
# ---- ⑥⑦ E2E (실 DB — 견적 선택 카드 + 서비스 레이어) ---------------------------
from sqlalchemy import column, delete, insert, select, table # noqa: E402
from common.database.db_session_manager import DB_SESSION_MNG # noqa: E402
from common.enums import DBType, DBWRType, ErrorType # noqa: E402
from router.v1.chat.protocol import Req_Chat # noqa: E402
from services.chat_service import ChatService, reset_sessions # noqa: E402
from tenancy.registry import TenantEngineRegistry # noqa: E402
_T_SESSIONS = table(
"sessions",
column("session_id"), column("quotation_id"), column("item_id"), column("supplier_id"),
column("qt_number"), column("qt_round"), column("qt_type"), column("target_price"),
column("anchoring_price"), column("status"), column("end_time"),
schema="negotiation",
)
_T_QUOTATIONS = table(
"quotations",
column("qt_id"), column("user_id"), column("qt_setting_id"), column("version_id"),
column("name"), column("number"), column("type"), column("status"),
column("start_time"), column("end_time"),
schema="quotation",
)
_T_VNC = table("version_nego_cards", column("vnc_id"), column("version_id"), column("nego_card_id"), schema="card")
_T_NEGO = table("nego_cards", column("nego_card_id"), column("number"), column("deleted"), schema="card")
_T_VWC = table("version_wild_cards", column("vwc_id"), column("version_id"), column("wild_card_id"), schema="card")
_T_WILD = table("wild_cards", column("wild_card_id"), column("number"), column("deleted"), schema="card")
async def _card_uuid(number: str, *, wild=False):
tbl, pk = (_T_WILD, _T_WILD.c.wild_card_id) if wild else (_T_NEGO, _T_NEGO.c.nego_card_id)
def _q(s):
return DB_SESSION_MNG.execute(
s, select(pk).where(tbl.c.number == number, tbl.c.deleted == False).limit(1)) # noqa: E712
_, rows = await DB_SESSION_MNG.execute_lambda(DBType.MAIN.value, DBWRType.DB_READ.value, _q)
return rows[0] if rows else None
async def _seed_quote_session(sid, selected_numbers, wild_numbers=(), target=10000, anchor=9900):
qid, ver_id, iid, sup = _uuid.uuid4(), _uuid.uuid4(), _uuid.uuid4(), _uuid.uuid4()
now = datetime.now(timezone.utc)
card_ids, wild_ids = {}, {}
for n in selected_numbers:
card_ids[n] = await _card_uuid(n)
assert card_ids[n] is not None, f"카탈로그에 {n} 없음(시드 확인)"
for n in wild_numbers:
wild_ids[n] = await _card_uuid(n, wild=True)
assert wild_ids[n] is not None, f"카탈로그에 {n} 없음(시드 확인)"
def _seed(s_):
async def run(s):
e = await DB_SESSION_MNG.add(s, insert(_T_QUOTATIONS).values(
qt_id=qid, user_id=_uuid.uuid4(), qt_setting_id=_uuid.uuid4(), version_id=ver_id,
name="전술E2E", number=f"QT-TACTIC-{str(sid)[:8]}", type=1, status=2,
start_time=now, end_time=now + timedelta(days=1)))
if e != ErrorType.SUCCESS:
return e
for n in selected_numbers:
e = await DB_SESSION_MNG.add(s, insert(_T_VNC).values(
vnc_id=_uuid.uuid4(), version_id=ver_id, nego_card_id=card_ids[n]))
if e != ErrorType.SUCCESS:
return e
for n in wild_numbers:
e = await DB_SESSION_MNG.add(s, insert(_T_VWC).values(
vwc_id=_uuid.uuid4(), version_id=ver_id, wild_card_id=wild_ids[n]))
if e != ErrorType.SUCCESS:
return e
return await DB_SESSION_MNG.add(s, insert(_T_SESSIONS).values(
session_id=sid, quotation_id=qid, item_id=iid, supplier_id=sup,
qt_number=f"QT-TACTIC-{str(sid)[:8]}", qt_round=1, qt_type=1,
target_price=target, anchoring_price=anchor, status=2,
end_time=now + timedelta(days=1)))
return run(s_)
err = await DB_SESSION_MNG.execute_lambda_run([DBType.MAIN.value], [_seed])
assert err == ErrorType.SUCCESS
return qid, ver_id
async def _cleanup(sid, qid, ver_id):
await DB_SESSION_MNG.execute_lambda_run(
[DBType.MAIN.value],
[lambda s: DB_SESSION_MNG.add(s, delete(_T_SESSIONS).where(_T_SESSIONS.c.session_id == sid)),
lambda s: DB_SESSION_MNG.add(s, delete(_T_VNC).where(_T_VNC.c.version_id == ver_id)),
lambda s: DB_SESSION_MNG.add(s, delete(_T_VWC).where(_T_VWC.c.version_id == ver_id)),
lambda s: DB_SESSION_MNG.add(s, delete(_T_QUOTATIONS).where(_T_QUOTATIONS.c.qt_id == qid))],
)
@pytest.mark.asyncio
async def test_e2e_counter_accept_settles_at_target(db_engine):
"""견적 선택 카드 NGC-010(향후 거래 연계 — 스크립트 {target_price} 파싱 → 목표가 제안)의
카운터를 수락하면 합의가 = 목표가(10000) '수락 즉시 타결' 기획 결정의 E2E 검증."""
reset_sessions()
sid = _uuid.uuid4()
qid, ver_id = await _seed_quote_session(sid, ["NGC-010"])
try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(_uuid.uuid4()))
svc = ChatService()
session_id = str(sid)
r = None
for ui in [None, "확인", "", "확인", "11000", ""]:
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=ui))
# 가격협상 카드 턴 → NGC-010 카운터(target) 제시 스텝
assert r.step == "가격협상_카운터", f"카운터 스텝 기대, 실제 {r.step}"
assert r.card_id == "NGC-010"
assert r.input_options == ["수락", "다른 가격 제시"]
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="수락"))
assert r.step == "협상완료"
assert r.settled_price == 10000 # 합의가 = 목표가 (고객사 이득)
finally:
await _cleanup(sid, qid, ver_id)
@pytest.mark.asyncio
async def test_e2e_over_target_ends_in_failure_after_closing(db_engine):
"""협력사가 목표가 초과(11000)를 고수하면: 카드 소진 → 종결 전술(목표가 최후통첩) →
그래도 거절 결렬(협상실패). 목표가 초과로는 절대 타결되지 않는다."""
reset_sessions()
sid = _uuid.uuid4()
qid, ver_id = await _seed_quote_session(sid, ["NGC-003"]) # 설득 카드 1장 → 빠른 소진
try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(_uuid.uuid4()))
svc = ChatService()
session_id = str(sid)
steps, r = [], None
# 고수 시나리오: 가격은 항상 11000, 카운터는 전부 거절
for ui in [None, "확인", "", "확인", "11000", "", "11000", ""]:
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=ui))
steps.append(r.step)
# 카드(NGC-003) 소진 → 종결 국면: 목표가 최후통첩 카운터 스텝
assert r.step == "가격협상_카운터", f"종결 카운터 기대, 실제 {steps}"
assert "10000" in r.script # 최후통첩 = 목표가 제시
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="다른 가격 제시"))
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="11000"))
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=""))
assert r.step == "협상실패" # target 초과 고수 → 결렬
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="확인"))
assert r.chat_end and r.outcome == "failure" # backend REJECTED → 개찰 이관
assert r.settled_price is None # 초과가 타결 없음
finally:
await _cleanup(sid, qid, ver_id)
@pytest.mark.asyncio
async def test_e2e_bb9a_no_duplicate_wildcard_and_real_middle(db_engine):
"""IMK BB9A 재현 E2E — 와일드카드 2장(WC-02·WC-05) + 설득 카드 1장.
기대 흐름(수정 ):
· 중반 와일드 진입 = 비종결 WC-02 (종결 전용 WC-05 예약 구현 전엔 WC-05 먼저 나갔다)
· 종결 국면 = WC-05, 절충가 = (당사 직전 제안 + 협력사 제시가)/2 실계산 (구현 전엔 목표가로 클램프)
· 같은 카드 2 발동 없음 + 종결 발동도 card_id 기록
"""
reset_sessions()
sid = _uuid.uuid4()
qid, ver_id = await _seed_quote_session(sid, ["NGC-003"], wild_numbers=["WC-02", "WC-05"])
try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(_uuid.uuid4()))
svc = ChatService()
session_id = str(sid)
r = None
# 10300: 1pct 존(≤ 9900×1.02=10098) 밖, entry 존(≤ 10395) 안 → 선택형 와일드 발동 구간
for ui in [None, "확인", "", "확인", "10300", ""]:
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=ui))
assert r.step == "wild_card_dynamic"
assert r.card_id == "WC-02" # 종결 전용 WC-05 가 아니라 비종결 카드
# WC-02 제안가 = (anchor 9900 + target 10000)/2 = 9950
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="다른 가격 제시"))
# 10010 재제시 → 설득 카드(NGC-003) 1장 소진
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="10010"))
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=""))
assert r.step == "가격협상" and r.card_id == "NGC-003"
# 10005 재제시 → 카드 소진 → 종결 국면: WC-05 절충가 = (9950 + 10005)/2 = 9980 (≤ target)
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="10005"))
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input=""))
assert r.step == "가격협상_카운터"
assert r.card_id == "WC-05" # 종결 발동도 카드 기록(구현 전 null)
assert "9980" in r.script # 실제 절충가 — 목표가(10000) 클램프 아님
r = await svc.chat(eng, Req_Chat(session_id=session_id, user_input="수락"))
assert r.step == "협상완료"
assert r.settled_price == 9980 # 표시가 = 타결가
finally:
await _cleanup(sid, qid, ver_id)

View File

@ -109,7 +109,7 @@ async def test_context_loaded_from_db(db_engine):
try: try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0)) reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("ktcommerce") eng = await reg.get_engine("imarketkorea")
r = await ChatService().chat(eng, Req_Chat(session_id=str(sid))) r = await ChatService().chat(eng, Req_Chat(session_id=str(sid)))
assert r.session_id == str(sid) and r.step == "서비스안내" assert r.session_id == str(sid) and r.step == "서비스안내"
@ -164,7 +164,7 @@ async def test_null_anchoring_falls_back_to_target(db_engine):
assert err == ErrorType.SUCCESS assert err == ErrorType.SUCCESS
try: try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0)) reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("ktcommerce") eng = await reg.get_engine("imarketkorea")
await ChatService().chat(eng, Req_Chat(session_id=str(sid))) await ChatService().chat(eng, Req_Chat(session_id=str(sid)))
saved = await ChatSessionRepository(eng.company_id).get(str(sid)) saved = await ChatSessionRepository(eng.company_id).get(str(sid))
assert saved is not None assert saved is not None
@ -186,15 +186,30 @@ async def test_loader_with_crud_double(db_engine):
class _FakeCRUD(INegoContextCRUD): class _FakeCRUD(INegoContextCRUD):
async def get_session_row(self, cdb, session_id): async def get_session_row(self, cdb, session_id):
# (qt_type, target, anchoring_price, item_id, quotation_id, supplier_id) — 재견적(2)·앵커 미박제 # (qt_type, target, anchoring_price, done_ceiling_price, item_id, quotation_id, supplier_id)
return ErrorType.SUCCESS, (2, 50000, None, uuid.uuid4(), uuid.uuid4(), uuid.uuid4()) # — 재견적(2)·앵커 미박제·타결상한 52,500(목표가 +5%)
return ErrorType.SUCCESS, (2, 50000, None, 52500, uuid.uuid4(), uuid.uuid4(), uuid.uuid4())
async def get_item_price(self, cdb, item_id): async def get_item_baseline(self, cdb, item_id):
return ErrorType.SUCCESS, 7000 # 기준가를 매입가로 고른 회사 + 거래상대 호칭을 '공급업체'로 바꾼 용어 사전.
# 호칭은 crud 가 어떤 회사든 '공급가'(공급사 화면 고정 용어)로 내려준다.
return ErrorType.SUCCESS, (7000, "공급가", {"supplier": "공급업체"})
async def get_item_lowest_price(self, cdb, item_id):
return ErrorType.SUCCESS, 6300 # 인터넷 최저가(items.internet_lowest_price)
async def get_card_count(self, cdb, session_id):
return ErrorType.SUCCESS, 3 # 협상카드 사용 횟수 상한(quotation_settings.card_count)
async def get_supplier_total_revenue(self, cdb, supplier_id): async def get_supplier_total_revenue(self, cdb, supplier_id):
return ErrorType.SUCCESS, 12_000_000.0 return ErrorType.SUCCESS, 12_000_000.0
async def get_item_name(self, cdb, item_id):
return ErrorType.SUCCESS, "테스트상품"
async def get_supplier_name(self, cdb, supplier_id):
return ErrorType.SUCCESS, "테스트협력사"
async def get_supply_type(self, cdb, supplier_id, item_id): async def get_supply_type(self, cdb, supplier_id, item_id):
return ErrorType.SUCCESS, 3 # sole_agency(총판) → "B" return ErrorType.SUCCESS, 3 # sole_agency(총판) → "B"
@ -205,19 +220,39 @@ async def test_loader_with_crud_double(db_engine):
return ErrorType.SUCCESS, 0 # 이력도 없음 → NONE return ErrorType.SUCCESS, 0 # 이력도 없음 → NONE
async def get_quotation_card_numbers(self, cdb, quotation_id): async def get_quotation_card_numbers(self, cdb, quotation_id):
return ErrorType.SUCCESS, (["NGC-003", "NGC-008"], ["WC-02"]) # 견적 선택 카드 # 행 = (number, script, tactic) — 스크립트 파싱 + tactic JSONB 로 card_specs 를 만든다
return ErrorType.SUCCESS, (
[("NGC-003", "설득 멘트(가격 변수 없음)", None),
("NGC-008", "시장가 {internet_lowest_price}원 인용(읽기 전용 변수)", None)],
[("WC-02", "이에 당사는 {target_mid_price}원을 역으로 제안 드립니다.", None)],
)
ctx = await NegotiationContextLoader(crud=_FakeCRUD()).load(str(uuid.uuid4())) ctx = await NegotiationContextLoader(crud=_FakeCRUD()).load(str(uuid.uuid4()))
assert ctx is not None assert ctx is not None
assert ctx.rq_type == "재견적" # qt_type=2(1:N) assert ctx.rq_type == "재견적" # qt_type=2(1:N)
assert ctx.target_price == 50000 assert ctx.target_price == 50000
assert ctx.anchor_price == 50000 # 미박제 → 무할인 폴백(anchor=target) assert ctx.anchor_price == 50000 # 미박제 → 무할인 폴백(anchor=target)
assert ctx.done_ceiling_price == 52500 # 타결 상한가 박제값(목표가 +5%)
assert ctx.item_price == 7000 assert ctx.item_price == 7000
assert ctx.item_price_label == "공급가" # 기준가 호칭이 멘트까지 전달되는지
assert ctx.labels == {"supplier": "공급업체"} # 회사 용어 사전이 스크립트 토큰용으로 실리는지
assert ctx.internet_lowest_price == 6300 # 인터넷 최저가 로드 확인
assert ctx.card_count == 3 # 협상카드 사용 횟수 상한 로드 확인
assert ctx.partner_name == "테스트협력사"
assert ctx.product_name == "테스트상품"
assert ctx.revenue_amount == 12_000_000.0 assert ctx.revenue_amount == 12_000_000.0
assert ctx.distribution_code == "B" # supply_type=3(총판) → B assert ctx.distribution_code == "B" # supply_type=3(총판) → B
assert ctx.partner_type is PartnerType.NONE assert ctx.partner_type is PartnerType.NONE
assert ctx.selected_nego_card_numbers == ["NGC-003", "NGC-008"] assert ctx.selected_nego_card_numbers == ["NGC-003", "NGC-008"]
assert ctx.selected_wild_card_numbers == ["WC-02"] assert ctx.selected_wild_card_numbers == ["WC-02"]
# 카드 전술 확정 — 설득 카드/읽기 전용 변수는 제안가 없음, WC-02 는 스크립트 파싱으로 중간가.
assert ctx.card_specs["NGC-003"]["offer_variable"] is None
assert ctx.card_specs["NGC-008"]["offer_variable"] is None # 인터넷 최저가는 읽어주기 변수 — 제안가 아님
# 시장가 인용 카드는 최저가 결측 세션에서 미발동하도록 requires 로 표시된다(토큰 노출 방지).
assert ctx.card_specs["NGC-008"]["requires"] == ["internet_lowest_price"]
assert ctx.card_specs["WC-02"] == {
"offer_variable": "target_mid_price", "min_round": 1, "closing": False, "requires": [],
}
@pytest.mark.asyncio @pytest.mark.asyncio
@ -225,7 +260,7 @@ async def test_context_falls_back_without_db_row(db_engine):
"""DB 에 세션 행이 없으면(데모/직접 호출) 기본 컨텍스트로 폴백한다.""" """DB 에 세션 행이 없으면(데모/직접 호출) 기본 컨텍스트로 폴백한다."""
reset_sessions() reset_sessions()
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0)) reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("ktcommerce") eng = await reg.get_engine("imarketkorea")
r = await ChatService().chat(eng, Req_Chat()) r = await ChatService().chat(eng, Req_Chat())
saved = await ChatSessionRepository(eng.company_id).get(r.session_id) saved = await ChatSessionRepository(eng.company_id).get(r.session_id)
assert saved is not None assert saved is not None

View File

@ -0,0 +1,145 @@
"""Phase 1 결정 스택 잔여분 검증 — 규칙 데이터화 + 선택카드 우선순위 prior.
1. 와일드카드 진입 임계(wildcard_1pct_ratio/entry_ratio)·카운터 라운드 상한(max_counter_rounds)
하드코딩이 아니라 테넌트 config(negotiation.*) 주입된다.
2. 의도층 prior: 갑이 견적에서 고른 카드 순서가 콜드 스타트 선택을 결정하고,
학습(Q·방문수) 쌓이면 영향이 소멸한다 Q-table 오염 없음.
"""
import os
import numpy as np
import pytest
from negotiation.chat.service.chat_engine import ChatEngine, ChatSession
from negotiation.chat.service.script_repository import ScriptRepository
from negotiation.policies.base import EpisodeState, PolicyContext
from negotiation.policies.qtable_policy import UCBQTablePolicy
from negotiation.qtable.domain.model.q_table import QTable
from negotiation.qtable.domain.model.snapshot import NegotiationSnapshot
from tenancy.config_loader import TenantConfigLoader
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
def _engine(**rule_overrides) -> ChatEngine:
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
for k, v in rule_overrides.items():
setattr(cfg.negotiation, k, v)
return ChatEngine(ScriptRepository(cfg, _TENANTS_DIR), rq_type="재협상")
def _session(price, anchor=10000, rnd=1, **ctx_over):
ctx = {"input_price": price, "anchor_price": anchor, "target_price": anchor + 100,
"round": rnd, "allow_selected_wildcards": False}
ctx.update(ctx_over)
return ChatSession(session_id="00000000-0000-0000-0000-00000000d001", tenant_id="imarketkorea",
company_id="imarketkorea", step="가격협상_확인", action_space_size=0, context=ctx)
# ---- 규칙 데이터화 -----------------------------------------------------------
def test_default_rules_loaded_from_config():
eng = _engine()
assert eng.rules.wildcard_1pct_ratio == 1.02
assert eng.rules.wildcard_entry_ratio == 1.05
assert eng.rules.max_counter_rounds == 3
def test_wildcard_threshold_is_config_driven():
# 기본(1.02): anchor 10000, 제시 10800 → 임계 밖 → 일반 가격협상
view = _engine().advance(_session(10800), "")
assert view.step == "가격협상"
# 임계를 1.10 으로 완화한 테넌트 → 같은 가격에서 1% 인하 와일드카드 발동.
# 1%가(10800×0.99=10692)도 제안가 공통 유효조건(≤목표가)을 타므로 목표가를 그 위로 둔다 —
# 기본 target(10100)이면 초과 제시 금지 규칙에 걸려 발동하지 않는 게 새 정답.
view = _engine(wildcard_1pct_ratio=1.10).advance(_session(10800, target_price=11000), "")
assert view.step == "wild_card_1pct"
# 목표가가 1%가 아래면(초과 제시 금지) 완화 임계라도 미발동 — 수락해도 결렬되는 모순 제안 차단.
view = _engine(wildcard_1pct_ratio=1.10).advance(_session(10800), "")
assert view.step == "가격협상"
def test_max_counter_rounds_is_config_driven():
# round=3(카운터 2회 경과), 제시 11000: 기본 상한 3 → 아직 협상 지속
view = _engine().advance(_session(11000, rnd=3), "")
assert view.step == "가격협상"
# 상한 1 → 종결 국면 진입: 곧장 실패가 아니라 종결 전술 발동 지점(force_closing)으로
s = _engine(max_counter_rounds=1), _session(11000, rnd=3)
view = s[0].advance(s[1], "")
assert view.step == "가격협상" and s[1].context.get("force_closing") is True
# 종결 전술까지 소진(closing_played) 후에도 target(10100) 초과 → 결렬
view = _engine(max_counter_rounds=1).advance(_session(11000, rnd=3, closing_played=True), "")
assert view.step == "협상실패"
# 종결 후 제시가가 target 이하로 내려오면 결렬이 아니라 타결 (새 규칙 — 구현 전엔 무조건 실패)
view = _engine(max_counter_rounds=1).advance(
_session(10050, rnd=3, closing_played=True, wildcard_used=True), "")
assert view.step == "협상완료"
# ---- 선택카드 우선순위 prior ---------------------------------------------------
def _snap():
return NegotiationSnapshot(revenue_amount=1, distribution_code="A", partner_count=1,
acceptance_ratio=0.1, input_price=900, anchor_price=800, target_price=1000)
def _ctx(prior=None, mask=None, n=11):
return PolicyContext(state_index=0, snapshot=_snap(), action_space_size=n,
available_mask=mask, prior_bonus=prior, episode=EpisodeState())
def test_prior_decides_cold_start_order():
"""콜드 스타트(Q=0·방문 0)에서는 갑이 먼저 고른 카드(높은 prior)가 먼저 나간다."""
qt = QTable(2, 11)
prior = np.zeros(11)
prior[7], prior[2] = 0.3, 0.15 # 선택 순서: action7 → action2
mask = np.zeros(11, dtype=bool)
mask[2] = mask[7] = True
p = UCBQTablePolicy(qt, mark_visits=False)
assert p.select(_ctx(prior=prior, mask=mask)).action_id == 7
def test_prior_decays_as_learning_accumulates():
"""학습이 쌓이면(Q·방문수) prior 는 1/(1+visits) 로 감쇠 — Q 가 지배한다."""
qt = QTable(2, 11)
qt.q[0, 2] = 1.0 # action2 가 학습상 우월
qt.visits[0, 2] = 5
qt.visits[0, 7] = 5 # 탐색 보너스 동률
prior = np.zeros(11)
prior[7] = 0.3 # 갑 선호는 action7
mask = np.zeros(11, dtype=bool)
mask[2] = mask[7] = True
p = UCBQTablePolicy(qt, mark_visits=False)
assert p.select(_ctx(prior=prior, mask=mask)).action_id == 2
def test_no_prior_keeps_existing_behavior():
"""prior 미주입(None) 시 기존 UCB 동작 그대로 — 회귀 없음."""
qt = QTable(2, 4)
qt.q[0] = np.array([1.0, 9.0, 2.0, 0.0])
qt.visits[0] = np.array([5, 5, 5, 5])
p = UCBQTablePolicy(qt, exploration_constant=0.1, mark_visits=False)
assert p.select(_ctx(n=4)).action_id == 1
def test_selection_prior_built_from_quotation_order():
"""ChatService._selection_prior — 견적 선택 순서 → prior 배열 (앞선 선택일수록 큼)."""
from services.chat_service import ChatService
class _Mapper:
_m = {i: f"NGC-B{i + 1:03d}" for i in range(11)}
def get_action_id(self, num):
return next((a for a, c in self._m.items() if c == num), None)
class _Engine:
mapper = _Mapper()
action_space_size = 11
session = ChatSession(session_id="00000000-0000-0000-0000-00000000d002", tenant_id="t", company_id="t",
context={"selected_nego_card_numbers": ["NGC-B008", "NGC-B003"]})
prior = ChatService._selection_prior(_Engine(), session)
assert prior is not None
assert prior[7] > prior[2] > 0 # 먼저 고른 NGC-B008(action7) 이 더 큼
assert prior[[0, 1, 4, 10]].sum() == 0 # 미선택 카드는 0
# 선택이 1장이면 순서 정보가 없어 None
session.context["selected_nego_card_numbers"] = ["NGC-B008"]
assert ChatService._selection_prior(_Engine(), session) is None

View File

@ -110,7 +110,7 @@ async def test_version_and_cell_persistence(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_service_step_learns_and_isolates(db_engine): async def test_service_step_learns_and_isolates(db_engine):
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0)) reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("ktcommerce") eng = await reg.get_engine("imarketkorea")
svc = NegotiationService() svc = NegotiationService()
def req(): def req():
@ -126,20 +126,21 @@ async def test_service_step_learns_and_isolates(db_engine):
assert r1.learned is True and r1.policy == "qtable_ucb" assert r1.learned is True and r1.policy == "qtable_ucb"
assert r1.updated_q > 0.0 # 성공 보상으로 Q 상승 assert r1.updated_q > 0.0 # 성공 보상으로 Q 상승
# DB 에서 state 전체 방문 누적 확인 (state 58 = ktcommerce 의 이 snapshot) # DB 에서 state 전체 방문 누적 확인 (state index 는 imarketkorea config 로 동적 계산)
from negotiation.qtable.domain.service.state_calculator import state_index from negotiation.qtable.domain.service.state_calculator import state_index
sidx = state_index(_snap(revenue_amount=20_000_000, acceptance_ratio=0.11, input_price=990, round_number=3), eng.config.state) sidx = state_index(_snap(revenue_amount=20_000_000, acceptance_ratio=0.11, input_price=990, round_number=3), eng.config.state)
repo_kt = LearningRepository("ktcommerce") repo_a = LearningRepository("imarketkorea")
vid = await repo_kt.get_or_create_active_version(state_space_size=162, action_space_size=9, learning_rate=0.1, discount_factor=0.95) vid = await repo_a.get_or_create_active_version(state_space_size=162, action_space_size=9, learning_rate=0.1, discount_factor=0.95)
_, vcells = await repo_kt.load_cells(vid) _, vcells = await repo_a.load_cells(vid)
state_total = sum(c for s, a, c in vcells if s == sidx) state_total = sum(c for s, a, c in vcells if s == sidx)
assert state_total == 3 # 3회 호출 → state 누적 방문 3 assert state_total == 3 # 3회 호출 → state 누적 방문 3
# 테넌트 격리: imarketkorea 는 별도 학습/별도 state # 테넌트 격리: 자동 온보딩 고객사(UUID)는 별도 학습/별도 state
eng2 = await reg.get_engine("imarketkorea") other = "00000000-0000-0000-0000-0000000000c1"
eng2 = await reg.get_engine(other)
ri = await svc.step(eng2, req()) ri = await svc.step(eng2, req())
assert ri.visit_count == 1 assert ri.visit_count == 1
err, ck = await repo_kt.read(lambda s: repo_kt.count_experience(s)) err, ck = await repo_a.read(lambda s: repo_a.count_experience(s))
err, ci = await LearningRepository("imarketkorea").read(lambda s: LearningRepository("imarketkorea").count_experience(s)) err, ci = await LearningRepository(other).read(lambda s: LearningRepository(other).count_experience(s))
assert ck == 3 and ci == 1 # experience 격리 assert ck == 3 and ci == 1 # experience 격리

View File

@ -35,7 +35,7 @@ def test_card_effectiveness_has_good_cards():
assert len(good) >= 3 # 효과 좋은 카드 존재 → 학습 대상 신호 assert len(good) >= 3 # 효과 좋은 카드 존재 → 학습 대상 신호
@pytest.mark.parametrize("tenant", ["ktcommerce", "imarketkorea"]) @pytest.mark.parametrize("tenant", ["_base", "imarketkorea"])
def test_learning_beats_baseline(tenant): def test_learning_beats_baseline(tenant):
report = run("configs/exp_default.yaml", tenant) report = run("configs/exp_default.yaml", tenant)
pols = report["policies"] pols = report["policies"]
@ -53,7 +53,7 @@ def test_learning_beats_baseline(tenant):
def test_static_does_not_learn(): def test_static_does_not_learn():
report = run("configs/exp_default.yaml", "ktcommerce") report = run("configs/exp_default.yaml", "imarketkorea")
static = report["policies"]["static"] static = report["policies"]["static"]
# 정적 정책은 항상 고정 카드 → 좋은카드 적중 학습 없음(우연 일치만) # 정적 정책은 항상 고정 카드 → 좋은카드 적중 학습 없음(우연 일치만)
assert static["good_card_hit_rate"] <= report["policies"]["qtable_ucb"]["good_card_hit_rate"] assert static["good_card_hit_rate"] <= report["policies"]["qtable_ucb"]["good_card_hit_rate"]

View File

@ -0,0 +1,161 @@
"""Phase 3 이해층 검증 — InputInterpreter (자유 발화 NLU → 기대 입력 구조화).
원칙 검증: LLM 의도 분류·가격 표현 위치만 찾고, 숫자 계산은 결정론 파서가 한다.
한국어 가격 파서 결정론 choice 선택지 목록 검증 price_text 원문 부분문자열 검증
실패/타임아웃 None(원문 폴백) 플로우: 자유 발화로 분기·가격 입력이 진행된다
LLM 미설정 기존(정형 입력) 동작 그대로 회귀 없음.
"""
import os
import pytest
from negotiation.chat.service.input_interpreter import (
InputInterpreter, InterpretedInput, parse_korean_price,
)
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
# ---- 결정론 한국어 가격 파서 -------------------------------------------------
@pytest.mark.parametrize("text,expected", [
("10,500원", 10500),
("10500", 10500),
("1만 500원", 10500),
("1만500원", 10500),
("만원", 10000),
("1.5만", 15000),
("3만2천원", 32000),
("2억", 200_000_000),
("0", None), # 0 이하 무효
("그건 어렵습니다", None), # 가격 아님
("만원에 3개", None), # 잡문자 혼입 → 해석 불가(안전 폴백)
])
def test_parse_korean_price(text, expected):
got = parse_korean_price(text)
assert (got == expected) if expected is not None else (got is None)
# ---- LLM 출력 검증 (환각 차단) ----------------------------------------------
def _fake(reply: dict):
def call(messages):
return reply
return call
@pytest.mark.asyncio
async def test_choice_mapped_to_option():
nat = InputInterpreter(llm_call=_fake({"intent": "choice", "choice": "", "price_text": None}))
out = await nat.interpret("네 접니다, 말씀하세요", input_mode="yes_no", input_options=["", "아니오"])
assert out == InterpretedInput(kind="choice", value="", source="")
@pytest.mark.asyncio
async def test_choice_outside_options_rejected():
nat = InputInterpreter(llm_call=_fake({"intent": "choice", "choice": "글쎄요", "price_text": None}))
assert await nat.interpret("음...", input_mode="yes_no", input_options=["", "아니오"]) is None
@pytest.mark.asyncio
async def test_price_span_verified_and_parsed_deterministically():
nat = InputInterpreter(llm_call=_fake({"intent": "price", "choice": None, "price_text": "1만 500원"}))
out = await nat.interpret("저희 마진상 1만 500원까지는 맞춰드릴 수 있습니다", input_mode="price")
assert out is not None and out.kind == "price"
assert out.value == "10500" # 숫자는 결정론 파서 산출(LLM 계산 아님)
assert out.source == "1만 500원"
@pytest.mark.asyncio
async def test_price_span_not_in_text_rejected():
"""LLM 이 원문에 없는 가격 표현을 지어내면 폐기(환각 차단)."""
nat = InputInterpreter(llm_call=_fake({"intent": "price", "choice": None, "price_text": "9,000원"}))
assert await nat.interpret("만원이면 가능합니다", input_mode="price") is None
@pytest.mark.asyncio
async def test_unknown_and_failures_fall_back():
assert await InputInterpreter(llm_call=_fake({"intent": "unknown"})).interpret(
"글쎄요 검토해 볼게요", input_mode="yes_no", input_options=["", "아니오"]) is None
def boom(messages):
raise RuntimeError("LLM down")
assert await InputInterpreter(llm_call=boom).interpret("", input_mode="yes_no", input_options=[""]) is None
def slow(messages):
import time
time.sleep(0.5)
return {"intent": "choice", "choice": ""}
nat = InputInterpreter(llm_call=slow, timeout_seconds=0.05)
assert await nat.interpret("", input_mode="yes_no", input_options=[""]) is None
# ---- 챗 플로우 E2E (fake LLM) ------------------------------------------------
def _routing_fake(messages):
"""단계별 fake — 기대 입력이 가격이면 price, 아니면 '' choice 로 응답."""
user = messages[-1]["content"]
if "가격(숫자)" in user:
return {"intent": "price", "choice": None, "price_text": "1만 500원"}
return {"intent": "choice", "choice": "", "price_text": None}
@pytest.mark.asyncio
async def test_chat_flow_free_text_negotiation(db_engine):
"""자유 발화만으로 담당자확인 분기 + 가격 입력이 진행된다 (버튼 없는 '진짜 대화')."""
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
reset_sessions()
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("imarketkorea")
eng.config.llm.enabled = True
svc = ChatService()
svc._interpreter = InputInterpreter(llm_call=_routing_fake)
orig = InputInterpreter.available
InputInterpreter.available = staticmethod(lambda: True)
try:
sid = None
r = await svc.chat(eng, Req_Chat(session_id=sid)) # 서비스안내
sid = r.session_id
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input="확인")) # 담당자확인 (fast path)
assert r.step == "담당자확인" and r.interpreted_input is None
# 자유 발화 → NLU 가 "예" 로 매핑 → 협상품목안내
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input="네 접니다, 말씀하세요"))
assert r.step == "협상품목안내"
assert r.interpreted_input == ""
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input="확인")) # 기존가격제시(price)
# 자유 발화 가격 → span "1만 500원" → 결정론 파서 10500 → 가격 저장 후 확인 단계
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input="저희 마진상 1만 500원까지는 맞춰드릴 수 있습니다"))
assert r.step == "가격협상_확인"
assert r.interpreted_input == "10500"
assert "10500" in r.script # 멘트 치환도 해석된 가격으로
finally:
InputInterpreter.available = orig
eng.config.llm.enabled = False
@pytest.mark.asyncio
async def test_chat_flow_without_llm_keeps_legacy_behavior(db_engine):
"""LLM 미설정(available=False)이면 자유 발화는 원문 그대로 엔진에 전달 — 기존 동작 회귀 없음."""
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
reset_sessions()
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("imarketkorea")
svc = ChatService()
sid = None
r = await svc.chat(eng, Req_Chat(session_id=sid))
sid = r.session_id
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input="확인"))
assert r.step == "담당자확인"
# conftest 가드로 available=False → NLU 미동작, interpreted_input 없음
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input="네 접니다"))
assert r.interpreted_input is None

View File

@ -0,0 +1,190 @@
"""협상 불변식 시나리오 하네스 — 실서비스 스택(ChatService + 실 DB 카드)으로 13개 협상을 완주시키고,
IMK 잡은 부류의 사고(같은 카드 반복 · 이상한 금액) 어떤 흐름에서도 나는지 검사한다.
시나리오별 기대 이벤트(카드가 나간 턴의 step·카드·금액) 정확히 못박고, 공통 불변식을 턴에 건다:
· 카드 중복 없음 협상에서 같은 card_id 2 발동 금지
· 카드 자리 규칙 종결 전용(WC-03·05) 가격협상_카운터에서만, 비종결 와일드는 wild_card_dynamic 에서만
· 타결가 목표가 어떤 성공 경로도 목표가 초과로 끝남
· 카운터 멘트의 금액 = 수락 타결가 (표시가=타결가)
"""
import uuid as _uuid
from dataclasses import dataclass, field
from typing import Optional
import pytest
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
from tests.test_card_tactics import _TENANTS_DIR, _cleanup, _seed_quote_session
# 종결 전용 와일드카드(DB tactic 시드와 동일) — 자리 규칙 검사용.
_CLOSING_WILDS = {"WC-03", "WC-05"}
_NONCLOSING_WILDS = {"WC-01", "WC-02", "WC-04"}
# 카드가 나갈 수 있는 스텝(이벤트로 수집).
_CARD_STEPS = {"가격협상", "wild_card_dynamic", "wild_card_1pct", "가격협상_카운터"}
_BOILERPLATE = [None, "확인", "", "확인"]
@dataclass
class Scenario:
name: str
inputs: list # 서두(안내~기존가격제시) 이후의 협력사 입력 시퀀스
# 기대 이벤트: (step, card, offer_substring). card="NGC-*" 는 임의 협상카드(중복만 검사).
events: list
settled: Optional[int] # 기대 타결가(원). None=결렬
nego: list = field(default_factory=lambda: ["NGC-001"])
wild: list = field(default_factory=list)
target: int = 10_000
anchor: int = 9_900
# 밴드(기본 target 10000·anchor 9900): 1% 존 ≤ 10,098 · 진입 존 ≤ 10,395.
SCENARIOS = [
# S01 BB9A 재현 — 중반 비종결 WC-02, 종결 WC-05 실절충가. 같은 카드 2회 없음.
Scenario("S01_bb9a_mid_wc02_close_wc05",
["10300", "", "다른 가격 제시", "10010", "", "10005", "", "수락", "확인"],
[("wild_card_dynamic", "WC-02", "9950"),
("가격협상", "NGC-001", None),
("가격협상_카운터", "WC-05", "9980")],
settled=9980, wild=["WC-02", "WC-05"]),
# S02 8AB0 재현 — 절충가(9,205)가 목표가(9,000) 초과 → WC-05 미발동, 목표가 최후통첩(카드 없음).
Scenario("S02_8ab0_middle_over_target_skips",
["9500", "", "9500", "", "다른 가격 제시", "9500", "", "확인"],
[("가격협상", "NGC-001", None),
("가격협상_카운터", None, "9000")],
settled=None, wild=["WC-05"], target=9_000, anchor=8_910),
# S03 와일드 5장 전부 + 협상카드 2장 — 중반 1장(WC-01)·종결 1장(WC-03)만, 협상카드는 서로 다른 2장.
Scenario("S03_five_wilds_full_run",
["10300", "", "다른 가격 제시", "10200", "", "10150", "", "10100", "", "수락", "확인"],
[("wild_card_dynamic", "WC-01", "10000"),
("가격협상", "NGC-*", None),
("가격협상", "NGC-*", None),
("가격협상_카운터", "WC-03", "10000")],
settled=10_000, nego=["NGC-001", "NGC-003"],
wild=["WC-01", "WC-02", "WC-03", "WC-04", "WC-05"]),
# S04 종결 전용 와일드만 담김 + 제시가가 진입 존에 머무름 — 소진 판정이 막히지 않고
# 종결로 넘어간다(프로브 픽스 회귀: 픽스 전엔 빈 덱에서 쓴 카드를 또 꺼내는 무한 협상).
Scenario("S04_closing_only_wild_no_deadlock",
["10300", "", "10250", "", "수락", "확인"],
[("가격협상", "NGC-001", None),
("가격협상_카운터", None, "10000")], # WC-05 절충 10,075>목표가 → 미발동 → 최후통첩
settled=10_000, wild=["WC-05"]),
# S05 1% 존 — 시스템 1% 카드, 수락 시 표시 금액 그대로 타결.
Scenario("S05_one_pct_zone_accept",
["10050", "", "", "확인"],
[("wild_card_1pct", None, "9950")],
settled=9_950),
# S06 앵커 이하 즉시 타결 — 카드 0장.
Scenario("S06_priority_match_no_cards",
["9800", "", "확인"],
[],
settled=9_800),
# S07 목표가 초과 고수 → 설득 1장 → 최후통첩 → 결렬.
Scenario("S07_hold_high_fails",
["11000", "", "11000", "", "다른 가격 제시", "11000", "", "확인"],
[("가격협상", "NGC-003", None),
("가격협상_카운터", None, "10000")],
settled=None, nego=["NGC-003"]),
# S08 협상카드 카운터(NGC-010 목표가 제안) 수락 — 협상카드도 카운터 스텝을 쓴다.
Scenario("S08_nego_counter_accept",
["11000", "", "수락", "확인"],
[("가격협상_카운터", "NGC-010", "10000")],
settled=10_000, nego=["NGC-010"]),
# S09 min_round=2 — WC-04 는 1라운드 진입 존에서 안 나가고 2라운드에 나간다.
Scenario("S09_min_round_two_defers_wc04",
["10300", "", "10200", "", "수락", "확인"],
[("가격협상", "NGC-001", None),
("wild_card_dynamic", "WC-04", "10000")],
settled=10_000, wild=["WC-04"]),
# S10 종결 체인 폴백 — WC-05 무효(절충 10,175>목표) → 다음 종결 WC-03 발동.
Scenario("S10_closing_chain_falls_to_wc03",
["10500", "", "10450", "", "다른 가격 제시", "10450", "", "확인"],
[("가격협상", "NGC-001", None),
("가격협상_카운터", "WC-03", "10000")],
settled=None, wild=["WC-05", "WC-03"]),
# S11 중반+종결 콤보 — WC-02 중반, 종결은 WC-05 무효 건너뛰고 WC-03. 전 카드 1회씩.
Scenario("S11_mid_and_closing_combo",
["10300", "", "다른 가격 제시", "10400", "", "10350", "", "수락", "확인"],
[("wild_card_dynamic", "WC-02", "9950"),
("가격협상", "NGC-001", None),
("가격협상_카운터", "WC-03", "10000")],
settled=10_000, wild=["WC-02", "WC-05", "WC-03"]),
# S12 라운드 상한 — 협상카드 3장 각 1회(중복 없음) 후 상한 도달 → 최후통첩 → 결렬.
Scenario("S12_round_cap_distinct_nego_cards",
["11000", "", "11000", "", "11000", "", "11000", "", "다른 가격 제시", "11000", "", "확인"],
[("가격협상", "NGC-*", None),
("가격협상", "NGC-*", None),
("가격협상", "NGC-*", None),
("가격협상_카운터", None, "10000")],
settled=None, nego=["NGC-001", "NGC-002", "NGC-003", "NGC-004", "NGC-005"]),
# S13 재생성 아님·재료 극단 — 앵커 미박제 세션(anchor=target 폴백)에서도 초과 제시·중복 없음.
Scenario("S13_anchor_equals_target_fallback",
["10300", "", "10200", "", "수락", "확인"],
[("가격협상", "NGC-001", None),
("가격협상_카운터", "WC-03", "10000")], # WC-05 절충 (10000+10200)/2=10100>목표 → 스킵
settled=10_000, wild=["WC-05", "WC-03"], anchor=10_000),
# S14 역행 금지(IMK 논의 재현) — 절충 카드(9,950) 뒤에 예산 상한 카드(NGC-007, 앵커 9,900)가
# 선택돼 있어도 발동하지 않는다(설득 폴백으로도 안 나감). 낼 카드가 없어져 종결(목표가 최후통첩)로.
Scenario("S14_no_offer_regression",
["10300", "", "다른 가격 제시", "10200", "", "수락", "확인"],
[("wild_card_dynamic", "WC-02", "9950"),
("가격협상_카운터", None, "10000")], # NGC-007 이벤트가 없어야 함(역행 차단)
settled=10_000, nego=["NGC-007"], wild=["WC-02"]),
]
@pytest.mark.asyncio
@pytest.mark.parametrize("sc", SCENARIOS, ids=[s.name for s in SCENARIOS])
async def test_negotiation_invariants(db_engine, sc: Scenario):
reset_sessions()
sid = _uuid.uuid4()
qid, ver_id = await _seed_quote_session(sid, sc.nego, wild_numbers=sc.wild,
target=sc.target, anchor=sc.anchor)
try:
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine(str(_uuid.uuid4()))
svc = ChatService()
trace, settled, outcome = [], None, None
for ui in [*_BOILERPLATE, *sc.inputs]:
r = await svc.chat(eng, Req_Chat(session_id=str(sid), user_input=ui))
assert r.result.success is True, f"{sc.name}: 턴 실패 input={ui} msg={r.msg}"
trace.append(r)
if r.settled_price is not None:
settled = r.settled_price
if r.chat_end:
outcome = r.outcome
# ── 기대 이벤트(카드/카운터 턴) 정확 일치 ──
events = [r for r in trace if r.step in _CARD_STEPS]
got = [(r.step, r.card_id) for r in events]
assert len(events) == len(sc.events), f"{sc.name}: 이벤트 수 {got} ≠ 기대 {sc.events}"
for r, (step, card, offer) in zip(events, sc.events):
assert r.step == step, f"{sc.name}: step {r.step}{step} (전체 {got})"
if card == "NGC-*":
assert r.card_id and r.card_id.startswith("NGC-"), f"{sc.name}: 협상카드 기대, 실제 {r.card_id}"
else:
assert r.card_id == card, f"{sc.name}: card {r.card_id}{card} (전체 {got})"
if offer is not None:
assert offer in (r.script or ""), f"{sc.name}: 멘트에 금액 {offer} 없음 — {r.script[:80]}"
# ── 공통 불변식 ──
played = [r.card_id for r in events if r.card_id]
assert len(played) == len(set(played)), f"{sc.name}: 카드 중복 발동 {played}"
for r in events:
if r.card_id in _CLOSING_WILDS:
assert r.step == "가격협상_카운터", f"{sc.name}: 종결 카드 {r.card_id}가 중반({r.step})에 발동"
if r.card_id in _NONCLOSING_WILDS:
assert r.step == "wild_card_dynamic", f"{sc.name}: 비종결 와일드 {r.card_id}{r.step}에서 발동"
# ── 결말 ──
if sc.settled is None:
assert outcome == "failure" and settled is None, f"{sc.name}: 결렬 기대, settled={settled} outcome={outcome}"
else:
assert outcome == "success", f"{sc.name}: 타결 기대, outcome={outcome}"
assert settled == sc.settled, f"{sc.name}: 타결가 {settled} ≠ 기대 {sc.settled}"
assert settled <= sc.target, f"{sc.name}: 목표가 초과 타결 {settled} > {sc.target}"
finally:
await _cleanup(sid, qid, ver_id)

View File

@ -29,12 +29,12 @@ async def test_step_requires_tenant_header(client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_step_tenant_divergence(client): async def test_step_tenant_divergence(client):
rk = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "ktcommerce"}, json=_BODY) rk = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "_base"}, json=_BODY)
ri = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "imarketkorea"}, json=_BODY) ri = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "imarketkorea"}, json=_BODY)
assert rk.status_code == 200 and ri.status_code == 200 assert rk.status_code == 200 and ri.status_code == 200
dk, di = rk.json(), ri.json() dk, di = rk.json(), ri.json()
# 같은 입력이 테넌트 config 에 따라 다른 상태/카드로 갈린다 # 같은 입력이 테넌트 config 에 따라 다른 상태/카드로 갈린다 (_base=공용 NGC-0xx, imk=NGC-Bxxx)
assert dk["card_id"].startswith("NGC-A") assert dk["card_id"].startswith("NGC-0")
assert di["card_id"].startswith("NGC-B") assert di["card_id"].startswith("NGC-B")
assert dk["state_index"] != di["state_index"] assert dk["state_index"] != di["state_index"]
# 응답 형태 # 응답 형태
@ -49,7 +49,7 @@ async def test_step_tenant_divergence(client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_step_invalid_distribution_code_is_domain_error(client): async def test_step_invalid_distribution_code_is_domain_error(client):
body = dict(_BODY, distribution_code="Z") body = dict(_BODY, distribution_code="Z")
r = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "ktcommerce"}, json=body) r = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "imarketkorea"}, json=body)
assert r.status_code == 200 # HTTP 는 200, 결과코드로 에러 전달(backend 규약) assert r.status_code == 200 # HTTP 는 200, 결과코드로 에러 전달(backend 규약)
assert r.json()["result"]["desc"] == "NEGO_INVALID_STEP" assert r.json()["result"]["desc"] == "NEGO_INVALID_STEP"
@ -57,5 +57,5 @@ async def test_step_invalid_distribution_code_is_domain_error(client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_step_invalid_outcome(client): async def test_step_invalid_outcome(client):
body = dict(_BODY, outcome="maybe") body = dict(_BODY, outcome="maybe")
r = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "ktcommerce"}, json=body) r = await client.post("/v1/negotiation/step", headers={"X-Tenant-ID": "imarketkorea"}, json=body)
assert r.json()["result"]["desc"] == "INVALID_REQUEST_DATA" assert r.json()["result"]["desc"] == "INVALID_REQUEST_DATA"

View File

@ -62,5 +62,5 @@ async def test_tenant_header_required(client):
assert r.json()["result"]["desc"] == "TENANT_HEADER_MISSING" assert r.json()["result"]["desc"] == "TENANT_HEADER_MISSING"
# 헤더가 있으면 미들웨어 통과 (라우트 미존재라 404). # 헤더가 있으면 미들웨어 통과 (라우트 미존재라 404).
r = await client.get("/v1/some-protected-path", headers={"X-Tenant-ID": "ktcommerce"}) r = await client.get("/v1/some-protected-path", headers={"X-Tenant-ID": "imarketkorea"})
assert r.status_code == 404 assert r.status_code == 404

View File

@ -22,7 +22,7 @@ def _loader() -> TenantConfigLoader:
def test_platform_neutral_defaults_load(): def test_platform_neutral_defaults_load():
cfg = _loader().load("ktcommerce") cfg = _loader().load("_base")
# 우리 플랫폼 중립 기본값 (CLEANROOM.md) # 우리 플랫폼 중립 기본값 (CLEANROOM.md)
assert cfg.state.revenue.thresholds == [10_000_000, 50_000_000] assert cfg.state.revenue.thresholds == [10_000_000, 50_000_000]
@ -47,12 +47,12 @@ def test_platform_neutral_defaults_load():
def test_state_space_and_action_space_size(): def test_state_space_and_action_space_size():
cfg = _loader().load("ktcommerce") cfg = _loader().load("_base")
assert cfg.state.state_space_size == 162 # 3×3×3×3×2 (차원 구성은 기능적 설계) assert cfg.state.state_space_size == 162 # 3×3×3×3×2 (차원 구성은 기능적 설계)
assert cfg.action_mapping.action_space_size == 11 assert cfg.action_mapping.action_space_size == 11
# 합성 데모 카드 코드 (우리 스킴) # 공용 카드 코드 (파일 폴백 스냅샷 — 정본은 DB 카탈로그)
assert cfg.action_mapping.action_to_card["0"] == "NGC-A001" assert cfg.action_mapping.action_to_card["0"] == "NGC-001"
assert cfg.action_mapping.action_to_card["8"] == "NGC-A009" assert cfg.action_mapping.action_to_card["8"] == "NGC-009"
def test_base_deep_merge_unit(): def test_base_deep_merge_unit():
@ -90,7 +90,6 @@ def test_base_self_does_not_inherit():
def test_is_registered(): def test_is_registered():
loader = _loader() loader = _loader()
assert loader.is_registered("ktcommerce") is True
assert loader.is_registered("imarketkorea") is True assert loader.is_registered("imarketkorea") is True
# 미등록 company_id(uuid 등)는 _base 자동 온보딩 대상이라 '등록됨'으로 본다. 빈 키만 미등록. # 미등록 company_id(uuid 등)는 _base 자동 온보딩 대상이라 '등록됨'으로 본다. 빈 키만 미등록.
assert loader.is_registered("00000000-0000-0000-0000-000000000001") is True assert loader.is_registered("00000000-0000-0000-0000-000000000001") is True
@ -99,7 +98,7 @@ def test_is_registered():
def test_no_proprietary_card_codes_or_labels_in_repo(): def test_no_proprietary_card_codes_or_labels_in_repo():
"""클린룸 가드: 독점 카드 코드/ verbatim 라벨이 로드된 config 에 존재하지 않는다.""" """클린룸 가드: 독점 카드 코드/ verbatim 라벨이 로드된 config 에 존재하지 않는다."""
for tid in ("_base", "ktcommerce", "imarketkorea"): for tid in ("_base", "imarketkorea"):
cfg = _loader().load(tid) cfg = _loader().load(tid)
cards = " ".join(cfg.action_mapping.action_to_card.values()) cards = " ".join(cfg.action_mapping.action_to_card.values())
assert "NC26" not in cards # 참고 엔진의 고유 카드 코드 assert "NC26" not in cards # 참고 엔진의 고유 카드 코드

View File

@ -46,7 +46,7 @@ def _snapshot(**over) -> NegotiationSnapshot:
def test_build_state_deterministic_and_in_range(): def test_build_state_deterministic_and_in_range():
cfg = _cfg("ktcommerce") cfg = _cfg("_base")
snap = _snapshot() snap = _snapshot()
s1 = build_state(snap, cfg.state) s1 = build_state(snap, cfg.state)
s2 = build_state(snap, cfg.state) s2 = build_state(snap, cfg.state)
@ -65,7 +65,7 @@ def test_encode_index_known_example():
def test_mixed_radix_bijection_over_full_space(): def test_mixed_radix_bijection_over_full_space():
cfg = _cfg("ktcommerce") cfg = _cfg("_base")
dims = state_dims(cfg.state) dims = state_dims(cfg.state)
assert dims == [3, 3, 3, 3, 2] assert dims == [3, 3, 3, 3, 2]
seen = set() seen = set()
@ -77,17 +77,17 @@ def test_mixed_radix_bijection_over_full_space():
def test_config_injection_changes_classification(): def test_config_injection_changes_classification():
# revenue=20,000,000 원: ktcommerce(th=[10M,50M]) → mid(1), imarketkorea(th=[30M,100M]) → low(0) # revenue=20,000,000 원: _base(th=[10M,50M]) → mid(1), imarketkorea(th=[30M,100M]) → low(0)
snap = _snapshot(revenue_amount=20_000_000) snap = _snapshot(revenue_amount=20_000_000)
kt = build_state(snap, _cfg("ktcommerce").state) base = build_state(snap, _cfg("_base").state)
imk = build_state(snap, _cfg("imarketkorea").state) imk = build_state(snap, _cfg("imarketkorea").state)
assert kt.revenue_idx == 1 assert base.revenue_idx == 1
assert imk.revenue_idx == 0 assert imk.revenue_idx == 0
assert kt != imk # 같은 입력이 테넌트 config 에 따라 다른 상태 assert base != imk # 같은 입력이 테넌트 config 에 따라 다른 상태
def test_distribution_unknown_code_raises(): def test_distribution_unknown_code_raises():
cfg = _cfg("ktcommerce") cfg = _cfg("_base")
snap = _snapshot(distribution_code="Z") # code_map 에 없음 snap = _snapshot(distribution_code="Z") # code_map 에 없음
try: try:
build_state(snap, cfg.state) build_state(snap, cfg.state)
@ -97,7 +97,7 @@ def test_distribution_unknown_code_raises():
def test_price_zone_and_partner_buckets(): def test_price_zone_and_partner_buckets():
cfg = _cfg("ktcommerce").state cfg = _cfg("_base").state
# 제시가 ≤ 앵커가(9900) → 우선협상 구간(0) # 제시가 ≤ 앵커가(9900) → 우선협상 구간(0)
assert build_state(_snapshot(input_price=9800), cfg).price_zone_idx == 0 assert build_state(_snapshot(input_price=9800), cfg).price_zone_idx == 0
# 제시가 > 앵커가 → 협상 지속 구간(1) # 제시가 > 앵커가 → 협상 지속 구간(1)
@ -110,28 +110,28 @@ def test_price_zone_and_partner_buckets():
def test_reward_deterministic_and_config_driven(): def test_reward_deterministic_and_config_driven():
snap = _snapshot(outcome=NegotiationOutcome.FAILURE, round_number=2) snap = _snapshot(outcome=NegotiationOutcome.FAILURE, round_number=2)
kt = _cfg("ktcommerce") base = _cfg("_base")
imk = _cfg("imarketkorea") imk = _cfg("imarketkorea")
kt_rc = RewardCalculator(kt.reward, kt.state) # failure_penalty -0.5 base_rc = RewardCalculator(base.reward, base.state) # failure_penalty -0.5
imk_rc = RewardCalculator(imk.reward, imk.state) # failure_penalty -0.7 imk_rc = RewardCalculator(imk.reward, imk.state) # failure_penalty -0.7
r1 = kt_rc.calculate(snap) r1 = base_rc.calculate(snap)
r2 = kt_rc.calculate(snap) r2 = base_rc.calculate(snap)
assert r1 == r2 # 결정론 assert r1 == r2 # 결정론
assert r1.end_reward == -0.5 # config 반영 assert r1.end_reward == -0.5 # config 반영
assert imk_rc.calculate(snap).end_reward == -0.7 assert imk_rc.calculate(snap).end_reward == -0.7
# 성공 라운드 보상이 실패보다 크다 (방향성) # 성공 라운드 보상이 실패보다 크다 (방향성)
success = _snapshot(outcome=NegotiationOutcome.SUCCESS, round_number=0) success = _snapshot(outcome=NegotiationOutcome.SUCCESS, round_number=0)
assert kt_rc.calculate(success).total > kt_rc.calculate(_snapshot(outcome=NegotiationOutcome.FAILURE, round_number=0)).total assert base_rc.calculate(success).total > base_rc.calculate(_snapshot(outcome=NegotiationOutcome.FAILURE, round_number=0)).total
def test_action_card_mapper_roundtrip_and_mask(): def test_action_card_mapper_roundtrip_and_mask():
cfg = _cfg("ktcommerce") cfg = _cfg("_base")
mapper = ActionCardMapper(cfg.action_mapping) mapper = ActionCardMapper(cfg.action_mapping)
assert mapper.action_space_size == 11 assert mapper.action_space_size == 11
assert mapper.get_card_id(0) == "NGC-A001" assert mapper.get_card_id(0) == "NGC-001"
assert mapper.get_action_id("NGC-A001") == 0 assert mapper.get_action_id("NGC-001") == 0
assert mapper.get_card_id(99) is None assert mapper.get_card_id(99) is None
# 중복방지 마스킹: 사용한 action 제외 # 중복방지 마스킹: 사용한 action 제외

View File

@ -27,22 +27,22 @@ def _registry() -> TenantEngineRegistry:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_two_tenants_distinct_engines(): async def test_two_tenants_distinct_engines():
reg = _registry() reg = _registry()
e1 = await reg.get_engine("ktcommerce") e1 = await reg.get_engine("_base")
e2 = await reg.get_engine("imarketkorea") e2 = await reg.get_engine("imarketkorea")
assert e1 is not e2 assert e1 is not e2
assert e1.tenant_id == "ktcommerce" and e2.tenant_id == "imarketkorea" assert e1.tenant_id == "_base" and e2.tenant_id == "imarketkorea"
# 서로 다른 카드매핑 (다른 카드셋) # 서로 다른 카드매핑 (다른 카드셋 — _base=공용 카탈로그, imk=파일 오버라이드)
assert e1.mapper.get_card_id(0) == "NGC-A001" assert e1.mapper.get_card_id(0) == "NGC-001"
assert e2.mapper.get_card_id(0) == "NGC-B001" assert e2.mapper.get_card_id(0) == "NGC-B001"
# 차원 # 차원
assert e1.state_space_size == 162 and e1.action_space_size == 11 assert e1.state_space_size == 162 and e1.action_space_size == 9 # 카탈로그 9장(NGC-006·009 소프트삭제)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_engine_cached(): async def test_engine_cached():
reg = _registry() reg = _registry()
a = await reg.get_engine("ktcommerce") a = await reg.get_engine("imarketkorea")
b = await reg.get_engine("ktcommerce") b = await reg.get_engine("imarketkorea")
assert a is b # 캐시 — 동일 인스턴스 assert a is b # 캐시 — 동일 인스턴스
@ -58,7 +58,7 @@ async def test_concurrent_first_build_once():
return EngineFactory.build(config) return EngineFactory.build(config)
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0), factory=CountingFactory) reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0), factory=CountingFactory)
results = await asyncio.gather(*[reg.get_engine("ktcommerce") for _ in range(12)]) results = await asyncio.gather(*[reg.get_engine("imarketkorea") for _ in range(12)])
# 모두 같은 인스턴스 + 1회만 조립 # 모두 같은 인스턴스 + 1회만 조립
assert all(r is results[0] for r in results) assert all(r is results[0] for r in results)
assert builds["n"] == 1 assert builds["n"] == 1
@ -69,9 +69,9 @@ async def test_unregistered_company_id_auto_onboards():
reg = _registry() reg = _registry()
# 미등록 company_id(uuid)는 _base 자동 온보딩 → 엔진 생성됨(베이스 9카드, 162 state). # 미등록 company_id(uuid)는 _base 자동 온보딩 → 엔진 생성됨(베이스 9카드, 162 state).
eng = await reg.get_engine("00000000-0000-0000-0000-000000000001") eng = await reg.get_engine("00000000-0000-0000-0000-000000000001")
assert eng.action_space_size == 11 and eng.state_space_size == 162 assert eng.action_space_size == 9 and eng.state_space_size == 162 # DB 카탈로그 9장
assert eng.company_id == "00000000-0000-0000-0000-000000000001" assert eng.company_id == "00000000-0000-0000-0000-000000000001"
assert reg.is_registered("ktcommerce") is True assert reg.is_registered("imarketkorea") is True
# 빈 키만 미등록 → KeyError # 빈 키만 미등록 → KeyError
with pytest.raises(KeyError): with pytest.raises(KeyError):
await reg.get_engine("") await reg.get_engine("")
@ -80,9 +80,9 @@ async def test_unregistered_company_id_auto_onboards():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_reload_rebuilds_only_that_tenant(): async def test_reload_rebuilds_only_that_tenant():
reg = _registry() reg = _registry()
a = await reg.get_engine("ktcommerce") a = await reg.get_engine("_base")
b = await reg.get_engine("imarketkorea") b = await reg.get_engine("imarketkorea")
reloaded = await reg.reload("ktcommerce") reloaded = await reg.reload("_base")
assert reloaded is not a # 재조립됨 assert reloaded is not a # 재조립됨
assert await reg.get_engine("imarketkorea") is b # 타테넌트는 그대로 assert await reg.get_engine("imarketkorea") is b # 타테넌트는 그대로
@ -171,8 +171,8 @@ async def test_demo_tenant_keeps_file_brand(db_engine):
loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0), loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0),
company_repo=_FakeCompany(), company_repo=_FakeCompany(),
) )
eng = await reg.get_engine("ktcommerce") # 비-UUID → 조회 안 함 eng = await reg.get_engine("imarketkorea") # 비-UUID → 조회 안 함
assert eng.config.resources.company_name == "데모상사 A" assert eng.config.resources.company_name == "데모상사 B"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -236,6 +236,6 @@ async def test_middleware_header_missing_unregistered_registered(client):
assert r.json().get("result", {}).get("desc") != "TENANT_NOT_REGISTERED" assert r.json().get("result", {}).get("desc") != "TENANT_NOT_REGISTERED"
# 등록 테넌트 → 미들웨어 통과 # 등록 테넌트 → 미들웨어 통과
r = await client.get("/v1/protected", headers={"X-Tenant-ID": "ktcommerce"}) r = await client.get("/v1/protected", headers={"X-Tenant-ID": "imarketkorea"})
assert r.status_code == 404 assert r.status_code == 404
assert r.json().get("result", {}).get("desc") != "TENANT_NOT_REGISTERED" assert r.json().get("result", {}).get("desc") != "TENANT_NOT_REGISTERED"

View File

@ -58,8 +58,8 @@ async def test_warm_start_copies_base_with_decayed_visits(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_cold_start_creates_warmstart_version(db_engine): async def test_cold_start_creates_warmstart_version(db_engine):
await _seed_base(A=11) # ktcommerce action_space=11 과 차원 일치해야 warm-start await _seed_base(A=11) # imarketkorea action_space=11 과 차원 일치해야 warm-start
eng = await _reg().get_engine("ktcommerce") # 활성 버전 없음 → cold-start eng = await _reg().get_engine("imarketkorea") # 활성 버전 없음 → cold-start
policy, version_id, repo = await QTablePolicyStore.load(eng) policy, version_id, repo = await QTablePolicyStore.load(eng)
err, ver = await repo.read(lambda s: repo.get_active_version(s)) err, ver = await repo.read(lambda s: repo.get_active_version(s))
assert ver.version_name == "v000_warmstart_from_base" assert ver.version_name == "v000_warmstart_from_base"
@ -69,27 +69,27 @@ async def test_cold_start_creates_warmstart_version(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_catalog_dim_change_migrates_preserving_learning(db_engine): async def test_catalog_dim_change_migrates_preserving_learning(db_engine):
"""카탈로그 카드 수 변경(9→11) 시 학습 보존 마이그레이션 — 겹치는 셀 복사 + 새 카드 fresh.""" """카탈로그 카드 수 변경(7→9) 시 학습 보존 마이그레이션 — 겹치는 셀 복사 + 새 카드 fresh."""
import uuid as _uuid import uuid as _uuid
cid = str(_uuid.uuid4()) cid = str(_uuid.uuid4())
# 이 회사 활성 버전을 A=9 로 시드 + 셀 (5,2)=0.9 # 이 회사 활성 버전을 A=7 로 시드 + 셀 (5,2)=0.9
repo = LearningRepository(cid) repo = LearningRepository(cid)
vid = await repo.get_or_create_active_version( vid = await repo.get_or_create_active_version(
state_space_size=162, action_space_size=9, learning_rate=0.1, discount_factor=0.95, state_space_size=162, action_space_size=7, learning_rate=0.1, discount_factor=0.95,
scope=2, version_name="old_v9") scope=2, version_name="old_v7")
await repo.upsert_cell(vid, state_index=5, action_id=2, q_value=0.9, count=7) await repo.upsert_cell(vid, state_index=5, action_id=2, q_value=0.9, count=7)
# 엔진(_base type:db → 카탈로그 11장) 로드 → 9≠11 감지 → 마이그레이션 # 엔진(_base type:db → 카탈로그 9장) 로드 → 7≠9 감지 → 마이그레이션
eng = await _reg().get_engine(cid) eng = await _reg().get_engine(cid)
assert eng.action_space_size == 11 assert eng.action_space_size == 9
policy, new_vid, _ = await QTablePolicyStore.load(eng) policy, new_vid, _ = await QTablePolicyStore.load(eng)
assert str(new_vid) != str(vid) # 새 버전 assert str(new_vid) != str(vid) # 새 버전
assert policy.qtable.q[5, 2] == 0.9 # 기존 학습 보존 assert policy.qtable.q[5, 2] == 0.9 # 기존 학습 보존
assert policy.qtable.q[5, 10] == 0.0 # 새 카드(action 10) fresh assert policy.qtable.q[5, 8] == 0.0 # 새 카드(action 8) fresh
assert policy.qtable.visits[5, 2] == 7 # 방문수도 보존 assert policy.qtable.visits[5, 2] == 7 # 방문수도 보존
# 새 버전이 활성 · 차원 11 # 새 버전이 활성 · 차원 11
err, active = await repo.read(lambda s: repo.get_active_version(s)) err, active = await repo.read(lambda s: repo.get_active_version(s))
assert str(active.version_id) == str(new_vid) and active.action_space_size == 11 assert str(active.version_id) == str(new_vid) and active.action_space_size == 9
@pytest.mark.asyncio @pytest.mark.asyncio
@ -140,7 +140,7 @@ async def test_no_base_returns_none(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_existing_version_not_warmstarted(db_engine): async def test_existing_version_not_warmstarted(db_engine):
await _seed_base() await _seed_base()
eng = await _reg().get_engine("ktcommerce") eng = await _reg().get_engine("imarketkorea")
# 첫 로드 → warm-start 버전 생성 # 첫 로드 → warm-start 버전 생성
await QTablePolicyStore.load(eng) await QTablePolicyStore.load(eng)
# 둘째 로드 → 기존 활성 버전 재사용(중복 warm-start 안 함) # 둘째 로드 → 기존 활성 버전 재사용(중복 warm-start 안 함)

View File

@ -7,7 +7,7 @@ reset-learning, reset-all, q-table/{versions,switch,current}, experience-logs, t
import pytest import pytest
H = {"X-Tenant-ID": "ktcommerce"} H = {"X-Tenant-ID": "imarketkorea"}
@pytest.mark.asyncio @pytest.mark.asyncio
@ -97,7 +97,7 @@ async def test_invalidate_and_reset_scoped(client, db_engine):
sid = cr.json()["session_id"] sid = cr.json()["session_id"]
if cr.json().get("chat_end"): if cr.json().get("chat_end"):
break break
H2 = {"X-Tenant-ID": "imarketkorea"} H2 = {"X-Tenant-ID": "00000000-0000-0000-0000-0000000000b2"} # 실고객사 모사(자동 온보딩)
sid2 = None sid2 = None
for ui in convo: for ui in convo:
cr = await client.post("/v1/chat", headers=H2, json={"session_id": sid2, "user_input": ui}) cr = await client.post("/v1/chat", headers=H2, json={"session_id": sid2, "user_input": ui})
@ -108,7 +108,7 @@ async def test_invalidate_and_reset_scoped(client, db_engine):
before2 = (await client.get("/v1/experience-logs", headers=H2)).json()["total"] before2 = (await client.get("/v1/experience-logs", headers=H2)).json()["total"]
assert before2 >= 1 assert before2 >= 1
# ktcommerce reset-all → imarketkorea 무영향 # imarketkorea reset-all → 타테넌트(H2) 무영향
assert (await client.post("/v1/reset-all", headers=H)).json()["success"] assert (await client.post("/v1/reset-all", headers=H)).json()["success"]
assert (await client.get("/v1/experience-logs", headers=H)).json()["total"] == 0 assert (await client.get("/v1/experience-logs", headers=H)).json()["total"] == 0
assert (await client.get("/v1/experience-logs", headers=H2)).json()["total"] == before2 assert (await client.get("/v1/experience-logs", headers=H2)).json()["total"] == before2

View File

@ -3,7 +3,7 @@
1. 전체 대화: 서비스안내담당자확인협상품목안내가격협상와일드카드협상완료협상종료(chat_end). 1. 전체 대화: 서비스안내담당자확인협상품목안내가격협상와일드카드협상완료협상종료(chat_end).
2. 가격협상 턴에서 카드 선택 + 학습(card_id, updated_q). 2. 가격협상 턴에서 카드 선택 + 학습(card_id, updated_q).
3. 와일드카드 발동(wild_card_1pct) + 종료 보상(success). 3. 와일드카드 발동(wild_card_1pct) + 종료 보상(success).
4. 브랜드 치환 테넌트별(데모상사 A/B), 클린룸(스크립트에 KT 흔적 없음). 4. 브랜드 치환(데모상사 B), 클린룸(스크립트에 KT 흔적 없음).
5. 경험로그 적재 + 종료 진행 에러. 5. 경험로그 적재 + 종료 진행 에러.
6. (HTTP) 헤더로 세션 시작 + 진행. 6. (HTTP) 헤더로 세션 시작 + 진행.
""" """
@ -44,7 +44,7 @@ async def _run(svc, eng, turns):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_full_conversation_reaches_completion(db_engine): async def test_full_conversation_reaches_completion(db_engine):
reset_sessions() reset_sessions()
eng = await _reg().get_engine("ktcommerce") eng = await _reg().get_engine("imarketkorea")
svc = ChatService() svc = ChatService()
# anchor=9900, target=10000(기본). 11000(>anchor*1.02)→가격협상(카드), # anchor=9900, target=10000(기본). 11000(>anchor*1.02)→가격협상(카드),
# 10000(anchor<p≤anchor*1.02)→1% 와일드카드→수락하면 협상완료. # 10000(anchor<p≤anchor*1.02)→1% 와일드카드→수락하면 협상완료.
@ -59,7 +59,7 @@ async def test_full_conversation_reaches_completion(db_engine):
# 가격협상 카드선택 + 학습 # 가격협상 카드선택 + 학습
nego = [r for r in out if r.step == "가격협상"] nego = [r for r in out if r.step == "가격협상"]
assert nego and nego[0].card_id and nego[0].card_id.startswith("NGC-A") assert nego and nego[0].card_id and nego[0].card_id.startswith("NGC-B")
assert nego[0].updated_q is not None assert nego[0].updated_q is not None
# 와일드카드 발동 (1% 인하) # 와일드카드 발동 (1% 인하)
@ -70,11 +70,11 @@ async def test_full_conversation_reaches_completion(db_engine):
assert done and done[0].reward_total is not None assert done and done[0].reward_total is not None
# 브랜드 치환 + 클린룸 # 브랜드 치환 + 클린룸
assert "데모상사 A" in out[0].script assert "데모상사 B" in out[0].script
assert not _KT.search(out[0].script) assert not _KT.search(out[0].script)
# 경험로그 적재 # 경험로그 적재
repo = LearningRepository("ktcommerce") repo = LearningRepository("imarketkorea")
err, cnt = await repo.read(lambda s: repo.count_experience(s)) err, cnt = await repo.read(lambda s: repo.count_experience(s))
assert cnt >= 1 assert cnt >= 1
@ -87,7 +87,7 @@ async def test_wildcard_1pct_accept_settles_at_offer_price(db_engine):
잡히던 버그 settled_price 인하가(: 19800) 내려와야 한다. 잡히던 버그 settled_price 인하가(: 19800) 내려와야 한다.
""" """
reset_sessions() reset_sessions()
eng = await _reg().get_engine("ktcommerce") eng = await _reg().get_engine("imarketkorea")
svc = ChatService() svc = ChatService()
# anchor=9900(기본). 10000 은 anchor*1.02(10098) 이내 → wild_card_1pct 발동, offer_1pct=9900. # anchor=9900(기본). 10000 은 anchor*1.02(10098) 이내 → wild_card_1pct 발동, offer_1pct=9900.
out = await _run(svc, eng, [None, "확인", "", "확인", "10000", "", "", out = await _run(svc, eng, [None, "확인", "", "확인", "10000", "", "",
@ -129,7 +129,7 @@ def test_card_id_fixed_mapping_and_selection_mask():
"""카드 정리 후: action_id↔카드는 테넌트 매핑으로 고정, 견적 선택은 available_mask 로 걸러진다. """카드 정리 후: action_id↔카드는 테넌트 매핑으로 고정, 견적 선택은 available_mask 로 걸러진다.
( 인덱스 방식 폐기 selected[action_id] 인덱싱은 견적마다 action_id 의미가 달라져 Q-table 오염.)""" ( 인덱스 방식 폐기 selected[action_id] 인덱싱은 견적마다 action_id 의미가 달라져 Q-table 오염.)"""
class _Mapper: class _Mapper:
_m = {i: f"NGC-A{i + 1:03d}" for i in range(11)} _m = {i: f"NGC-B{i + 1:03d}" for i in range(11)}
def get_card_id(self, a): def get_card_id(self, a):
return self._m.get(a) return self._m.get(a)
def get_action_id(self, num): def get_action_id(self, num):
@ -139,15 +139,15 @@ def test_card_id_fixed_mapping_and_selection_mask():
action_space_size = 11 action_space_size = 11
eng = _Engine() eng = _Engine()
session = ChatSession( session = ChatSession(
session_id="00000000-0000-0000-0000-000000000001", tenant_id="ktcommerce", company_id="ktcommerce", session_id="00000000-0000-0000-0000-000000000001", tenant_id="imarketkorea", company_id="imarketkorea",
context={"selected_nego_card_numbers": ["NGC-A003", "NGC-A008"]}, action_space_size=11, context={"selected_nego_card_numbers": ["NGC-B003", "NGC-B008"]}, action_space_size=11,
) )
# ① card_id 는 고정 매핑 (선택 리스트 인덱싱 아님) # ① card_id 는 고정 매핑 (선택 리스트 인덱싱 아님)
assert ChatService._card_id_for_action(eng, session, 0) == "NGC-A001" assert ChatService._card_id_for_action(eng, session, 0) == "NGC-B001"
assert ChatService._card_id_for_action(eng, session, 2) == "NGC-A003" assert ChatService._card_id_for_action(eng, session, 2) == "NGC-B003"
# ② 선택은 mask 로 — NGC-A003(action 2), NGC-A008(action 7) 만 pickable # ② 선택은 mask 로 — NGC-B003(action 2), NGC-B008(action 7) 만 pickable
mask = ChatService._selection_mask(eng, session) mask = ChatService._selection_mask(eng, session)
assert mask is not None and mask[2] and mask[7] assert mask is not None and mask[2] and mask[7]
assert not mask[0] and not mask[5] and mask.sum() == 2 assert not mask[0] and not mask[5] and mask.sum() == 2
@ -162,15 +162,40 @@ def test_card_id_fixed_mapping_and_selection_mask():
assert ChatService._selection_mask(eng, session) is None assert ChatService._selection_mask(eng, session) is None
def test_counter_display_price_equals_settlement():
"""회귀(표시가≠투찰가): 카운터 제시 중 멘트에 보이는 절충/중간 변수
(middle_price·target_mid_price) 수락 타결가(pending_counter_price) 정확히 일치해야 한다.
버그: WC-05(중간값 절충)에서 compute_counter target 클램프·prev_customer 갱신으로 1,700,000
pending 으로 적재하는데, vars_for {middle_price} 재계산해 1,740,000 으로 표시 화면엔
1,740,000 인데 실제로는 1,700,000 으로 투찰되던 문제. pending 으로 고정해 표시가==타결가."""
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
repo = ScriptRepository(cfg, _TENANTS_DIR)
engine = ChatEngine(repo, rq_type="재협상")
session = ChatSession(
session_id="00000000-0000-0000-0000-000000000009",
tenant_id="imarketkorea", company_id="imarketkorea", action_space_size=0,
context={
"anchor_price": 2000000, "target_price": 1700000, "input_price": 1780000,
"prev_customer_price": 1700000, # 종결 전술이 counter 로 덮어쓴 상태
"pending_counter_price": 1700000, # compute_counter 의 target 클램프 결과(실제 타결가)
},
)
v = engine.vars_for(session)
assert v["counter_price"] == 1700000
assert v["middle_price"] == 1700000 # 재계산값 1,740,000 이 아니라 pending
assert v["target_mid_price"] == 1700000
def test_default_1pct_wildcard_still_runs_without_selected_wildcard(): def test_default_1pct_wildcard_still_runs_without_selected_wildcard():
"""1% 인하는 기본 제공 카드라 DB 견적에서 와일드카드를 선택하지 않아도 발동한다.""" """1% 인하는 기본 제공 카드라 DB 견적에서 와일드카드를 선택하지 않아도 발동한다."""
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("ktcommerce") cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
repo = ScriptRepository(cfg, _TENANTS_DIR) repo = ScriptRepository(cfg, _TENANTS_DIR)
engine = ChatEngine(repo, rq_type="재협상") engine = ChatEngine(repo, rq_type="재협상")
session = ChatSession( session = ChatSession(
session_id="00000000-0000-0000-0000-000000000002", session_id="00000000-0000-0000-0000-000000000002",
tenant_id="ktcommerce", tenant_id="imarketkorea",
company_id="ktcommerce", company_id="imarketkorea",
step="가격협상_확인", step="가격협상_확인",
action_space_size=0, action_space_size=0,
context={ context={
@ -188,13 +213,13 @@ def test_default_1pct_wildcard_still_runs_without_selected_wildcard():
def test_budget_wildcard_requires_selected_wildcard_for_db_context(): def test_budget_wildcard_requires_selected_wildcard_for_db_context():
"""재원부족 구간은 DB 견적에서 와일드카드를 선택했을 때만 발동한다.""" """재원부족 구간은 DB 견적에서 와일드카드를 선택했을 때만 발동한다."""
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("ktcommerce") cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
repo = ScriptRepository(cfg, _TENANTS_DIR) repo = ScriptRepository(cfg, _TENANTS_DIR)
engine = ChatEngine(repo, rq_type="재협상") engine = ChatEngine(repo, rq_type="재협상")
session = ChatSession( session = ChatSession(
session_id="00000000-0000-0000-0000-000000000003", session_id="00000000-0000-0000-0000-000000000003",
tenant_id="ktcommerce", tenant_id="imarketkorea",
company_id="ktcommerce", company_id="imarketkorea",
step="가격협상_확인", step="가격협상_확인",
action_space_size=0, action_space_size=0,
context={ context={
@ -214,7 +239,7 @@ def test_budget_wildcard_requires_selected_wildcard_for_db_context():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_priority_completes_without_wildcard(db_engine): async def test_priority_completes_without_wildcard(db_engine):
reset_sessions() reset_sessions()
eng = await _reg().get_engine("ktcommerce") eng = await _reg().get_engine("imarketkorea")
svc = ChatService() svc = ChatService()
# 첫 제시가가 앵커가(9900) 이하 → 우선협상 → 바로 협상완료(카드/와일드카드 없이) # 첫 제시가가 앵커가(9900) 이하 → 우선협상 → 바로 협상완료(카드/와일드카드 없이)
out = await _run(svc, eng, [None, "확인", "", "확인", "9800", "", out = await _run(svc, eng, [None, "확인", "", "확인", "9800", "",
@ -237,7 +262,7 @@ async def test_tenant_brand_isolation(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_advance_after_end_errors(db_engine): async def test_advance_after_end_errors(db_engine):
reset_sessions() reset_sessions()
eng = await _reg().get_engine("ktcommerce") eng = await _reg().get_engine("imarketkorea")
svc = ChatService() svc = ChatService()
out = await _run(svc, eng, [None, "확인", "", "확인", "1000", "", out = await _run(svc, eng, [None, "확인", "", "확인", "1000", "",
"협상 내용을 확인했으며, 이의가 없음에 동의합니다."]) "협상 내용을 확인했으며, 이의가 없음에 동의합니다."])
@ -248,12 +273,12 @@ async def test_advance_after_end_errors(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_http_chat_start(client): async def test_http_chat_start(client):
r = await client.post("/v1/chat", headers={"X-Tenant-ID": "ktcommerce"}, json={"rq_type": "재협상"}) r = await client.post("/v1/chat", headers={"X-Tenant-ID": "imarketkorea"}, json={"rq_type": "재협상"})
assert r.status_code == 200 assert r.status_code == 200
d = r.json() d = r.json()
assert d["step"] == "서비스안내" assert d["step"] == "서비스안내"
assert d["input_options"] == ["확인"] assert d["input_options"] == ["확인"]
assert "데모상사 A" in d["script"] assert "데모상사 B" in d["script"]
# 헤더 없으면 400 # 헤더 없으면 400
r2 = await client.post("/v1/chat", json={"rq_type": "재협상"}) r2 = await client.post("/v1/chat", json={"rq_type": "재협상"})
assert r2.status_code == 400 assert r2.status_code == 400

View File

@ -25,7 +25,7 @@ def _eng():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_session_persists_and_resumes_across_instances(db_engine): async def test_session_persists_and_resumes_across_instances(db_engine):
reg = _eng() reg = _eng()
eng = await reg.get_engine("ktcommerce") eng = await reg.get_engine("imarketkorea")
# 인스턴스 1: 협상 시작 + 몇 턴 진행 (컨텍스트는 DB 조회 — 행이 없으므로 기본값 폴백) # 인스턴스 1: 협상 시작 + 몇 턴 진행 (컨텍스트는 DB 조회 — 행이 없으므로 기본값 폴백)
svc1 = ChatService() svc1 = ChatService()
@ -51,19 +51,19 @@ async def test_session_persists_and_resumes_across_instances(db_engine):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_session_company_scoped(db_engine): async def test_session_company_scoped(db_engine):
reg = _eng() reg = _eng()
eng = await reg.get_engine("ktcommerce") eng = await reg.get_engine("imarketkorea")
svc = ChatService() svc = ChatService()
r = await svc.chat(eng, Req_Chat()) r = await svc.chat(eng, Req_Chat())
sid = r.session_id sid = r.session_id
# 자사(ktcommerce)로는 조회됨 # 자사(imarketkorea)로는 조회됨
assert await ChatSessionRepository(eng.company_id).get(sid) is not None assert await ChatSessionRepository(eng.company_id).get(sid) is not None
# 타테넌트(imarketkorea) company_id 로는 조회 안 됨 (격리) # 타테넌트 company_id 로는 조회 안 됨 (격리)
assert await ChatSessionRepository("imarketkorea").get(sid) is None assert await ChatSessionRepository("00000000-0000-0000-0000-0000000000aa").get(sid) is None
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_none_for_missing(db_engine): async def test_get_none_for_missing(db_engine):
repo = ChatSessionRepository("ktcommerce") repo = ChatSessionRepository("imarketkorea")
assert await repo.get(None) is None assert await repo.get(None) is None
assert await repo.get("00000000-0000-0000-0000-000000000000") is None assert await repo.get("00000000-0000-0000-0000-000000000000") is None

View File

@ -0,0 +1,157 @@
"""Phase 2 표현층 검증 — ScriptNaturalizer (LLM 카드 멘트 자연화).
원칙 검증: LLM 말만 다듬고 숫자는 절대 만들지 않는다.
치환자 보존 성공 경로 치환자 누락/추가 폐기 숫자 폐기
타임아웃/예외 폐기(폴백) 상황 라벨은 정성(수치 미노출)
플로우: llm.enabled=True 카드 멘트가 자연화본으로, 실패 원본으로.
"""
import asyncio
import os
import pytest
from negotiation.chat.service.script_naturalizer import ScriptNaturalizer, build_situation
_TEMPLATE = "제안해 주신 **{input_price}원**, 감사합니다. 한 번 더 검토해 가격을 제안해 주시겠어요?"
def _fake(reply):
"""llm_call 더블 — 고정 응답."""
def call(messages):
return {"script": reply}
return call
@pytest.mark.asyncio
async def test_naturalize_success_preserves_placeholders():
nat = ScriptNaturalizer(llm_call=_fake(
"긍정적으로 검토 중입니다. 다만 **{input_price}원**은 조정 여지가 있어 보입니다. 재제안 부탁드립니다."))
out = await nat.naturalize(_TEMPLATE, situation={"라운드": "초반 조율"}, tone=2, strategy=1)
assert out and "{input_price}" in out and out != _TEMPLATE
@pytest.mark.asyncio
async def test_naturalize_rejects_missing_placeholder():
nat = ScriptNaturalizer(llm_call=_fake("가격 재검토 부탁드립니다.")) # 치환자 삭제됨
assert await nat.naturalize(_TEMPLATE) is None
@pytest.mark.asyncio
async def test_naturalize_rejects_added_placeholder():
nat = ScriptNaturalizer(llm_call=_fake("{input_price}원과 {secret_discount}까지 드리겠습니다."))
assert await nat.naturalize(_TEMPLATE) is None # 원본에 없던 변수 환각
@pytest.mark.asyncio
async def test_naturalize_rejects_new_digits():
nat = ScriptNaturalizer(llm_call=_fake("{input_price}원에서 5% 더 인하해 주시면 즉시 계약하겠습니다."))
assert await nat.naturalize(_TEMPLATE) is None # LLM 이 만든 숫자(5) 금지
@pytest.mark.asyncio
async def test_naturalize_rejects_dropped_emphasis_markers():
"""고객사가 지정한 볼드/색 마커를 LLM 이 떨어뜨리면 폐기 → 원본(스타일 보존) 폴백."""
tmpl = "제안해 주신 **{input_price}원**, {{강조|재검토}} 부탁드립니다."
# 볼드·색 마커를 모두 지운 재작성 → 검증 실패
nat = ScriptNaturalizer(llm_call=_fake("제안해 주신 {input_price}원, 재검토 부탁드립니다."))
assert await nat.naturalize(tmpl) is None
# 마커를 그대로 유지한 재작성 → 통과
nat = ScriptNaturalizer(llm_call=_fake("제시하신 **{input_price}원** 관련, {{강조|재검토}}를 요청드립니다."))
out = await nat.naturalize(tmpl)
assert out and out.count("**") == 2 and "{{강조|" in out
@pytest.mark.asyncio
async def test_naturalize_timeout_falls_back():
def slow(messages):
import time
time.sleep(0.5)
return {"script": _TEMPLATE}
nat = ScriptNaturalizer(llm_call=slow, timeout_seconds=0.05)
assert await nat.naturalize(_TEMPLATE) is None
@pytest.mark.asyncio
async def test_naturalize_exception_falls_back():
def boom(messages):
raise RuntimeError("LLM down")
nat = ScriptNaturalizer(llm_call=boom)
assert await nat.naturalize(_TEMPLATE) is None
def test_build_situation_is_qualitative_only():
"""상황 라벨에 실제 수치가 노출되지 않는다(숫자 환각 차단의 전제)."""
ctx = {"round": 2, "input_price": 10200, "anchor_price": 9900, "target_price": 10000, "item_price": 11000}
s = build_situation(ctx)
assert s["라운드"] == "초반 조율"
assert s["가격구간"] == "목표 상회(추가 인하 필요)"
joined = str(s)
for n in ("10200", "9900", "10000", "11000"):
assert n not in joined # 수치 미노출
@pytest.mark.asyncio
async def test_chat_flow_uses_naturalized_script_when_llm_enabled(db_engine):
"""llm.enabled=True + 자격증명 존재 시 가격협상 카드 멘트가 자연화본(치환 완료)으로 나온다."""
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
reset_sessions()
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("imarketkorea")
eng.config.llm.enabled = True # 테넌트 게이트 on
svc = ChatService()
svc._naturalizer = ScriptNaturalizer(llm_call=_fake(
"【자연화】 제시해 주신 **{input_price}원** 잘 검토했습니다. 초반 조율 단계이니 한 걸음 더 부탁드립니다."))
# 자격증명 게이트 우회(테스트 환경에 키가 없어도 동작 검증)
orig_available = ScriptNaturalizer.available
ScriptNaturalizer.available = staticmethod(lambda: True)
try:
sid = None
for ui in [None, "확인", "", "확인", "11000", ""]:
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input=ui))
sid = r.session_id
assert r.step == "가격협상" and r.card_id
assert r.script.startswith("【자연화】") # LLM 재작성본 사용
assert "11000" in r.script # 치환은 엔진이 수행(숫자 정확)
assert "{input_price}" not in r.script # 치환 완료
finally:
ScriptNaturalizer.available = orig_available
eng.config.llm.enabled = False
@pytest.mark.asyncio
async def test_chat_flow_falls_back_to_template_on_llm_failure(db_engine):
"""LLM 실패 시 카드 원본 멘트로 폴백 — 협상은 절대 멈추지 않는다."""
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
reset_sessions()
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
eng = await reg.get_engine("imarketkorea")
eng.config.llm.enabled = True
def boom(messages):
raise RuntimeError("LLM down")
svc = ChatService()
svc._naturalizer = ScriptNaturalizer(llm_call=boom)
orig_available = ScriptNaturalizer.available
ScriptNaturalizer.available = staticmethod(lambda: True)
try:
sid = None
for ui in [None, "확인", "", "확인", "11000", ""]:
r = await svc.chat(eng, Req_Chat(session_id=sid, user_input=ui))
sid = r.session_id
assert r.step == "가격협상" and r.card_id
assert r.script and "11000" in r.script # 원본 템플릿 + 치환으로 정상 응답
finally:
ScriptNaturalizer.available = orig_available
eng.config.llm.enabled = False

View File

@ -20,7 +20,7 @@ _TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__fi
_FORBIDDEN = re.compile(r"kt\s*commerce|케이티|커머스|nego-?wiz", re.IGNORECASE) _FORBIDDEN = re.compile(r"kt\s*commerce|케이티|커머스|nego-?wiz", re.IGNORECASE)
def _repo(tenant_id="ktcommerce"): def _repo(tenant_id="imarketkorea"):
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load(tenant_id) cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load(tenant_id)
return ScriptRepository(cfg, _TENANTS_DIR) return ScriptRepository(cfg, _TENANTS_DIR)
@ -42,7 +42,10 @@ def test_requote_structure_preserved():
for key in ["서비스안내", "가격제안", "배송형태선택", "가격협상_확인", "결과안내", "결과제출", "협상종료"]: for key in ["서비스안내", "가격제안", "배송형태선택", "가격협상_확인", "결과안내", "결과제출", "협상종료"]:
assert key in s assert key in s
assert s["배송형태선택"]["next_input_mode"] == "delivery_type" assert s["배송형태선택"]["next_input_mode"] == "delivery_type"
assert s["배송형태선택"]["input_options"] == ["협력사배송", "지정택배배송", "픽업배송"] # 리소스 원본은 회사 용어 토큰({label_*}) — 렌더 시 회사 라벨(없으면 기본값)로 치환된다.
assert s["배송형태선택"]["input_options"] == [
"{label_delivery_type_1}", "{label_delivery_type_2}", "{label_delivery_type_3}",
]
def test_wildcard_present_and_merged(): def test_wildcard_present_and_merged():
@ -65,11 +68,9 @@ def test_cleanroom_no_proprietary_brand_in_any_resource():
def test_brand_substitution_per_tenant(): def test_brand_substitution_per_tenant():
kt = _repo("ktcommerce").get_step("서비스안내", "재협상")
im = _repo("imarketkorea").get_step("서비스안내", "재협상") im = _repo("imarketkorea").get_step("서비스안내", "재협상")
assert "데모상사 A" in kt["script"] and "Negosium" in kt["script"] assert "데모상사 B" in im["script"] and "Negosium" in im["script"]
assert "데모상사 B" in im["script"] assert "{company_name}" not in im["script"] # 치환 완료
assert "{company_name}" not in kt["script"] # 치환 완료
def test_variable_substitution_and_missing_kept(): def test_variable_substitution_and_missing_kept():
@ -103,12 +104,16 @@ async def test_resolve_card_script_file_mode_default():
repo = _repo() repo = _repo()
assert repo._config.cards.source_type == "file" assert repo._config.cards.source_type == "file"
# action 0 파일 멘트가 변수 치환되어 나온다 (DB 무접근) # action 0 파일 멘트가 변수 치환되어 나온다 (DB 무접근)
out = await repo.resolve_card_script(0, "NGC-A001", {"input_price": 9800}) out = await repo.resolve_card_script(0, "NGC-B001", {"input_price": 9800})
assert out and "9800" in out assert out and "9800" in out
class _FakeCardRepo: from negotiation.cards.ports.card_script_port import ICardScriptRepository
"""ICardScriptRepository 더블 — DB 없이 카드코드→멘트 매핑만 흉내(세션 인자 무시)."""
class _FakeCardRepo(ICardScriptRepository):
"""ICardScriptRepository 더블 — DB 없이 카드코드→멘트 매핑만 흉내(세션 인자 무시).
포트 상속으로 get_card_by_number 기본 구현(메타 None) 물려받는다."""
def __init__(self, by_number: dict): def __init__(self, by_number: dict):
self._by = by_number self._by = by_number
@ -121,9 +126,9 @@ class _FakeCardRepo:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_resolve_card_script_db_mode_prefers_db(monkeypatch): async def test_resolve_card_script_db_mode_prefers_db(monkeypatch):
"""source_type='backoffice_db': card.nego_cards.script(정본)를 파일보다 우선 사용 + 마커 보존.""" """source_type='backoffice_db': card.nego_cards.script(정본)를 파일보다 우선 사용 + 마커 보존."""
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("ktcommerce") cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
cfg.cards.source_type = "backoffice_db" cfg.cards.source_type = "backoffice_db"
fake = _FakeCardRepo({"NGC-A001": "DB 편집 멘트 **{input_price}원** 검토 중입니다."}) fake = _FakeCardRepo({"NGC-B001": "DB 편집 멘트 **{input_price}원** 검토 중입니다."})
repo = ScriptRepository(cfg, _TENANTS_DIR, card_repo=fake) repo = ScriptRepository(cfg, _TENANTS_DIR, card_repo=fake)
# execute_lambda 를 세션 없이 콜백만 실행하도록 대체(순수 단위검증) # execute_lambda 를 세션 없이 콜백만 실행하도록 대체(순수 단위검증)
@ -132,14 +137,14 @@ async def test_resolve_card_script_db_mode_prefers_db(monkeypatch):
from negotiation.chat.service import script_repository as _sr from negotiation.chat.service import script_repository as _sr
monkeypatch.setattr(_sr.DB_SESSION_MNG, "execute_lambda", _fake_lambda) monkeypatch.setattr(_sr.DB_SESSION_MNG, "execute_lambda", _fake_lambda)
out = await repo.resolve_card_script(0, "NGC-A001", {"input_price": 9800}) out = await repo.resolve_card_script(0, "NGC-B001", {"input_price": 9800})
assert out == "DB 편집 멘트 **9800원** 검토 중입니다." # DB 우선 + 마커(**) 불투명 보존 + 변수 치환 assert out == "DB 편집 멘트 **9800원** 검토 중입니다." # DB 우선 + 마커(**) 불투명 보존 + 변수 치환
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_resolve_card_script_db_mode_falls_back_to_file(monkeypatch): async def test_resolve_card_script_db_mode_falls_back_to_file(monkeypatch):
"""DB 에 해당 카드 멘트가 없으면 파일(scripts_cards.json)로 폴백.""" """DB 에 해당 카드 멘트가 없으면 파일(scripts_cards.json)로 폴백."""
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("ktcommerce") cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
cfg.cards.source_type = "backoffice_db" cfg.cards.source_type = "backoffice_db"
fake = _FakeCardRepo({}) # DB 미보유 fake = _FakeCardRepo({}) # DB 미보유
repo = ScriptRepository(cfg, _TENANTS_DIR, card_repo=fake) repo = ScriptRepository(cfg, _TENANTS_DIR, card_repo=fake)
@ -149,14 +154,14 @@ async def test_resolve_card_script_db_mode_falls_back_to_file(monkeypatch):
from negotiation.chat.service import script_repository as _sr from negotiation.chat.service import script_repository as _sr
monkeypatch.setattr(_sr.DB_SESSION_MNG, "execute_lambda", _fake_lambda) monkeypatch.setattr(_sr.DB_SESSION_MNG, "execute_lambda", _fake_lambda)
out = await repo.resolve_card_script(0, "NGC-A001", {"input_price": 9800}) out = await repo.resolve_card_script(0, "NGC-B001", {"input_price": 9800})
assert out and "9800" in out # 파일 폴백 멘트 assert out and "9800" in out # 파일 폴백 멘트
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_resolve_card_script_prefer_db_for_selected_cards(monkeypatch): async def test_resolve_card_script_prefer_db_for_selected_cards(monkeypatch):
"""견적에서 선택된 백오피스 카드 번호는 file 모드여도 DB 멘트를 우선한다.""" """견적에서 선택된 백오피스 카드 번호는 file 모드여도 DB 멘트를 우선한다."""
cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("ktcommerce") cfg = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0).load("imarketkorea")
assert cfg.cards.source_type == "file" assert cfg.cards.source_type == "file"
fake = _FakeCardRepo({"2": "선택 카드 DB 멘트 **{input_price}원**"}) fake = _FakeCardRepo({"2": "선택 카드 DB 멘트 **{input_price}원**"})
repo = ScriptRepository(cfg, _TENANTS_DIR, card_repo=fake) repo = ScriptRepository(cfg, _TENANTS_DIR, card_repo=fake)
@ -168,3 +173,24 @@ async def test_resolve_card_script_prefer_db_for_selected_cards(monkeypatch):
out = await repo.resolve_card_script(1, "2", {"input_price": 10200}, prefer_db=True) out = await repo.resolve_card_script(1, "2", {"input_price": 10200}, prefer_db=True)
assert out == "선택 카드 DB 멘트 **10200원**" assert out == "선택 카드 DB 멘트 **10200원**"
def test_option_label_tokens_rendered():
"""검증: 옵션에 회사 용어 토큰({label_delivery_type_*})이 있는 스텝을 정상 렌더·에러 재렌더로 출력.
기대결과: 경로 모두 버튼 문자열이 기본 라벨(협력사배송 ) 치환되고 토큰이 남지 않는다."""
import os
from negotiation.chat.service.chat_engine import ChatEngine, ChatSession
from tenancy.config_loader import TenantConfigLoader
tenants = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
cfg = TenantConfigLoader(tenants_dir=tenants, cache_ttl_seconds=0).load("_base")
engine = ChatEngine(ScriptRepository(cfg, tenants), rq_type="재견적")
session = ChatSession(session_id="s", tenant_id="_base", company_id="_base")
view = engine.render_step(session, "배송형태선택")
assert view.input_options == ["협력사배송", "지정택배배송", "픽업배송"]
# 에러 재렌더(잘못된 입력 등)도 같은 치환을 타야 한다 — raw 옵션이면 토큰이 버튼에 노출된다.
err_view = engine._error(session, "다시 선택해 주세요.")
assert err_view.input_options == ["협력사배송", "지정택배배송", "픽업배송"]

View File

@ -1,63 +0,0 @@
"""카드 스크립트 → 임베딩 캐시 생성 (action-as-feature 준비, 1회 실행).
card.nego_cards(11) name+script 문장 임베딩으로 변환해 artifacts/card_embeddings.npz 저장.
카드가 추가되면 스크립트를 다시 돌리면 된다( 카드만 임베딩돼 캐시에 합류).
실행:
APP_ENV=local python -m tools.build_card_embeddings
출력:
artifacts/card_embeddings.npz (numbers, names, strategy, tone, embeddings[N,384])
"""
import asyncio
import os
import numpy as np
_HERE = os.path.dirname(os.path.abspath(__file__))
ARTIFACTS = os.path.join(_HERE, "..", "artifacts")
OUT_PATH = os.path.join(ARTIFACTS, "card_embeddings.npz")
MODEL_NAME = "paraphrase-multilingual-MiniLM-L12-v2" # 384차원, 한국어 지원, 로컬/무료
async def load_cards():
"""card.nego_cards 에서 (number, name, script, strategy_type, tone) 로드."""
import asyncpg
conn = await asyncpg.connect(
host="127.0.0.1", port=5432, user="postgres", password="password", database="negosium_db")
try:
rows = await conn.fetch(
"SELECT number, name, script, strategy_type, tone FROM card.nego_cards "
"WHERE deleted = FALSE ORDER BY number")
return [(r["number"], r["name"], r["script"], r["strategy_type"], r["tone"]) for r in rows]
finally:
await conn.close()
def main():
cards = asyncio.run(load_cards())
if not cards:
raise SystemExit("card.nego_cards 가 비어있음 — DB 시드 확인 (docker start negosium-pg)")
print(f"카드 {len(cards)}장 로드: {[c[0] for c in cards]}")
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(MODEL_NAME)
texts = [f"{name}. {script}" for _, name, script, _, _ in cards]
emb = model.encode(texts, normalize_embeddings=True) # [N, 384], 단위벡터
print(f"임베딩 shape: {emb.shape}")
os.makedirs(ARTIFACTS, exist_ok=True)
np.savez(
OUT_PATH,
numbers=np.array([c[0] for c in cards]),
names=np.array([c[1] for c in cards]),
strategy=np.array([c[3] for c in cards], dtype=np.int64),
tone=np.array([c[4] for c in cards], dtype=np.int64),
embeddings=emb.astype(np.float32),
)
print(f"저장: {OUT_PATH}")
if __name__ == "__main__":
main()

View File

@ -1,251 +0,0 @@
"""기존 Q-Table(UCB) vs action-as-feature DQN 공정 비교 — 고객사 성향 조건화 환경 (최종).
같은 환경(FeatureBuyer 2 + 협력사·고객사성향 랜덤)에서 동일 에피소드로 학습·평가.
- Q-Table: 이산 state 162 + 카드=슬롯. 성향(고객사) 입력 자체가 불가능 평균 성향에 수렴
- DQN : 연속 상태 + 성향 벡터 + 카드 특징(임베딩+전략/ one-hot)
평가 4:
학습 카드 9 평균보상(진짜 목적함수) + top3 적중(MC 정답 기준)
zero-shot 11 카드 2 포함
카드 사용률 구조적 차이
성향 극단 테스트 같은 협력사, 성향만 바꿨을 카드를 바꾸는가
실행: APP_ENV=local python -m tools.compare_qtable_vs_dqn
"""
import random
import numpy as np
import torch
from eval_harness.buyer import Scenario
from eval_harness.feature_buyer import FeatureBuyer, SupplierProfile, sample_supplier
from negotiation.policies.feature_dqn_policy import FeatureDQNPolicy
from negotiation.policies.qtable_policy import UCBQTablePolicy
from negotiation.policies.base import EpisodeState, PolicyContext, Transition
from negotiation.qtable.domain.model.q_table import QTable
from negotiation.qtable.domain.model.snapshot import NegotiationOutcome
from negotiation.qtable.domain.service.feature_builder import (
STATE_FEATURE_DIM, TENANT_FEATURE_DIM, build_state_features)
from negotiation.qtable.domain.service.reward_calculator import RewardCalculator
from negotiation.qtable.domain.service.state_calculator import state_index
from tenancy.config_loader import TenantConfigLoader
from tools.train_feature_dqn import (
ANCHOR, HOLDOUT, MAX_TURNS, TARGET, load_cards, make_snapshot, pref_config, sample_tenant_pref)
# ---- 정책 어댑터 ------------------------------------------------------------------
class DQNAdapter:
name = "feature_dqn"
def __init__(self, policy, feat):
self.p, self.feat = policy, feat
def _sf(self, snap, tf):
return np.concatenate([build_state_features(snap), tf])
def choose(self, snap, tf, avail, greedy):
self.p.greedy = greedy
i, _, _ = self.p.select(self._sf(snap, tf), np.stack([self.feat[c] for c in avail]))
return avail[i]
def learn(self, snap, tf, card, reward, next_snap, next_avail, done):
sf = self._sf(snap, tf)
if done or next_snap is None:
self.p.remember(sf, self.feat[card], reward, None, None, True)
else:
self.p.remember(sf, self.feat[card], reward, self._sf(next_snap, tf),
np.stack([self.feat[c] for c in next_avail]), False)
self.p.train_step()
class QTableAdapter:
"""기존 UCBQTablePolicy. 성향(tf)은 구조상 받을 수 없다 — 이산 state 162칸에 그 축이 없음."""
name = "qtable_ucb"
def __init__(self, all_numbers, state_cfg, lr=0.1, gamma=0.95):
self.numbers = list(all_numbers)
self.a_of = {n: i for i, n in enumerate(self.numbers)}
self.state_cfg = state_cfg
self.qt = QTable(162, len(self.numbers), learning_rate=lr, discount_factor=gamma)
self.pol = UCBQTablePolicy(self.qt)
def choose(self, snap, tf, avail, greedy):
idx = state_index(snap, self.state_cfg)
if greedy:
q = self.qt.row(idx)
return max(avail, key=lambda c: q[self.a_of[c]])
mask = np.zeros(len(self.numbers), dtype=bool)
for c in avail:
mask[self.a_of[c]] = True
ctx = PolicyContext(state_index=idx, snapshot=snap, action_space_size=len(self.numbers),
episode=EpisodeState(), available_mask=mask)
return self.numbers[self.pol.select(ctx).action_id]
def learn(self, snap, tf, card, reward, next_snap, next_avail, done):
idx = state_index(snap, self.state_cfg)
nidx = state_index(next_snap, self.state_cfg) if (next_snap is not None and not done) else None
self.pol.update(Transition(state_index=idx, action_id=self.a_of[card], reward=reward,
next_state_index=nidx, done=done))
class RandomAdapter:
name = "random"
def __init__(self, seed=0):
self.rng = np.random.default_rng(seed)
def choose(self, snap, tf, avail, greedy):
return avail[self.rng.integers(len(avail))]
def learn(self, *a, **k):
pass
# ---- 공용 에피소드 -----------------------------------------------------------------
def run_episode(adapter, sup, tf, pool, strat, rc, seed, learn=True, greedy=False, forced_first=None):
buyer = FeatureBuyer(sup, strat, seed=seed, max_turns=MAX_TURNS)
scenario = Scenario(anchor_price=ANCHOR, target_price=TARGET, revenue_amount=sup.revenue_amount,
distribution_code=sup.distribution_code, partner_count=sup.partner_count)
price0 = TARGET * 1.15
price, used, total_r, first_card = price0, set(), 0.0, None
for turn in range(1, MAX_TURNS + 1):
acceptance = max(0.0, (price0 - price) / price0)
snap = make_snapshot(sup, price, turn, acceptance)
avail = [c for c in pool if c not in used] or list(pool)
if turn == 1 and forced_first is not None:
card = forced_first
else:
card = adapter.choose(snap, tf, avail, greedy)
used.add(card)
if first_card is None:
first_card = card
resp = buyer.respond(card, scenario, turn, price)
price = resp.new_price
done = resp.accept or price <= ANCHOR or turn >= MAX_TURNS
success = resp.accept or price <= ANCHOR
outcome = (NegotiationOutcome.SUCCESS if success
else NegotiationOutcome.FAILURE if done else NegotiationOutcome.ONGOING)
# 채점은 최종 결과 시점만 (중간 턴 0 → γ 부트스트랩으로 전파).
# 진행 중 보상을 누적하면 '질질 끄는 전략'이 부당하게 유리해지는 인공물이 생긴다.
r = rc.calculate(make_snapshot(sup, price, turn, acceptance, outcome)).total if done else 0.0
total_r += r
if learn:
if done:
adapter.learn(snap, tf, card, r, None, None, True)
else:
acc2 = max(0.0, (price0 - price) / price0)
nsnap = make_snapshot(sup, price, turn + 1, acc2)
navail = [c for c in pool if c not in used] or list(pool)
adapter.learn(snap, tf, card, r, nsnap, navail, False)
if done:
return total_r, success, price, first_card
return total_r, False, price, first_card
# ---- MC 정답 랭킹: 이 (협력사, 성향)에서 진짜 좋은 첫 카드 top-k ---------------------
_rand = RandomAdapter(seed=1)
def rank_cards_mc(sup, tf, pool, strat, rc, seed, sims=6, k=3):
means = {}
for c in pool:
rs = [run_episode(_rand, sup, tf, pool, strat, rc, seed=seed + 17 * s,
learn=False, greedy=False, forced_first=c)[0] for s in range(sims)]
means[c] = np.mean(rs)
return sorted(means, key=lambda c: -means[c])[:k]
# ---- 학습/평가 ---------------------------------------------------------------------
def train(adapter, pool, strat, base_reward, state_cfg, episodes, seed):
rng = np.random.default_rng(seed)
for ep in range(1, episodes + 1):
sup = sample_supplier(rng)
rcfg, tf = sample_tenant_pref(rng, base_reward)
rc = RewardCalculator(rcfg, state_cfg)
run_episode(adapter, sup, tf, pool, strat, rc, seed=seed * 100 + ep, learn=True)
def evaluate(adapter, pool, strat, base_reward, state_cfg, n=300, seed0=777, label=""):
from negotiation.qtable.domain.service.feature_builder import build_tenant_features
rng = np.random.default_rng(seed0)
rewards, succ, ratios, hits, holdout_first = [], 0, [], 0, 0
for i in range(n):
sup = sample_supplier(rng)
rcfg, tf = sample_tenant_pref(rng, base_reward)
rc = RewardCalculator(rcfg, state_cfg)
good = rank_cards_mc(sup, tf, pool, strat, rc, seed=seed0 * 7 + i)
r, ok, price, first = run_episode(adapter, sup, tf, pool, strat, rc,
seed=seed0 * 1000 + i, learn=False, greedy=True)
rewards.append(r); succ += ok; ratios.append(price / TARGET)
hits += (first in good); holdout_first += (first in HOLDOUT)
m, ci = float(np.mean(rewards)), float(1.96 * np.std(rewards) / np.sqrt(n))
print(f"{label:<14} mean_rwd={m:.4f} ±{ci:.4f} success={succ/n:.3f} "
f"settled/tgt={np.mean(ratios):.3f} top3_hit={hits/n:.3f} 새카드첫턴={holdout_first/n:.3f}")
def pref_behavior_test(adapters, pool, strat, base_reward, state_cfg):
"""④ 같은 협력사, 성향만 바꿨을 때 카드를 바꾸는가 (greedy).
턴은 '일단 깎기' 공통 정답이라 성향 차이가 드러난다.
협상 중반(가격이 이미 target 근처, 3턴째) 상태를 함께 프로브: 여기서
성사중시는 '마무리(수락 잘 되는) 카드', 가격중시는 '더 깎는 카드' 갈려야 한다.
"""
from negotiation.qtable.domain.service.feature_builder import build_tenant_features
sups = [SupplierProfile(5_000_000, 3, "A"), # 소형·경쟁多
SupplierProfile(200_000_000, 1, "A")] # 대형·단독
probes = [("첫턴", TARGET * 1.15, 1, 0.0),
("중반(3턴,가격↓)", TARGET * 1.02, 3, 0.11)]
for pr_name, price, turn, acc in probes:
print(f"\n ── 프로브: {pr_name} (price={price:.0f}) ──")
print(f" {'협력사':<13} {'성향':<9} " + " ".join(f"{a.name:<15}" for a in adapters))
for sup in sups:
row = {}
for p, pname in [(0.05, "성사중시"), (0.95, "가격중시")]:
rcfg = pref_config(base_reward, p)
tf = build_tenant_features(rcfg)
picks = []
for a in adapters:
snap = make_snapshot(sup, price, turn, acc)
picks.append(a.choose(snap, tf, pool, True))
seg = f"{sup.segment[0]}·{sup.segment[1]}"
print(f" {seg:<13} {pname:<9} " + " ".join(f"{c}(전략{strat[c]})".ljust(15) for c in picks))
def main(episodes=10000, seed=42):
random.seed(seed); np.random.seed(seed); torch.manual_seed(seed)
numbers, feat, strat = load_cards()
train_pool = [c for c in numbers if c not in HOLDOUT]
tcfg = TenantConfigLoader().load("ktcommerce")
card_dim = feat[numbers[0]].shape[0]
print(f"환경: 2축 FeatureBuyer + 성향 랜덤 · 학습 {episodes}ep · 카드특징 {card_dim}차원 "
f"(임베딩384+전략4+톤4) · 학습 {len(train_pool)}장 / 홀드아웃 {HOLDOUT}")
qt = QTableAdapter(numbers, tcfg.state)
dqn = DQNAdapter(FeatureDQNPolicy(state_dim=STATE_FEATURE_DIM + TENANT_FEATURE_DIM,
card_dim=card_dim, eps_decay=4000), feat)
print("\n[학습] qtable_ucb ...")
train(qt, train_pool, strat, tcfg.reward, tcfg.state, episodes, seed)
print("[학습] feature_dqn ...")
train(dqn, train_pool, strat, tcfg.reward, tcfg.state, episodes, seed)
print("\n=== ① 학습 카드 9장 풀 ===")
evaluate(RandomAdapter(seed), train_pool, strat, tcfg.reward, tcfg.state, label="random")
evaluate(qt, train_pool, strat, tcfg.reward, tcfg.state, label="qtable_ucb")
evaluate(dqn, train_pool, strat, tcfg.reward, tcfg.state, label="feature_dqn")
print("\n=== ② zero-shot 11장 풀 (안 본 카드 2장 포함) ===")
evaluate(RandomAdapter(seed), numbers, strat, tcfg.reward, tcfg.state, label="random")
evaluate(qt, numbers, strat, tcfg.reward, tcfg.state, label="qtable_ucb")
evaluate(dqn, numbers, strat, tcfg.reward, tcfg.state, label="feature_dqn")
print("\n=== ④ 성향 극단 테스트 — 같은 협력사, 성향만 바꾸면 카드를 바꾸는가 (11장 풀) ===")
pref_behavior_test([qt, dqn], numbers, strat, tcfg.reward, tcfg.state)
if __name__ == "__main__":
main()

View File

@ -9,9 +9,9 @@
실행: 실행:
cd agent cd agent
APP_ENV=local python -m tools.console_demo --tenant ktcommerce # 기본 시나리오 APP_ENV=local python -m tools.console_demo --tenant imarketkorea # 기본 시나리오
APP_ENV=local python -m tools.console_demo --tenant imarketkorea --no-db # DB 로깅 없이 APP_ENV=local python -m tools.console_demo --tenant imarketkorea --no-db # DB 로깅 없이
APP_ENV=local python -m tools.console_demo --tenant ktcommerce --interactive APP_ENV=local python -m tools.console_demo --tenant imarketkorea --interactive
""" """
import argparse import argparse
@ -74,7 +74,7 @@ def _scenario():
async def run(tenant_id: str, use_db: bool, interactive: bool): async def run(tenant_id: str, use_db: bool, interactive: bool):
loader = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0) loader = TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0)
if not loader.is_registered(tenant_id): if not loader.is_registered(tenant_id):
print(f"[!] 미등록 테넌트: {tenant_id}. 등록된 테넌트: ktcommerce, imarketkorea, _base") print(f"[!] 미등록 테넌트: {tenant_id}. 등록된 테넌트: imarketkorea, _base")
return return
registry = TenantEngineRegistry(loader=loader) registry = TenantEngineRegistry(loader=loader)
engine = await registry.get_engine(tenant_id) engine = await registry.get_engine(tenant_id)
@ -172,7 +172,7 @@ def _interactive_turns():
def main(): def main():
ap = argparse.ArgumentParser(description="협상 의사결정 루프 콘솔 데모 (P0~P4)") ap = argparse.ArgumentParser(description="협상 의사결정 루프 콘솔 데모 (P0~P4)")
ap.add_argument("--tenant", default="ktcommerce", help="테넌트 id (ktcommerce|imarketkorea)") ap.add_argument("--tenant", default="imarketkorea", help="테넌트 id (imarketkorea|_base)")
ap.add_argument("--no-db", action="store_true", help="DB 로깅 비활성화") ap.add_argument("--no-db", action="store_true", help="DB 로깅 비활성화")
ap.add_argument("--interactive", action="store_true", help="턴마다 직접 입력") ap.add_argument("--interactive", action="store_true", help="턴마다 직접 입력")
args = ap.parse_args() args = ap.parse_args()

View File

@ -1,42 +0,0 @@
"""full_autonomy 체크포인트(.pt) → 서빙 번들(autonomy_serving.npz) export.
dqn_serving 동일 패턴: ScoreNet 가중치만 numpy 묶어 PyTorch 없이 서빙한다.
행동 특징은 코드(autonomy_actions) 런타임 생성하므로 번들에는 가중치만 담는다.
실행(호스트, torch 필요): APP_ENV=local python -m tools.export_autonomy_serving
"""
import os
import numpy as np
import torch
from negotiation.policies.autonomy_actions import ACTION_DIM, EXTRA_STATE_DIM
from negotiation.qtable.domain.service.feature_builder import STATE_FEATURE_DIM, TENANT_FEATURE_DIM
_HERE = os.path.dirname(os.path.abspath(__file__))
CKPT_PATH = os.path.join(_HERE, "..", "artifacts", "full_autonomy.pt")
OUT_PATH = os.path.join(_HERE, "..", "artifacts", "autonomy_serving.npz")
STATE_DIM = STATE_FEATURE_DIM + TENANT_FEATURE_DIM + EXTRA_STATE_DIM
def main():
sd = torch.load(CKPT_PATH, map_location="cpu")
W0, b0 = sd["net.0.weight"].numpy(), sd["net.0.bias"].numpy()
W1, b1 = sd["net.2.weight"].numpy(), sd["net.2.bias"].numpy()
W2, b2 = sd["net.4.weight"].numpy(), sd["net.4.bias"].numpy()
assert W0.shape[1] == STATE_DIM + ACTION_DIM, f"입력 차원 불일치: {W0.shape[1]}"
tmp = OUT_PATH + ".tmp"
with open(tmp, "wb") as f:
np.savez(f, W0=W0, b0=b0, W1=W1, b1=b1, W2=W2, b2=b2,
state_dim=STATE_DIM, action_dim=ACTION_DIM)
if os.path.exists(OUT_PATH):
os.replace(OUT_PATH, OUT_PATH + ".prev")
os.replace(tmp, OUT_PATH)
print(f"[저장] {os.path.abspath(OUT_PATH)} (state {STATE_DIM} + action {ACTION_DIM})")
if __name__ == "__main__":
main()

View File

@ -1,79 +0,0 @@
"""feature_dqn 체크포인트(.pt) → 서빙 번들(dqn_serving.npz) export.
서빙 컨테이너에 PyTorch 넣지 않기 위해 ScoreNet(3 MLP) 가중치와 카드 특징
(임베딩384 + 전략 one-hot4 + one-hot4 = 392) numpy 번들 하나로 묶는다.
추론은 negotiation.policy.dqn_store numpy forward 수행한다.
실행(호스트, torch 필요): APP_ENV=local python -m tools.export_dqn_serving
산출: agent/artifacts/dqn_serving.npz (.dockerignore 미제외 이미지에 포함)
"""
import os
import numpy as np
import torch
from tools.train_feature_dqn import load_cards
_HERE = os.path.dirname(os.path.abspath(__file__))
CKPT_PATH = os.path.join(_HERE, "..", "artifacts", "feature_dqn_ktcommerce.pt")
OUT_PATH = os.path.join(_HERE, "..", "artifacts", "dqn_serving.npz")
STATE_DIM = 14 # build_state_features(9) + build_tenant_features(5)
CARD_DIM = 392
def _np_forward(x, W0, b0, W1, b1, W2, b2):
h = np.maximum(x @ W0.T + b0, 0.0)
h = np.maximum(h @ W1.T + b1, 0.0)
return h @ W2.T + b2
def export_bundle(sd, out_path: str) -> str:
"""state_dict → 서빙 번들 npz (원자적 교체: .tmp 작성 후 replace). 반환: 절대경로.
retrain_from_logs 재학습 배포도 함수를 쓴다 검증(torch/numpy 일치) main() 전용.
"""
W0, b0 = sd["net.0.weight"].numpy(), sd["net.0.bias"].numpy()
W1, b1 = sd["net.2.weight"].numpy(), sd["net.2.bias"].numpy()
W2, b2 = sd["net.4.weight"].numpy(), sd["net.4.bias"].numpy()
assert W0.shape[1] == STATE_DIM + CARD_DIM, f"입력 차원 불일치: {W0.shape[1]}"
numbers, feat, _ = load_cards()
card_feats = np.stack([feat[n] for n in numbers]).astype(np.float32)
tmp = out_path + ".tmp"
with open(tmp, "wb") as f:
np.savez(
f,
W0=W0, b0=b0, W1=W1, b1=b1, W2=W2, b2=b2,
card_numbers=np.array(numbers), card_feats=card_feats,
state_dim=STATE_DIM, card_dim=CARD_DIM,
)
if os.path.exists(out_path):
os.replace(out_path, out_path + ".prev") # 직전 번들 백업(롤백용)
os.replace(tmp, out_path)
return os.path.abspath(out_path)
def main():
sd = torch.load(CKPT_PATH, map_location="cpu")
# 정합성 검증: torch forward == numpy forward
from negotiation.policies.feature_dqn_policy import ScoreNet
net = ScoreNet(STATE_DIM, CARD_DIM)
net.load_state_dict(sd)
net.eval()
x = np.random.default_rng(0).normal(size=(8, STATE_DIM + CARD_DIM)).astype(np.float32)
with torch.no_grad():
ref = net(torch.tensor(x)).numpy()
W0, b0 = sd["net.0.weight"].numpy(), sd["net.0.bias"].numpy()
W1, b1 = sd["net.2.weight"].numpy(), sd["net.2.bias"].numpy()
W2, b2 = sd["net.4.weight"].numpy(), sd["net.4.bias"].numpy()
out = _np_forward(x, W0, b0, W1, b1, W2, b2).squeeze(-1)
diff = float(np.abs(ref - out).max())
assert diff < 1e-4, f"numpy/torch forward 불일치: {diff}"
path = export_bundle(sd, OUT_PATH)
print(f"[저장] {path} forward 오차 {diff:.2e}")
if __name__ == "__main__":
main()

View File

@ -1,61 +0,0 @@
"""probe_serving_dqn — 서빙 번들(dqn_serving.npz)의 상황별 카드 선택 프로브.
배포된 모델이 '상황에 맞게' 고르는지 눈으로 확인하는 진단 도구:
협력사 세그먼트 × 고객사 성향 × 협상 국면(가격대) 선택 카드를 표로 출력한다.
전부 다르길 기대하는 아니라, 축을 바꿨을 선택이 '움직이는지' 본다.
실행: APP_ENV=local python -m tools.probe_serving_dqn (numpy 필요, DB 불필요)
"""
import numpy as np
from negotiation.qtable.domain.model.snapshot import NegotiationSnapshot
from negotiation.qtable.domain.service.feature_builder import build_state_features, build_tenant_features
from tenancy.config_loader import TenantConfigLoader
from tools.export_dqn_serving import OUT_PATH
from tools.retrain_from_logs import np_scorer_from_bundle
from tools.train_feature_dqn import pref_config
ANCHOR, TARGET = 495_000.0, 500_000.0 # BUGCHECK 견적과 동일 스케일
SUPPLIERS = {
"소형·경쟁多": dict(revenue_amount=5_000_000, partner_count=3, distribution_code="A"),
"소형·단독": dict(revenue_amount=5_000_000, partner_count=1, distribution_code="A"),
"대형·경쟁多": dict(revenue_amount=200_000_000, partner_count=3, distribution_code="A"),
"대형·단독": dict(revenue_amount=200_000_000, partner_count=1, distribution_code="A"),
}
PHASES = { # (라운드, 제시가): 첫턴 높은 가격 / 중반 목표가 근접 / 막판 앵커존 직전
"첫턴(575k)": (1, 575_000.0),
"중반(510k)": (2, 510_000.0),
"막판(501k)": (3, 501_000.0),
}
PREFS = {"성사중시": 0.1, "가격중시": 0.9}
def main():
score = np_scorer_from_bundle(OUT_PATH)
z = np.load(OUT_PATH, allow_pickle=False)
numbers = [str(n) for n in z["card_numbers"]]
feats = z["card_feats"]
base = TenantConfigLoader().load("ktcommerce").reward
for phase, (turn, price) in PHASES.items():
print(f"\n=== {phase} (앵커 {int(ANCHOR):,} / 목표 {int(TARGET):,}) ===")
print(f"{'협력사':<12}" + "".join(f"{p:>16}" for p in PREFS))
for sup_name, sup in SUPPLIERS.items():
row = []
for _, p in PREFS.items():
tf = build_tenant_features(pref_config(base, p))
snap = NegotiationSnapshot(
revenue_amount=sup["revenue_amount"], distribution_code=sup["distribution_code"],
partner_count=sup["partner_count"],
acceptance_ratio=max(0.0, (575_000.0 - price) / 575_000.0),
input_price=price, anchor_price=ANCHOR, target_price=TARGET, round_number=turn,
)
sf = np.concatenate([build_state_features(snap), tf])
row.append(numbers[int(np.argmax(score(sf, feats)))])
print(f"{sup_name:<12}" + "".join(f"{c:>16}" for c in row))
if __name__ == "__main__":
main()

View File

@ -1,238 +0,0 @@
"""retrain_from_logs — experience_logs 실데이터로 feature_dqn 오프라인 재학습 + OPE 게이트.
파이프라인:
learning.experience_logs 로드( 테넌트 범용 에이전트는 테넌트를 특징으로 조건화하므로 통합 학습)
세션별 에피소드 재구성: 카드턴(done=False) N개 + 종료행(done=True) 1.
보상은 학습 규약(최종 결과 시점만 채점) 맞춰 종료행 reward 쓰고 중간턴은 0.
현재 체크포인트에서 fine-tune (낮은 lr 시뮬 사전학습 망각 방지)
OPE(SNIPS, 궤적 IS): 후보 모델 vs 현재 서빙 번들. 후보가 넘으면 배포하지 않는다.
통과 dqn_serving.npz 원자적 교체(직전본 .prev 백업) `docker compose build agent && up -d agent` 배포.
실행(호스트, torch+DB 필요):
APP_ENV=local python -m tools.retrain_from_logs
환경변수:
MIN_EPISODES(기본 200) 재학습 최소 에피소드 미달 skip (과적합 방지)
EPOCHS(기본 20) / LR(기본 1e-4) / FORCE_DEPLOY=1 (OPE 게이트 무시 테스트 전용)
주의: 서빙이 greedy(탐색 없음) 로그가 선택 편향됨 OPE 유효표본(ESS) 작으면
게이트가 보수적으로 배포를 막는다. 이는 의도된 동작이다(조용한 성능저하 방지).
"""
import asyncio
import json
import os
from collections import defaultdict
import numpy as np
import torch
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import ExperienceLog
from common.enums import DBType, DBWRType
from negotiation.policies.feature_dqn_policy import FeatureDQNPolicy
from negotiation.qtable.domain.model.snapshot import NegotiationSnapshot
from negotiation.qtable.domain.service.feature_builder import (
STATE_FEATURE_DIM, TENANT_FEATURE_DIM, build_state_features, build_tenant_features)
from sqlalchemy import select
from tenancy.config_loader import TenantConfigLoader
from tools.export_dqn_serving import CKPT_PATH, OUT_PATH, export_bundle
from tools.train_feature_dqn import load_cards
_HERE = os.path.dirname(os.path.abspath(__file__))
RETRAIN_CKPT = os.path.join(_HERE, "..", "artifacts", "feature_dqn_retrained.pt")
REPORT_PATH = os.path.join(_HERE, "..", "artifacts", "retrain_report.json")
MIN_EPISODES = int(os.getenv("MIN_EPISODES", "200"))
EPOCHS = int(os.getenv("EPOCHS", "20"))
LR = float(os.getenv("LR", "1e-4"))
FORCE_DEPLOY = os.getenv("FORCE_DEPLOY") == "1"
PROPENSITY_FALLBACK = 0.9 # 구로그 propensity 누락 시 (UCB/DQN 모두 greedy≈(1-ε)+ε/n)
# ---- ① 로그 로드 -------------------------------------------------------------
async def fetch_logs():
def _q(s):
q = (select(ExperienceLog.company_id, ExperienceLog.session_id, ExperienceLog.card_id,
ExperienceLog.reward, ExperienceLog.done, ExperienceLog.snapshot,
ExperienceLog.propensity, ExperienceLog.turn, ExperienceLog.id)
.where(ExperienceLog.is_invalidated == False) # noqa: E712
.order_by(ExperienceLog.company_id, ExperienceLog.session_id, ExperienceLog.id))
return DB_SESSION_MNG.execute(s, q)
err, rows = await DB_SESSION_MNG.execute_lambda(DBType.MAIN.value, DBWRType.DB_READ.value, _q)
return rows
# ---- ② 에피소드 재구성 --------------------------------------------------------
def build_episodes(rows, known_cards: set):
"""→ [{tenant, steps:[(snapshot, card, propensity)], terminal_reward}], 스킵 사유 카운트."""
by_session = defaultdict(list)
for r in rows:
if r[1] is not None:
by_session[(r[0], str(r[1]))].append(r)
episodes, skipped = [], defaultdict(int)
for (company_id, _sid), items in by_session.items():
selects = [r for r in items if not r[4] and r[5]] # done=False, snapshot 有
terminals = [r for r in items if r[4] and r[3] is not None] # done=True, reward 有
if not selects or not terminals:
skipped["종료행/카드턴 없음(미완결 세션)"] += 1
continue
if any(str(r[2] or "").startswith("AUT|") for r in selects):
skipped["완전 자율 세션(카드 재학습 대상 아님)"] += 1
continue
if any(r[2] not in known_cards for r in selects):
skipped["임베딩 없는 카드(파일매핑 테넌트 등)"] += 1
continue
episodes.append(dict(
tenant=company_id,
steps=[(r[5], r[2], r[6] if r[6] else PROPENSITY_FALLBACK) for r in selects],
terminal_reward=float(terminals[-1][3]),
))
return episodes, skipped
def tenant_feat_for(cache: dict, loader: TenantConfigLoader, company_id: str) -> np.ndarray:
"""테넌트 보상설정 → 성향 특징. 미온보딩/로드 실패는 _base 폴백."""
if company_id not in cache:
try:
cfg = loader.load(company_id)
except Exception:
cfg = loader.load("_base")
cache[company_id] = build_tenant_features(cfg.reward)
return cache[company_id]
def to_transitions(episodes, feat, tenant_feats):
"""학습 규약(train_feature_dqn 과 동일): 중간턴 r=0, 종료턴만 terminal_reward. 다음 후보 = 전체 사용분."""
all_cards = list(feat.keys())
out = []
for ep in episodes:
tf = tenant_feats[ep["tenant"]]
used = set()
n = len(ep["steps"])
for i, (snap_d, card, _p) in enumerate(ep["steps"]):
sf = np.concatenate([build_state_features(NegotiationSnapshot.from_dict(snap_d)), tf])
used.add(card)
if i == n - 1:
out.append((sf, feat[card], ep["terminal_reward"], None, None, True))
else:
s2_d = ep["steps"][i + 1][0]
s2 = np.concatenate([build_state_features(NegotiationSnapshot.from_dict(s2_d)), tf])
cands = [c for c in all_cards if c not in used] or all_cards
out.append((sf, feat[card], 0.0, s2, np.stack([feat[c] for c in cands]), False))
return out
# ---- ④ OPE (SNIPS, 궤적 단위 IS) ----------------------------------------------
def _greedy_match(score_fn, ep, feat, tf) -> float:
"""궤적 IS 가중치: Π 1[greedy(sᵢ)=aᵢ]/pᵢ. 한 턴이라도 불일치면 0."""
all_cards = list(feat.keys())
w, used = 1.0, set()
for snap_d, card, p in ep["steps"]:
sf = np.concatenate([build_state_features(NegotiationSnapshot.from_dict(snap_d)), tf])
cands = [c for c in all_cards if c not in used] or all_cards
sc = score_fn(sf, np.stack([feat[c] for c in cands]))
if cands[int(np.argmax(sc))] != card:
return 0.0
w /= max(p, 1e-3)
used.add(card)
return w
def snips(score_fn, episodes, feat, tenant_feats):
"""SNIPS 추정치 + 유효표본크기(ESS). 매치 0건이면 (None, 0)."""
ws, rs = [], []
for ep in episodes:
w = _greedy_match(score_fn, ep, feat, tenant_feats[ep["tenant"]])
ws.append(w)
rs.append(ep["terminal_reward"])
ws, rs = np.array(ws), np.array(rs)
if ws.sum() <= 0:
return None, 0.0
est = float((ws * rs).sum() / ws.sum())
ess = float(ws.sum() ** 2 / (ws ** 2).sum())
return est, ess
def np_scorer_from_bundle(path):
"""현재 서빙 번들(npz) → score_fn (dqn_store 와 동일 forward)."""
z = np.load(path, allow_pickle=False)
W0, b0, W1, b1, W2, b2 = z["W0"], z["b0"], z["W1"], z["b1"], z["W2"], z["b2"]
def score(sf, card_feats):
x = np.concatenate([np.repeat(sf[None, :], card_feats.shape[0], axis=0), card_feats], axis=1)
h = np.maximum(x @ W0.T + b0, 0.0)
h = np.maximum(h @ W1.T + b1, 0.0)
return (h @ W2.T + b2).squeeze(-1)
return score
# ---- 메인 ---------------------------------------------------------------------
async def run():
numbers, feat, _ = load_cards()
rows = await fetch_logs()
episodes, skipped = build_episodes(rows, set(numbers))
print(f"로그 {len(rows)}행 → 에피소드 {len(episodes)}개 (스킵: {dict(skipped) or '없음'})")
report = dict(rows=len(rows), episodes=len(episodes), skipped=dict(skipped),
min_episodes=MIN_EPISODES, deployed=False)
if len(episodes) < MIN_EPISODES and not FORCE_DEPLOY:
print(f"[skip] 에피소드 {len(episodes)} < MIN_EPISODES {MIN_EPISODES} — 과적합 위험으로 재학습 안 함")
report["result"] = "skipped_insufficient_data"
return report
loader = TenantConfigLoader()
tenant_feats = {}
for ep in episodes:
tenant_feat_for(tenant_feats, loader, ep["tenant"])
# ③ fine-tune (시뮬 사전학습 체크포인트에서 이어서, 낮은 lr)
transitions = to_transitions(episodes, feat, tenant_feats)
batch = min(64, max(8, len(transitions) // 4))
policy = FeatureDQNPolicy(state_dim=STATE_FEATURE_DIM + TENANT_FEATURE_DIM,
card_dim=feat[numbers[0]].shape[0], lr=LR, batch_size=batch)
if os.path.exists(CKPT_PATH):
policy.load(CKPT_PATH)
print(f"[fine-tune] 시작점: {os.path.basename(CKPT_PATH)} lr={LR} batch={batch}")
policy.buf.extend(transitions)
steps = EPOCHS * max(1, len(transitions) // batch)
losses = [l for _ in range(steps) if (l := policy.train_step()) is not None]
print(f"[fine-tune] {steps} step loss {losses[0]:.4f}{losses[-1]:.4f}" if losses else "[fine-tune] 스텝 없음")
# ④ OPE 게이트: 후보 vs 현재 서빙
def cand_score(sf, cf):
return policy.scores(sf, cf)
cand_est, cand_ess = snips(cand_score, episodes, feat, tenant_feats)
cur_est, cur_ess = (snips(np_scorer_from_bundle(OUT_PATH), episodes, feat, tenant_feats)
if os.path.exists(OUT_PATH) else (None, 0.0))
print(f"[OPE/SNIPS] 후보 {cand_est} (ESS {cand_ess:.1f}) vs 현재 {cur_est} (ESS {cur_ess:.1f})")
report.update(ope_candidate=cand_est, ope_candidate_ess=cand_ess,
ope_current=cur_est, ope_current_ess=cur_ess)
min_ess = max(3.0, 0.02 * len(episodes))
passed = (cand_est is not None and cand_ess >= min_ess
and (cur_est is None or cand_est >= cur_est - 0.01))
if not passed and not FORCE_DEPLOY:
print(f"[게이트 불통과] 배포하지 않음 (필요 ESS ≥ {min_ess:.1f}). 현재 번들 유지.")
report["result"] = "gate_failed"
return report
# ⑤ 배포: 후보 저장 + 번들 교체 (.prev 백업)
policy.save(RETRAIN_CKPT)
path = export_bundle(policy.q.state_dict(), OUT_PATH)
print(f"[배포] {path} (직전본 → dqn_serving.npz.prev)")
print(" 적용: docker compose build agent && docker compose up -d agent")
report.update(result="deployed" if passed else "force_deployed", deployed=True,
ckpt=os.path.abspath(RETRAIN_CKPT))
return report
def main():
report = asyncio.run(run())
with open(REPORT_PATH, "w", encoding="utf-8") as f:
json.dump(report, f, ensure_ascii=False, indent=2)
print(f"[리포트] {os.path.abspath(REPORT_PATH)}")
if __name__ == "__main__":
main()

View File

@ -1,317 +0,0 @@
"""결함 회귀 게이트 — 실전에서 발견된 협상 결함을 시나리오로 재생해 서빙 번들을 검증한다.
프로브(probe_serving_dqn) '눈으로 보는 행동 표'라면 이것은 '자동 합격/불합격'이다.
모든 검사 항목은 과거 실제 발생했던 결함이며, 하나라도 실패하면 exit 1 배포 금지.
재학습 번들은 반드시 게이트를 통과한 뒤에만 autonomy_serving.npz 교체한다.
검사 대상은 서빙 실물이다: AutonomyPolicy.decide(봉투 마스크 포함) + ChatEngine._autonomy_next
(최종제안 전환) + ment_generator 가드(목표가 누설·할루시네이션). 시뮬 협력사는 스크립트라
결정론적이고, 정책도 greedy 실행마다 같은 결과가 나온다. DB/LLM/도커 불필요.
지형은 복수로 검사한다 v3.4 실스케일(423,198)에선 통과하고 드라이브 지형(10,000)에서
'첫 턴 목표가 통보' 퇴화했던 사고: 지형 통과는 다른 지형을 보증하지 않는다.
실행: agent 디렉터리에서 APP_ENV=local python -m tools.test_autonomy_defects [번들경로]
"""
import os
import sys
from types import SimpleNamespace
import numpy as np
from negotiation.chat.service import ment_generator
from negotiation.chat.service.chat_engine import ChatEngine
from negotiation.policy import autonomy_store
from negotiation.policy.autonomy_store import AutonomyPolicy
from tenancy.config_loader import TenantConfigLoader
# 검사 지형: 실제 견적(앵커율 ~1%) + 로컬 드라이브 견적(소액) — 스케일이 달라도 예절은 같아야 한다.
GEOS = {
"실스케일": dict(anchor=418_966, target=423_198, first=540_000, il=459_000),
"소액": dict(anchor=9_900, target=10_000, first=11_500, il=0),
}
MIN_PRESS = int(os.getenv("AUTONOMY_MIN_PRESS", "2"))
_RESULTS = []
def check(name: str, ok: bool, detail: str = ""):
_RESULTS.append((name, ok, detail))
print(f" {'' if ok else '✘ FAIL'} {name}" + (f"{detail}" if detail and not ok else ""))
# ---- 하니스: 서빙 실물 구동 (chat_service 의 ctx 관리 순서를 그대로 재현) ----------------
def base_ctx(geo) -> dict:
return dict(revenue_amount=50_000_000, distribution_code="A", partner_count=3,
item_price=geo["first"], input_price=geo["first"], round=1,
anchor_price=geo["anchor"], target_price=geo["target"],
internet_lowest_price=geo["il"])
def run_scenario(policy, supplier, geo, max_steps=30):
"""정책 결정 → 스텝 전환 → 컨텍스트 부기(chat_service 순서) → 스크립트 협력사 반응 루프.
trace 원소: (step, kind, q, 당시 제시가, autonomy_offer, 결정 시점 press_n)
"""
eng = ChatEngine.__new__(ChatEngine) # _autonomy_next 는 decider 와 ctx 만 쓴다
ctx = base_ctx(geo)
def decide(c):
act = policy.decide(c)
c["autonomy_pending"] = {"kind": act.kind, "q": act.counter_q, "s": act.strategy}
return act
eng.autonomy_decider = decide
sess = SimpleNamespace(context=ctx)
trace, end = [], None
for _ in range(max_steps):
press_n_at = int(ctx.get("autonomy_press_n") or 0)
step = eng._autonomy_next(sess)
pending = ctx.pop("autonomy_pending", None)
if pending: # chat_service 부기: pending → last(+prev), 역제안 별도 보존, press 카운터
if ctx.get("autonomy_last"):
ctx["autonomy_prev"] = ctx["autonomy_last"]
ctx["autonomy_last"] = dict(pending)
if pending["kind"] == "counter":
ctx["autonomy_last_counter"] = dict(pending)
if pending["kind"] == "press":
ctx["autonomy_press_n"] = press_n_at + 1
trace.append((step, (pending or {}).get("kind"), (pending or {}).get("q"),
ctx["input_price"], ctx.get("autonomy_offer"), press_n_at))
if step in ("협상완료", "협상실패"):
end = step
break
if step == "자율_최종제안": # 예→그 금액 타결 / 아니오→협상실패 (엔진 스텝 정의)
end = "협상완료" if supplier.final_yes(ctx) else "협상실패"
break
if step == "자율_역제안" and supplier.counter_yes(ctx):
ctx["input_price"] = ctx["autonomy_offer"]
end = "협상완료"
break
ctx["input_price"] = int(supplier.next_price(ctx))
ctx["round"] = ctx.get("round", 1) + 1
return trace, end, ctx
def fmt(trace):
out = []
for step, kind, q, price, offer, _ in trace:
s = f"{price:,}{step}"
if kind == "counter":
s += f"({offer:,})"
out.append(s)
return " ".join(out)
# ---- 스크립트 협력사 (결정론, 지형 비율로 정의) -----------------------------------------
class Stubborn:
"""조금씩 내리지만 하한이 목표가 위(×1.028) — 성사 불가능. 역제안·최종 전부 거절.
기대 궤적: 설득 2 앵커 이하 개시 단조 상향 사다리 최종제안(목표가) 결렬."""
def __init__(self, geo):
self.floor = int(geo["target"] * 1.028)
def next_price(self, ctx):
return max(self.floor, int(ctx["input_price"] * 0.96))
def counter_yes(self, ctx):
return False
def final_yes(self, ctx):
return False
class HoverNearTarget:
"""목표가 +0.19% 고정 — 마무리 국면. 압박이 나오면 안 되는 구간."""
def __init__(self, geo):
self.price = int(geo["target"] * 1.0019)
def next_price(self, ctx):
return self.price
def counter_yes(self, ctx):
return False
def final_yes(self, ctx):
return False
class Dealable:
"""4% 씩 내려와 목표가 바로 아래까지 협조 — 성사 가능 케이스."""
def __init__(self, geo):
self.floor = int(geo["target"] * 0.9995)
self.accept_from = geo["anchor"] + 0.4 * (geo["target"] - geo["anchor"])
def next_price(self, ctx):
return max(self.floor, int(ctx["input_price"] * 0.96))
def counter_yes(self, ctx):
return ctx["autonomy_offer"] >= self.accept_from # 목표가 부근 제안은 수락
def final_yes(self, ctx):
return True
# ---- 시나리오 검사 (각 항목 = 과거 실제 결함) -------------------------------------------
def assert_defects(tag, trace, end, geo):
anchor, target = geo["anchor"], geo["target"]
near = target * 1.005
# '역제안' 검사는 일반 역제안 스텝만 센다 — 같은 금액 재시도가 자율_최종제안으로 전환된 것은
# 반복이 아니라 설계된 최종 통보(제품 결정: 같은 금액 재호출 = 탄약 소진 → 마지막으로 묻고 종료).
counters = [(i, t) for i, t in enumerate(trace) if t[0] == "자율_역제안"]
presses = [t for t in trace if t[1] == "press"]
if trace and trace[0][3] > near:
check(f"[{tag}] 개시 턴은 설득 (결함: v3.4 첫턴 walk→목표가 통보)",
trace[0][1] == "press", f"첫 결정이 {trace[0][1]}")
if counters:
i0, first = counters[0]
check(f"[{tag}] 첫 역제안은 앵커 이하 (결함: 사다리 꼭대기 개시)",
first[4] <= anchor, f"첫 역제안 {first[4]:,} > 앵커 {anchor:,}")
pressed_before = sum(1 for t in trace[:i0] if t[1] == "press")
if first[3] > near: # 마무리 국면은 해금 예외
check(f"[{tag}] 역제시 해금 전 설득 ≥{MIN_PRESS}회 (결함: 첫턴 역제시)",
pressed_before >= MIN_PRESS, f"설득 {pressed_before}회 만에 역제안")
offers = [t[4] for _, t in counters]
check(f"[{tag}] 역제안 단조 상향 (결함: 제안 철회 423,198→420,024)",
all(b >= a for a, b in zip(offers, offers[1:])), f"철회 발생: {offers}")
check(f"[{tag}] 역제안 ≤ 목표가", all(o <= target for o in offers), f"{offers}")
check(f"[{tag}] 같은 금액 역제안 반복 없음 (결함: 421,082 반복)",
all(b != a for a, b in zip(offers, offers[1:])), f"{offers}")
check(f"[{tag}] 마무리 국면(≤목표가×1.005) 압박 없음 (결함: 802원 푼돈 흥정)",
all(t[3] > near for t in presses), "목표가 코앞에서 압박")
check(f"[{tag}] 목표가 초과 제시가 수락 없음 (결함: 목표가+14% 매입)",
not any(t[1] == "accept" and t[3] > target for t in trace), "")
finals = [t for t in trace if t[0] == "자율_최종제안"]
for f in finals:
check(f"[{tag}] 최종제안 금액 = 목표가 (결함: 직전 금액 재사용 60,548)",
f[4] == target, f"최종제안 {f[4]:,} ≠ 목표가 {target:,}")
# 결렬 의사(walk)로 끝났다면 반드시 최종제안을 거쳤어야 한다 (턴캡 종료는 예외)
walked_direct = any(t[1] == "walk" and t[0] == "협상실패" for t in trace)
capped = trace and trace[-1][0] == "협상실패" and trace[-1][1] is None
check(f"[{tag}] 결렬 전 최종제안 1회 보장 (결함: 최종 의사 확인 없이 종료)",
not walked_direct or capped or bool(finals), "walk 즉시 결렬")
check(f"[{tag}] 종료 보장 (무한 세션 없음)", end is not None, "max_steps 내 미종료")
# ---- 엔진 단위 검사 (정책 무관 — 전환 로직 자체) ----------------------------------------
def engine_unit_tests():
print("\n[엔진 전환 로직 단위 검사]")
geo = GEOS["실스케일"]
target = geo["target"]
def force(kind, q=0.0, s=3):
eng = ChatEngine.__new__(ChatEngine)
eng.autonomy_decider = lambda c: SimpleNamespace(kind=kind, counter_q=q, strategy=s)
return eng
# walk → 최종제안(목표가) → 재차 walk → 협상실패
ctx = base_ctx(geo)
eng = force("walk")
sess = SimpleNamespace(context=ctx)
step1 = eng._autonomy_next(sess)
check("walk 1회차 → 자율_최종제안 전환", step1 == "자율_최종제안", f"got {step1}")
check("walk 전환 최종제안 금액 = 목표가", ctx.get("autonomy_offer") == target,
f"{ctx.get('autonomy_offer')}")
step2 = eng._autonomy_next(sess)
check("walk 2회차(최종 거절 후) → 협상실패", step2 == "협상실패", f"got {step2}")
# 같은 q 역제안 반복 → 최종제안(목표가) 전환
ctx = base_ctx(geo)
ctx["autonomy_last"] = ctx["autonomy_last_counter"] = {"kind": "counter", "q": 0.5, "s": 3}
sess = SimpleNamespace(context=ctx)
step = force("counter", q=0.5)._autonomy_next(sess)
check("같은 금액 재역제안 → 자율_최종제안 전환", step == "자율_최종제안", f"got {step}")
check("탄약소진 최종제안 금액 = 목표가", ctx.get("autonomy_offer") == target,
f"{ctx.get('autonomy_offer')}")
# 턴 상한 — 캡 종료도 최종제안 보장을 우회하지 않는다
ctx = base_ctx(geo)
ctx["round"] = 13
sess = SimpleNamespace(context=ctx)
step = force("press")._autonomy_next(sess)
check("턴 상한 초과(최종 미실시) → 자율_최종제안", step == "자율_최종제안", f"got {step}")
check("턴캡 최종제안 금액 = 목표가", ctx.get("autonomy_offer") == target,
f"{ctx.get('autonomy_offer')}")
step = force("press")._autonomy_next(sess)
check("턴 상한 초과(최종 거절 후) → 협상실패", step == "협상실패", f"got {step}")
# ---- 봉투 마스크 단위 검사 (모델 무관 — 후보 필터 자체) ----------------------------------
def envelope_unit_tests(policy):
print("\n[봉투 마스크 단위 검사]")
geo = GEOS["소액"]
ctx = base_ctx(geo) # 설득 0회, 제시가 목표가 위 → 설득만 가능해야 한다
act = policy.decide(ctx)
check("설득 0회 상태의 결정은 press 만 가능 (walk·counter·accept 잠금)",
act.kind == "press", f"got {act.kind}")
# ---- 멘트 가드 검사 (목표가 누설·할루시네이션 — LLM 호출 없음) ---------------------------
def ment_guard_tests():
print("\n[멘트 가드 검사]")
geo = GEOS["실스케일"]
target, anchor, il = geo["target"], geo["anchor"], geo["il"]
ctx = base_ctx(geo)
prompt = ment_generator._prompt_for("자율_압박_3", ctx)
check("압박 프롬프트에 목표가 숫자 없음 (결함: 목표가 노출 멘트)",
str(target) not in prompt.replace(",", ""), "프롬프트가 목표가를 담고 있음")
leak = f"저희 내부 산정 기준은 {target:,}원입니다. 이 가격에 맞춰 재검토 부탁드립니다."
check("목표가 포함 압박 멘트 → 폐기", not ment_generator._guard("자율_압박_3", ctx, leak), "")
invented = "시장 상황을 고려해 400,000원 수준으로 재검토 부탁드립니다."
check("지어낸 금액 멘트 → 폐기 (할루시네이션)",
not ment_generator._guard("자율_압박_3", ctx, invented), "")
ctx2 = dict(ctx, autonomy_offer=anchor)
ok_ment = f"내부 검토 결과 {anchor:,}원이면 즉시 진행이 가능합니다. 수락해 주시겠습니까?"
check("정상 역제안 멘트(제안가 포함) → 통과",
ment_generator._guard("자율_역제안", ctx2, ok_ment), "")
no_offer = "말씀하신 조건을 검토했고 조정이 필요합니다. 수락해 주시겠습니까?"
check("제안가 없는 역제안 멘트 → 폐기",
not ment_generator._guard("자율_역제안", ctx2, no_offer), "")
ev = f"동일 품목 인터넷 최저가가 {il:,}원으로 확인됩니다. 재검토 부탁드립니다."
check("최저가 인용: 근거 있음(수집됨+제시가>최저가) → 허용",
ment_generator._guard("자율_압박_1", ctx, ev), "")
ctx3 = dict(ctx, internet_lowest_price=0)
ev0 = "동일 품목 인터넷 최저가 대비 높은 수준입니다. 재검토 부탁드립니다."
check("최저가 인용: 미수집 품목 → 폐기 (지어낸 시장 주장)",
not ment_generator._guard("자율_압박_1", ctx3, ev0), "")
def main():
bundle = sys.argv[1] if len(sys.argv) > 1 else autonomy_store.BUNDLE_PATH
z = np.load(bundle, allow_pickle=False)
policy = AutonomyPolicy(z, TenantConfigLoader().load("ktcommerce").reward)
print(f"번들: {os.path.abspath(bundle)} (state_dim={int(z['state_dim'])})")
for geo_name, geo in GEOS.items():
print(f"\n{'' * 60}\n지형 [{geo_name}] 앵커 {geo['anchor']:,} / 목표 {geo['target']:,} "
f"/ 첫 제시가 {geo['first']:,}")
for tag, sup_cls in (("완고", Stubborn), ("목표가위 고정", HoverNearTarget),
("협조", Dealable)):
trace, end, _ = run_scenario(policy, sup_cls(geo), geo)
full_tag = f"{geo_name}·{tag}"
print(f"\n[{full_tag}] {fmt(trace)}{end}")
assert_defects(full_tag, trace, end, geo)
engine_unit_tests()
envelope_unit_tests(policy)
ment_guard_tests()
fails = [(n, d) for n, ok, d in _RESULTS if not ok]
print(f"\n{'=' * 60}\n결과: {len(_RESULTS) - len(fails)}/{len(_RESULTS)} 통과")
if fails:
print("실패 항목 — 이 번들은 배포 금지:")
for n, d in fails:
print(f"{n} {d}")
sys.exit(1)
print("전 항목 통과 — 배포 가능.")
if __name__ == "__main__":
main()

View File

@ -1,148 +0,0 @@
"""action-as-feature DQN 학습 (Phase 2·3) — 공용 환경 헬퍼 + 단독 학습 엔트리.
카드 특징 = 스크립트 임베딩(384) + 전략 one-hot(4) + one-hot(4) = 392차원
상태 특징 = 연속 상태(9) + 고객사 성향(5) = 14차원 협력사·고객사 조건화
학습 환경 = FeatureBuyer(양보력/수락력 2) + 에피소드마다 협력사·고객사성향 랜덤 샘플링
비교 평가는 tools.compare_qtable_vs_dqn 에서 수행한다.
실행: APP_ENV=local python -m tools.train_feature_dqn
"""
import os
import random
import numpy as np
import torch
from eval_harness.buyer import Scenario
from eval_harness.feature_buyer import FeatureBuyer, sample_supplier
from negotiation.policies.feature_dqn_policy import FeatureDQNPolicy
from negotiation.qtable.domain.model.snapshot import NegotiationOutcome, NegotiationSnapshot
from negotiation.qtable.domain.service.feature_builder import (
STATE_FEATURE_DIM, TENANT_FEATURE_DIM, build_state_features, build_tenant_features)
from negotiation.qtable.domain.service.reward_calculator import RewardCalculator
from tenancy.config_loader import TenantConfigLoader
_HERE = os.path.dirname(os.path.abspath(__file__))
EMB_PATH = os.path.join(_HERE, "..", "artifacts", "card_embeddings.npz")
CKPT_PATH = os.path.join(_HERE, "..", "artifacts", "feature_dqn_ktcommerce.pt")
# zero-shot 실험용 홀드아웃 (전략 1·4 — 남은 풀에도 같은 전략 존재).
# 서빙용 최종 학습은 전체 풀 사용: FULL_POOL=1 python -m tools.train_feature_dqn
HOLDOUT = [] if os.getenv("FULL_POOL") == "1" else ["NGC-002", "NGC-010"]
ANCHOR, TARGET = 8000.0, 10000.0
MAX_TURNS = 5
N_STRATEGY, N_TONE = 4, 4
# ---- 카드 특징: 임베딩 + 전략/톤 one-hot ------------------------------------------
def load_cards():
z = np.load(EMB_PATH, allow_pickle=True)
numbers = [str(n) for n in z["numbers"]]
feat, strat = {}, {}
for i, n in enumerate(numbers):
s, t = int(z["strategy"][i]), int(z["tone"][i])
s_oh = np.eye(N_STRATEGY, dtype=np.float32)[s - 1]
t_oh = np.eye(N_TONE, dtype=np.float32)[t - 1]
feat[n] = np.concatenate([z["embeddings"][i].astype(np.float32), s_oh, t_oh])
strat[n] = s
return numbers, feat, strat
# ---- 고객사 성향: 보상 설정 샘플링 ---------------------------------------------------
def sample_tenant_pref(rng: np.random.Generator, base_cfg):
"""p ∈ [0,1]: 0=성사중시(협력 유리) ↔ 1=가격중시(경쟁 유리). 반환: (RewardConfig, tenant_feat)."""
p = float(rng.uniform(0.0, 1.0))
cfg = base_cfg.model_copy(update=dict(
max_weight=0.25 + 0.60 * p, # 가격보상 비중
min_weight=(0.25 + 0.60 * p) * 0.7,
success_reward=1.6 - 1.2 * p, # 성사중시일수록 성공보상↑
failure_penalty=-(1.4 - 1.1 * p), # 성사중시일수록 결렬이 아픔
beta=0.1 + 0.4 * p,
penalty_lambda=float(rng.uniform(0.005, 0.05)),
))
return cfg, build_tenant_features(cfg)
def pref_config(base_cfg, p: float, lam: float = 0.02):
"""평가용: 성향 p 를 고정해 RewardConfig 생성 (극단 테스트)."""
return base_cfg.model_copy(update=dict(
max_weight=0.25 + 0.60 * p, min_weight=(0.25 + 0.60 * p) * 0.7,
success_reward=1.6 - 1.2 * p, failure_penalty=-(1.4 - 1.1 * p),
beta=0.1 + 0.4 * p, penalty_lambda=lam,
))
def make_snapshot(sup, price: float, turn: int, acceptance: float,
outcome=NegotiationOutcome.ONGOING) -> NegotiationSnapshot:
return NegotiationSnapshot(
revenue_amount=sup.revenue_amount, distribution_code=sup.distribution_code,
partner_count=sup.partner_count, acceptance_ratio=acceptance,
input_price=price, anchor_price=ANCHOR, target_price=TARGET,
round_number=turn, outcome=outcome,
)
# ---- 단독 학습 엔트리 (비교는 compare_qtable_vs_dqn) --------------------------------
def main(episodes=10000, seed=42):
random.seed(seed); np.random.seed(seed); torch.manual_seed(seed)
numbers, feat, strat = load_cards()
train_pool = [c for c in numbers if c not in HOLDOUT]
card_dim = feat[numbers[0]].shape[0]
print(f"카드 {len(numbers)}장 (학습 {len(train_pool)} / 홀드아웃 {HOLDOUT}) card_dim={card_dim}")
tcfg = TenantConfigLoader().load("ktcommerce")
policy = FeatureDQNPolicy(state_dim=STATE_FEATURE_DIM + TENANT_FEATURE_DIM,
card_dim=card_dim, eps_decay=4000, gamma=0.95)
rng = np.random.default_rng(seed)
print(f"=== 학습 {episodes} 에피소드 (협력사·성향 랜덤, CPU) ===")
recent = []
for ep in range(1, episodes + 1):
sup = sample_supplier(rng)
rcfg, tf = sample_tenant_pref(rng, tcfg.reward)
rc = RewardCalculator(rcfg, tcfg.state)
buyer = FeatureBuyer(sup, strat, seed=seed * 100 + ep, max_turns=MAX_TURNS)
scenario = Scenario(anchor_price=ANCHOR, target_price=TARGET, revenue_amount=sup.revenue_amount,
distribution_code=sup.distribution_code, partner_count=sup.partner_count)
price0 = TARGET * 1.15
price, used, total_r = price0, set(), 0.0
for turn in range(1, MAX_TURNS + 1):
acceptance = max(0.0, (price0 - price) / price0)
s = make_snapshot(sup, price, turn, acceptance)
sf = np.concatenate([build_state_features(s), tf])
avail = [c for c in train_pool if c not in used] or list(train_pool)
embs = np.stack([feat[c] for c in avail])
i, _, _ = policy.select(sf, embs)
card = avail[i]; used.add(card)
resp = buyer.respond(card, scenario, turn, price)
price = resp.new_price
done = resp.accept or price <= ANCHOR or turn >= MAX_TURNS
success = resp.accept or price <= ANCHOR
outcome = (NegotiationOutcome.SUCCESS if success
else NegotiationOutcome.FAILURE if done else NegotiationOutcome.ONGOING)
# 최종 결과 시점만 채점 (중간 0 → γ 부트스트랩) — compare 스크립트와 동일 규칙.
r = rc.calculate(make_snapshot(sup, price, turn, acceptance, outcome)).total if done else 0.0
total_r += r
if done:
policy.remember(sf, feat[card], r, None, None, True)
else:
acc2 = max(0.0, (price0 - price) / price0)
s2 = make_snapshot(sup, price, turn + 1, acc2)
navail = [c for c in train_pool if c not in used] or list(train_pool)
policy.remember(sf, feat[card], r, np.concatenate([build_state_features(s2), tf]),
np.stack([feat[c] for c in navail]), False)
policy.train_step()
if done:
break
recent.append(total_r)
if ep % 2000 == 0:
print(f" ep {ep:>6} eps={policy.eps():.3f} 최근2000 평균보상={np.mean(recent[-2000:]):.4f}")
policy.save(CKPT_PATH)
print(f"[저장] {CKPT_PATH}")
return policy
if __name__ == "__main__":
main()

View File

@ -1,365 +0,0 @@
"""train_full_autonomy — 행동 룰 0개, 완전 자율 협상 에이전트 (v2 시뮬 프로토타입).
기존 시스템의 (앵커 이하 강제타결 / 3라운드 강제결렬 / 와일드카드 / 카드 카탈로그)
전부 제거하고, 모든 결정을 에이전트 행동으로 이관한다:
행동 공간 (action-as-feature, ScoreNet 후보 열거 채점):
ACCEPT 현재 제시가로 타결 '앵커 이하 강제타결' 대체
WALK 협상 결렬 선언 '3라운드 강제결렬' 대체
COUNTER(C) "C원이면 수락" 역제안 '와일드카드 1%' 대체 (금액도 학습)
PRESS(strategy) 설득 압박(카드의 일반화) 카드 카탈로그 대체 (전략만 남음)
룰이 사라진 자리는 보상이 채운다(유일한 스펙):
R = W×R_price + (1W)×R_end λ×round (기존 RewardCalculator 그대로)
협상이 끝나는 : 에이전트의 ACCEPT/WALK, 협력사의 COUNTER 수락, 협력사의 인내심 소진(이탈).
마지막 것은 시스템 룰이 아니라 상대방 특성이다.
베이스라인 = 현행 시스템을 같은 환경에서 재현(앵커타결/1%클로징/3라운드결렬 + 압박).
실행: APP_ENV=local PYTHONUTF8=1 python -m tools.train_full_autonomy
"""
import os
import random
from typing import Optional, Tuple
import numpy as np
import torch
from eval_harness.feature_buyer import AFFINITY, STRATEGY_PROFILE, SupplierProfile, sample_supplier
from negotiation.policies.autonomy_actions import (
ACTION_DIM, ACTIONS, COUNTER_GRID, EXTRA_STATE_DIM, Action, extra_state,
internet_gap_feat, settle_norm as extra_settle)
from negotiation.policies.feature_dqn_policy import FeatureDQNPolicy
from negotiation.qtable.domain.model.snapshot import NegotiationOutcome, NegotiationSnapshot
from negotiation.qtable.domain.service.feature_builder import (
STATE_FEATURE_DIM, TENANT_FEATURE_DIM, build_state_features, build_tenant_features)
from negotiation.qtable.domain.service.reward_calculator import RewardCalculator
from tenancy.config_loader import TenantConfigLoader
from tools.train_feature_dqn import pref_config, sample_tenant_pref
_HERE = os.path.dirname(os.path.abspath(__file__))
CKPT_PATH = os.path.join(_HERE, "..", "artifacts", "full_autonomy.pt")
TARGET = 10000.0
# 앵커율(v3.1): 실운영 기하 정합 — 앵커가 = 목표가×(1a), a ∈ [0.8%, 6%] 를 에피소드마다 샘플링.
# (기존 고정 20% 폭은 실제(≈1%)와 지형이 달라, 실서비스에서 압박/역제안 밸런스가 어긋났다.)
ANCHOR_RATE_RANGE = (0.008, 0.06)
# 행동 공간(Action/ACTIONS/COUNTER_GRID/ACTION_DIM)은 negotiation.policies.autonomy_actions 공유
# — 서빙(autonomy_store, numpy 전용)과 학습이 같은 인코딩을 쓴다.
# ---- 협력사 모델 (상대 반응: 역제안 수락/재제안 포함) ---------------------------------
class AutonomousBuyer:
"""FeatureBuyer 확장: 역제안(C)에 반응한다. 이탈은 '인내심' — 시스템 룰이 아닌 상대 특성."""
def __init__(self, sup: SupplierProfile, seed: int):
self.sup = sup
self.rng = np.random.default_rng(seed)
# 기질 t ∈ [0,1]: 0=터프(하한 높고 안 물러섬) ↔ 1=수월. 관측 가능한 이력·최저가가
# 이 숨은 기질과 상관되게 생성된다 → 에이전트가 이력/최저가 특징을 읽을 '이유'가 생긴다.
# 하한은 '우리 앵커'가 아니라 협력사 사정(≈목표가 기준)으로 정해진다(v3.1) —
# 하한 > 목표가(≈35%)면 애초에 성사 불가능한 협상이고, 그걸 빨리 알아채고 끊는 것도 실력이다.
t = float(self.rng.uniform(0.0, 1.0))
self.floor = TARGET * float(np.clip(1.12 - 0.24 * t + self.rng.normal(0, 0.02), 0.85, 1.18))
self.patience = int(self.rng.integers(4, 9)) + (1 if t > 0.7 else 0)
# 첫 제시가: 목표가의 105~150% — 실운영(기존 공급가가 목표가를 26%+ 상회) 분포를 덮는다.
# 좁게(110~125%) 학습하면 큰 갭 상황에서 정책이 분포 밖 일반화(대형컷 역제안)를 한다.
self.price = TARGET * float(self.rng.uniform(1.05, 1.50))
# 하한가가 첫 제시가보다 높을 수 없다(자기 하한 밑으로 부르고 시작하는 판매자는 없음).
# 이 보정이 없으면 on_press 의 max(floor,·)가 가격을 '역주행'시키는 비현실이 생긴다.
self.floor = min(self.floor, self.price * 0.98)
self._last_c: Optional[float] = None # 직전 역제안 (같은 숫자 반복 짜증 모델링)
# ---- 관측 가능 부가정보 (v3 특징 소스 — 기질과 상관, 노이즈 있음) ----
self.hist_n = int(self.rng.integers(0, 6)) # 과거 협상 횟수 (0=신규)
if self.hist_n:
self.hist_success = float(np.clip(0.25 + 0.6 * t + self.rng.normal(0, 0.10), 0.0, 1.0))
self.hist_settle_ratio = float(np.clip(1.18 - 0.28 * t + self.rng.normal(0, 0.04), 0.80, 1.30))
else:
self.hist_success = self.hist_settle_ratio = None
# 인터넷최저가: 숨은 하한가의 노이즈 관측치. 60% 확률로만 수집돼 있음(현실: 미수집 흔함).
self.internet_lowest = (self.floor * float(self.rng.uniform(0.98, 1.08))
if self.rng.random() < 0.6 else None)
def _powers(self, strategy: int) -> Tuple[float, float]:
conc, acc = STRATEGY_PROFILE.get(strategy, (0.5, 0.5))
m = AFFINITY[self.sup.segment].get(strategy, 0.5)
scale = 0.35 + 0.85 * m
return conc * scale, acc * scale
def on_press(self, strategy: int, turn: int) -> Tuple[bool, float]:
"""(이탈여부, 새 제시가). 압박이 안 먹히는 세그먼트면 이탈 위험이 실재한다."""
c_pow, a_pow = self._powers(strategy)
walk_p = 0.04 + 0.30 * (1.0 - a_pow) * (turn / self.patience)
if self.rng.random() < walk_p:
return True, self.price
concession = (self.price - self.floor) * (0.10 + 0.55 * c_pow)
self.price = max(self.floor, self.price - concession)
return False, self.price
def on_counter(self, c: float, strategy: int, turn: int) -> Tuple[str, float]:
"""역제안 C 반응: 'accept'(C로 타결) | 'walk' | 'counter'(새 제시가).
현실화(v2): 제시가 대비 인하 요구폭(cut) 클수록 수락률이 급감하고 이탈 위험이 커진다
초기 버전에서 에이전트가 't1 원샷 로우볼' 시뮬 허점을 착취하던 것을 막는다.
압박으로 가격을 충분히 끌어내린 작은 컷으로 클로징해야 통하는 구조.
"""
_, a_pow = self._powers(strategy or 3)
cut = max(0.0, (self.price - c) / max(self.price, 1.0)) # 인하 요구폭 (현 제시가 대비)
prev_c = self._last_c
repeated = prev_c is not None and abs(c - prev_c) < 1e-6 # 같은 숫자 반복
self._last_c = c
# 양보 상호성(v3.3): 직전 제안보다 올려 부르면(성의 있는 양보) 호의적으로 반응한다.
# 이 신호가 있어야 '상대가 내리면 우리도 조금 올리는' tit-for-tat 이 학습으로 나온다.
warm = 0.0
if prev_c is not None and c > prev_c + 1e-9:
warm = float(np.clip((c - prev_c) / max(self.price - self.floor, 1.0), 0.0, 0.35))
if c >= self.floor:
margin = (c - self.floor) / max(self.floor, 1.0)
p_acc = float(np.clip(0.20 + 0.9 * margin / 0.08, 0.0, 0.95)) * (0.75 + 0.35 * a_pow)
p_acc *= float(np.clip(1.0 - (cut - 0.05) / 0.20, 0.0, 1.0)) # 컷 5% 초과부터 반발, 25%면 수락 0
if repeated:
p_acc *= 0.25 # 이미 거절한 숫자를 또 내밀면 설득력 급감
p_acc *= 1.0 + warm
if self.rng.random() < min(p_acc, 0.97):
return "accept", c
# 모욕적 요구(하한 미달·과도한 원샷 컷·앵무새 반복) → 이탈 위험
low = max(0.0, (self.floor - c) / max(self.floor, 1.0))
p_walk = min(0.5, 2.0 * low) + 0.35 * max(0.0, cut - 0.20) / 0.20 + (0.15 if repeated else 0.0)
if self.rng.random() < min(p_walk * (1.0 - warm), 0.7):
return "walk", self.price
self.price = max(self.floor, c + (self.price - c) * float(self.rng.uniform(0.30, 0.60) + warm))
return "counter", self.price
# ---- 에피소드 실행 (룰 없음 — 종료는 행동 또는 상대 특성으로만) ------------------------
def make_snapshot(sup, price, turn, p0, anchor, outcome=NegotiationOutcome.ONGOING):
return NegotiationSnapshot(
revenue_amount=sup.revenue_amount, distribution_code=sup.distribution_code,
partner_count=sup.partner_count, acceptance_ratio=max(0.0, (p0 - price) / p0),
input_price=price, anchor_price=anchor, target_price=TARGET,
round_number=turn, outcome=outcome)
MIN_PRESS = int(os.getenv("AUTONOMY_MIN_PRESS", "2")) # 역제시 해금에 필요한 최소 설득 횟수
def available_actions(price: float, last_counter_q: Optional[float] = None,
counter_locked: bool = False) -> list:
"""행동 봉투 (serving autonomy_store 와 동일해야 한다):
목표가 초과 제시가는 '수락' 제외 매입 승인 범위(목표가 초과 수락 착취 방지)
직전 역제안 미만 금액의 역제안 제외 단조 양보 원칙(제안 철회 금지;
양보 '속도' 정책이 배우고, 후퇴 '금지' 구조로 보장)
counter_locked: 설득 MIN_PRESS 전에는 역제시 잠금 제품 의미론
(일반 카드=설득, 역제시=와일드카드 성격의 마무리 수단) 복원
마무리 국면(제시가 목표가×1.005): 압박 제외 푼돈 차이에서 재검토 요청 반복 방지
역제안은 앵커 이하(q 0) 낮게 개시해 사다리를 쓰며 올라간다"""
near_target = price <= TARGET * 1.005
return [a for a in ACTIONS
if not (a.kind == "accept" and price > TARGET)
and not (a.kind == "counter" and counter_locked and not near_target)
and not (a.kind == "walk" and counter_locked and not near_target)
and not (a.kind == "press" and near_target)
and not (a.kind == "counter" and last_counter_q is None and a.counter_q > 1e-9)
and not (a.kind == "counter" and last_counter_q is not None
and a.counter_q < last_counter_q - 1e-9)]
def action_feats(price: float, anchor: float, last_counter_q: Optional[float] = None,
counter_locked: bool = False):
"""현 제시가 기준 (가용 행동 리스트, 특징 [K, ACTION_DIM]). counter 는 컷폭 포함."""
span = max(TARGET - anchor, 1.0)
pos = (price - anchor) / span
acts = available_actions(price, last_counter_q, counter_locked)
rows = []
for a in acts:
cut = 0.0
if a.kind == "counter":
c = anchor + a.counter_q * span
cut = max(0.0, (price - c) / max(price, 1.0))
rows.append(a.feat(pos, cut))
return acts, np.stack(rows)
def run_episode(policy_fn, sup, rc: RewardCalculator, tf: np.ndarray, seed: int,
learner: Optional[FeatureDQNPolicy] = None, trace: Optional[list] = None):
"""policy_fn(state_feat, price_pos) → Action. learner 지정 시 replay 저장+학습."""
buyer = AutonomousBuyer(sup, seed)
p0 = buyer.price
env_rng = np.random.default_rng(seed + 7)
# 앵커율 샘플링(v3.1): 실운영처럼 앵커가 목표가 바로 아래(0.8~6%) — 좁은 스팬 지형에서 학습.
anchor = TARGET * (1.0 - float(env_rng.uniform(*ANCHOR_RATE_RANGE)))
span = max(TARGET - anchor, 1.0)
turn, settled, walked = 0, None, False
last_kind, last_q = "", 0.0 # 직전 역제안 기억 (같은 숫자 반복 방지의 학습 근거)
press_n = 0 # 설득 횟수 — 역제시 해금(MIN_PRESS) 카운터
# 견적 마감(환경 사실): 마감 도달 시 협상은 미타결 종료된다 — 룰이 아니라 세상의 시계.
deadline_turns = int(env_rng.integers(3, 11))
# 관측성 마스크(v3.5): 실서빙은 마감·이력·최저가가 '없는' 세션이 흔하고 로더가 중립값
# (0.5/0)을 대입한다. 시뮬이 항상 다 아는 세계만 학습하면 그 중립 상태가 분포 밖이 된다
# — v3.4 가 라이브 소액 지형에서 첫 턴 결렬로 퇴화한 원인 추정. 세계(마감 종료·상대 특성)는
# 그대로 두고 관측만 가린다: 마감은 40% 미관측(0.5 고정), 15% 는 전부 미상(신규 견적의 전형).
deadline_known = env_rng.random() < 0.6
blind = env_rng.random() < 0.15
if blind:
deadline_known = False
# 협력사 이력·최저가 특징 (에피소드 내 불변)
known_hist = buyer.hist_n and not blind
fixed_extra = dict(
hist_n=min(buyer.hist_n, 5) / 5.0 if not blind else 0.0,
hist_success=buyer.hist_success if known_hist else 0.5,
hist_settle=extra_settle(buyer.hist_settle_ratio) if known_hist else 0.5,
internet_gap=internet_gap_feat(buyer.internet_lowest or 0.0, anchor) if not blind else 0.0,
)
pending = None # (state_feat, action_feat) — 최종 결과 시점만 채점, 중간 r=0
while True:
turn += 1
price = buyer.price
deadline_remain = (max(0.0, (deadline_turns - turn + 1) / deadline_turns)
if deadline_known else 0.5) # 미관측 → 서빙 로더와 동일한 중립값
sf = np.concatenate([build_state_features(make_snapshot(sup, price, turn, p0, anchor)), tf,
extra_state(last_kind, last_q, deadline=deadline_remain, **fixed_extra)])
lcq = last_q if last_kind == "counter" else None
locked = lcq is None and press_n < MIN_PRESS
act = policy_fn(sf, price, anchor, lcq, locked)
if trace is not None:
trace.append((turn, int(price), act))
if act.kind == "accept":
settled = price
elif act.kind == "walk":
walked = True
elif act.kind == "counter":
c = anchor + act.counter_q * span
resp, val = buyer.on_counter(c, act.strategy, turn)
last_kind, last_q = "counter", act.counter_q # 역제안 기억 갱신
if resp == "accept":
settled = c
elif resp == "walk":
walked = True
else: # press
press_n += 1
left, _ = buyer.on_press(act.strategy, turn)
walked = walked or left
if not settled and not walked and turn >= buyer.patience:
walked = True # 인내심 소진(상대 특성) — 시스템 룰 아님
if not settled and not walked and turn >= deadline_turns:
walked = True # 견적 마감 도달(환경 사실) — 미타결 종료
done = settled is not None or walked
final_price = settled if settled is not None else buyer.price
# 성사 보너스는 목표가 이하 타결에만 — v3.1 이 '비싸게라도 성사'로 착취한 보상 구멍의
# 원인 차단(봉투 ① 의 마스크와 이중 방어: 유인 자체를 올바르게). 초과 타결 = 결렬 취급.
outcome = (NegotiationOutcome.SUCCESS if settled is not None and settled <= TARGET
else NegotiationOutcome.FAILURE if done else NegotiationOutcome.ONGOING)
r = rc.calculate(make_snapshot(sup, final_price, turn, p0, anchor, outcome)).total if done else 0.0
if learner is not None:
pos = (price - anchor) / span
cut = 0.0
if act.kind == "counter":
cut = max(0.0, (price - (anchor + act.counter_q * span)) / max(price, 1.0))
af = act.feat(pos, cut)
if pending:
nxt_lcq = last_q if last_kind == "counter" else None
learner.remember(*pending, 0.0, sf,
action_feats(price, anchor, nxt_lcq,
nxt_lcq is None and press_n < MIN_PRESS)[1], False)
pending = (sf, af)
if done:
learner.remember(sf, af, r, None, None, True)
learner.train_step()
if done:
return settled, turn, r
# ---- 정책들 ------------------------------------------------------------------
def dqn_policy(policy: FeatureDQNPolicy):
def f(sf, price, anchor, last_counter_q=None, counter_locked=False):
acts, feats = action_feats(price, anchor, last_counter_q, counter_locked)
i, _, _ = policy.select(sf, feats)
return acts[i]
return f
class RuleBaseline:
"""현행 시스템 룰 재현: 앵커 이하 수락 / 존내 1% 클로징 / 3회 압박 후 결렬."""
def __init__(self):
self.presses, self.closed = 0, False
def __call__(self, sf, price, anchor, last_counter_q=None, counter_locked=False) -> Action:
span = max(TARGET - anchor, 1.0)
if price <= anchor:
return Action("accept")
if price <= anchor * 1.02 and not self.closed:
self.closed = True
return Action("counter", (price * 0.99 - anchor) / span, 3)
if self.presses < 3:
self.presses += 1
return Action("press", 0.0, 3)
return Action("walk")
# ---- 학습/평가 ----------------------------------------------------------------
def evaluate(name, make_policy_fn, base_cfg, tcfg_state, episodes=3000, seed0=777):
rc = RewardCalculator(pref_config(base_cfg, 0.5), tcfg_state)
tf = build_tenant_features(pref_config(base_cfg, 0.5))
rng = np.random.default_rng(seed0)
rewards, settles, rounds = [], [], []
for i in range(episodes):
sup = sample_supplier(rng)
settled, turn, r = run_episode(make_policy_fn(), sup, rc, tf, seed0 * 91 + i)
rewards.append(r)
rounds.append(turn)
if settled is not None:
settles.append(settled / TARGET)
sr = len(settles) / episodes
print(f"{name:<14} 보상 {np.mean(rewards):.4f} ±{np.std(rewards)/np.sqrt(episodes):.4f}"
f" 성사율 {sr:.3f} 타결가/목표 {np.mean(settles):.3f} 평균라운드 {np.mean(rounds):.2f}")
return dict(reward=float(np.mean(rewards)), success=sr,
settle_ratio=float(np.mean(settles)) if settles else None, rounds=float(np.mean(rounds)))
def main(episodes=15000, seed=42):
random.seed(seed); np.random.seed(seed); torch.manual_seed(seed)
tcfg = TenantConfigLoader().load("ktcommerce")
policy = FeatureDQNPolicy(state_dim=STATE_FEATURE_DIM + TENANT_FEATURE_DIM + EXTRA_STATE_DIM,
card_dim=ACTION_DIM, eps_decay=5000, gamma=0.97)
rng = np.random.default_rng(seed)
print(f"=== 완전 자율 학습 {episodes}ep (행동 {len(ACTIONS)}개, 룰 0개) ===")
recent = []
for ep in range(1, episodes + 1):
sup = sample_supplier(rng)
rcfg, tf = sample_tenant_pref(rng, tcfg.reward)
rc = RewardCalculator(rcfg, tcfg.state)
_, _, r = run_episode(dqn_policy(policy), sup, rc, tf, seed * 131 + ep, learner=policy)
recent.append(r)
if ep % 3000 == 0:
print(f" ep {ep:>6} eps={policy.eps():.3f} 최근3000 평균보상={np.mean(recent[-3000:]):.4f}")
policy.save(CKPT_PATH)
print("\n=== 평가 3000ep (중립 성향 p=0.5, 동일 협력사 분포) ===")
policy.greedy = True
evaluate("룰시스템(현행)", lambda: RuleBaseline(), tcfg.reward, tcfg.state)
evaluate("완전자율 DQN", lambda: dqn_policy(policy), tcfg.reward, tcfg.state)
# 궤적 예시 — 에이전트가 룰 없이 뭘 하는지 눈으로
print("\n=== 궤적 예시 (완전자율) ===")
rc = RewardCalculator(pref_config(tcfg.reward, 0.5), tcfg.state)
tf = build_tenant_features(pref_config(tcfg.reward, 0.5))
rng2 = np.random.default_rng(7)
for k in range(3):
sup = sample_supplier(rng2)
trace = []
settled, turn, r = run_episode(dqn_policy(policy), sup, rc, tf, 5000 + k, trace=trace)
seg = "·".join(sup.segment)
print(f"[{seg}] " + "".join(
f"t{t}:{p:,}{a.kind}{'' if a.kind in ('accept', 'walk') else f'({a.counter_q:.2f},전략{a.strategy})' if a.kind == 'counter' else f'(전략{a.strategy})'}"
for t, p, a in trace) + f"{'타결 ' + format(int(settled), ',') + '' if settled else '결렬'} (r={r:.3f})")
if __name__ == "__main__":
main()

View File

@ -58,6 +58,21 @@ class suppliers(MAIN_BASE):
deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부 deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부
class companies(MAIN_BASE):
# company.companies (고객사). 공급사 포털 브랜딩(settings.branding) 조회 전용 미러.
@staticmethod
def DBType():
return DBType.PARTNER.value
__tablename__ = "companies"
__table_args__ = {"schema": "company"}
company_id = Column(UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()")) # 회사 식별자(PK)
name = Column(String(100), nullable=False) # 회사명
settings = Column(JSONB, nullable=True) # 회사별 커스터마이징(branding/labels 등, negodata 소유)
deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부
class items(MAIN_BASE): class items(MAIN_BASE):
# partner.items (상품). # partner.items (상품).
@staticmethod @staticmethod
@ -123,6 +138,7 @@ class sessions(MAIN_BASE):
reject_reason = Column(String(255), nullable=True) # 거절 사유 reject_reason = Column(String(255), nullable=True) # 거절 사유
reject_price = Column(BigInteger, nullable=True) # 거절 시 제시가(원) reject_price = Column(BigInteger, nullable=True) # 거절 시 제시가(원)
reject_delivery_type = Column(SmallInteger, nullable=True) # 거절 시 배송 유형 (코드) reject_delivery_type = Column(SmallInteger, nullable=True) # 거절 시 배송 유형 (코드)
custom = Column(JSONB, nullable=True) # 협상완료 부가정보 값 {key: value} (정의는 companies.settings.session_fields)
created_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')")) # 생성 시각(UTC) created_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')")) # 생성 시각(UTC)
updated_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')"), onupdate=text("(now() AT TIME ZONE 'utc')")) # 수정 시각(UTC, UPDATE 시 자동 갱신) updated_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')"), onupdate=text("(now() AT TIME ZONE 'utc')")) # 수정 시각(UTC, UPDATE 시 자동 갱신)
deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부 deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부
@ -157,6 +173,7 @@ class quotations(MAIN_BASE):
preferred_sp_yn = Column(Boolean, nullable=True) # 선호 공급사 지정 여부 preferred_sp_yn = Column(Boolean, nullable=True) # 선호 공급사 지정 여부
preferred_sp_id = Column(UUID(as_uuid=True), nullable=True) # 선호 공급사(partner.suppliers.supplier_id) preferred_sp_id = Column(UUID(as_uuid=True), nullable=True) # 선호 공급사(partner.suppliers.supplier_id)
preferred_sp_name = Column(String(20), nullable=True) # 선호 공급사명(스냅샷) preferred_sp_name = Column(String(20), nullable=True) # 선호 공급사명(스냅샷)
close_reason = Column(SmallInteger, nullable=True) # 마감 사유(CloseReason). 재협상 요청 자격 판정에 읽는다
equal_bid_yn = Column(Boolean, nullable=True) # 동일가 입찰 발생 여부 equal_bid_yn = Column(Boolean, nullable=True) # 동일가 입찰 발생 여부
equal_bid_data = Column(JSONB, nullable=True) # 동일가 입찰 상세(JSON) equal_bid_data = Column(JSONB, nullable=True) # 동일가 입찰 상세(JSON)
created_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')")) # 생성 시각(UTC) created_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')")) # 생성 시각(UTC)
@ -164,6 +181,28 @@ class quotations(MAIN_BASE):
deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부 deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부
class notifications(MAIN_BASE):
# company.notifications (담당자 인박스). 포털은 재협상 요청 알림을 만들기 위해서만 쓴다(조회는 negodata).
# company 스키마 전용 DBType 이 없어 USER 커넥션을 재사용한다(물리 DB 동일).
@staticmethod
def DBType():
return DBType.USER.value
__tablename__ = "notifications"
__table_args__ = {"schema": "company"}
notification_id = Column(UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"))
user_id = Column(UUID(as_uuid=True), nullable=False) # 수신자(company.users.user_id) = 견적 작성자
type = Column(SmallInteger, nullable=False) # NotificationType
ref_qt_id = Column(UUID(as_uuid=True), nullable=True)
ref_session_id = Column(UUID(as_uuid=True), nullable=True)
data = Column(JSONB, nullable=True) # 렌더 스냅샷(공급사명·사유·희망가 등)
read_at = Column(DateTime(timezone=True), nullable=True)
created_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')"))
updated_at = Column(DateTime(timezone=True), nullable=False, server_default=text("(now() AT TIME ZONE 'utc')"), onupdate=text("(now() AT TIME ZONE 'utc')"))
deleted = Column(Boolean, nullable=False, server_default=text("false"))
class quotation_settings(MAIN_BASE): class quotation_settings(MAIN_BASE):
# quotation.quotation_settings (견적 설정). 견적 설정 스냅샷 — anchoring_value 는 구(舊) 앵커 산출용으로 채팅 경로에서는 더 이상 사용하지 않음(앵커는 sessions.anchoring_price 박제값). # quotation.quotation_settings (견적 설정). 견적 설정 스냅샷 — anchoring_value 는 구(舊) 앵커 산출용으로 채팅 경로에서는 더 이상 사용하지 않음(앵커는 sessions.anchoring_price 박제값).
@staticmethod @staticmethod
@ -183,6 +222,34 @@ class quotation_settings(MAIN_BASE):
deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부 deleted = Column(Boolean, nullable=False, server_default=text("false")) # 소프트 삭제 여부
class nego_cards(MAIN_BASE):
# card.nego_cards (협상카드). backend 는 번호→UUID 변환(chats.card_id 저장)만 위해 최소 컬럼 미러.
@staticmethod
def DBType():
return DBType.NEGOTIATION.value # 같은 negosium_db — chats 와 동일 세션풀로 조회
__tablename__ = "nego_cards"
__table_args__ = {"schema": "card"}
nego_card_id = Column(UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"))
number = Column(String(10), nullable=True) # 카드 번호(agent turn.card_id 와 매칭)
deleted = Column(Boolean, nullable=False, server_default=text("false"))
class wild_cards(MAIN_BASE):
# card.wild_cards (와일드카드). backend 는 번호→UUID 변환(chats.card_id 저장)만 위해 최소 컬럼 미러.
@staticmethod
def DBType():
return DBType.NEGOTIATION.value # 같은 negosium_db
__tablename__ = "wild_cards"
__table_args__ = {"schema": "card"}
wild_card_id = Column(UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"))
number = Column(String(10), nullable=True)
deleted = Column(Boolean, nullable=False, server_default=text("false"))
class chats(MAIN_BASE): class chats(MAIN_BASE):
# negotiation.chats (협상 채팅 메시지 로그). session 1 : N chats. (session_id, seq) 유니크. # negotiation.chats (협상 채팅 메시지 로그). session 1 : N chats. (session_id, seq) 유니크.
@staticmethod @staticmethod

View File

@ -130,6 +130,41 @@ class QuotationStatus(Enum):
CLOSED = 3 # 견적마감 CLOSED = 3 # 견적마감
class CloseReason(Enum):
"""견적 마감 사유. quotation.quotations.close_reason
낙찰(AWARDED) OPEN_* 낙찰자 미정으로 마감된 '결렬' 공급사 재협상 요청 대상."""
AWARDED = 1 # 낙찰
OPEN_PRICE = 5 # 개찰: 낙찰 기준 미달
OPEN_EQUAL = 6 # 개찰: 동가
OPEN_NOSHOW = 7 # 개찰: 전원 미응찰
OPEN_REJECT = 8 # 개찰: 협상거부 존재
# 재협상 요청 가능한 마감 사유(낙찰 건은 제외).
RENEGOTIABLE_CLOSE_REASONS = (
CloseReason.OPEN_PRICE.value,
CloseReason.OPEN_EQUAL.value,
CloseReason.OPEN_NOSHOW.value,
CloseReason.OPEN_REJECT.value,
)
class RenegotiationStatus(Enum):
"""sessions.custom.renegotiation.status — 공급사 재협상 요청 상태(IMK #15)."""
PENDING = 1 # 접수, 담당자 심사 대기
APPROVED = 2 # 승인 — 다음 라운드 생성됨
REJECTED = 3 # 반려
CANCELED = 4 # 공급사 철회
class NotificationType(Enum):
"""company.notifications.type — negodata 담당자 인박스. 포털에서 만드는 건 재협상 요청뿐."""
RENEGO_REQUESTED = 5
class ChatSender(Enum): class ChatSender(Enum):
"""채팅 발신자 코드. negotiation.chats.sender """ """채팅 발신자 코드. negotiation.chats.sender """

View File

@ -6,7 +6,7 @@ from sqlalchemy import asc, desc, select, update
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from common.database.db_session_manager import DB_SESSION_MNG from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import chats, items, sessions from common.database.model.models import chats, items, sessions, nego_cards, wild_cards
from common.enums import ErrorType, SessionStatus from common.enums import ErrorType, SessionStatus
from common.logger import LOG from common.logger import LOG
@ -47,6 +47,14 @@ class IChatCRUD(ABC):
async def update_last_offer_price(self, cdb: AsyncSession, session_id, price: int) -> ErrorType: async def update_last_offer_price(self, cdb: AsyncSession, session_id, price: int) -> ErrorType:
pass pass
@abstractmethod
async def get_nego_card_id_by_number(self, cdb: AsyncSession, number: str):
pass
@abstractmethod
async def get_wild_card_id_by_number(self, cdb: AsyncSession, number: str):
pass
class ChatCRUD(IChatCRUD): class ChatCRUD(IChatCRUD):
async def list_by_session(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, list]: async def list_by_session(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, list]:
@ -112,6 +120,30 @@ class ChatCRUD(IChatCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None return ErrorType.DB_RUN_FAILED, None
async def get_nego_card_id_by_number(self, cdb: AsyncSession, number: str):
# 협상카드 번호(agent turn.card_id) → nego_card_id(UUID). 없으면 None. 카드 사용 로그(chats.card_id) 저장용.
try:
query = select(nego_cards.nego_card_id).where(nego_cards.number == number, nego_cards.deleted == False).limit(1) # noqa: E712
err_type, row_list = await DB_SESSION_MNG.execute(cdb, query, f"get_nego_card_id_by_number({number}) failed.")
if err_type != ErrorType.SUCCESS or not row_list:
return None
return row_list[0]
except Exception as ex:
LOG.e_no_callstack(ex)
return None
async def get_wild_card_id_by_number(self, cdb: AsyncSession, number: str):
# 와일드카드 번호(agent turn.card_id, wild_card_dynamic) → wild_card_id(UUID). 없으면 None.
try:
query = select(wild_cards.wild_card_id).where(wild_cards.number == number, wild_cards.deleted == False).limit(1) # noqa: E712
err_type, row_list = await DB_SESSION_MNG.execute(cdb, query, f"get_wild_card_id_by_number({number}) failed.")
if err_type != ErrorType.SUCCESS or not row_list:
return None
return row_list[0]
except Exception as ex:
LOG.e_no_callstack(ex)
return None
async def finalize_session( async def finalize_session(
self, cdb: AsyncSession, session_id, status: int, self, cdb: AsyncSession, session_id, status: int,
bid_price: Optional[int] = None, reject_reason: Optional[str] = None, reject_price: Optional[int] = None, bid_price: Optional[int] = None, reject_reason: Optional[str] = None, reject_price: Optional[int] = None,

View File

@ -1,24 +1,40 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Tuple from typing import Optional, Tuple
from sqlalchemy import case, func, nulls_last, select, update from sqlalchemy import and_, case, cast, func, nulls_last, or_, select, text, update
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from common.database.db_session_manager import DB_SESSION_MNG from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import items, quotations, sessions from common.database.model.models import chats, items, quotations, sessions
from common.enums import ErrorType, SessionStatus from common.enums import CloseReason, ErrorType, QuotationStatus, RENEGOTIABLE_CLOSE_REASONS, SessionStatus
from common.logger import LOG from common.logger import LOG
# 협상 세션 CRUD. 목록은 세션(negotiation) ⨝ 상품(partner) ⨝ 견적(quotation) 조인으로 만든다. # 협상 세션 CRUD. 목록은 세션(negotiation) ⨝ 상품(partner) ⨝ 견적(quotation) 조인으로 만든다.
# 마감일(qt_end_time)은 견적(quotation.end_time)이 진실값이다(session.end_time 은 협상 종료 시점 기록용). # 마감일(qt_end_time)은 견적(quotation.end_time)이 진실값이다(session.end_time 은 협상 종료 시점 기록용).
def _effective_status():
"""표시용 세션 상태. 견적이 마감됐거나 마감시간이 지났으면 협상생성(1)은 더 참여할 수 없으므로 미참여(4)로 본다.
참여/채팅진입이 진입 시점에 하는 전이(negotiation_service._load_actionable_session, chat_service.init) 같은 규칙을
목록에서는 쓰기 없이 파생으로만 맞춘다. 마감 일괄정리 이후에 만들어진 세션도 '협상 대기' 남지 않는다.
"""
ended = or_(quotations.status == QuotationStatus.CLOSED.value, quotations.end_time < func.now())
return case(
(and_(sessions.status == SessionStatus.CREATED.value, ended), SessionStatus.NOT_PARTICIPATED.value),
else_=sessions.status,
)
class ISessionCRUD(ABC): class ISessionCRUD(ABC):
@abstractmethod @abstractmethod
async def list_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type, order, offset, limit) -> Tuple[ErrorType, list]: async def list_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type, order, offset, limit, keyword=None, result=None) -> Tuple[ErrorType, list]:
pass pass
@abstractmethod @abstractmethod
async def count_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type) -> Tuple[ErrorType, int]: async def count_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type, keyword=None, result=None) -> Tuple[ErrorType, int]:
pass pass
@abstractmethod @abstractmethod
@ -38,23 +54,56 @@ class ISessionCRUD(ABC):
pass pass
@abstractmethod @abstractmethod
async def update_session_reject(self, cdb: AsyncSession, session_id, status: int, reject_reason: str) -> ErrorType: async def update_session_reject(
self, cdb: AsyncSession, session_id, status: int, reject_reason: str, reject_price: Optional[int] = None,
) -> ErrorType:
pass
@abstractmethod
async def update_session_custom(self, cdb: AsyncSession, session_id, supplier_id, custom: dict) -> ErrorType:
pass
@abstractmethod
async def merge_session_custom(self, cdb: AsyncSession, session_id, supplier_id, patch: dict) -> ErrorType:
pass
@abstractmethod
async def chain_max_round(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, int]:
pass pass
class SessionCRUD(ISessionCRUD): class SessionCRUD(ISessionCRUD):
@staticmethod @staticmethod
def __filters(supplier_id, status, qt_type): def __filters(supplier_id, status, qt_type, keyword=None, result=None):
conds = [sessions.supplier_id == supplier_id, sessions.deleted == False] # noqa: E712 conds = [sessions.supplier_id == supplier_id, sessions.deleted == False] # noqa: E712
if status is not None: if status is not None:
conds.append(sessions.status == status) # 표시 상태로 필터 — 탭/KPI 카운트가 목록 배지와 어긋나지 않게 파생값을 그대로 쓴다.
conds.append(_effective_status() == status)
if qt_type is not None: if qt_type is not None:
conds.append(sessions.qt_type == qt_type) conds.append(sessions.qt_type == qt_type)
# 검색: 견적번호·상품명·상품코드 부분일치(대소문자 무시). items 는 목록/카운트 둘 다 조인돼 있다.
# ILIKE 와일드카드(%,_)는 escape 해 사용자 입력이 패턴으로 새지 않게 한다.
if keyword and keyword.strip():
kw = keyword.strip().replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
like = f"%{kw}%"
conds.append(or_(sessions.qt_number.ilike(like), items.name.ilike(like), items.code.ilike(like)))
# 결과(SessionResult) 필터 — _to_result 파생 규칙을 SQL WHERE 로 그대로 복제(집계·필터 일치용).
# 1=낙찰 2=미낙찰 3=결렬(개찰). 전부 견적 마감(CLOSED) 이 전제.
if result in (1, 2, 3):
conds.append(quotations.status == QuotationStatus.CLOSED.value)
if result == 1:
conds.append(quotations.close_reason == CloseReason.AWARDED.value)
conds.append(quotations.preferred_sp_id == sessions.supplier_id)
elif result == 2:
conds.append(quotations.close_reason == CloseReason.AWARDED.value)
conds.append(or_(quotations.preferred_sp_id.is_(None), quotations.preferred_sp_id != sessions.supplier_id))
else:
conds.append(quotations.close_reason.in_(RENEGOTIABLE_CLOSE_REASONS))
return conds return conds
async def list_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type, order, offset, limit) -> Tuple[ErrorType, list]: async def list_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type, order, offset, limit, keyword=None, result=None) -> Tuple[ErrorType, list]:
try: try:
conds = self.__filters(supplier_id, status, qt_type) conds = self.__filters(supplier_id, status, qt_type, keyword, result)
# 정렬 규칙: # 정렬 규칙:
# - order 를 명시(asc/desc)하면 그룹 구분 없이 전체를 마감 기준 한 줄로 정렬(전체 정렬). # - order 를 명시(asc/desc)하면 그룹 구분 없이 전체를 마감 기준 한 줄로 정렬(전체 정렬).
@ -69,7 +118,7 @@ class SessionCRUD(ISessionCRUD):
else: else:
# 그룹별로 정렬 방향이 달라, case 로 '자기 그룹 행만 end_time' 을 갖는 키를 만들고 # 그룹별로 정렬 방향이 달라, case 로 '자기 그룹 행만 end_time' 을 갖는 키를 만들고
# 반대 그룹은 NULL 로 눌러 간섭을 없앤다. status_rank 가 1차 키라 그룹 경계는 항상 유지. # 반대 그룹은 NULL 로 눌러 간섭을 없앤다. status_rank 가 1차 키라 그룹 경계는 항상 유지.
actionable = sessions.status.in_((SessionStatus.CREATED.value, SessionStatus.IN_PROGRESS.value)) actionable = _effective_status().in_((SessionStatus.CREATED.value, SessionStatus.IN_PROGRESS.value))
status_rank = case((actionable, 0), else_=1) status_rank = case((actionable, 0), else_=1)
action_order = case((actionable, quotations.end_time), else_=None).asc() action_order = case((actionable, quotations.end_time), else_=None).asc()
done_order = case((~actionable, quotations.end_time), else_=None).desc() done_order = case((~actionable, quotations.end_time), else_=None).desc()
@ -78,7 +127,7 @@ class SessionCRUD(ISessionCRUD):
query = ( query = (
select( select(
sessions.session_id, sessions.session_id,
sessions.status, _effective_status(), # 마감 후 남은 협상생성은 미참여로 내린다
sessions.qt_type, sessions.qt_type,
sessions.qt_number, sessions.qt_number,
quotations.end_time, # qt_end_time = 견적 마감 시각 quotations.end_time, # qt_end_time = 견적 마감 시각
@ -86,6 +135,17 @@ class SessionCRUD(ISessionCRUD):
items.name, items.name,
items.model_name, items.model_name,
items.manufacturer, items.manufacturer,
sessions.custom,
quotations.status, # 재협상 요청 자격 판정용(마감 여부)
quotations.close_reason, # 개찰(결렬) 사유
quotations.round,
quotations.preferred_sp_id, # 낙찰자(공급사) — 나와 같으면 낙찰, 다르면 미낙찰
sessions.supplier_id, # 이 세션 소유 공급사(=조회자). 낙찰자와 대조
# 대화 이력 유무 — 종료된 협상의 '결과 보기'(열람) 버튼을 띄울지 판단용. 열 게 없으면 프론트가 감춘다.
select(1).where(chats.session_id == sessions.session_id, chats.deleted == False).exists(), # noqa: E712
# 거부 건이 제출한 사유·희망가 — 목록의 '거부 내역' 열람용(의견은 custom.opinion).
sessions.reject_reason,
sessions.reject_price,
) )
.join(items, items.item_id == sessions.item_id) .join(items, items.item_id == sessions.item_id)
.join(quotations, quotations.qt_id == sessions.quotation_id) .join(quotations, quotations.qt_id == sessions.quotation_id)
@ -102,9 +162,9 @@ class SessionCRUD(ISessionCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, [] return ErrorType.DB_RUN_FAILED, []
async def count_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type) -> Tuple[ErrorType, int]: async def count_by_supplier(self, cdb: AsyncSession, supplier_id, status, qt_type, keyword=None, result=None) -> Tuple[ErrorType, int]:
try: try:
conds = self.__filters(supplier_id, status, qt_type) conds = self.__filters(supplier_id, status, qt_type, keyword, result)
query = ( query = (
select(func.count()) select(func.count())
.select_from(sessions) .select_from(sessions)
@ -162,12 +222,58 @@ class SessionCRUD(ISessionCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED return ErrorType.DB_RUN_FAILED
async def update_session_reject(self, cdb: AsyncSession, session_id, status: int, reject_reason: str) -> ErrorType: async def update_session_reject(
self, cdb: AsyncSession, session_id, status: int, reject_reason: str, reject_price: Optional[int] = None,
) -> ErrorType:
try: try:
values = {"status": status, "reject_reason": reject_reason}
# 공급 희망 가격은 선택 입력이라 안 들어올 수 있다 — 그때는 컬럼을 건드리지 않는다.
if reject_price is not None:
values["reject_price"] = reject_price
query = ( query = (
update(sessions) update(sessions)
.where(sessions.session_id == session_id) .where(sessions.session_id == session_id)
.values(status=status, reject_reason=reject_reason) .values(**values)
)
return await DB_SESSION_MNG.add(cdb, query)
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED
async def chain_max_round(self, cdb: AsyncSession, number: str) -> Tuple[ErrorType, int]:
# 같은 견적번호(체인)의 최대 차수. 이미 다음 라운드가 있으면 재협상 요청은 의미가 없다.
try:
query = select(func.max(quotations.round)).where(quotations.number == number, quotations.deleted == False) # noqa: E712
err_type, rows = await DB_SESSION_MNG.execute(cdb, query)
if err_type != ErrorType.SUCCESS:
return err_type, 0
# 단일 컬럼 select 는 scalars() 로 내려와 rows 가 값 리스트다(행 튜플이 아님).
top = rows[0] if rows else None
return ErrorType.SUCCESS, int(top or 0)
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, 0
async def merge_session_custom(self, cdb: AsyncSession, session_id, supplier_id, patch: dict) -> ErrorType:
# sessions.custom 부분 갱신(기존 키 보존). 부가정보와 재협상 요청이 같은 컬럼을 쓰므로 덮어쓰면 안 된다.
try:
query = (
update(sessions)
.where(sessions.session_id == session_id, sessions.supplier_id == supplier_id)
.values(custom=func.coalesce(sessions.custom, cast(text("'{}'"), JSONB)).op("||")(cast(patch, JSONB)))
)
return await DB_SESSION_MNG.add(cdb, query)
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED
async def update_session_custom(self, cdb: AsyncSession, session_id, supplier_id, custom: dict) -> ErrorType:
# 협상완료 부가정보(sessions.custom) 저장. 본인 공급사 세션만(supplier_id 가드).
try:
query = (
update(sessions)
.where(sessions.session_id == session_id, sessions.supplier_id == supplier_id)
.values(custom=custom)
) )
return await DB_SESSION_MNG.add(cdb, query) return await DB_SESSION_MNG.add(cdb, query)
except Exception as ex: except Exception as ex:

View File

@ -5,7 +5,7 @@ from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from common.database.db_session_manager import DB_SESSION_MNG from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import supplier_user_tokens, supplier_users, suppliers from common.database.model.models import supplier_user_tokens, supplier_users, suppliers, companies, sessions
from common.enums import ErrorType, TokenType from common.enums import ErrorType, TokenType
from common.logger import LOG from common.logger import LOG
from common.utils.gtime import GTime from common.utils.gtime import GTime
@ -28,6 +28,14 @@ class IUserCRUD(ABC):
async def get_supplier_name(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, str]: async def get_supplier_name(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, str]:
pass pass
@abstractmethod
async def get_company_settings(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, dict]:
pass
@abstractmethod
async def get_branding_by_session(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, dict]:
pass
@abstractmethod @abstractmethod
async def is_account(self, cdb: AsyncSession, login_id: str) -> ErrorType: async def is_account(self, cdb: AsyncSession, login_id: str) -> ErrorType:
pass pass
@ -117,6 +125,43 @@ class UserCRUD(IUserCRUD):
LOG.e_no_callstack(ex) LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, None return ErrorType.DB_RUN_FAILED, None
async def get_company_settings(self, cdb: AsyncSession, supplier_id) -> Tuple[ErrorType, dict]:
"""공급사 소속 회사 설정(companies.settings) 전체. 브랜딩·협상완료 필드 등이 들어있다. 미설정이면 빈 dict."""
try:
query = (
select(companies.settings)
.join(suppliers, suppliers.company_id == companies.company_id)
.where(suppliers.supplier_id == supplier_id, suppliers.deleted == False, companies.deleted == False) # noqa: E712
.limit(1)
)
err_type, row_list = await DB_SESSION_MNG.execute(cdb, query, f"get_company_settings(supplier_id:{supplier_id}) failed.")
if err_type != ErrorType.SUCCESS:
return err_type, {}
settings = row_list[0] if row_list else None
return ErrorType.SUCCESS, settings or {}
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, {}
async def get_branding_by_session(self, cdb: AsyncSession, session_id) -> Tuple[ErrorType, dict]:
"""세션이 속한 회사의 브랜딩(companies.settings.branding). 로그인 전 화면이 쓰므로 branding 만 꺼낸다."""
try:
query = (
select(companies.settings)
.join(suppliers, suppliers.company_id == companies.company_id)
.join(sessions, sessions.supplier_id == suppliers.supplier_id)
.where(sessions.session_id == session_id, sessions.deleted == False, companies.deleted == False) # noqa: E712
.limit(1)
)
err_type, row_list = await DB_SESSION_MNG.execute(cdb, query, f"get_branding_by_session(session_id:{session_id}) failed.")
if err_type != ErrorType.SUCCESS:
return err_type, {}
settings = (row_list[0] if row_list else None) or {}
return ErrorType.SUCCESS, settings.get("branding") or {}
except Exception as ex:
LOG.e_no_callstack(ex)
return ErrorType.DB_RUN_FAILED, {}
async def is_account(self, cdb: AsyncSession, login_id: str) -> ErrorType: async def is_account(self, cdb: AsyncSession, login_id: str) -> ErrorType:
try: try:
query = ( query = (

View File

@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, Request from fastapi import APIRouter, Depends, Path, Request
from fastapi.security import HTTPAuthorizationCredentials from fastapi.security import HTTPAuthorizationCredentials
from common.models.gmodel import UserInfo from common.models.gmodel import UserInfo
@ -20,6 +20,7 @@ from .protocol import (
Res_Me, Res_Me,
Res_PopupStatus, Res_PopupStatus,
Res_RefreshToken, Res_RefreshToken,
Res_SessionBranding,
) )
# 라우터(MVC 의 컨트롤러). 요청 검증 -> service 호출 -> RemoveNoneResponse 반환만 담당. # 라우터(MVC 의 컨트롤러). 요청 검증 -> service 호출 -> RemoveNoneResponse 반환만 담당.
@ -106,3 +107,16 @@ async def hide_popup(
service: AuthService = Depends(), service: AuthService = Depends(),
): ):
return RemoveNoneResponse(await service.hide_popup(user_info, credentials.credentials, req.popup_type)) return RemoveNoneResponse(await service.hide_popup(user_info, credentials.credentials, req.popup_type))
@router.get(
path="/session-branding/{session_id}",
response_model=Res_SessionBranding,
summary="세션 브랜딩(무인증)",
description="초청 링크로 진입한 로그인 전 화면에서 회사 서비스명·로고·색상만 조회한다. 인증 없이 열려 있으므로 브랜딩 외 정보는 내리지 않는다.",
)
async def session_branding(
session_id: str = Path(..., description="협상 세션 uuid (초청 링크의 session_id)"),
service: AuthService = Depends(),
):
return RemoveNoneResponse(await service.session_branding(session_id))

View File

@ -48,6 +48,9 @@ class Res_Me(Res_WebPacketProtocol):
supplier_id: str = Field("", description="소속 공급사 uuid") supplier_id: str = Field("", description="소속 공급사 uuid")
supplier_name: str = Field("", description="공급사명") supplier_name: str = Field("", description="공급사명")
role: int = Field(0, description="권한 코드 1=user, 2=manager (UserRole)") role: int = Field(0, description="권한 코드 1=user, 2=manager (UserRole)")
branding: dict = Field(default_factory=dict, description="소속 회사 브랜딩(companies.settings.branding). 서비스명/로고/색")
session_fields: list = Field(default_factory=list, description="협상완료 부가정보 필드 정의(companies.settings.session_fields). 공급사가 타결 후 입력")
guide_notices: list = Field(default_factory=list, description="협상 유의사항 항목(companies.settings.guide_notices). 빈 값이면 포털 기본 문구")
class Res_Logout(Res_WebPacketProtocol): class Res_Logout(Res_WebPacketProtocol):
@ -64,3 +67,9 @@ class Req_HidePopup(AuthProtocol):
class Res_HidePopup(Res_WebPacketProtocol): class Res_HidePopup(Res_WebPacketProtocol):
pass pass
class Res_SessionBranding(Res_WebPacketProtocol):
service_name: str = Field("", description="회사 서비스명(companies.settings.branding.service_name). 미설정 시 빈 값")
logo_url: str = Field("", description="회사 로고 URL")
helpdesk: list = Field(default_factory=list, description="헬프데스크 연락처 줄 목록(companies.settings.branding.helpdesk). 한 줄 = 담당자 한 명")

View File

@ -63,6 +63,7 @@ class Res_ChatInit(Res_WebPacketProtocol):
session_id: str = Field("", description="협상 세션 uuid") session_id: str = Field("", description="협상 세션 uuid")
session_status: int = Field(0, description="세션 상태 코드 (SessionStatus: 1=생성 2=진행중 3=완료 4=미참여 5=거부)") session_status: int = Field(0, description="세션 상태 코드 (SessionStatus: 1=생성 2=진행중 3=완료 4=미참여 5=거부)")
quotation_id: str = Field("", description="소속 견적 uuid") quotation_id: str = Field("", description="소속 견적 uuid")
qt_number: str = Field("", description="견적번호(EST-...)")
quotation_end_time: str = Field("", description="견적 마감 시각 (ISO 8601, 타이머용)") quotation_end_time: str = Field("", description="견적 마감 시각 (ISO 8601, 타이머용)")
quotation_memo: str = Field("", description="견적 메모") quotation_memo: str = Field("", description="견적 메모")
item_id: str = Field("", description="상품 uuid") item_id: str = Field("", description="상품 uuid")
@ -77,6 +78,10 @@ class Res_ChatInit(Res_WebPacketProtocol):
item_min_order_quantity: str = Field("", description="최소 주문 수량") item_min_order_quantity: str = Field("", description="최소 주문 수량")
item_vat_yn: Optional[bool] = Field(None, description="VAT 포함 여부(미설정 시 null)") item_vat_yn: Optional[bool] = Field(None, description="VAT 포함 여부(미설정 시 null)")
item_delivery_fee_yn: Optional[bool] = Field(None, description="배송비 포함 여부(미설정 시 null)") item_delivery_fee_yn: Optional[bool] = Field(None, description="배송비 포함 여부(미설정 시 null)")
custom: dict = Field(default_factory=dict, description="협상완료 부가정보 기존 입력값(sessions.custom). 재진입 시 폼 프리필용")
reject_reason: str = Field("", description="협상 거부 시 제출한 사유. 거부 건이 아니면 빈 문자열")
reject_price: Optional[int] = Field(None, description="협상 거부 시 함께 낸 공급 희망 가격(원). 미입력이면 null")
labels: dict = Field(default_factory=dict, description="회사 커스텀 라벨(companies.settings.labels). 상품 상세 필드명(예: lead_time) 치환용. 없으면 프론트 기본값")
# 대화 히스토리(재진입 복원) # 대화 히스토리(재진입 복원)

View File

@ -1,3 +1,5 @@
from typing import Optional
from pydantic import Field from pydantic import Field
from common.models.gmodel import Res_WebPacketProtocol, WebPacketProtocol from common.models.gmodel import Res_WebPacketProtocol, WebPacketProtocol
@ -14,6 +16,14 @@ class ListItem(WebPacketProtocol):
item_name: str = Field("", description="상품명") item_name: str = Field("", description="상품명")
model_name: str = Field("", description="모델명") model_name: str = Field("", description="모델명")
maker_name: str = Field("", description="제조사") maker_name: str = Field("", description="제조사")
custom: dict = Field(default_factory=dict, description="협상완료 부가정보 값(sessions.custom). 미입력이면 빈 dict")
renegotiable: bool = Field(False, description="재협상 요청 가능 여부 — 낙찰 없이 마감(개찰)된 마지막 차수이고 대기 중 요청이 없을 때만 True")
renegotiation_status: int = Field(0, description="현재 재협상 요청 상태(RenegotiationStatus). 요청 이력이 없으면 0")
renegotiation_memo: str = Field("", description="담당자 심사 메모(반려 사유). 없으면 빈 문자열")
result: int = Field(0, description="공급사 관점 협상 결과(SessionResult): 0=미정 1=낙찰 2=미낙찰 3=결렬(개찰, 재협상 대상)")
has_chat: bool = Field(False, description="대화 이력 존재 여부 — 종료된 협상(미참여·거부)의 '결과 보기' 노출 판단용")
reject_reason: str = Field("", description="협상 거부 시 제출한 사유. 거부 건이 아니면 빈 문자열")
reject_price: Optional[int] = Field(None, description="협상 거부 시 함께 낸 공급 희망 가격(원). 미입력이면 null")
class Res_SessionList(Res_WebPacketProtocol): class Res_SessionList(Res_WebPacketProtocol):
@ -29,7 +39,27 @@ class Res_Participate(Res_WebPacketProtocol):
class Req_Reject(WebPacketProtocol): class Req_Reject(WebPacketProtocol):
reject_reason: str = Field("", max_length=255, description="거부 사유 (단종/품절 프리셋 라벨 또는 직접 입력)") reject_reason: str = Field("", max_length=255, description="거부 사유 (단종/품절 프리셋 라벨 또는 직접 입력)")
reject_price: Optional[int] = Field(None, description="공급 희망 가격(원). 선택 입력 — 없으면 컬럼 미변경")
opinion: Optional[str] = Field(None, max_length=255, description="추가 의견 — sessions.custom.opinion 에 병합")
class Res_Reject(Res_WebPacketProtocol): class Res_Reject(Res_WebPacketProtocol):
session_id: str = Field("", description="거부 처리된 세션 uuid") session_id: str = Field("", description="거부 처리된 세션 uuid")
class Req_ExtraInfo(WebPacketProtocol):
custom: dict = Field(default_factory=dict, description="협상완료 부가정보 값 {key: value} (회사 정의 session_fields 대로)")
class Res_ExtraInfo(Res_WebPacketProtocol):
session_id: str = Field("", description="부가정보 저장된 세션 uuid")
class Req_Renegotiation(WebPacketProtocol):
reason: str = Field("", max_length=255, description="재협상 요청 사유(프리셋 라벨 또는 직접 입력)")
desired_price: Optional[int] = Field(None, description="희망 공급가(원). 담당자 판단 근거로만 쓰인다")
class Res_Renegotiation(Res_WebPacketProtocol):
session_id: str = Field("", description="요청이 기록된 세션 uuid")
status: int = Field(0, description="요청 상태(RenegotiationStatus): 1=심사중 2=승인 3=반려 4=철회")

View File

@ -6,7 +6,16 @@ from fastapi.security import HTTPAuthorizationCredentials
from common.models.gmodel import UserInfo from common.models.gmodel import UserInfo
from router.v1.validator.dependencies import IsValidAccessToken, RemoveNoneResponse, security from router.v1.validator.dependencies import IsValidAccessToken, RemoveNoneResponse, security
from services.negotiation_service import NegotiationService from services.negotiation_service import NegotiationService
from .protocol import Req_Reject, Res_Participate, Res_Reject, Res_SessionList from .protocol import (
Req_ExtraInfo,
Req_Reject,
Req_Renegotiation,
Res_ExtraInfo,
Res_Participate,
Res_Reject,
Res_Renegotiation,
Res_SessionList,
)
router = APIRouter(prefix="/v1/negotiation", tags=["Negotiation"], responses={404: {"description": "Not found"}}) router = APIRouter(prefix="/v1/negotiation", tags=["Negotiation"], responses={404: {"description": "Not found"}})
@ -26,9 +35,11 @@ async def list_sessions(
order: Optional[str] = Query(None, description="마감일 전체 정렬: asc(임박순)/desc(여유순). 미지정 시 기본 그룹 정렬('할 일' 우선 → 종료는 하단·최근순). 지정하면 그룹 없이 전체를 마감 기준으로 정렬."), order: Optional[str] = Query(None, description="마감일 전체 정렬: asc(임박순)/desc(여유순). 미지정 시 기본 그룹 정렬('할 일' 우선 → 종료는 하단·최근순). 지정하면 그룹 없이 전체를 마감 기준으로 정렬."),
page: int = Query(1, ge=1, description="페이지 (1부터)"), page: int = Query(1, ge=1, description="페이지 (1부터)"),
page_size: int = Query(20, ge=1, le=100, description="페이지당 건수 (1~100)"), page_size: int = Query(20, ge=1, le=100, description="페이지당 건수 (1~100)"),
keyword: Optional[str] = Query(None, description="검색어 — 견적번호·상품명·상품코드 부분일치(대소문자 무시)"),
result: Optional[int] = Query(None, description="결과 필터(SessionResult): 1=낙찰 2=미낙찰 3=결렬(개찰). 미지정 시 전체"),
): ):
return RemoveNoneResponse( return RemoveNoneResponse(
await service.list_sessions(user_info, credentials.credentials, status, qt_type, order, page, page_size) await service.list_sessions(user_info, credentials.credentials, status, qt_type, order, page, page_size, keyword, result)
) )
@ -51,7 +62,7 @@ async def participate(
path="/sessions/{session_id}/reject", path="/sessions/{session_id}/reject",
response_model=Res_Reject, response_model=Res_Reject,
summary="협상 거부", summary="협상 거부",
description="세션 참여를 거부한다. 소유(공급사)·세션상태(완료/미참여/거부 불가)·견적마감·마감시간 검증 후 협상거부로 전이하고 사유 저장.", description="세션 참여를 거부하거나 진행 중인 협상을 거부한다. 소유(공급사)·세션상태(완료/미참여/거부 불가)·견적마감·마감시간 검증 후 협상거부로 전이하고 사유·공급 희망 가격·의견을 저장.",
) )
async def reject( async def reject(
session_id: str = Path(description="대상 협상 세션 uuid"), session_id: str = Path(description="대상 협상 세션 uuid"),
@ -60,4 +71,59 @@ async def reject(
credentials: HTTPAuthorizationCredentials = Depends(security), credentials: HTTPAuthorizationCredentials = Depends(security),
service: NegotiationService = Depends(), service: NegotiationService = Depends(),
): ):
return RemoveNoneResponse(await service.reject(user_info, credentials.credentials, session_id, req.reject_reason)) return RemoveNoneResponse(
await service.reject(
user_info, credentials.credentials, session_id, req.reject_reason, req.reject_price, req.opinion,
)
)
@router.post(
path="/sessions/{session_id}/extra-info",
response_model=Res_ExtraInfo,
summary="협상완료 부가정보 저장",
description="협상 타결(완료) 세션에 부가정보(표준납기/MOQ/발주배수/배송유형 등, 회사 정의 session_fields)를 저장한다. 본인 공급사의 완료 세션만 허용.",
)
async def save_extra_info(
session_id: str = Path(description="대상 협상 세션 uuid"),
req: Req_ExtraInfo = ...,
user_info: UserInfo = Depends(IsValidAccessToken),
credentials: HTTPAuthorizationCredentials = Depends(security),
service: NegotiationService = Depends(),
):
return RemoveNoneResponse(await service.save_extra_info(user_info, credentials.credentials, session_id, req))
@router.post(
path="/session/{session_id}/renegotiation",
response_model=Res_Renegotiation,
summary="재협상 요청",
description="낙찰 없이 마감된(개찰) 건에 대해 공급사가 재협상을 요청한다. 담당자 승인 시 다음 라운드가 생성된다. 본인 공급사의 마지막 라운드 세션만 허용.",
)
async def request_renegotiation(
req: Req_Renegotiation,
session_id: str = Path(..., description="협상 세션 uuid"),
user_info: UserInfo = Depends(IsValidAccessToken),
credentials: HTTPAuthorizationCredentials = Depends(security),
service: NegotiationService = Depends(),
):
return RemoveNoneResponse(
await service.request_renegotiation(user_info, credentials.credentials, session_id, req)
)
@router.delete(
path="/session/{session_id}/renegotiation",
response_model=Res_Renegotiation,
summary="재협상 요청 철회",
description="심사 대기(PENDING) 중인 본인 요청을 철회한다.",
)
async def cancel_renegotiation(
session_id: str = Path(..., description="협상 세션 uuid"),
user_info: UserInfo = Depends(IsValidAccessToken),
credentials: HTTPAuthorizationCredentials = Depends(security),
service: NegotiationService = Depends(),
):
return RemoveNoneResponse(
await service.cancel_renegotiation(user_info, credentials.credentials, session_id)
)

View File

@ -18,6 +18,7 @@ from router.v1.auth.protocol import (
Res_Me, Res_Me,
Res_PopupStatus, Res_PopupStatus,
Res_RefreshToken, Res_RefreshToken,
Res_SessionBranding,
) )
from router.v1.validator.dependencies import CreateAccessToken, CreateRefreshToken, GetHashedPW, VerifyPW from router.v1.validator.dependencies import CreateAccessToken, CreateRefreshToken, GetHashedPW, VerifyPW
@ -250,6 +251,35 @@ class AuthService:
res.supplier_id = info.supplier_id res.supplier_id = info.supplier_id
res.supplier_name = info.supplier_name res.supplier_name = info.supplier_name
res.role = info.role res.role = info.role
# 소속 회사 설정(companies.settings) — 로고/서비스명(branding) + 협상완료 부가필드(session_fields). 실패해도 기본값.
_e, settings = await DB_SESSION_MNG.execute_lambda(
suppliers.DBType(),
DBWRType.DB_READ.value,
lambda s: self.user_crud.get_company_settings(s, uuid.UUID(info.supplier_id)),
)
settings = settings or {}
res.branding = settings.get("branding") or {}
res.session_fields = settings.get("session_fields") or []
res.guide_notices = settings.get("guide_notices") or []
return res
async def session_branding(self, session_id: str) -> Res_SessionBranding:
"""로그인 전(초청 링크 진입) 화면용 브랜딩. 인증 없이 session_id 로만 조회하며 브랜딩 외 정보는 내리지 않는다."""
res = Res_SessionBranding()
try:
sid = uuid.UUID(session_id)
except ValueError:
res.result.SetResult(ErrorType.INVALID_REQUEST_DATA)
return res
_e, branding = await DB_SESSION_MNG.execute_lambda(
suppliers.DBType(),
DBWRType.DB_READ.value,
lambda s: self.user_crud.get_branding_by_session(s, sid),
)
branding = branding or {}
res.service_name = branding.get("service_name") or ""
res.logo_url = branding.get("logo_url") or ""
res.helpdesk = branding.get("helpdesk") or []
return res return res
async def popup_status(self, user_info: UserInfo, access_token: str) -> Res_PopupStatus: async def popup_status(self, user_info: UserInfo, access_token: str) -> Res_PopupStatus:

View File

@ -24,6 +24,7 @@ from common.logger import LOG
from common.models.gmodel import UserInfo from common.models.gmodel import UserInfo
from crud.chat_crud import ChatCRUD, IChatCRUD from crud.chat_crud import ChatCRUD, IChatCRUD
from crud.session_crud import ISessionCRUD, SessionCRUD from crud.session_crud import ISessionCRUD, SessionCRUD
from crud.user_crud import IUserCRUD, UserCRUD
from router.v1.chat.protocol import ChatMessage, ChatSummary, Res_ChatInit, Res_ChatMessages, Res_ChatSend from router.v1.chat.protocol import ChatMessage, ChatSummary, Res_ChatInit, Res_ChatMessages, Res_ChatSend
from services.agent_client import AgentChatContext, IAgentClient, get_agent_client from services.agent_client import AgentChatContext, IAgentClient, get_agent_client
from services.auth_service import AuthService from services.auth_service import AuthService
@ -44,11 +45,13 @@ class ChatService:
auth: AuthService = Depends(AuthService), auth: AuthService = Depends(AuthService),
session_crud: ISessionCRUD = Depends(SessionCRUD), session_crud: ISessionCRUD = Depends(SessionCRUD),
chat_crud: IChatCRUD = Depends(ChatCRUD), chat_crud: IChatCRUD = Depends(ChatCRUD),
user_crud: IUserCRUD = Depends(UserCRUD),
agent: IAgentClient = Depends(get_agent_client), agent: IAgentClient = Depends(get_agent_client),
): ):
self.auth = auth self.auth = auth
self.session_crud = session_crud self.session_crud = session_crud
self.chat_crud = chat_crud self.chat_crud = chat_crud
self.user_crud = user_crud
self.agent = agent self.agent = agent
# ---- 순수 헬퍼/매퍼 (self 불필요, 상단 집약) ---- # ---- 순수 헬퍼/매퍼 (self 불필요, 상단 집약) ----
@ -59,6 +62,35 @@ class ChatService:
digits = "".join(ch for ch in text if ch.isdigit()) digits = "".join(ch for ch in text if ch.isdigit())
return int(digits) if digits else None return int(digits) if digits else None
@staticmethod
def _parse_reject(text: Optional[str]) -> dict:
"""통일 결렬 폼 제출 문자열 파싱 → {offer_price, reason, opinion}.
형식: '공급희망가격-{원}, 합의불가사유-{사유}, 의견-{의견}' (사유는 '기타-{내용}' 가능).
의견은 자유서술이라 콤마 포함 가능 '의견-' 기준으로 먼저 떼어낸다."""
s = text or ""
opinion = None
if ", 의견-" in s:
s, opinion = s.split(", 의견-", 1)
# 폼이 아닌 자유 입력("협상 포기합니다" 등)은 원문이 곧 사유다. 폼 마커가 없으면 가격도 읽지 않는다
# — 문장에 섞인 숫자를 희망가로 오인해 저장하는 것을 막는다.
if "합의불가사유-" not in s and "공급희망가격-" not in s:
return {
"offer_price": None,
"reason": s.strip()[:255] or None,
"opinion": (opinion.strip() or None) if opinion is not None else None,
}
reason = None
if ", 합의불가사유-" in s:
price_part, reason = s.split(", 합의불가사유-", 1)
else:
price_part = s
price_digits = "".join(ch for ch in price_part.replace("공급희망가격-", "") if ch.isdigit())
return {
"offer_price": int(price_digits) if price_digits else None,
"reason": reason or None,
"opinion": (opinion.strip() or None) if opinion is not None else None,
}
@staticmethod @staticmethod
def _in_price_range(price: int, target_price: Optional[int]) -> bool: def _in_price_range(price: int, target_price: Optional[int]) -> bool:
if not target_price: if not target_price:
@ -103,13 +135,18 @@ class ChatService:
) )
@staticmethod @staticmethod
def _build_bot_chat(sess, seq: int, turn, bot_chat_type: Optional[str] = None, summary: Optional[dict] = None) -> chats: def _build_bot_chat(sess, seq: int, turn, bot_chat_type: Optional[str] = None, summary: Optional[dict] = None, card_uuid=None, card_type=None) -> chats:
# bot_chat_type/summary 도 meta 에 영속화 → 히스토리 복원 시 폼 재현. indicator_value 는 전용 컬럼에도 적재. # bot_chat_type/summary 도 meta 에 영속화 → 히스토리 복원 시 폼 재현. indicator_value 는 전용 컬럼에도 적재.
# nego_card_uuid: turn.card_id(번호)를 UUID 로 변환한 값(nego 카드). 있으면 chats.card_id/card_type/card_used_yn 컬럼에 적재
# → negodata 가 이 컬럼으로 카드 사용/효과를 조인한다. (wild 카드는 agent 가 card_id 미제공 — 별도 작업)
return chats( return chats(
chat_id=uuid.uuid4(), session_id=sess.session_id, seq=seq, chat_id=uuid.uuid4(), session_id=sess.session_id, seq=seq,
sender=ChatSender.BOT.value, sender=ChatSender.BOT.value,
target_price=int(sess.target_price or 0), target_price=int(sess.target_price or 0),
indicator_value=turn.indicator_value, indicator_value=turn.indicator_value,
card_id=card_uuid,
card_type=card_type if card_uuid else None, # CardType: 1=nego, 2=wild
card_used_yn=True if card_uuid else None,
meta={ meta={
"script": turn.script, "step": turn.step, "client_step": turn.client_step, "script": turn.script, "step": turn.step, "client_step": turn.client_step,
"input_mode": turn.input_mode, "input_options": turn.input_options, "input_mode": turn.input_mode, "input_options": turn.input_options,
@ -198,15 +235,15 @@ class ChatService:
) )
sess.status = SessionStatus.NOT_PARTICIPATED.value sess.status = SessionStatus.NOT_PARTICIPATED.value
# 미참여/협상거부 상태는 진입(열람) 불가 (participate/reject 와 동일 규칙). # 미참여/협상거부 세션도 '결과 보기'로 지난 대화를 열람할 수 있다(중간 이탈·거부로 끝난 건).
# 위 마감 변환으로 미참여가 된 세션도 여기서 함께 막힌다. # 대화 재개는 send() 가 협상중(2)만 허용하므로 여기서 막지 않아도 읽기 전용이다.
if sess.status in (SessionStatus.NOT_PARTICIPATED.value, SessionStatus.REJECTED.value):
res.result.SetResult(ErrorType.NEGO_NOT_PARTICIPABLE) await self._ensure_in_progress(sess, quote)
return res
res.session_id = str(sess.session_id) res.session_id = str(sess.session_id)
res.session_status = sess.status res.session_status = sess.status
res.quotation_id = str(sess.quotation_id) res.quotation_id = str(sess.quotation_id)
res.qt_number = sess.qt_number or ""
res.quotation_end_time = quote.end_time.isoformat(timespec="seconds") if quote.end_time else "" res.quotation_end_time = quote.end_time.isoformat(timespec="seconds") if quote.end_time else ""
res.quotation_memo = quote.memo or "" res.quotation_memo = quote.memo or ""
res.item_id = str(item.item_id) res.item_id = str(item.item_id)
@ -221,8 +258,65 @@ class ChatService:
res.item_min_order_quantity = item.moq or "" res.item_min_order_quantity = item.moq or ""
res.item_vat_yn = item.vat_yn res.item_vat_yn = item.vat_yn
res.item_delivery_fee_yn = item.delivery_fee_yn res.item_delivery_fee_yn = item.delivery_fee_yn
res.custom = sess.custom or {}
# 거부로 끝난 세션은 대화에 남지 않는 제출 내역(사유·희망가)을 열람용으로 함께 내린다.
res.reject_reason = sess.reject_reason or ""
res.reject_price = sess.reject_price
# 회사 커스텀 라벨(companies.settings.labels) — 상품 상세 필드명 치환용(예: lead_time→표준납기). 실패해도 빈 dict 폴백.
_e, settings = await DB_SESSION_MNG.execute_lambda(
suppliers.DBType(), DBWRType.DB_READ.value,
lambda s: self.user_crud.get_company_settings(s, sess.supplier_id),
)
res.labels = (settings.get("labels") or {}) if _e == ErrorType.SUCCESS and settings else {}
_hidden = (settings.get("hidden_fields") or []) if _e == ErrorType.SUCCESS and settings else []
_features = (settings.get("features") or {}) if _e == ErrorType.SUCCESS and settings else {}
# VAT 표기 — 부가세 전체 통일 회사(features.vat_mode)는 상품 잔존값과 무관하게 'VAT 별도' 고정(False).
# 상품별 관리 회사가 vat_yn 을 숨겼으면(구 방식) 표기 자체를 생략한다(값 null → 프론트 라벨 생략).
if _features.get("vat_mode") == "unified_excluded":
res.item_vat_yn = False
elif "vat_yn" in _hidden:
res.item_vat_yn = None
# 협상 기준가 — 회사 설정에서 고른 가격 컬럼(features.nego_baseline_field).
# agent 의 인하율 멘트(nego_context_crud._resolve_baseline)와 같은 규칙이어야 화면과 멘트가 어긋나지 않는다.
_baseline = _features.get("nego_baseline_field")
if _baseline not in ("price", "purchase_price"):
# 미설정 회사 폴백 — 공급가를 감췄으면 그 회사는 공급가를 관리하지 않는다는 뜻.
_baseline = "purchase_price" if ("price" in _hidden and "purchase_price" not in _hidden) else "price"
if _baseline == "purchase_price":
res.item_price = item.purchase_price or 0
return res return res
async def _ensure_in_progress(self, sess, quote) -> None:
"""협상생성(1) 세션을 채팅 진입만으로 협상중(2)으로 전이한다(participate 와 동일 전이).
negodata 안내 메일/링크는 목록의 참여 버튼을 거치지 않고 chat 으로 바로 들어오는데,
오프닝 메시지는 협상중일 때만 seed 되므로 전이가 없으면 채팅으로 멈춘다.
마감된 견적은 진입해도 대화가 불가하므로 전이하지 않는다."""
if sess.status != SessionStatus.CREATED.value:
return
if quote is None or quote.status == QuotationStatus.CLOSED.value:
return
end = quote.end_time
if end is not None and end.tzinfo is None:
end = end.replace(tzinfo=timezone.utc)
if end is not None and end < datetime.now(timezone.utc):
return
err_type = await DB_SESSION_MNG.execute_lambda_run(
[sessions.DBType()],
[
lambda s: self.session_crud.update_session_status(s, sess.session_id, SessionStatus.IN_PROGRESS.value),
lambda s: self.session_crud.update_quotation_status(s, sess.quotation_id, QuotationStatus.IN_PROGRESS.value),
],
)
if err_type != ErrorType.SUCCESS:
return
sess.status = SessionStatus.IN_PROGRESS.value
# ---- messages ------------------------------------------------------- # ---- messages -------------------------------------------------------
async def messages(self, user_info: UserInfo, access_token: str, session_id_str: str) -> Res_ChatMessages: async def messages(self, user_info: UserInfo, access_token: str, session_id_str: str) -> Res_ChatMessages:
res = Res_ChatMessages() res = Res_ChatMessages()
@ -239,6 +333,15 @@ class ChatService:
res.result.SetResult(err_type) res.result.SetResult(err_type)
return res return res
# 협상생성 상태로 바로 진입한 경우(메일 링크) 여기서도 전이한다 —
# init 과 병렬로 호출돼 init 의 전이를 못 본 채 읽었을 수 있다.
if not rows and sess.status == SessionStatus.CREATED.value:
_, quote = await DB_SESSION_MNG.execute_lambda(
quotations.DBType(), DBWRType.DB_READ.value,
lambda s: self.session_crud.get_quotation_by_id(s, sess.quotation_id),
)
await self._ensure_in_progress(sess, quote)
# 비어 있고 협상중이면 agent 오프닝 한 턴을 seed (재진입 시 인사 메시지 보존) # 비어 있고 협상중이면 agent 오프닝 한 턴을 seed (재진입 시 인사 메시지 보존)
if not rows and sess.status == SessionStatus.IN_PROGRESS.value: if not rows and sess.status == SessionStatus.IN_PROGRESS.value:
opening = await self._seed_opening(sess) opening = await self._seed_opening(sess)
@ -371,8 +474,43 @@ class ChatService:
# 유저 미입력 가격 타결 케이스 — 마지막 유저 제시가와 다를 수 있다). # 유저 미입력 가격 타결 케이스 — 마지막 유저 제시가와 다를 수 있다).
summary = await self._build_summary(sess, quote, item, final_price, turn.settled_price or last_price) summary = await self._build_summary(sess, quote, item, final_price, turn.settled_price or last_price)
# 카드 번호(turn.card_id) → UUID 변환. 번호 정본 표기(NGC-/WC- prefix)로 종류를 가르고,
# prefix 없는 구번호는 step 휴리스틱 폴백. 1차 조회가 비면 반대 테이블 재조회 —
# 종결 전술의 와일드카드는 step 이 '가격협상_카운터'(wild 미시작)라 step 만으론 카드가
# 영영 null 로 남았다(사용 카드 통계·화면 누락 원인).
# 카드 사용 로그(chats.card_id/type/used)를 negodata 조인용으로 남긴다. (1% 인하 시스템 카드는 agent 가 card_id 미제공)
card_uuid = None
card_type = None
if turn.card_id:
number = str(turn.card_id)
if number.startswith("WC"):
wild_first = True
elif number.startswith("NGC"):
wild_first = False
else:
wild_first = bool(turn.step and turn.step.startswith("wild"))
async def _lookup(wild: bool):
if wild:
found = await DB_SESSION_MNG.execute_lambda(
chats.DBType(), DBWRType.DB_READ.value,
lambda s: self.chat_crud.get_wild_card_id_by_number(s, number),
)
return found, 2
found = await DB_SESSION_MNG.execute_lambda(
chats.DBType(), DBWRType.DB_READ.value,
lambda s: self.chat_crud.get_nego_card_id_by_number(s, number),
)
return found, 1
card_uuid, card_type = await _lookup(wild_first)
if card_uuid is None:
card_uuid, card_type = await _lookup(not wild_first)
if card_uuid is None:
card_type = None
# 봇 메시지 + 종료 시 확정(성공=DONE+입찰가 / 실패=REJECTED+거부사유·제시가). 한 트랜잭션. # 봇 메시지 + 종료 시 확정(성공=DONE+입찰가 / 실패=REJECTED+거부사유·제시가). 한 트랜잭션.
bot_msg = self._build_bot_chat(sess, seq=max_seq + 2, turn=turn, bot_chat_type=bot_chat_type, summary=summary) bot_msg = self._build_bot_chat(sess, seq=max_seq + 2, turn=turn, bot_chat_type=bot_chat_type, summary=summary, card_uuid=card_uuid, card_type=card_type)
funcs = [lambda s: self.chat_crud.insert_message(s, bot_msg)] funcs = [lambda s: self.chat_crud.insert_message(s, bot_msg)]
# 가격 입력 턴 → 마지막 제시가를 봇 메시지 저장과 같은 트랜잭션으로 갱신. # 가격 입력 턴 → 마지막 제시가를 봇 메시지 저장과 같은 트랜잭션으로 갱신.
# 앵커링 표본 판정의 "가격 흔적"(가격을 써낸 협상만 집계 — 중간 이탈해도 실패로 측정 가능). # 앵커링 표본 판정의 "가격 흔적"(가격을 써낸 협상만 집계 — 중간 이탈해도 실패로 측정 가능).
@ -392,10 +530,15 @@ class ChatService:
funcs.append(lambda s: self.chat_crud.finalize_session(s, sess.session_id, new_status, bid_price=bid)) funcs.append(lambda s: self.chat_crud.finalize_session(s, sess.session_id, new_status, bid_price=bid))
else: else:
new_status = SessionStatus.REJECTED.value new_status = SessionStatus.REJECTED.value
parsed = self._parse_reject(user_input)
funcs.append(lambda s: self.chat_crud.finalize_session( funcs.append(lambda s: self.chat_crud.finalize_session(
s, sess.session_id, new_status, s, sess.session_id, new_status,
reject_reason=(user_input or None), reject_price=price, reject_reason=parsed["reason"], reject_price=parsed["offer_price"],
)) ))
if parsed["opinion"]:
funcs.append(lambda s, op=parsed["opinion"]: self.session_crud.merge_session_custom(
s, sess.session_id, sess.supplier_id, {"opinion": op},
))
err_type = await DB_SESSION_MNG.execute_lambda_run([chats.DBType()], funcs) err_type = await DB_SESSION_MNG.execute_lambda_run([chats.DBType()], funcs)
if err_type != ErrorType.SUCCESS: if err_type != ErrorType.SUCCESS:
@ -505,7 +648,17 @@ class ChatService:
# 배송형태: 재견적(CM)의 '배송형태선택' 단계에서 공급사가 고른 라벨. 재협상엔 단계가 없어 None. # 배송형태: 재견적(CM)의 '배송형태선택' 단계에서 공급사가 고른 라벨. 재협상엔 단계가 없어 None.
delivery_label = await self._delivery_choice(sess) if sess.qt_type == 2 else None delivery_label = await self._delivery_choice(sess) if sess.qt_type == 2 else None
# 상품 기본 배송유형(코드→라벨). 선택값이 없으면 표시에 폴백으로 쓸 수 있다. # 상품 기본 배송유형(코드→라벨). 선택값이 없으면 표시에 폴백으로 쓸 수 있다.
item_delivery_label = DeliveryType.label_of(item.delivery_type) if item and item.delivery_type is not None else "" # 회사가 배송유형 보기를 자기 용어로 바꿨으면(settings.labels['delivery_type.N']) 그 단어를 쓴다 —
# 협상 중 공급사가 고른 보기와 요약 표기가 갈리지 않도록.
item_delivery_label = ""
if item and item.delivery_type is not None:
_e2, _settings = await DB_SESSION_MNG.execute_lambda(
suppliers.DBType(), DBWRType.DB_READ.value,
lambda s: self.user_crud.get_company_settings(s, sess.supplier_id),
)
_labels = (_settings.get("labels") or {}) if _e2 == ErrorType.SUCCESS and _settings else {}
item_delivery_label = (_labels.get(f"delivery_type.{item.delivery_type}")
or DeliveryType.label_of(item.delivery_type))
def _iso(dt): def _iso(dt):
if dt is None: if dt is None:

View File

@ -1,14 +1,35 @@
import uuid import uuid
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Optional
from fastapi import Depends from fastapi import Depends
from common.database.db_session_manager import DB_SESSION_MNG from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import sessions from common.database.model.models import chats, notifications, sessions
from common.enums import DBWRType, ErrorType, QuotationStatus, SessionStatus from common.enums import (
CloseReason,
DBWRType,
ErrorType,
NotificationType,
QuotationStatus,
RENEGOTIABLE_CLOSE_REASONS,
RenegotiationStatus,
SessionStatus,
)
from common.logger import LOG
from common.models.gmodel import UserInfo from common.models.gmodel import UserInfo
from crud.chat_crud import ChatCRUD, IChatCRUD
from crud.session_crud import ISessionCRUD, SessionCRUD from crud.session_crud import ISessionCRUD, SessionCRUD
from router.v1.negotiation.protocol import ListItem, Res_Participate, Res_Reject, Res_SessionList from router.v1.negotiation.protocol import (
ListItem,
Req_ExtraInfo,
Req_Renegotiation,
Res_ExtraInfo,
Res_Participate,
Res_Reject,
Res_Renegotiation,
Res_SessionList,
)
from services.auth_service import AuthService from services.auth_service import AuthService
@ -18,11 +39,20 @@ class NegotiationService:
- 목록은 로그인 유저의 supplier_id 로만 조회한다. - 목록은 로그인 유저의 supplier_id 로만 조회한다.
""" """
def __init__(self, auth: AuthService = Depends(AuthService), session_crud: ISessionCRUD = Depends(SessionCRUD)): # 부가정보 입력 폼을 띄우는 요약 말풍선 종류. 이 말풍선이 나온 뒤면 협상은 타결된 것으로 본다.
_SUMMARY_BOT_TYPES = ("summaryRSP", "summaryCM")
def __init__(
self,
auth: AuthService = Depends(AuthService),
session_crud: ISessionCRUD = Depends(SessionCRUD),
chat_crud: IChatCRUD = Depends(ChatCRUD),
):
self.auth = auth self.auth = auth
self.session_crud = session_crud self.session_crud = session_crud
self.chat_crud = chat_crud
async def list_sessions(self, user_info: UserInfo, access_token: str, status, qt_type, order: str, page: int, page_size: int) -> Res_SessionList: async def list_sessions(self, user_info: UserInfo, access_token: str, status, qt_type, order: str, page: int, page_size: int, keyword: str = None, result: int = None) -> Res_SessionList:
res = Res_SessionList() res = Res_SessionList()
# 1) 인증 (활성 + 저장된 access 토큰 대조) # 1) 인증 (활성 + 저장된 access 토큰 대조)
@ -38,7 +68,7 @@ class NegotiationService:
err_type, rows = await DB_SESSION_MNG.execute_lambda( err_type, rows = await DB_SESSION_MNG.execute_lambda(
sessions.DBType(), sessions.DBType(),
DBWRType.DB_READ.value, DBWRType.DB_READ.value,
lambda s: self.session_crud.list_by_supplier(s, supplier_id, status, qt_type, order, offset, page_size), lambda s: self.session_crud.list_by_supplier(s, supplier_id, status, qt_type, order, offset, page_size, keyword, result),
) )
if err_type != ErrorType.SUCCESS: if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type) res.result.SetResult(err_type)
@ -48,31 +78,274 @@ class NegotiationService:
err_type, total = await DB_SESSION_MNG.execute_lambda( err_type, total = await DB_SESSION_MNG.execute_lambda(
sessions.DBType(), sessions.DBType(),
DBWRType.DB_READ.value, DBWRType.DB_READ.value,
lambda s: self.session_crud.count_by_supplier(s, supplier_id, status, qt_type), lambda s: self.session_crud.count_by_supplier(s, supplier_id, status, qt_type, keyword, result),
) )
if err_type != ErrorType.SUCCESS: if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type) res.result.SetResult(err_type)
return res return res
res.items = [ # 같은 견적번호(체인)의 최대 차수 — 이미 다음 라운드가 있으면 재협상 요청 대상이 아니다.
ListItem( max_rounds: dict = {}
session_id=str(r[0]), for number in {r[3] for r in rows if r[3]}:
session_status=r[1], _e, mx = await DB_SESSION_MNG.execute_lambda(
qt_type=r[2], sessions.DBType(), DBWRType.DB_READ.value,
qt_number=r[3], lambda s, n=number: self.session_crud.chain_max_round(s, n),
qt_end_time=r[4].isoformat(timespec="seconds") if r[4] else "",
item_code=r[5] or "",
item_name=r[6] or "",
model_name=r[7] or "",
maker_name=r[8] or "",
) )
for r in rows max_rounds[number] = mx or 0
]
res.items = [self._to_list_item(r, max_rounds) for r in rows]
res.total = total res.total = total
res.page = page res.page = page
res.page_size = page_size res.page_size = page_size
return res return res
async def save_extra_info(self, user_info: UserInfo, access_token: str, session_id_str: str, req: Req_ExtraInfo) -> Res_ExtraInfo:
"""협상완료(타결) 부가정보 저장. 견적 마감 여부와 무관하게, 본인 공급사의 '협상완료' 세션에만 허용.
_load_actionable_session 견적마감·마감시간을 막으므로(타결 후엔 마감됐을 있음) 쓰지 않고 직접 검증한다.
"""
res = Res_ExtraInfo()
# 1) 인증
err_type, info = await self.auth.authenticate(user_info, access_token)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
try:
session_id = uuid.UUID(session_id_str)
except (ValueError, TypeError):
res.result.SetResult(ErrorType.NEGO_NOT_FOUND)
return res
# 2) 세션 조회 + 소유 검증
err_type, sess = await DB_SESSION_MNG.execute_lambda(
sessions.DBType(), DBWRType.DB_READ.value,
lambda s: self.session_crud.get_session_by_id(s, session_id),
)
if err_type != ErrorType.SUCCESS or sess is None:
res.result.SetResult(ErrorType.NEGO_NOT_FOUND)
return res
if str(sess.supplier_id) != info.supplier_id:
res.result.SetResult(ErrorType.NEGO_FORBIDDEN)
return res
# 3) 협상완료(타결) 세션만 부가정보 입력 허용.
# 단 '협상완료' 요약 말풍선은 chat_end=false 라 세션이 아직 협상중(2)이다
# (동의 → '협상종료' 턴에서야 완료로 전이). 폼은 요약 시점에 뜨므로 그 구간도 허용한다.
if sess.status != SessionStatus.DONE.value:
if sess.status != SessionStatus.IN_PROGRESS.value or not await self._is_after_summary(sess.session_id):
res.result.SetResult(ErrorType.NEGO_NOT_PARTICIPABLE)
return res
# 4) 저장(supplier_id 가드 crud)
err_type = await DB_SESSION_MNG.execute_lambda_run(
[sessions.DBType()],
[lambda s: self.session_crud.update_session_custom(s, session_id, uuid.UUID(info.supplier_id), req.custom or {})],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
res.session_id = str(session_id)
return res
@staticmethod
def _to_list_item(r, max_rounds: dict) -> ListItem:
"""세션 행 → 목록 아이템. 재협상 요청 가능 여부는 서버가 판정해 내려준다(프론트가 규칙을 몰라도 되게)."""
custom = r[9] or {}
renego = custom.get("renegotiation") or {}
status = renego.get("status") or 0
is_last_round = (r[12] or 0) >= max_rounds.get(r[3], 0)
renegotiable = (
r[10] == QuotationStatus.CLOSED.value
and r[11] in RENEGOTIABLE_CLOSE_REASONS
and is_last_round
and status
not in (
RenegotiationStatus.PENDING.value,
RenegotiationStatus.APPROVED.value,
RenegotiationStatus.REJECTED.value,
)
)
return ListItem(
session_id=str(r[0]),
session_status=r[1],
qt_type=r[2],
qt_number=r[3],
qt_end_time=r[4].isoformat(timespec="seconds") if r[4] else "",
item_code=r[5] or "",
item_name=r[6] or "",
model_name=r[7] or "",
maker_name=r[8] or "",
custom=custom,
renegotiable=renegotiable,
renegotiation_status=status,
renegotiation_memo=renego.get("memo") or "",
result=NegotiationService._to_result(r[10], r[11], r[13], r[14]),
has_chat=bool(r[15]),
reject_reason=r[16] or "",
reject_price=r[17],
)
@staticmethod
def _to_result(qt_status, close_reason, winner_id, my_id) -> int:
"""공급사 관점 협상 결과(SessionResult). 견적 마감 전이면 0(미정).
낙찰 건은 낙찰자가 나면 1(낙찰)·아니면 2(미낙찰), 개찰(OPEN_*) 마감은 3(결렬=재협상 대상)."""
if qt_status != QuotationStatus.CLOSED.value:
return 0
if close_reason == CloseReason.AWARDED.value:
return 1 if winner_id is not None and str(winner_id) == str(my_id) else 2
if close_reason in RENEGOTIABLE_CLOSE_REASONS:
return 3
return 0
async def request_renegotiation(
self, user_info: UserInfo, access_token: str, session_id_str: str, req: Req_Renegotiation
) -> Res_Renegotiation:
"""결렬(개찰) 마감 건에 대해 공급사가 재협상을 요청한다(IMK #15).
전용 테이블 없이 sessions.custom.renegotiation 기록하고, 견적 작성자에게 알림을 남긴다."""
res = Res_Renegotiation()
err_type, info, sess, quote = await self._load_renegotiable(user_info, access_token, session_id_str)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
# 심사 대기·승인·반려 건은 재요청을 막는다(전용 테이블이 없어 유니크 대신 여기서 검증).
# 반려는 담당자가 이미 판단한 결과라 같은 건으로 다시 올릴 수 없다. 철회(CANCELED)만 재요청 허용.
current = (sess.custom or {}).get("renegotiation") or {}
if current.get("status") in (
RenegotiationStatus.PENDING.value,
RenegotiationStatus.APPROVED.value,
RenegotiationStatus.REJECTED.value,
):
res.result.SetResult(ErrorType.NEGO_NOT_PARTICIPABLE)
return res
payload = {
"status": RenegotiationStatus.PENDING.value,
"reason": (req.reason or "").strip(),
"desired_price": req.desired_price,
"requested_at": datetime.now(timezone.utc).isoformat(),
}
err_type = await DB_SESSION_MNG.execute_lambda_run(
[sessions.DBType()],
[lambda s: self.session_crud.merge_session_custom(
s, sess.session_id, uuid.UUID(info.supplier_id), {"renegotiation": payload}
)],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
await self._notify_renegotiation(quote, sess, info, payload)
res.session_id = str(sess.session_id)
res.status = RenegotiationStatus.PENDING.value
return res
async def cancel_renegotiation(self, user_info: UserInfo, access_token: str, session_id_str: str) -> Res_Renegotiation:
"""공급사가 자기 요청을 철회한다. 심사 대기(PENDING) 중에만 가능."""
res = Res_Renegotiation()
err_type, info, sess, _quote = await self._load_renegotiable(user_info, access_token, session_id_str)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
current = (sess.custom or {}).get("renegotiation") or {}
if current.get("status") != RenegotiationStatus.PENDING.value:
res.result.SetResult(ErrorType.NEGO_NOT_PARTICIPABLE)
return res
patch = {**current, "status": RenegotiationStatus.CANCELED.value}
err_type = await DB_SESSION_MNG.execute_lambda_run(
[sessions.DBType()],
[lambda s: self.session_crud.merge_session_custom(
s, sess.session_id, uuid.UUID(info.supplier_id), {"renegotiation": patch}
)],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
res.session_id = str(sess.session_id)
res.status = RenegotiationStatus.CANCELED.value
return res
async def _load_renegotiable(self, user_info: UserInfo, access_token: str, session_id_str: str):
"""재협상 요청 자격 검증 — 인증 → 본인 세션 → 결렬(개찰) 마감 → 마지막 라운드.
성공 (SUCCESS, info, sess, quote)."""
err_type, info = await self.auth.authenticate(user_info, access_token)
if err_type != ErrorType.SUCCESS:
return err_type, None, None, None
try:
session_id = uuid.UUID(session_id_str)
except (ValueError, TypeError):
return ErrorType.NEGO_NOT_FOUND, None, None, None
err_type, sess = await DB_SESSION_MNG.execute_lambda(
sessions.DBType(), DBWRType.DB_READ.value,
lambda s: self.session_crud.get_session_by_id(s, session_id),
)
if err_type != ErrorType.SUCCESS or sess is None:
return ErrorType.NEGO_NOT_FOUND, None, None, None
if str(sess.supplier_id) != info.supplier_id:
return ErrorType.NEGO_FORBIDDEN, None, None, None
err_type, quote = await DB_SESSION_MNG.execute_lambda(
sessions.DBType(), DBWRType.DB_READ.value,
lambda s: self.session_crud.get_quotation_by_id(s, sess.quotation_id),
)
if err_type != ErrorType.SUCCESS or quote is None:
return ErrorType.NEGO_NOT_FOUND, None, None, None
# 낙찰됐거나 아직 진행 중인 건은 요청 대상이 아니다.
if quote.status != QuotationStatus.CLOSED.value or quote.close_reason not in RENEGOTIABLE_CLOSE_REASONS:
return ErrorType.NEGO_NOT_PARTICIPABLE, None, None, None
# 이미 다음 라운드가 만들어졌으면 요청할 이유가 없다.
_e, max_round = await DB_SESSION_MNG.execute_lambda(
sessions.DBType(), DBWRType.DB_READ.value,
lambda s: self.session_crud.chain_max_round(s, quote.number),
)
if max_round and quote.round < max_round:
return ErrorType.NEGO_NOT_PARTICIPABLE, None, None, None
return ErrorType.SUCCESS, info, sess, quote
async def _notify_renegotiation(self, quote, sess, info, payload: dict) -> None:
"""견적 작성자 인박스에 재협상 요청 알림을 남긴다. 부가 효과라 실패해도 본 흐름을 막지 않는다."""
notif = notifications(
user_id=quote.user_id,
type=NotificationType.RENEGO_REQUESTED.value,
ref_qt_id=quote.qt_id,
ref_session_id=sess.session_id,
data={
"supplier_name": info.supplier_name,
"qt_number": quote.number,
"qt_round": quote.round,
"reason": payload.get("reason"),
"desired_price": payload.get("desired_price"),
},
)
err = await DB_SESSION_MNG.execute_lambda_run(
[notifications.DBType()],
[lambda s: DB_SESSION_MNG.insert(s, notif, raise_error=False)],
)
if err != ErrorType.SUCCESS:
LOG.e_no_callstack(f"[renego] 알림 기록 실패 qt={quote.qt_id} session={sess.session_id}")
async def _is_after_summary(self, session_id) -> bool:
"""마지막 말풍선이 타결 요약(summaryRSP/CM)인지 — 즉 협상이 타결된 뒤인지."""
err_type, (_, _, last_meta) = await DB_SESSION_MNG.execute_lambda(
chats.DBType(), DBWRType.DB_READ.value,
lambda s: self.chat_crud.get_last(s, session_id),
)
if err_type != ErrorType.SUCCESS or not last_meta:
return False
return last_meta.get("bot_chat_type") in self._SUMMARY_BOT_TYPES
async def _load_actionable_session(self, user_info: UserInfo, access_token: str, session_id_str: str, blocked_statuses: tuple): async def _load_actionable_session(self, user_info: UserInfo, access_token: str, session_id_str: str, blocked_statuses: tuple):
"""참여/거부 공통 전처리: 인증 → 세션/견적 로드 → 소유·상태·견적마감·마감시간 검증. """참여/거부 공통 전처리: 인증 → 세션/견적 로드 → 소유·상태·견적마감·마감시간 검증.
성공 (SUCCESS, sess, quote), 실패 (err_type, None, None) 반환한다. 성공 (SUCCESS, sess, quote), 실패 (err_type, None, None) 반환한다.
@ -165,7 +438,10 @@ class NegotiationService:
res.session_id = str(sess.session_id) res.session_id = str(sess.session_id)
return res return res
async def reject(self, user_info: UserInfo, access_token: str, session_id_str: str, reject_reason: str) -> Res_Reject: async def reject(
self, user_info: UserInfo, access_token: str, session_id_str: str, reject_reason: str,
reject_price: Optional[int] = None, opinion: Optional[str] = None,
) -> Res_Reject:
res = Res_Reject() res = Res_Reject()
# 거부 사유 필수 # 거부 사유 필수
@ -185,11 +461,19 @@ class NegotiationService:
res.result.SetResult(err_type) res.result.SetResult(err_type)
return res return res
# 거부 처리 — 세션을 협상거부로 전이하고 사유 저장 # 거부 처리 — 세션을 협상거부로 전이하고 사유·공급 희망 가격 저장.
err_type = await DB_SESSION_MNG.execute_lambda_run( # 의견은 부가정보와 같은 custom 컬럼이라 병합(덮어쓰기 금지) — 채팅 결렬 폼과 같은 자리.
[sessions.DBType()], funcs = [
[lambda s: self.session_crud.update_session_reject(s, sess.session_id, SessionStatus.REJECTED.value, reason)], lambda s: self.session_crud.update_session_reject(
) s, sess.session_id, SessionStatus.REJECTED.value, reason, reject_price,
)
]
note = (opinion or "").strip()[:255]
if note:
funcs.append(
lambda s: self.session_crud.merge_session_custom(s, sess.session_id, sess.supplier_id, {"opinion": note})
)
err_type = await DB_SESSION_MNG.execute_lambda_run([sessions.DBType()], funcs)
if err_type != ErrorType.SUCCESS: if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type) res.result.SetResult(err_type)
return res return res

View File

@ -220,12 +220,58 @@ async def test_chat_init_returns_meta(client, chat_seed):
assert body["quotation_end_time"] # 타이머용 마감 시각 assert body["quotation_end_time"] # 타이머용 마감 시각
async def test_chat_init_returns_reject_detail(client, chat_seed, db_engine):
"""검증: 협상 거부로 끝난 세션에 재진입('결과 보기')했을 때의 init 응답.
기대결과: 대화에 남지 않는 제출 내역(reject_reason·reject_price) 실려 열람 카드를 그릴 있다."""
token = await _login_token(client)
sid = chat_seed["sids"]["P"]
await client.post(
f"/v1/negotiation/sessions/{sid}/reject",
headers={"Authorization": f"Bearer {token}"},
json={"reject_reason": "단종", "reject_price": 91000, "opinion": "후속 모델로 제안 가능합니다"},
)
body = (await _init(client, token, sid)).json()
assert body["session_status"] == 5
assert body["reject_reason"] == "단종"
assert body["reject_price"] == 91000
assert body["custom"]["opinion"] == "후속 모델로 제안 가능합니다"
async def test_chat_init_forbidden_other_supplier(client, chat_seed): async def test_chat_init_forbidden_other_supplier(client, chat_seed):
token = await _login_token(client) token = await _login_token(client)
body = (await _init(client, token, chat_seed["sids"]["X"])).json() body = (await _init(client, token, chat_seed["sids"]["X"])).json()
assert body["result"]["code"] == 1300 # NEGO_FORBIDDEN assert body["result"]["code"] == 1300 # NEGO_FORBIDDEN
async def test_chat_init_vat_mode_unified_shows_excluded(client, chat_seed, db_engine):
"""검증: 부가세 전체 통일 회사(features.vat_mode=unified_excluded)의 세션 채팅 init.
기대결과: 상품에 vat_yn=true 잔존값이 있어도 item_vat_yn=False 프론트가 'VAT별도' 고정 표기."""
import json
company_id = uuid.uuid4()
async with db_engine.begin() as conn:
await conn.execute(
text("INSERT INTO company.companies (company_id, name, status, settings) VALUES (:c, :n, 1, CAST(:s AS JSONB))"),
{"c": company_id, "n": f"{MARK}VAT통일사", "s": json.dumps({"features": {"vat_mode": "unified_excluded"}})},
)
await conn.execute(
text("UPDATE partner.suppliers SET company_id = :c WHERE supplier_id = :sid"),
{"c": company_id, "sid": chat_seed["supplier_id"]},
)
await conn.execute(
text("UPDATE partner.items SET vat_yn = true WHERE item_id = (SELECT item_id FROM negotiation.sessions WHERE session_id = :s)"),
{"s": chat_seed["sids"]["P"]},
)
try:
token = await _login_token(client)
body = (await _init(client, token, chat_seed["sids"]["P"])).json()
assert body["result"]["success"] is True
assert body["item_vat_yn"] is False
finally:
async with db_engine.begin() as conn:
await conn.execute(text("DELETE FROM company.companies WHERE company_id = :c"), {"c": company_id})
# ---- messages (오프닝 seed) ------------------------------------------------- # ---- messages (오프닝 seed) -------------------------------------------------
async def test_messages_seeds_opening(client, chat_seed): async def test_messages_seeds_opening(client, chat_seed):
token = await _login_token(client) token = await _login_token(client)
@ -351,23 +397,28 @@ async def test_send_blocked_when_prev_turn_pending(client, chat_seed, db_engine)
async def test_init_marks_expired_created_as_not_participated(client, chat_seed, db_engine): async def test_init_marks_expired_created_as_not_participated(client, chat_seed, db_engine):
"""검증: 마감시간이 지난 협상생성 세션으로 채팅 진입.
기대결과: DB 상태가 미참여(4) 정리되고, init 자체는 열람용으로 성공한다."""
token = await _login_token(client) token = await _login_token(client)
sid, qid = chat_seed["sids"]["C"], chat_seed["qids"]["C"] # 협상생성(1) sid, qid = chat_seed["sids"]["C"], chat_seed["qids"]["C"] # 협상생성(1)
async with db_engine.begin() as conn: async with db_engine.begin() as conn:
await conn.execute(text("UPDATE quotation.quotations SET end_time = now() - make_interval(hours => 1) WHERE qt_id = :qid"), {"qid": qid}) await conn.execute(text("UPDATE quotation.quotations SET end_time = now() - make_interval(hours => 1) WHERE qt_id = :qid"), {"qid": qid})
body = (await _init(client, token, sid)).json() body = (await _init(client, token, sid)).json()
# 마감된 협상생성은 DB 상 미참여로 정리되고, 미참여는 진입 불가라 init 은 에러로 막는다. assert body["result"]["success"] is True
assert body["result"]["code"] == 1301 # NEGO_NOT_PARTICIPABLE assert body["session_status"] == 4
assert await _session_status(db_engine, sid) == 4 # DB 는 미참여로 전이됨 assert await _session_status(db_engine, sid) == 4 # DB 도 미참여로 전이
async def test_init_blocks_rejected_session(client, chat_seed, db_engine): async def test_init_allows_viewing_rejected_session(client, chat_seed, db_engine):
"""검증: 협상거부(5)로 끝난 세션에 '결과 보기'로 재진입.
기대결과: init 성공(열람 허용) 대화 재개는 send 협상중만 허용해 막는다."""
token = await _login_token(client) token = await _login_token(client)
sid = chat_seed["sids"]["P"] sid = chat_seed["sids"]["P"]
async with db_engine.begin() as conn: async with db_engine.begin() as conn:
await conn.execute(text("UPDATE negotiation.sessions SET status = 5 WHERE session_id = :sid"), {"sid": sid}) # 협상거부 await conn.execute(text("UPDATE negotiation.sessions SET status = 5 WHERE session_id = :sid"), {"sid": sid}) # 협상거부
body = (await _init(client, token, sid)).json() body = (await _init(client, token, sid)).json()
assert body["result"]["code"] == 1301 # NEGO_NOT_PARTICIPABLE — 거부 세션 진입 차단 assert body["result"]["success"] is True and body["session_status"] == 5
assert (await _send(client, token, sid, "")).json()["result"]["code"] == 1400 # CHAT_NOT_IN_PROGRESS
# ---- 순수 헬퍼 단위 테스트 (DB 불필요, ChatService @staticmethod) ---------- # ---- 순수 헬퍼 단위 테스트 (DB 불필요, ChatService @staticmethod) ----------

View File

@ -16,6 +16,35 @@ TEST_SUPPLIER_NAME = "파이테스트협상공급사"
MARK = "PYTESTNEGO-" # 시드 식별용 prefix (item code / qt number) MARK = "PYTESTNEGO-" # 시드 식별용 prefix (item code / qt number)
async def _seed_case(conn, code, sess_st, qt_type, hrs, quote_st, sup):
"""상품·견적·세션 1세트 시드. 코드/견적번호에 MARK prefix 를 달아 cleanup 이 함께 지운다.
hrs 마감(quotation.end_time)까지의 시간 음수면 이미 마감시간이 지난 . 반환: (session_id, qt_id)."""
item_id, qt_id, session_id = uuid.uuid4(), uuid.uuid4(), uuid.uuid4()
await conn.execute(
text(
"INSERT INTO partner.items (item_id, company_id, user_id, name, code, model_name, manufacturer) "
"VALUES (:iid, gen_random_uuid(), gen_random_uuid(), :name, :code, :model, '테스트제조사')"
),
{"iid": item_id, "name": f"상품 {code}", "code": f"{MARK}{code}", "model": f"MODEL-{code}"},
)
await conn.execute(
text(
"INSERT INTO quotation.quotations (qt_id, user_id, qt_setting_id, version_id, name, number, type, status, start_time, end_time) "
"VALUES (:qid, gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), :name, :num, :tp, :st, now(), now() + make_interval(hours => :hrs))"
),
{"qid": qt_id, "name": f"견적 {code}", "num": f"{MARK}{code}", "tp": qt_type, "st": quote_st, "hrs": hrs},
)
await conn.execute(
text(
"INSERT INTO negotiation.sessions "
"(session_id, quotation_id, item_id, supplier_id, qt_number, qt_round, qt_type, target_price, status, end_time) "
"VALUES (:sesid, :qid, :iid, :sup, :qtn, 1, :qtt, 100000, :st, now())"
),
{"sesid": session_id, "qid": qt_id, "iid": item_id, "sup": sup, "qtn": f"{MARK}{code}", "qtt": qt_type, "st": sess_st},
)
return session_id, qt_id
@pytest_asyncio.fixture @pytest_asyncio.fixture
async def nego_seed(db_engine): async def nego_seed(db_engine):
"""공급사 + 유저 + 세션/견적 3건(본인) + 1건(타 공급사) 시드. 세션/견적 id 를 반환.""" """공급사 + 유저 + 세션/견적 3건(본인) + 1건(타 공급사) 시드. 세션/견적 id 를 반환."""
@ -33,6 +62,11 @@ async def nego_seed(db_engine):
sids, qids = {}, {} sids, qids = {}, {}
async def _cleanup(conn): async def _cleanup(conn):
# 대화는 세션보다 먼저 지운다(세션이 사라지면 대상을 못 고른다).
await conn.execute(text(
f"DELETE FROM negotiation.chats WHERE session_id IN "
f"(SELECT session_id FROM negotiation.sessions WHERE qt_number LIKE '{MARK}%')"
))
await conn.execute(text(f"DELETE FROM negotiation.sessions WHERE qt_number LIKE '{MARK}%'")) await conn.execute(text(f"DELETE FROM negotiation.sessions WHERE qt_number LIKE '{MARK}%'"))
await conn.execute(text(f"DELETE FROM quotation.quotations WHERE number LIKE '{MARK}%'")) await conn.execute(text(f"DELETE FROM quotation.quotations WHERE number LIKE '{MARK}%'"))
await conn.execute(text(f"DELETE FROM partner.items WHERE code LIKE '{MARK}%'")) await conn.execute(text(f"DELETE FROM partner.items WHERE code LIKE '{MARK}%'"))
@ -52,31 +86,9 @@ async def nego_seed(db_engine):
), ),
{"sid": supplier_id, "id": TEST_LOGIN_ID, "pw": pw_hash}, {"sid": supplier_id, "id": TEST_LOGIN_ID, "pw": pw_hash},
) )
for code, sess_st, qt_type, hrs, quote_st, sup in specs: for spec in specs:
item_id, qt_id, session_id = uuid.uuid4(), uuid.uuid4(), uuid.uuid4() code = spec[0]
sids[code], qids[code] = session_id, qt_id sids[code], qids[code] = await _seed_case(conn, *spec)
await conn.execute(
text(
"INSERT INTO partner.items (item_id, company_id, user_id, name, code, model_name, manufacturer) "
"VALUES (:iid, gen_random_uuid(), gen_random_uuid(), :name, :code, :model, '테스트제조사')"
),
{"iid": item_id, "name": f"상품 {code}", "code": f"{MARK}{code}", "model": f"MODEL-{code}"},
)
await conn.execute(
text(
"INSERT INTO quotation.quotations (qt_id, user_id, qt_setting_id, version_id, name, number, type, status, start_time, end_time) "
"VALUES (:qid, gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), :name, :num, :tp, :st, now(), now() + make_interval(hours => :hrs))"
),
{"qid": qt_id, "name": f"견적 {code}", "num": f"{MARK}{code}", "tp": qt_type, "st": quote_st, "hrs": hrs},
)
await conn.execute(
text(
"INSERT INTO negotiation.sessions "
"(session_id, quotation_id, item_id, supplier_id, qt_number, qt_round, qt_type, target_price, status, end_time) "
"VALUES (:sesid, :qid, :iid, :sup, :qtn, 1, :qtt, 100000, :st, now())"
),
{"sesid": session_id, "qid": qt_id, "iid": item_id, "sup": sup, "qtn": f"{MARK}{code}", "qtt": qt_type, "st": sess_st},
)
yield {"supplier_id": supplier_id, "sids": sids, "qids": qids} yield {"supplier_id": supplier_id, "sids": sids, "qids": qids}
@ -141,6 +153,63 @@ async def test_list_filter_status(client, nego_seed):
assert body["total"] == 1 and body["items"][0]["item_code"] == f"{MARK}B" assert body["total"] == 1 and body["items"][0]["item_code"] == f"{MARK}B"
async def test_list_shows_closed_quotation_created_session_as_not_participated(client, db_engine, nego_seed):
"""검증: 견적이 마감(3)된 뒤에도 세션이 협상생성(1)으로 남아 있는 건(마감 일괄정리 이후 생성 등).
기대결과: 목록 상태는 미참여(4) '협상 대기' 새지 않고, status=1 필터에서도 빠지고 status=4 필터에 잡힌다."""
async with db_engine.begin() as conn:
await _seed_case(conn, "CLOSED1", 1, 2, -1, 3, nego_seed["supplier_id"])
token = await _login_token(client)
listed = next(i for i in (await _list(client, token)).json()["items"] if i["item_code"] == f"{MARK}CLOSED1")
assert listed["session_status"] == 4
waiting = (await _list(client, token, status=1)).json()
assert waiting["total"] == 1 and {i["item_code"] for i in waiting["items"]} == {f"{MARK}A"}
assert f"{MARK}CLOSED1" in {i["item_code"] for i in (await _list(client, token, status=4)).json()["items"]}
async def test_list_shows_deadline_passed_created_session_as_not_participated(client, db_engine, nego_seed):
"""검증: 견적은 아직 진행중(2)인데 마감시간(end_time)만 지난 협상생성 세션.
기대결과: 미참여(4) 참여/입장이 막히는 건이라 목록도 같은 상태로 보인다(DB 값은 그대로)."""
async with db_engine.begin() as conn:
session_id, _ = await _seed_case(conn, "OVERDUE", 1, 2, -3, 2, nego_seed["supplier_id"])
token = await _login_token(client)
listed = next(i for i in (await _list(client, token)).json()["items"] if i["item_code"] == f"{MARK}OVERDUE")
assert listed["session_status"] == 4
assert await _session_status(db_engine, session_id) == 1 # 목록은 파생 표시만, 쓰기는 하지 않는다
async def test_list_marks_stale_round_not_renegotiable(client, db_engine, nego_seed):
"""검증: 개찰(결렬) 마감된 1차 견적에 2차가 이미 생성돼 있는 체인.
기대결과: renegotiable False 다음 라운드가 있으면 재협상 요청 대상이 아니다(체인 최대 차수 판정)."""
async with db_engine.begin() as conn:
await _seed_case(conn, "CHAIN", 3, 2, -2, 3, nego_seed["supplier_id"])
await conn.execute(text(
f"UPDATE quotation.quotations SET close_reason = 5 WHERE number = '{MARK}CHAIN'"))
# 같은 견적번호의 2차 — 번호가 같아야 체인으로 묶인다.
await conn.execute(text(
"INSERT INTO quotation.quotations (qt_id, user_id, qt_setting_id, version_id, name, number, type, status, round, start_time, end_time) "
f"VALUES (gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), '견적 CHAIN 2차', '{MARK}CHAIN', 2, 2, 2, now(), now() + make_interval(hours => 2))"))
token = await _login_token(client)
listed = next(i for i in (await _list(client, token)).json()["items"] if i["item_code"] == f"{MARK}CHAIN")
assert listed["result"] == 3 and listed["renegotiable"] is False
async def test_list_has_chat_flags_sessions_with_history(client, db_engine, nego_seed):
"""검증: 대화 이력이 있는 세션과 없는 세션의 has_chat.
기대결과: 이력 있는 건만 True 종료 건의 '결과 보기' 노출이 값으로 갈린다."""
async with db_engine.begin() as conn:
await conn.execute(
text("INSERT INTO negotiation.chats (session_id, seq, sender, target_price) VALUES (:sid, 1, 1, 0)"),
{"sid": nego_seed["sids"]["C"]},
)
token = await _login_token(client)
by_code = {i["item_code"]: i["has_chat"] for i in (await _list(client, token)).json()["items"]}
assert by_code[f"{MARK}C"] is True
assert by_code[f"{MARK}A"] is False
async def test_list_filter_qt_type(client, nego_seed): async def test_list_filter_qt_type(client, nego_seed):
token = await _login_token(client) token = await _login_token(client)
body = (await _list(client, token, qt_type=2)).json() body = (await _list(client, token, qt_type=2)).json()
@ -177,6 +246,56 @@ async def test_list_requires_auth(client):
assert (await client.get("/v1/negotiation/sessions")).status_code in (401, 403) assert (await client.get("/v1/negotiation/sessions")).status_code in (401, 403)
# ---- 검색(keyword) ----------------------------------------------------------
async def test_search_by_qt_number_and_item_code(client, nego_seed):
"""검증: 견적번호/상품코드가 같은 값(PYTESTNEGO-B)으로 검색.
기대결과: B 1건만, total 1(카운트도 같은 필터 적용)."""
token = await _login_token(client)
body = (await _list(client, token, keyword=f"{MARK}B")).json()
assert body["total"] == 1
assert [i["item_code"] for i in body["items"]] == [f"{MARK}B"]
async def test_search_by_item_name(client, nego_seed):
"""검증: 상품명 일부('상품 A')로 검색.
기대결과: A 1건만."""
token = await _login_token(client)
body = (await _list(client, token, keyword="상품 A")).json()
assert {i["item_code"] for i in body["items"]} == {f"{MARK}A"}
async def test_search_prefix_matches_all_own(client, nego_seed):
"""검증: 공통 prefix(PYTESTNEGO)로 검색.
기대결과: 본인 공급사 3 전부( 공급사 X 제외 유지)."""
token = await _login_token(client)
body = (await _list(client, token, keyword=MARK.rstrip("-"))).json()
assert body["total"] == 3
async def test_search_case_insensitive(client, nego_seed):
"""검증: 소문자로 검색(pytestnego-c).
기대결과: ILIKE 대소문자 무시하고 C 매칭."""
token = await _login_token(client)
body = (await _list(client, token, keyword=f"{MARK}c".lower())).json()
assert {i["item_code"] for i in body["items"]} == {f"{MARK}C"}
async def test_search_no_match_returns_empty(client, nego_seed):
"""검증: 어디에도 없는 검색어.
기대결과: 0, total 0."""
token = await _login_token(client)
body = (await _list(client, token, keyword="존재하지않는검색어zzz")).json()
assert body["total"] == 0 and body["items"] == []
async def test_search_wildcard_is_escaped(client, nego_seed):
"""검증: ILIKE 와일드카드('%')를 그대로 검색 — 패턴으로 새면 전건 매칭될 위험.
기대결과: escape 되어 리터럴 '%' 취급 매칭 0."""
token = await _login_token(client)
body = (await _list(client, token, keyword="%")).json()
assert body["total"] == 0
# ---- 참여 ------------------------------------------------------------------- # ---- 참여 -------------------------------------------------------------------
async def test_participate_success(client, nego_seed, db_engine): async def test_participate_success(client, nego_seed, db_engine):
token = await _login_token(client) token = await _login_token(client)
@ -266,6 +385,65 @@ async def test_reject_success(client, nego_seed, db_engine):
assert status == 5 and reason == "단종 상품입니다" # REJECTED + 사유 저장 assert status == 5 and reason == "단종 상품입니다" # REJECTED + 사유 저장
async def test_reject_with_price_and_opinion(client, nego_seed, db_engine):
# 채팅 내 협상 거부 경로 — 사유 외에 공급 희망 가격과 의견까지 함께 남긴다.
token = await _login_token(client)
sid = nego_seed["sids"]["B"]
r = await client.post(
f"/v1/negotiation/sessions/{sid}/reject",
headers={"Authorization": f"Bearer {token}"},
json={"reject_reason": "품절", "reject_price": 88000, "opinion": "대체품으로 재견적 부탁드립니다"},
)
assert r.json()["result"]["success"] is True
async with db_engine.begin() as conn:
row = (await conn.execute(
text("SELECT status, reject_reason, reject_price, custom FROM negotiation.sessions WHERE session_id = :sid"),
{"sid": sid},
)).first()
assert row.status == 5 and row.reject_reason == "품절"
assert row.reject_price == 88000
assert row.custom["opinion"] == "대체품으로 재견적 부탁드립니다"
async def test_reject_without_price_keeps_null(client, nego_seed, db_engine):
# 목록 거부 경로 — 가격이 없으면 reject_price 를 건드리지 않는다.
token = await _login_token(client)
sid = nego_seed["sids"]["B"]
r = await _reject(client, token, sid, "단종")
assert r.json()["result"]["success"] is True
async with db_engine.begin() as conn:
row = (await conn.execute(
text("SELECT reject_price, custom FROM negotiation.sessions WHERE session_id = :sid"),
{"sid": sid},
)).first()
assert row.reject_price is None and row.custom is None
async def test_list_returns_reject_detail(client, nego_seed):
# 거부 제출 내역은 대화에 남지 않는다 — 목록이 사유·희망가를 실어야 '거부 내역'을 열람할 수 있다.
token = await _login_token(client)
sid = nego_seed["sids"]["B"]
await client.post(
f"/v1/negotiation/sessions/{sid}/reject",
headers={"Authorization": f"Bearer {token}"},
json={"reject_reason": "품절", "reject_price": 77000, "opinion": "재고 확보 후 연락드리겠습니다"},
)
items = (await _list(client, token)).json()["items"]
row = next(i for i in items if i["session_id"] == str(sid))
assert row["reject_reason"] == "품절"
assert row["reject_price"] == 77000
assert row["custom"]["opinion"] == "재고 확보 후 연락드리겠습니다"
async def test_list_reject_detail_empty_for_active(client, nego_seed):
# 거부 건이 아니면 빈 값 — 프론트가 '거부 내역' 버튼 노출을 상태로만 판단하므로 값이 새면 안 된다.
token = await _login_token(client)
items = (await _list(client, token)).json()["items"]
row = next(i for i in items if i["session_id"] == str(nego_seed["sids"]["A"]))
assert row["reject_reason"] == ""
assert row.get("reject_price") is None
async def test_reject_empty_reason(client, nego_seed): async def test_reject_empty_reason(client, nego_seed):
token = await _login_token(client) token = await _login_token(client)
r = await _reject(client, token, nego_seed["sids"]["B"], " ") # 공백만 → 사유 없음 r = await _reject(client, token, nego_seed["sids"]["B"], " ") # 공백만 → 사유 없음
@ -294,3 +472,76 @@ async def test_reject_requires_auth(client, nego_seed):
sid = nego_seed["sids"]["B"] sid = nego_seed["sids"]["B"]
r = await client.post(f"/v1/negotiation/sessions/{sid}/reject", json={"reject_reason": "사유"}) r = await client.post(f"/v1/negotiation/sessions/{sid}/reject", json={"reject_reason": "사유"})
assert r.status_code in (401, 403) assert r.status_code in (401, 403)
# ---- 결과 필터(result) ------------------------------------------------------
# 마감(CLOSED) + 마감사유/낙찰자로 낙찰(1)·미낙찰(2)·결렬(3)을 만들고 result= 로 거른다.
# nego_seed 의 공급사/로그인을 재사용하고, MARK prefix 라 픽스처 teardown 이 함께 정리한다.
async def _seed_result_row(engine, *, supplier_id, code, close_reason, winner_id):
import uuid as _uuid
item_id, qt_id, session_id = _uuid.uuid4(), _uuid.uuid4(), _uuid.uuid4()
async with engine.begin() as conn:
await conn.execute(
text("INSERT INTO partner.items (item_id, company_id, user_id, name, code, model_name, manufacturer) "
"VALUES (:iid, gen_random_uuid(), gen_random_uuid(), :name, :code, 'M', '제조사')"),
{"iid": item_id, "name": f"상품 {code}", "code": f"{MARK}{code}"},
)
await conn.execute(
text("INSERT INTO quotation.quotations "
"(qt_id, user_id, qt_setting_id, version_id, name, number, type, status, close_reason, "
" preferred_sp_id, round, start_time, end_time) VALUES "
"(:qid, gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), :name, :num, 2, 3, :cr, "
" :win, 1, now() - make_interval(hours => 2), now() - make_interval(hours => 1))"),
{"qid": qt_id, "name": f"견적 {code}", "num": f"{MARK}{code}", "cr": close_reason, "win": winner_id},
)
await conn.execute(
text("INSERT INTO negotiation.sessions "
"(session_id, quotation_id, item_id, supplier_id, qt_number, qt_round, qt_type, "
" target_price, status, bid_price, end_time) VALUES "
"(:sid, :qid, :iid, :sup, :num, 1, 2, 100000, 3, 95000, now() - make_interval(hours => 1))"),
{"sid": session_id, "qid": qt_id, "iid": item_id, "sup": supplier_id, "num": f"{MARK}{code}"},
)
@pytest_asyncio.fixture
async def result_rows(nego_seed, db_engine):
"""nego_seed 공급사에 낙찰/미낙찰/결렬 각 1건을 추가한다(개찰 5=OPEN_PRICE, 1=AWARDED)."""
sup = nego_seed["supplier_id"]
await _seed_result_row(db_engine, supplier_id=sup, code="RWON", close_reason=1, winner_id=sup) # 낙찰(나)
await _seed_result_row(db_engine, supplier_id=sup, code="RLOST", close_reason=1, winner_id=uuid.uuid4()) # 미낙찰(남)
await _seed_result_row(db_engine, supplier_id=sup, code="ROPEN", close_reason=5, winner_id=None) # 결렬(개찰)
return nego_seed
async def test_result_filter_won(client, result_rows):
"""검증: result=1(낙찰)로 필터. 기대결과: 낙찰 건만, total=1."""
token = await _login_token(client)
body = (await _list(client, token, result=1)).json()
assert body["total"] == 1
assert body["items"][0]["item_code"] == f"{MARK}RWON"
assert body["items"][0]["result"] == 1
async def test_result_filter_lost(client, result_rows):
"""검증: result=2(미낙찰)로 필터. 기대결과: 미낙찰 건만."""
token = await _login_token(client)
body = (await _list(client, token, result=2)).json()
assert {i["item_code"] for i in body["items"]} == {f"{MARK}RLOST"}
assert body["items"][0]["result"] == 2
async def test_result_filter_open(client, result_rows):
"""검증: result=3(결렬)로 필터. 기대결과: 개찰 결렬 건만 + 재협상 대상(renegotiable=True)."""
token = await _login_token(client)
body = (await _list(client, token, result=3)).json()
assert {i["item_code"] for i in body["items"]} == {f"{MARK}ROPEN"}
assert body["items"][0]["result"] == 3
assert body["items"][0]["renegotiable"] is True
async def test_result_filter_composes_with_paging(client, result_rows):
"""검증: 결과 필터가 total(페이징)에 반영. 기대결과: result=1 이면 total=1(전체 목록과 별개)."""
token = await _login_token(client)
all_total = (await _list(client, token)).json()["total"]
won_total = (await _list(client, token, result=1)).json()["total"]
assert won_total == 1 and all_total > won_total

View File

@ -0,0 +1,215 @@
"""공급사 재협상 요청/철회(IMK #15) 포털 e2e — 요청 접수 + 철회.
담당자 심사(승인/반려) negodata 백엔드 몫이고, 여기(포털) 공급사가
sessions.custom.renegotiation 요청을 남기고(PENDING) 스스로 철회(CANCELED)하는 절반을 본다:
· 개찰(OPEN_*) 마감 + 본인 마지막 라운드 세션 요청 기록(PENDING) + 담당자 알림
· 낙찰(AWARDED) 요청 거부
· 남의 공급사 세션 거부(FORBIDDEN)
· 이미 대기 중인데 재요청 거부(중복 방지)
· 대기 철회 CANCELED, 이후 재요청 허용
dev negosium_db 그대로 쓰므로(APP_ENV=local) 전용 테스트 행만 시드하고 끝나면 지운다.
"""
import uuid
import bcrypt
import pytest_asyncio
from sqlalchemy import text
from common.enums import CloseReason, QuotationStatus, RenegotiationStatus, SessionStatus
TEST_LOGIN_ID = "pytest_renego_user"
TEST_PW = "pytest1234"
TEST_SUPPLIER_NAME = "파이테스트재협상공급사"
MARK = "PYTESTRENEGO-" # 시드 식별용 prefix (item code / qt number)
@pytest_asyncio.fixture
async def renego_seed(db_engine):
"""공급사 + 로그인유저 + 재협상 후보 세션들을 시드하고 (supplier_id, sids, uids) 반환.
(code, quotation.status, close_reason, 소속 공급사) 요청 자격은 견적 마감사유·소유로 갈린다.
"""
supplier_id = uuid.uuid4()
other_supplier_id = uuid.uuid4()
pw_hash = bcrypt.hashpw(TEST_PW.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
specs = [
("OPEN", QuotationStatus.CLOSED.value, CloseReason.OPEN_PRICE.value, supplier_id), # 개찰 → 요청 가능
("AWARD", QuotationStatus.CLOSED.value, CloseReason.AWARDED.value, supplier_id), # 낙찰 → 불가
("OTHER", QuotationStatus.CLOSED.value, CloseReason.OPEN_PRICE.value, other_supplier_id), # 남의 공급사
]
sids, uids = {}, {}
async def _cleanup(conn):
await conn.execute(text(f"DELETE FROM negotiation.sessions WHERE qt_number LIKE '{MARK}%'"))
await conn.execute(text(f"DELETE FROM company.notifications WHERE ref_qt_id IN "
f"(SELECT qt_id FROM quotation.quotations WHERE number LIKE '{MARK}%')"))
await conn.execute(text(f"DELETE FROM quotation.quotations WHERE number LIKE '{MARK}%'"))
await conn.execute(text(f"DELETE FROM partner.items WHERE code LIKE '{MARK}%'"))
await conn.execute(text("DELETE FROM supplier.supplier_users WHERE id = :id"), {"id": TEST_LOGIN_ID})
await conn.execute(text("DELETE FROM partner.suppliers WHERE name = :n"), {"n": TEST_SUPPLIER_NAME})
async with db_engine.begin() as conn:
await _cleanup(conn)
await conn.execute(
text("INSERT INTO partner.suppliers (supplier_id, company_id, user_id, name) "
"VALUES (:sid, gen_random_uuid(), gen_random_uuid(), :name)"),
{"sid": supplier_id, "name": TEST_SUPPLIER_NAME},
)
await conn.execute(
text("INSERT INTO supplier.supplier_users (supplier_id, id, password, name, last_accessed_at, status, role) "
"VALUES (:sid, :id, :pw, '협상담당자', now(), 1, 1)"),
{"sid": supplier_id, "id": TEST_LOGIN_ID, "pw": pw_hash},
)
for code, quote_st, close_reason, sup in specs:
item_id, qt_id, session_id, user_id = uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), uuid.uuid4()
sids[code], uids[code] = session_id, user_id
await conn.execute(
text("INSERT INTO partner.items (item_id, company_id, user_id, name, code, model_name, manufacturer) "
"VALUES (:iid, gen_random_uuid(), gen_random_uuid(), :name, :code, :model, '테스트제조사')"),
{"iid": item_id, "name": f"상품 {code}", "code": f"{MARK}{code}", "model": f"MODEL-{code}"},
)
await conn.execute(
text("INSERT INTO quotation.quotations "
"(qt_id, user_id, qt_setting_id, version_id, name, number, type, status, close_reason, "
" round, start_time, end_time) VALUES "
"(:qid, :uid, gen_random_uuid(), gen_random_uuid(), :name, :num, 2, :st, :cr, "
" 1, now() - make_interval(hours => 2), now() - make_interval(hours => 1))"),
{"qid": qt_id, "uid": user_id, "name": f"견적 {code}", "num": f"{MARK}{code}", "st": quote_st, "cr": close_reason},
)
await conn.execute(
text("INSERT INTO negotiation.sessions "
"(session_id, quotation_id, item_id, supplier_id, qt_number, qt_round, qt_type, "
" target_price, status, bid_price, end_time) VALUES "
"(:sesid, :qid, :iid, :sup, :qtn, 1, 2, 100000, :sst, 95000, now() - make_interval(hours => 1))"),
{"sesid": session_id, "qid": qt_id, "iid": item_id, "sup": sup, "qtn": f"{MARK}{code}", "sst": SessionStatus.DONE.value},
)
yield {"supplier_id": supplier_id, "sids": sids, "uids": uids}
async with db_engine.begin() as conn:
await _cleanup(conn)
async def _login_token(client):
r = await client.post("/v1/auth/login", json={"id": TEST_LOGIN_ID, "pw": TEST_PW})
return r.json()["access_token"]
async def _request(client, token, session_id, *, reason="가격 재검토", desired_price=90000):
return await client.post(
f"/v1/negotiation/session/{session_id}/renegotiation",
headers={"Authorization": f"Bearer {token}"},
json={"reason": reason, "desired_price": desired_price},
)
async def _cancel(client, token, session_id):
return await client.delete(
f"/v1/negotiation/session/{session_id}/renegotiation",
headers={"Authorization": f"Bearer {token}"},
)
async def _renego(db_engine, session_id):
async with db_engine.begin() as conn:
row = (await conn.execute(
text("SELECT custom FROM negotiation.sessions WHERE session_id = :sid"),
{"sid": session_id},
)).scalar()
return (row or {}).get("renegotiation") or {}
async def _notif_count(db_engine, qt_number):
async with db_engine.begin() as conn:
return (await conn.execute(
text("SELECT count(*) FROM company.notifications WHERE ref_qt_id IN "
"(SELECT qt_id FROM quotation.quotations WHERE number = :num)"),
{"num": qt_number},
)).scalar()
# ---- 요청 -------------------------------------------------------------------
async def test_request_records_pending(client, renego_seed, db_engine):
"""검증: 개찰(OPEN_PRICE) 마감 + 본인 마지막 라운드 세션에 재협상 요청.
기대결과: success + PENDING 기록(사유·희망가 저장) + 담당자 알림 1."""
token = await _login_token(client)
sid = renego_seed["sids"]["OPEN"]
body = (await _request(client, token, sid, reason="원자재 인상 반영", desired_price=88000)).json()
assert body["result"]["success"] is True
assert body["status"] == RenegotiationStatus.PENDING.value
saved = await _renego(db_engine, sid)
assert saved["status"] == RenegotiationStatus.PENDING.value
assert saved["reason"] == "원자재 인상 반영"
assert saved["desired_price"] == 88000
assert await _notif_count(db_engine, f"{MARK}OPEN") == 1
async def test_request_twice_blocked(client, renego_seed, db_engine):
"""검증: 이미 대기(PENDING) 요청이 있는 세션에 다시 요청.
기대결과: 2번째는 거부(중복 방지) + 상태는 여전히 PENDING 1."""
token = await _login_token(client)
sid = renego_seed["sids"]["OPEN"]
first = (await _request(client, token, sid)).json()
second = (await _request(client, token, sid)).json()
assert first["result"]["success"] is True
assert second["result"]["success"] is False
assert (await _renego(db_engine, sid))["status"] == RenegotiationStatus.PENDING.value
async def test_request_blocked_on_awarded(client, renego_seed, db_engine):
"""검증: 낙찰(AWARDED)로 마감된 건에 재협상 요청.
기대결과: 거부(낙찰 건은 재협상 불가) + custom.renegotiation 미기록."""
token = await _login_token(client)
sid = renego_seed["sids"]["AWARD"]
body = (await _request(client, token, sid)).json()
assert body["result"]["success"] is False
assert await _renego(db_engine, sid) == {}
async def test_request_forbidden_other_supplier(client, renego_seed, db_engine):
"""검증: 다른 공급사 소유 세션에 재협상 요청.
기대결과: 거부 + custom.renegotiation 미기록(소유 가드)."""
token = await _login_token(client)
sid = renego_seed["sids"]["OTHER"]
body = (await _request(client, token, sid)).json()
assert body["result"]["success"] is False
assert await _renego(db_engine, sid) == {}
# ---- 철회 -------------------------------------------------------------------
async def test_cancel_sets_canceled_and_allows_rerequest(client, renego_seed, db_engine):
"""검증: 대기 중 요청을 철회한 뒤 다시 요청.
기대결과: 철회 CANCELED 재요청 다시 PENDING(철회 건은 재요청 허용)."""
token = await _login_token(client)
sid = renego_seed["sids"]["OPEN"]
await _request(client, token, sid)
cancelled = (await _cancel(client, token, sid)).json()
assert cancelled["result"]["success"] is True
assert cancelled["status"] == RenegotiationStatus.CANCELED.value
assert (await _renego(db_engine, sid))["status"] == RenegotiationStatus.CANCELED.value
again = (await _request(client, token, sid)).json()
assert again["result"]["success"] is True
assert (await _renego(db_engine, sid))["status"] == RenegotiationStatus.PENDING.value
async def test_cancel_requires_pending(client, renego_seed, db_engine):
"""검증: 대기 요청이 없는 세션에 철회 시도.
기대결과: 거부(철회할 대기 요청 없음)."""
token = await _login_token(client)
sid = renego_seed["sids"]["OPEN"]
body = (await _cancel(client, token, sid)).json()
assert body["result"]["success"] is False

View File

@ -0,0 +1,51 @@
"""공급사 관점 협상 결과 파생(SessionResult) 단위 테스트.
목록의 result 코드는 견적 마감상태·마감사유·낙찰자로 파생한다(DDL 무변경). 공급사가 배지로
'내가 낙찰인지 / 결렬이라 재협상 요청 대상인지' 구분한다. 결렬(3) renegotiable 짝을 이룬다.
"""
import uuid
from common.enums import CloseReason, QuotationStatus
from services.negotiation_service import NegotiationService
_R = NegotiationService._to_result
ME = uuid.uuid4()
OTHER = uuid.uuid4()
CLOSED = QuotationStatus.CLOSED.value
def test_result_undecided_before_close():
"""검증: 견적이 아직 마감 전(진행중)이면 결과 미정.
기대결과: 0(미정)."""
assert _R(QuotationStatus.IN_PROGRESS.value, None, None, ME) == 0
def test_result_won_when_winner_is_me():
"""검증: 낙찰(AWARDED) 마감 + 낙찰자가 나.
기대결과: 1(낙찰)."""
assert _R(CLOSED, CloseReason.AWARDED.value, ME, ME) == 1
def test_result_lost_when_winner_is_other():
"""검증: 낙찰 마감이지만 낙찰자가 남.
기대결과: 2(미낙찰)."""
assert _R(CLOSED, CloseReason.AWARDED.value, OTHER, ME) == 2
def test_result_lost_when_awarded_without_winner_id():
"""검증: 낙찰인데 낙찰자 id 가 비어 나와 대조 불가.
기대결과: 2(미낙찰) 낙찰이라 단정 하면 낙찰로 오인시키지 않는다."""
assert _R(CLOSED, CloseReason.AWARDED.value, None, ME) == 2
def test_result_open_is_renegotiable():
"""검증: 개찰(OPEN_*) 4종으로 마감(낙찰자 미정=결렬).
기대결과: 전부 3(결렬) 재협상 요청 대상."""
for cr in (CloseReason.OPEN_PRICE, CloseReason.OPEN_EQUAL, CloseReason.OPEN_NOSHOW, CloseReason.OPEN_REJECT):
assert _R(CLOSED, cr.value, None, ME) == 3, cr
def test_result_none_when_closed_without_reason():
"""검증: 마감됐지만 close_reason 이 아직 없음(경계).
기대결과: 0(미정) 낙찰/결렬 어느 쪽도 아님."""
assert _R(CLOSED, None, None, ME) == 0

BIN
dev-settings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

Some files were not shown because too many files have changed in this diff Show More