Repository navigation
Expand file tree
/
Copy pathfeatures.rs
More file actions
579 lines (495 loc) · 15.5 KB
/
Copy pathfeatures.rs
File metadata and controls
579 lines (495 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Feature flags and experiments.
//!
//! Provides a system for managing feature flags, A/B testing,
//! and gradual rollouts.
use std::collections::HashMap;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
/// Feature flag.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Feature {
/// Feature name/ID.
pub name: String,
/// Description.
pub description: String,
/// Is enabled by default.
pub default_enabled: bool,
/// Current state.
pub enabled: bool,
/// Rollout percentage (0-100).
pub rollout_percentage: u8,
/// Required features.
pub requires: Vec<String>,
/// Conflicts with features.
pub conflicts: Vec<String>,
/// Tags for categorization.
pub tags: Vec<String>,
/// Metadata.
pub metadata: HashMap<String, String>,
/// Is experimental.
pub experimental: bool,
/// Is deprecated.
pub deprecated: bool,
/// Deprecation message.
pub deprecation_message: Option<String>,
}
impl Feature {
/// Create a new feature.
pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
Self {
name: name.into(),
description: description.into(),
default_enabled: false,
enabled: false,
rollout_percentage: 0,
requires: Vec::new(),
conflicts: Vec::new(),
tags: Vec::new(),
metadata: HashMap::new(),
experimental: false,
deprecated: false,
deprecation_message: None,
}
}
/// Set enabled by default.
pub fn enabled_by_default(mut self) -> Self {
self.default_enabled = true;
self.enabled = true;
self
}
/// Set rollout percentage.
pub fn with_rollout(mut self, percentage: u8) -> Self {
self.rollout_percentage = percentage.min(100);
self
}
/// Add required feature.
pub fn requires(mut self, feature: impl Into<String>) -> Self {
self.requires.push(feature.into());
self
}
/// Add conflicting feature.
pub fn conflicts_with(mut self, feature: impl Into<String>) -> Self {
self.conflicts.push(feature.into());
self
}
/// Add tag.
pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
self.tags.push(tag.into());
self
}
/// Add metadata.
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
/// Mark as experimental.
pub fn experimental(mut self) -> Self {
self.experimental = true;
self
}
/// Mark as deprecated.
pub fn deprecated(mut self, message: impl Into<String>) -> Self {
self.deprecated = true;
self.deprecation_message = Some(message.into());
self
}
/// Enable the feature.
pub fn enable(&mut self) {
self.enabled = true;
}
/// Disable the feature.
pub fn disable(&mut self) {
self.enabled = false;
}
/// Check if feature is available for a given user/context.
pub fn is_available_for(&self, user_hash: u64) -> bool {
if !self.enabled {
return false;
}
if self.rollout_percentage >= 100 {
return true;
}
if self.rollout_percentage == 0 {
return false;
}
// Simple hash-based rollout
let bucket = (user_hash % 100) as u8;
bucket < self.rollout_percentage
}
}
/// Feature flag manager.
pub struct FeatureManager {
/// Features indexed by name.
features: RwLock<HashMap<String, Feature>>,
/// Overrides per user/context.
overrides: RwLock<HashMap<String, HashMap<String, bool>>>,
/// Default context ID.
default_context: RwLock<Option<String>>,
}
impl FeatureManager {
/// Create a new feature manager.
pub fn new() -> Self {
Self {
features: RwLock::new(HashMap::new()),
overrides: RwLock::new(HashMap::new()),
default_context: RwLock::new(None),
}
}
/// Create with standard features.
pub fn with_standard_features() -> Self {
// Register standard features synchronously isn't possible with async,
// so we return the manager and let the caller register features
Self::new()
}
/// Register a feature.
pub async fn register(&self, feature: Feature) {
self.features
.write()
.await
.insert(feature.name.clone(), feature);
}
/// Get a feature.
pub async fn get(&self, name: &str) -> Option<Feature> {
self.features.read().await.get(name).cloned()
}
/// Check if a feature is enabled.
pub async fn is_enabled(&self, name: &str) -> bool {
self.is_enabled_for(name, None).await
}
/// Check if a feature is enabled for a context.
pub async fn is_enabled_for(&self, name: &str, context: Option<&str>) -> bool {
// Check overrides first
if let Some(ctx) = context.or(self.default_context.read().await.as_deref()) {
let overrides = self.overrides.read().await;
if let Some(ctx_overrides) = overrides.get(ctx)
&& let Some(&enabled) = ctx_overrides.get(name)
{
return enabled;
}
}
// Check feature state
let features = self.features.read().await;
if let Some(feature) = features.get(name) {
if !feature.enabled {
return false;
}
// Check dependencies (non-recursive check)
for req in &feature.requires {
if let Some(req_feature) = features.get(req) {
if !req_feature.enabled {
return false;
}
} else {
return false;
}
}
// Check conflicts (non-recursive check)
for conflict in &feature.conflicts {
if let Some(conf_feature) = features.get(conflict)
&& conf_feature.enabled
{
return false;
}
}
return true;
}
false
}
/// Enable a feature.
pub async fn enable(&self, name: &str) -> bool {
if let Some(feature) = self.features.write().await.get_mut(name) {
feature.enable();
return true;
}
false
}
/// Disable a feature.
pub async fn disable(&self, name: &str) -> bool {
if let Some(feature) = self.features.write().await.get_mut(name) {
feature.disable();
return true;
}
false
}
/// Set override for a context.
pub async fn set_override(&self, context: &str, feature: &str, enabled: bool) {
let mut overrides = self.overrides.write().await;
overrides
.entry(context.to_string())
.or_insert_with(HashMap::new)
.insert(feature.to_string(), enabled);
}
/// Clear override for a context.
pub async fn clear_override(&self, context: &str, feature: &str) {
let mut overrides = self.overrides.write().await;
if let Some(ctx_overrides) = overrides.get_mut(context) {
ctx_overrides.remove(feature);
}
}
/// Clear all overrides for a context.
pub async fn clear_context_overrides(&self, context: &str) {
self.overrides.write().await.remove(context);
}
/// Set default context.
pub async fn set_default_context(&self, context: impl Into<String>) {
*self.default_context.write().await = Some(context.into());
}
/// List all features.
pub async fn list(&self) -> Vec<Feature> {
self.features.read().await.values().cloned().collect()
}
/// List enabled features.
pub async fn enabled(&self) -> Vec<String> {
self.features
.read()
.await
.iter()
.filter(|(_, f)| f.enabled)
.map(|(n, _)| n.clone())
.collect()
}
/// List disabled features.
pub async fn disabled(&self) -> Vec<String> {
self.features
.read()
.await
.iter()
.filter(|(_, f)| !f.enabled)
.map(|(n, _)| n.clone())
.collect()
}
/// List experimental features.
pub async fn experimental(&self) -> Vec<String> {
self.features
.read()
.await
.iter()
.filter(|(_, f)| f.experimental)
.map(|(n, _)| n.clone())
.collect()
}
/// List deprecated features.
pub async fn deprecated(&self) -> Vec<String> {
self.features
.read()
.await
.iter()
.filter(|(_, f)| f.deprecated)
.map(|(n, _)| n.clone())
.collect()
}
/// Get features by tag.
pub async fn by_tag(&self, tag: &str) -> Vec<Feature> {
self.features
.read()
.await
.values()
.filter(|f| f.tags.contains(&tag.to_string()))
.cloned()
.collect()
}
/// Export configuration.
pub async fn export(&self) -> FeatureConfig {
let features = self.features.read().await;
let overrides = self.overrides.read().await;
FeatureConfig {
features: features.values().cloned().collect(),
overrides: overrides.clone(),
}
}
/// Import configuration.
pub async fn import(&self, config: FeatureConfig) {
let mut features = self.features.write().await;
let mut overrides = self.overrides.write().await;
for feature in config.features {
features.insert(feature.name.clone(), feature);
}
for (ctx, ctx_overrides) in config.overrides {
overrides.insert(ctx, ctx_overrides);
}
}
}
impl Default for FeatureManager {
fn default() -> Self {
Self::new()
}
}
/// Feature configuration for export/import.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureConfig {
/// Features.
pub features: Vec<Feature>,
/// Overrides.
pub overrides: HashMap<String, HashMap<String, bool>>,
}
/// Standard feature flags.
pub mod standard {
use super::Feature;
/// Extended thinking/reasoning.
pub fn extended_thinking() -> Feature {
Feature::new(
"extended_thinking",
"Enable extended thinking/reasoning for complex tasks",
)
.enabled_by_default()
.with_tag("ai")
}
/// Streaming responses.
pub fn streaming() -> Feature {
Feature::new("streaming", "Enable streaming responses from models")
.enabled_by_default()
.with_tag("performance")
}
/// Tool use.
pub fn tool_use() -> Feature {
Feature::new("tool_use", "Enable tool/function calling")
.enabled_by_default()
.with_tag("ai")
}
/// File watching.
pub fn file_watching() -> Feature {
Feature::new("file_watching", "Watch files for changes").with_tag("files")
}
/// Sandbox execution.
pub fn sandbox() -> Feature {
Feature::new("sandbox", "Execute commands in sandbox environment").with_tag("security")
}
/// Auto-approval of safe commands.
pub fn auto_approve() -> Feature {
Feature::new("auto_approve", "Automatically approve safe commands").with_tag("automation")
}
/// Metrics collection.
pub fn metrics() -> Feature {
Feature::new("metrics", "Collect usage metrics").with_tag("analytics")
}
/// Context compaction.
pub fn context_compaction() -> Feature {
Feature::new(
"context_compaction",
"Automatically compact context when limit approached",
)
.enabled_by_default()
.with_tag("ai")
}
/// MCP support.
pub fn mcp() -> Feature {
Feature::new("mcp", "Enable Model Context Protocol support").with_tag("integration")
}
/// Undo support.
pub fn undo() -> Feature {
Feature::new("undo", "Enable undo functionality for file changes")
.enabled_by_default()
.with_tag("files")
}
/// Dark mode (for TUI).
pub fn dark_mode() -> Feature {
Feature::new("dark_mode", "Use dark color scheme")
.enabled_by_default()
.with_tag("ui")
}
/// Vim keybindings.
pub fn vim_mode() -> Feature {
Feature::new("vim_mode", "Enable vim-style keybindings")
.with_tag("ui")
.experimental()
}
/// Get all standard features.
pub fn all() -> Vec<Feature> {
vec![
extended_thinking(),
streaming(),
tool_use(),
file_watching(),
sandbox(),
auto_approve(),
metrics(),
context_compaction(),
mcp(),
undo(),
dark_mode(),
vim_mode(),
]
}
}
/// Feature guard for conditional execution.
pub struct FeatureGuard {
manager: Arc<FeatureManager>,
feature: String,
}
impl FeatureGuard {
/// Create a new feature guard.
pub fn new(manager: Arc<FeatureManager>, feature: impl Into<String>) -> Self {
Self {
manager,
feature: feature.into(),
}
}
/// Check if feature is enabled.
pub async fn is_enabled(&self) -> bool {
self.manager.is_enabled(&self.feature).await
}
/// Execute if feature is enabled.
pub async fn if_enabled<F, Fut, T>(&self, f: F) -> Option<T>
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = T>,
{
if self.is_enabled().await {
Some(f().await)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_feature_creation() {
let feature = Feature::new("test", "Test feature")
.enabled_by_default()
.experimental()
.with_tag("testing");
assert!(feature.enabled);
assert!(feature.experimental);
assert!(feature.tags.contains(&"testing".to_string()));
}
#[test]
fn test_rollout() {
let mut feature = Feature::new("test", "Test").with_rollout(50);
feature.enable();
// Test multiple hashes
let enabled_count = (0..1000)
.map(|i| feature.is_available_for(i))
.filter(|&b| b)
.count();
// Should be approximately 50% (allow some variance)
assert!(enabled_count > 400 && enabled_count < 600);
}
#[tokio::test]
async fn test_feature_manager() {
let manager = FeatureManager::new();
let feature = Feature::new("test", "Test").enabled_by_default();
manager.register(feature).await;
assert!(manager.is_enabled("test").await);
manager.disable("test").await;
assert!(!manager.is_enabled("test").await);
}
#[tokio::test]
async fn test_overrides() {
let manager = FeatureManager::new();
let feature = Feature::new("test", "Test").enabled_by_default();
manager.register(feature).await;
manager.set_override("user1", "test", false).await;
assert!(manager.is_enabled("test").await);
assert!(!manager.is_enabled_for("test", Some("user1")).await);
}
#[test]
fn test_standard_features() {
let features = standard::all();
assert!(!features.is_empty());
let streaming = standard::streaming();
assert!(streaming.default_enabled);
}
}