oxicode-cli 0.77.0

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
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
//! State, rendering, and input handling for the `/issue` TUI panel.
//! See docs/superpowers/specs/2026-08-27-tui-issues-panel-design.md.

use std::path::Path;

use oxicode_sdk::{FileIssueStore, IssueFilter, IssuePatch, Priority, Status, liveness};
mod filter_parse;
// Consumed by `input::FilterInput` Enter handler in Task 6.
pub(crate) use filter_parse::parse_issue_filter;
mod store_handle;

pub(crate) use store_handle::get_or_open_store;

mod form;
// Task 8: `cycle_priority` is wired into the `Left/Right` cycling on the
// Priority field; `parse_labels` translates the comma-separated `labels_input`
// String into `Vec<String>` at submit time.
pub(crate) use form::{cycle_priority, parse_labels};

mod input;
mod render;

pub(crate) use input::handle_issues_panel_key;
pub(crate) use render::render_issues_panel;

// `status_filter`: `Some(Status::Open)` is the default view (open issues
// only); `None` = All (no status constraint). The `f` key in the panel
// toggles between the two. We hand-write `Default` so the derived form's
// `Option::default()` (= `None` = All) does NOT shadow the intended
// Open-on-first-open behavior.
#[derive(Debug)]
pub(crate) struct IssuesPanelState {
    pub mode: IssuesPanelMode,
    pub status_filter: Option<Status>,
    pub extra_filter: IssueFilter,
    pub rows: Vec<IssueRow>,
    pub selected: usize,
    pub pending: bool,
    pub error: Option<String>,
    /// Body text for the current Detail view, populated synchronously via
    /// `FileIssueStore::read(id)` on List→Detail transition. `None` while
    /// the read hasn't happened yet (or failed) — render layer shows a
    /// "(loading…)" placeholder in that case.
    pub detail_body_cache: Option<String>,
}

impl Default for IssuesPanelState {
    fn default() -> Self {
        Self {
            mode: IssuesPanelMode::default(),
            status_filter: Some(Status::Open),
            extra_filter: IssueFilter::default(),
            rows: Vec::new(),
            selected: 0,
            pending: false,
            error: None,
            detail_body_cache: None,
        }
    }
}

#[derive(Debug, Default)]
pub(crate) enum IssuesPanelMode {
    #[default]
    List,
    Detail {
        id: u32,
        scroll: usize,
    },
    Form(Box<IssueFormState>),
    FilterInput(String),
}

#[derive(Clone, Debug)]
pub(crate) struct IssueRow {
    pub id: u32,
    pub title: String,
    pub status: Status,
    pub priority: Priority,
    pub labels: Vec<String>,
    pub assignee_badge: Option<AssigneeBadge>,
    /// Snapshot of `IssueMeta` timestamps at `refresh()` time (design §5
    /// Detail meta header). Carried read-only; never written back.
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub updated_at: chrono::DateTime<chrono::Utc>,
    pub closed_at: Option<chrono::DateTime<chrono::Utc>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum AssigneeBadge {
    Live(String),
    Stale(String),
}

#[derive(Debug)]
pub(crate) struct IssueFormState {
    pub editing_id: Option<u32>,
    pub content_hash: Option<String>,
    pub title: String,
    pub priority: Priority,
    pub labels_input: String,
    pub body: oxicode_textarea::TextArea,
    pub focus: FormField,
}

impl Default for IssueFormState {
    fn default() -> Self {
        Self {
            editing_id: None,
            content_hash: None,
            title: String::new(),
            priority: Priority::default(),
            labels_input: String::new(),
            body: oxicode_textarea::TextArea::new(),
            focus: FormField::Title,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub(crate) enum FormField {
    #[default]
    Title,
    Priority,
    Labels,
    Body,
}

impl IssuesPanelState {
    pub fn refresh(&mut self, store: &FileIssueStore, issues_dir: &Path) {
        let filter = self.effective_filter();
        let issues = match store.list(&filter) {
            Ok(v) => v,
            Err(e) => {
                self.error = Some(e.to_string());
                self.rows.clear();
                return;
            }
        };
        self.rows = issues
            .into_iter()
            .map(|issue| IssueRow {
                id: issue.meta.id,
                title: issue.meta.title,
                status: issue.meta.status,
                priority: issue.meta.priority,
                labels: issue.meta.labels,
                assignee_badge: issue.meta.assigned_to.map(|a| {
                    if liveness::is_session_alive(issues_dir, &a.session) {
                        AssigneeBadge::Live(a.session)
                    } else {
                        AssigneeBadge::Stale(a.session)
                    }
                }),
                created_at: issue.meta.created_at,
                updated_at: issue.meta.updated_at,
                closed_at: issue.meta.closed_at,
            })
            .collect();
        self.selected = self.selected.min(self.rows.len().saturating_sub(1));
    }

    /// Union of the status toggle and the `/` filter-modal fields (design §4).
    /// `None` in `status_filter` flows through as `None` here, matching the
    /// design's "All" view (no status constraint).
    fn effective_filter(&self) -> oxicode_sdk::IssueFilter {
        oxicode_sdk::IssueFilter {
            status: self.status_filter,
            ..self.extra_filter.clone()
        }
    }
}

/// Requests the panel's synchronous key-handling code cannot satisfy itself
/// (CAS-guarded async store writes). Sent over a dedicated channel into
/// `run_event_loop`'s `select!` — kept out of `oxicode_vtui::InlineEvent` so
/// the framework crate stays free of `oxicode-*` dependencies.
#[derive(Clone, Debug)]
pub(crate) enum IssueActionRequest {
    Close {
        id: u32,
        caller: String,
        hash: Option<String>,
    },
    Reopen {
        id: u32,
        hash: Option<String>,
    },
    ApplyPatch {
        id: u32,
        patch: IssuePatch,
        caller: Option<String>,
        hash: Option<String>,
    },
}

/// Receive an action from the panel's dedicated mpsc channel and resolve it
/// with a real CAS-guarded store write.
///
/// Runs the mutation on a spawned task so the event loop never blocks on
/// filesystem I/O. The `parking_lot` guard is dropped before the `.await`
/// and re-acquired after completion (AGENTS.md pitfall: never hold it across
/// an await). On completion the panel's `pending` flag clears, the outcome
/// lands in `panel.error`, and the row list refreshes regardless — a failed
/// write may still reflect a concurrent change made by another session.
pub(crate) fn dispatch_action(
    req: IssueActionRequest,
    state: std::sync::Arc<parking_lot::Mutex<crate::tui_vt::main_loop::RenderState>>,
) {
    let store = { state.lock().issue_store.clone() };
    let Some(store) = store else {
        let mut s = state.lock();
        if let Some(panel) = s.issues_panel.as_mut() {
            panel.pending = false;
            panel.error = Some("issue store not initialized".into());
        }
        return;
    };
    tokio::spawn(async move {
        let result = match req {
            IssueActionRequest::Close { id, caller, hash } => {
                // Mirror the CLI's `oxicode issue close` flow
                // (`oxicode-cli/src/cli/commands/issue.rs:84-103`):
                //   1. `start` claims ownership (fails `Assigned` for a
                //      live OTHER session — surfaced, not closed).
                //   2. Re-read for a fresh hash.
                //   3. `close` requires the assignee — now satisfied.
                // The whole dance lives inside `cas_retry`'s closure so a
                // `Conflict` from any step re-runs the sequence; `start`
                // is idempotent for the same caller, so a retry after a
                // close-side conflict is safe.
                oxicode_sdk::cas_retry(&store, id, hash, |h| {
                    let store = store.clone();
                    let caller = caller.clone();
                    async move {
                        // `h: Option<String>` matches `store.start`'s
                        // `expected_hash: Option<String>` directly — do
                        // NOT wrap in `Some()`.
                        store.start(id, &caller, h).await?;
                        let (_, fresh_hash) = store.read(id)?;
                        store.close(id, &caller, Some(fresh_hash)).await
                    }
                })
                .await
            }
            IssueActionRequest::Reopen { id, hash } => {
                oxicode_sdk::cas_retry(&store, id, hash, |h| {
                    let store = store.clone();
                    async move { store.reopen(id, h).await }
                })
                .await
            }
            IssueActionRequest::ApplyPatch {
                id,
                patch,
                caller,
                hash,
            } => {
                oxicode_sdk::cas_retry(&store, id, hash, |h| {
                    let store = store.clone();
                    let patch = patch.clone();
                    let caller = caller.clone();
                    async move { store.apply_patch(id, patch, caller, h).await }
                })
                .await
            }
        };

        let mut s = state.lock();
        if let Some(panel) = s.issues_panel.as_mut() {
            panel.pending = false;
            match result {
                Ok(_) => panel.error = None,
                Err(e) => panel.error = Some(e.to_string()),
            }
        }
        // Refresh regardless of outcome — a failed write may still reflect
        // a concurrent change made by another session.
        let store2 = s.issue_store.clone();
        if let (Some(store2), Some(panel)) = (store2, s.issues_panel.as_mut()) {
            panel.refresh(&store2, &store2.issues_dir());
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn default_state_opens_in_list_mode_with_open_filter() {
        let state = IssuesPanelState::default();
        assert!(matches!(state.mode, IssuesPanelMode::List));
        assert_eq!(state.status_filter, Some(Status::Open));
        assert!(!state.pending);
        assert!(state.error.is_none());
        assert!(state.rows.is_empty());
    }
}

#[cfg(test)]
mod refresh_tests {
    use super::*;
    use oxicode_sdk::{FileIssueStore, Priority};

    fn tmp_store() -> (tempfile::TempDir, FileIssueStore) {
        let tmp = tempfile::tempdir().unwrap();
        let store = FileIssueStore::open(tmp.path().to_path_buf()).unwrap();
        (tmp, store)
    }

    #[test]
    fn refresh_populates_rows_sorted_by_recency() {
        let (tmp, store) = tmp_store();
        store
            .create("first".into(), "body".into(), Priority::Low, vec![], None)
            .unwrap();
        store
            .create("second".into(), "body".into(), Priority::High, vec![], None)
            .unwrap();

        let mut panel = IssuesPanelState::default();
        panel.refresh(&store, tmp.path());

        assert_eq!(panel.rows.len(), 2);
        // FileIssueStore::list sorts by updated_at desc — "second" was
        // created after "first" so it sorts first.
        assert_eq!(panel.rows[0].title, "second");
        assert_eq!(panel.rows[1].title, "first");
    }

    #[test]
    fn refresh_marks_unassigned_issues_with_no_badge() {
        let (tmp, store) = tmp_store();
        store
            .create("solo".into(), "body".into(), Priority::Medium, vec![], None)
            .unwrap();
        let mut panel = IssuesPanelState::default();
        panel.refresh(&store, tmp.path());
        assert!(panel.rows[0].assignee_badge.is_none());
    }
}

/// Task 9: real dispatch (`IssueActionRequest` → CAS-guarded store writes).
/// Both requests below carry the SAME (stale-after-the-first-write) hash so
/// the second one must flow through `cas_retry`'s re-read-and-retry path.
#[cfg(test)]
mod dispatch_tests {
    use super::*;
    use oxicode_sdk::{FileIssueStore, IssuePatch, Priority};
    use std::sync::Arc;

    #[tokio::test]
    async fn concurrent_apply_patch_via_dispatch_action_both_eventually_succeed() {
        let tmp = tempfile::tempdir().unwrap();
        let store = Arc::new(FileIssueStore::open(tmp.path().to_path_buf()).unwrap());
        let issue = store
            .create("t".into(), "b".into(), Priority::Low, vec![], None)
            .unwrap();
        let (_issue, hash) = store.read(issue.meta.id).unwrap();

        let state = Arc::new(parking_lot::Mutex::new(
            crate::tui_vt::main_loop::RenderState {
                issue_store: Some(store.clone()),
                issues_panel: Some(IssuesPanelState::default()),
                ..Default::default()
            },
        ));

        // Both requests carry the SAME (now-stale-after-the-first-write) hash,
        // forcing the second one through cas_retry's re-read-and-retry path.
        dispatch_action(
            IssueActionRequest::ApplyPatch {
                id: issue.meta.id,
                patch: IssuePatch {
                    title: Some("first".into()),
                    ..Default::default()
                },
                caller: None,
                hash: Some(hash.clone()),
            },
            state.clone(),
        );
        dispatch_action(
            IssueActionRequest::ApplyPatch {
                id: issue.meta.id,
                patch: IssuePatch {
                    priority: Some(Priority::High),
                    ..Default::default()
                },
                caller: None,
                hash: Some(hash),
            },
            state.clone(),
        );

        // Give both spawned tasks a chance to run.
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        let (final_issue, _) = store.read(issue.meta.id).unwrap();
        assert_eq!(final_issue.meta.title, "first");
        assert_eq!(final_issue.meta.priority, Priority::High);
        let s = state.lock();
        assert!(s.issues_panel.as_ref().unwrap().error.is_none());
    }

    /// F1 regression: closing an UNASSIGNED issue via the panel's
    /// `IssueActionRequest::Close` path used to fail with `NotAssigned`
    /// because `store.close` requires the caller to hold the assignment.
    /// The fixed dispatch does `start` → re-read → `close` (mirroring
    /// the CLI), so an unassigned issue is now claimed-then-closed in
    /// one CAS-guarded sequence.
    #[tokio::test]
    async fn dispatch_action_close_unassigned_issue_claims_then_closes() {
        let tmp = tempfile::tempdir().unwrap();
        let store = Arc::new(FileIssueStore::open(tmp.path().to_path_buf()).unwrap());
        let issue = store
            .create("t".into(), "b".into(), Priority::Low, vec![], None)
            .unwrap();
        // Sanity: the issue is unassigned at creation.
        assert!(issue.meta.assigned_to.is_none());

        let state = Arc::new(parking_lot::Mutex::new(
            crate::tui_vt::main_loop::RenderState {
                issue_store: Some(store.clone()),
                issues_panel: Some(IssuesPanelState::default()),
                ..Default::default()
            },
        ));

        dispatch_action(
            IssueActionRequest::Close {
                id: issue.meta.id,
                caller: "tui-ownership".into(),
                hash: None,
            },
            state.clone(),
        );

        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        let (closed, _) = store.read(issue.meta.id).unwrap();
        assert_eq!(closed.meta.status, Status::Closed);
        let s = state.lock();
        let panel = s.issues_panel.as_ref().unwrap();
        assert!(!panel.pending, "pending should clear on completion");
        assert!(
            panel.error.is_none(),
            "close must not error: {:?}",
            panel.error
        );
    }
}