nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! Edit History - Undo/Redo for text input
//!
//! Provides undo/redo functionality for text editing in the TUI.
//! Coalesces rapid keystrokes into single undo steps to avoid
//! stepping through every character.
//!
//! # Usage
//!
//! ```rust,ignore
//! let mut history = EditHistory::new(100); // max 100 states
//! history.push("hello", 5);  // Save state
//! history.push("hello!", 6); // Save another state
//! let (text, cursor) = history.undo().unwrap(); // "hello", 5
//! let (text, cursor) = history.redo().unwrap(); // "hello!", 6
//! ```

use std::collections::VecDeque;
use std::time::{Duration, Instant};

/// How long between keystrokes to consider them part of the same "edit group"
/// If user types rapidly, we coalesce into one undo step.
const COALESCE_TIMEOUT: Duration = Duration::from_millis(500);

/// Minimum change size to trigger a new undo checkpoint
const MIN_CHANGE_SIZE: usize = 1;

/// A snapshot of edit state (text + cursor position)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EditState {
    pub text: String,
    pub cursor: usize,
    timestamp: Instant,
}

impl EditState {
    /// Create a new edit state snapshot
    pub fn new(text: String, cursor: usize) -> Self {
        Self {
            text,
            cursor,
            timestamp: Instant::now(),
        }
    }
}

/// Edit history manager supporting undo/redo
///
/// Maintains two stacks:
/// - `undo_stack`: Past states we can undo to
/// - `redo_stack`: States we undid and can redo to
///
/// Redo stack is cleared on any new edit.
#[derive(Debug, Clone)]
pub struct EditHistory {
    /// Stack of past states (undo targets)
    undo_stack: VecDeque<EditState>,
    /// Stack of undone states (redo targets)
    redo_stack: VecDeque<EditState>,
    /// Maximum number of states to keep
    max_size: usize,
    /// Current state (not in either stack)
    current: Option<EditState>,
}

impl Default for EditHistory {
    fn default() -> Self {
        Self::new(100)
    }
}

impl EditHistory {
    /// Create a new edit history with given max size
    pub fn new(max_size: usize) -> Self {
        Self {
            undo_stack: VecDeque::new(),
            redo_stack: VecDeque::new(),
            max_size,
            current: None,
        }
    }

    /// Initialize history with starting state
    pub fn init(&mut self, text: &str, cursor: usize) {
        self.current = Some(EditState::new(text.to_string(), cursor));
        self.undo_stack.clear();
        self.redo_stack.clear();
    }

    /// Push a new state onto the history
    ///
    /// This clears the redo stack (new edits invalidate redo).
    /// Coalesces rapid edits into single undo steps.
    pub fn push(&mut self, text: &str, cursor: usize) {
        let new_state = EditState::new(text.to_string(), cursor);

        // Check if we should coalesce with current state
        if let Some(ref current) = self.current {
            // If text hasn't changed, just update cursor
            if current.text == text {
                self.current = Some(new_state);
                return;
            }

            // If within coalesce timeout and small change, update in place
            let elapsed = new_state.timestamp.duration_since(current.timestamp);
            let change_size = text.len().abs_diff(current.text.len());

            if elapsed < COALESCE_TIMEOUT && change_size <= MIN_CHANGE_SIZE {
                // Coalesce: update current without pushing to undo stack
                self.current = Some(new_state);
                // Clear redo on any edit
                self.redo_stack.clear();
                return;
            }

            // Not coalescing: push current to undo stack
            self.undo_stack.push_back(current.clone());

            // Enforce max size - O(1) with VecDeque
            if self.undo_stack.len() > self.max_size {
                self.undo_stack.pop_front();
            }
        }

        // Update current state
        self.current = Some(new_state);

        // Clear redo stack on new edit
        self.redo_stack.clear();
    }

    /// Force a checkpoint regardless of coalescing rules
    ///
    /// Use this before operations like paste or clear.
    pub fn checkpoint(&mut self, text: &str, cursor: usize) {
        if let Some(ref current) = self.current {
            self.undo_stack.push_back(current.clone());
            // Enforce max size - O(1) with VecDeque
            if self.undo_stack.len() > self.max_size {
                self.undo_stack.pop_front();
            }
        }
        self.current = Some(EditState::new(text.to_string(), cursor));
        self.redo_stack.clear();
    }

    /// Undo to previous state
    ///
    /// Returns the state to restore, or None if nothing to undo.
    pub fn undo(&mut self) -> Option<(String, usize)> {
        let previous = self.undo_stack.pop_back()?;

        // Push current to redo stack
        if let Some(current) = self.current.take() {
            self.redo_stack.push_back(current);
        }

        // Make previous the current
        let result = (previous.text.clone(), previous.cursor);
        self.current = Some(previous);

        Some(result)
    }

    /// Redo to next state
    ///
    /// Returns the state to restore, or None if nothing to redo.
    pub fn redo(&mut self) -> Option<(String, usize)> {
        let next = self.redo_stack.pop_back()?;

        // Push current to undo stack
        if let Some(current) = self.current.take() {
            self.undo_stack.push_back(current);
        }

        // Make next the current
        let result = (next.text.clone(), next.cursor);
        self.current = Some(next);

        Some(result)
    }

    /// Check if undo is available
    pub fn can_undo(&self) -> bool {
        !self.undo_stack.is_empty()
    }

    /// Check if redo is available
    pub fn can_redo(&self) -> bool {
        !self.redo_stack.is_empty()
    }

    /// Get current undo stack depth
    pub fn undo_depth(&self) -> usize {
        self.undo_stack.len()
    }

    /// Get current redo stack depth
    pub fn redo_depth(&self) -> usize {
        self.redo_stack.len()
    }

    /// Clear all history
    pub fn clear(&mut self) {
        self.undo_stack.clear();
        self.redo_stack.clear();
        self.current = None;
    }
}

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

    #[test]
    fn test_edit_history_new_creates_empty() {
        let history = EditHistory::new(100);
        assert!(!history.can_undo());
        assert!(!history.can_redo());
        assert_eq!(history.undo_depth(), 0);
        assert_eq!(history.redo_depth(), 0);
    }

    #[test]
    fn test_edit_history_default_has_100_max() {
        let history = EditHistory::default();
        assert_eq!(history.max_size, 100);
    }

    #[test]
    fn test_edit_history_init_sets_current() {
        let mut history = EditHistory::new(100);
        history.init("hello", 5);
        assert!(history.current.is_some());
        assert_eq!(history.current.as_ref().unwrap().text, "hello");
        assert_eq!(history.current.as_ref().unwrap().cursor, 5);
    }

    #[test]
    fn test_edit_history_init_clears_stacks() {
        let mut history = EditHistory::new(100);
        history.push("a", 1);
        history.push("ab", 2);
        history.undo();

        history.init("fresh", 5);
        assert!(!history.can_undo());
        assert!(!history.can_redo());
    }

    #[test]
    fn test_edit_history_push_enables_undo() {
        let mut history = EditHistory::new(100);
        history.init("", 0);

        // Force checkpoint to avoid coalescing
        std::thread::sleep(Duration::from_millis(600));
        history.push("hello", 5);

        assert!(history.can_undo());
    }

    #[test]
    fn test_edit_history_undo_returns_previous() {
        let mut history = EditHistory::new(100);
        history.init("", 0);
        history.checkpoint("hello", 5); // Force checkpoint

        let result = history.undo();
        assert!(result.is_some());
        let (text, cursor) = result.unwrap();
        assert_eq!(text, "");
        assert_eq!(cursor, 0);
    }

    #[test]
    fn test_edit_history_redo_after_undo() {
        let mut history = EditHistory::new(100);
        history.init("", 0);
        history.checkpoint("hello", 5);

        history.undo();
        assert!(history.can_redo());

        let result = history.redo();
        assert!(result.is_some());
        let (text, cursor) = result.unwrap();
        assert_eq!(text, "hello");
        assert_eq!(cursor, 5);
    }

    #[test]
    fn test_edit_history_new_edit_clears_redo() {
        let mut history = EditHistory::new(100);
        history.init("", 0);
        history.checkpoint("hello", 5);
        history.undo();
        assert!(history.can_redo());

        // New edit should clear redo
        history.checkpoint("world", 5);
        assert!(!history.can_redo());
    }

    #[test]
    fn test_edit_history_max_size_enforced() {
        let mut history = EditHistory::new(3);
        history.init("0", 1);
        history.checkpoint("1", 1);
        history.checkpoint("2", 1);
        history.checkpoint("3", 1);
        history.checkpoint("4", 1);

        // Should only have 3 items in undo stack
        assert_eq!(history.undo_depth(), 3);
    }

    #[test]
    fn test_edit_history_undo_empty_returns_none() {
        let mut history = EditHistory::new(100);
        assert!(history.undo().is_none());
    }

    #[test]
    fn test_edit_history_redo_empty_returns_none() {
        let mut history = EditHistory::new(100);
        assert!(history.redo().is_none());
    }

    #[test]
    fn test_edit_history_clear_removes_all() {
        let mut history = EditHistory::new(100);
        history.init("", 0);
        history.checkpoint("hello", 5);
        history.undo();

        history.clear();
        assert!(!history.can_undo());
        assert!(!history.can_redo());
        assert!(history.current.is_none());
    }

    #[test]
    fn test_edit_history_cursor_only_change_no_undo_push() {
        let mut history = EditHistory::new(100);
        history.init("hello", 0);
        history.push("hello", 3); // Same text, different cursor

        // Should not have created an undo entry
        assert!(!history.can_undo());
    }

    #[test]
    fn test_edit_history_multiple_undo_redo() {
        let mut history = EditHistory::new(100);
        history.init("a", 1);
        history.checkpoint("ab", 2);
        history.checkpoint("abc", 3);

        // Undo twice
        let (text1, _) = history.undo().unwrap();
        assert_eq!(text1, "ab");
        let (text2, _) = history.undo().unwrap();
        assert_eq!(text2, "a");

        // Redo twice
        let (text3, _) = history.redo().unwrap();
        assert_eq!(text3, "ab");
        let (text4, _) = history.redo().unwrap();
        assert_eq!(text4, "abc");
    }

    #[test]
    fn test_edit_state_new() {
        let state = EditState::new("test".to_string(), 4);
        assert_eq!(state.text, "test");
        assert_eq!(state.cursor, 4);
    }

    #[test]
    fn test_edit_state_equality() {
        let state1 = EditState::new("test".to_string(), 4);
        std::thread::sleep(Duration::from_millis(1));
        let state2 = EditState::new("test".to_string(), 4);

        // Note: timestamp differs, but we don't include it in equality
        // Actually our PartialEq derives include timestamp, so they won't be equal
        // This is fine for our use case
        assert_ne!(state1, state2); // Different timestamps
    }

    #[test]
    fn test_edit_history_checkpoint_always_pushes() {
        let mut history = EditHistory::new(100);
        history.init("a", 1);

        // Even rapid checkpoints should push
        history.checkpoint("ab", 2);
        history.checkpoint("abc", 3);
        history.checkpoint("abcd", 4);

        assert_eq!(history.undo_depth(), 3);
    }

    #[test]
    fn test_coalesce_timeout_constant() {
        assert_eq!(COALESCE_TIMEOUT, Duration::from_millis(500));
    }

    #[test]
    fn test_min_change_size_constant() {
        assert_eq!(MIN_CHANGE_SIZE, 1);
    }
}