Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 09:07:22 +08:00
commit fab98d059b
179 changed files with 46209 additions and 0 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,369 @@
# Build Quality Gates - Implementation Patterns
This document captures the key patterns and practices discovered during the BAIME build-quality-gates experiment.
## Three-Layer Architecture Pattern
### P0: Critical Checks (Pre-commit)
**Purpose**: Block commits that would definitely fail CI
**Target**: <10 seconds, 50-70% error coverage
**Examples**: Temporary files, dependency issues, test fixtures
```makefile
check-workspace: check-temp-files check-fixtures check-deps
@echo "✅ Workspace validation passed"
```
### P1: Enhanced Checks (Quality Assurance)
**Purpose**: Ensure code quality and team standards
**Target**: <30 seconds, 80-90% error coverage
**Examples**: Script validation, import formatting, debug statements
```makefile
check-quality: check-workspace check-scripts check-imports check-debug
@echo "✅ Quality validation passed"
```
### P2: Advanced Checks (Comprehensive)
**Purpose**: Full validation for important changes
**Target**: <60 seconds, 95%+ error coverage
**Examples**: Language-specific quality, security, performance
```makefile
check-full: check-quality check-security check-performance
@echo "✅ Comprehensive validation passed"
```
## Script Structure Pattern
### Standard Template
```bash
#!/bin/bash
# check-[category].sh - [One-line description]
#
# Part of: Build Quality Gates
# Iteration: [P0/P1/P2]
# Purpose: [What problems this prevents]
# Historical Impact: [X% of errors this catches]
set -euo pipefail
# Colors for consistent output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m'
echo "Checking [category]..."
ERRORS=0
# ... check logic ...
# Summary
if [ $ERRORS -eq 0 ]; then
echo -e "${GREEN}✅ All [category] checks passed${NC}"
exit 0
else
echo -e "${RED}❌ Found $ERRORS [category] issue(s)${NC}"
exit 1
fi
```
## Error Message Pattern
### Clear, Actionable Messages
```
❌ ERROR: Temporary test/debug scripts found:
- ./test_parser.go
- ./debug_analyzer.go
Action: Delete these temporary files before committing
To fix:
1. Delete temporary files: rm test_*.go debug_*.go
2. Move legitimate files to appropriate packages
3. Run again: make check-temp-files
```
### Message Components
1. **Clear problem statement** in red
2. **Specific items found** with paths
3. **Required action** clearly stated
4. **Step-by-step fix instructions**
5. **Verification command** to re-run
## Performance Optimization Patterns
### Parallel Execution
```makefile
check-parallel:
@make check-temp-files & \
make check-fixtures & \
make check-deps & \
wait
@echo "✅ Parallel checks completed"
```
### Incremental Checking
```bash
check-incremental:
@if [ -n "$(git status --porcelain)" ]; then
CHANGED=$$(git diff --name-only --cached);
echo "Checking changed files: $$CHANGED";
# Run checks only on changed files
else
$(MAKE) check-workspace
fi
```
### Caching Strategy
```bash
# Use Go test cache
go test -short ./...
# Cache expensive operations
CACHE_DIR=.cache/check-deps
if [ ! -f "$CACHE_DIR/verified" ]; then
go mod verify
touch "$CACHE_DIR/verified"
fi
```
## Integration Patterns
### Makefile Structure
```makefile
# =============================================================================
# Build Quality Gates - Three-Layer Architecture
# =============================================================================
# P0: Critical checks (must pass before commit)
check-workspace: check-temp-files check-fixtures check-deps
@echo "✅ Workspace validation passed"
# P1: Enhanced checks (quality assurance)
check-quality: check-workspace check-scripts check-imports check-debug
@echo "✅ Quality validation passed"
# P2: Advanced checks (comprehensive validation)
check-full: check-quality check-security check-performance
@echo "✅ Comprehensive validation passed"
# =============================================================================
# Workflow Targets
# =============================================================================
# Development iteration (fastest)
dev: fmt build
@echo "✅ Development build complete"
# Pre-commit validation (recommended)
pre-commit: check-workspace test-short
@echo "✅ Pre-commit checks passed"
# Full validation (before important commits)
all: check-quality test-full build-all
@echo "✅ Full validation passed"
# CI-level validation
ci: check-full test-all build-all verify
@echo "✅ CI validation passed"
```
### CI/CD Integration Pattern
```yaml
# GitHub Actions
- name: Run quality gates
run: make ci
# GitLab CI
script:
- make ci
# Jenkins
sh 'make ci'
```
## Tool Chain Management Patterns
### Version Consistency
```bash
# Pin versions in configuration
.golangci.yml: version: "1.64.8"
.tool-versions: golangci-lint 1.64.8
```
### Docker-based Toolchains
```dockerfile
FROM golang:1.21.0
RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.8
RUN go install golang.org/x/tools/cmd/goimports@latest
```
### Cross-Platform Compatibility
```bash
# Use portable tools
find . -name "*.go" # instead of platform-specific tools
grep -r "TODO" . # instead of IDE-specific search
```
## Quality Metrics Patterns
### V_instance Calculation
```bash
V_instance=$(echo "scale=3;
0.4 * (1 - $ci_failure_rate) +
0.3 * (1 - $avg_iterations/$baseline_iterations) +
0.2 * ($baseline_time/$detection_time/10) +
0.1 * $error_coverage" | bc)
```
### Metrics Collection
```bash
# Automated metrics collection
collect_metrics() {
local ci_failure_rate=$(get_ci_failure_rate)
local detection_time=$(measure_detection_time)
local error_coverage=$(calculate_error_coverage)
# Calculate and store metrics
}
```
### Trend Monitoring
```python
# Plot quality trends over time
def plot_metrics_trend(metrics_data):
# Visualize V_instance and V_meta improvement
# Show convergence toward targets
pass
```
## Error Handling Patterns
### Graceful Degradation
```bash
# Continue checking even if one check fails
ERRORS=0
check_temp_files || ERRORS=$((ERRORS + 1))
check_fixtures || ERRORS=$((ERRORS + 1))
if [ $ERRORS -gt 0 ]; then
echo "Found $ERRORS issues"
exit 1
fi
```
### Tool Availability
```bash
# Handle missing optional tools
if command -v goimports >/dev/null; then
goimports -l .
else
echo "⚠️ goimports not available, skipping import check"
fi
```
### Clear Exit Codes
```bash
# 0: Success
# 1: Errors found
# 2: Configuration issues
# 3: Tool not available
```
## Team Adoption Patterns
### Gradual Enforcement
```bash
# Start with warnings
if [ "${ENFORCE_QUALITY:-false}" = "true" ]; then
make check-workspace-strict
else
make check-workspace-warning
fi
```
### Easy Fix Commands
```bash
# Provide one-command fixes
fix-imports:
@echo "Fixing imports..."
@goimports -w .
@echo "✅ Imports fixed"
fix-temp-files:
@echo "Removing temporary files..."
@rm -f test_*.go debug_*.go
@echo "✅ Temporary files removed"
```
### Documentation Integration
```bash
# Link to documentation in error messages
echo "See: docs/guides/build-quality-gates.md#temporary-files"
```
## Maintenance Patterns
### Regular Updates
```bash
# Monthly tool updates
update-quality-tools:
@echo "Updating quality gate tools..."
@go install -a github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@make check-full && echo "✅ Tools updated successfully"
```
### Performance Monitoring
```bash
# Benchmark performance regularly
benchmark-quality-gates:
@for i in {1..10}; do
time make check-full 2>&1 | grep real
done
```
### Feedback Collection
```bash
# Collect team feedback
collect-quality-feedback:
@echo "Please share your experience with quality gates:"
@echo "1. What's working well?"
@echo "2. What's frustrating?"
@echo "3. Suggested improvements?"
```
## Anti-Patterns to Avoid
### ❌ Don't Do This
```bash
# Too strict - blocks legitimate work
if [ -n "$(git status --porcelain)" ]; then
echo "Working directory must be clean"
exit 1
fi
# Too slow - developers won't use it
make check-slow-heavy-analysis # Takes 5+ minutes
# Unclear errors - developers don't know how to fix
echo "❌ Code quality issues found"
exit 1
```
### ✅ Do This Instead
```bash
# Flexible - allows legitimate work
if [ -n "$(find . -name "*.tmp")" ]; then
echo "❌ Temporary files found"
echo "Remove: find . -name '*.tmp' -delete"
fi
# Fast - developers actually use it
make check-quick-essentials # <30 seconds
# Clear errors - developers can fix immediately
echo "❌ Import formatting issues in:"
echo " - internal/parser.go"
echo "Fix: goimports -w ."
```

View File

@@ -0,0 +1,110 @@
#!/bin/bash
# benchmark-performance.sh - Performance regression testing for quality gates
#
# Part of: Build Quality Gates Implementation
# Purpose: Ensure quality gates remain fast and efficient
set -euo pipefail
# Colors
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
ITERATIONS=5
TARGET_SECONDS=60
RESULTS_FILE="performance-benchmark-$(date +%Y%m%d-%H%M%S).csv"
echo "Quality Gates Performance Benchmark"
echo "=================================="
echo "Target: <${TARGET_SECONDS}s per run"
echo "Iterations: $ITERATIONS"
echo ""
# Initialize results file
echo "Iteration,Time_Seconds,Status" > "$RESULTS_FILE"
# Run benchmarks
TOTAL_TIME=0
FAILED_RUNS=0
for i in $(seq 1 $ITERATIONS); do
echo -n "Run $i/$ITERATIONS... "
start_time=$(date +%s.%N)
if make check-full >/dev/null 2>&1; then
end_time=$(date +%s.%N)
duration=$(echo "$end_time - $start_time" | bc)
status="SUCCESS"
echo -e "${GREEN}${NC} ${duration}s"
else
end_time=$(date +%s.%N)
duration=$(echo "$end_time - $start_time" | bc)
status="FAILED"
echo -e "${RED}${NC} ${duration}s (failed)"
((FAILED_RUNS++)) || true
fi
TOTAL_TIME=$(echo "$TOTAL_TIME + $duration" | bc)
echo "$i,$duration,$status" >> "$RESULTS_FILE"
done
# Calculate statistics
avg_time=$(echo "scale=2; $TOTAL_TIME / $ITERATIONS" | bc)
success_rate=$(echo "scale=1; ($ITERATIONS - $FAILED_RUNS) * 100 / $ITERATIONS" | bc)
echo ""
echo "Results Summary"
echo "==============="
# Performance assessment
if (( $(echo "$avg_time < $TARGET_SECONDS" | bc -l) )); then
echo -e "Average Time: ${GREEN}${avg_time}s${NC} ✅ Within target"
else
echo -e "Average Time: ${RED}${avg_time}s${NC} ❌ Exceeds target of ${TARGET_SECONDS}s"
fi
echo "Success Rate: ${success_rate}% ($(($ITERATIONS - $FAILED_RUNS))/$ITERATIONS)"
echo "Results saved to: $RESULTS_FILE"
# Performance trend analysis (if previous results exist)
LATEST_RESULT=$(echo "$avg_time")
if [ -f "latest-performance.txt" ]; then
PREVIOUS_RESULT=$(cat latest-performance.txt)
CHANGE=$(echo "scale=2; ($LATEST_RESULT - $PREVIOUS_RESULT) / $PREVIOUS_RESULT * 100" | bc)
if (( $(echo "$CHANGE > 5" | bc -l) )); then
echo -e "${YELLOW}⚠️ Performance degraded by ${CHANGE}%${NC}"
elif (( $(echo "$CHANGE < -5" | bc -l) )); then
echo -e "${GREEN}✓ Performance improved by ${ABS_CHANGE}%${NC}"
else
echo "Performance stable (±5%)"
fi
fi
echo "$LATEST_RESULT" > latest-performance.txt
# Recommendations
echo ""
echo "Recommendations"
echo "==============="
if (( $(echo "$avg_time > $TARGET_SECONDS" | bc -l) )); then
echo "⚠️ Performance exceeds target. Consider:"
echo " • Parallel execution of independent checks"
echo " • Caching expensive operations"
echo " • Incremental checking for changed files only"
echo " • Optimizing slow individual checks"
elif [ $FAILED_RUNS -gt 0 ]; then
echo "⚠️ Some runs failed. Investigate:"
echo " • Check intermittent failures"
echo " • Review error logs for patterns"
echo " • Consider environmental factors"
else
echo "✅ Performance is within acceptable range"
fi
exit $FAILED_RUNS

View File

@@ -0,0 +1,121 @@
#!/bin/bash
# check-temp-files.sh - Detect temporary files that should not be committed
#
# Part of: Build Quality Gates (BAIME Experiment)
# Iteration: 1 (P0)
# Purpose: Prevent commit of temporary test/debug files
# Historical Impact: Catches 28% of commit errors
set -euo pipefail
# Colors
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m'
echo "Checking for temporary files..."
ERRORS=0
# ============================================================================
# Check 1: Root directory .go files (except main.go)
# ============================================================================
echo " [1/4] Checking root directory for temporary .go files..."
TEMP_GO=$(find . -maxdepth 1 -name "*.go" ! -name "main.go" -type f 2>/dev/null || true)
if [ -n "$TEMP_GO" ]; then
echo -e "${RED}❌ ERROR: Temporary .go files in project root:${NC}"
echo "$TEMP_GO" | sed 's/^/ - /'
echo ""
echo "These files should be:"
echo " 1. Moved to appropriate package directories (e.g., cmd/, internal/)"
echo " 2. Or deleted if they are debug/test scripts"
echo ""
((ERRORS++)) || true
fi
# ============================================================================
# Check 2: Common temporary file patterns
# ============================================================================
echo " [2/4] Checking for test/debug script patterns..."
TEMP_SCRIPTS=$(find . -type f \( \
-name "test_*.go" -o \
-name "debug_*.go" -o \
-name "tmp_*.go" -o \
-name "scratch_*.go" -o \
-name "experiment_*.go" \
\) ! -path "./vendor/*" ! -path "./.git/*" ! -path "*/temp_file_manager*.go" 2>/dev/null || true)
if [ -n "$TEMP_SCRIPTS" ]; then
echo -e "${RED}❌ ERROR: Temporary test/debug scripts found:${NC}"
echo "$TEMP_SCRIPTS" | sed 's/^/ - /'
echo ""
echo "Action: Delete these temporary files before committing"
echo ""
((ERRORS++)) || true
fi
# ============================================================================
# Check 3: Editor temporary files
# ============================================================================
echo " [3/4] Checking for editor temporary files..."
EDITOR_TEMP=$(find . -type f \( \
-name "*~" -o \
-name "*.swp" -o \
-name ".*.swp" -o \
-name "*.swo" -o \
-name "#*#" \
\) ! -path "./.git/*" 2>/dev/null | head -10 || true)
if [ -n "$EDITOR_TEMP" ]; then
echo -e "${YELLOW}⚠️ WARNING: Editor temporary files found:${NC}"
echo "$EDITOR_TEMP" | sed 's/^/ - /'
echo ""
echo "These files should be in .gitignore"
echo "(Not blocking, but recommended to clean up)"
echo ""
fi
# ============================================================================
# Check 4: Compiled binaries in root
# ============================================================================
echo " [4/4] Checking for compiled binaries..."
BINARIES=$(find . -maxdepth 1 -type f \( \
-name "meta-cc" -o \
-name "meta-cc-mcp" -o \
-name "*.exe" \
\) 2>/dev/null || true)
if [ -n "$BINARIES" ]; then
echo -e "${YELLOW}⚠️ WARNING: Compiled binaries in root directory:${NC}"
echo "$BINARIES" | sed 's/^/ - /'
echo ""
echo "These should be in .gitignore or build/"
echo "(Not blocking, but verify they are not accidentally staged)"
echo ""
fi
# ============================================================================
# Summary
# ============================================================================
echo ""
if [ "$ERRORS" -eq 0 ]; then
echo -e "${GREEN}✅ No temporary files found${NC}"
exit 0
else
echo -e "${RED}❌ Found $ERRORS temporary file issue(s)${NC}"
echo ""
echo "Quick fix:"
echo " # Remove temporary .go files"
echo " find . -maxdepth 2 -name 'test_*.go' -o -name 'debug_*.go' | xargs rm -f"
echo ""
echo " # Update .gitignore"
echo " echo 'test_*.go' >> .gitignore"
echo " echo 'debug_*.go' >> .gitignore"
exit 1
fi

View File

@@ -0,0 +1,70 @@
#!/bin/bash
# check-[category].sh - [One-line description]
#
# Part of: Build Quality Gates
# Iteration: [P0/P1/P2]
# Purpose: [What problems this prevents]
# Historical Impact: [X% of errors this catches]
#
# shellcheck disable=SC2078,SC1073,SC1072,SC1123
# Note: This is a template file with placeholder syntax, not meant to be executed as-is
set -euo pipefail
# Colors for consistent output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
echo "Checking [category]..."
ERRORS=0
WARNINGS=0
# ============================================================================
# Check 1: [Specific check name]
# ============================================================================
echo " [1/N] Checking [specific pattern]..."
# Your validation logic here
if [ condition ]; then
echo -e "${RED}❌ ERROR: [Clear problem description]${NC}"
echo "[Detailed explanation of what was found]"
echo ""
echo "To fix:"
echo " 1. [Specific action step]"
echo " 2. [Specific action step]"
echo " 3. [Verification step]"
echo ""
((ERRORS++)) || true
elif [ warning_condition ]; then
echo -e "${YELLOW}⚠️ WARNING: [Warning description]${NC}"
echo "[Optional improvement suggestion]"
echo ""
((WARNINGS++)) || true
else
echo -e "${GREEN}${NC} [Check passed]"
fi
# ============================================================================
# Continue with more checks...
# ============================================================================
# ============================================================================
# Summary
# ============================================================================
echo ""
if [ $ERRORS -eq 0 ]; then
if [ $WARNINGS -eq 0 ]; then
echo -e "${GREEN}✅ All [category] checks passed${NC}"
else
echo -e "${YELLOW}⚠️ All critical checks passed, $WARNINGS warning(s)${NC}"
fi
exit 0
else
echo -e "${RED}❌ Found $ERRORS [category] error(s), $WARNINGS warning(s)${NC}"
echo "Please fix errors before committing"
exit 1
fi