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
fix(memory): make cross-session updates and partial deletions reliable (#4121)
Jiajie - He/him/hisb9ec4f5b9adb
diff --git a/openviking/session/memory/extract_loop.py b/openviking/session/memory/extract_loop.pyindex 4e3c7ea12..81769c5fa 100644--- a/openviking/session/memory/extract_loop.py+++ b/openviking/session/memory/extract_loop.py@@ -166,6 +166,9 @@ class ExtractLoop:self.schema_model_generator = SchemaModelGenerator(schemas,template_context={"language": output_language},+ # include_decision_reasoning=(+ # type(self.context_provider).__name__ != "PatchMergeContextProvider"+ # ),)self.schema_model_generator.generate_all_models()@@ -208,6 +211,7 @@ class ExtractLoop:- For new items, assign a unique page_id >= 100.- When editing an existing item, reuse its existing page_id.- To delete an existing item, add an entry to `delete_ids` using its page_id.+- `delete_ids` deletes the whole item: use it only if every substantive fact is in scope; otherwise MUST use DELETE blocks for affected lines, preserving the rest and not inferring scope from the file name/topic.- For canonical merges, set `replacement_page_id` to the surviving page that should inherit the deleted page's existing links/backlinks; for pure deletes, set `replacement_page_id` to null."""link_rules = ""@@ -227,7 +231,7 @@ class ExtractLoop:## Read Format Rules- The read tool accepts `uri`, optional `offset` (0-indexed), and optional `limit`.- Read content is returned in Claude Code format: each visible line is prefixed with `line_number<TAB>`.-- When you copy text from read results into SEARCH/REPLACE operations, copy the exact text after the line-number prefix. Never include the line-number prefix itself in `search` or `replace`.+- When you copy text from read results into SEARCH/REPLACE or DELETE operations, copy the exact text after the line-number prefix. Never include the line-number prefix itself in `search`, `replace`, or `delete`.## Output FormatThe final output of the model must strictly follow the JSON Schema format shown below:```json@@ -783,7 +787,11 @@ The final output of the model must strictly follow the JSON Schema format shownself,operations: ResolvedOperations,) -> List[Dict[str, Any]]:- from openviking.session.memory.merge_op.base import SearchReplaceBlock, StrPatch+ from openviking.session.memory.merge_op.base import (+ DeleteBlock,+ SearchReplaceBlock,+ StrPatch,+ )from openviking.session.memory.merge_op.patch_handler import unescape_markerserrors = []@@ -802,10 +810,10 @@ The final output of the model must strictly follow the JSON Schema format shownblocks = patch_value.blockselif isinstance(patch_value, dict) and "blocks" in patch_value:for raw_block in patch_value.get("blocks", []):- if isinstance(raw_block, SearchReplaceBlock):+ if isinstance(raw_block, (SearchReplaceBlock, DeleteBlock)):blocks.append(raw_block)elif isinstance(raw_block, dict):- blocks.append(SearchReplaceBlock(**raw_block))+ blocks.extend(StrPatch.model_validate({"blocks": [raw_block]}).blocks)if not blocks:continueworking_content = current_content@@ -854,18 +862,18 @@ The final output of the model must strictly follow the JSON Schema format shown)breakif errors:- tracer.info(f"SEARCH/REPLACE patch validation failed before apply: {errors}")+ tracer.info(f"String patch validation failed before apply: {errors}")return errorsdef _build_patch_repair_instruction(self, patch_errors: List[Dict[str, Any]]) -> str:details = json.dumps(patch_errors, ensure_ascii=False, indent=2)return (- "SEARCH/REPLACE patch could not be applied to the target memory file. "- "The SEARCH text must be copied exactly from the read result of the file bound to that operation's page_id. "- "The SEARCH text must occur exactly once in the target file. "- "If it occurs more than once, include enough contiguous surrounding context to make the SEARCH text unique. "- "Do not use SEARCH text from the conversation or from another page. "- "If you copy from numbered read output, exclude the `line_number<TAB>` prefix from SEARCH and REPLACE text. "+ "The SEARCH/REPLACE or DELETE patch could not be applied to the target memory file. "+ "The SEARCH or DELETE text must be copied exactly from the read result of the file bound to that operation's page_id. "+ "The matched text must occur exactly once in the target file. "+ "If it occurs more than once, include enough contiguous surrounding context to make it unique. "+ "Do not use match text from the conversation or from another page. "+ "If you copy from numbered read output, exclude the `line_number<TAB>` prefix from SEARCH, REPLACE, and DELETE text. ""If found_in_other_uris is non-empty, diagnose this as a possible page_id mismatch and choose the correct target page_id or rewrite the patch for the current page_id; do not silently move the patch. ""Regenerate the complete operations JSON, including previous successful operations and fixed failed operations. ""Output ONLY the complete JSON object matching the required schema.\n\n"diff --git a/openviking/session/memory/memory_updater.py b/openviking/session/memory/memory_updater.pyindex 8ca6b391f..40a7c5b16 100644--- a/openviking/session/memory/memory_updater.py+++ b/openviking/session/memory/memory_updater.py@@ -779,16 +779,16 @@ class MemoryUpdater:directory_uri: str,ctx: RequestContext,strict: bool = False,- ) -> None:+ ) -> bool:memory_type = cls.memory_type_from_uri(directory_uri)if not memory_type:- return+ return Falsetry:from openviking.session.memory.memory_type_registry import create_default_registryupdater = cls(registry=create_default_registry())updater._viking_fs = viking_fs- await updater.generate_overview(memory_type, directory_uri, ctx)+ return await updater.generate_overview(memory_type, directory_uri, ctx)except Exception:logger.warning("Failed to refresh memory overview for %s",@@ -797,6 +797,7 @@ class MemoryUpdater:)if strict:raise+ return False@classmethodasync def refresh_file_embedding(@@ -1498,7 +1499,7 @@ class MemoryUpdater:ctx: RequestContext,extract_context: Any = None,lease_ref: Any = None,- ) -> None:+ ) -> bool:"""Generate .overview.md file for a directory based on overview_template.@@ -1515,7 +1516,7 @@ class MemoryUpdater:if not schema or not schema.overview_template:logger.debug(f"No overview_template for memory type: {memory_type}")- return+ return Falseviking_fs = self._get_viking_fs()@@ -1538,10 +1539,10 @@ class MemoryUpdater:except (NotFoundError, FileNotFoundError):logger.debug("Skip overview generation for deleted directory: %s", directory)- return+ return Falseexcept Exception as e:tracer.error(f"Failed to list files in {directory}: {e}")- return+ return False# If no memory files, delete the .overview.md and the directory if emptyif not md_files:@@ -1569,7 +1570,7 @@ class MemoryUpdater:)except Exception:pass- return+ return True# Parse each file and collect itemsitems = []@@ -1594,7 +1595,7 @@ class MemoryUpdater:if not items:logger.debug(f"No valid memory files parsed in {directory}")- return+ return Falseoverview_context = {"memory_type": memory_type,@@ -1611,7 +1612,7 @@ class MemoryUpdater:)except Exception as e:tracer.error(f"Failed to render overview template for {memory_type}: {e}")- return+ return False# Write .overview.md to the directoryoverview_path = f"{directory.rstrip('/')}/.overview.md"@@ -1633,5 +1634,7 @@ class MemoryUpdater:ctx=ctx,lease_ref=lease_ref,)+ return Trueexcept Exception as e:tracer.error(f"Failed to write overview {overview_path}: {e}")+ return Falsediff --git a/openviking/session/memory/merge_op/__init__.py b/openviking/session/memory/merge_op/__init__.pyindex e8c2bafdf..8c96244e6 100644--- a/openviking/session/memory/merge_op/__init__.py+++ b/openviking/session/memory/merge_op/__init__.py@@ -5,6 +5,7 @@ Merge operation implementations."""from openviking.session.memory.merge_op.base import (+ DeleteBlock,FieldType,MergeOp,MergeOpBase,@@ -14,17 +15,18 @@ from openviking.session.memory.merge_op.base import (from openviking.session.memory.merge_op.factory import MergeOpFactoryfrom openviking.session.memory.merge_op.immutable import ImmutableOpfrom openviking.session.memory.merge_op.patch import PatchOp-from openviking.session.memory.merge_op.replace import ReplaceOpfrom openviking.session.memory.merge_op.patch_handler import (PatchParseError,apply_str_patch,)+from openviking.session.memory.merge_op.replace import ReplaceOpfrom openviking.session.memory.merge_op.sum import SumOp__all__ = ["MergeOp","MergeOpBase","FieldType",+ "DeleteBlock","SearchReplaceBlock","StrPatch","PatchOp",diff --git a/openviking/session/memory/merge_op/base.py b/openviking/session/memory/merge_op/base.pyindex 0edbb8fa3..413ac6c28 100644--- a/openviking/session/memory/merge_op/base.py+++ b/openviking/session/memory/merge_op/base.py@@ -6,7 +6,7 @@ Merge operation base classes and registry.from abc import ABC, abstractmethodfrom enum import Enum-from typing import Any, Dict, List, Optional, Type+from typing import Any, Dict, List, Optional, Type, Unionfrom pydantic import BaseModel, Field@@ -59,24 +59,49 @@ class SearchReplaceBlock(BaseModel):)replace: str = Field(...,- description="The text to replace it with (must be different from search). Use empty string to delete the matched content. Never include `line_number<TAB>` prefixes in REPLACE text.",+ description="Replacement text (must differ from search). Use a DELETE block for complete-line deletion. Never include `line_number<TAB>` prefixes.",)+class DeleteBlock(BaseModel):+ """Delete one or more complete, contiguous lines from a string field."""++ delete: str = Field(+ ...,+ min_length=1,+ description=(+ "Exact, unique text of complete, contiguous lines from the target page. Use this "+ "when other content must remain; delete_ids deletes the whole item. Omit "+ "`line_number<TAB>` prefixes and surrounding newlines; use separate blocks for "+ "non-contiguous lines."+ ),+ )++ @property+ def search(self) -> str:+ """Expose the text through the common patch-block interface."""+ return self.delete++ @property+ def replace(self) -> str:+ """A delete is a replacement with an empty string."""+ return ""++class StrPatch(BaseModel):- """String patch containing multiple SEARCH/REPLACE blocks.+ """String patch containing SEARCH/REPLACE and DELETE blocks.All string fields with merge_op=patch use this structure.IMPORTANT format rules for blocks:- - Each block MUST have both "search" and "replace" fields- - ✅ Correct: {"blocks": [{"search": "old text", "replace": "new text"}]}- - ❌ Wrong: {"blocks": ["just a string"]} or {"blocks": [{"search": "old"}]} (missing replace)+ - SEARCH/REPLACE: {"search": "old text", "replace": "new text"}+ - DELETE: {"delete": "one or more complete contiguous lines"}+ - Each matched text must occur exactly once in the file"""- blocks: List[SearchReplaceBlock] = Field(+ blocks: List[Union[SearchReplaceBlock, DeleteBlock]] = Field(default_factory=list,- description="List of SEARCH/REPLACE blocks. Each search block must be unique in the file.",+ description="SEARCH/REPLACE or DELETE blocks; use DELETE for partial line removal.",)def get_first_replace(self) -> Optional[str]:diff --git a/openviking/session/memory/merge_op/patch.py b/openviking/session/memory/merge_op/patch.pyindex f00c0bec7..76281e4fe 100644--- a/openviking/session/memory/merge_op/patch.py+++ b/openviking/session/memory/merge_op/patch.py@@ -1,8 +1,6 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0-"""-Patch merge operation - SEARCH/REPLACE for strings, direct replace for others.-"""+"""Patch merge operation for strings, direct replacement for other field types."""import asynciofrom typing import Any, Type@@ -11,14 +9,13 @@ from openviking.session.memory.merge_op.base import (FieldType,MergeOp,MergeOpBase,- SearchReplaceBlock,StrPatch,get_python_type_for_field,)class PatchOp(MergeOpBase):- """Patch merge operation - SEARCH/REPLACE for strings, direct replace for others."""+ """Apply SEARCH/REPLACE or DELETE blocks to strings."""op_type = MergeOp.PATCH@@ -32,7 +29,10 @@ class PatchOp(MergeOpBase):def get_output_schema_description(self, field_description: str) -> str:if self._field_type == FieldType.STRING:- return f"PATCH operation for '{field_description}'. Follow the shared SEARCH/REPLACE rules above."+ return (+ f"PATCH operation for '{field_description}'. Follow the shared "+ "SEARCH/REPLACE rules above. Use a DELETE block to remove complete lines."+ )return f"Replace value for '{field_description}'"async def apply(self, current_value: Any, patch_value: Any) -> Any:@@ -82,12 +82,7 @@ class PatchOp(MergeOpBase):if isinstance(patch_value, dict):if "blocks" in patch_value:try:- blocks = []- for block_dict in patch_value["blocks"]:- if isinstance(block_dict, dict):- blocks.append(SearchReplaceBlock(**block_dict))- else:- blocks.append(block_dict)+ blocks = StrPatch.model_validate(patch_value).blocks# Filter out empty-search blocks when there's existing contentvalid_blocks = [b for b in blocks if b.search]converted_patch = StrPatch(blocks=valid_blocks) if valid_blocks else Nonediff --git a/openviking/session/memory/merge_op/patch_handler.py b/openviking/session/memory/merge_op/patch_handler.pyindex 13bb4b97d..c50d98964 100644--- a/openviking/session/memory/merge_op/patch_handler.py+++ b/openviking/session/memory/merge_op/patch_handler.py@@ -4,11 +4,11 @@Patch handler for memory updates.Supports two modes:-1. Content patch: SEARCH/REPLACE format (enhanced with RooCode's multi-search-replace strategy)+1. Content patch: structured SEARCH/REPLACE and DELETE blocks2. Field patch: Field-level updates based on merge_opEnhanced features from RooCode:-- Support for multiple SEARCH/REPLACE blocks+- Support for multiple SEARCH/REPLACE and DELETE blocks- Fuzzy matching (fuzzy matching)- Line number handling (add, strip, detect)- Marker escaping support@@ -23,7 +23,7 @@ from dataclasses import dataclassfrom enum import Enumfrom typing import TYPE_CHECKING, Any, Dict, List, Optional-from openviking.session.memory.merge_op.base import StrPatch+from openviking.session.memory.merge_op.base import DeleteBlock, StrPatchfrom openviking.session.memory.utils.line_numbers import (add_line_numbers,every_line_has_line_numbers,@@ -848,6 +848,32 @@ class MultiSearchReplaceDiffStrategy:# ============================================================================+def _delete_complete_lines(content: str, delete_content: str) -> str:+ """Delete a unique block of complete lines, including one adjacent line ending."""+ start = content.index(delete_content)+ end = start + len(delete_content)++ starts_at_line_boundary = start == 0 or content[start - 1] == "\n"+ ends_at_line_boundary = (+ end == len(content) or content.startswith("\r\n", end) or content.startswith("\n", end)+ )+ if not starts_at_line_boundary or not ends_at_line_boundary:+ raise PatchParseError("DELETE content must contain one or more complete lines")++ # Prefer consuming the following line ending. At EOF, consume the preceding+ # line ending so deleting the final line does not leave a trailing blank line.+ if content.startswith("\r\n", end):+ end += 2+ elif content.startswith("\n", end):+ end += 1+ elif start > 0 and content[start - 2 : start] == "\r\n":+ start -= 2+ elif start > 0 and content[start - 1] == "\n":+ start -= 1++ return content[:start] + content[end:]++def apply_str_patch(original_content: str, patch: StrPatch) -> str:"""Apply a StrPatch to original content.@@ -888,7 +914,10 @@ def apply_str_patch(original_content: str, patch: StrPatch) -> str:"to make sure it is unique.")- result_content = result_content.replace(search_content, replace_content, 1)+ if isinstance(block, DeleteBlock):+ result_content = _delete_complete_lines(result_content, search_content)+ else:+ result_content = result_content.replace(search_content, replace_content, 1)if all_applied:return result_contentdiff --git a/openviking/session/memory/patch_merge_context_provider.py b/openviking/session/memory/patch_merge_context_provider.pyindex 112259154..ffbdb52a0 100644--- a/openviking/session/memory/patch_merge_context_provider.py+++ b/openviking/session/memory/patch_merge_context_provider.py@@ -181,6 +181,9 @@ is an existing file, put it in delete_ids; if it is only a new proposal, omit it"""Resolve required files plus semantic-search candidates for this merge."""required_uris = _dedupe_uris(self.required_file_uris)+ if not any(patch.before_file is None for patch in self.patches):+ return required_uris+max_extra_candidate_files = min(_MAX_EXTRA_CANDIDATE_FILES, max(5, len(required_uris)))search_limit = max_extra_candidate_files * 2candidate_uris = await self._search_candidate_file_uris(limit=search_limit)diff --git a/openviking/session/memory/schema_model_generator.py b/openviking/session/memory/schema_model_generator.pyindex c47a0895e..fdf821da5 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 (@@ -37,6 +37,33 @@ def to_pascal_case(s: str) -> str:return "".join(word.title() for word in words)+# from typing import Literal+#+# class PageDecision(BaseModel):+# """Temporary page-level reasoning for memory bad-case analysis."""+#+# page_id: int = Field(..., description="The related page_id from read results.")+# remove: List[str] = Field(+# ...,+# description=(+# "For UPDATE, list exact affected `- ...` bullets or standalone summary sentences. "+# "Use [] for KEEP or DELETE."+# ),+# )+# has_unaffected_facts: bool = Field(+# ...,+# description="Whether the page contains any fact outside remove that must be preserved.",+# )+# action: Literal["KEEP", "UPDATE", "DELETE"] = Field(+# ...,+# description=(+# "KEEP when no fact is affected; UPDATE when remove is non-empty and "+# "has_unaffected_facts is true; DELETE when the whole page is affected and "+# "has_unaffected_facts is false. For DELETE, leave remove empty."+# ),+# )++class SchemaModelGenerator:"""Dynamic Pydantic model generator from memory type schemas.@@ -49,6 +76,7 @@ class SchemaModelGenerator:self,schemas: List[MemoryTypeSchema],template_context: Optional[Dict[str, Any]] = None,+ # include_decision_reasoning: bool = True,):if hasattr(schemas, "list_all"):self._all_schemas = schemas.list_all(include_disabled=True)@@ -57,6 +85,7 @@ class SchemaModelGenerator:self._all_schemas = list(schemas)self.schemas = list(schemas)self._template_context = dict(template_context or {})+ # self._include_decision_reasoning = include_decision_reasoningself._model_cache: Dict[str, Type[BaseModel]] = {}self._flat_data_models: Dict[str, Type[BaseModel]] = {}self._operations_model: Optional[Type[BaseModel]] = None@@ -132,11 +161,14 @@ class SchemaModelGenerator:),)+ immutable_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)field_definitions[field.name] = (base_type,Field(..., description=self._render_description(field.description)),@@ -204,10 +236,17 @@ class SchemaModelGenerator:# Build field definitions for each memory_typefield_definitions: Dict[str, Tuple[Type[Any], Any]] = {}- # field_definitions["reasoning"] = (- # str,- # Field("", description="reasoning"),- # )+ # if self._include_decision_reasoning:+ # field_definitions["decision_reasoning"] = (+ # List[PageDecision],+ # Field(+ # default_factory=list,+ # description=(+ # "Before choosing operations, return one decision for every related "+ # "read page."+ # ),+ # ),+ # )for mt in enabled_memory_types:flat_model = self.create_flat_data_model(mt, role_scope)@@ -310,8 +349,7 @@ class SchemaModelGenerator:StructuredMemoryOperations.is_empty = is_emptyStructuredMemoryOperations.to_legacy_operations = to_legacy_operationsStructuredMemoryOperations._memory_type_fields = memory_type_fields # type: ignore- # Opt this model into treating a bare `[]` LLM response as an empty-ops result- # (every field is default_factory=list); see parse_json_with_stability Layer 3.+ # Every top-level field defaults to a list, so [] is a valid no-operations result.StructuredMemoryOperations._allow_empty_list_response = True # type: ignoreself._operations_model = StructuredMemoryOperationsdiff --git a/openviking/session/memory/streaming_memory_updater.py b/openviking/session/memory/streaming_memory_updater.pyindex 05c6e087b..6c424c41c 100644--- a/openviking/session/memory/streaming_memory_updater.py+++ b/openviking/session/memory/streaming_memory_updater.py@@ -13,6 +13,7 @@ from __future__ import annotationsimport asyncioimport reimport threading+from collections.abc import Iterablefrom dataclasses import dataclass, fieldfrom typing import Any, Hashable@@ -486,28 +487,105 @@ class StreamingMemoryUpdater:await viking_fs._async_agfs.pathlock_release(lease)async def _merge_requests(self, requests: list[MemoryUpdateRequest]) -> ResolvedOperations:- all_ops = ResolvedOperations(- upsert_operations=[],- delete_file_contents=[],- errors=[],- resolved_links=[],- delete_replacements={},- )+ all_ops = _combine_resolved_operations(request.operations for request in requests)+ if all_ops.has_errors():+ return all_ops++ requests_by_kind: dict[str, list[MemoryUpdateRequest]] = {+ "add": [],+ "update": [],+ "delete": [],+ }for request in requests:- ops = request.operations- all_ops.upsert_operations.extend(list(ops.upsert_operations or []))- all_ops.delete_file_contents.extend(list(ops.delete_file_contents or []))- all_ops.errors.extend(list(ops.errors or []))- all_ops.resolved_links.extend(list(getattr(ops, "resolved_links", []) or []))- all_ops.delete_replacements.update(dict(getattr(ops, "delete_replacements", {}) or {}))- return await merge_memory_operations(- operations=all_ops,- messages=_combined_request_messages(requests),- ctx=requests[0].ctx,- registry=self.registry or create_default_registry(),- strict_extract_errors=any(request.strict_extract_errors for request in requests),- trace_console=self.config.trace_console,+ adds = [+ op+ for op in request.operations.upsert_operations+ if op.old_memory_file_content is None+ ]+ updates = [+ op+ for op in request.operations.upsert_operations+ if op.old_memory_file_content is not None+ ]+ for kind, upserts, deletes in (+ ("add", adds, []),+ ("update", updates, []),+ ("delete", [], list(request.operations.delete_file_contents or [])),+ ):+ if not upserts and not deletes:+ continue+ requests_by_kind[kind].append(+ clone_memory_update_request(+ request,+ operations=ResolvedOperations(+ upsert_operations=upserts,+ delete_file_contents=deletes,+ errors=[],+ resolved_links=[],+ delete_replacements={+ file.uri: replacement_uri+ for file in deletes+ if file.uri+ if (+ replacement_uri := request.operations.delete_replacements.get(+ file.uri+ )+ )+ },+ ),+ )+ )++ async def merge_kind(kind_requests: list[MemoryUpdateRequest]) -> ResolvedOperations:+ operations = _combine_resolved_operations(+ request.operations for request in kind_requests+ )+ if not _requests_span_sessions(kind_requests):+ return operations+ return await merge_memory_operations(+ operations=operations,+ messages=_combined_request_messages(kind_requests),+ ctx=kind_requests[0].ctx,+ registry=self.registry or create_default_registry(),+ strict_extract_errors=any(+ request.strict_extract_errors for request in kind_requests+ ),+ trace_console=self.config.trace_console,+ force_merge=True,+ )++ kind_batches = [+ (kind, kind_requests)+ for kind, kind_requests in requests_by_kind.items()+ if kind_requests+ ]+ kind_results = await asyncio.gather(+ *(merge_kind(kind_requests) for _, kind_requests in kind_batches)+ )+ uri_kinds: dict[str, str] = {}+ conflicting_uris: set[str] = set()+ for (kind, _), operations in zip(kind_batches, kind_results, strict=True):+ for uri in _operation_uri_set(operations):+ previous_kind = uri_kinds.setdefault(uri, kind)+ if previous_kind != kind:+ conflicting_uris.add(uri)+ if conflicting_uris:+ return ResolvedOperations(+ upsert_operations=[],+ delete_file_contents=[],+ errors=[+ "Conflicting add/update/delete results for URIs: "+ + ", ".join(sorted(conflicting_uris))+ ],+ resolved_links=[],+ delete_replacements={},+ )+ merged = _combine_resolved_operations(kind_results)+ merged.resolved_links = merge_link_lists(+ list(all_ops.resolved_links or []),+ list(merged.resolved_links or []),)+ return mergeddef split_request_by_merge_group(@@ -605,6 +683,7 @@ async def merge_memory_operations(registry: MemoryTypeRegistry | None = None,strict_extract_errors: bool = False,trace_console: bool = False,+ force_merge: bool = False,) -> ResolvedOperations:"""Merge resolved memory operations by memory type/URI using patch context."""@@ -667,6 +746,7 @@ async def merge_memory_operations(registry=registry,peer_id=peer_id,trace_console=trace_console,+ force_merge=force_merge,)for (peer_id, memory_type) in all_group_keys],@@ -725,6 +805,13 @@ async def merge_memory_operations(if replacement_uri:merged_delete_replacements[delete_file.uri] = replacement_uri+ final_delete_uris = {file.uri for file in merged_deletes if file.uri}+ for deleted_uri, replacement_uri in dict(+ getattr(operations, "delete_replacements", {}) or {}+ ).items():+ if deleted_uri in final_delete_uris:+ merged_delete_replacements.setdefault(deleted_uri, replacement_uri)+merged_links = await filter_valid_links(merged_links,upsert_operations=merged_upserts,@@ -751,6 +838,7 @@ async def merge_one_memory_type_operations(registry: MemoryTypeRegistry | None = None,peer_id: str | None = None,trace_console: bool = False,+ force_merge: bool = False,) -> ResolvedOperations:registry = registry or create_default_registry()schema = registry.get(memory_type)@@ -768,7 +856,7 @@ async def merge_one_memory_type_operations()# Fast path: no upserts, only deletes — passthrough directly- if not operations and delete_files:+ if not force_merge and not operations and delete_files:tracer.info("[streaming_memory_updater] memory_type merge decision "f"memory_type={memory_type} mode=no_merge "@@ -794,13 +882,16 @@ async def merge_one_memory_type_operations()return ResolvedOperations(upsert_operations=list(operations),- delete_file_contents=[],+ delete_file_contents=list(delete_files),errors=[],resolved_links=[],delete_replacements={},)- fast_path, fast_path_reason = await classify_memory_merge_mode(operations, schema=schema)+ if force_merge:+ fast_path, fast_path_reason = False, "cross_session_batch"+ else:+ fast_path, fast_path_reason = await classify_memory_merge_mode(operations, schema=schema)if fast_path:tracer.info("[streaming_memory_updater] memory_type merge decision "@@ -1825,6 +1916,44 @@ def _combined_request_messages(items: list[MemoryUpdateRequest]) -> list[Messagereturn messages+def _combine_resolved_operations(+ items: Iterable[ResolvedOperations],+) -> ResolvedOperations:+ combined = ResolvedOperations(+ upsert_operations=[],+ delete_file_contents=[],+ errors=[],+ resolved_links=[],+ delete_replacements={},+ )+ for operations in items:+ combined.upsert_operations.extend(list(operations.upsert_operations or []))+ combined.delete_file_contents.extend(list(operations.delete_file_contents or []))+ combined.errors.extend(list(operations.errors or []))+ combined.resolved_links = merge_link_lists(+ combined.resolved_links,+ list(getattr(operations, "resolved_links", []) or []),+ )+ combined.delete_replacements.update(+ dict(getattr(operations, "delete_replacements", {}) or {})+ )+ return combined+++def _requests_span_sessions(items: list[MemoryUpdateRequest]) -> bool:+ """Return whether a kind batch cannot be proven to come from one session."""++ if len(items) < 2:+ return False+ session_ids: set[str] = set()+ for request in items:+ session_id = str((request.metadata or {}).get("session_id") or "").strip()+ if not session_id:+ return True+ session_ids.add(session_id)+ return len(session_ids) > 1++def _make_isolation_handler(request: MemoryUpdateRequest,extract_context: ExtractContext,diff --git a/openviking/storage/content_write.py b/openviking/storage/content_write.pyindex 09e7494f5..82a00c4c5 100644--- a/openviking/storage/content_write.py+++ b/openviking/storage/content_write.py@@ -1179,7 +1179,7 @@ class ContentWriteCoordinator:if wait and telemetry_id and self._vikingdb_has_queue():get_request_wait_tracker().register_request(telemetry_id)request_registered = True- await MemoryUpdater.refresh_schema_overview(+ overview_refreshed = await MemoryUpdater.refresh_schema_overview(viking_fs=self._viking_fs,directory_uri=root_uri,ctx=ctx,@@ -1213,7 +1213,7 @@ class ContentWriteCoordinator:queue_status=queue_status,semantic_status="skipped",vector_status=vector_status,- overview_status="complete",+ overview_status="complete" if overview_refreshed else "skipped",)except Exception:if not released:@@ -1457,7 +1457,12 @@ class ContentWriteCoordinator:raise InvalidArgumentError(f"memory write target must be inside a memory type directory: {uri}")- root_uri = VikingURI.build(*parts[: memories_idx + 2])+ if anchor_to_parent:+ parent = parsed.parent+ if parent is not None:+ root_uri = parent.uri+ else:+ root_uri = VikingURI.build(*parts[: memories_idx + 2])else:# Plain files directly under the user root are allowed (e.g. a# persona file at viking://user/<user>/persona.md); the manageddiff --git a/tests/server/test_content_write_service.py b/tests/server/test_content_write_service.pyindex c64f26787..b5bd30d9b 100644--- a/tests/server/test_content_write_service.py+++ b/tests/server/test_content_write_service.py@@ -263,12 +263,27 @@ async def test_memory_write_ignores_resource_uri_in_inline_code(service):@pytest.mark.asyncioasync def test_memory_create_refreshes_nested_schema_overview(service):ctx = RequestContext(user=service.user, role=Role.USER)+ memory_type_dir = f"viking://user/{ctx.user.user_space_name()}/memories/entities"memory_dir = f"viking://user/{ctx.user.user_space_name()}/memories/entities/动漫角色"memory_uri = f"{memory_dir}/不二周助-link-test.md"+ # Reproduce writes after the memory type root already exists. Previously this+ # collapsed the refresh root to memories/entities and skipped the category overview.+ await service.viking_fs.mkdir(memory_type_dir, exist_ok=True, ctx=ctx)+result = await service.fs.write(memory_uri,- content="用户保存了一张[不二周助](viking://resources/images/2026/06/10/不二周助_jpeg)的照片",+ content=MemoryFileUtils.write(+ MemoryFile(+ uri=memory_uri,+ memory_type="entities",+ content=(+ "用户保存了一张[不二周助]"+ "(viking://resources/images/2026/06/10/不二周助_jpeg)的照片"+ ),+ extra_fields={"category": "动漫角色", "name": "不二周助-link-test"},+ )+ ),ctx=ctx,mode="create",wait=False,diff --git a/tests/session/memory/test_memory_react.py b/tests/session/memory/test_memory_react.pyindex 69f84bdb6..30cfc4f3c 100644--- a/tests/session/memory/test_memory_react.py+++ b/tests/session/memory/test_memory_react.py@@ -193,8 +193,14 @@ class TestExtractLoopFinalJsonRetry:vlm = MagicMock()vlm.model = "test-model"vlm.get_completion_async = AsyncMock(- return_value=('{"delete_ids": [{"delete_page_id": 7, "replacement_page_id": 11}]}')+ return_value='{"delete_ids": [{"delete_page_id": 7, "replacement_page_id": 11}]}')+ # decision_reasoning response retained for when the schema field is re-enabled:+ # return_value=(+ # '{"delete_ids": [{"delete_page_id": 7, "replacement_page_id": 11}], '+ # '"decision_reasoning": [{"page_id": 7, "remove": [], '+ # '"has_unaffected_facts": false, "action": "DELETE"}]}'+ # )extract_loop = ExtractLoop(vlm=vlm,viking_fs=MagicMock(),@@ -217,6 +223,8 @@ class TestExtractLoopFinalJsonRetry:assert len(parsed_operations.delete_ids) == 1assert parsed_operations.delete_ids[0].delete_page_id == 7assert parsed_operations.delete_ids[0].replacement_page_id == 11+ # assert parsed_operations.decision_reasoning[0].action == "DELETE"+ # assert parsed_operations.decision_reasoning[0].remove == []def test_add_only_contract_does_not_allow_delete_ids(self):schema = MemoryTypeSchema(@@ -229,11 +237,53 @@ class TestExtractLoopFinalJsonRetry:)generator = SchemaModelGenerator([schema], template_context={"language": "en"})- assert "delete_ids" not in generator.create_structured_operations_model().model_fields+ operations_model = generator.create_structured_operations_model()+ fields = operations_model.model_fields+ assert "decision_reasoning" not in fields+ assert "delete_ids" not in fields++ # decision_reasoning schema tests retained for when the field is re-enabled:+ # assert next(iter(fields)) == "decision_reasoning"+ # description = fields["decision_reasoning"].description+ # assert "one decision for every related read page" in description+ # decisions = operations_model.model_validate(+ # {+ # "decision_reasoning": [+ # {+ # "page_id": 7,+ # "remove": ["- affected fact"],+ # "has_unaffected_facts": True,+ # "action": "UPDATE",+ # }+ # ]+ # }+ # ).decision_reasoning+ # assert decisions[0].has_unaffected_facts is True+ # assert operations_model.model_validate({}).decision_reasoning == []+ # with pytest.raises(ValueError):+ # operations_model.model_validate(+ # {+ # "decision_reasoning": [+ # {+ # "page_id": 7,+ # "remove": [],+ # "has_unaffected_facts": True,+ # "action": "REMOVE",+ # }+ # ]+ # }+ # )+ # merge_fields = (+ # SchemaModelGenerator([schema], include_decision_reasoning=False)+ # .create_structured_operations_model()+ # .model_fields+ # )+ # assert "decision_reasoning" not in merge_fieldsdef test_final_instruction_includes_schema_aware_empty_json(self):extract_loop = object.__new__(ExtractLoop)extract_loop._expected_fields = ["preferences", "tools"]+ # extract_loop._expected_fields = ["preferences", "tools", "decision_reasoning"]instruction = extract_loop._build_final_operations_instruction()@@ -241,6 +291,8 @@ class TestExtractLoopFinalJsonRetry:assert '"delete_ids": []' in instructionassert '"preferences": []' in instructionassert '"tools": []' in instruction+ assert '"decision_reasoning": []' not in instruction+ # assert '"decision_reasoning": []' in instruction@pytest.mark.asyncioasync def test_patch_validation_uses_plain_and_sequential_content(self):@@ -353,3 +405,16 @@ class TestExtractLoopFinalJsonRetry:assert final_promptsassert '"delete_ids": []' in final_prompts[-1]assert '"preferences": []' in final_prompts[-1]++ system_prompts = [+ message["content"]+ for messages in vlm.seen_messages+ for message in messages+ if message.get("role") == "system"+ ]+ assert system_prompts+ initial_system_prompt = system_prompts[0]+ assert "`delete_ids` deletes the whole item" in initial_system_prompt+ assert "only if every substantive fact is in scope" in initial_system_prompt+ assert "otherwise MUST use DELETE blocks" in initial_system_prompt+ assert "not inferring scope from the file name/topic" in initial_system_promptdiff --git a/tests/session/memory/test_merge_ops.py b/tests/session/memory/test_merge_ops.pyindex 8f00f558b..5d74403ad 100644--- a/tests/session/memory/test_merge_ops.py+++ b/tests/session/memory/test_merge_ops.py@@ -13,6 +13,7 @@ from openviking.session.memory.dataclass import (MemoryField,)from openviking.session.memory.merge_op import (+ DeleteBlock,ImmutableOp,MergeOp,MergeOpFactory,@@ -125,13 +126,20 @@ class TestPatchOp:async def test_apply_dict_patch_propagates_patch_parse_error(self):"""Patch errors raised after dict conversion must reach the caller."""op = PatchOp(FieldType.STRING)- patch = {- "blocks": [{"search": "status: pending", "replace": "status: done"}]- }+ patch = {"blocks": [{"search": "status: pending", "replace": "status: done"}]}with pytest.raises(PatchParseError, match="matched 2 locations"):await op.apply("status: pending\nstatus: pending", patch)+ @pytest.mark.asyncio+ async def test_apply_dict_delete_patch(self):+ """Dict-form DELETE blocks should remove complete lines."""+ op = PatchOp(FieldType.STRING)+ patch = {"blocks": [{"delete": "line 2\nline 3"}]}++ assert await op.apply("line 1\nline 2\nline 3\nline 4", patch) == "line 1\nline 4"++class TestSumOp:"""Tests for SumOp."""@@ -285,6 +293,29 @@ class TestSearchReplaceBlock:assert description is not Noneassert "line_number<TAB>" in descriptionassert "Never include" in description+ assert "Use a DELETE block for complete-line deletion" in description+++class TestDeleteBlock:+ """Tests for DeleteBlock."""++ def test_create_basic(self):+ block = DeleteBlock(delete="line 2\nline 3")++ assert block.delete == "line 2\nline 3"+ assert block.search == "line 2\nline 3"+ assert block.replace == ""++ def test_delete_description_requires_complete_contiguous_lines(self):+ description = DeleteBlock.model_fields["delete"].description++ assert description is not None+ assert "complete, contiguous lines" in description+ assert "unique" in description+ assert "line_number<TAB>" in description+ assert "other content must remain" in description+ assert "delete_ids deletes the whole item" in description+ assert "non-contiguous" in descriptionclass TestStrPatch:@@ -302,6 +333,34 @@ class TestStrPatch:patch = StrPatch(blocks=[block1, block2])assert len(patch.blocks) == 2+ def test_create_with_mixed_blocks(self):+ patch = StrPatch(+ blocks=[+ SearchReplaceBlock(search="a", replace="A"),+ DeleteBlock(delete="b"),+ ]+ )++ assert isinstance(patch.blocks[0], SearchReplaceBlock)+ assert isinstance(patch.blocks[1], DeleteBlock)++ def test_parse_delete_block_from_json_shape(self):+ patch = StrPatch.model_validate({"blocks": [{"delete": "line 2"}]})++ assert patch.blocks == [DeleteBlock(delete="line 2")]++ def test_json_schema_exposes_delete_block(self):+ schema = StrPatch.model_json_schema()++ assert schema["$defs"]["DeleteBlock"]["required"] == ["delete"]+ block_refs = {+ option["$ref"] for option in schema["properties"]["blocks"]["items"]["anyOf"]+ }+ assert block_refs == {+ "#/$defs/SearchReplaceBlock",+ "#/$defs/DeleteBlock",+ }+# ============================================================================# Test StrPatch Conversion@@ -326,6 +385,34 @@ class TestApplyStrPatch:# Directly test apply_str_patchassert result == "hello there"+ @pytest.mark.parametrize(+ ("original", "delete", "expected"),+ [+ ("line 1\nline 2\nline 3", "line 2", "line 1\nline 3"),+ ("line 1\nline 2\nline 3\nline 4", "line 2\nline 3", "line 1\nline 4"),+ ("line 1\nline 2\nline 3", "line 2\nline 3", "line 1"),+ ("line 1\nline 2", "line 1", "line 2"),+ ("line 1", "line 1", ""),+ ("line 1\r\nline 2\r\nline 3", "line 2", "line 1\r\nline 3"),+ ],+ )+ def test_delete_complete_lines(self, original, delete, expected):+ patch = StrPatch(blocks=[DeleteBlock(delete=delete)])++ assert apply_str_patch(original, patch) == expected++ def test_delete_rejects_partial_line(self):+ patch = StrPatch(blocks=[DeleteBlock(delete="world")])++ with pytest.raises(PatchParseError, match="complete lines"):+ apply_str_patch("hello world", patch)++ def test_duplicate_delete_is_rejected(self):+ patch = StrPatch(blocks=[DeleteBlock(delete="duplicate")])++ with pytest.raises(PatchParseError, match="matched 2 locations"):+ apply_str_patch("duplicate\nduplicate", patch)+def test_duplicate_search_is_rejected(self):"""Ambiguous SEARCH content must fail instead of replacing globally."""original = "status: pending\nstatus: pending"diff --git a/tests/session/memory/test_patch_merge_context_provider.py b/tests/session/memory/test_patch_merge_context_provider.pyindex 39c98fdb2..29e677bee 100644--- a/tests/session/memory/test_patch_merge_context_provider.py+++ b/tests/session/memory/test_patch_merge_context_provider.py@@ -133,6 +133,35 @@ async def test_patch_merge_context_provider_prefetch_searches_and_reads_extra_caassert messages[-1]["content"].startswith("# Memory File Patches")+async def test_patch_merge_context_provider_skips_extra_candidates_for_existing_files():+ uri = "viking://user/u/memories/experiences/booking.md"+ provider = PatchMergeContextProvider(+ memory_type="experiences",+ required_file_uris=[uri],+ patches=[+ PatchMergePatch(+ before_file=_memory_file(name="booking", uri=uri, content="old line"),+ after_file=_memory_file(name="booking", uri=uri, content="new line"),+ )+ ],+ )+ provider.search_files = AsyncMock(return_value=["viking://user/u/memories/experiences/other.md"])+ provider.read_file = AsyncMock(+ return_value={+ "memory_type": "experiences",+ "experience_name": "booking",+ "content": "1\told line",+ }+ )++ messages = await provider.prefetch()++ provider.search_files.assert_not_awaited()+ provider.read_file.assert_awaited_once_with(uri)+ assert len(messages) == 2++@pytest.mark.asyncioasync def test_patch_merge_context_provider_caps_extra_candidate_reads_at_ten():schema = MemoryTypeSchema(diff --git a/tests/session/memory/test_schema_models.py b/tests/session/memory/test_schema_models.pyindex a96f4f092..3ba88e7fb 100644--- a/tests/session/memory/test_schema_models.py+++ b/tests/session/memory/test_schema_models.py@@ -2,6 +2,7 @@# SPDX-License-Identifier: AGPL-3.0"""Tests for schema_models.py - dynamic Pydantic model generation."""+import jsonimport tempfilefrom pathlib import Path@@ -22,6 +23,7 @@ from openviking.session.memory.schema_model_generator import (SchemaModelGenerator,to_pascal_case,)+from openviking.session.memory.utils.json_parser import parse_json_with_stabilityclass TestToPascalCase:@@ -271,6 +273,74 @@ class TestSchemaModelGenerator:"Temporary page_id for identifying the target memory item.")+ def test_existing_patch_can_omit_immutable_fields_but_new_item_cannot(+ self, real_registry+ ):+ entities = real_registry.get("entities")+ model = SchemaModelGenerator([entities]).create_flat_data_model(entities)++ existing = model.model_validate(+ {+ "page_id": 2,+ "content": {"blocks": [{"search": "old", "replace": "new"}]},+ }+ )++ assert existing.category is None+ assert existing.name is None+ schema = model.model_json_schema()+ assert schema["required"] == ["page_id"]+ assert schema["allOf"][0]["then"]["required"] == ["category", "name"]++ for page_id in (100, "100"):+ with pytest.raises(ValueError, match="category, name"):+ model.model_validate({"page_id": page_id, "content": "new entity"})++ created = model.model_validate(+ {+ "page_id": 100,+ "category": "activity",+ "name": "horse riding",+ "content": "new entity",+ }+ )+ assert created.category == "activity"+ assert created.name == "horse riding"++ def test_parser_keeps_existing_patch_without_immutable_fields_alongside_delete(+ self, real_registry+ ):+ generator = SchemaModelGenerator(+ [real_registry.get("preferences"), real_registry.get("entities")]+ )+ operations_model = generator.create_structured_operations_model()+ content = json.dumps(+ {+ "entities": [+ {+ "page_id": 2,+ "content": {+ "blocks": [{"search": "一周学习计划", "replace": ""}]+ },+ }+ ],+ "delete_ids": [+ {"delete_page_id": 1, "replacement_page_id": None}+ ],+ }+ )++ parsed, error = parse_json_with_stability(+ content,+ model_class=operations_model,+ expected_fields=list(operations_model.model_fields),+ )++ assert error is None+ assert len(parsed.entities) == 1+ assert parsed.entities[0].page_id == 2+ assert len(parsed.delete_ids) == 1+def test_links_field_is_not_emitted_when_links_disabled(self, registry_with_sample):from unittest.mock import patchdiff --git a/tests/session/memory/test_streaming_memory_updater.py b/tests/session/memory/test_streaming_memory_updater.pyindex cff8a7990..3b2772a0c 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 (classify_memory_merge_mode,enforce_merge_group_peer_id,get_streaming_memory_updater,+ merge_memory_operations,merge_one_memory_type_operations,operation_to_patch,render_operation_after_file_content,@@ -211,6 +212,41 @@ def _note_op_with_source(name: str, extraction_id: str) -> ResolvedOperation:return op+def _note_update_op(name: str) -> ResolvedOperation:+ uri = f"viking://user/u/memories/notes/{name}.md"+ old_file = MemoryFile(+ uri=uri,+ content=f"old {name}",+ memory_type="notes",+ extra_fields={"note_name": name},+ )+ return ResolvedOperation(+ old_memory_file_content=old_file,+ memory_type="notes",+ uris=[uri],+ memory_fields={+ "note_name": name,+ "content": StrPatch(+ blocks=[+ SearchReplaceBlock(+ search=f"old {name}",+ replace=f"new {name}",+ )+ ]+ ),+ },+ )+++def _note_delete_file(name: str) -> MemoryFile:+ return MemoryFile(+ uri=f"viking://user/u/memories/notes/{name}.md",+ content=f"delete {name}",+ memory_type="notes",+ extra_fields={"note_name": name},+ )++def _peer_note_op(name: str, peer_id: str) -> ResolvedOperation:op = _note_op(name)op.memory_fields["peer_id"] = peer_id@@ -624,6 +660,131 @@ async def test_streaming_memory_updater_batches_non_append_only_submits(monkeypaassert sorted(result1.metadata["unscoped_written_uris"]) == sorted([op1.uris[0], op2.uris[0]])+async def test_merge_requests_skips_patch_merge_for_same_session(monkeypatch):+ merge_mock = AsyncMock()+ monkeypatch.setattr(+ "openviking.session.memory.streaming_memory_updater.merge_memory_operations",+ merge_mock,+ )+ updater = StreamingMemoryUpdater(registry=_registry())++ def make_request(suffix: str, extraction_id: str) -> MemoryUpdateRequest:+ return MemoryUpdateRequest(+ operations=ResolvedOperations(+ upsert_operations=[+ _note_op(f"add_{suffix}"),+ _note_update_op(f"update_{suffix}"),+ ],+ delete_file_contents=[_note_delete_file(f"delete_{suffix}")],+ errors=[],+ ),+ messages=[],+ ctx=_ctx(),+ metadata={+ "session_id": "same-session",+ "source_extraction_id": extraction_id,+ },+ )++ merged = await updater._merge_requests(+ [+ make_request("a", "extract-a"),+ make_request("b", "extract-b"),+ ]+ )++ merge_mock.assert_not_awaited()+ assert len(merged.upsert_operations) == 4+ assert len(merged.delete_file_contents) == 2+++async def test_merge_requests_merges_cross_session_operation_kinds_in_parallel(monkeypatch):+ entered: set[str] = set()+ all_entered = asyncio.Event()+ release = asyncio.Event()++ async def fake_merge_memory_operations(**kwargs):+ operations = kwargs["operations"]+ if operations.delete_file_contents:+ kind = "delete"+ assert not operations.upsert_operations+ elif all(op.old_memory_file_content is None for op in operations.upsert_operations):+ kind = "add"+ else:+ kind = "update"+ assert all(+ op.old_memory_file_content is not None+ for op in operations.upsert_operations+ )+ assert kwargs["force_merge"] is True+ entered.add(kind)+ if len(entered) == 3:+ all_entered.set()+ await release.wait()+ return operations++ monkeypatch.setattr(+ "openviking.session.memory.streaming_memory_updater.merge_memory_operations",+ fake_merge_memory_operations,+ )+ updater = StreamingMemoryUpdater(registry=_registry())++ def make_request(suffix: str, session_id: str) -> MemoryUpdateRequest:+ return MemoryUpdateRequest(+ operations=ResolvedOperations(+ upsert_operations=[+ _note_op(f"add_{suffix}"),+ _note_update_op(f"update_{suffix}"),+ ],+ delete_file_contents=[_note_delete_file(f"delete_{suffix}")],+ errors=[],+ ),+ messages=[],+ ctx=_ctx(),+ metadata={"session_id": session_id},+ )++ merge_task = asyncio.create_task(+ updater._merge_requests(+ [+ make_request("a", "session-a"),+ make_request("b", "session-b"),+ ]+ )+ )+ await asyncio.wait_for(all_entered.wait(), timeout=5)+ assert not merge_task.done()+ release.set()+ merged = await asyncio.wait_for(merge_task, timeout=5)++ assert entered == {"add", "update", "delete"}+ assert len(merged.upsert_operations) == 4+ assert len(merged.delete_file_contents) == 2+++async def test_merge_requests_rejects_uri_conflicts_between_operation_kinds():+ updater = StreamingMemoryUpdater(registry=_registry())+ request = MemoryUpdateRequest(+ operations=ResolvedOperations(+ upsert_operations=[_note_op("conflict")],+ delete_file_contents=[_note_delete_file("conflict")],+ errors=[],+ ),+ messages=[],+ ctx=_ctx(),+ metadata={"session_id": "session-a"},+ )++ merged = await updater._merge_requests([request])++ assert merged.upsert_operations == []+ assert merged.delete_file_contents == []+ assert "Conflicting add/update/delete results" in merged.errors[0]++def test_scope_memory_update_result_to_submitter_filters_shared_batch_by_source():from openviking.session.memory.streaming_memory_updater import (scope_memory_update_result_to_submitter,@@ -1186,6 +1347,75 @@ async def test_cross_extraction_merge_preserves_existing_uri_without_explicit_deassert merged.delete_file_contents == []+async def test_force_merge_sends_delete_only_group_through_patch_merge(monkeypatch):+ delete_file = _note_delete_file("obsolete")+ replacement_uri = "viking://user/u/memories/notes/replacement.md"+ merge_called = False++ async def fake_run(self):+ nonlocal merge_called+ merge_called = True+ return (+ ResolvedOperations(+ upsert_operations=[],+ delete_file_contents=[delete_file],+ errors=[],+ ),+ [],+ )++ monkeypatch.setattr(+ "openviking.session.memory.streaming_memory_updater.ExtractLoop.run",+ fake_run,+ )+ fs = InMemoryVikingFS({delete_file.uri: delete_file.content})+ fs.search = AsyncMock(return_value=[])+ monkeypatch.setattr(+ "openviking.session.memory.streaming_memory_updater.get_viking_fs",+ lambda: fs,+ )++ merged = await merge_memory_operations(+ operations=ResolvedOperations(+ upsert_operations=[],+ delete_file_contents=[delete_file],+ errors=[],+ delete_replacements={delete_file.uri: replacement_uri},+ ),+ messages=[],+ ctx=_ctx(),+ registry=_registry(),+ force_merge=True,+ )++ assert merge_called is True+ assert merged.delete_file_contents == [delete_file]+ assert merged.delete_replacements == {delete_file.uri: replacement_uri}+++async def test_force_merge_does_not_drop_add_only_delete():+ delete_file = MemoryFile(+ uri="viking://user/u/memories/cases/obsolete.md",+ content="obsolete",+ memory_type="cases",+ extra_fields={"case_name": "obsolete"},+ )++ merged = await merge_one_memory_type_operations(+ memory_type="cases",+ operations=[],+ delete_files=[delete_file],+ messages=[],+ ctx=_ctx(),+ registry=_registry(),+ force_merge=True,+ )++ assert merged.delete_file_contents == [delete_file]++@pytest.mark.asyncioasync def test_patch_merge_uses_original_messages_for_output_language(monkeypatch):existing_uri = "viking://user/u/memories/notes/code.md"diff --git a/tests/storage/test_content_write_processing_mode.py b/tests/storage/test_content_write_processing_mode.pyindex 3aa3a029c..5658c3b80 100644--- a/tests/storage/test_content_write_processing_mode.py+++ b/tests/storage/test_content_write_processing_mode.py@@ -197,12 +197,18 @@ async def test_vectors_only_write_wait_reports_skipped_when_nothing_enqueued(mon@pytest.mark.asyncio-async def test_memory_write_accepts_processing_mode_without_switching_refresh(monkeypatch, ctx):+ ("overview_refreshed", "expected_overview_status"),+ [(True, "complete"), (False, "skipped")],+)+async def test_memory_write_accepts_processing_mode_without_switching_refresh(+ monkeypatch, ctx, overview_refreshed, expected_overview_status+):fake_fs = _FakeVikingFS()monkeypatch.setattr(content_write_module.MemoryUpdater,"refresh_schema_overview",- AsyncMock(),+ AsyncMock(return_value=overview_refreshed),)monkeypatch.setattr(content_write_module.MemoryUpdater,@@ -234,4 +240,4 @@ async def test_memory_write_accepts_processing_mode_without_switching_refresh(mocontent_write_module.MemoryUpdater.refresh_file_embedding.assert_awaited_once()assert result["context_type"] == "memory"assert result["semantic_status"] == "skipped"- assert result["overview_status"] == "complete"+ assert result["overview_status"] == expected_overview_statusdiff --git a/tests/unit/session/memory/test_extract_loop_match_text.py b/tests/unit/session/memory/test_extract_loop_match_text.pyindex ce89f8ada..342717ba7 100644--- a/tests/unit/session/memory/test_extract_loop_match_text.py+++ b/tests/unit/session/memory/test_extract_loop_match_text.py@@ -31,7 +31,7 @@ class TestResolveOperations:directory="viking://user/{{ user_space }}/memories/entities",filename_template="{{ name }}.md",fields=[- MemoryField(name="name", field_type=FieldType.STRING, merge_op=MergeOp.REPLACE),+ MemoryField(name="name", field_type=FieldType.STRING, merge_op=MergeOp.IMMUTABLE),MemoryField(name="content", field_type=FieldType.STRING, merge_op=MergeOp.PATCH),],)@@ -64,7 +64,7 @@ class TestResolveOperations:operations, _ = await loop.resolve_operations(AttrDict(- entities=[{"name": "WrongName", "content": "new content", "page_id": 7}],+ entities=[{"content": "new content", "page_id": 7}],delete_uris=[],))