Files
learnharness/app/api/chats/[id]/settings/route.ts
T
amosblomqvist 9387418864 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.
2026-09-22 21:44:43 +03:00

45 lines
1.8 KiB
TypeScript

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).
* 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
* (src/server/settings-body.ts, default-settings wave 2026-09-18).
*/
export async function PUT(req: Request, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
if (!getChat(id)) return Response.json({ error: "Not found" }, { status: 404 });
const body = await req.json().catch(() => null);
const parsed = parseSettingsBody(body);
if (!parsed.ok) return Response.json({ error: parsed.error }, { status: 400 });
const verdict = validateChatSettings(parsed.settings);
if (!verdict.ok) {
return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 });
}
// 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 });
}