agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
Documentation
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
//! Operating modes for AGCodex (Plan, Build, Review).
//! Minimal scaffolding to start the refactor without impacting existing flows.

use std::sync::Arc;
use std::sync::Mutex;
use std::time::SystemTime;

/// Color representation for mode indicators
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModeColor {
    Blue,
    Green,
    Yellow,
}

/// Visual properties for each mode
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModeVisuals {
    pub indicator: &'static str,
    pub color: ModeColor,
    pub description: &'static str,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum OperatingMode {
    Plan,
    Build,
    Review,
}

impl OperatingMode {
    /// Get the visual properties for this mode
    pub const fn visuals(&self) -> ModeVisuals {
        match self {
            Self::Plan => ModeVisuals {
                indicator: "📋 PLAN",
                color: ModeColor::Blue,
                description: "Read-only analysis mode",
            },
            Self::Build => ModeVisuals {
                indicator: "🔨 BUILD",
                color: ModeColor::Green,
                description: "Full access development mode",
            },
            Self::Review => ModeVisuals {
                indicator: "🔍 REVIEW",
                color: ModeColor::Yellow,
                description: "Quality-focused review mode",
            },
        }
    }

    /// Cycle to the next mode
    pub const fn cycle(&self) -> Self {
        match self {
            Self::Plan => Self::Build,
            Self::Build => Self::Review,
            Self::Review => Self::Plan,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ModeRestrictions {
    pub allow_file_write: bool,
    pub allow_command_exec: bool,
    pub allow_network_access: bool,
    pub allow_git_operations: bool,
    pub max_file_size: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct ModeManager {
    pub current_mode: OperatingMode,
    pub mode_history: Vec<(OperatingMode, SystemTime)>,
    pub restrictions: ModeRestrictions,
}

impl ModeManager {
    pub fn new(initial: OperatingMode) -> Self {
        let mut mgr = Self {
            current_mode: initial,
            mode_history: Vec::new(),
            restrictions: ModeRestrictions::default(),
        };
        mgr.apply_restrictions(initial);
        mgr
    }

    pub fn switch_mode(&mut self, new_mode: OperatingMode) {
        self.mode_history
            .push((self.current_mode, SystemTime::now()));
        self.current_mode = new_mode;
        self.apply_restrictions(new_mode);
    }

    /// Cycle to the next mode (Plan → Build → Review → Plan)
    pub fn cycle(&mut self) -> OperatingMode {
        let new_mode = self.current_mode.cycle();
        self.switch_mode(new_mode);
        new_mode
    }

    /// Get the current mode
    pub const fn current_mode(&self) -> OperatingMode {
        self.current_mode
    }

    /// Get visual properties for the current mode
    pub const fn current_visuals(&self) -> ModeVisuals {
        self.current_mode.visuals()
    }

    /// Create a new thread-safe ModeManager
    pub fn new_shared(initial: OperatingMode) -> Arc<Mutex<Self>> {
        Arc::new(Mutex::new(Self::new(initial)))
    }

    const fn apply_restrictions(&mut self, mode: OperatingMode) {
        self.restrictions = match mode {
            OperatingMode::Plan => ModeRestrictions {
                allow_file_write: false,
                allow_command_exec: false,
                allow_network_access: true, // Allow research
                allow_git_operations: false,
                max_file_size: None,
            },
            OperatingMode::Build => ModeRestrictions {
                allow_file_write: true,
                allow_command_exec: true,
                allow_network_access: true,
                allow_git_operations: true,
                max_file_size: None,
            },
            OperatingMode::Review => ModeRestrictions {
                allow_file_write: true, // Limited edits allowed
                allow_command_exec: false,
                allow_network_access: true,
                allow_git_operations: false,
                max_file_size: Some(10_000),
            },
        };
    }

    /// Check if the current mode is read-only
    pub const fn is_read_only(&self) -> bool {
        matches!(self.current_mode, OperatingMode::Plan)
    }

    /// Check if file writes are allowed in the current mode
    pub const fn allows_write(&self) -> bool {
        self.restrictions.allow_file_write
    }

    /// Check if command execution is allowed in the current mode
    pub const fn allows_execution(&self) -> bool {
        self.restrictions.allow_command_exec
    }

    /// Check if a file operation is allowed in the current mode
    pub const fn can_write_file(&self, file_size: Option<usize>) -> bool {
        if !self.restrictions.allow_file_write {
            return false;
        }

        if let (Some(max_size), Some(size)) = (self.restrictions.max_file_size, file_size) {
            return size <= max_size;
        }

        true
    }

    /// Check if command execution is allowed in the current mode
    pub const fn can_execute_command(&self) -> bool {
        self.restrictions.allow_command_exec
    }

    /// Check if git operations are allowed in the current mode
    pub const fn can_perform_git_operations(&self) -> bool {
        self.restrictions.allow_git_operations
    }

    /// Check if network access is allowed in the current mode
    pub const fn can_access_network(&self) -> bool {
        self.restrictions.allow_network_access
    }

    /// Get a user-friendly error message for disallowed operations
    pub fn restriction_message(&self, operation: &str) -> String {
        match self.current_mode {
            OperatingMode::Plan => format!(
                "⛔ Operation '{}' not allowed in Plan mode (read-only). Switch to Build mode (Shift+Tab) for full access.",
                operation
            ),
            OperatingMode::Review => {
                // Check if it's a size restriction issue
                if operation.contains("file") || operation.contains("edit") {
                    if let Some(max_size) = self.restrictions.max_file_size {
                        format!(
                            "⚠️ Operation '{}' is limited in Review mode. File edits must be under {} bytes. Switch to Build mode (Shift+Tab) for unlimited access.",
                            operation, max_size
                        )
                    } else {
                        format!(
                            "⚠️ Operation '{}' not allowed in Review mode (quality focus). Switch to Build mode (Shift+Tab) for full access.",
                            operation
                        )
                    }
                } else {
                    format!(
                        "⚠️ Operation '{}' not allowed in Review mode (quality focus). Switch to Build mode (Shift+Tab) for full access.",
                        operation
                    )
                }
            }
            OperatingMode::Build => format!(
                "✓ Operation '{}' should be allowed in Build mode. This may be a bug.",
                operation
            ),
        }
    }

    /// Validate an operation and return a Result with appropriate error message
    pub fn validate_file_write(&self, path: &str, size: Option<usize>) -> Result<(), String> {
        if !self.allows_write() {
            return Err(self.restriction_message(&format!("write to {}", path)));
        }

        if let Some(file_size) = size
            && !self.can_write_file(Some(file_size))
            && let Some(max_size) = self.restrictions.max_file_size
        {
            return Err(format!(
                "⚠️ File '{}' exceeds the {} byte limit in Review mode. File size: {} bytes. Switch to Build mode (Shift+Tab) for unlimited file editing.",
                path, max_size, file_size
            ));
        }

        Ok(())
    }

    /// Validate command execution and return a Result with appropriate error message
    pub fn validate_command_execution(&self, command: &str) -> Result<(), String> {
        if !self.allows_execution() {
            return Err(self.restriction_message(&format!("execute command '{}'", command)));
        }
        Ok(())
    }

    /// Validate git operations and return a Result with appropriate error message
    pub fn validate_git_operation(&self, operation: &str) -> Result<(), String> {
        if !self.can_perform_git_operations() {
            return Err(self.restriction_message(&format!("git operation '{}'", operation)));
        }
        Ok(())
    }

    pub const fn prompt_suffix(&self) -> &'static str {
        match self.current_mode {
            OperatingMode::Plan => {
                r#"
<mode>PLAN MODE - Read Only</mode>
You are analyzing and planning. You CAN:
✓ Read any file
✓ Search code using AST-powered semantic search
✓ Analyze AST structure with tree-sitter
✓ Create detailed implementation plans

You CANNOT:
✗ Edit or write files
✗ Execute commands that modify state
"#
            }
            OperatingMode::Build => {
                r#"
<mode>BUILD MODE - Full Access</mode>
You have complete development capabilities:
✓ Read, write, edit files
✓ Execute commands
✓ Use all tools
✓ Full AST-based editing
"#
            }
            OperatingMode::Review => {
                r#"
<mode>REVIEW MODE - Quality Focus</mode>
You are reviewing code quality. You CAN:
✓ Read and analyze code
✓ Suggest improvements
✓ Make small fixes (< 100 lines)
Focus on: bugs, performance, best practices, security
"#
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mode_cycling() {
        let mut manager = ModeManager::new(OperatingMode::Plan);
        assert_eq!(manager.current_mode(), OperatingMode::Plan);

        // Cycle from Plan to Build
        manager.cycle();
        assert_eq!(manager.current_mode(), OperatingMode::Build);

        // Cycle from Build to Review
        manager.cycle();
        assert_eq!(manager.current_mode(), OperatingMode::Review);

        // Cycle from Review back to Plan
        manager.cycle();
        assert_eq!(manager.current_mode(), OperatingMode::Plan);
    }

    #[test]
    fn test_plan_mode_restrictions() {
        let manager = ModeManager::new(OperatingMode::Plan);

        assert!(manager.is_read_only());
        assert!(!manager.allows_write());
        assert!(!manager.allows_execution());
        assert!(!manager.can_write_file(Some(100)));
        assert!(!manager.can_execute_command());
        assert!(!manager.can_perform_git_operations());
        assert!(manager.can_access_network()); // Research is allowed
    }

    #[test]
    fn test_build_mode_restrictions() {
        let manager = ModeManager::new(OperatingMode::Build);

        assert!(!manager.is_read_only());
        assert!(manager.allows_write());
        assert!(manager.allows_execution());
        assert!(manager.can_write_file(Some(1_000_000))); // Any size allowed
        assert!(manager.can_execute_command());
        assert!(manager.can_perform_git_operations());
        assert!(manager.can_access_network());
    }

    #[test]
    fn test_review_mode_restrictions() {
        let manager = ModeManager::new(OperatingMode::Review);

        assert!(!manager.is_read_only());
        assert!(manager.allows_write()); // Limited writes allowed
        assert!(!manager.allows_execution());

        // Test file size restrictions (10KB limit)
        assert!(manager.can_write_file(Some(5_000))); // Under limit
        assert!(manager.can_write_file(Some(10_000))); // At limit
        assert!(!manager.can_write_file(Some(10_001))); // Over limit

        assert!(!manager.can_execute_command());
        assert!(!manager.can_perform_git_operations());
        assert!(manager.can_access_network());
    }

    #[test]
    fn test_validate_file_write() {
        let mut manager = ModeManager::new(OperatingMode::Plan);

        // Plan mode - no writes allowed
        assert!(manager.validate_file_write("test.txt", Some(100)).is_err());

        // Build mode - all writes allowed
        manager.switch_mode(OperatingMode::Build);
        assert!(
            manager
                .validate_file_write("test.txt", Some(1_000_000))
                .is_ok()
        );

        // Review mode - limited writes
        manager.switch_mode(OperatingMode::Review);
        assert!(manager.validate_file_write("test.txt", Some(5_000)).is_ok());
        assert!(
            manager
                .validate_file_write("test.txt", Some(20_000))
                .is_err()
        );
    }

    #[test]
    fn test_validate_command_execution() {
        let mut manager = ModeManager::new(OperatingMode::Plan);

        // Plan mode - no execution
        assert!(manager.validate_command_execution("ls").is_err());

        // Build mode - execution allowed
        manager.switch_mode(OperatingMode::Build);
        assert!(manager.validate_command_execution("ls").is_ok());

        // Review mode - no execution
        manager.switch_mode(OperatingMode::Review);
        assert!(manager.validate_command_execution("rm -rf /").is_err());
    }

    #[test]
    fn test_mode_history() {
        let mut manager = ModeManager::new(OperatingMode::Plan);
        assert_eq!(manager.mode_history.len(), 0);

        manager.switch_mode(OperatingMode::Build);
        assert_eq!(manager.mode_history.len(), 1);
        assert_eq!(manager.mode_history[0].0, OperatingMode::Plan);

        manager.switch_mode(OperatingMode::Review);
        assert_eq!(manager.mode_history.len(), 2);
        assert_eq!(manager.mode_history[1].0, OperatingMode::Build);
    }

    #[test]
    fn test_restriction_messages() {
        let manager = ModeManager::new(OperatingMode::Plan);
        let msg = manager.restriction_message("write file");
        assert!(msg.contains("Plan mode"));
        assert!(msg.contains("read-only"));
        assert!(msg.contains("Shift+Tab"));

        let manager = ModeManager::new(OperatingMode::Review);
        let msg = manager.restriction_message("edit large file");
        assert!(msg.contains("Review mode"));
        assert!(msg.contains("10000 bytes")); // Should mention the size limit

        let manager = ModeManager::new(OperatingMode::Build);
        let msg = manager.restriction_message("anything");
        assert!(msg.contains("should be allowed"));
        assert!(msg.contains("Build mode"));
    }

    #[test]
    fn test_mode_visuals() {
        assert_eq!(OperatingMode::Plan.visuals().indicator, "📋 PLAN");
        assert_eq!(OperatingMode::Build.visuals().indicator, "🔨 BUILD");
        assert_eq!(OperatingMode::Review.visuals().indicator, "🔍 REVIEW");

        assert_eq!(OperatingMode::Plan.visuals().color, ModeColor::Blue);
        assert_eq!(OperatingMode::Build.visuals().color, ModeColor::Green);
        assert_eq!(OperatingMode::Review.visuals().color, ModeColor::Yellow);
    }
}