rpi-cli 0.31.0

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
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
use super::*;

struct RestoreConfig(Option<std::ffi::OsString>);

impl Drop for RestoreConfig {
    fn drop(&mut self) {
        match self.0.take() {
            Some(value) => std::env::set_var(crate::config::CONFIG_DIR_ENV, value),
            None => std::env::remove_var(crate::config::CONFIG_DIR_ENV),
        }
    }
}

#[tokio::test]
async fn selected_root_survives_build_runtime_actions_and_both_reload_host_paths() {
    let temporary = tempfile::tempdir().unwrap();
    let env_root = temporary.path().join("env sessions");
    let _env = crate::config::test_support::SessionDirEnv::new(Some(&env_root));
    let _config = RestoreConfig(std::env::var_os(crate::config::CONFIG_DIR_ENV));
    let agent = temporary.path().join("agent");
    std::fs::create_dir_all(&agent).unwrap();
    std::env::set_var(crate::config::CONFIG_DIR_ENV, &agent);
    let settings_path = agent.join("settings.json");

    for mode in ["flag", "settings", "environment"] {
        let cwd = temporary.path().join(mode);
        std::fs::create_dir_all(&cwd).unwrap();
        let root = if mode == "flag" {
            cwd.join("../flag selected sessions")
        } else {
            temporary.path().join(format!("{mode} selected sessions"))
        };
        let settings = match mode {
            "environment" => serde_json::json!({}),
            "flag" => {
                serde_json::json!({"sessionDir": temporary.path().join("ignored settings root")})
            }
            _ => serde_json::json!({"sessionDir": root}),
        };
        std::fs::write(&settings_path, settings.to_string()).unwrap();
        std::env::set_var(
            "RPI_CODING_AGENT_SESSION_DIR",
            if mode == "environment" {
                &root
            } else {
                &env_root
            },
        );
        let resolved = crate::provider::resolve(
            &crate::config::AgentHome::process(),
            Some("anthropic"),
            Some(crate::provider::DEFAULT_MODEL_ID),
            None,
            Some("test-key"),
            None,
        )
        .unwrap();
        let args = Args {
            session_dir: (mode == "flag")
                .then(|| std::path::PathBuf::from("../flag selected sessions")),
            no_extensions: true,
            no_tools: true,
            no_skills: true,
            no_prompt_templates: true,
            no_context_files: true,
            system_prompt: Some("test".into()),
            ..Default::default()
        };
        let prepared =
            prepare_extensions(&crate::config::AgentHome::process(), &args, &cwd, false).unwrap();
        assert_eq!(prepared.session_dir, root);
        // Extension discovery must not make later session setup re-resolve the
        // root from changed settings or process environment.
        let changed_root = temporary.path().join(format!("{mode} changed sessions"));
        std::env::set_var("RPI_CODING_AGENT_SESSION_DIR", &changed_root);
        std::fs::write(
            &settings_path,
            serde_json::json!({"sessionDir": changed_root}).to_string(),
        )
        .unwrap();
        let (harness, _events, context) = build_prepared(
            &crate::config::AgentHome::process(),
            &resolved,
            &args,
            &cwd,
            false,
            prepared,
        )
        .await
        .unwrap();
        assert_eq!(context.session_dir(), root);
        let initial_id = harness.session().storage().metadata().id.clone();
        harness.session().set_name(Some("original")).await.unwrap();
        let original_host = context
            .action_bridge
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .clone_host();
        let fork_id = original_host.fork(serde_json::json!({})).await.unwrap()["sessionId"]
            .as_str()
            .unwrap()
            .to_owned();
        assert_ne!(fork_id, initial_id);
        assert!(
            !reload_extension_resources(&harness, &context)
                .await
                .had_errors
        );
        let host = context
            .action_bridge
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .clone_host();
        host.switch_session(serde_json::json!({"id":initial_id}))
            .await
            .unwrap();
        assert_eq!(
            harness.session().get_name().await.unwrap().as_deref(),
            Some("original")
        );
        // Exercise reload's fallback constructor as well as bridge reuse.
        context.action_bridge.lock().unwrap().take();
        assert!(
            !reload_extension_resources(&harness, &context)
                .await
                .had_errors
        );
        let host = context
            .action_bridge
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .clone_host();
        let new_id = host.new_session(serde_json::json!({})).await.unwrap()["sessionId"]
            .as_str()
            .unwrap()
            .to_owned();
        assert_eq!(harness.session().storage().metadata().id, new_id);
        host.switch_session(serde_json::json!({"id":fork_id}))
            .await
            .unwrap();
        let metadata = list_session_metadata_in_dir(&cwd.to_string_lossy(), &root)
            .await
            .unwrap();
        assert_eq!(metadata.len(), 3, "{mode}");
        for meta in metadata {
            let lock = std::fs::OpenOptions::new()
                .create(true)
                .write(true)
                .open(format!("{}.lock", meta.path))
                .unwrap();
            if meta.id == fork_id {
                assert!(fs2::FileExt::try_lock_exclusive(&lock).is_err());
            } else {
                assert!(fs2::FileExt::try_lock_exclusive(&lock).is_ok());
            }
        }
        assert!(
            !changed_root.exists(),
            "{mode}: runtime must not drift to the changed root"
        );
        assert!(!cwd.join(".rpi/sessions").exists());
    }
}

#[tokio::test]
async fn resume_picker_reports_the_selected_root_when_it_has_no_history() {
    let temporary = tempfile::tempdir().unwrap();
    let root = temporary.path().join("custom empty sessions");
    let error = crate::resume_picker::select_in_dir(temporary.path(), &root)
        .await
        .unwrap_err();
    assert!(error.contains(&root.display().to_string()));
    create_jsonl_session(&root, &temporary.path().to_string_lossy())
        .await
        .unwrap();
    let error = crate::resume_picker::select_in_dir(temporary.path(), &root)
        .await
        .unwrap_err();
    assert!(error.contains("no resumable sessions"));
    assert!(error.contains(&root.display().to_string()));
}

#[tokio::test]
async fn importing_a_torn_tail_repairs_only_the_private_copy() {
    let temporary = tempfile::tempdir().unwrap();
    let cwd = temporary.path().to_string_lossy();
    let source_root = temporary.path().join("source");
    let session = create_jsonl_session(&source_root, &cwd).await.unwrap();
    session.set_name(Some("preserved")).await.unwrap();
    let path = list_session_files_sync(&source_root).remove(0);
    let mut content = std::fs::read(&path).unwrap();
    content.extend_from_slice(b"{\"kind\":");
    std::fs::write(&path, &content).unwrap();
    // Import opens a fresh writer binding for the copied artifact; release the
    // fixture's original storage lease first.
    drop(session);
    let root = temporary.path().join("destination");
    let imported = import_session_file(&path, &cwd, &root).await.unwrap();
    assert_eq!(
        imported.get_name().await.unwrap().as_deref(),
        Some("preserved")
    );
    assert_eq!(std::fs::read(&path).unwrap(), content);
    let saved = list_session_files_sync(&root).remove(0);
    assert!(!std::fs::read_to_string(saved)
        .unwrap()
        .ends_with("{\"kind\":"));
}

#[tokio::test]
async fn fork_entry_cuts_the_new_branch_at_the_selected_message() {
    let temporary = tempfile::tempdir().unwrap();
    let cwd = temporary.path().to_string_lossy();
    let source_root = temporary.path().join("source");
    let source = create_jsonl_session(&source_root, &cwd).await.unwrap();
    let first = source
        .append_message(rpi_agent::message::AgentMessage::User(
            rpi_ai::types::UserMessage::new("first", 1),
        ))
        .await
        .unwrap();
    let second = source
        .append_message(rpi_agent::message::AgentMessage::User(
            rpi_ai::types::UserMessage::new("second", 2),
        ))
        .await
        .unwrap();
    let source_file = list_session_files_sync(&source_root).remove(0);
    let root = temporary.path().join("sessions");
    let forked = fork_session_at_launch(
        &source_file.to_string_lossy(),
        &root,
        &cwd,
        None,
        Some(&first),
        Some(rpi_harness::session::types::ForkPosition::At),
    )
    .await
    .unwrap();
    let ids: Vec<String> = forked
        .find_entries(&rpi_harness::session::types::EntryQuery {
            order: Some(rpi_harness::session::types::EntryOrder::OldestFirst),
            ..Default::default()
        })
        .await
        .unwrap()
        .into_iter()
        .map(|entry| entry.id().to_owned())
        .collect();
    assert!(
        ids.contains(&first),
        "inclusive fork must keep the selected entry"
    );
    assert!(
        !ids.contains(&second),
        "entries after the fork point must be absent"
    );
}

/// `before` drops the selected entry itself, so the branch ends at its parent.
/// This is also the position the harness picks when `--fork-position` is
/// omitted, which is why the CLI must not substitute its own default.
#[tokio::test]
async fn fork_entry_before_excludes_the_selected_message() {
    use rpi_harness::session::types::{EntryOrder, EntryQuery, ForkPosition};

    for position in [Some(ForkPosition::Before), None] {
        let temporary = tempfile::tempdir().unwrap();
        let cwd = temporary.path().to_string_lossy();
        let source_root = temporary.path().join("source");
        let source = create_jsonl_session(&source_root, &cwd).await.unwrap();
        let first = source
            .append_message(rpi_agent::message::AgentMessage::User(
                rpi_ai::types::UserMessage::new("first", 1),
            ))
            .await
            .unwrap();
        let second = source
            .append_message(rpi_agent::message::AgentMessage::User(
                rpi_ai::types::UserMessage::new("second", 2),
            ))
            .await
            .unwrap();
        let source_file = list_session_files_sync(&source_root).remove(0);
        let root = temporary.path().join("sessions");
        let forked = fork_session_at_launch(
            &source_file.to_string_lossy(),
            &root,
            &cwd,
            None,
            Some(&second),
            position,
        )
        .await
        .unwrap();
        let ids: Vec<String> = forked
            .find_entries(&EntryQuery {
                order: Some(EntryOrder::OldestFirst),
                ..Default::default()
            })
            .await
            .unwrap()
            .into_iter()
            .map(|entry| entry.id().to_owned())
            .collect();
        assert!(
            ids.contains(&first),
            "{position:?}: the parent must survive an exclusive cut"
        );
        assert!(
            !ids.contains(&second),
            "{position:?}: the selected entry must be dropped"
        );
    }
}

/// `--fork-entry` is an exact entry id, unlike `--fork`'s partial session id.
/// An unknown id must fail the launch instead of silently forking at the leaf.
#[tokio::test]
async fn fork_entry_rejects_an_unknown_entry_id() {
    let temporary = tempfile::tempdir().unwrap();
    let cwd = temporary.path().to_string_lossy();
    let source_root = temporary.path().join("source");
    let source = create_jsonl_session(&source_root, &cwd).await.unwrap();
    let first = source
        .append_message(rpi_agent::message::AgentMessage::User(
            rpi_ai::types::UserMessage::new("first", 1),
        ))
        .await
        .unwrap();
    let source_file = list_session_files_sync(&source_root).remove(0);
    let root = temporary.path().join("sessions");
    // A prefix of a real id must not match.
    let prefix = &first[..first.len() - 1];
    for unknown in ["no-such-entry", prefix] {
        let error = match fork_session_at_launch(
            &source_file.to_string_lossy(),
            &root,
            &cwd,
            None,
            Some(unknown),
            None,
        )
        .await
        {
            Ok(_) => panic!("an unknown --fork-entry must not fall back to the lane leaf"),
            Err(error) => error,
        };
        assert!(
            error.to_string().contains(unknown),
            "{unknown}: the error must name the entry it could not find, got {error}"
        );
    }
}

#[tokio::test]
async fn fork_takes_a_session_file_outside_the_session_directory() {
    let temporary = tempfile::tempdir().unwrap();
    let cwd = temporary.path().to_string_lossy();
    let source_root = temporary.path().join("source");
    let source = create_jsonl_session(&source_root, &cwd).await.unwrap();
    source.set_name(Some("parent")).await.unwrap();
    let parent_id = source.get_metadata().await.unwrap().id;
    // A caller copies the parent file elsewhere and forks from that copy.
    let copy = temporary.path().join("elsewhere").join("parent.jsonl");
    std::fs::create_dir_all(copy.parent().unwrap()).unwrap();
    // Move the complete recovery pair, not only the history manifest.
    let original = list_session_files_sync(&source_root).remove(0);
    std::fs::copy(&original, &copy).unwrap();
    std::fs::copy(
        format!("{}.progress.jsonl", original.display()),
        format!("{}.progress.jsonl", copy.display()),
    )
    .unwrap();
    let root = temporary.path().join("sessions");
    let forked = fork_session_at_launch(&copy.to_string_lossy(), &root, &cwd, None, None, None)
        .await
        .unwrap_or_else(|error| panic!("fork from a file path failed: {error}"));
    let forked_meta = forked.get_metadata().await.unwrap();
    assert_ne!(forked_meta.id, parent_id);
    assert_eq!(forked.get_name().await.unwrap().as_deref(), Some("parent"));
    assert_eq!(list_session_files_sync(&root).len(), 1);
}

#[tokio::test]
async fn launch_fork_cuts_at_or_before_a_historical_message() {
    let temporary = tempfile::tempdir().unwrap();
    let cwd = temporary.path().to_string_lossy();
    let root = temporary.path().join("sessions");
    let source = create_jsonl_session(&root, &cwd).await.unwrap();
    let mut entries = Vec::new();
    for content in ["first", "target", "later"] {
        let message = rpi_ai::types::UserMessage::new(content, 42);
        entries.push(source.append_message(message.into()).await.unwrap());
    }
    let custom = source.append_custom_entry("test", None).await.unwrap();
    let path = list_session_files_sync(&root).remove(0);
    let original = std::fs::read(&path).unwrap();
    let launch = |entry: &str, position: &str| {
        let args = crate::args::parse_args(&[
            "--fork".into(),
            path.to_string_lossy().into_owned(),
            format!("--fork-entry={entry}"),
            format!("--fork-position={position}"),
        ]);
        assert!(args.errors.is_empty(), "{:?}", args.errors);
        args
    };
    for (position, leaf) in [("at", &entries[1]), ("before", &entries[0])] {
        let args = launch(&entries[1], position);
        let selection = select_session_in_dir(&args, root.clone());
        let forked = build_session(&selection, &cwd, None, None).await.unwrap();
        assert_eq!(forked.get_leaf_id().await.unwrap().as_ref(), Some(leaf));
        assert!(forked.get_entry(&entries[2]).await.unwrap().is_none());
    }
    for entry in ["missing", &custom] {
        let args = launch(entry, "at");
        let selection = select_session_in_dir(&args, root.clone());
        assert!(build_session(&selection, &cwd, None, None).await.is_err());
    }
    assert_eq!(list_session_files_sync(&root).len(), 3);
    assert_eq!(std::fs::read(path).unwrap(), original);
}