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
129 changes: 71 additions & 58 deletions src/acp_client.rs

Large diffs are not rendered by default.

102 changes: 45 additions & 57 deletions src/acp_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl crate::app::App {
is_replay,
} => {
self.apply_acp_session_update(&session_id, update, is_replay);
self.drain_pending_commands()
vec![]
}
AcpAppEvent::SessionReplay {
session_id,
Expand All @@ -358,7 +358,7 @@ impl crate::app::App {
for update in updates {
self.apply_acp_session_update(&session_id, update, true);
}
self.drain_pending_commands()
vec![]
}
AcpAppEvent::UndoStack(undo_stack) => {
self.undo_state = self.build_undo_state_from_server_stack(&undo_stack, None, None);
Expand Down Expand Up @@ -386,10 +386,12 @@ impl crate::app::App {
self.streaming_cache.invalidate();
self.set_status(LogLevel::Info, "session", "undone - reloading session");
if let Some(ref sid) = self.session_id {
return vec![Command::LoadSession {
session_id: sid.clone(),
cwd: self.current_session_cwd(),
}];
return Command::load_session_commands(
sid.clone(),
self.current_session_cwd(),
self.agent_id.clone(),
)
.into();
}
}
UndoResult::Rejected {
Expand Down Expand Up @@ -421,10 +423,12 @@ impl crate::app::App {
self.build_undo_state_from_server_stack(&stack, None, None);
self.set_status(LogLevel::Info, "session", "redone - reloading session");
if let Some(ref sid) = self.session_id {
return vec![Command::LoadSession {
session_id: sid.clone(),
cwd: self.current_session_cwd(),
}];
return Command::load_session_commands(
sid.clone(),
self.current_session_cwd(),
self.agent_id.clone(),
)
.into();
}
}
RedoResult::Rejected { message, stack } => {
Expand All @@ -449,16 +453,12 @@ impl crate::app::App {
} => {
self.popup = Popup::None;
self.set_status(LogLevel::Info, "fork", "forked - loading session");
return vec![
Command::LoadSession {
session_id: forked_session_id.clone(),
cwd: self.current_session_cwd(),
},
Command::SubscribeSession {
session_id: forked_session_id,
agent_id: self.agent_id.clone(),
},
];
return Command::load_session_commands(
forked_session_id,
self.current_session_cwd(),
self.agent_id.clone(),
)
.into();
}
ForkResult::Succeeded {
source_session_id: _,
Expand Down Expand Up @@ -2532,8 +2532,9 @@ mod tests {
}

#[test]
fn native_remote_attach_loads_attached_session() {
fn native_remote_attach_loads_and_subscribes_once() {
let mut app = App::new();
app.agent_id = Some("agent-1".into());

let replies = app.handle_acp_event(AcpAppEvent::RemoteSessionAttached(
RemoteSessionAttachInfo {
Expand All @@ -2548,7 +2549,12 @@ mod tests {
assert_eq!(app.session_remote_node_id("remote-1"), Some("node-1"));
assert!(matches!(
replies.as_slice(),
[Command::LoadSession { session_id, .. }] if session_id == "remote-1"
[
Command::LoadSession { session_id: load_id, .. },
Command::SubscribeSession { session_id: subscribe_id, agent_id },
] if load_id == "remote-1"
&& subscribe_id == "remote-1"
&& agent_id.as_deref() == Some("agent-1")
));
}

Expand Down Expand Up @@ -3564,6 +3570,7 @@ mod tests {
fn native_undo_result_success_updates_state_and_reloads_session() {
let mut app = App::new();
app.session_id = Some("session-1".into());
app.agent_id = Some("agent-1".into());
app.activity = ActivityState::SessionOp(SessionOp::Undo);
app.undoable_turns.push(UndoableTurn {
turn_id: "u1".into(),
Expand All @@ -3589,7 +3596,13 @@ mod tests {
);
assert!(matches!(
replies.as_slice(),
[Command::LoadSession { session_id, cwd }] if session_id == "session-1" && cwd.is_none()
[
Command::LoadSession { session_id: load_id, cwd },
Command::SubscribeSession { session_id: subscribe_id, agent_id },
] if load_id == "session-1"
&& subscribe_id == "session-1"
&& cwd.is_none()
&& agent_id.as_deref() == Some("agent-1")
));
}

Expand Down Expand Up @@ -3685,6 +3698,7 @@ mod tests {
fn native_redo_result_success_rebuilds_state_and_reloads_session() {
let mut app = App::new();
app.session_id = Some("session-1".into());
app.agent_id = Some("agent-1".into());
app.activity = ActivityState::SessionOp(SessionOp::Redo);

let replies = app.handle_acp_event(AcpAppEvent::RedoResult(RedoResult::Applied {
Expand All @@ -3699,7 +3713,13 @@ mod tests {
assert!(app.can_redo());
assert!(matches!(
replies.as_slice(),
[Command::LoadSession { session_id, cwd }] if session_id == "session-1" && cwd.is_none()
[
Command::LoadSession { session_id: load_id, cwd },
Command::SubscribeSession { session_id: subscribe_id, agent_id },
] if load_id == "session-1"
&& subscribe_id == "session-1"
&& cwd.is_none()
&& agent_id.as_deref() == Some("agent-1")
));
}

Expand Down Expand Up @@ -3741,6 +3761,7 @@ mod tests {
assert_eq!(app.pending_fork_message_id, None);
assert_eq!(app.popup, Popup::None);
assert_eq!(app.status, "forked - loading session");
assert_eq!(replies.len(), 2);
assert!(matches!(
replies.as_slice(),
[
Expand Down Expand Up @@ -3796,39 +3817,6 @@ mod tests {
assert!(app.can_redo());
}

#[test]
fn session_update_and_replay_drain_pending_commands() {
let mut app = app_with_active_session();
app.pending_commands.push(Command::SubscribeSession {
session_id: "child-1".into(),
agent_id: Some("agent-1".into()),
});

let update_commands = app.handle_acp_event(AcpAppEvent::SessionUpdate {
session_id: TEST_SESSION_ID.into(),
is_replay: false,
update: AcpSessionUpdate::TurnStarted,
});

assert_eq!(
update_commands,
vec![Command::SubscribeSession {
session_id: "child-1".into(),
agent_id: Some("agent-1".into()),
}]
);
assert!(app.pending_commands.is_empty());

app.pending_commands.push(Command::GetFileIndex);
let replay_commands = app.handle_acp_event(AcpAppEvent::SessionReplay {
session_id: TEST_SESSION_ID.into(),
updates: Vec::new(),
});

assert_eq!(replay_commands, vec![Command::GetFileIndex]);
assert!(app.pending_commands.is_empty());
}

#[test]
fn native_session_update_for_other_session_marks_activity_only() {
let mut app = App::new();
Expand Down
8 changes: 0 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,6 @@ pub struct App {
/// Set after DelegationCompleted/DelegationFailed; consumed by the next
/// UserMessageStored to suppress the noisy batch-result message.
pub suppress_delegation_result: bool,
/// Commands queued by event handlers (e.g. SubscribeSession for child sessions).
/// Drained by native ACP after each event/replay batch.
pub pending_commands: Vec<Command>,
/// Child-session state observed before a delegation entry can be linked.
pub pending_delegate_child_states: HashMap<String, DelegateChildState>,
pub pending_delegate_child_stats: HashMap<String, DelegateStats>,
Expand Down Expand Up @@ -814,10 +811,6 @@ fn move_wrapping_cursor(cursor: usize, len: usize, delta: isize) -> usize {
}

impl App {
pub(crate) fn drain_pending_commands(&mut self) -> Vec<Command> {
std::mem::take(&mut self.pending_commands)
}

pub fn begin_session_discovery(&mut self) -> Option<Command> {
if self.session_discovery_in_progress || !self.pending_session_group_loads.is_empty() {
return None;
Expand Down Expand Up @@ -990,7 +983,6 @@ impl App {
parent_session_id: None,
pending_parent_session_id: None,
suppress_delegation_result: false,
pending_commands: Vec::new(),
pending_delegate_child_states: HashMap::new(),
pending_delegate_child_stats: HashMap::new(),
delegate_child_message_ids: HashMap::new(),
Expand Down
114 changes: 114 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,36 @@ impl SessionListRequest {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PromptBlock {
Text { text: String },
ResourceLink { name: String, uri: String },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Command {
Init,
ListSessions {
request: SessionListRequest,
cursor: Option<String>,
},
ListRemoteNodes,
ListRemoteSessions {
node_id: String,
offset: u32,
limit: u32,
},
CreateRemoteSession {
node_id: String,
cwd: Option<String>,
},
AttachRemoteSession {
node_id: String,
session_id: String,
},
DismissRemoteSession {
session_id: String,
},
CreateMeshInvite {
mesh_name: Option<String>,
ttl: Option<String>,
Expand All @@ -38,6 +57,7 @@ pub enum Command {
SetReasoningEffort {
reasoning_effort: String,
},
ListProfiles,
ListProfileAgents {
profile_id: String,
},
Expand All @@ -47,19 +67,68 @@ pub enum Command {
model_id: Option<String>,
node_id: Option<String>,
},
NewSession {
cwd: Option<String>,
profile_id: Option<String>,
},
LoadSession {
session_id: String,
cwd: Option<String>,
},
Prompt {
prompt: Vec<PromptBlock>,
local_id: String,
},
CancelSession,
ListAllModels {
refresh: bool,
},
SetSessionModel {
session_id: String,
model_id: String,
node_id: Option<String>,
},
SubscribeSession {
session_id: String,
agent_id: Option<String>,
},
DeleteSession {
session_id: String,
},
ForkSession {
message_id: String,
},
Undo {
message_id: String,
},
Redo,
GetFileIndex,
SetAgentMode {
mode: String,
},
ElicitationResponse {
elicitation_id: String,
action: String,
content: Option<serde_json::Value>,
},
ListAuthProviders,
StartOAuthLogin {
provider: String,
},
CompleteOAuthLogin {
flow_id: String,
response: String,
},
DisconnectOAuth {
provider: String,
},
SetApiToken {
provider: String,
api_key: String,
},
ClearApiToken {
provider: String,
},
}

impl Command {
Expand Down Expand Up @@ -99,4 +168,49 @@ impl Command {
limit,
}
}

pub fn load_session_commands(
session_id: String,
cwd: Option<String>,
agent_id: Option<String>,
) -> [Self; 2] {
[
Self::LoadSession {
session_id: session_id.clone(),
cwd,
},
Self::SubscribeSession {
session_id,
agent_id,
},
]
}
}

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

#[test]
fn load_session_commands_preserve_order_and_fields() {
let commands = Command::load_session_commands(
"session-1".into(),
Some("/repo".into()),
Some("agent-1".into()),
);

assert_eq!(
commands,
[
Command::LoadSession {
session_id: "session-1".into(),
cwd: Some("/repo".into()),
},
Command::SubscribeSession {
session_id: "session-1".into(),
agent_id: Some("agent-1".into()),
},
]
);
}
}
Loading