mirror of
https://github.com/openai/codex.git
synced 2026-11-01 19:33:35 +00:00
In this PR, I am exploring migrating task kind to an invocation of Codex. The main reason would be getting rid off multiple `ConversationHistory` state and streamlining our context/history management. This approach depends on opening a channel between the sub-codex and codex. This channel is responsible for forwarding `interactive` (`approvals`) and `non-interactive` events. The `task` is responsible for handling those events. This opens the door for implementing `codex as a tool`, replacing `compact` and `review`, and potentially subagents. One consideration is this code is very similar to `app-server` specially in the approval part. If in the future we wanted an interactive `sub-codex` we should consider using `codex-mcp`
116 lines
3.1 KiB
Rust
116 lines
3.1 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::sync::Notify;
|
|
use tokio_util::sync::CancellationToken;
|
|
use tokio_util::task::AbortOnDropHandle;
|
|
|
|
use codex_protocol::models::ResponseInputItem;
|
|
use tokio::sync::oneshot;
|
|
|
|
use crate::codex::TurnContext;
|
|
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) done: Arc<Notify>,
|
|
pub(crate) kind: TaskKind,
|
|
pub(crate) task: Arc<dyn SessionTask>,
|
|
pub(crate) cancellation_token: CancellationToken,
|
|
pub(crate) handle: Arc<AbortOnDropHandle<()>>,
|
|
pub(crate) turn_context: Arc<TurnContext>,
|
|
}
|
|
|
|
impl ActiveTurn {
|
|
pub(crate) fn add_task(&mut self, task: RunningTask) {
|
|
let sub_id = task.turn_context.sub_id.clone();
|
|
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) -> Vec<RunningTask> {
|
|
self.tasks.drain(..).map(|(_, task)| task).collect()
|
|
}
|
|
}
|
|
|
|
/// 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();
|
|
}
|
|
}
|