Skip to content

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

Closed

Description

@ktz03

Observed Behavior

consolidate_memories accepts a valid JSON response whose memories array contains a string with an ordinary line break and returns {"consolidated_topics": 1}. A fresh MemoryFileStore then returns only the first line of that memory.

For example, the supplied memory is Deploy staging.\nThen deploy production., but a fresh read returns ['Deploy staging.']. The continuation is still present in the raw Markdown file immediately after consolidation; it is omitted by the record parser. This report concerns that parsed-memory discrepancy, not a model-quality result or a general storage-loss claim.

The same string through the public write_memory tool retains both sentences by normalizing whitespace. A single-line consolidation result round-trips normally. The default consolidation prompt asks for strings in a memories array and does not prohibit line breaks.

Expected Behavior

A successful consolidation should not silently omit part of an accepted memory when the file is read again. Either preserve all of the memory text, normalize it to the existing single-line representation, or reject unsupported formatting while preserving the previous record.

Steps to Reproduce

  1. Create a MemoryFileStore and MemoryContextProvider with the deterministic offline client below.
  2. Write a normal memory using the provider's public tool.
  3. Consolidate with one valid JSON memory containing LF or CRLF.
  4. Open a fresh store and compare the memory with the supplied text. The single-line case is a normal control.

Minimal Reproduction

The client returns an actual SDK ChatResponse; there are no private provider calls, network requests or model calls.

import asyncio
import json
import tempfile

from agent_framework import (
    AgentSession, ChatResponse, MemoryContextProvider, MemoryFileStore,
    Message, SessionContext,
)


class OfflineClient:
    def __init__(self, memory):
        self.memory = memory

    async def get_response(self, messages, **kwargs):
        result = {"summary": "Deployment", "memories": [self.memory]}
        return ChatResponse(messages=[Message(role="assistant", contents=[json.dumps(result)])])


async def reproduce(memory):
    with tempfile.TemporaryDirectory() as directory:
        session = AgentSession(session_id="example")
        session.state["owner"] = "example-owner"
        store = MemoryFileStore(directory, owner_state_key="owner")
        provider = MemoryContextProvider(
            store=store, recent_turns=0, max_extractions=0,
            consolidation_client=OfflineClient(memory),
        )
        context = SessionContext(session_id=session.session_id, input_messages=[])
        await provider.before_run(agent=None, session=session, context=context, state={})
        tools = {tool.name: tool for tool in context.tools}
        await tools["write_memory"].invoke(
            arguments={"topic": "Deployment", "memory": "Deploy staging then production."},
            skip_parsing=True,
        )
        response = await tools["consolidate_memories"].invoke(arguments={}, skip_parsing=True)
        fresh = MemoryFileStore(directory, owner_state_key="owner").get_topic(
            session, source_id="memory", topic="Deployment"
        )
        print({"input": memory, "result": response, "reloaded": fresh.memories})


async def main():
    await reproduce("Deploy staging then production.")
    await reproduce("Deploy staging.\nThen deploy production.")
    await reproduce("Deploy staging.\r\nThen deploy production.")


asyncio.run(main())

Observed output, with the experimental warning retained:

single-line: result={"consolidated_topics": 1}; reloaded=['Deploy staging then production.']
LF:          result={"consolidated_topics": 1}; reloaded=['Deploy staging.']
CRLF:        result={"consolidated_topics": 1}; reloaded=['Deploy staging.']

Package Versions

agent-framework-core source at current main 2d9cc3f8ae465baf1aed3022f89b6848db4ffa7b (pyproject.toml version 1.21.0). All 84 checked package Python files match that commit's Git blobs. The cached dependency environment reports Core 1.20.0 metadata; the imported executable source is the verified main source. No fresh workspace-lock installation or full core-suite claim.

Python Version

Python 3.14.3.

Operating System

Windows.

Regression

Unknown.

Additional Context

The original memory harness and Markdown format belong to #5613. Its review already called for heading-marker round-trip preservation; this observation uses an ordinary continuation line and the accepted consolidation response path. #9078/#9079 concern summary text resembling metadata; #9153 and #9205/#9227 concern topic identity; #9218 concerns consolidation scheduling markers. Their existing scope and authorship are preserved.

Public provider/store/tool controls covered single lines, ordinary LF/CRLF continuations, list continuations and heading controls. A separate independent check confirms LF/CRLF omissions and normal single-line, two-item, list and Unicode controls. Matrices overlap and are not added. No product patch, live model, remote store, other operating system or performance validation.

Please confirm the desired single-line normalization or unsupported-format handling before implementation. This report does not propose a new file format or attempt to reconstruct already omitted content.

AI Assistance

AI-assisted analysis, reproduction, and writing.

Acknowledgements

  • I searched existing issues and did not find a duplicate.
  • I personally verified this behavior and the reproduction details are authentic.
  • I will wait for explicit maintainer agreement before starting implementation of a non-trivial change.

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 9, 2026
  2. added
    reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
    on Oct 9, 2026
  3. github-actions commented on Oct 9, 2026

    @github-actions
    Contributor

    🤖 Automated triage reproduction notes (agent-authored — trust but verify)

    Agent analysis

    Repro: python/packages/core/agent_framework/_harness/_memory.py::MemoryTopicRecord.to_markdown/from_markdown loses continuation lines when MemoryContextProvider._consolidate_topic accepts a memory string containing LF or CRLF. Minimal repro: consolidate an existing topic with memories=["Deploy staging.\nThen deploy production."], confirm {"consolidated_topics": 1}, then read it through a fresh MemoryFileStore; only Deploy staging. is returned.

    • Failing test: python/packages/core/tests/core/test_harness_memory.py::test_memory_consolidation_round_trips_multiline_memories
    • Files examined: python/packages/core/agent_framework/_harness/_memory.py, python/packages/core/tests/core/test_harness_memory.py, python/packages/core/pyproject.toml
    • Tests run: test_memory_context_provider_uses_explicit_consolidation_client, test_memory_consolidation_round_trips_multiline_memories
    • Reported version: 1.21.0
    • Current version: 1.21.0
  4. added
    harness[Issues, PRs], Target: harness-level items
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 9, 2026
  5. he-yufeng commented on Oct 9, 2026

    @he-yufeng
    Contributor

    Reproduced on current main (65a12f9, core 1.21.0). The write path already normalizes: _merge_memory runs every memory through _normalize_memory_text, so write_memory never persists a line break. The consolidation path skips that step: _consolidate_topic passes the LLM's memories strings straight into MemoryTopicRecord, to_markdown writes the break verbatim as a non-bullet line, and from_markdown only reads - bullets, so the continuation vanishes on the next load while the raw file still shows it.

    I'd like to take this. Proposed fix: normalize memories in MemoryTopicRecord.__init__ with the same _normalize_memory_text the write path uses (keeping the current drop-empty tolerance), so single-line memories become a record invariant and the markdown round-trip is stable by construction. That covers consolidation, extraction, and any future JSON-sourced path at once, matching the write_memory behavior you noted as the reference. Regression coverage: LF and CRLF consolidation memories read back intact (normalized) through a fresh store, plus a record-level markdown round-trip.

  6. he-yufeng commented on Oct 10, 2026

    @he-yufeng
    Contributor

    This is exactly what #9240 fixes: to_markdown wrote the line break verbatim as a non-bullet line, and the reload path only collected bullet items, so everything after the first break was dropped. The PR makes multi-line memory strings survive the round trip; verified with the same fresh-store reload scenario.

  7. eavanvalkenburg commented on Nov 3, 2026

    @eavanvalkenburg
    Member

    /plan Settle the smallest complete repair for accepted consolidation memory continuation text disappearing on fresh reload, with owner westey-m and existing #9240 rather than competing implementation. Main consolidate_memories accepts LF/CRLF strings and reports success, but the Markdown parser collects only bullet lines; ordinary write_memory already normalizes. This is parsed-memory fidelity, not general deletion or model quality. Current #9240 normalizes every MemoryTopicRecord constructor and the author reports legacy bare-line folding in 51fe190; verify that candidate rather than repeat the older legacy-omission finding as unfixed. Compare narrow producer normalization with a public record-wide single-line invariant: settle whether collapsing all whitespace/deduplicating constructor inputs changes previously supported direct-record values, and how already-written ordinary continuations are handled without swallowing headings/metadata. Coordinate the shared #9727/#9725 escaped-marker/placeholder format plan, but do not assume that distinct plan already approves this constructor/parser policy or introduce a parallel format migration. Cover public deterministic consolidation plus fresh-store LF/CRLF/list/heading/Unicode/empty controls, raw legacy disk input, ordinary single-line/whitespace stability and unchanged write_memory behavior. Estimate executable runtime lines excluding docs/tests; a small diff is not implementation-ready while representation/legacy compatibility remains unresolved. No new ready label or assignment is implied.

  8. eavanvalkenburg commented on Nov 12, 2026

    @eavanvalkenburg
    Member

    /plan Settle the smallest complete repair for accepted consolidation memory continuation text disappearing on fresh reload, with owner westey-m and existing #9240 rather than competing implementation. Main consolidate_memories accepts LF/CRLF strings and reports success, but the Markdown parser collects only bullet lines; ordinary write_memory already normalizes. This is parsed-memory fidelity, not general deletion or model quality. Current #9240 normalizes every MemoryTopicRecord constructor and the author reports legacy bare-line folding in 51fe190; verify that candidate rather than repeat the older legacy-omission finding as unfixed. Compare narrow producer normalization with a public record-wide single-line invariant: settle whether collapsing all whitespace/deduplicating constructor inputs changes previously supported direct-record values, and how already-written ordinary continuations are handled without swallowing headings/metadata. Coordinate the shared #9727/#9725 escaped-marker/placeholder format plan, but do not assume that distinct plan already approves this constructor/parser policy or introduce a parallel format migration. Cover public deterministic consolidation plus fresh-store LF/CRLF/list/heading/Unicode/empty controls, raw legacy disk input, ordinary single-line/whitespace stability and unchanged write_memory behavior. Estimate executable runtime lines excluding docs/tests; a small diff is not implementation-ready while representation/legacy compatibility remains unresolved. No new ready label or assignment is implied.

  9. github-actions commented on Nov 12, 2026

    @github-actions
    Contributor

    Summary

    consolidate_memories accepts a model-supplied memory string that contains a line break and reports success. The file writer then emits the break as a plain line. The file reader only keeps lines that start with - , so the continuation is lost on the next fresh load. Ordinary write_memory does not have this problem because it collapses whitespace first. This is about fidelity of parsed memories, not general deletion or model quality.

    Evidence (current main, read-only):

    • _normalize_memory_text collapses all whitespace and rejects empty text. It is called only from _merge_memory (the write_memory path). _memory.py L105
    • _consolidate_topic passes model strings straight into MemoryTopicRecord(...). L1707-L1790
    • The MemoryTopicRecord constructor runs _dedupe_strings, which only strip()s the ends and dedupes by casefold(). It does not touch inner newlines. L138
    • from_markdown appends a memory only for - lines in the Memories section. Any other line there is silently ignored. L420-L480
    • The summary is already collapsed to one line by _coerce_summary, so only memories are affected.

    What I could not check

    I did not read the open PR #9240 or commit 51fe190b9, per this workflow's rules. I therefore cannot confirm that the candidate's legacy folding works. That has to be verified by running it against the test list below. I could not find the #9727/#9725 plans through search, so I did not rely on them.

    Recommendation

    Code change, small, in _memory.py. The bug is real and confined to one module. There is no workaround except asking the model for single-line strings, and the default prompt does not forbid line breaks.

    Producer-only vs record-wide. Normalizing only in _consolidate_topic fixes the reported path. It leaves from_dict, extraction and any direct MemoryTopicRecord(memories=["a\nb"]) writing a file that cannot be read back. A record-wide rule closes all of those.

    Settled constraint: the record-wide rule should NOT reuse _normalize_memory_text (collapse all whitespace). That would change values that round-trip today. For example "a b" or a tab-separated memory is stored and reloaded unchanged now. Collapsing also makes "a b" and "a b" count as duplicates, which they are not today. Memories containing a line break never round-tripped, so changing them is not a regression.

    Preferred invariant: in the constructor only, replace line breaks with a single space. Use splitlines() (the same splitter from_markdown uses, so it covers \r, \x85, \u2028 and similar), strip each piece, drop empty pieces, and join with a space. Keep inner spacing and the existing empty-drop and casefold-dedupe behavior. Single-line, whitespace and Unicode values stay identical. write_memory is unchanged.

    Legacy files (already-written continuations). Normalization cannot repair files that already have bare continuation lines. A small reader change can: in the Memories section, a non-empty line that is not - -prefixed folds into the previous memory with a space. Limits needed so it does not swallow structure:

    Runtime size estimate (no tests/docs): about 5-8 lines for the constructor line-break join, plus about 6-10 for the reader fold. Roughly 12-18 lines total. The constructor part alone is about 4-6. Because the legacy fold changes how existing files are read, I would settle the points above before calling it ready. I am not applying any label or assignment.

    Proposed plan (proposal only; nothing has been changed)

    1. Add a private helper in _memory.py that joins splitlines() pieces with a space, drops empties, and leaves other whitespace alone. Use it in _dedupe_strings for memories only (not session ids), or in the MemoryTopicRecord constructor before dedupe.

    2. Update from_markdown to fold bare continuation lines into the previous bullet, with the limits above.

    3. Leave _merge_memory, _normalize_memory_text, to_markdown escaping, topic and summary handling unchanged.

    4. Tests in test_harness_memory.py:

      • Public deterministic consolidation with LF, CRLF, \r, list-marker (- x\n- y), heading-like (ok\n# Title, ## Memories) and Unicode (\u2028, non-ASCII) input. Read back through a fresh MemoryFileStore. Expect one normalized line each.
      • Empty and whitespace-only entries are dropped as before. An all-empty list gives the placeholder.
      • Single-line memories, memories with double spaces or tabs, and casefold duplicates read back exactly as before.
      • Raw legacy file with continuation lines, then a heading, Updated:, a blank line, and a stray line before the first bullet. Check which fold and which do not.
      • write_memory output is byte-identical to today.
    5. Success means the issue's LF/CRLF repro reloads as Deploy staging. Then deploy production., the control cases are unchanged, and Python: fix(core): stop losing multiline consolidated memories on reload #9240's behavior matches or the differences are noted on that PR.

    Questions for the owner (westey (@westey-m))

    1. Is "line breaks become a space, other whitespace kept" acceptable for the constructor, or do you prefer exact write_memory parity (collapse everything)? The latter changes today's round-trip for inner double spaces.
    2. Is folding legacy bare lines into the previous bullet acceptable, given the ambiguous - -leading continuation limit?

    Generated by Issue Planning for #9228 · copilot · auto · ~39 AIC · ⌖ ~1 AIC · ⊞ 4.8K · ◷

  10. westey-m commented on Nov 12, 2026

    @westey-m
    Collaborator

    This feature is being deprecated in favor of FileMemoryProvider. See #10273.
    Therefore closing this issue as won't fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

harness[Issues, PRs], Target: harness-level itemspythonUsage: [Issues, PRs], Target: Pythonready-for-implementationMaintainers agree the problem and direction are ready for implementation.reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions