From b1f62cde2e77227c2b28083a12fb3202ce9565b9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 14 Oct 2026 01:00:08 +0000 Subject: [PATCH] fix(sdk): expand ~ in OH_PERSISTENCE_DIR for the meta-profile store default_meta_profile_dir() built the path from the raw OH_PERSISTENCE_DIR value, so '~/x' wrote meta-profiles to '/~/x/meta-profiles' while settings.json landed in the expanded directory. Resolve it through get_user_persistence_dir() like every other store, which expands '~' and anchors relative values. Drop the F22.persistence-dir known-bug marker. Fixes #5878 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg --- .../feature-map/F22-meta-profiles.md | 26 ++++++++----------- .../openhands/sdk/llm/meta_profile_store.py | 9 +++---- tests/sdk/llm/test_meta_profile_store.py | 11 ++++++++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.agents/skills/verify-agent-server/references/feature-map/F22-meta-profiles.md b/.agents/skills/verify-agent-server/references/feature-map/F22-meta-profiles.md index 39560b1b2c8..e3082c6c369 100644 --- a/.agents/skills/verify-agent-server/references/feature-map/F22-meta-profiles.md +++ b/.agents/skills/verify-agent-server/references/feature-map/F22-meta-profiles.md @@ -652,7 +652,7 @@ Preconditions: missing name and invalid bodies or names reject with `HttpError` status 404 and 422. It re-activates `qa-direct`, so the list afterwards is as before. -- **Persistence directory with `~` (`F22.persistence-dir`), known bug.** +- **Persistence directory with `~` (`F22.persistence-dir`).** Launch a second server whose `OH_PERSISTENCE_DIR` is `~/qa-f22-tilde` (its `HOME` is the run's `home/`), save and activate a meta-profile there, and look where the files landed. The second server is stopped when the @@ -666,18 +666,14 @@ Preconditions: control-agent-server api POST /api/meta-profiles/qa-tilde/activate --run "$T" --expect 200 control-agent-server api GET /api/meta-profiles --run "$T" --check meta_profiles len-eq 1 --check active_meta_profile eq qa-tilde control-agent-server state ls 'home/qa-f22-tilde/settings.json' --run "$T" --expect-count 1 - control-agent-server state ls 'home/qa-f22-tilde/meta-profiles/qa-tilde.json' --run "$T" --expect-count 1 # bug: count eq 0 + control-agent-server state ls 'home/qa-f22-tilde/meta-profiles/qa-tilde.json' --run "$T" --expect-count 1 ``` - `settings.json` lands in `home/qa-f22-tilde/` (the settings store expands - `~` through `get_user_persistence_dir`). Expected the meta-profile next to - it; today it is written to `server/~/qa-f22-tilde/meta-profiles/qa-tilde.json`, - a directory literally named `~` under the server's working directory, - because `default_meta_profile_dir()` uses `Path(os.environ["OH_PERSISTENCE_DIR"])` - without `expanduser()` or the relative-path anchoring every other store - gets (`meta_profile_store.py`). The API still works because the router and - the routing tool share that helper; the files are just outside the - persistence directory. The `--save` file lands under the second run's - `evidence/` directory. + `settings.json` lands in `home/qa-f22-tilde/` and the meta-profile next to + it in `home/qa-f22-tilde/meta-profiles/qa-tilde.json`: both stores resolve + `OH_PERSISTENCE_DIR` through `get_user_persistence_dir`, which expands `~` + and anchors a relative value, so nothing is written to a directory + literally named `~` under the server's working directory. The `--save` + file lands under the second run's `evidence/` directory. ## Gotchas @@ -723,6 +719,6 @@ Preconditions: - `503` and `500` bodies use the server's generic shape: `detail` is `Internal Server Error` and the route's message is in `exception`. - The store directory comes from `OH_PERSISTENCE_DIR` read at call time - (`default_meta_profile_dir()`), falling back to `~/.openhands/meta-profiles` - when it is unset; give the server an absolute `OH_PERSISTENCE_DIR` - (`F22.persistence-dir`). + (`default_meta_profile_dir()`, through `get_user_persistence_dir`, so `~` + is expanded like in every other store), falling back to + `~/.openhands/meta-profiles` when it is unset (`F22.persistence-dir`). diff --git a/openhands-sdk/openhands/sdk/llm/meta_profile_store.py b/openhands-sdk/openhands/sdk/llm/meta_profile_store.py index e22fa58b9cd..a8ecd4b4dc9 100644 --- a/openhands-sdk/openhands/sdk/llm/meta_profile_store.py +++ b/openhands-sdk/openhands/sdk/llm/meta_profile_store.py @@ -10,7 +10,6 @@ from __future__ import annotations import json -import os import re import tempfile from collections.abc import Iterator @@ -23,6 +22,7 @@ from openhands.sdk.llm.llm_profile_store import PROFILE_NAME_REGEX from openhands.sdk.logger import get_logger +from openhands.sdk.utils.path import get_user_persistence_dir _LOCK_TIMEOUT_SECONDS: Final[float] = 30.0 @@ -37,11 +37,10 @@ def default_meta_profile_dir() -> Path: persisted state. Honor that for bare ``MetaProfileStore()`` instances so runtime tools read the same meta-profiles exposed by the HTTP API. Without that env var, keep the standalone SDK default under ``~/.openhands``. + Resolved at call time through ``get_user_persistence_dir`` so ``~`` and + relative values land where the other stores put their files. """ - env_dir = os.environ.get("OH_PERSISTENCE_DIR") - if env_dir: - return Path(env_dir) / "meta-profiles" - return Path.home() / ".openhands" / "meta-profiles" + return get_user_persistence_dir() / "meta-profiles" class MetaProfileLimitExceeded(Exception): diff --git a/tests/sdk/llm/test_meta_profile_store.py b/tests/sdk/llm/test_meta_profile_store.py index 4d6b3dd1f2c..89593c04bb2 100644 --- a/tests/sdk/llm/test_meta_profile_store.py +++ b/tests/sdk/llm/test_meta_profile_store.py @@ -35,6 +35,17 @@ def test_default_store_honors_oh_persistence_dir( assert store.base_dir == tmp_path / "meta-profiles" +def test_default_store_expands_user_in_oh_persistence_dir( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("OH_PERSISTENCE_DIR", "~/state") + + store = MetaProfileStore() + + assert store.base_dir == tmp_path / "state" / "meta-profiles" + + def test_load_valid_meta_profile(tmp_path: Path) -> None: _write(tmp_path, "balanced", VALID) store = MetaProfileStore(base_dir=tmp_path)