skillc 0.2.1

A development kit for Agent Skills - the open format for extending AI agent capabilities
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//! Search protocol per RFC-0004
//!
//! Provides full-text search over skill content using SQLite FTS5.

use crate::config::{ensure_dir, get_cwd};
use crate::error::{Result, SkillcError};
use crate::index::{self, SCHEMA_VERSION};
use crate::logging::{LogEntry, get_run_id, init_log_db, log_access_with_fallback};
use crate::markdown;
use crate::resolver::{ResolvedSkill, resolve_skill};
use crate::{OutputFormat, verbose};
use chrono::Utc;
use crossterm::style::Stylize;
use rusqlite::{Connection, params};
use serde::Serialize;
use std::fs;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::time::Instant;
use walkdir::WalkDir;

/// Search result entry.
#[derive(Debug, Serialize)]
pub struct SearchResult {
    pub file: String,
    pub section: String,
    pub snippet: String,
    pub score: f64,
}

/// Search response for JSON output.
#[derive(Debug, Serialize)]
pub struct SearchResponse {
    pub query: String,
    pub results: Vec<SearchResult>,
}

// Re-export HeadingEntry from index module for backward compatibility
pub use crate::index::HeadingEntry;

/// Compute hash16 for index filename (delegates to index module).
fn compute_hash16(source_path: &Path) -> String {
    index::compute_hash16(source_path)
}

/// Get the index database path (delegates to index module).
fn get_index_path(runtime_dir: &Path, source_dir: &Path) -> PathBuf {
    index::get_index_path(runtime_dir, source_dir)
}

/// Determine the tokenizer preference per [[RFC-0009:C-TOKENIZER]] and [[RFC-0004:C-INDEX]].
///
/// Uses config-based tokenizer setting:
/// - "ascii": porter unicode61 (English word-level with stemming)
/// - "cjk": unicode61 (character-level for CJK content)
fn get_tokenizer_preference(conn: &Connection) -> String {
    use crate::config::{Tokenizer, get_tokenizer};

    let config_tokenizer = get_tokenizer();

    match config_tokenizer {
        Tokenizer::Cjk => {
            // CJK mode: use unicode61 without porter (character-level)
            "unicode61".to_string()
        }
        Tokenizer::Ascii => {
            // ASCII mode: try porter unicode61 first (word-level with stemming)
            let result = conn.execute_batch(
                "CREATE VIRTUAL TABLE IF NOT EXISTS _tokenizer_test USING fts5(x, tokenize='porter unicode61');
                 DROP TABLE IF EXISTS _tokenizer_test;",
            );

            if result.is_ok() {
                "porter unicode61".to_string()
            } else {
                "unicode61".to_string()
            }
        }
    }
}

/// Get short tokenizer name for metadata storage.
fn tokenizer_short_name(tokenizer: &str) -> &str {
    if tokenizer.contains("porter") {
        "porter"
    } else {
        "unicode61"
    }
}

/// Read required metadata key from index.
fn read_meta(conn: &Connection, key: &str) -> Result<String> {
    conn.query_row(
        "SELECT value FROM index_meta WHERE key = ?1",
        [key],
        |row| row.get(0),
    )
    .map_err(|_| {
        SkillcError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("Missing metadata key: {}", key),
        ))
    })
}

/// Index state for decision making.
#[derive(Debug)]
enum IndexState {
    /// File does not exist
    Missing,
    /// Cannot read or parse
    Corrupt,
    /// skill_path mismatch (hash collision)
    Collision,
    /// Metadata differs (needs rebuild)
    Stale,
    /// All metadata matches
    UpToDate,
}

/// Check index state per [[RFC-0004:C-INDEX]].
fn check_index_state(
    index_path: &Path,
    source_dir: &Path,
    source_hash: &str,
    tokenizer_pref: &str,
) -> IndexState {
    if !index_path.exists() {
        return IndexState::Missing;
    }

    let conn = match Connection::open(index_path) {
        Ok(c) => c,
        Err(_) => return IndexState::Corrupt,
    };

    // Check if index_meta table exists
    let table_exists: bool = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='index_meta'",
            [],
            |row| row.get::<_, i32>(0).map(|c| c > 0),
        )
        .unwrap_or(false);

    if !table_exists {
        return IndexState::Corrupt;
    }

    // Read required keys
    let stored_skill_path = match read_meta(&conn, "skill_path") {
        Ok(v) => v,
        Err(_) => return IndexState::Corrupt,
    };
    let stored_hash = match read_meta(&conn, "source_hash") {
        Ok(v) => v,
        Err(_) => return IndexState::Corrupt,
    };
    let stored_schema: i32 = match read_meta(&conn, "schema_version") {
        Ok(v) => match v.parse() {
            Ok(n) => n,
            Err(_) => return IndexState::Corrupt,
        },
        Err(_) => return IndexState::Corrupt,
    };
    let stored_tokenizer = match read_meta(&conn, "tokenizer") {
        Ok(v) => v,
        Err(_) => return IndexState::Corrupt,
    };

    // Check collision
    let canonical_source = source_dir
        .canonicalize()
        .unwrap_or_else(|_| source_dir.to_path_buf())
        .to_string_lossy()
        .to_string();

    if stored_skill_path != canonical_source {
        return IndexState::Collision;
    }

    // Check staleness
    let current_tokenizer_short = tokenizer_short_name(tokenizer_pref);
    if stored_hash != source_hash
        || stored_schema < SCHEMA_VERSION
        || stored_tokenizer != current_tokenizer_short
    {
        return IndexState::Stale;
    }

    IndexState::UpToDate
}

/// Build the search index for a skill per [[RFC-0004:C-INDEX]].
pub fn build_index(source_dir: &Path, runtime_dir: &Path, source_hash: &str) -> Result<()> {
    let start = Instant::now();
    let index_path = get_index_path(runtime_dir, source_dir);

    verbose!("build_index: index_path={}", index_path.display());

    // Ensure directory exists
    if let Some(parent) = index_path.parent() {
        ensure_dir(parent)?;
    }

    // Create a temporary connection to determine tokenizer preference
    let temp_conn = Connection::open_in_memory()?;
    let tokenizer_pref = get_tokenizer_preference(&temp_conn);
    drop(temp_conn);

    verbose!("build_index: tokenizer={}", tokenizer_pref);

    // Check current state
    let state = check_index_state(&index_path, source_dir, source_hash, &tokenizer_pref);

    verbose!("build_index: state={:?}", state);

    match state {
        IndexState::UpToDate => {
            // Skip rebuild
            verbose!("build_index: skipping rebuild (up to date)");
            return Ok(());
        }
        IndexState::Collision => {
            // E003: Cannot proceed
            let hash16 = compute_hash16(source_dir);
            return Err(SkillcError::IndexHashCollision(hash16));
        }
        IndexState::Missing => {
            // Will create new
            verbose!("build_index: creating new index");
        }
        IndexState::Corrupt | IndexState::Stale => {
            // Delete and rebuild
            verbose!("build_index: deleting stale/corrupt index");
            let _ = fs::remove_file(&index_path);
        }
    }

    // Create new index
    create_index(&index_path, source_dir, source_hash, &tokenizer_pref)?;

    verbose!("build_index: completed in {:?}", start.elapsed());

    Ok(())
}

/// Create a new search index per [[RFC-0004:C-INDEX]].
fn create_index(
    index_path: &Path,
    source_dir: &Path,
    source_hash: &str,
    tokenizer: &str,
) -> Result<()> {
    let conn = Connection::open(index_path)?;

    // Create FTS5 table for full-text search
    let create_fts = format!(
        "CREATE VIRTUAL TABLE sections USING fts5(file, section, content, tokenize='{}')",
        tokenizer
    );
    conn.execute(&create_fts, [])?;

    // Create headings table for section lookup per [[RFC-0002:C-SHOW]]
    conn.execute(
        "CREATE TABLE headings (
            id INTEGER PRIMARY KEY,
            file TEXT NOT NULL,
            text TEXT NOT NULL,
            level INTEGER NOT NULL,
            start_line INTEGER NOT NULL,
            end_line INTEGER NOT NULL
        )",
        [],
    )?;
    conn.execute(
        "CREATE INDEX idx_headings_text ON headings(text COLLATE NOCASE)",
        [],
    )?;

    // Create metadata table
    conn.execute(
        "CREATE TABLE index_meta (key TEXT PRIMARY KEY, value TEXT)",
        [],
    )?;

    // Index files
    index_files(&conn, source_dir)?;

    // Write metadata
    let canonical_path = source_dir
        .canonicalize()
        .unwrap_or_else(|_| source_dir.to_path_buf())
        .to_string_lossy()
        .to_string();
    let tokenizer_short = tokenizer_short_name(tokenizer);
    let indexed_at = Utc::now().to_rfc3339();

    conn.execute(
        "INSERT INTO index_meta (key, value) VALUES (?1, ?2)",
        params!["skill_path", canonical_path],
    )?;
    conn.execute(
        "INSERT INTO index_meta (key, value) VALUES (?1, ?2)",
        params!["source_hash", source_hash],
    )?;
    conn.execute(
        "INSERT INTO index_meta (key, value) VALUES (?1, ?2)",
        params!["schema_version", SCHEMA_VERSION.to_string()],
    )?;
    conn.execute(
        "INSERT INTO index_meta (key, value) VALUES (?1, ?2)",
        params!["tokenizer", tokenizer_short],
    )?;
    conn.execute(
        "INSERT INTO index_meta (key, value) VALUES (?1, ?2)",
        params!["indexed_at", indexed_at],
    )?;

    Ok(())
}

/// Index all supported files in source directory.
fn index_files(conn: &Connection, source_dir: &Path) -> Result<()> {
    for entry in WalkDir::new(source_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
    {
        let path = entry.path();
        let ext = path.extension().and_then(|e| e.to_str());

        match ext {
            Some("md") => index_markdown(conn, source_dir, path)?,
            Some("txt") => index_text(conn, source_dir, path)?,
            _ => {
                // Silently skip unsupported formats per [[RFC-0004:C-FORMATS]]
            }
        }
    }

    Ok(())
}

/// Index a markdown file by sections per [[RFC-0004:C-FORMATS]].
fn index_markdown(conn: &Connection, source_dir: &Path, file_path: &Path) -> Result<()> {
    let content = fs::read_to_string(file_path)?;
    let relative_path = file_path
        .strip_prefix(source_dir)
        .unwrap_or(file_path)
        .to_string_lossy()
        .to_string();

    let lines: Vec<&str> = content.lines().collect();

    // Find all headings with their positions using AST parsing.
    // This avoids false positives from code blocks.
    let mut headings: Vec<(usize, usize, String)> = Vec::new(); // (line_idx, level, text)
    for heading in markdown::extract_headings(&content) {
        let line_idx = heading.line.saturating_sub(1);
        headings.push((line_idx, heading.level, heading.text));
    }

    if headings.is_empty() {
        // No headings, index entire file as one section
        conn.execute(
            "INSERT INTO sections (file, section, content) VALUES (?1, ?2, ?3)",
            params![relative_path, "", content],
        )?;
        return Ok(());
    }

    // Extract each section and populate both tables
    for (idx, (start_line, level, heading_text)) in headings.iter().enumerate() {
        // Find end of section (next heading of equal or higher level)
        let end_line = headings
            .iter()
            .skip(idx + 1)
            .find(|(_, l, _)| *l <= *level)
            .map(|(line, _, _)| *line)
            .unwrap_or(lines.len());

        // Extract content for FTS
        let section_content = lines[*start_line..end_line].join("\n");

        // Insert into sections table (for full-text search)
        conn.execute(
            "INSERT INTO sections (file, section, content) VALUES (?1, ?2, ?3)",
            params![relative_path, heading_text, section_content],
        )?;

        // Insert into headings table (for section lookup per [[RFC-0002:C-SHOW]])
        // Line numbers are 1-based in the index
        conn.execute(
            "INSERT INTO headings (file, text, level, start_line, end_line) VALUES (?1, ?2, ?3, ?4, ?5)",
            params![
                relative_path,
                heading_text,
                *level as i64,
                (*start_line + 1) as i64,  // Convert to 1-based
                (end_line + 1) as i64      // end_line is exclusive, 1-based
            ],
        )?;
    }

    Ok(())
}

/// Index a plain text file as single document per [[RFC-0004:C-FORMATS]].
fn index_text(conn: &Connection, source_dir: &Path, file_path: &Path) -> Result<()> {
    let content = fs::read_to_string(file_path)?;
    let relative_path = file_path
        .strip_prefix(source_dir)
        .unwrap_or(file_path)
        .to_string_lossy()
        .to_string();

    // Section field MUST be empty string for .txt files
    conn.execute(
        "INSERT INTO sections (file, section, content) VALUES (?1, ?2, ?3)",
        params![relative_path, "", content],
    )?;

    Ok(())
}

/// Execute search command per [[RFC-0004:C-SEARCH]].
///
/// Returns formatted output as a string.
pub fn search(skill: &str, query: &str, limit: usize, format: OutputFormat) -> Result<String> {
    let start = Instant::now();

    // Validate query per [[RFC-0004:C-QUERY-SYNTAX]]
    if query.trim().is_empty() {
        return Err(SkillcError::EmptyQuery);
    }

    let resolved = resolve_skill(skill)?;
    let run_id = get_run_id();

    verbose!("search: query=\"{}\" limit={}", query, limit);
    verbose!("search: source_dir={}", resolved.source_dir.display());

    // Initialize logging
    let log_conn = init_log_db(&resolved.runtime_dir);

    let result = do_search(&resolved, query, limit, &format);

    verbose!("search: completed in {:?}", start.elapsed());

    // Log access - extract result count from successful result
    let result_count = match &result {
        Ok((_, count)) => *count,
        Err(_) => 0,
    };

    let args = serde_json::json!({
        "query": query,
        "result_count": result_count,
    });

    // Log access (with automatic fallback for sandboxed environments)
    log_access_with_fallback(
        log_conn.as_ref(),
        &LogEntry {
            run_id,
            command: "search".to_string(),
            skill: resolved.name.clone(),
            skill_path: resolved.source_dir.to_string_lossy().to_string(),
            cwd: get_cwd(),
            args: args.to_string(),
            error: result.as_ref().err().map(|e| e.to_string()),
        },
    );

    result.map(|(output, _)| output)
}

/// Perform the actual search.
/// Returns (output_string, result_count).
fn do_search(
    resolved: &ResolvedSkill,
    query: &str,
    limit: usize,
    format: &OutputFormat,
) -> Result<(String, usize)> {
    let index_path = get_index_path(&resolved.runtime_dir, &resolved.source_dir);

    // Check if index exists
    if !index_path.exists() {
        return Err(SkillcError::IndexUnusable(resolved.name.clone()));
    }

    // Open and validate index
    let conn = Connection::open(&index_path)
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?;

    // Check index state (simplified - just check skill_path for collision)
    let table_exists: bool = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='index_meta'",
            [],
            |row| row.get::<_, i32>(0).map(|c| c > 0),
        )
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?;

    if !table_exists {
        return Err(SkillcError::IndexUnusable(resolved.name.clone()));
    }

    // Read skill_path and check for collision
    let stored_skill_path: String = conn
        .query_row(
            "SELECT value FROM index_meta WHERE key = 'skill_path'",
            [],
            |row| row.get(0),
        )
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?;

    let canonical_source = resolved
        .source_dir
        .canonicalize()
        .unwrap_or_else(|_| resolved.source_dir.clone())
        .to_string_lossy()
        .to_string();

    if stored_skill_path != canonical_source {
        let hash16 = compute_hash16(&resolved.source_dir);
        return Err(SkillcError::IndexHashCollision(hash16));
    }

    // Check staleness per [[RFC-0004:C-INDEX]]: source_hash, schema_version, tokenizer
    let stored_hash: String = conn
        .query_row(
            "SELECT value FROM index_meta WHERE key = 'source_hash'",
            [],
            |row| row.get(0),
        )
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?;

    let stored_schema: i32 = conn
        .query_row(
            "SELECT value FROM index_meta WHERE key = 'schema_version'",
            [],
            |row| row.get::<_, String>(0),
        )
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?
        .parse()
        .unwrap_or(0);

    let stored_tokenizer: String = conn
        .query_row(
            "SELECT value FROM index_meta WHERE key = 'tokenizer'",
            [],
            |row| row.get(0),
        )
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?;

    // Check schema version
    if stored_schema < SCHEMA_VERSION {
        return Err(SkillcError::IndexUnusable(resolved.name.clone()));
    }

    // Check tokenizer preference (use temp connection to test availability)
    let temp_conn = Connection::open_in_memory()
        .map_err(|_| SkillcError::IndexUnusable(resolved.name.clone()))?;
    let tokenizer_pref = get_tokenizer_preference(&temp_conn);
    drop(temp_conn);
    let current_tokenizer = tokenizer_short_name(&tokenizer_pref);
    if stored_tokenizer != current_tokenizer {
        return Err(SkillcError::IndexUnusable(resolved.name.clone()));
    }

    // Read manifest to get current source_hash
    let manifest_path = resolved
        .runtime_dir
        .join(".skillc-meta")
        .join("manifest.json");
    if manifest_path.exists() {
        let manifest_content = fs::read_to_string(&manifest_path)?;
        let manifest: serde_json::Value = serde_json::from_str(&manifest_content)?;
        if let Some(current_hash) = manifest.get("source_hash").and_then(|v| v.as_str())
            && stored_hash != current_hash
        {
            return Err(SkillcError::IndexUnusable(resolved.name.clone()));
        }
    }

    // Build FTS5 query per [[RFC-0004:C-QUERY-SYNTAX]]
    let fts_query = build_fts_query(query);

    // Execute search
    let mut stmt = conn.prepare(
        "SELECT file, section, snippet(sections, 2, '[MATCH]', '[/MATCH]', '...', 32), bm25(sections)
         FROM sections
         WHERE sections MATCH ?1
         ORDER BY bm25(sections)
         LIMIT ?2",
    )?;

    let results: Vec<SearchResult> = stmt
        .query_map(params![fts_query, limit as i64], |row| {
            Ok(SearchResult {
                file: row.get(0)?,
                section: row.get(1)?,
                snippet: row.get(2)?,
                score: -row.get::<_, f64>(3)?, // Negate BM25 score
            })
        })?
        .filter_map(|r| r.ok())
        .collect();

    let result_count = results.len();

    // Format output
    let output = match format {
        OutputFormat::Json => {
            let response = SearchResponse {
                query: query.to_string(),
                results,
            };
            serde_json::to_string_pretty(&response)?
        }
        OutputFormat::Text => {
            let is_tty = std::io::stdout().is_terminal();
            let mut lines = Vec::new();
            for result in &results {
                if result.section.is_empty() {
                    lines.push(format!("{} (score: {:.4})", result.file, result.score));
                } else {
                    lines.push(format!(
                        "{}#{} (score: {:.4})",
                        result.file, result.section, result.score
                    ));
                }
                // Render [MATCH]...[/MATCH] as colored text when outputting to TTY
                let snippet = if is_tty {
                    render_match_highlights(&result.snippet)
                } else {
                    // Strip markers for non-TTY output
                    result
                        .snippet
                        .replace("[MATCH]", "")
                        .replace("[/MATCH]", "")
                };
                lines.push(format!("  {}", snippet));
            }
            lines.join("\n")
        }
    };

    Ok((output, result_count))
}

/// Render [MATCH]...[/MATCH] markers as colored text.
fn render_match_highlights(text: &str) -> String {
    let mut result = String::new();
    let mut remaining = text;

    while let Some(start_idx) = remaining.find("[MATCH]") {
        // Add text before the match
        result.push_str(&remaining[..start_idx]);

        // Find the end marker
        let after_start = &remaining[start_idx + 7..]; // Skip "[MATCH]"
        if let Some(end_idx) = after_start.find("[/MATCH]") {
            // Extract matched text and apply yellow bold styling
            let matched = &after_start[..end_idx];
            result.push_str(&format!("{}", matched.yellow().bold()));
            remaining = &after_start[end_idx + 8..]; // Skip "[/MATCH]"
        } else {
            // No closing marker, just add the rest as-is
            result.push_str(&remaining[start_idx..]);
            break;
        }
    }

    // Add any remaining text
    result.push_str(remaining);
    result
}

/// Build FTS5 query from user input per [[RFC-0004:C-QUERY-SYNTAX]].
fn build_fts_query(query: &str) -> String {
    // Split on ASCII whitespace only
    let tokens: Vec<&str> = query
        .split([' ', '\t', '\n', '\r'])
        .filter(|s| !s.is_empty())
        .collect();

    // Quote each token (escape internal quotes)
    let quoted: Vec<String> = tokens
        .iter()
        .map(|t| {
            let escaped = t.replace('"', "\"\"");
            format!("\"{}\"", escaped)
        })
        .collect();

    quoted.join(" ")
}

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

    #[test]
    fn test_build_fts_query_simple() {
        assert_eq!(
            build_fts_query("configure authentication"),
            "\"configure\" \"authentication\""
        );
    }

    #[test]
    fn test_build_fts_query_with_quotes() {
        assert_eq!(
            build_fts_query("my \"special\" app"),
            "\"my\" \"\"\"special\"\"\" \"app\""
        );
    }

    #[test]
    fn test_build_fts_query_extra_whitespace() {
        assert_eq!(build_fts_query("  hello   world  "), "\"hello\" \"world\"");
    }

    #[test]
    fn test_compute_hash16() {
        let path = PathBuf::from("/tmp/test-skill");
        let hash = compute_hash16(&path);
        assert_eq!(hash.len(), 16);
    }

    #[test]
    fn test_index_markdown_ignores_code_block_headings() {
        let temp = TempDir::new().unwrap();
        let source_dir = temp.path();
        let file_path = source_dir.join("perf.md");
        let content = r#"# Title

## Typst Performance Profiling

```md
## Not a heading
```

More text

## Next Section
Text
"#;
        std::fs::write(&file_path, content).unwrap();

        let conn = Connection::open_in_memory().unwrap();
        conn.execute(
            "CREATE VIRTUAL TABLE sections USING fts5(file, section, content, tokenize='unicode61')",
            [],
        )
        .unwrap();
        conn.execute(
            "CREATE TABLE headings (
                id INTEGER PRIMARY KEY,
                file TEXT NOT NULL,
                text TEXT NOT NULL,
                level INTEGER NOT NULL,
                start_line INTEGER NOT NULL,
                end_line INTEGER NOT NULL
            )",
            [],
        )
        .unwrap();
        conn.execute(
            "CREATE INDEX idx_headings_text ON headings(text COLLATE NOCASE)",
            [],
        )
        .unwrap();

        index_markdown(&conn, source_dir, &file_path).unwrap();

        let headings = index::query_headings(&conn, "Typst Performance Profiling", None).unwrap();
        assert_eq!(headings.len(), 1);
        assert_eq!(headings[0].start_line, 3);
        assert_eq!(headings[0].end_line, 11);
    }
}