kcode-k1-chat-codex-state 0.2.0

Deterministic Codex-specific K1 conversation state
Documentation
pub use kcode_k1_chat_codex_codec::{BoxValue, Call};
pub use kcode_k1_chat_state::{ChatBox, ToolCallId};

use kcode_k1_chat_codex_codec::project;
use kcode_k1_chat_state::{ActorState, BoxContent, ProviderCall};
use kcode_k1_codex_adapter::{ShimItem, ShimOutput};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Arrival {
    System(String),
    User(String),
    Attachment,
    Return {
        tool_call_id: ToolCallId,
        result: Result<String, String>,
    },
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Start {
    pub job: u64,
    pub boxes: Vec<BoxValue>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PreparedCall {
    pub tool_call_id: ToolCallId,
    pub name: String,
    pub arguments: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Status {
    Running,
    Quiet,
    Stalled { message: String, restartable: bool },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RestartError {
    NotStalled,
    NotRestartable,
}

struct Round {
    job: u64,
    accepted_call_wave: bool,
}

enum Mode {
    Idle,
    Running(Round),
    Stalled { message: String, restartable: bool },
}

pub struct ConversationState {
    state: ActorState,
    session: [u8; 12],
    sequence: u64,
    submitted: usize,
    mode: Mode,
}

impl ConversationState {
    pub fn new(session: [u8; 12]) -> Self {
        Self {
            state: ActorState::new(false),
            session,
            sequence: 0,
            submitted: 0,
            mode: Mode::Idle,
        }
    }

    pub fn recover(session: [u8; 12], boxes: Vec<ChatBox>, force: bool) -> Result<Self, String> {
        let sequence = recovered_sequence(session, &boxes)?;
        let state = ActorState::recover(boxes, force).map_err(debug)?;
        Ok(Self {
            state,
            session,
            sequence,
            submitted: 0,
            mode: Mode::Idle,
        })
    }

    pub fn boxes(&self) -> &[ChatBox] {
        self.state.boxes()
    }

    pub fn status(&self) -> Status {
        match &self.mode {
            Mode::Running(_) => Status::Running,
            Mode::Idle if self.state.quiet() => Status::Quiet,
            Mode::Idle => Status::Running,
            Mode::Stalled {
                message,
                restartable,
            } => Status::Stalled {
                message: message.clone(),
                restartable: *restartable,
            },
        }
    }

    pub fn accept(&mut self, arrival: Arrival) -> Result<(), String> {
        match arrival {
            Arrival::System(text) => self.state.accept_system(text).map_err(debug),
            Arrival::User(text) => self.state.accept_user(text).map_err(debug),
            Arrival::Attachment => self.state.accept_attachment().map_err(debug),
            Arrival::Return {
                tool_call_id,
                result,
            } => self
                .state
                .accept_async_return(tool_call_id, result)
                .map_err(debug),
        }
    }

    pub fn begin(&mut self) -> Result<Option<Start>, String> {
        if !matches!(self.mode, Mode::Idle) {
            return Ok(None);
        }
        let Some(start) = self.state.begin_inference().map_err(debug)? else {
            return Ok(None);
        };
        let boxes = self.state.boxes();
        let projected = boxes[self.submitted..].iter().map(project).collect();
        self.submitted = boxes.len();
        self.mode = Mode::Running(Round {
            job: start.job,
            accepted_call_wave: false,
        });
        Ok(Some(Start {
            job: start.job,
            boxes: projected,
        }))
    }

    pub fn prepare_stage(
        &mut self,
        job: u64,
        text: String,
        values: Vec<BoxValue>,
    ) -> Result<Vec<PreparedCall>, String> {
        let calls = values
            .into_iter()
            .map(|value| match value {
                BoxValue::Call(Ok(call)) => Ok(call),
                _ => Err("stage contains a malformed tool call".to_owned()),
            })
            .collect::<Result<Vec<_>, _>>()?;
        match &self.mode {
            Mode::Running(round) if round.job == job => {}
            _ => return Err("stale Codex inference stage".to_owned()),
        }
        let mut sequence = self.sequence;
        let prepared = calls
            .iter()
            .map(|call| {
                sequence = sequence
                    .checked_add(1)
                    .ok_or_else(|| "ToolCallId space was exhausted".to_owned())?;
                Ok(PreparedCall {
                    tool_call_id: ToolCallId::new(self.session, sequence),
                    name: call.name.clone(),
                    arguments: call.arguments.clone(),
                })
            })
            .collect::<Result<Vec<_>, String>>()?;
        let provider_calls = prepared
            .iter()
            .map(|call| ProviderCall {
                tool_call_id: call.tool_call_id,
                name: call.name.clone(),
                arguments: call.arguments.clone(),
            })
            .collect();
        self.state
            .append_stage(job, text, provider_calls)
            .map_err(debug)?;
        self.sequence = sequence;
        if !prepared.is_empty()
            && let Mode::Running(round) = &mut self.mode
            && round.job == job
        {
            round.accepted_call_wave = true;
        }
        Ok(prepared)
    }

    pub fn complete(&mut self, job: u64, output: ShimOutput<BoxValue>) -> Result<(), String> {
        let round = self.take_round(job)?;
        let mut text = String::new();
        for item in output.items {
            match item {
                ShimItem::Text(value) => text.push_str(&value),
                ShimItem::Box(_) => {
                    let message = "terminal Codex output contains a box".to_owned();
                    self.preserve(round, message.clone(), false);
                    return Err(message);
                }
            }
        }
        if let Err(error) = self.state.complete_inference(job, text).map_err(debug) {
            self.preserve(round, error.clone(), false);
            return Err(error);
        }
        self.mode = Mode::Idle;
        Ok(())
    }

    pub fn fail(&mut self, job: u64, message: String, restartable_before_launch: bool) {
        if let Ok(round) = self.take_round(job) {
            self.preserve(round, message, restartable_before_launch);
        }
    }

    pub fn restart(&mut self) -> Result<(), RestartError> {
        match self.mode {
            Mode::Stalled {
                restartable: true, ..
            } => {}
            Mode::Stalled { .. } => return Err(RestartError::NotRestartable),
            _ => return Err(RestartError::NotStalled),
        }
        self.state
            .restart()
            .map_err(|_| RestartError::NotRestartable)?;
        self.submitted = 0;
        self.mode = Mode::Idle;
        Ok(())
    }

    fn take_round(&mut self, job: u64) -> Result<Round, String> {
        let mode = std::mem::replace(&mut self.mode, Mode::Idle);
        match mode {
            Mode::Running(round) if round.job == job => Ok(round),
            other => {
                self.mode = other;
                Err("stale Codex inference completion".to_owned())
            }
        }
    }

    fn preserve(&mut self, round: Round, message: String, restartable_before_launch: bool) {
        if round.accepted_call_wave {
            let _ = self.state.complete_inference(round.job, String::new());
            let _ = self.state.halt(message.clone());
            self.mode = Mode::Stalled {
                message,
                restartable: false,
            };
        } else {
            let stalled = self
                .state
                .stall_inference(round.job, message.clone())
                .is_ok();
            self.mode = Mode::Stalled {
                message,
                restartable: stalled && restartable_before_launch,
            };
        }
    }
}

fn recovered_sequence(session: [u8; 12], boxes: &[ChatBox]) -> Result<u64, String> {
    let mut maximum = 0;
    for box_ in boxes {
        let id = match box_.content() {
            BoxContent::KtoolCall { tool_call_id, .. }
            | BoxContent::KtoolReturn { tool_call_id, .. } => tool_call_id,
            _ => continue,
        };
        if id.session() != session {
            return Err("recovered ToolCallId belongs to another session".to_owned());
        }
        maximum = maximum.max(id.sequence());
    }
    Ok(maximum)
}

fn debug(error: impl std::fmt::Debug) -> String {
    format!("{error:?}")
}