infinity-daemon 0.2.0

The Infinity daemon: hosts durable agent sessions and serves local clients.
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
//! Registry of model providers and the models they offer.
//!
//! Providers are stored in a map keyed by their stable unique id (e.g.
//! `"bedrock"`). Models are identified globally by a [`ModelRef`]
//! (provider id + provider-scoped model id).
//!
//! Providers run as separate processes configured in
//! `~/.infinity/providers.json` (see [`ProvidersConfig`]): each entry maps a
//! provider id to the command that serves that provider over a Unix socket.
//! The daemon spawns each command, reads the socket path the process prints
//! on stdout, and talks to it through a
//! [`RemoteModelProvider`](infinity_provider_protocol::remote::RemoteModelProvider).

use infinity_agent_core::ThreadId;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use infinity_protocol::ModelRef;
use infinity_provider_protocol::remote::RemoteModelProvider;
use infinity_provider_protocol::{ModelEntry, ModelProvider};
use serde::{Deserialize, Serialize};
use tokio::io::AsyncBufReadExt;

type BoxError = Box<dyn std::error::Error + Send + Sync>;

/// How long to wait for a spawned provider process to print its socket path.
const PROVIDER_STARTUP_TIMEOUT: Duration = Duration::from_secs(30);

/// Configuration for a single model provider process in `providers.json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// Command (argv) that serves this provider over a Unix socket and
    /// prints the socket path on stdout.
    pub command: Vec<String>,
    /// Installation source info (mirroring rap.json toolset commands) so the
    /// provider can be re-installed by `infinity provider update`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub crate_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub git: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
}

/// The providers configured in `~/.infinity/providers.json`: a JSON object
/// mapping provider id to a [`ProviderConfig`], e.g.:
///
/// ```json
/// {
///   "bedrock": { "command": ["infinity-provider-bedrock"] },
///   "custom": { "command": ["my-provider", "--flag"], "crate_name": "my-provider" }
/// }
/// ```
///
/// Stored as a `Vec` of `(id, config)` pairs because the JSON object's
/// insertion order is significant — providers are registered in config
/// order, and the first model of the first provider is the global default.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ProvidersConfig {
    pub providers: Vec<(String, ProviderConfig)>,
}

impl ProvidersConfig {
    pub fn get(&self, id: &str) -> Option<&ProviderConfig> {
        self.providers
            .iter()
            .find(|(pid, _)| pid == id)
            .map(|(_, config)| config)
    }

    /// Insert or replace the config for `id`, preserving its position when
    /// it already exists.
    pub fn upsert(&mut self, id: String, config: ProviderConfig) {
        if let Some((_, existing)) = self.providers.iter_mut().find(|(pid, _)| *pid == id) {
            *existing = config;
        } else {
            self.providers.push((id, config));
        }
    }
}

// Serialize as a JSON object (entries in `providers` order).
impl Serialize for ProvidersConfig {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        let mut map = serializer.serialize_map(Some(self.providers.len()))?;
        for (id, config) in &self.providers {
            map.serialize_entry(id, config)?;
        }
        map.end()
    }
}

// Deserialize from a JSON object, preserving the document's entry order
// (serde visits map entries in the order they appear in the input).
impl<'de> Deserialize<'de> for ProvidersConfig {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct OrderedMapVisitor;

        impl<'de> serde::de::Visitor<'de> for OrderedMapVisitor {
            type Value = ProvidersConfig;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                f.write_str("a map from provider id to provider config")
            }

            fn visit_map<A: serde::de::MapAccess<'de>>(
                self,
                mut access: A,
            ) -> Result<Self::Value, A::Error> {
                let mut providers = Vec::new();
                while let Some((id, config)) = access.next_entry::<String, ProviderConfig>()? {
                    providers.push((id, config));
                }
                Ok(ProvidersConfig { providers })
            }
        }

        deserializer.deserialize_map(OrderedMapVisitor)
    }
}

/// Load the providers config from `path`. There is no implicit default: at
/// least one provider must be configured (e.g. via
/// `infinity provider install`) for the daemon to start.
pub fn load_providers_config(path: &Path) -> Result<ProvidersConfig, BoxError> {
    if !path.exists() {
        return Err(format!(
            "no model providers configured ({} not found); \
             run `infinity provider install <id> --crate <name>` to install one \
             (e.g. `infinity provider install bedrock --crate infinity-provider-bedrock`)",
            path.display()
        )
        .into());
    }
    let json = std::fs::read_to_string(path)
        .map_err(|e| format!("failed to read {}: {e}", path.display()))?;
    let config: ProvidersConfig = serde_json::from_str(&json)
        .map_err(|e| format!("failed to parse {}: {e}", path.display()))?;
    if config.providers.is_empty() {
        return Err(format!("no providers configured in {}", path.display()).into());
    }
    let mut seen = std::collections::HashSet::new();
    for (provider_id, provider) in &config.providers {
        if provider_id.is_empty() {
            return Err(format!("empty provider id in {}", path.display()).into());
        }
        if !seen.insert(provider_id) {
            return Err(
                format!("duplicate provider id {provider_id} in {}", path.display()).into(),
            );
        }
        if provider.command.is_empty() {
            return Err(format!(
                "provider {provider_id} in {} has an empty command",
                path.display()
            )
            .into());
        }
    }
    Ok(config)
}

/// Forward a child output stream to the daemon's tracing log, line by line.
/// Also keeps the pipe drained so the child never blocks on a full pipe.
fn forward_output_to_log(
    provider_id: &str,
    stream_name: &'static str,
    stream: impl tokio::io::AsyncRead + Unpin + Send + 'static,
) {
    let id = provider_id.to_owned();
    tokio::spawn(async move {
        let mut lines = tokio::io::BufReader::new(stream).lines();
        loop {
            match lines.next_line().await {
                Ok(Some(line)) => tracing::info!("provider {id} {stream_name}: {line}"),
                Ok(None) => break,
                Err(e) => {
                    tracing::warn!("failed reading {stream_name} of provider {id}: {e}");
                    break;
                }
            }
        }
    });
}

/// Spawn a provider process and wait for it to print its socket path on
/// stdout. The returned [`Child`](tokio::process::Child) kills the process
/// when dropped; callers must keep it alive for as long as the provider is
/// in use.
///
/// The child gets a null stdin and piped stdout/stderr — never the daemon's
/// own handles, which can be closed pipes when the daemon was auto-launched
/// in the background. Everything the provider prints (beyond the initial
/// socket path line) is forwarded to the daemon's log.
pub async fn spawn_provider(
    provider_id: &str,
    command: &[String],
) -> Result<(RemoteModelProvider, tokio::process::Child), BoxError> {
    let mut child = tokio::process::Command::new(&command[0])
        .args(&command[1..])
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .map_err(|e| {
            format!(
                "failed to spawn provider {provider_id} ({}): {e}",
                command[0]
            )
        })?;

    // Forward stderr to the log right away so startup logs are captured and
    // a chatty provider can't fill the pipe while we wait for the socket
    // path below.
    let stderr = child
        .stderr
        .take()
        .expect("bug: piped child stderr missing");
    forward_output_to_log(provider_id, "stderr", stderr);

    let stdout = child
        .stdout
        .take()
        .expect("bug: piped child stdout missing");
    let mut lines = tokio::io::BufReader::new(stdout).lines();
    let socket_path = tokio::time::timeout(PROVIDER_STARTUP_TIMEOUT, lines.next_line())
        .await
        .map_err(|_| format!("provider {provider_id} did not print a socket path in time"))?
        .map_err(|e| format!("failed reading socket path from provider {provider_id}: {e}"))?
        .ok_or_else(|| format!("provider {provider_id} exited without printing a socket path"))?;

    // Forward any further stdout output to the log as well.
    forward_output_to_log(provider_id, "stdout", lines.into_inner());

    Ok((RemoteModelProvider::new(socket_path.trim()), child))
}

/// Spawn every provider in the config. Returns the `(provider_id, provider)`
/// pairs for [`ModelCatalog::new`] plus the child process handles, which
/// must be kept alive for as long as the providers are in use.
pub async fn spawn_configured_providers(
    config: &ProvidersConfig,
) -> Result<
    (
        Vec<(String, Arc<dyn ModelProvider>)>,
        Vec<tokio::process::Child>,
    ),
    BoxError,
> {
    let mut providers: Vec<(String, Arc<dyn ModelProvider>)> = Vec::new();
    let mut children = Vec::new();
    for (provider_id, provider_config) in &config.providers {
        let (provider, child) = spawn_provider(provider_id, &provider_config.command).await?;
        tracing::info!(
            "provider {provider_id} serving on {}",
            provider.socket_path().display()
        );
        providers.push((provider_id.clone(), Arc::new(provider) as _));
        children.push(child);
    }
    Ok((providers, children))
}

/// A model in the catalog, tagged with the provider that offers it.
#[derive(Clone)]
pub struct CatalogModel {
    pub provider_id: String,
    pub entry: ModelEntry,
}

/// All registered providers and their available models.
pub struct ModelCatalog {
    /// Providers keyed by their stable unique id.
    providers: HashMap<String, Arc<dyn ModelProvider>>,
    /// Flattened list of all available models, in registration order.
    models: Vec<CatalogModel>,
    /// The global default model, used for any thread without a selected model.
    default: ModelRef,
}

impl ModelCatalog {
    /// Build a catalog from `(provider_id, provider)` pairs (in registration
    /// order). Provider ids must be stable, unique, and non-empty. The first
    /// model of the first provider becomes the global default.
    pub async fn new(providers: Vec<(String, Arc<dyn ModelProvider>)>) -> Result<Self, BoxError> {
        let mut provider_map: HashMap<String, Arc<dyn ModelProvider>> = HashMap::new();
        let mut models = Vec::new();

        for (provider_id, provider) in providers {
            // The empty string is reserved: the conversation store uses an
            // empty provider id as the serde sentinel for thread metadata
            // that predates per-thread model tracking.
            if provider_id.is_empty() {
                return Err("model provider id must not be empty".into());
            }
            if provider_map.contains_key(&provider_id) {
                return Err(format!("duplicate model provider id: {provider_id}").into());
            }
            for entry in provider
                .list_models()
                .await
                .map_err(|e| format!("failed to list models from provider {provider_id}: {e}"))?
            {
                models.push(CatalogModel {
                    provider_id: provider_id.clone(),
                    entry,
                });
            }
            provider_map.insert(provider_id, provider);
        }

        let default = models
            .first()
            .map(|m| ModelRef {
                provider_id: m.provider_id.clone(),
                model_id: m.entry.model_id.clone(),
            })
            .ok_or("no models available from any provider")?;

        Ok(Self {
            providers: provider_map,
            models,
            default,
        })
    }

    /// All available models, in registration order.
    pub fn models(&self) -> &[CatalogModel] {
        &self.models
    }

    /// The global default model.
    pub fn default_ref(&self) -> &ModelRef {
        &self.default
    }

    /// The entry for the global default model.
    pub fn default_entry(&self) -> &ModelEntry {
        self.find(&self.default)
            .expect("bug: default model missing from catalog")
    }

    /// Look up a model's entry by reference.
    pub fn find(&self, model: &ModelRef) -> Option<&ModelEntry> {
        self.models
            .iter()
            .find(|m| m.provider_id == model.provider_id && m.entry.model_id == model.model_id)
            .map(|m| &m.entry)
    }

    /// Look up a provider by its id.
    pub fn provider(&self, provider_id: &str) -> Option<&Arc<dyn ModelProvider>> {
        self.providers.get(provider_id)
    }

    /// Resolve a selected model to a concrete `(ModelRef, entry)`, falling
    /// back to the global default when the selection is no longer available.
    /// Returns `(model, entry, fell_back)` where `fell_back` is true when a
    /// stale selection was replaced by the default.
    pub fn resolve(&self, selected: &ModelRef) -> (ModelRef, &ModelEntry, bool) {
        if let Some(entry) = self.find(selected) {
            return (selected.clone(), entry, false);
        }
        let entry = self
            .find(&self.default)
            .expect("bug: default model missing from catalog");
        (self.default.clone(), entry, true)
    }
}

/// [`ModelSource`] over the daemon's catalog: each completion round resolves
/// the model persisted for its thread in the conversation store, falling back
/// to the global default when the selection is no longer available. Model
/// switching is just persisting a new selection — the next round picks it up,
/// and an in-flight completion finishes on the model it started with.
pub struct CatalogModelSource {
    pub catalog: Arc<ModelCatalog>,
    pub conversation_store: crate::memory_store::PersistentConversationStore,
}

#[async_trait::async_trait(?Send)]
impl infinity_agent_core::system::ModelSource for CatalogModelSource {
    async fn resolve(
        &self,
        thread_id: &ThreadId<str>,
    ) -> Result<infinity_agent_core::system::ResolvedModel, BoxError> {
        let selected = self.conversation_store.get_thread_model(thread_id);
        let (model_ref, entry, fell_back) = self.catalog.resolve(&selected);
        if fell_back {
            tracing::warn!(
                "model {}/{} is not available for thread {}; using default {}/{}",
                selected.provider_id,
                selected.model_id,
                thread_id,
                model_ref.provider_id,
                model_ref.model_id
            );
        }
        let provider = self
            .catalog
            .provider(&model_ref.provider_id)
            .expect("bug: cataloged model's provider missing from catalog")
            .clone();
        Ok(infinity_agent_core::system::ResolvedModel {
            provider,
            model_id: model_ref.model_id,
            context_window: entry.context_window,
            supports_image_input: entry.supports_image_input,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn missing_providers_config_is_an_error() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let Err(err) = load_providers_config(&dir.path().join("providers.json")) else {
            panic!("missing config should be an error");
        };
        assert!(
            err.to_string().contains("infinity provider install"),
            "error should point at `infinity provider install`: {err}"
        );
    }

    #[test]
    fn providers_config_parses_entries_and_preserves_order() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("providers.json");
        // Note: ids deliberately not in alphabetical order — the document
        // order must be preserved (it determines the default provider).
        std::fs::write(
            &path,
            r#"{
                "zeta": { "command": ["zeta-provider"] },
                "bedrock": {
                    "command": ["infinity-provider-bedrock"],
                    "crate_name": "infinity-provider-bedrock"
                },
                "custom": {
                    "command": ["/usr/local/bin/my-provider", "--flag", "value"],
                    "crate_name": "my-provider",
                    "git": "https://example.com/my-provider.git"
                }
            }"#,
        )
        .expect("write config");
        let config = load_providers_config(&path).expect("load config");
        let ids: Vec<&str> = config.providers.iter().map(|(id, _)| id.as_str()).collect();
        assert_eq!(ids, ["zeta", "bedrock", "custom"]);
        let custom = config.get("custom").expect("custom provider");
        assert_eq!(
            custom.command,
            vec![
                "/usr/local/bin/my-provider".to_owned(),
                "--flag".to_owned(),
                "value".to_owned()
            ]
        );
        assert_eq!(custom.crate_name.as_deref(), Some("my-provider"));
        assert_eq!(
            custom.git.as_deref(),
            Some("https://example.com/my-provider.git")
        );
        assert_eq!(custom.path, None);
    }

    #[test]
    fn providers_config_round_trips_in_order() {
        let mut config = ProvidersConfig::default();
        config.upsert(
            "zeta".to_owned(),
            ProviderConfig {
                command: vec!["zeta-provider".to_owned()],
                crate_name: None,
                git: None,
                path: None,
            },
        );
        config.upsert(
            "alpha".to_owned(),
            ProviderConfig {
                command: vec!["alpha-provider".to_owned()],
                crate_name: Some("alpha-provider".to_owned()),
                git: None,
                path: Some("/src/alpha".to_owned()),
            },
        );
        let json = serde_json::to_string_pretty(&config).expect("serialize");
        // "zeta" must come before "alpha" in the serialized object.
        assert!(json.find("zeta").expect("zeta") < json.find("alpha").expect("alpha"));
        let parsed: ProvidersConfig = serde_json::from_str(&json).expect("parse");
        assert_eq!(parsed, config);
    }

    #[test]
    fn providers_config_upsert_replaces_in_place() {
        let mut config = ProvidersConfig::default();
        let entry = |cmd: &str| ProviderConfig {
            command: vec![cmd.to_owned()],
            crate_name: None,
            git: None,
            path: None,
        };
        config.upsert("first".to_owned(), entry("first-v1"));
        config.upsert("second".to_owned(), entry("second-v1"));
        config.upsert("first".to_owned(), entry("first-v2"));
        let ids: Vec<&str> = config.providers.iter().map(|(id, _)| id.as_str()).collect();
        assert_eq!(ids, ["first", "second"]);
        assert_eq!(
            config.get("first").map(|p| p.command.clone()),
            Some(vec!["first-v2".to_owned()])
        );
    }

    #[test]
    fn providers_config_rejects_invalid_entries() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("providers.json");

        std::fs::write(&path, "{}").expect("write config");
        assert!(load_providers_config(&path).is_err(), "empty config");

        std::fs::write(&path, r#"{"bedrock": {"command": []}}"#).expect("write config");
        assert!(load_providers_config(&path).is_err(), "empty command");

        std::fs::write(
            &path,
            r#"{"a": {"command": ["x"]}, "a": {"command": ["y"]}}"#,
        )
        .expect("write config");
        assert!(load_providers_config(&path).is_err(), "duplicate ids");

        std::fs::write(&path, "not json").expect("write config");
        assert!(load_providers_config(&path).is_err(), "invalid json");
    }
}