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
13 changes: 13 additions & 0 deletions src/memos/graph_dbs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,16 @@ def add_nodes_batch(self, nodes: list[dict[str, Any]], user_name: str | None = N
- metadata: dict[str, Any] - Node metadata
user_name: Optional user name (will use config default if not provided)
"""

def get_all_user_names(self) -> list[str]:
"""
List every tenant (user_name) that currently has data in the graph store.

Default fallback for backends that do not support tenant enumeration:
returns only the configured default user_name (if any), preserving the
previous single-tenant behavior. Backends with tenant isolation (e.g.
Neo4j) override this with a real implementation.
"""
config = getattr(self, "config", None)
user_name = getattr(config, "user_name", None)
return [user_name] if isinstance(user_name, str) and user_name.strip() else []
32 changes: 32 additions & 0 deletions src/memos/graph_dbs/neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,38 @@ def node_not_exist(self, scope: str, user_name: str | None = None) -> int:
)
return result.single() is None

def get_all_user_names(self) -> list[str]:
"""List every tenant (user_name) that currently has Memory nodes.

Used by the structure reorganizer to optimize all tenants instead of
only the configured default one. Falls back to the configured
``user_name`` when the query fails or the graph has no named tenants,
so callers keep the previous single-tenant behavior.
"""
user_name = self.config.user_name
fallback = [user_name] if isinstance(user_name, str) and user_name.strip() else []
if self.config.use_multi_db:
return fallback
try:
query = """
MATCH (n:Memory)
WHERE n.user_name IS NOT NULL
RETURN DISTINCT n.user_name AS user_name
"""
with self.driver.session(database=self.db_name) as session:
result = session.run(query)
user_names = sorted(
{
record["user_name"]
for record in result
if isinstance(record["user_name"], str) and record["user_name"].strip()
}
)
return user_names or fallback
except Exception as e:
logger.warning("[get_all_user_names] Failed to enumerate tenants: %s", e)
return fallback

def remove_oldest_memory(
self, memory_type: str, keep_latest: int, user_name: str | None = None
) -> None:
Expand Down
174 changes: 136 additions & 38 deletions src/memos/memories/textual/tree_text_memory/organize/reorganizer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
import re
import time
import traceback

from collections import defaultdict
from concurrent.futures import as_completed
from queue import PriorityQueue
from threading import Event
from typing import Literal

import numpy as np
Expand Down Expand Up @@ -93,13 +95,14 @@ def __init__(

self.is_reorganize = is_reorganize
self._reorganize_needed = True
self._stop_scheduler = False
self._scheduler_stop_event = Event()
self._is_optimizing = {"LongTermMemory": False, "UserMemory": False}
if self.is_reorganize:
# ____ 1. For queue message driven thread ___________
self.thread = ContextThread(target=self._run_message_consumer_loop)
self.thread.start()
# ____ 2. For periodic structure optimization _______
self._stop_scheduler = False
self._is_optimizing = {"LongTermMemory": False, "UserMemory": False}
self.structure_optimizer_thread = ContextThread(
target=self._run_structure_organizer_loop
)
Expand Down Expand Up @@ -155,20 +158,66 @@ def _run_structure_organizer_loop(self):
"""
import schedule

schedule.every(100).seconds.do(self.optimize_structure, scope="LongTermMemory")
schedule.every(100).seconds.do(self.optimize_structure, scope="UserMemory")

scheduler = schedule.Scheduler()
jobs = [
scheduler.every(100).seconds.do(self._optimize_all_users, scope=scope)
for scope in ("LongTermMemory", "UserMemory")
]
logger.info("Structure optimizer schedule started.")
while not getattr(self, "_stop_scheduler", False):
if any(self._is_optimizing.values()):
time.sleep(1)
continue
if self._reorganize_needed:
logger.info("[Reorganizer] Triggering optimize_structure due to new nodes.")
self.optimize_structure(scope="LongTermMemory")
self.optimize_structure(scope="UserMemory")
self._reorganize_needed = False
time.sleep(30)
try:
while not self._stop_scheduler:
if not any(self._is_optimizing.values()):
if self._reorganize_needed:
# Clear before work so arrivals during optimization remain pending.
self._reorganize_needed = False
for job in jobs:
job.run()
else:
scheduler.run_pending()
self._scheduler_stop_event.wait(30)
finally:
scheduler.clear()

def _optimize_all_users(self, scope: str, **kwargs) -> None:
"""Optimize named tenants independently, with an explicit backend fallback."""
config = getattr(self.graph_store, "config", None)
get_user_names = getattr(self.graph_store, "get_all_user_names", None)
try:
names = get_user_names() if callable(get_user_names) else []
if isinstance(names, str):
names = []
user_names = list(
dict.fromkeys(
name for name in names or [] if isinstance(name, str) and name.strip()
)
)
except Exception as exc:
logger.warning("Failed to enumerate graph tenants: %s", exc)
user_names = []

if not user_names:
default = getattr(config, "user_name", None)
if isinstance(default, str) and default.strip():
user_names = [default]
elif getattr(config, "use_multi_db", False) is True:
# This backend's configured database is already the isolation boundary.
user_names = [None]
else:
logger.warning("Skipping graph optimization without a tenant scope")
return

for user_name in user_names:
if self._stop_scheduler:
return
try:
self.optimize_structure(scope=scope, user_name=user_name, **kwargs)
except Exception:
logger.warning(
"Graph optimization failed for tenant %s, scope %s",
user_name,
scope,
exc_info=True,
)

def stop(self):
"""
Expand All @@ -177,10 +226,11 @@ def stop(self):
if not self.is_reorganize:
return

self._stop_scheduler = True
self._scheduler_stop_event.set()
self.add_message(QueueMessage(op="end"))
self.thread.join()
logger.info("Reorganize thread stopped.")
self._stop_scheduler = True
self.structure_optimizer_thread.join()
logger.info("Structure optimizer stopped.")

Expand Down Expand Up @@ -330,17 +380,20 @@ def _process_cluster_and_write(
if len(sub_nodes) < min_cluster_size:
continue # Skip tiny noise
sub_parent_node = self._summarize_cluster(sub_nodes, scope)
if sub_parent_node is None:
continue # LLM output was not a valid JSON object; skip this sub-cluster
self._create_parent_node(sub_parent_node, user_name=user_name)
self._link_cluster_nodes(sub_parent_node, sub_nodes, user_name=user_name)
sub_parents.append(sub_parent_node)

if sub_parents and len(sub_parents) >= min_cluster_size:
cluster_parent_node = self._summarize_cluster(cluster_nodes, scope)
self._create_parent_node(cluster_parent_node, user_name=user_name)
for sub_parent in sub_parents:
self.graph_store.add_edge(
cluster_parent_node.id, sub_parent.id, "PARENT", user_name=user_name
)
if cluster_parent_node is not None:
self._create_parent_node(cluster_parent_node, user_name=user_name)
for sub_parent in sub_parents:
self.graph_store.add_edge(
cluster_parent_node.id, sub_parent.id, "PARENT", user_name=user_name
)

logger.info("Adding relations/reasons")
nodes_to_check = cluster_nodes
Expand Down Expand Up @@ -436,13 +489,25 @@ def _local_subcluster(
messages = [{"role": "user", "content": prompt}]
response_text = self.llm.generate(messages)
response_json = self._parse_json_result(response_text)
if response_json is None:
logger.warning(
"[GraphStructureReorganize] Skip local sub-clustering: "
"LLM returned no valid JSON object."
)
return []
assigned_ids = set()
result_subclusters = []

for cluster in response_json.get("clusters", []):
clusters = response_json.get("clusters", [])
if not isinstance(clusters, list):
return []
known_ids = {node.id for node in cluster_nodes}
for cluster in clusters:
if not isinstance(cluster, dict) or not isinstance(cluster.get("ids"), list):
continue
ids = []
for nid in cluster.get("ids", []):
if nid not in assigned_ids:
for nid in cluster["ids"]:
if isinstance(nid, str) and nid in known_ids and nid not in assigned_ids:
ids.append(nid)
assigned_ids.add(nid)
sub_nodes = [node for node in cluster_nodes if node.id in ids]
Expand Down Expand Up @@ -547,9 +612,14 @@ def recursive_clustering(nodes_list, depth=0):

return filtered_clusters

def _summarize_cluster(self, cluster_nodes: list[GraphDBNode], scope: str) -> GraphDBNode:
def _summarize_cluster(
self, cluster_nodes: list[GraphDBNode], scope: str
) -> GraphDBNode | None:
"""
Generate a cluster label using LLM, based on top keys in the cluster.

Returns ``None`` when the LLM does not return a valid JSON object, so
the caller can skip the cluster instead of writing an empty parent node.
"""
if not cluster_nodes:
raise ValueError("Cluster nodes cannot be empty.")
Expand All @@ -567,12 +637,31 @@ def _summarize_cluster(self, cluster_nodes: list[GraphDBNode], scope: str) -> Gr
messages = [{"role": "user", "content": prompt}]
response_text = self.llm.generate(messages)
response_json = self._parse_json_result(response_text)
if response_json is None:
logger.warning(
"[GraphStructureReorganize] Skip cluster summary: "
"LLM returned no valid JSON object."
)
return None

# Extract fields
parent_key = response_json.get("key", "").strip()
parent_value = response_json.get("value", "").strip()
parent_key = response_json.get("key")
parent_value = response_json.get("value")
parent_tags = response_json.get("tags", [])
parent_background = response_json.get("summary", "").strip()
parent_background = response_json.get("summary", "")
if not (
isinstance(parent_key, str)
and parent_key.strip()
and isinstance(parent_value, str)
and parent_value.strip()
and isinstance(parent_tags, list)
and all(isinstance(tag, str) for tag in parent_tags)
and isinstance(parent_background, str)
):
logger.warning("Skipping cluster summary with invalid or empty fields")
return None
parent_key = parent_key.strip()
parent_value = parent_value.strip()
parent_background = parent_background.strip()

embedding = self.embedder.embed([parent_value])[0]

Expand All @@ -595,16 +684,25 @@ def _summarize_cluster(self, cluster_nodes: list[GraphDBNode], scope: str) -> Gr
)
return parent_node

def _parse_json_result(self, response_text):
def _parse_json_result(self, response_text: str | None) -> dict | None:
"""Read an object from plain or fenced JSON, allowing surrounding prose."""
if not isinstance(response_text, str) or not response_text.strip():
return None
text = response_text.strip()
fence = re.fullmatch(r"```(?:json)?\s*(.*?)\s*```", text, flags=re.DOTALL | re.IGNORECASE)
if fence:
text = fence.group(1)
try:
response_text = response_text.replace("```", "").replace("json", "")
response_json = extract_first_to_last_brace(response_text)[1]
return response_json
except json.JSONDecodeError as e:
logger.warning(
f"Failed to parse LLM response as JSON: {e}\nRaw response:\n{response_text}"
)
return {}
result = json.loads(text)
except json.JSONDecodeError:
if text.startswith("["):
return None
try:
result = extract_first_to_last_brace(text)[1]
except json.JSONDecodeError:
logger.warning("Skipping malformed JSON in graph reorganization")
return None
return result if isinstance(result, dict) else None

def _create_parent_node(self, parent_node: GraphDBNode, user_name: str | None = None) -> None:
"""
Expand Down
Loading
Loading