diff --git a/apps/host/server/native-models.mjs b/apps/host/server/native-models.mjs index 909b4c4d68..d53b46f666 100644 --- a/apps/host/server/native-models.mjs +++ b/apps/host/server/native-models.mjs @@ -4,6 +4,8 @@ const tokens = value => Number.isInteger(value) && value > 0 ? value : undefined export function nativeModelEntries(instances) { return (instances ?? []).filter(i => i.status === 'ready').map(i => { const n_ctx = tokens(i.metrics?.engine?.max_context) ?? tokens(i.context); - return { id: i.apiModelId, object: 'model', owned_by: 'strata', meta: { instanceId: i.id, gpus: i.gpus, label: i.label, aliases: i.aliases, ...(n_ctx ? { n_ctx } : {}) } }; + // #5893: also as context_length, the field OpenAI-style clients read; a client that needs the window then has no reason to use the + // engine's private port, whose key is new at every load + return { id: i.apiModelId, object: 'model', owned_by: 'strata', ...(n_ctx ? { context_length: n_ctx } : {}), meta: { instanceId: i.id, gpus: i.gpus, label: i.label, aliases: i.aliases, ...(n_ctx ? { n_ctx } : {}) } }; }); } diff --git a/apps/host/tests/native-models-context.test.mjs b/apps/host/tests/native-models-context.test.mjs new file mode 100644 index 0000000000..b1c9ad3c7c --- /dev/null +++ b/apps/host/tests/native-models-context.test.mjs @@ -0,0 +1,30 @@ +// #5893: a client that needs the loaded context window (Unsloth Studio sizes its compaction from it) used the engine's private port, whose key +// is new at every load, so every reload broke it. The app's own /v1/models carries the window (PR #4370: meta.n_ctx, the engine's live value +// first) and, here, also as context_length, the field OpenAI-style clients read; the app's API key stays the one the person set. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { nativeModelEntries } from '../server/native-models.mjs'; + +const instances = [ + { id: 'n1', apiModelId: 'qwen3.8-flash-next', status: 'ready', context: 131072, metrics: { engine: { max_context: 120000 } }, gpus: [0], label: 'Qwen3.8 Flash Next', aliases: ['local'] }, + { id: 'n2', apiModelId: 'other', status: 'loading', context: 32768, gpus: [1], label: 'Other', aliases: [] }, + { id: 'n3', apiModelId: 'configured-only', status: 'ready', context: 65536, gpus: [], label: 'C', aliases: [] }, + { id: 'n4', apiModelId: 'unknown-window', status: 'ready', context: 0, gpus: [], label: 'U', aliases: [] }, +]; + +test('ready models carry their window as context_length and meta.n_ctx; an unknown window is left out', () => { + const list = nativeModelEntries(instances); + assert.deepEqual(list.map(m => m.id), ['qwen3.8-flash-next', 'configured-only', 'unknown-window'], 'a model still loading is not listed'); + const [live, configured, unknown] = list; + assert.equal(live.context_length, 120000, 'the engine\'s live window first'); + assert.equal(live.meta.n_ctx, 120000); + assert.equal(configured.context_length, 65536); + assert.equal(configured.meta.n_ctx, 65536); + assert.ok(!('context_length' in unknown) && !('n_ctx' in unknown.meta), 'never reported as 0'); +}); + +test('the host\'s /v1/models answers with these entries', () => { + const src = readFileSync(new URL('../server/index.mjs', import.meta.url), 'utf8'); + assert.match(src, /if\(path==='\/v1\/models'&&method==='GET'\)return json\(res,200,\{object:'list',data:nativeModelEntries\(native\.state\(\)\.instances\)\}\);/); +}); diff --git a/apps/host/tests/native-models.test.mjs b/apps/host/tests/native-models.test.mjs index 34bdb28b89..726945529d 100644 --- a/apps/host/tests/native-models.test.mjs +++ b/apps/host/tests/native-models.test.mjs @@ -4,7 +4,7 @@ import { nativeModelEntries } from '../server/native-models.mjs'; const ready = (patch = {}) => ({ id: 'inst-a', apiModelId: 'qwen-q2_0', status: 'ready', gpus: [0], label: 'Qwen 3.6', aliases: ['qwen-q2_0'], context: 65536, metrics: null, ...patch }); test('the configured context is reported as n_ctx and the other keys are unchanged', () => { - assert.deepEqual(nativeModelEntries([ready()]), [{ id: 'qwen-q2_0', object: 'model', owned_by: 'strata', meta: { instanceId: 'inst-a', gpus: [0], label: 'Qwen 3.6', aliases: ['qwen-q2_0'], n_ctx: 65536 } }]); + assert.deepEqual(nativeModelEntries([ready()]), [{ id: 'qwen-q2_0', object: 'model', owned_by: 'strata', context_length: 65536, meta: { instanceId: 'inst-a', gpus: [0], label: 'Qwen 3.6', aliases: ['qwen-q2_0'], n_ctx: 65536 } }]); }); test('the engine-reported window wins over the config', () => { @@ -56,6 +56,6 @@ test('the input is not changed', () => { test('n_ctx is the only key added to meta', () => { const [entry] = nativeModelEntries([ready()]); - assert.deepEqual(Object.keys(entry).sort(), ['id', 'meta', 'object', 'owned_by']); + assert.deepEqual(Object.keys(entry).sort(), ['context_length', 'id', 'meta', 'object', 'owned_by'], 'context_length (#5893) beside the old keys'); assert.deepEqual(Object.keys(entry.meta).sort(), ['aliases', 'gpus', 'instanceId', 'label', 'n_ctx']); });