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 abs = state.absolute_path(&path);
385 match std::fs::read(&abs) {
386 Ok(bytes) => Ok(str_value(fnv1a64(&bytes).to_string())),
387 Err(_) => Ok(VmValue::Nil),
388 }
389}
390
391pub(super) fn run_read_range(
394 index: &SharedIndex,
395 args: &[VmValue],
396) -> Result<VmValue, HostlibError> {
397 let raw = dict_arg(BUILTIN_READ_RANGE, args)?;
398 let dict = raw.as_ref();
399 let path = require_string(BUILTIN_READ_RANGE, dict, "path")?;
400 let start = match dict.get("start") {
401 None | Some(VmValue::Nil) => None,
402 Some(VmValue::Int(n)) => Some(*n),
403 Some(other) => {
404 return Err(HostlibError::InvalidParameter {
405 builtin: BUILTIN_READ_RANGE,
406 param: "start",
407 message: format!("expected integer, got {}", other.type_name()),
408 });
409 }
410 };
411 let end = match dict.get("end") {
412 None | Some(VmValue::Nil) => None,
413 Some(VmValue::Int(n)) => Some(*n),
414 Some(other) => {
415 return Err(HostlibError::InvalidParameter {
416 builtin: BUILTIN_READ_RANGE,
417 param: "end",
418 message: format!("expected integer, got {}", other.type_name()),
419 });
420 }
421 };
422 let guard = index.lock().expect("code_index mutex poisoned");
423 let abs = match guard.as_ref() {
424 Some(state) => state.absolute_path(&path),
425 None => PathBuf::from(&path),
426 };
427 drop(guard);
428
429 let content = match std::fs::read_to_string(&abs) {
430 Ok(s) => s,
431 Err(_) => {
432 return Err(HostlibError::Backend {
433 builtin: BUILTIN_READ_RANGE,
434 message: format!("file not found: {path}"),
435 })
436 }
437 };
438
439 if start.is_none() && end.is_none() {
440 return Ok(build_dict([("content", str_value(&content))]));
441 }
442 let lines: Vec<&str> = content.split('\n').collect();
443 let total = lines.len() as i64;
444 let lo = (start.unwrap_or(1) - 1).max(0) as usize;
445 let hi = end.unwrap_or(total).min(total).max(0) as usize;
446 if lo >= hi {
447 return Ok(build_dict([
448 ("content", str_value("")),
449 ("start", VmValue::Int((lo as i64) + 1)),
450 ("end", VmValue::Int(hi as i64)),
451 ]));
452 }
453 let slice = lines[lo..hi].join("\n");
454 Ok(build_dict([
455 ("content", str_value(&slice)),
456 ("start", VmValue::Int((lo as i64) + 1)),
457 ("end", VmValue::Int(hi as i64)),
458 ]))
459}
460
461pub(super) fn run_reindex_file(
462 index: &SharedIndex,
463 args: &[VmValue],
464) -> Result<VmValue, HostlibError> {
465 let raw = dict_arg(BUILTIN_REINDEX_FILE, args)?;
466 let path = require_string(BUILTIN_REINDEX_FILE, raw.as_ref(), "path")?;
467 let mut guard = index.lock().expect("code_index mutex poisoned");
468 let Some(state) = guard.as_mut() else {
469 return Ok(build_dict([
470 ("indexed", VmValue::Bool(false)),
471 ("file_id", VmValue::Nil),
472 ]));
473 };
474 let abs = state.absolute_path(&path);
475 let id = state.reindex_file(&abs);
476 Ok(build_dict([
477 ("indexed", VmValue::Bool(id.is_some())),
478 (
479 "file_id",
480 id.map(|i| VmValue::Int(i as i64)).unwrap_or(VmValue::Nil),
481 ),
482 ]))
483}
484
485pub(super) fn run_trigram_query(
486 index: &SharedIndex,
487 args: &[VmValue],
488) -> Result<VmValue, HostlibError> {
489 let raw = dict_arg(BUILTIN_TRIGRAM_QUERY, args)?;
490 let dict = raw.as_ref();
491 let trigrams_raw = optional_int_list(BUILTIN_TRIGRAM_QUERY, dict, "trigrams")?;
492 let max_files = match dict.get("max_files") {
493 None | Some(VmValue::Nil) => None,
494 Some(VmValue::Int(n)) => Some(*n as usize),
495 Some(other) => {
496 return Err(HostlibError::InvalidParameter {
497 builtin: BUILTIN_TRIGRAM_QUERY,
498 param: "max_files",
499 message: format!("expected integer, got {}", other.type_name()),
500 })
501 }
502 };
503 let trigrams: Vec<u32> = trigrams_raw.into_iter().map(|n| n as u32).collect();
504 let guard = index.lock().expect("code_index mutex poisoned");
505 let mut ids: Vec<FileId> = match guard.as_ref() {
506 Some(state) => state.trigrams.query(&trigrams).into_iter().collect(),
507 None => Vec::new(),
508 };
509 ids.sort_unstable();
510 if let Some(limit) = max_files {
511 ids.truncate(limit);
512 }
513 Ok(VmValue::List(Rc::new(
514 ids.into_iter().map(|id| VmValue::Int(id as i64)).collect(),
515 )))
516}
517
518pub(super) fn run_extract_trigrams(
519 _index: &SharedIndex,
520 args: &[VmValue],
521) -> Result<VmValue, HostlibError> {
522 let raw = dict_arg(BUILTIN_EXTRACT_TRIGRAMS, args)?;
523 let query = require_string(BUILTIN_EXTRACT_TRIGRAMS, raw.as_ref(), "query")?;
524 let mut tgs = trigram::query_trigrams(&query);
525 tgs.sort_unstable();
526 Ok(VmValue::List(Rc::new(
527 tgs.into_iter().map(|n| VmValue::Int(n as i64)).collect(),
528 )))
529}
530
531pub(super) fn run_word_get(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
532 let raw = dict_arg(BUILTIN_WORD_GET, args)?;
533 let word = require_string(BUILTIN_WORD_GET, raw.as_ref(), "word")?;
534 let guard = index.lock().expect("code_index mutex poisoned");
535 let hits: Vec<VmValue> = match guard.as_ref() {
536 Some(state) => state
537 .words
538 .get(&word)
539 .iter()
540 .map(|h| {
541 build_dict([
542 ("file_id", VmValue::Int(h.file as i64)),
543 ("line", VmValue::Int(h.line as i64)),
544 ])
545 })
546 .collect(),
547 None => Vec::new(),
548 };
549 Ok(VmValue::List(Rc::new(hits)))
550}
551
552pub(super) fn run_deps_get(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
553 let raw = dict_arg(BUILTIN_DEPS_GET, args)?;
554 let dict = raw.as_ref();
555 let id = require_int(BUILTIN_DEPS_GET, dict, "file_id")? as FileId;
556 let direction = optional_string(BUILTIN_DEPS_GET, dict, "direction")?
557 .unwrap_or_else(|| "importers".to_string());
558 let guard = index.lock().expect("code_index mutex poisoned");
559 let mut neighbors: Vec<FileId> = match guard.as_ref() {
560 Some(state) => match direction.as_str() {
561 "importers" => state.deps.importers_of(id),
562 "imports" => state.deps.imports_of(id),
563 _ => {
564 return Err(HostlibError::InvalidParameter {
565 builtin: BUILTIN_DEPS_GET,
566 param: "direction",
567 message: format!("expected \"importers\" or \"imports\", got {direction:?}"),
568 })
569 }
570 },
571 None => Vec::new(),
572 };
573 neighbors.sort_unstable();
574 Ok(VmValue::List(Rc::new(
575 neighbors
576 .into_iter()
577 .map(|id| VmValue::Int(id as i64))
578 .collect(),
579 )))
580}
581
582pub(super) fn run_outline_get(
583 index: &SharedIndex,
584 args: &[VmValue],
585) -> Result<VmValue, HostlibError> {
586 let raw = dict_arg(BUILTIN_OUTLINE_GET, args)?;
587 let id = require_int(BUILTIN_OUTLINE_GET, raw.as_ref(), "file_id")? as FileId;
588 let guard = index.lock().expect("code_index mutex poisoned");
589 let symbols: Vec<VmValue> = match guard.as_ref().and_then(|s| s.files.get(&id)) {
590 Some(file) => file
591 .symbols
592 .iter()
593 .map(|sym| {
594 build_dict([
595 ("name", str_value(&sym.name)),
596 ("kind", str_value(&sym.kind)),
597 ("start_line", VmValue::Int(sym.start_line as i64)),
598 ("end_line", VmValue::Int(sym.end_line as i64)),
599 ("signature", str_value(&sym.signature)),
600 ])
601 })
602 .collect(),
603 None => Vec::new(),
604 };
605 Ok(VmValue::List(Rc::new(symbols)))
606}
607
608pub(super) fn run_current_seq(
611 index: &SharedIndex,
612 _args: &[VmValue],
613) -> Result<VmValue, HostlibError> {
614 let guard = index.lock().expect("code_index mutex poisoned");
615 let seq = guard.as_ref().map(|s| s.versions.current_seq).unwrap_or(0);
616 Ok(VmValue::Int(seq as i64))
617}
618
619pub(super) fn run_changes_since(
620 index: &SharedIndex,
621 args: &[VmValue],
622) -> Result<VmValue, HostlibError> {
623 let raw = dict_arg(BUILTIN_CHANGES_SINCE, args)?;
624 let dict = raw.as_ref();
625 let seq = optional_int(BUILTIN_CHANGES_SINCE, dict, "seq", 0)?.max(0) as u64;
626 let limit = match dict.get("limit") {
627 None | Some(VmValue::Nil) => None,
628 Some(VmValue::Int(n)) => Some(*n as usize),
629 Some(other) => {
630 return Err(HostlibError::InvalidParameter {
631 builtin: BUILTIN_CHANGES_SINCE,
632 param: "limit",
633 message: format!("expected integer, got {}", other.type_name()),
634 })
635 }
636 };
637 let guard = index.lock().expect("code_index mutex poisoned");
638 let records = match guard.as_ref() {
639 Some(state) => state.versions.changes_since(seq, limit),
640 None => Vec::new(),
641 };
642 Ok(VmValue::List(Rc::new(
643 records
644 .into_iter()
645 .map(|r| {
646 build_dict([
647 ("path", str_value(&r.path)),
648 ("seq", VmValue::Int(r.seq as i64)),
649 ("agent_id", VmValue::Int(r.agent_id as i64)),
650 ("op", str_value(r.op.as_str())),
651 ("hash", str_value(r.hash.to_string())),
652 ("size", VmValue::Int(r.size as i64)),
653 ("timestamp_ms", VmValue::Int(r.timestamp_ms)),
654 ])
655 })
656 .collect(),
657 )))
658}
659
660pub(super) fn run_version_record(
661 index: &SharedIndex,
662 args: &[VmValue],
663) -> Result<VmValue, HostlibError> {
664 let raw = dict_arg(BUILTIN_VERSION_RECORD, args)?;
665 let dict = raw.as_ref();
666 let agent_id = require_int(BUILTIN_VERSION_RECORD, dict, "agent_id")? as AgentId;
667 let path = require_string(BUILTIN_VERSION_RECORD, dict, "path")?;
668 let op_str =
669 optional_string(BUILTIN_VERSION_RECORD, dict, "op")?.unwrap_or_else(|| "write".to_string());
670 let op = EditOp::parse(&op_str).unwrap_or(EditOp::Write);
671 let hash = parse_hash(BUILTIN_VERSION_RECORD, dict, "hash")?;
672 let size = optional_int(BUILTIN_VERSION_RECORD, dict, "size", 0)?.max(0) as u64;
673 let now = now_unix_ms();
674 let mut guard = index.lock().expect("code_index mutex poisoned");
675 let state = ensure_state(BUILTIN_VERSION_RECORD, &mut guard)?;
676 let normalized = normalize_relative_path(state, &path);
677 let seq = state
678 .versions
679 .record(normalized, agent_id, op, hash, size, now);
680 state.agents.note_edit(agent_id, now);
681 Ok(VmValue::Int(seq as i64))
682}
683
684pub(super) fn run_agent_register(
687 index: &SharedIndex,
688 args: &[VmValue],
689) -> Result<VmValue, HostlibError> {
690 let raw = dict_arg(BUILTIN_AGENT_REGISTER, args)?;
691 let dict = raw.as_ref();
692 let name = optional_string(BUILTIN_AGENT_REGISTER, dict, "name")?
693 .unwrap_or_else(|| "agent".to_string());
694 let requested_id = match dict.get("agent_id") {
695 None | Some(VmValue::Nil) => None,
696 Some(VmValue::Int(n)) => Some(*n as AgentId),
697 Some(other) => {
698 return Err(HostlibError::InvalidParameter {
699 builtin: BUILTIN_AGENT_REGISTER,
700 param: "agent_id",
701 message: format!("expected integer, got {}", other.type_name()),
702 })
703 }
704 };
705 let now = now_unix_ms();
706 let mut guard = index.lock().expect("code_index mutex poisoned");
707 let state = ensure_state(BUILTIN_AGENT_REGISTER, &mut guard)?;
708 let id = match requested_id {
709 Some(id) => state.agents.register_with_id(id, name, now),
710 None => state.agents.register(name, now),
711 };
712 Ok(VmValue::Int(id as i64))
713}
714
715pub(super) fn run_agent_heartbeat(
716 index: &SharedIndex,
717 args: &[VmValue],
718) -> Result<VmValue, HostlibError> {
719 let raw = dict_arg(BUILTIN_AGENT_HEARTBEAT, args)?;
720 let id = require_int(BUILTIN_AGENT_HEARTBEAT, raw.as_ref(), "agent_id")? as AgentId;
721 let now = now_unix_ms();
722 let mut guard = index.lock().expect("code_index mutex poisoned");
723 let state = ensure_state(BUILTIN_AGENT_HEARTBEAT, &mut guard)?;
724 state.agents.heartbeat(id, now);
725 Ok(VmValue::Bool(true))
726}
727
728pub(super) fn run_agent_unregister(
729 index: &SharedIndex,
730 args: &[VmValue],
731) -> Result<VmValue, HostlibError> {
732 let raw = dict_arg(BUILTIN_AGENT_UNREGISTER, args)?;
733 let id = require_int(BUILTIN_AGENT_UNREGISTER, raw.as_ref(), "agent_id")? as AgentId;
734 let mut guard = index.lock().expect("code_index mutex poisoned");
735 let state = ensure_state(BUILTIN_AGENT_UNREGISTER, &mut guard)?;
736 state.agents.unregister(id);
737 Ok(VmValue::Bool(true))
738}
739
740pub(super) fn run_lock_try(index: &SharedIndex, args: &[VmValue]) -> Result<VmValue, HostlibError> {
741 let raw = dict_arg(BUILTIN_LOCK_TRY, args)?;
742 let dict = raw.as_ref();
743 let agent_id = require_int(BUILTIN_LOCK_TRY, dict, "agent_id")? as AgentId;
744 let path = require_string(BUILTIN_LOCK_TRY, dict, "path")?;
745 let ttl = match dict.get("ttl_ms") {
746 None | Some(VmValue::Nil) => None,
747 Some(VmValue::Int(n)) => Some(*n),
748 Some(other) => {
749 return Err(HostlibError::InvalidParameter {
750 builtin: BUILTIN_LOCK_TRY,
751 param: "ttl_ms",
752 message: format!("expected integer, got {}", other.type_name()),
753 })
754 }
755 };
756 let now = now_unix_ms();
757 let mut guard = index.lock().expect("code_index mutex poisoned");
758 let state = ensure_state(BUILTIN_LOCK_TRY, &mut guard)?;
759 let granted = state.agents.try_lock(agent_id, &path, ttl, now);
760 if granted {
761 return Ok(build_dict([
762 ("locked", VmValue::Bool(true)),
763 ("holder", VmValue::Int(agent_id as i64)),
764 ]));
765 }
766 let holder = state.agents.lock_holder(&path, now);
767 Ok(build_dict([
768 ("locked", VmValue::Bool(false)),
769 (
770 "holder",
771 holder
772 .map(|id| VmValue::Int(id as i64))
773 .unwrap_or(VmValue::Nil),
774 ),
775 ]))
776}
777
778pub(super) fn run_lock_release(
779 index: &SharedIndex,
780 args: &[VmValue],
781) -> Result<VmValue, HostlibError> {
782 let raw = dict_arg(BUILTIN_LOCK_RELEASE, args)?;
783 let dict = raw.as_ref();
784 let agent_id = require_int(BUILTIN_LOCK_RELEASE, dict, "agent_id")? as AgentId;
785 let path = require_string(BUILTIN_LOCK_RELEASE, dict, "path")?;
786 let mut guard = index.lock().expect("code_index mutex poisoned");
787 let state = ensure_state(BUILTIN_LOCK_RELEASE, &mut guard)?;
788 state.agents.release_lock(agent_id, &path);
789 Ok(VmValue::Bool(true))
790}
791
792pub(super) fn run_status(index: &SharedIndex, _args: &[VmValue]) -> Result<VmValue, HostlibError> {
793 let guard = index.lock().expect("code_index mutex poisoned");
794 match guard.as_ref() {
795 Some(state) => Ok(build_dict([
796 ("file_count", VmValue::Int(state.files.len() as i64)),
797 (
798 "current_seq",
799 VmValue::Int(state.versions.current_seq as i64),
800 ),
801 ("last_indexed_at_ms", VmValue::Int(state.last_built_unix_ms)),
802 (
803 "git_head",
804 state
805 .git_head
806 .as_deref()
807 .map(str_value)
808 .unwrap_or(VmValue::Nil),
809 ),
810 (
811 "agents",
812 VmValue::List(Rc::new(
813 state
814 .agents
815 .agents()
816 .map(|info| {
817 build_dict([
818 ("id", VmValue::Int(info.id as i64)),
819 ("name", str_value(&info.name)),
820 (
821 "state",
822 str_value(match info.state {
823 super::agents::AgentState::Active => "active",
824 super::agents::AgentState::Crashed => "crashed",
825 super::agents::AgentState::Gone => "gone",
826 }),
827 ),
828 ("last_seen_ms", VmValue::Int(info.last_seen_ms)),
829 ("edit_count", VmValue::Int(info.edit_count as i64)),
830 ("lock_count", VmValue::Int(info.locked_paths.len() as i64)),
831 ])
832 })
833 .collect(),
834 )),
835 ),
836 ])),
837 None => Ok(build_dict([
838 ("file_count", VmValue::Int(0)),
839 ("current_seq", VmValue::Int(0)),
840 ("last_indexed_at_ms", VmValue::Int(0)),
841 ("git_head", VmValue::Nil),
842 ("agents", VmValue::List(Rc::new(Vec::new()))),
843 ])),
844 }
845}
846
847pub(super) fn run_current_agent_id(
848 slot: &Arc<Mutex<Option<AgentId>>>,
849 _args: &[VmValue],
850) -> Result<VmValue, HostlibError> {
851 let guard = slot.lock().expect("current_agent slot poisoned");
852 Ok(match *guard {
853 Some(id) => VmValue::Int(id as i64),
854 None => VmValue::Nil,
855 })
856}
857
858fn ensure_state<'a>(
861 builtin: &'static str,
862 guard: &'a mut std::sync::MutexGuard<'_, Option<IndexState>>,
863) -> Result<&'a mut IndexState, HostlibError> {
864 if guard.is_none() {
865 return Err(HostlibError::Backend {
866 builtin,
867 message: "code index has not been initialised — call \
868 `hostlib_code_index_rebuild` or restore from a snapshot first"
869 .to_string(),
870 });
871 }
872 Ok(guard.as_mut().unwrap())
873}
874
875fn parse_hash(
876 builtin: &'static str,
877 dict: &BTreeMap<String, VmValue>,
878 key: &'static str,
879) -> Result<u64, HostlibError> {
880 match dict.get(key) {
881 None | Some(VmValue::Nil) => Ok(0),
882 Some(VmValue::Int(n)) => Ok(*n as u64),
883 Some(VmValue::String(s)) => s
884 .parse::<u64>()
885 .map_err(|_| HostlibError::InvalidParameter {
886 builtin,
887 param: key,
888 message: format!("expected u64-parseable string, got {s:?}"),
889 }),
890 Some(other) => Err(HostlibError::InvalidParameter {
891 builtin,
892 param: key,
893 message: format!(
894 "expected integer or numeric string, got {}",
895 other.type_name()
896 ),
897 }),
898 }
899}
900
901fn normalize_relative_path(state: &IndexState, path: &str) -> String {
902 if let Some(rel) = state
903 .lookup_path(path)
904 .and_then(|id| state.files.get(&id))
905 .map(|f| f.relative_path.clone())
906 {
907 return rel;
908 }
909 let p = std::path::Path::new(path);
910 if p.is_absolute() {
911 if let Ok(rel) = p.strip_prefix(&state.root) {
912 return rel.to_string_lossy().replace('\\', "/");
913 }
914 }
915 path.to_string()
916}
917
918fn candidates_for(state: &IndexState, needle: &str) -> Vec<FileId> {
919 if needle.len() >= 3 {
920 let trigrams = trigram::query_trigrams(needle);
921 return state.trigrams.query(&trigrams).into_iter().collect();
922 }
923 state.files.keys().copied().collect()
924}
925
926fn read_file_text(root: &std::path::Path, relative: &str) -> Option<String> {
927 std::fs::read_to_string(root.join(relative)).ok()
928}
929
930fn count_matches(haystack: &str, needle: &str, case_sensitive: bool) -> u64 {
931 if case_sensitive {
932 haystack.matches(needle).count() as u64
933 } else {
934 let lower_h = haystack.to_lowercase();
935 let lower_n = needle.to_lowercase();
936 lower_h.matches(&lower_n).count() as u64
937 }
938}
939
940fn scope_allows(scope: &[String], relative: &str) -> bool {
941 if scope.is_empty() {
942 return true;
943 }
944 scope
945 .iter()
946 .any(|s| relative == s || relative.starts_with(&format!("{s}/")) || s.is_empty())
947}
948
949struct Hit {
950 path: String,
951 score: f64,
952 match_count: u64,
953}
954
955fn hit_to_value(hit: Hit) -> VmValue {
956 let Hit {
957 path,
958 score,
959 match_count,
960 } = hit;
961 build_dict([
962 ("path", str_value(&path)),
963 ("score", VmValue::Float(score)),
964 ("match_count", VmValue::Int(match_count as i64)),
965 ])
966}
967
968fn import_entry(module: &str, resolved: Option<&str>, kind: &str) -> VmValue {
969 let mut map: BTreeMap<String, VmValue> = BTreeMap::new();
970 map.insert("module".into(), str_value(module));
971 map.insert(
972 "resolved_path".into(),
973 match resolved {
974 Some(p) => str_value(p),
975 None => VmValue::Nil,
976 },
977 );
978 map.insert("kind".into(), str_value(kind));
979 VmValue::Dict(Rc::new(map))
980}
981
982fn empty_query_response() -> VmValue {
983 build_dict([
984 ("results", VmValue::List(Rc::new(Vec::new()))),
985 ("truncated", VmValue::Bool(false)),
986 ])
987}
988
989fn empty_stats_response() -> VmValue {
990 build_dict([
991 ("indexed_files", VmValue::Int(0)),
992 ("trigrams", VmValue::Int(0)),
993 ("words", VmValue::Int(0)),
994 ("memory_bytes", VmValue::Int(0)),
995 ("last_rebuild_unix_ms", VmValue::Nil),
996 ])
997}
998
999fn empty_imports_response(path: &str) -> VmValue {
1000 build_dict([
1001 ("path", str_value(path)),
1002 ("imports", VmValue::List(Rc::new(Vec::new()))),
1003 ])
1004}
1005
1006fn empty_importers_response(module: &str) -> VmValue {
1007 build_dict([
1008 ("module", str_value(module)),
1009 ("importers", VmValue::List(Rc::new(Vec::new()))),
1010 ])
1011}