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
address copilot review (round 2): guard migrate flip + FS-safe LM Studio paths
Adam Eivy090d89f66d86
diff --git a/server/lib/localLlmDisk.js b/server/lib/localLlmDisk.jsindex 1310efab82..29a325339e 100644--- a/server/lib/localLlmDisk.js+++ b/server/lib/localLlmDisk.js@@ -117,14 +117,21 @@ export function isShardedGguf(basename) {return isShardName(String(basename || ''));}+// Make one id segment safe to use as a directory name on every platform —+// anything outside [A-Za-z0-9._-] (notably `:` from Ollama tags like+// `model:latest`, and `\` on Windows) collapses to `-`.+const fsSafeSegment = (s) => String(s || '').replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '') || 'model';+/**- * Split an LM Studio model id into its on-disk `<publisher>/<repo>` parts.+ * Split an LM Studio model id into its on-disk `<publisher>/<repo>` parts,+ * sanitized into filesystem-safe path components (the id may be migration-+ * generated, e.g. `imported/llama3.2:latest`, not just a clean LM Studio id).* Bare ids (no `/`) land under an `imported` publisher.*/export function lmStudioPublisherRepo(lmstudioId) {const parts = String(lmstudioId || '').split('/').filter(Boolean);- if (parts.length >= 2) return { publisher: parts[0], repo: parts.slice(1).join('-') };- return { publisher: 'imported', repo: parts[0] || 'model' };+ if (parts.length >= 2) return { publisher: fsSafeSegment(parts[0]), repo: fsSafeSegment(parts.slice(1).join('-')) };+ return { publisher: 'imported', repo: fsSafeSegment(parts[0] || 'model') };}/** A `FROM <gguf>` Modelfile body for importing a local GGUF into Ollama. */diff --git a/server/lib/localLlmDisk.test.js b/server/lib/localLlmDisk.test.jsindex 0232ccd0ca..252643fca9 100644--- a/server/lib/localLlmDisk.test.js+++ b/server/lib/localLlmDisk.test.js@@ -98,6 +98,11 @@ describe('localLlmDisk', () => {.toEqual({ publisher: 'lmstudio-community', repo: 'Llama-3.2-3B-Instruct-GGUF' });expect(lmStudioPublisherRepo('justaname')).toEqual({ publisher: 'imported', repo: 'justaname' });});+ it('sanitizes path-unsafe chars (e.g. Ollama tag colons) into filesystem-safe segments', () => {+ // a migration-generated id like `imported/llama3.2:latest` must not put `:` in a dir name+ expect(lmStudioPublisherRepo('imported/llama3.2:latest')).toEqual({ publisher: 'imported', repo: 'llama3.2-latest' });+ expect(lmStudioPublisherRepo('gpt-oss:20b')).toEqual({ publisher: 'imported', repo: 'gpt-oss-20b' });+ });});describe('buildModelfile', () => {diff --git a/server/services/localLlm.js b/server/services/localLlm.jsindex 5282f2ad5b..e9bff6ca8d 100644--- a/server/services/localLlm.js+++ b/server/services/localLlm.js@@ -417,14 +417,27 @@ export async function migrateBackend(to, onProgress = () => {}) {results.push({ source: model.id, target: targetId, status, reason: r.error })}- writeBackend(to)- await ensureBackendProvider(to)-const imported = results.filter((r) => r.status === 'imported').lengthconst installed = results.filter((r) => r.status === 'installed').lengthconst started = results.filter((r) => r.status === 'started').lengthconst failed = results.filter((r) => r.status === 'failed').lengthconst skipped = results.length - imported - installed - started - failed+ const succeeded = imported + installed + started++ // Don't flip the active marker onto a backend we couldn't provision anything+ // on — if every attempt failed (target not installed/running), switching would+ // leave PortOS pointed at an unusable backend. All-skipped is fine (the target+ // itself works; we just had no equivalent to move).+ if (failed > 0 && succeeded === 0) {+ const error = `Migration to ${to} failed — no models could be provisioned (is ${to} installed and running?). Active backend left unchanged.`+ onProgress({ event: 'error', message: error })+ console.error(`⚠️ Migration to ${to} aborted: ${failed} failed, 0 succeeded — marker unchanged`)+ return { success: false, error, results }+ }++ writeBackend(to)+ await ensureBackendProvider(to)+const parts = [imported ? `${imported} copied locally` : null,installed ? `${installed} downloaded` : null,diff --git a/server/services/localLlm.test.js b/server/services/localLlm.test.jsindex 7ab2c89a2e..89616ee7a2 100644--- a/server/services/localLlm.test.js+++ b/server/services/localLlm.test.js@@ -174,6 +174,21 @@ describe('localLlm', () => {expect(r.results[0].status).toBe('installed');});+ it('does not flip the marker when every provision fails (target unusable)', async () => {+ writeEnv('LLM_BACKEND=lmstudio\n');+ mocks.lmstudio.getAvailableModels.mockResolvedValueOnce([+ { id: 'lmstudio-community/Llama-3.2-3B-Instruct-GGUF' }+ ]);+ mocks.lmstudio.resolveLocalModel.mockResolvedValueOnce(null); // no local copy → re-pull+ mocks.ollama.pullModel.mockResolvedValueOnce({ success: false, error: 'Ollama not available' });++ const r = await svc.migrateBackend('ollama');+ expect(r.success).toBe(false);+ expect(r.error).toMatch(/no models could be provisioned/);+ expect(svc.getBackend()).toBe('lmstudio'); // marker left unchanged+ expect(mocks.providers.updateProvider).not.toHaveBeenCalled(); // provider not flipped either+ });+it('rejects migrating to the already-active backend (no-op)', async () => {writeEnv('LLM_BACKEND=ollama\n');const r = await svc.migrateBackend('ollama');