From 123084b2af64e5e55c97be655dc1ef78b0e0c3b8 Mon Sep 17 00:00:00 2001 From: Christian-Sidak <61099993+Christian-Sidak@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:37:40 -0800 Subject: [PATCH] fix(llm_registry): track restored metrics id after subscriber notify 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 #6487 Signed-off-by: Christian Sidak Signed-off-by: Christian-Sidak <61099993+Christian-Sidak@users.noreply.github.com> --- .../openhands/sdk/llm/llm_registry.py | 6 +++ .../conversation/test_conversation_stats.py | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/openhands-sdk/openhands/sdk/llm/llm_registry.py b/openhands-sdk/openhands/sdk/llm/llm_registry.py index 3858037cd23..0f9c471d2cc 100644 --- a/openhands-sdk/openhands/sdk/llm/llm_registry.py +++ b/openhands-sdk/openhands/sdk/llm/llm_registry.py @@ -133,6 +133,12 @@ def add(self, llm: LLM) -> None: self._usage_to_llm[usage_id] = llm self.notify(RegistryEvent(llm=llm)) + # A subscriber (e.g. ConversationStats.register_llm) may have called + # restore_metrics() on this LLM, replacing its metrics object with a + # restored one whose id was never recorded. Track the current id so + # that any subsequent model_copy() derivative will be detected as + # sharing metrics and given its own independent instance. + self._metrics_ids.add(id(llm.metrics)) logger.debug( f"[LLM registry {self.registry_id}]: Added LLM for usage {usage_id}" ) diff --git a/tests/sdk/conversation/test_conversation_stats.py b/tests/sdk/conversation/test_conversation_stats.py index 98d37808ef9..5ace0674a59 100644 --- a/tests/sdk/conversation/test_conversation_stats.py +++ b/tests/sdk/conversation/test_conversation_stats.py @@ -296,6 +296,50 @@ def test_multiple_llm_usages(connected_registry_and_stats): ) # max of 8000 and 4000 +def test_resume_then_derive_no_double_count(connected_registry_and_stats): + """Regression test for issue #6487: resume-then-derive double-counts cost. + + After a conversation is resumed, ConversationStats.register_llm() calls + restore_metrics() on the agent LLM, replacing its metrics with a restored + object whose id was never tracked by the registry. A subsequent + model_copy() derivative (ask-agent) therefore shares that restored object, + and get_combined_metrics() counts the spend twice. + + The fix: LLMRegistry.add() re-records the LLM's metrics id after notifying + subscribers, so any post-notify restore is reflected in _metrics_ids and a + later model_copy() derivative is detected as sharing metrics. + """ + registry, stats = connected_registry_and_stats + + # Seed persisted metrics as if the conversation was resumed from storage. + persisted = Metrics(model_name="gpt-4o") + persisted.add_cost(2.0) + stats.usage_to_metrics["agent"] = persisted + + # Register the agent LLM. The subscriber (register_llm) will call + # restore_metrics() and replace the LLM's metrics with `persisted`. + agent_llm = LLM(model="gpt-4o", api_key=SecretStr("test-key"), usage_id="agent") + registry.add(agent_llm) + + # Simulate what LocalConversation.ask_agent() does: derive a copy via + # model_copy() and register it under a different usage id. + question_llm = agent_llm.model_copy( + update={"usage_id": "ask-agent", "stream": False} + ) + registry.add(question_llm) + + # The derived LLM must have its own independent metrics object. + assert question_llm.metrics is not agent_llm.metrics + + # Charging cost to the derived LLM must not affect the agent LLM. + question_llm.metrics.add_cost(1.0) + assert agent_llm.metrics.accumulated_cost == 2.0 + assert question_llm.metrics.accumulated_cost == 1.0 + + # Combined cost must equal persisted + new spend, not double-counted. + assert stats.get_combined_metrics().accumulated_cost == 3.0 + + def test_register_llm_with_multiple_restored_usage_ids(conversation_stats): """ Test that reproduces the bug where del self.restored_metrics