fix(mneme): auto-migrate legacy MD5 narrative files to SHA-256 paths (#128)#161
Open
tcconnally wants to merge 1 commit into
Open
fix(mneme): auto-migrate legacy MD5 narrative files to SHA-256 paths (#128)#161tcconnally wants to merge 1 commit into
tcconnally wants to merge 1 commit into
Conversation
…128) Pre-1.0.3, Mnēmē derived per-workspace narrative file names from an MD5 hash of the canonicalized workspace path. v1.0.3 switched to SHA-256 without any migration path. On upgrade, every existing narrative file on disk was silently orphaned: `_mneme_path()` returned a path that didn't exist, Mnēmē reported "No narrative found for this workspace", and started fresh. The old MD5 files sat on disk untouched (preserved, but unreachable through any documented command). This patch makes the upgrade lossless and gives operators a manual recovery tool for edge cases. Changes: src/perseus/mneme_narrative.py: - New _workspace_hash_legacy_md5(): reproduces the pre-1.0.3 hash exactly. Uses hashlib.md5(canonical, usedforsecurity=False) so FIPS-mode Pythons don't reject it (it's a file-naming hash, not a security primitive). Falls back to no-kwarg call on Python < 3.9. - _mneme_path() now performs a one-shot in-place migration: if the SHA-256 path doesn't exist but the legacy MD5 path does, os.replace atomically renames it. Idempotent. If both paths exist (race or operator staging), SHA-256 wins and legacy file is left untouched. If the rename fails (cross-device, permission), both files are preserved and the caller creates a fresh narrative at the SHA-256 path (non-fatal). - New _mneme_doctor_scan(): classifies every *.md in the memory store as sha256, legacy_md5, orphan (frontmatter workspace doesn't match filename), or unknown (non-hex stem). Returns a structured dict. - New _mneme_doctor_migrate(): walks scan output and renames every legacy MD5 file. Returns a report of migrated/skipped/errors tuples. src/perseus/agora.py: - New cmd_memory_doctor handler. Plain-text or JSON output. Read-only scan by default; `--migrate` flag performs the renames. src/perseus/cli.py: - Register `perseus memory doctor` subcommand with `--migrate` and `--json`. Tests (tests/test_mneme.py): - test_mneme_path_auto_migrates_legacy_md5_file - test_mneme_path_no_migration_when_sha256_already_exists - test_mneme_path_is_idempotent_after_migration - test_memory_doctor_scan_classifies_files (4 file types) - test_memory_doctor_migrate_renames_legacy_files (idempotent check) - test_memory_doctor_migrate_skips_when_destination_exists All 6 new regression tests pass. All 19 mneme tests pass. CLI help confirmed: `perseus memory doctor --help` works end-to-end. Closes #128 Refs milestone v1.0.6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #128 — most complex of the v1.0.6 fixes (data migration + new CLI command + 6 regression tests).
Pre-1.0.3, Mnēmē derived per-workspace narrative file names from an MD5 hash of the canonicalized workspace path. v1.0.3 switched to SHA-256 without any migration path. On upgrade, every existing narrative file was silently orphaned:
_mneme_path()returned a path that didn't exist, Mnēmē reported "No narrative found for this workspace", and started fresh. The old MD5 files sat on disk untouched — preserved, but unreachable through any documented command.Fix
Lossless auto-migration on first access
_mneme_path()performs a one-shot in-place rename: if the SHA-256 path doesn't exist but the legacy MD5 path does,os.replaceatomically renames it. Idempotent. If both paths exist (race or operator staging), SHA-256 wins and the legacy file is left untouched (no data destruction). If rename fails (cross-device, permission), both files are preserved and the caller creates a fresh narrative at the SHA-256 path (non-fatal degradation).New CLI:
perseus memory doctorFor operators who want to migrate all workspaces in one pass or audit the memory store explicitly. Flags:
--migrate(perform renames),--json(machine-readable output).New public helpers (importable from
perseus.py)_workspace_hash_legacy_md5(workspace)— reproduces pre-1.0.3 hash exactly_mneme_doctor_scan(cfg)— classifies every*.mdin store as sha256/legacy_md5/orphan/unknown_mneme_doctor_migrate(cfg)— bulk rename with structured reportFiles Changed (6)
src/perseus/mneme_narrative.py— legacy hash + auto-migrate + doctor helperssrc/perseus/agora.py—cmd_memory_doctorhandler + subcommand dispatchsrc/perseus/cli.py— argparse registration ofmemory doctortests/test_mneme.py— 6 regression testsCHANGELOG.md— v1.0.6 entryperseus.py— rebuilt artifactTests
6 new regression tests in
tests/test_mneme.py:test_mneme_path_auto_migrates_legacy_md5_file— happy pathtest_mneme_path_no_migration_when_sha256_already_exists— no overwritetest_mneme_path_is_idempotent_after_migration— double-call safetest_memory_doctor_scan_classifies_files— 4-way classificationtest_memory_doctor_migrate_renames_legacy_files— bulk migrate + idempotenttest_memory_doctor_migrate_skips_when_destination_exists— no clobberTest results
test_mneme.pypassperseus memory doctor --helpworks end-to-endMigration Notes
No manual action required. Migration happens automatically on first access for any operation that calls
_mneme_path()(essentially all memory operations: update, compact, show, status, query, federation).Operators with many workspaces can opt to run
perseus memory doctor --migrateonce after upgrading to surface and fix every workspace in one pass — useful for ops automation or audit.CHANGELOG Note
This PR's CHANGELOG entry adds a
[1.0.6] — UNRELEASEDblock; PRs #159 and #160 also add 1.0.6 entries. The merger should reconcile (keep all bullets, single header).Third of 12 PRs in the v1.0.6 milestone. Suggested next: #131 (memory compact hang — second-most complex remaining).