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([llm-playground-preserve-partial-output]): address codex+claude review
[._.]/ Adam Eivyce3e2bea0fca
diff --git a/server/services/localLlmPlayground.js b/server/services/localLlmPlayground.jsindex 467eea62b6..d93336903f 100644--- a/server/services/localLlmPlayground.js+++ b/server/services/localLlmPlayground.js@@ -240,6 +240,8 @@ export async function runLocalLlmTest({// A timeout/abort mid-stream still has tokens worth keeping — surface what the// model already streamed (attached to the error by streamChatCompletion) instead// of discarding it. Persist it on the failed run record too so /runs replay shows it.+ // (TTFT is best-effort here: a reasoning-only partial leaves firstChunkAt unset,+ // since reasoning deltas don't mark first-chunk timing during the stream.)const partialText = typeof err?.partialOutput === 'string' ? err.partialOutput : '';if (runId) {await finalizeRunRecord({diff --git a/server/services/localLlmPlayground.test.js b/server/services/localLlmPlayground.test.jsindex 5931ddff6c..6d20fed1ac 100644--- a/server/services/localLlmPlayground.test.js+++ b/server/services/localLlmPlayground.test.js@@ -1,5 +1,14 @@-import { describe, expect, it } from 'vitest';-import { buildPrompt, buildMessages, summarizeTimings, extractStreamDelta, resolvePartialOutput } from './localLlmPlayground.js';+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';++vi.mock('./runner.js', () => ({ createRun: vi.fn(), finalizeRunRecord: vi.fn() }));+vi.mock('./localLlm.js', () => ({ ensureBackendProvider: vi.fn(() => Promise.resolve()) }));+vi.mock('./providers.js', () => ({ getProviderById: vi.fn() }));+vi.mock('./providerStatus.js', () => ({ markProviderAvailable: vi.fn(() => Promise.resolve()) }));+vi.mock('./ollamaManager.js', () => ({ ensureProviderReady: vi.fn(() => Promise.resolve({ success: true })) }));++import { buildPrompt, buildMessages, summarizeTimings, extractStreamDelta, resolvePartialOutput, runLocalLlmTest } from './localLlmPlayground.js';+import { createRun, finalizeRunRecord } from './runner.js';+import { getProviderById } from './providers.js';describe('buildPrompt', () => {it('returns the bare prompt when no system instructions', () => {@@ -90,3 +99,70 @@ describe('resolvePartialOutput', () => {expect(resolvePartialOutput({})).toBe('');});});++// Build a fake stream reader: yields each SSE line as a chunk, then either+// finishes cleanly (done) or throws an AbortError to simulate a timeout.+function makeReader(lines, { abort = false } = {}) {+ let i = 0;+ return {+ read: vi.fn(async () => {+ if (i < lines.length) return { done: false, value: new TextEncoder().encode(lines[i++]) };+ if (abort) {+ const err = new Error('The operation was aborted');+ err.name = 'AbortError';+ throw err;+ }+ return { done: true, value: undefined };+ }),+ cancel: vi.fn(async () => {}),+ };+}++const sse = (delta) => `data: ${JSON.stringify({ choices: [{ delta }] })}\n`;++describe('runLocalLlmTest timeout/abort contract', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ getProviderById.mockResolvedValue({ id: 'lmstudio', type: 'api', endpoint: 'http://localhost:1234/v1' });+ createRun.mockResolvedValue({ runId: 'run-1', provider: { id: 'lmstudio' } });+ finalizeRunRecord.mockResolvedValue(undefined);+ });++ afterEach(() => { delete global.fetch; });++ const stubStream = (reader) => {+ global.fetch = vi.fn().mockResolvedValue({ ok: true, status: 200, body: { getReader: () => reader } });+ };++ it('returns the content streamed before an abort as text and persists it on the failed run', async () => {+ stubStream(makeReader([sse({ content: 'Hello ' }), sse({ content: 'world' })], { abort: true }));++ const result = await runLocalLlmTest({ backend: 'lmstudio', modelId: 'm1', prompt: 'hi', timeoutMs: 5000 });++ expect(result.error).toContain('Timed out');+ expect(result.text).toBe('Hello world');+ expect(finalizeRunRecord).toHaveBeenCalledWith(+ expect.objectContaining({ runId: 'run-1', output: 'Hello world', success: false, exitCode: 1 }),+ );+ });++ it('surfaces a reasoning-only partial when no visible content streamed before the abort', async () => {+ stubStream(makeReader([sse({ reasoning: 'thinking hard…' })], { abort: true }));++ const result = await runLocalLlmTest({ backend: 'lmstudio', modelId: 'm1', prompt: 'hi', timeoutMs: 5000 });++ expect(result.error).toContain('Timed out');+ expect(result.text).toBe('thinking hard…');+ expect(finalizeRunRecord).toHaveBeenCalledWith(expect.objectContaining({ output: 'thinking hard…', success: false }));+ });++ it('returns the full text and a success record when the stream finishes normally', async () => {+ stubStream(makeReader([sse({ content: 'Done.' })], { abort: false }));++ const result = await runLocalLlmTest({ backend: 'lmstudio', modelId: 'm1', prompt: 'hi', timeoutMs: 5000 });++ expect(result.error).toBeUndefined();+ expect(result.text).toBe('Done.');+ expect(finalizeRunRecord).toHaveBeenCalledWith(expect.objectContaining({ output: 'Done.', success: true, exitCode: 0 }));+ });+});