Visitar URL original
Python: fix(core): stop losing multiline consolidated memories on reload by he-yufeng · Pull Request #9240 · microsoft/agent-framework · GitHub
Skip to content

Python: fix(core): stop losing multiline consolidated memories on reload - #9240

Closed
Yufeng He (he-yufeng) wants to merge 7 commits into
microsoft:mainfrom
he-yufeng:fix/memory-consolidation-multiline-roundtrip
Closed

Yufeng He (he-yufeng) wants to merge 7 commits into
microsoft:mainfrom
he-yufeng:fix/memory-consolidation-multiline-roundtrip

Conversation

@he-yufeng

@he-yufeng Yufeng He (he-yufeng) commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

consolidate_memories accepts a valid JSON response whose memories array contains a string with an ordinary line break and reports success, but a fresh MemoryFileStore reads back only the first line: to_markdown writes the break verbatim as a non-bullet line in the Memories section, and from_markdown only reads - bullets, so the continuation silently disappears on reload while the raw file still shows it. The write path does not hit this because _merge_memory normalizes every memory through _normalize_memory_text before it reaches the record.

Description & Review Guide

  • What are the major changes? MemoryTopicRecord.__init__ now runs each memory through _normalize_memory_text, the same normalizer the write path uses, while keeping the existing drop-empty tolerance. Single-line memories become a record invariant, so the markdown round-trip is stable no matter which path supplied the text.
  • What is the impact of these changes? Consolidation, extraction, and any JSON-sourced record construction now behave like write_memory: line breaks fold into spaces instead of truncating on the next read. The on-disk format is unchanged, and existing single-line records are byte-identical after a rewrite.
  • What do you want reviewers to focus on? The decision to normalize at record construction rather than only inside _consolidate_topic, so every current and future intake path inherits the invariant.

Related Issue

Fixes #9228

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: implementation, tests, and this PR description. The regression tests listed in the checks were actually run locally and pass.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

The write path normalizes every memory through _normalize_memory_text
before it reaches a record, but _consolidate_topic hands the LLM's
memories strings to MemoryTopicRecord raw. A consolidated memory with
a line break is written by to_markdown as a bullet whose continuation
lands on a non-bullet line, and from_markdown only reads "- " bullets,
so a fresh store silently returns just the first line while the raw
file still shows the rest.

MemoryTopicRecord now normalizes each memory at construction, keeping
the existing drop-empty tolerance, so single-line memories are a record
invariant and the markdown round-trip is stable no matter which path
supplied the text.

Verified with the full agent-framework-core suite: 8536 tests, 0
failures; ruff and pyright clean on the changed files.

Signed-off-by: Yufeng He <40085740+he-yufeng@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 13:17
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused normalization fix addresses the reported data loss and is covered at both record and consolidation levels.

0 open findings

What changed in this PR

Normalizes memory text at record construction to prevent multiline consolidated memories from being truncated after Markdown reload.

Changes:

  • Enforces single-line, deduplicated memory records.
  • Adds unit and consolidation round-trip coverage for LF and CRLF content.
File Description
python/​packages/​core/​agent_framework/​_harness/​_memory.py Normalizes memories during record initialization.
python/​packages/​core/​tests/​core/​test_harness_memory.py Tests normalization and fresh-store reload behavior.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@eavanvalkenburg

Copy link
Copy Markdown
Member

Yufeng He (@he-yufeng) Thanks for the contribution. The current PR template's required AI Assistance field is unanswered. Please update the PR body to state whether material AI assistance was used and, if so, identify the assisted areas.

The constructor normalization only covers memories constructed after the
fix; a pre-normalization file still loses its continuation on load
because the memories section ignores bare lines. Fold a non-empty bare
line back into the bullet it continues and cover the legacy shape,
round-trip included.
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Eduard van Valkenburg (@eavanvalkenburg) done, the body now uses the current template and the AI Assistance field is filled in (assisted areas: implementation, tests, PR description).

def _normalize_memory_text(memory: str) -> str:
normalized = " ".join(memory.strip().split())
"""Fold line breaks without changing meaningful spaces or tabs within a line."""
normalized = re.sub(r"\s*(?:\r\n|\r|\n)\s*", " ", memory.strip())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yufeng He (@he-yufeng), this folds only CR/LF separators, but from_markdown uses splitlines(), which also splits on U+2028, U+2029, NEL, vertical tab, form feed, and related separators. Those values can still turn one memory into a heading, bullet, or summary fragment on reload. Please normalize every separator recognized by splitlines() while preserving intended inline spaces, with representative round-trip regressions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 91ef97447.

Normalization now covers every separator recognized by str.splitlines(): CR/LF variants, VT, FF, the three record separators, NEL, U+2028, and U+2029. Inline spaces and tabs remain unchanged.

The parameterized regression covers all eleven spellings. The eight non-CR/LF cases failed before this change and all eleven now pass, including Markdown round trips. The complete memory test file and scoped pre-commit checks pass.

@westey-m

Copy link
Copy Markdown
Collaborator

This feature is being deprecated in favor of FileMemoryProvider. See #10273.
Therefore closing this pr.

This branch was successfully deployed

1 active deployment
github-app-auth — 91ef9744 Deployed Nov 10, 2026 by he-yufeng via add_label #27547
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Consolidated memory continuation lines disappear on reload

4 participants