# Failure Patterns Summary

## Pattern Groups

### Group 1: Tool Body Statement Support (2 tests)
**Tests**: `test_tool_with_multiple_statements`, `test_then_chaining_with_nested_calls`

**Core Issue**: Tool bodies cannot contain assignment statements
```alloy
tool process_order takes order returns order_result {
    tax_amount = order + 0.08    # ❌ Parser fails here
    total = order + tax_amount    # ❌ Not reached
    order_result = { ... }        # ❌ Not reached
}
```

**Grammar Limitation**:
- Current: `tool_body → expression`
- Needed: `tool_body → statement_list`

### Group 2: Chain Expression Precedence (2 tests)
**Tests**: `test_then_chaining_agent_to_tool`, `test_agent_block_in_chain`

**Core Issue**: Complex expressions don't chain properly
```alloy
# Agent command with data doesn't chain correctly
final_result, error = claude Research "AI trends" then process_analysis
# Parses as: (claude Research) + ("AI trends" then process_analysis) ❌

# Agent blocks can't be chain targets
final_result, error = prepare_text("hello") then claude {
    must Analyze  # ❌ Parser fails here
}
```

**Grammar Limitation**:
- Precedence rules split expressions incorrectly
- Agent blocks not recognized as chain targets

### Group 3: Missing Grammar Rules (1 test)
**Test**: `test_alloy_agent_builtin`

**Core Issue**: Specific token pattern not defined
```alloy
code_review = alloy Review "tool_code"  # ❌ STRING after command not supported
```

**Grammar Limitation**:
- Pattern `alloy IDENTIFIER STRING` not in grammar
- Only supports predefined patterns

## Impact Analysis

### High Impact (Affects Core Features):
1. **Tool Body Statements** - Prevents complex tool implementations
2. **Chain Expression Precedence** - Breaks data flow in pipelines

### Medium Impact (Limits Functionality):
3. **Agent Block Chaining** - Reduces composability of agent operations
4. **Alloy Command Patterns** - Limits built-in agent usage

### Low Impact (Already Working):
- Basic agent blocks work correctly
- Simple tool definitions work
- Basic chaining works for simple cases

## Root Cause Summary

**The fundamental issue is that the grammar was designed for simple cases and doesn't scale to complex, nested expressions.**

Key architectural limitations:
1. **Flat grammar structure** - No hierarchy for different contexts (tool body vs top-level)
2. **Insufficient precedence rules** - Can't disambiguate complex expressions
3. **Missing composition rules** - Some valid combinations aren't defined
4. **Context-insensitive parsing** - Parser doesn't know it's inside a tool body

## Why 94.6% Pass Rate?

The 123 passing tests cover:
- ✅ Simple, single-feature usage
- ✅ Basic combinations of 2 features
- ✅ Well-defined precedence cases
- ✅ Non-ambiguous syntax

The 7 failing tests expose:
- ❌ Complex, multi-feature interactions
- ❌ Ambiguous precedence situations
- ❌ Context-dependent parsing needs
- ❌ Missing grammar productions

These edge cases represent the "last mile" of language completeness - where simple grammar rules meet complex real-world usage patterns.