mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
608b8b1cc6
## Why - Currently, there is no analytics event for `/goal` behavior - Existing events cannot identify goal execution or its resulting outcome - The original update in [#26182](https://github.com/openai/codex/pull/26182) was implemented before `/goal` moved into `codex-goal-extension`. ## What Changed - Adds `codex_goal_event` serialization and enrichment to `codex-analytics` - Emits goal events from the canonical `codex-goal-extension` mutation and accounting paths: - `created` when a new logical goal is persisted - `usage_accounted` when cumulative goal usage is persisted - `status_changed` when the stored goal status changes - `cleared` when the goal is deleted - Preserves causal `turn_id` for turn driven events and uses null attribution for external or idle lifecycle events - Changes goal deletion to return the deleted row so `cleared` retains the stable goal ID ## Event Details Includes standard analytics metadata along with goal specific fields: - `goal_id`: Stable ID stored in the local SQLite goal row and shared across the goal's events - `event_kind`: Observed operation (see the 4 lifecycle events cited in the above bullet) - `goal_status`: Resulting or last stored status: `active`, `paused`, `blocked`, `usage_limited`, etc. - `has_token_budget`: Indicates whether a token budget is configured - `turn_id`: Causal turn ID, or null when no causal turn exists - `cumulative_tokens_accounted`: Cumulative tokens on `usage_accounted` events; null otherwise - `cumulative_time_accounted_seconds`: Cumulative active time on `usage_accounted` events; null otherwise ## Validation - `just test -p codex-analytics -p codex-state -p codex-goal-extension` - `just test -p codex-core -E 'test(/goal/)'` - `just test -p codex-app-server` - `cargo build -p codex-analytics -p codex-core -p codex-state -p codex-app-server`
118 lines
3.3 KiB
Rust
118 lines
3.3 KiB
Rust
use anyhow::Result;
|
|
use anyhow::anyhow;
|
|
use chrono::DateTime;
|
|
use chrono::Utc;
|
|
use codex_protocol::ThreadId;
|
|
use serde::Serialize;
|
|
use sqlx::Row;
|
|
use sqlx::sqlite::SqliteRow;
|
|
|
|
use super::epoch_millis_to_datetime;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum ThreadGoalStatus {
|
|
Active,
|
|
Paused,
|
|
Blocked,
|
|
UsageLimited,
|
|
BudgetLimited,
|
|
Complete,
|
|
}
|
|
|
|
impl ThreadGoalStatus {
|
|
pub fn as_str(self) -> &'static str {
|
|
match self {
|
|
Self::Active => "active",
|
|
Self::Paused => "paused",
|
|
Self::Blocked => "blocked",
|
|
Self::UsageLimited => "usage_limited",
|
|
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),
|
|
"blocked" => Ok(Self::Blocked),
|
|
"usage_limited" => Ok(Self::UsageLimited),
|
|
"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)?,
|
|
})
|
|
}
|
|
}
|