diff --git a/openhands-agent-server/openhands/agent_server/conversation_service.py b/openhands-agent-server/openhands/agent_server/conversation_service.py index 70e357a44ee..619d1883e17 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_service.py +++ b/openhands-agent-server/openhands/agent_server/conversation_service.py @@ -387,6 +387,7 @@ def _register_agent_definitions( agent_defs: list["AgentDefinition"], *, context: str, + cipher: Cipher | None, ) -> None: """Register agent definitions into the subagent registry. @@ -401,7 +402,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, @@ -981,6 +982,7 @@ def _prepare_persisted_runtime(self, stored: StoredConversation) -> None: _register_agent_definitions( stored.agent_definitions, context=f"resuming conversation {stored.id}", + cipher=self.cipher, ) def _get_conversation_lock(self, conversation_id: UUID) -> asyncio.Lock: @@ -1600,6 +1602,7 @@ async def _create_conversation( _register_agent_definitions( request.agent_definitions, context=f"conversation {conversation_id}", + cipher=self.cipher, ) # Plugin loading is handled lazily by LocalConversation, which fetches diff --git a/openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py b/openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py index 80190008531..4f786c31fba 100644 --- a/openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py +++ b/openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py @@ -427,6 +427,8 @@ def __init__( if recovered_specs: register_client_tools(recovered_specs) self.agent = agent + self._cipher = cipher + self._bind_fallback_cipher(self.agent.llm) # Default callback: persist every event to state def _default_callback(e): @@ -520,8 +522,6 @@ def _default_callback(e): # This ensures plugins are loaded before agent initialization self.llm_registry = LLMRegistry() self._profile_store = LLMProfileStore(profile_store_dir) - self._cipher = cipher - # Seed agent_context.secrets into the registry for every agent (regular # and ACP), covering callers that skip create_request() — canvas / # TypeScript, or the server-side agent_settings -> create_agent fold. @@ -893,6 +893,7 @@ def fork( stuck_detection=self._stuck_detector is not None, visualizer=type(self._visualizer) if self._visualizer else None, delete_on_close=self.delete_on_close, + cipher=self._cipher, tags=tags, _parent_llm_call_context=self._llm_call_context, ) @@ -1265,6 +1266,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 @@ -1508,6 +1510,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) @@ -1546,7 +1549,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. @@ -1610,6 +1613,7 @@ def _ensure_agent_ready(self) -> None: if llm.usage_id not in registered: self.llm_registry.add(llm) registered.add(llm.usage_id) + self._bind_fallback_cipher(llm) self._agent_ready = True @@ -1632,6 +1636,11 @@ def get_llm_call_context(self) -> LLMCallContext: """ return self._llm_call_context + def _bind_fallback_cipher(self, llm: LLM) -> None: + """Bind the conversation cipher for lazy fallback profile loading.""" + if llm.fallback_strategy is not None: + llm.fallback_strategy._bind_cipher(self._cipher) + def _condenser_for_switched_llm( self, current_llm: LLM, @@ -1694,6 +1703,7 @@ def switch_llm(self, llm: LLM) -> None: ) self.agent = self.agent.model_copy(update=update) self._state.agent = self.agent + self._bind_fallback_cipher(new_llm) # Invalidate the cached ask-agent LLM so it re-clones. self.llm_registry.remove(ASK_AGENT_LLM_USAGE_ID) @@ -1734,6 +1744,7 @@ def get_or_create_profile_llm(self, profile_name: str, usage_id: str) -> LLM: llm = loaded.model_copy(update={"usage_id": usage_id}) llm = create_subscription_llm_from_config(llm) self.llm_registry.add(llm) + self._bind_fallback_cipher(llm) return llm def switch_acp_model(self, model: str) -> None: diff --git a/openhands-sdk/openhands/sdk/llm/fallback_strategy.py b/openhands-sdk/openhands/sdk/llm/fallback_strategy.py index faa2f43a4fc..130a6138a09 100644 --- a/openhands-sdk/openhands/sdk/llm/fallback_strategy.py +++ b/openhands-sdk/openhands/sdk/llm/fallback_strategy.py @@ -20,13 +20,17 @@ is_budget_exceeded_error, is_transient_http_error, ) -from openhands.sdk.llm.llm_profile_store import LLMProfileStore +from openhands.sdk.llm.llm_profile_store import ( + LLMProfileStore, + ProfileDecryptionError, +) from openhands.sdk.logger import get_logger if TYPE_CHECKING: from openhands.sdk.llm.llm_response import LLMResponse from openhands.sdk.llm.utils.metrics import Metrics + from openhands.sdk.utils.cipher import Cipher logger = get_logger(__name__) @@ -61,6 +65,11 @@ class FallbackStrategy(BaseModel): # Private: lazily resolved LLM instances _resolved: list[Any] | None = PrivateAttr(default=None) + _cipher: Cipher | None = PrivateAttr(default=None) + + def _bind_cipher(self, cipher: Cipher | None) -> None: + """Bind the conversation cipher used for lazy profile resolution.""" + self._cipher = cipher def should_fallback(self, error: Exception) -> bool: """Whether this error type is eligible for fallback.""" @@ -151,9 +160,14 @@ def _iter_fallbacks(self) -> Generator[Any]: remaining_names = self.fallback_llms[len(self._resolved) :] for name in remaining_names: try: - fb = self._profile_store.load(name) + fb = self._profile_store._load_for_execution( + name, + cipher=self._cipher, + ) self._resolved.append(fb) yield fb + except ProfileDecryptionError: + raise except (FileNotFoundError, ValueError) as exc: logger.error( "[Fallback Strategy] Failed to load " diff --git a/openhands-sdk/openhands/sdk/llm/llm_profile_store.py b/openhands-sdk/openhands/sdk/llm/llm_profile_store.py index 278ee357a21..869805128fc 100644 --- a/openhands-sdk/openhands/sdk/llm/llm_profile_store.py +++ b/openhands-sdk/openhands/sdk/llm/llm_profile_store.py @@ -16,6 +16,7 @@ canonicalize_openhands_llm_payload, ) from openhands.sdk.logger import get_logger +from openhands.sdk.utils.cipher import FERNET_TOKEN_PREFIX from openhands.sdk.utils.path import get_user_persistence_dir from openhands.sdk.utils.pydantic_secrets import REDACTED_SECRET_VALUE @@ -41,6 +42,10 @@ class ProfileLimitExceeded(Exception): """Raised when saving would exceed the configured profile limit.""" +class ProfileDecryptionError(ValueError): + """Raised when an encrypted profile secret cannot be decrypted.""" + + def _api_key_present(llm: LLM) -> bool: """True when ``llm`` carries a non-empty, non-redacted API key.""" from pydantic import SecretStr @@ -381,6 +386,39 @@ def _resolve_provider_connection( updates["api_key"] = SecretStr(api_key) return llm.model_copy(update=updates) + def _load_for_execution( + self, + name: str, + *, + cipher: Cipher | None = None, + ) -> LLM: + """Load a profile and reject any secret that remained encrypted.""" + from openhands.sdk.llm.llm import LLM_SECRET_FIELDS + + profile_path = self._get_profile_path(name) + llm = self.load(name, cipher=cipher) + profile_name = name.removesuffix(".json") + with self._acquire_lock(): + stored_profile = json.loads(profile_path.read_text()) + + loaded_profile = llm.model_dump(mode="json", context={"expose_secrets": True}) + for field in LLM_SECRET_FIELDS: + stored_value = stored_profile.get(field) + loaded_value = loaded_profile[field] + if ( + isinstance(loaded_value, str) + and loaded_value.startswith(FERNET_TOKEN_PREFIX) + ) or ( + isinstance(stored_value, str) + and stored_value.startswith(FERNET_TOKEN_PREFIX) + and loaded_value is None + ): + raise ProfileDecryptionError( + f"Could not decrypt secret '{field}' for profile " + f"'{profile_name}'. Use the cipher that encrypted the profile." + ) + return llm + def delete(self, name: str) -> None: """Delete an existing profile. diff --git a/openhands-sdk/openhands/sdk/subagent/registry.py b/openhands-sdk/openhands/sdk/subagent/registry.py index a8a7ce65dd6..2a6ebaf4655 100644 --- a/openhands-sdk/openhands/sdk/subagent/registry.py +++ b/openhands-sdk/openhands/sdk/subagent/registry.py @@ -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__) @@ -132,7 +133,9 @@ def register_agent_if_absent( no-ops when an agent with *name* is already registered, instead of raising `ValueError`. This is used by file-based and plugin-based agent loading to gracefully skip conflicts with programmatically - registered agents. + registered agents. Because the first registration wins, values captured + by ``factory_func`` (including a profile cipher) are pinned by the first + caller for that name. See `register_agent` for full parameter documentation. @@ -160,6 +163,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`. @@ -181,6 +186,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. @@ -225,7 +231,7 @@ def _factory(llm: "LLM") -> "Agent": f"Available profiles: {available_profiles}" ) - llm = store.load(profile_name) + llm = store._load_for_execution(profile_name, cipher=cipher) # the system prompt of the subagent is added as a suffix of the # main system prompt @@ -285,7 +291,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. @@ -295,6 +305,10 @@ def register_file_agents(work_dir: str | Path) -> list[str]: Does not overwrite agents already registered programmatically or by plugins. + Args: + work_dir: Project directory used to discover agent definitions. + cipher: Cipher for decrypting secrets in selected LLM profiles. + Returns: List of agent names that were actually registered. """ @@ -317,7 +331,7 @@ def register_file_agents(work_dir: str | Path) -> list[str]: registered: list[str] = [] for agent_def in deduplicated: - if _register_definition(agent_def, work_dir, kind="file-based"): + if _register_definition(agent_def, work_dir, kind="file-based", cipher=cipher): registered.append(agent_def.name) return registered @@ -327,6 +341,7 @@ def _register_definition( agent_def: AgentDefinition, work_dir: str | Path | None, kind: str, + cipher: "Cipher | None", ) -> bool: """Register one agent definition; skip it with a warning if it is invalid. @@ -336,7 +351,9 @@ def _register_definition( """ source = f" from {agent_def.source}" if agent_def.source else "" try: - factory = agent_definition_to_factory(agent_def, work_dir=work_dir) + factory = agent_definition_to_factory( + agent_def, work_dir=work_dir, cipher=cipher + ) except ValueError as e: logger.warning(f"Skipping {kind} agent '{agent_def.name}'{source}: {e}") return False @@ -353,6 +370,8 @@ def _register_definition( 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. @@ -365,13 +384,14 @@ def register_plugin_agents( agents: Agent definitions collected from loaded plugins. work_dir: Project directory for resolving skill names in agent definitions. If None, only user-level skills are searched. + cipher: Cipher for decrypting secrets in selected LLM profiles. Returns: List of agent names that were actually registered. """ registered: list[str] = [] for agent_def in agents: - if _register_definition(agent_def, work_dir, kind="plugin"): + if _register_definition(agent_def, work_dir, kind="plugin", cipher=cipher): registered.append(agent_def.name) return registered diff --git a/openhands-tools/openhands/tools/delegate/impl.py b/openhands-tools/openhands/tools/delegate/impl.py index 3c0b9f9f4da..84700811d32 100644 --- a/openhands-tools/openhands/tools/delegate/impl.py +++ b/openhands-tools/openhands/tools/delegate/impl.py @@ -223,6 +223,7 @@ def _spawn_agents(self, action: "DelegateAction") -> DelegateObservation: "visualizer": sub_visualizer, "hook_config": factory.definition.hooks, "persistence_dir": subagents_persistence_dir, + "cipher": parent_conversation._cipher, "_parent_llm_call_context": ( parent_conversation.get_llm_call_context() ), diff --git a/openhands-tools/openhands/tools/task/manager.py b/openhands-tools/openhands/tools/task/manager.py index 49cdfc3f2e3..56227bdd2e9 100644 --- a/openhands-tools/openhands/tools/task/manager.py +++ b/openhands-tools/openhands/tools/task/manager.py @@ -232,6 +232,7 @@ def _resume_task(self, resume: str, subagent_type: str) -> Task: mcp_tool_provider=self.parent_conversation._mcp_tool_provider, _mcp_config_from_parent=True, delete_on_close=True, + cipher=self.parent_conversation._cipher, observability_metadata=self._delegate_observability_metadata( task_id=resume, subagent_type=subagent_type, link=link ), @@ -337,6 +338,7 @@ def _get_conversation( mcp_tool_provider=parent._mcp_tool_provider, _mcp_config_from_parent=True, delete_on_close=True, + cipher=parent._cipher, prompt_cache_key=str(parent.state.id), observability_metadata=self._delegate_observability_metadata( task_id=task_id, subagent_type=subagent_type, link=link diff --git a/tests/sdk/conversation/local/test_fork.py b/tests/sdk/conversation/local/test_fork.py index b9b93141cda..529bb1201a3 100644 --- a/tests/sdk/conversation/local/test_fork.py +++ b/tests/sdk/conversation/local/test_fork.py @@ -16,6 +16,7 @@ from openhands.sdk.event.llm_convertible import MessageEvent, SystemPromptEvent from openhands.sdk.llm import LLM, Message, TextContent from openhands.sdk.tool import Action, Observation, ToolDefinition, ToolExecutor +from openhands.sdk.utils.cipher import Cipher def _agent() -> Agent: @@ -72,12 +73,19 @@ def create(cls, *args, **kwargs) -> Sequence[Self]: def test_fork_creates_new_id(): """Forked conversation must have a distinct ID.""" + cipher = Cipher("test-secret") with tempfile.TemporaryDirectory() as tmpdir: - src = Conversation(agent=_agent(), persistence_dir=tmpdir, workspace=tmpdir) + src = LocalConversation( + agent=_agent(), + persistence_dir=tmpdir, + workspace=tmpdir, + cipher=cipher, + ) fork = src.fork() assert fork.id != src.id assert isinstance(fork.id, uuid.UUID) + assert fork._cipher is cipher def test_fork_with_explicit_id(): diff --git a/tests/sdk/llm/test_llm_fallback.py b/tests/sdk/llm/test_llm_fallback.py index 289200efe2e..2d0a4cdbc0b 100644 --- a/tests/sdk/llm/test_llm_fallback.py +++ b/tests/sdk/llm/test_llm_fallback.py @@ -16,6 +16,8 @@ ) from pydantic import SecretStr +from openhands.sdk import Agent +from openhands.sdk.conversation.impl.local_conversation import LocalConversation from openhands.sdk.llm import ( LLM, FallbackStrategy, @@ -28,6 +30,11 @@ LLMRateLimitError, LLMServiceUnavailableError, ) +from openhands.sdk.llm.llm_profile_store import ( + LLMProfileStore, + ProfileDecryptionError, +) +from openhands.sdk.utils.cipher import Cipher def _get_mock_response(content: str = "ok", model: str = "gpt-4o") -> ModelResponse: @@ -481,6 +488,58 @@ def side_effect(**kwargs): assert content.text == "from store" +def test_fallback_profile_uses_bound_cipher(tmp_path): + cipher = Cipher("test-secret") + store = LLMProfileStore(base_dir=tmp_path) + store.save( + "encrypted-fallback", + _get_llm("fallback-model"), + include_secrets=True, + cipher=cipher, + ) + strategy = FallbackStrategy( + fallback_llms=["encrypted-fallback"], + profile_store_dir=tmp_path, + ) + + conversation = LocalConversation( + agent=Agent(llm=_get_llm("primary", fallback_strategy=strategy), tools=[]), + workspace=tmp_path, + cipher=cipher, + visualizer=None, + ) + bound_strategy = conversation.agent.llm.fallback_strategy + assert bound_strategy is not None + [fallback] = list(bound_strategy._iter_fallbacks()) + + assert isinstance(fallback.api_key, SecretStr) + assert fallback.api_key.get_secret_value() == "k" + + unbound_strategy = FallbackStrategy( + fallback_llms=["encrypted-fallback"], + profile_store_dir=tmp_path, + ) + with pytest.raises(ProfileDecryptionError, match="encrypted-fallback"): + list(unbound_strategy._iter_fallbacks()) + + +def test_fallback_skips_profile_with_missing_provider_connection(tmp_path): + store = LLMProfileStore(base_dir=tmp_path) + store.save( + "missing-connection", + LLM(model="gpt-4o-mini", provider_connection_id="deleted"), + ) + store.save("working", _get_llm("fallback-model"), include_secrets=True) + strategy = FallbackStrategy( + fallback_llms=["missing-connection", "working"], + profile_store_dir=tmp_path, + ) + + [fallback] = list(strategy._iter_fallbacks()) + + assert fallback.model == "fallback-model" + + # ========================================================================= # Async error-handling parity tests (acompletion / aresponses) # ========================================================================= diff --git a/tests/sdk/llm/test_llm_profile_store.py b/tests/sdk/llm/test_llm_profile_store.py index 34028d552c3..3efc9c41525 100644 --- a/tests/sdk/llm/test_llm_profile_store.py +++ b/tests/sdk/llm/test_llm_profile_store.py @@ -10,8 +10,10 @@ from openhands.sdk.llm import LLM, LLM_PROFILE_SCHEMA_VERSION from openhands.sdk.llm.llm_profile_store import ( LLMProfileStore, + ProfileDecryptionError, ProfileLimitExceeded, ) +from openhands.sdk.utils.cipher import Cipher @pytest.fixture @@ -310,6 +312,24 @@ def test_save_with_secrets( assert "secret-api-key-12345" in content +def test_execution_load_checks_actual_case_variant_path( + profile_store: LLMProfileStore, + sample_llm_with_secrets: LLM, + monkeypatch: pytest.MonkeyPatch, +) -> None: + profile_store.save( + "Prod", + sample_llm_with_secrets, + include_secrets=True, + cipher=Cipher("right-secret"), + ) + stored_path = profile_store.base_dir / "Prod.json" + monkeypatch.setattr(profile_store, "_get_profile_path", lambda _name: stored_path) + + with pytest.raises(ProfileDecryptionError, match="api_key.*prod"): + profile_store._load_for_execution("prod") + + @pytest.mark.parametrize("name", ["my_profile", "my_profile.json"]) def test_load_existing_profile( name: str, profile_store: LLMProfileStore, sample_llm: LLM diff --git a/tests/sdk/subagent/test_subagent_registry.py b/tests/sdk/subagent/test_subagent_registry.py index 0465b750a0e..bdfbb34d056 100644 --- a/tests/sdk/subagent/test_subagent_registry.py +++ b/tests/sdk/subagent/test_subagent_registry.py @@ -8,7 +8,10 @@ from openhands.sdk import LLM, Agent from openhands.sdk.context.condenser import LLMSummarizingCondenser, NoOpCondenser from openhands.sdk.hooks.config import HookConfig, HookDefinition, HookMatcher -from openhands.sdk.llm.llm_profile_store import LLMProfileStore +from openhands.sdk.llm.llm_profile_store import ( + LLMProfileStore, + ProfileDecryptionError, +) from openhands.sdk.mcp.config import MCPServer, dump_mcp_config from openhands.sdk.subagent.registry import ( _reset_registry_for_tests, @@ -21,6 +24,7 @@ register_plugin_agents, ) from openhands.sdk.subagent.schema import AgentDefinition +from openhands.sdk.utils.cipher import Cipher def setup_function() -> None: @@ -648,6 +652,69 @@ def test_agent_definition_to_factory_model_profile_custom_store(tmp_path: Path) assert agent.llm.metrics is not parent_llm.metrics +def test_register_file_agents_forwards_cipher_to_factory(tmp_path: Path) -> None: + cipher = Cipher("test-secret") + profile_dir = tmp_path / "profiles" + store = LLMProfileStore(base_dir=profile_dir) + store.save( + "encrypted-profile", + LLM(model="gpt-4o-mini", api_key=SecretStr("profile-key")), + include_secrets=True, + cipher=cipher, + ) + agents_dir = tmp_path / ".agents" / "agents" + agents_dir.mkdir(parents=True) + (agents_dir / "encrypted-agent.md").write_text( + "---\n" + "name: encrypted-agent\n" + "model: encrypted-profile\n" + f"profile_store_dir: {profile_dir}\n" + "---\n" + ) + + with patch( + "openhands.sdk.subagent.load.Path.home", return_value=tmp_path / "no_user" + ): + register_file_agents(tmp_path, cipher=cipher) + + agent = get_agent_factory("encrypted-agent").factory_func(_make_test_llm()) + + assert isinstance(agent.llm.api_key, SecretStr) + assert agent.llm.api_key.get_secret_value() == "profile-key" + + +@pytest.mark.parametrize("cipher", [None, Cipher("wrong-secret")]) +@pytest.mark.parametrize( + "secret_field", + ["api_key", "aws_access_key_id", "aws_secret_access_key", "aws_session_token"], +) +def test_agent_definition_to_factory_rejects_undecrypted_secret( + tmp_path: Path, + cipher: Cipher | None, + secret_field: str, +) -> None: + store = LLMProfileStore(base_dir=tmp_path) + profile = LLM.model_validate( + {"model": "gpt-4o-mini", secret_field: "profile-secret"} + ) + store.save( + "encrypted-profile", + profile, + include_secrets=True, + cipher=Cipher("right-secret"), + ) + agent_def = AgentDefinition( + name="encrypted-profile-agent", + model="encrypted-profile", + profile_store_dir=str(tmp_path), + ) + + with pytest.raises( + ProfileDecryptionError, match=rf"{secret_field}.*encrypted-profile" + ): + agent_definition_to_factory(agent_def, cipher=cipher)(_make_test_llm()) + + 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) diff --git a/tests/tools/delegate/test_delegation.py b/tests/tools/delegate/test_delegation.py index 1c55672a3d3..9fb3def3882 100644 --- a/tests/tools/delegate/test_delegation.py +++ b/tests/tools/delegate/test_delegation.py @@ -18,6 +18,7 @@ register_agent, ) from openhands.sdk.subagent.schema import AgentDefinition +from openhands.sdk.utils.cipher import Cipher from openhands.tools.delegate import ( DelegateExecutor, DelegateObservation, @@ -269,6 +270,8 @@ def test_spawn_disables_streaming_for_sub_agents(): parent_conversation.state.workspace.working_dir = "/tmp" parent_conversation.state.persistence_dir = None parent_conversation._visualizer = None + cipher = Cipher("test-secret") + parent_conversation._cipher = cipher executor = DelegateExecutor() @@ -284,6 +287,7 @@ def test_spawn_disables_streaming_for_sub_agents(): sub_conversation = executor._sub_agents["test_agent"] sub_llm = sub_conversation.agent.llm assert sub_llm.stream is False, "Sub-agent LLM should have streaming disabled" + assert sub_conversation._cipher is cipher # Verify parent LLM still has streaming enabled (wasn't mutated) assert parent_llm.stream is True, "Parent LLM should still have streaming enabled" diff --git a/tests/tools/task/test_task_manager.py b/tests/tools/task/test_task_manager.py index e459f42ebea..8bf68bce6eb 100644 --- a/tests/tools/task/test_task_manager.py +++ b/tests/tools/task/test_task_manager.py @@ -15,6 +15,7 @@ register_agent, ) from openhands.sdk.subagent.schema import AgentDefinition +from openhands.sdk.utils.cipher import Cipher from openhands.tools.preset import register_builtins_agents from openhands.tools.task.manager import ( Task, @@ -34,6 +35,7 @@ def _make_llm() -> LLM: def _make_parent_conversation( tmp_path: Path, persistence_dir: str | Path | None = None, + cipher: Cipher | None = None, ) -> LocalConversation: """Create a real (minimal) parent conversation for the manager.""" llm = _make_llm() @@ -44,6 +46,7 @@ def _make_parent_conversation( visualizer=None, delete_on_close=False, persistence_dir=persistence_dir, + cipher=cipher, ) @@ -191,6 +194,22 @@ def test_registers_uuid(self, tmp_path): assert task.id in manager._tasks assert isinstance(manager._tasks[task.id].conversation_id, uuid.UUID) + def test_create_and_resume_inherit_parent_cipher(self, tmp_path): + cipher = Cipher("test-secret") + parent = _make_parent_conversation(tmp_path, cipher=cipher) + manager = TaskManager() + manager._ensure_parent(parent) + register_builtins_agents() + + task = manager._create_task(subagent_type="general-purpose", description=None) + assert task.conversation is not None + assert task.conversation._cipher is cipher + + manager._evict_task(task) + resumed = manager._resume_task(resume=task.id, subagent_type="general-purpose") + assert resumed.conversation is not None + assert resumed.conversation._cipher is cipher + def test_create_task_uses_parent_max_iteration_when_factory_is_none(self, tmp_path): """Fallback to parent's max_iteration_per_run when factory has none.""" register_builtins_agents() diff --git a/tests/tools/test_builtin_agents.py b/tests/tools/test_builtin_agents.py index 80ab9c16ac8..40b2f56539f 100644 --- a/tests/tools/test_builtin_agents.py +++ b/tests/tools/test_builtin_agents.py @@ -77,6 +77,7 @@ def test_register_builtins_agents_registers_expected_factories( factory = get_agent_factory(name) agent = factory.factory_func(llm) assert isinstance(agent, Agent) + assert agent.llm is llm agent_tool_names[name] = [t.name for t in agent.tools] assert len(agent_tool_names) == len(expected_agents)