buildwithnexus 0.12.3

A hilariously fast agentic AI coding CLI — remote or local models, full TUI with live autocomplete, clean diffs, multimodal input, hooks, and checkpoints
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
// Background workflow system. Workflows run the CLI binary as a subprocess so
// they have their own context and don't write to the foreground TUI's stdout.
// The REPL calls tick() each turn to check due schedules and collect status.

use std::io::{BufRead, BufReader};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex, OnceLock};
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

#[derive(Clone, Debug, PartialEq)]
pub enum WorkflowStatus {
    Pending,
    Running,
    Done,
    Failed(String),
    Cancelled,
}

#[derive(Clone, Debug)]
pub enum WorkflowKind {
    Once,
    // Repeat with `interval_secs` between the end of one run and start of next.
    Loop { interval_secs: u64 },
    // One-shot, fires when wall clock >= fire_at_ms.
    Scheduled { fire_at_ms: u64 },
}

#[derive(Debug)]
pub struct Workflow {
    pub id: usize,
    pub task: String,
    pub kind: WorkflowKind,
    pub status: WorkflowStatus,
    pub output: Vec<String>,
    pub created_ms: u64,
    pub started_ms: Option<u64>,
    pub finished_ms: Option<u64>,
    pub iteration: u32,
    pub next_fire_ms: Option<u64>, // for Loop: when the next iteration fires
}

struct ActiveProcess {
    id: usize,
    child: Child,
    output_buf: Arc<Mutex<Vec<String>>>,
}

struct Manager {
    workflows: Vec<Workflow>,
    next_id: usize,
    active: Option<ActiveProcess>,
}

fn manager() -> &'static Mutex<Manager> {
    static M: OnceLock<Mutex<Manager>> = OnceLock::new();
    M.get_or_init(|| {
        Mutex::new(Manager {
            workflows: Vec::new(),
            next_id: 1,
            active: None,
        })
    })
}

// Human-readable label for a workflow kind.
pub fn kind_label(kind: &WorkflowKind) -> String {
    match kind {
        WorkflowKind::Once => "once".to_string(),
        WorkflowKind::Loop { interval_secs } => format!("loop {}s", interval_secs),
        WorkflowKind::Scheduled { fire_at_ms } => {
            let now = now_ms();
            if *fire_at_ms <= now {
                "scheduled (due)".to_string()
            } else {
                let secs = (*fire_at_ms - now) / 1000;
                format!("in {}s", secs)
            }
        }
    }
}

pub fn status_label(s: &WorkflowStatus) -> &'static str {
    match s {
        WorkflowStatus::Pending => "pending",
        WorkflowStatus::Running => "running",
        WorkflowStatus::Done => "done",
        WorkflowStatus::Failed(_) => "failed",
        WorkflowStatus::Cancelled => "cancelled",
    }
}

/// Add a workflow to the queue. Returns its ID.
pub fn enqueue(task: &str, kind: WorkflowKind) -> usize {
    let mut m = manager().lock().unwrap_or_else(|e| e.into_inner());
    let id = m.next_id;
    m.next_id += 1;
    let initial_status = match &kind {
        WorkflowKind::Scheduled { fire_at_ms } if *fire_at_ms > now_ms() => WorkflowStatus::Pending,
        WorkflowKind::Loop { .. } => WorkflowStatus::Pending,
        _ => WorkflowStatus::Pending,
    };
    m.workflows.push(Workflow {
        id,
        task: task.to_string(),
        kind,
        status: initial_status,
        output: Vec::new(),
        created_ms: now_ms(),
        started_ms: None,
        finished_ms: None,
        iteration: 0,
        next_fire_ms: None,
    });
    id
}

/// Cancel a workflow by ID. Kills the subprocess if running.
pub fn cancel(id: usize) -> bool {
    let mut m = manager().lock().unwrap_or_else(|e| e.into_inner());
    if m.active.as_ref().is_some_and(|a| a.id == id) {
        if let Some(mut a) = m.active.take() {
            let _ = a.child.kill();
            drop(a.child);
        }
    }
    if let Some(wf) = m.workflows.iter_mut().find(|w| w.id == id) {
        if matches!(wf.status, WorkflowStatus::Pending | WorkflowStatus::Running) {
            wf.status = WorkflowStatus::Cancelled;
            wf.finished_ms = Some(now_ms());
            return true;
        }
    }
    false
}

/// Returns a summary snapshot (task, kind, status, id) for the /workflows panel.
pub struct WorkflowSnapshot {
    pub id: usize,
    pub task: String,
    pub kind_str: String,
    pub status_str: String,
    pub iteration: u32,
    pub elapsed_secs: Option<u64>,
    pub output_lines: usize,
}

pub fn snapshots() -> Vec<WorkflowSnapshot> {
    let m = manager().lock().unwrap_or_else(|e| e.into_inner());
    let now = now_ms();
    m.workflows
        .iter()
        .map(|w| {
            let elapsed_secs = w.started_ms.map(|s| (now - s) / 1000);
            WorkflowSnapshot {
                id: w.id,
                task: w.task.chars().take(60).collect(),
                kind_str: kind_label(&w.kind),
                status_str: status_label(&w.status).to_string(),
                iteration: w.iteration,
                elapsed_secs,
                output_lines: w.output.len(),
            }
        })
        .collect()
}

/// Returns the buffered output for a workflow (for inspect panel).
pub fn output(id: usize) -> Vec<String> {
    let m = manager().lock().unwrap_or_else(|e| e.into_inner());
    m.workflows
        .iter()
        .find(|w| w.id == id)
        .map(|w| w.output.clone())
        .unwrap_or_default()
}

/// Clear completed/cancelled workflows older than the last N kept.
pub fn prune(keep: usize) {
    let mut m = manager().lock().unwrap_or_else(|e| e.into_inner());
    let done: Vec<usize> = m
        .workflows
        .iter()
        .enumerate()
        .filter(|(_, w)| {
            matches!(
                w.status,
                WorkflowStatus::Done | WorkflowStatus::Failed(_) | WorkflowStatus::Cancelled
            )
        })
        .map(|(i, _)| i)
        .collect();
    if done.len() > keep {
        let to_remove = &done[..done.len() - keep];
        // Remove in reverse order so indices stay valid.
        for &i in to_remove.iter().rev() {
            m.workflows.remove(i);
        }
    }
}

/// Called by the REPL each turn. Returns a notification message if a workflow
/// just completed, so the REPL can surface it above the next prompt.
pub fn tick() -> Option<String> {
    let mut m = manager().lock().unwrap_or_else(|e| e.into_inner());
    let now = now_ms();
    let mut notification = None;

    // Check if the currently active subprocess has finished.
    if let Some(a) = &mut m.active {
        let finished = a.child.try_wait().ok().and_then(|s| s);
        if let Some(exit) = finished {
            let id = a.id;
            let buf_lines = a
                .output_buf
                .lock()
                .ok()
                .map(|b| b.clone())
                .unwrap_or_default();
            let ok = exit.success();
            if let Some(a) = m.active.take() {
                drop(a.child);
            }

            if let Some(wf) = m.workflows.iter_mut().find(|w| w.id == id) {
                wf.output.extend(buf_lines);
                wf.finished_ms = Some(now);
                let label: String = wf.task.chars().take(40).collect();
                if ok {
                    wf.status = WorkflowStatus::Done;
                    notification = Some(format!("  ✓ workflow #{id} done: {label}"));
                    // For Loop kind: schedule next iteration.
                    if let WorkflowKind::Loop { interval_secs } = wf.kind.clone() {
                        wf.status = WorkflowStatus::Pending;
                        wf.next_fire_ms = Some(now + interval_secs * 1000);
                        wf.started_ms = None;
                        wf.finished_ms = None;
                    }
                } else {
                    wf.status = WorkflowStatus::Failed("non-zero exit".to_string());
                    notification = Some(format!("  ✗ workflow #{id} failed: {label}"));
                }
            }
        }
    }

    // If nothing is running, try to start the next due workflow.
    if m.active.is_none() {
        let due_idx = m.workflows.iter().position(|w| {
            if !matches!(w.status, WorkflowStatus::Pending) {
                return false;
            }
            match &w.kind {
                WorkflowKind::Scheduled { fire_at_ms } => *fire_at_ms <= now,
                WorkflowKind::Loop { .. } => w.next_fire_ms.map(|f| f <= now).unwrap_or(true),
                WorkflowKind::Once => true,
            }
        });

        if let Some(idx) = due_idx {
            if let Ok(bin) = std::env::current_exe() {
                let task = m.workflows[idx].task.clone();
                let id = m.workflows[idx].id;
                let buf: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
                let buf2 = buf.clone();
                let child_res = Command::new(&bin)
                    .args(["run", "--json", &task])
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .spawn();

                if let Ok(mut child) = child_res {
                    // Drain stdout line-by-line in a reader thread.
                    if let Some(stdout) = child.stdout.take() {
                        let b = buf2.clone();
                        thread::spawn(move || {
                            let reader = BufReader::new(stdout);
                            for l in reader.lines().map_while(Result::ok) {
                                if let Ok(mut bv) = b.lock() {
                                    bv.push(l);
                                }
                            }
                        });
                    }
                    // Drain stderr too so the child doesn't block.
                    if let Some(stderr) = child.stderr.take() {
                        let b = buf.clone();
                        thread::spawn(move || {
                            let reader = BufReader::new(stderr);
                            for l in reader.lines().map_while(Result::ok) {
                                if let Ok(mut bv) = b.lock() {
                                    bv.push(format!("[stderr] {l}"));
                                }
                            }
                        });
                    }
                    m.workflows[idx].status = WorkflowStatus::Running;
                    m.workflows[idx].started_ms = Some(now);
                    m.workflows[idx].iteration += 1;
                    m.active = Some(ActiveProcess {
                        id,
                        child,
                        output_buf: buf.clone(),
                    });
                } else {
                    m.workflows[idx].status = WorkflowStatus::Failed("failed to spawn".to_string());
                }
            }
        }
    }

    notification
}

/// How many workflows are currently pending or running.
pub fn active_count() -> usize {
    let m = manager().lock().unwrap_or_else(|e| e.into_inner());
    m.workflows
        .iter()
        .filter(|w| matches!(w.status, WorkflowStatus::Pending | WorkflowStatus::Running))
        .count()
}

/// Parse a delay string like "5s", "2m", "1h" → milliseconds from now.
/// Returns None if unparseable.
pub fn parse_delay(s: &str) -> Option<u64> {
    let s = s.trim();
    if let Some(n) = s.strip_suffix('s') {
        n.trim().parse::<u64>().ok().map(|v| now_ms() + v * 1000)
    } else if let Some(n) = s.strip_suffix('m') {
        n.trim()
            .parse::<u64>()
            .ok()
            .map(|v| now_ms() + v * 60 * 1000)
    } else if let Some(n) = s.strip_suffix('h') {
        n.trim()
            .parse::<u64>()
            .ok()
            .map(|v| now_ms() + v * 3600 * 1000)
    } else {
        // bare number interpreted as seconds
        s.parse::<u64>().ok().map(|v| now_ms() + v * 1000)
    }
}

/// Parse an interval string like "30s", "5m" → seconds.
pub fn parse_interval_secs(s: &str) -> Option<u64> {
    let s = s.trim();
    if let Some(n) = s.strip_suffix('s') {
        n.trim().parse::<u64>().ok()
    } else if let Some(n) = s.strip_suffix('m') {
        n.trim().parse::<u64>().ok().map(|v| v * 60)
    } else if let Some(n) = s.strip_suffix('h') {
        n.trim().parse::<u64>().ok().map(|v| v * 3600)
    } else {
        s.parse::<u64>().ok()
    }
}

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

    #[test]
    fn parse_delay_seconds() {
        let now = now_ms();
        let result = parse_delay("30s").unwrap();
        assert!(result >= now + 29_000 && result <= now + 31_000);
    }

    #[test]
    fn parse_delay_minutes() {
        let now = now_ms();
        let result = parse_delay("2m").unwrap();
        assert!(result >= now + 119_000 && result <= now + 121_000);
    }

    #[test]
    fn parse_delay_hours() {
        let now = now_ms();
        let result = parse_delay("1h").unwrap();
        assert!(result >= now + 3_599_000 && result <= now + 3_601_000);
    }

    #[test]
    fn parse_delay_bare_number_is_seconds() {
        let now = now_ms();
        let result = parse_delay("60").unwrap();
        assert!(result >= now + 59_000 && result <= now + 61_000);
    }

    #[test]
    fn parse_delay_invalid_returns_none() {
        assert!(parse_delay("abc").is_none());
        assert!(parse_delay("").is_none());
    }

    #[test]
    fn parse_interval_secs_works() {
        assert_eq!(parse_interval_secs("30s"), Some(30));
        assert_eq!(parse_interval_secs("5m"), Some(300));
        assert_eq!(parse_interval_secs("1h"), Some(3600));
        assert_eq!(parse_interval_secs("45"), Some(45));
        assert_eq!(parse_interval_secs("bad"), None);
    }

    #[test]
    fn kind_label_once() {
        assert_eq!(kind_label(&WorkflowKind::Once), "once");
    }

    #[test]
    fn kind_label_loop() {
        assert_eq!(
            kind_label(&WorkflowKind::Loop { interval_secs: 60 }),
            "loop 60s"
        );
    }
}