refactor(memory): promote on_session_reset to base provider hook

Replace hasattr-forked OpenViking-specific paths with a proper base-class
hook. Collapse the two agent wrappers into a single rotate_memory_session
so callers don't orchestrate commit + rebind themselves.

- MemoryProvider: add on_session_reset(new_session_id) as a default no-op
- MemoryManager: on_session_reset fans out unconditionally (no hasattr,
  no builtin skip — base no-op covers it)
- OpenViking: rename reset_session -> on_session_reset; drop the explicit
  POST /api/v1/sessions (OV auto-creates on first message) and the two
  debug raise_for_status wrappers
- AIAgent: collapse commit_memory_session + reinitialize_memory_session
  into rotate_memory_session(new_sid, messages)
- cli.py / run_agent.py: replace hasattr blocks and the split calls with
  a single unconditional rotate_memory_session call; compression path
  now passes the real messages list instead of []
- tests: align with on_session_reset, assert reset does NOT POST /sessions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
zhiheng.liu 2026-04-16 00:31:48 +08:00 committed by Teknium
parent 7856d304f2
commit 8275fa597a
6 changed files with 68 additions and 152 deletions

22
cli.py
View file

@ -4095,18 +4095,12 @@ class HermesCLI:
def new_session(self, silent=False):
"""Start a fresh session with a new session ID and cleared agent state."""
if self.agent and self.conversation_history:
old_history = self.conversation_history
if self.agent and old_history:
try:
self.agent.flush_memories(self.conversation_history)
self.agent.flush_memories(old_history)
except (Exception, KeyboardInterrupt):
pass
# Commit external memory providers (e.g. OpenViking) BEFORE
# session_id changes so extraction runs on the correct session.
if hasattr(self.agent, "commit_memory_session"):
try:
self.agent.commit_memory_session(self.conversation_history)
except Exception:
pass
self._notify_session_boundary("on_session_finalize")
elif self.agent:
# First session or empty history — still finalize the old session
@ -4155,13 +4149,9 @@ class HermesCLI:
)
except Exception:
pass
# Reinitialize external memory providers with the new session_id
# so subsequent turns are tracked under the new session.
if hasattr(self.agent, "reinitialize_memory_session"):
try:
self.agent.reinitialize_memory_session(self.session_id)
except Exception:
pass
# Commit the old session and rebind memory providers to the
# new session_id so subsequent turns are tracked correctly.
self.agent.rotate_memory_session(self.session_id, old_history)
self._notify_session_boundary("on_session_reset")
if not silent: