Examples of using gnaw's AI-powered agent mode for intelligent code analysis and understanding.
Find and understand functions in your codebase:
# Build index for semantic search
gnaw agent index build
# Find authentication functions
gnaw agent ask "authentication functions"
# Find error handling functions
gnaw agent ask "error handling functions" --type rs
# Find database-related functions
gnaw agent ask "database functions" --dir src/db
Query: authentication functions
→ src/auth.rs (score: 0.95)
Lines 15-25
• Function definition matches query
• Contains authentication keywords
• High semantic similarity
pub fn authenticate_user(token: &str) -> Result<User, AuthError> {
// Authentication logic here
}
→ src/middleware.rs (score: 0.87)
Lines 45-55
• Middleware function for authentication
• Contains auth-related logic
pub fn auth_middleware(req: &Request) -> Result<(), AuthError> {
// Middleware logic here
}
Identify common coding patterns:
# Find async/await patterns
gnaw agent ask "async await patterns" --type js,ts
# Find error handling patterns
gnaw agent ask "error handling with try catch" --type js
# Find API endpoint patterns
gnaw agent ask "API endpoint definitions" --type rs,go
Understand how code components relate:
# Find functions that call authentication
gnaw agent ask "functions that call authentication"
# Find error handling dependencies
gnaw agent ask "error handling dependencies"
# Find database connection usage
gnaw agent ask "database connection usage"
Analyze system architecture:
# Find microservice boundaries
gnaw agent ask "microservice boundaries"
# Find API gateway patterns
gnaw agent ask "API gateway patterns"
# Find service discovery
gnaw agent ask "service discovery patterns"
Identify design patterns:
# Find singleton patterns
gnaw agent ask "singleton pattern implementation"
# Find factory patterns
gnaw agent ask "factory pattern implementation"
# Find observer patterns
gnaw agent ask "observer pattern implementation"
Find potential security issues:
# Find hardcoded credentials
gnaw agent ask "hardcoded credentials or secrets"
# Find SQL injection vulnerabilities
gnaw agent ask "SQL injection vulnerabilities"
# Find authentication bypasses
gnaw agent ask "authentication bypass patterns"
Identify security implementations:
# Find encryption usage
gnaw agent ask "encryption and cryptographic functions"
# Find input validation
gnaw agent ask "input validation and sanitization"
# Find access control
gnaw agent ask "access control and authorization"
Identify performance issues:
# Find slow database queries
gnaw agent ask "slow database queries and N+1 problems"
# Find memory leaks
gnaw agent ask "memory leak patterns"
# Find inefficient algorithms
gnaw agent ask "inefficient algorithms and O(n²) complexity"
Find optimization opportunities:
# Find caching opportunities
gnaw agent ask "caching and memoization opportunities"
# Find parallel processing opportunities
gnaw agent ask "parallel processing and concurrency"
# Find database optimization
gnaw agent ask "database optimization and indexing"
Analyze test coverage:
# Find untested functions
gnaw agent ask "untested functions and methods"
# Find test patterns
gnaw agent ask "test patterns and testing strategies"
# Find mock usage
gnaw agent ask "mock objects and test doubles"
Assess test quality:
# Find integration tests
gnaw agent ask "integration test patterns"
# Find unit test patterns
gnaw agent ask "unit test patterns and best practices"
# Find test data management
gnaw agent ask "test data management and fixtures"
Analyze code documentation:
# Find undocumented functions
gnaw agent ask "undocumented functions and methods"
# Find documentation patterns
gnaw agent ask "documentation patterns and examples"
# Find API documentation
gnaw agent ask "API documentation and OpenAPI"
Identify knowledge gaps:
# Find complex code sections
gnaw agent ask "complex code sections that need documentation"
# Find business logic
gnaw agent ask "business logic and domain knowledge"
# Find configuration management
gnaw agent ask "configuration management and environment setup"
Integrate with CI/CD pipelines:
#!/bin/bash
# CI/CD script for code analysis
echo "Building code index..."
gnaw agent index build
echo "Analyzing code quality..."
gnaw agent ask "code quality issues" --json > quality-report.json
echo "Checking for security issues..."
gnaw agent ask "security vulnerabilities" --json > security-report.json
echo "Analyzing performance..."
gnaw agent ask "performance bottlenecks" --json > performance-report.json
Generate automated reports:
#!/bin/bash
# Generate weekly code analysis report
DATE=$(date +%Y-%m-%d)
REPORT_DIR="reports/$DATE"
mkdir -p "$REPORT_DIR"
# Analyze different aspects
gnaw agent ask "authentication functions" --json > "$REPORT_DIR/auth-analysis.json"
gnaw agent ask "error handling patterns" --json > "$REPORT_DIR/error-analysis.json"
gnaw agent ask "performance issues" --json > "$REPORT_DIR/performance-analysis.json"
# Generate summary
echo "Code Analysis Report - $DATE" > "$REPORT_DIR/summary.txt"
echo "Authentication functions: $(jq '.results | length' "$REPORT_DIR/auth-analysis.json")" >> "$REPORT_DIR/summary.txt"
echo "Error handling patterns: $(jq '.results | length' "$REPORT_DIR/error-analysis.json")" >> "$REPORT_DIR/summary.txt"
echo "Performance issues: $(jq '.results | length' "$REPORT_DIR/performance-analysis.json")" >> "$REPORT_DIR/summary.txt"
Integrate with development environments:
#!/bin/bash
# VS Code extension script
QUERY="$1"
FILE_PATH="$2"
# Get semantic search results
gnaw agent ask "$QUERY" --dir "$FILE_PATH" --json | jq '.results[] | {file: .file, snippet: .snippet, score: .score}'
Assist with code migration:
# Find deprecated patterns
gnaw agent ask "deprecated patterns and legacy code"
# Find migration opportunities
gnaw agent ask "migration opportunities and refactoring"
# Find compatibility issues
gnaw agent ask "compatibility issues and breaking changes"
Assist with code reviews:
# Find potential issues
gnaw agent ask "potential issues and code smells"
# Find best practices
gnaw agent ask "best practices and coding standards"
# Find code duplication
gnaw agent ask "code duplication and DRY violations"
Facilitate knowledge transfer:
# Find expert knowledge
gnaw agent ask "expert knowledge and domain expertise"
# Find learning opportunities
gnaw agent ask "learning opportunities and skill development"
# Find mentoring opportunities
gnaw agent ask "mentoring opportunities and knowledge sharing"