Skip to content

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

fix: refuse symlinked model-store subdirectories and match xet chunk_cache (#10134)

Adam Eivy80ea3d1856d0
diff --git a/server/routes/imageVideoModels.store.test.js b/server/routes/imageVideoModels.store.test.js
index e6d1af391e..e0aac1bb79 100644
--- a/server/routes/imageVideoModels.store.test.js
+++ b/server/routes/imageVideoModels.store.test.js
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import express from 'express';
-import { mkdir, mkdtemp, rm, stat, utimes, writeFile } from 'fs/promises';
+import { mkdir, mkdtemp, rm, stat, symlink, utimes, writeFile } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { errorMiddleware } from '../lib/errorHandler.js';
@@ -80,8 +80,20 @@ describe('file-system model stores', () => {
expect(await exists(join(bank, 'a'))).toBe(false);
});
+ // Regression: a symlinked session-bank must not turn "clear the cache" into
+ // "delete the children of wherever the link points".
+ it('ignores a session-bank symlink instead of clearing its target', async () => {
+ const outside = join(root, 'outside');
+ await fill(join(outside, 'precious.bin'), 10);
+ await mkdir(join(root, 'mtplx'), { recursive: true });
+ await symlink(outside, join(root, 'mtplx', 'session-bank'));
+ expect((await listModelStore('mtplx')).items).toEqual([]);
+ expect((await request(app).delete('/api/image-video/models/store/mtplx/session-bank')).status).toBe(404);
+ expect(await exists(join(outside, 'precious.bin'))).toBe(true);
+ });
+
it('clears xet chunk caches and only logs older than 7 days', async () => {
- await fill(join(root, 'xet', 'abc', 'chunk-cache', 'c.bin'), 100);
+ await fill(join(root, 'xet', 'abc', 'chunk_cache', 'c.bin'), 100);
await fill(join(root, 'xet', 'logs', 'old.log'), 40);
await fill(join(root, 'xet', 'logs', 'new.log'), 30);
const old = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000);
@@ -91,7 +103,7 @@ describe('file-system model stores', () => {
expect(items).toHaveLength(1);
expect(items[0].size).toBeGreaterThanOrEqual(140);
expect((await request(app).delete('/api/image-video/models/store/hf-xet-cache/chunk-cache')).status).toBe(200);
- expect(await exists(join(root, 'xet', 'abc', 'chunk-cache'))).toBe(false);
+ expect(await exists(join(root, 'xet', 'abc', 'chunk_cache'))).toBe(false);
expect(await exists(join(root, 'xet', 'logs', 'old.log'))).toBe(false);
expect(await exists(join(root, 'xet', 'logs', 'new.log'))).toBe(true);
});
diff --git a/server/services/modelStoreStorage.js b/server/services/modelStoreStorage.js
index 7f14c97c81..ee5006af1a 100644
--- a/server/services/modelStoreStorage.js
+++ b/server/services/modelStoreStorage.js
@@ -14,7 +14,7 @@
* instead of reading it as empty and pruning the manifest.
*/
-import { readdir, stat } from 'fs/promises';
+import { lstat, readdir, stat } from 'fs/promises';
import { homedir } from 'os';
import { join, relative, isAbsolute } from 'path';
import { dirSize, rmGuarded } from '../lib/fileUtils.js';
@@ -49,6 +49,15 @@ const subdirs = async (dir) => (await readEntries(dir))
.filter((entry) => entry.isDirectory() && !entry.name.startsWith('.'))
.map((entry) => entry.name);
+/** True only for a real directory — a symlink named like a store subdirectory is not one. */
+const isRealDirectory = (path) => lstat(path).then(
+ (info) => info.isDirectory(),
+ (err) => {
+ if (err?.code === 'ENOENT' || err?.code === 'ENOTDIR') return false;
+ throw err;
+ },
+);
+
const item = ({ key, name, detail, size, root, removePaths, risk, cleanupReason }) => ({
key, name, detail, size, root, removePaths, risk, cleanupReason,
});
@@ -72,7 +81,7 @@ async function scanMtplx() {
}));
}
const bankDir = join(root, SESSION_BANK);
- const bankSize = await dirSize(bankDir, { strict: true });
+ const bankSize = await isRealDirectory(bankDir) ? await dirSize(bankDir, { strict: true }) : 0;
if (bankSize > 0) {
items.push(item({
key: SESSION_BANK,
@@ -105,13 +114,14 @@ async function scanHy3dgen() {
return items;
}
-/** Every `chunk-cache` directory under the xet root, to a bounded depth. */
+/** Every xet chunk-cache directory under the xet root, to a bounded depth. */
async function findChunkCaches(dir, depth = XET_SEARCH_DEPTH) {
if (depth <= 0) return [];
const found = [];
for (const name of await subdirs(dir)) {
const path = join(dir, name);
- if (name === 'chunk-cache') found.push(path);
+ // The Hugging Face xet client writes `chunk_cache`; `chunk-cache` is accepted too.
+ if (name === 'chunk_cache' || name === 'chunk-cache') found.push(path);
else found.push(...await findChunkCaches(path, depth - 1));
}
return found;
@@ -132,7 +142,8 @@ async function scanXetCache({ now = Date.now() } = {}) {
const root = getXetRoot();
const chunkCaches = await findChunkCaches(root);
const chunkSizes = await Promise.all(chunkCaches.map((path) => dirSize(path, { strict: true })));
- const logs = await staleXetLogs(join(root, 'logs'), now);
+ const logsDir = join(root, 'logs');
+ const logs = await isRealDirectory(logsDir) ? await staleXetLogs(logsDir, now) : [];
const size = chunkSizes.reduce((sum, bytes) => sum + bytes, 0) + logs.reduce((sum, log) => sum + log.size, 0);
if (size <= 0) return [];
return [item({