The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(hindsight): inline chmod helper, comprehension-based scope normalizer, class-level poll interval
Teknium070ce2389e2c
diff --git a/plugins/memory/hindsight/__init__.py b/plugins/memory/hindsight/__init__.pyindex d3427b51ef2..bcca91fe9e3 100644--- a/plugins/memory/hindsight/__init__.py+++ b/plugins/memory/hindsight/__init__.py@@ -81,11 +81,12 @@ def _maybe_upgrade_client() -> None:outcome = install_specs([f"hindsight-client>={_MIN_CLIENT_VERSION}"], timeout=120)if outcome.ok:logger.info("hindsight-client upgraded to >=%s", _MIN_CLIENT_VERSION)+ elif outcome.blocked:+ logger.warning("Auto-upgrade unavailable: %s. Run: uv pip install 'hindsight-client>=%s'",+ outcome.reason, _MIN_CLIENT_VERSION)else:- what, why = (("unavailable", outcome.reason) if outcome.blocked- else ("failed", (outcome.stderr or "").strip() or "install error"))- logger.warning("Auto-upgrade %s: %s. Run: uv pip install 'hindsight-client>=%s'",- what, why, _MIN_CLIENT_VERSION)+ logger.warning("Auto-upgrade failed: %s. Run: uv pip install 'hindsight-client>=%s'",+ (outcome.stderr or "").strip() or "install error", _MIN_CLIENT_VERSION)except Exception:pass # packaging not available or other issue — proceed anyway@@ -103,9 +104,7 @@ def _fetch_hindsight_api_version(api_url: str, api_key: str | None = None,if not api_url:return Noneurl = api_url.rstrip("/") + "/version"- req = urllib.request.Request(url)- if api_key:- req.add_header("Authorization", f"Bearer {api_key}")+ req = urllib.request.Request(url, headers={"Authorization": f"Bearer {api_key}"} if api_key else {})try:with urllib.request.urlopen(req, timeout=timeout) as resp: # noqa: S310data = json.loads(resp.read().decode("utf-8", errors="replace"))@@ -296,6 +295,9 @@ _SYSTEM_PROMPT_TAILS = {class HindsightMemoryProvider(MemoryProvider):"""Hindsight long-term memory with knowledge graph and multi-strategy retrieval."""+ # Each server-side op status poll is a round trip — coarser than the 0.05s queue poll.+ _RETAIN_OP_POLL_INTERVAL_S = 0.5+def backup_paths(self) -> List[str]:"""Legacy shared config + embedded-mode profile env files live under ~/.hindsight."""with contextlib.suppress(Exception):@@ -339,8 +341,6 @@ class HindsightMemoryProvider(MemoryProvider):self._pending_retain_ops: set[str] = set()self._pending_retain_ops_lock = threading.Lock()self._retain_ops_bank_id = ""- # Each status poll is a server round trip — coarser than the 0.05s queue poll.- self._RETAIN_OP_POLL_INTERVAL_S = 0.5# The next turn's warm prefetch could read BEFORE an async retain is# recall-visible; when True it first waits (bounded, off the reply path)# for the queue to drain AND the server-side op(s) to complete.@@ -833,7 +833,7 @@ class HindsightMemoryProvider(MemoryProvider):_log(f"\n=== Daemon startup failed: {e} ===\n" + traceback.format_exc())def system_prompt_block(self) -> str:- mode = self._memory_mode if self._memory_mode in ("context", "tools") else "hybrid"+ mode = self._memory_mode if self._memory_mode in _SYSTEM_PROMPT_TAILS else "hybrid"label = "" if mode == "hybrid" else f" ({mode} mode)"return f"# Hindsight Memory\nActive{label}. Bank: {self._bank_id}, budget: {self._budget}.\n{_SYSTEM_PROMPT_TAILS[mode]}"@@ -969,12 +969,8 @@ class HindsightMemoryProvider(MemoryProvider):"timestamp": (occurred_at or "").strip() or _event_timestamp(),}merged_tags = _normalize_retain_tags(list(self._retain_tags) + _normalize_retain_tags(tags))- for key, value in (("context", context), ("update_mode", update_mode)):- if value is not None:- item[key] = value- for key, value in (("tags", merged_tags), ("observation_scopes", self._observation_scopes)):- if value:- item[key] = value+ item.update({k: v for k, v in (("context", context), ("update_mode", update_mode)) if v is not None})+ item.update({k: v for k, v in (("tags", merged_tags), ("observation_scopes", self._observation_scopes)) if v})return itemdef _retain_batch(self, item: dict, *, bank_id: str, document_id: str | None = None,diff --git a/plugins/memory/hindsight/embedded.py b/plugins/memory/hindsight/embedded.pyindex 9e6b291cadf..40d1ec5cc5c 100644--- a/plugins/memory/hindsight/embedded.py+++ b/plugins/memory/hindsight/embedded.py@@ -115,18 +115,12 @@ def _build_embedded_profile_env(config: dict[str, Any], *, llm_api_key: str | Noreturn env_values-def _chmod_owner_only(path: Path) -> None:- try:- os.chmod(path, 0o600)- except OSError:- pass--def _secure_write_profile_env(profile_env: Path, content: str) -> None:"""Create/overwrite *profile_env* owner-only (0600); a pre-existing file istightened BEFORE the plaintext LLM API key is written."""if profile_env.exists():- _chmod_owner_only(profile_env)+ with contextlib.suppress(OSError):+ os.chmod(profile_env, 0o600)fd = os.open(str(profile_env), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)with os.fdopen(fd, "w", encoding="utf-8") as fh:fh.write(content)@@ -139,7 +133,8 @@ def _validate_profile_env_permissions(profile_env: Path) -> None:import statif stat.S_IMODE(profile_env.stat().st_mode) != 0o600:- _chmod_owner_only(profile_env)+ with contextlib.suppress(OSError):+ os.chmod(profile_env, 0o600)if stat.S_IMODE(profile_env.stat().st_mode) != 0o600:raise PermissionError(f"Embedded Hindsight profile environment is not owner-only: {profile_env}"diff --git a/plugins/memory/hindsight/settings.py b/plugins/memory/hindsight/settings.pyindex b1f56318897..752d88f8bba 100644--- a/plugins/memory/hindsight/settings.py+++ b/plugins/memory/hindsight/settings.py@@ -92,20 +92,16 @@ def _normalize_observation_scopes(value: Any) -> Any:except Exception:return Nonereturn None- if isinstance(value, (list, tuple)):- if all(isinstance(entry, str) for entry in value):- inner = [entry.strip() for entry in value if entry.strip()]- return [inner] if inner else None- scopes: list[list[str]] = []- for entry in value:- if isinstance(entry, (list, tuple)):- inner = [str(tag).strip() for tag in entry if str(tag).strip()]- if inner:- scopes.append(inner)- elif isinstance(entry, str) and entry.strip():- scopes.append([entry.strip()])- return scopes or None- return None+ if not isinstance(value, (list, tuple)):+ return None+ if all(isinstance(entry, str) for entry in value): # flat tag list -> one scope+ value = [value]+ scopes = [+ [str(tag).strip() for tag in entry if str(tag).strip()] if isinstance(entry, (list, tuple))+ else [entry.strip()] if isinstance(entry, str) and entry.strip() else []+ for entry in value+ ]+ return [s for s in scopes if s] or Nonedef _sanitize_bank_segment(value: str) -> str: