nika-engine 0.38.0

Nika workflow engine — embeddable runtime, provider, DAG, and binding logic
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
//! nika:qr_validate — Decode QR codes and compute a scan quality score.
//!
//! Uses `qrcode-ai-scanner-core` for robust QR decoding with multi-decoder
//! strategy (rxing + rqrr) and 4-tier brute-force preprocessing that handles
//! artistic/styled QR codes standard decoders fail on.
//!
//! Returns decoded data, QR metadata (version, EC level, modules), and a
//! weighted 0-100 scannability score from stress tests (blur, downscale, contrast).
//!
//! SECURITY:
//! - Decoded QR data is returned as a string — never executed.
//! - Images decoded via `decode_image_safe()` with Nika resource limits.
//! - Then passed to `multi_decode_image()` / `run_fast_stress_tests()`.

use std::future::Future;
use std::pin::Pin;

use super::context::MediaToolContext;
use super::error::invalid_args;
use super::safety::decode_image_safe;
use super::{MediaOp, MediaOpResult};
use crate::error::NikaError;

pub struct QrValidateOp;

impl MediaOp for QrValidateOp {
    fn name(&self) -> &'static str {
        "qr_validate"
    }

    fn description(&self) -> &'static str {
        "Decode QR codes from an image and compute scan quality score (0-100) via stress tests"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "hash": {
                    "type": "string",
                    "description": "CAS hash of the image containing QR code(s)"
                },
                "fast": {
                    "type": "boolean",
                    "description": "Use fast mode (2 stress tests instead of 6, ~2-3x faster)",
                    "default": true
                }
            },
            "required": ["hash"],
            "additionalProperties": false
        })
    }

    fn execute<'a>(
        &'a self,
        args: serde_json::Value,
        ctx: &'a MediaToolContext,
    ) -> Pin<Box<dyn Future<Output = Result<MediaOpResult, NikaError>> + Send + 'a>> {
        Box::pin(async move {
            ctx.check_cancelled()?;

            let hash = args
                .get("hash")
                .and_then(|v| v.as_str())
                .ok_or_else(|| invalid_args("qr_validate", "missing 'hash'"))?;

            let fast = args.get("fast").and_then(|v| v.as_bool()).unwrap_or(true);

            let data = ctx.read_media(hash).await?;

            // Decode + validate on compute pool (CPU-bound: image decode, multi-decoder, stress tests)
            let result = ctx
                .compute
                .compute(move || -> Result<serde_json::Value, NikaError> {
                    // Phase 1: Safe decode with Nika resource limits (10k x 10k, 256MB)
                    let img = decode_image_safe(&data)?;

                    // Phase 2: Multi-decoder with 4-tier preprocessing (rxing + rqrr)
                    let decode_result = qrcode_ai_scanner_core::decoder::multi_decode_image(&img);

                    match decode_result {
                        Ok(decode) => {
                            // Phase 3: Stress tests for scannability score
                            let stress = if fast {
                                qrcode_ai_scanner_core::scorer::run_fast_stress_tests(&img)
                            } else {
                                qrcode_ai_scanner_core::scorer::run_stress_tests_on_image(&img)
                            };

                            let num_decoders = decode
                                .metadata
                                .as_ref()
                                .map(|m| m.decoders_success.len())
                                .unwrap_or(1);

                            let score = match stress {
                                Ok(ref s) if fast => {
                                    qrcode_ai_scanner_core::scorer::calculate_fast_score(
                                        s,
                                        num_decoders,
                                    )
                                }
                                Ok(ref s) => {
                                    qrcode_ai_scanner_core::scorer::calculate_score(s, num_decoders)
                                }
                                Err(_) => 50, // Decode succeeded but stress tests failed — partial score
                            };

                            let stress_json = stress.as_ref().ok().map(|s| {
                                serde_json::json!({
                                    "original": s.original,
                                    "downscale_50": s.downscale_50,
                                    "downscale_25": s.downscale_25,
                                    "blur_light": s.blur_light,
                                    "blur_medium": s.blur_medium,
                                    "low_contrast": s.low_contrast,
                                })
                            });

                            let metadata_json = decode.metadata.as_ref().map(|m| {
                                serde_json::json!({
                                    "version": m.version,
                                    "error_correction": format!("{:?}", m.error_correction),
                                    "modules": m.modules,
                                    "decoders_success": m.decoders_success,
                                })
                            });

                            let ec = decode
                                .metadata
                                .as_ref()
                                .map(|m| format!("{:?}", m.error_correction))
                                .unwrap_or_default();

                            Ok(serde_json::json!({
                                "decoded": true,
                                "data": decode.content,
                                "error_correction": ec,
                                "scan_score": score,
                                "metadata": metadata_json,
                                "stress_results": stress_json,
                            }))
                        }
                        Err(_) => {
                            // QR decode failed entirely
                            Ok(serde_json::json!({
                                "decoded": false,
                                "data": null,
                                "error_correction": null,
                                "scan_score": 0,
                                "metadata": null,
                                "stress_results": null,
                            }))
                        }
                    }
                })
                .await??;

            Ok(MediaOpResult::Metadata(result))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::media::CasStore;
    use std::sync::Arc;

    async fn setup() -> (tempfile::TempDir, Arc<MediaToolContext>) {
        let dir = tempfile::tempdir().unwrap();
        let ctx = Arc::new(MediaToolContext::new(CasStore::new(dir.path())));
        (dir, ctx)
    }

    /// Generate a clean QR code PNG using the `qrcode` crate (dev-dependency).
    fn fixture_qr_png(text: &str) -> Vec<u8> {
        use image::{ImageEncoder, Luma};

        let code = qrcode::QrCode::new(text.as_bytes()).expect("QR encode should work");
        let module_count = code.width() as u32;

        // Scale up: each module = 8 pixels for reliable scanning
        let scale = 8u32;
        let quiet = 4u32; // quiet zone in modules
        let img_size = (module_count + quiet * 2) * scale;
        let mut img = image::GrayImage::from_pixel(img_size, img_size, Luma([255u8]));

        for y in 0..module_count {
            for x in 0..module_count {
                if code[(x as usize, y as usize)] == qrcode::types::Color::Dark {
                    for dy in 0..scale {
                        for dx in 0..scale {
                            img.put_pixel(
                                (x + quiet) * scale + dx,
                                (y + quiet) * scale + dy,
                                Luma([0u8]),
                            );
                        }
                    }
                }
            }
        }

        let mut buf = Vec::new();
        let encoder = image::codecs::png::PngEncoder::new(&mut buf);
        encoder
            .write_image(
                img.as_raw(),
                img_size,
                img_size,
                image::ExtendedColorType::L8,
            )
            .unwrap();
        buf
    }

    fn fixture_png(w: u32, h: u32, r: u8, g: u8, b: u8) -> Vec<u8> {
        use image::{ImageBuffer, Rgb};
        let img = ImageBuffer::from_pixel(w, h, Rgb([r, g, b]));
        let mut buf = Vec::new();
        let enc = image::codecs::png::PngEncoder::new(&mut buf);
        image::ImageEncoder::write_image(enc, img.as_raw(), w, h, image::ExtendedColorType::Rgb8)
            .unwrap();
        buf
    }

    #[tokio::test]
    async fn qr_decode_valid_qr() {
        let (_dir, ctx) = setup().await;
        let qr_png = fixture_qr_png("https://qrcode-ai.com/test");
        let sr = ctx.cas.store(&qr_png).await.unwrap();

        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": sr.hash}), &ctx)
            .await
            .unwrap();

        if let MediaOpResult::Metadata(v) = result {
            assert_eq!(v["decoded"], true);
            assert_eq!(v["data"], "https://qrcode-ai.com/test");
            assert!(v["scan_score"].as_u64().unwrap() > 0, "score should be > 0");
        } else {
            panic!("expected Metadata result");
        }
    }

    #[tokio::test]
    async fn qr_decode_returns_metadata() {
        let (_dir, ctx) = setup().await;
        let qr_png = fixture_qr_png("hello");
        let sr = ctx.cas.store(&qr_png).await.unwrap();

        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": sr.hash}), &ctx)
            .await
            .unwrap();
        if let MediaOpResult::Metadata(v) = result {
            assert!(v["metadata"].is_object(), "metadata should be present");
            let meta = &v["metadata"];
            assert!(meta["version"].is_number(), "QR version should be present");
            assert!(meta["error_correction"].is_string(), "EC should be present");
        }
    }

    #[tokio::test]
    async fn qr_decode_returns_stress_results() {
        let (_dir, ctx) = setup().await;
        let qr_png = fixture_qr_png("stress-test");
        let sr = ctx.cas.store(&qr_png).await.unwrap();

        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": sr.hash, "fast": true}), &ctx)
            .await
            .unwrap();
        if let MediaOpResult::Metadata(v) = result {
            assert!(
                v["stress_results"].is_object(),
                "stress_results should be present"
            );
        }
    }

    #[tokio::test]
    async fn qr_not_a_qr_returns_decoded_false() {
        let (_dir, ctx) = setup().await;
        let png = fixture_png(100, 100, 255, 0, 0);
        let sr = ctx.cas.store(&png).await.unwrap();

        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": sr.hash}), &ctx)
            .await
            .unwrap();
        if let MediaOpResult::Metadata(v) = result {
            assert_eq!(v["decoded"], false);
            assert_eq!(v["scan_score"], 0);
        }
    }

    #[tokio::test]
    async fn qr_missing_hash_param() {
        let (_dir, ctx) = setup().await;
        let op = QrValidateOp;
        let result = op.execute(serde_json::json!({}), &ctx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("NIKA-294"));
    }

    #[tokio::test]
    async fn qr_nonexistent_hash() {
        let (_dir, ctx) = setup().await;
        let op = QrValidateOp;
        let result = op.execute(
            serde_json::json!({"hash": "blake3:0000000000000000000000000000000000000000000000000000000000000000"}),
            &ctx,
        ).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn qr_non_image_data() {
        let (_dir, ctx) = setup().await;
        let sr = ctx.cas.store(b"this is not an image").await.unwrap();
        let op = QrValidateOp;
        let result = op.execute(serde_json::json!({"hash": sr.hash}), &ctx).await;
        assert!(result.is_err(), "non-image data should error");
    }

    #[tokio::test]
    async fn qr_cancelled_workflow() {
        let (_dir, ctx) = setup().await;
        ctx.cancel.cancel();
        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": "blake3:abc"}), &ctx)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("cancelled"));
    }

    #[tokio::test]
    async fn qr_fuzz_no_panic() {
        let (_dir, ctx) = setup().await;
        let op = QrValidateOp;
        for i in 1..20u8 {
            let data: Vec<u8> = (0..=i).collect();
            if let Ok(sr) = ctx.cas.store(&data).await {
                let result = op.execute(serde_json::json!({"hash": sr.hash}), &ctx).await;
                if let Err(e) = &result {
                    assert!(
                        !e.to_string().contains("panicked"),
                        "fuzz input {i} panicked"
                    );
                }
            }
        }
    }

    #[tokio::test]
    async fn qr_score_clean_qr_is_high() {
        let (_dir, ctx) = setup().await;
        let qr_png = fixture_qr_png("high-quality");
        let sr = ctx.cas.store(&qr_png).await.unwrap();

        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": sr.hash}), &ctx)
            .await
            .unwrap();
        if let MediaOpResult::Metadata(v) = result {
            let score = v["scan_score"].as_u64().unwrap();
            assert!(score >= 50, "clean QR should score >= 50, got {score}");
        }
    }

    #[tokio::test]
    async fn qr_adapter_dispatch() {
        use crate::runtime::builtin::BuiltinTool;
        let (_dir, ctx) = setup().await;
        let adapter = super::super::MediaToolAdapter::new(Arc::new(QrValidateOp), ctx);
        assert_eq!(adapter.name(), "qr_validate");
    }

    #[tokio::test]
    async fn qr_full_mode_works() {
        let (_dir, ctx) = setup().await;
        let qr_png = fixture_qr_png("full-mode-test");
        let sr = ctx.cas.store(&qr_png).await.unwrap();

        let op = QrValidateOp;
        let result = op
            .execute(serde_json::json!({"hash": sr.hash, "fast": false}), &ctx)
            .await
            .unwrap();
        if let MediaOpResult::Metadata(v) = result {
            assert_eq!(v["decoded"], true);
            let stress = &v["stress_results"];
            // Full mode should have all 6 stress test fields
            assert!(stress["original"].is_boolean());
            assert!(stress["blur_medium"].is_boolean());
            assert!(stress["low_contrast"].is_boolean());
        }
    }
}