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

113 lines
3.5 KiB
Python

"""Build tools_index.yaml — classifies all tools/ Python files as active, support, or archive.
Active: called directly from spec/41_release_dag.yaml
Support: imported by other active tools (not entry-point but needed)
Archive: no direct or transitive reference from DAG or active tools
"""
from __future__ import annotations
import argparse
import re
from pathlib import Path
from typing import Any
import yaml
ROOT = Path(__file__).resolve().parents[1]
TOOLS_DIR = ROOT / "tools"
DAG_SPEC = ROOT / "spec" / "41_release_dag.yaml"
DEFAULT_OUT = ROOT / "tools_index.yaml"
def _load_dag_active(dag_path: Path) -> set[str]:
if not dag_path.exists():
return set()
try:
data = yaml.safe_load(dag_path.read_text(encoding="utf-8")) or {}
except Exception:
return set()
active: set[str] = set()
for node in (data.get("dag") or {}).get("nodes", {}).values():
cmd = node.get("command", "")
if isinstance(cmd, list):
cmd = " ".join(str(c) for c in cmd)
for m in re.findall(r"tools/([\w]+\.py)", str(cmd)):
active.add(m)
return active
def _find_imports(py_file: Path) -> set[str]:
text = py_file.read_text(encoding="utf-8", errors="ignore")
imports: set[str] = set()
for m in re.findall(r"from\s+([\w_]+)\s+import|import\s+([\w_]+)", text):
name = (m[0] or m[1]).strip()
if name:
candidate = f"{name}.py"
imports.add(candidate)
return imports
def _compute_support(active: set[str], all_tools: set[str]) -> set[str]:
support: set[str] = set()
frontier = set(active)
for _ in range(5):
new_support: set[str] = set()
for tool_name in frontier:
py = TOOLS_DIR / tool_name
if not py.exists():
continue
for imp in _find_imports(py):
if imp in all_tools and imp not in active and imp not in support:
new_support.add(imp)
if not new_support:
break
support.update(new_support)
frontier = new_support
return support
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--root", default=str(ROOT))
ap.add_argument("--out", default=str(DEFAULT_OUT))
args = ap.parse_args()
tools_dir = Path(args.root) / "tools"
all_tool_names = {p.name for p in tools_dir.glob("*.py")}
dag_active = _load_dag_active(DAG_SPEC)
support = _compute_support(dag_active, all_tool_names)
active_list = sorted(dag_active & all_tool_names)
support_list = sorted(support - dag_active)
archive_candidates = sorted(all_tool_names - dag_active - support)
result: dict[str, Any] = {
"schema_version": "tools_index.v1",
"generated_at": "2026-06-10",
"summary": {
"total": len(all_tool_names),
"active": len(active_list),
"support": len(support_list),
"archive_candidates": len(archive_candidates),
},
"active": active_list,
"support": support_list,
"archive_candidates": archive_candidates,
}
out_path = Path(args.out)
out_path.write_text(yaml.dump(result, allow_unicode=True, default_flow_style=False), encoding="utf-8")
print("TOOL_INVENTORY_V1_OK")
print(f" total: {len(all_tool_names)}")
print(f" active (in DAG): {len(active_list)}")
print(f" support (imported by active): {len(support_list)}")
print(f" archive_candidates: {len(archive_candidates)}")
return 0
if __name__ == "__main__":
raise SystemExit(main())