cora-code 0.17.0

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
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
use crate::error::CoraError;
use sha2::{Digest, Sha256};
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::debug;

use crate::engine::types::ReviewResponse;

/// Get the cache directory: ~/.codecora/cora-code/cache/reviews/
fn cache_dir() -> std::result::Result<PathBuf, CoraError> {
    let dir = crate::data_dir::cora_data_dir()
        .join("cache")
        .join("reviews");

    // One-time migration from legacy ~/.cache/cora/reviews/
    migrate_legacy_cache_dir(&dir);

    Ok(dir)
}

/// Migrate review cache from legacy `~/.cache/cora/reviews/` to the new location.
fn migrate_legacy_cache_dir(new_dir: &std::path::Path) {
    let marker = new_dir
        .parent()
        .map(|p| p.join(".migrated-to-codecora"))
        .unwrap_or_else(|| new_dir.join(".migrated-to-codecora"));
    if marker.is_file() {
        return;
    }

    let Some(home) = dirs::home_dir() else {
        return;
    };
    let old_dir = home.join(".cache").join("cora").join("reviews");
    if !old_dir.is_dir() {
        // No legacy cache — write marker so we never check again
        let _ = std::fs::create_dir_all(new_dir);
        let _ = std::fs::write(&marker, "1");
        return;
    }

    // Ensure new directory exists
    if let Err(e) = std::fs::create_dir_all(new_dir) {
        debug!("skip cache migration, cannot create new dir: {e}");
        return;
    }

    // Move all .json cache files
    let entries = match std::fs::read_dir(&old_dir) {
        Ok(e) => e,
        Err(_) => return,
    };

    let mut count = 0;
    for entry in entries.flatten() {
        let path = entry.path();
        if path.extension().is_some_and(|ext| ext == "json") {
            let filename = path.file_name().unwrap_or_default();
            let new_path = new_dir.join(filename);
            if !new_path.exists() && std::fs::rename(&path, &new_path).is_ok() {
                count += 1;
            }
        }
    }

    // Write marker
    let _ = std::fs::write(&marker, "1");

    if count > 0 {
        debug!(
            "migrated {count} cache files from ~/.cache/cora/reviews/ to ~/.codecora/cora-code/cache/reviews/"
        );
    }
}

/// Compute SHA-256 hex digest of the diff content + config parameters.
/// Includes model, provider, base_url, and temperature so config changes
/// invalidate the cache (e.g., switching providers with the same model name).
#[allow(clippy::format_collect)]
fn cache_key(diff: &str, model: &str, temperature: f32, provider: &str, base_url: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(diff.as_bytes());
    hasher.update(model.as_bytes());
    hasher.update(provider.as_bytes());
    hasher.update(base_url.as_bytes());
    hasher.update(temperature.to_le_bytes());
    let result = hasher.finalize();
    result.iter().map(|b| format!("{b:02x}")).collect()
}

/// Get cached review response if available and not expired.
///
/// Returns `None` if no cache exists or if the cache entry has expired
/// (based on `ttl` in minutes).
pub fn get_cached_review(
    diff: &str,
    model: &str,
    temperature: f32,
    ttl: u64,
    provider: &str,
    base_url: &str,
) -> Option<ReviewResponse> {
    let hash = cache_key(diff, model, temperature, provider, base_url);
    let dir = cache_dir().ok()?;
    let path = dir.join(format!("{hash}.json"));

    if !path.is_file() {
        debug!("cache miss: file not found");
        return None;
    }

    let content = std::fs::read_to_string(&path).ok()?;
    let cached: CachedReview = match serde_json::from_str(&content) {
        Ok(c) => c,
        Err(e) => {
            debug!("cache corrupt, ignoring: {}", e);
            return None;
        }
    };

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let age_secs = now.saturating_sub(cached.timestamp);
    let ttl_secs = ttl * 60;

    if age_secs > ttl_secs {
        debug!(age_secs = age_secs, ttl_secs = ttl_secs, "cache expired");
        // Clean up expired cache file
        let _ = std::fs::remove_file(&path);
        return None;
    }

    debug!(hash = %hash, age_secs = age_secs, "cache hit");
    Some(cached.response)
}

/// Save a review response to the cache.
pub fn save_cached_review(
    diff: &str,
    model: &str,
    temperature: f32,
    response: &ReviewResponse,
    provider: &str,
    base_url: &str,
) -> std::result::Result<(), CoraError> {
    let dir = cache_dir()?;
    std::fs::create_dir_all(&dir).map_err(CoraError::CacheIo)?;

    let hash = cache_key(diff, model, temperature, provider, base_url);
    let path = dir.join(format!("{hash}.json"));

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let cached = CachedReview {
        response: response.clone(),
        timestamp: now,
    };

    let json = serde_json::to_string_pretty(&cached)
        .map_err(|e| CoraError::CacheSerialize(e.to_string()))?;

    std::fs::write(&path, json).map_err(CoraError::CacheIo)?;

    debug!(hash = %hash, "saved review to cache");
    Ok(())
}

/// Internal cache entry format.
#[derive(serde::Serialize, serde::Deserialize)]
struct CachedReview {
    response: ReviewResponse,
    timestamp: u64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::Severity;
    use crate::engine::types::ReviewIssue;

    fn make_response() -> ReviewResponse {
        ReviewResponse {
            issues: vec![ReviewIssue {
                rule_id: None,
                also_matches: Vec::new(),
                file: "src/main.rs".to_string(),
                line: Some(10),
                severity: Severity::Major,
                issue_type: Some("bugs".to_string()),
                title: "Null pointer".to_string(),
                body: "Could be null here".to_string(),
                suggested_fix: Some("Add a check".to_string()),
            }],
            summary: "Found 1 issue.".to_string(),
            tokens_used: None,
            should_block: false,
        }
    }

    #[test]
    fn cache_key_is_deterministic() {
        let hash1 = cache_key(
            "hello world",
            "gpt-4",
            0.0,
            "openai",
            "https://api.openai.com/v1",
        );
        let hash2 = cache_key(
            "hello world",
            "gpt-4",
            0.0,
            "openai",
            "https://api.openai.com/v1",
        );
        assert_eq!(hash1, hash2);
        assert_eq!(hash1.len(), 64); // SHA-256 hex = 64 chars
    }

    #[test]
    fn cache_key_differs_for_different_inputs() {
        let hash1 = cache_key(
            "hello world",
            "gpt-4",
            0.0,
            "openai",
            "https://api.openai.com/v1",
        );
        let hash2 = cache_key(
            "hello earth",
            "gpt-4",
            0.0,
            "openai",
            "https://api.openai.com/v1",
        );
        assert_ne!(hash1, hash2);
    }

    #[test]
    fn cache_key_includes_model_and_temperature() {
        let h1 = cache_key("diff", "gpt-4", 0.0, "openai", "https://api.openai.com/v1");
        let h2 = cache_key(
            "diff",
            "gpt-3.5",
            0.0,
            "openai",
            "https://api.openai.com/v1",
        );
        let h3 = cache_key("diff", "gpt-4", 0.7, "openai", "https://api.openai.com/v1");
        assert_ne!(h1, h2, "different models should differ");
        assert_ne!(h1, h3, "different temperatures should differ");
    }

    #[test]
    fn cache_key_len_is_64() {
        let diff = "diff --git a/file.txt b/file.txt\n+ hello";
        let hash = cache_key(diff, "model", 0.0, "openai", "https://api.openai.com/v1");
        assert_eq!(hash.len(), 64);
    }

    #[test]
    fn cache_miss_on_different_diff() {
        let diff1 = "diff --git a/a.txt b/a.txt\n+ hello";
        let diff2 = "diff --git a/b.txt b/b.txt\n+ world";
        let hash1 = cache_key(diff1, "model", 0.0, "openai", "https://api.openai.com/v1");
        let hash2 = cache_key(diff2, "model", 0.0, "openai", "https://api.openai.com/v1");
        assert_ne!(hash1, hash2);
    }

    #[test]
    fn cache_key_differs_for_different_providers() {
        let h1 = cache_key("diff", "gpt-4", 0.0, "openai", "https://api.openai.com/v1");
        let h2 = cache_key(
            "diff",
            "gpt-4",
            0.0,
            "azure",
            "https://my-azure.openai.azure.com",
        );
        assert_ne!(
            h1, h2,
            "different providers should produce different cache keys"
        );
    }

    #[test]
    fn cache_key_differs_for_different_base_urls() {
        let h1 = cache_key("diff", "gpt-4", 0.0, "openai", "https://api.openai.com/v1");
        let h2 = cache_key(
            "diff",
            "gpt-4",
            0.0,
            "openai",
            "https://proxy.example.com/v1",
        );
        assert_ne!(
            h1, h2,
            "different base_urls should produce different cache keys"
        );
    }

    #[test]
    fn cached_review_serialization_roundtrip() {
        let response = make_response();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let cached = CachedReview {
            response,
            timestamp: now,
        };

        let json = serde_json::to_string(&cached).unwrap();
        let back: CachedReview = serde_json::from_str(&json).unwrap();
        assert_eq!(back.timestamp, now);
        assert_eq!(back.response.issues.len(), 1);
        assert_eq!(back.response.issues[0].file, "src/main.rs");
        assert_eq!(back.response.summary, "Found 1 issue.");
    }

    #[test]
    fn ttl_expiry() {
        let response = make_response();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let cached = CachedReview {
            response,
            timestamp: now.saturating_sub(2 * 60 * 60), // 2 hours ago
        };

        let json = serde_json::to_string(&cached).unwrap();
        let back: CachedReview = serde_json::from_str(&json).unwrap();

        // With ttl = 60 (1 hour), this should be expired
        let age_secs = now - back.timestamp;
        let ttl_secs = 60 * 60;
        assert!(age_secs > ttl_secs);
    }

    #[test]
    fn ttl_not_expired() {
        let response = make_response();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let cached = CachedReview {
            response,
            timestamp: now.saturating_sub(10 * 60), // 10 minutes ago
        };

        let json = serde_json::to_string(&cached).unwrap();
        let back: CachedReview = serde_json::from_str(&json).unwrap();

        // With ttl = 1440 (24h), this should NOT be expired
        let age_secs = now - back.timestamp;
        let ttl_secs = 1440 * 60;
        assert!(age_secs <= ttl_secs);
    }

    #[test]
    fn save_and_get_cached_review() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path();

        // Manually set up a cache entry in the temp dir
        let diff = "test diff content";
        let hash = cache_key(diff, "model", 0.0, "openai", "https://api.openai.com/v1");
        let path = dir.join(format!("{hash}.json"));

        let response = make_response();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let cached = CachedReview {
            response: response.clone(),
            timestamp: now,
        };

        let json = serde_json::to_string(&cached).unwrap();
        std::fs::write(&path, json).unwrap();

        // Verify we can read it back
        let content = std::fs::read_to_string(&path).unwrap();
        let back: CachedReview = serde_json::from_str(&content).unwrap();
        assert_eq!(back.response.issues.len(), 1);
        assert_eq!(back.response.issues[0].title, "Null pointer");
    }
}