nika-engine 0.47.1

Nika workflow engine — embeddable runtime, provider, DAG, and binding logic
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
//! Shared helpers used by verb implementations
//!
//! Free functions used across the per-verb modules (infer, exec, fetch, invoke, agent).
//! All verb `impl TaskExecutor` methods have been extracted into their own modules.

/// Estimate token count from character length using ceiling division.
///
/// Uses ~4 chars/token heuristic. Ceiling division ensures non-empty strings
/// always produce at least 1 token (fixes off-by-one where `len / 4 == 0`
/// for strings shorter than 4 characters).
#[inline]
pub(crate) fn estimate_tokens(char_len: usize) -> u64 {
    char_len.div_ceil(4) as u64
}

/// Estimate the serialized size of a JSON Value without allocating a String.
///
/// Walks the Value tree recursively. Approximate (doesn't account for escaping
/// or exact number formatting) but avoids the allocation from `value.to_string()`.
/// Used by ContextAssembled event to estimate token counts (H10 fix).
pub(crate) fn json_value_size_estimate(value: &serde_json::Value) -> usize {
    match value {
        serde_json::Value::Null => 4,
        serde_json::Value::Bool(b) => if *b { 4 } else { 5 },
        serde_json::Value::Number(n) => {
            // Most numbers are 1-20 chars
            let s = n.to_string();
            s.len()
        }
        serde_json::Value::String(s) => s.len() + 2, // +2 for quotes
        serde_json::Value::Array(arr) => {
            2 + arr.iter().map(json_value_size_estimate).sum::<usize>() + arr.len().saturating_sub(1) // commas
        }
        serde_json::Value::Object(obj) => {
            2 + obj.iter().map(|(k, v)| k.len() + 3 + json_value_size_estimate(v)).sum::<usize>() + obj.len().saturating_sub(1)
        }
    }
}

/// Coerce string values that look like numbers/booleans back to native JSON types.
///
/// Template resolution is string-based: `{{with.count}}` resolving to 42 produces
/// `"42"` (string), not `42` (number). MCP tools expecting integers/booleans fail.
/// This walks the Value tree and converts unambiguous string representations back
/// to their native JSON types.
pub(crate) fn coerce_json_types(value: &mut serde_json::Value) {
    use serde_json::Value;
    match value {
        Value::Object(map) => {
            for v in map.values_mut() {
                coerce_json_types(v);
            }
        }
        Value::Array(arr) => {
            for v in arr {
                coerce_json_types(v);
            }
        }
        Value::String(s) => {
            if let Ok(n) = s.parse::<i64>() {
                *value = Value::Number(n.into());
            } else if let Ok(n) = s.parse::<f64>() {
                if n.is_finite() {
                    if let Some(num) = serde_json::Number::from_f64(n) {
                        *value = Value::Number(num);
                    }
                }
            } else if s == "true" {
                *value = Value::Bool(true);
            } else if s == "false" {
                *value = Value::Bool(false);
            } else if s == "null" {
                *value = Value::Null;
            }
        }
        _ => {}
    }
}

/// Truncate resolved template for event logging (avoids leaking secrets from $env).
#[inline]
pub(crate) fn redact_for_event(s: &str) -> String {
    if s.len() <= 200 {
        s.to_string()
    } else {
        // Find the last valid char boundary at or before byte 200
        let mut boundary = 200;
        while boundary > 0 && !s.is_char_boundary(boundary) {
            boundary -= 1;
        }
        format!("{}... ({} bytes)", &s[..boundary], s.len())
    }
}

/// Detect image MIME type from magic bytes and return rig-core ImageMediaType.
pub(crate) fn detect_image_media_type(
    data: &[u8],
) -> Option<rig::completion::message::ImageMediaType> {
    use rig::completion::message::ImageMediaType;
    if data.len() < 4 {
        return None;
    }
    if data.starts_with(&[0x89, 0x50, 0x4E, 0x47]) {
        Some(ImageMediaType::PNG)
    } else if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
        Some(ImageMediaType::JPEG)
    } else if data.starts_with(b"GIF8") {
        Some(ImageMediaType::GIF)
    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
        Some(ImageMediaType::WEBP)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn resource_text_non_json_returns_string() {
        let text = "Hello, this is plain text from a resource";
        let content_text: Option<String> = Some(text.to_string());
        let result: serde_json::Value = content_text
            .map(|t| serde_json::from_str(&t).unwrap_or(serde_json::Value::String(t)))
            .unwrap_or(serde_json::Value::Null);
        assert!(
            result.is_string(),
            "Non-JSON text should be String, not Null"
        );
        assert_eq!(result.as_str().unwrap(), text);
    }

    #[test]
    fn resource_text_json_returns_parsed() {
        let text = r#"{"key": "value"}"#;
        let content_text: Option<String> = Some(text.to_string());
        let result: serde_json::Value = content_text
            .map(|t| serde_json::from_str(&t).unwrap_or(serde_json::Value::String(t)))
            .unwrap_or(serde_json::Value::Null);
        assert!(result.is_object());
    }

    #[test]
    fn resource_text_none_returns_null() {
        let content_text: Option<String> = None;
        let result: serde_json::Value = content_text
            .map(|t| serde_json::from_str(&t).unwrap_or(serde_json::Value::String(t)))
            .unwrap_or(serde_json::Value::Null);
        assert!(result.is_null());
    }

    // ========================================================================
    // Wave 2: Deep Audit - Bug-Proving Tests
    // ========================================================================

    // ---- BUG: run_agent response extraction loses non-string JSON ----
    // In verbs.rs lines 1153-1158:
    //   let response = result.final_output
    //       .get("response")
    //       .and_then(|v| v.as_str())
    //       .unwrap_or("");
    //
    // If the agent's response is a JSON object (e.g., from structured output),
    // `as_str()` returns None because it's not a string, and the entire response
    // is silently replaced with an empty string.
    //
    // FIX: Use a more robust extraction:
    //   let response = match result.final_output.get("response") {
    //       Some(Value::String(s)) => s.clone(),
    //       Some(v) => v.to_string(), // Serialize non-string JSON
    //       None => String::new(),
    //   };
    #[test]
    fn wave2_run_agent_response_extraction_loses_json_objects() {
        // Simulate what run_agent does when extracting the response
        // The agent wraps its output as: serde_json::json!({ "response": response })

        // Case 1: String response - works fine
        let output_string = serde_json::json!({ "response": "Hello world" });
        let extracted_string = output_string
            .get("response")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        assert_eq!(extracted_string, "Hello world", "String extraction works");

        // Case 2: JSON object response - BUG: silently lost
        let output_object = serde_json::json!({
            "response": {
                "title": "AI Blog Post",
                "content": "This is a structured response",
                "metadata": { "word_count": 42 }
            }
        });
        let extracted_object = output_object
            .get("response")
            .and_then(|v| v.as_str()) // Returns None for objects!
            .unwrap_or("");

        // BUG PROVEN: The entire structured response is lost, replaced with ""
        assert_eq!(
            extracted_object, "",
            "BUG PROVEN: JSON object response is silently replaced with empty string. \
             The response field exists and contains valid JSON, but as_str() returns None \
             for non-string JSON values."
        );

        // Show what the correct extraction would look like
        let correct_extraction = output_object
            .get("response")
            .map(|v| match v {
                serde_json::Value::String(s) => s.clone(),
                other => other.to_string(),
            })
            .unwrap_or_default();
        assert!(
            !correct_extraction.is_empty(),
            "Correct extraction should preserve the JSON object"
        );
        assert!(
            correct_extraction.contains("AI Blog Post"),
            "Correct extraction should contain the title"
        );

        // Case 3: Array response - also lost
        let output_array = serde_json::json!({
            "response": ["item1", "item2", "item3"]
        });
        let extracted_array = output_array
            .get("response")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        assert_eq!(
            extracted_array, "",
            "BUG PROVEN: Array responses are also silently lost"
        );

        // Case 4: Numeric response - also lost
        let output_number = serde_json::json!({ "response": 42 });
        let extracted_number = output_number
            .get("response")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        assert_eq!(
            extracted_number, "",
            "BUG PROVEN: Numeric responses are also silently lost"
        );

        // Case 5: Boolean response - also lost
        let output_bool = serde_json::json!({ "response": true });
        let extracted_bool = output_bool
            .get("response")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        assert_eq!(
            extracted_bool, "",
            "BUG PROVEN: Boolean responses are also silently lost"
        );
    }

    // ========================================================================
    // TDD tests for agent response extraction fix
    // ========================================================================

    #[test]
    fn agent_response_preserves_json_object() {
        let mut output = serde_json::Map::new();
        let obj = serde_json::json!({"title": "Hello", "score": 42});
        output.insert("response".to_string(), obj.clone());
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        // Compare as parsed JSON (key order is non-deterministic)
        let parsed: serde_json::Value = serde_json::from_str(&response).unwrap();
        assert_eq!(parsed, serde_json::json!({"title": "Hello", "score": 42}));
    }

    #[test]
    fn agent_response_preserves_json_array() {
        let mut output = serde_json::Map::new();
        let arr = serde_json::json!(["item1", "item2", "item3"]);
        output.insert("response".to_string(), arr);
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        assert_eq!(response, r#"["item1","item2","item3"]"#);
    }

    #[test]
    fn agent_response_preserves_string() {
        let mut output = serde_json::Map::new();
        output.insert(
            "response".to_string(),
            serde_json::Value::String("Hello world".to_string()),
        );
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        assert_eq!(response, "Hello world");
    }

    #[test]
    fn agent_response_handles_number() {
        let mut output = serde_json::Map::new();
        output.insert("response".to_string(), serde_json::json!(42));
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        assert_eq!(response, "42");
    }

    #[test]
    fn agent_response_handles_boolean() {
        let mut output = serde_json::Map::new();
        output.insert("response".to_string(), serde_json::json!(true));
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        assert_eq!(response, "true");
    }

    #[test]
    fn agent_response_handles_null() {
        let mut output = serde_json::Map::new();
        output.insert("response".to_string(), serde_json::Value::Null);
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        assert_eq!(response, "null");
    }

    #[test]
    fn agent_response_handles_missing_key() {
        let output = serde_json::Map::new();
        let final_output = serde_json::Value::Object(output);

        let response = match final_output.get("response") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
            None => String::new(),
        };

        assert_eq!(response, "");
    }

    // =========================================================================
    // Vision Helper Tests
    // =========================================================================

    #[test]
    fn detect_image_media_type_png() {
        let data = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
        let result = super::detect_image_media_type(&data);
        assert_eq!(result, Some(rig::completion::message::ImageMediaType::PNG));
    }

    #[test]
    fn detect_image_media_type_jpeg() {
        let data = [0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10];
        let result = super::detect_image_media_type(&data);
        assert_eq!(result, Some(rig::completion::message::ImageMediaType::JPEG));
    }

    #[test]
    fn detect_image_media_type_gif() {
        let data = b"GIF89a\x00\x00";
        let result = super::detect_image_media_type(data);
        assert_eq!(result, Some(rig::completion::message::ImageMediaType::GIF));
    }

    #[test]
    fn detect_image_media_type_webp() {
        let data = b"RIFF\x00\x00\x00\x00WEBP";
        let result = super::detect_image_media_type(data);
        assert_eq!(result, Some(rig::completion::message::ImageMediaType::WEBP));
    }

    #[test]
    fn detect_image_media_type_unknown() {
        let data = [0x00, 0x01, 0x02, 0x03];
        let result = super::detect_image_media_type(&data);
        assert_eq!(result, None);
    }

    #[test]
    fn detect_image_media_type_too_small() {
        let data = [0x89, 0x50];
        let result = super::detect_image_media_type(&data);
        assert_eq!(result, None);
    }
}