Files
learnharness/electron/main.cjs
T
amosblomqvist ad61301324 Appearance wave: searchable reading-font dropdown + emphasis inks default to text color
Two owner rulings (2026-09-26):

READING FONT — the free-text CommitInput is retired; a searchable
dropdown of the user's INSTALLED fonts over model-menu's exact
fuzzy + arrow-walk + Enter machine (FontMenuList in model-menu.tsx,
fed by window.queryLocalFonts in the desktop shell):

- The DEFAULT row (Source Sans 3) is a fuzzy CANDIDATE, not a ghost:
  fontMenuResults (pure, pinned) matches it on empty/source/sans/
  default queries only, so an unfiltered list opens with the default
  first and a searched list drops it honestly — Enter can never pick
  a row that isn't on screen. Picking it writes fontFamily null
  (empty = Source Sans 3 semantics preserved).
- The enumeration is CLICK-bound (queryLocalFonts requires user
  activation — it cannot be a mount effect), deduped to unique
  families, the factory family excluded (the DEFAULT row is it),
  cached for the section's life; a failed enumeration falls to the
  curated list rather than an empty picker.
- Dev-browser fallback is honest, never fabricated: queryLocalFonts
  absent → a small curated list (the app's own knowns + ubiquitous
  macOS families) with the footer 'Common families — your installed
  list needs the desktop app'. The manual escape lives inside the
  menu: a custom-family input + Set commits any family name the
  enumeration didn't surface (the old free-text input's heir).
- composeFontFamily (pure, pinned): null/blank → null, else verbatim.
- The trigger gets a Hint (tooltip wave convention); the row's
  empty-state hint keeps reading 'Empty = Source Sans 3'.

⚠ electron/main.cjs — one line, FLAGGED FOR PLATFORM: 'local-fonts'
joined the allowedPermissions set (the Local Font Access grant the
picker needs in the desktop shell; the loopback-origin discipline is
untouched).

EMPHASIS INKS — the factory #ee928c/#6fa9d6 are gone: both globals.css
theme blocks now default --lh-ink-strong/--lh-ink-italic to
var(--foreground) — unset emphasis renders the regular text color.
Explicit hex overrides ride unchanged (the appearance seam was already
null-honest); the rows' empty-state labels now say 'same as text'.

Tests (+16, 983 total): fontMenuResults default-row candidacy +
filtering, composeFontFamily null/blank/verbatim, the curated roster
(no factory-family duplicate), FontMenuList SSR shape (default row,
families, effective highlight, custom escape, loading state), the
trigger-replaces-input pin, and the globals.css factory pin (no old
hexes in the declarations; var(--foreground) ×2 in both blocks).

Live-verified on :3001 (dev browser — the curated-fallback path):
dropdown opens with the default row + curated list + honest footer;
search filters ('serif' → CMU Serif only); picking Georgia lands
record + --font-prose live; the default row unsets (var cleared);
the custom input commits Baskerville verbatim; Esc closes the menu
WITHOUT closing the dialog. Ink: strong/em computed = rgb(230,230,230)
(the text color) at factory; #ff0000 through the dialog renders red
and clears back cleanly. The desktop queryLocalFonts path cannot run
in this environment — the seam is click-bound + cached with the
failure fallback, and needs one desktop-shell smoke.
2026-09-25 20:02:19 +03:00

331 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* LearnHarness — the Electron shell (wrapper milestone, 2026-09-17).
*
* What this process is and is not:
* IS: a thin launcher + security guard around the app's real server —
* `next build`'s standalone server.js (Next 16), spawned as a child
* on 127.0.0.1 with the BYOK auth file and the LH_DATA_DIR seam.
* NOT: a host for any app logic. Everything — teacher turns, extraction,
* the graph — runs in the Next server, unchanged.
*
* Pattern (fact-checked 2026-09-17 against current Electron 44 / Next 16
* docs — see .memory release-strategy + electron-wrapper notes):
* - spawn via process.execPath + ELECTRON_RUN_AS_NODE=1 (Electron's
* bundled Node 24; the runAsNode fuse must stay enabled);
* - the server dir ships UNPACKED via extraResources (asar archives are
* read-only and native .node/dylib can't dlopen from one);
* - all mutable state under userData (never inside the .app bundle —
* translocated apps are read-only);
* - keys ride the child's ENV, never argv; auth.json (BYOK) is a flat
* {VAR: value} JSON map, plaintext, mode 0600 — server-managed since
* the auth-keys wave (2026-09-19): the child reads/writes it DIRECTLY
* (LH_AUTH_FILE below) and reflects values into its own env. The old
* spawn-time env injection retired with that wave — a copy made here
* would shadow every settings-page key change until restart.
*/
const { app, BrowserWindow, Menu, ipcMain, session, shell } = require("electron");
const { spawn } = require("node:child_process");
const fs = require("node:fs");
const http = require("node:http");
const net = require("node:net");
const path = require("node:path");
// Mirrors SIDEBAR_AUTOCLOSE in components/sidebar.tsx — keep in sync (the
// renderer owns the number; this is the IPC widen target).
const SIDEBAR_AUTOCLOSE = 928;
const PRODUCT_NAME = "LearnHarness";
const AUTH_FILE = () => path.join(app.getPath("userData"), "auth.json");
const DATA_DIR = () => path.join(app.getPath("userData"), "data");
/** The packaged standalone server (extraResources → Resources/server).
* In dev LH_DEV_URL short-circuits everything: no spawn, no packaging —
* the window points at the running `next dev` server (full HMR). */
function serverEntry() {
// extraResources ships the whole .next payload under server/standalone/
// (copied from .next with an explicit filter — see electron-builder.yml).
return path.join(process.resourcesPath, "server", "standalone", "server.js");
}
// ─── Single instance: two instances would fight over the data dir ─────────
// Dev shell (LH_DEV_URL) runs BESIDE the packaged app, not instead of it:
// its own userData so (a) the single-instance lock — checked below — doesn't
// silently kill it, and (b) it never reads/writes the real app's
// localStorage, auth.json, or logs. Must run before the lock check.
if (process.env.LH_DEV_URL) {
app.setPath("userData", `${app.getPath("userData")}-dev`);
}
if (!app.requestSingleInstanceLock()) {
app.quit();
} else {
app.on("second-instance", () => {
const win = BrowserWindow.getAllWindows()[0];
if (win) {
if (win.isMinimized()) win.restore();
win.focus();
}
});
main();
}
let serverProcess = null;
/** A free TCP port (listen(0) → the kernel assigns one). The close→spawn
* race is theoretical; localhost ports are rarely contended, and a failed
* spawn surfaces in the health check with the child's stderr in the log. */
function freePort() {
return new Promise((resolve, reject) => {
const srv = net.createServer();
srv.listen(0, "127.0.0.1", () => {
const { port } = srv.address();
srv.close(() => resolve(port));
});
srv.on("error", reject);
});
}
/** BYOK keys: auth.json in userData, flat {VAR: value} map, 0600 —
* SERVER-MANAGED since the auth-keys wave (2026-09-19). This shell no
* longer reads or injects it: the child gets LH_AUTH_FILE below and does
* its own read/reflect/write, so a key saved in the settings page takes
* effect on the next model call — no restart. Real environment variables
* still win over the file inside the child (its documented rule). */
function log(line) {
const stamp = new Date().toISOString();
console.log(`[lh ${stamp}] ${line}`);
// Belt and braces: also append to userData/logs so a misbehaving child
// can be diagnosed after the window is gone. Best effort only.
try {
const dir = path.join(app.getPath("userData"), "logs");
fs.mkdirSync(dir, { recursive: true });
fs.appendFileSync(path.join(dir, "main.log"), `[${stamp}] ${line}\n`);
} catch {
/* best effort */
}
}
function startServer(port) {
return new Promise((resolve, reject) => {
const child = spawn(
process.execPath,
[serverEntry()],
{
cwd: path.dirname(serverEntry()),
env: {
...process.env,
ELECTRON_RUN_AS_NODE: "1",
NODE_ENV: "production",
PORT: String(port),
HOSTNAME: "127.0.0.1", // the server defaults to 0.0.0.0 — never
LH_DATA_DIR: DATA_DIR(),
// The auth/keys store — the child reads AND writes this file itself
// (no env injection anymore: a spawn-time copy would shadow every
// settings-page key change until restart; env wins over file).
LH_AUTH_FILE: AUTH_FILE(),
},
stdio: ["ignore", "pipe", "pipe"],
},
);
serverProcess = child;
const fwd = (buf) => buf.toString().trimEnd().split("\n").forEach((l) => l && log(`server: ${l}`));
child.stdout.on("data", fwd);
child.stderr.on("data", fwd);
child.on("exit", (code, signal) => {
log(`server exited (code=${code} signal=${signal})`);
serverProcess = null;
});
resolve(child);
});
}
/** No "ready" event exists — poll until the server answers (docs-confirmed). */
function waitUntilUp(url, tries = 150) {
return new Promise((resolve, reject) => {
const attempt = (n) => {
const req = http.get(url, (res) => {
res.resume();
resolve(res.statusCode !== undefined);
});
req.on("error", () => {
if (n <= 0) reject(new Error(`server never answered on ${url}`));
else setTimeout(() => attempt(n - 1), 200);
});
};
attempt(tries);
});
}
function killServer() {
if (!serverProcess) return;
const child = serverProcess;
serverProcess = null;
child.kill("SIGTERM");
setTimeout(() => {
try {
child.kill("SIGKILL");
} catch {
/* already gone */
}
}, 3000);
}
/** Security posture (all defaults asserted explicitly; the guards are the
* checklist's own recommendations for localhost-app shells):
* - deny-by-default permission handler allowing only our loopback origin
* (groundwork for the mic/STT milestone — Electron auto-approves by
* default, we do not);
* - will-navigate pinned to the app origin; window.open always denied —
* external links go to the real browser via shell.openExternal. */
function createWindow(url) {
const win = new BrowserWindow({
width: 1440,
height: 900,
title: PRODUCT_NAME,
// Merged chrome (owner, 2026-09-17): the system title bar is gone;
// the app's own 60px header bands ARE the title bar. The traffic
// lights float over the sidebar's LH wordmark band, vertically
// Aligned to the PHASE PILL's drawn box, not the band's geometric
// center (owner ask, 2026-09-17): macOS centers each 12px circle at
// y+6.75, and the pill box center measures 29.25 in the running app
// (band 0–59.5, pill box 19–39.5) — y 22.5 lands the circles at
// 29.25, exactly on the pill. Wordmark gutter: components/sidebar.tsx.
// Dragging comes from the renderer's .lh-drag bands (app/globals.css).
titleBarStyle: "hiddenInset",
trafficLightPosition: { x: 16, y: 22.5 },
webPreferences: {
contextIsolation: true,
sandbox: true,
nodeIntegration: false,
preload: path.join(__dirname, "preload.cjs"),
},
// Live-resize + first-paint flash (owner, 2026-09-17): during a resize
// the native window surface changes size before Chromium's compositor
// has a frame at the new size, and the gap shows the window's NATIVE
// background — Electron's default is white, glaring on a dark app.
// The app background (#141414) fills the lag instead. `show: false` +
// ready-to-show is the same treatment for first paint: the window
// appears only once the page has actually rendered.
backgroundColor: "#141414",
show: false,
// Narrow-window behavior (owner, 2026-09-17): 640px is the floor —
// below it even a wrapped chrome row gets cramped and the chat header
// (lights gutter 92 + toggle + title) runs out of room. The sidebar
// auto-closes below 928 = 640 + 288 (sidebar) in the renderer
// (components/sidebar.tsx, SIDEBAR_AUTOCLOSE); its "lh:expand-window"
// IPC widens the window back to that threshold.
minWidth: 640,
minHeight: 600,
});
const allowedOrigin = () => new URL(url).origin;
// Deny-by-default; the ONLY grants are media (mic/STT groundwork) and
// clipboard-sanitized-write — the copy affordances in the transcript
// (md-blocks copy corners) call navigator.clipboard.writeText(), which
// Chromium only honors with that permission (chrome-melt tweaks wave,
// 2026-09-19; before this grant every copy silently rejected). Still
// loopback-origin only — Electron's default is auto-approve-everything,
// and we do not ship that.
// "local-fonts": the appearance section's reading-font picker (2026-09-26).
const allowedPermissions = new Set(["media", "clipboard-sanitized-write", "local-fonts"]);
const fromOwnOrigin = (origin, permission) =>
origin === allowedOrigin() && allowedPermissions.has(permission);
session.defaultSession.setPermissionRequestHandler((_wc, permission, callback, details) => {
const requestOrigin = details.requestingUrl ? new URL(details.requestingUrl).origin : null;
callback(fromOwnOrigin(requestOrigin, permission));
});
session.defaultSession.setPermissionCheckHandler((_wc, permission, origin) => {
return fromOwnOrigin(origin, permission);
});
win.webContents.on("will-navigate", (event, target) => {
if (new URL(target).origin !== allowedOrigin()) {
event.preventDefault();
void shell.openExternal(target);
}
});
win.webContents.setWindowOpenHandler(({ url: target }) => {
void shell.openExternal(target);
return { action: "deny" };
});
void win.loadURL(url);
win.once("ready-to-show", () => win.show());
return win;
}
/** macOS needs an application menu with the Edit roles — without one,
* Cmd+C/V/A do nothing in the renderer. Everything else stays minimal. */
function installMenu() {
const template = [
{
label: app.name,
submenu: [
{ role: "about" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ type: "separator" },
{ role: "quit" },
],
},
{ label: "Edit", submenu: [{ role: "undo" }, { role: "redo" }, { type: "separator" }, { role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectAll" }] },
{
label: "View",
submenu: [{ role: "reload" }, { role: "toggleDevTools" }, { type: "separator" }, { role: "resetZoom" }, { role: "zoomIn" }, { role: "zoomOut" }, { type: "separator" }, { role: "togglefullscreen" }],
},
{ role: "windowMenu" },
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}
async function main() {
app.on("window-all-closed", () => {
// macOS convention: the app stays resident, reactivated from the Dock.
if (process.platform !== "darwin") app.quit();
});
// The child must not outlive us — cover every quit path.
app.on("before-quit", killServer);
process.on("exit", killServer);
process.on("SIGHUP", killServer);
await app.whenReady();
// Dev mode: point at a running `next dev` server (LH_DEV_URL) — no
// spawn, no packaging, full HMR. The rest of this shell still applies
// (guards, menu, single instance).
const devUrl = process.env.LH_DEV_URL;
const url = devUrl ?? `http://127.0.0.1:${await freePort()}`;
if (!devUrl) {
log(`starting server: ${serverEntry()}`);
log(`data dir: ${DATA_DIR()}${fs.existsSync(AUTH_FILE()) ? " · auth.json: present" : " · auth.json: ABSENT (no BYOK keys yet)"}`);
await startServer(new URL(url).port);
await waitUntilUp(url);
log(`server up: ${url}`);
} else {
log(`dev mode: ${devUrl}`);
}
installMenu();
// Sidebar-at-narrow-window (owner, 2026-09-17): the renderer auto-closes
// the sidebar below SIDEBAR_AUTOCLOSE (928px); opening it again at that
// width asks the window to widen instead — the layout never has to
// accommodate a narrow window + open sidebar. Top-left stays fixed, the
// window grows right. No-op when already wide.
ipcMain.on("lh:expand-window", (event) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (!win) return;
const { x, y, width, height } = win.getBounds();
const target = SIDEBAR_AUTOCLOSE;
if (width < target) win.setBounds({ x, y, width: target, height });
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow(url);
});
createWindow(url);
log(`window open: ${url}`);
}