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++
Local models compact automatically: reply room is half the window and compaction headroom fits the window
Niko1221c6a86ce168e2
diff --git a/apps/host/server/agent/preset.mjs b/apps/host/server/agent/preset.mjsindex bd36e68c0..40ebbfdb0 100644--- a/apps/host/server/agent/preset.mjs+++ b/apps/host/server/agent/preset.mjs@@ -50,7 +50,7 @@ export const AGENT_PROMPT = [* @param {{platform?: string, search?: boolean}} [options]* @returns {object} the `config` of the `preset-standard` row*/-export function standardPreset({ platform = process.platform, search = false, off = new Set(), skillsDir = null } = {}) {+export function standardPreset({ platform = process.platform, search = false, off = new Set(), skillsDir = null, compaction = [] } = {}) {const windows = platform === 'win32';const plugins = [{ id: 'persona', name: '@deepseek-ai/dsh-persona', config: { prefix: AGENT_PROMPT, complete: true, includeRuntimeContext: false } },@@ -70,7 +70,7 @@ export function standardPreset({ platform = process.platform, search = false, of{id: 'compaction', name: 'cordis:group', group: true, isolate: { compaction: true, toolResultPruner: true },config: [- { id: 'compaction-basic', name: '@deepseek-ai/dsh-compaction-basic' },+ { id: 'compaction-basic', name: '@deepseek-ai/dsh-compaction-basic', ...(compaction.length ? { config: { modelPolicies: compaction } } : {}) },{ id: 'command-compact', name: '@deepseek-ai/dsh-command-compact' },{ id: 'tool-result-pruner', name: '@deepseek-ai/dsh-compaction-tool-result-pruner', config: { thresholdChars: 8192, headChars: 4096, tailChars: 1024 } },],diff --git a/apps/host/server/agent/profile.mjs b/apps/host/server/agent/profile.mjsindex 3d076838d..b77722244 100644--- a/apps/host/server/agent/profile.mjs+++ b/apps/host/server/agent/profile.mjs@@ -104,7 +104,20 @@ function fingerprint(provider, secret) {*/export function localOutputBudget(context) {const c = Number.isFinite(context) && context > 0 ? context : 32768;- return Math.max(8192, c - 4096);+ // Half the window, at most 32K: still far more than the old 8192 that cut big answers off, and it leaves the other half for the+ // conversation. The Harness's automatic compaction reads this number as the room the reply reserves: with nearly the whole window+ // reserved it had no room left to measure pressure in, and a long chat or a long helper task never compacted.+ return Math.max(4096, Math.min(32768, Math.floor(c / 2)));+}++/**+ * When the Harness's automatic compaction starts condensing old history, for one model: its own default keeps 64K tokens of headroom,+ * which on a local window of 32K to 128K leaves no room at all (it then reports a configuration error once and never compacts). A quarter+ * of what the window has left after the reply's room works from 16K up to the largest windows. Compaction then starts at about 70 percent.+ */+export function compactionHeadroom(context, outputBudget) {+ const room = Math.max(0, (Number.isFinite(context) && context > 0 ? context : 32768) - outputBudget);+ return Math.max(512, Math.min(65536, Math.floor(room * 0.25)));}/**@@ -144,6 +157,8 @@ export function buildProfile({ local = [], connections = [], gate = true, toolsconst mcpEnv = {};// Helper agents: at least as many as the engine has request slots (a local model); API models take the Harness's own limit.let slots = 0;+ /** Per-model compaction timing (see compactionHeadroom). */+ const compaction = [];local.forEach((inst, index) => {const route = index === 0 ? 'strata-local' : `strata-local-${slug(inst.id)}`;@@ -166,6 +181,7 @@ export function buildProfile({ local = [], connections = [], gate = true, toolsreasoning: true, reasoningEfforts: LOCAL_EFFORTS,}],};+ 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 });});@@ -194,6 +210,7 @@ export function buildProfile({ local = [], connections = [], gate = true, tools})),};const fp = fingerprint(providers[route], conn.apiKey ?? '');+ 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 });});@@ -226,7 +243,7 @@ export function buildProfile({ local = [], connections = [], gate = true, tools...(mcp.length || ownRows.length ? [{ insert: [...mcp, ...ownRows] }] : []),...webRows,// The reminder tools would be a second scheduler beside the app's Tasks. The tool groups the person switched off in Settings -> Tools are left out.- { id: 'preset-standard', name: '@deepseek-ai/dsh-agent-preset', config: standardPreset({ off: tools.off, search: searchOn, skillsDir }) },+ { id: 'preset-standard', name: '@deepseek-ai/dsh-agent-preset', config: standardPreset({ off: tools.off, search: searchOn, skillsDir, compaction }) },// Strata names its chats itself; the Harness's own title request would be an extra model call on// every first prompt (and, for an API route, an unrequested one).{ id: 'session-title-llm', disabled: true },diff --git a/apps/host/tests/agent-compaction.test.mjs b/apps/host/tests/agent-compaction.test.mjsnew file mode 100644index 000000000..e6c3cd5b3--- /dev/null+++ b/apps/host/tests/agent-compaction.test.mjs@@ -0,0 +1,51 @@+// Automatic compaction for a LOCAL model: it starts before the window is full, the chat goes on after it, and helpers get it too.+import test from 'node:test';+import assert from 'node:assert/strict';+import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { startScriptedWorker } from './helpers/scripted-worker.mjs';+import { createAgentBridge } from '../server/agent/agent-bridge.mjs';+import { resolveHarness } from '../server/agent/harness-runtime.mjs';+import { buildProfile, compactionHeadroom, localOutputBudget } from '../server/agent/profile.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('the reply keeps half the window (at most 32K) and compaction gets a headroom that fits, for every window size', () => {+ assert.equal(localOutputBudget(32768), 16384); assert.equal(localOutputBudget(262144), 32768); assert.equal(localOutputBudget(8192), 4096); assert.equal(localOutputBudget(undefined), 16384);+ for (const w of [8192, 16384, 32768, 65536, 131072, 262144, 1_000_000]) {+ const out = localOutputBudget(w), head = compactionHeadroom(w, out);+ const threshold = Math.floor(Math.min(w * 0.8, w - out - head));+ assert.ok(threshold > 0, `window ${w}: a positive start point (${threshold})`);+ assert.ok(threshold >= (w - out) * 0.5, `window ${w}: compaction starts after at least half of the room the conversation has (${threshold})`);+ assert.ok(Math.floor((w - out) * 0.16) < threshold, `window ${w}: the verbatim tail is smaller than the start point`);+ }+ const profile = buildProfile({ local: [{ id: 'a', modelId: 'm', baseUrl: 'http://x', context: 32768 }], connections: [] });+ assert.match(profile.patch, /"modelPolicies": \[\s*\{\s*"provider": "strata-local",\s*"model": "m",\s*"headroomTokens": 4096/);+});++test('a long chat on a small window compacts before it overflows, and the conversation goes on', { skip }, async (t) => {+ const worker = await startScriptedWorker({});+ const dir = mkdtempSync(join(tmpdir(), 'strata-compact-'));+ const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });+ const bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'S', baseUrl: worker.url, modelId: 'scripted-model', context: 16384, slots: 2 }], connections: () => [], env: process.env, startTimeoutMs: 90_000 });+ t.after(async () => { await bridge.close(); await worker.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } });+ const big = 'alpha '.repeat(1500);+ const replies = [];+ for (let i = 0; i < 6; i++) {+ const frames = [];+ await bridge.runTurn({ chatId: 'long', message: `SCRIPT:echo round${i} ${big}`, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) });+ replies.push(frames.filter(f => f.type === 'delta').map(f => f.text).join('').slice(0, 12));+ assert.equal(frames.at(-1).type, 'done', `round ${i} ended normally`);+ }+ assert.ok(replies.every(r => /^round\d/.test(r)), 'every round was answered: ' + replies.join(' | '));+ const reqs = worker.requests;+ const summaries = reqs.filter(r => r.summary);+ assert.ok(summaries.length >= 1, 'the Harness asked for a summary of the old history');+ const answers = reqs.filter(r => !r.summary && r.toolNames.length);+ const peak = Math.max(...answers.map(r => r.promptChars));+ assert.ok(peak < 16384 * 4, `no request was bigger than the window (about ${Math.round(peak / 4)} tokens of 16384)`);+ const lastRoundPrompt = answers.at(-1).promptChars;+ assert.ok(lastRoundPrompt < answers[Math.min(4, answers.length - 1)].promptChars + 1, 'the prompt stopped growing');+});diff --git a/apps/host/tests/agent-events.test.mjs b/apps/host/tests/agent-events.test.mjsindex 71ad09837..401fc412c 100644--- a/apps/host/tests/agent-events.test.mjs+++ b/apps/host/tests/agent-events.test.mjs@@ -18,7 +18,7 @@ test('the generated profile names credentials, never carries them, and declaresconst providers = entries.find(e => e.id === 'llm-pi-ai').config.providers;assert.deepEqual(Object.keys(providers).sort(), ['api-c0nn-1234-ab', 'strata-local']);assert.equal(providers['strata-local'].models[0].contextWindow, 32768);- assert.equal(providers['strata-local'].models[0].maxTokens, 28672, 'a local model may use nearly the whole window (it was 8192, which cut big answers off)');+ assert.equal(providers['strata-local'].models[0].maxTokens, 16384, 'a local model gets half its window for a reply (it was 8192, which cut big answers off) and the rest stays for the conversation, so the Harness can compact it');assert.equal(providers['strata-local'].models[0].reasoning, true);assert.equal(providers['strata-local'].compat.thinkingFormat, 'chat-template');assert.equal(providers['api-c0nn-1234-ab'].models[1].contextWindow, ASSUMED_API_CONTEXT, 'an unknown API window is a stated assumption, not a catalog default');diff --git a/apps/host/tests/helpers/scripted-worker.mjs b/apps/host/tests/helpers/scripted-worker.mjsindex c3a15b104..0ff247bf0 100644--- a/apps/host/tests/helpers/scripted-worker.mjs+++ b/apps/host/tests/helpers/scripted-worker.mjs@@ -123,6 +123,8 @@ export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model =return say(['Tool finished. ', `Result: ${result}`]);}const [name, args] = [directive?.[1], (directive?.[2] ?? '').trim()];+ // The Harness's compaction request (its summarizer prompt asks for the person's evolving goals): answer with a short summary.+ if (messages.some(m => /evolving goals/.test(text(m)))) { requests.at(-1).summary = true; return say(['Summary of the earlier conversation: the person asked for echoes of a word and got them.']); }// The Harness's session-title request carries the same user text but offers no tools.if (!(body.tools ?? []).length && messages.some(m => /concise title/i.test(text(m)))) return say(['Scripted title']);if (name === 'slow') {