agentstatedeveloper-core 1.4.8

Core types, traits, and ASG-backed implementations for AgentStateDeveloper
Documentation
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Document adapters: produce [`SearchDoc`] chunks from non-code files.
//!
//! Each adapter takes a file path + raw content and returns `Vec<SearchDoc>`.
//! Adapters are intentionally thin — no deep structural parsing, just enough
//! text extraction to make files discoverable in `asd search`.
//!
//! Supported file types:
//!   - Markdown (.md, .markdown) — each H1/H2/H3 heading becomes a chunk
//!   - Config (JSON, TOML, YAML) — key-path flattening for leaf values
//!   - HTML (.html, .htm) — strip tags, extract title + heading text
//!   - CSS (.css) — extract selector names and custom property names
//!   - Manifests (Package.swift, Cargo.toml, pubspec.yaml, package.json) —
//!     extract name + description + top-level keys
//!   - Build scripts (Makefile, Fastfile, Podfile, Gemfile) — extract target/lane names

use std::path::Path;

use crate::search_fts::{DocKind, SearchDoc};

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

/// Detect the kind for a file path and, if recognised, parse it into chunks.
/// Returns `None` when the file is not handled by any adapter.
pub fn adapt_document(path: &Path, content: &str) -> Option<Vec<SearchDoc>> {
    let name = path.file_name()?.to_string_lossy().to_lowercase();
    let ext = path
        .extension()
        .map(|e| e.to_string_lossy().to_lowercase())
        .unwrap_or_default();

    // Manifests — check by specific filename before extension.
    let is_manifest = matches!(
        name.as_str(),
        "package.swift"
            | "cargo.toml"
            | "pubspec.yaml"
            | "pubspec.yml"
            | "package.json"
            | "package-lock.json"
            | "podfile"
            | "gemfile"
            | "makefile"
            | "fastfile"
            | "appfile"
            | "deliverfile"
    );
    if is_manifest {
        let kind = if matches!(
            name.as_str(),
            "makefile" | "fastfile" | "podfile" | "gemfile"
        ) {
            DocKind::BuildScript
        } else {
            DocKind::Manifest
        };
        return Some(adapt_manifest(path, content, kind));
    }

    match ext.as_str() {
        "md" | "markdown" => Some(adapt_markdown(path, content)),
        "json" => Some(adapt_json(path, content)),
        "toml" => Some(adapt_toml(path, content)),
        "yaml" | "yml" => Some(adapt_yaml(path, content)),
        "html" | "htm" => Some(adapt_html(path, content)),
        "css" => Some(adapt_css(path, content)),
        _ => None,
    }
}

/// Returns true if this path should be offered to document adapters.
/// Used to quickly gate file walking before reading content.
pub fn is_doc_file(path: &Path) -> bool {
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_lowercase())
        .unwrap_or_default();
    let ext = path
        .extension()
        .map(|e| e.to_string_lossy().to_lowercase())
        .unwrap_or_default();

    matches!(
        name.as_str(),
        "package.swift"
            | "cargo.toml"
            | "pubspec.yaml"
            | "pubspec.yml"
            | "package.json"
            | "makefile"
            | "fastfile"
            | "podfile"
            | "gemfile"
    ) || matches!(
        ext.as_str(),
        "md" | "markdown" | "json" | "toml" | "yaml" | "yml" | "html" | "htm" | "css"
    )
}

// ---------------------------------------------------------------------------
// Markdown adapter
// ---------------------------------------------------------------------------

fn adapt_markdown(path: &Path, content: &str) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let mut docs: Vec<SearchDoc> = Vec::new();
    let mut current_title = String::new();
    let mut current_body: Vec<&str> = Vec::new();
    let mut current_line: u32 = 0;
    let mut chunk_start: u32 = 1;

    let flush = |title: &str, body: &[&str], start: u32, docs: &mut Vec<SearchDoc>| {
        let body_text = body.join(" ").trim().to_string();
        if !title.is_empty() || !body_text.is_empty() {
            docs.push(SearchDoc::new(
                DocKind::Markdown,
                path_str.as_ref(),
                Some(start),
                title,
                body_text,
            ));
        }
    };

    for line in content.lines() {
        current_line += 1;
        let trimmed = line.trim();

        if trimmed.starts_with("# ") || trimmed.starts_with("## ") || trimmed.starts_with("### ") {
            // Flush previous chunk.
            flush(&current_title, &current_body, chunk_start, &mut docs);
            current_title = trimmed.trim_start_matches('#').trim().to_string();
            current_body = Vec::new();
            chunk_start = current_line;
        } else if !trimmed.is_empty() && !trimmed.starts_with("```") {
            current_body.push(line);
        }
    }
    // Flush last chunk.
    flush(&current_title, &current_body, chunk_start, &mut docs);

    // If no headings found, emit a single whole-file chunk using the filename as title.
    if docs.is_empty() {
        let title = path
            .file_stem()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_default();
        docs.push(SearchDoc::new(
            DocKind::Markdown,
            path_str.as_ref(),
            None,
            title,
            content.chars().take(2000).collect::<String>(),
        ));
    }
    docs
}

// ---------------------------------------------------------------------------
// JSON adapter
// ---------------------------------------------------------------------------

fn adapt_json(path: &Path, content: &str) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    // For small JSON files (≤4KB) emit a single chunk with flattened key paths.
    let body = flatten_json_text(content, 3);
    if body.is_empty() {
        return vec![];
    }
    vec![SearchDoc::new(
        DocKind::Config,
        path_str.as_ref(),
        None,
        name,
        body,
    )]
}

/// Flatten up to `max_depth` levels of a JSON object into "key: value" lines.
/// Avoids serde_json entirely — simple line-based heuristic for discovery text.
fn flatten_json_text(content: &str, _max_depth: usize) -> String {
    // Strip string values, keep keys + short scalar values for searchability.
    content
        .lines()
        .map(|l| l.trim())
        .filter(|l| l.contains(':') || l.contains('"'))
        .take(100)
        .map(|l| l.trim_matches(|c: char| c == ',' || c == '{' || c == '}' || c == '[' || c == ']'))
        .filter(|l| !l.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
        .chars()
        .take(2000)
        .collect()
}

// ---------------------------------------------------------------------------
// TOML adapter
// ---------------------------------------------------------------------------

fn adapt_toml(path: &Path, content: &str) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    // Emit key = value lines as body; section headers ([section]) become subchunks.
    let mut docs: Vec<SearchDoc> = Vec::new();
    let mut current_section = name.clone();
    let mut body_lines: Vec<&str> = Vec::new();
    let mut chunk_start: u32 = 0;
    let mut line_no: u32 = 0;

    for line in content.lines() {
        line_no += 1;
        let trimmed = line.trim();
        if trimmed.starts_with('[') && trimmed.ends_with(']') {
            let section_body = body_lines.join(" ");
            if !section_body.trim().is_empty() {
                docs.push(SearchDoc::new(
                    DocKind::Manifest,
                    path_str.as_ref(),
                    Some(chunk_start),
                    &current_section,
                    section_body,
                ));
            }
            current_section = format!(
                "{} {}",
                name,
                trimmed.trim_matches(|c| c == '[' || c == ']')
            );
            body_lines = Vec::new();
            chunk_start = line_no;
        } else if !trimmed.is_empty() && !trimmed.starts_with('#') {
            body_lines.push(line);
        }
    }
    let section_body = body_lines.join(" ");
    if !section_body.trim().is_empty() {
        docs.push(SearchDoc::new(
            DocKind::Manifest,
            path_str.as_ref(),
            Some(chunk_start),
            &current_section,
            section_body,
        ));
    }
    if docs.is_empty() {
        docs.push(SearchDoc::new(
            DocKind::Config,
            path_str.as_ref(),
            None,
            name,
            content.chars().take(1000).collect::<String>(),
        ));
    }
    docs
}

// ---------------------------------------------------------------------------
// YAML adapter
// ---------------------------------------------------------------------------

fn adapt_yaml(path: &Path, content: &str) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    // Flatten top-level keys + values into a single searchable chunk.
    let body: String = content
        .lines()
        .filter(|l| !l.trim_start().starts_with('#') && !l.trim().is_empty())
        .take(80)
        .collect::<Vec<_>>()
        .join(" ")
        .chars()
        .take(2000)
        .collect();
    if body.is_empty() {
        return vec![];
    }
    vec![SearchDoc::new(
        DocKind::Config,
        path_str.as_ref(),
        None,
        name,
        body,
    )]
}

// ---------------------------------------------------------------------------
// HTML adapter
// ---------------------------------------------------------------------------

fn adapt_html(path: &Path, content: &str) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    // Strip HTML tags, collapse whitespace.
    let text = strip_html_tags(content);
    if text.trim().is_empty() {
        return vec![];
    }
    vec![SearchDoc::new(
        DocKind::Html,
        path_str.as_ref(),
        None,
        name,
        text.chars().take(2000).collect::<String>(),
    )]
}

fn strip_html_tags(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut in_tag = false;
    let mut in_script = false;
    let mut prev_space = true;
    let mut i = 0;
    let bytes = s.as_bytes();
    while i < bytes.len() {
        // Detect <script> blocks. Byte-wise ASCII comparison so a multi-byte
        // char (em-dash, arrow, …) elsewhere in the string can never panic a
        // string slice on a non-char boundary.
        if !in_tag && i + 7 <= bytes.len() && bytes[i..i + 7].eq_ignore_ascii_case(b"<script") {
            in_script = true;
        }
        if in_script {
            if i + 9 <= bytes.len() && bytes[i..i + 9].eq_ignore_ascii_case(b"</script>") {
                in_script = false;
                i += 9;
                continue;
            }
            i += 1;
            continue;
        }
        match bytes[i] {
            b'<' => {
                in_tag = true;
                i += 1;
            }
            b'>' => {
                in_tag = false;
                if !prev_space {
                    out.push(' ');
                    prev_space = true;
                }
                i += 1;
            }
            _ if !in_tag => {
                // `i` is always on a char boundary here (text advances by the
                // char's UTF-8 width; tags/markup are skipped byte-by-byte
                // through ASCII delimiters), so this slice is safe.
                let c = s[i..].chars().next().unwrap_or(' ');
                if c.is_whitespace() {
                    if !prev_space {
                        out.push(' ');
                        prev_space = true;
                    }
                } else {
                    out.push(c);
                    prev_space = false;
                }
                i += c.len_utf8();
            }
            _ => {
                i += 1;
            }
        }
    }
    out
}

// ---------------------------------------------------------------------------
// CSS adapter
// ---------------------------------------------------------------------------

fn adapt_css(path: &Path, content: &str) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    // Extract selector names and custom property declarations (--var-name).
    let mut selectors: Vec<String> = Vec::new();
    let mut custom_props: Vec<String> = Vec::new();
    for line in content.lines() {
        let t = line.trim();
        if t.ends_with('{') && !t.starts_with("/*") {
            let sel = t.trim_end_matches('{').trim();
            if !sel.is_empty() {
                selectors.push(sel.to_string());
            }
        }
        if t.starts_with("--") {
            let prop = t.split(':').next().unwrap_or("").trim();
            if !prop.is_empty() {
                custom_props.push(prop.to_string());
            }
        }
    }
    let body = format!("{} {}", selectors.join(" "), custom_props.join(" "));
    if body.trim().is_empty() {
        return vec![];
    }
    vec![SearchDoc::new(
        DocKind::Css,
        path_str.as_ref(),
        None,
        name,
        body.chars().take(2000).collect::<String>(),
    )]
}

// ---------------------------------------------------------------------------
// Manifest / build-script adapter
// ---------------------------------------------------------------------------

fn adapt_manifest(path: &Path, content: &str, kind: DocKind) -> Vec<SearchDoc> {
    let path_str = path.to_string_lossy();
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    // For build scripts, extract target/lane names.
    if kind == DocKind::BuildScript {
        let targets: Vec<String> = content
            .lines()
            .filter_map(|l| {
                let t = l.trim();
                // Makefile targets: "target:"
                if t.ends_with(':') && !t.starts_with('\t') && !t.starts_with('#') {
                    let target = t.trim_end_matches(':').trim();
                    if !target.is_empty() && !target.contains(' ') {
                        return Some(target.to_string());
                    }
                }
                // Fastlane lanes: "lane :name do"
                if t.starts_with("lane :") || t.starts_with("desc \"") {
                    return Some(t.to_string());
                }
                None
            })
            .take(50)
            .collect();
        let body = targets.join(" ");
        if body.is_empty() {
            return vec![SearchDoc::new(
                kind,
                path_str.as_ref(),
                None,
                name,
                content.chars().take(500).collect::<String>(),
            )];
        }
        return vec![SearchDoc::new(kind, path_str.as_ref(), None, name, body)];
    }

    // Generic manifest: flatten all key = value / "key": "value" lines.
    let body: String = content
        .lines()
        .filter(|l| {
            let t = l.trim();
            !t.is_empty() && !t.starts_with("//") && !t.starts_with('#')
        })
        .take(60)
        .collect::<Vec<_>>()
        .join(" ")
        .chars()
        .take(2000)
        .collect();
    if body.is_empty() {
        return vec![];
    }
    vec![SearchDoc::new(kind, path_str.as_ref(), None, name, body)]
}

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

    // Regression (Plan Q t-001): the indexer panicked on any doc containing a
    // multi-byte char (em-dash, arrow, …) because `strip_html_tags` iterated
    // bytes but sliced the string and advanced one byte past a wide char. These
    // must not panic and must preserve the char.
    #[test]
    fn strip_html_tags_survives_multibyte_chars() {
        assert_eq!(strip_html_tags("a — b"), "a — b");
        assert_eq!(strip_html_tags("x ← y → z"), "x ← y → z");
        assert_eq!(strip_html_tags("<b>café</b> — déjà"), "café — déjà");
        // A wide char adjacent to a <script> block (the original crash shape).
        assert_eq!(strip_html_tags("— <script>evil()</script> ok"), "— ok");
        // Emoji (4-byte) must not desync the index either.
        assert_eq!(strip_html_tags("ship 🚀 it"), "ship 🚀 it");
    }

    #[test]
    fn adapt_document_does_not_panic_on_unicode_html() {
        let html = "<html><body><h1>Title —</h1><p>arrow ← here</p></body></html>";
        // Must return Some(..) without panicking.
        let _ = adapt_document(Path::new("doc.html"), html);
    }
}