nika-core 0.62.0

Lightweight AST and analysis core for Nika workflows
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
//! Artifact configuration for file output
//!
//! Provides declarative file persistence for workflow task outputs.
//! Artifacts are written atomically and tracked in a manifest.
//!
//! # YAML Syntax
//!
//! ```yaml
//! # Workflow-level defaults
//! artifacts:
//!   dir: ./output/{{context.meta.date}}
//!   format: json
//!   mode: overwrite
//!   manifest: true
//!   max_size: 104857600  # 100MB
//!
//! tasks:
//!   # Simple: use defaults
//!   - id: task1
//!     artifact: true
//!
//!   # Custom path
//!   - id: task2
//!     artifact:
//!       path: ./data/{{context.meta.task_id}}.json
//!
//!   # Multiple outputs
//!   - id: task3
//!     artifact:
//!       - path: ./raw.json
//!         source: raw_data
//!       - path: ./processed.json
//! ```

use serde::{Deserialize, Serialize};

// ═══════════════════════════════════════════════════════════════════════════
// WORKFLOW-LEVEL CONFIG
// ═══════════════════════════════════════════════════════════════════════════

/// Default max artifact size: 100MB
pub const DEFAULT_MAX_ARTIFACT_SIZE: u64 = 100 * 1024 * 1024;

/// Workflow-level artifact defaults
///
/// Configures default behavior for all task artifacts.
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct ArtifactsConfig {
    /// Base directory for artifacts (supports context.meta.* templates)
    #[serde(default)]
    pub dir: Option<String>,

    /// Default output format
    #[serde(default)]
    pub format: ArtifactFormat,

    /// Default write mode
    #[serde(default)]
    pub mode: ArtifactMode,

    /// Generate artifacts.json manifest at workflow end
    #[serde(default)]
    pub manifest: bool,

    /// Maximum artifact size in bytes (default: 100MB)
    #[serde(default = "default_max_size")]
    pub max_size: u64,
}

fn default_max_size() -> u64 {
    DEFAULT_MAX_ARTIFACT_SIZE
}

// ═══════════════════════════════════════════════════════════════════════════
// TASK-LEVEL SPEC
// ═══════════════════════════════════════════════════════════════════════════

/// Task-level artifact specification
///
/// Supports three forms:
/// - `artifact: true` — use workflow defaults
/// - `artifact: { path: ... }` — single artifact with options
/// - `artifact: [{ path: ... }, ...]` — multiple artifacts
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ArtifactSpec {
    /// Simple boolean: use defaults
    Enabled(bool),

    /// Single artifact with options
    Single(ArtifactOutput),

    /// Multiple artifacts
    Multiple(Vec<ArtifactOutput>),
}

impl Default for ArtifactSpec {
    fn default() -> Self {
        Self::Enabled(false)
    }
}

/// Individual artifact output configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ArtifactOutput {
    /// Output path (supports context.meta.* templates)
    pub path: String,

    /// Which binding to save (default: task result)
    #[serde(default)]
    pub source: Option<String>,

    /// Template string to render (supports {{with.*}} bindings)
    /// If set, this is used instead of task output
    #[serde(default)]
    pub template: Option<String>,

    /// Output format override
    #[serde(default)]
    pub format: Option<ArtifactFormat>,

    /// Write mode override
    #[serde(default)]
    pub mode: Option<ArtifactMode>,
}

// ═══════════════════════════════════════════════════════════════════════════
// ENUMS
// ═══════════════════════════════════════════════════════════════════════════

/// Artifact output format
#[derive(Debug, Clone, Copy, Deserialize, Serialize, Default, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum ArtifactFormat {
    /// Plain text (default)
    #[default]
    Text,

    /// JSON (pretty-printed)
    Json,

    /// YAML format
    Yaml,

    /// Binary (raw bytes from CAS store)
    Binary,

    /// Markdown format
    Markdown,
}

impl std::fmt::Display for ArtifactFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Text => write!(f, "text"),
            Self::Json => write!(f, "json"),
            Self::Yaml => write!(f, "yaml"),
            Self::Binary => write!(f, "binary"),
            Self::Markdown => write!(f, "markdown"),
        }
    }
}

impl ArtifactFormat {
    /// Get file extension for this format
    pub fn extension(&self) -> &'static str {
        match self {
            Self::Text => "txt",
            Self::Json => "json",
            Self::Yaml => "yaml",
            Self::Binary => "bin",
            Self::Markdown => "md",
        }
    }
}

/// File write mode
#[derive(Debug, Clone, Copy, Deserialize, Serialize, Default, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum ArtifactMode {
    /// Overwrite existing file (default)
    #[default]
    Overwrite,

    /// Append to existing file
    Append,

    /// Create unique filename if exists (file.json → file-1.json)
    Unique,

    /// Fail if file already exists
    Fail,
}

impl std::fmt::Display for ArtifactMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Overwrite => write!(f, "overwrite"),
            Self::Append => write!(f, "append"),
            Self::Unique => write!(f, "unique"),
            Self::Fail => write!(f, "fail"),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// MANIFEST ENTRY
// ═══════════════════════════════════════════════════════════════════════════

/// Artifact metadata for manifest and events
///
/// Returned by ArtifactWriter after successful write.
/// Accumulated by Runner for manifest generation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArtifactEntry {
    /// Task that produced this artifact
    pub task_id: String,

    /// Absolute path where artifact was written
    pub path: String,

    /// File size in bytes
    pub size: u64,

    /// Format used (text, json, yaml)
    pub format: String,

    /// xxHash3 checksum (not cryptographic, for integrity)
    pub checksum: String,

    /// Creation timestamp (UTC)
    pub created_at: chrono::DateTime<chrono::Utc>,
}

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

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

    #[test]
    fn test_parse_artifact_enabled() {
        let yaml = r#"true"#;
        let spec: ArtifactSpec = serde_yaml::from_str(yaml).unwrap();
        assert!(matches!(spec, ArtifactSpec::Enabled(true)));
    }

    #[test]
    fn test_parse_artifact_disabled() {
        let yaml = r#"false"#;
        let spec: ArtifactSpec = serde_yaml::from_str(yaml).unwrap();
        assert!(matches!(spec, ArtifactSpec::Enabled(false)));
    }

    #[test]
    fn test_parse_artifact_single() {
        let yaml = r#"
path: ./output/{{context.meta.task_id}}.json
mode: unique
"#;
        let spec: ArtifactSpec = serde_yaml::from_str(yaml).unwrap();
        match spec {
            ArtifactSpec::Single(output) => {
                assert!(output.path.contains("context.meta.task_id"));
                assert_eq!(output.mode, Some(ArtifactMode::Unique));
            }
            _ => panic!("Expected Single artifact"),
        }
    }

    #[test]
    fn test_parse_artifact_multiple() {
        let yaml = r#"
- path: ./raw.json
  source: raw_data
- path: ./processed.json
  format: json
"#;
        let spec: ArtifactSpec = serde_yaml::from_str(yaml).unwrap();
        match spec {
            ArtifactSpec::Multiple(outputs) => {
                assert_eq!(outputs.len(), 2);
                assert_eq!(outputs[0].source, Some("raw_data".to_string()));
                assert_eq!(outputs[1].format, Some(ArtifactFormat::Json));
            }
            _ => panic!("Expected Multiple artifacts"),
        }
    }

    #[test]
    fn test_parse_artifacts_config() {
        let yaml = r#"
dir: ./output/{{context.meta.date}}
format: json
mode: overwrite
manifest: true
max_size: 52428800
"#;
        let config: ArtifactsConfig = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(
            config.dir,
            Some("./output/{{context.meta.date}}".to_string())
        );
        assert_eq!(config.format, ArtifactFormat::Json);
        assert_eq!(config.mode, ArtifactMode::Overwrite);
        assert!(config.manifest);
        assert_eq!(config.max_size, 50 * 1024 * 1024); // 50MB
    }

    #[test]
    fn test_artifacts_config_defaults() {
        let yaml = "{}";
        let config: ArtifactsConfig = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(config.dir, None);
        assert_eq!(config.format, ArtifactFormat::Text);
        assert_eq!(config.mode, ArtifactMode::Overwrite);
        assert!(!config.manifest);
        assert_eq!(config.max_size, DEFAULT_MAX_ARTIFACT_SIZE);
    }

    #[test]
    fn test_artifact_format_display() {
        assert_eq!(ArtifactFormat::Text.to_string(), "text");
        assert_eq!(ArtifactFormat::Json.to_string(), "json");
        assert_eq!(ArtifactFormat::Yaml.to_string(), "yaml");
        assert_eq!(ArtifactFormat::Binary.to_string(), "binary");
        assert_eq!(ArtifactFormat::Markdown.to_string(), "markdown");
    }

    #[test]
    fn test_artifact_format_extension() {
        assert_eq!(ArtifactFormat::Text.extension(), "txt");
        assert_eq!(ArtifactFormat::Json.extension(), "json");
        assert_eq!(ArtifactFormat::Yaml.extension(), "yaml");
        assert_eq!(ArtifactFormat::Binary.extension(), "bin");
        assert_eq!(ArtifactFormat::Markdown.extension(), "md");
    }

    #[test]
    fn test_artifact_mode_display() {
        assert_eq!(ArtifactMode::Overwrite.to_string(), "overwrite");
        assert_eq!(ArtifactMode::Append.to_string(), "append");
        assert_eq!(ArtifactMode::Unique.to_string(), "unique");
        assert_eq!(ArtifactMode::Fail.to_string(), "fail");
    }

    #[test]
    fn test_artifact_format_binary_serde() {
        // Deserialize from YAML
        let yaml = r#""binary""#;
        let format: ArtifactFormat = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(format, ArtifactFormat::Binary);

        // Serialize to JSON
        let json = serde_json::to_string(&format).unwrap();
        assert_eq!(json, r#""binary""#);

        // Deserialize from JSON
        let back: ArtifactFormat = serde_json::from_str(&json).unwrap();
        assert_eq!(back, ArtifactFormat::Binary);

        // Extension
        assert_eq!(ArtifactFormat::Binary.extension(), "bin");

        // Display
        assert_eq!(ArtifactFormat::Binary.to_string(), "binary");
    }

    #[test]
    fn test_artifact_format_binary_in_artifact_spec() {
        let yaml = r#"
path: ./output/image.bin
format: binary
"#;
        let spec: ArtifactSpec = serde_yaml::from_str(yaml).unwrap();
        match spec {
            ArtifactSpec::Single(output) => {
                assert_eq!(output.format, Some(ArtifactFormat::Binary));
                assert_eq!(output.path, "./output/image.bin");
            }
            _ => panic!("Expected Single artifact"),
        }
    }

    #[test]
    fn test_artifact_format_markdown_serde() {
        // Deserialize from YAML
        let yaml = r#""markdown""#;
        let format: ArtifactFormat = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(format, ArtifactFormat::Markdown);

        // Serialize to JSON
        let json = serde_json::to_string(&format).unwrap();
        assert_eq!(json, r#""markdown""#);

        // Deserialize from JSON
        let back: ArtifactFormat = serde_json::from_str(&json).unwrap();
        assert_eq!(back, ArtifactFormat::Markdown);

        // Extension
        assert_eq!(ArtifactFormat::Markdown.extension(), "md");

        // Display
        assert_eq!(ArtifactFormat::Markdown.to_string(), "markdown");
    }

    #[test]
    fn test_artifact_format_markdown_in_artifact_spec() {
        let yaml = r#"
path: report.md
format: markdown
"#;
        let spec: ArtifactSpec = serde_yaml::from_str(yaml).unwrap();
        match spec {
            ArtifactSpec::Single(output) => {
                assert_eq!(output.format, Some(ArtifactFormat::Markdown));
                assert_eq!(output.path, "report.md");
            }
            _ => panic!("Expected Single artifact"),
        }
    }

    #[test]
    fn test_artifact_entry_serialization() {
        let entry = ArtifactEntry {
            task_id: "test_task".to_string(),
            path: "/tmp/output.json".to_string(),
            size: 1234,
            format: "json".to_string(),
            checksum: "abc123".to_string(),
            created_at: chrono::Utc::now(),
        };

        let json = serde_json::to_string(&entry).unwrap();
        assert!(json.contains("test_task"));
        assert!(json.contains("1234"));
    }
}