use std::path::PathBuf;
use super::{GoalOrchestration, GoalStatus, GoalTracker};
use crate::error::SessionStoreError;
const GOAL_SNAPSHOT_FILE: &str = "goal.json";
impl GoalStatus {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Active => "active",
Self::UserPaused => "user_paused",
Self::BackOffPaused => "back_off_paused",
Self::NoProgressPaused => "no_progress_paused",
Self::InfraPaused => "infra_paused",
Self::Blocked => "blocked",
Self::BudgetLimited => "budget_limited",
Self::Complete => "complete",
}
}
#[must_use]
pub const fn display_name(self) -> &'static str {
match self {
Self::Active => "Active",
Self::UserPaused => "Paused",
Self::BackOffPaused => "Paused (backing off)",
Self::NoProgressPaused => "Paused (no progress)",
Self::InfraPaused => "Paused (infra error)",
Self::Blocked => "Blocked",
Self::BudgetLimited => "Budget limited",
Self::Complete => "Complete",
}
}
}
impl GoalTracker {
#[must_use]
pub fn has_goal(&self) -> bool {
self.orchestration.is_some()
}
#[must_use]
pub fn goal_id(&self) -> Option<&str> {
self.orchestration.as_ref().map(|o| o.goal_id.as_str())
}
#[must_use]
pub fn pause_message(&self) -> Option<&str> {
self.orchestration.as_ref().and_then(|o| o.pause_message.as_deref())
}
#[must_use]
pub fn elapsed_ms(&self) -> u64 {
let base = self.orchestration.as_ref().map_or(0, |o| o.elapsed_ms);
if self.orchestration.is_some()
&& let Some(since) = self.active_since
{
return base.saturating_add(since.elapsed().as_millis() as u64);
}
base
}
#[must_use]
pub fn tokens_used(&self) -> i64 {
self.orchestration.as_ref().map_or(0, |o| o.tokens_used_high_water)
}
#[must_use]
pub fn auto_continue_turns(&self) -> u8 {
self.orchestration.as_ref().map_or(0, |o| o.auto_continue_turns)
}
pub fn claim_auto_continue_turn(&mut self, max_turns: u8) -> Option<u8> {
let o = self.orchestration.as_mut()?;
if o.auto_continue_turns >= max_turns {
return None;
}
o.auto_continue_turns = o.auto_continue_turns.saturating_add(1);
Some(o.auto_continue_turns)
}
pub fn record_token_observation(&mut self, session_total: i64) {
if let Some(o) = self.orchestration.as_mut() {
o.last_session_tokens_seen = Some(session_total);
let used = session_total.saturating_sub(o.token_baseline).max(0);
o.tokens_used_high_water = o.tokens_used_high_water.max(used);
}
}
pub fn set_token_budget(&mut self, budget: Option<i64>) -> bool {
if let Some(o) = self.orchestration.as_mut() {
o.token_budget = budget;
return true;
}
false
}
pub fn enforce_budget(&mut self) -> bool {
let over = self.orchestration.as_ref().is_some_and(|o| {
o.status == GoalStatus::Active && o.token_budget.is_some_and(|b| o.tokens_used_high_water >= b)
});
if over { self.budget_limit() } else { false }
}
pub fn set_objective(&mut self, objective: &str) -> bool {
if let Some(o) = self.orchestration.as_mut() {
o.objective = objective.trim().to_string();
return true;
}
false
}
pub fn persist(&self) -> Result<(), SessionStoreError> {
let dir = self.goal_dir();
std::fs::create_dir_all(&dir).map_err(|e| SessionStoreError::CreateDir { path: dir.clone(), source: e })?;
let path = dir.join(GOAL_SNAPSHOT_FILE);
match &self.orchestration {
Some(o) => {
let bytes = serde_json::to_vec(o)?;
std::fs::write(&path, bytes).map_err(|e| SessionStoreError::io(path, e))?;
Ok(())
}
None => {
if path.exists() {
std::fs::remove_file(&path).map_err(|e| SessionStoreError::io(path, e))?;
}
Ok(())
}
}
}
pub fn restore(session_dir: PathBuf) -> Result<Self, SessionStoreError> {
let path = session_dir.join("goal").join(GOAL_SNAPSHOT_FILE);
if !path.exists() {
return Ok(Self::new(session_dir));
}
let bytes = std::fs::read(&path).map_err(|e| SessionStoreError::io(path.clone(), e))?;
let snapshot: GoalOrchestration = serde_json::from_slice(&bytes)?;
Ok(Self::from_snapshot(session_dir, snapshot))
}
#[must_use]
pub fn goal_snapshot(&self) -> Option<vtcode_exec_events::goal::GoalSnapshot> {
use vtcode_exec_events::goal::GoalSnapshot;
self.orchestration.as_ref().map(|o| GoalSnapshot {
goal_id: o.goal_id.clone(),
state: vtcode_exec_events::goal::GoalState::from(o.status),
objective: o.objective.clone(),
elapsed_ms: self.elapsed_ms(),
tokens_used: o.tokens_used_high_water,
token_budget: o.token_budget,
pause_message: o.pause_message.clone(),
})
}
#[must_use]
pub fn to_goal_updated_event(&self) -> vtcode_exec_events::ThreadEvent {
use vtcode_exec_events::goal::GoalUpdatedEvent;
let goal = self.goal_snapshot();
vtcode_exec_events::ThreadEvent::GoalUpdated(Box::new(GoalUpdatedEvent { goal }))
}
}
impl From<GoalStatus> for vtcode_exec_events::goal::GoalState {
fn from(status: GoalStatus) -> Self {
use vtcode_exec_events::goal::GoalState;
match status {
GoalStatus::Active => GoalState::Active,
GoalStatus::UserPaused
| GoalStatus::BackOffPaused
| GoalStatus::NoProgressPaused
| GoalStatus::InfraPaused => GoalState::Paused,
GoalStatus::Blocked => GoalState::Blocked,
GoalStatus::BudgetLimited => GoalState::BudgetLimited,
GoalStatus::Complete => GoalState::Complete,
}
}
}