Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

Response settings reach agent turns (thinking budget, Expert sampling) through a local forwarding endpoint; step summaries from what the step changed; plan title heading

Niko1221b6b546ae1313
diff --git a/apps/host/scripts/agent-http.mjs b/apps/host/scripts/agent-http.mjs
index 6779e1c78..6e75e2a7c 100644
--- a/apps/host/scripts/agent-http.mjs
+++ b/apps/host/scripts/agent-http.mjs
@@ -192,6 +192,32 @@ ${filler(n)}`}, { quiet: true });
}
frames = await stream({ ...common, message: 'What was the code word I asked you to remember at the very start? Answer with the code word only.' }, { quiet: true });
console.log(`${stamp()} ANSWER: ${frames.filter(f => f.type === "delta").map(f => f.text).join("").trim().slice(0, 120)}`);
+} else if (command === 'effort') {
+ // The same question at each reasoning level (and optional thinking budget): what really changes is the thinking the model did.
+ const [question = 'What is 17 times 23, and is the result divisible by 3? Explain briefly.', budget = '0'] = rest;
+ const rows = [];
+ for (const level of ['Off', 'Low', 'Medium', 'High', 'Max']) {
+ const t = Date.now();
+ const frames = await stream({ sessionId: `ef-${level}-${Date.now()}`, message: question, mode: 'bypass', remoteModel: await model(), thinking: level, thinkingBudget: Number(budget), agent: process.env.PLAIN ? false : undefined }, { quiet: true });
+ const reasoning = frames.filter(x => x.type === 'reasoning').map(x => x.text ?? '').join('').length;
+ const answer = frames.filter(x => x.type === 'delta').map(x => x.text ?? '').join('');
+ const done = frames.at(-1);
+ rows.push({ level, budget: Number(budget), reasoningChars: reasoning, answerChars: answer.length, tokensOut: done.tokensOut, seconds: Math.round((Date.now() - t) / 1000), answer: answer.replace(/\s+/g, ' ').slice(0, 90) });
+ console.log(JSON.stringify(rows.at(-1)));
+ }
+} else if (command === 'sampling') {
+ // Set Expert sampling through the engine settings route (as the Response settings dialog does), then ask for a random word a few times.
+ const [temp = '0', seed = '1', n = '3'] = rest;
+ const before = await j('/api/engine');
+ const set = async values => j('/api/engine', { expert: Object.entries(values).map(([id, value]) => ({ id, value })) }, 'PATCH');
+ await set({ 'sample.temp': Number(temp), 'sample.seed': Number(seed) });
+ const words = [];
+ for (let i = 0; i < Number(n); i++) {
+ const frames = await stream({ sessionId: `sm-${Date.now()}-${i}`, message: 'Reply with one random everyday English word and nothing else.', mode: 'bypass', remoteModel: await model(), thinking: 'Off' }, { quiet: true });
+ words.push(frames.filter(x => x.type === 'delta').map(x => x.text ?? '').join('').trim().slice(0, 30));
+ }
+ console.log(JSON.stringify({ temp: Number(temp), seed: Number(seed), words }));
+ await set({ 'sample.temp': 0.6, 'sample.seed': -1 });
} else {
- console.error('commands: connect | stream | goal | timeskip | plan | modeswitch | long'); process.exit(2);
+ console.error('commands: connect | stream | goal | timeskip | plan | modeswitch | long | effort | sampling'); process.exit(2);
}
diff --git a/apps/host/server/agent/agent-bridge.mjs b/apps/host/server/agent/agent-bridge.mjs
index d335601f8..feb9f1868 100644
--- a/apps/host/server/agent/agent-bridge.mjs
+++ b/apps/host/server/agent/agent-bridge.mjs
@@ -23,7 +23,8 @@ import { StatusReporter } from './status.mjs';
import { buildProfile, routeFor, MODE_PRESETS, modeOf, effortOf } from './profile.mjs';
import { TurnProjector, parseArguments as parseArgs } from './events.mjs';
import { ChildProjector } from './children.mjs';
-import { PlanStore, planPrompt, parsePlan, parsePoints, stepObjective, stepLine, firstSentence, planAsPrompt, editedSteps, planView, researchPrompt, researchWritePrompt, RESEARCH_NUDGE, parseSources } from './plan.mjs';
+import { nearestBudget } from './llm-shape.mjs';
+import { PlanStore, planPrompt, parsePlan, parsePoints, stepObjective, stepLine, firstSentence, stepSummary, planAsPrompt, editedSteps, planView, researchPrompt, researchWritePrompt, RESEARCH_NUDGE, parseSources } from './plan.mjs';
/**
* A failure the person can read. `message` is a plain sentence with the next step; `details` keeps the technical
@@ -56,7 +57,7 @@ const deferred = () => { let resolve; const promise = new Promise(r => { resolve
* @param {string} [options.nodePath] the Node runtime that runs the Harness
* @param {(line:string)=>void} [options.log]
*/
-export function createAgentBridge({ dataDir, componentsDir = null, bundledDir = null, gate = true, localInstances, connections, tools = () => undefined, skillsDir = () => null, onGoal = () => {}, onPlan = () => {}, onQueue = () => {}, env = process.env, nodePath = process.execPath, log = () => {}, startTimeoutMs = 120_000 }) {
+export function createAgentBridge({ dataDir, componentsDir = null, bundledDir = null, gate = true, localInstances, connections, tools = () => undefined, skillsDir = () => null, onGoal = () => {}, onPlan = () => {}, onQueue = () => {}, proxyBase = null, expertSampling = () => null, env = process.env, nodePath = process.execPath, log = () => {}, startTimeoutMs = 120_000 }) {
const root = join(dataDir, 'agent');
const home = join(root, 'home');
const sessionsFile = join(root, 'sessions.json');
@@ -97,6 +98,15 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
signal?.addEventListener('abort', () => { clearTimeout(timer); idleWaiters.delete(done); reject(signal.reason ?? new Error('aborted')); }, { once: true });
});
+ /** The agent's profile now, plus where its shaped routes really lead (read by the forwarding endpoint). */
+ let shapeTargets = {};
+ function buildProfileNow() {
+ const profile = buildProfile({ local: localInstances(), connections: connections(), gate, tools: tools(), skillsDir: skillsDir(), proxyBase });
+ shapeTargets = profile.shapeTargets ?? {};
+ return profile;
+ }
+ const llmTarget = key => shapeTargets[key] ?? null;
+
function harnessInfo() { return resolveHarness({ env, componentsDir, bundledDir }); }
/** Where the agent component is (the browser tool server lives inside it), or null. */
const harnessDir = () => { const h = harnessInfo(); return h.ok ? h.dir : null; };
@@ -105,7 +115,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
function status() {
const h = harnessInfo();
if (!h.ok) return { state: 'unavailable', reason: 'The coding assistant isn’t installed on this computer yet. Chat works without it.', detail: h.reason, code: 'component-missing' };
- const profile = buildProfile({ local: localInstances(), connections: connections(), gate, tools: tools(), skillsDir: skillsDir() });
+ const profile = buildProfileNow();
return {
state: rt?.hp.running ? 'ready' : starting ? 'starting' : lastError ? 'error' : 'idle',
reason: lastError ? plainAgentFailure(lastError) === lastError ? 'The coding assistant could not start. Try again.' : plainAgentFailure(lastError) : null,
@@ -153,7 +163,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
async function ensureRuntime(choice, signal) {
for (;;) {
if (starting) { await starting.catch(() => {}); continue; }
- const profile = buildProfile({ local: localInstances(), connections: connections(), gate, tools: tools(), skillsDir: skillsDir() });
+ const profile = buildProfileNow();
const route = routeFor(profile, choice);
if (!route) {
throw new AgentUnavailable(choice?.connectionId
@@ -246,7 +256,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
try { return await runTurnInner(c, options); } finally { c.starting = false; }
}
- async function runTurnInner(c, { chatId, message, workspacePath, choice = null, mode = null, history = [], onFrame, requestId = `req-${randomUUID()}`, signal, goal = null, thinking = null, planTask = null, planRun = null, researchTopic = null }) {
+ async function runTurnInner(c, { chatId, message, workspacePath, choice = null, mode = null, history = [], onFrame, requestId = `req-${randomUUID()}`, signal, goal = null, thinking = null, thinkingBudget = 0, planTask = null, planRun = null, researchTopic = null }) {
let send = onFrame; let attached = true;
// A research run gathers first; what the assistant writes while gathering is notes, not the answer, so it is not shown.
let hideDeltas = !!researchTopic;
@@ -271,9 +281,14 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
// The model and the chat's thinking level (the Reasoning menu) are sent to the Harness when either changed. A level the route does
// not have (an API model with its own vocabulary) is dropped and the model's own default stands: never an error for the person.
const level = effortOf(thinking ?? 'Medium'); // the menu's default is Medium
- const wanted = `${route.route}/${route.modelId}/${level ?? ''}`;
+ // Response settings the Harness cannot send itself (the thinking budget, the Expert sampling) go through this host's forwarding
+ // endpoint, on a copy of the route; with every setting at its default the route is used directly, as before.
+ const budget = level === 'off' ? 0 : nearestBudget(thinkingBudget);
+ const sampling = proxyBase && route.shaped ? expertSampling() : null;
+ const provider = route.shaped && (budget || sampling) ? route.shaped[budget] : route.route;
+ const wanted = `${provider}/${route.modelId}/${level ?? ''}`;
if (c.route !== wanted || c.routeGeneration !== runtime.generation) {
- const select = effort => runtime.gw.rpc('session/selectModel', { request: { sessionId, provider: route.route, model: route.modelId, ...(effort ? { reasoningEffort: effort } : {}) } });
+ const select = effort => runtime.gw.rpc('session/selectModel', { request: { sessionId, provider, model: route.modelId, ...(effort ? { reasoningEffort: effort } : {}) } });
try {
try { await select(level); } catch (e) { if (!level || e.code === 'session/not-found') throw e; await select(null); }
} catch (e) {
@@ -487,7 +502,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
plans.update(chatId, p => { p.state = 'running'; p.goalStarted = true; p.steps[index].state = 'running'; });
const now = plans.get(chatId);
await runtime.gw.rpc('goals/create', { agentId: sessionId, request: { objective: stepObjective(now, index) } });
- written.step = '';
+ written.step = ''; written.stepAt = Date.now();
emit(stepFrame(now, index));
emit(publishPlan('step', now));
const st = now.steps[index];
@@ -501,7 +516,8 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
if (!run || !plan || plan.id !== run.id) return false;
const at = currentStep(plan);
if (at < 0) return false;
- const summary = firstSentence(written.step.split(/\n\s*\n/).filter(Boolean).at(-1) ?? written.step);
+ const cards = [...projector.tools.values()].filter(x => x.calledAt >= (written.stepAt ?? 0) - 50 && x.card?.state === 'done').map(x => x.card);
+ const summary = stepSummary(cards, written.step.split(/\n\s*\n/).filter(Boolean).at(-1) ?? written.step);
plans.update(chatId, p => { p.steps[at].state = 'done'; p.steps[at].summary = summary; });
let now = plans.get(chatId);
emit(stepFrame(now, at));
@@ -1007,7 +1023,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
/** Let go of a chat's Harness session mapping (a task starts every run in a session of its own). */
function forget(chatId) { if (chats.get(chatId)?.turn) return; delete sessionMap[chatId]; chats.delete(chatId); persist(); }
- return { goalState, goalControl, harnessDir, status, runTurn, goalCommand, planCommand, researchCommand, planGoal, planControl, planState, queueMessage, cancelQueued, takeQueue, forget, approve, setMode, stop, pendingFor, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) };
+ return { goalState, goalControl, harnessDir, status, runTurn, goalCommand, planCommand, researchCommand, llmTarget, planGoal, planControl, planState, queueMessage, cancelQueued, takeQueue, forget, approve, setMode, stop, pendingFor, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) };
}
/** A goal that stopped by itself, in words. */
diff --git a/apps/host/server/agent/llm-shape.mjs b/apps/host/server/agent/llm-shape.mjs
new file mode 100644
index 000000000..e0e4f03d5
--- /dev/null
+++ b/apps/host/server/agent/llm-shape.mjs
@@ -0,0 +1,118 @@
+/**
+ * Response settings for agent turns.
+ *
+ * The chat's own path sends the Expert sampling (temperature, top-p, top-k, repeat penalty, seed) and the composer's thinking budget with
+ * every request. The agent's requests are made by the Harness, which has no place for either, so they never reached the model. When a
+ * person has changed one of them, the agent's model route goes through the small forwarding endpoint below (on this computer only), which
+ * adds exactly the fields that differ from the defaults and passes everything else through untouched. With every setting at its default the
+ * agent talks to the model directly, byte for byte as before.
+ *
+ * Only a local model, or an OpenAI-style server on this computer or this network, is shaped: a cloud service would reject fields it does not know.
+ */
+
+import http from 'node:http';
+import https from 'node:https';
+
+/** The thinking budgets the composer offers (0 = no limit is not a budget). */
+export const BUDGETS = Object.freeze([2048, 8192, 32768]);
+/** The offered budget nearest to what a chat asks for (0 stays 0). */
+export function nearestBudget(value) {
+ const n = Number(value);
+ if (!Number.isFinite(n) || n <= 0) return 0;
+ return BUDGETS.reduce((best, b) => (Math.abs(b - n) < Math.abs(best - n) ? b : best), BUDGETS[0]);
+}
+
+/** Expert setting id -> request field, and the default each one has (the catalog's recommended value). */
+const SAMPLING = Object.freeze({
+ 'sample.temp': ['temperature', 0.6],
+ 'sample.topP': ['top_p', 0.95],
+ 'sample.topK': ['top_k', 40],
+ 'sample.repeat': ['repeat_penalty', 1.05],
+ 'sample.seed': ['seed', -1],
+});
+
+/** The request fields for the Expert settings that are not at their default, or null when all are. */
+export function samplingFromExpert(expert) {
+ const out = {};
+ for (const [id, [field, recommended]] of Object.entries(SAMPLING)) {
+ const v = expert?.[id];
+ if (typeof v === 'number' && Number.isFinite(v) && v !== recommended) out[field] = v;
+ }
+ if (expert?.['sample.speedProjection'] === false) out.experimental_speed_projection = false;
+ const cap = Number(expert?.['sample.maxTokens']);
+ if (Number.isFinite(cap) && cap > 0) out.max_tokens = Math.floor(cap);
+ return Object.keys(out).length ? out : null;
+}
+
+/**
+ * The request body with the person's settings added. A field the request already carries is left alone (the Harness's own wins), the
+ * thinking budget is only added while thinking is on, and an output cap only ever lowers the request's own.
+ */
+export function shapeBody(body, { sampling = null, budget = 0 } = {}) {
+ if (!body || typeof body !== 'object' || Array.isArray(body)) return body;
+ const out = { ...body };
+ for (const [field, value] of Object.entries(sampling ?? {})) {
+ if (field === 'max_tokens' || field === 'max_completion_tokens') continue;
+ if (out[field] === undefined) out[field] = value;
+ }
+ if (sampling?.max_tokens) {
+ for (const key of ['max_tokens', 'max_completion_tokens']) if (Number.isFinite(out[key])) out[key] = Math.min(out[key], sampling.max_tokens);
+ if (out.max_tokens === undefined && out.max_completion_tokens === undefined) out.max_tokens = sampling.max_tokens;
+ }
+ const thinkingOff = out.chat_template_kwargs?.enable_thinking === false;
+ if (budget > 0 && !thinkingOff && out.reasoning_budget_tokens === undefined) out.reasoning_budget_tokens = budget;
+ return out;
+}
+
+/** An address on this computer or on a private network: where the person's own model servers live. */
+export function isPrivateHost(url) {
+ let host;
+ try { host = new URL(url).hostname.toLowerCase(); } catch { return false; }
+ host = host.replace(/^\[|\]$/g, '');
+ if (host === 'localhost' || host === '::1' || host.endsWith('.local') || host.endsWith('.lan')) return true;
+ const m = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(host);
+ if (!m) return false;
+ const [a, b] = [Number(m[1]), Number(m[2])];
+ return a === 127 || a === 10 || (a === 192 && b === 168) || (a === 172 && b >= 16 && b <= 31) || (a === 169 && b === 254) || (a === 100 && b >= 64 && b <= 127);
+}
+
+const HOP = new Set(['host', 'connection', 'content-length', 'transfer-encoding', 'keep-alive', 'upgrade', 'proxy-connection']);
+
+/**
+ * The forwarding endpoint. `targetOf(key)` gives the real base address of a shaped route (or null), `samplingNow()` the Expert fields that
+ * differ from the defaults right now. Called for `/api/internal/agent-llm/<key>/<path>`; this computer's own address only.
+ */
+export function createLlmProxy({ targetOf, samplingNow }) {
+ return function handle(req, res, key, rest) {
+ const remote = req.socket?.remoteAddress ?? '';
+ if (!/^(::1|127\.|::ffff:127\.)/.test(remote)) { res.writeHead(403, { 'content-type': 'application/json' }); res.end(JSON.stringify({ error: 'This endpoint is for the assistant on this computer only.' })); return; }
+ const base = targetOf(key);
+ if (!base) { res.writeHead(404, { 'content-type': 'application/json' }); res.end(JSON.stringify({ error: 'Unknown model route.' })); return; }
+ const chunks = [];
+ req.on('data', c => chunks.push(c));
+ req.on('error', () => res.destroy());
+ req.on('end', () => {
+ let body = Buffer.concat(chunks);
+ const budget = nearestBudget(req.headers['x-strata-budget']);
+ if (req.method === 'POST' && /\/chat\/completions\/?$/.test(rest) && body.length) {
+ try { body = Buffer.from(JSON.stringify(shapeBody(JSON.parse(body.toString('utf8')), { sampling: samplingNow(), budget }))); } catch { /* not JSON: pass it on untouched */ }
+ }
+ // The route's address is the model's own base (it ends in /v1): the assistant's /v1 is that same prefix.
+ const target = new URL(base.replace(/\/+$/, '') + rest.replace(/^\/v1(?=\/|$|\?)/, ''));
+ const headers = {};
+ for (const [k, v] of Object.entries(req.headers)) if (!HOP.has(k) && !k.startsWith('x-strata-')) headers[k] = v;
+ headers['content-length'] = String(body.length);
+ const lib = target.protocol === 'https:' ? https : http;
+ const upstream = lib.request(target, { method: req.method, headers }, reply => {
+ const out = { ...reply.headers }; delete out['transfer-encoding']; delete out.connection;
+ res.writeHead(reply.statusCode ?? 502, out);
+ reply.pipe(res);
+ reply.on('error', () => res.destroy());
+ });
+ upstream.on('error', e => { if (!res.headersSent) { res.writeHead(502, { 'content-type': 'application/json' }); res.end(JSON.stringify({ error: `The model server could not be reached: ${e.message}` })); } else res.destroy(); });
+ // The assistant stopped waiting (Stop): stop the model too.
+ res.on('close', () => { if (!res.writableEnded) upstream.destroy(); });
+ upstream.end(body);
+ });
+ };
+}
diff --git a/apps/host/server/agent/plan.mjs b/apps/host/server/agent/plan.mjs
index f2b072473..7e4ee35d1 100644
--- a/apps/host/server/agent/plan.mjs
+++ b/apps/host/server/agent/plan.mjs
@@ -36,6 +36,7 @@ export function planPrompt(task) {
return [
'Make a plan for the request below. Do not change anything: you may only look (read files, list folders, search).',
'When you have looked enough, end your reply with the plan as a numbered list: one short line per step, in the order they should be done.',
+ 'Put a short heading line right before the list, like "## Pagoda page" (two to five words that name the plan).',
'You may add the reason after a dash. Use 3 to 8 steps. Write nothing after the list.',
'',
`Request: ${task}`,
@@ -131,6 +132,24 @@ export function firstSentence(text, max = 140) {
return clip(m ? m[1] : s, max);
}
+/**
+ * The short summary of a finished step: what it changed (from the tool cards: "Wrote pagoda.html, Changed style.css"), else the first
+ * sentence of its closing words when that reads as a result. A sentence about what is NOT done, or about later steps, is never a summary.
+ */
+export function stepSummary(cards, closing) {
+ const done = [];
+ for (const c of cards ?? []) {
+ const verb = /^(Wrote|Changed|Created|Edited|Deleted|Removed|Moved|Renamed)\b/.exec(String(c?.verb ?? ''))?.[1];
+ const target = String(c?.target ?? '').split(/[\\/]/).filter(Boolean).at(-1);
+ if (!verb || !target) continue;
+ const line = verb + ' ' + clip(target, 40);
+ if (!done.includes(line)) done.push(line);
+ }
+ if (done.length) return clip(done.slice(0, 3).join(', ') + (done.length > 3 ? ' and more' : ''), 140);
+ const s = firstSentence(closing);
+ return s && !/not done|not yet|remaining|handled separately|next steps?|later steps?|still (?:need|to)|todo/i.test(s) ? s : null;
+}
+
/** The words a person sees for the plan, as one message the model can carry out (the plain Approve path). */
export function planAsPrompt(plan) {
return ['Carry out this plan, step by step, and tell me briefly when each step is done.', '', plan.title ? `Plan: ${plan.title}` : '', ...plan.steps.map((s, i) => `${i + 1}. ${s.text}${s.detail ? ` (${s.detail})` : ''}`)].filter((l, i) => l !== '' || i === 1).join('\n');
diff --git a/apps/host/server/agent/profile.mjs b/apps/host/server/agent/profile.mjs
index b77722244..4afda2c58 100644
--- a/apps/host/server/agent/profile.mjs
+++ b/apps/host/server/agent/profile.mjs
@@ -20,6 +20,7 @@
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { standardPreset } from './preset.mjs';
+import { BUDGETS, isPrivateHost } from './llm-shape.mjs';
import { DDG_PROVIDER_ID } from '../web-search-ddg.mjs';
const PROTOCOL = { 'openai-chat': 'openai-completions', 'openai-responses': 'openai-responses', anthropic: 'anthropic-messages' };
@@ -150,7 +151,7 @@ export function outputBudget(context, maxOutput = null) {
* @param {Array<{id:string,name:string,baseUrl:string,protocol:string,apiKey?:string,models:Array<{id:string,name?:string,contextMax?:number|null}>}>} input.connections
* @returns {{patch:string, env:Record<string,string>, routes:Array, defaultRoute:object|null, signature:string}}
*/
-export function buildProfile({ local = [], connections = [], gate = true, tools = NO_TOOLS_SETTINGS, skillsDir = null }) {
+export function buildProfile({ local = [], connections = [], gate = true, tools = NO_TOOLS_SETTINGS, skillsDir = null, proxyBase = null }) {
const providers = {};
const env = {};
const routes = [];
@@ -159,6 +160,20 @@ export function buildProfile({ local = [], connections = [], gate = true, tools
let slots = 0;
/** Per-model compaction timing (see compactionHeadroom). */
const compaction = [];
+ /** Routes the person's Response settings can reach: a copy of the provider whose address is this host's forwarding endpoint (see llm-shape.mjs). */
+ const shapeTargets = {};
+ function addShaped(route, def, realBase) {
+ if (!proxyBase) return null;
+ const keys = {};
+ for (const budget of [0, ...BUDGETS]) {
+ const key = `${route}~s${budget}`;
+ providers[key] = { ...def, displayName: `${def.displayName} (with your response settings)`, baseURL: `${proxyBase}/${key}/v1`, ...(budget ? { headers: { ...(def.headers ?? {}), 'x-strata-budget': String(budget) } } : {}) };
+ shapeTargets[key] = realBase;
+ for (const m of def.models) { const w = m.contextWindow, out = m.maxTokens; compaction.push({ provider: key, model: m.id, headroomTokens: compactionHeadroom(w, out) }); }
+ keys[budget] = key;
+ }
+ return keys;
+ }
local.forEach((inst, index) => {
const route = index === 0 ? 'strata-local' : `strata-local-${slug(inst.id)}`;
@@ -183,7 +198,8 @@ export function buildProfile({ local = [], connections = [], gate = true, tools
};
compaction.push({ provider: route, model: inst.modelId, headroomTokens: compactionHeadroom(context, localOutputBudget(context)) });
slots = Math.max(slots, Number.isFinite(inst.slots) && inst.slots >= 1 ? Math.floor(inst.slots) : 1);
- routes.push({ route, fp: fingerprint(providers[route], env[keyEnv]), kind: 'local', instanceId: inst.id, modelId: inst.modelId, name: inst.label || inst.modelId, contextWindow: context });
+ const shaped = addShaped(route, providers[route], inst.baseUrl);
+ routes.push({ route, fp: fingerprint(providers[route], env[keyEnv]), kind: 'local', instanceId: inst.id, modelId: inst.modelId, name: inst.label || inst.modelId, contextWindow: context, ...(shaped ? { shaped } : {}) });
});
connections.forEach(conn => {
@@ -210,8 +226,9 @@ export function buildProfile({ local = [], connections = [], gate = true, tools
})),
};
const fp = fingerprint(providers[route], conn.apiKey ?? '');
+ const shaped = protocol === 'openai-completions' && isPrivateHost(baseURL) ? addShaped(route, providers[route], baseURL) : null;
for (const m of conn.models) { const ctx = m.contextMax ?? ASSUMED_API_CONTEXT; compaction.push({ provider: route, model: m.id, headroomTokens: compactionHeadroom(ctx, outputBudget(ctx, m.maxOutput)) }); }
- for (const m of conn.models) routes.push({ route, fp, kind: 'api', connectionId: conn.id, modelId: m.id, name: m.name || m.id, contextWindow: m.contextMax ?? ASSUMED_API_CONTEXT });
+ for (const m of conn.models) routes.push({ route, fp, kind: 'api', connectionId: conn.id, modelId: m.id, name: m.name || m.id, contextWindow: m.contextMax ?? ASSUMED_API_CONTEXT, ...(shaped ? { shaped } : {}) });
});
const defaultRoute = routes.find(r => r.kind === 'local') ?? null;
@@ -260,7 +277,7 @@ export function buildProfile({ local = [], connections = [], gate = true, tools
// What the agent is offered (tool groups, web search, tool servers, the person's own tools) decides whether a running agent must be
// restarted for the next message; the routes are compared separately (a changed context size alone does not restart it).
const toolsSignature = createHash('sha256').update(JSON.stringify([mcp, ownRows, webRows, entries.find(e => e.id === 'preset-standard'), env.STRATA_SEARCH_KEY ?? '', mcpEnv])).digest('hex').slice(0, 16);
- return { patch, env, routes, defaultRoute, signature, toolsSignature };
+ return { patch, env, routes, defaultRoute, signature, toolsSignature, shapeTargets };
}
/** Find the route for a chat's model choice. */
diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjs
index 39144594e..412803324 100644
--- a/apps/host/server/index.mjs
+++ b/apps/host/server/index.mjs
@@ -50,6 +50,7 @@ import { EventHub, error, json, readBody, uid } from './util.mjs';
import { RequestQueue } from './request-queue.mjs';
import { selectSkills, skillInstructions } from './skill-selection.mjs';
import { createModelProviders } from './model-providers.mjs';
+import { createLlmProxy, samplingFromExpert } from './agent/llm-shape.mjs';
import { checkUpdates, checkComponentUpdates } from './updates.mjs';
import { createAppUpdater } from './app-updater.mjs';
import { createImageModels } from './image-models.mjs';
@@ -110,12 +111,16 @@ const native=createNativeControl({control,appUpdater,hardwareProbe:()=>(hardware
pinned component (STRATA_HARNESS_DIR), not part of this host, so its absence is a visible capability
state and the chat falls back to the direct path with a notice, never silently to another model. */
// What the person chose in Settings -> Tools (tool groups, web search service and key, the browser, their own tools).
+const llmProxy = createLlmProxy({ targetOf: key => agent.llmTarget(key), samplingNow: () => samplingFromExpert(store.get().expert) });
const agentTools = createAgentTools({ read: () => store.get().agentTools, write: value => store.update({ agentTools: value }), dataDir: store.DATA_DIR, browser: () => ({ ...findBrowser(), cli: findPlaywrightMcp(agent.harnessDir()) }) });
agentTools.ensureFile();
const agent = createAgentBridge({
tools: () => agentTools.profileView(),
skillsDir: () => AGENT_SKILLS,
onGoal: (chatId, goal) => hub.send('goal', { chatId, goal }),
+ // The person's Response settings (thinking budget, Expert sampling) reach the agent's model through this host's own forwarding endpoint.
+ proxyBase: `http://127.0.0.1:${PORT}/api/internal/agent-llm`,
+ expertSampling: () => samplingFromExpert(store.get().expert),
onPlan: (chatId, event, plan) => hub.send('plan', { chatId, event, plan }),
onQueue: (chatId, queue) => hub.send('queue', { chatId, queue }),
dataDir: store.DATA_DIR,
@@ -525,7 +530,7 @@ async function runAgentTurn({ res, controller, chatId, body, message, workspaceI
const goalLine = /^\/goal(?:\s+([\s\S]*))?$/i.exec(message.trim());
const planLine = /^\/plan(?:\s+([\s\S]*))?$/i.exec(message.trim());
const researchLine = /^\/research(?:\s+([\s\S]*))?$/i.exec(message.trim());
- const turnOptions = { chatId, message, workspacePath: cwd, choice, mode: typeof body.mode === 'string' ? body.mode : null, thinking: typeof body.thinking === 'string' ? body.thinking : null, history: Array.isArray(body.history) ? body.history : [], onFrame: write, signal: controller.signal };
+ const turnOptions = { chatId, message, workspacePath: cwd, choice, mode: typeof body.mode === 'string' ? body.mode : null, thinking: typeof body.thinking === 'string' ? body.thinking : null, thinkingBudget: Number.isFinite(body.thinkingBudget) ? body.thinkingBudget : 0, history: Array.isArray(body.history) ? body.history : [], onFrame: write, signal: controller.signal };
const result = typeof body.planId === 'string' && body.message === '/plan start' ? await agent.planGoal({ ...turnOptions, planId: body.planId })
: researchLine ? await agent.researchCommand({ ...turnOptions, line: researchLine[1] ?? '' })
: planLine ? await agent.planCommand({ ...turnOptions, line: planLine[1] ?? '' })
@@ -1605,6 +1610,9 @@ async function api(req, res, path, method, url) {
// --- agent --------------------------------------------------------------
if (path === '/api/agent/status' && method === 'GET') return json(res, 200, agent.status());
+ // The assistant's model requests, with the person's Response settings added (llm-shape.mjs). This computer only.
+ const llmRoute = /^\/api\/internal\/agent-llm\/([A-Za-z0-9_.~-]+)(\/.*)$/.exec(path);
+ if (llmRoute) { llmProxy(req, res, llmRoute[1], llmRoute[2] + (url.search ?? '')); return undefined; }
// The goal bar (notes-split-agent3.md): the state, and the instant actions. Resume runs rounds, so it is a chat stream with {goalAction:'resume'}.
if (path === '/api/goal' && method === 'GET') {
try { return json(res, 200, { goal: await agent.goalState(url.searchParams.get('chatId') ?? '') }); } catch (e) { return error(res, 400, e.message); }
diff --git a/apps/host/tests/agent-plan.test.mjs b/apps/host/tests/agent-plan.test.mjs
index 85b76f9a2..2ce90ab3b 100644
--- a/apps/host/tests/agent-plan.test.mjs
+++ b/apps/host/tests/agent-plan.test.mjs
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
-import { parsePlan, parsePoints, stepObjective, stepLine, firstSentence, editedSteps, planAsPrompt, PlanStore, planPrompt } from '../server/agent/plan.mjs';
+import { stepSummary, parsePlan, parsePoints, stepObjective, stepLine, firstSentence, editedSteps, planAsPrompt, PlanStore, planPrompt } from '../server/agent/plan.mjs';
test('a plan is the last numbered list of the reply, with reasons after a dash and indented detail kept', () => {
const p = parsePlan([
@@ -58,6 +58,13 @@ test('a step goal names its step, what is done and what is not its job', () => {
assert.equal(firstSentence(' '), null);
});
+test('a step summary says what it changed, and is empty rather than wrong', () => {
+ assert.equal(stepSummary([{ verb: 'Wrote a file', target: 'C:\\work\\pagoda.html' }, { verb: 'Changed a file', target: 'style.css' }, { verb: 'Read a file', target: 'a.txt' }, { verb: 'Wrote a file', target: 'pagoda.html' }], 'Done.'), 'Wrote pagoda.html, Changed style.css');
+ assert.equal(stepSummary([], 'The roof is tiled and checked. Next I will glow.'), 'The roof is tiled and checked.');
+ assert.equal(stepSummary([], 'Not done yet (these are the remaining steps, which will be handled separately): the CSS.'), null);
+ assert.equal(stepSummary([], ''), null);
+});
+
test('an edit keeps order, drops empty lines and refuses an empty plan', () => {
const steps = editedSteps([' one ', '', { text: 'two', detail: 'why' }, { text: ' ' }]);
assert.deepEqual(steps.map(s => [s.id, s.text, s.detail]), [['s1', 'one', ''], ['s2', 'two', 'why']]);
@@ -79,10 +86,11 @@ test('plans survive a restart', () => {
} finally { rmSync(dir, { recursive: true, force: true }); }
});
-import { parseSources, researchPrompt } from '../server/agent/plan.mjs';
+import { parseSources, researchPrompt, researchWritePrompt } from '../server/agent/plan.mjs';
test('research: the answer shape is asked for, and the sources are read back as [{n, title, url}]', () => {
- assert.ok(researchPrompt('heat pumps').includes('web_search') && researchPrompt('heat pumps').includes('## Sources'));
+ assert.ok(researchPrompt('heat pumps').includes('web_search') && researchPrompt('heat pumps').includes('web_fetch'));
+ assert.ok(researchWritePrompt('heat pumps').includes('## Sources') && researchWritePrompt('heat pumps').includes('## Summary'));
const answer = [
'## Summary', 'Heat pumps move heat.', '## Key findings', '- They are efficient [1].', '- They cost more up front [2].',
'## Sources',
diff --git a/apps/host/tests/agent-shape.test.mjs b/apps/host/tests/agent-shape.test.mjs
new file mode 100644
index 000000000..b97e6826b
--- /dev/null
+++ b/apps/host/tests/agent-shape.test.mjs
@@ -0,0 +1,120 @@
+// Response settings (thinking effort, thinking budget, Expert sampling) reach the agent's model request, and nothing changes while they are at their defaults.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import http from 'node:http';
+import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { BUDGETS, createLlmProxy, isPrivateHost, nearestBudget, samplingFromExpert, shapeBody } from '../server/agent/llm-shape.mjs';
+import { buildProfile } from '../server/agent/profile.mjs';
+import { startScriptedWorker } from './helpers/scripted-worker.mjs';
+import { createAgentBridge } from '../server/agent/agent-bridge.mjs';
+import { resolveHarness } from '../server/agent/harness-runtime.mjs';
+
+const HARNESS = process.env.STRATA_HARNESS_DIR;
+const skip = !HARNESS || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false;
+
+test('only the Expert settings that differ from their defaults are sent; the thinking budget only while thinking is on', () => {
+ assert.equal(samplingFromExpert({}), null);
+ assert.equal(samplingFromExpert({ 'sample.temp': 0.6, 'sample.topP': 0.95, 'sample.topK': 40, 'sample.repeat': 1.05, 'sample.seed': -1, 'sample.speedProjection': true, 'sample.maxTokens': 0 }), null, 'all defaults: nothing');
+ assert.deepEqual(samplingFromExpert({ 'sample.temp': 0.2, 'sample.topK': 10, 'sample.seed': 7, 'sample.maxTokens': 500, 'sample.speedProjection': false }),
+ { temperature: 0.2, top_k: 10, seed: 7, max_tokens: 500, experimental_speed_projection: false });
+ const body = { model: 'm', messages: [], max_tokens: 4096, chat_template_kwargs: { enable_thinking: true, reasoning_effort: 'high' } };
+ assert.deepEqual(shapeBody(body, {}), body, 'no settings: the request is the same');
+ const shaped = shapeBody(body, { sampling: { temperature: 0.2, max_tokens: 500 }, budget: 8192 });
+ assert.equal(shaped.temperature, 0.2); assert.equal(shaped.max_tokens, 500, 'an output cap only lowers the request'); assert.equal(shaped.reasoning_budget_tokens, 8192);
+ assert.equal(shapeBody({ ...body, max_tokens: 100 }, { sampling: { max_tokens: 500 } }).max_tokens, 100);
+ assert.equal(shapeBody({ ...body, temperature: 1 }, { sampling: { temperature: 0.2 } }).temperature, 1, 'a field the request already has wins');
+ assert.equal(shapeBody({ ...body, chat_template_kwargs: { enable_thinking: false } }, { budget: 8192 }).reasoning_budget_tokens, undefined, 'thinking off: no budget');
+ assert.deepEqual(BUDGETS, [2048, 8192, 32768]); assert.equal(nearestBudget(0), 0); assert.equal(nearestBudget(5000), 2048); assert.equal(nearestBudget(7000), 8192); assert.equal(nearestBudget('32768'), 32768);
+});
+
+test('only this computer and private networks are shaped, never a cloud address', () => {
+ for (const ok of ['http://127.0.0.1:8080/v1', 'http://localhost:11434/v1', 'http://192.168.1.20:8080', 'http://10.0.0.5/v1', 'http://172.20.1.1/v1', 'http://z440.local:8080', 'http://[::1]:9/v1']) assert.equal(isPrivateHost(ok), true, ok);
+ for (const no of ['https://api.openai.com/v1', 'http://8.8.8.8/v1', 'http://172.32.0.1/v1', 'http://example.com', 'nonsense']) assert.equal(isPrivateHost(no), false, no);
+});
+
+test('the profile adds a response-settings copy of each shapeable route (local, or an OpenAI-style server on this network) and never of a cloud route', () => {
+ const profile = buildProfile({
+ local: [{ id: 'a', modelId: 'm', baseUrl: 'http://127.0.0.1:9001', context: 65536 }],
+ connections: [
+ { id: 'lan', name: 'LAN', baseUrl: 'http://192.168.1.9:8080/v1', protocol: 'openai-chat', apiKey: '', models: [{ id: 'x' }] },
+ { id: 'cloud', name: 'Cloud', baseUrl: 'https://api.deepseek.com', protocol: 'openai-chat', apiKey: 'k', models: [{ id: 'y' }] },
+ ],
+ proxyBase: 'http://127.0.0.1:8791/api/internal/agent-llm',
+ });
+ const local = profile.routes.find(r => r.kind === 'local');
+ assert.deepEqual(Object.keys(local.shaped), ['0', '2048', '8192', '32768']);
+ assert.equal(local.shaped[8192], 'strata-local~s8192');
+ assert.deepEqual(Object.keys(profile.shapeTargets).sort(), [...Object.values(local.shaped), ...Object.values(profile.routes.find(r => r.connectionId === 'lan').shaped)].sort());
+ assert.equal(profile.shapeTargets['strata-local~s0'], 'http://127.0.0.1:9001');
+ assert.equal(profile.routes.find(r => r.connectionId === 'cloud').shaped, undefined);
+ assert.match(profile.patch, /"baseURL": "http:\/\/127\.0\.0\.1:8791\/api\/internal\/agent-llm\/strata-local~s8192\/v1"/);
+ assert.match(profile.patch, /"x-strata-budget": "8192"/);
+ assert.match(profile.patch, /"provider": "strata-local~s8192"/, 'compaction keeps its timing on the copy too');
+ const plain = buildProfile({ local: [{ id: 'a', modelId: 'm', baseUrl: 'http://127.0.0.1:9001', context: 65536 }], connections: [] });
+ assert.equal(plain.routes[0].shaped, undefined); assert.ok(!/~s/.test(plain.patch), 'without a forwarding address nothing changes');
+});
+
+test('the forwarding endpoint adds the settings, streams the reply back and passes the key through', async (t) => {
+ const seen = [];
+ const upstream = http.createServer((req, res) => {
+ let raw = ''; req.on('data', c => { raw += c; }); req.on('end', () => {
+ seen.push({ url: req.url, auth: req.headers.authorization, internal: req.headers['x-strata-budget'], body: raw ? JSON.parse(raw) : null });
+ res.writeHead(200, { 'content-type': 'text/event-stream' }); res.write('data: one\n\n'); setTimeout(() => { res.write('data: two\n\n'); res.end(); }, 30);
+ });
+ });
+ await new Promise(r => upstream.listen(0, '127.0.0.1', r));
+ let expert = null;
+ const proxy = createLlmProxy({ targetOf: key => (key === 'k1' ? `http://127.0.0.1:${upstream.address().port}/v1` : null), samplingNow: () => expert });
+ const front = http.createServer((req, res) => { const m = /^\/api\/internal\/agent-llm\/([^/]+)(\/.*)$/.exec(req.url); proxy(req, res, m[1], m[2]); });
+ await new Promise(r => front.listen(0, '127.0.0.1', r));
+ t.after(() => { front.close(); upstream.close(); });
+ const call = (key, headers = {}) => new Promise((resolve, reject) => {
+ const r = http.request({ host: '127.0.0.1', port: front.address().port, path: `/api/internal/agent-llm/${key}/v1/chat/completions`, method: 'POST', headers: { 'content-type': 'application/json', authorization: 'Bearer abc', ...headers } }, res => { let t2 = ''; res.on('data', c => { t2 += c; }); res.on('end', () => resolve({ status: res.statusCode, text: t2 })); });
+ r.on('error', reject); r.end(JSON.stringify({ model: 'm', stream: true, messages: [], max_tokens: 100 }));
+ });
+ const a = await call('k1');
+ assert.equal(a.status, 200); assert.match(a.text, /one[\s\S]*two/);
+ assert.deepEqual(seen[0].body, { model: 'm', stream: true, messages: [], max_tokens: 100 }, 'defaults: the body is untouched'); assert.equal(seen[0].auth, 'Bearer abc'); assert.equal(seen[0].url, '/v1/chat/completions'); assert.equal(seen[0].internal, undefined, 'our own header is not forwarded');
+ expert = { temperature: 0.1 };
+ await call('k1', { 'x-strata-budget': '8192' });
+ assert.equal(seen[1].body.temperature, 0.1); assert.equal(seen[1].body.reasoning_budget_tokens, 8192);
+ assert.equal((await call('nope')).status, 404);
+});
+
+test('a turn uses the response-settings route only when a setting is changed: the model receives the sampling and the budget; defaults go straight to the model', { skip }, async (t) => {
+ const worker = await startScriptedWorker({});
+ const dir = mkdtempSync(join(tmpdir(), 'strata-shape-'));
+ const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });
+ let expert = null; let front; let bridge;
+ const proxy = createLlmProxy({ targetOf: key => bridge.llmTarget(key), samplingNow: () => expert });
+ front = http.createServer((req, res) => { const m = /^\/api\/internal\/agent-llm\/([^/]+)(\/.*)$/.exec(req.url); proxy(req, res, m[1], m[2]); });
+ await new Promise(r => front.listen(0, '127.0.0.1', r));
+ bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'S', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots: 2 }], connections: () => [], env: process.env, startTimeoutMs: 90_000,
+ proxyBase: `http://127.0.0.1:${front.address().port}/api/internal/agent-llm`, expertSampling: () => expert });
+ t.after(async () => { await bridge.close(); await worker.close(); front.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } });
+ const run = (chatId, extra = {}) => bridge.runTurn({ chatId, message: 'SCRIPT:echo hello', workspacePath: ws, mode: 'bypass', onFrame: () => {}, ...extra });
+ const last = () => worker.requests.filter(r => r.directive === 'echo').at(-1).params;
+
+ await run('s1', { thinking: 'High' });
+ assert.equal(last().temperature, undefined, 'defaults: no sampling sent'); assert.equal(last().reasoning_budget_tokens, undefined);
+ assert.equal(last().chat_template_kwargs.reasoning_effort, 'high', 'the reasoning level reaches the request');
+ await run('s2', { thinking: 'Low' });
+ assert.equal(last().chat_template_kwargs.reasoning_effort, 'low');
+ await run('s3', { thinking: 'Off' });
+ assert.equal(last().chat_template_kwargs.enable_thinking, false, 'Off turns thinking off');
+ await run('s4', { thinking: 'High', thinkingBudget: 8192 });
+ assert.equal(last().reasoning_budget_tokens, 8192, 'the composer budget reaches the request');
+ assert.equal(last().chat_template_kwargs.reasoning_effort, 'high');
+ await run('s5', { thinking: 'Off', thinkingBudget: 8192 });
+ assert.equal(last().reasoning_budget_tokens, undefined, 'a budget is not sent while thinking is off');
+ expert = { temperature: 0.2, top_k: 12, seed: 5 };
+ await run('s6', { thinking: 'Medium' });
+ assert.equal(last().temperature, 0.2); assert.equal(last().top_k, 12); assert.equal(last().seed, 5); assert.equal(last().reasoning_budget_tokens, undefined);
+ await run('s7', { thinking: 'Medium', thinkingBudget: 2048 });
+ assert.equal(last().temperature, 0.2); assert.equal(last().reasoning_budget_tokens, 2048);
+ expert = null;
+ await run('s8', { thinking: 'Medium' });
+ assert.equal(last().temperature, undefined, 'back to defaults: straight to the model again');
+});