fix(custom-model): serialize every write of the endpoint store

The four endpoint routes and the 5-minute discovery sweep each read
custom-model-hosts.json, changed their copy and wrote it back through one
shared temp file name. Two of them at once could tear the file or drop each
other's record: ten creates sent together did not all land.

Every write now goes through mutateCustomModelHosts() (one promise chain, a
random temp name, mode 0600), with each check inside the same step as its
write. A file that does not parse is refused with 409 and never replaced.
A delete of an unknown id no longer rewrites the file. A guard test fails
any other writer in src/.

The id `local-ollama` is reserved for the model Codeman runs on this Mac: a
new endpoint may not take it, and while the record is the managed one it is
not edited, deleted or re-discovered from these routes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-06 22:16:38 +01:00
1 parent be9279de35
commit a864acc52c
5 files changed
+800 -52

No files matched your search

+107 -2
View File
@@ -13,7 +13,9 @@
* directly plus the mocked webviewFetch dispatcher).
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getDataDir } from '../src/config/instance.js';
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
import { dataPath, getDataDir } from '../src/config/instance.js';
import { customModelHostsPath } from '../src/custom-model-hosts.js';
import { readCustomModelHosts, writeCustomModelHosts, type CustomModelHost } from '../src/custom-model-hosts.js';
import { clearPropsProbeCache, refreshAllCustomModelHosts } from '../src/web/routes/custom-model-routes.js';
import { webviewFetch } from '../src/web/webview-egress.js';
@@ -498,7 +500,9 @@ describe('refreshAllCustomModelHosts: parallel-slot enrichment (llama.cpp splits
if (url.pathname === '/running') {
return new Response(
JSON.stringify({
running: [{ model: 'm', state: 'ready', cmd: 'llama-server -m /models/m.gguf --ctx-size 262144 --parallel 4' }],
running: [
{ model: 'm', state: 'ready', cmd: 'llama-server -m /models/m.gguf --ctx-size 262144 --parallel 4' },
],
}),
{ status: 200 }
);
@@ -602,3 +606,104 @@ describe('refreshAllCustomModelHosts: model-size enrichment (parsed from /v1/mod
expect(updated.modelSizesGB).toEqual({ a: 8 }); // 'a' kept from before, 'b' dropped (gone from the list)
});
});
describe('the sweep and the serialized writer', () => {
it('an endpoint created while the sweep was probing another one is kept', async () => {
const dir = getDataDir();
await writeCustomModelHosts(dir, [host({ id: 'probed', baseUrl: 'http://localhost:8080' })]);
fetchMock.mockImplementation(async () => {
// An admin saves a second endpoint between the sweep's read and its write. The sweep
// must splice its result onto the list as it is then, not write back the one it read.
const current = await readCustomModelHosts(dir);
if (!current.some((h) => h.id === 'added')) {
await writeCustomModelHosts(dir, [...current, host({ id: 'added', baseUrl: 'http://localhost:8082' })]);
}
return new Response(JSON.stringify({ data: [{ id: 'qwen3' }] }), { status: 200 });
});
await refreshAllCustomModelHosts();
const hosts = await readCustomModelHosts(dir);
expect(hosts.map((h) => h.id)).toEqual(['probed', 'added']);
expect(hosts[0].models).toEqual(['qwen3']);
});
it('never writes over a store that stopped parsing while the sweep ran', async () => {
const dir = getDataDir();
vi.spyOn(console, 'warn').mockImplementation(() => {});
await writeCustomModelHosts(dir, [
host({ id: 'a', baseUrl: 'http://localhost:8080', apiKey: 'sk-keep' }),
host({ id: 'b', baseUrl: 'http://localhost:8081' }),
]);
const broken = '[{"id":"a","apiKey":"sk-keep"},';
fetchMock.mockImplementation(async () => {
writeFileSync(customModelHostsPath(dir), broken);
return new Response(JSON.stringify({ data: [{ id: 'qwen3' }] }), { status: 200 });
});
await expect(refreshAllCustomModelHosts()).resolves.toBeUndefined();
// It used to read the broken file as an empty list and carry on; it is left as it is.
expect(readFileSync(customModelHostsPath(dir), 'utf8')).toBe(broken);
// And the sweep stopped there: the second endpoint was not probed for nothing.
expect(fetchMock.mock.calls.every(([url]) => !(url as URL).href.includes('8081'))).toBe(true);
vi.restoreAllMocks();
});
});
describe('the sweep leaves the local model record alone', () => {
const STATE = () => dataPath('local-model.json');
const MANAGED: CustomModelHost = {
id: 'local-ollama',
label: 'This Mac',
baseUrl: 'http://127.0.0.1:11435',
authStyle: 'bearer',
models: ['qwen3.6:35b-a3b'],
defaultModelId: 'qwen3.6:35b-a3b',
defaultContextLength: 131072,
};
beforeEach(() => {
rmSync(STATE(), { force: true });
});
it('while it is managed: it is not probed, and a model somebody pulled through the port does not become launchable', async () => {
const dir = getDataDir();
writeFileSync(
STATE(),
JSON.stringify({
schema: 1,
state: 'installed',
model: { id: 'qwen3.6:35b-a3b', label: 'Qwen3.6 35B-A3B', context: 131072 },
cli: { id: 'opencode', label: 'OpenCode' },
runtime: { version: '0.40.15', port: 11435, enabled: true },
connection: { endpointId: 'local-ollama', flagWasEnabled: false },
})
);
await writeCustomModelHosts(dir, [MANAGED, host({ id: 'other', baseUrl: 'http://localhost:8080' })]);
fetchMock.mockImplementation(
async () =>
new Response(JSON.stringify({ data: [{ id: 'qwen3.6:35b-a3b' }, { id: 'sneaked-in:7b' }] }), { status: 200 })
);
await refreshAllCustomModelHosts();
const hosts = await readCustomModelHosts(dir);
// Our record is exactly what the install wrote: no new model, no discovery stamp.
expect(hosts.find((h) => h.id === 'local-ollama')).toEqual(MANAGED);
// Every other endpoint is refreshed as before.
expect(hosts.find((h) => h.id === 'other')?.models).toEqual(['qwen3.6:35b-a3b', 'sneaked-in:7b']);
for (const [url] of fetchMock.mock.calls) expect((url as URL).href).not.toContain('11435');
rmSync(STATE(), { force: true });
});
it('a record with that id that nobody installed is refreshed like any other', async () => {
const dir = getDataDir();
await writeCustomModelHosts(dir, [{ ...MANAGED, models: [], defaultModelId: undefined }]);
fetchMock.mockImplementation(
async () => new Response(JSON.stringify({ data: [{ id: 'mine:1b' }] }), { status: 200 })
);
await refreshAllCustomModelHosts();
expect((await readCustomModelHosts(dir))[0].models).toEqual(['mine:1b']);
});
});
+239
View File
@@ -0,0 +1,239 @@
/**
* @fileoverview The serialized writer of `custom-model-hosts.json`
* (src/custom-model-hosts-writer.ts) and the rule that every writer in src/ goes through it.
*
* What is pinned:
* - two mutations started at the same moment both land (the fault the writer ends: the
* endpoint routes and the discovery sweep used to read, change and write on their own);
* - the check a caller makes runs in the same step as its write;
* - a file that does not parse is refused and left byte for byte, and the refusal never
* repeats the file's text (it can hold an API key);
* - the file is written 0600 through a temp file that is gone afterwards;
* - no module of src/ calls `writeCustomModelHosts()` any more.
*
* Port: N/A (no server; files in a temp folder).
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdtempSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
CustomModelHostsWriteRefusedError,
customModelHostsWriteErrorResponse,
mutateCustomModelHosts,
} from '../src/custom-model-hosts-writer.js';
import { customModelHostsPath, readCustomModelHosts, type CustomModelHost } from '../src/custom-model-hosts.js';
const host = (id: string, extra: Partial<CustomModelHost> = {}): CustomModelHost => ({
id,
label: id,
baseUrl: `http://${id}.example:8080`,
...extra,
});
let dir: string;
beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'codeman-hosts-writer-'));
});
afterEach(() => {
rmSync(dir, { recursive: true, force: true });
vi.restoreAllMocks();
});
describe('mutateCustomModelHosts', () => {
it('starts from an empty list when the file is missing, and writes what the mutation returns', async () => {
const result = await mutateCustomModelHosts((hosts) => {
expect(hosts).toEqual([]);
return { hosts: [host('a')], result: 'added' };
}, dir);
expect(result).toBe('added');
expect(await readCustomModelHosts(dir)).toEqual([host('a')]);
});
it('an empty file is an empty list too: it holds no endpoint to lose', async () => {
writeFileSync(customModelHostsPath(dir), ' \n');
await mutateCustomModelHosts((hosts) => ({ hosts: [...hosts, host('a')], result: undefined }), dir);
expect(await readCustomModelHosts(dir)).toEqual([host('a')]);
});
it('writes nothing when the mutation returns no list', async () => {
writeFileSync(customModelHostsPath(dir), JSON.stringify([host('a')]));
const before = statSync(customModelHostsPath(dir)).mtimeMs;
const result = await mutateCustomModelHosts((hosts) => ({ result: hosts.length }), dir);
expect(result).toBe(1);
expect(statSync(customModelHostsPath(dir)).mtimeMs).toBe(before);
expect(readFileSync(customModelHostsPath(dir), 'utf8')).toBe(JSON.stringify([host('a')]));
});
it('two mutations started together both land: the second sees what the first wrote', async () => {
const seen: string[][] = [];
const add = (id: string) =>
mutateCustomModelHosts((hosts) => {
seen.push(hosts.map((item) => item.id));
return { hosts: [...hosts, host(id)], result: id };
}, dir);
const results = await Promise.all([add('first'), add('second')]);
expect(results).toEqual(['first', 'second']);
expect(seen).toEqual([[], ['first']]);
expect((await readCustomModelHosts(dir)).map((item) => item.id)).toEqual(['first', 'second']);
});
it('twenty mutations at once keep every record (unserialized writers lost all but the last)', async () => {
const ids = Array.from({ length: 20 }, (_, i) => `ep${i}`);
await Promise.all(
ids.map((id) => mutateCustomModelHosts((hosts) => ({ hosts: [...hosts, host(id)], result: undefined }), dir))
);
expect((await readCustomModelHosts(dir)).map((item) => item.id)).toEqual(ids);
// Every temp file was renamed over the target: only the one file is left.
expect(readdirSync(dir)).toEqual(['custom-model-hosts.json']);
});
it('a check made inside the mutation is made on the list as it is then: two creates of one id, one wins', async () => {
const create = (label: string) =>
mutateCustomModelHosts<'created' | 'exists'>((hosts) => {
if (hosts.some((item) => item.id === 'same')) return { result: 'exists' };
return { hosts: [...hosts, host('same', { label })], result: 'created' };
}, dir);
expect(await Promise.all([create('one'), create('two')])).toEqual(['created', 'exists']);
expect(await readCustomModelHosts(dir)).toEqual([host('same', { label: 'one' })]);
});
it('hands the mutation a list it may change in place', async () => {
writeFileSync(customModelHostsPath(dir), JSON.stringify([host('a'), host('b')]));
await mutateCustomModelHosts((hosts) => {
hosts[1] = { ...hosts[1], label: 'changed' };
return { hosts, result: undefined };
}, dir);
expect((await readCustomModelHosts(dir)).map((item) => item.label)).toEqual(['a', 'changed']);
});
it('writes the file 0600 (it holds API keys), also over a file that was world-readable', async () => {
writeFileSync(customModelHostsPath(dir), '[]', { mode: 0o644 });
await mutateCustomModelHosts(() => ({ hosts: [host('a', { apiKey: 'sk-secret' })], result: undefined }), dir);
expect(statSync(customModelHostsPath(dir)).mode & 0o777).toBe(0o600);
});
it('writes through a symlink: the real file changes and the link stays a link', async () => {
const real = join(dir, 'real-hosts.json');
writeFileSync(real, '[]');
symlinkSync(real, customModelHostsPath(dir));
await mutateCustomModelHosts(() => ({ hosts: [host('a')], result: undefined }), dir);
expect(JSON.parse(readFileSync(real, 'utf8'))).toEqual([host('a')]);
expect(await readCustomModelHosts(dir)).toEqual([host('a')]);
});
describe('a file that must not be overwritten', () => {
const broken = '[{"id":"keep","apiKey":"sk-do-not-leak"},';
it.each([
['text that does not parse', broken],
['a JSON object instead of a list', '{"id":"keep","apiKey":"sk-do-not-leak"}'],
['a JSON string', '"sk-do-not-leak"'],
])('refuses %s, leaves it byte for byte and never calls the mutation', async (_label, content) => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
writeFileSync(customModelHostsPath(dir), content);
const fn = vi.fn(() => ({ hosts: [host('new')], result: undefined }));
await expect(mutateCustomModelHosts(fn, dir)).rejects.toBeInstanceOf(CustomModelHostsWriteRefusedError);
expect(fn).not.toHaveBeenCalled();
expect(readFileSync(customModelHostsPath(dir), 'utf8')).toBe(content);
expect(readdirSync(dir)).toEqual(['custom-model-hosts.json']);
// Neither the error nor the log repeats the file, which holds a key.
const error = await mutateCustomModelHosts(fn, dir).catch((err: Error) => err);
expect((error as Error).message).not.toContain('sk-do-not-leak');
expect((error as Error).message).toContain('custom-model-hosts.json');
expect(warn.mock.calls.flat().join(' ')).not.toContain('sk-do-not-leak');
});
it('the plain reader calls such a file an empty list, which is why a writer must not start from it', async () => {
writeFileSync(customModelHostsPath(dir), broken);
expect(await readCustomModelHosts(dir)).toEqual([]);
});
it('a refused mutation does not stop the ones after it', async () => {
vi.spyOn(console, 'warn').mockImplementation(() => {});
writeFileSync(customModelHostsPath(dir), broken);
const refused = mutateCustomModelHosts(() => ({ hosts: [], result: undefined }), dir);
const other = mkdtempSync(join(tmpdir(), 'codeman-hosts-writer-other-'));
try {
const next = mutateCustomModelHosts(() => ({ hosts: [host('a')], result: 'ok' }), other);
await expect(refused).rejects.toBeInstanceOf(CustomModelHostsWriteRefusedError);
expect(await next).toBe('ok');
expect(await readCustomModelHosts(other)).toEqual([host('a')]);
} finally {
rmSync(other, { recursive: true, force: true });
}
});
});
it('a mutation that throws rejects its own caller, writes nothing, and the chain goes on', async () => {
writeFileSync(customModelHostsPath(dir), JSON.stringify([host('a')]));
const thrown = mutateCustomModelHosts(() => {
throw new Error('boom');
}, dir);
const next = mutateCustomModelHosts((hosts) => ({ hosts: [...hosts, host('b')], result: undefined }), dir);
await expect(thrown).rejects.toThrow('boom');
await next;
expect((await readCustomModelHosts(dir)).map((item) => item.id)).toEqual(['a', 'b']);
});
});
describe('customModelHostsWriteErrorResponse', () => {
it('a refused write is 409 CONFLICT with the fix', () => {
const response = customModelHostsWriteErrorResponse(
new CustomModelHostsWriteRefusedError('the file is not a valid JSON list')
);
expect(response).toEqual({
success: false,
error: 'the file is not a valid JSON list',
errorCode: 'CONFLICT',
});
});
it('any other failure stays OPERATION_FAILED', () => {
expect(customModelHostsWriteErrorResponse(new Error('EACCES'))).toMatchObject({
success: false,
errorCode: 'OPERATION_FAILED',
error: 'EACCES',
});
});
});
describe('every writer goes through the serialized writer', () => {
const root = fileURLToPath(new URL('../', import.meta.url));
function sourceFiles(folder: string, out: string[] = []): string[] {
for (const name of readdirSync(folder)) {
const path = join(folder, name);
if (statSync(path).isDirectory()) {
if (name !== 'public' && name !== 'node_modules') sourceFiles(path, out);
} else if (name.endsWith('.ts')) {
out.push(path);
}
}
return out;
}
/** The file without its comment lines: a comment may name the function it explains. */
const code = (path: string): string =>
readFileSync(path, 'utf8')
.split('\n')
.filter((line) => !/^\s*(\/\/|\*|\/\*)/.test(line))
.join('\n');
it('no module of src/ calls writeCustomModelHosts(): the store itself only defines it', () => {
const callers = sourceFiles(join(root, 'src'))
.filter((path) => /\bwriteCustomModelHosts\s*\(/.test(code(path)))
.map((path) => path.slice(root.length));
expect(callers).toEqual(['src/custom-model-hosts.ts']);
});
it('the endpoint routes and the sweep name the writer, five times', () => {
const routes = code(join(root, 'src/web/routes/custom-model-routes.ts'));
// create, update, delete, discover-models and the re-discovery sweep.
expect(routes.match(/await mutateCustomModelHosts</g)).toHaveLength(5);
expect(routes).not.toContain('writeCustomModelHosts');
});
});
+282 -5
View File
@@ -8,7 +8,10 @@
* been bypassed.
* Port: N/A (app.inject, no real port needed)
*/
import { describe, it, expect, vi, afterEach } from 'vitest';
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
import { readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { dataPath, getDataDir } from '../../src/config/instance.js';
import { customModelHostsPath, readCustomModelHosts, type CustomModelHost } from '../../src/custom-model-hosts.js';
import { registerCustomModelRoutes } from '../../src/web/routes/custom-model-routes.js';
import { webviewFetch } from '../../src/web/webview-egress.js';
import { createRouteTestHarness, type RouteTestHarness } from './_route-test-utils.js';
@@ -524,10 +527,9 @@ describe('context-length discovery from the /v1/models listing', () => {
defaultContextLength: 65536,
});
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({ data: [{ id: 're-listed', context_length: 32768 }, { id: 'untouched' }] }),
{ status: 200 }
)
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' });
@@ -580,3 +582,278 @@ describe('defaultContextLength (the user-set fallback discovery never overwrites
}
});
});
describe('every write goes through the serialized writer', () => {
const HOSTS = () => customModelHostsPath(getDataDir());
beforeEach(() => {
rmSync(HOSTS(), { force: true });
});
afterEach(() => {
fetchMock.mockReset();
rmSync(HOSTS(), { force: true });
vi.restoreAllMocks();
});
it('ten creates sent at once all land (each used to read, change and write on its own)', async () => {
const { app } = await setup();
const ids = Array.from({ length: 10 }, (_, i) => `burst${i}`);
const responses = await Promise.all(
ids.map((id) =>
app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id, label: id, baseUrl: 'http://localhost:8200', apiKey: `sk-${id}` },
})
)
);
for (const res of responses) expect(res.json().success).toBe(true);
const stored = await readCustomModelHosts(getDataDir());
expect(stored.map((host) => host.id).sort()).toEqual([...ids].sort());
// Every key survived: none of the writes replaced the list with its own record.
for (const host of stored) expect(host.apiKey).toBe(`sk-${host.id}`);
});
it('two creates of one id sent at once: one is stored, the other is told it exists', async () => {
const { app } = await setup();
const create = (label: string) =>
app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id: 'race', label, baseUrl: 'http://localhost:8201' },
});
const [first, second] = await Promise.all([create('one'), create('two')]);
expect([first.json().success, second.json().success].sort()).toEqual([false, true]);
expect([first.json().errorCode, second.json().errorCode]).toContain('ALREADY_EXISTS');
const stored = await readCustomModelHosts(getDataDir());
expect(stored).toHaveLength(1);
});
it('a discovery that takes a moment does not undo an edit made while it ran', async () => {
const { app } = await setup();
await app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id: 'slow', label: 'Before', baseUrl: 'http://localhost:8202' },
});
let answer: (response: Response) => void = () => {};
fetchMock.mockImplementation(
(async () => new Promise<Response>((resolve) => (answer = resolve))) as unknown as typeof webviewFetch
);
const discovering = app.inject({ method: 'POST', url: '/api/model-endpoints/slow/discover-models' });
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalled());
// The endpoint is renamed while the probe is still out.
const renamed = await app.inject({
method: 'PUT',
url: '/api/model-endpoints/slow',
payload: { label: 'After', baseUrl: 'http://localhost:8202' },
});
expect(renamed.json().success).toBe(true);
answer(new Response(JSON.stringify({ data: [{ id: 'qwen3' }] }), { status: 200 }));
expect((await discovering).json()).toEqual({ success: true, data: { models: ['qwen3'] } });
const [stored] = await readCustomModelHosts(getDataDir());
// Both changes are there: the new name and the discovered list.
expect(stored.label).toBe('After');
expect(stored.models).toEqual(['qwen3']);
});
it('an endpoint deleted while its discovery ran stays deleted', async () => {
const { app } = await setup();
await app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id: 'gone', label: 'Gone', baseUrl: 'http://localhost:8203' },
});
let answer: (response: Response) => void = () => {};
fetchMock.mockImplementation(
(async () => new Promise<Response>((resolve) => (answer = resolve))) as unknown as typeof webviewFetch
);
const discovering = app.inject({ method: 'POST', url: '/api/model-endpoints/gone/discover-models' });
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalled());
await app.inject({ method: 'DELETE', url: '/api/model-endpoints/gone' });
answer(new Response(JSON.stringify({ data: [{ id: 'qwen3' }] }), { status: 200 }));
expect((await discovering).json()).toMatchObject({ success: false, errorCode: 'NOT_FOUND' });
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
});
it('deleting an id that is not saved leaves the file as it is', async () => {
const { app } = await setup();
await app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id: 'keep', label: 'Keep', baseUrl: 'http://localhost:8204' },
});
const before = { text: readFileSync(HOSTS(), 'utf8'), mtime: statSync(HOSTS()).mtimeMs };
const res = await app.inject({ method: 'DELETE', url: '/api/model-endpoints/never-saved' });
expect(res.json()).toEqual({ success: true, data: { id: 'never-saved' } });
expect(readFileSync(HOSTS(), 'utf8')).toBe(before.text);
expect(statSync(HOSTS()).mtimeMs).toBe(before.mtime);
});
describe('a store that does not parse', () => {
const BROKEN = '[{"id":"mine","label":"Mine","baseUrl":"http://localhost:9","apiKey":"sk-keep-me"},';
beforeEach(() => {
writeFileSync(HOSTS(), BROKEN);
vi.spyOn(console, 'warn').mockImplementation(() => {});
});
it.each([
['create', 'POST', '/api/model-endpoints', { id: 'new', label: 'New', baseUrl: 'http://localhost:8205' }],
['update', 'PUT', '/api/model-endpoints/mine', { label: 'Renamed', baseUrl: 'http://localhost:8205' }],
['delete', 'DELETE', '/api/model-endpoints/mine', undefined],
] as const)(
'%s is refused with 409 and the file is left byte for byte (it used to be replaced by one record)',
async (_label, method, url, payload) => {
const { app } = await setup();
const res = await app.inject({ method, url, ...(payload ? { payload } : {}) });
expect(res.statusCode).toBe(409);
expect(res.json()).toMatchObject({ success: false, errorCode: 'CONFLICT' });
expect(res.json().error).toContain('custom-model-hosts.json');
// The refusal never repeats the file, which holds a key.
expect(res.body).not.toContain('sk-keep-me');
expect(readFileSync(HOSTS(), 'utf8')).toBe(BROKEN);
}
);
it('reading still answers an empty list, as before', async () => {
const { app } = await setup();
expect((await app.inject({ method: 'GET', url: '/api/model-endpoints' })).json()).toEqual([]);
});
});
});
describe('the endpoint the local model install manages', () => {
const HOSTS = () => customModelHostsPath(getDataDir());
const STATE = () => dataPath('local-model.json');
const RECORD: CustomModelHost = {
id: 'local-ollama',
label: 'This Mac',
baseUrl: 'http://127.0.0.1:11435',
authStyle: 'bearer',
models: ['qwen3.6:35b-a3b'],
defaultModelId: 'qwen3.6:35b-a3b',
defaultContextLength: 131072,
};
/** The state file of an installed model whose connection is the record above. */
function markManaged(): void {
writeFileSync(
STATE(),
JSON.stringify({
schema: 1,
state: 'installed',
model: { id: 'qwen3.6:35b-a3b', label: 'Qwen3.6 35B-A3B', context: 131072 },
cli: { id: 'opencode', label: 'OpenCode' },
runtime: { version: '0.40.15', port: 11435, enabled: true },
connection: { endpointId: 'local-ollama', flagWasEnabled: false },
})
);
}
beforeEach(() => {
rmSync(STATE(), { force: true });
writeFileSync(HOSTS(), JSON.stringify([RECORD]));
});
afterEach(() => {
fetchMock.mockReset();
rmSync(STATE(), { force: true });
rmSync(HOSTS(), { force: true });
});
it('a new endpoint may not take the reserved id, whether or not a model is installed', async () => {
rmSync(HOSTS(), { force: true });
const { app } = await setup();
const res = await app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id: 'local-ollama', label: 'Mine', baseUrl: 'http://127.0.0.1:11434' },
});
expect(res.json()).toEqual({
success: false,
error: 'The endpoint id "local-ollama" is reserved for the model Codeman runs on this Mac. Pick another id.',
errorCode: 'INVALID_INPUT',
});
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
});
it('while it is managed it is not edited: its port stays the runtime Codeman supervises', async () => {
markManaged();
const { app } = await setup();
const res = await app.inject({
method: 'PUT',
url: '/api/model-endpoints/local-ollama',
payload: { label: 'This Mac', baseUrl: 'http://192.168.1.99:11434' },
});
expect(res.json()).toEqual({
success: false,
error:
'This endpoint belongs to the model Codeman runs on this Mac. Change or remove it in the local model window.',
errorCode: 'INVALID_INPUT',
});
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
});
it('while it is managed it is not deleted from here: the runtime would keep running without it', async () => {
markManaged();
const { app } = await setup();
const res = await app.inject({ method: 'DELETE', url: '/api/model-endpoints/local-ollama' });
expect(res.json()).toMatchObject({ success: false, errorCode: 'INVALID_INPUT' });
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
});
it('while it is managed a discovery answers the one model the install checked and asks the runtime nothing', async () => {
markManaged();
const { app } = await setup();
const res = await app.inject({ method: 'POST', url: '/api/model-endpoints/local-ollama/discover-models' });
expect(res.json()).toEqual({ success: true, data: { models: ['qwen3.6:35b-a3b'] } });
expect(fetchMock).not.toHaveBeenCalled();
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
});
it('it is listed like any other endpoint', async () => {
markManaged();
const { app } = await setup();
const list = (await app.inject({ method: 'GET', url: '/api/model-endpoints' })).json();
expect(list).toEqual([{ ...RECORD, apiKeySet: false }]);
});
it('a record with that id that nobody installed (made by hand) can still be edited and deleted by its owner', async () => {
// No state file: this feature wrote nothing, so the record is the person's own.
const { app } = await setup();
const update = await app.inject({
method: 'PUT',
url: '/api/model-endpoints/local-ollama',
payload: { label: 'My own Ollama', baseUrl: 'http://127.0.0.1:11434' },
});
expect(update.json().success).toBe(true);
expect((await readCustomModelHosts(getDataDir()))[0].label).toBe('My own Ollama');
const del = await app.inject({ method: 'DELETE', url: '/api/model-endpoints/local-ollama' });
expect(del.json()).toEqual({ success: true, data: { id: 'local-ollama' } });
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
});
it('any other endpoint is edited and deleted as before while a model is installed', async () => {
markManaged();
const { app } = await setup();
await app.inject({
method: 'POST',
url: '/api/model-endpoints',
payload: { id: 'office', label: 'Office', baseUrl: 'http://localhost:8210' },
});
const update = await app.inject({
method: 'PUT',
url: '/api/model-endpoints/office',
payload: { label: 'Office GPU', baseUrl: 'http://localhost:8210' },
});
expect(update.json().data.host.label).toBe('Office GPU');
await app.inject({ method: 'DELETE', url: '/api/model-endpoints/office' });
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
});
});