Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Hy Memory and MemHub are versioned and released independently.

## Unreleased

- System 2 automatic prompt selection now detects each fact with the SDK language
detector and uses a majority vote, so code-heavy Chinese facts are not diluted
by long ASCII payloads. Explicit language settings still take precedence.

## MemHub 1.0.7 — 2026-11-12

Build 48, on `hy-memory==2.0.16`. Linux x64/arm64 Runtime and macOS arm64
Expand Down
53 changes: 26 additions & 27 deletions hy_memory/pipelines/system2_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2308,34 +2308,33 @@ def _parse_operations_json(text: str) -> Optional[List[Dict[str, Any]]]:


def _detect_language(materials: Dict[str, Any]) -> str:
"""Vote over fact languages using the SDK's code-aware language detector.

Each nonblank fact gets one vote so a long path or code payload cannot
outweigh the prose in other facts. Empty materials and ties use English.
Explicit language overrides are resolved by the caller before detection.
"""
从 materials 的 fact content 检测语言。

扫描所有聚类中的 fact content,计算中文字符占比。
中文比例 >= 70% → "zh",否则 → "en"。
"""
import re
all_text = ""
for cluster in materials.get("clusters", []):
for f in cluster.get("facts", []):
all_text += f.get("content", "") + " "
for f in materials.get("unprocessed_facts", []):
all_text += f.get("content", "") + " "

if not all_text.strip():
return "en"

# 统计中文字符(CJK Unified Ideographs)
zh_chars = len(re.findall(r'[\u4e00-\u9fff]', all_text))
# 统计所有非空白字符
total_chars = len(re.findall(r'\S', all_text))

if total_chars == 0:
return "en"

zh_ratio = zh_chars / total_chars
lang = "zh" if zh_ratio >= 0.7 else "en"
logger.debug(f"[S2-agent] language detection: zh_chars={zh_chars} total={total_chars} ratio={zh_ratio:.2f} → {lang}")
from ..utils.lang_detect import detect_language

facts = [
fact
for cluster in materials.get("clusters", [])
for fact in cluster.get("facts", [])
]
facts.extend(materials.get("unprocessed_facts", []))
zh_votes = total = 0
for fact in facts:
content = fact.get("content", "")
if not content.strip():
continue
total += 1
zh_votes += detect_language(content) == "zh"

lang = "zh" if zh_votes > total / 2 else "en"
logger.debug(
"[S2-agent] language detection: zh_facts=%d total=%d → %s",
zh_votes, total, lang,
)
return lang


Expand Down
19 changes: 16 additions & 3 deletions memhub/apps/core/tests/test_search_without_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,21 @@ async def test_a_committed_switch_rebuilds_reads_without_closing_one_in_flight(
from one_memory.secrets import InMemorySecretStore

recorder = _Recorder(tmp_path)
monkeypatch.setattr("one_memory.hy_sdk_runtime.acquire_client", recorder)
loop = asyncio.get_running_loop()
client_closed = asyncio.Event()

def acquire(*args, **kwargs):
lease = recorder(*args, **kwargs)
close = lease.close

def signal_close() -> None:
close()
loop.call_soon_threadsafe(client_closed.set)

lease.close = signal_close
return lease

monkeypatch.setattr("one_memory.hy_sdk_runtime.acquire_client", acquire)
profiles = ModelProfileStore(tmp_path / "one-memory.sqlite3")
secrets = InMemorySecretStore()
spaces = EmbeddingSpaceStore(profiles.database_path)
Expand Down Expand Up @@ -277,8 +291,7 @@ async def read() -> str:

finish.set()
assert await in_flight == old
for _ in range(5):
await asyncio.sleep(0)
await asyncio.wait_for(client_closed.wait(), timeout=5)
assert recorder.closed == [old]
await handle.close()
assert recorder.closed == [old, new]
Expand Down
8 changes: 6 additions & 2 deletions tests/test_prompt_language_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ def test_system2_prompts_follow_language(code):


@pytest.mark.asyncio
async def test_system2_agent_uses_explicit_language(monkeypatch):
@pytest.mark.parametrize("fact_content", [
"用户每天跑步",
"用户使用 Python 开发,运行 pytest tests/test_search_client.py --maxfail=1",
])
async def test_system2_agent_uses_explicit_language(monkeypatch, fact_content):
from hy_memory.pipelines import system2_agent as s2

seen = {}
Expand All @@ -422,7 +426,7 @@ async def complete_messages(self, messages, **kwargs):

monkeypatch.setattr("hy_memory.agent.llm_provider.LLMProvider", _Provider)
materials = {"stats": {}, "clusters": [{"facts": [
{"layer": "L2", "content": "用户每天跑步", "node_id": "f1"}], "centroid_text": "跑步"}]}
{"layer": "L2", "content": fact_content, "node_id": "f1"}], "centroid_text": "跑步"}]}
async def _run(**kwargs):
# Only the prompt sent to the model matters here; executing the
# (empty) operation list needs a real tool executor.
Expand Down
41 changes: 41 additions & 0 deletions tests/test_system2_language_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""System 2 should route technical facts by prose language, not code length."""

import pytest

from hy_memory.pipelines.system2_agent import _detect_language


ZH_FACT = "用户使用 Python 开发,运行 pytest tests/test_search_client.py --maxfail=1"
EN_FACT = "The user runs pytest tests/test_search_client.py --maxfail=1"


@pytest.mark.parametrize("source", ["clusters", "unprocessed_facts"])
@pytest.mark.parametrize("content,expected", [
(ZH_FACT, "zh"),
("用户喜欢咖啡", "zh"),
(EN_FACT, "en"),
("Please update the 中文 README file.", "en"),
("", "en"),
(" \n\t", "en"),
])
def test_fact_language_ignores_technical_payload_length(source, content, expected):
facts = [{"content": content}]
materials = {source: [{"facts": facts}] if source == "clusters" else facts}
assert _detect_language(materials) == expected


@pytest.mark.parametrize("contents,expected", [
([EN_FACT * 20, ZH_FACT, ZH_FACT], "zh"),
([ZH_FACT * 20, EN_FACT, EN_FACT], "en"),
([ZH_FACT, EN_FACT], "en"),
(["", " ", ZH_FACT], "zh"),
([], "en"),
])
def test_materials_vote_across_both_fact_sources(contents, expected):
facts = [{"content": content} for content in contents]
for ordered in (facts, list(reversed(facts))):
materials = {
"clusters": [{"facts": ordered[:1]}],
"unprocessed_facts": ordered[1:],
}
assert _detect_language(materials) == expected
Loading