rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::{cell::RefCell, sync::mpsc};

use pretty_assertions::assert_eq;
use rho_providers::model::Message;
use tempfile::TempDir;

use super::*;
use crate::{app::row_locations, session::Session};

struct MigrationWait {
    blocked: mpsc::Sender<()>,
    resume: mpsc::Receiver<()>,
}

thread_local! {
    static MIGRATION_WAIT: RefCell<Option<MigrationWait>> = const { RefCell::new(None) };
}

// Covers: a waiting opener must not invalidate evidence rebuilt by the first
// migration. Owner: search index SQLite migration, with actual lock contention.
#[test]
fn concurrent_migration_preserves_rebuilt_cache() {
    let root = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let session = Session::create_in_root(root.path(), cwd.path()).unwrap();
    session
        .append_message(&Message::user_text("migrationneedle"))
        .unwrap();
    let cancellation = CancellationToken::new();
    let mut first = open(root.path()).unwrap();
    refresh(
        &mut first,
        root.path(),
        /*reconcile*/ false,
        row_locations,
        &cancellation,
    )
    .unwrap();
    first.pragma_update(None, "user_version", 1).unwrap();
    let (blocked_tx, blocked_rx) = mpsc::channel();
    let (resume_tx, resume_rx) = mpsc::channel();
    std::thread::scope(|scope| {
        let lock = first
            .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)
            .unwrap();
        let root = root.path();
        let second = scope.spawn(move || {
            MIGRATION_WAIT.with(|wait| {
                *wait.borrow_mut() = Some(MigrationWait {
                    blocked: blocked_tx,
                    resume: resume_rx,
                });
            });
            let mut connection = Connection::open(root.join("search.sqlite3")).unwrap();
            connection
                .busy_handler(Some(|_| {
                    MIGRATION_WAIT.with(|wait| {
                        let Some(wait) = wait.borrow_mut().take() else {
                            return false;
                        };
                        // Use the store's existing lock budget only as a failure
                        // bound. Channels, not elapsed time, order the migration.
                        wait.blocked.send(()).is_ok()
                            && wait
                                .resume
                                .recv_timeout(crate::sqlite_support::BUSY_TIMEOUT)
                                .is_ok()
                    })
                }))
                .unwrap();
            migrate(&mut connection).unwrap();
            refresh(
                &mut connection,
                root,
                /*reconcile*/ false,
                row_locations,
                &CancellationToken::new(),
            )
            .unwrap()
        });
        blocked_rx
            .recv_timeout(crate::sqlite_support::BUSY_TIMEOUT)
            .unwrap();
        lock.rollback().unwrap();
        migrate(&mut first).unwrap();
        let rebuilt = refresh(
            &mut first,
            root,
            /*reconcile*/ false,
            row_locations,
            &cancellation,
        )
        .unwrap();
        assert_eq!(rebuilt.files_updated, 1);
        resume_tx.send(()).unwrap();
        let warm = second.join().unwrap();
        assert_eq!(
            (warm.reconciled, warm.files_checked, warm.bytes_read),
            (false, 0, 0)
        );
        assert_eq!(
            first
                .query_row(
                    "select count(*) from evidence_fts where evidence_fts match 'migrationneedle'",
                    [],
                    |row| row.get::<_, usize>(0)
                )
                .unwrap(),
            1
        );
    });
}

// Covers: the cache is rewritten only when both the absolute and relative
// free-space thresholds hold. Owner: search index vacuum policy.
#[test]
fn vacuum_requires_both_free_space_thresholds() {
    const MIB: u64 = 1024 * 1024;
    let pages = |total_mib: u64, free_mib: u64| Pages {
        size: 4096,
        total: total_mib * MIB / 4096,
        free: free_mib * MIB / 4096,
    };
    let cases = [
        ("small cache, mostly free", pages(60, 40), false),
        ("large cache, small fraction free", pages(1000, 100), false),
        ("both thresholds at the boundary", pages(200, 50), true),
        ("observed bloated cache", pages(331, 165), true),
    ];
    let actual: Vec<_> = cases
        .iter()
        .map(|(name, pages, _)| (*name, pages.worth_vacuuming(VACUUM_MIN_FREE_BYTES)))
        .collect();
    let expected: Vec<_> = cases.iter().map(|(name, _, want)| (*name, *want)).collect();
    assert_eq!(actual, expected);
}

// Covers: deleted sessions leave freed pages that only VACUUM returns to the
// OS, without losing surviving evidence. Owner: search index compaction
// against a real SQLite file.
#[test]
fn vacuum_returns_deleted_session_pages_to_the_filesystem() {
    let root = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let cancellation = CancellationToken::new();
    let filler = "evidence ".repeat(64 * 1024);
    let sessions: Vec<_> = (0..4)
        .map(|index| {
            let session = Session::create_in_root(root.path(), cwd.path()).unwrap();
            session
                .append_message(&Message::user_text(format!("vacuumneedle{index} {filler}")))
                .unwrap();
            session
        })
        .collect();
    let mut connection = open(root.path()).unwrap();
    refresh(
        &mut connection,
        root.path(),
        /*reconcile*/ false,
        row_locations,
        &cancellation,
    )
    .unwrap();
    for session in &sessions[1..] {
        fs::remove_file(session.path()).unwrap();
    }
    // Real refreshes vacuum past 50 MiB free; this cache stays below that.
    refresh(
        &mut connection,
        root.path(),
        /*reconcile*/ true,
        row_locations,
        &cancellation,
    )
    .unwrap();
    // Flush WAL frames so the file size reflects every allocated page.
    connection
        .query_row("pragma wal_checkpoint(truncate)", [], |_| Ok(()))
        .unwrap();
    let database = root.path().join("search.sqlite3");
    let bloated = Pages::read(&connection).unwrap();
    assert_eq!(
        fs::metadata(&database).unwrap().len(),
        bloated.total * bloated.size
    );
    assert!(bloated.worth_vacuuming(/*min_free_bytes*/ 0));

    vacuum_if_bloated(&connection, /*min_free_bytes*/ 0).unwrap();

    let vacuumed = Pages::read(&connection).unwrap();
    assert_eq!(
        (
            vacuumed.free,
            vacuumed.total,
            fs::metadata(&database).unwrap().len()
        ),
        (
            0,
            bloated.total - bloated.free,
            (bloated.total - bloated.free) * bloated.size
        )
    );
    assert_eq!(
        connection
            .query_row(
                "select count(*) from evidence_fts where evidence_fts match 'vacuumneedle0'",
                [],
                |row| row.get::<_, usize>(0)
            )
            .unwrap(),
        1
    );
}

// Covers: evidence an older schema indexed from a session whose header is
// format 1-3 must not stay searchable once rho refuses to open that session,
// even though the file's size and mtime still match the cached stamp.
// Owner: search index SQLite migration.
#[test]
fn schema_upgrade_drops_evidence_from_pre_tree_sessions() {
    let root = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let session = Session::create_in_root(root.path(), cwd.path()).unwrap();
    session
        .append_message(&Message::user_text("pretreeneedle"))
        .unwrap();
    let cancellation = CancellationToken::new();
    let matches = |connection: &Connection| {
        connection
            .query_row(
                "select count(*) from evidence_fts where evidence_fts match 'pretreeneedle'",
                [],
                |row| row.get::<_, usize>(0),
            )
            .unwrap()
    };
    let mut connection = open(root.path()).unwrap();
    refresh(
        &mut connection,
        root.path(),
        /*reconcile*/ false,
        row_locations,
        &cancellation,
    )
    .unwrap();
    assert_eq!(matches(&connection), 1);

    // The cache an earlier rho 3 left: schema 4, with evidence from a file
    // whose header (same size, same mtime) declares format 2.
    let modified = fs::metadata(session.path()).unwrap().modified().unwrap();
    let transcript = fs::read_to_string(session.path()).unwrap();
    fs::write(
        session.path(),
        transcript.replacen("\"version\":5", "\"version\":2", 1),
    )
    .unwrap();
    fs::File::options()
        .write(true)
        .open(session.path())
        .unwrap()
        .set_modified(modified)
        .unwrap();
    connection.pragma_update(None, "user_version", 4).unwrap();
    drop(connection);

    let mut connection = open(root.path()).unwrap();
    refresh(
        &mut connection,
        root.path(),
        /*reconcile*/ false,
        row_locations,
        &cancellation,
    )
    .unwrap();
    assert_eq!(matches(&connection), 0);
}

// Covers: a transcript that does not load is not indexed, and is not read
// again on every refresh, which a search runs before each query, until it
// changes. Owner: search index refresh.
#[test]
fn a_transcript_that_does_not_load_is_read_once() {
    let root = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let session = Session::create_in_root(root.path(), cwd.path()).unwrap();
    session
        .append_message(&Message::user_text("brokenneedle"))
        .unwrap();
    let mut file = fs::OpenOptions::new()
        .append(true)
        .open(session.path())
        .unwrap();
    std::io::Write::write_all(&mut file, b"{\"type\":\"node\",\n").unwrap();
    let cancellation = CancellationToken::new();
    let mut connection = open(root.path()).unwrap();
    let report = |refresh: Refresh| {
        (
            refresh.files_checked,
            refresh.files_updated,
            refresh.skipped_files,
            refresh.omitted_records,
        )
    };

    let first = refresh(
        &mut connection,
        root.path(),
        /*reconcile*/ false,
        row_locations,
        &cancellation,
    )
    .unwrap();
    let second = refresh(
        &mut connection,
        root.path(),
        /*reconcile*/ false,
        row_locations,
        &cancellation,
    )
    .unwrap();

    let matches: usize = connection
        .query_row(
            "select count(*) from evidence_fts where evidence_fts match 'brokenneedle'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    assert_eq!(
        (report(first), report(second), matches),
        ((1, 1, 0, 1), (0, 0, 0, 1), 0)
    );
}

// Covers: a transcript whose read failed, as on a full or unmounted disk,
// is read again once reads work, not cached as one that does not load, and
// the index reports itself incomplete meanwhile, which a daemon search
// answers as `indexing`. Owner: search index refresh.
#[cfg(unix)]
#[test]
fn a_transcript_whose_read_failed_is_read_again() {
    use std::os::unix::fs::PermissionsExt;

    let root = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let session = Session::create_in_root(root.path(), cwd.path()).unwrap();
    session
        .append_message(&Message::user_text("retryneedle"))
        .unwrap();
    let readable = |mode| fs::set_permissions(session.path(), fs::Permissions::from_mode(mode));
    readable(0o000).unwrap();
    // Root or CAP_DAC_OVERRIDE can still open 0o000 files; skip if this process can.
    if fs::File::open(session.path()).is_ok() {
        readable(0o600).unwrap();
        return;
    }
    let cancellation = CancellationToken::new();
    let mut connection = open(root.path()).unwrap();
    let state = |connection: &mut Connection| {
        refresh(
            connection,
            root.path(),
            /*reconcile*/ false,
            row_locations,
            &cancellation,
        )
        .unwrap();
        let matches: usize = connection
            .query_row(
                "select count(*) from evidence_fts where evidence_fts match 'retryneedle'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        (complete(connection).unwrap(), matches)
    };

    let failed = state(&mut connection);
    readable(0o600).unwrap();
    let recovered = state(&mut connection);

    assert_eq!((failed, recovered), ((false, 0), (true, 1)));
}

// Covers: which indexing failures a later refresh retries: failed reads and
// a transcript that changed while read, wrapped or not, but not a
// transcript that is malformed, text that is not UTF-8 included, which
// would otherwise keep a search `indexing` for good. Owner: search index
// refresh.
#[test]
fn only_failed_reads_and_changes_are_retried() {
    use std::io::{Error, ErrorKind};

    let cases: [(&str, anyhow::Error, bool); 6] = [
        (
            "failed read",
            Error::from(ErrorKind::PermissionDenied).into(),
            true,
        ),
        (
            "failed read with context",
            anyhow::Error::from(Error::from(ErrorKind::Other)).context("loading"),
            true,
        ),
        ("changed while read", ChangedWhileRead.into(), true),
        (
            "not UTF-8",
            Error::from(ErrorKind::InvalidData).into(),
            false,
        ),
        (
            "malformed record",
            serde_json::from_str::<serde_json::Value>("{")
                .unwrap_err()
                .into(),
            false,
        ),
        (
            "invalid header",
            anyhow::anyhow!("missing session header"),
            false,
        ),
    ];

    let actual: Vec<_> = cases
        .iter()
        .map(|(name, error, _)| (*name, transient(error)))
        .collect();
    let expected: Vec<_> = cases
        .iter()
        .map(|(name, _, retried)| (*name, *retried))
        .collect();
    assert_eq!(actual, expected);
}