vtcode-ui 0.175.3

Unified UI crate for VT Code: design system, theme registry, and TUI framework
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
use super::*;
use vtcode_commons::ui_protocol::ProgressOperation;

fn enabled() -> ProgramStatus {
    let mut status = ProgramStatus::default();
    status.apply(ProgramStatusUpdate::Configure { enabled: true });
    status
}

fn child(id: &str, kind: LocalAgentKind, state: ProgramState, updated_at: i64) -> LocalAgentEntry {
    LocalAgentEntry {
        id: id.into(),
        kind,
        program_status: state,
        updated_at,
        display_label: "secret command".into(),
        agent_name: "private agent".into(),
        status: "untrusted display state".into(),
        color: None,
        summary: Some("secret summary".into()),
        preview: "private path".into(),
        transcript_path: None,
    }
}

#[test]
fn program_status_opt_in_and_non_terminal_silence() {
    let mut status = ProgramStatus::default();
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(output.is_empty());
    status.apply(ProgramStatusUpdate::Configure { enabled: true });
    // A headless/test session has no attached terminal, even when opted in.
    status.flush();
    assert!(status.delivered.is_empty());
    status.deliver(&mut output).unwrap();
    assert!(String::from_utf8(output).unwrap().contains("state=idle"));
}

#[test]
fn program_status_owned_nested_waits_restore_latest_underlying_outcome() {
    let mut status = enabled();
    let operation = ProgressOperation::start();
    status.progress(ProgressUpdate::Begin { operation, phase: ProgressPhase::WaitingForModel });
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Working);
    status.apply(ProgramStatusUpdate::Wait { token: 3, kind: InteractionKind::Permission });
    status.apply(ProgramStatusUpdate::Wait { token: 7, kind: InteractionKind::Question });
    status.apply(ProgramStatusUpdate::Resume { token: 99 });
    assert_eq!(status.desired()[&status.parent].kind, Some(InteractionKind::Question));
    status.apply(ProgramStatusUpdate::Resume { token: 3 });
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Blocked);
    status.apply(ProgramStatusUpdate::Outcome(ProgramState::Error));
    status.apply(ProgramStatusUpdate::Resume { token: 7 });
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Error);
    status.progress(ProgressUpdate::Finish { operation });
    status.activity(ActivityState::Idle);
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Error);
}

#[test]
fn program_status_recovery_cancellation_and_stale_progress() {
    let mut status = enabled();
    status.activity(ActivityState::Planning);
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Idle);
    let old = ProgressOperation::start();
    let current = ProgressOperation::start();
    status.progress(ProgressUpdate::Begin {
        operation: old,
        phase: ProgressPhase::PreparingContext,
    });
    status.activity(ActivityState::Recovery);
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Working);
    status.activity(ActivityState::Blocked);
    assert_eq!(status.desired()[&status.parent].kind, None);
    status.progress(ProgressUpdate::Begin {
        operation: current,
        phase: ProgressPhase::RunningTools,
    });
    status.progress(ProgressUpdate::Finish { operation: old });
    status.progress(ProgressUpdate::Begin { operation: old, phase: ProgressPhase::Initializing });
    assert_eq!(status.desired()[&status.parent].message, "Running tools");
    status.apply(ProgramStatusUpdate::Outcome(ProgramState::Idle));
    status.progress(ProgressUpdate::Phase {
        operation: current,
        phase: ProgressPhase::Processing,
    });
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Idle);
}

#[test]
fn program_status_typed_children_cap_order_stable_ids_and_removal() {
    let mut status = enabled();
    let mut entries = (0..65)
        .map(|n| child(&format!("secret-{n}"), LocalAgentKind::Background, ProgramState::Done, n))
        .collect::<Vec<_>>();
    entries.push(child("old-active", LocalAgentKind::Delegated, ProgramState::Working, -10));
    entries.push(child("recent-error", LocalAgentKind::ExecSession, ProgramState::Error, 100));
    entries.push(child("new-idle", LocalAgentKind::ExecSession, ProgramState::Idle, 1000));
    status.children(&entries);
    assert_eq!(status.desired().len(), 64);
    let active_id = format!("{}/{}", status.parent, record_segment("delegated", "old-active"));
    assert_eq!(status.children[&active_id].state, ProgramState::Working);
    let excluded_id = format!("{}/{}", status.parent, record_segment("background", "secret-0"));
    assert!(!status.children.contains_key(&excluded_id));
    let idle_id = format!("{}/{}", status.parent, record_segment("exec-session", "new-idle"));
    assert!(!status.children.contains_key(&idle_id));
    let expected = status.children.clone();
    entries.reverse();
    status.children(&entries);
    assert_eq!(status.children, expected);
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    let wire = String::from_utf8(output).unwrap();
    assert!(!wire.contains("secret"));
    assert!(!wire.contains("private"));
    status.children(&[]);
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert_eq!(String::from_utf8(output).unwrap().matches("state=clear").count(), 63);
    assert_eq!(status.delivered.len(), 1);
}

#[test]
fn program_status_dedup_and_disable_only_owned_subtree() {
    let mut status = enabled();
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    output.clear();
    status.deliver(&mut output).unwrap();
    assert!(output.is_empty());
    status.apply(ProgramStatusUpdate::Configure { enabled: false });
    status.deliver(&mut output).unwrap();
    let wire = String::from_utf8(output).unwrap();
    assert!(wire.contains(&format!("state=clear:id={}:app=vtcode", status.parent)));
    assert!(!wire.contains("state=clear:app="));
    assert!(status.delivered.is_empty());
    status.apply(ProgramStatusUpdate::Configure { enabled: true });
    status.deliver(&mut Vec::new()).unwrap();
    assert_eq!(status.delivered.len(), 1);
}

struct FailedFlush;
impl Write for FailedFlush {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        Ok(bytes.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        Err(io::Error::other("delivery failed"))
    }
}

struct FailedWrite;
impl Write for FailedWrite {
    fn write(&mut self, _bytes: &[u8]) -> io::Result<usize> {
        Err(io::Error::other("write failed"))
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

#[test]
fn program_status_failed_write_and_clear_retry() {
    let mut status = enabled();
    assert!(status.deliver(&mut FailedWrite).is_err());
    assert!(status.delivered.is_empty());
    assert!(status.deliver(&mut FailedFlush).is_err());
    assert!(status.delivered.is_empty());
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(!output.is_empty());
    status.apply(ProgramStatusUpdate::Configure { enabled: false });
    assert!(status.deliver(&mut FailedFlush).is_err());
    assert!(status.delivered.is_empty());
    output.clear();
    status.deliver(&mut output).unwrap();
    assert!(String::from_utf8(output).unwrap().contains("state=clear"));
}

#[test]
fn program_status_disable_clears_uncertain_delivery_without_caching_it() {
    let mut status = enabled();
    assert!(status.deliver(&mut FailedFlush).is_err());
    assert!(status.delivered.is_empty());
    status.apply(ProgramStatusUpdate::Configure { enabled: false });
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert_eq!(
        String::from_utf8(output).unwrap(),
        format!("\x1b]7501;state=clear:id={}:app=vtcode:title=:msg=\x1b\\", status.parent)
    );
    assert!(status.attempted.is_empty());
    output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(output.is_empty());
}

#[test]
fn program_status_removed_child_clears_uncertain_delivery() {
    let mut status = enabled();
    status.deliver(&mut Vec::new()).unwrap();
    status.children(&[child("uncertain", LocalAgentKind::Background, ProgramState::Working, 1)]);
    assert!(status.deliver(&mut FailedFlush).is_err());
    assert_eq!(status.delivered.len(), 1);
    status.children(&[]);
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    let wire = String::from_utf8(output).unwrap();
    assert!(wire.contains("state=clear:"));
    assert!(!wire.contains(&format!(":id={}:app=", status.parent)));
    assert_eq!(status.attempted.len(), 1);
    assert_eq!(status.delivered.len(), 1);
}

#[test]
fn program_status_failed_wait_delivery_republishes_restored_state() {
    let mut status = enabled();
    status.deliver(&mut Vec::new()).unwrap();
    status.apply(ProgramStatusUpdate::Wait { token: 10, kind: InteractionKind::Question });
    assert!(status.deliver(&mut FailedFlush).is_err());
    status.apply(ProgramStatusUpdate::Resume { token: 10 });
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(String::from_utf8(output).unwrap().contains("state=idle:"));
}

#[test]
fn program_status_failed_subtree_clear_republishes_children_on_enable() {
    let mut status = enabled();
    let entries = [child("finished", LocalAgentKind::Background, ProgramState::Done, 1)];
    status.children(&entries);
    status.deliver(&mut Vec::new()).unwrap();
    status.apply(ProgramStatusUpdate::Configure { enabled: false });
    assert!(status.deliver(&mut FailedFlush).is_err());
    status.apply(ProgramStatusUpdate::Configure { enabled: true });
    status.children(&entries);
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    let wire = String::from_utf8(output).unwrap();
    assert_eq!(wire.matches("\x1b]7501;").count(), 2);
    assert!(wire.contains("state=idle:"));
    assert!(wire.contains("state=done:"));
    assert_eq!(status.delivered.len(), 2);
}

#[test]
fn program_status_disabled_flush_skips_terminal_lock() {
    let mut status = ProgramStatus { terminal: true, ..Default::default() };
    // Disabled reporting must not join terminal I/O or restoration bookkeeping.
    let guard = super::super::panic_hook::lock_terminal_operations();
    let (tx, rx) = std::sync::mpsc::channel();
    let worker = std::thread::spawn(move || {
        status.flush();
        tx.send(()).unwrap();
        status
    });
    let result = rx.recv_timeout(std::time::Duration::from_secs(1));
    // Release and join even on regression, so a failed check cannot hang cleanup.
    drop(guard);
    let status = worker.join().unwrap();
    assert!(result.is_ok(), "disabled flush must return while terminal operations are locked: {result:?}");
    assert!(status.attempted.is_empty());
    assert!(status.delivered.is_empty());
}

#[test]
fn program_status_live_enable_projects_latest_session_children_and_waits() {
    use crate::tui::core_tui::{app::AppSession, app::types::InlineCommand, types::InlineTheme};

    let mut session = AppSession::new(InlineTheme::default(), None, 24);
    let configure = |enabled| InlineCommand::ProgramStatus(ProgramStatusUpdate::Configure { enabled });
    let update = |update| InlineCommand::ProgramStatus(update);
    let first = child("first", LocalAgentKind::Background, ProgramState::Working, 1);
    session.handle_command(InlineCommand::SetLocalAgents { entries: vec![first.clone()] });
    session.handle_command(update(ProgramStatusUpdate::Wait { token: 42, kind: InteractionKind::Auth }));
    session.handle_command(update(ProgramStatusUpdate::Outcome(ProgramState::Done)));
    let latest = vec![
        child("first", LocalAgentKind::Background, ProgramState::Error, 2),
        child("second", LocalAgentKind::Delegated, ProgramState::Working, 3),
    ];
    session.handle_command(InlineCommand::SetLocalAgents { entries: latest.clone() });
    assert!(session.core.program_status.children.is_empty());
    assert_eq!(session.core.local_agents, latest);

    session.handle_command(configure(true));
    let status = &mut session.core.program_status;
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Blocked);
    assert_eq!(status.desired()[&status.parent].kind, Some(InteractionKind::Auth));
    assert_eq!(status.children.len(), 2);
    assert_eq!(
        status
            .children
            .values()
            .filter(|report| report.state == ProgramState::Error)
            .count(),
        1
    );
    status.deliver(&mut Vec::new()).unwrap();
    let prior_ids: Vec<_> = status.children.keys().cloned().collect();

    session.handle_command(configure(false));
    let status = &mut session.core.program_status;
    assert!(status.children.is_empty());
    assert!(status.deliver(&mut FailedFlush).is_err());
    assert!(!status.attempted.is_empty(), "failed disable must remain retryable");
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(String::from_utf8(output).unwrap().contains("state=clear:"));
    assert!(status.attempted.is_empty());

    // Keep parent ownership and the rendering snapshot current while disabled.
    session.handle_command(update(ProgramStatusUpdate::Resume { token: 42 }));
    session.handle_command(update(ProgramStatusUpdate::Outcome(ProgramState::Error)));
    session.handle_command(InlineCommand::SetLocalAgents { entries: vec![first] });
    session.handle_command(configure(true));
    let status = &mut session.core.program_status;
    assert_eq!(status.desired()[&status.parent].state, ProgramState::Error);
    assert_eq!(status.desired()[&status.parent].kind, None);
    assert_eq!(status.children.len(), 1);
    assert_eq!(status.children.values().next().unwrap().state, ProgramState::Working);
    assert!(prior_ids.contains(status.children.keys().next().unwrap()));
    assert!(status.delivered.is_empty(), "enable must publish a fresh subtree");
}

#[test]
fn program_status_shutdown_preserves_pending_finished_children_and_parent() {
    let mut status = enabled();
    status.children(&[
        child("done", LocalAgentKind::Background, ProgramState::Done, 1),
        child("busy", LocalAgentKind::Delegated, ProgramState::Working, 2),
    ]);
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    status.apply(ProgramStatusUpdate::Outcome(ProgramState::Error));
    status.closed = true;
    output.clear();
    status.deliver(&mut output).unwrap();
    assert_eq!(status.delivered.len(), 2);
    assert!(status.delivered.values().all(|report| report.state.is_finished()));
    let wire = String::from_utf8(output).unwrap();
    assert!(wire.contains("state=error"));
    assert_eq!(wire.matches("state=clear").count(), 1);
    status.deliver(&mut Vec::new()).unwrap();
}

#[test]
fn program_status_shutdown_does_not_clear_finished_child_with_parent() {
    let mut status = enabled();
    status.children(&[child("done", LocalAgentKind::Background, ProgramState::Done, 1)]);
    status.deliver(&mut Vec::new()).unwrap();
    status.closed = true;
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert_eq!(status.delivered.len(), 1);
    let wire = String::from_utf8(output).unwrap();
    assert!(wire.contains("state=idle"));
    assert!(!wire.contains("state=clear"));
}

#[test]
fn program_status_restoration_preserves_finished_descendants() {
    let records = BTreeMap::from([
        ("vt-owned".into(), ProgramState::Blocked),
        ("vt-owned/finished".into(), ProgramState::Error),
        ("vt-owned/running".into(), ProgramState::Working),
    ]);
    let mut output = Vec::new();
    retire_records(&records, &mut output);
    let wire = String::from_utf8(output).unwrap();
    assert!(wire.contains("state=idle:id=vt-owned:"));
    assert!(wire.contains("state=clear:id=vt-owned/running:"));
    assert!(!wire.contains("id=vt-owned/finished:"));
    assert!(!wire.contains("state=clear:id=vt-owned:"));
    let mut empty = Vec::new();
    retire_records(&BTreeMap::new(), &mut empty);
    assert!(empty.is_empty());
}

#[test]
fn program_status_commands_do_not_change_input_or_accessibility() {
    use crate::tui::core_tui::{
        session::Session,
        types::{InlineCommand, InlineTheme},
    };
    let mut session = Session::new(InlineTheme::default(), None, 24);
    let original_activity = session.activity_state;
    session.handle_command(InlineCommand::ProgramStatus(ProgramStatusUpdate::Configure { enabled: true }));
    session.handle_command(InlineCommand::ProgramStatus(ProgramStatusUpdate::Wait {
        token: 4,
        kind: InteractionKind::Auth,
    }));
    assert_eq!(session.activity_state, original_activity);
    assert!(!session.needs_animation_tick());
}

#[test]
fn program_status_task_progress_emits_determinate_working_only() {
    let mut status = enabled();
    let operation = ProgressOperation::start();
    status.progress(ProgressUpdate::Begin { operation, phase: ProgressPhase::RunningTools });
    assert_eq!(status.desired()[&status.parent].progress, None);
    status.apply(ProgramStatusUpdate::Progress { percent: Some(40) });
    assert_eq!(status.desired()[&status.parent].progress, Some(40));
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(String::from_utf8(output).unwrap().contains(":progress=40:"));
    status.apply(ProgramStatusUpdate::Outcome(ProgramState::Done));
    assert_eq!(status.desired()[&status.parent].progress, None);
    let mut output = Vec::new();
    status.deliver(&mut output).unwrap();
    assert!(!String::from_utf8(output).unwrap().contains("progress="));
    // Invalid values are ignored, retaining the last good percent. Use a
    // fresh operation so Begin is accepted and the state is Working.
    status.apply(ProgramStatusUpdate::Outcome(ProgramState::Idle));
    let next = ProgressOperation::start();
    status.progress(ProgressUpdate::Begin {
        operation: next,
        phase: ProgressPhase::RunningTools,
    });
    assert_eq!(status.desired()[&status.parent].progress, Some(40));
    status.apply(ProgramStatusUpdate::Progress { percent: Some(101) });
    assert_eq!(status.desired()[&status.parent].progress, Some(40));
    status.apply(ProgramStatusUpdate::Progress { percent: None });
    assert_eq!(status.desired()[&status.parent].progress, None);
}