spectral-m2 0.3.0

A syntax-aware terminal shell for Macaulay2
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
//! One independently evaluated Macaulay2 workspace.

use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;

use m2_syn::PrettyNode;

use crate::backend::{captured_error, captured_output, M2Backend, M2Response};
use crate::code::highlight::Highlighter;
use crate::code::parser::{self, ParseVerdict};
use crate::errors::Result;
use crate::ui::block::{Block, OutputBlock, TextBlock, TypeLineBlock, WarningBlock};
use crate::ui::{submitted_command, EditorUpdate, SubmittedCommand};

/// One successfully evaluated input cell retained by its workspace.
#[derive(Clone)]
pub struct CommittedCell {
    pub number: u32,
    pub code: String,
    pub tree: PrettyNode,
}

/// A parsed cell waiting for its worker to evaluate it.
#[derive(Clone)]
struct SubmittedCell {
    code: String,
    tree: PrettyNode,
}

/// The ordered cells belonging to the workspace's active submission.
struct PendingSubmission {
    id: u64,
    cells: Vec<SubmittedCell>,
}

/// Work sent to the thread that exclusively owns an M2 process.
enum WorkerRequest {
    Evaluate {
        id: u64,
        first_number: u32,
        cells: Vec<String>,
    },
    Restart,
    Stop,
}

/// Incremental results returned by one workspace worker.
enum WorkerEvent {
    Cell {
        id: u64,
        index: usize,
        number: u32,
        result: std::result::Result<M2Response, String>,
    },
    Restarted(std::result::Result<String, String>),
}

/// A live M2 worker and the source state committed through it.
pub struct Session {
    requests: Sender<WorkerRequest>,
    events: Receiver<WorkerEvent>,
    version: String,
    in_debugger: bool,
    next_input_number: u32,
    next_submission_id: u64,
    committed: Vec<CommittedCell>,
    pending: Option<PendingSubmission>,
    restarting: bool,
}

impl Session {
    pub fn new(backend: M2Backend) -> Self {
        let version = backend.version().unwrap_or_else(|_| "unknown".to_string());
        let (request_tx, request_rx) = mpsc::channel();
        let (event_tx, event_rx) = mpsc::channel();
        thread::spawn(move || worker(backend, request_rx, event_tx));
        Self {
            requests: request_tx,
            events: event_rx,
            version,
            in_debugger: false,
            next_input_number: 1,
            next_submission_id: 0,
            committed: Vec::new(),
            pending: None,
            restarting: false,
        }
    }

    pub fn version(&self) -> Result<String> {
        Ok(self.version.clone())
    }

    pub fn next_input_number(&self) -> u32 {
        self.next_input_number
    }

    pub fn in_debugger(&self) -> bool {
        self.in_debugger
    }

    pub fn committed(&self) -> &[CommittedCell] {
        &self.committed
    }

    pub fn busy(&self) -> bool {
        self.pending.is_some() || self.restarting
    }

    pub fn restart(&mut self) -> EditorUpdate {
        if self.busy() {
            return EditorUpdate::Diagnostic("workspace is still evaluating".to_string());
        }
        if self.requests.send(WorkerRequest::Restart).is_err() {
            return EditorUpdate::Diagnostic("Macaulay2 worker stopped".to_string());
        }
        self.restarting = true;
        EditorUpdate::Ignore
    }

    pub fn submit(&mut self, source: &str, highlighter: &mut dyn Highlighter) -> EditorUpdate {
        if source.is_empty() {
            return EditorUpdate::Ignore;
        }
        if let Some(command) = submitted_command(source) {
            return match command {
                SubmittedCommand::Quit => EditorUpdate::Quit,
                SubmittedCommand::Restart => self.restart(),
            };
        }
        if self.busy() {
            return EditorUpdate::Diagnostic("workspace is still evaluating".to_string());
        }

        let source = source.trim();
        let parsed = match parser::check(source) {
            ParseVerdict::Incomplete => return EditorUpdate::ContinueSubmission,
            ParseVerdict::SyntaxError(diagnostic) => {
                return EditorUpdate::SyntaxDiagnostic {
                    message: diagnostic.message,
                    range: diagnostic.range,
                };
            }
            ParseVerdict::Complete(cells) if cells.is_empty() => return EditorUpdate::Ignore,
            ParseVerdict::Complete(cells) => cells,
        };
        let cells: Vec<_> = parsed
            .into_iter()
            .map(|cell| SubmittedCell {
                code: source[cell.range].to_string(),
                tree: cell.tree,
            })
            .collect();
        let displayed = cells
            .iter()
            .map(|cell| (cell.code.clone(), highlighter.highlight(&cell.code)))
            .collect();
        let id = self.next_submission_id;
        self.next_submission_id += 1;
        let request = WorkerRequest::Evaluate {
            id,
            first_number: self.next_input_number,
            cells: cells.iter().map(|cell| cell.code.clone()).collect(),
        };
        if self.requests.send(request).is_err() {
            return EditorUpdate::Diagnostic("Macaulay2 worker stopped".to_string());
        }
        self.pending = Some(PendingSubmission { id, cells });
        EditorUpdate::Pending {
            id,
            cells: displayed,
        }
    }

    pub fn poll(&mut self) -> Vec<EditorUpdate> {
        let mut updates = Vec::new();
        while let Ok(event) = self.events.try_recv() {
            match event {
                WorkerEvent::Cell {
                    id,
                    index,
                    number,
                    result,
                } => updates.push(self.resolve_cell(id, index, number, result)),
                WorkerEvent::Restarted(result) => updates.push(self.resolve_restart(result)),
            }
        }
        updates
    }

    fn resolve_cell(
        &mut self,
        id: u64,
        index: usize,
        number: u32,
        result: std::result::Result<M2Response, String>,
    ) -> EditorUpdate {
        let Some(cell) = self
            .pending
            .as_ref()
            .filter(|pending| pending.id == id)
            .and_then(|pending| pending.cells.get(index))
            .cloned()
        else {
            return EditorUpdate::Ignore;
        };
        match result {
            Ok(response) if !response.is_error => {
                self.in_debugger = response.depth > 1;
                self.next_input_number = number + 1;
                self.committed.push(CommittedCell {
                    number,
                    code: cell.code.clone(),
                    tree: cell.tree,
                });
                let finished = self
                    .pending
                    .as_ref()
                    .is_some_and(|pending| index + 1 == pending.cells.len());
                if finished {
                    self.pending = None;
                }
                EditorUpdate::CellSucceeded {
                    id,
                    index,
                    number,
                    outputs: response_blocks(&response, &cell.code, number),
                }
            }
            Ok(response) => {
                self.in_debugger = response.depth > 1;
                self.pending = None;
                EditorUpdate::CellFailed {
                    id,
                    index,
                    number,
                    message: captured_error(&response.transcript, &cell.code),
                }
            }
            Err(message) => {
                self.pending = None;
                EditorUpdate::CellFailed {
                    id,
                    index,
                    number,
                    message: format!("Communication error: {message}"),
                }
            }
        }
    }

    fn resolve_restart(&mut self, result: std::result::Result<String, String>) -> EditorUpdate {
        self.restarting = false;
        match result {
            Ok(version) => {
                self.version = version;
                self.in_debugger = false;
                self.next_input_number = 1;
                self.committed.clear();
                EditorUpdate::Commit(vec![Box::new(TextBlock::system(
                    "Macaulay2 restarted -- the session is empty and numbering begins again",
                ))])
            }
            Err(error) => EditorUpdate::Diagnostic(format!("could not restart M2: {error}")),
        }
    }
}

impl Drop for Session {
    fn drop(&mut self) {
        let _ = self.requests.send(WorkerRequest::Stop);
    }
}

fn worker(mut backend: M2Backend, requests: Receiver<WorkerRequest>, events: Sender<WorkerEvent>) {
    while let Ok(request) = requests.recv() {
        match request {
            WorkerRequest::Evaluate {
                id,
                first_number,
                cells,
            } => {
                for (index, code) in cells.iter().enumerate() {
                    let number = first_number + index as u32;
                    let result = backend
                        .evaluate(code, Some(number))
                        .map_err(|error| error.to_string());
                    let stop = result.as_ref().map_or(true, |response| response.is_error);
                    if events
                        .send(WorkerEvent::Cell {
                            id,
                            index,
                            number,
                            result,
                        })
                        .is_err()
                    {
                        return;
                    }
                    if stop {
                        break;
                    }
                }
            }
            WorkerRequest::Restart => {
                let result = M2Backend::spawn().and_then(|replacement| {
                    let version = replacement.version()?;
                    backend = replacement;
                    Ok(version)
                });
                if events
                    .send(WorkerEvent::Restarted(
                        result.map_err(|error| error.to_string()),
                    ))
                    .is_err()
                {
                    return;
                }
            }
            WorkerRequest::Stop => return,
        }
    }
}

fn response_blocks(response: &M2Response, code: &str, number: u32) -> Vec<Box<dyn Block>> {
    let output = captured_output(
        &response.transcript,
        code,
        response.value.as_deref(),
        response.type_info.as_deref(),
    );
    let mut blocks: Vec<Box<dyn Block>> = output
        .warnings
        .into_iter()
        .map(|text| Box::new(WarningBlock { text }) as Box<dyn Block>)
        .collect();
    if !output.printed.is_empty() {
        blocks.push(Box::new(TextBlock::printed(output.printed)));
    }
    match (output.value, response.has_value) {
        (Some(value), bound) => blocks.push(Box::new(OutputBlock {
            number: bound.then_some(number),
            text: value.text,
            ty: value.type_info,
        })),
        (None, true) => blocks.extend(
            response
                .type_info
                .clone()
                .map(|ty| Box::new(TypeLineBlock { number, ty }) as Box<dyn Block>),
        ),
        (None, false) => {}
    }
    blocks
}

#[cfg(test)]
mod tests {
    use std::time::{Duration, Instant};

    use super::*;
    use crate::code::highlight::SyntaxHighlighter;

    fn updates_until_idle(session: &mut Session) -> Vec<EditorUpdate> {
        let deadline = Instant::now() + Duration::from_secs(5);
        let mut updates = Vec::new();
        while session.busy() && Instant::now() < deadline {
            updates.extend(session.poll());
            thread::sleep(Duration::from_millis(5));
        }
        updates.extend(session.poll());
        assert!(!session.busy(), "evaluation timed out");
        updates
    }

    #[test]
    fn submission_resolves_incrementally_and_preserves_numbering() {
        let backend = M2Backend::spawn().expect("spawn M2");
        let mut session = Session::new(backend);
        let mut highlighter = SyntaxHighlighter::new();

        assert!(matches!(
            session.submit("1 + 1\n2 + 2", &mut highlighter),
            EditorUpdate::Pending { .. }
        ));
        let updates = updates_until_idle(&mut session);
        assert_eq!(
            updates
                .iter()
                .filter(|update| matches!(update, EditorUpdate::CellSucceeded { .. }))
                .count(),
            2
        );
        assert_eq!(session.next_input_number(), 3);
        assert_eq!(
            session
                .committed()
                .iter()
                .map(|cell| cell.code.as_str())
                .collect::<Vec<_>>(),
            ["1 + 1", "2 + 2"]
        );
    }

    #[test]
    fn a_runtime_failure_reuses_its_number() {
        let backend = M2Backend::spawn().expect("spawn M2");
        let mut session = Session::new(backend);
        let mut highlighter = SyntaxHighlighter::new();

        session.submit("1/0\n2 + 2", &mut highlighter);
        let updates = updates_until_idle(&mut session);
        assert!(updates
            .iter()
            .any(|update| matches!(update, EditorUpdate::CellFailed { index: 0, .. })));
        assert_eq!(session.next_input_number(), 1);
        assert!(session.committed().is_empty());

        session.submit("2 + 2", &mut highlighter);
        updates_until_idle(&mut session);
        assert_eq!(session.committed()[0].number, 1);
    }
}