Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
317 changes: 317 additions & 0 deletions crates/agent/src/antigravity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ struct Antigravity {
/// so their immediate spawn-ack `completed` updates do not prematurely
/// close the subagent item.
spawned_subagents: Mutex<HashMap<String, subagents::SpawnedSubagent>>,
/// In-flight `exit_plan_mode` / `enter_plan_mode` calls observed on the ACP
/// wire, keyed by `toolCallId`, so their `tool_call_update` frames stay on
/// `ItemContent::Plan` / `ItemContent::PlanModeEntered`.
plan_mode_calls: Mutex<HashMap<String, PlanModeCall>>,
/// Control channel for the session's background subagent/proactive watcher.
watcher: Mutex<Option<Sender<subagents::WatcherCommand>>>,
/// When the agent was last heard from in the open turn ([`watchdog`]),
Expand All @@ -489,6 +493,90 @@ struct SessionRecord {
path: PathBuf,
}

#[derive(Clone)]
enum PlanModeCall {
Exit { plan: String },
Enter { reason: Option<String> },
}

impl PlanModeCall {
fn from_tool_call(call: &acp::ToolCall) -> Option<Self> {
match call.title.as_str() {
"Run exit_plan_mode" => {
let plan = plan_input(call.raw_input.as_ref())?;
Some(Self::Exit { plan })
}
"Run enter_plan_mode" => Some(Self::Enter {
reason: reason_input(call.raw_input.as_ref()),
}),
_ => None,
}
}

fn apply_raw_input(&mut self, raw_input: Option<&Value>) {
match self {
Self::Exit { plan } => {
if let Some(updated) = plan_input(raw_input) {
*plan = updated;
}
}
Self::Enter { reason } => {
if let Some(updated) = reason_input(raw_input) {
*reason = Some(updated);
}
}
}
}

fn item_content(&self) -> ItemContent {
match self {
Self::Exit { plan } => ItemContent::Plan {
markdown: plan.clone(),
},
Self::Enter { reason } => ItemContent::PlanModeEntered {
reason: reason.clone(),
},
}
}

fn failed_tool_call(&self, output: Option<String>) -> ItemContent {
let (name, input) = match self {
Self::Exit { plan } => ("exit_plan_mode", json!({ "plan": plan })),
Self::Enter { reason } => (
"enter_plan_mode",
reason
.as_ref()
.map_or_else(|| json!({}), |reason| json!({ "reason": reason })),
),
};
ItemContent::ToolCall {
image_reads: Vec::new(),
name: name.to_owned(),
input,
output,
status: ItemStatus::Failed,
family: None,
}
}
}

fn plan_input(raw_input: Option<&Value>) -> Option<String> {
raw_input?
.get("plan")
.and_then(Value::as_str)
.filter(|plan| !plan.trim().is_empty())
.map(str::to_owned)
}

fn reason_input(raw_input: Option<&Value>) -> Option<String> {
raw_input?
.get("reason")
.and_then(Value::as_str)
.map(str::trim)
.filter(|reason| !reason.is_empty())
.map(str::to_owned)
}

impl Antigravity {
fn send_watcher(&self, command: subagents::WatcherCommand) {
if let Some(tx) = self
Expand Down Expand Up @@ -526,6 +614,11 @@ impl Antigravity {
.lock()
.unwrap_or_else(|held| held.into_inner())
.contains_key(call.tool_call_id.0.as_ref())
&& !self
.plan_mode_calls
.lock()
.unwrap_or_else(|held| held.into_inner())
.contains_key(call.tool_call_id.0.as_ref())
}
acp::SessionUpdate::ToolCallUpdate(update) => {
!state.has_tool(&update.tool_call_id.0)
Expand All @@ -534,6 +627,11 @@ impl Antigravity {
.lock()
.unwrap_or_else(|held| held.into_inner())
.contains_key(update.tool_call_id.0.as_ref())
&& !self
.plan_mode_calls
.lock()
.unwrap_or_else(|held| held.into_inner())
.contains_key(update.tool_call_id.0.as_ref())
}
_ => false,
}
Expand Down Expand Up @@ -946,6 +1044,27 @@ impl Dialect for Antigravity {
{
active.usage_seen = false;
}
if call.status != acp::ToolCallStatus::Failed
&& let Some(plan_call) = PlanModeCall::from_tool_call(call)
{
events.extend(state.flush_text());
let id = call.tool_call_id.0.to_string();
let item = ThreadItem {
id: id.clone(),
parent_item_id: None,
content: plan_call.item_content(),
};
if call.status == acp::ToolCallStatus::Completed {
events.push(AgentEvent::ItemCompleted(item));
} else {
self.plan_mode_calls
.lock()
.unwrap_or_else(|held| held.into_inner())
.insert(id, plan_call);
events.push(AgentEvent::ItemStarted(item));
}
return events;
}
if let Some((recipient, message)) = subagents::parse_send_message(call) {
self.send_watcher(subagents::WatcherCommand::MessageSent {
recipient,
Expand Down Expand Up @@ -992,6 +1111,57 @@ impl Dialect for Antigravity {
// the subagent item from its on-disk trajectory.
return events;
}
let plan_update = {
let mut calls = self
.plan_mode_calls
.lock()
.unwrap_or_else(|held| held.into_inner());
if let Some(plan_call) = calls.get_mut(id) {
plan_call.apply_raw_input(update.fields.raw_input.as_ref());
let snapshot = plan_call.clone();
if matches!(
update.fields.status,
Some(acp::ToolCallStatus::Completed | acp::ToolCallStatus::Failed)
) {
calls.remove(id);
}
Some(snapshot)
} else {
None
}
};
if let Some(plan_call) = plan_update {
events.extend(state.flush_text());
let item_id = id.to_owned();
match update.fields.status {
Some(acp::ToolCallStatus::Failed) => {
let summary = tool_error_summary(
update.fields.content.as_deref(),
update.fields.raw_output.as_ref(),
);
events.push(AgentEvent::ItemCompleted(ThreadItem {
id: item_id,
parent_item_id: None,
content: plan_call.failed_tool_call(summary),
}));
}
Some(acp::ToolCallStatus::Completed) => {
events.push(AgentEvent::ItemCompleted(ThreadItem {
id: item_id,
parent_item_id: None,
content: plan_call.item_content(),
}));
}
_ => {
events.push(AgentEvent::ItemUpdated(ThreadItem {
id: item_id,
parent_item_id: None,
content: plan_call.item_content(),
}));
}
}
return events;
}
if state.tool_title(id) == Some("Run manage_subagents") {
self.send_watcher(subagents::WatcherCommand::Wake);
}
Expand Down Expand Up @@ -1773,6 +1943,153 @@ mod tests {
assert_eq!(normalize_mcp_tool_name("read_file", &servers), None);
}

/// `exit_plan_mode` and `enter_plan_mode` map to canonical `Plan` and
/// `PlanModeEntered` items across `tool_call` -> `tool_call_update`
/// (observed in `agy acp` 1.5.20 orchestrator turns), while failures keep
/// their error visible as a failed `ToolCall` and empty plans fall through.
#[test]
fn plan_mode_tools_map_to_plan_and_plan_mode_entered() {
let dialect = Antigravity::default();
let mut state = State::new(ProviderKind::Antigravity, PathBuf::from("/repo"));
let _ = state.begin_agent_turn("turn-1");

let plan = "# Batch 1\n\n1. Update parser\n2. Run tests";
let started_exit = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call",
"toolCallId": "call_exit",
"title": "Run exit_plan_mode",
"kind": "other",
"status": "in_progress",
"rawInput": { "plan": plan }
})),
);
assert!(matches!(
started_exit.as_slice(),
[AgentEvent::ItemStarted(ThreadItem {
id,
parent_item_id: None,
content: ItemContent::Plan { markdown },
})] if id == "call_exit" && markdown == plan
));

let completed_exit = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call_exit",
"status": "completed",
"rawOutput": "Plan mode is off."
})),
);
assert!(matches!(
completed_exit.as_slice(),
[AgentEvent::ItemCompleted(ThreadItem {
id,
parent_item_id: None,
content: ItemContent::Plan { markdown },
})] if id == "call_exit" && markdown == plan
));

let reason = "Tests failed twice.\n1. Foo failed\n2. Bar panicked";
let started_enter = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call",
"toolCallId": "call_enter",
"title": "Run enter_plan_mode",
"kind": "other",
"status": "in_progress",
"rawInput": { "reason": reason }
})),
);
assert!(matches!(
started_enter.as_slice(),
[AgentEvent::ItemStarted(ThreadItem {
id,
parent_item_id: None,
content: ItemContent::PlanModeEntered { reason: Some(text) },
})] if id == "call_enter" && text == reason
));

let completed_enter = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call_enter",
"status": "completed",
"rawOutput": "Plan mode is on."
})),
);
assert!(matches!(
completed_enter.as_slice(),
[AgentEvent::ItemCompleted(ThreadItem {
id,
parent_item_id: None,
content: ItemContent::PlanModeEntered { reason: Some(text) },
})] if id == "call_enter" && text == reason
));

// A failed update reverts to a failed ToolCall so the error stays visible.
let _ = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call",
"toolCallId": "call_enter_fail",
"title": "Run enter_plan_mode",
"kind": "other",
"status": "in_progress",
"rawInput": { "reason": "Re-plan" }
})),
);
let failed_enter = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call_enter_fail",
"status": "failed",
"rawOutput": "Already in plan mode"
})),
);
assert!(matches!(
failed_enter.as_slice(),
[AgentEvent::ItemCompleted(ThreadItem {
id,
content: ItemContent::ToolCall {
name,
status: ItemStatus::Failed,
output: Some(output),
..
},
..
})] if id == "call_enter_fail"
&& name == "enter_plan_mode"
&& output == "Already in plan mode"
));

// An exit_plan_mode call with no plan text falls through as a regular ToolCall.
let empty_exit = dialect.session_update(
&mut state,
notification(json!({
"sessionUpdate": "tool_call",
"toolCallId": "call_exit_empty",
"title": "Run exit_plan_mode",
"kind": "other",
"status": "completed",
"rawInput": { "plan": " " }
})),
);
assert!(matches!(
empty_exit.as_slice(),
[AgentEvent::ItemCompleted(ThreadItem {
id,
content: ItemContent::ToolCall { .. },
..
})] if id == "call_exit_empty"
));
}

/// `read_file` on an image over `agy acp` 1.5.23 sends only the text
/// placeholder `"Showing binary file of type image/..."` with no inline
/// image block; `enrich_events` captures the file from disk, skips
Expand Down
Loading
Loading