Skip to content

The agent that grows with you

fix(state): keep the pinned "keep" flag when importing sessions

Drexuxuxe00b87869c93
diff --git a/hermes_state_portability.py b/hermes_state_portability.py
index f622b15118c..be1939be002 100644
--- a/hermes_state_portability.py
+++ b/hermes_state_portability.py
@@ -42,7 +42,7 @@ _IMPORT_SESSION_INSERT_SQL = """INSERT INTO sessions (
cwd, git_branch, git_repo_root,
billing_provider, billing_base_url, billing_mode,
estimated_cost_usd, actual_cost_usd, cost_status, cost_source,
- pricing_version, title, api_call_count, archived
+ pricing_version, title, api_call_count, archived, pinned
)
VALUES (
:id, :source, :user_id, :model, :model_config,
@@ -53,7 +53,7 @@ _IMPORT_SESSION_INSERT_SQL = """INSERT INTO sessions (
:billing_provider, :billing_base_url, :billing_mode,
:estimated_cost_usd, :actual_cost_usd, :cost_status,
:cost_source, :pricing_version, :title,
- :api_call_count, :archived
+ :api_call_count, :archived, :pinned
)"""
# Columns copied verbatim from the payload; typed columns are converted below.
_IMPORT_PASSTHROUGH_COLS = (
@@ -64,6 +64,9 @@ _IMPORT_INT_COLS = (
"input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens", "reasoning_tokens", "api_call_count",
)
_IMPORT_FLOAT_COLS = ("ended_at", "estimated_cost_usd", "actual_cost_usd")
+# Durable user decisions, not live runtime state: a restore that drops ``pinned`` hands the "keep"
+# session to the next startup prune and the stale-archive sweep, the two things the pin opts out of.
+_IMPORT_FLAG_COLS = ("archived", "pinned")
def _rich_select(select_cols: str, where: str, tail: str = "", prompt_select: Optional[str] = "") -> str:
@@ -541,7 +544,7 @@ class SessionPortabilityMixin:
"id": session_id, "source": str(raw.get("source") or "import"),
"system_prompt_hash": self._store_system_prompt(conn, raw.get("system_prompt")),
"started_at": time.time() if started_at is None else started_at,
- "archived": 1 if raw.get("archived") else 0,
+ **{col: 1 if raw.get(col) else 0 for col in _IMPORT_FLAG_COLS},
**{col: raw.get(col) for col in _IMPORT_PASSTHROUGH_COLS},
**{col: self._coerce_or(raw.get(col), float, None) for col in _IMPORT_FLOAT_COLS},
**{col: self._coerce_or(raw.get(col), int, 0) for col in _IMPORT_INT_COLS},
diff --git a/tests/hermes_state/test_session_import_keep_flags.py b/tests/hermes_state/test_session_import_keep_flags.py
new file mode 100644
index 00000000000..3469abd22bc
--- /dev/null
+++ b/tests/hermes_state/test_session_import_keep_flags.py
@@ -0,0 +1,41 @@
+"""A restored or adopted session keeps the user's durable flags.
+
+``import_sessions`` (the dashboard import of a ``hermes sessions export`` backup, and stranded-session
+adoption) restored ``archived`` but not ``pinned``. Pinned is the "keep" flag the startup prune and the
+stale-archive sweep both exempt, so an old pinned session came back unpinned and the next startup
+deleted it.
+"""
+
+from __future__ import annotations
+
+import json
+import time
+
+from hermes_state import SessionDB
+
+SESSION_ID = "20260301_120000_abc123"
+
+
+def test_restored_pinned_session_survives_the_startup_prune(tmp_path):
+ source = SessionDB(db_path=tmp_path / "source.db")
+ target = SessionDB(db_path=tmp_path / "target.db")
+ try:
+ source.create_session(SESSION_ID, source="cli")
+ source.append_message(SESSION_ID, "user", "keep this one")
+ source.end_session(SESSION_ID, "user_exit")
+ old = time.time() - 200 * 86400
+ source._conn.execute("UPDATE sessions SET started_at = ?, ended_at = ? WHERE id = ?", (old, old, SESSION_ID))
+ source._conn.execute("UPDATE messages SET timestamp = ? WHERE session_id = ?", (old, SESSION_ID))
+ source._conn.commit()
+ source.set_session_pinned(SESSION_ID, True)
+ payload = json.loads(json.dumps(source.export_all(include_inactive=True)))
+
+ assert target.import_sessions(payload)["imported"] == 1
+ target.maybe_auto_prune_and_vacuum(retention_days=90, vacuum=False)
+
+ restored = target.get_session(SESSION_ID)
+ assert restored is not None, "the startup prune deleted a session the user pinned to keep"
+ assert restored["pinned"]
+ finally:
+ source.close()
+ target.close()