Files
QuantEngineByItz/tools/measure_semantic_formula_coverage.py
kjh2064 ee3e799de1 feat: 리밸런싱 엔진 V1 + GAS 버그 수정 (2026-06-13)
주요 변경:
- tools/build_rebalance_engine_v1.py: REBALANCE_ENGINE_V1 신규
  * account_snapshot 직접 합산(_build_snap_position_map) → 소수주 분리 행 병합
  * 레짐 소스 macro.REGIME_PRELIM 최우선 (GAS 와 동일)
- src/gas_adapter_parts/gdf_06_rebalance.gs: runRebalanceSheet_() 신규
  * Logger.log / getSpreadsheet_() 로 run_all 연동 수정
- src/gas_adapter_parts/gdc_01_fetch_fundamentals.gs
  * _mergePositionRecord_(): 소수주 중복 행 합산 신규
  * parseInt → parseFloat (qty, availQty)
- src/gas_adapter_parts/gdf_01_price_metrics.gs
  * 미보유 종목 SELL_READY → WATCH_EXIT_SIGNAL
- spec/41_release_dag.yaml: build_rebalance_sheet 노드 추가 (step_count 63)
- spec/51_formula_lifecycle_registry.yaml: REBALANCE_ENGINE_V1 등록

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-13 13:20:14 +09:00

115 lines
4.2 KiB
Python

from __future__ import annotations
import argparse
import json
import re
from pathlib import Path
from typing import Any
import yaml
ROOT = Path(__file__).resolve().parents[1]
REGISTRY = ROOT / "spec" / "13_formula_registry.yaml"
GOLDEN_V2 = ROOT / "spec" / "formula_golden_cases_v2.yaml"
GOLDEN_TEMP = ROOT / "Temp" / "formula_golden_cases.yaml"
DEFAULT_OUT = ROOT / "Temp" / "semantic_formula_coverage_v1.json"
def _load_registry(path: Path) -> list[str]:
if not path.exists():
return []
try:
payload = yaml.safe_load(path.read_text(encoding="utf-8"))
except Exception:
return []
fr = (payload.get("formula_registry") or {}) if isinstance(payload, dict) else {}
formulas = fr.get("formulas") or {}
if formulas:
return sorted(formulas.keys())
# fallback: regex scan
text = json.dumps(payload, ensure_ascii=False)
ids = sorted(set(re.findall(r'"formula_id"\s*:\s*"([A-Z0-9_]+)"', text)))
if not ids:
ids = sorted(set(re.findall(r'\b([A-Z][A-Z0-9_]+_V[0-9]+)\b', text)))
return ids
def _load_golden(path: Path) -> dict[str, Any]:
if not path.exists():
return {}
try:
payload = yaml.safe_load(path.read_text(encoding="utf-8"))
except Exception:
return {}
return payload if isinstance(payload, dict) else {}
def _scan_code() -> str:
blobs: list[str] = []
for p in ROOT.rglob("*"):
if not p.is_file():
continue
if p.suffix.lower() not in {".py", ".gs", ".yaml", ".yml", ".md"}:
continue
try:
blobs.append(p.read_text(encoding="utf-8", errors="ignore"))
except Exception:
continue
return "\n".join(blobs)
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--out", default=str(DEFAULT_OUT))
args = ap.parse_args()
out_path = Path(args.out)
if not out_path.is_absolute():
out_path = ROOT / out_path
formula_ids = _load_registry(REGISTRY)
corpus = _scan_code()
spec_total = len(formula_ids)
impl = [fid for fid in formula_ids if fid in corpus]
report_binding = [fid for fid in formula_ids if fid in corpus and "render_operational_report.py" in corpus]
outcome_binding = [fid for fid in formula_ids if fid.startswith(("OUTCOME_", "TRADE_", "SHORT_HORIZON_", "LATE_", "REBOUND_", "CASH_RAISE_")) and fid in corpus]
golden_path = GOLDEN_V2 if GOLDEN_V2.exists() else GOLDEN_TEMP
golden = _load_golden(golden_path)
golden_rows = golden.get("golden_cases_v2") if isinstance(golden.get("golden_cases_v2"), list) else golden.get("golden_cases")
if not isinstance(golden_rows, list):
golden_rows = []
golden_formula_ids = [str(row.get("formula_id") or "") for row in golden_rows if isinstance(row, dict)]
golden_formula_ids = [fid for fid in golden_formula_ids if fid]
golden_covered = sum(1 for fid in golden_formula_ids if fid in corpus)
grade = "PASS" if spec_total > 0 and len(impl) == spec_total and golden_covered == len(golden_formula_ids) else ("WARN" if len(impl) > 0 else "FAIL")
# outcome_binding_deferred: decision-critical formulas not yet bound to operational T+20 outcomes.
# Marked DEFERRED (not absent) until V8-P1-06 operational_t20_count >= 30.
outcome_binding_deferred = spec_total - len(outcome_binding)
out = {
"formula_id": "SEMANTIC_FORMULA_COVERAGE_HARNESS_V1",
"spec_total": spec_total,
"implementation_covered": len(impl),
"golden_test_covered": golden_covered,
"report_binding_covered": len(report_binding),
"outcome_binding_covered": len(outcome_binding),
"outcome_binding_deferred": outcome_binding_deferred,
"outcome_binding_deferred_reason": "PENDING_OPERATIONAL_T20_SAMPLE_V8_P1_06",
"outcome_binding_total_check": len(outcome_binding) + outcome_binding_deferred,
"coverage_grade": grade,
"missing": [fid for fid in formula_ids if fid not in impl][:200],
"golden_source": str(golden_path.name),
}
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(out, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(out, ensure_ascii=False, indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main())