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
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
580
581
582
583
584
585
586
587
588
//! Partial completion support for agent execution
//!
//! When an agent hits a limit (turns, tokens, cost, duration), this module
//! allows capturing and saving the partial progress for potential resumption.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use nika::runtime::partial::{PartialResult, PartialCheckpoint};
//!
//! // Create checkpoint when limit is reached
//! let checkpoint = PartialCheckpoint::new(
//!     "task-1",
//!     0.65,  // 65% complete
//!     "Generated 3 of 5 sections",
//! );
//!
//! // Save progress
//! checkpoint.save_to_file("./checkpoints/task-1.json").await?;
//!
//! // Load and resume later
//! let restored = PartialCheckpoint::load_from_file("./checkpoints/task-1.json").await?;
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::error::NikaError;

// ═══════════════════════════════════════════════════════════════════════════
// PARTIAL RESULT
// ═══════════════════════════════════════════════════════════════════════════

/// Result of a partially completed task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartialResult {
    /// Current output content (may be incomplete)
    pub content: String,

    /// Progress percentage (0.0-1.0)
    pub progress: f64,

    /// Preview of the result for display
    pub preview: String,

    /// Reason why execution stopped
    pub stop_reason: StopReason,

    /// Number of turns completed
    pub turns_completed: u32,

    /// Total tokens used
    pub tokens_used: u64,

    /// Cost incurred (USD)
    pub cost_usd: f64,
}

impl PartialResult {
    /// Create a new partial result
    pub fn new(content: impl Into<String>, progress: f64, stop_reason: StopReason) -> Self {
        let content = content.into();
        let preview = Self::generate_preview(&content);

        Self {
            content,
            progress: progress.clamp(0.0, 1.0),
            preview,
            stop_reason,
            turns_completed: 0,
            tokens_used: 0,
            cost_usd: 0.0,
        }
    }

    /// Set usage statistics
    pub fn with_usage(mut self, turns: u32, tokens: u64, cost: f64) -> Self {
        self.turns_completed = turns;
        self.tokens_used = tokens;
        self.cost_usd = cost;
        self
    }

    /// Generate a preview from content (first 100 chars or first line)
    fn generate_preview(content: &str) -> String {
        let first_line = content.lines().next().unwrap_or("");
        if first_line.len() > 100 {
            format!("{}...", crate::util::truncate_str(first_line, 97))
        } else if first_line.len() < content.len() {
            format!("{}...", first_line)
        } else {
            first_line.to_string()
        }
    }

    /// Check if this is a meaningful partial result
    pub fn is_meaningful(&self) -> bool {
        !self.content.is_empty() && self.progress > 0.0
    }

    /// Calculate progress percentage as integer (0-100)
    pub fn progress_percent(&self) -> u32 {
        (self.progress * 100.0).round() as u32
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// STOP REASON
// ═══════════════════════════════════════════════════════════════════════════

/// Reason for partial completion
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StopReason {
    /// Hit maximum turns limit
    TurnsLimit,
    /// Hit token budget limit
    TokensLimit,
    /// Hit cost budget limit
    CostLimit,
    /// Hit duration limit
    DurationLimit,
    /// User requested stop
    UserRequested,
    /// Error occurred
    Error,
}

impl StopReason {
    /// Get human-readable description
    pub fn description(&self) -> &'static str {
        match self {
            Self::TurnsLimit => "Maximum turns reached",
            Self::TokensLimit => "Token budget exhausted",
            Self::CostLimit => "Cost budget exhausted",
            Self::DurationLimit => "Duration limit reached",
            Self::UserRequested => "User requested stop",
            Self::Error => "Error occurred",
        }
    }

    /// Get short label for display
    pub fn label(&self) -> &'static str {
        match self {
            Self::TurnsLimit => "turns",
            Self::TokensLimit => "tokens",
            Self::CostLimit => "cost",
            Self::DurationLimit => "duration",
            Self::UserRequested => "user",
            Self::Error => "error",
        }
    }

    /// Check if this is a limit-based stop
    pub fn is_limit(&self) -> bool {
        matches!(
            self,
            Self::TurnsLimit | Self::TokensLimit | Self::CostLimit | Self::DurationLimit
        )
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// PARTIAL CHECKPOINT
// ═══════════════════════════════════════════════════════════════════════════

/// Checkpoint for resuming partial execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartialCheckpoint {
    /// Version for compatibility checking
    pub version: u32,

    /// Task identifier
    pub task_id: Arc<str>,

    /// Timestamp when checkpoint was created (Unix epoch seconds)
    pub created_at: u64,

    /// Current progress (0.0-1.0)
    pub progress: f64,

    /// The partial result
    pub result: PartialResult,

    /// Conversation history for resumption
    pub history: Vec<CheckpointMessage>,

    /// Context data for resumption
    pub context: HashMap<String, serde_json::Value>,

    /// Provider that was being used
    pub provider: Option<String>,

    /// Model that was being used
    pub model: Option<String>,
}

/// Message in checkpoint history
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointMessage {
    /// Role: "user", "assistant", or "system"
    pub role: String,
    /// Message content
    pub content: String,
}

impl PartialCheckpoint {
    /// Current checkpoint format version
    pub const CURRENT_VERSION: u32 = 1;

    /// Create a new checkpoint
    pub fn new(
        task_id: impl Into<Arc<str>>,
        progress: f64,
        content: impl Into<String>,
        stop_reason: StopReason,
    ) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        Self {
            version: Self::CURRENT_VERSION,
            task_id: task_id.into(),
            created_at: now,
            progress: progress.clamp(0.0, 1.0),
            result: PartialResult::new(content, progress, stop_reason),
            history: Vec::new(),
            context: HashMap::new(),
            provider: None,
            model: None,
        }
    }

    /// Add a message to history
    pub fn add_message(&mut self, role: impl Into<String>, content: impl Into<String>) {
        self.history.push(CheckpointMessage {
            role: role.into(),
            content: content.into(),
        });
    }

    /// Set context data
    pub fn set_context(&mut self, key: impl Into<String>, value: serde_json::Value) {
        self.context.insert(key.into(), value);
    }

    /// Set provider info
    pub fn with_provider(mut self, provider: impl Into<String>, model: impl Into<String>) -> Self {
        self.provider = Some(provider.into());
        self.model = Some(model.into());
        self
    }

    /// Set usage statistics
    pub fn with_usage(mut self, turns: u32, tokens: u64, cost: f64) -> Self {
        self.result = self.result.with_usage(turns, tokens, cost);
        self
    }

    /// Save checkpoint to file
    pub async fn save_to_file(&self, path: impl AsRef<Path>) -> Result<(), NikaError> {
        let path = path.as_ref();

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent)
                .await
                .map_err(NikaError::IoError)?;
        }

        // Serialize to JSON
        let json =
            serde_json::to_string_pretty(self).map_err(|e| NikaError::SerializationError {
                details: format!("Failed to serialize checkpoint: {}", e),
            })?;

        // Write atomically (write to temp, then rename)
        let temp_path = path.with_extension("tmp");
        tokio::fs::write(&temp_path, &json)
            .await
            .map_err(NikaError::IoError)?;
        tokio::fs::rename(&temp_path, path)
            .await
            .map_err(NikaError::IoError)?;

        Ok(())
    }

    /// Load checkpoint from file
    pub async fn load_from_file(path: impl AsRef<Path>) -> Result<Self, NikaError> {
        let path = path.as_ref();

        let json = tokio::fs::read_to_string(path)
            .await
            .map_err(NikaError::IoError)?;

        let checkpoint: Self =
            serde_json::from_str(&json).map_err(|e| NikaError::SerializationError {
                details: format!("Failed to parse checkpoint: {}", e),
            })?;

        // Check version compatibility
        if checkpoint.version > Self::CURRENT_VERSION {
            return Err(NikaError::ValidationError {
                reason: format!(
                    "Checkpoint version {} is newer than supported version {}",
                    checkpoint.version,
                    Self::CURRENT_VERSION
                ),
            });
        }

        Ok(checkpoint)
    }

    /// Check if checkpoint exists at path
    pub async fn exists(path: impl AsRef<Path>) -> bool {
        tokio::fs::metadata(path.as_ref()).await.is_ok()
    }

    /// Generate checkpoint filename for a task
    pub fn filename(task_id: &str) -> String {
        // Sanitize task_id for use as filename
        let safe_id: String = task_id
            .chars()
            .map(|c| {
                if c.is_alphanumeric() || c == '-' || c == '_' {
                    c
                } else {
                    '_'
                }
            })
            .collect();
        format!("{}.checkpoint.json", safe_id)
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════

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

    // ───────────────────────────────────────────────────────────────────────
    // PartialResult tests
    // ───────────────────────────────────────────────────────────────────────

    #[test]
    fn partial_result_new() {
        let result = PartialResult::new("Hello world", 0.5, StopReason::TurnsLimit);
        assert_eq!(result.content, "Hello world");
        assert!((result.progress - 0.5).abs() < 0.0001);
        assert_eq!(result.stop_reason, StopReason::TurnsLimit);
    }

    #[test]
    fn partial_result_clamps_progress() {
        let result = PartialResult::new("test", 1.5, StopReason::CostLimit);
        assert!((result.progress - 1.0).abs() < 0.0001);

        let result = PartialResult::new("test", -0.5, StopReason::CostLimit);
        assert!((result.progress - 0.0).abs() < 0.0001);
    }

    #[test]
    fn partial_result_with_usage() {
        let result =
            PartialResult::new("test", 0.75, StopReason::TokensLimit).with_usage(10, 5000, 0.05);

        assert_eq!(result.turns_completed, 10);
        assert_eq!(result.tokens_used, 5000);
        assert!((result.cost_usd - 0.05).abs() < 0.0001);
    }

    #[test]
    fn partial_result_preview_short() {
        let result = PartialResult::new("Short content", 0.5, StopReason::TurnsLimit);
        assert_eq!(result.preview, "Short content");
    }

    #[test]
    fn partial_result_preview_multiline() {
        let result = PartialResult::new(
            "First line\nSecond line\nThird",
            0.5,
            StopReason::TurnsLimit,
        );
        assert_eq!(result.preview, "First line...");
    }

    #[test]
    fn partial_result_preview_long_line() {
        let long_content = "x".repeat(150);
        let result = PartialResult::new(&long_content, 0.5, StopReason::TurnsLimit);
        assert!(result.preview.ends_with("..."));
        assert!(result.preview.len() <= 103); // 97 + "..."
    }

    #[test]
    fn partial_result_is_meaningful() {
        let meaningful = PartialResult::new("content", 0.5, StopReason::TurnsLimit);
        assert!(meaningful.is_meaningful());

        let empty = PartialResult::new("", 0.5, StopReason::TurnsLimit);
        assert!(!empty.is_meaningful());

        let zero_progress = PartialResult::new("content", 0.0, StopReason::TurnsLimit);
        assert!(!zero_progress.is_meaningful());
    }

    #[test]
    fn partial_result_progress_percent() {
        let result = PartialResult::new("test", 0.654, StopReason::TurnsLimit);
        assert_eq!(result.progress_percent(), 65);
    }

    // ───────────────────────────────────────────────────────────────────────
    // StopReason tests
    // ───────────────────────────────────────────────────────────────────────

    #[test]
    fn stop_reason_descriptions() {
        assert!(!StopReason::TurnsLimit.description().is_empty());
        assert!(!StopReason::TokensLimit.description().is_empty());
        assert!(!StopReason::CostLimit.description().is_empty());
        assert!(!StopReason::DurationLimit.description().is_empty());
        assert!(!StopReason::UserRequested.description().is_empty());
        assert!(!StopReason::Error.description().is_empty());
    }

    #[test]
    fn stop_reason_labels() {
        assert_eq!(StopReason::TurnsLimit.label(), "turns");
        assert_eq!(StopReason::TokensLimit.label(), "tokens");
        assert_eq!(StopReason::CostLimit.label(), "cost");
        assert_eq!(StopReason::DurationLimit.label(), "duration");
    }

    #[test]
    fn stop_reason_is_limit() {
        assert!(StopReason::TurnsLimit.is_limit());
        assert!(StopReason::TokensLimit.is_limit());
        assert!(StopReason::CostLimit.is_limit());
        assert!(StopReason::DurationLimit.is_limit());
        assert!(!StopReason::UserRequested.is_limit());
        assert!(!StopReason::Error.is_limit());
    }

    // ───────────────────────────────────────────────────────────────────────
    // PartialCheckpoint tests
    // ───────────────────────────────────────────────────────────────────────

    #[test]
    fn checkpoint_new() {
        let checkpoint = PartialCheckpoint::new(
            "task-1",
            0.75,
            "Partial content here",
            StopReason::TurnsLimit,
        );

        assert_eq!(checkpoint.version, PartialCheckpoint::CURRENT_VERSION);
        assert_eq!(&*checkpoint.task_id, "task-1");
        assert!((checkpoint.progress - 0.75).abs() < 0.0001);
        assert!(checkpoint.created_at > 0);
    }

    #[test]
    fn checkpoint_add_message() {
        let mut checkpoint = PartialCheckpoint::new("task-1", 0.5, "test", StopReason::TurnsLimit);
        checkpoint.add_message("user", "Hello");
        checkpoint.add_message("assistant", "Hi there");

        assert_eq!(checkpoint.history.len(), 2);
        assert_eq!(checkpoint.history[0].role, "user");
        assert_eq!(checkpoint.history[1].content, "Hi there");
    }

    #[test]
    fn checkpoint_set_context() {
        let mut checkpoint = PartialCheckpoint::new("task-1", 0.5, "test", StopReason::TurnsLimit);
        checkpoint.set_context("key1", serde_json::json!({"nested": "value"}));

        assert!(checkpoint.context.contains_key("key1"));
    }

    #[test]
    fn checkpoint_with_provider() {
        let checkpoint = PartialCheckpoint::new("task-1", 0.5, "test", StopReason::TurnsLimit)
            .with_provider("claude", "claude-sonnet-4-6");

        assert_eq!(checkpoint.provider, Some("claude".to_string()));
        assert_eq!(checkpoint.model, Some("claude-sonnet-4-6".to_string()));
    }

    #[test]
    fn checkpoint_with_usage() {
        let checkpoint = PartialCheckpoint::new("task-1", 0.5, "test", StopReason::TurnsLimit)
            .with_usage(5, 10000, 0.15);

        assert_eq!(checkpoint.result.turns_completed, 5);
        assert_eq!(checkpoint.result.tokens_used, 10000);
        assert!((checkpoint.result.cost_usd - 0.15).abs() < 0.0001);
    }

    #[test]
    fn checkpoint_filename() {
        assert_eq!(
            PartialCheckpoint::filename("task-1"),
            "task-1.checkpoint.json"
        );
        assert_eq!(
            PartialCheckpoint::filename("task/with/slashes"),
            "task_with_slashes.checkpoint.json"
        );
        assert_eq!(
            PartialCheckpoint::filename("task with spaces"),
            "task_with_spaces.checkpoint.json"
        );
    }

    #[test]
    fn checkpoint_serialization_roundtrip() {
        let mut checkpoint =
            PartialCheckpoint::new("task-1", 0.75, "Content", StopReason::CostLimit)
                .with_provider("openai", "gpt-4o")
                .with_usage(3, 5000, 0.05);

        checkpoint.add_message("user", "Question");
        checkpoint.add_message("assistant", "Answer");
        checkpoint.set_context("data", serde_json::json!({"key": "value"}));

        let json = serde_json::to_string(&checkpoint).unwrap();
        let restored: PartialCheckpoint = serde_json::from_str(&json).unwrap();

        assert_eq!(restored.task_id, checkpoint.task_id);
        assert_eq!(restored.progress, checkpoint.progress);
        assert_eq!(restored.history.len(), 2);
        assert!(restored.context.contains_key("data"));
    }

    // ───────────────────────────────────────────────────────────────────────
    // Async file tests (require tokio runtime)
    // ───────────────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn checkpoint_save_and_load() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test.checkpoint.json");

        let checkpoint =
            PartialCheckpoint::new("test-task", 0.8, "Test content", StopReason::TokensLimit)
                .with_usage(5, 8000, 0.12);

        // Save
        checkpoint.save_to_file(&file_path).await.unwrap();
        assert!(file_path.exists());

        // Load
        let loaded = PartialCheckpoint::load_from_file(&file_path).await.unwrap();
        assert_eq!(&*loaded.task_id, "test-task");
        assert!((loaded.progress - 0.8).abs() < 0.0001);
        assert_eq!(loaded.result.tokens_used, 8000);
    }

    #[tokio::test]
    async fn checkpoint_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("exists.json");

        assert!(!PartialCheckpoint::exists(&file_path).await);

        tokio::fs::write(&file_path, "{}").await.unwrap();
        assert!(PartialCheckpoint::exists(&file_path).await);
    }

    #[tokio::test]
    async fn checkpoint_load_nonexistent() {
        let result = PartialCheckpoint::load_from_file("/nonexistent/path.json").await;
        assert!(result.is_err());
    }
}