1use 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
30pub type SharedIndex = Arc<Mutex<Option<IndexState>>>;
37
38pub(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
81pub(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 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
144fn 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
178pub(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 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
332pub(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
516pub(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 ("start_line", VmValue::Int(sym.start_line as i64)),
738 ("end_line", VmValue::Int(sym.end_line as i64)),
739 ("signature", str_value(&sym.signature)),
740 ])
741 })
742 .collect(),
743 None => Vec::new(),
744 };
745 Ok(VmValue::List(Arc::new(symbols)))
746}
747
748pub(super) fn run_current_seq(
751 index: &SharedIndex,
752 _args: &[VmValue],
753) -> Result<VmValue, HostlibError> {
754 let guard = index.lock().expect("code_index mutex poisoned");
755 let seq = guard.as_ref().map(|s| s.versions.current_seq).unwrap_or(0);
756 Ok(VmValue::Int(seq as i64))
757}
758
759pub(super) fn run_changes_since(
760 index: &SharedIndex,
761 args: &[VmValue],
762) -> Result<VmValue, HostlibError> {
763 let raw = dict_arg(BUILTIN_CHANGES_SINCE, args)?;
764 let dict = raw.as_ref();
765 let seq = optional_non_negative_u64(BUILTIN_CHANGES_SINCE, dict, "seq", 0)?;
766 let limit = optional_positive_usize(BUILTIN_CHANGES_SINCE, dict, "limit")?;
767 let guard = index.lock().expect("code_index mutex poisoned");
768 let records = match guard.as_ref() {
769 Some(state) => state.versions.changes_since(seq, limit),
770 None => Vec::new(),
771 };
772 Ok(VmValue::List(Arc::new(
773 records
774 .into_iter()
775 .map(|r| {
776 build_dict([
777 ("path", str_value(&r.path)),
778 ("seq", VmValue::Int(r.seq as i64)),
779 ("agent_id", VmValue::Int(r.agent_id as i64)),
780 ("op", str_value(r.op.as_str())),
781 ("hash", str_value(r.hash.to_string())),
782 ("size", VmValue::Int(r.size as i64)),
783 ("timestamp_ms", VmValue::Int(r.timestamp_ms)),
784 ])
785 })
786 .collect(),
787 )))
788}
789
790pub(super) fn run_version_record(
791 index: &SharedIndex,
792 args: &[VmValue],
793) -> Result<VmValue, HostlibError> {
794 let raw = dict_arg(BUILTIN_VERSION_RECORD, args)?;
795 let dict = raw.as_ref();
796 let agent_id = require_non_negative_u64(BUILTIN_VERSION_RECORD, dict, "agent_id")?;
797 let path = require_string(BUILTIN_VERSION_RECORD, dict, "path")?;
798 let op_str =
799 optional_string(BUILTIN_VERSION_RECORD, dict, "op")?.unwrap_or_else(|| "write".to_string());
800 let op = EditOp::parse(&op_str).unwrap_or(EditOp::Write);
801 let hash = parse_hash(BUILTIN_VERSION_RECORD, dict, "hash")?;
802 let size = optional_non_negative_u64(BUILTIN_VERSION_RECORD, dict, "size", 0)?;
803 let now = now_unix_ms();
804 let mut guard = index.lock().expect("code_index mutex poisoned");
805 let state = ensure_state(BUILTIN_VERSION_RECORD, &mut guard)?;
806 let normalized = normalize_relative_path(state, &path);
807 let seq = state
808 .versions
809 .record(normalized, agent_id, op, hash, size, now);
810 state.agents.note_edit(agent_id, now);
811 Ok(VmValue::Int(seq as i64))
812}
813
814pub(super) fn run_agent_register(
817 index: &SharedIndex,
818 args: &[VmValue],
819) -> Result<VmValue, HostlibError> {
820 let raw = dict_arg(BUILTIN_AGENT_REGISTER, args)?;
821 let dict = raw.as_ref();
822 let name = optional_string(BUILTIN_AGENT_REGISTER, dict, "name")?
823 .unwrap_or_else(|| "agent".to_string());
824 let requested_id = optional_positive_u64(BUILTIN_AGENT_REGISTER, dict, "agent_id")?;
825 let now = now_unix_ms();
826 let mut guard = index.lock().expect("code_index mutex poisoned");
827 let state = ensure_state(BUILTIN_AGENT_REGISTER, &mut guard)?;
828 let id = match requested_id {
829 Some(id) => state.agents.register_with_id(id, name, now),
830 None => state.agents.register(name, now),
831 };
832 Ok(VmValue::Int(id as i64))
833}
834
835pub(super) fn run_agent_heartbeat(
836 index: &SharedIndex,
837 args: &[VmValue],
838) -> Result<VmValue, HostlibError> {
839 let raw = dict_arg(BUILTIN_AGENT_HEARTBEAT, args)?;
840 let id = require_positive_u64(BUILTIN_AGENT_HEARTBEAT, raw.as_ref(), "agent_id")?;
841 let now = now_unix_ms();
842 let mut guard = index.lock().expect("code_index mutex poisoned");
843 let state = ensure_state(BUILTIN_AGENT_HEARTBEAT, &mut guard)?;
844 state.agents.heartbeat(id, now);
845 Ok(VmValue::Bool(true))
846}
847
848pub(super) fn run_agent_unregister(
849 index: &SharedIndex,
850 args: &[VmValue],
851) -> Result<VmValue, HostlibError> {
852 let raw = dict_arg(BUILTIN_AGENT_UNREGISTER, args)?;
853 let id = require_positive_u64(BUILTIN_AGENT_UNREGISTER, raw.as_ref(), "agent_id")?;
854 let mut guard = index.lock().expect("code_index mutex poisoned");
855 let state = ensure_state(BUILTIN_AGENT_UNREGISTER, &mut guard)?;
856 state.agents.unregister(id);
857 Ok(VmValue::Bool(true))
858}
859
860pub(super) fn run_lock_try(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
861 let raw = dict_arg(BUILTIN_LOCK_TRY, args)?;
862 let dict = raw.as_ref();
863 let agent_id = require_positive_u64(BUILTIN_LOCK_TRY, dict, "agent_id")?;
864 let path = require_string(BUILTIN_LOCK_TRY, dict, "path")?;
865 let ttl = optional_positive_i64(BUILTIN_LOCK_TRY, dict, "ttl_ms")?;
866 let now = now_unix_ms();
867 let mut guard = index.lock().expect("code_index mutex poisoned");
868 let state = ensure_state(BUILTIN_LOCK_TRY, &mut guard)?;
869 let granted = state.agents.try_lock(agent_id, &path, ttl, now);
870 if granted {
871 return Ok(build_dict([
872 ("locked", VmValue::Bool(true)),
873 ("holder", VmValue::Int(agent_id as i64)),
874 ]));
875 }
876 let holder = state.agents.lock_holder(&path, now);
877 Ok(build_dict([
878 ("locked", VmValue::Bool(false)),
879 (
880 "holder",
881 holder
882 .map(|id| VmValue::Int(id as i64))
883 .unwrap_or(VmValue::Nil),
884 ),
885 ]))
886}
887
888pub(super) fn run_lock_release(
889 index: &SharedIndex,
890 args: &[VmValue],
891) -> Result<VmValue, HostlibError> {
892 let raw = dict_arg(BUILTIN_LOCK_RELEASE, args)?;
893 let dict = raw.as_ref();
894 let agent_id = require_positive_u64(BUILTIN_LOCK_RELEASE, dict, "agent_id")?;
895 let path = require_string(BUILTIN_LOCK_RELEASE, dict, "path")?;
896 let mut guard = index.lock().expect("code_index mutex poisoned");
897 let state = ensure_state(BUILTIN_LOCK_RELEASE, &mut guard)?;
898 state.agents.release_lock(agent_id, &path);
899 Ok(VmValue::Bool(true))
900}
901
902pub(super) fn run_status(index: &SharedIndex, _args: &[VmValue]) -> Result<VmValue, HostlibError> {
903 let guard = index.lock().expect("code_index mutex poisoned");
904 match guard.as_ref() {
905 Some(state) => Ok(build_dict([
906 ("file_count", VmValue::Int(state.files.len() as i64)),
907 (
908 "current_seq",
909 VmValue::Int(state.versions.current_seq as i64),
910 ),
911 ("last_indexed_at_ms", VmValue::Int(state.last_built_unix_ms)),
912 (
913 "git_head",
914 state
915 .git_head
916 .as_deref()
917 .map(str_value)
918 .unwrap_or(VmValue::Nil),
919 ),
920 (
921 "agents",
922 VmValue::List(Arc::new(
923 state
924 .agents
925 .agents()
926 .map(|info| {
927 build_dict([
928 ("id", VmValue::Int(info.id as i64)),
929 ("name", str_value(&info.name)),
930 (
931 "state",
932 str_value(match info.state {
933 super::agents::AgentState::Active => "active",
934 super::agents::AgentState::Crashed => "crashed",
935 super::agents::AgentState::Gone => "gone",
936 }),
937 ),
938 ("last_seen_ms", VmValue::Int(info.last_seen_ms)),
939 ("edit_count", VmValue::Int(info.edit_count as i64)),
940 ("lock_count", VmValue::Int(info.locked_paths.len() as i64)),
941 ])
942 })
943 .collect(),
944 )),
945 ),
946 ])),
947 None => Ok(build_dict([
948 ("file_count", VmValue::Int(0)),
949 ("current_seq", VmValue::Int(0)),
950 ("last_indexed_at_ms", VmValue::Int(0)),
951 ("git_head", VmValue::Nil),
952 ("agents", VmValue::List(Arc::new(Vec::new()))),
953 ])),
954 }
955}
956
957pub(super) fn run_current_agent_id(
958 slot: &Arc<Mutex<Option<AgentId>>>,
959 _args: &[VmValue],
960) -> Result<VmValue, HostlibError> {
961 let guard = slot.lock().expect("current_agent slot poisoned");
962 Ok(match *guard {
963 Some(id) => VmValue::Int(id as i64),
964 None => VmValue::Nil,
965 })
966}
967
968pub(super) fn run_cypher(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
971 let raw = dict_arg(BUILTIN_CYPHER, args)?;
972 let dict = raw.as_ref();
973 let query = require_string(BUILTIN_CYPHER, dict, "query")?;
974
975 let guard = index.lock().expect("code_index mutex poisoned");
976 let Some(state) = guard.as_ref() else {
977 return Ok(build_dict([
978 ("rows", VmValue::List(Arc::new(Vec::new()))),
979 ("overlay", VmValue::Nil),
980 ]));
981 };
982
983 let graph = state.overlays.graph(&state.symbols);
984 let rows = super::cypher::execute(&query, graph).map_err(|err| HostlibError::Backend {
985 builtin: BUILTIN_CYPHER,
986 message: err.to_string(),
987 })?;
988
989 let rows_vm: Vec<VmValue> = rows
990 .into_iter()
991 .map(|row| {
992 let mut map: harn_vm::value::DictMap = harn_vm::value::DictMap::new();
993 for (k, v) in row {
994 map.insert(harn_vm::value::intern_key(&k), v.to_vm());
995 }
996 VmValue::dict(map)
997 })
998 .collect();
999
1000 Ok(build_dict([
1001 ("rows", VmValue::List(Arc::new(rows_vm))),
1002 (
1003 "overlay",
1004 match state.overlays.active() {
1005 Some(name) => str_value(name),
1006 None => VmValue::Nil,
1007 },
1008 ),
1009 ]))
1010}
1011
1012pub(super) fn run_branch_overlay(
1013 index: &SharedIndex,
1014 args: &[VmValue],
1015) -> Result<VmValue, HostlibError> {
1016 let raw = dict_arg(BUILTIN_BRANCH_OVERLAY, args)?;
1017 let dict = raw.as_ref();
1018 let branch = optional_string(BUILTIN_BRANCH_OVERLAY, dict, "branch")?;
1019 let activate = optional_bool(BUILTIN_BRANCH_OVERLAY, dict, "activate", true)?;
1020 let action = optional_string(BUILTIN_BRANCH_OVERLAY, dict, "action")?;
1021
1022 let mut guard = index.lock().expect("code_index mutex poisoned");
1023 let state = ensure_state(BUILTIN_BRANCH_OVERLAY, &mut guard)?;
1024
1025 let mut reuse: f64 = 1.0;
1026 match action.as_deref().unwrap_or("activate") {
1027 "deactivate" => {
1028 state.overlays.activate(None);
1029 }
1030 "create" => {
1031 let branch_name = branch.ok_or(HostlibError::MissingParameter {
1032 builtin: BUILTIN_BRANCH_OVERLAY,
1033 param: "branch",
1034 })?;
1035 let mut overlay = super::overlay::BranchOverlay::new(&branch_name);
1036 overlay.materialize(&state.symbols);
1037 state.overlays.set(overlay);
1038 if activate {
1039 state.overlays.activate(Some(branch_name));
1040 }
1041 reuse = state.overlays.reuse_fraction(&state.symbols);
1042 }
1043 "activate" => {
1044 let branch_name = branch.ok_or(HostlibError::MissingParameter {
1045 builtin: BUILTIN_BRANCH_OVERLAY,
1046 param: "branch",
1047 })?;
1048 if state.overlays.get(&branch_name).is_none() {
1052 let mut overlay = super::overlay::BranchOverlay::new(&branch_name);
1053 overlay.materialize(&state.symbols);
1054 state.overlays.set(overlay);
1055 }
1056 state.overlays.activate(Some(branch_name));
1057 reuse = state.overlays.reuse_fraction(&state.symbols);
1058 }
1059 other => {
1060 return Err(HostlibError::InvalidParameter {
1061 builtin: BUILTIN_BRANCH_OVERLAY,
1062 param: "action",
1063 message: format!("expected one of activate|deactivate|create, got `{other}`"),
1064 })
1065 }
1066 }
1067
1068 Ok(build_dict([
1069 (
1070 "active",
1071 match state.overlays.active() {
1072 Some(name) => str_value(name),
1073 None => VmValue::Nil,
1074 },
1075 ),
1076 ("reuse_fraction", VmValue::Float(reuse)),
1077 ]))
1078}
1079
1080pub(super) fn run_freshness(
1081 index: &SharedIndex,
1082 args: &[VmValue],
1083) -> Result<VmValue, HostlibError> {
1084 let raw = dict_arg(BUILTIN_FRESHNESS, args)?;
1085 let dict = raw.as_ref();
1086 let path = require_string(BUILTIN_FRESHNESS, dict, "path")?;
1087
1088 let guard = index.lock().expect("code_index mutex poisoned");
1089 let state = guard.as_ref().ok_or_else(|| HostlibError::Backend {
1090 builtin: BUILTIN_FRESHNESS,
1091 message: "code index has not been initialised — call \
1092 `hostlib_code_index_rebuild` first"
1093 .to_string(),
1094 })?;
1095
1096 let normalized = normalize_relative_path(state, &path);
1097 let file = state
1098 .lookup_path(&normalized)
1099 .and_then(|id| state.files.get(&id));
1100 let Some(file) = file else {
1101 return Ok(unknown_freshness_response(&path));
1102 };
1103
1104 let abs = state.root.join(&file.relative_path);
1105 let (disk_mtime, disk_hash) = match std::fs::read(&abs) {
1106 Ok(bytes) => {
1107 let hash = fnv1a64(&bytes);
1108 let mtime = std::fs::metadata(&abs)
1109 .ok()
1110 .and_then(|m| m.modified().ok())
1111 .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
1112 .map(|d| d.as_millis() as i64)
1113 .unwrap_or(0);
1114 (mtime, Some(hash))
1115 }
1116 Err(_) => (0, None),
1117 };
1118 let stale = disk_hash != Some(file.content_hash);
1119 Ok(build_dict([
1120 ("path", str_value(&file.relative_path)),
1121 ("known", VmValue::Bool(true)),
1122 ("stale", VmValue::Bool(stale)),
1123 (
1124 "indexed_hash",
1125 VmValue::String(arcstr::ArcStr::from(
1126 format!("{:016x}", file.content_hash).as_str(),
1127 )),
1128 ),
1129 ("indexed_mtime_ms", VmValue::Int(file.mtime_ms)),
1130 (
1131 "disk_hash",
1132 match disk_hash {
1133 Some(h) => VmValue::String(arcstr::ArcStr::from(format!("{h:016x}").as_str())),
1134 None => VmValue::Nil,
1135 },
1136 ),
1137 ("disk_mtime_ms", VmValue::Int(disk_mtime)),
1138 ]))
1139}
1140
1141struct FileHashSnapshotEntry {
1144 value: VmValue,
1145 path: String,
1146 hash: Option<String>,
1147}
1148
1149fn file_hash_snapshot_entry(state: &IndexState, path: &str) -> FileHashSnapshotEntry {
1150 let normalized = normalize_relative_path(state, path);
1151 let indexed_file = state
1152 .lookup_path(&normalized)
1153 .and_then(|id| state.files.get(&id));
1154 let abs = state
1155 .absolute_path(path)
1156 .or_else(|| state.absolute_path(&normalized));
1157 let (readable, hash, hash_source, disk_size, disk_mtime_ms) = match abs {
1158 Some(abs) => {
1159 let metadata = std::fs::metadata(&abs).ok();
1160 let mtime_ms = metadata
1161 .as_ref()
1162 .and_then(|m| m.modified().ok())
1163 .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
1164 .map(|d| d.as_millis() as i64);
1165 if let (Some(file), Some(metadata), Some(mtime_ms)) =
1166 (indexed_file, metadata.as_ref(), mtime_ms)
1167 {
1168 if metadata.len() == file.size_bytes && mtime_ms == file.mtime_ms {
1169 return file_hash_snapshot_value(
1170 state,
1171 normalized,
1172 indexed_file,
1173 true,
1174 Some(file.content_hash.to_string()),
1175 "indexed",
1176 VmValue::Int(file.size_bytes as i64),
1177 VmValue::Int(file.mtime_ms),
1178 );
1179 }
1180 }
1181 let bytes = match crate::fs::read(&abs, None) {
1182 Some(result) => result,
1183 None => std::fs::read(&abs),
1184 };
1185 match bytes {
1186 Ok(bytes) => {
1187 let hash = fnv1a64(&bytes).to_string();
1188 (
1189 true,
1190 Some(hash),
1191 "disk",
1192 VmValue::Int(bytes.len() as i64),
1193 mtime_ms.map(VmValue::Int).unwrap_or(VmValue::Nil),
1194 )
1195 }
1196 Err(_) => (false, None, "missing", VmValue::Nil, VmValue::Nil),
1197 }
1198 }
1199 None => (false, None, "missing", VmValue::Nil, VmValue::Nil),
1200 };
1201 file_hash_snapshot_value(
1202 state,
1203 normalized,
1204 indexed_file,
1205 readable,
1206 hash,
1207 hash_source,
1208 disk_size,
1209 disk_mtime_ms,
1210 )
1211}
1212
1213fn file_hash_snapshot_value(
1214 state: &IndexState,
1215 normalized: String,
1216 indexed_file: Option<&super::file_table::IndexedFile>,
1217 readable: bool,
1218 hash: Option<String>,
1219 hash_source: &str,
1220 disk_size: VmValue,
1221 disk_mtime_ms: VmValue,
1222) -> FileHashSnapshotEntry {
1223 let indexed_hash = indexed_file
1224 .map(|file| str_value(file.content_hash.to_string()))
1225 .unwrap_or(VmValue::Nil);
1226 let indexed_mtime_ms = indexed_file
1227 .map(|file| VmValue::Int(file.mtime_ms))
1228 .unwrap_or(VmValue::Nil);
1229 let last_edit_seq = state
1230 .versions
1231 .last_entry(&normalized)
1232 .map(|entry| entry.seq as i64)
1233 .unwrap_or(0);
1234 let hash_value = hash.as_ref().map(str_value).unwrap_or(VmValue::Nil);
1235 let value = build_dict([
1236 ("path", str_value(&normalized)),
1237 ("known", VmValue::Bool(indexed_file.is_some())),
1238 ("readable", VmValue::Bool(readable)),
1239 ("hash", hash_value),
1240 ("hash_source", str_value(hash_source)),
1241 ("size", disk_size),
1242 ("mtime_ms", disk_mtime_ms),
1243 ("indexed_hash", indexed_hash),
1244 ("indexed_mtime_ms", indexed_mtime_ms),
1245 ("last_edit_seq", VmValue::Int(last_edit_seq)),
1246 ]);
1247 FileHashSnapshotEntry {
1248 value,
1249 path: normalized,
1250 hash,
1251 }
1252}
1253
1254fn ensure_state<'a>(
1255 builtin: &'static str,
1256 guard: &'a mut std::sync::MutexGuard<'_, Option<IndexState>>,
1257) -> Result<&'a mut IndexState, HostlibError> {
1258 if guard.is_none() {
1259 return Err(HostlibError::Backend {
1260 builtin,
1261 message: "code index has not been initialised — call \
1262 `hostlib_code_index_rebuild` or restore from a snapshot first"
1263 .to_string(),
1264 });
1265 }
1266 Ok(guard.as_mut().unwrap())
1267}
1268
1269fn parse_hash(
1270 builtin: &'static str,
1271 dict: &harn_vm::value::DictMap,
1272 key: &'static str,
1273) -> Result<u64, HostlibError> {
1274 match dict.get(key) {
1275 None | Some(VmValue::Nil) => Ok(0),
1276 Some(VmValue::Int(n)) if *n >= 0 => Ok(*n as u64),
1277 Some(VmValue::Int(n)) => Err(HostlibError::InvalidParameter {
1278 builtin,
1279 param: key,
1280 message: format!("must be >= 0, got {n}"),
1281 }),
1282 Some(VmValue::String(s)) => s
1283 .parse::<u64>()
1284 .map_err(|_| HostlibError::InvalidParameter {
1285 builtin,
1286 param: key,
1287 message: format!("expected u64-parseable string, got {s:?}"),
1288 }),
1289 Some(other) => Err(HostlibError::InvalidParameter {
1290 builtin,
1291 param: key,
1292 message: format!(
1293 "expected integer or numeric string, got {}",
1294 other.type_name()
1295 ),
1296 }),
1297 }
1298}
1299
1300fn require_positive_u64(
1301 builtin: &'static str,
1302 dict: &harn_vm::value::DictMap,
1303 key: &'static str,
1304) -> Result<u64, HostlibError> {
1305 let raw = require_non_negative_u64(builtin, dict, key)?;
1306 if raw == 0 {
1307 return Err(HostlibError::InvalidParameter {
1308 builtin,
1309 param: key,
1310 message: "must be >= 1".to_string(),
1311 });
1312 }
1313 Ok(raw)
1314}
1315
1316fn require_positive_file_id(
1317 builtin: &'static str,
1318 dict: &harn_vm::value::DictMap,
1319 key: &'static str,
1320) -> Result<FileId, HostlibError> {
1321 let raw = require_positive_u64(builtin, dict, key)?;
1322 FileId::try_from(raw).map_err(|_| HostlibError::InvalidParameter {
1323 builtin,
1324 param: key,
1325 message: "does not fit in file id".to_string(),
1326 })
1327}
1328
1329fn require_non_negative_u64(
1330 builtin: &'static str,
1331 dict: &harn_vm::value::DictMap,
1332 key: &'static str,
1333) -> Result<u64, HostlibError> {
1334 match value_args::optional_i64_no_default(builtin, dict, key)? {
1335 Some(value) if value >= 0 => Ok(value as u64),
1336 Some(value) => Err(HostlibError::InvalidParameter {
1337 builtin,
1338 param: key,
1339 message: format!("must be >= 0, got {value}"),
1340 }),
1341 None => Err(HostlibError::MissingParameter {
1342 builtin,
1343 param: key,
1344 }),
1345 }
1346}
1347
1348fn optional_positive_u64(
1349 builtin: &'static str,
1350 dict: &harn_vm::value::DictMap,
1351 key: &'static str,
1352) -> Result<Option<u64>, HostlibError> {
1353 match dict.get(key) {
1354 None | Some(VmValue::Nil) => Ok(None),
1355 Some(_) => require_positive_u64(builtin, dict, key).map(Some),
1356 }
1357}
1358
1359fn optional_non_negative_u64(
1360 builtin: &'static str,
1361 dict: &harn_vm::value::DictMap,
1362 key: &'static str,
1363 default: u64,
1364) -> Result<u64, HostlibError> {
1365 match dict.get(key) {
1366 None | Some(VmValue::Nil) => Ok(default),
1367 Some(_) => require_non_negative_u64(builtin, dict, key),
1368 }
1369}
1370
1371fn optional_positive_i64(
1372 builtin: &'static str,
1373 dict: &harn_vm::value::DictMap,
1374 key: &'static str,
1375) -> Result<Option<i64>, HostlibError> {
1376 match value_args::optional_i64_no_default(builtin, dict, key)? {
1377 None => Ok(None),
1378 Some(value) if value >= 1 => Ok(Some(value)),
1379 Some(value) => Err(HostlibError::InvalidParameter {
1380 builtin,
1381 param: key,
1382 message: format!("must be >= 1, got {value}"),
1383 }),
1384 }
1385}
1386
1387fn optional_positive_usize(
1388 builtin: &'static str,
1389 dict: &harn_vm::value::DictMap,
1390 key: &'static str,
1391) -> Result<Option<usize>, HostlibError> {
1392 match optional_positive_u64(builtin, dict, key)? {
1393 Some(value) => {
1394 usize::try_from(value)
1395 .map(Some)
1396 .map_err(|_| HostlibError::InvalidParameter {
1397 builtin,
1398 param: key,
1399 message: "does not fit in usize".to_string(),
1400 })
1401 }
1402 None => Ok(None),
1403 }
1404}
1405
1406pub(super) fn normalize_relative_path_for(state: &IndexState, path: &str) -> String {
1412 normalize_relative_path(state, path)
1413}
1414
1415fn normalize_relative_path(state: &IndexState, path: &str) -> String {
1416 if let Some(rel) = state
1417 .lookup_path(path)
1418 .and_then(|id| state.files.get(&id))
1419 .map(|f| f.relative_path.clone())
1420 {
1421 return rel;
1422 }
1423 let p = std::path::Path::new(path);
1424 if p.is_absolute() {
1425 if let Ok(rel) = p.strip_prefix(&state.root) {
1426 return to_agent_path(rel);
1427 }
1428 }
1429 to_agent_path_str(path)
1430}
1431
1432fn candidates_for(state: &IndexState, needle: &str) -> Vec<FileId> {
1433 if needle.len() >= 3 {
1434 let trigrams = trigram::query_trigrams(needle);
1435 return state.trigrams.query(&trigrams).into_iter().collect();
1436 }
1437 state.files.keys().copied().collect()
1438}
1439
1440fn read_file_text(root: &std::path::Path, relative: &str) -> Option<String> {
1441 let path = root.join(relative);
1442 match crate::fs::read_to_string(&path, None) {
1443 Some(result) => result.ok(),
1444 None => std::fs::read_to_string(path).ok(),
1445 }
1446}
1447
1448fn count_matches(haystack: &str, needle: &str, case_sensitive: bool) -> u64 {
1449 if case_sensitive {
1450 haystack.matches(needle).count() as u64
1451 } else {
1452 let lower_h = haystack.to_lowercase();
1453 let lower_n = needle.to_lowercase();
1454 lower_h.matches(&lower_n).count() as u64
1455 }
1456}
1457
1458fn scope_allows(scope: &[String], relative: &str) -> bool {
1459 if scope.is_empty() {
1460 return true;
1461 }
1462 scope
1463 .iter()
1464 .any(|s| relative == s || relative.starts_with(&format!("{s}/")) || s.is_empty())
1465}
1466
1467pub(super) struct Hit {
1468 pub(super) path: String,
1469 pub(super) match_count: u64,
1470 pub(super) root: Option<String>,
1473}
1474
1475fn hit_to_value(hit: Hit) -> VmValue {
1476 let Hit {
1477 path,
1478 match_count,
1479 root,
1480 } = hit;
1481 build_dict([
1482 ("path", str_value(&path)),
1483 ("score", VmValue::Float(match_count as f64)),
1484 ("match_count", VmValue::Int(match_count as i64)),
1485 (
1486 "root",
1487 match root {
1488 Some(r) => str_value(&r),
1489 None => VmValue::Nil,
1490 },
1491 ),
1492 ])
1493}
1494
1495fn import_entry(module: &str, resolved: Option<&str>, kind: &str) -> VmValue {
1496 let mut map: harn_vm::value::DictMap = harn_vm::value::DictMap::new();
1497 map.insert(harn_vm::value::intern_key("module"), str_value(module));
1498 map.insert(
1499 harn_vm::value::intern_key("resolved_path"),
1500 match resolved {
1501 Some(p) => str_value(p),
1502 None => VmValue::Nil,
1503 },
1504 );
1505 map.insert(harn_vm::value::intern_key("kind"), str_value(kind));
1506 VmValue::dict(map)
1507}
1508
1509fn empty_stats_response() -> VmValue {
1510 build_dict([
1511 ("indexed_files", VmValue::Int(0)),
1512 ("trigrams", VmValue::Int(0)),
1513 ("words", VmValue::Int(0)),
1514 ("memory_bytes", VmValue::Int(0)),
1515 ("last_rebuild_unix_ms", VmValue::Nil),
1516 ])
1517}
1518
1519fn empty_imports_response(path: &str) -> VmValue {
1520 build_dict([
1521 ("path", str_value(path)),
1522 ("imports", VmValue::List(Arc::new(Vec::new()))),
1523 ])
1524}
1525
1526fn empty_importers_response(module: &str) -> VmValue {
1527 build_dict([
1528 ("module", str_value(module)),
1529 ("importers", VmValue::List(Arc::new(Vec::new()))),
1530 ])
1531}
1532
1533fn unknown_freshness_response(path: &str) -> VmValue {
1534 build_dict([
1535 ("path", str_value(path)),
1536 ("known", VmValue::Bool(false)),
1537 ("stale", VmValue::Bool(true)),
1538 ("indexed_hash", VmValue::Nil),
1539 ("indexed_mtime_ms", VmValue::Nil),
1540 ("disk_hash", VmValue::Nil),
1541 ("disk_mtime_ms", VmValue::Nil),
1542 ])
1543}