mirror of
https://github.com/openai/codex.git
synced 2026-05-28 06:55:01 +00:00
Adds the persisted goal foundation for the rest of the stack. This PR is intentionally limited to feature flag and state-layer behavior; app-server APIs, model tools, runtime continuation, and TUI UX are layered in later PRs. ## Why Goal mode needs durable thread-level state before clients or model tools can safely build on it. The state layer needs to know whether a goal exists, what objective it tracks, whether it is active, paused, budget-limited, or complete, and how much time/token usage has already been accounted. ## What changed - Added the `goals` feature flag and generated config schema entry. - Added the `thread_goals` state table and Rust model for persisted thread goals. - Added state runtime APIs for creating, replacing, updating, deleting, and accounting goal usage. - Added `goal_id`-based stale update protection so an old goal update cannot overwrite a replacement. - Kept this PR scoped to persistence and state runtime behavior, with no app-server, model-facing, continuation, or TUI behavior yet. ## Verification - Added state runtime coverage for goal creation, replacement, stale update protection, status transitions, token-budget behavior, and usage accounting.
110 lines
3.0 KiB
Rust
110 lines
3.0 KiB
Rust
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)?,
|
|
})
|
|
}
|
|
}
|