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

Aperta Adatta ai principianti
#8,113 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@r266-tech ci sta già lavorando.

Dal 20/11/2026.

  • #8115 di @r266-tech — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia con parse_memory_file_with_fields in openviking/session/memory/utils/messages.py, confrontando il suo MEMORY_FIELDS_COMMENT_RE.search con find_memory_fields_trailer in openviking/session/memory/utils/memory_fields.py. Esegui il reproducer minimo dell'issue per confermare che viene scelto il primo blocco, poi passa il parser all'helper del trailer e taglia il corpo con content[:match.start()].rstrip(). Fatto significa che il reproducer analizza il blocco finale e gli esempi inline sopravvivono verbatim; controlla che MemoryFileUtils.read/write in openviking/storage/content_write.py continuino a fare round-trip.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
Python
Stelle
42.6k
Fork
3.6k
Merge medio
15h 44m
PR unite (30g)
558

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di volcengine/OpenViking

Tutte le issue di volcengine/OpenViking

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.