Skip to content
25 changes: 19 additions & 6 deletions python/packages/core/agent_framework/_harness/_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ def _normalize_topic(topic: str) -> str:
return normalized


_SPLITLINES_BREAK_RE = re.compile(r"\s*(?:\r\n|[\n\r\v\f\x1c-\x1e\x85\u2028\u2029])\s*")


def _normalize_memory_text(memory: str) -> str:
normalized = " ".join(memory.strip().split())
"""Fold every splitlines separator without changing inline spaces or tabs."""
normalized = _SPLITLINES_BREAK_RE.sub(" ", memory.strip())
if not normalized:
raise ValueError("memory must not be empty.")
return normalized
Expand Down Expand Up @@ -387,7 +391,10 @@ def __init__(
normalized_topic = _normalize_topic(topic)
self.topic = normalized_topic
self.slug = _slugify_topic(slug or normalized_topic)
self.memories = _dedupe_strings(memories)
# Collapse line breaks up front: the markdown format stores one bullet per line,
# and the consolidation path hands LLM-supplied strings in raw, so a memory kept
# multiline here would write fine but lose its continuation on the next read.
self.memories = _dedupe_strings([_normalize_memory_text(memory) for memory in memories if memory.strip()])
Comment thread
he-yufeng marked this conversation as resolved.
Resolved
Comment thread
he-yufeng marked this conversation as resolved.
Resolved
self.summary = _coerce_summary(summary, self.memories)
self.updated_at = updated_at
self.session_ids = _dedupe_strings(session_ids or [])
Expand Down Expand Up @@ -532,10 +539,16 @@ def from_markdown(cls, markdown: str, *, fallback_topic: str | None = None) -> M
if stripped:
summary_lines.append(_unescape_markdown_line(stripped))
continue
if current_section == "memories" and stripped.startswith("- "):
memory_text = _unescape_markdown_line(stripped[2:].strip())
if memory_text and memory_text != DEFAULT_MEMORY_NO_TOPICS_TEXT[2:]:
memories.append(memory_text)
if current_section == "memories":
if stripped.startswith("- "):
memory_text = _unescape_markdown_line(stripped[2:].strip())
if memory_text and memory_text != DEFAULT_MEMORY_NO_TOPICS_TEXT[2:]:
memories.append(memory_text)
elif stripped and memories and not stripped.startswith("#"):
# Files written before multiline normalization carry a
# continuation line as bare text; fold it back into the
# bullet it continues instead of dropping it.
memories[-1] = f"{memories[-1]} {_unescape_markdown_line(stripped)}"

if topic is None:
raise ValueError("Memory topic markdown is missing a '# <topic>' heading.")
Expand Down
116 changes: 110 additions & 6 deletions python/packages/core/tests/core/test_harness_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,58 @@ def test_memory_topic_record_round_trips_through_dict_and_markdown() -> None:
assert "MemoryTopicRecord(" in repr(record)


def test_memory_topic_record_normalizes_multiline_memories() -> None:
"""Record memories collapse line breaks so the markdown round-trip stays stable."""
record = MemoryTopicRecord(
topic="deployment",
summary="Deployment",
memories=["Deploy staging.\nThen deploy production.", " \n "],
updated_at="2026-04-21T10:05:00+00:00",
)

assert record.memories == ["Deploy staging. Then deploy production."]
assert MemoryTopicRecord.from_markdown(record.to_markdown()) == record


@pytest.mark.parametrize(
"separator",
["\n", "\r", "\r\n", "\v", "\f", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"],
)
def test_memory_topic_record_folds_every_splitlines_separator(separator: str) -> None:
"""No separator recognized by splitlines can create Markdown structure on reload."""
record = MemoryTopicRecord(
topic="deployment",
summary="Deployment",
memories=[
f"Deploy staging.{separator}Then deploy production.",
'the literal "a b"',
"keep\ttabs",
],
updated_at="2026-04-21T10:05:00+00:00",
)

assert record.memories == [
"Deploy staging. Then deploy production.",
'the literal "a b"',
"keep\ttabs",
]
assert MemoryTopicRecord.from_markdown(record.to_markdown()) == record


def test_memory_topic_record_preserves_single_line_whitespace_on_reload() -> None:
"""Single-line spaces and tabs are content, and distinct values must not deduplicate."""
memories = ['the literal "a b"', 'the literal "a b"', "keep\ttabs"]
record = MemoryTopicRecord(
topic="compatibility",
summary="Compatibility",
memories=memories,
updated_at="2026-04-21T10:05:00+00:00",
)

assert record.memories == memories
assert MemoryTopicRecord.from_markdown(record.to_markdown()).memories == memories


@pytest.mark.parametrize(
"summary",
[
Expand Down Expand Up @@ -189,6 +241,25 @@ def test_memory_topic_record_preserves_multiline_summary_metadata_text(summary_l
assert record.memories == ["Updated: the milestone moved.", "Sessions: retain previous notes."]


def test_memory_topic_record_folds_legacy_continuation_lines() -> None:
"""A bare continuation line in a pre-normalization file rejoins the bullet it continues."""
markdown = (
"# deploy\n\n"
"Updated: 2026-04-21T10:05:00+00:00\n"
"Sessions: -\n\n"
"## Summary\n\n"
"## Memories\n"
"- Deploy staging.\n"
"Then deploy production.\n"
"- Rollback plan ready.\n"
)

record = MemoryTopicRecord.from_markdown(markdown)

assert record.memories == ["Deploy staging. Then deploy production.", "Rollback plan ready."]
assert MemoryTopicRecord.from_markdown(record.to_markdown()).memories == record.memories


async def test_memory_file_store_writes_topics_index_state_and_transcripts(tmp_path) -> None:
"""The file-backed memory store should manage topics, ``MEMORY.md``, state, and transcript search."""
store = MemoryFileStore(
Expand Down Expand Up @@ -439,12 +510,8 @@ async def test_search_transcripts_non_positive_limit_returns_nothing(tmp_path) -
store = MemoryFileStore(tmp_path, owner_state_key="owner_id")
await _write_transcript(store, session, "session-1", "Matching transcript.")

assert store.search_transcripts(
session, source_id=DEFAULT_MEMORY_SOURCE_ID, query="transcript", limit=0
) == []
assert store.search_transcripts(
session, source_id=DEFAULT_MEMORY_SOURCE_ID, query="transcript", limit=-1
) == []
assert store.search_transcripts(session, source_id=DEFAULT_MEMORY_SOURCE_ID, query="transcript", limit=0) == []
assert store.search_transcripts(session, source_id=DEFAULT_MEMORY_SOURCE_ID, query="transcript", limit=-1) == []


def test_memory_file_store_rejects_owner_path_traversal(tmp_path) -> None:
Expand Down Expand Up @@ -1512,6 +1579,43 @@ async def test_memory_consolidation_transient_failure_preserves_state(tmp_path)
assert surviving.summary == "Prefers concise answers."


async def test_memory_consolidation_round_trips_multiline_memories(tmp_path) -> None:
"""A consolidated memory containing line breaks must survive a fresh-store reload."""
session = AgentSession(session_id="session-1")
session.state["owner_id"] = "alice"
store = MemoryFileStore(tmp_path, owner_state_key="owner_id")
multiline_client = _MemoryHarnessClient(
consolidation_payload={
"summary": "Deployment",
"memories": ["Deploy staging.\nThen deploy production.", "Roll back\r\nif health checks fail."],
}
)
provider = MemoryContextProvider(store=store, consolidation_client=multiline_client) # type: ignore[arg-type] # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type]
store.write_topic(
session,
MemoryTopicRecord(
topic="deployment",
summary="Deployment",
memories=["Deploy staging."],
updated_at="2026-04-21T10:00:00+00:00",
session_ids=["session-1"],
),
source_id=DEFAULT_MEMORY_SOURCE_ID,
)

consolidated_count = await provider._run_consolidation( # pyright: ignore[reportPrivateUsage]
client=multiline_client, # type: ignore[arg-type] # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type]
session=session,
force=True,
now=datetime(2026, 4, 22, tzinfo=timezone.utc),
)

assert consolidated_count == 1
fresh_store = MemoryFileStore(tmp_path, owner_state_key="owner_id")
reloaded = fresh_store.get_topic(session, source_id=DEFAULT_MEMORY_SOURCE_ID, topic="deployment")
assert reloaded.memories == ["Deploy staging. Then deploy production.", "Roll back if health checks fail."]


async def test_memory_extraction_propagates_programmer_errors(tmp_path) -> None:
"""Non-transient errors from the chat client must surface so misconfigurations fail loudly."""
session = AgentSession(session_id="session-1")
Expand Down
Loading