Skip to content

The agent that grows with you

refactor(langfuse,hindsight,embedded): fold child-ending, content shape descriptor, chmod helper

Tekniumf612374fe97a
diff --git a/plugins/memory/hindsight/__init__.py b/plugins/memory/hindsight/__init__.py
index 6a0d7f84ae7..7d88f54b4a6 100644
--- a/plugins/memory/hindsight/__init__.py
+++ b/plugins/memory/hindsight/__init__.py
@@ -136,8 +136,7 @@ def _fetch_hindsight_api_version(api_url: str, api_key: str | None = None,
return str(version) if version else None
-def _check_api_supports_update_mode_append(api_url: str,
- api_key: str | None = None) -> bool:
+def _check_api_supports_update_mode_append(api_url: str, api_key: str | None = None) -> bool:
"""Cached ``update_mode='append'`` check for *api_url*. False on any probe failure
(safe default: per-process document_id, no update_mode = resume-overwrite fix intact)."""
if not api_url:
@@ -150,19 +149,17 @@ def _check_api_supports_update_mode_append(api_url: str,
with _append_capability_lock:
# A concurrent probe may have filled the cache meanwhile; its answer wins.
supported = _append_capability_cache.setdefault(api_url, supported)
- if not supported:
+ if supported:
+ logger.debug("Hindsight API %s version %s supports update_mode='append'", api_url, version)
+ else:
logger.warning(
"Hindsight API at %s reports version %r, older than %s. "
"Falling back to per-process document_id — retains across "
"processes/sessions create separate documents instead of "
"appending to a session-scoped one. Upgrade Hindsight to "
"%s+ to enable update_mode='append' deduplication.",
- api_url, version, _MIN_VERSION_FOR_UPDATE_MODE_APPEND,
- _MIN_VERSION_FOR_UPDATE_MODE_APPEND,
+ api_url, version, _MIN_VERSION_FOR_UPDATE_MODE_APPEND, _MIN_VERSION_FOR_UPDATE_MODE_APPEND,
)
- else:
- logger.debug("Hindsight API %s version %s supports update_mode='append'",
- api_url, version)
return supported
@@ -970,8 +967,7 @@ class HindsightMemoryProvider(MemoryProvider):
def _finish_prefetch(self, result: str, count: int) -> str:
"""Record indicator state (cleared on empty turns, never a stale count); format the block."""
- self._last_recall_returned = bool(result)
- self._last_recall_count = count if result else 0
+ self._last_recall_returned, self._last_recall_count = bool(result), count if result else 0
if not result:
logger.debug("Prefetch: no results available")
return ""
@@ -1111,8 +1107,7 @@ class HindsightMemoryProvider(MemoryProvider):
self._session_id = str(session_id).strip()
self._session_turns.append(json.dumps(self._build_turn_messages(user_content, assistant_content), ensure_ascii=False))
- self._turn_counter += 1
- self._turn_index = self._turn_counter
+ self._turn_counter = self._turn_index = self._turn_counter + 1
remainder = self._turn_counter % self._retain_every_n_turns
if remainder:
logger.debug("sync_turn: buffered turn %d (will retain at turn %d)",
diff --git a/plugins/memory/hindsight/embedded.py b/plugins/memory/hindsight/embedded.py
index 92ad23e799a..94fc175bcf4 100644
--- a/plugins/memory/hindsight/embedded.py
+++ b/plugins/memory/hindsight/embedded.py
@@ -65,8 +65,7 @@ def _local_runtime_hint(reason: str | None) -> str:
declares only ``hindsight-client``, so a hand-written config, the legacy
``"mode": "local"`` alias or a restored backup hits ``No module named 'hindsight'``."""
text = (reason or "").lower()
- if "no module named" in text and ("hindsight'" in text or 'hindsight"' in text
- or "hindsight_embed" in text):
+ if "no module named" in text and any(m in text for m in ("hindsight'", 'hindsight"', "hindsight_embed")):
return (
f" Install the embedded runtime with: uv pip install --python "
f"{sys.executable} hindsight-all — or run 'hermes memory setup'. "
@@ -113,14 +112,18 @@ def _build_embedded_profile_env(config: dict[str, Any], *, llm_api_key: str | No
return 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 is
tightened BEFORE the plaintext LLM API key is written."""
if profile_env.exists():
- try:
- os.chmod(profile_env, 0o600)
- except OSError:
- pass
+ _chmod_owner_only(profile_env)
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)
@@ -132,12 +135,8 @@ def _validate_profile_env_permissions(profile_env: Path) -> None:
return
import stat
- mode = stat.S_IMODE(profile_env.stat().st_mode)
- if mode != 0o600:
- try:
- os.chmod(profile_env, 0o600)
- except OSError:
- pass
+ if stat.S_IMODE(profile_env.stat().st_mode) != 0o600:
+ _chmod_owner_only(profile_env)
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/observability/langfuse/__init__.py b/plugins/observability/langfuse/__init__.py
index 977fac1f64f..3da9f0bd27e 100644
--- a/plugins/observability/langfuse/__init__.py
+++ b/plugins/observability/langfuse/__init__.py
@@ -127,16 +127,18 @@ def _describe_content(value: Any) -> Any:
if value is None or isinstance(value, bool):
return value
if isinstance(value, (int, float)):
- return {"omitted": True, "type": "number"}
- if isinstance(value, bytes):
- return {"omitted": True, "type": "bytes", "length": len(value)}
- if isinstance(value, str):
- return {"omitted": True, "type": "text", "chars": len(value)}
- if isinstance(value, dict):
- return {"omitted": True, "type": "object", "keys": [str(k) for k in list(value.keys())[:20]]}
- if isinstance(value, (list, tuple, set)):
- return {"omitted": True, "type": "array", "items": len(value)}
- return {"omitted": True, "type": type(value).__name__}
+ shape = {"type": "number"}
+ elif isinstance(value, bytes):
+ shape = {"type": "bytes", "length": len(value)}
+ elif isinstance(value, str):
+ shape = {"type": "text", "chars": len(value)}
+ elif isinstance(value, dict):
+ shape = {"type": "object", "keys": [str(k) for k in list(value.keys())[:20]]}
+ elif isinstance(value, (list, tuple, set)):
+ shape = {"type": "array", "items": len(value)}
+ else:
+ shape = {"type": type(value).__name__}
+ return {"omitted": True, **shape}
def _capture_content(value: Any, *, parse_json_strings: bool = False) -> Any:
@@ -639,14 +641,10 @@ def _end_observation(observation: Any, *, output: Any = None, metadata: Optional
def _end_children(state: TraceState, *, include_subagents: bool = False) -> None:
- for observation in (*state.generations.values(), *state.tools.values()):
+ pending = [obs for queue in state.pending_tools_by_name.values() for obs in queue]
+ subagents = state.subagents.values() if include_subagents else ()
+ for observation in (*state.generations.values(), *state.tools.values(), *pending, *subagents):
_end_observation(observation)
- for queue in state.pending_tools_by_name.values():
- for observation in queue:
- _end_observation(observation)
- if include_subagents:
- for observation in state.subagents.values():
- _end_observation(observation)
def _exit_root_ctx(state: TraceState) -> None: