π OpenHands: AI-Driven Development
refactor: Use direct file API in FileSettingsStore and FileSecretsStore (#14172)
Tim O'Farrelld5036c28138a
diff --git a/openhands/app_server/secrets/file_secrets_store.py b/openhands/app_server/secrets/file_secrets_store.pyindex d40d0cbc28..dc2699a178 100644--- a/openhands/app_server/secrets/file_secrets_store.py+++ b/openhands/app_server/secrets/file_secrets_store.py@@ -2,23 +2,22 @@ from __future__ import annotationsimport jsonfrom dataclasses import dataclass+from pathlib import Pathfrom openhands.app_server.secrets.secrets_models import Secretsfrom openhands.app_server.secrets.secrets_store import SecretsStore+from openhands.app_server.utils.file_store_mixin import FileStoreMixinfrom openhands.core.config.openhands_config import OpenHandsConfig-from openhands.storage import get_file_store-from openhands.storage.files import FileStore-from openhands.utils.async_utils import call_sync_from_async@dataclass-class FileSecretsStore(SecretsStore):- file_store: FileStore- path: str = 'secrets.json'+class FileSecretsStore(FileStoreMixin, SecretsStore):+ root_dir: Path+ filename: str = 'secrets.json'async def load(self) -> Secrets | None:try:- json_str = await call_sync_from_async(self.file_store.read, self.path)+ json_str = await self._read_file_async()kwargs = json.loads(json_str)provider_tokens = {k: v@@ -33,14 +32,11 @@ class FileSecretsStore(SecretsStore):async def store(self, secrets: Secrets) -> None:json_str = secrets.model_dump_json(context={'expose_secrets': True})- await call_sync_from_async(self.file_store.write, self.path, json_str)+ await self._write_file_async(json_str)@classmethodasync def get_instance(cls, config: OpenHandsConfig, user_id: str | None) -> FileSecretsStore:- file_store = get_file_store(- file_store_type=config.file_store,- file_store_path=config.file_store_path,- )- return FileSecretsStore(file_store)+ root_dir = cls._resolve_root_dir(config.file_store_path)+ return FileSecretsStore(root_dir=root_dir)diff --git a/openhands/app_server/settings/file_settings_store.py b/openhands/app_server/settings/file_settings_store.pyindex f086b772b9..ed4b28175e 100644--- a/openhands/app_server/settings/file_settings_store.py+++ b/openhands/app_server/settings/file_settings_store.py@@ -2,23 +2,22 @@ from __future__ import annotationsimport jsonfrom dataclasses import dataclass+from pathlib import Pathfrom openhands.app_server.settings.settings_models import Settingsfrom openhands.app_server.settings.settings_store import SettingsStore+from openhands.app_server.utils.file_store_mixin import FileStoreMixinfrom openhands.core.config.openhands_config import OpenHandsConfig-from openhands.storage import get_file_store-from openhands.storage.files import FileStore-from openhands.utils.async_utils import call_sync_from_async@dataclass-class FileSettingsStore(SettingsStore):- file_store: FileStore- path: str = 'settings.json'+class FileSettingsStore(FileStoreMixin, SettingsStore):+ root_dir: Path+ filename: str = 'settings.json'async def load(self) -> Settings | None:try:- json_str = await call_sync_from_async(self.file_store.read, self.path)+ json_str = await self._read_file_async()kwargs = json.loads(json_str)settings = Settings(**kwargs)@@ -34,14 +33,11 @@ class FileSettingsStore(SettingsStore):json_str = settings.model_dump_json(context={'expose_secrets': True, 'persist_settings': True})- await call_sync_from_async(self.file_store.write, self.path, json_str)+ await self._write_file_async(json_str)@classmethodasync def get_instance(cls, config: OpenHandsConfig, user_id: str | None) -> FileSettingsStore:- file_store = get_file_store(- file_store_type=config.file_store,- file_store_path=config.file_store_path,- )- return FileSettingsStore(file_store)+ root_dir = cls._resolve_root_dir(config.file_store_path)+ return FileSettingsStore(root_dir=root_dir)diff --git a/openhands/app_server/utils/file_store_mixin.py b/openhands/app_server/utils/file_store_mixin.pynew file mode 100644index 0000000000..416b25fef5--- /dev/null+++ b/openhands/app_server/utils/file_store_mixin.py@@ -0,0 +1,52 @@+"""I/O utility functions for the app server."""++from __future__ import annotations++import tempfile+from pathlib import Path++from openhands.utils.async_utils import call_sync_from_async+++class FileStoreMixin:+ """Mixin providing common file I/O operations for file-based stores.++ Classes using this mixin must define:+ - root_dir: Path - the base directory for file storage+ - filename: str - the name of the file to read/write+ """++ root_dir: Path+ filename: str++ @property+ def file_path(self) -> Path:+ return self.root_dir / self.filename++ def _read_file(self) -> str:+ with open(self.file_path, encoding='utf-8') as f:+ return f.read()++ def _write_file_atomic(self, contents: str) -> None:+ """Write contents to file atomically using write-to-temp-then-rename."""+ self.root_dir.mkdir(parents=True, exist_ok=True)+ with tempfile.NamedTemporaryFile(+ mode='w',+ encoding='utf-8',+ dir=self.root_dir,+ delete=False,+ ) as f:+ f.write(contents)+ temp_path = Path(f.name)+ temp_path.replace(self.file_path)++ async def _read_file_async(self) -> str:+ return await call_sync_from_async(self._read_file)++ async def _write_file_async(self, contents: str) -> None:+ await call_sync_from_async(self._write_file_atomic, contents)++ @staticmethod+ def _resolve_root_dir(path: str) -> Path:+ """Resolve a path string to a Path, expanding ~ if present."""+ return Path(path).expanduser()diff --git a/tests/unit/app_server/test_secrets_api.py b/tests/unit/app_server/test_secrets_api.pyindex fd8eb023cb..a29f40422b 100644--- a/tests/unit/app_server/test_secrets_api.py+++ b/tests/unit/app_server/test_secrets_api.py@@ -2,6 +2,7 @@# flake8: noqa: E501import os+from pathlib import Pathfrom unittest.mock import AsyncMock, patchimport pytest@@ -19,7 +20,6 @@ from openhands.integrations.provider import (ProviderToken,ProviderType,)-from openhands.storage import get_file_store@pytest.fixture@@ -36,14 +36,13 @@ def test_client():@pytest.fixture-def temp_dir(tmp_path_factory: pytest.TempPathFactory) -> str:- return str(tmp_path_factory.mktemp('secrets_store'))+def temp_dir(tmp_path_factory: pytest.TempPathFactory) -> Path:+ return tmp_path_factory.mktemp('secrets_store')@pytest.fixturedef file_secrets_store(temp_dir):- file_store = get_file_store('local', temp_dir)- store = FileSecretsStore(file_store)+ store = FileSecretsStore(root_dir=temp_dir)with patch('openhands.app_server.secrets.file_secrets_store.FileSecretsStore.get_instance',AsyncMock(return_value=store),diff --git a/tests/unit/app_server/test_settings_api.py b/tests/unit/app_server/test_settings_api.pyindex 7522422c63..c7c95bda6a 100644--- a/tests/unit/app_server/test_settings_api.py+++ b/tests/unit/app_server/test_settings_api.py@@ -21,7 +21,6 @@ from openhands.sdk.settings import ()from openhands.server.app import appfrom openhands.server.user_auth.user_auth import UserAuth-from openhands.storage.memory import InMemoryFileStore_EXPOSE = {'expose_secrets': True}@@ -89,8 +88,8 @@ class MockUserAuth(UserAuth):@pytest.fixture-def test_client():- # Create a test client+def test_client(tmp_path):+ # Create a test client with a temp directory for file settings storewith (patch.dict(os.environ,@@ -104,7 +103,7 @@ def test_client():),patch('openhands.app_server.settings.file_settings_store.FileSettingsStore.get_instance',- AsyncMock(return_value=FileSettingsStore(InMemoryFileStore())),+ AsyncMock(return_value=FileSettingsStore(root_dir=tmp_path)),),):client = TestClient(app)diff --git a/tests/unit/server/routes/test_skills_api.py b/tests/unit/server/routes/test_skills_api.pyindex a929d5b3be..b932e7045d 100644--- a/tests/unit/server/routes/test_skills_api.py+++ b/tests/unit/server/routes/test_skills_api.py@@ -15,7 +15,6 @@ from openhands.integrations.provider import ProviderToken, ProviderTypefrom openhands.integrations.service_types import UserGitInfofrom openhands.server.app import appfrom openhands.server.user_auth.user_auth import UserAuth-from openhands.storage.memory import InMemoryFileStoreclass MockUserAuth(UserAuth):@@ -66,7 +65,7 @@ class MockUserAuth(UserAuth):@pytest.fixture-def test_client():+def test_client(tmp_path):with (patch.dict(os.environ, {'SESSION_API_KEY': ''}, clear=False),patch('openhands.app_server.utils.dependencies._SESSION_API_KEY', None),@@ -76,7 +75,7 @@ def test_client():),patch('openhands.app_server.settings.file_settings_store.FileSettingsStore.get_instance',- AsyncMock(return_value=FileSettingsStore(InMemoryFileStore())),+ AsyncMock(return_value=FileSettingsStore(root_dir=tmp_path)),),):client = TestClient(app)diff --git a/tests/unit/server/test_openapi_schema_generation.py b/tests/unit/server/test_openapi_schema_generation.pyindex 8526c6164e..c2db2192d2 100644--- a/tests/unit/server/test_openapi_schema_generation.py+++ b/tests/unit/server/test_openapi_schema_generation.py@@ -14,7 +14,6 @@ from openhands.integrations.provider import ProviderToken, ProviderTypefrom openhands.integrations.service_types import UserGitInfofrom openhands.server.app import appfrom openhands.server.user_auth.user_auth import UserAuth-from openhands.storage.memory import InMemoryFileStoreclass MockUserAuth(UserAuth):@@ -63,7 +62,7 @@ class MockUserAuth(UserAuth):@pytest.fixture-def test_client():+def test_client(tmp_path):# Create a test clientwith (patch(@@ -72,7 +71,7 @@ def test_client():),patch('openhands.app_server.settings.file_settings_store.FileSettingsStore.get_instance',- AsyncMock(return_value=FileSettingsStore(InMemoryFileStore())),+ AsyncMock(return_value=FileSettingsStore(root_dir=tmp_path)),),):client = TestClient(app)diff --git a/tests/unit/storage/settings/test_file_settings_store.py b/tests/unit/storage/settings/test_file_settings_store.pyindex 552b38219a..335b4f2fb5 100644--- a/tests/unit/storage/settings/test_file_settings_store.py+++ b/tests/unit/storage/settings/test_file_settings_store.py@@ -9,12 +9,6 @@ from openhands.app_server.settings.settings_models import Settingsfrom openhands.core.config.openhands_config import OpenHandsConfigfrom openhands.sdk.llm import LLMfrom openhands.sdk.settings import AgentSettings, ConversationSettings-from openhands.storage.files import FileStore---def mock_file_store():- return MagicMock(spec=FileStore)@pytest.fixture(autouse=True)@@ -24,8 +18,13 @@ def allow_short_context_windows():@pytest.fixture-def file_settings_store(mock_file_store):- return FileSettingsStore(mock_file_store)+def temp_dir(tmp_path):+ return tmp_path+++def file_settings_store(temp_dir):+ return FileSettingsStore(root_dir=temp_dir)@pytest.mark.asyncio@@ -34,12 +33,11 @@ async def test_load_nonexistent_data(file_settings_store):'openhands.app_server.settings.settings_models.load_openhands_config',MagicMock(return_value=OpenHandsConfig()),):- file_settings_store.file_store.read.side_effect = FileNotFoundError()assert await file_settings_store.load() is None@pytest.mark.asyncio-async def test_store_and_load_data(file_settings_store):+async def test_store_and_load_data(file_settings_store, temp_dir):# Test datainit_data = Settings(language='python',@@ -61,16 +59,9 @@ async def test_store_and_load_data(file_settings_store):# Store dataawait file_settings_store.store(init_data)- # Verify store called with correct JSON- expected_json = init_data.model_dump_json(- context={'expose_secrets': True, 'persist_settings': True}- )- file_settings_store.file_store.write.assert_called_once_with(- 'settings.json', expected_json- )-- # Setup mock for load- file_settings_store.file_store.read.return_value = expected_json+ # Verify file was written+ expected_path = temp_dir / 'settings.json'+ assert expected_path.exists()# Load and verify dataloaded_data = await file_settings_store.load()@@ -102,20 +93,10 @@ async def test_store_and_load_data(file_settings_store):@pytest.mark.asyncio-async def test_get_instance():- config = OpenHandsConfig(file_store='local', file_store_path='/test/path')+async def test_get_instance(tmp_path):+ config = OpenHandsConfig(file_store='local', file_store_path=str(tmp_path))- with patch(- 'openhands.app_server.settings.file_settings_store.get_file_store'- ) as mock_get_store:- mock_store = MagicMock(spec=FileStore)- mock_get_store.return_value = mock_store-- store = await FileSettingsStore.get_instance(config, None)-- assert isinstance(store, FileSettingsStore)- assert store.file_store == mock_store- mock_get_store.assert_called_once_with(- file_store_type='local',- file_store_path='/test/path',- )+ store = await FileSettingsStore.get_instance(config, None)++ assert isinstance(store, FileSettingsStore)+ assert store.root_dir == tmp_path