|
| 1 | +import { useEffect, useMemo, useState } from 'react'; |
| 2 | + |
| 3 | +// The self-improvement history of a published scene, read from the static |
| 4 | +// gallery (`/samples/runs/<id>/manifest.json`, written by `visually upload`). |
| 5 | +// Renders nothing when a scene ships no history, so every other detail page |
| 6 | +// is unaffected. Improve runs show their iteration renders + review verdicts; |
| 7 | +// verify runs show the formal-verification outcome with the run log. |
| 8 | + |
| 9 | +type HistoryRun = { dir: string; kind: string; at: string; files: string[] }; |
| 10 | +type HistoryManifest = { id: string; runs: HistoryRun[] }; |
| 11 | + |
| 12 | +type Review = { |
| 13 | + total?: number | null; |
| 14 | + verdict?: string | null; |
| 15 | + critique?: string | null; |
| 16 | + changelog?: string[]; |
| 17 | + remaining_gaps?: string[]; |
| 18 | +}; |
| 19 | + |
| 20 | +type Iteration = { n: string; render: string | null; review: Review | null }; |
| 21 | + |
| 22 | +const RENDER_RE = /^iter-(\d+)-render\.png$/; |
| 23 | +const VERIFY_TXT_RE = /^verify-(\d+)\.txt$/; |
| 24 | + |
| 25 | +function runBase(id: string, dir: string) { |
| 26 | + return `/samples/runs/${encodeURIComponent(id)}/${encodeURIComponent(dir)}`; |
| 27 | +} |
| 28 | + |
| 29 | +function fmtDate(at: string) { |
| 30 | + return at ? at.slice(0, 10) : ''; |
| 31 | +} |
| 32 | + |
| 33 | +// ---- improve run: iteration render + review cards -------------------------- |
| 34 | + |
| 35 | +function ImproveRun({ id, run }: { id: string; run: HistoryRun }) { |
| 36 | + const [reviews, setReviews] = useState<Record<string, Review>>({}); |
| 37 | + |
| 38 | + const iterations = useMemo<Iteration[]>(() => { |
| 39 | + const ns = new Set<string>(); |
| 40 | + for (const f of run.files) { |
| 41 | + const m = RENDER_RE.exec(f); |
| 42 | + if (m) ns.add(m[1]); |
| 43 | + } |
| 44 | + return [...ns].sort().map((n) => ({ |
| 45 | + n, |
| 46 | + render: run.files.includes(`iter-${n}-render.png`) ? `${runBase(id, run.dir)}/iter-${n}-render.png` : null, |
| 47 | + review: reviews[n] ?? null, |
| 48 | + })); |
| 49 | + }, [id, run, reviews]); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + let cancelled = false; |
| 53 | + const wanted = run.files.filter((f) => /^iter-\d+-review\.json$/.test(f)); |
| 54 | + void Promise.all(wanted.map(async (f) => { |
| 55 | + try { |
| 56 | + const res = await fetch(`${runBase(id, run.dir)}/${f}`); |
| 57 | + if (!res.ok) return null; |
| 58 | + const data = (await res.json()) as Review; |
| 59 | + return { n: /^iter-(\d+)-/.exec(f)?.[1] ?? '', data }; |
| 60 | + } catch { return null; } |
| 61 | + })).then((loaded) => { |
| 62 | + if (cancelled) return; |
| 63 | + const next: Record<string, Review> = {}; |
| 64 | + for (const r of loaded) if (r && r.n) next[r.n] = r.data; |
| 65 | + setReviews(next); |
| 66 | + }); |
| 67 | + return () => { cancelled = true; }; |
| 68 | + }, [id, run]); |
| 69 | + |
| 70 | + if (iterations.length === 0) return null; |
| 71 | + return ( |
| 72 | + <div className="htl__strip"> |
| 73 | + {iterations.map((it) => ( |
| 74 | + <figure className="htl__card" key={it.n}> |
| 75 | + {it.render ? <img className="htl__render" src={it.render} alt={`iteration ${it.n} render`} loading="lazy" /> : null} |
| 76 | + <figcaption className="htl__cardbody"> |
| 77 | + <div className="htl__cardhead"> |
| 78 | + <span className="htl__iter">iter {Number(it.n)}</span> |
| 79 | + {typeof it.review?.total === 'number' ? <span className="htl__score">{it.review.total}/100</span> : null} |
| 80 | + {it.review?.verdict ? <span className="htl__verdict">{it.review.verdict}</span> : null} |
| 81 | + </div> |
| 82 | + {it.review?.critique ? <p className="htl__critique">{it.review.critique}</p> : null} |
| 83 | + </figcaption> |
| 84 | + </figure> |
| 85 | + ))} |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +// ---- verify run: formal-verification outcome -------------------------------- |
| 91 | + |
| 92 | +function VerifyRun({ id, run }: { id: string; run: HistoryRun }) { |
| 93 | + const [log, setLog] = useState<string | null>(null); |
| 94 | + const txt = run.files.find((f) => VERIFY_TXT_RE.test(f)) ?? run.files.find((f) => f === 'verify.txt'); |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + if (!txt) return; |
| 98 | + let cancelled = false; |
| 99 | + fetch(`${runBase(id, run.dir)}/${txt}`) |
| 100 | + .then((res) => (res.ok ? res.text() : Promise.reject(res))) |
| 101 | + .then((body) => { if (!cancelled) setLog(body); }) |
| 102 | + .catch(() => { if (!cancelled) setLog(null); }); |
| 103 | + return () => { cancelled = true; }; |
| 104 | + }, [id, run, txt]); |
| 105 | + |
| 106 | + const pass = log ? /pass=true|VERIFIED/.test(log) : null; |
| 107 | + const checks = log ? (log.match(/^ok {2}/gm) ?? []).length : 0; |
| 108 | + const check = run.files.find((f) => /^check-\d+\./.test(f)); |
| 109 | + return ( |
| 110 | + <div className="htl__verify"> |
| 111 | + {pass === null ? null : ( |
| 112 | + <span className={`htl__badge ${pass ? 'htl__badge--pass' : 'htl__badge--fail'}`}> |
| 113 | + {pass ? 'VERIFIED' : 'FAILED'} |
| 114 | + </span> |
| 115 | + )} |
| 116 | + {checks > 0 ? <span className="htl__checks">{checks} checks</span> : null} |
| 117 | + {check ? ( |
| 118 | + <a className="htl__src" href={`${runBase(id, run.dir)}/${check}`} target="_blank" rel="noreferrer"> |
| 119 | + self-check source |
| 120 | + </a> |
| 121 | + ) : null} |
| 122 | + {log ? ( |
| 123 | + <details className="htl__log"> |
| 124 | + <summary>run log</summary> |
| 125 | + <pre>{log}</pre> |
| 126 | + </details> |
| 127 | + ) : null} |
| 128 | + </div> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +// ---- the timeline ------------------------------------------------------------ |
| 133 | + |
| 134 | +const KIND_LABEL: Record<string, string> = { |
| 135 | + create: 'created', |
| 136 | + improve: 'visual self-improve', |
| 137 | + verify: 'formal verification', |
| 138 | + amend: 'spec amend', |
| 139 | + reproduce: 'reproduce', |
| 140 | + evidence: 'evidence fetch', |
| 141 | +}; |
| 142 | + |
| 143 | +export function HistoryTimeline({ id }: { id: string }) { |
| 144 | + const [manifest, setManifest] = useState<HistoryManifest | null>(null); |
| 145 | + |
| 146 | + useEffect(() => { |
| 147 | + let cancelled = false; |
| 148 | + fetch(`/samples/runs/${encodeURIComponent(id)}/manifest.json`) |
| 149 | + .then((res) => (res.ok ? (res.json() as Promise<HistoryManifest>) : Promise.reject(res))) |
| 150 | + .then((data) => { if (!cancelled && Array.isArray(data.runs) && data.runs.length > 0) setManifest(data); }) |
| 151 | + .catch(() => { /* no published history — render nothing */ }); |
| 152 | + return () => { cancelled = true; }; |
| 153 | + }, [id]); |
| 154 | + |
| 155 | + if (!manifest) return null; |
| 156 | + return ( |
| 157 | + <section className="htl" aria-label="Self-improvement history"> |
| 158 | + <header className="htl__head"> |
| 159 | + <h2 className="htl__title">Self-improvement history</h2> |
| 160 | + <p className="htl__sub"> |
| 161 | + every step of the visualize → verify → refine loop that produced this scene, oldest first |
| 162 | + </p> |
| 163 | + </header> |
| 164 | + <ol className="htl__list"> |
| 165 | + {manifest.runs.map((run) => ( |
| 166 | + <li className={`htl__run htl__run--${run.kind}`} key={run.dir}> |
| 167 | + <div className="htl__meta"> |
| 168 | + <span className="htl__kind">{KIND_LABEL[run.kind] ?? run.kind}</span> |
| 169 | + <span className="htl__date">{fmtDate(run.at)}</span> |
| 170 | + </div> |
| 171 | + {run.kind === 'improve' ? <ImproveRun id={id} run={run} /> : null} |
| 172 | + {run.kind === 'verify' ? <VerifyRun id={id} run={run} /> : null} |
| 173 | + </li> |
| 174 | + ))} |
| 175 | + </ol> |
| 176 | + </section> |
| 177 | + ); |
| 178 | +} |
0 commit comments