codescout 0.15.0

High-performance coding agent toolkit MCP server
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
use super::index::format_index_status;
use super::semantic_search::{
    apply_file_diversity_cap, format_search_result_item, format_semantic_search,
};
use super::*;
use crate::agent::Agent;
use crate::lsp::LspManager;
use crate::tools::{Tool, ToolContext};
use serde_json::json;
use tempfile::tempdir;

fn sr(file: &str, score: f32) -> crate::embed::schema::SearchResult {
    crate::embed::schema::SearchResult {
        id: 0,
        file_path: file.to_string(),
        language: "rust".to_string(),
        content: String::new(),
        start_line: 0,
        end_line: 0,
        score,
        source: "project".to_string(),
        project_id: String::new(),
    }
}

#[test]
fn file_diversity_cap_drops_excess_same_file_entries() {
    let input = vec![
        sr("a.rs", 0.9),
        sr("a.rs", 0.8),
        sr("a.rs", 0.7),
        sr("a.rs", 0.6),
        sr("b.rs", 0.5),
        sr("a.rs", 0.4),
    ];
    let out = apply_file_diversity_cap(input, 3);
    let files: Vec<&str> = out.iter().map(|r| r.file_path.as_str()).collect();
    // a.rs capped at 3; later a.rs entry dropped; b.rs survives with original ordering
    assert_eq!(files, vec!["a.rs", "a.rs", "a.rs", "b.rs"]);
}

#[test]
fn file_diversity_cap_zero_disables() {
    let input = vec![sr("a.rs", 0.9), sr("a.rs", 0.8), sr("a.rs", 0.7)];
    let out = apply_file_diversity_cap(input.clone(), 0);
    assert_eq!(out.len(), 3);
}

#[test]
fn file_diversity_cap_preserves_score_order() {
    // Order is preserved — cap does not re-rank; it just filters.
    let input = vec![
        sr("a.rs", 0.9),
        sr("b.rs", 0.8),
        sr("a.rs", 0.7),
        sr("c.rs", 0.6),
    ];
    let out = apply_file_diversity_cap(input, 1);
    let files: Vec<&str> = out.iter().map(|r| r.file_path.as_str()).collect();
    assert_eq!(files, vec!["a.rs", "b.rs", "c.rs"]);
}

#[tokio::test]
async fn index_project_sets_initial_running_state() {
    use crate::agent::IndexingState;
    // Call index_project on a project with no embedder configured.
    // It will start and quickly fail in the background, but the
    // initial Running state is set synchronously before the spawn.
    let (_dir, ctx) = project_ctx().await;
    let _ = IndexProject.call(json!({}), &ctx).await;

    // State should not be left Idle after a call — it was either
    // set to Running (still in progress) or transitioned to Done/Failed.
    let state = ctx.agent.indexing.lock().unwrap().clone();
    assert!(!matches!(state, IndexingState::Idle));
}

async fn project_ctx() -> (tempfile::TempDir, ToolContext) {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join(".codescout")).unwrap();
    let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
    (
        dir,
        ToolContext {
            agent,
            lsp: LspManager::new_arc(),
            output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
            progress: None,
            peer: None,
            section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
                crate::tools::section_coverage::SectionCoverage::new(),
            )),
            guide_hints_emitted: std::sync::Arc::new(parking_lot::Mutex::new(Default::default())),
            workspace_override: None,
        },
    )
}

#[tokio::test]
async fn index_status_no_index() {
    let (_dir, ctx) = project_ctx().await;
    let result = IndexStatus.call(json!({}), &ctx).await.unwrap();
    assert_eq!(result["indexed"], false);
}

#[tokio::test]
async fn index_status_shows_running_progress() {
    use crate::agent::IndexingState;
    let (_dir, ctx) = project_ctx().await;

    // Simulate mid-run state — IndexStatus appends the indexing block
    // regardless of whether Qdrant is reachable.
    {
        let mut state = ctx.agent.indexing.lock().unwrap();
        *state = IndexingState::Running {
            done: 10,
            total: 50,
            eta_secs: Some(20),
        };
    }

    let result = IndexStatus.call(json!({}), &ctx).await.unwrap();
    let indexing = &result["indexing"];
    assert_eq!(indexing["status"], "running");
    assert_eq!(indexing["done"], 10);
    assert_eq!(indexing["total"], 50);
    assert_eq!(indexing["eta_secs"], 20);

    // eta_secs: None renders as JSON null
    {
        let mut state = ctx.agent.indexing.lock().unwrap();
        *state = IndexingState::Running {
            done: 50,
            total: 50,
            eta_secs: None,
        };
    }
    let result2 = IndexStatus.call(json!({}), &ctx).await.unwrap();
    assert_eq!(result2["indexing"]["eta_secs"], serde_json::Value::Null);
}

#[tokio::test]
async fn index_status_running_zero_zero_carries_liveness_note() {
    use crate::agent::IndexingState;
    let (_dir, ctx) = project_ctx().await;

    // Project-scope builds (sync_project) never advance done/total — they
    // sit at 0/0 for the whole build. Status must NOT let that read as a
    // stall: it annotates the 0/0 case with a liveness note + chunks_so_far.
    {
        let mut state = ctx.agent.indexing.lock().unwrap();
        *state = IndexingState::Running {
            done: 0,
            total: 0,
            eta_secs: None,
        };
    }

    let result = IndexStatus.call(json!({}), &ctx).await.unwrap();
    let indexing = &result["indexing"];
    assert_eq!(indexing["status"], "running");
    assert_eq!(indexing["done"], 0);
    assert_eq!(indexing["total"], 0);

    let note = indexing["note"]
        .as_str()
        .expect("0/0 running state must carry an explanatory note");
    assert!(
        note.contains("0/0") && note.contains("not a stall"),
        "note must explain 0/0 is the healthy in-progress shape, not a stall, got: {note}"
    );
    assert!(
        indexing["chunks_so_far"].is_u64(),
        "chunks_so_far must be present as the adjacent liveness number, got: {}",
        indexing["chunks_so_far"]
    );

    // The non-zero path must NOT carry the note — it is specific to 0/0.
    {
        let mut state = ctx.agent.indexing.lock().unwrap();
        *state = IndexingState::Running {
            done: 5,
            total: 20,
            eta_secs: Some(10),
        };
    }
    let result2 = IndexStatus.call(json!({}), &ctx).await.unwrap();
    assert!(
        result2["indexing"]["note"].is_null(),
        "non-zero progress must not carry the 0/0 liveness note"
    );
}

#[test]
fn resolve_done_total_prefers_qdrant_count_over_placeholder() {
    use super::index::resolve_done_total;

    // Qdrant supplied the totals (top-level file_count/chunk_count present):
    // prefer them over the producer's 0 placeholder (see IndexProject::call).
    let with_qdrant = json!({ "file_count": 1185, "chunk_count": 31286 });
    assert_eq!(resolve_done_total(&with_qdrant, "file_count", 0), 1185);
    assert_eq!(resolve_done_total(&with_qdrant, "chunk_count", 0), 31286);

    // Qdrant unavailable (key absent): fall back to the variant value.
    let no_qdrant = json!({ "indexed": false });
    assert_eq!(resolve_done_total(&no_qdrant, "file_count", 0), 0);
    assert_eq!(resolve_done_total(&no_qdrant, "chunk_count", 7), 7);
}

#[tokio::test]
async fn tools_error_without_project() {
    let ctx = ToolContext {
        agent: Agent::new(None).await.unwrap(),
        lsp: LspManager::new_arc(),
        output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
        progress: None,
        peer: None,
        section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
            crate::tools::section_coverage::SectionCoverage::new(),
        )),
        guide_hints_emitted: std::sync::Arc::new(parking_lot::Mutex::new(Default::default())),
        workspace_override: None,
    };
    assert!(SemanticSearch
        .call(json!({ "query": "test" }), &ctx)
        .await
        .is_err());
    assert!(IndexProject.call(json!({}), &ctx).await.is_err());
    assert!(IndexStatus.call(json!({}), &ctx).await.is_err());
}

#[tokio::test]
async fn semantic_search_schema_has_detail_level() {
    let schema = SemanticSearch.input_schema();
    let props = schema["properties"].as_object().unwrap();
    assert!(
        props.contains_key("detail_level"),
        "should accept detail_level parameter"
    );
    assert!(
        props.contains_key("offset"),
        "should accept offset parameter"
    );
}

#[test]
fn preview_truncation_works() {
    // Helper mirrors the inline logic in SemanticSearch::call
    let truncate = |content: &str| -> String {
        let first_line = content.lines().next().unwrap_or("").trim();
        let char_count = first_line.chars().count();
        if char_count > 50 {
            let truncated: String = first_line.chars().take(47).collect();
            format!("{}...", truncated)
        } else {
            first_line.to_string()
        }
    };

    // ASCII: long content truncated to 47 chars + "..."
    let long_ascii = "x".repeat(100);
    let preview = truncate(&long_ascii);
    assert_eq!(preview.chars().count(), 50); // 47 + "..."
    assert!(preview.ends_with("..."));

    // Unicode: multi-byte chars must not panic
    // Each '日' is 3 bytes; 51 chars = 153 bytes — old [..47] would panic mid-codepoint
    let long_unicode = "".repeat(51);
    let preview_unicode = truncate(&long_unicode);
    assert_eq!(preview_unicode.chars().count(), 50); // 47 '日' + "..."
    assert!(preview_unicode.ends_with("..."));

    // Emoji: also multi-byte
    let emoji_line = "🦀".repeat(51);
    let preview_emoji = truncate(&emoji_line);
    assert_eq!(preview_emoji.chars().count(), 50);

    // Multi-line: only first line is used
    let multiline = "first line\nsecond line\nthird line";
    assert_eq!(truncate(multiline), "first line");

    // Short content: no truncation
    assert_eq!(truncate("short"), "short");
}

#[tokio::test]
async fn semantic_search_schema_has_scope() {
    let schema = SemanticSearch.input_schema();
    let props = schema["properties"].as_object().unwrap();
    assert!(props.contains_key("scope"), "should accept scope parameter");
}

#[test]
fn semantic_search_schema_has_include_memories() {
    let schema = SemanticSearch.input_schema();
    assert!(schema["properties"]["include_memories"].is_object());
    assert_eq!(schema["properties"]["include_memories"]["type"], "boolean");
}

#[test]
fn semantic_search_schema_has_project() {
    let schema = SemanticSearch.input_schema();
    let props = schema["properties"].as_object().unwrap();
    assert!(
        props.contains_key("project_id"),
        "schema should have project_id param"
    );
}

#[tokio::test]
async fn concurrent_semantic_search_does_not_deadlock() {
    let (_dir, ctx) = project_ctx().await;
    let ctx = std::sync::Arc::new(ctx);
    let input = json!({"query": "test"});

    // Run two searches concurrently — neither should hang.
    // They'll likely error (no embedder available in test), but that's fine.
    // The point is they complete without deadlocking.
    let ctx1 = ctx.clone();
    let input1 = input.clone();
    let ctx2 = ctx.clone();
    let input2 = input.clone();

    let (r1, r2) = tokio::join!(
        async move { SemanticSearch.call(input1, &ctx1).await },
        async move { SemanticSearch.call(input2, &ctx2).await },
    );

    // Both should complete (either Ok or Err, but not hang)
    // We expect errors since there's no embedder in test environment
    let _ = r1;
    let _ = r2;
}

// --- format_semantic_search tests ---

#[test]
fn semantic_search_basic() {
    let val = serde_json::json!({
        "results": [
            {
                "file_path": "src/tools/output.rs",
                "language": "rust",
                "content": "pub struct OutputGuard {\n    mode: OutputMode,\n}",
                "start_line": 35,
                "end_line": 50,
                "score": 0.923,
                "source": "project"
            },
            {
                "file_path": "src/tools/mod.rs",
                "language": "rust",
                "content": "pub trait Tool {\n    fn name(&self) -> &str;\n}",
                "start_line": 120,
                "end_line": 140,
                "score": 0.81,
                "source": "project"
            }
        ],
        "total": 2
    });
    let result = format_semantic_search(&val);
    assert!(result.starts_with("2 results\n"));
    assert!(result.contains("src/tools/output.rs:35-50"));
    assert!(result.contains("src/tools/mod.rs:120-140"));
    assert!(result.contains("pub struct OutputGuard {"));
    assert!(result.contains("pub trait Tool {"));
}

#[test]
fn semantic_search_single_result() {
    let val = serde_json::json!({
        "results": [
            {
                "file_path": "src/main.rs",
                "language": "rust",
                "content": "fn main() {}",
                "start_line": 1,
                "end_line": 1,
                "score": 0.95,
                "source": "project"
            }
        ],
        "total": 1
    });
    let result = format_semantic_search(&val);
    assert!(result.starts_with("1 result\n"));
    assert!(!result.starts_with("1 results"));
    assert!(result.contains("src/main.rs:1"));
}

#[test]
fn semantic_search_empty() {
    let val = serde_json::json!({
        "results": [],
        "total": 0
    });
    assert_eq!(format_semantic_search(&val), "0 results");
}

#[test]
fn semantic_search_missing_results() {
    let val = serde_json::json!({});
    assert_eq!(format_semantic_search(&val), "");
}

#[test]
fn semantic_search_with_staleness() {
    let val = serde_json::json!({
        "results": [
            {
                "file_path": "src/a.rs",
                "content": "fn foo() {}",
                "start_line": 1,
                "end_line": 5,
                "score": 0.9,
                "source": "project"
            }
        ],
        "total": 1,
        "git_sync": { "status": "behind", "behind_commits": 5 }
    });
    let result = format_semantic_search(&val);
    assert!(result.contains("5 commits not yet indexed"));
    assert!(result.contains("index(action='build')"));
}

#[test]
fn semantic_search_with_overflow() {
    let val = serde_json::json!({
        "results": [
            {
                "file_path": "src/a.rs",
                "content": "fn foo() {}",
                "start_line": 1,
                "end_line": 5,
                "score": 0.9,
                "source": "project"
            }
        ],
        "total": 50,
        "overflow": {
            "shown": 10,
            "total": 50,
            "hint": "Use detail_level='full' with offset for pagination"
        }
    });
    let result = format_semantic_search(&val);
    assert!(result.contains("10 of 50"));
}

#[test]
fn semantic_search_long_content_truncated() {
    let long_content = "a".repeat(80);
    let val = serde_json::json!({
        "results": [
            {
                "file_path": "src/a.rs",
                "content": long_content,
                "start_line": 1,
                "end_line": 10,
                "score": 0.85,
                "source": "project"
            }
        ],
        "total": 1
    });
    let result = format_semantic_search(&val);
    assert!(result.contains("..."));
    assert!(!result.contains(&"a".repeat(80)));
}

#[test]
fn semantic_search_score_alignment() {
    let val = serde_json::json!({
        "results": [
            {
                "file_path": "a.rs",
                "content": "short",
                "start_line": 1, "end_line": 1,
                "score": 0.9, "source": "project"
            },
            {
                "file_path": "very/long/path/to/file.rs",
                "content": "long path",
                "start_line": 100, "end_line": 200,
                "score": 0.85, "source": "project"
            }
        ],
        "total": 2
    });
    let result = format_semantic_search(&val);
    assert!(result.contains("a.rs:1"));
    assert!(result.contains("very/long/path/to/file.rs:100-200"));
}

// --- format_index_status tests ---

#[test]
fn format_index_status_shows_model_and_timestamp() {
    let result = serde_json::json!({
        "indexed": true,
        "file_count": 42,
        "chunk_count": 1234,
        "git_sync": { "status": "up_to_date" },
        "indexed_with_model": "text-embedding-3-small",
        "indexed_at": "2026-03-01 14:22"
    });
    let out = format_index_status(&result);
    assert!(
        out.contains("42 files"),
        "should show file count, got: {out}"
    );
    assert!(
        out.contains("1234 chunks"),
        "should show chunk count, got: {out}"
    );
    assert!(
        out.contains("text-embedding-3-small"),
        "should show model, got: {out}"
    );
    assert!(
        out.contains("2026-03-01"),
        "should show timestamp, got: {out}"
    );
}

#[test]
fn format_index_status_stale_shows_commit_count() {
    let result = serde_json::json!({
        "indexed": true,
        "file_count": 10,
        "chunk_count": 100,
        "git_sync": { "status": "behind", "behind_commits": 5 }
    });
    let out = format_index_status(&result);
    assert!(
        out.contains("5 commits not yet indexed"),
        "should note git sync lag, got: {out}"
    );
}

#[test]
fn search_result_item_content_is_last_field() {
    // Regression: content must be last — it is the bulk payload.
    // Score is excluded from output; ranking is communicated by result order.
    let item =
        format_search_result_item("src/foo.rs", 10, 20, "project", "fn hello() {}".to_string());
    let keys: Vec<&str> = item
        .as_object()
        .unwrap()
        .keys()
        .map(|s| s.as_str())
        .collect();

    assert!(
        keys.iter().all(|k| *k != "score"),
        "score must not appear in output, got key order: {keys:?}"
    );
    let content_pos = keys.iter().position(|k| *k == "content").unwrap();
    assert_eq!(
        content_pos,
        keys.len() - 1,
        "content must be the last field, got key order: {keys:?}"
    );
}

#[tokio::test]
async fn semantic_search_uses_scope_for_library_search() {
    // Scope "lib:nonexistent" should return a RecoverableError (not a panic or fatal error).
    // Since issue #5, search_multi_db validates the scope against the registry, so an
    // unregistered library name returns a RecoverableError before any embedder call.
    let (_dir, ctx) = project_ctx().await;
    let tool = SemanticSearch;
    let result = tool
        .call(
            json!({"query": "runtime", "scope": "lib:nonexistent"}),
            &ctx,
        )
        .await;
    // Acceptable outcomes:
    // 1. RecoverableError about unregistered library name
    // 2. RecoverableError about missing embedder / model config
    // A plain anyhow error from source_filter/search_scoped code paths would be a regression.
    match &result {
        Ok(val) => {
            assert!(val["total"].as_u64().unwrap_or(0) == 0);
        }
        Err(e) => {
            let msg = e.to_string();
            assert!(
                msg.contains("model")
                    || msg.contains("embed")
                    || msg.contains("project")
                    || msg.contains("not registered")
                    || msg.contains("registered")
                    || msg.contains("hybrid_query")
                    || msg.contains("retrieval stack"),
                "unexpected error (not embedder or registry-related): {msg}"
            );
        }
    }
}

// --- Progress notification tests (T11) ---

use crate::tools::progress::test_support::CountingSink;
use std::sync::atomic::Ordering;

fn make_progress_pair() -> (
    std::sync::Arc<crate::tools::progress::ProgressReporter>,
    std::sync::Arc<CountingSink>,
) {
    let sink = std::sync::Arc::new(CountingSink::default());
    let reporter = crate::tools::progress::ProgressReporter::with_sink(
        sink.clone(),
        rmcp::model::NumberOrString::Number(1),
    );
    (reporter, sink)
}

async fn project_ctx_with_progress(
) -> (tempfile::TempDir, ToolContext, std::sync::Arc<CountingSink>) {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join(".codescout")).unwrap();
    let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
    let (reporter, sink) = make_progress_pair();
    let ctx = ToolContext {
        agent,
        lsp: LspManager::new_arc(),
        output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
        progress: Some(reporter),
        peer: None,
        section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
            crate::tools::section_coverage::SectionCoverage::new(),
        )),
        guide_hints_emitted: std::sync::Arc::new(parking_lot::Mutex::new(Default::default())),
        workspace_override: None,
    };
    (dir, ctx, sink)
}

#[tokio::test]
async fn semantic_search_emits_progress_text() {
    // SemanticSearch calls report_text("loading embedding model") and report_text("searching")
    // even when the embedder itself fails. Verify the text sink fires at least once.
    let (_dir, ctx, sink) = project_ctx_with_progress().await;
    let _ = SemanticSearch
        .call(json!({"query": "test function"}), &ctx)
        .await;
    assert!(
        sink.text_calls.load(Ordering::Relaxed) >= 1,
        "expected at least 1 report_text() call from semantic_search"
    );
}

#[tokio::test]
async fn index_project_emits_progress_on_start() {
    // Progress is opt-in: a ctx with `progress: Some(_)` models a client that
    // sent `_meta.progressToken`, so index_project emits an initial progress +
    // status-text report. BUG-038 was the OLD unconditional emit (token
    // synthesized from the request id) that fired even without opt-in and
    // crashed Claude Code 2.x; see server.rs::call_tool.
    let (_dir, ctx, sink) = project_ctx_with_progress().await;
    let result = IndexProject.call(json!({}), &ctx).await;
    assert!(result.is_ok());
    assert!(
        sink.progress_calls.load(Ordering::Relaxed) >= 1,
        "index_project should emit an initial progress report when the client opted in"
    );
    assert!(
        sink.text_calls.load(Ordering::Relaxed) >= 1,
        "index_project should emit an initial status text when the client opted in"
    );
}

// --- Preflight elicitation tests (T7) ---

#[tokio::test]
async fn index_project_no_elicit_for_normal_project() {
    // A tiny project well under an explicit 10 000-byte threshold.
    // Writing project.toml makes the coupling explicit: preflight must
    // read the threshold, compare it to the ~14-byte file, decide Clear,
    // and never trigger elicitation.  If elicitation WERE triggered,
    // ctx.peer = None would cause the tool to abort with
    // "does not support elicitation" — caught by the Err arm below.
    let (dir, mut ctx) = project_ctx().await;

    let cs_dir = dir.path().join(".codescout");
    std::fs::create_dir_all(&cs_dir).unwrap();
    std::fs::write(
        cs_dir.join("project.toml"),
        "[project]\nname = \"test\"\n\n[security]\nmax_index_bytes = 10000\n",
    )
    .unwrap();
    std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();

    // Rebuild agent so it picks up the new project.toml.
    let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
    ctx.agent = agent;

    let result = IndexProject.call(json!({}), &ctx).await;
    match &result {
        Ok(_) => { /* indexing ran through preflight — test passes */ }
        Err(e) => {
            let msg = format!("{e:?}");
            assert!(
                !msg.contains("client does not support elicitation"),
                "preflight should not have elicited for a tiny project: {msg}"
            );
        }
    }
}

#[tokio::test]
async fn index_project_aborts_when_elicit_unavailable_on_oversized_root() {
    // Force max_index_bytes = 0 so any non-empty directory triggers
    // RequiresConfirmation. With peer = None, elicit returns Ok(None)
    // and the tool must refuse with a clear error rather than proceeding.
    let (dir, mut ctx) = project_ctx().await;

    let cs_dir = dir.path().join(".codescout");
    std::fs::create_dir_all(&cs_dir).unwrap();
    std::fs::write(
        cs_dir.join("project.toml"),
        "[project]\nname = \"test\"\n\n[security]\nmax_index_bytes = 0\n",
    )
    .unwrap();
    std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();

    // Rebuild agent so it picks up the new project.toml.
    let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
    ctx.agent = agent;

    let err = IndexProject.call(json!({}), &ctx).await.unwrap_err();
    let msg = format!("{err:?}");
    assert!(
        msg.contains("does not support elicitation") || msg.contains("user did not confirm"),
        "expected elicit-unavailable abort, got: {msg}"
    );
    // TODO: add confirm=true and confirm=false tests once a MockPeer exists.
}

#[tokio::test]
async fn index_action_unknown_errors() {
    let (_dir, ctx) = project_ctx().await;
    let err = Index
        .call(json!({ "action": "wat" }), &ctx)
        .await
        .unwrap_err();
    assert!(
        err.to_string().contains("unknown index action"),
        "expected unknown action error, got: {err}"
    );
}

#[tokio::test]
async fn index_action_missing_errors() {
    let (_dir, ctx) = project_ctx().await;
    let err = Index.call(json!({}), &ctx).await.unwrap_err();
    assert!(
        err.to_string().contains("index requires 'action'"),
        "expected missing action error, got: {err}"
    );
}

#[test]
fn index_is_write_depends_on_action() {
    assert!(Index.is_write(&json!({ "action": "build" })));
    assert!(!Index.is_write(&json!({ "action": "status" })));
    assert!(!Index.is_write(&json!({})));
}