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
use super::support::*;
use super::*;

fn generate_docs_for_scope(input: &CodewikiInput, doc_scope: &DocPruneScope) -> Vec<BuiltDoc> {
    let mut docs = Vec::new();
    let mut generate = None::<&mut TextGenerator<'_>>;
    let mut progress = CodewikiProgress::silent();
    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(())
        },
    )
    .expect("generate docs for scope");
    docs
}

#[test]
fn incremental_write_always_rewrites_docs_without_provenance() {
    let project = tempfile::tempdir().expect("project dir");
    std::fs::create_dir_all(project.path().join("src")).expect("source dir");
    std::fs::write(project.path().join("src/lib.rs"), "pub struct Client;\n").expect("write lib");
    let out_dir = project.path().join("codewiki");

    let provenance_doc =
        "---\ntitle: Lib\nprovenance:\n- file: src/lib.rs\n  ranges:\n  - '1'\n---\n# Lib\n"
            .to_string();
    let first = vec![
        ("code/_special.md".to_string(), "# Special v1\n".to_string()),
        (
            "code/files/src/lib.rs.md".to_string(),
            provenance_doc.clone(),
        ),
    ];
    write_incremental_doc_set(project.path(), &out_dir, &first).expect("first write");

    let second = vec![
        ("code/_special.md".to_string(), "# Special v2\n".to_string()),
        ("code/files/src/lib.rs.md".to_string(), provenance_doc),
    ];
    let written =
        write_incremental_doc_set(project.path(), &out_dir, &second).expect("second write");

    // No provenance => always rewritten; matching non-empty hashes => preserved.
    assert_eq!(written, vec!["code/_special.md".to_string()]);
    let special =
        std::fs::read_to_string(out_dir.join("code/_special.md")).expect("special content");
    assert!(special.contains("Special v2"));
}

#[test]
fn degraded_doc_is_rewritten_once_generation_succeeds() {
    let project = tempfile::tempdir().expect("project tempdir");
    std::fs::create_dir_all(project.path().join("src")).expect("source dirs");
    std::fs::write(project.path().join("src/lib.rs"), "pub struct Client;\n").expect("write lib");
    let out_dir = project.path().join("codewiki");
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/lib.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![test_symbol(
            "src/lib.rs",
            "Client",
            "class",
            1,
            "pub struct Client;",
        )],
    };
    let file_doc = "code/files/src/lib.rs.md".to_string();
    let build = |generator: Option<&mut TextGenerator<'_>>| {
        let mut progress = CodewikiProgress::silent();
        generate_hierarchical_docs_with_progress(&input, generator, AiDepth::Symbols, &mut progress)
    };

    // Run 1: every generation fails, so the docs land degraded.
    let mut failing = |_prompt: &str, _system: &str, _tier: PromptTier| None;
    let degraded_docs = build(Some(&mut failing));
    write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &degraded_docs,
        None,
        "symbols",
        DocPruneScope::unscoped(),
    )
    .expect("degraded write");

    // Run 2: generation succeeds and sources are unchanged — the recorded
    // degradation must force a rewrite where hash equality alone would skip.
    let mut succeeding = |_prompt: &str, _system: &str, _tier: PromptTier| {
        Some("Healthy generated prose.".to_string())
    };
    let healthy_docs = build(Some(&mut succeeding));
    let repaired = write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &healthy_docs,
        None,
        "symbols",
        DocPruneScope::unscoped(),
    )
    .expect("repair write");
    assert!(repaired.contains(&file_doc), "degraded doc is repaired");
    let on_disk = std::fs::read_to_string(out_dir.join(&file_doc)).expect("repaired content");
    assert!(on_disk.contains("Healthy generated prose."));

    // Run 3: healthy and unchanged — skipped again.
    let skipped = write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &healthy_docs,
        None,
        "symbols",
        DocPruneScope::unscoped(),
    )
    .expect("healthy rewrite");
    assert!(!skipped.contains(&file_doc), "healthy unchanged doc skips");

    // Run 4: a later failed run must not displace healthy prose for
    // unchanged sources.
    let mut failing_again = |_prompt: &str, _system: &str, _tier: PromptTier| None;
    let degraded_again = build(Some(&mut failing_again));
    let preserved = write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &degraded_again,
        None,
        "symbols",
        DocPruneScope::unscoped(),
    )
    .expect("failed rerun write");
    assert!(!preserved.contains(&file_doc), "healthy doc is preserved");
    let on_disk = std::fs::read_to_string(out_dir.join(&file_doc)).expect("preserved content");
    assert!(on_disk.contains("Healthy generated prose."));
}

#[test]
fn incremental_regenerates_only_changed() {
    let project = tempfile::tempdir().expect("project tempdir");
    std::fs::create_dir_all(project.path().join("src/nested")).expect("source dirs");
    std::fs::write(project.path().join("src/lib.rs"), "pub struct Client;\n").expect("write lib");
    std::fs::write(
        project.path().join("src/nested/api.rs"),
        "pub fn serve() {}\n",
    )
    .expect("write api");
    let out_dir = project.path().join("codewiki");

    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/lib.rs".to_string(), "src/nested/api.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![
            test_symbol("src/lib.rs", "Client", "class", 1, "pub struct Client;"),
            test_symbol(
                "src/nested/api.rs",
                "serve",
                "function",
                1,
                "pub fn serve()",
            ),
        ],
    };

    let first_docs = generate_hierarchical_docs(&input, None);
    let first_written =
        write_incremental_doc_set(project.path(), &out_dir, &first_docs).expect("first write");
    assert!(first_written.contains(&"code/repo.md".to_string()));
    assert!(first_written.contains(&"code/modules/src.md".to_string()));
    assert!(first_written.contains(&"code/files/src/lib.rs.md".to_string()));
    assert!(first_written.contains(&"code/files/src/nested/api.rs.md".to_string()));

    let unchanged_file_doc = out_dir.join("code/files/src/nested/api.rs.md");
    let mut unchanged_content =
        std::fs::read_to_string(&unchanged_file_doc).expect("unchanged doc content");
    unchanged_content.push_str("\n<!-- preserve unchanged doc -->\n");
    std::fs::write(&unchanged_file_doc, unchanged_content).expect("write unchanged marker");

    std::fs::write(
        project.path().join("src/lib.rs"),
        "pub struct Client;\npub fn connect() {}\n",
    )
    .expect("modify lib");
    let changed_docs = generate_hierarchical_docs(&input, None);
    let changed_written = write_incremental_doc_set(project.path(), &out_dir, &changed_docs)
        .expect("incremental write");
    let unchanged_after =
        std::fs::read_to_string(&unchanged_file_doc).expect("unchanged doc after content");

    assert!(unchanged_after.contains("preserve unchanged doc"));
    // _hotspots.md carries no provenance frontmatter, so it is always
    // rewritten (empty source-hash sets cannot prove the doc unchanged).
    // Docs are listed in build order — leaves before the aggregates that
    // consume them — because each one is persisted as soon as it is built.
    assert_eq!(
        changed_written,
        vec![
            "code/files/src/lib.rs.md".to_string(),
            "code/modules/src.md".to_string(),
            "code/concepts/index.md".to_string(),
            "code/concepts/src.md".to_string(),
            "code/narrative/01-introduction.md".to_string(),
            "code/narrative/02-architecture.md".to_string(),
            "code/narrative/03-data-flow.md".to_string(),
            "code/repo.md".to_string(),
            "code/_architecture.md".to_string(),
            "code/_onboarding.md".to_string(),
            "code/_hotspots.md".to_string()
        ]
    );
    let meta = std::fs::read_to_string(out_dir.join("_meta/codewiki.json")).expect("read meta log");
    let meta: serde_json::Value = serde_json::from_str(&meta).expect("parse meta log");
    let generated_docs = meta["generated_docs"].as_array().expect("generated docs");
    assert_eq!(
        generated_docs,
        &vec![
            serde_json::Value::String("code/files/src/lib.rs.md".to_string()),
            serde_json::Value::String("code/modules/src.md".to_string()),
            serde_json::Value::String("code/concepts/index.md".to_string()),
            serde_json::Value::String("code/concepts/src.md".to_string()),
            serde_json::Value::String("code/narrative/01-introduction.md".to_string()),
            serde_json::Value::String("code/narrative/02-architecture.md".to_string()),
            serde_json::Value::String("code/narrative/03-data-flow.md".to_string()),
            serde_json::Value::String("code/repo.md".to_string()),
            serde_json::Value::String("code/_architecture.md".to_string()),
            serde_json::Value::String("code/_onboarding.md".to_string()),
            serde_json::Value::String("code/_hotspots.md".to_string())
        ]
    );

    let reduced_input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/lib.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![test_symbol(
            "src/lib.rs",
            "Client",
            "class",
            1,
            "pub struct Client;",
        )],
    };
    let reduced_docs = generate_hierarchical_docs(&reduced_input, None);
    write_incremental_doc_set(project.path(), &out_dir, &reduced_docs).expect("stale docs removed");

    assert!(!unchanged_file_doc.exists());
    let meta =
        std::fs::read_to_string(out_dir.join("_meta/codewiki.json")).expect("read final meta");
    let meta: serde_json::Value = serde_json::from_str(&meta).expect("parse final meta");
    assert!(
        meta["docs"]
            .get("code/files/src/nested/api.rs.md")
            .is_none()
    );
}

#[test]
fn scoped_incremental_write_preserves_out_of_scope_docs_and_meta() {
    let project = tempfile::tempdir().expect("project tempdir");
    std::fs::create_dir_all(project.path().join("src")).expect("source dir");
    std::fs::create_dir_all(project.path().join("tools")).expect("tools dir");
    std::fs::write(project.path().join("src/lib.rs"), "pub struct Client;\n").expect("write lib");
    std::fs::write(project.path().join("src/old.rs"), "pub struct OldClient;\n")
        .expect("write old");
    std::fs::write(
        project.path().join("tools/helper.rs"),
        "pub fn helper() {}\n",
    )
    .expect("write helper");
    let out_dir = project.path().join("codewiki");

    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec![
            "src/lib.rs".to_string(),
            "src/old.rs".to_string(),
            "tools/helper.rs".to_string(),
        ],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![
            test_symbol("src/lib.rs", "Client", "class", 1, "pub struct Client;"),
            test_symbol(
                "src/old.rs",
                "OldClient",
                "class",
                1,
                "pub struct OldClient;",
            ),
            test_symbol(
                "tools/helper.rs",
                "helper",
                "function",
                1,
                "pub fn helper()",
            ),
        ],
    };
    let mut first_docs = generate_hierarchical_docs(&input, None)
        .into_iter()
        .map(|(path, content)| BuiltDoc::healthy(path, content))
        .collect::<Vec<_>>();
    first_docs.push(BuiltDoc::healthy(
        "code/_changes.md",
        "changes before scoped run\n".to_string(),
    ));
    first_docs.push(BuiltDoc::healthy(
        "code/_ownership.md",
        "ownership before scoped run\n".to_string(),
    ));
    write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &first_docs,
        None,
        "off",
        DocPruneScope::unscoped(),
    )
    .expect("first write");

    let out_of_scope_file_doc = out_dir.join("code/files/tools/helper.rs.md");
    let out_of_scope_module_doc = out_dir.join("code/modules/tools.md");
    let stale_in_scope_file_doc = out_dir.join("code/files/src/old.rs.md");
    assert!(out_of_scope_file_doc.exists());
    assert!(out_of_scope_module_doc.exists());
    assert!(stale_in_scope_file_doc.exists());
    let global_paths = [
        "code/repo.md",
        "code/_architecture.md",
        "code/_onboarding.md",
        "code/_hotspots.md",
        "code/_changes.md",
        "code/_ownership.md",
    ];
    let global_before = global_paths
        .iter()
        .map(|path| {
            (
                *path,
                std::fs::read_to_string(out_dir.join(path)).expect("global doc before"),
            )
        })
        .collect::<Vec<_>>();

    let scoped_input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/lib.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![test_symbol(
            "src/lib.rs",
            "Client",
            "class",
            1,
            "pub struct Client;",
        )],
    };
    let doc_scope = DocPruneScope::from_scopes(&["src".to_string()]);
    let scoped_docs = generate_docs_for_scope(&scoped_input, &doc_scope);
    write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &scoped_docs,
        None,
        "off",
        doc_scope,
    )
    .expect("scoped write");

    assert!(out_of_scope_file_doc.exists());
    assert!(out_of_scope_module_doc.exists());
    assert!(!stale_in_scope_file_doc.exists());
    for (path, before) in global_before {
        let after = std::fs::read_to_string(out_dir.join(path)).expect("global doc after");
        assert_eq!(after, before, "{path} changed during scoped write");
    }
    let meta = std::fs::read_to_string(out_dir.join("_meta/codewiki.json")).expect("read meta");
    let meta: serde_json::Value = serde_json::from_str(&meta).expect("parse meta");
    assert!(meta["docs"].get("code/files/tools/helper.rs.md").is_some());
    assert!(meta["docs"].get("code/modules/tools.md").is_some());
    assert!(meta["docs"].get("code/files/src/old.rs.md").is_none());
    for path in global_paths {
        assert!(meta["docs"].get(path).is_some(), "{path} meta was pruned");
    }
    let generated_docs = meta["generated_docs"].as_array().expect("generated docs");
    for path in global_paths {
        assert!(
            !generated_docs.contains(&serde_json::Value::String(path.to_string())),
            "{path} was regenerated during scoped write"
        );
    }
}

#[test]
fn scoped_incremental_write_preserves_partial_ancestor_module() {
    let project = tempfile::tempdir().expect("project tempdir");
    std::fs::create_dir_all(project.path().join("src/nested")).expect("source dirs");
    std::fs::write(
        project.path().join("src/sibling.rs"),
        "pub struct Sibling;\n",
    )
    .expect("write sibling");
    std::fs::write(
        project.path().join("src/nested/leaf.rs"),
        "pub fn leaf() {}\n",
    )
    .expect("write leaf");
    let out_dir = project.path().join("codewiki");

    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec![
            "src/sibling.rs".to_string(),
            "src/nested/leaf.rs".to_string(),
        ],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![
            test_symbol(
                "src/sibling.rs",
                "Sibling",
                "class",
                1,
                "pub struct Sibling;",
            ),
            test_symbol("src/nested/leaf.rs", "leaf", "function", 1, "pub fn leaf()"),
        ],
    };
    let first_docs = generate_hierarchical_docs(&input, None)
        .into_iter()
        .map(|(path, content)| BuiltDoc::healthy(path, content))
        .collect::<Vec<_>>();
    let snapshot = build_codewiki_index_snapshot(project.path(), &input).expect("snapshot");
    write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &first_docs,
        Some(snapshot),
        "off",
        DocPruneScope::unscoped(),
    )
    .expect("first write");

    let ancestor_module_path = out_dir.join("code/modules/src.md");
    let ancestor_before = std::fs::read_to_string(&ancestor_module_path).expect("ancestor before");
    assert!(ancestor_before.contains("src/sibling.rs"));

    std::fs::write(
        project.path().join("src/nested/leaf.rs"),
        "pub fn leaf() {}\npub fn changed() {}\n",
    )
    .expect("modify leaf");
    let scoped_input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/nested/leaf.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![test_symbol(
            "src/nested/leaf.rs",
            "leaf",
            "function",
            1,
            "pub fn leaf()",
        )],
    };
    let doc_scope = DocPruneScope::from_scopes(&["src/nested".to_string()]);
    let scoped_docs = generate_docs_for_scope(&scoped_input, &doc_scope);
    assert!(
        scoped_docs
            .iter()
            .any(|doc| doc.path == "code/modules/src/nested.md")
    );
    assert!(
        !scoped_docs
            .iter()
            .any(|doc| doc.path == "code/modules/src.md")
    );

    let changed_paths = write_incremental_doc_set_with_snapshot(
        project.path(),
        &out_dir,
        &scoped_docs,
        None,
        "off",
        doc_scope,
    )
    .expect("scoped write");
    assert!(!changed_paths.contains(&"code/modules/src.md".to_string()));

    let ancestor_after = std::fs::read_to_string(&ancestor_module_path).expect("ancestor after");
    assert_eq!(ancestor_after, ancestor_before);
    assert!(ancestor_after.contains("src/sibling.rs"));

    let meta = std::fs::read_to_string(out_dir.join("_meta/codewiki.json")).expect("read meta");
    let meta: serde_json::Value = serde_json::from_str(&meta).expect("parse meta");
    assert!(meta["docs"].get("code/modules/src.md").is_some());
    assert!(
        meta["index_snapshot"]["files"]
            .get("src/sibling.rs")
            .is_some(),
        "scoped write must preserve the previous full index snapshot"
    );
    let generated_docs = meta["generated_docs"].as_array().expect("generated docs");
    assert!(
        !generated_docs.contains(&serde_json::Value::String(
            "code/modules/src.md".to_string()
        )),
        "ancestor module was regenerated from partial scoped input"
    );
}