gobby-wiki 0.7.0

Gobby wiki CLI shell
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
use std::path::{Path, PathBuf};

#[cfg(feature = "ai")]
use gobby_core::ai::effective_route;
use gobby_core::ai_context::{AiConfigSource, AiContext, LocalAiConfigSource};
use gobby_core::config::{
    AiCapability, AiRouting, ConfigSource, resolve_embedding_config, resolve_falkordb_config,
    resolve_qdrant_config,
};
use gobby_core::degradation::{DegradationKind, ServiceState};
use postgres::Client;
use serde_json::json;

use crate::ingest::{self, IngestResult};
use crate::search::SearchScope;
use crate::support::config::{index_options_from_conn, local_index_options, qdrant_config_has_url};
use crate::support::counts::{IndexCounts, index_counts, postgres_index_counts};
use crate::support::env::database_url_for;
use crate::support::scope::{
    resolve_command_scope, resolved_scope_identity, search_scope_for_resolved,
    store_scope_for_search,
};
use crate::support::search::PostgresConfigSource;
use crate::support::text::degradation_label;
use crate::support::time::collect_timestamp;
use crate::{
    CommandOutcome, IngestFileOptions, ScopeIdentity, ScopeKind, ScopeSelection, WikiError,
    indexer, store, vault, vector,
};

const VIDEO_FRAME_INTERVAL_KEY: &str = "gwiki.ingest.video_frame_interval_seconds";
const QDRANT_SERVICE: &str = "qdrant";
const FALKORDB_SERVICE: &str = "falkordb";

struct IndexReport {
    counts: IndexCounts,
    degradations: Vec<DegradationKind>,
}

pub(crate) fn execute(selection: ScopeSelection) -> Result<CommandOutcome, WikiError> {
    let scope = resolve_command_scope(&selection)?;
    ensure_scope_root(&scope)?;
    let output_scope = resolved_scope_identity(&scope);
    let report = index_resolved_scope_report(&scope)?;
    Ok(render_index(output_scope, scope.root(), report))
}

pub(crate) fn index_resolved_scope(
    scope: &crate::scope::ResolvedScope,
) -> Result<IndexCounts, WikiError> {
    Ok(index_resolved_scope_report(scope)?.counts)
}

fn index_resolved_scope_report(
    scope: &crate::scope::ResolvedScope,
) -> Result<IndexReport, WikiError> {
    if let Some(database_url) = database_url_for("gwiki index")? {
        let mut conn = connect_postgres_index(&database_url, "gwiki index")?;
        let search_scope = search_scope_for_resolved(scope);
        let index_options = index_options_from_conn(&mut conn)?;
        {
            let mut store = postgres_store_for_search(&mut conn, &search_scope);
            indexer::index_vault_with_options(scope.root(), &mut store, index_options)?;
        }
        let mut degradations = Vec::new();
        if let Some(degradation) = sync_qdrant_vectors(&mut conn, &search_scope, "gwiki index")? {
            degradations.push(degradation);
        }
        if let Some(degradation) = sync_falkor_graph(&mut conn, &search_scope, "gwiki index")? {
            degradations.push(degradation);
        }
        let counts = indexed_counts_for_postgres(&mut conn, &search_scope, true)?;
        return Ok(IndexReport {
            counts,
            degradations,
        });
    }

    let index_options = local_index_options()?;
    let mut store = store::MemoryWikiStore::default();
    indexer::index_vault_with_options(scope.root(), &mut store, index_options)?;
    Ok(IndexReport {
        counts: index_counts(&store),
        degradations: Vec::new(),
    })
}

pub(crate) fn execute_ingest_file(
    path: PathBuf,
    selection: ScopeSelection,
    options: IngestFileOptions,
) -> Result<CommandOutcome, WikiError> {
    let scope = resolve_command_scope(&selection)?;
    // Vault initialization is idempotent here; ingest only needs the paths to exist.
    let initialized = vault::initialize(&scope)?;
    if !initialized.directories.is_empty() || !initialized.files.is_empty() {
        log::debug!(
            "initialized gwiki vault paths before ingest-file: directories={:?} files={:?}",
            initialized.directories,
            initialized.files
        );
    }
    let output_scope = resolved_scope_identity(&scope);
    let project_id = ai_project_id(&output_scope);
    let gobby_home = gobby_home()?;
    let fetched_at = collect_timestamp()?;
    if let Some(database_url) = database_url_for("gwiki ingest-file")? {
        let mut conn = connect_postgres_index(&database_url, "gwiki ingest-file")?;
        let (ai_context, options) = {
            let primary = PostgresConfigSource { conn: &mut conn };
            let mut source = AiConfigSource::with_primary_from_gobby_home(primary, &gobby_home)
                .map_err(|error| WikiError::Config {
                    detail: format!("failed to resolve AI config for gwiki ingest-file: {error}"),
                })?;
            resolve_ingest_ai_context(project_id, &options, &mut source)?
        };
        let search_scope = search_scope_for_resolved(&scope);
        let result = {
            let mut store = postgres_store_for_search(&mut conn, &search_scope);
            ingest::file::ingest_path(
                scope.root(),
                &mut store,
                &output_scope,
                &ai_context,
                &options,
                &path,
                &fetched_at,
            )?
        };
        sync_qdrant_vectors(&mut conn, &search_scope, "gwiki ingest-file")?;
        let counts = indexed_counts_for_postgres(&mut conn, &search_scope, true)?;
        sync_falkor_graph(&mut conn, &search_scope, "gwiki ingest-file")?;
        return Ok(render_ingest_file(&path, output_scope, &result, counts));
    }

    let mut source =
        LocalAiConfigSource::from_gobby_home(&gobby_home).map_err(|error| WikiError::Config {
            detail: format!("failed to resolve AI config for gwiki ingest-file: {error}"),
        })?;
    let (ai_context, options) = resolve_ingest_ai_context(project_id, &options, &mut source)?;
    let mut store = store::MemoryWikiStore::default();
    let result = ingest::file::ingest_path(
        scope.root(),
        &mut store,
        &output_scope,
        &ai_context,
        &options,
        &path,
        &fetched_at,
    )?;
    let counts = index_counts(&store);
    Ok(render_ingest_file(&path, output_scope, &result, counts))
}

pub(crate) fn execute_ingest_url(
    urls: Vec<String>,
    selection: ScopeSelection,
) -> Result<CommandOutcome, WikiError> {
    let scope = resolve_command_scope(&selection)?;
    // Vault initialization is idempotent here; ingest only needs the paths to exist.
    let initialized = vault::initialize(&scope)?;
    if !initialized.directories.is_empty() || !initialized.files.is_empty() {
        log::debug!(
            "initialized gwiki vault paths before ingest-url: directories={:?} files={:?}",
            initialized.directories,
            initialized.files
        );
    }
    let output_scope = resolved_scope_identity(&scope);
    let fetched_at = collect_timestamp()?;
    if let Some(database_url) = database_url_for("gwiki ingest-url")? {
        let mut conn = connect_postgres_index(&database_url, "gwiki ingest-url")?;
        let search_scope = search_scope_for_resolved(&scope);
        let result = {
            let mut store = postgres_store_for_search(&mut conn, &search_scope);
            ingest::url::ingest_urls(scope.root(), &mut store, &urls, &fetched_at)?
        };
        let counts =
            indexed_counts_for_postgres(&mut conn, &search_scope, !result.accepted.is_empty())?;
        if !result.accepted.is_empty() {
            sync_qdrant_vectors(&mut conn, &search_scope, "gwiki ingest-url")?;
            sync_falkor_graph(&mut conn, &search_scope, "gwiki ingest-url")?;
        }
        return Ok(render_ingest_url(output_scope, &result, counts));
    }

    let mut store = store::MemoryWikiStore::default();
    let result = ingest::url::ingest_urls(scope.root(), &mut store, &urls, &fetched_at)?;
    let counts = index_counts(&store);
    Ok(render_ingest_url(output_scope, &result, counts))
}

fn resolve_ingest_ai_context(
    project_id: Option<String>,
    options: &IngestFileOptions,
    source: &mut impl ConfigSource,
) -> Result<(AiContext, IngestFileOptions), WikiError> {
    let mut context = AiContext::resolve(project_id, source);
    let mut options = options.clone();
    if options.video_frame_interval_seconds.is_none() {
        options.video_frame_interval_seconds = Some(resolve_video_frame_interval_seconds(source)?);
    }
    options.apply_to_ai_context(&mut context);
    Ok((context, options))
}

pub(crate) fn resolve_ingest_file_ai_context(
    scope: &ScopeIdentity,
    options: &IngestFileOptions,
    command: &str,
) -> Result<(AiContext, IngestFileOptions), WikiError> {
    let project_id = ai_project_id(scope);
    let gobby_home = gobby_home()?;
    if let Some(database_url) = database_url_for(command)? {
        let mut conn = connect_postgres_index(&database_url, command)?;
        let primary = PostgresConfigSource { conn: &mut conn };
        let mut source = AiConfigSource::with_primary_from_gobby_home(primary, &gobby_home)
            .map_err(|error| WikiError::Config {
                detail: format!("failed to resolve AI config for {command}: {error}"),
            })?;
        return resolve_ingest_ai_context(project_id, options, &mut source);
    }

    let mut source =
        LocalAiConfigSource::from_gobby_home(&gobby_home).map_err(|error| WikiError::Config {
            detail: format!("failed to resolve AI config for {command}: {error}"),
        })?;
    resolve_ingest_ai_context(project_id, options, &mut source)
}

fn resolve_video_frame_interval_seconds(source: &mut impl ConfigSource) -> Result<u32, WikiError> {
    let Some(raw_value) = source.config_value(VIDEO_FRAME_INTERVAL_KEY) else {
        return Ok(ingest::video::DEFAULT_FRAME_INTERVAL_SECONDS);
    };
    let value = source
        .resolve_value(&raw_value)
        .map_err(|error| WikiError::Config {
            detail: format!("failed to resolve {VIDEO_FRAME_INTERVAL_KEY}: {error}"),
        })?;
    let interval = value
        .trim()
        .parse::<u32>()
        .map_err(|error| WikiError::Config {
            detail: format!("invalid {VIDEO_FRAME_INTERVAL_KEY} value `{value}`: {error}"),
        })?;
    if interval == 0 {
        return Err(WikiError::Config {
            detail: format!(
                "invalid {VIDEO_FRAME_INTERVAL_KEY} value `{value}`: must be greater than 0"
            ),
        });
    }
    Ok(interval)
}

fn ai_project_id(scope: &ScopeIdentity) -> Option<String> {
    (scope.kind == ScopeKind::Project).then(|| scope.id.clone())
}

fn ai_project_id_for_search(scope: &SearchScope) -> Option<String> {
    match scope {
        SearchScope::Global => None,
        SearchScope::Project { project_id } => Some(project_id.clone()),
        SearchScope::Topic { .. } => None,
    }
}

fn gobby_home() -> Result<PathBuf, WikiError> {
    gobby_core::gobby_home().map_err(|error| WikiError::Config {
        detail: format!("failed to resolve Gobby home for gwiki AI config: {error}"),
    })
}

pub(crate) fn connect_postgres_index(
    database_url: &str,
    command: &str,
) -> Result<Client, WikiError> {
    gobby_core::postgres::connect_readwrite(database_url).map_err(|error| WikiError::Config {
        detail: format!("failed to connect to PostgreSQL for {command}: {error}"),
    })
}

pub(crate) fn postgres_store_for_search<'a>(
    conn: &'a mut Client,
    search_scope: &SearchScope,
) -> store::PostgresWikiStore<'a> {
    store::PostgresWikiStore::new(conn, store_scope_for_search(search_scope))
}

pub(crate) fn sync_falkor_graph(
    conn: &mut Client,
    search_scope: &SearchScope,
    command: &'static str,
) -> Result<Option<DegradationKind>, WikiError> {
    let gobby_home = gobby_home()?;
    let primary = PostgresConfigSource { conn };
    let mut source =
        AiConfigSource::with_primary_from_gobby_home(primary, &gobby_home).map_err(|error| {
            WikiError::Config {
                detail: format!("failed to resolve FalkorDB config for {command}: {error}"),
            }
        })?;
    let Some(falkor) = resolve_falkordb_config(&mut source) else {
        log::warn!("{command}: FalkorDB config not found; skipping gwiki graph sync");
        return Ok(Some(not_configured_degradation(FALKORDB_SERVICE)));
    };
    if let Err(error) = crate::falkor_graph::sync_scope_from_postgres(conn, search_scope, &falkor) {
        log::warn!(
            "{command}: FalkorDB graph sync failed; continuing with PostgreSQL index: {error}"
        );
        return Ok(Some(unreachable_degradation(FALKORDB_SERVICE, error)));
    }
    Ok(None)
}

pub(crate) fn sync_qdrant_vectors(
    conn: &mut Client,
    search_scope: &SearchScope,
    command: &'static str,
) -> Result<Option<DegradationKind>, WikiError> {
    let gobby_home = gobby_home()?;
    let (embedding, qdrant) = {
        let primary = PostgresConfigSource { conn };
        let mut source = AiConfigSource::with_primary_from_gobby_home(primary, &gobby_home)
            .map_err(|error| WikiError::Config {
                detail: format!("failed to resolve AI config for {command}: {error}"),
            })?;
        let ai_context = AiContext::resolve(ai_project_id_for_search(search_scope), &mut source);
        let embedding = resolve_vector_embedding(&ai_context, &mut source);
        let qdrant = resolve_qdrant_config(&mut source).filter(qdrant_config_has_url);
        (embedding, qdrant)
    };

    let Some(embedding) = embedding else {
        log::warn!("{command}: embedding config not found; skipping gwiki vector sync");
        return Ok(Some(not_configured_degradation(QDRANT_SERVICE)));
    };
    let Some(qdrant) = qdrant else {
        log::warn!("{command}: Qdrant config not found; skipping gwiki vector sync");
        return Ok(Some(not_configured_degradation(QDRANT_SERVICE)));
    };

    let mut source = vector::PostgresWikiVectorChunkSource::new(conn);
    let mut embedder = vector::GwikiEmbeddingBackend::new(embedding);
    let mut store = vector::GwikiQdrantVectorStore::new(qdrant);
    let outcome = match vector::sync_scope_vectors(
        search_scope,
        &mut source,
        &mut embedder,
        &mut store,
    ) {
        Ok(outcome) => outcome,
        Err(error) => {
            log::warn!(
                "{command}: Qdrant vector sync failed; continuing with PostgreSQL index: {error}"
            );
            return Ok(Some(qdrant_sync_degradation(error)));
        }
    };
    log::debug!(
        "{command}: synced gwiki Qdrant vectors: chunks={} upserted={} stale_paths_deleted={}",
        outcome.chunks,
        outcome.upserted,
        outcome.deleted_stale_paths
    );
    Ok(None)
}

fn qdrant_sync_degradation(error: vector::WikiVectorError) -> DegradationKind {
    unreachable_degradation(QDRANT_SERVICE, error)
}

fn not_configured_degradation(service: &'static str) -> DegradationKind {
    service_unavailable_degradation(service, ServiceState::NotConfigured)
}

fn unreachable_degradation(
    service: &'static str,
    error: impl std::fmt::Display,
) -> DegradationKind {
    service_unavailable_degradation(
        service,
        ServiceState::Unreachable {
            message: error.to_string(),
        },
    )
}

fn service_unavailable_degradation(service: &'static str, state: ServiceState) -> DegradationKind {
    DegradationKind::ServiceUnavailable {
        service: service.to_string(),
        state,
    }
}

fn resolve_vector_embedding(
    context: &AiContext,
    source: &mut impl ConfigSource,
) -> Option<crate::search::semantic::SemanticEmbedding> {
    match effective_embedding_route(context) {
        AiRouting::Off => None,
        AiRouting::Daemon => {
            #[cfg(feature = "ai")]
            {
                Some(crate::search::semantic::SemanticEmbedding::Daemon(
                    Box::new(context.clone()),
                ))
            }
            #[cfg(not(feature = "ai"))]
            {
                None
            }
        }
        AiRouting::Direct => {
            resolve_embedding_config(source).map(crate::search::semantic::SemanticEmbedding::Direct)
        }
        AiRouting::Auto => {
            #[cfg(feature = "ai")]
            {
                Some(crate::search::semantic::SemanticEmbedding::Daemon(
                    Box::new(context.clone()),
                ))
            }
            #[cfg(not(feature = "ai"))]
            {
                resolve_embedding_config(source)
                    .map(crate::search::semantic::SemanticEmbedding::Direct)
            }
        }
    }
}

fn effective_embedding_route(context: &AiContext) -> AiRouting {
    #[cfg(feature = "ai")]
    {
        effective_route(context, AiCapability::Embed)
    }
    #[cfg(not(feature = "ai"))]
    {
        match context.binding(AiCapability::Embed).routing {
            AiRouting::Off => AiRouting::Off,
            AiRouting::Direct => AiRouting::Direct,
            AiRouting::Daemon => {
                log::warn!(
                    "gwiki was built without ai support; daemon-backed embeddings are disabled"
                );
                AiRouting::Off
            }
            AiRouting::Auto => {
                log::warn!(
                    "gwiki was built without ai support; auto embedding route cannot use the daemon"
                );
                AiRouting::Direct
            }
        }
    }
}

pub(crate) fn indexed_counts_for_postgres(
    conn: &mut Client,
    search_scope: &SearchScope,
    should_count: bool,
) -> Result<IndexCounts, WikiError> {
    if should_count {
        postgres_index_counts(conn, search_scope)
    } else {
        Ok(IndexCounts::default())
    }
}

fn render_index(scope: ScopeIdentity, root: &Path, report: IndexReport) -> CommandOutcome {
    let IndexReport {
        counts,
        degradations,
    } = report;
    let degradation_labels = degradations
        .iter()
        .map(degradation_label)
        .collect::<Vec<_>>();
    let payload = json!({
        "command": "index",
        "scope": scope,
        "status": "indexed",
        "root": root.display().to_string(),
        "indexed": {
            "documents": counts.documents,
            "chunks": counts.chunks,
            "links": counts.links,
            "sources": counts.sources,
            "ingestions": counts.ingestions,
        },
        "degradations": degradations,
    });
    let degradations_text = if degradation_labels.is_empty() {
        "none".to_string()
    } else {
        degradation_labels.join(", ")
    };
    let mut text = format!(
        "Index complete
Scope: {scope}
Documents: {}
Chunks: {}
Links: {}
Sources: {}
Ingestions: {}",
        counts.documents, counts.chunks, counts.links, counts.sources, counts.ingestions
    );
    text.push_str("\nDegradations: ");
    text.push_str(&degradations_text);
    super::scoped_outcome("index", &scope, payload, text)
}

fn render_ingest_file(
    path: &Path,
    scope: ScopeIdentity,
    result: &IngestResult,
    counts: IndexCounts,
) -> CommandOutcome {
    let payload = json!({
        "command": "ingest-file",
        "scope": scope,
        "status": "ingested",
        "path": path,
        "raw_path": &result.raw_path,
        "asset_path": &result.asset_path,
        "source": {
            "id": &result.record.id,
            "kind": &result.record.kind,
            "content_hash": &result.record.content_hash,
            "location": &result.record.location,
        },
        "indexed": {
            "documents": counts.documents,
            "chunks": counts.chunks,
            "links": counts.links,
            "sources": counts.sources,
            "ingestions": counts.ingestions,
        },
    });
    let text = format!(
        "Ingested file
Scope: {scope}
Raw: {}
Asset: {}
Source: {} ({})
Content hash: {}
Documents: {}
Chunks: {}
Links: {}
Sources: {}
Ingestions: {}",
        ingest::path_to_string(&result.raw_path),
        result
            .asset_path
            .as_ref()
            .map(|path| ingest::path_to_string(path))
            .unwrap_or_else(|| "<none>".to_string()),
        result.record.location,
        result.record.kind,
        result.record.content_hash,
        counts.documents,
        counts.chunks,
        counts.links,
        counts.sources,
        counts.ingestions
    );
    super::scoped_outcome("ingest-file", &scope, payload, text)
}

fn render_ingest_url(
    scope: ScopeIdentity,
    result: &ingest::url::UrlBatchIngest,
    counts: IndexCounts,
) -> CommandOutcome {
    let accepted = result
        .accepted
        .iter()
        .map(|accepted| {
            json!({
                "requested_url": &accepted.requested_url,
                "final_url": &accepted.final_url,
                "raw_path": &accepted.result.raw_path,
                "source": {
                    "id": &accepted.result.record.id,
                    "kind": &accepted.result.record.kind,
                    "content_hash": &accepted.result.record.content_hash,
                    "location": &accepted.result.record.location,
                },
            })
        })
        .collect::<Vec<_>>();
    let failed = result
        .failed
        .iter()
        .map(|failure| {
            json!({
                "url": &failure.url,
                "code": &failure.code,
                "message": &failure.message,
            })
        })
        .collect::<Vec<_>>();
    let payload = json!({
        "command": "ingest-url",
        "scope": scope,
        "status": result.status(),
        "accepted": accepted,
        "failed": failed,
        "indexed": {
            "documents": counts.documents,
            "chunks": counts.chunks,
            "links": counts.links,
            "sources": counts.sources,
            "ingestions": counts.ingestions,
        },
    });
    let text = format!(
        "Ingested URLs
Scope: {scope}
Status: {}
Accepted: {}
Failed: {}
Documents: {}
Chunks: {}
Links: {}
Sources: {}
Ingestions: {}",
        result.status(),
        result.accepted.len(),
        result.failed.len(),
        counts.documents,
        counts.chunks,
        counts.links,
        counts.sources,
        counts.ingestions
    );
    let mut outcome = super::scoped_outcome("ingest-url", &scope, payload, text);
    outcome.exit_code = result.exit_code();
    outcome
}

fn ensure_scope_root(scope: &crate::scope::ResolvedScope) -> Result<(), WikiError> {
    if scope.root().is_dir() {
        return Ok(());
    }
    Err(WikiError::InvalidScope {
        detail: format!(
            "wiki scope root is missing or not a directory: {}",
            scope.root().display()
        ),
    })
}

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

    struct TestConfigSource {
        value: Option<&'static str>,
    }

    impl ConfigSource for TestConfigSource {
        fn config_value(&mut self, key: &str) -> Option<String> {
            (key == VIDEO_FRAME_INTERVAL_KEY)
                .then(|| self.value.map(str::to_string))
                .flatten()
        }

        fn resolve_value(&mut self, value: &str) -> anyhow::Result<String> {
            Ok(value.to_string())
        }
    }

    #[test]
    fn video_frame_interval_zero_is_invalid() {
        let mut source = TestConfigSource { value: Some("0") };
        let error = resolve_video_frame_interval_seconds(&mut source)
            .expect_err("zero interval must be invalid");

        assert!(matches!(error, WikiError::Config { .. }));
        assert!(error.to_string().contains("must be greater than 0"));
    }

    #[test]
    fn index_render_includes_empty_degradations() {
        let outcome = render_index(
            ScopeIdentity::project("project-1"),
            Path::new("/vault"),
            IndexReport {
                counts: sample_counts(),
                degradations: Vec::new(),
            },
        );

        assert!(
            outcome.result.payload["degradations"]
                .as_array()
                .expect("degradations array")
                .is_empty()
        );
        assert!(outcome.result.text.contains("Degradations: none"));
    }

    #[test]
    fn index_render_reports_qdrant_sync_failure_degradation() {
        let outcome = render_index(
            ScopeIdentity::project("project-1"),
            Path::new("/vault"),
            IndexReport {
                counts: sample_counts(),
                degradations: vec![qdrant_sync_degradation(vector::WikiVectorError::Qdrant(
                    "connection refused".to_string(),
                ))],
            },
        );

        let degradation = &outcome.result.payload["degradations"][0]["ServiceUnavailable"];
        assert_eq!(degradation["service"], "qdrant");
        assert_eq!(
            degradation["state"]["Unreachable"]["message"],
            "wiki vector qdrant error: connection refused"
        );
        assert!(
            outcome
                .result
                .text
                .contains("Degradations: qdrant_unreachable")
        );
    }

    #[test]
    #[cfg(not(feature = "ai"))]
    fn auto_embedding_route_falls_back_to_direct_without_ai() {
        let mut source = TestConfigSource { value: None };
        let context = AiContext::resolve(None, &mut source);

        assert_eq!(effective_embedding_route(&context), AiRouting::Direct);
    }

    fn sample_counts() -> IndexCounts {
        IndexCounts {
            documents: 3,
            chunks: 5,
            links: 7,
            sources: 11,
            ingestions: 13,
        }
    }
}