mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-07-31 03:05:21 +00:00
380 lines
14 KiB
Python
380 lines
14 KiB
Python
#!/usr/bin/env python3
|
|
# ==============================================================================
|
|
# COPYRIGHT NO ONE EVERYWHERE LLC (WYOMING HOLDING COMPANY)
|
|
# PROJECT: SOVEREIGN STACK
|
|
# This artifact is entirely proprietary and cryptographically proven.
|
|
# Open-Source usage requires explicit permission from Brandon Scott Schneider.
|
|
# ==============================================================================
|
|
|
|
# [WARDEN BOUNDARY ENFORCEMENT INJECTED]
|
|
import sys
|
|
import os
|
|
try:
|
|
from io_harness_compat import spawn_isolated_process, fetch_network_resource
|
|
except ImportError:
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from io_harness_compat import spawn_isolated_process, fetch_network_resource
|
|
|
|
#!/usr/bin/env python3
|
|
"""
|
|
Graph OS Risk-Based Remediation for Hyperfluid CarrierState Anomalies
|
|
|
|
Bridge hyperfluid carrier_state findings → Graph OS risk scoring → auto-remediation
|
|
Converts carrier_state anomalies into risk nodes, scores them, then auto-fixes high-risk chains.
|
|
|
|
Usage:
|
|
python graph_os_remediate_carrier_state_findings.py \
|
|
--carrier_state-report hyperfluid_causal_report_with_carrier_state.json \
|
|
--graph_os-input metadata_report.json \
|
|
--execute # actually apply fixes; omit to dry-run
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
# import subprocess (REMOVED BY WARDEN)
|
|
import sys
|
|
from dataclasses import dataclass, asdict
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
@dataclass
|
|
class RemediationAction:
|
|
chain: str
|
|
carrier_state_energy: float
|
|
risk_score: int
|
|
action: str # 'pause' | 'reduce_size' | 'retrain' | 'alert'
|
|
reason: str
|
|
executed: bool = False
|
|
result: Optional[str] = None
|
|
|
|
|
|
def load_carrier_state_report(path: str) -> Dict[str, Any]:
|
|
"""Load hyperfluid report augmented with carrier_state data."""
|
|
with open(path) as f:
|
|
return json.load(f)
|
|
|
|
|
|
def convert_carrier_state_to_metadata(carrier_state_report: Dict) -> Dict[str, Dict]:
|
|
"""
|
|
Convert carrier_state anomaly findings to Graph OS metadata node format.
|
|
|
|
Each chain becomes a "node" with tier + tags + metadata.
|
|
CarrierState energy, coherence, and anomaly count become tagged risk indicators.
|
|
"""
|
|
metadata = {}
|
|
|
|
chain_backtests = carrier_state_report.get("chain_backtests", {})
|
|
|
|
for chain_name, chain_data in chain_backtests.items():
|
|
carrier_state_summary = chain_data.get("carrier_state_summary", {})
|
|
|
|
# Extract key carrier_state metrics
|
|
coherence = carrier_state_summary.get("mean_coherence", 1.0)
|
|
manifest_carrier_states = carrier_state_summary.get("manifest_carrier_states", 0)
|
|
top_anomalies = chain_data.get("top_anomalies", [])
|
|
|
|
# Build risk tags
|
|
tags = []
|
|
if coherence < 0.95:
|
|
tags.append("coherence_degradation")
|
|
if manifest_carrier_states > 0:
|
|
tags.append(f"manifest_carrier_state_count_{manifest_carrier_states}")
|
|
if len(top_anomalies) > 0:
|
|
top_energy = max(a.get("carrier_state_energy", 0) for a in top_anomalies)
|
|
if top_energy > 0.35:
|
|
tags.append("high_carrier_state_energy")
|
|
elif top_energy > 0.25:
|
|
tags.append("moderate_carrier_state_energy")
|
|
|
|
# Estimate "tier" based on stability
|
|
if coherence >= 0.99:
|
|
tier = "stable"
|
|
elif coherence >= 0.95:
|
|
tier = "degraded"
|
|
else:
|
|
tier = "critical"
|
|
|
|
metadata[f"chain_{chain_name}"] = {
|
|
"tier": tier,
|
|
"tags": tags,
|
|
"metadata": {
|
|
"module": "hyperfluid_causal_pressure",
|
|
"chain": chain_name,
|
|
"coherence": float(coherence),
|
|
"manifest_carrier_states": int(manifest_carrier_states),
|
|
"top_anomaly_count": len(top_anomalies),
|
|
"top_anomaly_max_energy": float(max(
|
|
(a.get("carrier_state_energy", 0) for a in top_anomalies),
|
|
default=0.0
|
|
)),
|
|
"export_report": carrier_state_report.get("export_report", ""),
|
|
}
|
|
}
|
|
|
|
return metadata
|
|
|
|
|
|
def run_graph_os_scoring(metadata: Dict, out_csv: Optional[str] = None) -> tuple[Dict, str]:
|
|
"""
|
|
Run Graph OS analyzer on metadata nodes.
|
|
For carrier_state findings, we score patterns directly without schema validation.
|
|
Returns (summary_dict, csv_path).
|
|
"""
|
|
import pandas as pd
|
|
|
|
if out_csv is None:
|
|
out_csv = "/tmp/graph_os_carrier_state_scores.csv"
|
|
|
|
# Direct pattern scoring (skip Graph OS schema validation which expects hex node IDs)
|
|
# This is a lightweight inline version for carrier_state anomalies
|
|
|
|
RISK_PATTERNS = {
|
|
"high_carrier_state_energy": {
|
|
"weight": 3,
|
|
"patterns": ["high_carrier_state_energy", "manifest_carrier_state"],
|
|
},
|
|
"coherence_degradation": {
|
|
"weight": 2,
|
|
"patterns": ["coherence_degradation", "degraded"],
|
|
},
|
|
}
|
|
|
|
rows = []
|
|
for node_id, node in metadata.items():
|
|
tier = node.get("tier", "")
|
|
tags = node.get("tags", []) or []
|
|
meta = node.get("metadata", {}) or {}
|
|
|
|
risk_score = 0
|
|
if "high_carrier_state_energy" in tags:
|
|
risk_score += RISK_PATTERNS["high_carrier_state_energy"]["weight"]
|
|
if "coherence_degradation" in tags:
|
|
risk_score += RISK_PATTERNS["coherence_degradation"]["weight"]
|
|
|
|
rows.append({
|
|
"node_id": node_id,
|
|
"tier": tier,
|
|
"tags": ",".join(tags),
|
|
"risk_score": risk_score,
|
|
"chain": meta.get("chain", ""),
|
|
"coherence": meta.get("coherence", 1.0),
|
|
"manifest_carrier_states": meta.get("manifest_carrier_states", 0),
|
|
})
|
|
|
|
df = pd.DataFrame(rows)
|
|
df.to_csv(out_csv, index=False)
|
|
|
|
# Create summary
|
|
high_risk = df[df["risk_score"] > 3].sort_values("risk_score", ascending=False)
|
|
|
|
summary = {
|
|
"totals": {
|
|
"node_count": len(df),
|
|
"high_risk_nodes": int((df["risk_score"] > 3).sum()),
|
|
"risk_total_sum": int(df["risk_score"].sum()),
|
|
},
|
|
"top_risky_nodes": high_risk[["node_id", "tier", "risk_score", "chain"]].to_dict(orient="records"),
|
|
}
|
|
|
|
return summary, out_csv
|
|
|
|
|
|
def recommend_remediations(
|
|
carrier_state_report: Dict,
|
|
graph_os_summary: Dict,
|
|
) -> List[RemediationAction]:
|
|
"""
|
|
Analyze Graph OS risk scores + carrier_state data → recommend remediation actions.
|
|
|
|
Decision tree:
|
|
- If net_score > 10 and manifest_carrier_states > 1 → PAUSE (critical)
|
|
- If net_score > 6 and carrier_state_energy > 0.35 → RETRAIN (drift)
|
|
- If net_score > 3 and coherence < 0.90 → REDUCE_SIZE (degraded)
|
|
- Otherwise → ALERT (watch)
|
|
"""
|
|
actions = []
|
|
|
|
# Get high-risk nodes from Graph OS
|
|
top_risky = graph_os_summary.get("top_risky_nodes", [])
|
|
|
|
chain_backtests = carrier_state_report.get("chain_backtests", {})
|
|
|
|
for node in top_risky:
|
|
node_id = node.get("node_id", "")
|
|
if not node_id.startswith("chain_"):
|
|
continue
|
|
|
|
chain_name = node_id.replace("chain_", "")
|
|
net_score = node.get("net_score", 0)
|
|
|
|
chain_data = chain_backtests.get(chain_name, {})
|
|
carrier_state_summary = chain_data.get("carrier_state_summary", {})
|
|
|
|
coherence = carrier_state_summary.get("mean_coherence", 1.0)
|
|
manifest_carrier_states = carrier_state_summary.get("manifest_carrier_states", 0)
|
|
|
|
top_anomalies = chain_data.get("top_anomalies", [])
|
|
max_energy = max(
|
|
(a.get("carrier_state_energy", 0) for a in top_anomalies),
|
|
default=0.0
|
|
)
|
|
|
|
# Decision logic
|
|
if net_score > 10 and manifest_carrier_states > 1:
|
|
action = RemediationAction(
|
|
chain=chain_name,
|
|
carrier_state_energy=max_energy,
|
|
risk_score=net_score,
|
|
action="pause",
|
|
reason=f"CRITICAL: {manifest_carrier_states} manifest carrier_states detected, net_score {net_score}",
|
|
)
|
|
elif net_score > 6 and max_energy > 0.35:
|
|
action = RemediationAction(
|
|
chain=chain_name,
|
|
carrier_state_energy=max_energy,
|
|
risk_score=net_score,
|
|
action="retrain",
|
|
reason=f"DRIFT: high carrier_state energy {max_energy:.2f}, net_score {net_score}",
|
|
)
|
|
elif net_score > 3 and coherence < 0.90:
|
|
action = RemediationAction(
|
|
chain=chain_name,
|
|
carrier_state_energy=max_energy,
|
|
risk_score=net_score,
|
|
action="reduce_size",
|
|
reason=f"DEGRADED: coherence {coherence:.2f}, net_score {net_score}",
|
|
)
|
|
else:
|
|
action = RemediationAction(
|
|
chain=chain_name,
|
|
carrier_state_energy=max_energy,
|
|
risk_score=net_score,
|
|
action="alert",
|
|
reason=f"WATCH: monitor {chain_name} (net_score {net_score})",
|
|
)
|
|
|
|
actions.append(action)
|
|
|
|
return actions
|
|
|
|
|
|
def execute_remediation(action: RemediationAction) -> None:
|
|
"""
|
|
Execute a remediation action (pause, reduce position, retrain, or alert).
|
|
|
|
For now: print recommended command; in production, integrate with:
|
|
- Position management (reduce trading size)
|
|
- Model retraining system
|
|
- Alert channels (Slack, email)
|
|
"""
|
|
|
|
if action.action == "pause":
|
|
cmd = f"# PAUSE trading on {action.chain}\n# .venv/bin/python 5-Applications/scripts/pause_trading.py --chain {action.chain}"
|
|
elif action.action == "reduce_size":
|
|
cmd = f"# REDUCE position size on {action.chain}\n# .venv/bin/python 5-Applications/scripts/adjust_position.py --chain {action.chain} --scale 0.5"
|
|
elif action.action == "retrain":
|
|
cmd = f"# RETRAIN model for {action.chain}\n# .venv/bin/python 5-Applications/scripts/model_hyperfluid_waveforms.py --retrain-chain {action.chain} --force"
|
|
else: # alert
|
|
cmd = f"# ALERT: {action.reason}"
|
|
|
|
print(f"\n{cmd}")
|
|
action.executed = True
|
|
action.result = cmd
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Graph OS risk-based remediation for carrier_state anomalies"
|
|
)
|
|
parser.add_argument(
|
|
"--carrier_state-report",
|
|
required=True,
|
|
help="Path to hyperfluid_causal_report_with_carrier_state.json",
|
|
)
|
|
parser.add_argument(
|
|
"--graph_os-input",
|
|
default="metadata_report.json",
|
|
help="Graph OS metadata input (created from carrier_state report if not provided)",
|
|
)
|
|
parser.add_argument(
|
|
"--out-csv",
|
|
help="Output CSV for Graph OS risk scores (default: /tmp/graph_os_carrier_state_scores.csv)",
|
|
)
|
|
parser.add_argument(
|
|
"--execute",
|
|
action="store_true",
|
|
help="Actually execute remediation actions (default: dry-run)",
|
|
)
|
|
parser.add_argument(
|
|
"--out-json",
|
|
help="Save remediation actions to JSON",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Load carrier_state report
|
|
try:
|
|
carrier_state_report = load_carrier_state_report(args.carrier_state_report)
|
|
except FileNotFoundError:
|
|
print(f"CarrierState report not found: {args.carrier_state_report}")
|
|
return 2
|
|
except json.JSONDecodeError as e:
|
|
print(f"Invalid JSON in carrier_state report: {e}")
|
|
return 3
|
|
|
|
print(f"[Graph OS] Loaded carrier_state report: {args.carrier_state_report}")
|
|
print(f"[Graph OS] Found {len(carrier_state_report.get('chain_backtests', {}))} chains")
|
|
|
|
# Convert carrier_state findings to Graph OS metadata
|
|
metadata = convert_carrier_state_to_metadata(carrier_state_report)
|
|
print(f"[Graph OS] Converted to {len(metadata)} Graph OS metadata nodes")
|
|
|
|
# Run Graph OS risk scoring
|
|
try:
|
|
graph_os_summary, csv_path = run_graph_os_scoring(metadata, args.out_csv)
|
|
except Exception as e:
|
|
print(f"Graph OS scoring failed: {e}")
|
|
return 4
|
|
|
|
print(f"[Graph OS] Risk scoring complete → {csv_path}")
|
|
print(f"[Graph OS] Summary: {json.dumps(graph_os_summary.get('totals', {}), indent=2)}")
|
|
|
|
# Recommend remediations
|
|
actions = recommend_remediations(carrier_state_report, graph_os_summary)
|
|
print(f"\n[REMEDIATE] Generated {len(actions)} remediation recommendations:")
|
|
|
|
for action in actions:
|
|
status = "✓ EXECUTE" if args.execute else "→ DRY-RUN"
|
|
print(f" {status} {action.action.upper():12} {action.chain:20} {action.reason}")
|
|
|
|
# Execute if requested
|
|
if args.execute:
|
|
print("\n[REMEDIATE] Executing actions...")
|
|
for action in actions:
|
|
execute_remediation(action)
|
|
print(f" ✓ {action.action:12} → executed")
|
|
else:
|
|
print("\n[REMEDIATE] Dry-run mode. Add --execute to actually remediate.")
|
|
|
|
# Save actions to JSON if requested
|
|
if args.out_json:
|
|
actions_json = [asdict(a) for a in actions]
|
|
with open(args.out_json, "w") as f:
|
|
json.dump(
|
|
{
|
|
"timestamp": carrier_state_report.get("export_report", ""),
|
|
"graph_os_summary": graph_os_summary,
|
|
"remediations": actions_json,
|
|
},
|
|
f,
|
|
indent=2,
|
|
)
|
|
print(f"\n[REMEDIATE] Actions saved to: {args.out_json}")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|