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 `[^<digits>]`. 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 <noreply@anthropic.com>
This commit is contained in:
Rudy Celekli
2026-11-01 09:13:39 -05:00
parent f7aed0744c
commit 6498bc7fbc
2 changed files with 32 additions and 1 deletions

View File

@@ -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<text>.*?)(?P<markers>(?:\s*,?\s*\[\^[^\]]+\])*)\s*<!--\s*(?P<id>{_ENTRY_ID_RE})\s*-->\s*$"
rf"^\s*-\s+(?P<text>.*?)(?P<markers>(?:\s*,?\s*\[\^\d+\])*)\s*<!--\s*(?P<id>{_ENTRY_ID_RE})\s*-->\s*$"
)
# Legacy bullet: "- text[^m_xxx]"
_OLD_BULLET_RE = re.compile(rf"^\s*-\s+(?P<text>.*?)\[\^(?P<id>{_ENTRY_ID_RE})\]\s*$")

View File

@@ -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