use super::{InternalFn, get_clm_args};
pub(super) fn dispatch(module_path: &str) -> Option<InternalFn> {
dispatch_network_discovery(module_path)
.or_else(|| dispatch_testing_verification(module_path))
}
fn dispatch_network_discovery(module_path: &str) -> Option<InternalFn> {
match module_path {
"clm_rust_core::discovery_engine::mdns_discovery" => Some(|_clm_root, clm_file| {
let args = get_clm_args(clm_file)?;
let mut mode = "both";
let mut port = None;
let mut name = None;
let mut did = None;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--mode" => { if i+1 < args.len() { mode = &args[i+1]; i += 2; } else { i += 1; } }
"--port" => { if i+1 < args.len() { port = args[i+1].parse().ok(); i += 2; } else { i += 1; } }
"--name" => { if i+1 < args.len() { name = Some(args[i+1].clone()); i += 2; } else { i += 1; } }
"--did" => { if i+1 < args.len() { did = Some(args[i+1].clone()); i += 2; } else { i += 1; } }
_ => i += 1,
}
}
let resolved_port = port.unwrap_or(9095);
let resolved_name = name.unwrap_or_else(|| "pkc-localnode".to_string());
let resolved_did = did.unwrap_or_else(|| "did:pkc:local:unknown".to_string());
let engine = crate::discovery_engine::DiscoveryEngine::new();
if mode == "announce" {
engine.announce(&resolved_name, &resolved_did, resolved_port).map_err(|e| e.to_string())?;
Ok((0, "Announced".to_string()))
} else {
let peers = engine.discover(std::time::Duration::from_secs(5)).map_err(|e| e.to_string())?;
let out = serde_json::to_string_pretty(&serde_json::json!({ "peers": peers })).unwrap();
Ok((0, out))
}
}),
"clm_rust_core::discovery_engine::discovery_node" => Some(|clm_root, clm_file| {
let args = get_clm_args(clm_file)?;
let mut port = None;
let mut db_path = None;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--port" => { if i+1 < args.len() { port = args[i+1].parse().ok(); i += 2; } else { i += 1; } }
"--db" => { if i+1 < args.len() { db_path = Some(args[i+1].clone()); i += 2; } else { i += 1; } }
_ => i += 1,
}
}
let resolved_port = port.unwrap_or(9095);
let resolved_db = db_path.unwrap_or_else(|| {
crate::db::get_db_path("agent_identities", clm_root).to_string_lossy().into_owned()
});
crate::discovery_engine::run_discovery_node(resolved_port, &resolved_db)
.map(|_| (0, "".to_string())).map_err(|e| e.to_string())
}),
"clm_rust_core::discovery_engine::discover" => Some(|_clm_root, clm_file| {
let args = get_clm_args(clm_file)?;
let mut handshake = None;
let mut did = None;
let mut key = None;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--handshake" => { if i+1 < args.len() { handshake = Some(args[i+1].clone()); i += 2; } else { i += 1; } }
"--did" => { if i+1 < args.len() { did = Some(args[i+1].clone()); i += 2; } else { i += 1; } }
"--key" => { if i+1 < args.len() { key = Some(args[i+1].clone()); i += 2; } else { i += 1; } }
_ => i += 1,
}
}
if let Some(target) = handshake {
let resolved_did = did.unwrap_or_default();
let resolved_key = key.unwrap_or_default();
let parts: Vec<&str> = target.split(':').collect();
if parts.len() != 2 {
return Err("Invalid handshake target. Must be ip:port".to_string());
}
let ip = parts[0];
let port: u16 = parts[1].parse().unwrap_or(0);
crate::discovery_engine::perform_handshake(ip, port, &resolved_did, &resolved_key)
.map(|res| (0, serde_json::to_string_pretty(&res).unwrap())).map_err(|e| e.to_string())
} else {
Err("Discover mode needs --handshake".to_string())
}
}),
"clm_rust_core::network_gateway::chat_gateway" => Some(|_clm_root, clm_file| {
let args = get_clm_args(clm_file)?;
let mut port = None;
let mut timeout = None;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--port" => { if i+1 < args.len() { port = args[i+1].parse().ok(); i += 2; } else { i += 1; } }
"--timeout" => { if i+1 < args.len() { timeout = args[i+1].parse().ok(); i += 2; } else { i += 1; } }
_ => i += 1,
}
}
let mut gateway = crate::network_gateway::NetworkGateway::new();
if let Some(p) = port { gateway.port = p; }
if let Some(t) = timeout { gateway.timeout = t; }
let rt = tokio::runtime::Runtime::new().map_err(|e| e.to_string())?;
rt.block_on(async {
gateway.start().await.map_err(|e| e.to_string())
})?;
Ok((0, "".to_string()))
}),
_ => None,
}
}
fn dispatch_testing_verification(module_path: &str) -> Option<InternalFn> {
match module_path {
"clm_rust_core::prove::prove_tests" => Some(|clm_root, clm_file| {
let args = get_clm_args(clm_file)?;
let file = if args.is_empty() { clm_file } else { &args[0] };
crate::prove::prove_tests(file, clm_root)
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_past_execution" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_past_execution()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_init_tridb" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_init_tridb()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_evaluator_functions" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_evaluator_functions()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_type_system" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_type_system()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_monadic_pcards" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_monadic_pcards()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_symbol_extraction" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_symbol_extraction()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_symbol_adapter_integration" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_symbol_adapter_integration()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_handle_indirection" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_handle_indirection()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_hash_migration" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_hash_migration()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_baldwin_operators" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_baldwin_operators()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_mcard_kernel" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_mcard_kernel()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_monadic_pcard_check_precondition" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_monadic_pcard_check_precondition()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_monadic_pcard_resolve_handle" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_monadic_pcard_resolve_handle()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_monadic_pcard_execute_payload" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_monadic_pcard_execute_payload()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_monadic_pcard_validate_postcondition" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_monadic_pcard_validate_postcondition()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_pcard_monad_composition" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_pcard_monad_composition()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_function_failures" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_function_failures()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_vcard_pre" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_vcard_pre()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_agent_containment" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_agent_containment()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_ephemeral_sessions" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_ephemeral_sessions()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint79_identity_hardening" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint79_identity_hardening()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_browser_portability" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_browser_portability()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_memdb_dispatch" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_memdb_dispatch()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint58_validation_chains" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint58_validation_chains()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint77_boundary_guards" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint77_boundary_guards()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint143_layer4_boundary" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint143_layer4_boundary()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint143_type_levels" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint143_type_levels()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint78_zero_fs_hermetic" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint78_zero_fs_hermetic()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint82_future_hardening" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint82_future_hardening()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint82_multilanguage" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint82_multilanguage()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sql_parameterized" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sql_parameterized()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_compose_nets_flattening" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_compose_nets_flattening()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_petri_invariants" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_petri_invariants()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_native_petri_vm_parity" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_native_petri_vm_parity()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_kernel_bootstrap_net" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_kernel_bootstrap_net()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_petri_math" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_petri_math()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint43_kenotic_petri" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint43_kenotic_petri()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_compiled_adapters" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_compiled_adapters()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint59_sandbox_isolation" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint59_sandbox_isolation()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_llm_codegen" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_llm_codegen()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_compose_nets_liveness" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_compose_nets_liveness()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_react_agent" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_react_agent()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint45_repl_showcase" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint45_repl_showcase()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint32_polyglot" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint32_polyglot()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_multi_language_benchmark" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_multi_language_benchmark()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_declarative_libraries" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_declarative_libraries()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_llm_adapter" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_llm_adapter()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_compose_functions" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_compose_functions()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_compose_conditions" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_compose_conditions()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_petri_net_suite" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_petri_net_suite()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_token_duality" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_token_duality()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_petri_binding_suite" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_petri_binding_suite()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint44_sovereign_accountability" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint44_sovereign_accountability()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_equivalence_assessment" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_equivalence_assessment()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_finetuning_sequence" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_finetuning_sequence()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_content_interpreter" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_content_interpreter()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_decomposition" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_decomposition()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_file_to_mcard" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_file_to_mcard()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint40_storage_equivalence" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint40_storage_equivalence()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_content_resolution_ioc" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_content_resolution_ioc()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_dependency_graph" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_dependency_graph()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint38_directory_equivalence" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint38_directory_equivalence()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_modelfile_sequence" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_modelfile_sequence()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_past_execution_layer2" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_past_execution_layer2()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_namespace_sync" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_namespace_sync()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_chat_interface" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_chat_interface()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_route_handle" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_route_handle()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_namespace_mapping" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_namespace_mapping()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_mdns_peer_discovery" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_mdns_peer_discovery()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_net_transport" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_net_transport()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_sprint42_decentralized_discovery" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_sprint42_decentralized_discovery()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_memorize_mcard" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_memorize_mcard()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_polyglot_ingest" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_polyglot_ingest()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_modular_adapters" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_modular_adapters()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_internal_dispatch_zerofs" => Some(|_clm_root, _clm_file| {
crate::integration::run_integration_internal_dispatch_zerofs()
.map(|out| (0, out))
}),
"clm_rust_core::integration::test_polyglot_sla_sandboxing" => Some(|clm_root, clm_file| {
crate::integration::run_integration_polyglot_sla_sandboxing(clm_root, clm_file)
.map(|out| (0, out))
}),
"clm_rust_core::test::math_op" => Some(|clm_root, _clm_file| {
let payload_str = std::env::var("CLM_PAYLOAD").unwrap_or_else(|_| "0".to_string());
let val = if payload_str.starts_with('{') {
let v: serde_json::Value = serde_json::from_str(&payload_str).unwrap_or(serde_json::json!({}));
v.get("val").and_then(|x| x.as_i64()).unwrap_or(0)
} else if payload_str.contains(':') {
let v: serde_json::Value = serde_yaml::from_str(&payload_str).unwrap_or(serde_json::json!({}));
v.get("val").and_then(|x| x.as_i64()).unwrap_or(0)
} else {
payload_str.trim().parse::<i64>().unwrap_or(0)
};
let result = val + 2;
let now = crate::utils::now_iso8601();
let writer_arc = crate::db::get_writer_connection("content_memory", clm_root)
.map_err(|e| format!("DB Error: {}", e))?;
let conn = writer_arc.lock().map_err(|e| e.to_string())?;
conn.execute(
"INSERT INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params![format!("sha256:test_math_{}", val), format!("result: {}", result), now],
).map_err(|e| format!("DB Insert failed: {}", e))?;
Ok((0, format!("result: {}", result)))
}),
"clm_rust_core::test::compiled_op" => Some(|clm_root, _clm_file| {
let payload_str = std::env::var("CLM_PAYLOAD").unwrap_or_else(|_| "0".to_string());
let val = if payload_str.starts_with('{') {
let v: serde_json::Value = serde_json::from_str(&payload_str).unwrap_or(serde_json::json!({}));
v.get("val").and_then(|x| x.as_i64()).unwrap_or(0)
} else if payload_str.contains(':') {
let v: serde_json::Value = serde_yaml::from_str(&payload_str).unwrap_or(serde_json::json!({}));
v.get("val").and_then(|x| x.as_i64()).unwrap_or(0)
} else {
payload_str.trim().parse::<i64>().unwrap_or(0)
};
let result = val + 2;
let now = crate::utils::now_iso8601();
let writer_arc = crate::db::get_writer_connection("content_memory", clm_root)
.map_err(|e| format!("DB Error: {}", e))?;
let conn = writer_arc.lock().map_err(|e| e.to_string())?;
conn.execute(
"INSERT INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params![format!("sha256:test_math_{}", val), format!("result: {}", result), now],
).map_err(|e| format!("DB Insert failed: {}", e))?;
Ok((0, format!("result: {}", result)))
}),
"clm_rust_core::test::braid_t1" => Some(|clm_root, _clm_file| {
let now = crate::utils::now_iso8601();
let writer_arc = crate::db::get_writer_connection("content_memory", clm_root)
.map_err(|e| format!("DB Error: {}", e))?;
let conn = writer_arc.lock().map_err(|e| e.to_string())?;
conn.execute(
"INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params!["sha256:mark_c", "val: 10", now],
).map_err(|e| format!("DB Insert failed: {}", e))?;
Ok((0, "val: 10".to_string()))
}),
"clm_rust_core::test::braid_t2" => Some(|clm_root, _clm_file| {
let now = crate::utils::now_iso8601();
let writer_arc = crate::db::get_writer_connection("content_memory", clm_root)
.map_err(|e| format!("DB Error: {}", e))?;
let conn = writer_arc.lock().map_err(|e| e.to_string())?;
conn.execute(
"INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params!["sha256:mark_d", "val: 20", now],
).map_err(|e| format!("DB Insert failed: {}", e))?;
Ok((0, "val: 20".to_string()))
}),
_ => None,
}
}