gobby-code 1.3.2

Fast Rust CLI for Gobby's code index — AST-aware search, symbol navigation, and dependency graph
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
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Write as _;
use std::path::Path;

use crate::index::hasher;
use crate::models::Symbol;

use super::{
    AiDepth, AuditContext, BuiltDoc, CodewikiGraphEdge, CodewikiGraphEdgeKind, CodewikiInput,
    CodewikiProgress, DocPruneScope, FeatureCatalogDoc, FileDoc, FileDocPosition, LeadingChunk,
    ModuleDoc, OwnershipMeta, OwnershipOptions, ReusePlan, SourceSpan, SystemModel, TextGenerator,
    TextVerifier, build_architecture_doc, build_curated_navigation_docs, build_deprecations_doc,
    build_file_doc, build_hotspots_doc, build_infrastructure_doc, build_module_docs_with_filter,
    build_onboarding_doc, build_ownership_doc, build_repo_doc, cluster, cluster_file_modules,
    file_doc_path, is_core_file, module_doc_path, module_for_file, relationship_facts_for_file,
    render_architecture_doc, render_deprecations_doc, render_feature_catalog_doc, render_file_doc,
    render_hotspots_doc, render_infrastructure_doc, render_module_doc, render_onboarding_doc,
    span_files,
};

pub fn generate_hierarchical_docs(
    input: &CodewikiInput,
    generate: Option<&mut TextGenerator<'_>>,
) -> Vec<(String, String)> {
    generate_hierarchical_docs_with_graph_availability(input, generate)
        .into_iter()
        .map(|doc| (doc.path, doc.content))
        .collect()
}

fn generate_hierarchical_docs_with_graph_availability(
    input: &CodewikiInput,
    mut generate: Option<&mut TextGenerator<'_>>,
) -> Vec<BuiltDoc> {
    let mut progress = CodewikiProgress::silent();
    let doc_scope = DocPruneScope::unscoped();
    let mut docs = Vec::new();
    if let Err(error) = generate_hierarchical_docs_core(
        input,
        None,
        None,
        None,
        None,
        &mut generate,
        &mut None,
        AiDepth::Symbols,
        &mut None,
        &mut progress,
        &doc_scope,
        &mut |doc| {
            docs.push(doc);
            Ok(())
        },
    ) {
        log::warn!("codewiki generation failed without ownership metadata: {error}");
        return Vec::new();
    }
    docs
}

#[expect(clippy::too_many_arguments)]
pub(crate) fn generate_hierarchical_docs_with_ownership(
    input: &CodewikiInput,
    ownership: Option<(&Path, &mut OwnershipMeta)>,
    system_model: Option<&SystemModel>,
    feature_catalog: Option<&FeatureCatalogDoc>,
    audit: Option<&AuditContext>,
    mut generate: Option<&mut TextGenerator<'_>>,
    mut verify: Option<&mut TextVerifier<'_>>,
    ai_depth: AiDepth,
    reuse: &mut Option<&mut ReusePlan>,
    progress: &mut CodewikiProgress,
    doc_scope: &DocPruneScope,
    emit: &mut dyn FnMut(BuiltDoc) -> anyhow::Result<()>,
) -> anyhow::Result<()> {
    generate_hierarchical_docs_core(
        input,
        ownership,
        system_model,
        feature_catalog,
        audit,
        &mut generate,
        &mut verify,
        ai_depth,
        reuse,
        progress,
        doc_scope,
        emit,
    )
}

#[cfg(test)]
pub(crate) fn generate_hierarchical_docs_with_progress(
    input: &CodewikiInput,
    generate: Option<&mut TextGenerator<'_>>,
    ai_depth: AiDepth,
    progress: &mut CodewikiProgress,
) -> Vec<BuiltDoc> {
    generate_hierarchical_docs_with_reuse(input, generate, ai_depth, &mut None, progress)
}

/// Test entry point that exercises the reuse path without the CLI runtime.
#[cfg(test)]
pub(crate) fn generate_hierarchical_docs_with_reuse(
    input: &CodewikiInput,
    mut generate: Option<&mut TextGenerator<'_>>,
    ai_depth: AiDepth,
    reuse: &mut Option<&mut ReusePlan>,
    progress: &mut CodewikiProgress,
) -> Vec<BuiltDoc> {
    let doc_scope = DocPruneScope::unscoped();
    let mut docs = Vec::new();
    if let Err(error) = generate_hierarchical_docs_core(
        input,
        None,
        None,
        None,
        None,
        &mut generate,
        &mut None,
        ai_depth,
        reuse,
        progress,
        &doc_scope,
        &mut |doc| {
            docs.push(doc);
            Ok(())
        },
    ) {
        log::warn!("codewiki generation failed without ownership metadata: {error}");
        return Vec::new();
    }
    docs
}

/// Test entry point that threads a verifier alongside the generator, so the
/// grounded verification pass can be exercised end-to-end through the curated
/// page pipeline without the CLI runtime.
#[cfg(test)]
pub(crate) fn generate_hierarchical_docs_with_verify(
    input: &CodewikiInput,
    generate: Option<&mut TextGenerator<'_>>,
    verify: Option<&mut TextVerifier<'_>>,
    ai_depth: AiDepth,
) -> Vec<BuiltDoc> {
    let mut generate = generate;
    let mut verify = verify;
    let mut progress = CodewikiProgress::silent();
    let doc_scope = DocPruneScope::unscoped();
    let mut docs = Vec::new();
    if let Err(error) = generate_hierarchical_docs_core(
        input,
        None,
        None,
        None,
        None,
        &mut generate,
        &mut verify,
        ai_depth,
        &mut None,
        &mut progress,
        &doc_scope,
        &mut |doc| {
            docs.push(doc);
            Ok(())
        },
    ) {
        log::warn!("codewiki generation failed without ownership metadata: {error}");
        return Vec::new();
    }
    docs
}

/// Reference-appendix links for the deterministic analysis/catalog pages,
/// included only for the pages that will actually be emitted this run (#904).
/// Returns `(label, wikilink-target)` pairs; an absent page is never linked, so
/// the repo overview can't dangle.
fn repo_audit_links(
    has_audit: bool,
    has_feature_catalog: bool,
    has_infrastructure: bool,
) -> Vec<(&'static str, &'static str)> {
    let mut links = Vec::new();
    if has_feature_catalog {
        links.push(("Feature catalog", "code/features"));
    }
    if has_infrastructure {
        links.push(("Infrastructure stack", "code/infrastructure"));
    }
    if has_audit {
        links.push(("Deprecations", "code/deprecations"));
    }
    links
}

#[expect(
    clippy::too_many_arguments,
    reason = "core generation threads mutable generator, verifier, reuse, progress, scope, and emit state"
)]
pub(crate) fn generate_hierarchical_docs_core(
    input: &CodewikiInput,
    ownership: Option<(&Path, &mut OwnershipMeta)>,
    // Deterministic workspace system model (#891). Seeds the architecture
    // page's model-derived Mermaid diagrams. The CLI runtime passes the real
    // model built from the project root; test/AI-off entry points pass `None`
    // to omit the diagram section.
    system_model: Option<&SystemModel>,
    // Deterministic feature catalog (#888), built from the pinned CLI contract
    // JSONs + dispatch resolver. The CLI runtime passes the real catalog; the
    // test/AI-off entry points pass `None` to omit the catalog page, exactly
    // like `system_model`.
    feature_catalog: Option<&FeatureCatalogDoc>,
    // Deterministic audit context (#889): the deprecation index (stamped into
    // each file doc's symbols for the badge + the `code/deprecations.md` page)
    // and the test-gated symbol index (for the file page's test-count collapse).
    // The CLI runtime passes the real context; test/AI-off entry points pass
    // `None` to omit the deprecations page, exactly like `system_model`.
    audit: Option<&AuditContext>,
    generate: &mut Option<&mut TextGenerator<'_>>,
    verify: &mut Option<&mut TextVerifier<'_>>,
    ai_depth: AiDepth,
    reuse: &mut Option<&mut ReusePlan>,
    progress: &mut CodewikiProgress,
    doc_scope: &DocPruneScope,
    emit: &mut dyn FnMut(BuiltDoc) -> anyhow::Result<()>,
) -> anyhow::Result<()> {
    let mut files = input
        .files
        .iter()
        .filter(|file| is_core_file(file) && doc_scope.includes_file(file))
        .cloned()
        .collect::<BTreeSet<_>>();
    for symbol in &input.symbols {
        if is_core_file(&symbol.file_path) && doc_scope.includes_file(&symbol.file_path) {
            files.insert(symbol.file_path.clone());
        }
    }
    let files = files.into_iter().collect::<Vec<_>>();

    let mut symbols_by_file: BTreeMap<String, Vec<Symbol>> = BTreeMap::new();
    for symbol in &input.symbols {
        if !is_core_file(&symbol.file_path) || !doc_scope.includes_file(&symbol.file_path) {
            continue;
        }
        symbols_by_file
            .entry(symbol.file_path.clone())
            .or_default()
            .push(symbol.clone());
    }
    for symbols in symbols_by_file.values_mut() {
        symbols.sort_by_key(|symbol| (symbol.line_start, symbol.byte_start, symbol.name.clone()));
    }

    let file_modules = cluster_file_modules(&files, &symbols_by_file, &input.graph_edges);
    // Resolve graph-edge endpoints (symbol component ids) back to their symbols
    // so each file's narrative can name concrete cross-file collaborators (#885).
    let symbols_by_id = input
        .symbols
        .iter()
        .map(|symbol| (symbol.id.as_str(), symbol))
        .collect::<HashMap<&str, &Symbol>>();
    let file_verb = if ai_depth.includes_files() {
        "generating"
    } else {
        "building"
    };
    progress.emit(format!("{file_verb} file docs for {} files", files.len()));
    let file_total = files.len();
    let mut file_docs = Vec::with_capacity(file_total);
    for (index, file) in files.iter().enumerate() {
        let file_symbols = symbols_by_file.remove(file).unwrap_or_default();
        // Cross-file relationships are derived before the symbols are moved into
        // the file doc; the id set borrows them only within this block.
        let relationships = {
            let file_symbol_ids = file_symbols
                .iter()
                .map(|symbol| symbol.id.as_str())
                .collect::<HashSet<&str>>();
            relationship_facts_for_file(file, &file_symbol_ids, &symbols_by_id, &input.graph_edges)
        };
        let file_doc = build_file_doc(
            file,
            file_modules
                .get(file)
                .cloned()
                .unwrap_or_else(|| module_for_file(file)),
            file_symbols,
            input.leading_chunks.get(file),
            &relationships,
            audit.map(|audit| &audit.deprecations),
            audit.map(|audit| &audit.tests),
            generate,
            verify,
            reuse,
            ai_depth,
            progress,
            FileDocPosition {
                index: index + 1,
                total: file_total,
            },
        );
        emit(
            BuiltDoc {
                path: file_doc_path(&file_doc.path),
                content: file_doc
                    .reused_page
                    .clone()
                    .unwrap_or_else(|| render_file_doc(&file_doc)),
                degraded: file_doc.degraded,
                summary: Some(file_doc.summary.clone()),
                neighbors: BTreeSet::new(),
                invalidation_key: None,
                invalidation_key_requires_sources: false,
            }
            // Record the cross-file neighbor set so a caller/import-target edit
            // invalidates this page on the next run (#885, Leaf H).
            .with_neighbors(relationships.neighbor_files(file)),
        )?;
        file_docs.push(file_doc);
    }
    progress.emit("generating module docs");
    let module_docs = build_module_docs_with_filter(
        &file_docs,
        &input.leading_chunks,
        &input.graph_edges,
        generate,
        reuse,
        progress,
        &|module| doc_scope.includes_module(module),
        &mut |module| {
            emit(BuiltDoc {
                path: module_doc_path(&module.module),
                content: module
                    .reused_page
                    .clone()
                    .unwrap_or_else(|| render_module_doc(module)),
                degraded: module.degraded,
                summary: Some(module.summary.clone()),
                // A module aggregate invalidates through its member files'
                // source hashes (member-set + members hash), recorded as the
                // page's provenance — no separate key or neighbor set needed.
                neighbors: BTreeSet::new(),
                invalidation_key: None,
                invalidation_key_requires_sources: false,
            })
        },
    )?;
    if !doc_scope.is_unscoped() {
        return Ok(());
    }
    for doc in build_curated_navigation_docs(
        &file_docs,
        &module_docs,
        &input.leading_chunks,
        generate,
        verify,
        reuse,
        progress,
    ) {
        emit(doc)?;
    }
    // Audit/analysis pages are deterministic, input-gated projections (#904).
    // Build the infrastructure page once here (reused at its emission site
    // below) and link every page that will actually be emitted into the repo
    // overview's appendix, so they are reachable instead of orphaned.
    let infrastructure_doc = build_infrastructure_doc(system_model);
    let audit_links = repo_audit_links(
        audit.is_some(),
        feature_catalog.is_some(),
        infrastructure_doc.is_some(),
    );
    let (repo_doc, repo_degraded, repo_key) = build_repo_doc(
        &file_docs,
        &module_docs,
        &input.leading_chunks,
        &audit_links,
        generate,
        reuse,
        progress,
    );
    emit(
        BuiltDoc {
            path: "code/repo.md".to_string(),
            content: repo_doc,
            degraded: repo_degraded,
            summary: None,
            neighbors: BTreeSet::new(),
            invalidation_key: Some(repo_key),
            invalidation_key_requires_sources: true,
        }
        .with_source_sensitive_key(),
    )?;
    progress.emit("generating architecture docs");
    // Architecture is keyed by the SystemModel plus architecture prompt inputs:
    // a function-body edit leaves it alone, while graph/prose evidence changes
    // rebuild it. Test/AI-off entry points pass no model and fall back to the
    // old full source-set reuse.
    let architecture_key = system_model.map(|model| {
        architecture_invalidation_key(
            model,
            &file_docs,
            &module_docs,
            &input.graph_edges,
            &input.leading_chunks,
        )
    });
    let infrastructure_key = system_model.map(infrastructure_invalidation_key);
    let subsystem_names = cluster::subsystem_roots(&files);
    let architecture_sources = span_files(
        &module_docs
            .iter()
            .filter(|module| subsystem_names.contains(&module.module))
            .flat_map(|module| module.source_spans.iter().cloned())
            .collect::<Vec<_>>(),
    );
    let reused_architecture = match architecture_key.as_deref() {
        Some(key) => reuse
            .as_deref_mut()
            .and_then(|plan| plan.reusable_page_keyed("code/_architecture.md", key)),
        None => reuse
            .as_deref_mut()
            .and_then(|plan| plan.reusable_page("code/_architecture.md", &architecture_sources)),
    };
    let architecture_built = match reused_architecture {
        Some(page) => {
            progress.emit("reusing architecture docs (system model unchanged)");
            match architecture_key.clone() {
                Some(key) => BuiltDoc::derived("code/_architecture.md", page, key),
                None => BuiltDoc::healthy("code/_architecture.md", page),
            }
        }
        None => {
            let architecture_doc = build_architecture_doc(
                &file_docs,
                &module_docs,
                &input.graph_edges,
                &input.leading_chunks,
                system_model,
                generate,
                progress,
            );
            BuiltDoc {
                path: "code/_architecture.md".to_string(),
                content: render_architecture_doc(&architecture_doc),
                degraded: architecture_doc
                    .degraded_sources
                    .iter()
                    .any(|source| source == "model-unavailable"),
                summary: None,
                neighbors: BTreeSet::new(),
                invalidation_key: architecture_key.clone(),
                invalidation_key_requires_sources: false,
            }
        }
    };
    emit(architecture_built)?;
    // Deterministic infra-stack page (#892). Built straight from the workspace
    // system model + curated descriptors — no LLM, never degraded. Omitted when
    // no model was supplied (AI-off / test entry points), exactly like the
    // architecture diagrams.
    progress.emit("generating infrastructure docs");
    if let Some(infrastructure_doc) = infrastructure_doc {
        let content = render_infrastructure_doc(&infrastructure_doc);
        emit(match infrastructure_key.clone() {
            Some(key) => BuiltDoc::derived("code/infrastructure.md", content, key),
            None => BuiltDoc::healthy("code/infrastructure.md", content),
        })?;
    }
    // Deterministic feature catalog page (#888). Built straight from the pinned
    // CLI contract JSONs + dispatch resolver — no LLM, never degraded. Omitted
    // when no catalog was supplied (AI-off / test entry points), exactly like
    // the architecture diagrams and the infrastructure stack page.
    progress.emit("generating feature catalog");
    if let Some(catalog) = feature_catalog {
        let content = render_feature_catalog_doc(catalog);
        // Faithful "contract hash" (Leaf H, #893): the feature catalog render is
        // a pure, deterministic projection of the pinned CLI contract, so a
        // digest of its output changes exactly when the contract surface does —
        // a function-body edit leaves it untouched.
        let key = hasher::content_hash(content.as_bytes());
        emit(BuiltDoc::derived("code/features.md", content, key))?;
    }
    // Deterministic audit page (#889): the deprecation aggregate. Built straight
    // from the source scan — no LLM, NEVER degraded. Omitted when no audit
    // context was supplied (AI-off / test entry points), exactly like the
    // feature catalog.
    if let Some(audit) = audit {
        // Faithful "deprecation-set hash" (Leaf H, #893): the page is a
        // deterministic projection of the deprecation scan, so a digest of its
        // rendered output invalidates exactly on those input changes.
        progress.emit("generating deprecations docs");
        let deprecations =
            render_deprecations_doc(&build_deprecations_doc(input, &audit.deprecations));
        let deprecations_key = hasher::content_hash(deprecations.as_bytes());
        emit(BuiltDoc::derived(
            "code/deprecations.md",
            deprecations,
            deprecations_key,
        ))?;
    }
    progress.emit("generating onboarding docs");
    let onboarding_doc = build_onboarding_doc(
        &file_docs,
        &module_docs,
        &input.graph_edges,
        input.graph_availability,
    );
    emit(BuiltDoc::healthy(
        "code/_onboarding.md",
        render_onboarding_doc(&onboarding_doc),
    ))?;
    progress.emit("generating hotspots docs");
    let hotspots_doc = build_hotspots_doc(&file_docs, &input.graph_edges, input.graph_availability);
    emit(BuiltDoc::healthy(
        "code/_hotspots.md",
        render_hotspots_doc(&hotspots_doc),
    ))?;
    if let Some((project_root, ownership_meta)) = ownership {
        progress.emit("generating ownership docs");
        emit(BuiltDoc::healthy(
            "code/_ownership.md",
            build_ownership_doc(
                project_root,
                &files,
                &file_modules,
                ownership_meta,
                OwnershipOptions::default(),
            )?,
        ))?;
    }
    Ok(())
}

fn architecture_invalidation_key(
    system_model: &SystemModel,
    file_docs: &[FileDoc],
    module_docs: &[ModuleDoc],
    graph_edges: &[CodewikiGraphEdge],
    leading_chunks: &BTreeMap<String, LeadingChunk>,
) -> String {
    let mut key = String::from("architecture:v2\n");
    let _ = writeln!(key, "system={}", system_model.digest());

    for file in file_docs {
        let _ = writeln!(
            key,
            "file\t{}\t{}\t{}",
            file.path, file.module, file.summary
        );
        for span in &file.source_spans {
            push_span_key(&mut key, "file-span", span);
        }
        for component_id in &file.component_ids {
            let _ = writeln!(key, "file-component\t{}\t{}", file.path, component_id);
        }
        for symbol in &file.symbols {
            let _ = writeln!(
                key,
                "symbol\t{}\t{}\t{}\t{}",
                file.path, symbol.component_label, symbol.component_id, symbol.purpose
            );
        }
    }

    for module in module_docs {
        let _ = writeln!(key, "module\t{}\t{}", module.module, module.summary);
        for span in &module.source_spans {
            push_span_key(&mut key, "module-span", span);
        }
        for file in &module.direct_files {
            let _ = writeln!(
                key,
                "module-file\t{}\t{}\t{}",
                module.module, file.path, file.summary
            );
        }
        for child in &module.child_modules {
            let _ = writeln!(
                key,
                "module-child\t{}\t{}\t{}",
                module.module, child.module, child.summary
            );
        }
    }

    let mut edges = graph_edges.iter().collect::<Vec<_>>();
    edges.sort_by(|left, right| {
        edge_kind_key(&left.kind)
            .cmp(edge_kind_key(&right.kind))
            .then_with(|| left.source_component_id.cmp(&right.source_component_id))
            .then_with(|| left.target_component_id.cmp(&right.target_component_id))
    });
    for edge in edges {
        let _ = writeln!(
            key,
            "edge\t{}\t{}\t{}",
            edge_kind_key(&edge.kind),
            edge.source_component_id,
            edge.target_component_id
        );
    }

    for (path, chunk) in leading_chunks {
        let chunk_hash = hasher::content_hash(chunk.content.as_bytes());
        let _ = writeln!(
            key,
            "leading\t{}\t{}\t{}\t{}",
            path, chunk.line_start, chunk.line_end, chunk_hash
        );
    }

    format!("architecture:{}", hasher::content_hash(key.as_bytes()))
}

fn infrastructure_invalidation_key(system_model: &SystemModel) -> String {
    format!("infrastructure:{}", system_model.digest())
}

fn push_span_key(out: &mut String, prefix: &str, span: &SourceSpan) {
    let _ = writeln!(
        out,
        "{}\t{}\t{}\t{}",
        prefix, span.file, span.line_start, span.line_end
    );
}

fn edge_kind_key(kind: &CodewikiGraphEdgeKind) -> &'static str {
    match kind {
        CodewikiGraphEdgeKind::Call => "call",
        CodewikiGraphEdgeKind::Import => "import",
    }
}