Repository navigation
fix(llm_registry): track restored metrics id after subscriber notify - #6497
Open
Christian-Sidak wants to merge 1 commit into
Open
Christian-Sidak wants to merge 1 commit into
Christian-Sidak wants to merge 1 commit into
Conversation
After a conversation is resumed, ConversationStats.register_llm() calls restore_metrics() on the agent LLM inside the notify() callback, swapping its _metrics for a persisted object whose id was never added to _metrics_ids. A subsequent model_copy() derivative (ask-agent) therefore shares that restored object, and _ensure_independent_metrics() fails to detect the sharing -- the ask-agent copy is never reset, and get_combined_metrics() counts the spend twice. Fix: in LLMRegistry.add(), add one line after notify() that re-records id(llm.metrics). Any post-notify restore is now reflected in _metrics_ids, so the next model_copy() derivative is correctly detected as sharing metrics and receives its own independent instance. Fixes OpenHands#6487 Signed-off-by: Christian Sidak <christian@sentineltech.eu> Signed-off-by: Christian-Sidak <61099993+Christian-Sidak@users.noreply.github.com>
1 of 9 tasks
Draft
1 of 5 tasks
Author
|
Following up on this. The fix tracks the restored metrics id after subscriber notify fires, so the restored id is what gets associated with the metrics record rather than the stale pre-restore id. CI is green. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HUMAN:
Bug: resumed conversations double-count LLM cost once a derived model is created via model_copy.
AGENT:
Why
After a conversation is resumed,
ConversationStats.register_llm()callsrestore_metrics()insideLLMRegistry.notify(), which replaces the agent LLM's_metricsobject with a persisted one. The id of this new object is never recorded in_metrics_ids. When a derived model is later created viamodel_copy(),_ensure_independent_metrics()fails to detect the sharing and skips the reset, so both the parent and derived model share the same metrics object.get_combined_metrics()then counts the spend twice (3.0 instead of 2.0 for the agent, 6.0 instead of 3.0 combined).Summary
One line added in
LLMRegistry.add()afternotify():self._metrics_ids.add(id(llm.metrics))records any post-notify metrics swap in_metrics_ids. The nextmodel_copy()derivative is then correctly detected as sharing metrics and receives its own independent instance.How to Test
Added
test_resume_then_derive_no_double_countintests/sdk/conversation/test_conversation_stats.py. It seeds persisted metrics, registers the agent LLM through the realLLMRegistryandConversationStats, derives a copy viamodel_copy(), and asserts independent metrics and correct combined cost (reproducer from the issue).Fixes #6487