mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-25 00:51:20 +00:00
fix(cli): sync session_id after compression and preserve original end_reason (#12920)
After context compression (manual /compress or auto), run_agent's _compress_context ends the current session and creates a new continuation child session, mutating agent.session_id. The classic CLI held its own self.session_id that never resynced, so /status showed the ended parent, the exit-summary --resume hint pointed at a closed row, and any later end_session() call (from /resume <other> or /branch) targeted the wrong row AND overwrote the parent's 'compression' end_reason. This only affected the classic prompt_toolkit CLI. The gateway path was already fixed in PR #1160 (March 2026); --tui and ACP use different session plumbing and were unaffected. Changes: - cli.py::_manual_compress — sync self.session_id from self.agent.session_id after _compress_context, clear _pending_title - cli.py chat loop — same sync post-run_conversation for auto-compression - cli.py hermes -q single-query mode — same sync so stderr session_id output points at the continuation - hermes_state.py::end_session — guard UPDATE with 'ended_at IS NULL' so the first end_reason wins; reopen_session() remains the explicit escape hatch for re-ending a closed row Tests: - 3 new in tests/cli/test_manual_compress.py (split sync, no-op guard, pending_title behavior) - 2 new in tests/test_hermes_state.py (preserve compression end_reason on double-end; reopen-then-re-end still works) Closes #12483. Credits @steve5636 for the same-day bug report and @dieutx for PR #3529 which proposed the CLI sync approach.
This commit is contained in:
parent
f23123e7b4
commit
8a6aa5882e
4 changed files with 140 additions and 2 deletions
|
|
@ -46,6 +46,37 @@ class TestSessionLifecycle:
|
|||
assert isinstance(session["ended_at"], float)
|
||||
assert session["end_reason"] == "user_exit"
|
||||
|
||||
def test_end_session_preserves_original_end_reason(self, db):
|
||||
"""The first end_reason wins — compression splits must not be
|
||||
overwritten when a later stale ``end_session()`` call lands on the
|
||||
same row (e.g. from a CLI session_id that desynced after compression
|
||||
and then tried to /resume another session).
|
||||
"""
|
||||
db.create_session(session_id="s1", source="cli")
|
||||
db.end_session("s1", end_reason="compression")
|
||||
first_ended_at = db.get_session("s1")["ended_at"]
|
||||
|
||||
# Simulate a stale CLI holding the old session_id and calling
|
||||
# end_session() again with a different reason.
|
||||
time.sleep(0.01)
|
||||
db.end_session("s1", end_reason="resumed_other")
|
||||
|
||||
session = db.get_session("s1")
|
||||
assert session["end_reason"] == "compression"
|
||||
assert session["ended_at"] == first_ended_at
|
||||
|
||||
def test_end_session_after_reopen_allows_re_end(self, db):
|
||||
"""reopen_session() is the explicit escape hatch for re-ending a
|
||||
closed session. After reopen, end_session() takes effect again.
|
||||
"""
|
||||
db.create_session(session_id="s1", source="cli")
|
||||
db.end_session("s1", end_reason="compression")
|
||||
db.reopen_session("s1")
|
||||
db.end_session("s1", end_reason="user_exit")
|
||||
|
||||
session = db.get_session("s1")
|
||||
assert session["end_reason"] == "user_exit"
|
||||
|
||||
def test_update_system_prompt(self, db):
|
||||
db.create_session(session_id="s1", source="cli")
|
||||
db.update_system_prompt("s1", "You are a helpful assistant.")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue