Skip to main content

code_kb_core/
lib.rs

1pub mod db;
2pub mod edit;
3pub mod formatters;
4pub mod models;
5pub mod ops;
6pub mod queries;
7pub mod slicer;
8pub mod sync;
9pub mod syntax;
10pub mod telemetry;
11pub mod watcher;
12pub mod workspace;
13
14pub use db::{
15    Connection, DbError, ensure_fts_index, ensure_fts_index_path, open_read_only, open_read_write,
16};
17pub use edit::{EditError, EditResult, replace_symbol_body};
18pub use formatters::{
19    format_blast_radius, format_context_slice, format_fact_categories, format_file_skeleton,
20    format_find_symbol_results, format_references, format_replace_symbol_result,
21    format_search_results, format_structural_facts, format_symbol_body,
22};
23pub use models::{
24    BlastRadiusResult, ContextSlice, FileFact, ImpactedSymbol, LiteralFact, ReferenceSite,
25    StructuralFact, Symbol, SymbolSearchResult, TestTarget, TypeFact,
26};
27pub use ops::{
28    OpError, blast_radius_op, codebase_outline_op, file_skeleton_op, get_context_slice_op,
29    get_symbol_body_op,
30};
31pub use queries::{
32    QueryError, compute_blast_radius, compute_blast_radius_scoped, find_callee_signatures,
33    find_literals, find_literals_scoped, find_references, find_references_ext,
34    find_references_for_symbol, find_references_scoped, find_related_tests, find_structural_facts,
35    find_structural_facts_scoped, find_type_facts, fts_search_symbols_scoped, get_file,
36    get_symbol_by_name, get_symbol_by_name_exact, is_test_path, list_structural_fact_categories,
37    list_structural_fact_categories_scoped, load_file_symbols, load_scoped_outline_symbols,
38    normalize_kind, sanitize_fts5_query, search_symbols, search_symbols_scoped,
39};
40pub use slicer::{SliceError, slice_symbol, slice_symbol_body};
41pub use sync::{
42    PINNED_JULIE_VERSION, ReconcileReport, SyncError, create_index, delete_file, ensure_fresh_file,
43    ensure_index_matches_extractor, find_julie_extract_binary, installed_extractor_version,
44    reconcile_offline_edits, scan_workspace, update_file,
45};
46pub use syntax::{SyntaxError, validate_syntax};
47pub use telemetry::{
48    BugReportBundle, TelemetryErrorRecord, TelemetryFilter, TelemetrySummary, TimeWindow,
49    ToolInvocation, ToolStat, format_telemetry_summary, generate_bug_report,
50    get_global_telemetry_dir, get_telemetry_summary, open_global_telemetry_db, open_telemetry_db,
51    record_tool_call, record_tool_call_conn,
52};
53pub use watcher::{WatcherError, WatcherHandle, start_watcher};
54pub use workspace::{
55    Workspace, WorkspaceError, is_hard_excluded, is_project_root, normalize_path, parse_file_uri,
56    strip_prefix_lossy, to_forward_slash,
57};
58
59/// Creates a temporary directory in a safe location, prioritizing `CARGO_TARGET_TMPDIR`,
60/// `TMPDIR`, and workspace `./target/tmp` over `/tmp` to avoid tmpfs quota limits.
61pub fn safe_tempdir() -> tempfile::TempDir {
62    if let Ok(target_tmp) = std::env::var("CARGO_TARGET_TMPDIR") {
63        let path = std::path::PathBuf::from(&target_tmp);
64        if (path.exists() || std::fs::create_dir_all(&path).is_ok())
65            && let Ok(dir) = tempfile::TempDir::new_in(&path)
66        {
67            return dir;
68        }
69    }
70    if let Ok(tmp) = std::env::var("TMPDIR") {
71        let path = std::path::PathBuf::from(tmp);
72        if (path.exists() || std::fs::create_dir_all(&path).is_ok())
73            && let Ok(dir) = tempfile::TempDir::new_in(&path)
74        {
75            return dir;
76        }
77    }
78
79    // Search ancestors of CARGO_MANIFEST_DIR or current_dir for workspace target/tmp
80    let mut search_dirs = Vec::new();
81    if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
82        search_dirs.push(std::path::PathBuf::from(manifest_dir));
83    }
84    if let Ok(cwd) = std::env::current_dir() {
85        search_dirs.push(cwd);
86    }
87    for base in search_dirs {
88        let mut cur = base;
89        loop {
90            let candidate = cur.join("target/tmp");
91            if (cur.join("Cargo.toml").exists() || cur.join(".git").exists())
92                && (candidate.exists() || std::fs::create_dir_all(&candidate).is_ok())
93                && let Ok(dir) = tempfile::TempDir::new_in(&candidate)
94            {
95                return dir;
96            }
97            if !cur.pop() {
98                break;
99            }
100        }
101    }
102
103    tempfile::tempdir().expect("failed to create temporary directory")
104}