Files
proffesor-for-testing__agen…/v2/scripts/add-code-execution-examples.sh
Profa fd2f37eaa0 fix(v3): update README claims and init hooks to use correct package name
Changes:
- Fix README.md agent counts (48 QE agents = 41 main + 7 subagents)
- Fix README.md skill counts (15 QE-specific skills)
- Fix v3/README.md agent counts (47 → 48)
- Fix init-wizard.ts hooks from 'npx @agentic-qe/v3' to 'npx agentic-qe'
- Complete v2 → v2/ folder reorganization (54 items moved)
- Update root package.json to point to v3 as main version

Verified claims:
- 48 QE agents (41 main + 7 TDD subagents)
- 15 QE-specific skills
- 12 DDD bounded contexts
- ~5,600+ tests
- HNSW, ReasoningBank, Queen Coordinator features exist

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-17 18:03:40 +00:00

642 lines
16 KiB
Bash
Executable File

#!/bin/bash
# Add Code Execution Examples to QE Agents (Phase 2)
#
# This script adds code execution workflow examples to all 18 QE agents
# per the improvement plan Phase 2 requirements.
#
# Based on:
# - Anthropic MCP code execution best practices
# - docs/QE-IMPROVEMENT-PLAN-SIMPLIFIED.md Phase 2
# - Claude Code agent development guidelines
set -e
echo "📝 Adding Code Execution Examples to QE Agents (Phase 2)"
echo "========================================================="
echo ""
echo "Goal: Show agents how to write code to orchestrate workflows"
echo "Benefit: 98.7% token reduction (150K → 2K tokens)"
echo ""
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Counters
TOTAL=0
UPDATED=0
SKIPPED=0
AGENT_DIR=".claude/agents"
if [ ! -d "$AGENT_DIR" ]; then
echo -e "${RED}❌ Agent directory not found: $AGENT_DIR${NC}"
exit 1
fi
# Function to add code execution section after coordination protocol
add_code_execution_section() {
local agent_file="$1"
local agent_name=$(basename "$agent_file" .md)
echo "📝 Processing: $agent_name"
# Check if agent already has code execution section
if grep -q "## Code Execution Workflows" "$agent_file"; then
echo -e " ${YELLOW}⚠️ Already has code execution section, skipping${NC}"
SKIPPED=$((SKIPPED + 1))
return
fi
# Create backup
cp "$agent_file" "$agent_file.backup-phase2-$(date +%Y%m%d-%H%M%S)"
# Find the insertion point (after Coordination Protocol section)
# We'll insert before the "Memory Namespace" section or at end if not found
# Create temp file with code execution section
local temp_file=$(mktemp)
# Copy everything up to (but not including) Memory Namespace section
awk '
/## Memory Namespace/ { found=1 }
!found { print }
' "$agent_file" > "$temp_file"
# Add code execution section based on agent type
case "$agent_name" in
qe-test-generator)
cat >> "$temp_file" << 'EOF'
## Code Execution Workflows
Instead of multiple MCP tool calls, write code to orchestrate test generation workflows. This approach is **352x faster** (Agent Booster WASM) and reduces token usage by 98.7%.
### Comprehensive Test Generation
```typescript
import {
generateUnitTests,
generateIntegrationTests,
analyzeGaps,
optimizeTestSuite
} from './servers/qe-tools/test-generation';
// Generate initial unit tests with coverage target
const unitTests = await generateUnitTests({
sourceFile: './src/UserService.ts',
framework: 'jest',
coverage: { target: 95, includeEdgeCases: true }
});
console.log(`Generated ${unitTests.count} unit tests`);
console.log(`Current coverage: ${unitTests.coverage.statements}%`);
// Check coverage and fill gaps if needed
if (unitTests.coverage.statements < 95) {
const gaps = await analyzeGaps(unitTests.coverage);
console.log(`Found ${gaps.length} coverage gaps`);
const integrationTests = await generateIntegrationTests({
gaps,
endpoints: ['POST /users', 'GET /users/:id']
});
unitTests.addTests(integrationTests);
}
// Optimize for execution speed
const optimized = await optimizeTestSuite({
tests: unitTests,
constraints: { maxExecutionTime: 300, parallelizable: true }
});
console.log(`Optimized to ${optimized.count} tests (${optimized.executionTime}ms)`);
return optimized;
```
### Property-Based Testing Generation
```typescript
import { generatePropertyTests } from './servers/qe-tools/test-generation';
import fc from 'fast-check';
// Generate property-based tests for complex logic
const propertyTests = await generatePropertyTests({
targetFunction: 'validateEmail',
properties: [
'idempotent', // f(f(x)) = f(x)
'commutative', // f(a,b) = f(b,a)
'associative' // f(f(a,b),c) = f(a,f(b,c))
],
arbitraries: {
email: fc.emailAddress(),
name: fc.string({ minLength: 1, maxLength: 100 })
},
iterations: 1000
});
console.log(`Generated ${propertyTests.length} property tests`);
```
### Multi-Framework Test Generation
```typescript
import { generateForFramework } from './servers/qe-tools/test-generation';
// Generate tests for multiple frameworks in parallel
const frameworks = ['jest', 'mocha', 'vitest', 'playwright'];
const allTests = await Promise.all(
frameworks.map(framework =>
generateForFramework({
sourceFile: './src/api/users.ts',
framework,
testType: 'integration'
})
)
);
console.log('Generated tests for:', frameworks.join(', '));
```
### Discover Available Tools
```bash
# List all test generation tools
ls ./servers/qe-tools/test-generation/
# Search for specific functionality
./servers/qe-tools/search_tools.ts "coverage" "basic"
# Check available test patterns
cat ./servers/qe-tools/test-generation/patterns.json
```
### Ultra-Fast Code Editing with Agent Booster
For rapid test file editing (352x faster than cloud APIs):
```typescript
import { editFile, batchEdit } from 'agent-booster';
// Single test file edit
await editFile({
target_filepath: './tests/UserService.test.ts',
instructions: 'Add error handling test case for invalid user ID',
code_edit: `
// ... existing code ...
test('should handle invalid user ID', () => {
expect(() => service.getUser(-1)).toThrow('Invalid user ID');
});
// ... existing code ...
`
});
// Batch edit multiple test files
await batchEdit({
edits: [
{
target_filepath: './tests/unit/UserService.test.ts',
instructions: 'Add authentication tests',
code_edit: '// Add auth tests...'
},
{
target_filepath: './tests/integration/api.test.ts',
instructions: 'Add API endpoint tests',
code_edit: '// Add API tests...'
}
]
});
```
EOF
;;
qe-test-executor)
cat >> "$temp_file" << 'EOF'
## Code Execution Workflows
Execute tests programmatically with intelligent orchestration and real-time progress tracking.
### Parallel Test Execution
```typescript
import {
executeTests,
aggregateResults,
generateReport
} from './servers/qe-tools/test-execution';
// Execute tests in parallel with retry logic
const results = await executeTests({
testSuites: [
'./tests/unit/**/*.test.ts',
'./tests/integration/**/*.test.ts'
],
parallel: true,
maxWorkers: 4,
retryFailedTests: true,
maxRetries: 3,
timeout: 30000
});
console.log(`Executed ${results.total} tests`);
console.log(`Passed: ${results.passed}, Failed: ${results.failed}`);
// Aggregate results across suites
const aggregated = await aggregateResults(results);
// Generate HTML report
await generateReport({
results: aggregated,
format: 'html',
output: './reports/test-results.html'
});
```
### Streaming Progress Updates
```typescript
import { executeWithProgress } from './servers/qe-tools/test-execution';
// Execute with real-time progress
for await (const event of executeWithProgress({
testSuites: ['./tests/**/*.test.ts']
})) {
if (event.type === 'progress') {
console.log(`Progress: ${event.percent}% - ${event.message}`);
} else if (event.type === 'test-complete') {
console.log(`✓ ${event.testName} (${event.duration}ms)`);
} else if (event.type === 'result') {
console.log('Final results:', event.data);
}
}
```
### Selective Test Execution
```typescript
import { selectTests, executeTests } from './servers/qe-tools/test-execution';
// Select tests based on code changes
const changedFiles = ['src/UserService.ts', 'src/AuthService.ts'];
const selectedTests = await selectTests({
changedFiles,
strategy: 'impact-analysis',
includeRelated: true
});
console.log(`Selected ${selectedTests.length} tests based on changes`);
// Execute only selected tests
const results = await executeTests({
testFiles: selectedTests,
parallel: true
});
```
### Discover Available Tools
```bash
# List test execution tools
ls ./servers/qe-tools/test-execution/
# Check supported frameworks
cat ./servers/qe-tools/test-execution/frameworks.json
# View execution strategies
cat ./servers/qe-tools/test-execution/strategies.json
```
EOF
;;
qe-coverage-analyzer)
cat >> "$temp_file" << 'EOF'
## Code Execution Workflows
Analyze test coverage with O(log n) algorithms for real-time gap detection.
### Comprehensive Coverage Analysis
```typescript
import {
analyzeCoverage,
detectGaps,
prioritizeGaps,
recommendTests
} from './servers/qe-tools/coverage';
// Analyze current coverage
const coverage = await analyzeCoverage({
coverageFile: './coverage/coverage-final.json',
sourceRoot: './src',
threshold: {
statements: 95,
branches: 90,
functions: 95,
lines: 95
}
});
console.log('Coverage Summary:');
console.log(` Statements: ${coverage.statements}%`);
console.log(` Branches: ${coverage.branches}%`);
console.log(` Functions: ${coverage.functions}%`);
console.log(` Lines: ${coverage.lines}%`);
// Detect gaps using sublinear algorithms
const gaps = await detectGaps({
coverage,
algorithm: 'sublinear', // O(log n) detection
minSeverity: 'medium'
});
console.log(`Found ${gaps.length} coverage gaps`);
// Prioritize gaps by risk
const prioritized = await prioritizeGaps({
gaps,
factors: ['complexity', 'criticalPath', 'changeFrequency']
});
// Recommend tests to fill gaps
const recommendations = await recommendTests({
gaps: prioritized,
maxTests: 10
});
console.log('Test Recommendations:');
recommendations.forEach(rec => {
console.log(` - ${rec.testName} (priority: ${rec.priority})`);
});
```
### Real-Time Gap Detection
```typescript
import { watchCoverage } from './servers/qe-tools/coverage';
// Watch for coverage changes
for await (const update of watchCoverage({
coverageDir: './coverage',
interval: 5000
})) {
console.log(`Coverage update: ${update.statements}%`);
if (update.newGaps.length > 0) {
console.log(`⚠️ New gaps detected: ${update.newGaps.length}`);
update.newGaps.forEach(gap => {
console.log(` - ${gap.file}:${gap.line} (${gap.type})`);
});
}
}
```
### Critical Path Analysis
```typescript
import { analyzeCriticalPaths } from './servers/qe-tools/coverage';
// Identify critical paths needing coverage
const criticalPaths = await analyzeCriticalPaths({
sourceRoot: './src',
entryPoints: ['src/index.ts', 'src/api/server.ts'],
coverage
});
console.log('Critical Paths:');
criticalPaths.forEach(path => {
console.log(` ${path.name}: ${path.coverage}% covered`);
if (path.coverage < 95) {
console.log(` ⚠️ Needs ${path.missingTests.length} more tests`);
}
});
```
### Discover Available Tools
```bash
# List coverage analysis tools
ls ./servers/qe-tools/coverage/
# Check available algorithms
cat ./servers/qe-tools/coverage/algorithms.json
# View gap detection strategies
cat ./servers/qe-tools/coverage/strategies.json
```
EOF
;;
qe-quality-gate)
cat >> "$temp_file" << 'EOF'
## Code Execution Workflows
Automated quality gate validation with risk assessment and policy enforcement.
### Comprehensive Quality Gate Check
```typescript
import {
validateQualityGate,
assessRisk,
checkPolicies,
generateReport
} from './servers/qe-tools/quality-gate';
// Define quality gate policies
const policies = {
coverage: { statements: 95, branches: 90 },
security: { maxVulnerabilities: 0, maxCritical: 0 },
performance: { maxResponseTime: 200, maxMemory: 512 },
complexity: { maxCyclomaticComplexity: 15 },
duplication: { maxDuplicateLines: 3 }
};
// Run quality gate validation
const result = await validateQualityGate({
policies,
sources: {
coverage: './coverage/coverage-final.json',
security: './reports/security-scan.json',
performance: './reports/performance.json',
complexity: './reports/complexity.json'
}
});
console.log('Quality Gate Result:', result.status);
console.log(`Passed: ${result.passed.length} checks`);
console.log(`Failed: ${result.failed.length} checks`);
// Assess deployment risk
const risk = await assessRisk({
qualityGate: result,
historicalData: './data/historical-quality.json'
});
console.log(`Deployment Risk: ${risk.level} (${risk.score}/100)`);
if (result.status === 'FAILED') {
console.error('❌ Quality gate failed. Deployment blocked.');
result.failed.forEach(check => {
console.error(` - ${check.name}: ${check.message}`);
});
process.exit(1);
} else {
console.log('✅ Quality gate passed. Deployment approved.');
}
```
### Policy Validation
```typescript
import { validatePolicies } from './servers/qe-tools/quality-gate';
// Load custom policies
const policies = require('./policies/quality-gate.json');
// Validate against policies
const validation = await validatePolicies({
policies,
metrics: {
testCoverage: 96,
codeComplexity: 12,
securityIssues: 0,
performance: { avgResponseTime: 150 }
}
});
validation.results.forEach(result => {
const icon = result.passed ? '✅' : '❌';
console.log(`${icon} ${result.policy}: ${result.message}`);
});
```
### Automated Go/No-Go Decision
```typescript
import { makeDeploymentDecision } from './servers/qe-tools/quality-gate';
// Make automated deployment decision
const decision = await makeDeploymentDecision({
qualityGate: result,
risk: risk,
environment: 'production',
requireApproval: true
});
console.log('Deployment Decision:', decision.recommendation);
console.log('Confidence:', decision.confidence);
console.log('Reasoning:', decision.reasoning);
```
### Discover Available Tools
```bash
# List quality gate tools
ls ./servers/qe-tools/quality-gate/
# View available policies
cat ./servers/qe-tools/quality-gate/policies.json
# Check risk models
cat ./servers/qe-tools/quality-gate/risk-models.json
```
EOF
;;
*)
# Generic template for other agents
cat >> "$temp_file" << EOF
## Code Execution Workflows
Write code to orchestrate ${agent_name#qe-} workflows programmatically.
### Basic Workflow
\`\`\`typescript
import { /* tools */ } from './servers/qe-tools/${agent_name#qe-}';
// Example workflow code
const result = await executeWorkflow({
// workflow parameters
});
console.log('Workflow completed:', result);
\`\`\`
### Discover Available Tools
\`\`\`bash
# List available tools
ls ./servers/qe-tools/${agent_name#qe-}/
# Search for specific functionality
./servers/qe-tools/search_tools.ts "keyword"
\`\`\`
EOF
;;
esac
# Append remaining content (Memory Namespace and beyond)
awk '
/## Memory Namespace/ { found=1 }
found { print }
' "$agent_file" >> "$temp_file"
# Replace original file
mv "$temp_file" "$agent_file"
echo -e " ${GREEN}✓ Added code execution section${NC}"
UPDATED=$((UPDATED + 1))
}
# Process all QE agent files
for agent_file in "$AGENT_DIR"/qe-*.md; do
if [ ! -f "$agent_file" ]; then
continue
fi
TOTAL=$((TOTAL + 1))
add_code_execution_section "$agent_file"
echo ""
done
# Summary
echo "========================================="
echo "📊 Summary"
echo "========================================="
echo ""
echo "Total agents processed: $TOTAL"
echo -e "${GREEN}✓ Updated: $UPDATED${NC}"
echo -e "${YELLOW}⚠️ Skipped: $SKIPPED${NC}"
echo ""
if [ $UPDATED -gt 0 ]; then
echo "💾 Backups created: .claude/agents/qe-*.md.backup-phase2-*"
echo ""
echo "🎯 Phase 2 Benefits:"
echo " • 98.7% token reduction (150K → 2K tokens)"
echo " • 352x faster with Agent Booster WASM"
echo " • Code execution instead of multiple tool calls"
echo ""
echo "✅ All agents now have code execution examples"
echo "✅ Agents can write code to orchestrate workflows"
echo "✅ Follows Anthropic MCP best practices"
fi
echo ""
echo "📚 Reference:"
echo " https://docs.anthropic.com/en/docs/agents-and-tools/agent-skills/best-practices"