aedabdd37b
suggest/quant_investment_engine_v8_9_portfolio_optimizer_canonical_refactored.yaml의
implementation_todo_v8_9(P0~P4) 전체를 spec/tool/golden case 레벨로 구현.
- P0: PORTFOLIO_TRANSITION_UTILITY_V1, SELL_LOT_PARETO_SELECTOR_V1, FORECAST_SIMULATION_ENGINE_V1
- P1: SECTOR_EXPOSURE_GRAPH_V1/LEADER_LIFECYCLE_GATE_V1, EXECUTION_CAPACITY_LADDER_V1, MODEL_GOVERNANCE_KILL_SWITCH_V1
- P2: SCENARIO_SHOCK_MATRIX_V1, TRANSITION_SET_ENUMERATOR_V1, IMMUTABLE_DECISION_LEDGER_V1, EXECUTION_PLAN_COMPILER_V1
- P3: STATE_VECTOR_CONSTRUCTOR_V1, WALK_FORWARD_BOOTSTRAP_V1, TRANSITION_SET_ENUMERATOR_V1(MRC/CVaR 확장),
REBALANCE_CADENCE_GATE_V1, WEEKLY_LEGACY_TRANSFER_PLAN_V1
기존 regime/cluster 연동 정책 수치(현금방어선, 반도체 cap)는 그대로 유지하고 신규 cap 필드만 추가.
spec/09_decision_flow.yaml과 runtime/active_artifact_manifest.yaml에 전 엔진 배선 완료.
governance/todo/v8_9_p{0,1,2,3}_adoption_plan.yaml에 각 단계 작업 추적 기록.
검증: validate_specs/validate_golden_coverage_100(100%)/validate_calibration_registry_v1/
validate_schema_model_generation_v1/validate_agents_shrink_v1 전부 PASS. golden test 53/53 PASS.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
110 lines
4.2 KiB
Python
110 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""IMMUTABLE_DECISION_LEDGER_V1 — spec/formulas/domains/governance.yaml.
|
|
|
|
Append-only decision log. Refuses to append a duplicate decision_id and never
|
|
mutates an existing record. governance/todo/v8_9_p2_adoption_plan.yaml P2-C.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_LEDGER = ROOT / "Temp" / "immutable_decision_ledger_v1.json"
|
|
DEFAULT_DECISION = ROOT / "Temp" / "portfolio_transition_optimizer_v1.json"
|
|
|
|
REQUIRED_FIELDS = [
|
|
"decision_id",
|
|
"engine_version",
|
|
"input_hash_bundle",
|
|
"execution_mode",
|
|
"candidate_ids",
|
|
]
|
|
|
|
|
|
def _load_ledger(path: Path) -> dict:
|
|
if not path.exists():
|
|
return {"formula_id": "IMMUTABLE_DECISION_LEDGER_V1", "records": []}
|
|
try:
|
|
data = json.loads(path.read_text(encoding="utf-8"))
|
|
if isinstance(data, dict) and isinstance(data.get("records"), list):
|
|
return data
|
|
return {"formula_id": "IMMUTABLE_DECISION_LEDGER_V1", "records": []}
|
|
except Exception:
|
|
return {"formula_id": "IMMUTABLE_DECISION_LEDGER_V1", "records": []}
|
|
|
|
|
|
def append_decision(ledger: dict, decision: dict) -> tuple[dict, str]:
|
|
missing = [f for f in REQUIRED_FIELDS if decision.get(f) is None]
|
|
if missing:
|
|
return ledger, "REJECTED_MISSING_FIELDS"
|
|
|
|
decision_id = decision["decision_id"]
|
|
existing_ids = {r["decision_id"] for r in ledger["records"]}
|
|
if decision_id in existing_ids:
|
|
return ledger, "DUPLICATE_DECISION_ID"
|
|
|
|
record = {
|
|
"decision_id": decision_id,
|
|
"timestamp": decision.get("timestamp") or datetime.now(timezone.utc).isoformat(),
|
|
"engine_version": decision["engine_version"],
|
|
"input_hash_bundle": decision["input_hash_bundle"],
|
|
"execution_mode": decision["execution_mode"],
|
|
"candidate_ids": decision["candidate_ids"],
|
|
"selected_transition_id": decision.get("selected_transition_id"),
|
|
"hard_blocks": decision.get("hard_blocks", []),
|
|
"transition_utility_krw": decision.get("transition_utility_krw"),
|
|
"operator_override": decision.get("operator_override", False),
|
|
"order_ids": decision.get("order_ids", []),
|
|
"fill_prices": decision.get("fill_prices", []),
|
|
"slippage": decision.get("slippage"),
|
|
"T1_return": None,
|
|
"T5_return": None,
|
|
"T20_return": None,
|
|
"MAE": None,
|
|
"MFE": None,
|
|
}
|
|
new_ledger = {**ledger, "records": ledger["records"] + [record]}
|
|
return new_ledger, "APPENDED"
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--ledger", default=str(DEFAULT_LEDGER))
|
|
ap.add_argument("--decision", default=str(DEFAULT_DECISION))
|
|
args = ap.parse_args()
|
|
|
|
ledger = _load_ledger(Path(args.ledger))
|
|
decision_doc = {}
|
|
decision_path = Path(args.decision)
|
|
if decision_path.exists():
|
|
try:
|
|
decision_doc = json.loads(decision_path.read_text(encoding="utf-8"))
|
|
except Exception:
|
|
decision_doc = {}
|
|
|
|
decision = {
|
|
"decision_id": decision_doc.get("packet_id") or decision_doc.get("formula_id"),
|
|
"engine_version": "PORTFOLIO_TRANSITION_UTILITY_V1",
|
|
"input_hash_bundle": decision_doc.get("input_hash") or "unknown",
|
|
"execution_mode": decision_doc.get("execution_mode") or decision_doc.get("final_action"),
|
|
"candidate_ids": [c.get("candidate_id") for c in decision_doc.get("candidate_actions", [])],
|
|
"selected_transition_id": (decision_doc.get("selected_transition") or {}).get("candidate_id"),
|
|
"hard_blocks": decision_doc.get("reason_codes", []),
|
|
"transition_utility_krw": (decision_doc.get("selected_transition") or {}).get("transition_utility_krw"),
|
|
}
|
|
|
|
new_ledger, status = append_decision(ledger, decision)
|
|
new_ledger["status"] = status
|
|
out = Path(args.ledger)
|
|
if status == "APPENDED":
|
|
out.write_text(json.dumps(new_ledger, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(json.dumps({"formula_id": "IMMUTABLE_DECISION_LEDGER_V1", "ledger_append_status": status}, ensure_ascii=False, indent=2))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|