vtcode_core/core/agent/runtime/
steering.rs1use super::*;
2use crate::core::agent::steering::{SteerOutcome, SteerRejection};
3
4pub struct RuntimeSteering {
6 steering_receiver: Option<UnboundedReceiver<SteeringMessage>>,
7 pub(super) queued_follow_up_inputs: VecDeque<QueuedFollowUpIntent>,
8 overflow_follow_up_inputs: VecDeque<QueuedFollowUpIntent>,
12 recovered_follow_up: Option<QueuedFollowUpIntent>,
13 pub(super) in_flight_follow_up_intents: VecDeque<QueuedFollowUpIntent>,
14 applied_follow_up_intent_ids: VecDeque<String>,
15 admission_closed: bool,
19 cancellation_precedence: bool,
21}
22
23impl Default for RuntimeSteering {
24 fn default() -> Self {
25 Self::new(None)
26 }
27}
28
29impl RuntimeSteering {
30 pub(super) fn new(steering_receiver: Option<UnboundedReceiver<SteeringMessage>>) -> Self {
31 Self {
32 steering_receiver,
33 queued_follow_up_inputs: VecDeque::new(),
34 overflow_follow_up_inputs: VecDeque::new(),
35 recovered_follow_up: None,
36 in_flight_follow_up_intents: VecDeque::new(),
37 applied_follow_up_intent_ids: VecDeque::new(),
38 admission_closed: false,
39 cancellation_precedence: false,
40 }
41 }
42
43 pub fn set_receiver(&mut self, receiver: Option<UnboundedReceiver<SteeringMessage>>) {
45 self.steering_receiver = receiver;
46 }
47
48 pub fn take_receiver(&mut self) -> Option<UnboundedReceiver<SteeringMessage>> {
50 self.steering_receiver.take()
51 }
52
53 pub fn open_admission(&mut self) {
55 self.admission_closed = false;
56 self.cancellation_precedence = false;
57 }
58
59 pub fn close_admission(&mut self) {
62 self.admission_closed = true;
63 }
64
65 #[must_use]
66 pub const fn admission_closed(&self) -> bool {
67 self.admission_closed
68 }
69
70 pub fn set_cancellation_precedence(&mut self, value: bool) {
73 self.cancellation_precedence = value;
74 }
75
76 pub fn drain_rejected_steers(&mut self) {
80 let Some(mut receiver) = self.steering_receiver.take() else {
81 return;
82 };
83 while let Ok(message) = receiver.try_recv() {
84 if let SteeringMessage::FollowUpInput(steer) = message {
85 steer.reject(SteerRejection::TurnClosing);
86 }
87 }
88 self.steering_receiver = Some(receiver);
89 }
90
91 pub fn admit_follow_up_steer(&mut self, steer: FollowUpSteer) -> Option<QueuedFollowUpIntent> {
99 let FollowUpSteer { text, mut receipt } = steer;
100 let reject = |reason: SteerRejection, receipt: &mut Option<tokio::sync::oneshot::Sender<SteerOutcome>>| {
101 if let Some(receipt) = receipt.take() {
102 let _ = receipt.send(SteerOutcome::Rejected { reason });
103 }
104 None
105 };
106 if self.admission_closed {
107 return reject(SteerRejection::TurnClosing, &mut receipt);
108 }
109 if self.cancellation_precedence {
110 return reject(SteerRejection::CancellationPrecedence, &mut receipt);
111 }
112 let intent = QueuedFollowUpIntent::new(text);
113 if let Err(error) = self.try_queue_follow_up_intent(intent.clone()) {
114 tracing::warn!(%error, "Rejected live steer: follow-up queue full");
115 return reject(SteerRejection::QueueFull, &mut receipt);
116 }
117 if let Some(receipt) = receipt {
118 let _ = receipt.send(SteerOutcome::Accepted { intent_id: intent.id().to_string() });
119 }
120 Some(intent)
121 }
122
123 #[must_use]
124 pub fn has_pending_follow_up_inputs(&self) -> bool {
125 !self.queued_follow_up_inputs.is_empty()
126 || !self.overflow_follow_up_inputs.is_empty()
127 || self.recovered_follow_up.is_some()
128 }
129
130 pub fn pop_follow_up_input(&mut self) -> Option<String> {
132 self.pop_follow_up_intent().map(|intent| intent.into_parts().1)
133 }
134
135 pub fn pop_follow_up_intent(&mut self) -> Option<QueuedFollowUpIntent> {
141 self.drain_overflow_into_primary();
142 self.queued_follow_up_inputs
143 .pop_front()
144 .or_else(|| self.recovered_follow_up.take())
145 }
146
147 fn drain_overflow_into_primary(&mut self) {
149 while self.queued_follow_up_inputs.len() < MAX_QUEUED_FOLLOW_UP_INTENTS
150 && let Some(intent) = self.overflow_follow_up_inputs.pop_front()
151 {
152 self.queued_follow_up_inputs.push_back(intent);
153 }
154 if self.overflow_follow_up_inputs.is_empty()
155 && self.queued_follow_up_inputs.len() < MAX_QUEUED_FOLLOW_UP_INTENTS
156 && let Some(intent) = self.recovered_follow_up.take()
157 {
158 self.queued_follow_up_inputs.push_back(intent);
159 }
160 }
161
162 pub fn queue_follow_up_input(&mut self, input: String) -> bool {
165 match self.try_queue_follow_up_input(input) {
166 Ok(()) => true,
167 Err(error) => {
168 tracing::warn!(%error, "Rejected follow-up steering intent");
169 false
170 }
171 }
172 }
173
174 pub fn try_queue_follow_up_input(&mut self, input: String) -> Result<(), FollowUpQueueFull> {
176 self.try_queue_follow_up_intent(QueuedFollowUpIntent::new(input))
177 }
178
179 pub fn try_queue_synthetic_follow_up_input(&mut self, input: String) -> Result<(), FollowUpQueueFull> {
181 self.try_queue_follow_up_intent(QueuedFollowUpIntent::new_with_provenance(input, FollowUpProvenance::Synthetic))
182 }
183
184 pub fn try_queue_follow_up_intent(&mut self, intent: QueuedFollowUpIntent) -> Result<(), FollowUpQueueFull> {
190 while self.queued_follow_up_inputs.len() + self.in_flight_follow_up_intents.len() < MAX_QUEUED_FOLLOW_UP_INTENTS
191 && let Some(older) = self.overflow_follow_up_inputs.pop_front()
192 {
193 self.queued_follow_up_inputs.push_back(older);
194 }
195 if self.overflow_follow_up_inputs.is_empty()
196 && self.queued_follow_up_inputs.len() + self.in_flight_follow_up_intents.len()
197 < MAX_QUEUED_FOLLOW_UP_INTENTS
198 && let Some(recovered) = self.recovered_follow_up.take()
199 {
200 self.queued_follow_up_inputs.push_back(recovered);
201 } else if self.overflow_follow_up_inputs.len() < MAX_OVERFLOW_FOLLOW_UP_INTENTS
202 && let Some(recovered) = self.recovered_follow_up.take()
203 {
204 self.overflow_follow_up_inputs.push_back(recovered);
205 }
206 if self.recovered_follow_up.is_some() {
207 return Err(FollowUpQueueFull {
208 capacity: MAX_QUEUED_FOLLOW_UP_INTENTS + MAX_OVERFLOW_FOLLOW_UP_INTENTS,
209 });
210 }
211 if self.queued_follow_up_inputs.len() + self.in_flight_follow_up_intents.len() < MAX_QUEUED_FOLLOW_UP_INTENTS {
212 self.queued_follow_up_inputs.push_back(intent);
213 return Ok(());
214 }
215 if self.overflow_follow_up_inputs.len() < MAX_OVERFLOW_FOLLOW_UP_INTENTS {
216 self.overflow_follow_up_inputs.push_back(intent);
217 return Ok(());
218 }
219 Err(FollowUpQueueFull {
220 capacity: MAX_QUEUED_FOLLOW_UP_INTENTS + MAX_OVERFLOW_FOLLOW_UP_INTENTS,
221 })
222 }
223
224 pub fn try_queue_recovered_follow_up(&mut self, intent: QueuedFollowUpIntent) -> Result<(), FollowUpQueueFull> {
226 if self.recovered_follow_up.is_some() {
227 return Err(FollowUpQueueFull { capacity: MAX_PENDING_FOLLOW_UP_INTENTS });
228 }
229 match self.try_queue_follow_up_intent(intent.clone()) {
230 Ok(()) => Ok(()),
231 Err(_) => {
232 self.recovered_follow_up = Some(intent);
233 Ok(())
234 }
235 }
236 }
237
238 pub fn acknowledge_follow_up_intent(&mut self, intent_id: impl Into<String>) {
240 if self.applied_follow_up_intent_ids.len() >= MAX_APPLIED_FOLLOW_UP_INTENT_IDS {
241 self.applied_follow_up_intent_ids.pop_front();
242 }
243 self.applied_follow_up_intent_ids.push_back(intent_id.into());
244 }
245
246 #[must_use]
247 pub fn applied_follow_up_intent_ids(&self) -> &VecDeque<String> {
248 &self.applied_follow_up_intent_ids
249 }
250
251 #[must_use]
255 pub fn pending_follow_up_intents(&self) -> &VecDeque<QueuedFollowUpIntent> {
256 &self.queued_follow_up_inputs
257 }
258
259 #[must_use]
263 pub fn pending_follow_up_intents_snapshot(&self) -> Vec<QueuedFollowUpIntent> {
264 self.in_flight_follow_up_intents
265 .iter()
266 .chain(self.queued_follow_up_inputs.iter())
267 .chain(self.overflow_follow_up_inputs.iter())
268 .chain(self.recovered_follow_up.iter())
269 .cloned()
270 .collect()
271 }
272
273 pub fn drain_follow_up_intents_to_in_flight(&mut self) -> Vec<QueuedFollowUpIntent> {
279 self.drain_overflow_into_primary();
280 let drained: Vec<_> = self.queued_follow_up_inputs.drain(..).collect();
281 self.in_flight_follow_up_intents.extend(drained.iter().cloned());
282 drained
283 }
284
285 pub fn acknowledge_durable_follow_up_intents(&mut self) {
289 while let Some(intent) = self.in_flight_follow_up_intents.pop_front() {
290 self.acknowledge_follow_up_intent(intent.id());
291 }
292 }
293
294 pub fn release_in_flight_follow_up_intents_without_persistence(&mut self) {
299 self.in_flight_follow_up_intents.clear();
300 }
301
302 pub fn clear_pending_follow_up_inputs(&mut self) {
303 self.queued_follow_up_inputs.clear();
304 self.overflow_follow_up_inputs.clear();
305 self.recovered_follow_up = None;
306 self.in_flight_follow_up_intents.clear();
307 }
308
309 pub async fn poll_turn_control(&mut self) -> RuntimeControl {
311 self.poll_control().await
312 }
313
314 pub async fn poll_tool_control(&mut self) -> RuntimeControl {
316 self.poll_control().await
317 }
318
319 async fn poll_control(&mut self) -> RuntimeControl {
320 let mut paused = false;
321
322 loop {
323 let Some(receiver) = self.steering_receiver.as_mut() else {
324 return if paused {
325 RuntimeControl::Resumed
326 } else {
327 RuntimeControl::Continue
328 };
329 };
330
331 match receiver.try_recv() {
332 Ok(SteeringMessage::SteerStop) => return RuntimeControl::StopRequested,
333 Ok(SteeringMessage::Pause) => {
334 paused = true;
335 if matches!(self.wait_for_resume().await, RuntimeControl::StopRequested) {
336 return RuntimeControl::StopRequested;
337 }
338 }
339 Ok(SteeringMessage::Resume) => {
340 paused = true;
341 }
342 Ok(SteeringMessage::FollowUpInput(steer)) => {
343 let _ = self.admit_follow_up_steer(steer);
344 }
345 Err(TryRecvError::Empty | TryRecvError::Disconnected) => {
346 return if paused {
347 RuntimeControl::Resumed
348 } else {
349 RuntimeControl::Continue
350 };
351 }
352 }
353 }
354 }
355
356 async fn wait_for_resume(&mut self) -> RuntimeControl {
357 loop {
358 let Some(receiver) = self.steering_receiver.as_mut() else {
359 return RuntimeControl::Continue;
360 };
361
362 match receiver.recv().await {
363 Some(SteeringMessage::Resume) => return RuntimeControl::Continue,
364 Some(SteeringMessage::SteerStop) => return RuntimeControl::StopRequested,
365 Some(SteeringMessage::FollowUpInput(steer)) => {
366 let _ = self.admit_follow_up_steer(steer);
367 }
368 Some(SteeringMessage::Pause) => {}
369 None => return RuntimeControl::Continue,
370 }
371 }
372 }
373}