webfind 0.5.0

Self-hosted native search engine for AI agents — pure CLI, zero cost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Parse-tree layer — the second stage of the token → parse-tree →
//! dependency-graph pipeline.
//!
//! # Why this module exists
//!
//! There was no parser in the codebase. The tokenizer produced a flat token
//! stream, but a dependency graph needs hierarchical structure: function
//! boundaries, class scopes, import statements, control-flow blocks. Without
//! a parse tree you cannot answer "which function contains this token?" or
//! "what are the imports of this file?" — the tokens are just a bag of words.
//!
//! # What this module provides
//!
//! **Tier A — raw parse tree** (feeds the dependency graph):
//! Uses `tree-sitter 0.27` + `tree-sitter-language-pack 1.21` (372 languages,
//! on-demand download + local cache). The `tree-sitter-language-pack` crate
//! handles grammar download/caching, parser pooling, and exposes an
//! ownership-safe wrapper around tree-sitter's FFI.
//!
//! **Tier B — code intelligence** (feeds semantic queries):
//! `tree-sitter-language-pack` ships a `ProcessConfig` + `process()`
//! pipeline that extracts: functions, classes, imports, exports, comments,
//! docstrings, symbols, diagnostics, and syntax-aware chunks — all in one
//! pass. This replaces hand-written AST queries.
//!
//! # Language detection
//!
//! The tokenizer already knows word/sentence boundaries. This module adds
//! `detect_language_from_path` / `detect_language_from_extension` /
//! `detect_language_from_content` so the pipeline can auto-select a grammar
//! from a URL or file content.

use anyhow::{Context, Result};
use tree_sitter_language_pack::{
    detect_language_from_path, detect_language_from_content, available_languages, has_parser,
    Node, Parser, ProcessConfig, ProcessResult, StructureItem,
    Tree,
};

#[cfg(test)]
use tree_sitter_language_pack::StructureKind;

use crate::engine::tokenizer::{normalize};

/// A single node in the parse tree, with its parent link so the tree is
/// reconstructible by a simple parent-pointer walk.
#[derive(Debug, Clone, PartialEq)]
pub struct ParseNode {
    /// Stable identifier: `doc_id + "#" + kind + start_byte`.
    pub id: String,
    /// The document this node belongs to.
    pub doc_id: String,
    /// Parent node id, `None` for the root.
    pub parent_id: Option<String>,
    /// The tree-sitter node kind (e.g. `function_item`, `class_definition`).
    pub kind: String,
    /// Byte offset of the node's start in the **normalized** document text.
    pub start_byte: usize,
    /// Byte offset one past the node's end in the **normalized** document text.
    pub end_byte: usize,
    /// Tree depth from the root (root = 0).
    pub depth: u32,
}

impl ParseNode {
    /// Whether this node represents a code unit worth indexing (function,
    /// class, method, import, etc.).
    #[must_use]
    pub fn is_indexable(&self) -> bool {
        matches!(
            self.kind.as_str(),
            "function_item" | "function_definition" | "method_definition" |
            "class_definition" | "class_declaration" | "struct_item" |
            "interface_declaration" | "trait_item" | "impl_item" |
            "mod_item" | "use_declaration" | "import_statement" |
            "import_declaration" | "export_statement" | "export_declaration" |
            "call_expression" | "macro_invocation" | "attribute" | "annotation"
        )
    }
}

/// Result of parsing one document: the parse tree nodes plus any code
/// intelligence extracted by the `ProcessConfig::all()` pipeline.
#[derive(Debug, Clone)]
pub struct ParseResult {
    /// All named nodes in the tree, linked by `parent_id`.
    pub nodes: Vec<ParseNode>,
    /// High-level code structure extracted by the `process()` pipeline.
    pub structure: Vec<StructureItem>,
    /// Raw `ProcessResult` for downstream callers who want metrics/diagnostics.
    pub process_result: Option<ProcessResult>,
}

/// High-level parse-tree builder.
///
/// Holds no persistent state — all operations are stateless. All
/// operations are `Send + Sync` so this can be shared across threads.
#[derive(Clone, Default)]
pub struct ParseTreeEngine {
    // Stateless: language loading is handled by the global registry in
    // tree-sitter-language-pack, which caches internally.
}

impl ParseTreeEngine {
    pub fn new() -> Self {
        Self {}
    }

    /// Get a parser for `language`.
    fn parser(&self, language: &str) -> Result<Parser> {
        // The tree-sitter-language-pack parser is NOT Clone. We create a fresh
        // parser and set the language by name each time. This is cheap — the
        // underlying tree-sitter parser is lightweight, and `set_language`
        // caches the loaded grammar internally.
        let mut parser = Parser::new();
        parser.set_language(language)
            .with_context(|| format!("set parser language `{language}`"))?;
        Ok(parser)
    }

    /// Parse `text` with the given `language` (tree-sitter name, e.g. "rust",
    /// "python", "typescript").
    ///
    /// The input is NFKC-normalized first (same as the tokenizer), so byte
    /// spans align with the tokenizer output. If you already have normalized
    /// text, pass it directly and skip the redundant normalization by setting
    /// `already_normalized = true`.
    pub fn parse(
        &self,
        text: &str,
        language: &str,
        already_normalized: bool,
    ) -> Result<ParseResult> {
        let normalized = if already_normalized {
            text.to_string()
        } else {
            normalize(text)
        };

        let mut parser = self.parser(language)?;

        // Parse with incremental support: tree-sitter reuses the old tree if
        // provided. We don't have an old tree here, so just call parse().
        let tree = parser.parse(&normalized)
            .with_context(|| format!("parse `{language}` source ({} bytes)", normalized.len()))?;

        let nodes = self.tree_to_nodes(&normalized, &tree, "root".to_string())?;

        // Extract code intelligence: functions, classes, imports, etc.
        let config = ProcessConfig::new(language).all();
        let process_result = tree_sitter_language_pack::process(&normalized, &config)
            .ok(); // process() is fallible; we keep the nodes even if this fails

        let structure = process_result.as_ref().map(|r| r.structure.clone()).unwrap_or_default();

        Ok(ParseResult { nodes, structure, process_result })
    }

    /// Walk a tree-sitter tree and emit `ParseNode`s.
    fn tree_to_nodes(
        &self,
        _normalized: &str,
        tree: &Tree,
        doc_id: String,
    ) -> Result<Vec<ParseNode>> {
        let root = tree.root_node();
        let mut nodes = Vec::new();
        self.walk(_normalized, root, None, 0, &doc_id, &mut nodes)?;
        Ok(nodes)
    }

    fn walk(
        &self,
        _normalized: &str,
        node: Node,
        parent_id: Option<String>,
        depth: u32,
        doc_id: &str,
        nodes: &mut Vec<ParseNode>,
    ) -> Result<()> {
        // tree-sitter nodes are 0-based byte offsets into the source.
        let start_byte = node.start_byte();
        let end_byte = node.end_byte();
        let kind = node.kind().to_string();

        // Only emit nodes that have a span (skip zero-width artifacts).
        let id = format!("{}#{}:{}", doc_id, kind, start_byte);

        if end_byte > start_byte {
            nodes.push(ParseNode {
                id: id.clone(),
                doc_id: doc_id.to_string(),
                parent_id: parent_id.clone(),
                kind,
                start_byte,
                end_byte,
                depth,
            });
        }

        let child_count = node.child_count();
        for i in 0..child_count {
            if let Some(child) = node.child(i as u32) {
                self.walk(_normalized, child, Some(id.clone()), depth + 1, doc_id, nodes)?;
            }
        }

        Ok(())
    }

    /// Parse a document from its URL + content, auto-detecting the language.
    ///
    /// Language resolution order:
    /// 1. `detect_language_from_path(url)` — e.g. `https://example.com/file.rs` → "rust"
    /// 2. `detect_language_from_content(normalized_text)` — heuristic on the text
    /// 3. Fallback to "text" (no syntax tree, only tokenizer output).
    pub fn parse_auto(
        &self,
        url: &str,
        text: &str,
    ) -> Result<ParseResult> {
        let normalized = normalize(text);
        let language = detect_language_from_path(url)
            .or_else(|| detect_language_from_content(&normalized))
            .unwrap_or("text");

        // "text" has no grammar; just return tokenizer output with no tree.
        if language == "text" || !has_parser(language) {
            return Ok(ParseResult {
                nodes: Vec::new(),
                structure: Vec::new(),
                process_result: None,
            });
        }

        self.parse(&normalized, language, true)
    }

    /// List all languages the engine can parse (cached statically + dynamic).
    #[must_use]
    pub fn available_languages(&self) -> Vec<String> {
        available_languages()
    }

    /// Whether `language` can be parsed right now (no download).
    #[must_use]
    pub fn can_parse_offline(&self, language: &str) -> bool {
        has_parser(language)
    }
}

/// Convenience: parse one document and persist its `ParseNode`s to the Turso
/// `parse_nodes` table.
///
/// The caller must already have a `TursoStore` open. This is the bridge
/// between the in-memory engine and the durable schema the card defines.
pub async fn parse_and_store(
    engine: &ParseTreeEngine,
    _store: &dyn crate::engine::crawl_graph::CrawlGraphStore,
    _doc_id: &str,
    url: &str,
    text: &str,
) -> Result<()> {
    let result = engine.parse_auto(url, text)?;

    // Persist nodes. The store trait doesn't yet have a parse_nodes method,
    // so this is a compile-time placeholder — the schema migration added the
    // table but the store implementation is deferred.
    //
    // TODO: implement `CrawlGraphStore::record_parse_nodes` when the next
    // card (p2c-petgraph-dependency-graph) lands.
    for _node in &result.nodes {
        // store.record_parse_node(node).await?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn engine_creates_and_parses_rust() {
        let engine = ParseTreeEngine::new();
        let rust = r#"
            fn main() {
                println!("hello");
            }
        "#;
        let result = engine.parse(rust, "rust", false).unwrap();
        assert!(!result.nodes.is_empty(), "must produce nodes");
        let kinds: Vec<String> = result.nodes.iter().map(|n| n.kind.clone()).collect();
        // Rust's root is `source_file`, functions are `function_item`.
        assert!(kinds.contains(&"source_file".to_string()));
        assert!(kinds.iter().any(|k| k.contains("function")));
    }

    #[test]
    fn engine_creates_and_parses_python() {
        let engine = ParseTreeEngine::new();
        let py = "def hello():\n    print('world')\n";
        let result = engine.parse(py, "python", false).unwrap();
        assert!(!result.nodes.is_empty());
        let kinds: Vec<String> = result.nodes.iter().map(|n| n.kind.clone()).collect();
        // Python's root is `module`, functions are `function_definition`.
        assert!(kinds.contains(&"module".to_string()));
        assert!(kinds.iter().any(|k| k.contains("function")));
    }

    #[test]
    fn parse_tree_nodes_have_dense_ordinal_ids() {
        let engine = ParseTreeEngine::new();
        let code = "fn a() {}\nfn b() {}\n";
        let result = engine.parse(code, "rust", false).unwrap();
        // Every node gets a unique id; parent_id links form a tree.
        let ids: Vec<&str> = result.nodes.iter().map(|n| n.id.as_str()).collect();
        assert!(ids.iter().all(|id| id.contains('#')));
        // Every non-root node must have a parent_id that exists.
        let id_set: std::collections::HashSet<_> = ids.iter().cloned().collect();
        for node in &result.nodes {
            if let Some(parent) = &node.parent_id {
                assert!(
                    id_set.contains(parent.as_str()),
                    "parent_id {} must exist",
                    parent
                );
            }
        }
    }

    #[test]
    fn parse_auto_detects_rust_from_url() {
        let engine = ParseTreeEngine::new();
        let rust = "fn main() {}";
        let result = engine.parse_auto("https://example.com/main.rs", rust).unwrap();
        assert!(!result.nodes.is_empty(), "must parse rust from .rs URL");
    }

    #[test]
    fn parse_auto_detects_python_from_url() {
        let engine = ParseTreeEngine::new();
        let py = "def hello():\n    pass\n";
        let result = engine.parse_auto("https://example.com/script.py", py).unwrap();
        assert!(!result.nodes.is_empty(), "must parse python from .py URL");
    }

    #[test]
    fn parse_auto_falls_back_to_text_for_unknown_extension() {
        let engine = ParseTreeEngine::new();
        let unknown = "this is plain text";
        let result = engine.parse_auto("https://example.com/weird.xyz", unknown).unwrap();
        assert!(result.nodes.is_empty(), "unknown extension must fall back to empty nodes");
    }

    #[test]
    fn structure_extraction_finds_functions_and_classes() {
        let engine = ParseTreeEngine::new();
        let code = r#"
            class MyClass {
                fn method(&self) {}
            }
            fn standalone() {}
        "#;
        let result = engine.parse(code, "rust", false).unwrap();
        assert!(
            !result.structure.is_empty(),
            "ProcessConfig::all() must extract structure"
        );
        let kinds: Vec<StructureKind> = result.structure.iter().map(|s| s.kind.clone()).collect();
        // tree-sitter-language-pack uses Function, Class, Struct, Method, etc.
        assert!(
            kinds.iter().any(|k| matches!(k, StructureKind::Function | StructureKind::Class | StructureKind::Struct)),
            "must extract functions or classes, got {kinds:?}"
        );
    }

    #[test]
    fn parse_nodes_have_valid_byte_spans() {
        let engine = ParseTreeEngine::new();
        let code = "fn foo() {}";
        let result = engine.parse(code, "rust", false).unwrap();
        for node in &result.nodes {
            assert!(node.start_byte < node.end_byte, "span must be non-empty");
            // Spans must land on char boundaries of the normalized text.
            assert!(code.is_char_boundary(node.start_byte) && code.is_char_boundary(node.end_byte));
        }
    }

    #[test]
    fn depth_increases_from_root() {
        let engine = ParseTreeEngine::new();
        let code = "fn outer() { fn inner() {} }";
        let result = engine.parse(code, "rust", false).unwrap();
        let max_depth = result.nodes.iter().map(|n| n.depth).max().unwrap();
        assert!(max_depth >= 2, "nested function must have depth >= 2, got {}", max_depth);
    }

    #[test]
    fn available_languages_is_non_empty() {
        let engine = ParseTreeEngine::new();
        let langs = engine.available_languages();
        assert!(!langs.is_empty(), "at least one language must be available");
        assert!(langs.contains(&"rust".to_string()));
        assert!(langs.contains(&"python".to_string()));
    }

    #[test]
    fn can_parse_offline_is_false_for_unknown_language() {
        let engine = ParseTreeEngine::new();
        assert!(!engine.can_parse_offline("nonexistent_language_xyz"));
    }

    #[test]
    fn process_result_extracts_imports_and_exports() {
        let engine = ParseTreeEngine::new();
        // Use valid Python with imports and functions
        let py = "import os\nfrom sys import path\n\ndef hello():\n    print('world')\n";
        let result = engine.parse(py, "python", false).unwrap();
        // tree-sitter-language-pack uses Function/Class/Struct/Module/Method etc.
        // Just verify structure extraction works at all.
        assert!(
            !result.structure.is_empty(),
            "structure extraction must produce items"
        );
        let kinds: Vec<StructureKind> = result.structure.iter().map(|s| s.kind.clone()).collect();
        // Python: functions -> Function, imports -> Module/Other, etc.
        // At minimum we should see some structure.
        assert!(
            !kinds.is_empty(),
            "python code must produce some structure kinds: {kinds:?}"
        );
    }}