File size: 56,240 Bytes
f0b4759
 
73d44ac
f0b4759
 
 
 
 
 
 
a0e9e53
f0b4759
 
a0e9e53
6e01804
a0e9e53
 
f0b4759
 
 
 
 
429f4ac
 
f0b4759
a0e9e53
 
f0b4759
1e16216
 
 
 
a0e9e53
f0b4759
73d44ac
 
 
 
 
 
 
 
 
 
f0b4759
73d44ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e16216
73d44ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b4759
 
 
73d44ac
 
 
 
 
 
 
 
 
 
 
 
 
1e16216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73d44ac
 
 
 
 
 
 
f0b4759
 
a0e9e53
f0b4759
1e16216
 
f0b4759
 
 
 
1e16216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b4759
1e16216
 
f0b4759
 
1e16216
f0b4759
1e16216
 
 
 
 
 
 
 
f0b4759
1e16216
f0b4759
 
1e16216
f0b4759
 
 
 
1e16216
 
 
 
 
 
 
 
 
94bc1eb
 
1e16216
 
 
 
f0b4759
1e16216
 
 
 
f0b4759
 
 
 
73d44ac
f0b4759
1e16216
 
f0b4759
 
 
1e16216
 
 
 
 
 
f0b4759
 
 
 
 
 
1e16216
f0b4759
1e16216
 
f0b4759
 
 
 
1e16216
 
f0b4759
1e16216
 
 
94bc1eb
 
 
 
f0b4759
 
 
 
 
 
 
 
 
a0e9e53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b4759
 
 
a0e9e53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b4759
 
 
 
 
 
1e16216
f0b4759
 
 
 
 
 
 
1e16216
 
f0b4759
 
 
 
 
 
 
 
 
 
a0e9e53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e01804
 
 
1e16216
 
 
 
 
 
6e01804
1e16216
 
 
 
 
 
 
 
 
6e01804
f0b4759
1e16216
 
f0b4759
1e16216
 
f0b4759
1e16216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b4759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73d44ac
f0b4759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e16216
 
f0b4759
 
73d44ac
f0b4759
 
 
 
 
 
 
 
 
a0e9e53
 
 
 
 
 
1e16216
 
 
a0e9e53
 
f0b4759
 
 
 
 
 
 
 
 
 
429f4ac
 
 
 
1e16216
429f4ac
 
 
 
 
 
 
1e16216
429f4ac
 
 
 
 
 
 
 
 
 
a0e9e53
429f4ac
 
 
 
 
 
 
 
 
 
a0e9e53
 
 
1e16216
 
f0b4759
 
 
 
 
1e16216
f0b4759
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
"""Run with 'streamlit run app.py'."""
from __future__ import annotations
import base64
import json
from pathlib import Path
import streamlit as st
from frontierlab.demo_data import QUESTION, QUERY
from frontierlab.quantitative import inverse_variance_heterogeneity
from frontierlab.openweight import OpenWeightClient
from frontierlab.pipeline import make_plan, run_research, continue_after_review
from frontierlab.planning_readiness import build_planning_readiness, readiness_summary
from frontierlab.review import review_packet, save_review
from frontierlab.reporting import export_json, summary
from frontierlab.process_metrics import process_metrics
from frontierlab.operating_status import research_operating_status
from frontierlab.trajectory_graph import build_trajectory_graph, summarize as trajectory_summary, to_json as trajectory_json
from frontierlab.interventions import record_human_event
from frontierlab.claim_benchmark import benchmark
from frontierlab.claim_verification import audit_predictions, conflict_transparency
from frontierlab.integrity_trials import suite_report
from frontierlab.evaluation_ui import render_evaluation
from frontierlab.evaluation import require_evaluation
from frontierlab.evidence_graph import (approve_plan_step, refresh_evidence_graph, step_gate,
                                        summarize as graph_summary, to_json as graph_json, to_svg as graph_svg)
from frontierlab.literature_pilot import load_dataset, reviewer_packet
from frontierlab.recorded_gate_ablation import run_recorded_pilot
from frontierlab.pilot_review import canonicalize_review_upload, labels_from_review_packet
from frontierlab.research_backend import NvidiaResearchClient, research_enabled, remaining_calls
from frontierlab import hf_providers
from frontierlab.hf_providers import HFProvidersUnavailable
from frontierlab.recorded_replay import build_recorded_replay
from frontierlab.evidence import europepmc_error_message
from frontierlab.version import __version__



ASSETS = Path(__file__).resolve().parent / "docs" / "assets"
_icon_path = ASSETS / "flatland-sphere-icon.png"
st.set_page_config(page_title="FrontierLab | Research Intelligence",
                   page_icon=str(_icon_path) if _icon_path.exists() else "◯", layout="wide")

# Presentation only: black-and-white "Flatland book plate" styling (see docs/assets/).
SERIF = "Georgia, 'Times New Roman', Times, serif"
st.markdown(f"""
<style>
:root {{ --fl-ink: #F2F2F2; --fl-soft: #CFCFCF; --fl-bg: #1E1E1E; --fl-bg2: #262626; --fl-line: rgba(242,242,242,0.55); }}
.stApp {{ background: var(--fl-bg); color: var(--fl-ink); }}
.stApp, .stApp p, .stApp li, .stApp label, .stApp input, .stApp textarea, .stApp button,
.stApp h1, .stApp h2, .stApp h3, .stApp h4, .stApp summary p, [data-testid="stMetricValue"],
[data-testid="stMetricLabel"], [data-testid="stCaptionContainer"] {{ font-family: {SERIF}; }}
[data-testid="stDecoration"] {{ display: none; }}
[data-testid="stHeader"] {{ background: transparent; }}
.block-container {{ padding-top: 2rem; max-width: 1320px; }}
h1 {{ font-weight: 400; font-style: italic; letter-spacing: 0.02em; }}
h2, h3 {{ font-weight: 400; letter-spacing: 0.04em; }}
[data-testid="stCaptionContainer"], .stCaption {{ color: var(--fl-soft); }}
hr {{ border-color: var(--fl-line) !important; }}
a {{ color: var(--fl-ink) !important; text-decoration: underline; text-underline-offset: 3px; }}
/* hero banner and section bands */
.fl-hero {{ display: block; width: 100%; height: auto; border: 1px solid var(--fl-line); margin: 0 0 1.2rem 0; }}
.fl-band {{ display: flex; align-items: center; gap: 0.6rem; margin: 1.6rem 0 0.4rem 0;
           padding-bottom: 0.35rem; border-bottom: 1px solid var(--fl-line);
           font-variant: small-caps; letter-spacing: 0.22em; font-size: 0.85rem; color: var(--fl-ink); }}
.fl-band svg {{ flex: none; }}
/* outlined white boxes instead of filled cards */
[data-testid="stMetric"] {{ background: transparent; padding: 14px 16px; border: 1px solid var(--fl-ink); border-radius: 0; }}
[data-testid="stSidebar"] {{ background: #1A1A1A; border-right: 1px solid var(--fl-line); }}
[data-testid="stExpander"] details {{ border: 1px solid var(--fl-line); border-radius: 0; background: transparent; }}
[data-testid="stForm"] {{ border: 1px solid var(--fl-line); border-radius: 0; }}
.st-key-judge_path {{ border: 1px solid var(--fl-ink) !important; border-radius: 0 !important; }}
[data-testid="stAlert"] > div, [data-testid="stAlertContainer"] {{ background: transparent !important;
           border: 1px solid var(--fl-line); border-radius: 0; color: var(--fl-ink); }}
[data-testid="stAlertContentWarning"], [data-testid="stAlertContentError"],
[data-testid="stAlertContentSuccess"], [data-testid="stAlertContentInfo"] {{ color: var(--fl-ink); }}
[data-testid="stAlert"]:has([data-testid="stAlertContentWarning"]) > div {{ border-style: dashed; border-color: var(--fl-ink); }}
[data-testid="stAlert"]:has([data-testid="stAlertContentError"]) > div {{ border: 2px solid var(--fl-ink); }}
[data-testid="stAlert"]:has([data-testid="stAlertContentSuccess"]) > div {{ border-left: 4px double var(--fl-ink); }}
[data-testid="stTextAreaRootElement"], [data-testid="stTextInputRootElement"], [data-testid="stNumberInputContainer"],
[data-testid="stSelectbox"] .react-aria-ComboBox > [role="group"], [data-testid="stMultiSelect"] .react-aria-ComboBox > [role="group"] {{
           background: transparent !important; border: 1px solid var(--fl-line) !important; border-radius: 0 !important; }}
[data-testid="stTextAreaRootElement"]:focus-within, [data-testid="stTextInputRootElement"]:focus-within,
[data-testid="stNumberInputContainer"]:focus-within {{ border-color: var(--fl-ink) !important; }}
[data-testid="stMultiSelectTagsContainer"] span[data-tag] {{
           background: transparent !important; color: var(--fl-ink) !important; border: 1px solid var(--fl-ink); border-radius: 0; }}
[data-testid="stMultiSelectTagsContainer"] span[data-tag] * {{ color: var(--fl-ink) !important; fill: var(--fl-ink); }}
/* checkboxes: outlined square, white tick on charcoal (never white-on-white) */
[data-testid="stCheckbox"] label > div:first-of-type {{ background: transparent !important; border: 1px solid var(--fl-ink) !important; border-radius: 0 !important; }}
[data-testid="stCheckbox"] label > div:first-of-type svg polyline {{ stroke: var(--fl-ink) !important; }}
[data-testid="stCode"] pre, [data-testid="stJson"] {{ border: 1px solid var(--fl-line); border-radius: 0; }}
/* JSON viewer: monochrome (keys soft, values ink) */
[data-testid="stJson"] span {{ color: var(--fl-ink) !important; }}
[data-testid="stJson"] .object-key, [data-testid="stJson"] .object-key span {{ color: var(--fl-soft) !important; }}
[data-testid="stJson"] .object-key-val {{ border-left-color: rgba(242,242,242,0.25) !important; }}
[data-testid="stJson"] svg {{ color: var(--fl-ink) !important; fill: var(--fl-ink) !important; }}
/* buttons: outlined white boxes that invert on hover */
.stApp :is([data-testid="stBaseButton-primary"], [data-testid="stBaseButton-secondary"], [data-testid="stBaseButton-primaryFormSubmit"], [data-testid="stBaseButton-secondaryFormSubmit"]) {{ background: transparent; color: var(--fl-ink); border: 1px solid var(--fl-ink);
           border-radius: 0; box-shadow: none; letter-spacing: 0.06em; }}
.stApp :is([data-testid="stBaseButton-primary"], [data-testid="stBaseButton-secondary"], [data-testid="stBaseButton-primaryFormSubmit"], [data-testid="stBaseButton-secondaryFormSubmit"]) p {{ color: inherit; }}
.stApp :is([data-testid="stBaseButton-primary"], [data-testid="stBaseButton-secondary"], [data-testid="stBaseButton-primaryFormSubmit"], [data-testid="stBaseButton-secondaryFormSubmit"]):hover:not(:disabled), .stApp :is([data-testid="stBaseButton-primary"], [data-testid="stBaseButton-secondary"], [data-testid="stBaseButton-primaryFormSubmit"], [data-testid="stBaseButton-secondaryFormSubmit"]):focus-visible {{
           background: var(--fl-ink); color: var(--fl-bg); border-color: var(--fl-ink); }}
.stApp :is([data-testid="stBaseButton-primary"], [data-testid="stBaseButton-secondary"], [data-testid="stBaseButton-primaryFormSubmit"], [data-testid="stBaseButton-secondaryFormSubmit"]):disabled {{ background: transparent; color: var(--fl-soft); border: 1px dashed var(--fl-line); opacity: 0.7; }}
</style>
""", unsafe_allow_html=True)

_ICON = 'width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#F2F2F2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"'
BAND_ICONS = {
    "point": f'<svg {_ICON}><line x1="2" y1="18" x2="22" y2="18" stroke-width="1"/><circle cx="12" cy="11" r="3" fill="#F2F2F2" stroke="none"/></svg>',
    "sphere": f'<svg {_ICON}><circle cx="12" cy="12" r="10" stroke-width="1.5"/><ellipse cx="12" cy="12" rx="10" ry="3" stroke-width="0.8" stroke-dasharray="2 2"/><ellipse cx="12" cy="12" rx="3.6" ry="10" stroke-width="0.8"/></svg>',
    "cube": f'<svg {_ICON}><rect x="3" y="8" width="13" height="13" stroke-width="1.4"/><polyline points="3,8 8,3 21,3 16,8" stroke-width="1.4"/><polyline points="21,3 21,16 16,21" stroke-width="1.4"/></svg>',
}


def band(label: str, icon: str) -> None:
    """Small-caps section band, as on the README architecture plate."""
    st.markdown(f'<div class="fl-band">{BAND_ICONS[icon]}<span>{label}</span></div>', unsafe_allow_html=True)


def _pct(value) -> str:
    return "N/A" if value is None else f"{value * 100:.0f}%"


def render_operating_status(roi: dict, *, key: str, download_label: str, file_name: str,
                            synthetic_note: str | None = None) -> None:
    """Research Operating Interface body; shared by live runs and the recorded replay."""
    trace = roi["traceability"]
    source_cov = trace["source_trace_coverage"]
    strict_cov = trace["strict_extracted_coverage"]
    o1, o2, o3, o4 = st.columns(4)
    o1.metric("Next required stage", roi["current_activity"]["next_required_stage"].title())
    o2.metric("Source trace coverage", _pct(source_cov))
    o3.metric("Strict extracted coverage", _pct(strict_cov))
    o4.metric("Auto repair", "OFF")
    if synthetic_note:
        st.caption(synthetic_note)
    st.caption(
        "Trace Coverage is measured from the current Evidence Graph. "
        "It measures provenance coverage, not scientific accuracy or semantic truth."
    )
    if source_cov is not None and strict_cov is not None and strict_cov < source_cov:
        st.info(
            "Some artifacts can be traced to a source but do not have an all-EXTRACTED path. "
            "FrontierLab preserves those provenance disagreements instead of promoting them to strict evidence."
        )
        blocked_rows = []
        for artifact in trace["artifacts"]:
            if artifact["strict_extracted"]:
                continue
            if artifact["blocking_edges"]:
                for edge in artifact["blocking_edges"]:
                    blocked_rows.append({
                        "Artifact": artifact["artifact_id"],
                        "Type": artifact["artifact_type"],
                        "Blocking provenance": edge["provenance"],
                        "Reason": edge["reason"],
                    })
            elif artifact["unresolved_evidence_ids"] or artifact["dead_ends"]:
                blocked_rows.append({
                    "Artifact": artifact["artifact_id"],
                    "Type": artifact["artifact_type"],
                    "Blocking provenance": "UNRESOLVED",
                    "Reason": (
                        f"unknown sources={artifact['unresolved_evidence_ids']} · "
                        f"dead ends={artifact['dead_ends']}"
                    ),
                })
        if blocked_rows:
            st.dataframe(blocked_rows, hide_index=True, width="stretch")
    a1, a2, a3, a4 = st.columns(4)
    audit_state = roi["source_audit"]
    a1.metric("Active papers", audit_state["papers"])
    a2.metric("Anchored passed claims", audit_state["anchored_passed_claims"])
    a3.metric(
        "Rejected claims",
        audit_state["rejected_claims_attached"] + audit_state["rejected_claims_unattached"],
    )
    a4.metric("Human events", roi["governance"]["human_events_recorded"])
    st.dataframe(
        [
            {
                "Control area": "Provenance",
                "Status": "MEASURED",
                "Detail": (
                    f"{trace['source_traceable_artifacts']} / {trace['material_artifacts']} "
                    "in-scope artifacts reach a known paper"
                ),
            },
            {
                "Control area": "Governance",
                "Status": "HUMAN-GATED",
                "Detail": (
                    f"Claim review: {roi['governance']['claim_review_status']} · "
                    f"Method evaluation: {'saved' if roi['governance']['method_evaluation_saved'] else 'not saved'} · "
                    f"Planning authorization recorded: "
                    f"{'yes' if roi['governance']['planning_authorized_recorded'] else 'no'}"
                ),
            },
            {
                "Control area": "Maintainability",
                "Status": roi["maintainability"]["status"],
                "Detail": "Scheduled dependency canary and advisory repair proposals are active; proposals cannot edit code/dependencies, commit, merge or deploy. Autonomous repair and environment rebuild are not implemented.",
            },
        ],
        hide_index=True,
        width="stretch",
    )
    if roi["active_sources"]:
        st.caption(
            "Active sources: " + ", ".join(source["paper_id"] for source in roi["active_sources"])
        )
    st.download_button(
        download_label,
        data=json.dumps(roi, ensure_ascii=False, indent=2, sort_keys=True),
        file_name=file_name,
        mime="application/json",
        key=f"{key}_roi_download",
    )


def render_planning_readiness(readiness: dict, ready_stats: dict, *, key: str,
                              download_label: str, file_name: str) -> None:
    """Planning-readiness body; shared by live runs and the recorded replay."""
    st.warning(
        "READY ≠ APPROVED. This packet validates the current reviewed context and "
        "selected method, but it does not authorize or call the Planning Agent."
    )
    p1, p2, p3, p4 = st.columns(4)
    p1.metric("Selected method", ready_stats["selected_candidate_id"])
    p2.metric("Open reproducibility checks", len(ready_stats["open_reproducibility_checks"]))
    p3.metric("Required planning obligations", ready_stats["required_obligations"])
    p4.metric("Planning approved", "YES" if ready_stats["planning_approved"] else "NO")
    st.caption(
        "Open checks: " + ", ".join(ready_stats["open_reproducibility_checks"]) +
        ". The future plan must preserve evidence/citation gates and address all obligations."
    )
    st.dataframe(
        [{
            "Obligation": item["obligation_id"],
            "Category": item["category"],
            "Status": item["status"],
            "Requirement": item["requirement"],
        } for item in readiness["planning_obligations"]],
        hide_index=True,
        width="stretch",
    )
    st.download_button(
        download_label,
        data=json.dumps(readiness, ensure_ascii=False, indent=2, sort_keys=True),
        file_name=file_name,
        mime="application/json",
        key=f"{key}_readiness_download",
    )


def render_graph_overview(graph: dict, g_stats: dict) -> None:
    """Evidence-graph legend, edge counts, drawing and surfaced links (read-only)."""
    st.caption("EXTRACTED: passage located in the cited abstract and source IDs checked (not semantic entailment). "
               "INFERRED: model stance without an anchored quote. AMBIGUOUS: conflicting labels. "
               "Only all-EXTRACTED paths to a paper count as evidence.")
    e_count = g_stats["edges_by_provenance"]
    x, y, z = st.columns(3)
    x.metric("EXTRACTED edges", e_count["EXTRACTED"])
    y.metric("INFERRED edges (not evidence)", e_count["INFERRED"])
    z.metric("AMBIGUOUS edges (not evidence)", e_count["AMBIGUOUS"])
    drawing = graph_svg(graph)
    if drawing:
        st.markdown('<img class="fl-hero" alt="Evidence provenance graph: solid lines EXTRACTED, dashed INFERRED, dotted AMBIGUOUS" '
                    f'src="data:image/svg+xml;base64,{base64.b64encode(drawing.encode("utf-8")).decode("ascii")}">',
                    unsafe_allow_html=True)
    else:
        st.caption("Graph too large to draw legibly; use the tables and graph.json below.")
    if g_stats["surfaced_for_review"]:
        st.warning("Surfaced for researcher review. These links are NOT counted as evidence.")
        st.dataframe([{"Provenance": e["provenance"], "From": e["source"], "Relation": e["relation"],
                       "To": e["target"], "Why": e["reason"]} for e in g_stats["surfaced_for_review"]],
                     hide_index=True, width="stretch")


def render_trajectory(trajectory: dict, t_stats: dict, *, key: str, model_calls_label: str,
                      download_label: str, file_name: str) -> None:
    """Trajectory audit graph body; shared by live runs and the recorded replay."""
    st.caption(
        "Audit-only process graph. It records model calls, human interventions, "
        "method evaluation, rejected claims and process signals. It never participates "
        "in the scientific evidence gate."
    )
    t1, t2, t3, t4 = st.columns(4)
    t1.metric(model_calls_label, t_stats["model_calls"])
    t2.metric("Human events", t_stats["human_events"])
    t3.metric("Rejected claims", t_stats["rejected_claims"])
    t4.metric("Process signals", t_stats["process_signals"])
    st.dataframe(
        [{"Node type": k, "Count": v} for k, v in t_stats["nodes_by_type"].items()],
        hide_index=True, width="stretch"
    )
    st.download_button(
        download_label,
        data=trajectory_json(trajectory),
        file_name=file_name,
        mime="application/json",
        key=f"{key}_trajectory_download",
    )


_hero_path = ASSETS / "flatland-hero.svg"
if _hero_path.exists():
    _hero = base64.b64encode(_hero_path.read_bytes()).decode("ascii")
    st.markdown(f'<img class="fl-hero" src="data:image/svg+xml;base64,{_hero}" '
                'alt="FrontierLab: From Flatland to Spaceland. Role shapes rise from a flat plane toward a sphere.">',
                unsafe_allow_html=True)

st.caption("META GLOBAL AI DEVELOPER HACKATHON 2026  /  INDIE DEVELOPER  /  PUSH THE FRONTIER")
st.title("FrontierLab  /  Research Intelligence")
st.caption(f"Research → Evidence → Hypothesis → Evaluation → Planning · v{__version__}")
deployment_path = Path(__file__).resolve().parent / "deployment.json"
on_public_space = deployment_path.exists()
if on_public_space:
    deployment = json.loads(deployment_path.read_text(encoding="utf-8"))
    st.caption(f"Deployed source: {deployment['source_sha'][:12]} · {deployment['version']} · {deployment['space_id']}")
st.write("An evidence-grounded scientific research workflow that **records disagreement instead of hiding it**.")

# Recorded real M2 core (2026-10-28): recomputed from docs/runs/*.json on every render.
# Zero model calls and zero network requests; see frontierlab/recorded_replay.py.
try:
    replay = build_recorded_replay()
    replay_error = None
except (OSError, ValueError, KeyError, TypeError, PermissionError) as exc:
    replay, replay_error = None, exc

REPLAY_TITLE = "Replay recorded real M2 case (no model call)"
SYNTHETIC_BANNER = (
    "SYNTHETIC / FICTIONAL MODE. Every publication, effect size, benchmark case, failure injection, "
    "method strategy and plan in this mode is invented for testing. None of it is real scientific "
    "evidence or a literature citation, and no model or laboratory experiment runs. It exists to show "
    "the gates failing closed. For real recorded results, see “" + REPLAY_TITLE + "” at the top."
)

with st.container(border=True, key="judge_path"):
    st.markdown(
        "**Judge path · start here (about 3 minutes)**\n\n"
        f"1. **Read the recorded real case.** “{REPLAY_TITLE}” is open just below: two published "
        "abstracts, real Meta Muse Glimmer outputs recorded on 2026-10-28, researcher-reviewed. "
        "Every number is recomputed from the recorded files; nothing calls a model.\n"
        "2. **Run the gates yourself.** Keep *Synthetic audit demonstration* selected in the sidebar and "
        "click **Run research audit**. The data is fictional and the gates fail closed by design.\n"
        "3. **Inspect provenance.** In either path, open *Research Operating Interface*, *Evidence graph* "
        "and *Trajectory*, and download the JSON receipts."
    )

with st.expander("Start here · " + REPLAY_TITLE, expanded=True):
    if replay is None:
        st.error(f"Recorded replay unavailable: {type(replay_error).__name__}: {replay_error}")
    else:
        r_calls = replay["recorded_calls"]
        r_run = replay["run"]
        r_sel = replay["selection"]
        r_trace = replay["trace_coverage"]
        r_ready = replay["readiness_summary"]
        st.info(
            f"RECORDED REAL RUN · REPLAY ONLY. Recorded on {r_calls['recorded_date_taipei']} (Asia/Taipei): "
            f"{r_calls['extraction_calls']} Evidence Extraction calls and {r_calls['continuation_calls']} "
            f"reviewed-continuation calls ({' → '.join(r_calls['continuation_roles'])}) to {r_calls['model']} "
            f"via {r_calls['provider']} on {len(r_run.papers)} published abstracts; claims reviewed and method "
            f"{r_sel['selected_candidate_id']} selected by {r_sel['reviewer']}. This panel recomputes every number "
            f"from docs/runs/*.json with the same code used for live runs and makes "
            f"{replay['new_model_calls']} model calls. Coverage measures provenance, not scientific accuracy. "
            "Planning is NOT approved."
        )
        k1, k2, k3, k4 = st.columns(4)
        k1.metric("Source trace coverage", _pct(r_trace["source_trace_coverage"]))
        k2.metric("Strict EXTRACTED coverage", _pct(r_trace["strict_extracted_coverage"]))
        k3.metric("Planning obligations", r_ready["required_obligations"])
        k4.metric("Model calls in this replay", replay["new_model_calls"])
        st.caption(
            f"{r_trace['source_traceable_artifacts']} / {r_trace['material_artifacts']} artifacts reach a known paper; "
            f"{r_trace['strict_extracted_artifacts']} / {r_trace['material_artifacts']} have an all-EXTRACTED path. "
            "Sources: " + ", ".join(p.paper_id for p in r_run.papers) + ". "
            f"Recorded runs: [extraction]({r_calls['extraction_run_url']}) · "
            f"[Skeptic → Hypothesis]({r_calls['continuation_run_url']})."
        )
        rt1, rt2, rt3, rt4, rt5 = st.tabs(["Research Operating Interface", "Planning readiness",
                                           "Evidence graph", "Trajectory", "Skeptic → Hypothesis"])
        with rt1:
            render_operating_status(replay["operating_status"], key="replay",
                                    download_label="Download recorded operating-status JSON",
                                    file_name="research_operating_status_recorded_20261028.json")
        with rt2:
            render_planning_readiness(replay["planning_readiness"], r_ready, key="replay",
                                      download_label="Download recorded planning-readiness JSON",
                                      file_name="planning_readiness_recorded_20261028.json")
        with rt3:
            render_graph_overview(replay["evidence_graph"], replay["graph_summary"])
            st.caption("No plan exists in the recorded case: planning approval was never granted.")
            st.download_button("Download recorded graph.json", data=graph_json(replay["evidence_graph"]),
                               file_name="graph_recorded_20261028.json", mime="application/json",
                               key="replay_graph_download")
        with rt4:
            render_trajectory(replay["trajectory"], replay["trajectory_summary"], key="replay",
                              model_calls_label="Model calls in recorded receipt",
                              download_label="Download recorded trajectory.json",
                              file_name="trajectory_recorded_20261028.json")
            st.caption(
                "The trajectory is rebuilt from the extraction receipt (recorded 2026-10-28). The "
                f"{r_calls['continuation_calls']} continuation calls are preserved separately in "
                "docs/runs/nim_reviewed_continuation_20261028.json. No call is made during replay."
            )
        with rt5:
            st.caption("Recorded verbatim on 2026-10-28 after researcher claim review. Not independently "
                       "verified; scientific accuracy is not measured.")
            st.markdown("**Adversarial Skeptic Agent (recorded)**")
            st.json(r_run.skeptic, expanded=False)
            st.markdown("**Hypothesis Agent (recorded)**")
            st.json(r_run.hypotheses, expanded=False)

LOCAL_MODE = "Live Europe PMC + local Meta model"
NIM_MODE = "Live Europe PMC + Meta via NVIDIA"
HF_MODE = hf_providers.MODE_LABEL
MODE_LABELS = ["Synthetic audit demonstration", "Live Europe PMC metadata (no model calls)", LOCAL_MODE, NIM_MODE,
               HF_MODE]
hf_available, hf_reason = hf_providers.live_status()


def _mode_display(label: str) -> str:
    if label == LOCAL_MODE and on_public_space:
        return label + " · not available on the public Space"
    if label == NIM_MODE and not research_enabled():
        return label + " · not available on the public Space (backend not enabled)"
    if label == HF_MODE and not hf_available:
        return label + " · unavailable (no Hugging Face token configured)"
    return label


nim_authorized = False
hf_consent = False
hf_session_budget = st.session_state.setdefault("hf_session_budget", hf_providers.SessionBudget())
with st.sidebar:
    st.header("Engine")
    mode_label = st.radio("Evidence mode", MODE_LABELS, index=0, format_func=_mode_display)
    mode = "demo" if mode_label.startswith("Synthetic") else "live"
    use_local_meta = mode_label == LOCAL_MODE
    use_nim = mode_label == NIM_MODE
    use_hf = mode_label == HF_MODE
    st.info(
        "Live Meta Muse calls are available through Hugging Face Inference Providers when the app "
        "owner has configured a token, with per-session and daily call caps. The direct billed "
        "Meta Model API adapter remains off."
    )
    if mode == "demo":
        st.caption("Synthetic mode: invented publications only (see the banner in the main panel).")
    elif use_nim:
        if not research_enabled():
            st.warning("Not available on the public Space: NVIDIA research inference has not been enabled by the app owner.")
        nim_authorized = st.checkbox(
            "Allow this research question and retrieved abstracts to be sent to NVIDIA for Meta inference",
            value=False, disabled=not research_enabled(), key="nim_research_consent")
        st.caption(f"Remaining shared model calls in this server process: {remaining_calls()} / 8. Failed requests consume the allowance; no automatic retries.")
    elif use_hf:
        if not hf_available:
            st.warning("Live Meta Muse is unavailable: " + hf_reason + ".")
        hf_consent = st.checkbox(hf_providers.CONSENT_LABEL, value=False,
                                 disabled=not hf_available, key="hf_live_consent")
        st.caption(
            f"Primary: {hf_providers.PRIMARY_MODEL} · automatic fallback: {hf_providers.FALLBACK_MODEL}. "
            f"Session calls left: {hf_session_budget.remaining} / {hf_session_budget.limit} · "
            f"server calls left today: {hf_providers.daily_budget().remaining} / {hf_providers.daily_budget().limit}. "
            "Every attempt, including a fallback, counts. Both models reason before answering, so "
            "one call can take up to 90 s (fallback 75 s). At most "
            f"{hf_providers.MAX_LIVE_ABSTRACTS} abstracts, each truncated to "
            f"{hf_providers.MAX_ABSTRACT_CHARS} characters. Quotes still pass the verbatim evidence gate "
            "and every step stays human-gated."
        )
    elif use_local_meta:
        if on_public_space:
            st.warning("Not available on the public Space: this mode needs a Muse Glimmer checkpoint served on the same machine. A run here fails safely without any model call.")
        else:
            st.warning("Experimental real open-weight model mode. Requires an actual Muse Glimmer checkpoint served on THIS machine; charges no Meta API credits. Never expose the local server publicly.")
    else:
        st.info("Europe PMC metadata only. No model inference, no paid API requests.")
    st.caption("No autonomous wet-lab execution, clinical advice or unverified study claims.")

band("I · Flatland — question and evidence", "point")
st.subheader("1 · Research question")
if mode == "demo":
    st.warning(SYNTHETIC_BANNER)
with st.form("research_form"):
    question = st.text_area("Scientific research question", value=QUESTION, height=85)
    query = st.text_input("Europe PMC search query", value=QUERY, disabled=(mode == "demo"))
    capped = use_nim or use_hf
    n = st.slider("Maximum live abstracts", 1, 2 if capped else 10,
                  2 if capped else 4, disabled=(mode == "demo"))
    hf_blocked = use_hf and not (hf_available and hf_consent and hf_session_budget.remaining > 0)
    run_clicked = st.form_submit_button("Run research audit", width="stretch", type="primary",
                                       disabled=(use_nim and not nim_authorized) or hf_blocked)

if run_clicked:
    with st.spinner("Retrieving evidence and applying verification gates..."):
        try:
            st.session_state.pop("frontier_result", None)
            st.session_state.pop("frontier_model_client", None)
            client = None
            client = (NvidiaResearchClient(authorized=nim_authorized) if use_nim else
                      hf_providers.HFMuseResearchClient(authorized=hf_consent,
                                                        session_budget=hf_session_budget) if use_hf else
                      OpenWeightClient() if use_local_meta else None)
            st.session_state["frontier_model_client"] = client
            result = run_research(question, query, mode=mode, model_client=client, max_papers=n)
            st.session_state["frontier_result"] = result
        except HFProvidersUnavailable as exc:
            st.error(str(exc))
        except Exception as exc:
            friendly = europepmc_error_message(exc)
            st.error(friendly if friendly else f"Research run failed: {type(exc).__name__}: {exc}")
        if use_hf and client is not None and "frontier_result" not in st.session_state:
            for line in hf_providers.failure_summary(getattr(client, "provenance", None)):
                st.caption("Failed attempt · " + line)
            with st.expander("Live Meta Muse call receipts (failed run)", expanded=True):
                st.json(getattr(client, "provenance", {}))

with st.expander("Published-literature pilot · 12 cases awaiting researcher annotation"):
    pilot = load_dataset()
    st.caption("Four published antibody-engineering papers; short abstract excerpts only. Draft reference labels are excluded from the review download. Researcher annotations remain pending.")
    recorded_path = Path(__file__).resolve().parent / "docs/runs/meta_nim_pilot_20261028.json"
    if recorded_path.exists():
        recorded = json.loads(recorded_path.read_text(encoding="utf-8"))["receipt"]
        st.info("RECORDED REAL META RUN · 12 structured predictions, 8 matched quotations and 4 insufficient-evidence responses. This panel makes no live model call; accuracy awaits researcher review.")
        st.download_button("Download recorded Meta predictions", data=json.dumps(recorded["predictions"], ensure_ascii=False, indent=2), file_name="meta_pilot_predictions_20261028.json", mime="application/json")
        ablation = run_recorded_pilot()
        with st.expander("Gate ablation · recorded 12-case Glimmer batch", expanded=False):
            st.warning(
                "POST-GENERATION REPLAY ONLY. The recorded model prompt already requested evidence quotes, "
                "so this does not isolate the causal effect of quote-grounded prompting. "
                "The 12-case human review is still pending; no accuracy is reported."
            )
            g1, g2, g3, g4 = st.columns(4)
            g1.metric("Raw decisive", ablation["arm_a_fixed_model_outputs"]["decisive"])
            g2.metric("Quote-gated decisive", ablation["arm_b_source_quote_post_gate"]["decisive"])
            g3.metric("New abstentions from gate", ablation["effects"]["quote_gate_new_abstentions"])
            g4.metric("Human reviews pending", ablation["arm_c_source_quote_plus_human_review"]["pending_review"])
            st.caption(
                f"Recorded Meta call: {ablation['recorded_model_call']['usage']['total_tokens']} tokens · "
                f"{ablation['recorded_model_call']['elapsed_seconds']:.3f}s · "
                f"new model calls for this replay: {ablation['new_model_calls']}."
            )
            st.caption(
                "In this clean recorded batch, all eight decisive outputs already had valid anchored quotes, "
                "so the post-processing source/quote gate introduced zero additional abstentions. "
                "That is a result, not evidence that the gate is unnecessary."
            )
            st.download_button(
                "Download recorded gate-ablation JSON",
                data=json.dumps(ablation, ensure_ascii=False, indent=2, sort_keys=True),
                file_name="gate_ablation_recorded_20261028.json",
                mime="application/json",
            )
    st.download_button(
        "Download literature reviewer packet", data=json.dumps(reviewer_packet(pilot), ensure_ascii=False, indent=2),
        file_name="frontierlab_real_excerpt_review.json", mime="application/json")
    reviewer_name = st.text_input(
        "Reviewer name for an uploaded 12-case packet",
        key="pilot_review_reviewer",
        help="Used for provenance only; FrontierLab does not verify identity."
    )
    uploaded_review = st.file_uploader(
        "Upload edited 12-case reviewer packet (JSON)",
        type=["json"],
        key="pilot_review_upload",
        help="Keep the source/case fields unchanged; edit only label and rationale fields."
    )
    if uploaded_review is not None:
        try:
            review_upload = json.loads(uploaded_review.getvalue().decode("utf-8"))
            canonical_review = canonicalize_review_upload(
                pilot, review_upload, reviewer=reviewer_name
            )
            review_stats = canonical_review["review_stats"]
            review_labels = labels_from_review_packet(pilot, canonical_review)
            reviewed_ablation = run_recorded_pilot(review_labels=review_labels)
            arm_c = reviewed_ablation["arm_c_source_quote_plus_human_review"]
            if canonical_review["status"] == "COMPLETE_RESEARCHER_REVIEW":
                st.success(
                    "12-case researcher review packet is complete and source-bound. "
                    "It remains researcher-supplied review, not independent gold."
                )
            else:
                st.info(
                    f"Partial researcher review: {review_stats['reviewed']} / "
                    f"{review_stats['total']} cases completed."
                )
            rv1, rv2, rv3 = st.columns(3)
            rv1.metric("Cases reviewed", review_stats["reviewed"])
            rv2.metric("Arm C reviews completed", arm_c["reviews_completed"])
            rv3.metric(
                "Reviewer changes to surviving decisions",
                arm_c["review_corrections_to_surviving_decisions"],
            )
            st.caption(
                f"Arm C pending reviews: {arm_c['pending_review']}. "
                "Accuracy remains unavailable because independence_verified=false."
            )
            st.download_button(
                "Download canonical source-bound review JSON",
                data=json.dumps(canonical_review, ensure_ascii=False, indent=2, sort_keys=True),
                file_name="frontierlab_real_excerpt_review_completed.json",
                mime="application/json",
            )
        except (ValueError, UnicodeDecodeError, json.JSONDecodeError) as exc:
            st.error(f"Reviewer packet rejected: {exc}")
    st.markdown("[Pilot protocol and source attribution](https://github.com/Jaycee871/FrontierLab-Meta-Hackathon-2026/blob/main/docs/REAL_LITERATURE_PILOT.md)")
    review_path = Path(__file__).resolve().parent / "docs/runs/nim_claim_review_completed_20261028.json"
    if review_path.exists():
        reviewed = json.loads(review_path.read_text(encoding="utf-8"))
        st.success("RECORDED CORE RUN · Jaycee871 reviewed both claims as SUPPORTED on October 28, 2026. These judgments cover two claims, separate from the 12-case pilot.")
        st.caption("The original extraction receipt is preserved. No hypothesis, method selection or plan was generated by recording this review; this panel makes no model call.")
        st.dataframe([{"Case": c["case_id"], "Claim": c["claim"], "Human judgment": c["review_label"], "Reason": c["review_note"]} for c in reviewed["cases"]], hide_index=True, width="stretch")
        st.download_button("Download completed core-workflow human review", data=review_path.read_text(encoding="utf-8"), file_name=review_path.name, mime="application/json")

result = st.session_state.get("frontier_result")
if result:
    active_client = st.session_state.get("frontier_model_client")
    if result.backend == "nvidia-nim" and (not use_nim or not nim_authorized):
        active_client = None
    if result.backend == hf_providers.BACKEND and (not use_hf or not hf_consent or not hf_available):
        active_client = None
    stats = summary(result)
    a, b, c, d = st.columns(4)
    a.metric("Sources", stats["sources"])
    b.metric("Claims: passage checks / synthetic", stats["claims"])
    c.metric("Conflicts", stats["conflicts"])
    d.metric("Rejected claims", stats["rejected"])
    st.caption(f"Decision gate: **{stats['decision']}** · Backend: {result.backend} · Model: {result.model} · Data source: {result.mode}")
    if result.inference:
        with st.expander("Model request receipts"):
            st.json(result.inference)
    with st.expander("Process metrics · audit only", expanded=False):
        pm = process_metrics(result)
        m1, m2, m3, m4 = st.columns(4)
        m1.metric("Completed stages", f"{pm['stage_completion']['completed']} / 5")
        m2.metric("SCR", f"{pm['scr'] * 100:.0f}%")
        m3.metric("Observed human actions", pm["human_interventions"]["observed_total"])
        aerr = pm["nim"]["aerr"]
        m4.metric("AERR", "N/A" if aerr is None else f"{aerr * 100:.0f}%")
        st.caption(
            "Process metrics describe workflow activity, not model accuracy. "
            "Observed human actions use recorded intervention events when available; legacy runs use a conservative fallback. Identity is not verified."
        )
        taxonomy = pm["error_taxonomy"]
        e1, e2, e3 = st.columns(3)
        e1.metric("Governance / evidence blocks", taxonomy["governance_or_evidence_blocks"])
        e2.metric("Runtime failures", taxonomy["runtime_failures"])
        e3.metric("Scientific tensions", taxonomy["scientific_tensions"])
        if pm["nim"]["applicable"]:
            st.caption(
                f"NIM calls: {pm['nim']['attempted_calls']} · "
                f"tokens: {pm['nim']['total_tokens']} · "
                f"recorded model time: {pm['nim']['elapsed_seconds']:.3f}s"
            )

    with st.expander("Research Operating Interface · operational status", expanded=False):
        roi = research_operating_status(result)
        synthetic_note = None
        if result.mode == "demo":
            synthetic_strict = roi["traceability"]["strict_extracted_coverage"]
            synthetic_note = (
                "The synthetic fixture contains no evidence quotes, so strict EXTRACTED coverage is "
                f"{_pct(synthetic_strict)} by design (fail-closed)."
            )
            if replay is not None:
                synthetic_note += (
                    " The recorded real M2 core measures "
                    f"{_pct(replay['trace_coverage']['source_trace_coverage'])} source / "
                    f"{_pct(replay['trace_coverage']['strict_extracted_coverage'])} strict: see "
                    f"“{REPLAY_TITLE}” at the top of the page."
                )
        render_operating_status(roi, key="live", download_label="Download operating-status JSON",
                                file_name="research_operating_status.json", synthetic_note=synthetic_note)

    for warning in result.audit.warnings:
        if result.mode == "demo" and warning.startswith("SYNTHETIC FIXTURE"):
            continue  # disclosed once by the synthetic banner
        st.warning(warning)
    if result.mode == "demo" and mode != "demo":
        st.warning(SYNTHETIC_BANNER)
    if result.mode == "demo":
        with st.expander("Synthetic audit fixtures (FICTIONAL) · heterogeneity, claim benchmark, failure lab", expanded=False):
            fx1, fx2, fx3 = st.tabs(["Wolfram-validated heterogeneity (FICTIONAL numbers)",
                                     "Claim-to-passage audit benchmark (FICTIONAL · hand-labeled)",
                                     "Research Failure Lab · 12 fictional failure injections"])
            with fx1:
                stats_q = inverse_variance_heterogeneity([0.4, -0.3], [0.15, 0.1])
                st.caption("SYNTHETIC: These invented effect sizes do NOT represent observed studies.")
                x, y, z = st.columns(3)
                x.metric("Pooled effect (not a conclusion)", f"{stats_q['pooled_effect']:.4f}")
                y.metric("Cochran Q", f"{stats_q['Q']:.4f}")
                z.metric("I²", f"{stats_q['I2'] * 100:.1f}%")
                st.caption("High inconsistency: do NOT force consensus. Independently checked with Wolfram Language; no online Wolfram call is made by this app.")
            with fx2:
                fixture_papers, fixture_cases = benchmark()
                verified = audit_predictions(fixture_papers, fixture_cases)
                st.caption("All ten examples and predictions are HAND-AUTHORED FICTION. Scores do not represent Meta/NVIDIA model accuracy, nor prove scientific validity.")
                x, y, z = st.columns(3)
                x.metric("Known source IDs", f"{verified['source_id_coverage']:.0%}")
                y.metric("Verbatim excerpt matches", f"{verified['verbatim_passage_coverage']:.0%}")
                z.metric("Synthetic gold-label agreement", f"{verified['label_accuracy_vs_independent_gold']:.0%}")
                st.caption("A quotation can exist yet be used to support a FALSE statement; genuine semantic verification needs independently annotated evidence.")
                st.json({"cases": verified["cases"], "missing_conflict_detection_example":
                         conflict_transparency([], [("SYNTHETIC:A","SYNTHETIC:B")])})
            with fx3:
                lab = suite_report()
                st.caption(
                    "OFFLINE HAND-AUTHORED POLICY TESTS ONLY: no actual Meta inference, "
                    "real biomedical model call, independent scientific assessment or "
                    "human learning experiment has occurred."
                )
                u, v, w = st.columns(3)
                u.metric("Policy test cases", lab["total"])
                v.metric("Expected guard decisions matched", lab["matched_expectations"])
                w.metric("Real model calls", lab["model_calls"])
                st.caption(
                    "Cross-stage scenarios: stale source snapshots, unsupported excerpts, "
                    "unverified BioNeMo origin, train/test peptide leakage, and human teachability evidence. "
                    "A quote that exists can still be semantically wrong."
                )
                st.json(lab["cases"])
                st.download_button(
                    "Download fictional failure-injection results",
                    data=json.dumps(lab, indent=2, ensure_ascii=False),
                    file_name="frontierlab_v011_failure_lab_fictional.json",
                    mime="application/json",
                )
    with st.expander("2 · Evidence catalogue", expanded=True):
        for p in result.papers:
            st.markdown(f"**{p.title}**  ·  {p.paper_id}  ·  {p.year}")
            st.caption(f"{p.authors} · {p.source} · {'SYNTHETIC / NO LINK' if p.synthetic else p.url}")
            st.write(p.abstract)
            st.divider()
    with st.expander("Evidence claims and citation checks", expanded=True):
        if not result.audit.verified_claims:
            st.info("No claims have passed the source-ID gate. This is not evidence that a scientific claim is false.")
        for claim in result.audit.verified_claims:
            st.markdown(f"**{claim.stance.upper()}** · {claim.paper_id} · {claim.claim_key}")
            st.write(claim.statement)
            if claim.passage_matched:
                st.caption(f"Verbatim abstract excerpt (NOT semantic verification): {claim.evidence_quote}")
            else:
                st.caption("No passage match recorded (synthetic fixture only).")
            st.caption(f"Limitations: {claim.limitation}")
        st.caption("Valid source IDs and verbatim excerpts are NOT independent scientific fact-checking or semantic entailment. Compare claims against the original article and methods.")
        if result.audit.rejected_claims:
            st.json(result.audit.rejected_claims)
    with st.expander("Evidence conflicts and skeptical review", expanded=True):
        if result.audit.conflicts:
            st.error("Incompatible directions detected. Do not report consensus.")
            st.json(result.audit.conflicts)
        else:
            st.info("No machine-detected contradictions; this is not proof of agreement.")
        st.json(result.skeptic)
    if result.mode == "live" and result.audit.verified_claims:
        band("II · The Sphere — human claim review", "sphere")
        with st.expander("Researcher claim review · required before synthesis", expanded=True):
            st.warning(
                "Verbatim excerpt matching does NOT establish entailment. "
                "Choose a human judgment for each claim. PENDING items and fewer than two "
                "supported distinct sources block research hypothesis generation."
            )
            packet=review_packet(result)
            previous={c["case_id"]:c for c in (result.review or {}).get("cases", [])}
            st.download_button(
                "Download source-bound blank reviewer packet",
                data=json.dumps(packet, ensure_ascii=False, indent=2),
                file_name="frontierlab_human_review_blank.json", mime="application/json"
            )
            decisions=[]
            with st.form("claim_reviewer_form"):
                for case in packet["cases"]:
                    cid=case["case_id"]
                    earlier=previous.get(cid, {})
                    st.markdown(f"**{cid} · {case['paper_title']}**")
                    st.caption(f"Source ID: {case['paper_id']} · {case['source']}")
                    st.write("**Model claim:** " + case["claim"])
                    st.code(case["evidence_quote"], language="text")
                    st.caption("The quotation exists in the source abstract. Its meaning has NOT been verified.")
                    opts=["PENDING","SUPPORTED","CONTRADICTED","INSUFFICIENT"]
                    default=earlier.get("review_label","PENDING")
                    verdict=st.selectbox(
                        "Reviewer judgment", opts,
                        index=opts.index(default) if default in opts else 0,
                        key=f"claim_review_{cid}"
                    )
                    note=st.text_input(
                        "Reason for verdict (required if contradicted or insufficient)",
                        value=earlier.get("review_note",""), key=f"claim_reason_{cid}"
                    )
                    decisions.append({"case_id":cid,"review_label":verdict,"review_note":note})
                    st.divider()
                submitted=st.form_submit_button("Save human review judgments", type="primary")
            if submitted:
                try:
                    save_review(result, decisions)
                    st.session_state["frontier_result"]=result
                    st.success("Human review saved in this session; download JSON to preserve it.")
                except ValueError as exc:
                    st.error(f"Review validation failed: {exc}")
            if result.review:
                st.json({"review_status":result.review.get("status"),
                         "review_statistics":result.review.get("review_stats"),
                         "independent_expert_adjudication_verified":False})
                st.download_button(
                    "Download human-annotated review JSON",
                    data=json.dumps(result.review,ensure_ascii=False,indent=2),
                    file_name="frontierlab_human_review_annotated.json",
                    mime="application/json"
                )
                if result.review.get("status")=="READY_FOR_HUMAN_GATED_SYNTHESIS":
                    st.info("Review complete. Hypothesis synthesis requires an actually connected, available model and further human scrutiny.")
                    if st.button("Resume model synthesis after human review", type="secondary",
                                 disabled=active_client is None):
                        try:
                            continue_after_review(result, model_client=active_client)
                            st.session_state["frontier_result"]=result
                            st.success("Provisional human-gated draft created; scientific validation is still needed.")
                        except HFProvidersUnavailable as exc:
                            st.error(str(exc))
                        except (ValueError, PermissionError, RuntimeError) as exc:
                            st.error(f"Synthesis remains blocked: {type(exc).__name__}: {exc}")
    band("III · Spaceland — hypothesis, evaluation and plan", "cube")
    with st.expander("3 · Falsifiable hypotheses", expanded=True):
        st.json(result.hypotheses)
    render_evaluation(result, model_client=active_client)
    st.subheader("5 · Research plan approval gate")
    try:
        require_evaluation(result)
        evaluation_ready = True
    except (ValueError, PermissionError):
        evaluation_ready = False

    if evaluation_ready and result.plan is None:
        try:
            readiness = build_planning_readiness(result)
            ready_stats = readiness_summary(readiness)
            with st.expander("Planning readiness · pre-approval contract", expanded=False):
                render_planning_readiness(readiness, ready_stats, key="live",
                                          download_label="Download planning-readiness JSON",
                                          file_name="planning_readiness.json")
        except (ValueError, PermissionError) as exc:
            st.info(f"Planning readiness not available: {exc}")
    approved = st.checkbox("I have reviewed the evidence, conflicts and limitations. Allow a DRAFT research plan.")
    if st.button("Generate cited plan", disabled=not approved or not result.hypotheses or not evaluation_ready or (result.mode == "live" and active_client is None), type="primary"):
        try:
            planner = active_client if result.mode == "live" else None
            make_plan(result, model_client=planner, approved=approved)
            st.success("Draft created. Human scientific review is still required.")
        except Exception as exc:
            st.error(f"Planning blocked: {exc}")
    if result.plan:
        st.json(result.plan)
    # Rebuilt from the current run on every render, so reviews, hypotheses and plans stay in sync.
    graph = refresh_evidence_graph(result)
    g_stats = graph_summary(graph)
    with st.expander("6 · Evidence graph · provenance of every plan step", expanded=False):
        render_graph_overview(graph, g_stats)
        steps = [n for n in graph["nodes"] if n["type"] == "plan_step"]
        if steps:
            gates = {n["id"]: step_gate(graph, n["id"]) for n in steps}
            st.dataframe([{"Step": n["index"], "Text": n["text"],
                           "Path gate": "ALL EXTRACTED" if gates[n["id"]]["approvable"] else "BLOCKED",
                           "Researcher approval": "RECORDED" if n["id"] in graph["approvals"] else "—",
                           "Reasons": "; ".join(gates[n["id"]]["reasons"])} for n in steps],
                         hide_index=True, width="stretch")
            eligible = [n["id"] for n in steps if gates[n["id"]]["approvable"] and n["id"] not in graph["approvals"]]
            if eligible:
                with st.form("plan_step_approval_form"):
                    chosen = st.selectbox("Plan step with an all-EXTRACTED evidence path", eligible,
                                          format_func=lambda sid: next(f"Step {n['index']}" for n in steps if n["id"] == sid))
                    reviewer = st.text_input("Reviewer")
                    rationale = st.text_input("Approval rationale (8+ characters)")
                    if st.form_submit_button("Record researcher approval of this step", type="secondary"):
                        try:
                            approve_plan_step(graph, chosen, reviewer=reviewer, note=rationale)
                            record_human_event(result, "PLAN_STEP_APPROVED", chosen, actor_label=reviewer, note=rationale)
                            st.session_state["frontier_result"] = result
                            st.success("Step approval recorded in this session; it remains a draft, not a validated protocol.")
                        except (ValueError, PermissionError) as exc:
                            st.error(f"Step approval blocked: {exc}")
            else:
                st.info("No plan step is currently eligible for approval under the EXTRACTED-only path rule.")
        else:
            st.caption("No plan steps yet. Steps appear here once a draft plan exists.")
        st.download_button("Download graph.json", data=graph_json(graph),
                           file_name="graph.json", mime="application/json")
    trajectory = build_trajectory_graph(result)
    t_stats = trajectory_summary(trajectory)
    with st.expander("7 · Trajectory audit graph · process trace", expanded=False):
        render_trajectory(trajectory, t_stats, key="live", model_calls_label="Model calls",
                          download_label="Download trajectory.json", file_name="trajectory.json")
    with st.expander("Audit trail", expanded=False):
        st.json(result.log)
    st.download_button("Download complete provenance JSON", data=export_json(result),
                       file_name="frontierlab_audit.json", mime="application/json")
else:
    st.info("Begin with the synthetic audit or Europe PMC. Model-backed research requires live Meta Muse via Hugging Face (owner-configured token and your consent), a configured local server or an explicitly enabled NVIDIA backend.")