Skip to main content

qualia_cli/handlers/
misc.rs

1use std::fs::OpenOptions;
2use std::io::{Read, Write};
3use std::path::PathBuf;
4
5use qualia_core_db::NQuin;
6
7use crate::cli::{
8    CompileAction, ExtensionAction, GovernanceAction, IngestFormat, IngestJobAction, MigrateAction,
9    ProfileAction, Q42Action, QueryDialect, ShaclAction,
10};
11use crate::sparql::run_sparql_query;
12
13/// Handles the  action.
14pub fn handle_extension(action: &ExtensionAction) {
15    match action {
16        ExtensionAction::Register { manifest_path } => {
17            println!("Registering extension from {:?}", manifest_path);
18        }
19        ExtensionAction::List => {
20            println!("Listing registered extensions...");
21        }
22        ExtensionAction::Dispatch { id, input } => {
23            println!("Dispatching to extension '{}': {}", id, input);
24        }
25    }
26}
27
28/// Handles the  action.
29pub fn handle_governance(action: &GovernanceAction) {
30    match action {
31        GovernanceAction::WalAppend { quin, sign } => {
32            println!("Appending to WAL: {} (signed by {})", quin, sign);
33        }
34        GovernanceAction::Ratify { agreement_did } => {
35            println!("Ratifying agreement: {}", agreement_did);
36        }
37    }
38}
39
40/// Handles the  action.
41pub fn handle_compile(action: &CompileAction) {
42    match action {
43        CompileAction::N3ToDeontic { file } => {
44            println!("Compiling N3 logic to native norms from {:?}", file);
45        }
46        CompileAction::EntityCompile(args) | CompileAction::TwinCompile(args) => {
47            if let Err(e) = super::entity_compile::handle(args) {
48                eprintln!("Error compiling entity: {e}");
49            }
50        }
51        CompileAction::EntityVerify(args) | CompileAction::TwinVerify(args) => {
52            if let Err(e) = super::entity_verify::handle(args) {
53                eprintln!("Error verifying entity: {e}");
54            }
55        }
56    }
57}
58
59/// Handles the  action.
60pub fn handle_shacl(action: &ShaclAction) {
61    match action {
62        ShaclAction::List => {
63            println!("============================================================");
64            println!("⚙️  QualiaDB SHACL Extensions Active in Binary");
65            println!("============================================================");
66            println!("  - DeonticObligate");
67            println!("  - DeonticPermit");
68            println!("  - DeonticForbid");
69            println!("  - DeonticNotExpired");
70            println!("  - EpistemicKnowledge");
71            println!("  - EpistemicBelief");
72            println!("  - CommonKnowledge");
73            println!("============================================================");
74        }
75        ShaclAction::Validate { dataset, shapes } => {
76            println!(
77                "Validating {:?} against SHACL shapes in {:?}...",
78                dataset, shapes
79            );
80        }
81    }
82}
83
84/// Handles the  action.
85pub fn handle_vault(init: bool) {
86    if init {
87        println!("Initializing Memory-Mapped Vault...");
88        let storage_dir = std::env::var("QUALIA_DATA_DIR").unwrap_or_else(|_| ".".to_string());
89        let _vault = qualia_core_db::key_vault::KeyVault::load_or_generate(&storage_dir)
90            .expect("Failed to load KeyVault");
91        println!("Vault Initialization Complete!");
92    }
93}
94
95/// Handles the  action.
96pub fn handle_migrate(action: &MigrateAction) -> Result<(), Box<dyn std::error::Error>> {
97    match action {
98        MigrateAction::Meta { path, dry_run } => {
99            if *dry_run {
100                use std::fs::File;
101                use std::io::Read as _;
102                let mut f = File::open(path)?;
103                let mut magic = [0u8; 6];
104                f.read_exact(&mut magic)?;
105                let version = u16::from_le_bytes([magic[4], magic[5]]);
106                if version >= 3 {
107                    println!(
108                        "[dry-run] {} is already v3 — no migration needed.",
109                        path.display()
110                    );
111                } else {
112                    println!("[dry-run] {} is v{version} — would migrate to v3 (Lamport bits [60:32]→[31:0], header bump).", path.display());
113                }
114            } else {
115                println!("Migrating {} to Q42 v3…", path.display());
116                qualia_core_db::q42_volume::migrate_v2_to_v3(path)?;
117                println!("Migration complete: {} is now v3.", path.display());
118            }
119        }
120    }
121    Ok(())
122}
123
124/// Handles the  action.
125pub fn handle_mem(inspect: bool) {
126    if inspect {
127        println!("Please use `qualia-cli inspect <superblock_path>` directly to inspect specific layouts.");
128    }
129}
130
131/// Handles the  action.
132pub fn handle_q42(action: &Q42Action) -> Result<(), Box<dyn std::error::Error>> {
133    match action {
134        Q42Action::Inspect { path } => {
135            let report = qualia_core_db::q42_volume::Q42InspectReport::from_path(path)?;
136            print!("{}", report.to_text());
137        }
138        Q42Action::Verify { path, level } => {
139            let level = qualia_core_db::q42_volume::VerifyLevel::parse(level)?;
140            let report = qualia_core_db::q42_volume::verify_volume_set_from_root(path, level)?;
141            print!("{}", report.to_text());
142            match report.overall {
143                qualia_core_db::q42_volume::CheckStatus::Fail => {
144                    return Err("Q42 verify failed".into());
145                }
146                qualia_core_db::q42_volume::CheckStatus::Incomplete => {
147                    return Err("Q42 verify incomplete".into());
148                }
149                _ => {}
150            }
151        }
152        Q42Action::Magnet {
153            path,
154            name,
155            port,
156            webseed,
157            set,
158            commons,
159        } => {
160            let intent = if *commons {
161                qualia_core_db::q42_volume::PublicationIntent::CommonsCatalog
162            } else {
163                qualia_core_db::q42_volume::PublicationIntent::Default
164            };
165            if *set {
166                let base = webseed
167                    .clone()
168                    .unwrap_or_else(|| format!("http://127.0.0.1:{port}/torrent/webseed/{{hash}}"));
169                let set = qualia_core_db::q42_volume::Q42VolumeSetMagnets::for_root_with_intent(
170                    path,
171                    Some(&base),
172                    intent,
173                )?;
174                println!("{}", set.root.magnet_uri);
175                for child in set.children {
176                    println!("{}", child.magnet_uri);
177                }
178            } else {
179                let display = name.clone().unwrap_or_else(|| {
180                    path.file_name()
181                        .and_then(|n| n.to_str())
182                        .unwrap_or("volume.q42")
183                        .to_string()
184                });
185                let magnet = if let Some(ws) = webseed {
186                    qualia_core_db::q42_volume::Q42Magnet::for_path_named_with_intent(
187                        path,
188                        &display,
189                        Some(ws),
190                        intent,
191                    )?
192                } else {
193                    qualia_core_db::q42_volume::Q42Magnet::for_daemon_seed_with_intent(
194                        path, &display, *port, intent,
195                    )?
196                };
197                println!("{}", magnet.magnet_uri);
198            }
199        }
200        Q42Action::Compact { root, out } => {
201            let out_dir = out.clone().unwrap_or_else(|| {
202                root.parent()
203                    .unwrap_or_else(|| std::path::Path::new("."))
204                    .join("compacted")
205            });
206            let produced = qualia_core_db::q42_volume::compact_volume_set(root, &out_dir)?;
207            println!("{}", produced.display());
208        }
209        Q42Action::Seed {
210            path,
211            name,
212            id,
213            commons,
214        } => {
215            let intent = if *commons {
216                qualia_core_db::q42_volume::PublicationIntent::CommonsCatalog
217            } else {
218                qualia_core_db::q42_volume::PublicationIntent::Default
219            };
220            let display = name.clone().unwrap_or_else(|| {
221                path.file_name()
222                    .and_then(|n| n.to_str())
223                    .unwrap_or("volume.q42")
224                    .to_string()
225            });
226            let ontology_id = id.clone().unwrap_or_else(|| display.clone());
227            let magnet = qualia_core_db::q42_volume::Q42Magnet::for_daemon_seed_with_intent(
228                path, &display, 4242, intent,
229            )?;
230            let record = qualia_core_db::webtorrent_seeder::register_seed(
231                qualia_core_db::webtorrent_seeder::RegisterSeedRequest {
232                    info_hash: magnet.info_hash_sha1.clone(),
233                    file_path: path.display().to_string(),
234                    display_name: display,
235                    ontology_id,
236                    bandwidth_limit_kbps: 512,
237                    commons_asserted: *commons,
238                },
239            )
240            .map_err(|e| e.to_string())?;
241            println!("{}", magnet.magnet_uri);
242            println!("seeded {} bytes as {}", record.file_size, record.info_hash);
243        }
244        Q42Action::AuditSource {
245            path,
246            source_root,
247            top,
248            json_out,
249        } => {
250            if let Some(root) = source_root {
251                let matrix = qualia_core_db::query::source_audit::audit_source_root(root)?;
252                let json = serde_json::to_string_pretty(&matrix)?;
253                if let Some(out) = json_out {
254                    std::fs::write(out, &json)?;
255                    println!("wrote {}", out.display());
256                } else {
257                    println!("{json}");
258                }
259            } else {
260                let path = path
261                    .as_ref()
262                    .ok_or("q42 audit-source requires PATH or --source-root")?;
263                let report = qualia_core_db::query::source_audit::audit_rdf_source(path, *top)?;
264                let json = serde_json::to_string_pretty(&report)?;
265                if let Some(out) = json_out {
266                    std::fs::write(out, &json)?;
267                    println!("wrote {}", out.display());
268                } else {
269                    println!("{json}");
270                }
271            }
272        }
273    }
274    Ok(())
275}
276
277/// Handles the  action.
278pub fn handle_hmc(action: &crate::cli::HmcAction) -> Result<(), Box<dyn std::error::Error>> {
279    use qualia_core_db::bundle::BundleMmap;
280    match action {
281        crate::cli::HmcAction::Inspect { path } => {
282            let mmap = BundleMmap::open(path)?;
283            let reader = mmap.reader()?;
284            println!("HMC  {}", path.display());
285            println!("  entries {}", reader.entries().len());
286            println!("  flags   0x{:04x}", reader.flags());
287            for e in reader.entries() {
288                println!(
289                    "  {:<32} offset={} len={} kind={}",
290                    e.key, e.offset, e.length, e.kind
291                );
292            }
293        }
294        crate::cli::HmcAction::Verify {
295            path,
296            q42_entry,
297            rdf_source,
298        } => {
299            let mmap = BundleMmap::open(path)?;
300            let reader = mmap.reader()?;
301            println!("HMC verify {}", path.display());
302            println!("  CRC-32C + parse: OK");
303            let mut failed = false;
304            for e in reader.entries() {
305                let ok = reader.verify_entry(&e.key);
306                println!("  SHA-256 {} {}", e.key, if ok { "OK" } else { "FAIL" });
307                if !ok {
308                    failed = true;
309                }
310            }
311            if failed {
312                return Err("HMC entry SHA-256 mismatch".into());
313            }
314            if let Some(key) = q42_entry {
315                let Some(bytes) = reader.get(key) else {
316                    return Err(format!("missing HMC entry {key}").into());
317                };
318                let tmp_path = std::env::temp_dir()
319                    .join(format!("qualia-hmc-embed-{}.q42", std::process::id()));
320                std::fs::write(&tmp_path, bytes)?;
321                let result = (|| -> Result<(), Box<dyn std::error::Error>> {
322                    if let Some(rdf) = rdf_source {
323                        let report = qualia_core_db::graph_proof::prove_import_rdf_q42_equivalence(
324                            rdf,
325                            &tmp_path,
326                            qualia_core_db::graph_proof::ImportProofOptions::default(),
327                        )?;
328                        println!(
329                            "  embedded q42 vs {}: sets_match={} iso={:?}",
330                            rdf.display(),
331                            report.encoded_sets_match(),
332                            report.rdf_isomorphism
333                        );
334                        if !report.encoded_sets_match() {
335                            return Err("embedded q42 graph proof failed".into());
336                        }
337                    } else {
338                        let inspect =
339                            qualia_core_db::q42_volume::Q42InspectReport::from_path(&tmp_path)?;
340                        print!("{}", inspect.to_text());
341                    }
342                    Ok(())
343                })();
344                let _ = std::fs::remove_file(&tmp_path);
345                result?;
346            }
347        }
348    }
349    Ok(())
350}
351
352/// Handles the  action.
353pub fn handle_inspect(file_path: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
354    if qualia_core_db::q42_volume::is_unified_volume(file_path)? {
355        let report = qualia_core_db::q42_volume::Q42InspectReport::from_path(file_path)?;
356        print!("{}", report.to_text());
357        return Ok(());
358    }
359    println!("Initializing Block Inspector for: {:?}", file_path);
360
361    let mut file = std::fs::File::open(file_path)?;
362    let mut buffer = Vec::new();
363    file.read_to_end(&mut buffer)?;
364
365    if buffer.len() % 48 != 0 {
366        eprintln!("WARNING: File size {} is not a multiple of 48 bytes (NQuin alignment). File may be corrupted.", buffer.len());
367    }
368
369    let quin_size = std::mem::size_of::<NQuin>();
370    let mut count = 0;
371
372    for chunk in buffer.chunks_exact(quin_size) {
373        let quin: NQuin = unsafe { std::ptr::read_unaligned(chunk.as_ptr() as *const NQuin) };
374        let lamport_clock = quin.extract_lamport_clock();
375        let geometric_payload = quin.extract_clean_metadata_value();
376
377        println!(
378            "[Quin {}] S: {}, P: {}, O: {}, Ctx: {}, LamportClock: {}, GeoPayload: {}, Parity: {}",
379            count,
380            quin.subject,
381            quin.predicate,
382            quin.object,
383            quin.context,
384            lamport_clock,
385            geometric_payload,
386            quin.parity
387        );
388        count += 1;
389    }
390
391    println!("Successfully inspected {} Quins.", count);
392    Ok(())
393}
394
395/// Handles the  action.
396pub fn handle_dump(out_path: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
397    println!("Dumping raw SuperBlock to: {:?}", out_path);
398
399    let mut file = OpenOptions::new()
400        .create(true)
401        .write(true)
402        .truncate(true)
403        .open(out_path)?;
404
405    let mut q1 = NQuin {
406        subject: 100,
407        predicate: 200,
408        object: 300,
409        context: 50,
410        metadata: 0,
411        parity: 0,
412    };
413    q1.set_lamport_clock(1);
414    let mut q2 = NQuin {
415        subject: 101,
416        predicate: 201,
417        object: 301,
418        context: 51,
419        metadata: 555,
420        parity: 0,
421    };
422    q2.set_lamport_clock(2);
423    let mut q3 = NQuin {
424        subject: 102,
425        predicate: 202,
426        object: 302,
427        context: 52,
428        metadata: 999,
429        parity: 0,
430    };
431    q3.set_lamport_clock(3);
432
433    let quins = [q1, q2, q3];
434
435    for quin in quins.iter() {
436        let bytes = unsafe {
437            std::slice::from_raw_parts(
438                (quin as *const NQuin) as *const u8,
439                std::mem::size_of::<NQuin>(),
440            )
441        };
442        file.write_all(bytes)?;
443    }
444
445    file.sync_all()?;
446    println!("Dumped 3 mocked Quins (144 bytes) to .q42 successfully.");
447    Ok(())
448}
449
450/// Handles the  action.
451pub fn handle_export_solid(input: &PathBuf, output: &PathBuf) {
452    println!("============================================================");
453    println!("🌐 W3C Solid Exporter Bridge");
454    println!("============================================================");
455
456    let in_path = input.to_string_lossy().to_string();
457    let out_path = output.to_string_lossy().to_string();
458
459    match qualia_core_db::solid_ldp::SolidExporter::export_to_solid_pod(&in_path, &out_path) {
460        Ok(_) => {
461            println!("Leave/migrate complete: RDF bundle written for Solid LDP import.");
462        }
463        Err(e) => {
464            eprintln!("❌ Export Failed: {}", e);
465        }
466    }
467}
468
469/// Handles the  action.
470pub fn handle_verify_integrity(input: &PathBuf, dataset: &PathBuf) {
471    println!("============================================================");
472    println!("🔒 QualiaDB Zero-Allocation Integrity Verification");
473    println!("  Input   : {}", input.display());
474    println!("  Dataset : {}", dataset.display());
475    println!("============================================================");
476
477    match qualia_core_db::ingest::verify_integrity(input.clone(), dataset.clone()) {
478        Ok(true) => {
479            println!("Warning: this legacy XOR result is diagnostic only, not a proof of exact graph equality.");
480            println!("\n✅ Integrity Check Passed: 100% Exact Match!");
481            println!("XOR folds and record counts match; this is not a losslessness or graph-equality proof. Use `verify-graph` for the bounded encoded-set proof.");
482        }
483        Ok(false) => {
484            eprintln!("\n❌ Integrity Check Failed: Checksums mismatch!");
485            std::process::exit(1);
486        }
487        Err(e) => {
488            eprintln!("\n❌ Integrity Verification Error: {}", e);
489            std::process::exit(1);
490        }
491    }
492}
493
494/// Run the exact, bounded-memory encoded graph proof.
495pub fn handle_verify_graph(
496    input: &PathBuf,
497    dataset: &PathBuf,
498    memory_mib: u64,
499    temp_gib: u64,
500    encoder: &str,
501    omit_predicates: Option<&str>,
502    omit_preset: Option<&str>,
503    require_isomorphism: bool,
504) -> Result<(), Box<dyn std::error::Error>> {
505    let memory_limit_bytes = memory_mib
506        .checked_mul(1024 * 1024)
507        .and_then(|bytes| usize::try_from(bytes).ok())
508        .ok_or_else(|| std::io::Error::other("--memory-mib is too large for this platform"))?;
509    let temporary_byte_budget = temp_gib
510        .checked_mul(1024 * 1024 * 1024)
511        .ok_or_else(|| std::io::Error::other("--temp-gib is too large"))?;
512
513    let encoder_l = encoder.trim().to_ascii_lowercase();
514    let iris: Vec<String> = omit_predicates
515        .unwrap_or("")
516        .split(',')
517        .map(|s| s.trim().to_string())
518        .filter(|s| !s.is_empty())
519        .collect();
520    let omit =
521        qualia_core_db::query::integrity_omit::resolve_omit_predicate_hashes(&iris, omit_preset)?;
522
523    println!("============================================================");
524    println!("QualiaDB bounded encoded-graph proof");
525    println!("  Input       : {}", input.display());
526    println!("  Q42         : {}", dataset.display());
527    println!("  Encoder     : {encoder_l}");
528    println!("  RAM budget  : {memory_mib} MiB");
529    println!("  Temp budget : {temp_gib} GiB");
530    if !omit.is_empty() {
531        println!("  Omit preds  : {} hash(es)", omit.len());
532    }
533    println!("============================================================");
534
535    let report = match encoder_l.as_str() {
536        "legacy-semantic" | "legacy" | "semantic" => {
537            if !omit.is_empty() {
538                return Err(
539                    "--omit-predicates requires --encoder=import (legacy path has no omit filter)"
540                        .into(),
541                );
542            }
543            qualia_core_db::graph_proof::prove_cli_ntriples_q42_equivalence(
544                input,
545                dataset,
546                qualia_core_db::graph_proof::GraphProofOptions {
547                    memory_limit_bytes,
548                    temporary_byte_budget,
549                },
550            )?
551        }
552        "import" | "complete" | "" => {
553            let mut opts = qualia_core_db::graph_proof::ImportProofOptions {
554                memory_limit_bytes,
555                temporary_byte_budget,
556                omit_predicate_hashes: omit,
557                encoder_profile: qualia_core_db::graph_proof::EncoderProfile::ImportComplete,
558            };
559            if omit_preset.is_some() || !iris.is_empty() {
560                // profile label stays ImportComplete; omit is reflected in skipped_or_filtered
561                let _ = &mut opts;
562            }
563            qualia_core_db::graph_proof::prove_import_rdf_q42_equivalence(input, dataset, opts)?
564        }
565        other => {
566            return Err(
567                format!("unknown --encoder '{other}' (use import | legacy-semantic)").into(),
568            );
569        }
570    };
571
572    println!("Encoder profile     : {:?}", report.encoder_profile);
573    println!("Source records      : {}", report.source_records);
574    println!("Source triple count : {}", report.source_triple_count);
575    println!("Skipped/filtered    : {}", report.skipped_or_filtered);
576    println!("Q42 records         : {}", report.q42_records);
577    println!("Unique source quads : {}", report.source_unique_records);
578    println!("Unique Q42 quads    : {}", report.q42_unique_records);
579    println!("Missing from Q42    : {}", report.missing_from_q42);
580    println!("Unexpected in Q42   : {}", report.unexpected_in_q42);
581    println!("Skipped source lines : {}", report.source_skipped_lines);
582    println!("Oversize terms      : {}", report.oversize_literal_count);
583    println!(
584        "Lexicon             : {} bytes, {:?} entries{}",
585        report.lexicon_length_bytes,
586        report.lexicon_entry_count,
587        if report.lexicon_has_no_terms {
588            " [empty/no terms]"
589        } else {
590            ""
591        }
592    );
593    if let Some(preview) = &report.first_oversize_preview {
594        println!("First oversize      : {preview}");
595    }
596
597    if !report.encoded_sets_match() {
598        if let Some(record) = report.first_missing {
599            println!("First missing encoded quad    : {record:016X?}");
600        }
601        if let Some(record) = report.first_unexpected {
602            println!("First unexpected encoded quad : {record:016X?}");
603        }
604        return Err(std::io::Error::other("encoded graph sets differ").into());
605    }
606
607    match report.rdf_isomorphism {
608        qualia_core_db::graph_proof::RdfIsomorphismStatus::GroundGraphProven => {
609            println!("PASS: exact ground-graph equivalence is proven in the Q42 encoding.");
610            Ok(())
611        }
612        qualia_core_db::graph_proof::RdfIsomorphismStatus::BlankNodeCanonicalizationRequired => {
613            // Encode-fidelity (label-faithful hashed-set match) already succeeded above.
614            // RDF isomorphism under blank relabeling is a stronger claim (RDFC-1.0 / Skolem).
615            println!("PASS: label-faithful encoded-set equivalence (import encoder).");
616            eprintln!(
617                "NOTICE: source contains blank nodes (often Turtle [] expanded by the parser to _: ids). \
618Structural RDF isomorphism / RDFC-1.0 is NOT proven — only that this serialization's labels hashed identically into Q42. \
619Use --require-isomorphism to fail closed until a canonicalization path exists."
620            );
621            if require_isomorphism {
622                return Err(std::io::Error::other(
623                    "encoded sets match only under blank-node label identity; --require-isomorphism demands GroundGraphProven or RDFC (not yet implemented)",
624                )
625                .into());
626            }
627            Ok(())
628        }
629        qualia_core_db::graph_proof::RdfIsomorphismStatus::Different => {
630            Err(std::io::Error::other("encoded graph sets differ").into())
631        }
632    }
633}
634
635/// Handles the  action.
636pub fn handle_import(
637    input: Option<&PathBuf>,
638    output: Option<&PathBuf>,
639    strip_literals: bool,
640    segment_mib: Option<u64>,
641    progress: crate::cli::ImportProgressFormat,
642    verbose: u8,
643    url: Option<&str>,
644    job_dir: Option<&PathBuf>,
645    resume: bool,
646    omit_predicates: Option<&str>,
647    omit_preset: Option<&str>,
648) {
649    use crate::cli::ImportProgressFormat;
650    use qualia_core_db::ingest::{
651        streaming_import_rdf_with_job, streaming_import_rdf_with_report,
652        streaming_import_rdf_with_report_filtered, IngestMode, IngestReport,
653    };
654    use qualia_core_db::ingest_job::{infer_rdf_format, IngestJob, IngestSourceKind};
655
656    let Some(output) = output else {
657        eprintln!("import requires an OUTPUT .q42 path");
658        return;
659    };
660
661    let iris: Vec<String> = omit_predicates
662        .unwrap_or("")
663        .split(',')
664        .map(|s| s.trim().to_string())
665        .filter(|s| !s.is_empty())
666        .collect();
667    let omit = match qualia_core_db::query::integrity_omit::resolve_omit_predicate_hashes(
668        &iris,
669        omit_preset,
670    ) {
671        Ok(v) => v,
672        Err(e) => {
673            eprintln!("{e}");
674            return;
675        }
676    };
677
678    if progress != ImportProgressFormat::Json {
679        eprintln!("============================================================");
680        eprintln!("QualiaDB RDF → Q42 ingest");
681        eprintln!("============================================================");
682        eprintln!("Use -v / -vv for process stats. --progress json emits NDJSON for UIs.");
683        eprintln!("Stream URL with --url (gzip/zstd in-flight). Durable jobs: --job-dir.");
684    }
685
686    let report = match progress {
687        ImportProgressFormat::Text => IngestReport::text_stderr(verbose),
688        ImportProgressFormat::Json => IngestReport::json_stdout(verbose),
689        ImportProgressFormat::None => IngestReport::silent(),
690    };
691
692    if resume {
693        let Some(dir) = job_dir else {
694            eprintln!("--resume requires --job-dir");
695            return;
696        };
697        match streaming_import_rdf_with_job(dir, report) {
698            Ok(n) => eprintln!("Continue finished. Super-Quins written: {n}"),
699            Err(e) => eprintln!("Continue failed: {e}"),
700        }
701        return;
702    }
703
704    let source = if let Some(url) = url {
705        IngestSourceKind::Url {
706            url: url.to_string(),
707        }
708    } else if let Some(path) = input {
709        IngestSourceKind::File {
710            path: path.to_string_lossy().into_owned(),
711        }
712    } else {
713        eprintln!("import needs an input path or --url");
714        return;
715    };
716
717    let mode = if strip_literals {
718        IngestMode::StripLiterals
719    } else {
720        IngestMode::Complete
721    };
722
723    if let Some(dir) = job_dir {
724        if !omit.is_empty() {
725            eprintln!("omit-predicates with --job-dir is not wired yet; use a direct file import");
726            return;
727        }
728        let locator = source.locator().to_string();
729        let encoding = qualia_core_db::ingest_job::detect_encoding(&locator, None, &[]);
730        let format = infer_rdf_format(&locator);
731        if let Err(e) = IngestJob::create(
732            dir.clone(),
733            source,
734            encoding,
735            format,
736            mode,
737            segment_mib,
738            output,
739        ) {
740            eprintln!("Could not create job dir: {e}");
741            return;
742        }
743        match streaming_import_rdf_with_job(dir, report) {
744            Ok(n) => eprintln!("Done. Super-Quins written: {n}"),
745            Err(e) => eprintln!("Import failed: {e}"),
746        }
747        return;
748    }
749
750    let IngestSourceKind::File { path } = source else {
751        eprintln!(
752            "--url requires --job-dir so the stream can be attested and resumed policy is explicit"
753        );
754        return;
755    };
756
757    let segment_bytes = match segment_mib {
758        Some(mib) => match mib.checked_mul(1024 * 1024) {
759            Some(bytes) if bytes != 0 => Some(bytes),
760            _ => {
761                eprintln!("--segment-mib must be greater than zero");
762                return;
763            }
764        },
765        None => None,
766    };
767
768    match if omit.is_empty() {
769        streaming_import_rdf_with_report(
770            &path,
771            &output.to_string_lossy(),
772            mode,
773            segment_bytes,
774            report,
775        )
776    } else {
777        streaming_import_rdf_with_report_filtered(
778            &path,
779            &output.to_string_lossy(),
780            mode,
781            segment_bytes,
782            report,
783            &omit,
784        )
785    } {
786        Ok(quin_count) => {
787            if progress != ImportProgressFormat::Json {
788                eprintln!(
789                    "Done. Wrote {quin_count} Super-Quins to {}.",
790                    output.display()
791                );
792            }
793        }
794        Err(e) => eprintln!("Import failed: {e}"),
795    }
796}
797
798/// Handles the  action.
799pub fn handle_ingest_job(action: &IngestJobAction) -> Result<(), Box<dyn std::error::Error>> {
800    use qualia_core_db::ingest::{streaming_import_rdf_with_job, IngestMode, IngestReport};
801    use qualia_core_db::ingest_job::{
802        adopt_legacy_scratch, append_rdf_to_root, compare_attestation_file_to_path, inspect_job,
803        inspect_legacy_scratch, inspect_volume_root, job_status, publish_job, IngestJob,
804        IngestSourceKind,
805    };
806
807    match action {
808        IngestJobAction::Status { dir, watch } => loop {
809            let st = job_status(dir)?;
810            println!("{}", st.summary);
811            if !*watch {
812                println!("dir: {}", st.dir);
813                println!(
814                    "checkpoint: {:?} accepted {}",
815                    st.phase, st.accepted_triples
816                );
817                println!(
818                    "chunks: {}  lex: {}  runs: {}",
819                    st.quin_chunks, st.lex_runs, st.run_bytes
820                );
821                println!("source: {}", st.source);
822                println!(
823                    "output: {} ({})",
824                    st.output,
825                    if st.output_exists { "present" } else { "none" }
826                );
827                println!("resumable: {}  complete: {}", st.resumable, st.complete);
828                if let Some(live) = &st.live {
829                    println!("live: {}", live.format_text(1));
830                } else {
831                    println!("live: no progress.json (this continue predates skip ticks)");
832                }
833                if let Some(att) = IngestJob::open(dir.clone())?.load_attestation()? {
834                    println!("attestation: {}", att.verify_story());
835                }
836                break;
837            }
838            if st.complete {
839                println!("complete");
840                break;
841            }
842            std::thread::sleep(std::time::Duration::from_secs(2));
843        },
844        IngestJobAction::Inspect { path } => {
845            let report = if path.join("job.json").is_file() {
846                inspect_job(path)?
847            } else if path.extension().and_then(|e| e.to_str()) == Some("q42") {
848                inspect_volume_root(path)?
849            } else {
850                inspect_legacy_scratch(path)?
851            };
852            println!("{}", serde_json::to_string_pretty(&report)?);
853        }
854        IngestJobAction::Compare {
855            attestation,
856            against,
857        } => {
858            let kind = if against.contains("://") {
859                IngestSourceKind::Url {
860                    url: against.clone(),
861                }
862            } else {
863                IngestSourceKind::File {
864                    path: against.clone(),
865                }
866            };
867            let report = compare_attestation_file_to_path(attestation, &kind)?;
868            println!("{}", serde_json::to_string_pretty(&report)?);
869        }
870        IngestJobAction::Continue { dir, detach } => {
871            if *detach {
872                let pid = spawn_detached_continue(dir)?;
873                println!(
874                    "detached continue pid {pid}; log {}",
875                    dir.join("continue.log").display()
876                );
877                return Ok(());
878            }
879            let n = streaming_import_rdf_with_job(dir, IngestReport::text_stderr(1))?;
880            println!("continue wrote {n} Super-Quins");
881        }
882        IngestJobAction::Publish { dir, detach } => {
883            if *detach {
884                let pid = spawn_detached_publish(dir)?;
885                println!(
886                    "detached publish pid {pid}; log {}",
887                    dir.join("publish.log").display()
888                );
889                return Ok(());
890            }
891            let n = publish_job(dir, IngestReport::text_stderr(1))?;
892            println!("publish wrote {n} Super-Quins");
893        }
894        IngestJobAction::AdoptScratch {
895            scratch,
896            out_job,
897            source,
898            output,
899            segment_mib,
900        } => {
901            let job = adopt_legacy_scratch(
902                scratch,
903                out_job.clone(),
904                source,
905                output,
906                IngestMode::Complete,
907                *segment_mib,
908            )?;
909            println!(
910                "adopted into {} ({} triples, {} chunks). Then: qualia-cli ingest-job continue {}",
911                job.dir.display(),
912                job.checkpoint.triples,
913                job.checkpoint.quin_chunks,
914                job.dir.display()
915            );
916        }
917        IngestJobAction::Append { root, extra, url } => {
918            let kind = if let Some(u) = url {
919                IngestSourceKind::Url { url: u.clone() }
920            } else if let Some(p) = extra {
921                IngestSourceKind::File {
922                    path: p.to_string_lossy().into_owned(),
923                }
924            } else {
925                return Err("append needs extra path or --url".into());
926            };
927            let n = append_rdf_to_root(root, &kind, IngestReport::text_stderr(1))?;
928            println!("appended {n} Super-Quins onto {}", root.display());
929        }
930    }
931    Ok(())
932}
933
934/// Handles the  action.
935pub fn handle_ingest(format: &IngestFormat) {
936    match format {
937        IngestFormat::Semantic {
938            file,
939            access_policy,
940            mapping_version,
941            context_digest,
942            compiler_build_digest,
943        } => {
944            let out_path = file.with_extension("q42");
945            let policy = match qualia_core_db::q42_volume::IngestAccessPolicy::parse(access_policy)
946            {
947                Ok(p) => p,
948                Err(e) => {
949                    eprintln!("Ingest error: {e}");
950                    return;
951                }
952            };
953            println!("Detecting format for: {}", file.display());
954            match crate::ingest::ingest_auto_with_policy(
955                file,
956                &out_path,
957                policy,
958                mapping_version.as_deref(),
959                context_digest.as_deref(),
960                compiler_build_digest.as_deref(),
961            ) {
962                Ok((stats, fmt)) => {
963                    println!("Format : {}", fmt.label());
964                    println!("Policy : {}", policy.as_str());
965                    println!("Triples: {}", stats.triples_ingested);
966                    println!("Lex    : {}", stats.lex_entries);
967                    println!("Blocks : {}", stats.blocks_written);
968                    println!("Output : {}", out_path.display());
969                    println!(
970                        "Provenance: {}",
971                        qualia_core_db::q42_volume::provenance_path_for(&out_path).display()
972                    );
973                    println!("Done.");
974                }
975                Err(e) => eprintln!("Ingest error: {e}"),
976            }
977        }
978        IngestFormat::Csv { file, map } => {
979            println!("CSV ingest for {:?} using map {:?}", file, map);
980            match crate::ingest::mapper::compile_shacl_mapping(map) {
981                Ok(mut profile) => {
982                    let path_str = file.to_string_lossy();
983                    let out_path = file.with_extension("q42").to_string_lossy().into_owned();
984                    crate::ingest::csv_mapper::stream_csv_to_quins(
985                        &path_str,
986                        &out_path,
987                        &mut profile,
988                    );
989                    println!("✅ CSV Ingest Complete");
990                }
991                Err(e) => eprintln!("❌ Failed to compile SHACL mapping: {}", e),
992            }
993        }
994        IngestFormat::Json { file, map } => {
995            println!("JSON ingest for {:?} using map {:?}", file, map);
996            match crate::ingest::mapper::compile_shacl_mapping(map) {
997                Ok(profile) => {
998                    let path_str = file.to_string_lossy();
999                    let out_path = file.with_extension("q42").to_string_lossy().into_owned();
1000                    crate::ingest::json_mapper::stream_json_to_quins(
1001                        &path_str, &out_path, &profile,
1002                    );
1003                    println!("✅ JSON Ingest Complete");
1004                }
1005                Err(e) => eprintln!("❌ Failed to compile SHACL mapping: {}", e),
1006            }
1007        }
1008    }
1009}
1010
1011fn spawn_detached_continue(dir: &std::path::Path) -> Result<u32, Box<dyn std::error::Error>> {
1012    let exe = std::env::current_exe()?;
1013    let log_path = dir.join("continue.log");
1014    let log = std::fs::File::create(&log_path)?;
1015    let err = log.try_clone()?;
1016    let mut cmd = std::process::Command::new(exe);
1017    cmd.arg("ingest-job")
1018        .arg("continue")
1019        .arg(dir)
1020        .stdin(std::process::Stdio::null())
1021        .stdout(std::process::Stdio::from(log))
1022        .stderr(std::process::Stdio::from(err));
1023    #[cfg(windows)]
1024    {
1025        use std::os::windows::process::CommandExt;
1026        const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x0100_0000;
1027        const DETACHED_PROCESS: u32 = 0x0000_0008;
1028        const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
1029        cmd.creation_flags(CREATE_BREAKAWAY_FROM_JOB | DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP);
1030    }
1031    let child = cmd.spawn()?;
1032    Ok(child.id())
1033}
1034
1035fn spawn_detached_publish(dir: &std::path::Path) -> Result<u32, Box<dyn std::error::Error>> {
1036    let exe = std::env::current_exe()?;
1037    let log_path = dir.join("publish.log");
1038    let log = std::fs::File::create(&log_path)?;
1039    let err = log.try_clone()?;
1040    let mut cmd = std::process::Command::new(exe);
1041    cmd.arg("ingest-job")
1042        .arg("publish")
1043        .arg(dir)
1044        .stdin(std::process::Stdio::null())
1045        .stdout(std::process::Stdio::from(log))
1046        .stderr(std::process::Stdio::from(err));
1047    #[cfg(windows)]
1048    {
1049        use std::os::windows::process::CommandExt;
1050        const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x0100_0000;
1051        const DETACHED_PROCESS: u32 = 0x0000_0008;
1052        const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
1053        cmd.creation_flags(CREATE_BREAKAWAY_FROM_JOB | DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP);
1054    }
1055    let child = cmd.spawn()?;
1056    Ok(child.id())
1057}
1058
1059/// Handles the  action.
1060pub fn handle_query(dialect: &QueryDialect) {
1061    match dialect {
1062        QueryDialect::Sparql {
1063            vault,
1064            query_string,
1065            file,
1066        } => {
1067            let qs = if let Some(q) = query_string {
1068                q.clone()
1069            } else if let Some(f) = file {
1070                std::fs::read_to_string(f).expect("Failed to read SPARQL file")
1071            } else {
1072                panic!("Must provide either a query_string or a file");
1073            };
1074            run_sparql_query(&vault, &qs);
1075        }
1076        QueryDialect::SparqlStar {
1077            vault,
1078            query_string,
1079            file,
1080        } => {
1081            let qs = if let Some(q) = query_string {
1082                q.clone()
1083            } else if let Some(f) = file {
1084                std::fs::read_to_string(f).expect("Failed to read SPARQL-Star file")
1085            } else {
1086                panic!("Must provide either a query_string or a file");
1087            };
1088            run_sparql_query(&vault, &qs);
1089        }
1090    }
1091}
1092
1093/// Handles the  action.
1094pub fn handle_compress(input: &PathBuf, output: &PathBuf) {
1095    let ext = input
1096        .extension()
1097        .and_then(|e| e.to_str())
1098        .unwrap_or("")
1099        .to_lowercase();
1100    let is_q42 = ext == "q42";
1101
1102    println!("============================================================");
1103    println!("QualiaDB LZ4 Block-Stream Compressor");
1104    println!("  input  : {}", input.display());
1105    println!("  output : {}", output.display());
1106    println!(
1107        "  mode   : {}",
1108        if is_q42 {
1109            "SuperBlock → raw Quins"
1110        } else {
1111            "raw bytes"
1112        }
1113    );
1114    println!("============================================================");
1115
1116    let result = if is_q42 {
1117        crate::compress::compress_q42(input, output)
1118    } else {
1119        crate::compress::compress_raw(input, output)
1120    };
1121
1122    match result {
1123        Ok(stats) => {
1124            println!("Done.");
1125            println!(
1126                "  Input  : {:.1} MB",
1127                stats.input_bytes as f64 / 1_048_576.0
1128            );
1129            println!(
1130                "  Output : {:.1} MB",
1131                stats.output_bytes as f64 / 1_048_576.0
1132            );
1133            println!("  Blocks : {}", stats.blocks);
1134            println!("  Ratio  : {:.2}x", stats.ratio);
1135        }
1136        Err(e) => eprintln!("Compression failed: {}", e),
1137    }
1138}
1139
1140/// Handles the  action.
1141pub fn handle_profile(action: &ProfileAction) {
1142    match action {
1143        ProfileAction::Compile { input, out } => {
1144            let out_path = out.clone().unwrap_or_else(|| input.with_extension("qchk"));
1145            println!("============================================================");
1146            println!("⚡ Qualia Capability Profile Compiler");
1147            println!("  input  : {}", input.display());
1148            println!("  output : {}", out_path.display());
1149            println!("============================================================");
1150            match std::fs::read_to_string(input) {
1151                Err(e) => eprintln!("❌ Failed to read profile source: {}", e),
1152                Ok(jsonld_src) => {
1153                    let stem = input.file_stem().unwrap_or_default().to_string_lossy();
1154                    let profile_id = qualia_core_db::q_hash(&format!("profile:{}", stem));
1155                    let mut chk_bytes: Vec<u8> = Vec::new();
1156                    chk_bytes.extend_from_slice(b"QCHK");
1157                    chk_bytes.extend_from_slice(&profile_id.to_le_bytes());
1158                    chk_bytes.extend_from_slice(&(jsonld_src.len() as u32).to_le_bytes());
1159                    chk_bytes.extend_from_slice(jsonld_src.as_bytes());
1160                    match std::fs::write(&out_path, &chk_bytes) {
1161                        Ok(_) => {
1162                            println!(
1163                                "✅ Compiled profile 0x{:016X} ({} bytes)",
1164                                profile_id,
1165                                chk_bytes.len()
1166                            );
1167                            println!("   Stem  : {}", stem);
1168                            println!("   Output: {}", out_path.display());
1169                            println!("   Next  : qualia-cli ingest --input data.nt --output out --profile {}", out_path.display());
1170                        }
1171                        Err(e) => eprintln!("❌ Write failed: {}", e),
1172                    }
1173                }
1174            }
1175        }
1176        ProfileAction::List => {
1177            println!("============================================================");
1178            println!("📋 Registered Capability Profiles");
1179            println!("============================================================");
1180            println!("  (Profiles are registered when ingested via ExternalSorter)");
1181            println!("  Known profile ID namespaces:");
1182            let known = [
1183                (
1184                    "profile:general",
1185                    "General purpose — no engine restrictions",
1186                ),
1187                (
1188                    "profile:health",
1189                    "Health/Clinical — NativeClinicalRisk, NativeBioAlignment",
1190                ),
1191                (
1192                    "profile:chemistry",
1193                    "Organic Chemistry — NativeChemicalSynthesis, NativeLipinski",
1194                ),
1195                (
1196                    "profile:research",
1197                    "Research — all scientific opcodes, no financial engines",
1198                ),
1199                (
1200                    "profile:legal",
1201                    "Legal/Deontic — OP_OBLIGATE, OP_FORBID, OP_PERMIT",
1202                ),
1203                (
1204                    "profile:financial",
1205                    "Financial — ILP dispatchers, tax schema, audit trail",
1206                ),
1207            ];
1208            for (name, desc) in &known {
1209                println!(
1210                    "  0x{:016X}  {}  — {}",
1211                    qualia_core_db::q_hash(name),
1212                    name,
1213                    desc
1214                );
1215            }
1216        }
1217        ProfileAction::Inspect { file } => {
1218            println!("============================================================");
1219            println!("🔎 Profile Inspector: {}", file.display());
1220            println!("============================================================");
1221            match std::fs::read(file) {
1222                Err(e) => eprintln!("❌ Cannot read file: {}", e),
1223                Ok(bytes) => {
1224                    if bytes.len() < 16 || &bytes[0..4] != b"QCHK" {
1225                        eprintln!(
1226                            "❌ Not a valid QCHK profile (.qchk or legacy .chk missing QCHK magic)"
1227                        );
1228                    } else {
1229                        let profile_id = u64::from_le_bytes(bytes[4..12].try_into().unwrap());
1230                        let payload_len =
1231                            u32::from_le_bytes(bytes[12..16].try_into().unwrap()) as usize;
1232                        let payload =
1233                            &bytes[16..16 + payload_len.min(bytes.len().saturating_sub(16))];
1234                        println!("  Profile ID : 0x{:016X}", profile_id);
1235                        println!("  Payload    : {} bytes (JSON-LD source)", payload_len);
1236                        println!("  Total file : {} bytes", bytes.len());
1237                        println!();
1238                        println!("--- JSON-LD Source ---");
1239                        println!("{}", String::from_utf8_lossy(payload));
1240                    }
1241                }
1242            }
1243        }
1244    }
1245}
1246
1247/// Handles the  action.
1248pub fn handle_capabilities(list: bool) {
1249    if list {
1250        println!("============================================================");
1251        println!("🧠 QualiaDB Runtime Capability Registry");
1252        println!("============================================================");
1253        for capability in qualia_core_db::CAPABILITY_DESCRIPTORS {
1254            println!(
1255                "  - {} [{}] -> {}",
1256                capability.name,
1257                capability.domain,
1258                capability.mcp_tools.join(", ")
1259            );
1260        }
1261        println!("============================================================");
1262    } else {
1263        println!("Use `qualia-cli capabilities --list` to view capabilities.");
1264    }
1265}