feat(models): clamp-on-write for pinned thinking levels + DEFAULT_THINKING_ORDER in the core barrel

Fix 1 — clamp, never reject (owner ruling, interlude 2026-09-22): a
model swap with a pinned thinking level must JUST WORK. The owner hit
the write-side 400 live (Claude Fable 5.1/xhigh → GLM 5.3 flash:
"z-ai/glm-5.3-flash does not support thinking level xhigh"). The
supported-level verdicts in validateChatSettings and
validateModelsSettings are DEAD — the WRITE clamps the pin to the new
model's nearest supported level (pi's clampThinkingLevel, the same
translation piEngineForSpec applies at runtime) and stores the clamped
result; the server is the truth — the record shows the clamped level
after write.

New pure helpers in model-catalog.ts, applied by ALL THREE write
routes (section, defaults, per-chat — one verdict seam, so every
surface that used the old 400 gets the clamp uniformly):
- clampModelsLevels: every slot's level clamps against the SLOT'S OWN
  model (the parser already requires a model on each slot write).
- clampChatLevel: the body's own model, else the record's resolved
  strong slot (the chain's fallback); no model anywhere → rides
  unchecked (runtime clamps at engine build; the turn gate refuses
  the model-less chat anyway).

Fix 2 — DEFAULT_THINKING_ORDER moves from config.ts to core/settings
(core barrel, AUTH_KEYS precedent — one source of truth, no drift).
The UI imports it to auto-populate a thinking level on FRESH model
picks; the ordering has no other client role. Legacy unset records
keep the runtime defaultThinkingLevel fallback at engine build
untouched. Import sites updated: agent/models.ts, tests.

Verified live on 3004: the owner's exact case stores max (not 400);
a bare chat-level write clamps to low against the strong slot's glm;
unknown provider/model still 400s with the provider list.

Tests: the level-verdict suites became clamp suites (the owner's case
pinned verbatim: xhigh→max, minimal→low, off→low, supported
pass-through, level-less slots ride); the machine-aware
expectBareLevelVerdict chain is retired — clampChatLevel takes the
strong slot as an explicit param, so the tests are pure again.

Gates: typecheck, lint, 839/839, build.
This commit is contained in:
amosblomqvist
2026-09-22 21:44:43 +03:00
parent 5ab52f07f6
commit 9387418864
8 changed files with 233 additions and 138 deletions
+21 -10
View File
@@ -1,13 +1,20 @@
import { getChat, validateChatSettings, parseSettingsBody, stores } from "@/src/server";
import {
clampChatLevel,
effectiveSettings,
getChat,
parseSettingsBody,
stores,
validateChatSettings,
} from "@/src/server";
import type { ChatSettings } from "@/src/core";
/**
* The per-chat model control (model-control wave): replace-whole settings —
* the picker sends the complete desired object ({} clears all overrides and
* returns the chat to config.ts's pin). Validation is the catalog gate
* (src/server/model-catalog.ts): unknown provider/model or an unsupported
* thinking level is a 400 carrying the supported list, never a silent clamp
* — the user must see what the model actually accepts. Effective next turn
* the picker sends the complete desired object ({} clears all overrides).
* Validation is the catalog gate (src/server/model-catalog.ts): unknown
* provider/model is a 400. Thinking levels never reject (owner,
* interlude 2026-09-22): a pinned level clamps to the named model's
* nearest supported and the clamped value is stored. Effective next turn
* by construction (agents are stateless per turn).
*
* The body parsing itself is shared with the draft route
@@ -26,8 +33,12 @@ export async function PUT(req: Request, { params }: { params: Promise<{ id: stri
return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 });
}
// Replace-whole persists the validated object verbatim — no merges, so a
// stale client can never resurrect a cleared override.
stores.chatStore.saveSettings(id, parsed.settings);
return Response.json({ settings: parsed.settings satisfies ChatSettings });
// Replace-whole persists the validated, level-clamped object — no
// merges, so a stale client can never resurrect a cleared override.
// Clamp-on-write (owner, interlude 2026-09-22): a pinned thinking
// level never rejects — it clamps to the named model's nearest
// supported (body model, else the record's strong slot).
const settings = clampChatLevel(parsed.settings, effectiveSettings().models.strong);
stores.chatStore.saveSettings(id, settings);
return Response.json({ settings });
}
+25 -5
View File
@@ -8,10 +8,13 @@
* section ∈ general | chat | agent | appearance | graph (else 404).
* Body: the complete desired section object. Parse + validate strictly
* (server/settings-body.ts): a bad shape/type is a 400 carrying the
* supported list where one exists, never a silent clamp. The chat
* section additionally passes the catalog gate (validateChatSettings —
* unknown provider/model or unsupported thinking level = 400), same as
* the defaults route. The agent section additionally validates prompt
* supported list where one exists. The chat and models sections
* additionally pass the catalog gate (validateChatSettings /
* validateModelsSettings — unknown provider/model = 400). Thinking
* levels never reject a write (owner, interlude 2026-09-22): a pinned
* level CLAMPS to the named model's nearest supported level and the
* clamped value is stored — model swaps just work, same as the
* defaults route. The agent section additionally validates prompt
* switches: an ON switch must reference a CURRENT source segment with an
* existing override file (an OFF switch may reference anything — users
* cleaning up orphans must not be blocked).
@@ -25,6 +28,9 @@ import type { AgentSettings, ChatSettings, ModelsSettings } from "@/src/core";
import {
SETTINGS_SECTIONS,
allSegmentPaths,
clampChatLevel,
clampModelsLevels,
effectiveSettings,
promptOverrides,
readSettingsRecord,
validateChatSettings,
@@ -87,10 +93,24 @@ export async function PUT(request: Request, ctx: { params: Promise<{ section: st
}
}
// Clamp-on-write (owner ruling, interlude 2026-09-22): a model swap
// with a pinned thinking level NEVER errors — the pin clamps to the
// new model's nearest supported level (pi's nearest-clamp, the same
// translation piEngineForSpec applies at runtime) and the clamped
// value is what lands in the record. The chat section's bare levels
// clamp against the record's resolved strong slot (the chain's
// fallback); the models section's slots each carry their own model.
const sectionOut =
section === "chat"
? clampChatLevel(parsed.settings as ChatSettings, effectiveSettings().models.strong)
: section === "models"
? clampModelsLevels(parsed.settings as ModelsSettings)
: parsed.settings;
// Read-modify-write ONE section of the record; everything else rides
// along untouched (other sections, unknown keys — sanitize preserves).
const record = readSettingsRecord() ?? { version: 1 };
const next = { ...record, [section]: parsed.settings };
const next = { ...record, [section]: sectionOut };
writeSettingsRecord(next);
return Response.json({ settings: next });
}
+21 -7
View File
@@ -9,16 +9,25 @@
* lives at data/default-settings.json (config.DEFAULT_SETTINGS_FILE).
*
* GET → 200 {settings} once saved, 200 null on first boot ("no model").
* PUT → 200 {settings} — the VALIDATED copy persisted verbatim. The exact
* per-chat contract (parseSettingsBody + the catalog gate): unknown
* provider/model or an unsupported thinking level is a 400 carrying
* the supported list, never a silent clamp.
* PUT → 200 {settings} — the VALIDATED copy, level-clamped, persisted.
* The exact per-chat contract (parseSettingsBody + the catalog
* gate): an unknown provider/model is a 400. Thinking levels
* never reject (owner, interlude 2026-09-22): a pinned level
* clamps to the named model's nearest supported and the clamped
* value is stored — model swaps just work.
*
* Per-session settings (PUT /api/chats/:id/settings) are a separate record
* untouched by this — the default still rides along on session creation
* exactly as before.
*/
import { readDefaultSettings, writeDefaultSettings, parseSettingsBody, validateChatSettings } from "@/src/server";
import {
clampChatLevel,
effectiveSettings,
parseSettingsBody,
readDefaultSettings,
validateChatSettings,
writeDefaultSettings,
} from "@/src/server";
export const dynamic = "force-dynamic";
@@ -34,6 +43,11 @@ export async function PUT(request: Request) {
if (!verdict.ok) {
return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 });
}
writeDefaultSettings(parsed.settings);
return Response.json({ settings: parsed.settings });
// Clamp-on-write (owner, interlude 2026-09-22): a pinned thinking
// level never rejects the write — it clamps to the named model's
// nearest supported level (body model, else the record's strong
// slot) and the clamped value is stored.
const settings = clampChatLevel(parsed.settings, effectiveSettings().models.strong);
writeDefaultSettings(settings);
return Response.json({ settings });
}