sinter-io 0.42.0

sinter command-line interface
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
//! Phase 5/6 gate: query, affected, path, impact against a small repo.

use std::path::Path;
use std::process::Command;

fn sinter(repo: &Path, args: &[&str]) -> (bool, String) {
    let out = Command::new(env!("CARGO_BIN_EXE_sinter"))
        .args(args)
        .env("HOME", repo)
        .env("USERPROFILE", repo)
        .current_dir(repo)
        .output()
        .expect("run sinter");
    (
        out.status.success(),
        format!(
            "{}{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        ),
    )
}

fn git(repo: &Path, args: &[&str]) {
    let ok = Command::new("git")
        .args(args)
        .current_dir(repo)
        .env("GIT_AUTHOR_NAME", "t")
        .env("GIT_AUTHOR_EMAIL", "t@t")
        .env("GIT_COMMITTER_NAME", "t")
        .env("GIT_COMMITTER_EMAIL", "t@t")
        .output()
        .expect("run git")
        .status
        .success();
    assert!(ok, "git {args:?} failed");
}

#[test]
fn query_affected_path_impact() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod util;\nuse crate::util::core_fn;\n\n/// Entry point.\npub fn entry() -> u32 {\n    core_fn()\n}\n\npub fn test_entry() -> u32 {\n    entry()\n}\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    41\n}\n",
    )
    .unwrap();

    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // query: content-bearing result.
    let (ok, out) = sinter(repo, &["query", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("fn core_fn()"), "{out}");
    assert!(out.contains("The core."), "{out}");

    // affected: transitive, cross-file (entry via import, test_entry via scope).
    let (ok, out) = sinter(repo, &["affected", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("entry"), "{out}");
    assert!(out.contains("test_entry"), "{out}");
    // Direct callers are stated apart from the transitive total, so a
    // blast radius never reads as a caller count.
    assert!(out.contains("3 dependents of core_fn"), "{out}");
    assert!(out.contains("2 direct in 1 file(s), 1 transitive"), "{out}");
    let (_, out) = sinter(repo, &["affected", "core_fn", "--depth", "1"]);
    assert!(
        out.contains("2 dependents of core_fn") && !out.contains("test_entry"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["affected", "core_fn", "--json"]);
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(
        (
            v["total"].as_u64(),
            v["direct"].as_u64(),
            v["direct_files"].as_u64()
        ),
        (Some(3), Some(2), Some(1)),
        "{out}"
    );

    // evidence filter: scip-only finds nothing (no index present) —
    // grep-style exit 1 for a valid query with no results.
    let (ok, out) = sinter(repo, &["affected", "core_fn", "--evidence", "scip"]);
    assert!(!ok, "{out}");
    assert!(out.contains("0 dependents"), "{out}");

    // path: entry reaches core_fn through the import-evidence call edge.
    let (ok, out) = sinter(repo, &["path", "test_entry", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("-[calls/"), "{out}");
    assert!(out.trim_end().ends_with("core_fn"), "{out}");

    // impact: edit core_fn, commit, ask for the blast radius of the commit.
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    42\n}\n",
    )
    .unwrap();
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "change core"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["impact", "HEAD~1..HEAD"]);
    assert!(ok, "{out}");
    assert!(out.contains("core_fn"), "{out}");
    assert!(out.contains("entry"), "{out}");
    assert!(out.contains("test_entry"), "{out}"); // matched as affected test

    // H1 regression: touching the imported file must not drop the
    // cross-file import-evidence edges — dependents survive the rebuild.
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    43\n}\n",
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["affected", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("entry"), "{out}");
    assert!(out.contains("test_entry"), "{out}");
}

/// Dynamic dispatch is blast radius: `affected <ImplType::method>` must
/// reach callers of the trait method through the `dynamic` fan-out edge,
/// and evidence filtering without "dynamic" must exclude them.
#[test]
fn affected_through_dyn_dispatch() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod cat;\nmod dog;\n\npub trait Speak {\n    fn speak(&self);\n}\n\npub fn announce(s: &dyn Speak) {\n    Speak::speak(s);\n}\n",
    )
    .unwrap();
    let dog =
        "use crate::Speak;\n\npub struct Dog;\n\nimpl Speak for Dog {\n    fn speak(&self) {}\n}\n";
    std::fs::write(repo.join("src/dog.rs"), dog).unwrap();
    std::fs::write(
        repo.join("src/cat.rs"),
        "use crate::Speak;\n\npub struct Cat;\n\nimpl Speak for Cat {\n    fn speak(&self) {}\n}\n",
    )
    .unwrap();

    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // Changing the impl must surface the caller of the trait method, with
    // the dynamic evidence visible on the bridging edge.
    let (ok, out) = sinter(repo, &["affected", "Dog::speak"]);
    assert!(ok, "{out}");
    assert!(out.contains("announce"), "{out}");
    assert!(out.contains("/dynamic"), "{out}");

    // Incremental: touching one impl file re-resolves the trait's file and
    // tears down its dynamic fan-out — the OTHER impl's edge must survive
    // the rebuild (dst-file facts rejoin the resolution set).
    std::fs::write(repo.join("src/dog.rs"), dog.replace("{}", "{ }")).unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["affected", "Cat::speak"]);
    assert!(ok, "{out}");
    assert!(out.contains("announce"), "{out}");
    assert!(out.contains("/dynamic"), "{out}");

    // Incremental: touching the trait's own file re-derives the fan-out
    // (impl files re-resolve through their `uses` reference on the trait).
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod cat;\nmod dog;\n\npub trait Speak {\n    fn speak(&self);\n}\n\npub fn announce(s: &dyn Speak) {\n    Speak::speak(s)\n}\n",
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["affected", "Dog::speak"]);
    assert!(ok, "{out}");
    assert!(out.contains("announce"), "{out}");
    assert!(out.contains("/dynamic"), "{out}");

    // Honesty: excluding dynamic evidence excludes the fan-out.
    let (_, out) = sinter(
        repo,
        &["affected", "Dog::speak", "--evidence", "import,scope,scip"],
    );
    assert!(!out.contains("announce"), "{out}");

    // --certain also excludes it (Dynamic is Inferred by construction).
    let (_, out) = sinter(repo, &["affected", "Dog::speak", "--certain"]);
    assert!(!out.contains("announce"), "{out}");
}

fn sinter_code(repo: &Path, args: &[&str]) -> (Option<i32>, String) {
    let out = Command::new(env!("CARGO_BIN_EXE_sinter"))
        .args(args)
        .env("HOME", repo)
        .env("USERPROFILE", repo)
        .current_dir(repo)
        .output()
        .expect("run sinter");
    (
        out.status.code(),
        format!(
            "{}{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        ),
    )
}

/// New surface: `--json` on the read commands (mirroring the MCP tool
/// shapes), grep-style exit codes, `--limit` on affected, and `--repo`
/// accepted by lifecycle commands.
#[test]
fn json_flags_exit_codes_and_repo_flag() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod util;\nuse crate::util::core_fn;\n\n/// Entry point.\npub fn entry() -> u32 {\n    core_fn()\n}\n\npub fn other_entry() -> u32 {\n    core_fn()\n}\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    41\n}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    // Lifecycle commands accept --repo (positional still works elsewhere).
    let (code, out) = sinter_code(repo, &["build", "--repo", "."]);
    assert_eq!(code, Some(0), "{out}");
    // Both at once is a usage error.
    let (code, _) = sinter_code(repo, &["build", ".", "--repo", "."]);
    assert_eq!(code, Some(2));

    // query --json: MCP `query` shape.
    let (code, out) = sinter_code(repo, &["query", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["exact"], serde_json::json!(true), "{out}");
    assert_eq!(v["results"][0]["name"], serde_json::json!("core_fn"));

    // show --json: MCP `show` shape.
    let (code, out) = sinter_code(repo, &["show", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["symbol"]["name"], serde_json::json!("core_fn"));
    assert!(v["incoming"].as_array().is_some(), "{out}");
    // Every edge exposes `site`; the call from `entry` names its call site
    // (`core_fn()` on line 6 of src/lib.rs).
    let entry_call = v["incoming"]
        .as_array()
        .unwrap()
        .iter()
        .find(|e| {
            e["symbol"].as_str().is_some_and(|s| s.ends_with("entry")) && e["relation"] == "calls"
        })
        .unwrap_or_else(|| panic!("no call edge from entry: {out}"));
    assert_eq!(
        entry_call["site"],
        serde_json::json!("src/lib.rs:6"),
        "{out}"
    );

    // affected --json: MCP `affected` shape, terse entries.
    let (code, out) = sinter_code(repo, &["affected", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(v["total"].as_u64().unwrap() >= 2, "{out}");
    assert!(v["dependents"][0]["s"].is_string(), "{out}");

    // affected --limit: truncation footer, ask-style.
    let (code, out) = sinter_code(repo, &["affected", "core_fn", "--limit", "1"]);
    assert_eq!(code, Some(0), "{out}");
    assert!(out.contains("more dependents below cutoff"), "{out}");
    assert!(out.contains("--limit"), "{out}");

    // path --json: MCP `path` shape.
    let (code, out) = sinter_code(repo, &["path", "entry", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["found"], serde_json::json!(true), "{out}");
    assert!(v["steps"][0]["relation"].is_string(), "{out}");
    // Each hop names where it is written.
    assert_eq!(
        v["steps"][0]["site"],
        serde_json::json!("src/lib.rs:6"),
        "{out}"
    );

    // impact --json: the ImpactReport shape.
    let (code, out) = sinter_code(repo, &["impact", "HEAD", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(v["changed_symbols"].is_array(), "{out}");

    // Grep-style exit codes: 1 = valid query, no results.
    let (code, out) = sinter_code(repo, &["show", "no_such_symbol_zzqx"]);
    assert_eq!(code, Some(1), "{out}");
    assert!(out.contains("sinter ask"), "no concept-search hint: {out}");
    let (code, _) = sinter_code(repo, &["query", "zzqxzzqx"]);
    assert_eq!(code, Some(1));
    let (code, out) = sinter_code(repo, &["path", "core_fn", "entry"]);
    assert_eq!(code, Some(1), "{out}");
    assert!(out.contains("no path"), "{out}");
    // 2 = usage/execution error (bad evidence kind).
    let (code, _) = sinter_code(repo, &["affected", "core_fn", "--evidence", "bogus"]);
    assert_eq!(code, Some(2));

    // map accepts --repo like the other read commands.
    let (code, out) = sinter_code(repo, &["map", "--repo", "."]);
    assert_eq!(code, Some(0), "{out}");
    assert!(out.contains("nodes"), "{out}");
}

/// Empty graph: read commands say so instead of "no match", build warns.
#[test]
fn empty_graph_says_so() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(repo.join("notes.txt"), "no source here\n").unwrap();

    let (code, out) = sinter_code(repo, &["build"]);
    assert_eq!(code, Some(0), "{out}");
    assert!(out.contains("0 source files found under"), "{out}");
    assert!(out.contains("wrong directory?"), "{out}");

    let (code, out) = sinter_code(repo, &["query", "anything"]);
    assert_eq!(code, Some(2), "{out}");
    assert!(out.contains("graph") && out.contains("is empty"), "{out}");
    assert!(out.contains("right directory"), "{out}");
}

/// Every negative answer is not-proven; missing/stale SCIP adds the precise
/// compiler-coverage gap while a fresh index keeps the generic limitation.
#[test]
fn negative_answers_flag_stale_scip() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub fn entry() -> u32 {\n    core_fn()\n}\n\npub fn core_fn() -> u32 {\n    41\n}\n\npub fn orphan() {}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // No index: explicit syntax-only coverage gap.
    let (_, out) = sinter(repo, &["path", "core_fn", "entry"]);
    assert!(
        out.contains("no path")
            && out.contains("status: not proven")
            && out.contains("compiler index missing for rust"),
        "{out}"
    );

    // Index older than the source: inconclusive.
    let index = repo.join(".sinter/index.scip");
    std::fs::write(&index, b"").unwrap();
    let old = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000);
    std::fs::OpenOptions::new()
        .write(true)
        .open(&index)
        .unwrap()
        .set_modified(old)
        .unwrap();
    let (_, out) = sinter(repo, &["path", "core_fn", "entry"]);
    assert!(
        out.contains("no path") && out.contains("compiler index is stale"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["affected", "orphan"]);
    assert!(
        out.contains("0 dependents") && out.contains("compiler index is stale"),
        "{out}"
    );
    // A hit never carries the note.
    let (_, out) = sinter(repo, &["path", "entry", "core_fn"]);
    assert!(!out.contains("status: not proven"), "{out}");

    // Index newer than the source: still not-proven, without stale/missing.
    std::fs::OpenOptions::new()
        .write(true)
        .open(&index)
        .unwrap()
        .set_modified(std::time::SystemTime::now())
        .unwrap();
    let (_, out) = sinter(repo, &["path", "core_fn", "entry"]);
    assert!(
        out.contains("no path")
            && out.contains("status: not proven")
            && !out.contains("compiler index is stale")
            && !out.contains("compiler index missing"),
        "{out}"
    );
}

/// Installation drift is reported by maintenance verbs, never beside a
/// query answer: agents read stderr with stdout, and a nag on every
/// `show` obscures the result it decorates.
#[test]
fn query_verbs_never_nag_about_stale_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub fn entry() -> u32 {\n    41\n}\n",
    )
    .unwrap();
    // A managed AGENTS.md block with stale contents.
    std::fs::write(
        repo.join("AGENTS.md"),
        "<!-- BEGIN sinter (managed by `sinter install`; edits inside are overwritten) -->\nold\n<!-- END sinter -->\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    assert!(out.contains("AGENTS.md sinter block is stale"), "{out}");

    for verb in [
        &["show", "entry"][..],
        &["ask", "entry"],
        &["affected", "entry"],
        &["path", "entry", "entry"],
    ] {
        let (_, out) = sinter(repo, verb);
        assert!(!out.contains("is stale"), "{verb:?} nagged: {out}");
    }
}

/// `show` names trait implementors and dynamic fan-out explicitly rather
/// than folding them into used-by / calls tallies.
#[test]
fn show_lists_implementations_and_dispatch() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub trait Speak {\n    fn speak(&self);\n}\n\npub struct Dog;\npub struct Cat;\n\nimpl Speak for Dog {\n    fn speak(&self) {}\n}\n\nimpl Speak for Cat {\n    fn speak(&self) {}\n}\n\npub fn announce(s: &dyn Speak) {\n    Speak::speak(s);\n}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    let (_, out) = sinter(repo, &["show", "Speak"]);
    assert!(out.contains("implemented by (2)    Cat, Dog"), "{out}");
    let (_, out) = sinter(repo, &["show", "Speak::speak"]);
    assert!(
        out.contains("dispatches to (2)    Cat::speak, Dog::speak"),
        "{out}"
    );
    assert!(
        !out.contains("calls ("),
        "dynamic edges leaked into calls: {out}"
    );
    let (_, out) = sinter(repo, &["show", "Dog"]);
    assert!(out.contains("implements       Speak"), "{out}");

    // A miss explains itself: forward reach, and who does reach the target.
    let (_, out) = sinter(repo, &["path", "Dog::speak", "announce"]);
    assert!(out.contains("no path Dog::speak -> announce"), "{out}");
    assert!(
        out.contains("forward search from Dog::speak reached 0 symbol(s)"),
        "{out}"
    );
    assert!(
        out.contains("nothing reaches announce under this filter"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["path", "Dog", "Dog::speak"]);
    assert!(out.contains("Dog::speak is reached by (1):"), "{out}");
    assert!(out.contains("Speak::speak [calls/dynamic]"), "{out}");
    let (_, out) = sinter(repo, &["path", "announce", "Dog::speak", "--certain"]);
    assert!(
        out.contains("1 incoming edge(s) excluded by --evidence/--certain"),
        "{out}"
    );
}