harn-hostlib 0.8.58

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! `ast.apply_node` — Tree-Sitter query → format-preserving replace.
//!
//! Locates one or more AST nodes via a Tree-Sitter query, then splices the
//! caller's `replacement` text in for each match by byte range. Because the
//! splice operates on the matched node's start/end bytes, leading
//! indentation, surrounding whitespace, and trailing trivia outside the
//! span are preserved verbatim.
//!
//! ## Wire shape
//!
//! Request fields (see `schemas/ast/apply_node.request.json`):
//!
//! - `path`: file to mutate.
//! - `query`: Tree-Sitter S-expression query with at least one capture.
//!   The capture named by `target_capture` (default `"target"`) is the
//!   span replaced; if the query has exactly one capture, that capture is
//!   the target regardless of its name.
//! - `replacement`: replacement text spliced in for every selected node.
//! - `select`: multi-match policy — one of `"unique" | "first" | "all" | "nth"`.
//!   Defaults to `"unique"`: matching exactly one node is required.
//! - `nth`: 1-based index when `select == "nth"`. Required in that mode.
//! - `language`: optional language hint (otherwise inferred from the
//!   extension).
//! - `target_capture`: capture name to treat as the replaceable span.
//! - `dry_run`: when true the file is left untouched but the response still
//!   carries the `preview` text the splice would produce.
//! - `validate`: when true (default) the post-edit source is re-parsed; if
//!   tree-sitter surfaces any ERROR/MISSING node the edit is rejected.
//! - `session_id`: routes the read+write through staged-fs (#1722) so the
//!   edit is atomic alongside siblings.
//!
//! Response is a tagged `result` union: `applied` on success and one of
//! `no_match | ambiguous | invalid_query | unsupported_language |
//! syntax_error` on failure. Bad selector / `nth` shapes surface as a
//! structured [`HostlibError::InvalidParameter`] before reaching the
//! union since they are caller bugs, not edit outcomes.

use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::Arc;

use harn_vm::process_sandbox::FsAccess;
use harn_vm::VmValue;
use streaming_iterator::StreamingIterator;
use tree_sitter::{Node, Query, QueryCursor, QueryError};

use crate::error::HostlibError;
use crate::tools::args::{
    build_dict, dict_arg, optional_bool, optional_int, optional_string, require_string, str_value,
};
use crate::tools::permissions::enforce_path_scope;

use super::edit_common::{
    first_syntax_error, format_query_error, read_source, resolve_target_capture, sha256_hex,
    write_source,
};
use super::language::{Language, TEXT_PATCH_FALLBACK};
use super::parse::parse_source;

const BUILTIN: &str = "hostlib_ast_apply_node";
const DEFAULT_TARGET_CAPTURE: &str = "target";

/// Multi-match selector.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Selector {
    /// Require exactly one match; otherwise `ambiguous`.
    Unique,
    /// Apply to the first match in document order.
    First,
    /// Apply to every match.
    All,
    /// Apply to the nth (1-based) match in document order.
    Nth(usize),
}

impl Selector {
    fn parse(raw: Option<&str>, nth: Option<i64>) -> Result<Self, HostlibError> {
        match raw.unwrap_or("unique") {
            "unique" => Ok(Self::Unique),
            "first" => Ok(Self::First),
            "all" => Ok(Self::All),
            "nth" => {
                let n = nth.ok_or(HostlibError::InvalidParameter {
                    builtin: BUILTIN,
                    param: "nth",
                    message: "`select: \"nth\"` requires a positive `nth` (1-based)".into(),
                })?;
                if n < 1 {
                    return Err(HostlibError::InvalidParameter {
                        builtin: BUILTIN,
                        param: "nth",
                        message: format!("`nth` must be >= 1, got {n}"),
                    });
                }
                Ok(Self::Nth(n as usize))
            }
            other => Err(HostlibError::InvalidParameter {
                builtin: BUILTIN,
                param: "select",
                message: format!(
                    "expected one of [\"unique\", \"first\", \"all\", \"nth\"], got `{other}`"
                ),
            }),
        }
    }
}

pub(super) fn run(args: &[VmValue]) -> Result<VmValue, HostlibError> {
    let raw = dict_arg(BUILTIN, args)?;
    let dict = raw.as_ref();

    let path_str = require_string(BUILTIN, dict, "path")?;
    let query_text = require_string(BUILTIN, dict, "query")?;
    let replacement = require_string(BUILTIN, dict, "replacement")?;
    let language_hint = optional_string(BUILTIN, dict, "language")?;
    let target_capture = optional_string(BUILTIN, dict, "target_capture")?
        .unwrap_or_else(|| DEFAULT_TARGET_CAPTURE.to_string());
    let select_raw = optional_string(BUILTIN, dict, "select")?;
    let nth_raw = match dict.get("nth") {
        None | Some(VmValue::Nil) => None,
        Some(VmValue::Int(n)) => Some(*n),
        Some(other) => {
            return Err(HostlibError::InvalidParameter {
                builtin: BUILTIN,
                param: "nth",
                message: format!("expected integer, got {}", other.type_name()),
            });
        }
    };
    let selector = Selector::parse(select_raw.as_deref(), nth_raw)?;
    let dry_run = optional_bool(BUILTIN, dict, "dry_run", false)?;
    let validate = optional_bool(BUILTIN, dict, "validate", true)?;
    let session_id = optional_string(BUILTIN, dict, "session_id")?;
    let max_bytes = optional_int(BUILTIN, dict, "max_bytes", 0)?;
    if max_bytes < 0 {
        return Err(HostlibError::InvalidParameter {
            builtin: BUILTIN,
            param: "max_bytes",
            message: "must be >= 0".into(),
        });
    }

    let path = PathBuf::from(&path_str);
    // Guards both the read below and the write-back; one scope check at the
    // strongest access the builtin can perform.
    enforce_path_scope(BUILTIN, &path, FsAccess::Write)?;
    let language = match Language::detect(&path, language_hint.as_deref()) {
        Some(l) => l,
        None => {
            return Ok(unsupported_language_response(
                &path_str,
                language_hint.as_deref(),
            ))
        }
    };

    let source = read_source(BUILTIN, &path, session_id.as_deref(), max_bytes as usize)?;

    let query = match Query::new(&language.ts_language(), &query_text) {
        Ok(q) => q,
        Err(err) => return Ok(invalid_query_response(&query_text, &err)),
    };

    let target_index = match resolve_target_capture(&query, &target_capture) {
        Ok(idx) => idx,
        Err(detail) => {
            return Ok(no_match_response(
                &path_str,
                &query_text,
                &target_capture,
                &detail,
            ))
        }
    };

    let tree = parse_source(&source, language).map_err(|err| HostlibError::Backend {
        builtin: BUILTIN,
        message: err.to_string(),
    })?;

    let spans = collect_target_spans(&query, &tree, source.as_bytes(), target_index);
    if spans.is_empty() {
        return Ok(no_match_response(
            &path_str,
            &query_text,
            &target_capture,
            "query produced zero captures",
        ));
    }

    let chosen = match select_spans(&spans, selector, &path_str, &query_text) {
        Ok(spans) => spans,
        Err(reason) => return Ok(reason),
    };

    let patched = splice(&source, &chosen, &replacement);

    if validate {
        if let Some(detail) = first_syntax_error(&patched, language) {
            return Ok(syntax_error_response(&path_str, &patched, &detail, &chosen));
        }
    }

    if !dry_run {
        write_source(BUILTIN, &path, &patched, session_id.as_deref())?;
    }

    Ok(applied_response(
        &path_str,
        &source,
        &patched,
        &chosen,
        &replacement,
        dry_run,
    ))
}

#[derive(Debug, Clone)]
struct Span {
    start_byte: usize,
    end_byte: usize,
    start_row: usize,
    start_col: usize,
    end_row: usize,
    end_col: usize,
    original: String,
}

fn collect_target_spans(
    query: &Query,
    tree: &tree_sitter::Tree,
    source_bytes: &[u8],
    target_index: u32,
) -> Vec<Span> {
    let mut cursor = QueryCursor::new();
    let mut matches = cursor.matches(query, tree.root_node(), source_bytes);
    let mut seen: BTreeMap<(usize, usize), Span> = BTreeMap::new();

    while let Some(m) = matches.next() {
        for capture in m.captures {
            if capture.index != target_index {
                continue;
            }
            insert_span(&mut seen, capture.node, source_bytes);
        }
    }

    let mut spans: Vec<Span> = seen.into_values().collect();
    spans.sort_by_key(|s| s.start_byte);
    spans
}

fn insert_span(into: &mut BTreeMap<(usize, usize), Span>, node: Node<'_>, source_bytes: &[u8]) {
    let key = (node.start_byte(), node.end_byte());
    into.entry(key).or_insert_with(|| {
        let start = node.start_position();
        let end = node.end_position();
        let original = std::str::from_utf8(&source_bytes[node.start_byte()..node.end_byte()])
            .unwrap_or_default()
            .to_string();
        Span {
            start_byte: node.start_byte(),
            end_byte: node.end_byte(),
            start_row: start.row,
            start_col: start.column,
            end_row: end.row,
            end_col: end.column,
            original,
        }
    });
}

fn select_spans(
    spans: &[Span],
    selector: Selector,
    path: &str,
    query: &str,
) -> Result<Vec<Span>, VmValue> {
    match selector {
        Selector::Unique => {
            if spans.len() > 1 {
                return Err(ambiguous_response(spans.len(), spans));
            }
            Ok(spans.to_vec())
        }
        Selector::First => Ok(spans.first().cloned().into_iter().collect()),
        Selector::All => Ok(spans.to_vec()),
        Selector::Nth(n) => match spans.get(n - 1) {
            Some(span) => Ok(vec![span.clone()]),
            None => Err(no_nth_response(spans.len(), n, path, query)),
        },
    }
}

/// Replace each chosen span's bytes with `replacement`. Spans are processed
/// in reverse byte order so earlier offsets remain valid; the original
/// `chosen` order is preserved by the caller.
fn splice(source: &str, chosen: &[Span], replacement: &str) -> String {
    let mut by_end: Vec<&Span> = chosen.iter().collect();
    by_end.sort_by_key(|s| std::cmp::Reverse(s.start_byte));
    let mut out = source.to_string();
    for span in by_end {
        out.replace_range(span.start_byte..span.end_byte, replacement);
    }
    out
}

// ---------------------------------------------------------------------------
// Response shaping
// ---------------------------------------------------------------------------

fn applied_response(
    path: &str,
    before: &str,
    after: &str,
    chosen: &[Span],
    replacement: &str,
    dry_run: bool,
) -> VmValue {
    VmValue::Dict(Arc::new(
        [
            ("result".to_string(), str_value("applied")),
            ("applied".to_string(), VmValue::Bool(true)),
            ("path".to_string(), str_value(path)),
            ("dry_run".to_string(), VmValue::Bool(dry_run)),
            ("match_count".to_string(), VmValue::Int(chosen.len() as i64)),
            (
                "edits".to_string(),
                VmValue::List(Arc::new(
                    chosen
                        .iter()
                        .map(|s| edit_to_value(s, replacement))
                        .collect(),
                )),
            ),
            (
                "before_sha256".to_string(),
                str_value(sha256_hex(before.as_bytes())),
            ),
            (
                "after_sha256".to_string(),
                str_value(sha256_hex(after.as_bytes())),
            ),
            ("preview".to_string(), str_value(after)),
        ]
        .into_iter()
        .collect(),
    ))
}

fn no_match_response(path: &str, query: &str, target_capture: &str, details: &str) -> VmValue {
    build_dict([
        ("result", str_value("no_match")),
        ("applied", VmValue::Bool(false)),
        ("path", str_value(path)),
        ("query", str_value(query)),
        ("target_capture", str_value(target_capture)),
        ("details", str_value(details)),
    ])
}

fn ambiguous_response(match_count: usize, spans: &[Span]) -> VmValue {
    VmValue::Dict(Arc::new(
        [
            ("result".to_string(), str_value("ambiguous")),
            ("applied".to_string(), VmValue::Bool(false)),
            ("match_count".to_string(), VmValue::Int(match_count as i64)),
            (
                "spans".to_string(),
                VmValue::List(Arc::new(spans.iter().map(span_to_value).collect())),
            ),
            (
                "details".to_string(),
                str_value(format!(
                    "`select: \"unique\"` requires a single match, found {match_count}; \
                     use `\"first\" | \"all\" | \"nth\"` to disambiguate"
                )),
            ),
        ]
        .into_iter()
        .collect(),
    ))
}

fn no_nth_response(match_count: usize, requested: usize, path: &str, query: &str) -> VmValue {
    build_dict([
        ("result", str_value("no_match")),
        ("applied", VmValue::Bool(false)),
        ("path", str_value(path)),
        ("query", str_value(query)),
        ("match_count", VmValue::Int(match_count as i64)),
        (
            "details",
            str_value(format!(
                "`select: \"nth\"` requested index {requested}, only {match_count} match(es) found"
            )),
        ),
    ])
}

fn invalid_query_response(query: &str, err: &QueryError) -> VmValue {
    build_dict([
        ("result", str_value("invalid_query")),
        ("applied", VmValue::Bool(false)),
        ("query", str_value(query)),
        ("details", str_value(format_query_error(err))),
        ("error_row", VmValue::Int(err.row as i64)),
        ("error_column", VmValue::Int(err.column as i64)),
    ])
}

fn unsupported_language_response(path: &str, hint: Option<&str>) -> VmValue {
    build_dict([
        ("result", str_value("unsupported_language")),
        ("applied", VmValue::Bool(false)),
        ("path", str_value(path)),
        (
            "details",
            str_value(format!(
                "could not infer a tree-sitter grammar for `{path}` (hint: {})",
                hint.unwrap_or("none")
            )),
        ),
        ("fallback_suggestion", str_value(TEXT_PATCH_FALLBACK)),
    ])
}

fn syntax_error_response(path: &str, after: &str, detail: &str, chosen: &[Span]) -> VmValue {
    build_dict([
        ("result", str_value("syntax_error")),
        ("applied", VmValue::Bool(false)),
        ("path", str_value(path)),
        ("details", str_value(detail)),
        ("match_count", VmValue::Int(chosen.len() as i64)),
        ("preview", str_value(after)),
    ])
}

fn edit_to_value(span: &Span, replacement: &str) -> VmValue {
    build_dict([
        ("start_byte", VmValue::Int(span.start_byte as i64)),
        ("end_byte", VmValue::Int(span.end_byte as i64)),
        ("start_row", VmValue::Int(span.start_row as i64)),
        ("start_col", VmValue::Int(span.start_col as i64)),
        ("end_row", VmValue::Int(span.end_row as i64)),
        ("end_col", VmValue::Int(span.end_col as i64)),
        ("original", str_value(&span.original)),
        ("replacement", str_value(replacement)),
    ])
}

fn span_to_value(span: &Span) -> VmValue {
    build_dict([
        ("start_byte", VmValue::Int(span.start_byte as i64)),
        ("end_byte", VmValue::Int(span.end_byte as i64)),
        ("start_row", VmValue::Int(span.start_row as i64)),
        ("start_col", VmValue::Int(span.start_col as i64)),
        ("end_row", VmValue::Int(span.end_row as i64)),
        ("end_col", VmValue::Int(span.end_col as i64)),
        ("text", str_value(&span.original)),
    ])
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn vm_string(s: &str) -> VmValue {
        VmValue::String(Arc::from(s))
    }

    fn dict(pairs: &[(&str, VmValue)]) -> VmValue {
        let mut map: BTreeMap<String, VmValue> = BTreeMap::new();
        for (k, v) in pairs {
            map.insert((*k).to_string(), v.clone());
        }
        VmValue::Dict(Arc::new(map))
    }

    fn field<'a>(value: &'a VmValue, key: &str) -> &'a VmValue {
        match value {
            VmValue::Dict(d) => d.get(key).expect("missing field"),
            _ => panic!("expected dict"),
        }
    }

    fn s(value: &VmValue) -> String {
        match value {
            VmValue::String(s) => s.to_string(),
            other => panic!("expected string, got {other:?}"),
        }
    }

    fn write_temp(extension: &str, source: &str) -> NamedTempFile {
        let mut file = tempfile::Builder::new()
            .suffix(&format!(".{extension}"))
            .tempfile()
            .expect("temp file");
        file.write_all(source.as_bytes()).expect("write source");
        file
    }

    fn invoke(payload: VmValue) -> VmValue {
        run(&[payload]).expect("apply_node runs")
    }

    #[test]
    fn replaces_unique_match_and_preserves_indentation() {
        let source = "fn alpha() {\n    let x = 1;\n}\n\nfn beta() {\n    let y = 2;\n}\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let query = r#"(function_item name: (identifier) @name (#eq? @name "beta")
                        body: (block) @target)"#;
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string(query)),
            ("replacement", vm_string("{ let y = 42; }")),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        // Indentation outside the replaced body is preserved.
        assert!(preview.contains("fn beta() {"));
        assert!(preview.contains("let y = 42"));
        assert!(!preview.contains("let y = 2"));
    }

    #[test]
    fn unique_selector_rejects_multiple_matches() {
        let source = "fn a() { 1 }\nfn b() { 2 }\nfn c() { 3 }\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ 0 }")),
        ]));
        assert_eq!(s(field(&result, "result")), "ambiguous");
    }

    #[test]
    fn select_all_replaces_every_match() {
        let source = "fn a() { 1 }\nfn b() { 2 }\nfn c() { 3 }\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ 0 }")),
            ("select", vm_string("all")),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        assert_eq!(preview.matches("{ 0 }").count(), 3);
        match field(&result, "match_count") {
            VmValue::Int(n) => assert_eq!(*n, 3),
            other => panic!("expected int, got {other:?}"),
        }
    }

    #[test]
    fn select_first_picks_lowest_offset_match() {
        let source = "fn a() { 1 }\nfn b() { 2 }\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ 99 }")),
            ("select", vm_string("first")),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        assert!(preview.starts_with("fn a() { 99 }"));
        assert!(preview.contains("fn b() { 2 }"));
    }

    #[test]
    fn select_nth_picks_specific_index() {
        let source = "fn a() { 1 }\nfn b() { 2 }\nfn c() { 3 }\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ 77 }")),
            ("select", vm_string("nth")),
            ("nth", VmValue::Int(2)),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        assert!(preview.contains("fn a() { 1 }"));
        assert!(preview.contains("fn b() { 77 }"));
        assert!(preview.contains("fn c() { 3 }"));
    }

    #[test]
    fn rejects_syntax_errors_after_edit() {
        let source = "fn alpha() { 1 }\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ ( }")), // unbalanced, produces ERROR node
        ]));
        assert_eq!(s(field(&result, "result")), "syntax_error");
        // Original file must remain untouched on rejection.
        let on_disk = std::fs::read_to_string(file.path()).expect("read");
        assert_eq!(on_disk, source);
    }

    #[test]
    fn reports_invalid_query() {
        let source = "fn a() {}\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("((((")),
            ("replacement", vm_string("{ }")),
        ]));
        assert_eq!(s(field(&result, "result")), "invalid_query");
    }

    #[test]
    fn reports_no_match() {
        let source = "fn alpha() {}\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            (
                "query",
                vm_string(
                    r#"(function_item name: (identifier) @name (#eq? @name "beta")
                       body: (block) @target)"#,
                ),
            ),
            ("replacement", vm_string("{ }")),
        ]));
        assert_eq!(s(field(&result, "result")), "no_match");
    }

    #[test]
    fn dry_run_returns_preview_without_writing() {
        let source = "fn alpha() { 1 }\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ 2 }")),
            ("dry_run", VmValue::Bool(true)),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        assert!(s(field(&result, "preview")).contains("{ 2 }"));
        let on_disk = std::fs::read_to_string(file.path()).expect("read");
        assert_eq!(on_disk, source);
    }

    #[test]
    fn supports_python_function_rewrite() {
        let source = "def greet(name):\n    return 'hi ' + name\n";
        let file = write_temp("py", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            (
                "query",
                vm_string("(function_definition name: (identifier) @name body: (block) @target)"),
            ),
            ("replacement", vm_string("return f'hi {name}!'")),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        assert!(preview.contains("f'hi {name}!'"));
    }

    #[test]
    fn supports_typescript_function_rewrite() {
        let source = "function greet(name: string) {\n  return 'hi ' + name;\n}\n";
        let file = write_temp("ts", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            (
                "query",
                vm_string("(function_declaration body: (statement_block) @target)"),
            ),
            ("replacement", vm_string("{\n  return `hi ${name}!`;\n}")),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        assert!(preview.contains("`hi ${name}!`"));
    }

    #[test]
    fn supports_go_function_rewrite() {
        let source =
            "package main\n\nfunc greet(name string) string {\n\treturn \"hi \" + name\n}\n";
        let file = write_temp("go", source);
        let path = file.path().to_string_lossy().to_string();
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            (
                "query",
                vm_string("(function_declaration body: (block) @target)"),
            ),
            (
                "replacement",
                vm_string("{\n\treturn \"hi \" + name + \"!\"\n}"),
            ),
        ]));
        assert_eq!(s(field(&result, "result")), "applied");
        let preview = s(field(&result, "preview"));
        assert!(preview.contains("name + \"!\""));
    }

    #[test]
    fn supports_swift_function_rewrite() {
        let source = "func greet(name: String) -> String {\n    return \"hi \\(name)\"\n}\n";
        let file = write_temp("swift", source);
        let path = file.path().to_string_lossy().to_string();
        let query = "(function_declaration body: (function_body) @target)";
        let result = invoke(dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string(query)),
            (
                "replacement",
                vm_string("{\n    return \"hi \\(name)!\"\n}"),
            ),
        ]));
        // tree-sitter-swift's node kind may differ; if so we tolerate
        // unsupported queries here, but the call itself should succeed
        // with a structured result.
        let result_kind = s(field(&result, "result"));
        assert!(
            matches!(
                result_kind.as_str(),
                "applied" | "no_match" | "invalid_query"
            ),
            "swift call returned unexpected result `{result_kind}`"
        );
    }

    #[test]
    fn rejects_unknown_selector() {
        let source = "fn a() {}\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let err = run(&[dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ }")),
            ("select", vm_string("everything-please")),
        ])])
        .expect_err("invalid selector must return error");
        match err {
            HostlibError::InvalidParameter { param, .. } => assert_eq!(param, "select"),
            other => panic!("expected InvalidParameter, got {other:?}"),
        }
    }

    #[test]
    fn nth_requires_index() {
        let source = "fn a() {}\n";
        let file = write_temp("rs", source);
        let path = file.path().to_string_lossy().to_string();
        let err = run(&[dict(&[
            ("path", vm_string(&path)),
            ("query", vm_string("(function_item body: (block) @target)")),
            ("replacement", vm_string("{ }")),
            ("select", vm_string("nth")),
        ])])
        .expect_err("nth without index is an error");
        match err {
            HostlibError::InvalidParameter { param, .. } => assert_eq!(param, "nth"),
            other => panic!("expected InvalidParameter, got {other:?}"),
        }
    }
}