Files
QuantEngineByItz/tools/validate_artifact_freshness_v1.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

100 lines
3.4 KiB
Python

from __future__ import annotations
import argparse
import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_OUT = ROOT / "Temp" / "artifact_freshness_gate_v1.json"
DEFAULT_JSON = ROOT / "GatherTradingData.json"
AUTHORITATIVE_ARTIFACTS = [
ROOT / "Temp" / "final_execution_decision_v2.json",
ROOT / "Temp" / "single_truth_ledger_v1.json",
ROOT / "Temp" / "operational_truth_score_v1.json",
ROOT / "Temp" / "execution_readiness_matrix_v1.json",
ROOT / "Temp" / "final_judgment_gate_v1.json",
ROOT / "Temp" / "strategy_hardening_harness_v2.json",
ROOT / "Temp" / "smart_cash_recovery_v6.json",
ROOT / "Temp" / "yaml_gs_ps_coverage.json",
ROOT / "Temp" / "harness_coverage_audit.json",
ROOT / "Temp" / "algorithm_guidance_proof_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 _hash(path: Path) -> str:
import hashlib
return hashlib.sha256(path.read_bytes()).hexdigest()
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--json", default=str(DEFAULT_JSON))
ap.add_argument("--out", default=str(DEFAULT_OUT))
ap.add_argument("--max-age-minutes", type=int, default=240)
args = ap.parse_args()
json_path = Path(args.json)
if not json_path.is_absolute():
json_path = ROOT / json_path
source_hash = _hash(json_path) if json_path.exists() else "MISSING"
source_mtime = datetime.fromtimestamp(json_path.stat().st_mtime, tz=timezone.utc) if json_path.exists() else None
stale_artifacts: list[dict[str, Any]] = []
artifact_rows: list[dict[str, Any]] = []
now = datetime.now(timezone.utc)
for art in AUTHORITATIVE_ARTIFACTS:
row = {
"path": str(art.relative_to(ROOT)).replace("\\", "/"),
"exists": art.exists(),
}
if art.exists():
row["sha256"] = _hash(art)
row["age_minutes"] = round((now - datetime.fromtimestamp(art.stat().st_mtime, tz=timezone.utc)).total_seconds() / 60.0, 2)
row["fresh"] = row["age_minutes"] <= args.max_age_minutes
if not row["fresh"]:
stale_artifacts.append(row)
else:
row["sha256"] = None
row["age_minutes"] = None
row["fresh"] = False
stale_artifacts.append(row)
artifact_rows.append(row)
gate = "PASS" if not stale_artifacts else "STALE_BLOCK"
result = {
"formula_id": "ARTIFACT_FRESHNESS_GATE_V1",
"gate": gate,
"source_payload_hash": source_hash,
"source_payload_mtime_utc": source_mtime.isoformat() if source_mtime else None,
"max_age_minutes": args.max_age_minutes,
"stale_artifact_count": len(stale_artifacts),
"stale_artifacts": stale_artifacts,
"artifacts": artifact_rows,
}
out_path = Path(args.out)
if not out_path.is_absolute():
out_path = ROOT / out_path
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(result, ensure_ascii=False, indent=2))
return 0 if gate == "PASS" else 1
if __name__ == "__main__":
raise SystemExit(main())