Skip to content

fix: recover model cache after provider failures - #1587

Open
cccplex wants to merge 2 commits into
CopilotC-Nvim:mainfrom
cccplex:fix/copilot-model-cache
Open

cccplex wants to merge 2 commits into
CopilotC-Nvim:mainfrom
cccplex:fix/copilot-model-cache

Conversation

@cccplex

@cccplex cccplex commented Oct 22, 2026 •

Copy link
Copy Markdown

Problem

Failed model discovery is cached as an empty list for five minutes, hiding
available models and replacing previously successful results. Retrying on every
call instead can flood the API, especially when chat completion runs while typing.

Fix

  • Preserve cached models on failure; allow another attempt after a 60-second cooldown.
  • Share pending discovery requests, including authentication, between callers.
  • Only trigger chat completion for a trigger word at the cursor.
  • Still cache genuinely successful empty results.

This fixes failure caching and excessive discovery attempts, not the underlying
invalid API response. Existing curl retries are unchanged.

Tests

All 112 tests, StyLua, and whitespace checks pass. Regression coverage includes
cooldowns, concurrent requests, stale-cache preservation, and completion triggers.
Manually verified with a real Copilot account.

Related: #1343 also changes discovery error handling; this approach preserves
cached models and allows other providers to remain usable.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

First review of this model-cache recovery fix (1 commit, 2 files).

  • get_cached preserves stale data on failure (lua/CopilotChat/client.lua:83-91) — a nil from the filler now returns any previously cached value and leaves _expires_at untouched, so the entry stays expired and is refetched on the next call instead of caching an empty result for the full 5-minute TTL. Because the inner cache is a plain table (not the defaulttable wrapper), cache[key] or {} does not auto-vivify and _expires_at is only ever written alongside cache[key], so the _expires_at > now comparison can't be reached with a nil/table value.
  • Client:models reports failure as nil (lua/CopilotChat/client.lua:243-255) — the auth-failure and get_models-failure branches now return nil, and a nil provider result is passed through unchanged, engaging the recovery path. Genuinely successful empty lists are still cached.
  • New regression suite (tests/client_spec.lua) — 243 lines covering first-fetch caching, retry after auth/model-fetch failure, the expiry boundary, stale preservation then recovery, preservation when auth refresh fails, persistent-failure retry without an internal loop, genuine empty-list caching, nil provider result not cached, independent per-provider caching, and an end-to-end auto recovery after an invalid JSON response.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Re-reviewed the delta since the prior pullfrog review (efadf6a → 597b597), which extends the cache-recovery fix with retry throttling, shared in-flight fetches, and a completion-trigger correction. All 112 tests pass locally on Neovim 0.11.4 with plenary.

  • Added a 60-second retry cooldown to get_cached — failed fills now record <key>_retry_at and preserve any stale value, so a failing provider is contacted at most once per cooldown instead of on every call.
  • Coalesced concurrent fills with a Condvar — a <key>_pending entry makes concurrent callers (including the authentication step inside the filler) wait for the in-flight request, then return the refreshed or stale result.
  • Hardened the owner path with pcall — the pending entry is always cleared and waiters always notified; unexpected filler errors are re-raised to the owner and to waiting callers.
  • Anchored the completion trigger to the cursor — M.complete now matches info.pattern .. '$' and bails when prefix == '', so TextChangedI/<Tab> over ordinary chat text no longer schedules model discovery.
  • Extended the regression suite — new cooldown, shared-fetch, shared-auth, waiter-release and stale-preservation tests in tests/client_spec.lua, plus tests/completion_spec.lua for trigger anchoring.

I traced the new ordering for the cache path: _pending is cleared before notify_all(), and the read→wait() sequence contains no yield, so a waiter cannot miss a notification; stale values survive both failure shapes and the cooldown boundary behaves as asserted. The completion change is also benign for the async omnifunc path (-2 means "stay in completion mode", and the next keystroke exits it).

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@cccplex
cccplex force-pushed the fix/copilot-model-cache branch from c8ed65f to e5c4b7a Compare November 10, 2026 09:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant