vtcode 0.169.4

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
use std::sync::Arc;

use anyhow::Result;
use tokio::sync::Notify;
use vtcode_commons::modal_hints::{APPROVAL_NAVIGATE_DENY, APPROVAL_NAVIGATE_STOP};
use vtcode_core::config::constants::tool_limits::MAX_TOOL_LOOP_INCREMENT_PER_PROMPT;
use vtcode_core::core::interfaces::ui::UiSession;
use vtcode_ui::tui::app::{InlineHandle, ListOverlayRequest, TransientRequest, TransientSubmission};

use crate::agent::runloop::unified::overlay_prompt::{OverlayWaitOutcome, show_overlay_and_wait};
use crate::agent::runloop::unified::state::CtrlCState;

pub(super) async fn prompt_session_limit_increase<S: UiSession + ?Sized>(
    handle: &InlineHandle,
    session: &mut S,
    ctrl_c_state: &Arc<CtrlCState>,
    ctrl_c_notify: &Arc<Notify>,
    max_limit: usize,
    agent_name: Option<&str>,
) -> Result<Option<usize>> {
    use vtcode_ui::tui::app::{InlineListItem, InlineListSelection};

    let description_lines = vec![
        format!("Session tool limit reached: {}", max_limit),
        format!("Current agent: {}", agent_name.unwrap_or("unknown")),
        "Grant an increase to retry the pending tool call in this turn.".to_string(),
        "Deny stops the call; reuse the outputs already gathered for the next response.".to_string(),
    ];

    let options = vec![
        InlineListItem {
            title: "+100 tool calls".to_string(),
            subtitle: Some("Increase the session limit by 100".to_string()),
            badge: None,
            indent: 0,
            selection: Some(InlineListSelection::SessionLimitIncrease(100)),
            search_value: Some("increase 100 hundred plus more".to_string()),
        },
        InlineListItem {
            title: "+50 tool calls".to_string(),
            subtitle: Some("Increase the session limit by 50".to_string()),
            badge: None,
            indent: 0,
            selection: Some(InlineListSelection::SessionLimitIncrease(50)),
            search_value: Some("increase 50 fifty plus more".to_string()),
        },
        InlineListItem {
            title: "".to_string(),
            subtitle: None,
            badge: None,
            indent: 0,
            selection: None,
            search_value: None,
        },
        InlineListItem {
            title: "Deny".to_string(),
            subtitle: Some("Do not increase limit (stops tool execution)".to_string()),
            badge: None,
            indent: 0,
            selection: Some(InlineListSelection::ToolApproval(false)),
            search_value: Some("deny no exit stop cancel".to_string()),
        },
    ];

    prompt_limit_increase_modal(
        handle,
        session,
        ctrl_c_state,
        ctrl_c_notify,
        "Session Limit Reached".to_string(),
        description_lines,
        options,
        100,
        APPROVAL_NAVIGATE_DENY,
    )
    .await
}

/// Standard tool-loop grant candidates, largest first. Bounded by
/// `MAX_TOOL_LOOP_INCREMENT_PER_PROMPT` (50).
const TOOL_LOOP_GRANT_CANDIDATES: [usize; 3] = [50, 20, 10];

const _: () = {
    assert!(TOOL_LOOP_GRANT_CANDIDATES[0] <= MAX_TOOL_LOOP_INCREMENT_PER_PROMPT);
    assert!(TOOL_LOOP_GRANT_CANDIDATES[0] > TOOL_LOOP_GRANT_CANDIDATES[1]);
    assert!(TOOL_LOOP_GRANT_CANDIDATES[1] > TOOL_LOOP_GRANT_CANDIDATES[2]);
};

/// Search text for a grant option. Keeps the original number-word aliases
/// (`fifty`/`twenty`/`ten`) so type-ahead filtering still matches words as
/// well as digits; custom remainder options fall back to digits only.
fn tool_loop_search_value(increment: usize) -> String {
    let word = match increment {
        50 => " fifty",
        20 => " twenty",
        10 => " ten",
        _ => "",
    };
    format!("increase {increment}{word} plus more continue")
}

/// Viable grant increments for the remaining headroom below the hard cap.
///
/// Filters the standard candidates to those that fit, and prepends the exact
/// remaining headroom as a one-shot "reaches cap" option when it is smaller
/// than the largest candidate (e.g. remaining 40 -> `[40, 20, 10]`). When the
/// remaining headroom is smaller than every candidate (e.g. 5), returns just
/// the remainder so the turn can consume the cap without another prompt loop.
/// Returns empty when there is no headroom.
fn tool_loop_grant_options(remaining_headroom: usize) -> Vec<usize> {
    if remaining_headroom == 0 {
        return Vec::new();
    }
    let mut viable: Vec<usize> = TOOL_LOOP_GRANT_CANDIDATES
        .iter()
        .copied()
        .filter(|candidate| *candidate <= remaining_headroom)
        .collect();
    if viable.is_empty() {
        return vec![remaining_headroom];
    }
    let largest_candidate = TOOL_LOOP_GRANT_CANDIDATES[0];
    if remaining_headroom < largest_candidate && !viable.contains(&remaining_headroom) {
        viable.insert(0, remaining_headroom);
    }
    viable
}

pub(super) async fn prompt_tool_loop_limit_increase<S: UiSession + ?Sized>(
    handle: &InlineHandle,
    session: &mut S,
    ctrl_c_state: &Arc<CtrlCState>,
    ctrl_c_notify: &Arc<Notify>,
    max_limit: usize,
    hard_cap: usize,
    agent_name: Option<&str>,
) -> Result<Option<usize>> {
    use vtcode_ui::tui::app::{InlineListItem, InlineListSelection};

    let remaining_headroom = hard_cap.saturating_sub(max_limit);
    if remaining_headroom == 0 {
        return Ok(None);
    }
    let viable_increments = tool_loop_grant_options(remaining_headroom);
    if viable_increments.is_empty() {
        return Ok(None);
    }

    let description_lines = vec![
        format!("Maximum tool loops reached: {max_limit} (cap {hard_cap}, {remaining_headroom} remaining)"),
        format!("Current agent: {}", agent_name.unwrap_or("unknown")),
        "Grant more loops to continue this turn with the current agent.".to_string(),
        "Stop synthesizes from the outputs already gathered.".to_string(),
    ];

    let mut options: Vec<InlineListItem> = viable_increments
        .iter()
        .map(|increment| {
            let reaches_cap = *increment == remaining_headroom;
            let subtitle = if reaches_cap {
                format!("Continue with {increment} more tool loops (reaches cap)")
            } else {
                format!("Continue with {increment} more tool loops")
            };
            InlineListItem {
                title: format!("+{increment} tool loops"),
                subtitle: Some(subtitle),
                badge: None,
                indent: 0,
                selection: Some(InlineListSelection::SessionLimitIncrease(*increment)),
                search_value: Some(tool_loop_search_value(*increment)),
            }
        })
        .collect();
    options.push(InlineListItem {
        title: "".to_string(),
        subtitle: None,
        badge: None,
        indent: 0,
        selection: None,
        search_value: None,
    });
    options.push(InlineListItem {
        title: "Stop".to_string(),
        subtitle: Some("Stop the current turn and wait for input".to_string()),
        badge: None,
        indent: 0,
        selection: Some(InlineListSelection::ToolApproval(false)),
        search_value: Some("stop no exit cancel done".to_string()),
    });

    let default_increment = viable_increments.first().copied().unwrap_or(remaining_headroom);

    prompt_limit_increase_modal(
        handle,
        session,
        ctrl_c_state,
        ctrl_c_notify,
        "Tool Loop Limit Reached".to_string(),
        description_lines,
        options,
        default_increment,
        APPROVAL_NAVIGATE_STOP,
    )
    .await
}

async fn prompt_limit_increase_modal<S: UiSession + ?Sized>(
    handle: &InlineHandle,
    session: &mut S,
    ctrl_c_state: &Arc<CtrlCState>,
    ctrl_c_notify: &Arc<Notify>,
    title: String,
    description_lines: Vec<String>,
    options: Vec<vtcode_ui::tui::app::InlineListItem>,
    default_increment: usize,
    footer_hint: &'static str,
) -> Result<Option<usize>> {
    use vtcode_ui::tui::app::InlineListSelection;

    // A bridge submission is deferred while the modal owns the input surface.
    // Re-show this limit prompt and continue waiting so that a transient
    // bridge event cannot accidentally become a denial of the grant.
    loop {
        let outcome = show_overlay_and_wait(
            handle,
            session,
            TransientRequest::List(ListOverlayRequest {
                title: title.clone(),
                lines: description_lines.clone(),
                footer_hint: Some(footer_hint.to_string()),
                items: options.clone(),
                selected: Some(InlineListSelection::SessionLimitIncrease(default_increment)),
                search: None,
                hotkeys: Vec::new(),
            }),
            ctrl_c_state,
            ctrl_c_notify,
            |submission| match submission {
                TransientSubmission::Selection(InlineListSelection::SessionLimitIncrease(inc)) => Some(inc),
                // All grant options are strictly positive; zero is the
                // explicit Deny/Stop selection sentinel.
                TransientSubmission::Selection(InlineListSelection::ToolApproval(false)) => Some(0),
                TransientSubmission::Selection(_) => None,
                _ => None,
            },
        )
        .await?;

        match outcome {
            OverlayWaitOutcome::Submitted(0) => return Ok(None),
            OverlayWaitOutcome::Submitted(increment) => return Ok(Some(increment)),
            // Esc/Cancel is the user's explicit denial. Interrupt and Exit
            // remain distinct control-flow outcomes but also deny the grant.
            OverlayWaitOutcome::Cancelled | OverlayWaitOutcome::Interrupted | OverlayWaitOutcome::Exit => {
                return Ok(None);
            }
            OverlayWaitOutcome::Deferred => continue,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::collections::VecDeque;
    use tokio::sync::{Notify, mpsc};
    use vtcode_ui::tui::app::{InlineCommand, InlineEvent, InlineListSelection, TransientEvent, TransientSubmission};

    struct TestSession {
        handle: InlineHandle,
        events: VecDeque<InlineEvent>,
    }

    #[async_trait]
    impl UiSession for TestSession {
        fn inline_handle(&self) -> &InlineHandle {
            &self.handle
        }

        async fn next_event(&mut self) -> Option<InlineEvent> {
            self.events.pop_front()
        }
    }

    #[tokio::test]
    async fn deferred_bridge_event_reshows_limit_prompt_until_grant() {
        let (command_sender, mut command_receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(command_sender);
        let mut session = TestSession {
            handle: handle.clone(),
            events: VecDeque::from([
                // A leaked mode-switch event belongs to the active turn and
                // must not dismiss the prompt.
                InlineEvent::CyclePrimaryAgent,
                InlineEvent::WebmcpSubmit("continue".into()),
                InlineEvent::Transient(TransientEvent::Submitted(TransientSubmission::Selection(
                    InlineListSelection::SessionLimitIncrease(50),
                ))),
            ]),
        };
        let ctrl_c_state = Arc::new(CtrlCState::new());
        let ctrl_c_notify = Arc::new(Notify::new());

        let result =
            prompt_session_limit_increase(&handle, &mut session, &ctrl_c_state, &ctrl_c_notify, 100, Some("build"))
                .await
                .expect("limit prompt should remain available after deferred input");

        assert_eq!(result, Some(50));
        let mut shown = 0;
        while let Ok(command) = command_receiver.try_recv() {
            if matches!(command, InlineCommand::ShowTransient { .. }) {
                shown += 1;
            }
        }
        assert_eq!(shown, 2, "the deferred bridge input should cause a re-show");
    }

    #[tokio::test]
    async fn cancel_is_an_explicit_denial() {
        let (command_sender, _command_receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(command_sender);
        let mut session = TestSession {
            handle: handle.clone(),
            events: VecDeque::from([InlineEvent::Cancel]),
        };
        let ctrl_c_state = Arc::new(CtrlCState::new());
        let ctrl_c_notify = Arc::new(Notify::new());

        let result =
            prompt_session_limit_increase(&handle, &mut session, &ctrl_c_state, &ctrl_c_notify, 100, Some("build"))
                .await
                .expect("cancel should be handled as a denial");

        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn deny_selection_is_an_explicit_denial() {
        let (command_sender, _command_receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(command_sender);
        let mut session = TestSession {
            handle: handle.clone(),
            events: VecDeque::from([InlineEvent::Transient(TransientEvent::Submitted(
                TransientSubmission::Selection(InlineListSelection::ToolApproval(false)),
            ))]),
        };
        let ctrl_c_state = Arc::new(CtrlCState::new());
        let ctrl_c_notify = Arc::new(Notify::new());

        let result =
            prompt_session_limit_increase(&handle, &mut session, &ctrl_c_state, &ctrl_c_notify, 100, Some("build"))
                .await
                .expect("deny selection should be handled as a denial");

        assert_eq!(result, None);
    }

    #[test]
    fn tool_loop_grant_options_fit_remaining_headroom() {
        assert_eq!(tool_loop_grant_options(0), Vec::<usize>::new());
        assert_eq!(tool_loop_grant_options(50), vec![50, 20, 10]);
        assert_eq!(tool_loop_grant_options(100), vec![50, 20, 10]);
        assert_eq!(tool_loop_grant_options(60), vec![50, 20, 10]);
    }

    #[test]
    fn tool_loop_grant_options_offer_exact_remainder_below_max_candidate() {
        // Reported bug: remaining 40 offered +50, then clamped to +40.
        assert_eq!(tool_loop_grant_options(40), vec![40, 20, 10]);
        assert_eq!(tool_loop_grant_options(25), vec![25, 20, 10]);
        assert_eq!(tool_loop_grant_options(15), vec![15, 10]);
        assert_eq!(tool_loop_grant_options(20), vec![20, 10]);
        assert_eq!(tool_loop_grant_options(10), vec![10]);
    }

    #[test]
    fn tool_loop_grant_options_offer_single_remainder_when_below_smallest_candidate() {
        assert_eq!(tool_loop_grant_options(9), vec![9]);
        assert_eq!(tool_loop_grant_options(5), vec![5]);
        assert_eq!(tool_loop_grant_options(1), vec![1]);
    }

    #[test]
    fn tool_loop_search_value_keeps_number_word_aliases() {
        assert!(tool_loop_search_value(50).contains("fifty"));
        assert!(tool_loop_search_value(20).contains("twenty"));
        assert!(tool_loop_search_value(10).contains("ten"));
        assert!(tool_loop_search_value(40).contains("40"));
    }

    #[tokio::test]
    async fn at_cap_tool_loop_prompt_returns_denial_without_modal() {
        let (command_sender, mut command_receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(command_sender);
        let mut session = TestSession { handle: handle.clone(), events: VecDeque::new() };
        let ctrl_c_state = Arc::new(CtrlCState::new());
        let ctrl_c_notify = Arc::new(Notify::new());

        let result = prompt_tool_loop_limit_increase(
            &handle,
            &mut session,
            &ctrl_c_state,
            &ctrl_c_notify,
            60,
            60,
            Some("build"),
        )
        .await
        .expect("at-cap prompt should fail closed without modal");

        assert_eq!(result, None);
        while let Ok(command) = command_receiver.try_recv() {
            assert!(!matches!(command, InlineCommand::ShowTransient { .. }), "at-cap must not show a grant modal");
        }
    }
}