Skip to content

fix(context): preserve ## <Agent>-Specific sections through round-trip#482

Merged
memtomem merged 1 commit intomainfrom
fix/specific-section-roundtrip
Apr 26, 2026
Merged

fix(context): preserve ## <Agent>-Specific sections through round-trip#482
memtomem merged 1 commit intomainfrom
fix/specific-section-roundtrip

Conversation

@pandas-studio
Copy link
Copy Markdown
Collaborator

Summary

mm context initmm context generate round-trip silently dropped ## <Agent>-Specific override sections, and mm context diff reported [in sync] because both sides had the same loss — masking the data loss. This fixes the round-trip and adds a regression test.

Root cause

extract_sections_from_agent_file maps headings to canonical section keys via the aliases dict. The five ## <Agent>-Specific headings (Claude / Cursor / Gemini / Codex / Copilot) had no entries in that dict, so they were stored under their literal heading strings (Claude-Specific, …). Each generator then checks for the canonical key (if "Claude" in sections: …), which was never populated, so the override section was discarded on regeneration.

mm context diff compared the regenerated output against the just-regenerated file (both written by the same code path), so the diff was [in sync] even though both sides were missing the override content.

Fix

  1. Add five aliases (claude-specificClaude, etc.) to extract_sections_from_agent_file's alias map so each override heading resolves to the canonical key the generators look for.
  2. CopilotGenerator was additionally asymmetric: it emitted the override content without any ## heading, so a second round-trip extracted it as part of the preceding ## Commands section. Fixed to write ## Copilot-Specific like the other four generators.

Test plan

  • uv run pytest packages/memtomem/tests/test_context.py — 23 passed (5 new parametrized round-trip cases)
  • uv run pytest -m "not ollama" — 2481 passed
  • uv run ruff check packages/memtomem/src packages/memtomem/tests — clean
  • uv run ruff format --check … — clean
  • End-to-end repro: write CLAUDE.md containing ## Claude-Specific, run extract_sections_from_agent_file + generate_for_agent("claude", …), confirm content survives. (Before this PR: dropped. After: preserved.)

🤖 Generated with Claude Code

`extract_sections_from_agent_file` mapped each agent-override heading
(`## Claude-Specific`, `## Cursor-Specific`, …) to its literal heading
string, but the generators look for canonical keys (`Claude`, `Cursor`,
…). On `mm context init` (extract-existing) followed by `mm context
generate`, the override section was silently dropped and `mm context
diff` reported `[in sync]` because both sides had the same loss —
masking the data loss from the user.

Add the five aliases to the reverse-extract map so override sections
survive the round-trip. Also fix `CopilotGenerator`, which emitted the
override content without any `##` heading; a second round-trip absorbed
it into the preceding section. It now writes `## Copilot-Specific` like
the other four generators.

Regression test parametrizes all five agents; they all assert that a
`## <Agent>-Specific` heading + body survive `extract_sections →
generate_for_agent`.

Co-Authored-By: Claude <[email protected]>
@memtomem memtomem merged commit 77c5bd2 into main Apr 26, 2026
7 checks passed
@memtomem memtomem deleted the fix/specific-section-roundtrip branch April 26, 2026 02:14
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants