From 6498bc7fbca6ac68dc63df3478e9701afde5b6e9 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sun, 1 Nov 2026 09:13:39 -0500 Subject: [PATCH] fix(memory): keep footnote-like tokens in entry text on round-trip parse() swallowed a `[^token]` that was part of an entry's text. The new-bullet regex matched trailing markers as `[^...]` (any non-`]` run), so prose ending in a footnote-like token (e.g. "footnote syntax is [^note]") was absorbed into the markers group and dropped, breaking the documented `parse(serialize(d)) == d` contract. Because the token label failed to resolve, the text was silently truncated and, once the entry also carried a real ref, its markers were mangled too. serialize() emits integer labels exclusively, so narrow the inline-marker pattern to `[^]`. Non-integer footnote-like tokens now stay in the text where they belong; real numeric citation markers still parse (including the legacy compact `[^1][^2]` form). Co-Authored-By: Claude Opus 4.8 --- deeptutor/services/memory/document.py | 6 +++++- tests/services/memory/test_document.py | 27 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/deeptutor/services/memory/document.py b/deeptutor/services/memory/document.py index f87e7b251..e10b528f0 100644 --- a/deeptutor/services/memory/document.py +++ b/deeptutor/services/memory/document.py @@ -47,8 +47,12 @@ _SECTION_RE = re.compile(r"^##\s+(.+?)\s*$") # Markers are optional (an entry may cite no refs); commas + whitespace # between markers are tolerated so the rendered superscripts read # ``¹, ³`` instead of the visually-merged ``¹³``. +# The label is matched as digits only, because ``serialize`` emits integer +# labels exclusively. A looser ``[^...]`` would also swallow a footnote-like +# token that is part of the entry text (e.g. prose ending in ``[^note]``), +# dropping it on the round-trip. _NEW_BULLET_RE = re.compile( - rf"^\s*-\s+(?P.*?)(?P(?:\s*,?\s*\[\^[^\]]+\])*)\s*\s*$" + rf"^\s*-\s+(?P.*?)(?P(?:\s*,?\s*\[\^\d+\])*)\s*\s*$" ) # Legacy bullet: "- text[^m_xxx]" _OLD_BULLET_RE = re.compile(rf"^\s*-\s+(?P.*?)\[\^(?P{_ENTRY_ID_RE})\]\s*$") diff --git a/tests/services/memory/test_document.py b/tests/services/memory/test_document.py index 9ff4c542c..6bceb3940 100644 --- a/tests/services/memory/test_document.py +++ b/tests/services/memory/test_document.py @@ -195,6 +195,33 @@ def test_new_format_round_trip_is_idempotent() -> None: assert rendered == again +def test_round_trip_preserves_footnote_like_token_in_text() -> None: + """A ``[^token]`` that is part of the entry text (not a real citation + marker) must survive serialize → parse. ``serialize`` only emits integer + labels, so a non-integer footnote-like token belongs to the text.""" + entry_id = "m_01HZK1ABCDEFGHJKMNPQRSTVWX" + # No refs: the whole bullet is text, ending in a footnote-like token. + doc = Document( + title="T", + sections=[("S", [Entry(id=entry_id, section="S", text="footnote syntax is [^note]")])], + ) + reparsed = parse(serialize(doc)).all_entries()[0] + assert reparsed.text == "footnote syntax is [^note]" + assert reparsed.refs == [] + + # With a real citation: the token stays in text, the ref still binds. + entry_id2 = "m_01HZK1ABCDEFGHJKMNPQRSTVWY" + doc2 = Document( + title="T", + sections=[ + ("S", [Entry(id=entry_id2, section="S", text="note syntax [^note]", refs=["chat:x"])]) + ], + ) + reparsed2 = parse(serialize(doc2)).all_entries()[0] + assert reparsed2.text == "note syntax [^note]" + assert reparsed2.refs == ["chat:x"] + + def test_parse_tolerates_both_separator_styles_between_markers() -> None: """Bullets emitted before the separator change had ``[^1][^2]`` — the comma-tolerant parser still binds refs correctly on legacy