From 72617cecfc02791dbee4588be40cb6493e2cb1c6 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Sun, 13 Sep 2026 12:53:30 -0700 Subject: [PATCH] fix(goals): evict a registry-torn-down handle from _DB_CACHE hermes profile delete calls hermes_state_registry.close_all_under(profile_dir) before rmtree, which force-closes the shared handle goals.py cached for that home. A same-name recreate in the long-lived dashboard process then reused the closed object: save_goal swallowed the closed-db error and the replacement state.db was never created. Drop the cache entry once the registry has torn the handle down (it clears _shared_registry_owned at teardown) so the next call acquires a live generation for the recreated profile. --- hermes_cli/goals.py | 15 ++++++++++++++ tests/hermes_cli/test_goals.py | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/hermes_cli/goals.py b/hermes_cli/goals.py index 850bb9092a..d0b754f99a 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 9f511a8f46..17c320913d 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