Skip to content

The agent that grows with you

fix(goals): evict a registry-torn-down handle from _DB_CACHE

teknium172617cecfc02
diff --git a/hermes_cli/goals.py b/hermes_cli/goals.py
index 850bb9092a5..d0b754f99ad 100644
--- a/hermes_cli/goals.py
+++ b/hermes_cli/goals.py
@@ -549,6 +549,14 @@ def _get_session_db() -> Optional[Any]:
return None
cached = _DB_CACHE.get(home)
+ if cached is not None and _registry_tore_down(cached):
+ # ``hermes profile delete`` force-closes every handle under the profile home
+ # (``hermes_state_registry.close_all_under``) before rmtree; a same-name recreate in this
+ # process must acquire a fresh handle, not keep writing into the torn-down one.
+ with _DB_BOOTSTRAP_LOCK:
+ if _DB_CACHE.get(home) is cached:
+ del _DB_CACHE[home]
+ cached = None
if cached is not None:
return cached
@@ -605,6 +613,13 @@ def _release_session_db(db) -> None:
pass
+def _registry_tore_down(db) -> bool:
+ """True once the registry force-closed *db* (``close_all`` / ``close_all_under`` clear the
+ shared-owned flag at teardown); every handle cached here was acquired through the registry, so a
+ cleared flag means the connection is gone and the cache entry is stale."""
+ return getattr(db, "_shared_registry_owned", True) is False
+
+
def _warn_dropped_write(manager: str, kind: str, session_id: str) -> None:
"""WARN on a dropped state write — the reply already told the user the state was set. One shared
message keeps goal, loop and heartbeat logs greppable as one bug class."""
diff --git a/tests/hermes_cli/test_goals.py b/tests/hermes_cli/test_goals.py
index 9f511a8f469..17c320913d1 100644
--- a/tests/hermes_cli/test_goals.py
+++ b/tests/hermes_cli/test_goals.py
@@ -272,6 +272,42 @@ class TestMigrateGoalToSession:
assert load_goal("c3").goal == "child already has one"
+class TestSessionDbCacheAfterProfileDelete:
+ """``hermes profile delete`` force-closes every registry handle under the profile home
+ (``close_all_under``) and rmtrees it; recreating the same name in the long-lived dashboard
+ process must not keep persisting goals into the torn-down handle."""
+
+ def test_delete_then_recreate_gets_a_live_store(self, hermes_home):
+ import shutil
+
+ import hermes_state
+ import hermes_state_registry as registry
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+ from hermes_cli.goals import GoalState, _get_session_db, load_goal, save_goal
+
+ # conftest re-points DEFAULT_DB_PATH at one fixed file; the registry must resolve the
+ # scoped profile home here, as production does.
+ with patch.object(hermes_state, "DEFAULT_DB_PATH", hermes_state._IMPORT_DEFAULT_DB_PATH):
+ profile = hermes_home / "profiles" / "p1"
+ profile.mkdir(parents=True)
+ token = set_hermes_home_override(profile)
+ try:
+ save_goal("s1", GoalState(goal="before delete"))
+ stale = _get_session_db()
+ assert registry.close_all_under(profile) == 1
+ shutil.rmtree(profile)
+ profile.mkdir(parents=True)
+
+ save_goal("s2", GoalState(goal="after recreate"))
+
+ assert _get_session_db() is not stale
+ assert (profile / "state.db").exists()
+ assert load_goal("s2").goal == "after recreate"
+ finally:
+ reset_hermes_home_override(token)
+ registry.close_all_under(profile)
+
+
class TestGoalManagerSubgoals:
def test_add_subgoal(self, hermes_home):
from hermes_cli.goals import GoalManager