|
| 1 | +--- |
| 2 | +status: planned |
| 3 | +created: '2025-12-18' |
| 4 | +tags: |
| 5 | + - migration |
| 6 | + - rust |
| 7 | + - backend |
| 8 | + - architecture |
| 9 | + - rewrite |
| 10 | +priority: high |
| 11 | +assignee: marvin |
| 12 | +created_at: '2025-12-18T01:00:49.397Z' |
| 13 | +--- |
| 14 | + |
| 15 | +# Migrate Devlog from Go to Rust |
| 16 | + |
| 17 | +> **Status**: 📅 Planned · **Priority**: High · **Created**: 2025-12-18 |
| 18 | +
|
| 19 | +## Overview |
| 20 | + |
| 21 | +Full rewrite of devlog from Go to Rust. Since the project is in early development phase, we'll do a clean rewrite instead of incremental migration. This allows us to leverage Rust's memory safety, performance, and type system without compromises. |
| 22 | + |
| 23 | +**Key Goals:** |
| 24 | +- Clean rewrite with improved architecture |
| 25 | +- Leverage Rust's type system and ownership model |
| 26 | +- Better error handling with Result types |
| 27 | +- Enhanced performance for parsing and buffering operations |
| 28 | +- Maintain functional compatibility (same features, better implementation) |
| 29 | + |
| 30 | +**Scope:** |
| 31 | +- All internal packages (adapters, buffer, watcher, hierarchy, backfill) |
| 32 | +- Main collector service |
| 33 | +- Configuration management |
| 34 | +- Simplified Docker setup |
| 35 | +- Drop-in replacement for existing Go binary |
| 36 | + |
| 37 | +## Design |
| 38 | + |
| 39 | +### Architecture Principles |
| 40 | + |
| 41 | +**1. Clean Rewrite Strategy** |
| 42 | +- No FFI bridge - pure Rust implementation |
| 43 | +- Reference Go implementation for behavior, not code structure |
| 44 | +- Opportunity to improve architecture based on lessons learned |
| 45 | +- Simplify where Go was over-engineered |
| 46 | + |
| 47 | +**2. Type System Leverage** |
| 48 | +- Strong typing for all data structures |
| 49 | +- `Result<T, E>` for error handling (no panics in production code) |
| 50 | +- Use `Option<T>` instead of nullable pointers |
| 51 | +- Leverage Serde for JSON serialization |
| 52 | +- Trait-based polymorphism for cleaner abstractions |
| 53 | + |
| 54 | +**3. Performance Targets** |
| 55 | +- Parsing: 30% faster than current Go implementation |
| 56 | +- Memory: 20% reduction in collector memory footprint |
| 57 | +- Startup: <100ms cold start time |
| 58 | +- Binary size: <20MB (compared to Go's ~15MB) |
| 59 | + |
| 60 | +### Component Mapping |
| 61 | + |
| 62 | +| Go Package | Rust Equivalent | Notes | |
| 63 | +|------------|-----------------|-------| |
| 64 | +| `internal/adapters` | `devlog_adapters` | Use trait system for polymorphism | |
| 65 | +| `internal/buffer` | `devlog_buffer` | Replace channels with async Tokio | |
| 66 | +| `internal/watcher` | `devlog_watcher` | Use `notify` crate for file watching | |
| 67 | +| `internal/hierarchy` | `devlog_hierarchy` | Tree structures with Rc/RefCell | |
| 68 | +| `internal/backfill` | `devlog_backfill` | Async processing with rayon | |
| 69 | +| `cmd/devlog` | `devlog_cli` | Use `clap` for CLI parsing | |
| 70 | + |
| 71 | +### Key Technology Choices |
| 72 | + |
| 73 | +**Async Runtime:** Tokio |
| 74 | +- Industry standard |
| 75 | +- Excellent ecosystem support |
| 76 | +- Compatible with most Rust async libraries |
| 77 | + |
| 78 | +**Error Handling:** `thiserror` + `anyhow` |
| 79 | +- `thiserror` for library errors |
| 80 | +- `anyhow` for application errors |
| 81 | +- Structured error context throughout |
| 82 | + |
| 83 | +**Serialization:** Serde |
| 84 | +- JSON compatibility with existing APIs |
| 85 | +- Better performance than Go's encoding/json |
| 86 | + |
| 87 | +**File Watching:** `notify` |
| 88 | +- Cross-platform file system events |
| 89 | +- Proven reliability |
| 90 | + |
| 91 | +**CLI:** `clap` v4 |
| 92 | +- Declarative API |
| 93 | +- Auto-generated help text |
| 94 | +- Shell completion support |
| 95 | + |
| 96 | +## Plan |
| 97 | + |
| 98 | +### Phase 1: Foundation & Setup (Week 1) |
| 99 | +- [ ] Initialize Rust workspace structure |
| 100 | +- [ ] Set up Cargo workspace with sub-crates |
| 101 | +- [ ] Configure CI/CD for Rust builds |
| 102 | +- [ ] Set up development tooling (clippy, rustfmt, cargo-watch) |
| 103 | +- [ ] Create project structure matching Rust idioms |
| 104 | + |
| 105 | +### Phase 2: Core Types & Models (Week 1-2) |
| 106 | +- [ ] Port `pkg/types` to Rust types |
| 107 | +- [ ] Implement `pkg/models` with Serde support |
| 108 | +- [ ] Create shared error types |
| 109 | +- [ ] Add comprehensive tests for type conversions |
| 110 | +- [ ] Benchmark serialization performance |
| 111 | + |
| 112 | +### Phase 3: Configuration System (Week 2) |
| 113 | +- [ ] Port `internal/config` to Rust |
| 114 | +- [ ] Use `config` crate for YAML/TOML support |
| 115 | +- [ ] Maintain backward compatibility with existing configs |
| 116 | +- [ ] Add config validation with meaningful errors |
| 117 | + |
| 118 | +### Phase 4: Buffer System (Week 2-3) |
| 119 | +- [ ] Implement circular buffer with Tokio channels |
| 120 | +- [ ] Port buffering logic from Go |
| 121 | +- [ ] Add backpressure handling |
| 122 | +- [ ] Performance test against Go implementation |
| 123 | +- [ ] Ensure thread-safety guarantees |
| 124 | + |
| 125 | +### Phase 5: Adapters (Week 3-4) |
| 126 | +- [ ] Design adapter trait system |
| 127 | +- [ ] Port Claude adapter |
| 128 | +- [ ] Port Copilot adapter |
| 129 | +- [ ] Port remaining adapters |
| 130 | +- [ ] Add adapter registry |
| 131 | +- [ ] Integration tests for each adapter |
| 132 | + |
| 133 | +### Phase 6: File Watcher (Week 4) |
| 134 | +- [ ] Implement file system watcher with `notify` crate |
| 135 | +- [ ] Port hierarchy building logic |
| 136 | +- [ ] Add debouncing for file events |
| 137 | +- [ ] Test cross-platform behavior |
| 138 | + |
| 139 | +### Phase 7: Backfill System (Week 4-5) |
| 140 | +- [ ] Port historical sync logic |
| 141 | +- [ ] Implement async batch processing |
| 142 | +- [ ] Add progress tracking |
| 143 | +- [ ] Performance optimization |
| 144 | + |
| 145 | +### Phase 8: Main Collector Service (Week 5-6) |
| 146 | +- [ ] Port main service initialization |
| 147 | +- [ ] Implement HTTP/WebSocket server with `axum` |
| 148 | +- [ ] Add health check endpoints |
| 149 | +- [ ] Port signal handling |
| 150 | +- [ ] Integration testing |
| 151 | + |
| 152 | +### Phase 9: CLI & Tooling (Week 6) |
| 153 | +- [ ] Port CLI commands with `clap` |
| 154 | +- [ ] Add shell completion support |
| 155 | +- [ ] Maintain command compatibility |
| 156 | +- [ ] Update documentation |
| 157 | + |
| 158 | +### Phase 10: Final Testing & Deployment (Week 7) |
| 159 | +- [ ] End-to-end testing against Go version for behavior parity |
| 160 | +- [ ] Performance benchmarking |
| 161 | +- [ ] Memory profiling |
| 162 | +- [ ] Docker image optimization |
| 163 | +- [ ] Update deployment scripts |
| 164 | +- [ ] Remove Go codebase |
| 165 | +- [ ] Update all documentation |
| 166 | +- [ ] Update CONTRIBUTING.md |
| 167 | +- [ ] Final performance validation |
| 168 | + |
| 169 | +## Test |
| 170 | + |
| 171 | +### Unit Testing |
| 172 | +- [ ] All core types have property tests with `proptest` |
| 173 | +- [ ] Adapter implementations tested in isolation |
| 174 | +- [ ] Buffer system stress tested (high load scenarios) |
| 175 | +- [ ] Configuration parsing edge cases covered |
| 176 | +- [ ] Error handling paths validated |
| 177 | + |
| 178 | +### Integration Testing |
| 179 | +- [ ] End-to-end collector pipeline test |
| 180 | +- [ ] File watcher integration with real file operations |
| 181 | +- [ ] Adapter compatibility with actual AI services (mocked) |
| 182 | +- [ ] Backfill process with historical data |
| 183 | +- [ ] CLI commands match Go behavior |
| 184 | + |
| 185 | +### Performance Testing |
| 186 | +- [ ] Parsing throughput ≥ 1.3x Go implementation |
| 187 | +- [ ] Memory usage ≤ 0.8x Go implementation |
| 188 | +- [ ] Cold start time < 100ms |
| 189 | +- [ ] Event processing latency < 5ms p99 |
| 190 | +- [ ] Sustained load test (24hr stability) |
| 191 | + |
| 192 | +### Compatibility Testing |
| 193 | +- [ ] Existing config files work without modification |
| 194 | +- [ ] Same functionality as Go version |
| 195 | +- [ ] Docker container works with same docker-compose.yml |
| 196 | +- [ ] Environment variables honored |
| 197 | +- [ ] Signal handling (SIGTERM, SIGINT) works correctly |
| 198 | + |
| 199 | +### Replacement Validation |
| 200 | +- [ ] Functional parity test suite (behavior comparison) |
| 201 | +- [ ] Performance meets or exceeds Go version |
| 202 | +- [ ] Memory usage validated |
| 203 | +- [ ] Deployment smoke tests |
| 204 | + |
| 205 | +## Notes |
| 206 | + |
| 207 | +### Why Rust? |
| 208 | + |
| 209 | +**Memory Safety:** Eliminates entire classes of bugs (use-after-free, data races) that are possible in Go despite its garbage collector. |
| 210 | + |
| 211 | +**Performance:** Zero-cost abstractions and no GC pauses make Rust ideal for high-throughput parsing and buffering operations. |
| 212 | + |
| 213 | +**Type System:** More expressive type system with traits, enums, and pattern matching leads to more maintainable code. |
| 214 | + |
| 215 | +**Ecosystem:** Excellent libraries for async (Tokio), serialization (Serde), CLI (clap), and web (axum). |
| 216 | + |
| 217 | +### Alternatives Considered |
| 218 | + |
| 219 | +**Keep Go:** |
| 220 | +- ✅ No migration effort |
| 221 | +- ❌ Type safety limitations |
| 222 | +- ❌ GC pause times affect latency |
| 223 | +- ❌ Less control over memory layout |
| 224 | + |
| 225 | +**Rewrite in TypeScript/Node:** |
| 226 | +- ✅ Familiar ecosystem |
| 227 | +- ❌ Performance concerns for high-throughput |
| 228 | +- ❌ Single-threaded limitations |
| 229 | + |
| 230 | +### Risk Mitigation |
| 231 | + |
| 232 | +**Risk:** Breaking functional behavior during rewrite |
| 233 | +- *Mitigation:* Reference Go implementation for behavior tests, comprehensive integration tests |
| 234 | + |
| 235 | +**Risk:** Performance regressions |
| 236 | +- *Mitigation:* Benchmark each component against Go baseline |
| 237 | + |
| 238 | +**Risk:** Team learning curve |
| 239 | +- *Mitigation:* Start with simpler components, leverage Rust resources |
| 240 | + |
| 241 | +**Risk:** Dependency on Rust ecosystem maturity |
| 242 | +- *Mitigation:* Use only well-maintained crates with >1M downloads |
| 243 | + |
| 244 | +**Risk:** Loss of momentum with full rewrite |
| 245 | +- *Mitigation:* Build in phases but deploy as complete replacement, no partial migration |
| 246 | + |
| 247 | +### Open Questions |
| 248 | + |
| 249 | +1. **Workspace Structure:** Monorepo with multiple crates or single binary? |
| 250 | + - *Leaning toward:* Workspace with separate crates for better organization |
| 251 | + |
| 252 | +2. **Database Migration:** If we add persistence later, SQLite or embedded DB? |
| 253 | + - *Decision deferred:* Out of scope for initial migration |
| 254 | + |
| 255 | +3. **Cross-compilation:** Support for ARM64 Macs immediately? |
| 256 | + - *Answer:* Yes, Rust has excellent cross-compilation support |
| 257 | + |
| 258 | +4. **Go Code Preservation:** Keep Go code in separate branch for reference? |
| 259 | + - *Answer:* Yes, create `archive/go-implementation` branch before deletion |
| 260 | + |
| 261 | +### References |
| 262 | + |
| 263 | +- [Tokio Documentation](https://tokio.rs/) |
| 264 | +- [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/) |
| 265 | +- [The Rust Performance Book](https://nnethercote.github.io/perf-book/) |
| 266 | +- Internal: See `lean-spec` project for Rust migration example |
0 commit comments