fix(local-model): claim the connection first, keep an outcome the disk refused, say why a start failed

The connect step records the connection in the state file BEFORE it writes
the endpoint record and the settings flag (the flag's earlier value read by
inspectLocalEndpoint()), so a state write that fails or a server killed
between the two never leaves a record of ours that the state file does not
know; a record under the reserved id that says exactly what ours would say
is ours. changePort() writes the state first and puts the old port back when
the endpoint move is refused.

A job whose end could not be written (a full disk) is kept in memory: the
view shows it, Resume and a new install start from it, the next verb writes
it, and Cancel never answers a 500. A first install on a full disk is a 507
with the sentence. "installing" in the file while this process runs no job
always reads as paused or that failure.

An installed model whose runtime does not start says so (runtime-start-failed
with Start as the next step, and the launch check says it too); the stored
runtime version is started while its folder is unpacked. Stop, Remove and
the boot of anything that is not a running installation end a runtime an
earlier server left behind before any file is deleted. Stop and "Use another
port" answer 409 sessions-open while a live session uses the model. A pull
that failed because the runtime is gone is runtime-stopped, not a network
problem. After "does not fit" only a model that needs less memory is offered.
A replaced job's partial blobs are deleted. Every 400 carries its sentence as
data.message, and the six inline sentences are labels with zh-CN entries.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-06 23:54:45 +01:00
1 parent 6319fffce1
commit fa978008d2
13 files changed
+1647 -184

No files matched your search

+1 -1
View File
@@ -777,7 +777,7 @@ describe('the endpoint the local model install manages', () => {
});
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.',
error: 'This endpoint id is reserved for the model Codeman runs on this Mac. Pick another id.',
errorCode: 'INVALID_INPUT',
});
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
+66 -3
View File
@@ -657,20 +657,43 @@ describe('the install verbs', () => {
expect(f.service.install).toHaveBeenCalledWith(INSTALL_BODY, { username: 'alice', ip: '127.0.0.1' });
});
it('a row the server does not offer is a 400 with the service own sentence', async () => {
it('a row the server does not offer is a 400 with the service own sentence, as text and as data', async () => {
const f = fakes();
f.service.install.mockResolvedValue({
ok: false,
status: 400,
message: 'This model at this context is not offered for this Mac right now.',
message: { text: LOCAL_MODEL_INSTALL_LABELS.refusedAgentNotInstallable, vars: { cli: 'OpenCode' } },
});
const { app } = await harness(f.deps);
const res = await app.inject({ method: 'POST', url: '/api/local-model/install', payload: INSTALL_BODY });
expect(res.statusCode).toBe(400);
// `error` is the English sentence with its slots filled; `data.message` is the sentence
// with its slots apart, which the window prints in its own language.
expect(res.json()).toEqual({
success: false,
error: 'This model at this context is not offered for this Mac right now.',
error: 'OpenCode is not installed and cannot be installed from here.',
errorCode: 'INVALID_INPUT',
data: {
message: { text: '{cli} is not installed and cannot be installed from here.', vars: { cli: 'OpenCode' } },
},
});
});
it('a first install on a full disk is a 507 with the sentence, not a 500', async () => {
const f = fakes();
f.service.install.mockResolvedValue({
ok: false,
status: 507,
message: { text: LOCAL_MODEL_INSTALL_LABELS.errDiskFull },
});
const { app } = await harness(f.deps);
const res = await app.inject({ method: 'POST', url: '/api/local-model/install', payload: INSTALL_BODY });
expect(res.statusCode).toBe(507);
expect(res.json()).toEqual({
success: false,
error: 'The disk is full. Free up space, then try again.',
errorCode: 'OPERATION_FAILED',
data: { message: { text: 'The disk is full. Free up space, then try again.' } },
});
});
@@ -743,6 +766,46 @@ describe('the install verbs', () => {
expect(f.service.resume.mock.calls[0][1]).toEqual({ username: 'admin', ip: '127.0.0.1' });
});
it('resume and runtime hand the service the live sessions on the model, read at the moment of the request', async () => {
const f = fakes();
const sessions = new Map<string, SessionLike>([
['a', { name: 'w1-app', customModel: { endpointId: 'local-ollama' } }],
['b', { name: 'w2-app', customModel: { endpointId: 'office-gpu' } }],
]);
const { app } = await createRouteTestHarness((instance) =>
registerLocalModelRoutes(instance, { sessions }, f.deps)
);
await app.inject({ method: 'POST', url: '/api/local-model/runtime', payload: { action: 'stop' } });
await app.inject({ method: 'POST', url: '/api/local-model/resume', payload: { newPort: true } });
sessions.delete('a');
await app.inject({ method: 'POST', url: '/api/local-model/runtime', payload: { action: 'stop' } });
expect(f.service.runtime.mock.calls.map((args) => args[1])).toEqual([['w1-app'], []]);
expect(f.service.resume.mock.calls[0][2]).toEqual(['w1-app']);
});
it('a stop while a live session uses the model: 409 with the session names', async () => {
const f = fakes();
f.service.runtime.mockImplementation(async (_request, names = []) => ({
ok: false,
status: 409,
conflict: {
reason: 'sessions-open',
message: { text: LOCAL_MODEL_INSTALL_LABELS.conflictSessionsOpen, vars: { sessions: names.join(', ') } },
sessions: [...names],
},
}));
const { app } = await harness(f.deps, undefined, [
{ name: 'w1-app', customModel: { endpointId: 'local-ollama' } },
]);
const res = await app.inject({ method: 'POST', url: '/api/local-model/runtime', payload: { action: 'stop' } });
expect(res.statusCode).toBe(409);
expect(res.json()).toMatchObject({
errorCode: 'CONFLICT',
error: 'Sessions are still using this model: w1-app. Close them first.',
data: { reason: 'sessions-open', sessions: ['w1-app'] },
});
});
it('resume refuses a body it does not know', async () => {
const f = fakes();
const { app } = await harness(f.deps);