# Implementation Plan: Deep AST Context Pruning (deep_ast_context_pruning_20260306)

> **Reference:** [Spec](./spec.md) | [Architecture Guide](../../../docs/guide_architecture.md)

## Phase 1: Verify Existing Infrastructure
Focus: Confirm tree-sitter integration works

- [ ] Task 1.1: Initialize MMA Environment
    - Run `activate_skill mma-orchestrator` before starting

- [ ] Task 1.2: Verify tree_sitter installation
    - WHERE: `requirements.txt`, imports
    - WHAT: Ensure `tree_sitter` and `tree_sitter_python` are installed
    - HOW: Check imports in `src/file_cache.py`
    - CMD: `uv pip list | grep tree`

- [ ] Task 1.3: Verify ASTParser functionality
    - WHERE: `src/file_cache.py`
    - WHAT: Test get_skeleton() and get_curated_view()
    - HOW: Use `manual-slop_py_get_definition` on ASTParser class
    - OUTPUT: Document exact API

- [ ] Task 1.4: Review worker context injection
    - WHERE: `src/multi_agent_conductor.py` `run_worker_lifecycle()`
    - WHAT: Understand current context injection pattern
    - HOW: Use `manual-slop_py_get_code_outline` on function

## Phase 2: Targeted Function Extraction
Focus: Extract only relevant functions from target files

- [ ] Task 2.1: Implement targeted extraction function
    - WHERE: `src/file_cache.py` or new `src/context_pruner.py`
    - WHAT: Function to extract specific functions by name
    - HOW:
      ```python
      def extract_functions(code: str, function_names: list[str]) -> str:
       parser = ASTParser("python")
       tree = parser.parse(code)
       # Walk AST, find function_definition nodes matching names
       # Return combined signatures + docstrings
      ```
    - CODE STYLE: 1-space indentation

- [ ] Task 2.2: Add dependency traversal
    - WHERE: Same as Task 2.1
    - WHAT: Find functions called by target functions
    - HOW: Parse function body for Call nodes, extract names
    - SAFETY: Limit traversal depth to prevent explosion

- [ ] Task 2.3: Integrate with worker context
    - WHERE: `src/multi_agent_conductor.py` `run_worker_lifecycle()`
    - WHAT: Use targeted extraction when ticket has target_file
    - HOW:
      - Check if `ticket.target_file` matches a context file
      - If so, use `extract_functions()` instead of full content
      - Fall back to skeleton for other files
    - SAFETY: Handle missing function names gracefully

## Phase 3: AST Caching
Focus: Cache parsed trees to avoid re-parsing

- [ ] Task 3.1: Implement AST cache in file_cache.py
    - WHERE: `src/file_cache.py`
    - WHAT: LRU cache for parsed AST trees
    - HOW:
      ```python
      from functools import lru_cache
      from pathlib import Path
      import time
      
      _ast_cache: dict[str, tuple[float, Any]] = {}  # path -> (mtime, tree)
      _CACHE_MAX_SIZE: int = 10
      
      def get_cached_tree(path: str) -> tree_sitter.Tree:
       mtime = Path(path).stat().st_mtime
       if path in _ast_cache:
        cached_mtime, tree = _ast_cache[path]
        if cached_mtime == mtime:
         return tree
       # Parse and cache
       code = Path(path).read_text()
       tree = parser.parse(code)
       _ast_cache[path] = (mtime, tree)
       if len(_ast_cache) > _CACHE_MAX_SIZE:
        # Evict oldest
        oldest = next(iter(_ast_cache))
        del _ast_cache[oldest]
       return tree
      ```
    - SAFETY: Thread-safe if called from single thread

- [ ] Task 3.2: Use cache in skeleton generation
    - WHERE: `src/file_cache.py`
    - WHAT: Use cached tree instead of re-parsing
    - HOW: Call `get_cached_tree()` in `get_skeleton()`

## Phase 4: Token Measurement
Focus: Measure and log token reduction

- [ ] Task 4.1: Add token counting to context injection
    - WHERE: `src/multi_agent_conductor.py`
    - WHAT: Count tokens before and after pruning
    - HOW:
      ```python
      def _count_tokens(text: str) -> int:
       return len(text) // 4  # Rough estimate
      ```
    - SAFETY: Non-blocking, fast calculation

- [ ] Task 4.2: Log token reduction metrics
    - WHERE: `src/multi_agent_conductor.py`
    - WHAT: Log reduction percentage
    - HOW: `print(f"Context tokens: {before} -> {after} ({reduction_pct}% reduction)")`
    - SAFETY: Use session_logger for structured logging

- [ ] Task 4.3: Display in MMA dashboard (optional)
    - WHERE: `src/gui_2.py` `_render_mma_dashboard()`
    - WHAT: Show token reduction per worker
    - HOW: Add to worker stream panel
    - SAFETY: Optional enhancement

## Phase 5: Testing
Focus: Verify all functionality

- [ ] Task 5.1: Write targeted extraction tests
    - WHERE: `tests/test_context_pruner.py` (new file)
    - WHAT: Test extraction returns only specified functions
    - HOW: Create test file with known functions, extract subset

- [ ] Task 5.2: Write integration test
    - WHERE: `tests/test_context_pruner.py`
    - WHAT: Run worker with skeleton context
    - HOW: Use `live_gui` fixture with mock provider
    - VERIFY: Worker completes ticket successfully

- [ ] Task 5.3: Performance test
    - WHERE: `tests/test_context_pruner.py`
    - WHAT: Verify parse time < 100ms
    - HOW: Time parsing of various file sizes

- [ ] Task 5.4: Conductor - Phase Verification
    - Run: `uv run pytest tests/test_context_pruner.py tests/test_ast_parser.py -v`
    - Verify token reduction in logs

## Implementation Notes

### tree-sitter Pattern
- Already implemented in `file_cache.py`
- Language: `tree_sitter_python`
- Node types: `function_definition`, `class_definition`, `import_statement`

### Cache Strategy
- Key: file path (absolute)
- Value: (mtime, tree) tuple
- Eviction: LRU with max 10 entries
- Invalidation: mtime comparison

### Files Modified
- `src/file_cache.py`: Add cache, targeted extraction
- `src/multi_agent_conductor.py`: Use targeted extraction
- `tests/test_context_pruner.py`: New test file

### Code Style Checklist
- [ ] 1-space indentation throughout
- [ ] CRLF line endings on Windows
- [ ] No comments unless documenting API
- [ ] Type hints on all functions
