devscout-rs 0.10.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
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
//! Unit tests for the pieces that need no repository: the evidence mapping, the
//! refusal vocabulary, the coverage table, and the stream's own ordering and
//! digest over a hand-built index.

use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

use crate::freshness::record::{self, ArtifactIdentity, Artifacts, Record};
use crate::graph::{
    Candidate, Edge, EdgesByKind, Graph, HeuristicByTier, HeuristicTier, Percent1,
    SemanticProvenance, Stats, GRAPH_SCHEMA_VERSION,
};

use super::evidence;
use super::refusal::ExportRefusal;
use super::snapshot;
use super::stream;

static COUNTER: AtomicU64 = AtomicU64::new(0);

fn scratch(label: &str) -> PathBuf {
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let dir = std::env::temp_dir().join(format!("scout-export-{label}-{}-{n}", std::process::id()));
    fs::create_dir_all(&dir).expect("create scratch dir");
    fs::canonicalize(&dir).expect("canonicalize scratch dir")
}

fn git(root: &std::path::Path, args: &[&str]) {
    let out = Command::new("git")
        .args(args)
        .current_dir(root)
        .output()
        .expect("git must run");
    assert!(out.status.success(), "git {args:?}: {out:?}");
}

fn empty_stats() -> Stats {
    Stats {
        def_count: 0,
        file_count: 0,
        edges_by_kind: EdgesByKind::default(),
        ambiguous_count: 0,
        ambiguous_pct: Percent1::zero(),
        unresolved_external_count: 0,
        heuristic_edge_count: 0,
        test_def_count: 0,
        ts: None,
        heuristic_by_tier: HeuristicByTier::default(),
        bus_vocabulary_derived: None,
        semantic: None,
        lane: None,
    }
}

/// A repository root carrying one published, empty index: a record, its digest
/// table, a graph and an empty fragment cache, all written by this build's own
/// writers so the identities the record certifies are the ones on disk.
pub(super) fn indexed_root(label: &str) -> PathBuf {
    let root = scratch(label);
    git(&root, &["init", "-q"]);
    git(&root, &["config", "user.email", "test@example.com"]);
    git(&root, &["config", "user.name", "test"]);

    let manifest = record::state_dir(&root).join("manifest.json");
    fs::create_dir_all(record::state_dir(&root)).expect("create state dir");
    fs::write(&manifest, b"{}").expect("write manifest");

    let graph = Graph {
        schema_version: GRAPH_SCHEMA_VERSION,
        built_at_head: None,
        defs: Vec::new(),
        edges: Vec::new(),
        stats: empty_stats(),
        names: Vec::new(),
        units: Vec::new(),
        unresolved_imports: Vec::new(),
        transport: Vec::new(),
        template_unsupported: Vec::new(),
    };
    crate::graph::write_graph(&root, &graph).expect("write graph");
    crate::graph::write_fragments_cache(&root, &crate::graph::OrderedMap::default())
        .expect("write fragments");

    let generation = record::new_generation("test");
    record::write_digests(&root, &generation, &BTreeMap::new()).expect("write digests");
    let published = Record {
        format: record::RECORD_FORMAT.to_string(),
        version: record::RECORD_VERSION,
        state: "published".to_string(),
        owner: record::owner_of(&root).expect("the scratch root is a git repository"),
        generation,
        indexed_head: None,
        scope: vec![".".to_string()],
        indexed_at: "2026-01-01T00:00:00Z".to_string(),
        file_count: 0,
        dirty_at_map: Some(Vec::new()),
        config_inputs: BTreeMap::new(),
        artifacts: Artifacts {
            graph: ArtifactIdentity::of(&crate::graph::graph_json_path(&root)),
            manifest: ArtifactIdentity::of(&manifest),
        },
    };
    record::write_record(&root, &published).expect("write record");
    root
}

fn member_edge(tier: Option<HeuristicTier>, source: Option<SemanticProvenance>) -> Edge {
    match (tier, source) {
        (Some(tier), None) => Edge::uses_member(
            "a.cs".to_string(),
            1,
            "T.M".to_string(),
            "b.cs".to_string(),
            Some("M".to_string()),
            Some(tier),
        ),
        (None, Some(source)) => Edge::uses_member_semantic(
            "a.cs".to_string(),
            1,
            "T.M".to_string(),
            "b.cs".to_string(),
            Some("M".to_string()),
            source,
            None,
        ),
        (None, None) => Edge::uses_member(
            "a.cs".to_string(),
            1,
            "T.M".to_string(),
            "b.cs".to_string(),
            Some("M".to_string()),
            None,
        ),
        (Some(_), Some(_)) => panic!("an edge is either tiered or compiler-sourced, never both"),
    }
}

fn ctor_di(resolution: &str) -> Edge {
    Edge::CtorDi {
        from_file: "a.cs".to_string(),
        from_line: 1,
        iface: "IService".to_string(),
        resolution: resolution.to_string(),
        args: None,
        to: None,
        candidates: Vec::new(),
    }
}

#[test]
fn fact_export_ac_3_the_evidence_mapping_states_the_tier_a_guess_came_from() {
    assert_eq!(
        evidence::edge_evidence(&member_edge(Some(HeuristicTier::Guess), None)),
        "heuristic-guess"
    );
    assert_eq!(
        evidence::edge_evidence(&member_edge(Some(HeuristicTier::Ext), None)),
        "heuristic-ext"
    );
    // A flagged edge that names no tier is stated as the weaker word rather
    // than as the tier it might have been.
    assert_eq!(
        evidence::edge_evidence(&Edge::UsesType {
            from_file: "a.cs".to_string(),
            from_line: 1,
            to: "T".to_string(),
            to_file: "b.cs".to_string(),
            heuristic: true,
        }),
        "heuristic"
    );
    assert_eq!(
        evidence::edge_evidence(&member_edge(None, None)),
        "exact",
        "a precise member reference is never exported as a guess"
    );
}

#[test]
fn fact_export_ac_3_a_compiler_sourced_edge_names_its_source() {
    assert_eq!(
        evidence::edge_evidence(&member_edge(None, Some(SemanticProvenance::Semantic))),
        "semantic"
    );
    assert_eq!(
        evidence::edge_evidence(&member_edge(
            None,
            Some(SemanticProvenance::SemanticDiscovered)
        )),
        "semantic-discovered"
    );
}

#[test]
fn fact_export_ac_3_every_edge_kind_and_resolution_word_maps_onto_one_evidence_class() {
    let inherited = Edge::Inherits {
        from_file: "a.cs".to_string(),
        from_line: 1,
        to: "B".to_string(),
        to_file: "b.cs".to_string(),
        heuristic: false,
    };
    assert_eq!(evidence::edge_evidence(&inherited), "exact");
    assert_eq!(
        evidence::edge_evidence(&Edge::Imports {
            from_file: "a.cs".to_string(),
            from_line: 1,
            target: "System.Text".to_string(),
        }),
        "declared",
        "a C# using names a namespace and resolves to no file"
    );
    assert_eq!(
        evidence::edge_evidence(&Edge::Ambiguous {
            origin: "a.cs".to_string(),
            from_file: "a.cs".to_string(),
            from_line: 1,
            raw: "Load".to_string(),
            candidates: vec![Candidate {
                id: "T.Load".to_string(),
                file: "b.cs".to_string(),
            }],
            candidate_count: 1,
        }),
        "ambiguous"
    );
    assert_eq!(evidence::edge_evidence(&ctor_di("plain")), "exact");
    assert_eq!(evidence::edge_evidence(&ctor_di("closed")), "exact");
    assert_eq!(evidence::edge_evidence(&ctor_di("open-generic")), "exact");
    assert_eq!(evidence::edge_evidence(&ctor_di("ambiguous")), "ambiguous");
    assert_eq!(
        evidence::edge_evidence(&ctor_di("unresolved")),
        "unresolved"
    );
    assert_eq!(evidence::edge_evidence(&ctor_di("infra")), "unresolved");
    assert_eq!(
        evidence::edge_evidence(&ctor_di("a-word-a-later-release-adds")),
        "unresolved",
        "an unknown resolution word must never be stated as a bound injection"
    );
    assert_eq!(
        evidence::edge_evidence(&Edge::BusHop {
            from_file: "a.cs".to_string(),
            from_line: 1,
            message: "OrderPlaced".to_string(),
            to: "Handler".to_string(),
            to_file: "b.cs".to_string(),
            evidence: "base-arg".to_string(),
        }),
        "derived"
    );
}

#[test]
fn fact_export_ac_3_every_evidence_word_the_mapping_produces_is_in_the_closed_vocabulary() {
    let produced = [
        evidence::edge_evidence(&inherited()),
        evidence::edge_evidence(&member_edge(Some(HeuristicTier::Ext), None)),
        evidence::edge_evidence(&member_edge(Some(HeuristicTier::Guess), None)),
        evidence::edge_evidence(&member_edge(None, Some(SemanticProvenance::Semantic))),
        evidence::edge_evidence(&member_edge(
            None,
            Some(SemanticProvenance::SemanticDiscovered),
        )),
        evidence::edge_evidence(&member_edge(None, None)),
        evidence::edge_evidence(&ctor_di("ambiguous")),
        evidence::edge_evidence(&ctor_di("infra")),
        evidence::edge_evidence(&Edge::Imports {
            from_file: "a.cs".to_string(),
            from_line: 1,
            target: "N".to_string(),
        }),
        evidence::edge_evidence(&Edge::BusHop {
            from_file: "a.cs".to_string(),
            from_line: 1,
            message: "M".to_string(),
            to: "H".to_string(),
            to_file: "b.cs".to_string(),
            evidence: "base-arg".to_string(),
        }),
        evidence::DECLARED,
        evidence::CONVENTION,
        evidence::UNRESOLVED,
    ];
    for word in produced {
        assert!(
            evidence::VOCABULARY.contains(&word),
            "{word} is not a documented evidence word"
        );
    }
    let mut unique: Vec<&str> = evidence::VOCABULARY.to_vec();
    unique.sort_unstable();
    unique.dedup();
    assert_eq!(
        unique.len(),
        evidence::VOCABULARY.len(),
        "the vocabulary repeats a word"
    );
}

fn inherited() -> Edge {
    Edge::Inherits {
        from_file: "a.cs".to_string(),
        from_line: 1,
        to: "B".to_string(),
        to_file: "b.cs".to_string(),
        heuristic: false,
    }
}

#[test]
fn fact_export_ac_3_the_coverage_table_states_what_this_release_extracts() {
    let text = serde_json::to_string(&super::header::coverage()).expect("coverage serializes");
    let coverage: serde_json::Value = serde_json::from_str(&text).expect("coverage parses");
    for family in ["definition", "edge", "test-file"] {
        for language in ["csharp", "typescript", "rust"] {
            assert_eq!(
                coverage[family][language], "extracted",
                "{family}/{language}"
            );
        }
    }
    for family in [
        "publish-site",
        "handler-registration",
        "di-registration",
        "transport-address",
    ] {
        assert_eq!(coverage[family]["csharp"], "extracted", "{family}");
        assert_eq!(coverage[family]["typescript"], "not-extracted", "{family}");
        assert_eq!(coverage[family]["rust"], "not-extracted", "{family}");
    }
    assert_eq!(coverage["unresolved-import"]["typescript"], "extracted");
    assert_eq!(coverage["unresolved-import"]["csharp"], "not-extracted");
    assert_eq!(coverage["unresolved-import"]["rust"], "not-extracted");
    for family in ["http-route", "http-client"] {
        for language in ["csharp", "typescript", "rust"] {
            assert_eq!(
                coverage[family][language], "not-extracted",
                "{family}/{language}: this release extracts none of them"
            );
        }
    }
}

#[test]
fn fact_export_ac_6_every_refusal_variant_renders_one_line_naming_the_reason_and_map() {
    for refusal in ExportRefusal::every_variant() {
        let line = refusal.line();
        assert!(!line.contains('\n'), "a refusal is one stderr line: {line}");
        assert!(
            line.contains(&format!("({})", refusal.reason())),
            "the reason word is stated: {line}"
        );
        assert!(
            line.contains("devscout map"),
            "every refusal names the one recovery: {line}"
        );
    }
}

#[test]
fn fact_export_ac_1_an_index_with_no_record_and_no_graph_refuses_with_no_index() {
    let root = scratch("bare");
    let refusal = snapshot::load(&root, &root)
        .map(|_| ())
        .expect_err("a bare directory has no index");
    assert_eq!(refusal.reason(), "no-index");
}

#[test]
fn fact_export_ac_6_a_record_removed_mid_run_refuses_as_index_changed() {
    let root = indexed_root("mid-run");
    assert!(
        snapshot::load(&root, &root).is_ok(),
        "the hand-built index is readable"
    );
    let refusal = snapshot::load_bracketed(&root, &root, &mut |root| {
        fs::remove_file(record::record_path(root)).expect("remove the record");
    })
    .map(|_| ())
    .expect_err("a record that disappears under the run cannot be published");
    assert_eq!(refusal.reason(), "index-changed");
}

#[test]
fn fact_export_ac_5_two_renders_of_one_index_agree_byte_for_byte() {
    let root = indexed_root("determinism");
    let first = snapshot::load(&root, &root).expect("the hand-built index is readable");
    let second = snapshot::load(&root, &root).expect("the hand-built index is readable");
    assert_eq!(
        stream::render(&first).bytes(),
        stream::render(&second).bytes(),
        "an unchanged index must render once, not twice"
    );
}

#[test]
fn fact_export_ac_4_the_payload_digest_covers_the_fact_lines_alone() {
    let root = indexed_root("digest");
    let loaded = snapshot::load(&root, &root).expect("the hand-built index is readable");
    let rendered = stream::render(&loaded).bytes();
    let text = String::from_utf8(rendered).expect("the stream is UTF-8");
    let mut lines = text.lines();
    let header = lines.next().expect("a header line");
    assert!(header.starts_with("{\"format\":\"devscout-facts\",\"schemaVersion\":1,"));
    let trailer = text.lines().last().expect("a trailer line");
    let trailer: serde_json::Value = serde_json::from_str(trailer).expect("the trailer parses");
    let body: String = lines
        .take_while(|line| !line.starts_with("{\"trailer\""))
        .map(|line| format!("{line}\n"))
        .collect();
    assert_eq!(trailer["facts"], 0);
    assert_eq!(
        trailer["payloadDigest"],
        format!("sha256:{}", record::sha256_hex(body.as_bytes()))
    );
    assert_eq!(trailer["counts"]["definition"], 0);
}

// The temporary name beside `--out` is predictable -- a pid and a counter, the
// same shape the repository's other atomic writers use -- so a local actor can
// put an entry there first. Following that entry would write through it: onto a
// symlink's target, or over an unrelated file. Every candidate is therefore
// created exclusively, and one that already exists is skipped rather than
// opened, so a leftover or a planted name costs an attempt, never a write.

#[test]
fn fact_export_ac_1_an_occupied_temporary_name_is_never_opened_or_truncated() {
    let dir = scratch("tmp-taken");
    let target = dir.join("stream.ndjson");
    fs::write(&target, b"the previous stream\n").expect("write the destination");
    // Every name this run can pick is already taken by an unrelated file.
    let pid = std::process::id();
    let mut planted = Vec::new();
    for counter in 0..512u64 {
        let path = dir.join(format!(".stream.ndjson.tmp.{pid}.{counter}"));
        fs::write(&path, format!("unrelated {counter}\n")).expect("plant a file");
        planted.push(path);
    }
    let error = stream::write_out(&target, b"the new stream\n")
        .expect_err("every temporary name is taken, so nothing may be written");
    assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists);
    for path in &planted {
        let content = fs::read_to_string(path).expect("the planted file is still readable");
        assert!(
            content.starts_with("unrelated "),
            "{} was truncated: {content}",
            path.display()
        );
    }
    assert_eq!(
        fs::read(&target).expect("read the destination"),
        b"the previous stream\n",
        "the destination keeps its previous bytes"
    );
    assert_eq!(
        fs::read_dir(&dir).expect("read the directory").count(),
        planted.len() + 1,
        "this run leaves no temporary entry of its own"
    );
}

#[test]
fn fact_export_ac_1_an_occupied_temporary_name_does_not_block_the_next_one() {
    let dir = scratch("tmp-reuse");
    let stale = dir.join(".stream.ndjson.tmp.claim.0");
    fs::write(&stale, b"a crashed run's leftover\n").expect("plant the leftover");
    let free = dir.join(".stream.ndjson.tmp.claim.1");
    let target = dir.join("stream.ndjson");
    fs::write(&target, b"the previous stream\n").expect("write the destination");
    stream::write_out_via(
        &target,
        b"the new stream\n",
        vec![stale.clone(), free.clone()].into_iter(),
    )
    .expect("the next free name is used");
    assert_eq!(
        fs::read(&stale).expect("read the leftover"),
        b"a crashed run's leftover\n",
        "an entry this run did not create is left alone"
    );
    assert_eq!(
        fs::read(&target).expect("read the destination"),
        b"the new stream\n"
    );
    assert!(
        !free.exists(),
        "the used name was renamed onto the destination"
    );
}

#[cfg(unix)]
#[test]
fn fact_export_ac_1_a_temporary_name_that_is_a_symlink_is_skipped_not_followed() {
    let dir = scratch("tmp-symlink");
    let victim = dir.join("sentinel.txt");
    fs::write(&victim, b"PREEXISTING USER DATA\n").expect("write the sentinel");
    let taken = dir.join(".stream.ndjson.tmp.claim.0");
    std::os::unix::fs::symlink(&victim, &taken).expect("plant the symlink");
    let free = dir.join(".stream.ndjson.tmp.claim.1");
    let target = dir.join("stream.ndjson");
    stream::write_out_via(
        &target,
        b"the new stream\n",
        vec![taken.clone(), free.clone()].into_iter(),
    )
    .expect("the next free name is used");
    assert_eq!(
        fs::read(&victim).expect("read the sentinel"),
        b"PREEXISTING USER DATA\n",
        "the symlink's target is untouched"
    );
    assert!(
        fs::symlink_metadata(&taken)
            .expect("stat the planted entry")
            .file_type()
            .is_symlink(),
        "the planted entry is left as it was"
    );
    assert_eq!(
        fs::read(&target).expect("read the destination"),
        b"the new stream\n"
    );
    assert!(
        !free.exists(),
        "the used name was renamed onto the destination"
    );
}