Skip to content

Python: fix(core): quarantine an unreadable memory state file instead of failing every turn - #9547

Open
Yufeng He (he-yufeng) wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/memory-store-corrupt-state
Open

Yufeng He (he-yufeng) wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/memory-store-corrupt-state

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Summary

MemoryFileStore.read_state parsed state.json with no guard, so a corrupt state file (interrupted editor, partial external write, hand edit) raised JSONDecodeError out of the per-turn memory bookkeeping path — and kept raising on every later turn, since nothing ever removed the bad file.

This follows the session store's existing corrupt-snapshot convention (_quarantine_corrupt_snapshot in _sessions.py): an unreadable state file is moved aside to .<name>.<uuid>.corrupt, a warning is logged, and the store restarts from a clean default state. The next write_state recreates a clean file. The deliberate ValueError for a well-formed non-object document is unchanged.

Tests

  • New test_memory_file_store_quarantines_corrupt_state_file: writes state through the real path, corrupts the file, asserts a clean default comes back, the corrupt file was moved aside with its content preserved, and a later write/read round-trips normally.
  • Mutation check: dropping the guard fails the test (JSONDecodeError propagates); restored green.
  • pytest tests/core/test_harness_memory.py: 34 passed.
  • pyright agent_framework/_harness/_memory.py and the test file: 0 errors.
  • Verified against the worktree checkout (PYTHONPATH pinning confirmed by agent_framework.__file__), not the shared editable install.

… of failing every turn

MemoryFileStore.read_state parsed state.json without a guard, so a
corrupt state file (interrupted editor, partial external write) raised
out of the per-turn memory bookkeeping path forever. Move the unreadable
file aside (the session store's corrupt-snapshot convention) and restart
from a clean state; the next write recreates the file.

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.

🟡 Changes recommended

Quarantine can move aside a concurrently repaired file and reset valid maintenance metadata.

1 open issue
What changed in this PR

Improves Python memory-store recovery so corrupt state files do not repeatedly fail per-turn bookkeeping.

Changes:

  • Quarantines unreadable state files, logs a warning, and returns defaults.
  • Adds regression coverage for quarantine, content preservation, and subsequent writes.
File Description
python/​packages/​core/​tests/​core/​test_harness_memory.py Tests corrupt-state recovery and later write/read behavior.
python/​packages/​core/​agent_framework/​_harness/​_memory.py Adds quarantine and default-state recovery.

Review effort: Balanced

except ValueError as exc:
quarantine_path = state_path.with_name(f".{state_path.name}.{uuid.uuid4().hex}.corrupt")
try:
state_path.rename(quarantine_path)

This branch was successfully deployed

1 active deployment
github-app-auth — ade245fa Deployed Oct 19, 2026 by he-yufeng via team_check #7085
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.

2 participants