Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat(compile): 通过 `--skill memory` 支持记忆整理模式 (#5178)
chenjwa09a9d20a8e0
diff --git a/bot/vikingbot/cli/commands.py b/bot/vikingbot/cli/commands.pyindex 57b27bae3..5f6232375 100644--- a/bot/vikingbot/cli/commands.py+++ b/bot/vikingbot/cli/commands.py@@ -12,34 +12,73 @@ import warningsfrom pathlib import Pathfrom typing import Any, Optional-import typer-from loguru import logger-from prompt_toolkit import PromptSession-from prompt_toolkit.formatted_text import FormattedText-from prompt_toolkit.history import FileHistory-from prompt_toolkit.patch_stdout import patch_stdout-from prompt_toolkit.styles import Style as PromptStyle-from rich.console import Console-from rich.table import Table--from openviking.utils.time_utils import parse_iso_datetime-from vikingbot import __logo__, __version__-from vikingbot.agent.loop import AgentLoop-from vikingbot.bus.queue import MessageBus-from vikingbot.channels.manager import ChannelManager-from vikingbot.config.loader import (++# --- stdout-safety guard (must run before any openviking/vikingbot imports) ---+#+# Some top-level imports below (e.g. vikingbot.agent -> vikingbot.config.loader+# -> openviking.server.config -> ...) eagerly call get_logger(__name__) inside+# openviking modules. That loads ov.conf and, if the config contains unknown+# fields (e.g. legacy `memory.working_memory_enabled`), emits a WARNING through+# openviking_cli.utils.logger's QueueListener whose backing StreamHandler is+# tied to log.output = "stdout" by default. In `vikingbot chat -e` mode stdout+# is a JSON contract, so any log line there breaks downstream JSON parsers+# (e.g. benchmark/locomo/vikingbot/run_eval.py). Redirect the shared stdout+# stream handler to stderr before we let those imports run.+def _preimport_redirect_openviking_logs_to_stderr() -> None:+ try:+ from openviking_cli.utils.logger import ( # type: ignore+ _create_queued_stream_handler,+ _std_stream_handlers,+ )+ except Exception:+ return+ # Force the "stdout" queue listener + real handler pair to exist, then+ # rebind its stream to stderr. Later get_logger("stdout") calls fetch the+ # cached listener and inherit the redirection.+ try:+ _create_queued_stream_handler("stdout")+ except Exception:+ return+ entry = _std_stream_handlers.get("stdout")+ if entry is None:+ return+ _listener, real_handler = entry+ try:+ real_handler.setStream(sys.stderr)+ except AttributeError:+ real_handler.stream = sys.stderr+++_preimport_redirect_openviking_logs_to_stderr()++import typer # noqa: E402+from loguru import logger # noqa: E402+from prompt_toolkit import PromptSession # noqa: E402+from prompt_toolkit.formatted_text import FormattedText # noqa: E402+from prompt_toolkit.history import FileHistory # noqa: E402+from prompt_toolkit.patch_stdout import patch_stdout # noqa: E402+from prompt_toolkit.styles import Style as PromptStyle # noqa: E402+from rich.console import Console # noqa: E402+from rich.table import Table # noqa: E402++from openviking.utils.time_utils import parse_iso_datetime # noqa: E402+from vikingbot import __logo__, __version__ # noqa: E402+from vikingbot.agent.loop import AgentLoop # noqa: E402+from vikingbot.bus.queue import MessageBus # noqa: E402+from vikingbot.channels.manager import ChannelManager # noqa: E402+from vikingbot.config.loader import ( # noqa: E402ensure_config,get_config_path,get_data_dir,load_config,validate_openviking_auth,)-from vikingbot.config.schema import Config, SessionKey, requires_gateway_token-from vikingbot.cron.service import CronService-from vikingbot.cron.types import CronJob-from vikingbot.heartbeat.service import HeartbeatService-from vikingbot.integrations.langfuse import LangfuseClient-from vikingbot.observability.feedback_stats import (+from vikingbot.config.schema import Config, SessionKey, requires_gateway_token # noqa: E402+from vikingbot.cron.service import CronService # noqa: E402+from vikingbot.cron.types import CronJob # noqa: E402+from vikingbot.heartbeat.service import HeartbeatService # noqa: E402+from vikingbot.integrations.langfuse import LangfuseClient # noqa: E402+from vikingbot.observability.feedback_stats import ( # noqa: E402compute_feedback_stats,format_feedback_stats_table,select_feedback_stats,@@ -47,9 +86,9 @@ from vikingbot.observability.feedback_stats import ()# Create sandbox manager-from vikingbot.sandbox.manager import SandboxManager-from vikingbot.session.manager import SessionManager-from vikingbot.utils.helpers import (+from vikingbot.sandbox.manager import SandboxManager # noqa: E402+from vikingbot.session.manager import SessionManager # noqa: E402+from vikingbot.utils.helpers import ( # noqa: E402get_bridge_path,get_history_path,get_source_workspace_path,@@ -97,13 +136,32 @@ def _redirect_openviking_logs_to_stderr() -> None:This prevents log output (e.g. deprecation warnings from memory_config)from polluting stdout when vikingbot chat is used in --eval mode or piped.+ Must run before ensure_config() so config-load warnings also land on+ stderr."""import logging+ def _redirect_stream_handler(handler: logging.Handler) -> None:+ """Swap a StreamHandler-like handler's stdout stream for stderr."""+ stream = getattr(handler, "stream", None)+ if stream is sys.stdout:+ try:+ handler.setStream(sys.stderr) # StreamHandler API+ except AttributeError:+ handler.stream = sys.stderr++ def _redirect_handler(handler: logging.Handler) -> None:+ # openviking_cli routes managed loggers through a QueueHandler whose+ # backing StreamHandler is the one that actually writes to stdout/stderr+ # from a listener thread. Redirect that real handler too.+ real = getattr(handler, "_ov_real_handler", None)+ if isinstance(real, logging.StreamHandler):+ _redirect_stream_handler(real)+ if isinstance(handler, logging.StreamHandler):+ _redirect_stream_handler(handler)+for root_name in ("openviking", "openviking_cli"):root_logger = logging.getLogger(root_name)- # If the logger has no handlers yet, add a stderr handler now.- # If it already has handlers, swap any stdout StreamHandlers to stderr.if not root_logger.handlers:handler = logging.StreamHandler(sys.stderr)formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")@@ -112,8 +170,21 @@ def _redirect_openviking_logs_to_stderr() -> None:root_logger.propagate = Falseelse:for handler in root_logger.handlers:- if isinstance(handler, logging.StreamHandler) and handler.stream is sys.stdout:- handler.setStream(sys.stderr)+ _redirect_handler(handler)++ # openviking_cli.utils.logger keeps a process-wide QueueListener whose real+ # StreamHandler was constructed from the current log.output setting (often+ # stdout). Redirect that shared handler too so future writes go to stderr.+ try:+ from openviking_cli.utils.logger import _std_stream_handlers # type: ignore++ entry = _std_stream_handlers.get("stdout")+ if entry is not None:+ _listener, real_handler = entry+ _redirect_stream_handler(real_handler)+ except Exception:+ # Best-effort; if the internal API changes we still fall through.+ pass# Also redirect Python warnings to stderrwarnings.simplefilter("default")@@ -885,13 +956,16 @@ def chat("""Interact with the agent directly."""path = Path(config_path).expanduser() if config_path is not None else None- bus = MessageBus()- config = ensure_config(path)-# Redirect openviking/openviking_cli standard-library logs to stderr so they# don't pollute stdout JSON output (important for --eval mode and piping).+ # Must run BEFORE ensure_config(): parsing ov.conf can emit warnings (e.g.+ # unknown config fields), and those would otherwise land on stdout ahead+ # of the JSON result._redirect_openviking_logs_to_stderr()+ bus = MessageBus()+ config = ensure_config(path)+validate_openviking_auth(config)_warn_deprecated_memory_user(memory_user)_init_bot_data(config)diff --git a/crates/ov_cli/src/commands/compile.rs b/crates/ov_cli/src/commands/compile.rsindex 57749ff8f..c844b4fae 100644--- a/crates/ov_cli/src/commands/compile.rs+++ b/crates/ov_cli/src/commands/compile.rs@@ -3,6 +3,9 @@ use crate::error::{Error, Result};use crate::output::{OutputFormat, output_success};use serde_json::{Map, Value};+/// Sentinel `--skill` value that routes to in-place memory consolidation.+const MEMORY_COMPILE_SKILL: &str = "memory";+pub async fn run(client: &HttpClient,from_uris: Vec<String>,@@ -13,7 +16,9 @@ pub async fn run(output_format: OutputFormat,compact: bool,) -> Result<()> {- let sources = normalize_sources(from_uris)?;+ let skill = skill.trim().to_string();+ let memory_mode = skill == MEMORY_COMPILE_SKILL;+ let sources = normalize_sources(from_uris, memory_mode)?;let args = parse_args(args.as_deref())?;let instruction = instruction.as_deref()@@ -23,7 +28,7 @@ pub async fn run(.create_compile(&sources,to.trim(),- skill.trim(),+ skill.as_str(),instruction,args.as_ref(),)@@ -44,7 +49,7 @@ fn parse_args(value: Option<&str>) -> Result<Option<Map<String, Value>>> {Ok((!args.is_empty()).then_some(args))}-fn normalize_sources(values: Vec<String>) -> Result<Vec<String>> {+fn normalize_sources(values: Vec<String>, memory_mode: bool) -> Result<Vec<String>> {let mut result = Vec::new();for value in values {for item in value.split(',') {@@ -57,6 +62,14 @@ fn normalize_sources(values: Vec<String>) -> Result<Vec<String>> {}}}+ if memory_mode {+ if !result.is_empty() {+ return Err(Error::Client(+ "--skill memory consolidates --to in place and takes no --from".into(),+ ));+ }+ return Ok(result);+ }if result.is_empty() {return Err(Error::Client("at least one --from directory is required".into(),@@ -86,21 +99,33 @@ mod tests {#[test]fn expands_comma_separated_and_repeated_sources_stably() {- let result = normalize_sources(vec![- "viking://resources/a,viking://resources/b".into(),- "viking://resources/a".into(),- ])+ let result = normalize_sources(+ vec![+ "viking://resources/a,viking://resources/b".into(),+ "viking://resources/a".into(),+ ],+ false,+ ).expect("sources should be valid");assert_eq!(result, vec!["viking://resources/a", "viking://resources/b"]);}#[test]fn rejects_empty_source_items() {- assert!(normalize_sources(vec!["viking://resources/a,".into()]).is_err());+ assert!(normalize_sources(vec!["viking://resources/a,".into()], false).is_err());let args = parse_args(Some(r#"{"model_name":"endpoint-1"}"#)).expect("args should be valid").expect("args should not be empty");assert_eq!(args["model_name"], "endpoint-1");assert!(parse_args(Some("[]")).is_err());}++ #[test]+ fn memory_mode_allows_no_sources_but_rejects_any() {+ assert!(normalize_sources(vec![], true)+ .expect("memory mode allows empty sources")+ .is_empty());+ assert!(normalize_sources(vec!["viking://resources/a".into()], true).is_err());+ assert!(normalize_sources(vec![], false).is_err());+ }}diff --git a/crates/ov_cli/src/help_ui.rs b/crates/ov_cli/src/help_ui.rsindex 4a2f10e31..2facc148e 100644--- a/crates/ov_cli/src/help_ui.rs+++ b/crates/ov_cli/src/help_ui.rs@@ -868,7 +868,7 @@ const COMMAND_HELP_SPECS: &[CommandHelpSpec] = &[},CommandHelpSpec {path: &["compile"],- purpose: "Use a required VikingBot Skill to compile OpenViking materials into Wiki pages or a Skill package.",+ purpose: "Use a Skill to compile OpenViking materials into Wiki pages or a Skill package, or `--skill memory` to consolidate existing memories in place.",examples: &[HelpItem {label: "ov compile --from viking://resources/weekly --to viking://resources/wiki --skill viking://agent/skills/monthly_wiki",@@ -878,6 +878,10 @@ const COMMAND_HELP_SPECS: &[CommandHelpSpec] = &[label: "ov compile --from viking://resources/weekly --to viking://agent/skills --skill viking://agent/skills/skill-creator",description: "Generate or update one shared Skill package.",},+ HelpItem {+ label: "ov compile --to viking://user/me/memories/entities --skill memory",+ description: "Consolidate one memory type in place (dedup/merge); takes no --from.",+ },],next_steps: &[HelpItem {diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rsindex 6927434a5..cda4023e2 100644--- a/crates/ov_cli/src/main.rs+++ b/crates/ov_cli/src/main.rs@@ -1157,10 +1157,10 @@ enum Commands {},/// [Interactive] Compile source materials with a VikingBot SkillCompile {- /// Source file or directory; repeat the flag or separate entries with commas+ /// Source file or directory; repeat the flag or separate entries with commas.+ /// Omitted with `--skill memory`, which consolidates `--to` in place.#[arg(long = "from",- required = true,value_delimiter = ',',value_name = "uri")]@@ -1168,7 +1168,7 @@ enum Commands {/// Target Wiki directory or skills namespace#[arg(long, value_name = "uri")]to: String,- /// Skill directory or SKILL.md Viking URI+ /// Skill directory or SKILL.md Viking URI; the literal `memory` runs memory consolidation#[arg(long, value_name = "uri")]skill: String,/// Additional instructions for this Compile task@@ -4177,6 +4177,28 @@ mod tests {);}+ #[test]+ fn cli_compile_memory_mode_parses_without_from() {+ let cli = Cli::try_parse_from([+ "ov",+ "compile",+ "--to",+ "viking://user/u1/memories/entities",+ "--skill",+ "memory",+ ])+ .expect("memory-mode compile should parse without --from");+ match cli.command {+ Commands::Compile {+ from_uris, skill, ..+ } => {+ assert!(from_uris.is_empty());+ assert_eq!(skill, "memory");+ }+ _ => panic!("expected compile command"),+ }+ }+#[test]fn cli_parses_system_backend_sync_status() {let cli = Cli::try_parse_from(["ov", "system", "backend", "sync-status", "viking://a"])diff --git a/docs/design/ov-compile-design.md b/docs/design/ov-compile-design.mdindex aa788ede7..7a549c68f 100644--- a/docs/design/ov-compile-design.md+++ b/docs/design/ov-compile-design.md@@ -77,6 +77,50 @@ CLI 不等待任务完成。用户通过 `ov task status <task_id>` 查询状态Task 结果中的 `created`、`updated` 和 `unchanged` 只统计 Agent 本次提交的页面;未被草稿触达的目标页面不计入 `unchanged`。`page_count` 等于三者之和,`link_count` 只统计最终正文中实际渲染出的 bundle 内 WikiLink。+### 2.3 记忆整理模式(`--skill memory`)++除了上面基于 VikingBot Skill 的 Wiki 整理,`ov compile` 还支持一种**记忆整理模式**:把 `--skill`+设为哨兵值 `memory`,即可对 `--to` 指定的 memories 根目录或某个记忆类型目录做**就地整理**(去重、合并、拆分、原地精简),+产物严格遵守各自记忆类型的 schema。该模式完全在 OpenViking 进程内通过现有 memory 框架+(`ExtractLoop` + `MemoryUpdater`)执行,**不经过 VikingBot**。++```bash+# 整理 entities 记忆(就地去重/合并/规范化)+ov compile \+ --to viking://user/<user_id>/memories/entities \+ --skill memory \+ --instruction "合并明显重复的实体,但不同实体不要合并;保留每个实体的独立事实"++# 也可以整理其它记忆类型,例如 preferences+ov compile \+ --to viking://user/<user_id>/memories/preferences \+ --skill memory+```++参数规则:++| 参数 | 规则 |+| --- | --- |+| `--skill` | 固定值 `memory`(哨兵,不解析为真实 Skill)触发记忆整理模式 |+| `--to` | 必填,可为 `.../memories` 根目录或某个记忆类型目录(如 `.../memories/entities`);不接受用户根目录 |+| `--from` | 记忆模式下**不接受**;整理就地发生在 `--to` 空间内 |+| `--instruction` | 可选,作为整理指令(软提示)喂给整理模型;用于点破"两条其实是同一实体需合并"这类模型无法自行判断的场景 |++行为要点:++- **类型范围**:指定类型目录时只加载该类型;指定 memories 根目录时按 schema 存储路径发现已存在且已启用的类型,包括根目录单文件记忆,在同一任务、同一 ExtractLoop 中联合整理,不拆子任务。各类型仍遵守自身 schema;结果的 `memory_types` 列出本次范围,根目录请求的 `memory_type` 为 `null`。+ 改名或合并时,系统仍可更新其它类型的相邻记忆文件以迁移 links/backlinks。+- **就地整理**:`--from` 与 `--to` 是同一空间,不引入外部来源,因此不存在跨身份空间的串号问题;+ 空间(self 或某个 `peers/{peer_id}`)由 `--to` 的 URI 决定。+- **保守合并**:默认只合并明显同一身份的记忆;模型无法从内容判断的合并(如"阿珍就是陈静娴"),+ 必须通过 `--instruction` 明确点破。+- **安全改名**:schema 允许 URI 字段变更时,公共 memory 写入层按“写入新 URI、迁移引用、删除旧 URI”+ 执行,结果记为 `adds` 新 URI + `deletes` 旧 URI。若目标 URI 已存在则报冲突、不覆盖,模型读取两份+ 记忆后需显式更新目标,并用 replacement 关系删除源文件。+- **异步任务**:与普通 compile 一致,命令返回一个 `cmp_` 前缀的 `task_id`,通过 `ov task status <task_id>`+ 查询结果。记忆模式的任务结果包含变化文件清单,字段沿用 `memory_diff.json` 的语义+ (`adds` / `updates` / `deletes` 及对应 `total_*`,仅文件 URI,不含内容),并带上本次整理的 `trace_id`。+## 3. 架构```textdiff --git a/docs/en/context-compilation/01-overview.md b/docs/en/context-compilation/01-overview.mdindex 26385803d..78638749d 100644--- a/docs/en/context-compilation/01-overview.md+++ b/docs/en/context-compilation/01-overview.md@@ -41,6 +41,10 @@ Compile itself does not decide *what* to compile into — the Skill does. The saThe first two examples also give the complete `ov` commands from **importing sources → adding the Skill → running compile → visualizing the output**, ending in an interactive HTML graph.+## Not only new artifacts: consolidating existing memories++Besides compiling source material into new knowledge artifacts with a Skill, `ov compile` has a special mode — set `--skill` to `memory` to consolidate an existing **memory** directory in place (dedup, merge, split, compact), without going through VikingBot. See [Memory Consolidation](./06-memory-consolidation.md).+## Prerequisites- A running OpenViking service with Bot enabled (`--with-bot`). The default endpoint is `http://localhost:1933`; remote use needs an API Key — see [Authentication](../guides/04-authentication.md). No service yet? Start with the [Quick Start](../getting-started/02-quickstart.md).diff --git a/docs/en/context-compilation/06-memory-consolidation.md b/docs/en/context-compilation/06-memory-consolidation.mdnew file mode 100644index 000000000..820f3e10c--- /dev/null+++ b/docs/en/context-compilation/06-memory-consolidation.md@@ -0,0 +1,109 @@+# Memory Consolidation++Besides compiling source material into Wikis, knowledge graphs, daily reports, and other artifacts with a Skill, `ov compile` has a special mode: **memory consolidation**. Instead of producing a new knowledge shape, it cleans up the **existing memories** in OpenViking in place — deduplicating, merging, splitting, and compacting them — while strictly conforming to each memory type's original schema.++Unlike regular compile, memory consolidation **does not go through VikingBot**; it runs directly inside OpenViking on the memory framework.++## When to use it++Memories accumulate session after session, and over time you get:++- **Duplicates**: the same person or event recorded multiple times with slightly different wording;+- **The same entity split across files**: extraction in different batches failed to recognize them as one object (e.g. "Ah-Zhen" and "Chen Jingxian" are actually the same person);+- **Mixed content**: unrelated objects crammed into one memory;+- **Verbosity**: repetitive phrasing that could be tighter.++Running memory consolidation over a memory directory makes such a collection clean and non-redundant again, without losing facts.++## How to use it++Set `--skill` to the sentinel value **`memory`** and point `--to` at a **memories root or memory-type directory**:++```bash+# Consolidate entities (dedup / merge / normalize in place)+ov compile \+ --to viking://user/<user_id>/memories/entities \+ --skill memory \+ --instruction "Merge clearly duplicate entities, but do not merge distinct ones; keep each entity's unique facts"+```++You can consolidate other memory types too, e.g. preferences:++```bash+ov compile \+ --to viking://user/<user_id>/memories/preferences \+ --skill memory+```++To consolidate all existing, enabled memory types in one task and one ExtractLoop:++```bash+ov compile --to viking://user/<user_id>/memories --skill memory \+ --instruction "Consolidate existing memories without losing facts; keep distinct objects separate"+```++Root-level files such as `profile.md`, `identity.md`, and `soul.md` are included when present. Disabled and unregistered types are skipped. The user's root does not include `peers/*/memories`; target a peer's memories root explicitly to consolidate that space.++The command returns a task ID immediately. Use `ov task status <id>` to check the result and `ov task cancel <id>` to stop it.++## Parameters++| Parameter | Description |+|-----------|-------------|+| `--skill memory` | The fixed sentinel value that triggers memory-consolidation mode (it is not resolved as a real Skill). |+| `--to` | Required: a **memories root** (`.../memories`) or a **memory-type directory** (e.g. `.../memories/entities`). User roots are not accepted. |+| `--from` | **Not accepted** in memory mode — consolidation pulls in no external sources; it only reorganizes memories already under `--to`. |+| `--instruction` | Optional. Passed to the model as a consolidation instruction (a soft hint). Use it to make merges the model cannot infer on its own, e.g. "Ah-Zhen is Chen Jingxian, please merge them." |++## Behavior++- **Type scope**: a type directory loads only its schema; a memories root loads all existing enabled types in that space into one ExtractLoop, with each type retaining its own schema. If a rename or merge affects existing links/backlinks, the system may still update neighboring memory files of other types to preserve referential integrity.+- **In place**: `--from` and `--to` are the same space and no external source is introduced, so there is no cross-identity leakage. The space being consolidated (the current user's own *self* space, or a `peers/{peer_id}` space) is determined by the `--to` URI.+- **Conservative merging**: only memories that are clearly the same identity are merged; distinct entities are kept separate even when they share a topic, category, or attributes. A merge the model cannot infer from content (e.g. two different names that are actually one person) is performed only when `--instruction` spells it out.+- **No fabrication**: it only reorganizes existing memories; it never invents new facts.+- **Facts preserved**: merges and compaction keep every distinct atomic fact and only compress duplicate wording.+- **Rename support**: when the schema allows a URI-defining field to change (for example an entity's `category` or `name`), consolidation writes the new URI, migrates links/backlinks, and then deletes the old URI. The result reports this as the new URI in `adds` and the old URI in `deletes`.+- **No conflict overwrite**: if the rename destination already exists, consolidation reports a conflict instead of overwriting it. The model must read both memories, update the explicit target with every distinct fact, and then delete the source with a replacement relationship.++## Result++A memory-mode task result includes the **list of changed files** for the run, with fields following the same semantics as the archived `memory_diff.json`:++| Field | Meaning |+|-------|---------|+| `adds` / `total_adds` | Newly created memory files (e.g. new entities produced by a split) |+| `updates` / `total_updates` | Modified memory files (a merge target, an in-place compaction) |+| `deletes` / `total_deletes` | Removed / merged-away memory files |+| `trace_id` | The trace of this consolidation run, for troubleshooting |+| `memory_types` | Types selected for this run; `memory_type` is the selected type for a type-directory request, or `null` for a root request |++The list contains file URIs only, not content — a single run may touch many files, and their bodies are not returned in the result.++If `errors` is non-empty and no file changes succeeded, the task is `failed` and retains its result and trace. Partial success remains `completed`, with successful changes and failures recorded separately in the lists and `errors`; callers must check `errors`, not just the task status. A no-op without errors is `completed`.++A typical merge (folding "Ah-Zhen" into "Chen Jingxian"):++```json+{+ "memory_type": "entities",+ "trace_id": "…",+ "adds": [],+ "updates": ["viking://user/xiaomei/memories/entities/person/陈静娴.md"],+ "deletes": ["viking://user/xiaomei/memories/entities/person/阿珍.md"],+ "total_adds": 0,+ "total_updates": 1,+ "total_deletes": 1,+ "errors": []+}+```++## Prerequisites++- A running OpenViking service (memory consolidation runs inside the service process; enabling Bot is not required). The default endpoint is `http://localhost:1933`; remote use needs an API Key — see [Authentication](../guides/04-authentication.md).+- The `ov` CLI configured with a connection (`~/.openviking/ovcli.conf` or `OPENVIKING_*` environment variables).+- The memory directory pointed to by `--to` already contains memories (typically extracted from sessions by session commit).++## Related docs++- [Context Compilation Overview](./01-overview.md) — the overall introduction to `ov compile`+- [Agent Runtime API](../api/23-agent-runtime.md) — full reference for creating, inspecting, and cancelling Compile tasksdiff --git a/docs/zh/context-compilation/01-overview.md b/docs/zh/context-compilation/01-overview.mdindex 5636cb957..7effab2da 100644--- a/docs/zh/context-compilation/01-overview.md+++ b/docs/zh/context-compilation/01-overview.md@@ -41,6 +41,10 @@ Compile 本身不规定「编译成什么」——那由 Skill 决定。同一前两个示例还给出了从**导入来源 → 添加 Skill → 执行编译 → 可视化产物**的完整 `ov` 命令,照着做就能得到一张可交互的 HTML 图。+## 不止生成新产物:整理已有记忆++除了用 Skill 把来源材料编译成新的知识产物,`ov compile` 还有一种特殊模式——把 `--skill` 设为 `memory`,即可对已有的**记忆**目录做就地整理(去重、合并、拆分、精简),且不经过 VikingBot。详见 [记忆整理](./06-memory-consolidation.md)。+## 前置条件- 一个正在运行、且启用了 Bot(`--with-bot`)的 OpenViking 服务。默认端点是 `http://localhost:1933`;远程使用需要 API Key,参见 [鉴权](../guides/04-authentication.md)。没有服务先看 [快速开始](../getting-started/02-quickstart.md)。diff --git a/docs/zh/context-compilation/06-memory-consolidation.md b/docs/zh/context-compilation/06-memory-consolidation.mdnew file mode 100644index 000000000..424cf2e87--- /dev/null+++ b/docs/zh/context-compilation/06-memory-consolidation.md@@ -0,0 +1,109 @@+# 记忆整理(Memory Consolidation)++除了用 Skill 把来源材料编译成 Wiki、知识图谱、日报等产物,`ov compile` 还有一种特殊模式:**记忆整理**。它不生成新的知识形态,而是把 OpenViking 里**已有的记忆**就地整理干净——去重、合并、拆分、精简,同时严格遵守每种记忆类型原本的 schema。++与普通 compile 不同,记忆整理**不经过 VikingBot**,而是直接在 OpenViking 内部用记忆框架完成。++## 什么时候用++记忆随着一次次会话不断累积,时间久了会出现:++- **重复**:同一个人、同一件事被多次记录,措辞略有不同;+- **同一实体多份**:模型在不同批次里没认出是同一个对象,拆成了多条(比如"阿珍"和"陈静娴"其实是同一个人);+- **混杂**:一条记忆里塞进了多个不相关的对象;+- **啰嗦**:同义反复、可以更紧凑。++这时对某个记忆目录跑一次记忆整理,就能让这类记忆集合重新变得干净、无冗余,且不丢事实。++## 怎么用++把 `--skill` 设为哨兵值 **`memory`**,`--to` 指向 **memories 根目录或某个记忆类型目录**即可:++```bash+# 整理 entities(就地去重 / 合并 / 规范化)+ov compile \+ --to viking://user/<user_id>/memories/entities \+ --skill memory \+ --instruction "合并明显重复的实体,但不同实体不要合并;保留每个实体的独立事实"+```++也可以整理其它记忆类型,比如偏好:++```bash+ov compile \+ --to viking://user/<user_id>/memories/preferences \+ --skill memory+```++一次整理所有已存在且已启用的类型,使用同一个任务、同一个 ExtractLoop:++```bash+ov compile --to viking://user/<user_id>/memories --skill memory \+ --instruction "整理已有记忆,保留独立事实,不要合并不同对象"+```++根目录下已存在的 `profile.md`、`identity.md`、`soul.md` 也会纳入;禁用或未注册的类型会跳过。用户 memories 根目录不包含 `peers/*/memories`,整理 peer 记忆需要明确指定该 peer 的 memories 根目录。++命令同样立即返回一个任务 ID,用 `ov task status <id>` 查看结果、用 `ov task cancel <id>` 取消。++## 参数++| 参数 | 说明 |+|------|------|+| `--skill memory` | 固定的哨兵值,触发记忆整理模式(不会被当作真实 Skill 解析)。 |+| `--to` | 必填,可为 **memories 根目录**(`.../memories`)或**某个记忆类型目录**(如 `.../memories/entities`);不接受用户根目录。 |+| `--from` | 记忆模式下**不接受**——整理不引入外部来源,只处理 `--to` 空间内已有的记忆。 |+| `--instruction` | 可选。作为整理指令(软提示)交给模型。用来点破模型自己判断不出来的合并,比如"阿珍就是陈静娴,请合并"。 |++## 行为要点++- **类型范围**:指定类型目录时只加载该类型的 schema;指定 memories 根目录时将该空间内所有已存在且已启用的类型加载到同一个 ExtractLoop,各类型仍遵守自身 schema。若改名或合并影响已有 links/backlinks,系统仍会更新其它类型中的相邻记忆文件以维持引用完整性。+- **就地整理**:`--from` 与 `--to` 是同一空间,不引入外部来源,因此不存在跨身份空间的串号问题。整理的空间(当前用户自己的 self 空间,还是某个 `peers/{peer_id}` 空间)由 `--to` 的 URI 决定。+- **保守合并**:默认只合并明显是同一身份的记忆;不同实体即使话题、类别、属性相近也不会被强行合并。模型从内容判断不出来的合并(例如两个不同名字其实是同一个人),必须通过 `--instruction` 明确点破才会执行。+- **不凭空创造**:只重组已有记忆,不会无中生有地新增事实。+- **保留事实**:合并、精简时会保留每一条独立的原子事实,只压缩重复表述。+- **支持改名**:当 schema 允许修改参与 URI 的字段时(例如 entity 的 `category` 或 `name`),整理会先写入新 URI、迁移 links/backlinks,再删除旧 URI;结果显示为 `adds` 新 URI + `deletes` 旧 URI。+- **冲突不覆盖**:如果改名后的目标 URI 已存在,整理会报冲突,不会覆盖。模型必须先读取两份记忆,把独立事实更新到明确的目标文件,再用 replacement 关系删除源文件。++## 返回结果++记忆模式的任务结果里包含本次整理的**变化文件清单**,字段沿用记忆归档 `memory_diff.json` 的语义:++| 字段 | 含义 |+|------|------|+| `adds` / `total_adds` | 新建的记忆文件(如拆分产生的新实体) |+| `updates` / `total_updates` | 被修改的记忆文件(合并后的目标、原地精简的文件) |+| `deletes` / `total_deletes` | 被删除/被合并掉的记忆文件 |+| `trace_id` | 本次整理的 trace,用于排查 |+| `memory_types` | 本次选中的类型列表;`memory_type` 在类型目录请求中为该类型,在根目录请求中为 `null` |++清单中只包含文件 URI,不含正文——一次整理可能触及很多文件,正文不随结果返回。++若 `errors` 非空且没有任何文件变更成功,任务标记为 `failed`,并保留结果和 trace。部分成功仍为 `completed`,成功变更和失败详情分别保留在清单与 `errors` 中;调用方必须检查 `errors`,不能只看任务状态。无改动且无错误时正常标记为 `completed`。++一个典型的合并例子(把"阿珍"合并进"陈静娴"):++```json+{+ "memory_type": "entities",+ "trace_id": "…",+ "adds": [],+ "updates": ["viking://user/xiaomei/memories/entities/person/陈静娴.md"],+ "deletes": ["viking://user/xiaomei/memories/entities/person/阿珍.md"],+ "total_adds": 0,+ "total_updates": 1,+ "total_deletes": 1,+ "errors": []+}+```++## 前置条件++- 一个正在运行的 OpenViking 服务(记忆整理在服务进程内执行,不需要额外启用 Bot)。默认端点 `http://localhost:1933`;远程使用需要 API Key,参见 [鉴权](../guides/04-authentication.md)。+- `ov` CLI 已配置好连接(`~/.openviking/ovcli.conf` 或 `OPENVIKING_*` 环境变量)。+- `--to` 指向的记忆目录里已经有记忆(一般由 session commit 从会话中抽取产生)。++## 相关文档++- [上下文编译概览](./01-overview.md) — `ov compile` 的整体介绍+- [Agent Runtime API](../api/23-agent-runtime.md) — 创建、查询和取消 Compile 任务的完整参考diff --git a/openviking/prompts/templates/memory/cases.yaml b/openviking/prompts/templates/memory/cases.yamlindex d291b5b06..69e164c5b 100644--- a/openviking/prompts/templates/memory/cases.yaml+++ b/openviking/prompts/templates/memory/cases.yaml@@ -12,7 +12,7 @@ description: |training pipeline to learn better experience memories from this commit rollout.directory: "viking://user/{{ user_space }}/memories/cases"-filename_template: "{{ case_name }}.md"+filename_template: "{{ case_name|lower }}.md"enabled: truepeer_enabled: falsecontent_template: |diff --git a/openviking/prompts/templates/memory/entities.yaml b/openviking/prompts/templates/memory/entities.yamlindex a8d9d95eb..c59b3116a 100644--- a/openviking/prompts/templates/memory/entities.yaml+++ b/openviking/prompts/templates/memory/entities.yaml@@ -30,13 +30,13 @@ fields:description: |Category written in {{ language }}.{% if language == 'en' %}Use lowercase with underscores, max 3 words.{% else %}Keep it concise and natural in {{ language }}.{% endif %}- merge_op: immutable+ merge_op: replace- name: nametype: stringdescription: |- Entity name in Chinese or English. If English, use lowercase with underscores, max 3 words.- merge_op: immutable+ merge_op: replace- name: contenttype: stringdiff --git a/openviking/prompts/templates/memory/experiences.yaml b/openviking/prompts/templates/memory/experiences.yamlindex 4f416c25a..5524f4e89 100644--- a/openviking/prompts/templates/memory/experiences.yaml+++ b/openviking/prompts/templates/memory/experiences.yaml@@ -4,7 +4,7 @@ description: |Captures a transferable pattern: what situation triggers it, what approach works, and why.directory: "viking://user/{{ user_space }}/memories/experiences"-filename_template: "{{ experience_name }}.md"+filename_template: "{{ experience_name|lower }}.md"enabled: trueoperation_mode: "upsert"stage: agentdiff --git a/openviking/prompts/templates/memory/experimental_memory/entities.yaml b/openviking/prompts/templates/memory/experimental_memory/entities.yamlindex cf99e06e9..a28fa9baa 100644--- a/openviking/prompts/templates/memory/experimental_memory/entities.yaml+++ b/openviking/prompts/templates/memory/experimental_memory/entities.yaml@@ -19,13 +19,13 @@ fields:type: stringdescription: |- Category name in Chinese or English. If English, use lowercase with underscores, max 3 words.- merge_op: immutable+ merge_op: replace- name: nametype: stringdescription: |- Entity name in Chinese or English. If English, use lowercase with underscores, max 3 words.- merge_op: immutable+ merge_op: replace- name: contenttype: stringdiff --git a/openviking/prompts/templates/memory/preferences.yaml b/openviking/prompts/templates/memory/preferences.yamlindex 789a48d8d..8e9b65d64 100644--- a/openviking/prompts/templates/memory/preferences.yaml+++ b/openviking/prompts/templates/memory/preferences.yaml@@ -6,7 +6,7 @@ description: |Topics can be: code style, communication style, tools, workflow, food, commute, etc.Store different topics as separate memory files, do NOT mix unrelated preferences.directory: "viking://user/{{ user_space }}/memories/preferences"-filename_template: "{{ user }}/{{ topic }}.md"+filename_template: "{{ user|lower }}/{{ topic|lower }}.md"enabled: trueembedding_template: |-{{ user }}diff --git a/openviking/prompts/templates/memory/trajectories.yaml b/openviking/prompts/templates/memory/trajectories.yamlindex bc910da91..9f0115586 100644--- a/openviking/prompts/templates/memory/trajectories.yaml+++ b/openviking/prompts/templates/memory/trajectories.yaml@@ -11,7 +11,7 @@ embedding_template: |-{{ retrieval_anchor }}directory: "viking://user/{{ user_space }}/memories/trajectories"-filename_template: "{{ trajectory_name }}_{{ extract_context.get_session_timestamp() }}.md"+filename_template: "{{ trajectory_name|lower }}_{{ extract_context.get_session_timestamp() }}.md"enabled: trueoperation_mode: "add_only"stage: agentdiff --git a/openviking/service/compile_service.py b/openviking/service/compile_service.pyindex 300d74e68..a92bce598 100644--- a/openviking/service/compile_service.py+++ b/openviking/service/compile_service.py@@ -20,6 +20,7 @@ from openviking.service.external_task_service import (ExternalTaskSnapshot,)from openviking.service.fs_service import FSService+from openviking.service.memory_compile import MemoryCompileRunnerfrom openviking.service.task_tracker import SENSITIVE_TASK_KEYS, TaskRecordfrom openviking_cli.exceptions import (InvalidArgumentError,@@ -32,16 +33,24 @@ from openviking_cli.utils.config.open_viking_config import CompileApiConfig_ACTIVE_STATUSES = frozenset({"accepted", "pending", "running", "committing"})_TERMINAL_STATUSES = frozenset({"completed", "failed", "cancelled"})+# Sentinel --skill value that routes a Compile request to in-process memory+# consolidation instead of the VikingBot agent path. It is not a real Skill URI.+MEMORY_COMPILE_SKILL = "memory"+class CompileRequest(BaseModel):model_config = ConfigDict(extra="forbid", populate_by_name=True)- from_: list[str] = Field(alias="from", min_length=1)+ from_: list[str] = Field(default_factory=list, alias="from")to: str = Field(min_length=1)skill: str = Field(min_length=1)instruction: str | None = Noneargs: dict[str, Any] | None = None+ @property+ def is_memory_mode(self) -> bool:+ return self.skill == MEMORY_COMPILE_SKILL+@model_validator(mode="before")@classmethoddef _accept_reason(cls, data: Any) -> Any:@@ -70,6 +79,12 @@ class CompileRequest(BaseModel):raise ValueError("to must not be empty")if not self.skill:raise ValueError("skill must not be empty")+ # Memory mode consolidates the --to space in place and takes no sources;+ # every other skill still requires at least one --from directory.+ if not self.is_memory_mode and not self.from_:+ raise ValueError("from must contain at least one directory")+ if self.is_memory_mode and self.from_:+ raise ValueError("--skill memory consolidates --to in place and takes no --from")return self@@ -264,6 +279,11 @@ class CompileService:self._tasks = tasksself._fs = fsself._local_endpoint: _CompileEndpoint | None = None+ self._memory_runner: MemoryCompileRunner | None = None++ def configure_memory_runner(self, vikingdb: Any = None) -> None:+ """Install the in-process memory-consolidation runner for `--skill memory`."""+ self._memory_runner = MemoryCompileRunner(self._fs, vikingdb=vikingdb)@propertydef poll_interval_seconds(self) -> float:@@ -318,6 +338,16 @@ class CompileService:ctx: RequestContext,idempotency_key: str | None = None,) -> TaskRecord:+ if request.is_memory_mode:+ if self._memory_runner is None:+ raise UnavailableError(+ "memory compile", "memory consolidation runner is not configured"+ )+ return await self._memory_runner.create(+ target=request.to,+ instruction=request.instruction,+ ctx=ctx,+ )request = self._normalize_request_uris(request, ctx)if idempotency_key:payload, private_payload = self._split_payload(request)diff --git a/openviking/service/core.py b/openviking/service/core.pyindex 41561174c..b94398aed 100644--- a/openviking/service/core.py+++ b/openviking/service/core.py@@ -600,6 +600,7 @@ class OpenVikingService:vikingdb=self._vikingdb_manager,viking_fs=self._viking_fs,)+ self._compile_service.configure_memory_runner(vikingdb=self._vikingdb_manager)if self._queue_manager:for queue_name in (diff --git a/openviking/service/memory_compile.py b/openviking/service/memory_compile.pynew file mode 100644index 000000000..25ae7e491--- /dev/null+++ b/openviking/service/memory_compile.py@@ -0,0 +1,374 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""In-process memory consolidation for `ov compile --skill memory`.++This runs the memory framework (ExtractLoop + MemoryUpdater) directly inside+OpenViking core, without the VikingBot agent path. It borrows the lightweight+`session.commit` task shape: create a tracked task, run one background+``asyncio.Task``, and record running/completed/failed via the task tracker.+There is no QueueFS re-delivery; a crashed run leaves the task failed.+"""++from __future__ import annotations++import asyncio+from typing import Any, Optional++from openviking.core.namespace import classify_uri, uri_parts+from openviking.core.path_variables import resolve_path_variables+from openviking.core.uri_validation import validate_request_viking_uri+from openviking.server.identity import RequestContext+from openviking.service.fs_service import FSService+from openviking.service.task_tracker import TaskRecord, get_task_tracker+from openviking.service.task_work_index import bind_task_context+from openviking.session.memory.consolidation_context_provider import (+ ConsolidationExtractContextProvider,+ build_consolidation_isolation_handler,+)+from openviking.session.memory.extract_loop import ExtractLoop+from openviking.session.memory.memory_type_registry import MemoryTypeRegistry, get_default_registry+from openviking.session.memory.memory_updater import MemoryUpdater+from openviking.session.memory.streaming_memory_updater import (+ acquire_memory_operation_lease,+)+from openviking.session.memory.utils.uri import render_template+from openviking.telemetry import tracer+from openviking.telemetry.span_models import create_root_span_attributes+from openviking_cli.exceptions import InvalidArgumentError, NotFoundError+from openviking_cli.utils import get_logger+from openviking_cli.utils.config import get_openviking_config++try:+ from openviking.observability.context import (+ bind_root_observability_context,+ reset_root_observability_context,+ )+except ImportError: # pragma: no cover - observability optional+ bind_root_observability_context = None+ reset_root_observability_context = None++logger = get_logger(__name__)+++def _peer_id_from_memory_uri(uri: str) -> Optional[str]:+ """Return the peer_id when a memory URI lives under a peer space, else None."""+ parts = uri_parts(uri)+ if len(parts) >= 5 and parts[0] == "user" and parts[2] == "peers":+ return parts[3]+ return None+++def _memory_type_from_target(uri: str) -> Optional[str]:+ classification = classify_uri(uri)+ parts = uri_parts(uri)+ if classification.context_type != "memory" or classification.content_index is None:+ raise InvalidArgumentError(+ "Memory-mode Compile target must be a memories root or inside a memory type directory"+ )+ if classification.is_memory_root:+ return None+ return parts[classification.content_index + 1]+++async def _discover_memory_types(+ target: str,+ registry: MemoryTypeRegistry,+ viking_fs: Any,+ ctx: RequestContext,+) -> list[str]:+ parts = uri_parts(target)+ user_space = "/".join(parts[1:-1])+ peer_id = _peer_id_from_memory_uri(target)+ memory_types = []+ for schema in registry.list_all():+ if not schema.directory or (peer_id and not schema.peer_enabled):+ continue+ directory = render_template(schema.directory, {"user_space": user_space}).rstrip("/")+ if directory != target and not directory.startswith(f"{target}/"):+ continue+ path = directory+ is_directory = schema.filename_has_variables()+ if not is_directory:+ path = f"{directory}/{schema.filename_template.lstrip('/')}"+ try:+ stat = await viking_fs.stat(path, ctx=ctx, skip_count=True)+ except NotFoundError:+ continue+ if bool(stat.get("isDir")) == is_directory:+ memory_types.append(schema.memory_type)+ return sorted(memory_types)+++class MemoryCompileRunner:+ """Own the validation and background execution of memory-mode Compile tasks."""++ task_type = "compile"+ task_id_prefix = "cmp_"++ def __init__(self, fs: FSService, vikingdb: Any = None) -> None:+ self._fs = fs+ self._vikingdb = vikingdb+ # Keep strong references so background tasks are not garbage collected.+ self._running: set[asyncio.Task[None]] = set()++ def set_vikingdb(self, vikingdb: Any) -> None:+ self._vikingdb = vikingdb++ async def create(+ self,+ *,+ target: str,+ instruction: Optional[str],+ ctx: RequestContext,+ ) -> TaskRecord:+ canonical_target, memory_type, peer_id = await self._normalize_target(target, ctx)+ tracker = get_task_tracker()+ task = await tracker.create(+ self.task_type,+ resource_id=canonical_target,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ meta={+ "request": {+ "to": canonical_target,+ "skill": "memory",+ "memory_type": memory_type,+ "peer_id": peer_id,+ "instruction": instruction,+ }+ },+ )+ await tracker.update_stage(+ task.task_id,+ "queued",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ background = asyncio.create_task(+ self._run(+ task_id=task.task_id,+ target=canonical_target,+ memory_type=memory_type,+ peer_id=peer_id,+ instruction=instruction,+ ctx=ctx,+ )+ )+ self._running.add(background)+ background.add_done_callback(self._running.discard)+ current = await tracker.get(+ task.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ return current or task++ async def _normalize_target(+ self,+ target: str,+ ctx: RequestContext,+ ) -> tuple[str, Optional[str], Optional[str]]:+ uri = validate_request_viking_uri(+ resolve_path_variables(target),+ ctx,+ field_name="to",+ ).rstrip("/")+ memory_type = _memory_type_from_target(uri)+ await self._fs.ensure_write_access(uri, ctx)+ stat = await self._fs.stat(uri, ctx)+ if not stat.get("isDir"):+ raise InvalidArgumentError("Memory-mode Compile target must be a directory")+ canonical = str(stat.get("uri") or uri).rstrip("/")+ return canonical, memory_type, _peer_id_from_memory_uri(canonical)++ async def _run(+ self,+ *,+ task_id: str,+ target: str,+ memory_type: Optional[str],+ peer_id: Optional[str],+ instruction: Optional[str],+ ctx: RequestContext,+ ) -> None:+ tracker = get_task_tracker()+ tracker.register_running_task(task_id)+ root_token = None+ if bind_root_observability_context is not None:+ root_attrs = create_root_span_attributes(+ http_method="TASK",+ http_route="/compile/memory",+ request_id=task_id,+ url_path=target,+ )+ root_attrs.account_id = ctx.account_id+ root_attrs.user_id = ctx.user.user_id+ root_token = bind_root_observability_context(root_attrs)+ try:+ with tracer.start_as_current_span(name="compile.memory.consolidate"):+ trace_id = tracer.get_trace_id() or ""+ await tracker.start(+ task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ stage="consolidating",+ )+ with bind_task_context(task_id, ctx.account_id, ctx.user.user_id):+ result = await self._consolidate(+ target=target,+ memory_type=memory_type,+ peer_id=peer_id,+ instruction=instruction,+ ctx=ctx,+ )+ result = {"to": target, "skill": "memory", "trace_id": trace_id, **result}+ if result["errors"]:+ if not any(result[key] for key in ("adds", "updates", "deletes")):+ await tracker.fail(+ task_id,+ "MEMORY_CONSOLIDATION_FAILED: No memory changes succeeded; "+ "see result.errors for details",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ result=result,+ )+ return+ logger.warning(+ "Memory-mode compile %s partially succeeded: %s",+ task_id,+ result["errors"],+ )+ await tracker.complete(+ task_id,+ result,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ except asyncio.CancelledError:+ return+ except Exception as exc: # noqa: BLE001 - surface any failure as task error+ tracer.error(f"Memory-mode compile failed: {exc}")+ await tracker.fail(+ task_id,+ f"MEMORY_CONSOLIDATION_FAILED: {exc}",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ finally:+ if root_token is not None and reset_root_observability_context is not None:+ reset_root_observability_context(root_token)+ await tracker.unregister_running_task(task_id)++ async def _consolidate(+ self,+ *,+ target: str,+ memory_type: Optional[str],+ peer_id: Optional[str],+ instruction: Optional[str],+ ctx: RequestContext,+ ) -> dict[str, Any]:+ default_registry = get_default_registry()+ viking_fs = self._fs._ensure_initialized()+ from openviking.session.memory.account_templates import resolve_account_memory_registry++ registry = await resolve_account_memory_registry(+ viking_fs, ctx.account_id, default_registry+ )+ memory_types = (+ [memory_type]+ if memory_type is not None+ else await _discover_memory_types(target, registry, viking_fs, ctx)+ )+ result = {+ "memory_type": memory_type,+ "memory_types": memory_types,+ "adds": [],+ "updates": [],+ "deletes": [],+ "total_adds": 0,+ "total_updates": 0,+ "total_deletes": 0,+ "errors": [],+ }+ if not memory_types:+ return result+ config = get_openviking_config()+ vlm = config.vlm.get_vlm_instance()+ provider = ConsolidationExtractContextProvider(+ memory_types=memory_types,+ target_directory=target,+ instruction=instruction,+ memory_registry=registry,+ ctx=ctx,+ viking_fs=viking_fs,+ )++ extract_context = provider.get_extract_context()+ isolation_handler = build_consolidation_isolation_handler(+ ctx,+ extract_context,+ memory_types=memory_types,+ peer_id=peer_id,+ )+ isolation_handler.prepare_messages()+ provider.bind_isolation_handler(isolation_handler)+ await provider.prepare_extraction_messages()++ orchestrator = ExtractLoop(+ vlm=vlm,+ viking_fs=viking_fs,+ ctx=ctx,+ context_provider=provider,+ isolation_handler=isolation_handler,+ # Compile is offline and user-initiated; allow a longer agentic loop+ # so the model can ls/search/read across the directory before writing.+ max_iterations=10,+ )+ operations, _tools_used = await orchestrator.run()+ if operations is None:+ return result++ lease = await acquire_memory_operation_lease(operations, viking_fs, ctx)+ try:+ updater = MemoryUpdater(+ registry=registry,+ vikingdb=self._vikingdb,+ transaction_handle=lease,+ )+ apply_result = await updater.apply_operations(+ operations,+ ctx,+ extract_context=extract_context,+ isolation_handler=isolation_handler,+ )+ finally:+ if lease is not None:+ await viking_fs._async_agfs.pathlock_release(lease)++ # Classify each touched URI as add vs update using the files the model+ # actually read. ExtractLoop's write-before-read guard guarantees any+ # modified pre-existing file is in read_file_contents, so a written URI+ # absent from it is a genuinely new file.+ before_uris = set(provider.read_file_contents.keys())+ adds: list[str] = []+ updates: list[str] = []+ for uri in apply_result.written_uris:+ (updates if uri in before_uris else adds).append(uri)+ for uri in apply_result.edited_uris:+ if uri not in updates:+ updates.append(uri)+ deletes = list(apply_result.deleted_uris)+ return {+ **result,+ "adds": adds,+ "updates": updates,+ "deletes": deletes,+ "total_adds": len(adds),+ "total_updates": len(updates),+ "total_deletes": len(deletes),+ "errors": [str(err) for _uri, err in apply_result.errors],+ }+++__all__ = ["MemoryCompileRunner"]diff --git a/openviking/session/memory/__init__.py b/openviking/session/memory/__init__.pyindex 16170c25a..a3f212f89 100644--- a/openviking/session/memory/__init__.py+++ b/openviking/session/memory/__init__.py@@ -7,6 +7,10 @@ This module provides a YAML-configurable memory templating system withReAct (Reasoning + Action) pattern for memory updates."""+from openviking.session.memory.consolidation_context_provider import (+ ConsolidationExtractContextProvider,+ build_consolidation_isolation_handler,+)from openviking.session.memory.dataclass import (MemoryData,MemoryField,@@ -62,6 +66,8 @@ __all__ = ["StructuredMemoryOperations","PatchMergeContextProvider","PatchMergePatch",+ "ConsolidationExtractContextProvider",+ "build_consolidation_isolation_handler",# Registry"MemoryTypeRegistry",# Schema modelsdiff --git a/openviking/session/memory/consolidation_context_provider.py b/openviking/session/memory/consolidation_context_provider.pynew file mode 100644index 000000000..fb9a5036a--- /dev/null+++ b/openviking/session/memory/consolidation_context_provider.py@@ -0,0 +1,264 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Consolidate selected memory types in one isolation space and extraction loop."""++from __future__ import annotations++import re+from typing import Any, Dict, List, Optional++from openviking.server.identity import RequestContext+from openviking.session.memory.dataclass import MemoryTypeSchema+from openviking.session.memory.memory_isolation_handler import MemoryIsolationHandler+from openviking.session.memory.memory_type_registry import MemoryTypeRegistry+from openviking.session.memory.merge_policy import MEMORY_MERGE_POLICY+from openviking.session.memory.session_extract_context_provider import (+ SessionExtractContextProvider,+)+from openviking.session.memory.tools import add_tool_call_pair_to_messages, get_tool+from openviking.session.memory.utils.language import resolve_output_language_from_text+from openviking.storage.viking_fs import VikingFS+from openviking_cli.exceptions import NotFoundError+from openviking_cli.utils.config import get_openviking_config++_RESERVED_NAMES = {".overview.md", ".abstract.md"}+_LANGUAGE_SAMPLE_FILES = 3+_LANGUAGE_SAMPLE_BYTES = 4096+_MEMORY_FIELDS_START = re.compile(r"<!--\s*MEMORY_FIELDS\b")+++class ConsolidationExtractContextProvider(SessionExtractContextProvider):+ """Reorganize selected memory types using a recursive inventory and read/search tools."""++ include_tool_parts_in_conversation = False+ split_long_text_messages_for_extraction = False++ def __init__(+ self,+ *,+ memory_types: List[str],+ target_directory: Optional[str] = None,+ instruction: Optional[str] = None,+ memory_registry: Optional[MemoryTypeRegistry] = None,+ output_language: Optional[str] = None,+ ctx: Optional[RequestContext] = None,+ viking_fs: Optional[VikingFS] = None,+ ):+ super().__init__(messages=[], ctx=ctx, viking_fs=viking_fs, memory_registry=memory_registry)+ self.memory_types = list(dict.fromkeys(memory_types))+ # Canonical `--to` directory. When provided, prefetch lists exactly this+ # directory instead of re-deriving it from ctx.user_id, which can be+ # empty/wrong and collapse the URI (viking://user//memories/...).+ self.target_directory = target_directory.rstrip("/") if target_directory else None+ self._instruction_text = (instruction or "").strip()+ self._output_language = resolve_output_language_from_text(+ "", fallback_language=output_language or "en"+ )+ self._language_resolved = bool(+ output_language or get_openviking_config().output_language_override.strip()+ )++ def bind_isolation_handler(self, isolation_handler: MemoryIsolationHandler) -> None:+ self._isolation_handler = isolation_handler++ def get_memory_schemas(self, ctx: RequestContext) -> List[MemoryTypeSchema]:+ del ctx+ schemas = []+ for memory_type in self.memory_types:+ schema = self._get_registry().get(memory_type)+ if schema is None or not schema.enabled:+ raise ValueError(f"Memory schema not found or disabled: {memory_type}")+ schemas.append(schema)+ return schemas++ def get_tools(self) -> List[str]:+ # Compile is an offline, user-initiated task, so favor an agentic loop:+ # give the model ls/search/read and let it explore the target directory+ # (including subdirectories) itself instead of relying on a one-shot,+ # possibly-incomplete prefetch. ExtractLoop's write-before-read guard+ # still forces a read of any file it intends to modify.+ return ["ls", "search", "read"]++ # ── Instruction ──++ def instruction(self) -> str:+ output_language = self._output_language+ extra = ""+ if self._instruction_text:+ extra = f"\n\n## User Instruction\n{self._instruction_text}\n"+ memory_types = ", ".join(self.memory_types)+ target = self.target_directory or "the selected memory directories"+ return f"""You are a memory consolidation agent. Reorganize the existing \+memories of these types: {memory_types}, under {target} in place so the final collection is \+clean, non-redundant, and conforms to each memory type's own schema.++## Workflow+1. Call `ls` with `recursive=true` on the target directory ONCE to see every file+ (including files inside subdirectories) as `relative/path size`.+2. `read` the full content of every file you may merge, split, edit, or delete.+ You MUST read a file before changing it. Do not repeatedly `ls` the same path.+3. Output ONLY the memory operations (no extra text) once you have gathered enough+ context.++## What to do+- Merge only memories that share the same identity under the schema.+- Split files that mix multiple identities.+- Deduplicate repeated or paraphrased facts, preserving every distinct atomic fact.+- Normalize fields to each memory's own schema; do not invent new facts.+- Only modify the selected memory types under the target. Do not merge different types+ or move memories into another user's or peer's space.++## Critical+- Only ls/search/read are available for exploration - there is no write tool; all+ changes are expressed as the final operations.+- Do NOT create memories from nothing; only reorganize what already exists.+- The system generates URIs from memory_type and fields; just provide correct fields.++## Target Output Language+All memory content MUST be written in {output_language}.+{extra}+{MEMORY_MERGE_POLICY}+"""++ # ── Prefetch: seed the loop with a recursive file listing, not content ──++ async def prefetch(self) -> List[Dict[str, Any]]:+ directories = self._target_directories()+ prefetch_messages: List[Dict[str, Any]] = []+ call_id = 0+ # Seed with a recursive ls of each target directory so the model sees every+ # file (including those in subdirectories) up front and can go straight to+ # read, instead of re-listing directories. Content is not fetched here.+ for directory in directories:+ listing = await self._recursive_listing(directory)+ add_tool_call_pair_to_messages(+ messages=prefetch_messages,+ call_id=call_id,+ tool_name="ls",+ params={"uri": directory, "recursive": True},+ result=listing,+ )+ call_id += 1++ prefetch_messages.append(+ {+ "role": "user",+ "content": (+ f"Above is a recursive listing for memory types {', '.join(self.memory_types)} "+ "(relative paths, no content). read the full content of "+ "every file you intend to merge, split, edit, or delete (you MUST "+ "read a file before changing it); use search only if you need more "+ "context. Then output ALL consolidation operations in a single "+ "response. If nothing needs reorganizing, return an empty operation set."+ ),+ }+ )+ return prefetch_messages++ async def _recursive_listing(self, directory: str) -> Any:+ """Return a recursive 'relative/path size' listing via the shared ls tool."""+ tool = get_tool("ls")+ if tool is None:+ from openviking.session.memory.tools import MemoryLsTool++ tool = MemoryLsTool()+ return await tool.execute(self.create_tool_context(), uri=directory, recursive=True)++ def _target_directories(self) -> List[str]:+ if self.target_directory:+ return [self.target_directory]+ if self._isolation_handler is not None:+ return list(+ dict.fromkeys(+ directory+ for schema in self.get_memory_schemas(self._ctx)+ for directory in self._isolation_handler.render_schema_directories(schema)+ )+ )+ return []++ async def prepare_extraction_messages(self) -> None:+ if self._language_resolved:+ return+ config = get_openviking_config()+ if config.output_language_override.strip():+ self._output_language = resolve_output_language_from_text("", config=config)+ self._language_resolved = True+ return+ samples: list[str] = []+ sampled_uris: set[str] = set()+ for directory in self._target_directories():+ if len(sampled_uris) >= _LANGUAGE_SAMPLE_FILES:+ break+ listing = await self._viking_fs.glob(+ "**/*.md",+ uri=directory,+ node_limit=_LANGUAGE_SAMPLE_FILES - len(sampled_uris),+ extra_fields=[],+ ctx=self._ctx,+ )+ for entry in listing["matches"]:+ uri = entry["uri"]+ if (+ entry.get("isDir")+ or uri.rsplit("/", 1)[-1] in _RESERVED_NAMES+ or uri in sampled_uris+ ):+ continue+ sampled_uris.add(uri)+ try:+ # A language sample must not satisfy ExtractLoop's read-before-write guard.+ raw = await self._viking_fs.read(+ uri, size=_LANGUAGE_SAMPLE_BYTES, ctx=self._ctx+ )+ except NotFoundError:+ continue+ text = raw.decode("utf-8", errors="ignore")+ # The byte cap can cut the metadata comment before its closing delimiter.+ samples.append(_MEMORY_FIELDS_START.split(text, maxsplit=1)[0])+ self._output_language = resolve_output_language_from_text(+ "\n".join(samples), config=config, fallback_language="en"+ )+ self._language_resolved = True++ def get_output_language(self) -> str:+ return self._output_language++ def _get_registry(self) -> MemoryTypeRegistry:+ if self._registry is None:+ self._registry = MemoryTypeRegistry(load_schemas=True)+ return self._registry+++def build_consolidation_isolation_handler(+ ctx: RequestContext,+ extract_context: Any,+ *,+ memory_types: List[str],+ peer_id: Optional[str],+) -> MemoryIsolationHandler:+ """Build the isolation handler for one consolidation space.++ ``peer_id=None`` consolidates the caller's self memory; a set ``peer_id``+ consolidates only that peer's space. There is no cross-space mixing.+ """+ if peer_id:+ return MemoryIsolationHandler(+ ctx,+ extract_context,+ allowed_memory_types=set(memory_types),+ allow_self=False,+ allowed_peer_ids={peer_id},+ )+ return MemoryIsolationHandler(+ ctx,+ extract_context,+ allowed_memory_types=set(memory_types),+ allow_self=True,+ )+++__all__ = [+ "ConsolidationExtractContextProvider",+ "build_consolidation_isolation_handler",+]diff --git a/openviking/session/memory/extract_loop.py b/openviking/session/memory/extract_loop.pyindex 189923d84..38c6896ae 100644--- a/openviking/session/memory/extract_loop.py+++ b/openviking/session/memory/extract_loop.py@@ -30,11 +30,18 @@ from openviking.session.memory.extraction_output_protocol import (create_extraction_output_protocol,)from openviking.session.memory.memory_isolation_handler import MemoryIsolationHandler-from openviking.session.memory.merge_op import FieldType, ImmutableOp, MergeOp, PatchOp+from openviking.session.memory.merge_op import (+ FieldType,+ ImmutableOp,+ MergeOp,+ MergeOpFactory,+ PatchOp,+)from openviking.session.memory.page_id_map import ResponsePageIdAllocatorfrom openviking.session.memory.schema_model_generator import SchemaModelGeneratorfrom openviking.session.memory.tools import MEMORY_TOOLS_REGISTRYfrom openviking.session.memory.utils.json_parser import JsonUtils+from openviking.session.memory.utils.uri import generate_urifrom openviking.storage.viking_fs import VikingFS, get_viking_fsfrom openviking.telemetry import bind_telemetry_stage, tracerfrom openviking_cli.utils import get_logger@@ -863,11 +870,24 @@ class ExtractLoop:for field in schema.fieldsif field.merge_op == MergeOp.IMMUTABLE}- for field_name in immutable_fields:- if ImmutableOp.is_set(old_content.extra_fields.get(field_name)):- resolved_op.memory_fields[field_name] = (- old_content.extra_fields[field_name]- )+ preserved_fields = set(immutable_fields)+ preserved_fields.update(schema.identity_fields(include_peer_id=False))+ for field_name in preserved_fields:+ old_value = old_content.extra_fields.get(field_name)+ if not ImmutableOp.is_set(old_value):+ continue+ new_value = resolved_op.memory_fields.get(field_name)+ if field_name in immutable_fields or not ImmutableOp.is_set(+ new_value+ ):+ resolved_op.memory_fields[field_name] = old_value+ target_uri = await self._updated_uri_for_existing_operation(+ resolved_op,+ schema=schema,+ source_uri=resolved_uri,+ )+ if target_uri != resolved_uri:+ resolved_op.uris = [target_uri]else:resolved_op.uris = self._isolation_handler.calculate_memory_uris(memory_type_schema=schema,@@ -883,8 +903,8 @@ class ExtractLoop:upsert_operations.append(resolved_op)- delete_ids = self._normalize_delete_ids(getattr(operations, "delete_ids", []) or [])delete_replacements: dict[str, str] = {}+ delete_ids = self._normalize_delete_ids(getattr(operations, "delete_ids", []) or [])for delete_id in delete_ids:if delete_id.delete_page_id is None or page_id_map is None:continue@@ -918,14 +938,19 @@ class ExtractLoop:delete_id.replacement_page_id,)continue- replacement_uri = page_id_map.resolve(replacement_page_id)+ replacement_uri = next(+ (+ op.uris[0]+ for op in upsert_operations+ if op.page_id == replacement_page_id and op.uris+ ),+ None,+ )if not replacement_uri:- for op in upsert_operations:- if op.page_id == replacement_page_id and op.uris:- replacement_uri = op.uris[0]- break+ replacement_uri = page_id_map.resolve(replacement_page_id)- delete_file_contents.append(old_content)+ if all(file.uri != old_content.uri for file in delete_file_contents):+ delete_file_contents.append(old_content)if replacement_uri and replacement_uri != delete_uri:delete_replacements[delete_uri] = replacement_uri@@ -958,6 +983,56 @@ class ExtractLoop:return resolved, raw_links+ async def _updated_uri_for_existing_operation(+ self,+ operation: ResolvedOperation,+ *,+ schema: Any,+ source_uri: str,+ ) -> str:+ """Recompute an existing object's URI after mutable identity-field updates."""+ old_content = operation.old_memory_file_content+ if old_content is None:+ return source_uri++ uri_fields = dict(old_content.extra_fields or {})+ schema_fields = {field.name: field for field in schema.fields}+ identity_field_changed = False+ for field_name in schema.identity_fields(include_peer_id=False):+ field = schema_fields.get(field_name)+ if field is None or field_name not in operation.memory_fields:+ continue+ current_value = (+ old_content.plain_content()+ if field_name == "content"+ else old_content.extra_fields.get(field_name)+ )+ try:+ new_value = await MergeOpFactory.from_field(field).apply(+ current_value,+ operation.memory_fields[field_name],+ )+ except Exception:+ new_value = current_value+ if new_value != current_value:+ identity_field_changed = True+ uri_fields[field_name] = new_value+ if not identity_field_changed:+ return source_uri++ prefix = "viking://user/"+ namespace, separator, _ = source_uri.partition("/memories/")+ if not separator or not namespace.startswith(prefix):+ return source_uri+ user_space = namespace.removeprefix(prefix)+ candidate_uri = generate_uri(+ memory_type=schema,+ fields=uri_fields,+ user_space=user_space,+ extract_context=self._extract_context,+ )+ return candidate_uri if candidate_uri != source_uri else source_uri+def _normalize_delete_ids(self, raw_delete_ids: List[Any]) -> List[DeleteId]:delete_ids: List[DeleteId] = []for raw in raw_delete_ids:@@ -1024,7 +1099,7 @@ class ExtractLoop:if page_id is None:continueuri = page_id_map.resolve(page_id)- if uri:+ if uri and page_id not in op_page_map:page_uri_map.setdefault(page_id, [])if uri not in page_uri_map[page_id]:page_uri_map[page_id].insert(0, uri)diff --git a/openviking/session/memory/extraction_output_protocol/python_protocol.py b/openviking/session/memory/extraction_output_protocol/python_protocol.pyindex d47b8faa2..cdab147dc 100644--- a/openviking/session/memory/extraction_output_protocol/python_protocol.py+++ b/openviking/session/memory/extraction_output_protocol/python_protocol.py@@ -23,6 +23,7 @@ from openviking.session.memory.merge_op import (FieldType,ImmutableOp,MergeOp,+ MergeOpFactory,SearchReplaceBlock,StrPatch,)@@ -92,7 +93,7 @@ _CONTRACT_PREAMBLE = ("Prose frequently contains apostrophes (e.g. Evan's), quotes, colons, or dates that break "'single- or double-quoted literals. Inside triple quotes, escape any literal """ and '"backslash; never put a real newline inside a single- or double-quoted string.",- "Only keyword arguments are accepted by create, set, and obj.update(); a field's update() takes one positional string. Unknown business fields are ignored.",+ "Only keyword arguments are accepted by create, set, and obj.update(); a field's update() takes one positional string.","You may end the program with sdk.commit(); when present it must be the final call. If there are no changes, return only sdk.commit().","Use the system-provided existing-object variable names exactly as shown. When a newly ""created memory must be referenced by delete(replacement=...) or link(...), assign the "@@ -155,6 +156,7 @@ class _FieldHandle:field_name: strblocks: list[Any] = field(default_factory=list)full_value: Any = _UNSET+ is_noop: bool = Falseclass PythonExtractionOutputProtocol(ExtractionOutputProtocol):@@ -215,12 +217,18 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):)for field in schema.fields}+ schema_fields = {field.name: field for field in schema.fields}for name, _type_name, description in fields:if name in merge_ops:# Render only YAML field descriptions, using the same context and# restricted renderer as JSON. Keep the DSL's own edit instructions# instead of copying the JSON model's merge-operation wrappers.description = render_description_template(description, context.template_context)+ field = schema_fields[name]+ if field.merge_op == MergeOp.REPLACE:+ description = MergeOpFactory.from_field(field).get_output_schema_description(+ description+ )normalized_description = " ".join(str(description or "").split())qualifier = f" [{merge_ops[name]}]" if name in merge_ops else ""lines.append(f" - {_identifier_alias(name)}{qualifier}: {normalized_description}")@@ -263,7 +271,10 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):- Update or delete an existing memory only through its system-provided bound object; never pass or construct a URI.- Create collection memories with listed sdk.create_<memory_type>(...) methods.- Set a single-file memory with its listed sdk.set_<memory_type>(...) method; each target scope has only one such object.-- Existing-object identity, storage paths, and immutable fields are preserved by the system.+- Existing-object immutable fields are preserved by the system. URI identity fields may be+ updated when their schema allows it; changing one renames the memory object.+- If a rename target already exists, do not rename over it. Read both objects, update the+ canonical target with every distinct fact, then delete the source with replacement=target.- delete() removes the whole object; use obj.content.drop(text=...) (with the real field name) when only some content must go and the rest stays.- For canonical merges, use duplicate.delete(replacement=canonical); for pure deletes, call delete() without replacement.- delete(replacement=canonical) discards the duplicate's content entirely and keeps only the canonical. Before deleting a duplicate, first fold every distinct valid fact it holds into the canonical (e.g. canonical.content.edit(...)); merging or compacting must never drop a unique fact that only the duplicate recorded.@@ -758,19 +769,11 @@ class _PythonProgramCompiler:alias_map = self._field_alias_to_real.get(owner.memory_type, {})real_field = alias_map.get(node.attr, node.attr)if node.attr.startswith("_") or real_field not in owner.fields:- available = (- ", ".join(sorted(_identifier_alias(name) for name in owner.fields)) or "(none)"- )- hint = (- " Use the real field name (e.g. content), not the literal word 'field'."- if node.attr == "field"- else ""- )- self._error(- node,- f"memory field {node.attr!r} is unavailable; editable fields on "- f"{owner.name or owner.memory_type}: {available}.{hint}",- )+ # Unknown field: mirror the tolerance kwargs already have on+ # create/set/update. Return a no-op handle so the whole+ # program keeps compiling; the offending statement produces+ # no effect on server state.+ return _FieldHandle(owner=owner, field_name=real_field, is_noop=True)# obj.<field> is a write handle, not the raw value: it exposes# .update()/.edit()/.drop() and cannot be read as a string.return _FieldHandle(owner=owner, field_name=real_field)@@ -1051,6 +1054,10 @@ class _PythonProgramCompiler:)def _call_field(self, handle: _FieldHandle, method: str, node: ast.Call) -> _FieldHandle:+ if handle.is_noop:+ # Unknown field: swallow the whole edit chain so the offending+ # statement compiles without touching server state.+ return handleif method == "update":kwargs = self._eval_keywords(node)if kwargs or len(node.args) != 1:@@ -1096,6 +1103,9 @@ class _PythonProgramCompiler:def _apply_field_handle(self, handle: _FieldHandle, node: ast.AST) -> None:owner = handle.ownername = handle.field_name+ if handle.is_noop:+ # Unknown field: skip application entirely.+ returnschema = self.schemas[owner.memory_type]field_schema = {item.name: item for item in schema.fields}.get(name)if handle.full_value is _UNSET and not handle.blocks:@@ -1212,10 +1222,15 @@ class _PythonProgramCompiler:fields = dict(obj.changed_fields)if obj.existing:schema = self.schemas[obj.memory_type]- for memory_field in schema.fields:- if memory_field.merge_op == MergeOp.IMMUTABLE:- if memory_field.name in obj.fields:- fields[memory_field.name] = obj.fields[memory_field.name]+ required_existing_fields = set(schema.identity_fields(include_peer_id=False))+ required_existing_fields.update(+ memory_field.name+ for memory_field in schema.fields+ if memory_field.merge_op == MergeOp.IMMUTABLE+ )+ for field_name in required_existing_fields:+ if field_name in obj.fields and field_name not in fields:+ fields[field_name] = obj.fields[field_name]payload[obj.memory_type].append({"page_id": obj.page_id, **fields})if self.context.link_enabled:payload["links"] = []diff --git a/openviking/session/memory/memory_updater.py b/openviking/session/memory/memory_updater.pyindex 93297c26b..27927a829 100644--- a/openviking/session/memory/memory_updater.py+++ b/openviking/session/memory/memory_updater.py@@ -49,7 +49,7 @@ from openviking.telemetry.request_wait_tracker import get_request_wait_trackerfrom openviking.telemetry.tracer import get_trace_idfrom openviking.utils.ingest_options import IngestOptionsfrom openviking.utils.time_utils import parse_iso_datetime-from openviking_cli.exceptions import NotFoundError+from openviking_cli.exceptions import ConflictError, NotFoundErrorfrom openviking_cli.utils import VikingURI, get_loggerlogger = get_logger(__name__)@@ -882,6 +882,17 @@ class MemoryUpdater:result.add_error("unknown", ValueError(error))return result+ migration_errors = self._materialize_uri_migrations(operations)+ if migration_errors:+ for source_uri, error in migration_errors:+ result.add_error(source_uri, error)+ return result+ migration_errors = await self._validate_uri_migrations(operations, ctx)+ if migration_errors:+ for source_uri, error in migration_errors:+ result.add_error(source_uri, error)+ return result+applicable_upserts: List[ResolvedOperation] = []has_unresolved_upserts = Falsefor resolved_op in operations.upsert_operations:@@ -923,6 +934,13 @@ class MemoryUpdater:f"Skipping unresolved memory operation: {error_target}: {resolution_error}")+ uri_remap = dict(getattr(operations, "delete_replacements", {}) or {})+ original_links = list(getattr(operations, "resolved_links", []) or [])+ operations.resolved_links = remap_stored_links(+ original_links,+ uri_remap,+ )+# Distribute resolved_links to corresponding upsert operationsself._distribute_links_to_operations(operations)@@ -934,9 +952,13 @@ class MemoryUpdater:ctx,extract_context=extract_context,lease_ref=self._transaction_handle,+ uri_remap=uri_remap,)# Add all uris to result (uris is List[str])- if resolved_op.is_edit():+ if self._is_uri_migration(resolved_op):+ for uri in resolved_op.uris:+ result.add_written(uri)+ elif resolved_op.is_edit():for uri in resolved_op.uris:result.add_edited(uri)else:@@ -950,16 +972,39 @@ class MemoryUpdater:for uri in resolved_op.uris:result.add_error(uri, e)- operations.resolved_links = remap_stored_links(- list(getattr(operations, "resolved_links", []) or []),- dict(getattr(operations, "delete_replacements", {}) or {}),+ failed_upsert_uris = {uri for uri, _error in result.errors}+ active_uri_remap = {+ source_uri: target_uri+ for source_uri, target_uri in uri_remap.items()+ if target_uri not in failed_upsert_uris+ }+ invalid_replacement_sources = await self._invalid_replacement_sources(+ active_uri_remap,+ successful_upsert_uris=set(result.written_uris + result.edited_uris),+ scheduled_delete_uris={+ file.uri for file in operations.delete_file_contents if file.uri+ },+ result=result,+ ctx=ctx,)- await self._inherit_deleted_link_relations(+ active_uri_remap = {+ source_uri: target_uri+ for source_uri, target_uri in active_uri_remap.items()+ if source_uri not in invalid_replacement_sources+ }+ operations.delete_replacements = active_uri_remap+ operations.resolved_links = remap_stored_links(original_links, active_uri_remap)+ link_migration_failed = not await self._inherit_deleted_link_relations(operations,result,ctx,lease_ref=self._transaction_handle,)+ failed_link_migration_sources = set(active_uri_remap) if link_migration_failed else set()+ if link_migration_failed:+ active_uri_remap = {}+ operations.delete_replacements = {}+ operations.resolved_links = original_links# Apply delete operations (delete_file_contents is List[MemoryFile])# Skip deletes whose URI was just written in the same batch — this happens when the@@ -976,6 +1021,21 @@ class MemoryUpdater:result.add_error(delete_uri, delete_error)tracer.error(f"Skipping delete for {delete_uri}: {delete_error}")continue+ if delete_uri in invalid_replacement_sources:+ continue+ if delete_uri in failed_link_migration_sources:+ delete_error = ValueError("Skipped delete because link migration did not complete")+ result.add_error(delete_uri, delete_error)+ tracer.error(f"Skipping delete for {delete_uri}: {delete_error}")+ continue+ replacement_uri = uri_remap.get(delete_uri)+ if replacement_uri in failed_upsert_uris:+ delete_error = ValueError(+ f"Skipped delete because replacement write failed: {replacement_uri}"+ )+ result.add_error(delete_uri, delete_error)+ tracer.error(f"Skipping delete for {delete_uri}: {delete_error}")+ continueif delete_uri in upserted_uris:tracer.info(f"[apply_operations] skipping delete for {delete_uri}: "@@ -1051,6 +1111,210 @@ class MemoryUpdater:return result+ async def _invalid_replacement_sources(+ self,+ uri_remap: Dict[str, str],+ *,+ successful_upsert_uris: set[str],+ scheduled_delete_uris: set[str],+ result: MemoryUpdateResult,+ ctx: RequestContext,+ ) -> set[str]:+ """Return replacement sources whose final target is cyclic or unavailable."""+ invalid_sources: set[str] = set()+ checked_targets: Dict[str, Exception | None] = {}+ viking_fs = self._get_viking_fs()+ for source_uri in uri_remap:+ target_uri = _resolve_replacement_uri(source_uri, uri_remap)+ if not target_uri or target_uri == source_uri:+ error = ConflictError(+ f"Memory replacement cycle detected for {source_uri}",+ resource=source_uri,+ )+ result.add_error(source_uri, error)+ invalid_sources.add(source_uri)+ continue+ if _same_batch_delete_conflict_key(source_uri) == _same_batch_delete_conflict_key(+ target_uri+ ):+ error = ConflictError(+ "Case-only memory replacement URIs are not portable; choose a distinct "+ f"target name: {source_uri} -> {target_uri}",+ resource=target_uri,+ )+ result.add_error(source_uri, error)+ invalid_sources.add(source_uri)+ continue+ if target_uri in successful_upsert_uris:+ continue+ if target_uri in scheduled_delete_uris:+ error = ConflictError(+ f"Memory replacement target is also scheduled for deletion: {target_uri}",+ resource=target_uri,+ )+ result.add_error(source_uri, error)+ invalid_sources.add(source_uri)+ continue+ if target_uri not in checked_targets:+ try:+ await viking_fs.read_file(target_uri, ctx=ctx)+ checked_targets[target_uri] = None+ except (NotFoundError, FileNotFoundError):+ checked_targets[target_uri] = NotFoundError(target_uri, "replacement memory")+ except Exception as exc:+ checked_targets[target_uri] = exc+ target_error = checked_targets[target_uri]+ if target_error is None:+ continue+ result.add_error(source_uri, target_error)+ invalid_sources.add(source_uri)+ return invalid_sources++ @staticmethod+ def _is_uri_migration(operation: ResolvedOperation) -> bool:+ old_file = operation.old_memory_file_content+ return bool(+ old_file+ and old_file.uri+ and operation.uris+ and any(uri != old_file.uri for uri in operation.uris)+ )++ @classmethod+ def _materialize_uri_migrations(+ cls,+ operations: ResolvedOperations,+ ) -> List[Tuple[str, Exception]]:+ """Translate an update whose URI changed into write-new/delete-old semantics."""+ errors: List[Tuple[str, Exception]] = []+ migration_ops = [+ operation+ for operation in operations.upsert_operations+ if cls._is_uri_migration(operation)+ ]+ if migration_ops and any(+ len(operation.uris) != 1+ or not operation.old_memory_file_content+ or not operation.old_memory_file_content.uri+ for operation in migration_ops+ ):+ return [+ (+ operation.old_memory_file_content.uri,+ ValueError("URI migration requires exactly one source and one target URI"),+ )+ for operation in migration_ops+ ]+ delete_files = {file.uri: file for file in operations.delete_file_contents if file.uri}+ replacements = dict(getattr(operations, "delete_replacements", {}) or {})+ for operation in migration_ops:+ old_file = operation.old_memory_file_content+ source_uri = old_file.uri+ target_uri = operation.uris[0]+ explicit_target = replacements.get(source_uri)+ if explicit_target and explicit_target != target_uri:+ errors.append(+ (+ source_uri,+ ConflictError(+ "Memory rename conflicts with an explicit replacement: "+ f"{source_uri} -> {target_uri} (replacement={explicit_target})",+ resource=source_uri,+ ),+ )+ )+ continue+ delete_files.setdefault(source_uri, old_file)+ replacements[source_uri] = target_uri+ operations.delete_file_contents = list(delete_files.values())+ operations.delete_replacements = replacements+ return errors++ async def _validate_uri_migrations(+ self,+ operations: ResolvedOperations,+ ctx: RequestContext,+ ) -> List[Tuple[str, Exception]]:+ """Reject implicit renames whose destination is already occupied."""+ migrations: List[Tuple[str, str]] = []+ upsert_targets: Dict[str, int] = {}+ for operation in operations.upsert_operations:+ for target_uri in operation.uris:+ upsert_targets[target_uri] = upsert_targets.get(target_uri, 0) + 1+ for operation in operations.upsert_operations:+ old_file = operation.old_memory_file_content+ if old_file is None or not old_file.uri:+ continue+ migrations.extend(+ (old_file.uri, target_uri)+ for target_uri in operation.uris+ if target_uri != old_file.uri+ )+ if not migrations:+ return []++ errors: List[Tuple[str, Exception]] = []+ seen_targets: Dict[str, str] = {}+ viking_fs = self._get_viking_fs()+ for source_uri, target_uri in migrations:+ if _same_batch_delete_conflict_key(source_uri) == _same_batch_delete_conflict_key(+ target_uri+ ):+ errors.append(+ (+ source_uri,+ ConflictError(+ "Case-only memory URI renames are not portable; choose a distinct "+ f"target name: {source_uri} -> {target_uri}",+ resource=target_uri,+ ),+ )+ )+ continue+ if upsert_targets.get(target_uri, 0) > 1:+ errors.append(+ (+ source_uri,+ ConflictError(+ f"Multiple memory operations target rename URI: {target_uri}",+ resource=target_uri,+ ),+ )+ )+ continue+ previous_source = seen_targets.setdefault(target_uri, source_uri)+ if previous_source != source_uri:+ errors.append(+ (+ source_uri,+ ConflictError(+ f"Multiple memories cannot be renamed to the same URI: {target_uri}",+ resource=target_uri,+ ),+ )+ )+ continue+ try:+ content = await viking_fs.read_file(target_uri, ctx=ctx)+ except (NotFoundError, FileNotFoundError):+ continue+ except Exception as exc:+ errors.append((source_uri, exc))+ continue+ if content is not None:+ errors.append(+ (+ source_uri,+ ConflictError(+ "Memory rename target already exists; read both memories, update "+ "the canonical target, then delete the source with replacement: "+ f"{source_uri} -> {target_uri}",+ resource=target_uri,+ ),+ )+ )+ return errors+async def _sync_resource_refs_for_result(self,result: MemoryUpdateResult,@@ -1090,6 +1354,7 @@ class MemoryUpdater:ctx: RequestContext,extract_context: Any = None,lease_ref: Any = None,+ uri_remap: Optional[Dict[str, str]] = None,):"""Apply upsert operation from a flat model."""viking_fs = self._get_viking_fs()@@ -1098,21 +1363,57 @@ class MemoryUpdater:schema = self._registry.get(memory_type)# Process each URI independentlyfor uri in resolved_op.uris:+ source_content = resolved_op.old_memory_file_content+ is_uri_migration = bool(+ source_content and source_content.uri and source_content.uri != uri+ )# Always read from disk first to get the latest content,# so consecutive patches to the same URI see each other's changes.old_content: Optional[MemoryFile] = Nonetry:content = await viking_fs.read_file(uri, ctx=ctx)+ if is_uri_migration:+ raise ConflictError(+ f"Memory rename target already exists: {uri}",+ resource=uri,+ )if content:old_content = MemoryFileUtils.read(content, uri=uri)- except Exception:+ except ConflictError:+ raise+ except (NotFoundError, FileNotFoundError):# File doesn't exist yet, that's okaypass+ except Exception:+ if is_uri_migration:+ raise+ # Preserve the legacy in-place update fallback to the prefetched+ # snapshot when a fresh disk read is unavailable.+ pass+ if is_uri_migration:+ try:+ source_raw = await viking_fs.read_file(source_content.uri, ctx=ctx)+ old_content = MemoryFileUtils.read(source_raw, uri=source_content.uri)+ except (NotFoundError, FileNotFoundError):+ raise ConflictError(+ f"Memory rename source no longer exists: {source_content.uri}",+ resource=source_content.uri,+ ) from None# Fall back to pre-fetched content if disk read failedif old_content is None:- old_content = resolved_op.old_memory_file_content+ old_content = source_content++ metadata: Dict[str, Any] = {}+ if is_uri_migration:+ from openviking.session.memory.utils.link_renderer import LinkRenderer- metadata: Dict[str, Any] = dict(resolved_op.memory_fields)+ metadata.update(old_content.extra_fields)+ metadata["content"] = LinkRenderer.strip_managed_links(+ old_content.content,+ old_content.uri,+ old_content.links,+ )+ metadata.update(resolved_op.memory_fields)source = getattr(resolved_op, "source", None)source_extraction_id = getattr(source, "extraction_id", None) if source else Noneif source_extraction_id:@@ -1175,6 +1476,10 @@ class MemoryUpdater:# Merge linksexisting_links = old_content.links if has_existing_links else []+ if is_uri_migration:+ existing_links = [+ _remap_link_dict(link, uri_remap or {}) for link in existing_links+ ]if incoming_links:merged_links = merge_links(existing_links,@@ -1186,6 +1491,10 @@ class MemoryUpdater:# Merge backlinksexisting_backlinks = old_content.backlinks if has_existing_links else []+ if is_uri_migration:+ existing_backlinks = [+ _remap_link_dict(link, uri_remap or {}) for link in existing_backlinks+ ]if incoming_backlinks:merged_backlinks = merge_links(existing_backlinks,@@ -1275,29 +1584,37 @@ class MemoryUpdater:result: MemoryUpdateResult,ctx: RequestContext,lease_ref: Any = None,- ) -> None:+ ) -> bool:uri_remap = dict(getattr(operations, "delete_replacements", {}) or {})if not uri_remap:- return+ return Trueviking_fs = self._get_viking_fs()if not viking_fs:- return+ return Truefrom openviking.session.memory.merge_op.link_merge import merge_links- inherited_by_uri: Dict[str, Dict[str, List[Dict[str, Any]]]] = {}+ # Track each inherited link's originating deleted URI so we can subtract+ # contributions that were already folded in during _apply_upsert (e.g.+ # the migration source of an implicit rename).+ inherited_by_uri: Dict[str, Dict[str, List[Tuple[str, Dict[str, Any]]]]] = {}+ completed = Truefor deleted_uri, replacement_uri in uri_remap.items():if not deleted_uri or not replacement_uri or deleted_uri == replacement_uri:continuetry:content = await viking_fs.read_file(deleted_uri, ctx=ctx)- except Exception as e:- # Benign: the replacement/deleted file may already be gone in the- # same batch. Link inheritance is best-effort, so warn and skip.+ except (NotFoundError, FileNotFoundError) as e:+ # Benign: the source may already be gone in an idempotent replay.logger.warning(f"Skipping link inheritance; could not read deleted memory {deleted_uri}: {e}")continue+ except Exception as e:+ tracer.error(f"Failed to read deleted memory links for {deleted_uri}: {e}")+ result.add_error(deleted_uri, e)+ completed = False+ continueif not content:continuedeleted_file = MemoryFileUtils.read(content, uri=deleted_uri)@@ -1309,12 +1626,12 @@ class MemoryUpdater:if target_uri:inherited_by_uri.setdefault(target_uri, {"links": [], "backlinks": []})["links"- ].append(remapped)+ ].append((deleted_uri, remapped))neighbor_uri = remapped.get("to_uri")if neighbor_uri and neighbor_uri not in uri_remap:inherited_by_uri.setdefault(neighbor_uri, {"links": [], "backlinks": []})["backlinks"- ].append(remapped)+ ].append((deleted_uri, remapped))for link in list(deleted_file.backlinks or []):remapped = _remap_link_dict(link, uri_remap)if remapped.get("from_uri") == remapped.get("to_uri"):@@ -1323,25 +1640,46 @@ class MemoryUpdater:if target_uri:inherited_by_uri.setdefault(target_uri, {"links": [], "backlinks": []})["backlinks"- ].append(remapped)+ ].append((deleted_uri, remapped))neighbor_uri = remapped.get("from_uri")if neighbor_uri and neighbor_uri not in uri_remap:inherited_by_uri.setdefault(neighbor_uri, {"links": [], "backlinks": []})["links"- ].append(remapped)+ ].append((deleted_uri, remapped))written_or_edited = set(result.written_uris + result.edited_uris)+ implicit_migration_source_by_target = {+ operation.uris[0]: operation.old_memory_file_content.uri+ for operation in operations.upsert_operations+ if self._is_uri_migration(operation)+ and len(operation.uris) == 1+ and operation.old_memory_file_content+ and operation.old_memory_file_content.uri+ }stale_uris = set(uri_remap)for uri, link_groups in inherited_by_uri.items():if uri in uri_remap:continue- if uri in written_or_edited:+ # Implicit renames already copied and remapped the source's links+ # while writing the new file; drop that source's contribution to+ # avoid a redundant rewrite. Contributions from other deleted+ # replacements (e.g. a duplicate merged into the same target) are+ # still folded in here.+ excluded_source = implicit_migration_source_by_target.get(uri)+ filtered_links = [link for src, link in link_groups["links"] if src != excluded_source]+ filtered_backlinks = [+ link for src, link in link_groups["backlinks"] if src != excluded_source+ ]+ if not filtered_links and not filtered_backlinks:continuetry:content = await viking_fs.read_file(uri, ctx=ctx)if not content:continuemf = MemoryFileUtils.read(content, uri=uri)+ from openviking.session.memory.utils.link_renderer import LinkRenderer++ plain_content = LinkRenderer.strip_managed_links(mf.content, uri, mf.links)# Remapped links have different dedup keys, so remove the old# endpoints before merging to avoid retaining dangling aliases.mf.links = [@@ -1356,13 +1694,14 @@ class MemoryUpdater:if link.get("from_uri") not in stale_urisand link.get("to_uri") not in stale_uris]- if link_groups["links"]:- mf.links = merge_links(mf.links, link_groups["links"])- if link_groups["backlinks"]:- mf.backlinks = merge_links(mf.backlinks, link_groups["backlinks"])+ if filtered_links:+ mf.links = merge_links(mf.links, filtered_links)+ if filtered_backlinks:+ mf.backlinks = merge_links(mf.backlinks, filtered_backlinks)current_trace_id = get_trace_id()if current_trace_id:mf.extra_fields["last_update_trace_id"] = current_trace_id+ mf.content = plain_contentbump_memory_version(mf)await viking_fs.write_file(uri,@@ -1370,13 +1709,17 @@ class MemoryUpdater:ctx=ctx,lease_ref=lease_ref,)- result.add_edited(uri)+ if uri not in written_or_edited:+ result.add_edited(uri)except (NotFoundError, FileNotFoundError) as e:# Benign: a linked neighbor may have been deleted in the same# batch. Link inheritance is best-effort, so warn and skip.logger.warning(f"Skipping link inheritance; could not read memory {uri}: {e}")except Exception as e:tracer.error(f"Failed to inherit deleted memory links for {uri}: {e}")+ result.add_error(uri, e)+ completed = False+ return completedasync def _apply_delete(self,@@ -1615,8 +1958,14 @@ class MemoryUpdater:ctx=ctx,lease_ref=lease_ref,)- except Exception:+ except (NotFoundError, FileNotFoundError):pass+ except Exception:+ logger.warning(+ "Failed to delete empty memory overview %s",+ overview_path,+ exc_info=True,+ )# Try to delete empty directoryif can_delete_directory:try:@@ -1626,8 +1975,14 @@ class MemoryUpdater:ctx=ctx,lease_ref=lease_ref,)- except Exception:+ except (NotFoundError, FileNotFoundError):pass+ except Exception:+ logger.warning(+ "Failed to delete empty memory directory %s",+ directory,+ exc_info=True,+ )return True# Parse each file and collect itemsdiff --git a/openviking/session/memory/schema_model_generator.py b/openviking/session/memory/schema_model_generator.pyindex e2f16966e..401e2ddd2 100644--- a/openviking/session/memory/schema_model_generator.py+++ b/openviking/session/memory/schema_model_generator.py@@ -10,7 +10,7 @@ definitions, with discriminator support for polymorphic fields.import refrom typing import Annotated, Any, Dict, List, Optional, Tuple, Type, Union-from pydantic import BaseModel, Field, WithJsonSchema, create_model+from pydantic import BaseModel, Field, WithJsonSchema, create_model, model_validatorfrom pydantic.config import ConfigDictfrom openviking.session.memory.dataclass import (@@ -159,17 +159,22 @@ class SchemaModelGenerator:),)- immutable_field_names = []+ identity_field_names = set(memory_type.identity_fields(include_peer_id=False))+ required_on_create = [+ field.name+ for field in memory_type.fields+ if field.merge_op == MergeOp.IMMUTABLE or field.name in identity_field_names+ ]# Add business fields from schemafor field in memory_type.fields:base_type = self._map_field_type(field.field_type)if field.merge_op == MergeOp.IMMUTABLE:- # Immutable fields: only base type, required- immutable_field_names.append(field.name)+ # Existing updates may omit immutable fields. New objects are+ # checked by the conditional validator below.field_definitions[field.name] = (- base_type,- Field(..., description=self._render_description(field.description)),+ Optional[base_type],+ Field(None, description=self._render_description(field.description)),)else:# Mutable fields: Union[base_type, patch_type], optional@@ -183,10 +188,40 @@ class SchemaModelGenerator:Optional[union_type],Field(None, description=desc),)+# Create the model+ @model_validator(mode="before")+ def require_new_object_identity(cls, data):+ del cls+ if not isinstance(data, dict):+ return data+ try:+ is_new = int(data.get("page_id")) >= 100+ except (TypeError, ValueError):+ return data+ missing = [name for name in required_on_create if data.get(name) is None]+ if is_new and missing:+ raise ValueError("new memory item requires fields: " + ", ".join(missing))+ return data++ json_schema_extra = None+ if required_on_create:+ json_schema_extra = {+ "allOf": [+ {+ "if": {+ "properties": {"page_id": {"minimum": 100}},+ "required": ["page_id"],+ },+ "then": {"required": required_on_create},+ }+ ]+ }+model = create_model(model_name,- __config__=ConfigDict(extra="ignore"),+ __config__=ConfigDict(extra="ignore", json_schema_extra=json_schema_extra),+ __validators__={"require_new_object_identity": require_new_object_identity},**field_definitions,)@@ -210,7 +245,9 @@ class SchemaModelGenerator:models[memory_type.memory_type] = self.create_flat_data_model(memory_type)return models- def create_structured_operations_model(self, role_scope: Optional[RoleScope] = None) -> Type[BaseModel]:+ def create_structured_operations_model(+ self, role_scope: Optional[RoleScope] = None+ ) -> Type[BaseModel]:"""Create a structured MemoryOperations model with type-safe write operations.@@ -353,6 +390,7 @@ class SchemaModelGenerator:self._operations_model = StructuredMemoryOperationsreturn self._operations_model+class SchemaPromptGenerator:"""Prompt generator that incorporates schema information into LLM prompts.diff --git a/openviking/session/memory/streaming_memory_updater.py b/openviking/session/memory/streaming_memory_updater.pyindex 698281e71..bee33ffb8 100644--- a/openviking/session/memory/streaming_memory_updater.py+++ b/openviking/session/memory/streaming_memory_updater.py@@ -498,7 +498,7 @@ class StreamingMemoryUpdater:isolation_handler = _make_isolation_handler(request, extract_context)async with self._apply_lock:viking_fs = safe_get_viking_fs()- lease = await _acquire_stable_operation_lease(+ lease = await acquire_memory_operation_lease(operations,viking_fs,request.ctx,@@ -1005,10 +1005,10 @@ async def merge_one_memory_type_operations(required_file_uris = list(dict.fromkeys([- uri+ op.old_memory_file_content.urifor op in operations- for uri in op.urisif getattr(op, "old_memory_file_content", None) is not None+ and op.old_memory_file_content.uri]+ [df.uri for df in delete_files if df.uri])@@ -1110,14 +1110,11 @@ def merge_output_language_from_messages(messages: list[Message]) -> str | None:def clone_operation_for_uri(op: ResolvedOperation, uri: str) -> ResolvedOperation:- old_file = getattr(op, "old_memory_file_content", None)- if old_file is not None and getattr(old_file, "uri", None) not in (None, uri):- old_file = Nonereturn op.model_copy(update={"uris": [uri],"memory_fields": dict(getattr(op, "memory_fields", {}) or {}),- "old_memory_file_content": old_file,+ "old_memory_file_content": getattr(op, "old_memory_file_content", None),"source": getattr(op, "source", None),},deep=True,@@ -1550,9 +1547,8 @@ def seed_patch_merge_read_contents() -> None:for op in operations:old_file = getattr(op, "old_memory_file_content", None)- uri = _first_uri(getattr(op, "uris", []) or [])- if old_file is not None and uri:- provider.read_file_contents[uri] = old_file+ if old_file is not None and old_file.uri:+ provider.read_file_contents[old_file.uri] = old_filedef safe_get_viking_fs() -> Any | None:@@ -2136,6 +2132,49 @@ def _operation_lock_paths(return _uri_lock_paths(uris, viking_fs, ctx)+def _operation_tree_lock_paths(+ operations: ResolvedOperations,+ viking_fs: Any | None,+ ctx: RequestContext,+) -> list[str]:+ """Tree-lock parent directories that may be emptied by this batch.++ `generate_overview` follows any batch whose last file in a directory is removed with+ a recursive rm of that directory, so the lease has to cover the parent. A same-dir+ rename does not empty the parent (the target replaces the source in place), so those+ can stay on exact locks.+ """+ replacements = dict(operations.delete_replacements or {})+ directories: set[str] = set()+ for source_uri, target_uri in replacements.items():+ source_directory = str(source_uri).rstrip("/").rpartition("/")[0]+ target_directory = str(target_uri).rstrip("/").rpartition("/")[0]+ if source_directory and source_directory != target_directory:+ directories.add(source_directory)+ for memory_file in operations.delete_file_contents or []:+ uri = getattr(memory_file, "uri", None)+ if not uri:+ continue+ source_directory = str(uri).rstrip("/").rpartition("/")[0]+ if not source_directory:+ continue+ replacement = replacements.get(str(uri))+ if replacement:+ replacement_directory = str(replacement).rstrip("/").rpartition("/")[0]+ if replacement_directory == source_directory:+ continue+ directories.add(source_directory)+ return _uri_lock_paths(directories, viking_fs, ctx)+++def _exclude_tree_covered_paths(exact_paths: set[str], tree_paths: set[str]) -> set[str]:+ return {+ path+ for path in exact_paths+ if not any(path == tree or path.startswith(f"{tree.rstrip('/')}/") for tree in tree_paths)+ }++async def _persisted_replacement_relation_uris(operations: ResolvedOperations,viking_fs: Any,@@ -2164,37 +2203,53 @@ async def _persisted_replacement_relation_uris(return uris-async def _acquire_stable_operation_lease(+async def acquire_memory_operation_lease(operations: ResolvedOperations,viking_fs: Any | None,ctx: RequestContext,) -> Any | None:- lock_paths = _operation_lock_paths(operations, viking_fs, ctx)- if not lock_paths:+ # Materialize implicit URI changes only at the final apply boundary. Doing+ # this before second-stage patch merging would present the same rename as+ # both an update patch and a delete patch.+ MemoryUpdater._materialize_uri_migrations(operations)+ exact_paths = set(_operation_lock_paths(operations, viking_fs, ctx))+ tree_paths = set(_operation_tree_lock_paths(operations, viking_fs, ctx))+ exact_paths = _exclude_tree_covered_paths(exact_paths, tree_paths)+ if not exact_paths and not tree_paths:return None- required_paths = set(lock_paths)+ required_exact_paths = set(exact_paths)for acquisition in range(1, _MEMORY_APPLY_LOCK_MAX_ACQUISITIONS + 1):- lease = await viking_fs._async_agfs.pathlock_acquire_exact_batch(- sorted(required_paths),- timeout_secs=_MEMORY_APPLY_LOCK_TIMEOUT_SECONDS,- )+ if tree_paths:+ lease = await viking_fs._async_agfs.pathlock_acquire_exact_tree_batch(+ sorted(required_exact_paths),+ sorted(tree_paths),+ timeout_secs=_MEMORY_APPLY_LOCK_TIMEOUT_SECONDS,+ )+ else:+ lease = await viking_fs._async_agfs.pathlock_acquire_exact_batch(+ sorted(required_exact_paths),+ timeout_secs=_MEMORY_APPLY_LOCK_TIMEOUT_SECONDS,+ )try:relation_uris = await _persisted_replacement_relation_uris(operations,viking_fs,ctx,)- expanded_paths = required_paths | set(_uri_lock_paths(relation_uris, viking_fs, ctx))+ expanded_exact_paths = required_exact_paths | set(+ _uri_lock_paths(relation_uris, viking_fs, ctx)+ )+ expanded_exact_paths = _exclude_tree_covered_paths(expanded_exact_paths, tree_paths)except BaseException:await viking_fs._async_agfs.pathlock_release(lease)raise- if expanded_paths == required_paths:+ if expanded_exact_paths == required_exact_paths:return leaseawait viking_fs._async_agfs.pathlock_release(lease)- required_paths = expanded_paths+ required_exact_paths = expanded_exact_pathsif acquisition == _MEMORY_APPLY_LOCK_MAX_ACQUISITIONS:raise RuntimeError("Unable to stabilize memory apply lock coverage after "diff --git a/openviking/session/memory/tools.py b/openviking/session/memory/tools.pyindex 309416f67..7cc698f1d 100644--- a/openviking/session/memory/tools.py+++ b/openviking/session/memory/tools.py@@ -329,13 +329,19 @@ def _format_size(size_bytes: int) -> str:class MemoryLsTool(MemoryTool):"""Tool to list directory contents."""+ # Cap on recursive listing so a huge directory cannot flood the context.+ _RECURSIVE_NODE_LIMIT = 500+@propertydef name(self) -> str:return "ls"@propertydef description(self) -> str:- return "List directory content, includes abstract field when output='agent'"+ return (+ "List directory content. Pass recursive=true to include files in "+ "subdirectories (returns 'relative/path size' per line)."+ )@propertydef parameters(self) -> Dict[str, Any]:@@ -349,6 +355,14 @@ class MemoryLsTool(MemoryTool):"'viking://~/memories'"),},+ "recursive": {+ "type": "boolean",+ "description": (+ "List files in all subdirectories with relative paths. "+ "Use this for memory directories organized into subfolders."+ ),+ "default": False,+ },},"required": ["uri"],}@@ -360,6 +374,8 @@ class MemoryLsTool(MemoryTool):) -> Any:try:uri = kwargs.get("uri", "")+ if kwargs.get("recursive"):+ return await self._execute_recursive(ctx, uri)entries = await ctx.viking_fs.ls(uri,output="agent",@@ -368,17 +384,19 @@ class MemoryLsTool(MemoryTool):node_limit=1000,ctx=ctx.request_ctx,)- # Format: filename size (e.g., "file.md 1.2K")+ # Format: filename size (e.g., "file.md 1.2K"). Directories are+ # surfaced with a trailing slash so a subfoldered directory does not+ # look empty; the model can ls into them or use recursive=true.result_lines = []for e in entries:- if not e.get("isDir", False):- # Extract name from entry or fallback to uri- name = e.get("name", "")- if not name:- uri = e.get("uri", "")- name = uri.rsplit("/", 1)[-1] if "/" in uri else uri- size = e.get("size", 0)- result_lines.append(f"{name} {_format_size(size)}")+ name = e.get("name", "")+ if not name:+ entry_uri = e.get("uri", "")+ name = entry_uri.rsplit("/", 1)[-1] if "/" in entry_uri else entry_uri+ if e.get("isDir", False):+ result_lines.append(f"{name}/")+ else:+ result_lines.append(f"{name} {_format_size(e.get('size', 0))}")if not result_lines:return "Directory is empty. You can write new files to create memory content."return "\n".join(result_lines)@@ -386,6 +404,38 @@ class MemoryLsTool(MemoryTool):tracer.info(f"Failed to execute ls: {e}")return {"error": str(e)}+ async def _execute_recursive(self, ctx: Optional["ToolContext"], uri: str) -> Any:+ base = uri.rstrip("/")+ result = await ctx.viking_fs.glob(+ "**/*",+ uri=base,+ node_limit=self._RECURSIVE_NODE_LIMIT + 1,+ extra_fields=[],+ ctx=ctx.request_ctx,+ )+ entries = result.get("matches", []) if isinstance(result, dict) else []+ lines: list[str] = []+ for e in entries:+ if not isinstance(e, dict) or e.get("isDir", False):+ continue+ entry_uri = str(e.get("uri", ""))+ name = entry_uri.rsplit("/", 1)[-1] if "/" in entry_uri else entry_uri+ if name in {".overview.md", ".abstract.md"}:+ continue+ rel = entry_uri[len(base) + 1 :] if entry_uri.startswith(base + "/") else name+ lines.append(f"{rel} {_format_size(e.get('size', 0))}")+ if not lines:+ return "Directory is empty. You can write new files to create memory content."+ truncated = len(lines) > self._RECURSIVE_NODE_LIMIT+ lines = lines[: self._RECURSIVE_NODE_LIMIT]+ text = "\n".join(lines)+ if truncated:+ text += (+ f"\n... (listing truncated at {self._RECURSIVE_NODE_LIMIT} files; "+ "use ls on a subdirectory or search to narrow down)"+ )+ return text+# Tool registryMEMORY_TOOLS_REGISTRY: Dict[str, MemoryTool] = {}diff --git a/openviking/session/memory/utils/link_renderer.py b/openviking/session/memory/utils/link_renderer.pyindex bb2699da6..849c06458 100644--- a/openviking/session/memory/utils/link_renderer.py+++ b/openviking/session/memory/utils/link_renderer.py@@ -313,6 +313,42 @@ class LinkRenderer:return LinkRenderer._replace_markdown_links(content, lambda link: link.text)+ @staticmethod+ def strip_managed_links(content: str, source_uri: str, links: List[Dict]) -> str:+ """Strip only Markdown links represented by the supplied StoredLink metadata.++ External links and unrelated hand-written relative links are preserved. This is used+ before remapping a managed relation so a stale rendered destination can be regenerated+ from the updated metadata.+ """+ managed: List[tuple[str, set[str]]] = []+ for stored_link in links or []:+ if stored_link.get("from_uri") != source_uri:+ continue+ match_text = str(stored_link.get("match_text") or "")+ target_uri = str(stored_link.get("to_uri") or "")+ if not match_text or not target_uri:+ continue+ expected_targets = {LinkRenderer.normalize_markdown_target(target_uri)}+ relative_target = LinkRenderer.relative_path(source_uri, target_uri)+ if relative_target is not None:+ expected_targets.add(LinkRenderer.normalize_markdown_target(relative_target))+ managed.append((match_text, expected_targets))++ if not managed:+ return content++ def _replace_link(link: MarkdownLink) -> str:+ normalized_target = LinkRenderer.normalize_markdown_target(link.target)+ for match_text, expected_targets in managed:+ if normalized_target not in expected_targets:+ continue+ if LinkRenderer._find_match_span(link.text, match_text) is not None:+ return link.text+ return content[link.start : link.end]++ return LinkRenderer._replace_markdown_links(content, _replace_link)+@staticmethoddef relative_path(source_uri: str, target_uri: str) -> Optional[str]:"""Compute a relative path from source_uri to target_uri in the viking:// namespace.diff --git a/tests/integration/test_compile_memory_xiaomei.py b/tests/integration/test_compile_memory_xiaomei.pynew file mode 100644index 000000000..9df480446--- /dev/null+++ b/tests/integration/test_compile_memory_xiaomei.py@@ -0,0 +1,1035 @@+#!/usr/bin/env python3+"""+OpenViking memory-mode compile 集成脚本 — 用户: 小美++默认跑全部单类型和 memories 根目录用例,产物遵守各自 schema。+rename 和 memory_root 直接创建唯一测试文件,其余用例通过种子对话抽取;测试数据保留,不自动清理。+脚本流程:++ Phase 0 (可选 --ingest): 复用小美对话灌一遍数据,产生 entities 等记忆+ Phase 1: 打印整理前该目录下的记忆文件+ Phase 2: 触发 compile(skill="memory") 并轮询 cmp_ 任务直到完成+ Phase 3: 打印整理后的记忆文件,对比前后++用法:+ python tests/integration/test_compile_memory_xiaomei.py --url http://localhost:1933+ python tests/integration/test_compile_memory_xiaomei.py --url http://localhost:1933 --ingest+ python tests/integration/test_compile_memory_xiaomei.py --url http://localhost:1933 \+ --case memory_root --no-ingest+"""++import argparse+import asyncio+import time+import uuid+from dataclasses import dataclass+from datetime import datetime+from posixpath import relpath++from rich import box+from rich.console import Console+from rich.panel import Panel+from rich.table import Table++import openviking as ov+from openviking.session.memory.dataclass import MemoryFile, StoredLink+from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils++try:+ from openviking_live_auth import API_KEY_HELP, resolve_api_key+except ModuleNotFoundError: # pytest/package import path+ from tests.integration.openviking_live_auth import API_KEY_HELP, resolve_api_key++try:+ from test_compressor_v2_xiaomei import _run_ingest_async+except ModuleNotFoundError: # pytest/package import path+ from tests.integration.test_compressor_v2_xiaomei import _run_ingest_async++# ── 常量 ───────────────────────────────────────────────────────────────────++DISPLAY_NAME = "小美"+DEFAULT_URL = "http://localhost:1933"+PANEL_WIDTH = 78+DEFAULT_API_KEY = None+DEFAULT_ACCOUNT = "default"+DEFAULT_USER = "xiaomei"+DEFAULT_SESSION_ID = "xiaomei-compile-demo"+++@dataclass+class CompileCase:+ """一个 compile 集成 case:种子对话 + 目标类型 + 整理指令。++ sessions: 每个内层 list 是一段对话,会各自独立 commit(不同抽取批次),+ 以便按需要让抽取产出独立实体。+ """++ key: str+ memory_type: str | None+ instruction: str+ sessions: list[list[dict]]+ description: str+ rename_target_prefix: str | None = None+ required_facts: tuple[tuple[str, ...], ...] = ()+++# A. 合并(merge):两段独立对话,同一个人两个不同称呼(阿珍 / 陈静娴)。+# 名字不同、从不点破,抽取阶段拆不开 → 产出两个实体;compile 收到点破指令后合并。+CASE_MERGE = CompileCase(+ key="merge",+ memory_type="entities",+ instruction=(+ "记忆库里的「阿珍」和「陈静娴」其实是同一个人(阿珍是陈静娴的昵称,是小美的大学室友)。"+ "请把这两个实体合并成一个,规范名用「陈静娴」,并保留两条记忆里的全部独立事实"+ "(大学室友、仗义借钱、上海 UI 设计师、下月来访)。其它不同的实体不要合并。"+ ),+ sessions=[+ [+ {+ "user": "我大学室友大家都叫她阿珍,人特别仗义,上次我手头紧她二话不说借钱给我应急。",+ "assistant": "阿珍真是个靠谱的朋友,这样仗义的室友很难得。",+ }+ ],+ [+ {+ "user": "我有个朋友叫陈静娴,在上海一家互联网公司做 UI 设计师,下个月要来找我玩。",+ "assistant": "陈静娴听起来很厉害呀,做 UI 设计的,下个月见面一定很开心。",+ }+ ],+ ],+ description="两个不同称呼实为同一人,compile 合并(delete 一个 + update 另一个)。",+)++# B. 拆分(split):两个同名不同人(都叫「小林」)。因为实体 URI = category/name,+# 同名会落到同一文件,抽取被迫把两人塞进一个 person/小林.md → compile 拆成两个。+CASE_SPLIT = CompileCase(+ key="split",+ memory_type="entities",+ instruction=(+ "记忆库里的「小林」其实混进了两个不同的人:一个是小美市场部的同事小林,"+ "另一个是小美的健身教练小林。请把它们拆成两个独立实体,分别保留各自的事实,"+ "用能区分二者的名字(例如「同事小林」和「教练小林」)。不要把不同的人合在一起。"+ ),+ sessions=[+ [+ {+ "user": "我同事小林在市场部,平时特别爱开玩笑,办公室气氛全靠他活跃。",+ "assistant": "有这样的同事上班一定很快乐,小林听起来很会调节气氛。",+ },+ {+ "user": "另外我健身房的教练也叫小林,特别专业,帮我把深蹲动作纠正过来了。",+ "assistant": "教练小林很专业呀,动作规范太重要了,能避免受伤。",+ },+ ],+ ],+ description="同名两人被抽取塞进一个文件,compile 拆分(add 新实体 + update/delete 原文件)。",+)++# C. 精简去重(dedup/edit):多段对话反复用不同措辞说同一个人的同一件事,+# 抽取累积出啰嗦/重复的正文 → compile 原地 edit/drop 精简(文件变小,不删不增)。+CASE_DEDUP = CompileCase(+ key="dedup",+ memory_type="entities",+ instruction=(+ "记忆库里关于「大壮」的记忆有很多重复、啰嗦、同义反复的表述。请在不丢失任何"+ "独立事实的前提下,原地精简去重,让内容更紧凑。不要合并或删除其它实体。"+ ),+ sessions=[+ [+ {+ "user": "我发小大壮人特别好,真的超级好,对朋友特别特别够意思,是个非常仗义的人。",+ "assistant": "大壮听起来是个很重情义的发小。",+ }+ ],+ [+ {+ "user": "大壮真的很讲义气,对哥们儿没话说,特别仗义,谁有事他都第一个到。",+ "assistant": "这样重义气的朋友很难得,遇到事有他在很安心。",+ }+ ],+ ],+ description="同一实体反复重复陈述,compile 原地精简(update,文件变小)。",+)++CASE_RENAME = CompileCase(+ key="rename",+ memory_type="entities",+ instruction=(+ "把人物「{source_name}」的目录和文件名改成英文:目录用 person,文件名用"+ " {target_name}.md。保留摄影老师、杭州、风光摄影和十月去西湖练习长曝光等事实;"+ "不要创建重复实体或修改其他实体。"+ ),+ sessions=[],+ description=(+ "目录和文件名在中英文之间往返改名,每一步均为 add 新 URI + delete 旧 URI,并迁移关系链接。"+ ),+ rename_target_prefix="photo_teacher",+ required_facts=(+ ("摄影老师", "photography teacher"),+ ("杭州", "hangzhou"),+ ("风光摄影", "landscape photography"),+ ("十月", "october"),+ ("西湖", "west lake"),+ ("长曝光", "long exposure"),+ ),+)++# E. 换 memory type:整理 preferences,而不是 entities,验证 compile 对不同 schema 都工作。+CASE_PREFERENCES = CompileCase(+ key="preferences",+ memory_type="preferences",+ instruction=(+ "整理小美的偏好记忆:合并明显重复或矛盾的偏好,保留每一条独立的偏好事实,"+ "让偏好集合清晰、无冗余。不同维度的偏好不要强行合并。"+ ),+ sessions=[+ [+ {+ "user": "我吃麻辣烫喜欢多放醋和麻酱,另外我不太能吃辣,最好少辣或者微辣。",+ "assistant": "记住啦,多醋多麻酱、少辣,这样的麻辣烫更合你口味。",+ }+ ],+ [+ {+ "user": "再补充一下,我吃东西真的不能太辣,微辣就是我的极限,麻酱一定要多给。",+ "assistant": "好的,微辣上限、麻酱多多,帮你记牢了。",+ }+ ],+ ],+ description="整理 preferences 类型,验证 compile 对非 entities schema 同样工作。",+)++CASE_MEMORY_ROOT = CompileCase(+ key="memory_root",+ memory_type=None,+ instruction=(+ "请在这次整理中同时完成两件事:把人物「{source_name}」的文件名改为 {target_name}.md,"+ "仍放在 person 目录,保留她是苏州陶艺老师、教授青瓷拉坯的事实;"+ "把「{topic}」这条偏好原地精简,删除重复表述,保留喜欢小班课、"+ "每班最多六人、方便老师逐个指导以及喜欢{source_name}老师课程的事实。"+ "保留课程资料链接,不要删除偏好,不要修改其它记忆,也不要合并不同的人。"+ ),+ sessions=[],+ description="一次 memories 根目录 compile 同时改名实体和精简偏好,验证根目录文件与跨类型链接。",+ required_facts=(+ ("陶艺老师", "pottery teacher", "ceramics teacher"),+ ("苏州", "suzhou"),+ ("青瓷", "celadon"),+ ("拉坯", "wheel throwing", "wheel-throwing"),+ ),+)+++CASES: dict[str, CompileCase] = {+ CASE_MERGE.key: CASE_MERGE,+ CASE_SPLIT.key: CASE_SPLIT,+ CASE_DEDUP.key: CASE_DEDUP,+ CASE_RENAME.key: CASE_RENAME,+ CASE_PREFERENCES.key: CASE_PREFERENCES,+ CASE_MEMORY_ROOT.key: CASE_MEMORY_ROOT,+}++console = Console()+++def _memory_dir(user: str, memory_type: str | None) -> str:+ root = f"viking://user/{user}/memories"+ return f"{root}/{memory_type}" if memory_type else root+++def _case_directory(args, case: CompileCase) -> str:+ directory = (args.to or _memory_dir(args.user, case.memory_type)).rstrip("/")+ if case.key == "memory_root" and directory != _memory_dir(args.user, None):+ raise ValueError("memory_root 用例的 --to 必须是当前用户的 memories 根目录")+ if case.rename_target_prefix and directory.rsplit("/", 1)[-1] != "entities":+ raise ValueError("rename 用例的 --to 必须是 entities 目录,不能直接传 memories 根目录")+ return directory+++def _unique_rename_names(case: CompileCase) -> tuple[str, str]:+ token = uuid.uuid4().hex[:8]+ surnames = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨"+ given_names = "清岚若晴雨桐诗涵婉宁静怡思远知夏"+ source_name = (+ surnames[int(token[:2], 16) % len(surnames)]+ + given_names[int(token[2:4], 16) % len(given_names)]+ + given_names[int(token[4:6], 16) % len(given_names)]+ + given_names[int(token[6:8], 16) % len(given_names)]+ )+ return (+ source_name,+ f"{case.rename_target_prefix}_{token}",+ )+++# ── 为一个 case 追加种子对话(每段独立 commit),让抽取产生实体 ───────────────+++async def _ingest_case_sessions(+ client,+ case: CompileCase,+ wait_seconds: float,+) -> None:+ """为 case 的每段对话独立开 session 并 commit,让抽取各自产出实体。"""+ base_time = datetime(2023, 4, 9, 20, 0)+ for idx, turns in enumerate(case.sessions):+ session = await client.create_session()+ session_id = session.get("session_id")+ console.print(f" Session[{idx + 1}]: [bold cyan]{session_id}[/bold cyan]")++ # 每段对话给不同的会话时间,进一步区分抽取批次。+ session_time_str = base_time.replace(day=base_time.day + idx).isoformat()+ for turn in turns:+ user_text = turn["user"]+ assistant_text = turn["assistant"]+ await client.add_message(+ session_id,+ role="user",+ parts=[{"type": "text", "text": user_text}],+ options={"created_at": session_time_str},+ )+ await client.add_message(+ session_id,+ role="assistant",+ parts=[{"type": "text", "text": assistant_text}],+ options={"created_at": session_time_str},+ )++ console.print(f" [yellow]提交种子对话 Session[{idx + 1}](触发记忆抽取)...[/yellow]")+ commit_result = await client.commit_session(session_id)+ task_id = commit_result.get("task_id")+ console.print(f" [bold cyan]trace_id: {commit_result.get('trace_id')}[/bold cyan]")+ if not task_id:+ raise AssertionError(f"种子对话提交未返回 task_id: {commit_result}")+ while True:+ task = await client.get_task(task_id)+ if not task or task.get("status") in ("completed", "failed", "cancelled"):+ break+ await asyncio.sleep(1)+ if not task or task.get("status") != "completed" or task.get("error"):+ raise AssertionError(f"种子对话抽取失败: {task}")+ console.print(" [green]抽取 completed[/green]")+ await client.wait_processed()+ if wait_seconds > 0:+ await asyncio.sleep(wait_seconds)+++# ── 列出目录下的记忆文件 ─────────────────────────────────────────────────────+++async def _snapshot_memory_dir(client, directory: str) -> list[dict]:+ """递归列出某记忆目录下的 .md 文件(跳过 .overview.md / .abstract.md)。"""+ files: list[dict] = []+ entries = await client.ls(+ directory,+ recursive=True,+ output="original",+ show_all_hidden=False,+ node_limit=1000,+ )+ if len(entries or []) >= 1000:+ raise AssertionError(f"目录快照可能被截断,无法完整验证: {directory}")++ for entry in entries or []:+ if not isinstance(entry, dict):+ continue+ if entry.get("isDir"):+ continue+ uri = str(entry.get("uri", ""))+ name = str(entry.get("name", ""))+ if not uri.endswith(".md"):+ continue+ if name in {".overview.md", ".abstract.md"} or uri.endswith(+ ("/.overview.md", "/.abstract.md")+ ):+ continue+ files.append(entry)+ return files+++def _render_snapshot(title: str, directory: str, files: list[dict]) -> None:+ table = Table(title=title, box=box.ROUNDED, show_header=True, header_style="bold")+ table.add_column("#", style="bold", width=4)+ table.add_column("URI", style="cyan")+ table.add_column("size", justify="right", width=8)+ for i, entry in enumerate(files, 1):+ uri = str(entry.get("uri", ""))+ rel = uri.replace(directory.rstrip("/") + "/", "")+ table.add_row(str(i), rel, str(entry.get("size", "")))+ console.print(table)+ console.print(f" 共 [bold]{len(files)}[/bold] 个记忆文件")+++def _render_change_lists(result: dict) -> None:+ """打印整理产生的 adds/updates/deletes 文件名清单(不含内容)。"""+ table = Table(title="变化文件清单", box=box.ROUNDED, show_header=True, header_style="bold")+ table.add_column("类型", style="bold", width=10)+ table.add_column("URI", style="cyan")+ for kind, style in (("adds", "green"), ("updates", "yellow"), ("deletes", "red")):+ for uri in result.get(kind, []) or []:+ table.add_row(f"[{style}]{kind}[/{style}]", str(uri))+ console.print(table)+ console.print(+ f" 新增 [green]{result.get('total_adds', 0)}[/green] / "+ f"修改 [yellow]{result.get('total_updates', 0)}[/yellow] / "+ f"删除 [red]{result.get('total_deletes', 0)}[/red]"+ )+ errors = result.get("errors") or []+ if errors:+ console.print(f" [red]errors: {errors}[/red]")+++def _entry_uri(entry: dict) -> str:+ return str(entry.get("uri", ""))+++async def _verify_rename_case(+ client,+ *,+ case: CompileCase,+ directory: str,+ source_uri: str,+ target_uri: str,+ task: dict,+ after: list[dict],+ event_uri: str,+) -> None:+ """Verify rename semantics from task diff through persisted content and links."""+ result = task.get("result") or {}+ errors = result.get("errors") or []+ if task.get("status") != "completed" or errors:+ raise AssertionError(+ f"rename compile 未成功完成: status={task.get('status')}, errors={errors}"+ )++ adds = set(result.get("adds") or [])+ deletes = set(result.get("deletes") or [])+ if target_uri not in adds or source_uri not in deletes:+ raise AssertionError(+ "rename diff 不符合 add(new)+delete(old): "+ f"expected add={target_uri}, delete={source_uri}, result={result}"+ )++ after_uris = {_entry_uri(entry) for entry in after}+ if source_uri in after_uris or target_uri not in after_uris:+ raise AssertionError(+ "rename 后目录状态不正确: "+ f"source_exists={source_uri in after_uris}, target_exists={target_uri in after_uris}"+ )++ target_content = await client.read(target_uri)+ normalized_content = target_content.casefold()+ missing_facts = [+ alternatives+ for alternatives in case.required_facts+ if not any(term.casefold() in normalized_content for term in alternatives)+ ]+ if missing_facts:+ raise AssertionError(f"改名目标文件丢失事实: {missing_facts}\n{target_content}")++ stale_markers = {source_uri, source_uri.removeprefix("viking://user/")}+ source_rel = source_uri.removeprefix(directory.rstrip("/") + "/")+ stale_markers.add(f"entities/{source_rel}")+ for updated_uri in [target_uri, *(result.get("updates") or [])]:+ updated_content = await client.read_raw(updated_uri)+ found_markers = [marker for marker in stale_markers if marker in updated_content]+ if found_markers:+ raise AssertionError(f"关系迁移后仍残留旧 URI/href: {updated_uri} -> {found_markers}")++ event = MemoryFileUtils.read(await client.read_raw(event_uri), uri=event_uri)+ target = MemoryFileUtils.read(await client.read_raw(target_uri), uri=target_uri)+ migrated_links = [link for link in event.links if link.get("to_uri") == target_uri]+ if event_uri not in (result.get("updates") or []) or not migrated_links:+ raise AssertionError(f"关联事件未更新到新实体: {event_uri}")+ if not all(link in target.backlinks for link in migrated_links):+ raise AssertionError(f"新实体缺少关联事件 backlink: {target_uri}")+ href = relpath(target_uri, event_uri.rpartition("/")[0])+ if f"]({href})" not in event.content:+ raise AssertionError(f"关联事件正文缺少新 URI 链接: {event_uri}")+ console.print(f" [bold green]rename 验证通过:[/bold green] {source_uri} → {target_uri}")+++async def _seed_rename_case(+ client,+ directory: str,+ source_name: str,+ english_name: str,+) -> tuple[str, str]:+ source_uri = f"{directory}/person/{source_name}.md"+ event_name = f"photography_plan_{english_name}"+ event_uri = f"{directory.rpartition('/')[0]}/events/2023/04/09/{event_name}.md"+ link = StoredLink(+ from_uri=event_uri,+ to_uri=source_uri,+ match_text=source_name,+ description="摄影课程的授课老师",+ ).model_dump()+ entity = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ extra_fields={"memory_type": "entities", "category": "person", "name": source_name},+ content=(+ f"# {source_name}\n她是小美的摄影老师。\n\n## 专业\n- 住在杭州。\n"+ "- 专门拍风光摄影。\n\n## 课程\n- 十月带小美去西湖练习长曝光。"+ ),+ backlinks=[link],+ )+ summary = f"{source_name}老师十月带小美去西湖练习长曝光。"+ event = MemoryFile(+ uri=event_uri,+ memory_type="events",+ extra_fields={+ "memory_type": "events",+ "event_name": event_name,+ "goal": "photography lessons",+ "summary": summary,+ "ranges": "0",+ },+ content=f"# Summary\n{summary}\n",+ links=[link],+ )+ # Session extraction may translate names before the rename test starts.+ for memory in (entity, event):+ await client.write(memory.uri, MemoryFileUtils.write(memory), mode="create", wait=True)+ await client.wait_processed()+ return source_uri, event_uri+++async def _run_rename_step(+ client,+ args,+ case: CompileCase,+ *,+ directory: str,+ label: str,+ source_uri: str,+ target_uri: str,+ instruction: str,+ event_uri: str,+) -> list[dict]:+ console.rule(f"[bold]Rename {label}: {source_uri} → {target_uri}[/bold]")+ before = await _snapshot_memory_dir(client, directory)+ before_uris = {_entry_uri(entry) for entry in before}+ if source_uri not in before_uris or target_uri in before_uris:+ raise AssertionError(+ f"rename {label} 前置状态错误: source_exists={source_uri in before_uris}, "+ f"target_exists={target_uri in before_uris}"+ )++ task = await _run_memory_compile(client, directory, instruction)+ result = task.get("result") or {}+ trace_id = result.get("trace_id") or task.get("trace_id") or ""+ if trace_id:+ console.print(f" [bold cyan]trace_id: {trace_id}[/bold cyan]")+ _render_change_lists(result)++ await client.wait_processed()+ if args.wait > 0:+ await asyncio.sleep(args.wait)+ after = await _snapshot_memory_dir(client, directory)+ await _verify_rename_case(+ client,+ case=case,+ directory=directory,+ source_uri=source_uri,+ target_uri=target_uri,+ task=task,+ after=after,+ event_uri=event_uri,+ )+ return after+++async def _run_rename_round_trip(+ client,+ args,+ case: CompileCase,+ *,+ directory: str,+ source_name: str,+ english_name: str,+ initial_uri: str,+ event_uri: str,+) -> None:+ english_uri = f"{directory.rstrip('/')}/person/{english_name}.md"+ chinese_uri = f"{directory.rstrip('/')}/人物/{source_name}.md"++ to_english = case.instruction.format(+ source_name=source_name,+ target_name=english_name,+ )+ await _run_rename_step(+ client,+ args,+ case,+ directory=directory,+ label="initial → English",+ source_uri=initial_uri,+ target_uri=english_uri,+ instruction=to_english,+ event_uri=event_uri,+ )++ to_chinese = (+ f"把英文名为「{english_name}」的人物改回中文名「{source_name}」,同时把分类目录改成"+ f"中文“人物”。保留摄影老师、杭州、风光摄影和十月去西湖练习长曝光等事实;"+ f"不要创建重复实体或修改其他实体。"+ )+ await _run_rename_step(+ client,+ args,+ case,+ directory=directory,+ label="English → Chinese",+ source_uri=english_uri,+ target_uri=chinese_uri,+ instruction=to_chinese,+ event_uri=event_uri,+ )++ final = await _run_rename_step(+ client,+ args,+ case,+ directory=directory,+ label="Chinese → English",+ source_uri=chinese_uri,+ target_uri=english_uri,+ instruction=to_english,+ event_uri=event_uri,+ )++ chinese_directory = chinese_uri.rpartition("/")[0]+ try:+ await client.stat(chinese_directory)+ except Exception as exc:+ if getattr(exc, "code", None) != "NOT_FOUND":+ raise+ else:+ raise AssertionError(f"中文源目录在最后一个文件迁出后仍然存在: {chinese_directory}")++ console.rule(f"[bold]Rename round trip 最终状态 — {directory}[/bold]")+ _render_snapshot("往返改名后", directory, final)+ console.print(+ Panel(+ f"[bold]case:[/bold] {case.key}\n"+ f"[bold]初始 URI:[/bold] {initial_uri}\n"+ f"[bold]英文 URI:[/bold] {english_uri}\n"+ f"[bold]中文 URI:[/bold] {chinese_uri}\n"+ f"[bold]中文目录清理:[/bold] {chinese_directory} 已删除\n"+ "[bold green]往返三步全部验证通过[/bold green]",+ title="对比 — rename round trip",+ style="magenta",+ width=PANEL_WIDTH,+ )+ )+++def _memory_root_seed_files(root: str, user: str, token: str) -> tuple[MemoryFile, MemoryFile, str]:+ name = f"陶艺老师林澄_{token}"+ source_uri = f"{root}/entities/person/{name}.md"+ target_uri = f"{root}/entities/person/pottery_teacher_{token}.md"+ topic = f"pottery_class_{token}"+ preference_uri = f"{root}/preferences/{user}/{topic}.md"+ link = StoredLink(+ from_uri=preference_uri,+ to_uri=source_uri,+ match_text=name,+ description="偏好课程的授课老师",+ ).model_dump()+ entity = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ extra_fields={"memory_type": "entities", "category": "person", "name": name},+ content=f"# {name}\n她是住在苏州的陶艺老师。\n\n## 专业\n- 教授青瓷拉坯。\n\n## 关系\n- 为小美授课。",+ backlinks=[link],+ )+ preference = MemoryFile(+ uri=preference_uri,+ memory_type="preferences",+ extra_fields={"memory_type": "preferences", "user": user, "topic": topic},+ content=(+ f"- 喜欢{name}老师的陶艺小班课,每班最多六人,方便老师逐个指导。\n" * 8+ + "- [课程资料](https://example.com/docs)\n"+ ),+ links=[link],+ )+ return entity, preference, target_uri+++async def _run_memory_root_case(client, args, case: CompileCase, root: str) -> None:+ console.rule(f"[bold magenta]CASE: {case.key} — {case.description}[/bold magenta]")+ entity, preference, target_uri = _memory_root_seed_files(root, args.user, uuid.uuid4().hex[:8])+ for memory in (entity, preference):+ await client.write(memory.uri, MemoryFileUtils.write(memory), mode="create", wait=True)++ profile_uri = f"{root}/profile.md"+ try:+ await client.stat(profile_uri)+ except Exception as exc:+ if getattr(exc, "code", None) != "NOT_FOUND":+ raise+ profile = MemoryFile(+ uri=profile_uri,+ memory_type="profile",+ extra_fields={"memory_type": "profile"},+ content=f"# {DISPLAY_NAME}\n- 姓名:{DISPLAY_NAME} (as of 2023-04-09)\n",+ )+ await client.write(profile_uri, MemoryFileUtils.write(profile), mode="create", wait=True)+ await client.wait_processed()+ before_files = await _snapshot_memory_dir(client, root)+ before = {_entry_uri(entry): await client.read_raw(_entry_uri(entry)) for entry in before_files}+ _render_snapshot("根目录整理前", root, before_files)+ instruction = case.instruction.format(+ source_name=entity.extra_fields["name"],+ target_name=target_uri.rsplit("/", 1)[-1].removesuffix(".md"),+ topic=preference.extra_fields["topic"],+ )+ console.print(f" instruction: {instruction}")+ task = await _run_memory_compile(client, root, instruction)+ _render_change_lists(task["result"])+ await client.wait_processed()+ if args.wait > 0:+ await asyncio.sleep(args.wait)+ after_files = await _snapshot_memory_dir(client, root)+ after = {_entry_uri(entry): await client.read_raw(_entry_uri(entry)) for entry in after_files}+ _verify_memory_root_case(+ root=root,+ source_uri=entity.uri,+ target_uri=target_uri,+ preference_uri=preference.uri,+ task=task,+ before=before,+ after=after,+ )+ _render_snapshot("根目录整理后", root, after_files)+ console.print(+ Panel(+ f"task_id: {task.get('task_id')}\n"+ f"memory_types: {task['result']['memory_types']}\n"+ f"实体: {entity.uri} → {target_uri}\n偏好: {preference.uri} 原地精简\n"+ "单次根目录 compile 验证通过:两种类型实际变更、profile 纳入范围、事实和链接保留。",+ title="对比 — memory_root",+ width=PANEL_WIDTH,+ )+ )+++def _verify_memory_root_case(+ *,+ root: str,+ source_uri: str,+ target_uri: str,+ preference_uri: str,+ task: dict,+ before: dict[str, str],+ after: dict[str, str],+) -> None:+ result = task.get("result") or {}+ if task.get("status") != "completed" or task.get("error") or result.get("errors"):+ raise AssertionError(f"memory_root compile 未成功完成: {task}")+ if result.get("to") != root or result.get("memory_type") is not None:+ raise AssertionError(f"未按 memories 根目录整理: {result}")+ required_types = {"entities", "preferences", "profile"}+ if not required_types.issubset(set(result.get("memory_types") or [])):+ raise AssertionError(f"根目录未纳入所需类型: {result.get('memory_types')}")+ for kind, expected in (+ ("adds", {target_uri}),+ ("deletes", {source_uri}),+ ("updates", {preference_uri}),+ ):+ if set(result.get(kind) or []) != expected or result.get(f"total_{kind}") != len(expected):+ raise AssertionError(f"根目录 {kind} 不符合预期: {result}")+ if (+ source_uri not in before+ or target_uri in before+ or preference_uri not in before+ or f"{root}/profile.md" not in before+ ):+ raise AssertionError("memory_root 前置文件不完整或改名目标已存在")+ if set(after) != (set(before) - {source_uri}) | {target_uri}:+ raise AssertionError("根目录文件集合不符合预期,可能误删或新增了其它记忆")+ for uri, raw in before.items():+ if uri not in {source_uri, preference_uri} and after[uri] != raw:+ raise AssertionError(f"根目录整理修改了非目标记忆: {uri}")++ entity = MemoryFileUtils.read(after[target_uri], uri=target_uri)+ preference = MemoryFileUtils.read(after[preference_uri], uri=preference_uri)+ old_preference = MemoryFileUtils.read(before[preference_uri], uri=preference_uri)+ if entity.memory_type != "entities" or preference.memory_type != "preferences":+ raise AssertionError("实体或偏好 memory_type 错误")+ for alternatives in CASE_MEMORY_ROOT.required_facts:+ if not any(term.casefold() in entity.content.casefold() for term in alternatives):+ raise AssertionError(f"实体丢失独立事实: {alternatives}")+ for alternatives in (+ ("喜欢", "偏好", "prefer", "like"),+ ("小班", "small-group", "small group", "small class"),+ (+ "最多六人",+ "最多6人",+ "不超过六人",+ "不超过6人",+ "上限六人",+ "上限6人",+ "≤6",+ "at most six",+ "at most 6",+ "up to six",+ "up to 6",+ "maximum of six",+ "maximum of 6",+ "no more than six",+ "no more than 6",+ ),+ ("逐个", "逐一", "一对一", "个别", "individual", "one-on-one", "one by one"),+ ("指导", "guidance", "instruction", "coaching"),+ ):+ if not any(term in preference.content.casefold() for term in alternatives):+ raise AssertionError(f"偏好丢失独立事实: {alternatives}")+ if preference.content == old_preference.content or len(preference.content) >= len(+ old_preference.content+ ):+ raise AssertionError("偏好没有实际精简,不能以 links metadata 变化代替内容整理")+ if entity.extra_fields.get("category") != "person" or entity.extra_fields.get(+ "name"+ ) != target_uri.rsplit("/", 1)[-1].removesuffix(".md"):+ raise AssertionError("改名后的实体身份字段与目标 URI 不一致")+ if any(+ preference.extra_fields.get(field) != old_preference.extra_fields.get(field)+ for field in ("user", "topic")+ ):+ raise AssertionError("偏好不可变字段被修改")+ lines = [line.strip() for line in preference.content.splitlines() if line.strip()]+ if len(lines) != len(set(lines)):+ raise AssertionError("偏好正文仍有完全重复的行")+ if "](https://example.com/docs)" not in preference.content:+ raise AssertionError("外部课程资料链接丢失")+ expected_link = {**old_preference.links[0], "to_uri": target_uri}+ if expected_link not in preference.links or expected_link not in entity.backlinks:+ raise AssertionError("实体和偏好的 links/backlinks 未同步迁移")+ href = relpath(target_uri, preference_uri.rpartition("/")[0])+ if f"]({href})" not in preference.content:+ raise AssertionError("偏好正文中缺少新实体的内部链接")+ old_relative = source_uri.removeprefix(root + "/")+ if any(source_uri in raw or old_relative in raw for raw in after.values()):+ raise AssertionError("根目录中仍残留旧实体 URI/href")+++# ── Phase 2: 触发 memory compile 并轮询 ─────────────────────────────────────+++async def _run_memory_compile(client, directory: str, instruction: str) -> dict:+ console.print()+ console.print(f" [yellow]触发 compile(skill=memory)... to={directory}[/yellow]")+ options = {"instruction": instruction} if instruction else None+ accepted = await client.compile(+ from_uris=[], # memory 模式不需要 --from,就地整理 --to+ to=directory,+ skill="memory",+ options=options,+ )+ console.print(f" Accepted: {accepted}")+ # client.compile 走 _handle_response,已经取过 .get("result"),+ # 所以 accepted 通常就是 result 本身;兜底再剥一层。+ result = accepted if isinstance(accepted, dict) else {}+ if isinstance(result.get("result"), dict):+ result = result["result"]+ task_id = result.get("task_id") or result.get("resource_id")++ if not task_id:+ raise AssertionError(f"compile 未返回 task_id: {accepted}")++ console.print(f" [yellow]等待整理完成 (task_id={task_id})...[/yellow]")+ now = time.time()+ task = None+ while True:+ task = await client.get_task(task_id)+ if not task or task.get("status") in ("completed", "failed", "cancelled"):+ break+ await asyncio.sleep(1)+ elapsed = time.time() - now+ status = task.get("status", "unknown") if task else "not found"+ console.print(f" [green]任务 {status},耗时 {elapsed:.2f}s[/green]")+ console.print(f" Task 详情: {task}")+ if (+ not task+ or status != "completed"+ or task.get("error")+ or (task.get("result") or {}).get("errors")+ ):+ raise AssertionError(f"compile 未成功完成: {task}")+ if not isinstance(task.get("result"), dict):+ raise AssertionError(f"compile 未返回结果: {task}")+ return task+++# ── 入口 ───────────────────────────────────────────────────────────────────+++async def _run_case(client, args, case: CompileCase) -> None:+ """跑单个 compile case:种子对话 → 整理前快照 → compile → 整理后对比。"""+ directory = _case_directory(args, case)+ if case.key == "memory_root":+ await _run_memory_root_case(client, args, case, directory)+ return+ source_name = None+ target_name = None+ if case.rename_target_prefix:+ source_name, target_name = _unique_rename_names(case)++ console.rule(f"[bold magenta]CASE: {case.key} — {case.description}[/bold magenta]")++ if source_name and target_name:+ console.rule("[bold]Phase 0b: 创建中文实体与关联事件(不经过语言抽取)[/bold]")+ initial_uri, event_uri = await _seed_rename_case(+ client,+ directory,+ source_name,+ target_name,+ )+ else:+ console.rule(f"[bold]Phase 0b: 种子对话 case={case.key}[/bold]")+ await _ingest_case_sessions(client, case, wait_seconds=args.wait)++ console.rule(f"[bold]Phase 1: 整理前 — {directory}[/bold]")+ before = await _snapshot_memory_dir(client, directory)+ _render_snapshot("整理前", directory, before)+ if source_name and target_name:+ await _run_rename_round_trip(+ client,+ args,+ case,+ directory=directory,+ source_name=source_name,+ english_name=target_name,+ initial_uri=initial_uri,+ event_uri=event_uri,+ )+ return++ console.rule("[bold]Phase 2: 执行 memory compile[/bold]")+ task = await _run_memory_compile(client, directory, case.instruction)++ result = task.get("result") or {}+ trace_id = result.get("trace_id") or task.get("trace_id") or ""+ if trace_id:+ console.print(f" [bold cyan]trace_id: {trace_id}[/bold cyan]")++ _render_change_lists(result)++ # 等待向量化/overview 刷新+ await client.wait_processed()+ if args.wait > 0:+ await asyncio.sleep(args.wait)++ console.rule(f"[bold]Phase 3: 整理后 — {directory}[/bold]")+ after = await _snapshot_memory_dir(client, directory)+ _render_snapshot("整理后", directory, after)++ console.print()+ console.print(+ Panel(+ f"[bold]case:[/bold] {case.key}\n"+ f"[bold]整理前:[/bold] {len(before)} 个文件\n"+ f"[bold]整理后:[/bold] {len(after)} 个文件\n"+ f"[bold]净变化:[/bold] {len(after) - len(before):+d}",+ title=f"对比 — {case.key}",+ style="magenta",+ width=PANEL_WIDTH,+ )+ )+++async def _main_async(args) -> None:+ client = ov.AsyncHTTPClient(+ url=args.url,+ api_key=resolve_api_key(args.api_key),+ account=args.account,+ user=args.user,+ timeout=600,+ )++ # 选择要跑的 case:--case all 跑全部,否则跑指定的一个。+ if args.case == "all":+ selected = list(CASES.values())+ else:+ selected = [CASES[args.case]]+ for case in selected:+ _case_directory(args, case)++ try:+ await client.initialize()+ console.print(f" [green]已连接[/green] {args.url}")++ if args.ingest:+ console.rule("[bold]Phase 0: 灌入小美对话数据[/bold]")+ await _run_ingest_async(client, session_id=args.session_id, wait_seconds=args.wait)++ for case in selected:+ await _run_case(client, args, case)++ except Exception as e:+ console.print(Panel(f"[bold red]Error:[/bold red] {e}", style="red", width=PANEL_WIDTH))+ import traceback++ traceback.print_exc()+ raise++ finally:+ await client.close()+++def main():+ parser = argparse.ArgumentParser(+ description=f"OpenViking memory-mode compile 集成脚本 — {DISPLAY_NAME}"+ )+ parser.add_argument("--url", default=DEFAULT_URL, help=f"Server URL (默认: {DEFAULT_URL})")+ parser.add_argument("--api-key", default=DEFAULT_API_KEY, help=API_KEY_HELP)+ parser.add_argument(+ "--account", default=DEFAULT_ACCOUNT, help=f"OpenViking account (默认: {DEFAULT_ACCOUNT})"+ )+ parser.add_argument(+ "--user", default=DEFAULT_USER, help=f"OpenViking user (默认: {DEFAULT_USER})"+ )+ parser.add_argument(+ "--to",+ default=None,+ help="直接指定要整理的记忆目录 URI(覆盖 case 的 memory_type)。",+ )+ parser.add_argument(+ "--ingest",+ action=argparse.BooleanOptionalAction,+ default=True,+ help="整理前先灌一遍小美对话数据 (默认: 是;用 --no-ingest 关闭)。",+ )+ parser.add_argument(+ "--case",+ choices=["all", *sorted(CASES.keys())],+ default="all",+ help=(+ "compile 集成 case(默认: all,依次跑全部)。"+ "merge=合并两个同人不同称呼;split=拆分同名两人;"+ "dedup=原地精简重复表述;rename=目录和文件名中英文往返;"+ "preferences=整理 preferences 类型;memory_root=单任务整理多类型并校验文件和链接。"+ ),+ )+ parser.add_argument(+ "--session-id", default=DEFAULT_SESSION_ID, help=f"Session ID (默认: {DEFAULT_SESSION_ID})"+ )+ parser.add_argument("--wait", type=float, default=5.0, help="每阶段后额外等待秒数 (默认: 5)")+ args = parser.parse_args()++ console.print(+ Panel(+ f"[bold]OpenViking memory compile — {DISPLAY_NAME}[/bold]\n"+ f"Server: {args.url} | case: {args.case} | ingest: {args.ingest}",+ style="magenta",+ width=PANEL_WIDTH,+ )+ )++ asyncio.run(_main_async(args))+++if __name__ == "__main__":+ main()diff --git a/tests/integration/test_compressor_v2_xiaomei.py b/tests/integration/test_compressor_v2_xiaomei.pyindex b2554c79a..aa3a7a30f 100644--- a/tests/integration/test_compressor_v2_xiaomei.py+++ b/tests/integration/test_compressor_v2_xiaomei.py@@ -4,6 +4,7 @@ OpenViking 记忆演示脚本 — 用户: 小美(日常生活记录)"""import argparse+import asyncioimport timefrom datetime import datetime@@ -22,9 +23,11 @@ except ModuleNotFoundError: # pytest/package import path# ── 常量 ───────────────────────────────────────────────────────────────────DISPLAY_NAME = "小美"-DEFAULT_URL = "http://localhost:1934"+DEFAULT_URL = "http://localhost:1933"PANEL_WIDTH = 78DEFAULT_API_KEY = None+DEFAULT_ACCOUNT = "default"+DEFAULT_USER = "xiaomei"DEFAULT_SESSION_ID = "xiaomei-demo"ASSISTANT_PEER_ID = "xiaomei-demo-assistant"@@ -106,12 +109,12 @@ VERIFY_QUERIES = [# ── Phase 1: 写入对话并提交 ────────────────────────────────────────────────-def run_ingest(client: ov.SyncHTTPClient, session_id: str, wait_seconds: float):+async def _run_ingest_async(client, session_id: str, wait_seconds: float):console.print()console.rule(f"[bold]Phase 1: 写入对话 — {DISPLAY_NAME} ({len(CONVERSATION)} 轮)[/bold]")# 获取 session;若不存在则由服务端按 session_id 自动创建- session = client.create_session()+ session = await client.create_session()session_id = session.get("session_id")print(f"session_id={session_id}")console.print(f" Session: [bold cyan]{session_id}[/bold cyan]")@@ -125,17 +128,17 @@ def run_ingest(client: ov.SyncHTTPClient, session_id: str, wait_seconds: float):total = len(CONVERSATION)for i, turn in enumerate(CONVERSATION, 1):console.print(f" [dim][{i}/{total}][/dim] 添加 user + assistant 消息...")- client.add_message(+ await client.add_message(session_id,role="user",parts=[{"type": "text", "text": turn["user"]}],- created_at=session_time_str,+ options={"created_at": session_time_str},)- client.add_message(+ await client.add_message(session_id,role="assistant",parts=[{"type": "text", "text": turn["assistant"]}],- created_at=session_time_str,+ options={"created_at": session_time_str},peer_id=ASSISTANT_PEER_ID,)@@ -145,7 +148,7 @@ def run_ingest(client: ov.SyncHTTPClient, session_id: str, wait_seconds: float):# 提交 session — 触发记忆抽取console.print()console.print(" [yellow]提交 Session(触发记忆抽取)...[/yellow]")- commit_result = client.commit_session(session_id)+ commit_result = await client.commit_session(session_id)task_id = commit_result.get("task_id")trace_id = commit_result.get("trace_id")console.print(f" [bold cyan]trace_id: {trace_id}[/bold cyan]")@@ -156,10 +159,10 @@ def run_ingest(client: ov.SyncHTTPClient, session_id: str, wait_seconds: float):now = time.time()console.print(f" [yellow]等待记忆提取完成 (task_id={task_id})...[/yellow]")while True:- task = client.get_task(task_id)+ task = await client.get_task(task_id)if not task or task.get("status") in ("completed", "failed"):break- time.sleep(1)+ await asyncio.sleep(1)elapsed = time.time() - nowstatus = task.get("status", "unknown") if task else "not found"console.print(f" [green]任务 {status},耗时 {elapsed:.2f}s[/green]")@@ -167,13 +170,13 @@ def run_ingest(client: ov.SyncHTTPClient, session_id: str, wait_seconds: float):# 等待向量化队列处理完成console.print(" [yellow]等待向量化完成...[/yellow]")- client.wait_processed()+ await client.wait_processed()if wait_seconds > 0:console.print(f" [dim]额外等待 {wait_seconds:.0f}s...[/dim]")- time.sleep(wait_seconds)+ await asyncio.sleep(wait_seconds)- session_info = client.get_session(session_id)+ session_info = await client.get_session(session_id)console.print(f" Session 详情: {session_info}")return session_id@@ -182,7 +185,7 @@ def run_ingest(client: ov.SyncHTTPClient, session_id: str, wait_seconds: float):# ── Phase 2: 验证记忆召回 ─────────────────────────────────────────────────-def run_verify(client: ov.SyncHTTPClient):+async def _run_verify_async(client):console.print()console.rule(f"[bold]Phase 2: 验证记忆召回 — {DISPLAY_NAME} ({len(VERIFY_QUERIES)} 条查询)[/bold]"@@ -208,36 +211,42 @@ def run_verify(client: ov.SyncHTTPClient):console.print(f" [dim]期望关键词: {', '.join(expected)}[/dim]")try:- results = client.find(query, limit=5)+ results = await client.find(query, limit=5)# 收集所有召回内容recall_texts = []count = 0- if hasattr(results, "memories") and results.memories:- for m in results.memories:- text = getattr(m, "content", "") or getattr(m, "text", "") or str(m)+ memories = results.get("memories") if isinstance(results, dict) else None+ if memories:+ for m in memories:+ text = m.get("content") or m.get("text") or str(m)print(f" [DEBUG] memory text: {repr(text)}")recall_texts.append(text)- uri = getattr(m, "uri", "")- score = getattr(m, "score", 0)+ uri = m.get("uri", "")+ score = m.get("score", 0) or 0console.print(f" [green]Memory:[/green] {uri} (score: {score:.4f})")console.print(f" [dim]{text[:120]}...[/dim]"if len(text) > 120else f" [dim]{text}[/dim]")- count += len(results.memories)+ count += len(memories)- if hasattr(results, "resources") and results.resources:- for r in results.resources:- text = getattr(r, "content", "") or getattr(r, "text", "") or str(r)+ resources = results.get("resources") if isinstance(results, dict) else None+ if resources:+ for r in resources:+ text = r.get("content") or r.get("text") or str(r)print(f" [DEBUG] resource text: {repr(text)}")recall_texts.append(text)- console.print(f" [blue]Resource:[/blue] {r.uri} (score: {r.score:.4f})")- count += len(results.resources)+ console.print(+ f" [blue]Resource:[/blue] {r.get('uri', '')} "+ f"(score: {(r.get('score', 0) or 0):.4f})"+ )+ count += len(resources)- if hasattr(results, "skills") and results.skills:- count += len(results.skills)+ skills = results.get("skills") if isinstance(results, dict) else None+ if skills:+ count += len(skills)# 检查关键词命中all_text = " ".join(recall_texts)@@ -261,6 +270,12 @@ def main():parser = argparse.ArgumentParser(description=f"OpenViking 记忆演示 — {DISPLAY_NAME}")parser.add_argument("--url", default=DEFAULT_URL, help=f"Server URL (默认: {DEFAULT_URL})")parser.add_argument("--api-key", default=DEFAULT_API_KEY, help=API_KEY_HELP)+ parser.add_argument(+ "--account", default=DEFAULT_ACCOUNT, help=f"OpenViking account (默认: {DEFAULT_ACCOUNT})"+ )+ parser.add_argument(+ "--user", default=DEFAULT_USER, help=f"OpenViking user (默认: {DEFAULT_USER})"+ )parser.add_argument("--phase",choices=["all", "ingest", "verify"],@@ -282,17 +297,27 @@ def main():))- client = ov.SyncHTTPClient(url=args.url, api_key=resolve_api_key(args.api_key), timeout=180)+ asyncio.run(_main_async(args))+++async def _main_async(args):+ client = ov.AsyncHTTPClient(+ url=args.url,+ api_key=resolve_api_key(args.api_key),+ account=args.account,+ user=args.user,+ timeout=180,+ )try:- client.initialize()+ await client.initialize()console.print(f" [green]已连接[/green] {args.url}")if args.phase in ("all", "ingest"):- run_ingest(client, session_id=args.session_id, wait_seconds=args.wait)+ await _run_ingest_async(client, session_id=args.session_id, wait_seconds=args.wait)if args.phase in ("all", "verify"):- run_verify(client)+ await _run_verify_async(client)console.print(Panel(@@ -309,7 +334,7 @@ def main():traceback.print_exc()finally:- client.close()+ await client.close()if __name__ == "__main__":diff --git a/tests/session/memory/test_consolidation_compile.py b/tests/session/memory/test_consolidation_compile.pynew file mode 100644index 000000000..085733b1c--- /dev/null+++ b/tests/session/memory/test_consolidation_compile.py@@ -0,0 +1,1268 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Tests for `ov compile --skill memory` in-place memory consolidation."""++import asyncio+from types import SimpleNamespace+from unittest.mock import AsyncMock, Mock++import pytest++from openviking.server.identity import RequestContext, Role+from openviking.service.compile_service import MEMORY_COMPILE_SKILL, CompileRequest+from openviking.service.memory_compile import (+ MemoryCompileRunner,+ _discover_memory_types,+ _memory_type_from_target,+ _peer_id_from_memory_uri,+)+from openviking.service.task_tracker import TaskStatus, TaskTracker+from openviking.service.task_work_index import get_task_context+from openviking.session.memory.consolidation_context_provider import (+ ConsolidationExtractContextProvider,+ build_consolidation_isolation_handler,+)+from openviking.session.memory.dataclass import (+ MemoryField,+ MemoryFile,+ MemoryTypeSchema,+ ResolvedOperation,+ ResolvedOperations,+)+from openviking.session.memory.memory_type_registry import MemoryTypeRegistry, get_default_registry+from openviking.session.memory.memory_updater import (+ ExtractContext,+ MemoryUpdater,+ MemoryUpdateResult,+)+from openviking.session.memory.merge_op import FieldType, MergeOp+from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils+from openviking_cli.exceptions import InvalidArgumentError, NotFoundError+from openviking_cli.session.user_id import UserIdentifier+++def _ctx(user_id: str = "u1") -> RequestContext:+ return RequestContext(+ user=UserIdentifier(account_id="acc", user_id=user_id),+ role=Role.USER,+ )+++# ── CompileRequest memory-mode validation ──+++def test_compile_request_memory_mode_needs_no_from():+ request = CompileRequest(**{"to": "viking://user/u1/memories/entities", "skill": "memory"})+ assert request.is_memory_mode is True+ assert request.skill == MEMORY_COMPILE_SKILL+ assert request.from_ == []+++def test_compile_request_memory_mode_rejects_from():+ with pytest.raises(ValueError):+ CompileRequest(+ **{+ "to": "viking://user/u1/memories/entities",+ "skill": "memory",+ "from": ["viking://resources/x"],+ }+ )+++def test_compile_request_normal_mode_requires_from():+ with pytest.raises(ValueError):+ CompileRequest(**{"to": "viking://resources/wiki", "skill": "viking://agent/skills/wiki"})++ request = CompileRequest(+ **{+ "to": "viking://resources/wiki",+ "skill": "viking://agent/skills/wiki",+ "from": ["viking://resources/y"],+ }+ )+ assert request.is_memory_mode is False+ assert request.from_ == ["viking://resources/y"]+++# ── Target parsing helpers ──+++def test_memory_type_from_target_reads_type_segment():+ assert _memory_type_from_target("viking://user/u1/memories/entities") == "entities"+ assert (+ _memory_type_from_target("viking://user/u1/peers/agent_x/memories/experiences")+ == "experiences"+ )+++def test_memory_type_from_target_accepts_memory_roots():+ assert _memory_type_from_target("viking://user/u1/memories") is None+ assert _memory_type_from_target("viking://user/u1/peers/agent_x/memories") is None+++def test_memory_type_from_target_rejects_non_memory_and_user_roots():+ with pytest.raises(InvalidArgumentError):+ _memory_type_from_target("viking://resources/wiki")+ with pytest.raises(InvalidArgumentError):+ _memory_type_from_target("viking://user/u1")++[email protected]("peer_id", [None, "agent_x"])+async def test_discover_root_types_uses_schema_paths_and_existing_files(peer_id):+ root = f"viking://user/u1/{'peers/' + peer_id + '/' if peer_id else ''}memories"+ registry = MemoryTypeRegistry(load_schemas=False)+ defaults = get_default_registry()+ for name in ("entities", "events", "preferences", "profile", "identity", "soul", "cases"):+ registry.register(defaults.get(name).model_copy(deep=True))+ disabled = defaults.get("entities").model_copy(+ update={"memory_type": "disabled", "enabled": False}+ )+ outside = defaults.get("entities").model_copy(+ update={"memory_type": "outside", "directory": "viking://user/u2/memories/entities"}+ )+ registry.register(disabled)+ registry.register(outside)+ paths = {+ f"{root}/entities": True,+ f"{root}/events": True,+ f"{root}/profile.md": False,+ f"{root}/identity.md": False,+ f"{root}/soul.md": False,+ f"{root}/cases": True,+ f"{root}/unknown": True,+ }++ async def stat(uri, ctx, skip_count=False):+ assert ctx == _ctx()+ if uri not in paths:+ raise NotFoundError(uri)+ return {"isDir": paths[uri]}++ fs = SimpleNamespace(stat=AsyncMock(side_effect=stat))+ types = await _discover_memory_types(root, registry, fs, _ctx())++ assert types == sorted(+ ["entities", "events", "profile", "identity", "soul"] + ([] if peer_id else ["cases"])+ )+ assert all(call.args[0].startswith(root + "/") for call in fs.stat.await_args_list)+ assert f"{root}/unknown" not in [call.args[0] for call in fs.stat.await_args_list]+ if peer_id:+ assert f"{root}/cases" not in [call.args[0] for call in fs.stat.await_args_list]+++async def test_discover_root_types_propagates_storage_errors():+ fs = SimpleNamespace(stat=AsyncMock(side_effect=RuntimeError("storage unavailable")))+ with pytest.raises(RuntimeError, match="storage unavailable"):+ await _discover_memory_types(+ "viking://user/u1/memories", get_default_registry(), fs, _ctx()+ )+++ "target", ["viking://user/u1/memories", "viking://user/u1/peers/agent_x/memories"]+)+async def test_root_compile_creates_one_task_and_one_loop(monkeypatch, language_config, target):+ language_config.output_language_override = "en"+ vlm = SimpleNamespace(model="test", get_completion_async=AsyncMock(return_value="sdk.commit()"))+ language_config.vlm = SimpleNamespace(get_vlm_instance=lambda: vlm)+ store = SimpleNamespace(+ create=AsyncMock(),+ update=AsyncMock(),+ get=AsyncMock(return_value=None),+ list=AsyncMock(return_value=[]),+ delete=AsyncMock(),+ )+ tracker = TaskTracker(store)+ monkeypatch.setattr("openviking.service.memory_compile.get_task_tracker", lambda: tracker)+ uris = [+ f"{target}/entities/person/alice.md",+ f"{target}/preferences/alice/food.md",+ f"{target}/profile.md",+ ]+ existing = {+ target: True,+ f"{target}/entities": True,+ f"{target}/preferences": True,+ f"{target}/profile.md": False,+ }++ async def stat(uri, ctx, skip_count=False):+ if uri not in existing:+ raise NotFoundError(uri)+ return {"uri": uri, "isDir": existing[uri]}++ fs = SimpleNamespace(+ stat=AsyncMock(side_effect=stat),+ glob=AsyncMock(+ return_value={+ "matches": [{"uri": uri, "isDir": False, "size": 100} for uri in uris],+ "count": 3,+ }+ ),+ )+ service = SimpleNamespace(+ ensure_write_access=AsyncMock(), stat=fs.stat, _ensure_initialized=lambda: fs+ )+ apply = AsyncMock(return_value=MemoryUpdateResult())+ monkeypatch.setattr("openviking.service.memory_compile.MemoryUpdater.apply_operations", apply)+ acquire = AsyncMock(return_value=None)+ monkeypatch.setattr("openviking.service.memory_compile.acquire_memory_operation_lease", acquire)+ runner = MemoryCompileRunner(service)++ accepted = await runner.create(target=target, instruction="整理全部已有记忆", ctx=_ctx())+ await asyncio.gather(*list(runner._running))+ final = await tracker.get(accepted.task_id, account_id="acc", user_id="u1")++ store.create.assert_awaited_once()+ vlm.get_completion_async.assert_awaited_once()+ fs.glob.assert_awaited_once()+ assert fs.glob.await_args.kwargs["uri"] == target+ apply.assert_awaited_once()+ acquire.assert_awaited_once()+ handler = apply.await_args.kwargs["isolation_handler"]+ assert handler.allowed_memory_types == {"entities", "preferences", "profile"}+ assert handler.allowed_peer_ids == ({"agent_x"} if "/peers/" in target else set())+ assert handler.allow_self == ("/peers/" not in target)+ assert final.status == TaskStatus.COMPLETED+ assert final.meta["request"]["memory_type"] is None+ assert final.result["memory_type"] is None+ assert final.result["memory_types"] == ["entities", "preferences", "profile"]+ assert final.result["errors"] == []+ prompt = vlm.get_completion_async.await_args.kwargs["messages"][0]["content"]+ for name in ("entities", "preferences", "profile"):+ assert name in prompt+ assert "create_events" not in prompt+ assert not tracker.has_work(accepted.task_id)+++async def test_root_compile_with_no_registered_memories_is_noop(monkeypatch, language_config):+ fs = SimpleNamespace(stat=AsyncMock(side_effect=NotFoundError("missing")))+ get_vlm = Mock(side_effect=AssertionError("empty root must not call the model"))+ language_config.vlm = SimpleNamespace(get_vlm_instance=get_vlm)+ loop = AsyncMock()+ monkeypatch.setattr("openviking.service.memory_compile.ExtractLoop.run", loop)+ runner = MemoryCompileRunner(SimpleNamespace(_ensure_initialized=lambda: fs))+ result = await runner._consolidate(+ target="viking://user/u1/memories",+ memory_type=None,+ peer_id=None,+ instruction=None,+ ctx=_ctx(),+ )+ assert result["memory_types"] == []+ assert result["errors"] == []+ assert result["adds"] == result["updates"] == result["deletes"] == []+ loop.assert_not_awaited()+ get_vlm.assert_not_called()++[email protected]("peer_id", [None, "agent_x"])+async def test_root_compile_updates_two_types_through_real_loop_and_updater(+ monkeypatch, language_config, peer_id+):+ root = f"viking://user/u1/{'peers/' + peer_id + '/' if peer_id else ''}memories"+ registry = MemoryTypeRegistry(load_schemas=False)+ for name in ("entities", "preferences"):+ registry.register(+ MemoryTypeSchema(+ memory_type=name,+ directory=f"viking://user/{{{{ user_space }}}}/memories/{name}",+ filename_template="{{ name }}.md",+ content_template="{{ content }}",+ fields=[+ MemoryField(name="name", field_type=FieldType.STRING, merge_op=MergeOp.REPLACE),+ MemoryField(+ name="content", field_type=FieldType.STRING, merge_op=MergeOp.PATCH+ ),+ ],+ )+ )+ monkeypatch.setattr("openviking.service.memory_compile.get_default_registry", lambda: registry)+ language_config.output_language_override = "en"+ uris = [f"{root}/{name}/alice.md" for name in ("entities", "preferences")]+ files = {+ uri: MemoryFileUtils.write(+ MemoryFile(+ uri=uri,+ memory_type=name,+ content="old",+ extra_fields={"name": "alice", "memory_type": name},+ )+ )+ for uri, name in zip(uris, ("entities", "preferences"), strict=True)+ }++ async def stat(uri, ctx, skip_count=False):+ if uri in files:+ return {"isDir": False}+ if any(path.startswith(uri + "/") for path in files):+ return {"isDir": True}+ raise NotFoundError(uri)++ async def read_file(uri, ctx):+ if uri not in files:+ raise NotFoundError(uri)+ return files[uri]++ async def write_file(uri, content, ctx, lease_ref=None):+ files[uri] = content++ fs = SimpleNamespace(+ stat=AsyncMock(side_effect=stat),+ read_file=AsyncMock(side_effect=read_file),+ write_file=AsyncMock(side_effect=write_file),+ glob=AsyncMock(+ return_value={+ "matches": [{"uri": uri, "isDir": False, "size": 100} for uri in uris],+ "count": 2,+ }+ ),+ )+ original_prefetch = ConsolidationExtractContextProvider.prefetch++ async def prefetch(provider):+ messages = await original_prefetch(provider)+ for uri in uris:+ assert await provider.read_file(uri) is not None+ return messages++ monkeypatch.setattr(ConsolidationExtractContextProvider, "prefetch", prefetch)+ vlm = SimpleNamespace(+ model="test",+ get_completion_async=AsyncMock(+ return_value=(+ 'entities_1.content.update("""Alice is a designer.""")\n'+ 'preferences_1.content.update("""Alice likes tea.""")\nsdk.commit()'+ )+ ),+ )+ language_config.vlm = SimpleNamespace(get_vlm_instance=lambda: vlm)+ monkeypatch.setattr("openviking.session.memory.memory_updater.get_viking_fs", lambda: fs)+ monkeypatch.setattr(+ "openviking.session.memory.memory_updater.MemoryUpdater.generate_overview", AsyncMock()+ )+ monkeypatch.setattr(+ "openviking.session.memory.memory_updater.MemoryUpdater._sync_resource_refs_for_result",+ AsyncMock(),+ )+ monkeypatch.setattr(+ "openviking.service.memory_compile.acquire_memory_operation_lease",+ AsyncMock(return_value=None),+ )+ runner = MemoryCompileRunner(SimpleNamespace(_ensure_initialized=lambda: fs))++ result = await runner._consolidate(+ target=root, memory_type=None, peer_id=peer_id, instruction="整理这两类记忆", ctx=_ctx()+ )++ assert result["errors"] == []+ assert result["memory_types"] == ["entities", "preferences"]+ assert set(result["updates"]) == set(uris)+ assert result["adds"] == result["deletes"] == []+ assert "Alice is a designer." in files[uris[0]]+ assert "Alice likes tea." in files[uris[1]]+ assert fs.write_file.await_count == 2+ assert set(files) == set(uris)+ vlm.get_completion_async.assert_awaited_once()+++def test_peer_id_from_memory_uri():+ assert _peer_id_from_memory_uri("viking://user/u1/memories/entities") is None+ assert _peer_id_from_memory_uri("viking://user/u1/peers/agent_x/memories/entities") == "agent_x"+++# ── Isolation handler scope ──+++def test_consolidation_isolation_self_vs_peer():+ ctx = _ctx()+ self_handler = build_consolidation_isolation_handler(+ ctx, ExtractContext([]), memory_types=["entities"], peer_id=None+ )+ assert self_handler.allow_self is True+ assert self_handler.allowed_peer_ids == set()++ peer_handler = build_consolidation_isolation_handler(+ ctx, ExtractContext([]), memory_types=["entities"], peer_id="agent_x"+ )+ assert peer_handler.allow_self is False+ assert peer_handler.allowed_peer_ids == {"agent_x"}+++# ── Provider schema scope and prefetch ──+++def test_provider_loads_single_schema_from_to():+ ctx = _ctx()+ provider = ConsolidationExtractContextProvider(memory_types=["entities"])+ schemas = provider.get_memory_schemas(ctx)+ assert [s.memory_type for s in schemas] == ["entities"]+++def test_provider_unknown_type_raises():+ provider = ConsolidationExtractContextProvider(memory_types=["does_not_exist"])+ with pytest.raises(ValueError):+ provider.get_memory_schemas(_ctx())+++async def test_prefetch_seeds_recursive_listing_and_lets_model_explore():+ ctx = _ctx()+ directory = "viking://user/u1/memories/entities"+ # glob backs the recursive ls: return files across subdirectories.+ glob_result = {+ "matches": [+ {"uri": f"{directory}/person/alice.md", "isDir": False, "size": 100},+ {"uri": f"{directory}/media/book.md", "isDir": False, "size": 200},+ {"uri": f"{directory}/person", "isDir": True, "size": 0},+ {"uri": f"{directory}/.overview.md", "isDir": False, "size": 50},+ ],+ "count": 4,+ }+ viking_fs = SimpleNamespace(glob=AsyncMock(return_value=glob_result))+ provider = ConsolidationExtractContextProvider(+ memory_types=["entities"],+ target_directory=directory,+ ctx=ctx,+ viking_fs=viking_fs,+ )++ messages = await provider.prefetch()+ tool_context = provider.create_tool_context()+ assert tool_context.request_ctx is ctx+ assert tool_context.viking_fs is viking_fs++ # A recursive ls seed is added as a tool-call pair, then a user instruction.+ seeded = "\n".join(str(m.get("content", "")) for m in messages)+ assert "person/alice.md" in seeded+ assert "media/book.md" in seeded+ # Reserved overview files are filtered out of the listing.+ assert ".overview.md" not in seeded+ assert messages[-1]["role"] == "user"+ assert "recursive listing" in messages[-1]["content"]+ assert "consolidation operations" in messages[-1]["content"]+++def test_provider_exposes_ls_search_read_tools():+ provider = ConsolidationExtractContextProvider(memory_types=["entities"])+ assert provider.get_tools() == ["ls", "search", "read"]+++def test_instruction_mentions_type_and_explore_tools():+ provider = ConsolidationExtractContextProvider(+ memory_types=["entities"], instruction="Only touch pets."+ )+ text = provider.instruction()+ assert "entities" in text+ assert "recursive=true" in text+ assert "read" in text+ assert "no write tool" in text+ assert "Only touch pets." in text+++def language_config(monkeypatch):+ registry = get_default_registry()+ config = SimpleNamespace(+ output_language_override="",+ memory=SimpleNamespace(+ eager_prefetch=False,+ prefetch_search_topn=5,+ link_enabled=False,+ maintenance_review_tokens=2000,+ ),+ vlm=SimpleNamespace(),+ registry=registry,+ )+ for module in (+ "openviking.service.memory_compile",+ "openviking.session.memory.consolidation_context_provider",+ "openviking.session.memory.session_extract_context_provider",+ "openviking.session.memory.extract_loop",+ "openviking.session.memory.utils.language",+ "openviking_cli.utils.config",+ ):+ monkeypatch.setattr(f"{module}.get_openviking_config", lambda: config)+ monkeypatch.setattr("openviking.service.memory_compile.get_default_registry", lambda: registry)++ async def _passthrough_registry(_fs, _account, base):+ return base++ monkeypatch.setattr(+ "openviking.session.memory.account_templates.resolve_account_memory_registry",+ _passthrough_registry,+ )+ return config++[email protected]("override, expected", [("", "zh-CN"), ("ja", "ja")])+async def test_compile_resolves_language_before_prompt_and_schema(+ monkeypatch, language_config, override, expected+):+ language_config.output_language_override = override+ vlm = SimpleNamespace(+ model="test-model", get_completion_async=AsyncMock(return_value="sdk.commit()")+ )+ language_config.vlm = SimpleNamespace(get_vlm_instance=lambda: vlm)+ directory = "viking://user/u1/memories/entities"+ uri = f"{directory}/person/alice.md"+ content = "# 小丽\n小丽是小美的同事,她们经常一起吃午饭,也会一起讨论活动文案。"+ viking_fs = SimpleNamespace(+ glob=AsyncMock(return_value={"matches": [{"uri": uri, "size": 200, "isDir": False}]}),+ read=AsyncMock(return_value=content.encode()),+ read_file=AsyncMock(side_effect=AssertionError("must not prefetch full content")),+ )+ registry = language_config.registry+ schema = registry.get("entities").model_copy(deep=True)+ schema.description = "Memory schema language: {{ language }}."+ schema.fields[-1].description = "Field language: {{ language }}."+ monkeypatch.setattr(registry, "get", lambda name: schema)+ apply = AsyncMock(return_value=MemoryUpdateResult())+ monkeypatch.setattr("openviking.service.memory_compile.MemoryUpdater.apply_operations", apply)+ acquire_lease = AsyncMock(return_value=None)+ monkeypatch.setattr(+ "openviking.service.memory_compile.acquire_memory_operation_lease",+ acquire_lease,+ )+ runner = MemoryCompileRunner(SimpleNamespace(_ensure_initialized=lambda: viking_fs))++ result = await runner._consolidate(+ target=directory,+ memory_type="entities",+ peer_id=None,+ instruction="Merge duplicate memories without losing facts.",+ ctx=_ctx(),+ )++ prompt = vlm.get_completion_async.call_args.kwargs["messages"][0]["content"]+ assert f"All memory content MUST be written in {expected}." in prompt+ assert f"Memory schema language: {expected}." in prompt+ assert f"Field language: {expected}." in prompt+ assert "{{ language }}" not in prompt+ assert content not in str(vlm.get_completion_async.call_args.kwargs["messages"])+ assert result["errors"] == []+ apply.assert_awaited_once()+ acquire_lease.assert_awaited_once()+ if override:+ viking_fs.read.assert_not_awaited()+ else:+ viking_fs.read.assert_awaited_once_with(uri, size=4096, ctx=_ctx())+ assert content not in str(vlm.get_completion_async.call_args.kwargs["messages"])+++async def test_compile_prefers_account_registry(monkeypatch, language_config):+ language_config.output_language_override = "en"+ language_config.vlm = SimpleNamespace(get_vlm_instance=lambda: SimpleNamespace(model="test"))+ directory = "viking://user/u1/memories/entities"+ deployment = language_config.registry+ account_registry = MemoryTypeRegistry(load_schemas=False)+ for schema in deployment.list_all(include_disabled=True):+ account_registry.register(schema.model_copy(deep=True))+ account_registry.get("soul").content_template = "# Company Assistant\n{{ content }}"++ captured = {}++ async def fake_resolver(fs, account_id, base_registry):+ captured["resolver_called_with"] = (fs, account_id, base_registry)+ return account_registry++ monkeypatch.setattr(+ "openviking.session.memory.account_templates.resolve_account_memory_registry",+ fake_resolver,+ )++ def capture_run(self):+ captured["provider_registry"] = self.context_provider._get_registry()+ return AsyncMock(return_value=(None, []))()++ monkeypatch.setattr("openviking.service.memory_compile.ExtractLoop.run", capture_run)+ original_updater_init = MemoryUpdater.__init__++ def capture_updater(self, *args, **kwargs):+ captured["updater_registry"] = kwargs.get("registry") or (args[0] if args else None)+ original_updater_init(self, *args, **kwargs)++ monkeypatch.setattr("openviking.service.memory_compile.MemoryUpdater.__init__", capture_updater)+ viking_fs = SimpleNamespace(+ glob=AsyncMock(return_value={"matches": []}),+ _async_agfs=SimpleNamespace(pathlock_release=AsyncMock()),+ )+ monkeypatch.setattr(+ "openviking.service.memory_compile.acquire_memory_operation_lease",+ AsyncMock(return_value=None),+ )++ runner = MemoryCompileRunner(SimpleNamespace(_ensure_initialized=lambda: viking_fs))+ await runner._consolidate(+ target=directory,+ memory_type="entities",+ peer_id=None,+ instruction="check account templates",+ ctx=_ctx(),+ )++ fs_arg, account_id, base_registry = captured["resolver_called_with"]+ assert fs_arg is viking_fs+ assert account_id == _ctx().account_id+ assert base_registry is language_config.registry+ assert captured["provider_registry"] is account_registry+ assert (+ captured["provider_registry"].get("soul").content_template+ == "# Company Assistant\n{{ content }}"+ )+++async def test_compile_reports_uri_migration_as_add_and_delete(monkeypatch, language_config):+ language_config.output_language_override = "en"+ language_config.vlm = SimpleNamespace(get_vlm_instance=lambda: SimpleNamespace(model="test"))+ directory = "viking://user/u1/memories/entities"+ source_uri = f"{directory}/person/old.md"+ target_uri = f"{directory}/person/new.md"+ source_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="source",+ extra_fields={"category": "person", "name": "old"},+ )+ viking_fs = SimpleNamespace(+ glob=AsyncMock(return_value={"matches": []}),+ _async_agfs=SimpleNamespace(pathlock_release=AsyncMock()),+ )+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=source_file,+ memory_fields={"category": "person", "name": "new"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )+ monkeypatch.setattr(+ "openviking.service.memory_compile.ExtractLoop.run",+ AsyncMock(return_value=(operations, [])),+ )+ apply_result = MemoryUpdateResult()+ apply_result.add_written(target_uri)+ apply_result.add_deleted(source_uri)+ monkeypatch.setattr(+ "openviking.service.memory_compile.MemoryUpdater.apply_operations",+ AsyncMock(return_value=apply_result),+ )+ monkeypatch.setattr(+ "openviking.service.memory_compile.acquire_memory_operation_lease",+ AsyncMock(return_value=None),+ )+ runner = MemoryCompileRunner(SimpleNamespace(_ensure_initialized=lambda: viking_fs))++ result = await runner._consolidate(+ target=directory,+ memory_type="entities",+ peer_id=None,+ instruction="Rename old to new.",+ ctx=_ctx(),+ )++ assert result["adds"] == [target_uri]+ assert result["updates"] == []+ assert result["deletes"] == [source_uri]+ assert result["total_adds"] == 1+ assert result["total_deletes"] == 1++[email protected]("peer_id", [None, "agent_x"])+async def test_provider_binds_isolation_before_prefetch(peer_id):+ ctx = _ctx()+ viking_fs = SimpleNamespace(glob=AsyncMock(return_value={"matches": [], "count": 0}))+ provider = ConsolidationExtractContextProvider(+ memory_types=["entities"],+ memory_registry=get_default_registry(),+ ctx=ctx,+ viking_fs=viking_fs,+ )+ extract_context = provider.get_extract_context()+ handler = build_consolidation_isolation_handler(+ ctx, extract_context, memory_types=["entities"], peer_id=peer_id+ )+ handler.prepare_messages()+ provider.bind_isolation_handler(handler)++ await provider.prefetch()++ space = f"peers/{peer_id}/" if peer_id else ""+ viking_fs.glob.assert_awaited_once()+ assert viking_fs.glob.await_args.kwargs["uri"] == f"viking://user/u1/{space}memories/entities"+ assert viking_fs.glob.await_args.kwargs["ctx"] is ctx+ assert provider.get_extract_context() is extract_context+ assert provider.create_tool_context().page_id_map is extract_context.page_id_map+++ "change, has_error, has_operations, expected_status",+ [+ (None, True, True, TaskStatus.FAILED),+ ("written", True, True, TaskStatus.COMPLETED),+ ("edited", True, True, TaskStatus.COMPLETED),+ ("deleted", True, True, TaskStatus.COMPLETED),+ ("written", False, True, TaskStatus.COMPLETED),+ (None, False, True, TaskStatus.COMPLETED),+ (None, False, False, TaskStatus.COMPLETED),+ ],+ ids=[+ "all-failed",+ "partial-add",+ "partial-edit",+ "partial-delete",+ "success",+ "no-op",+ "no-operations",+ ],+)+async def test_compile_task_apply_outcomes(+ monkeypatch, language_config, change, has_error, has_operations, expected_status+):+ language_config.output_language_override = "en"+ language_config.vlm = SimpleNamespace(get_vlm_instance=lambda: SimpleNamespace(model="test"))+ ctx = _ctx()+ store = SimpleNamespace(+ create=AsyncMock(),+ update=AsyncMock(),+ get=AsyncMock(return_value=None),+ list=AsyncMock(return_value=[]),+ delete=AsyncMock(),+ )+ tracker = TaskTracker(store)+ task = await tracker.create("compile", account_id=ctx.account_id, user_id=ctx.user.user_id)+ monkeypatch.setattr("openviking.service.memory_compile.get_task_tracker", lambda: tracker)+ monkeypatch.setattr(+ "openviking.service.memory_compile.tracer.get_trace_id", lambda: "test-trace"+ )+ warning = Mock()+ monkeypatch.setattr("openviking.service.memory_compile.logger.warning", warning)+ directory = "viking://user/u1/memories/entities"+ uri = f"{directory}/person/alice.md"+ operations = ResolvedOperations(upsert_operations=[], delete_file_contents=[], errors=[])+ monkeypatch.setattr(+ "openviking.service.memory_compile.ExtractLoop.run",+ AsyncMock(return_value=(operations if has_operations else None, [])),+ )+ lease = {"lease_ref": "test-lease"}+ viking_fs = SimpleNamespace(_async_agfs=SimpleNamespace(pathlock_release=AsyncMock()))+ monkeypatch.setattr(+ "openviking.service.memory_compile.acquire_memory_operation_lease",+ AsyncMock(return_value=lease),+ )+ apply_result = MemoryUpdateResult()+ if change:+ getattr(apply_result, f"add_{change}")(uri)+ if has_error:+ apply_result.add_error(f"{directory}/person/bob.md", RuntimeError("write failed"))+ apply = AsyncMock(return_value=apply_result)+ monkeypatch.setattr("openviking.service.memory_compile.MemoryUpdater.apply_operations", apply)+ runner = MemoryCompileRunner(SimpleNamespace(_ensure_initialized=lambda: viking_fs))++ await runner._run(+ task_id=task.task_id,+ target=directory,+ memory_type="entities",+ peer_id=None,+ instruction=None,+ ctx=ctx,+ )++ final = await tracker.get(task.task_id, account_id=ctx.account_id, user_id=ctx.user.user_id)+ assert final.status == expected_status+ assert final.stage == expected_status.value+ assert final.result == {+ "to": directory,+ "skill": "memory",+ "trace_id": "test-trace",+ "memory_type": "entities",+ "memory_types": ["entities"],+ "adds": [uri] if change == "written" else [],+ "updates": [uri] if change == "edited" else [],+ "deletes": [uri] if change == "deleted" else [],+ "total_adds": int(change == "written"),+ "total_updates": int(change == "edited"),+ "total_deletes": int(change == "deleted"),+ "errors": ["write failed"] if has_error else [],+ }+ if expected_status == TaskStatus.FAILED:+ assert final.error.startswith("MEMORY_CONSOLIDATION_FAILED:")+ else:+ assert final.error is None+ assert not tracker.has_work(task.task_id)+ if has_operations:+ apply.assert_awaited_once()+ viking_fs._async_agfs.pathlock_release.assert_awaited_once_with(lease)+ else:+ apply.assert_not_awaited()+ viking_fs._async_agfs.pathlock_release.assert_not_awaited()+ if change and has_error:+ warning.assert_called_once_with(+ "Memory-mode compile %s partially succeeded: %s", task.task_id, ["write failed"]+ )+ else:+ warning.assert_not_called()++[email protected]("memory_type", [None, "entities"])+async def test_cancel_running_compile_interrupts_before_memory_write(monkeypatch, memory_type):+ store = SimpleNamespace(+ create=AsyncMock(),+ update=AsyncMock(),+ get=AsyncMock(return_value=None),+ list=AsyncMock(return_value=[]),+ delete=AsyncMock(),+ )+ tracker = TaskTracker(store)+ ctx = _ctx()+ task = await tracker.create(+ "compile",+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ task_id="cmp_cancel",+ )+ monkeypatch.setattr("openviking.service.memory_compile.get_task_tracker", lambda: tracker)+ model_started = asyncio.Event()+ memory_write = AsyncMock()+ runner = MemoryCompileRunner(SimpleNamespace())++ async def consolidate(**kwargs):+ del kwargs+ assert get_task_context().task_id == task.task_id+ model_started.set()+ await asyncio.Future()+ await memory_write()++ monkeypatch.setattr(runner, "_consolidate", consolidate)+ worker = asyncio.create_task(+ runner._run(+ task_id=task.task_id,+ target="viking://user/u1/memories" + (f"/{memory_type}" if memory_type else ""),+ memory_type=memory_type,+ peer_id=None,+ instruction=None,+ ctx=ctx,+ )+ )+ await model_started.wait()++ cancelling = await tracker.cancel(+ task.task_id, account_id=ctx.account_id, user_id=ctx.user.user_id+ )+ assert cancelling.status == TaskStatus.CANCELLING+ await asyncio.wait_for(worker, timeout=1)++ final = await tracker.get(task.task_id, account_id=ctx.account_id, user_id=ctx.user.user_id)+ assert final.status == TaskStatus.CANCELLED+ assert not tracker.has_work(task.task_id)+ memory_write.assert_not_awaited()+++def xiaomei_root_result():+ from tests.integration import test_compile_memory_xiaomei as script++ root = "viking://user/xiaomei/memories"+ entity, preference, target = script._memory_root_seed_files(root, "xiaomei", "test1234")+ profile = MemoryFile(+ uri=f"{root}/profile.md",+ memory_type="profile",+ extra_fields={"memory_type": "profile"},+ content="# 小美\n- 姓名:小美 (as of 2023-04-09)\n",+ )+ before = {memory.uri: MemoryFileUtils.write(memory) for memory in (entity, preference, profile)}+ link = {**preference.links[0], "to_uri": target}+ renamed = entity.model_copy(deep=True)+ renamed.uri = target+ renamed.extra_fields["name"] = "pottery_teacher_test1234"+ renamed.backlinks = [link]+ compacted = preference.model_copy(deep=True)+ compacted.links = [link]+ compacted.content = (+ f"- 喜欢{entity.extra_fields['name']}老师的陶艺小班课,每班最多六人,方便老师逐个指导。\n"+ "- [课程资料](https://example.com/docs)\n"+ )+ after = {+ profile.uri: before[profile.uri],+ target: MemoryFileUtils.write(renamed),+ preference.uri: MemoryFileUtils.write(compacted),+ }+ task = {+ "task_id": "root-task",+ "status": "completed",+ "result": {+ "to": root,+ "memory_type": None,+ "memory_types": ["entities", "preferences", "profile"],+ "adds": [target],+ "updates": [preference.uri],+ "deletes": [entity.uri],+ "total_adds": 1,+ "total_updates": 1,+ "total_deletes": 1,+ "errors": [],+ },+ }+ return {+ "root": root,+ "source_uri": entity.uri,+ "target_uri": target,+ "preference_uri": preference.uri,+ "task": task,+ "before": before,+ "after": after,+ }+++def test_xiaomei_memory_root_integrity_checks(xiaomei_root_result):+ from tests.integration import test_compile_memory_xiaomei as script++ script._verify_memory_root_case(**xiaomei_root_result)+ assert script.CASES["memory_root"] is script.CASE_MEMORY_ROOT+++ "damage",+ [+ "scope",+ "flat-file",+ "diff",+ "extra-delete",+ "fact",+ "no-dedup",+ "external-link",+ "backlink",+ "stale-href",+ "unrelated",+ ],+)+def test_xiaomei_memory_root_rejects_false_success(xiaomei_root_result, damage):+ from tests.integration import test_compile_memory_xiaomei as script++ data = xiaomei_root_result+ result, after = data["task"]["result"], data["after"]+ preference_uri, target = data["preference_uri"], data["target_uri"]+ if damage == "scope":+ result["memory_type"] = "entities"+ elif damage == "flat-file":+ result["memory_types"].remove("profile")+ elif damage == "diff":+ result["updates"] = []+ elif damage == "extra-delete":+ del after[f"{data['root']}/profile.md"]+ elif damage == "fact":+ after[target] = after[target].replace("苏州", "上海")+ elif damage == "no-dedup":+ old = MemoryFileUtils.read(data["before"][preference_uri], uri=preference_uri)+ compacted = MemoryFileUtils.read(after[preference_uri], uri=preference_uri)+ compacted.content = old.content+ after[preference_uri] = MemoryFileUtils.write(compacted)+ elif damage == "external-link":+ after[preference_uri] = after[preference_uri].replace(+ "[课程资料](https://example.com/docs)", "课程资料"+ )+ elif damage == "backlink":+ entity = MemoryFileUtils.read(after[target], uri=target)+ entity.backlinks = []+ after[target] = MemoryFileUtils.write(entity)+ elif damage == "stale-href":+ after[preference_uri] += data["source_uri"]+ else:+ after[f"{data['root']}/profile.md"] += "\nchanged"+ with pytest.raises(AssertionError):+ script._verify_memory_root_case(**data)++[email protected]("existing_profile", [True, False])+async def test_xiaomei_root_case_submits_once_without_overwriting_history(+ monkeypatch, xiaomei_root_result, existing_profile+):+ from tests.integration import test_compile_memory_xiaomei as script++ data = xiaomei_root_result+ profile_uri = f"{data['root']}/profile.md"+ files = {profile_uri: data["before"][profile_uri]} if existing_profile else {}+ monkeypatch.setattr(script.uuid, "uuid4", lambda: SimpleNamespace(hex="test1234"))++ async def stat(uri):+ if uri not in files:+ raise NotFoundError(uri)+ return {"isDir": False}++ async def write(uri, content, mode, wait):+ assert mode == "create" and wait+ assert uri not in files+ files[uri] = content++ async def ls(*args, **kwargs):+ return [{"uri": uri, "isDir": False} for uri in files]++ async def read_raw(uri):+ return files[uri]++ async def get_task(task_id):+ assert files == data["before"]+ files.clear()+ files.update(data["after"])+ return data["task"]++ client = SimpleNamespace(+ stat=AsyncMock(side_effect=stat),+ write=AsyncMock(side_effect=write),+ ls=AsyncMock(side_effect=ls),+ read_raw=AsyncMock(side_effect=read_raw),+ compile=AsyncMock(return_value={"task_id": "root-task"}),+ get_task=AsyncMock(side_effect=get_task),+ wait_processed=AsyncMock(),+ )+ await script._run_case(+ client, SimpleNamespace(user="xiaomei", to=None, wait=0), script.CASE_MEMORY_ROOT+ )+ client.compile.assert_awaited_once()+ assert client.compile.await_args.kwargs["to"] == data["root"]+ assert client.compile.await_args.kwargs["skill"] == "memory"+ assert client.write.await_count == (2 if existing_profile else 3)+ assert files[profile_uri] == data["before"][profile_uri]+++ "task",+ [+ None,+ {},+ {"status": "failed"},+ {"status": "cancelled"},+ {"status": "completed", "error": "failed"},+ {"status": "completed", "result": {"errors": ["write failed"]}},+ {"status": "completed"},+ ],+)+async def test_xiaomei_compile_rejects_failed_or_missing_result(task):+ from tests.integration import test_compile_memory_xiaomei as script++ client = SimpleNamespace(+ compile=AsyncMock(return_value={"task_id": "task"}), get_task=AsyncMock(return_value=task)+ )+ with pytest.raises(AssertionError):+ await script._run_memory_compile(client, "viking://user/xiaomei/memories/entities", "整理")+++async def test_xiaomei_compile_missing_id_and_noop():+ from tests.integration import test_compile_memory_xiaomei as script++ client = SimpleNamespace(compile=AsyncMock(return_value={}), get_task=AsyncMock())+ with pytest.raises(AssertionError, match="task_id"):+ await script._run_memory_compile(client, "viking://user/xiaomei/memories/entities", "整理")+ client.get_task.assert_not_awaited()+ task = {+ "status": "completed",+ "result": {"errors": [], "adds": [], "updates": [], "deletes": []},+ }+ client.compile.return_value = {"task_id": "task"}+ client.get_task.return_value = task+ assert (+ await script._run_memory_compile(client, "viking://user/xiaomei/memories/entities", "整理")+ == task+ )+++def test_xiaomei_case_directory_rejects_wrong_scope():+ from tests.integration import test_compile_memory_xiaomei as script++ args = SimpleNamespace(user="xiaomei", to="viking://user/xiaomei/memories")+ assert script._case_directory(args, script.CASE_MEMORY_ROOT) == args.to+ with pytest.raises(ValueError, match="entities"):+ script._case_directory(args, script.CASE_RENAME)+ args.to = "viking://user/xiaomei/memories/entities"+ with pytest.raises(ValueError, match="根目录"):+ script._case_directory(args, script.CASE_MEMORY_ROOT)+++async def test_xiaomei_default_all_runs_root_after_single_type_cases(monkeypatch):+ from tests.integration import test_compile_memory_xiaomei as script++ client = SimpleNamespace(initialize=AsyncMock(), close=AsyncMock())+ monkeypatch.setattr(script.ov, "AsyncHTTPClient", lambda **kwargs: client)+ monkeypatch.setattr(script, "resolve_api_key", lambda key: key)+ run_case = AsyncMock()+ monkeypatch.setattr(script, "_run_case", run_case)+ args = SimpleNamespace(+ url="http://localhost:1933",+ api_key=None,+ account="default",+ user="xiaomei",+ to=None,+ case="all",+ ingest=False,+ )+ await script._main_async(args)+ assert [call.args[2].key for call in run_case.await_args_list] == [+ "merge",+ "split",+ "dedup",+ "rename",+ "preferences",+ "memory_root",+ ]+ client.close.assert_awaited_once()+++async def test_xiaomei_rename_uses_known_chinese_seed_with_english_output(+ monkeypatch, language_config+):+ from openviking.session.memory.utils.link_renderer import LinkRenderer+ from tests.integration import test_compile_memory_xiaomei as script++ language_config.output_language_override = "en"+ directory = "viking://user/xiaomei/memories/entities"+ chinese_name, english_name = "沈雨诗宁", "photo_teacher_test1234"+ chinese_uri = f"{directory}/人物/{chinese_name}.md"+ english_uri = f"{directory}/person/{english_name}.md"+ old_uri = f"{directory}/person/shen yushining.md"+ old_raw = "# Shen Yushining\nHistorical teacher memory must stay unchanged."+ files = {old_uri: old_raw}+ steps = [+ (f"{directory}/person/{chinese_name}.md", english_uri),+ (english_uri, chinese_uri),+ (chinese_uri, english_uri),+ ]+ completed = []+ monkeypatch.setattr(script, "_unique_rename_names", lambda case: (chinese_name, english_name))++ async def write(uri, raw, *, mode, wait):+ assert mode == "create" and wait+ assert uri not in files+ files[uri] = raw++ async def ls(uri, **kwargs):+ return [+ {"uri": path, "isDir": False, "size": len(raw)}+ for path, raw in files.items()+ if path.startswith(uri + "/")+ ]++ async def read(uri):+ return files[uri]++ async def stat(uri):+ if uri not in files and not any(path.startswith(uri + "/") for path in files):+ raise NotFoundError(uri)+ return {"isDir": uri not in files}++ async def compile(**kwargs):+ assert kwargs["to"] == directory and kwargs["skill"] == "memory"+ return {"task_id": str(len(completed))}++ async def get_task(task_id):+ source, target = steps[int(task_id)]+ entity = MemoryFileUtils.read(files.pop(source), uri=source)+ assert entity.memory_type == "entities"+ entity.uri = target+ entity.extra_fields.update(category=target.split("/")[-2], name=target.split("/")[-1][:-3])+ entity.content = (+ "# Shen Yushining\nXiaomei's photography teacher.\n\n## Background\n"+ "- Lives in Hangzhou.\n- Specializes in landscape photography.\n\n## Lesson\n"+ "- Takes Xiaomei to West Lake in October to practice long exposure."+ )+ event_uri = entity.backlinks[0]["from_uri"]+ event = MemoryFileUtils.read(files[event_uri], uri=event_uri)+ event.content = LinkRenderer.strip_managed_links(event.content, event_uri, event.links)+ event.links = [{**link, "to_uri": target} for link in event.links]+ entity.backlinks = [dict(link) for link in event.links]+ files[target] = MemoryFileUtils.write(entity)+ files[event_uri] = MemoryFileUtils.write(event)+ completed.append((source, target))+ return {+ "task_id": task_id,+ "status": "completed",+ "result": {+ "adds": [target],+ "updates": [event_uri],+ "deletes": [source],+ "total_adds": 1,+ "total_updates": 1,+ "total_deletes": 1,+ "errors": [],+ },+ }++ client = SimpleNamespace(+ write=AsyncMock(side_effect=write),+ ls=AsyncMock(side_effect=ls),+ read=AsyncMock(side_effect=read),+ read_raw=AsyncMock(side_effect=read),+ stat=AsyncMock(side_effect=stat),+ wait_processed=AsyncMock(),+ compile=AsyncMock(side_effect=compile),+ get_task=AsyncMock(side_effect=get_task),+ create_session=AsyncMock(side_effect=AssertionError("rename must not use extraction")),+ )+ await script._run_case(+ client, SimpleNamespace(user="xiaomei", to=None, wait=0), script.CASE_RENAME+ )+ assert completed == steps+ assert client.write.await_count == 2+ assert chinese_name in client.write.await_args_list[0].args[1]+ assert client.compile.await_count == 3+ client.create_session.assert_not_awaited()+ assert files[old_uri] == old_raw+ assert chinese_uri not in files+ assert english_uri in files+ assert language_config.output_language_override == "en"+++async def test_xiaomei_rename_seed_collision_preserves_existing_file():+ from openviking_cli.exceptions import ConflictError+ from tests.integration import test_compile_memory_xiaomei as script++ source = "viking://user/xiaomei/memories/entities/person/沈雨诗宁.md"+ files = {source: "existing"}++ async def write(uri, raw, *, mode, wait):+ assert mode == "create"+ if uri in files:+ raise ConflictError("already exists", resource=uri)+ files[uri] = raw++ client = SimpleNamespace(write=AsyncMock(side_effect=write), wait_processed=AsyncMock())+ with pytest.raises(ConflictError):+ await script._seed_rename_case(+ client, source.rpartition("/person/")[0], "沈雨诗宁", "photo_teacher_test1234"+ )+ assert files == {source: "existing"}+ assert client.write.await_count == 1+ client.wait_processed.assert_not_awaited()+++async def test_xiaomei_snapshot_does_not_hide_errors_or_truncation():+ from tests.integration import test_compile_memory_xiaomei as script++ client = SimpleNamespace(ls=AsyncMock(side_effect=RuntimeError("unavailable")))+ with pytest.raises(RuntimeError, match="unavailable"):+ await script._snapshot_memory_dir(client, "viking://user/xiaomei/memories")+ client.ls.side_effect = None+ client.ls.return_value = [{}] * 1000+ with pytest.raises(AssertionError, match="截断"):+ await script._snapshot_memory_dir(client, "viking://user/xiaomei/memories")diff --git a/tests/session/memory/test_entities_template.py b/tests/session/memory/test_entities_template.pyindex b098d3190..ced732647 100644--- a/tests/session/memory/test_entities_template.py+++ b/tests/session/memory/test_entities_template.py@@ -23,3 +23,6 @@ def test_entity_filename_is_case_insensitive(schema_path):upper_path = template.render(category="CONCEPT", name="SMART")assert lower_path == upper_path == "concept/smart.md"+ fields = {field["name"]: field for field in schema["fields"]}+ assert fields["category"]["merge_op"] == "replace"+ assert fields["name"]["merge_op"] == "replace"diff --git a/tests/session/memory/test_extraction_output_protocol.py b/tests/session/memory/test_extraction_output_protocol.pyindex 03a6776ae..81b3c5470 100644--- a/tests/session/memory/test_extraction_output_protocol.py+++ b/tests/session/memory/test_extraction_output_protocol.py@@ -77,6 +77,32 @@ def _project_schema() -> MemoryTypeSchema:)+def _renameable_entity_schema() -> MemoryTypeSchema:+ return MemoryTypeSchema(+ memory_type="entities",+ description="Entities",+ directory="viking://user/{{ user_space }}/memories/entities",+ filename_template="{{ category }}/{{ name }}.md",+ fields=[+ MemoryField(+ name="category",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="name",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="content",+ field_type=FieldType.STRING,+ merge_op=MergeOp.PATCH,+ ),+ ],+ )++def _profile_schema() -> MemoryTypeSchema:return MemoryTypeSchema(memory_type="profile",@@ -178,6 +204,28 @@ def test_python_contract_and_bindings_expose_only_selected_schema_fields():assert protocol.render_new_bindings(context, source="duplicate read") == ""+def test_python_update_preserves_unchanged_mutable_identity_fields():+ uri = "viking://user/alice/memories/entities/person/阿珍.md"+ file = MemoryFile(+ uri=uri,+ memory_type="entities",+ content="大学室友",+ extra_fields={"category": "person", "name": "阿珍"},+ )+ context = _context([_renameable_entity_schema()], files=[file])+ protocol = create_extraction_output_protocol("python")+ _bind(protocol, context)++ operations, error = protocol.parse(+ "entities_1.update(name='陈静娴')\nsdk.commit()",+ context,+ )++ assert error is None+ assert operations.entities[0].category == "person"+ assert operations.entities[0].name == "陈静娴"++def test_python_contract_includes_link_rules_when_enabled():context = _context([_preference_schema()], link_enabled=True)protocol = create_extraction_output_protocol("python")@@ -541,9 +589,7 @@ def test_python_reserved_existing_retry_explains_new_replacement_binding():def test_python_string_literal_retry_pushes_triple_quotes():protocol = create_extraction_output_protocol("python")- retry = protocol.render_format_retry(- "Line 33: invalid syntax. Perhaps you forgot a comma?"- )+ retry = protocol.render_format_retry("Line 33: invalid syntax. Perhaps you forgot a comma?")assert "offending line is shown above" in retryassert 'triple-quoted string ("""...""")' in retry@@ -727,7 +773,7 @@ def test_python_syntax_error_includes_offending_source_line():assert error is not Noneassert "invalid Python syntax" in error- assert 'Little Women' in error+ assert "Little Women" in errorassert "^" in error@@ -1122,7 +1168,7 @@ sdk.commit()assert blocks == [{"search": "Prefers Neovim", "replace": "Prefers Emacs"}]-def test_python_field_edit_rejects_literal_field_placeholder():+def test_python_unknown_field_is_silently_ignored():uri = "viking://user/alice/memories/preferences/editor.md"context = _context([_preference_schema()],@@ -1136,9 +1182,39 @@ def test_python_field_edit_rejects_literal_field_placeholder():context,)- assert operations is None- assert "memory field 'field' is unavailable" in error- assert "not the literal word 'field'" in error+ # Unknown field access is a no-op: the statement compiles without+ # touching server state and the whole program still commits.+ assert error is None+ assert operations is not None+ assert operations.preferences == []+++def test_python_unknown_field_does_not_block_sibling_updates():+ uri = "viking://user/alice/memories/preferences/editor.md"+ context = _context(+ [_preference_schema()],+ files=[_existing_preference(uri, "editor", "Use Vim", 0)],+ )+ protocol = create_extraction_output_protocol("python")+ _bind(protocol, context)++ operations, error = protocol.parse(+ """+preferences_1.bogus.update("ignored")+preferences_1.bogus.edit(search="Use Vim", replace="Use Neovim")+preferences_1.bogus.drop(text="Tabs")+preferences_1.content.edit(search="Use Vim", replace="Use Neovim")+sdk.commit()+""",+ context,+ )++ assert error is None+ item = operations.model_dump()["preferences"][0]+ # The bogus field is silently dropped; the real content edit still applies.+ assert item["content"]["blocks"] == [+ {"search": "Use Vim", "replace": "Use Neovim"},+ ]def test_python_field_edit_emits_block_regardless_of_uniqueness():@@ -1683,7 +1759,7 @@ def test_python_rejects_fstring_width_format_spec():# A width format spec turns a small integer literal into a huge padded string# with no repeat operator; format specs are disallowed.operations, error = protocol.parse(- 'sdk.set_profile(content=f"{\'x\':>1000001}")\nsdk.commit()',+ "sdk.set_profile(content=f\"{'x':>1000001}\")\nsdk.commit()",context,)diff --git a/tests/session/memory/test_memory_react.py b/tests/session/memory/test_memory_react.pyindex 4b40addc3..4b52b444f 100644--- a/tests/session/memory/test_memory_react.py+++ b/tests/session/memory/test_memory_react.py@@ -11,6 +11,7 @@ import pytestfrom pydantic import BaseModel, Fieldfrom openviking.session.memory.dataclass import (+ MemoryField,MemoryFile,MemoryTypeSchema,ResolvedOperation,@@ -20,7 +21,13 @@ from openviking.session.memory.extract_loop import (ExtractLoop,)from openviking.session.memory.memory_isolation_handler import MemoryIsolationHandler-from openviking.session.memory.merge_op import SearchReplaceBlock, StrPatch+from openviking.session.memory.memory_updater import MemoryUpdater+from openviking.session.memory.merge_op import (+ FieldType,+ MergeOp,+ SearchReplaceBlock,+ StrPatch,+)from openviking.session.memory.page_id_map import PageIdMapfrom openviking.session.memory.schema_model_generator import SchemaModelGenerator@@ -218,6 +225,321 @@ class TestExtractLoopFinalJsonRetry:"user_id": "user_a",}+ @pytest.mark.asyncio+ async def test_existing_page_id_recomputes_uri_for_mutable_identity_fields(self):+ class EntityItem(BaseModel):+ page_id: int+ category: str+ name: str++ class Operations(BaseModel):+ entities: list[EntityItem]+ delete_ids: list = Field(default_factory=list)++ source_uri = "viking://user/user_a/memories/entities/person/阿珍.md"+ target_uri = "viking://user/user_a/memories/entities/person/陈静娴.md"+ schema = MemoryTypeSchema(+ memory_type="entities",+ directory="viking://user/{{ user_space }}/memories/entities",+ filename_template="{{ category|lower }}/{{ name|lower }}.md",+ fields=[+ MemoryField(+ name="category",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="name",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ ],+ )+ old_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="大学室友",+ extra_fields={"category": "person", "name": "阿珍"},+ )+ context_provider = MagicMock()+ context_provider.get_memory_schemas.return_value = [schema]+ context_provider.read_file_contents = {source_uri: old_file}+ ctx = MagicMock()+ ctx.user.user_id = "user_a"+ extract_context = MagicMock()+ extract_context.messages = []+ extract_context.page_id_map = PageIdMap()+ page_id = extract_context.page_id_map.get_page_id(source_uri)+ loop = object.__new__(ExtractLoop)+ loop.ctx = ctx+ loop.context_provider = context_provider+ loop._extract_context = extract_context+ loop._isolation_handler = MemoryIsolationHandler(ctx, extract_context)++ resolved, _ = await loop.resolve_operations(+ Operations(entities=[EntityItem(page_id=page_id, category="person", name="陈静娴")])+ )++ operation = resolved.upsert_operations[0]+ assert operation.uris == [target_uri]+ assert operation.old_memory_file_content is old_file++ @pytest.mark.asyncio+ async def test_existing_update_preserves_omitted_mutable_identity_fields(self):+ class EntityItem(BaseModel):+ page_id: int+ category: str | None = None+ name: str | None = None+ content: str | None = None++ class Operations(BaseModel):+ entities: list[EntityItem]+ delete_ids: list = Field(default_factory=list)++ source_uri = "viking://user/user_a/memories/entities/person/阿珍.md"+ schema = MemoryTypeSchema(+ memory_type="entities",+ directory="viking://user/{{ user_space }}/memories/entities",+ filename_template="{{ category|lower }}/{{ name|lower }}.md",+ fields=[+ MemoryField(+ name="category",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="name",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="content",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ ],+ )+ old_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="大学室友",+ extra_fields={"category": "person", "name": "阿珍"},+ )+ context_provider = MagicMock()+ context_provider.get_memory_schemas.return_value = [schema]+ context_provider.read_file_contents = {source_uri: old_file}+ ctx = MagicMock()+ ctx.user.user_id = "user_a"+ extract_context = MagicMock()+ extract_context.messages = []+ extract_context.page_id_map = PageIdMap()+ page_id = extract_context.page_id_map.get_page_id(source_uri)+ loop = object.__new__(ExtractLoop)+ loop.ctx = ctx+ loop.context_provider = context_provider+ loop._extract_context = extract_context+ loop._isolation_handler = MemoryIsolationHandler(ctx, extract_context)++ resolved, _ = await loop.resolve_operations(+ Operations(entities=[EntityItem(page_id=page_id, content="精简后的正文")])+ )++ operation = resolved.upsert_operations[0]+ assert operation.uris == [source_uri]+ assert operation.memory_fields["category"] == "person"+ assert operation.memory_fields["name"] == "阿珍"++ @pytest.mark.asyncio+ async def test_existing_update_keeps_legacy_case_uri_when_only_content_changes(self):+ class PreferenceItem(BaseModel):+ page_id: int+ user: str | None = None+ topic: str | None = None+ content: str | None = None++ class Operations(BaseModel):+ preferences: list[PreferenceItem]+ delete_ids: list = Field(default_factory=list)++ source_uri = "viking://user/user_a/memories/preferences/Alice/food.md"+ schema = MemoryTypeSchema(+ memory_type="preferences",+ directory="viking://user/{{ user_space }}/memories/preferences",+ filename_template="{{ user|lower }}/{{ topic|lower }}.md",+ fields=[+ MemoryField(+ name="user",+ field_type=FieldType.STRING,+ merge_op=MergeOp.IMMUTABLE,+ ),+ MemoryField(+ name="topic",+ field_type=FieldType.STRING,+ merge_op=MergeOp.IMMUTABLE,+ ),+ MemoryField(+ name="content",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ ],+ )+ old_file = MemoryFile(+ uri=source_uri,+ memory_type="preferences",+ content="- 喜欢辣的",+ extra_fields={"user": "Alice", "topic": "food"},+ )+ context_provider = MagicMock()+ context_provider.get_memory_schemas.return_value = [schema]+ context_provider.read_file_contents = {source_uri: old_file}+ ctx = MagicMock()+ ctx.user.user_id = "user_a"+ extract_context = MagicMock()+ extract_context.messages = []+ extract_context.page_id_map = PageIdMap()+ page_id = extract_context.page_id_map.get_page_id(source_uri)+ loop = object.__new__(ExtractLoop)+ loop.ctx = ctx+ loop.context_provider = context_provider+ loop._extract_context = extract_context+ loop._isolation_handler = MemoryIsolationHandler(ctx, extract_context)++ resolved, _ = await loop.resolve_operations(+ Operations(preferences=[PreferenceItem(page_id=page_id, content="- 微辣")])+ )++ operation = resolved.upsert_operations[0]+ assert operation.uris == [source_uri]+ assert operation.old_memory_file_content is old_file+ assert not MemoryUpdater._is_uri_migration(operation)++ @pytest.mark.asyncio+ async def test_rename_conflict_refetches_target_then_requires_explicit_merge(self):+ source_uri = "viking://user/user_a/memories/entities/person/阿珍.md"+ target_uri = "viking://user/user_a/memories/entities/person/陈静娴.md"+ source_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="大学室友",+ extra_fields={"category": "person", "name": "阿珍"},+ )+ target_file = MemoryFile(+ uri=target_uri,+ memory_type="entities",+ content="上海 UI 设计师",+ extra_fields={"category": "person", "name": "陈静娴"},+ )+ schema = MemoryTypeSchema(+ memory_type="entities",+ directory="viking://user/{{ user_space }}/memories/entities",+ filename_template="{{ category|lower }}/{{ name|lower }}.md",+ fields=[+ MemoryField(+ name="category",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="name",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="content",+ field_type=FieldType.STRING,+ merge_op=MergeOp.PATCH,+ ),+ ],+ )+ extract_context = SimpleNamespace(messages=[], page_id_map=PageIdMap())++ class FakeContextProvider:+ def __init__(self):+ self.read_file_contents = {source_uri: source_file}+ self.read_uris = []++ def get_memory_schemas(self, ctx):+ del ctx+ return [schema]++ def get_tools(self):+ return []++ def get_extract_context(self):+ return extract_context++ def get_output_language(self):+ return "zh-CN"++ def instruction(self):+ return "Merge aliases without losing facts."++ async def prefetch(self):+ return []++ async def execute_tool(self, tool_call):+ uri = tool_call.arguments["uri"]+ self.read_uris.append(uri)+ assert uri == target_uri+ self.read_file_contents[uri] = target_file+ return {+ **target_file.to_metadata(),+ "page_id": extract_context.page_id_map.get_page_id(uri),+ }++ class FakeVLM:+ model = "test-model"++ def __init__(self):+ self.responses = iter(+ [+ "entities_1.update(name='陈静娴')\nsdk.commit()",+ (+ "entities_2.content.update('大学室友;上海 UI 设计师')\n"+ "entities_1.delete(replacement=entities_2)\n"+ "sdk.commit()"+ ),+ ]+ )++ async def get_completion_async(self, **kwargs):+ del kwargs+ return next(self.responses)++ provider = FakeContextProvider()+ ctx = MagicMock()+ ctx.user.user_id = "user_a"+ config = SimpleNamespace(+ memory=SimpleNamespace(link_enabled=False, extraction_output_format="python"),+ vlm=SimpleNamespace(max_tokens=None),+ )+ loop = ExtractLoop(+ vlm=FakeVLM(),+ viking_fs=MagicMock(),+ ctx=ctx,+ context_provider=provider,+ isolation_handler=MemoryIsolationHandler(ctx, extract_context),+ max_iterations=2,+ )++ with (+ patch(+ "openviking.session.memory.extract_loop.get_openviking_config",+ return_value=config,+ ),+ patch(+ "openviking_cli.utils.config.get_openviking_config",+ return_value=config,+ ),+ ):+ operations, _ = await loop.run()++ assert provider.read_uris == [target_uri]+ assert [operation.uris for operation in operations.upsert_operations] == [[target_uri]]+ assert [file.uri for file in operations.delete_file_contents] == [source_uri]+ assert operations.delete_replacements == {source_uri: target_uri}+@pytest.mark.asyncioasync def test_invalid_peer_hint_preserves_legacy_self_write_fallback(self):class PreferenceItem(BaseModel):diff --git a/tests/session/memory/test_memory_updater.py b/tests/session/memory/test_memory_updater.pyindex 0d6b49f33..2138ff121 100644--- a/tests/session/memory/test_memory_updater.py+++ b/tests/session/memory/test_memory_updater.py@@ -41,7 +41,7 @@ from openviking.session.memory.utils import (parse_memory_file_with_fields,)from openviking.storage.abstract_overview import parse_abstract_overview-from openviking_cli.exceptions import NotFoundError+from openviking_cli.exceptions import ConflictError, NotFoundErrorfrom openviking_cli.session.user_id import UserIdentifier@@ -1019,6 +1019,712 @@ class TestMemoryUpdater:(replacement_uri, profile_uri)}+ @pytest.mark.asyncio+ async def test_apply_operations_migrates_uri_and_preserves_unmodified_fields(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ profile_uri = "viking://user/u/memories/profile.md"+ schema = MemoryTypeSchema(+ memory_type="entities",+ directory="viking://user/{{ user_space }}/memories/entities",+ filename_template="{{ category }}/{{ name }}.md",+ fields=[+ MemoryField(+ name="category",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="name",+ field_type=FieldType.STRING,+ merge_op=MergeOp.REPLACE,+ ),+ MemoryField(+ name="content",+ field_type=FieldType.STRING,+ merge_op=MergeOp.PATCH,+ ),+ ],+ )+ registry = MemoryTypeRegistry(load_schemas=False)+ registry.register(schema)+ old_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="大学室友,仗义借钱。",+ extra_fields={+ "category": "person",+ "name": "阿珍",+ "custom_metadata": "keep",+ "version": 3,+ },+ links=[+ {+ "from_uri": source_uri,+ "to_uri": profile_uri,+ "link_type": "related_to",+ "weight": 0.8,+ "match_text": "室友",+ "description": "关系",+ }+ ],+ )+ profile_file = MemoryFile(+ uri=profile_uri,+ memory_type="profile",+ content="小美",+ backlinks=list(old_file.links),+ )+ store = {+ source_uri: MemoryFileUtils.write(old_file),+ profile_uri: MemoryFileUtils.write(profile_file),+ }+ mock_viking_fs = MagicMock()++ async def read_file(uri, **kwargs):+ if uri not in store:+ raise NotFoundError(uri, "file")+ return store[uri]++ async def write_file(uri, content, **kwargs):+ store[uri] = content++ async def rm(uri, **kwargs):+ store.pop(uri, None)++ mock_viking_fs.read_file = AsyncMock(side_effect=read_file)+ mock_viking_fs.write_file = AsyncMock(side_effect=write_file)+ mock_viking_fs.rm = AsyncMock(side_effect=rm)+ updater = MemoryUpdater(registry=registry)+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_fields={"category": "person", "name": "陈静娴"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ ctx = MagicMock()+ result = await updater.apply_operations(operations, ctx)++ assert result.written_uris == [target_uri]+ assert result.deleted_uris == [source_uri]+ assert source_uri not in store+ migrated = MemoryFileUtils.read(store[target_uri], uri=target_uri)+ assert migrated.plain_content() == "大学室友,仗义借钱。"+ assert migrated.extra_fields["category"] == "person"+ assert migrated.extra_fields["name"] == "陈静娴"+ assert migrated.extra_fields["custom_metadata"] == "keep"+ assert migrated.extra_fields["version"] == 4+ assert migrated.links[0]["from_uri"] == target_uri+ profile = MemoryFileUtils.read(store[profile_uri], uri=profile_uri)+ assert profile.backlinks[0]["from_uri"] == target_uri+ assert operations.delete_replacements == {source_uri: target_uri}++ @pytest.mark.asyncio+ async def test_apply_operations_rename_reads_latest_source_under_lock(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ stale_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="stale fact",+ extra_fields={"category": "person", "name": "阿珍", "version": 1},+ )+ current_file = stale_file.model_copy(+ update={+ "content": "stale fact; concurrent fact",+ "extra_fields": {+ "category": "person",+ "name": "阿珍",+ "version": 2,+ },+ }+ )+ store = {source_uri: MemoryFileUtils.write(current_file)}+ mock_viking_fs = MagicMock()++ async def read_file(uri, **kwargs):+ if uri not in store:+ raise NotFoundError(uri, "file")+ return store[uri]++ async def write_file(uri, content, **kwargs):+ store[uri] = content++ async def rm(uri, **kwargs):+ store.pop(uri, None)++ mock_viking_fs.read_file = AsyncMock(side_effect=read_file)+ mock_viking_fs.write_file = AsyncMock(side_effect=write_file)+ mock_viking_fs.rm = AsyncMock(side_effect=rm)+ schema = MemoryTypeSchema(+ memory_type="entities",+ fields=[+ MemoryField(name="name", field_type=FieldType.STRING, merge_op=MergeOp.REPLACE),+ MemoryField(name="content", field_type=FieldType.STRING, merge_op=MergeOp.PATCH),+ ],+ )+ registry = MemoryTypeRegistry(load_schemas=False)+ registry.register(schema)+ updater = MemoryUpdater(registry=registry)+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=stale_file,+ memory_fields={"name": "陈静娴"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert result.errors == []+ migrated = MemoryFileUtils.read(store[target_uri], uri=target_uri)+ assert migrated.plain_content() == "stale fact; concurrent fact"+ assert migrated.extra_fields["version"] == 3++ @pytest.mark.asyncio+ @pytest.mark.parametrize("target_content", ["occupied", ""])+ async def test_apply_operations_rejects_occupied_rename_target_before_writes(+ self, target_content+ ):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ old_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ mock_viking_fs = MagicMock()+ mock_viking_fs.read_file = AsyncMock(return_value=target_content)+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_upsert = AsyncMock()+ updater._apply_delete = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_fields={"name": "陈静娴"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert len(result.errors) == 1+ assert isinstance(result.errors[0][1], ConflictError)+ updater._apply_upsert.assert_not_awaited()+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_rejects_case_only_uri_rename(self):+ source_uri = "viking://user/u/memories/entities/person/Alice.md"+ target_uri = "viking://user/u/memories/entities/person/alice.md"+ old_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ mock_viking_fs = MagicMock()+ mock_viking_fs.read_file = AsyncMock()+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_upsert = AsyncMock()+ updater._apply_delete = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_fields={"name": "alice"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert len(result.errors) == 1+ assert isinstance(result.errors[0][1], ConflictError)+ assert "Case-only" in str(result.errors[0][1])+ mock_viking_fs.read_file.assert_not_awaited()+ updater._apply_upsert.assert_not_awaited()+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_keeps_source_when_rename_write_fails(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ old_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ mock_viking_fs = MagicMock()+ mock_viking_fs.read_file = AsyncMock(side_effect=NotFoundError(target_uri, "file"))+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_upsert = AsyncMock(side_effect=OSError("write failed"))+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_fields={"name": "陈静娴"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert any(uri == target_uri for uri, _error in result.errors)+ assert result.deleted_uris == []+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_keeps_source_when_locked_target_read_fails(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ old_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ mock_viking_fs = MagicMock()+ mock_viking_fs.read_file = AsyncMock(+ side_effect=[NotFoundError(target_uri, "file"), OSError("read failed")]+ )+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_fields={"name": "陈静娴"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert any(uri == target_uri for uri, _error in result.errors)+ assert result.deleted_uris == []+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_explicit_merge_to_existing_target_is_allowed(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ event_uri = "viking://user/u/memories/events/2023/04/09/loan.md"+ event_link = {+ "from_uri": event_uri,+ "to_uri": source_uri,+ "link_type": "related_to",+ "weight": 0.8,+ "match_text": "阿珍",+ "description": "source link",+ }+ source_file = MemoryFile(+ uri=source_uri,+ memory_type="entities",+ content="source",+ backlinks=[event_link],+ )+ event_file = MemoryFile(+ uri=event_uri,+ memory_type="events",+ content=(+ "[阿珍](../../../../entities/person/阿珍.md) helped. "+ "[docs](https://example.com/docs) and [notes](./notes.md)."+ ),+ links=[event_link],+ )+ target_file = MemoryFile(uri=target_uri, memory_type="entities", content="merged")+ store = {+ source_uri: MemoryFileUtils.write(source_file),+ target_uri: MemoryFileUtils.write(target_file),+ event_uri: MemoryFileUtils.write(event_file, render_links=False),+ }+ mock_viking_fs = MagicMock()++ async def read_file(uri, **kwargs):+ return store[uri]++ async def write_file(uri, content, **kwargs):+ store[uri] = content++ mock_viking_fs.read_file = AsyncMock(side_effect=read_file)+ mock_viking_fs.write_file = AsyncMock(side_effect=write_file)+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_upsert = AsyncMock()+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=target_file,+ memory_fields={"content": "merged"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[source_file],+ delete_replacements={source_uri: target_uri},+ errors=[],+ )++ ctx = MagicMock()+ result = await updater.apply_operations(operations, ctx)++ assert result.edited_uris == [target_uri, event_uri]+ assert result.deleted_uris == [source_uri]+ target = MemoryFileUtils.read(store[target_uri], uri=target_uri)+ assert target.backlinks[0]["from_uri"] == event_uri+ assert target.backlinks[0]["to_uri"] == target_uri+ event = MemoryFileUtils.read(store[event_uri], uri=event_uri)+ assert event.links[0]["from_uri"] == event_uri+ assert event.links[0]["to_uri"] == target_uri+ assert "../../../../entities/person/阿珍.md" not in event.content+ assert "[阿珍](../../../../entities/person/陈静娴.md)" in event.content+ assert "[docs](https://example.com/docs)" in event.content+ assert "[notes](./notes.md)" in event.content+ updater._apply_upsert.assert_awaited_once()+ updater._apply_delete.assert_awaited_once_with(source_uri, ctx, lease_ref=None)++ @pytest.mark.asyncio+ async def test_apply_operations_explicit_merge_inherits_source_forward_links(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ profile_uri = "viking://user/u/memories/profile.md"+ source_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ source_file.links = [+ {+ "from_uri": source_uri,+ "to_uri": profile_uri,+ "link_type": "related_to",+ "weight": 0.8,+ "match_text": "source",+ "description": "source link",+ }+ ]+ target_file = MemoryFile(uri=target_uri, memory_type="entities", content="merged")+ profile_file = MemoryFile(+ uri=profile_uri,+ memory_type="profile",+ content="profile",+ backlinks=list(source_file.links),+ )+ store = {+ source_uri: MemoryFileUtils.write(source_file),+ target_uri: MemoryFileUtils.write(target_file),+ profile_uri: MemoryFileUtils.write(profile_file),+ }+ mock_viking_fs = MagicMock()++ async def read_file(uri, **kwargs):+ return store[uri]++ async def write_file(uri, content, **kwargs):+ store[uri] = content++ mock_viking_fs.read_file = AsyncMock(side_effect=read_file)+ mock_viking_fs.write_file = AsyncMock(side_effect=write_file)+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_upsert = AsyncMock()+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=target_file,+ memory_fields={"content": "merged"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[source_file],+ delete_replacements={source_uri: target_uri},+ errors=[],+ )++ ctx = MagicMock()+ result = await updater.apply_operations(operations, ctx)++ assert result.edited_uris == [target_uri, profile_uri]+ assert result.deleted_uris == [source_uri]+ target = MemoryFileUtils.read(store[target_uri], uri=target_uri)+ assert target.links[0]["from_uri"] == target_uri+ assert target.links[0]["to_uri"] == profile_uri+ profile = MemoryFileUtils.read(store[profile_uri], uri=profile_uri)+ assert profile.backlinks[0]["from_uri"] == target_uri+ updater._apply_upsert.assert_awaited_once()+ updater._apply_delete.assert_awaited_once_with(source_uri, ctx, lease_ref=None)++ @pytest.mark.asyncio+ async def test_apply_operations_rename_plus_merge_folds_duplicate_links(self):+ # Same batch renames `old.md` -> `new.md` (implicit rename via URI+ # field change) and merges a duplicate entity `duplicate.md` into the+ # renamed target through an explicit replacement. Only `duplicate.md`+ # holds a link to `profile.md`; the renamed target must inherit it and+ # `profile.md`'s backlink must point at the renamed target.+ old_uri = "viking://user/u/memories/entities/person/old.md"+ new_uri = "viking://user/u/memories/entities/person/new.md"+ duplicate_uri = "viking://user/u/memories/entities/person/duplicate.md"+ profile_uri = "viking://user/u/memories/profile.md"+ duplicate_link = {+ "from_uri": duplicate_uri,+ "to_uri": profile_uri,+ "link_type": "related_to",+ "weight": 0.8,+ "match_text": "duplicate",+ "description": "duplicate-only link",+ }+ old_file = MemoryFile(+ uri=old_uri,+ memory_type="entities",+ content="primary",+ extra_fields={"category": "person", "name": "old"},+ )+ duplicate_file = MemoryFile(+ uri=duplicate_uri,+ memory_type="entities",+ content="alt",+ extra_fields={"category": "person", "name": "duplicate"},+ links=[duplicate_link],+ )+ profile_file = MemoryFile(+ uri=profile_uri,+ memory_type="profile",+ content="profile",+ backlinks=[duplicate_link],+ )+ # `new.md` should not already exist on disk; the renamed target is+ # created by the upsert. Serialize the source files so read_file can+ # produce them lazily.+ store = {+ old_uri: MemoryFileUtils.write(old_file),+ duplicate_uri: MemoryFileUtils.write(duplicate_file),+ profile_uri: MemoryFileUtils.write(profile_file),+ }+ mock_viking_fs = MagicMock()++ async def read_file(uri, **kwargs):+ if uri not in store:+ raise NotFoundError(uri, "file")+ return store[uri]++ async def write_file(uri, content, **kwargs):+ store[uri] = content++ mock_viking_fs.read_file = AsyncMock(side_effect=read_file)+ mock_viking_fs.write_file = AsyncMock(side_effect=write_file)+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ # Bypass the real upsert path: simulate _apply_upsert copying old.md's+ # (empty) links to new.md, which is what the production code does for+ # implicit rename targets before _inherit_deleted_link_relations runs.+ updater._apply_upsert = AsyncMock(+ side_effect=lambda *args, **kwargs: store.__setitem__(+ new_uri,+ MemoryFileUtils.write(+ MemoryFile(+ uri=new_uri,+ memory_type="entities",+ content="primary+alt",+ extra_fields={"category": "person", "name": "new"},+ )+ ),+ )+ )+ updater._apply_delete = AsyncMock()+ updater._validate_uri_migrations = AsyncMock(return_value=[])+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()++ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_fields={"category": "person", "name": "new"},+ memory_type="entities",+ uris=[new_uri],+ )+ ],+ delete_file_contents=[duplicate_file],+ delete_replacements={duplicate_uri: new_uri},+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ # The duplicate's link must land on the renamed target.+ new_file = MemoryFileUtils.read(store[new_uri], uri=new_uri)+ assert any(+ link.get("from_uri") == new_uri and link.get("to_uri") == profile_uri+ for link in new_file.links+ ), new_file.links+ # And profile.md's backlink must be rewritten to come from `new.md`.+ profile_after = MemoryFileUtils.read(store[profile_uri], uri=profile_uri)+ assert any(+ link.get("from_uri") == new_uri and link.get("to_uri") == profile_uri+ for link in profile_after.backlinks+ ), profile_after.backlinks+ assert set(result.deleted_uris) >= {duplicate_uri, old_uri}+ assert result.errors == []++ @pytest.mark.asyncio+ async def test_apply_operations_keeps_source_when_explicit_replacement_is_missing(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ source_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ mock_viking_fs = MagicMock()+ mock_viking_fs.read_file = AsyncMock(side_effect=NotFoundError(target_uri, "file"))+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[],+ delete_file_contents=[source_file],+ delete_replacements={source_uri: target_uri},+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert result.deleted_uris == []+ assert len(result.errors) == 1+ assert result.errors[0][0] == source_uri+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_rejects_replacement_cycle(self):+ first_uri = "viking://user/u/memories/entities/person/first.md"+ second_uri = "viking://user/u/memories/entities/person/second.md"+ first = MemoryFile(uri=first_uri, memory_type="entities", content="first")+ second = MemoryFile(uri=second_uri, memory_type="entities", content="second")+ mock_viking_fs = MagicMock()+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[],+ delete_file_contents=[first, second],+ delete_replacements={first_uri: second_uri, second_uri: first_uri},+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert result.deleted_uris == []+ assert len(result.errors) == 2+ assert all(isinstance(error, ConflictError) for _uri, error in result.errors)+ mock_viking_fs.read_file.assert_not_called()+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_rejects_case_only_explicit_replacement(self):+ source_uri = "viking://user/u/memories/entities/person/Alice.md"+ target_uri = "viking://user/u/memories/entities/person/alice.md"+ source = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ target = MemoryFile(uri=target_uri, memory_type="entities", content="target")+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=MagicMock())+ updater._apply_upsert = AsyncMock()+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=target,+ memory_fields={"content": "merged"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[source],+ delete_replacements={source_uri: target_uri},+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert result.deleted_uris == []+ assert any("Case-only" in str(error) for _uri, error in result.errors)+ updater._apply_delete.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_apply_operations_keeps_source_when_link_migration_fails(self):+ source_uri = "viking://user/u/memories/entities/person/阿珍.md"+ target_uri = "viking://user/u/memories/entities/person/陈静娴.md"+ source_file = MemoryFile(uri=source_uri, memory_type="entities", content="source")+ mock_viking_fs = MagicMock()+ mock_viking_fs.read_file = AsyncMock(side_effect=NotFoundError(target_uri, "file"))+ updater = MemoryUpdater(registry=MagicMock())+ updater._get_viking_fs = MagicMock(return_value=mock_viking_fs)+ updater._apply_upsert = AsyncMock()+ updater._inherit_deleted_link_relations = AsyncMock(return_value=False)+ updater._apply_delete = AsyncMock()+ updater._sync_resource_refs_for_result = AsyncMock()+ updater._vectorize_memories = AsyncMock()+ updater.generate_overview = AsyncMock()+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=source_file,+ memory_fields={"name": "陈静娴"},+ memory_type="entities",+ uris=[target_uri],+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ result = await updater.apply_operations(operations, MagicMock())++ assert result.deleted_uris == []+ assert any(uri == source_uri for uri, _error in result.errors)+ updater._apply_delete.assert_not_awaited()+@pytest.mark.asyncioasync def test_apply_operations_routes_backlinks_to_matching_uri_only(self):caroline_uri = (diff --git a/tests/session/memory/test_streaming_memory_updater.py b/tests/session/memory/test_streaming_memory_updater.pyindex b24fab812..176a964e5 100644--- a/tests/session/memory/test_streaming_memory_updater.py+++ b/tests/session/memory/test_streaming_memory_updater.py@@ -31,6 +31,7 @@ from openviking.session.memory.streaming_memory_updater import (StreamingMemoryUpdater,StreamingMemoryUpdaterConfig,StreamingMemoryUpdateResult,+ acquire_memory_operation_lease,classify_memory_merge_mode,enforce_merge_group_peer_id,get_streaming_memory_updater,@@ -126,6 +127,11 @@ class RecordingPathlockClient:self.events.append(("acquire", tuple(paths), timeout_secs))return lease+ async def pathlock_acquire_exact_tree_batch(self, exact_paths, tree_paths, timeout_secs=0.0):+ lease = {"lease_ref": "memory-mixed-lease"}+ self.events.append(("acquire_mixed", tuple(exact_paths), tuple(tree_paths), timeout_secs))+ return lease+async def pathlock_release(self, lease):self.events.append(("release", lease))@@ -375,6 +381,125 @@ async def test_replacement_reacquires_persisted_relation_locks_before_writes(monassert fs.events.index(acquires[1]) < min(fs.events.index(event) for event in writes)+async def test_streaming_apply_migrates_uri_under_one_stable_lease(monkeypatch):+ source_uri = "viking://user/u/memories/notes/old.md"+ target_uri = "viking://user/u/memories/notes/new.md"+ old_file = MemoryFile(+ uri=source_uri,+ content="old content",+ memory_type="notes",+ extra_fields={"note_name": "old"},+ )+ fs = PathlockedInMemoryVikingFS({source_uri: MemoryFileUtils.write(old_file)})+ fs.search = AsyncMock(return_value=[])+ for module in ("streaming_memory_updater", "memory_updater"):+ monkeypatch.setattr(f"openviking.session.memory.{module}.get_viking_fs", lambda: fs)++ registry = _registry()+ registry.get("notes").fields[0].merge_op = MergeOp.REPLACE+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_type="notes",+ uris=[target_uri],+ memory_fields={"note_name": "new"},+ )+ ],+ delete_file_contents=[],+ errors=[],+ )+ messages = [Message(id="m1", role="user", parts=[TextPart("rename note")])]++ result = await StreamingMemoryUpdater(registry=registry)._apply_operations(+ operations=operations,+ request=MemoryUpdateRequest(+ operations=operations,+ messages=messages,+ ctx=_ctx(),+ memory_registry=registry,+ ),+ messages=messages,+ )++ assert result.written_uris == [target_uri]+ assert result.deleted_uris == [source_uri]+ assert source_uri not in fs.files+ assert MemoryFileUtils.read(fs.files[target_uri]).extra_fields["note_name"] == "new"+ acquire = next(event for event in fs.events if event[0] == "acquire")+ assert "/user/u/memories/notes/old.md" in acquire[1]+ assert "/user/u/memories/notes/new.md" in acquire[1]+ lease = {"lease_ref": "memory-batch-lease"}+ assert all(event[2] == lease for event in fs.events if event[0] == "write")+ assert fs.events[-1] == ("release", lease)+++async def test_cross_directory_migration_tree_locks_old_parent():+ source_uri = "viking://user/u/memories/entities/人物/小美.md"+ target_uri = "viking://user/u/memories/entities/person/xiaomei.md"+ old_file = MemoryFile(+ uri=source_uri,+ content="小美",+ memory_type="entities",+ extra_fields={"category": "人物", "name": "小美"},+ )+ fs = PathlockedInMemoryVikingFS({source_uri: MemoryFileUtils.write(old_file)})+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_type="entities",+ uris=[target_uri],+ memory_fields={"category": "person", "name": "xiaomei"},+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ lease = await acquire_memory_operation_lease(operations, fs, _ctx())++ source_directory_path = "/user/u/memories/entities/人物"+ target_file_path = "/user/u/memories/entities/person/xiaomei.md"+ mixed = next(event for event in fs.events if event[0] == "acquire_mixed")+ assert mixed[2] == (source_directory_path,)+ assert target_file_path in mixed[1]+ assert not any(path.startswith(f"{source_directory_path}/") for path in mixed[1])+ assert lease == {"lease_ref": "memory-mixed-lease"}+++async def test_same_directory_migration_keeps_exact_locks():+ source_uri = "viking://user/u/memories/entities/person/小美.md"+ target_uri = "viking://user/u/memories/entities/person/xiaomei.md"+ old_file = MemoryFile(+ uri=source_uri,+ content="小美",+ memory_type="entities",+ extra_fields={"category": "person", "name": "小美"},+ )+ fs = PathlockedInMemoryVikingFS({source_uri: MemoryFileUtils.write(old_file)})+ operations = ResolvedOperations(+ upsert_operations=[+ ResolvedOperation(+ old_memory_file_content=old_file,+ memory_type="entities",+ uris=[target_uri],+ memory_fields={"category": "person", "name": "xiaomei"},+ )+ ],+ delete_file_contents=[],+ errors=[],+ )++ lease = await acquire_memory_operation_lease(operations, fs, _ctx())++ assert not any(event[0] == "acquire_mixed" for event in fs.events)+ exact = next(event for event in fs.events if event[0] == "acquire")+ assert "/user/u/memories/entities/person/小美.md" in exact[1]+ assert "/user/u/memories/entities/person/xiaomei.md" in exact[1]+ assert lease == {"lease_ref": "memory-batch-lease"}++async def test_operation_to_patch_skips_failed_field_preview_update():schema = MemoryTypeSchema(memory_type="notes",@@ -884,6 +1009,40 @@ def test_scope_memory_update_result_to_submitter_filters_shared_batch_by_source(assert scoped.metadata["unscoped_written_uris"] == [op_a.uris[0], op_b.uris[0]]+def test_split_request_by_merge_group_keeps_rename_source_for_add_and_delete():+ old_uri = "viking://user/u/memories/notes/old.md"+ new_uri = "viking://user/u/memories/notes/new.md"+ old_file = MemoryFile(+ uri=old_uri,+ content="old body",+ memory_type="notes",+ extra_fields={"note_name": "old"},+ )+ rename_op = ResolvedOperation(+ old_memory_file_content=old_file,+ memory_type="notes",+ uris=[new_uri],+ memory_fields={"note_name": "new", "content": "old body"},+ )+ request = MemoryUpdateRequest(+ operations=ResolvedOperations(+ upsert_operations=[rename_op],+ delete_file_contents=[],+ errors=[],+ ),+ messages=[],+ ctx=_ctx(),+ )++ grouped = split_request_by_merge_group(request)++ [(_, group_request)] = grouped+ [cloned] = group_request.operations.upsert_operations+ assert cloned.uris == [new_uri]+ assert cloned.old_memory_file_content is not None+ assert cloned.old_memory_file_content.uri == old_uri++def test_split_request_by_merge_group_groups_by_peer_and_memory_type():self_op = _note_op("self_note")peer_op = _peer_note_op("peer_note", "web-visitor-alice")diff --git a/tests/test_link_renderer.py b/tests/test_link_renderer.pyindex 95ea78547..2461f7ca5 100644--- a/tests/test_link_renderer.py+++ b/tests/test_link_renderer.py@@ -608,6 +608,22 @@ class TestStripLinks:result = LinkRenderer.strip_all_links(content)assert result == "用户上传了一张越前龙马的照片。"+ def test_strip_managed_links_preserves_external_and_unrelated_links(self):+ source = "viking://user/u/memories/events/visit.md"+ old_target = "viking://user/u/memories/entities/person/阿珍.md"+ content = (+ "[阿珍](../entities/person/阿珍.md) visited "+ "[GitHub](https://github.com) and [notes](./notes.md)."+ )++ result = LinkRenderer.strip_managed_links(+ content,+ source,+ [{"from_uri": source, "to_uri": old_target, "match_text": "阿珍"}],+ )++ assert result == "阿珍 visited [GitHub](https://github.com) and [notes](./notes.md)."+class TestRoundTrip:def test_render_then_strip(self):