ai: cache the models manifest, skip it offline, show a loading state

The Manage tab fetched the CDN manifest and its signature on every
catalog request with a 30s timeout each, and the tab polls every 30s.
On a black-holed or offline network every poll paid up to 60s before
falling back to the bundled list, the frontend's 60s limit threw the
response away, and the tab showed a blank page the whole time.

get_models_catalog now skips the CDN while the have_internet flag is
explicitly clear, reuses the last attempt (success or failure) for an
hour, and uses a 5s timeout for the 2KB manifest. A skipped attempt is
not cached so the CDN is tried as soon as internet comes up. The cache
is per process and is cleared between tests by an autouse fixture.

The Manage tab shows "Loading the model catalog" while the first
response is pending, and on failure shows the error with a Retry
button and a note that the API may still be starting, instead of
rendering nothing.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-09-24 18:28:11 -06:00
parent 5b1b0559b5
commit 861e183bd2
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 136 additions and 9 deletions

View file

@ -54,6 +54,7 @@ async def test_models_catalog_fallback(async_client):
assert source == 'bundled'
assert models == catalog.AI_MODELS
catalog.clear_models_catalog_cache() # the failed attempt is cached; see test_models_catalog_is_cached
manifest = dict(version=1, models=[dict(name='new.gguf', tier='small')])
with mock.patch('modules.ai.catalog.fetch_models_manifest', return_value=manifest):
models, source = await catalog.get_models_catalog()
@ -285,3 +286,53 @@ async def test_manage_catalog_skips_unloadable_custom_models(async_client, test_
content=json.dumps(dict(active_model=bad)))
assert response.status_code == HTTPStatus.BAD_REQUEST, bad
assert 'cannot be loaded' in response.json['error']
@pytest.mark.asyncio
async def test_models_catalog_is_cached(async_client):
"""The CDN manifest is fetched once per hour, and a failed attempt is cached too.
The Manage tab polls the catalog every 30s; without a cache an offline device paid the fetch
timeout on every poll."""
manifest = dict(version=1, models=[dict(name='new.gguf', tier='small')])
with mock.patch('modules.ai.catalog.fetch_models_manifest', return_value=manifest) as fetch:
assert (await catalog.get_models_catalog())[1] == 'cdn'
assert (await catalog.get_models_catalog())[1] == 'cdn'
assert fetch.call_count == 1
# A failure is cached for the same window: the next call does not retry.
catalog.clear_models_catalog_cache()
with mock.patch('modules.ai.catalog.fetch_models_manifest', side_effect=RuntimeError('offline')) as fetch:
assert (await catalog.get_models_catalog())[1] == 'bundled'
assert (await catalog.get_models_catalog())[1] == 'bundled'
assert fetch.call_count == 1
# Once the window passes, the CDN is tried again.
with mock.patch('modules.ai.catalog.fetch_models_manifest', return_value=manifest) as fetch, \
mock.patch('modules.ai.catalog.time.monotonic', return_value=1e9):
assert (await catalog.get_models_catalog())[1] == 'cdn'
assert fetch.call_count == 1
@pytest.mark.asyncio
async def test_models_catalog_skips_cdn_without_internet(async_client, flags_lock):
"""When the have_internet flag is clear the bundled catalog is served without touching the
network, so an offline Manage page renders immediately. A skipped attempt is not cached: the
CDN is tried as soon as internet comes up."""
from wrolpi import flags
flags.have_internet.clear()
with mock.patch('modules.ai.catalog.fetch_models_manifest') as fetch:
models, source = await catalog.get_models_catalog()
assert source == 'bundled' and models == catalog.AI_MODELS
fetch.assert_not_called()
flags.have_internet.set()
manifest = dict(version=1, models=[dict(name='new.gguf', tier='small')])
with mock.patch('modules.ai.catalog.fetch_models_manifest', return_value=manifest) as fetch:
assert (await catalog.get_models_catalog())[1] == 'cdn'
fetch.assert_called_once()
def test_manifest_fetch_timeout_is_short():
"""A 2KB manifest must not wait 30s per request on a black-holed network."""
assert catalog.MANIFEST_TIMEOUT <= 5