diff --git a/hermes_cli/oneshot.py b/hermes_cli/oneshot.py index c73a1ebdb1..eb129df9f2 100644 --- a/hermes_cli/oneshot.py +++ b/hermes_cli/oneshot.py @@ -350,7 +350,10 @@ def _load_resume_target(session_db, resume: Optional[str]) -> tuple[Optional[str ``resolve_resume_session_id``, safe-resume guard, model-projection history with ``session_meta`` rows dropped. An unknown session raises (the user passed an explicit id; silently starting a fresh session is the resume-dropped failure mode this exists to fix — - see #105892). An empty stored transcript keeps the turn as a fresh session. + see #105892). An empty stored transcript still returns the resolved id: the turn replays + nothing but is recorded under the requested session — ``hermes -z "hello" -c + --create-if-missing`` must fill the titled session it created, not mint a fresh id + (same contract as the interactive /resume of an empty session). """ if not resume: return None, [] @@ -362,7 +365,7 @@ def _load_resume_target(session_db, resume: Optional[str]) -> tuple[Optional[str session_db.assert_resume_safe(resolved, tip_only=True) restored, _display = session_db.get_resume_conversations(resolved) history = [m for m in restored if m.get("role") != "session_meta"] - return (resolved if history else None), history + return resolved, history def _run_agent( diff --git a/tests/hermes_cli/test_oneshot_resume.py b/tests/hermes_cli/test_oneshot_resume.py index fbdf8383d3..ea293ce353 100644 --- a/tests/hermes_cli/test_oneshot_resume.py +++ b/tests/hermes_cli/test_oneshot_resume.py @@ -73,12 +73,26 @@ class TestLoadResumeTarget: finally: db.close() - def test_empty_session_starts_fresh(self, tmp_path): - # Chat's contract: a resumed session with no messages starts fresh (same session - # id, no history rows to replay) — oneshot must not crash or resurrect the id. + def test_empty_session_keeps_resolved_id(self, tmp_path): + # Chat's contract: a resumed session with no messages starts fresh (no rows to + # replay) but the turn is recorded under the SELECTED id. Dropping the id here + # re-minted a session for `hermes -z "hello" -c <title> --create-if-missing`, + # leaving the freshly created titled session empty (review on #105957). db = _db_with_session(tmp_path, "s1") try: - assert _load_resume_target(db, "s1") == (None, []) + assert _load_resume_target(db, "s1") == ("s1", []) + finally: + db.close() + + def test_empty_compression_head_without_rows_keeps_head_id(self, tmp_path): + # A chain head with no descendant rows must not fall through to a fresh id either: + # the turn stays anchored to the resolved head (redirect only happens when the + # descendant actually holds messages). + db = SessionDB(db_path=tmp_path / "state.db") + db.create_session(session_id="head", source="cli") + db.create_session(session_id="child", source="cli", parent_session_id="head") + try: + assert _load_resume_target(db, "head") == ("head", []) finally: db.close()