use std::path::{Path, PathBuf};
use agent_client_protocol::v1::schema::{
AuthMethod, AuthMethodId, RequestPermissionRequest, SessionConfigOption, SessionModeId,
ToolKind,
};
use serde_json::{json, Value};
use crate::{
acp_runtime::{
agent_tools::{McpToolNaming, SERVER_NAME},
permission::PermissionDecision,
AcpAgentPolicy, AcpSpawnPlan, AgentToolCallRef, ExtensionAnswer, NestedAgents,
SessionConfigChoice,
},
agent::{AntigravityTool, PromptPolicy},
cli_runtime::{status_sink::RuntimeLabel, CliExecutable},
permission::PermissionMode,
};
use super::{
agent_file, executable,
fence::AntigravityFence,
models::{self, MODEL_CONFIG_ID},
ANTIGRAVITY_LABEL,
};
pub(crate) const MCP_TOOL_NAMING: McpToolNaming = McpToolNaming::separated_by("_");
pub(crate) struct AntigravityAcpPolicy {
model: Option<String>,
tools: Vec<AntigravityTool>,
permission_mode: PermissionMode,
cwd: PathBuf,
system_prompt: PromptPolicy,
rho_tools: RhoTools,
prepared: Option<Prepared>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum RhoTools {
Unserved,
Served,
}
impl RhoTools {
fn mcp_servers(self) -> &'static [&'static str] {
match self {
Self::Unserved => &[],
Self::Served => &[SERVER_NAME],
}
}
}
struct Prepared {
fence: AntigravityFence,
agent: agent_file::RunAgent,
}
impl AntigravityAcpPolicy {
pub(crate) fn new(
model: Option<String>,
tools: Vec<AntigravityTool>,
permission_mode: PermissionMode,
cwd: PathBuf,
system_prompt: PromptPolicy,
) -> Self {
Self {
model,
tools,
permission_mode,
cwd,
system_prompt,
rho_tools: RhoTools::Unserved,
prepared: None,
}
}
pub(crate) fn serving_rho_tools(self) -> Self {
Self {
rho_tools: RhoTools::Served,
..self
}
}
}
impl AcpAgentPolicy for AntigravityAcpPolicy {
fn label(&self) -> RuntimeLabel {
ANTIGRAVITY_LABEL
}
fn resolve_executable(
&self,
environment: &crate::session_environment::Ambient,
) -> Result<CliExecutable, String> {
executable::resolve(environment).map_err(|error| error.to_string())
}
fn spawn_plan(&self) -> Result<AcpSpawnPlan, String> {
let prepared = self
.prepared
.as_ref()
.ok_or("antigravity: run was not prepared")?;
Ok(AcpSpawnPlan {
argv: executable::server_args(&prepared.agent),
cwd: self.cwd.clone(),
env: Vec::new(),
})
}
fn prepare(&mut self, run_dir: &Path) -> Result<(), String> {
if let PromptPolicy::Replace(_) = self.system_prompt {
return Err(
"antigravity: `prompt: replace` is unsupported: agy only appends to its system prompt"
.into(),
);
}
let fence = AntigravityFence::new(self.permission_mode, &self.tools)
.map_err(|error| error.to_string())?;
let agent = agent_file::write(run_dir, fence.tools(), self.rho_tools.mcp_servers())
.map_err(|error| {
format!("antigravity: could not write the run's agy agent: {error}")
})?;
self.prepared = Some(Prepared { fence, agent });
Ok(())
}
fn log_path(&self, output_file: &Path) -> PathBuf {
output_file.with_file_name(crate::subagent::LOG_FILE_NAME)
}
fn auth_method(&self, _advertised: &[AuthMethod]) -> Option<AuthMethodId> {
None
}
fn session_meta(
&self,
_nested: NestedAgents,
) -> Option<agent_client_protocol::v1::schema::Meta> {
match &self.system_prompt {
PromptPolicy::Extend(text) if !text.trim().is_empty() => {
json!({"antigravity": {"systemPrompt": {"append": text}}})
.as_object()
.cloned()
}
PromptPolicy::Extend(_) | PromptPolicy::Replace(_) => None,
}
}
fn session_mode(&self) -> Option<SessionModeId> {
self.prepared
.as_ref()
.map(|prepared| SessionModeId::new(prepared.fence.session_mode()))
}
fn session_config(&self) -> Vec<SessionConfigChoice> {
self.model
.iter()
.map(|model| SessionConfigChoice {
id: MODEL_CONFIG_ID.into(),
value: model.clone().into(),
})
.collect()
}
fn config_seen(&self, options: &[SessionConfigOption]) {
models::record(options);
}
fn decide_permission(&self, request: &RequestPermissionRequest) -> PermissionDecision {
self.prepared
.as_ref()
.map_or(PermissionDecision::Reject, |prepared| {
prepared.fence.decide(request)
})
}
fn agent_tool<'a>(&self, call: AgentToolCallRef<'a>) -> Option<&'a str> {
if call.kind != Some(ToolKind::Other) {
return None;
}
MCP_TOOL_NAMING.tool_of(call.name?)
}
fn answer_extension(&self, _method: &str, _params: &Value) -> Option<ExtensionAnswer> {
None
}
}
#[cfg(test)]
#[path = "policy_tests.rs"]
mod tests;