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>
This commit is contained in:
2026-06-13 13:20:14 +09:00
commit ee3e799de1
1474 changed files with 176087 additions and 0 deletions
+153
View File
@@ -0,0 +1,153 @@
"""validate_artifact_sync_v1.py — P4-T04: Artifact Sync Validation
Checks that engine_harness_gate_result.json (runtime log) and
formula_runtime_registry_v1.json (artifact file) report consistent gates.
Blocks release if log says PASS/OK but artifact file says FAIL.
formula_id: VALIDATE_ARTIFACT_SYNC_V1
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_ENGINE_RESULT = ROOT / "Temp" / "engine_harness_gate_result.json"
DEFAULT_REGISTRY = ROOT / "Temp" / "formula_runtime_registry_v1.json"
DEFAULT_MANIFEST = ROOT / "runtime" / "active_artifact_manifest.yaml"
def _load_json(path: Path) -> dict:
if not path.exists():
return {"_missing": True, "_path": str(path)}
try:
return json.loads(path.read_text(encoding="utf-8"))
except Exception as e:
return {"_error": str(e), "_path": str(path)}
def _load_yaml(path: Path) -> dict:
if not path.exists():
return {"_missing": True, "_path": str(path)}
try:
obj = yaml.safe_load(path.read_text(encoding="utf-8"))
return obj if isinstance(obj, dict) else {}
except Exception as e:
return {"_error": str(e), "_path": str(path)}
def _check_engine_result(engine: dict) -> tuple[bool, str]:
if engine.get("_missing"):
return False, f"engine_harness_gate_result.json missing: {engine.get('_path')}"
status = str(engine.get("status") or "").upper()
# gate field might not exist; status=OK is the success signal
if status not in ("OK", "PASS"):
return False, f"engine_harness_gate_result status={status!r} (expected OK/PASS)"
return True, "OK"
def _check_registry(registry: dict) -> tuple[bool, str]:
if registry.get("_missing"):
return False, f"formula_runtime_registry_v1.json missing: {registry.get('_path')}"
gate = str(registry.get("gate") or "").upper()
unmapped = registry.get("unmapped_formula_count", -1)
coverage = registry.get("runtime_adjusted_coverage_pct", 0)
if gate != "PASS":
return False, (
f"formula_runtime_registry gate={gate!r}, "
f"unmapped={unmapped}, coverage={coverage}%"
)
return True, f"gate=PASS coverage={coverage}%"
def _check_manifest(manifest: dict) -> tuple[bool, str]:
if manifest.get("_missing"):
return False, f"active_artifact_manifest.yaml missing: {manifest.get('_path')}"
artifacts = manifest.get("artifacts") or []
if not artifacts:
# Some manifests use different structure
artifacts = list(manifest.values()) if isinstance(manifest, dict) else []
collision_count = 0
stale_count = 0
for art in artifacts:
if not isinstance(art, dict):
continue
if art.get("stale"):
stale_count += 1
active_aliases = art.get("active_aliases") or []
if len(active_aliases) > 1:
collision_count += 1
if collision_count > 0:
return False, f"authority_collision_count={collision_count}"
if stale_count > 0:
return False, f"stale_artifact_count={stale_count}"
return True, "OK"
def _check_sync_consistency(engine_ok: bool, registry_ok: bool) -> tuple[bool, str]:
"""Log PASS but artifact FAIL → ARTIFACT_SYNC_FAIL"""
if engine_ok and not registry_ok:
return False, "ARTIFACT_SYNC_MISMATCH: engine_log=OK but registry=FAIL"
return True, "consistent"
def main() -> int:
ap = argparse.ArgumentParser(description="P4-T04 artifact sync validator")
ap.add_argument("--engine-result", default=str(DEFAULT_ENGINE_RESULT))
ap.add_argument("--manifest", default=str(DEFAULT_MANIFEST))
ap.add_argument("--registry", default=str(DEFAULT_REGISTRY))
args = ap.parse_args()
engine = _load_json(Path(args.engine_result))
registry = _load_json(Path(args.registry))
manifest = _load_yaml(Path(args.manifest))
engine_ok, engine_msg = _check_engine_result(engine)
registry_ok, registry_msg = _check_registry(registry)
manifest_ok, manifest_msg = _check_manifest(manifest)
sync_ok, sync_msg = _check_sync_consistency(engine_ok, registry_ok)
mismatches = []
if not engine_ok:
mismatches.append(f"engine: {engine_msg}")
if not registry_ok:
mismatches.append(f"registry: {registry_msg}")
if not manifest_ok:
mismatches.append(f"manifest: {manifest_msg}")
if not sync_ok:
mismatches.append(f"sync: {sync_msg}")
gate = "PASS" if not mismatches else "FAIL"
result = {
"formula_id": "VALIDATE_ARTIFACT_SYNC_V1",
"artifact_sync_mismatch_count": len(mismatches),
"checks": {
"engine_result": {"ok": engine_ok, "detail": engine_msg},
"formula_registry": {"ok": registry_ok, "detail": registry_msg},
"active_manifest": {"ok": manifest_ok, "detail": manifest_msg},
"sync_consistency": {"ok": sync_ok, "detail": sync_msg},
},
"mismatches": mismatches,
"gate": gate,
}
print(json.dumps(result, ensure_ascii=False, indent=2))
if gate == "PASS":
print("VALIDATE_ARTIFACT_SYNC_V1_OK")
return 0
else:
print("VALIDATE_ARTIFACT_SYNC_V1_FAIL")
return 1
if __name__ == "__main__":
raise SystemExit(main())