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
//! Indexing tools: IndexProject, IndexStatus, Index.

use super::super::{parse_bool_param, Tool, ToolContext};
use serde_json::{json, Value};

pub struct IndexProject;
pub struct IndexStatus;
pub struct Index;

#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
struct IndexConfirm {
    /// Confirm indexing this directory
    confirm: bool,
}
rmcp::elicit_safe!(IndexConfirm);

#[async_trait::async_trait]
impl Tool for IndexProject {
    fn name(&self) -> &str {
        "index_project"
    }

    fn is_write(&self, _input: &Value) -> bool {
        true
    }

    fn description(&self) -> &str {
        "Build or incrementally update the semantic search index for the active project. \
         Use scope='lib:<name>' to index a registered library (replaces index_library)."
    }
    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "force": { "type": "boolean", "default": false,
                    "description": "Force full reindex, ignoring cached file hashes" },
                "scope": {
                    "type": "string",
                    "default": "project",
                    "description": "Scope to index: 'project' (default) to index the active project, or 'lib:<name>' to index a registered library. Replaces index_library."
                }
            }
        })
    }
    async fn call(&self, input: Value, ctx: &ToolContext) -> anyhow::Result<Value> {
        use crate::agent::IndexingState;

        let scope_str = input["scope"].as_str().unwrap_or("project");

        // Library scope: delegate to library indexing logic (replaces index_library tool)
        if let Some(lib_name) = scope_str.strip_prefix("lib:") {
            let force = parse_bool_param(&input["force"]);

            // Guard against concurrent runs — mirror the project-scope branch
            // so two concurrent `index_project(scope="lib:foo")` calls (or a
            // lib + project call together) don't race on the shared
            // `libraries.json` rewrite or the sqlite busy-timeout.
            {
                let mut state = ctx.agent.indexing.lock().unwrap_or_else(|e| e.into_inner());
                if matches!(*state, IndexingState::Running { .. }) {
                    return Ok(json!({
                        "status": "already_running",
                        "hint": "Use index(action='status') to check progress.",
                    }));
                }
                *state = IndexingState::Running {
                    done: 0,
                    total: 0,
                    eta_secs: None,
                };
            }

            // Ensure we always reset indexing state on every exit path from
            // the lib-scope branch — success, error, or early return.
            struct StateGuard {
                indexing: std::sync::Arc<std::sync::Mutex<IndexingState>>,
                active: bool,
            }
            impl Drop for StateGuard {
                fn drop(&mut self) {
                    if self.active {
                        let mut s = self.indexing.lock().unwrap_or_else(|e| e.into_inner());
                        if matches!(*s, IndexingState::Running { .. }) {
                            *s = IndexingState::Idle;
                        }
                    }
                }
            }
            let _state_guard = StateGuard {
                indexing: ctx.agent.indexing.clone(),
                active: true,
            };

            let (root, lib_path) = ctx
                .agent
                .with_project_at(ctx.workspace_override.as_deref(), |project| {
                let entry = project.library_registry.lookup(lib_name).ok_or_else(|| {
                    crate::tools::RecoverableError::with_hint(
                        format!("Library '{}' not found in registry.", lib_name),
                        "Use library(action='list') to see registered libraries.",
                    )
                })?;
                if !entry.source_available {
                    return Err(crate::tools::RecoverableError::with_hint(
                        format!(
                            "Library '{}' source code is not available locally.",
                            lib_name
                        ),
                        "Download sources using the project's build tool, then call \
                         library(action='register', path=\"/path/to/source\", name, language) and retry.",
                    )
                    .into());
                }
                    Ok((project.root.clone(), entry.path.clone()))
                })
                .await?;

            let source = format!("lib:{}", lib_name);
            let lib_project_id = source.clone();

            // Sync the library directory into Qdrant under its own
            // project_id namespace (`lib:<name>`). The retrieval stack
            // handles chunking, embedding, and incremental upsert/delete.
            let client = crate::retrieval::client::RetrievalClient::from_env().await?;
            let opts = crate::retrieval::sync::SyncOpts {
                force_reindex: force,
                ..Default::default()
            };
            client
                .sync_project(&lib_project_id, &lib_path, opts)
                .await?;

            // Read current version from lockfile and update the registry.
            let versions = crate::library::versions::resolve_dependency_versions(&root);
            let current_version = crate::library::versions::find_version(&versions, lib_name);
            if current_version.is_none() {
                tracing::debug!(
                    "version tracking not available for library '{}' — unsupported lockfile ecosystem",
                    lib_name
                );
            }

            ctx.agent
                .with_project_at_mut(ctx.workspace_override.as_deref(), |project| {
                    if let Some(entry) = project.library_registry.lookup_mut(lib_name) {
                        entry.indexed = true;
                        if let Some(ver) = &current_version {
                            entry.version = Some(ver.clone());
                            entry.version_indexed = Some(ver.clone());
                            entry.nudge_dismissed = false;
                        }
                    }
                    let registry_path = project.root.join(".codescout").join("libraries.json");
                    project.library_registry.save(&registry_path)?;
                    Ok(())
                })
                .await?;

            // Read counts back from Qdrant for the response. We re-scroll
            // here rather than threading them through sync_project's return
            // type so the same shape works for force / non-force reruns.
            let collection = client.config.collection("code_chunks");
            let (chunk_count, file_count) = client
                .project_index_stats(&collection, &lib_project_id)
                .await
                .unwrap_or((0, 0));

            return Ok(json!({
                "status": "ok",
                "library": lib_name,
                "source": source,
                "files_indexed": file_count,
                "chunks": chunk_count,
            }));
        }

        let force = parse_bool_param(&input["force"]);
        let root = ctx
            .agent
            .require_project_root_for(ctx.workspace_override.as_deref())
            .await?;

        // ── Preflight scope check ───────────────────────────────────────
        // Stat-walk the root to estimate size + detect broad roots (home, system).
        // Requires user confirmation via elicitation if either trigger fires.
        // Runs BEFORE the concurrent-run guard so that a declined or unavailable
        // elicitation never leaves IndexingState stuck in Running.
        {
            use crate::embed::preflight::{check_index_scope, PreflightVerdict};

            let max_bytes = ctx
                .agent
                .with_project_at(ctx.workspace_override.as_deref(), |p| {
                    Ok(p.config.security.max_index_bytes)
                })
                .await
                .unwrap_or(500 * 1024 * 1024);
            let preflight_root = root.clone();
            let verdict =
                tokio::task::spawn_blocking(move || check_index_scope(&preflight_root, max_bytes))
                    .await
                    .map_err(|e| anyhow::anyhow!("preflight task join error: {e}"))??;

            if let PreflightVerdict::RequiresConfirmation(info) = verdict {
                tracing::info!(
                    root = ?info.root,
                    file_count = info.file_count,
                    approx_bytes = info.approx_bytes,
                    suspicious = ?info.suspicious_reason,
                    size_over = info.size_exceeds_threshold,
                    "index_project preflight requires confirmation"
                );
                let msg = info.elicitation_message();
                match ctx.elicit::<IndexConfirm>(msg).await? {
                    Some(IndexConfirm { confirm: true }) => {
                        tracing::info!(root = ?info.root, "index scope confirmed by user");
                    }
                    Some(IndexConfirm { confirm: false }) => {
                        return Err(crate::tools::RecoverableError::with_hint(
                            "Indexing aborted — user did not confirm the scope",
                            "Activate a more specific project root, or raise \
                             security.max_index_bytes in .codescout/project.toml, then retry.",
                        )
                        .into());
                    }
                    None => {
                        // No peer, client lacks elicitation capability, or no content returned.
                        // For this guard, the safe default is to refuse — never silently proceed.
                        return Err(crate::tools::RecoverableError::with_hint(
                            "index_project needs confirmation but client does not support elicitation",
                            "Raise security.max_index_bytes in .codescout/project.toml, \
                             or activate a narrower project root, then retry.",
                        )
                        .into());
                    }
                }
            }
        }
        // ────────────────────────────────────────────────────────────────

        // Guard against concurrent runs.
        {
            let mut state = ctx.agent.indexing.lock().unwrap_or_else(|e| e.into_inner());
            if matches!(*state, IndexingState::Running { .. }) {
                return Ok(json!({
                    "status": "already_running",
                    "hint": "Use index(action='status') to check progress."
                }));
            }
            *state = IndexingState::Running {
                done: 0,
                total: 0,
                eta_secs: None,
            };
        }

        let state_arc = ctx.agent.indexing.clone();
        let progress = ctx.progress.clone();
        // Progress is opt-in: `ctx.progress` is `Some` only when the client sent
        // `_meta.progressToken` (see server.rs::call_tool), so emitting here is
        // safe — never an unsolicited notification. BUG-038 was the old
        // unconditional case that crashed Claude Code 2.x.
        if let Some(p) = &progress {
            p.report(0, None).await;
            p.report_text("indexing project").await;
        }

        // Resolve project_id up front — sync_project needs it as the
        // multi-tenant namespace inside the shared Qdrant collection.
        let project_id = ctx
            .agent
            .with_project_at(ctx.workspace_override.as_deref(), |p| {
                Ok(p.project_id().to_string())
            })
            .await?;

        // Capture the dirty-files Arc before spawning so the task can clear it on success.
        let dirty_files_arc = ctx
            .agent
            .dirty_files_arc_for(ctx.workspace_override.as_deref())
            .await;

        tracing::info!(force, "spawning sync task for project");
        let sync_abort_for_task = ctx.agent.active_sync_abort.clone();
        let sync_abort_for_store = ctx.agent.active_sync_abort.clone();
        let task = tokio::spawn(async move {
            // sync_project does not yet stream incremental progress; that deeper
            // wiring is tracked separately. `progress` (opt-in via progressToken)
            // is moved in so future increments can report safely. IndexingState
            // stays at Running{done:0, total:0} until completion sets Done/Failed.
            let _progress = progress;

            tracing::info!("sync task entered");
            let sync_result = async {
                tracing::info!("constructing RetrievalClient::from_env");
                let client = crate::retrieval::client::RetrievalClient::from_env().await?;
                tracing::info!("RetrievalClient ready, calling sync_project");
                let opts = crate::retrieval::sync::SyncOpts {
                    force_reindex: force,
                    record_index_state: true,
                    ..Default::default()
                };
                client.sync_project(&project_id, &root, opts).await
            }
            .await;

            // Drop the MutexGuard before any `.await` — MutexGuard is !Send.
            {
                let mut state = state_arc.lock().unwrap_or_else(|e| e.into_inner());
                *state = match sync_result {
                    Ok(report) => {
                        tracing::info!(
                            added = report.added,
                            deleted = report.deleted,
                            elapsed_ms = report.elapsed_ms,
                            "sync task succeeded",
                        );
                        // Indexing succeeded — files are now fresh, clear the dirty set.
                        if let Some(ref arc) = dirty_files_arc {
                            arc.lock().unwrap_or_else(|e| e.into_inner()).clear();
                        }
                        IndexingState::Done {
                            files_indexed: report.added + report.updated,
                            files_deleted: report.deleted,
                            detail: format!("elapsed_ms={}", report.elapsed_ms),
                            // Total counts now live in Qdrant — IndexStatus
                            // re-route (task #91) will scroll the collection
                            // for these. For now leave 0 to avoid a sqlite
                            // round-trip that step 8 will delete anyway.
                            total_files: 0,
                            total_chunks: 0,
                        }
                    }
                    Err(e) => {
                        tracing::error!(error = %e, "sync task failed");
                        IndexingState::Failed(e.to_string())
                    }
                };
            }

            // Clear the abort handle slot — the task is done, nothing to cancel.
            *sync_abort_for_task
                .lock()
                .unwrap_or_else(|e| e.into_inner()) = None;
        });
        *sync_abort_for_store
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = Some(task.abort_handle());

        Ok(json!({
            "status": "started",
            "hint": "Indexing is running in the background. Use index(action='status') to check when complete."
        }))
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        Some(format_index_project(result))
    }

    fn availability(&self, _caps: &crate::tools::ToolCapabilities) -> crate::tools::Availability {
        crate::tools::Availability::RequiresEmbeddings
    }
}

#[async_trait::async_trait]
impl Tool for IndexStatus {
    fn name(&self) -> &str {
        "index_status"
    }
    fn description(&self) -> &str {
        "Show index stats: file count, chunk count, model, last update."
    }
    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {}
        })
    }
    async fn call(&self, _input: Value, ctx: &ToolContext) -> anyhow::Result<Value> {
        let project_id = ctx
            .agent
            .with_project_at(ctx.workspace_override.as_deref(), |p| {
                Ok(p.project_id().to_string())
            })
            .await?;

        // Try the Qdrant-backed status. If the retrieval stack is offline or
        // the project has no chunks indexed, return a "not indexed" envelope
        // that callers can branch on the same way they did against the
        // legacy sqlite "no db" path.
        let mut result = match crate::retrieval::client::RetrievalClient::from_env().await {
            Ok(client) => {
                let collection = client.config.collection("code_chunks");
                match client.project_index_stats(&collection, &project_id).await {
                    Ok((0, 0)) => json!({
                        "indexed": false,
                        "project_id": project_id,
                        "message": format!(
                            "No chunks indexed for project '{project_id}' in collection '{collection}'. Run index(action='build')."
                        ),
                    }),
                    Ok((chunk_count, file_count)) => json!({
                        "indexed": true,
                        "queryable": true,
                        "project_id": project_id,
                        "collection": collection,
                        "file_count": file_count,
                        "chunk_count": chunk_count,
                    }),
                    Err(e) => json!({
                        "indexed": false,
                        "project_id": project_id,
                        "message": format!("Qdrant scroll failed: {e}"),
                    }),
                }
            }
            Err(e) => json!({
                "indexed": false,
                "project_id": project_id,
                "message": format!(
                    "Retrieval stack offline: {e}. Run scripts/retrieval-stack.sh up."
                ),
            }),
        };

        // Append background indexing state (agent-tracked, independent of
        // the Qdrant collection state — surfaces in-flight `index(build)`
        // progress and the completion summary).
        {
            use crate::agent::IndexingState;
            let state = ctx.agent.indexing.lock().unwrap_or_else(|e| e.into_inner());
            match &*state {
                IndexingState::Idle => {}
                IndexingState::Running {
                    done,
                    total,
                    eta_secs,
                } => {
                    let mut indexing = json!({
                        "status": "running",
                        "done": done,
                        "total": total,
                        "eta_secs": eta_secs,
                    });
                    // Project-scope builds (sync_project) don't stream per-file
                    // progress, so done/total stay at 0/0 for the whole build —
                    // see IndexProject::call. Without a note this reads as a
                    // stall. chunk_count climbs from 0 on an initial build; on a
                    // force re-embed it stays ~stable (chunks upserted in place),
                    // so the note is scenario-honest rather than promising movement.
                    if *done == 0 && *total == 0 {
                        indexing["note"] = json!(
                            "0/0 is the healthy in-progress shape for project-scope \
                             builds (per-file progress isn't streamed), not a stall. \
                             chunk_count climbs from 0 on an initial build; on a \
                             force re-embed it stays ~stable (chunks upserted in place)."
                        );
                        indexing["chunks_so_far"] = result
                            .get("chunk_count")
                            .and_then(|v| v.as_u64())
                            .map_or_else(|| json!(0), |cc| json!(cc));
                    }
                    result["indexing"] = indexing;
                }
                IndexingState::Done {
                    files_indexed,
                    files_deleted,
                    detail,
                    total_files,
                    total_chunks,
                } => {
                    // The producer leaves total_files/total_chunks at 0 (see
                    // IndexProject::call, task #91) — the authoritative totals
                    // are the top-level Qdrant file_count/chunk_count above.
                    let total_files = resolve_done_total(&result, "file_count", *total_files);
                    let total_chunks = resolve_done_total(&result, "chunk_count", *total_chunks);
                    result["indexing"] = json!({
                        "status": "done",
                        "files_indexed": files_indexed,
                        "files_deleted": files_deleted,
                        "detail": detail,
                        "total_files": total_files,
                        "total_chunks": total_chunks,
                    });
                }
                IndexingState::Failed(e) => {
                    result["indexing"] = json!({ "status": "failed", "error": e });
                }
            }
        }

        // Per-library indexing states (agent-tracked, non-idle only).
        let lib_states = ctx.agent.library_states_summary();
        if !lib_states.is_empty() {
            result["libraries"] = serde_json::to_value(&lib_states)?;
        }

        // Index freshness vs git HEAD — surfaces external checkout/pull/HEAD
        // moves that the on-edit reindex never observes. Resilient: any failure
        // (no project root, no sidecar, non-git root) simply omits git_sync.
        if result["indexed"].as_bool() == Some(true) {
            if let Ok(root) = ctx
                .agent
                .require_project_root_for(ctx.workspace_override.as_deref())
                .await
            {
                if let Some(gs) = crate::retrieval::index_state::git_sync_status(&root) {
                    result["git_sync"] = gs;
                }
            }
        }

        Ok(result)
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        Some(format_index_status(result))
    }

    fn availability(&self, _caps: &crate::tools::ToolCapabilities) -> crate::tools::Availability {
        crate::tools::Availability::RequiresEmbeddings
    }
}

#[async_trait::async_trait]
impl Tool for Index {
    fn name(&self) -> &str {
        "index"
    }

    fn is_write(&self, input: &Value) -> bool {
        input.get("action").and_then(Value::as_str) == Some("build")
    }

    fn description(&self) -> &str {
        "Semantic index operations. Actions: \
             `build` (build/update the project's semantic index; pass `scope='lib:<name>'` to index a registered library), \
             `status` (show index stats), \
             `cancel` (abort an in-flight reindex — no-op if nothing is running)."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["build", "status", "cancel"],
                    "description": "Operation to perform."
                },
                "force": {
                    "type": "boolean",
                    "default": false,
                    "description": "For action='build': force full reindex, ignoring cached file hashes."
                },
                "scope": {
                    "type": "string",
                    "default": "project",
                    "description": "For action='build': 'project' (default) or 'lib:<name>' to index a registered library."
                }
            },
            "required": ["action"]
        })
    }

    async fn call(&self, input: Value, ctx: &ToolContext) -> anyhow::Result<Value> {
        let action = input
            .get("action")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                crate::tools::RecoverableError::with_hint(
                    "index requires 'action' parameter",
                    "Pass action='build' or action='status'.",
                )
            })?;
        match action {
            "build" => IndexProject.call(input, ctx).await,
            "status" => IndexStatus.call(input, ctx).await,
            "cancel" => {
                let handle = ctx
                    .agent
                    .active_sync_abort
                    .lock()
                    .unwrap_or_else(|e| e.into_inner())
                    .take();
                match handle {
                    Some(h) => {
                        h.abort();
                        // Aborted future won't reach its terminal-state arm —
                        // reset IndexingState here so status reflects reality.
                        *ctx.agent.indexing.lock().unwrap_or_else(|e| e.into_inner()) =
                            crate::agent::IndexingState::Failed("cancelled by user".into());
                        tracing::info!("sync task cancelled by user");
                        Ok(json!({"status": "cancelled"}))
                    }
                    None => Ok(json!({"status": "no_active_sync"})),
                }
            }
            other => Err(crate::tools::RecoverableError::with_hint(
                format!("unknown index action: {}", other),
                "Valid actions: 'build', 'status', 'cancel'.",
            )
            .into()),
        }
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        if result.get("indexed").is_some() || result.get("file_count").is_some() {
            IndexStatus.format_compact(result)
        } else {
            IndexProject.format_compact(result)
        }
    }

    fn availability(&self, caps: &crate::tools::ToolCapabilities) -> crate::tools::Availability {
        IndexProject.availability(caps)
    }
}

fn format_index_project(result: &Value) -> String {
    let status = result["status"].as_str().unwrap_or("?");
    format!("index {status}")
}
pub(crate) fn format_index_status(result: &Value) -> String {
    let indexed = result["indexed"].as_bool().unwrap_or(false);
    if !indexed {
        return "not indexed".to_string();
    }
    let files = result["file_count"].as_u64().unwrap_or(0);
    let chunks = result["chunk_count"].as_u64().unwrap_or(0);

    let mut out = format!("good · queryable · {files} files · {chunks} chunks");

    if let Some(model) = result["indexed_with_model"].as_str() {
        out.push_str(&format!(" · {model}"));
    }
    if let Some(ts) = result["indexed_at"].as_str() {
        out.push_str(&format!(" · {ts}"));
    }
    if result["git_sync"]["status"].as_str() == Some("behind") {
        if let Some(behind) = result["git_sync"]["behind_commits"]
            .as_u64()
            .filter(|&n| n > 0)
        {
            out.push_str(&format!(
                " · {behind} commits not yet indexed (queryable, run index(action='build') to catch up)"
            ));
        }
    }
    out
}

/// Resolve a `done`-state total for `IndexStatus` output.
///
/// The sync-task completion path leaves `IndexingState::Done.total_files` /
/// `.total_chunks` at 0 (see `IndexProject::call`, task #91) — the authoritative
/// totals live in Qdrant and are already surfaced as the top-level `file_count`
/// / `chunk_count`. Prefer those so the `done` summary matches reality; fall
/// back to the placeholder variant value only when Qdrant didn't supply a count
/// (offline / not yet indexed).
pub(crate) fn resolve_done_total(result: &Value, key: &str, fallback: usize) -> u64 {
    result
        .get(key)
        .and_then(serde_json::Value::as_u64)
        .unwrap_or(fallback as u64)
}