File size: 8,055 Bytes
844d75c
 
 
d5fddc3
 
844d75c
 
 
 
 
 
d5fddc3
844d75c
 
 
 
 
 
d5fddc3
 
 
 
844d75c
d5fddc3
844d75c
 
 
d5fddc3
 
 
 
 
 
 
 
 
 
844d75c
 
 
 
 
 
 
 
 
d5fddc3
844d75c
d5fddc3
844d75c
 
 
 
d5fddc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
844d75c
 
 
 
 
 
d5fddc3
 
 
 
844d75c
 
d5fddc3
844d75c
 
d5fddc3
 
 
 
844d75c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5fddc3
844d75c
 
 
 
 
d5fddc3
844d75c
 
 
d5fddc3
 
 
 
 
 
 
 
 
844d75c
 
d5fddc3
 
 
 
 
 
 
 
 
 
844d75c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5fddc3
844d75c
 
 
 
 
 
 
 
 
 
 
 
d5fddc3
844d75c
 
 
 
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
import json
import os
import tempfile
import zipfile
from typing import Any, Dict, Optional, Tuple, List

import gradio as gr

from drp.bundle import verify_bundle
from drp.diff import diff_bundles
from drp.report import render_report_markdown
from drp.pdf_report import write_pdf_report
from drp.simulate import make_demo_bundle_zip, fork_patch_bundle


APP_TITLE = "TimelineDiff — Differential Reproducibility Protocol (DRP)"


def _tmp_dir() -> str:
    return tempfile.mkdtemp(prefix="drp_")


def _tmp_path(name: str) -> str:
    d = _tmp_dir()
    return os.path.join(d, name)


def _as_alignment_rows(alignment: List[Dict[str, Any]]) -> List[List[Any]]:
    """
    Convert alignment dict rows to Dataframe rows.
    """
    rows = []
    for r in alignment:
        rows.append([r.get("i"), r.get("status"), r.get("kind_a"), r.get("step_a"), r.get("kind_b"), r.get("step_b")])
    return rows


def ui_verify(bundle_file) -> Tuple[str, Dict[str, Any]]:
    if not bundle_file:
        return "Upload a bundle zip first.", {"ok": False}

    ok, summary = verify_bundle(bundle_file.name)
    msg = "✅ Bundle verifies (hash chain OK)." if ok else "❌ Bundle failed verification."
    return msg, summary


def ui_diff(bundle_a, bundle_b) -> Tuple[str, str, Dict[str, Any], List[List[Any]], str, str, str]:
    if not bundle_a or not bundle_b:
        return "Upload two bundles.", "", {"error": "missing input"}, [], "", "", ""

    diff = diff_bundles(bundle_a.name, bundle_b.name)
    md = render_report_markdown(diff)

    # Summary header (quick context above JSON)
    s = diff.get("summary", {})
    parts = []
    if s.get("first_divergence_index") is None:
        parts.append("Timelines identical")
    else:
        parts.append(f"Identical until step/index {s.get('first_divergence_index')}")
    parts.append(f"{s.get('diff_event_count', 0)} events differ")
    if s.get("missing_event_count", 0):
        parts.append(f"{s.get('missing_event_count')} missing")
    if s.get("final_reward_delta") is not None:
        parts.append(f"Final reward delta: {s.get('final_reward_delta'):.6g}")
    summary_line = " · ".join(parts)

    # Export artifacts
    out_dir = _tmp_dir()
    out_md = os.path.join(out_dir, "report.md")
    out_json = os.path.join(out_dir, "diff.json")
    out_pdf = os.path.join(out_dir, "report.pdf")
    out_zip = os.path.join(out_dir, "drp-diff-report.zip")

    with open(out_md, "w", encoding="utf-8") as f:
        f.write(md)
    with open(out_json, "w", encoding="utf-8") as f:
        json.dump(diff, f, ensure_ascii=False, indent=2)

    # PDF: render from markdown lines (plain, reliable)
    md_lines = md.splitlines()
    write_pdf_report(out_pdf, "DRP Differential Report", md_lines)

    with zipfile.ZipFile(out_zip, "w", compression=zipfile.ZIP_DEFLATED) as z:
        z.write(out_md, arcname="report.md")
        z.write(out_pdf, arcname="report.pdf")
        z.write(out_json, arcname="diff.json")

    alignment_rows = _as_alignment_rows(diff.get("alignment", []))

    status = "✅ Diff complete."
    return status, summary_line, diff, alignment_rows, out_zip, out_pdf, out_md


def ui_generate(seed_a: int, seed_b: int, chaos: float) -> Tuple[str, str, str]:
    chaos = float(max(0.0, min(1.0, chaos)))

    path_a = _tmp_path("demo-A.zip")
    path_b = _tmp_path("demo-B.zip")
    make_demo_bundle_zip(path_a, seed=int(seed_a), chaos=chaos, label="A")
    make_demo_bundle_zip(path_b, seed=int(seed_b), chaos=chaos, label="B")

    return "✅ Generated demo bundles.", path_a, path_b


def ui_fork(source_bundle, fork_index: int, patch_kind: str, patch_step: str, patch_payload: str) -> Tuple[str, str]:
    if not source_bundle:
        return "Upload a source bundle first.", ""

    patch_payload_json: Optional[Dict[str, Any]] = None
    if patch_payload.strip():
        try:
            patch_payload_json = json.loads(patch_payload)
        except Exception as e:
            return f"Invalid JSON patch payload: {e}", ""

    out = _tmp_path("forked.zip")
    fork_patch_bundle(
        out,
        source_zip=source_bundle.name,
        fork_at_index=int(fork_index),
        patch_kind=patch_kind.strip() or None,
        patch_step=patch_step.strip() or None,
        patch_payload_json=patch_payload_json,
    )
    return "✅ Fork bundle created (patched + re-hash-chained).", out


with gr.Blocks(title=APP_TITLE) as demo:
    gr.Markdown(f"# {APP_TITLE}\nDiff two agent timelines. Find first divergence. Export forensic reports.")

    with gr.Tab("Diff two bundles"):
        with gr.Row():
            bundle_a = gr.File(label="Bundle A (.zip)", file_types=[".zip"])
            bundle_b = gr.File(label="Bundle B (.zip)", file_types=[".zip"])

        run = gr.Button("Compute differential report", variant="primary")

        status = gr.Textbox(label="Status", interactive=False)
        summary = gr.Textbox(label="Summary (fast context)", interactive=False)

        alignment_table = gr.Dataframe(
            headers=["i", "status", "kind_a", "step_a", "kind_b", "step_b"],
            label="Timeline alignment (scan for first divergence)",
            interactive=False,
            wrap=True,
        )

        diff_json = gr.JSON(label="Diff JSON (summary + per-event diffs)")

        with gr.Row():
            report_zip = gr.File(label="Download drp-diff-report.zip (MD + PDF + JSON)")
            report_pdf = gr.File(label="Download report.pdf")
            report_md = gr.File(label="Download report.md")

        run.click(
            fn=ui_diff,
            inputs=[bundle_a, bundle_b],
            outputs=[status, summary, diff_json, alignment_table, report_zip, report_pdf, report_md],
        )

    with gr.Tab("Verify bundle integrity"):
        bundle_v = gr.File(label="Bundle (.zip)", file_types=[".zip"])
        verify_btn = gr.Button("Verify hash chain", variant="secondary")
        v_msg = gr.Textbox(label="Result", interactive=False)
        v_json = gr.JSON(label="Verification summary")
        verify_btn.click(fn=ui_verify, inputs=[bundle_v], outputs=[v_msg, v_json])

    with gr.Tab("Generate demo bundles"):
        gr.Markdown("Use this to test the diff UI without integrating exporters yet.")
        with gr.Row():
            seed_a = gr.Number(value=1, label="Seed A", precision=0)
            seed_b = gr.Number(value=2, label="Seed B", precision=0)
            chaos = gr.Slider(0, 1, value=0.35, step=0.01, label="Chaos (divergence likelihood)")
        gen_btn = gr.Button("Generate", variant="primary")
        gen_msg = gr.Textbox(label="Status", interactive=False)
        demo_a = gr.File(label="Demo Bundle A")
        demo_b = gr.File(label="Demo Bundle B")
        gen_btn.click(fn=ui_generate, inputs=[seed_a, seed_b, chaos], outputs=[gen_msg, demo_a, demo_b])

    with gr.Tab("Fork / patch a bundle"):
        gr.Markdown("Counterfactual workflow: patch an event at index N, then re-hash-chain into a new bundle.")
        src = gr.File(label="Source bundle (.zip)", file_types=[".zip"])
        fork_index = gr.Number(value=0, label="Fork at event index", precision=0)

        with gr.Row():
            patch_kind = gr.Textbox(label="Patch kind (optional)", placeholder="e.g. tool_result")
            patch_step = gr.Textbox(label="Patch step (optional)", placeholder="e.g. t12.tool_result")

        patch_payload = gr.Textbox(
            label="Patch payload JSON (optional)",
            lines=8,
            placeholder='{"ok": true, "value": 42}',
        )

        fork_btn = gr.Button("Create fork bundle", variant="primary")
        fork_msg = gr.Textbox(label="Status", interactive=False)
        fork_file = gr.File(label="Forked bundle (.zip)")
        fork_btn.click(fn=ui_fork, inputs=[src, fork_index, patch_kind, patch_step, patch_payload], outputs=[fork_msg, fork_file])

    gr.Markdown(
        "Exporter tip: emit DRP events at LLM calls, tool calls/results, memory writes, planner steps, guardrails, and state snapshots."
    )

if __name__ == "__main__":
    demo.launch()