per-session models: every session owns its model + thinking level from birth

The reported bug (2026-09-28): every chat ran glm-5.3-flash regardless of
any pick, while composer and settings UIs agreed with each other and lied
about the run. Root cause: a fossil default-settings record (settings.json
chat section, frozen pre-slots-wave) was stamped verbatim into every new
chat at birth by the new-session flow, and resolveConversationSpec prefers
the per-chat record over the strong slot forever.

Owner redesign — models are per session, always; the strong slot is the
new-session default:

- POST /api/chats stamps the newborn verbatim from the strong slot
  (model + thinking level); no client settings PUT on create anymore
- the composer control is dual-bound: in-session it reads/writes the
  session's own record (GET /api/chats/:id, PUT .../settings); on the
  draft surfaces it binds the strong slot (picking there = changing the
  default)
- thinking levels follow the identical mechanics (confirmed by owner)
- the zombie default-settings system dies whole: useDefaultSettings,
  /api/settings/defaults, src/store/default-settings.ts, the chat
  section of UserSettings (sanitizer strips fossil copies), the legacy
  default-settings.json fold, and DEFAULT_SETTINGS_FILE
- catalogDefaultsFor collapses to the strong slot alone
- thinking display for a legacy unstamped session resolves exactly like
  the engine (session ?? strong ?? model ladder)

Tests: new chat-birth-stamp suite pins the stamp, the unconfigured boot,
the unset level, and the copy-not-live-view regression; defaults pins
rewritten strong-only; legacy-migration suite deleted.
This commit is contained in:
amosblomqvist
2026-09-28 23:30:11 +03:00
parent 1f5e21a674
commit 3e9a85d60f
27 changed files with 336 additions and 491 deletions
+108
View File
@@ -0,0 +1,108 @@
/**
* The BIRTH STAMP (per-session models, owner ruling 2026-09-28): every
* session owns its model + thinking level from birth — POST /api/chats
* stamps the newborn chat verbatim from the settings record's STRONG
* slot, the new-session default. From then on the strong slot never
* reaches the session again (in-session picks write the session's own
* record; resolveConversationSpec prefers chat.settings.model).
*
* This pin drives the actual route handler (framework-free
* Request/Response) under an isolated LH_DATA_DIR — the seam must be set
* before the first import that transitively loads config.ts, so every
* app-module import is dynamic (the prompt-overrides discipline).
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { after, describe, it } from "node:test";
// ── the seam, BEFORE any config-loading import ──────────────────────────────
const TMP_DATA = fs.mkdtempSync(path.join(os.tmpdir(), "lh-birth-stamp-"));
process.env.LH_DATA_DIR = TMP_DATA;
const { POST } = await import("../app/api/chats/route.ts");
const { readSettings, writeSettings } = await import("../src/store/settings.ts");
const { chatStore } = await import("../src/store/chats.ts");
after(() => {
fs.rmSync(TMP_DATA, { recursive: true, force: true });
});
function post(): Promise<Response> {
return POST(new Request("http://test/api/chats", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title: "teach me the chain rule" }),
}));
}
describe("POST /api/chats — the birth stamp", () => {
it("stamps the newborn verbatim from the strong slot (model + thinking level)", async () => {
writeSettings({
version: 1,
models: {
strong: {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "low",
},
},
});
const res = await post();
assert.equal(res.status, 200);
const { id } = (await res.json()) as { id: string };
const record = chatStore.getChat(id);
assert.ok(record);
assert.deepEqual(record.settings, {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "low",
});
});
it("no strong slot configured (fresh BYOK boot) → NO stamp; the turn gate refuses downstream", async () => {
fs.rmSync(path.join(TMP_DATA, "settings.json"), { force: true });
assert.equal(readSettings(), null);
const res = await post();
assert.equal(res.status, 200);
const { id } = (await res.json()) as { id: string };
const record = chatStore.getChat(id);
assert.ok(record);
assert.equal(record.settings, undefined, "nothing stamped — nothing to lie about");
});
it("an unset thinking level rides as-is — the session's level resolves (strong's, then the model's ladder)", async () => {
writeSettings({
version: 1,
models: { strong: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" } } },
});
const res = await post();
const { id } = (await res.json()) as { id: string };
const record = chatStore.getChat(id);
assert.ok(record);
assert.equal(record.settings?.thinkingLevel, undefined);
assert.deepEqual(record.settings, { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" } });
});
it("changing the strong slot afterward NEVER touches existing sessions — the stamp is a copy, not a live view", async () => {
writeSettings({
version: 1,
models: { strong: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low" } },
});
const first = (await (await post()).json()) as { id: string };
writeSettings({
version: 1,
models: { strong: { model: { provider: "openai", id: "gpt-6-luna" }, thinkingLevel: "low" } },
});
const second = (await (await post()).json()) as { id: string };
// The pre-change session keeps its stamp; the post-change session
// carries the new default. This is the regression pin for the
// reported bug (2026-09-28): both UIs showed the strong slot while
// every chat ran a fossil stamp.
const firstRecord = chatStore.getChat(first.id);
const secondRecord = chatStore.getChat(second.id);
assert.ok(firstRecord && secondRecord);
assert.equal(firstRecord.settings?.model?.id, "z-ai/glm-5.3-flash");
assert.equal(secondRecord.settings?.model?.id, "gpt-6-luna");
});
});
-54
View File
@@ -1,54 +0,0 @@
/**
* Tests for the shared settings-body parser, chat-section flavor
* (default-settings wave, 2026-09-18; record-backed 2026-09-19): the seam
* behind BOTH chat-settings routes — per-chat and the defaults alias.
*
* The record store, the legacy-file migration, and the record-backed
* default-settings facade are covered in tests/settings-store.test.ts
* (one fs owner per file — the test runner executes files in parallel).
*/
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { parseSettingsBody } from "../src/server/settings-body.ts";
describe("parseSettingsBody (shared by both settings routes)", () => {
it("an empty object parses to {} — the clear-overrides write", () => {
const r = parseSettingsBody({});
assert.ok(r.ok);
assert.deepEqual(r.ok ? r.settings : null, {});
});
it("a full valid body parses field-for-field", () => {
const r = parseSettingsBody({
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "high",
});
assert.ok(r.ok);
assert.deepEqual(r.settings, {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "high",
});
});
it("partial bodies keep only the present overrides (replace-whole)", () => {
const r = parseSettingsBody({ thinkingLevel: "low" });
assert.ok(r.ok);
assert.deepEqual(r.settings, { thinkingLevel: "low" });
});
it("rejects non-object bodies", () => {
for (const body of ["junk", 42, [{}]]) {
const r = parseSettingsBody(body);
assert.equal(r.ok, false, JSON.stringify(body));
if (!r.ok) assert.match(r.error, /Invalid JSON body/);
}
});
it("rejects malformed model and non-string thinkingLevel", () => {
assert.equal(parseSettingsBody({ model: "nope" }).ok, false);
assert.equal(parseSettingsBody({ model: { provider: 1, id: "x" } }).ok, false);
assert.equal(parseSettingsBody({ thinkingLevel: 7 }).ok, false);
});
});
+10 -25
View File
@@ -86,11 +86,10 @@ describe("the model catalog projection", () => {
// the record is empty (defaults/slots NULL), on the owner's machine
// configured slots must surface exactly as configured.
const record = readSettings();
const chat = record?.chat;
const strong = effectiveSettings().models.strong;
if (chat?.model || strong) {
assert.ok(defaults, "a configured chain yields a default");
assert.deepEqual(defaults!.model, chat?.model ?? { provider: strong!.provider, id: strong!.id });
if (strong) {
assert.ok(defaults, "a configured strong slot yields a default");
assert.deepEqual(defaults!.model, { provider: strong.provider, id: strong.id });
} else {
assert.equal(defaults, null);
}
@@ -105,29 +104,15 @@ describe("the model catalog projection", () => {
}
});
it("defaults: the chat section over the strong slot; null when both empty (BYOK)", () => {
assert.equal(catalogDefaultsFor(undefined, null), null);
it("defaults: the strong slot verbatim; null when unset (BYOK, per-session models 2026-09-28)", () => {
assert.equal(catalogDefaultsFor(null), null);
const strong = { provider: "openrouter", id: "z-ai/glm-5.3-flash", thinkingLevel: "low" as const };
const d = catalogDefaultsFor(undefined, strong);
const d = catalogDefaultsFor(strong);
assert.deepEqual(d, { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low" });
assert.deepEqual(catalogDefaultsFor({ model: { provider: "openrouter", id: "qwen/qwen3.8-flash" }, thinkingLevel: "high" }, strong), {
model: { provider: "openrouter", id: "qwen/qwen3.8-flash" },
thinkingLevel: "high",
});
// partial: only the model chosen → level falls back to the slot's
const partial = catalogDefaultsFor({ model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } }, strong);
assert.ok(partial);
assert.equal(partial!.thinkingLevel, "low");
// thinkingLevel-only record: model untouched
assert.deepEqual(catalogDefaultsFor({ thinkingLevel: "minimal" }, strong), {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "minimal",
});
// chat.model with NO strong slot: the conversation runs on it alone
assert.deepEqual(
catalogDefaultsFor({ model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } }, null),
{ model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } },
);
// No level stored → the model alone reports (the level resolves at
// engine build from DEFAULT_THINKING_ORDER).
const bare = catalogDefaultsFor({ provider: "openrouter", id: "qwen/qwen3.8-flash" });
assert.deepEqual(bare, { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } });
});
it("toModelDto projects the raw catalog model verbatim", () => {
+8 -25
View File
@@ -5,7 +5,6 @@ import { renderToStaticMarkup } from "react-dom/server";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { DraftShell, startDraftSession } from "../components/new-session.tsx";
import type { ChatSettings } from "../src/core/index.ts";
/**
* The sessionless draft view (/chat/new, owner 2026-09-13): "+ New session"
@@ -28,7 +27,7 @@ function fakeFetch(id: string) {
describe("startDraftSession — the create-on-send contract", () => {
it("creates the session titled with the first message, returns the prompt redirect", async () => {
const { f, calls } = fakeFetch("c_new");
const target = await startDraftSession("teach me orbital mechanics", undefined, f);
const target = await startDraftSession("teach me orbital mechanics", f);
assert.equal(target, "/chat/c_new?prompt=teach%20me%20orbital%20mechanics");
assert.equal(calls.length, 1);
assert.equal(calls[0]!.url, "/api/chats");
@@ -36,26 +35,16 @@ describe("startDraftSession — the create-on-send contract", () => {
assert.deepEqual(calls[0]!.body, { title: "teach me orbital mechanics" });
});
it("persists the lifted pre-chat settings after creation", async () => {
const { f, calls } = fakeFetch("c_2");
const settings: ChatSettings = { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" }, thinkingLevel: "medium" };
await startDraftSession("hello", settings, f);
assert.equal(calls.length, 2);
assert.equal(calls[1]!.url, "/api/chats/c_2/settings");
assert.equal(calls[1]!.method, "PUT");
assert.deepEqual(calls[1]!.body, settings);
});
it("no settings lift → no settings write (a single create call)", async () => {
it("no settings PUT on create — the server stamps the newborn (per-session models, owner 2026-09-28)", async () => {
const { f, calls } = fakeFetch("c_3");
await startDraftSession("hello", undefined, f);
assert.equal(calls.length, 1);
await startDraftSession("hello", f);
assert.equal(calls.length, 1, "a single create call — the birth stamp is server-side");
});
it("empty or whitespace text never creates anything", async () => {
const { f, calls } = fakeFetch("never");
assert.equal(await startDraftSession("", undefined, f), null);
assert.equal(await startDraftSession(" ", undefined, f), null);
assert.equal(await startDraftSession("", f), null);
assert.equal(await startDraftSession(" ", f), null);
assert.equal(calls.length, 0);
});
});
@@ -68,14 +57,12 @@ describe("DraftShell — the sessionless surface", () => {
text=""
onChange={() => {}}
onSend={() => {}}
settings={undefined}
onSettingsSaved={() => {}}
/>,
);
// The chat composer, ready for the first message.
assert.ok(html.includes('data-testid="composer"'));
// The pre-chat model control (fix wave: the strong-slot control —
// app-wide, no per-chat write, no empty-chatId defer).
// The draft's model control binds the strong slot — the
// new-session default (per-session models, owner 2026-09-28).
assert.ok(html.includes('data-testid="stats-model"'));
// No CTA by default: a configured surface stays quiet.
assert.ok(!html.includes('data-testid="setup-cta"'));
@@ -94,8 +81,6 @@ describe("DraftShell — the sessionless surface", () => {
text=""
onChange={() => {}}
onSend={() => {}}
settings={undefined}
onSettingsSaved={() => {}}
setupCta={
<button type="button" data-testid="setup-cta">
Set up your models — chats start after the three picks
@@ -118,8 +103,6 @@ describe("DraftShell — the sessionless surface", () => {
text=""
onChange={() => {}}
onSend={() => {}}
settings={undefined}
onSettingsSaved={() => {}}
/>,
);
assert.ok(!/\/chat\/c[a-z0-9_]+/.test(html));
-3
View File
@@ -24,7 +24,6 @@ describe("resolveSettings", () => {
it("null record (first boot): non-model knobs land on factory defaults; every model view is EMPTY", () => {
const eff = resolveSettings(null);
assert.deepEqual(eff.general, { autoUpdate: true });
assert.deepEqual(eff.chat, {}); // BYOK: no default model, ever
assert.deepEqual(eff.agent.tools, {});
assert.deepEqual(eff.agent.prompts, {});
assert.deepEqual(eff.agent.search, {});
@@ -51,7 +50,6 @@ describe("resolveSettings", () => {
const record: UserSettings = {
version: 1,
general: { autoUpdate: false },
chat: { thinkingLevel: "high" },
agent: {
models: { researcher: { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } } },
tools: { research: false },
@@ -63,7 +61,6 @@ describe("resolveSettings", () => {
};
const eff = resolveSettings(record);
assert.equal(eff.general.autoUpdate, false);
assert.deepEqual(eff.chat, { thinkingLevel: "high" });
assert.deepEqual(eff.agent.models.researcher, {
provider: "openrouter",
id: "qwen/qwen3.8-flash",
+31 -76
View File
@@ -1,28 +1,23 @@
/**
* Tests for the settings STORE (settings-storage wave, 2026-09-19):
* src/store/settings.ts — the record's fs adapter + the LAZY MIGRATION
* from the legacy default-settings.json — and the record-backed
* default-settings facade (src/store/default-settings.ts). Writes the
* real repo data/ files (npm test's shared data/, disposable scratch by
* doctrine) and cleans up after itself; a pre-existing settings.json is
* snapshotted and restored so the wave's own dev state survives the run.
* src/store/settings.ts — the record's fs adapter. Writes the real repo
* data/ files (npm test's shared data/, disposable scratch by doctrine)
* and cleans up after itself; a pre-existing settings.json is snapshotted
* and restored so the worktree's own dev state survives the run.
*
* (The legacy default-settings.json fold and the chat-section facade died
* with per-session models, owner 2026-09-28 — sessions own their model
* from birth, stamped from the strong slot server-side.)
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { after, afterEach, describe, it } from "node:test";
import { DEFAULT_SETTINGS_FILE, SETTINGS_FILE } from "../config.ts";
import { SETTINGS_FILE } from "../config.ts";
import { readSettings, writeSettings } from "../src/store/settings.ts";
import { readDefaultSettings, writeDefaultSettings } from "../src/store/default-settings.ts";
import type { ChatSettings } from "../src/core";
const CHAT_FIXTURE: ChatSettings = {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "low",
};
// Snapshot any pre-existing files; restore in after() so the worktree's
// Snapshot any pre-existing record; restore in after() so the worktree's
// own dev state survives a test run.
let hadSettings = false;
let savedSettings = "";
@@ -30,22 +25,11 @@ try {
savedSettings = fs.readFileSync(SETTINGS_FILE, "utf-8");
hadSettings = true;
} catch {
/* none */
/* first boot — nothing to snapshot */
}
let hadDefaults = false;
let savedDefaults = "";
try {
savedDefaults = fs.readFileSync(DEFAULT_SETTINGS_FILE, "utf-8");
hadDefaults = true;
} catch {
/* none */
}
after(() => {
if (hadSettings) fs.writeFileSync(SETTINGS_FILE, savedSettings, "utf-8");
else fs.rmSync(SETTINGS_FILE, { force: true });
if (hadDefaults) fs.writeFileSync(DEFAULT_SETTINGS_FILE, savedDefaults, "utf-8");
else fs.rmSync(DEFAULT_SETTINGS_FILE, { force: true });
});
afterEach(() => {
@@ -53,9 +37,8 @@ afterEach(() => {
});
describe("the settings store", () => {
it("reads null on first boot (no record, no legacy file)", () => {
it("reads null on first boot (no record)", () => {
fs.rmSync(SETTINGS_FILE, { force: true });
fs.rmSync(DEFAULT_SETTINGS_FILE, { force: true });
assert.equal(readSettings(), null);
});
@@ -66,56 +49,28 @@ describe("the settings store", () => {
assert.deepEqual(out.graph, { enabled: false });
});
it("a chat section on disk is IGNORED — the field died with per-session models (owner, 2026-09-28)", () => {
fs.mkdirSync(path.dirname(SETTINGS_FILE), { recursive: true });
// A fossil `chat` section (the old default-settings record) must not
// resurrect through the sanitizer — the key is simply gone.
fs.writeFileSync(
SETTINGS_FILE,
JSON.stringify({
version: 1,
chat: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low" },
graph: { enabled: true },
}),
"utf-8",
);
const out = readSettings();
assert.ok(out);
assert.equal("chat" in out, false, "the chat section never surfaces");
assert.deepEqual(out.graph, { enabled: true }); // siblings survive
});
it("corrupt settings.json → null (first-boot semantics), never throws", () => {
fs.mkdirSync(path.dirname(SETTINGS_FILE), { recursive: true });
fs.writeFileSync(SETTINGS_FILE, "{not json", "utf-8");
assert.equal(readSettings(), null);
});
it("LEGACY MIGRATION: default-settings.json folds into the record's chat section on first read", () => {
fs.rmSync(SETTINGS_FILE, { force: true });
fs.mkdirSync(path.dirname(DEFAULT_SETTINGS_FILE), { recursive: true });
fs.writeFileSync(DEFAULT_SETTINGS_FILE, JSON.stringify(CHAT_FIXTURE), "utf-8");
const migrated = readSettings();
assert.ok(migrated, "the legacy file became a record");
assert.equal(migrated.version, 1);
assert.deepEqual(migrated.chat, CHAT_FIXTURE);
// The record is now persisted — the legacy file is left inert on disk.
assert.ok(fs.existsSync(SETTINGS_FILE));
assert.deepEqual(readSettings()?.chat, CHAT_FIXTURE);
// And the default-settings facade reads the SAME section.
assert.deepEqual(readDefaultSettings(), CHAT_FIXTURE);
});
it("LEGACY MIGRATION is one-shot: an unreadable/corrupt legacy file never blocks first boot", () => {
fs.rmSync(SETTINGS_FILE, { force: true });
fs.mkdirSync(path.dirname(DEFAULT_SETTINGS_FILE), { recursive: true });
fs.writeFileSync(DEFAULT_SETTINGS_FILE, "junk", "utf-8");
assert.equal(readSettings(), null);
});
});
describe("the record-backed default-settings facade", () => {
it("writeDefaultSettings lands in the record's chat section WITHOUT touching other sections", () => {
writeSettings({ version: 1, graph: { enabled: false }, agent: { tools: { research: false } } });
writeDefaultSettings(CHAT_FIXTURE);
const out = readSettings();
assert.ok(out);
assert.deepEqual(out.chat, CHAT_FIXTURE);
assert.deepEqual(out.graph, { enabled: false }); // untouched
assert.deepEqual(out.agent?.tools, { research: false }); // untouched
});
it("writeDefaultSettings creates the record on first boot", () => {
fs.rmSync(SETTINGS_FILE, { force: true });
fs.rmSync(DEFAULT_SETTINGS_FILE, { force: true });
assert.equal(readSettings(), null);
writeDefaultSettings(CHAT_FIXTURE);
assert.deepEqual(readDefaultSettings(), CHAT_FIXTURE);
});
it("readDefaultSettings returns null when the record has no chat section", () => {
writeSettings({ version: 1, graph: { enabled: true } });
assert.equal(readDefaultSettings(), null);
});
});
-1
View File
@@ -55,7 +55,6 @@ describe("sanitizeUserSettings", () => {
const record = {
version: 1,
general: { autoUpdate: false },
chat: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low" },
agent: {
models: {
researcher: { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" }, thinkingLevel: "high" },