core/memory: order sessions by recency and touch on activity

List/get-latest by updated_at; bump updated_at when messages are
appended so workspace resume picks the lately-used session.
This commit is contained in:
2026-07-15 10:03:15 +08:00
parent a7d9c9879b
commit 19128735a2
2 changed files with 48 additions and 2 deletions
+28 -2
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
from datetime import UTC, datetime
from pathlib import Path
from typing import TYPE_CHECKING, Self
@@ -124,10 +125,10 @@ class MemoryStore:
uid: int | None = None,
workspace: str | Path | None = None,
) -> Sequence[Session]:
"""List sessions; optionally filter by user and/or bound workspace path."""
"""List sessions (most recently updated first); filter by user/workspace."""
ws = normalize_workspace(workspace)
async with self._session_factory() as session:
stmt = select(Session).order_by(Session.sid)
stmt = select(Session).order_by(Session.updated_at.desc(), Session.sid.desc())
if uid is not None:
stmt = stmt.where(Session.uid == uid)
if ws is not None:
@@ -135,6 +136,27 @@ class MemoryStore:
result = await session.execute(stmt)
return result.scalars().all()
async def get_latest_session(
self,
*,
uid: int | None = None,
workspace: str | Path | None = None,
) -> Session | None:
"""Return the most recently updated session for the filters, if any."""
sessions = await self.list_sessions(uid=uid, workspace=workspace)
return sessions[0] if sessions else None
async def touch_session(self, sid: int) -> Session | None:
"""Bump ``updated_at`` so the session ranks as lately used."""
async with self._session_factory() as session:
row = await session.get(Session, sid)
if row is None:
return None
row.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(row)
return row
async def update_session_workspace(
self,
sid: int,
@@ -148,6 +170,7 @@ class MemoryStore:
msg = f"session not found: {sid}"
raise ValueError(msg)
row.workspace = ws
row.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(row)
return row
@@ -167,6 +190,9 @@ class MemoryStore:
seq = 0 if last_seq is None else last_seq + 1
row = Message(sid=sid, seq=seq, data=data)
session.add(row)
chat = await session.get(Session, sid)
if chat is not None:
chat.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(row)
return row