fix(mcp): delete_episode cascade to extracted edges and orphan entities#1491
Open
brentkearney wants to merge 1 commit into
Open
fix(mcp): delete_episode cascade to extracted edges and orphan entities#1491brentkearney wants to merge 1 commit into
brentkearney wants to merge 1 commit into
Conversation
…ntities delete_episode was calling EpisodicNode.delete, which drops only the episode node — leaving behind every entity edge sourced from it and every entity node that the episode mentioned. The result: edges with stale episode_uuid references in their .episodes list, and orphan Entity nodes with no remaining provenance. Graphiti exposes Graphiti.remove_episode for exactly this case: it deletes the episode, the edges where this episode is the first provenance entry, and any Entity nodes that were mentioned only by the deleted episode. Needed before re-ingesting historical episodes whose extracted edges landed with hallucinated valid_at timestamps — without the cascade, re-ingest would orphan the old edges instead of replacing them. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This was referenced May 15, 2026
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.
Resolves part of #1489 (Gap 2).
Summary
The MCP
delete_episodetool currently callsEpisodicNode.delete(client.driver), which drops only theEpisodicnode. TheRELATES_TOedges extracted from the episode remain in the graph with staleepisodesarrays pointing at the now-deleted UUID, andEntitynodes that were mentioned only by the deleted episode are left behind as orphans.graphiti_core.Graphiti.remove_episodealready exists and does the right thing — it deletes the episode, the edges where this episode is the first provenance entry, and any entity nodes mentioned only by the deleted episode. This PR wires the MCP tool through to it.Why this matters
Without the cascade,
delete_episodecorrupts the graph silently:episodesarrays referencing nonexistent UUIDs.Changes
Single-file, three-line behavioral change:
The top-level
EpisodicNodeimport becomes unused after the swap; the in-functionfrom graphiti_core.nodes import EpisodicNodeat theget_episodessite (around line 647) is preserved and remains the only EpisodicNode usage in the file.Design notes
Behavior change vs. the previous tool: From a caller's perspective, the tool still takes a UUID and still returns
SuccessResponse(message=...). The difference is observable only in the graph: edges and orphan entities are now removed alongside the episode, rather than being left dangling.No new arguments, no new failure modes.
Graphiti.remove_episodeitself looks up the episode by UUID (same as before) and raises on a missing UUID just likeEpisodicNode.get_by_uuiddid — the surrounding try/except still catches that and returnsErrorResponse.Why this isn't an opt-in flag. Leaving stale edges in the graph is unlikely to be the desired behavior for
delete_episode. That said, this is a behavior change. If maintainers prefer to preserve existing behavior, an opt-out flag (cascade: bool = True) would preserve the option to fall back to the old behavior — happy to add it on request.Verification
ruff check+ruff format --checkclean.pyrightclean (0 errors, 0 warnings, 0 informations).Episodicnode, allRELATES_TOedges where the episode was the first provenance entry, and any entity nodes that were mentioned only by that episode. Edges that share provenance with other episodes are preserved.Related PRs
This is PR 2 of 3 from #1489. The companion PRs: