From a97fa0de1764ebbad739bf08de5e6e4df0a2a03f Mon Sep 17 00:00:00 2001 From: Devvyn <22340871+opticon454@users.noreply.github.com> Date: Sun, 1 Nov 2026 19:04:29 +0800 Subject: [PATCH] feat(custom-model): discover context length from /v1/models and add a default context length Fixes #754 --- src/custom-model-hosts.ts | 26 ++- src/web/public/i18n.js | 3 + src/web/public/index.html | 4 + src/web/public/settings-ui.js | 16 ++ src/web/routes/custom-model-routes.ts | 55 ++++++- src/web/routes/session-routes.ts | 9 +- src/web/schemas.ts | 3 + test/routes/custom-model-routes.test.ts | 165 ++++++++++++++++++- test/routes/quick-start-custom-model.test.ts | 44 +++++ test/routes/session-custom-model.test.ts | 52 ++++++ 10 files changed, 361 insertions(+), 16 deletions(-) diff --git a/src/custom-model-hosts.ts b/src/custom-model-hosts.ts index 8ad2c17d..46dc7271 100644 --- a/src/custom-model-hosts.ts +++ b/src/custom-model-hosts.ts @@ -52,13 +52,16 @@ export interface CustomModelHost { defaultModelId?: string; /** * Discovered context-window size (tokens) per model id, keyed by the same strings as - * `models`. Populated opportunistically during discovery (`custom-model-routes.ts`) from - * llama.cpp/llama-swap's `GET /props?model=` — the plain OpenAI-shaped `/v1/models` - * response has no such field. Only ever probed for a model the server already reports as - * loaded (llama-swap's `status.value === 'loaded'`); an unloaded one is deliberately never - * probed, since llama-swap treats `/props?model=` as a routing hint that can trigger an - * actual (slow, GPU-swapping) model load as a side effect of merely asking. A model this - * has no entry for simply gets no context-length env override applied — never a guess. + * `models`. Populated only by discovery (`custom-model-routes.ts`), from the strongest + * source the server offers: the llama-swap launch command reported by `GET /running` + * (the source of truth for a loaded model), then llama.cpp's `GET /props?model=` + * `n_ctx` (only ever probed for a model the server already reports as loaded, since + * llama-swap treats `/props?model=` as a routing hint that can trigger an actual + * (slow, GPU-swapping) model load as a side effect of merely asking), then the model's + * own `GET /v1/models` entry (`context_length`, else `context_window`, which current + * llama-swap, Bifrost and OpenRouter-style servers each state to varying degrees). A + * model with no entry here falls back to the endpoint's `defaultContextLength`; with + * neither, it simply gets no context-length env override applied, never a guess. */ modelContextLengths?: Record; /** @@ -88,6 +91,15 @@ export interface CustomModelHost { * server-side check relies on. */ modelSizesGB?: Record; + /** + * User-set fallback context length (tokens), applied by the read sites in + * `session-routes.ts` only when `modelContextLengths` has no entry for the selected + * model (a gateway that states no per-model figure, a model never loaded yet). Never + * written by discovery, so a value set in the endpoint editor survives every + * re-discovery sweep; unset keeps the long-standing behaviour of letting each CLI + * assume its own default. + */ + defaultContextLength?: number; } export function customModelHostsPath(configDir: string): string { diff --git a/src/web/public/i18n.js b/src/web/public/i18n.js index d425adf8..dceab1ac 100644 --- a/src/web/public/i18n.js +++ b/src/web/public/i18n.js @@ -615,6 +615,9 @@ 'API key': 'API 密钥', 'Optional. Left blank on edit keeps the existing key.': '可选。编辑时留空将保留现有密钥。', 'Auth header': '认证请求头', + 'Default context length (tokens)': '默认上下文长度(token 数)', + 'Used for models whose size cannot be discovered. Leave empty to let each CLI assume its own default.': + '用于无法自动发现上下文大小的模型。留空则让各个 CLI 使用其自身的默认值。', 'Never send both — some servers hang indefinitely.': '切勿同时发送两者 — 部分服务器会因此无限期挂起。', 'Authorization: Bearer (default)': 'Authorization: Bearer(默认)', 'api-key header (Azure)': 'api-key 请求头(Azure)', diff --git a/src/web/public/index.html b/src/web/public/index.html index 3c6fb573..a1e49ce0 100644 --- a/src/web/public/index.html +++ b/src/web/public/index.html @@ -2799,6 +2799,10 @@
Base URL
+
+
Default context length (tokens)Used for models whose size cannot be discovered. Leave empty to let each CLI assume its own default.
+ +
API keyOptional. Left blank on edit keeps the existing key.
diff --git a/src/web/public/settings-ui.js b/src/web/public/settings-ui.js index 0bf5025c..659ec0e1 100644 --- a/src/web/public/settings-ui.js +++ b/src/web/public/settings-ui.js @@ -3809,6 +3809,9 @@ Object.assign(CodemanApp.prototype, { document.getElementById('customModelHostApiKey').value = ''; // the server never returns the real value (apiKeySet is a bool) document.getElementById('customModelHostApiKey').placeholder = host?.apiKeySet ? '•••••••• (unchanged if left blank)' : ''; document.getElementById('customModelHostAuthStyle').value = host?.authStyle || 'bearer'; + // User-set fallback, round-tripped verbatim (discovery never touches it, so the + // cached copy is authoritative); blank means "no default", cleared on save. + document.getElementById('customModelHostDefaultContextLength').value = host?.defaultContextLength ?? ''; this._populateCustomModelDefaultSelect(host); document.getElementById('customModelHostEditor').style.display = ''; }, @@ -3834,6 +3837,18 @@ Object.assign(CodemanApp.prototype, { const baseUrl = document.getElementById('customModelHostBaseUrl').value.trim(); const apiKeyInput = document.getElementById('customModelHostApiKey').value; const authStyle = document.getElementById('customModelHostAuthStyle').value; + // Blank means "no default" (key omitted, so PUT clears it); a value must satisfy + // the same bound the server schema enforces, caught here for a friendlier message. + const defaultContextLengthInput = document.getElementById('customModelHostDefaultContextLength').value.trim(); + let defaultContextLength; + if (defaultContextLengthInput !== '') { + const parsed = Number(defaultContextLengthInput); + if (!Number.isInteger(parsed) || parsed < 1 || parsed > 100000000) { + this.showToast('Default context length must be a whole number of tokens between 1 and 100,000,000', 'warning'); + return; + } + defaultContextLength = parsed; + } const defaultModelId = document.getElementById('customModelHostDefaultModel').value || undefined; if (!id || !label || !baseUrl) { this.showToast('Id, label and base URL are all required', 'warning'); @@ -3854,6 +3869,7 @@ Object.assign(CodemanApp.prototype, { baseUrl, authStyle, defaultModelId, + defaultContextLength, apiKey: apiKeyInput || undefined, models: existing?.models, lastDiscoveredAt: existing?.lastDiscoveredAt, diff --git a/src/web/routes/custom-model-routes.ts b/src/web/routes/custom-model-routes.ts index 3339ae3c..5cf24df5 100644 --- a/src/web/routes/custom-model-routes.ts +++ b/src/web/routes/custom-model-routes.ts @@ -158,7 +158,12 @@ function authHeaders(host: Pick): Recor export interface DiscoveryResult { models: string[]; - /** See `CustomModelHost.modelContextLengths` — only ever populated for models already loaded. */ + /** + * See `CustomModelHost.modelContextLengths`: populated from a model's own `/v1/models` + * entry (`context_length`, else `context_window`) whenever the server states one, and + * from the launch command or `/props` for loaded models, which overwrite (and so win + * over) any listed figure for the same model. + */ contextLengths: Record; /** See `CustomModelHost.modelSlotCounts`; populated on the same loaded-models-only basis as `contextLengths`. */ slotCounts: Record; @@ -182,6 +187,27 @@ function parseSizeGB(description: unknown): number | undefined { return Number.isFinite(size) && size > 0 ? size : undefined; } +/** + * Ceiling shared with `schemas.ts`'s `CustomModelHostSchema` bound on context lengths: + * anything above it reads as nonsense and is ignored, because a bogus figure here + * overflows the smaller server, which is the exact failure this field exists to prevent. + */ +const MAX_DISCOVERED_CONTEXT_TOKENS = 100_000_000; + +/** + * Best-effort: reads a context length (tokens) stated by a model's OWN entry in + * `GET /v1/models`. OpenRouter-style servers call the field `context_length`; current + * llama-swap sends both `context_length` and `context_window` per model, and current + * Bifrost (a gateway in front of llama-swap) passes `context_window` through. Older + * servers send neither, and a server that states nothing gets no entry, never a guess. + * Non-numeric, zero, negative, fractional or over-the-ceiling values are ignored, the + * same rule the stored field's schema enforces: a wrong figure here is worse than none. + */ +function parseListedContextLength(value: unknown): number | undefined { + if (typeof value !== 'number' || !Number.isInteger(value) || value <= 0) return undefined; + return value <= MAX_DISCOVERED_CONTEXT_TOKENS ? value : undefined; +} + /** * Best-effort: fetches `GET /props?model=` (llama.cpp-native, llama-swap-proxied) for * ONE already-loaded model and pulls its `n_ctx` and `total_slots` out. Never called for a model that @@ -312,24 +338,38 @@ async function discoverModels( }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const body = (await res.json()) as { - data?: Array<{ id?: unknown; status?: { value?: unknown }; description?: unknown }>; + data?: Array<{ + id?: unknown; + status?: { value?: unknown }; + description?: unknown; + context_length?: unknown; + context_window?: unknown; + }>; }; const entries = body.data ?? []; const models = entries.map((m) => m.id).filter((id): id is string => typeof id === 'string' && id.length > 0); const sizesGB: Record = {}; + // The lowest-precedence context source, and the only one a gateway without /running or + // /props of its own (e.g. Bifrost) can offer: the figure a model's own listing states + // (`context_length`, else `context_window`). The loaded-model pass below OVERWRITES + // these for whatever it reads from the launch command or /props, so precedence per + // model stays: `/running` cmd, then `/props`, then this listed figure. + const contextLengths: Record = {}; for (const entry of entries) { if (typeof entry.id !== 'string' || !entry.id) continue; const size = parseSizeGB(entry.description); if (size !== undefined) sizesGB[entry.id] = size; + const listed = parseListedContextLength(entry.context_length) ?? parseListedContextLength(entry.context_window); + if (listed !== undefined) contextLengths[entry.id] = listed; } // llama-swap-specific, feature-detected: a server that never mentions `status` on ANY - // entry gets no context-length enrichment at all, rather than treating "no status field" - // as "assume unloaded" — either reading is a guess, and skipping is the safe one, since - // fetchContextLength must only ever run against a model this server itself calls loaded. + // entry gets no launch-command/props context enrichment at all, rather than treating + // "no status field" as "assume unloaded"; either reading is a guess, and skipping is + // the safe one, since fetchContextLength must only ever run against a model this server + // itself calls loaded. Listed figures above are unaffected: reading them triggers nothing. const hasStatusField = entries.some((m) => m && typeof m === 'object' && 'status' in m); - const contextLengths: Record = {}; const slotCounts: Record = {}; if (hasStatusField) { const loadedIds = entries @@ -364,6 +404,9 @@ async function discoverModels( // merges onto what was learned before). if (cmdSlots !== undefined && ctxFromProps) slots = 1; } + // Assigning only when a probe answered keeps the listed figure (if any) when + // neither the launch command nor /props stated one, and replaces it with the + // stronger source whenever they did: cmd > /props > listed, never the reverse. if (ctx !== undefined) contextLengths[id] = ctx; if (slots !== undefined) slotCounts[id] = slots; } diff --git a/src/web/routes/session-routes.ts b/src/web/routes/session-routes.ts index 0eb80b82..424de49b 100644 --- a/src/web/routes/session-routes.ts +++ b/src/web/routes/session-routes.ts @@ -1541,7 +1541,10 @@ export function registerSessionRoutes( if (!endpoint) { return createErrorResponse(ApiErrorCode.NOT_FOUND, 'Model endpoint not found'); } - const contextLength = endpoint.modelContextLengths?.[body.modelId]; + // Discovery's per-model figure wins; the endpoint's user-set default covers models + // whose size discovery could not determine (a gateway that states none, say). With + // neither, the existing "no override, CLI assumes its own default" behaviour stands. + const contextLength = endpoint.modelContextLengths?.[body.modelId] ?? endpoint.defaultContextLength; const slotCount = endpoint.modelSlotCounts?.[body.modelId]; // Some CLIs (today: only claude) carry enough of their own fixed system-prompt/tool- @@ -4447,7 +4450,9 @@ export function registerSessionRoutes( const cmHosts = await readCustomModelHosts(CODEMAN_CONFIG_DIR); const cmEndpoint = cmHosts.find((h) => h.id === customModel.endpointId); if (!cmEndpoint) return createErrorResponse(ApiErrorCode.NOT_FOUND, 'Model endpoint not found'); - const cmContextLength = cmEndpoint.modelContextLengths?.[customModel.modelId]; + // Per-model discovery figure first, user-set endpoint default second, nothing third + // (see the dedicated route's own comment above). + const cmContextLength = cmEndpoint.modelContextLengths?.[customModel.modelId] ?? cmEndpoint.defaultContextLength; const cmSlotCount = cmEndpoint.modelSlotCounts?.[customModel.modelId]; // See the dedicated route's own comment for the full reasoning: some CLIs' own fixed diff --git a/src/web/schemas.ts b/src/web/schemas.ts index 6c1ce474..bf97b7b4 100644 --- a/src/web/schemas.ts +++ b/src/web/schemas.ts @@ -2380,6 +2380,9 @@ export const CustomModelHostSchema = z.object({ modelSlotCounts: z.record(z.string().max(200), z.number().int().positive().max(4096)).optional(), // Same reasoning as modelContextLengths above. modelSizesGB: z.record(z.string().max(200), z.number().positive().max(100_000)).optional(), + // User-set fallback for models discovery has no context length for (session-routes.ts); + // discovery never writes it. Same bound as the modelContextLengths values above. + defaultContextLength: z.number().int().positive().max(100_000_000).optional(), }); /** diff --git a/test/routes/custom-model-routes.test.ts b/test/routes/custom-model-routes.test.ts index 80d5a5de..afadff5c 100644 --- a/test/routes/custom-model-routes.test.ts +++ b/test/routes/custom-model-routes.test.ts @@ -11,7 +11,7 @@ import { describe, it, expect, vi, afterEach } from 'vitest'; import { registerCustomModelRoutes } from '../../src/web/routes/custom-model-routes.js'; import { webviewFetch } from '../../src/web/webview-egress.js'; -import { createRouteTestHarness } from './_route-test-utils.js'; +import { createRouteTestHarness, type RouteTestHarness } from './_route-test-utils.js'; vi.mock('../../src/web/webview-egress.js', async () => { const actual = await vi.importActual( @@ -417,3 +417,166 @@ describe('apiKey is never handed back to the browser', () => { expect(fetchMock).toHaveBeenCalledTimes(1); }); }); + +describe('context-length discovery from the /v1/models listing', () => { + // The gateway path (a Bifrost in front of llama-swap, an OpenRouter-style server): + // no /running, no /props, just a per-model `context_length` or `context_window` in the + // model listing. Distinct ports per test keep the file's shared data dir unambiguous. + afterEach(() => { + fetchMock.mockReset(); + }); + + type StoredHost = { id: string; modelContextLengths?: Record; defaultContextLength?: number }; + + type HarnessApp = RouteTestHarness['app']; + + async function endpointWith(id: string, port: number, extra: Record = {}): Promise { + const { app } = await setup(); + const res = await app.inject({ + method: 'POST', + url: '/api/model-endpoints', + payload: { id, label: 'A', baseUrl: `http://localhost:${port}`, ...extra }, + }); + expect(res.statusCode).toBe(200); + return app; + } + + async function storedHost(app: HarnessApp, id: string): Promise { + const list = await app.inject({ method: 'GET', url: '/api/model-endpoints' }); + return (list.json() as StoredHost[]).find((h) => h.id === id); + } + + it('reads context_length (preferred) then context_window from each entry into modelContextLengths', async () => { + const app = await endpointWith('ep-ctx-listed', 8101); + fetchMock.mockResolvedValue( + new Response( + JSON.stringify({ + data: [ + { id: 'both', context_length: 131072, context_window: 32768 }, + { id: 'window-only', context_window: 8192 }, + { id: 'no-figure' }, + ], + }), + { status: 200 } + ) + ); + + const discover = await app.inject({ method: 'POST', url: '/api/model-endpoints/ep-ctx-listed/discover-models' }); + expect(discover.json().success).toBe(true); + + const stored = await storedHost(app, 'ep-ctx-listed'); + expect(stored?.modelContextLengths).toEqual({ both: 131072, 'window-only': 8192 }); + }); + + it('ignores non-numeric, zero, negative and absurd listed values; the schema bound itself is still accepted', async () => { + const app = await endpointWith('ep-ctx-bad', 8102); + fetchMock.mockResolvedValue( + new Response( + JSON.stringify({ + data: [ + { id: 'string', context_length: '131072' }, + { id: 'zero', context_window: 0 }, + { id: 'negative', context_length: -10 }, + { id: 'over-bound', context_length: 100_000_001 }, + { id: 'fractional', context_window: 16384.5 }, + { id: 'at-bound', context_length: 100_000_000 }, + ], + }), + { status: 200 } + ) + ); + + await app.inject({ method: 'POST', url: '/api/model-endpoints/ep-ctx-bad/discover-models' }); + + const stored = await storedHost(app, 'ep-ctx-bad'); + expect(stored?.modelContextLengths).toEqual({ 'at-bound': 100_000_000 }); + }); + + it('prefers the /running launch command over the listed figure for a loaded model', async () => { + const app = await endpointWith('ep-ctx-cmd', 8103); + fetchMock.mockImplementation(async (url: URL) => { + if (url.pathname === '/v1/models') { + return new Response( + JSON.stringify({ data: [{ id: 'cmd-wins', status: { value: 'loaded' }, context_length: 131072 }] }), + { status: 200 } + ); + } + if (url.pathname === '/running') { + return new Response( + JSON.stringify({ running: [{ model: 'cmd-wins', state: 'ready', cmd: 'llama-server --ctx-size 16384' }] }), + { status: 200 } + ); + } + if (url.pathname === '/props') return new Response('nope', { status: 404 }); + throw new Error(`unexpected request in this test: ${url.href}`); + }); + + await app.inject({ method: 'POST', url: '/api/model-endpoints/ep-ctx-cmd/discover-models' }); + + const stored = await storedHost(app, 'ep-ctx-cmd'); + expect(stored?.modelContextLengths).toEqual({ 'cmd-wins': 16384 }); + }); + + it('keeps a previously learned value the fresh listing says nothing about, and discovery never touches defaultContextLength', async () => { + const app = await endpointWith('ep-ctx-keep', 8104, { + models: ['re-listed', 'untouched'], + modelContextLengths: { 're-listed': 16384, untouched: 4096 }, + defaultContextLength: 65536, + }); + fetchMock.mockResolvedValue( + new Response( + JSON.stringify({ data: [{ id: 're-listed', context_length: 32768 }, { id: 'untouched' }] }), + { status: 200 } + ) + ); + + await app.inject({ method: 'POST', url: '/api/model-endpoints/ep-ctx-keep/discover-models' }); + + const stored = await storedHost(app, 'ep-ctx-keep'); + expect(stored?.modelContextLengths).toEqual({ 're-listed': 32768, untouched: 4096 }); + expect(stored?.defaultContextLength).toBe(65536); + }); +}); + +describe('defaultContextLength (the user-set fallback discovery never overwrites)', () => { + afterEach(() => { + fetchMock.mockReset(); + }); + + it('stores it on create, updates it on PUT, and a PUT without it clears it', async () => { + const { app } = await setup(); + const create = await app.inject({ + method: 'POST', + url: '/api/model-endpoints', + payload: { id: 'ep-defctx', label: 'A', baseUrl: 'http://localhost:8105', defaultContextLength: 131072 }, + }); + expect(create.json().data.host.defaultContextLength).toBe(131072); + + const update = await app.inject({ + method: 'PUT', + url: '/api/model-endpoints/ep-defctx', + payload: { label: 'A', baseUrl: 'http://localhost:8105', defaultContextLength: 32768 }, + }); + expect(update.json().data.host.defaultContextLength).toBe(32768); + + const cleared = await app.inject({ + method: 'PUT', + url: '/api/model-endpoints/ep-defctx', + payload: { label: 'A', baseUrl: 'http://localhost:8105' }, + }); + expect(cleared.json().data.host.defaultContextLength).toBeUndefined(); + }); + + it('rejects zero, negative, fractional, over-the-bound and non-numeric values', async () => { + const { app } = await setup(); + for (const defaultContextLength of [0, -1, 1.5, 100_000_001, '131072']) { + const res = await app.inject({ + method: 'POST', + url: '/api/model-endpoints', + payload: { id: 'ep-defctx-bad', label: 'A', baseUrl: 'http://localhost:8106', defaultContextLength }, + }); + expect(res.json().success, String(defaultContextLength)).toBe(false); + expect(res.json().errorCode, String(defaultContextLength)).toBe('INVALID_INPUT'); + } + }); +}); diff --git a/test/routes/quick-start-custom-model.test.ts b/test/routes/quick-start-custom-model.test.ts index 17a0402f..c9c919e2 100644 --- a/test/routes/quick-start-custom-model.test.ts +++ b/test/routes/quick-start-custom-model.test.ts @@ -367,6 +367,50 @@ describe('POST /api/quick-start: customModel (one-shot custom-model launch)', () expect(res.statusCode).toBe(200); expect(res.json().requiresContextWarning).toBeUndefined(); }); + + it('warns using the endpoint defaultContextLength when discovery recorded no per-model figure', async () => { + const endpoint: CustomModelHost = { + id: 'ep-default', + label: 'silent gateway', + baseUrl: 'http://192.168.1.53:8080', + apiKey: 'k', + defaultContextLength: 16384, + }; + await writeCustomModelHosts(getDataDir(), [ENDPOINT, endpoint]); + + const res = await quickStart({ + caseName: 'cm-default-ctx', + mode: 'claude', + customModel: { endpointId: 'ep-default', modelId: 'some-unknown-model' }, + }); + + const body = res.json(); + expect(body.requiresContextWarning).toBe(true); + expect(body.contextLength).toBe(16384); + expect(ctx.sessions.size).toBe(1); // nothing was created + }); + + it('a discovered per-model figure wins over the endpoint defaultContextLength', async () => { + const endpoint: CustomModelHost = { + id: 'ep-precedence', + label: 'mixed box', + baseUrl: 'http://192.168.1.54:8080', + apiKey: 'k', + modelContextLengths: { m: 131072 }, + defaultContextLength: 16384, + }; + await writeCustomModelHosts(getDataDir(), [ENDPOINT, endpoint]); + + const res = await quickStart({ + caseName: 'cm-precedence', + mode: 'claude', + customModel: { endpointId: 'ep-precedence', modelId: 'm' }, + }); + + expect(res.statusCode).toBe(200); + expect(res.json().requiresContextWarning).toBeUndefined(); + expect(ctx.sessions.size).toBe(2); + }); }); describe('triggering the actual llama-swap load (not just watching for it)', () => { diff --git a/test/routes/session-custom-model.test.ts b/test/routes/session-custom-model.test.ts index 9c8062d6..3a15dfc7 100644 --- a/test/routes/session-custom-model.test.ts +++ b/test/routes/session-custom-model.test.ts @@ -449,6 +449,58 @@ describe('POST /api/sessions/:id/custom-model', () => { expect(session.restartCli).not.toHaveBeenCalled(); }); + it('the endpoint defaultContextLength stands in when discovery recorded no per-model figure', async () => { + const { app, ctx } = await setup(); + // A gateway that states nothing per model: only the user's default is known. + const endpoint: CustomModelHost = { + id: 'ep-default', + label: 'silent gateway', + baseUrl: 'http://192.168.1.53:8080', + apiKey: 'k', + defaultContextLength: 16384, + }; + await writeCustomModelHosts(getDataDir(), [CLAUDE_ENDPOINT, endpoint]); + const session = ctx.sessions.get('test-session-1')!; + session.mode = 'claude'; + + const res = await app.inject({ + method: 'POST', + url: '/api/sessions/test-session-1/custom-model', + payload: { endpointId: 'ep-default', modelId: 'some-unknown-model' }, + }); + + const body = res.json(); + expect(body.requiresContextWarning).toBe(true); + expect(body.contextLength).toBe(16384); + expect(session.setCustomModel).not.toHaveBeenCalled(); + }); + + it('a discovered per-model figure wins over the endpoint defaultContextLength', async () => { + const { app, ctx } = await setup(); + // The default is small enough to warn on; the discovered figure for THIS model is + // not, so the fallback must not leak into the per-model path. + const endpoint: CustomModelHost = { + id: 'ep-precedence', + label: 'mixed box', + baseUrl: 'http://192.168.1.54:8080', + apiKey: 'k', + modelContextLengths: { 'qwen3.8-27b-ud-q4_k_xl': 131072 }, + defaultContextLength: 16384, + }; + await writeCustomModelHosts(getDataDir(), [CLAUDE_ENDPOINT, endpoint]); + const session = ctx.sessions.get('test-session-1')!; + session.mode = 'claude'; + + const res = await app.inject({ + method: 'POST', + url: '/api/sessions/test-session-1/custom-model', + payload: { endpointId: 'ep-precedence', modelId: 'qwen3.8-27b-ud-q4_k_xl' }, + }); + + expect(res.json().requiresContextWarning).toBeUndefined(); + expect(session.setCustomModel).toHaveBeenCalledTimes(1); + }); + // llama-server splits its context across --parallel slots, and Claude Code sends several // requests at once, so a context that clears the floor as a whole can still fail per slot. describe('parallel slots share the context', () => {