Skip to content

fix(sdk): forget a removed LLM's metrics id - #6581

Merged
VascoSch92 merged 1 commit into
OpenHands:mainfrom
yftx293:fix/registry-metrics-id-leak
Nov 9, 2026
Merged

VascoSch92 merged 1 commit into
OpenHands:mainfrom
yftx293:fix/registry-metrics-id-leak

Conversation

@yftx293

@yftx293 yftx293 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

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_metrics treats a known id as evidence that a model_copy shares 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

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

uv run pytest tests/sdk/llm/test_llm_registry.py -k readd_after_remove

Before this change llm.metrics.accumulated_cost is 0.0 after the re-add; after, it stays 3.0. tests/sdk/llm/test_llm_registry.py is 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 unpatched main. ruff check/format on the touched files are clean.

Type

  • Bug fix

Notes

Only the id of the instance actually removed is discarded; ids of metrics still registered elsewhere are untouched.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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, including sdk-tests, pre-commit, and build-binary-and-test (windows-latest).
  • Workspace: uv run pytest tests/sdk/llm/test_llm_registry.py → 12 passed. Full tests/sdk/llm/ → 1585 passed; the 2 failures are in tests/sdk/llm/auth/test_credentials.py and are environment-specific (this sandbox's HOME differs) and untouched by this diff.
  • ruff check and ruff format --check clean on both touched files.

No material findings.

✅ APPROVED

@VascoSch92
VascoSch92 merged commit 4db1ad7 into OpenHands:main Nov 9, 2026
36 checks passed
@enyst enyst mentioned this pull request Nov 9, 2026
1 of 5 tasks
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.

3 participants