vtcode-core 0.136.2

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
//! Loop memory store for durable loop-engineering state.
//!
//! The loop memory store provides append-only markdown files that the agent
//! writes during a loop run and reads on the next iteration. This is the
//! "memory" primitive from Osmani's loop engineering pattern:
//!
//! > "The agent forgets, the repo doesn't."
//!
//! The default implementation writes to `{workspace}/.vtcode/state/notes.md`
//! and `{workspace}/.vtcode/state/decisions.md`.

use anyhow::{Context, Result};
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};

use crate::loop_state::state_dir;

const NOTES_FILENAME: &str = "notes.md";
const DECISIONS_FILENAME: &str = "decisions.md";

// ─── Trait ───────────────────────────────────────────────────────────────────

/// A trait for persisting loop-level memory across runs. Implementations may
/// store notes and decisions in markdown, a database, or any other durable form.
pub trait LoopMemoryStore: Send + Sync {
    /// Read all accumulated notes.
    fn read_notes(&self) -> Result<String>;

    /// Append a note. Notes are agent-written, human-readable, append-only.
    fn write_note(&self, note: &str) -> Result<()>;

    /// Read all accumulated decisions.
    fn read_decisions(&self) -> Result<String>;

    /// Append a decision entry. Decisions are the agent's choices that should
    /// survive across loop iterations.
    fn write_decision(&self, decision: &str) -> Result<()>;
}

// ─── Markdown Implementation ─────────────────────────────────────────────────

/// Markdown-backed loop memory store. Writes to `.vtcode/state/notes.md` and
/// `.vtcode/state/decisions.md` under the workspace root.
pub struct MarkdownLoopMemory {
    notes_path: PathBuf,
    decisions_path: PathBuf,
}

impl MarkdownLoopMemory {
    /// Create a new markdown loop memory store for the given workspace.
    pub fn new(workspace_root: &Path) -> Self {
        let dir = state_dir(workspace_root);
        Self {
            notes_path: dir.join(NOTES_FILENAME),
            decisions_path: dir.join(DECISIONS_FILENAME),
        }
    }

    /// Create a new markdown loop memory store with explicit paths (for testing).
    pub fn with_paths(notes_path: PathBuf, decisions_path: PathBuf) -> Self {
        Self { notes_path, decisions_path }
    }
}

impl LoopMemoryStore for MarkdownLoopMemory {
    fn read_notes(&self) -> Result<String> {
        read_markdown_file(&self.notes_path)
    }

    fn write_note(&self, note: &str) -> Result<()> {
        append_markdown_entry(&self.notes_path, note, "# Loop Notes\n\n")
    }

    fn read_decisions(&self) -> Result<String> {
        read_markdown_file(&self.decisions_path)
    }

    fn write_decision(&self, decision: &str) -> Result<()> {
        append_markdown_entry(&self.decisions_path, decision, "# Loop Decisions\n\n")
    }
}

// ─── Helpers ─────────────────────────────────────────────────────────────────

fn read_markdown_file(path: &Path) -> Result<String> {
    if !path.exists() {
        return Ok(String::new());
    }
    fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display()))
}

fn append_markdown_entry(path: &Path, content: &str, header: &str) -> Result<()> {
    let trimmed = content.trim();
    if trimmed.is_empty() {
        return Ok(());
    }

    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("Failed to create directory {}", parent.display()))?;
    }

    let timestamp = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ");
    let entry = format!("- [{timestamp}] {trimmed}\n");

    // Open a single file handle with read+append+create to avoid TOCTOU race
    // between checking whether the header exists and writing the entry.
    let mut file = OpenOptions::new()
        .read(true)
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("Failed to open {} for appending", path.display()))?;

    // Check if the file is empty (needs a header) using the same handle.
    let needs_header = {
        let metadata = file.metadata().with_context(|| format!("Failed to stat {}", path.display()))?;
        metadata.len() == 0
    };

    if needs_header {
        file.write_all(header.as_bytes())
            .with_context(|| format!("Failed to write header to {}", path.display()))?;
    }

    file.write_all(entry.as_bytes())
        .with_context(|| format!("Failed to append to {}", path.display()))?;
    Ok(())
}

// ─── Sqlite Implementation ───────────────────────────────────────────────────

/// Sqlite-backed loop memory store for faster queries. Enabled behind the
/// `sqlite` feature flag. Stores notes and decisions in a single database
/// file under `.vtcode/state/loop_memory.db`.
#[cfg(feature = "sqlite")]
pub struct SqliteLoopMemory {
    conn: std::sync::Mutex<rusqlite::Connection>,
}

#[cfg(feature = "sqlite")]
impl SqliteLoopMemory {
    /// Create a new sqlite loop memory store for the given workspace.
    pub fn new(workspace_root: &Path) -> Result<Self> {
        let dir = state_dir(workspace_root);
        let db_path = dir.join("loop_memory.db");
        Self::from_path(db_path)
    }

    /// Create with an explicit database path (for testing).
    pub fn with_path(db_path: PathBuf) -> Result<Self> {
        Self::from_path(db_path)
    }

    fn from_path(db_path: PathBuf) -> Result<Self> {
        let conn = rusqlite::Connection::open(&db_path)?;
        conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS notes (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp TEXT NOT NULL,
                content TEXT NOT NULL
            );
            CREATE TABLE IF NOT EXISTS decisions (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp TEXT NOT NULL,
                content TEXT NOT NULL
            );",
        )?;
        Ok(Self { conn: std::sync::Mutex::new(conn) })
    }
}

#[cfg(feature = "sqlite")]
impl LoopMemoryStore for SqliteLoopMemory {
    fn read_notes(&self) -> Result<String> {
        let conn = self.conn.lock().map_err(|e| anyhow::anyhow!("Lock poisoned: {e}"))?;
        let mut stmt = conn.prepare("SELECT timestamp, content FROM notes ORDER BY id")?;
        let rows = stmt.query_map([], |row| {
            let ts: String = row.get(0)?;
            let content: String = row.get(1)?;
            Ok(format!("- [{ts}] {content}"))
        })?;

        let entries: Vec<String> = rows.collect::<Result<Vec<_>, _>>()?;
        // Match Markdown behavior: return empty string when no entries exist.
        if entries.is_empty() {
            return Ok(String::new());
        }
        let mut output = String::from("# Loop Notes\n\n");
        for entry in entries {
            output.push_str(&entry);
            output.push('\n');
        }
        Ok(output)
    }

    fn write_note(&self, note: &str) -> Result<()> {
        let trimmed = note.trim();
        if trimmed.is_empty() {
            return Ok(());
        }
        let timestamp = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ");
        let conn = self.conn.lock().map_err(|e| anyhow::anyhow!("Lock poisoned: {e}"))?;
        conn.execute(
            "INSERT INTO notes (timestamp, content) VALUES (?1, ?2)",
            rusqlite::params![timestamp.to_string(), trimmed],
        )?;
        Ok(())
    }

    fn read_decisions(&self) -> Result<String> {
        let conn = self.conn.lock().map_err(|e| anyhow::anyhow!("Lock poisoned: {e}"))?;
        let mut stmt = conn.prepare("SELECT timestamp, content FROM decisions ORDER BY id")?;
        let rows = stmt.query_map([], |row| {
            let ts: String = row.get(0)?;
            let content: String = row.get(1)?;
            Ok(format!("- [{ts}] {content}"))
        })?;

        let entries: Vec<String> = rows.collect::<Result<Vec<_>, _>>()?;
        if entries.is_empty() {
            return Ok(String::new());
        }
        let mut output = String::from("# Loop Decisions\n\n");
        for entry in entries {
            output.push_str(&entry);
            output.push('\n');
        }
        Ok(output)
    }

    fn write_decision(&self, decision: &str) -> Result<()> {
        let trimmed = decision.trim();
        if trimmed.is_empty() {
            return Ok(());
        }
        let timestamp = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ");
        let conn = self.conn.lock().map_err(|e| anyhow::anyhow!("Lock poisoned: {e}"))?;
        conn.execute(
            "INSERT INTO decisions (timestamp, content) VALUES (?1, ?2)",
            rusqlite::params![timestamp.to_string(), trimmed],
        )?;
        Ok(())
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn markdown_memory_read_empty_when_no_files() {
        let tmp = TempDir::new().expect("temp dir");
        let memory = MarkdownLoopMemory::new(tmp.path());
        assert_eq!(memory.read_notes().expect("read"), "");
        assert_eq!(memory.read_decisions().expect("read"), "");
    }

    #[test]
    fn markdown_memory_write_and_read_notes() {
        let tmp = TempDir::new().expect("temp dir");
        let memory = MarkdownLoopMemory::new(tmp.path());

        memory.write_note("First observation").expect("write");
        memory.write_note("Second observation").expect("write");

        let notes = memory.read_notes().expect("read");
        assert!(notes.contains("First observation"));
        assert!(notes.contains("Second observation"));
        assert!(notes.starts_with("# Loop Notes"));
    }

    #[test]
    fn markdown_memory_write_and_read_decisions() {
        let tmp = TempDir::new().expect("temp dir");
        let memory = MarkdownLoopMemory::new(tmp.path());

        memory.write_decision("Use retry with backoff").expect("write");
        memory.write_decision("Skip tests for now").expect("write");

        let decisions = memory.read_decisions().expect("read");
        assert!(decisions.contains("Use retry with backoff"));
        assert!(decisions.contains("Skip tests for now"));
        assert!(decisions.starts_with("# Loop Decisions"));
    }

    #[test]
    fn markdown_memory_ignores_empty_entries() {
        let tmp = TempDir::new().expect("temp dir");
        let memory = MarkdownLoopMemory::new(tmp.path());

        memory.write_note("").expect("write");
        memory.write_note("   ").expect("write");

        let notes = memory.read_notes().expect("read");
        assert_eq!(notes, "");
    }

    #[test]
    fn markdown_memory_entries_are_timestamped() {
        let tmp = TempDir::new().expect("temp dir");
        let notes_path = tmp.path().join("notes.md");
        let decisions_path = tmp.path().join("decisions.md");
        let memory = MarkdownLoopMemory::with_paths(notes_path.clone(), decisions_path);

        memory.write_note("test entry").expect("write");

        let content = fs::read_to_string(&notes_path).expect("read");
        // Should contain a timestamp in [YYYY-MM-DDTHH:MM:SSZ] format
        assert!(content.contains("[20"));
        assert!(content.contains("] test entry"));
    }

    #[test]
    fn markdown_memory_append_is_durable() {
        let tmp = TempDir::new().expect("temp dir");
        let notes_path = tmp.path().join("notes.md");
        let decisions_path = tmp.path().join("decisions.md");

        {
            let memory = MarkdownLoopMemory::with_paths(notes_path.clone(), decisions_path.clone());
            memory.write_note("first run note").expect("write");
        }
        // Simulate a new run (drop and recreate)
        {
            let memory = MarkdownLoopMemory::with_paths(notes_path, decisions_path);
            memory.write_note("second run note").expect("write");
            let notes = memory.read_notes().expect("read");
            assert!(notes.contains("first run note"));
            assert!(notes.contains("second run note"));
        }
    }

    #[cfg(feature = "sqlite")]
    mod sqlite_tests {
        use super::super::*;
        use tempfile::TempDir;

        #[test]
        fn sqlite_memory_read_empty_when_no_data() {
            let tmp = TempDir::new().expect("temp dir");
            let db_path = tmp.path().join("test.db");
            let memory = SqliteLoopMemory::with_path(db_path).expect("create");
            // Empty state matches Markdown behavior: returns empty string.
            assert_eq!(memory.read_notes().expect("read"), "");
            assert_eq!(memory.read_decisions().expect("read"), "");
        }

        #[test]
        fn sqlite_memory_write_and_read_notes() {
            let tmp = TempDir::new().expect("temp dir");
            let db_path = tmp.path().join("test.db");
            let memory = SqliteLoopMemory::with_path(db_path).expect("create");

            memory.write_note("First observation").expect("write");
            memory.write_note("Second observation").expect("write");

            let notes = memory.read_notes().expect("read");
            assert!(notes.contains("First observation"));
            assert!(notes.contains("Second observation"));
            assert!(notes.starts_with("# Loop Notes"));
        }

        #[test]
        fn sqlite_memory_write_and_read_decisions() {
            let tmp = TempDir::new().expect("temp dir");
            let db_path = tmp.path().join("test.db");
            let memory = SqliteLoopMemory::with_path(db_path).expect("create");

            memory.write_decision("Use retry with backoff").expect("write");
            memory.write_decision("Skip tests for now").expect("write");

            let decisions = memory.read_decisions().expect("read");
            assert!(decisions.contains("Use retry with backoff"));
            assert!(decisions.contains("Skip tests for now"));
            assert!(decisions.starts_with("# Loop Decisions"));
        }

        #[test]
        fn sqlite_memory_ignores_empty_entries() {
            let tmp = TempDir::new().expect("temp dir");
            let db_path = tmp.path().join("test.db");
            let memory = SqliteLoopMemory::with_path(db_path).expect("create");

            memory.write_note("").expect("write");
            memory.write_note("   ").expect("write");

            let notes = memory.read_notes().expect("read");
            // Empty entries are skipped, so the result is empty (no rows).
            assert_eq!(notes, "");
        }

        #[test]
        fn sqlite_memory_survives_reopen() {
            let tmp = TempDir::new().expect("temp dir");
            let db_path = tmp.path().join("test.db");

            {
                let memory = SqliteLoopMemory::with_path(db_path.clone()).expect("create");
                memory.write_note("first run note").expect("write");
            }
            {
                let memory = SqliteLoopMemory::with_path(db_path).expect("reopen");
                memory.write_note("second run note").expect("write");
                let notes = memory.read_notes().expect("read");
                assert!(notes.contains("first run note"));
                assert!(notes.contains("second run note"));
            }
        }
    }
}