Skip to content
Python

fix: list and count every memory scene, and migrate without a Chroma clash

alvinfei38a4da6849e6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2711b64..2ab98dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,17 @@ Hy Memory and MemHub are versioned and released independently.
## Unreleased
+- SDK: `list_memories(scene="chat" | "coding" | "working")` lists one kind of
+ memory. `coding` and `working` list that scene's whole store, newest update
+ first (`workspace_id` still narrows it), and `total` is now the real count
+ rather than the page size. The new `memory_stats()` returns the count and the
+ latest created/updated time for each scene and their total, without reading
+ any memory text. Previously coding and working memories could not be listed
+ or counted without a workspace, so a library of 45 memories listed 22.
+- The SDK migration no longer fails its first run on a home with legacy
+ vectors: reattaching them left a Chroma instance open with its own settings,
+ and the readback that followed could not open the same store
+ ("An instance of Chroma already exists ... with different settings").
- The SDK memory page now reads its list, count, source and time labels and
graph from one page response for one source filter, so rows, totals and the
map no longer disagree. Memories with no recorded source are shown and
diff --git a/hy_memory/client.py b/hy_memory/client.py
index 08aa595..2b09091 100644
--- a/hy_memory/client.py
+++ b/hy_memory/client.py
@@ -1980,6 +1980,7 @@ class HyMemoryClient:
layers: Optional[List[Any]] = None,
statuses: Optional[List[Any]] = None,
with_evolution: bool = False,
+ scene: Optional[str] = None,
) -> Dict[str, Any]:
"""
列出用户的记忆(同步)。
@@ -2008,6 +2009,9 @@ class HyMemoryClient:
保持兼容,只返回 ACTIVE。可显式读取 SUPERSEDED / SHADOW /
NEGATED / CONFLICTED / ARCHIVED;物理删除的节点不会返回。
with_evolution: 可选,为当前页条目附加 evolution_chain(默认关闭)。
+ scene: 可选,列哪一类记忆:"chat" | "coding" | "working"。
+ "coding"/"working" 列该类全部经验(workspace_id 可再收窄),
+ 按 updated_at 倒序,total 为真实总数。不传时保持原行为。
Returns:
chat 路径: {"vdb": {...}, "graph": {...} | 省略, "elapsed_ms": ...}
@@ -2023,9 +2027,36 @@ class HyMemoryClient:
limit=limit, offset=offset, order=order,
workspace_id=workspace_id, branch=branch,
layers=layers, statuses=statuses, with_evolution=with_evolution,
+ scene=scene,
)
)
+ def memory_stats(
+ self,
+ *,
+ user_id: str = "",
+ statuses: Optional[List[Any]] = None,
+ ) -> Dict[str, Any]:
+ """
+ 每类记忆的数量与最新时间(同步)。纯聚合,不返回正文。
+
+ Returns:
+ {
+ "total": chat + coding + working 的条数,
+ "scenes": {
+ "chat": {"count", "latest_created_at", "latest_updated_at"},
+ "coding": {"count", "latest_created_at", "latest_updated_at", "facts"},
+ "working": {同 coding},
+ },
+ "request_id", "elapsed_ms",
+ }
+ 时间均为秒级 epoch,没有记忆时为 None。chat 不含 L1_RAW;
+ coding/working 的 count 是经验条数,facts 是 active 事实数(不计入 total)。
+ """
+ return self._loop_thread.run(
+ self.async_memory_stats(user_id=user_id, statuses=statuses)
+ )
+
def get_user_basic_info(
self,
*,
@@ -4917,14 +4948,11 @@ class HyMemoryClient:
nodes.sort(key=_sort_key, reverse=reverse)
return nodes
- async def _list_vdb_bucket(
+ async def _list_vdb_nodes(
self,
*,
user_id: str,
agent_id: Optional[str],
- limit: int,
- offset: int,
- order: str,
user_ids: Optional[List[str]] = None,
agent_ids: Optional[List[str]] = None,
session_ids: Optional[List[str]] = None,
@@ -4932,8 +4960,8 @@ class HyMemoryClient:
exclude_sources: Optional[List[str]] = None,
layers: Optional[List] = None,
statuses: Optional[List] = None,
- with_evolution: bool = False,
- ) -> Dict[str, Any]:
+ ) -> List:
+ """chat 路径的全部节点(已过滤、未排序)。list 与 stats 共用同一套口径。"""
from .models.memory import MemoryLayer, MemoryStatus
from .pipelines._retrieval.user_level_l0 import collapse_user_level_l0_hits
@@ -4995,6 +5023,33 @@ class HyMemoryClient:
node for node in all_nodes if node.source not in blocked_sources
]
+ return all_nodes
+
+ async def _list_vdb_bucket(
+ self,
+ *,
+ user_id: str,
+ agent_id: Optional[str],
+ limit: int,
+ offset: int,
+ order: str,
+ user_ids: Optional[List[str]] = None,
+ agent_ids: Optional[List[str]] = None,
+ session_ids: Optional[List[str]] = None,
+ include_sources: Optional[List[str]] = None,
+ exclude_sources: Optional[List[str]] = None,
+ layers: Optional[List] = None,
+ statuses: Optional[List] = None,
+ with_evolution: bool = False,
+ ) -> Dict[str, Any]:
+ all_nodes = await self._list_vdb_nodes(
+ user_id=user_id, agent_id=agent_id,
+ user_ids=user_ids, agent_ids=agent_ids,
+ session_ids=session_ids,
+ include_sources=include_sources,
+ exclude_sources=exclude_sources,
+ layers=layers, statuses=statuses,
+ )
all_nodes = self._sort_memory_nodes(all_nodes, order=order)
total = len(all_nodes)
page_nodes = all_nodes[offset: offset + limit]
@@ -5154,11 +5209,15 @@ class HyMemoryClient:
request_id: str,
t0: float,
statuses: Optional[List] = None,
+ variant: Optional[str] = None,
) -> Dict[str, Any]:
"""按 workspace_id(+可选 branch)纯过滤列出经验层完整详情。
枚举而非召回,所以只有经验层、不带事实层。无 query、无语义排序,按
updated_at 倒序。fail-safe:失败返回空列表。
+
+ variant 给定时读该变体自己的库(没写过的变体不建空库,直接返回空),
+ workspace_id 为空表示该 user 的全部经验。未给定时沿用"最近一次用的那份"。
"""
empty = {
"request_id": request_id,
@@ -5169,20 +5228,31 @@ class HyMemoryClient:
"offset": offset,
"elapsed_ms": round((time.perf_counter() - t0) * 1000, 2),
}
+ if variant is not None:
+ empty["variant"] = variant
from .models.memory import MemoryStatus
if statuses and MemoryStatus.ACTIVE not in statuses:
return empty
try:
- await self._get_coding_writer() # 副作用:lazy init store
+ if variant is None:
+ await self._get_coding_writer() # 副作用:lazy init store
+ store = self._coding_store
+ else:
+ store = await self._coding_store_for_exact_get(variant)
except Exception as e:
logger.warning(f"[list-coding] init failed: {e}")
return empty
+ if store is None:
+ return empty
try:
- mems = await self._coding_store.list_by_workspace(
- user_id=user_id, workspace_id=workspace_id, branch=branch,
+ mems = await store.list_by_workspace(
+ user_id=user_id, workspace_id=workspace_id or "", branch=branch,
limit=limit, offset=offset,
)
+ total = (await store.stats(
+ user_id, workspace_id=workspace_id or None, branch=branch,
+ ))["count"]
except Exception as e:
logger.warning(f"[list-coding] list_by_workspace failed: {e}")
return empty
@@ -5201,6 +5271,7 @@ class HyMemoryClient:
"source_fact_ids": list(d.source_fact_ids),
"confidence": d.confidence,
"status": MemoryStatus.ACTIVE.value,
+ "created_at": d.created_at.isoformat() if d.created_at else None,
"updated_at": d.updated_at.isoformat() if d.updated_at else None,
"source": d.source,
}
@@ -5211,15 +5282,99 @@ class HyMemoryClient:
f"[list-coding] user={user_id} workspace_id={workspace_id!r} "
f"branch={branch!r} returned={len(out_memories)} elapsed={elapsed}ms"
)
- return {
+ out: Dict[str, Any] = {
"request_id": request_id,
"scene": "productivity",
"experiences": out_memories,
- "total": len(out_memories),
+ # 真实总数,不是本页条数:分页靠它判断是否还有下一页
+ "total": total,
"limit": limit,
"offset": offset,
"elapsed_ms": elapsed,
}
+ if variant is not None:
+ out["variant"] = variant
+ return out
+
+ @staticmethod
+ def _list_scene(scene: Optional[str]) -> Optional[str]:
+ """list/stats 的 scene:None(原有路由)| chat | coding | working。"""
+ if scene is None:
+ return None
+ value = str(scene).strip().lower()
+ if value not in ("chat", "coding", "working"):
+ raise ValueError(f"scene must be chat, coding or working, got {scene!r}")
+ return value
+
+ @_ensure_internal_loop
+ async def async_memory_stats(
+ self,
+ *,
+ user_id: str = "",
+ statuses: Optional[List[Any]] = None,
+ ) -> Dict[str, Any]:
+ """每类记忆的数量与最新时间(异步)。字段见同步版 memory_stats。"""
+ from .models.memory import MemoryStatus
+
+ request_id = str(uuid.uuid4())
+ set_request_id(request_id)
+ t0 = time.perf_counter()
+ effective_statuses = self._normalize_statuses(statuses)
+
+ def _epoch(value: Optional[datetime]) -> Optional[int]:
+ return int(value.timestamp()) if isinstance(value, datetime) else None
+
+ nodes = await self._list_vdb_nodes(
+ user_id=user_id, agent_id=None, statuses=effective_statuses,
+ )
+ created = [
+ stamp for stamp in (
+ _epoch(n.gmt_created) or _epoch(getattr(n, "valid_from", None))
+ for n in nodes
+ ) if stamp is not None
+ ]
+ updated = [
+ stamp for stamp in (_epoch(getattr(n, "gmt_modified", None)) for n in nodes)
+ if stamp is not None
+ ]
+ scenes: Dict[str, Dict[str, Any]] = {
+ "chat": {
+ "count": len(nodes),
+ "latest_created_at": max(created) if created else None,
+ "latest_updated_at": max(updated or created) if (updated or created) else None,
+ }
+ }
+ # 经验层没有生命周期字段,只算 ACTIVE;没写过的变体不建空库。
+ include_productivity = (
+ not effective_statuses or MemoryStatus.ACTIVE in effective_statuses
+ )
+ for variant in ("coding", "working"):
+ stats = {"count": 0, "latest_created_at": None, "latest_updated_at": None, "facts": 0}
+ if include_productivity:
+ store = await self._coding_store_for_exact_get(variant)
+ if store is not None:
+ raw = await store.stats(user_id)
+ stats = {
+ "count": raw["count"],
+ "latest_created_at": _epoch(raw["latest_created_at"]),
+ "latest_updated_at": _epoch(raw["latest_updated_at"]),
+ "facts": raw["facts"],
+ }
+ scenes[variant] = stats
+
+ elapsed = round((time.perf_counter() - t0) * 1000, 2)
+ total = sum(int(item["count"]) for item in scenes.values())
+ logger.info(
+ f"[stats] user={user_id} total={total} "
+ + " ".join(f"{name}={item['count']}" for name, item in scenes.items())
+ + f" elapsed={elapsed}ms"
+ )
+ return {
+ "total": total,
+ "scenes": scenes,
+ "request_id": request_id,
+ "elapsed_ms": elapsed,
+ }
@_ensure_internal_loop
async def async_list_memories(
@@ -5240,6 +5395,7 @@ class HyMemoryClient:
layers: Optional[List[Any]] = None,
statuses: Optional[List[Any]] = None,
with_evolution: bool = False,
+ scene: Optional[str] = None,
) -> Dict[str, Any]:
"""
列出用户的记忆(异步)。
@@ -5299,9 +5455,17 @@ class HyMemoryClient:
set_request_id(request_id)
t0 = time.perf_counter()
effective_statuses = self._normalize_statuses(statuses)
+ list_scene = self._list_scene(scene)
- # workspace_id 非空 → 走 coding memory 列表路径
- if workspace_id:
+ # scene 显式指定 coding/working → 列该类全部经验(workspace_id 可再收窄)
+ if list_scene in ("coding", "working"):
+ return await self._list_coding(
+ user_id=user_id, workspace_id=workspace_id, branch=branch,
+ limit=limit, offset=offset, request_id=request_id, t0=t0,
+ statuses=effective_statuses, variant=list_scene,
+ )
+ # workspace_id 非空且未指定 scene → 走 coding memory 列表路径(原有行为)
+ if workspace_id and list_scene is None:
return await self._list_coding(
user_id=user_id, workspace_id=workspace_id, branch=branch,
limit=limit, offset=offset, request_id=request_id, t0=t0,
diff --git a/hy_memory/coding/store.py b/hy_memory/coding/store.py
index 123a9f1..0f5225c 100644
--- a/hy_memory/coding/store.py
+++ b/hy_memory/coding/store.py
@@ -710,6 +710,42 @@ class CodingMemoryStore:
cur = self._conn.execute(sql, params)
return [_row_to_memory(r) for r in cur.fetchall()]
+ async def stats(
+ self,
+ user_id: str,
+ workspace_id: Optional[str] = None,
+ branch: Optional[str] = None,
+ ) -> Dict[str, Any]:
+ """经验数、最新创建/修改时间、active 事实数。纯聚合,不读正文。
+
+ 作用域与 list_by_workspace 相同:workspace_id 为空表示该 user 全部。
+ """
+ return await _run_in_pool(self._stats_sync, user_id, workspace_id, branch)
+
+ def _stats_sync(
+ self, user_id: str, workspace_id: Optional[str], branch: Optional[str],
+ ) -> Dict[str, Any]:
+ scope_sql, scope_params = _scope_where(workspace_id, branch)
+ with self._lock:
+ assert self._conn is not None
+ row = self._conn.execute(
+ f"SELECT COUNT(*) AS n, MAX(created_at) AS created, "
+ f"MAX(updated_at) AS updated FROM coding_memory_meta "
+ f"WHERE user_id = ? AND {scope_sql}",
+ [user_id] + scope_params,
+ ).fetchone()
+ facts = self._conn.execute(
+ f"SELECT COUNT(*) AS n FROM working_fact "
+ f"WHERE user_id = ? AND status = 'active' AND {scope_sql}",
+ [user_id] + scope_params,
+ ).fetchone()
+ return {
+ "count": int(row["n"] or 0),
+ "latest_created_at": _parse_dt(row["created"]) if row["created"] else None,
+ "latest_updated_at": _parse_dt(row["updated"]) if row["updated"] else None,
+ "facts": int(facts["n"] or 0),
+ }
+
# ------------------------------------------------------------
# 召回
# ------------------------------------------------------------
diff --git a/memhub/apps/core/src/one_memory/hy_sdk_migration.py b/memhub/apps/core/src/one_memory/hy_sdk_migration.py
index c90f87f..b955d14 100644
--- a/memhub/apps/core/src/one_memory/hy_sdk_migration.py
+++ b/memhub/apps/core/src/one_memory/hy_sdk_migration.py
@@ -326,13 +326,9 @@ def _repair_vector(
directory = data_dir / "hy-memory" / HY_MEMORY_NAMESPACE / "vector"
if not (directory / "chroma.sqlite3").is_file():
return 0
- import chromadb
- from chromadb.config import Settings
+ from one_memory.sdk_vector_reattach import open_offline_chroma
- client = chromadb.PersistentClient(
- path=str(directory),
- settings=Settings(anonymized_telemetry=False),
- )
+ client = open_offline_chroma(directory)
# The SDK client is not open yet (startup has not created its handle), so
# this is the only writer; release the store before the SDK takes it.
try:
diff --git a/memhub/apps/core/src/one_memory/sdk_vector_reattach.py b/memhub/apps/core/src/one_memory/sdk_vector_reattach.py
index 4136cb4..2c69243 100644
--- a/memhub/apps/core/src/one_memory/sdk_vector_reattach.py
+++ b/memhub/apps/core/src/one_memory/sdk_vector_reattach.py
@@ -23,6 +23,21 @@ from one_memory.sdk_data_plane_migration import DataPlaneMigrationError
_BATCH = 500
+def open_offline_chroma(directory: Path) -> Any:
+ """A Chroma client on the SDK's vector store, configured as the SDK opens it.
+
+ Chroma refuses a second client on a directory whose shared instance was
+ created with different settings, so offline code must not pass its own.
+ Telemetry is turned off the way the Runtime turns it off, by environment.
+ """
+ import os
+
+ import chromadb
+
+ os.environ["ANONYMIZED_TELEMETRY"] = "False"
+ return chromadb.PersistentClient(path=str(directory))
+
+
@dataclass(frozen=True)
class ReattachedCollection:
legacy: str
@@ -97,45 +112,46 @@ def reattach_legacy_vectors(
pairs = legacy_vector_collections(data_dir, profile)
if not pairs:
return ()
- import chromadb
- from chromadb.config import Settings
-
- client = chromadb.PersistentClient(
- path=str(_vector_directory(data_dir)),
- settings=Settings(anonymized_telemetry=False),
- )
- existing = set(_stored_names(data_dir))
- done: list[ReattachedCollection] = []
- for legacy_name, current_name in pairs:
- legacy = client.get_collection(legacy_name)
- legacy_count = legacy.count()
- merged = 0
- if current_name in existing:
- current = client.get_collection(current_name)
- legacy_ids = set(_ids(legacy))
- current_ids: set[str] = set()
- for batch in _records(current):
- legacy.upsert(**batch)
- current_ids.update(batch["ids"])
- merged = len(current_ids)
- if legacy.count() != len(legacy_ids | current_ids):
- raise DataPlaneMigrationError("sdk_migration_vector_merge_incomplete")
- client.delete_collection(current_name)
- legacy.modify(name=current_name)
- final = client.get_collection(current_name).count()
- if final < legacy_count:
- raise DataPlaneMigrationError("sdk_migration_vector_reattach_incomplete")
- existing.discard(legacy_name)
- existing.add(current_name)
- done.append(
- ReattachedCollection(
- legacy=legacy_name,
- current=current_name,
- legacy_count=legacy_count,
- merged_count=merged,
- final_count=final,
+ client = open_offline_chroma(_vector_directory(data_dir))
+ # Closed, not left to the process: Chroma keeps one shared instance per
+ # directory, and the readback that follows opens the same directory
+ # through the SDK. A leaked instance failed that open on every 1.0.4 home
+ # with legacy vectors, so the first launch after upgrade always failed.
+ try:
+ existing = set(_stored_names(data_dir))
+ done: list[ReattachedCollection] = []
+ for legacy_name, current_name in pairs:
+ legacy = client.get_collection(legacy_name)
+ legacy_count = legacy.count()
+ merged = 0
+ if current_name in existing:
+ current = client.get_collection(current_name)
+ legacy_ids = set(_ids(legacy))
+ current_ids: set[str] = set()
+ for batch in _records(current):
+ legacy.upsert(**batch)
+ current_ids.update(batch["ids"])
+ merged = len(current_ids)
+ if legacy.count() != len(legacy_ids | current_ids):
+ raise DataPlaneMigrationError("sdk_migration_vector_merge_incomplete")
+ client.delete_collection(current_name)
+ legacy.modify(name=current_name)
+ final = client.get_collection(current_name).count()
+ if final < legacy_count:
+ raise DataPlaneMigrationError("sdk_migration_vector_reattach_incomplete")
+ existing.discard(legacy_name)
+ existing.add(current_name)
+ done.append(
+ ReattachedCollection(
+ legacy=legacy_name,
+ current=current_name,
+ legacy_count=legacy_count,
+ merged_count=merged,
+ final_count=final,
+ )
)
- )
+ finally:
+ client.close()
return tuple(done)
diff --git a/memhub/apps/core/tests/test_sdk_data_plane_migration.py b/memhub/apps/core/tests/test_sdk_data_plane_migration.py
index eb9cc95..797d1b5 100644
--- a/memhub/apps/core/tests/test_sdk_data_plane_migration.py
+++ b/memhub/apps/core/tests/test_sdk_data_plane_migration.py
@@ -119,14 +119,10 @@ def test_post_cutover_metadata_repair_restores_source_and_original_times(store):
),
)
- import chromadb
- from chromadb.config import Settings as ChromaSettings
+ from one_memory.sdk_vector_reattach import open_offline_chroma
vector = data_dir / "hy-memory" / "governed-v2" / "vector"
- collection = chromadb.PersistentClient(
- path=str(vector),
- settings=ChromaSettings(anonymized_telemetry=False),
- ).get_or_create_collection("memory")
+ collection = open_offline_chroma(vector).get_or_create_collection("memory")
collection.add(
ids=["sdk-1"],
documents=["remember"],
@@ -230,13 +226,10 @@ def test_metadata_repair_reaches_memories_the_cutover_backfilled_under_legacy_id
"INSERT INTO evidence VALUES ('e7','workbuddy','2026-09-01T00:00:00+00:00',NULL)"
)
- import chromadb
- from chromadb.config import Settings as ChromaSettings
+ from one_memory.sdk_vector_reattach import open_offline_chroma
vector = data_dir / "hy-memory" / "governed-v2" / "vector"
- client = chromadb.PersistentClient(
- path=str(vector), settings=ChromaSettings(anonymized_telemetry=False)
- )
+ client = open_offline_chroma(vector)
collection = client.get_or_create_collection("memory")
imported = int(datetime(2026, 10, 20, tzinfo=UTC).timestamp())
collection.add(
@@ -256,9 +249,7 @@ def test_metadata_repair_reaches_memories_the_cutover_backfilled_under_legacy_id
now=datetime(2026, 10, 25, tzinfo=UTC),
)
- client = chromadb.PersistentClient(
- path=str(vector), settings=ChromaSettings(anonymized_telemetry=False)
- )
+ client = open_offline_chroma(vector)
rows = client.get_collection("memory").get(ids=["legacy-7", "unlinked"], include=["metadatas"])
client.close()
metadata = dict(zip(rows["ids"], rows["metadatas"], strict=True))
@@ -546,13 +537,9 @@ def test_finished_jobs_without_input_do_not_stop_the_migration(store):
def _chroma(home: Path):
- import chromadb
- from chromadb.config import Settings as ChromaSettings
+ from one_memory.sdk_vector_reattach import open_offline_chroma
- return chromadb.PersistentClient(
- path=str(home / "hy-memory" / "governed-v2" / "vector"),
- settings=ChromaSettings(anonymized_telemetry=False),
- )
+ return open_offline_chroma(home / "hy-memory" / "governed-v2" / "vector")
def _fill(client, name: str, ids: list[str], *, text: str = "old") -> None:
@@ -599,6 +586,38 @@ def test_legacy_vectors_are_renamed_and_newer_writes_are_kept(tmp_path) -> None:
assert reattach_legacy_vectors(tmp_path, profile) == ()
+def test_the_readback_can_open_the_store_right_after_reattach(tmp_path, monkeypatch) -> None:
+ """The 1.0.4 upgrade failed its first launch at `verify.readback`.
+
+ Reattach opened Chroma with `anonymized_telemetry=False` and never closed
+ it. The SDK then opened the same directory in the same process with
+ default settings, and Chroma refused: "An instance of Chroma already
+ exists ... with different settings". The second launch only passed because
+ nothing was left to reattach.
+ """
+ import chromadb
+
+ from one_memory.hy_sdk_runtime import builtin_retrieval_snapshot, vector_collection_bases
+ from one_memory.sdk_vector_reattach import reattach_legacy_vectors
+
+ profile = builtin_retrieval_snapshot()
+ current, legacy = vector_collection_bases(profile)
+ seeded = _chroma(tmp_path)
+ _fill(seeded, f"{legacy[0]}_4", ["a", "b"])
+ seeded.close()
+ # The migration process has not configured the SDK environment yet.
+ monkeypatch.delenv("ANONYMIZED_TELEMETRY", raising=False)
+
+ assert len(reattach_legacy_vectors(tmp_path, profile)) == 1
+
+ # What the SDK does next, in the same process: no settings of its own.
+ sdk = chromadb.PersistentClient(path=str(tmp_path / "hy-memory" / "governed-v2" / "vector"))
+ try:
+ assert sdk.get_collection(f"{current}_4").count() == 2
+ finally:
+ sdk.close()
+
+
def _confirmed(store, *memory_ids: str, kind: str = "chat") -> None:
with main_database(store.database_path).write() as database, database:
for memory_id in memory_ids:
diff --git a/tests/coding/test_list_and_stats_by_scene.py b/tests/coding/test_list_and_stats_by_scene.py
new file mode 100644
index 0000000..e05d4d9
--- /dev/null
+++ b/tests/coding/test_list_and_stats_by_scene.py
@@ -0,0 +1,197 @@
+# -*- coding: utf-8 -*-
+"""list_memories(scene=...) 与 memory_stats:三类记忆都列得出、数得清。
+
+MemHub 的记忆库只调 list_memories,不传 workspace_id 时只列 chat,coding /
+working 两类经验既列不出也数不到:一个 45 条记忆的库在界面上只剩 22 条。
+"""
+
+import asyncio
+from datetime import datetime, timedelta
+from types import SimpleNamespace
+
+import pytest
+
+from hy_memory.client import HyMemoryClient
+from hy_memory.coding.store import CodingMemoryStore
+from hy_memory.coding.types import CodingMemory, WorkingFact
+from hy_memory.models.memory import MemoryLayer, MemoryStatus
+
+BASE = datetime(2026, 10, 20, 8, 0, 0)
+
+
+def _node(node_id, *, created, modified=None, layer=MemoryLayer.L2_FACT):
+ return SimpleNamespace(
+ node_id=node_id,
+ layer=layer,
+ gmt_created=created,
+ gmt_modified=modified,
+ valid_from=None,
+ session_id="s",
+ source="cursor",
+ )
+
+
+class _FakeVectorStore:
+ def __init__(self, nodes):
+ self.nodes = nodes
+
+ async def list_by_user(self, *, layers=None, **_):
+ if layers == [MemoryLayer.L0_BASIC_INFO]:
+ return []
+ return list(self.nodes)
+
+
+async def _store(coding_config, mock_embed, variant):
+ store = CodingMemoryStore(coding_config, embed_service=mock_embed, variant=variant)
+ await store.initialize()
+ return store
+
+
+def _client(nodes, stores):
+ client = object.__new__(HyMemoryClient)
+ client._vector_store = _FakeVectorStore(nodes)
+ client._coding_stores = dict(stores)
+ client._config = None
+ return client
+
+
+def _unwrapped(name):
+ # The public async_* methods hop onto the client's own loop thread; the
+ # logic under test does not need one.
+ return getattr(HyMemoryClient, name).__wrapped__
+
+
+async def test_stats_counts_every_scene_with_its_latest_times(coding_config, mock_embed):
+ coding = await _store(coding_config, mock_embed, "coding")
+ working = await _store(coding_config, mock_embed, "working")
+ try:
+ await coding.insert(CodingMemory(
+ experience_id="c1", user_id="u", task="T", solution="S",
+ created_at=BASE, updated_at=BASE + timedelta(hours=1),
+ ))
+ await coding.insert(CodingMemory(
+ experience_id="c2", user_id="u", task="T", solution="S",
+ workspace_id="ws/a",
+ created_at=BASE + timedelta(hours=2), updated_at=BASE + timedelta(hours=3),
+ ))
+ await coding.insert(CodingMemory(
+ experience_id="other-user", user_id="v", task="T", solution="S",
+ ))
+ await working.insert(CodingMemory(
+ experience_id="w1", user_id="u", task="T", solution="S",
+ created_at=BASE, updated_at=BASE,
+ ))
+ await working.insert_fact(WorkingFact(fact_id="f1", user_id="u", statement="x"))
+ nodes = [
+ _node("m1", created=BASE, modified=BASE + timedelta(days=1)),
+ _node("m2", created=BASE + timedelta(hours=5)),
+ ]
+ client = _client(nodes, {"coding": coding, "working": working})
+
+ stats = await _unwrapped("async_memory_stats")(client, user_id="u")
+
+ assert stats["total"] == 2 + 2 + 1
+ assert stats["scenes"]["chat"] == {
+ "count": 2,
+ "latest_created_at": int((BASE + timedelta(hours=5)).timestamp()),
+ "latest_updated_at": int((BASE + timedelta(days=1)).timestamp()),
+ }
+ assert stats["scenes"]["coding"]["count"] == 2
+ assert stats["scenes"]["coding"]["latest_created_at"] == int(
+ (BASE + timedelta(hours=2)).timestamp()
+ )
+ # The store stamps updated_at on write; the stat is the newest it holds.
+ stored = [await coding.get_by_id(eid, user_id="u") for eid in ("c1", "c2")]
+ assert stats["scenes"]["coding"]["latest_updated_at"] == max(
+ int(item.updated_at.timestamp()) for item in stored
+ )
+ assert stats["scenes"]["working"]["count"] == 1
+ assert stats["scenes"]["working"]["facts"] == 1
+ finally:
+ await coding.close()
+ await working.close()
+
+
+async def test_stats_of_an_unwritten_scene_is_zero_without_creating_its_database(
+ coding_config,
+):
+ client = _client([], {})
+ client._config = coding_config
+
+ stats = await _unwrapped("async_memory_stats")(client, user_id="u")
+
+ assert stats["total"] == 0
+ assert stats["scenes"]["coding"] == {
+ "count": 0, "latest_created_at": None, "latest_updated_at": None, "facts": 0,
+ }
+ assert not CodingMemoryStore.database_exists(
+ coding_config, db_path=coding_config.coding.db_path, variant="coding"
+ )
+
+
+async def test_list_by_scene_reads_that_scene_alone_with_its_real_total(
+ coding_config, mock_embed,
+):
+ coding = await _store(coding_config, mock_embed, "coding")
+ working = await _store(coding_config, mock_embed, "working")
+ try:
+ for index in range(3):
+ await coding.insert(CodingMemory(
+ experience_id=f"c{index}", user_id="u", task=f"T{index}", solution="S",
+ workspace_id="ws/a" if index == 0 else None,
+ updated_at=BASE + timedelta(hours=index),
+ ))
+ await working.insert(CodingMemory(
+ experience_id="w1", user_id="u", task="W", solution="S",
+ ))
+ client = _client([], {"coding": coding, "working": working})
+ list_memories = _unwrapped("async_list_memories")
+
+ page = await list_memories(client, user_id="u", scene="coding", limit=2)
+ assert page["variant"] == "coding"
+ assert [item["experience_id"] for item in page["experiences"]] == ["c2", "c1"]
+ assert page["total"] == 3
+
+ scoped = await list_memories(client, user_id="u", scene="coding", workspace_id="ws/a")
+ assert [item["experience_id"] for item in scoped["experiences"]] == ["c0"]
+ assert scoped["total"] == 1
+
+ other = await list_memories(client, user_id="u", scene="working")
+ assert [item["experience_id"] for item in other["experiences"]] == ["w1"]
+
+ archived = await list_memories(
+ client, user_id="u", scene="coding", statuses=[MemoryStatus.ARCHIVED]
+ )
+ assert archived["experiences"] == [] and archived["total"] == 0
+ finally:
+ await coding.close()
+ await working.close()
+
+
+def test_an_unknown_scene_is_refused():
+ with pytest.raises(ValueError, match="scene"):
+ HyMemoryClient._list_scene("raw")
+ assert HyMemoryClient._list_scene(None) is None
+ assert HyMemoryClient._list_scene(" Coding ") == "coding"
+
+
+def test_list_without_scene_still_lists_chat(monkeypatch):
+ client = _client([_node("m1", created=BASE)], {})
+ client._sort_memory_nodes = lambda nodes, order: nodes
+ client._memory_node_to_list_item = lambda node: {"memory_id": node.node_id}
+
+ async def no_graph(**_):
+ return None
+
+ async def no_op_log(**_):
+ return None
+
+ client._list_graph_bucket = no_graph
+ client._maybe_store_op_log = no_op_log
+ client._backend_identity = lambda: {}
+ client._normalize_layers = lambda layers: layers
+
+ out = asyncio.run(_unwrapped("async_list_memories")(client, user_id="u"))
+
+ assert out["vdb"]["total"] == 1
+ assert out["vdb"]["memories"] == [{"memory_id": "m1"}]