Skip to main content

qualia_client_core/
vision_pipeline.rs

1//! End-to-end native vision pipeline (MVP + GSW W/G/S).
2//!
3//! Synthetic or raw RGB → detect (reference or production weights) → track →
4//! epistemic quins → overlay BMP. Plus generate + image-to-3D. No Python.
5
6use base64::{engine::general_purpose::STANDARD as B64, Engine as _};
7use qualia_core_db::container_10d::provenance_section::ProvenanceSidecar;
8use qualia_core_db::render::assets::{mesh_to_nquins_with_digests, Mesh};
9use qualia_core_db::render::compile_10d::{
10    compile_mesh_to_10d_vision, compile_mesh_to_10d_vision_with_provenance,
11};
12use qualia_core_db::sparql_library::vision_shacl::{
13    validate_vision_observation_graph, VisionShaclReport,
14};
15use qualia_core_db::specialized_libs::computational_geometry::{
16    decimate_qem, DecimateOptions, Point3,
17};
18use qualia_core_db::tensor::Tensor10D;
19use qualia_core_db::NQuin;
20use qualia_vision::detector::{
21    GridMultiObjectDetector, CLASS_MOSTLY_BLUE, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_RED,
22};
23use qualia_vision::generator::{compile_generation_receipt_quins, NativeImageGenerator};
24use qualia_vision::media_store::{MediaStore, RetentionClass};
25use qualia_vision::metrics::evaluate_synthetic;
26use qualia_vision::overlay::{box_css_percent, compose_rgb_overlay_rgba8, encode_bmp_rgba8};
27use qualia_vision::query_instances_in_region;
28use qualia_vision::semantic::{compile_observation_quins_full, media_digest, VisionQuin};
29use qualia_vision::spatial::{
30    cleanup_mesh_ir, detections_to_node_hints, image_to_heightfield_mesh, mesh_ir_to_export,
31    mesh_ir_to_obj, pack_geometry_export_for_10d, MeshCleanupOptions, MeshIR, NodeHint,
32};
33use qualia_vision::synthetic::{generate_scene_rgb8, sample_id, DatasetSplit, SyntheticSampleId};
34use qualia_vision::tracker::BoundedTracker;
35use qualia_vision::types::{Detection, ImageView, PixelFormat, VisualModel, MAX_DETECTIONS};
36use qualia_vision::weights::{ProductionVision, VisionBackendKind, VisionWeightBundle};
37use serde::Serialize;
38
39use crate::vision_ingest::{
40    append_human_attestation, append_native_observation_quins, human_correct_quin,
41    human_reject_quin, NativeDetection,
42};
43
44#[derive(Debug, Clone, Serialize)]
45pub struct OverlayBoxDto {
46    pub class_hash: String,
47    pub instance_hash: String,
48    pub score: f32,
49    pub track_id: u32,
50    pub frame_index: u32,
51    /// CSS percent: left, top, width, height
52    pub left: f32,
53    pub top: f32,
54    pub width: f32,
55    pub height: f32,
56    pub rejected: bool,
57}
58
59#[derive(Debug, Clone, Serialize)]
60pub struct VisionDemoResult {
61    pub width: u32,
62    pub height: u32,
63    pub seed: u64,
64    pub split: String,
65    pub model_hash: String,
66    pub media_hash: String,
67    pub detections: Vec<OverlayBoxDto>,
68    pub n_gt: usize,
69    pub n_pred: usize,
70    pub quins_written: usize,
71    pub shacl_ok: bool,
72    pub shacl_observations: u32,
73    pub shacl_human: u32,
74    /// data:image/bmp;base64,... with boxes drawn
75    pub overlay_data_url: String,
76    pub note: String,
77    /// `reference` | `production_weights`
78    pub backend: String,
79    pub is_reference_backend: bool,
80    pub synthetic_match_acc: Option<f32>,
81}
82
83#[derive(Debug, Clone, Serialize)]
84pub struct GenerateResult {
85    pub width: u32,
86    pub height: u32,
87    pub seed: u64,
88    pub steps: u32,
89    pub model_hash: String,
90    pub prompt_hash: String,
91    pub output_hash: String,
92    pub is_reference_generator: bool,
93    pub image_data_url: String,
94    pub note: String,
95}
96
97#[derive(Debug, Clone, Serialize)]
98pub struct ImageTo3dResult {
99    pub vertex_count: u32,
100    pub triangle_count: u32,
101    pub mesh_hash: String,
102    pub model_hash: String,
103    pub validation_ok: bool,
104    pub validation_status: String,
105    pub is_reference_recon: bool,
106    pub note: String,
107}
108
109/// Full G→S continuum: generate → media store → recon → validate → OBJ + .10d + quins.
110#[derive(Debug, Clone, Serialize)]
111pub struct GsContinuumResult {
112    pub generate: GenerateResult,
113    pub mesh: ImageTo3dResult,
114    pub media_digest_hex: String,
115    pub media_stored: bool,
116    pub obj_bytes: usize,
117    pub container_10d_bytes: usize,
118    pub geometry_quins: usize,
119    pub generation_quins: usize,
120    pub obj_path: Option<String>,
121    pub container_10d_path: Option<String>,
122    pub note: String,
123}
124
125/// Convert vision MeshIR → core `Mesh` via public export + 10d pack (C1 handoff).
126fn mesh_ir_to_core_mesh(ir: &MeshIR) -> Result<Mesh, String> {
127    let export = mesh_ir_to_export(ir).map_err(|e| format!("mesh_ir_to_export: {e:?}"))?;
128    let g = pack_geometry_export_for_10d(&export);
129    Ok(Mesh {
130        positions: g.positions,
131        triangles: g.triangles,
132        min: g.min,
133        max: g.max,
134    })
135}
136
137/// Map vision [`NodeHint`] → [`Tensor10D`] for spectral paint (D1/D2).
138///
139/// Epistemic parallel context (q=1): vision detections are proposals, not ground truth.
140pub fn node_hint_to_tensor10d(h: &NodeHint) -> Tensor10D {
141    Tensor10D::parallel_context(
142        1.0, // q: proposal
143        0.0, // v: Euclidean
144        0.0, // w: medical-class slot (vision still uses 0 until domain wiring)
145        h.x,
146        h.y,
147        h.z,
148        h.t,
149        1.0, // alpha full amplitude; score is in σ path already
150        0.0,
151        h.sigma.clamp(0.0, 1.0),
152    )
153}
154
155/// Host C2: optional QEM decimate when triangle count exceeds `max_faces`.
156fn maybe_decimate_mesh(mesh: &mut Mesh, max_faces: usize) -> Result<Option<String>, String> {
157    if mesh.triangle_count() <= max_faces || max_faces == 0 {
158        return Ok(None);
159    }
160    let verts: Vec<Point3> = mesh
161        .positions
162        .iter()
163        .map(|p| Point3::new(p[0] as f64, p[1] as f64, p[2] as f64))
164        .collect();
165    let tris = mesh.triangles.clone();
166    let mut out_v = vec![Point3::new(0.0, 0.0, 0.0); verts.len()];
167    let mut out_t = vec![[0u32; 3]; tris.len()];
168    let report = decimate_qem(
169        &verts,
170        &tris,
171        DecimateOptions::to_faces(max_faces),
172        &mut out_v,
173        &mut out_t,
174    )
175    .map_err(|e| format!("decimate_qem: {e:?}"))?;
176    mesh.positions = out_v[..report.vertices]
177        .iter()
178        .map(|p| [p.x as f32, p.y as f32, p.z as f32])
179        .collect();
180    mesh.triangles = out_t[..report.faces].to_vec();
181    // Recompute AABB.
182    let mut min = [f32::INFINITY; 3];
183    let mut max = [f32::NEG_INFINITY; 3];
184    for p in &mesh.positions {
185        for k in 0..3 {
186            min[k] = min[k].min(p[k]);
187            max[k] = max[k].max(p[k]);
188        }
189    }
190    mesh.min = min;
191    mesh.max = max;
192    Ok(Some(format!(
193        "decimated faces {}→{} ({} collapses)",
194        tris.len(),
195        report.faces,
196        report.collapses
197    )))
198}
199
200fn det_to_dto(d: &Detection, rejected: bool) -> OverlayBoxDto {
201    let (left, top, width, height) = box_css_percent(d);
202    OverlayBoxDto {
203        class_hash: format!("0x{:016x}", d.class_hash),
204        instance_hash: format!("0x{:016x}", d.instance_hash),
205        score: d.score_f32(),
206        track_id: d.track_id,
207        frame_index: d.frame_index,
208        left,
209        top,
210        width,
211        height,
212        rejected,
213    }
214}
215
216fn vision_quin_to_nquin(v: &VisionQuin) -> NQuin {
217    NQuin {
218        subject: v.subject,
219        predicate: v.predicate,
220        object: v.object,
221        context: v.context,
222        metadata: v.metadata,
223        parity: v.parity,
224    }
225}
226
227/// Run synthetic sample through detect → track → compile → SHACL → overlay BMP.
228pub fn run_synthetic_demo(
229    split: DatasetSplit,
230    index: u32,
231    width: u32,
232    height: u32,
233) -> Result<VisionDemoResult, String> {
234    run_synthetic_demo_with_backend(split, index, width, height, "reference")
235}
236
237/// Synthetic-vision demo with a chosen backend (self-marking).
238pub fn run_synthetic_demo_with_backend(
239    split: DatasetSplit,
240    index: u32,
241    width: u32,
242    height: u32,
243    backend: &str,
244) -> Result<VisionDemoResult, String> {
245    let sample = sample_id(split, index, width, height);
246    run_sample_demo_with_backend(&sample, backend)
247}
248
249/// Sample demo on the reference backend.
250pub fn run_sample_demo(sample: &SyntheticSampleId) -> Result<VisionDemoResult, String> {
251    run_sample_demo_with_backend(sample, "reference")
252}
253
254/// Sample demo with a chosen backend.
255pub fn run_sample_demo_with_backend(
256    sample: &SyntheticSampleId,
257    backend: &str,
258) -> Result<VisionDemoResult, String> {
259    let w = sample.width;
260    let h = sample.height;
261    let px = (w as usize) * (h as usize);
262    let mut rgb = vec![0u8; px * 3];
263    let mut gt = [Detection::empty(); MAX_DETECTIONS];
264    let n_gt = generate_scene_rgb8(sample, &mut rgb, &mut gt).map_err(|e| format!("{e:?}"))?;
265
266    let img = ImageView {
267        bytes: &rgb,
268        width: w,
269        height: h,
270        row_stride: w * 3,
271        format: PixelFormat::Rgb8,
272    };
273    let mut preds = [Detection::empty(); MAX_DETECTIONS];
274    let mut emb = [0.0f32; 32];
275    let mut ws = [0u8; MAX_DETECTIONS];
276    let use_prod = backend.eq_ignore_ascii_case("production")
277        || backend.eq_ignore_ascii_case("production_weights")
278        || backend.eq_ignore_ascii_case("weights");
279
280    let (n_pred, model_hash, backend_kind, is_ref, synth_acc) = if use_prod {
281        let classes = [CLASS_MOSTLY_RED, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_BLUE];
282        let bundle = VisionWeightBundle::from_seed(0x01D1_FACE_u64, 16, &classes);
283        let mh = bundle.model_hash();
284        let mut prod = ProductionVision::new(bundle);
285        let counts = prod
286            .infer(img, &mut preds, &mut emb, &mut ws)
287            .map_err(|e| format!("{e:?}"))?;
288        let mut m2 =
289            ProductionVision::new(VisionWeightBundle::from_seed(0x01D1_FACE_u64, 16, &classes));
290        let metrics =
291            evaluate_synthetic(&mut m2, VisionBackendKind::ProductionWeights, mh, 4, 32, 24);
292        (
293            counts.detections,
294            mh,
295            "production_weights",
296            false,
297            Some(metrics.mean_match_acc),
298        )
299    } else {
300        let det = GridMultiObjectDetector::new(4, 3);
301        let n_pred = det
302            .detect(img, 0, &mut preds, &mut ws)
303            .map_err(|e| format!("{e:?}"))?;
304        let mh = det.model_hash();
305        let mut det2 = GridMultiObjectDetector::new(4, 3);
306        let metrics = evaluate_synthetic(&mut det2, VisionBackendKind::Reference, mh, 4, 32, 24);
307        (n_pred, mh, "reference", true, Some(metrics.mean_match_acc))
308    };
309
310    let mut tracker = BoundedTracker::new();
311    tracker.update(0, &mut preds, n_pred);
312
313    let digest = media_digest(&rgb);
314    let mut vquins = [VisionQuin::with_parity(0, 0, 0, 0, 0); 256];
315    let n_q = compile_observation_quins_full(digest, &preds[..n_pred], model_hash, &mut vquins);
316
317    let mut nquins = Vec::with_capacity(n_q);
318    for q in vquins.iter().take(n_q) {
319        nquins.push(vision_quin_to_nquin(q));
320    }
321    let report = validate_vision_observation_graph(&nquins);
322
323    let mut rgba = vec![0u8; px * 4];
324    compose_rgb_overlay_rgba8(w, h, &rgb, &preds, n_pred, [0, 255, 180, 255], 2, &mut rgba)
325        .map_err(|e| format!("{e:?}"))?;
326    let mut bmp = vec![0u8; 54 + px * 4];
327    let bmp_n = encode_bmp_rgba8(w, h, &rgba, &mut bmp).map_err(|e| format!("{e:?}"))?;
328    let b64 = B64.encode(&bmp[..bmp_n]);
329
330    let split_s = match sample.split {
331        DatasetSplit::Train => "train",
332        DatasetSplit::Test => "test",
333    };
334
335    let note = if is_ref {
336        "Backend=reference (grid). Epistemic only — not ground truth. H1 real eval not run."
337    } else {
338        "Backend=production_weights (QVWT seed fixture). Synthetic metrics only until H1 corpus."
339    };
340
341    Ok(VisionDemoResult {
342        width: w,
343        height: h,
344        seed: sample.seed,
345        split: split_s.to_string(),
346        model_hash: format!("0x{:016x}", model_hash),
347        media_hash: format!("0x{:016x}", digest.hash),
348        detections: preds[..n_pred]
349            .iter()
350            .map(|d| det_to_dto(d, false))
351            .collect(),
352        n_gt,
353        n_pred,
354        quins_written: n_q,
355        shacl_ok: report.ok,
356        shacl_observations: report.observation_count,
357        shacl_human: report.human_attestation_count,
358        overlay_data_url: format!("data:image/bmp;base64,{b64}"),
359        note: note.into(),
360        backend: backend_kind.into(),
361        is_reference_backend: is_ref,
362        synthetic_match_acc: synth_acc,
363    })
364}
365
366/// Swarm G — native seeded image generation.
367pub fn generate_image(
368    prompt: &str,
369    seed: u64,
370    steps: u32,
371    width: u32,
372    height: u32,
373) -> Result<GenerateResult, String> {
374    let w = width.clamp(8, 256);
375    let h = height.clamp(8, 256);
376    let mut rgb = vec![0u8; (w * h * 3) as usize];
377    let g = NativeImageGenerator::new();
378    let rec = g
379        .generate_rgb8(prompt, seed, steps, w, h, &mut rgb)
380        .map_err(|e| format!("{e:?}"))?;
381    let mut rgba = vec![0u8; (w * h * 4) as usize];
382    for i in 0..(w * h) as usize {
383        rgba[i * 4] = rgb[i * 3];
384        rgba[i * 4 + 1] = rgb[i * 3 + 1];
385        rgba[i * 4 + 2] = rgb[i * 3 + 2];
386        rgba[i * 4 + 3] = 255;
387    }
388    let mut bmp = vec![0u8; 54 + rgba.len()];
389    let n = encode_bmp_rgba8(w, h, &rgba, &mut bmp).map_err(|e| format!("{e:?}"))?;
390    Ok(GenerateResult {
391        width: w,
392        height: h,
393        seed,
394        steps: rec.steps,
395        model_hash: format!("0x{:016x}", rec.model_hash),
396        prompt_hash: format!("0x{:016x}", rec.prompt_hash),
397        output_hash: format!("0x{:016x}", rec.output_digest.hash),
398        is_reference_generator: rec.is_reference_generator,
399        image_data_url: format!("data:image/bmp;base64,{}", B64.encode(&bmp[..n])),
400        note: "Native reference generator (seeded). Not a foundation DiT; swap weights under G0 licence.".into(),
401    })
402}
403
404/// Swarm S-V10 — heightfield recon + validation.
405pub fn image_to_3d_from_rgb(
406    width: u32,
407    height: u32,
408    rgb: &[u8],
409    grid: u32,
410) -> Result<ImageTo3dResult, String> {
411    let img = ImageView {
412        bytes: rgb,
413        width,
414        height,
415        row_stride: width * 3,
416        format: PixelFormat::Rgb8,
417    };
418    let (mesh, rec, rep) = image_to_heightfield_mesh(img, grid).map_err(|e| format!("{e:?}"))?;
419    let status = format!("{:?}", rep.status);
420    Ok(ImageTo3dResult {
421        vertex_count: mesh.vertex_count() as u32,
422        triangle_count: mesh.triangle_count() as u32,
423        mesh_hash: format!("0x{:016x}", mesh.content_hash),
424        model_hash: format!("0x{:016x}", rec.model_hash),
425        validation_ok: rep.ok(),
426        validation_status: status,
427        is_reference_recon: rec.is_reference_recon,
428        note: "Heightfield recon is epistemic proposal; validated before any Q42 geometry commit."
429            .into(),
430    })
431}
432
433/// Generate then reconstruct (G→S10 smoke path).
434pub fn generate_and_reconstruct(
435    prompt: &str,
436    seed: u64,
437) -> Result<(GenerateResult, ImageTo3dResult), String> {
438    let gen = generate_image(prompt, seed, 4, 32, 32)?;
439    let mut rgb = vec![0u8; 32 * 32 * 3];
440    let g = NativeImageGenerator::new();
441    g.generate_rgb8(prompt, seed, 4, 32, 32, &mut rgb)
442        .map_err(|e| format!("{e:?}"))?;
443    let mesh = image_to_3d_from_rgb(32, 32, &rgb, 8)?;
444    Ok((gen, mesh))
445}
446
447/// Full continuum used before handing off to auditory work:
448/// generate → content-addressed store → heightfield recon → validate →
449/// OBJ + sealed `.10d` + generation receipt quins + geometry quins.
450pub fn run_gs_continuum(
451    storage_root: &std::path::Path,
452    prompt: &str,
453    seed: u64,
454    steps: u32,
455    width: u32,
456    height: u32,
457    recon_grid: u32,
458    media_time_ms: u64,
459) -> Result<GsContinuumResult, String> {
460    let w = width.clamp(8, 128);
461    let h = height.clamp(8, 128);
462    let mut rgb = vec![0u8; (w * h * 3) as usize];
463    let gen = NativeImageGenerator::new();
464    let rec = gen
465        .generate_rgb8_cancellable(prompt, seed, steps, w, h, &mut rgb, None, media_time_ms)
466        .map_err(|e| format!("{e:?}"))?;
467
468    // Media store (no partial commit if later steps fail after store — store is deduped).
469    let media_dir = storage_root.join("vision_media");
470    let store = MediaStore::open(&media_dir).map_err(|e| e)?;
471    let now = std::time::SystemTime::now()
472        .duration_since(std::time::UNIX_EPOCH)
473        .map(|d| d.as_secs())
474        .unwrap_or(0);
475    let record = store
476        .import_bytes(
477            &rgb,
478            "application/octet-stream",
479            w,
480            h,
481            RetentionClass::Restricted,
482            now,
483        )
484        .map_err(|e| e)?;
485
486    let mut gen_quins = [VisionQuin::with_parity(0, 0, 0, 0, 0); 8];
487    let n_gen_q = compile_generation_receipt_quins(&rec, &mut gen_quins);
488
489    // BMP preview for API
490    let mut rgba = vec![0u8; (w * h * 4) as usize];
491    for i in 0..(w * h) as usize {
492        rgba[i * 4] = rgb[i * 3];
493        rgba[i * 4 + 1] = rgb[i * 3 + 1];
494        rgba[i * 4 + 2] = rgb[i * 3 + 2];
495        rgba[i * 4 + 3] = 255;
496    }
497    let mut bmp = vec![0u8; 54 + rgba.len()];
498    let bmp_n = encode_bmp_rgba8(w, h, &rgba, &mut bmp).map_err(|e| format!("{e:?}"))?;
499    let generate = GenerateResult {
500        width: w,
501        height: h,
502        seed,
503        steps: rec.steps,
504        model_hash: format!("0x{:016x}", rec.model_hash),
505        prompt_hash: format!("0x{:016x}", rec.prompt_hash),
506        output_hash: format!("0x{:016x}", rec.output_digest.hash),
507        is_reference_generator: rec.is_reference_generator,
508        image_data_url: format!("data:image/bmp;base64,{}", B64.encode(&bmp[..bmp_n])),
509        note: format!(
510            "Stored media digest {}; media_time_ms={media_time_ms} for cross-modal timeline.",
511            record.digest_hex
512        ),
513    };
514
515    let img = ImageView {
516        bytes: &rgb,
517        width: w,
518        height: h,
519        row_stride: w * 3,
520        format: PixelFormat::Rgb8,
521    };
522    let (mut mesh_ir, recon_rec, rep) =
523        image_to_heightfield_mesh(img, recon_grid).map_err(|e| format!("{e:?}"))?;
524    if !rep.ok() {
525        return Err(format!("mesh validation failed: {:?}", rep.status));
526    }
527
528    // C2 vision-side: drop degenerate faces before export.
529    let quality = cleanup_mesh_ir(
530        &mut mesh_ir,
531        MeshCleanupOptions {
532            weld_epsilon: 1e-6,
533            min_area: 0.0,
534        },
535    )
536    .map_err(|e| format!("mesh quality cleanup: {e:?}"))?;
537
538    let mut obj_buf = vec![0u8; mesh_ir.positions.len() * 64 + mesh_ir.indices.len() * 24 + 256];
539    let obj_n = mesh_ir_to_obj(&mesh_ir, &mut obj_buf).map_err(|e| format!("{e:?}"))?;
540    obj_buf.truncate(obj_n);
541
542    let mut core_mesh = mesh_ir_to_core_mesh(&mesh_ir)?;
543    // C2 host: QEM when heightfield is dense (cap 2048 faces for edge browsers).
544    let decimate_note = maybe_decimate_mesh(&mut core_mesh, 2048)?;
545
546    // D1: seal with Tensor10DNodes from a single plane marker (heightfield centre).
547    // Full detection→node path uses seal_vision_mesh_with_detections when callers have dets.
548    let centre = Tensor10D::parallel_context(
549        1.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.35, // mid-band σ for recon marker
550    );
551    // D4: provenance sidecar — media digest bytes + recon model hash in-envelope.
552    let mut version_hash = [0u8; 32];
553    let mh = recon_rec.model_hash.to_le_bytes();
554    version_hash[..8].copy_from_slice(&mh);
555    let dig = record.digest_u64.to_le_bytes();
556    version_hash[8..16].copy_from_slice(&dig);
557    // Source bytes: short media digest prefix (self-authenticating CRC of this payload).
558    let source_tag = format!(
559        "qualia-vision-recon;media={};model=0x{:016x}",
560        record.digest_hex, recon_rec.model_hash
561    );
562    let provenance = ProvenanceSidecar::new(
563        source_tag.into_bytes(),
564        "application/x-qualia-vision-recon",
565        "PermissiveReady-local", // synthetic continuum — not a third-party weight licence
566    )
567    .with_metadata(
568        format!(
569            r#"{{"media_digest":"{}","model_hash":"0x{:016x}"}}"#,
570            record.digest_hex, recon_rec.model_hash
571        )
572        .into_bytes(),
573        0,
574        version_hash,
575    );
576    // C3+D4: topology/spatial when CG linked + provenance always.
577    let container = compile_mesh_to_10d_vision_with_provenance(&core_mesh, &[centre], &provenance)
578        .map_err(|e| e.to_string())?;
579    // CRC of container for compiled digest (first 4 bytes of crc is enough for quin object)
580    let compiled_digest = {
581        let mut h: u32 = 0;
582        for chunk in container.chunks(4) {
583            let mut b = [0u8; 4];
584            b[..chunk.len()].copy_from_slice(chunk);
585            h ^= u32::from_le_bytes(b);
586        }
587        h
588    };
589    let source_digest = (record.digest_u64 & 0xFFFF_FFFF) as u32;
590    let asset_uri = format!("urn:qualia:vision:recon:{}", record.digest_hex);
591    let (geo_quins, _lex) = mesh_to_nquins_with_digests(
592        &core_mesh,
593        &asset_uri,
594        "obj",
595        source_digest,
596        compiled_digest,
597    );
598
599    let out_dir = storage_root
600        .join("vision_geometry")
601        .join(&record.digest_hex);
602    std::fs::create_dir_all(&out_dir).map_err(|e| e.to_string())?;
603    let obj_path = out_dir.join("recon.obj");
604    let c10_path = out_dir.join("recon.10d");
605    std::fs::write(&obj_path, &obj_buf).map_err(|e| e.to_string())?;
606    std::fs::write(&c10_path, &container).map_err(|e| e.to_string())?;
607
608    // Append generation + geometry quins to vision_native.wal when possible
609    let mut nquin_buf = Vec::with_capacity(n_gen_q + geo_quins.len());
610    for q in gen_quins.iter().take(n_gen_q) {
611        nquin_buf.push(NQuin {
612            subject: q.subject,
613            predicate: q.predicate,
614            object: q.object,
615            context: q.context,
616            metadata: q.metadata,
617            parity: q.parity,
618        });
619    }
620    nquin_buf.extend(geo_quins.iter().cloned());
621    let wal_path = storage_root.join("models").join("vision_native.wal");
622    if let Some(parent) = wal_path.parent() {
623        let _ = std::fs::create_dir_all(parent);
624    }
625    if let Ok(mut wal) = qualia_core_db::wal::WriteAheadLog::open(&wal_path) {
626        for q in &nquin_buf {
627            let _ = wal.append_mutation(q);
628        }
629    }
630
631    let mesh = ImageTo3dResult {
632        vertex_count: mesh_ir.vertex_count() as u32,
633        triangle_count: mesh_ir.triangle_count() as u32,
634        mesh_hash: format!("0x{:016x}", mesh_ir.content_hash),
635        model_hash: format!("0x{:016x}", recon_rec.model_hash),
636        validation_ok: true,
637        validation_status: format!("{:?}", rep.status),
638        is_reference_recon: recon_rec.is_reference_recon,
639        note: format!(
640            "Validated MeshIR → cleanup(deg={},weld={}) → OBJ + sealed .10d with Tensor10DNodes; digests on quins.",
641            quality.degenerates_removed, quality.vertices_welded
642        ),
643    };
644
645    let mut note = String::from(
646        "G→S continuum closed: store + validate + cleanup + compile(mesh+nodes). Ready for 10d browse.",
647    );
648    if let Some(d) = decimate_note {
649        note.push(' ');
650        note.push_str(&d);
651    }
652
653    Ok(GsContinuumResult {
654        generate,
655        mesh,
656        media_digest_hex: record.digest_hex,
657        media_stored: true,
658        obj_bytes: obj_n,
659        container_10d_bytes: container.len(),
660        geometry_quins: nquin_buf.len().saturating_sub(n_gen_q),
661        generation_quins: n_gen_q,
662        obj_path: Some(obj_path.display().to_string()),
663        container_10d_path: Some(c10_path.display().to_string()),
664        note,
665    })
666}
667
668/// Seal a MeshIR with detection-derived Tensor10D nodes (D1 product path).
669pub fn seal_vision_mesh_with_detections(
670    mesh_ir: &MeshIR,
671    dets: &[Detection],
672) -> Result<Vec<u8>, String> {
673    let mut cleaned = mesh_ir.clone();
674    let _ = cleanup_mesh_ir(
675        &mut cleaned,
676        MeshCleanupOptions {
677            weld_epsilon: 1e-6,
678            min_area: 0.0,
679        },
680    );
681    let mut core = mesh_ir_to_core_mesh(&cleaned)?;
682    let _ = maybe_decimate_mesh(&mut core, 4096)?;
683    let mut hints = vec![
684        NodeHint {
685            x: 0.0,
686            y: 0.0,
687            z: 0.0,
688            t: 0.0,
689            sigma: 0.0,
690        };
691        dets.len().min(256)
692    ];
693    let n = detections_to_node_hints(dets, &mut hints);
694    let nodes: Vec<Tensor10D> = hints[..n].iter().map(node_hint_to_tensor10d).collect();
695    compile_mesh_to_10d_vision(&core, &nodes).map_err(|e| e.to_string())
696}
697
698/// Persist native observations to WAL and return SHACL report.
699pub fn ingest_demo_to_wal(
700    storage_root: &std::path::Path,
701    demo: &VisionDemoResult,
702) -> Result<VisionShaclReport, String> {
703    let media_hash = u64::from_str_radix(demo.media_hash.trim_start_matches("0x"), 16)
704        .map_err(|e| e.to_string())?;
705    let model_hash = u64::from_str_radix(demo.model_hash.trim_start_matches("0x"), 16)
706        .map_err(|e| e.to_string())?;
707    let mut natives = Vec::new();
708    for d in &demo.detections {
709        let instance_hash = u64::from_str_radix(d.instance_hash.trim_start_matches("0x"), 16)
710            .map_err(|e| e.to_string())?;
711        let class_hash = u64::from_str_radix(d.class_hash.trim_start_matches("0x"), 16)
712            .map_err(|e| e.to_string())?;
713        let (x0, y0, x1, y1) = css_to_u16(d.left, d.top, d.width, d.height);
714        natives.push(NativeDetection {
715            class_hash,
716            instance_hash,
717            score_u16: (d.score.clamp(0.0, 1.0) * 65535.0) as u16,
718            x_min_u16: x0,
719            y_min_u16: y0,
720            x_max_u16: x1,
721            y_max_u16: y1,
722            frame_index: d.frame_index,
723            track_id: d.track_id,
724            flags: 0,
725        });
726    }
727    let n = append_native_observation_quins(
728        storage_root,
729        media_hash,
730        (demo.width as u64) * (demo.height as u64) * 3,
731        model_hash,
732        &natives,
733    )
734    .map_err(|e| e.to_string())?;
735    // Rebuild minimal graph for SHACL
736    let mut buf = [NQuin {
737        subject: 0,
738        predicate: 0,
739        object: 0,
740        context: 0,
741        metadata: 0,
742        parity: 0,
743    }; 256];
744    let written = crate::vision_ingest::compile_native_observation_quins(
745        media_hash,
746        (demo.width as u64) * (demo.height as u64) * 3,
747        model_hash,
748        &natives,
749        &mut buf,
750    )
751    .map_err(|e| e.to_string())?;
752    let _ = n;
753    Ok(validate_vision_observation_graph(&buf[..written]))
754}
755
756fn css_to_u16(left: f32, top: f32, width: f32, height: f32) -> (u16, u16, u16, u16) {
757    let x0 = ((left / 100.0) * 65535.0).clamp(0.0, 65535.0) as u16;
758    let y0 = ((top / 100.0) * 65535.0).clamp(0.0, 65535.0) as u16;
759    let x1 = (((left + width) / 100.0) * 65535.0).clamp(0.0, 65535.0) as u16;
760    let y1 = (((top + height) / 100.0) * 65535.0).clamp(0.0, 65535.0) as u16;
761    (x0, y0, x1, y1)
762}
763
764/// Human rejects instance; machine claims remain. Returns updated attestation note.
765pub fn reject_instance(
766    storage_root: &std::path::Path,
767    human_did_hash: u64,
768    instance_hash: u64,
769    reason_hash: u64,
770) -> Result<(), String> {
771    let q = human_reject_quin(human_did_hash, instance_hash, reason_hash);
772    append_human_attestation(storage_root, &q).map_err(|e| e.to_string())
773}
774
775/// Appends a human-correct attestation quin.
776pub fn correct_instance(
777    storage_root: &std::path::Path,
778    human_did_hash: u64,
779    instance_hash: u64,
780    new_class_hash: u64,
781) -> Result<(), String> {
782    let q = human_correct_quin(human_did_hash, instance_hash, new_class_hash);
783    append_human_attestation(storage_root, &q).map_err(|e| e.to_string())
784}
785
786/// Public helpers for UI without storage.
787pub fn demo_train(index: u32) -> Result<VisionDemoResult, String> {
788    run_synthetic_demo(DatasetSplit::Train, index, 96, 64)
789}
790
791/// Demo on the test split.
792pub fn demo_test(index: u32) -> Result<VisionDemoResult, String> {
793    run_synthetic_demo(DatasetSplit::Test, index, 96, 64)
794}
795
796#[cfg(test)]
797mod tests {
798    use super::*;
799
800    #[test]
801    fn synthetic_demo_produces_overlay_and_shacl() {
802        let r = demo_test(0).expect("demo");
803        assert!(r.n_pred >= 1 || r.n_gt >= 1);
804        assert!(r.overlay_data_url.starts_with("data:image/bmp;base64,"));
805        assert!(r.shacl_ok, "shacl should pass on full compile");
806        assert!(r.quins_written >= 1);
807        assert_eq!(r.backend, "reference");
808    }
809
810    #[test]
811    fn production_backend_labelled() {
812        let r = run_synthetic_demo_with_backend(DatasetSplit::Test, 0, 48, 32, "production")
813            .expect("prod");
814        assert_eq!(r.backend, "production_weights");
815        assert!(!r.is_reference_backend);
816        assert!(r.synthetic_match_acc.is_some());
817    }
818
819    #[test]
820    fn generate_and_recon_smoke() {
821        let (g, m) = generate_and_reconstruct("test field", 7).expect("g+s");
822        assert!(g.is_reference_generator);
823        assert!(m.validation_ok);
824        assert!(m.triangle_count > 0);
825    }
826
827    #[test]
828    fn gs_continuum_writes_obj_and_10d() {
829        let dir = std::env::temp_dir().join(format!(
830            "qv-gs-{}",
831            std::time::SystemTime::now()
832                .duration_since(std::time::UNIX_EPOCH)
833                .unwrap()
834                .as_nanos()
835        ));
836        let _ = std::fs::remove_dir_all(&dir);
837        std::fs::create_dir_all(&dir).unwrap();
838        let r = run_gs_continuum(&dir, "hills", 11, 3, 24, 24, 6, 1000).expect("continuum");
839        assert!(r.media_stored);
840        assert!(r.obj_bytes > 0);
841        assert!(r.container_10d_bytes > 64);
842        assert!(r.geometry_quins >= 1);
843        assert!(r.generation_quins == 3);
844        let obj = r.obj_path.as_ref().unwrap();
845        assert!(std::path::Path::new(obj).is_file());
846        let c10 = r.container_10d_path.as_ref().unwrap();
847        assert!(std::path::Path::new(c10).is_file());
848        let _ = std::fs::remove_dir_all(&dir);
849    }
850
851    #[test]
852    fn section15_smoke_passes() {
853        let s = section15_smoke().expect("§15");
854        assert!(s.contains("OK"));
855    }
856
857    #[test]
858    fn rgb_import_detects() {
859        let dir = std::env::temp_dir().join(format!(
860            "qv-imp-{}",
861            std::time::SystemTime::now()
862                .duration_since(std::time::UNIX_EPOCH)
863                .unwrap()
864                .as_nanos()
865        ));
866        let _ = std::fs::create_dir_all(&dir);
867        let mut rgb = vec![0u8; 16 * 16 * 3];
868        for p in rgb.chunks_mut(3) {
869            p[0] = 220;
870            p[1] = 20;
871            p[2] = 20;
872        }
873        let r = detect_from_rgb8(&dir, &rgb, 16, 16, "reference", true).unwrap();
874        assert!(r.n_pred >= 1);
875        assert!(r.shacl_ok);
876        let _ = std::fs::remove_dir_all(&dir);
877    }
878}
879
880/// Detect from raw RGB8 buffer (file import path).
881pub fn detect_from_rgb8(
882    storage_root: &std::path::Path,
883    rgb: &[u8],
884    width: u32,
885    height: u32,
886    backend: &str,
887    persist: bool,
888) -> Result<VisionDemoResult, String> {
889    if rgb.len() < (width as usize) * (height as usize) * 3 || width == 0 || height == 0 {
890        return Err("bad rgb geometry".into());
891    }
892    let img = ImageView {
893        bytes: rgb,
894        width,
895        height,
896        row_stride: width * 3,
897        format: PixelFormat::Rgb8,
898    };
899    let mut preds = [Detection::empty(); MAX_DETECTIONS];
900    let mut emb = [0.0f32; 32];
901    let mut ws = [0u8; MAX_DETECTIONS];
902    let use_prod = backend.eq_ignore_ascii_case("production")
903        || backend.eq_ignore_ascii_case("production_weights");
904    let (n_pred, model_hash, backend_kind, is_ref) = if use_prod {
905        let classes = [CLASS_MOSTLY_RED, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_BLUE];
906        let bundle = VisionWeightBundle::from_seed(0x01D1_FACE_u64, 16, &classes);
907        let mh = bundle.model_hash();
908        let mut prod = ProductionVision::new(bundle);
909        let counts = prod
910            .infer(img, &mut preds, &mut emb, &mut ws)
911            .map_err(|e| format!("{e:?}"))?;
912        (counts.detections, mh, "production_weights", false)
913    } else {
914        let det = GridMultiObjectDetector::new(4, 3);
915        let n = det
916            .detect(img, 0, &mut preds, &mut ws)
917            .map_err(|e| format!("{e:?}"))?;
918        (n, det.model_hash(), "reference", true)
919    };
920    let mut tracker = BoundedTracker::new();
921    tracker.update(0, &mut preds, n_pred);
922    let digest = media_digest(rgb);
923    if persist {
924        let store = MediaStore::open(storage_root.join("vision_media")).map_err(|e| e)?;
925        let now = std::time::SystemTime::now()
926            .duration_since(std::time::UNIX_EPOCH)
927            .map(|d| d.as_secs())
928            .unwrap_or(0);
929        let _ = store.import_bytes(
930            rgb,
931            "image/x-rgb8",
932            width,
933            height,
934            RetentionClass::Restricted,
935            now,
936        );
937    }
938    let mut vquins = [VisionQuin::with_parity(0, 0, 0, 0, 0); 256];
939    let n_q = compile_observation_quins_full(digest, &preds[..n_pred], model_hash, &mut vquins);
940    let mut nq = Vec::new();
941    for q in vquins.iter().take(n_q) {
942        nq.push(vision_quin_to_nquin(q));
943    }
944    let report = validate_vision_observation_graph(&nq);
945    let mut rgba = vec![0u8; (width * height * 4) as usize];
946    compose_rgb_overlay_rgba8(
947        width,
948        height,
949        rgb,
950        &preds,
951        n_pred,
952        [0, 255, 180, 255],
953        2,
954        &mut rgba,
955    )
956    .map_err(|e| format!("{e:?}"))?;
957    let mut bmp = vec![0u8; 54 + rgba.len()];
958    let bmp_n = encode_bmp_rgba8(width, height, &rgba, &mut bmp).map_err(|e| format!("{e:?}"))?;
959    Ok(VisionDemoResult {
960        width,
961        height,
962        seed: 0,
963        split: "import".into(),
964        model_hash: format!("0x{:016x}", model_hash),
965        media_hash: format!("0x{:016x}", digest.hash),
966        detections: preds[..n_pred]
967            .iter()
968            .map(|d| det_to_dto(d, false))
969            .collect(),
970        n_gt: 0,
971        n_pred,
972        quins_written: n_q,
973        shacl_ok: report.ok,
974        shacl_observations: report.observation_count,
975        shacl_human: report.human_attestation_count,
976        overlay_data_url: format!("data:image/bmp;base64,{}", B64.encode(&bmp[..bmp_n])),
977        note: format!("RGB import path. Backend={backend_kind}. Epistemic only."),
978        backend: backend_kind.into(),
979        is_reference_backend: is_ref,
980        synthetic_match_acc: None,
981    })
982}
983
984/// Decode PNG/JPEG and detect.
985pub fn detect_from_image_file(
986    storage_root: &std::path::Path,
987    path: &std::path::Path,
988    backend: &str,
989) -> Result<VisionDemoResult, String> {
990    let img = image::open(path).map_err(|e| e.to_string())?;
991    let rgb = img.to_rgb8();
992    let (w, h) = rgb.dimensions();
993    detect_from_rgb8(storage_root, rgb.as_raw(), w, h, backend, true)
994}
995
996/// Query observation quins by normalized region (u16).
997pub fn query_vision_region(quins: &[VisionQuin], x0: u16, y0: u16, x1: u16, y1: u16) -> Vec<u64> {
998    let mut out = [0u64; 64];
999    let n = query_instances_in_region(quins, x0, y0, x1, y1, &mut out);
1000    out[..n].to_vec()
1001}
1002
1003/// §15 automated smoke: synthetic detect + SHACL + overlay.
1004pub fn section15_smoke() -> Result<String, String> {
1005    let r = demo_test(0)?;
1006    if !r.shacl_ok {
1007        return Err("SHACL failed".into());
1008    }
1009    if r.n_pred == 0 && r.n_gt == 0 {
1010        return Err("no detections".into());
1011    }
1012    if !r.overlay_data_url.starts_with("data:image/bmp") {
1013        return Err("no overlay".into());
1014    }
1015    Ok(format!(
1016        "section15_smoke OK backend={} dets={} quins={}",
1017        r.backend, r.n_pred, r.quins_written
1018    ))
1019}
1020
1021/// Phase 11–12: twin eligibility + closed-form elasticity preview (A1, not FEA).
1022pub fn twin_elasticity_demo() -> Result<serde_json::Value, String> {
1023    use qualia_vision::spatial::{
1024        closed_form_bar_stretch, promote_elasticity_preview, refuse_fea_unless_eligible,
1025        run_elasticity_preview_if_eligible, BarStretchInput, MeshIR,
1026    };
1027    let mut m = MeshIR::empty();
1028    m.positions = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
1029    m.indices = vec![0, 1, 2];
1030    m.recompute_bounds_and_hash();
1031    let viz = qualia_vision::spatial::assess_twin_eligibility(&m);
1032    let promoted = promote_elasticity_preview(&m);
1033    let refuse_viz = refuse_fea_unless_eligible(viz).is_err();
1034    let r = run_elasticity_preview_if_eligible(
1035        promoted,
1036        BarStretchInput {
1037            force_n: 1000.0,
1038            length_m: 1.0,
1039            area_m2: 0.01,
1040            youngs_pa: 2.0e11,
1041        },
1042    )
1043    .map_err(|e| e.to_string())?;
1044    let _ = closed_form_bar_stretch(BarStretchInput {
1045        force_n: 1.0,
1046        length_m: 1.0,
1047        area_m2: 1.0,
1048        youngs_pa: 1.0e9,
1049    });
1050    Ok(serde_json::json!({
1051        "viz_only_refuses_fea": refuse_viz,
1052        "promoted_domain": format!("{:?}", promoted.domain),
1053        "displacement_m": r.displacement_m,
1054        "assurance": r.assurance_note,
1055        "note": "A1 closed-form bar stretch only — not mesh FEA / not A4."
1056    }))
1057}
1058
1059/// Ensure seed QVWT on disk; load for production path.
1060pub fn ensure_vision_weights(storage_root: &std::path::Path) -> Result<String, String> {
1061    use qualia_vision::detector::{CLASS_MOSTLY_BLUE, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_RED};
1062    let path = storage_root.join("models").join("vision_seed.qvwt");
1063    let classes = [CLASS_MOSTLY_RED, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_BLUE];
1064    if path.is_file() {
1065        let b = VisionWeightBundle::load_path(&path, &classes)?;
1066        return Ok(format!(
1067            "loaded QVWT hash=0x{:016x} path={}",
1068            b.content_hash,
1069            path.display()
1070        ));
1071    }
1072    let b = VisionWeightBundle::from_seed(0x01D1_FACE_u64, 16, &classes);
1073    b.save_path(&path)?;
1074    Ok(format!(
1075        "wrote QVWT seed hash=0x{:016x} path={}",
1076        b.content_hash,
1077        path.display()
1078    ))
1079}
1080
1081/// Detect with QVWT from disk if present.
1082pub fn detect_with_disk_weights(
1083    storage_root: &std::path::Path,
1084    rgb: &[u8],
1085    width: u32,
1086    height: u32,
1087) -> Result<VisionDemoResult, String> {
1088    use qualia_vision::detector::{CLASS_MOSTLY_BLUE, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_RED};
1089    let path = storage_root.join("models").join("vision_seed.qvwt");
1090    let classes = [CLASS_MOSTLY_RED, CLASS_MOSTLY_GREEN, CLASS_MOSTLY_BLUE];
1091    let bundle = if path.is_file() {
1092        VisionWeightBundle::load_path(&path, &classes)?
1093    } else {
1094        let b = VisionWeightBundle::from_seed(0x01D1_FACE_u64, 16, &classes);
1095        b.save_path(&path)?;
1096        b
1097    };
1098    let img = ImageView {
1099        bytes: rgb,
1100        width,
1101        height,
1102        row_stride: width * 3,
1103        format: PixelFormat::Rgb8,
1104    };
1105    let model_hash = bundle.content_hash;
1106    let mut prod = ProductionVision::new(bundle);
1107    let mut preds = [Detection::empty(); MAX_DETECTIONS];
1108    let mut emb = [0.0f32; 32];
1109    let mut ws = [0u8; MAX_DETECTIONS];
1110    let counts = prod
1111        .infer(img, &mut preds, &mut emb, &mut ws)
1112        .map_err(|e| format!("{e:?}"))?;
1113    let digest = media_digest(rgb);
1114    let mut tracker = BoundedTracker::new();
1115    tracker.update(0, &mut preds, counts.detections);
1116    let mut vquins = [VisionQuin::with_parity(0, 0, 0, 0, 0); 256];
1117    let n_q = compile_observation_quins_full(
1118        digest,
1119        &preds[..counts.detections],
1120        model_hash,
1121        &mut vquins,
1122    );
1123    let mut nq = Vec::new();
1124    for q in vquins.iter().take(n_q) {
1125        nq.push(vision_quin_to_nquin(q));
1126    }
1127    let report = validate_vision_observation_graph(&nq);
1128    let mut rgba = vec![0u8; (width * height * 4) as usize];
1129    compose_rgb_overlay_rgba8(
1130        width,
1131        height,
1132        rgb,
1133        &preds,
1134        counts.detections,
1135        [0, 255, 180, 255],
1136        2,
1137        &mut rgba,
1138    )
1139    .map_err(|e| format!("{e:?}"))?;
1140    let mut bmp = vec![0u8; 54 + rgba.len()];
1141    let bmp_n = encode_bmp_rgba8(width, height, &rgba, &mut bmp).map_err(|e| format!("{e:?}"))?;
1142    Ok(VisionDemoResult {
1143        width,
1144        height,
1145        seed: 0,
1146        split: "disk-qvwt".into(),
1147        model_hash: format!("0x{:016x}", model_hash),
1148        media_hash: format!("0x{:016x}", digest.hash),
1149        detections: preds[..counts.detections]
1150            .iter()
1151            .map(|d| det_to_dto(d, false))
1152            .collect(),
1153        n_gt: 0,
1154        n_pred: counts.detections,
1155        quins_written: n_q,
1156        shacl_ok: report.ok,
1157        shacl_observations: report.observation_count,
1158        shacl_human: report.human_attestation_count,
1159        overlay_data_url: format!("data:image/bmp;base64,{}", B64.encode(&bmp[..bmp_n])),
1160        note: "QVWT loaded from models/vision_seed.qvwt (seed weights, not foundation).".into(),
1161        backend: "production_weights".into(),
1162        is_reference_backend: false,
1163        synthetic_match_acc: None,
1164    })
1165}
1166
1167/// Biosense excellence demos (consent-bound recipes; no training).
1168#[derive(Debug, Clone, Serialize)]
1169pub struct BiosensePulseDemo {
1170    pub bpm: f32,
1171    pub confidence: f32,
1172    pub snr: f32,
1173    pub abstained: bool,
1174    pub used_evm: bool,
1175    pub reason: String,
1176}
1177
1178/// Synthetic pulse + optional EVM → rPPG (security consent template for demo).
1179pub fn biosense_self_monitor_pulse_demo(use_evm: bool) -> Result<BiosensePulseDemo, String> {
1180    use qualia_vision::{self_monitor_pulse_evm, synthetic_pulse_sequence, BiosenseConsent};
1181    let seq = synthetic_pulse_sequence(32, 32, 90, 30.0, 72.0).map_err(|e| format!("{e}"))?;
1182    let consent = BiosenseConsent::grant_security_template(1);
1183    let r = self_monitor_pulse_evm(
1184        consent,
1185        seq.as_packed_rgb(),
1186        seq.n_frames,
1187        seq.width,
1188        seq.height,
1189        seq.fps,
1190        use_evm,
1191        0.15,
1192    );
1193    Ok(BiosensePulseDemo {
1194        bpm: r.bpm,
1195        confidence: r.confidence,
1196        snr: r.snr,
1197        abstained: r.abstained,
1198        used_evm: r.used_evm,
1199        reason: r
1200            .reason
1201            .map(|a| format!("{a:?}"))
1202            .unwrap_or_else(|| "ok".into()),
1203    })
1204}
1205
1206/// Super-resolution ("Enhance") result: before/after PNG data URLs + honesty.
1207#[derive(Debug, Clone, serde::Serialize)]
1208pub struct SrResultDto {
1209    pub before_data_url: String,
1210    pub after_data_url: String,
1211    pub backend_id: String,
1212    pub device: String,
1213    pub generative: bool,
1214    pub out_width: u32,
1215    pub out_height: u32,
1216    pub degraded: bool,
1217}
1218
1219/// Decode arbitrary image bytes, classically super-resolve (Enhance), and return
1220/// before/after PNG data URLs plus device/honesty metadata.
1221///
1222/// Mirrors the Listen/audio_capabilities composition: decode → engine op →
1223/// PNG-encode both frames → DTO. Classical SR is non-generative (`Sharpen`).
1224pub fn super_resolve_image(
1225    bytes: &[u8],
1226    scale: u8,
1227    kernel: &str,
1228    prefer_gpu: bool,
1229) -> Result<SrResultDto, String> {
1230    use qualia_core_db::specialized_libs::computer_vision::cv::buffer::RgbView;
1231    use qualia_core_db::specialized_libs::computer_vision::cv::codecs::encode_png;
1232    use qualia_core_db::specialized_libs::computer_vision::gpu::dispatch::VisionComputeDevice;
1233    use qualia_core_db::specialized_libs::computer_vision::gpu::policy::{
1234        ThermalHint, VisionVramBudget,
1235    };
1236    use qualia_core_db::specialized_libs::computer_vision::sr::device_policy::super_resolve_with_policy;
1237    use qualia_core_db::specialized_libs::computer_vision::sr::super_resolve::{
1238        ClassicalKernel, EnhancementMode, SrBackend, SrRequest,
1239    };
1240
1241    if !(2..=4).contains(&scale) {
1242        return Err("scale must be 2..=4".into());
1243    }
1244
1245    // 1. Decode → RGB8.
1246    let img = image::load_from_memory(bytes).map_err(|e| format!("decode image: {e}"))?;
1247    let rgb_img = img.to_rgb8();
1248    let (w, h) = rgb_img.dimensions();
1249    if w == 0 || h == 0 {
1250        return Err("empty image".into());
1251    }
1252    let rgb: Vec<u8> = rgb_img.into_raw();
1253
1254    // 2. Map kernel string (same mapping as mcp vision.rs op).
1255    let ck = match kernel {
1256        "nearest" => ClassicalKernel::Nearest,
1257        "bilinear" => ClassicalKernel::Bilinear,
1258        "lanczos" | "lanczos3" => ClassicalKernel::Lanczos3,
1259        _ => ClassicalKernel::Bicubic,
1260    };
1261    let req = SrRequest {
1262        rgb: &rgb,
1263        width: w,
1264        height: h,
1265        scale,
1266        backend: SrBackend::Classical(ck),
1267        mode: EnhancementMode::Sharpen,
1268    };
1269
1270    // 3. Allocate output and run under device policy.
1271    let ow = w.checked_mul(scale as u32).ok_or("output width overflow")?;
1272    let oh = h
1273        .checked_mul(scale as u32)
1274        .ok_or("output height overflow")?;
1275    let out_len = (ow as usize)
1276        .checked_mul(oh as usize)
1277        .and_then(|n| n.checked_mul(3))
1278        .ok_or("output size overflow")?;
1279    let mut out = vec![0u8; out_len];
1280    let (report, compute) = super_resolve_with_policy(
1281        &req,
1282        prefer_gpu,
1283        ThermalHint::Cool,
1284        VisionVramBudget::default(),
1285        &mut out,
1286    )
1287    .map_err(|e| format!("super_resolve: {e:?}"))?;
1288
1289    // 4. PNG-encode both input and output.
1290    let before_view = RgbView::new(w, h, w.saturating_mul(3), &rgb).ok_or("bad input rgb view")?;
1291    let before_png = encode_png(before_view).map_err(|e| format!("encode before png: {e:?}"))?;
1292    let after_view = RgbView::new(
1293        report.out_width,
1294        report.out_height,
1295        ow.saturating_mul(3),
1296        &out,
1297    )
1298    .ok_or("bad output rgb view")?;
1299    let after_png = encode_png(after_view).map_err(|e| format!("encode after png: {e:?}"))?;
1300
1301    let device = match compute.device {
1302        VisionComputeDevice::Cpu => "cpu",
1303        VisionComputeDevice::SharedGpu => "shared_gpu",
1304        VisionComputeDevice::Unavailable => "unavailable",
1305    };
1306
1307    Ok(SrResultDto {
1308        before_data_url: format!("data:image/png;base64,{}", B64.encode(&before_png)),
1309        after_data_url: format!("data:image/png;base64,{}", B64.encode(&after_png)),
1310        backend_id: report.backend_id.to_string(),
1311        device: device.to_string(),
1312        generative: report.generative,
1313        out_width: report.out_width,
1314        out_height: report.out_height,
1315        degraded: compute.degraded,
1316    })
1317}
1318
1319/// Local CBIR proxy hashes for an RGB buffer (not CLIP).
1320pub fn vision_local_embed_demo(
1321    rgb: &[u8],
1322    width: u32,
1323    height: u32,
1324) -> Result<serde_json::Value, String> {
1325    use qualia_vision::{
1326        ahash_u64, color_hist_embed_rgb, dhash_u64, GrayView, RgbView, COLOR_HIST_EMBED_DIM,
1327    };
1328    let n = (width * height) as usize;
1329    if rgb.len() < n * 3 {
1330        return Err("buffer too small".into());
1331    }
1332    let mut gray = vec![0u8; n];
1333    for i in 0..n {
1334        let o = i * 3;
1335        gray[i] = ((rgb[o] as u16 + rgb[o + 1] as u16 + rgb[o + 2] as u16) / 3) as u8;
1336    }
1337    let g = GrayView::new(width, height, width, &gray).ok_or("bad gray view")?;
1338    let ah = ahash_u64(g).map_err(|e| format!("{e:?}"))?;
1339    let dh = dhash_u64(g).map_err(|e| format!("{e:?}"))?;
1340    let mut hist = [0.0f32; COLOR_HIST_EMBED_DIM];
1341    let rv = RgbView::new(width, height, width * 3, rgb).ok_or("bad rgb view")?;
1342    color_hist_embed_rgb(rv, &mut hist).map_err(|e| format!("{e:?}"))?;
1343    Ok(serde_json::json!({
1344        "ahash": format!("{:016x}", ah),
1345        "dhash": format!("{:016x}", dh),
1346        "hist_dim": COLOR_HIST_EMBED_DIM,
1347        "hist0": hist[0],
1348        "note": "local CBIR proxy; not foundation CLIP"
1349    }))
1350}