implement trace tool v2: tree output, bidirectional traversal#260
Conversation
…ional traversal Replace flat edges/paths output with nested tree format. Add configurable collapse_roles, collapse_min_chain_length, direction="both" for bidirectional traversal with shared visited set, min_result_nodes retry, and source-relative fan-out ranking. Breaking API change: TraceEdge/TracePath replaced by TreeNode/RankedLeaf. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Code Review: Trace Tool v2Overall this is a solid architectural upgrade — the tree structure is cleaner than flat edges for LLM consumption, and the bidirectional mode is a useful addition. Found a couple of bugs and some cleanup items: Bugs1. edges_to_remove.add(id(id(out_edge))) # ← id() of an integer, meaningless
# Also remove the original out_edge.
edges_to_remove.add(id(out_edge)) # ← this is the correct oneLine 524 calls 2. Bidirectional advisory is dead code ( if direction == "both" and pass_idx == 0:
pass
if direction == "both" and pass_idx == 1:
# Nodes discovered in pass 0 that would also be discovered in pass 1...
passTwo no-op blocks with comments about a future advisory. Either implement the advisory or remove these. Code Quality3. Triple tree walk in hints ( The cross-service hint code walks the tree 3 times: once to find xs_nodes with empty parent_id (discarded immediately), once to build 4. Source-relative priority tables have identical values for SERVICE and REPOSITORY sources (both rank REPOSITORY=5, SERVICE=4, CONTROLLER=3). Intentional or should they differ? Test GapNo test verifies After fixing the two bugs (remove 🤖 Generated with Claude Code |
- Remove duplicate id() call in collapse logic - Remove dead bidirectional advisory blocks - Consolidate triple tree walk to single pass in mcp_hints.py - Make REPOSITORY source-relative priority distinct from SERVICE - Add test for seed with zero matching edges Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
The retry acceptance check used `or` which silently accepted improved- but-still-below-target results without emitting the advisory. Split into separate checks so the advisory fires whenever below target. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Summary
Implements PLAN-TRACE-TOOL-V2 as a single PR on the
experimentalbranch.edges/pathswith nestedtree(TreeNode) andranked_leaves(RankedLeaf). Breaking API change —TraceEdge/TracePathremoved, replaced byTreeNode/EdgeFromParent/RankedLeaf.collapse_roles(default["OTHER"]) andcollapse_min_chain_length(default 1) control which roles are collapsed. Collapsed intermediates retained innodesdict (v1 removed them).direction="both"runs out + in BFS with shared visited set for impact analysis in one call._SOURCE_RELATIVE_PRIORITYtable produces different fan-out ranking based on source node role (e.g., from SERVICE, REPOSITORY outranks CONTROLLER).min_result_nodesretry: When initial BFS produces fewer nodes than target, retries with doubledfan_out_cap(one retry, clamped bymax_nodes_discovered).Changed files
mcp_trace.py_build_tree,_build_ranked_leaves, bidirectional, source-relative rankingserver.pycollapse_roles,collapse_min_chain_length,min_result_nodes,direction="both")mcp_hints.py_trace_structured_hintsmigrated fromedgestotreewalktests/test_mcp_trace.pytests/test_mcp_hints.pydocs/AGENT-GUIDE.mdskills/explore-codebase/SKILL.mdTest plan
.venv/bin/ruff check .clean.venv/bin/python -m pytest tests/test_mcp_trace.py -v— 54 passed, 5 skipped.venv/bin/python -m pytest tests/test_mcp_hints.py -v— all pass.venv/bin/python -m pytest tests -v— 675 passed, 13 skipped (4 pre-existing CLI path failures unrelated to this PR)🤖 Generated with Claude Code