Skip to content

Fix/64 model discovery n plus 1 - #100

Merged
dkotter merged 8 commits into
Fueled:developfrom
saarnilauri:fix/64-model-discovery-n-plus-1
Sep 16, 2026
Merged

dkotter merged 8 commits into
Fueled:developfrom
saarnilauri:fix/64-model-discovery-n-plus-1

Conversation

@saarnilauri

@saarnilauri saarnilauri commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #64.

The problem

Listing models issued GET /api/tags and then one POST /api/show per model, sequentially. Availability went through the same path: createProviderAvailability() returned the php-ai-client SDK's ListModelsApiBasedProviderAvailability, whose isConfigured() is listModelMetadata() in a try/catch, so a yes/no "can this site reach Ollama?" check enumerated every model's capabilities.

The SDK caches the assembled list for 24h, but that cache is wp_cache_*. Without a persistent object cache, the default on most shared hosting and it does not survive the request, so the TTL never takes effect and every fresh request paid the full cost. At ~4-5s the check sits at the edge of typical request timeouts, and when jitter pushed GET /api/tags over the line the exception was swallowed and a healthy provider was reported as not connected. That is the intermittency in the issue.

What changed

Read capabilities from /api/tags when Ollama reports them. Ollama 0.34 includes capabilities and details.families in the tag listing, so the per-model request is pure waste there. Ollama Cloud does not, so /api/show is still needed for those hosts.

Cache the remaining /api/show answers in a transient, keyed by model digest. Transients live in the options table and do persist without an object cache, which is the part the SDK's cache cannot do here. The digest changes when a model is re-pulled, so an entry invalidates itself and there is nothing to flush by hand; the store is keyed by host, so changing the host misses rather than reading another instance's answers. Each listing prunes the store to the models the host still offers and writes only when the result changed. Entries are only written for models the host gave a digest for, and failed lookups are never cached.

Give availability its own cheap probe. OllamaProviderAvailability asks for the model tags and nothing else. The directory memoizes that listing, so checking availability and then listing models costs one request between them rather than one each. A stopped Ollama still reports as not connected, which is the behaviour #15 asked for.

Bound the discovery requests. createRequest() passed no RequestOptions, so /api/tags and /api/show inherited WordPress's 5s default with no connect timeout. Discovery now gets 10s request / 3s connect, filterable through ai_provider_for_ollama_discovery_request_timeout and ai_provider_for_ollama_discovery_connect_timeout. These are deliberately separate from the existing ai_provider_for_ollama_request_timeout / _connect_timeout: those get raised for slow generations, and a 120s generation timeout should not become a 120s connection check.

Measured

wp-env, no object-cache drop-in, Settings > Ollama host pointed at https://ollama.com (20 models). Numbers are from wp eval with a pre_http_request counter, timing isConfigured() and listModelMetadata() separately.

connection check listing HTTP requests
Cloud, before 4.0-4.5s — 21, on every request
Cloud, after (first) 0.22s 4.2s 21, once
Cloud, after (later) 0.18s 0.002s 1
Local Ollama 0.34, after 0.029s 0.002s 1, even cold

Capability detection is unchanged: spot-checked tools and vision across the cloud models and a local qwen2.5:3b.

Behaviour worth reviewing

  • New transient per host: ai_provider_for_ollama_model_details_<md5(host) prefix>, 30 day TTL, holding digest => {capabilities, families}. Pruned every listing, so it is bounded by the number of models the host offers.
  • buildModelMetadata() now takes a normalised capabilities/families pair rather than the raw /api/show body. That made getModelCapabilities() and isImageGenerationModel() dead code. The first only re-checked a shape that is now guaranteed, the second re-derived capabilities a second time. The empty-name guard the latter carried moved to the listing loop, which now skips tag entries without a usable name instead of building a model with an empty ID.
  • /api/show now sends model rather than the deprecated name.
  • Outside WordPress (Composer package use) the cache is inert and behaviour is what it was before.

Testing

phpunit 124 tests / 226 assertions, phpcs and phpstan clean, run at each of the five commits rather than only at the tip. The mock transporter now records every request, which is what lets the new tests assert request counts. That is the thing this PR is actually about. New coverage: capabilities read from tags, mixed tag entries, vision from details.families, digest reuse across directory instances, changed digest, entries without a digest, uncached failures, and the availability check in isolation.

OllamaModelDetailsCache uses no SDK types, so unlike the rest of includes/ it stays inside PHPStan analysis; OllamaProviderAvailability is added to the existing exclusion list.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Issue analyze, planning, code writing and running the test. Code was reviewed by me. Also this PR message was drafted with AI and the final wording was reviewed and edited by me.

ðŸĪ– Generated with Claude Code

Open WordPress Playground Preview

saarnilauri and others added 5 commits September 15, 2026 10:30
The mock only kept the last request, so nothing could assert how many
requests a code path makes. Keep the full list and expose get_requests()
and get_request_count() alongside the existing get_last_request().

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Listing models issued a GET /api/tags followed by a POST /api/show for
every model returned, sequentially. Recent Ollama versions already report
capabilities and families in the tag listing, so the extra request per
model is pure waste there: against a local Ollama 0.34 the listing now
costs a single request.

Capabilities are normalised at the point the payload is parsed, so
buildModelMetadata() receives a flat capabilities/families pair instead of
the raw /api/show body. That makes getModelCapabilities() and
isImageGenerationModel() dead code: the first only re-checked the shape
that is now guaranteed, and the second re-derived capabilities a second
time. The empty-model-name guard the latter carried moves to the listing
loop, which now skips tag entries without a usable name rather than
building a model with an empty ID.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Ollama Cloud does not report capabilities in /api/tags, so those hosts
still need one /api/show per model. The SDK caches the assembled list for
24 hours, but that cache is backed by wp_cache_*, which does not survive
the request on a site without a persistent object cache — the default on
most shared hosting. The 24h TTL therefore never takes effect and every
fresh request pays the full cost: measured at ~4.2s and 21 requests
against ollama.com.

Keep the answers in a transient instead, which lives in the options table
and does persist. Entries are keyed by the model digest, so a re-pulled
model invalidates its own entry and nothing has to be flushed by hand, and
the store is keyed by host, so changing the host misses rather than reads
another instance's answers. Each listing prunes the store to the models
the host still offers and writes only when the result changed.

Against ollama.com the first listing is unchanged and every later one
costs a single request. Outside WordPress the cache is inert.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
createProviderAvailability() returned the SDK's
ListModelsApiBasedProviderAvailability, whose isConfigured() is
listModelMetadata() in a try/catch. A yes/no "can this site reach Ollama?"
check therefore enumerated every model's capabilities. Against
ollama.com that took ~4.0-4.5s, close enough to typical request timeouts
that ordinary latency variance made the check throw and report a healthy
provider as not connected — the intermittency in Fueled#64.

Ask the host for its model tags instead, which is the cheapest complete
answer it gives: reaching it proves the host is up, speaks Ollama and
accepted the credentials. The directory memoizes that listing, so checking
availability and then listing models still costs one request between them
rather than one each. The check now takes ~0.2s and keeps the behaviour
Fueled#15 asked for: a stopped Ollama still reports as not connected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
createRequest() passed no RequestOptions, so /api/tags and /api/show
inherited whatever the HTTP client defaults to — 5 seconds with no connect
timeout under WordPress. A host that hangs rather than refuses therefore
stalled the admin screen for that long per request, once per model on the
listing path.

Give discovery its own tighter budget, 10s request and 3s connect, with
its own filters. The existing ai_provider_for_ollama_request_timeout and
ai_provider_for_ollama_connect_timeout are deliberately left alone: those
are raised for slow generations, and a 120s generation timeout must not
become a 120s connection check.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@jeffpaul jeffpaul added this to the 1.3.0 milestone Sep 15, 2026
@jeffpaul
jeffpaul requested a review from dkotter September 15, 2026 14:58
@dkotter
dkotter merged commit e10f8de into Fueled:develop Sep 16, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants