-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdash.py
More file actions
770 lines (574 loc) · 22.5 KB
/
Copy pathdash.py
File metadata and controls
770 lines (574 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#!/usr/bin/env python3
"""
REPRO / CARS Dashboard v1.1
===========================
Read-only Streamlit dashboard for CARS artifacts.
Features:
- Bill inventory
- Latest run detail view
- Mechanisms / Gates / Risk / Constitutional / Founders tabs
- Bill version comparison viewer
- Report comparison viewer
- Prompt package comparison viewer
- SQLite-aware, but falls back to filesystem artifacts
Run:
pip install streamlit pandas
streamlit run dashboard.py
If running in a userland VM:
streamlit run dashboard.py --server.address 0.0.0.0 --server.port 8501
"""
from __future__ import annotations
import json
import difflib
import sqlite3
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import pandas as pd
import streamlit as st
# ============================================================
# CONFIG
# ============================================================
BASE_DIR = Path.cwd()
ANALYSIS_DIR = BASE_DIR / "analysis"
RUNS_DIR = BASE_DIR / "runs"
RECORDS_DIR = BASE_DIR / "records"
DB_PATH = BASE_DIR / "repro_cars.db"
st.set_page_config(
page_title="REPRO CARS Dashboard",
layout="wide",
)
# ============================================================
# HELPERS
# ============================================================
def load_json(path: Path) -> Optional[Any]:
try:
if not path.exists():
return None
return json.loads(path.read_text(encoding="utf-8"))
except Exception:
return None
def load_text(path: Path) -> str:
try:
if not path.exists():
return ""
return path.read_text(encoding="utf-8")
except Exception:
return ""
def safe_path(value: Any) -> Optional[Path]:
if not value:
return None
try:
return Path(value)
except Exception:
return None
def path_exists_text(path: Optional[Path]) -> bool:
return bool(path and path.exists())
def json_preview(obj: Any):
st.json(obj if obj is not None else {})
def unified_text_diff(left: str, right: str, left_label: str, right_label: str) -> str:
left_lines = left.splitlines()
right_lines = right.splitlines()
diff = difflib.unified_diff(
left_lines,
right_lines,
fromfile=left_label,
tofile=right_label,
lineterm="",
)
return "\n".join(diff)
def get_nested(data: Dict[str, Any], *keys: str, default=None):
cur = data
for key in keys:
if not isinstance(cur, dict):
return default
cur = cur.get(key)
return cur if cur is not None else default
# ============================================================
# DATABASE / FILESYSTEM DISCOVERY
# ============================================================
def sqlite_available() -> bool:
return DB_PATH.exists()
def read_sql(query: str, params: Tuple = ()) -> pd.DataFrame:
if not sqlite_available():
return pd.DataFrame()
try:
with sqlite3.connect(DB_PATH) as conn:
return pd.read_sql_query(query, conn, params=params)
except Exception:
return pd.DataFrame()
def discover_runs_from_sqlite() -> pd.DataFrame:
if not sqlite_available():
return pd.DataFrame()
query = """
SELECT
pr.run_id,
pr.bill_id,
pr.session,
pr.prompt_version,
pr.runtime_version,
pr.report_version,
pr.model,
pr.system_version,
pr.prompt_package_path,
pr.created_at,
r.report_path,
r.brief_path,
r.risk_level,
r.risk_score,
r.structural_adherence_score
FROM prompt_runs pr
LEFT JOIN reports r
ON pr.run_id = r.run_id
ORDER BY pr.created_at DESC
"""
return read_sql(query)
def discover_runs_from_filesystem() -> pd.DataFrame:
rows = []
if RUNS_DIR.exists():
for bill_dir in RUNS_DIR.iterdir():
if not bill_dir.is_dir():
continue
bill_id = bill_dir.name
for run_dir in bill_dir.iterdir():
if not run_dir.is_dir():
continue
record = load_json(run_dir / "run_record.json") or {}
risk = load_json(run_dir / "risk.json") or {}
gates = load_json(run_dir / "gates.json") or {}
prompt_package = load_json(run_dir / "prompt_package.json") or {}
rows.append({
"run_id": record.get("run_id") or run_dir.name,
"bill_id": record.get("bill_id") or bill_id,
"session": record.get("session"),
"prompt_version": record.get("prompt_version") or prompt_package.get("prompt_version"),
"runtime_version": record.get("runtime_version") or prompt_package.get("runtime_version"),
"report_version": record.get("report_version") or prompt_package.get("report_version"),
"model": record.get("model") or prompt_package.get("model"),
"system_version": record.get("system_version") or prompt_package.get("system_version"),
"prompt_package_path": str(run_dir / "prompt_package.json"),
"created_at": record.get("generated_at") or prompt_package.get("created_at"),
"report_path": str(run_dir / "report.md"),
"brief_path": str(run_dir / "brief.md"),
"risk_level": risk.get("risk_level"),
"risk_score": risk.get("risk_score_0_to_10"),
"structural_adherence_score": get_nested(gates, "gate_summary", "structural_adherence_score"),
"run_dir": str(run_dir),
})
# Backward compatibility with older records/
if not rows and RECORDS_DIR.exists():
for record_path in RECORDS_DIR.glob("*_record.json"):
record = load_json(record_path) or {}
analysis_dir = Path(record.get("analysis_dir", ""))
risk = load_json(analysis_dir / "risk.json") or {}
gates = load_json(analysis_dir / "gates.json") or {}
rows.append({
"run_id": record.get("run_id") or record.get("bill_id"),
"bill_id": record.get("bill_id"),
"session": record.get("session"),
"prompt_version": record.get("prompt_version"),
"runtime_version": record.get("runtime_version"),
"report_version": record.get("report_version"),
"model": record.get("model"),
"system_version": record.get("system_version"),
"prompt_package_path": str(analysis_dir / "prompt_package.json"),
"created_at": record.get("generated_at"),
"report_path": str(analysis_dir / "report.md"),
"brief_path": str(analysis_dir / "brief.md"),
"risk_level": risk.get("risk_level"),
"risk_score": risk.get("risk_score_0_to_10"),
"structural_adherence_score": get_nested(gates, "gate_summary", "structural_adherence_score"),
"run_dir": str(analysis_dir),
})
return pd.DataFrame(rows)
def discover_runs() -> pd.DataFrame:
df = discover_runs_from_sqlite()
if df.empty:
df = discover_runs_from_filesystem()
if df.empty:
return df
# Ensure run_dir exists even when DB didn't store it.
if "run_dir" not in df.columns:
df["run_dir"] = df.apply(
lambda row: str(RUNS_DIR / str(row["bill_id"]) / str(row["run_id"])),
axis=1,
)
return df
def get_run_dir(row: pd.Series) -> Path:
run_dir = row.get("run_dir")
if run_dir:
p = Path(run_dir)
if p.exists():
return p
report_path = row.get("report_path")
if report_path:
return Path(report_path).parent
return RUNS_DIR / str(row["bill_id"]) / str(row["run_id"])
def get_runs_for_bill(df: pd.DataFrame, bill_id: str) -> pd.DataFrame:
return df[df["bill_id"] == bill_id].copy()
# ============================================================
# RENDERERS
# ============================================================
def render_kpis(row: pd.Series):
col1, col2, col3, col4, col5 = st.columns(5)
col1.metric("Bill", row.get("bill_id"))
col2.metric("Session", row.get("session"))
col3.metric("Risk Level", row.get("risk_level"))
col4.metric("Risk Score", row.get("risk_score"))
col5.metric("Structural Score", row.get("structural_adherence_score"))
def render_brief(run_dir: Path, row: pd.Series):
brief_path = safe_path(row.get("brief_path")) or (run_dir / "brief.md")
if path_exists_text(brief_path):
st.markdown(load_text(brief_path))
else:
st.info("No brief.md found for this run.")
def render_report(run_dir: Path, row: pd.Series):
report_path = safe_path(row.get("report_path")) or (run_dir / "report.md")
if path_exists_text(report_path):
st.markdown(load_text(report_path))
else:
st.info("No report.md found for this run.")
def render_mechanisms(run_dir: Path):
mechanisms = load_json(run_dir / "mechanisms.json") or {}
mech_list = mechanisms.get("mechanisms", [])
st.subheader(f"Mechanisms ({len(mech_list)})")
if not mech_list:
st.info("No mechanisms found.")
return
for mech in mech_list:
with st.expander(f"{mech.get('mechanism_id')}: {mech.get('name')}", expanded=False):
st.write(mech.get("description", ""))
st.markdown("**Actors**")
st.write(mech.get("actors", []))
st.markdown("**Decision Points**")
st.write(mech.get("decision_points", []))
st.markdown("**Authority Changes**")
st.write(mech.get("authority_changes", []))
st.markdown("**Funding Changes**")
st.write(mech.get("funding_changes", []))
st.markdown("**Enforcement Changes**")
st.write(mech.get("enforcement_changes", []))
st.markdown("**Practical Effects**")
st.write(mech.get("practical_effects", []))
st.markdown("**Citations**")
st.write(mech.get("citations", []))
st.markdown("**Raw JSON**")
st.json(mech)
def render_gates(run_dir: Path):
gates = load_json(run_dir / "gates.json") or {}
summary = gates.get("gate_summary", {})
st.subheader("Gate Summary")
c1, c2, c3 = st.columns(3)
c1.metric("Total Raw Points", summary.get("total_raw_points"))
c2.metric("Maximum Raw Points", summary.get("maximum_raw_points"))
c3.metric("Structural Score", summary.get("structural_adherence_score"))
st.write(summary.get("summary", ""))
st.subheader("Gate Reviews")
for review in gates.get("gate_reviews", []):
with st.expander(f"{review.get('mechanism_id')}: {review.get('mechanism_name')}", expanded=False):
rows = []
for gate in review.get("gates", []):
rows.append({
"gate": gate.get("gate_name"),
"outcome": gate.get("outcome"),
"points": gate.get("raw_points"),
"confidence": gate.get("confidence"),
"finding": gate.get("structural_finding"),
})
if rows:
st.dataframe(pd.DataFrame(rows), use_container_width=True)
st.json(review)
def render_risk(run_dir: Path):
risk = load_json(run_dir / "risk.json") or {}
c1, c2, c3 = st.columns(3)
c1.metric("Risk Level", risk.get("risk_level"))
c2.metric("Risk Score", risk.get("risk_score_0_to_10"))
c3.metric("Raw Points", risk.get("raw_risk_points"))
factors = risk.get("risk_factors", [])
st.subheader("Risk Factors")
if factors:
st.dataframe(pd.DataFrame(factors), use_container_width=True)
else:
st.info("No risk factors found.")
with st.expander("Raw risk.json"):
st.json(risk)
def render_constitutional(run_dir: Path):
data = load_json(run_dir / "constitutional.json") or {}
st.write(data.get("summary", ""))
issues = data.get("constitutional_review", [])
if issues:
st.dataframe(pd.DataFrame(issues), use_container_width=True)
else:
st.info("No constitutional issues found.")
with st.expander("Raw constitutional.json"):
st.json(data)
def render_founders(run_dir: Path):
data = load_json(run_dir / "founders.json") or {}
review = data.get("founders_review", {})
st.write(review.get("overall_founders_note", ""))
rows = []
for key, value in review.items():
if isinstance(value, dict):
rows.append({
"category": key,
"concern_level": value.get("concern_level"),
"finding": value.get("finding"),
"citations": value.get("citations"),
})
if rows:
st.dataframe(pd.DataFrame(rows), use_container_width=True)
with st.expander("Raw founders.json"):
st.json(data)
def render_versions(run_dir: Path):
versions = load_json(run_dir / "versions.json") or []
diff = load_json(run_dir / "diff.json") or {}
st.subheader("Available Bill Versions")
if versions:
view = []
for v in versions:
view.append({
"version": v.get("version"),
"text_length": v.get("text_length"),
"sections": v.get("section_count"),
"fingerprint": v.get("fingerprint"),
"url": v.get("url"),
})
st.dataframe(pd.DataFrame(view), use_container_width=True)
else:
st.info("No versions.json found.")
st.subheader("Version Comparisons")
comparisons = diff.get("comparisons", [])
if comparisons:
for comp in comparisons:
with st.expander(f"{comp.get('from_version')} → {comp.get('to_version')}", expanded=False):
c1, c2, c3 = st.columns(3)
c1.metric("Changed", comp.get("changed"))
c2.metric("Added Lines", comp.get("added_line_count"))
c3.metric("Removed Lines", comp.get("removed_line_count"))
st.markdown("**Added Preview**")
st.code("\n".join(comp.get("added_preview", [])) or "No additions previewed.")
st.markdown("**Removed Preview**")
st.code("\n".join(comp.get("removed_preview", [])) or "No removals previewed.")
else:
st.info("No diff comparisons found.")
with st.expander("Raw diff.json"):
st.json(diff)
def render_prompt_package(run_dir: Path):
prompt_config = load_json(run_dir / "prompt_config.json") or {}
prompt_package = load_json(run_dir / "prompt_package.json") or {}
st.subheader("Prompt / Runtime Metadata")
meta = {
"prompt_profile": prompt_package.get("prompt_profile"),
"prompt_version": prompt_package.get("prompt_version"),
"authority_layer_version": prompt_package.get("authority_layer_version"),
"runtime_version": prompt_package.get("runtime_version"),
"benchmark_layer_version": prompt_package.get("benchmark_layer_version"),
"report_version": prompt_package.get("report_version"),
"model": prompt_package.get("model"),
"system_version": prompt_package.get("system_version"),
}
st.json(meta)
st.subheader("Prompt Config")
st.json(prompt_config)
prompts = prompt_package.get("prompts", {})
selected_prompt = st.selectbox(
"Prompt module",
list(prompts.keys()) if prompts else [],
key="prompt_module_viewer",
)
if selected_prompt:
st.text_area(
f"{selected_prompt} prompt",
prompts.get(selected_prompt, ""),
height=500,
)
# ============================================================
# COMPARISON RENDERERS
# ============================================================
def render_report_compare(runs_df: pd.DataFrame, bill_id: str):
st.subheader("Report Comparison Viewer")
bill_runs = get_runs_for_bill(runs_df, bill_id)
if len(bill_runs) < 2:
st.info("At least two runs are needed for report comparison.")
return
run_options = bill_runs["run_id"].tolist()
col1, col2 = st.columns(2)
run_a = col1.selectbox("Run A", run_options, index=0, key="report_run_a")
run_b = col2.selectbox(
"Run B",
run_options,
index=1 if len(run_options) > 1 else 0,
key="report_run_b",
)
row_a = bill_runs[bill_runs["run_id"] == run_a].iloc[0]
row_b = bill_runs[bill_runs["run_id"] == run_b].iloc[0]
dir_a = get_run_dir(row_a)
dir_b = get_run_dir(row_b)
report_a = load_text(dir_a / "report.md")
report_b = load_text(dir_b / "report.md")
c1, c2 = st.columns(2)
with c1:
st.markdown(f"### {run_a}")
st.markdown(report_a if report_a else "No report.md")
with c2:
st.markdown(f"### {run_b}")
st.markdown(report_b if report_b else "No report.md")
st.subheader("Unified Diff")
st.code(
unified_text_diff(report_a, report_b, run_a, run_b)[:100000],
language="diff",
)
def flatten_prompt_metadata(package: Dict[str, Any]) -> Dict[str, Any]:
return {
"system_version": package.get("system_version"),
"prompt_profile": package.get("prompt_profile"),
"prompt_version": package.get("prompt_version"),
"authority_layer_version": package.get("authority_layer_version"),
"runtime_version": package.get("runtime_version"),
"benchmark_layer_version": package.get("benchmark_layer_version"),
"report_version": package.get("report_version"),
"model": package.get("model"),
}
def render_prompt_compare(runs_df: pd.DataFrame, bill_id: str):
st.subheader("Prompt Package Comparison Viewer")
bill_runs = get_runs_for_bill(runs_df, bill_id)
if len(bill_runs) < 2:
st.info("At least two runs are needed for prompt comparison.")
return
run_options = bill_runs["run_id"].tolist()
col1, col2 = st.columns(2)
run_a = col1.selectbox("Prompt Run A", run_options, index=0, key="prompt_run_a")
run_b = col2.selectbox(
"Prompt Run B",
run_options,
index=1 if len(run_options) > 1 else 0,
key="prompt_run_b",
)
row_a = bill_runs[bill_runs["run_id"] == run_a].iloc[0]
row_b = bill_runs[bill_runs["run_id"] == run_b].iloc[0]
dir_a = get_run_dir(row_a)
dir_b = get_run_dir(row_b)
package_a = load_json(dir_a / "prompt_package.json") or {}
package_b = load_json(dir_b / "prompt_package.json") or {}
st.markdown("### Metadata Comparison")
meta_df = pd.DataFrame([
{"field": k, "Run A": v, "Run B": flatten_prompt_metadata(package_b).get(k)}
for k, v in flatten_prompt_metadata(package_a).items()
])
st.dataframe(meta_df, use_container_width=True)
prompts_a = package_a.get("prompts", {})
prompts_b = package_b.get("prompts", {})
modules = sorted(set(prompts_a.keys()) | set(prompts_b.keys()))
selected = st.selectbox("Prompt module to compare", modules, key="prompt_compare_module")
if selected:
a_text = prompts_a.get(selected, "")
b_text = prompts_b.get(selected, "")
c1, c2 = st.columns(2)
with c1:
st.markdown(f"### Run A: {selected}")
st.text_area("A", a_text, height=400, label_visibility="collapsed")
with c2:
st.markdown(f"### Run B: {selected}")
st.text_area("B", b_text, height=400, label_visibility="collapsed")
st.markdown("### Prompt Diff")
st.code(
unified_text_diff(a_text, b_text, f"{run_a}:{selected}", f"{run_b}:{selected}")[:100000],
language="diff",
)
def render_run_compare_summary(runs_df: pd.DataFrame, bill_id: str):
st.subheader("Run Comparison Summary")
bill_runs = get_runs_for_bill(runs_df, bill_id)
if bill_runs.empty:
st.info("No runs found.")
return
columns = [
"run_id",
"created_at",
"prompt_version",
"runtime_version",
"report_version",
"model",
"risk_level",
"risk_score",
"structural_adherence_score",
]
existing = [c for c in columns if c in bill_runs.columns]
st.dataframe(bill_runs[existing], use_container_width=True)
# ============================================================
# MAIN APP
# ============================================================
st.title("REPRO CARS Dashboard")
runs_df = discover_runs()
if runs_df.empty:
st.warning("No CARS runs found. Run cars_v2_3_fixed.py first.")
st.stop()
# Sidebar filters
st.sidebar.header("Filters")
bill_ids = sorted(runs_df["bill_id"].dropna().unique().tolist())
selected_bill = st.sidebar.selectbox("Bill", bill_ids)
bill_runs = get_runs_for_bill(runs_df, selected_bill)
run_ids = bill_runs["run_id"].tolist()
selected_run = st.sidebar.selectbox("Run", run_ids)
selected_row = bill_runs[bill_runs["run_id"] == selected_run].iloc[0]
selected_run_dir = get_run_dir(selected_row)
st.caption(f"Run directory: `{selected_run_dir}`")
render_kpis(selected_row)
main_tabs = st.tabs([
"Inventory",
"Brief",
"Mechanisms",
"Gates",
"Risk",
"Founders",
"Constitutional",
"Bill Versions",
"Prompt Package",
"Comparisons",
"Full Report",
])
with main_tabs[0]:
st.subheader("All Runs")
show_cols = [
"bill_id",
"run_id",
"session",
"created_at",
"prompt_version",
"runtime_version",
"report_version",
"model",
"risk_level",
"risk_score",
"structural_adherence_score",
]
existing = [c for c in show_cols if c in runs_df.columns]
st.dataframe(runs_df[existing], use_container_width=True)
with main_tabs[1]:
render_brief(selected_run_dir, selected_row)
with main_tabs[2]:
render_mechanisms(selected_run_dir)
with main_tabs[3]:
render_gates(selected_run_dir)
with main_tabs[4]:
render_risk(selected_run_dir)
with main_tabs[5]:
render_founders(selected_run_dir)
with main_tabs[6]:
render_constitutional(selected_run_dir)
with main_tabs[7]:
render_versions(selected_run_dir)
with main_tabs[8]:
render_prompt_package(selected_run_dir)
with main_tabs[9]:
compare_tabs = st.tabs([
"Run Summary",
"Report Compare",
"Prompt Compare",
])
with compare_tabs[0]:
render_run_compare_summary(runs_df, selected_bill)
with compare_tabs[1]:
render_report_compare(runs_df, selected_bill)
with compare_tabs[2]:
render_prompt_compare(runs_df, selected_bill)
with main_tabs[10]:
render_report(selected_run_dir, selected_row)