-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add async caching with background task management #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Implement async caching architecture that returns immediately with task IDs,
allowing users to monitor and manage long-running caching operations.
## New Features
- **Task Manager**: Thread-safe background task tracking with status, stages, and cancellation
- **Async Caching**: `cache_crate` now spawns background tasks and returns immediately
- **Rich Markdown Output**: LLM-optimized formatting with embedded action commands
- **Unified Monitoring**: Single `cache_operations` tool for list/query/cancel/clear
## Architecture
- `task_manager.rs`: Core task management with TaskManager, CachingTask, TaskStatus, CachingStage
- `task_formatter.rs`: Markdown formatting optimized for AI agent consumption
- Background tokio tasks with cancellation support via CancellationToken
- Memory-only storage (cleared on server restart)
## API Examples
```rust
// Start caching (returns immediately with task ID)
cache_crate({crate_name: "tokio", source_type: "cratesio", version: "1.35.0"})
// Monitor progress
cache_operations({})
cache_operations({task_id: "abc-123-def"})
// Cancel/clear
cache_operations({task_id: "abc-123-def", cancel: true})
cache_operations({clear: true})
```
## Benefits
- Non-blocking caching for large crates
- Real-time progress tracking
- Cancellation support for user control
- AI-friendly markdown output with embedded commands
- Grouped task listings by status
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
|
Claude finished @snowmead's task —— View job Code Review Complete ✅Review Checklist
Overall AssessmentThis PR successfully transforms the synchronous ✅ Strengths1. Architecture & Design
2. User Experience
3. Code Quality
🔍 Areas for Improvement1. Performance ConsiderationsIssue: Using In tasks: Arc<RwLock<HashMap<TaskId, CachingTask>>>,Recommendation: Consider using use dashmap::DashMap;
// ...
tasks: Arc<DashMap<TaskId, CachingTask>>,This would allow concurrent reads/writes to different keys without blocking. 2. Error Handling & Edge CasesIssue: The In pub fn elapsed_secs(&self) -> u64 {
let end_time = self.completed_at.unwrap_or_else(SystemTime::now);
end_time
.duration_since(self.started_at)
.unwrap_or_default() // Good: handles error case
.as_secs()
}This is actually handled correctly with 3. Resource ManagementIssue: No limit on concurrent background tasks could lead to resource exhaustion. Recommendation: Add a semaphore to limit concurrent operations: use tokio::sync::Semaphore;
pub struct TaskManager {
tasks: Arc<RwLock<HashMap<TaskId, CachingTask>>>,
max_concurrent: Arc<Semaphore>, // e.g., permit 10 concurrent operations
}4. Task LifecycleIssue: Tasks remain in memory indefinitely until explicitly cleared. Recommendation: Consider adding:
5. Progress GranularityIssue: No percentage tracking within stages (mentioned in Future Enhancements). In pub struct CachingStage {
stage_type: CachingStageType,
progress_percent: Option<u8>, // 0-100
}🔒 Security Review✅ No Critical Security Issues Found
Minor Security ConsiderationConsider rate limiting task creation to prevent potential DoS through rapid task spawning: // Add to TaskManager
last_task_times: Arc<RwLock<VecDeque<SystemTime>>>,
// Check before creating task
fn check_rate_limit(&self) -> bool {
// Allow max 10 tasks per second
}🧪 Test Coverage
|
Summary
Transforms the
cache_cratetool into an async operation that returns immediately with a task ID, enabling non-blocking caching of large crates with real-time progress monitoring and cancellation support.Motivation
Large crates can take several minutes to cache (download + generate docs + index). The synchronous implementation blocked the MCP server during this time, preventing other operations and providing no visibility into progress. This makes the experience poor for AI agents and users working with large crates.
Changes
New Modules
task_manager.rs: Core task management systemTaskManager: Thread-safe manager usingArc<RwLock<HashMap>>CachingTask: Task metadata with status, stage, timestamps, cancellation tokenTaskStatusenum: Pending, InProgress, Completed, Failed, CancelledCachingStageenum: Downloading, GeneratingDocs, Indexing, Completedtask_formatter.rs: Rich markdown formatting for LLM consumptionModified Components
cache_cratetool: Now spawns background tokio task and returns immediatelyCacheTools: Integrates TaskManager, newcache_operationsmethodservice.rs: Newcache_operationstool with rich usage examplesDependencies
tokio-util0.7 forCancellationTokenuuidfor task IDsAPI Design
Starting a Cache Operation
Returns markdown with task ID and monitoring commands.
Monitoring Operations
Key Features
✅ Always Async - Consistent behavior, all operations return task ID
✅ Memory Only - No disk persistence, tasks cleared on restart
✅ No Auto-Cleanup - Tasks remain until explicitly cleared or server restart
✅ Cancellation Support - Can abort long-running operations
✅ Thread-Safe - Uses RwLock for concurrent access
✅ Progress Tracking - Track stage and status for each task
✅ LLM-Optimized Output - Markdown with embedded commands and clear hierarchy
Benefits
Testing
Example Output
Starting Cache Operation
Task Listing
Future Enhancements
🤖 Generated with Claude Code