ReMe/integrations/hermes_agent/backend.py
jinliyl 5231f3970c
Some checks failed
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
fix(hermes): preserve profile context and reset invalid provider state (#554)
* Fix Hermes provider profile thread compatibility and reinitialization

* docs(hermes): clarify compatibility checks were manual
2026-09-16 16:01:15 +08:00

67 lines
2.1 KiB
Python

"""Transport-independent backend contract for the Hermes provider."""
from __future__ import annotations
import contextvars
import threading
from typing import Any, Callable, Protocol
class ReMeBackendError(RuntimeError):
"""Raised when a ReMe backend cannot complete an operation."""
class ReMeBackend(Protocol):
"""Synchronous interface matching Hermes' memory-provider lifecycle."""
label: str
def start(self, *, deadline: float | None = None) -> None:
"""Start owned resources before an optional absolute deadline."""
def health(self, *, timeout: float) -> dict[str, Any]:
"""Return a semantically healthy ReMe response."""
def search(self, query: str, *, limit: int, timeout: float) -> dict[str, Any]:
"""Search the configured ReMe workspace."""
def auto_memory(
self,
session_id: str,
messages: list[dict[str, Any]],
*,
timeout: float,
) -> dict[str, Any]:
"""Record and extract memory from one completed turn."""
def close(self, *, timeout: float) -> None:
"""Release resources within a bounded interval."""
def spawn_profile_thread(
target: Callable[..., Any],
*,
name: str,
args: tuple[Any, ...] = (),
) -> threading.Thread:
"""Create a profile-aware daemon thread across supported Hermes versions."""
try:
from agent.memory_provider import spawn_context_thread
except ImportError:
context = contextvars.copy_context()
def run() -> None:
context.run(target, *args)
return threading.Thread(target=run, name=name, daemon=True)
return spawn_context_thread(target, name=name, args=args)
def require_healthy(response: dict[str, Any]) -> dict[str, Any]:
"""Validate the semantic health flag in a successful ReMe response."""
metadata = response.get("metadata")
health = metadata.get("health") if isinstance(metadata, dict) else None
if not isinstance(health, dict) or health.get("healthy") is not True:
raise ReMeBackendError("ReMe did not report a healthy component snapshot")
return response