recursive-agent 0.8.3

A Rust coding-agent platform: kernel, tools, multi-agent orchestration, HTTP API, MCP, and TUI
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Remote provider preset catalog — download, cache, and merge with the
//! bundled + `providers.d` preset sources.
//!
//! # Design
//!
//! The bundled `providers.toml` is the compile-time fallback. A JSON cache
//! file at `<user_data_dir>/providers_cache.json` (written by
//! `recursive providers update`) holds presets fetched from an upstream
//! catalog URL. When the kernel resolves the *effective* preset list (see
//! [`crate::providers::all_presets_effective`]), cached presets override
//! bundled entries with the same `id` (so stale bundled pricing gets
//! refreshed upstream), while bundled presets absent from the cache are
//! kept as-is. User overrides from `providers.d/` are layered on top by
//! [`crate::providers::all_presets_effective`].
//!
//! # Cache file format
//!
//! ```json
//! {
//!   "schema_version": 1,
//!   "updated_at": "2026-06-06T00:00:00Z",
//!   "source_url": "https://...",
//!   "providers": [ ... ]
//! }
//! ```
//!
//! The `providers` array uses the same structure as [`ProviderPreset`] plus
//! the same optional fields (`anthropic_api_base`, `pricing`, etc.).
//!
//! # Network policy
//!
//! The only network touch is [`fetch_and_save`], invoked explicitly by
//! `recursive providers update` (and, if enabled, the best-effort
//! [`spawn_background_refresh`]). Preset *lookup* never hits the network —
//! [`load_cache`] reads a local JSON file. URLs are validated by
//! [`validate_providers_url`] which mirrors the SSRF guard in
//! `tools::web_fetch` (scheme + loopback/private/metadata blocking).

use std::net::IpAddr;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};

use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::paths::user_data_dir;
use crate::providers::ProviderPreset;

/// How old the cache can be before [`needs_update`] returns true.
pub const CACHE_TTL: Duration = Duration::from_secs(7 * 24 * 60 * 60);

/// Default upstream catalog. Users can override via the
/// `RECURSIVE_PROVIDERS_URL` env var or `recursive providers update --url`.
/// The repo `jeffkit/recursive-providers` publishes `providers.json` here.
pub const DEFAULT_PROVIDERS_URL: &str =
    "https://raw.githubusercontent.com/jeffkit/recursive-providers/main/providers.json";

/// Env var override for the upstream URL.
pub const PROVIDERS_URL_ENV: &str = "RECURSIVE_PROVIDERS_URL";

/// On-disk cache envelope.
#[derive(Debug, Serialize, Deserialize)]
pub struct ProvidersCache {
    pub schema_version: u32,
    /// RFC 3339 timestamp of when this cache was written.
    pub updated_at: String,
    /// URL this cache was fetched from.
    pub source_url: String,
    pub providers: Vec<ProviderPreset>,
}

/// Return the configured upstream URL: `RECURSIVE_PROVIDERS_URL` if set,
/// otherwise [`DEFAULT_PROVIDERS_URL`].
pub fn configured_url() -> String {
    std::env::var(PROVIDERS_URL_ENV).unwrap_or_else(|_| DEFAULT_PROVIDERS_URL.to_string())
}

/// Return path to the cache file: `<user_data_dir>/providers_cache.json`.
pub fn cache_path() -> PathBuf {
    user_data_dir().join("providers_cache.json")
}

/// Load the cache from disk. Returns `None` if the file is absent or
/// unreadable — callers fall back to bundled presets in that case.
pub fn load_cache() -> Option<ProvidersCache> {
    let path = cache_path();
    if !path.exists() {
        return None;
    }
    let content = std::fs::read_to_string(&path).ok()?;
    serde_json::from_str(&content).ok()
}

/// True if the cache is absent or older than [`CACHE_TTL`].
pub fn needs_update() -> bool {
    let path = cache_path();
    let metadata = match std::fs::metadata(&path) {
        Ok(m) => m,
        Err(_) => return true,
    };
    let modified = match metadata.modified() {
        Ok(t) => t,
        Err(_) => return true,
    };
    SystemTime::now()
        .duration_since(modified)
        .map(|age| age > CACHE_TTL)
        .unwrap_or(true)
}

/// Validate an upstream catalog URL. Mirrors the SSRF guard in
/// `tools::web_fetch`: requires an `http(s)` scheme and rejects
/// loopback / private / link-local / cloud-metadata hosts so a
/// misconfigured `RECURSIVE_PROVIDERS_URL` cannot be turned into an
/// internal-network probe.
pub fn validate_providers_url(url: &str) -> Result<()> {
    if !url.starts_with("http://") && !url.starts_with("https://") {
        return Err(Error::Config {
            message: "providers URL must start with http:// or https://".into(),
        });
    }

    let host = url
        .split("://")
        .nth(1)
        .and_then(|rest| rest.split('/').next())
        .and_then(|host_port| {
            if host_port.starts_with('[') {
                host_port.find(']').map(|i| &host_port[1..i])
            } else {
                host_port.split(':').next()
            }
        })
        .unwrap_or("");

    let host_lower = host.to_ascii_lowercase();
    if host_lower == "localhost"
        || host_lower.ends_with(".localhost")
        || host_lower == "metadata.google.internal"
    {
        return Err(Error::Config {
            message: format!("SSRF protection: host '{host}' is not allowed"),
        });
    }

    if let Ok(ip) = host.parse::<IpAddr>() {
        if is_non_routable_ip(ip) {
            return Err(Error::Config {
                message: format!("SSRF protection: IP address '{ip}' is not routable"),
            });
        }
    }

    Ok(())
}

/// Returns true for IP addresses that must not be reached
/// (loopback, private RFC-1918, link-local, cloud metadata, unspecified).
/// Duplicated from `tools::web_fetch` rather than shared, because the
/// fetch tool's helper is private and lifting it into a common util would
/// widen this change's blast radius.
fn is_non_routable_ip(ip: IpAddr) -> bool {
    match ip {
        IpAddr::V4(v4) => {
            v4.is_loopback()
                || v4.is_private()
                || v4.is_link_local()
                || v4.is_broadcast()
                || v4.is_unspecified()
        }
        IpAddr::V6(v6) => {
            v6.is_loopback()
                || v6.is_unspecified()
                || v6.is_multicast()
                || (v6.segments()[0] & 0xfe00) == 0xfc00
                || (v6.segments()[0] & 0xffc0) == 0xfe80
        }
    }
}

/// Download the providers JSON from `url`, parse it, write to the cache
/// file, and return the parsed [`ProvidersCache`]. The URL is validated by
/// [`validate_providers_url`] before any request is made.
///
/// Async — must be called from a tokio runtime.
pub async fn fetch_and_save(url: &str) -> Result<ProvidersCache> {
    validate_providers_url(url)?;

    let client = reqwest::Client::builder()
        .timeout(Duration::from_secs(15))
        .build()
        .map_err(Error::Http)?;

    let body = client
        .get(url)
        .header(
            "User-Agent",
            concat!("recursive-agent/", env!("CARGO_PKG_VERSION")),
        )
        .send()
        .await
        .map_err(Error::Http)?
        .error_for_status()
        .map_err(Error::Http)?
        .text()
        .await
        .map_err(Error::Http)?;

    let cache: ProvidersCache = serde_json::from_str(&body).map_err(|e| Error::Config {
        message: format!("providers JSON parse error: {e}"),
    })?;

    if cache.schema_version != 1 {
        return Err(Error::Config {
            message: format!(
                "unsupported providers cache schema version: {}",
                cache.schema_version
            ),
        });
    }

    let path = cache_path();
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(Error::Io)?;
    }
    let serialized = serde_json::to_string_pretty(&cache).map_err(|e| Error::Config {
        message: format!("failed to serialize cache: {e}"),
    })?;
    std::fs::write(&path, serialized).map_err(Error::Io)?;

    Ok(cache)
}

/// Merge cached presets over bundled ones. For each preset in `cache`, the
/// cached copy wins (placed first); bundled presets whose `id` is absent
/// from the cache are appended afterwards. Used by
/// [`crate::providers::all_presets_effective`].
pub fn merge_over_bundled(
    bundled: &[ProviderPreset],
    cache: &[ProviderPreset],
) -> Vec<ProviderPreset> {
    let mut result: Vec<ProviderPreset> = cache.to_vec();
    for bp in bundled {
        if !result.iter().any(|cp| cp.id == bp.id) {
            result.push(bp.clone());
        }
    }
    result
}

/// Spawn a best-effort background task that silently refreshes the cache if
/// it is stale. Errors are logged at `debug` level and never surface to the
/// caller. Returns immediately; the refresh happens concurrently. No-op if
/// the cache is fresh.
///
/// Only spawned by long-running command paths in the CLI, and only when
/// `RECURSIVE_PROVIDERS_AUTO_REFRESH=1` is set — one-shot commands must not
/// make surprise network requests.
pub fn spawn_background_refresh() {
    if !needs_update() {
        return;
    }
    if std::env::var("RECURSIVE_PROVIDERS_AUTO_REFRESH")
        .map(|v| v != "1")
        .unwrap_or(true)
    {
        return;
    }
    let url = configured_url();
    tokio::spawn(async move {
        match fetch_and_save(&url).await {
            Ok(cache) => {
                tracing::debug!(
                    "providers cache refreshed: {} presets from {}",
                    cache.providers.len(),
                    cache.source_url
                );
            }
            Err(e) => {
                tracing::debug!("providers cache refresh failed (non-fatal): {e}");
            }
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::{all_presets, ModelPricingSpec, ModelSpec};
    use crate::test_util::PinnedRecursiveHome;

    #[test]
    fn merge_overrides_bundled() {
        let bundled = all_presets();
        let mut fake = bundled[0].clone();
        fake.default_model = "fake-model-override".to_string();
        let merged = merge_over_bundled(bundled, &[fake]);
        assert_eq!(merged[0].default_model, "fake-model-override");
        assert_eq!(merged.len(), bundled.len());
    }

    #[test]
    fn merge_adds_new_preset() {
        let bundled = all_presets();
        let new_preset = ProviderPreset {
            id: "test-only-provider".to_string(),
            name: "Test".to_string(),
            provider_type: "openai".to_string(),
            api_base: "https://test.example.com/v1".to_string(),
            anthropic_api_base: None,
            default_model: "test-model".to_string(),
            models: vec![ModelSpec {
                name: "test-model".to_string(),
                context_window: 4096,
                max_tokens: None,
                pricing: Some(ModelPricingSpec {
                    input_per_million: 0.10,
                    output_per_million: 0.20,
                    cache_hit_input_per_million: None,
                }),
            }],
            mainland_accessible: false,
            key_env: "TEST_API_KEY".to_string(),
            key_url: "https://test.example.com/keys".to_string(),
        };
        let merged = merge_over_bundled(bundled, &[new_preset]);
        assert_eq!(merged.len(), bundled.len() + 1);
        assert!(merged.iter().any(|p| p.id == "test-only-provider"));
    }

    #[test]
    fn merge_empty_cache_returns_bundled() {
        let bundled = all_presets();
        let merged = merge_over_bundled(bundled, &[]);
        assert_eq!(merged.len(), bundled.len());
        assert_eq!(merged[0].id, bundled[0].id);
    }

    #[test]
    fn needs_update_true_when_no_cache() -> Result<(), Box<dyn std::error::Error>> {
        let tmp = tempfile::tempdir()?;
        let _pin = PinnedRecursiveHome::new(tmp.path());
        assert!(needs_update());
        Ok(())
    }

    #[test]
    fn validate_url_accepts_https_public_host() {
        assert!(validate_providers_url(DEFAULT_PROVIDERS_URL).is_ok());
        assert!(validate_providers_url("https://example.com/x.json").is_ok());
    }

    #[test]
    fn validate_url_rejects_non_http_scheme() {
        assert!(validate_providers_url("ftp://example.com/x").is_err());
        assert!(validate_providers_url("example.com/x").is_err());
    }

    #[test]
    fn validate_url_rejects_ssrf_targets() {
        assert!(validate_providers_url("http://127.0.0.1/x").is_err());
        assert!(validate_providers_url("http://localhost/x").is_err());
        assert!(validate_providers_url("http://10.0.0.1/x").is_err());
        assert!(validate_providers_url("http://169.254.169.254/x").is_err());
        assert!(validate_providers_url("http://[::1]/x").is_err());
        assert!(validate_providers_url("http://metadata.google.internal/x").is_err());
    }

    #[test]
    fn load_cache_returns_none_when_absent() -> Result<(), Box<dyn std::error::Error>> {
        let tmp = tempfile::tempdir()?;
        let _pin = PinnedRecursiveHome::new(tmp.path());
        assert!(load_cache().is_none());
        Ok(())
    }

    #[test]
    fn fetch_roundtrips_through_cache_file() -> Result<(), Box<dyn std::error::Error>> {
        // No network: write a cache envelope directly and confirm load_cache
        // parses it back. PinnedRecursiveHome points user_data_dir() at the
        // temp dir on every platform (incl. Windows, where HOME/USERPROFILE
        // pinning alone is unreliable).
        let tmp = tempfile::tempdir()?;
        let _pin = PinnedRecursiveHome::new(tmp.path());

        let cache = ProvidersCache {
            schema_version: 1,
            updated_at: "2026-06-06T00:00:00Z".to_string(),
            source_url: "https://example.com/providers.json".to_string(),
            providers: vec![ProviderPreset {
                id: "roundtrip".to_string(),
                name: "Roundtrip".to_string(),
                provider_type: "openai".to_string(),
                api_base: "https://rt.example.com/v1".to_string(),
                anthropic_api_base: None,
                default_model: "rt-1".to_string(),
                models: vec![],
                mainland_accessible: false,
                key_env: "RT_KEY".to_string(),
                key_url: "https://rt.example.com/keys".to_string(),
            }],
        };
        let path = cache_path();
        std::fs::create_dir_all(path.parent().unwrap())?;
        std::fs::write(&path, serde_json::to_string_pretty(&cache)?)?;

        let loaded = load_cache().expect("cache should load");
        assert_eq!(loaded.schema_version, 1);
        assert_eq!(loaded.providers.len(), 1);
        assert_eq!(loaded.providers[0].id, "roundtrip");
        Ok(())
    }

    // ── Additional gap-filling tests ─────────────────────────────────────────

    #[test]
    fn cache_ttl_is_seven_days() {
        let seven_days = Duration::from_secs(7 * 24 * 60 * 60);
        assert_eq!(CACHE_TTL, seven_days, "CACHE_TTL must be exactly 7 days");
    }

    #[test]
    fn configured_url_returns_default_when_env_unset() {
        let _lock = crate::test_util::env_lock();
        std::env::remove_var(PROVIDERS_URL_ENV);
        let url = configured_url();
        assert_eq!(
            url, DEFAULT_PROVIDERS_URL,
            "must return default when env var unset"
        );
    }

    #[test]
    fn configured_url_honors_env_override() {
        let _lock = crate::test_util::env_lock();
        let custom = "https://custom.example.com/providers.json";
        std::env::set_var(PROVIDERS_URL_ENV, custom);
        let url = configured_url();
        std::env::remove_var(PROVIDERS_URL_ENV);
        assert_eq!(url, custom);
    }

    #[test]
    fn validate_url_rejects_192_168_private() {
        assert!(validate_providers_url("http://192.168.1.1/x").is_err());
    }

    #[test]
    fn validate_url_rejects_172_16_private() {
        assert!(validate_providers_url("http://172.16.0.1/x").is_err());
    }

    #[test]
    fn validate_url_rejects_ipv6_loopback() {
        assert!(validate_providers_url("http://[::1]/x").is_err());
    }

    #[test]
    fn validate_url_accepts_http_public() {
        assert!(validate_providers_url("http://example.com/providers.json").is_ok());
    }

    #[test]
    fn is_non_routable_ip_loopback_true() {
        use std::net::IpAddr;
        let ip: IpAddr = "127.0.0.1".parse().unwrap();
        assert!(is_non_routable_ip(ip));
    }

    #[test]
    fn is_non_routable_ip_private_true() {
        use std::net::IpAddr;
        let ip: IpAddr = "10.0.0.1".parse().unwrap();
        assert!(is_non_routable_ip(ip));
        let ip2: IpAddr = "192.168.0.1".parse().unwrap();
        assert!(is_non_routable_ip(ip2));
    }

    #[test]
    fn is_non_routable_ip_public_false() {
        use std::net::IpAddr;
        let ip: IpAddr = "8.8.8.8".parse().unwrap();
        assert!(!is_non_routable_ip(ip));
    }

    #[test]
    fn is_non_routable_ip_ipv4_unspecified_true() {
        // kills `v4.is_unspecified()` removal mutation
        use std::net::IpAddr;
        let ip: IpAddr = "0.0.0.0".parse().unwrap();
        assert!(is_non_routable_ip(ip), "0.0.0.0 must be non-routable");
    }

    #[test]
    fn is_non_routable_ip_ipv4_link_local_true() {
        // kills `v4.is_link_local()` removal mutation (169.254.0.0/16)
        use std::net::IpAddr;
        let ip: IpAddr = "169.254.1.1".parse().unwrap();
        assert!(
            is_non_routable_ip(ip),
            "link-local 169.254.x.x must be non-routable"
        );
    }

    #[test]
    fn is_non_routable_ip_ipv6_multicast_true() {
        // kills `v6.is_multicast()` removal mutation
        use std::net::IpAddr;
        let ip: IpAddr = "ff02::1".parse().unwrap();
        assert!(
            is_non_routable_ip(ip),
            "IPv6 multicast must be non-routable"
        );
    }

    #[test]
    fn is_non_routable_ip_ipv6_ula_true() {
        // kills `(v6.segments()[0] & 0xfe00) == 0xfc00` removal mutation (fc00::/7 ULA)
        use std::net::IpAddr;
        let ip: IpAddr = "fd12:3456:789a::1".parse().unwrap();
        assert!(
            is_non_routable_ip(ip),
            "IPv6 ULA fd::/8 must be non-routable"
        );
    }

    #[test]
    fn is_non_routable_ip_ipv6_link_local_true() {
        // kills `(v6.segments()[0] & 0xffc0) == 0xfe80` removal mutation (fe80::/10)
        use std::net::IpAddr;
        let ip: IpAddr = "fe80::1".parse().unwrap();
        assert!(
            is_non_routable_ip(ip),
            "IPv6 link-local fe80::/10 must be non-routable"
        );
    }

    #[test]
    fn is_non_routable_ip_ipv6_public_false() {
        // kills function-level replacement of `is_non_routable_ip`
        use std::net::IpAddr;
        let ip: IpAddr = "2607:f8b0:4004:811::200e".parse().unwrap();
        assert!(
            !is_non_routable_ip(ip),
            "global unicast IPv6 must be routable"
        );
    }

    #[test]
    fn needs_update_false_when_fresh_cache() -> Result<(), Box<dyn std::error::Error>> {
        let tmp = tempfile::tempdir()?;
        let _pin = PinnedRecursiveHome::new(tmp.path());

        // Write a fresh cache file
        let path = cache_path();
        std::fs::create_dir_all(path.parent().unwrap())?;
        let cache = ProvidersCache {
            schema_version: 1,
            updated_at: "2026-01-01T00:00:00Z".to_string(),
            source_url: "https://example.com/providers.json".to_string(),
            providers: vec![],
        };
        std::fs::write(&path, serde_json::to_string_pretty(&cache)?)?;

        // File was just written — modification time is NOW → not stale
        assert!(
            !needs_update(),
            "freshly written cache must not need update"
        );
        Ok(())
    }
}