The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(hindsight): compact config schema, deadline predicate shared across retain waits
Tekniumb13fc2c6d940
diff --git a/plugins/memory/hindsight/__init__.py b/plugins/memory/hindsight/__init__.pyindex 3bd47383ce6..d3427b51ef2 100644--- a/plugins/memory/hindsight/__init__.py+++ b/plugins/memory/hindsight/__init__.py@@ -373,11 +373,10 @@ class HindsightMemoryProvider(MemoryProvider):"""Install hint for an unavailable local_embedded runtime (is_available() gatesinitialize() out, so the hint it would log never fires; agent_init shows this)."""try:- mode = _load_config().get("mode", "cloud")+ if _load_config().get("mode", "cloud") not in _LOCAL_MODES:+ return ""except Exception:return ""- if mode not in _LOCAL_MODES:- return ""available, reason = _check_local_runtime()return "" if available else _local_runtime_hint(reason).strip()@@ -567,12 +566,10 @@ class HindsightMemoryProvider(MemoryProvider):(pending until recall-visible). No id (older API / sync completion) leavesonly the local queue drain as a signal."""raw_ids = [getattr(retain_response, "operation_id", None), *(getattr(retain_response, "operation_ids", None) or [])]- ids = [str(op) for op in raw_ids if op]- if not ids:- return- self._retain_ops_bank_id = bank_id- with self._pending_retain_ops_lock:- self._pending_retain_ops.update(ids)+ if ids := [str(op) for op in raw_ids if op]:+ self._retain_ops_bank_id = bank_id+ with self._pending_retain_ops_lock:+ self._pending_retain_ops.update(ids)def _is_retain_op_complete(self, bank_id: str, op_id: str) -> bool:"""True when a server-side retain op is done or gone (completed ops are evicted,@@ -598,25 +595,23 @@ class HindsightMemoryProvider(MemoryProvider):server-side async ops complete (async retain returns on acceptance, notdurability). False on timeout/shutdown."""deadline = None if timeout <= 0 else time.monotonic() + timeout+ expired = lambda: deadline is not None and time.monotonic() >= deadline # noqa: E731while self._retain_queue.unfinished_tasks > 0:if self._shutting_down.is_set():return False- if deadline is not None and time.monotonic() >= deadline:+ if expired():logger.debug("Prefetch: retain drain timed out after %.1fs (%d pending)",timeout, self._retain_queue.unfinished_tasks)return Falsetime.sleep(0.05)- return self._wait_for_server_retain_ops(deadline, timeout)+ return self._wait_for_server_retain_ops(expired, timeout)- def _wait_for_server_retain_ops(self, deadline: float | None, timeout: float) -> bool:- """Poll tracked async retain ops until complete or *deadline* (monotonic; None- = unbounded). Ops still pending at the deadline are DROPPED: keeping them+ def _wait_for_server_retain_ops(self, _expired: Callable[[], bool], timeout: float) -> bool:+ """Poll tracked async retain ops until complete or *_expired()* (deadline+ predicate). Ops still pending at the deadline are DROPPED: keeping themwould let a permanently failing status endpoint burn the full timeout onEVERY later prefetch (a per-turn latency penalty via prefetch()'s boundedjoin). Trades a possibly-stale recall for liveness; WARNING once per prefetch."""- def _expired() -> bool:- return deadline is not None and time.monotonic() >= deadline-while True:with self._pending_retain_ops_lock:bank_id = self._retain_ops_bank_id or self._bank_iddiff --git a/plugins/memory/hindsight/config_schema.py b/plugins/memory/hindsight/config_schema.pyindex a6e7ef9ee76..4a2f1dbf242 100644--- a/plugins/memory/hindsight/config_schema.py+++ b/plugins/memory/hindsight/config_schema.py@@ -1,12 +1,7 @@"""Hindsight's declared config surface — rendered by the generic desktop panel."""from plugins.memory.config_schema import (- KIND_SECRET,- KIND_SELECT,- KIND_TEXT,- ProviderConfigSchema,- ProviderField,- ProviderFieldOption,+ KIND_SECRET, KIND_SELECT, KIND_TEXT, ProviderConfigSchema, ProviderField, ProviderFieldOption,)CONFIG_SCHEMA = ProviderConfigSchema(@@ -14,62 +9,27 @@ CONFIG_SCHEMA = ProviderConfigSchema(label="Hindsight",fields=(ProviderField(- key="mode",- label="Mode",- kind=KIND_SELECT,- default="cloud",+ key="mode", label="Mode", kind=KIND_SELECT, default="cloud",description="How Hermes connects to Hindsight.",options=(- ProviderFieldOption(- "cloud",- "Cloud",- "Hindsight Cloud API (lightweight, just needs an API key)",- ),- ProviderFieldOption(- "local_external",- "Local External",- "Connect to an existing Hindsight instance",- ),+ ProviderFieldOption("cloud", "Cloud", "Hindsight Cloud API (lightweight, just needs an API key)"),+ ProviderFieldOption("local_external", "Local External", "Connect to an existing Hindsight instance"),),inline=True,),ProviderField(- key="api_key",- label="API key",- kind=KIND_SECRET,- env_key="HINDSIGHT_API_KEY",+ key="api_key", label="API key", kind=KIND_SECRET, env_key="HINDSIGHT_API_KEY",description="Used to authenticate with the Hindsight API.",- placeholder="Enter Hindsight API key",- inline=True,+ placeholder="Enter Hindsight API key", inline=True,),ProviderField(- key="api_url",- label="API URL",- kind=KIND_TEXT,- default="https://api.hindsight.vectorize.io",- aliases=("apiUrl",),- env_fallbacks=("HINDSIGHT_API_URL",),- inline=True,+ key="api_url", label="API URL", kind=KIND_TEXT, default="https://api.hindsight.vectorize.io",+ aliases=("apiUrl",), env_fallbacks=("HINDSIGHT_API_URL",), inline=True,),+ ProviderField(key="bank_id", label="Bank ID", kind=KIND_TEXT, default="hermes", aliases=("bankId",), inline=True),ProviderField(- key="bank_id",- label="Bank ID",- kind=KIND_TEXT,- default="hermes",- aliases=("bankId",),- inline=True,- ),- ProviderField(- key="recall_budget",- label="Recall budget",- kind=KIND_SELECT,- default="mid",- aliases=("budget",),- options=(- ProviderFieldOption("low", "low"),- ProviderFieldOption("mid", "mid"),- ProviderFieldOption("high", "high"),- ),+ key="recall_budget", label="Recall budget", kind=KIND_SELECT, default="mid", aliases=("budget",),+ options=tuple(ProviderFieldOption(b, b) for b in ("low", "mid", "high")),inline=True,),),diff --git a/plugins/memory/hindsight/embedded.py b/plugins/memory/hindsight/embedded.pyindex f16b1491aca..9e6b291cadf 100644--- a/plugins/memory/hindsight/embedded.py+++ b/plugins/memory/hindsight/embedded.py@@ -3,6 +3,7 @@ file the standalone ``hindsight-embed`` daemon consumes, and the health-grace exfrom __future__ import annotations+import contextlibimport importlibimport loggingimport os@@ -156,9 +157,7 @@ def _materialize_embedded_profile_env(config: dict[str, Any], *, llm_api_key: st_secure_write_profile_env(profile_env, content)_validate_profile_env_permissions(profile_env)except BaseException:- try:+ with contextlib.suppress(OSError):profile_env.unlink()- except OSError:- passraisereturn profile_env