|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: 2025 Knitli Inc. <[email protected]> |
| 3 | +SPDX-FileContributor: Adam Poulemanos <[email protected]> |
| 4 | +SPDX-License-Identifier: MIT OR Apache-2.0 |
| 5 | +--> |
| 6 | + |
| 7 | +# Copilot Instructions for Thread |
| 8 | + |
| 9 | +## Project Overview |
| 10 | + |
| 11 | +Thread is a Rust code analysis engine that provides intelligent context for AI assistants. The project is transitioning from vendored ast-grep CLI code to a multi-environment service architecture supporting CLI, cloud, WASM, and CI/CD deployments. |
| 12 | + |
| 13 | +## Architecture Guidelines |
| 14 | + |
| 15 | +### Service Layer Pattern |
| 16 | +The codebase follows a service abstraction pattern to support multiple environments: |
| 17 | + |
| 18 | +```rust |
| 19 | +// Pure service functions (environment-agnostic) |
| 20 | +pub async fn scan_with_services( |
| 21 | + file_discovery: Arc<dyn FileDiscoveryService>, |
| 22 | + config_service: Arc<dyn ConfigurationService>, |
| 23 | + options: ScanOptions, |
| 24 | +) -> Result<ScanResults> |
| 25 | +``` |
| 26 | + |
| 27 | +### Crate Organization |
| 28 | + |
| 29 | +- **ag-thread/**: Vendored ast-grep modules being refactored for service layers |
| 30 | +- **thread-core/**: Core traits, types, and errors (pure abstractions) |
| 31 | +- **thread-engine/**: Main analysis implementation using petgraph |
| 32 | +- **thread-parse/**: AST-grep integration and language detection |
| 33 | +- **Service-ready crates**: `ag-core`, `ag-search`, `ag-fix`, `ag-types`, `ag-label` |
| 34 | +- **Needs refactoring**: `ag-scan`, `ag-utils`, `ag-check-rule` (heavy CLI dependencies) |
| 35 | +
|
| 36 | +## Development Commands |
| 37 | +
|
| 38 | +Essential commands for this workspace: |
| 39 | +
|
| 40 | +```bash |
| 41 | +# Build all crates (except WASM) |
| 42 | +mise run build |
| 43 | +mise run b |
| 44 | +
|
| 45 | +# WASM builds |
| 46 | +mise run build-wasm # Development (single-threaded) |
| 47 | +mise run build-wasm-release # Production optimized |
| 48 | +
|
| 49 | +# Testing and quality |
| 50 | +mise run test # Tests with cargo nextest |
| 51 | +mise run lint # Full linting via hk run check |
| 52 | +mise run ci # All CI checks |
| 53 | +``` |
| 54 | +
|
| 55 | +## Key Patterns to Follow |
| 56 | +
|
| 57 | +### 1. Service Trait Definitions |
| 58 | +When creating new services, follow the pattern from `ag-types`: |
| 59 | +
|
| 60 | +```rust |
| 61 | +#[async_trait] |
| 62 | +pub trait YourService: Send + Sync { |
| 63 | + async fn your_method(&self, input: &str) -> Result<Output>; |
| 64 | +} |
| 65 | +``` |
| 66 | +
|
| 67 | +### 2. Environment-Agnostic Core Functions |
| 68 | +Avoid CLI dependencies in core logic: |
| 69 | +
|
| 70 | +```rust |
| 71 | +// ✅ Good: Pure function with injected services |
| 72 | +pub async fn analyze_with_services( |
| 73 | + content: String, |
| 74 | + services: &ServiceRegistry |
| 75 | +) -> Result<AnalysisResult> |
| 76 | +
|
| 77 | +// ❌ Avoid: Direct filesystem or terminal access |
| 78 | +pub fn analyze_files(paths: Vec<PathBuf>) -> Result<()> |
| 79 | +``` |
| 80 | +
|
| 81 | +### 3. Multi-Environment Support |
| 82 | +Structure implementations for different environments: |
| 83 | +
|
| 84 | +```rust |
| 85 | +// CLI implementation |
| 86 | +impl YourService for CliYourService { /* uses std::fs */ } |
| 87 | +
|
| 88 | +// Cloud implementation |
| 89 | +impl YourService for CloudYourService { /* uses S3/HTTP */ } |
| 90 | +
|
| 91 | +// WASM implementation |
| 92 | +impl YourService for WasmYourService { /* uses fetch API */ } |
| 93 | +``` |
| 94 | +
|
| 95 | +## CLI Dependencies Analysis Status |
| 96 | +
|
| 97 | +Refer to individual `CLI_DEPENDENCIES.md` files in each ag-thread crate: |
| 98 | +
|
| 99 | +- **Immediate attention needed**: `ag-scan/`, `ag-utils/` (heavy CLI dependencies) |
| 100 | +- **Service-ready**: `ag-core/`, `ag-search/`, `ag-fix/`, `ag-types/`, `ag-label/` |
| 101 | +- **Minor refactoring**: `ag-rule/`, `ag-check-rule/` |
| 102 | +
|
| 103 | +## Critical Abstractions |
| 104 | +
|
| 105 | +### File Operations |
| 106 | +Replace direct filesystem access with service traits: |
| 107 | +```rust |
| 108 | +// Instead of std::fs::read_to_string |
| 109 | +let content = file_service.read_file(path).await?; |
| 110 | +``` |
| 111 | +
|
| 112 | +### Terminal I/O |
| 113 | +Replace direct terminal access with service traits: |
| 114 | +```rust |
| 115 | +// Instead of println! or crossterm |
| 116 | +output_service.write(&format!("Result: {}", result)).await?; |
| 117 | +``` |
| 118 | +
|
| 119 | +### Configuration Loading |
| 120 | +Replace direct file config loading: |
| 121 | +```rust |
| 122 | +// Instead of reading YAML files directly |
| 123 | +let rules = config_service.load_rules(ConfigSource::Path(path)).await?; |
| 124 | +``` |
| 125 | +
|
| 126 | +## Testing Strategy |
| 127 | +
|
| 128 | +- Use `cargo nextest -j 1` for parallel tests with race condition prevention |
| 129 | +- Mock service implementations for unit tests |
| 130 | +- Environment-specific integration tests for each service implementation |
| 131 | +- `RUST_BACKTRACE=1` enabled for debugging |
| 132 | +
|
| 133 | +## WASM Considerations |
| 134 | +
|
| 135 | +- Default builds are single-threaded for Cloudflare Workers compatibility |
| 136 | +- Core logic separated from filesystem operations for WASM portability |
| 137 | +- Multi-threaded builds available for browser environments (`--multi-threading`) |
| 138 | +
|
| 139 | +When working with WASM targets, ensure no direct filesystem or process dependencies in core libraries. |
| 140 | +
|
| 141 | +## Current Development Focus |
| 142 | +
|
| 143 | +**Week 1 Sprint**: Establishing service layer foundations |
| 144 | +- Refactoring `ag-scan` to use service abstractions |
| 145 | +- Creating `ag-services` crate with core trait definitions |
| 146 | +- Implementing CLI service adapters to maintain current functionality |
| 147 | +
|
| 148 | +The goal is to enable Thread to analyze code and provide AI-friendly context across all deployment environments while maintaining the performance and functionality of the original ast-grep implementation. |
0 commit comments