Repository navigation
Fix/64 model discovery n plus 1 - #100
Merged
dkotter merged 8 commits intoSep 16, 2026
Merged
Conversation
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>
âĶ the name, not model. Ensure model tags are cleared properly when cache is invalidated
dkotter
approved these changes
Sep 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #64.
The problem
Listing models issued
GET /api/tagsand then onePOST /api/showper model, sequentially. Availability went through the same path:createProviderAvailability()returned thephp-ai-clientSDK'sListModelsApiBasedProviderAvailability, whoseisConfigured()islistModelMetadata()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 pushedGET /api/tagsover 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/tagswhen Ollama reports them. Ollama 0.34 includescapabilitiesanddetails.familiesin the tag listing, so the per-model request is pure waste there. Ollama Cloud does not, so/api/showis still needed for those hosts.Cache the remaining
/api/showanswers 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.
OllamaProviderAvailabilityasks 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 noRequestOptions, so/api/tagsand/api/showinherited WordPress's 5s default with no connect timeout. Discovery now gets 10s request / 3s connect, filterable throughai_provider_for_ollama_discovery_request_timeoutandai_provider_for_ollama_discovery_connect_timeout. These are deliberately separate from the existingai_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 > Ollamahost pointed athttps://ollama.com(20 models). Numbers are fromwp evalwith apre_http_requestcounter, timingisConfigured()andlistModelMetadata()separately.Capability detection is unchanged: spot-checked tools and vision across the cloud models and a local
qwen2.5:3b.Behaviour worth reviewing
ai_provider_for_ollama_model_details_<md5(host) prefix>, 30 day TTL, holdingdigest => {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/showbody. That madegetModelCapabilities()andisImageGenerationModel()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/shownow sendsmodelrather than the deprecatedname.Testing
phpunit124 tests / 226 assertions,phpcsandphpstanclean, 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 fromdetails.families, digest reuse across directory instances, changed digest, entries without a digest, uncached failures, and the availability check in isolation.OllamaModelDetailsCacheuses no SDK types, so unlike the rest ofincludes/it stays inside PHPStan analysis;OllamaProviderAvailabilityis 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