Skip to main content

harn_hostlib/code_index/
builtins.rs

1//! Host-builtin handlers for the `code_index` module.
2//!
3//! Each handler shape mirrors the schema in
4//! `schemas/code_index/<method>.{request,response}.json`. A single shared
5//! [`SharedIndex`] cell is captured by the closure of every handler so
6//! every builtin observes the same in-memory state. The `current_agent_id`
7//! op also reads from the capability's `current_agent` slot, but for
8//! every other op the index mutex is the source of truth.
9
10use std::collections::HashSet;
11use std::path::PathBuf;
12use std::sync::{Arc, Mutex};
13use std::time::Instant;
14
15use harn_vm::VmValue;
16
17use super::agents::AgentId;
18use super::file_table::{fnv1a64, FileId};
19use super::imports;
20use super::state::{now_unix_ms, IndexState};
21use super::trigram;
22use super::versions::EditOp;
23use crate::error::HostlibError;
24use crate::tools::args::{
25    build_dict, dict_arg, optional_bool, optional_int_list, optional_string, optional_string_list,
26    require_string, str_value, to_agent_path, to_agent_path_str,
27};
28use crate::value_args;
29
30/// Shared, mutable cell carrying the (at most one) live workspace index.
31/// `Mutex` rather than `RwLock` because rebuilds flip the slot wholesale
32/// and every mutating op (record_edit, agent_register, lock_try, etc.)
33/// needs exclusive access. Single-threaded VM scripts pay no real cost
34/// from the choice; embedders that fan out across threads are still
35/// safe because the mutex serialises everyone.
36pub type SharedIndex = Arc<Mutex<Option<IndexState>>>;
37
38// === Builtin name constants ===
39//
40// Every handler routes through one of these. They double as the module's
41// public surface area so cross-repo schema-drift tests can discover them
42// without scraping source.
43
44pub(super) const BUILTIN_QUERY: &str = "hostlib_code_index_query";
45pub(super) const BUILTIN_REBUILD: &str = "hostlib_code_index_rebuild";
46pub(super) const BUILTIN_STATS: &str = "hostlib_code_index_stats";
47pub(super) const BUILTIN_IMPORTS_FOR: &str = "hostlib_code_index_imports_for";
48pub(super) const BUILTIN_IMPORTERS_OF: &str = "hostlib_code_index_importers_of";
49
50pub(super) const BUILTIN_PATH_TO_ID: &str = "hostlib_code_index_path_to_id";
51pub(super) const BUILTIN_ID_TO_PATH: &str = "hostlib_code_index_id_to_path";
52pub(super) const BUILTIN_FILE_IDS: &str = "hostlib_code_index_file_ids";
53pub(super) const BUILTIN_FILE_META: &str = "hostlib_code_index_file_meta";
54pub(super) const BUILTIN_FILE_HASH: &str = "hostlib_code_index_file_hash";
55pub(super) const BUILTIN_FILE_HASH_SNAPSHOT: &str = "hostlib_code_index_file_hash_snapshot";
56
57pub(super) const BUILTIN_READ_RANGE: &str = "hostlib_code_index_read_range";
58pub(super) const BUILTIN_REINDEX_FILE: &str = "hostlib_code_index_reindex_file";
59pub(super) const BUILTIN_TRIGRAM_QUERY: &str = "hostlib_code_index_trigram_query";
60pub(super) const BUILTIN_EXTRACT_TRIGRAMS: &str = "hostlib_code_index_extract_trigrams";
61pub(super) const BUILTIN_WORD_GET: &str = "hostlib_code_index_word_get";
62pub(super) const BUILTIN_DEPS_GET: &str = "hostlib_code_index_deps_get";
63pub(super) const BUILTIN_OUTLINE_GET: &str = "hostlib_code_index_outline_get";
64
65pub(super) const BUILTIN_CURRENT_SEQ: &str = "hostlib_code_index_current_seq";
66pub(super) const BUILTIN_CHANGES_SINCE: &str = "hostlib_code_index_changes_since";
67pub(super) const BUILTIN_VERSION_RECORD: &str = "hostlib_code_index_version_record";
68
69pub(super) const BUILTIN_AGENT_REGISTER: &str = "hostlib_code_index_agent_register";
70pub(super) const BUILTIN_AGENT_HEARTBEAT: &str = "hostlib_code_index_agent_heartbeat";
71pub(super) const BUILTIN_AGENT_UNREGISTER: &str = "hostlib_code_index_agent_unregister";
72pub(super) const BUILTIN_LOCK_TRY: &str = "hostlib_code_index_lock_try";
73pub(super) const BUILTIN_LOCK_RELEASE: &str = "hostlib_code_index_lock_release";
74pub(super) const BUILTIN_STATUS: &str = "hostlib_code_index_status";
75pub(super) const BUILTIN_CURRENT_AGENT_ID: &str = "hostlib_code_index_current_agent_id";
76
77pub(super) const BUILTIN_CYPHER: &str = "hostlib_code_index_cypher";
78pub(super) const BUILTIN_BRANCH_OVERLAY: &str = "hostlib_code_index_branch_overlay";
79pub(super) const BUILTIN_FRESHNESS: &str = "hostlib_code_index_freshness";
80
81// === Search / rebuild / stats ===
82
83/// Shared body for `query`. When `readonly` is supplied, hits from every
84/// read-only secondary root (issue #2403 follow-up) are merged in after the
85/// primary index so library/dependency symbols are discoverable without
86/// clobbering the project index. Primary hits keep `root: nil`; read-only
87/// hits carry the absolute path of their dependency root.
88pub(super) fn run_query_merged(
89    index: &SharedIndex,
90    readonly: Option<&super::readonly::ReadonlyRoots>,
91    args: &[VmValue],
92) -> Result<VmValue, HostlibError> {
93    let raw = dict_arg(BUILTIN_QUERY, args)?;
94    let dict = raw.as_ref();
95    let needle = require_string(BUILTIN_QUERY, dict, "needle")?;
96    if needle.is_empty() {
97        return Err(HostlibError::InvalidParameter {
98            builtin: BUILTIN_QUERY,
99            param: "needle",
100            message: "must not be empty".to_string(),
101        });
102    }
103    let case_sensitive = optional_bool(BUILTIN_QUERY, dict, "case_sensitive", false)?;
104    let max_results = optional_positive_usize(BUILTIN_QUERY, dict, "max_results")?.unwrap_or(100);
105    let scope = optional_string_list(BUILTIN_QUERY, dict, "scope")?;
106
107    let mut hits: Vec<Hit> = Vec::new();
108    {
109        let guard = index.lock().expect("code_index mutex poisoned");
110        if let Some(state) = guard.as_ref() {
111            collect_hits_scoped(state, &needle, case_sensitive, &scope, &mut hits);
112        }
113    }
114    if let Some(readonly) = readonly {
115        // Dependency roots ignore `scope` (it is a project-relative
116        // restriction) — they are an additive symbol-discovery surface.
117        if scope.is_empty() {
118            hits.extend(super::readonly::query_readonly_hits(
119                readonly,
120                &needle,
121                case_sensitive,
122            ));
123        }
124    }
125
126    hits.sort_by(|a, b| {
127        b.match_count
128            .cmp(&a.match_count)
129            .then_with(|| a.path.cmp(&b.path))
130    });
131    let truncated = hits.len() > max_results;
132    if truncated {
133        hits.truncate(max_results);
134    }
135    Ok(build_dict([
136        (
137            "results",
138            VmValue::List(Arc::new(hits.into_iter().map(hit_to_value).collect())),
139        ),
140        ("truncated", VmValue::Bool(truncated)),
141    ]))
142}
143
144/// Score `needle` against every file in `state`, honoring `scope`, and push
145/// matching files onto `hits`. Hits are tagged with the index's root only
146/// when it is a read-only secondary root (the caller passes the primary
147/// index with an empty `scope` to leave `root: nil`).
148fn collect_hits_scoped(
149    state: &IndexState,
150    needle: &str,
151    case_sensitive: bool,
152    scope: &[String],
153    hits: &mut Vec<Hit>,
154) {
155    let candidate_ids = candidates_for(state, needle);
156    for id in candidate_ids {
157        let Some(file) = state.files.get(&id) else {
158            continue;
159        };
160        if !scope_allows(scope, &file.relative_path) {
161            continue;
162        }
163        let Some(text) = read_file_text(&state.root, &file.relative_path) else {
164            continue;
165        };
166        let count = count_matches(&text, needle, case_sensitive);
167        if count == 0 {
168            continue;
169        }
170        hits.push(Hit {
171            path: file.relative_path.clone(),
172            match_count: count,
173            root: None,
174        });
175    }
176}
177
178/// Read-only entry point used by [`super::readonly::query_readonly_hits`]:
179/// score `needle` against `state` (a dependency root) with no scope filter
180/// and tag every hit with the root's absolute path.
181pub(super) fn collect_hits_into(
182    state: &IndexState,
183    needle: &str,
184    case_sensitive: bool,
185    hits: &mut Vec<Hit>,
186) {
187    let before = hits.len();
188    collect_hits_scoped(state, needle, case_sensitive, &[], hits);
189    let root = to_agent_path(&state.root);
190    for hit in &mut hits[before..] {
191        hit.root = Some(root.clone());
192    }
193}
194
195pub(super) fn run_rebuild(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
196    let raw = dict_arg(BUILTIN_REBUILD, args)?;
197    let dict = raw.as_ref();
198    let _force = optional_bool(BUILTIN_REBUILD, dict, "force", false)?;
199    let root = optional_string(BUILTIN_REBUILD, dict, "root")?
200        .map(PathBuf::from)
201        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
202    if !root.exists() {
203        return Err(HostlibError::InvalidParameter {
204            builtin: BUILTIN_REBUILD,
205            param: "root",
206            message: format!("path `{}` does not exist", root.display()),
207        });
208    }
209    if !root.is_dir() {
210        return Err(HostlibError::InvalidParameter {
211            builtin: BUILTIN_REBUILD,
212            param: "root",
213            message: format!("path `{}` is not a directory", root.display()),
214        });
215    }
216    let started = Instant::now();
217    let (state, outcome) = IndexState::build_from_root(&root);
218    let elapsed_ms = started.elapsed().as_millis() as i64;
219    {
220        let mut guard = index.lock().expect("code_index mutex poisoned");
221        *guard = Some(state);
222    }
223    Ok(build_dict([
224        ("files_indexed", VmValue::Int(outcome.files_indexed as i64)),
225        ("files_skipped", VmValue::Int(outcome.files_skipped as i64)),
226        ("elapsed_ms", VmValue::Int(elapsed_ms)),
227    ]))
228}
229
230pub(super) fn run_stats(index: &SharedIndex, _args: &[VmValue]) -> Result<VmValue, HostlibError> {
231    let guard = index.lock().expect("code_index mutex poisoned");
232    let Some(state) = guard.as_ref() else {
233        return Ok(empty_stats_response());
234    };
235    Ok(build_dict([
236        ("indexed_files", VmValue::Int(state.files.len() as i64)),
237        (
238            "trigrams",
239            VmValue::Int(state.trigrams.distinct_trigrams() as i64),
240        ),
241        ("words", VmValue::Int(state.words.distinct_words() as i64)),
242        ("memory_bytes", VmValue::Int(state.estimated_bytes() as i64)),
243        (
244            "last_rebuild_unix_ms",
245            VmValue::Int(state.last_built_unix_ms),
246        ),
247    ]))
248}
249
250pub(super) fn run_imports_for(
251    index: &SharedIndex,
252    args: &[VmValue],
253) -> Result<VmValue, HostlibError> {
254    let raw = dict_arg(BUILTIN_IMPORTS_FOR, args)?;
255    let dict = raw.as_ref();
256    let path = require_string(BUILTIN_IMPORTS_FOR, dict, "path")?;
257    let guard = index.lock().expect("code_index mutex poisoned");
258    let Some(state) = guard.as_ref() else {
259        return Ok(empty_imports_response(&path));
260    };
261    let Some(file_id) = state.lookup_path(&path) else {
262        return Ok(empty_imports_response(&path));
263    };
264    let Some(file) = state.files.get(&file_id) else {
265        return Ok(empty_imports_response(&path));
266    };
267    let kind = imports::import_kind(&file.language).to_string();
268    let base_dir = imports::parent_dir(&file.relative_path);
269    let resolved_ids: HashSet<FileId> = state.deps.imports_of(file_id).into_iter().collect();
270    let mut entries: Vec<VmValue> = Vec::with_capacity(file.imports.len());
271    for raw_import in &file.imports {
272        let resolved_path =
273            imports::resolve_module(raw_import, &file.language, &base_dir, &state.path_to_id)
274                .filter(|id| resolved_ids.contains(id))
275                .and_then(|id| state.files.get(&id).map(|f| f.relative_path.clone()));
276        entries.push(import_entry(raw_import, resolved_path.as_deref(), &kind));
277    }
278    Ok(build_dict([
279        ("path", str_value(&file.relative_path)),
280        ("imports", VmValue::List(Arc::new(entries))),
281    ]))
282}
283
284pub(super) fn run_importers_of(
285    index: &SharedIndex,
286    args: &[VmValue],
287) -> Result<VmValue, HostlibError> {
288    let raw = dict_arg(BUILTIN_IMPORTERS_OF, args)?;
289    let dict = raw.as_ref();
290    let module = require_string(BUILTIN_IMPORTERS_OF, dict, "module")?;
291    let guard = index.lock().expect("code_index mutex poisoned");
292    let Some(state) = guard.as_ref() else {
293        return Ok(empty_importers_response(&module));
294    };
295
296    let target_id = state.lookup_path(&module).or_else(|| {
297        // Fallback: suffix-match on relative paths so callers can request
298        // by basename (matching the `allowSuffixMatch` convention used by
299        // the resolver itself).
300        let needle = format!("/{module}");
301        state
302            .path_to_id
303            .iter()
304            .find(|(p, _)| p.ends_with(&needle) || *p == &module)
305            .map(|(_, id)| *id)
306    });
307
308    let mut importers: Vec<String> = match target_id {
309        Some(id) => state
310            .deps
311            .importers_of(id)
312            .into_iter()
313            .filter_map(|importer_id| {
314                state
315                    .files
316                    .get(&importer_id)
317                    .map(|f| f.relative_path.clone())
318            })
319            .collect(),
320        None => Vec::new(),
321    };
322    importers.sort();
323    Ok(build_dict([
324        ("module", str_value(&module)),
325        (
326            "importers",
327            VmValue::List(Arc::new(importers.into_iter().map(str_value).collect())),
328        ),
329    ]))
330}
331
332// === File table accessors ===
333
334pub(super) fn run_path_to_id(
335    index: &SharedIndex,
336    args: &[VmValue],
337) -> Result<VmValue, HostlibError> {
338    let raw = dict_arg(BUILTIN_PATH_TO_ID, args)?;
339    let path = require_string(BUILTIN_PATH_TO_ID, raw.as_ref(), "path")?;
340    let guard = index.lock().expect("code_index mutex poisoned");
341    let id = guard.as_ref().and_then(|s| s.lookup_path(&path));
342    Ok(match id {
343        Some(id) => VmValue::Int(id as i64),
344        None => VmValue::Nil,
345    })
346}
347
348pub(super) fn run_id_to_path(
349    index: &SharedIndex,
350    args: &[VmValue],
351) -> Result<VmValue, HostlibError> {
352    let raw = dict_arg(BUILTIN_ID_TO_PATH, args)?;
353    let id = require_positive_file_id(BUILTIN_ID_TO_PATH, raw.as_ref(), "file_id")?;
354    let guard = index.lock().expect("code_index mutex poisoned");
355    let path = guard
356        .as_ref()
357        .and_then(|s| s.files.get(&id))
358        .map(|f| f.relative_path.clone());
359    Ok(match path {
360        Some(p) => str_value(&p),
361        None => VmValue::Nil,
362    })
363}
364
365pub(super) fn run_file_ids(
366    index: &SharedIndex,
367    _args: &[VmValue],
368) -> Result<VmValue, HostlibError> {
369    let guard = index.lock().expect("code_index mutex poisoned");
370    let mut ids: Vec<FileId> = guard
371        .as_ref()
372        .map(|s| s.files.keys().copied().collect())
373        .unwrap_or_default();
374    ids.sort_unstable();
375    Ok(VmValue::List(Arc::new(
376        ids.into_iter().map(|id| VmValue::Int(id as i64)).collect(),
377    )))
378}
379
380pub(super) fn run_file_meta(
381    index: &SharedIndex,
382    args: &[VmValue],
383) -> Result<VmValue, HostlibError> {
384    let raw = dict_arg(BUILTIN_FILE_META, args)?;
385    let dict = raw.as_ref();
386    let guard = index.lock().expect("code_index mutex poisoned");
387    let Some(state) = guard.as_ref() else {
388        return Ok(VmValue::Nil);
389    };
390    let id_opt: Option<FileId> = if dict.contains_key("file_id") {
391        Some(require_positive_file_id(
392            BUILTIN_FILE_META,
393            dict,
394            "file_id",
395        )?)
396    } else if let Some(VmValue::String(p)) = dict.get("path") {
397        state.lookup_path(p)
398    } else {
399        return Err(HostlibError::MissingParameter {
400            builtin: BUILTIN_FILE_META,
401            param: "file_id|path",
402        });
403    };
404    let Some(id) = id_opt else {
405        return Ok(VmValue::Nil);
406    };
407    let Some(file) = state.files.get(&id) else {
408        return Ok(VmValue::Nil);
409    };
410    let last_edit_seq = state
411        .versions
412        .last_entry(&file.relative_path)
413        .map(|e| e.seq)
414        .unwrap_or(0);
415    Ok(build_dict([
416        ("id", VmValue::Int(file.id as i64)),
417        ("path", str_value(&file.relative_path)),
418        ("language", str_value(&file.language)),
419        ("size", VmValue::Int(file.size_bytes as i64)),
420        ("line_count", VmValue::Int(file.line_count as i64)),
421        ("hash", str_value(file.content_hash.to_string())),
422        ("mtime_ms", VmValue::Int(file.mtime_ms)),
423        ("last_edit_seq", VmValue::Int(last_edit_seq as i64)),
424    ]))
425}
426
427pub(super) fn run_file_hash(
428    index: &SharedIndex,
429    args: &[VmValue],
430) -> Result<VmValue, HostlibError> {
431    let raw = dict_arg(BUILTIN_FILE_HASH, args)?;
432    let path = require_string(BUILTIN_FILE_HASH, raw.as_ref(), "path")?;
433    let guard = index.lock().expect("code_index mutex poisoned");
434    let Some(state) = guard.as_ref() else {
435        return Ok(VmValue::Nil);
436    };
437    let Some(abs) = state.absolute_path(&path) else {
438        return Ok(VmValue::Nil);
439    };
440    let bytes = match crate::fs::read(&abs, None) {
441        Some(result) => result,
442        None => std::fs::read(&abs),
443    };
444    match bytes {
445        Ok(bytes) => Ok(str_value(fnv1a64(&bytes).to_string())),
446        Err(_) => Ok(VmValue::Nil),
447    }
448}
449
450pub(super) fn run_file_hash_snapshot(
451    index: &SharedIndex,
452    args: &[VmValue],
453) -> Result<VmValue, HostlibError> {
454    let raw = dict_arg(BUILTIN_FILE_HASH_SNAPSHOT, args)?;
455    let dict = raw.as_ref();
456    if !dict.contains_key("paths") {
457        return Err(HostlibError::MissingParameter {
458            builtin: BUILTIN_FILE_HASH_SNAPSHOT,
459            param: "paths",
460        });
461    }
462    let paths = optional_string_list(BUILTIN_FILE_HASH_SNAPSHOT, dict, "paths")?;
463    if paths.is_empty() {
464        return Err(HostlibError::InvalidParameter {
465            builtin: BUILTIN_FILE_HASH_SNAPSHOT,
466            param: "paths",
467            message: "must contain at least one path".to_string(),
468        });
469    }
470    if paths.len() > 4096 {
471        return Err(HostlibError::InvalidParameter {
472            builtin: BUILTIN_FILE_HASH_SNAPSHOT,
473            param: "paths",
474            message: "must contain at most 4096 paths".to_string(),
475        });
476    }
477
478    let guard = index.lock().expect("code_index mutex poisoned");
479    let Some(state) = guard.as_ref() else {
480        return Ok(build_dict([
481            ("seq", VmValue::Int(0)),
482            ("captured_at_ms", VmValue::Int(now_unix_ms())),
483            ("algorithm", str_value("fnv1a64")),
484            ("snapshot", VmValue::dict(harn_vm::value::DictMap::new())),
485            (
486                "missing",
487                VmValue::List(Arc::new(paths.into_iter().map(str_value).collect())),
488            ),
489            ("files", VmValue::List(Arc::new(Vec::new()))),
490        ]));
491    };
492    let seq = state.versions.current_seq as i64;
493    let captured_at_ms = now_unix_ms();
494    let mut files = Vec::with_capacity(paths.len());
495    let mut snapshot = harn_vm::value::DictMap::new();
496    let mut missing = Vec::new();
497    for path in paths {
498        let entry = file_hash_snapshot_entry(state, &path);
499        if let Some(hash) = &entry.hash {
500            snapshot.insert(harn_vm::value::intern_key(&entry.path), str_value(hash));
501        } else {
502            missing.push(str_value(&entry.path));
503        }
504        files.push(entry.value);
505    }
506    Ok(build_dict([
507        ("seq", VmValue::Int(seq)),
508        ("captured_at_ms", VmValue::Int(captured_at_ms)),
509        ("algorithm", str_value("fnv1a64")),
510        ("snapshot", VmValue::dict(snapshot)),
511        ("missing", VmValue::List(Arc::new(missing))),
512        ("files", VmValue::List(Arc::new(files))),
513    ]))
514}
515
516// === Cached reads ===
517
518/// Shared body for `read_range`. When `readonly` is supplied, a path that
519/// is not inside the primary workspace root is resolved against the
520/// read-only secondary roots (issue #2403 follow-up) so a symbol
521/// discovered in a dependency root can be read back. Resolution stays
522/// confined to a known indexed root in every case — arbitrary host paths
523/// are still rejected.
524pub(super) fn run_read_range_merged(
525    index: &SharedIndex,
526    readonly: Option<&super::readonly::ReadonlyRoots>,
527    args: &[VmValue],
528) -> Result<VmValue, HostlibError> {
529    let raw = dict_arg(BUILTIN_READ_RANGE, args)?;
530    let dict = raw.as_ref();
531    let path = require_string(BUILTIN_READ_RANGE, dict, "path")?;
532    let start = optional_positive_i64(BUILTIN_READ_RANGE, dict, "start")?;
533    let end = optional_positive_i64(BUILTIN_READ_RANGE, dict, "end")?;
534    let abs =
535        match readonly {
536            Some(readonly) => super::readonly::resolve_read_path(index, readonly, &path)
537                .ok_or_else(|| HostlibError::InvalidParameter {
538                    builtin: BUILTIN_READ_RANGE,
539                    param: "path",
540                    message: "path must stay within the indexed workspace root or a read-only \
541                          dependency root"
542                        .to_string(),
543                })?,
544            None => {
545                let guard = index.lock().expect("code_index mutex poisoned");
546                match guard.as_ref() {
547                    Some(state) => state.absolute_path(&path).ok_or_else(|| {
548                        HostlibError::InvalidParameter {
549                            builtin: BUILTIN_READ_RANGE,
550                            param: "path",
551                            message: "path must stay within the indexed workspace root".to_string(),
552                        }
553                    })?,
554                    None => PathBuf::from(&path),
555                }
556            }
557        };
558
559    let content_result = match crate::fs::read_to_string(&abs, None) {
560        Some(result) => result,
561        None => std::fs::read_to_string(&abs),
562    };
563    let content = match content_result {
564        Ok(s) => s,
565        Err(_) => {
566            return Err(HostlibError::Backend {
567                builtin: BUILTIN_READ_RANGE,
568                message: format!("file not found: {path}"),
569            })
570        }
571    };
572
573    if start.is_none() && end.is_none() {
574        return Ok(build_dict([("content", str_value(&content))]));
575    }
576    let lines: Vec<&str> = content.split('\n').collect();
577    let total = lines.len() as i64;
578    let lo = (start.unwrap_or(1) - 1).max(0) as usize;
579    let hi = end.unwrap_or(total).min(total).max(0) as usize;
580    if lo >= hi {
581        return Ok(build_dict([
582            ("content", str_value("")),
583            ("start", VmValue::Int((lo as i64) + 1)),
584            ("end", VmValue::Int(hi as i64)),
585        ]));
586    }
587    let slice = lines[lo..hi].join("\n");
588    Ok(build_dict([
589        ("content", str_value(&slice)),
590        ("start", VmValue::Int((lo as i64) + 1)),
591        ("end", VmValue::Int(hi as i64)),
592    ]))
593}
594
595pub(super) fn run_reindex_file(
596    index: &SharedIndex,
597    args: &[VmValue],
598) -> Result<VmValue, HostlibError> {
599    let raw = dict_arg(BUILTIN_REINDEX_FILE, args)?;
600    let path = require_string(BUILTIN_REINDEX_FILE, raw.as_ref(), "path")?;
601    let mut guard = index.lock().expect("code_index mutex poisoned");
602    let Some(state) = guard.as_mut() else {
603        return Ok(build_dict([
604            ("indexed", VmValue::Bool(false)),
605            ("file_id", VmValue::Nil),
606        ]));
607    };
608    let Some(abs) = state.absolute_path(&path) else {
609        return Err(HostlibError::InvalidParameter {
610            builtin: BUILTIN_REINDEX_FILE,
611            param: "path",
612            message: "path must stay within the indexed workspace root".to_string(),
613        });
614    };
615    let id = state.reindex_file(&abs);
616    Ok(build_dict([
617        ("indexed", VmValue::Bool(id.is_some())),
618        (
619            "file_id",
620            id.map(|i| VmValue::Int(i as i64)).unwrap_or(VmValue::Nil),
621        ),
622    ]))
623}
624
625pub(super) fn run_trigram_query(
626    index: &SharedIndex,
627    args: &[VmValue],
628) -> Result<VmValue, HostlibError> {
629    let raw = dict_arg(BUILTIN_TRIGRAM_QUERY, args)?;
630    let dict = raw.as_ref();
631    let trigrams_raw = optional_int_list(BUILTIN_TRIGRAM_QUERY, dict, "trigrams")?;
632    let max_files = optional_positive_usize(BUILTIN_TRIGRAM_QUERY, dict, "max_files")?;
633    let mut trigrams = Vec::with_capacity(trigrams_raw.len());
634    for n in trigrams_raw {
635        if n < 0 {
636            return Err(HostlibError::InvalidParameter {
637                builtin: BUILTIN_TRIGRAM_QUERY,
638                param: "trigrams",
639                message: "entries must be >= 0".to_string(),
640            });
641        }
642        trigrams.push(n as u32);
643    }
644    let guard = index.lock().expect("code_index mutex poisoned");
645    let mut ids: Vec<FileId> = match guard.as_ref() {
646        Some(state) => state.trigrams.query(&trigrams).into_iter().collect(),
647        None => Vec::new(),
648    };
649    ids.sort_unstable();
650    if let Some(limit) = max_files {
651        ids.truncate(limit);
652    }
653    Ok(VmValue::List(Arc::new(
654        ids.into_iter().map(|id| VmValue::Int(id as i64)).collect(),
655    )))
656}
657
658pub(super) fn run_extract_trigrams(
659    _index: &SharedIndex,
660    args: &[VmValue],
661) -> Result<VmValue, HostlibError> {
662    let raw = dict_arg(BUILTIN_EXTRACT_TRIGRAMS, args)?;
663    let query = require_string(BUILTIN_EXTRACT_TRIGRAMS, raw.as_ref(), "query")?;
664    let mut tgs = trigram::query_trigrams(&query);
665    tgs.sort_unstable();
666    Ok(VmValue::List(Arc::new(
667        tgs.into_iter().map(|n| VmValue::Int(n as i64)).collect(),
668    )))
669}
670
671pub(super) fn run_word_get(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
672    let raw = dict_arg(BUILTIN_WORD_GET, args)?;
673    let word = require_string(BUILTIN_WORD_GET, raw.as_ref(), "word")?;
674    let guard = index.lock().expect("code_index mutex poisoned");
675    let hits: Vec<VmValue> = match guard.as_ref() {
676        Some(state) => state
677            .words
678            .get(&word)
679            .iter()
680            .map(|h| {
681                build_dict([
682                    ("file_id", VmValue::Int(h.file as i64)),
683                    ("line", VmValue::Int(h.line as i64)),
684                ])
685            })
686            .collect(),
687        None => Vec::new(),
688    };
689    Ok(VmValue::List(Arc::new(hits)))
690}
691
692pub(super) fn run_deps_get(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
693    let raw = dict_arg(BUILTIN_DEPS_GET, args)?;
694    let dict = raw.as_ref();
695    let id = require_positive_file_id(BUILTIN_DEPS_GET, dict, "file_id")?;
696    let direction = optional_string(BUILTIN_DEPS_GET, dict, "direction")?
697        .unwrap_or_else(|| "importers".to_string());
698    let guard = index.lock().expect("code_index mutex poisoned");
699    let mut neighbors: Vec<FileId> = match guard.as_ref() {
700        Some(state) => match direction.as_str() {
701            "importers" => state.deps.importers_of(id),
702            "imports" => state.deps.imports_of(id),
703            _ => {
704                return Err(HostlibError::InvalidParameter {
705                    builtin: BUILTIN_DEPS_GET,
706                    param: "direction",
707                    message: format!("expected \"importers\" or \"imports\", got {direction:?}"),
708                })
709            }
710        },
711        None => Vec::new(),
712    };
713    neighbors.sort_unstable();
714    Ok(VmValue::List(Arc::new(
715        neighbors
716            .into_iter()
717            .map(|id| VmValue::Int(id as i64))
718            .collect(),
719    )))
720}
721
722pub(super) fn run_outline_get(
723    index: &SharedIndex,
724    args: &[VmValue],
725) -> Result<VmValue, HostlibError> {
726    let raw = dict_arg(BUILTIN_OUTLINE_GET, args)?;
727    let id = require_positive_file_id(BUILTIN_OUTLINE_GET, raw.as_ref(), "file_id")?;
728    let guard = index.lock().expect("code_index mutex poisoned");
729    let symbols: Vec<VmValue> = match guard.as_ref().and_then(|s| s.files.get(&id)) {
730        Some(file) => file
731            .symbols
732            .iter()
733            .map(|sym| {
734                build_dict([
735                    ("name", str_value(&sym.name)),
736                    ("kind", str_value(&sym.kind)),
737                    (
738                        "access_level",
739                        sym.access_level
740                            .as_deref()
741                            .map(str_value)
742                            .unwrap_or(VmValue::Nil),
743                    ),
744                    ("start_line", VmValue::Int(sym.start_line as i64)),
745                    ("end_line", VmValue::Int(sym.end_line as i64)),
746                    ("signature", str_value(&sym.signature)),
747                ])
748            })
749            .collect(),
750        None => Vec::new(),
751    };
752    Ok(VmValue::List(Arc::new(symbols)))
753}
754
755// === Change log ===
756
757pub(super) fn run_current_seq(
758    index: &SharedIndex,
759    _args: &[VmValue],
760) -> Result<VmValue, HostlibError> {
761    let guard = index.lock().expect("code_index mutex poisoned");
762    let seq = guard.as_ref().map(|s| s.versions.current_seq).unwrap_or(0);
763    Ok(VmValue::Int(seq as i64))
764}
765
766pub(super) fn run_changes_since(
767    index: &SharedIndex,
768    args: &[VmValue],
769) -> Result<VmValue, HostlibError> {
770    let raw = dict_arg(BUILTIN_CHANGES_SINCE, args)?;
771    let dict = raw.as_ref();
772    let seq = optional_non_negative_u64(BUILTIN_CHANGES_SINCE, dict, "seq", 0)?;
773    let limit = optional_positive_usize(BUILTIN_CHANGES_SINCE, dict, "limit")?;
774    let guard = index.lock().expect("code_index mutex poisoned");
775    let records = match guard.as_ref() {
776        Some(state) => state.versions.changes_since(seq, limit),
777        None => Vec::new(),
778    };
779    Ok(VmValue::List(Arc::new(
780        records
781            .into_iter()
782            .map(|r| {
783                build_dict([
784                    ("path", str_value(&r.path)),
785                    ("seq", VmValue::Int(r.seq as i64)),
786                    ("agent_id", VmValue::Int(r.agent_id as i64)),
787                    ("op", str_value(r.op.as_str())),
788                    ("hash", str_value(r.hash.to_string())),
789                    ("size", VmValue::Int(r.size as i64)),
790                    ("timestamp_ms", VmValue::Int(r.timestamp_ms)),
791                ])
792            })
793            .collect(),
794    )))
795}
796
797pub(super) fn run_version_record(
798    index: &SharedIndex,
799    args: &[VmValue],
800) -> Result<VmValue, HostlibError> {
801    let raw = dict_arg(BUILTIN_VERSION_RECORD, args)?;
802    let dict = raw.as_ref();
803    let agent_id = require_non_negative_u64(BUILTIN_VERSION_RECORD, dict, "agent_id")?;
804    let path = require_string(BUILTIN_VERSION_RECORD, dict, "path")?;
805    let op_str =
806        optional_string(BUILTIN_VERSION_RECORD, dict, "op")?.unwrap_or_else(|| "write".to_string());
807    let op = EditOp::parse(&op_str).unwrap_or(EditOp::Write);
808    let hash = parse_hash(BUILTIN_VERSION_RECORD, dict, "hash")?;
809    let size = optional_non_negative_u64(BUILTIN_VERSION_RECORD, dict, "size", 0)?;
810    let now = now_unix_ms();
811    let mut guard = index.lock().expect("code_index mutex poisoned");
812    let state = ensure_state(BUILTIN_VERSION_RECORD, &mut guard)?;
813    let normalized = normalize_relative_path(state, &path);
814    let seq = state
815        .versions
816        .record(normalized, agent_id, op, hash, size, now);
817    state.agents.note_edit(agent_id, now);
818    Ok(VmValue::Int(seq as i64))
819}
820
821// === Agent registry + locks ===
822
823pub(super) fn run_agent_register(
824    index: &SharedIndex,
825    args: &[VmValue],
826) -> Result<VmValue, HostlibError> {
827    let raw = dict_arg(BUILTIN_AGENT_REGISTER, args)?;
828    let dict = raw.as_ref();
829    let name = optional_string(BUILTIN_AGENT_REGISTER, dict, "name")?
830        .unwrap_or_else(|| "agent".to_string());
831    let requested_id = optional_positive_u64(BUILTIN_AGENT_REGISTER, dict, "agent_id")?;
832    let now = now_unix_ms();
833    let mut guard = index.lock().expect("code_index mutex poisoned");
834    let state = ensure_state(BUILTIN_AGENT_REGISTER, &mut guard)?;
835    let id = match requested_id {
836        Some(id) => state.agents.register_with_id(id, name, now),
837        None => state.agents.register(name, now),
838    };
839    Ok(VmValue::Int(id as i64))
840}
841
842pub(super) fn run_agent_heartbeat(
843    index: &SharedIndex,
844    args: &[VmValue],
845) -> Result<VmValue, HostlibError> {
846    let raw = dict_arg(BUILTIN_AGENT_HEARTBEAT, args)?;
847    let id = require_positive_u64(BUILTIN_AGENT_HEARTBEAT, raw.as_ref(), "agent_id")?;
848    let now = now_unix_ms();
849    let mut guard = index.lock().expect("code_index mutex poisoned");
850    let state = ensure_state(BUILTIN_AGENT_HEARTBEAT, &mut guard)?;
851    state.agents.heartbeat(id, now);
852    Ok(VmValue::Bool(true))
853}
854
855pub(super) fn run_agent_unregister(
856    index: &SharedIndex,
857    args: &[VmValue],
858) -> Result<VmValue, HostlibError> {
859    let raw = dict_arg(BUILTIN_AGENT_UNREGISTER, args)?;
860    let id = require_positive_u64(BUILTIN_AGENT_UNREGISTER, raw.as_ref(), "agent_id")?;
861    let mut guard = index.lock().expect("code_index mutex poisoned");
862    let state = ensure_state(BUILTIN_AGENT_UNREGISTER, &mut guard)?;
863    state.agents.unregister(id);
864    Ok(VmValue::Bool(true))
865}
866
867pub(super) fn run_lock_try(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
868    let raw = dict_arg(BUILTIN_LOCK_TRY, args)?;
869    let dict = raw.as_ref();
870    let agent_id = require_positive_u64(BUILTIN_LOCK_TRY, dict, "agent_id")?;
871    let path = require_string(BUILTIN_LOCK_TRY, dict, "path")?;
872    let ttl = optional_positive_i64(BUILTIN_LOCK_TRY, dict, "ttl_ms")?;
873    let now = now_unix_ms();
874    let mut guard = index.lock().expect("code_index mutex poisoned");
875    let state = ensure_state(BUILTIN_LOCK_TRY, &mut guard)?;
876    let granted = state.agents.try_lock(agent_id, &path, ttl, now);
877    if granted {
878        return Ok(build_dict([
879            ("locked", VmValue::Bool(true)),
880            ("holder", VmValue::Int(agent_id as i64)),
881        ]));
882    }
883    let holder = state.agents.lock_holder(&path, now);
884    Ok(build_dict([
885        ("locked", VmValue::Bool(false)),
886        (
887            "holder",
888            holder
889                .map(|id| VmValue::Int(id as i64))
890                .unwrap_or(VmValue::Nil),
891        ),
892    ]))
893}
894
895pub(super) fn run_lock_release(
896    index: &SharedIndex,
897    args: &[VmValue],
898) -> Result<VmValue, HostlibError> {
899    let raw = dict_arg(BUILTIN_LOCK_RELEASE, args)?;
900    let dict = raw.as_ref();
901    let agent_id = require_positive_u64(BUILTIN_LOCK_RELEASE, dict, "agent_id")?;
902    let path = require_string(BUILTIN_LOCK_RELEASE, dict, "path")?;
903    let mut guard = index.lock().expect("code_index mutex poisoned");
904    let state = ensure_state(BUILTIN_LOCK_RELEASE, &mut guard)?;
905    state.agents.release_lock(agent_id, &path);
906    Ok(VmValue::Bool(true))
907}
908
909pub(super) fn run_status(index: &SharedIndex, _args: &[VmValue]) -> Result<VmValue, HostlibError> {
910    let guard = index.lock().expect("code_index mutex poisoned");
911    match guard.as_ref() {
912        Some(state) => Ok(build_dict([
913            ("file_count", VmValue::Int(state.files.len() as i64)),
914            (
915                "current_seq",
916                VmValue::Int(state.versions.current_seq as i64),
917            ),
918            ("last_indexed_at_ms", VmValue::Int(state.last_built_unix_ms)),
919            (
920                "git_head",
921                state
922                    .git_head
923                    .as_deref()
924                    .map(str_value)
925                    .unwrap_or(VmValue::Nil),
926            ),
927            (
928                "agents",
929                VmValue::List(Arc::new(
930                    state
931                        .agents
932                        .agents()
933                        .map(|info| {
934                            build_dict([
935                                ("id", VmValue::Int(info.id as i64)),
936                                ("name", str_value(&info.name)),
937                                (
938                                    "state",
939                                    str_value(match info.state {
940                                        super::agents::AgentState::Active => "active",
941                                        super::agents::AgentState::Crashed => "crashed",
942                                        super::agents::AgentState::Gone => "gone",
943                                    }),
944                                ),
945                                ("last_seen_ms", VmValue::Int(info.last_seen_ms)),
946                                ("edit_count", VmValue::Int(info.edit_count as i64)),
947                                ("lock_count", VmValue::Int(info.locked_paths.len() as i64)),
948                            ])
949                        })
950                        .collect(),
951                )),
952            ),
953        ])),
954        None => Ok(build_dict([
955            ("file_count", VmValue::Int(0)),
956            ("current_seq", VmValue::Int(0)),
957            ("last_indexed_at_ms", VmValue::Int(0)),
958            ("git_head", VmValue::Nil),
959            ("agents", VmValue::List(Arc::new(Vec::new()))),
960        ])),
961    }
962}
963
964pub(super) fn run_current_agent_id(
965    slot: &Arc<Mutex<Option<AgentId>>>,
966    _args: &[VmValue],
967) -> Result<VmValue, HostlibError> {
968    let guard = slot.lock().expect("current_agent slot poisoned");
969    Ok(match *guard {
970        Some(id) => VmValue::Int(id as i64),
971        None => VmValue::Nil,
972    })
973}
974
975// === Symbol graph: cypher, branch_overlay, freshness (issue #2434) ===
976
977pub(super) fn run_cypher(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
978    let raw = dict_arg(BUILTIN_CYPHER, args)?;
979    let dict = raw.as_ref();
980    let query = require_string(BUILTIN_CYPHER, dict, "query")?;
981
982    let guard = index.lock().expect("code_index mutex poisoned");
983    let Some(state) = guard.as_ref() else {
984        return Ok(build_dict([
985            ("rows", VmValue::List(Arc::new(Vec::new()))),
986            ("overlay", VmValue::Nil),
987        ]));
988    };
989
990    let graph = state.overlays.graph(&state.symbols);
991    let rows = super::cypher::execute(&query, graph).map_err(|err| HostlibError::Backend {
992        builtin: BUILTIN_CYPHER,
993        message: err.to_string(),
994    })?;
995
996    let rows_vm: Vec<VmValue> = rows
997        .into_iter()
998        .map(|row| {
999            let mut map: harn_vm::value::DictMap = harn_vm::value::DictMap::new();
1000            for (k, v) in row {
1001                map.insert(harn_vm::value::intern_key(&k), v.to_vm());
1002            }
1003            VmValue::dict(map)
1004        })
1005        .collect();
1006
1007    Ok(build_dict([
1008        ("rows", VmValue::List(Arc::new(rows_vm))),
1009        (
1010            "overlay",
1011            match state.overlays.active() {
1012                Some(name) => str_value(name),
1013                None => VmValue::Nil,
1014            },
1015        ),
1016    ]))
1017}
1018
1019pub(super) fn run_branch_overlay(
1020    index: &SharedIndex,
1021    args: &[VmValue],
1022) -> Result<VmValue, HostlibError> {
1023    let raw = dict_arg(BUILTIN_BRANCH_OVERLAY, args)?;
1024    let dict = raw.as_ref();
1025    let branch = optional_string(BUILTIN_BRANCH_OVERLAY, dict, "branch")?;
1026    let activate = optional_bool(BUILTIN_BRANCH_OVERLAY, dict, "activate", true)?;
1027    let action = optional_string(BUILTIN_BRANCH_OVERLAY, dict, "action")?;
1028
1029    let mut guard = index.lock().expect("code_index mutex poisoned");
1030    let state = ensure_state(BUILTIN_BRANCH_OVERLAY, &mut guard)?;
1031
1032    let mut reuse: f64 = 1.0;
1033    match action.as_deref().unwrap_or("activate") {
1034        "deactivate" => {
1035            state.overlays.activate(None);
1036        }
1037        "create" => {
1038            let branch_name = branch.ok_or(HostlibError::MissingParameter {
1039                builtin: BUILTIN_BRANCH_OVERLAY,
1040                param: "branch",
1041            })?;
1042            let mut overlay = super::overlay::BranchOverlay::new(&branch_name);
1043            overlay.materialize(&state.symbols);
1044            state.overlays.set(overlay);
1045            if activate {
1046                state.overlays.activate(Some(branch_name));
1047            }
1048            reuse = state.overlays.reuse_fraction(&state.symbols);
1049        }
1050        "activate" => {
1051            let branch_name = branch.ok_or(HostlibError::MissingParameter {
1052                builtin: BUILTIN_BRANCH_OVERLAY,
1053                param: "branch",
1054            })?;
1055            // If the overlay doesn't exist, create an empty pass-through
1056            // one — the base graph then serves it untouched, giving the
1057            // 100% reuse / 0-change baseline.
1058            if state.overlays.get(&branch_name).is_none() {
1059                let mut overlay = super::overlay::BranchOverlay::new(&branch_name);
1060                overlay.materialize(&state.symbols);
1061                state.overlays.set(overlay);
1062            }
1063            state.overlays.activate(Some(branch_name));
1064            reuse = state.overlays.reuse_fraction(&state.symbols);
1065        }
1066        other => {
1067            return Err(HostlibError::InvalidParameter {
1068                builtin: BUILTIN_BRANCH_OVERLAY,
1069                param: "action",
1070                message: format!("expected one of activate|deactivate|create, got `{other}`"),
1071            })
1072        }
1073    }
1074
1075    Ok(build_dict([
1076        (
1077            "active",
1078            match state.overlays.active() {
1079                Some(name) => str_value(name),
1080                None => VmValue::Nil,
1081            },
1082        ),
1083        ("reuse_fraction", VmValue::Float(reuse)),
1084    ]))
1085}
1086
1087pub(super) fn run_freshness(
1088    index: &SharedIndex,
1089    args: &[VmValue],
1090) -> Result<VmValue, HostlibError> {
1091    let raw = dict_arg(BUILTIN_FRESHNESS, args)?;
1092    let dict = raw.as_ref();
1093    let path = require_string(BUILTIN_FRESHNESS, dict, "path")?;
1094
1095    let guard = index.lock().expect("code_index mutex poisoned");
1096    let state = guard.as_ref().ok_or_else(|| HostlibError::Backend {
1097        builtin: BUILTIN_FRESHNESS,
1098        message: "code index has not been initialised — call \
1099            `hostlib_code_index_rebuild` first"
1100            .to_string(),
1101    })?;
1102
1103    let normalized = normalize_relative_path(state, &path);
1104    let file = state
1105        .lookup_path(&normalized)
1106        .and_then(|id| state.files.get(&id));
1107    let Some(file) = file else {
1108        return Ok(unknown_freshness_response(&path));
1109    };
1110
1111    let abs = state.root.join(&file.relative_path);
1112    let (disk_mtime, disk_hash) = match std::fs::read(&abs) {
1113        Ok(bytes) => {
1114            let hash = fnv1a64(&bytes);
1115            let mtime = std::fs::metadata(&abs)
1116                .ok()
1117                .and_then(|m| m.modified().ok())
1118                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
1119                .map(|d| d.as_millis() as i64)
1120                .unwrap_or(0);
1121            (mtime, Some(hash))
1122        }
1123        Err(_) => (0, None),
1124    };
1125    let stale = disk_hash != Some(file.content_hash);
1126    Ok(build_dict([
1127        ("path", str_value(&file.relative_path)),
1128        ("known", VmValue::Bool(true)),
1129        ("stale", VmValue::Bool(stale)),
1130        (
1131            "indexed_hash",
1132            VmValue::String(arcstr::ArcStr::from(
1133                format!("{:016x}", file.content_hash).as_str(),
1134            )),
1135        ),
1136        ("indexed_mtime_ms", VmValue::Int(file.mtime_ms)),
1137        (
1138            "disk_hash",
1139            match disk_hash {
1140                Some(h) => VmValue::String(arcstr::ArcStr::from(format!("{h:016x}").as_str())),
1141                None => VmValue::Nil,
1142            },
1143        ),
1144        ("disk_mtime_ms", VmValue::Int(disk_mtime)),
1145    ]))
1146}
1147
1148// === Helpers ===
1149
1150struct FileHashSnapshotEntry {
1151    value: VmValue,
1152    path: String,
1153    hash: Option<String>,
1154}
1155
1156fn file_hash_snapshot_entry(state: &IndexState, path: &str) -> FileHashSnapshotEntry {
1157    let normalized = normalize_relative_path(state, path);
1158    let indexed_file = state
1159        .lookup_path(&normalized)
1160        .and_then(|id| state.files.get(&id));
1161    let abs = state
1162        .absolute_path(path)
1163        .or_else(|| state.absolute_path(&normalized));
1164    let (readable, hash, hash_source, disk_size, disk_mtime_ms) = match abs {
1165        Some(abs) => {
1166            let metadata = std::fs::metadata(&abs).ok();
1167            let mtime_ms = metadata
1168                .as_ref()
1169                .and_then(|m| m.modified().ok())
1170                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
1171                .map(|d| d.as_millis() as i64);
1172            if let (Some(file), Some(metadata), Some(mtime_ms)) =
1173                (indexed_file, metadata.as_ref(), mtime_ms)
1174            {
1175                if metadata.len() == file.size_bytes && mtime_ms == file.mtime_ms {
1176                    return file_hash_snapshot_value(
1177                        state,
1178                        normalized,
1179                        indexed_file,
1180                        true,
1181                        Some(file.content_hash.to_string()),
1182                        "indexed",
1183                        VmValue::Int(file.size_bytes as i64),
1184                        VmValue::Int(file.mtime_ms),
1185                    );
1186                }
1187            }
1188            let bytes = match crate::fs::read(&abs, None) {
1189                Some(result) => result,
1190                None => std::fs::read(&abs),
1191            };
1192            match bytes {
1193                Ok(bytes) => {
1194                    let hash = fnv1a64(&bytes).to_string();
1195                    (
1196                        true,
1197                        Some(hash),
1198                        "disk",
1199                        VmValue::Int(bytes.len() as i64),
1200                        mtime_ms.map(VmValue::Int).unwrap_or(VmValue::Nil),
1201                    )
1202                }
1203                Err(_) => (false, None, "missing", VmValue::Nil, VmValue::Nil),
1204            }
1205        }
1206        None => (false, None, "missing", VmValue::Nil, VmValue::Nil),
1207    };
1208    file_hash_snapshot_value(
1209        state,
1210        normalized,
1211        indexed_file,
1212        readable,
1213        hash,
1214        hash_source,
1215        disk_size,
1216        disk_mtime_ms,
1217    )
1218}
1219
1220fn file_hash_snapshot_value(
1221    state: &IndexState,
1222    normalized: String,
1223    indexed_file: Option<&super::file_table::IndexedFile>,
1224    readable: bool,
1225    hash: Option<String>,
1226    hash_source: &str,
1227    disk_size: VmValue,
1228    disk_mtime_ms: VmValue,
1229) -> FileHashSnapshotEntry {
1230    let indexed_hash = indexed_file
1231        .map(|file| str_value(file.content_hash.to_string()))
1232        .unwrap_or(VmValue::Nil);
1233    let indexed_mtime_ms = indexed_file
1234        .map(|file| VmValue::Int(file.mtime_ms))
1235        .unwrap_or(VmValue::Nil);
1236    let last_edit_seq = state
1237        .versions
1238        .last_entry(&normalized)
1239        .map(|entry| entry.seq as i64)
1240        .unwrap_or(0);
1241    let hash_value = hash.as_ref().map(str_value).unwrap_or(VmValue::Nil);
1242    let value = build_dict([
1243        ("path", str_value(&normalized)),
1244        ("known", VmValue::Bool(indexed_file.is_some())),
1245        ("readable", VmValue::Bool(readable)),
1246        ("hash", hash_value),
1247        ("hash_source", str_value(hash_source)),
1248        ("size", disk_size),
1249        ("mtime_ms", disk_mtime_ms),
1250        ("indexed_hash", indexed_hash),
1251        ("indexed_mtime_ms", indexed_mtime_ms),
1252        ("last_edit_seq", VmValue::Int(last_edit_seq)),
1253    ]);
1254    FileHashSnapshotEntry {
1255        value,
1256        path: normalized,
1257        hash,
1258    }
1259}
1260
1261fn ensure_state<'a>(
1262    builtin: &'static str,
1263    guard: &'a mut std::sync::MutexGuard<'_, Option<IndexState>>,
1264) -> Result<&'a mut IndexState, HostlibError> {
1265    if guard.is_none() {
1266        return Err(HostlibError::Backend {
1267            builtin,
1268            message: "code index has not been initialised — call \
1269                 `hostlib_code_index_rebuild` or restore from a snapshot first"
1270                .to_string(),
1271        });
1272    }
1273    Ok(guard.as_mut().unwrap())
1274}
1275
1276fn parse_hash(
1277    builtin: &'static str,
1278    dict: &harn_vm::value::DictMap,
1279    key: &'static str,
1280) -> Result<u64, HostlibError> {
1281    match dict.get(key) {
1282        None | Some(VmValue::Nil) => Ok(0),
1283        Some(VmValue::Int(n)) if *n >= 0 => Ok(*n as u64),
1284        Some(VmValue::Int(n)) => Err(HostlibError::InvalidParameter {
1285            builtin,
1286            param: key,
1287            message: format!("must be >= 0, got {n}"),
1288        }),
1289        Some(VmValue::String(s)) => s
1290            .parse::<u64>()
1291            .map_err(|_| HostlibError::InvalidParameter {
1292                builtin,
1293                param: key,
1294                message: format!("expected u64-parseable string, got {s:?}"),
1295            }),
1296        Some(other) => Err(HostlibError::InvalidParameter {
1297            builtin,
1298            param: key,
1299            message: format!(
1300                "expected integer or numeric string, got {}",
1301                other.type_name()
1302            ),
1303        }),
1304    }
1305}
1306
1307fn require_positive_u64(
1308    builtin: &'static str,
1309    dict: &harn_vm::value::DictMap,
1310    key: &'static str,
1311) -> Result<u64, HostlibError> {
1312    let raw = require_non_negative_u64(builtin, dict, key)?;
1313    if raw == 0 {
1314        return Err(HostlibError::InvalidParameter {
1315            builtin,
1316            param: key,
1317            message: "must be >= 1".to_string(),
1318        });
1319    }
1320    Ok(raw)
1321}
1322
1323fn require_positive_file_id(
1324    builtin: &'static str,
1325    dict: &harn_vm::value::DictMap,
1326    key: &'static str,
1327) -> Result<FileId, HostlibError> {
1328    let raw = require_positive_u64(builtin, dict, key)?;
1329    FileId::try_from(raw).map_err(|_| HostlibError::InvalidParameter {
1330        builtin,
1331        param: key,
1332        message: "does not fit in file id".to_string(),
1333    })
1334}
1335
1336fn require_non_negative_u64(
1337    builtin: &'static str,
1338    dict: &harn_vm::value::DictMap,
1339    key: &'static str,
1340) -> Result<u64, HostlibError> {
1341    match value_args::optional_i64_no_default(builtin, dict, key)? {
1342        Some(value) if value >= 0 => Ok(value as u64),
1343        Some(value) => Err(HostlibError::InvalidParameter {
1344            builtin,
1345            param: key,
1346            message: format!("must be >= 0, got {value}"),
1347        }),
1348        None => Err(HostlibError::MissingParameter {
1349            builtin,
1350            param: key,
1351        }),
1352    }
1353}
1354
1355fn optional_positive_u64(
1356    builtin: &'static str,
1357    dict: &harn_vm::value::DictMap,
1358    key: &'static str,
1359) -> Result<Option<u64>, HostlibError> {
1360    match dict.get(key) {
1361        None | Some(VmValue::Nil) => Ok(None),
1362        Some(_) => require_positive_u64(builtin, dict, key).map(Some),
1363    }
1364}
1365
1366fn optional_non_negative_u64(
1367    builtin: &'static str,
1368    dict: &harn_vm::value::DictMap,
1369    key: &'static str,
1370    default: u64,
1371) -> Result<u64, HostlibError> {
1372    match dict.get(key) {
1373        None | Some(VmValue::Nil) => Ok(default),
1374        Some(_) => require_non_negative_u64(builtin, dict, key),
1375    }
1376}
1377
1378fn optional_positive_i64(
1379    builtin: &'static str,
1380    dict: &harn_vm::value::DictMap,
1381    key: &'static str,
1382) -> Result<Option<i64>, HostlibError> {
1383    match value_args::optional_i64_no_default(builtin, dict, key)? {
1384        None => Ok(None),
1385        Some(value) if value >= 1 => Ok(Some(value)),
1386        Some(value) => Err(HostlibError::InvalidParameter {
1387            builtin,
1388            param: key,
1389            message: format!("must be >= 1, got {value}"),
1390        }),
1391    }
1392}
1393
1394fn optional_positive_usize(
1395    builtin: &'static str,
1396    dict: &harn_vm::value::DictMap,
1397    key: &'static str,
1398) -> Result<Option<usize>, HostlibError> {
1399    match optional_positive_u64(builtin, dict, key)? {
1400        Some(value) => {
1401            usize::try_from(value)
1402                .map(Some)
1403                .map_err(|_| HostlibError::InvalidParameter {
1404                    builtin,
1405                    param: key,
1406                    message: "does not fit in usize".to_string(),
1407                })
1408        }
1409        None => Ok(None),
1410    }
1411}
1412
1413/// Re-export of [`normalize_relative_path`] for sibling modules
1414/// (e.g. [`super::rename`]). Inputs may be a workspace-relative path,
1415/// an absolute path inside the workspace, or an unknown path; the
1416/// returned string is always workspace-relative when resolvable and
1417/// falls back to the raw input otherwise.
1418pub(super) fn normalize_relative_path_for(state: &IndexState, path: &str) -> String {
1419    normalize_relative_path(state, path)
1420}
1421
1422fn normalize_relative_path(state: &IndexState, path: &str) -> String {
1423    if let Some(rel) = state
1424        .lookup_path(path)
1425        .and_then(|id| state.files.get(&id))
1426        .map(|f| f.relative_path.clone())
1427    {
1428        return rel;
1429    }
1430    let p = std::path::Path::new(path);
1431    if p.is_absolute() {
1432        if let Ok(rel) = p.strip_prefix(&state.root) {
1433            return to_agent_path(rel);
1434        }
1435    }
1436    to_agent_path_str(path)
1437}
1438
1439fn candidates_for(state: &IndexState, needle: &str) -> Vec<FileId> {
1440    if needle.len() >= 3 {
1441        let trigrams = trigram::query_trigrams(needle);
1442        return state.trigrams.query(&trigrams).into_iter().collect();
1443    }
1444    state.files.keys().copied().collect()
1445}
1446
1447fn read_file_text(root: &std::path::Path, relative: &str) -> Option<String> {
1448    let path = root.join(relative);
1449    match crate::fs::read_to_string(&path, None) {
1450        Some(result) => result.ok(),
1451        None => std::fs::read_to_string(path).ok(),
1452    }
1453}
1454
1455fn count_matches(haystack: &str, needle: &str, case_sensitive: bool) -> u64 {
1456    if case_sensitive {
1457        haystack.matches(needle).count() as u64
1458    } else {
1459        let lower_h = haystack.to_lowercase();
1460        let lower_n = needle.to_lowercase();
1461        lower_h.matches(&lower_n).count() as u64
1462    }
1463}
1464
1465fn scope_allows(scope: &[String], relative: &str) -> bool {
1466    if scope.is_empty() {
1467        return true;
1468    }
1469    scope
1470        .iter()
1471        .any(|s| relative == s || relative.starts_with(&format!("{s}/")) || s.is_empty())
1472}
1473
1474pub(super) struct Hit {
1475    pub(super) path: String,
1476    pub(super) match_count: u64,
1477    /// Absolute path of the read-only dependency root this hit came from,
1478    /// or `None` for a primary-workspace hit (issue #2403 follow-up).
1479    pub(super) root: Option<String>,
1480}
1481
1482fn hit_to_value(hit: Hit) -> VmValue {
1483    let Hit {
1484        path,
1485        match_count,
1486        root,
1487    } = hit;
1488    build_dict([
1489        ("path", str_value(&path)),
1490        ("score", VmValue::Float(match_count as f64)),
1491        ("match_count", VmValue::Int(match_count as i64)),
1492        (
1493            "root",
1494            match root {
1495                Some(r) => str_value(&r),
1496                None => VmValue::Nil,
1497            },
1498        ),
1499    ])
1500}
1501
1502fn import_entry(module: &str, resolved: Option<&str>, kind: &str) -> VmValue {
1503    let mut map: harn_vm::value::DictMap = harn_vm::value::DictMap::new();
1504    map.insert(harn_vm::value::intern_key("module"), str_value(module));
1505    map.insert(
1506        harn_vm::value::intern_key("resolved_path"),
1507        match resolved {
1508            Some(p) => str_value(p),
1509            None => VmValue::Nil,
1510        },
1511    );
1512    map.insert(harn_vm::value::intern_key("kind"), str_value(kind));
1513    VmValue::dict(map)
1514}
1515
1516fn empty_stats_response() -> VmValue {
1517    build_dict([
1518        ("indexed_files", VmValue::Int(0)),
1519        ("trigrams", VmValue::Int(0)),
1520        ("words", VmValue::Int(0)),
1521        ("memory_bytes", VmValue::Int(0)),
1522        ("last_rebuild_unix_ms", VmValue::Nil),
1523    ])
1524}
1525
1526fn empty_imports_response(path: &str) -> VmValue {
1527    build_dict([
1528        ("path", str_value(path)),
1529        ("imports", VmValue::List(Arc::new(Vec::new()))),
1530    ])
1531}
1532
1533fn empty_importers_response(module: &str) -> VmValue {
1534    build_dict([
1535        ("module", str_value(module)),
1536        ("importers", VmValue::List(Arc::new(Vec::new()))),
1537    ])
1538}
1539
1540fn unknown_freshness_response(path: &str) -> VmValue {
1541    build_dict([
1542        ("path", str_value(path)),
1543        ("known", VmValue::Bool(false)),
1544        ("stale", VmValue::Bool(true)),
1545        ("indexed_hash", VmValue::Nil),
1546        ("indexed_mtime_ms", VmValue::Nil),
1547        ("disk_hash", VmValue::Nil),
1548        ("disk_mtime_ms", VmValue::Nil),
1549    ])
1550}