|
| 1 | +import hashlib |
| 2 | +import json |
1 | 3 | import os # Forced reload triggered by agent |
2 | 4 | import sys |
3 | 5 | import types |
@@ -92,6 +94,56 @@ def _resolve_logo(): |
92 | 94 | return None, 50 |
93 | 95 |
|
94 | 96 |
|
| 97 | +def _run_local_scan(target_dir: str): |
| 98 | + """Scan the project directory to populate evidence for Phase 3.""" |
| 99 | + from pathlib import Path |
| 100 | + |
| 101 | + # 1. Evidence hash — SHA-256 of all .py files |
| 102 | + h = hashlib.sha256() |
| 103 | + py_files = sorted(Path(target_dir).rglob("*.py")) |
| 104 | + for pf in py_files: |
| 105 | + if ".venv" in pf.parts or "__pycache__" in pf.parts: |
| 106 | + continue |
| 107 | + try: |
| 108 | + h.update(pf.read_bytes()) |
| 109 | + except OSError: |
| 110 | + pass |
| 111 | + st.session_state["evidence_hash"] = h.hexdigest() |
| 112 | + |
| 113 | + # 2. BOM scan |
| 114 | + try: |
| 115 | + from venturalitica.scanner import BOMScanner |
| 116 | + |
| 117 | + scanner = BOMScanner(target_dir) |
| 118 | + bom_json = scanner.scan() |
| 119 | + bom_data = json.loads(bom_json) |
| 120 | + st.session_state["bom"] = bom_data |
| 121 | + st.session_state["bom_security"] = {"vulnerable": False, "issues": []} |
| 122 | + except Exception: |
| 123 | + st.session_state["bom"] = {} |
| 124 | + st.session_state["bom_security"] = {} |
| 125 | + |
| 126 | + # 3. Code context from latest trace (if available) |
| 127 | + runs_dir = Path(target_dir) / ".venturalitica" / "runs" |
| 128 | + if runs_dir.exists(): |
| 129 | + sessions = sorted( |
| 130 | + [d for d in runs_dir.iterdir() if d.is_dir() and d.name != "latest"], |
| 131 | + reverse=True, |
| 132 | + ) |
| 133 | + for session_dir in sessions: |
| 134 | + for trace_file in session_dir.glob("trace_*.json"): |
| 135 | + try: |
| 136 | + trace = json.loads(trace_file.read_text()) |
| 137 | + if trace.get("code_context"): |
| 138 | + st.session_state["code_context"] = trace["code_context"] |
| 139 | + st.session_state["runtime_meta"] = trace |
| 140 | + break |
| 141 | + except Exception: |
| 142 | + pass |
| 143 | + if st.session_state.get("code_context"): |
| 144 | + break |
| 145 | + |
| 146 | + |
95 | 147 | def render_dashboard(): |
96 | 148 | # --- HEADER --- |
97 | 149 | logo_path, logo_width = _resolve_logo() |
@@ -291,6 +343,14 @@ def format_phase(key): |
291 | 343 | # PHASE 3 & 4: SESSION CONTEXT REQUIRED |
292 | 344 | # ========================================================================= |
293 | 345 | else: |
| 346 | + # --- Refresh Local Scan button --- |
| 347 | + st.sidebar.markdown("### 🔬 Local Scan") |
| 348 | + if st.sidebar.button("🔄 Refresh Local Scan", use_container_width=True): |
| 349 | + with st.sidebar: |
| 350 | + with st.spinner("Scanning project..."): |
| 351 | + _run_local_scan(target_dir) |
| 352 | + st.rerun() |
| 353 | + |
294 | 354 | # Session Selector |
295 | 355 | st.sidebar.markdown("### 🕒 Session Context") |
296 | 356 | available_sessions = ["Global / History"] + list_available_sessions() |
|
0 commit comments