vtcode_core/core/agent/runtime/
steering.rs1use super::*;
2
3pub struct RuntimeSteering {
5 steering_receiver: Option<UnboundedReceiver<SteeringMessage>>,
6 pub(super) queued_follow_up_inputs: VecDeque<QueuedFollowUpIntent>,
7 overflow_follow_up_inputs: VecDeque<QueuedFollowUpIntent>,
11 pub(super) in_flight_follow_up_intents: VecDeque<QueuedFollowUpIntent>,
12 applied_follow_up_intent_ids: VecDeque<String>,
13}
14
15impl Default for RuntimeSteering {
16 fn default() -> Self {
17 Self::new(None)
18 }
19}
20
21impl RuntimeSteering {
22 pub(super) fn new(steering_receiver: Option<UnboundedReceiver<SteeringMessage>>) -> Self {
23 Self {
24 steering_receiver,
25 queued_follow_up_inputs: VecDeque::new(),
26 overflow_follow_up_inputs: VecDeque::new(),
27 in_flight_follow_up_intents: VecDeque::new(),
28 applied_follow_up_intent_ids: VecDeque::new(),
29 }
30 }
31
32 pub fn set_receiver(&mut self, receiver: Option<UnboundedReceiver<SteeringMessage>>) {
34 self.steering_receiver = receiver;
35 }
36
37 pub fn take_receiver(&mut self) -> Option<UnboundedReceiver<SteeringMessage>> {
39 self.steering_receiver.take()
40 }
41
42 #[must_use]
43 pub fn has_pending_follow_up_inputs(&self) -> bool {
44 !self.queued_follow_up_inputs.is_empty() || !self.overflow_follow_up_inputs.is_empty()
45 }
46
47 pub fn pop_follow_up_input(&mut self) -> Option<String> {
49 self.pop_follow_up_intent().map(|intent| intent.into_parts().1)
50 }
51
52 pub fn pop_follow_up_intent(&mut self) -> Option<QueuedFollowUpIntent> {
58 self.drain_overflow_into_primary();
59 self.queued_follow_up_inputs.pop_front()
60 }
61
62 fn drain_overflow_into_primary(&mut self) {
64 while self.queued_follow_up_inputs.len() < MAX_QUEUED_FOLLOW_UP_INTENTS
65 && let Some(intent) = self.overflow_follow_up_inputs.pop_front()
66 {
67 self.queued_follow_up_inputs.push_back(intent);
68 }
69 }
70
71 pub fn queue_follow_up_input(&mut self, input: String) -> bool {
74 match self.try_queue_follow_up_input(input) {
75 Ok(()) => true,
76 Err(error) => {
77 tracing::warn!(%error, "Rejected follow-up steering intent");
78 false
79 }
80 }
81 }
82
83 pub fn try_queue_follow_up_input(&mut self, input: String) -> Result<(), FollowUpQueueFull> {
85 self.try_queue_follow_up_intent(QueuedFollowUpIntent::new(input))
86 }
87
88 pub fn try_queue_synthetic_follow_up_input(&mut self, input: String) -> Result<(), FollowUpQueueFull> {
90 self.try_queue_follow_up_intent(QueuedFollowUpIntent::new_with_provenance(input, FollowUpProvenance::Synthetic))
91 }
92
93 pub fn try_queue_follow_up_intent(&mut self, intent: QueuedFollowUpIntent) -> Result<(), FollowUpQueueFull> {
99 if self.queued_follow_up_inputs.len() + self.in_flight_follow_up_intents.len() < MAX_QUEUED_FOLLOW_UP_INTENTS {
100 self.queued_follow_up_inputs.push_back(intent);
101 return Ok(());
102 }
103 if self.overflow_follow_up_inputs.len() < MAX_OVERFLOW_FOLLOW_UP_INTENTS {
104 self.overflow_follow_up_inputs.push_back(intent);
105 return Ok(());
106 }
107 Err(FollowUpQueueFull {
108 capacity: MAX_QUEUED_FOLLOW_UP_INTENTS + MAX_OVERFLOW_FOLLOW_UP_INTENTS,
109 })
110 }
111
112 pub fn acknowledge_follow_up_intent(&mut self, intent_id: impl Into<String>) {
114 if self.applied_follow_up_intent_ids.len() >= MAX_APPLIED_FOLLOW_UP_INTENT_IDS {
115 self.applied_follow_up_intent_ids.pop_front();
116 }
117 self.applied_follow_up_intent_ids.push_back(intent_id.into());
118 }
119
120 #[must_use]
121 pub fn applied_follow_up_intent_ids(&self) -> &VecDeque<String> {
122 &self.applied_follow_up_intent_ids
123 }
124
125 #[must_use]
129 pub fn pending_follow_up_intents(&self) -> &VecDeque<QueuedFollowUpIntent> {
130 &self.queued_follow_up_inputs
131 }
132
133 #[must_use]
137 pub fn pending_follow_up_intents_snapshot(&self) -> Vec<QueuedFollowUpIntent> {
138 self.queued_follow_up_inputs
139 .iter()
140 .chain(self.overflow_follow_up_inputs.iter())
141 .chain(self.in_flight_follow_up_intents.iter())
142 .cloned()
143 .collect()
144 }
145
146 pub fn drain_follow_up_intents_to_in_flight(&mut self) -> Vec<QueuedFollowUpIntent> {
152 self.drain_overflow_into_primary();
153 let drained: Vec<_> = self.queued_follow_up_inputs.drain(..).collect();
154 self.in_flight_follow_up_intents.extend(drained.iter().cloned());
155 drained
156 }
157
158 pub fn acknowledge_durable_follow_up_intents(&mut self) {
162 while let Some(intent) = self.in_flight_follow_up_intents.pop_front() {
163 self.acknowledge_follow_up_intent(intent.id());
164 }
165 }
166
167 pub fn release_in_flight_follow_up_intents_without_persistence(&mut self) {
172 self.in_flight_follow_up_intents.clear();
173 }
174
175 pub fn clear_pending_follow_up_inputs(&mut self) {
176 self.queued_follow_up_inputs.clear();
177 self.overflow_follow_up_inputs.clear();
178 self.in_flight_follow_up_intents.clear();
179 }
180
181 pub async fn poll_turn_control(&mut self) -> RuntimeControl {
183 self.poll_control().await
184 }
185
186 pub async fn poll_tool_control(&mut self) -> RuntimeControl {
188 self.poll_control().await
189 }
190
191 async fn poll_control(&mut self) -> RuntimeControl {
192 let mut paused = false;
193
194 loop {
195 let Some(receiver) = self.steering_receiver.as_mut() else {
196 return if paused {
197 RuntimeControl::Resumed
198 } else {
199 RuntimeControl::Continue
200 };
201 };
202
203 match receiver.try_recv() {
204 Ok(SteeringMessage::SteerStop) => return RuntimeControl::StopRequested,
205 Ok(SteeringMessage::Pause) => {
206 paused = true;
207 if matches!(self.wait_for_resume().await, RuntimeControl::StopRequested) {
208 return RuntimeControl::StopRequested;
209 }
210 }
211 Ok(SteeringMessage::Resume) => {
212 paused = true;
213 }
214 Ok(SteeringMessage::FollowUpInput(input)) => {
215 if let Err(error) = self.try_queue_follow_up_input(input) {
216 tracing::warn!(%error, "Rejected follow-up steering intent");
217 }
218 }
219 Err(TryRecvError::Empty | TryRecvError::Disconnected) => {
220 return if paused {
221 RuntimeControl::Resumed
222 } else {
223 RuntimeControl::Continue
224 };
225 }
226 }
227 }
228 }
229
230 async fn wait_for_resume(&mut self) -> RuntimeControl {
231 loop {
232 let Some(receiver) = self.steering_receiver.as_mut() else {
233 return RuntimeControl::Continue;
234 };
235
236 match receiver.recv().await {
237 Some(SteeringMessage::Resume) => return RuntimeControl::Continue,
238 Some(SteeringMessage::SteerStop) => return RuntimeControl::StopRequested,
239 Some(SteeringMessage::FollowUpInput(input)) => {
240 if let Err(error) = self.try_queue_follow_up_input(input) {
241 tracing::warn!(%error, "Rejected follow-up steering intent");
242 }
243 }
244 Some(SteeringMessage::Pause) => {}
245 None => return RuntimeControl::Continue,
246 }
247 }
248 }
249}