Skip to main content

a3s_code_core/agent_api/
run_facade.rs

1use super::*;
2
3impl AgentSession {
4    /// Append a steer as another `user.message` fact and fold the log.
5    pub async fn steer(
6        &self,
7        request: crate::run_control::SteerRequest,
8    ) -> crate::error::Result<crate::run_control::RunControlReceipt> {
9        if self.is_closed() {
10            return Err(crate::error::CodeError::SessionClosed {
11                session_id: self.session_id.clone(),
12            });
13        }
14        RunControl::from_session(self).steer(request).await
15    }
16
17    /// Cooperatively interrupt the active run. Cleanup, checkpoint and
18    /// single-flight settlement still happen through the normal lifecycle.
19    pub async fn interrupt(
20        &self,
21        request: crate::run_control::InterruptRequest,
22    ) -> crate::error::Result<crate::run_control::RunControlReceipt> {
23        if self.is_closed() {
24            return Err(crate::error::CodeError::SessionClosed {
25                session_id: self.session_id.clone(),
26            });
27        }
28        RunControl::from_session(self).interrupt(request).await
29    }
30
31    /// Return the optimistic-concurrency state of the active run's control
32    /// inbox, if a run is currently executing.
33    pub async fn run_control_snapshot(&self) -> Option<crate::run_control::RunControlSnapshot> {
34        RunControl::from_session(self).run_control_snapshot().await
35    }
36
37    /// Cancel a specific run only if it is still the active run.
38    ///
39    /// This is useful for SDK callers that hold a previously observed run ID:
40    /// stale run IDs will not cancel a newer operation.
41    pub async fn cancel_run(&self, run_id: &str) -> bool {
42        RunControl::from_session(self).cancel_run(run_id).await
43    }
44
45    /// Return snapshots for runs recorded by this session.
46    pub async fn runs(&self) -> Vec<crate::run::RunSnapshot> {
47        RunControl::from_session(self).runs().await
48    }
49
50    /// Return a snapshot for a recorded run.
51    pub async fn run_snapshot(&self, run_id: &str) -> Option<crate::run::RunSnapshot> {
52        RunControl::from_session(self).run_snapshot(run_id).await
53    }
54
55    /// Return recorded runtime events for a run.
56    pub async fn run_events(&self, run_id: &str) -> Vec<crate::run::RunEventRecord> {
57        RunControl::from_session(self).run_events(run_id).await
58    }
59
60    /// Return a cursor-based page from the run's retained event window.
61    ///
62    /// `after_sequence` is exclusive. The result is `None` for an unknown run;
63    /// a known run with no retained events returns an empty page. Inspect
64    /// `retention_gap` before treating the page as complete history.
65    pub async fn run_event_page(
66        &self,
67        run_id: &str,
68        after_sequence: Option<usize>,
69        limit: usize,
70    ) -> Option<crate::run::RunEventPage> {
71        RunControl::from_session(self)
72            .run_event_page(run_id, after_sequence, limit)
73            .await
74    }
75
76    /// Return one internally consistent snapshot and event page generation.
77    pub(crate) async fn run_event_observation(
78        &self,
79        run_id: &str,
80        after_sequence: Option<usize>,
81        limit: usize,
82    ) -> Option<crate::run::RunEventObservation> {
83        RunControl::from_session(self)
84            .run_event_observation(run_id, after_sequence, limit)
85            .await
86    }
87
88    /// Return a handle for the currently running operation, if any.
89    pub async fn current_run(&self) -> Option<crate::run::RunHandle> {
90        RunControl::from_session(self).current_run().await
91    }
92
93    /// Return active tool calls observed for the currently running operation.
94    pub async fn active_tools(&self) -> Vec<crate::run::ActiveToolSnapshot> {
95        SessionView::from_session(self).active_tools().await
96    }
97
98    /// Look up a delegated subagent task by id. Returns `None` if no such task
99    /// has been observed in this session.
100    pub async fn subagent_task(
101        &self,
102        task_id: &str,
103    ) -> Option<crate::subagent_task_tracker::SubagentTaskSnapshot> {
104        self.subagent_tasks.get(task_id).await
105    }
106
107    /// Return snapshots of every delegated subagent task observed in this
108    /// session (including completed and failed ones), oldest first.
109    pub async fn subagent_tasks(&self) -> Vec<crate::subagent_task_tracker::SubagentTaskSnapshot> {
110        self.subagent_tasks.list_for_parent(&self.session_id).await
111    }
112
113    /// Return snapshots of subagent tasks still in `Running` state.
114    pub async fn pending_subagent_tasks(
115        &self,
116    ) -> Vec<crate::subagent_task_tracker::SubagentTaskSnapshot> {
117        use crate::subagent_task_tracker::SubagentStatus;
118        self.subagent_tasks
119            .list_for_parent(&self.session_id)
120            .await
121            .into_iter()
122            .filter(|task| task.status == SubagentStatus::Running)
123            .collect()
124    }
125
126    /// Cancel an in-flight delegated subagent task by id. Returns `true`
127    /// when a cancellation token was found and fired, `false` when the
128    /// task id is unknown or the task has already finished. The eventual
129    /// `SubagentEnd` from the cancelled child loop won't downgrade the
130    /// terminal status — it stays `Cancelled`.
131    pub async fn cancel_subagent_task(&self, task_id: &str) -> bool {
132        self.subagent_tasks.cancel(task_id).await
133    }
134
135    /// Return a shared handle to the session's subagent task tracker.
136    ///
137    /// Advanced: embedders implementing a custom subagent execution path
138    /// (i.e. spawning child loops outside the built-in `task` tool) can use
139    /// this to register cancellation tokens and feed `AgentEvent`s into the
140    /// tracker so the standard
141    /// [`subagent_task`](Self::subagent_task) / [`pending_subagent_tasks`](Self::pending_subagent_tasks) /
142    /// [`cancel_subagent_task`](Self::cancel_subagent_task) APIs and
143    /// [`close`](Self::close) keep working uniformly across execution paths.
144    pub fn subagent_tracker(
145        &self,
146    ) -> Arc<crate::subagent_task_tracker::InMemorySubagentTaskTracker> {
147        Arc::clone(&self.subagent_tasks)
148    }
149}