drep-ai 2.6.1

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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! A5, A6, A7, A8, A9: every shape of the LLM section.

use crate::cli::doctor::{DoctorArgs, run_at, run_to};
use std::path::Path;

fn args(dir: &Path) -> DoctorArgs {
    DoctorArgs {
        path: dir.to_path_buf(),
        config: None,
    }
}

fn args_with_config(dir: &Path, config: std::path::PathBuf) -> DoctorArgs {
    DoctorArgs {
        path: dir.to_path_buf(),
        config: Some(config),
    }
}

fn write_py(dir: &Path) {
    std::fs::write(dir.join("a.py"), "x = 1\n").expect("a.py");
}

/// Run `doctor` against `dir` and return what it printed.
fn report_for(dir: &Path) -> String {
    let mut out = Vec::new();
    run_to(&mut out, &args(dir)).expect("run_to");
    String::from_utf8(out).expect("utf8")
}

#[test]
fn no_config_file_prints_the_unconfigured_message_and_still_prints_tools() {
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());

    let mut out = Vec::new();
    let exit = run_to(&mut out, &args(dir.path())).expect("run_to");
    assert_eq!(exit, crate::Exit::Clean);
    let rendered = String::from_utf8(out).expect("utf8");

    let root = dir.path().canonicalize().expect("canonical");
    let expected_path = root.join("drep.toml");
    assert!(
        rendered.contains("No config file at"),
        "rendered:\n{rendered}"
    );
    assert!(
        rendered.contains(&expected_path.display().to_string()),
        "rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Run `drep init`"),
        "rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Deterministic checks"),
        "LLM section must not suppress the rest of the report; rendered:\n{rendered}"
    );
}

#[test]
fn two_providers_render_in_file_order() {
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    let body = "\
        [[llm]]\n\
        model = \"model-a\"\n\
        endpoint = \"http://a.example/v1\"\n\
        \n\
        [[llm]]\n\
        model = \"model-b\"\n\
        endpoint = \"http://b.example/v1\"\n\
        ";
    std::fs::write(dir.path().join("drep.toml"), body).expect("drep.toml");

    let mut out = Vec::new();
    let exit = run_to(&mut out, &args(dir.path())).expect("run_to");
    assert_eq!(exit, crate::Exit::Clean);
    let rendered = String::from_utf8(out).expect("utf8");

    let a_idx = rendered
        .find("  1. model-a at http://a.example/v1")
        .expect("first provider line");
    let b_idx = rendered
        .find("  2. model-b at http://b.example/v1")
        .expect("second provider line");
    assert!(
        a_idx < b_idx,
        "providers render in file order; rendered:\n{rendered}"
    );
}

#[test]
fn unset_env_var_is_named_and_provider_still_renders() {
    // The discriminating case: routing display through `config::load` would
    // fail on the unset variable and never print the model. The raw-file
    // path is what makes both halves appear.
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    let body = "\
        [[llm]]\n\
        model = \"m\"\n\
        endpoint = \"http://example/v1\"\n\
        api_key = \"${DREP_TEST_DOCTOR_UNSET_XYZ}\"\n\
        ";
    std::fs::write(dir.path().join("drep.toml"), body).expect("drep.toml");

    let mut out = Vec::new();
    let exit = run_to(&mut out, &args(dir.path())).expect("run_to");
    assert_eq!(exit, crate::Exit::Clean);
    let rendered = String::from_utf8(out).expect("utf8");

    assert!(
        rendered.contains("DREP_TEST_DOCTOR_UNSET_XYZ is NOT set"),
        "rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("  1. m at http://example/v1"),
        "the provider's model/endpoint line must still render; rendered:\n{rendered}"
    );
}

#[test]
fn invalid_toml_is_reported_and_run_to_returns_clean() {
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    let body = "[[llm]\nmodel = "; // truncated mid-key
    std::fs::write(dir.path().join("drep.toml"), body).expect("drep.toml");

    let mut out = Vec::new();
    let exit = run_to(&mut out, &args(dir.path())).expect("run_to must not error");
    assert_eq!(exit, crate::Exit::Clean);
    let rendered = String::from_utf8(out).expect("utf8");

    assert!(
        rendered.contains("could not be parsed"),
        "rendered:\n{rendered}"
    );
}

#[test]
fn provider_with_no_model_and_no_endpoint_renders_placeholder_text() {
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    let body = "[[llm]]\n";
    std::fs::write(dir.path().join("drep.toml"), body).expect("drep.toml");

    let mut out = Vec::new();
    let exit = run_to(&mut out, &args(dir.path())).expect("run_to");
    assert_eq!(exit, crate::Exit::Clean);
    let rendered = String::from_utf8(out).expect("utf8");

    assert!(
        rendered.contains("  1. (no model set) at (no endpoint set)"),
        "rendered:\n{rendered}"
    );
}

#[test]
fn explicit_config_flag_overrides_default_path() {
    // Sanity check for the `--config` path: pointing at a file outside the
    // root makes the report read *that* file rather than `drep.toml`.
    let dir = tempfile::tempdir().expect("tempdir");
    let other = tempfile::tempdir().expect("other tempdir");
    write_py(dir.path());
    std::fs::write(
        other.path().join("custom.toml"),
        "[[llm]]\nmodel = \"x\"\nendpoint = \"http://x/v1\"\n",
    )
    .expect("custom.toml");

    let mut out = Vec::new();
    let exit = run_to(
        &mut out,
        &args_with_config(dir.path(), other.path().join("custom.toml")),
    )
    .expect("run_to");
    assert_eq!(exit, crate::Exit::Clean);
    let rendered = String::from_utf8(out).expect("utf8");

    assert!(
        rendered.contains("  1. x at http://x/v1"),
        "rendered:\n{rendered}"
    );
}

/// A disabled provider is marked inert rather than listed as if it will run.
///
/// The listing was truthful when only the head was ever consulted only by
/// accident - it said nothing about which entries were live. Now that the list
/// is a real failover chain, an entry the chain will skip is the one thing this
/// section can still misreport.
#[test]
fn a_disabled_provider_is_marked_as_skipped() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        r#"
[[llm]]
enabled = false
model = "local-model"
endpoint = "http://localhost:1234/v1"

[[llm]]
model = "cloud-model"
endpoint = "https://api.example/v1"
"#,
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    let local = rendered
        .lines()
        .find(|line| line.contains("local-model"))
        .unwrap_or_else(|| panic!("the disabled entry must still be listed:\n{rendered}"));
    assert!(
        local.contains("disabled"),
        "the disabled entry must say so; got {local:?}"
    );
    let cloud = rendered
        .lines()
        .find(|line| line.contains("cloud-model"))
        .expect("the enabled entry is listed");
    assert!(
        !cloud.contains("disabled"),
        "the enabled entry must not be marked skipped; got {cloud:?}"
    );
}

/// With one provider, `doctor` says there is no fallback.
///
/// "Providers are tried in order" is true here and useless. The fact this user
/// needs is that an unreachable endpoint means exit 2 with nothing to catch it.
#[test]
fn a_single_provider_is_reported_as_having_no_fallback() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        "[[llm]]\nmodel = \"only\"\nendpoint = \"http://localhost:1234/v1\"\n",
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        rendered.contains("no fallback"),
        "a lone provider must be reported as having no fallback:\n{rendered}"
    );
}

/// With two enabled providers, `doctor` states the failover rule - including
/// the exception, because "it falls through" without "except on a 401" is the
/// half that leads a user to expect their broken key to be routed around.
#[test]
fn two_providers_are_reported_as_a_failover_chain_with_the_401_exception() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        r#"
[[llm]]
model = "a"
endpoint = "http://a/v1"

[[llm]]
model = "b"
endpoint = "http://b/v1"
"#,
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        rendered.contains("2 providers, tried in order"),
        "the count and the order must be stated:\n{rendered}"
    );
    assert!(
        rendered.contains("401"),
        "the exception must be stated too:\n{rendered}"
    );
}

/// Every provider disabled: the report says the gate cannot run at all.
///
/// `config::load` rejects this file, so `drep check` would fail with the same
/// message - but `doctor` is the command a user runs to find out *why*, and it
/// must not stop at listing two entries that look fine.
#[test]
fn an_all_disabled_config_is_reported_as_unable_to_run() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        r#"
[[llm]]
enabled = false
model = "a"

[[llm]]
enabled = false
model = "b"
"#,
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        rendered.contains("Every provider is disabled"),
        "an all-disabled config must be called out:\n{rendered}"
    );
}

/// The numbering is the chain position, not the position in the file.
///
/// `drep check` reports a failure as "[1] cloud-model", meaning the head of the
/// chain. If this listing numbered the file instead, a parked entry above would
/// make the same provider "2" here and "1" there - two numbering schemes for
/// one list, and a user counting blocks in `drep.toml` to find the offender
/// would land on the wrong one.
#[test]
fn providers_are_numbered_by_chain_position_not_file_position() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        r#"
[[llm]]
enabled = false
model = "parked"
endpoint = "http://parked/v1"

[[llm]]
model = "leads-the-chain"
endpoint = "http://live/v1"
"#,
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        rendered.contains("1. leads-the-chain at http://live/v1"),
        "the first ENABLED entry is number 1, whatever sits above it:\n{rendered}"
    );
    let parked = rendered
        .lines()
        .find(|line| line.contains("parked"))
        .expect("the disabled entry is still listed");
    assert!(
        !parked.contains("1."),
        "a disabled entry holds no position in the chain; got {parked:?}"
    );
}

/// An `llm` key that is not an array of tables is reported as such.
///
/// Reporting this as "declares no `[[llm]]` provider" points the user at a
/// command that will refuse to overwrite their file and skips the
/// `config::load` check that names the real problem.
#[test]
fn a_non_array_llm_key_is_not_reported_as_a_missing_provider() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        "[llm]\nmodel = \"x\"\nendpoint = \"http://a/v1\"\n",
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        !rendered.contains("declares no `[[llm]]` provider"),
        "the key is present - saying it is absent sends the user to `drep init`:\n{rendered}"
    );
    assert!(
        rendered.contains("not a `[[llm]]` array of tables"),
        "the shape problem must be named:\n{rendered}"
    );
    assert!(
        rendered.contains("will not load"),
        "and the load error must still be surfaced:\n{rendered}"
    );
}

/// An empty `llm = []` array reports as "no provider", not "all disabled".
///
/// The two look alike once the entries are gone: an empty array walks the same
/// listing loop as an all-disabled one and reaches the same trailing summary.
/// They are different problems — one needs a provider written, the other needs
/// one re-enabled — and the message has to send the user to the right fix.
#[test]
fn an_empty_llm_array_is_reported_as_declaring_no_provider() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(temp.path().join("drep.toml"), "llm = []\n").expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        rendered.contains("declares no `[[llm]]` provider"),
        "an empty array declares no provider:\n{rendered}"
    );
    assert!(
        !rendered.contains("Every provider is disabled"),
        "there is nothing to re-enable - that message sends the user to the wrong fix:\n{rendered}"
    );
}

/// A parked provider's unset variable is not reported as a problem.
///
/// `config::load` no longer expands a disabled entry, so the variable is not
/// required — and `doctor` warning "LLM analysis will fail until you export it"
/// about a run that will succeed is the same disagreement, in the opposite
/// direction, that its old narrower `${VAR}` scanner produced.
#[test]
fn a_disabled_providers_unset_env_var_is_not_warned_about() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        r#"
[[llm]]
model = "live"
endpoint = "http://live/v1"

[[llm]]
enabled = false
model = "parked"
endpoint = "http://parked/v1"
api_key = "${DREP_DOCTOR_VAR_THAT_IS_NOT_SET}"
"#,
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        !rendered.contains("DREP_DOCTOR_VAR_THAT_IS_NOT_SET"),
        "the parked provider's variable is not required:\n{rendered}"
    );
    assert!(
        !rendered.contains("will not load"),
        "and the config does load:\n{rendered}"
    );
}

/// An *enabled* provider's unset variable is still reported.
///
/// The discriminating half: a scanner that skipped every provider would pass
/// the test above and go silent on the one warning that matters.
#[test]
fn an_enabled_providers_unset_env_var_is_still_warned_about() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join("drep.toml"),
        r#"
[[llm]]
model = "live"
endpoint = "http://live/v1"
api_key = "${DREP_DOCTOR_VAR_THAT_IS_NOT_SET}"

[[llm]]
enabled = false
model = "parked"
"#,
    )
    .expect("write config");

    let rendered = report_for(temp.path());
    assert!(
        rendered.contains("DREP_DOCTOR_VAR_THAT_IS_NOT_SET is NOT set"),
        "the live provider's variable must still be flagged:\n{rendered}"
    );
}

/// Run `doctor` against `dir` with an empty auth store, and return its output.
///
/// The store is a temp path so the report never depends on what the developer
/// has stored, and never writes to the real one.
fn report_with_empty_store(dir: &Path) -> String {
    let mut out = Vec::new();
    run_at(&mut out, &args(dir), &dir.join("auth.toml")).expect("run_at");
    String::from_utf8(out).expect("utf8")
}

#[test]
fn a_var_reference_is_echoed_so_the_reader_can_see_which_one() {
    // The whole reason doctor reads the raw tree rather than the loaded config:
    // `${VAR}` has to print as itself.
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    std::fs::write(
        dir.path().join("drep.toml"),
        "[[llm]]\nendpoint = \"http://e/v1\"\nmodel = \"m\"\napi_key = \"${SOME_TOKEN}\"\n",
    )
    .expect("config");

    let report = report_with_empty_store(dir.path());

    assert!(report.contains("${SOME_TOKEN}"), "got {report}");
    assert!(report.contains("from drep.toml"), "got {report}");
}

#[test]
fn a_literal_key_is_never_echoed() {
    // `config::load` accepts a literal `api_key`, and doctor's output is what
    // people paste into bug reports and CI logs. Printing the value verbatim
    // would put a live credential in both.
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    std::fs::write(
        dir.path().join("drep.toml"),
        "[[llm]]\nendpoint = \"http://e/v1\"\nmodel = \"m\"\napi_key = \"sk-live-secret-value\"\n",
    )
    .expect("config");

    let report = report_with_empty_store(dir.path());

    assert!(
        !report.contains("sk-live-secret-value"),
        "the key reached the report: {report}"
    );
    assert!(
        report.contains("a literal value"),
        "and the reader is told a key is set, and to prefer a variable: {report}"
    );
}

#[test]
fn a_provider_with_no_key_anywhere_says_where_to_get_one() {
    let dir = tempfile::tempdir().expect("tempdir");
    write_py(dir.path());
    std::fs::write(
        dir.path().join("drep.toml"),
        "[[llm]]\nendpoint = \"http://e/v1\"\nmodel = \"m\"\n",
    )
    .expect("config");

    let report = report_with_empty_store(dir.path());

    assert!(report.contains("drep auth login"), "got {report}");
}