vtcode-core 0.136.7

Core library for VT Code - a Rust-based terminal coding agent
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Pure builders for prefire two-pass compaction.
//!
//! Pass1 summarizes ~95% of history (by estimated-token weight) -> NOTE₁.
//! Pass2 rewrites NOTE₁ + the ~5% tail into the successor-visible NOTE₂.
//! Sampling lives in [`super`]; this module has no I/O.

use crate::llm::provider::Message;

/// Default history fraction covered by pass1; the remainder is the blocking
/// pass2 tail, so keep it small (prod pass2 latency is dominated by tail prefill).
pub const TWO_PASS_DEFAULT_SPLIT_FRACTION: f64 = 0.95;

/// Minimum char length for a closed `<summary>` block to be preferred as NOTE₁
/// over the full pass1 response.
const TWO_PASS_MIN_SUMMARY_BLOCK_CHARS: usize = 1000;

/// Cap on NOTE₁ text embedded in pass2 (carrier + special turn).
const TWO_PASS_MAX_NOTE1_CHARS: usize = 12_000;

/// Result of splitting a conversation for two-pass compaction.
#[derive(Debug, Clone, Copy)]
pub struct TwoPassSplit<'a> {
    pub prefix: &'a [Message],
    pub tail: &'a [Message],
    pub split_idx: usize,
}

/// Choose a split index so prefix weight is at least `fraction` of total.
#[allow(clippy::cast_sign_loss)]
fn split_index_by_token_fraction(weights: &[usize], fraction: f64) -> usize {
    if weights.is_empty() {
        return 0;
    }
    let frac = fraction.clamp(0.05, 0.95);
    let total_w: usize = weights.iter().copied().sum::<usize>().max(1);
    let target_w = (frac * total_w as f64).round() as u64 as usize;
    let mut acc = 0usize;
    let mut split_idx = weights.len().saturating_sub(1).max(1);
    for (i, w) in weights.iter().enumerate() {
        acc = acc.saturating_add(*w);
        if acc >= target_w {
            split_idx = (i + 1).max(1);
            break;
        }
    }
    if split_idx >= weights.len() && weights.len() > 1 {
        split_idx = weights.len() - 1;
    }
    split_idx
}

/// Never separate an assistant `tool_calls` turn from its following `tool_call_id` results.
fn snap_split_idx_to_tool_boundaries(conversation: &[Message], mut split_idx: usize) -> usize {
    let n = conversation.len();
    if n == 0 {
        return 0;
    }
    split_idx = split_idx.min(n);

    while split_idx < n {
        let Some(msg) = conversation.get(split_idx) else {
            break;
        };
        if msg.tool_call_id.is_some() {
            split_idx += 1;
        } else {
            break;
        }
    }
    if split_idx < n
        && let Some(msg) = conversation.get(split_idx)
        && msg.tool_calls.is_some()
        && !msg.tool_calls.as_ref().unwrap().is_empty()
    {
        split_idx += 1;
        while split_idx < n {
            let Some(msg) = conversation.get(split_idx) else {
                break;
            };
            if msg.tool_call_id.is_some() {
                split_idx += 1;
            } else {
                break;
            }
        }
    }
    while split_idx > 0 && split_idx < n {
        let Some(msg) = conversation.get(split_idx - 1) else {
            break;
        };
        if msg.tool_calls.is_some() && !msg.tool_calls.as_ref().unwrap().is_empty() {
            if conversation.get(split_idx).map(|m| m.tool_call_id.is_some()) != Some(true) {
                break;
            }
            while split_idx < n {
                let Some(msg) = conversation.get(split_idx) else {
                    break;
                };
                if msg.tool_call_id.is_some() {
                    split_idx += 1;
                } else {
                    break;
                }
            }
        } else {
            break;
        }
    }

    if split_idx >= n && n > 1 {
        let mut candidate = n - 1;
        while candidate > 1 {
            if let Some(msg) = conversation.get(candidate) {
                if msg.tool_call_id.is_none() {
                    break;
                }
                candidate -= 1;
            } else {
                break;
            }
        }
        if candidate > 0
            && let Some(msg) = conversation.get(candidate)
            && msg.tool_calls.is_some()
            && !msg.tool_calls.as_ref().unwrap().is_empty()
        {
            if conversation.get(candidate + 1).map(|m| m.tool_call_id.is_some()) == Some(true) {
                split_idx = candidate;
            }
        } else if candidate > 0 && conversation.get(candidate).map(|m| m.tool_call_id.is_some()) == Some(true) {
            let mut i = candidate;
            while i > 0 && conversation.get(i - 1).map(|m| m.tool_call_id.is_some()) == Some(true) {
                i -= 1;
            }
            if conversation
                .get(i)
                .map(|m| m.tool_calls.is_some() && !m.tool_calls.as_ref().unwrap().is_empty())
                == Some(true)
            {
                split_idx = i;
            }
        }
        if candidate >= 1 && candidate < n {
            split_idx = candidate;
        }
    }

    split_idx.min(n)
}

/// Split `conversation` into pass1 prefix / pass2 tail by estimated-token weight.
pub fn split_conversation_for_two_pass(conversation: &[Message], split_fraction: f64) -> TwoPassSplit<'_> {
    let weights: Vec<usize> = conversation.iter().map(|m| m.estimate_tokens()).collect();
    let mut split_idx = split_index_by_token_fraction(&weights, split_fraction);
    split_idx = snap_split_idx_to_tool_boundaries(conversation, split_idx);
    let split_idx = split_idx.min(conversation.len());
    TwoPassSplit {
        prefix: &conversation[..split_idx],
        tail: &conversation[split_idx..],
        split_idx,
    }
}

fn extract_summary_block(text: &str, min_chars: usize) -> Option<String> {
    if text.is_empty() {
        return None;
    }
    let open = "<summary>";
    let close = "</summary>";
    let lower = text.to_ascii_lowercase();
    let mut blocks: Vec<String> = Vec::new();
    let text_bytes = text.as_bytes();
    let lower_bytes = lower.as_bytes();
    let mut search_from = 0usize;
    while search_from < lower_bytes.len() {
        let Some(rel) = find_bytes(&lower_bytes[search_from..], open.as_bytes()) else {
            break;
        };
        let start = search_from + rel + open.len();
        let Some(rel_close) = find_bytes(&lower_bytes[start..], close.as_bytes()) else {
            break;
        };
        let end = start + rel_close;
        let inner = std::str::from_utf8(&text_bytes[start..end]).unwrap_or("");
        blocks.push(inner.to_string());
        search_from = end + close.len();
    }
    for block in blocks.into_iter().rev() {
        let stripped = block.trim();
        if stripped.chars().count() > min_chars {
            return Some(stripped.to_string());
        }
    }
    None
}

fn find_bytes(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack.windows(needle.len()).position(|w| w == needle)
}

/// Prefer a substantive `<summary>` inner for NOTE₁; otherwise the full pass1 response.
pub fn note_for_two_pass_pass2(pass1_raw: &str) -> String {
    let mut note = extract_summary_block(pass1_raw, TWO_PASS_MIN_SUMMARY_BLOCK_CHARS)
        .unwrap_or_else(|| pass1_raw.trim().to_string());
    let n = note.chars().count();
    if n > TWO_PASS_MAX_NOTE1_CHARS {
        note = note.chars().take(TWO_PASS_MAX_NOTE1_CHARS).collect();
        note.push_str("\n\n[… NOTE₁ truncated for pass2 input budget …]");
    }
    note
}

fn format_two_pass_note1_carrier(note1: &str) -> String {
    let note1 = note1.trim();
    format!(
        "Your conversation was summarized due to context constraints. \
         Here is the summary of the conversation so far:\n\n\
         <summary_content>\n{note1}\n</summary_content>\n\n\
         Continue with the compaction task below."
    )
}

fn format_two_pass_special_pass2_user(note1: &str, compaction_prompt: &str) -> String {
    let note1 = note1.trim();
    let summary_block = format!("<summary_content>\n{note1}\n</summary_content>");
    let uq = if compaction_prompt.trim().is_empty() {
        "Please summarize the conversation so far."
    } else {
        compaction_prompt
    };
    format!(
        "This is a special compaction case (two-pass / hierarchical summarization).\n\
         You are writing the *final* compaction note that a successor assistant will \
         rely on as their only memory of the conversation.\n\n\
         Critical requirements:\n\
         - Incorporate the **entire** prior summary below into your final note — do not \
         omit sections, defer to \"see prior compaction\", or drop early history because \
         newer turns are in context.\n\
         - Merge that prior summary with the more recent conversation turns above into \
         one coherent, faithful, self-contained summary (same structure/sections you \
         normally use for compaction).\n\
         - Preserve concrete values, file paths, errors/blockers, operational how-tos, \
         key findings, and pending tasks from *both* the prior summary and the recent \
         turns when they still matter.\n\n\
         Prior summary to incorporate in full (duplicate of the summary_content above):\n\n\
         {summary_block}\n\n\
         Compaction instruction:\n\
         {uq}"
    )
}

/// Pass1 sample history: `prefix` + compaction instruction user turn.
pub fn build_two_pass_pass1_history(prefix: &[Message], compaction_prompt: &str) -> Vec<Message> {
    let mut history = prefix.to_vec();
    history.push(Message {
        role: crate::llm::provider::MessageRole::User,
        content: crate::llm::provider::MessageContent::Text(compaction_prompt.to_string()),
        reasoning: None,
        reasoning_details: None,
        tool_calls: None,
        tool_call_id: None,
        phase: None,
        origin_tool: None,
        metadata: None,
    });
    history
}

/// Pass2 sample history: system (from prefix) + NOTE₁ carrier + tail + special turn.
///
/// Successor-visible artifact is the model output of *this* history only (NOTE₂).
pub fn build_two_pass_pass2_history(
    prefix: &[Message],
    tail: &[Message],
    note1: &str,
    compaction_prompt: &str,
) -> Vec<Message> {
    let mut history: Vec<Message> = Vec::new();

    for item in prefix {
        if item.role == crate::llm::provider::MessageRole::System {
            history.push(item.clone());
        }
    }
    if history.is_empty() {
        history.push(Message {
            role: crate::llm::provider::MessageRole::System,
            content: crate::llm::provider::MessageContent::Text("You are a helpful assistant.".to_string()),
            reasoning: None,
            reasoning_details: None,
            tool_calls: None,
            tool_call_id: None,
            phase: None,
            origin_tool: None,
            metadata: None,
        });
    }

    history.push(Message {
        role: crate::llm::provider::MessageRole::User,
        content: crate::llm::provider::MessageContent::Text(format_two_pass_note1_carrier(note1)),
        reasoning: None,
        reasoning_details: None,
        tool_calls: None,
        tool_call_id: None,
        phase: None,
        origin_tool: None,
        metadata: None,
    });
    history.extend(tail.iter().cloned());
    history.push(Message {
        role: crate::llm::provider::MessageRole::User,
        content: crate::llm::provider::MessageContent::Text(format_two_pass_special_pass2_user(
            note1,
            compaction_prompt,
        )),
        reasoning: None,
        reasoning_details: None,
        tool_calls: None,
        tool_call_id: None,
        phase: None,
        origin_tool: None,
        metadata: None,
    });
    history
}

use std::hash::{Hash, Hasher};

/// Cheap fingerprint of a conversation prefix for prefire NOTE₁ validity.
/// A mismatch means the prefix changed (edit / rewind / branch) since pass-1,
/// so the cached NOTE₁ no longer summarizes the current prefix.
pub fn fingerprint_prefix(prefix: &[Message]) -> u64 {
    use std::collections::hash_map::DefaultHasher;
    let mut h = DefaultHasher::new();
    prefix.len().hash(&mut h);
    for msg in prefix {
        let tag: u8 = match msg.role {
            crate::llm::provider::MessageRole::System => 0,
            crate::llm::provider::MessageRole::User => 1,
            crate::llm::provider::MessageRole::Assistant => 2,
            crate::llm::provider::MessageRole::Tool => 3,
        };
        tag.hash(&mut h);
        msg.content.as_text().hash(&mut h);
    }
    h.finish()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::llm::provider::{FunctionCall, MessageRole, ToolCall};

    fn make_tool_call(id: &str, name: &str) -> Option<Vec<ToolCall>> {
        Some(vec![ToolCall {
            id: id.to_string(),
            call_type: "function".to_string(),
            function: Some(FunctionCall {
                namespace: None,
                name: name.to_string(),
                arguments: "{}".to_string(),
            }),
            text: None,
            thought_signature: None,
        }])
    }

    #[test]
    fn split_leaves_tail_when_possible() {
        let weights = vec![10usize, 10, 10, 10, 10];
        let idx = split_index_by_token_fraction(&weights, 0.9);
        assert!(idx < weights.len());
        assert!(idx >= 1);
    }

    #[test]
    fn default_split_fraction_leaves_five_percent_tail() {
        assert!((TWO_PASS_DEFAULT_SPLIT_FRACTION - 0.95).abs() < f64::EPSILON);
        let weights = vec![10usize; 40];
        let idx = split_index_by_token_fraction(&weights, TWO_PASS_DEFAULT_SPLIT_FRACTION);
        assert_eq!(idx, 38);
        assert!(idx < weights.len());
    }

    #[test]
    fn split_does_not_sever_tool_pairs() {
        let assistant = Message {
            role: MessageRole::Assistant,
            content: crate::llm::provider::MessageContent::Text("call".to_string()),
            reasoning: None,
            reasoning_details: None,
            tool_calls: make_tool_call("tc1", "bash"),
            tool_call_id: None,
            phase: None,
            origin_tool: None,
            metadata: None,
        };
        let conv = vec![
            Message {
                role: MessageRole::User,
                content: crate::llm::provider::MessageContent::Text("a".repeat(400)),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            Message {
                role: MessageRole::Assistant,
                content: crate::llm::provider::MessageContent::Text("b".repeat(400)),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            assistant.clone(),
            Message {
                role: MessageRole::Tool,
                content: crate::llm::provider::MessageContent::Text("ok".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: Some("tc1".to_string()),
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            Message {
                role: MessageRole::User,
                content: crate::llm::provider::MessageContent::Text("tail".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
        ];
        let split = split_conversation_for_two_pass(&conv, 0.9);
        if let Some(msg) = split.prefix.last() {
            if msg.tool_calls.is_some() && !msg.tool_calls.as_ref().unwrap().is_empty() {
                assert!(!matches!(split.tail.first(), Some(m) if m.tool_call_id.is_some()));
            }
        }
        assert!(!matches!(split.tail.first(), Some(m) if m.tool_call_id.is_some()));
        let prefix_has_call = split.prefix.iter().any(|m| {
            m.tool_calls
                .as_ref()
                .map(|tc| tc.iter().any(|t| t.id == "tc1"))
                .unwrap_or(false)
        });
        let prefix_has_result = split.prefix.iter().any(|m| m.tool_call_id.as_deref() == Some("tc1"));
        let tail_has_call = split.tail.iter().any(|m| {
            m.tool_calls
                .as_ref()
                .map(|tc| tc.iter().any(|t| t.id == "tc1"))
                .unwrap_or(false)
        });
        let tail_has_result = split.tail.iter().any(|m| m.tool_call_id.as_deref() == Some("tc1"));
        assert_eq!(prefix_has_call, prefix_has_result);
        assert_eq!(tail_has_call, tail_has_result);
    }

    #[test]
    fn note_prefers_summary_block_and_caps_huge_raw() {
        let text = format!("<summary>short</summary>\n<summary>\n{}\n</summary>", "x".repeat(1001));
        assert_eq!(note_for_two_pass_pass2(&text), "x".repeat(1001));
        assert_eq!(note_for_two_pass_pass2("no tags"), "no tags");
        let huge = "n".repeat(TWO_PASS_MAX_NOTE1_CHARS + 500);
        let note = note_for_two_pass_pass2(&huge);
        assert!(note.chars().count() <= TWO_PASS_MAX_NOTE1_CHARS + 80);
        assert!(note.contains("truncated"));
    }

    #[test]
    fn pass_histories_shape() {
        let conv = vec![
            Message {
                role: MessageRole::System,
                content: crate::llm::provider::MessageContent::Text("You are VT Code.".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            Message {
                role: MessageRole::User,
                content: crate::llm::provider::MessageContent::Text("early".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            Message {
                role: MessageRole::Assistant,
                content: crate::llm::provider::MessageContent::Text("early-a".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            Message {
                role: MessageRole::User,
                content: crate::llm::provider::MessageContent::Text("late".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
            Message {
                role: MessageRole::Assistant,
                content: crate::llm::provider::MessageContent::Text("late-a".to_string()),
                reasoning: None,
                reasoning_details: None,
                tool_calls: None,
                tool_call_id: None,
                phase: None,
                origin_tool: None,
                metadata: None,
            },
        ];
        let split = split_conversation_for_two_pass(&conv, 0.5);
        let prompt = "1. Primary Request and Intent: x\n5. Optional Next Step: y\n";
        let pass1 = build_two_pass_pass1_history(split.prefix, prompt);
        assert!(matches!(pass1.last(), Some(m) if m.role == MessageRole::User));

        let note1 = "x".repeat(1001);
        let pass2 = build_two_pass_pass2_history(split.prefix, split.tail, &note1, prompt);
        assert!(pass2.iter().any(|m| m.role == MessageRole::System));
        let texts: Vec<String> = pass2
            .iter()
            .map(|m| match &m.content {
                crate::llm::provider::MessageContent::Text(t) => t.clone(),
                crate::llm::provider::MessageContent::Parts(parts) => parts
                    .iter()
                    .filter_map(|p| match p {
                        crate::llm::provider::ContentPart::Text { text } => Some(text.clone()),
                        _ => None,
                    })
                    .collect(),
            })
            .collect();
        assert!(texts.iter().any(|t| t.contains("<summary_content>")));
        assert!(texts.last().is_some_and(|t| t.contains("special compaction case")));
    }
}