Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def _register_agent_definitions(
agent_defs: list["AgentDefinition"],
*,
context: str,
cipher: Cipher | None,
) -> None:
"""Register agent definitions into the subagent registry.

Expand All @@ -516,7 +517,7 @@ def _register_agent_definitions(
registered = 0
for agent_def in agent_defs:
try:
factory = agent_definition_to_factory(agent_def)
factory = agent_definition_to_factory(agent_def, cipher=cipher)
register_agent_if_absent(
name=agent_def.name,
factory_func=factory,
Expand Down Expand Up @@ -930,6 +931,7 @@ def _prepare_persisted_runtime(self, stored: StoredConversation) -> None:
_register_agent_definitions(
stored.agent_definitions,
context=f"resuming conversation {stored.id}",
cipher=self.cipher,
)

async def _get_or_load_event_service(
Expand Down Expand Up @@ -1432,6 +1434,7 @@ async def _start_conversation(
_register_agent_definitions(
request.agent_definitions,
context=f"conversation {conversation_id}",
cipher=self.cipher,
)

# Plugin loading is now handled lazily by LocalConversation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ def _ensure_plugins_loaded(self) -> None:
register_plugin_agents(
agents=all_plugin_agents,
work_dir=self.workspace.working_dir,
cipher=self._cipher,
)

# Combine explicit hook_config with plugin hooks
Expand Down Expand Up @@ -1400,6 +1401,7 @@ def load_plugin(self, plugin_ref: str) -> None:
register_plugin_agents(
agents=plugin.agents,
work_dir=self.workspace.working_dir,
cipher=self._cipher,
)
if plugin.hooks and not plugin.hooks.is_empty():
self._merge_runtime_plugin_hooks(plugin.hooks)
Expand Down Expand Up @@ -1438,7 +1440,7 @@ def _register_file_based_agents(self) -> None:
then `~/.openhands/agents/*.md`)
"""
# register project-level and then user-level file-based agents
register_file_agents(self.workspace.working_dir)
register_file_agents(self.workspace.working_dir, cipher=self._cipher)

def _ensure_agent_ready(self) -> None:
"""Ensure the agent is fully initialized with plugins and agents loaded.
Expand Down
26 changes: 22 additions & 4 deletions openhands-sdk/openhands/sdk/subagent/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def create_security_expert(llm):
if TYPE_CHECKING:
from openhands.sdk.agent.agent import Agent
from openhands.sdk.llm.llm import LLM
from openhands.sdk.utils.cipher import Cipher

logger = get_logger(__name__)

Expand Down Expand Up @@ -160,6 +161,8 @@ def _get_profile_store(profile_store_dir: str | None) -> LLMProfileStore:
def agent_definition_to_factory(
agent_def: AgentDefinition,
work_dir: str | Path | None = None,
*,
cipher: "Cipher | None" = None,
) -> Callable[["LLM"], "Agent"]:
"""Create an agent factory closure from an `AgentDefinition`.

Expand All @@ -181,6 +184,7 @@ def agent_definition_to_factory(
agent_def: The agent definition to convert.
work_dir: Project directory for resolving skill names. If None,
only user-level skills are searched.
cipher: Cipher for decrypting secrets in the selected LLM profile.

Raises:
ValueError: If a tool or skill is not found.
Expand Down Expand Up @@ -225,7 +229,7 @@ def _factory(llm: "LLM") -> "Agent":
f"Available profiles: {available_profiles}"
)

llm = store.load(profile_name)
llm = store.load(profile_name, cipher=cipher)

# the system prompt of the subagent is added as a suffix of the
# main system prompt
Expand Down Expand Up @@ -285,7 +289,11 @@ def _factory(llm: "LLM") -> "Agent":
return _factory


def register_file_agents(work_dir: str | Path) -> list[str]:
def register_file_agents(
work_dir: str | Path,
*,
cipher: "Cipher | None" = None,
) -> list[str]:
"""Load and register file-based agents from project-level `.agents/agents` and
`.openhands/agents`, and user-level `~/.agents/agents` and `~/.openhands/agents`
directories.
Expand Down Expand Up @@ -317,7 +325,11 @@ def register_file_agents(work_dir: str | Path) -> list[str]:

registered: list[str] = []
for agent_def in deduplicated:
factory = agent_definition_to_factory(agent_def, work_dir=work_dir)
factory = agent_definition_to_factory(
agent_def,
work_dir=work_dir,
cipher=cipher,
)
was_registered = register_agent_if_absent(
name=agent_def.name,
factory_func=factory,
Expand All @@ -336,6 +348,8 @@ def register_file_agents(work_dir: str | Path) -> list[str]:
def register_plugin_agents(
agents: list[AgentDefinition],
work_dir: str | Path | None = None,
*,
cipher: "Cipher | None" = None,
) -> list[str]:
"""Register plugin-provided agent definitions into the delegate registry.

Expand All @@ -354,7 +368,11 @@ def register_plugin_agents(
"""
registered: list[str] = []
for agent_def in agents:
factory = agent_definition_to_factory(agent_def, work_dir=work_dir)
factory = agent_definition_to_factory(
agent_def,
work_dir=work_dir,
cipher=cipher,
)
was_registered = register_agent_if_absent(
name=agent_def.name,
factory_func=factory,
Expand Down
23 changes: 23 additions & 0 deletions tests/sdk/subagent/test_subagent_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
register_plugin_agents,
)
from openhands.sdk.subagent.schema import AgentDefinition
from openhands.sdk.utils.cipher import Cipher


def setup_function() -> None:
Expand Down Expand Up @@ -591,6 +592,28 @@ def test_agent_definition_to_factory_model_profile_custom_store(tmp_path: Path)
assert agent.llm.metrics is not parent_llm.metrics


def test_agent_definition_to_factory_decrypts_model_profile(tmp_path: Path) -> None:
"""Encrypted model profiles are decrypted for file-based sub-agents."""
cipher = Cipher("test-secret")
store = LLMProfileStore(base_dir=tmp_path)
profile_llm = LLM(
model="gpt-4o-mini",
api_key=SecretStr("profile-key"),
usage_id="profile-llm",
)
store.save("encrypted-profile", profile_llm, include_secrets=True, cipher=cipher)
agent_def = AgentDefinition(
name="encrypted-profile-agent",
model="encrypted-profile",
profile_store_dir=str(tmp_path),
)

agent = agent_definition_to_factory(agent_def, cipher=cipher)(_make_test_llm())

assert isinstance(agent.llm.api_key, SecretStr)
assert agent.llm.api_key.get_secret_value() == "profile-key"


def test_agent_definition_to_factory_profile_store_dir(tmp_path: Path) -> None:
"""profile_store_dir on AgentDefinition is used by the factory."""
store = LLMProfileStore(base_dir=tmp_path)
Expand Down
Loading