refactor(mcp): extract shared persistence helper for entity writes#254
refactor(mcp): extract shared persistence helper for entity writes#254gaodan-fang wants to merge 1 commit into
Conversation
Collapse the duplicated update_entities + NamespaceNotFoundException retry pattern in store_user_facts, save_trajectory, and create_entity into a single _persist_entities helper. New entity-writing tools can now reuse the helper for consistent error handling. Closes #241
📝 WalkthroughWalkthroughThis PR extracts a shared ChangesEntity Persistence Refactor
Sequence DiagramNo sequence diagram; this is a refactoring that consolidates inline retry logic into a helper without changing the observable data flow or component interactions. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
altk_evolve/frontend/mcp/mcp_server.py (1)
510-548: ⚡ Quick winGuideline write still bypasses the new helper — inconsistent retry behavior within the same tool.
The trajectory write now goes through
_persist_entities, but the guideline write a few lines below at 543-548 is still a rawget_client().update_entities(...)call with no namespace-eviction/retry path. If the namespace is evicted (or deleted externally) between the trajectory write and the guideline write, this second call will raiseNamespaceNotFoundExceptionand bubble up unhandled, defeating the consistency this PR is trying to establish. Since the helper already supportsenable_conflict_resolution=True, the swap is mechanical.♻️ Proposed fix to route the guideline write through `_persist_entities`
if guideline_entities: - get_client().update_entities( - namespace_id=resolved_ns, - entities=guideline_entities, - enable_conflict_resolution=True, - ) + _, resolved_ns = _persist_entities( + namespace_id=resolved_ns, + entities=guideline_entities, + enable_conflict_resolution=True, + )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@altk_evolve/frontend/mcp/mcp_server.py` around lines 510 - 548, The guideline write currently calls get_client().update_entities(...) directly which bypasses the retry/namespace-eviction handling in _persist_entities; replace that raw update with a call into _persist_entities using the same resolved_ns and guideline_entities and enable_conflict_resolution=True (e.g., call _persist_entities(namespace_id=resolved_ns, entities=guideline_entities, enable_conflict_resolution=True) and ignore/handle its returned (namespace, resolved_ns) tuple as done earlier) so guideline writes get the same eviction/retry behavior as the trajectory write.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@altk_evolve/frontend/mcp/mcp_server.py`:
- Around line 510-548: The guideline write currently calls
get_client().update_entities(...) directly which bypasses the
retry/namespace-eviction handling in _persist_entities; replace that raw update
with a call into _persist_entities using the same resolved_ns and
guideline_entities and enable_conflict_resolution=True (e.g., call
_persist_entities(namespace_id=resolved_ns, entities=guideline_entities,
enable_conflict_resolution=True) and ignore/handle its returned (namespace,
resolved_ns) tuple as done earlier) so guideline writes get the same
eviction/retry behavior as the trajectory write.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8c8e0edf-418e-455f-a0d3-ee395e1b7867
📒 Files selected for processing (1)
altk_evolve/frontend/mcp/mcp_server.py
Summary
_persist_entitieshelper inaltk_evolve/frontend/mcp/mcp_server.pythat centralizesupdate_entitiesplus theNamespaceNotFoundException→ evict → re-resolve → retry pattern.store_user_facts,save_trajectory, andcreate_entityto call the helper, removing three copies of the retry block.Test plan
pytest tests/unit/test_mcp_server.py— 20/20 passedpytest tests/unit/test_entity_sharing.py— 14/14 passedruff checkcleanmypyclean (pre-commit hook)Closes #241
Summary by CodeRabbit