Loading...
Loading...
Detects orphaned code (files/functions that exist but are never imported or called in production), preventing "created but not integrated" failures. Use before marking features complete, before moving ADRs to completed, during code reviews, or as part of quality gates. Triggers on "detect orphaned code", "find dead code", "check for unused modules", "verify integration", or proactively before completion. Works with Python modules, functions, classes, and LangGraph nodes. Catches the ADR-013 failure pattern where code exists and tests pass but is never integrated.
npx skill4agent add dawiddutoit/custom-claude quality-detect-orphaned-code# Quick check for a specific module
grep -r "from.*your_module import\|import.*your_module" src/ --include="*.py" | grep -v test
# If output is EMPTY → Module is orphaned (not imported in production)
# Comprehensive orphan detection (if script exists)
./scripts/verify_integration.sh
# Check specific function usage
grep -r "your_function_name" src/ --include="*.py" | grep -v test | grep -v "^[^:]*:.*def "
# If output is EMPTY → Function is orphaned (never called)# File: src/myapp/features/new_feature.py (175 lines)
def process_data(data: dict) -> Result:
"""Process data and return result."""
# ... implementation ...$ grep -r "from.*new_feature import\|import.*new_feature" src/ --include="*.py" | grep -v test
(empty) ❌# File: src/myapp/services/utils.py
def helper_function(x: int) -> int: # Orphaned
return x * 2
def another_function(x: int) -> int: # Used
return x + 1$ grep -r "helper_function" src/ --include="*.py" | grep -v test | grep -v "def helper_function"
(empty) ❌ # Only definition, no call-sites
$ grep -r "another_function" src/ --include="*.py" | grep -v test | grep -v "def another_function"
src/myapp/main.py:45: result = another_function(5) ✅ # Has call-site# File: src/myapp/models/new_model.py
class NewModel:
def __init__(self, data: dict) -> None:
self.data = data$ grep -r "NewModel" src/ --include="*.py" | grep -v test | grep -v "class NewModel"
(empty) ❌ # Only definition, no instantiation# File: src/myapp/graph/architecture_nodes.py
async def create_architecture_review_node(agent):
"""Create architecture review node."""
# ... implementation ...$ grep "architecture_nodes" src/myapp/graph/builder.py
(empty) ❌ # Not imported in builder
$ grep "add_node.*architecture_review" src/myapp/graph/builder.py
(empty) ❌ # Not added to graph# In builder.py
if settings.enable_experimental_features: # Never True in production
graph.add_node("experimental", experimental_node)# Check configuration
$ grep "enable_experimental_features" .env
# Not set (defaults to False)
# Check logs for execution
$ grep "experimental" logs/*.log
(empty) ❌ # Never executes# Check if module is imported
grep -r "from.*MODULE_NAME import\|import.*MODULE_NAME" src/ --include="*.py" | grep -v test
# Expected: At least one import line from production code
# If empty: Module is orphaned# Check if function is called
grep -r "FUNCTION_NAME" src/ --include="*.py" | grep -v test | grep -v "def FUNCTION_NAME"
# Expected: At least one call-site
# If empty: Function is orphaned# Check if class is instantiated
grep -r "CLASS_NAME(" src/ --include="*.py" | grep -v test | grep -v "class CLASS_NAME"
# Expected: At least one instantiation
# If empty: Class is orphanedscripts/verify_integration.sh#!/bin/bash
# Detects orphaned modules (created but never imported in production)
set -e
echo "=== Orphan Detection ==="
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
orphaned=0
checked=0
for file in $(find src/ -name "*.py" -type f | sort); do
module=$(basename "$file" .py)
dir=$(dirname "$file")
# Skip special files
[[ "$module" == "__init__" ]] && continue
[[ "$module" == "__main__" ]] && continue
[[ "$module" == "main" ]] && continue
[[ "$module" == *"test"* ]] && continue
checked=$((checked + 1))
# Check if module is imported (excluding self and tests)
imports=$(grep -r -E "import\s+.*\b${module}\b|from\s+.*\b${module}\b\s+import" src/ \
--include="*.py" 2>/dev/null | \
grep -v "$file" | \
grep -v "test_" | \
grep -v "__pycache__" | \
wc -l | tr -d ' ')
if [ "$imports" -eq 0 ]; then
# Check if exported via __init__.py
init_file="$dir/__init__.py"
init_export=0
if [ -f "$init_file" ]; then
init_export=$(grep -c "\b$module\b" "$init_file" 2>/dev/null || echo "0")
fi
if [ "$init_export" -eq 0 ]; then
echo -e "${YELLOW}⚠️ ORPHANED:${NC} $file"
echo " No production imports found"
orphaned=$((orphaned + 1))
fi
fi
done
echo ""
echo "=== Results ==="
echo "Modules checked: $checked"
if [ $orphaned -gt 0 ]; then
echo -e "${RED}❌ ERROR: $orphaned orphaned module(s) found${NC}"
echo ""
echo "These files exist but are never imported in production code."
echo "Either wire them up or remove them."
exit 1
fi
echo -e "${GREEN}✅ All modules are integrated${NC}"
exit 0tests/integration/test_graph_completeness.py"""Verify all expected nodes are in the compiled graph."""
import pytest
from langgraph.checkpoint.memory import MemorySaver
from myapp.graph.builder import create_coordination_graph
# List ALL nodes that should be in the graph
EXPECTED_NODES: list[str] = [
"query_claude",
"analyze_task",
"architecture_review", # Add new nodes here
# ... other nodes
]
@pytest.mark.asyncio
async def test_all_expected_nodes_in_graph() -> None:
"""Verify all expected nodes are wired into the graph."""
graph = await create_coordination_graph(MemorySaver())
actual_nodes = list(graph.get_graph().nodes.keys())
# Filter internal nodes
actual_nodes = [n for n in actual_nodes if not n.startswith("__")]
missing = [n for n in EXPECTED_NODES if n not in actual_nodes]
assert not missing, (
f"Nodes in EXPECTED_NODES but not in graph: {missing}. "
f"Either wire these nodes into builder.py or remove from EXPECTED_NODES."
)# Find all .py files (excluding tests and special files)
find src/ -name "*.py" -type f | \
grep -v __pycache__ | \
grep -v test_ | \
grep -v __init__.py | \
sort# Extract module name
MODULE=$(basename "$file" .py)
# Check for imports
echo "Checking: $MODULE"
grep -r "from.*$MODULE import\|import.*$MODULE" src/ --include="*.py" | grep -v test
# If empty → ORPHANED
# If has lines → Check if they're from production code# Example report
echo "=== Orphan Detection Report ==="
echo ""
echo "ORPHANED (Category C):"
echo " - src/features/architecture_nodes.py (CRITICAL)"
echo " - src/utils/old_helper.py"
echo ""
echo "SUSPICIOUS (Category B):"
echo " - src/features/experimental.py (only 1 import, in disabled code)"
echo ""
echo "INTEGRATED (Category A):"
echo " - src/core/main.py"
echo " - src/services/coordinator.py"scripts/verify_integration.sh# Check entire project
./scripts/verify_integration.sh
# Expected output:
# === Orphan Detection ===
# ⚠️ ORPHANED: src/features/architecture_nodes.py
# No production imports found
#
# === Results ===
# Modules checked: 45
# ❌ ERROR: 1 orphaned module(s) found01scripts/check_imports.sh# Check specific module
./scripts/check_imports.sh architecture_nodes
# Expected output:
# Checking module: architecture_nodes
# Production imports: 0
# Test imports: 2
# Status: ❌ ORPHANED (only test imports)#!/bin/bash
# Check if a specific module is imported in production code
MODULE=$1
if [ -z "$MODULE" ]; then
echo "Usage: $0 <module_name>"
exit 1
fi
echo "Checking module: $MODULE"
# Count production imports
PROD=$(grep -r "from.*$MODULE import\|import.*$MODULE" src/ --include="*.py" | \
grep -v test | wc -l | tr -d ' ')
# Count test imports
TEST=$(grep -r "from.*$MODULE import\|import.*$MODULE" tests/ --include="*.py" | \
wc -l | tr -d ' ')
echo "Production imports: $PROD"
echo "Test imports: $TEST"
if [ "$PROD" -eq 0 ]; then
if [ "$TEST" -gt 0 ]; then
echo "Status: ❌ ORPHANED (only test imports)"
else
echo "Status: ❌ ORPHANED (no imports at all)"
fi
exit 1
else
echo "Status: ✅ INTEGRATED"
exit 0
fiscripts/check_callsites.sh# Check if function is called
./scripts/check_callsites.sh create_architecture_review_node
# Expected output:
# Checking function: create_architecture_review_node
# Definitions: 1
# Call-sites: 0
# Status: ❌ ORPHANED (defined but never called)#!/bin/bash
# Check if a function is called in production code
FUNCTION=$1
if [ -z "$FUNCTION" ]; then
echo "Usage: $0 <function_name>"
exit 1
fi
echo "Checking function: $FUNCTION"
# Count definitions
DEFS=$(grep -r "def $FUNCTION" src/ --include="*.py" | wc -l | tr -d ' ')
# Count call-sites (excluding definitions and tests)
CALLS=$(grep -r "$FUNCTION" src/ --include="*.py" | \
grep -v "def $FUNCTION" | \
grep -v test | \
wc -l | tr -d ' ')
echo "Definitions: $DEFS"
echo "Call-sites: $CALLS"
if [ "$CALLS" -eq 0 ]; then
echo "Status: ❌ ORPHANED (defined but never called)"
exit 1
else
echo "Status: ✅ CALLED ($CALLS call-sites)"
exit 0
fiquality-run-quality-gates### Gate 6: Integration Verification (MANDATORY for new modules)
When the change involves new files or modules:
```bash
# Detect orphaned modules
./scripts/verify_integration.sh
# For LangGraph changes
uv run pytest tests/integration/test_graph_completeness.py -v.py
### Add to ADR Completion Checklist
**In `create-adr-spike` skill:**
```markdown
### Phase 6: Integration Verification
Before moving ADR to completed:
- [ ] Run: `./scripts/verify_integration.sh` (passes)
- [ ] No orphaned modules detected
- [ ] All new modules imported in production code
- [ ] All new functions have call-sites.github/workflows/quality.yml- name: Detect Orphaned Code
run: |
./scripts/verify_integration.shreferences/orphan-detection-theory.mdreferences/adr-013-orphan-analysis.mdreferences/vulture-vs-integration-check.mdexamples/orphan-module-detection.mdexamples/orphan-function-detection.mdexamples/fixing-orphaned-code.mdscripts/verify_integration.shscripts/check_imports.shscripts/check_callsites.shtemplates/orphan-detection-report.md$ ./scripts/verify_integration.sh
=== Orphan Detection ===
Checking for orphaned modules in src/...
=== Results ===
Modules checked: 45
✅ All modules are integrated$ ./scripts/verify_integration.sh
=== Orphan Detection ===
Checking for orphaned modules in src/...
⚠️ ORPHANED: src/temet_run/coordination/graph/architecture_nodes.py
No production imports found
⚠️ ORPHANED: src/temet_run/coordination/graph/task_provider_nodes.py
No production imports found
=== Results ===
Modules checked: 45
❌ ERROR: 2 orphaned module(s) found
These files exist but are never imported in production code.
To fix, either:
1. Wire them up (add imports and call-sites)
2. Remove them if no longer needed
3. Export via __init__.py if they're part of the public API$ uv run pytest tests/integration/test_graph_completeness.py -v
FAILED tests/integration/test_graph_completeness.py::test_all_expected_nodes_in_graph
AssertionError: Nodes in EXPECTED_NODES but not in graph: ['architecture_review', 'retrieve_tasks'].
Either wire these nodes into builder.py or remove from EXPECTED_NODES.src/EXPECTED_NODESquality-verify-implementation-completeutil-manage-todo