Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (antigravity): serialize the refresh fan-out and name the vanished-lead skip (#4154)

[._.]/ Adam Eivy8d10c48697fe
diff --git a/server/lib/aiToolkit/internal/modelFetchers.js b/server/lib/aiToolkit/internal/modelFetchers.js
index 9ec0f36d91..2511809ce4 100644
--- a/server/lib/aiToolkit/internal/modelFetchers.js
+++ b/server/lib/aiToolkit/internal/modelFetchers.js
@@ -204,6 +204,12 @@ export function ollamaRefreshGroupKey(provider, table = MODEL_FETCHERS) {
if (provider.apiKey) return null;
const endpoint = String(provider.endpoint || '');
if (!endpoint.includes('ollama') && !endpoint.includes(':11434')) return null;
+ // Only a trailing slash is normalized away here — NOT an OpenAI-compat
+ // `/v1`, the way `ollamaBaseFromProvider` does for the tools namespace.
+ // The api arm probes `${endpoint}/api/tags` and then `${endpoint}/models`
+ // verbatim, so `…:11434` and `…:11434/v1` are two DIFFERENT requests (only
+ // the `/v1` spelling answers `/models`). Folding them together would share
+ // one provider's catalog onto another whose own probe would have 404'd.
return `api:${endpoint.replace(/\/+$/, '')}`;
}
return resolveModelFetcher(provider, table)?.key === 'ollama'
diff --git a/server/lib/aiToolkit/internal/modelFetchers.test.js b/server/lib/aiToolkit/internal/modelFetchers.test.js
index 617b121cfb..a8ac7814a7 100644
--- a/server/lib/aiToolkit/internal/modelFetchers.test.js
+++ b/server/lib/aiToolkit/internal/modelFetchers.test.js
@@ -72,11 +72,27 @@ describe('ollamaRefreshGroupKey — one probe per daemon, not one per provider',
});
it('normalizes trailing slashes and an OpenAI-compat /v1 to the same daemon key', () => {
+ // Safe here because `_fetchOllamaToolCapableModels` itself resolves the base
+ // through `ollamaBaseFromProvider` — both spellings issue the SAME request.
const bare = { id: 'a', type: 'tui', ollamaBacked: true, envVars: { ANTHROPIC_BASE_URL: 'http://localhost:11434' } };
const suffixed = { id: 'b', type: 'tui', ollamaBacked: true, envVars: { ANTHROPIC_BASE_URL: 'http://localhost:11434/v1/' } };
expect(ollamaRefreshGroupKey(suffixed)).toBe(ollamaRefreshGroupKey(bare));
});
+ it('does NOT fold /v1 away for an api provider — that arm probes the endpoint verbatim', () => {
+ // `_refreshAPIProviderModels` requests `${endpoint}/api/tags` then
+ // `${endpoint}/models` with no normalization, so `…:11434` and
+ // `…:11434/v1` are genuinely different probes (only the `/v1` spelling
+ // answers `/models`). Sharing one answer between them would persist a
+ // catalog onto a provider whose own refresh would have failed.
+ const bare = { id: 'a', type: 'api', endpoint: 'http://localhost:11434' };
+ const v1 = { id: 'b', type: 'api', endpoint: 'http://localhost:11434/v1' };
+ expect(ollamaRefreshGroupKey(bare)).not.toBe(ollamaRefreshGroupKey(v1));
+ // A bare trailing slash IS just a spelling of the same URL, so it folds.
+ expect(ollamaRefreshGroupKey({ id: 'c', type: 'api', endpoint: 'http://localhost:11434/v1/' }))
+ .toBe(ollamaRefreshGroupKey(v1));
+ });
+
it('returns null — never a shared bucket — for anything that is not an Ollama probe', () => {
// A null key means "refresh me individually". Treating it as a group would
// persist one vendor's catalog onto every other provider.
diff --git a/server/lib/aiToolkit/internal/ollamaBacked.js b/server/lib/aiToolkit/internal/ollamaBacked.js
index 4b4642684d..0a6216e767 100644
--- a/server/lib/aiToolkit/internal/ollamaBacked.js
+++ b/server/lib/aiToolkit/internal/ollamaBacked.js
@@ -30,9 +30,10 @@ export function isOllamaBackedProvider(provider) {
*
* Lives here beside {@link isOllamaBackedProvider} rather than in `providers.js`
* so `internal/modelFetchers.js` can build a refresh group key on it without
- * importing back into `providers.js` and forming a module cycle. Re-exported
- * from `providers.js` (and `server/services/providers.js`) for hosts that group
- * providers by daemon.
+ * importing back into `providers.js` and forming a module cycle. Deliberately
+ * NOT re-exported from `providers.js`: `ollamaRefreshGroupKey` is the contract
+ * hosts group on, and exporting the normalizer alongside it only invites a
+ * caller to re-derive the grouping rule and drift from the real dispatch.
*/
export function ollamaBaseFromProvider(provider) {
const base = String(provider?.envVars?.ANTHROPIC_BASE_URL || provider?.endpoint || 'http://localhost:11434');
diff --git a/server/services/localLlm.js b/server/services/localLlm.js
index a3eb5b35df..3b7f5a7ba0 100644
--- a/server/services/localLlm.js
+++ b/server/services/localLlm.js
@@ -861,15 +861,18 @@ async function listVisionCliModels() {
// ---- install / delete --------------------------------------------------------
+// Probe-failure sentinel, distinct from `fetchProviderModels`' own `null` ("no
+// such provider") and from a legitimately empty `[]` catalog. Three outcomes,
+// three values — collapsing any two of them loses a real distinction.
+const PROBE_FAILED = Symbol('ollama-probe-failed')
+
/**
* Fetch one Ollama probe for a group of providers that share a daemon + probe
* shape, then persist that single answer onto each of them.
*
* The probe is issued through the group's first member; every member (that one
* included) is then written via `updateProvider`, so no provider is special-
- * cased. Writes are sequential because `saveProviders` is a whole-file
- * read-modify-write — firing them concurrently would have members clobber each
- * other's models.
+ * cased.
*
* A failed probe skips the whole group with ONE log line: every member would
* have failed identically against the same unreachable daemon, so N copies of
@@ -879,11 +882,18 @@ async function refreshOllamaProviderGroup(group) {
const [lead] = group
const models = await fetchProviderModels(lead.id).catch((err) => {
console.error(`⚠️ Failed to refresh models for ${group.length} Ollama-backed provider(s) via ${lead.id}: ${err.message}`)
- return null
+ return PROBE_FAILED
})
- // `null` = probe failed or the provider vanished; `[]` = a real, empty catalog
- // (the user just deleted their last model) and MUST still be persisted.
- if (models === null) return
+ if (models === PROBE_FAILED) return
+ if (models === null) {
+ // The lead was deleted between listing the providers and probing it. Say so
+ // — silently dropping the whole group would leave its siblings stale with no
+ // trace of why.
+ console.error(`⚠️ Skipped refreshing ${group.length} Ollama-backed provider(s): lead provider ${lead.id} no longer exists`)
+ return
+ }
+ // `[]` is a real, empty catalog (the user just deleted their last model) and
+ // MUST still be persisted — only the two cases above are skips.
for (const p of group) {
await updateProvider(p.id, { models }).catch((err) => {
console.error(`⚠️ Failed to save refreshed models for provider ${p.id}: ${err.message}`)
@@ -925,17 +935,22 @@ function refreshOllamaBackedProviders() {
// Only a group with something to SHARE takes the split fetch/apply path; a
// lone member keeps the plain one-call refresh, so the common single-daemon
// single-provider install behaves exactly as it did before.
- const shared = []
+ const work = []
for (const group of groups.values()) {
if (group.length === 1) singles.push(group[0])
- else shared.push(group)
+ else work.push(() => refreshOllamaProviderGroup(group))
}
- return Promise.all([
- ...singles.map((p) => refreshProviderModels(p.id).catch((err) => {
+ for (const p of singles) {
+ work.push(() => refreshProviderModels(p.id).catch((err) => {
console.error(`⚠️ Failed to refresh models for provider ${p.id} after Ollama model change: ${err.message}`)
- })),
- ...shared.map(refreshOllamaProviderGroup),
- ])
+ }))
+ }
+ // Sequential, not `Promise.all`: every one of these ends in `saveProviders`,
+ // a whole-file read-modify-write of providers.json. Run concurrently they
+ // interleave and clobber each other's model arrays. This whole chain is
+ // fire-and-forget background work behind an install/delete, so nothing is
+ // waiting on the wall-clock saving.
+ return work.reduce((tail, run) => tail.then(run), Promise.resolve())
}).catch((err) => {
console.error(`⚠️ Failed to list providers for post-install Ollama refresh: ${err.message}`)
})
diff --git a/server/services/localLlm.test.js b/server/services/localLlm.test.js
index 3d31bf525a..8c6ebfc181 100644
--- a/server/services/localLlm.test.js
+++ b/server/services/localLlm.test.js
@@ -318,6 +318,28 @@ describe('localLlm', () => {
errSpy.mockRestore();
});
+ it('logs rather than silently dropping a group whose lead provider vanished', async () => {
+ // `fetchProviderModels` answers null (not a throw) for a provider deleted
+ // between the listing and the probe — a third outcome that must not be
+ // confused with a failed probe or an empty catalog.
+ mocks.providers.getAllProviders.mockResolvedValueOnce({
+ providers: [
+ { id: 'local-a', type: 'cli', ollamaBacked: true },
+ { id: 'local-b', type: 'tui', ollamaBacked: true }
+ ]
+ });
+ mocks.providers.fetchProviderModels.mockResolvedValueOnce(null);
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+
+ await svc.installModel('ollama', 'llama3.2');
+ await flushMicrotasks();
+
+ expect(mocks.providers.updateProvider).not.toHaveBeenCalled();
+ expect(errSpy).toHaveBeenCalledTimes(1);
+ expect(String(errSpy.mock.calls[0][0])).toMatch(/local-a no longer exists/);
+ errSpy.mockRestore();
+ });
+
it('persists a legitimately EMPTY catalog across the group', async () => {
// `[]` (the user just deleted their last model) is a real answer and must
// be written; only `null` — probe failed — is the skip signal.