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

+69 -5
View File
@@ -15,6 +15,15 @@
* Rules:
* - The id is reserved. A record with it that this feature did not write is never
* overwritten: the connect step fails with `endpoint-id-taken`.
* - The connection is CLAIMED before it is written: the install job records `connection` in
* the state file first (`inspectLocalEndpoint()` tells it what is there and what the flag
* is), then calls `connectLocalModel()` with `ours: true`. A state write that fails, or a
* server that is killed between the two, therefore never leaves a record of ours that the
* state file does not know: the worst case is a claim with nothing behind it, which a
* resumed job fills in and Remove undoes.
* - Second net: a record with the reserved id that already says exactly what ours would say
* (`isSameLocalEndpoint()`) is ours whatever `ours` says. Nobody else can have made it: the
* endpoint routes refuse the reserved id.
* - Both writes are idempotent, so a resumed job writes them again.
* - Undoing removes our record, and switches the flag off again only when this feature
* switched it on AND no other endpoint is saved.
@@ -27,7 +36,10 @@
import { mutateCustomModelHosts } from '../custom-model-hosts-writer.js';
import type { CustomModelHost } from '../custom-model-hosts.js';
import { getDataDir } from '../config/instance.js';
import { readCustomModelHosts } from '../custom-model-hosts.js';
import { getErrorMessage, type LocalModelErrorCode } from '../types.js';
import { readJsonConfig, SETTINGS_PATH } from '../web/route-helpers.js';
import { CustomModelHostSchema } from '../web/schemas.js';
import { mutateSettingsFile } from '../web/settings-writer.js';
import { readLocalModelState } from './state-store.js';
@@ -63,10 +75,58 @@ export function buildLocalEndpointRecord(input: LocalEndpointInput): CustomModel
};
}
/**
* Whether a saved record says exactly what our record for `input` says: the same address, the
* one model, its context, no key. What discovery may have added to it (context lengths, a
* time stamp) does not count. Pure.
*/
export function isSameLocalEndpoint(host: CustomModelHost, input: LocalEndpointInput): boolean {
const ours = buildLocalEndpointRecord(input);
return (
host.id === ours.id &&
host.label === ours.label &&
host.baseUrl === ours.baseUrl &&
host.authStyle === ours.authStyle &&
host.apiKey === undefined &&
Array.isArray(host.models) &&
host.models.length === 1 &&
host.models[0] === input.modelId &&
host.defaultModelId === ours.defaultModelId &&
host.defaultContextLength === ours.defaultContextLength
);
}
/** What the connect step finds before it claims the connection. */
export interface LocalEndpointInspection {
/**
* The saved record with the reserved id: there is none, it says what ours would say, or it
* says something else (somebody else's: `endpoint-id-taken`).
*/
record: 'absent' | 'same' | 'other';
/** `customModelEndpointsEnabled` right now: what the claim keeps as the value before this feature. */
flagEnabled: boolean;
}
/**
* Looks at the endpoint store and the settings flag WITHOUT writing either. The install job
* calls it before it claims the connection in the state file. Rejects when the endpoint store
* cannot be read; a settings file that cannot be read counts as "the flag is off".
*/
export async function inspectLocalEndpoint(input: LocalEndpointInput): Promise<LocalEndpointInspection> {
const hosts = await readCustomModelHosts(getDataDir());
const existing = hosts.find((host) => host.id === LOCAL_MODEL_ENDPOINT_ID);
const settings = await readJsonConfig<Record<string, unknown>>(SETTINGS_PATH, 'settings.json', {});
return {
record: !existing ? 'absent' : isSameLocalEndpoint(existing, input) ? 'same' : 'other',
flagEnabled: settings[ENDPOINTS_FLAG] === true,
};
}
export interface ConnectLocalModelInput extends LocalEndpointInput {
/**
* True when the state file already records a connection of ours: an existing record with
* our id is then ours and is replaced. False: such a record is somebody else's.
* our id is then ours and is replaced. False: such a record is somebody else's, unless it
* says exactly what ours would say (`isSameLocalEndpoint()`).
*/
ours: boolean;
/**
@@ -106,7 +166,7 @@ export async function connectLocalModel(input: ConnectLocalModelInput): Promise<
outcome = await mutateCustomModelHosts<'added' | 'replaced' | 'taken'>((hosts) => {
const index = hosts.findIndex((host) => host.id === record.id);
if (index === -1) return { hosts: [...hosts, record], result: 'added' };
if (!input.ours) return { result: 'taken' };
if (!input.ours && !isSameLocalEndpoint(hosts[index], input)) return { result: 'taken' };
const next = [...hosts];
next[index] = record;
return { hosts: next, result: 'replaced' };
@@ -158,13 +218,17 @@ async function removeLocalEndpoint(): Promise<number> {
});
}
/** Moves our record to another port (the answer to "Use another port" once connected). A missing record is not an error. */
/**
* Moves our record to another port (the answer to "Use another port" once connected). A
* missing record is not an error, and a record that is already there is not written again.
*/
export async function moveLocalEndpoint(port: number): Promise<void> {
const baseUrl = `http://127.0.0.1:${port}`;
await mutateCustomModelHosts<void>((hosts) => {
const index = hosts.findIndex((host) => host.id === LOCAL_MODEL_ENDPOINT_ID);
if (index === -1) return { result: undefined };
if (index === -1 || hosts[index].baseUrl === baseUrl) return { result: undefined };
const next = [...hosts];
next[index] = { ...next[index], baseUrl: `http://127.0.0.1:${port}` };
next[index] = { ...next[index], baseUrl };
return { hosts: next, result: undefined };
});
}
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -31,6 +31,7 @@
*/
import type { LocalModelErrorCode } from '../types.js';
import { formatGb } from './format.js';
/** How often a pull that ended on an error line or a cut stream is started again. */
export const PULL_RETRIES = 5;
@@ -683,7 +684,8 @@ export function judgeLoaded(
return {
ok: false,
code: 'does-not-fit',
detail: `${model.sizeVram} of ${model.size} bytes are on the GPU`,
// A size a person reads: decimal GB, like every other size of this feature.
detail: `${formatGb(model.sizeVram)} of ${formatGb(model.size)} GB of the model are on the GPU`,
};
}
if (model.contextLength !== context) {