From 4cbd4d62cd0cc62924071444b33bf9ec26b38ee3 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:16:38 +0800 Subject: [PATCH] feat(steward): add isolated OpenViking long-term memory with per-speaker boundaries Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../capability-localization.ts | 19 + .../machine-configuration-settings.tsx | 13 +- .../personal-workspace-contract.test.mjs | 3 +- loopx/capabilities/configuration_ui.py | 46 ++ .../machine_configuration/builtins.py | 5 + .../capabilities/native_chat/codex_context.py | 4 +- .../native_chat/external_conversations.py | 4 +- loopx/capabilities/steward_memory/__init__.py | 33 + .../steward_memory/machine_defaults.py | 103 +++ loopx/capabilities/steward_memory/runtime.py | 753 ++++++++++++++++++ loopx/chat.py | 31 + loopx/chat/coordination.py | 5 + loopx/chat_runtime.py | 8 + .../collaboration/steward_memory.ts | 268 +++++++ .../control_plane/effect_runtime_handlers.ts | 2 + .../test_capability_configuration_ui.py | 3 +- .../test_machine_configuration_contract.py | 2 + tests/control_plane_ts/steward_memory.test.ts | 155 ++++ tests/test_chat_machine_configuration_api.py | 23 +- tests/test_steward_openviking_memory.py | 306 +++++++ 20 files changed, 1777 insertions(+), 9 deletions(-) create mode 100644 loopx/capabilities/steward_memory/__init__.py create mode 100644 loopx/capabilities/steward_memory/machine_defaults.py create mode 100644 loopx/capabilities/steward_memory/runtime.py create mode 100644 loopx/control_plane/collaboration/steward_memory.ts create mode 100644 tests/control_plane_ts/steward_memory.test.ts create mode 100644 tests/test_steward_openviking_memory.py diff --git a/apps/presentation/dashboard/src/features/personal-workspace/capability-localization.ts b/apps/presentation/dashboard/src/features/personal-workspace/capability-localization.ts index bb4d4f76dc..3e21cefefd 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/capability-localization.ts +++ b/apps/presentation/dashboard/src/features/personal-workspace/capability-localization.ts @@ -25,6 +25,10 @@ const capabilityCopy: Record> = { displayName: "Model and executor", description: "Guides the steward executor, model, and selection boundary for this machine. A pinned route blocks substitution; a flexible pool permits only authorized fallback.", }, + steward_memory: { + displayName: "Isolated OpenViking memory", + description: "Connects owner private steward conversations to an isolated OpenViking memory namespace with per-speaker preferences and bounded recall.", + }, todo_replan_cadence: { displayName: "Goal review and stride", description: "Set review frequency and optionally choose a versioned stride strategy for work scope and route changes." }, change_quality_qualification: { displayName: "Change quality qualification", @@ -95,6 +99,10 @@ const capabilityCopy: Record> = { displayName: "模型与执行器", description: "配置本机管家的执行器、模型与选择边界;锁定路径禁止替代,灵活池只允许在已授权范围内回退。", }, + steward_memory: { + displayName: "独立 OpenViking 长期记忆", + description: "为本人私聊管家启用独立身份的 OpenViking 记忆命名空间:按已验证发言人持久化偏好、跨会话按需检索并限制超时,不向群聊或工作 Agent 泄露。", + }, todo_replan_cadence: { displayName: "Goal 复盘与步幅", description: "设置复盘周期,可选用带版本的步幅策略,调节工作大小与路线变化。" }, change_quality_qualification: { displayName: "变更质量验证", @@ -164,6 +172,9 @@ const fieldCopy: Record = { eligible_endpoints: { label: "Flexible eligible executors", description: "One authorized executor per line. Use only with flexible selection and include the primary executor." }, executor_model: { label: "Model", description: "Optional model for the selected executor. Leave blank to keep the executor's own default." }, executor_reasoning_effort: { label: "Reasoning effort", description: "Optional reasoning effort for the selected executor. Leave blank to keep the executor's own default." }, + user_space: { label: "User space", description: "OpenViking user space isolating steward memory on this machine." }, + auto_recall: { label: "Auto recall history", description: "Search past steward events automatically on substantive private messages." }, + recall_timeout_seconds: { label: "Recall timeout (seconds)", description: "Wait budget (1–15s) before continuing without recalled memory." }, count_unit: { label: "Review after", description: "Work Turns require accepted settlement; polls and retries do not count.", options: { completed_todos: "Completed Todos", effective_turns: "Settled work Turns" } }, planning_horizon_goal: { label: "Show Goal in planning horizon", description: "Default on. Keep the original Goal and acceptance visible before choosing work. Turn off for rollback or comparisons; replan, stride and execution authority are unchanged." }, yardstick_enabled: { label: "Review measurement criteria", description: "Default off. Review measurement scope and feedback using available Explore evidence. Requires Explore evidence; does not enable it or authorize execution." }, @@ -202,6 +213,9 @@ const fieldCopy: Record = { eligible_endpoints: { label: "灵活池可用执行器", description: "每行一个已授权执行器,仅用于 flexible;必须包含首选执行器。" }, executor_model: { label: "模型", description: "所选执行器使用的模型,可留空;留空表示沿用执行器自身的默认模型。" }, executor_reasoning_effort: { label: "推理档位", description: "所选执行器使用的推理档位,可留空;留空表示沿用执行器自身的默认档位。" }, + user_space: { label: "记忆用户空间", description: "本机管家在 OpenViking 中隔离使用的 user space 标识。" }, + auto_recall: { label: "跨会话自动召回", description: "收到非命令的实质私聊消息时,执行一次有界历史事件检索。" }, + recall_timeout_seconds: { label: "召回超时(秒)", description: "等待记忆读取的上限(1–15 秒);超时后跳过记忆直接恢复正常回复。" }, count_unit: { label: "复核计数依据", description: "有效工作 Turn 须完成结算;轮询和重复重试不计数。", options: { completed_todos: "已完成 Todo", effective_turns: "已结算工作 Turn" } }, planning_horizon_goal: { label: "在规划视野中显示 Goal", description: "默认开启,选择工作前可见原目标与验收要求。可为回滚或对照实验关闭;不改变复盘、步幅策略或执行权限。" }, yardstick_enabled: { label: "复核衡量标准", description: "默认关闭。使用已有 Explore 证据复核测量范围与反馈;需有可用的 Explore 证据,不会自动启用它或授予执行权限。" }, @@ -280,6 +294,11 @@ export function localizedCapabilityFieldCopy(locale: WorkspaceLocale, capability label: "Exploration mode", description: "Planning includes the evidence graph; worker and execution permissions remain separate.", options: {off: "Off", evidence: "Evidence only", planning: "Evidence and planning"}, }}; + if (capabilityId === "steward_memory") return {...fieldCopy[locale], enabled: locale === "zh-CN" ? { + label: "启用", description: "为本人私聊管家开启独立 OpenViking 长期记忆。", + } : { + label: "Enabled", description: "Enable isolated OpenViking long-term memory for owner private steward conversations.", + }}; return fieldCopy[locale]; } diff --git a/apps/presentation/dashboard/src/features/personal-workspace/machine-configuration-settings.tsx b/apps/presentation/dashboard/src/features/personal-workspace/machine-configuration-settings.tsx index 20730ae91d..0d8c7d1042 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/machine-configuration-settings.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/machine-configuration-settings.tsx @@ -135,8 +135,8 @@ export function MachineConfigurationSettings({ section, onChanged }: { (inspection?.capability_catalog.capabilities ?? []).filter((capability) => capability.available_scopes.includes("machine") && (section === "conversation" ? capability.capability_id === "codex_chat" : section === "steward" - ? capability.capability_id === "steward_executor" || capability.capability_id === "manager_runtime" - : capability.capability_id !== "steward_executor" && capability.capability_id !== "manager_runtime" && capability.capability_id !== "codex_chat")), + ? capability.capability_id === "steward_executor" || capability.capability_id === "manager_runtime" || capability.capability_id === "steward_memory" + : capability.capability_id !== "steward_executor" && capability.capability_id !== "manager_runtime" && capability.capability_id !== "steward_memory" && capability.capability_id !== "codex_chat")), locale, ), [inspection, locale, section]); const invalidNamespace = inspection?.invalid_namespaces[0]; @@ -526,6 +526,15 @@ export function MachineConfigurationSettings({ section, onChanged }: { ) : null} + {selected.capability_id === "steward_memory" ? ( +
+ +
{locale === "zh-CN" ? "仅限本人私聊与独立身份" : "Owner private chats only"}

{locale === "zh-CN" + ? "连接信息读取自本机管家私有文件,不在此表单收集;记忆按已验证发言人隔离保存,超时不阻塞正常回复,也不会进入群聊或转交给工作 Agent。" + : "Connection settings stay in the private local steward file outside this form. Memory stays isolated per verified speaker, fails open on timeout, and never enters group chats or worker handoffs."}

+
+ ) : null} + {selected.capability_id === "pull_request_review" ? (
diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs index 0fdc7b1162..cf2ed749d0 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs @@ -502,11 +502,12 @@ for (const capabilityId of [ "pull_request_review", "reward_memory", "steward_executor", + "steward_memory", ]) { const matches = capabilityLocalization.match(new RegExp(`${capabilityId}:`, "g")) ?? []; assert.equal(matches.length, 2, `${capabilityId} has English and Simplified Chinese metadata`); } -for (const fieldKey of ["agent_orders", "allowed_domains", "coordinator_agent_id", "eligible_endpoints", "enabled", "executor_endpoint", "executor_model", "executor_reasoning_effort", "max_children", "profile", "profile_preset", "review_order", "route_ref", "safe_fix", "selection_policy", "strict_receipt", "timezone"]) { +for (const fieldKey of ["agent_orders", "allowed_domains", "auto_recall", "coordinator_agent_id", "eligible_endpoints", "enabled", "executor_endpoint", "executor_model", "executor_reasoning_effort", "max_children", "profile", "profile_preset", "recall_timeout_seconds", "review_order", "route_ref", "safe_fix", "selection_policy", "strict_receipt", "timezone", "user_space"]) { const matches = capabilityLocalization.match(new RegExp(`^\\s+${fieldKey}:`, "gm")) ?? []; assert.equal(matches.length, 2, `${fieldKey} has English and Simplified Chinese field copy`); } diff --git a/loopx/capabilities/configuration_ui.py b/loopx/capabilities/configuration_ui.py index e991c2d0e9..d6803d4cb0 100644 --- a/loopx/capabilities/configuration_ui.py +++ b/loopx/capabilities/configuration_ui.py @@ -268,6 +268,52 @@ def capability_configuration_editor( ), ], }, + "steward_memory": { + "supported_scopes": ["machine"], + "writable_scopes": ["machine"], + "fields": [ + _field( + "enabled", + "Enabled", + "boolean", + required=True, + description=( + "Enable isolated OpenViking long-term memory for owner " + "private steward conversations." + ), + ), + _field( + "user_space", + "User space", + "text", + required=True, + description=( + "OpenViking user space isolating steward memory on this machine." + ), + ), + _field( + "auto_recall", + "Auto recall history", + "boolean", + required=True, + description=( + "Run one bounded search across past steward exchanges for " + "substantive private messages." + ), + ), + _field( + "recall_timeout_seconds", + "Recall timeout (seconds)", + "number", + minimum=1, + maximum=15, + required=True, + description=( + "Maximum wait before continuing normal replies without memory recall." + ), + ), + ], + }, "multi_subagent": { "supported_scopes": ["goal"], "writable_scopes": ["goal"], diff --git a/loopx/capabilities/machine_configuration/builtins.py b/loopx/capabilities/machine_configuration/builtins.py index 8e26aab087..1805619573 100644 --- a/loopx/capabilities/machine_configuration/builtins.py +++ b/loopx/capabilities/machine_configuration/builtins.py @@ -61,6 +61,11 @@ "loopx.capabilities.steward_executor.machine_defaults", "steward_executor_machine_configuration_namespace", ), + ( + "steward_memory", + "loopx.capabilities.steward_memory.machine_defaults", + "steward_memory_machine_configuration_namespace", + ), ) diff --git a/loopx/capabilities/native_chat/codex_context.py b/loopx/capabilities/native_chat/codex_context.py index 62f316f1f5..572bc70f7f 100644 --- a/loopx/capabilities/native_chat/codex_context.py +++ b/loopx/capabilities/native_chat/codex_context.py @@ -99,8 +99,10 @@ def require_session_home(base_home: Path, session: Mapping[str, Any]) -> None: def process_environment(home: Path, *, isolated: bool) -> dict[str, str]: + from ..steward_memory.runtime import scrub_openviking_environment + if not isolated: - return {**os.environ, "CODEX_HOME": str(home)} + return {**scrub_openviking_environment(os.environ), "CODEX_HOME": str(home)} host_home = home.parent host_home.mkdir(parents=True, exist_ok=True, mode=0o700) home.mkdir(exist_ok=True, mode=0o700) diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py index d7d351ebb1..7c3866f765 100644 --- a/loopx/capabilities/native_chat/external_conversations.py +++ b/loopx/capabilities/native_chat/external_conversations.py @@ -1144,7 +1144,7 @@ def execute_turn(controller: Any, adapter: Any, session: dict[str, Any], turn: d response = adapter.start_turn(text, lambda kind, payload: event_sink(kind, payload) if kind not in {"answer.delta", "answer.final"} else None) external._checked_answer_review(session, turn) - if any(response.get(key) for key in ("proposals", "context_handoff", "protected_action", "goal_draft", "unfinished_work")): + if any(response.get(key) for key in ("proposals", "context_handoff", "protected_action", "goal_draft", "unfinished_work", "memory_preferences")): raise ValueError("an answer review cannot admit actions or business continuation") if answer_review.get("context_correction") and response.get("gate") is None: response = {**response, "gate": {"kind": "evidence_gap", @@ -1186,7 +1186,7 @@ def validate_metadata_review_response(self, session: dict[str, Any], turn: dict[ handoffs = response.get("context_handoff") expected = continuation["metadata_handoffs"] if (not isinstance(handoffs, list) or len(handoffs) != len(expected) - or any(response.get(key) for key in ("gate", "proposals", "protected_action", "goal_draft", "unfinished_work")) + or any(response.get(key) for key in ("gate", "proposals", "protected_action", "goal_draft", "unfinished_work", "memory_preferences")) or any(set(item) != {"goal_id", "agent_id", "brief"} or any(item.get(key) != original[key] for key in ("goal_id", "agent_id")) or item["brief"].get("context") != original["brief"] diff --git a/loopx/capabilities/steward_memory/__init__.py b/loopx/capabilities/steward_memory/__init__.py new file mode 100644 index 0000000000..1d5e43afca --- /dev/null +++ b/loopx/capabilities/steward_memory/__init__.py @@ -0,0 +1,33 @@ +"""Isolated OpenViking long-term memory capability for bound owner steward chats.""" + +from .machine_defaults import ( + DEFAULT_RECALL_TIMEOUT_SECONDS, + DEFAULT_USER_SPACE, + STEWARD_MEMORY_NAMESPACE, + STEWARD_MEMORY_SCHEMA, + default_steward_memory_configuration, + load_effective_steward_memory_setting, + normalize_steward_memory_configuration, + steward_memory_machine_configuration_namespace, +) +from .runtime import ( + clear_steward_memory_cache, + commit_steward_memory_after_turn, + recall_steward_memory_for_turn, + scrub_openviking_environment, +) + +__all__ = [ + "DEFAULT_RECALL_TIMEOUT_SECONDS", + "DEFAULT_USER_SPACE", + "STEWARD_MEMORY_NAMESPACE", + "STEWARD_MEMORY_SCHEMA", + "clear_steward_memory_cache", + "commit_steward_memory_after_turn", + "default_steward_memory_configuration", + "load_effective_steward_memory_setting", + "normalize_steward_memory_configuration", + "recall_steward_memory_for_turn", + "scrub_openviking_environment", + "steward_memory_machine_configuration_namespace", +] diff --git a/loopx/capabilities/steward_memory/machine_defaults.py b/loopx/capabilities/steward_memory/machine_defaults.py new file mode 100644 index 0000000000..ef01684a15 --- /dev/null +++ b/loopx/capabilities/steward_memory/machine_defaults.py @@ -0,0 +1,103 @@ +"""Machine configuration defaults and runtime owner for isolated steward memory.""" +from __future__ import annotations + +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +from ..machine_configuration.contract import MachineConfigurationNamespace + +STEWARD_MEMORY_NAMESPACE = "steward_memory" +STEWARD_MEMORY_SCHEMA = "steward_memory_machine_defaults_v0" +DEFAULT_USER_SPACE = "steward" +DEFAULT_RECALL_TIMEOUT_SECONDS = 3 +MIN_RECALL_TIMEOUT_SECONDS = 1 +MAX_RECALL_TIMEOUT_SECONDS = 15 + + +def default_steward_memory_configuration() -> dict[str, Any]: + """Return the default-off machine configuration for isolated steward memory.""" + return { + "schema_version": STEWARD_MEMORY_SCHEMA, + "enabled": False, + "user_space": DEFAULT_USER_SPACE, + "auto_recall": True, + "recall_timeout_seconds": DEFAULT_RECALL_TIMEOUT_SECONDS, + } + + +def normalize_steward_memory_configuration(raw: Mapping[str, Any]) -> dict[str, Any]: + """Validate and normalize machine-level steward memory defaults.""" + from ..context_providers.openviking_scope import OPENVIKING_IDENTITY_RE + + allowed = { + "schema_version", + "enabled", + "user_space", + "auto_recall", + "recall_timeout_seconds", + } + unknown = sorted(set(raw) - allowed) + if unknown: + raise ValueError( + "steward_memory contains unsupported fields: " + ", ".join(unknown) + ) + if raw.get("schema_version") != STEWARD_MEMORY_SCHEMA: + raise ValueError(f"steward_memory must use {STEWARD_MEMORY_SCHEMA}") + if type(raw.get("enabled")) is not bool: + raise ValueError("steward_memory.enabled must be a boolean") + user_space = str(raw.get("user_space") or "").strip() + if not OPENVIKING_IDENTITY_RE.fullmatch(user_space): + raise ValueError("steward_memory.user_space must use a safe identifier") + if type(raw.get("auto_recall")) is not bool: + raise ValueError("steward_memory.auto_recall must be a boolean") + timeout = raw.get("recall_timeout_seconds") + if ( + type(timeout) is not int + or not MIN_RECALL_TIMEOUT_SECONDS <= timeout <= MAX_RECALL_TIMEOUT_SECONDS + ): + raise ValueError( + "steward_memory.recall_timeout_seconds must be an integer between 1 and 15" + ) + return { + "schema_version": STEWARD_MEMORY_SCHEMA, + "enabled": raw["enabled"], + "user_space": user_space, + "auto_recall": raw["auto_recall"], + "recall_timeout_seconds": timeout, + } + + +def steward_memory_machine_configuration_namespace() -> MachineConfigurationNamespace: + """Describe the steward_memory machine configuration owner.""" + return MachineConfigurationNamespace( + namespace=STEWARD_MEMORY_NAMESPACE, + schema_versions=frozenset({STEWARD_MEMORY_SCHEMA}), + normalize=normalize_steward_memory_configuration, + project_public=lambda value: dict(value), + apply_public_update=lambda _current, update: dict(update), + title="Steward OpenViking memory", + description=( + "Enables an isolated OpenViking memory namespace for owner private steward " + "conversations with per-speaker preferences, bounded recall timeouts, and " + "strict non-leakage to group chats or worker Agents." + ), + default_configuration=default_steward_memory_configuration(), + ) + + +def load_effective_steward_memory_setting(runtime_root: Path) -> dict[str, Any]: + """Read normalized steward_memory machine configuration or return default-off.""" + from ..machine_configuration.builtins import build_builtin_machine_configuration_registry + from ..machine_configuration.store import read_machine_configuration + + registry = build_builtin_machine_configuration_registry() + configuration = read_machine_configuration( + runtime_root, + registry=registry, + namespace_ids=(STEWARD_MEMORY_NAMESPACE,), + ) + stored = (configuration or {}).get("namespaces", {}).get(STEWARD_MEMORY_NAMESPACE) + if isinstance(stored, Mapping): + return normalize_steward_memory_configuration(stored) + return default_steward_memory_configuration() diff --git a/loopx/capabilities/steward_memory/runtime.py b/loopx/capabilities/steward_memory/runtime.py new file mode 100644 index 0000000000..3d9f1ce556 --- /dev/null +++ b/loopx/capabilities/steward_memory/runtime.py @@ -0,0 +1,753 @@ +"""Host-owned isolated OpenViking recall and persistence for owner steward chats.""" +from __future__ import annotations + +import json +import os +import re +import shutil +import subprocess +import tempfile +import threading +import time +from collections.abc import Callable, Mapping, Sequence +from contextlib import contextmanager +from datetime import datetime, timezone +from pathlib import Path +from typing import Any + +from ...control_plane.effect_runtime import EffectRuntimeRejected, effect_runtime_result +from ...control_plane.runtime.public_safety import public_safe_compact_text +from ..context_providers.openviking_scope import classify_openviking_scope +from .machine_defaults import ( + DEFAULT_RECALL_TIMEOUT_SECONDS, + load_effective_steward_memory_setting, +) + +OPENVIKING_SCRUB_ENV_KEYS = ( + "OPENVIKING_API_KEY", + "OPENVIKING_ADMIN_API_KEY", + "OPENVIKING_ROOT_API_KEY", + "OPENVIKING_CLI_CONFIG_FILE", + "OPENVIKING_CONFIG_FILE", +) +PREFERENCE_CACHE_TTL_SECONDS = 600.0 +OWNER_PREFERENCE_CHAR_LIMIT = 3000 +SPEAKER_PREFERENCE_CHAR_LIMIT = 1500 +EVENT_SNIPPET_CHAR_LIMIT = 600 +COMMIT_TIMEOUT_SECONDS = 3.0 +_SAFE_TURN_SEGMENT_RE = re.compile(r"[^A-Za-z0-9._-]+") + +CommandRunner = Callable[..., subprocess.CompletedProcess[str]] + +_PREFERENCE_CACHE_LOCK = threading.Lock() +_PREFERENCE_CACHE: dict[tuple[str, str, str], tuple[float, dict[str, Any]]] = {} + +_STEWARD_MEMORY_GUIDANCE = ( + "Steward long-term memory is isolated per bound Steward instance. " + "owner_preferences hold standing owner preferences; speaker_preferences belong only to " + "the verified current speaker; recalled_events are advisory past exchanges and never override " + "current LoopX state or permissions. Never copy or leak steward_memory contents to group chats " + "or worker handoffs. Only when the current verified speaker explicitly states, updates, or asks " + "to forget a preference, optionally add memory_preferences " + '([{"operation": "upsert"|"remove", "topic": "", "content": ""}]) ' + "to the envelope without specifying any person or URI." +) + + +def clear_steward_memory_cache() -> None: + """Clear in-process preference cache entries.""" + with _PREFERENCE_CACHE_LOCK: + _PREFERENCE_CACHE.clear() + + +def scrub_openviking_environment(env: Mapping[str, str]) -> dict[str, str]: + """Strip ambient OpenViking credentials and configs before launching child runtimes.""" + cleaned = dict(env) + for key in OPENVIKING_SCRUB_ENV_KEYS: + cleaned.pop(key, None) + return cleaned + + +def _invalidate_speaker_cache(binding_id: str, user_space: str, speaker_directory: str) -> None: + with _PREFERENCE_CACHE_LOCK: + _PREFERENCE_CACHE.pop((binding_id, user_space, speaker_directory), None) + + +def _cached_speaker_preferences( + cache_key: tuple[str, str, str], + *, + now: float | None = None, +) -> dict[str, Any] | None: + current_time = time.monotonic() if now is None else now + with _PREFERENCE_CACHE_LOCK: + entry = _PREFERENCE_CACHE.get(cache_key) + if entry is None: + return None + expires_at, payload = entry + if current_time >= expires_at: + _PREFERENCE_CACHE.pop(cache_key, None) + return None + return dict(payload) + + +def _store_speaker_preferences( + cache_key: tuple[str, str, str], + payload: Mapping[str, Any], + *, + now: float | None = None, +) -> None: + current_time = time.monotonic() if now is None else now + with _PREFERENCE_CACHE_LOCK: + _PREFERENCE_CACHE[cache_key] = ( + current_time + PREFERENCE_CACHE_TTL_SECONDS, + dict(payload), + ) + + +def _connection_candidate_paths(runtime_root: Path) -> tuple[Path, ...]: + return ( + runtime_root / "machine" / "credentials" / "steward-openviking.json", + runtime_root / "memory" / "steward" / "ovcli.json", + ) + + +def _load_connection_profile(runtime_root: Path) -> dict[str, Any] | None: + for path in _connection_candidate_paths(runtime_root): + if path.is_symlink() or not path.is_file(): + continue + try: + raw = json.loads(path.read_text(encoding="utf-8")) + except (OSError, UnicodeDecodeError, ValueError): + continue + if not isinstance(raw, Mapping): + continue + service_url = str(raw.get("service_url") or raw.get("url") or "").strip() + api_key = str(raw.get("api_key") or "").strip() + if not service_url or not api_key: + continue + ov_bin = str(raw.get("ov_bin") or "").strip() or shutil.which("ov") or "" + if not ov_bin: + return None + pass_actor = raw.get("pass_actor_peer_id") + if type(pass_actor) is not bool: + pass_actor = True + return { + "service_url": service_url, + "api_key": api_key, + "account_id": str(raw.get("account_id") or raw.get("account") or "").strip() or None, + "user_id": str(raw.get("user_id") or raw.get("user") or "").strip() or None, + "ov_bin": ov_bin, + "pass_actor_peer_id": pass_actor, + } + return None + + +@contextmanager +def _ephemeral_ov_env( + profile: Mapping[str, Any], + *, + timeout_seconds: float, + scratch_root: Path | None = None, +): + base_dir = scratch_root if scratch_root and scratch_root.is_dir() else None + config = { + "url": profile["service_url"], + "api_key": profile["api_key"], + "timeout": max(2, int(timeout_seconds)), + "output": "json", + "verbose": False, + "echo_command": False, + } + with tempfile.TemporaryDirectory(prefix="steward-ov-", dir=base_dir) as tmp: + conf_path = Path(tmp) / "ovcli.conf" + fd = os.open(conf_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, "w", encoding="utf-8") as stream: + json.dump(config, stream) + env = scrub_openviking_environment(os.environ) + for key in list(env): + if key.lower() in {"http_proxy", "https_proxy", "all_proxy"}: + env.pop(key) + env["NO_PROXY"] = "127.0.0.1,localhost" + env["OPENVIKING_CLI_CONFIG_FILE"] = str(conf_path) + yield env, Path(tmp) + + +def _extract_json(text: str) -> Any: + stripped = text.strip() + if not stripped: + raise ValueError("empty output") + try: + return json.loads(stripped) + except json.JSONDecodeError: + pass + decoder = json.JSONDecoder() + for index, char in enumerate(stripped): + if char not in "[{": + continue + try: + value, _end = decoder.raw_decode(stripped[index:]) + except json.JSONDecodeError: + continue + return value + raise ValueError("no JSON payload") + + +def _extract_entries(payload: Any) -> list[Mapping[str, Any]]: + target = payload.get("result") if isinstance(payload, Mapping) and "result" in payload else payload + if isinstance(target, list): + return [row for row in target if isinstance(row, Mapping)] + if isinstance(target, Mapping): + for key in ("items", "entries", "memories", "resources", "results"): + rows = target.get(key) + if isinstance(rows, list): + return [row for row in rows if isinstance(row, Mapping)] + return [] + + +def _extract_content(payload: Any) -> str: + target = payload.get("result") if isinstance(payload, Mapping) and "result" in payload else payload + if isinstance(target, str): + return target + if isinstance(target, Mapping): + for key in ("content", "text"): + val = target.get(key) + if isinstance(val, str): + return val + return "" + + +def _run_ov_command( + profile: Mapping[str, Any], + args: Sequence[str], + *, + actor_peer_id: str, + env: Mapping[str, str], + deadline: float, + runner: CommandRunner, +) -> subprocess.CompletedProcess[str]: + remaining = deadline - time.monotonic() + if remaining <= 0.05: + raise subprocess.TimeoutExpired([str(profile["ov_bin"]), *args], remaining) + command = [str(profile["ov_bin"])] + if profile.get("pass_actor_peer_id", True): + command.extend(["--actor-peer-id", actor_peer_id]) + if profile.get("account_id"): + command.extend(["--account", str(profile["account_id"])]) + if profile.get("user_id"): + command.extend(["--user", str(profile["user_id"])]) + command.extend(args) + return runner( + command, + text=True, + encoding="utf-8", + errors="replace", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + timeout=remaining, + env=dict(env), + check=False, + ) + + +def _validate_peer_scope(uri: str, actor_peer_id: str) -> None: + scope = classify_openviking_scope(uri) + if scope.actor_scope_id != actor_peer_id: + raise ValueError("OpenViking URI does not match isolated Steward peer") + + +def _read_preferences_directory( + profile: Mapping[str, Any], + *, + preferences_uri: str, + actor_peer_id: str, + char_limit: int, + env: Mapping[str, str], + deadline: float, + runner: CommandRunner, +) -> dict[str, Any]: + _validate_peer_scope(preferences_uri, actor_peer_id) + ls_res = _run_ov_command( + profile, + ["ls", preferences_uri, "-n", "100", "-o", "json", "-c", "true"], + actor_peer_id=actor_peer_id, + env=env, + deadline=deadline, + runner=runner, + ) + if ls_res.returncode != 0: + if "NOT_FOUND" in ls_res.stdout or "404" in ls_res.stdout: + return {"items": [], "omitted_topics": []} + raise RuntimeError("preference_ls_failed") + try: + entries = _extract_entries(_extract_json(ls_res.stdout)) + except ValueError: + return {"items": [], "omitted_topics": []} + + md_entries = [] + for entry in entries: + if entry.get("isDir") is True or entry.get("type") == "dir": + continue + uri = str(entry.get("uri") or "").strip() + name = uri.rstrip("/").rsplit("/", 1)[-1] if uri else str(entry.get("name") or "") + if not name.endswith(".md") or name.startswith("."): + continue + target_uri = uri if uri.startswith("viking://") else f"{preferences_uri.rstrip('/')}/{name}" + if not target_uri.startswith(f"{preferences_uri.rstrip('/')}/"): + continue + md_entries.append((str(entry.get("mtime") or ""), name, target_uri)) + + md_entries.sort(key=lambda row: (row[0], row[1]), reverse=True) + items: list[dict[str, str]] = [] + omitted_topics: list[str] = [] + used_chars = 0 + for _mtime, name, target_uri in md_entries: + topic = name[:-3] + if used_chars >= char_limit: + omitted_topics.append(topic) + continue + read_res = _run_ov_command( + profile, + ["read", target_uri, "-o", "json", "-c", "true"], + actor_peer_id=actor_peer_id, + env=env, + deadline=deadline, + runner=runner, + ) + if read_res.returncode != 0: + continue + try: + content = _extract_content(_extract_json(read_res.stdout)).strip() + except ValueError: + continue + if not content: + continue + remaining_budget = char_limit - used_chars + if len(content) > remaining_budget and items: + omitted_topics.append(topic) + continue + bounded = content[:remaining_budget] + used_chars += len(bounded) + items.append({"topic": topic, "topic_file": name, "content": bounded}) + return {"items": items, "omitted_topics": omitted_topics} + + +def _is_substantive_message(message: str) -> bool: + text = str(message or "").strip() + if len(text) <= 3: + return False + if text.startswith("/"): + return False + return True + + +def _recall_events( + profile: Mapping[str, Any], + *, + events_uri: str, + actor_peer_id: str, + query: str, + env: Mapping[str, str], + deadline: float, + runner: CommandRunner, +) -> list[dict[str, str]]: + _validate_peer_scope(events_uri, actor_peer_id) + compact_query = public_safe_compact_text(query, limit=240) + if not compact_query: + return [] + find_res = _run_ov_command( + profile, + ["find", compact_query, "-u", events_uri, "-n", "3", "-L", "2", "-o", "json", "-c", "true"], + actor_peer_id=actor_peer_id, + env=env, + deadline=deadline, + runner=runner, + ) + if find_res.returncode != 0: + return [] + try: + rows = _extract_entries(_extract_json(find_res.stdout)) + except ValueError: + return [] + + hits: list[dict[str, str]] = [] + seen: set[str] = set() + prefix = f"{events_uri.rstrip('/')}/" + for row in rows: + uri = str(row.get("uri") or row.get("resource_uri") or "").strip() + if not uri.startswith(prefix) or uri in seen: + continue + if uri.rsplit("/", 1)[-1] in {".overview.md", ".abstract.md"}: + continue + seen.add(uri) + body = _extract_content(row).strip() or str( + row.get("abstract") or row.get("summary") or row.get("overview") or "" + ).strip() + if not body and time.monotonic() < deadline - 0.15: + read_res = _run_ov_command( + profile, + ["read", uri, "-o", "json", "-c", "true"], + actor_peer_id=actor_peer_id, + env=env, + deadline=deadline, + runner=runner, + ) + if read_res.returncode == 0: + try: + body = _extract_content(_extract_json(read_res.stdout)).strip() + except ValueError: + body = "" + if body: + hits.append( + { + "event_ref": uri.removeprefix(prefix), + "excerpt": body[:EVENT_SNIPPET_CHAR_LIMIT], + } + ) + if len(hits) >= 3: + break + return hits + + +def _plan_turn( + runtime_root: Path, + session: Mapping[str, Any], + scope: Mapping[str, Any], + turn: Mapping[str, Any] | None, + *, + sender_ref: str | None = None, + sender_type: str | None = None, + sender_label: str | None = None, +) -> dict[str, Any]: + try: + setting = load_effective_steward_memory_setting(runtime_root) + except (OSError, ValueError, TypeError): + return {"eligible": False, "reason": "setting_unavailable"} + if not setting.get("enabled"): + return {"eligible": False, "reason": "disabled"} + try: + return effect_runtime_result( + "collaboration.steward.memory_policy", + { + "setting": setting, + "session": dict(session), + "scope": dict(scope), + "turn": dict(turn) if isinstance(turn, Mapping) else None, + **({"sender_ref": sender_ref} if sender_ref is not None else {}), + **({"sender_type": sender_type} if sender_type is not None else {}), + **({"sender_label": sender_label} if sender_label is not None else {}), + }, + ) + except (EffectRuntimeRejected, ValueError, TypeError): + return {"eligible": False, "reason": "policy_rejected"} + + +def recall_steward_memory_for_turn( + *, + runtime_root: Path, + session: Mapping[str, Any], + scope: Mapping[str, Any], + turn: Mapping[str, Any] | None, + message: str, + sender_ref: str | None = None, + sender_type: str | None = None, + sender_label: str | None = None, + runner: CommandRunner = subprocess.run, +) -> dict[str, Any] | None: + """Recall isolated OpenViking preferences and bounded events on ingress. + + Returns ``None`` immediately when disabled or outside an eligible owner private + steward Turn. Fails open on timeout or unavailable OpenViking service so normal + Steward replies are never blocked. + """ + plan = _plan_turn( + runtime_root, + session, + scope, + turn, + sender_ref=sender_ref, + sender_type=sender_type, + sender_label=sender_label, + ) + if not plan.get("eligible"): + return None + + started = time.monotonic() + timeout_seconds = float(plan.get("recall_timeout_seconds") or DEFAULT_RECALL_TIMEOUT_SECONDS) + deadline = started + max(1.0, timeout_seconds) + base_payload: dict[str, Any] = { + "schema_version": "steward_memory_recall_v0", + "actor_peer_id": plan["actor_peer_id"], + "speaker_directory": plan["speaker_directory"], + "is_owner": bool(plan["is_owner"]), + "owner_preferences": [], + "omitted_owner_topics": [], + "speaker_preferences": [], + "omitted_speaker_topics": [], + "recalled_events": [], + "guidance": _STEWARD_MEMORY_GUIDANCE, + } + profile = _load_connection_profile(runtime_root) + if profile is None: + return { + **base_payload, + "status": "not_configured", + "latency_ms": int((time.monotonic() - started) * 1000), + } + + binding_id = str(plan["binding_id"]) + user_space = str(plan["user_space"]) + actor_peer_id = str(plan["actor_peer_id"]) + owner_key = (binding_id, user_space, "owner") + speaker_dir = str(plan["speaker_directory"]) + speaker_key = (binding_id, user_space, speaker_dir) + + owner_data = _cached_speaker_preferences(owner_key) + speaker_data = ( + owner_data + if plan["is_owner"] + else _cached_speaker_preferences(speaker_key) + ) + recalled_events: list[dict[str, str]] = [] + status = "completed" + + try: + with _ephemeral_ov_env(profile, timeout_seconds=timeout_seconds) as (env, _tmp): + if owner_data is None: + owner_data = _read_preferences_directory( + profile, + preferences_uri=str(plan["owner_preferences_uri"]), + actor_peer_id=actor_peer_id, + char_limit=OWNER_PREFERENCE_CHAR_LIMIT, + env=env, + deadline=deadline, + runner=runner, + ) + _store_speaker_preferences(owner_key, owner_data) + if plan["is_owner"]: + speaker_data = owner_data + if not plan["is_owner"] and speaker_data is None: + speaker_data = _read_preferences_directory( + profile, + preferences_uri=str(plan["speaker_preferences_uri"]), + actor_peer_id=actor_peer_id, + char_limit=SPEAKER_PREFERENCE_CHAR_LIMIT, + env=env, + deadline=deadline, + runner=runner, + ) + _store_speaker_preferences(speaker_key, speaker_data) + if ( + plan.get("auto_recall") is True + and _is_substantive_message(message) + and time.monotonic() < deadline - 0.15 + ): + recalled_events = _recall_events( + profile, + events_uri=str(plan["events_uri"]), + actor_peer_id=actor_peer_id, + query=message, + env=env, + deadline=deadline, + runner=runner, + ) + except subprocess.TimeoutExpired: + status = "timeout" + except (OSError, RuntimeError, ValueError): + status = "unavailable" + + owner_items = list((owner_data or {}).get("items") or []) + owner_omitted = list((owner_data or {}).get("omitted_topics") or []) + speaker_items = [] if plan["is_owner"] else list((speaker_data or {}).get("items") or []) + speaker_omitted = [] if plan["is_owner"] else list((speaker_data or {}).get("omitted_topics") or []) + return { + **base_payload, + "status": status, + "owner_preferences": owner_items, + "omitted_owner_topics": owner_omitted, + "speaker_preferences": speaker_items, + "omitted_speaker_topics": speaker_omitted, + "recalled_events": recalled_events, + "latency_ms": int((time.monotonic() - started) * 1000), + } + + +def _safe_event_filename(turn_id: str) -> str: + cleaned = _SAFE_TURN_SEGMENT_RE.sub("-", str(turn_id or "").strip()).strip("-.")[:64] + if not cleaned: + cleaned = f"turn-{int(time.time())}" + return f"{cleaned}.md" + + +def _write_ov_file( + profile: Mapping[str, Any], + *, + target_uri: str, + content: str, + actor_peer_id: str, + env: Mapping[str, str], + tmp_dir: Path, + deadline: float, + runner: CommandRunner, + index: int, +) -> bool: + _validate_peer_scope(target_uri, actor_peer_id) + local_file = tmp_dir / f"payload-{index}.md" + local_file.write_text(content, encoding="utf-8") + res = _run_ov_command( + profile, + [ + "write", + target_uri, + "--from-file", + str(local_file), + "--mode", + "replace", + "--wait", + "--processing-mode", + "vectors_only", + "-o", + "json", + "-c", + "true", + ], + actor_peer_id=actor_peer_id, + env=env, + deadline=deadline, + runner=runner, + ) + return res.returncode == 0 + + +def commit_steward_memory_after_turn( + *, + runtime_root: Path, + session: Mapping[str, Any], + scope: Mapping[str, Any], + turn: Mapping[str, Any] | None, + turn_id: str, + user_message: str, + response: Mapping[str, Any], + sender_ref: str | None = None, + sender_type: str | None = None, + sender_label: str | None = None, + timeout_seconds: float = COMMIT_TIMEOUT_SECONDS, + runner: CommandRunner = subprocess.run, +) -> dict[str, Any] | None: + """Persist speaker-scoped preference mutations and compact turn event after completion.""" + plan = _plan_turn( + runtime_root, + session, + scope, + turn, + sender_ref=sender_ref, + sender_type=sender_type, + sender_label=sender_label, + ) + if not plan.get("eligible"): + return None + + started = time.monotonic() + deadline = started + max(1.0, float(timeout_seconds)) + profile = _load_connection_profile(runtime_root) + if profile is None: + return { + "schema_version": "steward_memory_commit_v0", + "status": "not_configured", + "preferences_applied": 0, + "event_written": False, + "latency_ms": int((time.monotonic() - started) * 1000), + } + + binding_id = str(plan["binding_id"]) + user_space = str(plan["user_space"]) + actor_peer_id = str(plan["actor_peer_id"]) + speaker_dir = str(plan["speaker_directory"]) + speaker_prefs_uri = str(plan["speaker_preferences_uri"]).rstrip("/") + events_uri = str(plan["events_uri"]).rstrip("/") + + raw_prefs = response.get("memory_preferences") + prefs: list[dict[str, Any]] = [] + if isinstance(raw_prefs, list) and raw_prefs: + try: + prefs = list( + effect_runtime_result( + "collaboration.steward.memory_preferences", + {"memory_preferences": raw_prefs}, + ).get("preferences") + or [] + ) + except (EffectRuntimeRejected, ValueError, TypeError): + prefs = [] + + if prefs: + _invalidate_speaker_cache(binding_id, user_space, speaker_dir) + + applied = 0 + event_written = False + status = "completed" + try: + with _ephemeral_ov_env(profile, timeout_seconds=timeout_seconds) as (env, tmp_dir): + for idx, pref in enumerate(prefs): + target_uri = f"{speaker_prefs_uri}/{pref['topic_file']}" + if pref["operation"] == "upsert": + if _write_ov_file( + profile, + target_uri=target_uri, + content=str(pref["content"]), + actor_peer_id=actor_peer_id, + env=env, + tmp_dir=tmp_dir, + deadline=deadline, + runner=runner, + index=idx, + ): + applied += 1 + elif pref["operation"] == "remove": + _validate_peer_scope(target_uri, actor_peer_id) + rm_res = _run_ov_command( + profile, + ["rm", target_uri, "-o", "json", "-c", "true"], + actor_peer_id=actor_peer_id, + env=env, + deadline=deadline, + runner=runner, + ) + if rm_res.returncode == 0: + applied += 1 + + compact_user = public_safe_compact_text(user_message, limit=800) + compact_assistant = public_safe_compact_text(response.get("message") or "", limit=800) + if compact_user or compact_assistant: + timestamp = datetime.now(timezone.utc).isoformat(timespec="seconds") + event_doc = ( + f"speaker={speaker_dir}\n" + f"timestamp={timestamp}\n" + f"user: {compact_user or '[empty]'}\n" + f"steward: {compact_assistant or '[empty]'}\n" + ) + event_uri = f"{events_uri}/{_safe_event_filename(turn_id)}" + event_written = _write_ov_file( + profile, + target_uri=event_uri, + content=event_doc, + actor_peer_id=actor_peer_id, + env=env, + tmp_dir=tmp_dir, + deadline=deadline, + runner=runner, + index=999, + ) + if (prefs and applied < len(prefs)) or not event_written: + status = "partial" + except subprocess.TimeoutExpired: + status = "timeout" + except (OSError, RuntimeError, ValueError): + status = "unavailable" + + return { + "schema_version": "steward_memory_commit_v0", + "status": status, + "preferences_applied": applied, + "event_written": event_written, + "latency_ms": int((time.monotonic() - started) * 1000), + } diff --git a/loopx/chat.py b/loopx/chat.py index d3b31f13d3..b9969dacc0 100644 --- a/loopx/chat.py +++ b/loopx/chat.py @@ -860,6 +860,35 @@ def normalize_agent_response( handoff = normalize_handoff(payload.get("context_handoff")) protected = tuple(protected_paths) + memory_prefs: list[dict[str, Any]] = [] + raw_prefs = payload.get("memory_preferences") + if raw_prefs is not None: + if not isinstance(raw_prefs, list): + raise ValueError("memory_preferences must be a list") + redacted_prefs = [ + { + **{str(k): v for k, v in item.items() if k != "content"}, + **( + {"content": redact_response_markdown(str(item["content"]), protected_paths=protected)} + if isinstance(item.get("content"), str) + else ({"content": item["content"]} if "content" in item else {}) + ), + } + if isinstance(item, Mapping) + else item + for item in raw_prefs + ] + from .control_plane.effect_runtime import EffectRuntimeRejected, effect_runtime_result + try: + memory_prefs = list( + effect_runtime_result( + "collaboration.steward.memory_preferences", + {"memory_preferences": redacted_prefs}, + ).get("preferences") + or [] + ) + except EffectRuntimeRejected as exc: + raise ValueError(str(exc)) from exc message = redact_response_markdown( str(payload.get("message") or ""), protected_paths=protected, @@ -873,6 +902,7 @@ def normalize_agent_response( or unfinished.get("effects_settled") is not True or any(not isinstance(unfinished.get(key), str) or not 1 <= len(unfinished[key].strip()) <= 1000 for key in ("remaining", "dependency")) + or bool(memory_prefs) or any(payload.get(key) for key in ("gate", "protected_action", "goal_draft", "context_handoff", "proposals"))): unfinished = None else: @@ -882,6 +912,7 @@ def normalize_agent_response( return { "schema_version": CHAT_AGENT_RESPONSE_SCHEMA_VERSION, "message": message, + **({"memory_preferences": memory_prefs} if memory_prefs else {}), **({"unfinished_work": unfinished} if unfinished else {}), **({"goal_draft": goal_draft} if goal_draft else {}), **({"context_handoff": handoff} if handoff else {}), diff --git a/loopx/chat/coordination.py b/loopx/chat/coordination.py index 98f8cbbb91..0c2ac8c1b0 100644 --- a/loopx/chat/coordination.py +++ b/loopx/chat/coordination.py @@ -328,6 +328,11 @@ def prepare_turn_context(controller, adapter, session, turn_id, event_sink, *, s if scope.get("bound_steward") is True and isinstance(context.get("bound_steward"), dict): from ..capabilities.native_chat.external_conversations import ChatExternalConversations context["bound_steward"]["executions"] = ChatExternalConversations(controller).commission_evidence(session) + if not observation_only: + from ..capabilities.steward_memory.runtime import recall_steward_memory_for_turn + recalled_memory = recall_steward_memory_for_turn(runtime_root=runtime_root, session=session, scope=scope, turn=turn, message=str(turn.get("message") or "")) + if recalled_memory is not None: + context["steward_memory"] = recalled_memory controller.store.append_event(session_id, turn_id, kind="manager.context", payload=context) if observation_only: # The scoped reader validates its full current source before/after. diff --git a/loopx/chat_runtime.py b/loopx/chat_runtime.py index 5fabe79c59..713722617e 100644 --- a/loopx/chat_runtime.py +++ b/loopx/chat_runtime.py @@ -2038,6 +2038,14 @@ def record_owner_resume() -> None: return if adapter.upstream_thread_id != str((self.store.load_session(session_id) or {}).get("upstream_thread_id") or ""): self.store.update_session(session_id, upstream_thread_id=adapter.upstream_thread_id) + if not native_loopx and scope.get("bound_steward") is True: + try: + from .capabilities.steward_memory.runtime import commit_steward_memory_after_turn + memory_receipt = commit_steward_memory_after_turn(runtime_root=self.coordination_runtime_root, session=session, scope=scope, turn=current_turn, turn_id=turn_id, user_message=str(current_turn.get("message") or ""), response=response) + if memory_receipt is not None: + self.store.append_event(session_id, turn_id, kind="steward.memory", payload=memory_receipt) + except Exception: # noqa: BLE001 - memory writeback must fail open without breaking Turn completion. + pass self.store.finalize_managed_turn_completion( session_id, turn_id, diff --git a/loopx/control_plane/collaboration/steward_memory.ts b/loopx/control_plane/collaboration/steward_memory.ts new file mode 100644 index 0000000000..761b2e26d8 --- /dev/null +++ b/loopx/control_plane/collaboration/steward_memory.ts @@ -0,0 +1,268 @@ +import {createHash} from "node:crypto"; +import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts"; +import {normalizeStewardContext} from "./conversation_scope.ts"; + +const USER_SPACE_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,199}$/; +const TOPIC_FILE_RE = /^[^/\\~%?#\x00-\x1f\x7f]+\.md$/; +const SPEAKER_DIGITS = 12; +const SPEAKER_SEGMENT_CHARS = 32; +const TOPIC_MAX_CHARS = 60; +const CONTENT_MAX_CHARS = 1200; +const MAX_PREFERENCE_OPS = 8; + +const FORBIDDEN_TARGET_KEYS = new Set([ + "speaker", + "owner", + "uri", + "scope_ref", + "user_space", + "peer_id", + "operator_ref", + "binding_id", + "person", + "target", + "sender_id", + "sender_ref", + "directory", +]); + +const ALLOWED_PREFERENCE_KEYS = new Set(["operation", "topic", "content", "topic_file"]); + +export interface StewardMemorySettings { + schema_version: "steward_memory_machine_defaults_v0"; + enabled: boolean; + user_space: string; + auto_recall: boolean; + recall_timeout_seconds: number; +} + +export interface StewardMemoryPreferenceOp { + operation: "upsert" | "remove"; + topic: string; + topic_file: string; + content?: string; +} + +export function normalizeStewardMemorySettings(raw: unknown): StewardMemorySettings { + if (raw == null) { + return { + schema_version: "steward_memory_machine_defaults_v0", + enabled: false, + user_space: "steward", + auto_recall: true, + recall_timeout_seconds: 3, + }; + } + if (typeof raw !== "object" || Array.isArray(raw)) { + throw new EffectRuntimeRequestError("steward_memory configuration must be an object"); + } + const row = raw as Record; + const unknown = Object.keys(row).filter( + (k) => !["schema_version", "enabled", "user_space", "auto_recall", "recall_timeout_seconds"].includes(k), + ); + if (unknown.length > 0) { + throw new EffectRuntimeRequestError(`steward_memory contains unsupported fields: ${unknown.sort().join(", ")}`); + } + if (row.schema_version !== "steward_memory_machine_defaults_v0") { + throw new EffectRuntimeRequestError("steward_memory must use steward_memory_machine_defaults_v0"); + } + if (typeof row.enabled !== "boolean") { + throw new EffectRuntimeRequestError("steward_memory.enabled must be a boolean"); + } + const userSpace = typeof row.user_space === "string" ? row.user_space.trim() : ""; + if (!USER_SPACE_RE.test(userSpace)) { + throw new EffectRuntimeRequestError("steward_memory.user_space must use a path-safe identifier"); + } + if (typeof row.auto_recall !== "boolean") { + throw new EffectRuntimeRequestError("steward_memory.auto_recall must be a boolean"); + } + if ( + typeof row.recall_timeout_seconds !== "number" + || !Number.isInteger(row.recall_timeout_seconds) + || row.recall_timeout_seconds < 1 + || row.recall_timeout_seconds > 15 + ) { + throw new EffectRuntimeRequestError("steward_memory.recall_timeout_seconds must be an integer between 1 and 15"); + } + return { + schema_version: "steward_memory_machine_defaults_v0", + enabled: row.enabled, + user_space: userSpace, + auto_recall: row.auto_recall, + recall_timeout_seconds: row.recall_timeout_seconds, + }; +} + +export function sanitizeSpeakerSegment(label: unknown): string { + const normalized = String(label ?? "").normalize("NFC"); + const cleaned = normalized + .replace(/[\u0000-\u001f\u007f/\\~%?#]+/g, "") + .replace(/\s+/g, "_") + .replace(/^[._]+/, "") + .slice(0, SPEAKER_SEGMENT_CHARS); + return cleaned || "speaker"; +} + +export function slugifyMemoryTopic(topic: unknown): {topic: string; topic_file: string} { + if (typeof topic !== "string") { + throw new EffectRuntimeRequestError("memory preference topic must be a string"); + } + const trimmed = topic.normalize("NFC").trim(); + if (/[/\\~%?#\x00-\x1f\x7f]/.test(trimmed) || trimmed.includes("..")) { + throw new EffectRuntimeRequestError("invalid memory preference topic"); + } + const slug = trimmed + .toLowerCase() + .replace(/[\s_]+/g, "-") + .replace(/^[.-]+|[.-]+$/g, "") + .slice(0, TOPIC_MAX_CHARS) + .replace(/\.+$/g, ""); + const file = `${slug}.md`; + if (!slug || slug === "." || slug === ".." || !TOPIC_FILE_RE.test(file)) { + throw new EffectRuntimeRequestError("invalid memory preference topic"); + } + return {topic: slug, topic_file: file}; +} + +export function normalizeStewardMemoryPreferences(value: unknown): StewardMemoryPreferenceOp[] { + if (value == null) return []; + if (!Array.isArray(value)) { + throw new EffectRuntimeRequestError("memory_preferences must be a list"); + } + if (value.length > MAX_PREFERENCE_OPS) { + throw new EffectRuntimeRequestError(`memory_preferences accepts at most ${MAX_PREFERENCE_OPS} operations`); + } + const operations: StewardMemoryPreferenceOp[] = []; + for (const item of value) { + if (!item || typeof item !== "object" || Array.isArray(item)) { + throw new EffectRuntimeRequestError("each memory preference item must be an object"); + } + const row = item as Record; + for (const key of Object.keys(row)) { + if (FORBIDDEN_TARGET_KEYS.has(key) || !ALLOWED_PREFERENCE_KEYS.has(key)) { + throw new EffectRuntimeRequestError(`unsupported memory preference field: ${key}`); + } + } + const op = typeof row.operation === "string" ? row.operation.trim().toLowerCase() : ""; + if (op !== "upsert" && op !== "remove") { + throw new EffectRuntimeRequestError("memory preference operation must be upsert or remove"); + } + const {topic, topic_file} = slugifyMemoryTopic(row.topic); + if (typeof row.topic_file === "string" && row.topic_file !== topic_file) { + throw new EffectRuntimeRequestError("memory preference topic_file mismatch"); + } + if (op === "upsert") { + if (typeof row.content !== "string") { + throw new EffectRuntimeRequestError("upsert memory preference requires string content"); + } + const content = row.content.trim(); + if (!content || content.length > CONTENT_MAX_CHARS) { + throw new EffectRuntimeRequestError(`memory preference content must contain 1 to ${CONTENT_MAX_CHARS} characters`); + } + operations.push({operation: "upsert", topic, topic_file, content}); + } else { + operations.push({operation: "remove", topic, topic_file}); + } + } + return operations; +} + +function rejectTurn(reason: string): Record { + return { + schema_version: "steward_memory_turn_plan_v0", + eligible: false, + reason, + }; +} + +export function planStewardMemoryTurn(input: Record): Record { + const setting = normalizeStewardMemorySettings(input.setting); + if (!setting.enabled) { + return rejectTurn("disabled"); + } + const session = input.session; + const scope = input.scope; + if (!session || typeof session !== "object" || Array.isArray(session)) { + return rejectTurn("session_unavailable"); + } + const sessionRow = session as Record; + if (sessionRow.project_context != null || sessionRow.steward_context == null) { + return rejectTurn("non_steward_session"); + } + if (sessionRow.answer_review === true) { + return rejectTurn("answer_review_turn"); + } + let stewardContext: Record; + try { + stewardContext = normalizeStewardContext(sessionRow.steward_context); + } catch { + return rejectTurn("invalid_steward_context"); + } + if (!scope || typeof scope !== "object" || Array.isArray(scope)) { + return rejectTurn("scope_unavailable"); + } + const scopeRow = scope as Record; + if ( + scopeRow.kind !== "external_audience" + || scopeRow.bound_steward !== true + || scopeRow.private_conversation !== true + ) { + return rejectTurn("audience_isolated"); + } + const turn = input.turn; + if (turn && typeof turn === "object" && !Array.isArray(turn)) { + const turnRow = turn as Record; + if ( + turnRow.loopx_execution === true + || turnRow.loopx_request != null + || turnRow.origin === "native_loopx" + || turnRow.origin === "peer" + || "collaboration_request" in turnRow + || turnRow.answer_review != null + || turnRow.metadata_review != null + ) { + return rejectTurn("internal_or_review_turn"); + } + } + const senderType = typeof input.sender_type === "string" ? input.sender_type.trim().toLowerCase() : ""; + if (senderType === "bot" || senderType === "app") { + return rejectTurn("bot_sender_excluded"); + } + const operatorRef = String(stewardContext.operator_ref ?? "").trim(); + if (!operatorRef) { + return rejectTurn("operator_ref_missing"); + } + const explicitSender = typeof input.sender_ref === "string" ? input.sender_ref.trim() : ""; + const senderRef = explicitSender || operatorRef; + if (!senderRef || senderRef === "unknown") { + return rejectTurn("sender_unverified"); + } + const isOwner = senderRef === operatorRef; + const channelId = String(sessionRow.channel_id ?? ""); + const speakerDirectory = isOwner + ? "owner" + : `${sanitizeSpeakerSegment(input.sender_label)}~${createHash("sha256") + .update(`${channelId}\n${senderRef}`) + .digest("hex") + .slice(0, SPEAKER_DIGITS)}`; + + const bindingId = String(stewardContext.binding_id); + const actorPeerId = `steward-${bindingId}`; + const memoryRootUri = `viking://user/${setting.user_space}/peers/${actorPeerId}/memories`; + return { + schema_version: "steward_memory_turn_plan_v0", + eligible: true, + reason: "eligible", + actor_peer_id: actorPeerId, + binding_id: bindingId, + user_space: setting.user_space, + auto_recall: setting.auto_recall, + recall_timeout_seconds: setting.recall_timeout_seconds, + is_owner: isOwner, + speaker_directory: speakerDirectory, + owner_preferences_uri: `${memoryRootUri}/preferences/owner`, + speaker_preferences_uri: `${memoryRootUri}/preferences/${speakerDirectory}`, + events_uri: `${memoryRootUri}/events`, + memory_root_uri: memoryRootUri, + }; +} diff --git a/loopx/control_plane/effect_runtime_handlers.ts b/loopx/control_plane/effect_runtime_handlers.ts index b3f3574f45..6fbe7ec4ab 100644 --- a/loopx/control_plane/effect_runtime_handlers.ts +++ b/loopx/control_plane/effect_runtime_handlers.ts @@ -630,6 +630,8 @@ export function createEffectRuntimeHandlers( ["collaboration.source.original_conversation_grants", lazyHandler(() => import("./collaboration/source_grants.ts"), ({inspectOriginalConversationGrants}) => inspectOriginalConversationGrants)], ["collaboration.project.session_identity", lazyHandler(() => import("./collaboration/conversation_scope.ts"), ({projectConversationIdentity}) => projectConversationIdentity)], ["collaboration.steward.session_identity", lazyHandler(() => import("./collaboration/conversation_scope.ts"), ({stewardConversationIdentity}) => stewardConversationIdentity)], + ["collaboration.steward.memory_policy", lazyHandler(() => import("./collaboration/steward_memory.ts"), ({planStewardMemoryTurn}) => planStewardMemoryTurn)], + ["collaboration.steward.memory_preferences", lazyHandler(() => import("./collaboration/steward_memory.ts"), ({normalizeStewardMemoryPreferences}) => (params) => ({preferences: normalizeStewardMemoryPreferences(params?.memory_preferences)}))], ["collaboration.steward.command", lazyHandler(() => import("./collaboration/conversation_binding.ts"), ({stewardCommand}) => stewardCommand)], ["collaboration.steward.authorize_creation", lazyHandler(() => import("./collaboration/conversation_binding.ts"), ({authorizeStewardCreation}) => authorizeStewardCreation)], ["collaboration.steward.managed_mode.plan", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({planStewardManagedMode}) => planStewardManagedMode)], diff --git a/tests/capabilities/test_capability_configuration_ui.py b/tests/capabilities/test_capability_configuration_ui.py index fce9397390..ab045c6bd1 100644 --- a/tests/capabilities/test_capability_configuration_ui.py +++ b/tests/capabilities/test_capability_configuration_ui.py @@ -37,7 +37,7 @@ def isolated_native_profile_discovery(tmp_path, monkeypatch): @pytest.mark.parametrize("capability_id", [ "context_delivery", "goal_capability_organization", "goal_storage", "todo_replan_cadence", "periodic_report", "manager_runtime", - "steward_executor", "multi_subagent", "peer_task_coordination", + "steward_executor", "steward_memory", "multi_subagent", "peer_task_coordination", "explore_graph", "explore_harness", "change_quality_qualification", "progress_review", "pull_request_review", "local_authority_shadow", "coordination_runtime_shadow", "lark_kanban_heartbeat_sync", "reward_memory", @@ -492,6 +492,7 @@ def test_resolution_rejects_values_for_unsupported_scopes() -> None: [ ("manager_runtime", ["machine"]), ("steward_executor", ["machine"]), + ("steward_memory", ["machine"]), ("periodic_report", ["machine", "goal"]), ("change_quality_qualification", ["machine", "goal"]), ("pull_request_review", ["machine", "goal"]), diff --git a/tests/capabilities/test_machine_configuration_contract.py b/tests/capabilities/test_machine_configuration_contract.py index aa1e8db415..8817473b7e 100644 --- a/tests/capabilities/test_machine_configuration_contract.py +++ b/tests/capabilities/test_machine_configuration_contract.py @@ -357,6 +357,7 @@ def test_builtin_catalog_lists_every_owner_in_namespace_order() -> None: "periodic_report", "pull_request_review", "steward_executor", + "steward_memory", "todo_replan_cadence", ) assert [ @@ -450,6 +451,7 @@ def test_builtin_registry_loads_only_the_owners_a_reader_resolves() -> None: "loopx.capabilities.periodic_report.machine_defaults", "loopx.capabilities.pr_review_queue.machine_defaults", "loopx.capabilities.steward_executor.machine_defaults", + "loopx.capabilities.steward_memory.machine_defaults", "loopx.capabilities.todo_replan_cadence.machine_defaults", ) if module in sys.modules diff --git a/tests/control_plane_ts/steward_memory.test.ts b/tests/control_plane_ts/steward_memory.test.ts new file mode 100644 index 0000000000..3bacb00f21 --- /dev/null +++ b/tests/control_plane_ts/steward_memory.test.ts @@ -0,0 +1,155 @@ +import assert from "node:assert/strict"; +import {test} from "node:test"; +import { + normalizeStewardMemoryPreferences, + normalizeStewardMemorySettings, + planStewardMemoryTurn, +} from "../../loopx/control_plane/collaboration/steward_memory.ts"; + +const STEWARD_CONTEXT = { + kind: "bound_steward", + binding_id: "111111111111111111111111", + project_ref: "222222222222222222222222", + workspace_path: "/tmp/steward-workspace", + source_ref: "333333333333333333333333", + provider_ref: "444444444444444444444444", + operator_ref: "555555555555555555555555", + audience: "bound_owner", + grant: "portfolio_read", + goal_ids: ["goal-alpha"], +}; + +const PRIVATE_SCOPE = { + kind: "external_audience", + bound_steward: true, + private_conversation: true, +}; + +const ENABLED_SETTING = { + schema_version: "steward_memory_machine_defaults_v0", + enabled: true, + user_space: "steward", + auto_recall: true, + recall_timeout_seconds: 3, +}; + +test("default-off and invalid settings reject cleanly", () => { + assert.equal(normalizeStewardMemorySettings(null).enabled, false); + const disabled = planStewardMemoryTurn({ + setting: {...ENABLED_SETTING, enabled: false}, + session: {channel_id: "manager.external.native", steward_context: STEWARD_CONTEXT}, + scope: PRIVATE_SCOPE, + }); + assert.equal(disabled.eligible, false); + assert.equal(disabled.reason, "disabled"); + + assert.throws(() => normalizeStewardMemorySettings({...ENABLED_SETTING, user_space: "bad/space"})); + assert.throws(() => normalizeStewardMemorySettings({...ENABLED_SETTING, recall_timeout_seconds: 30})); + assert.throws(() => normalizeStewardMemorySettings({...ENABLED_SETTING, extra: true})); +}); + +test("owner vs non-owner speaker directories stay isolated and collision-safe", () => { + const session = { + channel_id: "manager.external.native.1111.3333", + steward_context: STEWARD_CONTEXT, + }; + const ownerPlan = planStewardMemoryTurn({ + setting: ENABLED_SETTING, + session, + scope: PRIVATE_SCOPE, + turn: {turn_id: "t1", origin: "external"}, + sender_ref: STEWARD_CONTEXT.operator_ref, + sender_label: "Owner", + sender_type: "user", + }); + assert.equal(ownerPlan.eligible, true); + assert.equal(ownerPlan.is_owner, true); + assert.equal(ownerPlan.speaker_directory, "owner"); + assert.equal(ownerPlan.actor_peer_id, `steward-${STEWARD_CONTEXT.binding_id}`); + assert.equal( + ownerPlan.speaker_preferences_uri, + `viking://user/steward/peers/steward-${STEWARD_CONTEXT.binding_id}/memories/preferences/owner`, + ); + + const guestA = planStewardMemoryTurn({ + setting: ENABLED_SETTING, + session, + scope: PRIVATE_SCOPE, + sender_ref: "ou_guest_a", + sender_label: "Alice", + sender_type: "user", + }); + const guestSameName = planStewardMemoryTurn({ + setting: ENABLED_SETTING, + session, + scope: PRIVATE_SCOPE, + sender_ref: "ou_guest_b", + sender_label: "Alice", + sender_type: "user", + }); + const guestRenamed = planStewardMemoryTurn({ + setting: ENABLED_SETTING, + session, + scope: PRIVATE_SCOPE, + sender_ref: "ou_guest_a", + sender_label: "Alice Updated", + sender_type: "user", + }); + assert.equal(guestA.is_owner, false); + assert.match(String(guestA.speaker_directory), /^Alice~[0-9a-f]{12}$/); + assert.notEqual(guestA.speaker_directory, guestSameName.speaker_directory); + assert.equal( + String(guestA.speaker_directory).split("~")[1], + String(guestRenamed.speaker_directory).split("~")[1], + ); +}); + +test("group chats, project chats, internal monitor/report turns, review turns, and bots are rejected", () => { + const baseSession = {channel_id: "manager.external.native", steward_context: STEWARD_CONTEXT}; + for (const [name, input] of [ + ["group scope", {setting: ENABLED_SETTING, session: baseSession, scope: {kind: "external_audience", bound_steward: false, private_conversation: false}}], + ["project session", {setting: ENABLED_SETTING, session: {project_context: {kind: "project_workspace"}}, scope: PRIVATE_SCOPE}], + ["loopx_execution turn", {setting: ENABLED_SETTING, session: baseSession, scope: PRIVATE_SCOPE, turn: {loopx_execution: true}}], + ["native_loopx turn", {setting: ENABLED_SETTING, session: baseSession, scope: PRIVATE_SCOPE, turn: {origin: "native_loopx"}}], + ["peer turn", {setting: ENABLED_SETTING, session: baseSession, scope: PRIVATE_SCOPE, turn: {origin: "peer"}}], + ["answer_review session", {setting: ENABLED_SETTING, session: {...baseSession, answer_review: true}, scope: PRIVATE_SCOPE}], + ["metadata_review turn", {setting: ENABLED_SETTING, session: baseSession, scope: PRIVATE_SCOPE, turn: {metadata_review: {}}}], + ["bot sender", {setting: ENABLED_SETTING, session: baseSession, scope: PRIVATE_SCOPE, sender_type: "bot"}], + ]) { + const plan = planStewardMemoryTurn(input as Record); + assert.equal(plan.eligible, false, `${name} must not receive steward memory`); + } +}); + +test("normalizeStewardMemoryPreferences blocks speaker/uri overrides and enforces slug & size bounds", () => { + const normalized = normalizeStewardMemoryPreferences([ + {operation: "upsert", topic: "PR Reply Style", content: "Prefer concise Chinese bullets"}, + {operation: "remove", topic: "Old Rule"}, + ]); + assert.deepEqual(normalized, [ + { + operation: "upsert", + topic: "pr-reply-style", + topic_file: "pr-reply-style.md", + content: "Prefer concise Chinese bullets", + }, + { + operation: "remove", + topic: "old-rule", + topic_file: "old-rule.md", + }, + ]); + + assert.throws(() => normalizeStewardMemoryPreferences([ + {operation: "upsert", topic: "style", content: "hi", speaker: "owner"}, + ])); + assert.throws(() => normalizeStewardMemoryPreferences([ + {operation: "upsert", topic: "style", content: "hi", uri: "viking://user/other"}, + ])); + assert.throws(() => normalizeStewardMemoryPreferences([ + {operation: "upsert", topic: "../escape", content: "hi"}, + ])); + assert.throws(() => normalizeStewardMemoryPreferences([ + {operation: "upsert", topic: "too-long", content: "x".repeat(1201)}, + ])); +}); diff --git a/tests/test_chat_machine_configuration_api.py b/tests/test_chat_machine_configuration_api.py index 637008efa0..6b903e1ced 100644 --- a/tests/test_chat_machine_configuration_api.py +++ b/tests/test_chat_machine_configuration_api.py @@ -235,6 +235,24 @@ def test_real_chat_http_catalog_and_machine_write_boundary(tmp_path: Path) -> No }, "private-invalid-endpoint", ), + ( + "steward_memory", + { + "schema_version": "steward_memory_machine_defaults_v0", + "enabled": True, + "user_space": "private/invalid/space", + "auto_recall": True, + "recall_timeout_seconds": 3, + }, + { + "schema_version": "steward_memory_machine_defaults_v0", + "enabled": False, + "user_space": "steward", + "auto_recall": True, + "recall_timeout_seconds": 3, + }, + "private/invalid/space", + ), ], ) def test_real_chat_http_invalid_namespace_has_safe_repair_path( @@ -348,6 +366,7 @@ def test_inspection_lists_registered_namespaces_without_local_refs( "periodic_report", "pull_request_review", "steward_executor", + "steward_memory", "todo_replan_cadence", ] namespace_catalog = { @@ -447,7 +466,7 @@ def test_machine_catalog_discovers_goal_features_without_granting_machine_writes ) # Machine-only capabilities are the ones a Goal cannot override: the # manager profile, native host defaults, steward executor and storage target. - assert set(machine) - {"manager_runtime", "steward_executor", "goal_storage", "codex_chat", "context_delivery"} == { + assert set(machine) - {"manager_runtime", "steward_executor", "steward_memory", "goal_storage", "codex_chat", "context_delivery"} == { feature["feature_id"] for feature in goal["features"] } assert machine["pull_request_review"]["available_scopes"] == ["machine", "goal"] @@ -463,7 +482,7 @@ def test_machine_catalog_discovers_goal_features_without_granting_machine_writes for capability_id, item in machine.items(): assert "current" not in item assert "commands" not in item - if capability_id in {"manager_runtime", "steward_executor", "goal_storage", "codex_chat", "context_delivery"}: + if capability_id in {"manager_runtime", "steward_executor", "steward_memory", "goal_storage", "codex_chat", "context_delivery"}: assert item["available_scopes"] == ["machine"] assert item["machine_namespace"] == capability_id assert item["configuration_editor"]["writable_scopes"] == ["machine"] diff --git a/tests/test_steward_openviking_memory.py b/tests/test_steward_openviking_memory.py new file mode 100644 index 0000000000..731a44796f --- /dev/null +++ b/tests/test_steward_openviking_memory.py @@ -0,0 +1,306 @@ +from __future__ import annotations + +import json +import subprocess +from pathlib import Path +from typing import Any + +import pytest + +from loopx.capabilities.machine_configuration.store import machine_configuration_store_path +from loopx.capabilities.native_chat.codex_context import process_environment +from loopx.capabilities.native_chat.external_conversations import ChatExternalConversations +from loopx.capabilities.steward_memory.runtime import ( + clear_steward_memory_cache, + commit_steward_memory_after_turn, + recall_steward_memory_for_turn, +) +from loopx.chat import CHAT_REVIEW_CLOSE_TAG, CHAT_REVIEW_OPEN_TAG, parse_agent_response + + +STEWARD_CTX = { + "kind": "bound_steward", + "binding_id": "111111111111111111111111", + "project_ref": "222222222222222222222222", + "workspace_path": "/tmp/steward-workspace", + "source_ref": "333333333333333333333333", + "provider_ref": "444444444444444444444444", + "operator_ref": "555555555555555555555555", + "audience": "bound_owner", + "grant": "portfolio_read", + "goal_ids": ["goal-alpha"], +} +PRIVATE_SCOPE = { + "kind": "external_audience", + "bound_steward": True, + "private_conversation": True, +} + + +def _enable_steward_memory(runtime_root: Path, *, timeout: int = 3) -> None: + cfg_path = machine_configuration_store_path(runtime_root) + cfg_path.parent.mkdir(parents=True, exist_ok=True) + cfg_path.write_text( + json.dumps( + { + "schema_version": "loopx_machine_configuration_v0", + "namespaces": { + "steward_memory": { + "schema_version": "steward_memory_machine_defaults_v0", + "enabled": True, + "user_space": "steward", + "auto_recall": True, + "recall_timeout_seconds": timeout, + } + }, + } + ), + encoding="utf-8", + ) + + +def _write_steward_credentials(runtime_root: Path) -> None: + cred_path = runtime_root / "machine" / "credentials" / "steward-openviking.json" + cred_path.parent.mkdir(parents=True, exist_ok=True) + cred_path.write_text( + json.dumps( + { + "service_url": "http://127.0.0.1:19999", + "api_key": "test-steward-ov-secret", + "ov_bin": "/usr/local/bin/ov.mjs", + } + ), + encoding="utf-8", + ) + + +class _FakeOpenVikingServer: + def __init__(self) -> None: + self.files: dict[str, str] = {} + self.calls: list[list[str]] = [] + self.observed_config_files: list[str | None] = [] + + def __call__(self, command: list[str], **kwargs: Any) -> subprocess.CompletedProcess[str]: + self.calls.append(list(command)) + env = kwargs.get("env") or {} + conf_path = env.get("OPENVIKING_CLI_CONFIG_FILE") + assert conf_path and Path(conf_path).is_file() + assert (Path(conf_path).stat().st_mode & 0o777) == 0o600 + self.observed_config_files.append(conf_path) + assert "--actor-peer-id" in command + idx = command.index("--actor-peer-id") + peer_id = command[idx + 1] + assert peer_id == f"steward-{STEWARD_CTX['binding_id']}" + + rest = command[idx + 2 :] + sub = rest[0] + if sub == "ls": + prefix = rest[1].rstrip("/") + "/" + entries = [ + {"uri": uri, "name": uri.rsplit("/", 1)[-1], "type": "file", "mtime": "2026-11-19T12:00:00Z"} + for uri in sorted(self.files) + if uri.startswith(prefix) + ] + return subprocess.CompletedProcess(command, 0, stdout=json.dumps({"ok": True, "result": entries}), stderr="") + if sub == "read": + uri = rest[1] + if uri not in self.files: + return subprocess.CompletedProcess(command, 1, stdout='{"ok":false,"error":{"code":"NOT_FOUND"}}', stderr="") + return subprocess.CompletedProcess( + command, 0, stdout=json.dumps({"ok": True, "result": {"content": self.files[uri]}}), stderr="" + ) + if sub == "write": + uri = rest[1] + from_idx = rest.index("--from-file") + content = Path(rest[from_idx + 1]).read_text(encoding="utf-8") + assert "--processing-mode" in rest and "vectors_only" in rest + self.files[uri] = content + return subprocess.CompletedProcess(command, 0, stdout='{"ok":true}', stderr="") + if sub == "rm": + uri = rest[1] + self.files.pop(uri, None) + return subprocess.CompletedProcess(command, 0, stdout='{"ok":true}', stderr="") + if sub == "find": + query = rest[1] + u_idx = rest.index("-u") + scope_prefix = rest[u_idx + 1].rstrip("/") + "/" + hits = [ + {"uri": uri, "abstract": content[:120], "content": content} + for uri, content in sorted(self.files.items(), reverse=True) + if uri.startswith(scope_prefix) and any(tok in content.lower() for tok in query.lower().split()) + ] + return subprocess.CompletedProcess( + command, 0, stdout=json.dumps({"ok": True, "result": {"memories": hits}}), stderr="" + ) + raise AssertionError(f"unexpected ov command: {command}") + + +def test_feature_off_and_non_steward_leakage_guards(tmp_path: Path) -> None: + clear_steward_memory_cache() + _write_steward_credentials(tmp_path) + called = False + + def fail_runner(*args: Any, **kwargs: Any) -> subprocess.CompletedProcess[str]: + nonlocal called + called = True + raise AssertionError("ov must not be called when disabled or ineligible") + + session = {"session_id": "s1", "channel_id": "manager.external.native", "steward_context": STEWARD_CTX} + # 1. Default-off (no steward_memory enabled setting) + assert recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + message="记住我的偏好", runner=fail_runner, + ) is None + assert not called + + # 2. Enabled, but group / project / native_loopx / answer_review scopes never trigger + _enable_steward_memory(tmp_path) + for bad_scope, bad_session, bad_turn in [ + ({"kind": "external_audience", "bound_steward": False, "private_conversation": False}, session, {}), + (PRIVATE_SCOPE, {"session_id": "p1", "project_context": {"kind": "project_workspace"}}, {}), + (PRIVATE_SCOPE, session, {"origin": "native_loopx", "loopx_execution": True}), + (PRIVATE_SCOPE, {**session, "answer_review": True}, {}), + ]: + assert recall_steward_memory_for_turn( + runtime_root=tmp_path, session=bad_session, scope=bad_scope, turn=bad_turn, + message="请查进展", runner=fail_runner, + ) is None + assert not called + + +def test_owner_and_speaker_preferences_persist_cache_and_recall_across_sessions(tmp_path: Path) -> None: + clear_steward_memory_cache() + _enable_steward_memory(tmp_path) + _write_steward_credentials(tmp_path) + server = _FakeOpenVikingServer() + + session_a = {"session_id": "sess_a", "channel_id": "manager.external.native.1111.3333", "steward_context": STEWARD_CTX} + session_b = {"session_id": "sess_b", "channel_id": "manager.external.native.1111.3333", "steward_context": STEWARD_CTX} + + # Owner states preference in session A -> parsed from envelope -> committed to /preferences/owner + event written + parsed = parse_agent_response( + "收到。" + + CHAT_REVIEW_OPEN_TAG + + json.dumps({ + "schema_version": "loopx_chat_agent_response_v0", + "message": "收到,后续直接用中文短列表汇报 deploy 总结。", + "memory_preferences": [ + {"operation": "upsert", "topic": "Report Format", "content": "Always answer deploy summaries in concise Chinese bullets."}, + ], + "proposals": [], + "protected_action": None, + "goal_draft": None, + "context_handoff": None, + "gate": None, + "unfinished_work": None, + }) + + CHAT_REVIEW_CLOSE_TAG + ) + receipt = commit_steward_memory_after_turn( + runtime_root=tmp_path, session=session_a, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + turn_id="turn_a1", user_message="记住以后 deploy 总结用中文短列表", response=parsed, runner=server, + ) + assert receipt and receipt["status"] == "completed" + assert receipt["preferences_applied"] == 1 and receipt["event_written"] is True + assert all(not Path(p).exists() for p in server.observed_config_files if p) + + owner_uri = f"viking://user/steward/peers/steward-{STEWARD_CTX['binding_id']}/memories/preferences/owner/report-format.md" + assert owner_uri in server.files + + # Fresh session B recalls owner preference + historical deploy event + recalled = recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session_b, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + message="今天 deploy 情况如何", runner=server, + ) + assert recalled and recalled["status"] == "completed" + assert recalled["owner_preferences"][0]["topic"] == "report-format" + assert "concise Chinese bullets" in recalled["owner_preferences"][0]["content"] + assert len(recalled["recalled_events"]) == 1 + + # Cache hit avoids redundant `ls`/`read` calls until invalidated by remove + ls_before = sum(1 for c in server.calls if "ls" in c) + recalled_cached = recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session_b, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + message="hi", runner=server, + ) + assert recalled_cached and recalled_cached["owner_preferences"][0]["topic"] == "report-format" + assert sum(1 for c in server.calls if "ls" in c) == ls_before + + # Guest speaker gets owner preferences as standing rules + separate hashed speaker directory + guest_receipt = commit_steward_memory_after_turn( + runtime_root=tmp_path, session=session_b, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + turn_id="turn_b2", user_message="叫我小陈", response={ + "message": "好的", + "memory_preferences": [{"operation": "upsert", "topic": "greeting", "content": "Call guest XiaoChen."}], + }, + sender_ref="ou_guest_chen", sender_label="Alice", sender_type="user", runner=server, + ) + assert guest_receipt and guest_receipt["preferences_applied"] == 1 + guest_recall = recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session_b, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + message="hi", sender_ref="ou_guest_chen", sender_label="Alice", sender_type="user", runner=server, + ) + assert guest_recall and not guest_recall["is_owner"] + assert guest_recall["speaker_directory"].startswith("Alice~") + assert guest_recall["owner_preferences"][0]["topic"] == "report-format" + assert guest_recall["speaker_preferences"][0]["topic"] == "greeting" + + # Remove invalidates cache + commit_steward_memory_after_turn( + runtime_root=tmp_path, session=session_b, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + turn_id="turn_b3", user_message="忘掉旧汇报格式", response={ + "message": "已忘掉", + "memory_preferences": [{"operation": "remove", "topic": "Report Format"}], + }, + runner=server, + ) + after_remove = recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session_b, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + message="hi", runner=server, + ) + assert after_remove and after_remove["owner_preferences"] == [] + + +def test_timeout_and_missing_credentials_fail_open_and_env_is_scrubbed(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + clear_steward_memory_cache() + _enable_steward_memory(tmp_path, timeout=1) + session = {"session_id": "s1", "channel_id": "manager.external.native", "steward_context": STEWARD_CTX} + + missing = recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session, scope=PRIVATE_SCOPE, turn={"origin": "external"}, message="hello world", + ) + assert missing and missing["status"] == "not_configured" + + _write_steward_credentials(tmp_path) + + def slow_runner(cmd: list[str], **kwargs: Any) -> subprocess.CompletedProcess[str]: + raise subprocess.TimeoutExpired(cmd, kwargs.get("timeout", 1)) + + timed_out = recall_steward_memory_for_turn( + runtime_root=tmp_path, session=session, scope=PRIVATE_SCOPE, turn={"origin": "external"}, + message="hello world", runner=slow_runner, + ) + assert timed_out and timed_out["status"] == "timeout" + + monkeypatch.setenv("OPENVIKING_API_KEY", "ambient-secret") + monkeypatch.setenv("OPENVIKING_CLI_CONFIG_FILE", "/tmp/ambient.conf") + env = process_environment(tmp_path / ".codex", isolated=False) + assert "OPENVIKING_API_KEY" not in env + assert "OPENVIKING_CLI_CONFIG_FILE" not in env + + +def test_parse_agent_response_rejects_memory_override_and_review_blocks_memory_preferences() -> None: + bad = parse_agent_response( + "已记录" + + CHAT_REVIEW_OPEN_TAG + + json.dumps({ + "schema_version": "loopx_chat_agent_response_v0", + "message": "已记录", + "memory_preferences": [{"operation": "upsert", "topic": "style", "content": "x", "speaker": "other"}], + "proposals": [], "protected_action": None, "goal_draft": None, "context_handoff": None, "gate": None, + }) + + CHAT_REVIEW_CLOSE_TAG + ) + assert "memory_preferences" not in bad + assert bad["gate"] and bad["gate"]["kind"] == "response_metadata_invalid" + assert isinstance(ChatExternalConversations, type)