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

address review: Copilot round 2

Adam Eivyf08718d2ed5d
diff --git a/server/lib/memoryStats.test.js b/server/lib/memoryStats.test.js
index 993062db72..ef26bd55bb 100644
--- a/server/lib/memoryStats.test.js
+++ b/server/lib/memoryStats.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import os from 'os';
vi.mock('child_process', () => ({
@@ -88,4 +88,51 @@ Pages occupied by compressor: 50.
await getMemoryStats();
expect(exec).toHaveBeenCalledTimes(1);
});
+
+ // Linux path — runs unconditionally by stubbing process.platform so CI
+ // (ubuntu-latest) actually exercises the /proc/meminfo branch instead of
+ // skipping every assertion.
+ describe('linux /proc/meminfo path', () => {
+ let platformSpy;
+ beforeEach(() => {
+ platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux');
+ });
+
+ it('uses MemAvailable when present', async () => {
+ readFile.mockResolvedValue(`MemTotal: 16384000 kB
+MemFree: 1000000 kB
+MemAvailable: 12000000 kB
+Buffers: 500000 kB
+Cached: 3000000 kB
+`);
+ const stats = await getMemoryStats();
+ expect(stats.source).toBe('meminfo');
+ expect(stats.total).toBe(16384000 * 1024);
+ expect(stats.free).toBe(12000000 * 1024);
+ expect(stats.used).toBe((16384000 - 12000000) * 1024);
+ });
+
+ it('falls back to MemFree+Buffers+Cached when MemAvailable missing (older kernels)', async () => {
+ readFile.mockResolvedValue(`MemTotal: 16384000 kB
+MemFree: 1000000 kB
+Buffers: 500000 kB
+Cached: 3000000 kB
+`);
+ const stats = await getMemoryStats();
+ expect(stats.source).toBe('meminfo');
+ expect(stats.free).toBe((1000000 + 500000 + 3000000) * 1024);
+ expect(stats.used).toBe((16384000 - (1000000 + 500000 + 3000000)) * 1024);
+ });
+
+ it('falls back to os.freemem when /proc/meminfo read fails', async () => {
+ readFile.mockRejectedValue(new Error('ENOENT'));
+ const stats = await getMemoryStats();
+ expect(stats.source).toBe('os');
+ expect(stats.total).toBe(os.totalmem());
+ });
+
+ afterEach(() => {
+ platformSpy?.mockRestore();
+ });
+ });
});
diff --git a/server/lib/multipart.js b/server/lib/multipart.js
index ad102fb996..13e3de8f2b 100644
--- a/server/lib/multipart.js
+++ b/server/lib/multipart.js
@@ -24,12 +24,15 @@ export function uploadSingle(fieldName, { limits = {}, fileFilter } = {}) {
return (req, res, next) => {
const ct = req.headers['content-type'] || '';
- if (!ct.startsWith('multipart/form-data')) {
+ // Media type is case-insensitive (RFC 2045), but the boundary value is
+ // case-sensitive — match the type prefix on a lowercased copy and parse
+ // the boundary off the original.
+ if (!ct.toLowerCase().startsWith('multipart/form-data')) {
const err = new Error('Expected multipart/form-data');
err.status = 400; err.code = 'INVALID_CONTENT_TYPE';
return next(err);
}
- const bm = ct.match(/boundary=([^\s;]+)/);
+ const bm = ct.match(/boundary=([^\s;]+)/i);
if (!bm) {
const err = new Error('Missing multipart boundary');
err.status = 400; err.code = 'INVALID_CONTENT_TYPE';
@@ -46,7 +49,7 @@ export function optionalUpload(fieldName, opts) {
const uploader = uploadSingle(fieldName, opts);
return (req, res, next) => {
const ct = req.headers['content-type'] || '';
- if (!ct.startsWith('multipart/form-data')) return next();
+ if (!ct.toLowerCase().startsWith('multipart/form-data')) return next();
return uploader(req, res, next);
};
}
diff --git a/server/routes/imageGen.js b/server/routes/imageGen.js
index 5a6531137e..498b0412dc 100644
--- a/server/routes/imageGen.js
+++ b/server/routes/imageGen.js
@@ -24,7 +24,7 @@ import {
isExternallyManaged, createVenv, isAllowedPython, pipNameFor,
} from '../lib/pythonSetup.js';
import { PATHS, ensureDir } from '../lib/fileUtils.js';
-import { join, basename, resolve as resolvePath, sep as PATH_SEP, extname } from 'node:path';
+import { join, basename, resolve as resolvePath, sep as PATH_SEP } from 'node:path';
const router = Router();
@@ -59,9 +59,15 @@ const generateSchema = z.object({
// JSON callers (SDAPI bridge, avatar route, the Imagine page's old payload
// shape) skip the parser entirely; FormData callers get req.file + string
// req.body that coerceFormFields() converts before Zod validation.
+// Only the formats mflux can decode — keep this in sync with the extension
+// allowlist below so the route never silently relabels (e.g. HEIC) bytes
+// as ".png".
+const ACCEPTED_INIT_IMAGE_MIME = new Set(['image/png', 'image/jpeg', 'image/webp']);
+const MIME_TO_EXT = { 'image/png': '.png', 'image/jpeg': '.jpg', 'image/webp': '.webp' };
+
const initImageUpload = optionalUpload('initImage', {
limits: { fileSize: 20 * 1024 * 1024 },
- fileFilter: (_req, file, cb) => cb(null, file.mimetype.startsWith('image/')),
+ fileFilter: (_req, file, cb) => cb(null, ACCEPTED_INIT_IMAGE_MIME.has((file.mimetype || '').toLowerCase())),
});
// Numerics arrive as strings from FormData — coerce before Zod validation.
@@ -104,8 +110,10 @@ router.post('/generate', initImageUpload, asyncHandler(async (req, res) => {
let uploadedInitTempPath = null;
if (req.file) {
await ensureDir(PATHS.images);
- const safeExt = (extname(req.file.originalname || '') || '.png').toLowerCase();
- const ext = ['.png', '.jpg', '.jpeg', '.webp'].includes(safeExt) ? safeExt : '.png';
+ // Trust the validated mimetype from the fileFilter — picking the ext
+ // off the original filename can mismatch the bytes (e.g. HEIC saved
+ // as .jpg). MIME_TO_EXT only contains formats the fileFilter accepts.
+ const ext = MIME_TO_EXT[(req.file.mimetype || '').toLowerCase()] || '.png';
const initFilename = `init-${randomUUID()}${ext}`;
initImagePath = join(PATHS.images, initFilename);
await copyFile(req.file.path, initImagePath);
diff --git a/server/services/imageGen/local.js b/server/services/imageGen/local.js
index 5482430296..1ee5e2a9a3 100644
--- a/server/services/imageGen/local.js
+++ b/server/services/imageGen/local.js
@@ -72,6 +72,9 @@ export const cancel = () => {
const buildArgs = ({ pythonPath, modelId, prompt, negativePrompt, width, height, steps, guidance, seed, quantize, outputPath, loraPaths, loraScales, stepwiseDir, initImagePath, initImageStrength }) => {
if (IS_WIN) {
+ // imagine_win.py does not implement i2i — silently drop the init-image
+ // args here so the request still produces a normal txt2img result rather
+ // than failing argparse with "unrecognized arguments".
const scriptPath = join(PATHS.root, 'scripts', 'imagine_win.py');
return {
bin: pythonPath,
@@ -80,8 +83,6 @@ const buildArgs = ({ pythonPath, modelId, prompt, negativePrompt, width, height,
...(negativePrompt ? ['--negative-prompt', negativePrompt] : []),
...(loraPaths.length ? ['--lora-paths', ...loraPaths] : []),
...(loraScales.length ? ['--lora-scales', ...loraScales.map(String)] : []),
- ...(initImagePath ? ['--image-path', initImagePath] : []),
- ...(initImagePath && initImageStrength != null ? ['--image-strength', String(initImageStrength)] : []),
],
};
}