[Bug]: parse_memory_file_with_fields parses first MEMORY_FIELDS match instead of trailing trailer, silently corrupting metadata
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
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:
- Parser inconsistency:
parse_memory_file_with_fields(openviking/session/memory/utils/messages.py:40) usesMEMORY_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_trailerandstrip_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()). - Silent degradation: When the matched block fails to parse as valid JSON or lacks required schema fields,
parse_memory_file_with_fieldscatches the error, logs a warning, and returns{}. Downstream inMemoryFile.to_metadata(), missing identity fields default toversion: 1, wiping outmemory_type,category,name,user, andtopic. - Write propagation: In
ContentWriteCoordinator._write_in_place(openviking/storage/content_write.py:2603),mode="replace"reads the file viaMemoryFileUtils.read(), attaches new body, and re-serializes viaMemoryFileUtils.write(). This permanently stamps the degraded<!-- MEMORY_FIELDS {"version": 1} -->trailer back into storage.
Steps to Reproduce
- 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
}
-->
- Read the memory card using
MemoryFileUtils.read(content)or trigger an update withov write --mode replace. - Inspect the parsed metadata and the re-serialized file.
Expected Behavior
parse_memory_file_with_fieldsshould strictly parse the trailing trailer (matchingfind_memory_fields_trailer), ignoring any<!-- MEMORY_FIELDSexamples embedded inside the markdown body.- 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
parse_memory_file_with_fieldspicks the first match ({"example": true}) and strips it, leaving the real metadata trailer dangling inside the body.- The core identity fields (
memory_type,category,name) are lost. - 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 ofMEMORY_FIELDS_COMMENT_RE.search(content)to locate the real metadata block at the end of the content. - Slice
content[:match.start()].rstrip()instead ofre.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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from volcengine/OpenViking
-
[Bug]: OpenCode v2 skips an assistant message that gains text after an earlier capturePossibly taken @jianyx1 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
volcengine/OpenViking#8354 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
volcengine/OpenViking#8116 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
volcengine/OpenViking#7732 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
volcengine/OpenViking#7622 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
volcengine/OpenViking#7620 ·
Maintainers usually reply within 1 day
All issues in volcengine/OpenViking
Similar issues
-
bug docs
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
ActivityWatch/docs#223 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ARTPARK-SAHAI-ORG/calibrate-frontend#842 ·
Maintainers usually reply within 1 day
-
comp/tools P2 tool/skills type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NousResearch/hermes-agent#159541 ·
Maintainers usually reply within 1 day
-
bot-triaged module: correctness (silent) module: cpu module: empty tensor module: loss triage review
Difficulty 1/5 Under an hour Newbie friendliness 80/100
Maintainers usually reply within 1 day