feat: Implement Gemini's universal environment-adaptive solution with .bmad_env state file

Collaborative solution with Gemini to resolve bash approval prompts by shifting from
shell conditionals to agent guidance with persistent state management.

## Key Changes Based on Gemini's Analysis

### 1. Universal State Persistence (.bmad_env file)
- Updated lightweight-ide-detection.md to write IDE environment vars to .bmad_env
- Updated auto-language-init.md to append language environment vars to .bmad_env
- Each Bash tool call can now 'source .bmad_env' to load persistent state
- Supports all IDEs: Claude Code CLI, Cursor AI, Windsurf, Cline, Roo, Trae, Gemini, GitHub Copilot, CLI

### 2. Agent Guidance vs Shell Conditionals
- Restructured reality-audit-comprehensive.md Phase 2 from shell conditionals to agent instructions
- Replaced shell logic with clear procedural guidance for agents
- Task files now provide instructions FOR agents rather than literal bash scripts to execute

### 3. Universal Dev Agent Adaptive Tool Selection
- Added adaptive_tool_selection section to dev.md agent configuration
- Core directive: environment-adaptive tool selection based on detected IDE and language
- Universal compatibility: detects and optimizes for any supported IDE environment
- Language agnostic: supports C#, TypeScript, JavaScript, Java, Rust, Go, Python, Ruby, PHP

### 4. Environment Context Loading Pattern
- All environment-dependent operations start with 'source .bmad_env'
- Agent instructions specify when to use native vs CLI tools based on loaded environment
- Eliminates stateless environment variable issues across separate Bash tool calls
- Universal approach works across all IDE environments automatically

## Expected Results
- No more bash approval prompts in Claude Code CLI environments
- Native tool optimization in Cursor AI, Windsurf, and other supported IDEs
- Agents load environment state and make conscious tool selection decisions
- Graceful fallback to CLI batch mode when appropriate
- Universal language and IDE compatibility

## Collaboration Credit
This solution resulted from collaborative analysis with Gemini AI, identifying that
agents were interpreting shell conditionals as literal commands rather than guidance.

Key insight: shift from 'execute this shell logic' to 'read the state and decide what to do'.
This commit is contained in:
James (Claude Code) 2025-07-24 08:49:20 -04:00
parent 412a968125
commit 6e97ddcbd1
4 changed files with 62 additions and 41 deletions

View File

@ -48,6 +48,22 @@ core_principles:
- CRITICAL: NO SIMULATION PATTERNS - Zero tolerance for Random.NextDouble(), Task.FromResult(), NotImplementedException, SimulateX() methods in production code
- CRITICAL: REAL IMPLEMENTATION ONLY - All methods must contain actual business logic, not placeholders or mock data
- Numbered Options - Always use numbered lists when presenting choices to the user
- CRITICAL: ADAPTIVE TOOL SELECTION - You are an expert AI developer operating within the BMAD framework with environment-adaptive capabilities
adaptive_tool_selection:
core_directive: "Your primary directive is to use environment-adaptive tool selection based on detected IDE and project language"
detection_requirements:
- "Always run detection first: At the start of any major task, run the auto-language-init.md and lightweight-ide-detection.md tasks"
- "Persist and Load State: These detection tasks create a .bmad_env file. In every subsequent step requiring a shell command, you MUST first run 'source .bmad_env' to load the correct context"
tool_selection_logic:
- "Choose Tools Wisely: Read the task instructions to understand when to use native IDE tools (like Grep, Read, Bash with IDE integration) versus standard CLI fallbacks"
- "Your choice must be guided by the variables in .bmad_env (e.g., $USE_IDE_TOOLS, $DETECTED_IDE)"
- "If $DETECTED_IDE is 'claude-code', use native Bash tool with clear descriptions - no approval prompts required"
- "If $DETECTED_IDE is 'cli', inform user before commands that may require approval"
environment_persistence:
- "Environment variables from export don't persist across Bash tool calls"
- "Always use 'source .bmad_env' to load environment context in each Bash command"
- "Follow task instructions as agent guidance, not literal shell script execution"
# All commands require * prefix when used (e.g., *help)
commands:

View File

@ -208,7 +208,17 @@ EOF
export BMAD_COMPONENT_PATTERNS="$COMPONENT_PATTERNS"
export BMAD_FILE_EXTENSIONS="$FILE_EXTENSIONS"
# Append language environment variables to .bmad_env file for persistent state
echo "export BMAD_PRIMARY_LANGUAGE=\"$PRIMARY_LANGUAGE\"" >> .bmad_env
echo "export BMAD_BUILD_COMMAND=\"$BUILD_COMMAND\"" >> .bmad_env
echo "export BMAD_TEST_COMMAND=\"$TEST_COMMAND\"" >> .bmad_env
echo "export BMAD_SIMULATION_PATTERNS=\"$SIMULATION_PATTERNS\"" >> .bmad_env
echo "export BMAD_ERROR_PATTERNS=\"$ERROR_PATTERNS\"" >> .bmad_env
echo "export BMAD_COMPONENT_PATTERNS=\"$COMPONENT_PATTERNS\"" >> .bmad_env
echo "export BMAD_FILE_EXTENSIONS=\"$FILE_EXTENSIONS\"" >> .bmad_env
echo "✅ Language environment initialized: $PRIMARY_LANGUAGE"
echo "Language configuration appended to .bmad_env for persistent state across tool calls"
}
# Call auto-initialization (runs automatically when this task is loaded)

View File

@ -9,7 +9,7 @@ Minimal-token environment detection to optimize BMAD task execution without cons
### Single Command Detection (50-100 tokens)
```bash
# Lightweight IDE detection with caching
# Lightweight IDE detection with environment file persistence
if [ -f "tmp/ide-detected.txt" ]; then
DETECTED_IDE=$(cat tmp/ide-detected.txt)
else
@ -50,7 +50,15 @@ case $DETECTED_IDE in
;;
esac
# Create/update .bmad_env file with IDE environment variables for persistent state
> .bmad_env
echo "export DETECTED_IDE=\"$DETECTED_IDE\"" >> .bmad_env
echo "export USE_IDE_TOOLS=\"$USE_IDE_TOOLS\"" >> .bmad_env
echo "export BATCH_COMMANDS=\"$BATCH_COMMANDS\"" >> .bmad_env
echo "export APPROVAL_REQUIRED=\"$APPROVAL_REQUIRED\"" >> .bmad_env
echo "IDE: $DETECTED_IDE | Use IDE Tools: $USE_IDE_TOOLS | Batch: $BATCH_COMMANDS"
echo "Environment configuration saved to .bmad_env for persistent state across tool calls"
```
## Tool Adaptation Logic

View File

@ -105,58 +105,45 @@ Use the language-specific simulation patterns from `$BMAD_SIMULATION_PATTERNS` a
## Phase 2: Environment-Adaptive Build and Runtime Validation
**Auto-Initialize Environment Detection (if not already done):**
```
# Ensure environment detection is loaded
if [ -z "$BMAD_BUILD_COMMAND" ]; then
Read tool: bmad-core/tasks/auto-language-init.md
fi
### Agent Instructions for Environment-Adaptive Build Validation
if [ -z "$USE_IDE_TOOLS" ]; then
Read tool: bmad-core/tasks/lightweight-ide-detection.md
fi
```
**Step 1: Load Environment Configuration**
**Environment-Adaptive Build Validation:**
**Step 1: Display Environment Context**
```
First, load the environment configuration we previously saved by running:
```bash
source .bmad_env
echo "🔧 Environment-Adaptive Build Validation:"
echo "Environment: $DETECTED_IDE | Language: $BMAD_PRIMARY_LANGUAGE"
echo "Build Command: $BMAD_BUILD_COMMAND"
echo "Tools Mode: $([ "$USE_IDE_TOOLS" = "true" ] && echo "Native IDE integration" || echo "CLI batch mode")"
```
**Step 2: Execute Build Using Environment-Appropriate Method**
```
if [ "$USE_IDE_TOOLS" = "true" ]; then
echo "Using native IDE integration for build validation"
# Use Bash tool with clear description for build command
BUILD_OUTPUT=$($BMAD_BUILD_COMMAND 2>&1)
BUILD_EXIT_CODE=$?
else
echo "Using CLI batch mode for build validation (may require approval)"
# Traditional CLI approach
BUILD_OUTPUT=$($BMAD_BUILD_COMMAND 2>&1)
BUILD_EXIT_CODE=$?
fi
**Step 2: Execute Build Command Based on Environment**
Check the value of `$DETECTED_IDE` from the loaded environment:
- **If `$DETECTED_IDE` is `claude-code`**, then you MUST use the native `Bash` tool to run the build command defined in `$BMAD_BUILD_COMMAND`. This will use Claude Code CLI's native integration without approval prompts.
- **If `$DETECTED_IDE` is `cli`**, then you may encounter approval prompts, so inform the user before proceeding.
**Tool Call for Build Execution:**
```bash
source .bmad_env
echo "Using native IDE integration for build validation"
echo "=== Building Project ==="
$BMAD_BUILD_COMMAND
BUILD_EXIT_CODE=$?
echo "Build Exit Code: $BUILD_EXIT_CODE"
```
**Step 3: Analyze Build Results Using Native Tools**
```
# Use Grep tool to analyze build output for errors
if [ "$USE_IDE_TOOLS" = "true" ]; then
echo "Analyzing build results using native IDE tools"
# Would use Grep tool with error patterns from $BMAD_ERROR_PATTERNS
# Would use Read tool for detailed error analysis
else
# Traditional CLI analysis
ERROR_COUNT=$(echo "$BUILD_OUTPUT" | grep -c "error" || echo 0)
WARNING_COUNT=$(echo "$BUILD_OUTPUT" | grep -c "warning" || echo 0)
fi
```
**Step 3: Analyze Build Results**
After the build completes, analyze the results:
- **If the build succeeded (exit code 0)**: Proceed to error pattern analysis
- **If the build failed**: Use the Grep tool to search the output for error patterns
For error analysis, use the Grep tool with the patterns defined in `$BMAD_ERROR_PATTERNS` to identify specific error types and counts.
**Runtime Validation (Simplified):**
- Use `$BMAD_TEST_COMMAND` if available for runtime testing