Learn — status (live)

This note refreshes itself automatically every 5 seconds while it’s open — just leave it in a pane while a book processes. It runs learn dashboard --json directly each time (a fast, local SQLite/filesystem query — no network, no API calls), so what you see here is always current, not a snapshot.

const { execSync } = require("child_process");
 
if (!document.getElementById("learn-status-style")) {
  const style = document.createElement("style");
  style.id = "learn-status-style";
  style.textContent = `
    @keyframes learn-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }
    .learn-status-root { font-family: var(--font-interface); }
    .learn-status-card {
      border: 1px solid var(--background-modifier-border);
      border-radius: 8px;
      padding: 12px 14px;
      margin-bottom: 10px;
      background: var(--background-secondary);
    }
    .learn-status-badge {
      display: inline-block;
      padding: 2px 9px;
      border-radius: 10px;
      font-size: 0.72em;
      font-weight: 600;
      color: white;
      white-space: nowrap;
    }
    .learn-status-bar-track {
      flex: 1;
      height: 8px;
      border-radius: 4px;
      background: var(--background-modifier-border);
      overflow: hidden;
    }
    .learn-status-bar-fill {
      height: 100%;
      background: linear-gradient(90deg, var(--interactive-accent), #33c17b);
      transition: width 0.4s ease;
    }
    .learn-status-table { width: 100%; border-collapse: collapse; font-size: 0.9em; }
    .learn-status-table th {
      text-align: left; padding: 4px 8px;
      border-bottom: 1px solid var(--background-modifier-border);
      color: var(--text-muted); font-weight: 600;
    }
    .learn-status-table td { padding: 6px 8px; }
    .learn-status-crash {
      border-left: 3px solid var(--color-red);
      background: var(--background-secondary);
      border-radius: 4px;
      padding: 6px 10px;
      margin-bottom: 6px;
    }
    .learn-status-crash summary { cursor: pointer; font-size: 0.85em; }
    .learn-status-crash pre {
      white-space: pre-wrap; font-size: 0.78em; margin: 6px 0 0;
      color: var(--text-muted);
    }
  `;
  document.head.appendChild(style);
}
 
const STAGE_COLORS = {
  ingest: "#8e8e93", skeleton: "#5e9eff", analyze: "#a374ff",
  verify: "#2bb3a3", external_verify: "#2bb3a3", graph: "#f5a623",
  render: "#33c17b", starting: "#8e8e93",
};
const STATE_COLORS = { done: "#33c17b", partial: "#f5a623", "not analyzed": "#8e8e93" };
 
function timeAgo(iso) {
  if (!iso) return "never";
  const mins = Math.round((Date.now() - new Date(iso).getTime()) / 60000);
  if (mins < 1) return "just now";
  if (mins < 60) return `${mins}m ago`;
  const hours = Math.round(mins / 60);
  if (hours < 24) return `${hours}h ago`;
  return `${Math.round(hours / 24)}d ago`;
}
 
function badge(parent, text, color) {
  return parent.createEl("span", { cls: "learn-status-badge", text, attr: { style: `background:${color};` } });
}
 
function progressBar(parent, done, total) {
  const row = parent.createEl("div", { attr: { style: "display:flex; align-items:center; gap:8px; margin-bottom:6px;" } });
  const track = row.createEl("div", { cls: "learn-status-bar-track" });
  const pct = total > 0 ? Math.min(100, Math.round((done / total) * 100)) : 0;
  track.createEl("div", { cls: "learn-status-bar-fill", attr: { style: `width:${pct}%;` } });
  row.createEl("span", {
    text: `${done}/${total}`,
    attr: { style: "font-size:0.8em; color:var(--text-muted); white-space:nowrap;" },
  });
}
 
const root = dv.container.createEl("div", { cls: "learn-status-root" });
const header = root.createEl("div", { attr: { style: "display:flex; align-items:center; gap:8px; margin-bottom:12px;" } });
header.createEl("span", {
  attr: { style: "width:10px; height:10px; border-radius:50%; background:var(--color-green); box-shadow:0 0 6px var(--color-green); animation: learn-pulse 1.5s ease-in-out infinite; display:inline-block;" },
});
header.createEl("span", { text: "Learn — Live Status", attr: { style: "font-weight:700; font-size:1.1em;" } });
const updatedEl = header.createEl("span", { attr: { style: "margin-left:auto; font-size:0.8em; color:var(--text-muted);" } });
 
const body = root.createEl("div");
 
function refresh() {
  let data;
  try {
    const output = execSync('"/Users/polymath/Learn/.venv/bin/learn" dashboard --json', {
      cwd: "/Users/polymath/Learn",
      timeout: 5000,
    }).toString();
    data = JSON.parse(output);
  } catch (e) {
    body.empty();
    body.createEl("pre", {
      text: "Error running `learn dashboard --json`:\n" + (e.stdout ? e.stdout.toString() : e.message),
      attr: { style: "color:var(--color-red); font-size:0.85em; white-space:pre-wrap;" },
    });
    return;
  }
 
  try {
    updatedEl.setText(`updated ${timeAgo(data.generated_at)}`);
    body.empty();
 
    if (data.running.length > 0) {
      for (const r of data.running) {
        const card = body.createEl("div", { cls: "learn-status-card" });
        const top = card.createEl("div", { attr: { style: "display:flex; align-items:center; gap:8px; margin-bottom:8px;" } });
        top.createEl("span", { text: r.book_id, attr: { style: "font-weight:600;" } });
        badge(top, r.stage, STAGE_COLORS[r.stage] || "#8e8e93");
 
        // chapters_done/chapters_total only ever tracks the analyze stage —
        // once a book moves on (verify/external_verify/graph/render) this
        // number freezes, which used to make a perfectly healthy run LOOK
        // stalled (confirmed live: The Redbook sat at "48/52" for 15+
        // minutes while verify was completing real calls every few
        // seconds). Mute it once it's known-stale and let the freshness
        // line below carry the real signal instead.
        const chaptersStale = r.stage !== "analyze";
        const barWrap = card.createEl("div", { attr: { style: chaptersStale ? "opacity:0.45;" : "" } });
        progressBar(barWrap, r.chapters_done, r.chapters_total);
        if (chaptersStale) {
          barWrap.createEl("div", {
            text: "(chapters count only tracks analyze — frozen since that stage finished)",
            attr: { style: "font-size:0.72em; color:var(--text-muted); margin:-2px 0 6px;" },
          });
        }
 
        const eta = r.eta_minutes !== null ? `~${Math.round(r.eta_minutes)} min left (rough)` : "estimating…";
        card.createEl("div", {
          text: `running ${Math.round(r.elapsed_minutes)} min · ${eta}`,
          attr: { style: "font-size:0.8em; color:var(--text-muted);" },
        });
 
        const freshRow = card.createEl("div", { attr: { style: "display:flex; align-items:center; gap:6px; margin-top:4px;" } });
        const fresh = r.last_activity_seconds !== null && r.last_activity_seconds < 30;
        freshRow.createEl("span", {
          attr: { style: `width:7px; height:7px; border-radius:50%; display:inline-block; background:${fresh ? "var(--color-green)" : "var(--color-orange)"};` },
        });
        const lastActivityText = r.last_activity_seconds !== null
          ? `${Math.round(r.last_activity_seconds)}s ago`
          : "no activity yet";
        freshRow.createEl("span", {
          text: `${r.stage_items_done} item(s) completed in this stage · last activity ${lastActivityText}`,
          attr: { style: "font-size:0.78em; color:var(--text-muted); font-weight:600;" },
        });
      }
    } else {
      body.createEl("div", {
        text: "Nothing running right now.",
        attr: { style: "color:var(--text-muted); font-style:italic; margin-bottom:14px;" },
      });
    }
 
    if (data.stale_locks.length > 0) {
      body.createEl("div", {
        text: "âš  Stale locks (process died without a clean crash record)",
        attr: { style: "font-weight:700; margin:14px 0 8px; color:var(--color-red);" },
      });
      for (const r of data.stale_locks) {
        body.createEl("div", {
          text: `${r.book_id}: ${r.command} (pid ${r.pid}, started ${r.started_at})`,
          attr: { style: "font-size:0.85em; color:var(--text-muted); margin-bottom:4px;" },
        });
      }
    }
 
    body.createEl("div", { text: "Books", attr: { style: "font-weight:700; margin:14px 0 8px;" } });
    const table = body.createEl("table", { cls: "learn-status-table" });
    const headRow = table.createEl("thead").createEl("tr");
    for (const h of ["Book", "State", "Chapters", "Spend", "Last activity"]) {
      headRow.createEl("th", { text: h });
    }
    const tbody = table.createEl("tbody");
    for (const b of data.books) {
      const row = tbody.createEl("tr");
      row.createEl("td", { text: b.book_id, attr: { style: "font-weight:500;" } });
      const stateCell = row.createEl("td");
      badge(stateCell, b.state, STATE_COLORS[b.state] || "#8e8e93");
      row.createEl("td", { text: `${b.n_analyzed}/${b.n_chunked}` });
      row.createEl("td", { text: `$${b.spend.toFixed(4)}` });
      row.createEl("td", { text: timeAgo(b.last_activity), attr: { style: "color:var(--text-muted);" } });
    }
 
    if (data.crashes.length > 0) {
      body.createEl("div", {
        text: "Recent crashes",
        attr: { style: "font-weight:700; margin:16px 0 8px; color:var(--color-red);" },
      });
      for (const c of data.crashes) {
        const details = body.createEl("details", { cls: "learn-status-crash" });
        details.createEl("summary", {
          text: `${timeAgo(c.occurred_at)} · ${c.book_id || "(no book)"} · ${c.error_type}`,
        });
        details.createEl("pre", { text: c.message });
      }
    }
 
    if (data.quarantines.length > 0) {
      body.createEl("div", {
        text: "Recent quarantines",
        attr: { style: "font-weight:700; margin:16px 0 8px; color:#f5a623;" },
      });
      const list = body.createEl("ul", { attr: { style: "margin:0; padding-left:18px; font-size:0.85em; color:var(--text-muted);" } });
      for (const q of data.quarantines) {
        list.createEl("li", { text: `${q.book_id} / ${q.stage} / ${q.reason} — ${q.n}× (last ${timeAgo(q.last_at)})` });
      }
    }
  } catch (e) {
    body.empty();
    body.createEl("pre", {
      text: "Error rendering status: " + e.message,
      attr: { style: "color:var(--color-red); font-size:0.85em; white-space:pre-wrap;" },
    });
  }
}
 
refresh();
const interval = setInterval(refresh, 5000);
dv.component.register(() => clearInterval(interval));