agent_servers: Add explicit ACP v2 session close (#65815)

- Implement typed v2 `session/close` against the existing owner
registry. Session capability support now enables explicit close;
automatic view release remains detach-only.
- Share one in-flight close per owner. Mark closing synchronously,
reject new prompt/config publication and reopen while closing, and fence
both success and error cleanup by the captured entity ID.
- Reject close during unfinished initialization without cancelling
initialization or sending a close RPC. Preserve typed close rejections,
including their data, and allow an explicit retry. Dropping a close
observer does not cancel the operation or release its routing fence
prematurely.
- Commit successful-close effects and owner retirement after the ordered
foreground barrier, before exposing Stop or close completion. Successful
close can finish waiting Stop observers without inventing an Idle
snapshot, including for unconfirmed activity. Close does not disconnect
receipt observation.
- Keep the original model alive for the pending receipt IDs captured by
that close attempt, independently of view/close-observer lifetime. A
rejected close cannot extend this retention to later submissions. Close
success and a subsequent reopen do not discard late acceptance or
pre-insertion cancellation, mutate a replacement, or hang
accepted-but-un-echoed receipts. Retired projections release on
settlement or transport loss.

Release Notes:

- N/A
This commit is contained in:
Ben Brandt authored and GitHub committed 2026-10-19 10:54:45 +00:00
1 parent da5921d6ba
commit 1f32ec0cea
3 files changed
+1198 -61

No files matched your search

+42
View File
@@ -4235,6 +4235,48 @@ impl AcpThread {
}
}
pub fn prepare_session_close(&mut self, cx: &mut Context<Self>) {
self.cancel_generic_permission_requests(cx);
let pending_receipt_ids = self.submissions.pending_receipt_ids().collect::<Vec<_>>();
if pending_receipt_ids.is_empty() {
return;
}
// Prompt receipts may follow successful close. Pin only those already pending:
// a rejected close must not extend this retention to later submissions.
let (sender, received) = oneshot::channel();
let mut sender = Some(sender);
let subscription = cx.subscribe_self(move |this, event, _cx| {
if matches!(event, AcpThreadEvent::SubmissionUpdated(_))
&& !pending_receipt_ids.iter().any(|id| {
this.submissions
.get(*id)
.is_some_and(|record| matches!(record.state, SubmissionState::Pending))
})
&& let Some(sender) = sender.take()
&& sender.send(()).is_err()
{
log::debug!("Receipt retention observer was dropped");
}
});
let thread = cx.entity();
cx.spawn(async move |_, _| {
let _subscription = subscription;
let _thread = thread;
if let Err(error) = received.await {
log::debug!("Receipt retention ended before settlement: {error}");
}
})
.detach();
}
pub fn handle_session_closed(&mut self, cx: &mut Context<Self>) {
// Close confirms stopping without inventing an Idle snapshot or ending receipts.
self.activity.finish_close();
self.cancel_generic_permission_requests(cx);
self.cancel_outstanding_elicitations(cx);
cx.notify();
}
fn register_submission(
&mut self,
content: Arc<[acp_v2::ContentBlock]>,
+20 -5
View File
@@ -193,6 +193,12 @@ impl SessionSubmissions {
self.records.values().any(SubmissionRecord::is_unsettled)
}
pub(crate) fn pending_receipt_ids(&self) -> impl Iterator<Item = SubmissionId> {
self.records.iter().filter_map(|(id, record)| {
matches!(record.state, SubmissionState::Pending).then_some(*id)
})
}
pub(crate) fn recoverable(&self) -> impl Iterator<Item = (SubmissionId, &SubmissionRecord)> {
self.records.iter().filter_map(|(id, record)| {
(self.receipt_transport.is_some() && record.is_recoverable()).then_some((*id, record))
@@ -410,18 +416,27 @@ impl SessionActivity {
self.duration = self.started_at.map(|started| started.elapsed());
}
if current == ForegroundActivity::Idle {
for waiter in self.cancel_waiters.drain(..) {
if waiter.send(()).is_err() {
log::debug!("Foreground cancellation observer was dropped");
}
self.complete_cancel_waiters();
}
}
fn complete_cancel_waiters(&mut self) {
for waiter in self.cancel_waiters.drain(..) {
if waiter.send(()).is_err() {
log::debug!("Foreground cancellation observer was dropped");
}
}
}
pub(crate) fn finish_close(&mut self) {
self.observation_closed = true;
self.complete_cancel_waiters();
}
pub(crate) fn ensure_observable(&self) -> anyhow::Result<()> {
anyhow::ensure!(
!self.observation_closed,
"Agent connection lost before cancellation was confirmed"
"Session closed or agent connection lost before cancellation was confirmed"
);
Ok(())
}
File diff suppressed because it is too large. Load diff