vtcode-core 0.181.10

Core library for VT Code - a Rust-based terminal coding agent
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
use super::*;
use tempfile::TempDir;

async fn setup_planning_workflow() -> (TempDir, PlanningWorkflowState, PlanningTaskTrackerTool) {
    let temp_dir = TempDir::new().expect("temp dir");
    let state = PlanningWorkflowState::new(temp_dir.path().to_path_buf());
    let plans_dir = state.plans_dir();
    std::fs::create_dir_all(&plans_dir).expect("create plans dir");
    let plan_file = plans_dir.join("test-plan.md");
    std::fs::write(&plan_file, "# Test Plan\n").expect("write plan");
    state.set_plan_file(Some(plan_file));
    state.enable();

    let tool = PlanningTaskTrackerTool::new(state.clone());
    (temp_dir, state, tool)
}

#[tokio::test]
async fn create_and_list_tracker_with_hierarchy() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    let created = tool
        .execute(json!({
            "action": "create",
            "title": "Updated Plan",
            "items": [
                "Add config cap",
                "  Use cap in guard logic",
                "[~] Expose setting in template"
            ]
        }))
        .await
        .expect("create tracker");

    assert_eq!(created["status"], "created");
    assert_eq!(created["checklist"]["total"], 3);
    assert_eq!(created["checklist"]["in_progress"], 1);
    assert_eq!(created["view"]["title"], "Updated Plan");
    assert_eq!(created["tracker_file"], ".vtcode/plans/test-plan.tasks.md");

    let lines = created["view"]["lines"].as_array().expect("view lines array");
    assert!(!lines.is_empty());
    let first = lines[0]["display"].as_str().unwrap_or_default();
    assert!(first.contains('└') || first.contains('├'));
}

#[tokio::test]
async fn create_accepts_metadata_and_verify_string_forms() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    let created = tool
        .execute(json!({
            "action": "create",
            "title": "Harness plan",
            "items": [
                {
                    "description": "Analyze",
                    "files": ["docs/ARCHITECTURE.md"],
                    "outcome": "Map the harness",
                    "verify": "cargo check"
                },
                {
                    "description": "Implement",
                    "verify": ["cargo test -p vtcode-core task_tracker", "cargo check -p vtcode"]
                }
            ]
        }))
        .await
        .expect("create tracker");

    assert_eq!(created["checklist"]["items"][0]["files"], json!(["docs/ARCHITECTURE.md"]));
    assert_eq!(created["checklist"]["items"][0]["outcome"], "Map the harness");
    assert_eq!(created["checklist"]["items"][0]["verify"], json!(["cargo check"]));
    assert_eq!(
        created["checklist"]["items"][1]["verify"],
        json!(["cargo test -p vtcode-core task_tracker", "cargo check -p vtcode"])
    );
}

#[tokio::test]
async fn scope_review_survives_resume_bulk_sync_and_global_mirror() {
    let (temp, state, tool) = setup_planning_workflow().await;
    tool.execute(json!({
        "action": "create", "items": [{
            "description": "Review final scope", "files": ["README.md"],
            "scope_review": ["git status --porcelain", "git diff --stat HEAD -- README.md"]
        }]
    }))
    .await
    .expect("create scope tracker");
    let resumed = PlanningTaskTrackerTool::new(state);
    let updated = resumed
        .execute(json!({
            "action": "update", "items": ["[x] Review final scope"]
        }))
        .await
        .expect("bulk status update");
    assert_eq!(
        updated["checklist"]["items"][0]["scope_review"],
        json!(["git status --porcelain", "git diff --stat HEAD -- README.md"])
    );
    assert_eq!(updated["checklist"]["items"][0]["verify"], json!([]));
    for path in [".vtcode/plans/test-plan.tasks.md", ".vtcode/tasks/current_task.md"] {
        let persisted = std::fs::read_to_string(temp.path().join(path)).expect("persisted tracker");
        assert!(persisted.contains("[x] Review final scope"));
        assert_eq!(persisted.matches("  scope-review:").count(), 2);
        assert!(!persisted.contains("  verify:"));
    }
}

#[tokio::test]
async fn add_and_update_nested_item() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Parent task"]
    }))
    .await
    .expect("create tracker");

    tool.execute(json!({
        "action": "add",
        "parent_index_path": "1",
        "description": "Child task"
    }))
    .await
    .expect("add nested task");

    let updated = tool
        .execute(json!({
            "action": "update",
            "index_path": "1.1",
            "status": "completed"
        }))
        .await
        .expect("update nested task");

    assert_eq!(updated["status"], "updated");
    assert_eq!(updated["checklist"]["completed"], 1);
}

#[tokio::test]
async fn persistence_across_instances() {
    let (_temp_dir, state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Persisted step"]
    }))
    .await
    .expect("create tracker");

    tool.execute(json!({
        "action": "update",
        "index_path": "1",
        "status": "completed"
    }))
    .await
    .expect("update tracker");

    let tool2 = PlanningTaskTrackerTool::new(state);
    let listed = tool2.execute(json!({"action": "list"})).await.expect("list tracker");

    assert_eq!(listed["status"], "ok");
    assert_eq!(listed["checklist"]["completed"], 1);
}

#[tokio::test]
async fn update_supports_bulk_item_sync_and_global_mirror() {
    let (temp_dir, _state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Step 1", "Step 2"]
    }))
    .await
    .expect("create tracker");

    let updated = tool
        .execute(json!({
            "action": "update",
            "items": ["[x] Step 1", "[~] Step 2", "[ ] Step 3"]
        }))
        .await
        .expect("bulk update");

    assert_eq!(updated["status"], "updated");
    assert_eq!(updated["checklist"]["completed"], 1);
    assert_eq!(updated["checklist"]["in_progress"], 1);
    assert_eq!(updated["checklist"]["pending"], 1);

    let mirrored = temp_dir.path().join(".vtcode").join("tasks").join("current_task.md");
    let mirrored_content = std::fs::read_to_string(mirrored).expect("read mirrored checklist");
    assert!(mirrored_content.contains("Step 3"));
}

#[tokio::test]
async fn update_accepts_flat_index_fallback() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Parent task"]
    }))
    .await
    .expect("create tracker");

    let updated = tool
        .execute(json!({
            "action": "update",
            "index": 1,
            "status": "completed"
        }))
        .await
        .expect("flat-index update");

    assert_eq!(updated["status"], "updated");
    assert_eq!(updated["checklist"]["completed"], 1);
}

#[tokio::test]
async fn update_rejects_zero_flat_index_even_for_checklist_completion() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Parent task"]
    }))
    .await
    .expect("create tracker");

    let error = tool
        .execute(json!({
            "action": "update",
            "index": 0,
            "status": "completed"
        }))
        .await
        .expect_err("planning index zero must be rejected");

    assert!(error.to_string().contains("index_path components must be >= 1"));
}

#[tokio::test]
async fn planning_task_tracker_schema_requires_positive_indices() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;
    let schema = tool.parameter_schema().expect("planning task tracker schema");

    assert_eq!(schema["properties"]["index"]["minimum"], 1);
    let items_description = schema["properties"]["items"]["description"].as_str().unwrap();
    assert!(items_description.contains("Full checklist replacement"));
    assert!(items_description.contains("never JSON-encoded strings"));
    assert!(items_description.contains("2-space indentation"));
    assert_eq!(schema["properties"]["index_path"]["pattern"], "^[1-9][0-9]*(\\.[1-9][0-9]*)*$");
    assert!(
        schema["properties"]["index_path"]["description"]
            .as_str()
            .expect("index path description")
            .contains("positive")
    );
}

#[tokio::test]
async fn planning_task_tracker_schema_rejects_mixed_bulk_and_single_updates() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;
    let schema = tool.parameter_schema().expect("planning task tracker schema");
    let invalid_cases = [
        json!({"action": "list", "index": 1}),
        json!({"action": "list", "index_path": "1"}),
        json!({"action": "create", "index": 1, "items": ["New item"]}),
        json!({"action": "add", "index_path": "1", "description": "New item"}),
        json!({"action": "update", "items": ["Done"], "index": 1, "status": "completed"}),
        json!({"action": "update", "items": ["Done"], "index_path": "1.1", "status": "completed"}),
        json!({"action": "update", "index": 1, "index_path": "1.1", "status": "completed"}),
        json!({"action": "update", "items": ["Done"], "status": "completed"}),
    ];

    for args in invalid_cases {
        assert!(jsonschema::validate(&schema, &args).is_err(), "expected invalid args: {args}");
    }
}

#[tokio::test]
async fn bulk_update_rejects_encoded_commands_without_resetting_completed_steps() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;
    tool.execute(json!({
        "action": "create",
        "title": "Preserve progress",
        "items": [
            {"description": "Update introduction", "status": "completed", "files": ["README.md"], "outcome": "Clear overview", "verify": ["review headings"]},
            {"description": "Review links", "status": "blocked", "outcome": "Await network"}
        ]
    })).await.unwrap();
    let tracker_path = _temp_dir.path().join(".vtcode/tasks/current_task.md");
    let persisted_before = std::fs::read_to_string(&tracker_path).unwrap();
    let before = tool.execute(json!({"action": "list"})).await.unwrap();

    for (action, encoded) in [
        ("update", r#"{"index_path":"1","status":"completed","outcome":"Done","verify":"review headings"}"#),
        ("update", r#"  [x] {"index_path":"1","status":"completed"}"#),
        ("create", r#"{"index":1,"status":"completed"}"#),
    ] {
        let error = tool
            .execute(json!({
                "action": action,
                "title": "Incorrect replacement",
                "items": ["Replacement must not leak", encoded]
            }))
            .await
            .expect_err("encoded updates must not replace the tracker");
        assert!(error.to_string().contains("JSON-encoded task updates"));
        assert!(
            error
                .to_string()
                .contains(r#"{"action":"update","index_path":"1","status":"completed"}"#)
        );
        assert_eq!(tool.execute(json!({"action": "list"})).await.unwrap(), before);
        assert_eq!(std::fs::read_to_string(&tracker_path).unwrap(), persisted_before);
    }

    tool.execute(json!({"action": "update", "index_path": "2", "status": "completed"}))
        .await
        .unwrap();
    let after = tool.execute(json!({"action": "list"})).await.unwrap();
    assert_eq!(after["checklist"]["completed"], 2);
    assert_eq!(after["checklist"]["items"][0]["description"], "Update introduction");
    assert_eq!(after["checklist"]["items"][0]["files"], json!(["README.md"]));
}

#[tokio::test]
async fn planning_update_rejects_mixed_bulk_and_single_fields_before_mutation() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Original"]
    }))
    .await
    .expect("create tracker");

    let error = tool
        .execute(json!({
            "action": "update",
            "items": ["Replacement"],
            "index_path": "1",
            "status": "completed"
        }))
        .await
        .expect_err("mixed update must fail closed");
    assert!(error.to_string().contains("cannot combine 'items'"));

    let result = tool.execute(json!({"action": "list"})).await.unwrap();
    assert_eq!(result["checklist"]["items"][0]["description"], "Original");
    assert_eq!(result["checklist"]["items"][0]["status"], "pending");
}

#[tokio::test]
async fn planning_update_rejects_both_index_forms_before_mutation() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    tool.execute(json!({
        "action": "create",
        "items": ["Original"]
    }))
    .await
    .expect("create tracker");

    let error = tool
        .execute(json!({
            "action": "update",
            "index": 1,
            "index_path": "1",
            "status": "completed"
        }))
        .await
        .expect_err("ambiguous index forms must fail closed");
    assert!(error.to_string().contains("cannot combine 'index' and 'index_path'"));

    let result = tool.execute(json!({"action": "list"})).await.unwrap();
    assert_eq!(result["checklist"]["items"][0]["description"], "Original");
    assert_eq!(result["checklist"]["items"][0]["status"], "pending");
}

#[tokio::test]
async fn execute_rejects_indices_for_non_update_actions() {
    let (_temp_dir, _state, tool) = setup_planning_workflow().await;

    let invalid_inputs = [
        json!({"action": "list", "index": 1}),
        json!({"action": "create", "index_path": "1", "items": ["New item"]}),
        json!({"action": "add", "index": 1, "description": "New item"}),
        json!({"action": "unexpected", "index_path": "1"}),
    ];

    for input in invalid_inputs {
        let error = tool.execute(input).await.expect_err("non-update index must fail closed");
        assert!(error.to_string().contains("cannot use 'index' or 'index_path'"), "unexpected error: {error}");
    }
}

#[tokio::test]
async fn rejects_when_planning_workflow_is_inactive() {
    let temp_dir = TempDir::new().expect("temp dir");
    let state = PlanningWorkflowState::new(temp_dir.path().to_path_buf());
    let tool = PlanningTaskTrackerTool::new(state);

    let err = tool
        .execute(json!({"action": "list"}))
        .await
        .expect_err("should fail outside planning workflow");

    assert!(err.to_string().contains("only available while planning"));
}