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
use std::error::Error;
use std::fmt;
use std::path::Path;
use std::process::Command;
use ag_protocol::TurnPromptAttachment;
use crate::channel::AgentRequestKind;
use crate::model::agent::ReasoningLevel;
use crate::model::permission::PermissionMode;
use crate::model::session::SpeedMode;
/// Maximum concurrent subagents requested from providers with a native limit.
///
/// Excludes the parent agent. Provider exceptions still apply; this is not a
/// host-wide CPU or memory limit.
pub(super) const MAX_CONCURRENT_SUBAGENTS: usize = 2;
/// Transport runtime used to execute turns for one backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentTransport {
/// Provider runs through a managed session runtime.
AppServer,
/// Provider runs as direct CLI subprocess commands.
Cli,
}
impl AgentTransport {
/// Returns whether this transport uses managed runtime sessions.
pub fn uses_app_server(self) -> bool {
matches!(self, Self::AppServer)
}
}
/// Prompt delivery mode used by one provider backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AgentPromptTransport {
/// Prompt is passed inline through argv.
Argv,
/// Prompt is streamed through stdin.
Stdin,
}
/// Managed-runtime thought-stream classification policy for one provider.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AppServerThoughtPolicy {
/// Provider does not expose dedicated thought phases.
None,
/// Provider uses phase labels to distinguish thought chunks.
PhaseLabel,
}
/// Request payload used to build provider transport commands.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BuildCommandRequest<'a> {
/// Ordered local image attachments referenced from the prompt body.
pub attachments: &'a [TurnPromptAttachment],
/// Working directory where the command will run.
pub folder: &'a Path,
/// Main repository checkout that must remain read-only during the turn,
/// when Agentty can resolve it.
pub main_checkout_root: Option<&'a Path>,
/// Provider-specific model identifier.
pub model: &'a str,
/// Filesystem and command permission policy for this turn.
pub permission_mode: PermissionMode,
/// Current personality body included during a full prompt bootstrap.
pub personality_prompt: Option<&'a str>,
/// User prompt to send.
pub prompt: &'a str,
/// Reasoning effort preference for this turn.
///
/// Ignored by backends/models that do not support reasoning effort.
pub reasoning_level: ReasoningLevel,
/// Replayable transcript text captured when the turn was queued.
pub replay_transcript: Option<&'a str>,
/// Canonical request kind that drives execution and protocol semantics.
pub request_kind: &'a AgentRequestKind,
/// Response-speed preference for this turn.
pub speed_mode: SpeedMode,
}
/// Error type for backend setup and command construction failures.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AgentBackendError {
/// One-time backend setup failure.
Setup(String),
/// Per-command build failure.
CommandBuild(String),
}
impl fmt::Display for AgentBackendError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Setup(message) | Self::CommandBuild(message) => {
write!(formatter, "{message}")
}
}
}
}
impl Error for AgentBackendError {}
/// Builds and configures external agent CLI commands.
#[cfg_attr(any(test, feature = "test-utils"), mockall::automock)]
pub trait AgentBackend: Send + Sync {
/// Performs one-time setup in an agent folder before first run.
///
/// # Errors
/// Returns an error when one-time backend setup cannot be completed.
fn setup(&self, folder: &Path) -> Result<(), AgentBackendError>;
/// Builds one provider transport command.
///
/// CLI-backed providers return the per-turn subprocess command. Managed
/// providers return the long-lived runtime command that owns later turn
/// execution over its native protocol.
///
/// # Errors
/// Returns an error when prompt rendering or provider argument
/// construction fails.
fn build_command<'request>(
&'request self,
request: BuildCommandRequest<'request>,
) -> Result<Command, AgentBackendError>;
}
#[cfg(test)]
#[path = "backend_test.rs"]
mod tests;