drep-ai 2.8.0

A local commit gate: runs the linters your repo configures, and sends changed code to an LLM for review
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
//! The cache file, and when it is trusted.

use std::path::{Path, PathBuf};

use super::super::{Cached, QuirksSource, Registry};
use super::{Canned, DOCUMENT};

const KIMI: &str = "https://api.kimi.com/coding/v1";
const WEEK: u64 = 7 * 24 * 60 * 60;

/// A cache path inside `dir`, never the developer's real one.
fn cache_path(dir: &tempfile::TempDir) -> PathBuf {
    dir.path().join("model-quirks.toml")
}

/// Write a registry stamped `fetched_at` to `path`.
fn seed(path: &Path, fetched_at: u64) {
    Registry::distil(DOCUMENT, fetched_at)
        .expect("distils")
        .save(path)
        .expect("saves");
}

#[tokio::test]
async fn a_fresh_cache_is_used_without_fetching() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    seed(&path, 1_000);

    let fetcher = Canned::offline();
    let registry = Cached::at(Some(path), &fetcher, 1_000 + WEEK)
        .registry()
        .await
        .expect("the cache answers");

    assert_eq!(fetcher.calls.get(), 0, "a fresh cache costs no request");
    assert!(registry.facts(KIMI, "k3").is_some());
}

#[tokio::test]
async fn a_cache_one_second_past_the_week_is_refetched() {
    // The boundary itself, because `>` and `>=` are one character apart and
    // both look right.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    seed(&path, 1_000);

    let fetcher = Canned::serving(DOCUMENT);
    Cached::at(Some(path), &fetcher, 1_000 + WEEK + 1)
        .registry()
        .await
        .expect("the fetch answers");

    assert_eq!(fetcher.calls.get(), 1);
}

#[tokio::test]
async fn a_missing_cache_is_fetched_and_written() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);

    let fetcher = Canned::serving(DOCUMENT);
    let registry = Cached::at(Some(path.clone()), &fetcher, 5_000)
        .registry()
        .await
        .expect("the fetch answers");

    assert_eq!(fetcher.calls.get(), 1);
    assert_eq!(
        Registry::load(&path).as_ref(),
        Some(&registry),
        "what was fetched is what was cached"
    );
}

#[tokio::test]
async fn what_was_written_is_read_back_without_a_second_fetch() {
    // The cache is only worth writing if the next run trusts it, which no test
    // of `save` alone can show.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);

    let first = Canned::serving(DOCUMENT);
    Cached::at(Some(path.clone()), &first, 5_000)
        .registry()
        .await
        .expect("the fetch answers");

    let second = Canned::offline();
    let registry = Cached::at(Some(path), &second, 5_001)
        .registry()
        .await
        .expect("the cache answers");

    assert_eq!(second.calls.get(), 0);
    assert_eq!(
        registry.facts(KIMI, "k3").and_then(|f| f.output_limit),
        Some(131_072),
        "the facts survive the round trip through TOML"
    );
}

#[tokio::test]
async fn an_unreadable_cache_is_fetched() {
    // A directory where the file should be: `read_to_string` fails, and the
    // caller must treat that as "no cache" rather than as an error to report.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    std::fs::create_dir(&path).expect("mkdir");

    let fetcher = Canned::serving(DOCUMENT);
    let registry = Cached::at(Some(path), &fetcher, 5_000)
        .registry()
        .await
        .expect("the fetch answers");

    assert_eq!(fetcher.calls.get(), 1);
    assert!(registry.facts(KIMI, "k3").is_some());
}

#[tokio::test]
async fn an_unparseable_cache_is_fetched() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    std::fs::write(&path, "this is not toml {{{").expect("write");

    let fetcher = Canned::serving(DOCUMENT);
    Cached::at(Some(path), &fetcher, 5_000)
        .registry()
        .await
        .expect("the fetch answers");

    assert_eq!(fetcher.calls.get(), 1);
}

#[tokio::test]
async fn a_cache_of_the_wrong_shape_is_fetched_too() {
    // Valid TOML, wrong contents - a truncated write, or a file from a future
    // version. Parsing it as an empty registry would silently answer every
    // lookup with "unknown" and never refetch.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    std::fs::write(&path, "unrelated = true\n").expect("write");

    let fetcher = Canned::serving(DOCUMENT);
    Cached::at(Some(path), &fetcher, 5_000)
        .registry()
        .await
        .expect("the fetch answers");

    assert_eq!(fetcher.calls.get(), 1);
}

#[tokio::test]
async fn a_failed_fetch_with_no_cache_reports_why() {
    // Non-fatal to `drep init` - the wizard reports this and carries on - but
    // it has to be an error rather than an empty registry, or the caller cannot
    // tell "models.dev says nothing about your model" from "models.dev was
    // unreachable".
    let dir = tempfile::tempdir().expect("tempdir");

    let err = Cached::at(Some(cache_path(&dir)), &Canned::offline(), 5_000)
        .registry()
        .await
        .expect_err("nothing to answer with");

    assert!(err.to_string().contains("could not reach"), "got {err}");
}

#[tokio::test]
async fn a_failed_fetch_falls_back_to_a_stale_cache() {
    // What a model accepts does not change once it has shipped, so a week-old
    // copy is worth more than nothing. Refusing it would make a user offline
    // for eight days strictly worse off than one offline for six.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    seed(&path, 1_000);

    let registry = Cached::at(Some(path), &Canned::offline(), 1_000 + 30 * WEEK)
        .registry()
        .await
        .expect("the stale cache answers");

    assert_eq!(
        registry.facts(KIMI, "k3").and_then(|f| f.output_limit),
        Some(131_072)
    );
}

#[tokio::test]
async fn a_document_that_will_not_distil_leaves_the_cache_alone() {
    // Caching a document drep could not read would make every later run answer
    // "unknown" from disk for a week.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);

    Cached::at(
        Some(path.clone()),
        &Canned::serving("<html>502</html>"),
        5_000,
    )
    .registry()
    .await
    .expect_err("the document is unreadable");

    assert!(!path.exists(), "nothing should have been written");
}

#[tokio::test]
async fn no_cache_path_fetches_every_time() {
    // A platform with no config directory. Slower, never wrong.
    let fetcher = Canned::serving(DOCUMENT);
    let source = Cached::at(None, &fetcher, 5_000);

    source.registry().await.expect("first");
    source.registry().await.expect("second");

    assert_eq!(fetcher.calls.get(), 2);
}

#[tokio::test]
async fn a_cache_that_cannot_be_written_still_answers() {
    // The registry in hand is the same either way, so a read-only config
    // directory is a slower run rather than a failed one.
    let dir = tempfile::tempdir().expect("tempdir");
    let blocked = dir.path().join("a-file");
    std::fs::write(&blocked, "not a directory").expect("write");

    let registry = Cached::at(
        Some(blocked.join("model-quirks.toml")),
        &Canned::serving(DOCUMENT),
        5_000,
    )
    .registry()
    .await
    .expect("the fetch still answers");

    assert!(registry.facts(KIMI, "k3").is_some());
}

#[test]
fn a_clock_that_moved_backwards_reads_as_fresh() {
    // `now - fetched_at` on unsigned integers wraps into an enormous age, which
    // would refetch on every run for as long as the clock was behind.
    let registry = Registry::distil(DOCUMENT, 10_000).expect("distils");

    assert!(!registry.is_stale(0));
    assert!(!registry.is_stale(9_999));
}

#[test]
fn a_cache_exactly_a_week_old_is_still_fresh() {
    let registry = Registry::distil(DOCUMENT, 1_000).expect("distils");

    assert!(!registry.is_stale(1_000 + WEEK));
    assert!(registry.is_stale(1_000 + WEEK + 1));
}

#[test]
fn saving_reports_a_directory_it_cannot_create() {
    let dir = tempfile::tempdir().expect("tempdir");
    let blocked = dir.path().join("a-file");
    std::fs::write(&blocked, "not a directory").expect("write");

    let err = Registry::distil(DOCUMENT, 0)
        .expect("distils")
        .save(&blocked.join("nested").join("model-quirks.toml"))
        .expect_err("the parent cannot be created");

    assert!(err.to_string().contains("could not write"), "got {err}");
}

#[cfg(unix)]
#[test]
fn saving_never_writes_through_a_predictable_temporary_symlink() {
    use std::os::unix::fs::symlink;

    let dir = tempfile::tempdir().expect("tempdir");
    let path = cache_path(&dir);
    let predictable = path.with_extension("toml.tmp");
    let outside = dir.path().join("outside.txt");
    std::fs::write(&outside, "keep me").expect("outside seed");
    symlink(&outside, &predictable).expect("planted sibling symlink");

    Registry::distil(DOCUMENT, 0)
        .expect("distils")
        .save(&path)
        .expect("saves without using the planted name");

    assert_eq!(
        std::fs::read_to_string(&outside).expect("outside body"),
        "keep me",
        "cache publication must not write through an attacker-planted sibling symlink"
    );
    assert!(path.is_file(), "the cache must still be published");
}

#[test]
fn the_cache_keeps_only_the_two_facts_drep_reads() {
    // The reason the document is distilled rather than stored: models.dev is
    // ~4 MB against the 600 KB this writes, and drep reads a boolean and an
    // integer per model. Everything else the vendors publish is dropped.
    //
    // Asserted as "the ignored fields are gone" rather than as a size ratio.
    // A ratio over a cut-down fixture measures how verbose the fixture is -
    // adding one model to it moved the number - while what distillation has to
    // guarantee is which fields survive.
    let body = toml::to_string(&Registry::distil(DOCUMENT, 0).expect("distils")).expect("toml");

    for dropped in ["context", "reasoning", "\"name\"", "Kimi K3"] {
        assert!(
            !body.contains(dropped),
            "`{dropped}` is not a fact drep reads, so it must not reach the cache:\n{body}"
        );
    }
    assert!(
        body.contains("output_limit") && body.contains("temperature"),
        "and the two that are read must survive:\n{body}"
    );
    assert!(
        body.len() < DOCUMENT.len(),
        "distilling must not grow the document: {} bytes from {}",
        body.len(),
        DOCUMENT.len()
    );
}

#[cfg(unix)]
#[test]
fn a_directory_the_cache_creates_is_private() {
    // The cache shares a directory with `auth.toml`. Creating it here without
    // narrowing it would leave the credential store's own directory
    // world-readable whenever `drep init` happened to cache first.
    use std::os::unix::fs::PermissionsExt;

    let dir = tempfile::tempdir().expect("tempdir");
    let nested = dir.path().join("drep");
    Registry::distil(DOCUMENT, 0)
        .expect("distils")
        .save(&nested.join("model-quirks.toml"))
        .expect("saves");

    let mode = std::fs::metadata(&nested)
        .expect("metadata")
        .permissions()
        .mode();
    assert_eq!(mode & 0o777, 0o700, "got {:o}", mode & 0o777);
}

#[test]
fn the_override_relocates_the_cache_and_the_default_sits_beside_auth_toml() {
    // Read as a parameter rather than from the process, because
    // `std::env::set_var` is `unsafe` in edition 2024 and `cargo test` is
    // multi-threaded - the same split `auth::path_from` exists for.
    use super::super::path_from;

    assert_eq!(
        path_from(Some(std::ffi::OsString::from("/tmp/elsewhere.toml"))),
        Some(PathBuf::from("/tmp/elsewhere.toml"))
    );

    let default = path_from(None).expect("this platform has a config directory");
    assert_eq!(
        default.file_name().and_then(|n| n.to_str()),
        Some("model-quirks.toml")
    );
    assert_eq!(
        default.parent(),
        crate::auth::path_from(None)
            .expect("this platform has a config directory")
            .parent(),
        "the cache and the credential store are siblings"
    );
}

#[test]
fn the_stamp_on_a_new_registry_is_the_wall_clock() {
    // `unix_now` stamps every registry drep writes and is the `now` every
    // staleness check is measured against, so a constant would make the cache
    // either permanently fresh or permanently stale - and both look like a
    // working cache from the outside.
    let now = super::super::unix_now();

    assert!(now > 1_735_689_600, "before 2025: {now}");
    assert!(now < 4_102_444_800, "after 2100: {now}");
}

#[test]
fn the_default_path_reads_the_override_variable() {
    // The variable is read, never written: `std::env::set_var` is `unsafe` in
    // edition 2024 and `cargo test` is multi-threaded, so this asserts against
    // whichever state the process is already in.
    use super::super::{PATH_VAR, default_path, path_from};

    match std::env::var_os(PATH_VAR) {
        Some(overridden) => assert_eq!(default_path(), Some(PathBuf::from(overridden))),
        None => {
            let platform = default_path().expect("this platform has a config directory");
            assert_eq!(
                platform.file_name().and_then(|name| name.to_str()),
                Some("model-quirks.toml")
            );
            assert_eq!(platform, path_from(None).expect("the same platform path"));
        }
    }
}