Skip to content

fix(backend/copilot): normalise SDK analytics fallback model to CLI form - #13018

Closed
majdyz wants to merge 3 commits into
devfrom
claude/fix-autopilot-costs-LGvgH
Closed

fix(backend/copilot): normalise SDK analytics fallback model to CLI form#13018
majdyz wants to merge 3 commits into
devfrom
claude/fix-autopilot-costs-LGvgH

Conversation

@majdyz

@majdyz majdyz commented May 6, 2026

Copy link
Copy Markdown
Contributor

Why

Admin cost-dashboard filter model=claude-sonnet-4-6&block_name=copilot:SDK stopped matching new rows around April 28-30 — extending the time range still surfaces the historical entries, so the rows weren't deleted, the column value just changed.

Two recent changes combined to flip the recorded model name on standard-tier subscription turns:

  1. refactor(platform/copilot): consolidate 4 model-routing LD flags into 1 JSON flag #12917 (Apr 25) consolidated the four copilot-*-model LD flags into one JSON copilot-model-routing flag. If the LD JSON now serves a value equal to config.thinking_standard_model (the default "anthropic/claude-sonnet-4-6") — which is the typical state right after the migration — _resolve_sdk_model_for_request hits the subscription early-return and yields sdk_model=None (service.py:1085-1094).
  2. The finally-block fallback then resolved to the raw config string:
effective_model = sdk_model or config.thinking_standard_model
# → "anthropic/claude-sonnet-4-6" (with the OpenRouter prefix)

That raw value got written to PlatformCostLog.model, even though the SDK CLI subprocess on subscription mode talks to api.anthropic.com via OAuth and never sees the anthropic/ prefix. Historical admin filters keyed on the unprefixed CLI form (model=claude-sonnet-4-6) silently stopped matching.

The LD-override branch already routed resolved through _normalize_model_name (and #12932 made that subscription-aware), so only the None fallback was missing the same treatment.

What

Route the finally-block fallback through _normalize_model_name so the cost-log model column carries the CLI-form name regardless of which branch produced effective_model:

  • subscription / direct-Anthropicclaude-sonnet-4-6 (matches the historical filter and what's actually on the wire)
  • OpenRouteranthropic/claude-sonnet-4-6 (unchanged — that's the slug that goes to OpenRouter)

Vendor-rejection ValueError (e.g. moonshotai/... on a non-OpenRouter transport) soft-fails to the raw value so the row still records, mirroring the resolver's own fallback semantics.

How

Single-spot fix in service.py finally block (line 4434):

try:
    fallback_model = _normalize_model_name(config.thinking_standard_model)
except ValueError:
    fallback_model = config.thinking_standard_model
effective_model = sdk_model or fallback_model

Regression coverage in service_helpers_test.py: three transport scenarios assert the fallback string the cost log would record (subscription strips, direct-Anthropic strips, OpenRouter preserves).

Test plan

  • poetry run pytest backend/copilot/sdk/service_helpers_test.py::TestAnalyticsFallbackModelNormalisation -v — 3 passed
  • poetry run ruff format + ruff check clean on touched files

Checklist

  • I have read the project's contributing guide.
  • I have clearly described what this PR changes and why.
  • My code follows the style guidelines of this project.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes (CI will confirm).

Generated by Claude Code

ntindle and others added 3 commits May 1, 2026 06:55
Removed COLLABORATOR checks for comments and reviews in the workflow.
The standard-tier subscription path returns sdk_model=None from
_resolve_sdk_model_for_request whenever LD's served value matches the
config default — which is the common production hit after the LD-flag
consolidation (#12917) moved the four copilot-*-model flags into one
JSON copilot-model-routing flag. The finally-block fallback
effective_model = sdk_model or config.thinking_standard_model then
leaked the raw anthropic/claude-sonnet-4-6 config slug into
PlatformCostLog.model, breaking historical admin filters keyed on
the unprefixed CLI form (model=claude-sonnet-4-6).

Route the fallback through _normalize_model_name so the recorded value
matches the actual transport — unprefixed under subscription /
direct-Anthropic, prefix preserved under OpenRouter — matching what
sdk_model already produces and what the cost dashboard / admin
filters expect.

Soft-fails to the raw value on vendor-rejection ValueError so the row
still records, mirroring the resolver's own fallback semantics.
@majdyz
majdyz requested review from a team as code owners May 6, 2026 14:53
@majdyz
majdyz requested review from Swiftyos and kcze and removed request for a team May 6, 2026 14:53
@github-project-automation github-project-automation Bot moved this to 🆕 Needs initial review in AutoGPT development kanban May 6, 2026
@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

Failed to post review comments

Walkthrough

This PR tightens GitHub Actions access controls by removing COLLABORATOR associations from Claude code action workflows, and adds fallback model name normalization logic to the copilot SDK for consistent analytics and cost logging across different transport configurations.

Changes

Workflow Access Control Tightening

Layer / File(s) Summary
Permission Gating
.github/workflows/claude.yml, .github/workflows/docs-claude-review.yml
Removed COLLABORATOR from allowed author associations; now only OWNER or MEMBER can trigger Claude code actions and documentation reviews.

Analytics Fallback Model Normalization

Layer / File(s) Summary
Core Implementation
autogpt_platform/backend/backend/copilot/sdk/service.py
Added fallback model name normalization logic that attempts to normalize config.thinking_standard_model for analytics, with graceful fallback to the original value if normalization fails. Sets effective_model to either the SDK model or the normalized fallback model.
Test Coverage
autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py
New TestAnalyticsFallbackModelNormalisation class validates that _normalize_model_name produces CLI-friendly slugs for subscription and direct-Anthropic paths while preserving full slugs for OpenRouter, ensuring consistent analytics naming across transport configurations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

size/m, platform/backend

Suggested reviewers

  • Swiftyos
  • 0ubbe
  • kcze

Poem

A rabbit hops through GitHub's halls so bright,
Tightening the gates with measured might,
While SDK names find fallback cheer—
Analytics logging, crystal clear! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: normalizing SDK analytics fallback model names to CLI form, which directly addresses the cost dashboard filtering issue.
Description check ✅ Passed The description provides a comprehensive explanation of the issue (cost-dashboard filter breakage), root cause (model name format mismatch), solution approach, and test coverage—all directly relevant to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/fix-autopilot-costs-LGvgH

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added size/m platform/backend AutoGPT Platform - Back end labels May 6, 2026
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

🔍 PR Overlap Detection

This check compares your PR against all other open PRs targeting the same branch to detect potential merge conflicts early.

🟢 Low Risk — File Overlap Only

These PRs touch the same files but different sections (click to expand)

Summary: 0 conflict(s), 0 medium risk, 5 low risk (out of 5 PRs with file overlap)


Auto-generated on push. Ignores: openapi.json, lock files.

@codecov

codecov Bot commented May 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.86%. Comparing base (9e41726) to head (812d17f).
⚠️ Report is 39 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #13018      +/-   ##
==========================================
+ Coverage   69.56%   69.86%   +0.30%     
==========================================
  Files        2114     2134      +20     
  Lines      157370   160309    +2939     
  Branches    16217    16524     +307     
==========================================
+ Hits       109467   112002    +2535     
- Misses      44688    45011     +323     
- Partials     3215     3296      +81     
Flag Coverage Δ
platform-backend 78.96% <100.00%> (+0.36%) ⬆️
platform-frontend-e2e 31.53% <ø> (+1.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Platform Backend 78.96% <100.00%> (+0.36%) ⬆️
Platform Frontend 37.18% <ø> (-0.01%) ⬇️
AutoGPT Libs ∅ <ø> (∅)
Classic AutoGPT 28.43% <ø> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

platform/backend AutoGPT Platform - Back end size/m

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants