Skip to content

fix(run_agent): invalidate cached system prompt on fallback switch and restore#6381

Open
SeeYangZhi wants to merge 4 commits into
NousResearch:mainfrom
SeeYangZhi:fix/fallback-system-prompt-cache-invalidation
Open

fix(run_agent): invalidate cached system prompt on fallback switch and restore#6381
SeeYangZhi wants to merge 4 commits into
NousResearch:mainfrom
SeeYangZhi:fix/fallback-system-prompt-cache-invalidation

Conversation

@SeeYangZhi

Copy link
Copy Markdown
Contributor

Closes #6380

When the primary model fails and the agent switches to a fallback provider, the CLI session header continues to display the primary model name because _build_system_prompt() caches the prompt and the fallback/restore paths never invalidate it.

Changes

  • _try_activate_fallback(): invalidate the cached system prompt after swapping to fallback runtime so the next request rebuilds with the fallback model/provider.
  • _restore_primary_runtime(): invalidate the cached system prompt after restoring the primary runtime so the next turn shows the correct primary model again.
  • _invalidate_system_prompt(): harden with getattr(self, "_memory_store", None) to avoid crashing in test fixtures where memory store may not be initialized.

Verification

python -m pytest tests/run_agent/test_compressor_fallback_update.py tests/run_agent/test_primary_runtime_restore.py -xvs
============================== 27 passed

…sync clients

When  is explicitly set to ,
the custom-endpoint path in  creates a plain
client without provider-specific headers. This means sync vision calls (e.g.
) use the generic  User-Agent and get rejected by
Kimi's coding endpoint with a 403:

    'Kimi For Coding is currently only available for Coding Agents such as Kimi CLI...'

The async converter  already injects , and the
auto-detected API-key provider path also injects it, but the explicit custom
endpoint shortcut was missing it entirely.

This patch adds the same  injection to the custom endpoint
branch, and updates all existing Kimi header sites to  for
consistency.

Fixes <issue number to be filled in>
…ding pool

The credential pool seeder (_seed_from_env) hardcoded the base URL
for API-key providers without running provider-specific auto-detection.
For kimi-coding, this caused sk-kimi- prefixed keys to be seeded with
the legacy api.moonshot.ai/v1 endpoint instead of api.kimi.com/coding/v1,
resulting in HTTP 401 on the first request.

Import and call _resolve_kimi_base_url for kimi-coding so the pool
uses the correct endpoint based on the key prefix, matching the
runtime credential resolver behavior.

Also fix a comment: sk-kimi- keys are issued by kimi.com/code,
not platform.kimi.ai.

Fixes NousResearch#5561
…lback

When _try_activate_fallback() swaps to a new provider (e.g.
kimi-coding), resolve_provider_client() correctly injects
provider-specific default_headers (like KimiCLI User-Agent) into the
returned OpenAI client. However, _client_kwargs was saved with only
api_key and base_url, dropping those headers.

Every subsequent API call rebuilds the client from _client_kwargs via
_create_request_openai_client(), producing a bare OpenAI client without
the required headers. Kimi Coding rejects this with 403; Copilot would
lose its auth headers similarly.

This patch reads _custom_headers from the fallback client (where the
OpenAI SDK stores the default_headers kwarg) and includes them in
_client_kwargs so any client rebuild preserves provider-specific headers.

Fixes NousResearch#6075
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent loop, run_agent.py, prompt builder labels Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI session displays stale primary model name after fallback activation

2 participants