Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: preserve exact model file identities on Windows (#9887)
Adam Eivyee773535b861
diff --git a/server/services/modelDeduplication.js b/server/services/modelDeduplication.jsindex 96c32fbaef..717d33658d 100644--- a/server/services/modelDeduplication.js+++ b/server/services/modelDeduplication.js@@ -21,9 +21,11 @@ const missing = (error) => {throw error;};const invalid = (message) => new ServerError(message, { status: 400, code: 'VALIDATION_ERROR' });+// File identifiers can exceed Number's exact integer range on Windows.+// Keep stats exact internally; convert only user-facing sizes to numbers.const identity = (info) => `${info.dev}:${info.ino}`;const unchanged = (a, b) => identity(a) === identity(b) && a.size === b.size- && a.mtimeMs === b.mtimeMs && a.ctimeMs === b.ctimeMs;+ && a.mtimeNs === b.mtimeNs && a.ctimeNs === b.ctimeNs;async function directories(root) {return (await fs.readdir(root, { withFileTypes: true }).catch(missing) || [])@@ -32,7 +34,7 @@ async function directories(root) {async function rootsForInstall() {const pinokio = resolve(process.env.PINOKIO_HOME || process.env.PINOKIO_PATH || join(homedir(), 'pinokio'));- if (!(await fs.stat(pinokio).catch(missing))?.isDirectory()) return null;+ if (!(await fs.stat(pinokio, { bigint: true }).catch(missing))?.isDirectory()) return null;const peers = await directories(join(pinokio, 'drive', 'drives', 'peers'));const apps = await directories(join(pinokio, 'api'));const external = [@@ -66,7 +68,7 @@ async function inventory(roots) {if (++count > 50000 || depth > 16) throw invalid('Model scan limit reached; narrow the model roots and retry.');const real = await fs.realpath(path).catch(missing);if (!real || !roots.some((root) => real === root || isPathInsideDir(root, real))) return;- const info = await fs.stat(real).catch(missing);+ const info = await fs.stat(real, { bigint: true }).catch(missing);if (!info) return;if (info.isDirectory()) {if (visited.has(real)) return;@@ -106,11 +108,11 @@ export async function scanModelDuplicates({ roots } = {}) {if (!alreadyLinked && await hash(source) !== await hash(target)) continue;// A target with other hardlink names cannot release blocks by replacing// this single name. Leave those aliases untouched and report zero savings.- const reclaimableBytes = alreadyLinked || target.info.nlink > 1 ? 0 : target.info.blocks * 512;+ const reclaimableBytes = alreadyLinked || target.info.nlink > 1n ? 0 : Number(target.info.blocks) * 512;items.push({sourcePath: source.path, targetPath: target.path, model: target.model,- sizeBytes: target.info.size, reclaimableBytes, alreadyLinked,- canLink: !alreadyLinked && target.info.nlink === 1 && source.info.dev === target.info.dev+ sizeBytes: Number(target.info.size), reclaimableBytes, alreadyLinked,+ canLink: !alreadyLinked && target.info.nlink === 1n && source.info.dev === target.info.dev&& source.info.mode === target.info.mode && source.info.uid === target.info.uid && source.info.gid === target.info.gid,});seen.add(target.real);@@ -126,7 +128,7 @@ async function validatedFile(path, roots) {}const real = await fs.realpath(path).catch(missing);if (!real || !roots.some((root) => isPathInsideDir(root, real))) throw invalid('Unsafe or missing model link.');- const info = await fs.stat(real);+ const info = await fs.stat(real, { bigint: true });if (!info.isFile() || info.size < MIN_BYTES) throw invalid('Expected a model weight file of at least 10 MB.');return { path, real, info };}@@ -151,7 +153,7 @@ async function performRectification(pairs, roots) {if (targets.has(target.real)) continue;targets.add(target.real);if (identity(source.info) === identity(target.info)) continue;- if (target.info.nlink !== 1) throw invalid('Target has additional hardlinks; no space can be reclaimed safely.');+ if (target.info.nlink !== 1n) throw invalid('Target has additional hardlinks; no space can be reclaimed safely.');if (source.info.mode !== target.info.mode || source.info.uid !== target.info.uid || source.info.gid !== target.info.gid) {throw invalid('File permissions or ownership differ; linking would change application access.');}@@ -165,8 +167,8 @@ async function performRectification(pairs, roots) {if (prepared.some(({ source }) => targets.has(source.real))) throw invalid('Overlapping source and target paths.');const sourceStates = new Map();for (const { source, target } of prepared) {- const sourceNow = await fs.stat(source.real);- const targetNow = await fs.stat(target.real);+ const sourceNow = await fs.stat(source.real, { bigint: true });+ const targetNow = await fs.stat(target.real, { bigint: true });if (!unchanged(sourceStates.get(source.real) || source.info, sourceNow) || !unchanged(target.info, targetNow)|| await fs.realpath(source.path) !== source.real || await fs.realpath(target.path) !== target.real) {throw invalid('Model changed since verification; rescan before linking.');@@ -177,8 +179,8 @@ async function performRectification(pairs, roots) {await fs.unlink(temporary);throw error;});- sourceStates.set(source.real, await fs.stat(source.real));- results.push({ ...pairs.find((pair) => pair.targetPath === target.path), reclaimedBytes: target.info.blocks * 512 });+ sourceStates.set(source.real, await fs.stat(source.real, { bigint: true }));+ results.push({ ...pairs.find((pair) => pair.targetPath === target.path), reclaimedBytes: Number(target.info.blocks) * 512 });}return { success: true, reclaimedBytes: results.reduce((sum, result) => sum + result.reclaimedBytes, 0), results };}diff --git a/server/services/modelDeduplication.test.js b/server/services/modelDeduplication.test.jsindex a2305ac3e4..77c147a263 100644--- a/server/services/modelDeduplication.test.js+++ b/server/services/modelDeduplication.test.js@@ -37,6 +37,7 @@ it('verifies contents, preserves HF snapshot links and links multiple copies toconst wrongPath = await weight(join(roots.external[0], 'wrong'), 'example.safetensors', Buffer.alloc(bytes.length, 8));const report = await scanModelDuplicates({ roots });expect(report.items).toHaveLength(2);+ expect(report.items[0].sizeBytes).toBe(bytes.length);expect(report.totalReclaimableBytes).toBe((await fs.stat(targetPath)).blocks * 1024);const rename = vi.spyOn(fs, 'rename');const result = await rectifyModelDuplicates(report.items, { roots });@@ -78,7 +79,7 @@ it('does not count target aliases and rejects cross-device replacement', async (const stat = fs.stat.bind(fs);vi.spyOn(fs, 'stat').mockImplementation(async (path, ...args) => {const info = await stat(path, ...args);- if (path === targetPath) info.dev += 1;+ if (path === targetPath) info.dev += 1n;return info;});await expect(rectifyModelDuplicates([{ sourcePath, targetPath }], { roots })).rejects.toThrow('span filesystems');@@ -113,3 +114,26 @@ it('discovers peer drives, app models and the separate HF cache from configuredexpect(report.pinokioDetected).toBe(true);expect(report.items).toHaveLength(3);});++// Windows inode identifiers can exceed Number's exact integer range. Distinct+// files must still reach hash verification and atomic replacement.+it('does not mistake rounded inode identifiers for already-linked files', async () => {+ const sourcePath = await weight(roots.local[0]);+ const targetPath = await weight(roots.external[0]);+ const stat = fs.stat.bind(fs);+ const sourceInode = 2n ** 60n;+ const targetInode = sourceInode + 1n;+ expect(Number(sourceInode)).toBe(Number(targetInode));+ vi.spyOn(fs, 'stat').mockImplementation(async (path, options) => {+ const info = await stat(path, options);+ if (path === sourcePath || path === targetPath) {+ const inode = path === sourcePath ? sourceInode : targetInode;+ info.ino = options?.bigint ? inode : Number(inode);+ }+ return info;+ });+ vi.spyOn(fs, 'rename').mockRejectedValue(new Error('rename failed'));+ await expect(rectifyModelDuplicates([{ sourcePath, targetPath }], { roots })).rejects.toThrow('rename failed');+ expect(await fs.readdir(roots.external[0])).toEqual(['example.safetensors']);+ expect((await fs.readFile(targetPath)).equals(bytes)).toBe(true);+});