Lightweight coding agent that runs in your terminal
Rust
Add goal persistence foundation (1 / 5) (#18073)
Eric Traut0ee737cea69f
diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.jsonindex a009fc2a55..dbc2316908 100644--- a/codex-rs/core/config.schema.json+++ b/codex-rs/core/config.schema.json@@ -424,6 +424,9 @@"general_analytics": {"type": "boolean"},+ "goals": {+ "type": "boolean"+ },"guardian_approval": {"type": "boolean"},@@ -2616,6 +2619,9 @@"general_analytics": {"type": "boolean"},+ "goals": {+ "type": "boolean"+ },"guardian_approval": {"type": "boolean"},diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rsindex 453b421c0f..6c4e97ab73 100644--- a/codex-rs/features/src/lib.rs+++ b/codex-rs/features/src/lib.rs@@ -185,6 +185,8 @@ pub enum Feature {DefaultModeRequestUserInput,/// Enable automatic review for approval prompts.GuardianApproval,+ /// Enable persisted thread goals and automatic goal continuation.+ Goals,/// Enable collaboration modes (Plan, Default)./// Kept for config backward compatibility; behavior is always collaboration-modes-enabled.CollaborationModes,@@ -928,6 +930,12 @@ pub const FEATURES: &[FeatureSpec] = &[stage: Stage::Stable,default_enabled: true,},+ FeatureSpec {+ id: Feature::Goals,+ key: "goals",+ stage: Stage::UnderDevelopment,+ default_enabled: false,+ },FeatureSpec {id: Feature::CollaborationModes,key: "collaboration_modes",diff --git a/codex-rs/state/migrations/0029_thread_goals.sql b/codex-rs/state/migrations/0029_thread_goals.sqlnew file mode 100644index 0000000000..6c6e86a199--- /dev/null+++ b/codex-rs/state/migrations/0029_thread_goals.sql@@ -0,0 +1,11 @@+CREATE TABLE thread_goals (+ thread_id TEXT PRIMARY KEY NOT NULL REFERENCES threads(id) ON DELETE CASCADE,+ goal_id TEXT NOT NULL,+ objective TEXT NOT NULL,+ status TEXT NOT NULL CHECK(status IN ('active', 'paused', 'budget_limited', 'complete')),+ token_budget INTEGER,+ tokens_used INTEGER NOT NULL DEFAULT 0,+ time_used_seconds INTEGER NOT NULL DEFAULT 0,+ created_at_ms INTEGER NOT NULL,+ updated_at_ms INTEGER NOT NULL+);diff --git a/codex-rs/state/src/lib.rs b/codex-rs/state/src/lib.rsindex 49529f3a33..c3dacae715 100644--- a/codex-rs/state/src/lib.rs+++ b/codex-rs/state/src/lib.rs@@ -44,12 +44,17 @@ pub use model::Stage1JobClaimOutcome;pub use model::Stage1Output;pub use model::Stage1OutputRef;pub use model::Stage1StartupClaimParams;+pub use model::ThreadGoal;+pub use model::ThreadGoalStatus;pub use model::ThreadMetadata;pub use model::ThreadMetadataBuilder;pub use model::ThreadsPage;pub use runtime::DeviceKeyBindingRecord;pub use runtime::RemoteControlEnrollmentRecord;pub use runtime::ThreadFilterOptions;+pub use runtime::ThreadGoalAccountingMode;+pub use runtime::ThreadGoalAccountingOutcome;+pub use runtime::ThreadGoalUpdate;pub use runtime::logs_db_filename;pub use runtime::logs_db_path;pub use runtime::state_db_filename;diff --git a/codex-rs/state/src/model/mod.rs b/codex-rs/state/src/model/mod.rsindex a5f9531aaa..213ae81fea 100644--- a/codex-rs/state/src/model/mod.rs+++ b/codex-rs/state/src/model/mod.rs@@ -3,6 +3,7 @@ mod backfill_state;mod graph;mod log;mod memories;+mod thread_goal;mod thread_metadata;pub use agent_job::AgentJob;@@ -25,6 +26,8 @@ pub use memories::Stage1JobClaimOutcome;pub use memories::Stage1Output;pub use memories::Stage1OutputRef;pub use memories::Stage1StartupClaimParams;+pub use thread_goal::ThreadGoal;+pub use thread_goal::ThreadGoalStatus;pub use thread_metadata::Anchor;pub use thread_metadata::BackfillStats;pub use thread_metadata::ExtractionOutcome;@@ -38,6 +41,7 @@ pub(crate) use agent_job::AgentJobItemRow;pub(crate) use agent_job::AgentJobRow;pub(crate) use memories::Stage1OutputRow;pub(crate) use memories::stage1_output_ref_from_parts;+pub(crate) use thread_goal::ThreadGoalRow;pub(crate) use thread_metadata::ThreadRow;pub(crate) use thread_metadata::anchor_from_item;pub(crate) use thread_metadata::datetime_to_epoch_millis;diff --git a/codex-rs/state/src/model/thread_goal.rs b/codex-rs/state/src/model/thread_goal.rsnew file mode 100644index 0000000000..e943c145f8--- /dev/null+++ b/codex-rs/state/src/model/thread_goal.rs@@ -0,0 +1,109 @@+use anyhow::Result;+use anyhow::anyhow;+use chrono::DateTime;+use chrono::Utc;+use codex_protocol::ThreadId;+use sqlx::Row;+use sqlx::sqlite::SqliteRow;++use super::epoch_millis_to_datetime;++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ThreadGoalStatus {+ Active,+ Paused,+ BudgetLimited,+ Complete,+}++impl ThreadGoalStatus {+ pub fn as_str(self) -> &'static str {+ match self {+ Self::Active => "active",+ Self::Paused => "paused",+ Self::BudgetLimited => "budget_limited",+ Self::Complete => "complete",+ }+ }++ pub fn is_active(self) -> bool {+ self == Self::Active+ }++ pub fn is_terminal(self) -> bool {+ matches!(self, Self::BudgetLimited | Self::Complete)+ }+}++impl TryFrom<&str> for ThreadGoalStatus {+ type Error = anyhow::Error;++ fn try_from(value: &str) -> Result<Self> {+ match value {+ "active" => Ok(Self::Active),+ "paused" => Ok(Self::Paused),+ "budget_limited" => Ok(Self::BudgetLimited),+ "complete" => Ok(Self::Complete),+ other => Err(anyhow!("unknown thread goal status `{other}`")),+ }+ }+}++#[derive(Debug, Clone, PartialEq, Eq)]+pub struct ThreadGoal {+ pub thread_id: ThreadId,+ pub goal_id: String,+ pub objective: String,+ pub status: ThreadGoalStatus,+ pub token_budget: Option<i64>,+ pub tokens_used: i64,+ pub time_used_seconds: i64,+ pub created_at: DateTime<Utc>,+ pub updated_at: DateTime<Utc>,+}++pub(crate) struct ThreadGoalRow {+ pub thread_id: String,+ pub goal_id: String,+ pub objective: String,+ pub status: String,+ pub token_budget: Option<i64>,+ pub tokens_used: i64,+ pub time_used_seconds: i64,+ pub created_at_ms: i64,+ pub updated_at_ms: i64,+}++impl ThreadGoalRow {+ pub(crate) fn try_from_row(row: &SqliteRow) -> Result<Self> {+ Ok(Self {+ thread_id: row.try_get("thread_id")?,+ goal_id: row.try_get("goal_id")?,+ objective: row.try_get("objective")?,+ status: row.try_get("status")?,+ token_budget: row.try_get("token_budget")?,+ tokens_used: row.try_get("tokens_used")?,+ time_used_seconds: row.try_get("time_used_seconds")?,+ created_at_ms: row.try_get("created_at_ms")?,+ updated_at_ms: row.try_get("updated_at_ms")?,+ })+ }+}++impl TryFrom<ThreadGoalRow> for ThreadGoal {+ type Error = anyhow::Error;++ fn try_from(row: ThreadGoalRow) -> Result<Self> {+ Ok(Self {+ thread_id: ThreadId::try_from(row.thread_id)?,+ goal_id: row.goal_id,+ objective: row.objective,+ status: ThreadGoalStatus::try_from(row.status.as_str())?,+ token_budget: row.token_budget,+ tokens_used: row.tokens_used,+ time_used_seconds: row.time_used_seconds,+ created_at: epoch_millis_to_datetime(row.created_at_ms)?,+ updated_at: epoch_millis_to_datetime(row.updated_at_ms)?,+ })+ }+}diff --git a/codex-rs/state/src/runtime.rs b/codex-rs/state/src/runtime.rsindex ec3ce79e82..18f81348a2 100644--- a/codex-rs/state/src/runtime.rs+++ b/codex-rs/state/src/runtime.rs@@ -20,6 +20,7 @@ use crate::apply_rollout_item;use crate::migrations::runtime_logs_migrator;use crate::migrations::runtime_state_migrator;use crate::model::AgentJobRow;+use crate::model::ThreadGoalRow;use crate::model::ThreadRow;use crate::model::anchor_from_item;use crate::model::datetime_to_epoch_millis;@@ -58,6 +59,7 @@ mod backfill;mod device_key;#[cfg(test)]mod device_key_tests;+mod goals;mod logs;mod memories;mod remote_control;@@ -66,6 +68,9 @@ mod test_support;mod threads;pub use device_key::DeviceKeyBindingRecord;+pub use goals::ThreadGoalAccountingMode;+pub use goals::ThreadGoalAccountingOutcome;+pub use goals::ThreadGoalUpdate;pub use remote_control::RemoteControlEnrollmentRecord;pub use threads::ThreadFilterOptions;diff --git a/codex-rs/state/src/runtime/goals.rs b/codex-rs/state/src/runtime/goals.rsnew file mode 100644index 0000000000..56613b7d92--- /dev/null+++ b/codex-rs/state/src/runtime/goals.rs@@ -0,0 +1,1253 @@+use super::*;+use uuid::Uuid;++pub struct ThreadGoalUpdate {+ pub status: Option<crate::ThreadGoalStatus>,+ pub token_budget: Option<Option<i64>>,+ pub expected_goal_id: Option<String>,+}++pub enum ThreadGoalAccountingOutcome {+ Unchanged(Option<crate::ThreadGoal>),+ Updated(crate::ThreadGoal),+}++#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub enum ThreadGoalAccountingMode {+ ActiveStatusOnly,+ ActiveOnly,+ ActiveOrComplete,+ ActiveOrStopped,+}++impl StateRuntime {+ pub async fn get_thread_goal(+ &self,+ thread_id: ThreadId,+ ) -> anyhow::Result<Option<crate::ThreadGoal>> {+ let row = sqlx::query(+ r#"+SELECT+ thread_id,+ goal_id,+ objective,+ status,+ token_budget,+ tokens_used,+ time_used_seconds,+ created_at_ms,+ updated_at_ms+FROM thread_goals+WHERE thread_id = ?+ "#,+ )+ .bind(thread_id.to_string())+ .fetch_optional(self.pool.as_ref())+ .await?;++ row.map(|row| thread_goal_from_row(&row)).transpose()+ }++ pub async fn replace_thread_goal(+ &self,+ thread_id: ThreadId,+ objective: &str,+ status: crate::ThreadGoalStatus,+ token_budget: Option<i64>,+ ) -> anyhow::Result<crate::ThreadGoal> {+ let goal_id = Uuid::new_v4().to_string();+ let now_ms = datetime_to_epoch_millis(Utc::now());+ let status = status_after_budget_limit(status, /*tokens_used*/ 0, token_budget);+ let row = sqlx::query(+ r#"+INSERT INTO thread_goals (+ thread_id,+ goal_id,+ objective,+ status,+ token_budget,+ tokens_used,+ time_used_seconds,+ created_at_ms,+ updated_at_ms+) VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?)+ON CONFLICT(thread_id) DO UPDATE SET+ goal_id = excluded.goal_id,+ objective = excluded.objective,+ status = excluded.status,+ token_budget = excluded.token_budget,+ tokens_used = 0,+ time_used_seconds = 0,+ created_at_ms = excluded.created_at_ms,+ updated_at_ms = excluded.updated_at_ms+RETURNING+ thread_id,+ goal_id,+ objective,+ status,+ token_budget,+ tokens_used,+ time_used_seconds,+ created_at_ms,+ updated_at_ms+ "#,+ )+ .bind(thread_id.to_string())+ .bind(goal_id)+ .bind(objective)+ .bind(status.as_str())+ .bind(token_budget)+ .bind(now_ms)+ .bind(now_ms)+ .fetch_one(self.pool.as_ref())+ .await?;++ thread_goal_from_row(&row)+ }++ pub async fn insert_thread_goal(+ &self,+ thread_id: ThreadId,+ objective: &str,+ status: crate::ThreadGoalStatus,+ token_budget: Option<i64>,+ ) -> anyhow::Result<Option<crate::ThreadGoal>> {+ let goal_id = Uuid::new_v4().to_string();+ let now_ms = datetime_to_epoch_millis(Utc::now());+ let status = status_after_budget_limit(status, /*tokens_used*/ 0, token_budget);+ let row = sqlx::query(+ r#"+INSERT INTO thread_goals (+ thread_id,+ goal_id,+ objective,+ status,+ token_budget,+ tokens_used,+ time_used_seconds,+ created_at_ms,+ updated_at_ms+) VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?)+ON CONFLICT(thread_id) DO NOTHING+RETURNING+ thread_id,+ goal_id,+ objective,+ status,+ token_budget,+ tokens_used,+ time_used_seconds,+ created_at_ms,+ updated_at_ms+ "#,+ )+ .bind(thread_id.to_string())+ .bind(goal_id)+ .bind(objective)+ .bind(status.as_str())+ .bind(token_budget)+ .bind(now_ms)+ .bind(now_ms)+ .fetch_optional(self.pool.as_ref())+ .await?;++ row.map(|row| thread_goal_from_row(&row)).transpose()+ }++ pub async fn update_thread_goal(+ &self,+ thread_id: ThreadId,+ update: ThreadGoalUpdate,+ ) -> anyhow::Result<Option<crate::ThreadGoal>> {+ let ThreadGoalUpdate {+ status,+ token_budget,+ expected_goal_id,+ } = update;+ let expected_goal_id = expected_goal_id.as_deref();+ let now_ms = datetime_to_epoch_millis(Utc::now());+ let result = match (status, token_budget) {+ (Some(status), Some(token_budget)) => {+ sqlx::query(+ r#"+UPDATE thread_goals+SET+ status = CASE+ WHEN status = ? AND ? = ? THEN status+ WHEN ? = 'active' AND ? IS NOT NULL AND tokens_used >= ? THEN ?+ ELSE ?+ END,+ token_budget = ?,+ updated_at_ms = ?+WHERE thread_id = ?+ AND (? IS NULL OR goal_id = ?)+ "#,+ )+ .bind(crate::ThreadGoalStatus::BudgetLimited.as_str())+ .bind(status.as_str())+ .bind(crate::ThreadGoalStatus::Paused.as_str())+ .bind(status.as_str())+ .bind(token_budget)+ .bind(token_budget)+ .bind(crate::ThreadGoalStatus::BudgetLimited.as_str())+ .bind(status.as_str())+ .bind(token_budget)+ .bind(now_ms)+ .bind(thread_id.to_string())+ .bind(expected_goal_id)+ .bind(expected_goal_id)+ .execute(self.pool.as_ref())+ .await?+ }+ (Some(status), None) => {+ sqlx::query(+ r#"+UPDATE thread_goals+SET+ status = CASE+ WHEN status = ? AND ? = ? THEN status+ WHEN ? = 'active' AND token_budget IS NOT NULL AND tokens_used >= token_budget THEN ?+ ELSE ?+ END,+ updated_at_ms = ?+WHERE thread_id = ?+ AND (? IS NULL OR goal_id = ?)+ "#,+ )+ .bind(crate::ThreadGoalStatus::BudgetLimited.as_str())+ .bind(status.as_str())+ .bind(crate::ThreadGoalStatus::Paused.as_str())+ .bind(status.as_str())+ .bind(crate::ThreadGoalStatus::BudgetLimited.as_str())+ .bind(status.as_str())+ .bind(now_ms)+ .bind(thread_id.to_string())+ .bind(expected_goal_id)+ .bind(expected_goal_id)+ .execute(self.pool.as_ref())+ .await?+ }+ (None, Some(token_budget)) => {+ sqlx::query(+ r#"+UPDATE thread_goals+SET+ token_budget = ?,+ status = CASE+ WHEN status = 'active' AND ? IS NOT NULL AND tokens_used >= ? THEN ?+ ELSE status+ END,+ updated_at_ms = ?+WHERE thread_id = ?+ AND (? IS NULL OR goal_id = ?)+ "#,+ )+ .bind(token_budget)+ .bind(token_budget)+ .bind(token_budget)+ .bind(crate::ThreadGoalStatus::BudgetLimited.as_str())+ .bind(now_ms)+ .bind(thread_id.to_string())+ .bind(expected_goal_id)+ .bind(expected_goal_id)+ .execute(self.pool.as_ref())+ .await?+ }+ (None, None) => {+ let goal = self.get_thread_goal(thread_id).await?;+ return Ok(match (goal, expected_goal_id) {+ (Some(goal), Some(expected_goal_id)) if goal.goal_id != expected_goal_id => {+ None+ }+ (goal, _) => goal,+ });+ }+ };++ if result.rows_affected() == 0 {+ return Ok(None);+ }++ self.get_thread_goal(thread_id).await+ }++ pub async fn pause_active_thread_goal(+ &self,+ thread_id: ThreadId,+ ) -> anyhow::Result<Option<crate::ThreadGoal>> {+ let now_ms = datetime_to_epoch_millis(Utc::now());+ let result = sqlx::query(+ r#"+UPDATE thread_goals+SET+ status = ?,+ updated_at_ms = ?+WHERE thread_id = ?+ AND status = 'active'+ "#,+ )+ .bind(crate::ThreadGoalStatus::Paused.as_str())+ .bind(now_ms)+ .bind(thread_id.to_string())+ .execute(self.pool.as_ref())+ .await?;++ if result.rows_affected() == 0 {+ return Ok(None);+ }++ self.get_thread_goal(thread_id).await+ }++ pub async fn delete_thread_goal(&self, thread_id: ThreadId) -> anyhow::Result<bool> {+ let result = sqlx::query(+ r#"+DELETE FROM thread_goals+WHERE thread_id = ?+ "#,+ )+ .bind(thread_id.to_string())+ .execute(self.pool.as_ref())+ .await?;++ Ok(result.rows_affected() > 0)+ }++ pub async fn account_thread_goal_usage(+ &self,+ thread_id: ThreadId,+ time_delta_seconds: i64,+ token_delta: i64,+ mode: ThreadGoalAccountingMode,+ expected_goal_id: Option<&str>,+ ) -> anyhow::Result<ThreadGoalAccountingOutcome> {+ let time_delta_seconds = time_delta_seconds.max(0);+ let token_delta = token_delta.max(0);+ if time_delta_seconds == 0 && token_delta == 0 {+ return Ok(ThreadGoalAccountingOutcome::Unchanged(+ self.get_thread_goal(thread_id).await?,+ ));+ }++ let now_ms = datetime_to_epoch_millis(Utc::now());+ let status_filter = match mode {+ ThreadGoalAccountingMode::ActiveStatusOnly => "status = 'active'",+ ThreadGoalAccountingMode::ActiveOnly => "status IN ('active', 'budget_limited')",+ ThreadGoalAccountingMode::ActiveOrComplete => {+ "status IN ('active', 'budget_limited', 'complete')"+ }+ ThreadGoalAccountingMode::ActiveOrStopped => {+ "status IN ('active', 'paused', 'budget_limited')"+ }+ };+ let budget_limit_status_filter = match mode {+ ThreadGoalAccountingMode::ActiveStatusOnly+ | ThreadGoalAccountingMode::ActiveOnly+ | ThreadGoalAccountingMode::ActiveOrComplete => "status = 'active'",+ ThreadGoalAccountingMode::ActiveOrStopped => {+ "status IN ('active', 'paused', 'budget_limited')"+ }+ };+ let goal_id_filter = if expected_goal_id.is_some() {+ "goal_id = ?"+ } else {+ "1 = 1"+ };+ let query = format!(+ r#"+UPDATE thread_goals+SET+ time_used_seconds = time_used_seconds + ?,+ tokens_used = tokens_used + ?,+ status = CASE+ WHEN {budget_limit_status_filter} AND token_budget IS NOT NULL AND tokens_used + ? >= token_budget+ THEN ?+ ELSE status+ END,+ updated_at_ms = ?+WHERE thread_id = ?+ AND {status_filter}+ AND {goal_id_filter}+RETURNING+ thread_id,+ goal_id,+ objective,+ status,+ token_budget,+ tokens_used,+ time_used_seconds,+ created_at_ms,+ updated_at_ms+ "#,+ );++ let mut query = sqlx::query(&query)+ .bind(time_delta_seconds)+ .bind(token_delta)+ .bind(token_delta)+ .bind(crate::ThreadGoalStatus::BudgetLimited.as_str())+ .bind(now_ms)+ .bind(thread_id.to_string());+ if let Some(expected_goal_id) = expected_goal_id {+ query = query.bind(expected_goal_id);+ }++ let row = query.fetch_optional(self.pool.as_ref()).await?;++ let Some(row) = row else {+ return Ok(ThreadGoalAccountingOutcome::Unchanged(+ self.get_thread_goal(thread_id).await?,+ ));+ };++ let updated = thread_goal_from_row(&row)?;+ Ok(ThreadGoalAccountingOutcome::Updated(updated))+ }+}++fn thread_goal_from_row(row: &sqlx::sqlite::SqliteRow) -> anyhow::Result<crate::ThreadGoal> {+ ThreadGoalRow::try_from_row(row).and_then(crate::ThreadGoal::try_from)+}++fn status_after_budget_limit(+ status: crate::ThreadGoalStatus,+ tokens_used: i64,+ token_budget: Option<i64>,+) -> crate::ThreadGoalStatus {+ if status == crate::ThreadGoalStatus::Active+ && token_budget.is_some_and(|budget| tokens_used >= budget)+ {+ crate::ThreadGoalStatus::BudgetLimited+ } else {+ status+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::runtime::test_support::test_thread_metadata;+ use crate::runtime::test_support::unique_temp_dir;+ use pretty_assertions::assert_eq;++ async fn test_runtime() -> std::sync::Arc<StateRuntime> {+ StateRuntime::init(unique_temp_dir(), "test-provider".to_string())+ .await+ .expect("state db should initialize")+ }++ fn test_thread_id() -> ThreadId {+ ThreadId::from_string("00000000-0000-0000-0000-000000000123").expect("valid thread id")+ }++ async fn upsert_test_thread(runtime: &StateRuntime, thread_id: ThreadId) {+ let metadata = test_thread_metadata(+ runtime.codex_home(),+ thread_id,+ runtime.codex_home().join("workspace"),+ );+ runtime+ .upsert_thread(&metadata)+ .await+ .expect("test thread should be upserted");+ }++ #[tokio::test]+ async fn replace_update_and_get_thread_goal() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;++ let goal = runtime+ .replace_thread_goal(+ thread_id,+ "optimize the benchmark",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100_000),+ )+ .await+ .expect("goal replacement should succeed");+ assert_eq!(+ Some(goal.clone()),+ runtime.get_thread_goal(thread_id).await.unwrap()+ );++ let updated = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Paused),+ token_budget: Some(Some(200_000)),+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("goal should exist");+ let expected = crate::ThreadGoal {+ status: crate::ThreadGoalStatus::Paused,+ token_budget: Some(200_000),+ updated_at: updated.updated_at,+ ..goal.clone()+ };+ assert_eq!(expected, updated);++ let replaced = runtime+ .replace_thread_goal(+ thread_id,+ "ship the new result",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ None,+ )+ .await+ .expect("goal replacement should succeed");+ assert_eq!("ship the new result", replaced.objective);+ assert_eq!(crate::ThreadGoalStatus::Active, replaced.status);+ assert_eq!(None, replaced.token_budget);+ assert_eq!(0, replaced.tokens_used);+ assert_eq!(0, replaced.time_used_seconds);++ assert!(runtime.delete_thread_goal(thread_id).await.unwrap());+ assert_eq!(None, runtime.get_thread_goal(thread_id).await.unwrap());+ assert!(!runtime.delete_thread_goal(thread_id).await.unwrap());+ }++ #[tokio::test]+ async fn replace_thread_goal_applies_budget_limit_immediately() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;++ let replaced = runtime+ .replace_thread_goal(+ thread_id,+ "stay within budget",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(0),+ )+ .await+ .expect("goal replacement should succeed");++ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, replaced.status);+ assert_eq!(Some(0), replaced.token_budget);+ assert_eq!(0, replaced.tokens_used);+ assert_eq!(0, replaced.time_used_seconds);+ }++ #[tokio::test]+ async fn insert_thread_goal_does_not_replace_existing_goal() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;++ let inserted = runtime+ .insert_thread_goal(+ thread_id,+ "optimize the benchmark",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100_000),+ )+ .await+ .expect("goal insertion should succeed")+ .expect("goal should be inserted");++ let duplicate = runtime+ .insert_thread_goal(+ thread_id,+ "replace the benchmark",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(200_000),+ )+ .await+ .expect("duplicate insert should not fail");++ assert_eq!(None, duplicate);+ assert_eq!(+ Some(inserted),+ runtime.get_thread_goal(thread_id).await.unwrap()+ );+ }++ #[tokio::test]+ async fn insert_thread_goal_applies_budget_limit_immediately() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;++ let inserted = runtime+ .insert_thread_goal(+ thread_id,+ "stay within budget",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(0),+ )+ .await+ .expect("goal insertion should succeed")+ .expect("goal should be inserted");++ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, inserted.status);+ assert_eq!(Some(0), inserted.token_budget);+ assert_eq!(0, inserted.tokens_used);+ assert_eq!(0, inserted.time_used_seconds);+ }++ #[tokio::test]+ async fn update_thread_goal_ignores_replaced_goal_version() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;++ let original = runtime+ .replace_thread_goal(+ thread_id,+ "old objective",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100),+ )+ .await+ .expect("goal replacement should succeed");+ let replacement = runtime+ .replace_thread_goal(+ thread_id,+ "new objective",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(10),+ )+ .await+ .expect("goal replacement should succeed");++ let stale_update = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Complete),+ token_budget: None,+ expected_goal_id: Some(original.goal_id),+ },+ )+ .await+ .expect("goal update should succeed");++ assert_eq!(None, stale_update);+ assert_eq!(+ Some(replacement.clone()),+ runtime+ .get_thread_goal(thread_id)+ .await+ .expect("goal read should succeed")+ );++ let fresh_update = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Complete),+ token_budget: None,+ expected_goal_id: Some(replacement.goal_id),+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("fresh update should match the replacement goal");+ assert_eq!(crate::ThreadGoalStatus::Complete, fresh_update.status);+ }++ #[tokio::test]+ async fn usage_accounting_ignores_replaced_goal_version() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;++ let original = runtime+ .replace_thread_goal(+ thread_id,+ "old objective",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100),+ )+ .await+ .expect("goal replacement should succeed");+ let replacement = runtime+ .replace_thread_goal(+ thread_id,+ "new objective",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(10),+ )+ .await+ .expect("goal replacement should succeed");++ let outcome = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 5,+ /*token_delta*/ 5,+ ThreadGoalAccountingMode::ActiveOnly,+ Some(original.goal_id.as_str()),+ )+ .await+ .expect("usage accounting should succeed");++ let ThreadGoalAccountingOutcome::Unchanged(Some(goal)) = outcome else {+ panic!("stale goal version should not be updated");+ };+ assert_ne!(replacement.goal_id, original.goal_id);+ assert_eq!(replacement.created_at, goal.created_at);+ assert_eq!("new objective", goal.objective);+ assert_eq!(0, goal.tokens_used);+ assert_eq!(0, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn concurrent_partial_updates_preserve_independent_fields() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "optimize the benchmark",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100_000),+ )+ .await+ .expect("goal replacement should succeed");++ let status_update = runtime.update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Paused),+ token_budget: None,+ expected_goal_id: None,+ },+ );+ let budget_update = runtime.update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: None,+ token_budget: Some(Some(200_000)),+ expected_goal_id: None,+ },+ );+ let (status_update, budget_update) = tokio::join!(status_update, budget_update);+ status_update.expect("status update should succeed");+ budget_update.expect("budget update should succeed");++ let goal = runtime+ .get_thread_goal(thread_id)+ .await+ .expect("goal read should succeed")+ .expect("goal should exist");+ assert_eq!(crate::ThreadGoalStatus::Paused, goal.status);+ assert_eq!(Some(200_000), goal.token_budget);+ }++ #[tokio::test]+ async fn pause_active_thread_goal_does_not_clobber_terminal_status() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ let goal = runtime+ .replace_thread_goal(+ thread_id,+ "optimize the benchmark",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100_000),+ )+ .await+ .expect("goal replacement should succeed");++ let paused = runtime+ .pause_active_thread_goal(thread_id)+ .await+ .expect("active pause should succeed")+ .expect("active goal should be paused");+ let expected = crate::ThreadGoal {+ status: crate::ThreadGoalStatus::Paused,+ updated_at: paused.updated_at,+ ..goal+ };+ assert_eq!(expected, paused);++ let complete = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Complete),+ token_budget: None,+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("goal should exist");+ let pause_result = runtime+ .pause_active_thread_goal(thread_id)+ .await+ .expect("terminal pause attempt should succeed");+ assert_eq!(None, pause_result);+ assert_eq!(+ Some(complete),+ runtime+ .get_thread_goal(thread_id)+ .await+ .expect("goal read should succeed")+ );+ }++ #[tokio::test]+ async fn usage_accounting_updates_active_goals_and_accounts_budget_limited_in_flight_usage() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "stay within budget",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(20),+ )+ .await+ .expect("goal replacement should succeed");++ let outcome = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 7,+ /*token_delta*/ 5,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Updated(goal) = outcome else {+ panic!("active goal should be updated");+ };+ assert_eq!(crate::ThreadGoalStatus::Active, goal.status);+ assert_eq!(5, goal.tokens_used);+ assert_eq!(7, goal.time_used_seconds);++ let outcome = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 3,+ /*token_delta*/ 15,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Updated(goal) = outcome else {+ panic!("budget crossing should update the goal");+ };+ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, goal.status);+ assert_eq!(20, goal.tokens_used);+ assert_eq!(10, goal.time_used_seconds);++ let outcome = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 5,+ /*token_delta*/ 5,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Updated(goal) = outcome else {+ panic!("budget-limited goal should still account in-flight active usage");+ };+ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, goal.status);+ assert_eq!(25, goal.tokens_used);+ assert_eq!(15, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn active_status_only_usage_accounting_does_not_update_budget_limited_goals() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "stay stopped",+ crate::ThreadGoalStatus::BudgetLimited,+ /*token_budget*/ Some(20),+ )+ .await+ .expect("goal replacement should succeed");++ let outcome = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 5,+ /*token_delta*/ 5,+ ThreadGoalAccountingMode::ActiveStatusOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Unchanged(Some(goal)) = outcome else {+ panic!("budget-limited goal should not be updated");+ };+ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, goal.status);+ assert_eq!(0, goal.tokens_used);+ assert_eq!(0, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn stopped_usage_accounting_promotes_paused_goal_over_budget() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "stop before overrun",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(20),+ )+ .await+ .expect("goal replacement should succeed");+ runtime+ .update_thread_goal(+ thread_id,+ crate::ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Paused),+ token_budget: None,+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed");++ let outcome = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 3,+ /*token_delta*/ 25,+ ThreadGoalAccountingMode::ActiveOrStopped,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Updated(goal) = outcome else {+ panic!("stopped goal should account final usage");+ };+ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, goal.status);+ assert_eq!(25, goal.tokens_used);+ assert_eq!(3, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn budget_updates_immediately_stop_active_goals_already_over_budget() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "stay within budget",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(100),+ )+ .await+ .expect("goal replacement should succeed");+ runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 1,+ /*token_delta*/ 50,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");++ let lowered = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: None,+ token_budget: Some(Some(40)),+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("goal should exist");++ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, lowered.status);+ assert_eq!(Some(40), lowered.token_budget);+ assert_eq!(50, lowered.tokens_used);+ }++ #[tokio::test]+ async fn activating_goal_already_over_budget_keeps_it_budget_limited() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "stay within budget",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(40),+ )+ .await+ .expect("goal replacement should succeed");+ runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 1,+ /*token_delta*/ 50,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");++ let reactivated = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Active),+ token_budget: None,+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("goal should exist");++ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, reactivated.status);+ assert_eq!(Some(40), reactivated.token_budget);+ assert_eq!(50, reactivated.tokens_used);+ }++ #[tokio::test]+ async fn pausing_budget_limited_goal_preserves_terminal_status() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "stay within budget",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(40),+ )+ .await+ .expect("goal replacement should succeed");+ runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 1,+ /*token_delta*/ 50,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");++ let paused = runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Paused),+ token_budget: None,+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("goal should exist");++ assert_eq!(crate::ThreadGoalStatus::BudgetLimited, paused.status);+ assert_eq!(Some(40), paused.token_budget);+ assert_eq!(50, paused.tokens_used);+ }++ #[tokio::test]+ async fn usage_accounting_can_finalize_completed_goal_for_completing_turn() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "finish the report",+ crate::ThreadGoalStatus::Complete,+ /*token_budget*/ Some(1_000),+ )+ .await+ .expect("goal replacement should succeed");++ let active_only = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 30,+ /*token_delta*/ 200,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Unchanged(Some(goal)) = active_only else {+ panic!("completed goal should not be updated by active-only accounting");+ };+ assert_eq!(crate::ThreadGoalStatus::Complete, goal.status);+ assert_eq!(0, goal.tokens_used);+ assert_eq!(0, goal.time_used_seconds);++ let completing_turn = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 30,+ /*token_delta*/ 200,+ ThreadGoalAccountingMode::ActiveOrComplete,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Updated(goal) = completing_turn else {+ panic!("completed goal should be updated for final accounting");+ };+ assert_eq!(crate::ThreadGoalStatus::Complete, goal.status);+ assert_eq!(200, goal.tokens_used);+ assert_eq!(30, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn usage_accounting_can_finalize_stopped_goal_for_in_flight_turn() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "finish the report",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(1_000),+ )+ .await+ .expect("goal replacement should succeed");+ runtime+ .update_thread_goal(+ thread_id,+ ThreadGoalUpdate {+ status: Some(crate::ThreadGoalStatus::Paused),+ token_budget: None,+ expected_goal_id: None,+ },+ )+ .await+ .expect("goal update should succeed")+ .expect("goal should exist");++ let active_only = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 30,+ /*token_delta*/ 200,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Unchanged(Some(goal)) = active_only else {+ panic!("paused goal should not be updated by active-only accounting");+ };+ assert_eq!(crate::ThreadGoalStatus::Paused, goal.status);+ assert_eq!(0, goal.tokens_used);+ assert_eq!(0, goal.time_used_seconds);++ let in_flight_turn = runtime+ .account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 30,+ /*token_delta*/ 200,+ ThreadGoalAccountingMode::ActiveOrStopped,+ /*expected_goal_id*/ None,+ )+ .await+ .expect("usage accounting should succeed");+ let ThreadGoalAccountingOutcome::Updated(goal) = in_flight_turn else {+ panic!("stopped goal should be updated for in-flight accounting");+ };+ assert_eq!(crate::ThreadGoalStatus::Paused, goal.status);+ assert_eq!(200, goal.tokens_used);+ assert_eq!(30, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn usage_accounting_adds_concurrent_token_deltas() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "count every token",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ Some(1_000),+ )+ .await+ .expect("goal replacement should succeed");++ let first = runtime.account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 4,+ /*token_delta*/ 40,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ );+ let second = runtime.account_thread_goal_usage(+ thread_id,+ /*time_delta_seconds*/ 6,+ /*token_delta*/ 60,+ ThreadGoalAccountingMode::ActiveOnly,+ /*expected_goal_id*/ None,+ );+ let (first, second) = tokio::join!(first, second);+ first.expect("first usage accounting should succeed");+ second.expect("second usage accounting should succeed");++ let goal = runtime+ .get_thread_goal(thread_id)+ .await+ .expect("goal read should succeed")+ .expect("goal should exist");+ assert_eq!(100, goal.tokens_used);+ assert_eq!(10, goal.time_used_seconds);+ }++ #[tokio::test]+ async fn deleting_thread_deletes_goal() {+ let runtime = test_runtime().await;+ let thread_id = test_thread_id();+ upsert_test_thread(&runtime, thread_id).await;+ runtime+ .replace_thread_goal(+ thread_id,+ "clean up with the thread",+ crate::ThreadGoalStatus::Active,+ /*token_budget*/ None,+ )+ .await+ .expect("goal replacement should succeed");++ runtime+ .delete_thread(thread_id)+ .await+ .expect("thread deletion should succeed");++ assert_eq!(+ None,+ runtime+ .get_thread_goal(thread_id)+ .await+ .expect("goal read should succeed")+ );+ }+}