roder-tools 0.1.1

Agentic software development tools and SDKs for Roder.
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
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use regex::RegexBuilder;
use roder_api::remote_runner::{
    RemoteRunnerSession, RunnerCommandRequest, RunnerFileReadRequest, RunnerFileWriteRequest,
};
use roder_api::tools::ToolExecutionContext;
use roder_search::{INDEX_VERSION, SearchEngine, SearchMetadata, SearchOptions};

use crate::workspace::Workspace;

pub(crate) type WorkspaceBackendHandle = Arc<dyn WorkspaceBackend>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TextEdit {
    pub(crate) old_string: String,
    pub(crate) new_string: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct EditOutcome {
    pub(crate) path: String,
    pub(crate) replacements: usize,
}

#[async_trait]
pub(crate) trait WorkspaceBackend: Send + Sync + 'static {
    async fn read_text(&self, path: &str) -> anyhow::Result<(String, String)>;

    async fn list_files(&self, path: &str) -> anyhow::Result<(String, Vec<String>)>;

    async fn write_text(&self, path: &str, content: String) -> anyhow::Result<String>;

    async fn edit_text(
        &self,
        path: &str,
        old_string: &str,
        new_string: &str,
    ) -> anyhow::Result<Option<EditOutcome>>;

    async fn multi_edit_text(
        &self,
        path: &str,
        edits: Vec<TextEdit>,
    ) -> anyhow::Result<Result<EditOutcome, usize>>;

    async fn grep_search(
        &self,
        options: SearchOptions,
    ) -> anyhow::Result<(String, Vec<String>, SearchMetadata)>;

    async fn glob(&self, pattern: &str) -> anyhow::Result<crate::search::GlobOutcome>;

    async fn apply_patch(&self, patch: &str) -> anyhow::Result<String>;

    /// Called when something outside the file tools (a shell or exec command)
    /// may have changed the workspace, so cached search state is rebuilt.
    fn note_external_change(&self) {}
}

#[derive(Debug)]
pub(crate) struct LocalWorkspaceBackend {
    workspace: Workspace,
    searcher: Arc<Mutex<roder_search::WorkspaceSearcher>>,
}

/**
 * Workspace backend that executes every operation through a
 * `RemoteRunnerSession`. The `guard` only scopes and displays paths (use a
 * remote `Workspace` so resolution never touches local disk); content and
 * existence come from the runner.
 */
pub(crate) struct RunnerWorkspaceBackend {
    guard: Workspace,
    session: Arc<dyn RemoteRunnerSession>,
}

impl RunnerWorkspaceBackend {
    pub(crate) fn new(guard: Workspace, session: Arc<dyn RemoteRunnerSession>) -> Self {
        Self { guard, session }
    }
}

impl LocalWorkspaceBackend {
    pub(crate) fn new(workspace: Workspace) -> Self {
        let searcher = Arc::new(Mutex::new(roder_search::WorkspaceSearcher::new(
            workspace.root(),
        )));
        Self {
            workspace,
            searcher,
        }
    }

    fn invalidate_search_index(&self) -> anyhow::Result<()> {
        self.searcher
            .lock()
            .map_err(|_| anyhow::anyhow!("search index lock is poisoned"))?
            .invalidate();
        Ok(())
    }
}

pub(crate) fn backend_from_context_or_fallback(
    ctx: &ToolExecutionContext,
    fallback_workspace: &Workspace,
    fallback_backend: &WorkspaceBackendHandle,
) -> anyhow::Result<WorkspaceBackendHandle> {
    if let Some(remote) = ctx.handles.remote_workspace.as_ref() {
        let guard = Workspace::remote_with_read_roots(
            remote.root.clone(),
            fallback_workspace.path_scope(),
            remote.read_roots.clone(),
        )?;
        return Ok(Arc::new(RunnerWorkspaceBackend::new(
            guard,
            remote.session.clone(),
        )));
    }
    let Some(handle) = ctx.handles.workspace.as_ref() else {
        return Ok(fallback_backend.clone());
    };
    let Some(root) = handle.workspace_root() else {
        return Ok(fallback_backend.clone());
    };
    let workspace = Workspace::new_with_scope(root, fallback_workspace.path_scope())?;
    if workspace.root() == fallback_workspace.root() {
        return Ok(fallback_backend.clone());
    }
    Ok(Arc::new(LocalWorkspaceBackend::new(workspace)))
}

#[async_trait]
impl WorkspaceBackend for LocalWorkspaceBackend {
    async fn read_text(&self, path: &str) -> anyhow::Result<(String, String)> {
        let path = self.workspace.resolve_existing(path)?;
        let text = std::fs::read_to_string(&path)?;
        Ok((self.workspace.display(&path), text))
    }

    async fn list_files(&self, path: &str) -> anyhow::Result<(String, Vec<String>)> {
        let path = self.workspace.resolve_existing(path)?;
        let mut names = Vec::new();
        for entry in std::fs::read_dir(&path)? {
            let entry = entry?;
            let mut name = entry.file_name().to_string_lossy().to_string();
            if entry.file_type()?.is_dir() {
                name.push('/');
            }
            names.push(name);
        }
        names.sort();
        Ok((self.workspace.display(&path), names))
    }

    async fn write_text(&self, path: &str, content: String) -> anyhow::Result<String> {
        let path = self.workspace.resolve_for_write(path)?;
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(&path, content)?;
        self.invalidate_search_index()?;
        Ok(self.workspace.display(&path))
    }

    async fn edit_text(
        &self,
        path: &str,
        old_string: &str,
        new_string: &str,
    ) -> anyhow::Result<Option<EditOutcome>> {
        let path = self.workspace.resolve_existing(path)?;
        let text = std::fs::read_to_string(&path)?;
        let rel = self.workspace.display(&path);
        let (updated, outcome) = match roder_edit_core::apply_edit(
            rel.clone(),
            &text,
            old_string,
            new_string,
            roder_edit_core::EditOptions {
                fuzzy: roder_edit_core::EditMatchMode::Off,
                strip_line_numbers: false,
                reindent_inserted: false,
            },
        ) {
            Ok(result) => result,
            Err(_) => return Ok(None),
        };
        std::fs::write(&path, updated)?;
        self.invalidate_search_index()?;
        Ok(Some(EditOutcome {
            path: outcome.path,
            replacements: outcome.replacements,
        }))
    }

    async fn multi_edit_text(
        &self,
        path: &str,
        edits: Vec<TextEdit>,
    ) -> anyhow::Result<Result<EditOutcome, usize>> {
        let path = self.workspace.resolve_existing(path)?;
        let text = std::fs::read_to_string(&path)?;
        let rel = self.workspace.display(&path);
        let core_edits = edits
            .iter()
            .map(|edit| roder_edit_core::TextEdit {
                old_string: edit.old_string.clone(),
                new_string: edit.new_string.clone(),
            })
            .collect::<Vec<_>>();
        let (updated, outcome) = match roder_edit_core::apply_multi_edit(
            rel.clone(),
            &text,
            &core_edits,
            roder_edit_core::EditOptions {
                fuzzy: roder_edit_core::EditMatchMode::Off,
                strip_line_numbers: false,
                reindent_inserted: false,
            },
        ) {
            Ok(result) => result,
            Err(roder_edit_core::EditApplyError::OldStringNotFound { edit, .. }) => {
                return Ok(Err(edit.unwrap_or(0)));
            }
            Err(roder_edit_core::EditApplyError::OldStringAmbiguous { edit, .. }) => {
                return Ok(Err(edit.unwrap_or(0)));
            }
        };
        std::fs::write(&path, updated)?;
        self.invalidate_search_index()?;
        Ok(Ok(EditOutcome {
            path: outcome.path,
            replacements: outcome.replacements,
        }))
    }

    async fn grep_search(
        &self,
        options: SearchOptions,
    ) -> anyhow::Result<(String, Vec<String>, SearchMetadata)> {
        let workspace = self.workspace.clone();
        let searcher = self.searcher.clone();
        tokio::task::spawn_blocking(move || {
            let input_path = options.path.to_string_lossy().to_string();
            let start = workspace.resolve_existing(&input_path)?;
            /*
             * Hand the searcher the canonicalized path, not the raw input:
             * scope filtering inside the index is lexical, so `~`, symlinks,
             * or case differences in the raw path silently drop every match.
             */
            let mut options = options;
            options.path = start.clone();
            let mut searcher = searcher
                .lock()
                .map_err(|_| anyhow::anyhow!("search index lock is poisoned"))?;
            let output = searcher.search(&options)?;
            Ok((workspace.display(&start), output.lines, output.metadata))
        })
        .await
        .map_err(|err| anyhow::anyhow!("grep search task failed: {err}"))?
    }

    async fn glob(&self, pattern: &str) -> anyhow::Result<crate::search::GlobOutcome> {
        let workspace = self.workspace.clone();
        let pattern = pattern.to_string();
        tokio::task::spawn_blocking(move || {
            let pattern = crate::search::prepare_glob_pattern(workspace.root(), &pattern)?;
            let matcher = crate::search::compile_glob(&pattern)?;
            let mut matches = Vec::new();
            let mut files_considered = 0usize;
            crate::search::visit_files(workspace.root(), &mut |path| {
                files_considered += 1;
                let rel = workspace.display(path);
                if matcher.is_match(&rel) {
                    matches.push(rel);
                }
                Ok(())
            })?;
            matches.sort();
            Ok(crate::search::GlobOutcome {
                matches,
                files_considered,
            })
        })
        .await
        .map_err(|err| anyhow::anyhow!("glob task failed: {err}"))?
    }

    async fn apply_patch(&self, patch: &str) -> anyhow::Result<String> {
        let result = crate::patch::apply_patch_to_workspace(&self.workspace, patch).await?;
        self.invalidate_search_index()?;
        Ok(result)
    }

    fn note_external_change(&self) {
        let _ = self.invalidate_search_index();
    }
}

#[async_trait]
impl WorkspaceBackend for RunnerWorkspaceBackend {
    async fn read_text(&self, path: &str) -> anyhow::Result<(String, String)> {
        let path = self.guard.resolve_existing(path)?;
        let rel = self.guard.display(&path);
        let read = self
            .session
            .read_file(RunnerFileReadRequest {
                path: rel.clone().into(),
            })
            .await?;
        Ok((rel, String::from_utf8(read.contents)?))
    }

    async fn list_files(&self, path: &str) -> anyhow::Result<(String, Vec<String>)> {
        let path = self.guard.resolve_existing(path)?;
        let rel = self.guard.display(&path);
        // The workspace root displays as ""; quote "." so the glob stays inside the root.
        let quoted = shell_quote(if rel.is_empty() { "." } else { &rel });
        let command = format!(
            "for p in {quoted}/* {quoted}/.[!.]* {quoted}/..?*; do [ -e \"$p\" ] || continue; name=$(basename \"$p\"); if [ -d \"$p\" ]; then printf '%s/\\n' \"$name\"; else printf '%s\\n' \"$name\"; fi; done"
        );
        let output = self.run_shell(command).await?;
        let mut names = output.lines().map(ToString::to_string).collect::<Vec<_>>();
        names.sort();
        Ok((rel, names))
    }

    async fn write_text(&self, path: &str, content: String) -> anyhow::Result<String> {
        let path = self.guard.resolve_for_write(path)?;
        let rel = self.guard.display(&path);
        self.session
            .write_file(RunnerFileWriteRequest {
                path: rel.clone().into(),
                contents: content.into_bytes(),
            })
            .await?;
        Ok(rel)
    }

    /**
     * Routed through roder_edit_core like the local backend so an ambiguous
     * old_string is refused instead of silently rewriting the first
     * occurrence in the runner workspace (which the hosted sandbox
     * auto-commits).
     */
    async fn edit_text(
        &self,
        path: &str,
        old_string: &str,
        new_string: &str,
    ) -> anyhow::Result<Option<EditOutcome>> {
        let (rel, text) = self.read_text(path).await?;
        let (updated, outcome) = match roder_edit_core::apply_edit(
            rel.clone(),
            &text,
            old_string,
            new_string,
            roder_edit_core::EditOptions {
                fuzzy: roder_edit_core::EditMatchMode::Off,
                strip_line_numbers: false,
                reindent_inserted: false,
            },
        ) {
            Ok(result) => result,
            Err(_) => return Ok(None),
        };
        self.session
            .write_file(RunnerFileWriteRequest {
                path: rel.into(),
                contents: updated.into_bytes(),
            })
            .await?;
        Ok(Some(EditOutcome {
            path: outcome.path,
            replacements: outcome.replacements,
        }))
    }

    async fn multi_edit_text(
        &self,
        path: &str,
        edits: Vec<TextEdit>,
    ) -> anyhow::Result<Result<EditOutcome, usize>> {
        let (rel, text) = self.read_text(path).await?;
        let core_edits = edits
            .iter()
            .map(|edit| roder_edit_core::TextEdit {
                old_string: edit.old_string.clone(),
                new_string: edit.new_string.clone(),
            })
            .collect::<Vec<_>>();
        let (updated, outcome) = match roder_edit_core::apply_multi_edit(
            rel.clone(),
            &text,
            &core_edits,
            roder_edit_core::EditOptions {
                fuzzy: roder_edit_core::EditMatchMode::Off,
                strip_line_numbers: false,
                reindent_inserted: false,
            },
        ) {
            Ok(result) => result,
            Err(roder_edit_core::EditApplyError::OldStringNotFound { edit, .. }) => {
                return Ok(Err(edit.unwrap_or(0)));
            }
            Err(roder_edit_core::EditApplyError::OldStringAmbiguous { edit, .. }) => {
                return Ok(Err(edit.unwrap_or(0)));
            }
        };
        self.session
            .write_file(RunnerFileWriteRequest {
                path: rel.into(),
                contents: updated.into_bytes(),
            })
            .await?;
        Ok(Ok(EditOutcome {
            path: outcome.path,
            replacements: outcome.replacements,
        }))
    }

    /**
     * One shell round trip on the runner instead of a read_file per
     * candidate: every runner request is a full sandbox round trip, so the
     * per-file variant took minutes on real workspaces (node_modules
     * included) and starved the host's turn inactivity budget.
     */
    async fn grep_search(
        &self,
        options: SearchOptions,
    ) -> anyhow::Result<(String, Vec<String>, SearchMetadata)> {
        let started_at = std::time::Instant::now();
        let input_path = options.path.to_string_lossy().to_string();
        let start = self.guard.resolve_existing(&input_path)?;
        let start = self.guard.display(&start);
        /*
         * Validate regex queries locally first: the find pipeline below
         * absorbs grep's exit status (a no-match exit 1 is indistinguishable
         * from a bad pattern), so an invalid pattern must error here.
         */
        if options.regex {
            RegexBuilder::new(&options.query)
                .case_insensitive(!options.case_sensitive)
                .build()?;
        }
        let mut flags = String::from("-n");
        if !options.case_sensitive {
            flags.push('i');
        }
        if options.word_boundary {
            flags.push('w');
        }
        flags.push(if options.regex { 'E' } else { 'F' });
        let target = if start.is_empty() || start == "." {
            ".".to_string()
        } else {
            format!("./{start}")
        };
        /*
         * /dev/null forces the file:line: prefix even when a -exec batch
         * holds a single file; the sed strips find's ./ prefix so paths come
         * back workspace-relative. Regex queries run as ERE on the runner,
         * which can diverge from the local Rust regex engine on perl-style
         * classes like \d.
         */
        let command = format!(
            "find {} -type f ! -path './.git/*' ! -path './target/*' -exec grep {flags} -e {} /dev/null {{}} + 2>/dev/null | sed 's#^\\./##'",
            shell_quote(&target),
            shell_quote(&options.query),
        );
        let output = self.run_shell(command).await?;
        let matches = output.lines().map(ToString::to_string).collect::<Vec<_>>();
        let matched_files = matches
            .iter()
            .filter_map(|line| line.split(':').next())
            .collect::<std::collections::HashSet<_>>()
            .len();
        let metadata = SearchMetadata {
            engine: SearchEngine::Fallback,
            // The runner reports matches only, so files scanned is unknown.
            candidate_files: matched_files,
            verified_files: matched_files,
            stale: false,
            elapsed_ms: started_at.elapsed().as_millis(),
            index_version: INDEX_VERSION.to_string(),
            index_bytes: None,
            index_build_time_ms: None,
        };
        Ok((start, matches, metadata))
    }

    async fn glob(&self, pattern: &str) -> anyhow::Result<crate::search::GlobOutcome> {
        let pattern = crate::search::prepare_glob_pattern(self.guard.root(), pattern)?;
        let matcher = crate::search::compile_glob(&pattern)?;
        let output = self
            .run_shell(
                "find . -type f ! -path './.git/*' ! -path './target/*' | sed 's#^./##'"
                    .to_string(),
            )
            .await?;
        let mut files_considered = 0usize;
        let mut matches = output
            .lines()
            .inspect(|_| files_considered += 1)
            .filter(|rel| matcher.is_match(rel))
            .map(ToString::to_string)
            .collect::<Vec<_>>();
        matches.sort();
        Ok(crate::search::GlobOutcome {
            matches,
            files_considered,
        })
    }

    async fn apply_patch(&self, patch: &str) -> anyhow::Result<String> {
        crate::patch::apply_patch_to_runner_workspace(&self.guard, self.session.as_ref(), patch)
            .await
    }
}

impl RunnerWorkspaceBackend {
    async fn run_shell(&self, command: String) -> anyhow::Result<String> {
        let output = self
            .session
            .run_command(RunnerCommandRequest {
                command_id: "workspace-backend".to_string(),
                program: "sh".to_string(),
                args: vec!["-lc".to_string(), command],
                cwd: Some(self.guard.root().to_path_buf()),
                env: Vec::new(),
            })
            .await?;
        if output.exit_code != Some(0) {
            anyhow::bail!("{}", output.stderr);
        }
        Ok(output.stdout)
    }
}

pub(crate) fn shell_quote(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\\''"))
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use roder_api::policy_mode::PolicyMode;
    use roder_api::remote_runner::RemoteWorkspace;

    use super::*;
    use crate::remote_test_support::{RecordingRunnerSession, RecordingRunnerState};

    fn remote_context(state: Arc<RecordingRunnerState>) -> ToolExecutionContext {
        ToolExecutionContext::new("thread-remote", "turn-remote", PolicyMode::Default)
            .with_remote_workspace(Arc::new(RemoteWorkspace {
                session: Arc::new(RecordingRunnerSession { state }),
                root: PathBuf::from("/sandbox/workspace"),
                read_roots: Vec::new(),
            }))
    }

    fn local_fallback(prefix: &str) -> (PathBuf, Workspace, WorkspaceBackendHandle) {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = std::env::temp_dir().join(format!("{prefix}-{nanos}"));
        std::fs::create_dir_all(&root).unwrap();
        let workspace = Workspace::new(root.clone()).unwrap();
        let backend: WorkspaceBackendHandle =
            Arc::new(LocalWorkspaceBackend::new(workspace.clone()));
        (root, workspace, backend)
    }

    #[tokio::test]
    async fn remote_context_routes_file_operations_through_runner_session() {
        let state = Arc::new(RecordingRunnerState::default());
        let ctx = remote_context(state.clone());
        let (root, fallback_workspace, fallback_backend) = local_fallback("runner-backend-files");

        let backend =
            backend_from_context_or_fallback(&ctx, &fallback_workspace, &fallback_backend).unwrap();
        backend
            .write_text("notes/todo.txt", "remote contents".to_string())
            .await
            .unwrap();
        let (path, text) = backend.read_text("notes/todo.txt").await.unwrap();

        assert_eq!(path, "notes/todo.txt");
        assert_eq!(text, "remote contents");
        assert_eq!(
            state
                .files
                .lock()
                .unwrap()
                .get("notes/todo.txt")
                .map(|contents| contents.as_slice()),
            Some(b"remote contents".as_slice())
        );
        // Nothing may leak onto the local fallback workspace.
        assert!(!root.join("notes").exists());
        assert!(!std::path::Path::new("/sandbox/workspace").exists());

        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn remote_edit_matches_local_ambiguity_semantics() {
        let state = Arc::new(RecordingRunnerState::default());
        state
            .files
            .lock()
            .unwrap()
            .insert("notes/dup.txt".to_string(), b"same\nsame\n".to_vec());
        let ctx = remote_context(state.clone());
        let (root, fallback_workspace, fallback_backend) = local_fallback("runner-backend-edit");

        let backend =
            backend_from_context_or_fallback(&ctx, &fallback_workspace, &fallback_backend).unwrap();

        // Ambiguous old_string is refused; the runner file stays untouched.
        let ambiguous = backend
            .edit_text("notes/dup.txt", "same", "changed")
            .await
            .unwrap();
        assert_eq!(ambiguous, None);
        assert_eq!(
            state
                .files
                .lock()
                .unwrap()
                .get("notes/dup.txt")
                .map(|contents| contents.as_slice()),
            Some(b"same\nsame\n".as_slice())
        );
        let multi_ambiguous = backend
            .multi_edit_text(
                "notes/dup.txt",
                vec![TextEdit {
                    old_string: "same".to_string(),
                    new_string: "changed".to_string(),
                }],
            )
            .await
            .unwrap();
        assert_eq!(multi_ambiguous, Err(0));

        let unique = backend
            .edit_text("notes/dup.txt", "same\nsame", "one\ntwo")
            .await
            .unwrap();
        assert_eq!(
            unique,
            Some(EditOutcome {
                path: "notes/dup.txt".to_string(),
                replacements: 1,
            })
        );
        assert_eq!(
            state
                .files
                .lock()
                .unwrap()
                .get("notes/dup.txt")
                .map(|contents| contents.as_slice()),
            Some(b"one\ntwo\n".as_slice())
        );

        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn remote_grep_runs_a_single_server_side_search() {
        let state = Arc::new(RecordingRunnerState::default());
        let ctx = remote_context(state.clone());
        let (root, fallback_workspace, fallback_backend) = local_fallback("runner-backend-grep");

        let backend =
            backend_from_context_or_fallback(&ctx, &fallback_workspace, &fallback_backend).unwrap();
        let (start, matches, metadata) = backend
            .grep_search(SearchOptions::new("needle"))
            .await
            .unwrap();

        assert_eq!(start, "");
        // The recording session returns one canned stdout line per command.
        assert_eq!(matches, vec!["remote ok".to_string()]);
        assert!(metadata.elapsed_ms < 10_000);
        let commands = state.commands.lock().unwrap();
        assert_eq!(commands.len(), 1, "grep must be a single runner round trip");
        let script = commands[0].args.last().unwrap();
        assert!(script.contains("find '.' -type f"), "{script}");
        assert!(
            script.contains("grep -nF -e 'needle' /dev/null"),
            "{script}"
        );

        let _ = std::fs::remove_dir_all(root);
    }

    #[tokio::test]
    async fn remote_context_applies_codex_patches_through_runner_session() {
        let state = Arc::new(RecordingRunnerState::default());
        state
            .files
            .lock()
            .unwrap()
            .insert("src/lib.rs".to_string(), b"old line\n".to_vec());
        let ctx = remote_context(state.clone());
        let (root, fallback_workspace, fallback_backend) = local_fallback("runner-backend-patch");

        let backend =
            backend_from_context_or_fallback(&ctx, &fallback_workspace, &fallback_backend).unwrap();
        let summary = backend
            .apply_patch(
                "*** Begin Patch\n*** Update File: src/lib.rs\n@@\n-old line\n+new line\n*** End Patch\n",
            )
            .await
            .unwrap();

        assert_eq!(summary, "Success. Updated src/lib.rs");
        assert_eq!(
            state
                .files
                .lock()
                .unwrap()
                .get("src/lib.rs")
                .map(|contents| contents.as_slice()),
            Some(b"new line\n".as_slice())
        );
        assert!(!root.join("src").exists());

        let _ = std::fs::remove_dir_all(root);
    }
}