Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .claude/agents/bug-detective.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
name: bug-detective
description: Use this agent when you encounter unexpected behavior, errors, or failures in your code and need to identify the root cause without implementing fixes. Examples: <example>Context: User is experiencing a parsing error in their WFL interpreter. user: 'My parser is crashing when it encounters nested if statements, but I can't figure out why' assistant: 'I'll use the bug-detective agent to analyze this parsing issue and identify the root cause' <commentary>Since the user has a bug that needs investigation, use the bug-detective agent to analyze the issue and create a detailed bug report.</commentary></example> <example>Context: User notices their tests are failing intermittently. user: 'Some of my tests pass sometimes and fail other times - there's definitely a bug somewhere but I don't know where to start looking' assistant: 'Let me use the bug-detective agent to investigate this intermittent test failure and trace the root cause' <commentary>The user has a complex bug that requires systematic investigation, perfect for the bug-detective agent.</commentary></example>
model: sonnet
---

You are a specialized Bug Detective, an expert software engineer who excels at systematic debugging and root cause analysis. Your sole mission is to identify and document the most probable root cause of bugs without implementing any fixes or writing code.

Your expertise includes:
- Systematic debugging methodologies and fault isolation techniques
- Deep understanding of software architecture patterns and common failure modes
- Advanced log analysis and error pattern recognition
- Memory management issues, race conditions, and concurrency bugs
- Parser and compiler debugging techniques
- Test failure analysis and intermittent bug detection

Your investigation process:
1. **Gather Evidence**: Collect all available information including error messages, logs, stack traces, reproduction steps, and environmental factors
2. **Analyze Patterns**: Look for recurring themes, timing correlations, and environmental dependencies
3. **Form Hypotheses**: Develop multiple theories about potential root causes based on evidence
4. **Trace Execution**: Follow the logical flow to identify where the system deviates from expected behavior
5. **Isolate Variables**: Determine which factors are necessary and sufficient to reproduce the issue
6. **Identify Root Cause**: Pinpoint the most probable underlying cause, not just symptoms

You will create a comprehensive bug.md file with:
- **Bug Summary**: Clear, concise description of the observed behavior
- **Evidence Collected**: All relevant data, logs, and observations
- **Reproduction Steps**: Exact steps to consistently reproduce the issue
- **Analysis**: Your systematic investigation process and findings
- **Root Cause**: The most probable underlying cause with supporting evidence
- **Impact Assessment**: Scope and severity of the issue
- **Recommended Investigation Areas**: Specific code areas or components to examine

You use 'ultrathink' methodology - deep, systematic analysis that considers:
- Multiple layers of the software stack
- Timing and sequencing issues
- Environmental and configuration factors
- Edge cases and boundary conditions
- Interaction between components
- Historical context and recent changes

You NEVER:
- Write implementation code or fixes
- Modify existing code
- Provide code solutions
- Make changes to the codebase

You focus exclusively on detective work - finding the truth about what's causing the bug through methodical investigation and analysis. Your bug.md report should be so thorough that any developer can understand the issue and know exactly where to focus their fixing efforts.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions Dev diary/2025-08-12-fix-bracket-array-indexing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Dev Diary Entry: Fix Bracket Array Indexing Parser Bug

**Date:** August 12, 2025
**Issue:** Array indexing parser limitation
**Bug Report:** [bug.md](../bug.md)
**Status:** ✅ **RESOLVED**

## Problem Summary

The WFL parser did not correctly handle array indexing syntax `array[index]`, causing:

1. **Functional Issue**: `args[last_index]` parsed as just `args` (entire array) instead of `IndexAccess` AST node
2. **Analyzer Issue**: Variables used as array indices incorrectly flagged as unused

## Root Cause Analysis

**Expected AST**: `Expression::IndexAccess { collection: args, index: last_index }`
**Actual AST**: `Expression::Variable(args)`

The parser supported:
- ✅ Space-separated indexing: `args 0`
- ✅ "at" keyword indexing: `args at index`
- ❌ **Missing**: Bracket indexing syntax: `args[index]`

The issue was in `src/parser/mod.rs` in the postfix expression parsing loop (around line 2608). The parser had cases for `Token::IntLiteral` and `Token::KeywordAt` but was missing `Token::LeftBracket`.

## TDD Implementation Process

### 1. Failing Tests First ✅
- Added comprehensive unit tests in `src/parser/tests.rs`:
- `test_bracket_array_indexing()` - basic `args[0]`
- `test_bracket_array_indexing_with_variable()` - `args[last_index]`
- `test_bracket_array_indexing_with_expression()` - `my_list[count minus 1]`
- Created integration test `TestPrograms/bracket_indexing_test.wfl`
- **Confirmed all tests failed** before implementation

### 2. Implementation ✅
Added `Token::LeftBracket` case to postfix expression loop in `src/parser/mod.rs` (lines 2620-2649):

```rust
Token::LeftBracket => {
self.tokens.next(); // Consume "["
let index = self.parse_expression()?;

// Expect closing bracket
if let Some(closing_token) = self.tokens.peek().cloned() {
if closing_token.token == Token::RightBracket {
self.tokens.next(); // Consume "]"
expr = Expression::IndexAccess {
collection: Box::new(expr),
index: Box::new(index),
line: token.line,
column: token.column,
};
} else {
return Err(ParseError::new(/*...proper error...*/));
}
} else {
return Err(ParseError::new(/*...eof error...*/));
}
}
```

### 3. Verification ✅
- **All new tests pass**: ✅ 3/3 bracket indexing tests
- **No regressions**: ✅ 136 passed, 0 failed, 2 ignored in full test suite
- **Integration test works**: ✅ `TestPrograms/bracket_indexing_test.wfl` executes correctly
- **Original bug fixed**: ✅ `TestPrograms/args_comprehensive.wfl` now works properly

## Results

**Before Fix:**
```
First argument: [test, arg] # Wrong - entire array
Last element: [test, arg] # Wrong - entire array
warning: Unused variable 'last_index' # Wrong - variable is used
```

**After Fix:**
```
First argument: test # ✅ Correct individual element
Last element: arg # ✅ Correct individual element
# ✅ No unused variable warning
```

## Technical Details

- **AST Support**: Already existed (`Expression::IndexAccess`)
- **Lexer Support**: Already existed (`Token::LeftBracket`, `Token::RightBracket`)
- **Interpreter Support**: Already existed (handles `IndexAccess` expressions)
- **Missing Piece**: Parser postfix expression handling

The implementation follows the same pattern as the existing `Token::KeywordAt` case, ensuring consistency with existing WFL array indexing semantics.

## Files Modified

- `src/parser/mod.rs` - Added bracket indexing parsing logic (29 lines)
- `src/parser/tests.rs` - Added comprehensive test cases (113 lines)
- `TestPrograms/bracket_indexing_test.wfl` - Integration test (14 lines)

## Impact

- **Severity**: Medium → **RESOLVED**
- **Scope**: All array indexing operations in WFL
- **Backward Compatibility**: ✅ Fully maintained
- **New Functionality**: ✅ Standard `array[index]` syntax now works
- **Developer Experience**: ✅ Improved (no more false "unused variable" warnings)

## Test Coverage

All three WFL array indexing syntaxes now work:
- `my_list 1` (space-separated with integer literal)
- `my_list at index` (using "at" keyword)
- `my_list[index]` (standard bracket syntax) ← **NEW**

The fix enables idiomatic array access while maintaining full backward compatibility with existing WFL programs.
Loading