vtcode 0.141.9

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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use super::archive::workspace_archive_label;
use super::*;
use crate::agent::runloop::git::{DirtyWorktreeStatus, git_dirty_worktree_entries, workspace_relative_display};
use crate::agent::runloop::unified::overlay_prompt::{OverlayWaitOutcome, show_overlay_and_wait};
use crate::agent::runloop::unified::turn::context::TurnLoopResult;
use std::sync::Arc;
use vtcode_core::llm::provider::MessageRole;
use vtcode_core::tools::registry::ToolRegistry;
use vtcode_core::utils::session_archive;
use vtcode_ui::tui::app::{
    InlineHandle, InlineListItem, InlineListSelection, InlineSession, ListOverlayRequest, TransientRequest,
    TransientSubmission,
};

const STARTUP_PLANNING_WORKFLOW_ENTER_ACTION: &str = "planning_active:start_enter";
const STARTUP_PLANNING_WORKFLOW_STAY_ACTION: &str = "planning_active:start_stay";

#[cfg(test)]
#[derive(Clone)]
pub(super) struct TurnHistoryCheckpoint {
    baseline_len: usize,
    #[cfg(debug_assertions)]
    prefix_fingerprint: u64,
}

#[cfg(test)]
impl TurnHistoryCheckpoint {
    pub(super) fn capture(history: &[vtcode_core::llm::provider::Message]) -> Self {
        Self {
            baseline_len: history.len(),
            #[cfg(debug_assertions)]
            prefix_fingerprint: Self::prefix_fingerprint(history),
        }
    }

    pub(super) fn rollback(&self, history: &mut Vec<vtcode_core::llm::provider::Message>) {
        #[cfg(debug_assertions)]
        self.assert_append_only(history);
        history.truncate(self.baseline_len);
    }

    #[cfg(debug_assertions)]
    fn assert_append_only(&self, history: &[vtcode_core::llm::provider::Message]) {
        debug_assert!(
            history.len() >= self.baseline_len,
            "turn history rollback requires append-only growth after checkpoint"
        );
        debug_assert_eq!(
            Self::prefix_fingerprint(&history[..self.baseline_len]),
            self.prefix_fingerprint,
            "turn history rollback requires the pre-checkpoint prefix to remain unchanged"
        );
    }

    #[cfg(debug_assertions)]
    fn prefix_fingerprint(history: &[vtcode_core::llm::provider::Message]) -> u64 {
        use std::hash::{Hash, Hasher};

        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        serde_json::to_string(history).unwrap_or_default().hash(&mut hasher);
        hasher.finish()
    }
}

pub(super) fn remove_transient_system_notes(history: &mut Vec<vtcode_core::llm::provider::Message>, notes: &[String]) {
    for note in notes.iter().rev() {
        if let Some(index) = history
            .iter()
            .rposition(|message| message.role == MessageRole::System && message.content.as_text() == note.as_str())
        {
            let _ = history.remove(index);
        }
    }
}

pub(super) fn build_tracked_file_freshness_note(
    workspace: &std::path::Path,
    stale_paths: &[std::path::PathBuf],
) -> Option<String> {
    if stale_paths.is_empty() {
        return None;
    }

    let display_paths = stale_paths
        .iter()
        .map(|path| format!("- {}", workspace_relative_display(workspace, path)))
        .collect::<Vec<_>>()
        .join("\n");

    Some(format!(
        "Freshness note: the following files changed on disk after VT Code last read them:\n{display_paths}\nRe-read these files before relying on earlier content because disk content is newer than the agent's prior read snapshot."
    ))
}

pub(super) fn build_unrelated_dirty_worktree_note(
    workspace: &std::path::Path,
    agent_touched_paths: &std::collections::BTreeSet<std::path::PathBuf>,
) -> Result<Option<String>> {
    let Some(entries) = git_dirty_worktree_entries(workspace)? else {
        return Ok(None);
    };

    let display_paths = entries
        .into_iter()
        .filter(|entry| entry.status == DirtyWorktreeStatus::Modified && !agent_touched_paths.contains(&entry.path))
        .map(|entry| format!("- {}", workspace_relative_display(workspace, &entry.path)))
        .collect::<Vec<_>>();

    if display_paths.is_empty() {
        return Ok(None);
    }

    Ok(Some(format!(
        "Workspace note: the following files already have unrelated user modifications before this turn:\n{}\nTreat these files as user-owned changes. Do not edit, format, revert, or overwrite them unless the user explicitly asks to work on those files.",
        display_paths.join("\n")
    )))
}

pub(super) fn append_transient_turn_notes(
    history: &mut Vec<vtcode_core::llm::provider::Message>,
    workspace: &std::path::Path,
    tool_registry: &ToolRegistry,
    agent_touched_paths: &std::collections::BTreeSet<std::path::PathBuf>,
) -> Vec<String> {
    let mut transient_system_notes = Vec::with_capacity(2);

    if let Some(note) = {
        let stale_paths = tool_registry.edited_file_monitor_ref().stale_tracked_paths();
        build_tracked_file_freshness_note(workspace, &stale_paths)
    } {
        transient_system_notes.push(note.clone());
        history.push(vtcode_core::llm::provider::Message::system(note));
    }

    match build_unrelated_dirty_worktree_note(workspace, agent_touched_paths) {
        Ok(Some(note)) => {
            transient_system_notes.push(note.clone());
            history.push(vtcode_core::llm::provider::Message::system(note));
        }
        Ok(None) => {}
        Err(err) => {
            tracing::warn!(
                error = %err,
                "Failed to inspect unrelated dirty worktree entries before turn"
            );
        }
    }

    transient_system_notes
}

pub(super) fn latest_assistant_result_text(messages: &[vtcode_core::llm::provider::Message]) -> Option<String> {
    messages
        .iter()
        .rev()
        .find(|message| message.role == MessageRole::Assistant)
        .map(|message| message.content.as_text().trim().to_string())
        .filter(|text| !text.is_empty())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ExecutionSummaryStatus {
    Completed,
    Blocked,
    Failed,
}

impl ExecutionSummaryStatus {
    pub(super) const fn as_str(self) -> &'static str {
        match self {
            Self::Completed => "completed",
            Self::Blocked => "blocked",
            Self::Failed => "failed",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ExecutionSummary {
    pub(super) status: ExecutionSummaryStatus,
    pub(super) blocker: Option<String>,
}

pub(super) fn classify_execution_summary(
    result: &TurnLoopResult,
    final_response_was_fallback: bool,
    checklist: Option<&serde_json::Value>,
    changed_files: bool,
) -> ExecutionSummaryStatus {
    match result {
        TurnLoopResult::Completed { .. } => {
            if final_response_was_fallback || !changed_files {
                return ExecutionSummaryStatus::Blocked;
            }
            let Some(checklist) = checklist else {
                return ExecutionSummaryStatus::Blocked;
            };
            let total = checklist.get("total").and_then(serde_json::Value::as_u64).unwrap_or_default();
            let completed = checklist
                .get("completed")
                .and_then(serde_json::Value::as_u64)
                .unwrap_or_default();
            let pending = checklist.get("pending").and_then(serde_json::Value::as_u64).unwrap_or_default();
            let in_progress = checklist
                .get("in_progress")
                .and_then(serde_json::Value::as_u64)
                .unwrap_or_default();
            let blocked = checklist.get("blocked").and_then(serde_json::Value::as_u64).unwrap_or_default();
            if total > 0 && completed >= total && pending == 0 && in_progress == 0 && blocked == 0 {
                ExecutionSummaryStatus::Completed
            } else {
                ExecutionSummaryStatus::Blocked
            }
        }
        TurnLoopResult::Blocked { .. } => ExecutionSummaryStatus::Blocked,
        TurnLoopResult::Aborted | TurnLoopResult::Cancelled | TurnLoopResult::Exit => ExecutionSummaryStatus::Failed,
    }
}

fn pending_checklist_items(checklist: &serde_json::Value) -> Vec<String> {
    checklist
        .get("items")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
        .filter(|item| item.get("status").and_then(serde_json::Value::as_str) != Some("completed"))
        .filter_map(|item| item.get("description").and_then(serde_json::Value::as_str))
        .map(str::to_string)
        .take(4)
        .collect()
}

fn execution_summary_blocker(
    result: &TurnLoopResult,
    final_response_was_fallback: bool,
    checklist: Option<&serde_json::Value>,
    changed_files: bool,
) -> Option<String> {
    if final_response_was_fallback {
        return Some("recovery ended with a deterministic fallback and did not confirm the requested work".to_string());
    }
    if !changed_files && matches!(result, TurnLoopResult::Completed { .. }) {
        return Some(
            "the approved-plan turn produced no file changes, so implementation completion was not confirmed"
                .to_string(),
        );
    }
    if matches!(result, TurnLoopResult::Aborted | TurnLoopResult::Cancelled | TurnLoopResult::Exit) {
        return Some("the execution turn did not finish successfully".to_string());
    }
    if let TurnLoopResult::Blocked { reason } = result {
        return Some(reason.clone().unwrap_or_else(|| "the execution turn was blocked".to_string()));
    }
    let Some(checklist) = checklist else {
        return Some("the approved-plan task checklist was not available".to_string());
    };
    let pending = pending_checklist_items(checklist);
    if !pending.is_empty() {
        return Some(format!("pending checklist items: {}", pending.join(", ")));
    }
    Some("the approved-plan checklist is not fully completed".to_string())
}

pub(super) async fn approved_plan_execution_summary(
    tool_registry: &ToolRegistry,
    result: &TurnLoopResult,
    final_response_was_fallback: bool,
    changed_files: bool,
) -> ExecutionSummary {
    let checklist_result = match tool_registry.get_tool(vtcode_core::config::constants::tools::TASK_TRACKER) {
        Some(tool) => match tool.execute(serde_json::json!({"action": "list"})).await {
            Ok(value) => Some(value),
            Err(err) => {
                tracing::warn!(error = %err, "Failed to read approved-plan task tracker for execution summary");
                None
            }
        },
        None => None,
    };
    let checklist = checklist_result.as_ref().and_then(|value| value.get("checklist"));
    let status = classify_execution_summary(result, final_response_was_fallback, checklist, changed_files);
    let blocker = (status != ExecutionSummaryStatus::Completed)
        .then(|| execution_summary_blocker(result, final_response_was_fallback, checklist, changed_files))
        .flatten();
    ExecutionSummary { status, blocker }
}

pub(super) fn take_pending_resumed_user_prompt(
    history: &mut Vec<vtcode_core::llm::provider::Message>,
) -> Option<String> {
    let user_index = history.iter().rposition(|message| message.role == MessageRole::User)?;
    if history
        .iter()
        .skip(user_index + 1)
        .any(|message| message.role != MessageRole::System)
    {
        return None;
    }

    let prompt = history[user_index].content.as_text().trim().to_string();
    if prompt.is_empty() {
        return None;
    }

    let _ = history.remove(user_index);
    Some(prompt)
}

pub(super) fn live_reload_preserves_session_config(
    initial_vt_cfg: Option<&VTCodeConfig>,
    runtime_cfg: &CoreAgentConfig,
) -> bool {
    let Some(initial_vt_cfg) = initial_vt_cfg else {
        return true;
    };

    let mut reloaded_vt_cfg = vtcode_core::config::loader::ConfigManager::load_from_workspace(&runtime_cfg.workspace)
        .ok()
        .map(|manager| manager.config().clone());
    crate::agent::agents::apply_runtime_overrides(reloaded_vt_cfg.as_mut(), runtime_cfg);

    let Some(reloaded_vt_cfg) = reloaded_vt_cfg else {
        return false;
    };

    let Ok(initial_value) = serde_json::to_value(initial_vt_cfg) else {
        return false;
    };
    let Ok(reloaded_value) = serde_json::to_value(reloaded_vt_cfg) else {
        return false;
    };

    initial_value == reloaded_value
}

pub(super) fn prepare_resume_bootstrap_without_archive(
    resume: &ResumeSession,
    mut metadata: session_archive::SessionArchiveMetadata,
    reserved_archive_id: Option<String>,
) -> (vtcode_core::core::threads::ThreadBootstrap, String) {
    let source_metadata = &resume.snapshot().metadata;
    let is_compatible = metadata.workspace_path == source_metadata.workspace_path
        && metadata.provider == source_metadata.provider
        && metadata.model == source_metadata.model;
    if is_compatible && let Some(lineage_id) = source_metadata.prompt_cache_lineage_id.as_ref() {
        metadata.prompt_cache_lineage_id = Some(lineage_id.clone());
    }
    metadata.continuation_metadata = source_metadata.continuation_metadata.clone();
    if resume.is_fork() {
        metadata.parent_session_id = Some(resume.identifier());
        metadata.fork_mode = Some(if resume.summarize_fork() {
            session_archive::SessionForkMode::Summarized
        } else {
            session_archive::SessionForkMode::FullCopy
        });
    }

    let mut bootstrap = resume.bootstrap().clone();
    bootstrap.metadata = Some(metadata);
    if resume.is_fork() {
        bootstrap.archive_listing = None;
    }

    let thread_id = match resume.intent() {
        vtcode_core::core::threads::ArchivedSessionIntent::ResumeInPlace => resume.identifier(),
        vtcode_core::core::threads::ArchivedSessionIntent::ForkNewArchive { .. } => {
            reserved_archive_id.unwrap_or_else(|| {
                session_archive::generate_session_archive_identifier(
                    &workspace_archive_label(std::path::Path::new(&resume.snapshot().metadata.workspace_path)),
                    resume.custom_suffix().map(str::to_owned),
                )
            })
        }
    };

    (bootstrap, thread_id)
}

pub(super) async fn checkpoint_session_archive_start(
    archive: &session_archive::SessionArchive,
    thread_handle: &vtcode_core::core::threads::ThreadRuntimeHandle,
) -> Result<()> {
    let snapshot = thread_handle.snapshot();
    let messages: Vec<SessionMessage> = snapshot.messages.iter().map(SessionMessage::from).collect();
    archive
        .persist_progress_async(SessionProgressArgs {
            total_messages: snapshot.messages.len(),
            distinct_tools: Vec::new(),
            messages: messages.clone(),
            recent_messages: messages,
            turn_number: 1,
            token_usage: None,
            max_context_tokens: None,
            loaded_skills: Some(snapshot.loaded_skills),
        })
        .await?;
    Ok(())
}

pub(super) async fn force_reload_workspace_config_for_execution(
    workspace: &std::path::Path,
    runtime_cfg: &CoreAgentConfig,
    vt_cfg: &mut Option<VTCodeConfig>,
    tool_registry: &mut ToolRegistry,
    async_mcp_manager: Option<&crate::agent::runloop::unified::async_mcp_manager::AsyncMcpManager>,
) -> Result<()> {
    crate::agent::runloop::unified::turn::workspace::refresh_vt_config(workspace, runtime_cfg, vt_cfg).await?;

    if let Some(cfg) = vt_cfg.as_ref() {
        crate::agent::runloop::unified::turn::workspace::apply_workspace_config_to_registry(tool_registry, cfg)?;

        if let Some(mcp_manager) = async_mcp_manager {
            let desired_policy =
                crate::agent::runloop::unified::async_mcp_manager::approval_policy_from_human_in_the_loop(
                    cfg.security.human_in_the_loop,
                );
            if mcp_manager.approval_policy() != desired_policy {
                mcp_manager.set_approval_policy(desired_policy);
            }
        }
    }

    Ok(())
}

pub(super) async fn prompt_startup_planning_workflow(
    handle: &InlineHandle,
    session: &mut InlineSession,
    ctrl_c_state: &Arc<crate::agent::runloop::unified::state::CtrlCState>,
    ctrl_c_notify: &Arc<Notify>,
) -> Result<bool> {
    let overlay = TransientRequest::List(ListOverlayRequest {
        title: "Start planning workflow?".to_string(),
        lines: vec![
            "Your configuration starts new sessions in the planning workflow.".to_string(),
            "The planning workflow keeps mutating tools blocked until execution is approved.".to_string(),
        ],
        footer_hint: Some("You can start or finish planning later with `/plan`.".to_string()),
        items: vec![
            InlineListItem {
                title: "Start planning".to_string(),
                subtitle: Some("Use the planning workflow before execution.".to_string()),
                badge: Some("Recommended".to_string()),
                indent: 0,
                selection: Some(InlineListSelection::ConfigAction(STARTUP_PLANNING_WORKFLOW_ENTER_ACTION.to_string())),
                search_value: None,
            },
            InlineListItem {
                title: "Start normally".to_string(),
                subtitle: Some("Use the selected primary agent without planning first.".to_string()),
                badge: None,
                indent: 0,
                selection: Some(InlineListSelection::ConfigAction(STARTUP_PLANNING_WORKFLOW_STAY_ACTION.to_string())),
                search_value: None,
            },
        ],
        selected: Some(InlineListSelection::ConfigAction(STARTUP_PLANNING_WORKFLOW_ENTER_ACTION.to_string())),
        search: None,
        hotkeys: Vec::new(),
    });

    let outcome =
        show_overlay_and_wait(handle, session, overlay, ctrl_c_state, ctrl_c_notify, |submission| match submission {
            TransientSubmission::Selection(InlineListSelection::ConfigAction(action))
                if action == STARTUP_PLANNING_WORKFLOW_ENTER_ACTION =>
            {
                Some(true)
            }
            TransientSubmission::Selection(InlineListSelection::ConfigAction(action))
                if action == STARTUP_PLANNING_WORKFLOW_STAY_ACTION =>
            {
                Some(false)
            }
            TransientSubmission::Selection(_) => Some(false),
            _ => None,
        })
        .await?;

    Ok(matches!(outcome, OverlayWaitOutcome::Submitted(true)))
}

#[cfg(test)]
mod tests {
    use super::{ExecutionSummaryStatus, classify_execution_summary};
    use crate::agent::runloop::unified::turn::context::TurnLoopResult;
    use serde_json::json;

    #[test]
    fn pending_approved_plan_checklist_cannot_be_completed() {
        let checklist = json!({
            "total": 3,
            "completed": 1,
            "pending": 1,
            "in_progress": 1,
            "blocked": 0
        });
        let result = TurnLoopResult::Completed { plan_approved_execution_pending: false };

        assert_eq!(classify_execution_summary(&result, false, Some(&checklist), true), ExecutionSummaryStatus::Blocked);
    }

    #[test]
    fn completed_approved_plan_requires_all_checklist_items() {
        let checklist = json!({
            "total": 2,
            "completed": 2,
            "pending": 0,
            "in_progress": 0,
            "blocked": 0
        });
        let result = TurnLoopResult::Completed { plan_approved_execution_pending: false };

        assert_eq!(
            classify_execution_summary(&result, false, Some(&checklist), true),
            ExecutionSummaryStatus::Completed
        );
    }

    #[test]
    fn recovery_fallback_is_blocked_even_with_a_complete_tracker() {
        let checklist = json!({
            "total": 1,
            "completed": 1,
            "pending": 0,
            "in_progress": 0,
            "blocked": 0
        });
        let result = TurnLoopResult::Completed { plan_approved_execution_pending: false };

        assert_eq!(classify_execution_summary(&result, true, Some(&checklist), true), ExecutionSummaryStatus::Blocked);
    }

    #[test]
    fn completed_plan_without_file_changes_is_blocked() {
        let checklist = json!({
            "total": 1,
            "completed": 1,
            "pending": 0,
            "in_progress": 0,
            "blocked": 0
        });
        let result = TurnLoopResult::Completed { plan_approved_execution_pending: false };

        assert_eq!(
            classify_execution_summary(&result, false, Some(&checklist), false),
            ExecutionSummaryStatus::Blocked
        );
    }
}