Skip to content

fix(models): validate active-model ref before persisting; avoid free-tier fallback - #601

Open
angri450 wants to merge 1 commit into
TencentCloud:developfrom
angri450:feature/model-config-hardening
Open

fix(models): validate active-model ref before persisting; avoid free-tier fallback#601
angri450 wants to merge 1 commit into
TencentCloud:developfrom
angri450:feature/model-config-hardening

Conversation

@angri450

@angri450 angri450 commented Sep 7, 2026

Copy link
Copy Markdown

背景(问题链)

用户把默认偏好模型(/admin/models 的 active-model)设成还没配置好的模型时:

  1. set_active_model 不校验 ref 可用性,把死引用(不存在的 provider / 未启用的模型)静默写入 settings
  2. 后续每轮对话模型解析失败 → 自动 fallback 到第一个可用模型(通常是 openrouter 的 free 档,id 含 free)
  3. free 档有日配额(如 50 次/天),用完 → 429 stream_error / model_retry 限流报错
  4. 表现为"手动设置的默认模型不起效果,反而报限流错"——排查困难(真实根因在配置校验缺失)

修改

1. octop/api/routers/providers.py set_active_model 加校验

  • provider 不存在 / 未启用 / 无 key / 无 base_url → 400 SLASH_BAD_ARGS,不写库
  • 模型不在该 provider 的 models 列表 → 400,不写库
  • 合法 ref → 正常写入 + on_provider_changed

2. octop/infra/agents/providers/store.py resolve_first_model_ref 避开免费档

  • id 含 free 的模型排到最后,fallback 优先选付费/本地模型
  • free 档有日配额,作为静默 fallback 会在配置异常时触发 429 限流误报

验证

  • PUT active-model 不存在的 provider → 400
  • PUT active-model 模型不在列表 → 400
  • PUT active-model 合法 ref → 200
  • fallback 排序:free 模型排在可用非免费模型之后

…tier fallback

- set_active_model now rejects unknown/disabled providers and models not in
  the provider's list with 400, instead of silently writing a dead ref.
  A dead active-model used to make every turn fall back to the first usable
  model (often a free-tier one with a daily quota), which surfaced as
  confusing 429 rate-limit errors.
- resolve_first_model_ref sorts models whose id contains 'free' last, so an
  unusable default no longer lands on a daily-quota free model.

Verified: PUT active-model with bad provider -> 400; bad model -> 400;
valid ref -> 200.
Copilot AI lite review requested due to automatic review settings September 7, 2026 16:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants