Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

stage-harness: flatten pnpm's links into one real folder per package (the installer copies files)

Niko1221b74d6ad3d300
diff --git a/apps/desktop/scripts/flatten-pnpm.mjs b/apps/desktop/scripts/flatten-pnpm.mjs
new file mode 100644
index 000000000..403d4df27
--- /dev/null
+++ b/apps/desktop/scripts/flatten-pnpm.mjs
@@ -0,0 +1,133 @@
+/**
+ * Turn a pnpm "isolated" node_modules (a virtual store in `.pnpm` full of links) into an ordinary hoisted tree of real folders.
+ *
+ * Why: the installer copies the Harness folder file by file. pnpm's layout is thousands of links (junctions on Windows); a copy
+ * that follows them duplicates every package once per dependent and one that does not breaks on the first dangling link. A hoisted
+ * tree has each package once (nested only where two versions of the same name are needed), no links, and Node resolves it
+ * exactly as it resolves any npm install.
+ *
+ * Algorithm (the one npm uses, in short): the packages the root names come first, at the top. Then, walking each placed package's
+ * own dependencies: look the name up from the package's own folder upwards; if the same instance is already visible, nothing to
+ * do; if a DIFFERENT instance of that name is visible, nest this one inside the dependent; if none is visible, hoist it to the top.
+ *
+ * @param {string} out folder holding `node_modules`
+ * @param {{skip?: RegExp}} [options] package names never copied (the caller removes them anyway)
+ * @returns {{packages:number, copied:number, nested:number}}
+ */
+import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync } from 'node:fs';
+import { dirname, join, relative, sep } from 'node:path';
+
+export function flattenPnpm(out, { skip = null } = {}) {
+ const nm = join(out, 'node_modules');
+ const store = join(nm, '.pnpm');
+ const flatRoot = join(out, 'node_modules.flat');
+ rmSync(flatRoot, { recursive: true, force: true });
+ mkdirSync(join(flatRoot, 'node_modules'), { recursive: true });
+
+ const manifest = dir => { try { return JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')); } catch { return null; } };
+ const nameOf = dir => manifest(dir)?.name ?? null;
+ /**
+ * What makes two instances "the same package": name and version. pnpm keeps one copy per set of peer packages (the same workspace
+ * package under many hashed folders); in one hoisted tree they are one package.
+ */
+ const identity = dir => { const m = manifest(dir); return m ? `${m.name}@${m.version}` : dir; };
+ /** The sibling links of a store instance: its dependencies, by name -> real instance folder. */
+ function storeDeps(instance, name) {
+ const container = name.startsWith('@') ? dirname(dirname(instance)) : dirname(instance);
+ const found = new Map();
+ for (const e of readdirSync(container, { withFileTypes: true })) {
+ if (e.name === '.bin') continue;
+ const entries = e.name.startsWith('@') ? readdirSync(join(container, e.name), { withFileTypes: true }).map(x => [`${e.name}/${x.name}`, join(container, e.name, x.name)]) : [[e.name, join(container, e.name)]];
+ for (const [dep, p] of entries) {
+ if (dep === name) continue;
+ try { if (lstatSync(p).isSymbolicLink() || existsSync(p)) found.set(dep, realpathSync(p)); } catch { /* a dangling link: the package was removed on purpose */ }
+ }
+ }
+ return found;
+ }
+ /** Every instance in the store by package name, for packages that arrived without a store entry (copied peers). */
+ const byName = new Map();
+ if (existsSync(store)) {
+ for (const id of readdirSync(store, { withFileTypes: true })) {
+ if (!id.isDirectory()) continue;
+ const container = join(store, id.name, 'node_modules');
+ if (!existsSync(container)) continue;
+ for (const e of readdirSync(container, { withFileTypes: true })) {
+ if (e.name === '.bin') continue;
+ const dirs = e.name.startsWith('@') ? readdirSync(join(container, e.name)).map(x => join(container, e.name, x)) : [join(container, e.name)];
+ for (const d of dirs) {
+ let real; try { real = realpathSync(d); } catch { continue; }
+ if (!real.startsWith(store)) continue; // a link to a workspace package outside the store is handled through the instance table
+ const n = nameOf(real);
+ if (n && !byName.has(n)) byName.set(n, real);
+ }
+ }
+ }
+ }
+
+ const placed = new Map(); // folder (in the flat tree) -> real instance folder it holds
+ let copied = 0, nested = 0;
+
+ const visibleFrom = (chain, name) => {
+ for (const dir of chain) {
+ const slot = join(dir, 'node_modules', ...name.split('/'));
+ if (placed.has(slot)) return { slot, instance: placed.get(slot) };
+ }
+ return null;
+ };
+ const copyInto = (slot, instance) => {
+ mkdirSync(dirname(slot), { recursive: true });
+ cpSync(instance, slot, { recursive: true, dereference: true, filter: f => !relative(instance, f).split(sep).includes('node_modules') });
+ placed.set(slot, identity(instance));
+ copied++;
+ };
+
+ const queue = []; // { slotDir (folder of the placed package), chain (folders searched upwards), instance, name }
+ const rootChain = [flatRoot];
+ const place = (name, instance, chain, forceHere) => {
+ if (skip?.test(name)) return;
+ const seen = visibleFrom(chain, name);
+ if (seen && seen.instance === identity(instance)) return;
+ const hoist = !seen && !forceHere;
+ const base = hoist ? flatRoot : chain[0];
+ const slot = join(base, 'node_modules', ...name.split('/'));
+ if (placed.has(slot)) return; // another instance already sits where this one would go (it nests below, handled by the caller's chain)
+ copyInto(slot, instance);
+ if (!hoist && base !== flatRoot) nested++;
+ queue.push({ slotDir: slot, chain: [slot, ...(hoist ? rootChain : chain)], instance, name });
+ };
+
+ // 1. the packages the root names
+ const rootEntries = [];
+ for (const e of readdirSync(nm, { withFileTypes: true })) {
+ if (e.name === '.pnpm' || e.name === '.bin' || e.name.startsWith('.')) continue;
+ const entries = e.name.startsWith('@') ? readdirSync(join(nm, e.name)).map(x => [`${e.name}/${x}`, join(nm, e.name, x)]) : [[e.name, join(nm, e.name)]];
+ for (const [name, p] of entries) rootEntries.push([name, p]);
+ }
+ for (const [name, p] of rootEntries) {
+ let real; try { real = realpathSync(p); } catch { continue; }
+ place(name, real, rootChain, true);
+ }
+ // 2. their dependencies, breadth first
+ while (queue.length) {
+ const { slotDir, chain, instance, name } = queue.shift();
+ let wants;
+ if (instance.startsWith(store)) wants = storeDeps(instance, name);
+ else {
+ // a package copied in without a store entry: its declared requirements, taken from the store by name when not visible yet
+ const m = manifest(instance) ?? {};
+ wants = new Map();
+ for (const dep of [...Object.keys(m.dependencies ?? {}), ...Object.keys(m.peerDependencies ?? {}), ...Object.keys(m.optionalDependencies ?? {})]) {
+ if (visibleFrom(chain, dep)) continue;
+ const real = byName.get(dep);
+ if (real) wants.set(dep, real);
+ }
+ }
+ for (const [dep, real] of wants) place(dep, real, chain, false);
+ }
+
+ rmSync(nm, { recursive: true, force: true });
+ renameSync(join(flatRoot, 'node_modules'), nm);
+ rmSync(flatRoot, { recursive: true, force: true });
+ return { packages: placed.size, copied, nested };
+}
diff --git a/apps/desktop/scripts/stage-harness.mjs b/apps/desktop/scripts/stage-harness.mjs
index 2d7328553..42f65253a 100644
--- a/apps/desktop/scripts/stage-harness.mjs
+++ b/apps/desktop/scripts/stage-harness.mjs
@@ -17,6 +17,7 @@ import { spawnSync } from 'node:child_process';
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { join, relative, resolve, sep } from 'node:path';
import { createHash } from 'node:crypto';
+import { flattenPnpm } from './flatten-pnpm.mjs';
const args = process.argv.slice(2);
const opt = (name, fallback) => { const i = args.indexOf(`--${name}`); return i >= 0 ? args[i + 1] : fallback; };
@@ -32,6 +33,8 @@ const run = (cmd, argv, cwd, extra = {}) => {
if (r.status !== 0) { console.error(`${cmd} ${argv.join(' ')} failed (${r.status ?? r.error})`); process.exit(1); }
};
+// The output folder in the repo carries a .gitignore that keeps the staged files out of git: keep it across the clean-up.
+const keepIgnore = existsSync(join(out, '.gitignore')) ? readFileSync(join(out, '.gitignore')) : null;
rmSync(out, { recursive: true, force: true });
mkdirSync(out, { recursive: true });
const pnpm = platform === 'win32' ? ['corepack.cmd', ['pnpm']] : ['corepack', ['pnpm']];
@@ -83,6 +86,11 @@ while (queue.length) {
}
if (added.length) console.log(`peer packages added: ${added.join(', ')}`);
+// --- one real folder per package, no links -------------------------------------------------------------------------------
+// The installer copies files; pnpm's layout is links. See flatten-pnpm.mjs. The two packages the app does not use are never copied.
+const flat = flattenPnpm(out, { skip: /libreoffice-kit|sherpa-onnx/i });
+console.log(`flattened: ${flat.packages} placed (${flat.nested} nested for a second version)`);
+
// --- prune ------------------------------------------------------------------------------------------------------------
const OTHER_OS = platform === 'win32' ? /(^|[\\/._-])(darwin|linux|android|freebsd|musl)([\\/._-]|$)/i
: platform === 'darwin' ? /(^|[\\/._-])(win32|linux|android|freebsd|musl)([\\/._-]|$)/i
@@ -127,6 +135,8 @@ writeFileSync(join(out, 'BUNDLE.json'), JSON.stringify({
note: 'Pruned production install (pnpm deploy --prod). No package manager runs on the user\'s PC.',
}, null, 2));
+if (keepIgnore) writeFileSync(join(out, '.gitignore'), keepIgnore);
+
// --- proof: the staged copy answers --version and boots the web profile once ----------------------------------------
const bin = join(out, 'apps', 'cli', 'lib', 'bin.js');
const entry = existsSync(bin) ? bin : join(out, 'lib', 'bin.js');