fix(server): send the session token on the sidecar's calls to its own proxy (UF-044)

Since the server stopped trusting loopback callers, the built-in model proxy
needs the session token like every other route, but the sidecar's own calls
to it sent none. The Models key check read the proxy's 401 as a rejected key,
so a working Anthropic key showed "Key found but not responding". Scheduled AI
tasks and loops, the dream diary narrative and the profile calls failed the
same way; the profile calls also built the port from address().toString(),
and the scheduled tasks used the configured port, which the desktop app's
port fallback can leave pointing at another Waggle server.

self-request.ts now builds the address from the bound port and adds the
session token; all these calls and command.ts use it, and the token is never
sent to Ollama. An end-to-end test runs the key check through the real
middleware and proxy with localhost trust off; it fails without the fix.

UF-045 records the persona generator, which posts to a route that does not
exist; it is left open.
This commit is contained in:
shumeiko.it
2026-10-26 01:46:29 +01:00
parent 5268881f46
commit d6217ad7c2
10 changed files with 200 additions and 34 deletions

View File

@@ -13,6 +13,7 @@ import websocket from '@fastify/websocket';
import { WaggleDB, importLegacyMindData, workspaceDataScope, type LegacyMindSource, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, CronStore, AwarenessLayer, VaultStore, SkillHashStore, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore } from '@waggle/core';
import { corsOriginAllowed } from './cors-config.js';
import { InstallAuditStore } from './install-audit.js';
import { selfBaseUrl, selfRequestHeaders } from './self-request.js';
import { getStorageProvider } from './storage/index.js';
import { isLoopbackBind, resolveBindHost } from './net-config.js';
import { isLocalRequest } from './origin-guard.js';
@@ -1550,10 +1551,10 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
}
// Use the built-in Anthropic proxy to process the prompt
const proxyUrl = `http://127.0.0.1:${fullConfig.port ?? 3333}/v1/chat/completions`;
const proxyUrl = `${selfBaseUrl(server)}/v1/chat/completions`;
const response = await fetch(proxyUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: selfRequestHeaders(server),
body: JSON.stringify({
model: server.agentState.currentModel ?? 'claude-sonnet-4-6',
max_tokens: 2048,
@@ -1662,11 +1663,11 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
}
if (wsId && wsId !== '*') activateWorkspaceMind(wsId);
const loopModel = server.agentState.currentModel ?? 'claude-sonnet-4-6';
const loopProxyUrl = `http://127.0.0.1:${fullConfig.port ?? 3333}/v1/chat/completions`;
const loopProxyUrl = `${selfBaseUrl(server)}/v1/chat/completions`;
const loopChat = async (prompt: string, maxTokens = 2048): Promise<string> => {
const resp = await fetch(loopProxyUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: selfRequestHeaders(server),
body: JSON.stringify({
model: loopModel,
max_tokens: maxTokens,