sinter-io 0.59.0

Persistent, evidence-backed code graph for coding agents
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
//! `sinter scip check` / bare `sinter scip`: the CI guard and the idempotent
//! index job. Freshness is mtime-based, so tests pin mtimes explicitly
//! instead of sleeping.

use std::path::Path;
use std::process::{Command, Output};
use std::time::{Duration, SystemTime};

fn sinter(repo: &Path, args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_sinter"))
        .args(args)
        .env("HOME", repo)
        .env("USERPROFILE", repo)
        .current_dir(repo)
        .stdin(std::process::Stdio::null())
        .output()
        .expect("run sinter")
}

fn set_mtime(path: &Path, t: SystemTime) {
    std::fs::File::options()
        .write(true)
        .open(path)
        .unwrap()
        .set_modified(t)
        .unwrap();
}

/// `scip check`: missing index exits 1; fresh index exits 0 with "index
/// fresh"; a source/configuration input newer than the index exits 1 with
/// the count.
#[test]
fn check_reports_freshness_without_indexing() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(repo.join("a.rs"), "pub fn f() {}\n").unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    std::fs::write(repo.join("b.rs"), "pub fn g() {}\n").unwrap();

    let out = sinter(repo, &["scip", "check"]);
    assert!(
        !out.status.success(),
        "missing index must fail `scip check`"
    );
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("no SCIP index"),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );

    std::fs::create_dir_all(repo.join(".sinter")).unwrap();
    std::fs::write(repo.join(".sinter/index.scip"), b"stub").unwrap();
    let past = SystemTime::now() - Duration::from_secs(60);
    set_mtime(&repo.join("a.rs"), past);
    set_mtime(&repo.join("b.rs"), past);

    let out = sinter(repo, &["scip", "check"]);
    assert!(out.status.success(), "fresh index must pass `scip check`");
    assert!(
        String::from_utf8_lossy(&out.stdout).contains("index fresh"),
        "{}",
        String::from_utf8_lossy(&out.stdout)
    );

    set_mtime(
        &repo.join("a.rs"),
        SystemTime::now() + Duration::from_secs(60),
    );
    let out = sinter(repo, &["scip", "check"]);
    assert!(!out.status.success(), "newer source must fail `scip check`");
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("1 source/config input"), "{err}");

    set_mtime(&repo.join("a.rs"), past);
    set_mtime(
        &repo.join("Cargo.toml"),
        SystemTime::now() + Duration::from_secs(60),
    );
    let out = sinter(repo, &["scip", "check"]);
    assert!(
        !out.status.success(),
        "newer project configuration must make SCIP stale"
    );
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("1 source/config input"), "{err}");

    // A file no compiler indexer covers (init writes AGENTS.md after
    // indexing) must not make the index stale.
    set_mtime(&repo.join("Cargo.toml"), past);
    std::fs::write(repo.join("AGENTS.md"), "# agents\n").unwrap();
    set_mtime(
        &repo.join("AGENTS.md"),
        SystemTime::now() + Duration::from_secs(60),
    );
    let out = sinter(repo, &["scip", "check"]);
    assert!(
        out.status.success(),
        "non-indexable file must not stale the index: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// A negative path answer is an explicit coverage verdict. It identifies
/// the indexed commit/dirty worktree, missing compiler index, unresolved
/// reason classes, and files extraction could not index.
#[test]
fn negative_path_reports_snapshot_and_coverage_gaps() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    std::fs::write(repo.join("lib.rs"), "pub fn from() {}\npub fn to() {}\n").unwrap();
    std::fs::write(repo.join("bad.rs"), [0xff, 0xfe]).unwrap();
    let git = |args: &[&str]| {
        let out = 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()
            .unwrap();
        assert!(out.status.success(), "git {args:?}");
    };
    git(&["init", "-q"]);
    git(&["add", "Cargo.toml", "lib.rs", "bad.rs"]);
    git(&["commit", "-qm", "fixture"]);

    let out = sinter(repo, &["build"]);
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    std::fs::write(
        repo.join("lib.rs"),
        "pub fn from() {}\npub fn to() {}\n// dirty, but query self-syncs it\n",
    )
    .unwrap();
    let out = sinter(repo, &["path", "from", "to", "--json", "--coverage"]);
    assert!(!out.status.success(), "a miss uses grep exit 1");
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    let coverage = &value["coverage"];
    assert_eq!(coverage["status"], "not_proven", "{value}");
    assert_eq!(coverage["conclusive"], false, "{value}");
    assert_eq!(coverage["snapshot"]["dirty"], true, "{value}");
    assert_eq!(
        coverage["snapshot"]["head"].as_str().unwrap().len(),
        40,
        "{value}"
    );
    assert_eq!(coverage["snapshot"]["working_tree_indexed"], true);
    assert_eq!(coverage["compiler_index"]["state"], "missing");
    assert!(
        coverage["compiler_index"]["missing_index_for"]
            .as_array()
            .unwrap()
            .iter()
            .any(|language| language == "rust"),
        "{value}"
    );
    assert!(
        coverage["graph"]["unindexed_files"]
            .as_array()
            .unwrap()
            .iter()
            .any(|file| file == "bad.rs"),
        "{value}"
    );
}

/// bare `scip`: fresh index is a one-line no-op that executes no indexer
/// (fake rust-analyzer on PATH records execution); stale index runs it.
#[cfg(unix)]
#[test]
fn bare_scip_skips_indexers_when_fresh() {
    let dir = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(repo.join("a.rs"), "pub fn f() {}\n").unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    // Fake rust-analyzer first on PATH: records execution, produces nothing.
    let marker = bin.path().join("executed");
    std::fs::write(
        bin.path().join("rust-analyzer"),
        format!("#!/bin/sh\ntouch {}\n", marker.display()),
    )
    .unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(
        bin.path().join("rust-analyzer"),
        std::fs::Permissions::from_mode(0o755),
    )
    .unwrap();
    let path_env = format!(
        "{}:{}",
        bin.path().display(),
        std::env::var("PATH").unwrap_or_default()
    );
    let run = |args: &[&str]| {
        Command::new(env!("CARGO_BIN_EXE_sinter"))
            .args(args)
            .current_dir(repo)
            .env("PATH", &path_env)
            .stdin(std::process::Stdio::null())
            .output()
            .expect("run sinter")
    };

    std::fs::create_dir_all(repo.join(".sinter")).unwrap();
    std::fs::write(repo.join(".sinter/index.scip"), b"stub").unwrap();
    set_mtime(
        &repo.join("a.rs"),
        SystemTime::now() - Duration::from_secs(60),
    );

    let out = run(&["scip"]);
    assert!(out.status.success());
    assert!(!marker.exists(), "fresh bare `scip` executed an indexer");
    assert!(
        String::from_utf8_lossy(&out.stdout).contains("nothing to do"),
        "{}",
        String::from_utf8_lossy(&out.stdout)
    );

    // Stale: the indexer must run (it produces nothing, so the command
    // fails afterwards — execution is the assertion).
    set_mtime(
        &repo.join("a.rs"),
        SystemTime::now() + Duration::from_secs(60),
    );
    run(&["scip"]);
    assert!(marker.exists(), "stale bare `scip` must run the indexer");
}

/// Source snippets are not projects. An extension alone must not launch an
/// indexer from a parent Rust repository or a fixture directory.
#[cfg(unix)]
#[test]
fn scip_skips_language_without_project_configuration() {
    let dir = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(repo.join("fixture.ts"), "export const value = 1;\n").unwrap();
    let marker = bin.path().join("typescript-executed");
    std::fs::write(
        bin.path().join("scip-typescript"),
        format!("#!/bin/sh\ntouch {}\n", marker.display()),
    )
    .unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(
        bin.path().join("scip-typescript"),
        std::fs::Permissions::from_mode(0o755),
    )
    .unwrap();
    let path_env = format!(
        "{}:{}",
        bin.path().display(),
        std::env::var("PATH").unwrap_or_default()
    );
    let out = Command::new(env!("CARGO_BIN_EXE_sinter"))
        .arg("scip")
        .current_dir(repo)
        .env("PATH", path_env)
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "no configured project can produce an index"
    );
    assert!(
        !marker.exists(),
        "extension-only fixture launched the indexer"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("no tsconfig*.json found"), "{stderr}");
}

/// A nested module must be indexed from its own project root. Running every
/// indexer at the repository root breaks multi-module repositories and makes
/// the emitted document paths disagree with Sinter's repo-relative paths.
#[cfg(unix)]
#[test]
fn scip_runs_indexer_from_nested_project_root() {
    let dir = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let repo = dir.path();
    let project = repo.join("services/worker");
    std::fs::create_dir_all(&project).unwrap();
    std::fs::write(project.join("go.mod"), "module example.com/worker\n").unwrap();
    std::fs::write(project.join("main.go"), "package main\nfunc main() {}\n").unwrap();

    let marker = bin.path().join("working-directory");
    std::fs::write(
        bin.path().join("scip-go"),
        format!("#!/bin/sh\npwd > '{}'\n", marker.display()),
    )
    .unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(
        bin.path().join("scip-go"),
        std::fs::Permissions::from_mode(0o755),
    )
    .unwrap();
    let path_env = format!(
        "{}:{}",
        bin.path().display(),
        std::env::var("PATH").unwrap_or_default()
    );

    let out = Command::new(env!("CARGO_BIN_EXE_sinter"))
        .arg("scip")
        .current_dir(repo)
        .env("PATH", path_env)
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();

    assert!(!out.status.success(), "fake indexer writes no SCIP output");
    assert_eq!(
        std::fs::read_to_string(marker).unwrap().trim(),
        project.canonicalize().unwrap().to_string_lossy()
    );
}

/// A build ingests `.sinter/index.scip` whenever it exists — including an
/// index older than the code it binds. The report has to say so: silent
/// ingestion of stale compiler evidence is how a graph starts lying.
#[test]
fn build_names_an_ingested_stale_index() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join(".sinter")).unwrap();
    std::fs::write(repo.join("a.rs"), "pub fn f() {}\n").unwrap();
    // Empty but valid SCIP: parses, binds nothing, ingests cleanly.
    let index = repo.join(".sinter/index.scip");
    std::fs::write(&index, b"").unwrap();

    set_mtime(&index, SystemTime::now() - Duration::from_secs(60));
    let out = sinter(repo, &["build"]);
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "{text}");
    assert!(text.contains("SCIP index is older"), "{text}");
    assert!(text.contains("rerun `sinter scip`"), "{text}");

    set_mtime(&index, SystemTime::now() + Duration::from_secs(60));
    let out = sinter(repo, &["build"]);
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(!text.contains("SCIP index is older"), "{text}");
}

/// A definition file edited after indexing: the index's occurrence
/// positions there no longer name what the compiler saw, so its symbols
/// bind nothing (a stale `certain` edge on a renamed line is worse than no
/// edge) and the call site left behind surfaces as an actionable gap.
#[test]
fn edited_definition_file_withholds_scip_evidence() {
    use protobuf::Message;
    use scip::types::{Document, Index, Occurrence};

    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    // fn main() { crate::util::helper(); }
    //                          ^25..31
    std::fs::write(
        repo.join("src/main.rs"),
        "mod util;\nfn main() { crate::util::helper(); }\n",
    )
    .unwrap();
    std::fs::write(repo.join("src/util.rs"), "pub fn helper() {}\n").unwrap();
    let symbol = "rust-analyzer cargo fixture 0.1.0 util/helper().";
    let index = Index {
        documents: vec![
            Document {
                relative_path: "src/util.rs".to_string(),
                occurrences: vec![Occurrence {
                    range: vec![0, 7, 13],
                    symbol: symbol.to_string(),
                    symbol_roles: scip::types::SymbolRole::Definition as i32,
                    ..Default::default()
                }],
                ..Default::default()
            },
            Document {
                relative_path: "src/main.rs".to_string(),
                occurrences: vec![Occurrence {
                    range: vec![1, 25, 31],
                    symbol: symbol.to_string(),
                    symbol_roles: 0,
                    ..Default::default()
                }],
                ..Default::default()
            },
        ],
        ..Default::default()
    };
    std::fs::create_dir_all(repo.join(".sinter")).unwrap();
    std::fs::write(
        repo.join(".sinter/index.scip"),
        index.write_to_bytes().unwrap(),
    )
    .unwrap();
    let past = SystemTime::now() - Duration::from_secs(60);
    set_mtime(&repo.join("src/main.rs"), past);
    set_mtime(&repo.join("src/util.rs"), past);
    set_mtime(&repo.join("Cargo.toml"), past);

    let out = sinter(repo, &["build"]);
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let out = sinter(repo, &["affected", "helper", "--json"]);
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(text.contains("main"), "{text}");

    // Rename the definition; the caller is untouched (index now stale).
    std::fs::write(repo.join("src/util.rs"), "pub fn helper2() {}\n").unwrap();
    set_mtime(
        &repo.join("src/util.rs"),
        SystemTime::now() + Duration::from_secs(60),
    );
    let out = sinter(repo, &["build"]);
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );

    // The stale occurrence must not rebind the caller to the renamed
    // symbol by position.
    let out = sinter(repo, &["affected", "helper2", "--json"]);
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(value["dependents"], serde_json::json!([]), "{value}");
    assert_eq!(
        value["coverage"]["evidence"]["certain"]["results"], 0,
        "{value}"
    );
    assert_eq!(
        value["coverage"]["compiler_index"]["state"], "stale",
        "{value}"
    );

    // And the dangling call is visible as an actionable gap.
    let out = sinter(repo, &["unresolved", "--name", "helper", "--json"]);
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(
        value["unresolved"][0]["reason"], "missing_internal_target",
        "{value}"
    );
    assert_eq!(
        value["unresolved"][0]["category"], "actionable_anchored_miss",
        "{value}"
    );
}