lemurclaw 0.0.1

Command-line interface for the lemurclaw AI coding agent
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
use std::time::Duration;
use std::time::Instant;

// Environment filter data models for the TUI
#[derive(Clone, Debug, Default)]
pub struct EnvironmentRow {
    pub id: String,
    pub label: Option<String>,
    pub is_pinned: bool,
    pub repo_hints: Option<String>, // e.g., "openai/codex"
}

#[derive(Clone, Debug, Default)]
pub struct EnvModalState {
    pub query: String,
    pub selected: usize,
}

#[derive(Clone, Debug, Default)]
pub struct BestOfModalState {
    pub selected: usize,
}

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ApplyResultLevel {
    Success,
    Partial,
    Error,
}

#[derive(Clone, Debug)]
pub struct ApplyModalState {
    pub task_id: TaskId,
    pub title: String,
    pub result_message: Option<String>,
    pub result_level: Option<ApplyResultLevel>,
    pub skipped_paths: Vec<String>,
    pub conflict_paths: Vec<String>,
    pub diff_override: Option<String>,
}

use crate::cloud_tasks::scrollable_diff::ScrollableDiff;
use lemurclaw_server::cloud_tasks_client::CloudBackend;
use lemurclaw_server::cloud_tasks_client::TaskId;
use lemurclaw_server::cloud_tasks_client::TaskSummary;
#[derive(Default)]
pub struct App {
    pub tasks: Vec<TaskSummary>,
    pub selected: usize,
    pub status: String,
    pub diff_overlay: Option<DiffOverlay>,
    pub spinner_start: Option<Instant>,
    pub refresh_inflight: bool,
    pub details_inflight: bool,
    // Environment filter state
    pub env_filter: Option<String>,
    pub env_modal: Option<EnvModalState>,
    pub apply_modal: Option<ApplyModalState>,
    pub best_of_modal: Option<BestOfModalState>,
    pub environments: Vec<EnvironmentRow>,
    pub env_last_loaded: Option<std::time::Instant>,
    pub env_loading: bool,
    pub env_error: Option<String>,
    // New Task page
    pub new_task: Option<crate::cloud_tasks::new_task::NewTaskPage>,
    pub best_of_n: usize,
    // Apply preflight spinner state
    pub apply_preflight_inflight: bool,
    // Apply action spinner state
    pub apply_inflight: bool,
    // Background enrichment coordination
    pub list_generation: u64,
    pub in_flight: std::collections::HashSet<String>,
    // Background enrichment caches were planned; currently unused.
}

impl App {
    pub fn new() -> Self {
        Self {
            tasks: Vec::new(),
            selected: 0,
            status: "Press r to refresh".to_string(),
            diff_overlay: None,
            spinner_start: None,
            refresh_inflight: false,
            details_inflight: false,
            env_filter: None,
            env_modal: None,
            apply_modal: None,
            best_of_modal: None,
            environments: Vec::new(),
            env_last_loaded: None,
            env_loading: false,
            env_error: None,
            new_task: None,
            best_of_n: 1,
            apply_preflight_inflight: false,
            apply_inflight: false,
            list_generation: 0,
            in_flight: std::collections::HashSet::new(),
        }
    }

    pub fn next(&mut self) {
        if self.tasks.is_empty() {
            return;
        }
        self.selected = (self.selected + 1).min(self.tasks.len().saturating_sub(1));
    }

    pub fn prev(&mut self) {
        if self.tasks.is_empty() {
            return;
        }
        if self.selected > 0 {
            self.selected -= 1;
        }
    }
}

pub async fn load_tasks(
    backend: &dyn CloudBackend,
    env: Option<&str>,
) -> anyhow::Result<Vec<TaskSummary>> {
    // In later milestones, add a small debounce, spinner, and error display.
    let tasks = tokio::time::timeout(
        Duration::from_secs(5),
        backend.list_tasks(env, Some(20), /*cursor*/ None),
    )
    .await??;
    // Hide review-only tasks from the main list.
    let filtered: Vec<TaskSummary> = tasks.tasks.into_iter().filter(|t| !t.is_review).collect();
    Ok(filtered)
}

pub struct DiffOverlay {
    pub title: String,
    pub task_id: TaskId,
    pub sd: ScrollableDiff,
    pub base_can_apply: bool,
    pub diff_lines: Vec<String>,
    pub text_lines: Vec<String>,
    pub prompt: Option<String>,
    pub attempts: Vec<AttemptView>,
    pub selected_attempt: usize,
    pub current_view: DetailView,
    pub base_turn_id: Option<String>,
    pub sibling_turn_ids: Vec<String>,
    pub attempt_total_hint: Option<usize>,
}

#[derive(Clone, Debug, Default)]
pub struct AttemptView {
    pub turn_id: Option<String>,
    pub status: lemurclaw_server::cloud_tasks_client::AttemptStatus,
    pub attempt_placement: Option<i64>,
    pub diff_lines: Vec<String>,
    pub text_lines: Vec<String>,
    pub prompt: Option<String>,
    pub diff_raw: Option<String>,
}

impl AttemptView {
    pub fn has_diff(&self) -> bool {
        !self.diff_lines.is_empty()
    }

    pub fn has_text(&self) -> bool {
        !self.text_lines.is_empty() || self.prompt.is_some()
    }
}

impl DiffOverlay {
    pub fn new(task_id: TaskId, title: String, attempt_total_hint: Option<usize>) -> Self {
        let mut sd = ScrollableDiff::new();
        sd.set_content(Vec::new());
        Self {
            title,
            task_id,
            sd,
            base_can_apply: false,
            diff_lines: Vec::new(),
            text_lines: Vec::new(),
            prompt: None,
            attempts: vec![AttemptView::default()],
            selected_attempt: 0,
            current_view: DetailView::Prompt,
            base_turn_id: None,
            sibling_turn_ids: Vec::new(),
            attempt_total_hint,
        }
    }

    pub fn current_attempt(&self) -> Option<&AttemptView> {
        self.attempts.get(self.selected_attempt)
    }

    pub fn base_attempt_mut(&mut self) -> &mut AttemptView {
        if self.attempts.is_empty() {
            self.attempts.push(AttemptView::default());
        }
        &mut self.attempts[0]
    }

    pub fn set_view(&mut self, view: DetailView) {
        self.current_view = view;
        self.apply_selection_to_fields();
    }

    pub fn expected_attempts(&self) -> Option<usize> {
        self.attempt_total_hint.or({
            if self.attempts.is_empty() {
                None
            } else {
                Some(self.attempts.len())
            }
        })
    }

    pub fn attempt_count(&self) -> usize {
        self.attempts.len()
    }

    pub fn attempt_display_total(&self) -> usize {
        self.expected_attempts()
            .unwrap_or_else(|| self.attempts.len().max(1))
    }

    pub fn step_attempt(&mut self, delta: isize) -> bool {
        let total = self.attempts.len();
        if total <= 1 {
            return false;
        }
        let total_isize = total as isize;
        let current = self.selected_attempt as isize;
        let mut next = current + delta;
        next = ((next % total_isize) + total_isize) % total_isize;
        let next = next as usize;
        self.selected_attempt = next;
        self.apply_selection_to_fields();
        true
    }

    pub fn current_can_apply(&self) -> bool {
        matches!(self.current_view, DetailView::Diff)
            && self
                .current_attempt()
                .and_then(|attempt| attempt.diff_raw.as_ref())
                .map(|diff| !diff.is_empty())
                .unwrap_or(false)
    }

    pub fn apply_selection_to_fields(&mut self) {
        let (diff_lines, text_lines, prompt) = if let Some(attempt) = self.current_attempt() {
            (
                attempt.diff_lines.clone(),
                attempt.text_lines.clone(),
                attempt.prompt.clone(),
            )
        } else {
            self.diff_lines.clear();
            self.text_lines.clear();
            self.prompt = None;
            self.sd.set_content(vec!["<loading attempt>".to_string()]);
            return;
        };

        self.diff_lines = diff_lines.clone();
        self.text_lines = text_lines.clone();
        self.prompt = prompt;

        match self.current_view {
            DetailView::Diff => {
                if diff_lines.is_empty() {
                    self.sd.set_content(vec!["<no diff available>".to_string()]);
                } else {
                    self.sd.set_content(diff_lines);
                }
            }
            DetailView::Prompt => {
                if text_lines.is_empty() {
                    self.sd.set_content(vec!["<no output>".to_string()]);
                } else {
                    self.sd.set_content(text_lines);
                }
            }
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DetailView {
    Diff,
    Prompt,
}

/// Internal app events delivered from background tasks.
/// These let the UI event loop remain responsive and keep the spinner animating.
#[derive(Debug)]
pub enum AppEvent {
    TasksLoaded {
        env: Option<String>,
        result: anyhow::Result<Vec<TaskSummary>>,
    },
    // Background diff summary events were planned; removed for now to keep code minimal.
    /// Autodetection of a likely environment id finished
    EnvironmentAutodetected(anyhow::Result<crate::cloud_tasks::env_detect::AutodetectSelection>),
    /// Background completion of environment list fetch
    EnvironmentsLoaded(anyhow::Result<Vec<EnvironmentRow>>),
    DetailsDiffLoaded {
        id: TaskId,
        title: String,
        diff: String,
    },
    DetailsMessagesLoaded {
        id: TaskId,
        title: String,
        messages: Vec<String>,
        prompt: Option<String>,
        turn_id: Option<String>,
        sibling_turn_ids: Vec<String>,
        attempt_placement: Option<i64>,
        attempt_status: lemurclaw_server::cloud_tasks_client::AttemptStatus,
    },
    DetailsFailed {
        id: TaskId,
        title: String,
        error: String,
    },
    AttemptsLoaded {
        id: TaskId,
        attempts: Vec<lemurclaw_server::cloud_tasks_client::TurnAttempt>,
    },
    /// Background completion of new task submission
    NewTaskSubmitted(Result<lemurclaw_server::cloud_tasks_client::CreatedTask, String>),
    /// Background completion of apply preflight when opening modal or on demand
    ApplyPreflightFinished {
        id: TaskId,
        title: String,
        message: String,
        level: ApplyResultLevel,
        skipped: Vec<String>,
        conflicts: Vec<String>,
    },
    /// Background completion of apply action (actual patch application)
    ApplyFinished {
        id: TaskId,
        result: std::result::Result<lemurclaw_server::cloud_tasks_client::ApplyOutcome, String>,
    },
}

// Convenience aliases; currently unused.
#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use lemurclaw_server::cloud_tasks_client::CloudBackendFuture;
    use lemurclaw_server::cloud_tasks_client::CloudTaskError;

    struct FakeBackend {
        // maps env key to titles
        by_env: std::collections::HashMap<Option<String>, Vec<&'static str>>,
    }

    impl FakeBackend {
        async fn list_tasks(
            &self,
            env: Option<&str>,
            limit: Option<i64>,
            cursor: Option<&str>,
        ) -> Result<lemurclaw_server::cloud_tasks_client::TaskListPage, CloudTaskError> {
            let key = env.map(str::to_string);
            let titles = self
                .by_env
                .get(&key)
                .cloned()
                .unwrap_or_else(|| vec!["default-a", "default-b"]);
            let mut out = Vec::new();
            for (i, t) in titles.into_iter().enumerate() {
                out.push(TaskSummary {
                    id: TaskId(format!("T-{i}")),
                    title: t.to_string(),
                    status: lemurclaw_server::cloud_tasks_client::TaskStatus::Ready,
                    updated_at: Utc::now(),
                    environment_id: env.map(str::to_string),
                    environment_label: None,
                    summary: lemurclaw_server::cloud_tasks_client::DiffSummary::default(),
                    is_review: false,
                    attempt_total: Some(1),
                });
            }
            let max = limit.unwrap_or(i64::MAX);
            let max = max.min(20);
            let mut limited = Vec::new();
            for task in out {
                if (limited.len() as i64) >= max {
                    break;
                }
                limited.push(task);
            }
            Ok(lemurclaw_server::cloud_tasks_client::TaskListPage {
                tasks: limited,
                cursor: cursor.map(str::to_string),
            })
        }

        async fn get_task_summary(&self, id: TaskId) -> Result<TaskSummary, CloudTaskError> {
            self.list_tasks(/*env*/ None, /*limit*/ None, /*cursor*/ None)
                .await?
                .tasks
                .into_iter()
                .find(|t| t.id == id)
                .ok_or_else(|| CloudTaskError::Msg(format!("Task {} not found", id.0)))
        }

        async fn get_task_text(
            &self,
            _id: TaskId,
        ) -> Result<lemurclaw_server::cloud_tasks_client::TaskText, CloudTaskError> {
            Ok(lemurclaw_server::cloud_tasks_client::TaskText {
                prompt: Some("Example prompt".to_string()),
                messages: Vec::new(),
                turn_id: Some("fake-turn".to_string()),
                sibling_turn_ids: Vec::new(),
                attempt_placement: Some(0),
                attempt_status: lemurclaw_server::cloud_tasks_client::AttemptStatus::Completed,
            })
        }
    }

    impl lemurclaw_server::cloud_tasks_client::CloudBackend for FakeBackend {
        fn list_tasks<'a>(
            &'a self,
            env: Option<&'a str>,
            limit: Option<i64>,
            cursor: Option<&'a str>,
        ) -> CloudBackendFuture<'a, lemurclaw_server::cloud_tasks_client::TaskListPage> {
            Box::pin(FakeBackend::list_tasks(self, env, limit, cursor))
        }

        fn get_task_summary(&self, id: TaskId) -> CloudBackendFuture<'_, TaskSummary> {
            Box::pin(FakeBackend::get_task_summary(self, id))
        }

        fn get_task_diff(&self, _id: TaskId) -> CloudBackendFuture<'_, Option<String>> {
            Box::pin(async {
                Err(lemurclaw_server::cloud_tasks_client::CloudTaskError::Unimplemented(
                    "not used in test",
                ))
            })
        }

        fn get_task_messages(&self, _id: TaskId) -> CloudBackendFuture<'_, Vec<String>> {
            Box::pin(async { Ok(vec![]) })
        }

        fn get_task_text(
            &self,
            id: TaskId,
        ) -> CloudBackendFuture<'_, lemurclaw_server::cloud_tasks_client::TaskText> {
            Box::pin(FakeBackend::get_task_text(self, id))
        }

        fn list_sibling_attempts(
            &self,
            _task: TaskId,
            _turn_id: String,
        ) -> CloudBackendFuture<'_, Vec<lemurclaw_server::cloud_tasks_client::TurnAttempt>> {
            Box::pin(async { Ok(Vec::new()) })
        }

        fn apply_task(
            &self,
            _id: TaskId,
            _diff_override: Option<String>,
        ) -> CloudBackendFuture<'_, lemurclaw_server::cloud_tasks_client::ApplyOutcome> {
            Box::pin(async {
                Err(lemurclaw_server::cloud_tasks_client::CloudTaskError::Unimplemented(
                    "not used in test",
                ))
            })
        }

        fn apply_task_preflight(
            &self,
            _id: TaskId,
            _diff_override: Option<String>,
        ) -> CloudBackendFuture<'_, lemurclaw_server::cloud_tasks_client::ApplyOutcome> {
            Box::pin(async {
                Err(lemurclaw_server::cloud_tasks_client::CloudTaskError::Unimplemented(
                    "not used in test",
                ))
            })
        }

        fn create_task<'a>(
            &'a self,
            _env_id: &'a str,
            _prompt: &'a str,
            _git_ref: &'a str,
            _qa_mode: bool,
            _best_of_n: usize,
        ) -> CloudBackendFuture<'a, lemurclaw_server::cloud_tasks_client::CreatedTask> {
            Box::pin(async {
                Err(lemurclaw_server::cloud_tasks_client::CloudTaskError::Unimplemented(
                    "not used in test",
                ))
            })
        }
    }

    #[tokio::test]
    async fn load_tasks_uses_env_parameter() {
        // Arrange: env-specific task titles
        let mut by_env = std::collections::HashMap::new();
        by_env.insert(None, vec!["root-1", "root-2"]);
        by_env.insert(Some("env-A".to_string()), vec!["A-1"]);
        by_env.insert(Some("env-B".to_string()), vec!["B-1", "B-2", "B-3"]);
        let backend = FakeBackend { by_env };

        // Act + Assert
        let root = load_tasks(&backend, /*env*/ None).await.unwrap();
        assert_eq!(root.len(), 2);
        assert_eq!(root[0].title, "root-1");

        let a = load_tasks(&backend, Some("env-A")).await.unwrap();
        assert_eq!(a.len(), 1);
        assert_eq!(a[0].title, "A-1");

        let b = load_tasks(&backend, Some("env-B")).await.unwrap();
        assert_eq!(b.len(), 3);
        assert_eq!(b[2].title, "B-3");
    }
}