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.
52 lines
2.4 KiB
TypeScript
52 lines
2.4 KiB
TypeScript
/**
|
|
* The LH_DATA_DIR seam (Electron contract, 2026-09-17): when set, every
|
|
* mutable record relocates under it; when unset, byte-for-byte the old
|
|
* repo-data/ behavior. The desktop shell's whole data story rests on this,
|
|
* so pin both directions.
|
|
*
|
|
* The seam is evaluated at MODULE LOAD, so the "set" case must set the env
|
|
* BEFORE the first import of config.ts in this process (dynamic import,
|
|
* below) and the "unset" case needs a fresh child process.
|
|
*/
|
|
import assert from "node:assert/strict";
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { execFileSync } from "node:child_process";
|
|
import { describe, it } from "node:test";
|
|
|
|
const REPO_ROOT = path.resolve(import.meta.dirname, "..");
|
|
const TSX_LOADER = path.join(REPO_ROOT, "node_modules", "tsx", "dist", "loader.mjs");
|
|
|
|
describe("LH_DATA_DIR seam", () => {
|
|
it("relocates data/chats/graph + modelCache under LH_DATA_DIR, keeps prompts ROOT-anchored", async () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "lh-seam-"));
|
|
process.env.LH_DATA_DIR = dir;
|
|
try {
|
|
const { PATHS, ROOT } = await import("../config.ts");
|
|
assert.equal(PATHS.data, dir, "PATHS.data resolves the env value (resolved absolute)");
|
|
assert.equal(PATHS.chats, path.join(dir, "chats"));
|
|
assert.equal(PATHS.graph, path.join(dir, "graph"));
|
|
assert.equal(PATHS.modelCache, path.join(dir, "models"), "the HF model cache follows the seam");
|
|
assert.equal(
|
|
PATHS.teacherPrompt,
|
|
path.join(ROOT, "src", "agent", "system-prompt.md"),
|
|
"read-only prompt assets stay bundle-anchored on purpose",
|
|
);
|
|
} finally {
|
|
delete process.env.LH_DATA_DIR;
|
|
}
|
|
});
|
|
|
|
it("unset = the repo data/ exactly as before (fresh child process, no env)", () => {
|
|
const out = execFileSync(
|
|
process.execPath,
|
|
["--import", TSX_LOADER, "--eval", `const m = await import(${JSON.stringify(path.join(REPO_ROOT, "config.ts"))}); console.log(JSON.stringify({ data: m.PATHS.data, chats: m.PATHS.chats, modelCache: m.PATHS.modelCache }));`],
|
|
{ cwd: REPO_ROOT, encoding: "utf-8" },
|
|
);
|
|
const parsed = JSON.parse(out) as { data: string; chats: string | null; modelCache: string | null };
|
|
assert.equal(parsed.data, path.join(REPO_ROOT, "data"));
|
|
assert.equal(parsed.chats, path.join(REPO_ROOT, "data", "chats"));
|
|
assert.equal(parsed.modelCache, null, "dev keeps the transformers package default cache");
|
|
});
|
|
}); |