rpi-tools 0.17.2

Built-in agent tools (read/write/edit/bash/grep/find/ls) + ExecutionEnv, Rust port of pi harness tools
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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! Source fixtures only for the neutral client seam; no transport/CLI behavior
//! is implied by these tests. Host calls panic intentionally in this test double.
use super::*;
use crate::env::{
    ExecutionEnv, FileContent, FileInfo, FileSystem, Shell, ShellExecOptions, ShellOutput,
};
use crate::error::ExecutionError;
use crate::file_mutation_queue::{MutatingEnv, MutationQueueRegistry};
use crate::{
    create_edit_tool, create_read_tool, create_write_tool, ExecutionToolContext,
    InMemoryExecutionEnv,
};
use rpi_agent::agent_tool::AgentTool;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Notify;

#[derive(Default)]
struct Backend {
    text: Mutex<String>,
    requests: Mutex<Vec<FileIORequest>>,
    error: Mutex<Option<FileIOErrorCode>>,
    response: Mutex<Option<FileIOResponse>>,
    cancel_after: AtomicBool,
    hold_write: AtomicBool,
    entered: Notify,
    release: Notify,
}

impl Backend {
    fn text(text: &str) -> Arc<Self> {
        Arc::new(Self {
            text: Mutex::new(text.to_owned()),
            ..Default::default()
        })
    }
}

#[async_trait]
impl FileIOBackend for Backend {
    async fn request(
        &self,
        request: FileIORequest,
        cancel: CancellationToken,
    ) -> Result<FileIOResponse, FileIOError> {
        self.requests.lock().await.push(request.clone());
        if self.cancel_after.load(Ordering::SeqCst) {
            cancel.cancel();
        }
        if let Some(code) = *self.error.lock().await {
            return Err(FileIOError::new(code, "fixture failure"));
        }
        if let Some(response) = self.response.lock().await.clone() {
            return Ok(response);
        }
        match request {
            FileIORequest::ReadText { range, .. } => {
                let text = self.text.lock().await;
                let content = match range {
                    FileIOReadRange::Complete => text.clone(),
                    FileIOReadRange::Lines {
                        offset_1based,
                        limit,
                    } => {
                        let lines = crate::truncate::split_lines_for_counting(&text)
                            .into_iter()
                            .skip(offset_1based.saturating_sub(1) as usize)
                            .take(limit as usize)
                            .collect::<Vec<_>>();
                        let mut result = lines.join("\n");
                        if lines.last() == Some(&"") {
                            result.push('\n');
                        }
                        result
                    }
                };
                Ok(FileIOResponse::Text { content })
            }
            FileIORequest::WriteText { content, .. } => {
                if self.hold_write.swap(false, Ordering::SeqCst) {
                    self.entered.notify_one();
                    self.release.notified().await;
                }
                *self.text.lock().await = content;
                Ok(FileIOResponse::Written)
            }
        }
    }
}

// Implement the actual required traits, not an imagined alternate env API.
struct TrapEnv {
    cwd: PathBuf,
}
impl TrapEnv {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            cwd: "/Workspace".into(),
        })
    }
}
#[async_trait]
impl ExecutionEnv for TrapEnv {}
impl MutatingEnv for TrapEnv {
    fn as_env(&self) -> &dyn ExecutionEnv {
        self
    }
    fn mutation_registry(&self) -> &MutationQueueRegistry {
        panic!("host queue accessed")
    }
}
#[async_trait]
impl Shell for TrapEnv {
    async fn exec<'a>(
        &'a self,
        _: &str,
        _: ShellExecOptions<'a>,
    ) -> Result<ShellOutput, ExecutionError> {
        panic!("host exec")
    }
    async fn cleanup(&self) {
        panic!("host shell cleanup")
    }
}
#[async_trait]
impl FileSystem for TrapEnv {
    fn cwd(&self) -> &Path {
        &self.cwd
    }
    async fn absolute_path(
        &self,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError> {
        panic!("host absolute_path")
    }
    async fn join_path(
        &self,
        _: &[&str],
        _: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError> {
        panic!("host join_path")
    }
    async fn read_text_file(
        &self,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<String, FileError> {
        panic!("host read_text_file")
    }
    async fn read_text_lines(
        &self,
        _: &str,
        _: Option<usize>,
        _: Option<&CancellationToken>,
    ) -> Result<Vec<String>, FileError> {
        panic!("host read_text_lines")
    }
    async fn read_binary_file(
        &self,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<Vec<u8>, FileError> {
        panic!("host read_binary_file")
    }
    async fn write_file(
        &self,
        _: &str,
        _: FileContent,
        _: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        panic!("host write_file")
    }
    async fn append_file(
        &self,
        _: &str,
        _: FileContent,
        _: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        panic!("host append_file")
    }
    async fn rename_file(
        &self,
        _: &str,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        panic!("host rename_file")
    }
    async fn file_info(
        &self,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<FileInfo, FileError> {
        panic!("host file_info")
    }
    async fn list_dir(
        &self,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<Vec<FileInfo>, FileError> {
        panic!("host list_dir")
    }
    async fn canonical_path(
        &self,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError> {
        panic!("host canonical_path")
    }
    async fn exists(&self, _: &str, _: Option<&CancellationToken>) -> Result<bool, FileError> {
        panic!("host exists")
    }
    async fn create_dir(
        &self,
        _: &str,
        _: bool,
        _: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        panic!("host mkdir")
    }
    async fn remove(
        &self,
        _: &str,
        _: bool,
        _: bool,
        _: Option<&CancellationToken>,
    ) -> Result<(), FileError> {
        panic!("host remove")
    }
    async fn create_temp_dir(
        &self,
        _: Option<&str>,
        _: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError> {
        panic!("host temp dir")
    }
    async fn create_temp_file(
        &self,
        _: &str,
        _: &str,
        _: Option<&CancellationToken>,
    ) -> Result<PathBuf, FileError> {
        panic!("host temp file")
    }
    async fn cleanup(&self) {
        panic!("host fs cleanup")
    }
}

async fn client_context(backend: Arc<Backend>) -> (Arc<FileIORouter>, ExecutionToolContext) {
    let router = Arc::new(FileIORouter::new());
    router
        .configure(
            FileIOCapabilities {
                read_text: true,
                write_text: true,
            },
            Some(backend),
        )
        .await
        .unwrap();
    let env = TrapEnv::new();
    let ctx = ExecutionToolContext::new(env.clone(), Some(env)).with_file_io(router.clone());
    (router, ctx)
}
async fn run(
    tool: Arc<dyn AgentTool>,
    input: serde_json::Value,
) -> Result<rpi_agent::types::AgentToolResult, rpi_agent::error::AgentError> {
    tool.execute("fixture", input, CancellationToken::new(), Arc::new(|_| {}))
        .await
}
fn output(result: rpi_agent::types::AgentToolResult) -> String {
    match result.content.into_iter().next().expect("text result") {
        rpi_agent::types::TextContentOrImage::Text(text) => text.text,
        rpi_agent::types::TextContentOrImage::Image(_) => panic!("expected text, not image"),
    }
}
fn edit(path: &str, old: &str, new: &str) -> serde_json::Value {
    serde_json::json!({"path": path, "edits": [{"oldText": old, "newText": new}]})
}

#[test]
fn lexical_namespaces_are_explicit_and_case_preserving() {
    for (cwd, path, expected) in [
        ("/Workspace", "@a/../File", "/Workspace/File"),
        ("C:\\Work", "a\\..\\Buffer", "C:/Work/Buffer"),
        ("/Workspace", "D:\\Remote\\..\\Buffer", "D:/Buffer"),
        (
            "/Workspace",
            "\\\\Server\\Share\\a\\..\\Buffer",
            "//Server/Share/Buffer",
        ),
        ("//Server/Share/Work", "../Buffer", "//Server/Share/Buffer"),
        ("C:/Work", "/remote/Buffer", "/remote/Buffer"),
    ] {
        assert_eq!(resolve_client_path(Path::new(cwd), path).unwrap(), expected);
    }
    for path in [
        "C:relative",
        "\\rooted",
        "\\\\?\\C:\\x",
        "//./x",
        "file:///x",
        "~/x",
        "\\\\server",
        "",
    ] {
        assert!(
            resolve_client_path(Path::new("/Workspace"), path).is_err(),
            "{path}"
        );
    }
}

#[tokio::test]
async fn capabilities_four_combinations_keep_disk_and_buffer_separate() {
    for (read_text, write_text) in [(false, false), (true, false), (false, true), (true, true)] {
        let local = Arc::new(InMemoryExecutionEnv::with_cwd("/Workspace".into()));
        let local_path = local.absolute_path("buffer", None).await.unwrap();
        local
            .seed_file(&local_path.to_string_lossy(), b"disk".to_vec())
            .await;
        let backend = Backend::text("buffer");
        let router = Arc::new(FileIORouter::new());
        router
            .configure(
                FileIOCapabilities {
                    read_text,
                    write_text,
                },
                Some(backend.clone()),
            )
            .await
            .unwrap();
        let ctx =
            ExecutionToolContext::new(local.clone(), Some(local.clone())).with_file_io(router);
        let read = output(
            run(
                create_read_tool(&ctx, None),
                serde_json::json!({"path":"buffer"}),
            )
            .await
            .unwrap(),
        );
        assert_eq!(read, if read_text { "buffer" } else { "disk" });
        let write = run(
            create_write_tool(&ctx),
            serde_json::json!({"path":"buffer", "content":"written"}),
        )
        .await;
        assert_eq!(write.is_ok(), !read_text || write_text);
        let edit_result = run(create_edit_tool(&ctx), edit("buffer", "written", "edited")).await;
        assert_eq!(edit_result.is_ok(), read_text == write_text);
        let disk = local
            .read_text_file(&local_path.to_string_lossy(), None)
            .await
            .unwrap();
        let buffer = backend.text.lock().await.clone();
        match (read_text, write_text) {
            (false, false) => {
                assert_eq!(disk, "edited");
                assert_eq!(buffer, "buffer");
            }
            (true, false) => {
                assert_eq!(disk, "disk");
                assert_eq!(buffer, "buffer");
            }
            (false, true) => {
                assert_eq!(disk, "disk");
                assert_eq!(buffer, "written");
            }
            (true, true) => {
                assert_eq!(disk, "disk");
                assert_eq!(buffer, "edited");
            }
        }
    }
}

#[tokio::test]
async fn client_paths_never_probe_host_and_edit_preserves_bom_crlf() {
    let backend = Backend::text("\u{feff}one\r\ntwo\r\n");
    let (_, ctx) = client_context(backend.clone()).await;
    run(
        create_edit_tool(&ctx),
        edit("absent/../unsaved.png", "one\ntwo", "ONE\nTWO"),
    )
    .await
    .unwrap();
    assert_eq!(*backend.text.lock().await, "\u{feff}ONE\r\nTWO\r\n");
    let requests = backend.requests.lock().await.clone();
    assert!(
        matches!(&requests[0], FileIORequest::ReadText { absolute_path, range: FileIOReadRange::Complete, max_bytes: MAX_FILE_IO_BYTES } if absolute_path == "/Workspace/unsaved.png")
    );
    assert!(matches!(&requests[1], FileIORequest::WriteText { .. }));
    // The .png extension is not permission to fetch local binary image bytes.
    let result = run(
        create_read_tool(&ctx, None),
        serde_json::json!({"path":"unsaved.png"}),
    )
    .await
    .unwrap();
    assert!(output(result).contains("ONE"));
    run(
        create_write_tool(&ctx),
        serde_json::json!({"path":"missing/parents/file", "content":"new"}),
    )
    .await
    .unwrap();
}

#[tokio::test]
async fn ranged_read_offset_once_sentinel_eof_zero_and_default_cap() {
    let backend = Backend::text("one\ntwo\nthree\nfour\n");
    let (_, ctx) = client_context(backend.clone()).await;
    let read = create_read_tool(&ctx, None);
    let text = output(
        run(
            read.clone(),
            serde_json::json!({"path":"x", "offset":2, "limit":1}),
        )
        .await
        .unwrap(),
    );
    assert!(text.starts_with("two\n\n["));
    assert!(text.contains("offset=3"));
    assert!(text.contains("total line count unknown"));
    assert!(matches!(
        &backend.requests.lock().await[0],
        FileIORequest::ReadText {
            range: FileIOReadRange::Lines {
                offset_1based: 2,
                limit: 2
            },
            ..
        }
    ));
    assert_eq!(
        output(
            run(
                read.clone(),
                serde_json::json!({"path":"x", "offset":4, "limit":1})
            )
            .await
            .unwrap()
        ),
        "four"
    );
    assert_eq!(
        output(
            run(read.clone(), serde_json::json!({"path":"x", "offset":20}))
                .await
                .unwrap()
        ),
        ""
    );
    let zero = output(
        run(read.clone(), serde_json::json!({"path":"x", "limit":0}))
            .await
            .unwrap(),
    );
    assert!(!zero.contains("one"));
    *backend.text.lock().await = (0..2010).map(|_| "x").collect::<Vec<_>>().join("\n");
    let text = output(
        run(read, serde_json::json!({"path":"x", "limit":9000}))
            .await
            .unwrap(),
    );
    assert!(text.contains("offset=2001"));
    assert!(matches!(
        backend.requests.lock().await.last().unwrap(),
        FileIORequest::ReadText {
            range: FileIOReadRange::Lines { limit: 2001, .. },
            ..
        }
    ));
}

#[tokio::test]
async fn blank_line_pages_advance_without_skipping_at_the_byte_boundary() {
    let backend = Backend::text("");
    let (_, ctx) = client_context(backend.clone()).await;
    let read = create_read_tool(&ctx, None);
    for (response, offset, limit, next) in [
        ("\nnext\n".to_owned(), 1, 1, 2),
        ("one\n\nnext\n".to_owned(), 1, 2, 3),
        ("\n\nnext\n".to_owned(), 7, 2, 9),
        (
            format!(
                "{}\n\nnext\n",
                "é".repeat(crate::truncate::DEFAULT_MAX_BYTES / 2)
            ),
            5,
            2,
            6,
        ),
    ] {
        *backend.response.lock().await = Some(FileIOResponse::Text { content: response });
        let text = output(
            run(
                read.clone(),
                serde_json::json!({"path":"x", "offset":offset, "limit":limit}),
            )
            .await
            .unwrap(),
        );
        assert!(
            text.contains(&format!("Use offset={next} to continue")),
            "{text}"
        );
    }
    *backend.response.lock().await = None;
    *backend.text.lock().await = "\n".into();
    assert_eq!(
        output(
            run(read, serde_json::json!({"path":"x", "limit":1}))
                .await
                .unwrap()
        ),
        "\n"
    );
}

#[tokio::test]
async fn read_display_and_raw_utf8_limits_and_write_edit_bounds() {
    let backend = Backend::text(&"x".repeat(crate::truncate::DEFAULT_MAX_BYTES + 1));
    let (_, ctx) = client_context(backend.clone()).await;
    let text = output(
        run(
            create_read_tool(&ctx, None),
            serde_json::json!({"path":"x"}),
        )
        .await
        .unwrap(),
    );
    assert!(text.contains("display limit"));
    *backend.text.lock().await = "é".repeat(MAX_FILE_IO_BYTES / 2 + 1);
    assert!(run(
        create_read_tool(&ctx, None),
        serde_json::json!({"path":"x"})
    )
    .await
    .is_err());
    assert!(run(create_edit_tool(&ctx), edit("x", "é", "a"))
        .await
        .is_err());
    let before = backend.requests.lock().await.len();
    assert!(run(
        create_write_tool(&ctx),
        serde_json::json!({"path":"x", "content":"x".repeat(MAX_FILE_IO_BYTES + 1)})
    )
    .await
    .is_err());
    assert_eq!(backend.requests.lock().await.len(), before);
    *backend.text.lock().await = "a".into();
    assert!(run(
        create_edit_tool(&ctx),
        edit("x", "a", &"x".repeat(MAX_FILE_IO_BYTES + 1))
    )
    .await
    .is_err());
    assert_eq!(*backend.text.lock().await, "a");
}

#[tokio::test]
async fn errors_and_cancellation_never_fall_back_or_retry() {
    let backend = Backend::text("one");
    let (_, ctx) = client_context(backend.clone()).await;
    for code in [
        FileIOErrorCode::Unsupported,
        FileIOErrorCode::Denied,
        FileIOErrorCode::NotFound,
        FileIOErrorCode::InvalidResponse,
        FileIOErrorCode::TooLarge,
        FileIOErrorCode::Timeout,
        FileIOErrorCode::Cancelled,
        FileIOErrorCode::Disconnected,
    ] {
        *backend.error.lock().await = Some(code);
        let before = backend.requests.lock().await.len();
        assert!(run(
            create_read_tool(&ctx, None),
            serde_json::json!({"path":"x"})
        )
        .await
        .is_err());
        assert!(run(
            create_write_tool(&ctx),
            serde_json::json!({"path":"x", "content":"two"})
        )
        .await
        .is_err());
        assert!(run(create_edit_tool(&ctx), edit("x", "one", "two"))
            .await
            .is_err());
        assert_eq!(backend.requests.lock().await.len(), before + 3);
    }
    *backend.error.lock().await = None;
    let signal = CancellationToken::new();
    signal.cancel();
    let before = backend.requests.lock().await.len();
    assert!(create_write_tool(&ctx)
        .execute(
            "cancelled",
            serde_json::json!({"path":"x", "content":"two"}),
            signal,
            Arc::new(|_| {})
        )
        .await
        .is_err());
    assert_eq!(backend.requests.lock().await.len(), before);
    backend.cancel_after.store(true, Ordering::SeqCst);
    assert!(run(
        create_write_tool(&ctx),
        serde_json::json!({"path":"x", "content":"already committed"})
    )
    .await
    .is_err());
    assert_eq!(*backend.text.lock().await, "already committed");
    assert!(run(
        create_edit_tool(&ctx),
        edit("x", "already committed", "three")
    )
    .await
    .is_err());
    assert_eq!(*backend.text.lock().await, "already committed");
}

#[tokio::test]
async fn write_and_edit_share_lexical_queue_and_cancel_does_not_skip_turn() {
    let backend = Backend::text("old");
    let (_, ctx) = client_context(backend.clone()).await;
    backend.hold_write.store(true, Ordering::SeqCst);
    let write = tokio::spawn(run(
        create_write_tool(&ctx),
        serde_json::json!({"path":"dir/../x", "content":"new"}),
    ));
    backend.entered.notified().await;
    let signal = CancellationToken::new();
    let edit_tool = create_edit_tool(&ctx);
    let signal_for_task = signal.clone();
    let (started, ready) = tokio::sync::oneshot::channel();
    let pending_edit = tokio::spawn(async move {
        started.send(()).unwrap();
        edit_tool
            .execute(
                "edit",
                edit("x", "new", "edited"),
                signal_for_task,
                Arc::new(|_| {}),
            )
            .await
    });
    ready.await.unwrap();
    tokio::task::yield_now().await;
    assert_eq!(backend.requests.lock().await.len(), 1);
    signal.cancel();
    assert!(!pending_edit.is_finished());
    backend.release.notify_one();
    write.await.unwrap().unwrap();
    assert!(pending_edit.await.unwrap().is_err());
    assert_eq!(backend.requests.lock().await.len(), 1);
    run(create_edit_tool(&ctx), edit("x", "new", "edited"))
        .await
        .unwrap();
    assert_eq!(*backend.text.lock().await, "edited");
}

#[tokio::test]
async fn snapshot_freezes_backend_and_invalid_response_is_not_success() {
    let backend = Backend::text("original");
    let (router, _) = client_context(backend.clone()).await;
    let frozen = router.select(FileIOOperation::Edit).await.unwrap().unwrap();
    // Test snapshot stability only: real CLI configuration must require idle.
    router
        .configure(FileIOCapabilities::default(), None)
        .await
        .unwrap();
    assert!(router
        .select(FileIOOperation::Read)
        .await
        .unwrap()
        .is_none());
    frozen
        .write_text("/Workspace/x", "frozen".into(), &CancellationToken::new())
        .await
        .unwrap();
    assert_eq!(*backend.text.lock().await, "frozen");
    *backend.response.lock().await = Some(FileIOResponse::Text {
        content: "not a write ack".into(),
    });
    assert_eq!(
        frozen
            .write_text("/Workspace/x", "bad".into(), &CancellationToken::new())
            .await
            .unwrap_err()
            .code,
        FileIOErrorCode::InvalidResponse
    );
    *backend.response.lock().await = Some(FileIOResponse::Written);
    assert_eq!(
        frozen
            .read_text(
                "/Workspace/x",
                FileIOReadRange::Complete,
                &CancellationToken::new()
            )
            .await
            .unwrap_err()
            .code,
        FileIOErrorCode::InvalidResponse
    );
    *backend.response.lock().await = Some(FileIOResponse::Text {
        content: "one\ntwo\nthree".into(),
    });
    assert_eq!(
        frozen
            .read_text(
                "/Workspace/x",
                FileIOReadRange::Lines {
                    offset_1based: 1,
                    limit: 2
                },
                &CancellationToken::new()
            )
            .await
            .unwrap_err()
            .code,
        FileIOErrorCode::InvalidResponse
    );
}

#[tokio::test]
async fn failed_or_ambiguous_edit_does_not_write() {
    let backend = Backend::text("same\nsame");
    let (_, ctx) = client_context(backend.clone()).await;
    assert!(run(create_edit_tool(&ctx), edit("x", "same", "different"))
        .await
        .is_err());
    assert!(
        run(create_edit_tool(&ctx), edit("x", "missing", "different"))
            .await
            .is_err()
    );
    assert!(backend.requests.lock().await.iter().all(|r| matches!(
        r,
        FileIORequest::ReadText {
            range: FileIOReadRange::Complete,
            ..
        }
    )));
}