Repository navigation
Description
Pre-submission checklist | 提交前检查
- I have searched existing issues and this hasn't been mentioned before | 我已搜索现有问题,确认此问题尚未提及
- I have read the project documentation and confirmed this issue doesn't already exist | 我已阅读项目文档并确认此问题尚未存在
- This issue is specific to MemOS and not a general software issue | 该问题是针对 MemOS 的,而不是一般软件问题
Bug Description | 问题描述
When MOS_ENABLE_REORGANIZE=true, the GraphStructureReorganizer periodic structure optimization silently works on only one graph tenant — the one configured on the shared default cube (memosdefault) — while data written through the REST API is stored under each request's user_name. As a result, no PARENT / INFERS / FOLLOWS edges are ever created for real tenant data, and the feature appears to "write nodes only, never build edges".
There are also two latent bugs in the same code path:
- The 100-second periodic schedule never runs.
GraphStructureReorganizer._periodic_optimize_structureregistersschedule.every(100).seconds.do(self.optimize_structure, ...)but the loop never callsschedule.run_pending(), so these scheduled jobs are dead code. Only the_reorganize_needednew-node trigger actually executes (again, single-tenant). _summarize_clustercrashes on non-JSON LLM output. If the LLM returns something that is not a JSON object,_parse_json_resultyields a non-dict value and the subsequentresponse_json.get(...)raises, killing the whole optimization pass (including any already-computed sub-clusters).
Environment | 环境信息
- Version: main @ a7367d0 (2.0.33)
- Deployment: docker compose (
docker/docker-compose.yml),MOS_ENABLE_REORGANIZE=true,NEO4J_BACKEND=neo4j-community
Root Cause | 原因分析
src/memos/memories/textual/tree_text_memory/organize/reorganizer.py:_periodic_optimize_structurecallsself.optimize_structure(scope=...)without auser_name, so it defaults toself.graph_store.config.user_name— the default cube's tenant (memosdefault), not the tenants that actually received writes via/product/add.- The same method registers
schedule.every(100).seconds.do(...)jobs but its polling loop lacksschedule.run_pending(). _summarize_clusterassumes the LLM always returns a parseable JSON object and passes the result straight into_create_parent_node, so a single malformed response aborts the entire pass.
Proposed Fix | 建议修复
I have a working patch (validated on a live deployment) that:
- Adds
Neo4jGraphDB.get_all_user_names()(src/memos/graph_dbs/neo4j.py) —MATCH (n:Memory) RETURN DISTINCT n.user_name, with graceful fallback to the configureduser_name. - Adds
GraphStructureReorganizer._optimize_all_users(scope)— enumerates all tenants in the graph and runsoptimize_structure(scope=..., user_name=...)for each, tolerating per-tenant failures. - Routes both the scheduled jobs and the
_reorganize_neededtrigger through_optimize_all_users, and adds the missingschedule.run_pending()call. - Makes
_summarize_clusterreturnNonewhen the LLM response is not a JSON object, and guards_create_parent_node/add_edgecalls againstNone.
Note: since BaseGraphDB defines the interface, other graph backends (neo4j-community / polardb / postgres) would need an equivalent get_all_user_names() implementation — or the base class could provide a default fallback to config.user_name.
I am not submitting a PR yet per my own workflow, but I can if the approach is endorsed.
Willingness to Implement | 实现意愿
- I'm willing to implement this myself | 我愿意自己解决
Activity
akushonkamen commentedon Oct 11, 2026
Confirmed all three root causes on
main@ a7367d0:_run_structure_organizer_looptriggersoptimize_structure(scope=...)withoutuser_name(reorganizer.py:168-169), and theneo4j.pymethods all resolveuser_name if user_name else self.config.user_name→ only the default cube's tenant (memosdefault) is ever optimized; per-requestuser_nametenants written via the REST API never getPARENT/INFERS/FOLLOWSedges.schedule.every(100).secondsjobs (reorganizer.py:158-159) never run: the loop never callsschedule.run_pending().extract_first_to_last_bracereturnsNonewhen the text has no braces and_parse_json_resultonly catchesJSONDecodeError, soNone/non-dict values reachresponse_json.get(...)in_summarize_cluster(:572) and_local_subcluster(:442), killing the whole pass including already-summarized sibling clusters. Also, theJSONDecodeError → {}path silently creates empty parent nodes withembed("").@smoryan your proposed fix matches exactly what we found — thanks for the thorough analysis. We're preparing a PR along those lines:
get_all_user_names()onNeo4jGraphDBplus a fallback default onBaseGraphDBso postgres/polardb keep today's behavior;_optimize_all_users(scope)that enumerates tenants with per-tenant failure isolation, routed through both the_reorganize_neededtrigger and the scheduled jobs;schedule.run_pending()in the loop; and_parse_json_resultreturningNonefor anything non-dict, withNone-short-circuit at both call sites (which also removes the empty-parent-node pollution).Since this carries
status:needs-product-review: the one product-level question is whether structure optimization should span all tenants. Our patch keeps it conservative — enumerate the tenants actually present in the graph, and fall back to the configured tenant on backends without the new capability (i.e. no behavior change where the interface isn't implemented). Happy to adjust scope, or to review your live-deployment patch instead if you'd rather land it yourself.Transparency note: this analysis and the upcoming patch are prepared with AI coding assistance.
akushonkamen commentedon Oct 11, 2026
PR is up: #2481 —
get_all_user_names()(Neo4j impl +BaseGraphDBfallback),_optimize_all_users(scope)with per-tenant failure isolation wired into both the trigger and the schedule,schedule.run_pending()in the loop, and_parse_json_resultnow returningNonefor non-dict output with short-circuits at both call sites. Tests: 25 focused tests green (17 defect cases red before the fix), graph_dbs 35 passed / 3 skipped, memories regression unchanged vs baseline. Details in the PR description.github-actions commentedon Nov 12, 2026
This issue has been automatically marked as stale due to inactivity.
fix(reorganizer): optimize all graph tenants, run scheduled jobs, and…