the-code-graph-eval 0.1.1

Evaluation framework for The Code 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
use serde::Deserialize;
use std::path::{Path, PathBuf};

use domain::error::{CodeGraphError, Result};

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
pub struct SuiteManifest {
    pub suite: SuiteInfo,
    pub repos: Vec<ManifestRepo>,
}

#[derive(Debug, Deserialize)]
pub struct SuiteInfo {
    pub name: String,
    pub description: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ManifestRepo {
    pub name: String,
    pub url: String,
    pub revision: String,
    pub languages: Vec<String>,
}

#[derive(Debug, Deserialize)]
pub struct SearchQueryFile {
    pub queries: Vec<SearchQuery>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct SearchQuery {
    pub repo: String,
    pub query: String,
    pub expected: Vec<String>,
    #[serde(default)]
    pub category: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ImpactQueryFile {
    pub scenarios: Vec<ImpactScenario>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ImpactScenario {
    pub repo: String,
    pub description: String,
    pub target: String,
    pub depth: usize,
    pub confidence: String,
    pub expected_affected: Vec<String>,
}

// ---------------------------------------------------------------------------
// Cache management
// ---------------------------------------------------------------------------

pub fn eval_cache_dir() -> Result<PathBuf> {
    if let Ok(xdg) = std::env::var("XDG_CACHE_HOME") {
        return Ok(PathBuf::from(xdg).join("code-graph-eval"));
    }
    let home = std::env::var("HOME").map_err(|_| CodeGraphError::Other("HOME not set".into()))?;
    Ok(PathBuf::from(home).join(".cache").join("code-graph-eval"))
}

pub fn repo_cache_path(repo: &ManifestRepo) -> Result<PathBuf> {
    Ok(eval_cache_dir()?.join(&repo.name).join(&repo.revision))
}

pub fn validate_cache(repo: &ManifestRepo) -> Result<bool> {
    let path = repo_cache_path(repo)?;
    if !path.exists() {
        return Ok(false);
    }
    let marker = path.join(".revision");
    if !marker.exists() {
        return Ok(false);
    }
    let stored = std::fs::read_to_string(&marker)
        .map_err(|e| CodeGraphError::Other(format!("read .revision: {e}")))?;
    Ok(stored.trim() == repo.revision)
}

pub fn clone_or_cache(repo: &ManifestRepo, no_cache: bool) -> Result<PathBuf> {
    let cache_path = repo_cache_path(repo)?;

    if no_cache {
        if cache_path.exists() {
            std::fs::remove_dir_all(&cache_path)
                .map_err(|e| CodeGraphError::Other(format!("remove cache: {e}")))?;
        }
    } else if validate_cache(repo)? {
        tracing::info!(repo = %repo.name, "Using cached clone");
        return Ok(cache_path);
    }

    // Validate inputs to prevent git argument injection
    if repo.revision.starts_with('-') {
        return Err(CodeGraphError::Other(format!(
            "invalid revision: '{}' (must not start with '-')",
            repo.revision
        )));
    }
    if !repo.url.starts_with("https://") && !repo.url.starts_with("http://") {
        return Err(CodeGraphError::Other(format!(
            "invalid repo URL: '{}' (must be an HTTP(S) URL)",
            repo.url
        )));
    }

    tracing::info!(repo = %repo.name, revision = %repo.revision, "Cloning");
    if cache_path.exists() {
        std::fs::remove_dir_all(&cache_path)
            .map_err(|e| CodeGraphError::Other(format!("remove stale cache: {e}")))?;
    }
    std::fs::create_dir_all(&cache_path)
        .map_err(|e| CodeGraphError::Other(format!("mkdir: {e}")))?;

    let output = std::process::Command::new("git")
        .args([
            "clone",
            "--depth",
            "1",
            "--branch",
            &repo.revision,
            &repo.url,
        ])
        .arg(&cache_path)
        .output()
        .map_err(|e| CodeGraphError::Other(format!("git clone failed: {e}")))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(CodeGraphError::Other(format!("git clone failed: {stderr}")));
    }

    std::fs::write(cache_path.join(".revision"), &repo.revision)
        .map_err(|e| CodeGraphError::Other(format!("write .revision: {e}")))?;

    Ok(cache_path)
}

pub fn clear_cache(repo: &ManifestRepo) -> Result<()> {
    let path = repo_cache_path(repo)?;
    if path.exists() {
        std::fs::remove_dir_all(&path)
            .map_err(|e| CodeGraphError::Other(format!("clear cache: {e}")))?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Manifest / query parsing
// ---------------------------------------------------------------------------

pub fn parse_manifest(path: &Path) -> Result<SuiteManifest> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CodeGraphError::Other(format!("Failed to read manifest: {e}")))?;
    serde_json::from_str(&content)
        .map_err(|e| CodeGraphError::Other(format!("Invalid manifest JSON: {e}")))
}

pub fn parse_search_queries(path: &Path) -> Result<Vec<SearchQuery>> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CodeGraphError::Other(format!("Failed to read queries: {e}")))?;
    let file: SearchQueryFile = serde_json::from_str(&content)
        .map_err(|e| CodeGraphError::Other(format!("Invalid query JSON: {e}")))?;
    Ok(file.queries)
}

pub fn parse_impact_queries(path: &Path) -> Result<Vec<ImpactScenario>> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CodeGraphError::Other(format!("Failed to read scenarios: {e}")))?;
    let file: ImpactQueryFile = serde_json::from_str(&content)
        .map_err(|e| CodeGraphError::Other(format!("Invalid scenario JSON: {e}")))?;
    Ok(file.scenarios)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use std::sync::Mutex;

    /// Tests that mutate `XDG_CACHE_HOME` must hold this lock to avoid races.
    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn test_repo() -> ManifestRepo {
        ManifestRepo {
            name: "test-repo".into(),
            url: "https://github.com/example/test-repo.git".into(),
            revision: "abc123".into(),
            languages: vec!["rust".into()],
        }
    }

    const MANIFEST_JSON: &str = r#"{
        "suite": {
            "name": "search-v1",
            "description": "Search evaluation suite"
        },
        "repos": [
            {
                "name": "sample-repo",
                "url": "https://github.com/example/sample.git",
                "revision": "v1.0.0",
                "languages": ["rust", "python"]
            }
        ]
    }"#;

    const SEARCH_QUERIES_JSON: &str = r#"{
        "queries": [
            {
                "repo": "sample-repo",
                "query": "find all error handlers",
                "expected": ["src/error.rs", "src/handler.rs"]
            }
        ]
    }"#;

    const IMPACT_QUERIES_JSON: &str = r#"{
        "scenarios": [
            {
                "repo": "sample-repo",
                "description": "Change error type",
                "target": "src/error.rs::AppError",
                "depth": 3,
                "confidence": "high",
                "expected_affected": ["src/handler.rs", "src/main.rs"]
            }
        ]
    }"#;

    // -- SearchQuery category field ----------------------------------------

    #[test]
    fn search_query_category_deserialization() {
        let json =
            r#"{"repo": "test", "query": "foo", "expected": ["a::b"], "category": "semantic"}"#;
        let q: SearchQuery = serde_json::from_str(json).unwrap();
        assert_eq!(q.category.unwrap(), "semantic");
    }

    #[test]
    fn search_query_category_optional() {
        let json = r#"{"repo": "test", "query": "foo", "expected": ["a::b"]}"#;
        let q: SearchQuery = serde_json::from_str(json).unwrap();
        assert!(q.category.is_none());
    }

    // -- Manifest parsing ---------------------------------------------------

    #[test]
    fn parse_search_manifest() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("manifest.json");
        std::fs::write(&path, MANIFEST_JSON).unwrap();

        let manifest = parse_manifest(&path).unwrap();
        assert_eq!(manifest.suite.name, "search-v1");
        assert_eq!(manifest.suite.description, "Search evaluation suite");
        assert_eq!(manifest.repos.len(), 1);
        assert_eq!(manifest.repos[0].name, "sample-repo");
        assert_eq!(manifest.repos[0].revision, "v1.0.0");
        assert_eq!(manifest.repos[0].languages, vec!["rust", "python"]);
    }

    #[test]
    fn parse_search_manifest_invalid_json() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.json");
        std::fs::write(&path, "{ not valid json }").unwrap();

        let err = parse_manifest(&path).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("Invalid manifest JSON"),
            "expected clear error, got: {msg}"
        );
    }

    // -- Query parsing ------------------------------------------------------

    #[test]
    fn parse_search_queries() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("search.json");
        std::fs::write(&path, SEARCH_QUERIES_JSON).unwrap();

        let queries = super::parse_search_queries(&path).unwrap();
        assert_eq!(queries.len(), 1);
        assert_eq!(queries[0].repo, "sample-repo");
        assert_eq!(queries[0].query, "find all error handlers");
        assert_eq!(queries[0].expected, vec!["src/error.rs", "src/handler.rs"]);
    }

    #[test]
    fn parse_impact_queries() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("impact.json");
        std::fs::write(&path, IMPACT_QUERIES_JSON).unwrap();

        let scenarios = super::parse_impact_queries(&path).unwrap();
        assert_eq!(scenarios.len(), 1);
        assert_eq!(scenarios[0].repo, "sample-repo");
        assert_eq!(scenarios[0].description, "Change error type");
        assert_eq!(scenarios[0].target, "src/error.rs::AppError");
        assert_eq!(scenarios[0].depth, 3);
        assert_eq!(scenarios[0].confidence, "high");
        assert_eq!(
            scenarios[0].expected_affected,
            vec!["src/handler.rs", "src/main.rs"]
        );
    }

    // -- Cache dir resolution -----------------------------------------------

    #[test]
    fn cache_dir_resolution() {
        let repo = test_repo();
        let path = repo_cache_path(&repo).unwrap();
        // Must end with <name>/<revision>
        assert!(
            path.ends_with("test-repo/abc123"),
            "unexpected cache path: {path:?}"
        );
    }

    #[test]
    fn cache_dir_respects_xdg() {
        let _guard = ENV_LOCK.lock().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let xdg_path = dir.path().to_str().unwrap().to_string();

        unsafe { std::env::set_var("XDG_CACHE_HOME", &xdg_path) };
        let result = eval_cache_dir().unwrap();
        unsafe { std::env::remove_var("XDG_CACHE_HOME") };

        assert_eq!(
            result,
            PathBuf::from(&xdg_path).join("code-graph-eval"),
            "XDG_CACHE_HOME should be respected"
        );
    }

    // -- Cache validation ---------------------------------------------------

    #[test]
    fn validate_cache_missing_dir() {
        let _guard = ENV_LOCK.lock().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let fake_home = dir.path().to_str().unwrap().to_string();

        unsafe { std::env::set_var("XDG_CACHE_HOME", &fake_home) };
        let repo = test_repo();
        let valid = validate_cache(&repo).unwrap();
        unsafe { std::env::remove_var("XDG_CACHE_HOME") };

        assert!(!valid, "cache should be invalid when directory is missing");
    }

    #[test]
    fn validate_cache_wrong_revision() {
        let _guard = ENV_LOCK.lock().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let cache_root = dir.path().to_str().unwrap().to_string();
        let repo = test_repo();

        let cache_dir = dir
            .path()
            .join("code-graph-eval")
            .join(&repo.name)
            .join(&repo.revision);
        std::fs::create_dir_all(&cache_dir).unwrap();
        let mut f = std::fs::File::create(cache_dir.join(".revision")).unwrap();
        f.write_all(b"wrong-revision").unwrap();

        unsafe { std::env::set_var("XDG_CACHE_HOME", &cache_root) };
        let valid = validate_cache(&repo).unwrap();
        unsafe { std::env::remove_var("XDG_CACHE_HOME") };

        assert!(
            !valid,
            "cache should be invalid when revision doesn't match"
        );
    }

    #[test]
    fn validate_cache_valid() {
        let _guard = ENV_LOCK.lock().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let cache_root = dir.path().to_str().unwrap().to_string();
        let repo = test_repo();

        let cache_dir = dir
            .path()
            .join("code-graph-eval")
            .join(&repo.name)
            .join(&repo.revision);
        std::fs::create_dir_all(&cache_dir).unwrap();
        std::fs::write(cache_dir.join(".revision"), &repo.revision).unwrap();

        unsafe { std::env::set_var("XDG_CACHE_HOME", &cache_root) };
        let valid = validate_cache(&repo).unwrap();
        unsafe { std::env::remove_var("XDG_CACHE_HOME") };

        assert!(
            valid,
            "cache should be valid when dir exists and revision matches"
        );
    }

    // -- Clear cache --------------------------------------------------------

    #[test]
    fn clear_cache_removes_dir() {
        let _guard = ENV_LOCK.lock().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let cache_root = dir.path().to_str().unwrap().to_string();
        let repo = test_repo();

        let cache_dir = dir
            .path()
            .join("code-graph-eval")
            .join(&repo.name)
            .join(&repo.revision);
        std::fs::create_dir_all(&cache_dir).unwrap();
        std::fs::write(cache_dir.join(".revision"), &repo.revision).unwrap();
        assert!(cache_dir.exists(), "setup: cache dir should exist");

        unsafe { std::env::set_var("XDG_CACHE_HOME", &cache_root) };
        clear_cache(&repo).unwrap();
        unsafe { std::env::remove_var("XDG_CACHE_HOME") };

        assert!(
            !cache_dir.exists(),
            "cache dir should be removed after clear"
        );
    }
}