harn-hostlib 0.8.112

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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//! Integration tests for the `code_index` host capability.
//!
//! Exercise every builtin end-to-end against a temp workspace: rebuild,
//! query, stats, imports_for, importers_of. The builtins are routed
//! through the same `BuiltinRegistry` plumbing the VM uses, so passing
//! these tests proves the schema-locked surface returns the right shape
//! for embedders.

use std::fs;
use std::sync::Arc;

use harn_hostlib::{
    code_index::CodeIndexCapability, BuiltinRegistry, HostlibCapability, RegisteredBuiltin,
};
use harn_vm::VmValue;

fn build_registry() -> (BuiltinRegistry, CodeIndexCapability) {
    let cap = CodeIndexCapability::new();
    let mut registry = BuiltinRegistry::new();
    cap.register_builtins(&mut registry);
    (registry, cap)
}

fn dict(entries: &[(&str, VmValue)]) -> VmValue {
    let mut map: harn_vm::value::DictMap = Default::default();
    for (k, v) in entries {
        map.insert((*k).to_string(), v.clone());
    }
    VmValue::dict(map)
}

fn call(registry: &BuiltinRegistry, name: &str, payload: VmValue) -> VmValue {
    let entry: &RegisteredBuiltin = registry.find(name).unwrap_or_else(|| {
        panic!("builtin {name} not registered");
    });
    (entry.handler)(&[payload]).unwrap_or_else(|err| {
        panic!("builtin {name} failed: {err:?}");
    })
}

fn extract_dict(value: &VmValue) -> Arc<harn_vm::value::DictMap> {
    match value {
        VmValue::Dict(d) => d.clone(),
        other => panic!("expected dict, got {other:?}"),
    }
}

fn extract_list(value: &VmValue) -> Arc<Vec<VmValue>> {
    match value {
        VmValue::List(l) => l.clone(),
        other => panic!("expected list, got {other:?}"),
    }
}

fn extract_int(value: &VmValue) -> i64 {
    match value {
        VmValue::Int(n) => *n,
        other => panic!("expected int, got {other:?}"),
    }
}

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

fn write_workspace() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    fs::create_dir_all(root.join("src")).unwrap();
    fs::create_dir_all(root.join("docs")).unwrap();
    fs::write(
        root.join("src/index.ts"),
        "import { helper } from \"./util\";\nimport { other } from \"./other\";\nexport const alphaToken = helper();\n",
    )
    .unwrap();
    fs::write(
        root.join("src/util.ts"),
        "export function helper() { return 'AlphaToken from util'; }\n",
    )
    .unwrap();
    fs::write(
        root.join("src/other.ts"),
        "import { helper } from \"./util\";\nexport function other() { return helper(); }\n",
    )
    .unwrap();
    fs::write(
        root.join("docs/notes.md"),
        "Random notes about alphaToken.\n",
    )
    .unwrap();
    fs::write(root.join("README.md"), "# project\nNo content here.\n").unwrap();
    dir
}

#[test]
fn rebuild_then_query_returns_hits_for_indexed_substring() {
    let dir = write_workspace();
    let (registry, _cap) = build_registry();

    let rebuild = call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );
    let r = extract_dict(&rebuild);
    assert!(extract_int(r.get("files_indexed").unwrap()) >= 4);

    let response = call(
        &registry,
        "hostlib_code_index_query",
        dict(&[("needle", VmValue::String(Arc::from("alphaToken")))]),
    );
    let response = extract_dict(&response);
    let results = extract_list(response.get("results").unwrap());
    let mut paths: Vec<String> = results
        .iter()
        .map(|hit| {
            let dict = extract_dict(hit);
            extract_str(dict.get("path").unwrap())
        })
        .collect();
    paths.sort();
    assert!(paths.contains(&"src/index.ts".to_string()));
    assert!(paths.contains(&"src/util.ts".to_string()));
    assert!(paths.contains(&"docs/notes.md".to_string()));
}

#[test]
fn query_respects_case_sensitive_flag() {
    let dir = write_workspace();
    let (registry, _) = build_registry();

    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );

    let case_sensitive = call(
        &registry,
        "hostlib_code_index_query",
        dict(&[
            ("needle", VmValue::String(Arc::from("alphaToken"))),
            ("case_sensitive", VmValue::Bool(true)),
        ]),
    );
    let cs = extract_dict(&case_sensitive);
    let cs_results = extract_list(cs.get("results").unwrap());

    let case_insensitive = call(
        &registry,
        "hostlib_code_index_query",
        dict(&[
            ("needle", VmValue::String(Arc::from("alphaToken"))),
            ("case_sensitive", VmValue::Bool(false)),
        ]),
    );
    let ci = extract_dict(&case_insensitive);
    let ci_results = extract_list(ci.get("results").unwrap());

    // Case-insensitive should never miss what case-sensitive sees, and
    // typically catches more.
    assert!(ci_results.len() >= cs_results.len());
    assert!(!cs_results.is_empty());
}

#[test]
fn query_truncates_to_max_results() {
    let dir = write_workspace();
    let (registry, _) = build_registry();

    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );
    let response = call(
        &registry,
        "hostlib_code_index_query",
        dict(&[
            ("needle", VmValue::String(Arc::from("export"))),
            ("max_results", VmValue::Int(1)),
        ]),
    );
    let response = extract_dict(&response);
    let results = extract_list(response.get("results").unwrap());
    assert_eq!(results.len(), 1);
    assert!(matches!(
        response.get("truncated").unwrap(),
        VmValue::Bool(true)
    ));
}

#[test]
fn query_scope_filter_restricts_results() {
    let dir = write_workspace();
    let (registry, _) = build_registry();

    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );

    let scope_value = VmValue::List(Arc::new(vec![VmValue::String(Arc::from("src"))]));
    let response = call(
        &registry,
        "hostlib_code_index_query",
        dict(&[
            ("needle", VmValue::String(Arc::from("alphaToken"))),
            ("scope", scope_value),
        ]),
    );
    let response = extract_dict(&response);
    let results = extract_list(response.get("results").unwrap());
    let paths: Vec<String> = results
        .iter()
        .map(|hit| {
            let dict = extract_dict(hit);
            extract_str(dict.get("path").unwrap())
        })
        .collect();
    assert!(paths.iter().all(|p| p.starts_with("src/")));
}

#[test]
fn imports_for_returns_resolved_and_unresolved() {
    let dir = write_workspace();
    let (registry, _) = build_registry();

    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );
    let response = call(
        &registry,
        "hostlib_code_index_imports_for",
        dict(&[("path", VmValue::String(Arc::from("src/index.ts")))]),
    );
    let response = extract_dict(&response);
    let imports = extract_list(response.get("imports").unwrap());
    let pairs: Vec<(String, Option<String>, String)> = imports
        .iter()
        .map(|item| {
            let d = extract_dict(item);
            let module = extract_str(d.get("module").unwrap());
            let resolved = match d.get("resolved_path").unwrap() {
                VmValue::Nil => None,
                VmValue::String(s) => Some(s.to_string()),
                other => panic!("expected str|nil, got {other:?}"),
            };
            let kind = extract_str(d.get("kind").unwrap());
            (module, resolved, kind)
        })
        .collect();

    let util_resolution = pairs
        .iter()
        .find(|(m, _, _)| m.contains("./util"))
        .expect("./util import surfaced");
    assert_eq!(util_resolution.1.as_deref(), Some("src/util.ts"));
    assert_eq!(util_resolution.2, "import");

    // The other import points at a path that doesn't exist in the
    // workspace — it should land in the response with `resolved_path: nil`.
    let other_resolution = pairs
        .iter()
        .find(|(m, _, _)| m.contains("./other"))
        .expect("./other import surfaced");
    assert_eq!(other_resolution.1.as_deref(), Some("src/other.ts"));
}

#[test]
fn importers_of_returns_paths_in_sorted_order() {
    let dir = write_workspace();
    let (registry, _) = build_registry();

    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );
    let response = call(
        &registry,
        "hostlib_code_index_importers_of",
        dict(&[("module", VmValue::String(Arc::from("src/util.ts")))]),
    );
    let response = extract_dict(&response);
    let importers = extract_list(response.get("importers").unwrap());
    let paths: Vec<String> = importers.iter().map(extract_str).collect();
    assert_eq!(paths, vec!["src/index.ts", "src/other.ts"]);
}

#[test]
fn stats_reflect_index_state() {
    let (registry, _) = build_registry();

    let pre = extract_dict(&call(&registry, "hostlib_code_index_stats", dict(&[])));
    assert_eq!(extract_int(pre.get("indexed_files").unwrap()), 0);
    assert!(matches!(
        pre.get("last_rebuild_unix_ms").unwrap(),
        VmValue::Nil
    ));

    let dir = write_workspace();
    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );
    let post = extract_dict(&call(&registry, "hostlib_code_index_stats", dict(&[])));
    assert!(extract_int(post.get("indexed_files").unwrap()) >= 4);
    assert!(extract_int(post.get("trigrams").unwrap()) > 0);
    assert!(extract_int(post.get("words").unwrap()) > 0);
    assert!(extract_int(post.get("memory_bytes").unwrap()) > 0);
    assert!(matches!(
        post.get("last_rebuild_unix_ms").unwrap(),
        VmValue::Int(_)
    ));
}

#[test]
fn rebuild_rejects_missing_root() {
    let (registry, _) = build_registry();
    let entry = registry.find("hostlib_code_index_rebuild").unwrap();
    let err = (entry.handler)(&[dict(&[(
        "root",
        VmValue::String(Arc::from("/definitely/not/here/zzz")),
    )])])
    .expect_err("missing root must error");
    let msg = format!("{err}");
    assert!(msg.contains("root"), "error mentions the param: {msg}");
}

#[test]
fn empty_workspace_returns_empty_responses() {
    let (registry, _) = build_registry();
    // No rebuild yet — every read op should still respond with a dict
    // shape rather than panicking.
    let q = extract_dict(&call(
        &registry,
        "hostlib_code_index_query",
        dict(&[("needle", VmValue::String(Arc::from("anything")))]),
    ));
    assert!(extract_list(q.get("results").unwrap()).is_empty());

    let imps = extract_dict(&call(
        &registry,
        "hostlib_code_index_imports_for",
        dict(&[("path", VmValue::String(Arc::from("src/main.rs")))]),
    ));
    assert!(extract_list(imps.get("imports").unwrap()).is_empty());

    let imps_of = extract_dict(&call(
        &registry,
        "hostlib_code_index_importers_of",
        dict(&[("module", VmValue::String(Arc::from("anything")))]),
    ));
    assert!(extract_list(imps_of.get("importers").unwrap()).is_empty());
}

// === Recall@K canary for the substring `query` scorer ===
//
// `run_query` ranks hits by raw substring `match_count` (desc, path asc) — no
// IDF or length normalization. This gold fixture pins what that scorer actually
// recovers so a future scorer change has to MEASURE against it rather than
// churn the load-bearing index blindly.
//
// Findings these assertions lock in (see PR investigation):
//   * Rare-symbol localization — the cheap-model grounding pain point — already
//     achieves recall@5 = 1.0 under raw count. There are few matching files and
//     they all fit in the top-K, so the scorer is NOT the bottleneck here.
//   * The one demonstrable count-scorer blind spot is recall@1 for a symbol
//     DEFINITION when a non-definition file mentions the symbol more times
//     (e.g. a test that repeats it). The definition is ranked 2nd, not buried —
//     recall@3 recovers it. A length/IDF-normalized scorer was prototyped and
//     REGRESSED other cases (common-token recall@5 1.0 -> 0.5) with no rare-
//     symbol gain, so the scorer is intentionally left as raw count.

/// Build a workspace where a rare symbol is defined once but mentioned many
/// times in a non-definition test file, plus several single-mention callers and
/// unrelated noise files. Mirrors the realistic "localize the definition" task.
fn write_recall_workspace() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    fs::create_dir_all(root.join("src")).unwrap();
    fs::create_dir_all(root.join("tests")).unwrap();

    // Definition file: the symbol appears twice (decl + impl).
    fs::write(
        root.join("src/retry_budget.rs"),
        "pub struct RetryBudget { remaining: u32 }\nimpl RetryBudget { pub fn new() -> Self { RetryBudget { remaining: 3 } } }\n",
    )
    .unwrap();
    // Non-definition test file mentions the symbol many times (high raw count).
    fs::write(
        root.join("tests/retry_budget_test.rs"),
        "let b = RetryBudget::new();\n".repeat(20),
    )
    .unwrap();
    // Single-mention callers.
    for i in 0..6 {
        fs::write(
            root.join(format!("src/caller{i}.rs")),
            "fn run() { let b = RetryBudget::new(); }\n",
        )
        .unwrap();
    }
    // Noise files without the symbol.
    for i in 0..8 {
        fs::write(root.join(format!("src/noise{i}.rs")), "fn unrelated() {}\n").unwrap();
    }
    dir
}

fn query_ranked_paths(registry: &BuiltinRegistry, needle: &str) -> Vec<String> {
    let response = call(
        registry,
        "hostlib_code_index_query",
        dict(&[
            ("needle", VmValue::String(Arc::from(needle))),
            ("max_results", VmValue::Int(100)),
        ]),
    );
    let response = extract_dict(&response);
    extract_list(response.get("results").unwrap())
        .iter()
        .map(|hit| extract_str(extract_dict(hit).get("path").unwrap()))
        .collect()
}

fn recall_at_k(ranked: &[String], expected: &[&str], k: usize) -> f64 {
    let top = &ranked[..ranked.len().min(k)];
    let hit = expected
        .iter()
        .filter(|e| top.iter().any(|p| p == *e))
        .count();
    hit as f64 / expected.len() as f64
}

#[test]
fn query_recall_gold_fixture_rare_symbol_and_definition_burial() {
    let dir = write_recall_workspace();
    let (registry, _) = build_registry();
    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(dir.path().to_string_lossy().to_string())),
        )]),
    );

    let ranked = query_ranked_paths(&registry, "RetryBudget");

    // Rare-symbol recall: every file that references the symbol is a relevant
    // localization target, and they all surface within the top-K. recall@5 is
    // perfect — the count scorer is not the rare-symbol grounding bottleneck.
    let all_refs = [
        "src/retry_budget.rs",
        "tests/retry_budget_test.rs",
        "src/caller0.rs",
        "src/caller1.rs",
        "src/caller2.rs",
        "src/caller3.rs",
        "src/caller4.rs",
        "src/caller5.rs",
    ];
    assert_eq!(
        recall_at_k(&ranked, &["src/retry_budget.rs"], 5),
        1.0,
        "definition must be within top-5 under raw count, got order {ranked:?}"
    );
    assert_eq!(
        ranked.len(),
        all_refs.len(),
        "every referencing file returns (no false drops), got {ranked:?}"
    );

    // Documented blind spot: the high-mention test file outranks the
    // definition, so recall@1 for the *definition* is 0. recall@3 recovers it.
    // If a scorer change flips these, this canary forces a re-measure.
    assert_eq!(
        ranked[0], "tests/retry_budget_test.rs",
        "raw count ranks the most-mentions file first, got {ranked:?}"
    );
    assert_eq!(
        recall_at_k(&ranked, &["src/retry_budget.rs"], 1),
        0.0,
        "known recall@1 definition-burial under raw count, order {ranked:?}"
    );
    assert_eq!(
        recall_at_k(&ranked, &["src/retry_budget.rs"], 3),
        1.0,
        "definition recovered by recall@3, order {ranked:?}"
    );
}

// === Additive read-only secondary roots (issue #2403 follow-up) ===

/// Build a tiny "dependency/SDK" root containing a symbol that exists
/// nowhere in the project, mimicking e.g. the macOS IOKit header that
/// declares `kIOPSTimeToFullChargeKey`.
fn write_dep_root() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    fs::create_dir_all(dir.path().join("IOKit")).unwrap();
    fs::write(
        dir.path().join("IOKit/IOPowerSources.h"),
        "#define kIOPSTimeToFullChargeKey \"Time to Full Charge\"\n\
         int IOPSGetTimeRemainingEstimate(void);\n",
    )
    .unwrap();
    dir
}

/// Build a minimal project workspace with its own unique symbol.
fn write_project_root() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    fs::create_dir_all(dir.path().join("src")).unwrap();
    fs::write(
        dir.path().join("src/battery.rs"),
        "pub fn project_only_symbol() -> i32 { 7 }\n",
    )
    .unwrap();
    dir
}

fn query_roots(registry: &BuiltinRegistry, needle: &str) -> Vec<(String, Option<String>)> {
    let resp = call(
        registry,
        "hostlib_code_index_query",
        dict(&[("needle", VmValue::String(Arc::from(needle)))]),
    );
    let d = extract_dict(&resp);
    extract_list(d.get("results").unwrap())
        .iter()
        .map(|hit| {
            let hd = extract_dict(hit);
            let path = extract_str(hd.get("path").unwrap());
            let root = match hd.get("root") {
                Some(VmValue::String(s)) => Some(s.to_string()),
                _ => None,
            };
            (path, root)
        })
        .collect()
}

#[test]
fn readonly_roots_do_not_clobber_the_project_index() {
    let project = write_project_root();
    let dep = write_dep_root();
    let (registry, _cap) = build_registry();
    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(project.path().to_string_lossy().to_string())),
        )]),
    );

    // Project symbol is discoverable before adding dep roots.
    assert!(
        !query_roots(&registry, "project_only_symbol").is_empty(),
        "project symbol must be indexed by rebuild"
    );

    let add = call(
        &registry,
        "hostlib_code_index_add_readonly_roots",
        dict(&[(
            "roots",
            VmValue::List(Arc::new(vec![VmValue::String(Arc::from(
                dep.path().to_string_lossy().to_string(),
            ))])),
        )]),
    );
    assert_eq!(
        extract_int(extract_dict(&add).get("readonly_root_count").unwrap()),
        1
    );

    // The project index SURVIVES — adding a dep root did not flip the slot.
    let project_hits = query_roots(&registry, "project_only_symbol");
    assert!(
        project_hits
            .iter()
            .any(|(p, root)| p == "src/battery.rs" && root.is_none()),
        "project symbol must still resolve against the primary index after \
         adding dep roots, got {project_hits:?}"
    );
    // stats still reflect the project, not the dependency.
    let stats = extract_dict(&call(&registry, "hostlib_code_index_stats", VmValue::Nil));
    assert_eq!(
        extract_int(stats.get("indexed_files").unwrap()),
        1,
        "primary stats must count only project files"
    );
}

#[test]
fn symbol_only_in_dep_root_is_found_via_query() {
    let project = write_project_root();
    let dep = write_dep_root();
    let (registry, _cap) = build_registry();
    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(project.path().to_string_lossy().to_string())),
        )]),
    );

    // Before adding the dep root, the SDK symbol is undiscoverable.
    assert!(
        query_roots(&registry, "kIOPSTimeToFullChargeKey").is_empty(),
        "dep symbol must not resolve before the dep root is added"
    );

    call(
        &registry,
        "hostlib_code_index_add_readonly_roots",
        dict(&[(
            "roots",
            VmValue::List(Arc::new(vec![VmValue::String(Arc::from(
                dep.path().to_string_lossy().to_string(),
            ))])),
        )]),
    );

    let hits = query_roots(&registry, "kIOPSTimeToFullChargeKey");
    assert_eq!(
        hits.len(),
        1,
        "dep symbol must resolve via read-only root, got {hits:?}"
    );
    let (path, root) = &hits[0];
    assert_eq!(path, "IOKit/IOPowerSources.h");
    assert!(
        root.as_deref()
            .is_some_and(|r| r.contains(dep.path().file_name().unwrap().to_str().unwrap())),
        "dep hit must be tagged with its dependency root, got {root:?}"
    );

    // And the discovered file is readable back through read_range.
    let read = call(
        &registry,
        "hostlib_code_index_read_range",
        dict(&[("path", VmValue::String(Arc::from(path.as_str())))]),
    );
    let content = extract_str(extract_dict(&read).get("content").unwrap());
    assert!(content.contains("kIOPSTimeToFullChargeKey"));
}

#[test]
fn dep_root_path_is_read_only_writes_are_rejected() {
    let project = write_project_root();
    let dep = write_dep_root();
    let (registry, _cap) = build_registry();
    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(project.path().to_string_lossy().to_string())),
        )]),
    );
    call(
        &registry,
        "hostlib_code_index_add_readonly_roots",
        dict(&[(
            "roots",
            VmValue::List(Arc::new(vec![VmValue::String(Arc::from(
                dep.path().to_string_lossy().to_string(),
            ))])),
        )]),
    );

    // reindex_file is a WRITE path scoped to the primary index — a dep-root
    // path must be rejected (it is not inside the project root).
    let dep_abs = dep.path().join("IOKit/IOPowerSources.h");
    let reindex = registry
        .find("hostlib_code_index_reindex_file")
        .expect("registered");
    let result = (reindex.handler)(&[dict(&[(
        "path",
        VmValue::String(Arc::from(dep_abs.to_string_lossy().to_string())),
    )])]);
    assert!(
        result.is_err(),
        "reindex_file must reject a dependency-root path (read-only scope), got {result:?}"
    );
}

#[test]
fn add_readonly_roots_is_idempotent() {
    let project = write_project_root();
    let dep = write_dep_root();
    let (registry, _cap) = build_registry();
    call(
        &registry,
        "hostlib_code_index_rebuild",
        dict(&[(
            "root",
            VmValue::String(Arc::from(project.path().to_string_lossy().to_string())),
        )]),
    );

    let dep_val = VmValue::List(Arc::new(vec![VmValue::String(Arc::from(
        dep.path().to_string_lossy().to_string(),
    ))]));
    let first = call(
        &registry,
        "hostlib_code_index_add_readonly_roots",
        dict(&[("roots", dep_val.clone())]),
    );
    let second = call(
        &registry,
        "hostlib_code_index_add_readonly_roots",
        dict(&[("roots", dep_val)]),
    );

    // Re-adding the same root must not append a duplicate.
    assert_eq!(
        extract_int(extract_dict(&first).get("readonly_root_count").unwrap()),
        1
    );
    assert_eq!(
        extract_int(extract_dict(&second).get("readonly_root_count").unwrap()),
        1,
        "re-adding the same dep root must be idempotent, not duplicated"
    );

    // The symbol still resolves exactly once.
    let hits = query_roots(&registry, "kIOPSTimeToFullChargeKey");
    assert_eq!(
        hits.len(),
        1,
        "idempotent re-add must not duplicate hits, got {hits:?}"
    );
}

#[test]
fn read_range_reads_raw_path_when_primary_index_is_unbuilt() {
    // Regression for the read-range fallback dropped by the read-only
    // secondary-roots work (#3352): before any `rebuild`, the primary index
    // slot is `None`, so there is no workspace root to confine the path
    // against. `read_range` must still read the file straight off disk (the
    // pre-#3352 `None => PathBuf::from(&path)` behavior) instead of
    // rejecting it as out-of-scope. This is the path `agent_run` and
    // eval/verify take when scanning a process-output temp file to surface
    // buried test-failure lines.
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("run-output.txt");
    fs::write(&file, "=== RUN\n--- FAIL: TestX\nfoo_test.go:42: boom\n").unwrap();

    let (registry, _cap) = build_registry(); // no rebuild — primary slot is None
    let read = call(
        &registry,
        "hostlib_code_index_read_range",
        dict(&[(
            "path",
            VmValue::String(Arc::from(file.to_string_lossy().as_ref())),
        )]),
    );
    let content = extract_str(extract_dict(&read).get("content").unwrap());
    assert!(
        content.contains("foo_test.go:42: boom"),
        "unbuilt-index read_range must return raw file contents, got {content:?}"
    );
}