Files
learnharness/config.ts
T
amosblomqvist 7902984bd6 feat: Electron desktop wrapper — LearnHarness.app (macOS arm64)
The web app becomes a desktop app without changing shape: the Electron
shell spawns the Next standalone server on a loopback port and points a
sandboxed window at it. All app logic stays in the Next server.

Bands:

1. LH_DATA_DIR seam (config.ts): PATHS.data/chats/graph derive from
   process.env.LH_DATA_DIR (else repo data/, unchanged in dev). New
   PATHS.modelCache routes the transformers.js embedding-model cache
   (~300MB) to <data>/models when the seam is set — the default cache
   inside node_modules is read-only in a packaged bundle. All existing
   fs writes flow through PATHS; teacherPrompt stays ROOT-anchored
   (read-only asset, ships inside the bundle by design).
   tests/data-dir-seam.test.ts covers set/unset via child process.

2. Standalone build (next.config.ts + scripts/build-server.mjs):
   output 'standalone'; outputFileTracingExcludes data/**/* keeps the
   repo's real session data out of the bundle (verified: an unexcluded
   scratch build traced 34 files / 716K of real chats + ledgers).
   Two empirically-verified tracer gaps required outputFileTracingIncludes:
   pi-ai's provider catalog (JSON import attributes — 39 files, 500s on
   /api/models without them) and onnxruntime-node's sibling dylib
   (libonnxruntime.dlopen via @rpath). build-server.mjs copies public/
   and .next/static per docs, refuses a leaked data/ dir, and asserts
   the runtime-read prompt .md assets are traced.

3. Electron shell (electron/main.cjs, no preload):
   - single-instance lock (two instances would fight over the data dir);
   - free port via listen(0); spawn via process.execPath +
     ELECTRON_RUN_AS_NODE=1 with PORT/HOSTNAME=127.0.0.1/LH_DATA_DIR;
   - BYOK: userData/auth.json, flat {VAR: value} JSON, mode 0600,
     merged into the child env only when absent from the real env
     (auth UI is the settings milestone; file is hand-editable);
   - poll until the server answers (no ready event), then open a
     contextIsolation+sandbox window; deny-by-default permission
     handler (media allowed only from the loopback origin — mic/STT
     groundwork); will-navigate pinned to origin, window.open denied
     with external links to the system browser; macOS Edit-menu roles;
   - quit path SIGTERM→3s→SIGKILL + exit/SIGHUP nets; child stdout
     mirrored to userData/logs/main.log;
   - dev mode via LH_DEV_URL: no spawn, points at next dev (HMR).

4. Packaging (electron-builder.yml + build/entitlements.mac.plist):
   files = electron shell only; the whole .next payload ships via
   extraResources from .next (filter standalone/**/*) — NOT from
   .next/standalone directly: electron-builder hard-excludes a copy's
   root node_modules and the default pattern is a literal match, so
   node_modules/ and .next/ were silently dropped and the server died
   with 'Cannot find module next'. Asar holds no native code.
   Unsigned (identity: null) until a Developer ID cert exists; hardened
   runtime + audio-input entitlements laid now for the STT milestone.
   desktop:dev / desktop:dist scripts (electron 44, electron-builder 26).

Live smoke of the packaged .app: server up on a random loopback port,
all API routes 200, a real GLM chat turn answered end-to-end via
auth.json BYOK, chat persisted under userData/data, second instance
exited via the lock, no orphaned server child after quit.

Gates: typecheck, eslint, 500/500 tests (+2 data-dir-seam), build.
2026-09-17 15:32:28 +03:00

135 lines
6.6 KiB
TypeScript

import path from "node:path";
import { fileURLToPath } from "node:url";
/** Repository root (this file is at <root>/config.ts). */
export const ROOT = path.dirname(fileURLToPath(import.meta.url));
/**
* THE data-location seam (the Electron contract, 2026-09-17): LH_DATA_DIR
* relocates every MUTABLE record — chat sessions, graph ledgers, the
* embedding model cache — when set. The desktop shell points it at
* userData so nothing is written inside the read-only .app bundle
* (macOS translocation guarantees the bundle is read-only anyway).
* Unset (plain web dev / repo work) it resolves to the repo's data/,
* byte-for-byte as before. Read-only assets (prompts) stay ROOT-anchored
* on purpose: they ship inside the app bundle by design.
*/
const DATA_ROOT = process.env.LH_DATA_DIR
? path.resolve(process.env.LH_DATA_DIR)
: path.join(ROOT, "data");
export const PATHS = {
root: ROOT,
/** Teacher system prompt — the agent's instructions (src/agent/). */
teacherPrompt: path.join(ROOT, "src", "agent", "system-prompt.md"),
data: DATA_ROOT,
/** Chat sessions (AI SDK UIMessage transcripts, one JSON file per chat). */
chats: path.join(DATA_ROOT, "chats"),
graph: path.join(DATA_ROOT, "graph"),
/** transformers.js model cache (server-side embeddings, ~300MB).
* null in dev = keep the package default (the model is already
* downloaded there); under LH_DATA_DIR the packaged app must never
* try to cache inside its own bundle. */
modelCache: process.env.LH_DATA_DIR ? path.join(DATA_ROOT, "models") : null,
} as const;
// ─── Models ────────────────────────────────────────────────────────────────
// THE single place every LLM is chosen and tuned. .env stays for secrets
// (API keys) only — model choice and thinking budget are product tuning,
// not deployment environment, and this section is the one place they grow.
// `effort` is pi's thinking-level scale (off|minimal|low|medium|high|xhigh
// |max): the per-model truth lives in pi-ai's catalog (thinkingLevelMap) —
// getSupportedThinkingLevels(model) lists what a model actually accepts,
// and streamSimple clamps any other value to the nearest supported one
// (e.g. GLM-5.3-flash supports exactly low|high|max).
export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
export interface ModelSpec {
/** OpenRouter model id (e.g. "z-ai/glm-5.3-flash") — must exist in pi-ai's catalog. */
model: string;
/** Thinking budget — what the model may spend on reasoning tokens. */
effort: ThinkingLevel;
}
export const MODELS = {
/** The prober (agent A) — edge-bracketing questions; keep it lively and adaptive. */
prober: { model: "z-ai/glm-5.3-flash", effort: "low" },
/** The teacher (agent B) — planning + streaming, lesson-facing: smart, but keep it lively. */
teacher: { model: "z-ai/glm-5.3-flash", effort: "low" },
/** Background sub-agents: fast and cheap beats deep thought. */
researcher: { model: "z-ai/glm-5.3-flash", effort: "low" },
demoBuilder: { model: "z-ai/glm-5.3-flash", effort: "low" },
/** Must be vision-capable: it looks at rendered SVGs. */
visualMaker: { model: "qwen/qwen3.8-flash", effort: "low" },
/** Extraction is judgment over given evidence — light thinking suffices. */
extractor: { model: "z-ai/glm-5.3-flash", effort: "low" },
/** The auditor verifies evidence exists; keep it cheap. */
auditor: { model: "z-ai/glm-5.3-flash", effort: "low" },
/** Names detected communities — one singular call, given member names
* + statements. Purely a labeling flourish; keep it cheap. */
labeler: { model: "z-ai/glm-5.3-flash", effort: "low" },
} satisfies Record<string, ModelSpec>;
export type ModelRole = keyof typeof MODELS;
/**
* The extraction pipeline (vision §1 construction): the sole graph writer.
* Triggered per chat once this many NEW quiz answers have landed since the
* chat's last extraction checkpoint. The trigger mechanism is a v1
* placeholder — deliberately isolated behind one seam
* (server/turn-service.ts) so it can be swapped without touching the
* pipeline.
*/
/**
* The extraction trigger (vision-extraction-coverage.md, 2026-09-15): a run
* fires when a chat's UNCOVERED transcript tokens reach the chunk budget.
* Runs are token-budgeted batches of the oldest uncovered messages — this
* knob is the transcript share of the extractor's context (it also carries
* the whole graph + instructions), so it bounds both cost and chunk size.
* Replaces the old every-3-quiz-answers trigger, which never fired for
* quiz-less sessions.
*/
export const EXTRACTION = { chunkTokens: 4096 } as const;
/**
* The LIVE graph store (src/store/graph.ts) — owned by the app's extraction
* pipeline. Line shapes: src/core/contracts.ts.
*/
export const LEDGER_FILES = {
nodes: path.join(PATHS.graph, "nodes.jsonl"),
edges: path.join(PATHS.graph, "edges.jsonl"),
events: path.join(PATHS.graph, "events.jsonl"),
mappings: path.join(PATHS.graph, "mappings.jsonl"),
sessions: path.join(PATHS.graph, "sessions.jsonl"),
/** Per-message extraction coverage (contracts/graph.ts CoverageEntry;
* the checkpoint's replacement for chat extractions — notes keep
* sessions.jsonl, whole-file as they are). */
coverage: path.join(PATHS.graph, "coverage.jsonl"),
/** LLM-named community records (contracts/graph.ts ClusterLabeling). */
clusterLabels: path.join(PATHS.graph, "cluster-labels.jsonl"),
} as const;
/**
* Notes-import bookkeeping (NOT a ledger): one JSON map, src-id → NotesSource
* record (contracts/graph.ts) — the file path + import time + the run's usage
* log. Read-modify-write like a chat file, not append-only like the ledgers.
*/
export const SOURCES_FILE = path.join(PATHS.graph, "sources.json");
/** The layout ledger (read-modify-write like sources.json): nodeId →
* {x,y} resting positions from the incremental settle (core/layout.ts).
* Carries an updatedAt stamp for future live-physics conflict handling. */
export const POSITIONS_FILE = path.join(PATHS.graph, "positions.json");
/** The embedding ledger: one L2-normalized vector per node id, all from
* ONE model (swapping models re-embeds everything — vectors from
* different models live in incompatible spaces). Read-modify-write
* JSON like positions/sources. */
export const EMBEDDINGS_FILE = path.join(PATHS.graph, "embeddings.json");
/**
* Graph render settings (the settings-panel controls): one small JSON blob,
* read-modify-write. Shared shape/sanitizer: src/core/render-settings.ts.
*/
export const RENDER_SETTINGS_FILE = path.join(PATHS.graph, "render-settings.json");