rho-coding-agent 1.48.0

A lightweight agent harness inspired by Pi
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::{
    fs,
    sync::{mpsc, Arc, Barrier},
    thread,
};

use pretty_assertions::assert_eq;
use tempfile::TempDir;

use super::paths::{prepare_private_directory, SessionSubagentsDir};
use super::{
    index::{
        initialize_index, insert_parent_lock_for_test, unix_timestamp_secs, PARENT_LOCK_TTL_SECS,
    },
    is_trusted_directory, list_workspace_runs_in_root, lock_parent_for_cleanup_in_root,
    reserve_run_directory_in_root as reserve_at, resolve_run_directory_in_root, RunPlacement,
};
use crate::session::Session;
use std::path::{Path, PathBuf};

fn reserve_in_default_workspace(
    rho_root: &Path,
    placement: &RunPlacement,
    next_id: impl FnMut() -> String,
) -> anyhow::Result<(String, PathBuf)> {
    reserve_at(rho_root, rho_root, placement, next_id)
}

fn create_session_subagents(root: &Path) -> PathBuf {
    let cwd = TempDir::new().unwrap();
    Session::create_in_root(&root.join("sessions"), cwd.path())
        .unwrap()
        .subagents_dir()
        .unwrap()
}

#[test]
fn skips_ids_used_by_unindexed_target_path() {
    let temp = TempDir::new().unwrap();
    let existing = temp.path().join("subagents/111111");
    fs::create_dir_all(&existing).unwrap();
    let mut ids = ["111111", "222222"].into_iter();

    let (id, directory) = reserve_in_default_workspace(
        temp.path(),
        &RunPlacement::Global {
            parent_session_id: None,
        },
        || ids.next().unwrap().into(),
    )
    .unwrap();

    assert_eq!(id, "222222");
    assert_eq!(directory, temp.path().join("subagents/222222"));
}

#[test]
fn scan_fallback_resolves_one_nested_run() {
    let temp = TempDir::new().unwrap();
    let directory = create_session_subagents(temp.path()).join("123abc");
    fs::create_dir_all(&directory).unwrap();

    assert_eq!(
        resolve_run_directory_in_root(temp.path(), "123abc").unwrap(),
        directory
    );
}

#[test]
fn scan_fallback_reports_ambiguous_nested_runs() {
    let temp = TempDir::new().unwrap();
    for _ in 0..2 {
        fs::create_dir_all(create_session_subagents(temp.path()).join("123abc")).unwrap();
    }

    let error = resolve_run_directory_in_root(temp.path(), "123abc").unwrap_err();
    assert!(error
        .to_string()
        .contains("ambiguous across session folders"));
}

#[test]
fn legacy_global_fallback_resolves_without_index() {
    let temp = TempDir::new().unwrap();
    let directory = temp.path().join("subagents/654321");
    fs::create_dir_all(&directory).unwrap();

    assert_eq!(
        resolve_run_directory_in_root(temp.path(), "654321").unwrap(),
        directory
    );
}

#[test]
fn concurrent_index_initialization_is_idempotent() {
    let temp = TempDir::new().unwrap();
    let path = temp.path().join("subagents/index.sqlite3");
    let barrier = Arc::new(Barrier::new(3));
    let handles = (0..2)
        .map(|_| {
            let path = path.clone();
            let barrier = Arc::clone(&barrier);
            thread::spawn(move || {
                barrier.wait();
                initialize_index(&path).map(drop)
            })
        })
        .collect::<Vec<_>>();
    barrier.wait();

    for handle in handles {
        handle.join().unwrap().unwrap();
    }
}

// Covers: concurrent first reservations must both succeed while racing to
// create a missing session subagents directory — the loser used to fail with
// EEXIST ("File exists (os error 17)") because the pre-fix code only
// tolerated a directory that already existed at check time.
// Owner: delegated-run reservation
#[test]
fn concurrent_session_subagents_dir_creation_is_tolerated() {
    for _ in 0..16 {
        let temp = TempDir::new().unwrap();
        let subagents = temp.path().join("sessions/ws/session-1/subagents");
        fs::create_dir_all(subagents.parent().unwrap()).unwrap();
        let dir = SessionSubagentsDir::parse(temp.path(), &subagents).unwrap();
        assert!(!subagents.exists());

        let barrier = Arc::new(Barrier::new(3));
        let handles = (0..2)
            .map(|_| {
                let dir = dir.clone();
                let barrier = Arc::clone(&barrier);
                thread::spawn(move || {
                    barrier.wait();
                    dir.ensure_ready()
                })
            })
            .collect::<Vec<_>>();
        barrier.wait();

        for handle in handles {
            handle.join().unwrap().unwrap();
        }
        assert!(is_trusted_directory(&subagents));
    }
}

// Covers: a reservation must succeed when the session subagents directory
// already exists — the loser of a concurrent first-spawn create used to fail
// here with EEXIST ("File exists (os error 17)").
// Owner: delegated-run reservation
#[test]
fn reservations_tolerate_existing_session_subagents_dir() {
    let temp = TempDir::new().unwrap();
    // Simulate the winner of a concurrent first-spawn create: the directory
    // exists by the time this reservation checks.
    let subagents_dir = create_session_subagents(temp.path());
    fs::create_dir(&subagents_dir).unwrap();
    let placement = RunPlacement::Session {
        parent_session_id: "session-id".into(),
        subagents_dir: subagents_dir.clone(),
    };

    let (id, directory) =
        reserve_in_default_workspace(temp.path(), &placement, || "abcdef".into()).unwrap();

    assert_eq!(id, "abcdef");
    assert_eq!(directory, subagents_dir.join("abcdef"));
}

// Covers: private-directory preparation must create a missing leaf with
// private modes and stay idempotent when the leaf already exists.
// Owner: delegated-run reservation
#[test]
fn prepare_private_directory_creates_and_secures_missing_leaf() {
    let temp = TempDir::new().unwrap();
    let target = temp.path().join("nested/subagents");

    prepare_private_directory(&target).unwrap();
    prepare_private_directory(&target).unwrap();

    assert!(is_trusted_directory(&target));
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = fs::metadata(&target).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o700);
    }
}

// Covers: a non-directory at the target path must still be rejected even
// though the create step tolerates AlreadyExists from concurrent creators.
// Owner: delegated-run reservation
#[test]
fn prepare_private_directory_rejects_non_directory_target() {
    let temp = TempDir::new().unwrap();
    let target = temp.path().join("not-a-dir");
    fs::write(&target, b"x").unwrap();

    let error = prepare_private_directory(&target).unwrap_err();
    assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
}

#[test]
fn reservation_fails_after_parent_session_is_deleted() {
    let temp = TempDir::new().unwrap();
    let subagents_dir = create_session_subagents(temp.path());
    fs::remove_dir_all(subagents_dir.parent().unwrap()).unwrap();
    let placement = RunPlacement::Session {
        parent_session_id: "deleted-session".into(),
        subagents_dir: subagents_dir.clone(),
    };

    let error =
        reserve_in_default_workspace(temp.path(), &placement, || "abcdef".into()).unwrap_err();

    assert!(error
        .to_string()
        .contains("not a trusted session directory"));
    assert!(!subagents_dir.exists());
}

#[test]
fn parent_cleanup_lock_blocks_reservations_for_that_parent() {
    let temp = TempDir::new().unwrap();
    let rho_root = temp.path().to_path_buf();
    let subagents_root = rho_root.join("subagents");
    let session_subagents = create_session_subagents(&rho_root);
    let session_dir = session_subagents.parent().unwrap().to_path_buf();
    let deleted_session_dir = session_dir.clone();
    let (entered_tx, entered_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();
    let cleanup_root = subagents_root.clone();
    let cleanup = thread::spawn(move || {
        let guard = lock_parent_for_cleanup_in_root(&cleanup_root, "session-id")?;
        entered_tx.send(()).unwrap();
        release_rx.recv().unwrap();
        fs::remove_dir_all(session_dir)?;
        guard.clear_index_and_unlock()
    });
    entered_rx.recv().unwrap();

    let (reserve_started_tx, reserve_started_rx) = mpsc::channel();
    let reserve_root = rho_root.clone();
    let reserve = thread::spawn(move || {
        reserve_in_default_workspace(
            &reserve_root,
            &RunPlacement::Session {
                parent_session_id: "session-id".into(),
                subagents_dir: session_subagents,
            },
            || {
                reserve_started_tx.send(()).unwrap();
                "abcdef".into()
            },
        )
    });
    reserve_started_rx.recv().unwrap();
    let reserve_error = reserve.join().unwrap().unwrap_err();
    assert!(
        reserve_error.to_string().contains("is being deleted"),
        "{reserve_error}"
    );
    release_tx.send(()).unwrap();

    cleanup.join().unwrap().unwrap();
    assert!(!deleted_session_dir.exists());
}

#[test]
fn parent_cleanup_lock_does_not_block_unrelated_parents() {
    let temp = TempDir::new().unwrap();
    let rho_root = temp.path();
    let subagents_root = rho_root.join("subagents");
    let other_subagents = create_session_subagents(rho_root);
    let _guard = lock_parent_for_cleanup_in_root(&subagents_root, "deleting-session").unwrap();

    let (_, directory) = reserve_in_default_workspace(
        rho_root,
        &RunPlacement::Session {
            parent_session_id: "other-session".into(),
            subagents_dir: other_subagents.clone(),
        },
        || "abcdef".into(),
    )
    .unwrap();

    assert_eq!(directory, other_subagents.join("abcdef"));
}

#[test]
fn stale_parent_lock_is_ignored_by_reserve() {
    let temp = TempDir::new().unwrap();
    let subagents_root = temp.path().join("subagents");
    let subagents_dir = create_session_subagents(temp.path());
    let stale_at = unix_timestamp_secs() - PARENT_LOCK_TTL_SECS - 1;
    insert_parent_lock_for_test(&subagents_root, "session-id", stale_at).unwrap();

    let (_, directory) = reserve_in_default_workspace(
        temp.path(),
        &RunPlacement::Session {
            parent_session_id: "session-id".into(),
            subagents_dir: subagents_dir.clone(),
        },
        || "abcdef".into(),
    )
    .unwrap();

    assert_eq!(directory, subagents_dir.join("abcdef"));
}

#[test]
fn stale_parent_lock_can_be_stolen_for_cleanup() {
    let temp = TempDir::new().unwrap();
    let subagents_root = temp.path().join("subagents");
    let stale_at = unix_timestamp_secs() - PARENT_LOCK_TTL_SECS - 1;
    insert_parent_lock_for_test(&subagents_root, "session-id", stale_at).unwrap();

    let guard = lock_parent_for_cleanup_in_root(&subagents_root, "session-id").unwrap();
    guard.clear_index_and_unlock().unwrap();

    // A fresh lock should succeed after the stolen cleanup finished.
    let _guard = lock_parent_for_cleanup_in_root(&subagents_root, "session-id").unwrap();
}

#[test]
fn fresh_parent_lock_rejects_second_cleanup() {
    let temp = TempDir::new().unwrap();
    let subagents_root = temp.path().join("subagents");
    let _guard = lock_parent_for_cleanup_in_root(&subagents_root, "session-id").unwrap();
    let error = lock_parent_for_cleanup_in_root(&subagents_root, "session-id").unwrap_err();
    assert!(
        error.to_string().contains("already being deleted"),
        "{error}"
    );
}

#[cfg(unix)]
#[test]
fn scan_ignores_symlinked_session_ancestors() {
    use std::os::unix::fs::symlink;

    let temp = TempDir::new().unwrap();
    let outside = TempDir::new().unwrap();
    let external_run = outside.path().join("session/subagents/abcdef");
    fs::create_dir_all(&external_run).unwrap();
    let sessions_root = temp.path().join("sessions");
    fs::create_dir_all(&sessions_root).unwrap();
    symlink(outside.path(), sessions_root.join("linked-workspace")).unwrap();

    let error = resolve_run_directory_in_root(temp.path(), "abcdef").unwrap_err();
    assert!(error.to_string().contains("unknown delegated run"));
}

#[test]
fn stale_index_row_falls_through_to_legacy_global() {
    let temp = TempDir::new().unwrap();
    let indexed = create_session_subagents(temp.path()).join("abcdef");
    let placement = RunPlacement::Session {
        parent_session_id: "session".into(),
        subagents_dir: indexed.parent().unwrap().to_path_buf(),
    };
    reserve_in_default_workspace(temp.path(), &placement, || "abcdef".into()).unwrap();
    fs::remove_dir(&indexed).unwrap();
    let legacy = temp.path().join("subagents/abcdef");
    fs::create_dir_all(&legacy).unwrap();

    assert_eq!(
        resolve_run_directory_in_root(temp.path(), "abcdef").unwrap(),
        legacy
    );
}

#[test]
fn failed_cleanup_releases_parent_lock() {
    let temp = TempDir::new().unwrap();
    let subagents_root = temp.path().join("subagents");
    let subagents_dir = create_session_subagents(temp.path());

    {
        let _guard = lock_parent_for_cleanup_in_root(&subagents_root, "session-id").unwrap();
    }

    let (_, directory) = reserve_in_default_workspace(
        temp.path(),
        &RunPlacement::Session {
            parent_session_id: "session-id".into(),
            subagents_dir: subagents_dir.clone(),
        },
        || "abcdef".into(),
    )
    .unwrap();
    assert_eq!(directory, subagents_dir.join("abcdef"));
}

// Covers: rho attach picker must not list subagents from another directory.
// Owner: delegated-run index listing
#[test]
fn list_running_runs_keeps_only_the_current_workspace() {
    let temp = TempDir::new().unwrap();
    let here = TempDir::new().unwrap();
    let there = TempDir::new().unwrap();
    let here_session = Session::create_in_root(&temp.path().join("sessions"), here.path()).unwrap();
    let there_session =
        Session::create_in_root(&temp.path().join("sessions"), there.path()).unwrap();
    let here_nested = reserve_running(
        temp.path(),
        here.path(),
        RunPlacement::Session {
            parent_session_id: here_session.id().to_string(),
            subagents_dir: here_session.subagents_dir().unwrap(),
        },
        "aaaaaa",
        "here-worker",
    );
    let there_nested = reserve_running(
        temp.path(),
        there.path(),
        RunPlacement::Session {
            parent_session_id: there_session.id().to_string(),
            subagents_dir: there_session.subagents_dir().unwrap(),
        },
        "bbbbbb",
        "there-worker",
    );
    let here_parentless = reserve_running(
        temp.path(),
        here.path(),
        RunPlacement::Global {
            parent_session_id: None,
        },
        "cccccc",
        "here-orphan",
    );
    let there_parentless = reserve_running(
        temp.path(),
        there.path(),
        RunPlacement::Global {
            parent_session_id: None,
        },
        "dddddd",
        "there-orphan",
    );

    let here_ids = workspace_ids(temp.path(), here.path());
    let there_ids = workspace_ids(temp.path(), there.path());

    assert_eq!(
        here_ids,
        std::collections::BTreeSet::from([here_nested, here_parentless])
    );
    assert_eq!(
        there_ids,
        std::collections::BTreeSet::from([there_nested, there_parentless])
    );
    assert_eq!(
        workspace_ids(temp.path(), &here.path().canonicalize().unwrap()),
        here_ids
    );
}

// Covers: attach listing can include finished transcripts for the same workspace.
// Owner: delegated-run index listing
#[test]
fn list_workspace_runs_can_include_finished_runs() {
    let temp = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let running = reserve_running(
        temp.path(),
        cwd.path(),
        RunPlacement::parentless(),
        "aaaaaa",
        "worker",
    );
    let finished = reserve_running(
        temp.path(),
        cwd.path(),
        RunPlacement::parentless(),
        "bbbbbb",
        "explorer",
    );
    write_terminal(temp.path(), &finished, crate::subagent::RunState::Ok);

    assert_eq!(
        workspace_ids(temp.path(), cwd.path()),
        std::collections::BTreeSet::from([running, finished])
    );
}

// Covers: a v3 index must upgrade and keep unscoped rows out of the picker.
// Owner: delegated-run index listing
#[test]
fn v3_index_upgrades_and_hides_unscoped_rows_from_the_picker() {
    let temp = TempDir::new().unwrap();
    let cwd = TempDir::new().unwrap();
    let directory = temp.path().join("subagents/eeeeee");
    fs::create_dir_all(&directory).unwrap();
    crate::subagent::write_status(
        &directory.join(crate::subagent::RESULT_FILE_NAME),
        &crate::subagent::RunStatus {
            state: crate::subagent::RunState::Running,
            agent_id: Some("legacy".into()),
            started_at: Some(1),
            ..crate::subagent::RunStatus::default()
        },
    )
    .unwrap();

    let index_path = temp.path().join("subagents/index.sqlite3");
    let connection = rusqlite::Connection::open(&index_path).unwrap();
    connection
        .execute_batch(
            "CREATE TABLE runs (
                 run_id TEXT PRIMARY KEY NOT NULL,
                 path TEXT NOT NULL UNIQUE,
                 parent_session_id TEXT,
                 created_at INTEGER NOT NULL
             );
             CREATE TABLE parent_locks (
                 parent_session_id TEXT PRIMARY KEY NOT NULL,
                 locked_at INTEGER NOT NULL
             );
             PRAGMA user_version = 3;",
        )
        .unwrap();
    connection
        .execute(
            "INSERT INTO runs (run_id, path, parent_session_id, created_at)
             VALUES (?1, ?2, NULL, ?3)",
            rusqlite::params!["eeeeee", directory.to_string_lossy(), unix_timestamp_secs()],
        )
        .unwrap();
    drop(connection);

    assert!(workspace_ids(temp.path(), cwd.path()).is_empty());
    assert_eq!(
        resolve_run_directory_in_root(temp.path(), "eeeeee").unwrap(),
        directory
    );
}

fn reserve_running(
    rho_root: &Path,
    cwd: &Path,
    placement: RunPlacement,
    id: &str,
    agent_id: &str,
) -> String {
    let next_id = id.to_string();
    let (id, directory) = reserve_at(rho_root, cwd, &placement, move || next_id.clone()).unwrap();
    crate::subagent::write_status(
        &directory.join(crate::subagent::RESULT_FILE_NAME),
        &crate::subagent::RunStatus {
            state: crate::subagent::RunState::Running,
            agent_id: Some(agent_id.into()),
            started_at: Some(1),
            ..crate::subagent::RunStatus::default()
        },
    )
    .unwrap();
    id
}

fn write_terminal(rho_root: &Path, id: &str, state: crate::subagent::RunState) {
    let directory = resolve_run_directory_in_root(rho_root, id).unwrap();
    crate::subagent::write_status(
        &directory.join(crate::subagent::RESULT_FILE_NAME),
        &crate::subagent::RunStatus {
            state,
            agent_id: Some("explorer".into()),
            started_at: Some(1),
            finished_at: Some(2),
            ..crate::subagent::RunStatus::default()
        },
    )
    .unwrap();
}

fn workspace_ids(rho_root: &Path, cwd: &Path) -> std::collections::BTreeSet<String> {
    list_workspace_runs_in_root(rho_root, cwd)
        .unwrap()
        .into_iter()
        .map(|run| run.id)
        .collect()
}