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

Chat store: save the chat list at most once a second, serialise only changed chats, cap the saved copy (#3604, #3603)

Niko12211298a3971ce0
diff --git a/apps/host/src/lib/session-persist.ts b/apps/host/src/lib/session-persist.ts
new file mode 100644
index 000000000..69e6510fd
--- /dev/null
+++ b/apps/host/src/lib/session-persist.ts
@@ -0,0 +1,110 @@
+import type { Session } from './types';
+
+/**
+ * Saving the chat list without hurting the page (#3604, #3603).
+ *
+ * Every streamed token used to stringify every chat and write the lot to localStorage. On a long history that is
+ * megabytes of string work per token, and the WebView2 renderer grew to gigabytes before it died (the window goes
+ * white). Three rules fix it, and the stored format is unchanged, so older builds and the flow scripts still read it:
+ *
+ * 1. Coalesce: the newest state wins and at most one write happens per `delayMs`. The caller flushes when a turn ends
+ * or the page goes away.
+ * 2. Only the chats that changed are serialised again. A chat is replaced by a new object when it changes (the store
+ * never mutates), so the previous JSON of every other chat is reused by object identity.
+ * 3. The stored copy is capped: a huge tool result or an old chat that no longer fits is left out of the saved copy,
+ * never out of memory. The saved list is a cache, not the source of truth.
+ */
+
+/** Longest text kept for one field in the saved copy. */
+export const MAX_FIELD_CHARS = 200_000;
+/** Longest saved copy of one chat (JSON characters). A longer chat is shortened from its oldest messages. */
+export const MAX_SESSION_CHARS = 1_500_000;
+/** Longest saved chat list (JSON characters). localStorage allows about 5M; keep a margin for everything else. */
+export const MAX_TOTAL_CHARS = 3_500_000;
+
+type Json = unknown;
+
+function clipFields(value: Json, depth = 0): Json {
+ if (typeof value === 'string') return value.length > MAX_FIELD_CHARS ? `${value.slice(0, MAX_FIELD_CHARS)}\n[shortened in the saved copy]` : value;
+ if (depth > 6 || value === null || typeof value !== 'object') return value;
+ if (Array.isArray(value)) return value.map(v => clipFields(v, depth + 1));
+ const out: Record<string, Json> = {};
+ for (const [k, v] of Object.entries(value as Record<string, Json>)) out[k] = clipFields(v, depth + 1);
+ return out;
+}
+
+/** The JSON text kept for one chat: whole when it fits, otherwise clipped and then shortened from the oldest message. */
+export function serializeSession(session: Session): string {
+ const whole = JSON.stringify(session);
+ if (whole.length <= MAX_SESSION_CHARS) return whole;
+ let copy = clipFields(session) as Session;
+ let text = JSON.stringify(copy);
+ if (text.length <= MAX_SESSION_CHARS) return text;
+ let messages = copy.messages ?? [];
+ // Drop the oldest messages in halves until it fits; always keep the latest two.
+ while (text.length > MAX_SESSION_CHARS && messages.length > 2) {
+ messages = messages.slice(Math.max(1, Math.floor(messages.length / 2)));
+ copy = { ...copy, messages };
+ text = JSON.stringify(copy);
+ }
+ return text;
+}
+
+export interface SessionWriterOptions {
+ /** Where the list is saved (the storage write). Receives the finished JSON text. */
+ write: (json: string) => void;
+ delayMs?: number;
+ maxTotalChars?: number;
+ /** Counts a serialisation of one chat; for tests. */
+ onSerialize?: (session: Session) => void;
+}
+
+export function createSessionWriter(options: SessionWriterOptions) {
+ const delayMs = options.delayMs ?? 1_000;
+ const maxTotal = options.maxTotalChars ?? MAX_TOTAL_CHARS;
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ let pending: Session[] | null = null;
+ // Chat object -> its JSON. WeakMap: a replaced chat is forgotten with its object.
+ const cache = new WeakMap<Session, string>();
+
+ const textOf = (s: Session): string => {
+ let text = cache.get(s);
+ if (text === undefined) {
+ options.onSerialize?.(s);
+ text = serializeSession(s);
+ cache.set(s, text);
+ }
+ return text;
+ };
+
+ const flush = () => {
+ if (timer !== null) { clearTimeout(timer); timer = null; }
+ const sessions = pending;
+ pending = null;
+ if (!sessions) return;
+ const parts = sessions.map(s => ({ s, text: textOf(s) }));
+ let total = parts.reduce((n, p) => n + p.text.length + 1, 2);
+ if (total > maxTotal) {
+ // Leave the oldest unpinned chats out of the saved copy until the rest fits. The newest chat is always kept.
+ const order = parts
+ .map((p, i) => ({ i, p }))
+ .sort((a, b) => Number(!!a.p.s.pinned) - Number(!!b.p.s.pinned) || (a.p.s.updatedAt < b.p.s.updatedAt ? -1 : 1));
+ const drop = new Set<number>();
+ for (const { i, p } of order) {
+ if (total <= maxTotal || drop.size >= parts.length - 1) break;
+ drop.add(i);
+ total -= p.text.length + 1;
+ }
+ options.write(`[${parts.filter((_, i) => !drop.has(i)).map(p => p.text).join(',')}]`);
+ return;
+ }
+ options.write(`[${parts.map(p => p.text).join(',')}]`);
+ };
+
+ const schedule = (sessions: Session[]) => {
+ pending = sessions;
+ if (timer === null) timer = setTimeout(flush, delayMs);
+ };
+
+ return { schedule, flush, hasPending: () => pending !== null };
+}
diff --git a/apps/host/src/lib/store.ts b/apps/host/src/lib/store.ts
index 14b31d8c9..b08653d40 100644
--- a/apps/host/src/lib/store.ts
+++ b/apps/host/src/lib/store.ts
@@ -19,6 +19,7 @@ import { noModelNotice } from './no-model';
import { taskHandoverMessage, taskHandoverTitle, chatHandoverMessage, chatHandoverTitle } from './task-handover';
import type { Task } from './tasks';
import { postWithFolderFallback } from './folder-pick';
+import { createSessionWriter } from './session-persist';
import { setTranslationLanguage, tr, validLanguage, type Language } from './i18n';
/**
* Strata app store.
@@ -1008,12 +1009,33 @@ export async function scopeSkill(id: string, workspaceId: string | null) {
/* ---------------------------------------------------------------- sessions */
-function persistSessions(sessions: Session[]) {
- try {
- localStorage.setItem(SESSION_KEY, JSON.stringify(sessions));
- } catch {
- /* quota — sessions are a cache, not the source of truth */
- }
+const sessionWriter = createSessionWriter({
+ write: (json) => {
+ try {
+ localStorage.setItem(SESSION_KEY, json);
+ } catch {
+ /* quota — sessions are a cache, not the source of truth */
+ }
+ },
+});
+
+/**
+ * Save the chat list. A one-off change (new, rename, delete, pin) is written now; `soon` is for the streaming
+ * path, where a write per token grew the renderer to gigabytes (#3604, #3603): those are coalesced to one a second.
+ */
+function persistSessions(sessions: Session[], soon = false) {
+ sessionWriter.schedule(sessions);
+ if (!soon) sessionWriter.flush();
+}
+
+/** Write the newest chat list now: a turn ended, or the page is going away. */
+export function flushSessions() {
+ sessionWriter.flush();
+}
+
+if (typeof window !== 'undefined' && typeof document !== 'undefined' && window.addEventListener) {
+ window.addEventListener('pagehide', flushSessions);
+ document.addEventListener?.('visibilitychange', () => { if (document.visibilityState === 'hidden') flushSessions(); });
}
export function newSession(): Session {
@@ -1120,7 +1142,7 @@ export function renameSession(id: string, title: string) {
function patchSession(id: string, fn: (s: Session) => Session) {
const sessions = state.sessions.map((s) => (s.id === id ? fn(s) : s));
- persistSessions(sessions);
+ persistSessions(sessions, true);
set({ sessions });
}
@@ -1756,6 +1778,8 @@ 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,
}));
+ // The final text is in `state`: write it once now instead of waiting for the coalescing timer.
+ flushSessions();
}
},
turnAbort.signal,
@@ -1772,6 +1796,8 @@ export async function sendMessage(text: string, options: { fromLoop?: boolean; g
patchAssistant((m) => ({ ...m, streaming: false }));
}
} finally {
+ // A cancelled or failed turn is still worth keeping.
+ flushSessions();
clearStatus(sessionId);
markTurn(sessionId, null);
const next = releasedMessages.get(sessionId)?.shift();
diff --git a/apps/host/tests/session-persist.test.mjs b/apps/host/tests/session-persist.test.mjs
new file mode 100644
index 000000000..87ee25391
--- /dev/null
+++ b/apps/host/tests/session-persist.test.mjs
@@ -0,0 +1,118 @@
+/** #3604 / #3603: the chat list is saved at most once a second, changed chats only, and capped; a streamed answer is a handful of writes. */
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { build } from 'esbuild';
+import { fileURLToPath } from 'node:url';
+
+const here = (p) => fileURLToPath(new URL(p, import.meta.url));
+
+async function bundle(entry, plugins = []) {
+ const result = await build({
+ entryPoints: [here(entry)], bundle: true, format: 'esm', platform: 'node', write: false, logLevel: 'silent',
+ alias: { react: here('./helpers/react-stub.mjs') }, loader: { '.json': 'json' }, plugins,
+ });
+ return import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
+}
+
+const persist = await bundle('../src/lib/session-persist.ts');
+const { createSessionWriter, serializeSession, MAX_SESSION_CHARS } = persist;
+
+test('2,000 schedules inside a second produce one write, and the newest state wins', (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] });
+ const writes = [];
+ const writer = createSessionWriter({ write: (json) => writes.push(JSON.parse(json)), delayMs: 1_000 });
+ for (let i = 0; i < 2_000; i++) writer.schedule([{ id: 's1', seq: i, messages: [] }]);
+ assert.equal(writes.length, 0, 'nothing is written synchronously');
+ t.mock.timers.tick(1_000);
+ assert.equal(writes.length, 1);
+ assert.equal(writes[0][0].seq, 1_999);
+ writer.schedule([{ id: 's1', seq: 2_000, messages: [] }]);
+ writer.flush();
+ assert.equal(writes.length, 2);
+ assert.equal(writes[1][0].seq, 2_000);
+ writer.flush();
+ assert.equal(writes.length, 2, 'a flush with nothing pending writes nothing');
+});
+
+test('only the chat that changed is serialised again', () => {
+ const seen = [];
+ const writes = [];
+ const writer = createSessionWriter({ write: (j) => writes.push(j), onSerialize: (s) => seen.push(s.id) });
+ const a = { id: 'a', updatedAt: '1', messages: [{ id: 'm', content: 'x'.repeat(1000) }] };
+ const b = { id: 'b', updatedAt: '2', messages: [] };
+ writer.schedule([a, b]); writer.flush();
+ assert.deepEqual(seen.sort(), ['a', 'b']);
+ seen.length = 0;
+ for (let i = 0; i < 50; i++) { writer.schedule([a, { ...b, title: `t${i}` }]); writer.flush(); }
+ assert.equal(seen.filter((id) => id === 'a').length, 0, 'the untouched chat is never stringified again');
+ assert.equal(seen.filter((id) => id === 'b').length, 50);
+ assert.equal(JSON.parse(writes.at(-1)).length, 2);
+});
+
+test('the saved copy is capped: a huge tool result is shortened, an over-long chat loses its oldest messages, old chats make room', () => {
+ const big = 'y'.repeat(1_600_000);
+ const one = serializeSession({ id: 'a', messages: [{ id: '1', tools: [{ result: big }] }] });
+ assert.ok(one.length < 250_000, 'one 1.6M field is clipped');
+ const many = { id: 'b', messages: Array.from({ length: 40 }, (_, i) => ({ id: String(i), content: 'z'.repeat(150_000) })) };
+ const text = serializeSession(many);
+ assert.ok(text.length <= MAX_SESSION_CHARS);
+ const kept = JSON.parse(text).messages;
+ assert.equal(kept.at(-1).id, '39', 'the latest message is kept');
+
+ const writes = [];
+ const writer = createSessionWriter({ write: (j) => writes.push(JSON.parse(j)), maxTotalChars: 320_000 });
+ const chats = ['old', 'mid', 'new'].map((id, i) => ({ id, updatedAt: String(i), messages: [{ id: 'm', content: 'q'.repeat(150_000) }] }));
+ writer.schedule(chats); writer.flush();
+ assert.deepEqual(writes[0].map((s) => s.id), ['mid', 'new'], 'the oldest chat is left out of the saved copy');
+});
+
+test('the saved list keeps the format older builds read (a JSON array of chats)', () => {
+ let out = '';
+ const w = createSessionWriter({ write: (j) => { out = j; } });
+ w.schedule([{ id: 'a', title: 'A', messages: [] }]); w.flush();
+ assert.deepEqual(JSON.parse(out), [{ id: 'a', title: 'A', messages: [] }]);
+});
+
+/* End to end through the real store: a 1,500-token streamed answer. */
+test('a streamed answer of 1,500 tokens is at most a few storage writes, and the final text is saved', async () => {
+ const writes = [];
+ globalThis.window ??= globalThis;
+ globalThis.document ??= { addEventListener() {}, visibilityState: 'visible', documentElement: { dataset: {}, style: { setProperty() {} }, classList: { toggle() {}, add() {}, remove() {} } } };
+ const memory = new Map();
+ globalThis.localStorage = {
+ getItem: (k) => memory.get(k) ?? null,
+ setItem: (k, v) => { if (k === 'strata.sessions.v1') writes.push(v); memory.set(k, v); },
+ removeItem: (k) => memory.delete(k),
+ };
+ globalThis.navigator ??= { language: 'en' };
+ globalThis.matchMedia ??= () => ({ matches: false, addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {} });
+ const TOKENS = 1_500;
+ const mockClient = {
+ name: 'mock-client',
+ setup(b) {
+ b.onResolve({ filter: /\/client$/ }, () => ({ path: 'mock-client', namespace: 'mock' }));
+ b.onLoad({ filter: /.*/, namespace: 'mock' }, () => ({
+ loader: 'js',
+ contents: `
+ export class ApiError extends Error {}
+ const reject = async () => { throw new Error('offline'); };
+ export const api = { get: reject, post: async () => ({}), put: reject, patch: reject, del: reject, delete: reject };
+ export function onEvent() { return () => {}; }
+ export async function streamPost(path, body, onFrame) {
+ onFrame({ type: 'start', model: 'm', contextMax: 8192, requestId: 'r1' });
+ for (let i = 0; i < ${TOKENS}; i++) onFrame({ type: 'delta', text: 'word ' });
+ onFrame({ type: 'done', tokensIn: 10, tokensOut: ${TOKENS}, durationMs: 100, usageSource: 'reported' });
+ }`,
+ }));
+ },
+ };
+ const store = await bundle('../src/lib/store.ts', [mockClient]);
+ const before = writes.length;
+ await store.sendMessage('Write a long answer');
+ const during = writes.length - before;
+ assert.ok(during <= 6, `a 1,500-token answer wrote the chat list ${during} times`);
+ const saved = JSON.parse(writes.at(-1));
+ const answer = saved[0].messages.find((m) => m.role === 'assistant');
+ assert.ok(answer.content.length >= TOKENS * 5 - 10, 'the finished answer is what was saved');
+ assert.equal(answer.streaming, false);
+});