mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-03 14:01:30 +01:00
agent_servers: Treat replayed ACP v2 subagent state as history (#66061)
When a load or resume replays history, its successful response is the freshness boundary for subagents. Work state and `cancel` grants reported during the replay are history: they may stay visible, but must not confirm current activity or allow cancelling until the agent reports them again after the response. Before this change, a replayed `subagent_update` with `capabilities.cancel` and a replayed `state_update: running` for the subagent left Zed showing it as running and cancellable. - **Boundary:** when a v2 resume with `replayFrom: start` succeeds, `AcpThread::invalidate_replayed_subagent_state` clears every subagent's `cancel` grant, recursively for nested subagents. A replayed running or requires-action state becomes unconfirmed; a replayed outcome (completed, canceled, failed) stays, since it confirms no activity. claude-agent-acp replays each subagent's outcome and sends nothing after the response. Titles, descriptions and transcripts stay. - **Ordering:** the call sits in the ordered response handler, next to the existing check that the agent reported the parent's current state. That handler applies after the replay and before any update sent after the response, so fresh snapshots are kept. - **Resume without replay is unchanged:** the RFD treats updates sent before its response as current. Release Notes: - N/A
This commit is contained in:
@@ -3979,6 +3979,26 @@ impl AcpThread {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ends a history replay into this session, after a successful load or resume. The
|
||||
/// subagents RFD treats what the replay reported about subagents as history: it may
|
||||
/// stay visible, but must not allow cancelling or confirm current activity until the
|
||||
/// agent reports it again. A replayed outcome, such as a completed or failed
|
||||
/// subagent, stays, since it confirms no activity.
|
||||
pub fn invalidate_replayed_subagent_state(&mut self, cx: &mut Context<Self>) {
|
||||
for subagent in self.subagents.values() {
|
||||
subagent.update(cx, |subagent, cx| {
|
||||
if let Some(association) = subagent.subagent_association.as_mut() {
|
||||
association.can_cancel = false;
|
||||
cx.notify();
|
||||
}
|
||||
if subagent.foreground_activity().is_active() {
|
||||
subagent.clear_session_state(cx).log_err();
|
||||
}
|
||||
subagent.invalidate_replayed_subagent_state(cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parent_session_id(&self) -> Option<&acp_v2::SessionId> {
|
||||
self.parent_session_id.as_ref()
|
||||
}
|
||||
|
||||
@@ -1024,6 +1024,13 @@ impl AcpV2Connection {
|
||||
let this = this.upgrade().context("Connection was released")?;
|
||||
let thread = thread.upgrade().context("Session was released")?;
|
||||
cx.update(|cx| {
|
||||
// Ordered responses apply after the replay and before any update sent
|
||||
// after the response, so this is the RFD's freshness boundary.
|
||||
if replay_history {
|
||||
thread.update(cx, |thread, cx| {
|
||||
thread.invalidate_replayed_subagent_state(cx)
|
||||
});
|
||||
}
|
||||
if !thread.read(cx).has_received_session_state() {
|
||||
log::warn!(
|
||||
"Agent resumed the session without reporting its current foreground state; activity remains unconfirmed"
|
||||
@@ -2462,6 +2469,106 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn v2_replayed_subagent_state_is_history(cx: &mut TestAppContext) {
|
||||
let harness = SessionHarness::new(session_capabilities(), vec![], vec![], cx).await;
|
||||
let announce_running_child = || {
|
||||
harness.send_update(
|
||||
"saved",
|
||||
acp::SessionUpdate::SubagentUpdate(
|
||||
acp::SubagentUpdate::new("child")
|
||||
.title("Investigator")
|
||||
.capabilities(
|
||||
acp::SubagentSessionCapabilities::new()
|
||||
.cancel(acp::SessionCancelCapabilities::new()),
|
||||
),
|
||||
),
|
||||
);
|
||||
harness.send_update(
|
||||
"child",
|
||||
acp::SessionUpdate::StateUpdate(acp::StateUpdate::Running(
|
||||
acp::RunningStateUpdate::new(),
|
||||
)),
|
||||
);
|
||||
};
|
||||
let cancelled = || {
|
||||
acp::StateUpdate::Idle(
|
||||
acp::IdleStateUpdate::new().stop_reason(acp::StopReason::Cancelled),
|
||||
)
|
||||
};
|
||||
for replay_history in [true, false] {
|
||||
let loading = harness.reopen(replay_history, cx);
|
||||
let (_, responder) = bounded(harness.resume_requests.recv(), cx)
|
||||
.await
|
||||
.expect("resume");
|
||||
harness.resume_state(acp::StateUpdate::Idle(acp::IdleStateUpdate::new()));
|
||||
announce_running_child();
|
||||
// A subagent whose replayed history ends, as claude-agent-acp replays each one.
|
||||
harness.send_update(
|
||||
"saved",
|
||||
acp::SessionUpdate::SubagentUpdate(acp::SubagentUpdate::new("finished")),
|
||||
);
|
||||
for state in [
|
||||
acp::StateUpdate::Running(acp::RunningStateUpdate::new()),
|
||||
cancelled(),
|
||||
] {
|
||||
harness.send_update("finished", acp::SessionUpdate::StateUpdate(state));
|
||||
}
|
||||
responder
|
||||
.respond(acp::ResumeSessionResponse::new())
|
||||
.expect("resumed");
|
||||
let parent = bounded(loading, cx).await.expect("resumed parent");
|
||||
let (child, finished) = parent.read_with(cx, |parent, _| {
|
||||
(
|
||||
parent.subagent(&"child".into()).cloned(),
|
||||
parent.subagent(&"finished".into()).cloned(),
|
||||
)
|
||||
});
|
||||
let child = child.expect("the subagent was announced before the response");
|
||||
let finished = finished.expect("the subagent was announced before the response");
|
||||
finished.read_with(cx, |finished, _| {
|
||||
assert_eq!(
|
||||
finished.foreground_state(),
|
||||
Some(&cancelled()),
|
||||
"a replayed outcome confirms no activity, so it stays"
|
||||
);
|
||||
});
|
||||
child.read_with(cx, |child, _| {
|
||||
let association = child.subagent_association().expect("subagent");
|
||||
assert_eq!(association.title.as_deref(), Some("Investigator"));
|
||||
if replay_history {
|
||||
assert!(!association.can_cancel, "a replayed grant is history");
|
||||
assert_eq!(
|
||||
child.foreground_activity(),
|
||||
acp_thread::ForegroundActivity::Unconfirmed,
|
||||
"replayed activity is history"
|
||||
);
|
||||
} else {
|
||||
// Without replay, updates sent before the response are current.
|
||||
assert!(association.can_cancel);
|
||||
assert_eq!(
|
||||
child.foreground_activity(),
|
||||
acp_thread::ForegroundActivity::Running
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
if replay_history {
|
||||
announce_running_child();
|
||||
harness.drain_updates(cx).await;
|
||||
child.read_with(cx, |child, _| {
|
||||
assert!(child.subagent_association().expect("subagent").can_cancel);
|
||||
assert_eq!(
|
||||
child.foreground_activity(),
|
||||
acp_thread::ForegroundActivity::Running
|
||||
);
|
||||
});
|
||||
}
|
||||
drop((parent, child, finished));
|
||||
cx.run_until_parked();
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn v2_subagent_updates_route_and_patch_child_sessions(cx: &mut TestAppContext) {
|
||||
use acp_thread::{ThreadStatus, subagents::SubagentAssociation};
|
||||
|
||||
Reference in New Issue
Block a user