mirror of
https://github.com/openai/codex.git
synced 2026-04-24 22:54:54 +00:00
Extracting tasks in a module and start abstraction behind a Trait (more to come on this but each task will be tackled in a dedicated PR) The goal was to drop the ActiveTask and to have a (potentially) set of tasks during each turn
116 lines
3.0 KiB
Rust
116 lines
3.0 KiB
Rust
//! Turn-scoped state and active turn metadata scaffolding.
|
|
|
|
use indexmap::IndexMap;
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use tokio::sync::Mutex;
|
|
use tokio::task::AbortHandle;
|
|
|
|
use codex_protocol::models::ResponseInputItem;
|
|
use tokio::sync::oneshot;
|
|
|
|
use crate::protocol::ReviewDecision;
|
|
use crate::tasks::SessionTask;
|
|
|
|
/// Metadata about the currently running turn.
|
|
pub(crate) struct ActiveTurn {
|
|
pub(crate) tasks: IndexMap<String, RunningTask>,
|
|
pub(crate) turn_state: Arc<Mutex<TurnState>>,
|
|
}
|
|
|
|
impl Default for ActiveTurn {
|
|
fn default() -> Self {
|
|
Self {
|
|
tasks: IndexMap::new(),
|
|
turn_state: Arc::new(Mutex::new(TurnState::default())),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub(crate) enum TaskKind {
|
|
Regular,
|
|
Review,
|
|
Compact,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) struct RunningTask {
|
|
pub(crate) handle: AbortHandle,
|
|
pub(crate) kind: TaskKind,
|
|
pub(crate) task: Arc<dyn SessionTask>,
|
|
}
|
|
|
|
impl ActiveTurn {
|
|
pub(crate) fn add_task(&mut self, sub_id: String, task: RunningTask) {
|
|
self.tasks.insert(sub_id, task);
|
|
}
|
|
|
|
pub(crate) fn remove_task(&mut self, sub_id: &str) -> bool {
|
|
self.tasks.swap_remove(sub_id);
|
|
self.tasks.is_empty()
|
|
}
|
|
|
|
pub(crate) fn drain_tasks(&mut self) -> IndexMap<String, RunningTask> {
|
|
std::mem::take(&mut self.tasks)
|
|
}
|
|
}
|
|
|
|
/// Mutable state for a single turn.
|
|
#[derive(Default)]
|
|
pub(crate) struct TurnState {
|
|
pending_approvals: HashMap<String, oneshot::Sender<ReviewDecision>>,
|
|
pending_input: Vec<ResponseInputItem>,
|
|
}
|
|
|
|
impl TurnState {
|
|
pub(crate) fn insert_pending_approval(
|
|
&mut self,
|
|
key: String,
|
|
tx: oneshot::Sender<ReviewDecision>,
|
|
) -> Option<oneshot::Sender<ReviewDecision>> {
|
|
self.pending_approvals.insert(key, tx)
|
|
}
|
|
|
|
pub(crate) fn remove_pending_approval(
|
|
&mut self,
|
|
key: &str,
|
|
) -> Option<oneshot::Sender<ReviewDecision>> {
|
|
self.pending_approvals.remove(key)
|
|
}
|
|
|
|
pub(crate) fn clear_pending(&mut self) {
|
|
self.pending_approvals.clear();
|
|
self.pending_input.clear();
|
|
}
|
|
|
|
pub(crate) fn push_pending_input(&mut self, input: ResponseInputItem) {
|
|
self.pending_input.push(input);
|
|
}
|
|
|
|
pub(crate) fn take_pending_input(&mut self) -> Vec<ResponseInputItem> {
|
|
if self.pending_input.is_empty() {
|
|
Vec::with_capacity(0)
|
|
} else {
|
|
let mut ret = Vec::new();
|
|
std::mem::swap(&mut ret, &mut self.pending_input);
|
|
ret
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ActiveTurn {
|
|
/// Clear any pending approvals and input buffered for the current turn.
|
|
pub(crate) async fn clear_pending(&self) {
|
|
let mut ts = self.turn_state.lock().await;
|
|
ts.clear_pending();
|
|
}
|
|
|
|
/// Best-effort, non-blocking variant for synchronous contexts (Drop/interrupt).
|
|
pub(crate) fn try_clear_pending_sync(&self) {
|
|
if let Ok(mut ts) = self.turn_state.try_lock() {
|
|
ts.clear_pending();
|
|
}
|
|
}
|
|
}
|