Visitar URL original
Python: [Bug]: An unreadable memory state file fails every later turn · Issue #9997 · microsoft/agent-framework · GitHub
Skip to content

Python: [Bug]: An unreadable memory state file fails every later turn #9997

Closed

Description

@he-yufeng

Observed Behavior

MemoryFileStore.read_state parses the owner's state.json on every memory bookkeeping path without handling malformed JSON or non-UTF-8 bytes. If an interrupted editor, partial external write, or manual edit leaves the file unreadable, read_state raises on that turn and every later turn because the same file remains in place.

This is different from a well-formed JSON document with the wrong top-level type. That case already raises a deliberate ValueError and is not the subject of this report.

Expected Behavior

An unreadable maintenance-state file should not permanently disable memory bookkeeping. The recovery policy should preserve the original bytes for diagnosis and either quarantine the bad file and restart with clean maintenance state, or expose another explicit recoverable path chosen by the maintainers.

The session store already uses a corrupt-snapshot quarantine convention. Applying the same convention here would keep recovery behavior consistent, but I would like confirmation that silently resetting consolidation bookkeeping is acceptable for memory state.

Steps to Reproduce

  1. Create a MemoryFileStore and write valid state for an owned session.
  2. Replace its state.json with malformed JSON such as {not json.
  3. Call read_state twice for the same session and source.

Minimal Reproduction

No model or network call is involved.

import tempfile
from pathlib import Path

from agent_framework import AgentSession, MemoryFileStore
from agent_framework._harness._memory import DEFAULT_MEMORY_SOURCE_ID

with tempfile.TemporaryDirectory() as directory:
    store = MemoryFileStore(Path(directory), owner_state_key="owner_id")
    session = AgentSession(session_id="session-1")
    session.state["owner_id"] = "alice"
    store.write_state(
        session,
        {"sessions_since_consolidation": ["session-1"]},
        source_id=DEFAULT_MEMORY_SOURCE_ID,
    )
    state_path = store._get_state_path(session, source_id=DEFAULT_MEMORY_SOURCE_ID)
    state_path.write_text("{not json", encoding="utf-8")

    store.read_state(session, source_id=DEFAULT_MEMORY_SOURCE_ID)
    store.read_state(session, source_id=DEFAULT_MEMORY_SOURCE_ID)

Both reads raise json.JSONDecodeError. A non-UTF-8 payload similarly raises UnicodeDecodeError on every read.

Package Versions

Reproduced against the core source used for #9547 and rechecked against the current implementation shape on main. No live model, remote store, or external service is required.

Python Version

Python 3.14.2.

Operating System

macOS.

Regression

Unknown.

Additional Context

Candidate PR #9547 already contains an implementation and regression coverage from earlier work. It quarantines malformed JSON and non-UTF-8 state beside the original, returns clean maintenance state, and rechecks the bytes before renaming so a valid replacement landing during the read is not quarantined. The deliberate non-object ValueError remains unchanged.

No further implementation is planned until the desired recovery policy is agreed. If preserving the bad file but failing closed is preferred over resetting maintenance state, the candidate should be adjusted rather than merged as written.

AI Assistance

AI-assisted analysis, reproduction, implementation, and writing. The candidate diff and tests were reviewed and run locally.

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.

The implementation in #9547 predates this tracking issue. Further work is paused pending maintainer agreement on the recovery direction.

Activity

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

    @github-actions
    Contributor

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

    Agent analysis

    MemoryFileStore.read_state in python/packages/core/agent_framework/_harness/_memory.py:888-901 persistently fails when state.json contains malformed JSON or non-UTF-8 bytes; line 893 propagates the decoder exception and leaves the file unchanged. This affects MemoryContextProvider.after_run and _run_consolidation. A minimal repro writes valid state, overwrites _get_state_path(...) with b"{not json" or b"\xff", and calls read_state twice; both calls raise the same decoder exception.

    • Failing test: python/packages/core/tests/core/test_harness_memory.py::test_memory_file_store_unreadable_state_does_not_fail_every_read
    • Files examined: python/packages/core/agent_framework/_harness/_memory.py, python/packages/core/agent_framework/_sessions.py, python/packages/core/tests/core/test_harness_memory.py, python/packages/core/pyproject.toml
    • Tests run: valid-state baseline test, unreadable-state regression test
    • Current version: 1.24.0
  4. added
    harness[Issues, PRs], Target: harness-level items
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Nov 3, 2026
  5. 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: PythonreproducedUsage: [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