Files
QuantEngineByItz/tools/validate_pass_100_criteria_v1.py
T
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

110 lines
4.1 KiB
Python

from __future__ import annotations
import argparse
import json
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_PATH = ROOT / "Temp" / "pass_100_criteria_v1.json"
def _load(path: Path) -> dict[str, Any]:
if not path.exists():
return {}
try:
obj = json.loads(path.read_text(encoding="utf-8"))
except Exception:
return {}
return obj if isinstance(obj, dict) else {}
def main() -> int:
ap = argparse.ArgumentParser(description="Validate PASS_100_CRITERIA_V1.")
ap.add_argument("--json", default=str(DEFAULT_PATH))
args = ap.parse_args()
path = Path(args.json)
if not path.is_absolute():
path = ROOT / path
payload = _load(path)
errors: list[str] = []
_VALID_IDS = {"PASS_100_CRITERIA_V1", "PASS_100_CRITERIA_V3_ALIAS_V1"}
if str(payload.get("formula_id") or "") not in _VALID_IDS:
errors.append("formula_id mismatch")
gate = str(payload.get("gate") or "")
if gate not in {"PASS_100", "BLOCK_EXECUTION"}:
errors.append(f"gate={gate}")
if not isinstance(payload.get("pass_100_allowed"), bool):
errors.append("pass_100_allowed must be bool")
criteria = payload.get("criteria")
if not isinstance(criteria, list) or not criteria:
errors.append("criteria must be non-empty list")
criteria = []
passed_count = 0
failed: list[str] = []
for idx, row in enumerate(criteria):
if not isinstance(row, dict):
errors.append(f"criteria[{idx}] must be object")
continue
if not str(row.get("criterion_id") or "").strip():
errors.append(f"criteria[{idx}].criterion_id missing")
if not isinstance(row.get("passed"), bool):
errors.append(f"criteria[{idx}].passed must be bool")
continue
if row["passed"]:
passed_count += 1
else:
failed.append(str(row.get("criterion_id") or f"criteria[{idx}]"))
if not str(row.get("remediation") or "").strip() or str(row.get("remediation")) == "NONE":
errors.append(f"criteria[{idx}].remediation missing")
if not str(row.get("source_json") or "").strip():
errors.append(f"criteria[{idx}].source_json missing")
if not str(row.get("formula_id") or "").strip():
errors.append(f"criteria[{idx}].formula_id missing")
expected_score = round(passed_count / len(criteria) * 100.0, 2) if criteria else 0.0
actual_score = payload.get("score_0_100")
if not isinstance(actual_score, (int, float)) or round(float(actual_score), 2) != expected_score:
errors.append(f"score_0_100 mismatch expected={expected_score} actual={actual_score}")
if payload.get("passed_count") != passed_count:
errors.append(f"passed_count mismatch expected={passed_count} actual={payload.get('passed_count')}")
if payload.get("failed_count") != len(failed):
errors.append(f"failed_count mismatch expected={len(failed)} actual={payload.get('failed_count')}")
if payload.get("failed_criteria") != failed:
errors.append("failed_criteria mismatch")
pass_allowed = bool(payload.get("pass_100_allowed"))
if pass_allowed and failed:
errors.append("pass_100_allowed cannot be true with failed criteria")
if gate == "PASS_100" and failed:
errors.append("PASS_100 cannot have failed criteria")
if gate == "BLOCK_EXECUTION" and not failed:
errors.append("BLOCK_EXECUTION requires failed criteria")
if gate == "PASS_100" and not pass_allowed:
errors.append("PASS_100 requires pass_100_allowed=true")
if gate == "BLOCK_EXECUTION" and pass_allowed:
errors.append("BLOCK_EXECUTION requires pass_100_allowed=false")
if errors:
print("PASS_100_CRITERIA_V1_FAIL")
for err in errors:
print(f" {err}")
return 1
print("PASS_100_CRITERIA_V1_OK")
print(f" gate: {gate}")
print(f" score_0_100: {float(payload.get('score_0_100')):.2f}")
print(f" failed_count: {len(failed)}")
return 0
if __name__ == "__main__":
raise SystemExit(main())