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