1use std::collections::{BTreeMap, HashSet};
11use std::path::PathBuf;
12use std::rc::Rc;
13use std::sync::{Arc, Mutex};
14use std::time::Instant;
15
16use harn_vm::VmValue;
17
18use super::agents::AgentId;
19use super::file_table::{fnv1a64, FileId};
20use super::imports;
21use super::state::{now_unix_ms, IndexState};
22use super::trigram;
23use super::versions::EditOp;
24use crate::error::HostlibError;
25use crate::tools::args::{
26 build_dict, dict_arg, optional_bool, optional_int, optional_int_list, optional_string,
27 optional_string_list, require_int, require_string, str_value,
28};
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";
55
56pub(super) const BUILTIN_READ_RANGE: &str = "hostlib_code_index_read_range";
57pub(super) const BUILTIN_REINDEX_FILE: &str = "hostlib_code_index_reindex_file";
58pub(super) const BUILTIN_TRIGRAM_QUERY: &str = "hostlib_code_index_trigram_query";
59pub(super) const BUILTIN_EXTRACT_TRIGRAMS: &str = "hostlib_code_index_extract_trigrams";
60pub(super) const BUILTIN_WORD_GET: &str = "hostlib_code_index_word_get";
61pub(super) const BUILTIN_DEPS_GET: &str = "hostlib_code_index_deps_get";
62pub(super) const BUILTIN_OUTLINE_GET: &str = "hostlib_code_index_outline_get";
63
64pub(super) const BUILTIN_CURRENT_SEQ: &str = "hostlib_code_index_current_seq";
65pub(super) const BUILTIN_CHANGES_SINCE: &str = "hostlib_code_index_changes_since";
66pub(super) const BUILTIN_VERSION_RECORD: &str = "hostlib_code_index_version_record";
67
68pub(super) const BUILTIN_AGENT_REGISTER: &str = "hostlib_code_index_agent_register";
69pub(super) const BUILTIN_AGENT_HEARTBEAT: &str = "hostlib_code_index_agent_heartbeat";
70pub(super) const BUILTIN_AGENT_UNREGISTER: &str = "hostlib_code_index_agent_unregister";
71pub(super) const BUILTIN_LOCK_TRY: &str = "hostlib_code_index_lock_try";
72pub(super) const BUILTIN_LOCK_RELEASE: &str = "hostlib_code_index_lock_release";
73pub(super) const BUILTIN_STATUS: &str = "hostlib_code_index_status";
74pub(super) const BUILTIN_CURRENT_AGENT_ID: &str = "hostlib_code_index_current_agent_id";
75
76pub(super) fn run_query(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
79 let raw = dict_arg(BUILTIN_QUERY, args)?;
80 let dict = raw.as_ref();
81 let needle = require_string(BUILTIN_QUERY, dict, "needle")?;
82 if needle.is_empty() {
83 return Err(HostlibError::InvalidParameter {
84 builtin: BUILTIN_QUERY,
85 param: "needle",
86 message: "must not be empty".to_string(),
87 });
88 }
89 let case_sensitive = optional_bool(BUILTIN_QUERY, dict, "case_sensitive", false)?;
90 let max_results = optional_int(BUILTIN_QUERY, dict, "max_results", 100)?;
91 if max_results < 1 {
92 return Err(HostlibError::InvalidParameter {
93 builtin: BUILTIN_QUERY,
94 param: "max_results",
95 message: "must be >= 1".to_string(),
96 });
97 }
98 let scope = optional_string_list(BUILTIN_QUERY, dict, "scope")?;
99
100 let guard = index.lock().expect("code_index mutex poisoned");
101 let Some(state) = guard.as_ref() else {
102 return Ok(empty_query_response());
103 };
104
105 let candidate_ids = candidates_for(state, &needle);
106 let mut hits: Vec<Hit> = Vec::new();
107 for id in candidate_ids {
108 let Some(file) = state.files.get(&id) else {
109 continue;
110 };
111 if !scope_allows(&scope, &file.relative_path) {
112 continue;
113 }
114 let Some(text) = read_file_text(&state.root, &file.relative_path) else {
115 continue;
116 };
117 let count = count_matches(&text, &needle, case_sensitive);
118 if count == 0 {
119 continue;
120 }
121 hits.push(Hit {
122 path: file.relative_path.clone(),
123 score: count as f64,
124 match_count: count,
125 });
126 }
127 hits.sort_by(|a, b| {
128 b.match_count
129 .cmp(&a.match_count)
130 .then_with(|| a.path.cmp(&b.path))
131 });
132 let max = max_results as usize;
133 let truncated = hits.len() > max;
134 if truncated {
135 hits.truncate(max);
136 }
137 Ok(build_dict([
138 (
139 "results",
140 VmValue::List(Rc::new(hits.into_iter().map(hit_to_value).collect())),
141 ),
142 ("truncated", VmValue::Bool(truncated)),
143 ]))
144}
145
146pub(super) fn run_rebuild(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
147 let raw = dict_arg(BUILTIN_REBUILD, args)?;
148 let dict = raw.as_ref();
149 let _force = optional_bool(BUILTIN_REBUILD, dict, "force", false)?;
150 let root = optional_string(BUILTIN_REBUILD, dict, "root")?
151 .map(PathBuf::from)
152 .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
153 if !root.exists() {
154 return Err(HostlibError::InvalidParameter {
155 builtin: BUILTIN_REBUILD,
156 param: "root",
157 message: format!("path `{}` does not exist", root.display()),
158 });
159 }
160 if !root.is_dir() {
161 return Err(HostlibError::InvalidParameter {
162 builtin: BUILTIN_REBUILD,
163 param: "root",
164 message: format!("path `{}` is not a directory", root.display()),
165 });
166 }
167 let started = Instant::now();
168 let (state, outcome) = IndexState::build_from_root(&root);
169 let elapsed_ms = started.elapsed().as_millis() as i64;
170 {
171 let mut guard = index.lock().expect("code_index mutex poisoned");
172 *guard = Some(state);
173 }
174 Ok(build_dict([
175 ("files_indexed", VmValue::Int(outcome.files_indexed as i64)),
176 ("files_skipped", VmValue::Int(outcome.files_skipped as i64)),
177 ("elapsed_ms", VmValue::Int(elapsed_ms)),
178 ]))
179}
180
181pub(super) fn run_stats(index: &SharedIndex, _args: &[VmValue]) -> Result<VmValue, HostlibError> {
182 let guard = index.lock().expect("code_index mutex poisoned");
183 let Some(state) = guard.as_ref() else {
184 return Ok(empty_stats_response());
185 };
186 Ok(build_dict([
187 ("indexed_files", VmValue::Int(state.files.len() as i64)),
188 (
189 "trigrams",
190 VmValue::Int(state.trigrams.distinct_trigrams() as i64),
191 ),
192 ("words", VmValue::Int(state.words.distinct_words() as i64)),
193 ("memory_bytes", VmValue::Int(state.estimated_bytes() as i64)),
194 (
195 "last_rebuild_unix_ms",
196 VmValue::Int(state.last_built_unix_ms),
197 ),
198 ]))
199}
200
201pub(super) fn run_imports_for(
202 index: &SharedIndex,
203 args: &[VmValue],
204) -> Result<VmValue, HostlibError> {
205 let raw = dict_arg(BUILTIN_IMPORTS_FOR, args)?;
206 let dict = raw.as_ref();
207 let path = require_string(BUILTIN_IMPORTS_FOR, dict, "path")?;
208 let guard = index.lock().expect("code_index mutex poisoned");
209 let Some(state) = guard.as_ref() else {
210 return Ok(empty_imports_response(&path));
211 };
212 let Some(file_id) = state.lookup_path(&path) else {
213 return Ok(empty_imports_response(&path));
214 };
215 let Some(file) = state.files.get(&file_id) else {
216 return Ok(empty_imports_response(&path));
217 };
218 let kind = imports::import_kind(&file.language).to_string();
219 let base_dir = imports::parent_dir(&file.relative_path);
220 let resolved_ids: HashSet<FileId> = state.deps.imports_of(file_id).into_iter().collect();
221 let mut entries: Vec<VmValue> = Vec::with_capacity(file.imports.len());
222 for raw_import in &file.imports {
223 let resolved_path =
224 imports::resolve_module(raw_import, &file.language, &base_dir, &state.path_to_id)
225 .filter(|id| resolved_ids.contains(id))
226 .and_then(|id| state.files.get(&id).map(|f| f.relative_path.clone()));
227 entries.push(import_entry(raw_import, resolved_path.as_deref(), &kind));
228 }
229 Ok(build_dict([
230 ("path", str_value(&file.relative_path)),
231 ("imports", VmValue::List(Rc::new(entries))),
232 ]))
233}
234
235pub(super) fn run_importers_of(
236 index: &SharedIndex,
237 args: &[VmValue],
238) -> Result<VmValue, HostlibError> {
239 let raw = dict_arg(BUILTIN_IMPORTERS_OF, args)?;
240 let dict = raw.as_ref();
241 let module = require_string(BUILTIN_IMPORTERS_OF, dict, "module")?;
242 let guard = index.lock().expect("code_index mutex poisoned");
243 let Some(state) = guard.as_ref() else {
244 return Ok(empty_importers_response(&module));
245 };
246
247 let target_id = state.lookup_path(&module).or_else(|| {
248 let needle = format!("/{module}");
252 state
253 .path_to_id
254 .iter()
255 .find(|(p, _)| p.ends_with(&needle) || *p == &module)
256 .map(|(_, id)| *id)
257 });
258
259 let mut importers: Vec<String> = match target_id {
260 Some(id) => state
261 .deps
262 .importers_of(id)
263 .into_iter()
264 .filter_map(|importer_id| {
265 state
266 .files
267 .get(&importer_id)
268 .map(|f| f.relative_path.clone())
269 })
270 .collect(),
271 None => Vec::new(),
272 };
273 importers.sort();
274 Ok(build_dict([
275 ("module", str_value(&module)),
276 (
277 "importers",
278 VmValue::List(Rc::new(importers.into_iter().map(str_value).collect())),
279 ),
280 ]))
281}
282
283pub(super) fn run_path_to_id(
286 index: &SharedIndex,
287 args: &[VmValue],
288) -> Result<VmValue, HostlibError> {
289 let raw = dict_arg(BUILTIN_PATH_TO_ID, args)?;
290 let path = require_string(BUILTIN_PATH_TO_ID, raw.as_ref(), "path")?;
291 let guard = index.lock().expect("code_index mutex poisoned");
292 let id = guard.as_ref().and_then(|s| s.lookup_path(&path));
293 Ok(match id {
294 Some(id) => VmValue::Int(id as i64),
295 None => VmValue::Nil,
296 })
297}
298
299pub(super) fn run_id_to_path(
300 index: &SharedIndex,
301 args: &[VmValue],
302) -> Result<VmValue, HostlibError> {
303 let raw = dict_arg(BUILTIN_ID_TO_PATH, args)?;
304 let id = require_int(BUILTIN_ID_TO_PATH, raw.as_ref(), "file_id")? as FileId;
305 let guard = index.lock().expect("code_index mutex poisoned");
306 let path = guard
307 .as_ref()
308 .and_then(|s| s.files.get(&id))
309 .map(|f| f.relative_path.clone());
310 Ok(match path {
311 Some(p) => str_value(&p),
312 None => VmValue::Nil,
313 })
314}
315
316pub(super) fn run_file_ids(
317 index: &SharedIndex,
318 _args: &[VmValue],
319) -> Result<VmValue, HostlibError> {
320 let guard = index.lock().expect("code_index mutex poisoned");
321 let mut ids: Vec<FileId> = guard
322 .as_ref()
323 .map(|s| s.files.keys().copied().collect())
324 .unwrap_or_default();
325 ids.sort_unstable();
326 Ok(VmValue::List(Rc::new(
327 ids.into_iter().map(|id| VmValue::Int(id as i64)).collect(),
328 )))
329}
330
331pub(super) fn run_file_meta(
332 index: &SharedIndex,
333 args: &[VmValue],
334) -> Result<VmValue, HostlibError> {
335 let raw = dict_arg(BUILTIN_FILE_META, args)?;
336 let dict = raw.as_ref();
337 let guard = index.lock().expect("code_index mutex poisoned");
338 let Some(state) = guard.as_ref() else {
339 return Ok(VmValue::Nil);
340 };
341 let id_opt: Option<FileId> = if let Some(VmValue::Int(n)) = dict.get("file_id") {
342 Some(*n as FileId)
343 } else if let Some(VmValue::String(p)) = dict.get("path") {
344 state.lookup_path(p)
345 } else {
346 return Err(HostlibError::MissingParameter {
347 builtin: BUILTIN_FILE_META,
348 param: "file_id|path",
349 });
350 };
351 let Some(id) = id_opt else {
352 return Ok(VmValue::Nil);
353 };
354 let Some(file) = state.files.get(&id) else {
355 return Ok(VmValue::Nil);
356 };
357 let last_edit_seq = state
358 .versions
359 .last_entry(&file.relative_path)
360 .map(|e| e.seq)
361 .unwrap_or(0);
362 Ok(build_dict([
363 ("id", VmValue::Int(file.id as i64)),
364 ("path", str_value(&file.relative_path)),
365 ("language", str_value(&file.language)),
366 ("size", VmValue::Int(file.size_bytes as i64)),
367 ("line_count", VmValue::Int(file.line_count as i64)),
368 ("hash", str_value(file.content_hash.to_string())),
369 ("mtime_ms", VmValue::Int(file.mtime_ms)),
370 ("last_edit_seq", VmValue::Int(last_edit_seq as i64)),
371 ]))
372}
373
374pub(super) fn run_file_hash(
375 index: &SharedIndex,
376 args: &[VmValue],
377) -> Result<VmValue, HostlibError> {
378 let raw = dict_arg(BUILTIN_FILE_HASH, args)?;
379 let path = require_string(BUILTIN_FILE_HASH, raw.as_ref(), "path")?;
380 let guard = index.lock().expect("code_index mutex poisoned");
381 let Some(state) = guard.as_ref() else {
382 return Ok(VmValue::Nil);
383 };
384 let Some(abs) = state.absolute_path(&path) else {
385 return Ok(VmValue::Nil);
386 };
387 match std::fs::read(&abs) {
388 Ok(bytes) => Ok(str_value(fnv1a64(&bytes).to_string())),
389 Err(_) => Ok(VmValue::Nil),
390 }
391}
392
393pub(super) fn run_read_range(
396 index: &SharedIndex,
397 args: &[VmValue],
398) -> Result<VmValue, HostlibError> {
399 let raw = dict_arg(BUILTIN_READ_RANGE, args)?;
400 let dict = raw.as_ref();
401 let path = require_string(BUILTIN_READ_RANGE, dict, "path")?;
402 let start = match dict.get("start") {
403 None | Some(VmValue::Nil) => None,
404 Some(VmValue::Int(n)) => Some(*n),
405 Some(other) => {
406 return Err(HostlibError::InvalidParameter {
407 builtin: BUILTIN_READ_RANGE,
408 param: "start",
409 message: format!("expected integer, got {}", other.type_name()),
410 });
411 }
412 };
413 let end = match dict.get("end") {
414 None | Some(VmValue::Nil) => None,
415 Some(VmValue::Int(n)) => Some(*n),
416 Some(other) => {
417 return Err(HostlibError::InvalidParameter {
418 builtin: BUILTIN_READ_RANGE,
419 param: "end",
420 message: format!("expected integer, got {}", other.type_name()),
421 });
422 }
423 };
424 let guard = index.lock().expect("code_index mutex poisoned");
425 let abs = match guard.as_ref() {
426 Some(state) => {
427 state
428 .absolute_path(&path)
429 .ok_or_else(|| HostlibError::InvalidParameter {
430 builtin: BUILTIN_READ_RANGE,
431 param: "path",
432 message: "path must stay within the indexed workspace root".to_string(),
433 })?
434 }
435 None => PathBuf::from(&path),
436 };
437 drop(guard);
438
439 let content = match std::fs::read_to_string(&abs) {
440 Ok(s) => s,
441 Err(_) => {
442 return Err(HostlibError::Backend {
443 builtin: BUILTIN_READ_RANGE,
444 message: format!("file not found: {path}"),
445 })
446 }
447 };
448
449 if start.is_none() && end.is_none() {
450 return Ok(build_dict([("content", str_value(&content))]));
451 }
452 let lines: Vec<&str> = content.split('\n').collect();
453 let total = lines.len() as i64;
454 let lo = (start.unwrap_or(1) - 1).max(0) as usize;
455 let hi = end.unwrap_or(total).min(total).max(0) as usize;
456 if lo >= hi {
457 return Ok(build_dict([
458 ("content", str_value("")),
459 ("start", VmValue::Int((lo as i64) + 1)),
460 ("end", VmValue::Int(hi as i64)),
461 ]));
462 }
463 let slice = lines[lo..hi].join("\n");
464 Ok(build_dict([
465 ("content", str_value(&slice)),
466 ("start", VmValue::Int((lo as i64) + 1)),
467 ("end", VmValue::Int(hi as i64)),
468 ]))
469}
470
471pub(super) fn run_reindex_file(
472 index: &SharedIndex,
473 args: &[VmValue],
474) -> Result<VmValue, HostlibError> {
475 let raw = dict_arg(BUILTIN_REINDEX_FILE, args)?;
476 let path = require_string(BUILTIN_REINDEX_FILE, raw.as_ref(), "path")?;
477 let mut guard = index.lock().expect("code_index mutex poisoned");
478 let Some(state) = guard.as_mut() else {
479 return Ok(build_dict([
480 ("indexed", VmValue::Bool(false)),
481 ("file_id", VmValue::Nil),
482 ]));
483 };
484 let Some(abs) = state.absolute_path(&path) else {
485 return Err(HostlibError::InvalidParameter {
486 builtin: BUILTIN_REINDEX_FILE,
487 param: "path",
488 message: "path must stay within the indexed workspace root".to_string(),
489 });
490 };
491 let id = state.reindex_file(&abs);
492 Ok(build_dict([
493 ("indexed", VmValue::Bool(id.is_some())),
494 (
495 "file_id",
496 id.map(|i| VmValue::Int(i as i64)).unwrap_or(VmValue::Nil),
497 ),
498 ]))
499}
500
501pub(super) fn run_trigram_query(
502 index: &SharedIndex,
503 args: &[VmValue],
504) -> Result<VmValue, HostlibError> {
505 let raw = dict_arg(BUILTIN_TRIGRAM_QUERY, args)?;
506 let dict = raw.as_ref();
507 let trigrams_raw = optional_int_list(BUILTIN_TRIGRAM_QUERY, dict, "trigrams")?;
508 let max_files = match dict.get("max_files") {
509 None | Some(VmValue::Nil) => None,
510 Some(VmValue::Int(n)) => Some(*n as usize),
511 Some(other) => {
512 return Err(HostlibError::InvalidParameter {
513 builtin: BUILTIN_TRIGRAM_QUERY,
514 param: "max_files",
515 message: format!("expected integer, got {}", other.type_name()),
516 })
517 }
518 };
519 let trigrams: Vec<u32> = trigrams_raw.into_iter().map(|n| n as u32).collect();
520 let guard = index.lock().expect("code_index mutex poisoned");
521 let mut ids: Vec<FileId> = match guard.as_ref() {
522 Some(state) => state.trigrams.query(&trigrams).into_iter().collect(),
523 None => Vec::new(),
524 };
525 ids.sort_unstable();
526 if let Some(limit) = max_files {
527 ids.truncate(limit);
528 }
529 Ok(VmValue::List(Rc::new(
530 ids.into_iter().map(|id| VmValue::Int(id as i64)).collect(),
531 )))
532}
533
534pub(super) fn run_extract_trigrams(
535 _index: &SharedIndex,
536 args: &[VmValue],
537) -> Result<VmValue, HostlibError> {
538 let raw = dict_arg(BUILTIN_EXTRACT_TRIGRAMS, args)?;
539 let query = require_string(BUILTIN_EXTRACT_TRIGRAMS, raw.as_ref(), "query")?;
540 let mut tgs = trigram::query_trigrams(&query);
541 tgs.sort_unstable();
542 Ok(VmValue::List(Rc::new(
543 tgs.into_iter().map(|n| VmValue::Int(n as i64)).collect(),
544 )))
545}
546
547pub(super) fn run_word_get(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
548 let raw = dict_arg(BUILTIN_WORD_GET, args)?;
549 let word = require_string(BUILTIN_WORD_GET, raw.as_ref(), "word")?;
550 let guard = index.lock().expect("code_index mutex poisoned");
551 let hits: Vec<VmValue> = match guard.as_ref() {
552 Some(state) => state
553 .words
554 .get(&word)
555 .iter()
556 .map(|h| {
557 build_dict([
558 ("file_id", VmValue::Int(h.file as i64)),
559 ("line", VmValue::Int(h.line as i64)),
560 ])
561 })
562 .collect(),
563 None => Vec::new(),
564 };
565 Ok(VmValue::List(Rc::new(hits)))
566}
567
568pub(super) fn run_deps_get(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
569 let raw = dict_arg(BUILTIN_DEPS_GET, args)?;
570 let dict = raw.as_ref();
571 let id = require_int(BUILTIN_DEPS_GET, dict, "file_id")? as FileId;
572 let direction = optional_string(BUILTIN_DEPS_GET, dict, "direction")?
573 .unwrap_or_else(|| "importers".to_string());
574 let guard = index.lock().expect("code_index mutex poisoned");
575 let mut neighbors: Vec<FileId> = match guard.as_ref() {
576 Some(state) => match direction.as_str() {
577 "importers" => state.deps.importers_of(id),
578 "imports" => state.deps.imports_of(id),
579 _ => {
580 return Err(HostlibError::InvalidParameter {
581 builtin: BUILTIN_DEPS_GET,
582 param: "direction",
583 message: format!("expected \"importers\" or \"imports\", got {direction:?}"),
584 })
585 }
586 },
587 None => Vec::new(),
588 };
589 neighbors.sort_unstable();
590 Ok(VmValue::List(Rc::new(
591 neighbors
592 .into_iter()
593 .map(|id| VmValue::Int(id as i64))
594 .collect(),
595 )))
596}
597
598pub(super) fn run_outline_get(
599 index: &SharedIndex,
600 args: &[VmValue],
601) -> Result<VmValue, HostlibError> {
602 let raw = dict_arg(BUILTIN_OUTLINE_GET, args)?;
603 let id = require_int(BUILTIN_OUTLINE_GET, raw.as_ref(), "file_id")? as FileId;
604 let guard = index.lock().expect("code_index mutex poisoned");
605 let symbols: Vec<VmValue> = match guard.as_ref().and_then(|s| s.files.get(&id)) {
606 Some(file) => file
607 .symbols
608 .iter()
609 .map(|sym| {
610 build_dict([
611 ("name", str_value(&sym.name)),
612 ("kind", str_value(&sym.kind)),
613 ("start_line", VmValue::Int(sym.start_line as i64)),
614 ("end_line", VmValue::Int(sym.end_line as i64)),
615 ("signature", str_value(&sym.signature)),
616 ])
617 })
618 .collect(),
619 None => Vec::new(),
620 };
621 Ok(VmValue::List(Rc::new(symbols)))
622}
623
624pub(super) fn run_current_seq(
627 index: &SharedIndex,
628 _args: &[VmValue],
629) -> Result<VmValue, HostlibError> {
630 let guard = index.lock().expect("code_index mutex poisoned");
631 let seq = guard.as_ref().map(|s| s.versions.current_seq).unwrap_or(0);
632 Ok(VmValue::Int(seq as i64))
633}
634
635pub(super) fn run_changes_since(
636 index: &SharedIndex,
637 args: &[VmValue],
638) -> Result<VmValue, HostlibError> {
639 let raw = dict_arg(BUILTIN_CHANGES_SINCE, args)?;
640 let dict = raw.as_ref();
641 let seq = optional_int(BUILTIN_CHANGES_SINCE, dict, "seq", 0)?.max(0) as u64;
642 let limit = match dict.get("limit") {
643 None | Some(VmValue::Nil) => None,
644 Some(VmValue::Int(n)) => Some(*n as usize),
645 Some(other) => {
646 return Err(HostlibError::InvalidParameter {
647 builtin: BUILTIN_CHANGES_SINCE,
648 param: "limit",
649 message: format!("expected integer, got {}", other.type_name()),
650 })
651 }
652 };
653 let guard = index.lock().expect("code_index mutex poisoned");
654 let records = match guard.as_ref() {
655 Some(state) => state.versions.changes_since(seq, limit),
656 None => Vec::new(),
657 };
658 Ok(VmValue::List(Rc::new(
659 records
660 .into_iter()
661 .map(|r| {
662 build_dict([
663 ("path", str_value(&r.path)),
664 ("seq", VmValue::Int(r.seq as i64)),
665 ("agent_id", VmValue::Int(r.agent_id as i64)),
666 ("op", str_value(r.op.as_str())),
667 ("hash", str_value(r.hash.to_string())),
668 ("size", VmValue::Int(r.size as i64)),
669 ("timestamp_ms", VmValue::Int(r.timestamp_ms)),
670 ])
671 })
672 .collect(),
673 )))
674}
675
676pub(super) fn run_version_record(
677 index: &SharedIndex,
678 args: &[VmValue],
679) -> Result<VmValue, HostlibError> {
680 let raw = dict_arg(BUILTIN_VERSION_RECORD, args)?;
681 let dict = raw.as_ref();
682 let agent_id = require_int(BUILTIN_VERSION_RECORD, dict, "agent_id")? as AgentId;
683 let path = require_string(BUILTIN_VERSION_RECORD, dict, "path")?;
684 let op_str =
685 optional_string(BUILTIN_VERSION_RECORD, dict, "op")?.unwrap_or_else(|| "write".to_string());
686 let op = EditOp::parse(&op_str).unwrap_or(EditOp::Write);
687 let hash = parse_hash(BUILTIN_VERSION_RECORD, dict, "hash")?;
688 let size = optional_int(BUILTIN_VERSION_RECORD, dict, "size", 0)?.max(0) as u64;
689 let now = now_unix_ms();
690 let mut guard = index.lock().expect("code_index mutex poisoned");
691 let state = ensure_state(BUILTIN_VERSION_RECORD, &mut guard)?;
692 let normalized = normalize_relative_path(state, &path);
693 let seq = state
694 .versions
695 .record(normalized, agent_id, op, hash, size, now);
696 state.agents.note_edit(agent_id, now);
697 Ok(VmValue::Int(seq as i64))
698}
699
700pub(super) fn run_agent_register(
703 index: &SharedIndex,
704 args: &[VmValue],
705) -> Result<VmValue, HostlibError> {
706 let raw = dict_arg(BUILTIN_AGENT_REGISTER, args)?;
707 let dict = raw.as_ref();
708 let name = optional_string(BUILTIN_AGENT_REGISTER, dict, "name")?
709 .unwrap_or_else(|| "agent".to_string());
710 let requested_id = match dict.get("agent_id") {
711 None | Some(VmValue::Nil) => None,
712 Some(VmValue::Int(n)) => Some(*n as AgentId),
713 Some(other) => {
714 return Err(HostlibError::InvalidParameter {
715 builtin: BUILTIN_AGENT_REGISTER,
716 param: "agent_id",
717 message: format!("expected integer, got {}", other.type_name()),
718 })
719 }
720 };
721 let now = now_unix_ms();
722 let mut guard = index.lock().expect("code_index mutex poisoned");
723 let state = ensure_state(BUILTIN_AGENT_REGISTER, &mut guard)?;
724 let id = match requested_id {
725 Some(id) => state.agents.register_with_id(id, name, now),
726 None => state.agents.register(name, now),
727 };
728 Ok(VmValue::Int(id as i64))
729}
730
731pub(super) fn run_agent_heartbeat(
732 index: &SharedIndex,
733 args: &[VmValue],
734) -> Result<VmValue, HostlibError> {
735 let raw = dict_arg(BUILTIN_AGENT_HEARTBEAT, args)?;
736 let id = require_int(BUILTIN_AGENT_HEARTBEAT, raw.as_ref(), "agent_id")? as AgentId;
737 let now = now_unix_ms();
738 let mut guard = index.lock().expect("code_index mutex poisoned");
739 let state = ensure_state(BUILTIN_AGENT_HEARTBEAT, &mut guard)?;
740 state.agents.heartbeat(id, now);
741 Ok(VmValue::Bool(true))
742}
743
744pub(super) fn run_agent_unregister(
745 index: &SharedIndex,
746 args: &[VmValue],
747) -> Result<VmValue, HostlibError> {
748 let raw = dict_arg(BUILTIN_AGENT_UNREGISTER, args)?;
749 let id = require_int(BUILTIN_AGENT_UNREGISTER, raw.as_ref(), "agent_id")? as AgentId;
750 let mut guard = index.lock().expect("code_index mutex poisoned");
751 let state = ensure_state(BUILTIN_AGENT_UNREGISTER, &mut guard)?;
752 state.agents.unregister(id);
753 Ok(VmValue::Bool(true))
754}
755
756pub(super) fn run_lock_try(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
757 let raw = dict_arg(BUILTIN_LOCK_TRY, args)?;
758 let dict = raw.as_ref();
759 let agent_id = require_int(BUILTIN_LOCK_TRY, dict, "agent_id")? as AgentId;
760 let path = require_string(BUILTIN_LOCK_TRY, dict, "path")?;
761 let ttl = match dict.get("ttl_ms") {
762 None | Some(VmValue::Nil) => None,
763 Some(VmValue::Int(n)) => Some(*n),
764 Some(other) => {
765 return Err(HostlibError::InvalidParameter {
766 builtin: BUILTIN_LOCK_TRY,
767 param: "ttl_ms",
768 message: format!("expected integer, got {}", other.type_name()),
769 })
770 }
771 };
772 let now = now_unix_ms();
773 let mut guard = index.lock().expect("code_index mutex poisoned");
774 let state = ensure_state(BUILTIN_LOCK_TRY, &mut guard)?;
775 let granted = state.agents.try_lock(agent_id, &path, ttl, now);
776 if granted {
777 return Ok(build_dict([
778 ("locked", VmValue::Bool(true)),
779 ("holder", VmValue::Int(agent_id as i64)),
780 ]));
781 }
782 let holder = state.agents.lock_holder(&path, now);
783 Ok(build_dict([
784 ("locked", VmValue::Bool(false)),
785 (
786 "holder",
787 holder
788 .map(|id| VmValue::Int(id as i64))
789 .unwrap_or(VmValue::Nil),
790 ),
791 ]))
792}
793
794pub(super) fn run_lock_release(
795 index: &SharedIndex,
796 args: &[VmValue],
797) -> Result<VmValue, HostlibError> {
798 let raw = dict_arg(BUILTIN_LOCK_RELEASE, args)?;
799 let dict = raw.as_ref();
800 let agent_id = require_int(BUILTIN_LOCK_RELEASE, dict, "agent_id")? as AgentId;
801 let path = require_string(BUILTIN_LOCK_RELEASE, dict, "path")?;
802 let mut guard = index.lock().expect("code_index mutex poisoned");
803 let state = ensure_state(BUILTIN_LOCK_RELEASE, &mut guard)?;
804 state.agents.release_lock(agent_id, &path);
805 Ok(VmValue::Bool(true))
806}
807
808pub(super) fn run_status(index: &SharedIndex, _args: &[VmValue]) -> Result<VmValue, HostlibError> {
809 let guard = index.lock().expect("code_index mutex poisoned");
810 match guard.as_ref() {
811 Some(state) => Ok(build_dict([
812 ("file_count", VmValue::Int(state.files.len() as i64)),
813 (
814 "current_seq",
815 VmValue::Int(state.versions.current_seq as i64),
816 ),
817 ("last_indexed_at_ms", VmValue::Int(state.last_built_unix_ms)),
818 (
819 "git_head",
820 state
821 .git_head
822 .as_deref()
823 .map(str_value)
824 .unwrap_or(VmValue::Nil),
825 ),
826 (
827 "agents",
828 VmValue::List(Rc::new(
829 state
830 .agents
831 .agents()
832 .map(|info| {
833 build_dict([
834 ("id", VmValue::Int(info.id as i64)),
835 ("name", str_value(&info.name)),
836 (
837 "state",
838 str_value(match info.state {
839 super::agents::AgentState::Active => "active",
840 super::agents::AgentState::Crashed => "crashed",
841 super::agents::AgentState::Gone => "gone",
842 }),
843 ),
844 ("last_seen_ms", VmValue::Int(info.last_seen_ms)),
845 ("edit_count", VmValue::Int(info.edit_count as i64)),
846 ("lock_count", VmValue::Int(info.locked_paths.len() as i64)),
847 ])
848 })
849 .collect(),
850 )),
851 ),
852 ])),
853 None => Ok(build_dict([
854 ("file_count", VmValue::Int(0)),
855 ("current_seq", VmValue::Int(0)),
856 ("last_indexed_at_ms", VmValue::Int(0)),
857 ("git_head", VmValue::Nil),
858 ("agents", VmValue::List(Rc::new(Vec::new()))),
859 ])),
860 }
861}
862
863pub(super) fn run_current_agent_id(
864 slot: &Arc<Mutex<Option<AgentId>>>,
865 _args: &[VmValue],
866) -> Result<VmValue, HostlibError> {
867 let guard = slot.lock().expect("current_agent slot poisoned");
868 Ok(match *guard {
869 Some(id) => VmValue::Int(id as i64),
870 None => VmValue::Nil,
871 })
872}
873
874fn ensure_state<'a>(
877 builtin: &'static str,
878 guard: &'a mut std::sync::MutexGuard<'_, Option<IndexState>>,
879) -> Result<&'a mut IndexState, HostlibError> {
880 if guard.is_none() {
881 return Err(HostlibError::Backend {
882 builtin,
883 message: "code index has not been initialised — call \
884 `hostlib_code_index_rebuild` or restore from a snapshot first"
885 .to_string(),
886 });
887 }
888 Ok(guard.as_mut().unwrap())
889}
890
891fn parse_hash(
892 builtin: &'static str,
893 dict: &BTreeMap<String, VmValue>,
894 key: &'static str,
895) -> Result<u64, HostlibError> {
896 match dict.get(key) {
897 None | Some(VmValue::Nil) => Ok(0),
898 Some(VmValue::Int(n)) => Ok(*n as u64),
899 Some(VmValue::String(s)) => s
900 .parse::<u64>()
901 .map_err(|_| HostlibError::InvalidParameter {
902 builtin,
903 param: key,
904 message: format!("expected u64-parseable string, got {s:?}"),
905 }),
906 Some(other) => Err(HostlibError::InvalidParameter {
907 builtin,
908 param: key,
909 message: format!(
910 "expected integer or numeric string, got {}",
911 other.type_name()
912 ),
913 }),
914 }
915}
916
917fn normalize_relative_path(state: &IndexState, path: &str) -> String {
918 if let Some(rel) = state
919 .lookup_path(path)
920 .and_then(|id| state.files.get(&id))
921 .map(|f| f.relative_path.clone())
922 {
923 return rel;
924 }
925 let p = std::path::Path::new(path);
926 if p.is_absolute() {
927 if let Ok(rel) = p.strip_prefix(&state.root) {
928 return rel.to_string_lossy().replace('\\', "/");
929 }
930 }
931 path.to_string()
932}
933
934fn candidates_for(state: &IndexState, needle: &str) -> Vec<FileId> {
935 if needle.len() >= 3 {
936 let trigrams = trigram::query_trigrams(needle);
937 return state.trigrams.query(&trigrams).into_iter().collect();
938 }
939 state.files.keys().copied().collect()
940}
941
942fn read_file_text(root: &std::path::Path, relative: &str) -> Option<String> {
943 std::fs::read_to_string(root.join(relative)).ok()
944}
945
946fn count_matches(haystack: &str, needle: &str, case_sensitive: bool) -> u64 {
947 if case_sensitive {
948 haystack.matches(needle).count() as u64
949 } else {
950 let lower_h = haystack.to_lowercase();
951 let lower_n = needle.to_lowercase();
952 lower_h.matches(&lower_n).count() as u64
953 }
954}
955
956fn scope_allows(scope: &[String], relative: &str) -> bool {
957 if scope.is_empty() {
958 return true;
959 }
960 scope
961 .iter()
962 .any(|s| relative == s || relative.starts_with(&format!("{s}/")) || s.is_empty())
963}
964
965struct Hit {
966 path: String,
967 score: f64,
968 match_count: u64,
969}
970
971fn hit_to_value(hit: Hit) -> VmValue {
972 let Hit {
973 path,
974 score,
975 match_count,
976 } = hit;
977 build_dict([
978 ("path", str_value(&path)),
979 ("score", VmValue::Float(score)),
980 ("match_count", VmValue::Int(match_count as i64)),
981 ])
982}
983
984fn import_entry(module: &str, resolved: Option<&str>, kind: &str) -> VmValue {
985 let mut map: BTreeMap<String, VmValue> = BTreeMap::new();
986 map.insert("module".into(), str_value(module));
987 map.insert(
988 "resolved_path".into(),
989 match resolved {
990 Some(p) => str_value(p),
991 None => VmValue::Nil,
992 },
993 );
994 map.insert("kind".into(), str_value(kind));
995 VmValue::Dict(Rc::new(map))
996}
997
998fn empty_query_response() -> VmValue {
999 build_dict([
1000 ("results", VmValue::List(Rc::new(Vec::new()))),
1001 ("truncated", VmValue::Bool(false)),
1002 ])
1003}
1004
1005fn empty_stats_response() -> VmValue {
1006 build_dict([
1007 ("indexed_files", VmValue::Int(0)),
1008 ("trigrams", VmValue::Int(0)),
1009 ("words", VmValue::Int(0)),
1010 ("memory_bytes", VmValue::Int(0)),
1011 ("last_rebuild_unix_ms", VmValue::Nil),
1012 ])
1013}
1014
1015fn empty_imports_response(path: &str) -> VmValue {
1016 build_dict([
1017 ("path", str_value(path)),
1018 ("imports", VmValue::List(Rc::new(Vec::new()))),
1019 ])
1020}
1021
1022fn empty_importers_response(module: &str) -> VmValue {
1023 build_dict([
1024 ("module", str_value(module)),
1025 ("importers", VmValue::List(Rc::new(Vec::new()))),
1026 ])
1027}