mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-16 09:21:50 +00:00
fix(integrations): address Hermes provider review
This commit is contained in:
parent
46d4f3f620
commit
4eba5cdab1
6 changed files with 165 additions and 25 deletions
|
|
@ -22,8 +22,8 @@ hermes memory setup
|
|||
Hermes Dashboard 会展示 provider 的模式相关字段和高级 recall、health、write、shutdown 设置;切换模式后只显示对应的
|
||||
HTTP endpoint 或 Embedded workspace 字段。
|
||||
|
||||
配置主路径为 `$HERMES_HOME/reme/config.json`。旧 `$HERMES_HOME/reme.json` 在新路径不存在时仍可读取,后续保存使用
|
||||
新路径但不会删除旧文件。
|
||||
配置主路径为 `$HERMES_HOME/reme/config.json`。新文件未包含的字段仍会从旧 `$HERMES_HOME/reme.json` 继承,新文件中的
|
||||
值优先;后续 CLI 保存会写入完整的新配置,但不会删除旧文件。
|
||||
|
||||
## HTTP 模式
|
||||
|
||||
|
|
|
|||
|
|
@ -78,9 +78,9 @@ Configuration is profile-local at:
|
|||
$HERMES_HOME/reme/config.json
|
||||
```
|
||||
|
||||
The earlier `$HERMES_HOME/reme.json` location is still read when the current
|
||||
path does not exist. The next CLI setup save writes the current path without
|
||||
deleting the legacy file.
|
||||
The earlier `$HERMES_HOME/reme.json` location remains a fallback for fields
|
||||
omitted from the current file. Current values take precedence, and the next CLI
|
||||
setup save writes a complete current config without deleting the legacy file.
|
||||
|
||||
## HTTP mode
|
||||
|
||||
|
|
|
|||
|
|
@ -60,8 +60,8 @@ Hermes Dashboard 的 **Plugins → Runtime provider plugins → Memory provider
|
|||
|
||||

|
||||
|
||||
配置主路径为 `$HERMES_HOME/reme/config.json`。旧 `$HERMES_HOME/reme.json` 在新路径不存在时仍可读取,后续保存使用
|
||||
新路径但不会删除旧文件。
|
||||
配置主路径为 `$HERMES_HOME/reme/config.json`。新文件未包含的字段仍会从旧 `$HERMES_HOME/reme.json` 继承,新文件中的
|
||||
值优先;后续 CLI 保存会写入完整的新配置,但不会删除旧文件。
|
||||
|
||||
## HTTP 模式
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import re
|
|||
import threading
|
||||
import time
|
||||
|
||||
from dataclasses import replace
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from agent.memory_provider import MemoryProvider, RecallStatus
|
||||
|
|
@ -76,6 +77,7 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
self._write_thread_lock = threading.Lock()
|
||||
self._backend_lock = threading.RLock()
|
||||
self._shutdown_started = False
|
||||
self._deferred_backend_close = False
|
||||
self._atexit_registered = False
|
||||
self._recall_status: RecallStatus | None = None
|
||||
self._unavailable_reason = ""
|
||||
|
|
@ -143,6 +145,7 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
self._write_queue = queue.Queue()
|
||||
self._write_thread = None
|
||||
self._shutdown_started = False
|
||||
self._deferred_backend_close = False
|
||||
self._recall_status = None
|
||||
if not self._atexit_registered:
|
||||
atexit.register(self._atexit_shutdown)
|
||||
|
|
@ -273,15 +276,31 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
query = str(query or "").strip()
|
||||
if not query or time.monotonic() < self._next_recall_attempt:
|
||||
return ""
|
||||
with self._backend_lock:
|
||||
if not self._ensure_backend():
|
||||
deadline = time.monotonic() + self._recall_timeout
|
||||
if not self._backend_lock.acquire( # pylint: disable=consider-using-with
|
||||
timeout=max(0.0, deadline - time.monotonic()),
|
||||
):
|
||||
logger.warning(
|
||||
"ReMe retrieval at %s timed out waiting for the backend",
|
||||
self._backend_label,
|
||||
)
|
||||
return ""
|
||||
try:
|
||||
if not self._ensure_backend(deadline=deadline):
|
||||
return ""
|
||||
assert self._backend is not None
|
||||
try:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
logger.warning(
|
||||
"ReMe retrieval at %s exhausted its timeout before search",
|
||||
self._backend_label,
|
||||
)
|
||||
return ""
|
||||
response = self._backend.search(
|
||||
query,
|
||||
limit=self._recall_limit,
|
||||
timeout=self._recall_timeout,
|
||||
timeout=remaining,
|
||||
)
|
||||
except ReMeBackendError as exc:
|
||||
self._next_recall_attempt = time.monotonic() + self._health_retry_seconds
|
||||
|
|
@ -293,6 +312,8 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
return ""
|
||||
finally:
|
||||
self._close_backend_if_shutdown_locked()
|
||||
finally:
|
||||
self._backend_lock.release()
|
||||
answer = response.get("answer")
|
||||
answer = answer.strip() if isinstance(answer, str) else ""
|
||||
if answer:
|
||||
|
|
@ -382,12 +403,16 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
self._shutdown_timeout,
|
||||
abandoned,
|
||||
)
|
||||
self._deferred_backend_close = True
|
||||
# A context manager cannot express the bounded wait required by shutdown.
|
||||
if self._backend_lock.acquire( # pylint: disable=consider-using-with
|
||||
timeout=max(0.0, deadline - time.monotonic()),
|
||||
):
|
||||
try:
|
||||
self._close_backend_locked()
|
||||
self._close_backend_locked(
|
||||
timeout=max(0.0, deadline - time.monotonic()),
|
||||
)
|
||||
self._deferred_backend_close = False
|
||||
finally:
|
||||
self._backend_lock.release()
|
||||
else:
|
||||
|
|
@ -464,7 +489,7 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
)
|
||||
return
|
||||
with self._backend_lock:
|
||||
if not self._ensure_backend():
|
||||
if not self._ensure_backend(allow_shutdown=True):
|
||||
logger.warning(
|
||||
"ReMe write for session %s skipped because backend is unavailable",
|
||||
payload["session_id"],
|
||||
|
|
@ -487,18 +512,34 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
finally:
|
||||
self._close_backend_if_shutdown_locked()
|
||||
|
||||
def _ensure_backend(self, *, force: bool = False) -> bool:
|
||||
# pylint: disable-next=too-many-return-statements
|
||||
def _ensure_backend(
|
||||
self,
|
||||
*,
|
||||
force: bool = False,
|
||||
allow_shutdown: bool = False,
|
||||
deadline: float | None = None,
|
||||
) -> bool:
|
||||
with self._backend_lock:
|
||||
if self._backend_available and self._backend is not None and not force:
|
||||
return True
|
||||
if self._config is None or self._shutdown_started:
|
||||
if self._config is None or (self._shutdown_started and not allow_shutdown):
|
||||
return False
|
||||
now = time.monotonic()
|
||||
if not force and now < self._next_health_probe:
|
||||
return False
|
||||
if self._backend is None:
|
||||
try:
|
||||
self._backend = _backend_for(self._config)
|
||||
backend_config = self._config
|
||||
if deadline is not None:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
return False
|
||||
backend_config = replace(
|
||||
backend_config,
|
||||
request_timeout=min(backend_config.request_timeout, remaining),
|
||||
)
|
||||
self._backend = _backend_for(backend_config)
|
||||
self._backend.start()
|
||||
except (ReMeBackendError, TypeError, ValueError, OSError) as exc:
|
||||
failed, self._backend = self._backend, None
|
||||
|
|
@ -513,7 +554,13 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
self._mark_unavailable("startup", exc)
|
||||
return False
|
||||
try:
|
||||
self._backend.health(timeout=self._health_timeout)
|
||||
health_timeout = self._health_timeout
|
||||
if deadline is not None:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
return False
|
||||
health_timeout = min(health_timeout, remaining)
|
||||
self._backend.health(timeout=health_timeout)
|
||||
except ReMeBackendError as exc:
|
||||
self._mark_unavailable("health check", exc)
|
||||
return False
|
||||
|
|
@ -522,14 +569,17 @@ class ReMeMemoryProvider(MemoryProvider):
|
|||
return True
|
||||
|
||||
def _close_backend_if_shutdown_locked(self) -> None:
|
||||
if self._shutdown_started:
|
||||
if self._deferred_backend_close:
|
||||
self._close_backend_locked()
|
||||
self._deferred_backend_close = False
|
||||
|
||||
def _close_backend_locked(self) -> None:
|
||||
def _close_backend_locked(self, *, timeout: float | None = None) -> None:
|
||||
backend, self._backend = self._backend, None
|
||||
if backend is not None:
|
||||
try:
|
||||
backend.close(timeout=self._shutdown_timeout)
|
||||
backend.close(
|
||||
timeout=self._shutdown_timeout if timeout is None else timeout,
|
||||
)
|
||||
except ReMeBackendError as exc:
|
||||
logger.warning(
|
||||
"ReMe backend shutdown failed at %s: %s",
|
||||
|
|
|
|||
|
|
@ -71,6 +71,13 @@ def _read_json_object(path: Path) -> dict[str, Any]:
|
|||
return loaded
|
||||
|
||||
|
||||
def _read_config_values(home: Path) -> dict[str, Any]:
|
||||
"""Merge legacy values under the current Dashboard-managed config."""
|
||||
values = _read_json_object(legacy_config_path(home))
|
||||
values.update(_read_json_object(config_path(home)))
|
||||
return values
|
||||
|
||||
|
||||
def _positive_float(value: Any, key: str, default: float) -> float:
|
||||
if value in (None, ""):
|
||||
return default
|
||||
|
|
@ -164,19 +171,16 @@ def parse_config(values: dict[str, Any], *, hermes_home: str | Path) -> ReMeConf
|
|||
|
||||
|
||||
def load_config(hermes_home: str | Path | None = None) -> ReMeConfig:
|
||||
"""Load current config, falling back to the legacy file when necessary."""
|
||||
"""Load current config, inheriting omitted fields from the legacy file."""
|
||||
home = Path(hermes_home).expanduser() if hermes_home is not None else _default_hermes_home()
|
||||
current = config_path(home)
|
||||
source = current if current.is_file() else legacy_config_path(home)
|
||||
return parse_config(_read_json_object(source), hermes_home=home)
|
||||
return parse_config(_read_config_values(home), hermes_home=home)
|
||||
|
||||
|
||||
def save_config(values: dict[str, Any], hermes_home: str | Path) -> ReMeConfig:
|
||||
"""Validate and atomically write config in the current Hermes layout."""
|
||||
home = Path(hermes_home).expanduser()
|
||||
current = config_path(home)
|
||||
source = current if current.is_file() else legacy_config_path(home)
|
||||
merged = _read_json_object(source)
|
||||
merged = _read_config_values(home)
|
||||
merged.update(
|
||||
{key: value for key, value in dict(values or {}).items() if value is not None},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -99,6 +99,21 @@ def test_current_config_precedes_legacy(tmp_path):
|
|||
assert load_config(tmp_path).endpoint == "http://current:2"
|
||||
|
||||
|
||||
def test_sparse_dashboard_config_inherits_legacy_values(tmp_path):
|
||||
(tmp_path / "reme.json").write_text(
|
||||
'{"endpoint": "http://legacy:2444", "recall_limit": 3}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
current = config_path(tmp_path)
|
||||
current.parent.mkdir()
|
||||
current.write_text('{"recall_limit": 7}', encoding="utf-8")
|
||||
|
||||
config = load_config(tmp_path)
|
||||
|
||||
assert config.endpoint == "http://legacy:2444"
|
||||
assert config.recall_limit == 7
|
||||
|
||||
|
||||
def test_embedded_config_normalizes_workspace(tmp_path):
|
||||
config = parse_config(
|
||||
{"mode": " EMBEDDED ", "workspace_dir": str(tmp_path / "workspace")},
|
||||
|
|
@ -378,6 +393,77 @@ def test_shutdown_defers_close_until_inflight_recall_finishes():
|
|||
assert backend.closed is True
|
||||
|
||||
|
||||
def test_shutdown_drains_all_accepted_writes_before_closing_backend():
|
||||
entered = threading.Event()
|
||||
release = threading.Event()
|
||||
|
||||
class BlockingBackend(_FakeBackend):
|
||||
def auto_memory(self, session_id, messages, *, timeout):
|
||||
del timeout
|
||||
self.writes.append((session_id, messages))
|
||||
if len(self.writes) == 1:
|
||||
entered.set()
|
||||
release.wait(timeout=2)
|
||||
|
||||
backend = BlockingBackend()
|
||||
provider = ReMeMemoryProvider()
|
||||
provider._config = ReMeConfig()
|
||||
provider._backend = backend
|
||||
provider._backend_available = True
|
||||
provider._shutdown_timeout = 1
|
||||
for index in range(3):
|
||||
provider.sync_turn(f"user {index}", f"assistant {index}", session_id=f"session-{index}")
|
||||
assert entered.wait(timeout=1)
|
||||
|
||||
shutdown = threading.Thread(target=provider.shutdown)
|
||||
shutdown.start()
|
||||
deadline = time.monotonic() + 1
|
||||
while not provider._shutdown_started and time.monotonic() < deadline:
|
||||
time.sleep(0.001)
|
||||
assert provider._shutdown_started is True
|
||||
release.set()
|
||||
shutdown.join(timeout=2)
|
||||
|
||||
assert shutdown.is_alive() is False
|
||||
assert len(backend.writes) == 3
|
||||
assert backend.closed is True
|
||||
|
||||
|
||||
def test_recall_timeout_includes_waiting_for_background_write():
|
||||
write_entered = threading.Event()
|
||||
write_release = threading.Event()
|
||||
search_entered = threading.Event()
|
||||
|
||||
class BlockingBackend(_FakeBackend):
|
||||
def auto_memory(self, session_id, messages, *, timeout):
|
||||
del session_id, messages, timeout
|
||||
write_entered.set()
|
||||
write_release.wait(timeout=2)
|
||||
|
||||
def search(self, query, *, limit, timeout):
|
||||
del query, limit, timeout
|
||||
search_entered.set()
|
||||
return {"success": True, "answer": "remembered", "metadata": {}}
|
||||
|
||||
backend = BlockingBackend()
|
||||
provider = ReMeMemoryProvider()
|
||||
provider._config = ReMeConfig()
|
||||
provider._backend = backend
|
||||
provider._backend_available = True
|
||||
provider._recall_timeout = 0.05
|
||||
provider.sync_turn("user", "assistant", session_id="session")
|
||||
assert write_entered.wait(timeout=1)
|
||||
|
||||
started = time.monotonic()
|
||||
assert provider.prefetch("query") == ""
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
assert elapsed < 0.2
|
||||
assert search_entered.is_set() is False
|
||||
write_release.set()
|
||||
provider.shutdown()
|
||||
|
||||
|
||||
def test_shutdown_discard_keeps_sentinel_for_inflight_writer():
|
||||
provider = ReMeMemoryProvider()
|
||||
provider._write_queue.put({"session_id": "queued"})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue