pie 0.1.1

Programmable Inference Engine (PIE)
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
use crate::instance::Id as InstanceId;
use crate::messaging::dispatch_u2i;
use crate::model::attach_new_remote_backend;
use crate::runtime::RuntimeError;
use crate::service::{Service, ServiceError};
use crate::utils::IdPool;
use crate::{auth, messaging, model, runtime, service};
use anyhow::Result;
use dashmap::DashMap;
use futures::{SinkExt, StreamExt};
use rmp_serde::decode;
use serde::{Deserialize, Serialize};
use std::mem;
use std::sync::{Arc, OnceLock};
use thiserror::Error;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{Mutex, mpsc, oneshot};
use tokio::task;
use tokio::task::JoinHandle;
use tokio_tungstenite::accept_async;
use tungstenite::Message;
use tungstenite::protocol::Message as WsMessage;
use uuid::Uuid;

pub const CHUNK_SIZE_BYTES: usize = 256 * 1024; // 256 KiB
static SERVICE_ID_SERVER: OnceLock<usize> = OnceLock::new();

/// Define the various errors that can happen while handling messages.
#[derive(Debug, Error)]
pub enum ServerError {
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    #[error("WebSocket accept error: {0}")]
    WsAccept(#[from] tungstenite::Error),

    #[error("MessagePack decode error: {0}")]
    MsgPackDecode(#[from] rmp_serde::decode::Error),

    #[error("Text frames not supported")]
    TextFrameNotSupported,

    #[error("Chunk size {actual} exceeds {limit} bytes limit")]
    ChunkTooLarge { actual: usize, limit: usize },

    #[error("Mismatch in total_chunks: was {was}, now {now}")]
    ChunkCountMismatch { was: usize, now: usize },

    #[error("Out-of-order chunk: expected {expected}, got {got}")]
    OutOfOrderChunk { expected: usize, got: usize },

    #[error("Hash mismatch: expected {expected}, got {found})")]
    HashMismatch { expected: String, found: String },

    #[error("Invalid instance_id: {0}")]
    InvalidInstanceId(String),

    #[error("Instance {instance} not owned by client")]
    NotOwnedInstance { instance: String },

    #[error("No such running instance: {0}")]
    NoSuchRunningInstance(String),

    #[error("Failed to write program: {0}")]
    FileWriteError(#[source] std::io::Error),

    #[error("Failed to start program: {0}")]
    StartProgramFailed(#[from] RuntimeError),
}

/// Messages from client -> server
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ClientMessage {
    #[serde(rename = "authenticate")]
    Authenticate { corr_id: u32, token: String },

    #[serde(rename = "query")]
    Query {
        corr_id: u32,
        subject: String,
        record: String,
    },

    #[serde(rename = "upload_program")]
    UploadProgram {
        corr_id: u32,
        program_hash: String,
        chunk_index: usize,
        total_chunks: usize,
        #[serde(with = "serde_bytes")]
        chunk_data: Vec<u8>,
    },

    #[serde(rename = "launch_instance")]
    LaunchInstance {
        corr_id: u32,
        program_hash: String,
        arguments: Vec<String>,
    },

    #[serde(rename = "launch_server_instance")]
    LaunchServerInstance {
        corr_id: u32,
        port: u32,
        program_hash: String,
        arguments: Vec<String>,
    },

    #[serde(rename = "signal_instance")]
    SignalInstance {
        instance_id: String,
        message: String,
    },

    #[serde(rename = "terminate_instance")]
    TerminateInstance { instance_id: String },

    #[serde(rename = "attach_remote_service")]
    AttachRemoteService {
        corr_id: u32,
        endpoint: String,
        service_type: String,
        service_name: String,
    },
}

/// Messages from server -> client
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ServerMessage {
    #[serde(rename = "response")]
    Response {
        corr_id: u32,
        successful: bool,
        result: String,
    },

    #[serde(rename = "instance_event")]
    InstanceEvent {
        instance_id: String,
        event: String,
        message: String,
    },

    #[serde(rename = "server_event")]
    ServerEvent { message: String },
}

type ClientId = u32;

#[derive(Debug)]
pub enum Command {
    Send {
        inst_id: InstanceId,
        message: String,
    },
    DetachInstance {
        inst_id: InstanceId,
        reason: String,
    },
}

impl Command {
    pub fn dispatch(self) -> Result<(), ServiceError> {
        let service_id =
            *SERVICE_ID_SERVER.get_or_init(move || service::get_service_id("server").unwrap());
        service::dispatch(service_id, self)
    }
}

struct ServerState {
    enable_auth: bool,
    client_id_pool: Mutex<IdPool<ClientId>>,
    clients: DashMap<ClientId, JoinHandle<()>>,
    instance_chans: DashMap<InstanceId, mpsc::Sender<ClientCommand>>,
}

pub struct Server {
    state: Arc<ServerState>,
    listener_loop: task::JoinHandle<()>,
}

impl Server {
    pub fn new(addr: &str, enable_auth: bool) -> Self {
        let state = Arc::new(ServerState {
            enable_auth,
            client_id_pool: Mutex::new(IdPool::new(ClientId::MAX)),
            clients: DashMap::new(),
            instance_chans: DashMap::new(),
        });

        let listener_loop = task::spawn(Self::listener_loop(addr.to_string(), state.clone()));
        Server {
            state,
            listener_loop,
        }
    }

    async fn listener_loop(addr: String, state: Arc<ServerState>) {
        let listener = TcpListener::bind(addr).await.unwrap();
        while let Ok((stream, _addr)) = listener.accept().await {
            let id = {
                let mut id_pool = state.client_id_pool.lock().await;
                id_pool.acquire().unwrap()
            };
            if let Ok(mut client) = Client::new(id, stream, state.clone()).await {
                let client_handle = task::spawn(async move {
                    client.run().await;
                });

                state.clients.insert(id, client_handle);
            }
        }
    }
}

impl Service for Server {
    type Command = Command;

    async fn handle(&mut self, cmd: Self::Command) {
        let inst_id = match cmd {
            Command::Send { inst_id, .. } | Command::DetachInstance { inst_id, .. } => inst_id,
        };

        // send it to the client if it's connected
        if let Some(chan) = self.state.instance_chans.get(&inst_id) {
            chan.send(ClientCommand::Internal(cmd)).await.ok();
        }
    }
}

struct InFlightUpload {
    program_hash: String,
    total_chunks: usize,
    buffer: Vec<u8>,
}

struct Client {
    id: ClientId,
    authenticated: bool,

    state: Arc<ServerState>,

    inflight_upload: Option<InFlightUpload>,
    inst_owned: Vec<InstanceId>,

    write_tx: mpsc::Sender<WsMessage>,
    incoming_rx: mpsc::Receiver<ClientCommand>,
    incoming_tx: mpsc::Sender<ClientCommand>,

    writer_task: JoinHandle<()>,
    reader_task: JoinHandle<()>,
}

enum ClientCommand {
    FromClient(ClientMessage),
    Internal(Command),
}

pub const QUERY_PROGRAM_EXISTS: &str = "program_exists";
pub const QUERY_MODEL_STATUS: &str = "model_status";

impl Client {
    async fn new(id: ClientId, stream: TcpStream, state: Arc<ServerState>) -> Result<Self> {
        let (write_tx, mut write_rx) = mpsc::channel(1000);
        let (incoming_tx, incoming_rx) = mpsc::channel(1000);

        let ws_stream = accept_async(stream).await?;
        let (mut ws_writer, mut ws_reader) = ws_stream.split();

        let writer_task = task::spawn(async move {
            while let Some(message) = write_rx.recv().await {
                if let Err(e) = ws_writer.send(message).await {
                    println!("Error writing to ws stream: {:?}", e);
                    break;
                }
            }
        });

        let incoming_tx_ = incoming_tx.clone();
        let reader_task = task::spawn(async move {
            let incoming_tx = incoming_tx_;
            while let Some(Ok(msg)) = ws_reader.next().await {
                match msg {
                    Message::Binary(bin) => {
                        // Decode via rmp-serde
                        match decode::from_slice::<ClientMessage>(&bin) {
                            Ok(client_message) => {
                                incoming_tx
                                    .send(ClientCommand::FromClient(client_message))
                                    .await
                                    .ok();
                            }
                            Err(e) => {
                                eprintln!("Failed to decode client msgpack: {:?}", e);
                            }
                        }
                    }
                    Message::Close(_) => {
                        break;
                    }
                    Message::Text(_) | Message::Ping(_) | Message::Pong(_) => {}
                    _ => {
                        eprintln!("Unexpected message type from client");
                        // ignore
                    }
                }
            }
        });

        Ok(Self {
            id,
            authenticated: !state.enable_auth,
            state,
            inflight_upload: None,
            inst_owned: Vec::new(),
            write_tx,
            incoming_rx,
            incoming_tx,
            writer_task,
            reader_task,
        })
    }

    /// Manages the entire lifecycle of a client connection.
    async fn run(&mut self) {
        loop {
            tokio::select! {
                // To prevent starvation, prioritize processing existing messages
                // over checking for disconnection.
                biased;

                // Branch 1: A new command was received from the client or an internal service.
                Some(cmd) = self.incoming_rx.recv() => {
                    self.handle_command(cmd).await;
                },

                // Branch 2: The reader task has finished. This is a primary signal
                // that the client has disconnected.
                _ = &mut self.reader_task => {
                    // println!("Client {} disconnected (reader task finished).", self.id);
                    break;
                },

                // Branch 3: The writer task has finished, likely due to a
                // "broken pipe" error when trying to send a message.
                _ = &mut self.writer_task => {
                    // println!("Client {} disconnected (writer task finished).", self.id);
                    break;
                }

                // The `else` branch is taken when all other branches are disabled,
                // meaning the channel is closed and tasks are done. This is a clean shutdown.
                else => break,
            }
        }

        // The loop has exited, so we can now guarantee that cleanup will run.
        self.cleanup().await;
    }

    /// Processes a single command. This contains the logic from your old `serve` method.
    async fn handle_command(&mut self, cmd: ClientCommand) {
        match cmd {
            ClientCommand::FromClient(message) => match message {
                ClientMessage::Authenticate { corr_id, token } => {
                    self.handle_authenticate(corr_id, token).await
                }
                ClientMessage::Query {
                    corr_id,
                    subject,
                    record,
                } => self.handle_query(corr_id, subject, record).await,
                ClientMessage::UploadProgram {
                    corr_id,
                    program_hash,
                    chunk_index,
                    total_chunks,
                    chunk_data,
                } => {
                    self.handle_upload_program(
                        corr_id,
                        program_hash,
                        chunk_index,
                        total_chunks,
                        chunk_data,
                    )
                    .await
                }
                ClientMessage::LaunchInstance {
                    corr_id,
                    program_hash,
                    arguments,
                } => {
                    self.handle_launch_instance(corr_id, program_hash, arguments)
                        .await
                }
                ClientMessage::LaunchServerInstance {
                    corr_id,
                    port,
                    program_hash,
                    arguments,
                } => {
                    self.handle_launch_server_instance(corr_id, port, program_hash, arguments)
                        .await
                }
                ClientMessage::SignalInstance {
                    instance_id,
                    message,
                } => self.handle_signal_instance(instance_id, message).await,
                ClientMessage::TerminateInstance { instance_id } => {
                    self.handle_terminate_instance(instance_id).await
                }
                ClientMessage::AttachRemoteService {
                    corr_id,
                    endpoint,
                    service_type,
                    service_name,
                } => {
                    self.handle_attach_remote_service(
                        corr_id,
                        endpoint,
                        service_type,
                        service_name,
                    )
                    .await;
                }
            },
            ClientCommand::Internal(cmd) => match cmd {
                Command::Send { inst_id, message } => {
                    self.send_inst_event(inst_id, "message".to_string(), message)
                        .await
                }
                Command::DetachInstance { inst_id, reason } => {
                    self.handle_detach_instance(inst_id, reason).await;
                }
            },
        }
    }

    async fn send(&mut self, msg: ServerMessage) {
        match rmp_serde::to_vec_named(&msg) {
            Ok(encoded) => {
                if let Err(e) = self.write_tx.send(WsMessage::Binary(encoded.into())).await {
                    eprintln!("WS write error: {:?}", e);
                }
            }
            Err(e) => {
                eprintln!("MessagePack encode error: {:?}", e);
            }
        }
    }

    async fn send_response(&mut self, corr_id: u32, successful: bool, result: String) {
        let msg = ServerMessage::Response {
            corr_id,
            successful,
            result,
        };
        self.send(msg).await;
    }

    async fn send_inst_event(&mut self, inst_id: InstanceId, event: String, message: String) {
        self.send(ServerMessage::InstanceEvent {
            instance_id: inst_id.to_string(),
            event,
            message,
        })
        .await;
    }

    async fn handle_detach_instance(&mut self, inst_id: InstanceId, reason: String) {
        if !self.authenticated {
            return;
        }
        self.inst_owned.retain(|&id| id != inst_id);

        if self.state.instance_chans.remove(&inst_id).is_some() {
            self.send_inst_event(inst_id, "terminated".to_string(), reason)
                .await;
        }
    }

    async fn handle_authenticate(&mut self, corr_id: u32, token: String) {
        if !self.authenticated {
            if let Ok(claims) = auth::validate_jwt(&token) {
                self.authenticated = true;
                self.send_response(corr_id, true, claims.sub).await;
            } else {
                self.send_response(corr_id, false, "Invalid token".to_string())
                    .await;
            }
        } else {
            self.send_response(corr_id, true, "Already authenticated".to_string())
                .await;
        }
    }

    async fn handle_query(&mut self, corr_id: u32, subject: String, record: String) {
        if !self.authenticated {
            self.send_response(corr_id, false, "Not authenticated".to_string())
                .await;
        }

        match subject.as_str() {
            QUERY_PROGRAM_EXISTS => {
                let (evt_tx, evt_rx) = oneshot::channel();

                runtime::Command::ProgramExists {
                    hash: record.clone(),
                    event: evt_tx,
                }
                .dispatch()
                .unwrap();

                let exists = evt_rx.await.unwrap();
                self.send_response(corr_id, true, exists.to_string()).await;
            }
            QUERY_MODEL_STATUS => {
                // gather model status from all attached backends
                let l4m_stats = model::gather_stats().await;

                self.send_response(corr_id, true, l4m_stats).await;
            }
            _ => {
                println!("Unknown query subject: {}", subject);
            }
        }
    }

    async fn handle_upload_program(
        &mut self,
        corr_id: u32,
        program_hash: String,
        chunk_index: usize,
        total_chunks: usize,
        mut chunk_data: Vec<u8>,
    ) {
        if !self.authenticated {
            self.send_response(corr_id, false, "Not authenticated".to_string())
                .await;
        }

        if self.inflight_upload.is_none() {
            self.inflight_upload = Some(InFlightUpload {
                program_hash: program_hash.clone(),
                total_chunks,
                buffer: Vec::new(),
            });
        }

        if chunk_data.len() > CHUNK_SIZE_BYTES {
            self.send_response(
                corr_id,
                false,
                format!(
                    "chunk size {} exceeds limit {}",
                    chunk_data.len(),
                    CHUNK_SIZE_BYTES
                ),
            )
            .await;

            self.inflight_upload = None;
            return;
        }

        let inflight_upload = self.inflight_upload.as_mut().unwrap();
        inflight_upload.buffer.append(&mut chunk_data);

        // The last chunk
        if chunk_index == total_chunks - 1 {
            let file_hash = blake3::hash(&inflight_upload.buffer).to_hex().to_string();

            if file_hash != inflight_upload.program_hash {
                self.send_response(
                    corr_id,
                    false,
                    format!(
                        "hash mismatch: expected {}, got {}",
                        program_hash, file_hash
                    ),
                )
                .await;
            } else {
                let (evt_tx, evt_rx) = oneshot::channel();
                runtime::Command::UploadProgram {
                    hash: file_hash.clone(),
                    raw: mem::take(&mut inflight_upload.buffer),
                    event: evt_tx,
                }
                .dispatch()
                .unwrap();
                let _ = evt_rx.await.unwrap();

                self.send_response(corr_id, true, file_hash).await;
            }

            self.inflight_upload = None;
        }
    }

    async fn handle_launch_instance(
        &mut self,
        corr_id: u32,
        program_hash: String,
        arguments: Vec<String>,
    ) {
        if !self.authenticated {
            self.send_response(corr_id, false, "Not authenticated".to_string())
                .await;
        }

        let (evt_tx, evt_rx) = oneshot::channel();
        runtime::Command::LaunchInstance {
            program_hash: program_hash.clone(),
            arguments,
            event: evt_tx,
        }
        .dispatch()
        .unwrap();

        match evt_rx.await.unwrap() {
            Ok(instance_id) => {
                //register
                self.state
                    .instance_chans
                    .insert(instance_id, self.incoming_tx.clone());

                self.inst_owned.push(instance_id);
                self.send_response(corr_id, true, instance_id.to_string())
                    .await;
            }
            Err(e) => {
                self.send_response(corr_id, false, e.to_string()).await;
            }
        }
    }

    async fn handle_launch_server_instance(
        &mut self,
        corr_id: u32,
        port: u32,
        program_hash: String,
        arguments: Vec<String>,
    ) {
        if !self.authenticated {
            self.send_response(corr_id, false, "Not authenticated".to_string())
                .await;
        }

        let (evt_tx, evt_rx) = oneshot::channel();
        runtime::Command::LaunchServerInstance {
            program_hash: program_hash.clone(),
            port,
            arguments,
            event: evt_tx,
        }
        .dispatch()
        .unwrap();

        match evt_rx.await.unwrap() {
            Ok(_) => {
                self.send_response(corr_id, true, "server launched".to_string())
                    .await;
            }
            Err(e) => {
                self.send_response(corr_id, false, e.to_string()).await;
            }
        }
    }

    async fn handle_signal_instance(&mut self, instance_id: String, message: String) {
        if !self.authenticated {
            return;
        }
        if let Ok(inst_id) = Uuid::parse_str(&instance_id) {
            if self.inst_owned.contains(&inst_id) {
                dispatch_u2i(messaging::PushPullCommand::Push {
                    topic: inst_id.to_string(),
                    message,
                });
            }
        }
    }

    async fn handle_terminate_instance(&mut self, instance_id: String) {
        if !self.authenticated {
            return;
        }
        if let Ok(inst_id) = Uuid::parse_str(&instance_id) {
            if self.inst_owned.contains(&inst_id) {
                runtime::trap(inst_id, "user terminated the program");
            }
        }
    }

    async fn handle_attach_remote_service(
        &mut self,
        corr_id: u32,
        endpoint: String,
        service_type: String,
        service_name: String,
    ) {
        if !self.authenticated {
            self.send_response(corr_id, false, "Not authenticated".to_string())
                .await;
        }
        match service_type.as_str() {
            "l4m" => {
                if attach_new_remote_backend(&service_name, endpoint)
                    .await
                    .is_some()
                {
                    self.send_response(corr_id, true, "Attached to L4M backend".to_string())
                        .await;
                } else {
                    self.send_response(
                        corr_id,
                        false,
                        "Failed to attach to L4M backend".to_string(),
                    )
                    .await;
                }
            }
            _ => {
                self.send_response(
                    corr_id,
                    false,
                    format!("Unknown service type: {}", service_type),
                )
                .await;
            }
        }
    }

    /// The cleanup logic is now guaranteed to run.
    async fn cleanup(&mut self) {
        // Terminate all instances owned by this client.
        for inst_id in self.inst_owned.drain(..) {
            if self.state.instance_chans.remove(&inst_id).is_some() {
                runtime::trap(inst_id, "socket terminated");
            }
        }

        // Abort the tasks to ensure they are stopped. It's safe to abort
        // tasks that have already completed.
        self.reader_task.abort();
        self.writer_task.abort();

        // Remove the client from the central map.
        self.state.clients.remove(&self.id);

        // Release the client ID back to the pool.
        self.state
            .client_id_pool
            .lock()
            .await
            .release(self.id)
            .expect("Failed to release client ID");
    }
}