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
//! Agent backend trait for provider setup and command construction, with its
//! request, error, and transport types.
use std::error::Error;
use std::fmt;
use std::path::Path;
use std::process::Command;
use ag_contracts::{AgentRequestKind, PermissionMode, ReasoningLevel, SpeedMode};
use ag_protocol::TurnPromptAttachment;
/// 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,
/// Provider runs in process through `ag-harness`, when the host configures
/// it.
Native,
}
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],
/// Worker-selected policy translated into provider startup settings.
pub execution_policy: &'a ag_contracts::ExecutionPolicy,
/// 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 agent body included during a full prompt bootstrap.
pub agent_profile_prompt: Option<&'a str>,
/// Agent change that must reach prompt rendering, including withdrawal
/// guidance when continuing a previously configured session.
pub agent_profile_update: &'a ag_contracts::AgentDefinitionPromptUpdate,
/// 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 AgentBackendError {
/// Wraps one command-construction failure, such as an unrepresentable
/// prompt image path or a prompt template rendering error, as
/// [`AgentBackendError::CommandBuild`].
pub(crate) fn command_build(error: impl fmt::Display) -> Self {
Self::CommandBuild(error.to_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 {}
impl From<AgentBackendError> for ag_contracts::AgentError {
fn from(error: AgentBackendError) -> Self {
Self::Backend(error.to_string())
}
}
/// 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;