vtcode 0.162.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
420
421
422
423
424
425
426
use anyhow::{Context, bail};
use std::collections::HashMap;
use vtcode_commons::paths::ensure_path_within_workspace_resolved;
use vtcode_core::config::constants::tools;
use vtcode_core::tools::handlers::task_tracking::split_task_description_metadata;
use vtcode_core::tools::registry::ToolRegistry;
use vtcode_ui::tui::app::{InlineHandle, InlineMessageKind, PlanContent};

use super::tracker_response::resolve_tracker_file_response;
use super::validate_plan_content;

fn render_created_task_tracker(handle: &InlineHandle, output: &serde_json::Value) {
    let lines = crate::agent::runloop::tool_output::tracker_view_lines(output);
    if lines.is_empty() {
        return;
    }

    // Approval creates the tracker outside the normal tool pipeline, so make
    // the same panel/transcript updates that a regular task_tracker call gets.
    // Without this, the checklist exists on disk but remains invisible until
    // the user manually opens the task panel.
    handle.update_task_panel_with_metadata(
        lines.clone(),
        crate::agent::runloop::tool_output::tracker_panel_metadata(output),
    );
    handle.show_task_panel();
    // Skip the transcript append when the same block is already visible so the
    // approval handoff and its pipeline replay do not stack identical lists.
    if vtcode_core::utils::transcript::tail_matches(&lines) {
        return;
    }
    handle.append_pasted_message(InlineMessageKind::Tool, lines.join("\n"), lines.len());
}

#[derive(Debug, Clone)]
pub(crate) struct TaskTrackerHandoff {
    pub(crate) plan_file: std::path::PathBuf,
    pub(crate) tracker_file: std::path::PathBuf,
    pub(crate) item_count: usize,
}

fn markdown_task_description(line: &str) -> Option<(&str, bool)> {
    let trimmed = line.trim().strip_prefix("- ").unwrap_or(line.trim());
    if let Some(description) = trimmed.strip_prefix("[ ] ") {
        return Some((description.trim(), false));
    }
    if let Some(description) = trimmed.strip_prefix("[x] ").or_else(|| trimmed.strip_prefix("[X] ")) {
        return Some((description.trim(), true));
    }

    let (prefix, description) = trimmed.split_once('.').or_else(|| trimmed.split_once(')'))?;
    prefix
        .chars()
        .all(|character| character.is_ascii_digit())
        .then_some(description.trim())
        .filter(|description| !description.is_empty())
        .map(|description| (description, false))
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum PlanSection {
    Summary,
    Implementation,
    Validation,
    Assumptions,
}

fn plan_section(line: &str) -> Option<PlanSection> {
    let mut label = line.trim().trim_start_matches('>').trim_start();
    while let Some(stripped) = label.strip_prefix('#') {
        label = stripped.trim_start();
    }
    let label = label
        .strip_prefix("- ")
        .or_else(|| label.strip_prefix("* "))
        .unwrap_or(label)
        .trim()
        .trim_end_matches(':')
        .trim();

    if label.eq_ignore_ascii_case("Summary") {
        Some(PlanSection::Summary)
    } else if label.eq_ignore_ascii_case("Implementation Steps") || label.eq_ignore_ascii_case("Steps") {
        Some(PlanSection::Implementation)
    } else if label.eq_ignore_ascii_case("Test Cases and Validation") || label.eq_ignore_ascii_case("Validation") {
        Some(PlanSection::Validation)
    } else if label.eq_ignore_ascii_case("Assumptions and Defaults") || label.eq_ignore_ascii_case("Assumptions") {
        Some(PlanSection::Assumptions)
    } else {
        None
    }
}

fn sparse_implementation_task_lines(plan: &PlanContent) -> Vec<(&str, bool)> {
    let mut in_implementation = false;
    let mut saw_implementation = false;
    let mut compact_after_summary = false;
    let mut tasks = Vec::new();

    for line in plan.raw_content.lines() {
        if let Some(section) = plan_section(line) {
            match section {
                PlanSection::Summary => {
                    in_implementation = false;
                    compact_after_summary = !saw_implementation;
                }
                PlanSection::Implementation => {
                    in_implementation = true;
                    saw_implementation = true;
                    compact_after_summary = false;
                }
                PlanSection::Validation | PlanSection::Assumptions => {
                    in_implementation = false;
                    compact_after_summary = false;
                }
            }
            continue;
        }

        if (in_implementation || compact_after_summary)
            && let Some(task) = markdown_task_description(line)
        {
            tasks.push(task);
        }
    }

    tasks
}

fn normalized_task_description(description: &str) -> String {
    description
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .to_ascii_lowercase()
}

fn append_unique_task_item(
    items: &mut Vec<serde_json::Value>,
    index_by_description: &mut HashMap<String, usize>,
    item: serde_json::Value,
) {
    let Some(description) = item.get("description").and_then(|value| value.as_str()) else {
        return;
    };
    let key = normalized_task_description(description);
    if key.is_empty() {
        return;
    }

    if let Some(index) = index_by_description.get(&key).copied() {
        let existing = &mut items[index];
        if item["status"].as_str() == Some("completed") && existing["status"].as_str() != Some("completed") {
            existing["status"] = serde_json::Value::String("completed".to_string());
        }
        let existing_files_empty = existing
            .get("files")
            .and_then(serde_json::Value::as_array)
            .is_none_or(Vec::is_empty);
        if existing_files_empty
            && let Some(files) = item.get("files").filter(|files| !files.as_array().is_none_or(Vec::is_empty))
        {
            existing["files"] = files.clone();
        }
        let existing_verify_empty = existing
            .get("verify")
            .and_then(serde_json::Value::as_array)
            .is_none_or(Vec::is_empty);
        if existing_verify_empty
            && let Some(verify) = item.get("verify").filter(|verify| !verify.as_array().is_none_or(Vec::is_empty))
        {
            existing["verify"] = verify.clone();
        }
        return;
    }

    index_by_description.insert(key, items.len());
    items.push(item);
}

/// Build a tracker item from a plan-step description, splitting an inline
/// `Action -> files: [...] -> verify: [...]` suffix into structured fields so
/// the visible description stays clean. Explicit `step_files` (parsed from the
/// plan phase) win over inline-parsed files; inline verify is kept when the
/// caller provides none.
fn build_task_item(description: &str, completed: bool, step_files: &[String]) -> serde_json::Value {
    let (clean, parsed_files, parsed_verify) = split_task_description_metadata(description.trim());
    let description = if clean.is_empty() {
        description.trim().to_string()
    } else {
        clean
    };
    let mut item = serde_json::json!({
        "description": description,
        "status": if completed { "completed" } else { "pending" },
    });
    let files: Vec<String> = if step_files.is_empty() {
        parsed_files
    } else {
        step_files
            .iter()
            .map(|file| file.trim())
            .filter(|file| !file.is_empty())
            .map(ToOwned::to_owned)
            .collect()
    };
    if !files.is_empty()
        && let Ok(files) = serde_json::to_value(&files)
    {
        item["files"] = files;
    }
    if !parsed_verify.is_empty()
        && let Ok(verify) = serde_json::to_value(&parsed_verify)
    {
        item["verify"] = verify;
    }
    item
}

fn task_items_from_plan(plan: &PlanContent) -> Vec<serde_json::Value> {
    let mut items = Vec::new();
    let mut index_by_description = HashMap::new();
    for phase in &plan.phases {
        if !phase.name.trim().eq_ignore_ascii_case("Implementation Steps")
            && !phase.name.trim().eq_ignore_ascii_case("Steps")
        {
            continue;
        }
        for step in &phase.steps {
            if step.description.trim().is_empty() {
                continue;
            }
            let item = build_task_item(&step.description, step.completed, &step.files);
            append_unique_task_item(&mut items, &mut index_by_description, item);
        }
    }

    // Sparse plans may omit a phase heading. Preserve their numbered and
    // checkbox steps so approval still produces a usable tracker.
    if items.is_empty() {
        for (description, completed) in sparse_implementation_task_lines(plan) {
            let item = build_task_item(description, completed, &[]);
            append_unique_task_item(&mut items, &mut index_by_description, item);
        }
    }

    items
}

/// Create the implementation checklist while the planning gate is still
/// active. `PlanningTaskTrackerTool` deliberately rejects calls after
/// `finish_planning_workflow`, so this is the single lifecycle boundary for
/// approved-plan task handoff.
pub(crate) async fn create_task_tracker_from_active_plan(
    tool_registry: &ToolRegistry,
    handle: &InlineHandle,
) -> anyhow::Result<TaskTrackerHandoff> {
    let plan_state = tool_registry.planning_workflow_state();
    if !plan_state.is_active() {
        bail!("approved-plan task tracker creation requires an active planning workflow");
    }
    let plan_file = plan_state
        .get_plan_file()
        .await
        .context("approved plan is missing its persisted plan file")?;
    let plan_content = tokio::fs::read_to_string(&plan_file)
        .await
        .with_context(|| format!("failed to read approved plan {}", plan_file.display()))?;
    let validation = validate_plan_content(&plan_content);
    if !validation.is_ready() {
        bail!("approved plan is not ready for execution: {}", validation.reasons().join("; "));
    }

    let plan = PlanContent::from_markdown(
        plan_file
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("Implementation Plan")
            .to_string(),
        &plan_content,
        plan_file.to_str().map(|s| s.to_string()),
    );
    let items = task_items_from_plan(&plan);
    if items.is_empty() {
        bail!("approved plan contains no executable task items");
    }
    let item_count = items.len();

    let tool = tool_registry
        .get_tool(tools::TASK_TRACKER)
        .context("task_tracker is unavailable; approved-plan execution is blocked")?;
    let args = serde_json::json!({
        "action": "create",
        "title": plan.title,
        "items": items,
    });

    let result = tool
        .execute(args)
        .await
        .context("task_tracker failed during approved-plan handoff")?;
    if result.get("status").and_then(|value| value.as_str()) == Some("error") {
        bail!("task_tracker returned an error during approved-plan handoff: {result}");
    }
    // Tool responses intentionally expose workspace-relative paths. Resolve
    // that model-facing value back into the workspace before using it for
    // filesystem verification; otherwise a relative response would be
    // interpreted against the process working directory.
    let workspace_root = plan_state.workspace_root();
    let tracker_file = resolve_tracker_file_response(&result, workspace_root.as_deref(), &plan_file)?;
    let tracker_file = if let Some(workspace_root) = workspace_root.as_deref() {
        ensure_path_within_workspace_resolved(&tracker_file, workspace_root)
            .await
            .with_context(|| format!("failed to validate task tracker {}", tracker_file.display()))?
    } else {
        tracker_file
    };
    if !tokio::fs::try_exists(&tracker_file)
        .await
        .with_context(|| format!("failed to verify task tracker {}", tracker_file.display()))?
    {
        bail!("task_tracker reported success but did not create {}", tracker_file.display());
    }
    render_created_task_tracker(handle, &result);
    let message = result
        .get("message")
        .and_then(|value| value.as_str())
        .unwrap_or("Task tracker created");
    tracing::info!(message = %message, tracker_file = %tracker_file.display(), item_count, "Task tracker created during approved-plan handoff");

    Ok(TaskTrackerHandoff { plan_file, tracker_file, item_count })
}

#[cfg(test)]
mod tests {
    use super::{PlanContent, resolve_tracker_file_response, task_items_from_plan};
    use serde_json::json;
    use std::path::{Path, PathBuf};

    #[test]
    fn tracker_response_resolves_workspace_relative_path() {
        let result = json!({"tracker_file": ".vtcode/plans/task.tasks.md"});
        let workspace = Path::new("/workspace");
        let fallback = Path::new("/workspace/.vtcode/plans/fallback.tasks.md");

        let resolved = resolve_tracker_file_response(&result, Some(workspace), fallback).unwrap();

        assert_eq!(resolved, PathBuf::from("/workspace/.vtcode/plans/task.tasks.md"));
    }

    #[test]
    fn tracker_response_rejects_workspace_escape() {
        let result = json!({"tracker_file": "../outside.tasks.md"});
        let workspace = Path::new("/workspace");
        let fallback = Path::new("/workspace/.vtcode/plans/fallback.tasks.md");

        let error = resolve_tracker_file_response(&result, Some(workspace), fallback).unwrap_err();

        assert!(error.to_string().contains("escapes workspace"));
    }

    #[test]
    fn sparse_approved_plan_is_distilled_into_tracker_items() {
        let plan = PlanContent::from_markdown(
            "Launch plan".to_string(),
            "Summary\nImprove startup latency.\n\n1. Measure startup\n2. Defer eager setup\n- [x] Verify with cargo check",
            None,
        );

        let items = task_items_from_plan(&plan);

        assert_eq!(items.len(), 3);
        assert_eq!(items[0]["description"], "Measure startup");
        assert_eq!(items[0]["status"], "pending");
        assert_eq!(items[2]["description"], "Verify with cargo check");
        assert_eq!(items[2]["status"], "completed");
    }

    #[test]
    fn repeated_plan_steps_are_deduplicated_in_order() {
        let plan = PlanContent::from_markdown(
            "Launch plan".to_string(),
            "Summary\nImprove the runtime.\n\n## Phase 1\n1. Inspect the runtime\n2. Apply the fix\n\n## Phase 2\n1. inspect   the runtime\n[x] Verify the fix\n[x] APPLY THE FIX",
            None,
        );

        let items = task_items_from_plan(&plan);

        assert_eq!(items.len(), 3);
        assert_eq!(items[0]["description"], "Inspect the runtime");
        assert_eq!(items[1]["description"], "Apply the fix");
        assert_eq!(items[1]["status"], "completed");
        assert_eq!(items[2]["description"], "Verify the fix");
    }

    #[test]
    fn tracker_only_contains_implementation_steps() {
        let plan = PlanContent::from_markdown(
            "Launch plan".to_string(),
            "## Summary\nImprove startup behavior.\n\n## Implementation Steps\n1. Update src/startup.rs -> verify: cargo nextest run -p vtcode\n\n## Test Cases and Validation\n1. Run cargo nextest run -p vtcode\n\n## Assumptions and Defaults\n1. Existing startup policy remains unchanged.",
            None,
        );

        let items = task_items_from_plan(&plan);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0]["description"], "Update src/startup.rs");
        assert_eq!(items[0]["verify"], serde_json::json!(["cargo nextest run -p vtcode"]));
    }

    #[test]
    fn inline_files_and_verify_are_split_into_structured_fields() {
        let plan = PlanContent::from_markdown(
            "Launch plan".to_string(),
            "## Summary\nImprove startup behavior.\n\n## Implementation Steps\n1. Emit summary -> files: [src/a.rs, src/b.rs] -> verify: [cargo check]\n\n## Test Cases and Validation\n1. Run cargo check.\n\n## Assumptions and Defaults\n1. Existing policy remains unchanged.",
            None,
        );

        let items = task_items_from_plan(&plan);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0]["description"], "Emit summary");
        assert_eq!(items[0]["files"], serde_json::json!(["src/a.rs", "src/b.rs"]));
        assert_eq!(items[0]["verify"], serde_json::json!(["cargo check"]));
    }
}