fix(cli): keep the resolved session id when a resumed oneshot session is empty

Review finding on #105957: `_load_resume_target` returned None for a
resolved session with no stored messages, so `hermes -z "hello" -c <title>
--create-if-missing` recorded the turn under a freshly minted session id and
the just-created titled session stayed empty. Preserve `resolved` unconditionally — the interactive /resume path keeps the selected id for an
empty session too; only the history replay is empty. Regression tests pin the
durable id for both a plain empty session and an empty compression-chain head.
This commit is contained in:
liuhao1024
2026-09-09 03:36:44 +08:00
committed by kshitij
parent 86d606ca34
commit 5ff6cb0edb
2 changed files with 23 additions and 6 deletions
+5 -2
View File
@@ -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 <title>
--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(
+18 -4
View File
@@ -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()