use std::collections::VecDeque;
use super::{ConfirmationStatus, ForgeSubmitResult, LastAgentDecision, PendingAgentAction};
const HISTORY_CAP: usize = super::CONFIRMATION_HISTORY_CAP;
#[derive(Debug, Default)]
pub struct AgentActionState {
pending: Option<PendingAgentAction>,
pub(super) history: VecDeque<PendingAgentAction>,
forge_completion: Option<tokio::sync::oneshot::Receiver<ForgeSubmitResult>>,
last_decision: Option<LastAgentDecision>,
}
impl AgentActionState {
pub fn pending(&self) -> Option<&PendingAgentAction> {
self.pending.as_ref()
}
pub fn pending_mut(&mut self) -> Option<&mut PendingAgentAction> {
self.pending.as_mut()
}
pub fn last_decision(&self) -> Option<&LastAgentDecision> {
self.last_decision.as_ref()
}
pub fn has_waiting_pending(&self) -> bool {
matches!(
self.pending.as_ref().map(|a| &a.status),
Some(ConfirmationStatus::Pending)
)
}
pub fn find(&self, id: &str) -> Option<&PendingAgentAction> {
if let Some(p) = &self.pending
&& p.id == id
{
return Some(p);
}
self.history.iter().find(|p| p.id == id)
}
pub fn arm_pending(&mut self, action: PendingAgentAction) {
self.pending = Some(action);
}
pub fn take_pending(&mut self) -> Option<PendingAgentAction> {
self.pending.take()
}
pub fn put_back_pending(&mut self, action: PendingAgentAction) {
self.pending = Some(action);
}
pub fn archive_with_decision(
&mut self,
action: PendingAgentAction,
decision: LastAgentDecision,
) {
if self.history.len() >= HISTORY_CAP {
self.history.pop_front();
}
self.history.push_back(action);
self.last_decision = Some(decision);
}
pub fn record_transient_rejection(
&mut self,
rejected: PendingAgentAction,
decision: LastAgentDecision,
) {
self.archive_with_decision(rejected, decision);
}
pub fn set_completion(&mut self, rx: tokio::sync::oneshot::Receiver<ForgeSubmitResult>) {
self.forge_completion = Some(rx);
}
pub fn take_completion(&mut self) -> Option<tokio::sync::oneshot::Receiver<ForgeSubmitResult>> {
self.forge_completion.take()
}
pub fn put_back_completion(&mut self, rx: tokio::sync::oneshot::Receiver<ForgeSubmitResult>) {
self.forge_completion = Some(rx);
}
pub fn history_len(&self) -> usize {
self.history.len()
}
}