Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(memory): preserve Markdown structure in resource links (#6054)

zhanggaoyuanf2b09cd1c4a8
diff --git a/openviking/session/memory/utils/resource_refs.py b/openviking/session/memory/utils/resource_refs.py
index f4b594941..b78951a01 100644
--- a/openviking/session/memory/utils/resource_refs.py
+++ b/openviking/session/memory/utils/resource_refs.py
@@ -14,8 +14,8 @@ from openviking.session.memory.utils.link_renderer import LinkRenderer
RESOURCE_REF_SOURCE_CONTENT_WRITE = "content.write"
RESOURCE_REF_SOURCE_SESSION_COMMIT = "session.commit"
-_RESOURCE_URI_PATH_CHARS = r"[^\s<>\]\)\"',。;:!?、,;:!?)】》]+"
-_RESOURCE_URI_BOUNDARY = r"(?=$|[\s<>\]\)\"',。;:!?、,;:!?.)】》])"
+_RESOURCE_URI_PATH_CHARS = r"[^\s<>\]\)\"',。;:!?、,;:!?()【】《》]+"
+_RESOURCE_URI_BOUNDARY = r"(?=$|[\s<>\]\)\"',。;:!?、,;:!?.()【】《》])"
_RESOURCE_URI_PATTERN = (
r"viking://(?:"
rf"resources(?:/{_RESOURCE_URI_PATH_CHARS})?"
@@ -33,6 +33,10 @@ _INLINE_CODE_RE = re.compile(r"`[^`\n]+`")
_TRAILING_URI_PUNCTUATION = ".,;:!?,。;:!?、)】》"
_SENTENCE_BOUNDARIES = "。!?.!?\n"
_MAX_LINKIFIED_SENTENCE_CHARS = 160
+_MARKDOWN_CONTAINER_PREFIX_RE = re.compile(
+ r" {0,3}(?:>[ \t]?|(?:[-+*]|\d{1,9}[.)])[ \t]+(?:\[[ xX]\][ \t]+)?)"
+)
+_MARKDOWN_HEADING_PREFIX_RE = re.compile(r" {0,3}#{1,6}(?:[ \t]+|$)")
def sync_memory_resource_refs(
@@ -272,12 +276,25 @@ def _previous_sentence_span(content: str, uri_start: int) -> Optional[tuple[int,
if sentence_end <= 0:
return None
+ line_start = content.rfind("\n", 0, sentence_end) + 1
+ sentence_start = _skip_markdown_line_prefix(content, line_start, uri_start)
+ table_start = sentence_start
+ while table_start < sentence_end and content[table_start].isspace():
+ table_start += 1
+ is_pipe_table = table_start < sentence_end and content[table_start] == "|"
boundary_search_end = sentence_end
if content[sentence_end - 1] in _SENTENCE_BOUNDARIES:
boundary_search_end = sentence_end - 1
- sentence_start = 0
- for idx in range(boundary_search_end - 1, -1, -1):
- if content[idx] in _SENTENCE_BOUNDARIES:
+ for idx in range(boundary_search_end - 1, sentence_start - 1, -1):
+ is_boundary = content[idx] in _SENTENCE_BOUNDARIES
+ if is_pipe_table and content[idx] == "|":
+ backslashes = 0
+ cursor = idx - 1
+ while cursor >= table_start and content[cursor] == "\\":
+ backslashes += 1
+ cursor -= 1
+ is_boundary = backslashes % 2 == 0
+ if is_boundary:
sentence_start = idx + 1
break
while sentence_start < sentence_end and content[sentence_start].isspace():
@@ -289,6 +306,15 @@ def _previous_sentence_span(content: str, uri_start: int) -> Optional[tuple[int,
return sentence_start, sentence_end
+def _skip_markdown_line_prefix(content: str, start: int, end: int) -> int:
+ position = start
+ while match := _MARKDOWN_CONTAINER_PREFIX_RE.match(content, position, end):
+ position = match.end()
+ if match := _MARKDOWN_HEADING_PREFIX_RE.match(content, position, end):
+ position = match.end()
+ return position
+
+
def _merge_resource_refs(
mf: MemoryFile,
refs: Sequence[Dict[str, Any]],
diff --git a/tests/session/memory/test_resource_refs.py b/tests/session/memory/test_resource_refs.py
index 620eec28f..74e23d843 100644
--- a/tests/session/memory/test_resource_refs.py
+++ b/tests/session/memory/test_resource_refs.py
@@ -69,6 +69,10 @@ def test_extract_resource_uris_stops_at_common_sentence_delimiters():
"看了 viking://resources/images/foo.jpeg)然后记录",
"viking://resources/images/foo.jpeg",
),
+ (
+ "看了 viking://resources/images/foo.jpeg(临时上传)",
+ "viking://resources/images/foo.jpeg",
+ ),
(
"read viking://resources/images/foo.jpeg, then commented",
"viking://resources/images/foo.jpeg",
@@ -119,6 +123,64 @@ def test_sync_memory_resource_refs_keeps_bare_uri_clean_before_chinese_punctuati
]
+ ("content", "expected"),
+ [
+ (
+ "## Notes\n- saved at viking://resources/a\n- other",
+ "## Notes\n- [saved at](viking://resources/a)\n- other",
+ ),
+ ("# Title viking://resources/a", "# [Title](viking://resources/a)"),
+ (
+ "> quote viking://resources/a",
+ "> [quote](viking://resources/a)",
+ ),
+ (
+ "| col | viking://resources/a |",
+ "| col | viking://resources/a |",
+ ),
+ (
+ "| A \\| B viking://resources/a |",
+ "| [A \\| B](viking://resources/a) |",
+ ),
+ (
+ "fallback A | use B viking://resources/a",
+ "[fallback A | use B](viking://resources/a)",
+ ),
+ (
+ "- handoff viking://resources/a(temp_upload 通道)",
+ "- [handoff](viking://resources/a)(temp_upload 通道)",
+ ),
+ (
+ "> 2. [x] handoff viking://resources/a",
+ "> 2. [x] [handoff](viking://resources/a)",
+ ),
+ (
+ "#5 release viking://resources/a",
+ "[#5 release](viking://resources/a)",
+ ),
+ (
+ "-42 release viking://resources/a",
+ "[-42 release](viking://resources/a)",
+ ),
+ (
+ "1. viking://resources/a",
+ "1. viking://resources/a",
+ ),
+ (
+ "- [ ] viking://resources/a",
+ "- [ ] viking://resources/a",
+ ),
+ ],
+)
+def test_sync_memory_resource_refs_preserves_markdown_line_structure(content, expected):
+ mf = MemoryFile(content=content, extra_fields={})
+
+ assert sync_memory_resource_refs(mf, source="session.commit")
+ assert mf.content == expected
+ assert mf.extra_fields["resource_refs"][0]["resource_uri"] == "viking://resources/a"
+
+
def test_unlink_resource_references_preserves_visible_markdown_text():
resource_uri = "viking://resources/images/2026/06/12/yueqian_jpeg"
child_uri = f"{resource_uri}/child.jpeg"