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

View file

@ -3040,33 +3040,17 @@ class AIAgent:
except Exception:
pass
def commit_memory_session(self, messages: list = None) -> None:
"""Commit external memory providers for the current session.
Calls on_session_end() WITHOUT shutting down providers the session
data (e.g. OpenViking) is committed for extraction, but the HTTP
client and provider state remain alive for the next session.
Called before session_id changes (e.g. /new, context compression).
"""
if self._memory_manager:
try:
self._memory_manager.on_session_end(messages or [])
except Exception:
pass
def reinitialize_memory_session(self, new_session_id: str) -> None:
"""Transition memory providers to a new session after commit.
Calls restart_session() which uses reset_session() on providers that
support it (cheap: keeps HTTP client, resets per-session counters) or
falls back to initialize() for providers that don't.
Called after session_id has been assigned (e.g. /new, compression).
"""
if self._memory_manager:
try:
self._memory_manager.restart_session(new_session_id)
except Exception:
pass
def rotate_memory_session(self, new_session_id: str, messages: list = None) -> None:
"""Commit the current memory session, then rebind providers to
new_session_id. Keeps HTTP clients/state alive across the transition.
Called when session_id rotates (e.g. /new, context compression)."""
if not self._memory_manager:
return
try:
self._memory_manager.on_session_end(messages or [])
self._memory_manager.on_session_reset(new_session_id)
except Exception:
pass
def close(self) -> None:
"""Release all resources held by this agent instance.
@ -6854,14 +6838,11 @@ class AIAgent:
try:
# Propagate title to the new session with auto-numbering
old_title = self._session_db.get_session_title(self.session_id)
# Commit external memory (e.g. OpenViking) before session_id
# changes so extraction runs on the correct session.
self.commit_memory_session([])
self._session_db.end_session(self.session_id, "compression")
old_session_id = self.session_id
self.session_id = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:6]}"
# Transition external memory providers to the new session_id.
self.reinitialize_memory_session(self.session_id)
# Commit the old memory session and rebind providers to the new one.
self.rotate_memory_session(self.session_id, messages)
# Update session_log_file to point to the new session's JSON file
self.session_log_file = self.logs_dir / f"session_{self.session_id}.json"
self._session_db.create_session(