Skip to content

The agent that grows with you

fix(state): pin the session-db-unavailable fallbacks to the failing profile

kshitijk4poor532b6d1cb133
diff --git a/agent/turn_finalizer.py b/agent/turn_finalizer.py
index df6c8fd49f7..516cb8d141c 100644
--- a/agent/turn_finalizer.py
+++ b/agent/turn_finalizer.py
@@ -589,8 +589,8 @@ def finalize_turn(
if failed and str(_turn_exit_reason) == "session_persistence_failed":
from hermes_constants import profile_cli_selector
- # A separate local: final_response feeds the memory sync and the background-review
- # gate below, which must still see the turn as having produced no text.
+ # Never rebind final_response here: the memory sync and the background-review gate
+ # below must still see an empty response on a persistence-failed turn.
result["error"] = final_response or (
"session storage could not be written — check the state database "
f"health (`hermes {profile_cli_selector()}doctor`), then send your message again"
diff --git a/hermes_state.py b/hermes_state.py
index 6040c299482..d4b4ee1d00d 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -351,7 +351,7 @@ _SESSION_DB_CONSEQUENCE = "Sessions will not be saved until this is fixed."
_NETWORK_DRIVE_HINT = " If the database lives on a network drive, move it to a local disk."
_NETWORK_DRIVE_GLOSS = "the session database could not be opened; it may be on a network or unsupported drive"
_NETWORK_DRIVE_ACTION = (
- "Move it to a local disk (`hermes doctor` shows where it is), then start Hermes again."
+ "Move it to a local disk (`hermes {profile_arg}doctor` shows where it is), then start Hermes again."
)
@@ -368,15 +368,21 @@ def format_session_db_unavailable(
cannot host SQLite's write-ahead log: when the raw cause carries one of those markers the
message names the network-drive suspicion, because ``hermes doctor --fix`` cannot repair a
mount — only moving the file can."""
+ from hermes_constants import profile_cli_selector
+
+ profile_arg = profile_cli_selector()
cause = get_last_init_error()
if not cause:
- return f"{prefix}. {_SESSION_DB_CONSEQUENCE} Run `hermes doctor` to check the storage location."
+ return (
+ f"{prefix}. {_SESSION_DB_CONSEQUENCE} Run `hermes {profile_arg}doctor` to check the "
+ "storage location."
+ )
from hermes_state_user_copy import describe_storage_failure
failure = describe_storage_failure(cause)
gloss, action, hint = failure.gloss, failure.action, ""
if any(m in cause.lower() for m in _WAL_INCOMPAT_MARKERS):
if failure.cause == "unknown":
- gloss, action = _NETWORK_DRIVE_GLOSS, _NETWORK_DRIVE_ACTION
+ gloss, action = _NETWORK_DRIVE_GLOSS, _NETWORK_DRIVE_ACTION.replace("{profile_arg}", profile_arg)
else:
hint = _NETWORK_DRIVE_HINT
text = f"{prefix}: {gloss}. {_SESSION_DB_CONSEQUENCE} {action}{hint}"
diff --git a/hermes_state_user_copy.py b/hermes_state_user_copy.py
index 94a25b2d6f5..6aa89233142 100644
--- a/hermes_state_user_copy.py
+++ b/hermes_state_user_copy.py
@@ -43,7 +43,7 @@ _STORAGE_FAILURES: dict[str, tuple[str, str, str]] = {
"corrupt": (
"storage_corrupt",
"the session database file is damaged",
- f"{_DOCTOR} Recovery: `hermes {{profile_arg}}sessions recover --source <state.db> --inspect-only`.",
+ _DOCTOR + " Recovery: `hermes {profile_arg}sessions recover --source <state.db> --inspect-only`.",
),
"fts_index": (
"storage_index_corrupt",
@@ -88,9 +88,7 @@ def describe_storage_failure(exc_or_str) -> StorageFailure:
cause = classify_persistence_error(exc_or_str)
key = "disk_full" if cause == "disk" and is_disk_full_error(exc_or_str) else cause
code, gloss, action = _STORAGE_FAILURES.get(key, _STORAGE_FAILURES["unknown"])
- # Pin the copy-pasteable command to the profile whose store failed: a multi-profile backend
- # serves sessions whose state.db is not the process default, and a bare `hermes` follows the
- # sticky active_profile (#105887).
+ # Pin the copy-pasteable command to the failing profile — see profile_cli_selector.
from hermes_constants import profile_cli_selector
return StorageFailure(
diff --git a/tests/hermes_state/test_hermes_state_ux_messages.py b/tests/hermes_state/test_hermes_state_ux_messages.py
index 76ae16bd970..13c95032fee 100644
--- a/tests/hermes_state/test_hermes_state_ux_messages.py
+++ b/tests/hermes_state/test_hermes_state_ux_messages.py
@@ -61,3 +61,23 @@ def test_db_unavailable_without_cause_still_names_doctor():
assert "hermes doctor" in text
assert "will not be saved" in text.lower()
assert "Details:" not in text
+
+
+def test_db_unavailable_commands_are_pinned_to_the_failing_profile(monkeypatch, tmp_path):
+ """Both fallbacks that bypass the shared cause table (no cause; network-drive gloss) name the
+ profile whose store failed, like the table's actions do."""
+ from hermes_constants import profile_cli_selector
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes" / "profiles" / "research"))
+ selector = profile_cli_selector()
+ assert selector.strip()
+ hermes_state._set_last_init_error(None)
+ no_cause = format_session_db_unavailable()
+ hermes_state._set_last_init_error("OperationalError: locking protocol")
+ try:
+ network = format_session_db_unavailable()
+ finally:
+ hermes_state._set_last_init_error(None)
+ for text in (no_cause, network):
+ assert f"`hermes {selector}doctor`" in text and "`hermes doctor`" not in text
+ assert "{profile_arg}" not in text