stubb 0.1.0

A Rust library for embedding coding agents (Codex, Claude Code, Pi, and ACP agents) in your application
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
573
574
575
576
577
578
579
580
581
582
use super::{Launch, config, connection, live};

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock, Weak};
use std::time::Duration;

use agent_client_protocol::v1::schema as wire;

use crate::catalog::{Catalog, ProviderInfo};
use crate::core::runtime::files::{now, timestamp_millis};
use crate::process::Environment;
use crate::provider::BoxFuture;
use crate::session::{self, Session};
use crate::{Error, Provider, Result, turn};
use connection::{Connection, Connector};
use live::AcpSession;

/// How many `session/list` pages [`AcpProvider::read_session`] reads to find
/// a session.
const MAX_LIST_PAGES: usize = 50;

/// Which agent an [`AcpProvider`] drives, and how.
#[derive(Debug, Clone)]
pub struct AcpConfig {
    /// Stubb's provider identifier, such as a registry id (`gemini`). It
    /// must not be one of Stubb's native providers (`claude`, `codex`, `pi`).
    pub id: String,
    /// The agent's name, for people.
    pub label: String,
    pub launch: Launch,
    /// The environment of each agent process: the host's whole environment
    /// by default. [`Launch::env`] is set over it.
    pub environment: Environment,
    /// Deadline for starting the agent, `initialize` and creating or loading
    /// a session. Agents started through `npx` or `uvx` may download
    /// themselves first.
    pub startup_timeout: Duration,
    /// Deadline for other requests, such as changing a setting.
    pub request_timeout: Duration,
    /// An `authMethods` id to `authenticate` with after `initialize`, for
    /// agents that require it. See [`Report::auth_methods`](super::Report::auth_methods).
    pub auth_method: Option<String>,
}

impl AcpConfig {
    pub fn new(id: impl Into<String>, label: impl Into<String>, launch: Launch) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            launch,
            environment: Environment::inherit(),
            startup_timeout: Duration::from_secs(120),
            request_timeout: Duration::from_secs(60),
            auth_method: None,
        }
    }

    /// A configuration for a [registry](super::registry) entry, started with
    /// `launch` (see [`Entry::resolve`](super::registry::Entry::resolve)).
    pub fn from_entry(entry: &super::registry::Entry, launch: Launch) -> Self {
        Self::new(&entry.id, &entry.name, launch)
    }
}

/// A provider for one ACP agent.
pub struct AcpProvider {
    config: AcpConfig,
    id: &'static str,
    connector: Connector,
    /// Live session connections, so `shutdown` can stop them.
    live: Mutex<HashMap<String, Weak<Connection>>>,
    /// Folders of sessions started or found here, to resume them.
    folders: Mutex<HashMap<String, PathBuf>>,
    /// What the agent reported in its last `initialize`.
    capabilities: Mutex<Option<wire::AgentCapabilities>>,
}

impl AcpProvider {
    pub fn new(config: AcpConfig) -> Self {
        let connector = Connector::Process {
            launch: config.launch.clone(),
            environment: config.environment.clone(),
        };
        Self::with_connector(config, connector)
    }

    pub(super) fn with_connector(config: AcpConfig, connector: Connector) -> Self {
        Self {
            id: intern(&config.id),
            config,
            connector,
            live: Mutex::new(HashMap::new()),
            folders: Mutex::new(HashMap::new()),
            capabilities: Mutex::new(None),
        }
    }

    pub fn config(&self) -> &AcpConfig {
        &self.config
    }

    /// The agent's name, for people.
    pub fn label(&self) -> &str {
        &self.config.label
    }

    pub(super) async fn connect(&self, cwd: &Path) -> Result<Connection> {
        let connection = Connection::open(
            &self.connector,
            cwd,
            self.config.startup_timeout,
            self.config.auth_method.as_deref(),
        )
        .await?;
        *self.capabilities.lock().expect("capabilities lock") =
            Some(connection.init.agent_capabilities.clone());
        Ok(connection)
    }

    /// Models and efforts the agent offers, read from a session it creates
    /// in the system's temporary folder, which is closed again at once.
    pub async fn catalog(&self) -> Result<Catalog> {
        let cwd = std::env::temp_dir();
        let connection = self.connect(&cwd).await?;
        let result = async {
            let response = connection
                .request(
                    "session/new",
                    wire::NewSessionRequest::new(cwd.clone()),
                    self.config.startup_timeout,
                )
                .await?;
            if connection
                .init
                .agent_capabilities
                .session_capabilities
                .close
                .is_some()
            {
                let close = wire::CloseSessionRequest::new(response.session_id.clone());
                let _ = connection
                    .request("session/close", close, self.config.request_timeout)
                    .await;
            }
            Ok::<_, Error>(response.config_options.unwrap_or_default())
        }
        .await;
        connection.shutdown().await;
        let options = result?;
        let agent = connection.init.agent_info.as_ref();
        Ok(Catalog {
            provider: ProviderInfo {
                id: self.id.to_owned(),
                version: agent.map(|agent| agent.version.clone()),
                user_agent: agent.map(|agent| format!("{}/{}", agent.name, agent.version)),
            },
            models: config::models(&options),
        })
    }

    /// Stored sessions, as the agent's `session/list` reports them, most
    /// recently updated first when the agent orders them so. `limit` is not
    /// passed on: the agent chooses its page size.
    pub async fn list_sessions(&self, query: session::Query) -> Result<session::Page> {
        if let Some(capabilities) = &*self.capabilities.lock().expect("capabilities lock")
            && capabilities.session_capabilities.list.is_none()
        {
            return Err(Error::Unsupported {
                operation: "list_sessions",
            });
        }
        let cwd = query.cwd.clone().unwrap_or_else(std::env::temp_dir);
        let connection = self.connect(&cwd).await?;
        let result = self.list_pages(&connection, query, 1).await;
        connection.shutdown().await;
        let (sessions, next_cursor) = result?;
        Ok(session::Page {
            sessions,
            next_cursor,
        })
    }

    async fn list_pages(
        &self,
        connection: &Connection,
        query: session::Query,
        pages: usize,
    ) -> Result<(Vec<session::Summary>, Option<String>)> {
        if connection
            .init
            .agent_capabilities
            .session_capabilities
            .list
            .is_none()
        {
            return Err(Error::Unsupported {
                operation: "list_sessions",
            });
        }
        let mut sessions = Vec::new();
        let mut cursor = query.cursor;
        for _ in 0..pages {
            let request = wire::ListSessionsRequest::new()
                .cwd(query.cwd.clone())
                .cursor(cursor.take());
            let page = connection
                .request("session/list", request, self.config.request_timeout)
                .await?;
            sessions.extend(page.sessions.iter().map(|info| self.summary(info)));
            cursor = page.next_cursor;
            if cursor.is_none() {
                break;
            }
        }
        let mut folders = self.folders.lock().expect("folders lock");
        for summary in &sessions {
            folders.insert(summary.id.clone(), summary.cwd.clone());
        }
        Ok((sessions, cursor))
    }

    fn summary(&self, info: &wire::SessionInfo) -> session::Summary {
        let updated = info
            .updated_at
            .as_deref()
            .and_then(timestamp_millis)
            .map_or(0, |millis| millis / 1000);
        session::Summary {
            id: info.session_id.0.to_string(),
            provider: self.id.to_owned(),
            title: info.title.clone(),
            preview: String::new(),
            cwd: info.cwd.clone(),
            model: None,
            effort: None,
            status: session::Status::NotLoaded,
            created_at: updated,
            updated_at: updated,
            source: None,
        }
    }

    /// A stored session, found through `session/list`.
    pub async fn read_session(&self, id: &str) -> Result<session::Summary> {
        let connection = self.connect(&std::env::temp_dir()).await?;
        let result = self
            .list_pages(&connection, session::Query::default(), MAX_LIST_PAGES)
            .await;
        connection.shutdown().await;
        result?
            .0
            .into_iter()
            .find(|summary| summary.id == id)
            .ok_or_else(|| Error::SessionNotFound { id: id.to_owned() })
    }

    /// Starts a new session in `spec.cwd`.
    pub async fn start_session(&self, spec: session::Spec) -> Result<Session> {
        self.open(None, &spec.cwd, spec.settings).await
    }

    /// Loads a stored session in its own folder, which Stubb knows for
    /// sessions it started or listed, and otherwise asks the agent through
    /// `session/list`. Use [`resume_session_in`](Self::resume_session_in)
    /// for agents that cannot list sessions.
    pub async fn resume_session(&self, id: &str, settings: session::Settings) -> Result<Session> {
        let known = self.folders.lock().expect("folders lock").get(id).cloned();
        let cwd = match known {
            Some(cwd) => cwd,
            None => match self.read_session(id).await {
                Ok(summary) => summary.cwd,
                Err(Error::Unsupported { .. }) => {
                    return Err(Error::Unsupported {
                        operation: "resume_session",
                    });
                }
                Err(error) => return Err(error),
            },
        };
        self.resume_session_in(id, &cwd, settings).await
    }

    /// Loads a stored session that belongs to `cwd`: with `session/resume`
    /// when the agent offers it, otherwise with `session/load`, which
    /// replays its history (Stubb passes the replay on as raw events, before
    /// the session is returned).
    pub async fn resume_session_in(
        &self,
        id: &str,
        cwd: &Path,
        settings: session::Settings,
    ) -> Result<Session> {
        self.open(Some(id), cwd, settings).await
    }

    /// Stops every live session's agent.
    pub async fn shutdown(&self) {
        let live: Vec<_> = self
            .live
            .lock()
            .expect("live lock")
            .drain()
            .filter_map(|(_, connection)| connection.upgrade())
            .collect();
        for connection in live {
            connection.state.withdraw_requests();
            connection.shutdown().await;
            connection.state.emit(session::Event::Closed {
                reason: session::CloseReason::Client,
            });
        }
    }

    async fn open(
        &self,
        resume: Option<&str>,
        cwd: &Path,
        settings: session::Settings,
    ) -> Result<Session> {
        if settings.permissions.is_some() {
            return Err(Error::Unsupported {
                operation: "permission_configuration",
            });
        }
        if settings.fast_mode == Some(true) {
            return Err(Error::InvalidSetting {
                setting: "fast_mode",
                value: "true".to_owned(),
                reason: "ACP agents have no fast mode".to_owned(),
            });
        }
        let connection = Arc::new(self.connect(cwd).await?);
        match self.attach(&connection, resume, cwd, &settings).await {
            Ok(session) => Ok(session),
            Err(error) => {
                connection.shutdown().await;
                Err(error)
            }
        }
    }

    async fn attach(
        &self,
        connection: &Arc<Connection>,
        resume: Option<&str>,
        cwd: &Path,
        settings: &session::Settings,
    ) -> Result<Session> {
        let capabilities = &connection.init.agent_capabilities;
        let directories = if capabilities
            .session_capabilities
            .additional_directories
            .is_some()
        {
            settings.additional_directories.clone()
        } else {
            Vec::new()
        };
        let timeout = self.config.startup_timeout;
        fn not_found(id: &str) -> impl FnOnce(Error) -> Error + '_ {
            move |error| match error {
                Error::Rpc { code: -32002, .. } => Error::SessionNotFound { id: id.to_owned() },
                error => error,
            }
        }
        let (session_id, modes, options) = match resume {
            None => {
                let request = wire::NewSessionRequest::new(cwd.to_path_buf())
                    .additional_directories(directories);
                let response = connection.request("session/new", request, timeout).await?;
                (response.session_id, response.modes, response.config_options)
            }
            Some(id) if capabilities.session_capabilities.resume.is_some() => {
                let request = wire::ResumeSessionRequest::new(id.to_owned(), cwd.to_path_buf())
                    .additional_directories(directories);
                let response = connection
                    .request("session/resume", request, timeout)
                    .await
                    .map_err(not_found(id))?;
                (
                    wire::SessionId::new(id.to_owned()),
                    response.modes,
                    response.config_options,
                )
            }
            Some(id) if capabilities.load_session => {
                let request = wire::LoadSessionRequest::new(id.to_owned(), cwd.to_path_buf())
                    .additional_directories(directories);
                let response = connection
                    .request("session/load", request, timeout)
                    .await
                    .map_err(not_found(id))?;
                (
                    wire::SessionId::new(id.to_owned()),
                    response.modes,
                    response.config_options,
                )
            }
            Some(_) => {
                return Err(Error::Unsupported {
                    operation: "resume_session",
                });
            }
        };
        let state = &connection.state;
        state.attach(
            &session_id.0,
            cwd,
            &settings.additional_directories,
            settings.forward_requests,
        );
        state.set_config(modes, options);
        self.apply(connection, &session_id, settings).await?;

        let options = state.config_options();
        let model = config::current(&options, &wire::SessionConfigOptionCategory::Model)
            .unwrap_or_else(|| config::DEFAULT_MODEL.to_owned());
        let effort = config::current(&options, &wire::SessionConfigOptionCategory::ThoughtLevel);
        let id = session_id.0.to_string();
        let summary = session::Summary {
            id: id.clone(),
            provider: self.id.to_owned(),
            title: None,
            preview: String::new(),
            cwd: cwd.to_path_buf(),
            model: Some(model.clone()),
            effort: effort.clone(),
            status: session::Status::Idle,
            created_at: now(),
            updated_at: now(),
            source: None,
        };
        let info = session::Info {
            model,
            effort,
            fast_mode: false,
            cwd: cwd.to_path_buf(),
            permissions: None,
            approval_policy: None,
            sandbox: None,
            restored: resume.is_some(),
            persisted: resume.is_some(),
            summary,
        };
        {
            let mut live = self.live.lock().expect("live lock");
            live.retain(|_, connection| connection.strong_count() > 0);
            live.insert(id.clone(), Arc::downgrade(connection));
        }
        self.folders
            .lock()
            .expect("folders lock")
            .insert(id, cwd.to_path_buf());
        let driver = AcpSession {
            connection: Arc::clone(connection),
            session_id,
            request_timeout: self.config.request_timeout,
        };
        Ok(Session::new(info, Arc::new(driver)))
    }

    /// Applies the requested model and effort through the session's
    /// configuration options.
    async fn apply(
        &self,
        connection: &Connection,
        session_id: &wire::SessionId,
        settings: &session::Settings,
    ) -> Result<()> {
        let wanted = [
            (
                "model",
                &settings.model,
                wire::SessionConfigOptionCategory::Model,
            ),
            (
                "effort",
                &settings.effort,
                wire::SessionConfigOptionCategory::ThoughtLevel,
            ),
        ];
        for (setting, value, category) in wanted {
            let Some(value) = value else { continue };
            let options = connection.state.config_options();
            let invalid = |reason: &str| Error::InvalidSetting {
                setting,
                value: value.clone(),
                reason: reason.to_owned(),
            };
            let Some((option, current, choices)) = config::select(&options, &category) else {
                if setting == "model" && value == config::DEFAULT_MODEL {
                    continue;
                }
                return Err(invalid("the agent offers no choice"));
            };
            if !choices.iter().any(|choice| choice.value == *value) {
                return Err(invalid("the agent does not offer this value"));
            }
            if current == *value {
                continue;
            }
            let request = wire::SetSessionConfigOptionRequest::new(
                session_id.clone(),
                option.id.clone(),
                wire::SessionConfigValueId::new(value.clone()),
            );
            let response = connection
                .request(
                    "session/set_config_option",
                    request,
                    self.config.request_timeout,
                )
                .await?;
            connection
                .state
                .set_config(None, Some(response.config_options));
        }
        Ok(())
    }
}

/// A `'static` copy of a provider id, made once per distinct id, so ACP
/// providers can report ids chosen at run time through
/// [`Provider::id`].
pub(super) fn intern(id: &str) -> &'static str {
    static IDS: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
    let mut ids = IDS.get_or_init(Default::default).lock().expect("ids lock");
    if let Some(id) = ids.get(id) {
        return id;
    }
    let id: &'static str = Box::leak(id.to_owned().into_boxed_str());
    ids.insert(id);
    id
}

impl Provider for AcpProvider {
    fn id(&self) -> &'static str {
        self.id
    }

    fn catalog(&self) -> BoxFuture<'_, Result<Catalog>> {
        Box::pin(AcpProvider::catalog(self))
    }

    fn list_sessions(&self, query: session::Query) -> BoxFuture<'_, Result<session::Page>> {
        Box::pin(AcpProvider::list_sessions(self, query))
    }

    fn read_session<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<session::Summary>> {
        Box::pin(AcpProvider::read_session(self, id))
    }

    /// ACP cannot read a stored session's turns without loading it.
    fn list_turns<'a>(
        &'a self,
        _id: &'a str,
        _query: turn::Query,
    ) -> BoxFuture<'a, Result<turn::Page>> {
        Box::pin(async {
            Err(Error::Unsupported {
                operation: "list_turns",
            })
        })
    }

    fn start_session(&self, spec: session::Spec) -> BoxFuture<'_, Result<Session>> {
        Box::pin(AcpProvider::start_session(self, spec))
    }

    fn resume_session<'a>(
        &'a self,
        id: &'a str,
        settings: session::Settings,
    ) -> BoxFuture<'a, Result<Session>> {
        Box::pin(AcpProvider::resume_session(self, id, settings))
    }

    fn shutdown(&self) -> BoxFuture<'_, ()> {
        Box::pin(AcpProvider::shutdown(self))
    }
}