Files
trex-core/docs/srs/Data/req-006_postgresql-slot-persistence.md
T
genichinandClaude Opus 4.8 6e8e79660c feat(slot): slot entry_count + closed-slot reallocation (dcy-019, BAT-025) + REL-005
dcy-019/issue #18: slot 자원을 진입 횟수 기준으로 고르게 재사용하고 idle 소진 시
closed 슬롯을 직접 재할당한다. 거버넌스는 전부 change-req(신규 REQ 없음).

- 도메인(REQ-002): Slot.entry_count(기본 0) + EXPECTED_SLOT_FIELDS, 상태 머신
  closed→pending_buy 신규 전이(base 8→9, ALLOWED_TRANSITIONS 14→15),
  _apply_transition_fields closed→pending_buy clean-slate + →pending_buy 시 +1
- 영속화(REQ-006): 멱등 마이그레이션 008(entry_count), flat 매핑,
  find_allocatable_slot(idle∪closed, entry_count→updated_at→slot_id) + in-memory 동등
- admission(REQ-007): _admit_buy find_idle_slot→find_allocatable_slot + from_state=slot.status
- 테스트(REQ-003): 전이 카운트/계약 가드 정합 + test_slot_entry_count.py / test_buy_admission_reallocation.py
- 문서(REQ-009): data-model.md / runtime-flows.md 정합
- 기존 daily_symbol_entries(symbol-scoped) 분리·미변경

BAT-025(release-candidate) delivery + change-req REQ-002/003/006/007/009(CHG-20260609-01).
REL-005(confirmed, app-service): BAT-024+BAT-025 묶음. operator-facing run-the-app
스모크로 enter --forced-close-at(BAT-024)·closed 재할당(BAT-025) 실 데몬 검증
(실 PG 마이그레이션 008 round-trip 포함).

검증: 단위 366 passed / 35 skipped(KIS sandbox 1건 deselect — live .env 환경 의존), ruff clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-09 14:49:22 +09:00

8.8 KiB
Raw Blame History

req-006: PostgreSQL SlotRepository 영속화 + 스키마

  • Status: Implemented
  • Type: Data
  • Priority: High
  • Owner: project maintainers

Intent

  • 코어가 daily_slot 을 durable 단일 writer 로 소유하도록 SlotRepository 의 PostgreSQL 구현과 스키마를 추가한다. dcy-001 부록 A 컬럼 계약을 영속화에서 실제로 충족시킨다.

Requirement

  • SlotRepository Protocol 의 psycopg 기반 PostgreSQL 구현을 추가하고, trex-core 소유의 신규 마이그레이션으로 daily_slot(부록 A 16컬럼)·daily_symbol_entries·slot_transition_log·intent_log 테이블을 생성한다. advisory lock 을 실제 pg_advisory_xact_lock 으로 구현한다. EXPECTED_SLOT_FIELDS ↔ daily_slot 실제 컬럼(information_schema) 드리프트 테스트를 동반한다.

Acceptance Criteria

  1. Given PostgreSQL 백엔드, When 마이그레이션 적용, Then daily_slot·daily_symbol_entries·slot_transition_log·intent_log 가 생성되고 daily_slot 컬럼이 부록 A(16컬럼)와 일치한다.
  2. Given PostgreSQL SlotRepository, When SlotService 를 이 repo 로 구동, Then 도메인 단위/통합테스트가 in-memory 와 동일하게 통과한다(전이/재진입+1/race 감지).
  3. Given 드리프트 테스트, When EXPECTED_SLOT_FIELDS ↔ information_schema 대조, Then 일치하고 컬럼 누락 시 실패한다(dcy-001 OQ-2 DB-side 검사 실현).
  4. Given 상태 변경, When transit_slot, Then pg_advisory_xact_lock 하 단일 트랜잭션으로 직렬화되고 lock 보유 중 외부 I/O 가 없다.

Impacted Area

  • Docs: docs/data-model.md(테이블/계약), dcy-001 부록 A(기준)
  • Modules: trex_core/ 하위 persistence 어댑터(psycopg) + 마이그레이션 SQL; 도메인 패키지는 무의존 유지(NFR-1)
  • Tests: PostgreSQL repo 통합테스트 + 드리프트 테스트

Notes

  • 스키마는 부록 A 단일 기준, orbit-v2 DDL 참조(dcy-002 OQ-4). daily_slot_archive/prune 는 범위 밖.
  • NFR-1(도메인 순수성) 일부 흡수: psycopg 의존은 persistence 어댑터에만, 도메인 import 스윕 0 유지.
  • REQ-005(런타임) 선행 권장, REQ-007/008(API)의 단일 writer 근거.
  • 출처: dcy-002 FR-5.

Change Log

CHG-20260603-01

  • Date: 2026-06-03
  • Author: Justin Ko

Change Summary

  • dcy-002 FR-5 로부터 최초 생성(Proposed). NFR-1 일부 흡수.

Intent

  • PostgreSQL 영속화 + 스키마 + 드리프트 테스트 요구사항을 backlog 로 정규화.

Acceptance Criteria Delta

  • Added:
    • 마이그레이션/컬럼 일치, repo 구동 테스트, 드리프트 테스트, advisory lock 4개 기준
  • Updated:
    • 없음
  • Removed:
    • 없음

Impacted Area Delta

  • Docs:
    • docs/data-model.md, dcy-001 부록 A
  • Modules:
    • persistence 어댑터(psycopg) + 마이그레이션 SQL
  • Tests:
    • PostgreSQL repo 통합테스트 + 드리프트 테스트

Delivery Trace Delta

  • Batches:
    • none
  • Releases:
    • none

Revalidation Impact

  • Reverification Needed: no
  • Existing Implementation Invalidated: no
  • Status Recommendation: keep-current
  • Follow-up Action: confirm-req 후 batch 편성

Validation Plan

  • PostgreSQL 통합테스트(가능 시) + 드리프트 테스트로 검증.

CHG-20260603-02

  • Date: 2026-06-03
  • Author: Justin Ko

Change Summary

  • Status Proposed → Approved (confirm-req).

Intent

  • 승인 게이트(필수 필드·구체 AC·blocker 없음) 충족, source dcy-002(confirmed, handoff 승인)와 모순 없음 확인.

Acceptance Criteria Delta

  • Added:
    • 없음
  • Updated:
    • 없음
  • Removed:
    • 없음

Impacted Area Delta

  • Docs:
    • docs/srs/index.md (Register 상태 Approved)
  • Modules:
    • 없음
  • Tests:
    • 없음

Delivery Trace Delta

  • Batches:
    • none (batch 편성 전)
  • Releases:
    • none

Revalidation Impact

  • Reverification Needed: no
  • Existing Implementation Invalidated: no
  • Status Recommendation: keep-current
  • Follow-up Action: suggest-batch-reqs / draft-batch 로 delivery batch 편성

Validation Plan

  • 승인 후 batch planning 에서 본 REQ 의 Acceptance Criteria 를 검증 기준으로 사용.

CHG-20260603-03

  • Date: 2026-06-03
  • Author: Justin Ko

Change Summary

  • Status Approved → Implemented (confirm-req-implemented, 근거 batch BAT-002 release-candidate).

Intent

  • BAT-002 verification.md 의 acceptance mapping + evidence(pytest 74 pass / PG 통합 / 데몬 E2E)로 모든 AC 충족 → 구현 완료 동기화.

Acceptance Criteria Delta

  • Added:
    • 없음
  • Updated:
    • 없음
  • Removed:
    • 없음

Impacted Area Delta

  • Docs:
    • docs/srs/index.md (Register Implemented)
  • Modules:
    • 없음
  • Tests:
    • 없음

Delivery Trace Delta

  • Batches:
    • BAT-002 (release-candidate)
  • Releases:
    • none

Revalidation Impact

  • Reverification Needed: no
  • Existing Implementation Invalidated: no
  • Status Recommendation: keep-current
  • Follow-up Action: draft-release 로 release 편성

Validation Plan

  • BAT-002 verification evidence 재참조로 충분.

CHG-20260608-01

  • Date: 2026-06-08
  • Author: Justin Ko

Change Summary

  • (change-req, dcy-012) daily_slot.entry_price → limit_price 컬럼 rename 마이그레이션(006_rename_entry_price_to_limit_price.sql, idempotent DO-block) + persistence 매핑(_SLOT_COLUMNS/update_slot/_row_to_slot) 갱신. 순수 rename 으로 기존 행 데이터 보존(S-3). BAT-016 재구현·재검증 완료 → Implemented 유지.

Intent

  • 도메인 필드 rename(REQ-002)을 영속 스키마/매핑에 반영하면서 기존 데이터를 보존.

Acceptance Criteria Delta

  • Updated:
    • daily_slot 컬럼 entry_price → limit_price (마이그레이션 006, 데이터 보존). persistence read/write 매핑 동기화.

Impacted Area Delta

  • Modules: trex_core/adapters/persistence/postgresql.py, trex_core/adapters/persistence/sql/006_rename_entry_price_to_limit_price.sql(신규)
  • Tests: tests/adapters/persistence/test_migration_and_drift.py(컬럼 계약 = limit_price, 마이그레이션 idempotent)

Delivery Trace Delta

  • Batches: BAT-016
  • Releases: none

Revalidation Impact

  • Reverification Needed: yes (BAT-016 에서 완료)
  • Existing Implementation Invalidated: yes (entry_price 컬럼/매핑)
  • Status Recommendation: Implemented (change-req → BAT-016 재검증)
  • Follow-up Action: confirm-req-implemented(BAT-016).

Validation Plan

  • BAT-016: PG 게이트 마이그레이션 round-trip + 컬럼 계약(limit_price) + idempotency.

CHG-20260609-01

  • Date: 2026-06-09
  • Author: Justin Ko

Change Summary

  • (change-req, dcy-019/issue #18) 영속화 레이어에 slot-scoped entry_count 반영: 멱등 마이그레이션 008(ALTER TABLE daily_slot ADD COLUMN IF NOT EXISTS entry_count INTEGER NOT NULL DEFAULT 0), _SLOT_COLUMNS/update_slot/_row_to_slot flat 매핑 +entry_count, 신규 find_allocatable_slot(idle ∪ closed, ORDER BY entry_count ASC, updated_at ASC, slot_id ASC LIMIT 1) selector 추가(in-memory 동등 구현 포함). 동시성은 기존 advisory lock + 재시도 규약 보존(find 쿼리 FOR UPDATE 미사용 — find_idle_slot 와 동일). BAT-025 검증 완료 → Implemented 유지.

Intent

  • buy admission 의 후보 확장(idle∪closed)·공정 정렬을 영속 store 에서 결정적으로 지원하고, slot row 에 진입 횟수를 durable 하게 보존.

Acceptance Criteria Delta

  • Updated:
    • AC-1 컬럼 계약: daily_slot 에 entry_count 컬럼 추가(마이그레이션 008, additive·멱등). EXPECTED_SLOT_FIELDS ↔ information_schema 드리프트 가드가 자동 반영.
  • Added:
    • find_allocatable_slot(trading_date) 가 idle ∪ closed 후보를 entry_count → updated_at → slot_id 정렬로 1건 반환(없으면 None).

Impacted Area Delta

  • Modules: trex_core/adapters/persistence/sql/008_add_slot_entry_count.sql(신규), trex_core/adapters/persistence/postgresql.py(_SLOT_COLUMNS/update_slot/_row_to_slot/find_allocatable_slot), trex_core/domain/slot/repository.py·in_memory_repository.py(selector 포트 + 동등 구현)
  • Tests: tests/adapters/persistence/test_migration_and_drift.py(자동 반영), tests/domain/slot/test_slot_entry_count.py(selector 정렬)

Delivery Trace Delta

  • Batches: BAT-025
  • Releases: none

Revalidation Impact

  • Reverification Needed: yes (BAT-025 에서 완료)
  • Existing Implementation Invalidated: no (additive 컬럼·신규 selector — 기존 find_idle_slot/round-trip 보존)
  • Status Recommendation: Implemented (change-req → BAT-025 재검증)
  • Follow-up Action: confirm-req-implemented(BAT-025).

Validation Plan

  • BAT-025: unit (in-memory selector 정렬·reallocation) green; 마이그레이션 008 round-trip + 드리프트 가드는 test DSN 환경에서 자동 검증(라이브 runtime DSN 미사용 — 파괴적 truncate 회피).