Repository navigation
fix(sdk): forget a removed LLM's metrics id - #6581
Conversation
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Reviewed at head cf7acd68 against base e4160d11. This is a correct, minimal fix for the #6348 tracker sub-item it cites ("LLMRegistry.remove() keeps the metrics id in _metrics_ids, so re-adding the same LLM is treated as shared...").
Correctness. On main, remove() pops the usage slot but leaves id(llm.metrics) in _metrics_ids, so re-adding the same instance hits the shared-metrics branch of _ensure_independent_metrics and reset_metrics() wipes the cost/token usage already recorded. Discarding the removed instance's metrics id is exactly the fix the tracker pins; the new test reproduces it (before: accumulated_cost == 0.0 after re-add, after: stays 3.0).
No collateral regression. Every add() routes through _ensure_independent_metrics, which resets any LLM sharing metrics with a registered one, so registered LLMs never share a metrics object — discarding the removed id can therefore never orphan an id still needed by another registered LLM. The only in-repo remove() call site, LocalConversation.switch_llm, removes the old entry and then calls new_llm.restore_metrics(registered.metrics) after add(), so its behavior is unchanged by this diff. The one residual interaction — an application re-registering a model_copy() of a removed-but-still-alive instance would now keep sharing metrics with that instance — is not exercised by any supported flow in this repository, and the tracker item explicitly requires forgetting the id.
Test. test_llm_registry_readd_after_remove_keeps_metrics is a faithful port of the pinned test from f2fcf6e (identical body, xfail(strict=True) removed per the tracker's acceptance criteria).
Validation on this head.
- Current GitHub Actions on
cf7acd68: all checks pass, includingsdk-tests,pre-commit, andbuild-binary-and-test (windows-latest). - Workspace:
uv run pytest tests/sdk/llm/test_llm_registry.py→ 12 passed. Fulltests/sdk/llm/→ 1585 passed; the 2 failures are intests/sdk/llm/auth/test_credentials.pyand are environment-specific (this sandbox'sHOMEdiffers) and untouched by this diff. ruff checkandruff format --checkclean on both touched files.
No material findings.
✅ APPROVED
HUMAN:
Fixed the metrics reset on re-add from #6348. I reviewed the diff and the test; removing and re-adding an instance must not erase the usage it already recorded.
AGENT:
Why
LLMRegistry.remove()dropped the usage slot but left the instance's metrics id in_metrics_ids._ensure_independent_metricstreats a known id as evidence that amodel_copyshares metrics with another registered LLM, so adding the same instance again reset its metrics — silently discarding the cost and token usage it had already accumulated.Summary
_metrics_ids, so a lateradd()sees it as the instance's own metrics rather than a shared copy.test_llm_registry_readd_after_remove_keeps_metricsfrom theworktree-test-edge-casesbranch (commit f2fcf6e) without itsxfail(strict=True)marker, per the [Bug]: Edge-case tests expose ~70 bugs across the SDK, agent server, tools and workspaces #6348 menu's acceptance criteria.Issue Number
Part of #6348 (menu sub-item: "LLMRegistry.remove() keeps the metrics id in _metrics_ids, so re-adding the same LLM is treated as shared ...")
How to Test
Before this change
llm.metrics.accumulated_costis0.0after the re-add; after, it stays3.0.tests/sdk/llm/test_llm_registry.pyis 12 passed.tests/sdk/llm/has two pre-existing Windows path failures (test_default_provider_store_is_sibling_of_base_dir,test_default_store_expands_user_in_oh_persistence_dir) that fail the same way on unpatchedmain.ruff check/formaton the touched files are clean.Type
Notes
Only the id of the instance actually removed is discarded; ids of metrics still registered elsewhere are untouched.