Skip to main content

drep/cli/init/
config_file.rs

1//! Render and write `drep.toml`.
2//!
3//! The on-disk shape is fixed by Phase 5b: every entry has its documented
4//! position, comments explain *why* a field exists (or doesn't), and the file
5//! parses cleanly through [`crate::config::load`] once the referenced env
6//! vars are set. `init` and `config` agreeing about the file is the whole
7//! reason `[[llm]]` lands in this phase rather than in 5c.
8//!
9//! Escaping happens here rather than at a higher level, so `render` is the
10//! single place a caller's value could fail to escape. See `escape` for what
11//! TOML actually requires - it is more than the two characters this originally
12//! handled.
13
14use std::path::{Path, PathBuf};
15
16use anyhow::{Context, Result, anyhow};
17
18use super::presets::{LlmPreset, PresetBackend};
19use crate::llm::quirks::Quirks;
20
21/// Fields used by exactly one execution backend.
22#[derive(Debug, Clone)]
23enum ChoiceBackend {
24    Http {
25        endpoint: String,
26        key_in_store: bool,
27        quirks: Quirks,
28    },
29    Codex,
30}
31
32/// One provider the user chose, ready to render.
33///
34/// `endpoint` and `model` are resolved rather than optional: the wizard and the
35/// flag path both fall back to the preset's defaults before building this, so
36/// the renderer never has to decide what "no model" means.
37#[derive(Debug, Clone)]
38pub struct Choice {
39    /// The preset this came from - it supplies the protocol, the timeout and
40    /// the environment variable name.
41    pub preset: &'static LlmPreset,
42    /// The model to ask for.
43    pub model: String,
44    /// Backend-specific values, already resolved by the wizard or flag path.
45    backend: ChoiceBackend,
46}
47
48impl Choice {
49    pub fn http(
50        preset: &'static LlmPreset,
51        model: String,
52        endpoint: String,
53        key_in_store: bool,
54        quirks: Quirks,
55    ) -> Self {
56        assert!(
57            matches!(preset.backend, PresetBackend::Http(_)),
58            "HTTP choice requires an HTTP preset"
59        );
60        Self {
61            preset,
62            model,
63            backend: ChoiceBackend::Http {
64                endpoint,
65                key_in_store,
66                quirks,
67            },
68        }
69    }
70
71    pub fn codex(preset: &'static LlmPreset, model: String) -> Self {
72        assert!(
73            matches!(preset.backend, PresetBackend::Codex(_)),
74            "Codex choice requires a Codex preset"
75        );
76        Self {
77            preset,
78            model,
79            backend: ChoiceBackend::Codex,
80        }
81    }
82
83    pub fn is_http(&self) -> bool {
84        matches!(self.backend, ChoiceBackend::Http { .. })
85    }
86
87    #[cfg(test)]
88    pub fn endpoint(&self) -> Option<&str> {
89        match &self.backend {
90            ChoiceBackend::Http { endpoint, .. } => Some(endpoint),
91            ChoiceBackend::Codex => None,
92        }
93    }
94
95    pub fn key_in_store(&self) -> bool {
96        match &self.backend {
97            ChoiceBackend::Http { key_in_store, .. } => *key_in_store,
98            ChoiceBackend::Codex => false,
99        }
100    }
101
102    #[cfg(test)]
103    pub fn quirks(&self) -> Quirks {
104        match &self.backend {
105            ChoiceBackend::Http { quirks, .. } => *quirks,
106            ChoiceBackend::Codex => self.preset.quirks(),
107        }
108    }
109}
110
111/// Render the whole `drep.toml`, comments included, for a failover chain.
112///
113/// Produces exactly the shape [`crate::config::load`] accepts: one `[[llm]]`
114/// table per choice in order, the keys in the order they appear in the file (the
115/// TOML document parser preserves order; serde reads them positionally), and
116/// each optional line only when the choice calls for it.
117///
118/// The order is the chain: entry one is tried first and each later entry is a
119/// fallback for the one before it.
120pub fn render_chain(choices: &[Choice]) -> String {
121    let mut body = String::new();
122    body.push_str("# drep configuration, written by `drep init`.\n");
123    body.push_str("#\n");
124    body.push_str("# Providers are declared as `[[llm]]`, an ordered array of tables: a\n");
125    body.push_str("# preference order. Each one is tried in turn, and a transport failure -\n");
126    body.push_str("# unreachable, timed out, rate limited, 5xx, or an empty answer - falls\n");
127    body.push_str("# through to the next. A 401 or 403 does not: that is a broken key, and\n");
128    body.push_str("# failing over would hide it. Add a fallback by adding another block:\n");
129    body.push_str("#\n");
130    body.push_str("#     [[llm]]\n");
131    body.push_str("#     endpoint = \"https://openrouter.ai/api/v1\"\n");
132    body.push_str("#     model = \"deepseek/deepseek-v4-pro-0813\"\n");
133    body.push_str("#\n");
134    body.push_str("# Set `enabled = false` on a block to park it without deleting it.\n");
135    body.push_str("#\n");
136    if choices.iter().any(Choice::is_http) {
137        body.push_str(
138            "# API keys are NOT in this file. `drep init` stores them per machine, keyed\n",
139        );
140        body.push_str("# by endpoint, so this file carries only the provider choice and can be\n");
141        body.push_str("# committed. `drep auth list` shows what is stored. To pin a key to a\n");
142        body.push_str(
143            "# variable instead - which is what CI wants - add `api_key = \"${VAR}\"` to\n",
144        );
145        body.push_str("# a block; an explicit value always wins over the stored one.\n");
146    }
147    if choices.iter().any(|choice| !choice.is_http()) {
148        body.push_str("# Codex owns ChatGPT subscription login and token refresh.\n");
149        body.push_str("# Run `codex login`; drep never reads or stores those credentials.\n");
150    }
151
152    for choice in choices {
153        body.push('\n');
154        render_one(&mut body, choice);
155    }
156
157    body
158}
159
160/// Render one `[[llm]]` block into `body`.
161fn render_one(body: &mut String, choice: &Choice) {
162    let preset = choice.preset;
163
164    body.push_str("[[llm]]\n");
165    body.push_str("enabled = true\n");
166    if let (PresetBackend::Codex(codex), ChoiceBackend::Codex) = (&preset.backend, &choice.backend)
167    {
168        body.push_str("backend = \"codex\"\n");
169        body.push_str(&format!("model = \"{}\"\n", escape(&choice.model)));
170        if let Some(effort) = &codex.reasoning_effort {
171            body.push_str(&format!("reasoning_effort = \"{}\"\n", effort.as_str()));
172        }
173        if let Some(timeout) = preset.timeout_secs {
174            body.push_str(&format!("timeout_secs = {timeout}\n"));
175        }
176        body.push_str(&format!("max_concurrent = {}\n", codex.max_concurrent));
177        body.push_str(
178            "# Reviews consume ChatGPT/Codex subscription allowance, not OpenAI API billing.\n",
179        );
180        return;
181    }
182
183    let (http, endpoint, key_in_store, quirks) = match (&preset.backend, &choice.backend) {
184        (
185            PresetBackend::Http(http),
186            ChoiceBackend::Http {
187                endpoint,
188                key_in_store,
189                quirks,
190            },
191        ) => (http, endpoint.as_str(), *key_in_store, quirks),
192        _ => panic!("choice backend does not match preset `{}`", preset.key),
193    };
194    body.push_str(&format!("endpoint = \"{}\"\n", escape(endpoint)));
195    body.push_str(&format!("model = \"{}\"\n", escape(&choice.model)));
196
197    // Omitted entirely when the key is in the store: an explicit `api_key` wins
198    // over a stored one, so writing `${VAR}` here would override the key
199    // `drep init` just saved with a variable nobody set.
200    if !key_in_store && let Some(env) = http.api_key_env {
201        body.push_str(&format!("api_key = \"${{{env}}}\"\n"));
202    }
203
204    // Written only when the preset names one, so an OpenAI-compatible block keeps
205    // the shape it has had since 2.0 and no existing file has to be migrated.
206    if let Some(protocol) = http.protocol {
207        body.push_str("# This endpoint speaks Anthropic's messages API, not chat completions.\n");
208        body.push_str(&format!("protocol = \"{}\"\n", escape(protocol)));
209    }
210
211    // Written only for an endpoint that refuses a request without it. Everywhere
212    // else an unset cap is what stops a reasoning model being truncated mid-thought.
213    //
214    // The second comment line says where the number came from, because "this is
215    // the model's own limit" is a claim in a file the user commits, and it is
216    // false whenever the registry could not name the model.
217    if let Some(max_tokens) = quirks.max_tokens {
218        body.push_str("# Required by this endpoint: it refuses a request that omits the field.\n");
219        if quirks.max_tokens_from_registry {
220            body.push_str(
221                "# This is the model's own published output limit, not a cap drep chose.\n",
222            );
223        } else {
224            body.push_str(
225                "# This model's own limit is not known here, so it is the provider's fallback:\n\
226                 # set well above any review-sized response.\n",
227            );
228        }
229        body.push_str(&format!("max_tokens = {max_tokens}\n"));
230    }
231
232    // Absent means the parameter is omitted from the request entirely, which is what
233    // a model that rejects it requires. That is a property of the model, so the
234    // chosen model decides rather than the file inheriting a default.
235    match quirks.temperature {
236        Some(temperature) => {
237            // `{:?}` rather than `{}`: Display renders `1.0` as `1`, which TOML
238            // reads as an *integer* and `config::load` then refuses with a type
239            // error - from a file `drep init` had just reported writing.
240            body.push_str(&format!("temperature = {temperature:?}\n"));
241        }
242        None => {
243            body.push_str("# `temperature` is deliberately absent: this model rejects the\n");
244            body.push_str("# parameter, and the resulting 400 neither fails over nor retries.\n");
245        }
246    }
247
248    if let Some(timeout) = preset.timeout_secs {
249        body.push_str(
250            "# A reasoning model can spend minutes on one file; the wall clock has to match.\n",
251        );
252        body.push_str(&format!("timeout_secs = {timeout}\n"));
253    }
254
255    if quirks.max_tokens.is_none() {
256        body.push_str(
257            "# max_tokens is deliberately unset: with no completion cap, a reasoning model\n",
258        );
259        body.push_str("# is never truncated mid-thought. Set it only to cap spend.\n");
260    }
261}
262
263/// The refusal to overwrite an existing config.
264///
265/// Shared with `init::existing_config`, which has to make the same decision
266/// *before* the wizard asks anything - the wizard stores a pasted key on its
267/// way through, so refusing at the write half-applies the run. Two copies of
268/// the message meant two places to keep the `--force` instruction in step.
269pub fn already_exists(path: &Path) -> anyhow::Error {
270    anyhow!(
271        "{} already exists. Re-run with --force to replace it.",
272        path.display()
273    )
274}
275
276/// Escape a string for a TOML basic string.
277///
278/// `\` and `"` are the obvious two, and were once the only two handled here -
279/// which was wrong: TOML forbids the literal control characters U+0000-U+0008,
280/// U+000A-U+001F and U+007F inside a basic string. A model or endpoint
281/// carrying a stray `\r` (a URL pasted from a CRLF file is the realistic way
282/// in) produced a `drep.toml` that `config::load` then refused to parse - so
283/// `drep init` reported success and left behind a config nothing could read,
284/// and `write` would not replace it without `--force`.
285///
286/// The characters with short escapes get them (tab included - legal literally,
287/// but clearer escaped in a file a human reads); anything else in the control
288/// range becomes `\uXXXX`.
289fn escape(s: &str) -> String {
290    let mut out = String::with_capacity(s.len());
291    for ch in s.chars() {
292        match ch {
293            '\\' => out.push_str("\\\\"),
294            '"' => out.push_str("\\\""),
295            '\n' => out.push_str("\\n"),
296            '\r' => out.push_str("\\r"),
297            '\t' => out.push_str("\\t"),
298            '\u{8}' => out.push_str("\\b"),
299            '\u{c}' => out.push_str("\\f"),
300            // The rest of the control range has no short form.
301            c if c.is_control() => out.push_str(&format!("\\u{:04X}", c as u32)),
302            other => out.push(other),
303        }
304    }
305    out
306}
307
308/// Write `drep.toml` under `root`.
309///
310/// Refuses to overwrite an existing file unless `force`. The error names the
311/// path and points at `--force` so the user does not have to read code to
312/// learn how to recover.
313pub fn write(root: &Path, body: &str, force: bool) -> Result<PathBuf> {
314    use std::io::Write;
315
316    let path = root.join(crate::config::default_config_path());
317    let mut temporary = tempfile::NamedTempFile::new_in(root)
318        .with_context(|| format!("could not write {}", path.display()))?;
319    temporary
320        .write_all(body.as_bytes())
321        .with_context(|| format!("could not write {}", path.display()))?;
322    temporary
323        .as_file()
324        .sync_all()
325        .with_context(|| format!("could not write {}", path.display()))?;
326
327    // Publish only a complete file. `persist_noclobber` keeps the no-force
328    // existence check atomic; `persist` replaces the directory entry itself,
329    // so `--force` cannot follow a symlink and overwrite its target.
330    let published = if force {
331        temporary.persist(&path)
332    } else {
333        temporary.persist_noclobber(&path)
334    };
335    match published {
336        Ok(_) => Ok(path),
337        Err(err) => match (force, err.error.kind()) {
338            (false, std::io::ErrorKind::AlreadyExists) => Err(already_exists(&path)),
339            _ => Err(anyhow::Error::new(err.error)
340                .context(format!("could not write {}", path.display()))),
341        },
342    }
343}
344
345#[cfg(test)]
346mod tests {
347    use super::*;
348
349    #[test]
350    fn a_write_that_fails_for_another_reason_is_not_reported_as_already_existing() {
351        // `already_exists` names `--force` as the way out, so reporting it for
352        // a missing directory or a permission error sends the user to a flag
353        // that cannot help - and `--force` would then fail the same way with
354        // the same message.
355        let dir = tempfile::tempdir().expect("tempdir");
356        let missing = dir.path().join("no-such-directory");
357
358        let err = write(&missing, "model = \"x\"\n", false)
359            .expect_err("there is no directory to write into");
360
361        assert!(
362            !err.to_string().contains("already exists"),
363            "nothing exists here; got {err}"
364        );
365        assert!(err.to_string().contains("could not write"), "got {err}");
366    }
367
368    #[cfg(unix)]
369    #[test]
370    fn a_dangling_symlink_named_drep_toml_is_refused_rather_than_followed() {
371        // `Path::exists` reports false for a symlink whose target is missing,
372        // while `fs::write` creates the target - so the guard said "nothing is
373        // there" and the write went somewhere nobody named.
374        let dir = tempfile::tempdir().expect("tempdir");
375        let elsewhere = dir.path().join("elsewhere.toml");
376        std::os::unix::fs::symlink(&elsewhere, dir.path().join("drep.toml")).expect("symlink");
377
378        let err = write(dir.path(), "model = \"x\"\n", false)
379            .expect_err("a dangling symlink is still something being there");
380
381        assert!(err.to_string().contains("drep.toml"), "got {err}");
382        assert!(
383            !elsewhere.exists(),
384            "and nothing was written through it to {}",
385            elsewhere.display()
386        );
387    }
388
389    #[test]
390    fn escape_handles_backslash_and_quote() {
391        assert_eq!(escape("plain"), "plain");
392        assert_eq!(escape(r#"a"b"#), r#"a\"b"#);
393        assert_eq!(escape(r"a\b"), r"a\\b");
394        assert_eq!(escape(r#"a"b\c"#), r#"a\"b\\c"#);
395    }
396}