anyagent 0.0.3

One Rust interface to the coding agents installed on a machine.
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! Entry point: find agents, open sessions.
//!
//! High level: `Runtime::new` registers one adapter per catalog agent;
//! `discover` scans offline, `probe`/`probe_auth` open a throwaway session,
//! `open` connects and starts the engine, `generate` runs one prompt to text,
//! `plan_usage` reads account quota.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use futures::StreamExt;
use serde::{Deserialize, Serialize};

use crate::adapter::{Adapter, ConnectRequest};
use crate::agent::{
    AgentDetails, AgentId, AgentInstallation, AuthStatus, Capabilities, Capability, Input,
    InstallationSource, PermissionMode, SessionOptions, SessionStart,
};
use crate::error::AgentError;
use crate::event::{
    Answer, Diagnostic, EventKind, PermissionChoice, PlanUsage, QuestionAnswer, Request, StopReason,
};
use crate::session::{self, Events, Session};

const USAGE_CACHE_TTL: Duration = Duration::from_secs(60);
/// How long `probe` waits for a command list that arrives after the
/// handshake (ACP `availableCommands`, codex skills).
const PROBE_COMMANDS_WAIT: Duration = Duration::from_secs(2);

/// The one object an application creates. Holds the adapter registry.
pub struct Runtime {
    adapters: HashMap<AgentId, Arc<dyn Adapter>>,
    /// Installations known without discovery (tests and pinned agents).
    pinned: Vec<AgentInstallation>,
    profiles: &'static [crate::catalog::AgentProfile],
    /// `plan_usage` results per installation, kept for `USAGE_CACHE_TTL`.
    usage_cache: Mutex<HashMap<(AgentId, PathBuf), (Instant, PlanUsage)>>,
}

impl Default for Runtime {
    fn default() -> Self {
        Self::new()
    }
}

impl Runtime {
    /// Registers one adapter per catalog agent whose adapter exists.
    pub fn new() -> Self {
        use crate::catalog::{Connection, NativeKind};
        let mut adapters: HashMap<AgentId, Arc<dyn Adapter>> = HashMap::new();
        for profile in crate::catalog::PROFILES {
            let adapter: Arc<dyn Adapter> = match &profile.connection {
                Connection::Acp { .. } => {
                    Arc::new(crate::adapter::acp::AcpAdapter::for_profile(profile))
                }
                Connection::Native(NativeKind::Claude) => {
                    Arc::new(crate::adapter::claude::ClaudeAdapter::new())
                }
                Connection::Native(NativeKind::Codex) => {
                    Arc::new(crate::adapter::codex::CodexAdapter::new())
                }
                Connection::Native(NativeKind::Pi) => {
                    Arc::new(crate::adapter::pi::PiAdapter::new())
                }
                Connection::Native(NativeKind::Opencode) => {
                    Arc::new(crate::adapter::opencode::OpencodeAdapter::new())
                }
                Connection::Native(NativeKind::Antigravity) => Arc::new(
                    crate::adapter::antigravity::AntigravityAdapter::new(profile),
                ),
            };
            adapters.insert(AgentId::new(profile.id), adapter);
        }
        Self {
            adapters,
            pinned: Vec::new(),
            profiles: crate::catalog::PROFILES,
            usage_cache: Mutex::new(HashMap::new()),
        }
    }

    /// A runtime whose only agent plays `script`, registered as `mock`: the
    /// real engine underneath, no subprocess. For testing an app's UI and
    /// routing against a scripted agent.
    #[cfg(any(test, feature = "mock"))]
    pub fn with_mock(script: crate::mock::Script) -> Self {
        Self::with_test_adapter(crate::mock::MockAdapter::new(script))
    }

    /// A runtime whose only agent is the given adapter, registered as `mock`.
    /// The real catalog is not scanned.
    #[cfg(any(test, feature = "mock"))]
    pub(crate) fn with_test_adapter(adapter: impl Adapter + 'static) -> Self {
        let id = AgentId::new("mock");
        let mut runtime = Self::new();
        runtime.profiles = &[];
        runtime.adapters.insert(id.clone(), Arc::new(adapter));
        runtime.pinned.push(AgentInstallation {
            name: "Mock".into(),
            id,
            executable_path: PathBuf::from("mock"),
            source: InstallationSource::Pinned,
            upgrade: None,
            acp_args: None,
        });
        runtime
    }

    /// Read-only inventory: which agents exist and where. Never launches an
    /// agent; login state comes from `probe` or `probe_auth`. An agent
    /// whose adapter is not implemented never appears as usable.
    pub async fn discover(&self) -> DiscoveryReport {
        let mut report = crate::discovery::discover(self.profiles).await;
        report.agents.retain(|a| self.adapters.contains_key(&a.id));
        report.missing.retain(|m| self.adapters.contains_key(&m.id));
        report.agents.splice(0..0, self.pinned.iter().cloned());
        report
    }

    /// Captures the login-shell PATH in the background so the first
    /// `discover` is instant. Optional; apps call it at boot.
    pub fn prewarm(&self) {
        tokio::spawn(crate::process::login_shell_path());
    }

    /// Launches the agent, completes the handshake, and returns the command
    /// handle plus the event stream.
    pub async fn open(
        &self,
        agent: &AgentInstallation,
        options: SessionOptions,
    ) -> Result<(Session, Events), AgentError> {
        // ACP args drive ACP even for a catalog agent with a native adapter:
        // an explicit `AgentInstallation::acp`, or discovery having found the
        // agent's ACP upgrade. Only the discovered one keeps the catalog's
        // auth facts; a pinned ad-hoc install named like a catalog agent is
        // still ad-hoc.
        let profile = (agent.source != InstallationSource::Pinned)
            .then(|| self.profiles.iter().find(|p| p.id == agent.id.as_str()))
            .flatten();
        let adapter: Arc<dyn Adapter> = match (self.adapters.get(&agent.id), &agent.acp_args) {
            (_, Some(args)) => Arc::new(crate::adapter::acp::AcpAdapter::with_args(
                profile,
                args.clone(),
            )),
            (Some(adapter), None) => Arc::clone(adapter),
            (None, None) => {
                return Err(AgentError::ProtocolFailed(format!(
                    "no adapter implemented for {} yet",
                    agent.id
                )));
            }
        };
        let connection = adapter
            .connect(ConnectRequest {
                installation: agent.clone(),
                options: options.clone(),
            })
            .await?;
        Ok(session::start(agent.clone(), connection, &options))
    }
    /// One-shot generation: prompt in, the agent's reply text out. Opens a
    /// throwaway session with tools disabled where the wire allows (claude,
    /// pi) and every permission declined elsewhere, gathers the text until
    /// the turn ends, and closes. Requires a new session; a tool event or a
    /// question requiring a choice cancels generation. Include context
    /// inline: path attachments cannot be opened without tools. Like the
    /// probes, the session is never persisted (claude, codex, pi) or is
    /// deleted at close (opencode), so it stays out of the user's history.
    pub async fn generate(
        &self,
        agent: &AgentInstallation,
        options: SessionOptions,
        prompt: impl Into<Input>,
    ) -> Result<String, AgentError> {
        if !matches!(options.start, SessionStart::New) {
            return Err(AgentError::InvalidConfiguration(
                "generate requires a new session".into(),
            ));
        }
        // Hands-off regardless of the caller's mode: AutoApprove would let
        // the agent run tools before any request reached this loop.
        let mut options = options.permission_mode(PermissionMode::Ask);
        options.no_tools = true;
        options.throwaway = true;
        let (session, mut events) = self.open(agent, options).await?;
        // Hands-off needs one of: tools switched off at launch, or every
        // tool gated by a permission request this loop can decline.
        let hands_off = session.tools_disabled()
            || session
                .info()
                .details
                .capabilities
                .supports(Capability::Permissions);
        if !hands_off {
            session.close().await.ok();
            return Err(AgentError::UnsupportedFeature(
                "generate requires tool permissions or launch-time tool disabling".into(),
            ));
        }
        let reply = collect_reply(&session, &mut events, prompt.into()).await;
        session.close().await.ok();
        reply
    }

    /// Opens a throwaway session in the temp dir, reads the details the
    /// handshake learned, and closes. A logged-out agent is a result, not
    /// an error.
    pub async fn probe(&self, agent: &AgentInstallation) -> Result<AgentDetails, AgentError> {
        let opened = self.open(agent, throwaway_options()).await;
        // Not logged is reported as a detail.
        let (session, mut events) = match opened {
            Err(AgentError::AuthRequired { login }) => {
                return Ok(AgentDetails {
                    version: None,
                    auth: AuthStatus::Unauthenticated { login },
                    capabilities: Capabilities::default(),
                    config_options: Vec::new(),
                    commands: Vec::new(),
                });
            }
            other => other?,
        };
        // ACP agents deliver `availableCommands` just after `session/new`
        // and codex fetches skills after open; wait briefly for the list.
        // Agents that report commands at handshake (claude) or have none to
        // report skip the wait.
        let deadline = tokio::time::Instant::now() + PROBE_COMMANDS_WAIT;
        let has_commands = session
            .info()
            .details
            .capabilities
            .supports(Capability::SlashCommands);
        while has_commands && session.info().details.commands.is_empty() {
            let Ok(Some(Ok(_))) = tokio::time::timeout_at(deadline, events.next()).await else {
                break;
            };
        }
        let details = session.info().details;
        session.close().await.ok();
        Ok(details)
    }

    /// Fast auth-only probe: does not wait for `availableCommands` (saves
    /// `PROBE_COMMANDS_WAIT`). Use when only `auth` is needed (e.g. kiro).
    pub async fn probe_auth(&self, agent: &AgentInstallation) -> Result<AuthStatus, AgentError> {
        let opened = self.open(agent, throwaway_options()).await;
        match opened {
            Err(AgentError::AuthRequired { login }) => Ok(AuthStatus::Unauthenticated { login }),
            Err(e) => Err(e),
            Ok((session, _events)) => {
                let auth = session.info().details.auth.clone();
                // _events dropped here; close shuts down the child.
                session.close().await.ok();
                Ok(auth)
            }
        }
    }

    /// Plan quota for the logged-in account. Agents without quota (or with
    /// an API-key login) return `UnsupportedFeature`. May spawn a short-lived
    /// agent process; results are cached for 60 s.
    pub async fn plan_usage(&self, agent: &AgentInstallation) -> Result<PlanUsage, AgentError> {
        let key = (agent.id.clone(), agent.executable_path.clone());
        if let Some((at, usage)) = self.usage_cache.lock().unwrap().get(&key)
            && at.elapsed() < USAGE_CACHE_TTL
        {
            return Ok(usage.clone());
        }
        let adapter = self
            .adapters
            .get(&agent.id)
            .ok_or_else(|| AgentError::UnsupportedFeature("plan usage".into()))?;
        let usage = adapter.plan_usage(agent).await?;
        self.usage_cache
            .lock()
            .unwrap()
            .insert(key, (Instant::now(), usage.clone()));
        Ok(usage)
    }

    /// One call for a usage page: every discovered agent with its quota or
    /// the typed reason it has none. Per-agent probes run concurrently.
    pub async fn plan_usage_all(&self) -> Vec<AgentPlanUsage> {
        let report = self.discover().await;
        let probes = report.agents.into_iter().map(|agent| async move {
            let usage = self.plan_usage(&agent).await;
            AgentPlanUsage { agent, usage }
        });
        futures::future::join_all(probes).await
    }
}

/// Options for a probe: temp dir, never persisted.
fn throwaway_options() -> SessionOptions {
    let mut options = SessionOptions::in_dir(std::env::temp_dir());
    options.throwaway = true;
    options
}

/// Sends the prompt and gathers the agent's own text (not subagents') until
/// the turn ends. Requests are declined so the agent stays hands-off.
async fn collect_reply(
    session: &Session,
    events: &mut Events,
    prompt: Input,
) -> Result<String, AgentError> {
    session.prompt(prompt).await?;
    let mut text = String::new();
    while let Some(event) = events.next().await {
        let event = event?;
        let nested = event
            .turn_info
            .as_ref()
            .is_some_and(|t| t.parent_tool_id.is_some());
        match event.kind {
            EventKind::TextDelta { text: delta, .. } if !nested => text.push_str(&delta),
            // Stop even on a proposed tool call; generation is text-only.
            EventKind::ToolUpdated(_) => {
                session.cancel(true).await?;
                return Err(AgentError::ProtocolFailed(
                    "generate: the agent attempted to use a tool".into(),
                ));
            }
            EventKind::RequestOpened(request) => match decline(&request) {
                Some(answer) => session.answer(request.id(), answer).await?,
                None => {
                    session.cancel(true).await?;
                    return Err(AgentError::ProtocolFailed(
                        "generate: the request cannot be declined without making a choice".into(),
                    ));
                }
            },
            EventKind::TurnEnded {
                stop: StopReason::Completed { .. },
                ..
            } => return Ok(text),
            EventKind::TurnEnded { stop, .. } => {
                return Err(AgentError::ProtocolFailed(format!(
                    "generate: turn ended with {stop:?}"
                )));
            }
            _ => {}
        }
    }
    Err(AgentError::SessionClosed)
}

/// Deny permissions and leave free-text answers blank. A request requiring
/// a choice cannot be declined safely, so the caller cancels generation.
fn decline(request: &Request) -> Option<Answer> {
    match request {
        Request::Permission(p) => [PermissionChoice::DenyOnce, PermissionChoice::DenyAlways]
            .into_iter()
            .find(|choice| p.options.contains(choice))
            .map(Answer::Permission),
        Request::Question(q) if q.questions.iter().all(|q| q.allows_free_text) => {
            Some(Answer::Question(
                q.questions
                    .iter()
                    .map(|_| QuestionAnswer::Text(String::new()))
                    .collect(),
            ))
        }
        Request::Question(_) => None,
    }
}

/// One row of a usage page: the agent and its quota, or why it has none.
#[derive(Debug)]
pub struct AgentPlanUsage {
    pub agent: AgentInstallation,
    pub usage: Result<PlanUsage, AgentError>,
}

/// What `discover` found and what it could not read.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct DiscoveryReport {
    pub agents: Vec<AgentInstallation>,
    /// Known agents that were not found, with where we looked and how to
    /// install them.
    pub missing: Vec<MissingAgent>,
    pub diagnostics: Vec<Diagnostic>,
}

impl DiscoveryReport {
    /// The installed agent with this id, or `NotInstalled`.
    pub fn require(&self, id: impl AsRef<str>) -> Result<&AgentInstallation, AgentError> {
        let id = id.as_ref();
        self.agents
            .iter()
            .find(|a| a.id.as_str() == id)
            .ok_or_else(|| AgentError::NotInstalled(AgentId::new(id)))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct MissingAgent {
    pub id: AgentId,
    pub name: String,
    pub searched: Vec<PathBuf>,
    pub install_hint: String,
}

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

    /// `generate` returns the turn's text and declines the permission
    /// request on the way, leaving nothing open.
    #[tokio::test]
    async fn generate_collects_text_and_declines_requests() {
        use crate::adapter::mock::MockAdapter;
        let runtime = Runtime::with_test_adapter(MockAdapter::permission_flow());
        let agent = runtime.discover().await.require("mock").unwrap().clone();
        let dir = tempfile::tempdir().unwrap();
        let text = runtime
            .generate(&agent, SessionOptions::in_dir(dir.path()), "title this")
            .await
            .unwrap();
        assert_eq!(text, "Let me check. Done.");
    }

    /// `generate` uses the offered deny and blank free text, but never picks
    /// a choice or accepts a permission without a deny.
    #[tokio::test]
    async fn generate_declines_within_what_the_request_offers() {
        use crate::adapter::mock::{MockAdapter, Script, Step, completed, text, tool};
        use crate::event::{
            Choice, ChoiceId, PermissionRequest, Question, QuestionId, QuestionRequest, RequestId,
            ToolStatus,
        };
        let EventKind::ToolUpdated(pending) = tool("tool-1", ToolStatus::Pending) else {
            unreachable!()
        };
        let permission = |options: Vec<PermissionChoice>| {
            EventKind::RequestOpened(Request::Permission(PermissionRequest {
                id: RequestId::new("r1"),
                tool: pending.clone(),
                options,
                detail: None,
            }))
        };
        let question = |allows_free_text| {
            EventKind::RequestOpened(Request::Question(QuestionRequest {
                id: RequestId::new("q1"),
                questions: vec![Question {
                    id: QuestionId::new("q"),
                    text: "Which?".into(),
                    header: None,
                    choices: vec![Choice {
                        id: ChoiceId::new("a"),
                        label: "A".into(),
                        description: None,
                    }],
                    multi_select: false,
                    allows_free_text,
                }],
            }))
        };
        let script = Script::default().turn(vec![
            Step::Emit(text("m1", "one ")),
            Step::Emit(permission(vec![
                PermissionChoice::AllowOnce,
                PermissionChoice::DenyAlways,
            ])),
            Step::AwaitAnswer,
            Step::Emit(question(true)),
            Step::AwaitAnswer,
            Step::Emit(text("m1", "two")),
            Step::End(completed()),
        ]);
        let runtime = Runtime::with_test_adapter(MockAdapter::new(script));
        let agent = runtime.discover().await.require("mock").unwrap().clone();
        let dir = tempfile::tempdir().unwrap();
        let text = runtime
            .generate(&agent, SessionOptions::in_dir(dir.path()), "go")
            .await
            .unwrap();
        assert_eq!(text, "one two");

        // A permission with no deny at all cannot be declined: generate fails
        // instead of allowing it.
        for request in [
            permission(vec![PermissionChoice::AllowOnce]),
            question(false),
        ] {
            let script = Script::default().turn(vec![
                Step::Emit(request),
                Step::AwaitAnswer,
                Step::End(completed()),
            ]);
            let runtime = Runtime::with_test_adapter(MockAdapter::new(script));
            let agent = runtime.discover().await.require("mock").unwrap().clone();
            let refused = runtime
                .generate(&agent, SessionOptions::in_dir(dir.path()), "go")
                .await;
            assert!(
                matches!(refused, Err(AgentError::ProtocolFailed(_))),
                "{refused:?}"
            );
        }
    }

    #[tokio::test]
    async fn generate_rejects_existing_sessions_before_launch() {
        let runtime = Runtime::new();
        let agent = AgentInstallation::at("pi", "/nonexistent/pi");
        for start in [
            SessionStart::Resume(crate::agent::ResumeToken::new("existing")),
            SessionStart::Fork {
                from: crate::agent::ResumeToken::new("existing"),
                at: None,
            },
        ] {
            let mut options = SessionOptions::in_dir(std::env::temp_dir());
            options.start = start;
            assert!(matches!(
                runtime.generate(&agent, options, "go").await,
                Err(AgentError::InvalidConfiguration(_))
            ));
        }
    }

    #[tokio::test]
    async fn generate_rejects_agents_without_tool_enforcement() {
        use crate::adapter::mock::{MockAdapter, Script};
        let adapter = MockAdapter::new(Script {
            permissions: false,
            ..Script::default()
        });
        let runtime = Runtime::with_test_adapter(adapter);
        let agent = runtime.discover().await.require("mock").unwrap().clone();
        let result = runtime
            .generate(&agent, SessionOptions::in_dir(std::env::temp_dir()), "go")
            .await;
        assert!(
            matches!(result, Err(AgentError::UnsupportedFeature(_))),
            "{result:?}"
        );
    }

    #[tokio::test]
    async fn generate_fails_on_tool_activity() {
        use crate::adapter::mock::{MockAdapter, Script, Step, completed, tool};
        use crate::event::ToolStatus;
        for status in [
            ToolStatus::Pending,
            ToolStatus::Running,
            ToolStatus::Completed,
        ] {
            let script = Script::default().turn(vec![
                Step::Emit(tool("tool-1", status)),
                Step::End(completed()),
            ]);
            let runtime = Runtime::with_test_adapter(MockAdapter::new(script));
            let agent = runtime.discover().await.require("mock").unwrap().clone();
            let result = runtime
                .generate(&agent, SessionOptions::in_dir(std::env::temp_dir()), "go")
                .await;
            assert!(
                matches!(result, Err(AgentError::ProtocolFailed(ref message)) if message.contains("tool")),
                "{result:?}"
            );
        }
    }

    /// `generate` stays hands-off even when the caller asked for AutoApprove.
    #[tokio::test]
    async fn generate_forces_ask_mode() {
        use crate::adapter::mock::MockAdapter;
        let runtime = Runtime::with_test_adapter(MockAdapter::permission_flow());
        let agent = runtime.discover().await.require("mock").unwrap().clone();
        let dir = tempfile::tempdir().unwrap();
        let options =
            SessionOptions::in_dir(dir.path()).permission_mode(PermissionMode::AutoApprove);
        // The mock's permission flow only reaches "Done." after an answer;
        // the text proves the request came through this loop, not auto-approval.
        let text = runtime
            .generate(&agent, options, "title this")
            .await
            .unwrap();
        assert_eq!(text, "Let me check. Done.");
    }

    /// Discovery through the runtime, over fixture shims on disk.
    mod discovery {
        use super::*;
        use crate::testutil::{HOME_VAR, shim, stub};
        use std::sync::{Mutex, OnceLock};

        fn env_lock() -> &'static Mutex<()> {
            static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
            LOCK.get_or_init(|| Mutex::new(()))
        }

        #[tokio::test]
        // The lock deliberately spans the awaits: it serializes tests that
        // mutate process-wide HOME/PATH.
        #[allow(clippy::await_holding_lock)]
        async fn discover_prefers_the_upgrade_and_names_it_when_missing() {
            let _guard = env_lock().lock().unwrap();
            let home = tempfile::tempdir().unwrap();
            let bin = home.path().join(".local/bin");
            let agy = stub(&bin, "agy");
            // The fixture wrappers exec `node`, so the real PATH stays behind
            // the fake bin.
            let path = std::env::var_os("PATH").unwrap_or_default();
            let mut paths = vec![bin.clone()];
            paths.extend(std::env::split_paths(&path));
            let _env = EnvGuard::set(&[
                (HOME_VAR, home.path().as_os_str().to_owned()),
                ("PATH", std::env::join_paths(paths).unwrap()),
            ]);

            // Only the CLI: it is the installation, and the ACP server is the
            // named upgrade with its own install hint.
            let report = Runtime::new().discover().await;
            let agent = report.require("antigravity").unwrap();
            assert_eq!(agent.executable_path, agy);
            assert!(agent.acp_args.is_none());
            let upgrade = agent.upgrade.as_ref().expect("upgrade named");
            assert_eq!(upgrade.name, "Antigravity ACP server");
            assert!(upgrade.install_hint.contains("antigravity-acp"));
            assert!(
                upgrade
                    .searched
                    .contains(&home.path().join(".local/agy-acp-server"))
            );

            // The server installed: it wins, over ACP, with nothing left to add.
            let server = stub(
                &home.path().join(".local/agy-acp-server"),
                "agy_acp_server.par",
            );
            let report = Runtime::new().discover().await;
            let agent = report.require("antigravity").unwrap();
            assert_eq!(agent.executable_path, server);
            assert_eq!(agent.acp_args.as_deref(), Some(&[][..]));
            assert!(agent.upgrade.is_none());

            // Opening it goes through the ACP adapter with the catalog's facts:
            // the server refuses `session/new` until `authenticate` adopts the
            // CLI's login, and the handshake does that itself.
            let fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("tests/fixtures/acp/fixture.mjs");
            shim(
                server.parent().unwrap(),
                "agy_acp_server.par",
                &fixture,
                "--auth-adopt",
            );
            let (session, _events) = Runtime::new()
                .open(agent, SessionOptions::in_dir(home.path()))
                .await
                .expect("open adopts the login");
            let info = session.info();
            assert!(matches!(
                info.details.auth,
                AuthStatus::Authenticated {
                    kind: crate::agent::AuthKind::Subscription,
                    ..
                }
            ));
            assert!(info.details.capabilities.supports(Capability::Permissions));
            session.close().await.unwrap();

            // Refused for good (the server's capitalised "Authentication
            // required", no runnable method of its own): typed, and the login
            // it names is the CLI's TUI, not the server.
            shim(
                server.parent().unwrap(),
                "agy_acp_server.par",
                &fixture,
                "--auth-required --capitalized-auth --no-auth-methods",
            );
            let err = Runtime::new()
                .open(agent, SessionOptions::in_dir(home.path()))
                .await
                .err()
                .expect("refused");
            let AgentError::AuthRequired { login } = err else {
                panic!("expected AuthRequired, got {err:?}");
            };
            assert!(
                matches!(&login[0], crate::agent::LoginMethod::Terminal { command, .. } if command == &["agy"]),
                "{login:?}"
            );
        }

        #[tokio::test]
        // The lock deliberately spans the awaits: it serializes tests that
        // mutate process-wide HOME/PATH.
        #[allow(clippy::await_holding_lock)]
        async fn env_override_to_the_cli_still_names_a_missing_upgrade() {
            let _guard = env_lock().lock().unwrap();
            let home = tempfile::tempdir().unwrap();
            let bin = home.path().join(".local/bin");
            let agy = stub(&bin, "agy");
            // The fixture wrappers exec `node`, so the real PATH stays behind
            // the fake bin.
            let path = std::env::var_os("PATH").unwrap_or_default();
            let mut paths = vec![bin.clone()];
            paths.extend(std::env::split_paths(&path));
            let _env = EnvGuard::set(&[
                (HOME_VAR, home.path().as_os_str().to_owned()),
                ("PATH", std::env::join_paths(paths).unwrap()),
                ("ANYAGENT_ANTIGRAVITY_BIN", agy.as_os_str().to_owned()),
            ]);

            // The override pins the CLI, but the missing server still rides
            // along as installation guidance.
            let report = Runtime::new().discover().await;
            let agent = report.require("antigravity").unwrap();
            assert_eq!(agent.executable_path, agy);
            assert!(agent.acp_args.is_none());
            let upgrade = agent.upgrade.as_ref().expect("upgrade named");
            assert_eq!(upgrade.name, "Antigravity ACP server");

            // The server installed: the override still forces headless, and
            // there is nothing missing left to name.
            stub(
                &home.path().join(".local/agy-acp-server"),
                "agy_acp_server.par",
            );
            let report = Runtime::new().discover().await;
            let agent = report.require("antigravity").unwrap();
            assert_eq!(agent.executable_path, agy);
            assert!(agent.acp_args.is_none());
            assert!(agent.upgrade.is_none());
        }

        /// Process-wide env vars set for one test and restored on drop, so a
        /// failed assertion cannot leak them into the next test.
        struct EnvGuard(Vec<(&'static str, Option<std::ffi::OsString>)>);

        impl EnvGuard {
            fn set(vars: &[(&'static str, std::ffi::OsString)]) -> Self {
                let saved = vars
                    .iter()
                    .map(|(name, value)| {
                        let orig = std::env::var_os(name);
                        unsafe { std::env::set_var(name, value) };
                        (*name, orig)
                    })
                    .collect();
                Self(saved)
            }
        }

        impl Drop for EnvGuard {
            fn drop(&mut self) {
                for (name, orig) in self.0.drain(..) {
                    unsafe {
                        match orig {
                            Some(v) => std::env::set_var(name, v),
                            None => std::env::remove_var(name),
                        }
                    }
                }
            }
        }
    }
}