use std::path::PathBuf;
use std::{fs, io};
use vtcode_commons::VtCodePaths;
use super::{GoalOrchestration, GoalStatus, GoalTracker};
use crate::error::SessionStoreError;
const GOAL_SNAPSHOT_FILE: &str = "goal.json";
fn goal_snapshot_path(session_dir: &std::path::Path) -> Result<PathBuf, SessionStoreError> {
for dir in [session_dir.to_path_buf(), session_dir.join("goal")] {
match fs::symlink_metadata(&dir) {
Ok(metadata) if !metadata.is_dir() => {
return Err(SessionStoreError::io(dir, io::Error::other("goal storage requires a real directory")));
}
Ok(_) => (),
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Err(err) => return Err(SessionStoreError::io(dir, err)),
}
}
Ok(session_dir.join("goal").join(GOAL_SNAPSHOT_FILE))
}
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 active_activation_id(&self) -> Option<uuid::Uuid> {
self.activation_id.filter(|_| self.is_active())
}
pub fn commit(&mut self, staged: Self) -> Result<(), SessionStoreError> {
staged.persist()?;
*self = staged;
Ok(())
}
#[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() {
let previous = self.last_session_tokens_seen.unwrap_or(session_total.max(0));
let current = session_total.max(previous);
self.last_session_tokens_seen = Some(current);
if o.status == GoalStatus::Active {
o.tokens_used_high_water = o.tokens_used_high_water.saturating_add(current - previous);
}
}
}
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 path = goal_snapshot_path(&self.session_dir)?;
match &self.orchestration {
Some(o) => {
let bytes = serde_json::to_vec(o)?;
VtCodePaths::write_private_file_atomic(&path, &bytes)
.map_err(|err| SessionStoreError::io(path, io::Error::other(err)))
}
None => {
vtcode_commons::paths::validate_no_escaping_symlink_ancestors(&path, true)
.map_err(|err| SessionStoreError::io(&path, err))?;
match fs::remove_file(&path) {
Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
Err(err) => Err(SessionStoreError::io(path, err)),
}
}
}
}
pub fn restore(session_dir: PathBuf) -> Result<Self, SessionStoreError> {
let path = goal_snapshot_path(&session_dir)?;
match fs::symlink_metadata(&path) {
Ok(_) => (),
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Self::new(session_dir)),
Err(err) => return Err(SessionStoreError::io(path, err)),
}
let bytes = VtCodePaths::read_file_no_follow(&path)
.map_err(|err| SessionStoreError::io(path, io::Error::other(err)))?;
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,
}
}
}