signaltrail / tree / demo/demo.mjs · raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Demo: run SignalTrail on the recorded demo dataset, then show replay, tamper detection and the practice portfolio.
// Local by default (starts server.js); SIGNALTRAIL_URL=https://signaltrail.agentgate.workers.dev runs against the live Worker.
import { spawn } from 'node:child_process';
const REMOTE = process.env.SIGNALTRAIL_URL; const PORT = process.env.PORT || 8798;
const BASE = REMOTE || `http://localhost:${PORT}`;
const BOOK = process.env.BOOK || 'demo-' + new Date().toISOString().slice(0, 16).replace(/[-:T]/g, '');
let srv;
if (!REMOTE) { srv = spawn(process.execPath, ['--no-warnings', new URL('../server.js', import.meta.url).pathname], { env: { ...process.env, PORT }, stdio: ['ignore', 'pipe', 'inherit'] }); await new Promise((r) => srv.stdout.once('data', r)); }
const j = async (p, init) => (await fetch(BASE + p, init)).json();
const post = (p, b) => j(p, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(b) });
try {
  const ds = await j('/v1/dataset'); const cfg = await j('/v1/config');
  console.log(`Dataset: ${ds.posts.length} posts from ${new Set(ds.posts.map((p) => p.author)).size} accounts (fictional @demo_* personas); ${cfg.creators.length} creators selected; rule: ≥${cfg.signal.min_voices} bullish voices in ${cfg.signal.window_hours}h, ≤${cfg.signal.max_bearish_voices} bearish`);
  console.log(`Practice book: ${BOOK} (paper portfolio, $10,000 start)`);
  const out = await post('/v1/run', { book: BOOK });
  if (out.error) { console.log('run:', out.error, out.detail || ''); process.exit(1); }
  for (const x of out.excluded_posts) console.log(`\nEXCLUDED ${x.post_id} ${x.author}: ${x.reason}\n   "${x.text_excerpt.slice(0, 90)}…"`);
  for (const x of out.decisions) {
    console.log(`\n#${x.seq} ${x.action === 'OPEN_PRACTICE_LONG' ? '[PRACTICE LONG]' : '[SKIP]'} ${x.verdict}`);
    console.log(`   narrative: ${x.narrative} · bullish: ${x.voices.bullish.join(', ') || '-'}${x.voices.bearish.length ? ' · bearish: ' + x.voices.bearish.join(', ') : ''}`);
    for (const b of x.bull_case.slice(0, 3)) console.log('   + ' + b);
    for (const b of x.bear_case.slice(0, 3)) console.log('   - ' + b);
    if (x.vetoes.length) console.log('   VETO ' + x.vetoes.slice(0, 2).join(' | '));
    if (x.position) console.log(`   paper: ${x.position.qty} @ $${x.position.entry_price_usd} · stop $${x.position.stop_price_usd} · target $${x.position.take_profit_price_usd}`);
  }
  const first = out.decisions.find((x) => x.symbol === 'AYB') || out.decisions[0];
  const rp = await j(`/v1/replay/${first.seq}`);
  console.log(`\nReplay #${first.seq} from its recorded evidence: reproducible=${rp.reproducible} (signals ${rp.steps?.signals}, narrative ${rp.steps?.narrative}, decision ${rp.steps?.decision}); signature valid=${rp.signature_valid}`);
  const rec = await j(`/v1/receipt/${first.seq}`);
  const forged = structuredClone(rec); if (forged.evidence.screen?.result) forged.evidence.screen.result.verdict = 'SAFE'; else forged.decision.action = 'OPEN_PRACTICE_LONG';
  const fr = await post('/v1/replay', forged);
  console.log(`Someone edits #${first.seq}'s TokenVerdict result to SAFE: signature valid=${fr.signature_valid} (${fr.signature_reason}); replay reproducible=${fr.reproducible} (recorded "${fr.recorded}" vs replayed "${fr.replayed}")`);
  const chain = await j('/v1/ledger/verify'); console.log(`Reasoning-trail hash chain: valid=${chain.valid}, ${chain.count} receipts`);
  const pf = await j('/v1/portfolio?book=' + BOOK);
  console.log(`\nPractice portfolio ${pf.book} (paper, $${pf.start_usd} start): cash $${pf.cash_usd}, marked equity $${pf.equity_marked_usd}`);
  for (const p of pf.positions) console.log(`   ${p.symbol}: $${p.size_usd} @ ${p.entry_price_usd} → ${p.mark_price_usd} (${p.unrealized_pnl_usd >= 0 ? '+' : ''}${p.unrealized_pnl_usd} USD)`);
} finally { srv?.kill(); }

agentgate-source: code mirror on Cloudflare Pages. All repos