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++

The chat checks a message against the model's window before sending it, handles /compact itself, and explains a full context in plain words

Niko1221b14862351bc6
diff --git a/apps/host/scripts/context-e2e.mjs b/apps/host/scripts/context-e2e.mjs
new file mode 100644
index 000000000..2696f8880
--- /dev/null
+++ b/apps/host/scripts/context-e2e.mjs
@@ -0,0 +1,181 @@
+#!/usr/bin/env node
+/**
+ * The context fixes against a REAL engine (not the scripted worker): Gyro-S at 64K on this PC, the agent bridge and the real Harness.
+ *
+ * gpu + heavy locks held first (python phaseA-tests\lock.py acquire gpu "023-agent: context e2e", then heavy); one engine; nothing of the
+ * installed app is used or touched: its model files are read, its Harness is copied (STRATA_HARNESS_DIR), data goes to a scratch folder.
+ *
+ * node scripts/context-e2e.mjs (needs STRATA_HARNESS_DIR; see the E2E_* names below)
+ *
+ * It starts serve/server.py as ITS OWN child (foreground: this script waits for it and stops it by PID at the end or on a memory alarm),
+ * runs four scenarios and prints one table. Exit code 0 when every expectation held.
+ *
+ * A a message that can never fit (about 60K tokens in a 64K window) is refused in seconds, before the engine reads it
+ * B a chat of dense log messages grows past the window several times over: automatic compaction runs, nothing is refused
+ * C /compact at a high fill: summarises, says the figures; and a second /compact is answered in plain words
+ * D the needle: a fact from the first message is asked about at the end (reported, not asserted: it is the summary's quality)
+ */
+import { spawn, spawnSync } from 'node:child_process';
+import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { createAgentBridge } from '../server/agent/agent-bridge.mjs';
+import { countTokens } from '../server/agent/context-fit.mjs';
+
+const REPO = join(import.meta.dirname, '..', '..', '..');
+const env = (name, fallback) => process.env[name] ?? fallback;
+const DOCS = env('E2E_DOCS', 'C:\\Users\\AI-Server\\Documents\\Strata');
+const ENGINE = env('E2E_ENGINE', 'C:\\Users\\AI-Server\\Desktop\\Strata\\GitHub\\wt-023-int\\build-cuda\\strata.exe');
+const PROFILE = env('E2E_EXPERT_PROFILE', 'C:\\Users\\AI-Server\\AppData\\Local\\Programs\\Strata\\control\\data\\expert-profile.bin');
+const PYTHON = env('E2E_PYTHON', 'python');
+const PORT = Number(env('E2E_PORT', '18590'));
+const MIN_FREE_GB = Number(env('E2E_MIN_FREE_GB', '7'));
+const GGUF = join(DOCS, 'models', 'gyro-Gyro-S', 'Qwen3.8-Flash-Next-Gyro-S-TQ1_0.gguf');
+const CUDA_BIN = env('E2E_CUDA_BIN', 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v13.0\\bin');
+const WINDOW = 65536;
+
+const sleep = ms => new Promise(r => setTimeout(r, ms));
+const freeGb = () => {
+ const out = spawnSync('powershell', ['-NoProfile', '-Command', '[math]::Round((Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory/1MB,1)'], { encoding: 'utf8' }).stdout.trim();
+ return Number(out);
+};
+const rows = [];
+const note = (id, ok, detail) => { rows.push({ id, ok, detail }); console.log(`${ok ? 'PASS' : 'FAIL'} ${id} ${detail}`); };
+
+if (!process.env.STRATA_HARNESS_DIR) { console.error('STRATA_HARNESS_DIR is not set'); process.exit(2); }
+for (const f of [ENGINE, GGUF]) if (!existsSync(f)) { console.error(`missing: ${f}`); process.exit(2); }
+if (freeGb() < 20) { console.error(`only ${freeGb()} GB of RAM are free; wait for the other streams (lock.py status) and run again`); process.exit(2); }
+
+const scratch = mkdtempSync(join(tmpdir(), 'strata-ctx-e2e-'));
+const configFile = join(scratch, 'server.json');
+const logFile = join(scratch, 'server.log');
+const readyFile = join(scratch, 'ready.json');
+writeFileSync(configFile, JSON.stringify({
+ exe: ENGINE,
+ args: ['--pack', join(DOCS, 'packs', 'gyro-gyro-s'), '--native', GGUF, '--ple-gguf', GGUF, ...(existsSync(PROFILE) ? ['--expert-profile', PROFILE] : []),
+ '--prefill', 'auto', '--spec-min-p', '0.5', '--mtp', join(DOCS, 'mtp', 'rt'), '--max-context', String(WINDOW), '--kv', 'int8', '--pool-workers', '5',
+ '--expert-cache', 'auto', '--kv-resident', '32768', '--spec', '4', '--vram-reserve-mib', '1536'],
+ cwd: REPO, tokenizer: join(DOCS, 'packs', 'gyro-gyro-s', 'tokenizer'), model_name: 'qwen3.8-flash-next-gyro-gyro-s', log: join(scratch, 'engine.log'), lib_dirs: [], port: PORT, gpu: 0,
+}, null, 2));
+
+let server = null, bridge = null, alarm = null;
+const stopServer = () => {
+ if (server?.pid) spawnSync('taskkill', ['/T', '/F', '/PID', String(server.pid)], { stdio: 'ignore' });
+ server = null;
+};
+const finish = async (code) => {
+ clearInterval(alarm);
+ try { await bridge?.close(); } catch { /* going away */ }
+ stopServer();
+ await sleep(1500);
+ console.log(`\nscratch folder: ${scratch}`);
+ process.exit(code);
+};
+process.on('SIGINT', () => finish(130));
+
+server = spawn(PYTHON, ['serve/server.py', '--engine', 'strata', '--config', configFile, '--port', String(PORT), '--no-api-key', '--ready-file', readyFile], {
+ cwd: REPO, env: { ...process.env, PATH: `${CUDA_BIN};${process.env.PATH}`, STRATA_DATA_DIR: join(scratch, 'data') }, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
+});
+const sink = [];
+server.stdout.on('data', d => sink.push(String(d))); server.stderr.on('data', d => sink.push(String(d)));
+server.on('exit', code => { sink.push(`[server exited ${code}]`); server = null; });
+// Memory alarm: this run must never be what freezes the PC. Under the floor the engine is stopped (by PID) and the run ends.
+alarm = setInterval(() => { const free = freeGb(); if (free < MIN_FREE_GB) { console.error(`ALARM: ${free} GB of RAM free, stopping the engine`); finish(3); } }, 3000);
+alarm.unref?.();
+
+const base = `http://127.0.0.1:${PORT}`;
+let ready = false;
+for (let i = 0; i < 120 && !ready; i++) {
+ if (!server) break;
+ try { ready = (await fetch(`${base}/health`, { signal: AbortSignal.timeout(2000) })).ok; } catch { /* still loading */ }
+ if (!ready) await sleep(2000);
+}
+writeFileSync(logFile, sink.join(''));
+if (!ready) { console.error('the engine did not become ready:\n' + sink.join('').slice(-1500)); await finish(4); }
+const model = (await (await fetch(`${base}/v1/models`)).json()).data[0].id;
+console.log(`engine ready: ${model}, free RAM ${freeGb()} GB`);
+
+// ---------------------------------------------------------------------------------------------------------------- the pieces
+
+const line = i => `2026-10-22 13:${String(Math.floor(i / 60) % 60).padStart(2, '0')}:${String(i % 60).padStart(2, '0')} INFO svc-${i % 17} req=${(i * 2654435761 >>> 0).toString(16)} status=${200 + (i % 7) * 3} ms=${(i * 37) % 900} path=/api/v1/items/${(i * 7919) % 100000}`;
+const logText = (chars, from = 0, needle = '') => {
+ const out = [];
+ let n = 0, i = from;
+ while (n < chars) { const l = line(i++); out.push(l); n += l.length + 1; }
+ if (needle) out.splice(Math.floor(out.length / 2), 0, needle);
+ return out.join('\n');
+};
+const count = text => countTokens({ baseUrl: `${base}/v1`, model, text });
+
+const turnsOf = async (message) => {
+ const frames = [];
+ const t0 = Date.now();
+ await bridge.runTurn({ chatId: 'e2e', message, workspacePath: scratch, mode: 'bypass', thinking: 'Off', onFrame: f => frames.push(f) });
+ return { frames, seconds: (Date.now() - t0) / 1000, last: frames.at(-1) };
+};
+const textOf = frames => frames.filter(f => f.type === 'delta' && !f.noteReason).map(f => f.text).join('').trim();
+const noteOf = frames => frames.find(f => f.type === 'delta' && f.noteReason === 'compacted')?.text?.trim() ?? null;
+
+bridge = createAgentBridge({
+ dataDir: join(scratch, 'agent'), env: process.env, startTimeoutMs: 120_000, log: l => console.log(' bridge:', l),
+ localInstances: () => [{ id: 'e2e', label: 'Gyro-S', baseUrl: `${base}/v1`, modelId: model, context: WINDOW, slots: 1, key: '' }], connections: () => [],
+});
+mkdirSync(join(scratch, 'agent'), { recursive: true });
+
+// ---------------------------------------------------------------------------------------------------------------- A
+{
+ // Grow a log until the engine itself counts about 60K tokens.
+ let chars = 150_000, tokens = await count(logText(chars));
+ while (tokens && tokens < 58_000 && chars < 400_000) { chars = Math.round(chars * (60_000 / tokens)); tokens = await count(logText(chars)); }
+ const t = await turnsOf(logText(chars) + '\n\nWhat is the most common status?');
+ const refused = t.last.type === 'error' && t.last.code === 'CONTEXT_MESSAGE_TOO_LARGE';
+ note('A too large is refused before it is read', refused && t.seconds < 15, `${chars} characters = ${tokens} tokens by the engine; answered in ${t.seconds.toFixed(1)} s; frame ${t.last.type}/${t.last.code ?? '-'}: ${String(t.last.message ?? '').slice(0, 110)}`);
+}
+
+// ---------------------------------------------------------------------------------------------------------------- B
+const needle = 'NOTE FROM OPS: the vault code is COBALT-27. Do not lose it.';
+const timeline = [];
+let notes = 0, refusedAny = false, peak = 0;
+{
+ for (let k = 0; k < 6; k++) {
+ const body = logText(34_000, k * 1000, k === 0 ? needle : '');
+ const t = await turnsOf(`${body}\n\nReply with the single word OK.`);
+ const note = noteOf(t.frames);
+ if (note) notes++;
+ if (t.last.type === 'error') refusedAny = true;
+ const used = t.last.contextTokens ?? 0;
+ peak = Math.max(peak, ...t.frames.filter(f => f.type === 'context').map(f => f.contextTokens), used);
+ timeline.push(`msg${k + 1}: ${t.last.type}${t.last.code ? '/' + t.last.code : ''} ${t.seconds.toFixed(0)} s context ${used} (${Math.round(100 * used / WINDOW)}%)${note ? ' [' + note.slice(0, 80) + ']' : ''}`);
+ console.log(' ' + timeline.at(-1));
+ if (t.last.type === 'error') break;
+ }
+ note('B six dense messages (about 14K tokens each) in a 64K window: no error', !refusedAny, `peak context ${peak} of ${WINDOW}; ${notes} compaction note(s) in the chat`);
+ note('B compaction ran and was announced', notes >= 1, timeline.join(' | '));
+}
+
+// ---------------------------------------------------------------------------------------------------------------- C
+{
+ const run = async (line) => { const frames = []; await bridge.compactCommand({ chatId: 'e2e', message: line, workspacePath: scratch, mode: 'bypass', choice: null, onFrame: f => frames.push(f) }); return frames; };
+ await turnsOf(`${logText(30_000, 90_000)}\n\nReply with the single word OK.`);
+ const t0 = Date.now();
+ const first = await run('/compact');
+ const said = first.find(f => f.type === 'delta');
+ const ok = said?.key === 'Older messages were summarized to make room. The chat was {0}% full and is now {1}% full.' && said.values[0] > said.values[1];
+ note('C /compact summarises and states the figures', ok, `${((Date.now() - t0) / 1000).toFixed(0)} s: ${said?.text}`);
+ const second = await run('/compact');
+ const again = second.find(f => f.type === 'delta')?.text ?? '';
+ note('C a second /compact is answered in plain words', /summarized|nothing|could not/i.test(again) && !/\{|400|invalid_request/.test(again), again);
+}
+
+// ---------------------------------------------------------------------------------------------------------------- D
+{
+ const t = await turnsOf('What was the vault code in the very first message? Answer with the code only.');
+ const answer = textOf(t.frames);
+ console.log(`D needle after compaction: "${answer.slice(0, 80)}" (${t.last.type})`);
+ rows.push({ id: 'D the needle', ok: /COBALT-27/.test(answer), detail: `reported only: answer "${answer.slice(0, 80)}"` });
+}
+
+console.log('\n' + rows.map(r => `${r.ok ? 'PASS' : 'FAIL'} ${r.id}`).join('\n'));
+const failed = rows.filter(r => !r.ok && !r.id.startsWith('D')).length;
+writeFileSync(join(scratch, 'result.json'), JSON.stringify(rows, null, 2));
+await finish(failed ? 1 : 0);
diff --git a/apps/host/server/agent/agent-bridge.mjs b/apps/host/server/agent/agent-bridge.mjs
index 5ba219612..7f61de553 100644
--- a/apps/host/server/agent/agent-bridge.mjs
+++ b/apps/host/server/agent/agent-bridge.mjs
@@ -30,6 +30,7 @@ import { DeliverableTracker, ActivityGroups } from './deliverables.mjs';
import { SkillsUsed, skillInfoFrom } from './skills-used.mjs';
import { turnEngineSpeed } from './engine-speed.mjs';
import { TurnGuard, guardNote, GUARD_NOTES } from './turn-guard.mjs';
+import { BASE_TOKENS, COUNT_ABOVE_CHARS, FIT_TEXT, countTokens, decideFit, fill, grouped } from './context-fit.mjs';
import { PlanStore, planPrompt, parsePlan, parsePoints, stepObjective, stepLine, firstSentence, stepSummary, planAsPrompt, editedSteps, planView, researchPrompt, researchWritePrompt, RESEARCH_NUDGE, parseSources } from './plan.mjs';
/**
@@ -406,7 +407,27 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
const appliedMode = planTask ? 'plan' : modeOf(mode);
await applyMode(runtime, sessionId, appliedMode);
- const projector = new TurnProjector({ policy: MODE_PRESETS[appliedMode].approval });
+ // Does the message fit the model's window? Asked before anything is sent, with the model server's own count (context-fit.mjs). A message
+ // that fits once the older messages are a summary is sent after a summary; one that can never fit is not sent, and the person is told
+ // plainly and offered the two ways out. Whatever cannot be counted goes through: the Harness recovers from a refusal by itself.
+ if (!goal && route.kind === 'local') {
+ const fit = await fitCheck({ runtime, route, sessionId, fresh, history, message }).catch(() => null);
+ if (fit?.action === 'too-large') {
+ reporter.clear();
+ const values = [grouped(fit.tokens), grouped(fit.window)];
+ const text = fill(FIT_TEXT.tooLarge, values);
+ emit({ type: 'error', message: text, key: FIT_TEXT.tooLarge, values, code: 'CONTEXT_MESSAGE_TOO_LARGE', tokens: fit.tokens, window: fit.window });
+ return { error: text };
+ }
+ if (fit?.action === 'summarize') {
+ reporter.set({ phase: 'compacting', code: 'compacting', text: FIT_TEXT.summarizing, key: FIT_TEXT.summarizing, values: [] });
+ const done = await compactChat(runtime, sessionId);
+ reporter.clear();
+ if (done.ok) emit(summarizedNote(done.before, done.after, fit.window));
+ }
+ }
+
+ const projector = new TurnProjector({ policy: MODE_PRESETS[appliedMode].approval, window: route.contextWindow });
if (!goal) guard = new TurnGuard({ plan: !!planTask, contextMax: route.kind === 'local' ? route.contextWindow : 0 }); // a saved connection's window is often only assumed: never judged by a guess
projector.onStatus = update => { if (!turn?.parentOpen && turn?.children.size && [...turn.children.values()].some(h => !h.done) && update.phase === 'waiting') return; reporter.set(update); };
const turn = {
@@ -951,6 +972,16 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
// -- /plan -----------------------------------------------------------------------------------------------
+ /** A short answer in the chat that the page translates (the sentence is in the interface catalog, "values" fill its slots). */
+ function sayKey(options, template, values = []) {
+ const { onFrame } = options;
+ onFrame({ type: 'queued', requestId: 'compact-' + randomUUID() });
+ onFrame({ type: 'start', model: '', contextMax: 0, demo: false, remoteModel: false, agent: true, mode: modeOf(options.mode), skills: [] });
+ onFrame({ type: 'delta', text: fill(template, values), key: template, values });
+ onFrame({ type: 'done', finishReason: 'stop', tokensIn: null, tokensOut: null, usageSource: 'unavailable', durationMs: 0 });
+ return { compact: true };
+ }
+
/** A short answer in the chat (no model): the same frames a turn sends. */
function sayOnce(options, reply) {
const { onFrame } = options;
@@ -1150,6 +1181,90 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =
* Stop one chat's turn. Resolves only once the turn has ended (or the wait times out), because the
* Harness acknowledges a cancel as admitted, not as done.
*/
+ /** What the chat holds now and the model's window, as the context plugin measures them (null when it cannot be read). */
+ async function contextOf(runtime, sessionId) {
+ try {
+ const r = await runtime.gw.rpc('commands/execute', { agentId: sessionId, line: '/strata-context', submittedAttachments: [] });
+ if (r?.result?.kind !== 'success') return null;
+ const v = JSON.parse(String(r.result.text));
+ return Number.isFinite(v.tokens) ? { tokens: Math.round(v.tokens), window: Number(v.window) > 0 ? Math.round(v.window) : 0 } : null;
+ } catch { return null; }
+ }
+
+ /** The decision of context-fit.mjs for the message about to be sent, or null when the model server cannot count it. */
+ async function fitCheck({ runtime, route, sessionId, fresh, history, message }) {
+ const inst = localInstances().find(i => i.id === route.instanceId);
+ if (!inst || !(route.contextWindow > 0)) return null;
+ const carried = fresh && history.length ? withEarlierConversation(history, message) : message;
+ if (carried.length < COUNT_ABOVE_CHARS) return null;
+ const count = text => countTokens({ baseUrl: inst.baseUrl, key: inst.key, model: inst.modelId, text });
+ let tokens = await count(carried);
+ if (tokens == null) return null;
+ const now = fresh ? null : await contextOf(runtime, sessionId);
+ const window = now?.window || route.contextWindow;
+ const pressure = now?.tokens ?? 0;
+ let verdict = decideFit({ window, pressure, messageTokens: tokens });
+ // The earlier conversation carried into a fresh session is what pushed it over: send the message alone instead.
+ if (verdict.action === 'too-large' && carried !== message) {
+ const alone = await count(message);
+ if (alone != null) { tokens = alone; verdict = decideFit({ window, pressure, messageTokens: alone }); }
+ }
+ return { ...verdict, tokens, window, pressure };
+ }
+
+ /**
+ * Summarise the older part of a chat with the Harness's /compact and say what happened. Never throws.
+ * ok:true carries the figures before and after; ok:false says why (nothing to summarise, busy, failed).
+ */
+ async function compactChat(runtime, sessionId) {
+ const before = await contextOf(runtime, sessionId);
+ let result;
+ try { result = (await runtime.gw.rpc('commands/execute', { agentId: sessionId, line: '/compact', submittedAttachments: [] }))?.result; }
+ catch (e) { return { ok: false, reason: /not.found|no such/i.test(String(e.message)) ? 'nothing' : 'failed', before, text: String(e.message ?? e) }; }
+ const text = String(result?.text ?? '');
+ log('agent · compact: ' + (result?.kind ?? 'none') + ' ' + text.slice(0, 140));
+ if (result?.kind === 'success' && /^Compacted/i.test(text)) return { ok: true, before, after: await contextOf(runtime, sessionId) };
+ if (result?.kind === 'success') return { ok: false, reason: 'nothing', before, text };
+ if (/active compaction|not idle/i.test(text)) return { ok: false, reason: 'busy', before, text };
+ // The Harness cannot make a summary smaller than what is left when the chat holds little beyond its first prompt: that is nothing to summarise, not a failure.
+ const little = before && before.window > 0 && before.tokens - BASE_TOKENS < before.window * 0.25;
+ return { ok: false, reason: little && /useful summary|not smaller/i.test(text) ? 'nothing' : 'failed', before, text };
+ }
+
+ /** The line in the chat that explains why the context meter fell: how full it was and how full it is. */
+ function summarizedNote(before, after, window) {
+ const w = after?.window || before?.window || window || 0;
+ const pct = v => Math.max(0, Math.min(100, Math.round(100 * v.tokens / w)));
+ const known = w > 0 && before && after;
+ const template = known ? FIT_TEXT.summarized : FIT_TEXT.summarizedShort;
+ const values = known ? [pct(before), pct(after)] : [];
+ return { type: 'delta', own: true, text: '\n\n' + fill(template, values), key: template, values, noteReason: 'compacted' };
+ }
+
+ /** "/compact": summarise the older messages of this chat now, and say in plain words what happened. */
+ async function compactCommand(options) {
+ const { chatId, choice, signal } = options;
+ const c = chat(chatId);
+ const say = (template, values = []) => sayKey(options, template, values);
+ if (c.turn || c.starting) return say(FIT_TEXT.compactBusy);
+ const known = sessionMap[chatId]?.sessionId;
+ if (!known) return say(FIT_TEXT.noChatYet);
+ let runtime;
+ try { ({ rt: runtime } = await ensureRuntime(choice, signal)); } catch { return say(FIT_TEXT.compactFailed); }
+ const done = await compactChat(runtime, known);
+ if (done.ok) {
+ const note = summarizedNote(done.before, done.after, 0);
+ return say(note.key, note.values);
+ }
+ if (done.reason === 'busy') return say(FIT_TEXT.compactBusy);
+ if (done.reason === 'nothing') {
+ const w = done.before?.window || 0;
+ const pct = w > 0 && done.before ? Math.round(100 * done.before.tokens / w) : 0;
+ return say(pct >= 60 ? FIT_TEXT.nothingFull : FIT_TEXT.nothingToSummarize, pct >= 60 ? [pct] : []);
+ }
+ return say(FIT_TEXT.compactFailed);
+ }
+
/** Summarise the older part of a session with the Harness's own /compact. Best effort: the next message compacts again by itself when it must. */
async function compactSession(runtime, sessionId) {
try {
@@ -1204,7 +1319,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, llmTarget, setPlanBriefing, resetSession, reopenPlan, skipStep, planGoal, planControl, planState, queueMessage, cancelQueued, takeQueue, forget, approve, setMode, stop, pendingFor, pendingAll, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, workingFolder: chatId => sessionMap[chatId]?.cwd ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) };
+ return { compactCommand, goalState, goalControl, harnessDir, status, runTurn, goalCommand, planCommand, researchCommand, llmTarget, setPlanBriefing, resetSession, reopenPlan, skipStep, planGoal, planControl, planState, queueMessage, cancelQueued, takeQueue, forget, approve, setMode, stop, pendingFor, pendingAll, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, workingFolder: chatId => sessionMap[chatId]?.cwd ?? 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/context-fit.mjs b/apps/host/server/agent/context-fit.mjs
new file mode 100644
index 000000000..04552cb29
--- /dev/null
+++ b/apps/host/server/agent/context-fit.mjs
@@ -0,0 +1,96 @@
+/**
+ * Does a message fit the model's window? Asked before it is sent, with the model server's own count, so a message the server would refuse
+ * is never read for half a minute first (2026-10-22: a 103 KB paste was 70,141 tokens in a 65,536-token window, and the 400 came back after
+ * the whole prompt had been read). No I/O except the one count request; everything else is arithmetic the tests call directly.
+ *
+ * Three answers: send it; summarise the chat first and then send it (the message fits once the older messages are a summary); or it can
+ * never fit, and the person is offered the two ways out that do (the text as a file the assistant reads in parts, or a shortened preview).
+ */
+
+/** What a new chat already holds before the person's first word: the system prompt and the tool list. Measured 10,510 by the Harness's price. */
+export const BASE_TOKENS = 12000;
+/** The least room a reply needs after the prompt: an eighth of the window, at most this. */
+export const REPLY_FLOOR = 4096;
+/** A message shorter than this many characters cannot matter to a window of 8K or more, so it is not even counted. */
+export const COUNT_ABOVE_CHARS = 3000;
+
+export const replyRoom = window => Math.max(512, Math.min(REPLY_FLOOR, Math.floor(window / 8)));
+
+/**
+ * @param {{window:number, pressure?:number, base?:number, messageTokens:number}} input
+ * `pressure` is what the chat holds now (measured), `base` what an empty chat holds, `messageTokens` the new message.
+ * @returns {{action:'send'|'summarize'|'too-large', room:number, needs:number}}
+ */
+export function decideFit({ window, pressure = 0, base: given = BASE_TOKENS, messageTokens }) {
+ const reply = replyRoom(window);
+ const base = Math.min(given, Math.floor(window * 0.6)); // on a small window the first prompt cannot be what the window is made of
+ // A chat holds at least what an empty one does (the system prompt and the tools), so a new chat is not counted as empty.
+ const needs = Math.max(pressure, base) + messageTokens + reply;
+ if (!(window > 0) || !(messageTokens >= 0)) return { action: 'send', room: Infinity, needs: 0 };
+ if (needs <= window) return { action: 'send', room: window - needs, needs };
+ // Not as it stands. Does the message fit in an otherwise empty chat (only the system prompt and the tools before it)?
+ if (base + messageTokens + reply <= window) return { action: 'summarize', room: window - (base + messageTokens + reply), needs };
+ return { action: 'too-large', room: window - (base + messageTokens + reply), needs };
+}
+
+/**
+ * The model server's count of a text (its tokenizer and chat template), through its Anthropic-style count endpoint.
+ * Returns the number, or null when it cannot be had (an old engine, a server that is busy, a timeout): then nothing is blocked.
+ */
+export async function countTokens({ baseUrl, key = '', model = '', text, fetchImpl = fetch, timeoutMs = 8000 }) {
+ try {
+ const root = String(baseUrl ?? '').replace(/\/v1\/?$/, '').replace(/\/$/, '');
+ const res = await fetchImpl(`${root}/v1/messages/count_tokens`, {
+ method: 'POST',
+ headers: { 'content-type': 'application/json', ...(key ? { authorization: `Bearer ${key}`, 'x-api-key': key } : {}) },
+ body: JSON.stringify({ model, messages: [{ role: 'user', content: String(text) }] }),
+ signal: AbortSignal.timeout(timeoutMs),
+ });
+ if (!res.ok) return null;
+ const n = Number((await res.json())?.input_tokens);
+ return Number.isFinite(n) && n >= 0 ? Math.round(n) : null;
+ } catch { return null; }
+}
+
+/** The plain sentences (also in the interface catalog in all eight languages). `{0}` slots are filled by the page. */
+export const FIT_TEXT = Object.freeze({
+ tooLarge: 'This message is about {0} tokens long, and this model can read about {1} at a time. It is too large to send as it is.',
+ tooLargeNext: 'Attach it as a file for the assistant to read in parts, or send a shortened preview.',
+ full: 'This conversation no longer fits the model’s window, and summarizing the older messages did not make enough room.',
+ fullNext: 'Summarize it now and continue, or start a new chat.',
+ summarizing: 'Summarizing earlier messages…',
+ summarized: 'Older messages were summarized to make room. The chat was {0}% full and is now {1}% full.',
+ summarizedWas: 'Older messages were summarized to make room. The chat was {0}% full.',
+ summarizedShort: 'Older messages were summarized to make room.',
+ nothingFull: 'This chat is {0}% full, but there is nothing older to summarize: the newest message is what fills it. Start a new chat, or send a shorter message.',
+ nothingToSummarize: 'There is nothing older to summarize yet. Everything in this chat is still needed by the assistant.',
+ noChatYet: 'There is nothing to summarize yet. This chat has not started a conversation with the assistant.',
+ compactFailed: 'The older messages could not be summarized right now. Try again in a moment, or start a new chat.',
+ compactBusy: 'This chat is still working. Stop it or wait for it to finish, then try again.',
+});
+export const fill = (template, values = []) => String(template).replace(/\{(\d+)\}/g, (_, i) => String(values[Number(i)] ?? ''));
+
+/** A number with thousands separators, the way the page writes it. */
+export const grouped = n => Math.round(n).toLocaleString('en-US');
+
+/**
+ * The two ways to send a message that is too large.
+ * file the whole text goes in a file in the chat's folder; the message names it and shows how it starts
+ * preview the start and the end of the text, cut to what the window can hold, with the number of characters left out
+ * @param {{text:string, mode:'file'|'preview', tokens:number, window:number, pressure?:number, base?:number, path?:string}} input
+ * @returns {{message:string, kept:number}}
+ */
+export function oversizedReplacement({ text, mode, tokens, window, pressure = 0, base = BASE_TOKENS, path = '' }) {
+ const t = String(text);
+ const charsPerToken = tokens > 0 ? Math.max(0.8, t.length / tokens) : 3;
+ if (mode === 'file') {
+ const start = t.slice(0, 1200).trimEnd();
+ return { kept: start.length, message: `My message was too long to send in the chat, so I saved it as a file: ${path}\n\nIt starts like this:\n\n${start}\n\n[the rest is in the file]\n\nPlease read the file in parts (it is long) and then do what my message asks.` };
+ }
+ const room = Math.max(1000, Math.floor((window - Math.max(pressure, base) - replyRoom(window)) * 0.7));
+ const keep = Math.max(500, Math.floor(room * charsPerToken * 0.9));
+ const head = Math.ceil(keep * 0.7), tail = keep - head;
+ const left = Math.max(0, t.length - head - tail);
+ if (!left) return { kept: t.length, message: t };
+ return { kept: head + tail, message: `${t.slice(0, head)}\n\n[… ${grouped(left)} characters from the middle are left out because the message was too long to send …]\n\n${t.slice(t.length - tail)}` };
+}
diff --git a/apps/host/server/agent/events.mjs b/apps/host/server/agent/events.mjs
index a8b82de85..5dcc1a2af 100644
--- a/apps/host/server/agent/events.mjs
+++ b/apps/host/server/agent/events.mjs
@@ -9,6 +9,8 @@
import { toolUpdate, TokenCount, fmtTokens } from './status.mjs';
import { askFor } from './ask.mjs';
+import { FIT_TEXT, fill } from './context-fit.mjs';
+import { isContextOverflow } from './context-budget.mjs';
const SHELL = new Set(['pwsh', 'bash', 'job_kill', 'job_output', 'job_list']);
const WRITE = new Set(['write', 'edit']);
@@ -196,8 +198,10 @@ export class TurnProjector {
* no question ever reaches the person: the card goes straight from running to denied, without a moment where it
* offers Approve and Deny buttons that would do nothing.
*/
- constructor({ startedAt = Date.now(), policy = 'ask' } = {}) {
+ constructor({ startedAt = Date.now(), policy = 'ask', window = 0 } = {}) {
this.startedAt = startedAt;
+ this.window = Number(window) || 0; // the model's window, for the figures in the line that says the chat was summarised
+ this.summarizing = false;
this.policy = policy;
this.byPolicy = new Set(); // callIds the approval policy rejected without asking
this.tools = new Map(); // callId -> { card, calledAt }
@@ -303,6 +307,7 @@ export class TurnProjector {
this.tokensIn += prompt;
this.tokensOut += Number(c.usage.outputTokens) || 0;
this.contextTokens = prompt + (Number(c.usage.outputTokens) || 0);
+ out.push({ type: 'context', contextTokens: this.contextTokens, contextMax: this.window || undefined });
}
}
return out;
@@ -371,6 +376,22 @@ export class TurnProjector {
if (frame) out.push(frame);
break;
}
+ case 'compaction/start':
+ // The Harness is summarising the older messages to make room: say so while it does, and again (once) when it is done.
+ this.summarizing = true;
+ this.#status({ phase: 'compacting', code: 'compacting', text: FIT_TEXT.summarizing, key: FIT_TEXT.summarizing, values: [] });
+ break;
+ case 'compaction/end':
+ if (!this.summarizing) break;
+ this.summarizing = false;
+ this.#afterTool();
+ if (!d.error) {
+ const before = this.window > 0 && this.contextTokens ? Math.min(100, Math.round(100 * this.contextTokens / this.window)) : null;
+ const text = before === null ? FIT_TEXT.summarizedShort : FIT_TEXT.summarizedWas;
+ const values = before === null ? [] : [before];
+ out.push({ type: 'delta', own: true, text: '\n\n' + fill(text, values), key: text, values, noteReason: 'compacted' });
+ }
+ break;
case 'turn/start':
// The parent woke up again (a helper finished and reported back): the message goes on, as a new paragraph.
this.ended = null;
@@ -420,7 +441,7 @@ export class TurnProjector {
case 'aborted':
return [{ type: 'done', ...base, ...usage, finishReason: 'aborted', cancelled: true }];
case 'error':
- return [{ type: 'error', message: friendlyFailure(reason.error), code: reason.error?.code ?? 'UNKNOWN' }];
+ return [{ type: 'error', message: friendlyFailure(reason.error), code: isContextFailure(reason.error) ? 'CONTEXT_WINDOW_EXCEEDED' : reason.error?.code ?? 'UNKNOWN', ...(isContextFailure(reason.error) ? { key: FIT_TEXT.full, details: String(reason.error?.message ?? '') } : {}) }];
case 'blocked':
return [{ type: 'error', message: 'The agent stopped because this turn was blocked by policy.', code: 'BLOCKED' }];
default:
@@ -429,10 +450,15 @@ export class TurnProjector {
}
}
+/** Whether a failure is the model server saying the conversation does not fit its window (its code, or the engine's own words). */
+export function isContextFailure(failure) {
+ return isContextOverflow(failure) || /context window|too long for|exceeds the context/i.test(String(failure?.message ?? ''));
+}
+
/** A model-route failure in words a user can act on. */
export function friendlyFailure(failure) {
const message = String(failure?.message ?? 'The model request failed.');
- if (/context window|too long|exceeds/i.test(message)) return `The conversation no longer fits the model's context window. ${clip(message, 200)}`;
+ if (isContextFailure(failure)) return FIT_TEXT.full;
if (/ECONNREFUSED|fetch failed|connect/i.test(message)) return `The model is not reachable. ${clip(message, 200)}`;
if (/401|403|credential|api key/i.test(message)) return 'The model rejected the credentials. Check the connection in Settings.';
return clip(message, 400);
diff --git a/apps/host/server/agent/plugins/strata-context.mjs b/apps/host/server/agent/plugins/strata-context.mjs
index 473cd192a..4d52218ae 100644
--- a/apps/host/server/agent/plugins/strata-context.mjs
+++ b/apps/host/server/agent/plugins/strata-context.mjs
@@ -81,6 +81,26 @@ export function apply(ctx, config = {}) {
};
}
+ // The host asks, with /strata-context, how much of the window the chat holds right now: the same measured number compaction decides on
+ // (JSON in the command's text: { tokens, window }). Before a message goes out, and after /compact, so the person is told true figures.
+ ctx.inject(['commands'], (commandCtx) => {
+ commandCtx.commands.register({
+ definitionId: 'strata-context',
+ name: 'strata-context',
+ description: 'Reports how many tokens the conversation holds now and the size of the model window',
+ handler: async (invocation) => {
+ try {
+ const session = invocation.agent.session;
+ const tokens = meter.measure(session).totalTokens;
+ const config = session.requestHeader?.()?.config;
+ let window = 0;
+ if (config) window = (await llm.resolveModelInfo(config.provider, config.model))?.context?.contextWindow ?? 0;
+ return { kind: 'success', text: JSON.stringify({ tokens, window }) };
+ } catch (e) { return { kind: 'error', text: `strata-context: ${e.message}` }; }
+ },
+ });
+ });
+
/** A finished stream: its chunks, the text it wrote and why it ended. */
async function drain(iterable) {
let text = '', finish = null;
diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjs
index f8d638ad7..a788ccf05 100644
--- a/apps/host/server/index.mjs
+++ b/apps/host/server/index.mjs
@@ -55,6 +55,7 @@ import { RequestQueue } from './request-queue.mjs';
import { selectSkills, skillInstructions } from './skill-selection.mjs';
import { createModelProviders } from './model-providers.mjs';
import { createForkTimeskip } from './timeskip-fork.mjs';
+import { prepareOversized } from './oversize.mjs';
import { createChatFolders, chatRootOf, openFile, resolveChatFile, previewFile, rawFile, createAttachments, withAttachments } from './chat-files.mjs';
import { createLlmProxy, samplingFromExpert } from './agent/llm-shape.mjs';
import { checkUpdates, checkComponentUpdates } from './updates.mjs';
@@ -601,8 +602,11 @@ 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());
+ // "/compact" summarises the older messages of this chat now (it used to go to the model as text, which answered it as a question).
+ const compactLine = /^\/compact\s*$/i.test(message.trim());
const turnOptions = { chatId, message, workspacePath: cwd, choice, images, 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 })
+ : compactLine ? await agent.compactCommand(turnOptions)
: researchLine ? await agent.researchCommand({ ...turnOptions, line: researchLine[1] ?? '' })
: planLine ? await agent.planCommand({ ...turnOptions, line: planLine[1] ?? '' })
: goalLine ? await agent.goalCommand({ ...turnOptions, line: goalLine[1] ?? '' }) : await agent.runTurn(turnOptions);
@@ -1717,6 +1721,16 @@ async function api(req, res, path, method, url) {
try { const f = rawFile(chatRoot(url.searchParams.get('sessionId') ?? ''), url.searchParams.get('path')); res.writeHead(200, { 'content-type': f.mime, 'content-length': f.bytes.length, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff' }); res.end(f.bytes); return undefined; }
catch (e) { return error(res, e.code === 'outside' ? 403 : 400, e.message); }
}
+ /* A message too large for the model's window, made small enough to send: its text as a file in the chat's folder, or a shortened preview (oversize.mjs). */
+ if (path === '/api/chat/oversize' && method === 'POST') {
+ const body = await readBody(req, 12 * 1024 * 1024);
+ const text = String(body.text ?? '');
+ if (!text.trim()) return error(res, 400, 'text is required');
+ let instance = null;
+ try { instance = native.resolveInstance(body.nativeModelId ?? native.state().defaultId); } catch { /* no local model: the length alone decides */ }
+ try { return json(res, 200, await prepareOversized({ mode: body.mode === 'file' ? 'file' : 'preview', text, root: chatRoot(String(body.sessionId ?? '')), instance })); }
+ catch (e) { return error(res, 400, 'The message could not be prepared. Try a shorter message.'); }
+ }
/* "Attach as context": the file is named in the next message of the chat, so the assistant reads it again. */
if (path === '/api/chat/attachments' && method === 'GET') return json(res, 200, { files: chatAttachments.list(url.searchParams.get('sessionId') ?? '') });
if (path === '/api/chat/attachments' && method === 'POST') {
diff --git a/apps/host/server/oversize.mjs b/apps/host/server/oversize.mjs
new file mode 100644
index 000000000..5b2407550
--- /dev/null
+++ b/apps/host/server/oversize.mjs
@@ -0,0 +1,30 @@
+/**
+ * A message too large for the model's window, made small enough to send (agent/context-fit.mjs decides that it is too large).
+ * The person picks how, on the error card:
+ * file the whole text is saved in the chat's folder and the message names the file; the assistant reads it in parts
+ * preview the start and the end of the text, cut to what the window can hold
+ * Nothing is sent from here: the page sends the returned message as the person's next message.
+ */
+import { mkdirSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { countTokens, oversizedReplacement } from './agent/context-fit.mjs';
+
+const stamp = d => `${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, '0')}${String(d.getDate()).padStart(2, '0')}-${String(d.getHours()).padStart(2, '0')}${String(d.getMinutes()).padStart(2, '0')}${String(d.getSeconds()).padStart(2, '0')}`;
+
+/**
+ * @param {{mode:'file'|'preview', text:string, root:string|null, instance?:{port:number, key?:string, apiModelId?:string, context?:number, metrics?:any}|null, now?:Date, fetchImpl?:typeof fetch}} input
+ * @returns {Promise<{message:string, mode:string, tokens:number|null, kept:number, path?:string}>}
+ */
+export async function prepareOversized({ mode, text, root, instance = null, now = new Date(), fetchImpl = fetch }) {
+ const window = instance?.metrics?.engine?.max_context ?? instance?.context ?? 32768;
+ const tokens = instance ? await countTokens({ baseUrl: `http://127.0.0.1:${instance.port}/v1`, key: instance.key ?? '', model: instance.apiModelId ?? '', text, fetchImpl }) : null;
+ const counted = tokens ?? Math.ceil(text.length / 3); // not countable: three characters a token is on the safe side for code and logs
+ if (mode === 'file') {
+ if (!root) throw new Error('This chat has no folder yet. Choose one first.');
+ mkdirSync(root, { recursive: true });
+ const path = join(root, `pasted-message-${stamp(now)}.txt`);
+ writeFileSync(path, text, { encoding: 'utf8', flag: 'wx' });
+ return { mode, tokens, path, ...oversizedReplacement({ text, mode, tokens: counted, window, path }) };
+ }
+ return { mode: 'preview', tokens, ...oversizedReplacement({ text, mode: 'preview', tokens: counted, window }) };
+}
diff --git a/apps/host/src/components/ui.tsx b/apps/host/src/components/ui.tsx
index 8a475f0e7..48f846457 100644
--- a/apps/host/src/components/ui.tsx
+++ b/apps/host/src/components/ui.tsx
@@ -518,18 +518,20 @@ export function MetricGrid({ children, min = 200 }: {
</div>);
}
/** Shared, announced error with a cause and a concrete recovery action. */
-export function ErrorNotice({ message, next, action, onRetry, detail }: {
+export function ErrorNotice({ message, next, action, onRetry, detail, secondary }: {
message: string;
next?: string;
action?: string;
onRetry?: () => void;
detail?: string;
+ /** A second way out, beside the first (a context error offers two). */
+ secondary?: { label: string; onClick: () => void };
}) {
// A known failure is shown as a sentence and a next step; the original text stays under Details for experts.
const plain = plainFailure(message);
const known = plain.kind !== 'plain';
const nextStep = known ? (plain.next ? tr(plain.next) : next) : next;
const raw = detail ?? (known ? plain.detail : undefined);
- return <div className="error-notice" role="alert"><Icon name="error" size={18}/><div><strong>{tr(known ? plain.message : userError(message))}</strong>{tr(nextStep ? <p>{tr(nextStep)}</p> : null)}{tr(raw ? <details className="error-detail"><summary className="small">{tr("Details")}</summary><pre className="small muted">{raw}</pre></details> : null)}</div>{tr(onRetry ? <Btn size="sm" onClick={onRetry}>{tr(action ?? 'Try again')}</Btn> : null)}</div>;
+ return <div className="error-notice" role="alert"><Icon name="error" size={18}/><div><strong>{tr(known ? plain.message : userError(message))}</strong>{tr(nextStep ? <p>{tr(nextStep)}</p> : null)}{tr(raw ? <details className="error-detail"><summary className="small">{tr("Details")}</summary><pre className="small muted">{raw}</pre></details> : null)}</div>{tr(onRetry ? (secondary ? <div className="row"><Btn size="sm" onClick={onRetry}>{tr(action ?? 'Try again')}</Btn><Btn size="sm" variant="ghost" onClick={secondary.onClick}>{tr(secondary.label)}</Btn></div> : <Btn size="sm" onClick={onRetry}>{tr(action ?? 'Try again')}</Btn>) : null)}</div>;
}
export function PreviewBadge() { const demo = useStore(s => s.library?.mode === 'simulated'); return demo ? <span className="preview-badge">{tr("Preview build")}</span> : null; }
diff --git a/apps/host/src/lib/chat-metrics.ts b/apps/host/src/lib/chat-metrics.ts
index 5fcd1af17..0b16c4235 100644
--- a/apps/host/src/lib/chat-metrics.ts
+++ b/apps/host/src/lib/chat-metrics.ts
@@ -13,7 +13,10 @@ export function chatMetrics(session:Session|null, monitor:MonitorState|null, con
const finished=latest?(latest.contextUsed??latest.inputTokens!+(latest.tokens??0)):null;
const liveUsed=active&&Number.isFinite(monitor!.context.usedTokens)&&monitor!.context.usedTokens>0?monitor!.context.usedTokens:null;
const live=replies.some(m=>m.streaming)||(active&&['reading','thinking','generating'].includes(monitor!.phase));
- const used=live?(liveUsed??finished):(finished??liveUsed);
+ // While a turn runs, its own figure comes first: the host reports what the context holds after each model request of the turn. The engine's live
+ // figure belongs to the request in flight (a small one between two tools read as 1%), and `finished` to an earlier reply.
+ const turnUsed=replies.find(m=>m.streaming&&Number.isFinite(m.contextUsed)&&m.contextUsed!>0)?.contextUsed??null;
+ const used=live?(turnUsed??liveUsed??finished):(finished??liveUsed);
const max=remote?(replies.at(-1)?.contextMax||null):configuredContext??(active?monitor!.context.maxTokens:latest?.contextMax??null);
const streaming=replies.find(m=>m.streaming);
// Answer speed: everything the model generated over its decode time. While a reply streams it is that reply's; after it, the LAST reply's own
diff --git a/apps/host/src/lib/context-recovery.ts b/apps/host/src/lib/context-recovery.ts
new file mode 100644
index 000000000..e01e30826
--- /dev/null
+++ b/apps/host/src/lib/context-recovery.ts
@@ -0,0 +1,32 @@
+import { tr } from './i18n';
+import { sendOversized, startNewChat, summarizeAndContinue } from './store';
+
+/**
+ * What the error card of a chat offers when the model's window is the problem. A conversation that no longer fits can be summarised and
+ * carried on, or left for a new chat; a single message that is too large can go as a file the assistant reads in parts, or as a shortened
+ * preview. "Try the message again" would only fail the same way, so the card never says it for these.
+ */
+export function contextRecovery(code: string | undefined): {
+ next: string;
+ action?: string;
+ onRetry?: () => void;
+ secondary?: { label: string; onClick: () => void };
+} {
+ if (code === 'CONTEXT_MESSAGE_TOO_LARGE') {
+ return {
+ next: tr('Attach it as a file for the assistant to read in parts, or send a shortened preview.'),
+ action: 'Attach as a file',
+ onRetry: () => void sendOversized('file'),
+ secondary: { label: 'Send a shortened preview', onClick: () => void sendOversized('preview') },
+ };
+ }
+ if (code === 'CONTEXT_WINDOW_EXCEEDED') {
+ return {
+ next: tr('Summarize it now and continue, or start a new chat.'),
+ action: 'Summarize and continue',
+ onRetry: () => void summarizeAndContinue(),
+ secondary: { label: 'Start a new chat', onClick: () => startNewChat() },
+ };
+ }
+ return { next: tr('Try sending the message again.') };
+}
diff --git a/apps/host/src/lib/plain-errors.ts b/apps/host/src/lib/plain-errors.ts
index 538b305f6..65f103bec 100644
--- a/apps/host/src/lib/plain-errors.ts
+++ b/apps/host/src/lib/plain-errors.ts
@@ -34,6 +34,7 @@ const RULES: Rule[] = [
{ kind: 'blocked', match: /0xC0000022|3221225506|\bEACCES\b|\bEPERM\b|operation not permitted/i, message: 'Windows or your security software stopped the engine from starting.', next: 'Allow Strata in your security software, then try again.' },
{ kind: 'file-missing', match: /\bENOENT\b|no such file or directory|cannot find the (?:file|path)|0x80070002|exit code 127|file not found/i, message: 'Strata could not find a file it needs to start the engine.', next: 'Reinstall Strata or press Repair.' },
{ kind: 'memory', match: /out of memory|memory guard|\boom\b|\bkilled\b|exit code 137|cannot allocate memory|not enough memory|bad_alloc|pinned-memory-exhausted|mapped staging|cudaErrorMemoryAllocation|insufficient memory/i, message: MEMORY, next: MEMORY_NEXT },
+ { kind: 'previous-session', match: /lost host child is still present|retry after it exits/i, message: 'The last Strata session is still closing down.', next: 'Wait a moment and try again. If it keeps happening, restart the computer.' },
{ kind: 'start-failed', match: /server_exited|engine stopped|exited before|died before/i, message: 'The model stopped while starting.', next: 'Try again. If it keeps happening, open Details and share them when you ask for help.' },
{ kind: 'slow-load', match: /loading took more than/i, message: 'Loading is taking too long.', next: 'The PC may be short on memory. Close other programs and try again.' },
{ kind: 'engine-missing', match: /no engine for this pc|engine component|never downloads an engine|engine .*not installed|no strata engine/i, message: 'Strata’s engine for this computer isn’t installed yet.', next: 'It is added when you download a model. Download your model again to add it.' },
diff --git a/apps/host/src/lib/store.ts b/apps/host/src/lib/store.ts
index 14b31d8c9..b3f452038 100644
--- a/apps/host/src/lib/store.ts
+++ b/apps/host/src/lib/store.ts
@@ -1366,6 +1366,56 @@ export async function retryLast(messageId?: string) {
}
}
+/** The open chat's last question and the answer that failed under it (null when the chat does not end that way). */
+function failedExchange(): { prompt: string; sessionId: string } | null {
+ const sessionId = state.activeSession;
+ const chat = state.sessions.find(s => s.id === sessionId);
+ if (!sessionId || !chat || state.streaming) return null;
+ const reply = chat.messages[chat.messages.length - 1], ask = chat.messages[chat.messages.length - 2];
+ return reply?.role === 'assistant' && reply.error && ask?.role === 'user' ? { prompt: ask.content, sessionId } : null;
+}
+/** Takes the failed question and its error out of the transcript, so asking again does not show the question twice. */
+function dropFailedExchange(sessionId: string) {
+ patchSession(sessionId, s => ({ ...s, messages: s.messages.slice(0, -2) }));
+}
+
+/**
+ * The error card of a conversation that no longer fits the model's window: summarise the older messages (the same as typing /compact), then
+ * ask the last question again in the same chat.
+ */
+export async function summarizeAndContinue() {
+ const failed = failedExchange();
+ if (!failed) return;
+ dropFailedExchange(failed.sessionId);
+ await sendMessage('/compact');
+ await sendMessage(failed.prompt);
+}
+
+/**
+ * The error card of a message that is too large to send: the host makes it small enough (its text as a file in the chat's folder that the
+ * assistant reads in parts, or a shortened preview), and that is sent in its place.
+ */
+export async function sendOversized(mode: 'file' | 'preview') {
+ const failed = failedExchange();
+ if (!failed) return;
+ let reply: { message?: string };
+ try {
+ reply = await api.post<{ message?: string }>('/api/chat/oversize', { sessionId: failed.sessionId, mode, text: failed.prompt, nativeModelId: nativeState()?.instances.length ? nativeState()?.defaultId : undefined });
+ } catch {
+ pushToast('error', 'The message could not be prepared. Try a shorter message.');
+ return;
+ }
+ if (!reply.message) return;
+ dropFailedExchange(failed.sessionId);
+ await sendMessage(reply.message);
+}
+
+/** A new, empty chat, opened. */
+export function startNewChat() {
+ const session = newSession();
+ go('chat', session.id);
+}
+
/**
* Forks a conversation at a message, leaving the original untouched.
*
@@ -1733,9 +1783,15 @@ export async function sendMessage(text: string, options: { fromLoop?: boolean; g
tps: typeof frame.tps === 'number' ? frame.tps : m.tps,
promptTps: typeof frame.promptTps === 'number' ? frame.promptTps : m.promptTps,
}));
+ } else if (type === 'context') {
+ // What the context holds after each model request of an agent turn: the meter follows the turn instead of waiting for its end.
+ const used = Number(frame.contextTokens), max = Number(frame.contextMax);
+ if (Number.isFinite(used) && used > 0) patchAssistant((m) => ({ ...m, contextUsed: used, ...(Number.isFinite(max) && max > 0 ? { contextMax: max } : {}) }));
} else if (type === 'error') {
clearStatus(sessionId);
- patchAssistant((m) => ({ ...m, error: plainFailure(String(frame.message ?? 'Engine error')).message, streaming: false }));
+ // A sentence the host wrote for the person (it carries its catalog key) is shown in their language as it is; anything else is made plain here.
+ const said = typeof frame.key === 'string' ? tr(frame.key, Array.isArray(frame.values) ? frame.values : undefined) : plainFailure(String(frame.message ?? 'Engine error')).message;
+ patchAssistant((m) => ({ ...m, error: said, streaming: false, ...(typeof frame.code === 'string' ? { errorCode: frame.code } : {}), ...(typeof frame.details === 'string' && frame.details ? { errorDetail: frame.details } : {}) }));
} else if (type === 'done') {
clearStatus(sessionId);
patchSession(sessionId, (sess) => ({
diff --git a/apps/host/src/lib/types.ts b/apps/host/src/lib/types.ts
index b4b37058e..94d745e3f 100644
--- a/apps/host/src/lib/types.ts
+++ b/apps/host/src/lib/types.ts
@@ -358,6 +358,10 @@ export interface ChatMessage {
promptTps?: number;
durationMs?: number;
error?: string;
+ /** What went wrong, as a code the page acts on (a context error offers its own recovery buttons). */
+ errorCode?: string;
+ /** The engine's own text of the error, kept behind Details. */
+ errorDetail?: string;
tools?: ToolCall[];
}
diff --git a/apps/host/src/locales/de.json b/apps/host/src/locales/de.json
index d181908ce..abe35d703 100644
--- a/apps/host/src/locales/de.json
+++ b/apps/host/src/locales/de.json
@@ -3489,5 +3489,25 @@
"s8643": "Nur Ihr Prozessor und Arbeitsspeicher werden verwendet.",
"s8644": "Für Experten: Die Engine startet auf den Karten {0} (Hauptkarte zuletzt).",
"s8645": "Wird nicht gespeichert, solange Sie die Empfehlung behalten: Strata wählt neu, wenn Sie eine Karte hinzufügen oder entfernen.",
- "s8646": "Nicht genutzt: {0} (anderer Grafikkarten-Hersteller als der, den diese Engine ansteuert)."
+ "s8646": "Nicht genutzt: {0} (anderer Grafikkarten-Hersteller als der, den diese Engine ansteuert).",
+ "s9001": "Diese Nachricht ist etwa {0} Tokens lang, und dieses Modell kann etwa {1} auf einmal lesen. Sie ist zu groß, um so gesendet zu werden.",
+ "s9002": "Hängen Sie sie als Datei an, die der Assistent in Teilen liest, oder senden Sie eine gekürzte Vorschau.",
+ "s9003": "Diese Unterhaltung passt nicht mehr in das Fenster des Modells, und das Zusammenfassen der älteren Nachrichten hat nicht genug Platz geschaffen.",
+ "s9004": "Fassen Sie sie jetzt zusammen und machen Sie weiter, oder beginnen Sie einen neuen Chat.",
+ "s9005": "Frühere Nachrichten werden zusammengefasst …",
+ "s9006": "Ältere Nachrichten wurden zusammengefasst, um Platz zu schaffen. Der Chat war zu {0} % voll und ist jetzt zu {1} % voll.",
+ "s9007": "Ältere Nachrichten wurden zusammengefasst, um Platz zu schaffen. Der Chat war zu {0} % voll.",
+ "s9008": "Ältere Nachrichten wurden zusammengefasst, um Platz zu schaffen.",
+ "s9009": "Dieser Chat ist zu {0} % voll, aber es gibt nichts Älteres zusammenzufassen: Die neueste Nachricht füllt ihn. Beginnen Sie einen neuen Chat oder senden Sie eine kürzere Nachricht.",
+ "s9010": "Es gibt noch nichts Älteres zusammenzufassen. Alles in diesem Chat braucht der Assistent noch.",
+ "s9011": "Es gibt noch nichts zusammenzufassen. In diesem Chat hat noch keine Unterhaltung mit dem Assistenten begonnen.",
+ "s9012": "Die älteren Nachrichten konnten gerade nicht zusammengefasst werden. Versuchen Sie es gleich noch einmal oder beginnen Sie einen neuen Chat.",
+ "s9013": "In diesem Chat läuft noch etwas. Halten Sie ihn an oder warten Sie, bis er fertig ist, und versuchen Sie es dann erneut.",
+ "s9014": "Als Datei anhängen",
+ "s9015": "Gekürzte Vorschau senden",
+ "s9016": "Zusammenfassen und weitermachen",
+ "s9017": "Neuen Chat beginnen",
+ "s9018": "Die Nachricht konnte nicht vorbereitet werden. Versuchen Sie es mit einer kürzeren Nachricht.",
+ "s9019": "Die letzte Strata-Sitzung wird noch beendet.",
+ "s9020": "Warten Sie einen Moment und versuchen Sie es erneut. Wenn es wieder passiert, starten Sie den Computer neu."
}
diff --git a/apps/host/src/locales/en.json b/apps/host/src/locales/en.json
index f1ecc9800..3304dfa28 100644
--- a/apps/host/src/locales/en.json
+++ b/apps/host/src/locales/en.json
@@ -3489,5 +3489,25 @@
"s8643": "Only your processor and memory are used.",
"s8644": "For experts: the engine starts on cards {0} (Main last).",
"s8645": "Not saved while you keep the recommendation: Strata picks again if you add or remove a card.",
- "s8646": "Not used: {0} (another make of graphics card than this engine drives)."
+ "s8646": "Not used: {0} (another make of graphics card than this engine drives).",
+ "s9001": "This message is about {0} tokens long, and this model can read about {1} at a time. It is too large to send as it is.",
+ "s9002": "Attach it as a file for the assistant to read in parts, or send a shortened preview.",
+ "s9003": "This conversation no longer fits the model’s window, and summarizing the older messages did not make enough room.",
+ "s9004": "Summarize it now and continue, or start a new chat.",
+ "s9005": "Summarizing earlier messages…",
+ "s9006": "Older messages were summarized to make room. The chat was {0}% full and is now {1}% full.",
+ "s9007": "Older messages were summarized to make room. The chat was {0}% full.",
+ "s9008": "Older messages were summarized to make room.",
+ "s9009": "This chat is {0}% full, but there is nothing older to summarize: the newest message is what fills it. Start a new chat, or send a shorter message.",
+ "s9010": "There is nothing older to summarize yet. Everything in this chat is still needed by the assistant.",
+ "s9011": "There is nothing to summarize yet. This chat has not started a conversation with the assistant.",
+ "s9012": "The older messages could not be summarized right now. Try again in a moment, or start a new chat.",
+ "s9013": "This chat is still working. Stop it or wait for it to finish, then try again.",
+ "s9014": "Attach as a file",
+ "s9015": "Send a shortened preview",
+ "s9016": "Summarize and continue",
+ "s9017": "Start a new chat",
+ "s9018": "The message could not be prepared. Try a shorter message.",
+ "s9019": "The last Strata session is still closing down.",
+ "s9020": "Wait a moment and try again. If it keeps happening, restart the computer."
}
diff --git a/apps/host/src/locales/es.json b/apps/host/src/locales/es.json
index 905102726..9dd428b32 100644
--- a/apps/host/src/locales/es.json
+++ b/apps/host/src/locales/es.json
@@ -3489,5 +3489,25 @@
"s8643": "Solo se usan tu procesador y tu memoria.",
"s8644": "Para expertos: el motor arranca en las tarjetas {0} (la principal al final).",
"s8645": "No se guarda mientras mantengas la recomendación: Strata elige de nuevo si añades o quitas una tarjeta.",
- "s8646": "No se usa: {0} (otra marca de tarjeta gráfica distinta de la que maneja este motor)."
+ "s8646": "No se usa: {0} (otra marca de tarjeta gráfica distinta de la que maneja este motor).",
+ "s9001": "Este mensaje tiene unos {0} tokens y este modelo puede leer unos {1} a la vez. Es demasiado grande para enviarlo tal cual.",
+ "s9002": "Adjúntalo como archivo para que el asistente lo lea por partes, o envía una vista previa acortada.",
+ "s9003": "Esta conversación ya no cabe en la ventana del modelo, y resumir los mensajes antiguos no dejó espacio suficiente.",
+ "s9004": "Resúmela ahora y sigue, o empieza un chat nuevo.",
+ "s9005": "Resumiendo mensajes anteriores…",
+ "s9006": "Se resumieron los mensajes antiguos para hacer sitio. El chat estaba al {0} % y ahora está al {1} %.",
+ "s9007": "Se resumieron los mensajes antiguos para hacer sitio. El chat estaba al {0} %.",
+ "s9008": "Se resumieron los mensajes antiguos para hacer sitio.",
+ "s9009": "Este chat está al {0} %, pero no hay nada antiguo que resumir: el mensaje más reciente es lo que lo llena. Empieza un chat nuevo o envía un mensaje más corto.",
+ "s9010": "Todavía no hay nada antiguo que resumir. El asistente aún necesita todo lo que hay en este chat.",
+ "s9011": "Todavía no hay nada que resumir. Este chat aún no ha empezado una conversación con el asistente.",
+ "s9012": "No se pudieron resumir los mensajes antiguos ahora mismo. Inténtalo de nuevo en un momento o empieza un chat nuevo.",
+ "s9013": "Este chat sigue trabajando. Deténlo o espera a que termine y vuelve a intentarlo.",
+ "s9014": "Adjuntar como archivo",
+ "s9015": "Enviar una vista previa acortada",
+ "s9016": "Resumir y continuar",
+ "s9017": "Empezar un chat nuevo",
+ "s9018": "No se pudo preparar el mensaje. Prueba con un mensaje más corto.",
+ "s9019": "La última sesión de Strata todavía se está cerrando.",
+ "s9020": "Espera un momento y vuelve a intentarlo. Si sigue pasando, reinicia el ordenador."
}
diff --git a/apps/host/src/locales/ja.json b/apps/host/src/locales/ja.json
index eeddc975a..5cdabe14b 100644
--- a/apps/host/src/locales/ja.json
+++ b/apps/host/src/locales/ja.json
@@ -3489,5 +3489,25 @@
"s8643": "プロセッサとメモリのみを使います。",
"s8644": "上級者向け:エンジンはカード {0} で起動します(メインが最後)。",
"s8645": "おすすめのままの間は保存されません。カードを追加・取り外すと Strata が選び直します。",
- "s8646": "未使用:{0}(このエンジンが対応するのとは別メーカーのカードです)。"
+ "s8646": "未使用:{0}(このエンジンが対応するのとは別メーカーのカードです)。",
+ "s9001": "このメッセージは約{0}トークンで、このモデルは一度に約{1}トークンまで読めます。このまま送るには大きすぎます。",
+ "s9002": "ファイルとして添付してアシスタントに分けて読んでもらうか、短くしたプレビューを送ってください。",
+ "s9003": "この会話はモデルのウィンドウに収まらなくなり、古いメッセージを要約しても十分な空きができませんでした。",
+ "s9004": "今すぐ要約して続けるか、新しいチャットを始めてください。",
+ "s9005": "これまでのメッセージを要約しています…",
+ "s9006": "空きを作るために古いメッセージを要約しました。チャットは{0}%埋まっていましたが、現在は{1}%です。",
+ "s9007": "空きを作るために古いメッセージを要約しました。チャットは{0}%埋まっていました。",
+ "s9008": "空きを作るために古いメッセージを要約しました。",
+ "s9009": "このチャットは{0}%埋まっていますが、要約できる古いメッセージはありません。最新のメッセージで埋まっています。新しいチャットを始めるか、もっと短いメッセージを送ってください。",
+ "s9010": "まだ要約できる古いメッセージはありません。このチャットの内容はどれもアシスタントがまだ必要としています。",
+ "s9011": "まだ要約するものがありません。このチャットではアシスタントとの会話が始まっていません。",
+ "s9012": "今は古いメッセージを要約できませんでした。しばらくしてからもう一度試すか、新しいチャットを始めてください。",
+ "s9013": "このチャットはまだ作業中です。停止するか、終わるのを待ってからもう一度試してください。",
+ "s9014": "ファイルとして添付",
+ "s9015": "短くしたプレビューを送る",
+ "s9016": "要約して続ける",
+ "s9017": "新しいチャットを始める",
+ "s9018": "メッセージを準備できませんでした。もっと短いメッセージを試してください。",
+ "s9019": "前回の Strata セッションがまだ終了処理中です。",
+ "s9020": "しばらく待ってからもう一度試してください。繰り返し起こる場合はコンピューターを再起動してください。"
}
diff --git a/apps/host/src/locales/ko.json b/apps/host/src/locales/ko.json
index ea1aded1a..ae8bb8c5e 100644
--- a/apps/host/src/locales/ko.json
+++ b/apps/host/src/locales/ko.json
@@ -3489,5 +3489,25 @@
"s8643": "프로세서와 메모리만 사용합니다.",
"s8644": "전문가용: 엔진은 카드 {0}에서 시작합니다 (메인이 마지막).",
"s8645": "추천 설정을 유지하는 동안은 저장되지 않습니다. 카드를 추가하거나 빼면 Strata가 다시 고릅니다.",
- "s8646": "사용 안 함: {0} (이 엔진이 다루는 것과 다른 제조사의 그래픽 카드입니다)."
+ "s8646": "사용 안 함: {0} (이 엔진이 다루는 것과 다른 제조사의 그래픽 카드입니다).",
+ "s9001": "이 메시지는 약 {0}토큰이고, 이 모델은 한 번에 약 {1}토큰까지 읽을 수 있습니다. 그대로 보내기에는 너무 큽니다.",
+ "s9002": "파일로 첨부해 어시스턴트가 나눠 읽게 하거나, 줄인 미리보기를 보내세요.",
+ "s9003": "이 대화는 더 이상 모델의 창에 맞지 않으며, 오래된 메시지를 요약해도 충분한 공간이 나오지 않았습니다.",
+ "s9004": "지금 요약하고 계속하거나, 새 채팅을 시작하세요.",
+ "s9005": "이전 메시지를 요약하는 중…",
+ "s9006": "공간을 만들기 위해 오래된 메시지를 요약했습니다. 채팅이 {0}% 찼던 것이 지금은 {1}%입니다.",
+ "s9007": "공간을 만들기 위해 오래된 메시지를 요약했습니다. 채팅이 {0}% 찼습니다.",
+ "s9008": "공간을 만들기 위해 오래된 메시지를 요약했습니다.",
+ "s9009": "이 채팅은 {0}% 찼지만 요약할 오래된 메시지가 없습니다. 가장 최근 메시지가 공간을 차지하고 있습니다. 새 채팅을 시작하거나 더 짧은 메시지를 보내세요.",
+ "s9010": "아직 요약할 오래된 메시지가 없습니다. 이 채팅의 내용은 어시스턴트가 아직 모두 필요로 합니다.",
+ "s9011": "아직 요약할 내용이 없습니다. 이 채팅은 아직 어시스턴트와 대화를 시작하지 않았습니다.",
+ "s9012": "지금은 오래된 메시지를 요약할 수 없었습니다. 잠시 후 다시 시도하거나 새 채팅을 시작하세요.",
+ "s9013": "이 채팅은 아직 작업 중입니다. 중지하거나 끝날 때까지 기다린 뒤 다시 시도하세요.",
+ "s9014": "파일로 첨부",
+ "s9015": "줄인 미리보기 보내기",
+ "s9016": "요약하고 계속하기",
+ "s9017": "새 채팅 시작",
+ "s9018": "메시지를 준비하지 못했습니다. 더 짧은 메시지를 시도해 보세요.",
+ "s9019": "지난 Strata 세션이 아직 종료되는 중입니다.",
+ "s9020": "잠시 기다렸다가 다시 시도하세요. 계속 반복되면 컴퓨터를 다시 시작하세요."
}
diff --git a/apps/host/src/locales/pt.json b/apps/host/src/locales/pt.json
index 7787a542c..8d06ebad1 100644
--- a/apps/host/src/locales/pt.json
+++ b/apps/host/src/locales/pt.json
@@ -3489,5 +3489,25 @@
"s8643": "Apenas o processador e a memória são usados.",
"s8644": "Para especialistas: o motor inicia nas placas {0} (a principal por último).",
"s8645": "Não é guardado enquanto mantiver a recomendação: o Strata escolhe de novo se adicionar ou remover uma placa.",
- "s8646": "Não usada: {0} (outra marca de placa de vídeo diferente da que este motor controla)."
+ "s8646": "Não usada: {0} (outra marca de placa de vídeo diferente da que este motor controla).",
+ "s9001": "Esta mensagem tem cerca de {0} tokens e este modelo consegue ler cerca de {1} de cada vez. É grande demais para ser enviada assim.",
+ "s9002": "Anexe-a como arquivo para o assistente ler por partes, ou envie uma prévia reduzida.",
+ "s9003": "Esta conversa já não cabe na janela do modelo, e resumir as mensagens mais antigas não liberou espaço suficiente.",
+ "s9004": "Resuma agora e continue, ou comece um novo chat.",
+ "s9005": "Resumindo as mensagens anteriores…",
+ "s9006": "As mensagens antigas foram resumidas para liberar espaço. O chat estava {0}% cheio e agora está {1}% cheio.",
+ "s9007": "As mensagens antigas foram resumidas para liberar espaço. O chat estava {0}% cheio.",
+ "s9008": "As mensagens antigas foram resumidas para liberar espaço.",
+ "s9009": "Este chat está {0}% cheio, mas não há nada antigo para resumir: a mensagem mais recente é o que o enche. Comece um novo chat ou envie uma mensagem mais curta.",
+ "s9010": "Ainda não há nada antigo para resumir. O assistente ainda precisa de tudo o que está neste chat.",
+ "s9011": "Ainda não há nada para resumir. Este chat ainda não iniciou uma conversa com o assistente.",
+ "s9012": "Não foi possível resumir as mensagens antigas agora. Tente novamente em instantes ou comece um novo chat.",
+ "s9013": "Este chat ainda está trabalhando. Pare-o ou espere terminar e tente novamente.",
+ "s9014": "Anexar como arquivo",
+ "s9015": "Enviar uma prévia reduzida",
+ "s9016": "Resumir e continuar",
+ "s9017": "Começar um novo chat",
+ "s9018": "Não foi possível preparar a mensagem. Tente uma mensagem mais curta.",
+ "s9019": "A última sessão do Strata ainda está sendo encerrada.",
+ "s9020": "Aguarde um momento e tente novamente. Se continuar acontecendo, reinicie o computador."
}
diff --git a/apps/host/src/locales/ru.json b/apps/host/src/locales/ru.json
index ed0d4358d..0ed4f38c6 100644
--- a/apps/host/src/locales/ru.json
+++ b/apps/host/src/locales/ru.json
@@ -3489,5 +3489,25 @@
"s8643": "Используются только процессор и память.",
"s8644": "Для опытных: движок запускается на картах {0} (основная — последняя).",
"s8645": "Не сохраняется, пока вы оставляете рекомендацию: Strata выберет заново, если вы добавите или уберёте карту.",
- "s8646": "Не используется: {0} (другой производитель видеокарт, чем тот, с которым работает этот движок)."
+ "s8646": "Не используется: {0} (другой производитель видеокарт, чем тот, с которым работает этот движок).",
+ "s9001": "Это сообщение состоит примерно из {0} токенов, а эта модель за раз читает примерно {1}. Оно слишком большое, чтобы отправить его как есть.",
+ "s9002": "Прикрепите его как файл, чтобы ассистент прочитал его по частям, или отправьте сокращённый предпросмотр.",
+ "s9003": "Этот разговор больше не помещается в окно модели, а краткое изложение старых сообщений не освободило достаточно места.",
+ "s9004": "Сжать его сейчас и продолжить или начать новый чат.",
+ "s9005": "Кратко пересказываю предыдущие сообщения…",
+ "s9006": "Старые сообщения были кратко пересказаны, чтобы освободить место. Чат был заполнен на {0}%, а теперь на {1}%.",
+ "s9007": "Старые сообщения были кратко пересказаны, чтобы освободить место. Чат был заполнен на {0}%.",
+ "s9008": "Старые сообщения были кратко пересказаны, чтобы освободить место.",
+ "s9009": "Этот чат заполнен на {0}%, но пересказывать старые сообщения нечего: место занимает самое новое сообщение. Начните новый чат или отправьте сообщение покороче.",
+ "s9010": "Пока нечего пересказывать. Всё, что есть в этом чате, ассистенту ещё нужно.",
+ "s9011": "Пока нечего пересказывать. В этом чате ещё не начался разговор с ассистентом.",
+ "s9012": "Старые сообщения сейчас не удалось кратко пересказать. Попробуйте ещё раз через минуту или начните новый чат.",
+ "s9013": "Этот чат ещё работает. Остановите его или дождитесь окончания и попробуйте снова.",
+ "s9014": "Прикрепить как файл",
+ "s9015": "Отправить сокращённый предпросмотр",
+ "s9016": "Сжать и продолжить",
+ "s9017": "Начать новый чат",
+ "s9018": "Не удалось подготовить сообщение. Попробуйте сообщение покороче.",
+ "s9019": "Прошлый сеанс Strata ещё завершается.",
+ "s9020": "Подождите немного и попробуйте снова. Если это повторяется, перезагрузите компьютер."
}
diff --git a/apps/host/src/locales/zh.json b/apps/host/src/locales/zh.json
index f7a00a9df..8c861dec8 100644
--- a/apps/host/src/locales/zh.json
+++ b/apps/host/src/locales/zh.json
@@ -3489,5 +3489,25 @@
"s8643": "只使用处理器和内存。",
"s8644": "专家信息:引擎在显卡 {0} 上启动(主卡在最后)。",
"s8645": "沿用推荐设置时不会保存:增减显卡后 Strata 会重新选择。",
- "s8646": "未使用:{0}(与此引擎驱动的显卡品牌不同)。"
+ "s8646": "未使用:{0}(与此引擎驱动的显卡品牌不同)。",
+ "s9001": "这条消息约有 {0} 个令牌,而这个模型一次大约能读 {1} 个。它太长了,不能原样发送。",
+ "s9002": "把它作为文件附上,让助手分段阅读,或者发送缩短的预览。",
+ "s9003": "这段对话已经放不进模型的窗口,总结较早的消息也没有腾出足够的空间。",
+ "s9004": "现在总结并继续,或者开始一个新的聊天。",
+ "s9005": "正在总结较早的消息…",
+ "s9006": "为腾出空间,较早的消息已被总结。聊天之前已用 {0}%,现在已用 {1}%。",
+ "s9007": "为腾出空间,较早的消息已被总结。聊天之前已用 {0}%。",
+ "s9008": "为腾出空间,较早的消息已被总结。",
+ "s9009": "这个聊天已用 {0}%,但没有更早的消息可总结:占满它的是最新的消息。请开始一个新的聊天,或发送更短的消息。",
+ "s9010": "还没有更早的消息可总结。这个聊天里的内容助手都还需要。",
+ "s9011": "还没有可总结的内容。这个聊天还没有开始和助手的对话。",
+ "s9012": "暂时无法总结较早的消息。请稍后再试,或开始一个新的聊天。",
+ "s9013": "这个聊天仍在工作。请停止它,或等它完成后再试。",
+ "s9014": "作为文件附上",
+ "s9015": "发送缩短的预览",
+ "s9016": "总结并继续",
+ "s9017": "开始新的聊天",
+ "s9018": "无法准备这条消息。请试试更短的消息。",
+ "s9019": "上一次 Strata 会话仍在关闭中。",
+ "s9020": "请稍等片刻再试。如果一直这样,请重启电脑。"
}
diff --git a/apps/host/src/screens/Chat.tsx b/apps/host/src/screens/Chat.tsx
index 2a95cedc1..1abb7a9d1 100644
--- a/apps/host/src/screens/Chat.tsx
+++ b/apps/host/src/screens/Chat.tsx
@@ -28,6 +28,7 @@ import { DropZone, DROP_EVENT, pastedFiles } from '../components/DropZone';
import { ActivityLine, EditedFiles, FileCard, SkillsUsed } from '../components/Deliverables';
import { queueMessage, usePlan, usePlanQuiet } from '../lib/plan';
import { plainFailureText } from '../lib/plain-errors';
+import { contextRecovery } from '../lib/context-recovery';
import { useLiveStatus } from '../lib/live-status';
import { askQuestion, askSub, askIcon } from '../lib/ask';
import { artifactFromUrl, type PreviewArtifact } from '../lib/artifacts';
@@ -638,7 +639,7 @@ function Message({ message: m, isLast, onEditPrompt, onPreview, onOpenLink, onOp
{tr(!(m.timeSkipId && timeSkipReal && !timeSkipDone) && (m.content || m.error || !m.streaming || !liveStatus) ? <div className="st-bubble">
{tr(planCard ? <PlanCard chatId={activeSession} planId={m.planId!}/> : null)}
- {tr(m.error ? (<ErrorNotice message={m.error} next={tr("Try sending the message again.")}/>) : null)}
+ {tr(m.error ? (<ErrorNotice message={m.error} detail={m.errorDetail} {...contextRecovery(m.errorCode)}/>) : null)}
{tr(grouped && !planCard && !m.timeSkipId ? m.blocks!.map((b, i) => b.kind === 'group' ? <ActivityLine key={b.id} group={b} sessionId={activeSession ?? ''} onPreview={onPreview}/> : <Markdown key={i} text={b.text} onOpenLink={onOpenLink} onPreviewHtml={content => onPreview({ id: m.id + '-html-' + i, kind: 'website', title: 'HTML preview', content })}/>) : null)}
{tr(m.content && !planCard && !grouped ? (<Markdown text={m.timeSkipId && !timeSkipReal ? hostLive ? tr('This was a sample run made while the app could not reach its host.') : timeSkipDone ? tr('The result is ready. Switch between Original and Updated, or compare them side by side. This example uses sample outputs and simulated checks.') : tr('Revising the saved draft. Follow the result in the sidebar. This example uses sample outputs and simulated checks.') : m.content} onOpenLink={onOpenLink} onPreviewHtml={content => onPreview({ id: m.id + '-html', kind: 'website', title: 'HTML preview', content })}/>) : !m.error && m.streaming && !liveStatus ? (<span className="typing" aria-label={tr("Generating")}>
<i />
diff --git a/apps/host/tests/agent-compaction.test.mjs b/apps/host/tests/agent-compaction.test.mjs
index 61667be63..9842362a9 100644
--- a/apps/host/tests/agent-compaction.test.mjs
+++ b/apps/host/tests/agent-compaction.test.mjs
@@ -36,7 +36,7 @@ test('a long chat on a small window compacts before it overflows, and the conver
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));
+ replies.push(frames.filter(f => f.type === 'delta' && !f.noteReason).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(' | '));
diff --git a/apps/host/tests/agent-context.test.mjs b/apps/host/tests/agent-context.test.mjs
index 07afa6f02..923f695d4 100644
--- a/apps/host/tests/agent-context.test.mjs
+++ b/apps/host/tests/agent-context.test.mjs
@@ -88,8 +88,8 @@ test('a summary request is split into pieces that fit, each message whole when i
});
/** The scripted model server counting like the engine (3 characters a token here: logs and code) in a 32K window. */
-async function rig(t, { window = 32768, charsPerToken = 3 } = {}) {
- const worker = await startScriptedWorker({ window, charsPerToken });
+async function rig(t, { window = 32768, charsPerToken = 3, noCount = false } = {}) {
+ const worker = await startScriptedWorker({ window, charsPerToken, noCount });
const dir = mkdtempSync(join(tmpdir(), 'strata-ctx-'));
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: window, slots: 2 }], connections: () => [], env: process.env, startTimeoutMs: 90_000 });
@@ -98,7 +98,7 @@ async function rig(t, { window = 32768, charsPerToken = 3 } = {}) {
const harnessLog = () => { try { return readFileSync(join(dir, 'agent', 'harness.log'), 'utf8'); } catch { return ''; } };
return { worker, bridge, send, ws, harnessLog };
}
-const answerOf = frames => frames.filter(f => f.type === 'delta').map(f => f.text).join('');
+const answerOf = frames => frames.filter(f => f.type === 'delta' && !f.noteReason).map(f => f.text).join('');
const dense = 'q=0x1f ok;'; // 10 characters: 2.5 tokens by the Harness price, 3.3 by the model server's count here
test('the plugin loads in the real Harness (a plugin that fails to import would leave compaction as it was, silently)', { skip }, async (t) => {
@@ -126,9 +126,10 @@ test('dense text compacts in time: no request is refused, and every message is a
});
test('a chat the model server refused once is not stuck: the next message compacts (the summary itself fits the window) and goes through', { skip }, async (t) => {
+ // (An engine that cannot count a message before it is sent: the check lets it through, and this is the recovery that remains.)
// 2026-10-22: a 103 KB message made a 70K-token prompt, the 400 left it in the history, and then neither the next message nor /compact
// could go through, because the summary request carried the same history and was refused for its size too.
- const { worker, send } = await rig(t);
+ const { worker, send } = await rig(t, { noCount: true });
assert.equal((await send(`SCRIPT:echo one ${dense.repeat(300)}`)).at(-1).type, 'done');
const huge = await send(`SCRIPT:echo two ${dense.repeat(7000)}`);
assert.equal(huge.at(-1).type, 'done', 'the Harness compacted after the refusal and asked again: ' + JSON.stringify(huge.at(-1)).slice(0, 200));
@@ -141,3 +142,134 @@ test('a chat the model server refused once is not stuck: the next message compac
assert.equal(refused.length, 1, `only the first, too large, prompt was refused: ${refused.map(r => r.refused).join(', ')}`);
assert.ok(worker.requests.filter(r => r.summary).every(r => Math.round(r.promptChars / 3) < 32768 - 64), 'every summary request fitted the window');
});
+
+// ------------------------------------------------------------------------------------------------ the message is checked before it is sent
+
+import { COUNT_ABOVE_CHARS, FIT_TEXT, countTokens, decideFit, fill, oversizedReplacement } from '../server/agent/context-fit.mjs';
+import { TurnProjector, friendlyFailure, isContextFailure } from '../server/agent/events.mjs';
+
+test('fit: a message that fits is sent, one that fits after a summary is summarised first, one that never fits is not sent', () => {
+ // Gyro-S at 64K: 65,536 window, a new chat already holds about 12,000 (system prompt and tools), the reply needs 4,096.
+ assert.equal(decideFit({ window: 65536, pressure: 12000, messageTokens: 5000 }).action, 'send');
+ assert.equal(decideFit({ window: 65536, pressure: 30000, messageTokens: 25000 }).action, 'send', 'it fits with the reply room');
+ assert.equal(decideFit({ window: 65536, pressure: 59048, messageTokens: 5000 }).action, 'summarize', 'the chat is full but the message alone is small');
+ const huge = decideFit({ window: 65536, pressure: 0, messageTokens: 70141 });
+ assert.equal(huge.action, 'too-large', 'the 103 KB message of 2026-10-22 (70,141 tokens) can never fit a 65,536 window');
+ assert.equal(decideFit({ window: 65536, pressure: 0, messageTokens: 49000 }).action, 'send');
+ assert.equal(decideFit({ window: 65536, pressure: 20000, messageTokens: 49000 }).action, 'summarize');
+ assert.equal(decideFit({ window: 65536, pressure: 20000, messageTokens: 50000 }).action, 'too-large', 'with 12,000 for the empty chat and 4,096 for the reply, 49,440 is the most');
+ assert.equal(decideFit({ window: 0, messageTokens: 99999 }).action, 'send', 'no window known: nothing is blocked');
+ assert.equal(decideFit({ window: 8192, pressure: 0, messageTokens: 5000 }).action, 'too-large', 'a small window has less room for a message');
+});
+
+test('count: the engine\'s own number, or nothing when it cannot be had (then nothing is blocked)', async () => {
+ const sent = [];
+ const ok = async (url, init) => { sent.push([url, JSON.parse(init.body), init.headers]); return { ok: true, json: async () => ({ input_tokens: 70141 }) }; };
+ assert.equal(await countTokens({ baseUrl: 'http://127.0.0.1:18500/v1', key: 'k', model: 'm', text: 'hello', fetchImpl: ok }), 70141);
+ assert.equal(sent[0][0], 'http://127.0.0.1:18500/v1/messages/count_tokens');
+ assert.deepEqual(sent[0][1].messages, [{ role: 'user', content: 'hello' }]);
+ assert.equal(sent[0][2].authorization, 'Bearer k');
+ assert.equal(await countTokens({ baseUrl: 'http://x/v1', text: 'a', fetchImpl: async () => ({ ok: false }) }), null);
+ assert.equal(await countTokens({ baseUrl: 'http://x/v1', text: 'a', fetchImpl: async () => { throw new Error('refused'); } }), null);
+ assert.equal(await countTokens({ baseUrl: 'http://x/v1', text: 'a', fetchImpl: async () => ({ ok: true, json: async () => ({}) }) }), null);
+ assert.ok(COUNT_ABOVE_CHARS >= 1000);
+});
+
+test('the two ways out for a message that is too large: a file the assistant reads in parts, or a shortened preview', () => {
+ const text = 'q=0x1f ok;'.repeat(11000); // 110,000 characters
+ const file = oversizedReplacement({ text, mode: 'file', tokens: 36000, window: 32768, path: 'C:\\work\\pasted-message.txt' });
+ assert.match(file.message, /saved it as a file: C:\\work\\pasted-message\.txt/);
+ assert.ok(file.message.length < 2000, 'the file message is short');
+ const preview = oversizedReplacement({ text, mode: 'preview', tokens: 36000, window: 32768, pressure: 12000 });
+ assert.ok(preview.message.length < text.length / 2, 'cut to what the window can hold');
+ assert.match(preview.message, /characters from the middle are left out/);
+ assert.ok(preview.message.startsWith('q=0x1f ok;') && preview.message.endsWith('q=0x1f ok;'), 'the start and the end are kept');
+ const small = oversizedReplacement({ text: 'short', mode: 'preview', tokens: 5, window: 32768 });
+ assert.equal(small.message, 'short');
+ assert.equal(fill(FIT_TEXT.tooLarge, ['70,141', '65,536']), 'This message is about 70,141 tokens long, and this model can read about 65,536 at a time. It is too large to send as it is.');
+});
+
+test('a context failure is told in plain words, with the engine\'s text kept for Details and a code the page acts on', () => {
+ const raw = '400: {"type":"invalid_request_error","message":"request exceeds the context window: prompt (85340 tokens) leaves no room to answer in the context (65536); requests are never truncated"}';
+ assert.ok(isContextFailure({ message: raw }));
+ assert.ok(isContextFailure({ code: 'CONTEXT_WINDOW_EXCEEDED', message: 'x' }));
+ assert.ok(!isContextFailure({ message: 'fetch failed' }));
+ const said = friendlyFailure({ message: raw });
+ assert.equal(said, FIT_TEXT.full);
+ assert.doesNotMatch(said, /\{|invalid_request|400/, 'no raw JSON in what the person reads');
+ const projector = new TurnProjector();
+ projector.consume({ type: 'event', event: { type: 'turn/end', data: { reason: { kind: 'error', error: { message: raw } } } } });
+ const [frame] = projector.finish({ model: 'm', contextMax: 65536 });
+ assert.equal(frame.type, 'error');
+ assert.equal(frame.code, 'CONTEXT_WINDOW_EXCEEDED');
+ assert.equal(frame.message, FIT_TEXT.full);
+ assert.equal(frame.details, raw);
+});
+
+test('compaction shows in the chat: a status line while it runs and one plain line when it is done, with how full the chat was', () => {
+ const projector = new TurnProjector({ window: 65536 });
+ const statuses = [];
+ projector.onStatus = u => statuses.push(u);
+ const usage = { type: 'assistant-stream', frame: { type: 'chunk', attemptId: 'a', chunk: { type: 'usage', usage: { inputTokens: 58000, outputTokens: 1048 } } } };
+ const live = projector.consume(usage);
+ assert.deepEqual(live.find(f => f.type === 'context'), { type: 'context', contextTokens: 59048, contextMax: 65536 }, 'the page is told what the context holds after every request');
+ assert.deepEqual(projector.consume({ type: 'event', event: { type: 'compaction/start', data: { compactionId: 'c' } } }), []);
+ assert.equal(statuses.at(-1).code, 'compacting');
+ assert.equal(statuses.at(-1).text, FIT_TEXT.summarizing);
+ const done = projector.consume({ type: 'event', event: { type: 'compaction/end', data: { compactionId: 'c' } } });
+ assert.equal(done.length, 1);
+ assert.equal(done[0].key, FIT_TEXT.summarizedWas);
+ assert.deepEqual(done[0].values, [90]);
+ assert.match(done[0].text, /Older messages were summarized to make room\. The chat was 90% full\./);
+ assert.equal(done[0].own, true);
+ // A summary that failed says nothing here (the turn's own error does), and an end with no start (an older log) says nothing.
+ const failed = new TurnProjector({ window: 65536 });
+ failed.consume({ type: 'event', event: { type: 'compaction/start', data: {} } });
+ assert.deepEqual(failed.consume({ type: 'event', event: { type: 'compaction/end', data: { error: 'x' } } }), []);
+ assert.deepEqual(new TurnProjector().consume({ type: 'event', event: { type: 'compaction/end', data: {} } }), []);
+});
+
+test('a message too large for the window is not sent: the person is told plainly and nothing reaches the model', { skip }, async (t) => {
+ const { worker, send } = await rig(t);
+ const frames = await send(`SCRIPT:echo big ${dense.repeat(11000)}`); // 110,000 characters: about 36,000 tokens in a 32,768 window
+ const last = frames.at(-1);
+ assert.equal(last.type, 'error');
+ assert.equal(last.code, 'CONTEXT_MESSAGE_TOO_LARGE');
+ assert.match(last.message, /^This message is about 3\d,\d{3} tokens long, and this model can read about 32,768 at a time/);
+ assert.equal(last.key, FIT_TEXT.tooLarge);
+ assert.equal(worker.requests.length, 0, 'no prompt was sent to the model');
+ assert.equal(worker.counts.length, 1, 'the model server counted it once');
+});
+
+test('a message that fits only after a summary is sent after one, and the chat says so', { skip }, async (t) => {
+ const { worker, send } = await rig(t);
+ for (let i = 0; i < 2; i++) assert.equal((await send(`SCRIPT:echo part${i} ${dense.repeat(900)}`)).at(-1).type, 'done');
+ const before = worker.requests.length;
+ const frames = await send(`SCRIPT:echo then ${dense.repeat(4000)}`); // 40,000 characters: about 13,000 tokens on top of about 18,000
+ assert.equal(frames.at(-1).type, 'done', JSON.stringify(frames.at(-1)).slice(0, 300));
+ const note = frames.find(f => f.type === 'delta' && f.noteReason === 'compacted');
+ assert.ok(note, 'a line says the older messages were summarized');
+ assert.match(note.text, /summarized to make room/);
+ const later = worker.requests.slice(before);
+ const firstAnswer = later.findIndex(r => !r.summary);
+ assert.ok(later.slice(0, firstAnswer).some(r => r.summary), 'the summary was asked for before the message was sent');
+ assert.equal(worker.requests.filter(r => r.refused).length, 0, 'nothing was refused');
+});
+
+test('/compact summarises the older messages with the real figures, and says so plainly when there is nothing to summarise', { skip }, async (t) => {
+ const { worker, bridge, ws } = await rig(t);
+ const run = async (line) => { const frames = []; await bridge.compactCommand({ chatId: 'c', message: line, workspacePath: ws, mode: 'bypass', choice: null, onFrame: f => frames.push(f) }); return frames; };
+ const fresh = await run('/compact');
+ assert.equal(fresh.find(f => f.type === 'delta').key, FIT_TEXT.noChatYet, 'a chat that has not started has nothing to summarise');
+ const frames = [];
+ for (let i = 0; i < 3; i++) { await bridge.runTurn({ chatId: 'c', message: `SCRIPT:echo r${i} ${dense.repeat(700)}`, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) }); }
+ const done = await run('/compact');
+ const said = done.find(f => f.type === 'delta');
+ assert.equal(said.key, FIT_TEXT.summarized, 'the figures are the measured ones: ' + said.text);
+ const [was, now] = said.values;
+ assert.ok(was > now && now > 0 && was <= 100, `the chat was ${was}% full and is now ${now}%`);
+ assert.equal(done.at(-1).type, 'done');
+ assert.ok(worker.requests.some(r => r.summary));
+ const again = await run('/compact');
+ assert.ok([FIT_TEXT.nothingToSummarize, FIT_TEXT.nothingFull, FIT_TEXT.summarized].includes(again.find(f => f.type === 'delta').key), 'a second /compact is answered in plain words');
+});
diff --git a/apps/host/tests/context-ui.test.mjs b/apps/host/tests/context-ui.test.mjs
new file mode 100644
index 000000000..8f68f642d
--- /dev/null
+++ b/apps/host/tests/context-ui.test.mjs
@@ -0,0 +1,119 @@
+// The page side of the context fixes (2026-10-22 hands-on test): what the error card offers, what the context meter reads while a turn runs,
+// and that every sentence the host or the page says about the context is in the catalog in all eight languages.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { readFileSync } from 'node:fs';
+import { build } from 'esbuild';
+import { fileURLToPath } from 'node:url';
+import { FIT_TEXT } from '../server/agent/context-fit.mjs';
+
+const here = rel => fileURLToPath(new URL(rel, import.meta.url));
+const read = rel => readFileSync(here(rel), 'utf8').replace(/\r\n/g, '\n');
+globalThis.window ??= globalThis; globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };
+globalThis.navigator ??= { language: 'en' };
+
+async function load(file, stubs = {}) {
+ const result = await build({
+ entryPoints: [here(`../src/lib/${file}`)], bundle: true, format: 'esm', platform: 'node', write: false, logLevel: 'silent', loader: { '.json': 'json' },
+ plugins: [{ name: 'stubs', setup(b) { b.onResolve({ filter: /^\.\/(store)$/ }, () => ({ path: 'store', namespace: 'stub' })); b.onLoad({ filter: /.*/, namespace: 'stub' }, () => ({ contents: stubs.store, loader: 'js' })); } }],
+ });
+ return import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
+}
+
+const calls = [];
+globalThis.__calls = calls;
+const recovery = await load('context-recovery.ts', {
+ store: `export const sendOversized = (mode) => globalThis.__calls.push(['oversized', mode]);
+ export const summarizeAndContinue = () => globalThis.__calls.push(['summarize']);
+ export const startNewChat = () => globalThis.__calls.push(['new-chat']);`,
+});
+const { chatMetrics } = await load('chat-metrics.ts');
+
+test('a conversation that no longer fits offers "Summarize and continue" and "Start a new chat", never "try again"', () => {
+ const r = recovery.contextRecovery('CONTEXT_WINDOW_EXCEEDED');
+ assert.equal(r.action, 'Summarize and continue');
+ assert.equal(r.secondary.label, 'Start a new chat');
+ assert.match(r.next, /Summarize it now and continue, or start a new chat\./);
+ assert.doesNotMatch(r.next, /again/i);
+ calls.length = 0;
+ r.onRetry(); r.secondary.onClick();
+ assert.deepEqual(calls, [['summarize'], ['new-chat']]);
+});
+
+test('a message that is too large offers the two ways that fit: a file the assistant reads in parts, or a shortened preview', () => {
+ const r = recovery.contextRecovery('CONTEXT_MESSAGE_TOO_LARGE');
+ assert.equal(r.action, 'Attach as a file');
+ assert.equal(r.secondary.label, 'Send a shortened preview');
+ calls.length = 0;
+ r.onRetry(); r.secondary.onClick();
+ assert.deepEqual(calls, [['oversized', 'file'], ['oversized', 'preview']]);
+});
+
+test('any other error keeps the card as it was: one sentence, no buttons of ours', () => {
+ for (const code of [undefined, 'UNKNOWN', 'AGENT_STOPPED']) {
+ const r = recovery.contextRecovery(code);
+ assert.equal(r.next, 'Try sending the message again.');
+ assert.equal(r.onRetry, undefined);
+ assert.equal(r.secondary, undefined);
+ }
+});
+
+test('the chat shows the error card with Details and the recovery buttons, and the card can hold a second button', () => {
+ const chat = read('../src/screens/Chat.tsx');
+ assert.match(chat, /<ErrorNotice message=\{m\.error\} detail=\{m\.errorDetail\} \{\.\.\.contextRecovery\(m\.errorCode\)\}\/>/);
+ const ui = read('../src/components/ui.tsx');
+ assert.match(ui, /secondary\?: \{ label: string; onClick: \(\) => void \}/);
+ assert.match(ui, /<Btn size="sm" variant="ghost" onClick=\{secondary\.onClick\}>/);
+});
+
+test('the store keeps the code and the engine\'s text of an error, shows a sentence the host wrote in the person\'s language, and follows the context live', () => {
+ const store = read('../src/lib/store.ts');
+ assert.match(store, /typeof frame\.key === 'string' \? tr\(frame\.key,/);
+ assert.match(store, /errorCode: frame\.code/);
+ assert.match(store, /errorDetail: frame\.details/);
+ assert.match(store, /type === 'context'/);
+ const summarize = store.slice(store.indexOf('export async function summarizeAndContinue'), store.indexOf('export async function sendOversized'));
+ assert.ok(summarize.indexOf("sendMessage('/compact')") > 0 && summarize.indexOf('sendMessage(failed.prompt)') > summarize.indexOf("sendMessage('/compact')"), 'summarise first, then ask the same question again');
+ assert.ok(summarize.indexOf('dropFailedExchange') < summarize.indexOf("sendMessage('/compact')"), 'the failed question is not shown twice');
+ const oversized = store.slice(store.indexOf('export async function sendOversized'), store.indexOf('export function startNewChat'));
+ assert.match(oversized, /'\/api\/chat\/oversize'/);
+ assert.ok(oversized.indexOf('dropFailedExchange') > oversized.indexOf("'/api/chat/oversize'"), 'the failed message stays until the shorter one is ready');
+});
+
+const reply = (extra) => ({ id: 'r', role: 'assistant', content: '', at: '2026-10-22T12:00:00Z', ...extra });
+const session = messages => ({ id: 's', title: 't', createdAt: '', updatedAt: '', messages, tokensIn: 0, tokensOut: 0, statsAvailable: true });
+
+test('the context meter follows the turn that is running: a tool step does not read as 1%', () => {
+ // The 2026-10-22 chat: an earlier reply left the meter at 1% (about 600 tokens), then a turn with tools ran on a 65,536 window.
+ const earlier = reply({ id: 'a', inputTokens: 590, tokens: 20, contextUsed: 610, contextMax: 65536 });
+ const running = reply({ id: 'b', streaming: true, contextMax: 65536 });
+ const before = chatMetrics(session([earlier, running]), null, 65536);
+ assert.equal(Math.round(before.pct), 1, 'with nothing reported yet for the turn, the earlier reply is all there is');
+ const stepped = reply({ id: 'b', streaming: true, contextMax: 65536, contextUsed: 43000 });
+ const during = chatMetrics(session([earlier, stepped]), null, 65536);
+ assert.equal(during.used, 43000);
+ assert.equal(Math.round(during.pct), 66);
+ const monitor = { phase: 'idle', context: { sessionId: 's', usedTokens: 600, maxTokens: 65536 } };
+ assert.equal(chatMetrics(session([earlier, stepped]), monitor, 65536).used, 43000, 'the engine\'s figure for the request in flight does not override the turn\'s own');
+ const done = reply({ id: 'b', inputTokens: 43000, tokens: 90, contextUsed: 43090, contextMax: 65536 });
+ assert.equal(chatMetrics(session([earlier, done]), null, 65536).used, 43090, 'when the turn has ended its final figure stands');
+});
+
+test('every sentence about the context is in the catalog in all eight languages, with the same slots', () => {
+ const langs = ['en', 'zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko'];
+ const catalogs = Object.fromEntries(langs.map(l => [l, JSON.parse(read(`../src/locales/${l}.json`))]));
+ const idOf = text => Object.entries(catalogs.en).find(([, v]) => v === text)?.[0];
+ const sentences = [...Object.values(FIT_TEXT), 'Attach as a file', 'Send a shortened preview', 'Summarize and continue', 'Start a new chat',
+ 'The message could not be prepared. Try a shorter message.', 'The last Strata session is still closing down.', 'Wait a moment and try again. If it keeps happening, restart the computer.'];
+ const slots = t => (t.match(/\{\d+\}/g) ?? []).sort().join();
+ for (const text of sentences) {
+ const id = idOf(text);
+ assert.ok(id, `"${text.slice(0, 60)}" is in the English catalog`);
+ for (const l of langs) {
+ const t = catalogs[l][id];
+ assert.ok(typeof t === 'string' && t.trim(), `${id} in ${l}`);
+ assert.equal(slots(t), slots(text), `${id} in ${l} has the same slots`);
+ if (l !== 'en' && text.length > 25) assert.notEqual(t, text, `${id} is translated in ${l}`);
+ }
+ }
+});
diff --git a/apps/host/tests/helpers/scripted-worker.mjs b/apps/host/tests/helpers/scripted-worker.mjs
index 34c07490c..8840b0b13 100644
--- a/apps/host/tests/helpers/scripted-worker.mjs
+++ b/apps/host/tests/helpers/scripted-worker.mjs
@@ -37,13 +37,14 @@ import { createHash } from 'node:crypto';
const delay = ms => new Promise(r => setTimeout(r, ms));
-export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = 'scripted-model', serial = false, window = 0, charsPerToken = 0 } = {}) {
+export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = 'scripted-model', serial = false, window = 0, charsPerToken = 0, noCount = false } = {}) {
// `window` + `charsPerToken` make it behave like the engine: every request's prompt is counted (its size over charsPerToken), the usage it
// reports is that count, and a prompt that leaves no room to answer is refused with the engine's own 400 ("requests are never truncated").
// `serial` stands in for an engine with one request slot: the next request waits until the one before it has finished.
let tail = Promise.resolve();
const turn = () => { let release; const mine = new Promise(r => { release = r; }); const wait = tail; tail = tail.then(() => mine); return wait.then(() => release); };
const requests = [];
+ const counts = [];
let aborted = 0;
const server = createServer(async (req, res) => {
const url = new URL(req.url, 'http://x');
@@ -51,6 +52,16 @@ export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model =
if (req.method === 'GET' && url.pathname === '/health') return json(200, { ok: true });
if (req.method === 'GET' && url.pathname === '/v1/models') return json(200, { object: 'list', data: [{ id: model, object: 'model' }] });
if (req.method === 'GET' && url.pathname === '/__requests') return json(200, { requests, aborted });
+ // The engine's own token count of a message (Anthropic style): characters over the density, plus the template's few tokens.
+ if (req.method === 'POST' && url.pathname.endsWith('/messages/count_tokens')) {
+ if (noCount) return json(404, { error: 'no count here' }); // an older engine: the message cannot be counted before it is sent
+ let text = '';
+ for await (const c of req) text += c;
+ const sent = JSON.parse(text);
+ const chars = (sent.messages ?? []).reduce((n, m) => n + (typeof m.content === 'string' ? m.content.length : JSON.stringify(m.content).length), 0);
+ counts.push(chars);
+ return json(200, { input_tokens: Math.ceil(chars / (charsPerToken || 4)) + 12 });
+ }
if (req.method !== 'POST' || !url.pathname.endsWith('/chat/completions')) return json(404, { error: 'no route' });
const release = serial ? await turn() : () => {};
res.on('close', release);
@@ -255,7 +266,7 @@ export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model =
return {
port: server.address().port,
url: `http://127.0.0.1:${server.address().port}/v1`,
- requests, get aborted() { return aborted; },
+ requests, counts, get aborted() { return aborted; },
close: () => new Promise(r => { server.closeAllConnections?.(); server.close(r); }),
};
}
diff --git a/apps/host/tests/plain-errors.test.mjs b/apps/host/tests/plain-errors.test.mjs
index ab380f3c0..fe054e84c 100644
--- a/apps/host/tests/plain-errors.test.mjs
+++ b/apps/host/tests/plain-errors.test.mjs
@@ -16,6 +16,7 @@ const cases = [
['The Cloudflare connector (cloudflared) is not installed here, and the app never downloads it while it runs. Install it from the optional connector component.', 'connector-missing', /could not find Cloudflare’s connector/],
['The cloudflared at /usr/bin/cloudflared is version 2021.3.0, which is too old. Strata needs 2022.1.0 or newer. Update cloudflared, then retry.', 'connector-old', /connector on this computer is too old/],
['The file at /x/cloudflared did not run as cloudflared. Reinstall cloudflared, then retry.', 'connector-broken', /did not run/],
+ ['lost host child is still present; retry after it exits', 'previous-session', /last Strata session is still closing down/],
['server_exited', 'start-failed', /model stopped while starting\./],
['server_exited (exit code 0xC0000135)', 'dll-missing', /program files is missing/],
['server_exited (exit code 0xC000007B)', 'dll-bad', /does not fit this computer or is damaged/],