venus 0.1.1-beta.2

Reactive notebook environment for Rust
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
//! Shared notebook execution pipeline for Venus CLI.
//!
//! This module provides a unified execution pipeline used by `run`, `watch`, and `export` commands.
//! It handles the common workflow of parsing, compiling, and executing notebook cells.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};

use venus_core::Error;
use venus_core::compile::{
    CellCompiler, CompilationResult, CompiledCell, CompilerConfig, ToolchainManager,
    UniverseBuilder,
};
use venus_core::execute::{ExecutionCallback, LinearExecutor};
use venus_core::graph::{CellId, CellInfo, CellParser, GraphEngine};
use venus_core::paths::NotebookDirs;
use venus_core::state::{BoxedOutput, StateManager};

use crate::colors;

/// Progress callback that prints execution status to the terminal.
pub struct ProgressCallback {
    /// Whether to show verbose output.
    verbose: bool,
}

impl ProgressCallback {
    /// Create a new progress callback.
    pub fn new() -> Self {
        Self { verbose: false }
    }

    /// Create a verbose progress callback.
    #[allow(dead_code)]
    pub fn verbose() -> Self {
        Self { verbose: true }
    }
}

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

impl ExecutionCallback for ProgressCallback {
    fn on_cell_started(&self, _cell_id: CellId, name: &str) {
        print!(
            "{}  ▶ Running{} {}{}... ",
            colors::CYAN,
            colors::RESET,
            colors::BOLD,
            name
        );
        colors::flush_stdout();
    }

    fn on_cell_completed(&self, _cell_id: CellId, _name: &str) {
        println!("{}{}", colors::GREEN, colors::RESET);
    }

    fn on_cell_error(&self, _cell_id: CellId, _name: &str, error: &Error) {
        println!("{}{}", colors::RED, colors::RESET);
        eprintln!("{}    Error:{} {}", colors::RED, colors::RESET, error);
    }

    fn on_level_started(&self, level: usize, cell_count: usize) {
        if self.verbose && cell_count > 1 {
            println!(
                "{}Level {}:{} {} cells (parallel)",
                colors::DIM,
                level,
                colors::RESET,
                cell_count
            );
        }
    }

    fn on_level_completed(&self, _level: usize) {}
}

/// Compiled cell with metadata for execution.
#[derive(Clone)]
pub struct CompiledCellInfo {
    /// The compiled cell.
    pub compiled: CompiledCell,
    /// Number of dependencies.
    pub dep_count: usize,
    /// Compilation time in milliseconds.
    #[allow(dead_code)]
    pub compile_time_ms: u64,
    /// Whether the cell was cached.
    #[allow(dead_code)]
    pub cached: bool,
}

/// Result of cell compilation.
pub struct CompilationInfo {
    /// Successfully compiled cells.
    pub cells: HashMap<CellId, CompiledCellInfo>,
    /// Compilation errors by cell name.
    pub errors: Vec<(String, Vec<venus_core::compile::CompileError>)>,
}

/// Result of cell execution.
pub struct ExecutionInfo {
    /// Cells that were executed (in order).
    pub executed_cells: Vec<CellId>,
    /// Execution time.
    pub execution_time: Duration,
    /// Cell outputs (cell_id -> output).
    pub outputs: HashMap<CellId, Arc<BoxedOutput>>,
}

/// Notebook executor that manages the full execution pipeline.
pub struct NotebookExecutor {
    /// Absolute path to the notebook file.
    pub notebook_path: PathBuf,
    /// Notebook source code.
    #[allow(dead_code)]
    pub source: String,
    /// Notebook directories.
    pub dirs: NotebookDirs,
    /// Toolchain manager.
    pub toolchain: ToolchainManager,
    /// Parsed cells.
    pub cells: Vec<CellInfo>,
    /// Cell name to ID mapping.
    pub cell_ids: HashMap<String, CellId>,
    /// Topological execution order.
    pub order: Vec<CellId>,
    /// Dependency map (cell_id -> dependencies).
    pub deps: HashMap<CellId, Vec<CellId>>,
    /// Compiler configuration.
    pub config: CompilerConfig,
    /// Universe builder (for dependency hash).
    pub universe_builder: UniverseBuilder,
    /// Path to compiled universe.
    pub universe_path: PathBuf,
    /// Whether using release mode.
    #[allow(dead_code)]
    pub release: bool,
}

impl NotebookExecutor {
    /// Create a new notebook executor.
    ///
    /// This performs the setup phase: parsing, dependency resolution, and universe building.
    pub fn new(notebook_path: &str, release: bool) -> anyhow::Result<Self> {
        let path = Path::new(notebook_path);
        if !path.exists() {
            anyhow::bail!("Notebook not found: {}", notebook_path);
        }

        let source = fs::read_to_string(path)?;
        let abs_path = path.canonicalize()?;
        let dirs = NotebookDirs::from_notebook_path(&abs_path)?;

        // Initialize toolchain
        Self::print_step("Checking toolchain");
        let toolchain = ToolchainManager::new()?;
        Self::print_success(None);

        // Parse cells
        Self::print_step("Parsing cells");
        let mut parser = CellParser::new();
        let parse_result = parser.parse_file(&abs_path)?;
        let cells = parse_result.code_cells;
        let definition_cells = parse_result.definition_cells;
        Self::print_success(Some(&format!("{} code cells", cells.len())));

        // Build dependency graph
        Self::print_step("Building dependency graph");
        let mut graph = GraphEngine::new();
        let mut cell_ids: HashMap<String, CellId> = HashMap::new();
        for cell in &cells {
            let real_id = graph.add_cell(cell.clone());
            cell_ids.insert(cell.name.clone(), real_id);
        }
        graph.resolve_dependencies()?;
        let order = graph.topological_order()?;
        Self::print_success(None);

        // Build dependency map
        let deps: HashMap<CellId, Vec<CellId>> = cells
            .iter()
            .map(|cell| {
                let real_id = cell_ids[&cell.name];
                let dep_ids: Vec<CellId> = cell
                    .dependencies
                    .iter()
                    .filter_map(|dep| cell_ids.get(&dep.param_name).copied())
                    .collect();
                (real_id, dep_ids)
            })
            .collect();

        // Build universe
        Self::print_step("Building universe");
        let config = if release {
            CompilerConfig::for_notebook_release(&dirs)
        } else {
            CompilerConfig::for_notebook(&dirs)
        };

        let mut universe_builder = UniverseBuilder::new(config.clone(), toolchain.clone(), None);
        universe_builder.parse_dependencies(&source, &definition_cells)?;
        let universe_path = universe_builder.build()?;

        if universe_builder.dependencies().is_empty() {
            Self::print_success(Some("runtime only"));
        } else {
            Self::print_success(None);
        }

        Ok(Self {
            notebook_path: abs_path,
            source,
            dirs,
            toolchain,
            cells,
            cell_ids,
            order,
            deps,
            config,
            universe_builder,
            universe_path,
            release,
        })
    }

    /// Get the notebook name (file stem).
    pub fn notebook_name(&self) -> String {
        self.notebook_path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string()
    }

    /// Compile all cells.
    pub fn compile(&self) -> anyhow::Result<CompilationInfo> {
        println!("\n{}Compiling cells...{}", colors::BOLD, colors::RESET);

        let compiler = CellCompiler::new(self.config.clone(), self.toolchain.clone())
            .with_universe(self.universe_path.clone());

        let mut compiled_cells = HashMap::new();
        let mut compile_errors = Vec::new();

        for cell in &self.cells {
            print!("  {} {} ... ", colors::DIM, cell.name);
            colors::flush_stdout();

            let real_id = self.cell_ids[&cell.name];
            let deps_hash = self.universe_builder.deps_hash();
            let result = compiler.compile(cell, deps_hash);

            match result {
                CompilationResult::Success(mut compiled) => {
                    let compile_time_ms = compiled.compile_time_ms;
                    compiled.cell_id = real_id;
                    println!(
                        "{}{} ({}ms)",
                        colors::GREEN,
                        colors::RESET,
                        compile_time_ms
                    );
                    compiled_cells.insert(
                        real_id,
                        CompiledCellInfo {
                            compiled,
                            dep_count: cell.dependencies.len(),
                            compile_time_ms,
                            cached: false,
                        },
                    );
                }
                CompilationResult::Cached(mut compiled) => {
                    compiled.cell_id = real_id;
                    println!(
                        "{}{} {}(cached){}",
                        colors::GREEN,
                        colors::RESET,
                        colors::DIM,
                        colors::RESET
                    );
                    compiled_cells.insert(
                        real_id,
                        CompiledCellInfo {
                            compiled,
                            dep_count: cell.dependencies.len(),
                            compile_time_ms: 0,
                            cached: true,
                        },
                    );
                }
                CompilationResult::Failed { cell_id: _, errors } => {
                    println!("{}{}", colors::RED, colors::RESET);
                    for error in &errors {
                        if let Some(rendered) = &error.rendered {
                            eprintln!("{}", rendered);
                        } else {
                            eprintln!("{}", error.format_terminal());
                        }
                    }
                    compile_errors.push((cell.name.clone(), errors));
                }
            }
        }

        Ok(CompilationInfo {
            cells: compiled_cells,
            errors: compile_errors,
        })
    }

    /// Execute cells with the given compilation info.
    ///
    /// If `cell_filter` is provided, only execute that cell and its dependencies.
    pub fn execute(
        &self,
        compilation: &CompilationInfo,
        cell_filter: Option<&str>,
    ) -> anyhow::Result<ExecutionInfo> {
        if !compilation.errors.is_empty() {
            println!(
                "\n{}Compilation failed for {} cell(s){}",
                colors::RED,
                compilation.errors.len(),
                colors::RESET
            );
            anyhow::bail!("Compilation failed");
        }

        println!("\n{}Executing cells...{}", colors::BOLD, colors::RESET);

        let state = StateManager::new(&self.dirs.state_dir)?;
        let mut executor = LinearExecutor::with_state(state);
        executor.set_callback(ProgressCallback::new());

        // Load all compiled cells
        for info in compilation.cells.values() {
            executor.load_cell(info.compiled.clone(), info.dep_count)?;
        }

        // Filter execution order if specific cell requested
        let execution_order = self.filter_execution_order(cell_filter)?;

        // Execute
        let exec_start = Instant::now();
        executor.execute_in_order(&execution_order, &self.deps)?;
        let execution_time = exec_start.elapsed();

        // Collect outputs
        let mut outputs = HashMap::new();
        for &cell_id in &execution_order {
            if let Some(output) = executor.state().get_output(cell_id) {
                outputs.insert(cell_id, output);
            }
        }

        Ok(ExecutionInfo {
            executed_cells: execution_order,
            execution_time,
            outputs,
        })
    }

    /// Execute cells without a callback (for export mode).
    pub fn execute_silent(
        &self,
        compilation: &CompilationInfo,
        cell_filter: Option<&str>,
    ) -> anyhow::Result<ExecutionInfo> {
        if !compilation.errors.is_empty() {
            anyhow::bail!("Compilation failed");
        }

        let state = StateManager::new(&self.dirs.state_dir)?;
        let mut executor = LinearExecutor::with_state(state);

        // Load all compiled cells
        for info in compilation.cells.values() {
            executor.load_cell(info.compiled.clone(), info.dep_count)?;
        }

        // Filter execution order if specific cell requested
        let execution_order = self.filter_execution_order(cell_filter)?;

        // Execute
        let exec_start = Instant::now();
        executor.execute_in_order(&execution_order, &self.deps)?;
        let execution_time = exec_start.elapsed();

        // Collect outputs
        let mut outputs = HashMap::new();
        for &cell_id in &execution_order {
            if let Some(output) = executor.state().get_output(cell_id) {
                outputs.insert(cell_id, output);
            }
        }

        Ok(ExecutionInfo {
            executed_cells: execution_order,
            execution_time,
            outputs,
        })
    }

    /// Filter execution order based on cell filter.
    fn filter_execution_order(&self, cell_filter: Option<&str>) -> anyhow::Result<Vec<CellId>> {
        if let Some(cell_name) = cell_filter {
            let target_id = self
                .cell_ids
                .get(cell_name)
                .ok_or_else(|| anyhow::anyhow!("Cell '{}' not found", cell_name))?;

            Ok(self
                .order
                .iter()
                .copied()
                .filter(|&id| {
                    id == *target_id || is_transitive_dependency(id, *target_id, &self.deps)
                })
                .collect())
        } else {
            Ok(self.order.clone())
        }
    }

    /// Get cell info by ID.
    pub fn cell_by_id(&self, cell_id: CellId) -> Option<&CellInfo> {
        self.cells
            .iter()
            .find(|c| self.cell_ids[&c.name] == cell_id)
    }

    /// Print a setup step.
    fn print_step(name: &str) {
        print!("{}{}{} ... ", colors::BLUE, name, colors::RESET);
        colors::flush_stdout();
    }

    /// Print success for a step.
    fn print_success(extra: Option<&str>) {
        match extra {
            Some(s) => println!("{}{} ({})", colors::GREEN, colors::RESET, s),
            None => println!("{}{}", colors::GREEN, colors::RESET),
        }
    }

    /// Print the header for a run.
    pub fn print_header(&self, action: &str) {
        println!(
            "\n{}Venus{} - {} {}{}{}",
            colors::BOLD,
            colors::RESET,
            action,
            colors::CYAN,
            self.notebook_name(),
            colors::RESET
        );
        println!("{}", "".repeat(50));
    }
}

/// Check if `dep_id` is a transitive dependency of `target_id`.
pub fn is_transitive_dependency(
    dep_id: CellId,
    target_id: CellId,
    deps: &HashMap<CellId, Vec<CellId>>,
) -> bool {
    let mut visited = std::collections::HashSet::new();
    let mut stack = vec![target_id];

    while let Some(current) = stack.pop() {
        if !visited.insert(current) {
            continue;
        }

        if let Some(current_deps) = deps.get(&current) {
            if current_deps.contains(&dep_id) {
                return true;
            }
            stack.extend(current_deps.iter().copied());
        }
    }

    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_progress_callback_creation() {
        let callback = ProgressCallback::new();
        assert!(!callback.verbose);

        let verbose = ProgressCallback::verbose();
        assert!(verbose.verbose);
    }
}