dist_agent_lang 1.0.19

Agentic programming with library and CLI support for Off/On-chain network integration
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
use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

#[derive(Debug, Clone)]
pub struct ThreadPoolStats {
    pub total_threads: usize,
    pub active_threads: usize,
    pub idle_threads: usize,
    pub queued_tasks: usize,
    pub completed_tasks: usize,
    pub failed_tasks: usize,
    pub average_task_duration: Duration,
}

pub struct ThreadPool {
    workers: Vec<Worker>,
    sender: std::sync::mpsc::Sender<Message>,
    stats: Arc<Mutex<ThreadPoolStats>>,
}

enum Message {
    NewJob(Box<dyn FnOnce() + Send + 'static>),
    Terminate,
}

struct Worker {
    #[allow(dead_code)]
    id: usize,
    thread: Option<thread::JoinHandle<()>>,
}

impl ThreadPool {
    pub fn new(size: usize) -> ThreadPool {
        assert!(size > 0);

        let (sender, receiver) = std::sync::mpsc::channel();
        let receiver = Arc::new(Mutex::new(receiver));
        let stats = Arc::new(Mutex::new(ThreadPoolStats {
            total_threads: size,
            active_threads: 0,
            idle_threads: size,
            queued_tasks: 0,
            completed_tasks: 0,
            failed_tasks: 0,
            average_task_duration: Duration::ZERO,
        }));

        let mut workers = Vec::with_capacity(size);

        for id in 0..size {
            workers.push(Worker::new(id, Arc::clone(&receiver), Arc::clone(&stats)));
        }

        ThreadPool {
            workers,
            sender,
            stats,
        }
    }

    pub fn execute<F>(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let job = Box::new(f);
        self.sender.send(Message::NewJob(job)).unwrap();

        // Update stats
        if let Ok(mut stats) = self.stats.lock() {
            stats.queued_tasks += 1;
        }
    }

    pub fn get_stats(&self) -> ThreadPoolStats {
        if let Ok(stats) = self.stats.lock() {
            stats.clone()
        } else {
            ThreadPoolStats {
                total_threads: 0,
                active_threads: 0,
                idle_threads: 0,
                queued_tasks: 0,
                completed_tasks: 0,
                failed_tasks: 0,
                average_task_duration: Duration::ZERO,
            }
        }
    }
}

impl Drop for ThreadPool {
    fn drop(&mut self) {
        for _ in &self.workers {
            self.sender.send(Message::Terminate).unwrap();
        }

        for worker in &mut self.workers {
            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        }
    }
}

impl Worker {
    fn new(
        id: usize,
        receiver: Arc<Mutex<std::sync::mpsc::Receiver<Message>>>,
        stats: Arc<Mutex<ThreadPoolStats>>,
    ) -> Worker {
        let thread = thread::spawn(move || {
            loop {
                let message = receiver.lock().unwrap().recv().unwrap();

                match message {
                    Message::NewJob(job) => {
                        // Update stats: worker becomes active
                        if let Ok(mut stats) = stats.lock() {
                            stats.active_threads += 1;
                            stats.idle_threads = stats.idle_threads.saturating_sub(1);
                        }

                        let start_time = Instant::now();
                        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(job));
                        let duration = start_time.elapsed();

                        // Update stats: job completed
                        if let Ok(mut stats) = stats.lock() {
                            stats.active_threads = stats.active_threads.saturating_sub(1);
                            stats.idle_threads += 1;
                            stats.queued_tasks = stats.queued_tasks.saturating_sub(1);

                            if result.is_ok() {
                                stats.completed_tasks += 1;
                            } else {
                                stats.failed_tasks += 1;
                            }

                            // Update average duration
                            let total_duration =
                                stats.average_task_duration * stats.completed_tasks as u32;
                            let new_total = total_duration + duration;
                            stats.average_task_duration =
                                new_total / (stats.completed_tasks + 1) as u32;
                        }
                    }
                    Message::Terminate => {
                        break;
                    }
                }
            }
        });

        Worker {
            id,
            thread: Some(thread),
        }
    }
}

// Async task scheduler
pub struct AsyncScheduler {
    thread_pool: ThreadPool,
    task_queue: Arc<Mutex<VecDeque<AsyncTask>>>,
    running_tasks: Arc<Mutex<Vec<RunningTask>>>,
}

pub struct AsyncTask {
    pub id: usize,
    pub name: String,
    pub priority: TaskPriority,
    pub task: Box<dyn FnOnce() -> Result<(), String> + Send + 'static>,
    pub created_at: Instant,
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum TaskPriority {
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 3,
}

#[derive(Debug)]
struct RunningTask {
    id: usize,
    #[allow(dead_code)]
    start_time: Instant,
    #[allow(dead_code)]
    thread_id: usize,
}

impl AsyncScheduler {
    pub fn new(thread_count: usize) -> Self {
        Self {
            thread_pool: ThreadPool::new(thread_count),
            task_queue: Arc::new(Mutex::new(VecDeque::new())),
            running_tasks: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub fn schedule<F>(&self, name: &str, priority: TaskPriority, task: F) -> usize
    where
        F: FnOnce() -> Result<(), String> + Send + 'static,
    {
        static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
        let task_id = NEXT_ID.fetch_add(1, Ordering::SeqCst);

        let async_task = AsyncTask {
            id: task_id,
            name: name.to_string(),
            priority,
            task: Box::new(task),
            created_at: Instant::now(),
        };

        if let Ok(mut queue) = self.task_queue.lock() {
            // Insert based on priority (higher priority first)
            let mut insert_index = None;
            for (i, existing_task) in queue.iter().enumerate() {
                if priority > existing_task.priority {
                    insert_index = Some(i);
                    break;
                }
            }

            if let Some(index) = insert_index {
                queue.insert(index, async_task);
            } else {
                queue.push_back(async_task);
            }
        }

        self.process_queue();
        task_id
    }

    fn process_queue(&self) {
        if let Ok(mut queue) = self.task_queue.lock() {
            if let Some(task) = queue.pop_front() {
                let task_id = task.id;
                let task_name = task.name.clone();

                let running_tasks = self.running_tasks.clone();
                self.thread_pool.execute(move || {
                    let start_time = Instant::now();

                    // Add to running tasks
                    if let Ok(mut running) = running_tasks.lock() {
                        running.push(RunningTask {
                            id: task_id,
                            start_time,
                            thread_id: std::time::Instant::now().elapsed().as_nanos() as usize,
                        });
                    }

                    // Execute task
                    let result = (task.task)();

                    // Remove from running tasks
                    if let Ok(mut running) = running_tasks.lock() {
                        running.retain(|t| t.id != task_id);
                    }

                    match result {
                        Ok(_) => println!("Task '{}' completed successfully", task_name),
                        Err(e) => println!("Task '{}' failed: {}", task_name, e),
                    }
                });
            }
        }
    }

    pub fn get_task_status(&self, task_id: usize) -> TaskStatus {
        // Check if task is running
        if let Ok(running) = self.running_tasks.lock() {
            if running.iter().any(|t| t.id == task_id) {
                return TaskStatus::Running;
            }
        }

        // Check if task is queued
        if let Ok(queue) = self.task_queue.lock() {
            if queue.iter().any(|t| t.id == task_id) {
                return TaskStatus::Queued;
            }
        }

        TaskStatus::Completed
    }

    pub fn cancel_task(&self, task_id: usize) -> bool {
        // Remove from queue
        if let Ok(mut queue) = self.task_queue.lock() {
            queue.retain(|t| t.id != task_id);
        }

        // Note: Can't cancel running tasks in this simple implementation
        true
    }
}

#[derive(Debug, Clone)]
pub enum TaskStatus {
    Queued,
    Running,
    Completed,
    Failed,
    Cancelled,
}

// Parallel execution utilities
pub struct ParallelExecutor;

impl ParallelExecutor {
    pub fn map_parallel<T, R, F>(items: Vec<T>, f: F, num_threads: usize) -> Vec<R>
    where
        T: Send + Sync + Clone + 'static,
        R: Send + Sync + 'static,
        F: Fn(T) -> R + Send + Sync + 'static,
    {
        let thread_pool = ThreadPool::new(num_threads);
        let (sender, receiver) = std::sync::mpsc::channel();
        let items = Arc::new(items);
        let f = Arc::new(f);

        // Submit tasks
        for i in 0..items.len() {
            let items = Arc::clone(&items);
            let f = Arc::clone(&f);
            let sender = sender.clone();

            thread_pool.execute(move || {
                let item = items[i].clone();
                let result = f(item);
                sender.send((i, result)).unwrap();
            });
        }

        // Collect results
        let mut results = Vec::new();
        results.resize_with(items.len(), || unsafe { std::mem::zeroed() });

        for _ in 0..items.len() {
            if let Ok((index, result)) = receiver.recv() {
                results[index] = result;
            }
        }

        results
    }

    pub fn reduce_parallel<T, F>(items: Vec<T>, f: F, num_threads: usize) -> T
    where
        T: Send + Sync + Clone + 'static,
        F: Fn(T, T) -> T + Send + Sync + Clone + 'static,
    {
        if items.is_empty() {
            panic!("Cannot reduce empty vector");
        }

        if items.len() == 1 {
            return items[0].clone();
        }

        // Divide and conquer approach
        let chunk_size = items.len().div_ceil(num_threads);
        let chunks: Vec<Vec<T>> = items
            .chunks(chunk_size)
            .map(|chunk| chunk.to_vec())
            .collect();

        let thread_pool = ThreadPool::new(num_threads);
        let (sender, receiver) = std::sync::mpsc::channel();

        let chunks_len = chunks.len();
        // Process each chunk
        for chunk in chunks {
            let sender = sender.clone();
            let f_clone = f.clone();
            thread_pool.execute(move || {
                let result = chunk.into_iter().reduce(f_clone).unwrap();
                sender.send(result).unwrap();
            });
        }

        // Combine results
        let mut final_result = receiver.recv().unwrap();
        for _ in 1..chunks_len {
            if let Ok(result) = receiver.recv() {
                final_result = f(final_result, result);
            }
        }

        final_result
    }
}

// Performance monitoring for concurrency
pub struct ConcurrencyProfiler {
    thread_pool_stats: Arc<Mutex<Vec<ThreadPoolStats>>>,
    task_execution_times: Arc<Mutex<Vec<Duration>>>,
}

impl ConcurrencyProfiler {
    pub fn new() -> Self {
        Self {
            thread_pool_stats: Arc::new(Mutex::new(Vec::new())),
            task_execution_times: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub fn record_thread_pool_stats(&self, stats: ThreadPoolStats) {
        if let Ok(mut stats_vec) = self.thread_pool_stats.lock() {
            stats_vec.push(stats);
            // Keep only last 100 entries
            if stats_vec.len() > 100 {
                stats_vec.remove(0);
            }
        }
    }

    pub fn record_task_execution_time(&self, duration: Duration) {
        if let Ok(mut times) = self.task_execution_times.lock() {
            times.push(duration);
            // Keep only last 1000 entries
            if times.len() > 1000 {
                times.remove(0);
            }
        }
    }

    pub fn generate_report(&self) -> String {
        let mut report = String::new();
        report.push_str("Concurrency Performance Report\n");
        report.push_str("=============================\n\n");

        // Thread pool statistics
        if let Ok(stats_vec) = self.thread_pool_stats.lock() {
            if let Some(latest_stats) = stats_vec.last() {
                report.push_str("Thread Pool Status:\n");
                report.push_str(&format!(
                    "  Total Threads: {}\n",
                    latest_stats.total_threads
                ));
                report.push_str(&format!(
                    "  Active Threads: {}\n",
                    latest_stats.active_threads
                ));
                report.push_str(&format!("  Idle Threads: {}\n", latest_stats.idle_threads));
                report.push_str(&format!("  Queued Tasks: {}\n", latest_stats.queued_tasks));
                report.push_str(&format!(
                    "  Completed Tasks: {}\n",
                    latest_stats.completed_tasks
                ));
                report.push_str(&format!("  Failed Tasks: {}\n", latest_stats.failed_tasks));
                report.push_str(&format!(
                    "  Average Task Duration: {:?}\n",
                    latest_stats.average_task_duration
                ));
            }
        }

        // Task execution time statistics
        if let Ok(times) = self.task_execution_times.lock() {
            if !times.is_empty() {
                let total_time: Duration = times.iter().sum();
                let avg_time = total_time / times.len() as u32;
                let min_time = times.iter().min().unwrap();
                let max_time = times.iter().max().unwrap();

                report.push_str("\nTask Execution Times:\n");
                report.push_str(&format!("  Total Tasks: {}\n", times.len()));
                report.push_str(&format!("  Average Time: {:?}\n", avg_time));
                report.push_str(&format!("  Min Time: {:?}\n", min_time));
                report.push_str(&format!("  Max Time: {:?}\n", max_time));
            }
        }

        report
    }
}

impl Default for ConcurrencyProfiler {
    fn default() -> Self {
        Self::new()
    }
}