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([issue-11742]): bound response bodies through completion (#11789)
[._.]/3ab3f74f0662
diff --git a/server/services/aiProvider.js b/server/services/aiProvider.jsindex c13b9abc20..8fb6105548 100644--- a/server/services/aiProvider.js+++ b/server/services/aiProvider.js@@ -136,13 +136,19 @@ async function ensureLMStudioModelLoaded(provider, statusOp) {const listCtl = new AbortController();const listTimer = setTimeout(() => listCtl.abort(), LM_STUDIO_LIST_TIMEOUT_MS);- const listResp = await fetch(`${baseUrl}/api/v0/models`, {- method: 'GET',- signal: listCtl.signal- }).catch(() => null).finally(() => clearTimeout(listTimer));-- if (!listResp?.ok) return null;- const payload = await listResp.json().catch(() => null);+ let payload;+ try {+ const listResp = await fetch(`${baseUrl}/api/v0/models`, {+ method: 'GET',+ signal: listCtl.signal+ });+ if (!listResp.ok) return null;+ payload = await listResp.json();+ } catch {+ return null;+ } finally {+ clearTimeout(listTimer);+ }const llms = (payload?.data || []).filter(m => m.type === 'llm');if (llms.length === 0) {console.warn('⚠️ LM Studio has no downloaded LLMs — auto-load impossible');@@ -167,18 +173,23 @@ async function ensureLMStudioModelLoaded(provider, statusOp) {const loadStart = Date.now();const loadCtl = new AbortController();const loadTimer = setTimeout(() => loadCtl.abort(), LM_STUDIO_LOAD_TIMEOUT_MS);- const loadResp = await fetch(`${baseUrl}/api/v1/models/load`, {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify({ model: target.id }),- signal: loadCtl.signal- }).catch(err => ({ ok: false, _err: err.message })).finally(() => clearTimeout(loadTimer));-- if (!loadResp.ok) {- const errText = loadResp._err || await loadResp.text?.().catch(() => 'unknown error') || 'unknown error';- console.error(`❌ Failed to auto-load LM Studio model ${target.id}: ${errText}`);- statusOp?.update('error', `Failed to load ${target.id}: ${errText}`, { model: target.id });+ try {+ const loadResp = await fetch(`${baseUrl}/api/v1/models/load`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ model: target.id }),+ signal: loadCtl.signal+ });+ if (!loadResp.ok) {+ const errText = await loadResp.text().catch(() => 'unknown error');+ throw new Error(errText || 'unknown error');+ }+ } catch (err) {+ console.error(`❌ Failed to auto-load LM Studio model ${target.id}: ${err.message}`);+ statusOp?.update('error', `Failed to load ${target.id}: ${err.message}`, { model: target.id });return null;+ } finally {+ clearTimeout(loadTimer);}const loadMs = Date.now() - loadStart;@@ -219,55 +230,54 @@ async function postChatCompletion(provider, model, prompt, { temperature, max_tosignal: controller.signal,body: JSON.stringify({ model, messages: [{ role: 'user', content: prompt }], temperature, max_tokens })});++ if (!response.ok) {+ const errorText = await response.text().catch(() => 'Unknown error');+ return { error: `Provider returned ${response.status}: ${errorText}`, status: response.status, body: errorText };+ }++ const data = await readResponseJson(response);+ // A valid completion always carries `choices`; its absence means the 200 body+ // wasn't the expected JSON (e.g. a proxy HTML page). Surface that as an error+ // rather than masquerading a malformed body as an empty-but-successful reply.+ const content = data.choices?.[0]?.message?.content;+ if (typeof content !== 'string') {+ return { error: 'Provider returned a malformed (non-JSON or unexpected) response body' };+ }+ // An empty/whitespace-only completion is unusable output, not a successful call —+ // same class as the malformed body above. Classifying it here keeps the `ai:status`+ // stream honest (a call that produced nothing must not report "done") and gives+ // callers a single "the provider failed" signal to test: `result.error`.+ if (!content.trim()) {+ return { error: 'Provider returned an empty completion' };+ }+ // OpenAI-compatible bodies report token counts under `usage`; surface the completion+ // token count so the AI Core landmark can size its activity beam by output volume+ // (tokens/sec). Absent on some providers — callers treat a missing count as "unknown".+ const completionTokens = Number.isFinite(data.usage?.completion_tokens) ? data.usage.completion_tokens : NaN;+ const promptTokens = Number.isFinite(data.usage?.prompt_tokens) ? data.usage.prompt_tokens : NaN;+ return {+ text: content,+ finishReason: normalizeReviewFinishReason(data?.choices?.[0]?.finish_reason),+ tokens: Number.isFinite(completionTokens) ? completionTokens : undefined,+ ...(Number.isFinite(promptTokens) || Number.isFinite(completionTokens)+ ? { usage: {+ ...(Number.isFinite(promptTokens) ? { inputTokens: promptTokens } : {}),+ ...(Number.isFinite(completionTokens) ? { outputTokens: completionTokens } : {}),+ } }+ : {}),+ };} catch (err) {- clearTimeout(timer);- signal?.removeEventListener('abort', onAbort);// undici wraps the real network error in `fetch failed`; the actionable// detail (ECONNRESET, model-specific transport quirk, etc.) lives in// `err.cause` and is otherwise silently discarded (#7669).const cause = err.cause;const causeDetail = cause ? ` (cause: ${[cause.code, cause.message].filter(Boolean).join(' ')})` : '';return { error: `Provider request failed: ${err.message}${causeDetail}` };+ } finally {+ clearTimeout(timer);+ signal?.removeEventListener('abort', onAbort);}- clearTimeout(timer);- signal?.removeEventListener('abort', onAbort);-- if (!response.ok) {- const errorText = await response.text().catch(() => 'Unknown error');- return { error: `Provider returned ${response.status}: ${errorText}`, status: response.status, body: errorText };- }-- const data = await readResponseJson(response);- // A valid completion always carries `choices`; its absence means the 200 body- // wasn't the expected JSON (e.g. a proxy HTML page). Surface that as an error- // rather than masquerading a malformed body as an empty-but-successful reply.- const content = data.choices?.[0]?.message?.content;- if (typeof content !== 'string') {- return { error: 'Provider returned a malformed (non-JSON or unexpected) response body' };- }- // An empty/whitespace-only completion is unusable output, not a successful call —- // same class as the malformed body above. Classifying it here keeps the `ai:status`- // stream honest (a call that produced nothing must not report "done") and gives- // callers a single "the provider failed" signal to test: `result.error`.- if (!content.trim()) {- return { error: 'Provider returned an empty completion' };- }- // OpenAI-compatible bodies report token counts under `usage`; surface the completion- // token count so the AI Core landmark can size its activity beam by output volume- // (tokens/sec). Absent on some providers — callers treat a missing count as "unknown".- const completionTokens = Number.isFinite(data.usage?.completion_tokens) ? data.usage.completion_tokens : NaN;- const promptTokens = Number.isFinite(data.usage?.prompt_tokens) ? data.usage.prompt_tokens : NaN;- return {- text: content,- finishReason: normalizeReviewFinishReason(data?.choices?.[0]?.finish_reason),- tokens: Number.isFinite(completionTokens) ? completionTokens : undefined,- ...(Number.isFinite(promptTokens) || Number.isFinite(completionTokens)- ? { usage: {- ...(Number.isFinite(promptTokens) ? { inputTokens: promptTokens } : {}),- ...(Number.isFinite(completionTokens) ? { outputTokens: completionTokens } : {}),- } }- : {}),- };}/**diff --git a/server/services/aiProvider.test.js b/server/services/aiProvider.test.jsindex ddaddd65e5..5d6d4d6d96 100644--- a/server/services/aiProvider.test.js+++ b/server/services/aiProvider.test.js@@ -491,3 +491,61 @@ describe('callProviderAISimple — endpoint guard (SSRF / key-exfiltration)', ()expect(result).toMatchObject({ text: 'hello' });});});+++describe('HTTP provider body cancellation', () => {+ const provider = { id: 'body-test', type: 'api', endpoint: 'https://api.example.com/v1', timeout: 1000 };++ afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ it.each(['deadline', 'caller'])('keeps %s cancellation active after completion headers', async (cancel) => {+ vi.useFakeTimers();+ const caller = new AbortController();+ let requestSignal;+ let started;+ const bodyStarted = new Promise(resolve => { started = resolve; });+ vi.stubGlobal('fetch', vi.fn(async (_url, { signal }) => {+ requestSignal = signal;+ return { ok: true, text: () => {+ started();+ return new Promise((_, reject) => signal.addEventListener('abort', () => reject(new Error('body aborted')), { once: true }));+ } };+ }));+ const pending = callProviderAISimple(provider, 'model', 'prompt', { signal: caller.signal, allowModelRecovery: false });+ await bodyStarted;+ expect(requestSignal.aborted).toBe(false);+ if (cancel === 'caller') caller.abort();+ else await vi.advanceTimersByTimeAsync(1000);+ expect((await pending).error).toContain('body aborted');+ expect(requestSignal.aborted).toBe(true);+ expect(vi.getTimerCount()).toBe(0);+ });++ it.each(['list', 'load-error'])('bounds the LM Studio %s body after headers', async (stage) => {+ vi.useFakeTimers();+ let started;+ const bodyStarted = new Promise(resolve => { started = resolve; });+ let requestSignal;+ vi.stubGlobal('fetch', vi.fn(async (url, { signal }) => {+ if (url.endsWith('/chat/completions')) return { ok: false, status: 400, text: async () => 'No models loaded' };+ if (url.endsWith('/api/v0/models') && stage !== 'list') {+ return { ok: true, json: async () => ({ data: [{ type: 'llm', id: 'model', state: 'not-loaded' }] }) };+ }+ requestSignal = signal;+ const stalled = () => {+ started();+ return new Promise((_, reject) => signal.addEventListener('abort', () => reject(new Error('body aborted')), { once: true }));+ };+ return { ok: stage === 'list', json: stalled, text: stalled };+ }));+ const pending = callProviderAISimple(provider, 'model', 'prompt');+ await bodyStarted;+ await vi.advanceTimersByTimeAsync(stage === 'list' ? 5000 : 120000);+ expect((await pending).error).toContain('No models loaded');+ expect(requestSignal.aborted).toBe(true);+ expect(vi.getTimerCount()).toBe(0);+ });+});diff --git a/server/services/privacyScan.js b/server/services/privacyScan.jsindex 6af0198cbe..a836c604fe 100644--- a/server/services/privacyScan.js+++ b/server/services/privacyScan.js@@ -207,15 +207,20 @@ async function isScanUrlSafe(target) {async function httpFetchLane(url, fetchImpl) {const controller = new AbortController();const timer = setTimeout(() => controller.abort(), HTTP_FETCH_TIMEOUT_MS);- const res = await fetchImpl(url, {- signal: controller.signal,- redirect: 'follow',- headers: { 'User-Agent': 'Mozilla/5.0 (compatible; PortOS-PrivacyScan/1.0)' },- }).catch(() => null);- clearTimeout(timer);- if (!res) return null;- const html = await res.text().catch(() => '');- return { status: res.status, html };+ try {+ const res = await fetchImpl(url, {+ signal: controller.signal,+ redirect: 'follow',+ headers: { 'User-Agent': 'Mozilla/5.0 (compatible; PortOS-PrivacyScan/1.0)' },+ });+ const html = await res.text();+ return { status: res.status, html };+ } catch {+ // An unreadable body is inconclusive, even when headers reported success.+ return null;+ } finally {+ clearTimeout(timer);+ }}/**diff --git a/server/services/privacyScan.test.js b/server/services/privacyScan.test.jsindex 9363856238..46e73729f3 100644--- a/server/services/privacyScan.test.js+++ b/server/services/privacyScan.test.js@@ -5,7 +5,7 @@* wiring is exercised without Postgres.*/-import { describe, it, expect, vi } from 'vitest';+import { describe, it, expect, vi, afterEach } from 'vitest';vi.mock('./privacyBrokers.js', async (importOriginal) => {const actual = await importOriginal();@@ -126,6 +126,35 @@ describe('scanBroker — classify → record wiring', () => {const vectors = { names: [{ full: 'Jane Q Doe', firstName: 'Jane', lastName: 'Doe' }], locations: [{ city: 'Portland', state: 'OR' }] };const broker = { id: 'spokeo', urls: { search: 'https://b.example/{firstName}-{lastName}/{state}' } };+ afterEach(() => vi.useRealTimers());++ it.each(['read failure', 'deadline'])('keeps an HTTP 200 body %s inconclusive without recording not_found', async (failure) => {+ vi.useFakeTimers();+ brokers.recordScanVerdict.mockClear();+ const browserFetch = vi.fn();+ let started;+ const bodyStarted = new Promise(resolve => { started = resolve; });+ let requestSignal;+ const fetchImpl = vi.fn(async (_url, { signal }) => {+ requestSignal = signal;+ return { status: 200, text: () => {+ started();+ if (failure === 'read failure') return Promise.reject(new Error('broken body'));+ return new Promise((_, reject) => signal.addEventListener('abort', () => reject(new Error('body aborted')), { once: true }));+ } };+ });+ const pending = scanBroker(broker, vectors, { fetchImpl, browserFetch, urlSafe: async () => true });+ await bodyStarted;+ if (failure === 'deadline') {+ await vi.advanceTimersByTimeAsync(15000);+ expect(requestSignal.aborted).toBe(true);+ }+ expect(await pending).toEqual({ skipped: true, reason: 'fetch_failed' });+ expect(brokers.recordScanVerdict).not.toHaveBeenCalled();+ expect(browserFetch).not.toHaveBeenCalled();+ expect(vi.getTimerCount()).toBe(0);+ });+it('records a found verdict from the HTTP lane', async () => {const fetchImpl = vi.fn(async () => ({ status: 200, text: async () => 'Found: Jane Q Doe in Portland, OR — full profile' }));const kase = await brokers.recordScanVerdict;diff --git a/server/services/sharing/integrity.js b/server/services/sharing/integrity.jsindex 331d59a95f..e7521e0981 100644--- a/server/services/sharing/integrity.js+++ b/server/services/sharing/integrity.js@@ -19,9 +19,10 @@ import { listLooms } from '../fableLoom/index.js';import { getPeers } from '../instances.js';import { assetIntegrityForRecord } from './peerSync.js';import { peerBaseUrl } from '../../lib/peerUrl.js';-import { peerFetch } from '../../lib/peerHttpClient.js';+import { peerFetch, readPeerBody } from '../../lib/peerHttpClient.js';const PEER_INTEGRITY_TIMEOUT_MS = 15_000;+const PEER_INTEGRITY_MAX_BYTES = 16 * 1024 * 1024;async function recordsForKind(kind) {if (kind === 'mediaCollection') return listCollections({ includeDeleted: true });@@ -82,7 +83,7 @@ export async function getPeerIntegrity({ peerId, kind }) {const res = await peerFetch(`${peerBaseUrl(peer)}/api/peer-sync/manifest?kind=${encodeURIComponent(kind)}`,- { signal: AbortSignal.timeout(PEER_INTEGRITY_TIMEOUT_MS) },+ { signal: AbortSignal.timeout(PEER_INTEGRITY_TIMEOUT_MS), maxBytes: PEER_INTEGRITY_MAX_BYTES },peer,).catch(() => null);@@ -94,7 +95,11 @@ export async function getPeerIntegrity({ peerId, kind }) {if (res.status === 404) return { available: false, reason: 'peer-too-old', records: [] };if (!res.ok) return { available: false, reason: 'fetch-failed', records: [] };- const body = await res.json().catch(() => null);+ const body = await readPeerBody(res, 'json', {+ idleTimeoutMs: PEER_INTEGRITY_TIMEOUT_MS,+ maxBytes: PEER_INTEGRITY_MAX_BYTES,+ }).catch(() => null);+ if (!body) return { available: false, reason: 'fetch-failed', records: [] };// The peer response is untrusted — computeRecordIntegrity assumes every entry// is a non-null object with a string `id` (it reads `r.id` and keys a Map on// it). A hostile/malformed manifest (nulls, scalars, id-less objects) woulddiff --git a/server/services/sharing/integrity.test.js b/server/services/sharing/integrity.test.jsindex 09b761d62d..b65c95be4c 100644--- a/server/services/sharing/integrity.test.js+++ b/server/services/sharing/integrity.test.js@@ -1,7 +1,8 @@-import { describe, it, expect, beforeEach, vi } from 'vitest';+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';// Mock peerFetch so no network calls go out.-vi.mock('../../lib/peerHttpClient.js', async () => ({+vi.mock('../../lib/peerHttpClient.js', async (importOriginal) => ({+ readPeerBody: (await importOriginal()).readPeerBody,peerFetch: vi.fn(),peerSocketOptions: {},}));@@ -142,6 +143,34 @@ describe('buildLocalManifest', () => {});describe('getPeerIntegrity', () => {+ afterEach(() => vi.useRealTimers());++ it.each(['stalled', 'oversized'])('rejects a %s manifest body without computing a misleading diff', async (failure) => {+ vi.useFakeTimers();+ vi.mocked(getPeers).mockResolvedValue([makePeer()]);+ const cancel = vi.fn();+ let started;+ const bodyStarted = new Promise(resolve => { started = resolve; });+ const stream = new ReadableStream({+ start(controller) {+ if (failure === 'oversized') controller.enqueue(new Uint8Array(16 * 1024 * 1024 + 1));+ },+ pull() { started(); },+ cancel,+ });+ vi.mocked(peerFetch).mockResolvedValue(new Response(stream));+ const pending = getPeerIntegrity({ peerId: 'peer-x', kind: 'mediaCollection' });+ await bodyStarted;+ if (failure === 'stalled') await vi.advanceTimersByTimeAsync(15000);+ expect(await pending).toEqual({ available: false, reason: 'fetch-failed', records: [] });+ expect(cancel).toHaveBeenCalled();+ expect(listCollections).not.toHaveBeenCalled();+ const [, options] = vi.mocked(peerFetch).mock.calls.at(-1);+ expect(options.maxBytes).toBe(16 * 1024 * 1024);+ expect(options.signal).toBeInstanceOf(AbortSignal);+ expect(vi.getTimerCount()).toBe(0);+ });+it('returns available:false with reason peer-not-found when peer is unknown', async () => {vi.mocked(getPeers).mockResolvedValue([]);const result = await getPeerIntegrity({ peerId: 'no-such-peer', kind: 'mediaCollection' });@@ -283,7 +312,7 @@ describe('getPeerIntegrity', () => {expect(result.records).toEqual([]);});- it('returns an empty available result when peer json parsing fails', async () => {+ it('returns unavailable when peer json parsing fails', async () => {const peer = makePeer('peer-x');vi.mocked(getPeers).mockResolvedValue([peer]);vi.mocked(listCollections).mockResolvedValue([]);@@ -294,6 +323,6 @@ describe('getPeerIntegrity', () => {});const result = await getPeerIntegrity({ peerId: 'peer-x', kind: 'mediaCollection' });- expect(result).toEqual({ available: true, records: [] });+ expect(result).toEqual({ available: false, reason: 'fetch-failed', records: [] });});});