Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions openhands-sdk/openhands/sdk/subagent/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ def agent_definition_to_factory(
def _factory(llm: "LLM") -> "Agent":
from openhands.sdk.agent.agent import Agent
from openhands.sdk.context.agent_context import AgentContext
from openhands.sdk.context.condenser import default_condenser
from openhands.sdk.context.condenser import (
LLMSummarizingCondenser,
default_condenser,
)
from openhands.sdk.tool.registry import list_registered_tools
from openhands.sdk.tool.spec import Tool

Expand Down Expand Up @@ -258,11 +261,25 @@ def _factory(llm: "LLM") -> "Agent":
# top-level agent) so deep runs auto-compact instead of erroring on context
# overflow. The condenser LLM needs a distinct usage_id or its tokens get
# deduped out of conversation stats. A NoOpCondenser disables condensation.
condenser = (
agent_def.condenser
if agent_def.condenser is not None
else default_condenser(llm.model_copy(update={"usage_id": "condenser"}))
)
if agent_def.condenser is not None:
condenser = agent_def.condenser
if isinstance(condenser, LLMSummarizingCondenser):
# Freshen per spawn: a distinct LLM with its own metrics (so
# sub-agents don't share/double-count condenser cost) and a
# usage_id distinct from the agent's, else its tokens get deduped.
cond_llm = condenser.llm
usage_id = (
cond_llm.usage_id
if cond_llm.usage_id != llm.usage_id
else "condenser"
)
fresh_llm = cond_llm.model_copy(update={"usage_id": usage_id})
fresh_llm.reset_metrics()
condenser = condenser.model_copy(update={"llm": fresh_llm})
else:
condenser = default_condenser(
llm.model_copy(update={"usage_id": "condenser"})
)

return Agent(
llm=llm,
Expand Down
39 changes: 39 additions & 0 deletions tests/sdk/subagent/test_subagent_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,42 @@ def test_factory_explicit_condenser_passthrough() -> None:
agent = factory(_make_test_llm())
assert isinstance(agent.condenser, LLMSummarizingCondenser)
assert agent.condenser.max_size == 40


def test_factory_explicit_condenser_fresh_per_spawn() -> None:
"""Explicit condensers are copied per spawn so sub-agents don't share one
Metrics object (cross-contamination/race), and a colliding usage_id is
normalized so condenser tokens aren't deduped out of stats."""
custom = LLMSummarizingCondenser(
llm=_make_test_llm(), # same usage_id as the agent -> must be normalized
max_size=40,
keep_first=2,
)
factory = agent_definition_to_factory(AgentDefinition(name="x", condenser=custom))
a1 = factory(_make_test_llm())
a2 = factory(_make_test_llm())

c1, c2 = a1.condenser, a2.condenser
assert isinstance(c1, LLMSummarizingCondenser)
assert isinstance(c2, LLMSummarizingCondenser)
# Fresh per spawn: distinct condenser, LLM, and Metrics objects.
assert c1 is not c2
assert c1.llm is not c2.llm
assert c1.llm.metrics is not c2.llm.metrics
# Colliding usage_id normalized; config preserved through the copy.
assert c1.llm.usage_id == "condenser"
assert c1.llm.usage_id != a1.llm.usage_id
assert c1.max_size == 40


def test_factory_explicit_condenser_preserves_distinct_usage_id() -> None:
"""A condenser usage_id that already differs from the agent's is kept."""
custom = LLMSummarizingCondenser(
llm=_make_test_llm().model_copy(update={"usage_id": "my-condenser"}),
max_size=40,
keep_first=2,
)
factory = agent_definition_to_factory(AgentDefinition(name="x", condenser=custom))
agent = factory(_make_test_llm())
assert isinstance(agent.condenser, LLMSummarizingCondenser)
assert agent.condenser.llm.usage_id == "my-condenser"
Loading