Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/octopal/infrastructure/store/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ def list_memory_entries_for_owner(self, owner_id: str, limit: int = 200) -> list

def list_memory_entries_by_chat(self, chat_id: int, limit: int = 50) -> list[MemoryEntry]:
cursor = self._conn.execute(
"SELECT * FROM memory_entries WHERE chat_id = ? ORDER BY id DESC LIMIT ?",
"SELECT * FROM memory_entries WHERE chat_id = ? ORDER BY created_at DESC, rowid DESC LIMIT ?",
(chat_id, limit),
)
return [self._row_to_memory(row) for row in cursor.fetchall()]
Expand Down
37 changes: 37 additions & 0 deletions tests/test_memory_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import uuid
from datetime import UTC, datetime, timedelta
from pathlib import Path

from octopal.infrastructure.store.models import MemoryEntry
Expand Down Expand Up @@ -58,6 +59,42 @@ def test_memory_owner_filter(tmp_path: Path) -> None:
assert rows[0].content == "owned by default"


def test_memory_chat_history_orders_by_created_at_not_uuid(tmp_path: Path) -> None:
store = SQLiteStore(_StoreSettings(tmp_path / "data", tmp_path / "workspace"))
base = datetime(2026, 6, 2, 12, 0, tzinfo=UTC)
entries = [
MemoryEntry(
id="ffffeeee-dddd-cccc-bbbb-aaaaaaaaaaaa",
role="user",
content="oldest",
embedding=None,
created_at=base,
metadata={"owner_id": "default", "chat_id": 7},
),
MemoryEntry(
id="11112222-3333-4444-5555-666677778888",
role="assistant",
content="newest",
embedding=None,
created_at=base + timedelta(minutes=2),
metadata={"owner_id": "default", "chat_id": 7},
),
MemoryEntry(
id="9999aaaa-bbbb-cccc-dddd-eeeeffffffff",
role="user",
content="middle",
embedding=None,
created_at=base + timedelta(minutes=1),
metadata={"owner_id": "default", "chat_id": 7},
),
]
for entry in entries:
store.add_memory_entry(entry)

rows = store.list_memory_entries_by_chat(7, limit=3)
assert [row.content for row in rows] == ["newest", "middle", "oldest"]


def test_canon_event_log_and_compaction(tmp_path: Path) -> None:
canon = CanonService(
workspace_dir=tmp_path / "workspace",
Expand Down