From cd1ba288bb93fa08b9e1ece417d8607f773866c4 Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Sat, 24 Oct 2026 11:55:54 +0200 Subject: [PATCH] feat(server): turn the LiteLLM proxy off by default (P1.1) Every sidecar start outside the desktop app looked for Python, probed each interpreter for litellm and, when it found one, started a LiteLLM child. The community edition must not need Python, so LiteLLM is now opt-in: startService starts it only when config.json contains "litellm": { "enabled": true } (WaggleConfig.isLiteLLMEnabled). Otherwise there is no Python lookup, no LiteLLM health probe and no child process, and the built-in provider proxy serves every request, as it already did in the desktop app. --skip-litellm and WAGGLE_SKIP_LITELLM=1 still force it off. The lookup for a bundled resources/python/python.exe is removed: no build ships one. When LiteLLM is enabled it runs on the user's own Python from PATH. --- packages/core/src/config.ts | 13 +++++++ packages/core/tests/config.test.ts | 22 +++++++++++ packages/server/src/local/lifecycle.ts | 42 ++------------------ packages/server/src/local/service.ts | 36 ++++++++++------- packages/server/tests/service.test.ts | 53 ++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 51 deletions(-) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 702039b..2ca20e1 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -50,6 +50,13 @@ interface ConfigData { threshold?: number; topK?: number; }; + /** + * Optional LiteLLM proxy. Off unless `enabled` is true; it needs the user's + * own Python with litellm installed. The built-in provider proxy is the default. + */ + litellm?: { + enabled?: boolean; + }; } /** Resolved MCP tool-retrieval config (all fields present). */ @@ -213,6 +220,12 @@ export class WaggleConfig { this.data.cli = { ...(this.data.cli ?? {}), allowlist: next }; } + // --- LiteLLM proxy (opt-in) --- + + isLiteLLMEnabled(): boolean { + return this.data.litellm?.enabled === true; + } + // --- Telemetry (M2-7) --- getTelemetryEnabled(): boolean { diff --git a/packages/core/tests/config.test.ts b/packages/core/tests/config.test.ts index d801564..74d8c6d 100644 --- a/packages/core/tests/config.test.ts +++ b/packages/core/tests/config.test.ts @@ -120,6 +120,28 @@ describe('WaggleConfig', () => { }); }); + describe('LiteLLM opt-in', () => { + it('is off by default and when not explicitly enabled', () => { + const configDir = makeTempDir(); + expect(new WaggleConfig(configDir).isLiteLLMEnabled()).toBe(false); + + fs.writeFileSync( + path.join(configDir, 'config.json'), + JSON.stringify({ defaultModel: 'claude-sonnet-4-6', providers: {}, litellm: {} }), + ); + expect(new WaggleConfig(configDir).isLiteLLMEnabled()).toBe(false); + }); + + it('is on when config.json sets litellm.enabled to true', () => { + const configDir = makeTempDir(); + fs.writeFileSync( + path.join(configDir, 'config.json'), + JSON.stringify({ defaultModel: 'claude-sonnet-4-6', providers: {}, litellm: { enabled: true } }), + ); + expect(new WaggleConfig(configDir).isLiteLLMEnabled()).toBe(true); + }); + }); + describe('governed CLI config', () => { it('normalizes, deduplicates, and persists the CLI allowlist', () => { const configDir = makeTempDir(); diff --git a/packages/server/src/local/lifecycle.ts b/packages/server/src/local/lifecycle.ts index 79d9518..4b306cd 100644 --- a/packages/server/src/local/lifecycle.ts +++ b/packages/server/src/local/lifecycle.ts @@ -1,13 +1,9 @@ import { spawnSync, type ChildProcess } from 'node:child_process'; -import { existsSync, openSync } from 'node:fs'; +import { openSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; -import { fileURLToPath } from 'node:url'; import { spawnSidecarOwnedProcess } from '@waggle/agent'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - export interface LiteLLMStatus { status: 'running' | 'started' | 'timeout' | 'error'; port: number; @@ -79,32 +75,6 @@ async function stopOwnedLiteLLMProcess( }); } -/** - * Look for a bundled Python executable in the app's resources directory. - * On an installed Tauri app the layout is: - * {exe_dir}/resources/python/python.exe - * During development we also check relative to this source file: - * app/src-tauri/resources/python/python.exe - * - * Returns the absolute path if found, otherwise null (falls back to system PATH). - */ -export function getBundledPythonPath(): string | null { - // Installed app: next to the running executable - const exeDir = path.dirname(process.execPath); - const installedPath = path.join(exeDir, 'resources', 'python', 'python.exe'); - if (existsSync(installedPath)) { - return installedPath; - } - - // Development: relative to this file → ../../app/src-tauri/resources - const devPath = path.resolve(__dirname, '..', '..', '..', 'app', 'src-tauri', 'resources', 'python', 'python.exe'); - if (existsSync(devPath)) { - return devPath; - } - - return null; -} - export function selectLiteLLMPython( candidates: readonly string[], supportsLiteLLM: (pythonBin: string) => boolean, @@ -169,13 +139,9 @@ function canImportLiteLLM(pythonBin: string): boolean { return result.status === 0 && !result.error; } +/** The user's own Python with litellm installed; Waggle does not ship one. */ export function resolveLiteLLMPython(): string | null { - const bundledPython = getBundledPythonPath(); - const candidates = [ - ...(bundledPython ? [bundledPython] : []), - ...discoverSystemPythonPaths(), - ]; - return selectLiteLLMPython(candidates, canImportLiteLLM); + return selectLiteLLMPython(discoverSystemPythonPaths(), canImportLiteLLM); } async function checkHealth(port: number): Promise { @@ -205,7 +171,7 @@ export async function getLiteLLMStatus(port?: number): Promise { /** * Start LiteLLM proxy. If already running, returns immediately. * Otherwise spawns `python -m litellm.proxy.proxy_cli --port {port}` and polls health. - * Prefers a compatible bundled Python, then probes every Python on system PATH. + * Probes every Python on the system PATH for one that can import LiteLLM. */ export async function startLiteLLM(port?: number, configPath?: string): Promise { const p = port ?? DEFAULT_PORT; diff --git a/packages/server/src/local/service.ts b/packages/server/src/local/service.ts index 6e95319..b5a8aec 100644 --- a/packages/server/src/local/service.ts +++ b/packages/server/src/local/service.ts @@ -4,7 +4,7 @@ import net from 'node:net'; import path from 'node:path'; import os from 'node:os'; import type { FastifyInstance } from 'fastify'; -import { needsMigration, migrateToMultiMind, MindDB, VaultStore } from '@waggle/core'; +import { needsMigration, migrateToMultiMind, MindDB, VaultStore, WaggleConfig } from '@waggle/core'; import { buildLocalServer } from './index.js'; import type { LlmHealthStatus } from './index.js'; import { startLiteLLM, stopLiteLLM, type LiteLLMStatus } from './lifecycle.js'; @@ -175,7 +175,7 @@ function getConfiguredProviderIds(dataDir: string, server?: FastifyInstance): st * 1. Resolves/creates dataDir (option > WAGGLE_DATA_DIR > ~/.waggle) * 2. Runs migration if needed (default.mind -> personal.mind) * 3. Creates personal.mind if fresh install - * 4. Starts LiteLLM proxy (unless skipped) + * 4. Starts the LiteLLM proxy when config.json enables it and it is not skipped * 5. Builds & starts local Fastify server * 6. Registers graceful shutdown handlers * @@ -256,12 +256,20 @@ export async function startService(options?: ServiceOptions): Promise => { try { await server.close(); } catch { /* preserve the listen error */ } - if (!skipLiteLLM) await stopLiteLLM().catch(() => undefined); + if (useLiteLLM) await stopLiteLLM().catch(() => undefined); }; try { await server.listen({ port, host: bindHost }); @@ -351,7 +359,7 @@ export async function startService(options?: ServiceOptions): Promise undefined); + if (useLiteLLM) await stopLiteLLM().catch(() => undefined); throw new Error('Unable to resolve the Waggle service listen port'); } const actualPort = listeningAddress.port; @@ -403,7 +411,9 @@ export async function startService(options?: ServiceOptions): Promise { }]); }); + it('leaves LiteLLM off and uses the built-in proxy unless config.json enables it', async () => { + const dataDir = makeTmpDir(); + tmpDirs.push(dataDir); + const port = randomPort(); + const litellmPort = randomPort(); + + clearProviderEnv(); + vi.stubEnv('OPENAI_API_KEY', 'openai-solo-test-key'); + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation( + async () => new Response('{}', { status: 503 }), + ); + + const { server, litellm } = await startService({ dataDir, port, litellmPort }); + cleanups.push(async () => { await server.close(); }); + + expect(litellm).toEqual({ status: 'error', port: litellmPort, error: 'Not enabled in config.json' }); + expect(server.localConfig.manageLiteLLM).toBe(false); + expect(server.localConfig.useBuiltInProxy).toBe(true); + expect(server.localConfig.litellmUrl).toBe(`http://127.0.0.1:${port}/v1`); + expect(server.agentState.llmProvider.provider).toBe('anthropic-proxy'); + expect(fetchSpy.mock.calls.some(([input]) => ( + String(input) === `http://localhost:${litellmPort}/health/liveliness` + ))).toBe(false); + }); + + it('manages LiteLLM when config.json enables it', async () => { + const dataDir = makeTmpDir(); + tmpDirs.push(dataDir); + const port = randomPort(); + const litellmPort = randomPort(); + fs.writeFileSync( + path.join(dataDir, 'config.json'), + JSON.stringify({ defaultModel: 'claude-sonnet-4-6', providers: {}, litellm: { enabled: true } }), + ); + + // No provider key: the runtime catalog is empty, so no Python is spawned. + clearProviderEnv(); + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation( + async () => new Response('{}', { status: 503 }), + ); + + const { server, litellm } = await startService({ dataDir, port, litellmPort }); + cleanups.push(async () => { await server.close(); }); + + expect(litellm).toEqual({ status: 'error', port: litellmPort, error: 'No provider models available' }); + expect(server.localConfig.manageLiteLLM).toBe(true); + expect(server.localConfig.managedLiteLLMPort).toBe(litellmPort); + expect(server.localConfig.useBuiltInProxy).toBe(true); + expect(fetchSpy.mock.calls.some(([input]) => ( + String(input) === `http://localhost:${litellmPort}/health/liveliness` + ))).toBe(true); + }); + it('atomically falls back from an occupied desktop port and routes spawned agents to it', async () => { const base = makeTmpDir(); tmpDirs.push(base);