main
John Lauer Publish 0.2.2 6e403aa
#!/usr/bin/env python3
"""Adom SPICE: one dashboard for ngspice, LTspice and PSpice for TI.
Runs a study in any engine, overlays the waveforms, measures every engine with the same code, flags
disagreement, and keeps a ledger of simulation time and AI tokens per run."""
import json, os, queue, re, signal, socket, sys, threading, time, traceback
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from adom_spice import engines, analysis  # noqa: E402

VERSION = '0.1.0'
SLUG = 'adom-spice'
HOME = os.path.expanduser('~')
INST = os.path.join(HOME, '.adom', 'instances', SLUG)
WEB = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'web')
FONTS = os.path.join(HOME, '.local', 'share', 'fonts', 'adom-theme')
os.makedirs(os.path.join(INST, 'snapshots'), exist_ok=True)
os.makedirs(os.path.join(INST, 'runs'), exist_ok=True)

DEFAULT_SETTINGS = {
    'bridgeTarget': '',
    'windowsWorkDir': '',
    'ltspiceExe': '',
    'pspiceExe': 'C:\\Cadence\\PSpiceTI\\tools\\bin\\pspice.exe',
    'pspiceRunButton': [0.4314, 0.0609],
    'timeoutSeconds': 900,
    'relTolerancePct': 3.0,
    'hideVendorTimes': True,
    'pricePerMTokLow': 4.0,
    'pricePerMTokHigh': 5.5,
    'ttlHours': 24,
    'defaultSurface': 'wv',
}
ENUMS = {'defaultSurface': ['wv', 'hbw', 'pup', 'nb'], 'hideVendorTimes': [True, False]}
SETTINGS_HELP = {
    'bridgeTarget': 'Adom Bridge machine that runs LTspice and PSpice for TI (Windows). Empty = your primary machine.',
    'windowsWorkDir': 'Folder on that machine for netlists and results. Empty = %USERPROFILE%\\Downloads\\adom-spice.',
    'ltspiceExe': 'LTspice.exe on that machine. Empty = the per-user install in %LOCALAPPDATA%\\Programs\\ADI\\LTspice.',
    'pspiceRunButton': 'Where the Run button sits in a full-screen capture of maximized PSpice for TI, as [x, y] fractions. Recalibrate if runs never start.',
    'hideVendorTimes': 'Hide LTspice and PSpice run times (their licences restrict publishing benchmarks). Turn off for private use.',
    'relTolerancePct': 'Engines "agree" on a measurement when their spread is within this percent of the median (or the per-measurement absolute limit).',
    'ttlHours': 'Idle hours before the server saves its state and shuts itself down.',
    'defaultSurface': 'Where `adom-spice show` opens the dashboard.',
}
COLORS = {'ngspice': '#2dd4bf', 'ltspice': '#ffb74d', 'pspice': '#f472b6', 'reference': '#ffffff'}


def load_settings():
    p = os.path.join(INST, 'settings.json')
    s = dict(DEFAULT_SETTINGS)
    if os.path.exists(p):
        try: s.update(json.load(open(p)))
        except Exception: pass
    return s


SETTINGS = load_settings()
STATE = {'version': VERSION, 'study': None, 'runs': [], 'agreement': [], 'verdict': None, 'busy': None, 'log': []}
WAVES = {}            # run id -> {view index: [x..], [y..]}
RAW_GRIDS = {}        # run id -> {view: (g, {sig: arr})}
CLIENTS = set(); LOCK = threading.RLock(); JOBS = queue.Queue()
LAST = [time.time()]; OWNER = [os.environ.get('ADOM_AI_THREAD', '')]
PORT = [0]


def broadcast(event, data):
    msg = ('event: ' + event + '\ndata: ' + json.dumps(data, default=float) + '\n\n').encode()
    for c in list(CLIENTS):
        try: c.write(msg); c.flush()
        except Exception: CLIENTS.discard(c)


def public_state():
    s = dict(STATE); s['settings'] = SETTINGS; s['colors'] = COLORS; s['waves'] = WAVES
    runs = []
    for r in STATE['runs']:
        r = dict(r)
        if SETTINGS['hideVendorTimes'] and r['engine'] in ('ltspice', 'pspice'): r['wall_s'] = None; r['timeHidden'] = True
        runs.append(r)
    s['runs'] = runs
    return s


def push():
    broadcast('state', public_state()); snapshot()


def log(line, thread=''):
    with LOCK:
        STATE['log'] = (STATE['log'] + [{'t': time.strftime('%H:%M:%S'), 'thread': thread, 'line': line}])[-200:]
    broadcast('log', STATE['log'][-1])


def snapshot():
    name = re.sub(r'[^a-z0-9]+', '-', (OWNER[0] or 'default').lower()).strip('-') or 'default'
    try: json.dump({'study': STATE['study'], 'runs': STATE['runs']}, open(os.path.join(INST, 'snapshots', name + '.json'), 'w'), default=float)
    except Exception: pass


def restore():
    name = re.sub(r'[^a-z0-9]+', '-', (OWNER[0] or 'default').lower()).strip('-') or 'default'
    p = os.path.join(INST, 'snapshots', name + '.json')
    if not os.path.exists(p): return False
    d = json.load(open(p)); STATE['study'] = d.get('study'); STATE['runs'] = d.get('runs', [])
    for r in STATE['runs']:
        f = os.path.join(INST, 'runs', r['id'] + '.json')
        if os.path.exists(f): WAVES[r['id']] = json.load(open(f))
    recompute(); return True


def registry_path():
    name = re.sub(r'[^a-z0-9]+', '-', (OWNER[0] or 'default').lower()).strip('-') or 'default'
    return os.path.join(INST, name + '.reg.json')


def register():
    json.dump({'pid': os.getpid(), 'port': PORT[0], 'thread': OWNER[0], 'task': (STATE['study'] or {}).get('name'),
               'version': VERSION, 'startedAt': time.strftime('%Y-%m-%dT%H:%M:%S'), 'lastActivity': LAST[0]},
              open(registry_path(), 'w'))


# ------------------------------------------------------------------ study + runs
def load_study(path):
    st = json.load(open(path)); base = os.path.dirname(os.path.abspath(path))
    for k, v in st['netlists'].items(): st['netlists'][k] = os.path.join(base, v) if not os.path.isabs(v) else v
    st['path'] = os.path.abspath(path)
    for ref in st.get('references', []):
        if not os.path.isabs(ref['file']): ref['file'] = os.path.join(base, ref['file'])
    return st


def add_run(engine, label, t, sigs, wall, thread, extra=None):
    rid = f"{engine}-{time.strftime('%H%M%S')}-{len(STATE['runs'])}"
    views = STATE['study']['views']; waves = {}
    for i, v in enumerate(views):
        g, s = analysis.resample(t, sigs, v['from'], v['to'], v.get('n', 1500))
        waves[str(i)] = {'x': g.tolist(), 'y': s[v['signal']].tolist()}
    metrics = analysis.measure(t, sigs, STATE['study']['measurements'])
    run = {'id': rid, 'engine': engine, 'label': label, 'status': 'done', 'wall_s': wall, 'thread': thread,
           'started': time.strftime('%H:%M:%S'), 'metrics': metrics, 'tokens': None, 'toolCalls': None, 'visible': True}
    if extra: run.update(extra)
    json.dump(waves, open(os.path.join(INST, 'runs', rid + '.json'), 'w'))
    with LOCK:
        STATE['runs'] = [r for r in STATE['runs'] if not (r['engine'] == engine and r['label'] == label)] + [run]
        for k in list(WAVES):
            if k not in {r['id'] for r in STATE['runs']}: WAVES.pop(k)
        WAVES[rid] = waves
    recompute(); return run


def recompute():
    st = STATE['study']
    if not st: return
    engs = {r['engine']: r['metrics'] for r in STATE['runs'] if r['engine'] in ('ngspice', 'ltspice', 'pspice') and r['status'] == 'done'}
    tol = {'rel': SETTINGS['relTolerancePct'] / 100.0, 'abs': {}}
    rows = analysis.agreement(engs, st['measurements'], tol)
    refs = [r for r in STATE['runs'] if r['engine'] == 'reference']
    for row in rows:
        row['reference'] = {r['label']: r['metrics'].get(row['metric']) for r in refs}
    STATE['agreement'] = rows
    checked = [r for r in rows if r['ok'] is not None]
    names = {'ngspice': 'ngspice', 'ltspice': 'LTspice', 'pspice': 'PSpice for TI'}
    if not engs: STATE['verdict'] = {'kind': 'pending', 'text': 'Pick an engine and press Run (ngspice runs right here in the container).'}
    elif len(engs) == 1:
        e = next(iter(engs)); n = len(st['measurements'])
        STATE['verdict'] = {'kind': 'single', 'text': f'{names[e]} run complete: {n} measurements below. Run a second engine any time to cross-check it.'}
    elif all(r['ok'] for r in checked):
        STATE['verdict'] = {'kind': 'agree', 'text': f'{len(engs)} engines agree on all {len(checked)} measurements.'}
    else:
        bad = [r for r in checked if not r['ok']]
        STATE['verdict'] = {'kind': 'disagree', 'text': f'{len(bad)} of {len(checked)} measurements disagree: ' +
                            ', '.join(f"{r['label']} ({r['outlier']} is the outlier)" for r in bad[:3]) +
                            '. Check that engine\'s netlist translation and models first.'}


def worker():
    while True:
        engine, thread = JOBS.get()
        STATE['busy'] = {'engine': engine, 'since': time.strftime('%H:%M:%S')}; push()
        wd = os.path.join(INST, 'work', engine); os.makedirs(wd, exist_ok=True)
        try:
            t, sigs, wall, _ = engines.RUNNERS[engine](STATE['study'], wd, thread or f'{SLUG} worker', SETTINGS,
                                                       lambda s: log(s, thread))
            run = add_run(engine, STATE['study'].get('model', 'study model'), t, sigs, wall, thread)
            log(f'{engine}: done' + ('' if SETTINGS['hideVendorTimes'] and engine != 'ngspice' else f' in {wall:.1f} s'), thread)
        except Exception as e:
            log(f'{engine}: FAILED {e}', thread); traceback.print_exc()
        finally:
            STATE['busy'] = None; LAST[0] = time.time(); push()


# ------------------------------------------------------------------ HTTP
READ_ONLY = {'/state', '/settings', '/events', '/version', '/console'}


class H(BaseHTTPRequestHandler):
    def log_message(self, *a): pass

    def _json(self, code, obj):
        b = json.dumps(obj, default=float).encode()
        self.send_response(code); self.send_header('Content-Type', 'application/json'); self.send_header('Content-Length', str(len(b)))
        self.end_headers(); self.wfile.write(b)

    def _file(self, path, ctype):
        if not os.path.exists(path): return self._json(404, {'ok': False, 'error': 'not found'})
        b = open(path, 'rb').read()
        self.send_response(200); self.send_header('Content-Type', ctype); self.send_header('Content-Length', str(len(b)))
        self.send_header('Cache-Control', 'no-cache'); self.end_headers(); self.wfile.write(b)

    def do_GET(self):
        LAST[0] = time.time(); p = urlparse(self.path).path
        if p in ('/', '/index.html'): return self._file(os.path.join(WEB, 'index.html'), 'text/html; charset=utf-8')
        if p in ('/app.js', '/style.css'):
            return self._file(os.path.join(WEB, p[1:]), 'text/javascript' if p.endswith('js') else 'text/css')
        if p.startswith('/fonts/'): return self._file(os.path.join(FONTS, os.path.basename(p)), 'font/woff2')
        if p == '/state': return self._json(200, public_state())
        if p == '/version': return self._json(200, {'version': VERSION})
        if p == '/settings': return self._json(200, {'settings': SETTINGS, 'enums': ENUMS, 'help': SETTINGS_HELP})
        if p == '/console': return self._json(200, {'log': STATE['log']})
        if p == '/events':
            self.send_response(200); self.send_header('Content-Type', 'text/event-stream'); self.send_header('Cache-Control', 'no-cache')
            self.send_header('Connection', 'keep-alive'); self.send_header('X-Accel-Buffering', 'no'); self.end_headers()
            self.wfile.write((':' + ' ' * 2048 + '\n\n').encode())
            self.wfile.write(('event: state\ndata: ' + json.dumps(public_state(), default=float) + '\n\n').encode()); self.wfile.flush()
            CLIENTS.add(self.wfile)
            try:
                while True:
                    time.sleep(15); self.wfile.write(b': hb\n\n'); self.wfile.flush()
            except Exception: CLIENTS.discard(self.wfile)
            return
        return self._json(404, {'ok': False, 'error': 'unknown path', 'hint': 'GET /state, /settings, /events, /version, /console'})

    def do_POST(self):
        LAST[0] = time.time(); p = urlparse(self.path).path
        n = int(self.headers.get('Content-Length') or 0)
        try: body = json.loads(self.rfile.read(n) or b'{}')
        except Exception: return self._json(400, {'ok': False, 'error': 'body must be JSON'})
        thread = body.get('aiThread') or ''
        if not thread and not body.get('fromUi'):
            return self._json(400, {'ok': False, 'errorCode': 'caller_identity_required',
                                    'error': 'This app requires every AI caller to give its ai-thread name.',
                                    'hint': 'Re-run with the global flag: adom-spice --ai-thread "<name>" <verb>, or add a top-level "aiThread" field if POSTing directly. Use a short human-recognizable name for THIS conversation and reuse it everywhere.'})
        who = thread or 'dashboard user'
        try:
            if p == '/settings':
                bad = [k for k in body.get('set', {}) if k not in DEFAULT_SETTINGS]
                if bad: return self._json(400, {'ok': False, 'error': f'unknown setting(s) {bad}', 'hint': f'valid: {sorted(DEFAULT_SETTINGS)}'})
                for k, v in body.get('set', {}).items():
                    if k in ENUMS and v not in ENUMS[k]: return self._json(400, {'ok': False, 'error': f'{k} must be one of {ENUMS[k]}'})
                    SETTINGS[k] = v
                json.dump(SETTINGS, open(os.path.join(INST, 'settings.json'), 'w'), indent=1)
                recompute(); push(); broadcast('toast', {'text': 'Settings saved', 'kind': 'success'})
                return self._json(200, {'ok': True, 'settings': SETTINGS})
            if p == '/study':
                STATE['study'] = load_study(body['path']); STATE['runs'] = []; WAVES.clear()
                log(f'thread "{who}": loaded study {STATE["study"]["name"]}', who)
                for ref in STATE['study'].get('references', []):
                    t, s = engines.load_file(ref['kind'], ref['file'], ref['signals'])
                    add_run('reference', ref['label'], t, s, None, who)
                recompute(); register(); push()
                return self._json(200, {'ok': True, 'study': STATE['study']['name']})
            if p == '/run':
                if not STATE['study']: return self._json(400, {'ok': False, 'error': 'no study loaded', 'hint': 'adom-spice --ai-thread "<name>" study <study.json>'})
                engs = body.get('engines') or ['ngspice']
                bad = [e for e in engs if e not in engines.RUNNERS]
                if bad: return self._json(400, {'ok': False, 'error': f'unknown engine(s) {bad}', 'hint': 'engines: ngspice, ltspice, pspice'})
                for e in engs: JOBS.put((e, who)); log(f'thread "{who}": queued {e}', who)
                return self._json(200, {'ok': True, 'queued': engs})
            if p == '/ingest':
                sig = body['signals']; t, s = engines.load_file(body.get('kind', 'txt'), body['file'], sig)
                r = add_run(body.get('engine', 'reference'), body.get('label', os.path.basename(body['file'])), t, s, body.get('wall_s'), who)
                push(); return self._json(200, {'ok': True, 'run': r['id']})
            if p == '/tokens':
                for r in STATE['runs']:
                    if r['engine'] == body['engine'] and (not body.get('label') or r['label'] == body['label']):
                        r['tokens'] = int(body['tokens']); r['toolCalls'] = body.get('toolCalls')
                push(); return self._json(200, {'ok': True})
            if p == '/visible':
                for r in STATE['runs']:
                    if r['id'] == body['id']: r['visible'] = bool(body['visible'])
                push(); return self._json(200, {'ok': True})
            if p in ('/ui/click', '/ui/reveal', '/ui/toast'):
                broadcast(p.split('/')[-1], body); return self._json(200, {'ok': True})
            if p == '/shutdown':
                if not body.get('reason'):
                    return self._json(400, {'ok': False, 'errorCode': 'reason_required', 'error': 'shutdown needs a reason', 'hint': 'adom-spice --ai-thread "<name>" shutdown --reason "<why>"'})
                snapshot(); self._json(200, {'ok': True}); threading.Timer(0.3, lambda: os._exit(0)).start(); return
        except Exception as e:
            traceback.print_exc(); return self._json(500, {'ok': False, 'error': str(e)})
        return self._json(404, {'ok': False, 'error': 'unknown path'})


def reaper():
    while True:
        time.sleep(60)
        if SETTINGS.get('ttlHours') and time.time() - LAST[0] > float(SETTINGS['ttlHours']) * 3600 and not STATE['busy']:
            snapshot()
            try: os.remove(registry_path())
            except Exception: pass
            os._exit(0)


def main():
    port = int(os.environ.get('ADOM_SPICE_PORT', '0'))
    OWNER[0] = os.environ.get('ADOM_SPICE_THREAD', OWNER[0])
    srv = ThreadingHTTPServer(('0.0.0.0', port), H); PORT[0] = srv.server_address[1]
    if restore(): print(f'restored snapshot for thread "{OWNER[0]}"', flush=True)
    register()
    threading.Thread(target=worker, daemon=True).start()
    if '--keep-alive' not in sys.argv: threading.Thread(target=reaper, daemon=True).start()
    print(json.dumps({'ok': True, 'port': PORT[0], 'pid': os.getpid(), 'version': VERSION}), flush=True)
    signal.signal(signal.SIGTERM, lambda *a: (snapshot(), os._exit(0)))
    srv.serve_forever()


if __name__ == '__main__':
    main()