clm-kernel 0.0.0

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::collections::HashSet;
use std::env;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream};
use tokio::io::AsyncWriteExt;
#[cfg(unix)]
use tokio::net::UnixStream;

use tokio::sync::Mutex;
use tokio::time::{sleep, Duration};
use futures_util::{StreamExt, SinkExt};
use tokio_tungstenite::accept_async;
use tokio_tungstenite::tungstenite::Message;
use serde::Serialize;
use serde_json::Value;
use regex::Regex;

#[derive(Clone)]
pub struct NetworkGateway {
    pub host: String,
    pub port: u16,
    pub timeout: u64,
}

#[derive(Serialize)]
struct StatusResponse<'a> {
    #[serde(rename = "type")]
    msg_type: &'static str,
    status: &'static str,
    clm: &'a str,
    payload: &'a str,
}

#[derive(Serialize)]
struct ErrorResponse<'a> {
    #[serde(rename = "type")]
    msg_type: &'static str,
    error: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    exit_code: Option<i32>,
}

#[derive(Serialize)]
struct LogResponse<'a> {
    #[serde(rename = "type")]
    msg_type: &'static str,
    message: &'a str,
}

#[derive(Serialize)]
struct CompletedResponse<'a> {
    #[serde(rename = "type")]
    msg_type: &'static str,
    exit_code: i32,
    #[serde(skip_serializing_if = "Option::is_none")]
    witness_hash: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    witness_yaml: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    result: Option<&'static str>,
}

type WsWriter = Arc<Mutex<futures_util::stream::SplitSink<tokio_tungstenite::WebSocketStream<TcpStream>, Message>>>;

// Response message serialization helpers
async fn send_error(writer: &WsWriter, error: &str, exit_code: Option<i32>) {
    let err_res = ErrorResponse {
        msg_type: "error",
        error,
        exit_code,
    };
    if let Ok(json_str) = serde_json::to_string(&err_res) {
        let mut writer_lock = writer.lock().await;
        let _ = writer_lock.send(Message::Text(json_str)).await;
    }
}

async fn send_status(writer: &WsWriter, status: &'static str, clm: &str, payload: &str) {
    let start_res = StatusResponse {
        msg_type: "status",
        status,
        clm,
        payload,
    };
    if let Ok(json_str) = serde_json::to_string(&start_res) {
        let mut writer_lock = writer.lock().await;
        let _ = writer_lock.send(Message::Text(json_str)).await;
    }
}

async fn send_log(writer: &WsWriter, message: &str) {
    let log_res = LogResponse {
        msg_type: "log",
        message,
    };
    if let Ok(json_str) = serde_json::to_string(&log_res) {
        let mut writer_lock = writer.lock().await;
        let _ = writer_lock.send(Message::Text(json_str)).await;
    }
}

async fn send_completed(
    writer: &WsWriter,
    exit_code: i32,
    witness_hash: Option<&str>,
    witness_yaml: Option<&str>,
    result: Option<&'static str>,
) {
    let completed_res = CompletedResponse {
        msg_type: "completed",
        exit_code,
        witness_hash,
        witness_yaml,
        result,
    };
    if let Ok(json_str) = serde_json::to_string(&completed_res) {
        let mut writer_lock = writer.lock().await;
        let _ = writer_lock.send(Message::Text(json_str)).await;
    }
}

async fn send_chat_token(writer: &WsWriter, token: &str) {
    let token_msg = serde_json::json!({
        "type": "chat_token",
        "token": token
    }).to_string();
    let mut writer_lock = writer.lock().await;
    let _ = writer_lock.send(Message::Text(token_msg)).await;
}

impl NetworkGateway {
    pub fn new() -> Self {
        let payload = env::var("CLM_PAYLOAD").unwrap_or_default();
        let mut host = "127.0.0.1".to_string();
        let mut port = 8080;
        let mut timeout = 30000;

        if !payload.is_empty() {
            if let Ok(host_re) = Regex::new(r"host=([^\s]+)") {
                if let Some(caps) = host_re.captures(&payload) {
                    if let Some(m) = caps.get(1) {
                        host = m.as_str().to_string();
                    }
                }
            }
            if let Ok(port_re) = Regex::new(r"port=(\d+)") {
                if let Some(caps) = port_re.captures(&payload) {
                    if let Some(m) = caps.get(1) {
                        if let Ok(p) = m.as_str().parse::<u16>() {
                            port = p;
                        }
                    }
                }
            }
            if let Ok(timeout_re) = Regex::new(r"timeout=(\d+)") {
                if let Some(caps) = timeout_re.captures(&payload) {
                    if let Some(m) = caps.get(1) {
                        if let Ok(t) = m.as_str().parse::<u64>() {
                            timeout = t;
                        }
                    }
                }
            }
        }

        Self { host, port, timeout }
    }

    pub async fn start(&self) -> Result<(), String> {
        let addr = format!("{}:{}", self.host, self.port);
        let listener = TcpListener::bind(&addr).await
            .map_err(|e| format!("Failed to bind to {}: {}", addr, e))?;

        println!("[GATEWAY] WebSocket Gateway server listening on ws://{}", addr);

        let active_connections = Arc::new(Mutex::new(HashSet::new()));
        let shutdown_trigger = Arc::new(tokio::sync::Notify::new());

        // Spawn timeout clean sweep
        let _active_connections_clone = Arc::clone(&active_connections);
        let shutdown_trigger_clone = Arc::clone(&shutdown_trigger);
        let timeout_duration = self.timeout;
        tokio::spawn(async move {
            sleep(Duration::from_millis(timeout_duration)).await;
            println!("[GATEWAY] Reached timeout of {}ms. Shutting down.", timeout_duration);
            shutdown_trigger_clone.notify_one();
        });

        loop {
            tokio::select! {
                _ = shutdown_trigger.notified() => {
                    break;
                }
                accept_res = listener.accept() => {
                    match accept_res {
                        Ok((stream, _)) => {
                            let active_connections = Arc::clone(&active_connections);
                            let shutdown_trigger = Arc::clone(&shutdown_trigger);
                            tokio::spawn(async move {
                                if let Err(e) = handle_connection(stream, active_connections, shutdown_trigger).await {
                                    eprintln!("[GATEWAY] Error handling connection: {}", e);
                                }
                            });
                        }
                        Err(e) => {
                            eprintln!("[GATEWAY] Failed to accept connection: {}", e);
                        }
                    }
                }
            }
        }

        // Graceful exit
        Ok(())
    }
}

async fn execute_ollama(
    query_text: &str,
    json_msg: &Option<Value>,
    writer: &WsWriter,
) -> Result<(), String> {
    println!("[GATEWAY] Entering Ollama fallback logic...");
    let selected_model = json_msg.as_ref()
        .and_then(|v| v.get("model").and_then(|m| m.as_str()))
        .unwrap_or("gemma4:latest");
    let ollama_url = env::var("OLLAMA_URL").unwrap_or_else(|_| "http://127.0.0.1:11434".to_string());
    
    let ollama_host;
    let mut ollama_port = 11434;

    // Parse basic host/port
    let temp = ollama_url.trim_start_matches("http://").trim_start_matches("https://");
    let temp = temp.split('/').next().unwrap_or(temp);
    if let Some(idx) = temp.find(':') {
        ollama_host = temp[..idx].to_string();
        if let Ok(p) = temp[idx + 1..].parse::<u16>() {
            ollama_port = p;
        }
    } else {
        ollama_host = temp.to_string();
    }

    let mut system_content = "You are a friendly and helpful AI math assistant inside the GASING (Gampang, Asyik, Menyenangkan) interactive learning game. Keep your answers brief, encouraging, and clear for kids.".to_string();
    if let Some(val) = json_msg {
        if let Some(game_id) = val.get("gameId").and_then(|g| g.as_str()) {
            system_content.push_str(&format!("\nCurrent Game context:\n- Game ID: {}\n- Round: {}", game_id, val.get("round").and_then(|r| r.as_u64()).unwrap_or(1)));
            if let Some(q) = val.get("question").and_then(|q| q.as_str()) {
                system_content.push_str(&format!("\n- Current Problem: {}", q));
            }
            if let Some(exp) = val.get("gasingExplanation").and_then(|e| e.as_str()) {
                system_content.push_str(&format!("\n- GASING Methodology/Explanation: {}", exp));
            }
        }
    }

    let post_data = serde_json::json!({
        "model": selected_model,
        "messages": [
            { "role": "system", "content": system_content },
            { "role": "user", "content": query_text }
        ],
        "stream": true
    }).to_string();

    // Send status
    send_status(writer, "started", &format!("Ollama ({})", selected_model), query_text).await;

    match TcpStream::connect(format!("{}:{}", ollama_host, ollama_port)).await {
        Ok(mut tcp_stream) => {
            let request_headers = format!(
                "POST /api/chat HTTP/1.1\r\n\
                 Host: {}:{}\r\n\
                 Content-Type: application/json\r\n\
                 Content-Length: {}\r\n\
                 Connection: close\r\n\r\n{}",
                ollama_host, ollama_port, post_data.len(), post_data
            );
            if tcp_stream.write_all(request_headers.as_bytes()).await.is_ok() {
                let mut reader = tokio::io::BufReader::new(tcp_stream);
                let mut line = String::new();
                use tokio::io::AsyncBufReadExt;
                
                // Skip HTTP response headers
                loop {
                    line.clear();
                    if reader.read_line(&mut line).await.unwrap_or(0) == 0 {
                        break;
                    }
                    if line.trim().is_empty() {
                        break;
                    }
                }

                // Read HTTP body line by line
                loop {
                    line.clear();
                    if reader.read_line(&mut line).await.unwrap_or(0) == 0 {
                        break;
                    }
                    let trimmed = line.trim();
                    if trimmed.is_empty() {
                        continue;
                    }
                    if let Ok(parsed) = serde_json::from_str::<Value>(trimmed) {
                        if let Some(token) = parsed.get("message").and_then(|m| m.get("content")).and_then(|c| c.as_str()) {
                            send_chat_token(writer, token).await;
                        }
                    }
                }
            }
            send_completed(writer, 0, None, None, None).await;
        }
        Err(err) => {
            send_error(writer, &format!("Ollama is offline or model could not be loaded. Error: {}", err), None).await;
        }
    }
    Ok(())
}

async fn execute_clm(
    clm_file: &str,
    clm_payload: &str,
    writer: &WsWriter,
) -> Result<(), String> {
    let clm_root = env::var("CLM_ROOT").unwrap_or_else(|_| ".".to_string());
    let path_buf = PathBuf::from(clm_file);
    let absolute_path = if path_buf.is_absolute() {
        path_buf
    } else {
        Path::new(&clm_root).join(clm_file)
    };

    if !absolute_path.exists() {
        send_error(writer, &format!("CLM file not found: {}", clm_file), None).await;
        return Ok(());
    }

    let file_name = absolute_path.file_name().and_then(|s| s.to_str()).unwrap_or("");
    send_status(writer, "started", file_name, clm_payload).await;

    // Execute via IPC Dispatcher
    let dispatch_sock = env::var("CLM_DISPATCH_SOCK").unwrap_or_default();
    if dispatch_sock.is_empty() {
        send_error(writer, "IPC dispatcher not configured (CLM_DISPATCH_SOCK missing)", None).await;
        return Ok(());
    }

    #[cfg(unix)]
    {
        match UnixStream::connect(&dispatch_sock).await {
            Ok(unix_stream) => {
                let (ipc_read, mut ipc_write) = unix_stream.into_split();
                let request = serde_json::json!({
                    "action": "execute",
                    "clm_file": absolute_path.to_string_lossy(),
                    "payload": clm_payload
                }).to_string() + "\n";

                if ipc_write.write_all(request.as_bytes()).await.is_ok() {
                    let mut ipc_reader = tokio::io::BufReader::new(ipc_read);
                    let mut line = String::new();
                    use tokio::io::AsyncBufReadExt;

                    while let Ok(n) = ipc_reader.read_line(&mut line).await {
                        if n == 0 {
                            break;
                        }
                        let trimmed = line.trim();
                        if trimmed.is_empty() {
                            line.clear();
                            continue;
                        }

                        if let Ok(parsed) = serde_json::from_str::<Value>(trimmed) {
                            let msg_type = parsed.get("type").and_then(|t| t.as_str()).unwrap_or("");
                            if msg_type == "stdout" {
                                if let Some(stdout_data) = parsed.get("data").and_then(|d| d.as_str()) {
                                    if stdout_data.contains("CLM_TRACE_JSON:") {
                                        if let Some(idx) = stdout_data.find("CLM_TRACE_JSON:") {
                                            let mut json_str = stdout_data[idx + "CLM_TRACE_JSON:".len()..].trim().to_string();
                                            // Strip ANSI escape codes
                                            if let Ok(ansi_re) = Regex::new(r"\x1B\[[0-9;]*m") {
                                                json_str = ansi_re.replace_all(&json_str, "").to_string();
                                            }
                                            if let Ok(trace_val) = serde_json::from_str::<Value>(&json_str) {
                                                let mut writer_lock = writer.lock().await;
                                                let _ = writer_lock.send(Message::Text(trace_val.to_string())).await;
                                            }
                                        }
                                    }
                                }
                            } else if msg_type == "stderr" {
                                if let Some(stderr_data) = parsed.get("data").and_then(|d| d.as_str()) {
                                    send_log(writer, stderr_data).await;
                                }
                            } else if msg_type == "completed" {
                                let exit_code = parsed.get("exit_code").and_then(|c| c.as_i64()).unwrap_or(0) as i32;
                                if exit_code != 0 {
                                    send_error(writer, &format!("Execution failed with exit code {}", exit_code), Some(exit_code)).await;
                                } else {
                                    let witness_hash = parsed.get("witness_hash").and_then(|w| w.as_str());
                                    let witness_yaml = parsed.get("witness_yaml").and_then(|w| w.as_str());
                                    
                                    send_completed(
                                        writer,
                                        0,
                                        witness_hash,
                                        witness_yaml,
                                        if witness_hash.is_none() { Some("Execution completed successfully") } else { None },
                                    ).await;
                                }
                                break;
                            } else if msg_type == "error" {
                                let err_msg = parsed.get("error").and_then(|e| e.as_str()).unwrap_or("Unknown dispatch error");
                                send_error(writer, err_msg, None).await;
                                break;
                            }
                        }
                        line.clear();
                    }
                }
            }
            Err(e) => {
                send_error(writer, &format!("IPC dispatch connection failed: {}", e), None).await;
            }
        }
    }

    #[cfg(not(unix))]
    {
        send_error(writer, "IPC Unix domain sockets are only supported on Unix targets", None).await;
    }

    Ok(())
}

async fn process_message(
    text: &str,
    writer: &WsWriter,
    shutdown_trigger: &Arc<tokio::sync::Notify>,
) -> Result<bool, String> {
    if text == "shutdown" || text.contains("\"type\":\"shutdown\"") {
        println!("[GATEWAY] Graceful shutdown requested.");
        let reply = serde_json::json!({ "type": "shutdown", "status": "ok" }).to_string();
        {
            let mut writer_lock = writer.lock().await;
            let _ = writer_lock.send(Message::Text(reply)).await;
        }
        shutdown_trigger.notify_one();
        return Ok(true);
    }

    let mut clm_file = String::new();
    let mut clm_payload = String::new();
    let mut is_ping = false;

    let mut json_msg: Option<Value> = None;
    if let Ok(val) = serde_json::from_str::<Value>(text) {
        json_msg = Some(val.clone());
        if let Some(msg_type) = val.get("type").and_then(|t| t.as_str()) {
            if msg_type == "execute" {
                clm_file = val.get("clm_file").and_then(|f| f.as_str()).unwrap_or("").to_string();
                clm_payload = val.get("payload").and_then(|p| p.as_str()).unwrap_or("").to_string();
            } else if msg_type == "ping" {
                is_ping = true;
            }
        }
    } else if text == "ping" {
        is_ping = true;
    }

    if is_ping {
        let reply = serde_json::json!({ "type": "pong" }).to_string();
        let mut writer_lock = writer.lock().await;
        let _ = writer_lock.send(Message::Text(reply)).await;
        return Ok(false);
    }

    // Extract message content
    let mut query_text = text.to_string();
    if let Some(val) = &json_msg {
        if val.get("type").and_then(|t| t.as_str()) == Some("chat") {
            query_text = val.get("message").and_then(|m| m.as_str()).unwrap_or("").to_string();
        }
    }

    if clm_file.is_empty() {
        let lower = query_text.to_lowercase();
        if lower.contains("hello world") || lower.contains("hello_world") {
            clm_file = "computable_knowledge/Abstract/layer5_system/playbooks/hello_world.yaml".to_string();
        } else if lower.contains("gcd") {
            clm_file = "tests/test_fixtures/fixture_python_gcd.yaml".to_string();
            if let Ok(num_re) = Regex::new(r"\d+") {
                let nums: Vec<&str> = num_re.find_iter(&query_text).map(|m| m.as_str()).collect();
                if nums.len() >= 2 {
                    clm_payload = format!("{} {}", nums[0], nums[1]);
                } else {
                    clm_payload = "24 36".to_string();
                }
            }
        } else if lower.contains("lcm") {
            clm_file = "tests/test_fixtures/fixture_python_lcm.yaml".to_string();
            if let Ok(num_re) = Regex::new(r"\d+") {
                let nums: Vec<&str> = num_re.find_iter(&query_text).map(|m| m.as_str()).collect();
                if nums.len() >= 2 {
                    clm_payload = format!("{} {}", nums[0], nums[1]);
                } else {
                    clm_payload = "12 18".to_string();
                }
            }
        } else if lower.contains("fibonacci") || lower.contains("fib") {
            clm_file = "tests/test_fixtures/fixture_python_fib.yaml".to_string();
            if let Ok(num_re) = Regex::new(r"\d+") {
                if let Some(m) = num_re.find(&query_text) {
                    clm_payload = m.as_str().to_string();
                } else {
                    clm_payload = "10".to_string();
                }
            }
        } else {
            execute_ollama(&query_text, &json_msg, writer).await?;
            return Ok(false);
        }
    }

    execute_clm(&clm_file, &clm_payload, writer).await?;
    Ok(false)
}

async fn handle_connection(
    stream: TcpStream,
    _active_connections: Arc<Mutex<HashSet<String>>>,
    shutdown_trigger: Arc<tokio::sync::Notify>,
) -> Result<(), String> {
    let ws_stream = accept_async(stream).await
        .map_err(|e| format!("WebSocket handshake failed: {}", e))?;

    println!("[GATEWAY] Client connected successfully.");
    let (ws_writer, mut ws_reader) = ws_stream.split();
    let ws_writer = Arc::new(Mutex::new(ws_writer));

    while let Some(msg_res) = ws_reader.next().await {
        let msg = match msg_res {
            Ok(m) => m,
            Err(e) => return Err(format!("Error reading WS message: {}", e)),
        };

        if msg.is_text() || msg.is_binary() {
            let text = msg.to_text().unwrap_or("").trim();
            println!("[GATEWAY] Received: {}", text);

            let should_exit = process_message(text, &ws_writer, &shutdown_trigger).await?;
            if should_exit {
                return Ok(());
            }
        }
    }

    println!("[GATEWAY] Client disconnected.");
    Ok(())
}