Files
OBLITERATUS/obliteratus/ui_observatory.py
Pliny the Younger a7846cb682 feat(ui): unify research workspace, paired neuron experiments and live surgery observatory
Add bounded contrast candidate screening, explicit prompt provenance, atomic paired conversations and redacted evidence exports. Unify the application shell and task navigation, add an opt-in per-run surgery event observatory, preserve public generator compatibility, and wire cooperative stream-close cancellation. Verify numerical invariants, lifecycle isolation and real Gradio paths.
2026-10-27 18:23:50 -04:00

283 lines
15 KiB
Python

"""Optional, per-run visualization of reported pipeline events.
The UI adapter appends presentation without changing the wrapped generator's
inputs or existing outputs. This is operation progress, never neuron capture.
"""
from __future__ import annotations
from collections import deque
from collections.abc import Mapping
from contextvars import ContextVar
from copy import deepcopy
from functools import wraps
from html import escape
from itertools import islice
import json
import math
import threading
import time
_CURRENT = ContextVar("obliteratus_observatory", default=None)
_STAGE_NAMES = {"summon": "Load model", "baseline": "Check baseline", "probe": "Collect activations",
"analyze": "Analyze geometry", "distill": "Extract directions", "excise": "Apply surgery",
"verify": "Evaluate change", "rebirth": "Save model"}
def current_observatory():
"""Capture once on the request thread, then pass to worker closures."""
return _CURRENT.get()
def _text(value, limit=320):
return str(value or "")[:limit]
def _safe(value, depth=0):
if value is None or isinstance(value, bool):
return value
if isinstance(value, int):
return value if abs(value) < 10 ** 15 else None
if isinstance(value, float):
return value if math.isfinite(value) else None
if isinstance(value, str):
return value[:160]
if depth >= 2:
return "[details truncated]"
if isinstance(value, Mapping):
return {_text(key, 60): _safe(item, depth + 1) for key, item in islice(value.items(), 12)}
if isinstance(value, (tuple, list)):
return [_safe(item, depth + 1) for item in value[:64]]
return "[unsupported detail]"
class OperationJournal:
"""Bounded actual-event journal shared only by one run and its worker."""
def __init__(self, operation="Obliterate pipeline", *, clock=time.monotonic):
self._clock = clock
self._started = clock()
self._lock = threading.RLock()
self.operation = _text(operation, 80)
self.status = "running"
self.phases = {}
self.logs = deque(maxlen=8)
self.message = "Preparing the run; no stage events received yet."
self._elapsed = None
def stage(self, result):
name = _text(getattr(result, "stage", ""), 60)
status = _text(getattr(result, "status", ""), 30)
if not name or status not in {"running", "done", "error"}:
return
message = _text(getattr(result, "message", ""))
with self._lock:
if name not in self.phases and len(self.phases) >= 16:
return
self.phases[name] = {"stage": name, "label": _STAGE_NAMES.get(name, name),
"status": status, "message": message,
"details": _safe(getattr(result, "details", {}))}
self.message = message or _STAGE_NAMES.get(name, name)
if status == "error":
self.status = "error"
def log(self, message):
with self._lock:
self.logs.append(_text(message, 240))
def output_status(self, message):
"""Retain the real UI status, including failures before model loading."""
if not isinstance(message, str):
return
with self._lock:
text = message.lstrip()
if text.startswith("**Timed out"):
self.finish("timed_out", message)
elif text.startswith("**Error (PipelineCancelledError"):
self.finish("cancelled", message)
elif text.startswith(("**Error", "**Run manifest failed", "**GPU admission failed")):
self.finish("error", message)
def finish(self, status="ended", message=None):
with self._lock:
# An adapter exhausting must not turn a recorded failure into success.
if status == "ended" and self.status in {"error", "timed_out", "done", "cancelled"}:
return
self.status = status
if message:
self.message = _text(message)
self._elapsed = max(0., self._clock() - self._started)
def snapshot(self):
with self._lock:
elapsed = self._elapsed if self._elapsed is not None else max(0., self._clock() - self._started)
return {"operation": self.operation, "status": self.status,
"message": self.message, "elapsed": round(elapsed, 1),
"phases": deepcopy(list(self.phases.values())), "logs": list(self.logs)}
def observe_operation(fn, *, operation="Obliterate pipeline"):
"""Append one observatory value; preserve the public function and its signature."""
@wraps(fn)
def stream(*args, **kwargs):
journal = OperationJournal(operation)
iterator = fn(*args, **kwargs)
try:
while True:
token = _CURRENT.set(journal)
try:
update = next(iterator)
except StopIteration:
return
finally:
_CURRENT.reset(token)
journal.output_status(update[0] if update else None)
yield (*update, render_observatory(journal.snapshot()))
finally:
token = _CURRENT.set(journal)
try:
iterator.close()
finally:
_CURRENT.reset(token)
return stream
def render_observatory(snapshot=None):
data = snapshot or {"operation": "Obliterate pipeline", "status": "idle", "message": "No pipeline events yet.",
"elapsed": None, "phases": [], "logs": []}
payload = escape(json.dumps(data, allow_nan=False), quote=True)
names = {"idle": "Standby", "running": "Running", "done": "Complete", "error": "Failed",
"timed_out": "Timed out · stopping", "stopping": "Stopping", "cancelled": "Cancelled", "ended": "Run ended"}
state = names.get(data["status"], "Status unrecorded")
phases = data.get("phases", [])
rows = "".join(f'<li class="ob-event ob-event-{escape(item["status"], quote=True)}">'
f'<span>{escape(item["label"])}</span><strong>{escape(item["status"])}</strong></li>'
for item in phases)
if not rows:
rows = '<li class="ob-event-empty">Start a run in Obliterate to see its reported stages.</li>'
completed = sum(item["status"] == "done" for item in phases)
elapsed = data.get("elapsed")
elapsed_text = f'{elapsed:.1f}s elapsed' if isinstance(elapsed, (int, float)) else 'Awaiting a run'
logs = "\n".join(data.get("logs", []))
details = {item["label"]: item.get("details") for item in phases if item.get("details")}
return f'''<section class="ob-observatory" data-observatory="{payload}">
<div class="ob-observatory-top"><button type="button" class="ob-observatory-toggle" aria-expanded="false">Open operation observatory</button>
<span class="ob-observatory-summary">{escape(state)} · actual pipeline events</span></div>
<div class="ob-observatory-body" hidden>
<div class="ob-observatory-scene"><canvas aria-hidden="true"></canvas>
<div class="ob-observatory-overlay"><span>OPERATION PROGRESS</span><h3>{escape(state)}</h3>
<p>{escape(data.get("message", ""))}</p><small>{completed} reported stages complete · {elapsed_text}</small></div>
<button type="button" class="ob-observatory-pause" aria-pressed="false">Pause motion</button></div>
<ol class="ob-event-list" aria-label="Reported pipeline stages">{rows}</ol>
<div class="ob-observatory-footnote">Orbital paths arrange reported stages. They are not activations, layer measurements, or an estimated completion percentage. Live adapter: Obliterate; other workspaces retain their own results.</div>
<details class="ob-observatory-details"><summary>Latest pipeline messages &amp; reported details</summary>
<pre>{escape(logs or "No log messages received.")}</pre><pre>{escape(json.dumps(details, ensure_ascii=False, indent=2, allow_nan=False))}</pre></details>
</div></section>'''
OBSERVATORY_CSS = """
.gradio-container .ob-observatory { border:1px solid var(--ob-line); border-radius:10px; overflow:hidden; background:var(--ob-surface); }
.gradio-container .ob-observatory-top { display:flex; justify-content:space-between; align-items:center; gap:12px; padding:9px 12px; }
.gradio-container .ob-observatory-toggle { border:0; color:var(--ob-violet); background:transparent; font:550 11px/1.5 var(--ob-font); text-align:left; }
.gradio-container .ob-observatory-toggle::before { content:"◉"; display:inline-block; margin-right:8px; }
.gradio-container .ob-observatory-summary { color:var(--ob-dim); font:10px/1.5 var(--ob-mono); }
.gradio-container .ob-observatory-body { display:grid; grid-template-columns:minmax(0,1fr) 230px; border-top:1px solid var(--ob-line); }
.gradio-container .ob-observatory-body[hidden] { display:none; }
.gradio-container .ob-observatory-scene { position:relative; min-height:235px; overflow:hidden; background:#080b19; }
.gradio-container .ob-observatory-scene canvas { position:absolute; width:100%; height:100%; inset:0; }
.gradio-container .ob-observatory-overlay { position:relative; padding:22px; max-width:430px; pointer-events:none; }
.gradio-container .ob-observatory-overlay > span { color:#b59aff; font:9px var(--ob-mono); letter-spacing:.18em; }
.gradio-container .ob-observatory-overlay h3 { color:#eef3ff; margin:10px 0; font:500 28px/1.2 var(--ob-font); letter-spacing:-.04em; }
.gradio-container .ob-observatory-overlay p { color:#b8c7e0; font:12px/1.65 var(--ob-font); overflow-wrap:anywhere; max-height:80px; overflow:auto; }
.gradio-container .ob-observatory-overlay small { color:#91a5c6; font:10px/1.5 var(--ob-mono); }
.gradio-container .ob-observatory-pause { position:absolute; bottom:13px; right:15px; color:#cabbf2; border:1px solid #b59aff55; border-radius:6px; background:#15152c; padding:6px 10px; font:10px var(--ob-font); }
.gradio-container .ob-event-list { margin:0; padding:14px; list-style:none; max-height:260px; overflow:auto; border-left:1px solid var(--ob-line); }
.gradio-container .ob-event { display:flex; justify-content:space-between; align-items:center; gap:12px; padding:8px 0; color:var(--ob-muted); font:11px/1.5 var(--ob-font); border-bottom:1px solid var(--ob-line); }
.gradio-container .ob-event strong { font:9px var(--ob-mono); color:var(--ob-dim); }
.gradio-container .ob-event-running strong { color:var(--ob-violet); }
.gradio-container .ob-event-done strong { color:var(--ob-cyan); }
.gradio-container .ob-event-error strong { color:var(--ob-danger); }
.gradio-container .ob-event-empty { color:var(--ob-dim); font:12px/1.7 var(--ob-font); padding:10px; }
.gradio-container .ob-observatory-footnote { grid-column:1/-1; padding:10px 14px; border-top:1px solid var(--ob-line); color:var(--ob-dim); font:10px/1.6 var(--ob-font); }
.gradio-container .ob-observatory-details { grid-column:1/-1; padding:0 14px 12px; }
.gradio-container .ob-observatory-details summary { color:var(--ob-muted); font:11px/1.5 var(--ob-font); cursor:pointer; }
.gradio-container .ob-observatory-details pre { white-space:pre-wrap; overflow-wrap:anywhere; color:var(--ob-muted); font:10px/1.7 var(--ob-mono); max-height:180px; overflow:auto; }
@media(max-width:700px) {
.gradio-container .ob-observatory-body { grid-template-columns:1fr; }
.gradio-container .ob-event-list { display:flex; gap:14px; max-height:110px; border-left:0; border-top:1px solid var(--ob-line); }
.gradio-container .ob-event { min-width:145px; }
.gradio-container .ob-observatory-summary { font-size:9px; }
}
"""
OBSERVATORY_JS = """
const state = element._obObservatoryView ||= {open:false, paused:false, angle:0};
let dispose = () => {};
function mount() {
dispose();
const root=element.querySelector('.ob-observatory');
if (!root) return;
const data=JSON.parse(root.dataset.observatory);
const body=root.querySelector('.ob-observatory-body');
const toggle=root.querySelector('.ob-observatory-toggle');
const pause=root.querySelector('.ob-observatory-pause');
const canvas=root.querySelector('canvas');
const ctx=canvas.getContext('2d');
const media=window.matchMedia('(prefers-reduced-motion: reduce)');
const listeners=new AbortController();
const listen=(target,event,fn)=>target.addEventListener(event,fn,{signal:listeners.signal});
const received=performance.now();
let frame=0, visible=true, alive=true, width=1, height=1, previous=0;
function stop(){if(frame)cancelAnimationFrame(frame);frame=0;previous=0;}
function canMove(){return alive&&state.open&&!state.paused&&!media.matches&&visible&&!document.hidden&&data.status==='running'&&performance.now()-received<4000;}
function draw(){
if(!ctx||!state.open)return;
if(data.status==='running'&&performance.now()-received>=4000)root.querySelector('.ob-observatory-summary').textContent='Awaiting update · last reported Running';
ctx.clearRect(0,0,width,height);
const cx=width*.70,cy=height*.47,rx=Math.min(width*.32,260),ry=height*.28;
ctx.save();ctx.translate(cx,cy);ctx.rotate(-.28);
for(let plane=0;plane<3;plane++){
ctx.beginPath();ctx.ellipse(0,0,rx*(1-plane*.14),ry*(1+plane*.2),plane*.5,0,Math.PI*2);
ctx.strokeStyle=plane===0?'#b59aff55':'#79d4db22';ctx.lineWidth=1;ctx.stroke();
}
const phases=data.phases||[];
phases.forEach((phase,i)=>{
const a=(i/Math.max(phases.length,1))*Math.PI*2-.8;
const x=Math.cos(a)*rx,y=Math.sin(a)*ry;
const color=phase.status==='done'?'#79d4db':phase.status==='error'?'#f49aab':'#b59aff';
const r=phase.status==='running'?5+Math.sin(state.angle*2)*1.2:4;
ctx.beginPath();ctx.arc(x,y,r,0,Math.PI*2);ctx.fillStyle=color;ctx.shadowColor=color;ctx.shadowBlur=16;ctx.fill();ctx.shadowBlur=0;
});
ctx.restore();
}
function resize(){
if(!state.open)return;
const rect=canvas.getBoundingClientRect();width=Math.max(1,rect.width);height=Math.max(1,rect.height);
const ratio=Math.min(window.devicePixelRatio||1,2);canvas.width=width*ratio;canvas.height=height*ratio;
if(ctx)ctx.setTransform(ratio,0,0,ratio,0,0);draw();
}
function schedule(){if(canMove()&&!frame)frame=requestAnimationFrame(tick);}
function tick(now){frame=0;if(!canMove()){draw();return;}if(previous)state.angle+=(now-previous)*.001;previous=now;draw();schedule();}
function update(){
body.hidden=!state.open;toggle.setAttribute('aria-expanded',String(state.open));
toggle.textContent=state.open?'Close operation observatory':'Open operation observatory';
pause.textContent=state.paused?'Resume motion':'Pause motion';pause.setAttribute('aria-pressed',String(state.paused));
root.dataset.motion=state.paused||media.matches||!state.open?'paused':'playing';
stop();resize();schedule();
}
listen(toggle,'click',()=>{state.open=!state.open;update();});
listen(pause,'click',()=>{state.paused=!state.paused;update();});
listen(document,'visibilitychange',()=>{stop();schedule();});
listen(media,'change',update);
const resizeObserver=new ResizeObserver(resize);resizeObserver.observe(canvas);
const intersection=new IntersectionObserver(entries=>{visible=entries[0].isIntersecting;stop();draw();schedule();});intersection.observe(canvas);
const detached=new MutationObserver(()=>{if(!element.isConnected)dispose();});
detached.observe(document.documentElement,{childList:true,subtree:true});
dispose=()=>{alive=false;stop();listeners.abort();resizeObserver.disconnect();intersection.disconnect();detached.disconnect();};
update();
}
watch('value',mount);
mount();
"""