[Bug]: parse_memory_file_with_fields parses first MEMORY_FIELDS match instead of trailing trailer, silently corrupting metadata

Open Beginner friendly
#8,113 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@r266-tech is already working on this.

Since Nov 20, 2026.

  • #8115 by @r266-tech — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with parse_memory_file_with_fields in openviking/session/memory/utils/messages.py, comparing its MEMORY_FIELDS_COMMENT_RE.search against find_memory_fields_trailer in openviking/session/memory/utils/memory_fields.py. Run the minimal reproducer from the issue to confirm the first block is picked, then switch the parser to the trailer helper and slice the body with content[:match.start()].rstrip(). Done means the reproducer parses the trailing block and inline examples survive verbatim; check MemoryFileUtils.read/write in openviking/storage/content_write.py still round-trips.

Written by the indexing model from the issue text.

Description

Issue Origin

Observed or reproduced in a real environment

Bug Description

In OpenViking, memory files store metadata in a trailing HTML comment block: <!-- MEMORY_FIELDS { ... } -->.

However, there is an architectural inconsistency between how the metadata trailer is parsed vs. stripped/validated:

  1. Parser inconsistency: parse_memory_file_with_fields (openviking/session/memory/utils/messages.py:40) uses MEMORY_FIELDS_COMMENT_RE.search(content). This searches from the beginning of the file and matches the first occurrence. If a memory file contains documentation, user notes, or markdown examples referencing <!-- MEMORY_FIELDS, the parser mistakes the body prose for the file's metadata. In contrast, find_memory_fields_trailer and strip_memory_fields_trailer (openviking/session/memory/utils/memory_fields.py) strictly require the trailer to be at the very end of the file (not content[match.end():].strip()).
  2. Silent degradation: When the matched block fails to parse as valid JSON or lacks required schema fields, parse_memory_file_with_fields catches the error, logs a warning, and returns {}. Downstream in MemoryFile.to_metadata(), missing identity fields default to version: 1, wiping out memory_type, category, name, user, and topic.
  3. Write propagation: In ContentWriteCoordinator._write_in_place (openviking/storage/content_write.py:2603), mode="replace" reads the file via MemoryFileUtils.read(), attaches new body, and re-serializes via MemoryFileUtils.write(). This permanently stamps the degraded <!-- MEMORY_FIELDS {"version": 1} --> trailer back into storage.
Steps to Reproduce
  1. Create a memory card containing an example or reference to MEMORY_FIELDS:
# Software Tool: Memory Fields

OpenViking uses the following trailer format:
<!-- MEMORY_FIELDS
{
  "example": true
}
-->
Some explanation text.

<!-- MEMORY_FIELDS
{
  "memory_type": "entities",
  "category": "software_tool",
  "name": "memory_fields",
  "version": 3
}
-->
  1. Read the memory card using MemoryFileUtils.read(content) or trigger an update with ov write --mode replace.
  2. Inspect the parsed metadata and the re-serialized file.
Expected Behavior
  1. parse_memory_file_with_fields should strictly parse the trailing trailer (matching find_memory_fields_trailer), ignoring any <!-- MEMORY_FIELDS examples embedded inside the markdown body.
  2. If metadata extraction or JSON parsing fails, the coordinator / parser should fail fast or surface an error rather than silently degrading into { "version": 1 }.
Actual Behavior
  1. parse_memory_file_with_fields picks the first match ({"example": true}) and strips it, leaving the real metadata trailer dangling inside the body.
  2. The core identity fields (memory_type, category, name) are lost.
  3. Subsequent indexing fails with Missing embedding template variables, and entity-based deduplication/compilation is completely broken for this file.
Minimal Reproducible Example
from openviking.session.memory.utils.messages import parse_memory_file_with_fields
from openviking.session.memory.utils.memory_fields import find_memory_fields_trailer

content = """# Memory Fields Guide
Example:
<!-- MEMORY_FIELDS
{
  "example_field": "foo"
}
-->

Content text.

<!-- MEMORY_FIELDS
{
  "memory_type": "entities",
  "category": "software_tool",
  "name": "memory_fields",
  "version": 2
}
-->"""

parsed = parse_memory_file_with_fields(content)
trailer = find_memory_fields_trailer(content)

print("Parsed fields (picked first):", {k: v for k, v in parsed.items() if k != "content"})
# Output: {'example_field': 'foo'}  <-- Wrong! Should have parsed the trailing block

print("find_memory_fields_trailer (picked last):", trailer.group("fields").strip() if trailer else None)
# Output: correctly finds the second block
Environment
  • OpenViking Version: 0.5.5 / main
  • Python Version: 3.12 / 3.13
  • Operating System: Windows / Linux
Proposed Fix

In openviking/session/memory/utils/messages.py:

  • Use find_memory_fields_trailer(content) instead of MEMORY_FIELDS_COMMENT_RE.search(content) to locate the real metadata block at the end of the content.
  • Slice content[:match.start()].rstrip() instead of re.sub(), so that comments inside the body are preserved verbatim.
Dominant language
Python
Stars
42.6k
Forks
3.6k
Avg merge
15h 44m
Merged PRs (30d)
558

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from volcengine/OpenViking

All issues in volcengine/OpenViking

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.